Skip to content

Commit 28a99e9

Browse files
Peter Zijlstrasuryasaimadhu
authored andcommitted
x86/amd: Use IBPB for firmware calls
On AMD IBRS does not prevent Retbleed; as such use IBPB before a firmware call to flush the branch history state. And because in order to do an EFI call, the kernel maps a whole lot of the kernel page table into the EFI page table, do an IBPB just in case in order to prevent the scenario of poisoning the BTB and causing an EFI call using the unprotected RET there. [ bp: Massage. ] Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent ff69927 commit 28a99e9

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

arch/x86/include/asm/cpufeatures.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@
302302
#define X86_FEATURE_RETPOLINE_LFENCE (11*32+13) /* "" Use LFENCE for Spectre variant 2 */
303303
#define X86_FEATURE_RETHUNK (11*32+14) /* "" Use REturn THUNK */
304304
#define X86_FEATURE_UNRET (11*32+15) /* "" AMD BTB untrain return */
305+
#define X86_FEATURE_USE_IBPB_FW (11*32+16) /* "" Use IBPB during runtime firmware calls */
305306

306307
/* Intel-defined CPU features, CPUID level 0x00000007:1 (EAX), word 12 */
307308
#define X86_FEATURE_AVX_VNNI (12*32+ 4) /* AVX VNNI instructions */

arch/x86/include/asm/nospec-branch.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,8 @@ do { \
297297
alternative_msr_write(MSR_IA32_SPEC_CTRL, \
298298
spec_ctrl_current() | SPEC_CTRL_IBRS, \
299299
X86_FEATURE_USE_IBRS_FW); \
300+
alternative_msr_write(MSR_IA32_PRED_CMD, PRED_CMD_IBPB, \
301+
X86_FEATURE_USE_IBPB_FW); \
300302
} while (0)
301303

302304
#define firmware_restrict_branch_speculation_end() \

arch/x86/kernel/cpu/bugs.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1516,7 +1516,16 @@ static void __init spectre_v2_select_mitigation(void)
15161516
* the CPU supports Enhanced IBRS, kernel might un-intentionally not
15171517
* enable IBRS around firmware calls.
15181518
*/
1519-
if (boot_cpu_has(X86_FEATURE_IBRS) && !spectre_v2_in_ibrs_mode(mode)) {
1519+
if (boot_cpu_has_bug(X86_BUG_RETBLEED) &&
1520+
(boot_cpu_data.x86_vendor == X86_VENDOR_AMD ||
1521+
boot_cpu_data.x86_vendor == X86_VENDOR_HYGON)) {
1522+
1523+
if (retbleed_cmd != RETBLEED_CMD_IBPB) {
1524+
setup_force_cpu_cap(X86_FEATURE_USE_IBPB_FW);
1525+
pr_info("Enabling Speculation Barrier for firmware calls\n");
1526+
}
1527+
1528+
} else if (boot_cpu_has(X86_FEATURE_IBRS) && !spectre_v2_in_ibrs_mode(mode)) {
15201529
setup_force_cpu_cap(X86_FEATURE_USE_IBRS_FW);
15211530
pr_info("Enabling Restricted Speculation for firmware calls\n");
15221531
}

0 commit comments

Comments
 (0)