x86/asm: cleanups after toolchain baseline upgrade
v2 cover x86/asm: cleanups after toolchain baseline upgrade
Patches 1-5 remove various workarounds under HAVE_AS_XXX for older compilers, as the minimally required GCC 5.1 / Binutils 2.25, and Clang 11 natively support instructions worked around in the hypervisor code.
Patch 6 cleans up error handling in VMX asm wrappers using asm goto.
[1] Link to v1: https://lore.kernel.org/xen-devel/[email protected]/
[2] CI: https://gitlab.com/xen-project/people/dmukhin/xen/-/pipelines/1751006178
Denis Mukhin (6):
x86/vmx: remove HAVE_AS_{EPT,VMX}, GAS_VMX_OP() and *_OPCODE x86/msr: remove HAVE_AS_FSGSBASE
x86/asm: remove HAVE_AS_INVPCID
x86/emulate: remove HAVE_AS_SSE4_2
x86/emulate: remove HAVE_AS_RDRAND and HAVE_AS_RDSEED x86/vmx: rework VMX wrappers to use asm goto()
xen/arch/x86/arch.mk | 9 +- xen/arch/x86/hvm/vmx/vmcs.c | 2 +- xen/arch/x86/include/asm/hvm/vmx/vmx.h | 236 ++++++++----------------- xen/arch/x86/include/asm/invpcid.h | 12 +- xen/arch/x86/include/asm/msr.h | 22 --- xen/arch/x86/x86_emulate/0fc7.c | 15 +- xen/arch/x86/x86_emulate/x86_emulate.c | 7 +- 7 files changed, 87 insertions(+), 216 deletions(-)
Signature · 1 line
2.34.1
PATCH v2 1/6 x86/vmx: remove HAVE_AS_{EPT,VMX}, GAS_VMX_OP() and *_OPCODE
From: Denis Mukhin <[email protected]>
The new toolchain baseline knows the VMX instructions, no need to carry the workaround in the code.
Move asm for vmxoff directly on the only callsite in vmcs.c
Updated formatting for all __xxx() calls to be consistent.
Resolves: https://gitlab.com/xen-project/xen/-/work_items/202
--- xen/arch/x86/arch.mk | 4 +- xen/arch/x86/hvm/vmx/vmcs.c | 2 +- xen/arch/x86/include/asm/hvm/vmx/vmx.h | 119 ++++--------------------- 3 files changed, 17 insertions(+), 108 deletions(-)
xen/arch/x86/arch.mk +1 -3
@@ -10,9 +10,7 @@ CFLAGS += -msoft-float $(call cc-options-add,CFLAGS,CC,$(EMBEDDED_EXTRA_CFLAGS)) $(call cc-option-add,CFLAGS,CC,-Wnested-externs) -$(call as-option-add,CFLAGS,CC,"vmcall",-DHAVE_AS_VMX) $(call as-option-add,CFLAGS,CC,"crc32 %eax$(comma)%eax",-DHAVE_AS_SSE4_2) -$(call as-option-add,CFLAGS,CC,"invept (%rax)$(comma)%rax",-DHAVE_AS_EPT) $(call as-option-add,CFLAGS,CC,"rdrand %eax",-DHAVE_AS_RDRAND) $(call as-option-add,CFLAGS,CC,"rdfsbase %rax",-DHAVE_AS_FSGSBASE) $(call as-option-add,CFLAGS,CC,"xsaveopt (%rax)",-DHAVE_AS_XSAVEOPT)
@@ -23,7 +21,7 @@ $(call as-option-add,CFLAGS,CC,"invpcid (%rax)$(comma)%rax",-DHAVE_AS_INVPCID) $(call as-option-add,CFLAGS,CC,"movdiri %rax$(comma)(%rax)",-DHAVE_AS_MOVDIR) $(call as-option-add,CFLAGS,CC,"enqcmd (%rax)$(comma)%rax",-DHAVE_AS_ENQCMD) -# Check to see whether the assmbler supports the .nop directive. +# Check to see whether the assembler supports the .nop directive. $(call as-option-add,CFLAGS,CC,\ ".L1: .L2: .nops (.L2 - .L1)$(comma)9",-DHAVE_AS_NOPS_DIRECTIVE)
xen/arch/x86/hvm/vmx/vmcs.c +1 -1
@@ -811,7 +811,7 @@ void cf_check vmx_cpu_down(void)BUG_ON (!(read_cr4 () &X86_CR4_VMXE ));this_cpu (vmxon) =0 ; -__vmxoff (); + asmvolatile ("vmxoff" : : :"memory" );local_irq_restore (flags); }
xen/arch/x86/include/asm/hvm/vmx/vmx.h +15 -104
223 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v2 1/6] x86/vmx: remove HAVE_AS_{EPT,VMX}, GAS_VMX_OP() and *_OPCODE
Quoted 6 lines · On 03/04/2025 7:23 pm, [email protected] wrote:
From: Denis Mukhin <[email protected]>
The new toolchain baseline knows the VMX instructions, no need to carry the workaround in the code.
Move asm for vmxoff directly on the only callsite in
vmcs.c
Ideally VMXOFF in capitals as it's an instruction name. But, this type of thing is more commonly phrased as "Inline __vmxoff() into it's single caller", or so.
Quoted 2 lines
Updated formatting for all
__xxx()calls to be consistent.
I'd suggest "for the other wrappers to be".
Quoted 7 lines
Resolves: https://gitlab.com/xen-project/xen/-/work_items/202
Signed-off-by Denis Mukhin <[email protected]>--- xen/arch/x86/arch.mk | 4 +- xen/arch/x86/hvm/vmx/vmcs.c | 2 +- xen/arch/x86/include/asm/hvm/vmx/vmx.h | 119 ++++---------------------
Just as a note, you're CC-ing The Rest, but this is an x86-only change, so should really only be CCing myself, Jan and Roger.
xen/arch/x86/hvm/vmx/vmcs.c void cf_check vmx_cpu_down(void) · line 814
Quoted 10 lines
xen/arch/x86/hvm/vmx/vmcs.c +1
-1@@ -811,7 +811,7 @@ void cf_check vmx_cpu_down(void)BUG_ON (!(read_cr4 () &X86_CR4_VMXE ));this_cpu (vmxon) =0 ; -__vmxoff (); + asmvolatile ("vmxoff" : : :"memory" );
asm volatile ( "vmxoff" ::: "memory" );
Quoted 16 lines
xen/arch/x86/hvm/vmx/vmcs.c +0
-0local_irq_restore (flags); }xen/arch/x86/include/asm/hvm/vmx/vmx.h +0
-5@@ -310,97 +292,54 @@ extern uint8_t posted_intr_vector;#define INVVPID_ALL_CONTEXT 2#define INVVPID_SINGLE_CONTEXT_RETAINING_GLOBAL 3 -#ifdef HAVE_AS_VMX -# define GAS_VMX_OP (yes, no) yes -#else -# define GAS_VMX_OP (yes, no) no -#endif
xen/arch/x86/include/asm/hvm/vmx/vmx.h extern uint8_t posted_intr_vector; · line 297
Quoted 10 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +1
-7- static always_inline void __vmptrld(u64 addr) { - asm volatile ( -#ifdef HAVE_AS_VMX - "vmptrld %0\n" -#else - VMPTRLD_OPCODE MODRM_EAX_06 -#endif + asm volatile ( "vmptrld %0\n"
As you're changing the line anyway, this ought to be \n\t. It's cosmetic, but comes in handy if you need to read the intermediate assembly.
Quoted 51 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +4
-22/* CF==1 or ZF==1 --> BUG() */ UNLIKELY_START(be, vmptrld) _ASM_BUGFRAME_TEXT(0) UNLIKELY_END_SECTION : -#ifdef HAVE_AS_VMX : "m" (addr), -#else - : "a" (&addr), -#endif _ASM_BUGFRAME_INFO(BUGFRAME_bug, __LINE__, __FILE__, 0) - : "memory"); + : "memory" ); } static always_inline void __vmpclear(u64 addr) { - asmvolatile ( -#ifdef HAVE_AS_VMX - "vmclear %0\n" -#else - VMCLEAR_OPCODE MODRM_EAX_06 -#endif + asmvolatile ("vmclear %0\n" /* CF==1 or ZF==1 --> BUG() */ UNLIKELY_START(be, vmclear) _ASM_BUGFRAME_TEXT(0) UNLIKELY_END_SECTION : -#ifdef HAVE_AS_VMX : "m" (addr), -#else - : "a" (&addr), -#endif _ASM_BUGFRAME_INFO(BUGFRAME_bug, __LINE__, __FILE__, 0) - : "memory"); + : "memory" ); } static always_inline void __vmread(unsigned long field, unsigned long *value) { - asm volatile ( -#ifdef HAVE_AS_VMX - "vmread %1, %0\n\t" -#else - VMREAD_OPCODE MODRM_EAX_ECX -#endif + asmvolatile ("vmread %1, %0\n\t" /* CF==1 or ZF==1 --> BUG() */ UNLIKELY_START(be, vmread)_ASM_BUGFRAME_TEXT (0 )
xen/arch/x86/include/asm/hvm/vmx/vmx.h extern uint8_t posted_intr_vector; · line 331
Quoted 10 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +0
-5UNLIKELY_END_SECTION -#ifdef HAVE_AS_VMX : "=rm" (*value) :"r" (field), -#else - : "=c" (*value) - : "a" (field), -#endif_ASM_BUGFRAME_INFO (BUGFRAME_bug, __LINE__, __FILE__,0 ) );
Fold this onto the previous line, as you're fixing up all the other closing brackets.
xen/arch/x86/include/asm/hvm/vmx/vmx.h static always_inline void __invvpid(unsigned long type, u16 vpid, u64 gva) · line 424
Quoted 4 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +0
-0@@ -494,24 +422,14 @@ static always_inline void __invvpid(unsigned long type, u16 vpid, u64 gva) } operand = {vpid, 0, gva};/* Fix up #UD exceptions which occur when TLBs are flushed before VMXON. */
I find this comment deeply troubling, but lets not go changing that right now.
I'm happy to fix this all on commit.
~Andrew
Re: [PATCH v2 1/6] x86/vmx: remove HAVE_AS_{EPT,VMX}, GAS_VMX_OP() and *_OPCODE
Quoted 29 lines · Andrew Cooper
Quoted 6 lines · On 03/04/2025 7:23 pm, [email protected] wrote:
From: Denis Mukhin [email protected]
The new toolchain baseline knows the VMX instructions, no need to carry the workaround in the code.
Move asm for vmxoff directly on the only callsite in
vmcs.cIdeally VMXOFF in capitals as it's an instruction name. But, this type of thing is more commonly phrased as "Inline
__vmxoff()into it's single caller", or so.Quoted 1 line
Updated formatting for all
__xxx()calls to be consistent.I'd suggest "for the other wrappers to be".
Quoted 6 lines
Resolves: https://gitlab.com/xen-project/xen/-/work_items/202
Signed-off-by Denis Mukhin [email protected]---
xen/arch/x86/arch.mk| 4 +-xen/arch/x86/hvm/vmx/vmcs.c| 2 +-xen/arch/x86/include/asm/hvm/vmx/vmx.h| 119 ++++---------------------
Quoted 2 lines
Just as a note, you're CC-ing The Rest, but this is an x86-only change, so should really only be CCing myself, Jan and Roger.
Whoops, I need to improve my tooling.
Quoted 114 lines
xen/arch/x86/hvm/vmx/vmcs.c void cf_check vmx_cpu_down(void) · line 814
Quoted 11 lines
xen/arch/x86/hvm/vmx/vmcs.c +1
-1@@ -811,7 +811,7 @@ void cf_check vmx_cpu_down(void)BUG_ON (!(read_cr4 () &X86_CR4_VMXE ));this_cpu (vmxon) =0 ; -__vmxoff (); + asmvolatile ("vmxoff" : : :"memory" );asm volatile ( "vmxoff" ::: "memory" );
Quoted 15 lines
xen/arch/x86/hvm/vmx/vmcs.c +0
-0local_irq_restore (flags); }xen/arch/x86/include/asm/hvm/vmx/vmx.h +0
-5@@ -310,97 +292,54 @@ extern uint8_t posted_intr_vector;#define INVVPID_ALL_CONTEXT 2#define INVVPID_SINGLE_CONTEXT_RETAINING_GLOBAL 3 -#ifdef HAVE_AS_VMX -# define GAS_VMX_OP (yes, no) yes -#else -# define GAS_VMX_OP (yes, no) no -#endif xen/arch/x86/include/asm/hvm/vmx/vmx.h extern uint8_t posted_intr_vector; · line 297
Quoted 10 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +1
-7- static always_inline void __vmptrld(u64 addr) { - asm volatile ( -#ifdef HAVE_AS_VMX - "vmptrld %0\n" -#else - VMPTRLD_OPCODE MODRM_EAX_06 -#endif + asm volatile ( "vmptrld %0\n"As you're changing the line anyway, this ought to be \n\t. It's cosmetic, but comes in handy if you need to read the intermediate assembly.
Quoted 59 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +0
-0/* CF==1 or ZF==1 --> BUG() */ UNLIKELY_START(be, vmptrld)_ASM_BUGFRAME_TEXT (0 )
UNLIKELY_END_SECTION
:
-#ifdefHAVE_AS_VMX
: "m" (addr),xen/arch/x86/include/asm/hvm/vmx/vmx.h +0
-3-#else - : "a" (&addr), -#endif
_ASM_BUGFRAME_INFO(BUGFRAME_bug, LINE, FILE, 0)
- : "memory");
+ : "memory" );
}static
always_inlinevoid__vmpclear(u64 addr)
{
- asm volatile (xen/arch/x86/include/asm/hvm/vmx/vmx.h +1
-5-#ifdef HAVE_AS_VMX -"vmclear %0\n" -#else - VMCLEAR_OPCODE MODRM_EAX_06 -#endif + asm volatile ( "vmclear %0\n"/* CF==1 or ZF==1 -->
BUG()*/UNLIKELY_START(be, vmclear)_ASM_BUGFRAME_TEXT(0)UNLIKELY_END_SECTION
:
-#ifdefHAVE_AS_VMX
: "m" (addr),xen/arch/x86/include/asm/hvm/vmx/vmx.h +0
-3-#else - : "a" (&addr), -#endif
_ASM_BUGFRAME_INFO(BUGFRAME_bug, LINE, FILE, 0)
- : "memory");
+ : "memory" );
}static
always_inlinevoid__vmread(unsigned long field, unsigned long value){
- asm volatile (xen/arch/x86/include/asm/hvm/vmx/vmx.h +1
-5-#ifdef HAVE_AS_VMX -"vmread %1, %0\n\t" -#else - VMREAD_OPCODE MODRM_EAX_ECX -#endif + asm volatile ( "vmread %1, %0\n\t"/ CF==1 or ZF==1 -->
BUG()*/UNLIKELY_START(be, vmread)_ASM_BUGFRAME_TEXT(0)UNLIKELY_END_SECTION
-#ifdefHAVE_AS_VMX
: "=rm" (*value)
: "r" (field),xen/arch/x86/include/asm/hvm/vmx/vmx.h +0
-4-#else - : "=c" (*value) - : "a" (field), -#endif Quoted 2 lines
_ASM_BUGFRAME_INFO(BUGFRAME_bug, LINE, FILE, 0)
);Fold this onto the previous line, as you're fixing up all the other closing brackets.
Quoted 3 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +0
-0@@ -494,24 +422,14 @@ static always_inline void __invvpid(unsigned long type, u16 vpid, u64 gva) } operand = {vpid, 0, gva};
Quoted 7 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h static always_inline void __invvpid(unsigned long type, u16 vpid, u64 gva) · line 424
Quoted 1 line
xen/arch/x86/include/asm/hvm/vmx/vmx.h +0
-0/* Fix up #UD exceptions which occur when TLBs are flushed before VMXON. */ I find this comment deeply troubling, but lets not go changing that right now.
I'm happy to fix this all on commit.
I will appreciate help with that!
Thanks!
Quoted 2 lines
~Andrew
PATCH v2 2/6 x86/msr: remove HAVE_AS_FSGSBASE
In reply to [email protected]From: Denis Mukhin <[email protected]>
The new toolchain baseline knows the {rd,wr}{f,g}sbase instructions, no need to carry the workaround in the code.
Resolves: https://gitlab.com/xen-project/xen/-/work_items/207
--- xen/arch/x86/arch.mk | 1 - xen/arch/x86/include/asm/msr.h | 22 ---------------------- 2 files changed, 23 deletions(-)
xen/arch/x86/arch.mk +0 -1
@@ -12,7 +12,6 @@ $(call cc-options-add,CFLAGS,CC,$(EMBEDDED_EXTRA_CFLAGS)) $(call cc-option-add,CFLAGS,CC,-Wnested-externs) $(call as-option-add,CFLAGS,CC,"crc32 %eax$(comma)%eax",-DHAVE_AS_SSE4_2) $(call as-option-add,CFLAGS,CC,"rdrand %eax",-DHAVE_AS_RDRAND) -$(call as-option-add,CFLAGS,CC,"rdfsbase %rax",-DHAVE_AS_FSGSBASE) $(call as-option-add,CFLAGS,CC,"xsaveopt (%rax)",-DHAVE_AS_XSAVEOPT) $(call as-option-add,CFLAGS,CC,"rdseed %eax",-DHAVE_AS_RDSEED) $(call as-option-add,CFLAGS,CC,"clwb (%rax)",-DHAVE_AS_CLWB)
xen/arch/x86/include/asm/msr.h +0 -22
@@ -152,11 +152,7 @@ static inline unsigned long __rdfsbase(void) {unsigned long base; -#ifdef HAVE_AS_FSGSBASE asmvolatile ("rdfsbase %0" :"=r" (base) ); -#else - asmvolatile (".byte 0xf3, 0x48, 0x0f, 0xae, 0xc0" :"=a" (base) ); -#endif return base; }
@@ -165,31 +161,19 @@ static inline unsigned long __rdgsbase(void) {unsigned long base; -#ifdef HAVE_AS_FSGSBASE asmvolatile ("rdgsbase %0" :"=r" (base) ); -#else - asmvolatile (".byte 0xf3, 0x48, 0x0f, 0xae, 0xc8" :"=a" (base) ); -#endif return base; }static inline void __wrfsbase (unsigned long base) { -#ifdef HAVE_AS_FSGSBASE asmvolatile ("wrfsbase %0" ::"r" (base) ); -#else - asmvolatile (".byte 0xf3, 0x48, 0x0f, 0xae, 0xd0" ::"a" (base) ); -#endif }static inline void __wrgsbase (unsigned long base) { -#ifdef HAVE_AS_FSGSBASE asmvolatile ("wrgsbase %0" ::"r" (base) ); -#else - asmvolatile (".byte 0xf3, 0x48, 0x0f, 0xae, 0xd8" ::"a" (base) ); -#endif }static inline unsigned long read_fs_base (void )
@@ -253,15 +237,9 @@ static inline void write_gs_shadow(unsigned long base)if (read_cr4 () &X86_CR4_FSGSBASE ) { asmvolatile ("swapgs\n\t" -#ifdef HAVE_AS_FSGSBASE"wrgsbase %0\n\t" "swapgs" ::"r" (base) ); -#else - ".byte 0xf3, 0x48, 0x0f, 0xae, 0xd8\n\t" - "swapgs" - :: "a" (base) ); -#endif }else wrmsrl (MSR_SHADOW_GS_BASE , base);
Signature · 1 line
2.34.1
Re: [PATCH v2 2/6] x86/msr: remove HAVE_AS_FSGSBASE
Quoted 7 lines · On 03/04/2025 7:23 pm, [email protected] wrote:
From: Denis Mukhin <[email protected]>
The new toolchain baseline knows the {rd,wr}{f,g}sbase instructions, no need to carry the workaround in the code.
Resolves: https://gitlab.com/xen-project/xen/-/work_items/207
Signed-off-by Denis Mukhin <[email protected]>
PATCH v2 3/6 x86/asm: remove HAVE_AS_INVPCID
In reply to [email protected]From: Denis Mukhin <[email protected]>
The new toolchain baseline knows the invpcid instruction, no need to carry the workaround in the code.
Resolves: https://gitlab.com/xen-project/xen/-/work_items/209
--- xen/arch/x86/arch.mk | 1 - xen/arch/x86/include/asm/invpcid.h | 12 +----------- 2 files changed, 1 insertion(+), 12 deletions(-)
xen/arch/x86/arch.mk +0 -1
@@ -16,7 +16,6 @@ $(call as-option-add,CFLAGS,CC,"xsaveopt (%rax)",-DHAVE_AS_XSAVEOPT) $(call as-option-add,CFLAGS,CC,"rdseed %eax",-DHAVE_AS_RDSEED) $(call as-option-add,CFLAGS,CC,"clwb (%rax)",-DHAVE_AS_CLWB) $(call as-option-add,CFLAGS,CC,".equ \"x\"$(comma)1",-DHAVE_AS_QUOTED_SYM) -$(call as-option-add,CFLAGS,CC,"invpcid (%rax)$(comma)%rax",-DHAVE_AS_INVPCID) $(call as-option-add,CFLAGS,CC,"movdiri %rax$(comma)(%rax)",-DHAVE_AS_MOVDIR) $(call as-option-add,CFLAGS,CC,"enqcmd (%rax)$(comma)%rax",-DHAVE_AS_ENQCMD)
xen/arch/x86/include/asm/invpcid.h +1 -11
@@ -5,9 +5,6 @@extern bool use_invpcid; -#define INVPCID_OPCODE ".byte 0x66, 0x0f, 0x38, 0x82\n" -#define MODRM_ECX_01 ".byte 0x01\n" -static inline void invpcid (unsigned int pcid,unsigned long addr,unsigned int type) {
@@ -17,16 +14,9 @@ static inline void invpcid(unsigned int pcid, unsigned long addr,uint64_t addr; } desc = { .pcid = pcid, .addr = addr }; - asmvolatile ( -#ifdef HAVE_AS_INVPCID - "invpcid %[desc], %q[type]" + asmvolatile ("invpcid %[desc], %q[type]" :/* No output */ : [desc]"m" (desc), [type]"r" (type) -#else - INVPCID_OPCODE MODRM_ECX_01 - : /* No output */ - : "a" (type), "c" (&desc) -#endif :"memory" ); }
Signature · 1 line
2.34.1
Re: [PATCH v2 3/6] x86/asm: remove HAVE_AS_INVPCID
Quoted 7 lines · On 03/04/2025 7:23 pm, [email protected] wrote:
From: Denis Mukhin <[email protected]>
The new toolchain baseline knows the invpcid instruction, no need to carry the workaround in the code.
Resolves: https://gitlab.com/xen-project/xen/-/work_items/209
Signed-off-by Denis Mukhin <[email protected]>
PATCH v2 4/6 x86/emulate: remove HAVE_AS_SSE4_2
In reply to [email protected]From: Denis Mukhin <[email protected]>
The new toolchain baseline knows the crc32 instructions, no need to carry the workaround in the code.
Resolves: https://gitlab.com/xen-project/xen/-/work_items/206
--- xen/arch/x86/arch.mk | 1 - xen/arch/x86/x86_emulate/x86_emulate.c | 7 +++---- 2 files changed, 3 insertions(+), 5 deletions(-)
xen/arch/x86/arch.mk +0 -1
@@ -10,7 +10,6 @@ CFLAGS += -msoft-float $(call cc-options-add,CFLAGS,CC,$(EMBEDDED_EXTRA_CFLAGS)) $(call cc-option-add,CFLAGS,CC,-Wnested-externs) -$(call as-option-add,CFLAGS,CC,"crc32 %eax$(comma)%eax",-DHAVE_AS_SSE4_2) $(call as-option-add,CFLAGS,CC,"rdrand %eax",-DHAVE_AS_RDRAND) $(call as-option-add,CFLAGS,CC,"xsaveopt (%rax)",-DHAVE_AS_XSAVEOPT) $(call as-option-add,CFLAGS,CC,"rdseed %eax",-DHAVE_AS_RDSEED)
xen/arch/x86/x86_emulate/x86_emulate.c +3 -4
@@ -6882,7 +6882,7 @@ x86_emulate( goto unhandleable; }break ; -#ifdef HAVE_AS_SSE4_2 +case X86EMUL_OPC_F2 (0x0f38 ,0xf0 ):/* crc32 r/m8, r{32,64} */ case X86EMUL_OPC_F2 (0x0f38 ,0xf1 ):/* crc32 r/m{16,32,64}, r{32,64} */ host_and_vcpu_must_have (sse4_2);
@@ -6901,17 +6901,16 @@ x86_emulate( asm ("crc32l %1,%k0" :"+r" (dst.val) :"rm" (*(uint32_t *)&src.val) );break ; -# ifdef __x86_64__ +#ifdef __x86_64__case 8 : asm ("crc32q %1,%0" :"+r" (dst.val) :"rm" (src.val) );break ; -# endif +#endif default :ASSERT_UNREACHABLE (); goto unhandleable; }break ; -#endif case X86EMUL_OPC_VEX (0x0f38 ,0xf2 ):/* andn r/m,r,r */ case X86EMUL_OPC_VEX (0x0f38 ,0xf5 ):/* bzhi r,r/m,r */
Signature · 1 line
2.34.1
Re: [PATCH v2 4/6] x86/emulate: remove HAVE_AS_SSE4_2
Quoted 7 lines · On 03/04/2025 7:23 pm, [email protected] wrote:
From: Denis Mukhin <[email protected]>
The new toolchain baseline knows the crc32 instructions, no need to carry the workaround in the code.
Resolves: https://gitlab.com/xen-project/xen/-/work_items/206
Signed-off-by Denis Mukhin <[email protected]>
PATCH v2 5/6 x86/emulate: remove HAVE_AS_RDRAND and HAVE_AS_RDSEED
In reply to [email protected]From: Denis Mukhin <[email protected]>
The new toolchain baseline knows the rdrand/rdseed instructions, no need to carry the workaround in the code.
Resolves: https://gitlab.com/xen-project/xen/-/work_items/208
--- xen/arch/x86/arch.mk | 2 -- xen/arch/x86/x86_emulate/0fc7.c | 15 +++++---------- 2 files changed, 5 insertions(+), 12 deletions(-)
xen/arch/x86/arch.mk +0 -2
@@ -10,9 +10,7 @@ CFLAGS += -msoft-float $(call cc-options-add,CFLAGS,CC,$(EMBEDDED_EXTRA_CFLAGS)) $(call cc-option-add,CFLAGS,CC,-Wnested-externs) -$(call as-option-add,CFLAGS,CC,"rdrand %eax",-DHAVE_AS_RDRAND) $(call as-option-add,CFLAGS,CC,"xsaveopt (%rax)",-DHAVE_AS_XSAVEOPT) -$(call as-option-add,CFLAGS,CC,"rdseed %eax",-DHAVE_AS_RDSEED) $(call as-option-add,CFLAGS,CC,"clwb (%rax)",-DHAVE_AS_CLWB) $(call as-option-add,CFLAGS,CC,".equ \"x\"$(comma)1",-DHAVE_AS_QUOTED_SYM) $(call as-option-add,CFLAGS,CC,"movdiri %rax$(comma)(%rax)",-DHAVE_AS_MOVDIR)
xen/arch/x86/x86_emulate/0fc7.c +5 -10
@@ -32,7 +32,6 @@ int x86emul_0fc7(struct x86_emulate_state *s,return X86EMUL_UNRECOGNIZED ;case 6 :/* rdrand */ -#ifdef HAVE_AS_RDRAND generate_exception_if (s->vex.pfx >= vex_f3,X86_EXC_UD );host_and_vcpu_must_have (rdrand); *dst = s->ea;
@@ -43,12 +42,12 @@ int x86emul_0fc7(struct x86_emulate_state *s, : "=r" (dst->val), ASM_FLAG_OUT("=@ccc", "=qm") (carry) );break ;default : -# ifdef __x86_64__ +#ifdef __x86_64__ asm ( "rdrand %k0" ASM_FLAG_OUT(, "; setc %1") : "=r" (dst->val), ASM_FLAG_OUT("=@ccc", "=qm") (carry) );break ;case 8 : -# endif +#endif asm ( "rdrand %0" ASM_FLAG_OUT(, "; setc %1") : "=r" (dst->val), ASM_FLAG_OUT("=@ccc", "=qm") (carry) );break ;
@@ -57,9 +56,6 @@ int x86emul_0fc7(struct x86_emulate_state *s,if ( carry ) regs->eflags |=X86_EFLAGS_CF ;break ; -#else -return X86EMUL_UNIMPLEMENTED ; -#endif case 7 :/* rdseed / rdpid */ if ( s->vex.pfx == vex_f3 )/* rdpid */
@@ -77,7 +73,7 @@ int x86emul_0fc7(struct x86_emulate_state *s, dst->bytes =4 ;break ; } -#ifdef HAVE_AS_RDSEED +generate_exception_if (s->vex.pfx >= vex_f3,X86_EXC_UD );host_and_vcpu_must_have (rdseed); *dst = s->ea;
@@ -88,12 +84,12 @@ int x86emul_0fc7(struct x86_emulate_state *s, : "=r" (dst->val), ASM_FLAG_OUT("=@ccc", "=qm") (carry) );break ;default : -# ifdef __x86_64__ +#ifdef __x86_64__ asm ( "rdseed %k0" ASM_FLAG_OUT(, "; setc %1") : "=r" (dst->val), ASM_FLAG_OUT("=@ccc", "=qm") (carry) );break ;case 8 : -# endif +#endif asm ( "rdseed %0" ASM_FLAG_OUT(, "; setc %1") : "=r" (dst->val), ASM_FLAG_OUT("=@ccc", "=qm") (carry) );break ;
@@ -102,7 +98,6 @@ int x86emul_0fc7(struct x86_emulate_state *s,if ( carry ) regs->eflags |=X86_EFLAGS_CF ;break ; -#endif } }else
Signature · 1 line
2.34.1
Re: [PATCH v2 5/6] x86/emulate: remove HAVE_AS_RDRAND and HAVE_AS_RDSEED
Quoted 10 lines · On 03/04/2025 7:23 pm, [email protected] wrote:
From: Denis Mukhin <[email protected]>
The new toolchain baseline knows the rdrand/rdseed instructions, no need to carry the workaround in the code.
Resolves: https://gitlab.com/xen-project/xen/-/work_items/208
Signed-off-by Denis Mukhin <[email protected]>--- xen/arch/x86/arch.mk | 2 -- xen/arch/x86/x86_emulate/0fc7.c | 15 +++++----------
Huh...
I was expecting a hunk in xen/arch/x86/include/asm/random.h but lookin at it, I see that didn't even get the HAVE_AS_RDRAND treatment in the first place. Also, the + constraint, rather than =, looks suspicious.
Could I ask you to clean those up too please? The rest of this patch looks fine.
~Andrew
Re: [PATCH v2 5/6] x86/emulate: remove HAVE_AS_RDRAND and HAVE_AS_RDSEED
Quoted 12 lines · Andrew Cooper
Quoted 10 lines · On 03/04/2025 7:23 pm, [email protected] wrote:
From: Denis Mukhin <[email protected]>
The new toolchain baseline knows the rdrand/rdseed instructions, no need to carry the workaround in the code.
Resolves: https://gitlab.com/xen-project/xen/-/work_items/208
Signed-off-by Denis Mukhin <[email protected]>--- xen/arch/x86/arch.mk | 2 -- xen/arch/x86/x86_emulate/0fc7.c | 15 +++++----------
Quoted 5 lines
Huh...
I was expecting a hunk in
xen/arch/x86/include/asm/random.hbut lookin at it, I see that didn't even get theHAVE_AS_RDRANDtreatment in the first place. Also, the + constraint, rather than =, looks suspicious.
Might that have been for a similar reason as the one you recently added a XOR ahead of POPCNT for? IOW us wanting the clearing of the output to happen irrespective of whether the if() condition is true?
Jan
PATCH v2 6/6 x86/vmx: rework VMX wrappers to use `asm goto()`
In reply to [email protected]From: Denis Mukhin <[email protected]>
Improve error handling in VMX wrappers by switching to asm goto() where possible.
vmread_safe() kept as is because the minimally required baseline GCC does not support output operands in asm goto.
Resolves: https://gitlab.com/xen-project/xen/-/work_items/210
--- xen/arch/x86/include/asm/hvm/vmx/vmx.h | 141 +++++++++++++------------ 1 file changed, 73 insertions(+), 68 deletions(-)
xen/arch/x86/include/asm/hvm/vmx/vmx.h +73 -68
@@ -294,54 +294,57 @@ extern uint8_t posted_intr_vector; static always_inline void __vmptrld(u64 addr) { - asmvolatile ("vmptrld %0\n" -/* CF==1 or ZF==1 --> BUG() */ - UNLIKELY_START(be, vmptrld) - _ASM_BUGFRAME_TEXT(0) - UNLIKELY_END_SECTION - : - : "m" (addr), - _ASM_BUGFRAME_INFO(BUGFRAME_bug, __LINE__, __FILE__, 0) - : "memory" ); + asm goto ("vmptrld %[addr]\n" +"jbe %l[vmfail]\n\t" + : + : [addr]"m" (addr) + :"memory" + : vmfail ); +return ; + + vmfail: +BUG (); } static always_inline void __vmpclear(u64 addr) { - asmvolatile ("vmclear %0\n" -/* CF==1 or ZF==1 --> BUG() */ - UNLIKELY_START(be, vmclear) - _ASM_BUGFRAME_TEXT(0) - UNLIKELY_END_SECTION - : - : "m" (addr), - _ASM_BUGFRAME_INFO(BUGFRAME_bug, __LINE__, __FILE__, 0) - : "memory" ); + asm goto ("vmclear %[addr]\n" +"jbe %l[vmfail]\n\t" + : + : [addr]"m" (addr) + :"memory" + : vmfail ); +return ; + + vmfail: +BUG (); } static always_inline void __vmread(unsigned long field, unsigned long *value) { - asmvolatile ("vmread %1, %0\n\t" -/* CF==1 or ZF==1 --> BUG() */ - UNLIKELY_START(be, vmread) - _ASM_BUGFRAME_TEXT(0) - UNLIKELY_END_SECTION - : "=rm" (*value) - :"r" (field), -_ASM_BUGFRAME_INFO (BUGFRAME_bug, __LINE__, __FILE__,0 ) - ); +bool vmfail; + + asmvolatile ("vmread %[field], %[value]\n\t" +"setbe %[vmfail]\n\t" + : [value]"=rm" (*value), [vmfail]"=rm" (vmfail) + : [field]"r" (field) + :"cc" ); +if ( vmfail ) +BUG (); } static always_inline void __vmwrite(unsigned long field, unsigned long value) { - asmvolatile ("vmwrite %1, %0\n" -/* CF==1 or ZF==1 --> BUG() */ - UNLIKELY_START(be, vmwrite) - _ASM_BUGFRAME_TEXT(0) - UNLIKELY_END_SECTION - : - : "r" (field) , "rm" (value), -_ASM_BUGFRAME_INFO (BUGFRAME_bug, __LINE__, __FILE__,0 ) - ); + asm goto ("vmwrite %[value], %[field]\n\t" +"jbe %l[vmfail]\n\t" + : + : [field]"r" (field), [value]"rm" (value) + : + : vmfail ); +return ; + + vmfail: +BUG (); } static inline enum vmx_insn_errno vmread_safe(unsigned long field,
@@ -369,22 +372,22 @@ static inline enum vmx_insn_errno vmread_safe(unsigned long field,static inline enum vmx_insn_errno vmwrite_safe (unsigned long field,unsigned long value) { -unsigned long ret =VMX_INSN_SUCCEED ; -bool fail_invalid, fail_valid; +unsigned long ret; - asm volatile ( "vmwrite %[value], %[field]\n\t" - ASM_FLAG_OUT(, "setc %[invalid]\n\t") - ASM_FLAG_OUT(, "setz %[valid]\n\t") - : ASM_FLAG_OUT("=@ccc", [invalid] "=rm") (fail_invalid), - ASM_FLAG_OUT("=@ccz", [valid] "=rm") (fail_valid) - : [field] "r" (field), - [value] "rm" (value) ); + asm goto ("vmwrite %[value], %[field]\n\t" +"jc %l[vmfail_invalid]\n\t" +"jz %l[vmfail_error]\n\t" + : + : [field]"r" (field), [value]"rm" (value) + : + : vmfail_invalid, vmfail_error ); +return VMX_INSN_SUCCEED ; -if (unlikely (fail_invalid) ) - ret =VMX_INSN_FAIL_INVALID ; -else if (unlikely (fail_valid) ) -__vmread (VM_INSTRUCTION_ERROR , &ret); + vmfail_invalid: +return VMX_INSN_FAIL_INVALID ; + vmfail_error: +__vmread (VM_INSTRUCTION_ERROR , &ret);return ret; }
@@ -402,15 +405,16 @@ static always_inline void __invept(unsigned long type, uint64_t eptp) !cpu_has_vmx_ept_invept_single_context ) type =INVEPT_ALL_CONTEXT ; - asm volatile ( "invept %0, %1\n" - /* CF==1 or ZF==1 --> BUG() */ - UNLIKELY_START(be, invept) - _ASM_BUGFRAME_TEXT(0) - UNLIKELY_END_SECTION - : - : "m" (operand), "r" (type), - _ASM_BUGFRAME_INFO(BUGFRAME_bug, __LINE__, __FILE__, 0) - : "memory" ); + asm goto ("invept %[operand], %[type]\n" +"jbe %l[vmfail]\n\t" + : + : [operand]"m" (operand), [type]"r" (type) + :"memory" + : vmfail ); +return ; + + vmfail: +BUG (); } static always_inline void __invvpid(unsigned long type, u16 vpid, u64 gva)
@@ -422,16 +426,17 @@ static always_inline void __invvpid(unsigned long type, u16 vpid, u64 gva) } operand = {vpid, 0, gva};/* Fix up #UD exceptions which occur when TLBs are flushed before VMXON. */ - asmvolatile ("1: invvpid %0, %1\n" -/* CF==1 or ZF==1 --> BUG() */ - UNLIKELY_START(be, invvpid) - _ASM_BUGFRAME_TEXT(0) - UNLIKELY_END_SECTION "\n" - "2:" _ASM_EXTABLE(1b, 2b) - : - : "m" (operand), "r" (type), - _ASM_BUGFRAME_INFO(BUGFRAME_bug, __LINE__, __FILE__, 0) - : "memory" ); + asm goto ("1: invvpid %[operand], %[type]\n" +" jbe %l[vmfail]\n\t" + "2:" _ASM_EXTABLE(1b, 2b) + : + : [operand] "m" (operand), [type] "r" (type) + : "memory" + : vmfail ); +return ; + + vmfail: +BUG (); }static inline void ept_sync_all (void )
Signature · 1 line
2.34.1
Re: [PATCH v2 6/6] x86/vmx: rework VMX wrappers to use `asm goto()`
Quoted 10 lines · On 03/04/2025 7:23 pm, [email protected] wrote:
From: Denis Mukhin <[email protected]>
Improve error handling in VMX wrappers by switching to
asm goto()where possible.
vmread_safe()kept as is because the minimally required baseline GCC does not support output operands inasm goto.Resolves: https://gitlab.com/xen-project/xen/-/work_items/210
Signed-off-by Denis Mukhin <[email protected]>
I'd suggest limiting this patch to the asm-goto transformations only.
Quoted 13 lines
--- xen/arch/x86/include/asm/hvm/vmx/vmx.h | 141 +++++++++++++------------ 1 file changed, 73 insertions(+), 68 deletions(-)xen/arch/x86/include/asm/hvm/vmx/vmx.h +0
-1@@ -294,54 +294,57 @@ extern uint8_t posted_intr_vector; static always_inline void __vmptrld(u64 addr) { - asm volatile ( "vmptrld %0\n"
xen/arch/x86/include/asm/hvm/vmx/vmx.h extern uint8_t posted_intr_vector; · line 298
Quoted 10 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +2
-8-/* CF==1 or ZF==1 --> BUG() */ - UNLIKELY_START(be, vmptrld) - _ASM_BUGFRAME_TEXT(0) - UNLIKELY_END_SECTION - : - : "m" (addr), - _ASM_BUGFRAME_INFO(BUGFRAME_bug, __LINE__, __FILE__, 0) - : "memory" ); + asm goto ("vmptrld %[addr]\n" + "jbe %l[vmfail]\n\t"
Also cosmetic, but the very final line in the asm block ideally doesn't want the \n\t. Except, this tends to be hard to spot because of the way we use macros such as UNLIKELY_START() or _ASM_EXTABLE() which do just expand to more strings under the hood.
Quoted 43 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +18
-17+ : + : [addr] "m" (addr) + : "memory" + : vmfail ); +return ; + + vmfail: +BUG (); } static always_inline void __vmpclear(u64 addr) { - asmvolatile ("vmclear %0\n" -/* CF==1 or ZF==1 --> BUG() */ - UNLIKELY_START(be, vmclear) - _ASM_BUGFRAME_TEXT(0) - UNLIKELY_END_SECTION - : - : "m" (addr), - _ASM_BUGFRAME_INFO(BUGFRAME_bug, __LINE__, __FILE__, 0) - : "memory" ); + asm goto ("vmclear %[addr]\n" +"jbe %l[vmfail]\n\t" + : + : [addr]"m" (addr) + :"memory" + : vmfail ); +return ; + + vmfail: +BUG (); } static always_inline void __vmread(unsigned long field, unsigned long *value) { - asmvolatile ("vmread %1, %0\n\t" -/* CF==1 or ZF==1 --> BUG() */ - UNLIKELY_START(be, vmread) - _ASM_BUGFRAME_TEXT(0) - UNLIKELY_END_SECTION - : "=rm" (*value) - :"r" (field), -_ASM_BUGFRAME_INFO (BUGFRAME_bug, __LINE__, __FILE__,0 )
xen/arch/x86/include/asm/hvm/vmx/vmx.h extern uint8_t posted_intr_vector; · line 333
Quoted 10 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +9
-1- ); +bool vmfail; + + asmvolatile ("vmread %[field], %[value]\n\t" +"setbe %[vmfail]\n\t" + : [value]"=rm" (*value), [vmfail]"=rm" (vmfail) + : [field]"r" (field) + :"cc" ); +if ( vmfail ) +BUG ();
This is almost certainly not an improvement in generated code. You now need register to hold the boolean, where previously there was a jbe to a ud2 and no extra state required.
Here's an example https://godbolt.org/z/GG4r1c7bK showing the difference. (It also shows up the Clang "rm" constraint bug. Change vmfail from "=rm" to "=r" to get sane(er) code generation.)
Also, you've added a "cc" clobber. This is one thing you don't need on x86; it's simply assumed, given how many instructions update flags naturally.
Quoted 23 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +9
-9@@ -369,22 +372,22 @@ static inline enum vmx_insn_errno vmread_safe(unsigned long field,static inline enum vmx_insn_errno vmwrite_safe (unsigned long field,unsigned long value) { -unsigned long ret =VMX_INSN_SUCCEED ; -bool fail_invalid, fail_valid; +unsigned long ret; - asm volatile ( "vmwrite %[value], %[field]\n\t" - ASM_FLAG_OUT(, "setc %[invalid]\n\t") - ASM_FLAG_OUT(, "setz %[valid]\n\t") - : ASM_FLAG_OUT("=@ccc", [invalid] "=rm") (fail_invalid), - ASM_FLAG_OUT("=@ccz", [valid] "=rm") (fail_valid) - : [field] "r" (field), - [value] "rm" (value) ); + asm goto ("vmwrite %[value], %[field]\n\t" +"jc %l[vmfail_invalid]\n\t" +"jz %l[vmfail_error]\n\t" + : + : [field]"r" (field), [value]"rm" (value) + : + : vmfail_invalid, vmfail_error ); +return VMX_INSN_SUCCEED ;
xen/arch/x86/include/asm/hvm/vmx/vmx.h static inline enum vmx_insn_errno vmread_safe(unsigned long field, · line 390
Quoted 10 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +4
-4-if (unlikely (fail_invalid) ) - ret =VMX_INSN_FAIL_INVALID ; -else if (unlikely (fail_valid) ) -__vmread (VM_INSTRUCTION_ERROR , &ret); + vmfail_invalid: +return VMX_INSN_FAIL_INVALID ; + vmfail_error: +__vmread (VM_INSTRUCTION_ERROR , &ret);
Something not technically toolchain cleanup, but is in desperate need of doing.
__vmread() being void and producing an output by pointer is insane, and leads to ugly code such as this, even if the compiler can usually fix up behind the scenes.
It would be lovely to have:
unsigned long vmread(unsigned int field)
which can be wrapped by __vmread() with "*value = vmread(field);" or so. (Don't go converting all users; that's a huge task).
Then, this vmfail_error can be simply "return vmread(VM_INSTRUCTION_ERROR);" and you can drop the ret variable.
~Andrew
Re: [PATCH v2 6/6] x86/vmx: rework VMX wrappers to use `asm goto()`
Quoted 5 lines · Andrew Cooper
Quoted 4 lines · On 03/04/2025 7:23 pm, [email protected] wrote:
xen/arch/x86/include/asm/hvm/vmx/vmx.h +0
-0@@ -294,54 +294,57 @@ extern uint8_t posted_intr_vector;
Quoted 16 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h extern uint8_t posted_intr_vector; · line 298
Quoted 13 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +2
-9static always_inline void __vmptrld(u64 addr) { - asmvolatile ("vmptrld %0\n" -/* CF==1 or ZF==1 --> BUG() */ - UNLIKELY_START(be, vmptrld) - _ASM_BUGFRAME_TEXT(0) - UNLIKELY_END_SECTION - : - : "m" (addr), - _ASM_BUGFRAME_INFO(BUGFRAME_bug, __LINE__, __FILE__, 0) - : "memory" ); + asm goto ("vmptrld %[addr]\n" + "jbe %l[vmfail]\n\t"Also cosmetic, but the very final line in the asm block ideally doesn't want the \n\t.
And to clarify (Andrew gave a similar comment elsewhere) the \t instead wants to appear on the first of these two lines.
Jan
Re: [PATCH v2 6/6] x86/vmx: rework VMX wrappers to use `asm goto()`
Quoted 21 lines · Jan Beulich
Quoted 5 lines · Andrew Cooper
Quoted 4 lines · On 03/04/2025 7:23 pm, [email protected] wrote:
xen/arch/x86/include/asm/hvm/vmx/vmx.h +0
-0@@ -294,54 +294,57 @@ extern uint8_t posted_intr_vector;Quoted 15 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h extern uint8_t posted_intr_vector; · line 298
Quoted 13 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +2
-9static always_inline void __vmptrld(u64 addr) { - asmvolatile ("vmptrld %0\n" -/* CF==1 or ZF==1 --> BUG() */ - UNLIKELY_START(be, vmptrld) - _ASM_BUGFRAME_TEXT(0) - UNLIKELY_END_SECTION - : - : "m" (addr), - _ASM_BUGFRAME_INFO(BUGFRAME_bug, __LINE__, __FILE__, 0) - : "memory" ); + asm goto ("vmptrld %[addr]\n" + "jbe %l[vmfail]\n\t"Also cosmetic, but the very final line in the asm block ideally doesn't want the \n\t.
Quoted 2 lines
And to clarify (Andrew gave a similar comment elsewhere) the \t instead wants to appear on the first of these two lines.
Indeed. This particular example got adjusted in patch 1, where I made said observation.
~Andrew