Skip to content

Commit 36259b4

Browse files
avagingvisor-bot
authored andcommitted
kvm: don't fail if a mapped virtual region is on a few phys regions
PiperOrigin-RevId: 766433699
1 parent a6a13e8 commit 36259b4

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

pkg/sentry/platform/kvm/machine_amd64.go

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,31 @@ func (c *vCPU) SwitchToUser(switchOpts ring0.SwitchOpts, info *linux.SignalInfo)
496496
}
497497
}
498498

499+
func (m *machine) mapUpperHalfRegion(
500+
pageTable *pagetables.PageTables,
501+
virtual uintptr, length uintptr,
502+
opts pagetables.MapOpts,
503+
) {
504+
for length != 0 {
505+
physical, plength, ok := translateToPhysical(virtual)
506+
if !ok || plength == 0 {
507+
panic(fmt.Sprintf("impossible translation: virtual %x length %x", virtual, length))
508+
}
509+
if plength > length {
510+
plength = length
511+
}
512+
513+
pageTable.Map(
514+
hostarch.Addr(ring0.KernelStartAddress|virtual),
515+
plength,
516+
opts,
517+
physical)
518+
519+
length -= plength
520+
virtual += plength
521+
}
522+
}
523+
499524
func (m *machine) mapUpperHalf(pageTable *pagetables.PageTables) {
500525
// Map all the executable regions so that all the entry functions
501526
// are mapped in the upper half.
@@ -506,30 +531,16 @@ func (m *machine) mapUpperHalf(pageTable *pagetables.PageTables) {
506531

507532
if vr.accessType.Execute {
508533
r := vr.region
509-
physical, length, ok := translateToPhysical(r.virtual)
510-
if !ok || length < r.length {
511-
panic("impossible translation")
512-
}
513-
pageTable.Map(
514-
hostarch.Addr(ring0.KernelStartAddress|r.virtual),
515-
r.length,
516-
pagetables.MapOpts{AccessType: hostarch.Execute, Global: true},
517-
physical)
534+
m.mapUpperHalfRegion(pageTable, r.virtual, r.length,
535+
pagetables.MapOpts{AccessType: hostarch.Execute, Global: true})
518536
}
519537
}); err != nil {
520538
panic(fmt.Sprintf("error parsing /proc/self/maps: %v", err))
521539
}
522540
for start, end := range m.kernel.EntryRegions() {
523541
regionLen := end - start
524-
physical, length, ok := translateToPhysical(start)
525-
if !ok || length < regionLen {
526-
panic("impossible translation")
527-
}
528-
pageTable.Map(
529-
hostarch.Addr(ring0.KernelStartAddress|start),
530-
regionLen,
531-
pagetables.MapOpts{AccessType: hostarch.ReadWrite, Global: true},
532-
physical)
542+
m.mapUpperHalfRegion(pageTable, start, regionLen,
543+
pagetables.MapOpts{AccessType: hostarch.ReadWrite, Global: true})
533544
}
534545
}
535546

0 commit comments

Comments
 (0)