Skip to content

Commit 6ac2f49

Browse files
konradwilkKAGA-KOKO
authored andcommitted
x86/bugs: Add AMD's SPEC_CTRL MSR usage
The AMD document outlining the SSBD handling 124441_AMD64_SpeculativeStoreBypassDisable_Whitepaper_final.pdf mentions that if CPUID 8000_0008.EBX[24] is set we should be using the SPEC_CTRL MSR (0x48) over the VIRT SPEC_CTRL MSR (0xC001_011f) for speculative store bypass disable. This in effect means we should clear the X86_FEATURE_VIRT_SSBD flag so that we would prefer the SPEC_CTRL MSR. See the document titled: 124441_AMD64_SpeculativeStoreBypassDisable_Whitepaper_final.pdf A copy of this document is available at https://bugzilla.kernel.org/show_bug.cgi?id=199889 Signed-off-by: Konrad Rzeszutek Wilk <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Cc: Tom Lendacky <[email protected]> Cc: Janakarajan Natarajan <[email protected]> Cc: [email protected] Cc: KarimAllah Ahmed <[email protected]> Cc: [email protected] Cc: Joerg Roedel <[email protected]> Cc: Radim Krčmář <[email protected]> Cc: Andy Lutomirski <[email protected]> Cc: "H. Peter Anvin" <[email protected]> Cc: Paolo Bonzini <[email protected]> Cc: Borislav Petkov <[email protected]> Cc: David Woodhouse <[email protected]> Cc: Kees Cook <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 2480986 commit 6ac2f49

File tree

5 files changed

+27
-10
lines changed

5 files changed

+27
-10
lines changed

arch/x86/include/asm/cpufeatures.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@
282282
#define X86_FEATURE_AMD_IBPB (13*32+12) /* "" Indirect Branch Prediction Barrier */
283283
#define X86_FEATURE_AMD_IBRS (13*32+14) /* "" Indirect Branch Restricted Speculation */
284284
#define X86_FEATURE_AMD_STIBP (13*32+15) /* "" Single Thread Indirect Branch Predictors */
285+
#define X86_FEATURE_AMD_SSBD (13*32+24) /* "" Speculative Store Bypass Disable */
285286
#define X86_FEATURE_VIRT_SSBD (13*32+25) /* Virtualized Speculative Store Bypass Disable */
286287
#define X86_FEATURE_AMD_SSB_NO (13*32+26) /* "" Speculative Store Bypass is fixed in hardware. */
287288

arch/x86/kernel/cpu/bugs.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -529,18 +529,20 @@ static enum ssb_mitigation __init __ssb_select_mitigation(void)
529529
if (mode == SPEC_STORE_BYPASS_DISABLE) {
530530
setup_force_cpu_cap(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE);
531531
/*
532-
* Intel uses the SPEC CTRL MSR Bit(2) for this, while AMD uses
533-
* a completely different MSR and bit dependent on family.
532+
* Intel uses the SPEC CTRL MSR Bit(2) for this, while AMD may
533+
* use a completely different MSR and bit dependent on family.
534534
*/
535535
switch (boot_cpu_data.x86_vendor) {
536536
case X86_VENDOR_INTEL:
537+
case X86_VENDOR_AMD:
538+
if (!static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL)) {
539+
x86_amd_ssb_disable();
540+
break;
541+
}
537542
x86_spec_ctrl_base |= SPEC_CTRL_SSBD;
538543
x86_spec_ctrl_mask |= SPEC_CTRL_SSBD;
539544
wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
540545
break;
541-
case X86_VENDOR_AMD:
542-
x86_amd_ssb_disable();
543-
break;
544546
}
545547
}
546548

arch/x86/kernel/cpu/common.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,12 @@ static void init_speculation_control(struct cpuinfo_x86 *c)
803803
set_cpu_cap(c, X86_FEATURE_STIBP);
804804
set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
805805
}
806+
807+
if (cpu_has(c, X86_FEATURE_AMD_SSBD)) {
808+
set_cpu_cap(c, X86_FEATURE_SSBD);
809+
set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
810+
clear_cpu_cap(c, X86_FEATURE_VIRT_SSBD);
811+
}
806812
}
807813

808814
void get_cpu_cap(struct cpuinfo_x86 *c)

arch/x86/kvm/cpuid.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,8 @@ static inline int __do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
379379

380380
/* cpuid 0x80000008.ebx */
381381
const u32 kvm_cpuid_8000_0008_ebx_x86_features =
382-
F(AMD_IBPB) | F(AMD_IBRS) | F(VIRT_SSBD) | F(AMD_SSB_NO);
382+
F(AMD_IBPB) | F(AMD_IBRS) | F(AMD_SSBD) | F(VIRT_SSBD) |
383+
F(AMD_SSB_NO);
383384

384385
/* cpuid 0xC0000001.edx */
385386
const u32 kvm_cpuid_C000_0001_edx_x86_features =
@@ -664,7 +665,12 @@ static inline int __do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
664665
entry->ebx |= F(VIRT_SSBD);
665666
entry->ebx &= kvm_cpuid_8000_0008_ebx_x86_features;
666667
cpuid_mask(&entry->ebx, CPUID_8000_0008_EBX);
667-
if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD))
668+
/*
669+
* The preference is to use SPEC CTRL MSR instead of the
670+
* VIRT_SPEC MSR.
671+
*/
672+
if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) &&
673+
!boot_cpu_has(X86_FEATURE_AMD_SSBD))
668674
entry->ebx |= F(VIRT_SSBD);
669675
break;
670676
}

arch/x86/kvm/svm.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4115,7 +4115,8 @@ static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
41154115
break;
41164116
case MSR_IA32_SPEC_CTRL:
41174117
if (!msr_info->host_initiated &&
4118-
!guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS))
4118+
!guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS) &&
4119+
!guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD))
41194120
return 1;
41204121

41214122
msr_info->data = svm->spec_ctrl;
@@ -4217,11 +4218,12 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
42174218
break;
42184219
case MSR_IA32_SPEC_CTRL:
42194220
if (!msr->host_initiated &&
4220-
!guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS))
4221+
!guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS) &&
4222+
!guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD))
42214223
return 1;
42224224

42234225
/* The STIBP bit doesn't fault even if it's not advertised */
4224-
if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP))
4226+
if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
42254227
return 1;
42264228

42274229
svm->spec_ctrl = data;

0 commit comments

Comments
 (0)