Skip to content

Commit 7cac7aa

Browse files
committed
target/i386/hvf: fix handling of XSAVE-related CPUID bits
The call to xgetbv() is passing the ecx value for cpuid function 0xD, index 0. The xgetbv call thus returns false (OSXSAVE is bit 27, which is well out of the range of CPUID[0xD,0].ECX) and eax is not modified. While fixing it, cache the whole computation of supported XCR0 bits since it will be used for more than just CPUID leaf 0xD. Furthermore, unsupported subleafs of CPUID 0xD (including all those corresponding to zero bits in host's XCR0) must be hidden; if OSXSAVE is not set at all, the whole of CPUID leaf 0xD plus the XSAVE bit must be hidden. Finally, unconditionally drop XSTATE_BNDREGS_MASK and XSTATE_BNDCSR_MASK; real hardware will only show them if the MPX bit is set in CPUID; this is never the case for hvf_get_supported_cpuid() because QEMU's Hypervisor.framework support does not handle the VMX fields related to MPX (even in the unlikely possibility that the host has MPX enabled). So hide those bits in the new cache_host_xcr0(). Cc: Phil Dennis-Jordan <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 9c07a7a commit 7cac7aa

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

host/include/i386/host/cpuinfo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
/* Digested version of <cpuid.h> */
1010

1111
#define CPUINFO_ALWAYS (1u << 0) /* so cpuinfo is nonzero */
12+
#define CPUINFO_OSXSAVE (1u << 1)
1213
#define CPUINFO_MOVBE (1u << 2)
1314
#define CPUINFO_LZCNT (1u << 3)
1415
#define CPUINFO_POPCNT (1u << 4)

target/i386/hvf/x86_cpuid.c

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,38 @@
2121
*/
2222

2323
#include "qemu/osdep.h"
24+
#include "qemu/cpuid.h"
25+
#include "host/cpuinfo.h"
2426
#include "cpu.h"
2527
#include "x86.h"
2628
#include "vmx.h"
2729
#include "sysemu/hvf.h"
2830
#include "hvf-i386.h"
2931

30-
static bool xgetbv(uint32_t cpuid_ecx, uint32_t idx, uint64_t *xcr)
32+
static bool cached_xcr0;
33+
static uint64_t supported_xcr0;
34+
35+
static void cache_host_xcr0()
3136
{
32-
uint32_t xcrl, xcrh;
37+
if (cached_xcr0) {
38+
return;
39+
}
3340

34-
if (cpuid_ecx & CPUID_EXT_OSXSAVE) {
35-
/*
36-
* The xgetbv instruction is not available to older versions of
37-
* the assembler, so we encode the instruction manually.
38-
*/
39-
asm(".byte 0x0f, 0x01, 0xd0" : "=a" (xcrl), "=d" (xcrh) : "c" (idx));
41+
if (cpuinfo & CPUINFO_OSXSAVE) {
42+
uint64_t host_xcr0 = xgetbv_low(0);
4043

41-
*xcr = (((uint64_t)xcrh) << 32) | xcrl;
42-
return true;
44+
/* Only show xcr0 bits corresponding to usable features. */
45+
supported_xcr0 = host_xcr0 & (XSTATE_FP_MASK |
46+
XSTATE_SSE_MASK | XSTATE_YMM_MASK |
47+
XSTATE_OPMASK_MASK | XSTATE_ZMM_Hi256_MASK |
48+
XSTATE_Hi16_ZMM_MASK);
49+
if ((supported_xcr0 & (XSTATE_FP_MASK | XSTATE_SSE_MASK)) !=
50+
(XSTATE_FP_MASK | XSTATE_SSE_MASK)) {
51+
supported_xcr0 = 0;
52+
}
4353
}
4454

45-
return false;
55+
cached_xcr0 = true;
4656
}
4757

4858
uint32_t hvf_get_supported_cpuid(uint32_t func, uint32_t idx,
@@ -51,6 +61,7 @@ uint32_t hvf_get_supported_cpuid(uint32_t func, uint32_t idx,
5161
uint64_t cap;
5262
uint32_t eax, ebx, ecx, edx;
5363

64+
cache_host_xcr0();
5465
host_cpuid(func, idx, &eax, &ebx, &ecx, &edx);
5566

5667
switch (func) {
@@ -66,7 +77,8 @@ uint32_t hvf_get_supported_cpuid(uint32_t func, uint32_t idx,
6677
ecx &= CPUID_EXT_SSE3 | CPUID_EXT_PCLMULQDQ | CPUID_EXT_SSSE3 |
6778
CPUID_EXT_FMA | CPUID_EXT_CX16 | CPUID_EXT_PCID |
6879
CPUID_EXT_SSE41 | CPUID_EXT_SSE42 | CPUID_EXT_MOVBE |
69-
CPUID_EXT_POPCNT | CPUID_EXT_AES | CPUID_EXT_XSAVE |
80+
CPUID_EXT_POPCNT | CPUID_EXT_AES |
81+
(supported_xcr0 ? CPUID_EXT_XSAVE : 0) |
7082
CPUID_EXT_AVX | CPUID_EXT_F16C | CPUID_EXT_RDRAND;
7183
ecx |= CPUID_EXT_HYPERVISOR;
7284
break;
@@ -107,16 +119,14 @@ uint32_t hvf_get_supported_cpuid(uint32_t func, uint32_t idx,
107119
eax = 0;
108120
break;
109121
case 0xD:
122+
if (!supported_xcr0 ||
123+
(idx > 1 && !(supported_xcr0 & (1 << idx)))) {
124+
eax = ebx = ecx = edx = 0;
125+
break;
126+
}
127+
110128
if (idx == 0) {
111-
uint64_t host_xcr0;
112-
if (xgetbv(ecx, 0, &host_xcr0)) {
113-
uint64_t supp_xcr0 = host_xcr0 & (XSTATE_FP_MASK |
114-
XSTATE_SSE_MASK | XSTATE_YMM_MASK |
115-
XSTATE_BNDREGS_MASK | XSTATE_BNDCSR_MASK |
116-
XSTATE_OPMASK_MASK | XSTATE_ZMM_Hi256_MASK |
117-
XSTATE_Hi16_ZMM_MASK);
118-
eax &= supp_xcr0;
119-
}
129+
eax = supported_xcr0;
120130
} else if (idx == 1) {
121131
hv_vmx_read_capability(HV_VMX_CAP_PROCBASED2, &cap);
122132
eax &= CPUID_XSAVE_XSAVEOPT | CPUID_XSAVE_XGETBV1;

util/cpuinfo-i386.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ unsigned __attribute__((constructor)) cpuinfo_init(void)
3535
__cpuid(1, a, b, c, d);
3636

3737
info |= (d & bit_SSE2 ? CPUINFO_SSE2 : 0);
38+
info |= (c & bit_OSXSAVE ? CPUINFO_OSXSAVE : 0);
3839
info |= (c & bit_MOVBE ? CPUINFO_MOVBE : 0);
3940
info |= (c & bit_POPCNT ? CPUINFO_POPCNT : 0);
4041
info |= (c & bit_PCLMUL ? CPUINFO_PCLMUL : 0);

0 commit comments

Comments
 (0)