Thomas Gleixner | 20c8ccb | 2019-06-04 10:11:32 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Kernel-based Virtual Machine driver for Linux |
| 4 | * cpuid support routines |
| 5 | * |
| 6 | * derived from arch/x86/kvm/x86.c |
| 7 | * |
| 8 | * Copyright 2011 Red Hat, Inc. and/or its affiliates. |
| 9 | * Copyright IBM Corporation, 2008 |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/kvm_host.h> |
Paul Gortmaker | 1767e93 | 2016-07-13 20:19:00 -0400 | [diff] [blame] | 13 | #include <linux/export.h> |
Jan Kiszka | bb5a798 | 2011-12-14 17:58:18 +0100 | [diff] [blame] | 14 | #include <linux/vmalloc.h> |
| 15 | #include <linux/uaccess.h> |
Ingo Molnar | 3905f9a | 2017-02-05 12:07:04 +0100 | [diff] [blame] | 16 | #include <linux/sched/stat.h> |
| 17 | |
Luwei Kang | 4504b5c | 2016-11-07 14:03:20 +0800 | [diff] [blame] | 18 | #include <asm/processor.h> |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 19 | #include <asm/user.h> |
Ingo Molnar | 669ebab | 2015-04-28 08:41:33 +0200 | [diff] [blame] | 20 | #include <asm/fpu/xstate.h> |
Sean Christopherson | 72add91 | 2021-04-12 16:21:42 +1200 | [diff] [blame] | 21 | #include <asm/sgx.h> |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 22 | #include "cpuid.h" |
| 23 | #include "lapic.h" |
| 24 | #include "mmu.h" |
| 25 | #include "trace.h" |
Wei Huang | 474a5bb | 2015-06-19 13:54:23 +0200 | [diff] [blame] | 26 | #include "pmu.h" |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 27 | |
Sean Christopherson | 66a6950 | 2020-03-02 15:56:41 -0800 | [diff] [blame] | 28 | /* |
| 29 | * Unlike "struct cpuinfo_x86.x86_capability", kvm_cpu_caps doesn't need to be |
| 30 | * aligned to sizeof(unsigned long) because it's not accessed via bitops. |
| 31 | */ |
Sean Christopherson | 4e66c0c | 2021-04-12 16:21:35 +1200 | [diff] [blame] | 32 | u32 kvm_cpu_caps[NR_KVM_CPU_CAPS] __read_mostly; |
Sean Christopherson | 66a6950 | 2020-03-02 15:56:41 -0800 | [diff] [blame] | 33 | EXPORT_SYMBOL_GPL(kvm_cpu_caps); |
| 34 | |
Guang Zeng | be50b20 | 2022-01-05 04:35:29 -0800 | [diff] [blame] | 35 | u32 xstate_required_size(u64 xstate_bv, bool compacted) |
Paolo Bonzini | 4344ee9 | 2013-10-02 16:06:16 +0200 | [diff] [blame] | 36 | { |
| 37 | int feature_bit = 0; |
| 38 | u32 ret = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET; |
| 39 | |
Dave Hansen | d91cab7 | 2015-09-02 16:31:26 -0700 | [diff] [blame] | 40 | xstate_bv &= XFEATURE_MASK_EXTEND; |
Paolo Bonzini | 4344ee9 | 2013-10-02 16:06:16 +0200 | [diff] [blame] | 41 | while (xstate_bv) { |
| 42 | if (xstate_bv & 0x1) { |
Paolo Bonzini | 412a3c4 | 2014-12-03 14:38:01 +0100 | [diff] [blame] | 43 | u32 eax, ebx, ecx, edx, offset; |
Paolo Bonzini | 4344ee9 | 2013-10-02 16:06:16 +0200 | [diff] [blame] | 44 | cpuid_count(0xD, feature_bit, &eax, &ebx, &ecx, &edx); |
Jing Liu | cc04b6a | 2022-01-05 04:35:14 -0800 | [diff] [blame] | 45 | /* ECX[1]: 64B alignment in compacted form */ |
| 46 | if (compacted) |
| 47 | offset = (ecx & 0x2) ? ALIGN(ret, 64) : ret; |
| 48 | else |
| 49 | offset = ebx; |
Paolo Bonzini | 412a3c4 | 2014-12-03 14:38:01 +0100 | [diff] [blame] | 50 | ret = max(ret, offset + eax); |
Paolo Bonzini | 4344ee9 | 2013-10-02 16:06:16 +0200 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | xstate_bv >>= 1; |
| 54 | feature_bit++; |
| 55 | } |
| 56 | |
| 57 | return ret; |
| 58 | } |
| 59 | |
Babu Moger | b73a543 | 2021-09-23 20:15:28 -0500 | [diff] [blame] | 60 | /* |
| 61 | * This one is tied to SSB in the user API, and not |
| 62 | * visible in /proc/cpuinfo. |
| 63 | */ |
| 64 | #define KVM_X86_FEATURE_PSFD (13*32+28) /* Predictive Store Forwarding Disable */ |
| 65 | |
Sean Christopherson | 8738200 | 2019-12-17 13:32:42 -0800 | [diff] [blame] | 66 | #define F feature_bit |
Sean Christopherson | 4e66c0c | 2021-04-12 16:21:35 +1200 | [diff] [blame] | 67 | #define SF(name) (boot_cpu_has(X86_FEATURE_##name) ? F(name) : 0) |
Paolo Bonzini | 5c404ca | 2014-12-03 14:34:47 +0100 | [diff] [blame] | 68 | |
Babu Moger | b73a543 | 2021-09-23 20:15:28 -0500 | [diff] [blame] | 69 | |
Vitaly Kuznetsov | f69858f | 2020-10-01 15:05:39 +0200 | [diff] [blame] | 70 | static inline struct kvm_cpuid_entry2 *cpuid_entry2_find( |
| 71 | struct kvm_cpuid_entry2 *entries, int nent, u32 function, u32 index) |
| 72 | { |
| 73 | struct kvm_cpuid_entry2 *e; |
| 74 | int i; |
| 75 | |
| 76 | for (i = 0; i < nent; i++) { |
| 77 | e = &entries[i]; |
| 78 | |
Sean Christopherson | e8a747d | 2021-09-29 15:24:25 -0700 | [diff] [blame] | 79 | if (e->function == function && |
| 80 | (!(e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) || e->index == index)) |
Vitaly Kuznetsov | f69858f | 2020-10-01 15:05:39 +0200 | [diff] [blame] | 81 | return e; |
| 82 | } |
| 83 | |
| 84 | return NULL; |
| 85 | } |
| 86 | |
Jing Liu | 5ab2f45 | 2022-01-05 04:35:19 -0800 | [diff] [blame] | 87 | static int kvm_check_cpuid(struct kvm_vcpu *vcpu, |
| 88 | struct kvm_cpuid_entry2 *entries, |
| 89 | int nent) |
Xiaoyao Li | a76733a | 2020-07-09 12:34:22 +0800 | [diff] [blame] | 90 | { |
| 91 | struct kvm_cpuid_entry2 *best; |
Jing Liu | 5ab2f45 | 2022-01-05 04:35:19 -0800 | [diff] [blame] | 92 | u64 xfeatures; |
Xiaoyao Li | a76733a | 2020-07-09 12:34:22 +0800 | [diff] [blame] | 93 | |
| 94 | /* |
| 95 | * The existing code assumes virtual address is 48-bit or 57-bit in the |
| 96 | * canonical address checks; exit if it is ever changed. |
| 97 | */ |
Vitaly Kuznetsov | f69858f | 2020-10-01 15:05:39 +0200 | [diff] [blame] | 98 | best = cpuid_entry2_find(entries, nent, 0x80000008, 0); |
Xiaoyao Li | a76733a | 2020-07-09 12:34:22 +0800 | [diff] [blame] | 99 | if (best) { |
| 100 | int vaddr_bits = (best->eax & 0xff00) >> 8; |
| 101 | |
| 102 | if (vaddr_bits != 48 && vaddr_bits != 57 && vaddr_bits != 0) |
| 103 | return -EINVAL; |
| 104 | } |
| 105 | |
Jing Liu | 5ab2f45 | 2022-01-05 04:35:19 -0800 | [diff] [blame] | 106 | /* |
| 107 | * Exposing dynamic xfeatures to the guest requires additional |
| 108 | * enabling in the FPU, e.g. to expand the guest XSAVE state size. |
| 109 | */ |
| 110 | best = cpuid_entry2_find(entries, nent, 0xd, 0); |
| 111 | if (!best) |
| 112 | return 0; |
| 113 | |
| 114 | xfeatures = best->eax | ((u64)best->edx << 32); |
| 115 | xfeatures &= XFEATURE_MASK_USER_DYNAMIC; |
| 116 | if (!xfeatures) |
| 117 | return 0; |
| 118 | |
| 119 | return fpu_enable_guest_xfd_features(&vcpu->arch.guest_fpu, xfeatures); |
Xiaoyao Li | a76733a | 2020-07-09 12:34:22 +0800 | [diff] [blame] | 120 | } |
| 121 | |
Vitaly Kuznetsov | c6617c6 | 2022-01-17 16:05:40 +0100 | [diff] [blame] | 122 | /* Check whether the supplied CPUID data is equal to what is already set for the vCPU. */ |
| 123 | static int kvm_cpuid_check_equal(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2, |
| 124 | int nent) |
| 125 | { |
| 126 | struct kvm_cpuid_entry2 *orig; |
| 127 | int i; |
| 128 | |
| 129 | if (nent != vcpu->arch.cpuid_nent) |
| 130 | return -EINVAL; |
| 131 | |
| 132 | for (i = 0; i < nent; i++) { |
| 133 | orig = &vcpu->arch.cpuid_entries[i]; |
| 134 | if (e2[i].function != orig->function || |
| 135 | e2[i].index != orig->index || |
Vitaly Kuznetsov | 033a3ea | 2022-01-26 14:18:04 +0100 | [diff] [blame] | 136 | e2[i].flags != orig->flags || |
Vitaly Kuznetsov | c6617c6 | 2022-01-17 16:05:40 +0100 | [diff] [blame] | 137 | e2[i].eax != orig->eax || e2[i].ebx != orig->ebx || |
| 138 | e2[i].ecx != orig->ecx || e2[i].edx != orig->edx) |
| 139 | return -EINVAL; |
| 140 | } |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
Paul Durrant | 760849b | 2021-11-05 09:51:01 +0000 | [diff] [blame] | 145 | static void kvm_update_kvm_cpuid_base(struct kvm_vcpu *vcpu) |
| 146 | { |
| 147 | u32 function; |
| 148 | struct kvm_cpuid_entry2 *entry; |
| 149 | |
| 150 | vcpu->arch.kvm_cpuid_base = 0; |
| 151 | |
| 152 | for_each_possible_hypervisor_cpuid_base(function) { |
| 153 | entry = kvm_find_cpuid_entry(vcpu, function, 0); |
| 154 | |
| 155 | if (entry) { |
| 156 | u32 signature[3]; |
| 157 | |
| 158 | signature[0] = entry->ebx; |
| 159 | signature[1] = entry->ecx; |
| 160 | signature[2] = entry->edx; |
| 161 | |
| 162 | BUILD_BUG_ON(sizeof(signature) > sizeof(KVM_SIGNATURE)); |
| 163 | if (!memcmp(signature, KVM_SIGNATURE, sizeof(signature))) { |
| 164 | vcpu->arch.kvm_cpuid_base = function; |
| 165 | break; |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
Vitaly Kuznetsov | ee3a5f9 | 2022-01-17 16:05:39 +0100 | [diff] [blame] | 171 | static struct kvm_cpuid_entry2 *__kvm_find_kvm_cpuid_features(struct kvm_vcpu *vcpu, |
| 172 | struct kvm_cpuid_entry2 *entries, int nent) |
Paul Durrant | 760849b | 2021-11-05 09:51:01 +0000 | [diff] [blame] | 173 | { |
| 174 | u32 base = vcpu->arch.kvm_cpuid_base; |
| 175 | |
| 176 | if (!base) |
| 177 | return NULL; |
| 178 | |
Vitaly Kuznetsov | ee3a5f9 | 2022-01-17 16:05:39 +0100 | [diff] [blame] | 179 | return cpuid_entry2_find(entries, nent, base | KVM_CPUID_FEATURES, 0); |
| 180 | } |
| 181 | |
| 182 | static struct kvm_cpuid_entry2 *kvm_find_kvm_cpuid_features(struct kvm_vcpu *vcpu) |
| 183 | { |
| 184 | return __kvm_find_kvm_cpuid_features(vcpu, vcpu->arch.cpuid_entries, |
| 185 | vcpu->arch.cpuid_nent); |
Paul Durrant | 760849b | 2021-11-05 09:51:01 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Oliver Upton | 01b4f51 | 2020-10-27 16:10:42 -0700 | [diff] [blame] | 188 | void kvm_update_pv_runtime(struct kvm_vcpu *vcpu) |
| 189 | { |
Paul Durrant | 760849b | 2021-11-05 09:51:01 +0000 | [diff] [blame] | 190 | struct kvm_cpuid_entry2 *best = kvm_find_kvm_cpuid_features(vcpu); |
Oliver Upton | 01b4f51 | 2020-10-27 16:10:42 -0700 | [diff] [blame] | 191 | |
| 192 | /* |
| 193 | * save the feature bitmap to avoid cpuid lookup for every PV |
| 194 | * operation |
| 195 | */ |
| 196 | if (best) |
| 197 | vcpu->arch.pv_cpuid.features = best->eax; |
| 198 | } |
| 199 | |
Vitaly Kuznetsov | 5c89be1 | 2022-01-24 11:36:05 +0100 | [diff] [blame] | 200 | /* |
| 201 | * Calculate guest's supported XCR0 taking into account guest CPUID data and |
| 202 | * supported_xcr0 (comprised of host configuration and KVM_SUPPORTED_XCR0). |
| 203 | */ |
| 204 | static u64 cpuid_get_supported_xcr0(struct kvm_cpuid_entry2 *entries, int nent) |
| 205 | { |
| 206 | struct kvm_cpuid_entry2 *best; |
| 207 | |
| 208 | best = cpuid_entry2_find(entries, nent, 0xd, 0); |
| 209 | if (!best) |
| 210 | return 0; |
| 211 | |
| 212 | return (best->eax | ((u64)best->edx << 32)) & supported_xcr0; |
| 213 | } |
| 214 | |
Vitaly Kuznetsov | ee3a5f9 | 2022-01-17 16:05:39 +0100 | [diff] [blame] | 215 | static void __kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *entries, |
| 216 | int nent) |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 217 | { |
| 218 | struct kvm_cpuid_entry2 *best; |
Vitaly Kuznetsov | 5c89be1 | 2022-01-24 11:36:05 +0100 | [diff] [blame] | 219 | u64 guest_supported_xcr0 = cpuid_get_supported_xcr0(entries, nent); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 220 | |
Vitaly Kuznetsov | ee3a5f9 | 2022-01-17 16:05:39 +0100 | [diff] [blame] | 221 | best = cpuid_entry2_find(entries, nent, 1, 0); |
Xiaoyao Li | 0d3b2ba | 2020-07-08 14:50:48 +0800 | [diff] [blame] | 222 | if (best) { |
| 223 | /* Update OSXSAVE bit */ |
| 224 | if (boot_cpu_has(X86_FEATURE_XSAVE)) |
| 225 | cpuid_entry_change(best, X86_FEATURE_OSXSAVE, |
Sean Christopherson | b32666b | 2020-03-02 15:56:31 -0800 | [diff] [blame] | 226 | kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 227 | |
Xiaoyao Li | 0d3b2ba | 2020-07-08 14:50:48 +0800 | [diff] [blame] | 228 | cpuid_entry_change(best, X86_FEATURE_APIC, |
Sean Christopherson | b32666b | 2020-03-02 15:56:31 -0800 | [diff] [blame] | 229 | vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE); |
Xiaoyao Li | 0d3b2ba | 2020-07-08 14:50:48 +0800 | [diff] [blame] | 230 | } |
Jim Mattson | c7dd15b | 2016-11-09 09:50:11 -0800 | [diff] [blame] | 231 | |
Vitaly Kuznetsov | ee3a5f9 | 2022-01-17 16:05:39 +0100 | [diff] [blame] | 232 | best = cpuid_entry2_find(entries, nent, 7, 0); |
Sean Christopherson | b32666b | 2020-03-02 15:56:31 -0800 | [diff] [blame] | 233 | if (best && boot_cpu_has(X86_FEATURE_PKU) && best->function == 0x7) |
| 234 | cpuid_entry_change(best, X86_FEATURE_OSPKE, |
| 235 | kvm_read_cr4_bits(vcpu, X86_CR4_PKE)); |
Huaitong Han | b9baba8 | 2016-03-22 16:51:21 +0800 | [diff] [blame] | 236 | |
Vitaly Kuznetsov | ee3a5f9 | 2022-01-17 16:05:39 +0100 | [diff] [blame] | 237 | best = cpuid_entry2_find(entries, nent, 0xD, 0); |
Xiaoyao Li | aedbaf4 | 2020-07-09 12:34:23 +0800 | [diff] [blame] | 238 | if (best) |
Xiaoyao Li | a71936ab | 2020-04-29 23:43:12 +0800 | [diff] [blame] | 239 | best->ebx = xstate_required_size(vcpu->arch.xcr0, false); |
Paolo Bonzini | d7876f1 | 2013-10-02 16:06:15 +0200 | [diff] [blame] | 240 | |
Vitaly Kuznetsov | ee3a5f9 | 2022-01-17 16:05:39 +0100 | [diff] [blame] | 241 | best = cpuid_entry2_find(entries, nent, 0xD, 1); |
Sean Christopherson | 4c61534 | 2020-03-02 15:56:30 -0800 | [diff] [blame] | 242 | if (best && (cpuid_entry_has(best, X86_FEATURE_XSAVES) || |
| 243 | cpuid_entry_has(best, X86_FEATURE_XSAVEC))) |
Paolo Bonzini | 412a3c4 | 2014-12-03 14:38:01 +0100 | [diff] [blame] | 244 | best->ebx = xstate_required_size(vcpu->arch.xcr0, true); |
| 245 | |
Vitaly Kuznetsov | ee3a5f9 | 2022-01-17 16:05:39 +0100 | [diff] [blame] | 246 | best = __kvm_find_kvm_cpuid_features(vcpu, entries, nent); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 247 | if (kvm_hlt_in_guest(vcpu->kvm) && best && |
| 248 | (best->eax & (1 << KVM_FEATURE_PV_UNHALT))) |
| 249 | best->eax &= ~(1 << KVM_FEATURE_PV_UNHALT); |
| 250 | |
Wanpeng Li | 511a8556 | 2019-05-21 14:06:54 +0800 | [diff] [blame] | 251 | if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT)) { |
Vitaly Kuznetsov | ee3a5f9 | 2022-01-17 16:05:39 +0100 | [diff] [blame] | 252 | best = cpuid_entry2_find(entries, nent, 0x1, 0); |
Sean Christopherson | b32666b | 2020-03-02 15:56:31 -0800 | [diff] [blame] | 253 | if (best) |
| 254 | cpuid_entry_change(best, X86_FEATURE_MWAIT, |
| 255 | vcpu->arch.ia32_misc_enable_msr & |
| 256 | MSR_IA32_MISC_ENABLE_MWAIT); |
Wanpeng Li | 511a8556 | 2019-05-21 14:06:54 +0800 | [diff] [blame] | 257 | } |
Vitaly Kuznetsov | 5c89be1 | 2022-01-24 11:36:05 +0100 | [diff] [blame] | 258 | |
| 259 | /* |
| 260 | * Bits 127:0 of the allowed SECS.ATTRIBUTES (CPUID.0x12.0x1) enumerate |
| 261 | * the supported XSAVE Feature Request Mask (XFRM), i.e. the enclave's |
| 262 | * requested XCR0 value. The enclave's XFRM must be a subset of XCRO |
| 263 | * at the time of EENTER, thus adjust the allowed XFRM by the guest's |
| 264 | * supported XCR0. Similar to XCR0 handling, FP and SSE are forced to |
| 265 | * '1' even on CPUs that don't support XSAVE. |
| 266 | */ |
| 267 | best = cpuid_entry2_find(entries, nent, 0x12, 0x1); |
| 268 | if (best) { |
| 269 | best->ecx &= guest_supported_xcr0 & 0xffffffff; |
| 270 | best->edx &= guest_supported_xcr0 >> 32; |
| 271 | best->ecx |= XFEATURE_MASK_FPSSE; |
| 272 | } |
Xiaoyao Li | aedbaf4 | 2020-07-09 12:34:23 +0800 | [diff] [blame] | 273 | } |
Vitaly Kuznetsov | ee3a5f9 | 2022-01-17 16:05:39 +0100 | [diff] [blame] | 274 | |
| 275 | void kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu) |
| 276 | { |
| 277 | __kvm_update_cpuid_runtime(vcpu, vcpu->arch.cpuid_entries, vcpu->arch.cpuid_nent); |
| 278 | } |
Jim Mattson | 2259c17 | 2020-10-29 10:06:48 -0700 | [diff] [blame] | 279 | EXPORT_SYMBOL_GPL(kvm_update_cpuid_runtime); |
Xiaoyao Li | aedbaf4 | 2020-07-09 12:34:23 +0800 | [diff] [blame] | 280 | |
Xiaoyao Li | 346ce35 | 2020-07-09 12:34:24 +0800 | [diff] [blame] | 281 | static void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu) |
Xiaoyao Li | aedbaf4 | 2020-07-09 12:34:23 +0800 | [diff] [blame] | 282 | { |
| 283 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 284 | struct kvm_cpuid_entry2 *best; |
| 285 | |
| 286 | best = kvm_find_cpuid_entry(vcpu, 1, 0); |
| 287 | if (best && apic) { |
| 288 | if (cpuid_entry_has(best, X86_FEATURE_TSC_DEADLINE_TIMER)) |
| 289 | apic->lapic_timer.timer_mode_mask = 3 << 17; |
| 290 | else |
| 291 | apic->lapic_timer.timer_mode_mask = 1 << 17; |
| 292 | |
| 293 | kvm_apic_set_version(vcpu); |
| 294 | } |
| 295 | |
Vitaly Kuznetsov | 5c89be1 | 2022-01-24 11:36:05 +0100 | [diff] [blame] | 296 | vcpu->arch.guest_supported_xcr0 = |
| 297 | cpuid_get_supported_xcr0(vcpu->arch.cpuid_entries, vcpu->arch.cpuid_nent); |
Sean Christopherson | 72add91 | 2021-04-12 16:21:42 +1200 | [diff] [blame] | 298 | |
Oliver Upton | 01b4f51 | 2020-10-27 16:10:42 -0700 | [diff] [blame] | 299 | kvm_update_pv_runtime(vcpu); |
| 300 | |
Eugene Korenevsky | 5a4f55c | 2015-03-29 23:56:12 +0300 | [diff] [blame] | 301 | vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu); |
Sean Christopherson | a8ac864 | 2021-02-03 16:01:15 -0800 | [diff] [blame] | 302 | vcpu->arch.reserved_gpa_bits = kvm_vcpu_reserved_gpa_bits_raw(vcpu); |
Eugene Korenevsky | 5a4f55c | 2015-03-29 23:56:12 +0300 | [diff] [blame] | 303 | |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 304 | kvm_pmu_refresh(vcpu); |
Krish Sadhukhan | b899c13 | 2020-07-08 00:39:55 +0000 | [diff] [blame] | 305 | vcpu->arch.cr4_guest_rsvd_bits = |
| 306 | __cr4_reserved_bits(guest_cpuid_has, vcpu); |
Sean Christopherson | c44d9b3 | 2020-09-29 21:16:56 -0700 | [diff] [blame] | 307 | |
Vitaly Kuznetsov | 8f01455 | 2021-01-26 14:48:14 +0100 | [diff] [blame] | 308 | kvm_hv_set_cpuid(vcpu); |
| 309 | |
Sean Christopherson | c44d9b3 | 2020-09-29 21:16:56 -0700 | [diff] [blame] | 310 | /* Invoke the vendor callback only after the above state is updated. */ |
Jason Baron | b3646477 | 2021-01-14 22:27:56 -0500 | [diff] [blame] | 311 | static_call(kvm_x86_vcpu_after_set_cpuid)(vcpu); |
Sean Christopherson | 5b7f575 | 2021-02-03 16:01:13 -0800 | [diff] [blame] | 312 | |
| 313 | /* |
Sean Christopherson | 49c6f87 | 2021-06-22 10:56:51 -0700 | [diff] [blame] | 314 | * Except for the MMU, which needs to do its thing any vendor specific |
| 315 | * adjustments to the reserved GPA bits. |
Sean Christopherson | 5b7f575 | 2021-02-03 16:01:13 -0800 | [diff] [blame] | 316 | */ |
Sean Christopherson | 49c6f87 | 2021-06-22 10:56:51 -0700 | [diff] [blame] | 317 | kvm_mmu_after_set_cpuid(vcpu); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 318 | } |
| 319 | |
Eugene Korenevsky | 5a4f55c | 2015-03-29 23:56:12 +0300 | [diff] [blame] | 320 | int cpuid_query_maxphyaddr(struct kvm_vcpu *vcpu) |
| 321 | { |
| 322 | struct kvm_cpuid_entry2 *best; |
| 323 | |
| 324 | best = kvm_find_cpuid_entry(vcpu, 0x80000000, 0); |
| 325 | if (!best || best->eax < 0x80000008) |
| 326 | goto not_found; |
| 327 | best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0); |
| 328 | if (best) |
| 329 | return best->eax & 0xff; |
| 330 | not_found: |
| 331 | return 36; |
| 332 | } |
Eugene Korenevsky | 5a4f55c | 2015-03-29 23:56:12 +0300 | [diff] [blame] | 333 | |
Sean Christopherson | a8ac864 | 2021-02-03 16:01:15 -0800 | [diff] [blame] | 334 | /* |
| 335 | * This "raw" version returns the reserved GPA bits without any adjustments for |
| 336 | * encryption technologies that usurp bits. The raw mask should be used if and |
| 337 | * only if hardware does _not_ strip the usurped bits, e.g. in virtual MTRRs. |
| 338 | */ |
| 339 | u64 kvm_vcpu_reserved_gpa_bits_raw(struct kvm_vcpu *vcpu) |
| 340 | { |
| 341 | return rsvd_bits(cpuid_maxphyaddr(vcpu), 63); |
| 342 | } |
| 343 | |
Sean Christopherson | 8b44b17 | 2021-11-05 09:51:00 +0000 | [diff] [blame] | 344 | static int kvm_set_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2, |
| 345 | int nent) |
| 346 | { |
Jing Liu | 5ab2f45 | 2022-01-05 04:35:19 -0800 | [diff] [blame] | 347 | int r; |
Sean Christopherson | 8b44b17 | 2021-11-05 09:51:00 +0000 | [diff] [blame] | 348 | |
Vitaly Kuznetsov | ee3a5f9 | 2022-01-17 16:05:39 +0100 | [diff] [blame] | 349 | __kvm_update_cpuid_runtime(vcpu, e2, nent); |
| 350 | |
Vitaly Kuznetsov | c6617c6 | 2022-01-17 16:05:40 +0100 | [diff] [blame] | 351 | /* |
| 352 | * KVM does not correctly handle changing guest CPUID after KVM_RUN, as |
| 353 | * MAXPHYADDR, GBPAGES support, AMD reserved bit behavior, etc.. aren't |
| 354 | * tracked in kvm_mmu_page_role. As a result, KVM may miss guest page |
| 355 | * faults due to reusing SPs/SPTEs. In practice no sane VMM mucks with |
| 356 | * the core vCPU model on the fly. It would've been better to forbid any |
| 357 | * KVM_SET_CPUID{,2} calls after KVM_RUN altogether but unfortunately |
| 358 | * some VMMs (e.g. QEMU) reuse vCPU fds for CPU hotplug/unplug and do |
| 359 | * KVM_SET_CPUID{,2} again. To support this legacy behavior, check |
| 360 | * whether the supplied CPUID data is equal to what's already set. |
| 361 | */ |
Sean Christopherson | 811f95f | 2022-01-25 21:04:45 +0000 | [diff] [blame] | 362 | if (vcpu->arch.last_vmentry_cpu != -1) { |
| 363 | r = kvm_cpuid_check_equal(vcpu, e2, nent); |
| 364 | if (r) |
| 365 | return r; |
| 366 | |
| 367 | kvfree(e2); |
| 368 | return 0; |
| 369 | } |
Vitaly Kuznetsov | c6617c6 | 2022-01-17 16:05:40 +0100 | [diff] [blame] | 370 | |
Jing Liu | 5ab2f45 | 2022-01-05 04:35:19 -0800 | [diff] [blame] | 371 | r = kvm_check_cpuid(vcpu, e2, nent); |
| 372 | if (r) |
| 373 | return r; |
Sean Christopherson | 8b44b17 | 2021-11-05 09:51:00 +0000 | [diff] [blame] | 374 | |
Jing Liu | 5ab2f45 | 2022-01-05 04:35:19 -0800 | [diff] [blame] | 375 | kvfree(vcpu->arch.cpuid_entries); |
| 376 | vcpu->arch.cpuid_entries = e2; |
| 377 | vcpu->arch.cpuid_nent = nent; |
Sean Christopherson | 8b44b17 | 2021-11-05 09:51:00 +0000 | [diff] [blame] | 378 | |
Jing Liu | 5ab2f45 | 2022-01-05 04:35:19 -0800 | [diff] [blame] | 379 | kvm_update_kvm_cpuid_base(vcpu); |
Jing Liu | 5ab2f45 | 2022-01-05 04:35:19 -0800 | [diff] [blame] | 380 | kvm_vcpu_after_set_cpuid(vcpu); |
Sean Christopherson | 8b44b17 | 2021-11-05 09:51:00 +0000 | [diff] [blame] | 381 | |
Jing Liu | 5ab2f45 | 2022-01-05 04:35:19 -0800 | [diff] [blame] | 382 | return 0; |
Sean Christopherson | 8b44b17 | 2021-11-05 09:51:00 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 385 | /* when an old userspace process fills a new kernel module */ |
| 386 | int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu, |
| 387 | struct kvm_cpuid *cpuid, |
| 388 | struct kvm_cpuid_entry __user *entries) |
| 389 | { |
| 390 | int r, i; |
Vitaly Kuznetsov | 255cbec | 2020-10-01 15:05:40 +0200 | [diff] [blame] | 391 | struct kvm_cpuid_entry *e = NULL; |
| 392 | struct kvm_cpuid_entry2 *e2 = NULL; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 393 | |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 394 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) |
Vitaly Kuznetsov | 255cbec | 2020-10-01 15:05:40 +0200 | [diff] [blame] | 395 | return -E2BIG; |
| 396 | |
Paolo Bonzini | 83676e9 | 2016-06-01 14:09:19 +0200 | [diff] [blame] | 397 | if (cpuid->nent) { |
Vitaly Kuznetsov | 255cbec | 2020-10-01 15:05:40 +0200 | [diff] [blame] | 398 | e = vmemdup_user(entries, array_size(sizeof(*e), cpuid->nent)); |
| 399 | if (IS_ERR(e)) |
| 400 | return PTR_ERR(e); |
| 401 | |
| 402 | e2 = kvmalloc_array(cpuid->nent, sizeof(*e2), GFP_KERNEL_ACCOUNT); |
| 403 | if (!e2) { |
| 404 | r = -ENOMEM; |
| 405 | goto out_free_cpuid; |
Denis Efremov | 7ec28e2 | 2020-06-03 13:11:31 +0300 | [diff] [blame] | 406 | } |
Paolo Bonzini | 83676e9 | 2016-06-01 14:09:19 +0200 | [diff] [blame] | 407 | } |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 408 | for (i = 0; i < cpuid->nent; i++) { |
Vitaly Kuznetsov | 255cbec | 2020-10-01 15:05:40 +0200 | [diff] [blame] | 409 | e2[i].function = e[i].function; |
| 410 | e2[i].eax = e[i].eax; |
| 411 | e2[i].ebx = e[i].ebx; |
| 412 | e2[i].ecx = e[i].ecx; |
| 413 | e2[i].edx = e[i].edx; |
| 414 | e2[i].index = 0; |
| 415 | e2[i].flags = 0; |
| 416 | e2[i].padding[0] = 0; |
| 417 | e2[i].padding[1] = 0; |
| 418 | e2[i].padding[2] = 0; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 419 | } |
Vitaly Kuznetsov | 255cbec | 2020-10-01 15:05:40 +0200 | [diff] [blame] | 420 | |
Sean Christopherson | 8b44b17 | 2021-11-05 09:51:00 +0000 | [diff] [blame] | 421 | r = kvm_set_cpuid(vcpu, e2, cpuid->nent); |
| 422 | if (r) |
Vitaly Kuznetsov | 255cbec | 2020-10-01 15:05:40 +0200 | [diff] [blame] | 423 | kvfree(e2); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 424 | |
Vitaly Kuznetsov | 255cbec | 2020-10-01 15:05:40 +0200 | [diff] [blame] | 425 | out_free_cpuid: |
| 426 | kvfree(e); |
| 427 | |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 428 | return r; |
| 429 | } |
| 430 | |
| 431 | int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu, |
| 432 | struct kvm_cpuid2 *cpuid, |
| 433 | struct kvm_cpuid_entry2 __user *entries) |
| 434 | { |
Vitaly Kuznetsov | 255cbec | 2020-10-01 15:05:40 +0200 | [diff] [blame] | 435 | struct kvm_cpuid_entry2 *e2 = NULL; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 436 | int r; |
| 437 | |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 438 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) |
Vitaly Kuznetsov | 255cbec | 2020-10-01 15:05:40 +0200 | [diff] [blame] | 439 | return -E2BIG; |
| 440 | |
| 441 | if (cpuid->nent) { |
| 442 | e2 = vmemdup_user(entries, array_size(sizeof(*e2), cpuid->nent)); |
| 443 | if (IS_ERR(e2)) |
| 444 | return PTR_ERR(e2); |
Xiaoyao Li | a76733a | 2020-07-09 12:34:22 +0800 | [diff] [blame] | 445 | } |
| 446 | |
Sean Christopherson | 8b44b17 | 2021-11-05 09:51:00 +0000 | [diff] [blame] | 447 | r = kvm_set_cpuid(vcpu, e2, cpuid->nent); |
| 448 | if (r) |
Vitaly Kuznetsov | 255cbec | 2020-10-01 15:05:40 +0200 | [diff] [blame] | 449 | kvfree(e2); |
Vitaly Kuznetsov | 255cbec | 2020-10-01 15:05:40 +0200 | [diff] [blame] | 450 | |
Sean Christopherson | 8b44b17 | 2021-11-05 09:51:00 +0000 | [diff] [blame] | 451 | return r; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu, |
| 455 | struct kvm_cpuid2 *cpuid, |
| 456 | struct kvm_cpuid_entry2 __user *entries) |
| 457 | { |
| 458 | int r; |
| 459 | |
| 460 | r = -E2BIG; |
| 461 | if (cpuid->nent < vcpu->arch.cpuid_nent) |
| 462 | goto out; |
| 463 | r = -EFAULT; |
Michael Roth | 181f494 | 2021-01-27 20:44:51 -0600 | [diff] [blame] | 464 | if (copy_to_user(entries, vcpu->arch.cpuid_entries, |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 465 | vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2))) |
| 466 | goto out; |
| 467 | return 0; |
| 468 | |
| 469 | out: |
| 470 | cpuid->nent = vcpu->arch.cpuid_nent; |
| 471 | return r; |
| 472 | } |
| 473 | |
Sean Christopherson | 4e66c0c | 2021-04-12 16:21:35 +1200 | [diff] [blame] | 474 | /* Mask kvm_cpu_caps for @leaf with the raw CPUID capabilities of this CPU. */ |
Sean Christopherson | 462f8dd | 2021-04-20 18:08:50 -0700 | [diff] [blame] | 475 | static __always_inline void __kvm_cpu_cap_mask(unsigned int leaf) |
Sean Christopherson | 66a6950 | 2020-03-02 15:56:41 -0800 | [diff] [blame] | 476 | { |
Sean Christopherson | d8577a4 | 2020-03-02 15:56:52 -0800 | [diff] [blame] | 477 | const struct cpuid_reg cpuid = x86_feature_cpuid(leaf * 32); |
| 478 | struct kvm_cpuid_entry2 entry; |
| 479 | |
Sean Christopherson | 66a6950 | 2020-03-02 15:56:41 -0800 | [diff] [blame] | 480 | reverse_cpuid_check(leaf); |
Sean Christopherson | d8577a4 | 2020-03-02 15:56:52 -0800 | [diff] [blame] | 481 | |
| 482 | cpuid_count(cpuid.function, cpuid.index, |
| 483 | &entry.eax, &entry.ebx, &entry.ecx, &entry.edx); |
| 484 | |
Sean Christopherson | 855c7e9 | 2020-03-25 12:12:59 -0700 | [diff] [blame] | 485 | kvm_cpu_caps[leaf] &= *__cpuid_entry_get_reg(&entry, cpuid.reg); |
Sean Christopherson | 66a6950 | 2020-03-02 15:56:41 -0800 | [diff] [blame] | 486 | } |
| 487 | |
Sean Christopherson | 462f8dd | 2021-04-20 18:08:50 -0700 | [diff] [blame] | 488 | static __always_inline |
| 489 | void kvm_cpu_cap_init_scattered(enum kvm_only_cpuid_leafs leaf, u32 mask) |
Sean Christopherson | 4e66c0c | 2021-04-12 16:21:35 +1200 | [diff] [blame] | 490 | { |
| 491 | /* Use kvm_cpu_cap_mask for non-scattered leafs. */ |
| 492 | BUILD_BUG_ON(leaf < NCAPINTS); |
| 493 | |
| 494 | kvm_cpu_caps[leaf] = mask; |
| 495 | |
| 496 | __kvm_cpu_cap_mask(leaf); |
| 497 | } |
| 498 | |
| 499 | static __always_inline void kvm_cpu_cap_mask(enum cpuid_leafs leaf, u32 mask) |
| 500 | { |
| 501 | /* Use kvm_cpu_cap_init_scattered for scattered leafs. */ |
| 502 | BUILD_BUG_ON(leaf >= NCAPINTS); |
| 503 | |
| 504 | kvm_cpu_caps[leaf] &= mask; |
| 505 | |
| 506 | __kvm_cpu_cap_mask(leaf); |
| 507 | } |
| 508 | |
Sean Christopherson | 66a6950 | 2020-03-02 15:56:41 -0800 | [diff] [blame] | 509 | void kvm_set_cpu_caps(void) |
| 510 | { |
Sean Christopherson | 66a6950 | 2020-03-02 15:56:41 -0800 | [diff] [blame] | 511 | #ifdef CONFIG_X86_64 |
| 512 | unsigned int f_gbpages = F(GBPAGES); |
| 513 | unsigned int f_lm = F(LM); |
Jing Liu | 690a757 | 2022-01-05 04:35:27 -0800 | [diff] [blame] | 514 | unsigned int f_xfd = F(XFD); |
Sean Christopherson | 66a6950 | 2020-03-02 15:56:41 -0800 | [diff] [blame] | 515 | #else |
| 516 | unsigned int f_gbpages = 0; |
| 517 | unsigned int f_lm = 0; |
Jing Liu | 690a757 | 2022-01-05 04:35:27 -0800 | [diff] [blame] | 518 | unsigned int f_xfd = 0; |
Sean Christopherson | 66a6950 | 2020-03-02 15:56:41 -0800 | [diff] [blame] | 519 | #endif |
Sean Christopherson | 4e66c0c | 2021-04-12 16:21:35 +1200 | [diff] [blame] | 520 | memset(kvm_cpu_caps, 0, sizeof(kvm_cpu_caps)); |
Sean Christopherson | 66a6950 | 2020-03-02 15:56:41 -0800 | [diff] [blame] | 521 | |
Sean Christopherson | 4e66c0c | 2021-04-12 16:21:35 +1200 | [diff] [blame] | 522 | BUILD_BUG_ON(sizeof(kvm_cpu_caps) - (NKVMCAPINTS * sizeof(*kvm_cpu_caps)) > |
Sean Christopherson | 66a6950 | 2020-03-02 15:56:41 -0800 | [diff] [blame] | 523 | sizeof(boot_cpu_data.x86_capability)); |
| 524 | |
| 525 | memcpy(&kvm_cpu_caps, &boot_cpu_data.x86_capability, |
Sean Christopherson | 4e66c0c | 2021-04-12 16:21:35 +1200 | [diff] [blame] | 526 | sizeof(kvm_cpu_caps) - (NKVMCAPINTS * sizeof(*kvm_cpu_caps))); |
Sean Christopherson | 66a6950 | 2020-03-02 15:56:41 -0800 | [diff] [blame] | 527 | |
| 528 | kvm_cpu_cap_mask(CPUID_1_ECX, |
| 529 | /* |
| 530 | * NOTE: MONITOR (and MWAIT) are emulated as NOP, but *not* |
| 531 | * advertised to guests via CPUID! |
| 532 | */ |
| 533 | F(XMM3) | F(PCLMULQDQ) | 0 /* DTES64, MONITOR */ | |
| 534 | 0 /* DS-CPL, VMX, SMX, EST */ | |
| 535 | 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ | |
Like Xu | 27461da3 | 2020-05-29 15:43:45 +0800 | [diff] [blame] | 536 | F(FMA) | F(CX16) | 0 /* xTPR Update */ | F(PDCM) | |
Sean Christopherson | 66a6950 | 2020-03-02 15:56:41 -0800 | [diff] [blame] | 537 | F(PCID) | 0 /* Reserved, DCA */ | F(XMM4_1) | |
| 538 | F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) | |
| 539 | 0 /* Reserved*/ | F(AES) | F(XSAVE) | 0 /* OSXSAVE */ | F(AVX) | |
| 540 | F(F16C) | F(RDRAND) |
| 541 | ); |
Sean Christopherson | 93c380e | 2020-03-02 15:56:54 -0800 | [diff] [blame] | 542 | /* KVM emulates x2apic in software irrespective of host support. */ |
| 543 | kvm_cpu_cap_set(X86_FEATURE_X2APIC); |
Sean Christopherson | 66a6950 | 2020-03-02 15:56:41 -0800 | [diff] [blame] | 544 | |
| 545 | kvm_cpu_cap_mask(CPUID_1_EDX, |
| 546 | F(FPU) | F(VME) | F(DE) | F(PSE) | |
| 547 | F(TSC) | F(MSR) | F(PAE) | F(MCE) | |
| 548 | F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) | |
| 549 | F(MTRR) | F(PGE) | F(MCA) | F(CMOV) | |
| 550 | F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLUSH) | |
| 551 | 0 /* Reserved, DS, ACPI */ | F(MMX) | |
| 552 | F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) | |
| 553 | 0 /* HTT, TM, Reserved, PBE */ |
| 554 | ); |
| 555 | |
| 556 | kvm_cpu_cap_mask(CPUID_7_0_EBX, |
Jim Mattson | e3bcfda | 2022-02-03 16:13:48 -0800 | [diff] [blame] | 557 | F(FSGSBASE) | F(SGX) | F(BMI1) | F(HLE) | F(AVX2) | |
| 558 | F(FDP_EXCPTN_ONLY) | F(SMEP) | F(BMI2) | F(ERMS) | F(INVPCID) | |
| 559 | F(RTM) | F(ZERO_FCS_FDS) | 0 /*MPX*/ | F(AVX512F) | |
| 560 | F(AVX512DQ) | F(RDSEED) | F(ADX) | F(SMAP) | F(AVX512IFMA) | |
| 561 | F(CLFLUSHOPT) | F(CLWB) | 0 /*INTEL_PT*/ | F(AVX512PF) | |
| 562 | F(AVX512ER) | F(AVX512CD) | F(SHA_NI) | F(AVX512BW) | |
| 563 | F(AVX512VL)); |
Sean Christopherson | 66a6950 | 2020-03-02 15:56:41 -0800 | [diff] [blame] | 564 | |
| 565 | kvm_cpu_cap_mask(CPUID_7_ECX, |
Babu Moger | fa44b82 | 2020-05-12 18:59:16 -0500 | [diff] [blame] | 566 | F(AVX512VBMI) | F(LA57) | F(PKU) | 0 /*OSPKE*/ | F(RDPID) | |
Sean Christopherson | 66a6950 | 2020-03-02 15:56:41 -0800 | [diff] [blame] | 567 | F(AVX512_VPOPCNTDQ) | F(UMIP) | F(AVX512_VBMI2) | F(GFNI) | |
| 568 | F(VAES) | F(VPCLMULQDQ) | F(AVX512_VNNI) | F(AVX512_BITALG) | |
Sean Christopherson | 72add91 | 2021-04-12 16:21:42 +1200 | [diff] [blame] | 569 | F(CLDEMOTE) | F(MOVDIRI) | F(MOVDIR64B) | 0 /*WAITPKG*/ | |
Paolo Bonzini | 76ea438 | 2021-05-06 06:30:04 -0400 | [diff] [blame] | 570 | F(SGX_LC) | F(BUS_LOCK_DETECT) |
Sean Christopherson | 66a6950 | 2020-03-02 15:56:41 -0800 | [diff] [blame] | 571 | ); |
| 572 | /* Set LA57 based on hardware capability. */ |
| 573 | if (cpuid_ecx(7) & F(LA57)) |
| 574 | kvm_cpu_cap_set(X86_FEATURE_LA57); |
| 575 | |
Babu Moger | fa44b82 | 2020-05-12 18:59:16 -0500 | [diff] [blame] | 576 | /* |
| 577 | * PKU not yet implemented for shadow paging and requires OSPKE |
| 578 | * to be set on the host. Clear it if that is not the case |
| 579 | */ |
| 580 | if (!tdp_enabled || !boot_cpu_has(X86_FEATURE_OSPKE)) |
| 581 | kvm_cpu_cap_clear(X86_FEATURE_PKU); |
| 582 | |
Sean Christopherson | 66a6950 | 2020-03-02 15:56:41 -0800 | [diff] [blame] | 583 | kvm_cpu_cap_mask(CPUID_7_EDX, |
| 584 | F(AVX512_4VNNIW) | F(AVX512_4FMAPS) | F(SPEC_CTRL) | |
| 585 | F(SPEC_CTRL_SSBD) | F(ARCH_CAPABILITIES) | F(INTEL_STIBP) | |
Paolo Bonzini | 43bd9ef | 2020-08-09 13:04:56 -0400 | [diff] [blame] | 586 | F(MD_CLEAR) | F(AVX512_VP2INTERSECT) | F(FSRM) | |
Jing Liu | 690a757 | 2022-01-05 04:35:27 -0800 | [diff] [blame] | 587 | F(SERIALIZE) | F(TSXLDTRK) | F(AVX512_FP16) | |
| 588 | F(AMX_TILE) | F(AMX_INT8) | F(AMX_BF16) |
Sean Christopherson | 66a6950 | 2020-03-02 15:56:41 -0800 | [diff] [blame] | 589 | ); |
| 590 | |
Sean Christopherson | 93c380e | 2020-03-02 15:56:54 -0800 | [diff] [blame] | 591 | /* TSC_ADJUST and ARCH_CAPABILITIES are emulated in software. */ |
| 592 | kvm_cpu_cap_set(X86_FEATURE_TSC_ADJUST); |
| 593 | kvm_cpu_cap_set(X86_FEATURE_ARCH_CAPABILITIES); |
| 594 | |
| 595 | if (boot_cpu_has(X86_FEATURE_IBPB) && boot_cpu_has(X86_FEATURE_IBRS)) |
| 596 | kvm_cpu_cap_set(X86_FEATURE_SPEC_CTRL); |
| 597 | if (boot_cpu_has(X86_FEATURE_STIBP)) |
| 598 | kvm_cpu_cap_set(X86_FEATURE_INTEL_STIBP); |
| 599 | if (boot_cpu_has(X86_FEATURE_AMD_SSBD)) |
| 600 | kvm_cpu_cap_set(X86_FEATURE_SPEC_CTRL_SSBD); |
| 601 | |
Sean Christopherson | 66a6950 | 2020-03-02 15:56:41 -0800 | [diff] [blame] | 602 | kvm_cpu_cap_mask(CPUID_7_1_EAX, |
Yang Zhong | 1085a6b | 2021-01-05 08:49:09 +0800 | [diff] [blame] | 603 | F(AVX_VNNI) | F(AVX512_BF16) |
Sean Christopherson | 66a6950 | 2020-03-02 15:56:41 -0800 | [diff] [blame] | 604 | ); |
| 605 | |
| 606 | kvm_cpu_cap_mask(CPUID_D_1_EAX, |
Jing Liu | 690a757 | 2022-01-05 04:35:27 -0800 | [diff] [blame] | 607 | F(XSAVEOPT) | F(XSAVEC) | F(XGETBV1) | F(XSAVES) | f_xfd |
Sean Christopherson | 66a6950 | 2020-03-02 15:56:41 -0800 | [diff] [blame] | 608 | ); |
| 609 | |
Sean Christopherson | 72add91 | 2021-04-12 16:21:42 +1200 | [diff] [blame] | 610 | kvm_cpu_cap_init_scattered(CPUID_12_EAX, |
| 611 | SF(SGX1) | SF(SGX2) |
| 612 | ); |
| 613 | |
Sean Christopherson | 66a6950 | 2020-03-02 15:56:41 -0800 | [diff] [blame] | 614 | kvm_cpu_cap_mask(CPUID_8000_0001_ECX, |
| 615 | F(LAHF_LM) | F(CMP_LEGACY) | 0 /*SVM*/ | 0 /* ExtApicSpace */ | |
| 616 | F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) | |
| 617 | F(3DNOWPREFETCH) | F(OSVW) | 0 /* IBS */ | F(XOP) | |
| 618 | 0 /* SKINIT, WDT, LWP */ | F(FMA4) | F(TBM) | |
Like Xu | b1d66da | 2021-11-17 16:03:04 +0800 | [diff] [blame] | 619 | F(TOPOEXT) | 0 /* PERFCTR_CORE */ |
Sean Christopherson | 66a6950 | 2020-03-02 15:56:41 -0800 | [diff] [blame] | 620 | ); |
| 621 | |
| 622 | kvm_cpu_cap_mask(CPUID_8000_0001_EDX, |
| 623 | F(FPU) | F(VME) | F(DE) | F(PSE) | |
| 624 | F(TSC) | F(MSR) | F(PAE) | F(MCE) | |
| 625 | F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) | |
| 626 | F(MTRR) | F(PGE) | F(MCA) | F(CMOV) | |
| 627 | F(PAT) | F(PSE36) | 0 /* Reserved */ | |
Sean Christopherson | 1383279 | 2021-08-05 11:38:04 -0700 | [diff] [blame] | 628 | F(NX) | 0 /* Reserved */ | F(MMXEXT) | F(MMX) | |
Sean Christopherson | 66a6950 | 2020-03-02 15:56:41 -0800 | [diff] [blame] | 629 | F(FXSR) | F(FXSR_OPT) | f_gbpages | F(RDTSCP) | |
| 630 | 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW) |
| 631 | ); |
| 632 | |
| 633 | if (!tdp_enabled && IS_ENABLED(CONFIG_X86_64)) |
| 634 | kvm_cpu_cap_set(X86_FEATURE_GBPAGES); |
| 635 | |
| 636 | kvm_cpu_cap_mask(CPUID_8000_0008_EBX, |
| 637 | F(CLZERO) | F(XSAVEERPTR) | |
| 638 | F(WBNOINVD) | F(AMD_IBPB) | F(AMD_IBRS) | F(AMD_SSBD) | F(VIRT_SSBD) | |
Babu Moger | b73a543 | 2021-09-23 20:15:28 -0500 | [diff] [blame] | 639 | F(AMD_SSB_NO) | F(AMD_STIBP) | F(AMD_STIBP_ALWAYS_ON) | |
| 640 | __feature_bit(KVM_X86_FEATURE_PSFD) |
Sean Christopherson | 66a6950 | 2020-03-02 15:56:41 -0800 | [diff] [blame] | 641 | ); |
| 642 | |
Sean Christopherson | 9b58b98 | 2020-03-02 15:56:42 -0800 | [diff] [blame] | 643 | /* |
Sean Christopherson | 93c380e | 2020-03-02 15:56:54 -0800 | [diff] [blame] | 644 | * AMD has separate bits for each SPEC_CTRL bit. |
| 645 | * arch/x86/kernel/cpu/bugs.c is kind enough to |
| 646 | * record that in cpufeatures so use them. |
| 647 | */ |
| 648 | if (boot_cpu_has(X86_FEATURE_IBPB)) |
| 649 | kvm_cpu_cap_set(X86_FEATURE_AMD_IBPB); |
| 650 | if (boot_cpu_has(X86_FEATURE_IBRS)) |
| 651 | kvm_cpu_cap_set(X86_FEATURE_AMD_IBRS); |
| 652 | if (boot_cpu_has(X86_FEATURE_STIBP)) |
| 653 | kvm_cpu_cap_set(X86_FEATURE_AMD_STIBP); |
| 654 | if (boot_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD)) |
| 655 | kvm_cpu_cap_set(X86_FEATURE_AMD_SSBD); |
| 656 | if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS)) |
| 657 | kvm_cpu_cap_set(X86_FEATURE_AMD_SSB_NO); |
| 658 | /* |
| 659 | * The preference is to use SPEC CTRL MSR instead of the |
| 660 | * VIRT_SPEC MSR. |
| 661 | */ |
| 662 | if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) && |
| 663 | !boot_cpu_has(X86_FEATURE_AMD_SSBD)) |
| 664 | kvm_cpu_cap_set(X86_FEATURE_VIRT_SSBD); |
| 665 | |
| 666 | /* |
Sean Christopherson | 9b58b98 | 2020-03-02 15:56:42 -0800 | [diff] [blame] | 667 | * Hide all SVM features by default, SVM will set the cap bits for |
| 668 | * features it emulates and/or exposes for L1. |
| 669 | */ |
| 670 | kvm_cpu_cap_mask(CPUID_8000_000A_EDX, 0); |
| 671 | |
Paolo Bonzini | d9db0fd | 2021-04-21 19:11:15 -0700 | [diff] [blame] | 672 | kvm_cpu_cap_mask(CPUID_8000_001F_EAX, |
| 673 | 0 /* SME */ | F(SEV) | 0 /* VM_PAGE_FLUSH */ | F(SEV_ES) | |
| 674 | F(SME_COHERENT)); |
| 675 | |
Sean Christopherson | 66a6950 | 2020-03-02 15:56:41 -0800 | [diff] [blame] | 676 | kvm_cpu_cap_mask(CPUID_C000_0001_EDX, |
| 677 | F(XSTORE) | F(XSTORE_EN) | F(XCRYPT) | F(XCRYPT_EN) | |
| 678 | F(ACE2) | F(ACE2_EN) | F(PHE) | F(PHE_EN) | |
| 679 | F(PMM) | F(PMM_EN) |
| 680 | ); |
Sean Christopherson | 78bba96 | 2021-05-04 10:17:34 -0700 | [diff] [blame] | 681 | |
| 682 | /* |
| 683 | * Hide RDTSCP and RDPID if either feature is reported as supported but |
| 684 | * probing MSR_TSC_AUX failed. This is purely a sanity check and |
| 685 | * should never happen, but the guest will likely crash if RDTSCP or |
| 686 | * RDPID is misreported, and KVM has botched MSR_TSC_AUX emulation in |
| 687 | * the past. For example, the sanity check may fire if this instance of |
| 688 | * KVM is running as L1 on top of an older, broken KVM. |
| 689 | */ |
| 690 | if (WARN_ON((kvm_cpu_cap_has(X86_FEATURE_RDTSCP) || |
| 691 | kvm_cpu_cap_has(X86_FEATURE_RDPID)) && |
| 692 | !kvm_is_supported_user_return_msr(MSR_TSC_AUX))) { |
| 693 | kvm_cpu_cap_clear(X86_FEATURE_RDTSCP); |
| 694 | kvm_cpu_cap_clear(X86_FEATURE_RDPID); |
| 695 | } |
Sean Christopherson | 66a6950 | 2020-03-02 15:56:41 -0800 | [diff] [blame] | 696 | } |
| 697 | EXPORT_SYMBOL_GPL(kvm_set_cpu_caps); |
| 698 | |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 699 | struct kvm_cpuid_array { |
| 700 | struct kvm_cpuid_entry2 *entries; |
Xiaoyao Li | 65b1891 | 2020-06-04 12:16:36 +0800 | [diff] [blame] | 701 | int maxnent; |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 702 | int nent; |
| 703 | }; |
| 704 | |
| 705 | static struct kvm_cpuid_entry2 *do_host_cpuid(struct kvm_cpuid_array *array, |
Sean Christopherson | aa10a7d | 2020-03-02 15:56:16 -0800 | [diff] [blame] | 706 | u32 function, u32 index) |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 707 | { |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 708 | struct kvm_cpuid_entry2 *entry; |
| 709 | |
| 710 | if (array->nent >= array->maxnent) |
Sean Christopherson | aa10a7d | 2020-03-02 15:56:16 -0800 | [diff] [blame] | 711 | return NULL; |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 712 | |
| 713 | entry = &array->entries[array->nent++]; |
Sean Christopherson | aa10a7d | 2020-03-02 15:56:16 -0800 | [diff] [blame] | 714 | |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 715 | entry->function = function; |
| 716 | entry->index = index; |
Paolo Bonzini | ab8bcf6 | 2019-06-24 10:23:33 +0200 | [diff] [blame] | 717 | entry->flags = 0; |
| 718 | |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 719 | cpuid_count(entry->function, entry->index, |
| 720 | &entry->eax, &entry->ebx, &entry->ecx, &entry->edx); |
Paolo Bonzini | d9aadaf | 2019-07-04 12:20:48 +0200 | [diff] [blame] | 721 | |
| 722 | switch (function) { |
Paolo Bonzini | d9aadaf | 2019-07-04 12:20:48 +0200 | [diff] [blame] | 723 | case 4: |
| 724 | case 7: |
| 725 | case 0xb: |
| 726 | case 0xd: |
Jim Mattson | a06dcd6 | 2019-09-12 09:55:03 -0700 | [diff] [blame] | 727 | case 0xf: |
| 728 | case 0x10: |
| 729 | case 0x12: |
Paolo Bonzini | d9aadaf | 2019-07-04 12:20:48 +0200 | [diff] [blame] | 730 | case 0x14: |
Jim Mattson | a06dcd6 | 2019-09-12 09:55:03 -0700 | [diff] [blame] | 731 | case 0x17: |
| 732 | case 0x18: |
Jing Liu | 690a757 | 2022-01-05 04:35:27 -0800 | [diff] [blame] | 733 | case 0x1d: |
| 734 | case 0x1e: |
Jim Mattson | a06dcd6 | 2019-09-12 09:55:03 -0700 | [diff] [blame] | 735 | case 0x1f: |
Paolo Bonzini | d9aadaf | 2019-07-04 12:20:48 +0200 | [diff] [blame] | 736 | case 0x8000001d: |
| 737 | entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
| 738 | break; |
| 739 | } |
Sean Christopherson | aa10a7d | 2020-03-02 15:56:16 -0800 | [diff] [blame] | 740 | |
| 741 | return entry; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 742 | } |
| 743 | |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 744 | static int __do_cpuid_func_emulated(struct kvm_cpuid_array *array, u32 func) |
Borislav Petkov | 9c15bb1 | 2013-09-22 16:44:50 +0200 | [diff] [blame] | 745 | { |
Sean Christopherson | 7c7f954 | 2020-03-02 15:56:56 -0800 | [diff] [blame] | 746 | struct kvm_cpuid_entry2 *entry; |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 747 | |
Sean Christopherson | 7c7f954 | 2020-03-02 15:56:56 -0800 | [diff] [blame] | 748 | if (array->nent >= array->maxnent) |
| 749 | return -E2BIG; |
| 750 | |
| 751 | entry = &array->entries[array->nent]; |
Paolo Bonzini | ab8bcf6 | 2019-06-24 10:23:33 +0200 | [diff] [blame] | 752 | entry->function = func; |
| 753 | entry->index = 0; |
| 754 | entry->flags = 0; |
| 755 | |
Borislav Petkov | 84cffe4 | 2013-10-29 12:54:56 +0100 | [diff] [blame] | 756 | switch (func) { |
| 757 | case 0: |
Paolo Bonzini | fb6d4d3 | 2016-07-12 11:04:26 +0200 | [diff] [blame] | 758 | entry->eax = 7; |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 759 | ++array->nent; |
Borislav Petkov | 84cffe4 | 2013-10-29 12:54:56 +0100 | [diff] [blame] | 760 | break; |
| 761 | case 1: |
| 762 | entry->ecx = F(MOVBE); |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 763 | ++array->nent; |
Borislav Petkov | 84cffe4 | 2013-10-29 12:54:56 +0100 | [diff] [blame] | 764 | break; |
Paolo Bonzini | fb6d4d3 | 2016-07-12 11:04:26 +0200 | [diff] [blame] | 765 | case 7: |
| 766 | entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
Paolo Bonzini | ab8bcf6 | 2019-06-24 10:23:33 +0200 | [diff] [blame] | 767 | entry->eax = 0; |
Sean Christopherson | 85d0011 | 2021-05-04 10:17:21 -0700 | [diff] [blame] | 768 | if (kvm_cpu_cap_has(X86_FEATURE_RDTSCP)) |
| 769 | entry->ecx = F(RDPID); |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 770 | ++array->nent; |
Gustavo A. R. Silva | 551912d | 2021-05-28 15:07:56 -0500 | [diff] [blame] | 771 | break; |
Borislav Petkov | 84cffe4 | 2013-10-29 12:54:56 +0100 | [diff] [blame] | 772 | default: |
| 773 | break; |
| 774 | } |
| 775 | |
Borislav Petkov | 9c15bb1 | 2013-09-22 16:44:50 +0200 | [diff] [blame] | 776 | return 0; |
| 777 | } |
| 778 | |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 779 | static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function) |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 780 | { |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 781 | struct kvm_cpuid_entry2 *entry; |
Sean Christopherson | 74fa0bc | 2020-03-02 15:56:17 -0800 | [diff] [blame] | 782 | int r, i, max_idx; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 783 | |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 784 | /* all calls to cpuid_count() should be made on the same cpu */ |
| 785 | get_cpu(); |
Sasha Levin | 831bf66 | 2011-11-28 11:20:29 +0200 | [diff] [blame] | 786 | |
| 787 | r = -E2BIG; |
| 788 | |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 789 | entry = do_host_cpuid(array, function, 0); |
Sean Christopherson | 7c7f954 | 2020-03-02 15:56:56 -0800 | [diff] [blame] | 790 | if (!entry) |
Sasha Levin | 831bf66 | 2011-11-28 11:20:29 +0200 | [diff] [blame] | 791 | goto out; |
| 792 | |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 793 | switch (function) { |
| 794 | case 0: |
Like Xu | a87f2d3 | 2019-06-06 09:18:45 +0800 | [diff] [blame] | 795 | /* Limited to the highest leaf implemented in KVM. */ |
| 796 | entry->eax = min(entry->eax, 0x1fU); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 797 | break; |
| 798 | case 1: |
Sean Christopherson | bd79199 | 2020-03-02 15:56:53 -0800 | [diff] [blame] | 799 | cpuid_entry_override(entry, CPUID_1_EDX); |
| 800 | cpuid_entry_override(entry, CPUID_1_ECX); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 801 | break; |
Sean Christopherson | 74fa0bc | 2020-03-02 15:56:17 -0800 | [diff] [blame] | 802 | case 2: |
Sean Christopherson | c571a14 | 2020-03-02 15:56:50 -0800 | [diff] [blame] | 803 | /* |
| 804 | * On ancient CPUs, function 2 entries are STATEFUL. That is, |
| 805 | * CPUID(function=2, index=0) may return different results each |
| 806 | * time, with the least-significant byte in EAX enumerating the |
| 807 | * number of times software should do CPUID(2, 0). |
| 808 | * |
Sean Christopherson | 7ff6c03 | 2020-03-02 15:56:51 -0800 | [diff] [blame] | 809 | * Modern CPUs, i.e. every CPU KVM has *ever* run on are less |
| 810 | * idiotic. Intel's SDM states that EAX & 0xff "will always |
| 811 | * return 01H. Software should ignore this value and not |
Sean Christopherson | c571a14 | 2020-03-02 15:56:50 -0800 | [diff] [blame] | 812 | * interpret it as an informational descriptor", while AMD's |
| 813 | * APM states that CPUID(2) is reserved. |
Sean Christopherson | 7ff6c03 | 2020-03-02 15:56:51 -0800 | [diff] [blame] | 814 | * |
| 815 | * WARN if a frankenstein CPU that supports virtualization and |
| 816 | * a stateful CPUID.0x2 is encountered. |
Sean Christopherson | c571a14 | 2020-03-02 15:56:50 -0800 | [diff] [blame] | 817 | */ |
Sean Christopherson | 7ff6c03 | 2020-03-02 15:56:51 -0800 | [diff] [blame] | 818 | WARN_ON_ONCE((entry->eax & 0xff) > 1); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 819 | break; |
Jim Mattson | 32a243d | 2019-03-27 13:15:36 -0700 | [diff] [blame] | 820 | /* functions 4 and 0x8000001d have additional index. */ |
| 821 | case 4: |
Sean Christopherson | c862903 | 2020-03-02 15:56:18 -0800 | [diff] [blame] | 822 | case 0x8000001d: |
| 823 | /* |
| 824 | * Read entries until the cache type in the previous entry is |
| 825 | * zero, i.e. indicates an invalid entry. |
| 826 | */ |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 827 | for (i = 1; entry->eax & 0x1f; ++i) { |
| 828 | entry = do_host_cpuid(array, function, i); |
| 829 | if (!entry) |
Sean Christopherson | 0fc6267 | 2020-03-02 15:56:08 -0800 | [diff] [blame] | 830 | goto out; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 831 | } |
| 832 | break; |
Jan Kiszka | e453aa0 | 2015-05-24 17:22:38 +0200 | [diff] [blame] | 833 | case 6: /* Thermal management */ |
| 834 | entry->eax = 0x4; /* allow ARAT */ |
| 835 | entry->ebx = 0; |
| 836 | entry->ecx = 0; |
| 837 | entry->edx = 0; |
| 838 | break; |
Paolo Bonzini | 54d360d | 2019-07-04 12:18:13 +0200 | [diff] [blame] | 839 | /* function 7 has additional index. */ |
Sean Christopherson | 74fa0bc | 2020-03-02 15:56:17 -0800 | [diff] [blame] | 840 | case 7: |
Sean Christopherson | 09f628a | 2020-03-02 15:56:48 -0800 | [diff] [blame] | 841 | entry->eax = min(entry->eax, 1u); |
Sean Christopherson | bd79199 | 2020-03-02 15:56:53 -0800 | [diff] [blame] | 842 | cpuid_entry_override(entry, CPUID_7_0_EBX); |
| 843 | cpuid_entry_override(entry, CPUID_7_ECX); |
| 844 | cpuid_entry_override(entry, CPUID_7_EDX); |
Sean Christopherson | 09f628a | 2020-03-02 15:56:48 -0800 | [diff] [blame] | 845 | |
Sean Christopherson | bcf600c | 2020-03-02 15:56:49 -0800 | [diff] [blame] | 846 | /* KVM only supports 0x7.0 and 0x7.1, capped above via min(). */ |
| 847 | if (entry->eax == 1) { |
| 848 | entry = do_host_cpuid(array, function, 1); |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 849 | if (!entry) |
Paolo Bonzini | 54d360d | 2019-07-04 12:18:13 +0200 | [diff] [blame] | 850 | goto out; |
| 851 | |
Sean Christopherson | bd79199 | 2020-03-02 15:56:53 -0800 | [diff] [blame] | 852 | cpuid_entry_override(entry, CPUID_7_1_EAX); |
Sean Christopherson | 09f628a | 2020-03-02 15:56:48 -0800 | [diff] [blame] | 853 | entry->ebx = 0; |
| 854 | entry->ecx = 0; |
| 855 | entry->edx = 0; |
Paolo Bonzini | 54d360d | 2019-07-04 12:18:13 +0200 | [diff] [blame] | 856 | } |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 857 | break; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 858 | case 9: |
| 859 | break; |
Gleb Natapov | a6c06ed | 2011-11-10 14:57:28 +0200 | [diff] [blame] | 860 | case 0xa: { /* Architectural Performance Monitoring */ |
| 861 | struct x86_pmu_capability cap; |
| 862 | union cpuid10_eax eax; |
| 863 | union cpuid10_edx edx; |
| 864 | |
| 865 | perf_get_x86_pmu_capability(&cap); |
| 866 | |
| 867 | /* |
Like Xu | 4732f24 | 2022-01-11 15:38:23 +0800 | [diff] [blame] | 868 | * The guest architecture pmu is only supported if the architecture |
| 869 | * pmu exists on the host and the module parameters allow it. |
Gleb Natapov | a6c06ed | 2011-11-10 14:57:28 +0200 | [diff] [blame] | 870 | */ |
Like Xu | 4732f24 | 2022-01-11 15:38:23 +0800 | [diff] [blame] | 871 | if (!cap.version || !enable_pmu) |
Gleb Natapov | a6c06ed | 2011-11-10 14:57:28 +0200 | [diff] [blame] | 872 | memset(&cap, 0, sizeof(cap)); |
| 873 | |
| 874 | eax.split.version_id = min(cap.version, 2); |
| 875 | eax.split.num_counters = cap.num_counters_gp; |
| 876 | eax.split.bit_width = cap.bit_width_gp; |
| 877 | eax.split.mask_length = cap.events_mask_len; |
| 878 | |
Like Xu | 2e8cd7a | 2020-06-24 09:59:28 +0800 | [diff] [blame] | 879 | edx.split.num_counters_fixed = min(cap.num_counters_fixed, MAX_FIXED_COUNTERS); |
Gleb Natapov | a6c06ed | 2011-11-10 14:57:28 +0200 | [diff] [blame] | 880 | edx.split.bit_width_fixed = cap.bit_width_fixed; |
Like Xu | 7234c36 | 2021-06-28 15:43:54 +0800 | [diff] [blame] | 881 | if (cap.version) |
| 882 | edx.split.anythread_deprecated = 1; |
Stephane Eranian | cadbaa0 | 2020-10-28 12:42:47 -0700 | [diff] [blame] | 883 | edx.split.reserved1 = 0; |
| 884 | edx.split.reserved2 = 0; |
Gleb Natapov | a6c06ed | 2011-11-10 14:57:28 +0200 | [diff] [blame] | 885 | |
| 886 | entry->eax = eax.full; |
| 887 | entry->ebx = cap.events_mask; |
| 888 | entry->ecx = 0; |
| 889 | entry->edx = edx.full; |
| 890 | break; |
| 891 | } |
Like Xu | a87f2d3 | 2019-06-06 09:18:45 +0800 | [diff] [blame] | 892 | /* |
| 893 | * Per Intel's SDM, the 0x1f is a superset of 0xb, |
| 894 | * thus they can be handled by common code. |
| 895 | */ |
| 896 | case 0x1f: |
Sean Christopherson | 74fa0bc | 2020-03-02 15:56:17 -0800 | [diff] [blame] | 897 | case 0xb: |
Jim Mattson | a1a640b | 2019-09-25 11:17:14 -0700 | [diff] [blame] | 898 | /* |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 899 | * Populate entries until the level type (ECX[15:8]) of the |
| 900 | * previous entry is zero. Note, CPUID EAX.{0x1f,0xb}.0 is |
| 901 | * the starting entry, filled by the primary do_host_cpuid(). |
Jim Mattson | a1a640b | 2019-09-25 11:17:14 -0700 | [diff] [blame] | 902 | */ |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 903 | for (i = 1; entry->ecx & 0xff00; ++i) { |
| 904 | entry = do_host_cpuid(array, function, i); |
| 905 | if (!entry) |
Sasha Levin | 831bf66 | 2011-11-28 11:20:29 +0200 | [diff] [blame] | 906 | goto out; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 907 | } |
| 908 | break; |
Jing Liu | 445ecdf | 2022-01-05 04:35:15 -0800 | [diff] [blame] | 909 | case 0xd: { |
Like Xu | 1ffce09 | 2022-01-25 19:52:23 +0800 | [diff] [blame] | 910 | u64 permitted_xcr0 = supported_xcr0 & xstate_get_guest_group_perm(); |
| 911 | u64 permitted_xss = supported_xss; |
Jing Liu | 445ecdf | 2022-01-05 04:35:15 -0800 | [diff] [blame] | 912 | |
Like Xu | 1ffce09 | 2022-01-25 19:52:23 +0800 | [diff] [blame] | 913 | entry->eax &= permitted_xcr0; |
| 914 | entry->ebx = xstate_required_size(permitted_xcr0, false); |
Radim Krčmář | e08e833 | 2014-12-04 18:30:41 +0100 | [diff] [blame] | 915 | entry->ecx = entry->ebx; |
Like Xu | 1ffce09 | 2022-01-25 19:52:23 +0800 | [diff] [blame] | 916 | entry->edx &= permitted_xcr0 >> 32; |
| 917 | if (!permitted_xcr0) |
Paolo Bonzini | b65d6e1 | 2014-11-21 18:13:26 +0100 | [diff] [blame] | 918 | break; |
| 919 | |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 920 | entry = do_host_cpuid(array, function, 1); |
| 921 | if (!entry) |
Sean Christopherson | 3dc4a9c | 2020-03-02 15:56:09 -0800 | [diff] [blame] | 922 | goto out; |
| 923 | |
Sean Christopherson | bd79199 | 2020-03-02 15:56:53 -0800 | [diff] [blame] | 924 | cpuid_entry_override(entry, CPUID_D_1_EAX); |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 925 | if (entry->eax & (F(XSAVES)|F(XSAVEC))) |
Like Xu | 1ffce09 | 2022-01-25 19:52:23 +0800 | [diff] [blame] | 926 | entry->ebx = xstate_required_size(permitted_xcr0 | permitted_xss, |
Paolo Bonzini | 408e9a3 | 2020-03-05 16:11:56 +0100 | [diff] [blame] | 927 | true); |
| 928 | else { |
Like Xu | 1ffce09 | 2022-01-25 19:52:23 +0800 | [diff] [blame] | 929 | WARN_ON_ONCE(permitted_xss != 0); |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 930 | entry->ebx = 0; |
Paolo Bonzini | 408e9a3 | 2020-03-05 16:11:56 +0100 | [diff] [blame] | 931 | } |
Like Xu | 1ffce09 | 2022-01-25 19:52:23 +0800 | [diff] [blame] | 932 | entry->ecx &= permitted_xss; |
| 933 | entry->edx &= permitted_xss >> 32; |
Sean Christopherson | 3dc4a9c | 2020-03-02 15:56:09 -0800 | [diff] [blame] | 934 | |
Sean Christopherson | 0eee8f9 | 2020-03-02 15:56:21 -0800 | [diff] [blame] | 935 | for (i = 2; i < 64; ++i) { |
Paolo Bonzini | 408e9a3 | 2020-03-05 16:11:56 +0100 | [diff] [blame] | 936 | bool s_state; |
Like Xu | 1ffce09 | 2022-01-25 19:52:23 +0800 | [diff] [blame] | 937 | if (permitted_xcr0 & BIT_ULL(i)) |
Paolo Bonzini | 408e9a3 | 2020-03-05 16:11:56 +0100 | [diff] [blame] | 938 | s_state = false; |
Like Xu | 1ffce09 | 2022-01-25 19:52:23 +0800 | [diff] [blame] | 939 | else if (permitted_xss & BIT_ULL(i)) |
Paolo Bonzini | 408e9a3 | 2020-03-05 16:11:56 +0100 | [diff] [blame] | 940 | s_state = true; |
| 941 | else |
Sean Christopherson | 1893c94 | 2020-03-02 15:56:10 -0800 | [diff] [blame] | 942 | continue; |
Sean Christopherson | 3dc4a9c | 2020-03-02 15:56:09 -0800 | [diff] [blame] | 943 | |
Sean Christopherson | 0eee8f9 | 2020-03-02 15:56:21 -0800 | [diff] [blame] | 944 | entry = do_host_cpuid(array, function, i); |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 945 | if (!entry) |
Sasha Levin | 831bf66 | 2011-11-28 11:20:29 +0200 | [diff] [blame] | 946 | goto out; |
| 947 | |
Sean Christopherson | 91001d4 | 2020-03-02 15:56:11 -0800 | [diff] [blame] | 948 | /* |
Sean Christopherson | cfc4818 | 2020-03-02 15:56:23 -0800 | [diff] [blame] | 949 | * The supported check above should have filtered out |
Paolo Bonzini | 408e9a3 | 2020-03-05 16:11:56 +0100 | [diff] [blame] | 950 | * invalid sub-leafs. Only valid sub-leafs should |
Sean Christopherson | 91001d4 | 2020-03-02 15:56:11 -0800 | [diff] [blame] | 951 | * reach this point, and they should have a non-zero |
Paolo Bonzini | 408e9a3 | 2020-03-05 16:11:56 +0100 | [diff] [blame] | 952 | * save state size. Furthermore, check whether the |
Like Xu | 1ffce09 | 2022-01-25 19:52:23 +0800 | [diff] [blame] | 953 | * processor agrees with permitted_xcr0/permitted_xss |
Paolo Bonzini | 408e9a3 | 2020-03-05 16:11:56 +0100 | [diff] [blame] | 954 | * on whether this is an XCR0- or IA32_XSS-managed area. |
Sean Christopherson | 91001d4 | 2020-03-02 15:56:11 -0800 | [diff] [blame] | 955 | */ |
Paolo Bonzini | 408e9a3 | 2020-03-05 16:11:56 +0100 | [diff] [blame] | 956 | if (WARN_ON_ONCE(!entry->eax || (entry->ecx & 0x1) != s_state)) { |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 957 | --array->nent; |
Sean Christopherson | 3dc4a9c | 2020-03-02 15:56:09 -0800 | [diff] [blame] | 958 | continue; |
Sean Christopherson | 8b2fc44 | 2020-03-02 15:56:12 -0800 | [diff] [blame] | 959 | } |
Like Xu | e973746 | 2022-01-17 15:45:31 +0800 | [diff] [blame] | 960 | |
| 961 | if (!kvm_cpu_cap_has(X86_FEATURE_XFD)) |
| 962 | entry->ecx &= ~BIT_ULL(2); |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 963 | entry->edx = 0; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 964 | } |
| 965 | break; |
Jing Liu | 445ecdf | 2022-01-05 04:35:15 -0800 | [diff] [blame] | 966 | } |
Sean Christopherson | 72add91 | 2021-04-12 16:21:42 +1200 | [diff] [blame] | 967 | case 0x12: |
| 968 | /* Intel SGX */ |
| 969 | if (!kvm_cpu_cap_has(X86_FEATURE_SGX)) { |
| 970 | entry->eax = entry->ebx = entry->ecx = entry->edx = 0; |
| 971 | break; |
| 972 | } |
| 973 | |
| 974 | /* |
| 975 | * Index 0: Sub-features, MISCSELECT (a.k.a extended features) |
| 976 | * and max enclave sizes. The SGX sub-features and MISCSELECT |
| 977 | * are restricted by kernel and KVM capabilities (like most |
| 978 | * feature flags), while enclave size is unrestricted. |
| 979 | */ |
| 980 | cpuid_entry_override(entry, CPUID_12_EAX); |
| 981 | entry->ebx &= SGX_MISC_EXINFO; |
| 982 | |
| 983 | entry = do_host_cpuid(array, function, 1); |
| 984 | if (!entry) |
| 985 | goto out; |
| 986 | |
| 987 | /* |
| 988 | * Index 1: SECS.ATTRIBUTES. ATTRIBUTES are restricted a la |
| 989 | * feature flags. Advertise all supported flags, including |
| 990 | * privileged attributes that require explicit opt-in from |
| 991 | * userspace. ATTRIBUTES.XFRM is not adjusted as userspace is |
| 992 | * expected to derive it from supported XCR0. |
| 993 | */ |
| 994 | entry->eax &= SGX_ATTR_DEBUG | SGX_ATTR_MODE64BIT | |
Sean Christopherson | fe7e948 | 2021-04-12 16:21:43 +1200 | [diff] [blame] | 995 | SGX_ATTR_PROVISIONKEY | SGX_ATTR_EINITTOKENKEY | |
Sean Christopherson | 72add91 | 2021-04-12 16:21:42 +1200 | [diff] [blame] | 996 | SGX_ATTR_KSS; |
| 997 | entry->ebx &= 0; |
| 998 | break; |
Chao Peng | 86f5201 | 2018-10-24 16:05:11 +0800 | [diff] [blame] | 999 | /* Intel PT */ |
Sean Christopherson | 74fa0bc | 2020-03-02 15:56:17 -0800 | [diff] [blame] | 1000 | case 0x14: |
Sean Christopherson | dd69cc2 | 2020-03-02 15:56:55 -0800 | [diff] [blame] | 1001 | if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT)) { |
Sean Christopherson | 7392079 | 2020-03-02 15:56:26 -0800 | [diff] [blame] | 1002 | entry->eax = entry->ebx = entry->ecx = entry->edx = 0; |
Chao Peng | 86f5201 | 2018-10-24 16:05:11 +0800 | [diff] [blame] | 1003 | break; |
Sean Christopherson | 7392079 | 2020-03-02 15:56:26 -0800 | [diff] [blame] | 1004 | } |
Chao Peng | 86f5201 | 2018-10-24 16:05:11 +0800 | [diff] [blame] | 1005 | |
Sean Christopherson | 74fa0bc | 2020-03-02 15:56:17 -0800 | [diff] [blame] | 1006 | for (i = 1, max_idx = entry->eax; i <= max_idx; ++i) { |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 1007 | if (!do_host_cpuid(array, function, i)) |
Chao Peng | 86f5201 | 2018-10-24 16:05:11 +0800 | [diff] [blame] | 1008 | goto out; |
Chao Peng | 86f5201 | 2018-10-24 16:05:11 +0800 | [diff] [blame] | 1009 | } |
| 1010 | break; |
Jing Liu | 690a757 | 2022-01-05 04:35:27 -0800 | [diff] [blame] | 1011 | /* Intel AMX TILE */ |
| 1012 | case 0x1d: |
| 1013 | if (!kvm_cpu_cap_has(X86_FEATURE_AMX_TILE)) { |
| 1014 | entry->eax = entry->ebx = entry->ecx = entry->edx = 0; |
| 1015 | break; |
| 1016 | } |
| 1017 | |
| 1018 | for (i = 1, max_idx = entry->eax; i <= max_idx; ++i) { |
| 1019 | if (!do_host_cpuid(array, function, i)) |
| 1020 | goto out; |
| 1021 | } |
| 1022 | break; |
| 1023 | case 0x1e: /* TMUL information */ |
| 1024 | if (!kvm_cpu_cap_has(X86_FEATURE_AMX_TILE)) { |
| 1025 | entry->eax = entry->ebx = entry->ecx = entry->edx = 0; |
| 1026 | break; |
| 1027 | } |
| 1028 | break; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1029 | case KVM_CPUID_SIGNATURE: { |
Paul Durrant | 760849b | 2021-11-05 09:51:01 +0000 | [diff] [blame] | 1030 | const u32 *sigptr = (const u32 *)KVM_SIGNATURE; |
Michael S. Tsirkin | 57c22e5 | 2012-05-02 17:55:56 +0300 | [diff] [blame] | 1031 | entry->eax = KVM_CPUID_FEATURES; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1032 | entry->ebx = sigptr[0]; |
| 1033 | entry->ecx = sigptr[1]; |
| 1034 | entry->edx = sigptr[2]; |
| 1035 | break; |
| 1036 | } |
| 1037 | case KVM_CPUID_FEATURES: |
| 1038 | entry->eax = (1 << KVM_FEATURE_CLOCKSOURCE) | |
| 1039 | (1 << KVM_FEATURE_NOP_IO_DELAY) | |
| 1040 | (1 << KVM_FEATURE_CLOCKSOURCE2) | |
| 1041 | (1 << KVM_FEATURE_ASYNC_PF) | |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 1042 | (1 << KVM_FEATURE_PV_EOI) | |
Srivatsa Vaddagiri | 6aef266 | 2013-08-26 14:18:34 +0530 | [diff] [blame] | 1043 | (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT) | |
Wanpeng Li | f38a7b7 | 2017-12-12 17:33:04 -0800 | [diff] [blame] | 1044 | (1 << KVM_FEATURE_PV_UNHALT) | |
Radim Krčmář | fe2a302 | 2018-02-01 22:16:21 +0100 | [diff] [blame] | 1045 | (1 << KVM_FEATURE_PV_TLB_FLUSH) | |
Wanpeng Li | 4180bf1 | 2018-07-23 14:39:54 +0800 | [diff] [blame] | 1046 | (1 << KVM_FEATURE_ASYNC_PF_VMEXIT) | |
Marcelo Tosatti | 2d5ba19 | 2019-06-03 19:52:44 -0300 | [diff] [blame] | 1047 | (1 << KVM_FEATURE_PV_SEND_IPI) | |
Wanpeng Li | 32b72ec | 2019-06-11 20:23:50 +0800 | [diff] [blame] | 1048 | (1 << KVM_FEATURE_POLL_CONTROL) | |
Vitaly Kuznetsov | 72de5fa4c | 2020-05-25 16:41:22 +0200 | [diff] [blame] | 1049 | (1 << KVM_FEATURE_PV_SCHED_YIELD) | |
| 1050 | (1 << KVM_FEATURE_ASYNC_PF_INT); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1051 | |
| 1052 | if (sched_info_on()) |
| 1053 | entry->eax |= (1 << KVM_FEATURE_STEAL_TIME); |
| 1054 | |
| 1055 | entry->ebx = 0; |
| 1056 | entry->ecx = 0; |
| 1057 | entry->edx = 0; |
| 1058 | break; |
| 1059 | case 0x80000000: |
Brijesh Singh | 8765d75 | 2017-12-04 10:57:25 -0600 | [diff] [blame] | 1060 | entry->eax = min(entry->eax, 0x8000001f); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1061 | break; |
| 1062 | case 0x80000001: |
Sean Christopherson | bd79199 | 2020-03-02 15:56:53 -0800 | [diff] [blame] | 1063 | cpuid_entry_override(entry, CPUID_8000_0001_EDX); |
| 1064 | cpuid_entry_override(entry, CPUID_8000_0001_ECX); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1065 | break; |
Eric Northup | 43d05de | 2020-04-14 18:23:20 -0700 | [diff] [blame] | 1066 | case 0x80000006: |
| 1067 | /* L2 cache and TLB: pass through host info. */ |
| 1068 | break; |
Marcelo Tosatti | e4c9a5a1 | 2014-04-26 22:30:23 -0300 | [diff] [blame] | 1069 | case 0x80000007: /* Advanced power management */ |
| 1070 | /* invariant TSC is CPUID.80000007H:EDX[8] */ |
| 1071 | entry->edx &= (1 << 8); |
| 1072 | /* mask against host */ |
| 1073 | entry->edx &= boot_cpu_data.x86_power; |
| 1074 | entry->eax = entry->ebx = entry->ecx = 0; |
| 1075 | break; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1076 | case 0x80000008: { |
| 1077 | unsigned g_phys_as = (entry->eax >> 16) & 0xff; |
| 1078 | unsigned virt_as = max((entry->eax >> 8) & 0xff, 48U); |
| 1079 | unsigned phys_as = entry->eax & 0xff; |
| 1080 | |
Sean Christopherson | 4bf48e3 | 2021-06-23 16:05:46 -0700 | [diff] [blame] | 1081 | /* |
Sean Christopherson | e39f00f | 2021-06-23 16:05:47 -0700 | [diff] [blame] | 1082 | * If TDP (NPT) is disabled use the adjusted host MAXPHYADDR as |
| 1083 | * the guest operates in the same PA space as the host, i.e. |
| 1084 | * reductions in MAXPHYADDR for memory encryption affect shadow |
| 1085 | * paging, too. |
| 1086 | * |
| 1087 | * If TDP is enabled but an explicit guest MAXPHYADDR is not |
| 1088 | * provided, use the raw bare metal MAXPHYADDR as reductions to |
| 1089 | * the HPAs do not affect GPAs. |
Sean Christopherson | 4bf48e3 | 2021-06-23 16:05:46 -0700 | [diff] [blame] | 1090 | */ |
Sean Christopherson | e39f00f | 2021-06-23 16:05:47 -0700 | [diff] [blame] | 1091 | if (!tdp_enabled) |
| 1092 | g_phys_as = boot_cpu_data.x86_phys_bits; |
| 1093 | else if (!g_phys_as) |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1094 | g_phys_as = phys_as; |
Sean Christopherson | 4bf48e3 | 2021-06-23 16:05:46 -0700 | [diff] [blame] | 1095 | |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1096 | entry->eax = g_phys_as | (virt_as << 8); |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 1097 | entry->edx = 0; |
Sean Christopherson | bd79199 | 2020-03-02 15:56:53 -0800 | [diff] [blame] | 1098 | cpuid_entry_override(entry, CPUID_8000_0008_EBX); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1099 | break; |
| 1100 | } |
Sean Christopherson | 2570387 | 2020-03-02 15:57:09 -0800 | [diff] [blame] | 1101 | case 0x8000000A: |
| 1102 | if (!kvm_cpu_cap_has(X86_FEATURE_SVM)) { |
| 1103 | entry->eax = entry->ebx = entry->ecx = entry->edx = 0; |
| 1104 | break; |
| 1105 | } |
| 1106 | entry->eax = 1; /* SVM revision 1 */ |
| 1107 | entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper |
| 1108 | ASID emulation to nested SVM */ |
| 1109 | entry->ecx = 0; /* Reserved */ |
| 1110 | cpuid_entry_override(entry, CPUID_8000_000A_EDX); |
| 1111 | break; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1112 | case 0x80000019: |
| 1113 | entry->ecx = entry->edx = 0; |
| 1114 | break; |
| 1115 | case 0x8000001a: |
Jim Mattson | 382409b | 2019-03-27 13:15:37 -0700 | [diff] [blame] | 1116 | case 0x8000001e: |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1117 | break; |
Peter Gonda | c1de0f2 | 2019-11-21 12:33:43 -0800 | [diff] [blame] | 1118 | case 0x8000001F: |
Sean Christopherson | e39f00f | 2021-06-23 16:05:47 -0700 | [diff] [blame] | 1119 | if (!kvm_cpu_cap_has(X86_FEATURE_SEV)) { |
Peter Gonda | c1de0f2 | 2019-11-21 12:33:43 -0800 | [diff] [blame] | 1120 | entry->eax = entry->ebx = entry->ecx = entry->edx = 0; |
Sean Christopherson | e39f00f | 2021-06-23 16:05:47 -0700 | [diff] [blame] | 1121 | } else { |
Paolo Bonzini | d9db0fd | 2021-04-21 19:11:15 -0700 | [diff] [blame] | 1122 | cpuid_entry_override(entry, CPUID_8000_001F_EAX); |
Sean Christopherson | e39f00f | 2021-06-23 16:05:47 -0700 | [diff] [blame] | 1123 | |
| 1124 | /* |
| 1125 | * Enumerate '0' for "PA bits reduction", the adjusted |
| 1126 | * MAXPHYADDR is enumerated directly (see 0x80000008). |
| 1127 | */ |
| 1128 | entry->ebx &= ~GENMASK(11, 6); |
| 1129 | } |
Peter Gonda | c1de0f2 | 2019-11-21 12:33:43 -0800 | [diff] [blame] | 1130 | break; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1131 | /*Add support for Centaur's CPUID instruction*/ |
| 1132 | case 0xC0000000: |
| 1133 | /*Just support up to 0xC0000004 now*/ |
| 1134 | entry->eax = min(entry->eax, 0xC0000004); |
| 1135 | break; |
| 1136 | case 0xC0000001: |
Sean Christopherson | bd79199 | 2020-03-02 15:56:53 -0800 | [diff] [blame] | 1137 | cpuid_entry_override(entry, CPUID_C000_0001_EDX); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1138 | break; |
| 1139 | case 3: /* Processor serial number */ |
| 1140 | case 5: /* MONITOR/MWAIT */ |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1141 | case 0xC0000002: |
| 1142 | case 0xC0000003: |
| 1143 | case 0xC0000004: |
| 1144 | default: |
| 1145 | entry->eax = entry->ebx = entry->ecx = entry->edx = 0; |
| 1146 | break; |
| 1147 | } |
| 1148 | |
Sasha Levin | 831bf66 | 2011-11-28 11:20:29 +0200 | [diff] [blame] | 1149 | r = 0; |
| 1150 | |
| 1151 | out: |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1152 | put_cpu(); |
Sasha Levin | 831bf66 | 2011-11-28 11:20:29 +0200 | [diff] [blame] | 1153 | |
| 1154 | return r; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1155 | } |
| 1156 | |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 1157 | static int do_cpuid_func(struct kvm_cpuid_array *array, u32 func, |
| 1158 | unsigned int type) |
Borislav Petkov | 9c15bb1 | 2013-09-22 16:44:50 +0200 | [diff] [blame] | 1159 | { |
| 1160 | if (type == KVM_GET_EMULATED_CPUID) |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 1161 | return __do_cpuid_func_emulated(array, func); |
Borislav Petkov | 9c15bb1 | 2013-09-22 16:44:50 +0200 | [diff] [blame] | 1162 | |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 1163 | return __do_cpuid_func(array, func); |
Borislav Petkov | 9c15bb1 | 2013-09-22 16:44:50 +0200 | [diff] [blame] | 1164 | } |
| 1165 | |
Sean Christopherson | 8b86079 | 2020-03-02 15:56:06 -0800 | [diff] [blame] | 1166 | #define CENTAUR_CPUID_SIGNATURE 0xC0000000 |
Sasha Levin | 831bf66 | 2011-11-28 11:20:29 +0200 | [diff] [blame] | 1167 | |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 1168 | static int get_cpuid_func(struct kvm_cpuid_array *array, u32 func, |
| 1169 | unsigned int type) |
Sean Christopherson | 619a17f | 2020-03-02 15:56:05 -0800 | [diff] [blame] | 1170 | { |
| 1171 | u32 limit; |
| 1172 | int r; |
| 1173 | |
Sean Christopherson | 8b86079 | 2020-03-02 15:56:06 -0800 | [diff] [blame] | 1174 | if (func == CENTAUR_CPUID_SIGNATURE && |
| 1175 | boot_cpu_data.x86_vendor != X86_VENDOR_CENTAUR) |
| 1176 | return 0; |
| 1177 | |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 1178 | r = do_cpuid_func(array, func, type); |
Sean Christopherson | 619a17f | 2020-03-02 15:56:05 -0800 | [diff] [blame] | 1179 | if (r) |
| 1180 | return r; |
| 1181 | |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 1182 | limit = array->entries[array->nent - 1].eax; |
Sean Christopherson | 619a17f | 2020-03-02 15:56:05 -0800 | [diff] [blame] | 1183 | for (func = func + 1; func <= limit; ++func) { |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 1184 | r = do_cpuid_func(array, func, type); |
Sean Christopherson | 619a17f | 2020-03-02 15:56:05 -0800 | [diff] [blame] | 1185 | if (r) |
| 1186 | break; |
| 1187 | } |
| 1188 | |
| 1189 | return r; |
| 1190 | } |
| 1191 | |
Borislav Petkov | 9c15bb1 | 2013-09-22 16:44:50 +0200 | [diff] [blame] | 1192 | static bool sanity_check_entries(struct kvm_cpuid_entry2 __user *entries, |
| 1193 | __u32 num_entries, unsigned int ioctl_type) |
| 1194 | { |
| 1195 | int i; |
Borislav Petkov | 1b2ca42 | 2013-11-06 15:46:02 +0100 | [diff] [blame] | 1196 | __u32 pad[3]; |
Borislav Petkov | 9c15bb1 | 2013-09-22 16:44:50 +0200 | [diff] [blame] | 1197 | |
| 1198 | if (ioctl_type != KVM_GET_EMULATED_CPUID) |
| 1199 | return false; |
| 1200 | |
| 1201 | /* |
| 1202 | * We want to make sure that ->padding is being passed clean from |
| 1203 | * userspace in case we want to use it for something in the future. |
| 1204 | * |
| 1205 | * Sadly, this wasn't enforced for KVM_GET_SUPPORTED_CPUID and so we |
| 1206 | * have to give ourselves satisfied only with the emulated side. /me |
| 1207 | * sheds a tear. |
| 1208 | */ |
| 1209 | for (i = 0; i < num_entries; i++) { |
Borislav Petkov | 1b2ca42 | 2013-11-06 15:46:02 +0100 | [diff] [blame] | 1210 | if (copy_from_user(pad, entries[i].padding, sizeof(pad))) |
| 1211 | return true; |
| 1212 | |
| 1213 | if (pad[0] || pad[1] || pad[2]) |
Borislav Petkov | 9c15bb1 | 2013-09-22 16:44:50 +0200 | [diff] [blame] | 1214 | return true; |
| 1215 | } |
| 1216 | return false; |
| 1217 | } |
| 1218 | |
| 1219 | int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid, |
| 1220 | struct kvm_cpuid_entry2 __user *entries, |
| 1221 | unsigned int type) |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1222 | { |
Sean Christopherson | 8b86079 | 2020-03-02 15:56:06 -0800 | [diff] [blame] | 1223 | static const u32 funcs[] = { |
| 1224 | 0, 0x80000000, CENTAUR_CPUID_SIGNATURE, KVM_CPUID_SIGNATURE, |
Sasha Levin | 831bf66 | 2011-11-28 11:20:29 +0200 | [diff] [blame] | 1225 | }; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1226 | |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 1227 | struct kvm_cpuid_array array = { |
| 1228 | .nent = 0, |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 1229 | }; |
| 1230 | int r, i; |
Sean Christopherson | d5a661d | 2020-03-02 15:56:07 -0800 | [diff] [blame] | 1231 | |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1232 | if (cpuid->nent < 1) |
Sean Christopherson | d5a661d | 2020-03-02 15:56:07 -0800 | [diff] [blame] | 1233 | return -E2BIG; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1234 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) |
| 1235 | cpuid->nent = KVM_MAX_CPUID_ENTRIES; |
Borislav Petkov | 9c15bb1 | 2013-09-22 16:44:50 +0200 | [diff] [blame] | 1236 | |
| 1237 | if (sanity_check_entries(entries, cpuid->nent, type)) |
| 1238 | return -EINVAL; |
| 1239 | |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 1240 | array.entries = vzalloc(array_size(sizeof(struct kvm_cpuid_entry2), |
Kees Cook | fad953c | 2018-06-12 14:27:37 -0700 | [diff] [blame] | 1241 | cpuid->nent)); |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 1242 | if (!array.entries) |
Sean Christopherson | d5a661d | 2020-03-02 15:56:07 -0800 | [diff] [blame] | 1243 | return -ENOMEM; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1244 | |
Xiaoyao Li | 65b1891 | 2020-06-04 12:16:36 +0800 | [diff] [blame] | 1245 | array.maxnent = cpuid->nent; |
| 1246 | |
Sean Christopherson | 8b86079 | 2020-03-02 15:56:06 -0800 | [diff] [blame] | 1247 | for (i = 0; i < ARRAY_SIZE(funcs); i++) { |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 1248 | r = get_cpuid_func(&array, funcs[i], type); |
Sasha Levin | 831bf66 | 2011-11-28 11:20:29 +0200 | [diff] [blame] | 1249 | if (r) |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1250 | goto out_free; |
| 1251 | } |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 1252 | cpuid->nent = array.nent; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1253 | |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 1254 | if (copy_to_user(entries, array.entries, |
| 1255 | array.nent * sizeof(struct kvm_cpuid_entry2))) |
Sean Christopherson | d5a661d | 2020-03-02 15:56:07 -0800 | [diff] [blame] | 1256 | r = -EFAULT; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1257 | |
| 1258 | out_free: |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 1259 | vfree(array.entries); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1260 | return r; |
| 1261 | } |
| 1262 | |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1263 | struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu, |
| 1264 | u32 function, u32 index) |
| 1265 | { |
Vitaly Kuznetsov | f69858f | 2020-10-01 15:05:39 +0200 | [diff] [blame] | 1266 | return cpuid_entry2_find(vcpu->arch.cpuid_entries, vcpu->arch.cpuid_nent, |
| 1267 | function, index); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1268 | } |
| 1269 | EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry); |
| 1270 | |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1271 | /* |
Sean Christopherson | 8d89231 | 2020-03-04 17:34:34 -0800 | [diff] [blame] | 1272 | * Intel CPUID semantics treats any query for an out-of-range leaf as if the |
| 1273 | * highest basic leaf (i.e. CPUID.0H:EAX) were requested. AMD CPUID semantics |
| 1274 | * returns all zeroes for any undefined leaf, whether or not the leaf is in |
| 1275 | * range. Centaur/VIA follows Intel semantics. |
| 1276 | * |
| 1277 | * A leaf is considered out-of-range if its function is higher than the maximum |
| 1278 | * supported leaf of its associated class or if its associated class does not |
| 1279 | * exist. |
| 1280 | * |
| 1281 | * There are three primary classes to be considered, with their respective |
| 1282 | * ranges described as "<base> - <top>[,<base2> - <top2>] inclusive. A primary |
| 1283 | * class exists if a guest CPUID entry for its <base> leaf exists. For a given |
| 1284 | * class, CPUID.<base>.EAX contains the max supported leaf for the class. |
| 1285 | * |
| 1286 | * - Basic: 0x00000000 - 0x3fffffff, 0x50000000 - 0x7fffffff |
| 1287 | * - Hypervisor: 0x40000000 - 0x4fffffff |
| 1288 | * - Extended: 0x80000000 - 0xbfffffff |
| 1289 | * - Centaur: 0xc0000000 - 0xcfffffff |
| 1290 | * |
| 1291 | * The Hypervisor class is further subdivided into sub-classes that each act as |
Ingo Molnar | d9f6e12 | 2021-03-18 15:28:01 +0100 | [diff] [blame] | 1292 | * their own independent class associated with a 0x100 byte range. E.g. if Qemu |
Sean Christopherson | 8d89231 | 2020-03-04 17:34:34 -0800 | [diff] [blame] | 1293 | * is advertising support for both HyperV and KVM, the resulting Hypervisor |
| 1294 | * CPUID sub-classes are: |
| 1295 | * |
| 1296 | * - HyperV: 0x40000000 - 0x400000ff |
| 1297 | * - KVM: 0x40000100 - 0x400001ff |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1298 | */ |
Sean Christopherson | 09c7431 | 2020-03-04 17:34:36 -0800 | [diff] [blame] | 1299 | static struct kvm_cpuid_entry2 * |
| 1300 | get_out_of_range_cpuid_entry(struct kvm_vcpu *vcpu, u32 *fn_ptr, u32 index) |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1301 | { |
Sean Christopherson | 8d89231 | 2020-03-04 17:34:34 -0800 | [diff] [blame] | 1302 | struct kvm_cpuid_entry2 *basic, *class; |
Sean Christopherson | 09c7431 | 2020-03-04 17:34:36 -0800 | [diff] [blame] | 1303 | u32 function = *fn_ptr; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1304 | |
Sean Christopherson | 8d89231 | 2020-03-04 17:34:34 -0800 | [diff] [blame] | 1305 | basic = kvm_find_cpuid_entry(vcpu, 0, 0); |
| 1306 | if (!basic) |
Sean Christopherson | 09c7431 | 2020-03-04 17:34:36 -0800 | [diff] [blame] | 1307 | return NULL; |
| 1308 | |
| 1309 | if (is_guest_vendor_amd(basic->ebx, basic->ecx, basic->edx) || |
| 1310 | is_guest_vendor_hygon(basic->ebx, basic->ecx, basic->edx)) |
| 1311 | return NULL; |
Sean Christopherson | 8d89231 | 2020-03-04 17:34:34 -0800 | [diff] [blame] | 1312 | |
| 1313 | if (function >= 0x40000000 && function <= 0x4fffffff) |
| 1314 | class = kvm_find_cpuid_entry(vcpu, function & 0xffffff00, 0); |
| 1315 | else if (function >= 0xc0000000) |
| 1316 | class = kvm_find_cpuid_entry(vcpu, 0xc0000000, 0); |
| 1317 | else |
| 1318 | class = kvm_find_cpuid_entry(vcpu, function & 0x80000000, 0); |
| 1319 | |
Sean Christopherson | 09c7431 | 2020-03-04 17:34:36 -0800 | [diff] [blame] | 1320 | if (class && function <= class->eax) |
| 1321 | return NULL; |
| 1322 | |
| 1323 | /* |
| 1324 | * Leaf specific adjustments are also applied when redirecting to the |
| 1325 | * max basic entry, e.g. if the max basic leaf is 0xb but there is no |
| 1326 | * entry for CPUID.0xb.index (see below), then the output value for EDX |
| 1327 | * needs to be pulled from CPUID.0xb.1. |
| 1328 | */ |
| 1329 | *fn_ptr = basic->eax; |
| 1330 | |
| 1331 | /* |
| 1332 | * The class does not exist or the requested function is out of range; |
| 1333 | * the effective CPUID entry is the max basic leaf. Note, the index of |
| 1334 | * the original requested leaf is observed! |
| 1335 | */ |
| 1336 | return kvm_find_cpuid_entry(vcpu, basic->eax, index); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1337 | } |
| 1338 | |
Yu Zhang | e911eb3 | 2017-08-24 20:27:52 +0800 | [diff] [blame] | 1339 | bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, |
Sean Christopherson | f91af51 | 2020-03-04 17:34:37 -0800 | [diff] [blame] | 1340 | u32 *ecx, u32 *edx, bool exact_only) |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1341 | { |
Jan Kiszka | b7fb848 | 2020-03-04 17:34:31 -0800 | [diff] [blame] | 1342 | u32 orig_function = *eax, function = *eax, index = *ecx; |
Jim Mattson | 4356112 | 2019-09-25 17:04:17 -0700 | [diff] [blame] | 1343 | struct kvm_cpuid_entry2 *entry; |
Sean Christopherson | 2b110b6 | 2020-03-17 12:53:54 -0700 | [diff] [blame] | 1344 | bool exact, used_max_basic = false; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1345 | |
Jim Mattson | 4356112 | 2019-09-25 17:04:17 -0700 | [diff] [blame] | 1346 | entry = kvm_find_cpuid_entry(vcpu, function, index); |
Sean Christopherson | f91af51 | 2020-03-04 17:34:37 -0800 | [diff] [blame] | 1347 | exact = !!entry; |
Sean Christopherson | 09c7431 | 2020-03-04 17:34:36 -0800 | [diff] [blame] | 1348 | |
Sean Christopherson | 2b110b6 | 2020-03-17 12:53:54 -0700 | [diff] [blame] | 1349 | if (!entry && !exact_only) { |
Sean Christopherson | 09c7431 | 2020-03-04 17:34:36 -0800 | [diff] [blame] | 1350 | entry = get_out_of_range_cpuid_entry(vcpu, &function, index); |
Sean Christopherson | 2b110b6 | 2020-03-17 12:53:54 -0700 | [diff] [blame] | 1351 | used_max_basic = !!entry; |
| 1352 | } |
Sean Christopherson | 09c7431 | 2020-03-04 17:34:36 -0800 | [diff] [blame] | 1353 | |
Jim Mattson | 4356112 | 2019-09-25 17:04:17 -0700 | [diff] [blame] | 1354 | if (entry) { |
| 1355 | *eax = entry->eax; |
| 1356 | *ebx = entry->ebx; |
| 1357 | *ecx = entry->ecx; |
| 1358 | *edx = entry->edx; |
Paolo Bonzini | edef5c3 | 2019-11-18 12:23:00 -0500 | [diff] [blame] | 1359 | if (function == 7 && index == 0) { |
| 1360 | u64 data; |
| 1361 | if (!__kvm_get_msr(vcpu, MSR_IA32_TSX_CTRL, &data, true) && |
| 1362 | (data & TSX_CTRL_CPUID_CLEAR)) |
| 1363 | *ebx &= ~(F(RTM) | F(HLE)); |
| 1364 | } |
Jim Mattson | 4356112 | 2019-09-25 17:04:17 -0700 | [diff] [blame] | 1365 | } else { |
Avi Kivity | 62046e5 | 2012-06-07 14:07:48 +0300 | [diff] [blame] | 1366 | *eax = *ebx = *ecx = *edx = 0; |
Jim Mattson | 4356112 | 2019-09-25 17:04:17 -0700 | [diff] [blame] | 1367 | /* |
| 1368 | * When leaf 0BH or 1FH is defined, CL is pass-through |
| 1369 | * and EDX is always the x2APIC ID, even for undefined |
| 1370 | * subleaves. Index 1 will exist iff the leaf is |
| 1371 | * implemented, so we pass through CL iff leaf 1 |
| 1372 | * exists. EDX can be copied from any existing index. |
| 1373 | */ |
| 1374 | if (function == 0xb || function == 0x1f) { |
| 1375 | entry = kvm_find_cpuid_entry(vcpu, function, 1); |
| 1376 | if (entry) { |
| 1377 | *ecx = index & 0xff; |
| 1378 | *edx = entry->edx; |
| 1379 | } |
| 1380 | } |
| 1381 | } |
Sean Christopherson | 2b110b6 | 2020-03-17 12:53:54 -0700 | [diff] [blame] | 1382 | trace_kvm_cpuid(orig_function, index, *eax, *ebx, *ecx, *edx, exact, |
| 1383 | used_max_basic); |
Sean Christopherson | f91af51 | 2020-03-04 17:34:37 -0800 | [diff] [blame] | 1384 | return exact; |
Avi Kivity | 62046e5 | 2012-06-07 14:07:48 +0300 | [diff] [blame] | 1385 | } |
Julian Stecklina | 66f7b72 | 2012-12-05 15:26:19 +0100 | [diff] [blame] | 1386 | EXPORT_SYMBOL_GPL(kvm_cpuid); |
Avi Kivity | 62046e5 | 2012-06-07 14:07:48 +0300 | [diff] [blame] | 1387 | |
Kyle Huey | 6a908b6 | 2016-11-29 12:40:37 -0800 | [diff] [blame] | 1388 | int kvm_emulate_cpuid(struct kvm_vcpu *vcpu) |
Avi Kivity | 62046e5 | 2012-06-07 14:07:48 +0300 | [diff] [blame] | 1389 | { |
Jiang Biao | 1e13175 | 2016-11-07 08:55:49 +0800 | [diff] [blame] | 1390 | u32 eax, ebx, ecx, edx; |
Avi Kivity | 62046e5 | 2012-06-07 14:07:48 +0300 | [diff] [blame] | 1391 | |
Kyle Huey | db2336a | 2017-03-20 01:16:28 -0700 | [diff] [blame] | 1392 | if (cpuid_fault_enabled(vcpu) && !kvm_require_cpl(vcpu, 0)) |
| 1393 | return 1; |
| 1394 | |
Sean Christopherson | de3cd11 | 2019-04-30 10:36:17 -0700 | [diff] [blame] | 1395 | eax = kvm_rax_read(vcpu); |
| 1396 | ecx = kvm_rcx_read(vcpu); |
Sean Christopherson | f91af51 | 2020-03-04 17:34:37 -0800 | [diff] [blame] | 1397 | kvm_cpuid(vcpu, &eax, &ebx, &ecx, &edx, false); |
Sean Christopherson | de3cd11 | 2019-04-30 10:36:17 -0700 | [diff] [blame] | 1398 | kvm_rax_write(vcpu, eax); |
| 1399 | kvm_rbx_write(vcpu, ebx); |
| 1400 | kvm_rcx_write(vcpu, ecx); |
| 1401 | kvm_rdx_write(vcpu, edx); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 1402 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1403 | } |
| 1404 | EXPORT_SYMBOL_GPL(kvm_emulate_cpuid); |