Skip to content

Commit d4b75c9

Browse files
committed
x86/bugs/AMD: Add support to disable RDS on Fam[15,16,17]h if requested
AMD does not need the Speculative Store Bypass mitigation to be enabled. The parameters for this are already available and can be done via MSR C001_1020. Each family uses a different bit in that MSR for this. [ tglx: Expose the bit mask via a variable and move the actual MSR fiddling into the bugs code as that's the right thing to do and also required to prepare for dynamic enable/disable ] Suggested-by: Borislav Petkov <[email protected]> Signed-off-by: Konrad Rzeszutek Wilk <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Reviewed-by: Ingo Molnar <[email protected]> (cherry picked from commit 764f3c2) Orabug: 28034177 CVE: CVE-2018-3639 Signed-off-by: Konrad Rzeszutek Wilk <[email protected]> Tested-by: Mihai Carabas <[email protected]> Reviewed-by: Mihai Carabas <[email protected]> Reviewed-by: John Haxby <[email protected]> Conflicts: arch/x86/include/asm/cpufeatures.h [As we have the other two CPUIDs for IBRS]
1 parent 07e5877 commit d4b75c9

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed

arch/x86/include/asm/cpufeatures.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@
214214
#define X86_FEATURE_USE_IBPB ( 7*32+21) /* "" Indirect Branch Prediction Barrier enabled */
215215
#define X86_FEATURE_USE_IBRS_FW ( 7*32+22) /* "" Use IBRS during runtime firmware calls */
216216
#define X86_FEATURE_IBRS_ALL ( 7*32+23) /* IBRS all the time */
217+
#define X86_FEATURE_AMD_RDS (7*32+24) /* "" AMD RDS implementation */
217218
#define X86_FEATURE_VMEXIT_RSB_FULL ( 7*32+27) /* "" Whether to stuff the RSB on VMEXIT. */
218219
#define X86_FEATURE_STUFF_RSB ( 7*32+28) /* "" Whether to stuff the RSB (usually dependent on !SMEP) */
219220
#define X86_FEATURE_SPEC_STORE_BYPASS_DISABLE ( 7*32+29) /* "" Disable Speculative Store Bypass. */

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ enum ssb_mitigation {
244244
SPEC_STORE_BYPASS_DISABLE,
245245
};
246246

247+
/* AMD specific Speculative Store Bypass MSR data */
248+
extern u64 x86_amd_ls_cfg_base;
249+
extern u64 x86_amd_ls_cfg_rds_mask;
250+
247251
extern char __indirect_thunk_start[];
248252
extern char __indirect_thunk_end[];
249253

arch/x86/kernel/cpu/amd.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <asm/processor.h>
1111
#include <asm/apic.h>
1212
#include <asm/cpu.h>
13+
#include <asm/nospec-branch.h>
1314
#include <asm/smp.h>
1415
#include <asm/pci-direct.h>
1516
#include <asm/delay.h>
@@ -554,6 +555,26 @@ static void bsp_init_amd(struct cpuinfo_x86 *c)
554555
rdmsrl(MSR_FAM10H_NODE_ID, value);
555556
nodes_per_socket = ((value >> 3) & 7) + 1;
556557
}
558+
559+
if (c->x86 >= 0x15 && c->x86 <= 0x17) {
560+
unsigned int bit;
561+
562+
switch (c->x86) {
563+
case 0x15: bit = 54; break;
564+
case 0x16: bit = 33; break;
565+
case 0x17: bit = 10; break;
566+
default: return;
567+
}
568+
/*
569+
* Try to cache the base value so further operations can
570+
* avoid RMW. If that faults, do not enable RDS.
571+
*/
572+
if (!rdmsrl_safe(MSR_AMD64_LS_CFG, &x86_amd_ls_cfg_base)) {
573+
setup_force_cpu_cap(X86_FEATURE_RDS);
574+
setup_force_cpu_cap(X86_FEATURE_AMD_RDS);
575+
x86_amd_ls_cfg_rds_mask = 1ULL << bit;
576+
}
577+
}
557578
}
558579

559580
static void early_detect_mem_encrypt(struct cpuinfo_x86 *c)
@@ -898,6 +919,11 @@ static void init_amd(struct cpuinfo_x86 *c)
898919
/* AMD CPUs don't reset SS attributes on SYSRET, Xen does. */
899920
if (!cpu_has(c, X86_FEATURE_XENPV))
900921
set_cpu_bug(c, X86_BUG_SYSRET_SS_ATTRS);
922+
923+
if (boot_cpu_has(X86_FEATURE_AMD_RDS)) {
924+
set_cpu_cap(c, X86_FEATURE_RDS);
925+
set_cpu_cap(c, X86_FEATURE_AMD_RDS);
926+
}
901927
}
902928

903929
#ifdef CONFIG_X86_32

arch/x86/kernel/cpu/bugs.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ EXPORT_SYMBOL_GPL(x86_spec_ctrl_priv);
106106
*/
107107
static u64 __ro_after_init x86_spec_ctrl_mask = ~SPEC_CTRL_IBRS;
108108

109+
/*
110+
* AMD specific MSR info for Speculative Store Bypass control.
111+
* x86_amd_ls_cfg_rds_mask is initialized in identify_boot_cpu().
112+
*/
113+
u64 __ro_after_init x86_amd_ls_cfg_base;
114+
u64 __ro_after_init x86_amd_ls_cfg_rds_mask;
115+
109116
void __init check_bugs(void)
110117
{
111118
identify_boot_cpu();
@@ -117,7 +124,8 @@ void __init check_bugs(void)
117124

118125
/*
119126
* Read the SPEC_CTRL MSR to account for reserved bits which may
120-
* have unknown values.
127+
* have unknown values. AMD64_LS_CFG MSR is cached in the early AMD
128+
* init code as it is not enumerated and depends on the family.
121129
*/
122130
if (boot_cpu_has(X86_FEATURE_IBRS)) {
123131
rdmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
@@ -242,6 +250,14 @@ void x86_spec_ctrl_restore_host(u64 guest_spec_ctrl)
242250
}
243251
EXPORT_SYMBOL_GPL(x86_spec_ctrl_restore_host);
244252

253+
static void x86_amd_rds_enable(void)
254+
{
255+
u64 msrval = x86_amd_ls_cfg_base | x86_amd_ls_cfg_rds_mask;
256+
257+
if (boot_cpu_has(X86_FEATURE_AMD_RDS))
258+
wrmsrl(MSR_AMD64_LS_CFG, msrval);
259+
}
260+
245261
#ifdef RETPOLINE
246262
static bool spectre_v2_bad_module;
247263

@@ -676,6 +692,11 @@ static enum ssb_mitigation_cmd __init __ssb_select_mitigation(void)
676692

677693
switch (cmd) {
678694
case SPEC_STORE_BYPASS_CMD_AUTO:
695+
/*
696+
* AMD platforms by default don't need SSB mitigation.
697+
*/
698+
if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
699+
break;
679700
case SPEC_STORE_BYPASS_CMD_ON:
680701
mode = SPEC_STORE_BYPASS_DISABLE;
681702
break;
@@ -702,6 +723,7 @@ static enum ssb_mitigation_cmd __init __ssb_select_mitigation(void)
702723
x86_spec_ctrl_set(SPEC_CTRL_RDS);
703724
break;
704725
case X86_VENDOR_AMD:
726+
x86_amd_rds_enable();
705727
break;
706728
}
707729
}
@@ -723,6 +745,9 @@ void x86_spec_ctrl_setup_ap(void)
723745
{
724746
if (boot_cpu_has(X86_FEATURE_IBRS))
725747
x86_spec_ctrl_set(x86_spec_ctrl_base & ~x86_spec_ctrl_mask);
748+
749+
if (ssb_mode == SPEC_STORE_BYPASS_DISABLE)
750+
x86_amd_rds_enable();
726751
}
727752

728753
#ifdef CONFIG_SYSFS

arch/x86/kernel/cpu/common.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,10 @@ static const __initconst struct x86_cpu_id cpu_no_spec_store_bypass[] = {
968968
{ X86_VENDOR_CENTAUR, 5, },
969969
{ X86_VENDOR_INTEL, 5, },
970970
{ X86_VENDOR_NSC, 5, },
971+
{ X86_VENDOR_AMD, 0x12, },
972+
{ X86_VENDOR_AMD, 0x11, },
973+
{ X86_VENDOR_AMD, 0x10, },
974+
{ X86_VENDOR_AMD, 0xf, },
971975
{ X86_VENDOR_ANY, 4, },
972976
{}
973977
};

0 commit comments

Comments
 (0)