github.com/nicocha30/gvisor-ligolo@v0.0.0-20230726075806-989fa2c0a413/pkg/sentry/platform/kvm/bluepill_allocator.go (about) 1 // Copyright 2018 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package kvm 16 17 import ( 18 "fmt" 19 20 "github.com/nicocha30/gvisor-ligolo/pkg/ring0/pagetables" 21 ) 22 23 type allocator struct { 24 base pagetables.RuntimeAllocator 25 26 // cpu must be set prior to any pagetable operation. 27 // 28 // Due to the way KVM's shadow paging implementation works, 29 // modifications to the page tables while in host mode may not be 30 // trapped, leading to the shadow pages being out of sync. Therefore, 31 // we need to ensure that we are in guest mode for page table 32 // modifications. See the call to bluepill, below. 33 cpu *vCPU 34 } 35 36 // newAllocator is used to define the allocator. 37 func newAllocator() *allocator { 38 a := new(allocator) 39 a.base.Init() 40 return a 41 } 42 43 // NewPTEs implements pagetables.Allocator.NewPTEs. 44 // 45 // +checkescape:all 46 // 47 //go:nosplit 48 func (a *allocator) NewPTEs() *pagetables.PTEs { 49 ptes := a.base.NewPTEs() // escapes: bluepill below. 50 if a.cpu != nil { 51 bluepill(a.cpu) 52 } 53 return ptes 54 } 55 56 // PhysicalFor returns the physical address for a set of PTEs. 57 // 58 // +checkescape:all 59 // 60 //go:nosplit 61 func (a *allocator) PhysicalFor(ptes *pagetables.PTEs) uintptr { 62 virtual := a.base.PhysicalFor(ptes) 63 physical, _, ok := translateToPhysical(virtual) 64 if !ok { 65 panic(fmt.Sprintf("PhysicalFor failed for %p", ptes)) // escapes: panic. 66 } 67 return physical 68 } 69 70 // LookupPTEs implements pagetables.Allocator.LookupPTEs. 71 // 72 // +checkescape:all 73 // 74 //go:nosplit 75 func (a *allocator) LookupPTEs(physical uintptr) *pagetables.PTEs { 76 virtualStart, physicalStart, _, pr := calculateBluepillFault(physical, physicalRegions) 77 if pr == nil { 78 panic(fmt.Sprintf("LookupPTEs failed for 0x%x", physical)) // escapes: panic. 79 } 80 return a.base.LookupPTEs(virtualStart + (physical - physicalStart)) 81 } 82 83 // FreePTEs implements pagetables.Allocator.FreePTEs. 84 // 85 // +checkescape:all 86 // 87 //go:nosplit 88 func (a *allocator) FreePTEs(ptes *pagetables.PTEs) { 89 a.base.FreePTEs(ptes) // escapes: bluepill below. 90 if a.cpu != nil { 91 bluepill(a.cpu) 92 } 93 } 94 95 // Recycle implements pagetables.Allocator.Recycle. 96 // 97 //go:nosplit 98 func (a *allocator) Recycle() { 99 a.base.Recycle() 100 }