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> |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 21 | #include "cpuid.h" |
| 22 | #include "lapic.h" |
| 23 | #include "mmu.h" |
| 24 | #include "trace.h" |
Wei Huang | 474a5bb | 2015-06-19 13:54:23 +0200 | [diff] [blame] | 25 | #include "pmu.h" |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 26 | |
Paolo Bonzini | 412a3c4 | 2014-12-03 14:38:01 +0100 | [diff] [blame] | 27 | static u32 xstate_required_size(u64 xstate_bv, bool compacted) |
Paolo Bonzini | 4344ee9 | 2013-10-02 16:06:16 +0200 | [diff] [blame] | 28 | { |
| 29 | int feature_bit = 0; |
| 30 | u32 ret = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET; |
| 31 | |
Dave Hansen | d91cab7 | 2015-09-02 16:31:26 -0700 | [diff] [blame] | 32 | xstate_bv &= XFEATURE_MASK_EXTEND; |
Paolo Bonzini | 4344ee9 | 2013-10-02 16:06:16 +0200 | [diff] [blame] | 33 | while (xstate_bv) { |
| 34 | if (xstate_bv & 0x1) { |
Paolo Bonzini | 412a3c4 | 2014-12-03 14:38:01 +0100 | [diff] [blame] | 35 | u32 eax, ebx, ecx, edx, offset; |
Paolo Bonzini | 4344ee9 | 2013-10-02 16:06:16 +0200 | [diff] [blame] | 36 | cpuid_count(0xD, feature_bit, &eax, &ebx, &ecx, &edx); |
Paolo Bonzini | 412a3c4 | 2014-12-03 14:38:01 +0100 | [diff] [blame] | 37 | offset = compacted ? ret : ebx; |
| 38 | ret = max(ret, offset + eax); |
Paolo Bonzini | 4344ee9 | 2013-10-02 16:06:16 +0200 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | xstate_bv >>= 1; |
| 42 | feature_bit++; |
| 43 | } |
| 44 | |
| 45 | return ret; |
| 46 | } |
| 47 | |
Sean Christopherson | 8738200 | 2019-12-17 13:32:42 -0800 | [diff] [blame] | 48 | #define F feature_bit |
Paolo Bonzini | 5c404ca | 2014-12-03 14:34:47 +0100 | [diff] [blame] | 49 | |
Nadav Amit | dd59809 | 2014-09-16 15:10:03 +0300 | [diff] [blame] | 50 | int kvm_update_cpuid(struct kvm_vcpu *vcpu) |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 51 | { |
| 52 | struct kvm_cpuid_entry2 *best; |
| 53 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 54 | |
| 55 | best = kvm_find_cpuid_entry(vcpu, 1, 0); |
| 56 | if (!best) |
Nadav Amit | dd59809 | 2014-09-16 15:10:03 +0300 | [diff] [blame] | 57 | return 0; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 58 | |
| 59 | /* Update OSXSAVE bit */ |
Borislav Petkov | d366bf7 | 2016-04-04 22:25:02 +0200 | [diff] [blame] | 60 | if (boot_cpu_has(X86_FEATURE_XSAVE) && best->function == 0x1) { |
Paolo Bonzini | 5c404ca | 2014-12-03 14:34:47 +0100 | [diff] [blame] | 61 | best->ecx &= ~F(OSXSAVE); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 62 | if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) |
Paolo Bonzini | 5c404ca | 2014-12-03 14:34:47 +0100 | [diff] [blame] | 63 | best->ecx |= F(OSXSAVE); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 64 | } |
| 65 | |
Jim Mattson | c7dd15b | 2016-11-09 09:50:11 -0800 | [diff] [blame] | 66 | best->edx &= ~F(APIC); |
| 67 | if (vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE) |
| 68 | best->edx |= F(APIC); |
| 69 | |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 70 | if (apic) { |
Paolo Bonzini | 5c404ca | 2014-12-03 14:34:47 +0100 | [diff] [blame] | 71 | if (best->ecx & F(TSC_DEADLINE_TIMER)) |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 72 | apic->lapic_timer.timer_mode_mask = 3 << 17; |
| 73 | else |
| 74 | apic->lapic_timer.timer_mode_mask = 1 << 17; |
| 75 | } |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 76 | |
Huaitong Han | b9baba8 | 2016-03-22 16:51:21 +0800 | [diff] [blame] | 77 | best = kvm_find_cpuid_entry(vcpu, 7, 0); |
| 78 | if (best) { |
| 79 | /* Update OSPKE bit */ |
| 80 | if (boot_cpu_has(X86_FEATURE_PKU) && best->function == 0x7) { |
| 81 | best->ecx &= ~F(OSPKE); |
| 82 | if (kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) |
| 83 | best->ecx |= F(OSPKE); |
| 84 | } |
| 85 | } |
| 86 | |
Paolo Bonzini | d7876f1 | 2013-10-02 16:06:15 +0200 | [diff] [blame] | 87 | best = kvm_find_cpuid_entry(vcpu, 0xD, 0); |
Paolo Bonzini | 4344ee9 | 2013-10-02 16:06:16 +0200 | [diff] [blame] | 88 | if (!best) { |
Paolo Bonzini | d7876f1 | 2013-10-02 16:06:15 +0200 | [diff] [blame] | 89 | vcpu->arch.guest_supported_xcr0 = 0; |
Paolo Bonzini | 4344ee9 | 2013-10-02 16:06:16 +0200 | [diff] [blame] | 90 | vcpu->arch.guest_xstate_size = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET; |
| 91 | } else { |
Paolo Bonzini | d7876f1 | 2013-10-02 16:06:15 +0200 | [diff] [blame] | 92 | vcpu->arch.guest_supported_xcr0 = |
Sean Christopherson | cfc4818 | 2020-03-02 15:56:23 -0800 | [diff] [blame] | 93 | (best->eax | ((u64)best->edx << 32)) & supported_xcr0; |
Liu, Jinsong | 56c103e | 2014-02-21 17:39:02 +0000 | [diff] [blame] | 94 | vcpu->arch.guest_xstate_size = best->ebx = |
Paolo Bonzini | 412a3c4 | 2014-12-03 14:38:01 +0100 | [diff] [blame] | 95 | xstate_required_size(vcpu->arch.xcr0, false); |
Paolo Bonzini | 4344ee9 | 2013-10-02 16:06:16 +0200 | [diff] [blame] | 96 | } |
Paolo Bonzini | d7876f1 | 2013-10-02 16:06:15 +0200 | [diff] [blame] | 97 | |
Paolo Bonzini | 412a3c4 | 2014-12-03 14:38:01 +0100 | [diff] [blame] | 98 | best = kvm_find_cpuid_entry(vcpu, 0xD, 1); |
| 99 | if (best && (best->eax & (F(XSAVES) | F(XSAVEC)))) |
| 100 | best->ebx = xstate_required_size(vcpu->arch.xcr0, true); |
| 101 | |
Nadav Amit | dd59809 | 2014-09-16 15:10:03 +0300 | [diff] [blame] | 102 | /* |
Yu Zhang | fd8cb43 | 2017-08-24 20:27:56 +0800 | [diff] [blame] | 103 | * The existing code assumes virtual address is 48-bit or 57-bit in the |
| 104 | * canonical address checks; exit if it is ever changed. |
Nadav Amit | dd59809 | 2014-09-16 15:10:03 +0300 | [diff] [blame] | 105 | */ |
| 106 | best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0); |
Yu Zhang | fd8cb43 | 2017-08-24 20:27:56 +0800 | [diff] [blame] | 107 | if (best) { |
| 108 | int vaddr_bits = (best->eax & 0xff00) >> 8; |
| 109 | |
| 110 | if (vaddr_bits != 48 && vaddr_bits != 57 && vaddr_bits != 0) |
| 111 | return -EINVAL; |
| 112 | } |
Nadav Amit | dd59809 | 2014-09-16 15:10:03 +0300 | [diff] [blame] | 113 | |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 114 | best = kvm_find_cpuid_entry(vcpu, KVM_CPUID_FEATURES, 0); |
| 115 | if (kvm_hlt_in_guest(vcpu->kvm) && best && |
| 116 | (best->eax & (1 << KVM_FEATURE_PV_UNHALT))) |
| 117 | best->eax &= ~(1 << KVM_FEATURE_PV_UNHALT); |
| 118 | |
Wanpeng Li | 511a8556 | 2019-05-21 14:06:54 +0800 | [diff] [blame] | 119 | if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT)) { |
| 120 | best = kvm_find_cpuid_entry(vcpu, 0x1, 0); |
| 121 | if (best) { |
| 122 | if (vcpu->arch.ia32_misc_enable_msr & MSR_IA32_MISC_ENABLE_MWAIT) |
| 123 | best->ecx |= F(MWAIT); |
| 124 | else |
| 125 | best->ecx &= ~F(MWAIT); |
| 126 | } |
| 127 | } |
| 128 | |
Eugene Korenevsky | 5a4f55c | 2015-03-29 23:56:12 +0300 | [diff] [blame] | 129 | /* Update physical-address width */ |
| 130 | vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu); |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 131 | kvm_mmu_reset_context(vcpu); |
Eugene Korenevsky | 5a4f55c | 2015-03-29 23:56:12 +0300 | [diff] [blame] | 132 | |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 133 | kvm_pmu_refresh(vcpu); |
Nadav Amit | dd59809 | 2014-09-16 15:10:03 +0300 | [diff] [blame] | 134 | return 0; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static int is_efer_nx(void) |
| 138 | { |
| 139 | unsigned long long efer = 0; |
| 140 | |
| 141 | rdmsrl_safe(MSR_EFER, &efer); |
| 142 | return efer & EFER_NX; |
| 143 | } |
| 144 | |
| 145 | static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu) |
| 146 | { |
| 147 | int i; |
| 148 | struct kvm_cpuid_entry2 *e, *entry; |
| 149 | |
| 150 | entry = NULL; |
| 151 | for (i = 0; i < vcpu->arch.cpuid_nent; ++i) { |
| 152 | e = &vcpu->arch.cpuid_entries[i]; |
| 153 | if (e->function == 0x80000001) { |
| 154 | entry = e; |
| 155 | break; |
| 156 | } |
| 157 | } |
Paolo Bonzini | 5c404ca | 2014-12-03 14:34:47 +0100 | [diff] [blame] | 158 | if (entry && (entry->edx & F(NX)) && !is_efer_nx()) { |
| 159 | entry->edx &= ~F(NX); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 160 | printk(KERN_INFO "kvm: guest NX capability removed\n"); |
| 161 | } |
| 162 | } |
| 163 | |
Eugene Korenevsky | 5a4f55c | 2015-03-29 23:56:12 +0300 | [diff] [blame] | 164 | int cpuid_query_maxphyaddr(struct kvm_vcpu *vcpu) |
| 165 | { |
| 166 | struct kvm_cpuid_entry2 *best; |
| 167 | |
| 168 | best = kvm_find_cpuid_entry(vcpu, 0x80000000, 0); |
| 169 | if (!best || best->eax < 0x80000008) |
| 170 | goto not_found; |
| 171 | best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0); |
| 172 | if (best) |
| 173 | return best->eax & 0xff; |
| 174 | not_found: |
| 175 | return 36; |
| 176 | } |
| 177 | EXPORT_SYMBOL_GPL(cpuid_query_maxphyaddr); |
| 178 | |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 179 | /* when an old userspace process fills a new kernel module */ |
| 180 | int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu, |
| 181 | struct kvm_cpuid *cpuid, |
| 182 | struct kvm_cpuid_entry __user *entries) |
| 183 | { |
| 184 | int r, i; |
Paolo Bonzini | 83676e9 | 2016-06-01 14:09:19 +0200 | [diff] [blame] | 185 | struct kvm_cpuid_entry *cpuid_entries = NULL; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 186 | |
| 187 | r = -E2BIG; |
| 188 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) |
| 189 | goto out; |
| 190 | r = -ENOMEM; |
Paolo Bonzini | 83676e9 | 2016-06-01 14:09:19 +0200 | [diff] [blame] | 191 | if (cpuid->nent) { |
Kees Cook | 42bc47b | 2018-06-12 14:27:11 -0700 | [diff] [blame] | 192 | cpuid_entries = |
| 193 | vmalloc(array_size(sizeof(struct kvm_cpuid_entry), |
| 194 | cpuid->nent)); |
Paolo Bonzini | 83676e9 | 2016-06-01 14:09:19 +0200 | [diff] [blame] | 195 | if (!cpuid_entries) |
| 196 | goto out; |
| 197 | r = -EFAULT; |
| 198 | if (copy_from_user(cpuid_entries, entries, |
| 199 | cpuid->nent * sizeof(struct kvm_cpuid_entry))) |
| 200 | goto out; |
| 201 | } |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 202 | for (i = 0; i < cpuid->nent; i++) { |
| 203 | vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function; |
| 204 | vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax; |
| 205 | vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx; |
| 206 | vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx; |
| 207 | vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx; |
| 208 | vcpu->arch.cpuid_entries[i].index = 0; |
| 209 | vcpu->arch.cpuid_entries[i].flags = 0; |
| 210 | vcpu->arch.cpuid_entries[i].padding[0] = 0; |
| 211 | vcpu->arch.cpuid_entries[i].padding[1] = 0; |
| 212 | vcpu->arch.cpuid_entries[i].padding[2] = 0; |
| 213 | } |
| 214 | vcpu->arch.cpuid_nent = cpuid->nent; |
| 215 | cpuid_fix_nx_cap(vcpu); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 216 | kvm_apic_set_version(vcpu); |
| 217 | kvm_x86_ops->cpuid_update(vcpu); |
Nadav Amit | dd59809 | 2014-09-16 15:10:03 +0300 | [diff] [blame] | 218 | r = kvm_update_cpuid(vcpu); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 219 | |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 220 | out: |
Paolo Bonzini | 83676e9 | 2016-06-01 14:09:19 +0200 | [diff] [blame] | 221 | vfree(cpuid_entries); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 222 | return r; |
| 223 | } |
| 224 | |
| 225 | int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu, |
| 226 | struct kvm_cpuid2 *cpuid, |
| 227 | struct kvm_cpuid_entry2 __user *entries) |
| 228 | { |
| 229 | int r; |
| 230 | |
| 231 | r = -E2BIG; |
| 232 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) |
| 233 | goto out; |
| 234 | r = -EFAULT; |
| 235 | if (copy_from_user(&vcpu->arch.cpuid_entries, entries, |
| 236 | cpuid->nent * sizeof(struct kvm_cpuid_entry2))) |
| 237 | goto out; |
| 238 | vcpu->arch.cpuid_nent = cpuid->nent; |
| 239 | kvm_apic_set_version(vcpu); |
| 240 | kvm_x86_ops->cpuid_update(vcpu); |
Nadav Amit | dd59809 | 2014-09-16 15:10:03 +0300 | [diff] [blame] | 241 | r = kvm_update_cpuid(vcpu); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 242 | out: |
| 243 | return r; |
| 244 | } |
| 245 | |
| 246 | int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu, |
| 247 | struct kvm_cpuid2 *cpuid, |
| 248 | struct kvm_cpuid_entry2 __user *entries) |
| 249 | { |
| 250 | int r; |
| 251 | |
| 252 | r = -E2BIG; |
| 253 | if (cpuid->nent < vcpu->arch.cpuid_nent) |
| 254 | goto out; |
| 255 | r = -EFAULT; |
| 256 | if (copy_to_user(entries, &vcpu->arch.cpuid_entries, |
| 257 | vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2))) |
| 258 | goto out; |
| 259 | return 0; |
| 260 | |
| 261 | out: |
| 262 | cpuid->nent = vcpu->arch.cpuid_nent; |
| 263 | return r; |
| 264 | } |
| 265 | |
Sean Christopherson | a7c48c3 | 2019-12-17 13:32:41 -0800 | [diff] [blame] | 266 | static __always_inline void cpuid_mask(u32 *word, int wordnum) |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 267 | { |
Sean Christopherson | a7c48c3 | 2019-12-17 13:32:41 -0800 | [diff] [blame] | 268 | reverse_cpuid_check(wordnum); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 269 | *word &= boot_cpu_data.x86_capability[wordnum]; |
| 270 | } |
| 271 | |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 272 | struct kvm_cpuid_array { |
| 273 | struct kvm_cpuid_entry2 *entries; |
| 274 | const int maxnent; |
| 275 | int nent; |
| 276 | }; |
| 277 | |
| 278 | 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] | 279 | u32 function, u32 index) |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 280 | { |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 281 | struct kvm_cpuid_entry2 *entry; |
| 282 | |
| 283 | if (array->nent >= array->maxnent) |
Sean Christopherson | aa10a7d | 2020-03-02 15:56:16 -0800 | [diff] [blame] | 284 | return NULL; |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 285 | |
| 286 | entry = &array->entries[array->nent++]; |
Sean Christopherson | aa10a7d | 2020-03-02 15:56:16 -0800 | [diff] [blame] | 287 | |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 288 | entry->function = function; |
| 289 | entry->index = index; |
Paolo Bonzini | ab8bcf6 | 2019-06-24 10:23:33 +0200 | [diff] [blame] | 290 | entry->flags = 0; |
| 291 | |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 292 | cpuid_count(entry->function, entry->index, |
| 293 | &entry->eax, &entry->ebx, &entry->ecx, &entry->edx); |
Paolo Bonzini | d9aadaf | 2019-07-04 12:20:48 +0200 | [diff] [blame] | 294 | |
| 295 | switch (function) { |
| 296 | case 2: |
| 297 | entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC; |
| 298 | break; |
| 299 | case 4: |
| 300 | case 7: |
| 301 | case 0xb: |
| 302 | case 0xd: |
Jim Mattson | a06dcd6 | 2019-09-12 09:55:03 -0700 | [diff] [blame] | 303 | case 0xf: |
| 304 | case 0x10: |
| 305 | case 0x12: |
Paolo Bonzini | d9aadaf | 2019-07-04 12:20:48 +0200 | [diff] [blame] | 306 | case 0x14: |
Jim Mattson | a06dcd6 | 2019-09-12 09:55:03 -0700 | [diff] [blame] | 307 | case 0x17: |
| 308 | case 0x18: |
| 309 | case 0x1f: |
Paolo Bonzini | d9aadaf | 2019-07-04 12:20:48 +0200 | [diff] [blame] | 310 | case 0x8000001d: |
| 311 | entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
| 312 | break; |
| 313 | } |
Sean Christopherson | aa10a7d | 2020-03-02 15:56:16 -0800 | [diff] [blame] | 314 | |
| 315 | return entry; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 316 | } |
| 317 | |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 318 | 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] | 319 | { |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 320 | struct kvm_cpuid_entry2 *entry = &array->entries[array->nent]; |
| 321 | |
Paolo Bonzini | ab8bcf6 | 2019-06-24 10:23:33 +0200 | [diff] [blame] | 322 | entry->function = func; |
| 323 | entry->index = 0; |
| 324 | entry->flags = 0; |
| 325 | |
Borislav Petkov | 84cffe4 | 2013-10-29 12:54:56 +0100 | [diff] [blame] | 326 | switch (func) { |
| 327 | case 0: |
Paolo Bonzini | fb6d4d3 | 2016-07-12 11:04:26 +0200 | [diff] [blame] | 328 | entry->eax = 7; |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 329 | ++array->nent; |
Borislav Petkov | 84cffe4 | 2013-10-29 12:54:56 +0100 | [diff] [blame] | 330 | break; |
| 331 | case 1: |
| 332 | entry->ecx = F(MOVBE); |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 333 | ++array->nent; |
Borislav Petkov | 84cffe4 | 2013-10-29 12:54:56 +0100 | [diff] [blame] | 334 | break; |
Paolo Bonzini | fb6d4d3 | 2016-07-12 11:04:26 +0200 | [diff] [blame] | 335 | case 7: |
| 336 | entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
Paolo Bonzini | ab8bcf6 | 2019-06-24 10:23:33 +0200 | [diff] [blame] | 337 | entry->eax = 0; |
| 338 | entry->ecx = F(RDPID); |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 339 | ++array->nent; |
Borislav Petkov | 84cffe4 | 2013-10-29 12:54:56 +0100 | [diff] [blame] | 340 | default: |
| 341 | break; |
| 342 | } |
| 343 | |
Borislav Petkov | 9c15bb1 | 2013-09-22 16:44:50 +0200 | [diff] [blame] | 344 | return 0; |
| 345 | } |
| 346 | |
Sean Christopherson | aceac6e | 2020-03-02 15:56:14 -0800 | [diff] [blame] | 347 | static inline void do_cpuid_7_mask(struct kvm_cpuid_entry2 *entry) |
Paolo Bonzini | 54d360d | 2019-07-04 12:18:13 +0200 | [diff] [blame] | 348 | { |
| 349 | unsigned f_invpcid = kvm_x86_ops->invpcid_supported() ? F(INVPCID) : 0; |
| 350 | unsigned f_mpx = kvm_mpx_supported() ? F(MPX) : 0; |
| 351 | unsigned f_umip = kvm_x86_ops->umip_emulated() ? F(UMIP) : 0; |
| 352 | unsigned f_intel_pt = kvm_x86_ops->pt_supported() ? F(INTEL_PT) : 0; |
| 353 | unsigned f_la57; |
John Allen | a47970e | 2019-12-19 14:17:59 -0600 | [diff] [blame] | 354 | unsigned f_pku = kvm_x86_ops->pku_supported() ? F(PKU) : 0; |
Paolo Bonzini | 54d360d | 2019-07-04 12:18:13 +0200 | [diff] [blame] | 355 | |
| 356 | /* cpuid 7.0.ebx */ |
| 357 | const u32 kvm_cpuid_7_0_ebx_x86_features = |
| 358 | F(FSGSBASE) | F(BMI1) | F(HLE) | F(AVX2) | F(SMEP) | |
| 359 | F(BMI2) | F(ERMS) | f_invpcid | F(RTM) | f_mpx | F(RDSEED) | |
| 360 | F(ADX) | F(SMAP) | F(AVX512IFMA) | F(AVX512F) | F(AVX512PF) | |
| 361 | F(AVX512ER) | F(AVX512CD) | F(CLFLUSHOPT) | F(CLWB) | F(AVX512DQ) | |
| 362 | F(SHA_NI) | F(AVX512BW) | F(AVX512VL) | f_intel_pt; |
| 363 | |
| 364 | /* cpuid 7.0.ecx*/ |
| 365 | const u32 kvm_cpuid_7_0_ecx_x86_features = |
John Allen | a47970e | 2019-12-19 14:17:59 -0600 | [diff] [blame] | 366 | F(AVX512VBMI) | F(LA57) | 0 /*PKU*/ | 0 /*OSPKE*/ | F(RDPID) | |
Paolo Bonzini | 54d360d | 2019-07-04 12:18:13 +0200 | [diff] [blame] | 367 | F(AVX512_VPOPCNTDQ) | F(UMIP) | F(AVX512_VBMI2) | F(GFNI) | |
| 368 | F(VAES) | F(VPCLMULQDQ) | F(AVX512_VNNI) | F(AVX512_BITALG) | |
Tao Xu | e69e72fa | 2019-07-16 14:55:49 +0800 | [diff] [blame] | 369 | F(CLDEMOTE) | F(MOVDIRI) | F(MOVDIR64B) | 0 /*WAITPKG*/; |
Paolo Bonzini | 54d360d | 2019-07-04 12:18:13 +0200 | [diff] [blame] | 370 | |
| 371 | /* cpuid 7.0.edx*/ |
| 372 | const u32 kvm_cpuid_7_0_edx_x86_features = |
| 373 | F(AVX512_4VNNIW) | F(AVX512_4FMAPS) | F(SPEC_CTRL) | |
| 374 | F(SPEC_CTRL_SSBD) | F(ARCH_CAPABILITIES) | F(INTEL_STIBP) | |
| 375 | F(MD_CLEAR); |
| 376 | |
Jing Liu | 0b77462 | 2019-07-11 13:49:57 +0800 | [diff] [blame] | 377 | /* cpuid 7.1.eax */ |
| 378 | const u32 kvm_cpuid_7_1_eax_x86_features = |
| 379 | F(AVX512_BF16); |
| 380 | |
Sean Christopherson | aceac6e | 2020-03-02 15:56:14 -0800 | [diff] [blame] | 381 | switch (entry->index) { |
Paolo Bonzini | 54d360d | 2019-07-04 12:18:13 +0200 | [diff] [blame] | 382 | case 0: |
Jing Liu | 0b77462 | 2019-07-11 13:49:57 +0800 | [diff] [blame] | 383 | entry->eax = min(entry->eax, 1u); |
Paolo Bonzini | 54d360d | 2019-07-04 12:18:13 +0200 | [diff] [blame] | 384 | entry->ebx &= kvm_cpuid_7_0_ebx_x86_features; |
| 385 | cpuid_mask(&entry->ebx, CPUID_7_0_EBX); |
| 386 | /* TSC_ADJUST is emulated */ |
| 387 | entry->ebx |= F(TSC_ADJUST); |
| 388 | |
| 389 | entry->ecx &= kvm_cpuid_7_0_ecx_x86_features; |
| 390 | f_la57 = entry->ecx & F(LA57); |
| 391 | cpuid_mask(&entry->ecx, CPUID_7_ECX); |
| 392 | /* Set LA57 based on hardware capability. */ |
| 393 | entry->ecx |= f_la57; |
| 394 | entry->ecx |= f_umip; |
John Allen | a47970e | 2019-12-19 14:17:59 -0600 | [diff] [blame] | 395 | entry->ecx |= f_pku; |
Paolo Bonzini | 54d360d | 2019-07-04 12:18:13 +0200 | [diff] [blame] | 396 | /* PKU is not yet implemented for shadow paging. */ |
| 397 | if (!tdp_enabled || !boot_cpu_has(X86_FEATURE_OSPKE)) |
| 398 | entry->ecx &= ~F(PKU); |
| 399 | |
| 400 | entry->edx &= kvm_cpuid_7_0_edx_x86_features; |
| 401 | cpuid_mask(&entry->edx, CPUID_7_EDX); |
Paolo Bonzini | 0c54914 | 2019-08-19 17:24:07 +0200 | [diff] [blame] | 402 | if (boot_cpu_has(X86_FEATURE_IBPB) && boot_cpu_has(X86_FEATURE_IBRS)) |
| 403 | entry->edx |= F(SPEC_CTRL); |
| 404 | if (boot_cpu_has(X86_FEATURE_STIBP)) |
| 405 | entry->edx |= F(INTEL_STIBP); |
Sean Christopherson | acfad33 | 2020-03-02 15:56:15 -0800 | [diff] [blame] | 406 | if (boot_cpu_has(X86_FEATURE_AMD_SSBD)) |
Paolo Bonzini | 0c54914 | 2019-08-19 17:24:07 +0200 | [diff] [blame] | 407 | entry->edx |= F(SPEC_CTRL_SSBD); |
Paolo Bonzini | 54d360d | 2019-07-04 12:18:13 +0200 | [diff] [blame] | 408 | /* |
| 409 | * We emulate ARCH_CAPABILITIES in software even |
| 410 | * if the host doesn't support it. |
| 411 | */ |
| 412 | entry->edx |= F(ARCH_CAPABILITIES); |
| 413 | break; |
Jing Liu | 0b77462 | 2019-07-11 13:49:57 +0800 | [diff] [blame] | 414 | case 1: |
| 415 | entry->eax &= kvm_cpuid_7_1_eax_x86_features; |
| 416 | entry->ebx = 0; |
| 417 | entry->ecx = 0; |
| 418 | entry->edx = 0; |
| 419 | break; |
Paolo Bonzini | 54d360d | 2019-07-04 12:18:13 +0200 | [diff] [blame] | 420 | default: |
| 421 | WARN_ON_ONCE(1); |
| 422 | entry->eax = 0; |
| 423 | entry->ebx = 0; |
| 424 | entry->ecx = 0; |
| 425 | entry->edx = 0; |
| 426 | break; |
| 427 | } |
| 428 | } |
| 429 | |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 430 | 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] | 431 | { |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 432 | struct kvm_cpuid_entry2 *entry; |
Sean Christopherson | 74fa0bc | 2020-03-02 15:56:17 -0800 | [diff] [blame] | 433 | int r, i, max_idx; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 434 | unsigned f_nx = is_efer_nx() ? F(NX) : 0; |
| 435 | #ifdef CONFIG_X86_64 |
| 436 | unsigned f_gbpages = (kvm_x86_ops->get_lpage_level() == PT_PDPE_LEVEL) |
| 437 | ? F(GBPAGES) : 0; |
| 438 | unsigned f_lm = F(LM); |
| 439 | #else |
| 440 | unsigned f_gbpages = 0; |
| 441 | unsigned f_lm = 0; |
| 442 | #endif |
| 443 | unsigned f_rdtscp = kvm_x86_ops->rdtscp_supported() ? F(RDTSCP) : 0; |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 444 | unsigned f_xsaves = kvm_x86_ops->xsaves_supported() ? F(XSAVES) : 0; |
Chao Peng | 86f5201 | 2018-10-24 16:05:11 +0800 | [diff] [blame] | 445 | unsigned f_intel_pt = kvm_x86_ops->pt_supported() ? F(INTEL_PT) : 0; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 446 | |
| 447 | /* cpuid 1.edx */ |
Huaitong Han | e0b18ef | 2016-03-22 16:51:14 +0800 | [diff] [blame] | 448 | const u32 kvm_cpuid_1_edx_x86_features = |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 449 | F(FPU) | F(VME) | F(DE) | F(PSE) | |
| 450 | F(TSC) | F(MSR) | F(PAE) | F(MCE) | |
| 451 | F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) | |
| 452 | F(MTRR) | F(PGE) | F(MCA) | F(CMOV) | |
H. Peter Anvin | 840d283 | 2014-02-27 08:31:30 -0800 | [diff] [blame] | 453 | F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLUSH) | |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 454 | 0 /* Reserved, DS, ACPI */ | F(MMX) | |
| 455 | F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) | |
| 456 | 0 /* HTT, TM, Reserved, PBE */; |
| 457 | /* cpuid 0x80000001.edx */ |
Huaitong Han | e0b18ef | 2016-03-22 16:51:14 +0800 | [diff] [blame] | 458 | const u32 kvm_cpuid_8000_0001_edx_x86_features = |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 459 | F(FPU) | F(VME) | F(DE) | F(PSE) | |
| 460 | F(TSC) | F(MSR) | F(PAE) | F(MCE) | |
| 461 | F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) | |
| 462 | F(MTRR) | F(PGE) | F(MCA) | F(CMOV) | |
| 463 | F(PAT) | F(PSE36) | 0 /* Reserved */ | |
| 464 | f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) | |
| 465 | F(FXSR) | F(FXSR_OPT) | f_gbpages | f_rdtscp | |
| 466 | 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW); |
| 467 | /* cpuid 1.ecx */ |
Huaitong Han | e0b18ef | 2016-03-22 16:51:14 +0800 | [diff] [blame] | 468 | const u32 kvm_cpuid_1_ecx_x86_features = |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 469 | /* NOTE: MONITOR (and MWAIT) are emulated as NOP, |
| 470 | * but *not* advertised to guests via CPUID ! */ |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 471 | F(XMM3) | F(PCLMULQDQ) | 0 /* DTES64, MONITOR */ | |
| 472 | 0 /* DS-CPL, VMX, SMX, EST */ | |
| 473 | 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ | |
Liu, Jinsong | fb21536 | 2011-11-28 03:55:19 -0800 | [diff] [blame] | 474 | F(FMA) | F(CX16) | 0 /* xTPR Update, PDCM */ | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 475 | F(PCID) | 0 /* Reserved, DCA */ | F(XMM4_1) | |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 476 | F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) | |
| 477 | 0 /* Reserved*/ | F(AES) | F(XSAVE) | 0 /* OSXSAVE */ | F(AVX) | |
| 478 | F(F16C) | F(RDRAND); |
| 479 | /* cpuid 0x80000001.ecx */ |
Huaitong Han | e0b18ef | 2016-03-22 16:51:14 +0800 | [diff] [blame] | 480 | const u32 kvm_cpuid_8000_0001_ecx_x86_features = |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 481 | F(LAHF_LM) | F(CMP_LEGACY) | 0 /*SVM*/ | 0 /* ExtApicSpace */ | |
| 482 | F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) | |
Boris Ostrovsky | 2b036c6 | 2012-01-09 14:00:35 -0500 | [diff] [blame] | 483 | F(3DNOWPREFETCH) | F(OSVW) | 0 /* IBS */ | F(XOP) | |
Stanislav Lanci | 806793f | 2018-01-29 11:39:44 -0500 | [diff] [blame] | 484 | 0 /* SKINIT, WDT, LWP */ | F(FMA4) | F(TBM) | |
Janakarajan Natarajan | c51eb52 | 2018-02-05 13:24:52 -0600 | [diff] [blame] | 485 | F(TOPOEXT) | F(PERFCTR_CORE); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 486 | |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 487 | /* cpuid 0x80000008.ebx */ |
| 488 | const u32 kvm_cpuid_8000_0008_ebx_x86_features = |
Sebastian Andrzej Siewior | 504ce19 | 2019-09-25 23:37:21 +0200 | [diff] [blame] | 489 | F(CLZERO) | F(XSAVEERPTR) | |
| 490 | F(WBNOINVD) | F(AMD_IBPB) | F(AMD_IBRS) | F(AMD_SSBD) | F(VIRT_SSBD) | |
| 491 | F(AMD_SSB_NO) | F(AMD_STIBP) | F(AMD_STIBP_ALWAYS_ON); |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 492 | |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 493 | /* cpuid 0xC0000001.edx */ |
Huaitong Han | e0b18ef | 2016-03-22 16:51:14 +0800 | [diff] [blame] | 494 | const u32 kvm_cpuid_C000_0001_edx_x86_features = |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 495 | F(XSTORE) | F(XSTORE_EN) | F(XCRYPT) | F(XCRYPT_EN) | |
| 496 | F(ACE2) | F(ACE2_EN) | F(PHE) | F(PHE_EN) | |
| 497 | F(PMM) | F(PMM_EN); |
| 498 | |
Paolo Bonzini | b65d6e1 | 2014-11-21 18:13:26 +0100 | [diff] [blame] | 499 | /* cpuid 0xD.1.eax */ |
Huaitong Han | e0b18ef | 2016-03-22 16:51:14 +0800 | [diff] [blame] | 500 | const u32 kvm_cpuid_D_1_eax_x86_features = |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 501 | F(XSAVEOPT) | F(XSAVEC) | F(XGETBV1) | f_xsaves; |
Paolo Bonzini | b65d6e1 | 2014-11-21 18:13:26 +0100 | [diff] [blame] | 502 | |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 503 | /* all calls to cpuid_count() should be made on the same cpu */ |
| 504 | get_cpu(); |
Sasha Levin | 831bf66 | 2011-11-28 11:20:29 +0200 | [diff] [blame] | 505 | |
| 506 | r = -E2BIG; |
| 507 | |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 508 | entry = do_host_cpuid(array, function, 0); |
| 509 | if (WARN_ON(!entry)) |
Sasha Levin | 831bf66 | 2011-11-28 11:20:29 +0200 | [diff] [blame] | 510 | goto out; |
| 511 | |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 512 | switch (function) { |
| 513 | case 0: |
Like Xu | a87f2d3 | 2019-06-06 09:18:45 +0800 | [diff] [blame] | 514 | /* Limited to the highest leaf implemented in KVM. */ |
| 515 | entry->eax = min(entry->eax, 0x1fU); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 516 | break; |
| 517 | case 1: |
Huaitong Han | e0b18ef | 2016-03-22 16:51:14 +0800 | [diff] [blame] | 518 | entry->edx &= kvm_cpuid_1_edx_x86_features; |
| 519 | cpuid_mask(&entry->edx, CPUID_1_EDX); |
| 520 | entry->ecx &= kvm_cpuid_1_ecx_x86_features; |
| 521 | cpuid_mask(&entry->ecx, CPUID_1_ECX); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 522 | /* we support x2apic emulation even if host does not support |
| 523 | * it since we emulate x2apic in software */ |
| 524 | entry->ecx |= F(X2APIC); |
| 525 | break; |
| 526 | /* function 2 entries are STATEFUL. That is, repeated cpuid commands |
| 527 | * may return different values. This forces us to get_cpu() before |
| 528 | * issuing the first command, and also to emulate this annoying behavior |
| 529 | * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */ |
Sean Christopherson | 74fa0bc | 2020-03-02 15:56:17 -0800 | [diff] [blame] | 530 | case 2: |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 531 | entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT; |
Sean Christopherson | 74fa0bc | 2020-03-02 15:56:17 -0800 | [diff] [blame] | 532 | |
| 533 | for (i = 1, max_idx = entry->eax & 0xff; i < max_idx; ++i) { |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 534 | entry = do_host_cpuid(array, function, 0); |
| 535 | if (!entry) |
Sasha Levin | 831bf66 | 2011-11-28 11:20:29 +0200 | [diff] [blame] | 536 | goto out; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 537 | } |
| 538 | break; |
Jim Mattson | 32a243d | 2019-03-27 13:15:36 -0700 | [diff] [blame] | 539 | /* functions 4 and 0x8000001d have additional index. */ |
| 540 | case 4: |
Sean Christopherson | c862903 | 2020-03-02 15:56:18 -0800 | [diff] [blame] | 541 | case 0x8000001d: |
| 542 | /* |
| 543 | * Read entries until the cache type in the previous entry is |
| 544 | * zero, i.e. indicates an invalid entry. |
| 545 | */ |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 546 | for (i = 1; entry->eax & 0x1f; ++i) { |
| 547 | entry = do_host_cpuid(array, function, i); |
| 548 | if (!entry) |
Sean Christopherson | 0fc6267 | 2020-03-02 15:56:08 -0800 | [diff] [blame] | 549 | goto out; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 550 | } |
| 551 | break; |
Jan Kiszka | e453aa0 | 2015-05-24 17:22:38 +0200 | [diff] [blame] | 552 | case 6: /* Thermal management */ |
| 553 | entry->eax = 0x4; /* allow ARAT */ |
| 554 | entry->ebx = 0; |
| 555 | entry->ecx = 0; |
| 556 | entry->edx = 0; |
| 557 | break; |
Paolo Bonzini | 54d360d | 2019-07-04 12:18:13 +0200 | [diff] [blame] | 558 | /* function 7 has additional index. */ |
Sean Christopherson | 74fa0bc | 2020-03-02 15:56:17 -0800 | [diff] [blame] | 559 | case 7: |
Sean Christopherson | aceac6e | 2020-03-02 15:56:14 -0800 | [diff] [blame] | 560 | do_cpuid_7_mask(entry); |
Sean Christopherson | 87849b1 | 2020-03-02 15:56:13 -0800 | [diff] [blame] | 561 | |
Sean Christopherson | 74fa0bc | 2020-03-02 15:56:17 -0800 | [diff] [blame] | 562 | for (i = 1, max_idx = entry->eax; i <= max_idx; i++) { |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 563 | entry = do_host_cpuid(array, function, i); |
| 564 | if (!entry) |
Paolo Bonzini | 54d360d | 2019-07-04 12:18:13 +0200 | [diff] [blame] | 565 | goto out; |
| 566 | |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 567 | do_cpuid_7_mask(entry); |
Paolo Bonzini | 54d360d | 2019-07-04 12:18:13 +0200 | [diff] [blame] | 568 | } |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 569 | break; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 570 | case 9: |
| 571 | break; |
Gleb Natapov | a6c06ed | 2011-11-10 14:57:28 +0200 | [diff] [blame] | 572 | case 0xa: { /* Architectural Performance Monitoring */ |
| 573 | struct x86_pmu_capability cap; |
| 574 | union cpuid10_eax eax; |
| 575 | union cpuid10_edx edx; |
| 576 | |
| 577 | perf_get_x86_pmu_capability(&cap); |
| 578 | |
| 579 | /* |
| 580 | * Only support guest architectural pmu on a host |
| 581 | * with architectural pmu. |
| 582 | */ |
| 583 | if (!cap.version) |
| 584 | memset(&cap, 0, sizeof(cap)); |
| 585 | |
| 586 | eax.split.version_id = min(cap.version, 2); |
| 587 | eax.split.num_counters = cap.num_counters_gp; |
| 588 | eax.split.bit_width = cap.bit_width_gp; |
| 589 | eax.split.mask_length = cap.events_mask_len; |
| 590 | |
| 591 | edx.split.num_counters_fixed = cap.num_counters_fixed; |
| 592 | edx.split.bit_width_fixed = cap.bit_width_fixed; |
| 593 | edx.split.reserved = 0; |
| 594 | |
| 595 | entry->eax = eax.full; |
| 596 | entry->ebx = cap.events_mask; |
| 597 | entry->ecx = 0; |
| 598 | entry->edx = edx.full; |
| 599 | break; |
| 600 | } |
Like Xu | a87f2d3 | 2019-06-06 09:18:45 +0800 | [diff] [blame] | 601 | /* |
| 602 | * Per Intel's SDM, the 0x1f is a superset of 0xb, |
| 603 | * thus they can be handled by common code. |
| 604 | */ |
| 605 | case 0x1f: |
Sean Christopherson | 74fa0bc | 2020-03-02 15:56:17 -0800 | [diff] [blame] | 606 | case 0xb: |
Jim Mattson | a1a640b | 2019-09-25 11:17:14 -0700 | [diff] [blame] | 607 | /* |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 608 | * Populate entries until the level type (ECX[15:8]) of the |
| 609 | * previous entry is zero. Note, CPUID EAX.{0x1f,0xb}.0 is |
| 610 | * the starting entry, filled by the primary do_host_cpuid(). |
Jim Mattson | a1a640b | 2019-09-25 11:17:14 -0700 | [diff] [blame] | 611 | */ |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 612 | for (i = 1; entry->ecx & 0xff00; ++i) { |
| 613 | entry = do_host_cpuid(array, function, i); |
| 614 | if (!entry) |
Sasha Levin | 831bf66 | 2011-11-28 11:20:29 +0200 | [diff] [blame] | 615 | goto out; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 616 | } |
| 617 | break; |
Sean Christopherson | cfc4818 | 2020-03-02 15:56:23 -0800 | [diff] [blame] | 618 | case 0xd: |
| 619 | entry->eax &= supported_xcr0; |
| 620 | entry->ebx = xstate_required_size(supported_xcr0, false); |
Radim Krčmář | e08e833 | 2014-12-04 18:30:41 +0100 | [diff] [blame] | 621 | entry->ecx = entry->ebx; |
Sean Christopherson | cfc4818 | 2020-03-02 15:56:23 -0800 | [diff] [blame] | 622 | entry->edx &= supported_xcr0 >> 32; |
| 623 | if (!supported_xcr0) |
Paolo Bonzini | b65d6e1 | 2014-11-21 18:13:26 +0100 | [diff] [blame] | 624 | break; |
| 625 | |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 626 | entry = do_host_cpuid(array, function, 1); |
| 627 | if (!entry) |
Sean Christopherson | 3dc4a9c | 2020-03-02 15:56:09 -0800 | [diff] [blame] | 628 | goto out; |
| 629 | |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 630 | entry->eax &= kvm_cpuid_D_1_eax_x86_features; |
| 631 | cpuid_mask(&entry->eax, CPUID_D_1_EAX); |
| 632 | if (entry->eax & (F(XSAVES)|F(XSAVEC))) |
Sean Christopherson | cfc4818 | 2020-03-02 15:56:23 -0800 | [diff] [blame] | 633 | entry->ebx = xstate_required_size(supported_xcr0, true); |
Sean Christopherson | 3dc4a9c | 2020-03-02 15:56:09 -0800 | [diff] [blame] | 634 | else |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 635 | entry->ebx = 0; |
Sean Christopherson | 3dc4a9c | 2020-03-02 15:56:09 -0800 | [diff] [blame] | 636 | /* Saving XSS controlled state via XSAVES isn't supported. */ |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 637 | entry->ecx = 0; |
| 638 | entry->edx = 0; |
Sean Christopherson | 3dc4a9c | 2020-03-02 15:56:09 -0800 | [diff] [blame] | 639 | |
Sean Christopherson | 0eee8f9 | 2020-03-02 15:56:21 -0800 | [diff] [blame] | 640 | for (i = 2; i < 64; ++i) { |
Sean Christopherson | cfc4818 | 2020-03-02 15:56:23 -0800 | [diff] [blame] | 641 | if (!(supported_xcr0 & BIT_ULL(i))) |
Sean Christopherson | 1893c94 | 2020-03-02 15:56:10 -0800 | [diff] [blame] | 642 | continue; |
Sean Christopherson | 3dc4a9c | 2020-03-02 15:56:09 -0800 | [diff] [blame] | 643 | |
Sean Christopherson | 0eee8f9 | 2020-03-02 15:56:21 -0800 | [diff] [blame] | 644 | entry = do_host_cpuid(array, function, i); |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 645 | if (!entry) |
Sasha Levin | 831bf66 | 2011-11-28 11:20:29 +0200 | [diff] [blame] | 646 | goto out; |
| 647 | |
Sean Christopherson | 91001d4 | 2020-03-02 15:56:11 -0800 | [diff] [blame] | 648 | /* |
Sean Christopherson | cfc4818 | 2020-03-02 15:56:23 -0800 | [diff] [blame] | 649 | * The supported check above should have filtered out |
Sean Christopherson | 91001d4 | 2020-03-02 15:56:11 -0800 | [diff] [blame] | 650 | * invalid sub-leafs as well as sub-leafs managed by |
| 651 | * IA32_XSS MSR. Only XCR0-managed sub-leafs should |
| 652 | * reach this point, and they should have a non-zero |
| 653 | * save state size. |
| 654 | */ |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 655 | if (WARN_ON_ONCE(!entry->eax || (entry->ecx & 1))) { |
| 656 | --array->nent; |
Sean Christopherson | 3dc4a9c | 2020-03-02 15:56:09 -0800 | [diff] [blame] | 657 | continue; |
Sean Christopherson | 8b2fc44 | 2020-03-02 15:56:12 -0800 | [diff] [blame] | 658 | } |
Sean Christopherson | 91001d4 | 2020-03-02 15:56:11 -0800 | [diff] [blame] | 659 | |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 660 | entry->ecx = 0; |
| 661 | entry->edx = 0; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 662 | } |
| 663 | break; |
Chao Peng | 86f5201 | 2018-10-24 16:05:11 +0800 | [diff] [blame] | 664 | /* Intel PT */ |
Sean Christopherson | 74fa0bc | 2020-03-02 15:56:17 -0800 | [diff] [blame] | 665 | case 0x14: |
Sean Christopherson | 7392079 | 2020-03-02 15:56:26 -0800 | [diff] [blame^] | 666 | if (!f_intel_pt) { |
| 667 | entry->eax = entry->ebx = entry->ecx = entry->edx = 0; |
Chao Peng | 86f5201 | 2018-10-24 16:05:11 +0800 | [diff] [blame] | 668 | break; |
Sean Christopherson | 7392079 | 2020-03-02 15:56:26 -0800 | [diff] [blame^] | 669 | } |
Chao Peng | 86f5201 | 2018-10-24 16:05:11 +0800 | [diff] [blame] | 670 | |
Sean Christopherson | 74fa0bc | 2020-03-02 15:56:17 -0800 | [diff] [blame] | 671 | for (i = 1, max_idx = entry->eax; i <= max_idx; ++i) { |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 672 | if (!do_host_cpuid(array, function, i)) |
Chao Peng | 86f5201 | 2018-10-24 16:05:11 +0800 | [diff] [blame] | 673 | goto out; |
Chao Peng | 86f5201 | 2018-10-24 16:05:11 +0800 | [diff] [blame] | 674 | } |
| 675 | break; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 676 | case KVM_CPUID_SIGNATURE: { |
Mathias Krause | 326d07c | 2012-08-30 01:30:13 +0200 | [diff] [blame] | 677 | static const char signature[12] = "KVMKVMKVM\0\0"; |
| 678 | const u32 *sigptr = (const u32 *)signature; |
Michael S. Tsirkin | 57c22e5 | 2012-05-02 17:55:56 +0300 | [diff] [blame] | 679 | entry->eax = KVM_CPUID_FEATURES; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 680 | entry->ebx = sigptr[0]; |
| 681 | entry->ecx = sigptr[1]; |
| 682 | entry->edx = sigptr[2]; |
| 683 | break; |
| 684 | } |
| 685 | case KVM_CPUID_FEATURES: |
| 686 | entry->eax = (1 << KVM_FEATURE_CLOCKSOURCE) | |
| 687 | (1 << KVM_FEATURE_NOP_IO_DELAY) | |
| 688 | (1 << KVM_FEATURE_CLOCKSOURCE2) | |
| 689 | (1 << KVM_FEATURE_ASYNC_PF) | |
Michael S. Tsirkin | ae7a2a3 | 2012-06-24 19:25:07 +0300 | [diff] [blame] | 690 | (1 << KVM_FEATURE_PV_EOI) | |
Srivatsa Vaddagiri | 6aef266 | 2013-08-26 14:18:34 +0530 | [diff] [blame] | 691 | (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT) | |
Wanpeng Li | f38a7b7 | 2017-12-12 17:33:04 -0800 | [diff] [blame] | 692 | (1 << KVM_FEATURE_PV_UNHALT) | |
Radim Krčmář | fe2a302 | 2018-02-01 22:16:21 +0100 | [diff] [blame] | 693 | (1 << KVM_FEATURE_PV_TLB_FLUSH) | |
Wanpeng Li | 4180bf1 | 2018-07-23 14:39:54 +0800 | [diff] [blame] | 694 | (1 << KVM_FEATURE_ASYNC_PF_VMEXIT) | |
Marcelo Tosatti | 2d5ba19 | 2019-06-03 19:52:44 -0300 | [diff] [blame] | 695 | (1 << KVM_FEATURE_PV_SEND_IPI) | |
Wanpeng Li | 32b72ec | 2019-06-11 20:23:50 +0800 | [diff] [blame] | 696 | (1 << KVM_FEATURE_POLL_CONTROL) | |
| 697 | (1 << KVM_FEATURE_PV_SCHED_YIELD); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 698 | |
| 699 | if (sched_info_on()) |
| 700 | entry->eax |= (1 << KVM_FEATURE_STEAL_TIME); |
| 701 | |
| 702 | entry->ebx = 0; |
| 703 | entry->ecx = 0; |
| 704 | entry->edx = 0; |
| 705 | break; |
| 706 | case 0x80000000: |
Brijesh Singh | 8765d75 | 2017-12-04 10:57:25 -0600 | [diff] [blame] | 707 | entry->eax = min(entry->eax, 0x8000001f); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 708 | break; |
| 709 | case 0x80000001: |
Huaitong Han | e0b18ef | 2016-03-22 16:51:14 +0800 | [diff] [blame] | 710 | entry->edx &= kvm_cpuid_8000_0001_edx_x86_features; |
| 711 | cpuid_mask(&entry->edx, CPUID_8000_0001_EDX); |
| 712 | entry->ecx &= kvm_cpuid_8000_0001_ecx_x86_features; |
| 713 | cpuid_mask(&entry->ecx, CPUID_8000_0001_ECX); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 714 | break; |
Marcelo Tosatti | e4c9a5a1 | 2014-04-26 22:30:23 -0300 | [diff] [blame] | 715 | case 0x80000007: /* Advanced power management */ |
| 716 | /* invariant TSC is CPUID.80000007H:EDX[8] */ |
| 717 | entry->edx &= (1 << 8); |
| 718 | /* mask against host */ |
| 719 | entry->edx &= boot_cpu_data.x86_power; |
| 720 | entry->eax = entry->ebx = entry->ecx = 0; |
| 721 | break; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 722 | case 0x80000008: { |
| 723 | unsigned g_phys_as = (entry->eax >> 16) & 0xff; |
| 724 | unsigned virt_as = max((entry->eax >> 8) & 0xff, 48U); |
| 725 | unsigned phys_as = entry->eax & 0xff; |
| 726 | |
| 727 | if (!g_phys_as) |
| 728 | g_phys_as = phys_as; |
| 729 | entry->eax = g_phys_as | (virt_as << 8); |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 730 | entry->edx = 0; |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 731 | entry->ebx &= kvm_cpuid_8000_0008_ebx_x86_features; |
| 732 | cpuid_mask(&entry->ebx, CPUID_8000_0008_EBX); |
Konrad Rzeszutek Wilk | 6ac2f49 | 2018-06-01 10:59:20 -0400 | [diff] [blame] | 733 | /* |
Paolo Bonzini | 4c6903a | 2019-08-14 12:07:34 -0400 | [diff] [blame] | 734 | * AMD has separate bits for each SPEC_CTRL bit. |
| 735 | * arch/x86/kernel/cpu/bugs.c is kind enough to |
| 736 | * record that in cpufeatures so use them. |
| 737 | */ |
| 738 | if (boot_cpu_has(X86_FEATURE_IBPB)) |
| 739 | entry->ebx |= F(AMD_IBPB); |
| 740 | if (boot_cpu_has(X86_FEATURE_IBRS)) |
| 741 | entry->ebx |= F(AMD_IBRS); |
| 742 | if (boot_cpu_has(X86_FEATURE_STIBP)) |
| 743 | entry->ebx |= F(AMD_STIBP); |
Sean Christopherson | acfad33 | 2020-03-02 15:56:15 -0800 | [diff] [blame] | 744 | if (boot_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD)) |
Paolo Bonzini | 4c6903a | 2019-08-14 12:07:34 -0400 | [diff] [blame] | 745 | entry->ebx |= F(AMD_SSBD); |
| 746 | if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS)) |
| 747 | entry->ebx |= F(AMD_SSB_NO); |
| 748 | /* |
Konrad Rzeszutek Wilk | 6ac2f49 | 2018-06-01 10:59:20 -0400 | [diff] [blame] | 749 | * The preference is to use SPEC CTRL MSR instead of the |
| 750 | * VIRT_SPEC MSR. |
| 751 | */ |
| 752 | if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) && |
| 753 | !boot_cpu_has(X86_FEATURE_AMD_SSBD)) |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 754 | entry->ebx |= F(VIRT_SSBD); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 755 | break; |
| 756 | } |
| 757 | case 0x80000019: |
| 758 | entry->ecx = entry->edx = 0; |
| 759 | break; |
| 760 | case 0x8000001a: |
Jim Mattson | 382409b | 2019-03-27 13:15:37 -0700 | [diff] [blame] | 761 | case 0x8000001e: |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 762 | break; |
Peter Gonda | c1de0f2 | 2019-11-21 12:33:43 -0800 | [diff] [blame] | 763 | /* Support memory encryption cpuid if host supports it */ |
| 764 | case 0x8000001F: |
| 765 | if (!boot_cpu_has(X86_FEATURE_SEV)) |
| 766 | entry->eax = entry->ebx = entry->ecx = entry->edx = 0; |
| 767 | break; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 768 | /*Add support for Centaur's CPUID instruction*/ |
| 769 | case 0xC0000000: |
| 770 | /*Just support up to 0xC0000004 now*/ |
| 771 | entry->eax = min(entry->eax, 0xC0000004); |
| 772 | break; |
| 773 | case 0xC0000001: |
Huaitong Han | e0b18ef | 2016-03-22 16:51:14 +0800 | [diff] [blame] | 774 | entry->edx &= kvm_cpuid_C000_0001_edx_x86_features; |
| 775 | cpuid_mask(&entry->edx, CPUID_C000_0001_EDX); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 776 | break; |
| 777 | case 3: /* Processor serial number */ |
| 778 | case 5: /* MONITOR/MWAIT */ |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 779 | case 0xC0000002: |
| 780 | case 0xC0000003: |
| 781 | case 0xC0000004: |
| 782 | default: |
| 783 | entry->eax = entry->ebx = entry->ecx = entry->edx = 0; |
| 784 | break; |
| 785 | } |
| 786 | |
| 787 | kvm_x86_ops->set_supported_cpuid(function, entry); |
| 788 | |
Sasha Levin | 831bf66 | 2011-11-28 11:20:29 +0200 | [diff] [blame] | 789 | r = 0; |
| 790 | |
| 791 | out: |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 792 | put_cpu(); |
Sasha Levin | 831bf66 | 2011-11-28 11:20:29 +0200 | [diff] [blame] | 793 | |
| 794 | return r; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 795 | } |
| 796 | |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 797 | static int do_cpuid_func(struct kvm_cpuid_array *array, u32 func, |
| 798 | unsigned int type) |
Borislav Petkov | 9c15bb1 | 2013-09-22 16:44:50 +0200 | [diff] [blame] | 799 | { |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 800 | if (array->nent >= array->maxnent) |
Paolo Bonzini | 433f4ba | 2019-12-04 10:28:54 +0100 | [diff] [blame] | 801 | return -E2BIG; |
| 802 | |
Borislav Petkov | 9c15bb1 | 2013-09-22 16:44:50 +0200 | [diff] [blame] | 803 | if (type == KVM_GET_EMULATED_CPUID) |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 804 | return __do_cpuid_func_emulated(array, func); |
Borislav Petkov | 9c15bb1 | 2013-09-22 16:44:50 +0200 | [diff] [blame] | 805 | |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 806 | return __do_cpuid_func(array, func); |
Borislav Petkov | 9c15bb1 | 2013-09-22 16:44:50 +0200 | [diff] [blame] | 807 | } |
| 808 | |
Sean Christopherson | 8b86079 | 2020-03-02 15:56:06 -0800 | [diff] [blame] | 809 | #define CENTAUR_CPUID_SIGNATURE 0xC0000000 |
Sasha Levin | 831bf66 | 2011-11-28 11:20:29 +0200 | [diff] [blame] | 810 | |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 811 | static int get_cpuid_func(struct kvm_cpuid_array *array, u32 func, |
| 812 | unsigned int type) |
Sean Christopherson | 619a17f | 2020-03-02 15:56:05 -0800 | [diff] [blame] | 813 | { |
| 814 | u32 limit; |
| 815 | int r; |
| 816 | |
Sean Christopherson | 8b86079 | 2020-03-02 15:56:06 -0800 | [diff] [blame] | 817 | if (func == CENTAUR_CPUID_SIGNATURE && |
| 818 | boot_cpu_data.x86_vendor != X86_VENDOR_CENTAUR) |
| 819 | return 0; |
| 820 | |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 821 | r = do_cpuid_func(array, func, type); |
Sean Christopherson | 619a17f | 2020-03-02 15:56:05 -0800 | [diff] [blame] | 822 | if (r) |
| 823 | return r; |
| 824 | |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 825 | limit = array->entries[array->nent - 1].eax; |
Sean Christopherson | 619a17f | 2020-03-02 15:56:05 -0800 | [diff] [blame] | 826 | for (func = func + 1; func <= limit; ++func) { |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 827 | r = do_cpuid_func(array, func, type); |
Sean Christopherson | 619a17f | 2020-03-02 15:56:05 -0800 | [diff] [blame] | 828 | if (r) |
| 829 | break; |
| 830 | } |
| 831 | |
| 832 | return r; |
| 833 | } |
| 834 | |
Borislav Petkov | 9c15bb1 | 2013-09-22 16:44:50 +0200 | [diff] [blame] | 835 | static bool sanity_check_entries(struct kvm_cpuid_entry2 __user *entries, |
| 836 | __u32 num_entries, unsigned int ioctl_type) |
| 837 | { |
| 838 | int i; |
Borislav Petkov | 1b2ca42 | 2013-11-06 15:46:02 +0100 | [diff] [blame] | 839 | __u32 pad[3]; |
Borislav Petkov | 9c15bb1 | 2013-09-22 16:44:50 +0200 | [diff] [blame] | 840 | |
| 841 | if (ioctl_type != KVM_GET_EMULATED_CPUID) |
| 842 | return false; |
| 843 | |
| 844 | /* |
| 845 | * We want to make sure that ->padding is being passed clean from |
| 846 | * userspace in case we want to use it for something in the future. |
| 847 | * |
| 848 | * Sadly, this wasn't enforced for KVM_GET_SUPPORTED_CPUID and so we |
| 849 | * have to give ourselves satisfied only with the emulated side. /me |
| 850 | * sheds a tear. |
| 851 | */ |
| 852 | for (i = 0; i < num_entries; i++) { |
Borislav Petkov | 1b2ca42 | 2013-11-06 15:46:02 +0100 | [diff] [blame] | 853 | if (copy_from_user(pad, entries[i].padding, sizeof(pad))) |
| 854 | return true; |
| 855 | |
| 856 | if (pad[0] || pad[1] || pad[2]) |
Borislav Petkov | 9c15bb1 | 2013-09-22 16:44:50 +0200 | [diff] [blame] | 857 | return true; |
| 858 | } |
| 859 | return false; |
| 860 | } |
| 861 | |
| 862 | int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid, |
| 863 | struct kvm_cpuid_entry2 __user *entries, |
| 864 | unsigned int type) |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 865 | { |
Sean Christopherson | 8b86079 | 2020-03-02 15:56:06 -0800 | [diff] [blame] | 866 | static const u32 funcs[] = { |
| 867 | 0, 0x80000000, CENTAUR_CPUID_SIGNATURE, KVM_CPUID_SIGNATURE, |
Sasha Levin | 831bf66 | 2011-11-28 11:20:29 +0200 | [diff] [blame] | 868 | }; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 869 | |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 870 | struct kvm_cpuid_array array = { |
| 871 | .nent = 0, |
| 872 | .maxnent = cpuid->nent, |
| 873 | }; |
| 874 | int r, i; |
Sean Christopherson | d5a661d | 2020-03-02 15:56:07 -0800 | [diff] [blame] | 875 | |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 876 | if (cpuid->nent < 1) |
Sean Christopherson | d5a661d | 2020-03-02 15:56:07 -0800 | [diff] [blame] | 877 | return -E2BIG; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 878 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) |
| 879 | cpuid->nent = KVM_MAX_CPUID_ENTRIES; |
Borislav Petkov | 9c15bb1 | 2013-09-22 16:44:50 +0200 | [diff] [blame] | 880 | |
| 881 | if (sanity_check_entries(entries, cpuid->nent, type)) |
| 882 | return -EINVAL; |
| 883 | |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 884 | array.entries = vzalloc(array_size(sizeof(struct kvm_cpuid_entry2), |
Kees Cook | fad953c | 2018-06-12 14:27:37 -0700 | [diff] [blame] | 885 | cpuid->nent)); |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 886 | if (!array.entries) |
Sean Christopherson | d5a661d | 2020-03-02 15:56:07 -0800 | [diff] [blame] | 887 | return -ENOMEM; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 888 | |
Sean Christopherson | 8b86079 | 2020-03-02 15:56:06 -0800 | [diff] [blame] | 889 | for (i = 0; i < ARRAY_SIZE(funcs); i++) { |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 890 | r = get_cpuid_func(&array, funcs[i], type); |
Sasha Levin | 831bf66 | 2011-11-28 11:20:29 +0200 | [diff] [blame] | 891 | if (r) |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 892 | goto out_free; |
| 893 | } |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 894 | cpuid->nent = array.nent; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 895 | |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 896 | if (copy_to_user(entries, array.entries, |
| 897 | array.nent * sizeof(struct kvm_cpuid_entry2))) |
Sean Christopherson | d5a661d | 2020-03-02 15:56:07 -0800 | [diff] [blame] | 898 | r = -EFAULT; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 899 | |
| 900 | out_free: |
Sean Christopherson | e53c95e | 2020-03-02 15:56:19 -0800 | [diff] [blame] | 901 | vfree(array.entries); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 902 | return r; |
| 903 | } |
| 904 | |
| 905 | static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i) |
| 906 | { |
| 907 | struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i]; |
Wanpeng Li | a364163 | 2017-06-08 01:22:07 -0700 | [diff] [blame] | 908 | struct kvm_cpuid_entry2 *ej; |
| 909 | int j = i; |
| 910 | int nent = vcpu->arch.cpuid_nent; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 911 | |
| 912 | e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT; |
| 913 | /* when no next entry is found, the current entry[i] is reselected */ |
Wanpeng Li | a364163 | 2017-06-08 01:22:07 -0700 | [diff] [blame] | 914 | do { |
| 915 | j = (j + 1) % nent; |
| 916 | ej = &vcpu->arch.cpuid_entries[j]; |
| 917 | } while (ej->function != e->function); |
| 918 | |
| 919 | ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT; |
| 920 | |
| 921 | return j; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | /* find an entry with matching function, matching index (if needed), and that |
| 925 | * should be read next (if it's stateful) */ |
| 926 | static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e, |
| 927 | u32 function, u32 index) |
| 928 | { |
| 929 | if (e->function != function) |
| 930 | return 0; |
| 931 | if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index) |
| 932 | return 0; |
| 933 | if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) && |
| 934 | !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT)) |
| 935 | return 0; |
| 936 | return 1; |
| 937 | } |
| 938 | |
| 939 | struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu, |
| 940 | u32 function, u32 index) |
| 941 | { |
| 942 | int i; |
| 943 | struct kvm_cpuid_entry2 *best = NULL; |
| 944 | |
| 945 | for (i = 0; i < vcpu->arch.cpuid_nent; ++i) { |
| 946 | struct kvm_cpuid_entry2 *e; |
| 947 | |
| 948 | e = &vcpu->arch.cpuid_entries[i]; |
| 949 | if (is_matching_cpuid_entry(e, function, index)) { |
| 950 | if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) |
| 951 | move_to_next_stateful_cpuid_entry(vcpu, i); |
| 952 | best = e; |
| 953 | break; |
| 954 | } |
| 955 | } |
| 956 | return best; |
| 957 | } |
| 958 | EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry); |
| 959 | |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 960 | /* |
Jim Mattson | 4356112 | 2019-09-25 17:04:17 -0700 | [diff] [blame] | 961 | * If the basic or extended CPUID leaf requested is higher than the |
| 962 | * maximum supported basic or extended leaf, respectively, then it is |
| 963 | * out of range. |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 964 | */ |
Jim Mattson | 4356112 | 2019-09-25 17:04:17 -0700 | [diff] [blame] | 965 | static bool cpuid_function_in_range(struct kvm_vcpu *vcpu, u32 function) |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 966 | { |
Jim Mattson | 4356112 | 2019-09-25 17:04:17 -0700 | [diff] [blame] | 967 | struct kvm_cpuid_entry2 *max; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 968 | |
Jim Mattson | 4356112 | 2019-09-25 17:04:17 -0700 | [diff] [blame] | 969 | max = kvm_find_cpuid_entry(vcpu, function & 0x80000000, 0); |
| 970 | return max && function <= max->eax; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 971 | } |
| 972 | |
Yu Zhang | e911eb3 | 2017-08-24 20:27:52 +0800 | [diff] [blame] | 973 | bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, |
| 974 | u32 *ecx, u32 *edx, bool check_limit) |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 975 | { |
Avi Kivity | 62046e5 | 2012-06-07 14:07:48 +0300 | [diff] [blame] | 976 | u32 function = *eax, index = *ecx; |
Jim Mattson | 4356112 | 2019-09-25 17:04:17 -0700 | [diff] [blame] | 977 | struct kvm_cpuid_entry2 *entry; |
| 978 | struct kvm_cpuid_entry2 *max; |
| 979 | bool found; |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 980 | |
Jim Mattson | 4356112 | 2019-09-25 17:04:17 -0700 | [diff] [blame] | 981 | entry = kvm_find_cpuid_entry(vcpu, function, index); |
| 982 | found = entry; |
| 983 | /* |
| 984 | * Intel CPUID semantics treats any query for an out-of-range |
| 985 | * leaf as if the highest basic leaf (i.e. CPUID.0H:EAX) were |
Jim Mattson | 5f41a37 | 2019-09-25 17:04:18 -0700 | [diff] [blame] | 986 | * requested. AMD CPUID semantics returns all zeroes for any |
| 987 | * undefined leaf, whether or not the leaf is in range. |
Jim Mattson | 4356112 | 2019-09-25 17:04:17 -0700 | [diff] [blame] | 988 | */ |
Jim Mattson | 5f41a37 | 2019-09-25 17:04:18 -0700 | [diff] [blame] | 989 | if (!entry && check_limit && !guest_cpuid_is_amd(vcpu) && |
| 990 | !cpuid_function_in_range(vcpu, function)) { |
Jim Mattson | 4356112 | 2019-09-25 17:04:17 -0700 | [diff] [blame] | 991 | max = kvm_find_cpuid_entry(vcpu, 0, 0); |
| 992 | if (max) { |
| 993 | function = max->eax; |
| 994 | entry = kvm_find_cpuid_entry(vcpu, function, index); |
| 995 | } |
Yu Zhang | e911eb3 | 2017-08-24 20:27:52 +0800 | [diff] [blame] | 996 | } |
Jim Mattson | 4356112 | 2019-09-25 17:04:17 -0700 | [diff] [blame] | 997 | if (entry) { |
| 998 | *eax = entry->eax; |
| 999 | *ebx = entry->ebx; |
| 1000 | *ecx = entry->ecx; |
| 1001 | *edx = entry->edx; |
Paolo Bonzini | edef5c3 | 2019-11-18 12:23:00 -0500 | [diff] [blame] | 1002 | if (function == 7 && index == 0) { |
| 1003 | u64 data; |
| 1004 | if (!__kvm_get_msr(vcpu, MSR_IA32_TSX_CTRL, &data, true) && |
| 1005 | (data & TSX_CTRL_CPUID_CLEAR)) |
| 1006 | *ebx &= ~(F(RTM) | F(HLE)); |
| 1007 | } |
Jim Mattson | 4356112 | 2019-09-25 17:04:17 -0700 | [diff] [blame] | 1008 | } else { |
Avi Kivity | 62046e5 | 2012-06-07 14:07:48 +0300 | [diff] [blame] | 1009 | *eax = *ebx = *ecx = *edx = 0; |
Jim Mattson | 4356112 | 2019-09-25 17:04:17 -0700 | [diff] [blame] | 1010 | /* |
| 1011 | * When leaf 0BH or 1FH is defined, CL is pass-through |
| 1012 | * and EDX is always the x2APIC ID, even for undefined |
| 1013 | * subleaves. Index 1 will exist iff the leaf is |
| 1014 | * implemented, so we pass through CL iff leaf 1 |
| 1015 | * exists. EDX can be copied from any existing index. |
| 1016 | */ |
| 1017 | if (function == 0xb || function == 0x1f) { |
| 1018 | entry = kvm_find_cpuid_entry(vcpu, function, 1); |
| 1019 | if (entry) { |
| 1020 | *ecx = index & 0xff; |
| 1021 | *edx = entry->edx; |
| 1022 | } |
| 1023 | } |
| 1024 | } |
| 1025 | trace_kvm_cpuid(function, *eax, *ebx, *ecx, *edx, found); |
| 1026 | return found; |
Avi Kivity | 62046e5 | 2012-06-07 14:07:48 +0300 | [diff] [blame] | 1027 | } |
Julian Stecklina | 66f7b72 | 2012-12-05 15:26:19 +0100 | [diff] [blame] | 1028 | EXPORT_SYMBOL_GPL(kvm_cpuid); |
Avi Kivity | 62046e5 | 2012-06-07 14:07:48 +0300 | [diff] [blame] | 1029 | |
Kyle Huey | 6a908b6 | 2016-11-29 12:40:37 -0800 | [diff] [blame] | 1030 | int kvm_emulate_cpuid(struct kvm_vcpu *vcpu) |
Avi Kivity | 62046e5 | 2012-06-07 14:07:48 +0300 | [diff] [blame] | 1031 | { |
Jiang Biao | 1e13175 | 2016-11-07 08:55:49 +0800 | [diff] [blame] | 1032 | u32 eax, ebx, ecx, edx; |
Avi Kivity | 62046e5 | 2012-06-07 14:07:48 +0300 | [diff] [blame] | 1033 | |
Kyle Huey | db2336a | 2017-03-20 01:16:28 -0700 | [diff] [blame] | 1034 | if (cpuid_fault_enabled(vcpu) && !kvm_require_cpl(vcpu, 0)) |
| 1035 | return 1; |
| 1036 | |
Sean Christopherson | de3cd11 | 2019-04-30 10:36:17 -0700 | [diff] [blame] | 1037 | eax = kvm_rax_read(vcpu); |
| 1038 | ecx = kvm_rcx_read(vcpu); |
Yu Zhang | e911eb3 | 2017-08-24 20:27:52 +0800 | [diff] [blame] | 1039 | kvm_cpuid(vcpu, &eax, &ebx, &ecx, &edx, true); |
Sean Christopherson | de3cd11 | 2019-04-30 10:36:17 -0700 | [diff] [blame] | 1040 | kvm_rax_write(vcpu, eax); |
| 1041 | kvm_rbx_write(vcpu, ebx); |
| 1042 | kvm_rcx_write(vcpu, ecx); |
| 1043 | kvm_rdx_write(vcpu, edx); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 1044 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1045 | } |
| 1046 | EXPORT_SYMBOL_GPL(kvm_emulate_cpuid); |