Commit 9aa67871 authored by Tom Lendacky's avatar Tom Lendacky Committed by Stefan Bader

KVM: SVM: Implement VIRT_SPEC_CTRL support for SSBD

Expose the new virtualized architectural mechanism, VIRT_SSBD, for using
speculative store bypass disable (SSBD) under SVM.  This will allow guests
to use SSBD on hardware that uses non-architectural mechanisms for enabling
SSBD.

[ tglx: Folded the migration fixup from Paolo Bonzini ]
Signed-off-by: default avatarTom Lendacky <thomas.lendacky@amd.com>
Signed-off-by: default avatarThomas Gleixner <tglx@linutronix.de>

CVE-2018-3639 (x86)

(backported from commit bc226f07)
[smb: context and dropped guest_cpuid_has checks in svm.c]
Signed-off-by: default avatarStefan Bader <stefan.bader@canonical.com>
parent 9d7bd095
...@@ -798,7 +798,7 @@ struct kvm_x86_ops { ...@@ -798,7 +798,7 @@ struct kvm_x86_ops {
int (*hardware_setup)(void); /* __init */ int (*hardware_setup)(void); /* __init */
void (*hardware_unsetup)(void); /* __exit */ void (*hardware_unsetup)(void); /* __exit */
bool (*cpu_has_accelerated_tpr)(void); bool (*cpu_has_accelerated_tpr)(void);
bool (*cpu_has_high_real_mode_segbase)(void); bool (*has_emulated_msr)(int index);
void (*cpuid_update)(struct kvm_vcpu *vcpu); void (*cpuid_update)(struct kvm_vcpu *vcpu);
/* Create, but do not attach this VCPU */ /* Create, but do not attach this VCPU */
......
...@@ -693,7 +693,8 @@ static void init_speculation_control(struct cpuinfo_x86 *c) ...@@ -693,7 +693,8 @@ static void init_speculation_control(struct cpuinfo_x86 *c)
if (cpu_has(c, X86_FEATURE_INTEL_STIBP)) if (cpu_has(c, X86_FEATURE_INTEL_STIBP))
set_cpu_cap(c, X86_FEATURE_STIBP); set_cpu_cap(c, X86_FEATURE_STIBP);
if (cpu_has(c, X86_FEATURE_SPEC_CTRL_SSBD)) if (cpu_has(c, X86_FEATURE_SPEC_CTRL_SSBD) ||
cpu_has(c, X86_FEATURE_VIRT_SSBD))
set_cpu_cap(c, X86_FEATURE_SSBD); set_cpu_cap(c, X86_FEATURE_SSBD);
if (cpu_has(c, X86_FEATURE_AMD_IBRS)) { if (cpu_has(c, X86_FEATURE_AMD_IBRS)) {
......
...@@ -367,7 +367,7 @@ static inline int __do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function, ...@@ -367,7 +367,7 @@ static inline int __do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
/* cpuid 0x80000008.0.ebx */ /* cpuid 0x80000008.0.ebx */
const u32 kvm_cpuid_80000008_0_ebx_x86_features = const u32 kvm_cpuid_80000008_0_ebx_x86_features =
F(AMD_IBPB) | F(AMD_IBRS); F(AMD_IBPB) | F(AMD_IBRS) | F(VIRT_SSBD);
/* all calls to cpuid_count() should be made on the same cpu */ /* all calls to cpuid_count() should be made on the same cpu */
get_cpu(); get_cpu();
...@@ -597,13 +597,20 @@ static inline int __do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function, ...@@ -597,13 +597,20 @@ static inline int __do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
if (!g_phys_as) if (!g_phys_as)
g_phys_as = phys_as; g_phys_as = phys_as;
entry->eax = g_phys_as | (virt_as << 8); entry->eax = g_phys_as | (virt_as << 8);
/* IBRS and IBPB aren't necessarily present in hardware cpuid */ /*
* IBRS, IBPB and VIRT_SSBD aren't necessarily present in
* hardware cpuid
*/
if (boot_cpu_has(X86_FEATURE_AMD_IBPB)) if (boot_cpu_has(X86_FEATURE_AMD_IBPB))
entry->ebx |= F(AMD_IBPB); entry->ebx |= F(AMD_IBPB);
if (boot_cpu_has(X86_FEATURE_AMD_IBRS)) if (boot_cpu_has(X86_FEATURE_AMD_IBRS))
entry->ebx |= F(AMD_IBRS); entry->ebx |= F(AMD_IBRS);
if (boot_cpu_has(X86_FEATURE_VIRT_SSBD))
entry->ebx |= F(VIRT_SSBD);
entry->ebx &= kvm_cpuid_80000008_0_ebx_x86_features; entry->ebx &= kvm_cpuid_80000008_0_ebx_x86_features;
cpuid_mask(&entry->ebx, CPUID_8000_0008_EBX); cpuid_mask(&entry->ebx, CPUID_8000_0008_EBX);
if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD))
entry->ebx |= F(VIRT_SSBD);
entry->edx = 0; entry->edx = 0;
break; break;
} }
......
...@@ -3086,6 +3086,9 @@ static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) ...@@ -3086,6 +3086,9 @@ static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
case MSR_IA32_UCODE_REV: case MSR_IA32_UCODE_REV:
msr_info->data = 0x01000065; msr_info->data = 0x01000065;
break; break;
case MSR_AMD64_VIRT_SPEC_CTRL:
msr_info->data = svm->virt_spec_ctrl;
break;
case MSR_F15H_IC_CFG: { case MSR_F15H_IC_CFG: {
int family, model; int family, model;
...@@ -3174,6 +3177,12 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr) ...@@ -3174,6 +3177,12 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
case MSR_IA32_TSC: case MSR_IA32_TSC:
kvm_write_tsc(vcpu, msr); kvm_write_tsc(vcpu, msr);
break; break;
case MSR_AMD64_VIRT_SPEC_CTRL:
if (data & ~SPEC_CTRL_SSBD)
return 1;
svm->virt_spec_ctrl = data;
break;
case MSR_STAR: case MSR_STAR:
svm->vmcb->save.star = data; svm->vmcb->save.star = data;
break; break;
...@@ -4056,7 +4065,7 @@ static bool svm_cpu_has_accelerated_tpr(void) ...@@ -4056,7 +4065,7 @@ static bool svm_cpu_has_accelerated_tpr(void)
return false; return false;
} }
static bool svm_has_high_real_mode_segbase(void) static bool svm_has_emulated_msr(int index)
{ {
return true; return true;
} }
...@@ -4340,7 +4349,7 @@ static struct kvm_x86_ops svm_x86_ops = { ...@@ -4340,7 +4349,7 @@ static struct kvm_x86_ops svm_x86_ops = {
.hardware_enable = svm_hardware_enable, .hardware_enable = svm_hardware_enable,
.hardware_disable = svm_hardware_disable, .hardware_disable = svm_hardware_disable,
.cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr, .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
.cpu_has_high_real_mode_segbase = svm_has_high_real_mode_segbase, .has_emulated_msr = svm_has_emulated_msr,
.vcpu_create = svm_create_vcpu, .vcpu_create = svm_create_vcpu,
.vcpu_free = svm_free_vcpu, .vcpu_free = svm_free_vcpu,
......
...@@ -8428,9 +8428,21 @@ static void vmx_handle_external_intr(struct kvm_vcpu *vcpu) ...@@ -8428,9 +8428,21 @@ static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
local_irq_enable(); local_irq_enable();
} }
static bool vmx_has_high_real_mode_segbase(void) static bool vmx_has_emulated_msr(int index)
{ {
return enable_unrestricted_guest || emulate_invalid_guest_state; switch (index) {
case MSR_IA32_SMBASE:
/*
* We cannot do SMM unless we can run the guest in big
* real mode.
*/
return enable_unrestricted_guest || emulate_invalid_guest_state;
case MSR_AMD64_VIRT_SPEC_CTRL:
/* This is AMD only. */
return false;
default:
return true;
}
} }
static bool vmx_mpx_supported(void) static bool vmx_mpx_supported(void)
...@@ -10882,7 +10894,7 @@ static struct kvm_x86_ops vmx_x86_ops = { ...@@ -10882,7 +10894,7 @@ static struct kvm_x86_ops vmx_x86_ops = {
.hardware_enable = hardware_enable, .hardware_enable = hardware_enable,
.hardware_disable = hardware_disable, .hardware_disable = hardware_disable,
.cpu_has_accelerated_tpr = report_flexpriority, .cpu_has_accelerated_tpr = report_flexpriority,
.cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase, .has_emulated_msr = vmx_has_emulated_msr,
.vcpu_create = vmx_create_vcpu, .vcpu_create = vmx_create_vcpu,
.vcpu_free = vmx_free_vcpu, .vcpu_free = vmx_free_vcpu,
......
...@@ -986,6 +986,7 @@ static u32 emulated_msrs[] = { ...@@ -986,6 +986,7 @@ static u32 emulated_msrs[] = {
MSR_IA32_MCG_STATUS, MSR_IA32_MCG_STATUS,
MSR_IA32_MCG_CTL, MSR_IA32_MCG_CTL,
MSR_IA32_SMBASE, MSR_IA32_SMBASE,
MSR_AMD64_VIRT_SPEC_CTRL,
}; };
static unsigned num_emulated_msrs; static unsigned num_emulated_msrs;
...@@ -2588,7 +2589,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) ...@@ -2588,7 +2589,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
* fringe case that is not enabled except via specific settings * fringe case that is not enabled except via specific settings
* of the module parameters. * of the module parameters.
*/ */
r = kvm_x86_ops->cpu_has_high_real_mode_segbase(); r = kvm_x86_ops->has_emulated_msr(MSR_IA32_SMBASE);
break; break;
case KVM_CAP_COALESCED_MMIO: case KVM_CAP_COALESCED_MMIO:
r = KVM_COALESCED_MMIO_PAGE_OFFSET; r = KVM_COALESCED_MMIO_PAGE_OFFSET;
...@@ -4101,14 +4102,8 @@ static void kvm_init_msr_list(void) ...@@ -4101,14 +4102,8 @@ static void kvm_init_msr_list(void)
num_msrs_to_save = j; num_msrs_to_save = j;
for (i = j = 0; i < ARRAY_SIZE(emulated_msrs); i++) { for (i = j = 0; i < ARRAY_SIZE(emulated_msrs); i++) {
switch (emulated_msrs[i]) { if (!kvm_x86_ops->has_emulated_msr(emulated_msrs[i]))
case MSR_IA32_SMBASE: continue;
if (!kvm_x86_ops->cpu_has_high_real_mode_segbase())
continue;
break;
default:
break;
}
if (j < i) if (j < i)
emulated_msrs[j] = emulated_msrs[i]; emulated_msrs[j] = emulated_msrs[i];
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment