Skip to content

Commit ccbcd26

Browse files
committed
x86/bugs, KVM: Extend speculation control for VIRT_SPEC_CTRL
AMD is proposing a VIRT_SPEC_CTRL MSR to handle the Speculative Store Bypass Disable via MSR_AMD64_LS_CFG so that guests do not have to care about the bit position of the SSBD bit and thus facilitate migration. Also, the sibling coordination on Family 17H CPUs can only be done on the host. Extend x86_spec_ctrl_set_guest() and x86_spec_ctrl_restore_host() with an extra argument for the VIRT_SPEC_CTRL MSR. Hand in 0 from VMX and in SVM add a new virt_spec_ctrl member to the CPU data structure which is going to be used in later patches for the actual implementation. Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Reviewed-by: Borislav Petkov <bp@suse.de> Reviewed-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
1 parent 1f50ddb commit ccbcd26

4 files changed

Lines changed: 35 additions & 9 deletions

File tree

‎arch/x86/include/asm/spec-ctrl.h‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@
1010
* the guest has, while on VMEXIT we restore the host view. This
1111
* would be easier if SPEC_CTRL were architecturally maskable or
1212
* shadowable for guests but this is not (currently) the case.
13-
* Takes the guest view of SPEC_CTRL MSR as a parameter.
13+
* Takes the guest view of SPEC_CTRL MSR as a parameter and also
14+
* the guest's version of VIRT_SPEC_CTRL, if emulated.
1415
*/
15-
extern void x86_spec_ctrl_set_guest(u64);
16-
extern void x86_spec_ctrl_restore_host(u64);
16+
extern void x86_spec_ctrl_set_guest(u64 guest_spec_ctrl,
17+
u64 guest_virt_spec_ctrl);
18+
extern void x86_spec_ctrl_restore_host(u64 guest_spec_ctrl,
19+
u64 guest_virt_spec_ctrl);
1720

1821
/* AMD specific Speculative Store Bypass MSR data */
1922
extern u64 x86_amd_ls_cfg_base;

‎arch/x86/kernel/cpu/bugs.c‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,15 @@ u64 x86_spec_ctrl_get_default(void)
151151
}
152152
EXPORT_SYMBOL_GPL(x86_spec_ctrl_get_default);
153153

154-
void x86_spec_ctrl_set_guest(u64 guest_spec_ctrl)
154+
/**
155+
* x86_spec_ctrl_set_guest - Set speculation control registers for the guest
156+
* @guest_spec_ctrl: The guest content of MSR_SPEC_CTRL
157+
* @guest_virt_spec_ctrl: The guest controlled bits of MSR_VIRT_SPEC_CTRL
158+
* (may get translated to MSR_AMD64_LS_CFG bits)
159+
*
160+
* Avoids writing to the MSR if the content/bits are the same
161+
*/
162+
void x86_spec_ctrl_set_guest(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl)
155163
{
156164
u64 host = x86_spec_ctrl_base;
157165

@@ -168,7 +176,15 @@ void x86_spec_ctrl_set_guest(u64 guest_spec_ctrl)
168176
}
169177
EXPORT_SYMBOL_GPL(x86_spec_ctrl_set_guest);
170178

171-
void x86_spec_ctrl_restore_host(u64 guest_spec_ctrl)
179+
/**
180+
* x86_spec_ctrl_restore_host - Restore host speculation control registers
181+
* @guest_spec_ctrl: The guest content of MSR_SPEC_CTRL
182+
* @guest_virt_spec_ctrl: The guest controlled bits of MSR_VIRT_SPEC_CTRL
183+
* (may get translated to MSR_AMD64_LS_CFG bits)
184+
*
185+
* Avoids writing to the MSR if the content/bits are the same
186+
*/
187+
void x86_spec_ctrl_restore_host(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl)
172188
{
173189
u64 host = x86_spec_ctrl_base;
174190

‎arch/x86/kvm/svm.c‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,12 @@ struct vcpu_svm {
213213
} host;
214214

215215
u64 spec_ctrl;
216+
/*
217+
* Contains guest-controlled bits of VIRT_SPEC_CTRL, which will be
218+
* translated into the appropriate L2_CFG bits on the host to
219+
* perform speculative control.
220+
*/
221+
u64 virt_spec_ctrl;
216222

217223
u32 *msrpm;
218224

@@ -2060,6 +2066,7 @@ static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
20602066

20612067
vcpu->arch.microcode_version = 0x01000065;
20622068
svm->spec_ctrl = 0;
2069+
svm->virt_spec_ctrl = 0;
20632070

20642071
if (!init_event) {
20652072
svm->vcpu.arch.apic_base = APIC_DEFAULT_PHYS_BASE |
@@ -5557,7 +5564,7 @@ static void svm_vcpu_run(struct kvm_vcpu *vcpu)
55575564
* is no need to worry about the conditional branch over the wrmsr
55585565
* being speculatively taken.
55595566
*/
5560-
x86_spec_ctrl_set_guest(svm->spec_ctrl);
5567+
x86_spec_ctrl_set_guest(svm->spec_ctrl, svm->virt_spec_ctrl);
55615568

55625569
asm volatile (
55635570
"push %%" _ASM_BP "; \n\t"
@@ -5681,7 +5688,7 @@ static void svm_vcpu_run(struct kvm_vcpu *vcpu)
56815688
if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
56825689
svm->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
56835690

5684-
x86_spec_ctrl_restore_host(svm->spec_ctrl);
5691+
x86_spec_ctrl_restore_host(svm->spec_ctrl, svm->virt_spec_ctrl);
56855692

56865693
reload_tss(vcpu);
56875694

‎arch/x86/kvm/vmx.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9717,7 +9717,7 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
97179717
* is no need to worry about the conditional branch over the wrmsr
97189718
* being speculatively taken.
97199719
*/
9720-
x86_spec_ctrl_set_guest(vmx->spec_ctrl);
9720+
x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
97219721

97229722
vmx->__launched = vmx->loaded_vmcs->launched;
97239723

@@ -9865,7 +9865,7 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
98659865
if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
98669866
vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
98679867

9868-
x86_spec_ctrl_restore_host(vmx->spec_ctrl);
9868+
x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
98699869

98709870
/* Eliminate branch target predictions from guest mode */
98719871
vmexit_fill_RSB();

0 commit comments

Comments
 (0)