From mboxrd@z Thu Jan 1 00:00:00 1970 Return-path: Received: from aserp2120.oracle.com ([141.146.126.78]) by Galois.linutronix.de with esmtps (TLS1.2:RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1f8ns9-0002br-8u for speck@linutronix.de; Wed, 18 Apr 2018 16:15:46 +0200 Received: from pps.filterd (aserp2120.oracle.com [127.0.0.1]) by aserp2120.oracle.com (8.16.0.22/8.16.0.22) with SMTP id w3IEBTCS184499 for ; Wed, 18 Apr 2018 14:15:38 GMT Received: from userv0021.oracle.com (userv0021.oracle.com [156.151.31.71]) by aserp2120.oracle.com with ESMTP id 2hdrxnaut0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Wed, 18 Apr 2018 14:15:38 +0000 Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by userv0021.oracle.com (8.14.4/8.14.4) with ESMTP id w3IEFbIM012963 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Wed, 18 Apr 2018 14:15:37 GMT Received: from abhmp0007.oracle.com (abhmp0007.oracle.com [141.146.116.13]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id w3IEFbfT003550 for ; Wed, 18 Apr 2018 14:15:37 GMT Message-Id: <20180413022657.545604228@localhost.localdomain> Date: Thu, 12 Apr 2018 22:26:55 -0400 From: konrad.wilk@oracle.com Subject: [MODERATED] [patch 6/8] [PATCH v1.3.1 6/7] Linux Patch 6 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit To: speck@linutronix.de List-ID: 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 --- 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