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