From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Google-Smtp-Source: AH8x22486bM1FXdjcos50lnYrDVCc1JJTL/khdMqKShCQzQyriVUjIOkKVcvMyHB27iXnv91Ls9x ARC-Seal: i=1; a=rsa-sha256; t=1516968716; cv=none; d=google.com; s=arc-20160816; b=JcHNhuVicN3fv7TtgnNvwcuFGLpgw5AYNdSTjpf/52bnRLVr++gu7MpUqT8E3REmRs Guvuv//ox9mp9Dy1mFBmIf/J5pi+v+LoKI4MYpeQ1ibfXYYUl7Wg6q/WwdAW/0bkMs9h w3L2pcah5Y+htV1zRB9FR9/6hkJ7x823BPwVWhkfK0qlkMBTSbpAfknbq3PiSk471fja sSjKKMcDYkfr/SgP1GV9WgDH+AZ1dJxgHSUYrh0fx1S4vAr9zd73axuSnsARX+RZ4UfP 2ElJ+o8KlhwXIZWSxBFm5maCUMb4E8JTxGtXcbH9iL+VrmIHc45pzQKQk2kBtKeijDmD YvOQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=sK7IJ08uqUPYX4wrV09Qa357+G/EIgkvNAhlWgaCAYo=; b=H4ZBdxvkVWHwPuUIE+2GpNnk042gn/O2jRR3FeClFzbCGLhkclzGrAaRn1B3Nzu6VW vXO6y3na+CS3j1C89FOJ5f9YmYvqr9f7R8lrRZ238IPPjvkt5U2nKpEy8NcsuU2Puyzx KuIVJ8movtVufmpTN/tYLrwLC6JsabE5WbnqAK8YRHRsc1OKxEA7M8rNPrm+TrPv3JMD 5ARPnt5xlf6XOarw7UkGRS0/o1WH+yCPITwJBnjhOULzUhIMKXz+UMSDqJ8IjfxQb26I uSac59iTa4LzdtQKfJU2Qc0Q+yQmnB8cyKYN67RiYnOKopDWB8ZkQuCXLDMz1tNGO2SE RDSg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of bp@alien8.de designates 5.9.137.197 as permitted sender) smtp.mailfrom=bp@alien8.de Authentication-Results: mx.google.com; spf=pass (google.com: domain of bp@alien8.de designates 5.9.137.197 as permitted sender) smtp.mailfrom=bp@alien8.de From: Borislav Petkov To: X86 ML Cc: LKML , David Woodhouse , Josh Poimboeuf , tim.c.chen@linux.intel.com, pjt@google.com, jikos@kernel.org, gregkh@linux-foundation.org, dave.hansen@intel.com, riel@redhat.com, luto@amacapital.net, torvalds@linux-foundation.org, ak@linux.intel.com, keescook@google.com, peterz@infradead.org Subject: [PATCH 3/4] x86/retpoline: Simplify vmexit_fill_RSB() Date: Fri, 26 Jan 2018 13:11:38 +0100 Message-Id: <20180126121139.31959-4-bp@alien8.de> X-Mailer: git-send-email 2.13.0 In-Reply-To: <20180126121139.31959-1-bp@alien8.de> References: <20180126121139.31959-1-bp@alien8.de> X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: =?utf-8?q?1590656989548468968?= X-GMAIL-MSGID: =?utf-8?q?1590656989548468968?= X-Mailing-List: linux-kernel@vger.kernel.org List-ID: From: Borislav Petkov Simplify it to call an asm-function instead of pasting 41 insn bytes at every call site. Also, add alignment to the macro as suggested here: https://support.google.com/faqs/answer/7625886 Signed-off-by: Borislav Petkov Cc: David Woodhouse --- arch/x86/include/asm/nospec-branch.h | 39 +++++++++++++++++++++++++++--------- arch/x86/include/asm/processor.h | 5 +++++ arch/x86/lib/Makefile | 1 + arch/x86/lib/retpoline.S | 5 +++++ 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h index 33a35daf6c4d..61d4d7033758 100644 --- a/arch/x86/include/asm/nospec-branch.h +++ b/arch/x86/include/asm/nospec-branch.h @@ -53,6 +53,8 @@ #ifdef __ASSEMBLY__ +#include + /* * This should be used immediately before a retpoline alternative. It tells * objtool where the retpolines are so that it can make sense of the control @@ -121,6 +123,30 @@ #endif .endm +/* Same as above but with alignment additionally */ +.macro ___FILL_RETURN_BUFFER reg:req nr:req sp:req + mov (\nr / 2), \reg + .align 16 +771: + call 772f +773: /* speculation trap */ + pause + lfence + jmp 773b + .align 16 +772: + call 774f +775: /* speculation trap */ + pause + lfence + jmp 775b + .align 16 +774: + dec \reg + jnz 771b + add (BITS_PER_LONG/8) * \nr, \sp +.endm + /* * A simpler FILL_RETURN_BUFFER macro. Don't make people use the CPP * monstrosity above, manually. @@ -206,15 +232,10 @@ extern char __indirect_thunk_end[]; static inline void vmexit_fill_RSB(void) { #ifdef CONFIG_RETPOLINE - unsigned long loops; - - asm volatile (ANNOTATE_NOSPEC_ALTERNATIVE - ALTERNATIVE("jmp 910f", - __stringify(__FILL_RETURN_BUFFER(%0, RSB_CLEAR_LOOPS, %1)), - X86_FEATURE_RETPOLINE) - "910:" - : "=r" (loops), ASM_CALL_CONSTRAINT - : : "memory" ); + alternative_input("", + "call __fill_rsb_clobber_ax", + X86_FEATURE_RETPOLINE, + ASM_NO_INPUT_CLOBBER(_ASM_AX, "memory")); #endif } diff --git a/arch/x86/include/asm/processor.h b/arch/x86/include/asm/processor.h index d3a67fba200a..491f6e0be66e 100644 --- a/arch/x86/include/asm/processor.h +++ b/arch/x86/include/asm/processor.h @@ -971,4 +971,9 @@ bool xen_set_default_idle(void); void stop_this_cpu(void *dummy); void df_debug(struct pt_regs *regs, long error_code); + +#ifdef CONFIG_RETPOLINE +asmlinkage void __fill_rsb_clobber_ax(void); +#endif + #endif /* _ASM_X86_PROCESSOR_H */ diff --git a/arch/x86/lib/Makefile b/arch/x86/lib/Makefile index f23934bbaf4e..69a473919260 100644 --- a/arch/x86/lib/Makefile +++ b/arch/x86/lib/Makefile @@ -27,6 +27,7 @@ lib-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o lib-$(CONFIG_INSTRUCTION_DECODER) += insn.o inat.o insn-eval.o lib-$(CONFIG_RANDOMIZE_BASE) += kaslr.o lib-$(CONFIG_RETPOLINE) += retpoline.o +OBJECT_FILES_NON_STANDARD_retpoline.o :=y obj-y += msr.o msr-reg.o msr-reg-export.o hweight.o diff --git a/arch/x86/lib/retpoline.S b/arch/x86/lib/retpoline.S index c909961e678a..297b0fd2ad10 100644 --- a/arch/x86/lib/retpoline.S +++ b/arch/x86/lib/retpoline.S @@ -46,3 +46,8 @@ GENERATE_THUNK(r13) GENERATE_THUNK(r14) GENERATE_THUNK(r15) #endif + +ENTRY(__fill_rsb_clobber_ax) + ___FILL_RETURN_BUFFER %_ASM_AX, RSB_CLEAR_LOOPS, %_ASM_SP +END(__fill_rsb_clobber_ax) +EXPORT_SYMBOL_GPL(__fill_rsb_clobber_ax) -- 2.13.0