Skip to content

Commit

Permalink
Check for support for xgetbv
Browse files Browse the repository at this point in the history
Fixes #5738

PiperOrigin-RevId: 413054140
  • Loading branch information
Ian Lewis authored and gvisor-bot committed Nov 30, 2021
1 parent 91d4826 commit afd549d
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions pkg/ring0/kernel_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"reflect"
"sync"

"gvisor.dev/gvisor/pkg/cpuid"
"gvisor.dev/gvisor/pkg/hostarch"
"gvisor.dev/gvisor/pkg/sentry/arch"
)
Expand Down Expand Up @@ -267,14 +268,20 @@ func doSwitchToUser(
needIRET uint64) Vector // +32(FP), +40(FP)

var (
sentryXCR0 uintptr
sentryXCR0Once sync.Once
sentryXCR0 uintptr
xgetbvIsSupported bool
sentryXCR0Once sync.Once
)

// initSentryXCR0 saves a value of XCR0 in the host mode. It is used to
// initialize XCR0 of guest vCPU-s.
func initSentryXCR0() {
sentryXCR0Once.Do(func() { sentryXCR0 = xgetbv(0) })
sentryXCR0Once.Do(func() {
if cpuid.HostFeatureSet().HasFeature(cpuid.X86FeatureXSAVE) {
sentryXCR0 = xgetbv(0)
xgetbvIsSupported = true
}
})
}

// startGo is the CPU entrypoint.
Expand Down Expand Up @@ -303,7 +310,9 @@ func startGo(c *CPU) {
fninit()
// Need to sync XCR0 with the host, because xsave and xrstor can be
// called from different contexts.
xsetbv(0, sentryXCR0)
if xgetbvIsSupported {
xsetbv(0, sentryXCR0)
}

// Set the syscall target.
wrmsr(_MSR_LSTAR, kernelFunc(addrOfSysenter()))
Expand Down

0 comments on commit afd549d

Please # to comment.