跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

0019-whpx-qemu64-cpu-features.patch

151 行 · 5.2 KB历史下载
1Subject: [PATCH] Expose WHPX-supported SSE features in the Windows qemu64 fallback
2
3Keep the explicit CPU model and newer QEMU host passthrough. On Windows
4x64, use WHvGetCapability to select additional qemu64 features.
5
6diff --git a/pkg/driver/qemu/cpu_other.go b/pkg/driver/qemu/cpu_other.go
7new file mode 100644
8index 0000000..99a48c3
9--- /dev/null
10+++ b/pkg/driver/qemu/cpu_other.go
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" }
21diff --git a/pkg/driver/qemu/cpu_windows_amd64.go b/pkg/driver/qemu/cpu_windows_amd64.go
22new file mode 100644
23index 0000000..77bdb8c
24--- /dev/null
25+++ b/pkg/driver/qemu/cpu_windows_amd64.go
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+}
77diff --git a/pkg/driver/qemu/cpu_windows_amd64_test.go b/pkg/driver/qemu/cpu_windows_amd64_test.go
78new file mode 100644
79index 0000000..d4e0f08
80--- /dev/null
81+++ b/pkg/driver/qemu/cpu_windows_amd64_test.go
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+}
127diff --git a/pkg/driver/qemu/qemu.go b/pkg/driver/qemu/qemu.go
128index 2dba8c5..e70322a 100644
129--- a/pkg/driver/qemu/qemu.go
130+++ b/pkg/driver/qemu/qemu.go
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