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