x86: Add support for STAC/CLAC instructions
PATCH v3 3/9 x86: Add support for STAC/CLAC instructions
The STAC/CLAC instructions are only available when SMAP is enabled, but on the other hand they aren't needed if SMAP is not available, or before we start to run userspace, in that case, the functions and macros do nothing.
--- xen/include/asm-x86/asm_defns.h | 46 ++++++++++++++++++++++++++++++++++++++++ xen/include/asm-x86/cpufeature.h | 4 ++++ 2 files changed, 50 insertions(+)
xen/include/asm-x86/asm_defns.h +46 -0
@@ -7,6 +7,8 @@#include <asm/asm-offsets.h> #endif #include <asm/processor.h> +#include <xen/stringify.h> +#include <asm/cpufeature.h> #ifndef __ASSEMBLY__void ret_from_intr (void );
@@ -105,4 +107,48 @@ void ret_from_intr(void);#endif +/* "Raw" instruction opcodes */ +#define __ASM_CLAC .byte 0x0f,0x01,0xca +#define __ASM_STAC .byte 0x0f,0x01,0xcb + +#ifdef __ASSEMBLY__ +#define CPUINFO_FEATURE_OFFSET (feature) \ + (((feature >> 3) & ~3) + CPUINFO_features) + +#define ASM_AC (op) \ + btl $X86_FEATURE_SMAP & 31, \ + CPUINFO_FEATURE_OFFSET(X86_FEATURE_SMAP)+boot_cpu_data(%rip); \ + jnc 881f; \ + op; \ +881: + +#define ASM_STAC ASM_AC(__ASM_STAC) +#define ASM_CLAC ASM_AC(__ASM_CLAC) +#else +#define CPUINFO_FEATURE_OFFSET (feature) \ + "(((" __stringify(feature) ">> 3) & ~3) + " \ + __stringify(CPUINFO_features)")" + +#define ASM_AC (op) \ + "btl $" __stringify(X86_FEATURE_SMAP) "&31, " \ + CPUINFO_FEATURE_OFFSET(X86_FEATURE_SMAP) \ + "+ boot_cpu_data(%%rip)\n\t" \ + "jnc 881f\n\t" \ + __stringify(op) "\n\t" \ +"881:" + +#define ASM_STAC ASM_AC(__ASM_STAC) +#define ASM_CLAC ASM_AC(__ASM_CLAC) + +static inline void clac (void ) +{ + asm volatile (ASM_CLAC : : : "memory"); +} + +static inline void stac (void ) +{ + asm volatile (ASM_STAC : : : "memory"); +} +#endif +#endif /* __X86_ASM_DEFNS_H__ */
xen/include/asm-x86/cpufeature.h +4 -0
@@ -7,7 +7,9 @@#ifndef __ASM_I386_CPUFEATURE_H#define __ASM_I386_CPUFEATURE_H +#ifndef __ASSEMBLY__#include <xen/bitops.h> +#endif #define NCAPINTS 8/* N 32-bit words worth of info */
@@ -151,6 +153,7 @@#define X86_FEATURE_ADX (7*32+19)/* ADCX, ADOX instructions */ #define X86_FEATURE_SMAP (7*32+20)/* Supervisor Mode Access Prevention */ +#ifndef __ASSEMBLY__#define cpu_has (c, bit) test_bit(bit, (c)->x86_capability)#define boot_cpu_has (bit) test_bit(bit, boot_cpu_data.x86_capability)#define cpufeat_mask (idx) (1u << ((idx) & 31))
@@ -209,6 +212,7 @@#define cpu_has_vmx boot_cpu_has(X86_FEATURE_VMXE)#define cpu_has_cpuid_faulting boot_cpu_has(X86_FEATURE_CPUID_FAULTING) +#endif #endif /* __ASM_I386_CPUFEATURE_H */
Signature · 1 line
1.8.3.1
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 28.04.14 at 05:15, <[email protected]> wrote:
The STAC/CLAC instructions are only available when SMAP is enabled, but on the other hand they aren't needed if SMAP is not available,
I think you got the "enabled" and "available" mixed up here.
xen/include/asm-x86/asm_defns.h void ret_from_intr(void); · line 116
Quoted 11 lines
xen/include/asm-x86/asm_defns.h +7
-0@@ -105,4 +107,48 @@ void ret_from_intr(void);#endif +/* "Raw" instruction opcodes */ +#define __ASM_CLAC .byte 0x0f,0x01,0xca +#define __ASM_STAC .byte 0x0f,0x01,0xcb + +#ifdef __ASSEMBLY__ +#define CPUINFO_FEATURE_OFFSET (feature) \ + (((feature >> 3) & ~3) + CPUINFO_features)
I gues this wants to go into patch 1.
xen/include/asm-x86/asm_defns.h void ret_from_intr(void); · line 126
Quoted 10 lines
xen/include/asm-x86/asm_defns.h +10
-0+ +#define ASM_AC (op) \ + btl $X86_FEATURE_SMAP & 31, \ + CPUINFO_FEATURE_OFFSET(X86_FEATURE_SMAP)+boot_cpu_data(%rip); \ + jnc 881f; \ + op; \ +881: + +#define ASM_STAC ASM_AC(__ASM_STAC) +#define ASM_CLAC ASM_AC(__ASM_CLAC)
Please pass only STAC/CLAC here, and concatenate the __ASM_ onto them inside the macro (reducing the chance of abuse and slightly increasing readability).
Quoted 5 lines
xen/include/asm-x86/asm_defns.h +5
-0+#else +#define CPUINFO_FEATURE_OFFSET (feature) \ + "(((" __stringify(feature) ">> 3) & ~3) + " \ + __stringify(CPUINFO_features)")" +
xen/include/asm-x86/asm_defns.h void ret_from_intr(void); · line 141
Quoted 10 lines
xen/include/asm-x86/asm_defns.h +10
-0+#define ASM_AC (op) \ + "btl $" __stringify(X86_FEATURE_SMAP) "&31, " \ + CPUINFO_FEATURE_OFFSET(X86_FEATURE_SMAP) \ + "+ boot_cpu_data(%%rip)\n\t" \ + "jnc 881f\n\t" \ + __stringify(op) "\n\t" \ +"881:" + +#define ASM_STAC ASM_AC(__ASM_STAC) +#define ASM_CLAC ASM_AC(__ASM_CLAC)
Also, with these now being identical between asm and C, they could move ahead of the conditional block.
Jan