All of lore.kernel.org
 help / color / mirror / Atom feed
From: konrad.wilk@oracle.com
To: speck@linutronix.de
Subject: [MODERATED] [patch 6/8] [PATCH v1.3.1 6/7] Linux Patch 6
Date: Thu, 12 Apr 2018 22:26:55 -0400	[thread overview]
Message-ID: <20180413022657.545604228@localhost.localdomain> (raw)

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

                 reply	other threads:[~2018-04-18 14:15 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180413022657.545604228@localhost.localdomain \
    --to=konrad.wilk@oracle.com \
    --cc=speck@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.