[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [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.

Signed-off-by: Feng Wu <feng.wu@xxxxxxxxx>
---
 xen/include/asm-x86/asm_defns.h  | 46 ++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-x86/cpufeature.h |  4 ++++
 2 files changed, 50 insertions(+)

diff --git a/xen/include/asm-x86/asm_defns.h b/xen/include/asm-x86/asm_defns.h
index e8fa989..3b9788b 100644
--- a/xen/include/asm-x86/asm_defns.h
+++ b/xen/include/asm-x86/asm_defns.h
@@ -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__ */
diff --git a/xen/include/asm-x86/cpufeature.h b/xen/include/asm-x86/cpufeature.h
index 0c4d6c1..3dfb875 100644
--- a/xen/include/asm-x86/cpufeature.h
+++ b/xen/include/asm-x86/cpufeature.h
@@ -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 */
 
-- 
1.8.3.1


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
http://lists.xen.org/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.