License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[linux-block.git] / arch / x86 / kvm / cpuid.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
00b27a3e
AK
2#ifndef ARCH_X86_KVM_CPUID_H
3#define ARCH_X86_KVM_CPUID_H
4
5#include "x86.h"
91713faf 6#include <asm/cpu.h>
d6321d49 7#include <asm/processor.h>
00b27a3e 8
dd598091 9int kvm_update_cpuid(struct kvm_vcpu *vcpu);
a87036ad 10bool kvm_mpx_supported(void);
00b27a3e
AK
11struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
12 u32 function, u32 index);
9c15bb1d
BP
13int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
14 struct kvm_cpuid_entry2 __user *entries,
15 unsigned int type);
00b27a3e
AK
16int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
17 struct kvm_cpuid *cpuid,
18 struct kvm_cpuid_entry __user *entries);
19int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
20 struct kvm_cpuid2 *cpuid,
21 struct kvm_cpuid_entry2 __user *entries);
22int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
23 struct kvm_cpuid2 *cpuid,
24 struct kvm_cpuid_entry2 __user *entries);
e911eb3b
YZ
25bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,
26 u32 *ecx, u32 *edx, bool check_limit);
00b27a3e 27
5a4f55cd
EK
28int cpuid_query_maxphyaddr(struct kvm_vcpu *vcpu);
29
30static inline int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
31{
32 return vcpu->arch.maxphyaddr;
33}
00b27a3e 34
d6321d49
RK
35struct cpuid_reg {
36 u32 function;
37 u32 index;
38 int reg;
39};
00b27a3e 40
d6321d49
RK
41static const struct cpuid_reg reverse_cpuid[] = {
42 [CPUID_1_EDX] = { 1, 0, CPUID_EDX},
43 [CPUID_8000_0001_EDX] = {0x80000001, 0, CPUID_EDX},
44 [CPUID_8086_0001_EDX] = {0x80860001, 0, CPUID_EDX},
45 [CPUID_1_ECX] = { 1, 0, CPUID_ECX},
46 [CPUID_C000_0001_EDX] = {0xc0000001, 0, CPUID_EDX},
47 [CPUID_8000_0001_ECX] = {0xc0000001, 0, CPUID_ECX},
48 [CPUID_7_0_EBX] = { 7, 0, CPUID_EBX},
49 [CPUID_D_1_EAX] = { 0xd, 1, CPUID_EAX},
50 [CPUID_F_0_EDX] = { 0xf, 0, CPUID_EDX},
51 [CPUID_F_1_EDX] = { 0xf, 1, CPUID_EDX},
52 [CPUID_8000_0008_EBX] = {0x80000008, 0, CPUID_EBX},
53 [CPUID_6_EAX] = { 6, 0, CPUID_EAX},
54 [CPUID_8000_000A_EDX] = {0x8000000a, 0, CPUID_EDX},
55 [CPUID_7_ECX] = { 7, 0, CPUID_ECX},
56 [CPUID_8000_0007_EBX] = {0x80000007, 0, CPUID_EBX},
57};
00b27a3e 58
d6321d49 59static __always_inline struct cpuid_reg x86_feature_cpuid(unsigned x86_feature)
e24dea2a 60{
d6321d49 61 unsigned x86_leaf = x86_feature / 32;
e24dea2a 62
d6321d49
RK
63 BUILD_BUG_ON(x86_leaf >= ARRAY_SIZE(reverse_cpuid));
64 BUILD_BUG_ON(reverse_cpuid[x86_leaf].function == 0);
e24dea2a 65
d6321d49 66 return reverse_cpuid[x86_leaf];
00b27a3e
AK
67}
68
d6321d49 69static __always_inline int *guest_cpuid_get_register(struct kvm_vcpu *vcpu, unsigned x86_feature)
97ec8c06 70{
d6321d49
RK
71 struct kvm_cpuid_entry2 *entry;
72 const struct cpuid_reg cpuid = x86_feature_cpuid(x86_feature);
660a5d51 73
d6321d49
RK
74 entry = kvm_find_cpuid_entry(vcpu, cpuid.function, cpuid.index);
75 if (!entry)
76 return NULL;
2b036c6b 77
d6321d49
RK
78 switch (cpuid.reg) {
79 case CPUID_EAX:
80 return &entry->eax;
81 case CPUID_EBX:
82 return &entry->ebx;
83 case CPUID_ECX:
84 return &entry->ecx;
85 case CPUID_EDX:
86 return &entry->edx;
87 default:
88 BUILD_BUG();
89 return NULL;
90 }
2b036c6b
BO
91}
92
d6321d49 93static __always_inline bool guest_cpuid_has(struct kvm_vcpu *vcpu, unsigned x86_feature)
ad756a16 94{
d6321d49 95 int *reg;
ad756a16 96
d6321d49
RK
97 if (x86_feature == X86_FEATURE_XSAVE &&
98 !static_cpu_has(X86_FEATURE_XSAVE))
99 return false;
ad756a16 100
d6321d49
RK
101 reg = guest_cpuid_get_register(vcpu, x86_feature);
102 if (!reg)
103 return false;
58cb628d 104
d6321d49 105 return *reg & bit(x86_feature);
58cb628d
JK
106}
107
1b4d56b8
RK
108static __always_inline void guest_cpuid_clear(struct kvm_vcpu *vcpu, unsigned x86_feature)
109{
110 int *reg;
111
112 reg = guest_cpuid_get_register(vcpu, x86_feature);
113 if (reg)
114 *reg &= ~bit(x86_feature);
115}
116
a0c0feb5
PB
117static inline bool guest_cpuid_is_amd(struct kvm_vcpu *vcpu)
118{
119 struct kvm_cpuid_entry2 *best;
120
121 best = kvm_find_cpuid_entry(vcpu, 0, 0);
122 return best && best->ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx;
123}
124
91713faf
BP
125static inline int guest_cpuid_family(struct kvm_vcpu *vcpu)
126{
127 struct kvm_cpuid_entry2 *best;
128
129 best = kvm_find_cpuid_entry(vcpu, 0x1, 0);
130 if (!best)
131 return -1;
132
133 return x86_family(best->eax);
134}
135
136static inline int guest_cpuid_model(struct kvm_vcpu *vcpu)
137{
138 struct kvm_cpuid_entry2 *best;
139
140 best = kvm_find_cpuid_entry(vcpu, 0x1, 0);
141 if (!best)
142 return -1;
143
144 return x86_model(best->eax);
145}
146
147static inline int guest_cpuid_stepping(struct kvm_vcpu *vcpu)
148{
149 struct kvm_cpuid_entry2 *best;
150
151 best = kvm_find_cpuid_entry(vcpu, 0x1, 0);
152 if (!best)
153 return -1;
154
155 return x86_stepping(best->eax);
156}
157
db2336a8
KH
158static inline bool supports_cpuid_fault(struct kvm_vcpu *vcpu)
159{
160 return vcpu->arch.msr_platform_info & MSR_PLATFORM_INFO_CPUID_FAULT;
161}
162
163static inline bool cpuid_fault_enabled(struct kvm_vcpu *vcpu)
164{
165 return vcpu->arch.msr_misc_features_enables &
166 MSR_MISC_FEATURES_ENABLES_CPUID_FAULT;
167}
168
00b27a3e 169#endif