Files
Reims-Go/internal/host/whpx_probe_windows.go
T

125 lines
3.7 KiB
Go

//go:build windows
package host
import (
"fmt"
"syscall"
"time"
"unsafe"
)
const (
// WinHvPlatformDefs.h capability & partition property codes.
whvCapFeatures = 0x00000001
whvCapProcessorPerfmon = 0x00001009
whvPropNestedVirtualization = 0x00000004
whvPropLocalApicEmulationMode = 0x00001005
whvPropProcessorCount = 0x00001fff
whvLocalApicModeX2Apic = 2
processQueryLimitedInfo = 0x1000
)
var procGetProcessTimes = kernel32.NewProc("GetProcessTimes")
// probeWHPXCapabilities checks WHPX kernel-irqchip and nested-virtualization
// partition property support on a throwaway WHP partition without starting any
// vCPU or requiring administrator privileges.
func probeWHPXCapabilities() WHPXCapabilities {
var caps WHPXCapabilities
mod := syscall.NewLazyDLL("WinHvPlatform.dll")
if err := mod.Load(); err != nil {
return caps
}
getCap := mod.NewProc("WHvGetCapability")
createPart := mod.NewProc("WHvCreatePartition")
deletePart := mod.NewProc("WHvDeletePartition")
setProp := mod.NewProc("WHvSetPartitionProperty")
if getCap.Find() != nil || createPart.Find() != nil || deletePart.Find() != nil || setProp.Find() != nil {
return caps
}
var perfmon uint64
var written uint32
hr, _, _ := getCap.Call(
uintptr(whvCapProcessorPerfmon),
uintptr(unsafe.Pointer(&perfmon)),
uintptr(unsafe.Sizeof(perfmon)),
uintptr(unsafe.Pointer(&written)),
)
caps.ModernOS = int32(hr) >= 0
var features uint64
hr, _, _ = getCap.Call(
uintptr(whvCapFeatures),
uintptr(unsafe.Pointer(&features)),
uintptr(unsafe.Sizeof(features)),
uintptr(unsafe.Pointer(&written)),
)
hasApicBit := int32(hr) >= 0 && (features&(1<<1)) != 0
hasApicProcs := mod.NewProc("WHvSetVirtualProcessorInterruptControllerState2").Find() == nil &&
mod.NewProc("WHvRequestInterrupt").Find() == nil
caps.APICEmulation = hasApicBit && hasApicProcs
var part uintptr
hr, _, _ = createPart.Call(uintptr(unsafe.Pointer(&part)))
if int32(hr) < 0 || part == 0 {
return caps
}
defer deletePart.Call(part)
var propBuf [24]byte
// ProcessorCount = 1
propBuf[0] = 1
hr, _, _ = setProp.Call(part, uintptr(whvPropProcessorCount), uintptr(unsafe.Pointer(&propBuf[0])), uintptr(len(propBuf)))
if int32(hr) < 0 {
return caps
}
if caps.APICEmulation {
var mode uint32 = whvLocalApicModeX2Apic
hr, _, _ = setProp.Call(part, uintptr(whvPropLocalApicEmulationMode), uintptr(unsafe.Pointer(&mode)), uintptr(unsafe.Sizeof(mode)))
caps.KernelIrqchipAvailable = int32(hr) >= 0
}
// Probe WHvPartitionPropertyCodeNestedVirtualization (same property that
// fails with hr=0x80370302 WHV_E_UNKNOWN_PROPERTY on some Windows 11 builds).
var nestedBuf [24]byte
nestedBuf[0] = 1
hr, _, _ = setProp.Call(part, uintptr(whvPropNestedVirtualization), uintptr(unsafe.Pointer(&nestedBuf[0])), uintptr(len(nestedBuf)))
if int32(hr) >= 0 {
caps.NestedVirtProperty = true
} else {
caps.NestedVirtProbeError = fmt.Sprintf("0x%08x", uint32(hr))
}
return caps
}
// ProcessCPUTime returns total kernel + user CPU time consumed by pid.
func ProcessCPUTime(pid uint32) (time.Duration, bool) {
if pid == 0 {
return 0, false
}
h, _, _ := procOpenProcess.Call(uintptr(processQueryLimitedInfo), 0, uintptr(pid))
if h == 0 {
return 0, false
}
defer procCloseHandle.Call(h)
var creation, exit, kernel, user syscall.Filetime
r, _, _ := procGetProcessTimes.Call(
h,
uintptr(unsafe.Pointer(&creation)),
uintptr(unsafe.Pointer(&exit)),
uintptr(unsafe.Pointer(&kernel)),
uintptr(unsafe.Pointer(&user)),
)
if r == 0 {
return 0, false
}
total100ns := (uint64(kernel.HighDateTime)<<32 | uint64(kernel.LowDateTime)) +
(uint64(user.HighDateTime)<<32 | uint64(user.LowDateTime))
return time.Duration(total100ns) * 100 * time.Nanosecond, true
}