x86/retpoline: Add initial retpoline support
Enable the use of -mindirect-branch=thunk-extern in newer GCC, and provide
the corresponding thunks. Provide assembler macros for invoking the thunks
in the same way that GCC does, from native and inline assembler.
This adds X86_FEATURE_RETPOLINE and sets it by default on all CPUs. In
some circumstances, IBRS microcode features may be used instead, and the
retpoline can be disabled.
On AMD CPUs if lfence is serialising, the retpoline can be dramatically
simplified to a simple "lfence; jmp *\reg". A future patch, after it has
been verified that lfence really is serialising in all circumstances, can
enable this by setting the X86_FEATURE_RETPOLINE_AMD feature bit in addition
to X86_FEATURE_RETPOLINE.
Do not align the retpoline in the altinstr section, because there is no
guarantee that it stays aligned when it's copied over the oldinstr during
alternative patching.
[ Andi Kleen: Rename the macros, add CONFIG_RETPOLINE option, export thunks]
[ tglx: Put actual function CALL/JMP in front of the macros, convert to
symbolic labels ]
[ dwmw2: Convert back to numeric labels, merge objtool fixes ]
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Arjan van de Ven <arjan@linux.intel.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
Cc: gnomes@lxorguk.ukuu.org.uk
Cc: Rik van Riel <riel@redhat.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: thomas.lendacky@amd.com
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Jiri Kosina <jikos@kernel.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Kees Cook <keescook@google.com>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Greg Kroah-Hartman <gregkh@linux-foundation.org>
Cc: Paul Turner <pjt@google.com>
Link: https://lkml.kernel.org/r/1515707194-20531-4-git-send-email-dwmw@amazon.co.uk
2018-01-11 21:46:25 +00:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
|
|
|
|
2018-01-26 13:11:37 +01:00
|
|
|
#ifndef _ASM_X86_NOSPEC_BRANCH_H_
|
|
|
|
#define _ASM_X86_NOSPEC_BRANCH_H_
|
x86/retpoline: Add initial retpoline support
Enable the use of -mindirect-branch=thunk-extern in newer GCC, and provide
the corresponding thunks. Provide assembler macros for invoking the thunks
in the same way that GCC does, from native and inline assembler.
This adds X86_FEATURE_RETPOLINE and sets it by default on all CPUs. In
some circumstances, IBRS microcode features may be used instead, and the
retpoline can be disabled.
On AMD CPUs if lfence is serialising, the retpoline can be dramatically
simplified to a simple "lfence; jmp *\reg". A future patch, after it has
been verified that lfence really is serialising in all circumstances, can
enable this by setting the X86_FEATURE_RETPOLINE_AMD feature bit in addition
to X86_FEATURE_RETPOLINE.
Do not align the retpoline in the altinstr section, because there is no
guarantee that it stays aligned when it's copied over the oldinstr during
alternative patching.
[ Andi Kleen: Rename the macros, add CONFIG_RETPOLINE option, export thunks]
[ tglx: Put actual function CALL/JMP in front of the macros, convert to
symbolic labels ]
[ dwmw2: Convert back to numeric labels, merge objtool fixes ]
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Arjan van de Ven <arjan@linux.intel.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
Cc: gnomes@lxorguk.ukuu.org.uk
Cc: Rik van Riel <riel@redhat.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: thomas.lendacky@amd.com
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Jiri Kosina <jikos@kernel.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Kees Cook <keescook@google.com>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Greg Kroah-Hartman <gregkh@linux-foundation.org>
Cc: Paul Turner <pjt@google.com>
Link: https://lkml.kernel.org/r/1515707194-20531-4-git-send-email-dwmw@amazon.co.uk
2018-01-11 21:46:25 +00:00
|
|
|
|
2018-11-25 19:33:45 +01:00
|
|
|
#include <linux/static_key.h>
|
2020-09-04 16:30:25 +01:00
|
|
|
#include <linux/objtool.h>
|
2021-10-26 14:01:40 +02:00
|
|
|
#include <linux/linkage.h>
|
2018-11-25 19:33:45 +01:00
|
|
|
|
x86/retpoline: Add initial retpoline support
Enable the use of -mindirect-branch=thunk-extern in newer GCC, and provide
the corresponding thunks. Provide assembler macros for invoking the thunks
in the same way that GCC does, from native and inline assembler.
This adds X86_FEATURE_RETPOLINE and sets it by default on all CPUs. In
some circumstances, IBRS microcode features may be used instead, and the
retpoline can be disabled.
On AMD CPUs if lfence is serialising, the retpoline can be dramatically
simplified to a simple "lfence; jmp *\reg". A future patch, after it has
been verified that lfence really is serialising in all circumstances, can
enable this by setting the X86_FEATURE_RETPOLINE_AMD feature bit in addition
to X86_FEATURE_RETPOLINE.
Do not align the retpoline in the altinstr section, because there is no
guarantee that it stays aligned when it's copied over the oldinstr during
alternative patching.
[ Andi Kleen: Rename the macros, add CONFIG_RETPOLINE option, export thunks]
[ tglx: Put actual function CALL/JMP in front of the macros, convert to
symbolic labels ]
[ dwmw2: Convert back to numeric labels, merge objtool fixes ]
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Arjan van de Ven <arjan@linux.intel.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
Cc: gnomes@lxorguk.ukuu.org.uk
Cc: Rik van Riel <riel@redhat.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: thomas.lendacky@amd.com
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Jiri Kosina <jikos@kernel.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Kees Cook <keescook@google.com>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Greg Kroah-Hartman <gregkh@linux-foundation.org>
Cc: Paul Turner <pjt@google.com>
Link: https://lkml.kernel.org/r/1515707194-20531-4-git-send-email-dwmw@amazon.co.uk
2018-01-11 21:46:25 +00:00
|
|
|
#include <asm/alternative.h>
|
|
|
|
#include <asm/cpufeatures.h>
|
2018-02-13 14:28:19 +01:00
|
|
|
#include <asm/msr-index.h>
|
2020-04-14 12:36:16 +02:00
|
|
|
#include <asm/unwind_hints.h>
|
2022-07-13 08:24:37 -07:00
|
|
|
#include <asm/percpu.h>
|
x86/retpoline: Add initial retpoline support
Enable the use of -mindirect-branch=thunk-extern in newer GCC, and provide
the corresponding thunks. Provide assembler macros for invoking the thunks
in the same way that GCC does, from native and inline assembler.
This adds X86_FEATURE_RETPOLINE and sets it by default on all CPUs. In
some circumstances, IBRS microcode features may be used instead, and the
retpoline can be disabled.
On AMD CPUs if lfence is serialising, the retpoline can be dramatically
simplified to a simple "lfence; jmp *\reg". A future patch, after it has
been verified that lfence really is serialising in all circumstances, can
enable this by setting the X86_FEATURE_RETPOLINE_AMD feature bit in addition
to X86_FEATURE_RETPOLINE.
Do not align the retpoline in the altinstr section, because there is no
guarantee that it stays aligned when it's copied over the oldinstr during
alternative patching.
[ Andi Kleen: Rename the macros, add CONFIG_RETPOLINE option, export thunks]
[ tglx: Put actual function CALL/JMP in front of the macros, convert to
symbolic labels ]
[ dwmw2: Convert back to numeric labels, merge objtool fixes ]
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Arjan van de Ven <arjan@linux.intel.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
Cc: gnomes@lxorguk.ukuu.org.uk
Cc: Rik van Riel <riel@redhat.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: thomas.lendacky@amd.com
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Jiri Kosina <jikos@kernel.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Kees Cook <keescook@google.com>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Greg Kroah-Hartman <gregkh@linux-foundation.org>
Cc: Paul Turner <pjt@google.com>
Link: https://lkml.kernel.org/r/1515707194-20531-4-git-send-email-dwmw@amazon.co.uk
2018-01-11 21:46:25 +00:00
|
|
|
|
2022-09-15 13:11:27 +02:00
|
|
|
/*
|
|
|
|
* Call depth tracking for Intel SKL CPUs to address the RSB underflow
|
|
|
|
* issue in software.
|
|
|
|
*
|
|
|
|
* The tracking does not use a counter. It uses uses arithmetic shift
|
|
|
|
* right on call entry and logical shift left on return.
|
|
|
|
*
|
|
|
|
* The depth tracking variable is initialized to 0x8000.... when the call
|
|
|
|
* depth is zero. The arithmetic shift right sign extends the MSB and
|
|
|
|
* saturates after the 12th call. The shift count is 5 for both directions
|
|
|
|
* so the tracking covers 12 nested calls.
|
|
|
|
*
|
|
|
|
* Call
|
|
|
|
* 0: 0x8000000000000000 0x0000000000000000
|
|
|
|
* 1: 0xfc00000000000000 0xf000000000000000
|
|
|
|
* ...
|
|
|
|
* 11: 0xfffffffffffffff8 0xfffffffffffffc00
|
|
|
|
* 12: 0xffffffffffffffff 0xffffffffffffffe0
|
|
|
|
*
|
|
|
|
* After a return buffer fill the depth is credited 12 calls before the
|
|
|
|
* next stuffing has to take place.
|
|
|
|
*
|
|
|
|
* There is a inaccuracy for situations like this:
|
|
|
|
*
|
|
|
|
* 10 calls
|
|
|
|
* 5 returns
|
|
|
|
* 3 calls
|
|
|
|
* 4 returns
|
|
|
|
* 3 calls
|
|
|
|
* ....
|
|
|
|
*
|
|
|
|
* The shift count might cause this to be off by one in either direction,
|
|
|
|
* but there is still a cushion vs. the RSB depth. The algorithm does not
|
|
|
|
* claim to be perfect and it can be speculated around by the CPU, but it
|
|
|
|
* is considered that it obfuscates the problem enough to make exploitation
|
2024-01-02 18:40:11 -06:00
|
|
|
* extremely difficult.
|
2022-09-15 13:11:27 +02:00
|
|
|
*/
|
|
|
|
#define RET_DEPTH_SHIFT 5
|
|
|
|
#define RSB_RET_STUFF_LOOPS 16
|
|
|
|
#define RET_DEPTH_INIT 0x8000000000000000ULL
|
|
|
|
#define RET_DEPTH_INIT_FROM_CALL 0xfc00000000000000ULL
|
|
|
|
#define RET_DEPTH_CREDIT 0xffffffffffffffffULL
|
|
|
|
|
2022-09-15 13:11:30 +02:00
|
|
|
#ifdef CONFIG_CALL_THUNKS_DEBUG
|
|
|
|
# define CALL_THUNKS_DEBUG_INC_CALLS \
|
2023-11-05 22:34:37 +01:00
|
|
|
incq PER_CPU_VAR(__x86_call_count);
|
2022-09-15 13:11:30 +02:00
|
|
|
# define CALL_THUNKS_DEBUG_INC_RETS \
|
2023-11-05 22:34:37 +01:00
|
|
|
incq PER_CPU_VAR(__x86_ret_count);
|
2022-09-15 13:11:30 +02:00
|
|
|
# define CALL_THUNKS_DEBUG_INC_STUFFS \
|
2023-11-05 22:34:37 +01:00
|
|
|
incq PER_CPU_VAR(__x86_stuffs_count);
|
2022-09-15 13:11:30 +02:00
|
|
|
# define CALL_THUNKS_DEBUG_INC_CTXSW \
|
2023-11-05 22:34:37 +01:00
|
|
|
incq PER_CPU_VAR(__x86_ctxsw_count);
|
2022-09-15 13:11:30 +02:00
|
|
|
#else
|
|
|
|
# define CALL_THUNKS_DEBUG_INC_CALLS
|
|
|
|
# define CALL_THUNKS_DEBUG_INC_RETS
|
|
|
|
# define CALL_THUNKS_DEBUG_INC_STUFFS
|
|
|
|
# define CALL_THUNKS_DEBUG_INC_CTXSW
|
|
|
|
#endif
|
|
|
|
|
2023-11-21 08:07:30 -08:00
|
|
|
#if defined(CONFIG_MITIGATION_CALL_DEPTH_TRACKING) && !defined(COMPILE_OFFSETS)
|
2022-09-15 13:11:27 +02:00
|
|
|
|
|
|
|
#include <asm/asm-offsets.h>
|
|
|
|
|
|
|
|
#define CREDIT_CALL_DEPTH \
|
2025-03-03 11:52:40 -05:00
|
|
|
movq $-1, PER_CPU_VAR(__x86_call_depth);
|
2022-09-15 13:11:27 +02:00
|
|
|
|
|
|
|
#define RESET_CALL_DEPTH \
|
2023-02-10 10:10:57 +00:00
|
|
|
xor %eax, %eax; \
|
|
|
|
bts $63, %rax; \
|
2025-03-03 11:52:40 -05:00
|
|
|
movq %rax, PER_CPU_VAR(__x86_call_depth);
|
2022-09-15 13:11:27 +02:00
|
|
|
|
|
|
|
#define RESET_CALL_DEPTH_FROM_CALL \
|
2023-02-10 10:10:57 +00:00
|
|
|
movb $0xfc, %al; \
|
2022-09-15 13:11:27 +02:00
|
|
|
shl $56, %rax; \
|
2025-03-03 11:52:40 -05:00
|
|
|
movq %rax, PER_CPU_VAR(__x86_call_depth); \
|
2022-09-15 13:11:30 +02:00
|
|
|
CALL_THUNKS_DEBUG_INC_CALLS
|
2022-09-15 13:11:27 +02:00
|
|
|
|
|
|
|
#define INCREMENT_CALL_DEPTH \
|
2025-03-03 11:52:40 -05:00
|
|
|
sarq $5, PER_CPU_VAR(__x86_call_depth); \
|
2022-09-15 13:11:30 +02:00
|
|
|
CALL_THUNKS_DEBUG_INC_CALLS
|
2022-09-15 13:11:27 +02:00
|
|
|
|
|
|
|
#else
|
|
|
|
#define CREDIT_CALL_DEPTH
|
|
|
|
#define RESET_CALL_DEPTH
|
|
|
|
#define RESET_CALL_DEPTH_FROM_CALL
|
2023-11-05 22:34:37 +01:00
|
|
|
#define INCREMENT_CALL_DEPTH
|
2022-09-15 13:11:27 +02:00
|
|
|
#endif
|
2021-10-26 14:01:41 +02:00
|
|
|
|
2018-02-19 10:50:56 +00:00
|
|
|
/*
|
|
|
|
* Fill the CPU return stack buffer.
|
|
|
|
*
|
|
|
|
* Each entry in the RSB, if used for a speculative 'ret', contains an
|
|
|
|
* infinite 'pause; lfence; jmp' loop to capture speculative execution.
|
|
|
|
*
|
|
|
|
* This is required in various cases for retpoline and IBRS-based
|
|
|
|
* mitigations for the Spectre variant 2 vulnerability. Sometimes to
|
|
|
|
* eliminate potentially bogus entries from the RSB, and sometimes
|
|
|
|
* purely to ensure that it doesn't get empty, which on some CPUs would
|
|
|
|
* allow predictions from other (unwanted!) sources to be used.
|
|
|
|
*
|
|
|
|
* We define a CPP macro such that it can be used from both .S files and
|
|
|
|
* inline assembly. It's possible to do a .macro and then include that
|
|
|
|
* from C via asm(".include <asm/nospec-branch.h>") but let's not go there.
|
|
|
|
*/
|
|
|
|
|
2022-09-15 13:11:27 +02:00
|
|
|
#define RETPOLINE_THUNK_SIZE 32
|
2018-02-19 10:50:56 +00:00
|
|
|
#define RSB_CLEAR_LOOPS 32 /* To forcibly overwrite all entries */
|
|
|
|
|
|
|
|
/*
|
2022-08-16 14:28:36 +02:00
|
|
|
* Common helper for __FILL_RETURN_BUFFER and __FILL_ONE_RETURN.
|
|
|
|
*/
|
|
|
|
#define __FILL_RETURN_SLOT \
|
|
|
|
ANNOTATE_INTRA_FUNCTION_CALL; \
|
|
|
|
call 772f; \
|
|
|
|
int3; \
|
|
|
|
772:
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Stuff the entire RSB.
|
|
|
|
*
|
2018-02-19 10:50:56 +00:00
|
|
|
* Google experimented with loop-unrolling and this turned out to be
|
2021-03-21 23:32:33 +01:00
|
|
|
* the optimal version - two calls, each with their own speculation
|
2018-02-19 10:50:56 +00:00
|
|
|
* trap should their return address end up getting used, in a loop.
|
|
|
|
*/
|
2022-08-19 13:01:35 +02:00
|
|
|
#ifdef CONFIG_X86_64
|
2022-08-16 14:28:36 +02:00
|
|
|
#define __FILL_RETURN_BUFFER(reg, nr) \
|
|
|
|
mov $(nr/2), reg; \
|
|
|
|
771: \
|
|
|
|
__FILL_RETURN_SLOT \
|
|
|
|
__FILL_RETURN_SLOT \
|
|
|
|
add $(BITS_PER_LONG/8) * 2, %_ASM_SP; \
|
|
|
|
dec reg; \
|
|
|
|
jnz 771b; \
|
|
|
|
/* barrier for jnz misprediction */ \
|
2022-09-15 13:11:27 +02:00
|
|
|
lfence; \
|
2023-11-05 22:34:37 +01:00
|
|
|
CREDIT_CALL_DEPTH \
|
2022-09-15 13:11:30 +02:00
|
|
|
CALL_THUNKS_DEBUG_INC_CTXSW
|
2022-08-19 13:01:35 +02:00
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* i386 doesn't unconditionally have LFENCE, as such it can't
|
|
|
|
* do a loop.
|
|
|
|
*/
|
|
|
|
#define __FILL_RETURN_BUFFER(reg, nr) \
|
|
|
|
.rept nr; \
|
|
|
|
__FILL_RETURN_SLOT; \
|
|
|
|
.endr; \
|
|
|
|
add $(BITS_PER_LONG/8) * nr, %_ASM_SP;
|
|
|
|
#endif
|
2022-08-16 14:28:36 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Stuff a single RSB slot.
|
|
|
|
*
|
|
|
|
* To mitigate Post-Barrier RSB speculation, one CALL instruction must be
|
|
|
|
* forced to retire before letting a RET instruction execute.
|
|
|
|
*
|
|
|
|
* On PBRSB-vulnerable CPUs, it is not safe for a RET to be executed
|
|
|
|
* before this point.
|
|
|
|
*/
|
|
|
|
#define __FILL_ONE_RETURN \
|
|
|
|
__FILL_RETURN_SLOT \
|
|
|
|
add $(BITS_PER_LONG/8), %_ASM_SP; \
|
2022-08-02 15:47:02 -07:00
|
|
|
lfence;
|
2018-02-19 10:50:56 +00:00
|
|
|
|
2025-03-19 11:30:57 +01:00
|
|
|
#ifdef __ASSEMBLER__
|
x86/retpoline: Add initial retpoline support
Enable the use of -mindirect-branch=thunk-extern in newer GCC, and provide
the corresponding thunks. Provide assembler macros for invoking the thunks
in the same way that GCC does, from native and inline assembler.
This adds X86_FEATURE_RETPOLINE and sets it by default on all CPUs. In
some circumstances, IBRS microcode features may be used instead, and the
retpoline can be disabled.
On AMD CPUs if lfence is serialising, the retpoline can be dramatically
simplified to a simple "lfence; jmp *\reg". A future patch, after it has
been verified that lfence really is serialising in all circumstances, can
enable this by setting the X86_FEATURE_RETPOLINE_AMD feature bit in addition
to X86_FEATURE_RETPOLINE.
Do not align the retpoline in the altinstr section, because there is no
guarantee that it stays aligned when it's copied over the oldinstr during
alternative patching.
[ Andi Kleen: Rename the macros, add CONFIG_RETPOLINE option, export thunks]
[ tglx: Put actual function CALL/JMP in front of the macros, convert to
symbolic labels ]
[ dwmw2: Convert back to numeric labels, merge objtool fixes ]
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Arjan van de Ven <arjan@linux.intel.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
Cc: gnomes@lxorguk.ukuu.org.uk
Cc: Rik van Riel <riel@redhat.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: thomas.lendacky@amd.com
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Jiri Kosina <jikos@kernel.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Kees Cook <keescook@google.com>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Greg Kroah-Hartman <gregkh@linux-foundation.org>
Cc: Paul Turner <pjt@google.com>
Link: https://lkml.kernel.org/r/1515707194-20531-4-git-send-email-dwmw@amazon.co.uk
2018-01-11 21:46:25 +00:00
|
|
|
|
2022-06-14 23:15:59 +02:00
|
|
|
/*
|
|
|
|
* (ab)use RETPOLINE_SAFE on RET to annotate away 'bare' RET instructions
|
|
|
|
* vs RETBleed validation.
|
|
|
|
*/
|
|
|
|
#define ANNOTATE_UNRET_SAFE ANNOTATE_RETPOLINE_SAFE
|
|
|
|
|
2022-06-14 23:16:03 +02:00
|
|
|
/*
|
|
|
|
* Abuse ANNOTATE_RETPOLINE_SAFE on a NOP to indicate UNRET_END, should
|
2024-01-02 18:40:11 -06:00
|
|
|
* eventually turn into its own annotation.
|
2022-06-14 23:16:03 +02:00
|
|
|
*/
|
2023-03-01 07:13:11 -08:00
|
|
|
.macro VALIDATE_UNRET_END
|
2023-06-28 11:02:39 +02:00
|
|
|
#if defined(CONFIG_NOINSTR_VALIDATION) && \
|
2023-11-21 08:07:36 -08:00
|
|
|
(defined(CONFIG_MITIGATION_UNRET_ENTRY) || defined(CONFIG_MITIGATION_SRSO))
|
2022-06-14 23:16:03 +02:00
|
|
|
ANNOTATE_RETPOLINE_SAFE
|
|
|
|
nop
|
|
|
|
#endif
|
|
|
|
.endm
|
|
|
|
|
2022-07-20 12:04:21 +02:00
|
|
|
/*
|
2025-02-28 18:35:58 -08:00
|
|
|
* Emits a conditional CS prefix that is compatible with
|
|
|
|
* -mindirect-branch-cs-prefix.
|
2022-07-20 12:04:21 +02:00
|
|
|
*/
|
|
|
|
.macro __CS_PREFIX reg:req
|
|
|
|
.irp rs,r8,r9,r10,r11,r12,r13,r14,r15
|
|
|
|
.ifc \reg,\rs
|
|
|
|
.byte 0x2e
|
|
|
|
.endif
|
|
|
|
.endr
|
|
|
|
.endm
|
|
|
|
|
x86/retpoline: Add initial retpoline support
Enable the use of -mindirect-branch=thunk-extern in newer GCC, and provide
the corresponding thunks. Provide assembler macros for invoking the thunks
in the same way that GCC does, from native and inline assembler.
This adds X86_FEATURE_RETPOLINE and sets it by default on all CPUs. In
some circumstances, IBRS microcode features may be used instead, and the
retpoline can be disabled.
On AMD CPUs if lfence is serialising, the retpoline can be dramatically
simplified to a simple "lfence; jmp *\reg". A future patch, after it has
been verified that lfence really is serialising in all circumstances, can
enable this by setting the X86_FEATURE_RETPOLINE_AMD feature bit in addition
to X86_FEATURE_RETPOLINE.
Do not align the retpoline in the altinstr section, because there is no
guarantee that it stays aligned when it's copied over the oldinstr during
alternative patching.
[ Andi Kleen: Rename the macros, add CONFIG_RETPOLINE option, export thunks]
[ tglx: Put actual function CALL/JMP in front of the macros, convert to
symbolic labels ]
[ dwmw2: Convert back to numeric labels, merge objtool fixes ]
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Arjan van de Ven <arjan@linux.intel.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
Cc: gnomes@lxorguk.ukuu.org.uk
Cc: Rik van Riel <riel@redhat.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: thomas.lendacky@amd.com
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Jiri Kosina <jikos@kernel.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Kees Cook <keescook@google.com>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Greg Kroah-Hartman <gregkh@linux-foundation.org>
Cc: Paul Turner <pjt@google.com>
Link: https://lkml.kernel.org/r/1515707194-20531-4-git-send-email-dwmw@amazon.co.uk
2018-01-11 21:46:25 +00:00
|
|
|
/*
|
|
|
|
* JMP_NOSPEC and CALL_NOSPEC macros can be used instead of a simple
|
|
|
|
* indirect jmp/call which may be susceptible to the Spectre variant 2
|
|
|
|
* attack.
|
2023-06-22 16:27:13 +02:00
|
|
|
*
|
|
|
|
* NOTE: these do not take kCFI into account and are thus not comparable to C
|
|
|
|
* indirect calls, take care when using. The target of these should be an ENDBR
|
|
|
|
* instruction irrespective of kCFI.
|
x86/retpoline: Add initial retpoline support
Enable the use of -mindirect-branch=thunk-extern in newer GCC, and provide
the corresponding thunks. Provide assembler macros for invoking the thunks
in the same way that GCC does, from native and inline assembler.
This adds X86_FEATURE_RETPOLINE and sets it by default on all CPUs. In
some circumstances, IBRS microcode features may be used instead, and the
retpoline can be disabled.
On AMD CPUs if lfence is serialising, the retpoline can be dramatically
simplified to a simple "lfence; jmp *\reg". A future patch, after it has
been verified that lfence really is serialising in all circumstances, can
enable this by setting the X86_FEATURE_RETPOLINE_AMD feature bit in addition
to X86_FEATURE_RETPOLINE.
Do not align the retpoline in the altinstr section, because there is no
guarantee that it stays aligned when it's copied over the oldinstr during
alternative patching.
[ Andi Kleen: Rename the macros, add CONFIG_RETPOLINE option, export thunks]
[ tglx: Put actual function CALL/JMP in front of the macros, convert to
symbolic labels ]
[ dwmw2: Convert back to numeric labels, merge objtool fixes ]
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Arjan van de Ven <arjan@linux.intel.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
Cc: gnomes@lxorguk.ukuu.org.uk
Cc: Rik van Riel <riel@redhat.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: thomas.lendacky@amd.com
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Jiri Kosina <jikos@kernel.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Kees Cook <keescook@google.com>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Greg Kroah-Hartman <gregkh@linux-foundation.org>
Cc: Paul Turner <pjt@google.com>
Link: https://lkml.kernel.org/r/1515707194-20531-4-git-send-email-dwmw@amazon.co.uk
2018-01-11 21:46:25 +00:00
|
|
|
*/
|
|
|
|
.macro JMP_NOSPEC reg:req
|
2023-11-21 08:07:32 -08:00
|
|
|
#ifdef CONFIG_MITIGATION_RETPOLINE
|
2022-07-20 12:04:21 +02:00
|
|
|
__CS_PREFIX \reg
|
|
|
|
jmp __x86_indirect_thunk_\reg
|
x86/retpoline: Add initial retpoline support
Enable the use of -mindirect-branch=thunk-extern in newer GCC, and provide
the corresponding thunks. Provide assembler macros for invoking the thunks
in the same way that GCC does, from native and inline assembler.
This adds X86_FEATURE_RETPOLINE and sets it by default on all CPUs. In
some circumstances, IBRS microcode features may be used instead, and the
retpoline can be disabled.
On AMD CPUs if lfence is serialising, the retpoline can be dramatically
simplified to a simple "lfence; jmp *\reg". A future patch, after it has
been verified that lfence really is serialising in all circumstances, can
enable this by setting the X86_FEATURE_RETPOLINE_AMD feature bit in addition
to X86_FEATURE_RETPOLINE.
Do not align the retpoline in the altinstr section, because there is no
guarantee that it stays aligned when it's copied over the oldinstr during
alternative patching.
[ Andi Kleen: Rename the macros, add CONFIG_RETPOLINE option, export thunks]
[ tglx: Put actual function CALL/JMP in front of the macros, convert to
symbolic labels ]
[ dwmw2: Convert back to numeric labels, merge objtool fixes ]
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Arjan van de Ven <arjan@linux.intel.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
Cc: gnomes@lxorguk.ukuu.org.uk
Cc: Rik van Riel <riel@redhat.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: thomas.lendacky@amd.com
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Jiri Kosina <jikos@kernel.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Kees Cook <keescook@google.com>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Greg Kroah-Hartman <gregkh@linux-foundation.org>
Cc: Paul Turner <pjt@google.com>
Link: https://lkml.kernel.org/r/1515707194-20531-4-git-send-email-dwmw@amazon.co.uk
2018-01-11 21:46:25 +00:00
|
|
|
#else
|
2020-04-22 17:16:40 +02:00
|
|
|
jmp *%\reg
|
2022-07-20 12:04:21 +02:00
|
|
|
int3
|
x86/retpoline: Add initial retpoline support
Enable the use of -mindirect-branch=thunk-extern in newer GCC, and provide
the corresponding thunks. Provide assembler macros for invoking the thunks
in the same way that GCC does, from native and inline assembler.
This adds X86_FEATURE_RETPOLINE and sets it by default on all CPUs. In
some circumstances, IBRS microcode features may be used instead, and the
retpoline can be disabled.
On AMD CPUs if lfence is serialising, the retpoline can be dramatically
simplified to a simple "lfence; jmp *\reg". A future patch, after it has
been verified that lfence really is serialising in all circumstances, can
enable this by setting the X86_FEATURE_RETPOLINE_AMD feature bit in addition
to X86_FEATURE_RETPOLINE.
Do not align the retpoline in the altinstr section, because there is no
guarantee that it stays aligned when it's copied over the oldinstr during
alternative patching.
[ Andi Kleen: Rename the macros, add CONFIG_RETPOLINE option, export thunks]
[ tglx: Put actual function CALL/JMP in front of the macros, convert to
symbolic labels ]
[ dwmw2: Convert back to numeric labels, merge objtool fixes ]
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Arjan van de Ven <arjan@linux.intel.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
Cc: gnomes@lxorguk.ukuu.org.uk
Cc: Rik van Riel <riel@redhat.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: thomas.lendacky@amd.com
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Jiri Kosina <jikos@kernel.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Kees Cook <keescook@google.com>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Greg Kroah-Hartman <gregkh@linux-foundation.org>
Cc: Paul Turner <pjt@google.com>
Link: https://lkml.kernel.org/r/1515707194-20531-4-git-send-email-dwmw@amazon.co.uk
2018-01-11 21:46:25 +00:00
|
|
|
#endif
|
|
|
|
.endm
|
|
|
|
|
|
|
|
.macro CALL_NOSPEC reg:req
|
2023-11-21 08:07:32 -08:00
|
|
|
#ifdef CONFIG_MITIGATION_RETPOLINE
|
2022-07-20 12:04:21 +02:00
|
|
|
__CS_PREFIX \reg
|
|
|
|
call __x86_indirect_thunk_\reg
|
x86/retpoline: Add initial retpoline support
Enable the use of -mindirect-branch=thunk-extern in newer GCC, and provide
the corresponding thunks. Provide assembler macros for invoking the thunks
in the same way that GCC does, from native and inline assembler.
This adds X86_FEATURE_RETPOLINE and sets it by default on all CPUs. In
some circumstances, IBRS microcode features may be used instead, and the
retpoline can be disabled.
On AMD CPUs if lfence is serialising, the retpoline can be dramatically
simplified to a simple "lfence; jmp *\reg". A future patch, after it has
been verified that lfence really is serialising in all circumstances, can
enable this by setting the X86_FEATURE_RETPOLINE_AMD feature bit in addition
to X86_FEATURE_RETPOLINE.
Do not align the retpoline in the altinstr section, because there is no
guarantee that it stays aligned when it's copied over the oldinstr during
alternative patching.
[ Andi Kleen: Rename the macros, add CONFIG_RETPOLINE option, export thunks]
[ tglx: Put actual function CALL/JMP in front of the macros, convert to
symbolic labels ]
[ dwmw2: Convert back to numeric labels, merge objtool fixes ]
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Arjan van de Ven <arjan@linux.intel.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
Cc: gnomes@lxorguk.ukuu.org.uk
Cc: Rik van Riel <riel@redhat.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: thomas.lendacky@amd.com
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Jiri Kosina <jikos@kernel.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Kees Cook <keescook@google.com>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Greg Kroah-Hartman <gregkh@linux-foundation.org>
Cc: Paul Turner <pjt@google.com>
Link: https://lkml.kernel.org/r/1515707194-20531-4-git-send-email-dwmw@amazon.co.uk
2018-01-11 21:46:25 +00:00
|
|
|
#else
|
2020-04-22 17:16:40 +02:00
|
|
|
call *%\reg
|
x86/retpoline: Add initial retpoline support
Enable the use of -mindirect-branch=thunk-extern in newer GCC, and provide
the corresponding thunks. Provide assembler macros for invoking the thunks
in the same way that GCC does, from native and inline assembler.
This adds X86_FEATURE_RETPOLINE and sets it by default on all CPUs. In
some circumstances, IBRS microcode features may be used instead, and the
retpoline can be disabled.
On AMD CPUs if lfence is serialising, the retpoline can be dramatically
simplified to a simple "lfence; jmp *\reg". A future patch, after it has
been verified that lfence really is serialising in all circumstances, can
enable this by setting the X86_FEATURE_RETPOLINE_AMD feature bit in addition
to X86_FEATURE_RETPOLINE.
Do not align the retpoline in the altinstr section, because there is no
guarantee that it stays aligned when it's copied over the oldinstr during
alternative patching.
[ Andi Kleen: Rename the macros, add CONFIG_RETPOLINE option, export thunks]
[ tglx: Put actual function CALL/JMP in front of the macros, convert to
symbolic labels ]
[ dwmw2: Convert back to numeric labels, merge objtool fixes ]
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Arjan van de Ven <arjan@linux.intel.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
Cc: gnomes@lxorguk.ukuu.org.uk
Cc: Rik van Riel <riel@redhat.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: thomas.lendacky@amd.com
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Jiri Kosina <jikos@kernel.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Kees Cook <keescook@google.com>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Greg Kroah-Hartman <gregkh@linux-foundation.org>
Cc: Paul Turner <pjt@google.com>
Link: https://lkml.kernel.org/r/1515707194-20531-4-git-send-email-dwmw@amazon.co.uk
2018-01-11 21:46:25 +00:00
|
|
|
#endif
|
2018-01-12 11:11:27 +00:00
|
|
|
.endm
|
|
|
|
|
2018-02-19 10:50:56 +00:00
|
|
|
/*
|
|
|
|
* A simpler FILL_RETURN_BUFFER macro. Don't make people use the CPP
|
|
|
|
* monstrosity above, manually.
|
|
|
|
*/
|
2022-08-16 14:28:36 +02:00
|
|
|
.macro FILL_RETURN_BUFFER reg:req nr:req ftr:req ftr2=ALT_NOT(X86_FEATURE_ALWAYS)
|
|
|
|
ALTERNATIVE_2 "jmp .Lskip_rsb_\@", \
|
|
|
|
__stringify(__FILL_RETURN_BUFFER(\reg,\nr)), \ftr, \
|
x86: Fix FILL_RETURN_BUFFER
With overlapping alternative validation fixed, objtool promptly
complains:
vmlinux.o: warning: objtool: __switch_to_asm+0x2c: stack layout conflict in alternatives: .altinstr_replacement+0x47
.rela.altinstructions:
000000000000009c 0000000200000002 R_X86_64_PC32 0000000000000000 .text + 16dc
00000000000000a0 0000000600000002 R_X86_64_PC32 0000000000000000 .altinstr_replacement + 3a
00000000000000a8 0000000200000002 R_X86_64_PC32 0000000000000000 .text + 16dc
00000000000000ac 0000000600000002 R_X86_64_PC32 0000000000000000 .altinstr_replacement + 66
.text:
00000000000016b0 <__switch_to_asm>:
16b0: f3 0f 1e fa endbr64
16b4: 55 push %rbp
16b5: 53 push %rbx
16b6: 41 54 push %r12
16b8: 41 55 push %r13
16ba: 41 56 push %r14
16bc: 41 57 push %r15
16be: 48 89 a7 18 0b 00 00 mov %rsp,0xb18(%rdi)
16c5: 48 8b a6 18 0b 00 00 mov 0xb18(%rsi),%rsp
16cc: 48 8b 9e 28 05 00 00 mov 0x528(%rsi),%rbx
16d3: 65 48 89 1c 25 00 00 00 00 mov %rbx,%gs:0x0 16d8: R_X86_64_32S fixed_percpu_data+0x28
16dc: eb 2a jmp 1708 <__switch_to_asm+0x58>
16de: 90 nop
16df: 90 nop
16e0: 90 nop
16e1: 90 nop
16e2: 90 nop
16e3: 90 nop
16e4: 90 nop
16e5: 90 nop
16e6: 90 nop
16e7: 90 nop
16e8: 90 nop
16e9: 90 nop
16ea: 90 nop
16eb: 90 nop
16ec: 90 nop
16ed: 90 nop
16ee: 90 nop
16ef: 90 nop
16f0: 90 nop
16f1: 90 nop
16f2: 90 nop
16f3: 90 nop
16f4: 90 nop
16f5: 90 nop
16f6: 90 nop
16f7: 90 nop
16f8: 90 nop
16f9: 90 nop
16fa: 90 nop
16fb: 90 nop
16fc: 90 nop
16fd: 90 nop
16fe: 90 nop
16ff: 90 nop
1700: 90 nop
1701: 90 nop
1702: 90 nop
1703: 90 nop
1704: 90 nop
1705: 90 nop
1706: 90 nop
1707: 90 nop
1708: 41 5f pop %r15
170a: 41 5e pop %r14
170c: 41 5d pop %r13
170e: 41 5c pop %r12
1710: 5b pop %rbx
1711: 5d pop %rbp
1712: e9 00 00 00 00 jmp 1717 <__switch_to_asm+0x67> 1713: R_X86_64_PLT32 __switch_to-0x4
.altinstr_replacement:
3a: 49 c7 c4 10 00 00 00 mov $0x10,%r12
41: e8 01 00 00 00 call 47 <.altinstr_replacement+0x47>
46: cc int3
47: e8 01 00 00 00 call 4d <.altinstr_replacement+0x4d>
4c: cc int3
4d: 48 83 c4 10 add $0x10,%rsp
51: 49 ff cc dec %r12
54: 75 eb jne 41 <.altinstr_replacement+0x41>
56: 0f ae e8 lfence
59: 65 48 c7 04 25 00 00 00 00 ff ff ff ff movq $0xffffffffffffffff,%gs:0x0 5e: R_X86_64_32S pcpu_hot+0x10
66: e8 01 00 00 00 call 6c <.altinstr_replacement+0x6c>
6b: cc int3
6c: 48 83 c4 08 add $0x8,%rsp
70: 0f ae e8 lfence
As can be seen from the two alternatives, when overlaid, the NOP after
the shorter (starting at 66) coinsides with the call at 47, leading to
conflicting CFI state for that instruction.
By offsetting the shorter alternative by 2 bytes, this alignment is
undone.
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Acked-by: Josh Poimboeuf <jpoimboe@kernel.org>
Tested-by: Nathan Chancellor <nathan@kernel.org> # build only
Tested-by: Thomas Weißschuh <linux@weissschuh.net> # compile and run
Link: https://lore.kernel.org/r/20230208172245.783099843@infradead.org
2023-02-08 18:18:04 +01:00
|
|
|
__stringify(nop;nop;__FILL_ONE_RETURN), \ftr2
|
2022-08-16 14:28:36 +02:00
|
|
|
|
2018-02-19 10:50:56 +00:00
|
|
|
.Lskip_rsb_\@:
|
x86/retpoline: Add initial retpoline support
Enable the use of -mindirect-branch=thunk-extern in newer GCC, and provide
the corresponding thunks. Provide assembler macros for invoking the thunks
in the same way that GCC does, from native and inline assembler.
This adds X86_FEATURE_RETPOLINE and sets it by default on all CPUs. In
some circumstances, IBRS microcode features may be used instead, and the
retpoline can be disabled.
On AMD CPUs if lfence is serialising, the retpoline can be dramatically
simplified to a simple "lfence; jmp *\reg". A future patch, after it has
been verified that lfence really is serialising in all circumstances, can
enable this by setting the X86_FEATURE_RETPOLINE_AMD feature bit in addition
to X86_FEATURE_RETPOLINE.
Do not align the retpoline in the altinstr section, because there is no
guarantee that it stays aligned when it's copied over the oldinstr during
alternative patching.
[ Andi Kleen: Rename the macros, add CONFIG_RETPOLINE option, export thunks]
[ tglx: Put actual function CALL/JMP in front of the macros, convert to
symbolic labels ]
[ dwmw2: Convert back to numeric labels, merge objtool fixes ]
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Arjan van de Ven <arjan@linux.intel.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
Cc: gnomes@lxorguk.ukuu.org.uk
Cc: Rik van Riel <riel@redhat.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: thomas.lendacky@amd.com
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Jiri Kosina <jikos@kernel.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Kees Cook <keescook@google.com>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Greg Kroah-Hartman <gregkh@linux-foundation.org>
Cc: Paul Turner <pjt@google.com>
Link: https://lkml.kernel.org/r/1515707194-20531-4-git-send-email-dwmw@amazon.co.uk
2018-01-11 21:46:25 +00:00
|
|
|
.endm
|
|
|
|
|
2024-03-28 13:59:05 +01:00
|
|
|
/*
|
|
|
|
* The CALL to srso_alias_untrain_ret() must be patched in directly at
|
|
|
|
* the spot where untraining must be done, ie., srso_alias_untrain_ret()
|
|
|
|
* must be the target of a CALL instruction instead of indirectly
|
|
|
|
* jumping to a wrapper which then calls it. Therefore, this macro is
|
|
|
|
* called outside of __UNTRAIN_RET below, for the time being, before the
|
|
|
|
* kernel can support nested alternatives with arbitrary nesting.
|
|
|
|
*/
|
|
|
|
.macro CALL_UNTRAIN_RET
|
2023-11-21 08:07:36 -08:00
|
|
|
#if defined(CONFIG_MITIGATION_UNRET_ENTRY) || defined(CONFIG_MITIGATION_SRSO)
|
2024-03-28 13:59:05 +01:00
|
|
|
ALTERNATIVE_2 "", "call entry_untrain_ret", X86_FEATURE_UNRET, \
|
|
|
|
"call srso_alias_untrain_ret", X86_FEATURE_SRSO_ALIAS
|
2022-06-27 22:21:17 +00:00
|
|
|
#endif
|
2024-03-28 13:59:05 +01:00
|
|
|
.endm
|
2022-06-27 22:21:17 +00:00
|
|
|
|
2022-06-14 23:15:48 +02:00
|
|
|
/*
|
|
|
|
* Mitigate RETBleed for AMD/Hygon Zen uarch. Requires KERNEL CR3 because the
|
|
|
|
* return thunk isn't mapped into the userspace tables (then again, AMD
|
|
|
|
* typically has NO_MELTDOWN).
|
|
|
|
*
|
2023-08-14 13:44:32 +02:00
|
|
|
* While retbleed_untrain_ret() doesn't clobber anything but requires stack,
|
2025-04-08 14:47:30 -07:00
|
|
|
* write_ibpb() will clobber AX, CX, DX.
|
2022-06-14 23:15:48 +02:00
|
|
|
*
|
|
|
|
* As such, this must be placed after every *SWITCH_TO_KERNEL_CR3 at a point
|
|
|
|
* where we have a stack but before any RET instruction.
|
|
|
|
*/
|
2023-09-04 22:05:03 -07:00
|
|
|
.macro __UNTRAIN_RET ibpb_feature, call_depth_insns
|
2023-11-21 08:07:37 -08:00
|
|
|
#if defined(CONFIG_MITIGATION_RETHUNK) || defined(CONFIG_MITIGATION_IBPB_ENTRY)
|
2023-03-01 07:13:11 -08:00
|
|
|
VALIDATE_UNRET_END
|
2024-03-28 13:59:05 +01:00
|
|
|
CALL_UNTRAIN_RET
|
|
|
|
ALTERNATIVE_2 "", \
|
2025-04-08 14:47:30 -07:00
|
|
|
"call write_ibpb", \ibpb_feature, \
|
2023-09-04 22:05:03 -07:00
|
|
|
__stringify(\call_depth_insns), X86_FEATURE_CALL_DEPTH
|
2022-09-15 13:11:27 +02:00
|
|
|
#endif
|
|
|
|
.endm
|
|
|
|
|
2023-09-04 22:05:03 -07:00
|
|
|
#define UNTRAIN_RET \
|
|
|
|
__UNTRAIN_RET X86_FEATURE_ENTRY_IBPB, __stringify(RESET_CALL_DEPTH)
|
2023-08-14 13:44:35 +02:00
|
|
|
|
2023-09-04 22:05:03 -07:00
|
|
|
#define UNTRAIN_RET_VM \
|
|
|
|
__UNTRAIN_RET X86_FEATURE_IBPB_ON_VMEXIT, __stringify(RESET_CALL_DEPTH)
|
|
|
|
|
|
|
|
#define UNTRAIN_RET_FROM_CALL \
|
|
|
|
__UNTRAIN_RET X86_FEATURE_ENTRY_IBPB, __stringify(RESET_CALL_DEPTH_FROM_CALL)
|
2022-09-15 13:11:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
.macro CALL_DEPTH_ACCOUNT
|
2023-11-21 08:07:30 -08:00
|
|
|
#ifdef CONFIG_MITIGATION_CALL_DEPTH_TRACKING
|
2022-09-15 13:11:27 +02:00
|
|
|
ALTERNATIVE "", \
|
2023-11-05 22:34:37 +01:00
|
|
|
__stringify(INCREMENT_CALL_DEPTH), X86_FEATURE_CALL_DEPTH
|
2022-06-14 23:15:48 +02:00
|
|
|
#endif
|
|
|
|
.endm
|
|
|
|
|
2024-02-13 18:21:35 -08:00
|
|
|
/*
|
2024-09-11 05:13:46 +02:00
|
|
|
* Macro to execute VERW insns that mitigate transient data sampling
|
|
|
|
* attacks such as MDS or TSA. On affected systems a microcode update
|
|
|
|
* overloaded VERW insns to also clear the CPU buffers. VERW clobbers
|
|
|
|
* CFLAGS.ZF.
|
2024-02-13 18:21:35 -08:00
|
|
|
* Note: Only the memory operand variant of VERW clears the CPU buffers.
|
|
|
|
*/
|
2024-09-11 10:53:08 +02:00
|
|
|
.macro __CLEAR_CPU_BUFFERS feature
|
2024-09-26 09:10:31 -07:00
|
|
|
#ifdef CONFIG_X86_64
|
2024-09-11 10:53:08 +02:00
|
|
|
ALTERNATIVE "", "verw x86_verw_sel(%rip)", \feature
|
2024-09-26 09:10:31 -07:00
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* In 32bit mode, the memory operand must be a %cs reference. The data
|
|
|
|
* segments may not be usable (vm86 mode), and the stack segment may not
|
|
|
|
* be flat (ESPFIX32).
|
|
|
|
*/
|
2024-09-11 10:53:08 +02:00
|
|
|
ALTERNATIVE "", "verw %cs:x86_verw_sel", \feature
|
2024-09-26 09:10:31 -07:00
|
|
|
#endif
|
2024-02-13 18:21:35 -08:00
|
|
|
.endm
|
|
|
|
|
2024-09-11 10:53:08 +02:00
|
|
|
#define CLEAR_CPU_BUFFERS \
|
|
|
|
__CLEAR_CPU_BUFFERS X86_FEATURE_CLEAR_CPU_BUF
|
|
|
|
|
|
|
|
#define VM_CLEAR_CPU_BUFFERS \
|
|
|
|
__CLEAR_CPU_BUFFERS X86_FEATURE_CLEAR_CPU_BUF_VM
|
|
|
|
|
2024-03-11 08:56:58 -07:00
|
|
|
#ifdef CONFIG_X86_64
|
|
|
|
.macro CLEAR_BRANCH_HISTORY
|
|
|
|
ALTERNATIVE "", "call clear_bhb_loop", X86_FEATURE_CLEAR_BHB_LOOP
|
|
|
|
.endm
|
2024-03-11 08:57:09 -07:00
|
|
|
|
|
|
|
.macro CLEAR_BRANCH_HISTORY_VMEXIT
|
2025-04-15 10:54:09 -07:00
|
|
|
ALTERNATIVE "", "call clear_bhb_loop", X86_FEATURE_CLEAR_BHB_VMEXIT
|
2024-03-11 08:57:09 -07:00
|
|
|
.endm
|
2024-03-11 08:56:58 -07:00
|
|
|
#else
|
|
|
|
#define CLEAR_BRANCH_HISTORY
|
2024-03-11 08:57:09 -07:00
|
|
|
#define CLEAR_BRANCH_HISTORY_VMEXIT
|
2024-03-11 08:56:58 -07:00
|
|
|
#endif
|
|
|
|
|
2025-03-19 11:30:57 +01:00
|
|
|
#else /* __ASSEMBLER__ */
|
x86/retpoline: Add initial retpoline support
Enable the use of -mindirect-branch=thunk-extern in newer GCC, and provide
the corresponding thunks. Provide assembler macros for invoking the thunks
in the same way that GCC does, from native and inline assembler.
This adds X86_FEATURE_RETPOLINE and sets it by default on all CPUs. In
some circumstances, IBRS microcode features may be used instead, and the
retpoline can be disabled.
On AMD CPUs if lfence is serialising, the retpoline can be dramatically
simplified to a simple "lfence; jmp *\reg". A future patch, after it has
been verified that lfence really is serialising in all circumstances, can
enable this by setting the X86_FEATURE_RETPOLINE_AMD feature bit in addition
to X86_FEATURE_RETPOLINE.
Do not align the retpoline in the altinstr section, because there is no
guarantee that it stays aligned when it's copied over the oldinstr during
alternative patching.
[ Andi Kleen: Rename the macros, add CONFIG_RETPOLINE option, export thunks]
[ tglx: Put actual function CALL/JMP in front of the macros, convert to
symbolic labels ]
[ dwmw2: Convert back to numeric labels, merge objtool fixes ]
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Arjan van de Ven <arjan@linux.intel.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
Cc: gnomes@lxorguk.ukuu.org.uk
Cc: Rik van Riel <riel@redhat.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: thomas.lendacky@amd.com
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Jiri Kosina <jikos@kernel.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Kees Cook <keescook@google.com>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Greg Kroah-Hartman <gregkh@linux-foundation.org>
Cc: Paul Turner <pjt@google.com>
Link: https://lkml.kernel.org/r/1515707194-20531-4-git-send-email-dwmw@amazon.co.uk
2018-01-11 21:46:25 +00:00
|
|
|
|
2024-06-21 21:17:21 -07:00
|
|
|
#define ITS_THUNK_SIZE 64
|
|
|
|
|
2021-10-26 14:01:41 +02:00
|
|
|
typedef u8 retpoline_thunk_t[RETPOLINE_THUNK_SIZE];
|
2024-06-21 21:17:21 -07:00
|
|
|
typedef u8 its_thunk_t[ITS_THUNK_SIZE];
|
2022-06-14 23:15:34 +02:00
|
|
|
extern retpoline_thunk_t __x86_indirect_thunk_array[];
|
2022-09-15 13:11:28 +02:00
|
|
|
extern retpoline_thunk_t __x86_indirect_call_thunk_array[];
|
|
|
|
extern retpoline_thunk_t __x86_indirect_jump_thunk_array[];
|
2024-06-21 21:17:21 -07:00
|
|
|
extern its_thunk_t __x86_indirect_its_thunk_array[];
|
2022-06-14 23:15:34 +02:00
|
|
|
|
2023-11-21 08:07:37 -08:00
|
|
|
#ifdef CONFIG_MITIGATION_RETHUNK
|
2022-06-14 23:15:36 +02:00
|
|
|
extern void __x86_return_thunk(void);
|
2023-08-14 13:44:30 +02:00
|
|
|
#else
|
|
|
|
static inline void __x86_return_thunk(void) {}
|
|
|
|
#endif
|
|
|
|
|
2023-11-21 08:07:34 -08:00
|
|
|
#ifdef CONFIG_MITIGATION_UNRET_ENTRY
|
2023-09-04 22:05:00 -07:00
|
|
|
extern void retbleed_return_thunk(void);
|
|
|
|
#else
|
|
|
|
static inline void retbleed_return_thunk(void) {}
|
|
|
|
#endif
|
|
|
|
|
2024-03-28 13:59:05 +01:00
|
|
|
extern void srso_alias_untrain_ret(void);
|
|
|
|
|
2023-11-21 08:07:36 -08:00
|
|
|
#ifdef CONFIG_MITIGATION_SRSO
|
2023-09-04 22:05:00 -07:00
|
|
|
extern void srso_return_thunk(void);
|
|
|
|
extern void srso_alias_return_thunk(void);
|
|
|
|
#else
|
|
|
|
static inline void srso_return_thunk(void) {}
|
|
|
|
static inline void srso_alias_return_thunk(void) {}
|
|
|
|
#endif
|
|
|
|
|
2024-06-21 21:17:21 -07:00
|
|
|
#ifdef CONFIG_MITIGATION_ITS
|
|
|
|
extern void its_return_thunk(void);
|
|
|
|
#else
|
|
|
|
static inline void its_return_thunk(void) {}
|
|
|
|
#endif
|
|
|
|
|
2023-08-14 13:44:32 +02:00
|
|
|
extern void retbleed_return_thunk(void);
|
2023-08-14 13:44:31 +02:00
|
|
|
extern void srso_return_thunk(void);
|
|
|
|
extern void srso_alias_return_thunk(void);
|
|
|
|
|
2023-08-14 13:44:34 +02:00
|
|
|
extern void entry_untrain_ret(void);
|
2025-04-08 14:47:30 -07:00
|
|
|
extern void write_ibpb(void);
|
2022-06-14 23:15:36 +02:00
|
|
|
|
2024-03-11 08:56:58 -07:00
|
|
|
#ifdef CONFIG_X86_64
|
|
|
|
extern void clear_bhb_loop(void);
|
|
|
|
#endif
|
|
|
|
|
2022-09-15 13:11:25 +02:00
|
|
|
extern void (*x86_return_thunk)(void);
|
|
|
|
|
2024-01-03 19:36:26 +01:00
|
|
|
extern void __warn_thunk(void);
|
|
|
|
|
2023-11-21 08:07:30 -08:00
|
|
|
#ifdef CONFIG_MITIGATION_CALL_DEPTH_TRACKING
|
2023-09-04 22:05:04 -07:00
|
|
|
extern void call_depth_return_thunk(void);
|
2022-09-15 13:11:37 +02:00
|
|
|
|
|
|
|
#define CALL_DEPTH_ACCOUNT \
|
|
|
|
ALTERNATIVE("", \
|
|
|
|
__stringify(INCREMENT_CALL_DEPTH), \
|
|
|
|
X86_FEATURE_CALL_DEPTH)
|
|
|
|
|
2025-03-03 11:52:40 -05:00
|
|
|
DECLARE_PER_CPU_CACHE_HOT(u64, __x86_call_depth);
|
|
|
|
|
2022-09-15 13:11:30 +02:00
|
|
|
#ifdef CONFIG_CALL_THUNKS_DEBUG
|
|
|
|
DECLARE_PER_CPU(u64, __x86_call_count);
|
|
|
|
DECLARE_PER_CPU(u64, __x86_ret_count);
|
|
|
|
DECLARE_PER_CPU(u64, __x86_stuffs_count);
|
|
|
|
DECLARE_PER_CPU(u64, __x86_ctxsw_count);
|
|
|
|
#endif
|
2023-11-21 08:07:30 -08:00
|
|
|
#else /* !CONFIG_MITIGATION_CALL_DEPTH_TRACKING */
|
2022-09-15 13:11:37 +02:00
|
|
|
|
2023-09-04 22:05:04 -07:00
|
|
|
static inline void call_depth_return_thunk(void) {}
|
2022-09-15 13:11:37 +02:00
|
|
|
#define CALL_DEPTH_ACCOUNT ""
|
|
|
|
|
2023-11-21 08:07:30 -08:00
|
|
|
#endif /* CONFIG_MITIGATION_CALL_DEPTH_TRACKING */
|
2022-09-15 13:11:27 +02:00
|
|
|
|
2023-11-21 08:07:32 -08:00
|
|
|
#ifdef CONFIG_MITIGATION_RETPOLINE
|
2021-10-26 14:01:41 +02:00
|
|
|
|
2021-10-26 14:01:40 +02:00
|
|
|
#define GEN(reg) \
|
2021-10-26 14:01:41 +02:00
|
|
|
extern retpoline_thunk_t __x86_indirect_thunk_ ## reg;
|
2021-10-26 14:01:40 +02:00
|
|
|
#include <asm/GEN-for-each-reg.h>
|
|
|
|
#undef GEN
|
|
|
|
|
2022-09-15 13:11:28 +02:00
|
|
|
#define GEN(reg) \
|
|
|
|
extern retpoline_thunk_t __x86_indirect_call_thunk_ ## reg;
|
|
|
|
#include <asm/GEN-for-each-reg.h>
|
|
|
|
#undef GEN
|
|
|
|
|
|
|
|
#define GEN(reg) \
|
|
|
|
extern retpoline_thunk_t __x86_indirect_jump_thunk_ ## reg;
|
|
|
|
#include <asm/GEN-for-each-reg.h>
|
|
|
|
#undef GEN
|
|
|
|
|
2018-11-02 01:45:41 -07:00
|
|
|
#ifdef CONFIG_X86_64
|
x86/retpoline: Add initial retpoline support
Enable the use of -mindirect-branch=thunk-extern in newer GCC, and provide
the corresponding thunks. Provide assembler macros for invoking the thunks
in the same way that GCC does, from native and inline assembler.
This adds X86_FEATURE_RETPOLINE and sets it by default on all CPUs. In
some circumstances, IBRS microcode features may be used instead, and the
retpoline can be disabled.
On AMD CPUs if lfence is serialising, the retpoline can be dramatically
simplified to a simple "lfence; jmp *\reg". A future patch, after it has
been verified that lfence really is serialising in all circumstances, can
enable this by setting the X86_FEATURE_RETPOLINE_AMD feature bit in addition
to X86_FEATURE_RETPOLINE.
Do not align the retpoline in the altinstr section, because there is no
guarantee that it stays aligned when it's copied over the oldinstr during
alternative patching.
[ Andi Kleen: Rename the macros, add CONFIG_RETPOLINE option, export thunks]
[ tglx: Put actual function CALL/JMP in front of the macros, convert to
symbolic labels ]
[ dwmw2: Convert back to numeric labels, merge objtool fixes ]
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Arjan van de Ven <arjan@linux.intel.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
Cc: gnomes@lxorguk.ukuu.org.uk
Cc: Rik van Riel <riel@redhat.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: thomas.lendacky@amd.com
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Jiri Kosina <jikos@kernel.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Kees Cook <keescook@google.com>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Greg Kroah-Hartman <gregkh@linux-foundation.org>
Cc: Paul Turner <pjt@google.com>
Link: https://lkml.kernel.org/r/1515707194-20531-4-git-send-email-dwmw@amazon.co.uk
2018-01-11 21:46:25 +00:00
|
|
|
|
2025-02-28 18:35:58 -08:00
|
|
|
/*
|
|
|
|
* Emits a conditional CS prefix that is compatible with
|
|
|
|
* -mindirect-branch-cs-prefix.
|
|
|
|
*/
|
|
|
|
#define __CS_PREFIX(reg) \
|
|
|
|
".irp rs,r8,r9,r10,r11,r12,r13,r14,r15\n" \
|
|
|
|
".ifc \\rs," reg "\n" \
|
|
|
|
".byte 0x2e\n" \
|
|
|
|
".endif\n" \
|
|
|
|
".endr\n"
|
|
|
|
|
x86/retpoline: Add initial retpoline support
Enable the use of -mindirect-branch=thunk-extern in newer GCC, and provide
the corresponding thunks. Provide assembler macros for invoking the thunks
in the same way that GCC does, from native and inline assembler.
This adds X86_FEATURE_RETPOLINE and sets it by default on all CPUs. In
some circumstances, IBRS microcode features may be used instead, and the
retpoline can be disabled.
On AMD CPUs if lfence is serialising, the retpoline can be dramatically
simplified to a simple "lfence; jmp *\reg". A future patch, after it has
been verified that lfence really is serialising in all circumstances, can
enable this by setting the X86_FEATURE_RETPOLINE_AMD feature bit in addition
to X86_FEATURE_RETPOLINE.
Do not align the retpoline in the altinstr section, because there is no
guarantee that it stays aligned when it's copied over the oldinstr during
alternative patching.
[ Andi Kleen: Rename the macros, add CONFIG_RETPOLINE option, export thunks]
[ tglx: Put actual function CALL/JMP in front of the macros, convert to
symbolic labels ]
[ dwmw2: Convert back to numeric labels, merge objtool fixes ]
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Arjan van de Ven <arjan@linux.intel.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
Cc: gnomes@lxorguk.ukuu.org.uk
Cc: Rik van Riel <riel@redhat.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: thomas.lendacky@amd.com
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Jiri Kosina <jikos@kernel.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Kees Cook <keescook@google.com>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Greg Kroah-Hartman <gregkh@linux-foundation.org>
Cc: Paul Turner <pjt@google.com>
Link: https://lkml.kernel.org/r/1515707194-20531-4-git-send-email-dwmw@amazon.co.uk
2018-01-11 21:46:25 +00:00
|
|
|
/*
|
2018-11-02 01:45:41 -07:00
|
|
|
* Inline asm uses the %V modifier which is only in newer GCC
|
2023-11-21 08:07:32 -08:00
|
|
|
* which is ensured when CONFIG_MITIGATION_RETPOLINE is defined.
|
x86/retpoline: Add initial retpoline support
Enable the use of -mindirect-branch=thunk-extern in newer GCC, and provide
the corresponding thunks. Provide assembler macros for invoking the thunks
in the same way that GCC does, from native and inline assembler.
This adds X86_FEATURE_RETPOLINE and sets it by default on all CPUs. In
some circumstances, IBRS microcode features may be used instead, and the
retpoline can be disabled.
On AMD CPUs if lfence is serialising, the retpoline can be dramatically
simplified to a simple "lfence; jmp *\reg". A future patch, after it has
been verified that lfence really is serialising in all circumstances, can
enable this by setting the X86_FEATURE_RETPOLINE_AMD feature bit in addition
to X86_FEATURE_RETPOLINE.
Do not align the retpoline in the altinstr section, because there is no
guarantee that it stays aligned when it's copied over the oldinstr during
alternative patching.
[ Andi Kleen: Rename the macros, add CONFIG_RETPOLINE option, export thunks]
[ tglx: Put actual function CALL/JMP in front of the macros, convert to
symbolic labels ]
[ dwmw2: Convert back to numeric labels, merge objtool fixes ]
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Arjan van de Ven <arjan@linux.intel.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
Cc: gnomes@lxorguk.ukuu.org.uk
Cc: Rik van Riel <riel@redhat.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: thomas.lendacky@amd.com
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Jiri Kosina <jikos@kernel.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Kees Cook <keescook@google.com>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Greg Kroah-Hartman <gregkh@linux-foundation.org>
Cc: Paul Turner <pjt@google.com>
Link: https://lkml.kernel.org/r/1515707194-20531-4-git-send-email-dwmw@amazon.co.uk
2018-01-11 21:46:25 +00:00
|
|
|
*/
|
2025-02-28 18:35:58 -08:00
|
|
|
#define CALL_NOSPEC __CS_PREFIX("%V[thunk_target]") \
|
|
|
|
"call __x86_indirect_thunk_%V[thunk_target]\n"
|
2020-04-16 14:34:26 +02:00
|
|
|
|
x86/retpoline: Add initial retpoline support
Enable the use of -mindirect-branch=thunk-extern in newer GCC, and provide
the corresponding thunks. Provide assembler macros for invoking the thunks
in the same way that GCC does, from native and inline assembler.
This adds X86_FEATURE_RETPOLINE and sets it by default on all CPUs. In
some circumstances, IBRS microcode features may be used instead, and the
retpoline can be disabled.
On AMD CPUs if lfence is serialising, the retpoline can be dramatically
simplified to a simple "lfence; jmp *\reg". A future patch, after it has
been verified that lfence really is serialising in all circumstances, can
enable this by setting the X86_FEATURE_RETPOLINE_AMD feature bit in addition
to X86_FEATURE_RETPOLINE.
Do not align the retpoline in the altinstr section, because there is no
guarantee that it stays aligned when it's copied over the oldinstr during
alternative patching.
[ Andi Kleen: Rename the macros, add CONFIG_RETPOLINE option, export thunks]
[ tglx: Put actual function CALL/JMP in front of the macros, convert to
symbolic labels ]
[ dwmw2: Convert back to numeric labels, merge objtool fixes ]
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Arjan van de Ven <arjan@linux.intel.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
Cc: gnomes@lxorguk.ukuu.org.uk
Cc: Rik van Riel <riel@redhat.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: thomas.lendacky@amd.com
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Jiri Kosina <jikos@kernel.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Kees Cook <keescook@google.com>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Greg Kroah-Hartman <gregkh@linux-foundation.org>
Cc: Paul Turner <pjt@google.com>
Link: https://lkml.kernel.org/r/1515707194-20531-4-git-send-email-dwmw@amazon.co.uk
2018-01-11 21:46:25 +00:00
|
|
|
# define THUNK_TARGET(addr) [thunk_target] "r" (addr)
|
|
|
|
|
2018-11-02 01:45:41 -07:00
|
|
|
#else /* CONFIG_X86_32 */
|
x86/retpoline: Add initial retpoline support
Enable the use of -mindirect-branch=thunk-extern in newer GCC, and provide
the corresponding thunks. Provide assembler macros for invoking the thunks
in the same way that GCC does, from native and inline assembler.
This adds X86_FEATURE_RETPOLINE and sets it by default on all CPUs. In
some circumstances, IBRS microcode features may be used instead, and the
retpoline can be disabled.
On AMD CPUs if lfence is serialising, the retpoline can be dramatically
simplified to a simple "lfence; jmp *\reg". A future patch, after it has
been verified that lfence really is serialising in all circumstances, can
enable this by setting the X86_FEATURE_RETPOLINE_AMD feature bit in addition
to X86_FEATURE_RETPOLINE.
Do not align the retpoline in the altinstr section, because there is no
guarantee that it stays aligned when it's copied over the oldinstr during
alternative patching.
[ Andi Kleen: Rename the macros, add CONFIG_RETPOLINE option, export thunks]
[ tglx: Put actual function CALL/JMP in front of the macros, convert to
symbolic labels ]
[ dwmw2: Convert back to numeric labels, merge objtool fixes ]
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Arjan van de Ven <arjan@linux.intel.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
Cc: gnomes@lxorguk.ukuu.org.uk
Cc: Rik van Riel <riel@redhat.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: thomas.lendacky@amd.com
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Jiri Kosina <jikos@kernel.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Kees Cook <keescook@google.com>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Greg Kroah-Hartman <gregkh@linux-foundation.org>
Cc: Paul Turner <pjt@google.com>
Link: https://lkml.kernel.org/r/1515707194-20531-4-git-send-email-dwmw@amazon.co.uk
2018-01-11 21:46:25 +00:00
|
|
|
/*
|
|
|
|
* For i386 we use the original ret-equivalent retpoline, because
|
|
|
|
* otherwise we'll run out of registers. We don't care about CET
|
|
|
|
* here, anyway.
|
|
|
|
*/
|
2018-03-14 11:24:27 +00:00
|
|
|
# define CALL_NOSPEC \
|
2018-09-18 07:45:00 -07:00
|
|
|
ALTERNATIVE_2( \
|
2018-03-14 11:24:27 +00:00
|
|
|
ANNOTATE_RETPOLINE_SAFE \
|
|
|
|
"call *%[thunk_target]\n", \
|
x86/retpoline: Add initial retpoline support
Enable the use of -mindirect-branch=thunk-extern in newer GCC, and provide
the corresponding thunks. Provide assembler macros for invoking the thunks
in the same way that GCC does, from native and inline assembler.
This adds X86_FEATURE_RETPOLINE and sets it by default on all CPUs. In
some circumstances, IBRS microcode features may be used instead, and the
retpoline can be disabled.
On AMD CPUs if lfence is serialising, the retpoline can be dramatically
simplified to a simple "lfence; jmp *\reg". A future patch, after it has
been verified that lfence really is serialising in all circumstances, can
enable this by setting the X86_FEATURE_RETPOLINE_AMD feature bit in addition
to X86_FEATURE_RETPOLINE.
Do not align the retpoline in the altinstr section, because there is no
guarantee that it stays aligned when it's copied over the oldinstr during
alternative patching.
[ Andi Kleen: Rename the macros, add CONFIG_RETPOLINE option, export thunks]
[ tglx: Put actual function CALL/JMP in front of the macros, convert to
symbolic labels ]
[ dwmw2: Convert back to numeric labels, merge objtool fixes ]
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Arjan van de Ven <arjan@linux.intel.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
Cc: gnomes@lxorguk.ukuu.org.uk
Cc: Rik van Riel <riel@redhat.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: thomas.lendacky@amd.com
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Jiri Kosina <jikos@kernel.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Kees Cook <keescook@google.com>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Greg Kroah-Hartman <gregkh@linux-foundation.org>
Cc: Paul Turner <pjt@google.com>
Link: https://lkml.kernel.org/r/1515707194-20531-4-git-send-email-dwmw@amazon.co.uk
2018-01-11 21:46:25 +00:00
|
|
|
" jmp 904f;\n" \
|
|
|
|
" .align 16\n" \
|
|
|
|
"901: call 903f;\n" \
|
|
|
|
"902: pause;\n" \
|
2018-01-13 17:27:30 -06:00
|
|
|
" lfence;\n" \
|
x86/retpoline: Add initial retpoline support
Enable the use of -mindirect-branch=thunk-extern in newer GCC, and provide
the corresponding thunks. Provide assembler macros for invoking the thunks
in the same way that GCC does, from native and inline assembler.
This adds X86_FEATURE_RETPOLINE and sets it by default on all CPUs. In
some circumstances, IBRS microcode features may be used instead, and the
retpoline can be disabled.
On AMD CPUs if lfence is serialising, the retpoline can be dramatically
simplified to a simple "lfence; jmp *\reg". A future patch, after it has
been verified that lfence really is serialising in all circumstances, can
enable this by setting the X86_FEATURE_RETPOLINE_AMD feature bit in addition
to X86_FEATURE_RETPOLINE.
Do not align the retpoline in the altinstr section, because there is no
guarantee that it stays aligned when it's copied over the oldinstr during
alternative patching.
[ Andi Kleen: Rename the macros, add CONFIG_RETPOLINE option, export thunks]
[ tglx: Put actual function CALL/JMP in front of the macros, convert to
symbolic labels ]
[ dwmw2: Convert back to numeric labels, merge objtool fixes ]
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Arjan van de Ven <arjan@linux.intel.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
Cc: gnomes@lxorguk.ukuu.org.uk
Cc: Rik van Riel <riel@redhat.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: thomas.lendacky@amd.com
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Jiri Kosina <jikos@kernel.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Kees Cook <keescook@google.com>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Greg Kroah-Hartman <gregkh@linux-foundation.org>
Cc: Paul Turner <pjt@google.com>
Link: https://lkml.kernel.org/r/1515707194-20531-4-git-send-email-dwmw@amazon.co.uk
2018-01-11 21:46:25 +00:00
|
|
|
" jmp 902b;\n" \
|
|
|
|
" .align 16\n" \
|
x86/retpoline: Don't clobber RFLAGS during CALL_NOSPEC on i386
Use 'lea' instead of 'add' when adjusting %rsp in CALL_NOSPEC so as to
avoid clobbering flags.
KVM's emulator makes indirect calls into a jump table of sorts, where
the destination of the CALL_NOSPEC is a small blob of code that performs
fast emulation by executing the target instruction with fixed operands.
adcb_al_dl:
0x000339f8 <+0>: adc %dl,%al
0x000339fa <+2>: ret
A major motiviation for doing fast emulation is to leverage the CPU to
handle consumption and manipulation of arithmetic flags, i.e. RFLAGS is
both an input and output to the target of CALL_NOSPEC. Clobbering flags
results in all sorts of incorrect emulation, e.g. Jcc instructions often
take the wrong path. Sans the nops...
asm("push %[flags]; popf; " CALL_NOSPEC " ; pushf; pop %[flags]\n"
0x0003595a <+58>: mov 0xc0(%ebx),%eax
0x00035960 <+64>: mov 0x60(%ebx),%edx
0x00035963 <+67>: mov 0x90(%ebx),%ecx
0x00035969 <+73>: push %edi
0x0003596a <+74>: popf
0x0003596b <+75>: call *%esi
0x000359a0 <+128>: pushf
0x000359a1 <+129>: pop %edi
0x000359a2 <+130>: mov %eax,0xc0(%ebx)
0x000359b1 <+145>: mov %edx,0x60(%ebx)
ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
0x000359a8 <+136>: mov -0x10(%ebp),%eax
0x000359ab <+139>: and $0x8d5,%edi
0x000359b4 <+148>: and $0xfffff72a,%eax
0x000359b9 <+153>: or %eax,%edi
0x000359bd <+157>: mov %edi,0x4(%ebx)
For the most part this has gone unnoticed as emulation of guest code
that can trigger fast emulation is effectively limited to MMIO when
running on modern hardware, and MMIO is rarely, if ever, accessed by
instructions that affect or consume flags.
Breakage is almost instantaneous when running with unrestricted guest
disabled, in which case KVM must emulate all instructions when the guest
has invalid state, e.g. when the guest is in Big Real Mode during early
BIOS.
Fixes: 776b043848fd2 ("x86/retpoline: Add initial retpoline support")
Fixes: 1a29b5b7f347a ("KVM: x86: Make indirect calls in emulator speculation safe")
Signed-off-by: Sean Christopherson <sean.j.christopherson@intel.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: stable@vger.kernel.org
Link: https://lkml.kernel.org/r/20190822211122.27579-1-sean.j.christopherson@intel.com
2019-08-22 14:11:22 -07:00
|
|
|
"903: lea 4(%%esp), %%esp;\n" \
|
x86/retpoline: Add initial retpoline support
Enable the use of -mindirect-branch=thunk-extern in newer GCC, and provide
the corresponding thunks. Provide assembler macros for invoking the thunks
in the same way that GCC does, from native and inline assembler.
This adds X86_FEATURE_RETPOLINE and sets it by default on all CPUs. In
some circumstances, IBRS microcode features may be used instead, and the
retpoline can be disabled.
On AMD CPUs if lfence is serialising, the retpoline can be dramatically
simplified to a simple "lfence; jmp *\reg". A future patch, after it has
been verified that lfence really is serialising in all circumstances, can
enable this by setting the X86_FEATURE_RETPOLINE_AMD feature bit in addition
to X86_FEATURE_RETPOLINE.
Do not align the retpoline in the altinstr section, because there is no
guarantee that it stays aligned when it's copied over the oldinstr during
alternative patching.
[ Andi Kleen: Rename the macros, add CONFIG_RETPOLINE option, export thunks]
[ tglx: Put actual function CALL/JMP in front of the macros, convert to
symbolic labels ]
[ dwmw2: Convert back to numeric labels, merge objtool fixes ]
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Arjan van de Ven <arjan@linux.intel.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
Cc: gnomes@lxorguk.ukuu.org.uk
Cc: Rik van Riel <riel@redhat.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: thomas.lendacky@amd.com
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Jiri Kosina <jikos@kernel.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Kees Cook <keescook@google.com>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Greg Kroah-Hartman <gregkh@linux-foundation.org>
Cc: Paul Turner <pjt@google.com>
Link: https://lkml.kernel.org/r/1515707194-20531-4-git-send-email-dwmw@amazon.co.uk
2018-01-11 21:46:25 +00:00
|
|
|
" pushl %[thunk_target];\n" \
|
|
|
|
" ret;\n" \
|
|
|
|
" .align 16\n" \
|
|
|
|
"904: call 901b;\n", \
|
2018-09-18 07:45:00 -07:00
|
|
|
X86_FEATURE_RETPOLINE, \
|
|
|
|
"lfence;\n" \
|
|
|
|
ANNOTATE_RETPOLINE_SAFE \
|
|
|
|
"call *%[thunk_target]\n", \
|
2022-02-16 20:57:00 +01:00
|
|
|
X86_FEATURE_RETPOLINE_LFENCE)
|
x86/retpoline: Add initial retpoline support
Enable the use of -mindirect-branch=thunk-extern in newer GCC, and provide
the corresponding thunks. Provide assembler macros for invoking the thunks
in the same way that GCC does, from native and inline assembler.
This adds X86_FEATURE_RETPOLINE and sets it by default on all CPUs. In
some circumstances, IBRS microcode features may be used instead, and the
retpoline can be disabled.
On AMD CPUs if lfence is serialising, the retpoline can be dramatically
simplified to a simple "lfence; jmp *\reg". A future patch, after it has
been verified that lfence really is serialising in all circumstances, can
enable this by setting the X86_FEATURE_RETPOLINE_AMD feature bit in addition
to X86_FEATURE_RETPOLINE.
Do not align the retpoline in the altinstr section, because there is no
guarantee that it stays aligned when it's copied over the oldinstr during
alternative patching.
[ Andi Kleen: Rename the macros, add CONFIG_RETPOLINE option, export thunks]
[ tglx: Put actual function CALL/JMP in front of the macros, convert to
symbolic labels ]
[ dwmw2: Convert back to numeric labels, merge objtool fixes ]
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Arjan van de Ven <arjan@linux.intel.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
Cc: gnomes@lxorguk.ukuu.org.uk
Cc: Rik van Riel <riel@redhat.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: thomas.lendacky@amd.com
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Jiri Kosina <jikos@kernel.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Kees Cook <keescook@google.com>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Greg Kroah-Hartman <gregkh@linux-foundation.org>
Cc: Paul Turner <pjt@google.com>
Link: https://lkml.kernel.org/r/1515707194-20531-4-git-send-email-dwmw@amazon.co.uk
2018-01-11 21:46:25 +00:00
|
|
|
|
|
|
|
# define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
|
2018-11-02 01:45:41 -07:00
|
|
|
#endif
|
2018-01-12 11:11:27 +00:00
|
|
|
#else /* No retpoline for C / inline asm */
|
x86/retpoline: Add initial retpoline support
Enable the use of -mindirect-branch=thunk-extern in newer GCC, and provide
the corresponding thunks. Provide assembler macros for invoking the thunks
in the same way that GCC does, from native and inline assembler.
This adds X86_FEATURE_RETPOLINE and sets it by default on all CPUs. In
some circumstances, IBRS microcode features may be used instead, and the
retpoline can be disabled.
On AMD CPUs if lfence is serialising, the retpoline can be dramatically
simplified to a simple "lfence; jmp *\reg". A future patch, after it has
been verified that lfence really is serialising in all circumstances, can
enable this by setting the X86_FEATURE_RETPOLINE_AMD feature bit in addition
to X86_FEATURE_RETPOLINE.
Do not align the retpoline in the altinstr section, because there is no
guarantee that it stays aligned when it's copied over the oldinstr during
alternative patching.
[ Andi Kleen: Rename the macros, add CONFIG_RETPOLINE option, export thunks]
[ tglx: Put actual function CALL/JMP in front of the macros, convert to
symbolic labels ]
[ dwmw2: Convert back to numeric labels, merge objtool fixes ]
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Arjan van de Ven <arjan@linux.intel.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
Cc: gnomes@lxorguk.ukuu.org.uk
Cc: Rik van Riel <riel@redhat.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: thomas.lendacky@amd.com
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Jiri Kosina <jikos@kernel.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Kees Cook <keescook@google.com>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Greg Kroah-Hartman <gregkh@linux-foundation.org>
Cc: Paul Turner <pjt@google.com>
Link: https://lkml.kernel.org/r/1515707194-20531-4-git-send-email-dwmw@amazon.co.uk
2018-01-11 21:46:25 +00:00
|
|
|
# define CALL_NOSPEC "call *%[thunk_target]\n"
|
|
|
|
# define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
|
|
|
|
#endif
|
|
|
|
|
2018-01-11 21:46:26 +00:00
|
|
|
/* The Spectre V2 mitigation variants */
|
|
|
|
enum spectre_v2_mitigation {
|
|
|
|
SPECTRE_V2_NONE,
|
2022-02-16 20:57:00 +01:00
|
|
|
SPECTRE_V2_RETPOLINE,
|
|
|
|
SPECTRE_V2_LFENCE,
|
2022-02-16 20:57:01 +01:00
|
|
|
SPECTRE_V2_EIBRS,
|
|
|
|
SPECTRE_V2_EIBRS_RETPOLINE,
|
|
|
|
SPECTRE_V2_EIBRS_LFENCE,
|
2022-06-14 23:15:55 +02:00
|
|
|
SPECTRE_V2_IBRS,
|
2018-01-11 21:46:26 +00:00
|
|
|
};
|
|
|
|
|
2018-11-25 19:33:45 +01:00
|
|
|
/* The indirect branch speculation control variants */
|
|
|
|
enum spectre_v2_user_mitigation {
|
|
|
|
SPECTRE_V2_USER_NONE,
|
|
|
|
SPECTRE_V2_USER_STRICT,
|
2018-12-13 23:03:54 +00:00
|
|
|
SPECTRE_V2_USER_STRICT_PREFERRED,
|
x86/speculation: Add prctl() control for indirect branch speculation
Add the PR_SPEC_INDIRECT_BRANCH option for the PR_GET_SPECULATION_CTRL and
PR_SET_SPECULATION_CTRL prctls to allow fine grained per task control of
indirect branch speculation via STIBP and IBPB.
Invocations:
Check indirect branch speculation status with
- prctl(PR_GET_SPECULATION_CTRL, PR_SPEC_INDIRECT_BRANCH, 0, 0, 0);
Enable indirect branch speculation with
- prctl(PR_SET_SPECULATION_CTRL, PR_SPEC_INDIRECT_BRANCH, PR_SPEC_ENABLE, 0, 0);
Disable indirect branch speculation with
- prctl(PR_SET_SPECULATION_CTRL, PR_SPEC_INDIRECT_BRANCH, PR_SPEC_DISABLE, 0, 0);
Force disable indirect branch speculation with
- prctl(PR_SET_SPECULATION_CTRL, PR_SPEC_INDIRECT_BRANCH, PR_SPEC_FORCE_DISABLE, 0, 0);
See Documentation/userspace-api/spec_ctrl.rst.
Signed-off-by: Tim Chen <tim.c.chen@linux.intel.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Reviewed-by: Ingo Molnar <mingo@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Jiri Kosina <jkosina@suse.cz>
Cc: Tom Lendacky <thomas.lendacky@amd.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Andrea Arcangeli <aarcange@redhat.com>
Cc: David Woodhouse <dwmw@amazon.co.uk>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Casey Schaufler <casey.schaufler@intel.com>
Cc: Asit Mallick <asit.k.mallick@intel.com>
Cc: Arjan van de Ven <arjan@linux.intel.com>
Cc: Jon Masters <jcm@redhat.com>
Cc: Waiman Long <longman9394@gmail.com>
Cc: Greg KH <gregkh@linuxfoundation.org>
Cc: Dave Stewart <david.c.stewart@intel.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: stable@vger.kernel.org
Link: https://lkml.kernel.org/r/20181125185005.866780996@linutronix.de
2018-11-25 19:33:53 +01:00
|
|
|
SPECTRE_V2_USER_PRCTL,
|
2018-11-25 19:33:55 +01:00
|
|
|
SPECTRE_V2_USER_SECCOMP,
|
2018-11-25 19:33:45 +01:00
|
|
|
};
|
|
|
|
|
2018-04-25 22:04:21 -04:00
|
|
|
/* The Speculative Store Bypass disable variants */
|
|
|
|
enum ssb_mitigation {
|
|
|
|
SPEC_STORE_BYPASS_NONE,
|
|
|
|
SPEC_STORE_BYPASS_DISABLE,
|
2018-04-29 15:26:40 +02:00
|
|
|
SPEC_STORE_BYPASS_PRCTL,
|
2018-05-03 14:37:54 -07:00
|
|
|
SPEC_STORE_BYPASS_SECCOMP,
|
2018-04-25 22:04:21 -04:00
|
|
|
};
|
|
|
|
|
2018-05-01 15:55:51 +02:00
|
|
|
static __always_inline
|
|
|
|
void alternative_msr_write(unsigned int msr, u64 val, unsigned int feature)
|
|
|
|
{
|
|
|
|
asm volatile(ALTERNATIVE("", "wrmsr", %c[feature])
|
|
|
|
: : "c" (msr),
|
2018-05-13 17:33:57 -04:00
|
|
|
"a" ((u32)val),
|
|
|
|
"d" ((u32)(val >> 32)),
|
2018-05-01 15:55:51 +02:00
|
|
|
[feature] "i" (feature)
|
|
|
|
: "memory");
|
|
|
|
}
|
2018-02-19 10:50:54 +00:00
|
|
|
|
2018-01-25 16:14:15 +00:00
|
|
|
static inline void indirect_branch_prediction_barrier(void)
|
|
|
|
{
|
2025-04-08 14:47:32 -07:00
|
|
|
asm_inline volatile(ALTERNATIVE("", "call write_ibpb", X86_FEATURE_IBPB)
|
|
|
|
: ASM_CALL_CONSTRAINT
|
|
|
|
:: "rax", "rcx", "rdx", "memory");
|
2018-01-25 16:14:15 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 20:49:16 +02:00
|
|
|
/* The Intel SPEC CTRL MSR base value cache */
|
|
|
|
extern u64 x86_spec_ctrl_base;
|
2022-07-13 08:24:37 -07:00
|
|
|
DECLARE_PER_CPU(u64, x86_spec_ctrl_current);
|
2022-11-30 07:25:51 -08:00
|
|
|
extern void update_spec_ctrl_cond(u64 val);
|
2022-06-14 23:15:58 +02:00
|
|
|
extern u64 spec_ctrl_current(void);
|
2018-05-12 20:49:16 +02:00
|
|
|
|
2018-02-19 10:50:54 +00:00
|
|
|
/*
|
|
|
|
* With retpoline, we must use IBRS to restrict branch prediction
|
|
|
|
* before calling into firmware.
|
2018-02-21 09:20:37 +01:00
|
|
|
*
|
|
|
|
* (Implemented as CPP macros due to header hell.)
|
2018-02-19 10:50:54 +00:00
|
|
|
*/
|
2018-02-21 09:20:37 +01:00
|
|
|
#define firmware_restrict_branch_speculation_start() \
|
|
|
|
do { \
|
|
|
|
preempt_disable(); \
|
2022-06-14 23:16:06 +02:00
|
|
|
alternative_msr_write(MSR_IA32_SPEC_CTRL, \
|
|
|
|
spec_ctrl_current() | SPEC_CTRL_IBRS, \
|
2018-02-21 09:20:37 +01:00
|
|
|
X86_FEATURE_USE_IBRS_FW); \
|
2022-07-18 13:41:37 +02:00
|
|
|
alternative_msr_write(MSR_IA32_PRED_CMD, PRED_CMD_IBPB, \
|
|
|
|
X86_FEATURE_USE_IBPB_FW); \
|
2018-02-21 09:20:37 +01:00
|
|
|
} while (0)
|
2018-02-19 10:50:54 +00:00
|
|
|
|
2018-02-21 09:20:37 +01:00
|
|
|
#define firmware_restrict_branch_speculation_end() \
|
|
|
|
do { \
|
2022-06-14 23:16:06 +02:00
|
|
|
alternative_msr_write(MSR_IA32_SPEC_CTRL, \
|
|
|
|
spec_ctrl_current(), \
|
2018-02-21 09:20:37 +01:00
|
|
|
X86_FEATURE_USE_IBRS_FW); \
|
|
|
|
preempt_enable(); \
|
|
|
|
} while (0)
|
2018-01-25 16:14:15 +00:00
|
|
|
|
2018-11-25 19:33:45 +01:00
|
|
|
DECLARE_STATIC_KEY_FALSE(switch_to_cond_stibp);
|
2018-11-25 19:33:49 +01:00
|
|
|
DECLARE_STATIC_KEY_FALSE(switch_mm_cond_ibpb);
|
|
|
|
DECLARE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
|
2018-11-25 19:33:45 +01:00
|
|
|
|
2025-02-27 01:27:10 +00:00
|
|
|
DECLARE_STATIC_KEY_FALSE(switch_vcpu_ibpb);
|
|
|
|
|
2024-09-11 05:13:46 +02:00
|
|
|
DECLARE_STATIC_KEY_FALSE(cpu_buf_idle_clear);
|
2019-02-18 23:42:51 +01:00
|
|
|
|
2021-04-26 21:42:30 +02:00
|
|
|
DECLARE_STATIC_KEY_FALSE(switch_mm_cond_l1d_flush);
|
|
|
|
|
2025-04-16 06:47:51 -07:00
|
|
|
DECLARE_STATIC_KEY_FALSE(cpu_buf_vm_clear);
|
2022-05-19 20:29:11 -07:00
|
|
|
|
2024-09-11 05:13:46 +02:00
|
|
|
extern u16 x86_verw_sel;
|
2024-02-13 18:21:35 -08:00
|
|
|
|
2019-02-18 23:13:06 +01:00
|
|
|
#include <asm/segment.h>
|
|
|
|
|
|
|
|
/**
|
2024-09-11 05:13:46 +02:00
|
|
|
* x86_clear_cpu_buffers - Buffer clearing support for different x86 CPU vulns
|
2019-02-18 23:13:06 +01:00
|
|
|
*
|
|
|
|
* This uses the otherwise unused and obsolete VERW instruction in
|
|
|
|
* combination with microcode which triggers a CPU buffer flush when the
|
|
|
|
* instruction is executed.
|
|
|
|
*/
|
2024-09-11 05:13:46 +02:00
|
|
|
static __always_inline void x86_clear_cpu_buffers(void)
|
2019-02-18 23:13:06 +01:00
|
|
|
{
|
|
|
|
static const u16 ds = __KERNEL_DS;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Has to be the memory-operand variant because only that
|
|
|
|
* guarantees the CPU buffer flush functionality according to
|
|
|
|
* documentation. The register-operand variant does not.
|
|
|
|
* Works with any segment selector, but a valid writable
|
|
|
|
* data segment is the fastest variant.
|
|
|
|
*
|
|
|
|
* "cc" clobber is required because VERW modifies ZF.
|
|
|
|
*/
|
|
|
|
asm volatile("verw %[ds]" : : [ds] "m" (ds) : "cc");
|
|
|
|
}
|
|
|
|
|
2019-02-18 23:04:01 +01:00
|
|
|
/**
|
2024-09-11 05:13:46 +02:00
|
|
|
* x86_idle_clear_cpu_buffers - Buffer clearing support in idle for the MDS
|
2024-09-11 10:53:08 +02:00
|
|
|
* and TSA vulnerabilities.
|
2019-02-18 23:04:01 +01:00
|
|
|
*
|
|
|
|
* Clear CPU buffers if the corresponding static key is enabled
|
|
|
|
*/
|
2024-09-11 05:13:46 +02:00
|
|
|
static __always_inline void x86_idle_clear_cpu_buffers(void)
|
2019-02-18 23:04:01 +01:00
|
|
|
{
|
2024-09-11 05:13:46 +02:00
|
|
|
if (static_branch_likely(&cpu_buf_idle_clear))
|
|
|
|
x86_clear_cpu_buffers();
|
2019-02-18 23:04:01 +01:00
|
|
|
}
|
|
|
|
|
2025-03-19 11:30:57 +01:00
|
|
|
#endif /* __ASSEMBLER__ */
|
bpf, x64: implement retpoline for tail call
Implement a retpoline [0] for the BPF tail call JIT'ing that converts
the indirect jump via jmp %rax that is used to make the long jump into
another JITed BPF image. Since this is subject to speculative execution,
we need to control the transient instruction sequence here as well
when CONFIG_RETPOLINE is set, and direct it into a pause + lfence loop.
The latter aligns also with what gcc / clang emits (e.g. [1]).
JIT dump after patch:
# bpftool p d x i 1
0: (18) r2 = map[id:1]
2: (b7) r3 = 0
3: (85) call bpf_tail_call#12
4: (b7) r0 = 2
5: (95) exit
With CONFIG_RETPOLINE:
# bpftool p d j i 1
[...]
33: cmp %edx,0x24(%rsi)
36: jbe 0x0000000000000072 |*
38: mov 0x24(%rbp),%eax
3e: cmp $0x20,%eax
41: ja 0x0000000000000072 |
43: add $0x1,%eax
46: mov %eax,0x24(%rbp)
4c: mov 0x90(%rsi,%rdx,8),%rax
54: test %rax,%rax
57: je 0x0000000000000072 |
59: mov 0x28(%rax),%rax
5d: add $0x25,%rax
61: callq 0x000000000000006d |+
66: pause |
68: lfence |
6b: jmp 0x0000000000000066 |
6d: mov %rax,(%rsp) |
71: retq |
72: mov $0x2,%eax
[...]
* relative fall-through jumps in error case
+ retpoline for indirect jump
Without CONFIG_RETPOLINE:
# bpftool p d j i 1
[...]
33: cmp %edx,0x24(%rsi)
36: jbe 0x0000000000000063 |*
38: mov 0x24(%rbp),%eax
3e: cmp $0x20,%eax
41: ja 0x0000000000000063 |
43: add $0x1,%eax
46: mov %eax,0x24(%rbp)
4c: mov 0x90(%rsi,%rdx,8),%rax
54: test %rax,%rax
57: je 0x0000000000000063 |
59: mov 0x28(%rax),%rax
5d: add $0x25,%rax
61: jmpq *%rax |-
63: mov $0x2,%eax
[...]
* relative fall-through jumps in error case
- plain indirect jump as before
[0] https://support.google.com/faqs/answer/7625886
[1] https://github.com/gcc-mirror/gcc/commit/a31e654fa107be968b802786d747e962c2fcdb2b
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2018-02-22 15:12:53 +01:00
|
|
|
|
2018-01-26 13:11:37 +01:00
|
|
|
#endif /* _ASM_X86_NOSPEC_BRANCH_H_ */
|