hvf: guard xgetbv call.
PATCH 1/1 hvf: guard xgetbv call.
This prevents illegal instruction on cpus do not support xgetbv.
--- target/i386/hvf/x86_cpuid.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
target/i386/hvf/x86_cpuid.c +8 -3
@@ -100,11 +100,16 @@ uint32_t hvf_get_supported_cpuid(uint32_t func, uint32_t idx,break ;case 0xD :if (idx ==0 ) { -uint64_t host_xcr0 =xgetbv (0 ); -uint64_t supp_xcr0 = host_xcr0 & (XSTATE_FP_MASK |XSTATE_SSE_MASK | +uint64_t supp_xcr0 =XSTATE_FP_MASK |XSTATE_SSE_MASK |XSTATE_YMM_MASK |XSTATE_BNDREGS_MASK |XSTATE_BNDCSR_MASK |XSTATE_OPMASK_MASK | - XSTATE_ZMM_Hi256_MASK | XSTATE_Hi16_ZMM_MASK); + XSTATE_ZMM_Hi256_MASK | XSTATE_Hi16_ZMM_MASK; +if ((ecx &CPUID_EXT_AVX ) && + (ecx &CPUID_EXT_XSAVE ) && + (ecx &CPUID_EXT_OSXSAVE )) { +uint64_t host_xcr0 =xgetbv (0 ); + supp_xcr0 &= host_xcr0; + } eax &= supp_xcr0; }else if (idx ==1 ) {hv_vmx_read_capability (HV_VMX_CAP_PROCBASED2 , &cap);
Signature · 1 line
2.20.1 (Apple Git-117)
Quoted 8 lines · Hill Ma
This prevents illegal instruction on cpus do not support xgetbv.
Signed-off-by Hill Ma <[email protected]>--- target/i386/hvf/x86_cpuid.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
Hi Hill,
I'm sorry for delay with the review.
Quoted 10 lines
target/i386/hvf/x86_cpuid.c +0
-2@@ -100,11 +100,16 @@ uint32_t hvf_get_supported_cpuid(uint32_t func, uint32_t idx,break ;case 0xD :if (idx ==0 ) { -uint64_t host_xcr0 =xgetbv (0 ); - uint64_t supp_xcr0 = host_xcr0 & (XSTATE_FP_MASK | XSTATE_SSE_MASK |
target/i386/hvf/x86_cpuid.c uint32_t hvf_get_supported_cpuid(uint32_t func, uint32_t idx, · line 111
Quoted 10 lines
target/i386/hvf/x86_cpuid.c +5
-1+uint64_t supp_xcr0 =XSTATE_FP_MASK |XSTATE_SSE_MASK |XSTATE_YMM_MASK |XSTATE_BNDREGS_MASK |XSTATE_BNDCSR_MASK |XSTATE_OPMASK_MASK | - XSTATE_ZMM_Hi256_MASK | XSTATE_Hi16_ZMM_MASK); + XSTATE_ZMM_Hi256_MASK | XSTATE_Hi16_ZMM_MASK; +if ((ecx &CPUID_EXT_AVX ) && + (ecx &CPUID_EXT_XSAVE ) && + (ecx &CPUID_EXT_OSXSAVE )) {
It's sufficient to check only CPUID_EXT_OSXSAVE to ensure xgetbv presence (per SDM Vol. 1 13-5):
Software operating with CPL > 0 may need to determine whether the XSAVE feature set and certain XSAVE-enabled features have been enabled. If CPL > 0, execution of the MOV from CR4 instruction causes a general-protection fault (#GP). The following alternative mechanisms allow software to discover the enabling of the XSAVE feature set regardless of CPL:
* The value of CR4.OSXSAVE is returned in CPUID.1:ECX.OSXSAVE[bit 27]. If software determines that CPUID.1:ECX.OSXSAVE = 1, the processor supports the XSAVE feature set and the feature set has been enabled in CR4.
* Executing the XGETBV instruction with ECX = 0 returns the value of XCR0 in EDX:EAX. XGETBV can be executed if CR4.OSXSAVE = 1 (if CPUID.1:ECX.OSXSAVE = 1), regardless of CPL.
target/i386/hvf/x86_cpuid.c uint32_t hvf_get_supported_cpuid(uint32_t func, uint32_t idx, · line 113
Quoted 4 lines
target/i386/hvf/x86_cpuid.c +3
-0+uint64_t host_xcr0 =xgetbv (0 ); + supp_xcr0 &= host_xcr0; + } eax &= supp_xcr0;
I think instead of the patch you can do:
- if (idx == 0) {
+ if (idx == 0 && (ecx & CPUID_EXT_OSXSAVE)) {
That'd keep host values returned from CPUID on platforms that don't support XSAVE.
Thanks,
Roman
Quoted 5 lines
} else if (idx == 1) { hv_vmx_read_capability(HV_VMX_CAP_PROCBASED2, &cap);Signature · 1 line
2.20.1 (Apple Git-117)
Quoted 11 lines · Roman Bolshakov
Quoted 8 lines · Hill Ma
This prevents illegal instruction on cpus do not support xgetbv.
Signed-off-by Hill Ma <[email protected]>--- target/i386/hvf/x86_cpuid.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
Quoted 3 lines
Hi Hill,
I'm sorry for delay with the review.
So, hvf added a third use of inline asm execution of "xgetbv" to the two we had already. Now we have:
* this in hvf
* a use in tcg_target_init() in tcg/i386/tcg-target.c.inc
* a use in init_cpuid_cache() in util/bufferiszero.c
Is it possible to abstract this out so we have one version of this, not three ? I note that the other two got the "avoid executing an illegal insn" tests right...
thanks
-- PMM
Quoted 22 lines · Peter Maydell
Quoted 11 lines · Roman Bolshakov
Quoted 8 lines · Hill Ma
This prevents illegal instruction on cpus do not support xgetbv.
Signed-off-by Hill Ma <[email protected]>--- target/i386/hvf/x86_cpuid.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)Quoted 3 lines
Hi Hill,
I'm sorry for delay with the review.
So, hvf added a third use of inline asm execution of "xgetbv" to the two we had already. Now we have:
* this in hvf
* a use intcg_target_init()in tcg/i386/tcg-target.c.inc
* a use ininit_cpuid_cache()inutil/bufferiszero.c
Quoted 3 lines
Is it possible to abstract this out so we have one version of this, not three ? I note that the other two got the "avoid executing an illegal insn" tests right...
It surely is. If xgetbv() is extended like below and moved out of hvf, we can reuse it in all other places and no duplication of #UD avoidance will happen.
target/i386/hvf/x86_cpuid.c +12 -6
@@ -27,15 +27,21 @@#include "vmx.h" #include "sysemu/hvf.h" -static uint64_t xgetbv (uint32_t xcr) +static int xgetbv (uint32_t cpuid_ecx,uint32_t idx,uint64_t *xcr) { -uint32_t eax, edx; +uint32_t xcrl, xcrh; - __asm__volatile ("xgetbv" - :"=a" (eax),"=d" (edx) - :"c" (xcr)); +if (cpuid_ecx &&CPUID_EXT_OSXSAVE ) { +/* The xgetbv instruction is not available to older versions of +* the assembler, so we encode the instruction manually. +*/ + asm(".byte 0x0f, 0x01, 0xd0" :"=a" (xcrl),"=d" (xcrh) :"c" (idx)); -return (((uint64_t )edx) <<32 ) | eax; + *xcr = (((uint64_t )xcrh) <<32 ) | xcrl; +return 0 ; + } + +return 1 ; } uint32_t hvf_get_supported_cpuid(uint32_t func, uint32_t idx,
Hill, feel free to update the three places Peter mentioned. If it's more convenient for you I can make complete patch.
Thanks,
Roman
Quoted 3 lines
thanks
-- PMM
Quoted 11 lines · Roman Bolshakov
target/i386/hvf/x86_cpuid.c +5
-4+static int xgetbv (uint32_t cpuid_ecx,uint32_t idx,uint64_t *xcr) { -uint32_t eax, edx; +uint32_t xcrl, xcrh; - __asm__volatile ("xgetbv" - :"=a" (eax),"=d" (edx) - :"c" (xcr)); +if (cpuid_ecx &&CPUID_EXT_OSXSAVE ) { +/* The xgetbv instruction is not available to older versions of +* the assembler, so we encode the instruction manually.
target/i386/hvf/x86_cpuid.c line 45
Quoted 10 lines
target/i386/hvf/x86_cpuid.c +7
-1+*/ + asm(".byte 0x0f, 0x01, 0xd0" :"=a" (xcrl),"=d" (xcrh) :"c" (idx)); -return (((uint64_t )edx) <<32 ) | eax; + *xcr = (((uint64_t )xcrh) <<32 ) | xcrl; +return 0 ; + } + +return 1 ; }
Not to bikeshed too much, but this looks like it should return bool, and true on success, not the other way around.
r~
Quoted 15 lines · Richard Henderson
Quoted 14 lines · Roman Bolshakov
target/i386/hvf/x86_cpuid.c +7
-4+static int xgetbv (uint32_t cpuid_ecx,uint32_t idx,uint64_t *xcr) { -uint32_t eax, edx; +uint32_t xcrl, xcrh; - __asm__volatile ("xgetbv" - :"=a" (eax),"=d" (edx) - :"c" (xcr)); +if (cpuid_ecx &&CPUID_EXT_OSXSAVE ) { +/* The xgetbv instruction is not available to older versions of +* the assembler, so we encode the instruction manually. +*/ + asm(".byte 0x0f, 0x01, 0xd0" :"=a" (xcrl),"=d" (xcrh) :"c" (idx));
Quoted 10 lines
target/i386/hvf/x86_cpuid.c line 45
Quoted 7 lines
target/i386/hvf/x86_cpuid.c +5
-1-return (((uint64_t )edx) <<32 ) | eax; + *xcr = (((uint64_t )xcrh) <<32 ) | xcrl; +return 0 ; + } + +return 1 ; }Not to bikeshed too much, but this looks like it should return bool, and true on success, not the other way around.
Also, if we're going to put this some place common, forcing the caller to do the cpuid that feeds this, then we should probably make all of the startup cpuid stuff common as well.
Note that we'd probably have to use constructor priorities to get that right for util/bufferiszero.c.
r~
Quoted 16 lines · Richard Henderson
Quoted 15 lines · Richard Henderson
Quoted 14 lines · Roman Bolshakov
target/i386/hvf/x86_cpuid.c +7
-4+static int xgetbv (uint32_t cpuid_ecx,uint32_t idx,uint64_t *xcr) { -uint32_t eax, edx; +uint32_t xcrl, xcrh; - __asm__volatile ("xgetbv" - :"=a" (eax),"=d" (edx) - :"c" (xcr)); +if (cpuid_ecx &&CPUID_EXT_OSXSAVE ) { +/* The xgetbv instruction is not available to older versions of +* the assembler, so we encode the instruction manually. +*/ + asm(".byte 0x0f, 0x01, 0xd0" :"=a" (xcrl),"=d" (xcrh) :"c" (idx));
Quoted 11 lines
Quoted 10 lines
target/i386/hvf/x86_cpuid.c line 45
Quoted 7 lines
target/i386/hvf/x86_cpuid.c +5
-1-return (((uint64_t )edx) <<32 ) | eax; + *xcr = (((uint64_t )xcrh) <<32 ) | xcrl; +return 0 ; + } + +return 1 ; }Not to bikeshed too much, but this looks like it should return bool, and true on success, not the other way around.
I agree, it'd better to comprehend (and Hill has already sent v2 with this).
Quoted 4 lines
Also, if we're going to put this some place common, forcing the caller to do the cpuid that feeds this, then we should probably make all of the startup cpuid stuff common as well.
I proposed the version because all callers of xgetbv instruction already call cpuid before invoking inline xgetbv.
Quoted 3 lines
Note that we'd probably have to use constructor priorities to get that right for
util/bufferiszero.c.
Please correct me if I read this wrong. What you're saying is we should initialize cpuid in constructors and then use cached cpuid ecx in xgetbv() (and drop one argument, respectively)?
Thanks,
Roman
Quoted 8 lines · Roman Bolshakov
Quoted 4 lines
Also, if we're going to put this some place common, forcing the caller to do the cpuid that feeds this, then we should probably make all of the startup cpuid stuff common as well.
I proposed the version because all callers of xgetbv instruction already call cpuid before invoking inline xgetbv.
Quoted 7 lines
Quoted 3 lines
Note that we'd probably have to use constructor priorities to get that right for
util/bufferiszero.c.Please correct me if I read this wrong. What you're saying is we should initialize cpuid in constructors and then use cached cpuid ecx in
xgetbv()(and drop one argument, respectively)?
I would have xgetbv and all of the cpuid values cached at startup, and all three places would read global variables.
r~
Quoted 13 lines · Richard Henderson
Quoted 12 lines · Roman Bolshakov
Quoted 4 lines
Also, if we're going to put this some place common, forcing the caller to do the cpuid that feeds this, then we should probably make all of the startup cpuid stuff common as well.
I proposed the version because all callers of xgetbv instruction already call cpuid before invoking inline xgetbv.
Quoted 3 lines
Note that we'd probably have to use constructor priorities to get that right for
util/bufferiszero.c.
Quoted 7 lines
Quoted 3 lines
Please correct me if I read this wrong. What you're saying is we should initialize cpuid in constructors and then use cached cpuid ecx in
xgetbv()(and drop one argument, respectively)?I would have xgetbv and all of the cpuid values cached at startup, and all three places would read global variables.
It makes sense, thanks for the clarification.
Do you think it should be part of the change Hill is doing or it might be a follow-up cleanup patch?
-Roman
Quoted 8 lines · Roman Bolshakov
Quoted 3 lines
I would have xgetbv and all of the cpuid values cached at startup, and all three places would read global variables.
It makes sense, thanks for the clarification.
Do you think it should be part of the change Hill is doing or it might be a follow-up cleanup patch?
Could be follow-up, yes.
r~