blob: 6eb62e97e59faab3a81c901b3e2a6a0ed50947fa [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Avi Kivity26eef702008-07-03 14:59:22 +03002#ifndef ARCH_X86_KVM_X86_H
3#define ARCH_X86_KVM_X86_H
4
5#include <linux/kvm_host.h>
Marcelo Tosatti8d93c872016-06-20 22:28:02 -03006#include <asm/pvclock.h>
Avi Kivity3eeb3282010-01-21 15:31:48 +02007#include "kvm_cache_regs.h"
Sean Christopherson2f728d62020-02-18 15:29:49 -08008#include "kvm_emulate.h"
Avi Kivity26eef702008-07-03 14:59:22 +03009
Babu Mogerc8e88712018-03-16 16:37:24 -040010#define KVM_DEFAULT_PLE_GAP 128
11#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
12#define KVM_DEFAULT_PLE_WINDOW_GROW 2
13#define KVM_DEFAULT_PLE_WINDOW_SHRINK 0
14#define KVM_VMX_DEFAULT_PLE_WINDOW_MAX UINT_MAX
Babu Moger8566ac82018-03-16 16:37:26 -040015#define KVM_SVM_DEFAULT_PLE_WINDOW_MAX USHRT_MAX
16#define KVM_SVM_DEFAULT_PLE_WINDOW 3000
Babu Mogerc8e88712018-03-16 16:37:24 -040017
18static inline unsigned int __grow_ple_window(unsigned int val,
19 unsigned int base, unsigned int modifier, unsigned int max)
20{
21 u64 ret = val;
22
23 if (modifier < 1)
24 return base;
25
26 if (modifier < base)
27 ret *= modifier;
28 else
29 ret += modifier;
30
31 return min(ret, (u64)max);
32}
33
34static inline unsigned int __shrink_ple_window(unsigned int val,
35 unsigned int base, unsigned int modifier, unsigned int min)
36{
37 if (modifier < 1)
38 return base;
39
40 if (modifier < base)
41 val /= modifier;
42 else
43 val -= modifier;
44
45 return max(val, min);
46}
47
Radim Krčmář74545702015-04-27 15:11:25 +020048#define MSR_IA32_CR_PAT_DEFAULT 0x0007040600070406ULL
49
Avi Kivity26eef702008-07-03 14:59:22 +030050static inline void kvm_clear_exception_queue(struct kvm_vcpu *vcpu)
51{
Liran Alon5c7d4f92017-11-19 18:25:43 +020052 vcpu->arch.exception.pending = false;
Wanpeng Li664f8e22017-08-24 03:35:09 -070053 vcpu->arch.exception.injected = false;
Avi Kivity26eef702008-07-03 14:59:22 +030054}
55
Gleb Natapov66fd3f72009-05-11 13:35:50 +030056static inline void kvm_queue_interrupt(struct kvm_vcpu *vcpu, u8 vector,
57 bool soft)
Avi Kivity937a7ea2008-07-03 15:17:01 +030058{
Liran Alon04140b42018-03-23 03:01:31 +030059 vcpu->arch.interrupt.injected = true;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030060 vcpu->arch.interrupt.soft = soft;
Avi Kivity937a7ea2008-07-03 15:17:01 +030061 vcpu->arch.interrupt.nr = vector;
62}
63
64static inline void kvm_clear_interrupt_queue(struct kvm_vcpu *vcpu)
65{
Liran Alon04140b42018-03-23 03:01:31 +030066 vcpu->arch.interrupt.injected = false;
Avi Kivity937a7ea2008-07-03 15:17:01 +030067}
68
Gleb Natapov3298b752009-05-11 13:35:46 +030069static inline bool kvm_event_needs_reinjection(struct kvm_vcpu *vcpu)
70{
Liran Alon04140b42018-03-23 03:01:31 +030071 return vcpu->arch.exception.injected || vcpu->arch.interrupt.injected ||
Gleb Natapov3298b752009-05-11 13:35:46 +030072 vcpu->arch.nmi_injected;
73}
Gleb Natapov66fd3f72009-05-11 13:35:50 +030074
75static inline bool kvm_exception_is_soft(unsigned int nr)
76{
77 return (nr == BP_VECTOR) || (nr == OF_VECTOR);
78}
Gleb Natapovfc61b802009-07-05 17:39:35 +030079
Avi Kivity3eeb3282010-01-21 15:31:48 +020080static inline bool is_protmode(struct kvm_vcpu *vcpu)
81{
82 return kvm_read_cr0_bits(vcpu, X86_CR0_PE);
83}
84
Avi Kivity836a1b32010-01-21 15:31:49 +020085static inline int is_long_mode(struct kvm_vcpu *vcpu)
86{
87#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +020088 return vcpu->arch.efer & EFER_LMA;
Avi Kivity836a1b32010-01-21 15:31:49 +020089#else
90 return 0;
91#endif
92}
93
Nadav Amit57773922014-06-18 17:19:23 +030094static inline bool is_64_bit_mode(struct kvm_vcpu *vcpu)
95{
96 int cs_db, cs_l;
97
98 if (!is_long_mode(vcpu))
99 return false;
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700100 kvm_x86_ops.get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Nadav Amit57773922014-06-18 17:19:23 +0300101 return cs_l;
102}
103
Yu Zhang855feb62017-08-24 20:27:55 +0800104static inline bool is_la57_mode(struct kvm_vcpu *vcpu)
105{
106#ifdef CONFIG_X86_64
107 return (vcpu->arch.efer & EFER_LMA) &&
108 kvm_read_cr4_bits(vcpu, X86_CR4_LA57);
109#else
110 return 0;
111#endif
112}
113
Marc Orr04473782018-06-20 17:21:29 -0700114static inline bool x86_exception_has_error_code(unsigned int vector)
115{
116 static u32 exception_has_error_code = BIT(DF_VECTOR) | BIT(TS_VECTOR) |
117 BIT(NP_VECTOR) | BIT(SS_VECTOR) | BIT(GP_VECTOR) |
118 BIT(PF_VECTOR) | BIT(AC_VECTOR);
119
120 return (1U << vector) & exception_has_error_code;
121}
122
Joerg Roedel6539e732010-09-10 17:30:50 +0200123static inline bool mmu_is_nested(struct kvm_vcpu *vcpu)
124{
125 return vcpu->arch.walk_mmu == &vcpu->arch.nested_mmu;
126}
127
Sean Christophersoneeeb4f62020-03-20 14:28:20 -0700128static inline void kvm_vcpu_flush_tlb_current(struct kvm_vcpu *vcpu)
129{
130 ++vcpu->stat.tlb_flush;
131 kvm_x86_ops.tlb_flush_current(vcpu);
132}
133
Avi Kivity836a1b32010-01-21 15:31:49 +0200134static inline int is_pae(struct kvm_vcpu *vcpu)
135{
136 return kvm_read_cr4_bits(vcpu, X86_CR4_PAE);
137}
138
139static inline int is_pse(struct kvm_vcpu *vcpu)
140{
141 return kvm_read_cr4_bits(vcpu, X86_CR4_PSE);
142}
143
144static inline int is_paging(struct kvm_vcpu *vcpu)
145{
Davidlohr Buesoc36fc042012-03-08 12:45:54 +0100146 return likely(kvm_read_cr0_bits(vcpu, X86_CR0_PG));
Avi Kivity836a1b32010-01-21 15:31:49 +0200147}
148
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +0200149static inline bool is_pae_paging(struct kvm_vcpu *vcpu)
150{
151 return !is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu);
152}
153
Yu Zhangfd8cb432017-08-24 20:27:56 +0800154static inline u8 vcpu_virt_addr_bits(struct kvm_vcpu *vcpu)
155{
156 return kvm_read_cr4_bits(vcpu, X86_CR4_LA57) ? 57 : 48;
157}
158
Yu Zhangfd8cb432017-08-24 20:27:56 +0800159static inline u64 get_canonical(u64 la, u8 vaddr_bits)
160{
161 return ((int64_t)la << (64 - vaddr_bits)) >> (64 - vaddr_bits);
162}
163
164static inline bool is_noncanonical_address(u64 la, struct kvm_vcpu *vcpu)
165{
Yu Zhangfd8cb432017-08-24 20:27:56 +0800166 return get_canonical(la, vcpu_virt_addr_bits(vcpu)) != la;
Yu Zhangfd8cb432017-08-24 20:27:56 +0800167}
168
Xiao Guangrongbebb1062011-07-12 03:23:20 +0800169static inline void vcpu_cache_mmio_info(struct kvm_vcpu *vcpu,
170 gva_t gva, gfn_t gfn, unsigned access)
171{
Sean Christophersonddfd1732019-02-05 13:01:13 -0800172 u64 gen = kvm_memslots(vcpu->kvm)->generation;
173
Sean Christopherson361209e2019-02-05 13:01:14 -0800174 if (unlikely(gen & KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS))
Sean Christophersonddfd1732019-02-05 13:01:13 -0800175 return;
176
Paolo Bonzini9034e6e2017-08-17 18:36:58 +0200177 /*
178 * If this is a shadow nested page table, the "GVA" is
179 * actually a nGPA.
180 */
181 vcpu->arch.mmio_gva = mmu_is_nested(vcpu) ? 0 : gva & PAGE_MASK;
Sean Christopherson871bd032019-08-01 13:35:21 -0700182 vcpu->arch.mmio_access = access;
Xiao Guangrongbebb1062011-07-12 03:23:20 +0800183 vcpu->arch.mmio_gfn = gfn;
Sean Christophersonddfd1732019-02-05 13:01:13 -0800184 vcpu->arch.mmio_gen = gen;
David Matlack56f17dd2014-08-18 15:46:07 -0700185}
186
187static inline bool vcpu_match_mmio_gen(struct kvm_vcpu *vcpu)
188{
189 return vcpu->arch.mmio_gen == kvm_memslots(vcpu->kvm)->generation;
Xiao Guangrongbebb1062011-07-12 03:23:20 +0800190}
191
192/*
David Matlack56f17dd2014-08-18 15:46:07 -0700193 * Clear the mmio cache info for the given gva. If gva is MMIO_GVA_ANY, we
194 * clear all mmio cache info.
Xiao Guangrongbebb1062011-07-12 03:23:20 +0800195 */
David Matlack56f17dd2014-08-18 15:46:07 -0700196#define MMIO_GVA_ANY (~(gva_t)0)
197
Xiao Guangrongbebb1062011-07-12 03:23:20 +0800198static inline void vcpu_clear_mmio_info(struct kvm_vcpu *vcpu, gva_t gva)
199{
David Matlack56f17dd2014-08-18 15:46:07 -0700200 if (gva != MMIO_GVA_ANY && vcpu->arch.mmio_gva != (gva & PAGE_MASK))
Xiao Guangrongbebb1062011-07-12 03:23:20 +0800201 return;
202
203 vcpu->arch.mmio_gva = 0;
204}
205
206static inline bool vcpu_match_mmio_gva(struct kvm_vcpu *vcpu, unsigned long gva)
207{
David Matlack56f17dd2014-08-18 15:46:07 -0700208 if (vcpu_match_mmio_gen(vcpu) && vcpu->arch.mmio_gva &&
209 vcpu->arch.mmio_gva == (gva & PAGE_MASK))
Xiao Guangrongbebb1062011-07-12 03:23:20 +0800210 return true;
211
212 return false;
213}
214
215static inline bool vcpu_match_mmio_gpa(struct kvm_vcpu *vcpu, gpa_t gpa)
216{
David Matlack56f17dd2014-08-18 15:46:07 -0700217 if (vcpu_match_mmio_gen(vcpu) && vcpu->arch.mmio_gfn &&
218 vcpu->arch.mmio_gfn == gpa >> PAGE_SHIFT)
Xiao Guangrongbebb1062011-07-12 03:23:20 +0800219 return true;
220
221 return false;
222}
223
Sean Christopherson489cbcf2019-09-27 14:45:20 -0700224static inline unsigned long kvm_register_readl(struct kvm_vcpu *vcpu, int reg)
Nadav Amit57773922014-06-18 17:19:23 +0300225{
226 unsigned long val = kvm_register_read(vcpu, reg);
227
228 return is_64_bit_mode(vcpu) ? val : (u32)val;
229}
230
Nadav Amit27e6fb52014-06-18 17:19:26 +0300231static inline void kvm_register_writel(struct kvm_vcpu *vcpu,
Sean Christopherson489cbcf2019-09-27 14:45:20 -0700232 int reg, unsigned long val)
Nadav Amit27e6fb52014-06-18 17:19:26 +0300233{
234 if (!is_64_bit_mode(vcpu))
235 val = (u32)val;
236 return kvm_register_write(vcpu, reg, val);
237}
238
Paolo Bonzini41dbc6b2015-07-23 08:22:45 +0200239static inline bool kvm_check_has_quirk(struct kvm *kvm, u64 quirk)
240{
241 return !(kvm->arch.disabled_quirks & quirk);
242}
243
Liran Alon27cbe7d2019-11-11 11:16:40 +0200244static inline bool kvm_vcpu_latch_init(struct kvm_vcpu *vcpu)
245{
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700246 return is_smm(vcpu) || kvm_x86_ops.apic_init_signal_blocked(vcpu);
Liran Alon27cbe7d2019-11-11 11:16:40 +0200247}
248
Nicholas Krausebab5bb32015-01-01 22:05:18 -0500249void kvm_set_pending_timer(struct kvm_vcpu *vcpu);
Sean Christopherson9497e1f2019-08-27 14:40:36 -0700250void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +0800251
Will Auld8fe8ab42012-11-29 12:42:12 -0800252void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr);
Paolo Bonzini108b2492016-09-01 14:21:03 +0200253u64 get_kvmclock_ns(struct kvm *kvm);
Zachary Amsden99e3e302010-08-19 22:07:17 -1000254
Paolo Bonzinice14e868a2018-06-06 17:37:49 +0200255int kvm_read_guest_virt(struct kvm_vcpu *vcpu,
Nadav Har'El064aea72011-05-25 23:04:56 +0300256 gva_t addr, void *val, unsigned int bytes,
257 struct x86_exception *exception);
258
Paolo Bonzinice14e868a2018-06-06 17:37:49 +0200259int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu,
Nadav Har'El6a4d7552011-05-25 23:08:00 +0300260 gva_t addr, void *val, unsigned int bytes,
261 struct x86_exception *exception);
262
Wanpeng Li082d06e2018-04-03 16:28:48 -0700263int handle_ud(struct kvm_vcpu *vcpu);
264
Jim Mattsonda998b42018-10-16 14:29:22 -0700265void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu);
266
Xiao Guangrong19efffa2015-06-15 16:55:31 +0800267void kvm_vcpu_mtrr_init(struct kvm_vcpu *vcpu);
Xiao Guangrongff536042015-06-15 16:55:22 +0800268u8 kvm_mtrr_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn);
Nadav Amit45666542014-09-18 22:39:44 +0300269bool kvm_mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data);
Xiao Guangrongff536042015-06-15 16:55:22 +0800270int kvm_mtrr_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data);
271int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata);
Xiao Guangrong6a39bbc2015-06-15 16:55:35 +0800272bool kvm_mtrr_check_gfn_range_consistency(struct kvm_vcpu *vcpu, gfn_t gfn,
273 int page_num);
Feng Wu520040142016-01-25 16:53:33 +0800274bool kvm_vector_hashing_enabled(void);
Sean Christopherson736c2912019-12-06 15:57:14 -0800275int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
Sean Christophersonc60658d2018-08-23 13:56:53 -0700276 int emulation_type, void *insn, int insn_len);
Wanpeng Li404d5d72020-04-28 14:23:25 +0800277fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu);
Nadav Amit45666542014-09-18 22:39:44 +0300278
Avi Kivity00b27a32011-11-23 16:30:32 +0200279extern u64 host_xcr0;
Sean Christophersoncfc48182020-03-02 15:56:23 -0800280extern u64 supported_xcr0;
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100281extern u64 supported_xss;
Paolo Bonzini4ff41732014-02-24 12:15:16 +0100282
Sean Christopherson615a4ae2020-03-02 15:56:25 -0800283static inline bool kvm_mpx_supported(void)
284{
285 return (supported_xcr0 & (XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR))
286 == (XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR);
287}
288
Marcelo Tosatti9ed96e82014-01-06 12:00:02 -0200289extern unsigned int min_timer_period_us;
290
Liran Alonc4ae60e2018-03-12 13:12:47 +0200291extern bool enable_vmware_backdoor;
292
Wanpeng Li0c5f81d2019-07-06 09:26:51 +0800293extern int pi_inject_timer;
294
Gleb Natapov54e98182012-08-05 15:58:32 +0300295extern struct static_key kvm_no_apic_vcpu;
Paolo Bonzinib51012d2016-01-22 11:39:22 +0100296
Marcelo Tosatti8d93c872016-06-20 22:28:02 -0300297static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec)
298{
299 return pvclock_scale_delta(nsec, vcpu->arch.virtual_tsc_mult,
300 vcpu->arch.virtual_tsc_shift);
301}
302
Paolo Bonzinib51012d2016-01-22 11:39:22 +0100303/* Same "calling convention" as do_div:
304 * - divide (n << 32) by base
305 * - put result in n
306 * - return remainder
307 */
308#define do_shl32_div32(n, base) \
309 ({ \
310 u32 __quot, __rem; \
311 asm("divl %2" : "=a" (__quot), "=d" (__rem) \
312 : "rm" (base), "0" (0), "1" ((u32) n)); \
313 n = __quot; \
314 __rem; \
315 })
316
Wanpeng Li4d5422c2018-03-12 04:53:02 -0700317static inline bool kvm_mwait_in_guest(struct kvm *kvm)
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +0200318{
Wanpeng Li4d5422c2018-03-12 04:53:02 -0700319 return kvm->arch.mwait_in_guest;
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +0200320}
321
Wanpeng Licaa057a2018-03-12 04:53:03 -0700322static inline bool kvm_hlt_in_guest(struct kvm *kvm)
323{
324 return kvm->arch.hlt_in_guest;
325}
326
Wanpeng Lib31c1142018-03-12 04:53:04 -0700327static inline bool kvm_pause_in_guest(struct kvm *kvm)
328{
329 return kvm->arch.pause_in_guest;
330}
331
Wanpeng Lib5170062019-05-21 14:06:53 +0800332static inline bool kvm_cstate_in_guest(struct kvm *kvm)
333{
334 return kvm->arch.cstate_in_guest;
335}
336
Andi Kleendd60d212017-07-25 17:20:32 -0700337DECLARE_PER_CPU(struct kvm_vcpu *, current_vcpu);
338
339static inline void kvm_before_interrupt(struct kvm_vcpu *vcpu)
340{
341 __this_cpu_write(current_vcpu, vcpu);
342}
343
344static inline void kvm_after_interrupt(struct kvm_vcpu *vcpu)
345{
346 __this_cpu_write(current_vcpu, NULL);
347}
348
Paolo Bonzini674ea352019-04-10 11:41:40 +0200349
350static inline bool kvm_pat_valid(u64 data)
351{
352 if (data & 0xF8F8F8F8F8F8F8F8ull)
353 return false;
354 /* 0, 1, 4, 5, 6, 7 are valid values. */
355 return (data | ((data & 0x0202020202020202ull) << 1)) == data;
356}
357
Sean Christopherson9b5e8532020-01-24 15:07:22 -0800358static inline bool kvm_dr7_valid(u64 data)
Krish Sadhukhanb91991b2020-01-15 19:54:32 -0500359{
360 /* Bits [63:32] are reserved */
361 return !(data >> 32);
362}
363
Aaron Lewis139a12c2019-10-21 16:30:25 -0700364void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu);
365void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu);
Paolo Bonzini6441fa62020-01-20 16:33:06 +0100366u64 kvm_spec_ctrl_valid_bits(struct kvm_vcpu *vcpu);
Wanpeng Li5a9f5442020-04-28 14:23:26 +0800367bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu);
Paolo Bonzini674ea352019-04-10 11:41:40 +0200368
Avi Kivity26eef702008-07-03 14:59:22 +0300369#endif