xref: /linux/arch/x86/kvm/x86.h (revision db215756ae5970aec8ad50257d2eb1678b552b91)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
226eef70cSAvi Kivity #ifndef ARCH_X86_KVM_X86_H
326eef70cSAvi Kivity #define ARCH_X86_KVM_X86_H
426eef70cSAvi Kivity 
526eef70cSAvi Kivity #include <linux/kvm_host.h>
63f1a18b9SUros Bizjak #include <asm/mce.h>
78d93c874SMarcelo Tosatti #include <asm/pvclock.h>
83eeb3288SAvi Kivity #include "kvm_cache_regs.h"
92f728d66SSean Christopherson #include "kvm_emulate.h"
1026eef70cSAvi Kivity 
1165297341SUros Bizjak void kvm_spurious_fault(void);
1265297341SUros Bizjak 
13bc908e09SSean Christopherson static __always_inline void kvm_guest_enter_irqoff(void)
14bc908e09SSean Christopherson {
15bc908e09SSean Christopherson 	/*
16bc908e09SSean Christopherson 	 * VMENTER enables interrupts (host state), but the kernel state is
17bc908e09SSean Christopherson 	 * interrupts disabled when this is invoked. Also tell RCU about
18bc908e09SSean Christopherson 	 * it. This is the same logic as for exit_to_user_mode().
19bc908e09SSean Christopherson 	 *
20bc908e09SSean Christopherson 	 * This ensures that e.g. latency analysis on the host observes
21bc908e09SSean Christopherson 	 * guest mode as interrupt enabled.
22bc908e09SSean Christopherson 	 *
23bc908e09SSean Christopherson 	 * guest_enter_irqoff() informs context tracking about the
24bc908e09SSean Christopherson 	 * transition to guest mode and if enabled adjusts RCU state
25bc908e09SSean Christopherson 	 * accordingly.
26bc908e09SSean Christopherson 	 */
27bc908e09SSean Christopherson 	instrumentation_begin();
28bc908e09SSean Christopherson 	trace_hardirqs_on_prepare();
29bc908e09SSean Christopherson 	lockdep_hardirqs_on_prepare(CALLER_ADDR0);
30bc908e09SSean Christopherson 	instrumentation_end();
31bc908e09SSean Christopherson 
32bc908e09SSean Christopherson 	guest_enter_irqoff();
33bc908e09SSean Christopherson 	lockdep_hardirqs_on(CALLER_ADDR0);
34bc908e09SSean Christopherson }
35bc908e09SSean Christopherson 
36bc908e09SSean Christopherson static __always_inline void kvm_guest_exit_irqoff(void)
37bc908e09SSean Christopherson {
38bc908e09SSean Christopherson 	/*
39bc908e09SSean Christopherson 	 * VMEXIT disables interrupts (host state), but tracing and lockdep
40bc908e09SSean Christopherson 	 * have them in state 'on' as recorded before entering guest mode.
41bc908e09SSean Christopherson 	 * Same as enter_from_user_mode().
42bc908e09SSean Christopherson 	 *
43bc908e09SSean Christopherson 	 * context_tracking_guest_exit() restores host context and reinstates
44bc908e09SSean Christopherson 	 * RCU if enabled and required.
45bc908e09SSean Christopherson 	 *
46bc908e09SSean Christopherson 	 * This needs to be done immediately after VM-Exit, before any code
47bc908e09SSean Christopherson 	 * that might contain tracepoints or call out to the greater world,
48bc908e09SSean Christopherson 	 * e.g. before x86_spec_ctrl_restore_host().
49bc908e09SSean Christopherson 	 */
50bc908e09SSean Christopherson 	lockdep_hardirqs_off(CALLER_ADDR0);
51bc908e09SSean Christopherson 	context_tracking_guest_exit();
52bc908e09SSean Christopherson 
53bc908e09SSean Christopherson 	instrumentation_begin();
54bc908e09SSean Christopherson 	trace_hardirqs_off_finish();
55bc908e09SSean Christopherson 	instrumentation_end();
56bc908e09SSean Christopherson }
57bc908e09SSean Christopherson 
58648fc8aeSSean Christopherson #define KVM_NESTED_VMENTER_CONSISTENCY_CHECK(consistency_check)		\
59648fc8aeSSean Christopherson ({									\
60648fc8aeSSean Christopherson 	bool failed = (consistency_check);				\
61648fc8aeSSean Christopherson 	if (failed)							\
62648fc8aeSSean Christopherson 		trace_kvm_nested_vmenter_failed(#consistency_check, 0);	\
63648fc8aeSSean Christopherson 	failed;								\
64648fc8aeSSean Christopherson })
65648fc8aeSSean Christopherson 
66c8e88717SBabu Moger #define KVM_DEFAULT_PLE_GAP		128
67c8e88717SBabu Moger #define KVM_VMX_DEFAULT_PLE_WINDOW	4096
68c8e88717SBabu Moger #define KVM_DEFAULT_PLE_WINDOW_GROW	2
69c8e88717SBabu Moger #define KVM_DEFAULT_PLE_WINDOW_SHRINK	0
70c8e88717SBabu Moger #define KVM_VMX_DEFAULT_PLE_WINDOW_MAX	UINT_MAX
718566ac8bSBabu Moger #define KVM_SVM_DEFAULT_PLE_WINDOW_MAX	USHRT_MAX
728566ac8bSBabu Moger #define KVM_SVM_DEFAULT_PLE_WINDOW	3000
73c8e88717SBabu Moger 
74c8e88717SBabu Moger static inline unsigned int __grow_ple_window(unsigned int val,
75c8e88717SBabu Moger 		unsigned int base, unsigned int modifier, unsigned int max)
76c8e88717SBabu Moger {
77c8e88717SBabu Moger 	u64 ret = val;
78c8e88717SBabu Moger 
79c8e88717SBabu Moger 	if (modifier < 1)
80c8e88717SBabu Moger 		return base;
81c8e88717SBabu Moger 
82c8e88717SBabu Moger 	if (modifier < base)
83c8e88717SBabu Moger 		ret *= modifier;
84c8e88717SBabu Moger 	else
85c8e88717SBabu Moger 		ret += modifier;
86c8e88717SBabu Moger 
87c8e88717SBabu Moger 	return min(ret, (u64)max);
88c8e88717SBabu Moger }
89c8e88717SBabu Moger 
90c8e88717SBabu Moger static inline unsigned int __shrink_ple_window(unsigned int val,
91c8e88717SBabu Moger 		unsigned int base, unsigned int modifier, unsigned int min)
92c8e88717SBabu Moger {
93c8e88717SBabu Moger 	if (modifier < 1)
94c8e88717SBabu Moger 		return base;
95c8e88717SBabu Moger 
96c8e88717SBabu Moger 	if (modifier < base)
97c8e88717SBabu Moger 		val /= modifier;
98c8e88717SBabu Moger 	else
99c8e88717SBabu Moger 		val -= modifier;
100c8e88717SBabu Moger 
101c8e88717SBabu Moger 	return max(val, min);
102c8e88717SBabu Moger }
103c8e88717SBabu Moger 
10474545705SRadim Krčmář #define MSR_IA32_CR_PAT_DEFAULT  0x0007040600070406ULL
10574545705SRadim Krčmář 
106cb6a32c2SSean Christopherson int kvm_check_nested_events(struct kvm_vcpu *vcpu);
107cb6a32c2SSean Christopherson 
10826eef70cSAvi Kivity static inline void kvm_clear_exception_queue(struct kvm_vcpu *vcpu)
10926eef70cSAvi Kivity {
1105c7d4f9aSLiran Alon 	vcpu->arch.exception.pending = false;
111664f8e26SWanpeng Li 	vcpu->arch.exception.injected = false;
11226eef70cSAvi Kivity }
11326eef70cSAvi Kivity 
11466fd3f7fSGleb Natapov static inline void kvm_queue_interrupt(struct kvm_vcpu *vcpu, u8 vector,
11566fd3f7fSGleb Natapov 	bool soft)
116937a7eaeSAvi Kivity {
11704140b41SLiran Alon 	vcpu->arch.interrupt.injected = true;
11866fd3f7fSGleb Natapov 	vcpu->arch.interrupt.soft = soft;
119937a7eaeSAvi Kivity 	vcpu->arch.interrupt.nr = vector;
120937a7eaeSAvi Kivity }
121937a7eaeSAvi Kivity 
122937a7eaeSAvi Kivity static inline void kvm_clear_interrupt_queue(struct kvm_vcpu *vcpu)
123937a7eaeSAvi Kivity {
12404140b41SLiran Alon 	vcpu->arch.interrupt.injected = false;
125937a7eaeSAvi Kivity }
126937a7eaeSAvi Kivity 
1273298b75cSGleb Natapov static inline bool kvm_event_needs_reinjection(struct kvm_vcpu *vcpu)
1283298b75cSGleb Natapov {
12904140b41SLiran Alon 	return vcpu->arch.exception.injected || vcpu->arch.interrupt.injected ||
1303298b75cSGleb Natapov 		vcpu->arch.nmi_injected;
1313298b75cSGleb Natapov }
13266fd3f7fSGleb Natapov 
13366fd3f7fSGleb Natapov static inline bool kvm_exception_is_soft(unsigned int nr)
13466fd3f7fSGleb Natapov {
13566fd3f7fSGleb Natapov 	return (nr == BP_VECTOR) || (nr == OF_VECTOR);
13666fd3f7fSGleb Natapov }
137fc61b800SGleb Natapov 
1383eeb3288SAvi Kivity static inline bool is_protmode(struct kvm_vcpu *vcpu)
1393eeb3288SAvi Kivity {
1403eeb3288SAvi Kivity 	return kvm_read_cr0_bits(vcpu, X86_CR0_PE);
1413eeb3288SAvi Kivity }
1423eeb3288SAvi Kivity 
143836a1b3cSAvi Kivity static inline int is_long_mode(struct kvm_vcpu *vcpu)
144836a1b3cSAvi Kivity {
145836a1b3cSAvi Kivity #ifdef CONFIG_X86_64
146f6801dffSAvi Kivity 	return vcpu->arch.efer & EFER_LMA;
147836a1b3cSAvi Kivity #else
148836a1b3cSAvi Kivity 	return 0;
149836a1b3cSAvi Kivity #endif
150836a1b3cSAvi Kivity }
151836a1b3cSAvi Kivity 
1525777392eSNadav Amit static inline bool is_64_bit_mode(struct kvm_vcpu *vcpu)
1535777392eSNadav Amit {
1545777392eSNadav Amit 	int cs_db, cs_l;
1555777392eSNadav Amit 
1565777392eSNadav Amit 	if (!is_long_mode(vcpu))
1575777392eSNadav Amit 		return false;
158b3646477SJason Baron 	static_call(kvm_x86_get_cs_db_l_bits)(vcpu, &cs_db, &cs_l);
1595777392eSNadav Amit 	return cs_l;
1605777392eSNadav Amit }
1615777392eSNadav Amit 
1620447378aSMarc Orr static inline bool x86_exception_has_error_code(unsigned int vector)
1630447378aSMarc Orr {
1640447378aSMarc Orr 	static u32 exception_has_error_code = BIT(DF_VECTOR) | BIT(TS_VECTOR) |
1650447378aSMarc Orr 			BIT(NP_VECTOR) | BIT(SS_VECTOR) | BIT(GP_VECTOR) |
1660447378aSMarc Orr 			BIT(PF_VECTOR) | BIT(AC_VECTOR);
1670447378aSMarc Orr 
1680447378aSMarc Orr 	return (1U << vector) & exception_has_error_code;
1690447378aSMarc Orr }
1700447378aSMarc Orr 
1716539e738SJoerg Roedel static inline bool mmu_is_nested(struct kvm_vcpu *vcpu)
1726539e738SJoerg Roedel {
1736539e738SJoerg Roedel 	return vcpu->arch.walk_mmu == &vcpu->arch.nested_mmu;
1746539e738SJoerg Roedel }
1756539e738SJoerg Roedel 
176eeeb4f67SSean Christopherson static inline void kvm_vcpu_flush_tlb_current(struct kvm_vcpu *vcpu)
177eeeb4f67SSean Christopherson {
178eeeb4f67SSean Christopherson 	++vcpu->stat.tlb_flush;
179b3646477SJason Baron 	static_call(kvm_x86_tlb_flush_current)(vcpu);
180eeeb4f67SSean Christopherson }
181eeeb4f67SSean Christopherson 
182836a1b3cSAvi Kivity static inline int is_pae(struct kvm_vcpu *vcpu)
183836a1b3cSAvi Kivity {
184836a1b3cSAvi Kivity 	return kvm_read_cr4_bits(vcpu, X86_CR4_PAE);
185836a1b3cSAvi Kivity }
186836a1b3cSAvi Kivity 
187836a1b3cSAvi Kivity static inline int is_pse(struct kvm_vcpu *vcpu)
188836a1b3cSAvi Kivity {
189836a1b3cSAvi Kivity 	return kvm_read_cr4_bits(vcpu, X86_CR4_PSE);
190836a1b3cSAvi Kivity }
191836a1b3cSAvi Kivity 
192836a1b3cSAvi Kivity static inline int is_paging(struct kvm_vcpu *vcpu)
193836a1b3cSAvi Kivity {
194c36fc04eSDavidlohr Bueso 	return likely(kvm_read_cr0_bits(vcpu, X86_CR0_PG));
195836a1b3cSAvi Kivity }
196836a1b3cSAvi Kivity 
197bf03d4f9SPaolo Bonzini static inline bool is_pae_paging(struct kvm_vcpu *vcpu)
198bf03d4f9SPaolo Bonzini {
199bf03d4f9SPaolo Bonzini 	return !is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu);
200bf03d4f9SPaolo Bonzini }
201bf03d4f9SPaolo Bonzini 
202fd8cb433SYu Zhang static inline u8 vcpu_virt_addr_bits(struct kvm_vcpu *vcpu)
203fd8cb433SYu Zhang {
204fd8cb433SYu Zhang 	return kvm_read_cr4_bits(vcpu, X86_CR4_LA57) ? 57 : 48;
205fd8cb433SYu Zhang }
206fd8cb433SYu Zhang 
207fd8cb433SYu Zhang static inline u64 get_canonical(u64 la, u8 vaddr_bits)
208fd8cb433SYu Zhang {
209fd8cb433SYu Zhang 	return ((int64_t)la << (64 - vaddr_bits)) >> (64 - vaddr_bits);
210fd8cb433SYu Zhang }
211fd8cb433SYu Zhang 
212fd8cb433SYu Zhang static inline bool is_noncanonical_address(u64 la, struct kvm_vcpu *vcpu)
213fd8cb433SYu Zhang {
214fd8cb433SYu Zhang 	return get_canonical(la, vcpu_virt_addr_bits(vcpu)) != la;
215fd8cb433SYu Zhang }
216fd8cb433SYu Zhang 
217bebb106aSXiao Guangrong static inline void vcpu_cache_mmio_info(struct kvm_vcpu *vcpu,
218bebb106aSXiao Guangrong 					gva_t gva, gfn_t gfn, unsigned access)
219bebb106aSXiao Guangrong {
220ddfd1730SSean Christopherson 	u64 gen = kvm_memslots(vcpu->kvm)->generation;
221ddfd1730SSean Christopherson 
222361209e0SSean Christopherson 	if (unlikely(gen & KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS))
223ddfd1730SSean Christopherson 		return;
224ddfd1730SSean Christopherson 
2259034e6e8SPaolo Bonzini 	/*
2269034e6e8SPaolo Bonzini 	 * If this is a shadow nested page table, the "GVA" is
2279034e6e8SPaolo Bonzini 	 * actually a nGPA.
2289034e6e8SPaolo Bonzini 	 */
2299034e6e8SPaolo Bonzini 	vcpu->arch.mmio_gva = mmu_is_nested(vcpu) ? 0 : gva & PAGE_MASK;
230871bd034SSean Christopherson 	vcpu->arch.mmio_access = access;
231bebb106aSXiao Guangrong 	vcpu->arch.mmio_gfn = gfn;
232ddfd1730SSean Christopherson 	vcpu->arch.mmio_gen = gen;
23356f17dd3SDavid Matlack }
23456f17dd3SDavid Matlack 
23556f17dd3SDavid Matlack static inline bool vcpu_match_mmio_gen(struct kvm_vcpu *vcpu)
23656f17dd3SDavid Matlack {
23756f17dd3SDavid Matlack 	return vcpu->arch.mmio_gen == kvm_memslots(vcpu->kvm)->generation;
238bebb106aSXiao Guangrong }
239bebb106aSXiao Guangrong 
240bebb106aSXiao Guangrong /*
24156f17dd3SDavid Matlack  * Clear the mmio cache info for the given gva. If gva is MMIO_GVA_ANY, we
24256f17dd3SDavid Matlack  * clear all mmio cache info.
243bebb106aSXiao Guangrong  */
24456f17dd3SDavid Matlack #define MMIO_GVA_ANY (~(gva_t)0)
24556f17dd3SDavid Matlack 
246bebb106aSXiao Guangrong static inline void vcpu_clear_mmio_info(struct kvm_vcpu *vcpu, gva_t gva)
247bebb106aSXiao Guangrong {
24856f17dd3SDavid Matlack 	if (gva != MMIO_GVA_ANY && vcpu->arch.mmio_gva != (gva & PAGE_MASK))
249bebb106aSXiao Guangrong 		return;
250bebb106aSXiao Guangrong 
251bebb106aSXiao Guangrong 	vcpu->arch.mmio_gva = 0;
252bebb106aSXiao Guangrong }
253bebb106aSXiao Guangrong 
254bebb106aSXiao Guangrong static inline bool vcpu_match_mmio_gva(struct kvm_vcpu *vcpu, unsigned long gva)
255bebb106aSXiao Guangrong {
25656f17dd3SDavid Matlack 	if (vcpu_match_mmio_gen(vcpu) && vcpu->arch.mmio_gva &&
25756f17dd3SDavid Matlack 	      vcpu->arch.mmio_gva == (gva & PAGE_MASK))
258bebb106aSXiao Guangrong 		return true;
259bebb106aSXiao Guangrong 
260bebb106aSXiao Guangrong 	return false;
261bebb106aSXiao Guangrong }
262bebb106aSXiao Guangrong 
263bebb106aSXiao Guangrong static inline bool vcpu_match_mmio_gpa(struct kvm_vcpu *vcpu, gpa_t gpa)
264bebb106aSXiao Guangrong {
26556f17dd3SDavid Matlack 	if (vcpu_match_mmio_gen(vcpu) && vcpu->arch.mmio_gfn &&
26656f17dd3SDavid Matlack 	      vcpu->arch.mmio_gfn == gpa >> PAGE_SHIFT)
267bebb106aSXiao Guangrong 		return true;
268bebb106aSXiao Guangrong 
269bebb106aSXiao Guangrong 	return false;
270bebb106aSXiao Guangrong }
271bebb106aSXiao Guangrong 
27227b4a9c4SSean Christopherson static inline unsigned long kvm_register_read(struct kvm_vcpu *vcpu, int reg)
2735777392eSNadav Amit {
27427b4a9c4SSean Christopherson 	unsigned long val = kvm_register_read_raw(vcpu, reg);
2755777392eSNadav Amit 
2765777392eSNadav Amit 	return is_64_bit_mode(vcpu) ? val : (u32)val;
2775777392eSNadav Amit }
2785777392eSNadav Amit 
27927b4a9c4SSean Christopherson static inline void kvm_register_write(struct kvm_vcpu *vcpu,
280489cbcf0SSean Christopherson 				       int reg, unsigned long val)
28127e6fb5dSNadav Amit {
28227e6fb5dSNadav Amit 	if (!is_64_bit_mode(vcpu))
28327e6fb5dSNadav Amit 		val = (u32)val;
28427b4a9c4SSean Christopherson 	return kvm_register_write_raw(vcpu, reg, val);
28527e6fb5dSNadav Amit }
28627e6fb5dSNadav Amit 
28741dbc6bcSPaolo Bonzini static inline bool kvm_check_has_quirk(struct kvm *kvm, u64 quirk)
28841dbc6bcSPaolo Bonzini {
28941dbc6bcSPaolo Bonzini 	return !(kvm->arch.disabled_quirks & quirk);
29041dbc6bcSPaolo Bonzini }
29141dbc6bcSPaolo Bonzini 
29227cbe7d6SLiran Alon static inline bool kvm_vcpu_latch_init(struct kvm_vcpu *vcpu)
29327cbe7d6SLiran Alon {
294b3646477SJason Baron 	return is_smm(vcpu) || static_call(kvm_x86_apic_init_signal_blocked)(vcpu);
29527cbe7d6SLiran Alon }
29627cbe7d6SLiran Alon 
297629b5348SJoao Martins void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock, int sec_hi_ofs);
2989497e1f2SSean Christopherson void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip);
299ff9d07a0SZhang, Yanmin 
300108b249cSPaolo Bonzini u64 get_kvmclock_ns(struct kvm *kvm);
30199e3e30aSZachary Amsden 
302ce14e868SPaolo Bonzini int kvm_read_guest_virt(struct kvm_vcpu *vcpu,
303064aea77SNadav Har'El 	gva_t addr, void *val, unsigned int bytes,
304064aea77SNadav Har'El 	struct x86_exception *exception);
305064aea77SNadav Har'El 
306ce14e868SPaolo Bonzini int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu,
3076a4d7550SNadav Har'El 	gva_t addr, void *val, unsigned int bytes,
3086a4d7550SNadav Har'El 	struct x86_exception *exception);
3096a4d7550SNadav Har'El 
310082d06edSWanpeng Li int handle_ud(struct kvm_vcpu *vcpu);
311082d06edSWanpeng Li 
312da998b46SJim Mattson void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu);
313da998b46SJim Mattson 
31419efffa2SXiao Guangrong void kvm_vcpu_mtrr_init(struct kvm_vcpu *vcpu);
315ff53604bSXiao Guangrong u8 kvm_mtrr_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn);
3164566654bSNadav Amit bool kvm_mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data);
317ff53604bSXiao Guangrong int kvm_mtrr_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data);
318ff53604bSXiao Guangrong int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata);
3196a39bbc5SXiao Guangrong bool kvm_mtrr_check_gfn_range_consistency(struct kvm_vcpu *vcpu, gfn_t gfn,
3206a39bbc5SXiao Guangrong 					  int page_num);
32152004014SFeng Wu bool kvm_vector_hashing_enabled(void);
32289786147SMohammed Gamal void kvm_fixup_and_inject_pf_error(struct kvm_vcpu *vcpu, gva_t gva, u16 error_code);
3234aa2691dSWei Huang int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type,
3244aa2691dSWei Huang 				    void *insn, int insn_len);
325736c291cSSean Christopherson int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
326c60658d1SSean Christopherson 			    int emulation_type, void *insn, int insn_len);
327404d5d7bSWanpeng Li fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu);
3284566654bSNadav Amit 
32900b27a3eSAvi Kivity extern u64 host_xcr0;
330cfc48181SSean Christopherson extern u64 supported_xcr0;
33186137773STom Lendacky extern u64 host_xss;
332408e9a31SPaolo Bonzini extern u64 supported_xss;
3334ff41732SPaolo Bonzini 
334615a4ae1SSean Christopherson static inline bool kvm_mpx_supported(void)
335615a4ae1SSean Christopherson {
336615a4ae1SSean Christopherson 	return (supported_xcr0 & (XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR))
337615a4ae1SSean Christopherson 		== (XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR);
338615a4ae1SSean Christopherson }
339615a4ae1SSean Christopherson 
3409ed96e87SMarcelo Tosatti extern unsigned int min_timer_period_us;
3419ed96e87SMarcelo Tosatti 
342c4ae60e4SLiran Alon extern bool enable_vmware_backdoor;
343c4ae60e4SLiran Alon 
3440c5f81daSWanpeng Li extern int pi_inject_timer;
3450c5f81daSWanpeng Li 
346d855066fSLike Xu extern bool report_ignored_msrs;
347d855066fSLike Xu 
3488d93c874SMarcelo Tosatti static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec)
3498d93c874SMarcelo Tosatti {
3508d93c874SMarcelo Tosatti 	return pvclock_scale_delta(nsec, vcpu->arch.virtual_tsc_mult,
3518d93c874SMarcelo Tosatti 				   vcpu->arch.virtual_tsc_shift);
3528d93c874SMarcelo Tosatti }
3538d93c874SMarcelo Tosatti 
354b51012deSPaolo Bonzini /* Same "calling convention" as do_div:
355b51012deSPaolo Bonzini  * - divide (n << 32) by base
356b51012deSPaolo Bonzini  * - put result in n
357b51012deSPaolo Bonzini  * - return remainder
358b51012deSPaolo Bonzini  */
359b51012deSPaolo Bonzini #define do_shl32_div32(n, base)					\
360b51012deSPaolo Bonzini 	({							\
361b51012deSPaolo Bonzini 	    u32 __quot, __rem;					\
362b51012deSPaolo Bonzini 	    asm("divl %2" : "=a" (__quot), "=d" (__rem)		\
363b51012deSPaolo Bonzini 			: "rm" (base), "0" (0), "1" ((u32) n));	\
364b51012deSPaolo Bonzini 	    n = __quot;						\
365b51012deSPaolo Bonzini 	    __rem;						\
366b51012deSPaolo Bonzini 	 })
367b51012deSPaolo Bonzini 
3684d5422ceSWanpeng Li static inline bool kvm_mwait_in_guest(struct kvm *kvm)
369668fffa3SMichael S. Tsirkin {
3704d5422ceSWanpeng Li 	return kvm->arch.mwait_in_guest;
371668fffa3SMichael S. Tsirkin }
372668fffa3SMichael S. Tsirkin 
373caa057a2SWanpeng Li static inline bool kvm_hlt_in_guest(struct kvm *kvm)
374caa057a2SWanpeng Li {
375caa057a2SWanpeng Li 	return kvm->arch.hlt_in_guest;
376caa057a2SWanpeng Li }
377caa057a2SWanpeng Li 
378b31c114bSWanpeng Li static inline bool kvm_pause_in_guest(struct kvm *kvm)
379b31c114bSWanpeng Li {
380b31c114bSWanpeng Li 	return kvm->arch.pause_in_guest;
381b31c114bSWanpeng Li }
382b31c114bSWanpeng Li 
383b5170063SWanpeng Li static inline bool kvm_cstate_in_guest(struct kvm *kvm)
384b5170063SWanpeng Li {
385b5170063SWanpeng Li 	return kvm->arch.cstate_in_guest;
386b5170063SWanpeng Li }
387b5170063SWanpeng Li 
388*db215756SSean Christopherson enum kvm_intr_type {
389*db215756SSean Christopherson 	/* Values are arbitrary, but must be non-zero. */
390*db215756SSean Christopherson 	KVM_HANDLING_IRQ = 1,
391*db215756SSean Christopherson 	KVM_HANDLING_NMI,
392*db215756SSean Christopherson };
393*db215756SSean Christopherson 
394*db215756SSean Christopherson static inline void kvm_before_interrupt(struct kvm_vcpu *vcpu,
395*db215756SSean Christopherson 					enum kvm_intr_type intr)
396dd60d217SAndi Kleen {
397*db215756SSean Christopherson 	WRITE_ONCE(vcpu->arch.handling_intr_from_guest, (u8)intr);
398dd60d217SAndi Kleen }
399dd60d217SAndi Kleen 
400dd60d217SAndi Kleen static inline void kvm_after_interrupt(struct kvm_vcpu *vcpu)
401dd60d217SAndi Kleen {
40273cd107bSSean Christopherson 	WRITE_ONCE(vcpu->arch.handling_intr_from_guest, 0);
403dd60d217SAndi Kleen }
404dd60d217SAndi Kleen 
40573cd107bSSean Christopherson static inline bool kvm_handling_nmi_from_guest(struct kvm_vcpu *vcpu)
40673cd107bSSean Christopherson {
407*db215756SSean Christopherson 	return vcpu->arch.handling_intr_from_guest == KVM_HANDLING_NMI;
40873cd107bSSean Christopherson }
409674ea351SPaolo Bonzini 
410674ea351SPaolo Bonzini static inline bool kvm_pat_valid(u64 data)
411674ea351SPaolo Bonzini {
412674ea351SPaolo Bonzini 	if (data & 0xF8F8F8F8F8F8F8F8ull)
413674ea351SPaolo Bonzini 		return false;
414674ea351SPaolo Bonzini 	/* 0, 1, 4, 5, 6, 7 are valid values.  */
415674ea351SPaolo Bonzini 	return (data | ((data & 0x0202020202020202ull) << 1)) == data;
416674ea351SPaolo Bonzini }
417674ea351SPaolo Bonzini 
4189b5e8532SSean Christopherson static inline bool kvm_dr7_valid(u64 data)
419b91991bfSKrish Sadhukhan {
420b91991bfSKrish Sadhukhan 	/* Bits [63:32] are reserved */
421b91991bfSKrish Sadhukhan 	return !(data >> 32);
422b91991bfSKrish Sadhukhan }
423f5f6145eSKrish Sadhukhan static inline bool kvm_dr6_valid(u64 data)
424f5f6145eSKrish Sadhukhan {
425f5f6145eSKrish Sadhukhan 	/* Bits [63:32] are reserved */
426f5f6145eSKrish Sadhukhan 	return !(data >> 32);
427f5f6145eSKrish Sadhukhan }
428b91991bfSKrish Sadhukhan 
4293f1a18b9SUros Bizjak /*
4303f1a18b9SUros Bizjak  * Trigger machine check on the host. We assume all the MSRs are already set up
4313f1a18b9SUros Bizjak  * by the CPU and that we still run on the same CPU as the MCE occurred on.
4323f1a18b9SUros Bizjak  * We pass a fake environment to the machine check handler because we want
4333f1a18b9SUros Bizjak  * the guest to be always treated like user space, no matter what context
4343f1a18b9SUros Bizjak  * it used internally.
4353f1a18b9SUros Bizjak  */
4363f1a18b9SUros Bizjak static inline void kvm_machine_check(void)
4373f1a18b9SUros Bizjak {
4383f1a18b9SUros Bizjak #if defined(CONFIG_X86_MCE)
4393f1a18b9SUros Bizjak 	struct pt_regs regs = {
4403f1a18b9SUros Bizjak 		.cs = 3, /* Fake ring 3 no matter what the guest ran on */
4413f1a18b9SUros Bizjak 		.flags = X86_EFLAGS_IF,
4423f1a18b9SUros Bizjak 	};
4433f1a18b9SUros Bizjak 
4443f1a18b9SUros Bizjak 	do_machine_check(&regs);
4453f1a18b9SUros Bizjak #endif
4463f1a18b9SUros Bizjak }
4473f1a18b9SUros Bizjak 
448139a12cfSAaron Lewis void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu);
449139a12cfSAaron Lewis void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu);
450841c2be0SMaxim Levitsky int kvm_spec_ctrl_test_value(u64 value);
451ee69c92bSSean Christopherson bool kvm_is_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
4523f3393b3SBabu Moger int kvm_handle_memory_failure(struct kvm_vcpu *vcpu, int r,
4533f3393b3SBabu Moger 			      struct x86_exception *e);
4549715092fSBabu Moger int kvm_handle_invpcid(struct kvm_vcpu *vcpu, unsigned long type, gva_t gva);
45551de8151SAlexander Graf bool kvm_msr_allowed(struct kvm_vcpu *vcpu, u32 index, u32 type);
456674ea351SPaolo Bonzini 
457cc4cb017SMaxim Levitsky /*
458cc4cb017SMaxim Levitsky  * Internal error codes that are used to indicate that MSR emulation encountered
459cc4cb017SMaxim Levitsky  * an error that should result in #GP in the guest, unless userspace
460cc4cb017SMaxim Levitsky  * handles it.
461cc4cb017SMaxim Levitsky  */
462cc4cb017SMaxim Levitsky #define  KVM_MSR_RET_INVALID	2	/* in-kernel MSR emulation #GP condition */
463cc4cb017SMaxim Levitsky #define  KVM_MSR_RET_FILTERED	3	/* #GP due to userspace MSR filter */
4646abe9c13SPeter Xu 
465b899c132SKrish Sadhukhan #define __cr4_reserved_bits(__cpu_has, __c)             \
466b899c132SKrish Sadhukhan ({                                                      \
467b899c132SKrish Sadhukhan 	u64 __reserved_bits = CR4_RESERVED_BITS;        \
468b899c132SKrish Sadhukhan                                                         \
469b899c132SKrish Sadhukhan 	if (!__cpu_has(__c, X86_FEATURE_XSAVE))         \
470b899c132SKrish Sadhukhan 		__reserved_bits |= X86_CR4_OSXSAVE;     \
471b899c132SKrish Sadhukhan 	if (!__cpu_has(__c, X86_FEATURE_SMEP))          \
472b899c132SKrish Sadhukhan 		__reserved_bits |= X86_CR4_SMEP;        \
473b899c132SKrish Sadhukhan 	if (!__cpu_has(__c, X86_FEATURE_SMAP))          \
474b899c132SKrish Sadhukhan 		__reserved_bits |= X86_CR4_SMAP;        \
475b899c132SKrish Sadhukhan 	if (!__cpu_has(__c, X86_FEATURE_FSGSBASE))      \
476b899c132SKrish Sadhukhan 		__reserved_bits |= X86_CR4_FSGSBASE;    \
477b899c132SKrish Sadhukhan 	if (!__cpu_has(__c, X86_FEATURE_PKU))           \
478b899c132SKrish Sadhukhan 		__reserved_bits |= X86_CR4_PKE;         \
479b899c132SKrish Sadhukhan 	if (!__cpu_has(__c, X86_FEATURE_LA57))          \
480b899c132SKrish Sadhukhan 		__reserved_bits |= X86_CR4_LA57;        \
481b899c132SKrish Sadhukhan 	if (!__cpu_has(__c, X86_FEATURE_UMIP))          \
482b899c132SKrish Sadhukhan 		__reserved_bits |= X86_CR4_UMIP;        \
48353efe527SPaolo Bonzini 	if (!__cpu_has(__c, X86_FEATURE_VMX))           \
48453efe527SPaolo Bonzini 		__reserved_bits |= X86_CR4_VMXE;        \
4854683d758SVitaly Kuznetsov 	if (!__cpu_has(__c, X86_FEATURE_PCID))          \
4864683d758SVitaly Kuznetsov 		__reserved_bits |= X86_CR4_PCIDE;       \
487b899c132SKrish Sadhukhan 	__reserved_bits;                                \
488b899c132SKrish Sadhukhan })
489b899c132SKrish Sadhukhan 
4908f423a80STom Lendacky int kvm_sev_es_mmio_write(struct kvm_vcpu *vcpu, gpa_t src, unsigned int bytes,
4918f423a80STom Lendacky 			  void *dst);
4928f423a80STom Lendacky int kvm_sev_es_mmio_read(struct kvm_vcpu *vcpu, gpa_t src, unsigned int bytes,
4938f423a80STom Lendacky 			 void *dst);
4947ed9abfeSTom Lendacky int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size,
4957ed9abfeSTom Lendacky 			 unsigned int port, void *data,  unsigned int count,
4967ed9abfeSTom Lendacky 			 int in);
4978f423a80STom Lendacky 
49826eef70cSAvi Kivity #endif
499