1 /* 2 * x86 KVM CPU type initialization 3 * 4 * Copyright 2021 SUSE LLC 5 * 6 * This work is licensed under the terms of the GNU GPL, version 2 or later. 7 * See the COPYING file in the top-level directory. 8 */ 9 10 #include "qemu/osdep.h" 11 #include "cpu.h" 12 #include "host-cpu.h" 13 #include "kvm-cpu.h" 14 #include "qapi/error.h" 15 #include "sysemu/sysemu.h" 16 #include "hw/boards.h" 17 18 #include "kvm_i386.h" 19 #include "hw/core/accel-cpu.h" 20 21 static bool kvm_cpu_realizefn(CPUState *cs, Error **errp) 22 { 23 X86CPU *cpu = X86_CPU(cs); 24 CPUX86State *env = &cpu->env; 25 26 /* 27 * The realize order is important, since x86_cpu_realize() checks if 28 * nothing else has been set by the user (or by accelerators) in 29 * cpu->ucode_rev and cpu->phys_bits. 30 * 31 * realize order: 32 * kvm_cpu -> host_cpu -> x86_cpu 33 */ 34 if (cpu->max_features) { 35 if (enable_cpu_pm && kvm_has_waitpkg()) { 36 env->features[FEAT_7_0_ECX] |= CPUID_7_0_ECX_WAITPKG; 37 } 38 if (cpu->ucode_rev == 0) { 39 cpu->ucode_rev = 40 kvm_arch_get_supported_msr_feature(kvm_state, 41 MSR_IA32_UCODE_REV); 42 } 43 } 44 return host_cpu_realizefn(cs, errp); 45 } 46 47 /* 48 * KVM-specific features that are automatically added/removed 49 * from all CPU models when KVM is enabled. 50 * 51 * NOTE: features can be enabled by default only if they were 52 * already available in the oldest kernel version supported 53 * by the KVM accelerator (see "OS requirements" section at 54 * docs/system/target-i386.rst) 55 */ 56 static PropValue kvm_default_props[] = { 57 { "kvmclock", "on" }, 58 { "kvm-nopiodelay", "on" }, 59 { "kvm-asyncpf", "on" }, 60 { "kvm-steal-time", "on" }, 61 { "kvm-pv-eoi", "on" }, 62 { "kvmclock-stable-bit", "on" }, 63 { "x2apic", "on" }, 64 { "kvm-msi-ext-dest-id", "off" }, 65 { "acpi", "off" }, 66 { "monitor", "off" }, 67 { "svm", "off" }, 68 { NULL, NULL }, 69 }; 70 71 void x86_cpu_change_kvm_default(const char *prop, const char *value) 72 { 73 PropValue *pv; 74 for (pv = kvm_default_props; pv->prop; pv++) { 75 if (!strcmp(pv->prop, prop)) { 76 pv->value = value; 77 break; 78 } 79 } 80 81 /* 82 * It is valid to call this function only for properties that 83 * are already present in the kvm_default_props table. 84 */ 85 assert(pv->prop); 86 } 87 88 static bool lmce_supported(void) 89 { 90 uint64_t mce_cap = 0; 91 92 if (kvm_ioctl(kvm_state, KVM_X86_GET_MCE_CAP_SUPPORTED, &mce_cap) < 0) { 93 return false; 94 } 95 return !!(mce_cap & MCG_LMCE_P); 96 } 97 98 static void kvm_cpu_max_instance_init(X86CPU *cpu) 99 { 100 CPUX86State *env = &cpu->env; 101 KVMState *s = kvm_state; 102 103 host_cpu_max_instance_init(cpu); 104 105 if (lmce_supported()) { 106 object_property_set_bool(OBJECT(cpu), "lmce", true, &error_abort); 107 } 108 109 env->cpuid_min_level = 110 kvm_arch_get_supported_cpuid(s, 0x0, 0, R_EAX); 111 env->cpuid_min_xlevel = 112 kvm_arch_get_supported_cpuid(s, 0x80000000, 0, R_EAX); 113 env->cpuid_min_xlevel2 = 114 kvm_arch_get_supported_cpuid(s, 0xC0000000, 0, R_EAX); 115 } 116 117 static void kvm_cpu_instance_init(CPUState *cs) 118 { 119 X86CPU *cpu = X86_CPU(cs); 120 121 host_cpu_instance_init(cpu); 122 123 if (!kvm_irqchip_in_kernel()) { 124 x86_cpu_change_kvm_default("x2apic", "off"); 125 } else if (kvm_irqchip_is_split() && kvm_enable_x2apic()) { 126 x86_cpu_change_kvm_default("kvm-msi-ext-dest-id", "on"); 127 } 128 129 /* Special cases not set in the X86CPUDefinition structs: */ 130 131 x86_cpu_apply_props(cpu, kvm_default_props); 132 133 if (cpu->max_features) { 134 kvm_cpu_max_instance_init(cpu); 135 } 136 } 137 138 static void kvm_cpu_accel_class_init(ObjectClass *oc, void *data) 139 { 140 AccelCPUClass *acc = ACCEL_CPU_CLASS(oc); 141 142 acc->cpu_realizefn = kvm_cpu_realizefn; 143 acc->cpu_instance_init = kvm_cpu_instance_init; 144 } 145 static const TypeInfo kvm_cpu_accel_type_info = { 146 .name = ACCEL_CPU_NAME("kvm"), 147 148 .parent = TYPE_ACCEL_CPU, 149 .class_init = kvm_cpu_accel_class_init, 150 .abstract = true, 151 }; 152 static void kvm_cpu_accel_register_types(void) 153 { 154 type_register_static(&kvm_cpu_accel_type_info); 155 } 156 type_init(kvm_cpu_accel_register_types); 157