x86/asm: cleanups after toolchain baseline upgrade
cover x86/asm: cleanups after toolchain baseline upgrade
Patches 1-4 remove various compiler workarounds in the hypervisor code for Intel's VMX instructions.
Patch 5 removes compiler workaround for missing clac/stac instructions.
CI: https://gitlab.com/xen-project/people/dmukhin/xen/-/pipelines/1747394875
Denis Mukhin (5):
x86/vmx: remove HAVE_AS_VMX
x86/vmx: remove HAVE_AS_EPT
x86/vmx: remove GAS_VMX_OP()
x86/vmx: remove *_OPCODE
x86/asm: remove HAVE_AS_CLAC_STAC
xen/arch/x86/arch.mk | 5 +- xen/arch/x86/include/asm/asm-defns.h | 10 --- xen/arch/x86/include/asm/hvm/vmx/vmx.h | 103 ++++--------------------- 3 files changed, 15 insertions(+), 103 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/5 x86/vmx: remove HAVE_AS_VMX
From: Denis Mukhin <[email protected]>
Remove the workaround under HAVE_AS_VMX for older compilers, as the minimally required GCC 5.1 / Binutils 2.25, and Clang 11 natively support the VMX instructions used in the hypervisor code.
--- xen/arch/x86/arch.mk | 3 +-- xen/arch/x86/include/asm/hvm/vmx/vmx.h | 37 -------------------------- 2 files changed, 1 insertion(+), 39 deletions(-)
xen/arch/x86/arch.mk +1 -2
@@ -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,"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)
@@ -24,7 +23,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/include/asm/hvm/vmx/vmx.h +0 -37
@@ -310,30 +310,18 @@ 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 static always_inline void __vmptrld(u64 addr) { asmvolatile ( -#ifdef HAVE_AS_VMX"vmptrld %0\n" -#else - VMPTRLD_OPCODE MODRM_EAX_06 -#endif/* 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"); }
@@ -341,21 +329,13 @@ static always_inline void __vmptrld(u64 addr) static always_inline void __vmpclear(u64 addr) { asmvolatile ( -#ifdef HAVE_AS_VMX"vmclear %0\n" -#else - VMCLEAR_OPCODE MODRM_EAX_06 -#endif/* 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"); }
@@ -363,22 +343,13 @@ static always_inline void __vmpclear(u64 addr) static always_inline void __vmread(unsigned long field, unsigned long *value) { asmvolatile ( -#ifdef HAVE_AS_VMX"vmread %1, %0\n\t" -#else - VMREAD_OPCODE MODRM_EAX_ECX -#endif/* CF==1 or ZF==1 --> BUG() */ UNLIKELY_START(be, vmread) _ASM_BUGFRAME_TEXT(0) UNLIKELY_END_SECTION -#ifdef HAVE_AS_VMX : "=rm" (*value) :"r" (field), -#else - : "=c" (*value) - : "a" (field), -#endif_ASM_BUGFRAME_INFO (BUGFRAME_bug, __LINE__, __FILE__,0 ) ); }
@@ -386,21 +357,13 @@ static always_inline void __vmread(unsigned long field, unsigned long *value) static always_inline void __vmwrite(unsigned long field, unsigned long value) { asmvolatile ( -#ifdef HAVE_AS_VMX"vmwrite %1, %0\n" -#else - VMWRITE_OPCODE MODRM_EAX_ECX -#endif/* CF==1 or ZF==1 --> BUG() */ UNLIKELY_START(be, vmwrite) _ASM_BUGFRAME_TEXT(0) UNLIKELY_END_SECTION : -#ifdef HAVE_AS_VMX : "r" (field) , "rm" (value), -#else - : "a" (field) , "c" (value), -#endif_ASM_BUGFRAME_INFO (BUGFRAME_bug, __LINE__, __FILE__,0 ) ); }
Signature · 1 line
2.34.1
PATCH 2/5 x86/vmx: remove HAVE_AS_EPT
In reply to [email protected]From: Denis Mukhin <[email protected]>
Remove the workaround under HAVE_AS_EPT for older compilers, as the minimally required GCC 5.1 / Binutils 2.25, and Clang 11 natively support the VMX instructions used in the hypervisor code.
--- xen/arch/x86/arch.mk | 1 - xen/arch/x86/include/asm/hvm/vmx/vmx.h | 16 ---------------- 2 files changed, 17 deletions(-)
xen/arch/x86/arch.mk +0 -1
@@ -11,7 +11,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,"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)
xen/arch/x86/include/asm/hvm/vmx/vmx.h +0 -16
@@ -429,21 +429,13 @@ static always_inline void __invept(unsigned long type, uint64_t eptp) type =INVEPT_ALL_CONTEXT ; asmvolatile ( -#ifdef HAVE_AS_EPT"invept %0, %1\n" -#else - INVEPT_OPCODE MODRM_EAX_08 -#endif/* CF==1 or ZF==1 --> BUG() */ UNLIKELY_START(be, invept) _ASM_BUGFRAME_TEXT(0) UNLIKELY_END_SECTION : -#ifdef HAVE_AS_EPT : "m" (operand), "r" (type), -#else - : "a" (&operand), "c" (type), -#endif _ASM_BUGFRAME_INFO(BUGFRAME_bug, __LINE__, __FILE__, 0) : "memory" ); }
@@ -458,11 +450,7 @@ static always_inline void __invvpid(unsigned long type, u16 vpid, u64 gva)/* Fix up #UD exceptions which occur when TLBs are flushed before VMXON. */ asmvolatile ("1: " -#ifdef HAVE_AS_EPT"invvpid %0, %1\n" -#else - INVVPID_OPCODE MODRM_EAX_08 -#endif/* CF==1 or ZF==1 --> BUG() */ UNLIKELY_START(be, invvpid)_ASM_BUGFRAME_TEXT (0 )
@@ -470,11 +458,7 @@ static always_inline void __invvpid(unsigned long type, u16 vpid, u64 gva) "2:" _ASM_EXTABLE(1b, 2b) : -#ifdef HAVE_AS_EPT : "m" (operand), "r" (type), -#else - : "a" (&operand), "c" (type), -#endif _ASM_BUGFRAME_INFO(BUGFRAME_bug, __LINE__, __FILE__, 0) : "memory" ); }
Signature · 1 line
2.34.1
PATCH 3/5 x86/vmx: remove GAS_VMX_OP()
In reply to [email protected]From: Denis Mukhin <[email protected]>
Remove unneeded GAS_VMX_OP() macro, now that all used VMX instuctions are natively supported by the baseline compiler.
--- xen/arch/x86/include/asm/hvm/vmx/vmx.h | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-)
xen/arch/x86/include/asm/hvm/vmx/vmx.h +6 -10
@@ -310,8 +310,6 @@ extern uint8_t posted_intr_vector;#define INVVPID_ALL_CONTEXT 2#define INVVPID_SINGLE_CONTEXT_RETAINING_GLOBAL 3 -# define GAS_VMX_OP (yes, no) yes - static always_inline void __vmptrld(u64 addr) { asm volatile (
@@ -374,14 +372,13 @@ static inline enum vmx_insn_errno vmread_safe(unsigned long field,unsigned long ret =VMX_INSN_SUCCEED ;bool fail_invalid, fail_valid; - asm volatile ( GAS_VMX_OP("vmread %[field], %[value]\n\t", - VMREAD_OPCODE MODRM_EAX_ECX) + asm volatile ( "vmread %[field], %[value]\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), - [value] GAS_VMX_OP("=rm", "=c") (*value) - : [field] GAS_VMX_OP("r", "a") (field)); + [value] "=rm" (*value) + : [field] "r" (field));if (unlikely (fail_invalid) ) ret =VMX_INSN_FAIL_INVALID ;
@@ -397,14 +394,13 @@ static inline enum vmx_insn_errno vmwrite_safe(unsigned long field,unsigned long ret =VMX_INSN_SUCCEED ;bool fail_invalid, fail_valid; - asm volatile ( GAS_VMX_OP("vmwrite %[value], %[field]\n\t", - VMWRITE_OPCODE MODRM_EAX_ECX) + 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] GAS_VMX_OP("r", "a") (field), - [value] GAS_VMX_OP("rm", "c") (value)); + : [field] "r" (field), + [value] "rm" (value));if (unlikely (fail_invalid) ) ret =VMX_INSN_FAIL_INVALID ;
Signature · 1 line
2.34.1
PATCH 4/5 x86/vmx: remove *_OPCODE
In reply to [email protected]From: Denis Mukhin <[email protected]>
Remove all *_OPCODE definitions from vmx.h now that all used VMX instructions are natively supported by the baseline compiler.
Use vmxon and vmxoff instructions directly.
Update __vmxon() to account for vmxon use.
Resolves: https://gitlab.com/xen-project/xen/-/work_items/202
--- xen/arch/x86/include/asm/hvm/vmx/vmx.h | 34 ++++++-------------------- 1 file changed, 8 insertions(+), 26 deletions(-)
xen/arch/x86/include/asm/hvm/vmx/vmx.h +8 -26
@@ -257,24 +257,6 @@ typedef union cr_access_qual {#define X86_SEG_AR_GRANULARITY (1u << 15)/* 15, granularity */ #define X86_SEG_AR_SEG_UNUSABLE (1u << 16)/* 16, segment unusable */ -#define VMCALL_OPCODE ".byte 0x0f,0x01,0xc1\n" -#define VMCLEAR_OPCODE ".byte 0x66,0x0f,0xc7\n"/* reg/opcode: /6 */ -#define VMLAUNCH_OPCODE ".byte 0x0f,0x01,0xc2\n" -#define VMPTRLD_OPCODE ".byte 0x0f,0xc7\n"/* reg/opcode: /6 */ -#define VMPTRST_OPCODE ".byte 0x0f,0xc7\n"/* reg/opcode: /7 */ -#define VMREAD_OPCODE ".byte 0x0f,0x78\n" -#define VMRESUME_OPCODE ".byte 0x0f,0x01,0xc3\n" -#define VMWRITE_OPCODE ".byte 0x0f,0x79\n" -#define INVEPT_OPCODE ".byte 0x66,0x0f,0x38,0x80\n"/* m128,r64/32 */ -#define INVVPID_OPCODE ".byte 0x66,0x0f,0x38,0x81\n"/* m128,r64/32 */ -#define VMXOFF_OPCODE ".byte 0x0f,0x01,0xc4\n" -#define VMXON_OPCODE ".byte 0xf3,0x0f,0xc7\n" - -#define MODRM_EAX_08 ".byte 0x08\n"/* ECX, [EAX] */ -#define MODRM_EAX_06 ".byte 0x30\n"/* [EAX], with reg/opcode: /6 */ -#define MODRM_EAX_07 ".byte 0x38\n"/* [EAX], with reg/opcode: /7 */ -#define MODRM_EAX_ECX ".byte 0xc1\n"/* EAX, ECX */ -extern uint8_t posted_intr_vector;#define cpu_has_vmx_ept_exec_only_supported \
@@ -497,9 +479,7 @@ static inline void vpid_sync_all(void)static inline void __vmxoff (void ) { - asmvolatile ( -VMXOFF_OPCODE - : : : "memory" ); + asmvolatile ("vmxoff" : : :"memory" ); }static inline int __vmxon (u64 addr)
@@ -507,15 +487,17 @@ static inline int __vmxon(u64 addr)int rc; asmvolatile ( -"1: " VMXON_OPCODE MODRM_EAX_06 "\n" -" setna %b0 ; neg %0\n" /* CF==1 or ZF==1 --> rc = -1 */ +"1: vmxon (%[addr])\n" +" setna %b[rc]\n" +" neg %[rc]\n" /* CF==1 or ZF==1 --> rc = -1 */ "2:\n" ".section .fixup,\"ax\"\n" -"3: sub $2,%0 ; jmp 2b\n" /* #UD or #GP --> rc = -2 */ +"3: mov $-2, %[rc]\n" +" jmp 2b\n" /* #UD or #GP --> rc = -2 */ ".previous\n" _ASM_EXTABLE(1b, 3b) - : "=q" (rc) - : "0" (0), "a" (&addr) + : [rc] "=q" (rc) + : "0" (0), [addr] "r" (&addr) : "memory"); return rc;
Signature · 1 line
2.34.1
Re: [PATCH v1 4/5] x86/vmx: remove *_OPCODE
Quoted 17 lines · On 01/04/2025 11:21 pm, [email protected] wrote:
xen/arch/x86/include/asm/hvm/vmx/vmx.h +4
-3@@ -507,15 +487,17 @@ static inline int __vmxon(u64 addr)int rc; asmvolatile ( -"1: " VMXON_OPCODE MODRM_EAX_06 "\n" -" setna %b0 ; neg %0\n" /* CF==1 or ZF==1 --> rc = -1 */ +"1: vmxon (%[addr])\n" +" setna %b[rc]\n" +" neg %[rc]\n" /* CF==1 or ZF==1 --> rc = -1 */ "2:\n" ".section .fixup,\"ax\"\n" - "3: sub $2,%0 ; jmp 2b\n" /* #UD or #GP --> rc = -2 */ + "3: mov $-2, %[rc]\n"
xen/arch/x86/include/asm/hvm/vmx/vmx.h static inline int __vmxon(u64 addr) · line 503
Quoted 10 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +3
-2+" jmp 2b\n" /* #UD or #GP --> rc = -2 */ ".previous\n" _ASM_EXTABLE(1b ,3b ) - : "=q" (rc) - : "0" (0), "a" (&addr) + : [rc] "=q" (rc) + : "0" (0), [addr] "r" (&addr) : "memory"); return rc;
A variant of this patch (improvements to __vmxon() helper, or whatever) probably wants pulling out and doing earlier.
For the function parameter, u64 addr wants to become paddr_t addr.
Use "int rc = 0;" and [rc] "+q" (rc). That takes away the "0" (0) that is otherwise unconnected.
Next, "vmxon %[addr]" and [addr] "m" (addr). The VMXON instruction strictly takes an m64 operand, and it doesn't need bouncing through another register.
Finally, __vmx{on,off}() have single callers only in vmcs.c, and really shouldn't be in vmx.h which is included ~everywhere. You can move them into vmcs.c (probably after parse_ept_param_runtime()) to limit their scope.
~Andrew
Re: [PATCH v1 4/5] x86/vmx: remove *_OPCODE
In reply to [email protected]xen/arch/x86/include/asm/hvm/vmx/vmx.h static inline void vpid_sync_all(void) · line 482
Quoted 8 lines · On 02.04.2025 00:21, [email protected] wrote:
xen/arch/x86/include/asm/hvm/vmx/vmx.h +1
-3@@ -497,9 +479,7 @@ static inline void vpid_sync_all(void)static inline void __vmxoff (void ) { - asmvolatile ( -VMXOFF_OPCODE - : : : "memory" ); + asmvolatile ("vmxoff" : : :"memory" );
Nit (style): Blanks immediately inside parentheses please. Ideally ...
Quoted 11 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +3
-2@@ -507,15 +487,17 @@ static inline int __vmxon(u64 addr)int rc; asmvolatile ( -"1: " VMXON_OPCODE MODRM_EAX_06 "\n" -" setna %b0 ; neg %0\n" /* CF==1 or ZF==1 --> rc = -1 */ +"1: vmxon (%[addr])\n" +" setna %b[rc]\n" +" neg %[rc]\n" /* CF==1 or ZF==1 --> rc = -1 */ "2:\n" ".section .fixup,\"ax\"\n"
xen/arch/x86/include/asm/hvm/vmx/vmx.h static inline int __vmxon(u64 addr) · line 501
Quoted 10 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +4
-3-"3: sub $2,%0 ; jmp 2b\n" /* #UD or #GP --> rc = -2 */ +"3: mov $-2, %[rc]\n" +" jmp 2b\n" /* #UD or #GP --> rc = -2 */ ".previous\n" _ASM_EXTABLE(1b ,3b ) - : "=q" (rc) - : "0" (0), "a" (&addr) + : [rc] "=q" (rc) + : "0" (0), [addr] "r" (&addr) : "memory");
... you'd also take the opportunity to add the missing one here. Then again Andrew eliminates this altogether anyway.
Jan
PATCH 5/5 x86/asm: remove HAVE_AS_CLAC_STAC
In reply to [email protected]From: Denis Mukhin <[email protected]>
The new toolchain baseline knows the STAC/CLAC instructions, no need to carry the workaround in the code.
Resolves: https://gitlab.com/xen-project/xen/-/work_items/203
--- xen/arch/x86/arch.mk | 1 - xen/arch/x86/include/asm/asm-defns.h | 10 ---------- 2 files changed, 11 deletions(-)
xen/arch/x86/arch.mk +0 -1
@@ -15,7 +15,6 @@ $(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,"clac",-DHAVE_AS_CLAC_STAC) $(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)
xen/arch/x86/include/asm/asm-defns.h +0 -10
@@ -1,15 +1,5 @@#include <asm/page-bits.h> -#ifndef HAVE_AS_CLAC_STAC -.macro clac - .byte 0x0f, 0x01, 0xca -.endm - -.macro stac - .byte 0x0f, 0x01, 0xcb -.endm -#endif - .macro vmrun .byte 0x0f, 0x01, 0xd8 .endm
Signature · 1 line
2.34.1
Re: [PATCH v1 5/5] x86/asm: remove HAVE_AS_CLAC_STAC
Quoted 7 lines · On 01/04/2025 11:21 pm, [email protected] wrote:
From: Denis Mukhin <[email protected]>
The new toolchain baseline knows the STAC/CLAC instructions, no need to carry the workaround in the code.
Resolves: https://gitlab.com/xen-project/xen/-/work_items/203
Signed-off-by Denis Mukhin <[email protected]>
Quoted 5 lines · On 01/04/2025 11:21 pm, [email protected] wrote:
Patches 1-4 remove various compiler workarounds in the hypervisor code for Intel's VMX instructions.
Patch 5 removes compiler workaround for missing clac/stac instructions.
Quoted 7 lines
CI: https://gitlab.com/xen-project/people/dmukhin/xen/-/pipelines/1747394875
Denis Mukhin (5):
x86/vmx: removeHAVE_AS_VMX
x86/vmx: removeHAVE_AS_EPT
x86/vmx: removeGAS_VMX_OP()
x86/vmx: remove*_OPCODE
These are fine, but probably ought to be merged because they're all really one thing.
When you've pulled the __vmx{on,off}() cleanup into a separate patch, the remains of patch 4 becomes straight deletion, which also means that
Resolves: https://gitlab.com/xen-project/xen/-/work_items/202 is the commit with almost all of the work in it.
~Andrew