Skip to contentSkip to conversation

x86: Add support for STAC/CLAC instructions

PATCH Superseded Feng Wu 2 participants 2 messages May 5, 2014

v4 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Feng Wu

author
#0 lore raw

PATCH v4 4/10 x86: Add support for STAC/CLAC instructions

The STAC/CLAC instructions are only available when SMAP feature is available, but on the other hand they aren't needed if SMAP is not enabled, or before we start to run userspace, in that case, the functions and macros do nothing.

---
 xen/include/asm-x86/asm_defns.h | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
xen/include/asm-x86/asm_defns.h +36 -0
index 852d92a..0eec635 100644
@@ -8,6 +8,8 @@
 #endif
 #include <asm/processor.h>
 #include <asm/percpu.h>
+#include <xen/stringify.h>
+#include <asm/cpufeature.h>
 
 #ifndef __ASSEMBLY__
 void ret_from_intr(void);
@@ -162,6 +164,40 @@ void ret_from_intr(void);
         __stringify(CPUINFO_features)")"
 #endif
 
+/* "Raw" instruction opcodes */
+#define __ASM_CLAC      .byte 0x0f,0x01,0xca
+#define __ASM_STAC      .byte 0x0f,0x01,0xcb
+
+#define ASM_STAC ASM_AC(STAC)
+#define ASM_CLAC ASM_AC(CLAC)
+
+#ifdef __ASSEMBLY__
+#define ASM_AC(op)                                       \
+        btl $X86_FEATURE_SMAP & 31,                      \
+        CPUINFO_FEATURE_OFFSET(X86_FEATURE_SMAP)+boot_cpu_data(%rip); \
+        jnc 881f;                                        \
+        __ASM_##op;                                      \
+881:
+#else
+#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(__ASM_##op) "\n\t"                  \
+"881:"
+
+static inline void clac(void)
+{
+    asm volatile (ASM_CLAC : : : "memory");
+}
+
+static inline void stac(void)
+{
+    asm volatile (ASM_STAC : : : "memory");
+}
+#endif
+
 #ifdef __ASSEMBLY__
 .macro SAVE_ALL
         addq  $-(UREGS_error_code-UREGS_r15), %rsp
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 05.05.14 at 10:21, <[email protected]> wrote:
xen/include/asm-x86/asm_defns.h +5 -0
+#ifdef __ASSEMBLY__
+#define ASM_AC(op)                                       \
+        btl $X86_FEATURE_SMAP & 31,                      \
+        CPUINFO_FEATURE_OFFSET(X86_FEATURE_SMAP)+boot_cpu_data(%rip); \
+        jnc 881f;                                        \

xen/include/asm-x86/asm_defns.h void ret_from_intr(void); · line 188

Quoted 10 lines
xen/include/asm-x86/asm_defns.h +10 -0
+        __ASM_##op;                                      \
+881:
+#else
+#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(__ASM_##op) "\n\t"                  \
+"881:"

Please use something like .L##op##%= as the label name for the C version ...

xen/include/asm-x86/asm_defns.h void ret_from_intr(void); · line 198

Quoted 9 lines
xen/include/asm-x86/asm_defns.h +9 -0
+static inline void clac(void)
+{
+    asm volatile (ASM_CLAC : : : "memory");
+}
+
+static inline void stac(void)
+{
+    asm volatile (ASM_STAC : : : "memory");
+}

... provided it wouldn't be better anyway to do all this properly in C, with just the raw instructions emitted via asm(). That would also eliminate the need for the C variant of CPUINFO_FEATURE_OFFSET() in the first patch.

Jan