Skip to contentSkip to conversation

x86: Add support for STAC/CLAC instructions

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

v5 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Feng Wu

author
#0 lore raw

PATCH v5 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 | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
xen/include/asm-x86/asm_defns.h +30 -0
index c4b2bb2..5aaba1a 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);
@@ -159,6 +161,34 @@ 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 ASM_AC(op)                                       \
+        btl $X86_FEATURE_SMAP & 31,                      \
+        CPUINFO_FEATURE_OFFSET(X86_FEATURE_SMAP)+boot_cpu_data(%rip); \
+        jnc 881f;                                        \
+        __ASM_##op;                                      \
+881:
+
+#define ASM_STAC ASM_AC(STAC)
+#define ASM_CLAC ASM_AC(CLAC)
+#else
+static inline void clac(void)
+{
+    if ( boot_cpu_has(X86_FEATURE_SMAP) )
+        asm volatile (__stringify(__ASM_CLAC));
+}
+
+static inline void stac(void)
+{
+    if ( boot_cpu_has(X86_FEATURE_SMAP) )
+        asm volatile (__stringify(__ASM_STAC));
+}
+#endif
+
 #ifdef __ASSEMBLY__
 .macro SAVE_ALL
         addq  $-(UREGS_error_code-UREGS_r15), %rsp
Signature · 1 line
1.8.3.1
#1 lore raw

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

Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 06.05.14 at 12:07, <[email protected]> wrote:
xen/include/asm-x86/asm_defns.h +12 -0
+static inline void clac(void)
+{
+    if ( boot_cpu_has(X86_FEATURE_SMAP) )
+        asm volatile (__stringify(__ASM_CLAC));
+}
+
+static inline void stac(void)
+{
+    if ( boot_cpu_has(X86_FEATURE_SMAP) )
+        asm volatile (__stringify(__ASM_STAC));
+}
+#endif

Hmm, did you really mean to lose the memory clobbers here? In the previous round's review I specifically said that this conversion to C is likely only safe due to their presence.

Jan