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 static PropValue kvm_default_props[] = { 52 { "kvmclock", "on" }, 53 { "kvm-nopiodelay", "on" }, 54 { "kvm-asyncpf", "on" }, 55 { "kvm-steal-time", "on" }, 56 { "kvm-pv-eoi", "on" }, 57 { "kvmclock-stable-bit", "on" }, 58 { "x2apic", "on" }, 59 { "kvm-msi-ext-dest-id", "off" }, 60 { "acpi", "off" }, 61 { "monitor", "off" }, 62 { "svm", "off" }, 63 { NULL, NULL }, 64 }; 65 66 void x86_cpu_change_kvm_default(const char *prop, const char *value) 67 { 68 PropValue *pv; 69 for (pv = kvm_default_props; pv->prop; pv++) { 70 if (!strcmp(pv->prop, prop)) { 71 pv->value = value; 72 break; 73 } 74 } 75 76 /* 77 * It is valid to call this function only for properties that 78 * are already present in the kvm_default_props table. 79 */ 80 assert(pv->prop); 81 } 82 83 static bool lmce_supported(void) 84 { 85 uint64_t mce_cap = 0; 86 87 if (kvm_ioctl(kvm_state, KVM_X86_GET_MCE_CAP_SUPPORTED, &mce_cap) < 0) { 88 return false; 89 } 90 return !!(mce_cap & MCG_LMCE_P); 91 } 92 93 static void kvm_cpu_max_instance_init(X86CPU *cpu) 94 { 95 CPUX86State *env = &cpu->env; 96 KVMState *s = kvm_state; 97 98 host_cpu_max_instance_init(cpu); 99 100 if (lmce_supported()) { 101 object_property_set_bool(OBJECT(cpu), "lmce", true, &error_abort); 102 } 103 104 env->cpuid_min_level = 105 kvm_arch_get_supported_cpuid(s, 0x0, 0, R_EAX); 106 env->cpuid_min_xlevel = 107 kvm_arch_get_supported_cpuid(s, 0x80000000, 0, R_EAX); 108 env->cpuid_min_xlevel2 = 109 kvm_arch_get_supported_cpuid(s, 0xC0000000, 0, R_EAX); 110 } 111 112 static void kvm_cpu_instance_init(CPUState *cs) 113 { 114 X86CPU *cpu = X86_CPU(cs); 115 116 host_cpu_instance_init(cpu); 117 118 if (!kvm_irqchip_in_kernel()) { 119 x86_cpu_change_kvm_default("x2apic", "off"); 120 } else if (kvm_irqchip_is_split() && kvm_enable_x2apic()) { 121 x86_cpu_change_kvm_default("kvm-msi-ext-dest-id", "on"); 122 } 123 124 /* Special cases not set in the X86CPUDefinition structs: */ 125 126 x86_cpu_apply_props(cpu, kvm_default_props); 127 128 if (cpu->max_features) { 129 kvm_cpu_max_instance_init(cpu); 130 } 131 } 132 133 static void kvm_cpu_accel_class_init(ObjectClass *oc, void *data) 134 { 135 AccelCPUClass *acc = ACCEL_CPU_CLASS(oc); 136 137 acc->cpu_realizefn = kvm_cpu_realizefn; 138 acc->cpu_instance_init = kvm_cpu_instance_init; 139 } 140 static const TypeInfo kvm_cpu_accel_type_info = { 141 .name = ACCEL_CPU_NAME("kvm"), 142 143 .parent = TYPE_ACCEL_CPU, 144 .class_init = kvm_cpu_accel_class_init, 145 .abstract = true, 146 }; 147 static void kvm_cpu_accel_register_types(void) 148 { 149 type_register_static(&kvm_cpu_accel_type_info); 150 } 151 type_init(kvm_cpu_accel_register_types); 152