2021-03-22 16:27:40 +03:00
|
|
|
/*
|
|
|
|
* x86 KVM CPU type initialization
|
|
|
|
*
|
|
|
|
* Copyright 2021 SUSE LLC
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "host-cpu.h"
|
|
|
|
#include "kvm-cpu.h"
|
|
|
|
#include "qapi/error.h"
|
|
|
|
#include "sysemu/sysemu.h"
|
|
|
|
#include "hw/boards.h"
|
|
|
|
|
|
|
|
#include "kvm_i386.h"
|
|
|
|
#include "hw/core/accel-cpu.h"
|
|
|
|
|
2021-03-22 16:27:44 +03:00
|
|
|
static bool kvm_cpu_realizefn(CPUState *cs, Error **errp)
|
2021-03-22 16:27:40 +03:00
|
|
|
{
|
|
|
|
X86CPU *cpu = X86_CPU(cs);
|
|
|
|
CPUX86State *env = &cpu->env;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The realize order is important, since x86_cpu_realize() checks if
|
|
|
|
* nothing else has been set by the user (or by accelerators) in
|
2021-06-03 15:30:00 +03:00
|
|
|
* cpu->ucode_rev and cpu->phys_bits, and updates the CPUID results in
|
|
|
|
* mwait.ecx.
|
|
|
|
* This accel realization code also assumes cpu features are already expanded.
|
2021-03-22 16:27:40 +03:00
|
|
|
*
|
|
|
|
* realize order:
|
2021-06-03 15:30:00 +03:00
|
|
|
*
|
|
|
|
* x86_cpu_realize():
|
|
|
|
* -> x86_cpu_expand_features()
|
|
|
|
* -> cpu_exec_realizefn():
|
|
|
|
* -> accel_cpu_realizefn()
|
|
|
|
* kvm_cpu_realizefn() -> host_cpu_realizefn()
|
|
|
|
* -> check/update ucode_rev, phys_bits, mwait
|
2021-03-22 16:27:40 +03:00
|
|
|
*/
|
|
|
|
if (cpu->max_features) {
|
|
|
|
if (enable_cpu_pm && kvm_has_waitpkg()) {
|
|
|
|
env->features[FEAT_7_0_ECX] |= CPUID_7_0_ECX_WAITPKG;
|
|
|
|
}
|
|
|
|
if (cpu->ucode_rev == 0) {
|
|
|
|
cpu->ucode_rev =
|
|
|
|
kvm_arch_get_supported_msr_feature(kvm_state,
|
|
|
|
MSR_IA32_UCODE_REV);
|
|
|
|
}
|
|
|
|
}
|
2021-03-22 16:27:44 +03:00
|
|
|
return host_cpu_realizefn(cs, errp);
|
2021-03-22 16:27:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool lmce_supported(void)
|
|
|
|
{
|
|
|
|
uint64_t mce_cap = 0;
|
|
|
|
|
|
|
|
if (kvm_ioctl(kvm_state, KVM_X86_GET_MCE_CAP_SUPPORTED, &mce_cap) < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return !!(mce_cap & MCG_LMCE_P);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kvm_cpu_max_instance_init(X86CPU *cpu)
|
|
|
|
{
|
|
|
|
CPUX86State *env = &cpu->env;
|
|
|
|
KVMState *s = kvm_state;
|
|
|
|
|
|
|
|
host_cpu_max_instance_init(cpu);
|
|
|
|
|
|
|
|
if (lmce_supported()) {
|
|
|
|
object_property_set_bool(OBJECT(cpu), "lmce", true, &error_abort);
|
|
|
|
}
|
|
|
|
|
|
|
|
env->cpuid_min_level =
|
|
|
|
kvm_arch_get_supported_cpuid(s, 0x0, 0, R_EAX);
|
|
|
|
env->cpuid_min_xlevel =
|
|
|
|
kvm_arch_get_supported_cpuid(s, 0x80000000, 0, R_EAX);
|
|
|
|
env->cpuid_min_xlevel2 =
|
|
|
|
kvm_arch_get_supported_cpuid(s, 0xC0000000, 0, R_EAX);
|
|
|
|
}
|
|
|
|
|
2021-07-05 13:46:31 +03:00
|
|
|
static void kvm_cpu_xsave_init(void)
|
|
|
|
{
|
|
|
|
static bool first = true;
|
2022-02-17 09:04:29 +03:00
|
|
|
uint32_t eax, ebx, ecx, edx;
|
2021-07-05 13:46:31 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!first) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
first = false;
|
|
|
|
|
|
|
|
/* x87 and SSE states are in the legacy region of the XSAVE area. */
|
|
|
|
x86_ext_save_areas[XSTATE_FP_BIT].offset = 0;
|
|
|
|
x86_ext_save_areas[XSTATE_SSE_BIT].offset = 0;
|
|
|
|
|
|
|
|
for (i = XSTATE_SSE_BIT + 1; i < XSAVE_STATE_AREA_COUNT; i++) {
|
|
|
|
ExtSaveArea *esa = &x86_ext_save_areas[i];
|
|
|
|
|
2022-03-23 14:33:25 +03:00
|
|
|
if (!esa->size) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((x86_cpu_get_supported_feature_word(esa->feature, false) & esa->bits)
|
|
|
|
!= esa->bits) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
host_cpuid(0xd, i, &eax, &ebx, &ecx, &edx);
|
|
|
|
if (eax != 0) {
|
|
|
|
assert(esa->size == eax);
|
|
|
|
esa->offset = ebx;
|
|
|
|
esa->ecx = ecx;
|
2021-07-05 13:46:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
i386: do not call cpudef-only models functions for max, host, base
Some cpu properties have to be set only for cpu models in builtin_x86_defs,
registered with x86_register_cpu_model_type, and not for
cpu models "base", "max", and the subclass "host".
These properties are the ones set by function x86_cpu_apply_props,
(also including kvm_default_props, tcg_default_props),
and the "vendor" property for the KVM and HVF accelerators.
After recent refactoring of cpu, which also affected these properties,
they were instead set unconditionally for all x86 cpus.
This has been detected as a bug with Nested on AMD with cpu "host",
as svm was not turned on by default, due to the wrongful setting of
kvm_default_props via x86_cpu_apply_props, which set svm to "off".
Rectify the bug introduced in commit "i386: split cpu accelerators"
and document the functions that are builtin_x86_defs-only.
Signed-off-by: Claudio Fontana <cfontana@suse.de>
Tested-by: Alexander Bulekov <alxndr@bu.edu>
Fixes: f5cc5a5c ("i386: split cpu accelerators from cpu.c,"...)
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/477
Message-Id: <20210723112921.12637-1-cfontana@suse.de>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2021-07-23 14:29:21 +03:00
|
|
|
/*
|
|
|
|
* KVM-specific features that are automatically added/removed
|
|
|
|
* from cpudef models when KVM is enabled.
|
|
|
|
* Only for builtin_x86_defs models initialized with x86_register_cpudef_types.
|
|
|
|
*
|
|
|
|
* NOTE: features can be enabled by default only if they were
|
|
|
|
* already available in the oldest kernel version supported
|
|
|
|
* by the KVM accelerator (see "OS requirements" section at
|
|
|
|
* docs/system/target-i386.rst)
|
|
|
|
*/
|
|
|
|
static PropValue kvm_default_props[] = {
|
|
|
|
{ "kvmclock", "on" },
|
|
|
|
{ "kvm-nopiodelay", "on" },
|
|
|
|
{ "kvm-asyncpf", "on" },
|
|
|
|
{ "kvm-steal-time", "on" },
|
|
|
|
{ "kvm-pv-eoi", "on" },
|
|
|
|
{ "kvmclock-stable-bit", "on" },
|
|
|
|
{ "x2apic", "on" },
|
|
|
|
{ "kvm-msi-ext-dest-id", "off" },
|
|
|
|
{ "acpi", "off" },
|
|
|
|
{ "monitor", "off" },
|
|
|
|
{ "svm", "off" },
|
|
|
|
{ NULL, NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Only for builtin_x86_defs models initialized with x86_register_cpudef_types.
|
|
|
|
*/
|
|
|
|
void x86_cpu_change_kvm_default(const char *prop, const char *value)
|
|
|
|
{
|
|
|
|
PropValue *pv;
|
|
|
|
for (pv = kvm_default_props; pv->prop; pv++) {
|
|
|
|
if (!strcmp(pv->prop, prop)) {
|
|
|
|
pv->value = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It is valid to call this function only for properties that
|
|
|
|
* are already present in the kvm_default_props table.
|
|
|
|
*/
|
|
|
|
assert(pv->prop);
|
|
|
|
}
|
|
|
|
|
2021-03-22 16:27:40 +03:00
|
|
|
static void kvm_cpu_instance_init(CPUState *cs)
|
|
|
|
{
|
|
|
|
X86CPU *cpu = X86_CPU(cs);
|
i386: do not call cpudef-only models functions for max, host, base
Some cpu properties have to be set only for cpu models in builtin_x86_defs,
registered with x86_register_cpu_model_type, and not for
cpu models "base", "max", and the subclass "host".
These properties are the ones set by function x86_cpu_apply_props,
(also including kvm_default_props, tcg_default_props),
and the "vendor" property for the KVM and HVF accelerators.
After recent refactoring of cpu, which also affected these properties,
they were instead set unconditionally for all x86 cpus.
This has been detected as a bug with Nested on AMD with cpu "host",
as svm was not turned on by default, due to the wrongful setting of
kvm_default_props via x86_cpu_apply_props, which set svm to "off".
Rectify the bug introduced in commit "i386: split cpu accelerators"
and document the functions that are builtin_x86_defs-only.
Signed-off-by: Claudio Fontana <cfontana@suse.de>
Tested-by: Alexander Bulekov <alxndr@bu.edu>
Fixes: f5cc5a5c ("i386: split cpu accelerators from cpu.c,"...)
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/477
Message-Id: <20210723112921.12637-1-cfontana@suse.de>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2021-07-23 14:29:21 +03:00
|
|
|
X86CPUClass *xcc = X86_CPU_GET_CLASS(cpu);
|
2021-03-22 16:27:40 +03:00
|
|
|
|
|
|
|
host_cpu_instance_init(cpu);
|
|
|
|
|
i386: do not call cpudef-only models functions for max, host, base
Some cpu properties have to be set only for cpu models in builtin_x86_defs,
registered with x86_register_cpu_model_type, and not for
cpu models "base", "max", and the subclass "host".
These properties are the ones set by function x86_cpu_apply_props,
(also including kvm_default_props, tcg_default_props),
and the "vendor" property for the KVM and HVF accelerators.
After recent refactoring of cpu, which also affected these properties,
they were instead set unconditionally for all x86 cpus.
This has been detected as a bug with Nested on AMD with cpu "host",
as svm was not turned on by default, due to the wrongful setting of
kvm_default_props via x86_cpu_apply_props, which set svm to "off".
Rectify the bug introduced in commit "i386: split cpu accelerators"
and document the functions that are builtin_x86_defs-only.
Signed-off-by: Claudio Fontana <cfontana@suse.de>
Tested-by: Alexander Bulekov <alxndr@bu.edu>
Fixes: f5cc5a5c ("i386: split cpu accelerators from cpu.c,"...)
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/477
Message-Id: <20210723112921.12637-1-cfontana@suse.de>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2021-07-23 14:29:21 +03:00
|
|
|
if (xcc->model) {
|
|
|
|
/* only applies to builtin_x86_defs cpus */
|
|
|
|
if (!kvm_irqchip_in_kernel()) {
|
|
|
|
x86_cpu_change_kvm_default("x2apic", "off");
|
2022-03-14 17:25:41 +03:00
|
|
|
} else if (kvm_irqchip_is_split()) {
|
i386: do not call cpudef-only models functions for max, host, base
Some cpu properties have to be set only for cpu models in builtin_x86_defs,
registered with x86_register_cpu_model_type, and not for
cpu models "base", "max", and the subclass "host".
These properties are the ones set by function x86_cpu_apply_props,
(also including kvm_default_props, tcg_default_props),
and the "vendor" property for the KVM and HVF accelerators.
After recent refactoring of cpu, which also affected these properties,
they were instead set unconditionally for all x86 cpus.
This has been detected as a bug with Nested on AMD with cpu "host",
as svm was not turned on by default, due to the wrongful setting of
kvm_default_props via x86_cpu_apply_props, which set svm to "off".
Rectify the bug introduced in commit "i386: split cpu accelerators"
and document the functions that are builtin_x86_defs-only.
Signed-off-by: Claudio Fontana <cfontana@suse.de>
Tested-by: Alexander Bulekov <alxndr@bu.edu>
Fixes: f5cc5a5c ("i386: split cpu accelerators from cpu.c,"...)
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/477
Message-Id: <20210723112921.12637-1-cfontana@suse.de>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2021-07-23 14:29:21 +03:00
|
|
|
x86_cpu_change_kvm_default("kvm-msi-ext-dest-id", "on");
|
|
|
|
}
|
2021-03-22 16:27:40 +03:00
|
|
|
|
i386: do not call cpudef-only models functions for max, host, base
Some cpu properties have to be set only for cpu models in builtin_x86_defs,
registered with x86_register_cpu_model_type, and not for
cpu models "base", "max", and the subclass "host".
These properties are the ones set by function x86_cpu_apply_props,
(also including kvm_default_props, tcg_default_props),
and the "vendor" property for the KVM and HVF accelerators.
After recent refactoring of cpu, which also affected these properties,
they were instead set unconditionally for all x86 cpus.
This has been detected as a bug with Nested on AMD with cpu "host",
as svm was not turned on by default, due to the wrongful setting of
kvm_default_props via x86_cpu_apply_props, which set svm to "off".
Rectify the bug introduced in commit "i386: split cpu accelerators"
and document the functions that are builtin_x86_defs-only.
Signed-off-by: Claudio Fontana <cfontana@suse.de>
Tested-by: Alexander Bulekov <alxndr@bu.edu>
Fixes: f5cc5a5c ("i386: split cpu accelerators from cpu.c,"...)
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/477
Message-Id: <20210723112921.12637-1-cfontana@suse.de>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2021-07-23 14:29:21 +03:00
|
|
|
/* Special cases not set in the X86CPUDefinition structs: */
|
|
|
|
x86_cpu_apply_props(cpu, kvm_default_props);
|
|
|
|
}
|
2021-03-22 16:27:40 +03:00
|
|
|
|
|
|
|
if (cpu->max_features) {
|
|
|
|
kvm_cpu_max_instance_init(cpu);
|
|
|
|
}
|
2021-07-05 13:46:31 +03:00
|
|
|
|
|
|
|
kvm_cpu_xsave_init();
|
2021-03-22 16:27:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void kvm_cpu_accel_class_init(ObjectClass *oc, void *data)
|
|
|
|
{
|
|
|
|
AccelCPUClass *acc = ACCEL_CPU_CLASS(oc);
|
|
|
|
|
|
|
|
acc->cpu_realizefn = kvm_cpu_realizefn;
|
|
|
|
acc->cpu_instance_init = kvm_cpu_instance_init;
|
|
|
|
}
|
|
|
|
static const TypeInfo kvm_cpu_accel_type_info = {
|
|
|
|
.name = ACCEL_CPU_NAME("kvm"),
|
|
|
|
|
|
|
|
.parent = TYPE_ACCEL_CPU,
|
|
|
|
.class_init = kvm_cpu_accel_class_init,
|
|
|
|
.abstract = true,
|
|
|
|
};
|
|
|
|
static void kvm_cpu_accel_register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&kvm_cpu_accel_type_info);
|
|
|
|
}
|
|
|
|
type_init(kvm_cpu_accel_register_types);
|