Received: by 10.223.176.46 with SMTP id f43csp1126303wra; Sat, 20 Jan 2018 11:26:55 -0800 (PST) X-Google-Smtp-Source: AH8x225s5A1o6ALtxa4ytnvwqmaFmpIOn3nZL7Uj61PPvIALrPlG1fWE0btvPlqqmYdT8LisfWRX X-Received: by 10.101.88.7 with SMTP id g7mr2796754pgr.249.1516476415332; Sat, 20 Jan 2018 11:26:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516476415; cv=none; d=google.com; s=arc-20160816; b=DJlrngS2NEaoUjU7WnmKiwx205Pd6XFHI/4XpIc7qDf7i0YMLVKJmHXWC73j7beaFx hQhBZhbSM56+TRYz6di/oZjc+NKvV7qwKKacAugTP0DBMyYdvtDhz8EuzxMUvjwTK8M7 sI36x9fsl3PMA1o5W0vqw3IWnldwp7FDj6s6sar1CeDhy8QJZmM/n6K7V/NghjlrEO0q EP2cDHj0hGA9xjK4JIdHHd8eS2wqztWQ0wzU8PN7wSOCjPWjMhDjYWy6tk/FUZmAYkm6 5nUrCqSctdysz8CNpFnGV0KpY2XD8VQGEwlJ1SelANQr300hU7Zdf4rNOf5M67Sa3BmX hrxg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=qtaQEmuwH/l84pB4/hi8R6FbSU8eDlMm5kQMvWxWElI=; b=Gq9+lJ/tcakqD96wtUMt36owh3zVo2IYxfzMsCUdCZ63+8ebO6U0fSFS8kock+6Rax H9pOk7l63l6HLUjQtkMsGYigW3CSoASsPm+oxqO8noF+XRDLu+3GdHUiLHtH8hvUJ//0 C+VzomoLPWUgG5GtiAHdZaQ3YWvknfPULWfzfjTJKA275+PKH/njJPudNTo959O6tU+q y1cjkGxtqByPt/cpaHKzTO68d1dDw4qEwU2fUNQqIoX0PqCo/M7+daVdav1Qel3G1OBT gt47jtjftCHxAw60mW6bKyZLIx64fqRppLxDRdwOJuFvGKnkSwGbHKxn8PSmewfyP9B6 s+IA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.de header.s=amazon201209 header.b=ZQ3sUoYf; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id s82si11902782pfg.317.2018.01.20.11.26.41; Sat, 20 Jan 2018 11:26:55 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.de header.s=amazon201209 header.b=ZQ3sUoYf; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932827AbeATTZK (ORCPT + 99 others); Sat, 20 Jan 2018 14:25:10 -0500 Received: from smtp-fw-33001.amazon.com ([207.171.190.10]:49196 "EHLO smtp-fw-33001.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932582AbeATTXw (ORCPT ); Sat, 20 Jan 2018 14:23:52 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1516476232; x=1548012232; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=qtaQEmuwH/l84pB4/hi8R6FbSU8eDlMm5kQMvWxWElI=; b=ZQ3sUoYfQ8dorA1D9m6LWmvY/kbScpMSOwiFIZh8+c4MOqlTxXK95ugB +gl1UsKlQd8EOynotdQDmefaJNjJ37CRapGJ7cX3jNuaMyWIvKY0CYkx/ FTqE5k9+5l0SZBKCHacxMwDrB6c8W9QuoKOmE7nOkyA4sN0Ar8hdS03xP E=; X-IronPort-AV: E=Sophos;i="5.46,387,1511827200"; d="scan'208";a="716423194" Received: from sea3-co-svc-lb6-vlan2.sea.amazon.com (HELO email-inbound-relay-2c-397e131e.us-west-2.amazon.com) ([10.47.22.34]) by smtp-border-fw-out-33001.sea14.amazon.com with ESMTP/TLS/DHE-RSA-AES256-SHA; 20 Jan 2018 19:23:51 +0000 Received: from u54e1ad5160425a4b64ea.ant.amazon.com (pdx2-ws-svc-lb17-vlan2.amazon.com [10.247.140.66]) by email-inbound-relay-2c-397e131e.us-west-2.amazon.com (8.14.7/8.14.7) with ESMTP id w0KJNjbR023216 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Sat, 20 Jan 2018 19:23:47 GMT Received: from u54e1ad5160425a4b64ea.ant.amazon.com (localhost [127.0.0.1]) by u54e1ad5160425a4b64ea.ant.amazon.com (8.15.2/8.15.2/Debian-3) with ESMTP id w0KJNhsW005300; Sat, 20 Jan 2018 20:23:43 +0100 Received: (from karahmed@localhost) by u54e1ad5160425a4b64ea.ant.amazon.com (8.15.2/8.15.2/Submit) id w0KJNgsw005297; Sat, 20 Jan 2018 20:23:42 +0100 From: KarimAllah Ahmed To: linux-kernel@vger.kernel.org Cc: KarimAllah Ahmed , Andi Kleen , Andrea Arcangeli , Andy Lutomirski , Arjan van de Ven , Ashok Raj , Asit Mallick , Borislav Petkov , Dan Williams , Dave Hansen , David Woodhouse , Greg Kroah-Hartman , "H . Peter Anvin" , Ingo Molnar , Janakarajan Natarajan , Joerg Roedel , Jun Nakajima , Laura Abbott , Linus Torvalds , Masami Hiramatsu , Paolo Bonzini , Peter Zijlstra , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Thomas Gleixner , Tim Chen , Tom Lendacky , kvm@vger.kernel.org, x86@kernel.org, Arjan Van De Ven Subject: [RFC 09/10] x86/enter: Create macros to restrict/unrestrict Indirect Branch Speculation Date: Sat, 20 Jan 2018 20:23:00 +0100 Message-Id: <1516476182-5153-10-git-send-email-karahmed@amazon.de> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1516476182-5153-1-git-send-email-karahmed@amazon.de> References: <1516476182-5153-1-git-send-email-karahmed@amazon.de> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Tim Chen Create macros to control Indirect Branch Speculation. Name them so they reflect what they are actually doing. The macros are used to restrict and unrestrict the indirect branch speculation. They do not *disable* (or *enable*) indirect branch speculation. A trip back to user-space after *restricting* speculation would still affect the BTB. Quoting from a commit by Tim Chen: """ If IBRS is set, near returns and near indirect jumps/calls will not allow their predicted target address to be controlled by code that executed in a less privileged prediction mode *BEFORE* the IBRS mode was last written with a value of 1 or on another logical processor so long as all Return Stack Buffer (RSB) entries from the previous less privileged prediction mode are overwritten. Thus a near indirect jump/call/return may be affected by code in a less privileged prediction mode that executed *AFTER* IBRS mode was last written with a value of 1. """ [ tglx: Changed macro names and rewrote changelog ] [ karahmed: changed macro names *again* and rewrote changelog ] Signed-off-by: Tim Chen Signed-off-by: Thomas Gleixner Signed-off-by: KarimAllah Ahmed Cc: Andrea Arcangeli Cc: Andi Kleen Cc: Peter Zijlstra Cc: Greg KH Cc: Dave Hansen Cc: Andy Lutomirski Cc: Paolo Bonzini Cc: Dan Williams Cc: Arjan Van De Ven Cc: Linus Torvalds Cc: David Woodhouse Cc: Ashok Raj Link: https://lkml.kernel.org/r/3aab341725ee6a9aafd3141387453b45d788d61a.1515542293.git.tim.c.chen@linux.intel.com Signed-off-by: David Woodhouse --- arch/x86/entry/calling.h | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/arch/x86/entry/calling.h b/arch/x86/entry/calling.h index 3f48f69..5aafb51 100644 --- a/arch/x86/entry/calling.h +++ b/arch/x86/entry/calling.h @@ -6,6 +6,8 @@ #include #include #include +#include +#include /* @@ -349,3 +351,74 @@ For 32-bit we have the following conventions - kernel is built with .Lafter_call_\@: #endif .endm + +/* + * IBRS related macros + */ +.macro PUSH_MSR_REGS + pushq %rax + pushq %rcx + pushq %rdx +.endm + +.macro POP_MSR_REGS + popq %rdx + popq %rcx + popq %rax +.endm + +.macro WRMSR_ASM msr_nr:req edx_val:req eax_val:req + movl \msr_nr, %ecx + movl \edx_val, %edx + movl \eax_val, %eax + wrmsr +.endm + +.macro RESTRICT_IB_SPEC + ALTERNATIVE "jmp .Lskip_\@", "", X86_FEATURE_IBRS + PUSH_MSR_REGS + WRMSR_ASM $MSR_IA32_SPEC_CTRL, $0, $SPEC_CTRL_IBRS + POP_MSR_REGS +.Lskip_\@: +.endm + +.macro UNRESTRICT_IB_SPEC + ALTERNATIVE "jmp .Lskip_\@", "", X86_FEATURE_IBRS + PUSH_MSR_REGS + WRMSR_ASM $MSR_IA32_SPEC_CTRL, $0, $0 + POP_MSR_REGS +.Lskip_\@: +.endm + +.macro RESTRICT_IB_SPEC_CLOBBER + ALTERNATIVE "jmp .Lskip_\@", "", X86_FEATURE_IBRS + WRMSR_ASM $MSR_IA32_SPEC_CTRL, $0, $SPEC_CTRL_IBRS +.Lskip_\@: +.endm + +.macro UNRESTRICT_IB_SPEC_CLOBBER + ALTERNATIVE "jmp .Lskip_\@", "", X86_FEATURE_IBRS + WRMSR_ASM $MSR_IA32_SPEC_CTRL, $0, $0 +.Lskip_\@: +.endm + +.macro RESTRICT_IB_SPEC_SAVE_AND_CLOBBER save_reg:req + ALTERNATIVE "jmp .Lskip_\@", "", X86_FEATURE_IBRS + movl $MSR_IA32_SPEC_CTRL, %ecx + rdmsr + movl %eax, \save_reg + movl $0, %edx + movl $SPEC_CTRL_IBRS, %eax + wrmsr +.Lskip_\@: +.endm + +.macro RESTORE_IB_SPEC_CLOBBER save_reg:req + ALTERNATIVE "jmp .Lskip_\@", "", X86_FEATURE_IBRS + /* Set IBRS to the value saved in the save_reg */ + movl $MSR_IA32_SPEC_CTRL, %ecx + movl $0, %edx + movl \save_reg, %eax + wrmsr +.Lskip_\@: +.endm -- 2.7.4