Skip to content

Commit d7caac9

Browse files
Peter Zijlstrasuryasaimadhu
authored andcommitted
x86/cpu/amd: Add Spectral Chicken
Zen2 uarchs have an undocumented, unnamed, MSR that contains a chicken bit for some speculation behaviour. It needs setting. Note: very belatedly AMD released naming; it's now officially called MSR_AMD64_DE_CFG2 and MSR_AMD64_DE_CFG2_SUPPRESS_NOBR_PRED_BIT but shall remain the SPECTRAL CHICKEN. Suggested-by: Andrew Cooper <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Reviewed-by: Josh Poimboeuf <[email protected]> Signed-off-by: Borislav Petkov <[email protected]>
1 parent a09a6e2 commit d7caac9

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

arch/x86/include/asm/msr-index.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,9 @@
568568
/* Fam 17h MSRs */
569569
#define MSR_F17H_IRPERF 0xc00000e9
570570

571+
#define MSR_ZEN2_SPECTRAL_CHICKEN 0xc00110e3
572+
#define MSR_ZEN2_SPECTRAL_CHICKEN_BIT BIT_ULL(1)
573+
571574
/* Fam 16h MSRs */
572575
#define MSR_F16H_L2I_PERF_CTL 0xc0010230
573576
#define MSR_F16H_L2I_PERF_CTR 0xc0010231

arch/x86/kernel/cpu/amd.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,26 @@ static void init_amd_bd(struct cpuinfo_x86 *c)
862862
clear_rdrand_cpuid_bit(c);
863863
}
864864

865+
void init_spectral_chicken(struct cpuinfo_x86 *c)
866+
{
867+
u64 value;
868+
869+
/*
870+
* On Zen2 we offer this chicken (bit) on the altar of Speculation.
871+
*
872+
* This suppresses speculation from the middle of a basic block, i.e. it
873+
* suppresses non-branch predictions.
874+
*
875+
* We use STIBP as a heuristic to filter out Zen2 from the rest of F17H
876+
*/
877+
if (!cpu_has(c, X86_FEATURE_HYPERVISOR) && cpu_has(c, X86_FEATURE_AMD_STIBP)) {
878+
if (!rdmsrl_safe(MSR_ZEN2_SPECTRAL_CHICKEN, &value)) {
879+
value |= MSR_ZEN2_SPECTRAL_CHICKEN_BIT;
880+
wrmsrl_safe(MSR_ZEN2_SPECTRAL_CHICKEN, value);
881+
}
882+
}
883+
}
884+
865885
static void init_amd_zn(struct cpuinfo_x86 *c)
866886
{
867887
set_cpu_cap(c, X86_FEATURE_ZEN);
@@ -907,7 +927,8 @@ static void init_amd(struct cpuinfo_x86 *c)
907927
case 0x12: init_amd_ln(c); break;
908928
case 0x15: init_amd_bd(c); break;
909929
case 0x16: init_amd_jg(c); break;
910-
case 0x17: fallthrough;
930+
case 0x17: init_spectral_chicken(c);
931+
fallthrough;
911932
case 0x19: init_amd_zn(c); break;
912933
}
913934

arch/x86/kernel/cpu/cpu.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ static inline void tsx_init(void) { }
6161
static inline void tsx_ap_init(void) { }
6262
#endif /* CONFIG_CPU_SUP_INTEL */
6363

64+
extern void init_spectral_chicken(struct cpuinfo_x86 *c);
65+
6466
extern void get_cpu_cap(struct cpuinfo_x86 *c);
6567
extern void get_cpu_address_sizes(struct cpuinfo_x86 *c);
6668
extern void cpu_detect_cache_sizes(struct cpuinfo_x86 *c);

arch/x86/kernel/cpu/hygon.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,12 @@ static void init_hygon(struct cpuinfo_x86 *c)
302302
/* get apicid instead of initial apic id from cpuid */
303303
c->apicid = hard_smp_processor_id();
304304

305+
/*
306+
* XXX someone from Hygon needs to confirm this DTRT
307+
*
308+
init_spectral_chicken(c);
309+
*/
310+
305311
set_cpu_cap(c, X86_FEATURE_ZEN);
306312
set_cpu_cap(c, X86_FEATURE_CPB);
307313

0 commit comments

Comments
 (0)