blob: 7a7dd5aa858616f5c97da292ca54667847c50fcd [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;
100 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
101 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
Avi Kivity836a1b32010-01-21 15:31:49 +0200128static inline int is_pae(struct kvm_vcpu *vcpu)
129{
130 return kvm_read_cr4_bits(vcpu, X86_CR4_PAE);
131}
132
133static inline int is_pse(struct kvm_vcpu *vcpu)
134{
135 return kvm_read_cr4_bits(vcpu, X86_CR4_PSE);
136}
137
138static inline int is_paging(struct kvm_vcpu *vcpu)
139{
Davidlohr Buesoc36fc042012-03-08 12:45:54 +0100140 return likely(kvm_read_cr0_bits(vcpu, X86_CR0_PG));
Avi Kivity836a1b32010-01-21 15:31:49 +0200141}
142
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +0200143static inline bool is_pae_paging(struct kvm_vcpu *vcpu)
144{
145 return !is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu);
146}
147
Yu Zhangfd8cb432017-08-24 20:27:56 +0800148static inline u8 vcpu_virt_addr_bits(struct kvm_vcpu *vcpu)
149{
150 return kvm_read_cr4_bits(vcpu, X86_CR4_LA57) ? 57 : 48;
151}
152
Yu Zhangfd8cb432017-08-24 20:27:56 +0800153static inline u64 get_canonical(u64 la, u8 vaddr_bits)
154{
155 return ((int64_t)la << (64 - vaddr_bits)) >> (64 - vaddr_bits);
156}
157
158static inline bool is_noncanonical_address(u64 la, struct kvm_vcpu *vcpu)
159{
Yu Zhangfd8cb432017-08-24 20:27:56 +0800160 return get_canonical(la, vcpu_virt_addr_bits(vcpu)) != la;
Yu Zhangfd8cb432017-08-24 20:27:56 +0800161}
162
Xiao Guangrongbebb1062011-07-12 03:23:20 +0800163static inline void vcpu_cache_mmio_info(struct kvm_vcpu *vcpu,
164 gva_t gva, gfn_t gfn, unsigned access)
165{
Sean Christophersonddfd1732019-02-05 13:01:13 -0800166 u64 gen = kvm_memslots(vcpu->kvm)->generation;
167
Sean Christopherson361209e2019-02-05 13:01:14 -0800168 if (unlikely(gen & KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS))
Sean Christophersonddfd1732019-02-05 13:01:13 -0800169 return;
170
Paolo Bonzini9034e6e2017-08-17 18:36:58 +0200171 /*
172 * If this is a shadow nested page table, the "GVA" is
173 * actually a nGPA.
174 */
175 vcpu->arch.mmio_gva = mmu_is_nested(vcpu) ? 0 : gva & PAGE_MASK;
Sean Christopherson871bd032019-08-01 13:35:21 -0700176 vcpu->arch.mmio_access = access;
Xiao Guangrongbebb1062011-07-12 03:23:20 +0800177 vcpu->arch.mmio_gfn = gfn;
Sean Christophersonddfd1732019-02-05 13:01:13 -0800178 vcpu->arch.mmio_gen = gen;
David Matlack56f17dd2014-08-18 15:46:07 -0700179}
180
181static inline bool vcpu_match_mmio_gen(struct kvm_vcpu *vcpu)
182{
183 return vcpu->arch.mmio_gen == kvm_memslots(vcpu->kvm)->generation;
Xiao Guangrongbebb1062011-07-12 03:23:20 +0800184}
185
186/*
David Matlack56f17dd2014-08-18 15:46:07 -0700187 * Clear the mmio cache info for the given gva. If gva is MMIO_GVA_ANY, we
188 * clear all mmio cache info.
Xiao Guangrongbebb1062011-07-12 03:23:20 +0800189 */
David Matlack56f17dd2014-08-18 15:46:07 -0700190#define MMIO_GVA_ANY (~(gva_t)0)
191
Xiao Guangrongbebb1062011-07-12 03:23:20 +0800192static inline void vcpu_clear_mmio_info(struct kvm_vcpu *vcpu, gva_t gva)
193{
David Matlack56f17dd2014-08-18 15:46:07 -0700194 if (gva != MMIO_GVA_ANY && vcpu->arch.mmio_gva != (gva & PAGE_MASK))
Xiao Guangrongbebb1062011-07-12 03:23:20 +0800195 return;
196
197 vcpu->arch.mmio_gva = 0;
198}
199
200static inline bool vcpu_match_mmio_gva(struct kvm_vcpu *vcpu, unsigned long gva)
201{
David Matlack56f17dd2014-08-18 15:46:07 -0700202 if (vcpu_match_mmio_gen(vcpu) && vcpu->arch.mmio_gva &&
203 vcpu->arch.mmio_gva == (gva & PAGE_MASK))
Xiao Guangrongbebb1062011-07-12 03:23:20 +0800204 return true;
205
206 return false;
207}
208
209static inline bool vcpu_match_mmio_gpa(struct kvm_vcpu *vcpu, gpa_t gpa)
210{
David Matlack56f17dd2014-08-18 15:46:07 -0700211 if (vcpu_match_mmio_gen(vcpu) && vcpu->arch.mmio_gfn &&
212 vcpu->arch.mmio_gfn == gpa >> PAGE_SHIFT)
Xiao Guangrongbebb1062011-07-12 03:23:20 +0800213 return true;
214
215 return false;
216}
217
Sean Christopherson489cbcf2019-09-27 14:45:20 -0700218static inline unsigned long kvm_register_readl(struct kvm_vcpu *vcpu, int reg)
Nadav Amit57773922014-06-18 17:19:23 +0300219{
220 unsigned long val = kvm_register_read(vcpu, reg);
221
222 return is_64_bit_mode(vcpu) ? val : (u32)val;
223}
224
Nadav Amit27e6fb52014-06-18 17:19:26 +0300225static inline void kvm_register_writel(struct kvm_vcpu *vcpu,
Sean Christopherson489cbcf2019-09-27 14:45:20 -0700226 int reg, unsigned long val)
Nadav Amit27e6fb52014-06-18 17:19:26 +0300227{
228 if (!is_64_bit_mode(vcpu))
229 val = (u32)val;
230 return kvm_register_write(vcpu, reg, val);
231}
232
Paolo Bonzini41dbc6b2015-07-23 08:22:45 +0200233static inline bool kvm_check_has_quirk(struct kvm *kvm, u64 quirk)
234{
235 return !(kvm->arch.disabled_quirks & quirk);
236}
237
Liran Alon27cbe7d2019-11-11 11:16:40 +0200238static inline bool kvm_vcpu_latch_init(struct kvm_vcpu *vcpu)
239{
240 return is_smm(vcpu) || kvm_x86_ops->apic_init_signal_blocked(vcpu);
241}
242
Nicholas Krausebab5bb32015-01-01 22:05:18 -0500243void kvm_set_pending_timer(struct kvm_vcpu *vcpu);
Sean Christopherson9497e1f2019-08-27 14:40:36 -0700244void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +0800245
Will Auld8fe8ab42012-11-29 12:42:12 -0800246void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr);
Paolo Bonzini108b2492016-09-01 14:21:03 +0200247u64 get_kvmclock_ns(struct kvm *kvm);
Zachary Amsden99e3e302010-08-19 22:07:17 -1000248
Paolo Bonzinice14e868a2018-06-06 17:37:49 +0200249int kvm_read_guest_virt(struct kvm_vcpu *vcpu,
Nadav Har'El064aea72011-05-25 23:04:56 +0300250 gva_t addr, void *val, unsigned int bytes,
251 struct x86_exception *exception);
252
Paolo Bonzinice14e868a2018-06-06 17:37:49 +0200253int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu,
Nadav Har'El6a4d7552011-05-25 23:08:00 +0300254 gva_t addr, void *val, unsigned int bytes,
255 struct x86_exception *exception);
256
Wanpeng Li082d06e2018-04-03 16:28:48 -0700257int handle_ud(struct kvm_vcpu *vcpu);
258
Jim Mattsonda998b42018-10-16 14:29:22 -0700259void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu);
260
Xiao Guangrong19efffa2015-06-15 16:55:31 +0800261void kvm_vcpu_mtrr_init(struct kvm_vcpu *vcpu);
Xiao Guangrongff536042015-06-15 16:55:22 +0800262u8 kvm_mtrr_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn);
Nadav Amit45666542014-09-18 22:39:44 +0300263bool kvm_mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data);
Xiao Guangrongff536042015-06-15 16:55:22 +0800264int kvm_mtrr_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data);
265int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata);
Xiao Guangrong6a39bbc2015-06-15 16:55:35 +0800266bool kvm_mtrr_check_gfn_range_consistency(struct kvm_vcpu *vcpu, gfn_t gfn,
267 int page_num);
Feng Wu520040142016-01-25 16:53:33 +0800268bool kvm_vector_hashing_enabled(void);
Sean Christopherson736c2912019-12-06 15:57:14 -0800269int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
Sean Christophersonc60658d2018-08-23 13:56:53 -0700270 int emulation_type, void *insn, int insn_len);
Wanpeng Li1e9e2622019-11-21 11:17:11 +0800271enum exit_fastpath_completion handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu);
Nadav Amit45666542014-09-18 22:39:44 +0300272
Avi Kivity00b27a32011-11-23 16:30:32 +0200273extern u64 host_xcr0;
Sean Christophersoncfc48182020-03-02 15:56:23 -0800274extern u64 supported_xcr0;
Paolo Bonzini4ff41732014-02-24 12:15:16 +0100275
Marcelo Tosatti9ed96e82014-01-06 12:00:02 -0200276extern unsigned int min_timer_period_us;
277
Liran Alonc4ae60e2018-03-12 13:12:47 +0200278extern bool enable_vmware_backdoor;
279
Wanpeng Li0c5f81d2019-07-06 09:26:51 +0800280extern int pi_inject_timer;
281
Gleb Natapov54e98182012-08-05 15:58:32 +0300282extern struct static_key kvm_no_apic_vcpu;
Paolo Bonzinib51012d2016-01-22 11:39:22 +0100283
Marcelo Tosatti8d93c872016-06-20 22:28:02 -0300284static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec)
285{
286 return pvclock_scale_delta(nsec, vcpu->arch.virtual_tsc_mult,
287 vcpu->arch.virtual_tsc_shift);
288}
289
Paolo Bonzinib51012d2016-01-22 11:39:22 +0100290/* Same "calling convention" as do_div:
291 * - divide (n << 32) by base
292 * - put result in n
293 * - return remainder
294 */
295#define do_shl32_div32(n, base) \
296 ({ \
297 u32 __quot, __rem; \
298 asm("divl %2" : "=a" (__quot), "=d" (__rem) \
299 : "rm" (base), "0" (0), "1" ((u32) n)); \
300 n = __quot; \
301 __rem; \
302 })
303
Wanpeng Li4d5422c2018-03-12 04:53:02 -0700304static inline bool kvm_mwait_in_guest(struct kvm *kvm)
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +0200305{
Wanpeng Li4d5422c2018-03-12 04:53:02 -0700306 return kvm->arch.mwait_in_guest;
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +0200307}
308
Wanpeng Licaa057a2018-03-12 04:53:03 -0700309static inline bool kvm_hlt_in_guest(struct kvm *kvm)
310{
311 return kvm->arch.hlt_in_guest;
312}
313
Wanpeng Lib31c1142018-03-12 04:53:04 -0700314static inline bool kvm_pause_in_guest(struct kvm *kvm)
315{
316 return kvm->arch.pause_in_guest;
317}
318
Wanpeng Lib5170062019-05-21 14:06:53 +0800319static inline bool kvm_cstate_in_guest(struct kvm *kvm)
320{
321 return kvm->arch.cstate_in_guest;
322}
323
Andi Kleendd60d212017-07-25 17:20:32 -0700324DECLARE_PER_CPU(struct kvm_vcpu *, current_vcpu);
325
326static inline void kvm_before_interrupt(struct kvm_vcpu *vcpu)
327{
328 __this_cpu_write(current_vcpu, vcpu);
329}
330
331static inline void kvm_after_interrupt(struct kvm_vcpu *vcpu)
332{
333 __this_cpu_write(current_vcpu, NULL);
334}
335
Paolo Bonzini674ea352019-04-10 11:41:40 +0200336
337static inline bool kvm_pat_valid(u64 data)
338{
339 if (data & 0xF8F8F8F8F8F8F8F8ull)
340 return false;
341 /* 0, 1, 4, 5, 6, 7 are valid values. */
342 return (data | ((data & 0x0202020202020202ull) << 1)) == data;
343}
344
Sean Christopherson9b5e8532020-01-24 15:07:22 -0800345static inline bool kvm_dr7_valid(u64 data)
Krish Sadhukhanb91991b2020-01-15 19:54:32 -0500346{
347 /* Bits [63:32] are reserved */
348 return !(data >> 32);
349}
350
Aaron Lewis139a12c2019-10-21 16:30:25 -0700351void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu);
352void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu);
Paolo Bonzini6441fa62020-01-20 16:33:06 +0100353u64 kvm_spec_ctrl_valid_bits(struct kvm_vcpu *vcpu);
Paolo Bonzini674ea352019-04-10 11:41:40 +0200354
Avi Kivity26eef702008-07-03 14:59:22 +0300355#endif