All of lore.kernel.org
 help / color / mirror / Atom feed
* [MODERATED] [patch 6/8] [PATCH v1.3.1 6/7] Linux Patch 6
@ 2018-04-13  2:26 konrad.wilk
  0 siblings, 0 replies; only message in thread
From: konrad.wilk @ 2018-04-13  2:26 UTC (permalink / raw)
  To: speck

These macros are wrapped with alternative assembler so that
they won't be enabled if memory disambiguation disablement is
turned on for boot.

But if the user picks 'mdd=userspace' then these macros
will frob the SPEC_CTRL MSR with the appropiate value.

TODO:
 - Perhaps use what Tim Chen posted and add lfence and so
   on so that you can also do IBRS?
 - Or alternatively make this more pretty?

---
v3.1: Ripped it out from the KVM commit, not sure why it made
  it in there.

Signed-off-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
---
 arch/x86/include/asm/nospec-branch.h | 76 ++++++++++++++++++++++++++++++++++++
 arch/x86/kernel/cpu/bugs.c           |  2 +
 2 files changed, 78 insertions(+)

diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h
index c3856b3f42eb..a53919a3a712 100644
--- a/arch/x86/include/asm/nospec-branch.h
+++ b/arch/x86/include/asm/nospec-branch.h
@@ -148,6 +148,82 @@
 #endif
 .endm
 
+.extern spec_ctrl_priv
+.extern spec_ctrl_unpriv
+
+#define __ASM_ENABLE_SPEC_CTRL			\
+	pushq %rax;				\
+	pushq %rcx;				\
+	pushq %rdx;				\
+	movl $MSR_IA32_SPEC_CTRL, %ecx;		\
+	movl $0, %edx;				\
+	movl spec_ctrl_priv, %eax;		\
+	wrmsr;					\
+	popq %rdx;				\
+	popq %rcx;				\
+	popq %rax
+
+#define __ASM_ENABLE_SPEC_CTRL_CLOBBER		\
+	movl $MSR_IA32_SPEC_CTRL, %ecx;		\
+	movl $0, %edx;				\
+	movl spec_ctrl_priv, %eax;		\
+	wrmsr;
+
+#define __ASM_DISABLE_SPEC_CTRL			\
+	pushq %rax;				\
+	pushq %rcx;				\
+	pushq %rdx;				\
+	movl $MSR_IA32_SPEC_CTRL, %ecx;		\
+	movl $0, %edx;				\
+	movl spec_ctrl_unpriv, %eax;		\
+	wrmsr;					\
+	popq %rdx;				\
+	popq %rcx;				\
+	popq %rax
+
+#define __ASM_DISABLE_SPEC_CTRL_CLOBBER		\
+	movl $MSR_IA32_SPEC_CTRL, %ecx;		\
+	movl $0, %edx;				\
+	movl spec_ctrl_unpriv, %eax;		\
+	wrmsr;
+
+.macro ENABLE_SPEC_CTRL
+ALTERNATIVE "", __stringify(__ASM_ENABLE_SPEC_CTRL), X86_FEATURE_USE_MDD
+.endm
+
+.macro ENABLE_SPEC_CTRL_CLOBBER
+ALTERNATIVE "", __stringify(__ASM_ENABLE_SPEC_CTRL_CLOBBER), X86_FEATURE_USE_MDD
+.endm
+
+.macro ENABLE_SPEC_CTRL_SAVE_N_CLOBBER save_reg:req
+	ALTERNATIVE "jmp .Ldone_\@", "", X86_FEATURE_USE_MDD
+
+	movl	$MSR_IA32_SPEC_CTRL, %ecx
+	rdmsr
+	movl	%eax, \save_reg
+
+	movl	$0, %edx
+	movl	spec_ctrl_priv, %eax
+	wrmsr
+.Ldone_\@:
+.endm
+
+.macro RESTORE_SPEC_CTRL_CLOBBER save_reg:req
+	ALTERNATIVE "jmp .Ldone_\@", "", X86_FEATURE_USE_MDD
+
+	cmpl	spec_ctrl_priv, \save_reg
+	je	.Ldone_\@
+
+	movl	$MSR_IA32_SPEC_CTRL, %ecx
+	movl	$0, %edx
+	movl	\save_reg, %eax
+	wrmsr
+.Ldone_\@:
+.endm
+
+.macro DISABLE_SPEC_CTRL
+ALTERNATIVE "", __stringify(__ASM_DISABLE_SPEC_CTRL), X86_FEATURE_USE_MDD
+.endm
 #else /* __ASSEMBLY__ */
 
 #define ANNOTATE_NOSPEC_ALTERNATIVE				\
diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c
index 250baf24da56..27f792e8d675 100644
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -417,6 +417,8 @@ static void __init md_select_mitigation(void)
 	else {
 		spec_ctrl_priv &= ~SPEC_CTRL_MDD;
 		spec_ctrl_unpriv |= SPEC_CTRL_MDD;
+		if (mode == MD_KERNEL_ON)
+			setup_force_cpu_cap(X86_FEATURE_USE_MDD);
 	}
 }
 
-- 
2.14.3

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2018-04-18 14:15 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-04-13  2:26 [MODERATED] [patch 6/8] [PATCH v1.3.1 6/7] Linux Patch 6 konrad.wilk

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.