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

[Xen-devel] [PATCH v1 1/6] 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/arch/x86/x86_64/asm-offsets.c      |  1 +
 xen/include/asm-x86/x86_64/asm_defns.h | 70 ++++++++++++++++++++++++++++++++++
 2 files changed, 71 insertions(+)

diff --git a/xen/arch/x86/x86_64/asm-offsets.c 
b/xen/arch/x86/x86_64/asm-offsets.c
index b0098b3..fa4cbb6 100644
--- a/xen/arch/x86/x86_64/asm-offsets.c
+++ b/xen/arch/x86/x86_64/asm-offsets.c
@@ -160,6 +160,7 @@ void __dummy__(void)
     BLANK();
 
     OFFSET(CPUINFO86_ext_features, struct cpuinfo_x86, x86_capability[1]);
+    OFFSET(CPUINFO86_leaf7_features, struct cpuinfo_x86, x86_capability[7]);
     BLANK();
 
     OFFSET(MB_flags, multiboot_info_t, flags);
diff --git a/xen/include/asm-x86/x86_64/asm_defns.h 
b/xen/include/asm-x86/x86_64/asm_defns.h
index bf63ac1..6805629 100644
--- a/xen/include/asm-x86/x86_64/asm_defns.h
+++ b/xen/include/asm-x86/x86_64/asm_defns.h
@@ -228,4 +228,74 @@ __asm__(                                        \
 # define _ASM_EX(p) #p "-."
 #endif
 
+/* "Raw" instruction opcodes */
+#define __ASM_CLAC      .byte 0x0f,0x01,0xca
+#define __ASM_STAC      .byte 0x0f,0x01,0xcb
+
+/* Indirect stringification.  Doing two levels allows the parameter to be a
+ * macro itself.  For example, compile with -DFOO=bar, __stringify(FOO)
+ * converts to "bar".
+ */
+#define __stringify_1(x...)     #x
+#define __stringify(x...)       __stringify_1(x)
+
+#ifdef __ASSEMBLY__
+#define X86_FEATURE_SMAP    (7*32+20)
+#define ASM_STAC                                         \
+        pushq %rax;                                      \
+        leaq boot_cpu_data(%rip),%rax;                   \
+        btl $X86_FEATURE_SMAP-7*32, CPUINFO86_leaf7_features(%rax);  \
+        jnc 881f;                                        \
+        movq %cr4,%rax;                                  \
+        testl $X86_CR4_SMAP,%eax;                        \
+        jz 881f;                                         \
+        __ASM_STAC;                                      \
+881:    popq %rax
+
+#define ASM_CLAC                                         \
+        pushq %rax;                                      \
+        leaq boot_cpu_data(%rip),%rax;                   \
+        btl $X86_FEATURE_SMAP-7*32, CPUINFO86_leaf7_features(%rax);  \
+        jnc 881f;                                        \
+        movq %cr4,%rax;                                  \
+        testl $X86_CR4_SMAP,%eax;                        \
+        jz 881f;                                         \
+        __ASM_CLAC;                                      \
+881:    popq %rax
+#else
+#define ASM_STAC                                         \
+        "\npushq %%rax\n\t"                              \
+        "leaq boot_cpu_data(%%rip),%%rax\n\t"            \
+        "btl $" __stringify(X86_FEATURE_SMAP) "-7*32,"   \
+        __stringify(CPUINFO86_leaf7_features) "(%%rax)\n\t"  \
+        "jnc 881f\n\t"                                   \
+        "movq %%cr4,%%rax\n\t"                           \
+        "testl $" __stringify(X86_CR4_SMAP) ",%%eax\n\t" \
+        "jz 881f\n\t"                                    \
+         __stringify(__ASM_STAC) "\n\t"                  \
+"881:    popq %%rax"
+
+#define ASM_CLAC                                         \
+        "\npushq %%rax\n\t"                              \
+        "leaq boot_cpu_data(%%rip),%%rax\n\t"            \
+        "btl $" __stringify(X86_FEATURE_SMAP) "-7*32,"   \
+        __stringify(CPUINFO86_leaf7_features) "(%%rax)\n\t"  \
+        "jnc 881f\n\t"                                   \
+        "movq %%cr4,%%rax\n\t"                           \
+        "testl $" __stringify(X86_CR4_SMAP) ",%%eax\n\t" \
+        "jz 881f\n\t"                                    \
+         __stringify(__ASM_CLAC) "\n\t"                  \
+"881:   popq %%rax"
+
+static inline void clac(void)
+{
+    asm volatile (ASM_CLAC : : : "memory");
+}
+
+static inline void stac(void)
+{
+    asm volatile (ASM_STAC : : : "memory");
+}
+#endif
+
 #endif /* __X86_64_ASM_DEFNS_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®.