blob: 635b75f9e14540aff2aceb6e4b1c5c3221c444ab [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>
Uros Bizjak3f1a18b2020-10-29 14:56:00 +01006#include <asm/mce.h>
Marcelo Tosatti8d93c872016-06-20 22:28:02 -03007#include <asm/pvclock.h>
Avi Kivity3eeb3282010-01-21 15:31:48 +02008#include "kvm_cache_regs.h"
Sean Christopherson2f728d62020-02-18 15:29:49 -08009#include "kvm_emulate.h"
Avi Kivity26eef702008-07-03 14:59:22 +030010
Uros Bizjak65297342021-08-09 10:39:55 -070011void kvm_spurious_fault(void);
12
Sean Christophersonbc908e02021-05-04 17:27:35 -070013static __always_inline void kvm_guest_enter_irqoff(void)
14{
15 /*
16 * VMENTER enables interrupts (host state), but the kernel state is
17 * interrupts disabled when this is invoked. Also tell RCU about
18 * it. This is the same logic as for exit_to_user_mode().
19 *
20 * This ensures that e.g. latency analysis on the host observes
21 * guest mode as interrupt enabled.
22 *
23 * guest_enter_irqoff() informs context tracking about the
24 * transition to guest mode and if enabled adjusts RCU state
25 * accordingly.
26 */
27 instrumentation_begin();
28 trace_hardirqs_on_prepare();
29 lockdep_hardirqs_on_prepare(CALLER_ADDR0);
30 instrumentation_end();
31
32 guest_enter_irqoff();
33 lockdep_hardirqs_on(CALLER_ADDR0);
34}
35
36static __always_inline void kvm_guest_exit_irqoff(void)
37{
38 /*
39 * VMEXIT disables interrupts (host state), but tracing and lockdep
40 * have them in state 'on' as recorded before entering guest mode.
41 * Same as enter_from_user_mode().
42 *
43 * context_tracking_guest_exit() restores host context and reinstates
44 * RCU if enabled and required.
45 *
46 * This needs to be done immediately after VM-Exit, before any code
47 * that might contain tracepoints or call out to the greater world,
48 * e.g. before x86_spec_ctrl_restore_host().
49 */
50 lockdep_hardirqs_off(CALLER_ADDR0);
51 context_tracking_guest_exit();
52
53 instrumentation_begin();
54 trace_hardirqs_off_finish();
55 instrumentation_end();
56}
57
Sean Christopherson648fc8a2021-02-03 16:01:16 -080058#define KVM_NESTED_VMENTER_CONSISTENCY_CHECK(consistency_check) \
59({ \
60 bool failed = (consistency_check); \
61 if (failed) \
62 trace_kvm_nested_vmenter_failed(#consistency_check, 0); \
63 failed; \
64})
65
Babu Mogerc8e88712018-03-16 16:37:24 -040066#define KVM_DEFAULT_PLE_GAP 128
67#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
68#define KVM_DEFAULT_PLE_WINDOW_GROW 2
69#define KVM_DEFAULT_PLE_WINDOW_SHRINK 0
70#define KVM_VMX_DEFAULT_PLE_WINDOW_MAX UINT_MAX
Babu Moger8566ac82018-03-16 16:37:26 -040071#define KVM_SVM_DEFAULT_PLE_WINDOW_MAX USHRT_MAX
72#define KVM_SVM_DEFAULT_PLE_WINDOW 3000
Babu Mogerc8e88712018-03-16 16:37:24 -040073
74static inline unsigned int __grow_ple_window(unsigned int val,
75 unsigned int base, unsigned int modifier, unsigned int max)
76{
77 u64 ret = val;
78
79 if (modifier < 1)
80 return base;
81
82 if (modifier < base)
83 ret *= modifier;
84 else
85 ret += modifier;
86
87 return min(ret, (u64)max);
88}
89
90static inline unsigned int __shrink_ple_window(unsigned int val,
91 unsigned int base, unsigned int modifier, unsigned int min)
92{
93 if (modifier < 1)
94 return base;
95
96 if (modifier < base)
97 val /= modifier;
98 else
99 val -= modifier;
100
101 return max(val, min);
102}
103
Radim Krčmář74545702015-04-27 15:11:25 +0200104#define MSR_IA32_CR_PAT_DEFAULT 0x0007040600070406ULL
105
Sean Christopherson40e5f9082021-11-25 01:49:43 +0000106void kvm_service_local_tlb_flush_requests(struct kvm_vcpu *vcpu);
Sean Christophersoncb6a32c2021-03-02 09:45:14 -0800107int kvm_check_nested_events(struct kvm_vcpu *vcpu);
108
Avi Kivity26eef702008-07-03 14:59:22 +0300109static inline void kvm_clear_exception_queue(struct kvm_vcpu *vcpu)
110{
Liran Alon5c7d4f92017-11-19 18:25:43 +0200111 vcpu->arch.exception.pending = false;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700112 vcpu->arch.exception.injected = false;
Avi Kivity26eef702008-07-03 14:59:22 +0300113}
114
Gleb Natapov66fd3f72009-05-11 13:35:50 +0300115static inline void kvm_queue_interrupt(struct kvm_vcpu *vcpu, u8 vector,
116 bool soft)
Avi Kivity937a7ea2008-07-03 15:17:01 +0300117{
Liran Alon04140b42018-03-23 03:01:31 +0300118 vcpu->arch.interrupt.injected = true;
Gleb Natapov66fd3f72009-05-11 13:35:50 +0300119 vcpu->arch.interrupt.soft = soft;
Avi Kivity937a7ea2008-07-03 15:17:01 +0300120 vcpu->arch.interrupt.nr = vector;
121}
122
123static inline void kvm_clear_interrupt_queue(struct kvm_vcpu *vcpu)
124{
Liran Alon04140b42018-03-23 03:01:31 +0300125 vcpu->arch.interrupt.injected = false;
Avi Kivity937a7ea2008-07-03 15:17:01 +0300126}
127
Gleb Natapov3298b752009-05-11 13:35:46 +0300128static inline bool kvm_event_needs_reinjection(struct kvm_vcpu *vcpu)
129{
Liran Alon04140b42018-03-23 03:01:31 +0300130 return vcpu->arch.exception.injected || vcpu->arch.interrupt.injected ||
Gleb Natapov3298b752009-05-11 13:35:46 +0300131 vcpu->arch.nmi_injected;
132}
Gleb Natapov66fd3f72009-05-11 13:35:50 +0300133
134static inline bool kvm_exception_is_soft(unsigned int nr)
135{
136 return (nr == BP_VECTOR) || (nr == OF_VECTOR);
137}
Gleb Natapovfc61b802009-07-05 17:39:35 +0300138
Avi Kivity3eeb3282010-01-21 15:31:48 +0200139static inline bool is_protmode(struct kvm_vcpu *vcpu)
140{
141 return kvm_read_cr0_bits(vcpu, X86_CR0_PE);
142}
143
Avi Kivity836a1b32010-01-21 15:31:49 +0200144static inline int is_long_mode(struct kvm_vcpu *vcpu)
145{
146#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +0200147 return vcpu->arch.efer & EFER_LMA;
Avi Kivity836a1b32010-01-21 15:31:49 +0200148#else
149 return 0;
150#endif
151}
152
Nadav Amit57773922014-06-18 17:19:23 +0300153static inline bool is_64_bit_mode(struct kvm_vcpu *vcpu)
154{
155 int cs_db, cs_l;
156
Tom Lendackyb5aead02021-05-24 12:48:57 -0500157 WARN_ON_ONCE(vcpu->arch.guest_state_protected);
158
Nadav Amit57773922014-06-18 17:19:23 +0300159 if (!is_long_mode(vcpu))
160 return false;
Jason Baronb36464772021-01-14 22:27:56 -0500161 static_call(kvm_x86_get_cs_db_l_bits)(vcpu, &cs_db, &cs_l);
Nadav Amit57773922014-06-18 17:19:23 +0300162 return cs_l;
163}
164
Tom Lendackyb5aead02021-05-24 12:48:57 -0500165static inline bool is_64_bit_hypercall(struct kvm_vcpu *vcpu)
166{
167 /*
168 * If running with protected guest state, the CS register is not
169 * accessible. The hypercall register values will have had to been
170 * provided in 64-bit mode, so assume the guest is in 64-bit.
171 */
172 return vcpu->arch.guest_state_protected || is_64_bit_mode(vcpu);
173}
174
Marc Orr04473782018-06-20 17:21:29 -0700175static inline bool x86_exception_has_error_code(unsigned int vector)
176{
177 static u32 exception_has_error_code = BIT(DF_VECTOR) | BIT(TS_VECTOR) |
178 BIT(NP_VECTOR) | BIT(SS_VECTOR) | BIT(GP_VECTOR) |
179 BIT(PF_VECTOR) | BIT(AC_VECTOR);
180
181 return (1U << vector) & exception_has_error_code;
182}
183
Joerg Roedel6539e732010-09-10 17:30:50 +0200184static inline bool mmu_is_nested(struct kvm_vcpu *vcpu)
185{
186 return vcpu->arch.walk_mmu == &vcpu->arch.nested_mmu;
187}
188
Avi Kivity836a1b32010-01-21 15:31:49 +0200189static inline int is_pae(struct kvm_vcpu *vcpu)
190{
191 return kvm_read_cr4_bits(vcpu, X86_CR4_PAE);
192}
193
194static inline int is_pse(struct kvm_vcpu *vcpu)
195{
196 return kvm_read_cr4_bits(vcpu, X86_CR4_PSE);
197}
198
199static inline int is_paging(struct kvm_vcpu *vcpu)
200{
Davidlohr Buesoc36fc042012-03-08 12:45:54 +0100201 return likely(kvm_read_cr0_bits(vcpu, X86_CR0_PG));
Avi Kivity836a1b32010-01-21 15:31:49 +0200202}
203
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +0200204static inline bool is_pae_paging(struct kvm_vcpu *vcpu)
205{
206 return !is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu);
207}
208
Yu Zhangfd8cb432017-08-24 20:27:56 +0800209static inline u8 vcpu_virt_addr_bits(struct kvm_vcpu *vcpu)
210{
211 return kvm_read_cr4_bits(vcpu, X86_CR4_LA57) ? 57 : 48;
212}
213
Yu Zhangfd8cb432017-08-24 20:27:56 +0800214static inline u64 get_canonical(u64 la, u8 vaddr_bits)
215{
216 return ((int64_t)la << (64 - vaddr_bits)) >> (64 - vaddr_bits);
217}
218
219static inline bool is_noncanonical_address(u64 la, struct kvm_vcpu *vcpu)
220{
Yu Zhangfd8cb432017-08-24 20:27:56 +0800221 return get_canonical(la, vcpu_virt_addr_bits(vcpu)) != la;
Yu Zhangfd8cb432017-08-24 20:27:56 +0800222}
223
Xiao Guangrongbebb1062011-07-12 03:23:20 +0800224static inline void vcpu_cache_mmio_info(struct kvm_vcpu *vcpu,
225 gva_t gva, gfn_t gfn, unsigned access)
226{
Sean Christophersonddfd1732019-02-05 13:01:13 -0800227 u64 gen = kvm_memslots(vcpu->kvm)->generation;
228
Sean Christopherson361209e2019-02-05 13:01:14 -0800229 if (unlikely(gen & KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS))
Sean Christophersonddfd1732019-02-05 13:01:13 -0800230 return;
231
Paolo Bonzini9034e6e2017-08-17 18:36:58 +0200232 /*
233 * If this is a shadow nested page table, the "GVA" is
234 * actually a nGPA.
235 */
236 vcpu->arch.mmio_gva = mmu_is_nested(vcpu) ? 0 : gva & PAGE_MASK;
Sean Christopherson871bd032019-08-01 13:35:21 -0700237 vcpu->arch.mmio_access = access;
Xiao Guangrongbebb1062011-07-12 03:23:20 +0800238 vcpu->arch.mmio_gfn = gfn;
Sean Christophersonddfd1732019-02-05 13:01:13 -0800239 vcpu->arch.mmio_gen = gen;
David Matlack56f17dd2014-08-18 15:46:07 -0700240}
241
242static inline bool vcpu_match_mmio_gen(struct kvm_vcpu *vcpu)
243{
244 return vcpu->arch.mmio_gen == kvm_memslots(vcpu->kvm)->generation;
Xiao Guangrongbebb1062011-07-12 03:23:20 +0800245}
246
247/*
David Matlack56f17dd2014-08-18 15:46:07 -0700248 * Clear the mmio cache info for the given gva. If gva is MMIO_GVA_ANY, we
249 * clear all mmio cache info.
Xiao Guangrongbebb1062011-07-12 03:23:20 +0800250 */
David Matlack56f17dd2014-08-18 15:46:07 -0700251#define MMIO_GVA_ANY (~(gva_t)0)
252
Xiao Guangrongbebb1062011-07-12 03:23:20 +0800253static inline void vcpu_clear_mmio_info(struct kvm_vcpu *vcpu, gva_t gva)
254{
David Matlack56f17dd2014-08-18 15:46:07 -0700255 if (gva != MMIO_GVA_ANY && vcpu->arch.mmio_gva != (gva & PAGE_MASK))
Xiao Guangrongbebb1062011-07-12 03:23:20 +0800256 return;
257
258 vcpu->arch.mmio_gva = 0;
259}
260
261static inline bool vcpu_match_mmio_gva(struct kvm_vcpu *vcpu, unsigned long gva)
262{
David Matlack56f17dd2014-08-18 15:46:07 -0700263 if (vcpu_match_mmio_gen(vcpu) && vcpu->arch.mmio_gva &&
264 vcpu->arch.mmio_gva == (gva & PAGE_MASK))
Xiao Guangrongbebb1062011-07-12 03:23:20 +0800265 return true;
266
267 return false;
268}
269
270static inline bool vcpu_match_mmio_gpa(struct kvm_vcpu *vcpu, gpa_t gpa)
271{
David Matlack56f17dd2014-08-18 15:46:07 -0700272 if (vcpu_match_mmio_gen(vcpu) && vcpu->arch.mmio_gfn &&
273 vcpu->arch.mmio_gfn == gpa >> PAGE_SHIFT)
Xiao Guangrongbebb1062011-07-12 03:23:20 +0800274 return true;
275
276 return false;
277}
278
Sean Christopherson27b4a9c42021-04-21 19:21:28 -0700279static inline unsigned long kvm_register_read(struct kvm_vcpu *vcpu, int reg)
Nadav Amit57773922014-06-18 17:19:23 +0300280{
Sean Christopherson27b4a9c42021-04-21 19:21:28 -0700281 unsigned long val = kvm_register_read_raw(vcpu, reg);
Nadav Amit57773922014-06-18 17:19:23 +0300282
283 return is_64_bit_mode(vcpu) ? val : (u32)val;
284}
285
Sean Christopherson27b4a9c42021-04-21 19:21:28 -0700286static inline void kvm_register_write(struct kvm_vcpu *vcpu,
Sean Christopherson489cbcf2019-09-27 14:45:20 -0700287 int reg, unsigned long val)
Nadav Amit27e6fb52014-06-18 17:19:26 +0300288{
289 if (!is_64_bit_mode(vcpu))
290 val = (u32)val;
Sean Christopherson27b4a9c42021-04-21 19:21:28 -0700291 return kvm_register_write_raw(vcpu, reg, val);
Nadav Amit27e6fb52014-06-18 17:19:26 +0300292}
293
Paolo Bonzini41dbc6b2015-07-23 08:22:45 +0200294static inline bool kvm_check_has_quirk(struct kvm *kvm, u64 quirk)
295{
296 return !(kvm->arch.disabled_quirks & quirk);
297}
298
Liran Alon27cbe7d2019-11-11 11:16:40 +0200299static inline bool kvm_vcpu_latch_init(struct kvm_vcpu *vcpu)
300{
Jason Baronb36464772021-01-14 22:27:56 -0500301 return is_smm(vcpu) || static_call(kvm_x86_apic_init_signal_blocked)(vcpu);
Liran Alon27cbe7d2019-11-11 11:16:40 +0200302}
303
Sean Christopherson9497e1f2019-08-27 14:40:36 -0700304void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +0800305
Paolo Bonzini108b2492016-09-01 14:21:03 +0200306u64 get_kvmclock_ns(struct kvm *kvm);
Zachary Amsden99e3e302010-08-19 22:07:17 -1000307
Paolo Bonzinice14e868a2018-06-06 17:37:49 +0200308int kvm_read_guest_virt(struct kvm_vcpu *vcpu,
Nadav Har'El064aea72011-05-25 23:04:56 +0300309 gva_t addr, void *val, unsigned int bytes,
310 struct x86_exception *exception);
311
Paolo Bonzinice14e868a2018-06-06 17:37:49 +0200312int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu,
Nadav Har'El6a4d7552011-05-25 23:08:00 +0300313 gva_t addr, void *val, unsigned int bytes,
314 struct x86_exception *exception);
315
Wanpeng Li082d06e2018-04-03 16:28:48 -0700316int handle_ud(struct kvm_vcpu *vcpu);
317
Jim Mattsonda998b42018-10-16 14:29:22 -0700318void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu);
319
Xiao Guangrong19efffa2015-06-15 16:55:31 +0800320void kvm_vcpu_mtrr_init(struct kvm_vcpu *vcpu);
Xiao Guangrongff536042015-06-15 16:55:22 +0800321u8 kvm_mtrr_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn);
Nadav Amit45666542014-09-18 22:39:44 +0300322bool kvm_mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data);
Xiao Guangrongff536042015-06-15 16:55:22 +0800323int kvm_mtrr_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data);
324int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata);
Xiao Guangrong6a39bbc2015-06-15 16:55:35 +0800325bool kvm_mtrr_check_gfn_range_consistency(struct kvm_vcpu *vcpu, gfn_t gfn,
326 int page_num);
Feng Wu520040142016-01-25 16:53:33 +0800327bool kvm_vector_hashing_enabled(void);
Mohammed Gamal89786142020-07-10 17:48:03 +0200328void kvm_fixup_and_inject_pf_error(struct kvm_vcpu *vcpu, gva_t gva, u16 error_code);
Wei Huang4aa26912021-01-26 03:18:28 -0500329int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type,
330 void *insn, int insn_len);
Sean Christopherson736c2912019-12-06 15:57:14 -0800331int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
Sean Christophersonc60658d2018-08-23 13:56:53 -0700332 int emulation_type, void *insn, int insn_len);
Wanpeng Li404d5d72020-04-28 14:23:25 +0800333fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu);
Nadav Amit45666542014-09-18 22:39:44 +0300334
Avi Kivity00b27a32011-11-23 16:30:32 +0200335extern u64 host_xcr0;
Sean Christophersoncfc48182020-03-02 15:56:23 -0800336extern u64 supported_xcr0;
Tom Lendacky86137772020-12-10 11:10:07 -0600337extern u64 host_xss;
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100338extern u64 supported_xss;
Like Xu4732f242022-01-11 15:38:23 +0800339extern bool enable_pmu;
Paolo Bonzini4ff41732014-02-24 12:15:16 +0100340
Sean Christopherson615a4ae2020-03-02 15:56:25 -0800341static inline bool kvm_mpx_supported(void)
342{
343 return (supported_xcr0 & (XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR))
344 == (XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR);
345}
346
Marcelo Tosatti9ed96e82014-01-06 12:00:02 -0200347extern unsigned int min_timer_period_us;
348
Liran Alonc4ae60e2018-03-12 13:12:47 +0200349extern bool enable_vmware_backdoor;
350
Wanpeng Li0c5f81d2019-07-06 09:26:51 +0800351extern int pi_inject_timer;
352
Like Xud8550662021-01-08 09:36:55 +0800353extern bool report_ignored_msrs;
354
Marcelo Tosatti8d93c872016-06-20 22:28:02 -0300355static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec)
356{
357 return pvclock_scale_delta(nsec, vcpu->arch.virtual_tsc_mult,
358 vcpu->arch.virtual_tsc_shift);
359}
360
Paolo Bonzinib51012d2016-01-22 11:39:22 +0100361/* Same "calling convention" as do_div:
362 * - divide (n << 32) by base
363 * - put result in n
364 * - return remainder
365 */
366#define do_shl32_div32(n, base) \
367 ({ \
368 u32 __quot, __rem; \
369 asm("divl %2" : "=a" (__quot), "=d" (__rem) \
370 : "rm" (base), "0" (0), "1" ((u32) n)); \
371 n = __quot; \
372 __rem; \
373 })
374
Wanpeng Li4d5422c2018-03-12 04:53:02 -0700375static inline bool kvm_mwait_in_guest(struct kvm *kvm)
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +0200376{
Wanpeng Li4d5422c2018-03-12 04:53:02 -0700377 return kvm->arch.mwait_in_guest;
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +0200378}
379
Wanpeng Licaa057a2018-03-12 04:53:03 -0700380static inline bool kvm_hlt_in_guest(struct kvm *kvm)
381{
382 return kvm->arch.hlt_in_guest;
383}
384
Wanpeng Lib31c1142018-03-12 04:53:04 -0700385static inline bool kvm_pause_in_guest(struct kvm *kvm)
386{
387 return kvm->arch.pause_in_guest;
388}
389
Wanpeng Lib5170062019-05-21 14:06:53 +0800390static inline bool kvm_cstate_in_guest(struct kvm *kvm)
391{
392 return kvm->arch.cstate_in_guest;
393}
394
Sean Christophersondb215752021-11-11 02:07:32 +0000395enum kvm_intr_type {
396 /* Values are arbitrary, but must be non-zero. */
397 KVM_HANDLING_IRQ = 1,
398 KVM_HANDLING_NMI,
399};
Andi Kleendd60d212017-07-25 17:20:32 -0700400
Sean Christophersondb215752021-11-11 02:07:32 +0000401static inline void kvm_before_interrupt(struct kvm_vcpu *vcpu,
402 enum kvm_intr_type intr)
Andi Kleendd60d212017-07-25 17:20:32 -0700403{
Sean Christophersondb215752021-11-11 02:07:32 +0000404 WRITE_ONCE(vcpu->arch.handling_intr_from_guest, (u8)intr);
Andi Kleendd60d212017-07-25 17:20:32 -0700405}
406
407static inline void kvm_after_interrupt(struct kvm_vcpu *vcpu)
408{
Sean Christopherson73cd1072021-11-11 02:07:31 +0000409 WRITE_ONCE(vcpu->arch.handling_intr_from_guest, 0);
Andi Kleendd60d212017-07-25 17:20:32 -0700410}
411
Sean Christopherson73cd1072021-11-11 02:07:31 +0000412static inline bool kvm_handling_nmi_from_guest(struct kvm_vcpu *vcpu)
413{
Sean Christophersondb215752021-11-11 02:07:32 +0000414 return vcpu->arch.handling_intr_from_guest == KVM_HANDLING_NMI;
Sean Christopherson73cd1072021-11-11 02:07:31 +0000415}
Paolo Bonzini674ea352019-04-10 11:41:40 +0200416
417static inline bool kvm_pat_valid(u64 data)
418{
419 if (data & 0xF8F8F8F8F8F8F8F8ull)
420 return false;
421 /* 0, 1, 4, 5, 6, 7 are valid values. */
422 return (data | ((data & 0x0202020202020202ull) << 1)) == data;
423}
424
Sean Christopherson9b5e8532020-01-24 15:07:22 -0800425static inline bool kvm_dr7_valid(u64 data)
Krish Sadhukhanb91991b2020-01-15 19:54:32 -0500426{
427 /* Bits [63:32] are reserved */
428 return !(data >> 32);
429}
Krish Sadhukhanf5f61452020-05-22 18:19:51 -0400430static inline bool kvm_dr6_valid(u64 data)
431{
432 /* Bits [63:32] are reserved */
433 return !(data >> 32);
434}
Krish Sadhukhanb91991b2020-01-15 19:54:32 -0500435
Uros Bizjak3f1a18b2020-10-29 14:56:00 +0100436/*
437 * Trigger machine check on the host. We assume all the MSRs are already set up
438 * by the CPU and that we still run on the same CPU as the MCE occurred on.
439 * We pass a fake environment to the machine check handler because we want
440 * the guest to be always treated like user space, no matter what context
441 * it used internally.
442 */
443static inline void kvm_machine_check(void)
444{
445#if defined(CONFIG_X86_MCE)
446 struct pt_regs regs = {
447 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
448 .flags = X86_EFLAGS_IF,
449 };
450
451 do_machine_check(&regs);
452#endif
453}
454
Aaron Lewis139a12c2019-10-21 16:30:25 -0700455void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu);
456void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu);
Maxim Levitsky841c2be2020-07-08 14:57:31 +0300457int kvm_spec_ctrl_test_value(u64 value);
Sean Christophersonee69c922020-10-06 18:44:16 -0700458bool kvm_is_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Babu Moger3f3393b2020-09-11 14:29:05 -0500459int kvm_handle_memory_failure(struct kvm_vcpu *vcpu, int r,
460 struct x86_exception *e);
Babu Moger97150922020-09-11 14:29:12 -0500461int kvm_handle_invpcid(struct kvm_vcpu *vcpu, unsigned long type, gva_t gva);
Alexander Graf51de8152020-09-25 16:34:17 +0200462bool kvm_msr_allowed(struct kvm_vcpu *vcpu, u32 index, u32 type);
Paolo Bonzini674ea352019-04-10 11:41:40 +0200463
Maxim Levitskycc4cb012020-11-01 13:55:23 +0200464/*
465 * Internal error codes that are used to indicate that MSR emulation encountered
466 * an error that should result in #GP in the guest, unless userspace
467 * handles it.
468 */
469#define KVM_MSR_RET_INVALID 2 /* in-kernel MSR emulation #GP condition */
470#define KVM_MSR_RET_FILTERED 3 /* #GP due to userspace MSR filter */
Peter Xu6abe9c12020-06-22 18:04:41 -0400471
Krish Sadhukhanb899c132020-07-08 00:39:55 +0000472#define __cr4_reserved_bits(__cpu_has, __c) \
473({ \
474 u64 __reserved_bits = CR4_RESERVED_BITS; \
475 \
476 if (!__cpu_has(__c, X86_FEATURE_XSAVE)) \
477 __reserved_bits |= X86_CR4_OSXSAVE; \
478 if (!__cpu_has(__c, X86_FEATURE_SMEP)) \
479 __reserved_bits |= X86_CR4_SMEP; \
480 if (!__cpu_has(__c, X86_FEATURE_SMAP)) \
481 __reserved_bits |= X86_CR4_SMAP; \
482 if (!__cpu_has(__c, X86_FEATURE_FSGSBASE)) \
483 __reserved_bits |= X86_CR4_FSGSBASE; \
484 if (!__cpu_has(__c, X86_FEATURE_PKU)) \
485 __reserved_bits |= X86_CR4_PKE; \
486 if (!__cpu_has(__c, X86_FEATURE_LA57)) \
487 __reserved_bits |= X86_CR4_LA57; \
488 if (!__cpu_has(__c, X86_FEATURE_UMIP)) \
489 __reserved_bits |= X86_CR4_UMIP; \
Paolo Bonzini53efe522020-07-08 07:02:50 -0400490 if (!__cpu_has(__c, X86_FEATURE_VMX)) \
491 __reserved_bits |= X86_CR4_VMXE; \
Vitaly Kuznetsov4683d752021-02-01 15:28:43 +0100492 if (!__cpu_has(__c, X86_FEATURE_PCID)) \
493 __reserved_bits |= X86_CR4_PCIDE; \
Krish Sadhukhanb899c132020-07-08 00:39:55 +0000494 __reserved_bits; \
495})
496
Tom Lendacky8f423a82020-12-10 11:09:53 -0600497int kvm_sev_es_mmio_write(struct kvm_vcpu *vcpu, gpa_t src, unsigned int bytes,
498 void *dst);
499int kvm_sev_es_mmio_read(struct kvm_vcpu *vcpu, gpa_t src, unsigned int bytes,
500 void *dst);
Tom Lendacky7ed9abf2020-12-10 11:09:54 -0600501int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size,
502 unsigned int port, void *data, unsigned int count,
503 int in);
Tom Lendacky8f423a82020-12-10 11:09:53 -0600504
Avi Kivity26eef702008-07-03 14:59:22 +0300505#endif