Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 1 | #ifndef ARCH_X86_KVM_CPUID_H |
| 2 | #define ARCH_X86_KVM_CPUID_H |
| 3 | |
| 4 | #include "x86.h" |
Borislav Petkov | 91713fa | 2015-11-23 11:12:22 +0100 | [diff] [blame] | 5 | #include <asm/cpu.h> |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 6 | |
Nadav Amit | dd59809 | 2014-09-16 15:10:03 +0300 | [diff] [blame] | 7 | int kvm_update_cpuid(struct kvm_vcpu *vcpu); |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame^] | 8 | bool kvm_mpx_supported(void); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 9 | struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu, |
| 10 | u32 function, u32 index); |
Borislav Petkov | 9c15bb1 | 2013-09-22 16:44:50 +0200 | [diff] [blame] | 11 | int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid, |
| 12 | struct kvm_cpuid_entry2 __user *entries, |
| 13 | unsigned int type); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 14 | int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu, |
| 15 | struct kvm_cpuid *cpuid, |
| 16 | struct kvm_cpuid_entry __user *entries); |
| 17 | int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu, |
| 18 | struct kvm_cpuid2 *cpuid, |
| 19 | struct kvm_cpuid_entry2 __user *entries); |
| 20 | int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu, |
| 21 | struct kvm_cpuid2 *cpuid, |
| 22 | struct kvm_cpuid_entry2 __user *entries); |
Avi Kivity | 62046e5 | 2012-06-07 14:07:48 +0300 | [diff] [blame] | 23 | void kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, u32 *ecx, u32 *edx); |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 24 | |
Eugene Korenevsky | 5a4f55c | 2015-03-29 23:56:12 +0300 | [diff] [blame] | 25 | int cpuid_query_maxphyaddr(struct kvm_vcpu *vcpu); |
| 26 | |
| 27 | static inline int cpuid_maxphyaddr(struct kvm_vcpu *vcpu) |
| 28 | { |
| 29 | return vcpu->arch.maxphyaddr; |
| 30 | } |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 31 | |
| 32 | static inline bool guest_cpuid_has_xsave(struct kvm_vcpu *vcpu) |
| 33 | { |
| 34 | struct kvm_cpuid_entry2 *best; |
| 35 | |
Petr Matousek | 6d1068b | 2012-11-06 19:24:07 +0100 | [diff] [blame] | 36 | if (!static_cpu_has(X86_FEATURE_XSAVE)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 37 | return false; |
Petr Matousek | 6d1068b | 2012-11-06 19:24:07 +0100 | [diff] [blame] | 38 | |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 39 | best = kvm_find_cpuid_entry(vcpu, 1, 0); |
| 40 | return best && (best->ecx & bit(X86_FEATURE_XSAVE)); |
| 41 | } |
| 42 | |
Paolo Bonzini | e24dea2 | 2015-12-22 15:20:00 +0100 | [diff] [blame] | 43 | static inline bool guest_cpuid_has_mtrr(struct kvm_vcpu *vcpu) |
| 44 | { |
| 45 | struct kvm_cpuid_entry2 *best; |
| 46 | |
| 47 | best = kvm_find_cpuid_entry(vcpu, 1, 0); |
| 48 | return best && (best->edx & bit(X86_FEATURE_MTRR)); |
| 49 | } |
| 50 | |
Will Auld | ba90463 | 2012-11-29 12:42:50 -0800 | [diff] [blame] | 51 | static inline bool guest_cpuid_has_tsc_adjust(struct kvm_vcpu *vcpu) |
| 52 | { |
| 53 | struct kvm_cpuid_entry2 *best; |
| 54 | |
| 55 | best = kvm_find_cpuid_entry(vcpu, 7, 0); |
| 56 | return best && (best->ebx & bit(X86_FEATURE_TSC_ADJUST)); |
| 57 | } |
| 58 | |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 59 | static inline bool guest_cpuid_has_smep(struct kvm_vcpu *vcpu) |
| 60 | { |
| 61 | struct kvm_cpuid_entry2 *best; |
| 62 | |
| 63 | best = kvm_find_cpuid_entry(vcpu, 7, 0); |
| 64 | return best && (best->ebx & bit(X86_FEATURE_SMEP)); |
| 65 | } |
| 66 | |
Feng Wu | 97ec8c0 | 2014-04-01 17:46:34 +0800 | [diff] [blame] | 67 | static inline bool guest_cpuid_has_smap(struct kvm_vcpu *vcpu) |
| 68 | { |
| 69 | struct kvm_cpuid_entry2 *best; |
| 70 | |
| 71 | best = kvm_find_cpuid_entry(vcpu, 7, 0); |
| 72 | return best && (best->ebx & bit(X86_FEATURE_SMAP)); |
| 73 | } |
| 74 | |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 75 | static inline bool guest_cpuid_has_fsgsbase(struct kvm_vcpu *vcpu) |
| 76 | { |
| 77 | struct kvm_cpuid_entry2 *best; |
| 78 | |
| 79 | best = kvm_find_cpuid_entry(vcpu, 7, 0); |
| 80 | return best && (best->ebx & bit(X86_FEATURE_FSGSBASE)); |
| 81 | } |
| 82 | |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 83 | static inline bool guest_cpuid_has_longmode(struct kvm_vcpu *vcpu) |
| 84 | { |
| 85 | struct kvm_cpuid_entry2 *best; |
| 86 | |
| 87 | best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0); |
| 88 | return best && (best->edx & bit(X86_FEATURE_LM)); |
| 89 | } |
| 90 | |
Boris Ostrovsky | 2b036c6 | 2012-01-09 14:00:35 -0500 | [diff] [blame] | 91 | static inline bool guest_cpuid_has_osvw(struct kvm_vcpu *vcpu) |
| 92 | { |
| 93 | struct kvm_cpuid_entry2 *best; |
| 94 | |
| 95 | best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0); |
| 96 | return best && (best->ecx & bit(X86_FEATURE_OSVW)); |
| 97 | } |
| 98 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 99 | static inline bool guest_cpuid_has_pcid(struct kvm_vcpu *vcpu) |
| 100 | { |
| 101 | struct kvm_cpuid_entry2 *best; |
| 102 | |
| 103 | best = kvm_find_cpuid_entry(vcpu, 1, 0); |
| 104 | return best && (best->ecx & bit(X86_FEATURE_PCID)); |
| 105 | } |
| 106 | |
Jan Kiszka | 58cb628 | 2014-01-24 16:48:44 +0100 | [diff] [blame] | 107 | static inline bool guest_cpuid_has_x2apic(struct kvm_vcpu *vcpu) |
| 108 | { |
| 109 | struct kvm_cpuid_entry2 *best; |
| 110 | |
| 111 | best = kvm_find_cpuid_entry(vcpu, 1, 0); |
| 112 | return best && (best->ecx & bit(X86_FEATURE_X2APIC)); |
| 113 | } |
| 114 | |
Paolo Bonzini | a0c0feb5 | 2014-09-02 13:24:12 +0200 | [diff] [blame] | 115 | static inline bool guest_cpuid_is_amd(struct kvm_vcpu *vcpu) |
| 116 | { |
| 117 | struct kvm_cpuid_entry2 *best; |
| 118 | |
| 119 | best = kvm_find_cpuid_entry(vcpu, 0, 0); |
| 120 | return best && best->ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx; |
| 121 | } |
| 122 | |
Nadav Amit | 5f7dde7 | 2014-05-07 15:32:50 +0300 | [diff] [blame] | 123 | static inline bool guest_cpuid_has_gbpages(struct kvm_vcpu *vcpu) |
| 124 | { |
| 125 | struct kvm_cpuid_entry2 *best; |
| 126 | |
| 127 | best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0); |
| 128 | return best && (best->edx & bit(X86_FEATURE_GBPAGES)); |
| 129 | } |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 130 | |
| 131 | static inline bool guest_cpuid_has_rtm(struct kvm_vcpu *vcpu) |
| 132 | { |
| 133 | struct kvm_cpuid_entry2 *best; |
| 134 | |
| 135 | best = kvm_find_cpuid_entry(vcpu, 7, 0); |
| 136 | return best && (best->ebx & bit(X86_FEATURE_RTM)); |
| 137 | } |
Liang Li | c447e76 | 2015-05-21 04:41:25 +0800 | [diff] [blame] | 138 | |
Xiao Guangrong | 8b3e34e | 2015-09-09 14:05:51 +0800 | [diff] [blame] | 139 | static inline bool guest_cpuid_has_pcommit(struct kvm_vcpu *vcpu) |
| 140 | { |
| 141 | struct kvm_cpuid_entry2 *best; |
| 142 | |
| 143 | best = kvm_find_cpuid_entry(vcpu, 7, 0); |
| 144 | return best && (best->ebx & bit(X86_FEATURE_PCOMMIT)); |
| 145 | } |
Xiao Guangrong | 1cea0ce | 2015-09-09 14:05:57 +0800 | [diff] [blame] | 146 | |
| 147 | static inline bool guest_cpuid_has_rdtscp(struct kvm_vcpu *vcpu) |
| 148 | { |
| 149 | struct kvm_cpuid_entry2 *best; |
| 150 | |
| 151 | best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0); |
| 152 | return best && (best->edx & bit(X86_FEATURE_RDTSCP)); |
| 153 | } |
Joerg Roedel | 6092d3d | 2015-10-14 15:10:54 +0200 | [diff] [blame] | 154 | |
| 155 | /* |
| 156 | * NRIPS is provided through cpuidfn 0x8000000a.edx bit 3 |
| 157 | */ |
| 158 | #define BIT_NRIPS 3 |
| 159 | |
| 160 | static inline bool guest_cpuid_has_nrips(struct kvm_vcpu *vcpu) |
| 161 | { |
| 162 | struct kvm_cpuid_entry2 *best; |
| 163 | |
| 164 | best = kvm_find_cpuid_entry(vcpu, 0x8000000a, 0); |
| 165 | |
| 166 | /* |
| 167 | * NRIPS is a scattered cpuid feature, so we can't use |
| 168 | * X86_FEATURE_NRIPS here (X86_FEATURE_NRIPS would be bit |
| 169 | * position 8, not 3). |
| 170 | */ |
| 171 | return best && (best->edx & bit(BIT_NRIPS)); |
| 172 | } |
| 173 | #undef BIT_NRIPS |
| 174 | |
Borislav Petkov | 91713fa | 2015-11-23 11:12:22 +0100 | [diff] [blame] | 175 | static inline int guest_cpuid_family(struct kvm_vcpu *vcpu) |
| 176 | { |
| 177 | struct kvm_cpuid_entry2 *best; |
| 178 | |
| 179 | best = kvm_find_cpuid_entry(vcpu, 0x1, 0); |
| 180 | if (!best) |
| 181 | return -1; |
| 182 | |
| 183 | return x86_family(best->eax); |
| 184 | } |
| 185 | |
| 186 | static inline int guest_cpuid_model(struct kvm_vcpu *vcpu) |
| 187 | { |
| 188 | struct kvm_cpuid_entry2 *best; |
| 189 | |
| 190 | best = kvm_find_cpuid_entry(vcpu, 0x1, 0); |
| 191 | if (!best) |
| 192 | return -1; |
| 193 | |
| 194 | return x86_model(best->eax); |
| 195 | } |
| 196 | |
| 197 | static inline int guest_cpuid_stepping(struct kvm_vcpu *vcpu) |
| 198 | { |
| 199 | struct kvm_cpuid_entry2 *best; |
| 200 | |
| 201 | best = kvm_find_cpuid_entry(vcpu, 0x1, 0); |
| 202 | if (!best) |
| 203 | return -1; |
| 204 | |
| 205 | return x86_stepping(best->eax); |
| 206 | } |
| 207 | |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 208 | #endif |