x86: Add "safe" macros to wrap ASM_TRY()
Sean Christopherson
reviewercover x86: Add "safe" macros to wrap ASM_TRY()
Provide macros to wrap single instructions with ASM_TRY(), and to automagically report success/fault as appropriate. In other words, make it easier to write code for testing (non)faulting instruction behavior.
Sean Christopherson (5):
x86: Add macros to wrap ASM_TRY() for single instructions
x86: Convert inputs-only "safe" instruction helpers to asm_safe()
x86: Add macros to wrap ASM_TRY() for single instructions with
output(s)
x86: Move invpcid_safe() to processor.h and convert to asm_safe()
x86: Move XSETBV and XGETBV "safe" helpers to processor.h
lib/x86/desc.h | 48 ++++++++++++++++++++++++++ lib/x86/processor.h | 82 +++++++++++++++++++++++++-------------------- x86/memory.c | 60 ++++++++------------------------- x86/pcid.c | 8 ----- x86/xsave.c | 31 ++--------------- 5 files changed, 110 insertions(+), 119 deletions(-)
base-commit: 408e9eaae1c628b4895c213ee1eeb9ab3f42effb
Signature · 1 line
2.40.0.348.gf938b09366-goog
Sean Christopherson
reviewerPATCH 1/5 x86: Add macros to wrap ASM_TRY() for single instructions
Add asm_safe(), asm_safe_report(), and asm_safe_report_ex() macros to reduce the boilerplate needed for using ASM_TRY() with a single instruction.
Convert the memory test to the report variants to showcase the usage.
--- lib/x86/desc.h | 26 ++++++++++++++++++++++ x86/memory.c | 60 ++++++++++++-------------------------------------- 2 files changed, 40 insertions(+), 46 deletions(-)
lib/x86/desc.h +26 -0
@@ -272,4 +272,30 @@ extern gdt_entry_t *get_tss_descr(void);extern unsigned long get_gdt_entry_base (gdt_entry_t *entry);extern unsigned long get_gdt_entry_limit (gdt_entry_t *entry); +#define asm_safe(insn, inputs...) \ +({ \ + asm volatile(ASM_TRY("1f") \ + insn "\n\t" \ + "1:\n\t" \ + : \ + : inputs \ + : "memory"); \ + exception_vector(); \ +}) + +#define __asm_safe_report(want, insn, inputs...) \ +do { \ + int vector = asm_safe(insn, inputs); \ + \ + report(vector == want, "Expected %s on '%s', got %s", \ + want ? exception_mnemonic(want) : "SUCCESS", \ + insn, \ + vector ? exception_mnemonic(vector) : "SUCCESS"); \ +} while (0) + +#define asm_safe_report(insn, inputs...) \ + __asm_safe_report(0, insn, inputs) + +#define asm_safe_report_ex __asm_safe_report +#endif
x86/memory.c +14 -46
90 lines not on this page · raw message
Signature · 1 line
2.40.0.348.gf938b09366-goog
Sean Christopherson
reviewerPATCH 2/5 x86: Convert inputs-only "safe" instruction helpers to asm_safe()
In reply to Sean ChristophersonConvert processor.h's "safe" helpers that do not have outputs to use asm_safe() instead of open coding the equivalent. Leave instructions without outputs for a separate, future change as handling outputs is a bit more complex.
--- lib/x86/processor.h | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-)
lib/x86/processor.h +4 -17
@@ -434,11 +434,7 @@ static inline int wrmsr_safe(u32 index, u64 val) {u32 a = val, d = val >>32 ; - asm volatile (ASM_TRY("1f") - "wrmsr\n\t" - "1:" - : : "a"(a), "d"(d), "c"(index) : "memory"); -return exception_vector (); +return asm_safe ("wrmsr" ,"a" (a),"d" (d),"c" (index)); }static inline int rdpmc_safe (u32 index,uint64_t *val)
@@ -465,10 +461,7 @@ static inline uint64_t rdpmc(uint32_t index)static inline int write_cr0_safe (ulong val) { - asm volatile(ASM_TRY("1f") - "mov %0,%%cr0\n\t" - "1:": : "r" (val)); -return exception_vector (); +return asm_safe ("mov %0,%%cr0" ,"r" (val)); }static inline void write_cr0 (ulong val)
@@ -500,10 +493,7 @@ static inline ulong read_cr2(void)static inline int write_cr3_safe (ulong val) { - asm volatile(ASM_TRY("1f") - "mov %0,%%cr3\n\t" - "1:": : "r" (val)); -return exception_vector (); +return asm_safe ("mov %0,%%cr3" ,"r" (val)); }static inline void write_cr3 (ulong val)
@@ -528,10 +518,7 @@ static inline void update_cr3(void *cr3)static inline int write_cr4_safe (ulong val) { - asm volatile(ASM_TRY("1f") - "mov %0,%%cr4\n\t" - "1:": : "r" (val)); -return exception_vector (); +return asm_safe ("mov %0,%%cr4" ,"r" (val)); }static inline void write_cr4 (ulong val)
Signature · 1 line
2.40.0.348.gf938b09366-goog
Sean Christopherson
reviewerPATCH 3/5 x86: Add macros to wrap ASM_TRY() for single instructions with output(s)
In reply to Sean ChristophersonAdd macros for "safe" execution of instructions with outputs. Outputs require dedicated macros as the variadic arguments shenanigans only work for one type, i.e. can either be used for inputs or outputs, not both.
Opportunistically provide a common macro for RDMSR and RDPMC, the common macro can also be used for xgetbv_safe().
--- lib/x86/desc.h | 22 ++++++++++++++++++++++ lib/x86/processor.h | 34 ++++++++++++++++------------------ 2 files changed, 38 insertions(+), 18 deletions(-)
lib/x86/desc.h +22 -0
@@ -283,6 +283,28 @@ extern unsigned long get_gdt_entry_limit(gdt_entry_t *entry);exception_vector (); \ }) +#define asm_safe_out1(insn, output, inputs...) \ +({ \ + asm volatile(ASM_TRY("1f") \ + insn "\n\t" \ + "1:\n\t" \ + : output \ + : inputs \ + : "memory"); \ + exception_vector(); \ +}) + +#define asm_safe_out2(insn, output1, output2, inputs...) \ +({ \ + asm volatile(ASM_TRY("1f") \ + insn "\n\t" \ + "1:\n\t" \ + : output1, output2 \ + : inputs \ + : "memory"); \ + exception_vector(); \ +}) + #define __asm_safe_report(want, insn, inputs...) \ do { \ int vector = asm_safe(insn, inputs); \
lib/x86/processor.h +16 -18
@@ -416,18 +416,23 @@ static inline void wrmsr(u32 index, u64 val) asmvolatile ("wrmsr" : :"a" (a),"d" (d),"c" (index) :"memory" ); } +#define rdreg64_safe (insn, index, val) \ +({ \ + uint32_t a, d; \ + int vector; \ + \ + vector = asm_safe_out2(insn, "=a"(a), "=d"(d), "c"(index)); \ + \ + if (vector) \ + *(val) = 0; \ + else \ + *(val) = (uint64_t)a | ((uint64_t)d << 32); \ + vector; \ +}) +static inline int rdmsr_safe (u32 index,uint64_t *val) { -uint32_t a, d; - - asm volatile (ASM_TRY("1f") - "rdmsr\n\t" - "1:" - : "=a"(a), "=d"(d) - : "c"(index) : "memory"); - - *val = (uint64_t )a | ((uint64_t )d <<32 ); -return exception_vector (); +return rdreg64_safe ("rdmsr" , index, val); }static inline int wrmsr_safe (u32 index,u64 val)
@@ -439,14 +444,7 @@ static inline int wrmsr_safe(u32 index, u64 val)static inline int rdpmc_safe (u32 index,uint64_t *val) { -uint32_t a, d; - - asm volatile (ASM_TRY("1f") - "rdpmc\n\t" - "1:" - : "=a"(a), "=d"(d) : "c"(index) : "memory"); - *val = (uint64_t )a | ((uint64_t )d <<32 ); -return exception_vector (); +return rdreg64_safe ("rdpmc" , index, val); }static inline uint64_t rdpmc (uint32_t index)
Signature · 1 line
2.40.0.348.gf938b09366-goog
Sean Christopherson
reviewerPATCH 4/5 x86: Move invpcid_safe() to processor.h and convert to asm_safe()
In reply to Sean ChristophersonMove invpcid_safe() to processor.h so that it can be used by other tests, and convert it to use asm_safe() instead of open coding ASM_TRY() usage.
--- lib/x86/processor.h | 7 +++++++ x86/pcid.c | 8 -------- 2 files changed, 7 insertions(+), 8 deletions(-)
lib/x86/processor.h +7 -0
@@ -737,6 +737,13 @@ static inline void invlpg(volatile void *va) asmvolatile ("invlpg (%0)" ::"r" (va) :"memory" ); } + +static inline int invpcid_safe (unsigned long type,void *desc) +{ +/* invpcid (%rax), %rbx */ +return asm_safe (".byte 0x66,0x0f,0x38,0x82,0x18" ,"a" (desc),"b" (type)); +} +static inline void safe_halt (void ) { asmvolatile ("sti; hlt" );
x86/pcid.c +0 -8
@@ -10,14 +10,6 @@ struct invpcid_desc { u64 addr : 64; }; -static int invpcid_safe (unsigned long type,void *desc) -{ - asm volatile (ASM_TRY("1f") - ".byte 0x66,0x0f,0x38,0x82,0x18 \n\t" /* invpcid (%rax), %rbx */ - "1:" : : "a" (desc), "b" (type)); -return exception_vector (); -} -static void test_pcid_enabled (void ) {int passed =0 ;
Signature · 1 line
2.40.0.348.gf938b09366-goog
Sean Christopherson
reviewerPATCH 5/5 x86: Move XSETBV and XGETBV "safe" helpers to processor.h
In reply to Sean ChristophersonMove the "safe" helpers for XSETBV and XGETBV to processor.h, and convert them to use asm_safe() and other common macros as appropriate.
--- lib/x86/processor.h | 22 +++++++++++++++++++--- x86/xsave.c | 31 +++---------------------------- 2 files changed, 22 insertions(+), 31 deletions(-)
lib/x86/processor.h +19 -3
@@ -430,6 +430,14 @@ static inline void wrmsr(u32 index, u64 val) vector; \ }) +#define wrreg64_safe (insn, index, val) \ +({ \ + uint32_t eax = (val), edx = (val) >> 32; \ + \ + asm_safe(insn, "a" (eax), "d" (edx), "c" (index)); \ +}) + +static inline int rdmsr_safe (u32 index,uint64_t *val) {return rdreg64_safe ("rdmsr" , index, val);
@@ -437,9 +445,7 @@ static inline int rdmsr_safe(u32 index, uint64_t *val)static inline int wrmsr_safe (u32 index,u64 val) { -u32 a = val, d = val >>32 ; - -return asm_safe ("wrmsr" ,"a" (a),"d" (d),"c" (index)); +return wrreg64_safe ("wrmsr" , index, val); }static inline int rdpmc_safe (u32 index,uint64_t *val)
@@ -457,6 +463,16 @@ static inline uint64_t rdpmc(uint32_t index)return val; } +static inline int xgetbv_safe (u32 index,u64 *result) +{ +return rdreg64_safe (".byte 0x0f,0x01,0xd0" , index, result); +} + +static inline int xsetbv_safe (u32 index,u64 value) +{ +return wrreg64_safe (".byte 0x0f,0x01,0xd1" , index, value); +} +static inline int write_cr0_safe (ulong val) {return asm_safe ("mov %0,%%cr0" ,"r" (val));
x86/xsave.c +3 -28
@@ -8,31 +8,6 @@#define uint64_t unsigned long long#endif -static int xgetbv_checking (u32 index,u64 *result) -{ -u32 eax, edx; - - asm volatile(ASM_TRY("1f") - ".byte 0x0f,0x01,0xd0\n\t" /* xgetbv */ - "1:" - : "=a" (eax), "=d" (edx) - : "c" (index)); - *result = eax + ((u64 )edx <<32 ); -return exception_vector (); -} - -static int xsetbv_safe (u32 index,u64 value) -{ -u32 eax = value; -u32 edx = value >>32 ; - - asm volatile(ASM_TRY("1f") - ".byte 0x0f,0x01,0xd1\n\t" /* xsetbv */ - "1:" - : : "a" (eax), "d" (edx), "c" (index)); -return exception_vector (); -} -static uint64_t get_supported_xcr0 (void ) {struct cpuid r;
@@ -78,7 +53,7 @@ static void test_xsave(void) test_bits =XSTATE_FP |XSTATE_SSE ;report (xsetbv_safe (XCR_XFEATURE_ENABLED_MASK , test_bits) ==0 ,"\t\txsetbv(XCR_XFEATURE_ENABLED_MASK, XSTATE_FP | XSTATE_SSE)" ); -report (xgetbv_checking (XCR_XFEATURE_ENABLED_MASK , &xcr0) ==0 , +report (xgetbv_safe (XCR_XFEATURE_ENABLED_MASK , &xcr0) ==0 ," xgetbv(XCR_XFEATURE_ENABLED_MASK)" );printf ("\tIllegal tests\n" );
@@ -123,7 +98,7 @@ static void test_xsave(void) "\t\txsetbv(XCR_XFEATURE_ENABLED_MASK, XSTATE_FP | XSTATE_SSE) - expect #UD");printf ("\tIllegal tests:\n" ); -report (xgetbv_checking (XCR_XFEATURE_ENABLED_MASK , &xcr0) ==UD_VECTOR , +report (xgetbv_safe (XCR_XFEATURE_ENABLED_MASK , &xcr0) ==UD_VECTOR ,"\txgetbv(XCR_XFEATURE_ENABLED_MASK) - expect #UD" ); }
@@ -141,7 +116,7 @@ static void test_no_xsave(void)report (write_cr4_safe (cr4 |X86_CR4_OSXSAVE ) ==GP_VECTOR ,"Set OSXSAVE in CR4 - expect #GP" ); -report (xgetbv_checking (XCR_XFEATURE_ENABLED_MASK , &xcr0) ==UD_VECTOR , +report (xgetbv_safe (XCR_XFEATURE_ENABLED_MASK , &xcr0) ==UD_VECTOR ,"Execute xgetbv - expect #UD" ); report(xsetbv_safe(XCR_XFEATURE_ENABLED_MASK, 0x3) == UD_VECTOR,
Signature · 1 line
2.40.0.348.gf938b09366-goog
Quoted 12 lines · Sean Christopherson
Provide macros to wrap single instructions with
ASM_TRY(), and to automagically report success/fault as appropriate. In other words, make it easier to write code for testing (non)faulting instruction behavior.Sean Christopherson (5):
x86: Add macros to wrapASM_TRY()for single instructions
x86: Convert inputs-only "safe" instruction helpers toasm_safe()
x86: Add macros to wrapASM_TRY()for single instructions with
output(s)
x86: Moveinvpcid_safe()toprocessor.hand convert toasm_safe()
x86: Move XSETBV and XGETBV "safe" helpers toprocessor.h
Quoted 1 line
[...]
Applied to kvm-x86 next, thanks!
[1/5] x86: Add macros to wrap ASM_TRY() for single instructions
https://github.com/kvm-x86/kvm-unit-tests/commit/72e2642f2aaf [2/5] x86: Convert inputs-only "safe" instruction helpers to asm_safe() https://github.com/kvm-x86/kvm-unit-tests/commit/b470217dd120 [3/5] x86: Add macros to wrap ASM_TRY() for single instructions with output(s) https://github.com/kvm-x86/kvm-unit-tests/commit/82f7d076088b [4/5] x86: Move invpcid_safe() to processor.h and convert to asm_safe() https://github.com/kvm-x86/kvm-unit-tests/commit/8ddc0f0c2d39 [5/5] x86: Move XSETBV and XGETBV "safe" helpers to processor.h https://github.com/kvm-x86/kvm-unit-tests/commit/700c2b7e5007