| 1 | Subject: [PATCH] Expose WHPX-supported SSE features in the Windows qemu64 fallback |
| 2 | |
| 3 | Keep the explicit CPU model and newer QEMU host passthrough. On Windows |
| 4 | x64, use WHvGetCapability to select additional qemu64 features. |
| 5 | |
| 6 | |
| 7 | new file mode 100644 |
| 8 | |
| 9 | |
| 10 | |
| 11 | @@ -0,0 +1,9 @@ |
| 12 | +// SPDX-FileCopyrightText: Copyright The Lima Authors |
| 13 | +// SPDX-License-Identifier: Apache-2.0 |
| 14 | + |
| 15 | +//go:build !windows || !amd64 |
| 16 | + |
| 17 | +package qemu |
| 18 | + |
| 19 | +// Only the Windows amd64 driver defaults to qemu64. |
| 20 | +func whpxQEMU64() string { return "qemu64" } |
| 21 | |
| 22 | new file mode 100644 |
| 23 | |
| 24 | |
| 25 | |
| 26 | @@ -0,0 +1,50 @@ |
| 27 | +// SPDX-FileCopyrightText: Copyright The Lima Authors |
| 28 | +// SPDX-License-Identifier: Apache-2.0 |
| 29 | + |
| 30 | +package qemu |
| 31 | + |
| 32 | +import ( |
| 33 | + "unsafe" |
| 34 | + |
| 35 | + "github.com/sirupsen/logrus" |
| 36 | + "golang.org/x/sys/windows" |
| 37 | +) |
| 38 | + |
| 39 | +func whpxQEMU64() string { |
| 40 | + // Query the processor features available to Windows Hypervisor Platform |
| 41 | + // guests. A nested hypervisor may expose a subset of the host's CPUID. |
| 42 | + query := windows.NewLazySystemDLL("WinHvPlatform.dll").NewProc("WHvGetCapability") |
| 43 | + if err := query.Find(); err != nil { |
| 44 | + logrus.WithError(err).Warn("Cannot query WHPX processor features; using qemu64 defaults") |
| 45 | + return "qemu64" |
| 46 | + } |
| 47 | + const processorFeatures = 0x1001 // WHvCapabilityCodeProcessorFeatures |
| 48 | + var bits uint64 |
| 49 | + var written uint32 |
| 50 | + status, _, _ := query.Call(processorFeatures, uintptr(unsafe.Pointer(&bits)), |
| 51 | + unsafe.Sizeof(bits), uintptr(unsafe.Pointer(&written))) |
| 52 | + if int32(status) < 0 || uintptr(written) != unsafe.Sizeof(bits) { |
| 53 | + logrus.Warnf("Cannot query WHPX processor features (HRESULT %#x, %d bytes); using qemu64 defaults", uint32(status), written) |
| 54 | + return "qemu64" |
| 55 | + } |
| 56 | + return whpxQEMU64Model(bits) |
| 57 | +} |
| 58 | + |
| 59 | +// Bit positions are from the Windows SDK's WHV_X64_PROCESSOR_FEATURES. |
| 60 | +func whpxQEMU64Model(bits uint64) string { |
| 61 | + model := "qemu64" |
| 62 | + for _, feature := range []struct { |
| 63 | + mask uint64 |
| 64 | + flag string |
| 65 | + }{ |
| 66 | + {1 << 2, "+ssse3"}, |
| 67 | + {1 << 3, "+sse4.1"}, |
| 68 | + {1 << 4, "+sse4.2"}, |
| 69 | + {1 << 7, "+popcnt"}, |
| 70 | + } { |
| 71 | + if bits&feature.mask != 0 { |
| 72 | + model += "," + feature.flag |
| 73 | + } |
| 74 | + } |
| 75 | + return model |
| 76 | +} |
| 77 | |
| 78 | new file mode 100644 |
| 79 | |
| 80 | |
| 81 | |
| 82 | @@ -0,0 +1,44 @@ |
| 83 | +// SPDX-FileCopyrightText: Copyright The Lima Authors |
| 84 | +// SPDX-License-Identifier: Apache-2.0 |
| 85 | + |
| 86 | +package qemu |
| 87 | + |
| 88 | +import ( |
| 89 | + "testing" |
| 90 | + |
| 91 | + "github.com/coreos/go-semver/semver" |
| 92 | + |
| 93 | + "github.com/lima-vm/lima/v2/pkg/limatype" |
| 94 | +) |
| 95 | + |
| 96 | +func TestWHPXQEMU64Features(t *testing.T) { |
| 97 | + for bits, want := range map[uint64]string{ |
| 98 | + 0: "qemu64", |
| 99 | + 0x60: "qemu64", // AMD SSE4a and XOP do not imply SSE4.2. |
| 100 | + 0x0c: "qemu64,+ssse3,+sse4.1", |
| 101 | + 0x9c: "qemu64,+ssse3,+sse4.1,+sse4.2,+popcnt", |
| 102 | + } { |
| 103 | + if got := whpxQEMU64Model(bits); got != want { |
| 104 | + t.Errorf("WHPX feature mask %#x: got %q, want %q", bits, got, want) |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func TestWindowsCPUResolution(t *testing.T) { |
| 110 | + arch := limatype.X8664 |
| 111 | + y := limatype.LimaYAML{Arch: &arch} |
| 112 | + // New and existing instances with no explicit CPU override both resolve |
| 113 | + // against the current host each time their QEMU command is generated. |
| 114 | + if got, want := resolveCPUType(&y, semver.New("11.1.0")), whpxQEMU64(); got != want { |
| 115 | + t.Fatalf("default for QEMU 11.1.0: got %q, want %q", got, want) |
| 116 | + } |
| 117 | + if got := resolveCPUType(&y, semver.New("11.1.1")); got != "host" { |
| 118 | + t.Fatalf("default for QEMU 11.1.1: got %q", got) |
| 119 | + } |
| 120 | + for _, override := range []string{"qemu64", "qemu64,+sse4.2", "Nehalem"} { |
| 121 | + y.VMOpts = limatype.VMOpts{limatype.QEMU: limatype.QEMUOpts{CPUType: limatype.CPUType{arch: override}}} |
| 122 | + if got := resolveCPUType(&y, semver.New("11.1.0")); got != override { |
| 123 | + t.Fatalf("explicit CPU type %q changed to %q", override, got) |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | @@ -458,6 +458,11 @@ func resolveCPUType(y *limatype.LimaYAML, qemuVersion *semver.Version) string { |
| 132 | if err := limayaml.Convert(y.VMOpts[limatype.QEMU], &qemuOpts, "vmOpts.qemu"); err != nil { |
| 133 | logrus.WithError(err).Warnf("Couldn't convert %#q", y.VMOpts[limatype.QEMU]) |
| 134 | } |
| 135 | + // WHPX uses qemu64 when host passthrough is unavailable. Preserve an |
| 136 | + // explicit CPU model, including an explicitly requested bare qemu64. |
| 137 | + if cpuType[*y.Arch] == "qemu64" && qemuOpts.CPUType[*y.Arch] == "" { |
| 138 | + cpuType[*y.Arch] = whpxQEMU64() |
| 139 | + } |
| 140 | for k, v := range qemuOpts.CPUType { |
| 141 | if !slices.Contains(limatype.ArchTypes, *y.Arch) { |
| 142 | logrus.Warnf("field `vmOpts.qemu.cpuType` uses unsupported arch %#q", k) |
| 143 | @@ -1407,7 +1412,7 @@ func hasHostCPU(qemuVersion *semver.Version) bool { |
| 144 | return true |
| 145 | case "windows": |
| 146 | // Should have been 11.1.0, but it contained a regression when running in nested virt |
| 147 | - if qemuVersion.LessThan(*semver.New("11.1.1")) { |
| 148 | + if qemuVersion == nil || qemuVersion.LessThan(*semver.New("11.1.1")) { |
| 149 | return false |
| 150 | } |
| 151 | return true |