Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Avi Kivity | 26eef70 | 2008-07-03 14:59:22 +0300 | [diff] [blame] | 2 | #ifndef ARCH_X86_KVM_X86_H |
| 3 | #define ARCH_X86_KVM_X86_H |
| 4 | |
| 5 | #include <linux/kvm_host.h> |
Uros Bizjak | 3f1a18b | 2020-10-29 14:56:00 +0100 | [diff] [blame^] | 6 | #include <asm/mce.h> |
Marcelo Tosatti | 8d93c87 | 2016-06-20 22:28:02 -0300 | [diff] [blame] | 7 | #include <asm/pvclock.h> |
Avi Kivity | 3eeb328 | 2010-01-21 15:31:48 +0200 | [diff] [blame] | 8 | #include "kvm_cache_regs.h" |
Sean Christopherson | 2f728d6 | 2020-02-18 15:29:49 -0800 | [diff] [blame] | 9 | #include "kvm_emulate.h" |
Avi Kivity | 26eef70 | 2008-07-03 14:59:22 +0300 | [diff] [blame] | 10 | |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 11 | #define KVM_DEFAULT_PLE_GAP 128 |
| 12 | #define KVM_VMX_DEFAULT_PLE_WINDOW 4096 |
| 13 | #define KVM_DEFAULT_PLE_WINDOW_GROW 2 |
| 14 | #define KVM_DEFAULT_PLE_WINDOW_SHRINK 0 |
| 15 | #define KVM_VMX_DEFAULT_PLE_WINDOW_MAX UINT_MAX |
Babu Moger | 8566ac8 | 2018-03-16 16:37:26 -0400 | [diff] [blame] | 16 | #define KVM_SVM_DEFAULT_PLE_WINDOW_MAX USHRT_MAX |
| 17 | #define KVM_SVM_DEFAULT_PLE_WINDOW 3000 |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 18 | |
| 19 | static inline unsigned int __grow_ple_window(unsigned int val, |
| 20 | unsigned int base, unsigned int modifier, unsigned int max) |
| 21 | { |
| 22 | u64 ret = val; |
| 23 | |
| 24 | if (modifier < 1) |
| 25 | return base; |
| 26 | |
| 27 | if (modifier < base) |
| 28 | ret *= modifier; |
| 29 | else |
| 30 | ret += modifier; |
| 31 | |
| 32 | return min(ret, (u64)max); |
| 33 | } |
| 34 | |
| 35 | static inline unsigned int __shrink_ple_window(unsigned int val, |
| 36 | unsigned int base, unsigned int modifier, unsigned int min) |
| 37 | { |
| 38 | if (modifier < 1) |
| 39 | return base; |
| 40 | |
| 41 | if (modifier < base) |
| 42 | val /= modifier; |
| 43 | else |
| 44 | val -= modifier; |
| 45 | |
| 46 | return max(val, min); |
| 47 | } |
| 48 | |
Radim Krčmář | 7454570 | 2015-04-27 15:11:25 +0200 | [diff] [blame] | 49 | #define MSR_IA32_CR_PAT_DEFAULT 0x0007040600070406ULL |
| 50 | |
Avi Kivity | 26eef70 | 2008-07-03 14:59:22 +0300 | [diff] [blame] | 51 | static inline void kvm_clear_exception_queue(struct kvm_vcpu *vcpu) |
| 52 | { |
Liran Alon | 5c7d4f9 | 2017-11-19 18:25:43 +0200 | [diff] [blame] | 53 | vcpu->arch.exception.pending = false; |
Wanpeng Li | 664f8e2 | 2017-08-24 03:35:09 -0700 | [diff] [blame] | 54 | vcpu->arch.exception.injected = false; |
Avi Kivity | 26eef70 | 2008-07-03 14:59:22 +0300 | [diff] [blame] | 55 | } |
| 56 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 57 | static inline void kvm_queue_interrupt(struct kvm_vcpu *vcpu, u8 vector, |
| 58 | bool soft) |
Avi Kivity | 937a7ea | 2008-07-03 15:17:01 +0300 | [diff] [blame] | 59 | { |
Liran Alon | 04140b4 | 2018-03-23 03:01:31 +0300 | [diff] [blame] | 60 | vcpu->arch.interrupt.injected = true; |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 61 | vcpu->arch.interrupt.soft = soft; |
Avi Kivity | 937a7ea | 2008-07-03 15:17:01 +0300 | [diff] [blame] | 62 | vcpu->arch.interrupt.nr = vector; |
| 63 | } |
| 64 | |
| 65 | static inline void kvm_clear_interrupt_queue(struct kvm_vcpu *vcpu) |
| 66 | { |
Liran Alon | 04140b4 | 2018-03-23 03:01:31 +0300 | [diff] [blame] | 67 | vcpu->arch.interrupt.injected = false; |
Avi Kivity | 937a7ea | 2008-07-03 15:17:01 +0300 | [diff] [blame] | 68 | } |
| 69 | |
Gleb Natapov | 3298b75 | 2009-05-11 13:35:46 +0300 | [diff] [blame] | 70 | static inline bool kvm_event_needs_reinjection(struct kvm_vcpu *vcpu) |
| 71 | { |
Liran Alon | 04140b4 | 2018-03-23 03:01:31 +0300 | [diff] [blame] | 72 | return vcpu->arch.exception.injected || vcpu->arch.interrupt.injected || |
Gleb Natapov | 3298b75 | 2009-05-11 13:35:46 +0300 | [diff] [blame] | 73 | vcpu->arch.nmi_injected; |
| 74 | } |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 75 | |
| 76 | static inline bool kvm_exception_is_soft(unsigned int nr) |
| 77 | { |
| 78 | return (nr == BP_VECTOR) || (nr == OF_VECTOR); |
| 79 | } |
Gleb Natapov | fc61b80 | 2009-07-05 17:39:35 +0300 | [diff] [blame] | 80 | |
Avi Kivity | 3eeb328 | 2010-01-21 15:31:48 +0200 | [diff] [blame] | 81 | static inline bool is_protmode(struct kvm_vcpu *vcpu) |
| 82 | { |
| 83 | return kvm_read_cr0_bits(vcpu, X86_CR0_PE); |
| 84 | } |
| 85 | |
Avi Kivity | 836a1b3 | 2010-01-21 15:31:49 +0200 | [diff] [blame] | 86 | static inline int is_long_mode(struct kvm_vcpu *vcpu) |
| 87 | { |
| 88 | #ifdef CONFIG_X86_64 |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 89 | return vcpu->arch.efer & EFER_LMA; |
Avi Kivity | 836a1b3 | 2010-01-21 15:31:49 +0200 | [diff] [blame] | 90 | #else |
| 91 | return 0; |
| 92 | #endif |
| 93 | } |
| 94 | |
Nadav Amit | 5777392 | 2014-06-18 17:19:23 +0300 | [diff] [blame] | 95 | static inline bool is_64_bit_mode(struct kvm_vcpu *vcpu) |
| 96 | { |
| 97 | int cs_db, cs_l; |
| 98 | |
| 99 | if (!is_long_mode(vcpu)) |
| 100 | return false; |
Sean Christopherson | afaf0b2 | 2020-03-21 13:26:00 -0700 | [diff] [blame] | 101 | kvm_x86_ops.get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
Nadav Amit | 5777392 | 2014-06-18 17:19:23 +0300 | [diff] [blame] | 102 | return cs_l; |
| 103 | } |
| 104 | |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 105 | static inline bool is_la57_mode(struct kvm_vcpu *vcpu) |
| 106 | { |
| 107 | #ifdef CONFIG_X86_64 |
| 108 | return (vcpu->arch.efer & EFER_LMA) && |
| 109 | kvm_read_cr4_bits(vcpu, X86_CR4_LA57); |
| 110 | #else |
| 111 | return 0; |
| 112 | #endif |
| 113 | } |
| 114 | |
Marc Orr | 0447378 | 2018-06-20 17:21:29 -0700 | [diff] [blame] | 115 | static inline bool x86_exception_has_error_code(unsigned int vector) |
| 116 | { |
| 117 | static u32 exception_has_error_code = BIT(DF_VECTOR) | BIT(TS_VECTOR) | |
| 118 | BIT(NP_VECTOR) | BIT(SS_VECTOR) | BIT(GP_VECTOR) | |
| 119 | BIT(PF_VECTOR) | BIT(AC_VECTOR); |
| 120 | |
| 121 | return (1U << vector) & exception_has_error_code; |
| 122 | } |
| 123 | |
Joerg Roedel | 6539e73 | 2010-09-10 17:30:50 +0200 | [diff] [blame] | 124 | static inline bool mmu_is_nested(struct kvm_vcpu *vcpu) |
| 125 | { |
| 126 | return vcpu->arch.walk_mmu == &vcpu->arch.nested_mmu; |
| 127 | } |
| 128 | |
Sean Christopherson | eeeb4f6 | 2020-03-20 14:28:20 -0700 | [diff] [blame] | 129 | static inline void kvm_vcpu_flush_tlb_current(struct kvm_vcpu *vcpu) |
| 130 | { |
| 131 | ++vcpu->stat.tlb_flush; |
| 132 | kvm_x86_ops.tlb_flush_current(vcpu); |
| 133 | } |
| 134 | |
Avi Kivity | 836a1b3 | 2010-01-21 15:31:49 +0200 | [diff] [blame] | 135 | static inline int is_pae(struct kvm_vcpu *vcpu) |
| 136 | { |
| 137 | return kvm_read_cr4_bits(vcpu, X86_CR4_PAE); |
| 138 | } |
| 139 | |
| 140 | static inline int is_pse(struct kvm_vcpu *vcpu) |
| 141 | { |
| 142 | return kvm_read_cr4_bits(vcpu, X86_CR4_PSE); |
| 143 | } |
| 144 | |
| 145 | static inline int is_paging(struct kvm_vcpu *vcpu) |
| 146 | { |
Davidlohr Bueso | c36fc04 | 2012-03-08 12:45:54 +0100 | [diff] [blame] | 147 | return likely(kvm_read_cr0_bits(vcpu, X86_CR0_PG)); |
Avi Kivity | 836a1b3 | 2010-01-21 15:31:49 +0200 | [diff] [blame] | 148 | } |
| 149 | |
Paolo Bonzini | bf03d4f | 2019-06-06 18:52:44 +0200 | [diff] [blame] | 150 | static inline bool is_pae_paging(struct kvm_vcpu *vcpu) |
| 151 | { |
| 152 | return !is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu); |
| 153 | } |
| 154 | |
Yu Zhang | fd8cb43 | 2017-08-24 20:27:56 +0800 | [diff] [blame] | 155 | static inline u8 vcpu_virt_addr_bits(struct kvm_vcpu *vcpu) |
| 156 | { |
| 157 | return kvm_read_cr4_bits(vcpu, X86_CR4_LA57) ? 57 : 48; |
| 158 | } |
| 159 | |
Yu Zhang | fd8cb43 | 2017-08-24 20:27:56 +0800 | [diff] [blame] | 160 | static inline u64 get_canonical(u64 la, u8 vaddr_bits) |
| 161 | { |
| 162 | return ((int64_t)la << (64 - vaddr_bits)) >> (64 - vaddr_bits); |
| 163 | } |
| 164 | |
| 165 | static inline bool is_noncanonical_address(u64 la, struct kvm_vcpu *vcpu) |
| 166 | { |
Yu Zhang | fd8cb43 | 2017-08-24 20:27:56 +0800 | [diff] [blame] | 167 | return get_canonical(la, vcpu_virt_addr_bits(vcpu)) != la; |
Yu Zhang | fd8cb43 | 2017-08-24 20:27:56 +0800 | [diff] [blame] | 168 | } |
| 169 | |
Xiao Guangrong | bebb106 | 2011-07-12 03:23:20 +0800 | [diff] [blame] | 170 | static inline void vcpu_cache_mmio_info(struct kvm_vcpu *vcpu, |
| 171 | gva_t gva, gfn_t gfn, unsigned access) |
| 172 | { |
Sean Christopherson | ddfd173 | 2019-02-05 13:01:13 -0800 | [diff] [blame] | 173 | u64 gen = kvm_memslots(vcpu->kvm)->generation; |
| 174 | |
Sean Christopherson | 361209e | 2019-02-05 13:01:14 -0800 | [diff] [blame] | 175 | if (unlikely(gen & KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS)) |
Sean Christopherson | ddfd173 | 2019-02-05 13:01:13 -0800 | [diff] [blame] | 176 | return; |
| 177 | |
Paolo Bonzini | 9034e6e | 2017-08-17 18:36:58 +0200 | [diff] [blame] | 178 | /* |
| 179 | * If this is a shadow nested page table, the "GVA" is |
| 180 | * actually a nGPA. |
| 181 | */ |
| 182 | vcpu->arch.mmio_gva = mmu_is_nested(vcpu) ? 0 : gva & PAGE_MASK; |
Sean Christopherson | 871bd03 | 2019-08-01 13:35:21 -0700 | [diff] [blame] | 183 | vcpu->arch.mmio_access = access; |
Xiao Guangrong | bebb106 | 2011-07-12 03:23:20 +0800 | [diff] [blame] | 184 | vcpu->arch.mmio_gfn = gfn; |
Sean Christopherson | ddfd173 | 2019-02-05 13:01:13 -0800 | [diff] [blame] | 185 | vcpu->arch.mmio_gen = gen; |
David Matlack | 56f17dd | 2014-08-18 15:46:07 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | static inline bool vcpu_match_mmio_gen(struct kvm_vcpu *vcpu) |
| 189 | { |
| 190 | return vcpu->arch.mmio_gen == kvm_memslots(vcpu->kvm)->generation; |
Xiao Guangrong | bebb106 | 2011-07-12 03:23:20 +0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /* |
David Matlack | 56f17dd | 2014-08-18 15:46:07 -0700 | [diff] [blame] | 194 | * Clear the mmio cache info for the given gva. If gva is MMIO_GVA_ANY, we |
| 195 | * clear all mmio cache info. |
Xiao Guangrong | bebb106 | 2011-07-12 03:23:20 +0800 | [diff] [blame] | 196 | */ |
David Matlack | 56f17dd | 2014-08-18 15:46:07 -0700 | [diff] [blame] | 197 | #define MMIO_GVA_ANY (~(gva_t)0) |
| 198 | |
Xiao Guangrong | bebb106 | 2011-07-12 03:23:20 +0800 | [diff] [blame] | 199 | static inline void vcpu_clear_mmio_info(struct kvm_vcpu *vcpu, gva_t gva) |
| 200 | { |
David Matlack | 56f17dd | 2014-08-18 15:46:07 -0700 | [diff] [blame] | 201 | if (gva != MMIO_GVA_ANY && vcpu->arch.mmio_gva != (gva & PAGE_MASK)) |
Xiao Guangrong | bebb106 | 2011-07-12 03:23:20 +0800 | [diff] [blame] | 202 | return; |
| 203 | |
| 204 | vcpu->arch.mmio_gva = 0; |
| 205 | } |
| 206 | |
| 207 | static inline bool vcpu_match_mmio_gva(struct kvm_vcpu *vcpu, unsigned long gva) |
| 208 | { |
David Matlack | 56f17dd | 2014-08-18 15:46:07 -0700 | [diff] [blame] | 209 | if (vcpu_match_mmio_gen(vcpu) && vcpu->arch.mmio_gva && |
| 210 | vcpu->arch.mmio_gva == (gva & PAGE_MASK)) |
Xiao Guangrong | bebb106 | 2011-07-12 03:23:20 +0800 | [diff] [blame] | 211 | return true; |
| 212 | |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | static inline bool vcpu_match_mmio_gpa(struct kvm_vcpu *vcpu, gpa_t gpa) |
| 217 | { |
David Matlack | 56f17dd | 2014-08-18 15:46:07 -0700 | [diff] [blame] | 218 | if (vcpu_match_mmio_gen(vcpu) && vcpu->arch.mmio_gfn && |
| 219 | vcpu->arch.mmio_gfn == gpa >> PAGE_SHIFT) |
Xiao Guangrong | bebb106 | 2011-07-12 03:23:20 +0800 | [diff] [blame] | 220 | return true; |
| 221 | |
| 222 | return false; |
| 223 | } |
| 224 | |
Sean Christopherson | 489cbcf | 2019-09-27 14:45:20 -0700 | [diff] [blame] | 225 | static inline unsigned long kvm_register_readl(struct kvm_vcpu *vcpu, int reg) |
Nadav Amit | 5777392 | 2014-06-18 17:19:23 +0300 | [diff] [blame] | 226 | { |
| 227 | unsigned long val = kvm_register_read(vcpu, reg); |
| 228 | |
| 229 | return is_64_bit_mode(vcpu) ? val : (u32)val; |
| 230 | } |
| 231 | |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 232 | static inline void kvm_register_writel(struct kvm_vcpu *vcpu, |
Sean Christopherson | 489cbcf | 2019-09-27 14:45:20 -0700 | [diff] [blame] | 233 | int reg, unsigned long val) |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 234 | { |
| 235 | if (!is_64_bit_mode(vcpu)) |
| 236 | val = (u32)val; |
| 237 | return kvm_register_write(vcpu, reg, val); |
| 238 | } |
| 239 | |
Paolo Bonzini | 41dbc6b | 2015-07-23 08:22:45 +0200 | [diff] [blame] | 240 | static inline bool kvm_check_has_quirk(struct kvm *kvm, u64 quirk) |
| 241 | { |
| 242 | return !(kvm->arch.disabled_quirks & quirk); |
| 243 | } |
| 244 | |
Liran Alon | 27cbe7d | 2019-11-11 11:16:40 +0200 | [diff] [blame] | 245 | static inline bool kvm_vcpu_latch_init(struct kvm_vcpu *vcpu) |
| 246 | { |
Sean Christopherson | afaf0b2 | 2020-03-21 13:26:00 -0700 | [diff] [blame] | 247 | return is_smm(vcpu) || kvm_x86_ops.apic_init_signal_blocked(vcpu); |
Liran Alon | 27cbe7d | 2019-11-11 11:16:40 +0200 | [diff] [blame] | 248 | } |
| 249 | |
Sean Christopherson | 9497e1f | 2019-08-27 14:40:36 -0700 | [diff] [blame] | 250 | void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip); |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 251 | |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 252 | void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr); |
Paolo Bonzini | 108b249 | 2016-09-01 14:21:03 +0200 | [diff] [blame] | 253 | u64 get_kvmclock_ns(struct kvm *kvm); |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 254 | |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 255 | int kvm_read_guest_virt(struct kvm_vcpu *vcpu, |
Nadav Har'El | 064aea7 | 2011-05-25 23:04:56 +0300 | [diff] [blame] | 256 | gva_t addr, void *val, unsigned int bytes, |
| 257 | struct x86_exception *exception); |
| 258 | |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 259 | int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu, |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 260 | gva_t addr, void *val, unsigned int bytes, |
| 261 | struct x86_exception *exception); |
| 262 | |
Wanpeng Li | 082d06e | 2018-04-03 16:28:48 -0700 | [diff] [blame] | 263 | int handle_ud(struct kvm_vcpu *vcpu); |
| 264 | |
Jim Mattson | da998b4 | 2018-10-16 14:29:22 -0700 | [diff] [blame] | 265 | void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu); |
| 266 | |
Xiao Guangrong | 19efffa | 2015-06-15 16:55:31 +0800 | [diff] [blame] | 267 | void kvm_vcpu_mtrr_init(struct kvm_vcpu *vcpu); |
Xiao Guangrong | ff53604 | 2015-06-15 16:55:22 +0800 | [diff] [blame] | 268 | u8 kvm_mtrr_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn); |
Nadav Amit | 4566654 | 2014-09-18 22:39:44 +0300 | [diff] [blame] | 269 | bool kvm_mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data); |
Xiao Guangrong | ff53604 | 2015-06-15 16:55:22 +0800 | [diff] [blame] | 270 | int kvm_mtrr_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data); |
| 271 | int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata); |
Xiao Guangrong | 6a39bbc | 2015-06-15 16:55:35 +0800 | [diff] [blame] | 272 | bool kvm_mtrr_check_gfn_range_consistency(struct kvm_vcpu *vcpu, gfn_t gfn, |
| 273 | int page_num); |
Feng Wu | 52004014 | 2016-01-25 16:53:33 +0800 | [diff] [blame] | 274 | bool kvm_vector_hashing_enabled(void); |
Mohammed Gamal | 8978614 | 2020-07-10 17:48:03 +0200 | [diff] [blame] | 275 | void kvm_fixup_and_inject_pf_error(struct kvm_vcpu *vcpu, gva_t gva, u16 error_code); |
Sean Christopherson | 736c291 | 2019-12-06 15:57:14 -0800 | [diff] [blame] | 276 | int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, |
Sean Christopherson | c60658d | 2018-08-23 13:56:53 -0700 | [diff] [blame] | 277 | int emulation_type, void *insn, int insn_len); |
Wanpeng Li | 404d5d7 | 2020-04-28 14:23:25 +0800 | [diff] [blame] | 278 | fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu); |
Nadav Amit | 4566654 | 2014-09-18 22:39:44 +0300 | [diff] [blame] | 279 | |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 280 | extern u64 host_xcr0; |
Sean Christopherson | cfc4818 | 2020-03-02 15:56:23 -0800 | [diff] [blame] | 281 | extern u64 supported_xcr0; |
Paolo Bonzini | 408e9a3 | 2020-03-05 16:11:56 +0100 | [diff] [blame] | 282 | extern u64 supported_xss; |
Paolo Bonzini | 4ff4173 | 2014-02-24 12:15:16 +0100 | [diff] [blame] | 283 | |
Sean Christopherson | 615a4ae | 2020-03-02 15:56:25 -0800 | [diff] [blame] | 284 | static inline bool kvm_mpx_supported(void) |
| 285 | { |
| 286 | return (supported_xcr0 & (XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR)) |
| 287 | == (XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR); |
| 288 | } |
| 289 | |
Marcelo Tosatti | 9ed96e8 | 2014-01-06 12:00:02 -0200 | [diff] [blame] | 290 | extern unsigned int min_timer_period_us; |
| 291 | |
Liran Alon | c4ae60e | 2018-03-12 13:12:47 +0200 | [diff] [blame] | 292 | extern bool enable_vmware_backdoor; |
| 293 | |
Wanpeng Li | 0c5f81d | 2019-07-06 09:26:51 +0800 | [diff] [blame] | 294 | extern int pi_inject_timer; |
| 295 | |
Gleb Natapov | 54e9818 | 2012-08-05 15:58:32 +0300 | [diff] [blame] | 296 | extern struct static_key kvm_no_apic_vcpu; |
Paolo Bonzini | b51012d | 2016-01-22 11:39:22 +0100 | [diff] [blame] | 297 | |
Marcelo Tosatti | 8d93c87 | 2016-06-20 22:28:02 -0300 | [diff] [blame] | 298 | static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec) |
| 299 | { |
| 300 | return pvclock_scale_delta(nsec, vcpu->arch.virtual_tsc_mult, |
| 301 | vcpu->arch.virtual_tsc_shift); |
| 302 | } |
| 303 | |
Paolo Bonzini | b51012d | 2016-01-22 11:39:22 +0100 | [diff] [blame] | 304 | /* Same "calling convention" as do_div: |
| 305 | * - divide (n << 32) by base |
| 306 | * - put result in n |
| 307 | * - return remainder |
| 308 | */ |
| 309 | #define do_shl32_div32(n, base) \ |
| 310 | ({ \ |
| 311 | u32 __quot, __rem; \ |
| 312 | asm("divl %2" : "=a" (__quot), "=d" (__rem) \ |
| 313 | : "rm" (base), "0" (0), "1" ((u32) n)); \ |
| 314 | n = __quot; \ |
| 315 | __rem; \ |
| 316 | }) |
| 317 | |
Wanpeng Li | 4d5422c | 2018-03-12 04:53:02 -0700 | [diff] [blame] | 318 | static inline bool kvm_mwait_in_guest(struct kvm *kvm) |
Michael S. Tsirkin | 668fffa | 2017-04-21 12:27:17 +0200 | [diff] [blame] | 319 | { |
Wanpeng Li | 4d5422c | 2018-03-12 04:53:02 -0700 | [diff] [blame] | 320 | return kvm->arch.mwait_in_guest; |
Michael S. Tsirkin | 668fffa | 2017-04-21 12:27:17 +0200 | [diff] [blame] | 321 | } |
| 322 | |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 323 | static inline bool kvm_hlt_in_guest(struct kvm *kvm) |
| 324 | { |
| 325 | return kvm->arch.hlt_in_guest; |
| 326 | } |
| 327 | |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 328 | static inline bool kvm_pause_in_guest(struct kvm *kvm) |
| 329 | { |
| 330 | return kvm->arch.pause_in_guest; |
| 331 | } |
| 332 | |
Wanpeng Li | b517006 | 2019-05-21 14:06:53 +0800 | [diff] [blame] | 333 | static inline bool kvm_cstate_in_guest(struct kvm *kvm) |
| 334 | { |
| 335 | return kvm->arch.cstate_in_guest; |
| 336 | } |
| 337 | |
Andi Kleen | dd60d21 | 2017-07-25 17:20:32 -0700 | [diff] [blame] | 338 | DECLARE_PER_CPU(struct kvm_vcpu *, current_vcpu); |
| 339 | |
| 340 | static inline void kvm_before_interrupt(struct kvm_vcpu *vcpu) |
| 341 | { |
| 342 | __this_cpu_write(current_vcpu, vcpu); |
| 343 | } |
| 344 | |
| 345 | static inline void kvm_after_interrupt(struct kvm_vcpu *vcpu) |
| 346 | { |
| 347 | __this_cpu_write(current_vcpu, NULL); |
| 348 | } |
| 349 | |
Paolo Bonzini | 674ea35 | 2019-04-10 11:41:40 +0200 | [diff] [blame] | 350 | |
| 351 | static inline bool kvm_pat_valid(u64 data) |
| 352 | { |
| 353 | if (data & 0xF8F8F8F8F8F8F8F8ull) |
| 354 | return false; |
| 355 | /* 0, 1, 4, 5, 6, 7 are valid values. */ |
| 356 | return (data | ((data & 0x0202020202020202ull) << 1)) == data; |
| 357 | } |
| 358 | |
Sean Christopherson | 9b5e853 | 2020-01-24 15:07:22 -0800 | [diff] [blame] | 359 | static inline bool kvm_dr7_valid(u64 data) |
Krish Sadhukhan | b91991b | 2020-01-15 19:54:32 -0500 | [diff] [blame] | 360 | { |
| 361 | /* Bits [63:32] are reserved */ |
| 362 | return !(data >> 32); |
| 363 | } |
Krish Sadhukhan | f5f6145 | 2020-05-22 18:19:51 -0400 | [diff] [blame] | 364 | static inline bool kvm_dr6_valid(u64 data) |
| 365 | { |
| 366 | /* Bits [63:32] are reserved */ |
| 367 | return !(data >> 32); |
| 368 | } |
Krish Sadhukhan | b91991b | 2020-01-15 19:54:32 -0500 | [diff] [blame] | 369 | |
Uros Bizjak | 3f1a18b | 2020-10-29 14:56:00 +0100 | [diff] [blame^] | 370 | /* |
| 371 | * Trigger machine check on the host. We assume all the MSRs are already set up |
| 372 | * by the CPU and that we still run on the same CPU as the MCE occurred on. |
| 373 | * We pass a fake environment to the machine check handler because we want |
| 374 | * the guest to be always treated like user space, no matter what context |
| 375 | * it used internally. |
| 376 | */ |
| 377 | static inline void kvm_machine_check(void) |
| 378 | { |
| 379 | #if defined(CONFIG_X86_MCE) |
| 380 | struct pt_regs regs = { |
| 381 | .cs = 3, /* Fake ring 3 no matter what the guest ran on */ |
| 382 | .flags = X86_EFLAGS_IF, |
| 383 | }; |
| 384 | |
| 385 | do_machine_check(®s); |
| 386 | #endif |
| 387 | } |
| 388 | |
Aaron Lewis | 139a12c | 2019-10-21 16:30:25 -0700 | [diff] [blame] | 389 | void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu); |
| 390 | void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu); |
Maxim Levitsky | 841c2be | 2020-07-08 14:57:31 +0300 | [diff] [blame] | 391 | int kvm_spec_ctrl_test_value(u64 value); |
Sean Christopherson | ee69c92 | 2020-10-06 18:44:16 -0700 | [diff] [blame] | 392 | bool kvm_is_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4); |
Wanpeng Li | 5a9f544 | 2020-04-28 14:23:26 +0800 | [diff] [blame] | 393 | bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu); |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 394 | int kvm_handle_memory_failure(struct kvm_vcpu *vcpu, int r, |
| 395 | struct x86_exception *e); |
Babu Moger | 9715092 | 2020-09-11 14:29:12 -0500 | [diff] [blame] | 396 | int kvm_handle_invpcid(struct kvm_vcpu *vcpu, unsigned long type, gva_t gva); |
Alexander Graf | 51de815 | 2020-09-25 16:34:17 +0200 | [diff] [blame] | 397 | bool kvm_msr_allowed(struct kvm_vcpu *vcpu, u32 index, u32 type); |
Paolo Bonzini | 674ea35 | 2019-04-10 11:41:40 +0200 | [diff] [blame] | 398 | |
Maxim Levitsky | cc4cb01 | 2020-11-01 13:55:23 +0200 | [diff] [blame] | 399 | /* |
| 400 | * Internal error codes that are used to indicate that MSR emulation encountered |
| 401 | * an error that should result in #GP in the guest, unless userspace |
| 402 | * handles it. |
| 403 | */ |
| 404 | #define KVM_MSR_RET_INVALID 2 /* in-kernel MSR emulation #GP condition */ |
| 405 | #define KVM_MSR_RET_FILTERED 3 /* #GP due to userspace MSR filter */ |
Peter Xu | 6abe9c1 | 2020-06-22 18:04:41 -0400 | [diff] [blame] | 406 | |
Krish Sadhukhan | b899c13 | 2020-07-08 00:39:55 +0000 | [diff] [blame] | 407 | #define __cr4_reserved_bits(__cpu_has, __c) \ |
| 408 | ({ \ |
| 409 | u64 __reserved_bits = CR4_RESERVED_BITS; \ |
| 410 | \ |
| 411 | if (!__cpu_has(__c, X86_FEATURE_XSAVE)) \ |
| 412 | __reserved_bits |= X86_CR4_OSXSAVE; \ |
| 413 | if (!__cpu_has(__c, X86_FEATURE_SMEP)) \ |
| 414 | __reserved_bits |= X86_CR4_SMEP; \ |
| 415 | if (!__cpu_has(__c, X86_FEATURE_SMAP)) \ |
| 416 | __reserved_bits |= X86_CR4_SMAP; \ |
| 417 | if (!__cpu_has(__c, X86_FEATURE_FSGSBASE)) \ |
| 418 | __reserved_bits |= X86_CR4_FSGSBASE; \ |
| 419 | if (!__cpu_has(__c, X86_FEATURE_PKU)) \ |
| 420 | __reserved_bits |= X86_CR4_PKE; \ |
| 421 | if (!__cpu_has(__c, X86_FEATURE_LA57)) \ |
| 422 | __reserved_bits |= X86_CR4_LA57; \ |
| 423 | if (!__cpu_has(__c, X86_FEATURE_UMIP)) \ |
| 424 | __reserved_bits |= X86_CR4_UMIP; \ |
Paolo Bonzini | 53efe52 | 2020-07-08 07:02:50 -0400 | [diff] [blame] | 425 | if (!__cpu_has(__c, X86_FEATURE_VMX)) \ |
| 426 | __reserved_bits |= X86_CR4_VMXE; \ |
Krish Sadhukhan | b899c13 | 2020-07-08 00:39:55 +0000 | [diff] [blame] | 427 | __reserved_bits; \ |
| 428 | }) |
| 429 | |
Avi Kivity | 26eef70 | 2008-07-03 14:59:22 +0300 | [diff] [blame] | 430 | #endif |