All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1 1/6] x86: Add support for STAC/CLAC instructions
@ 2014-04-15 13:01 Feng Wu
  2014-04-15  8:40 ` Jan Beulich
  0 siblings, 1 reply; 13+ messages in thread
From: Feng Wu @ 2014-04-15 13:01 UTC (permalink / raw)
  To: JBeulich, Ian.Campbell, xen-devel; +Cc: Feng Wu, eddie.dong, jun.nakajima

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@intel.com>
---
 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

^ permalink raw reply related	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2014-04-23 15:59 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-04-15 13:01 [PATCH v1 1/6] x86: Add support for STAC/CLAC instructions Feng Wu
2014-04-15  8:40 ` Jan Beulich
2014-04-22  7:41   ` Wu, Feng
2014-04-22  8:07     ` Jan Beulich
2014-04-22  8:46       ` Wu, Feng
2014-04-22  9:17         ` Jan Beulich
2014-04-22 12:19           ` Wu, Feng
2014-04-22 13:09             ` Konrad Rzeszutek Wilk
2014-04-23 13:43               ` Wu, Feng
2014-04-23 14:52                 ` Is: alternative_asm as dependency for STAC/CLAC/new features? Was:Re: " Konrad Rzeszutek Wilk
2014-04-23 15:59                   ` Is: alternative_asm as dependency for STAC/CLAC/new features? Jan Beulich
2014-04-22  9:43       ` [PATCH v1 1/6] x86: Add support for STAC/CLAC instructions Andrew Cooper
2014-04-22  9:48         ` Jan Beulich

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.