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