Skip to contentSkip to conversation

Linux Patch 6

PATCH Superseded [email protected] 1 participant 1 message Apr 18, 2018

Subsystems (2)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH 6/7 Linux Patch 6

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.

---
 arch/x86/include/asm/nospec-branch.h | 76 ++++++++++++++++++++++++++++++++++++
 arch/x86/kernel/cpu/bugs.c           |  2 +
 2 files changed, 78 insertions(+)
arch/x86/include/asm/nospec-branch.h +76 -0
index c3856b3f42eb..a53919a3a712 100644
@@ -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				\
arch/x86/kernel/cpu/bugs.c +2 -0
index 250baf24da56..27f792e8d675 100644
@@ -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);
 	}
 }
 
Signature · 1 line
2.14.3