Skip to content

Commit

Permalink
feat(wsl): support detection of current system has virtualization ena…
Browse files Browse the repository at this point in the history
…bled (#75)

Signed-off-by: Kevin Cui <[email protected]>
  • Loading branch information
BlackHole1 authored Dec 24, 2024
1 parent 1ef7e64 commit c3eb16f
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 9 deletions.
11 changes: 6 additions & 5 deletions pkg/ipc/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ type prepare string
const (
SystemNotSupport prepare = "SystemNotSupport"

NeedEnableFeature prepare = "NeedEnableFeature"
EnableFeaturing prepare = "EnableFeaturing"
EnableFeatureFailed prepare = "EnableFeatureFailed"
EnableFeatureSuccess prepare = "EnableFeatureSuccess"
NeedReboot prepare = "NeedReboot"
NotSupportVirtualization prepare = "NotSupportVirtualization"
NeedEnableFeature prepare = "NeedEnableFeature"
EnableFeaturing prepare = "EnableFeaturing"
EnableFeatureFailed prepare = "EnableFeatureFailed"
EnableFeatureSuccess prepare = "EnableFeatureSuccess"
NeedReboot prepare = "NeedReboot"

NeedUpdateWSL prepare = "NeedUpdateWSL"
UpdatingWSL prepare = "UpdatingWSL"
Expand Down
5 changes: 5 additions & 0 deletions pkg/winapi/kernel32.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ func AttachConsole(pid uintptr) error {

return nil
}

func IsProcessorFeaturePresent(feature int) bool {
ret, _, _ := isProcessorFeaturePresent.Call(uintptr(feature))
return ret != 0
}
19 changes: 19 additions & 0 deletions pkg/winapi/sys/processthreadsapi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package sys

import (
"github.com/oomol-lab/ovm-win/pkg/winapi"
)

// https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-isprocessorfeaturepresent
const (
PF_SECOND_LEVEL_ADDRESS_TRANSLATION = 20
PF_VIRT_FIRMWARE_ENABLED = 21
)

// IsSupportedVirtualization checks if the system supports virtualization
func IsSupportedVirtualization() (vf, slat bool) {
vf = winapi.IsProcessorFeaturePresent(PF_VIRT_FIRMWARE_ENABLED)
slat = winapi.IsProcessorFeaturePresent(PF_SECOND_LEVEL_ADDRESS_TRANSLATION)

return
}
10 changes: 6 additions & 4 deletions pkg/winapi/winapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ var (
kernel32 *windows.LazyDLL
mpr *windows.LazyDLL

shellExecuteEx *windows.LazyProc
freeConsole *windows.LazyProc
attachConsole *windows.LazyProc
wNetGetUniversalName *windows.LazyProc
shellExecuteEx *windows.LazyProc
freeConsole *windows.LazyProc
attachConsole *windows.LazyProc
isProcessorFeaturePresent *windows.LazyProc
wNetGetUniversalName *windows.LazyProc
)

func init() {
Expand All @@ -31,6 +32,7 @@ func init() {
shellExecuteEx = shell32.NewProc("ShellExecuteExW")
freeConsole = kernel32.NewProc("FreeConsole")
attachConsole = kernel32.NewProc("AttachConsole")
isProcessorFeaturePresent = kernel32.NewProc("IsProcessorFeaturePresent")
wNetGetUniversalName = mpr.NewProc("WNetGetUniversalNameW")
}

Expand Down
23 changes: 23 additions & 0 deletions pkg/wsl/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/oomol-lab/ovm-win/pkg/logger"
"github.com/oomol-lab/ovm-win/pkg/types"
"github.com/oomol-lab/ovm-win/pkg/util"
"github.com/oomol-lab/ovm-win/pkg/winapi/sys"
)

var (
Expand All @@ -24,6 +25,13 @@ var (

func Check(opt *types.PrepareOpt) {
log := opt.Logger

if !isSupportedVirtualization(log) {
log.Info("Virtualization is not supported")
event.NotifyPrepare(event.NotSupportVirtualization)
return
}

if isEnabled := isFeatureEnabled(log); !isEnabled {
log.Info("WSL2 feature is not enabled")
event.NotifyPrepare(event.NeedEnableFeature)
Expand Down Expand Up @@ -51,6 +59,21 @@ func Check(opt *types.PrepareOpt) {
return
}

func isSupportedVirtualization(log *logger.Context) bool {
vf, slat := sys.IsSupportedVirtualization()
// If the CPU does not support SLAT, WSL2 cannot be started (but WSL1 can be started).
// In modern CPUs, almost all CPUs support SLAT.
// It is not possible to strictly determine this through `vf && slat`, because in VMware, SLAT is always false (even if "Virtualize Intel VT-x/EPT or AMD-V/RVI" is checked).
// See:
// https://github.com/microsoft/WSL/issues/4709
// https://www.reddit.com/r/bashonubuntuonwindows/comments/izf4qp/cpus_without_slat_capability_cant_run_wsl_2/
if !slat {
log.Warn("SLAT is not supported")
}

return vf
}

func existsKernel() bool {
// from `MSI` or `Windows Update`
if system32, ok := util.System32Root(); ok {
Expand Down

0 comments on commit c3eb16f

Please sign in to comment.