xref: /linux/arch/x86/kvm/x86.h (revision 7709aba8f71613ae5d18d8c00adb54948e6bedb3)
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 
11938c8745SSean Christopherson struct kvm_caps {
12938c8745SSean Christopherson 	/* control of guest tsc rate supported? */
13938c8745SSean Christopherson 	bool has_tsc_control;
14938c8745SSean Christopherson 	/* maximum supported tsc_khz for guests */
15938c8745SSean Christopherson 	u32  max_guest_tsc_khz;
16938c8745SSean Christopherson 	/* number of bits of the fractional part of the TSC scaling ratio */
17938c8745SSean Christopherson 	u8   tsc_scaling_ratio_frac_bits;
18938c8745SSean Christopherson 	/* maximum allowed value of TSC scaling ratio */
19938c8745SSean Christopherson 	u64  max_tsc_scaling_ratio;
20938c8745SSean Christopherson 	/* 1ull << kvm_caps.tsc_scaling_ratio_frac_bits */
21938c8745SSean Christopherson 	u64  default_tsc_scaling_ratio;
22938c8745SSean Christopherson 	/* bus lock detection supported? */
23938c8745SSean Christopherson 	bool has_bus_lock_exit;
242f4073e0STao Xu 	/* notify VM exit supported? */
252f4073e0STao Xu 	bool has_notify_vmexit;
26938c8745SSean Christopherson 
27938c8745SSean Christopherson 	u64 supported_mce_cap;
28938c8745SSean Christopherson 	u64 supported_xcr0;
29938c8745SSean Christopherson 	u64 supported_xss;
30938c8745SSean Christopherson };
31938c8745SSean Christopherson 
3265297341SUros Bizjak void kvm_spurious_fault(void);
3365297341SUros Bizjak 
34648fc8aeSSean Christopherson #define KVM_NESTED_VMENTER_CONSISTENCY_CHECK(consistency_check)		\
35648fc8aeSSean Christopherson ({									\
36648fc8aeSSean Christopherson 	bool failed = (consistency_check);				\
37648fc8aeSSean Christopherson 	if (failed)							\
38648fc8aeSSean Christopherson 		trace_kvm_nested_vmenter_failed(#consistency_check, 0);	\
39648fc8aeSSean Christopherson 	failed;								\
40648fc8aeSSean Christopherson })
41648fc8aeSSean Christopherson 
42c8e88717SBabu Moger #define KVM_DEFAULT_PLE_GAP		128
43c8e88717SBabu Moger #define KVM_VMX_DEFAULT_PLE_WINDOW	4096
44c8e88717SBabu Moger #define KVM_DEFAULT_PLE_WINDOW_GROW	2
45c8e88717SBabu Moger #define KVM_DEFAULT_PLE_WINDOW_SHRINK	0
46c8e88717SBabu Moger #define KVM_VMX_DEFAULT_PLE_WINDOW_MAX	UINT_MAX
478566ac8bSBabu Moger #define KVM_SVM_DEFAULT_PLE_WINDOW_MAX	USHRT_MAX
488566ac8bSBabu Moger #define KVM_SVM_DEFAULT_PLE_WINDOW	3000
49c8e88717SBabu Moger 
50c8e88717SBabu Moger static inline unsigned int __grow_ple_window(unsigned int val,
51c8e88717SBabu Moger 		unsigned int base, unsigned int modifier, unsigned int max)
52c8e88717SBabu Moger {
53c8e88717SBabu Moger 	u64 ret = val;
54c8e88717SBabu Moger 
55c8e88717SBabu Moger 	if (modifier < 1)
56c8e88717SBabu Moger 		return base;
57c8e88717SBabu Moger 
58c8e88717SBabu Moger 	if (modifier < base)
59c8e88717SBabu Moger 		ret *= modifier;
60c8e88717SBabu Moger 	else
61c8e88717SBabu Moger 		ret += modifier;
62c8e88717SBabu Moger 
63c8e88717SBabu Moger 	return min(ret, (u64)max);
64c8e88717SBabu Moger }
65c8e88717SBabu Moger 
66c8e88717SBabu Moger static inline unsigned int __shrink_ple_window(unsigned int val,
67c8e88717SBabu Moger 		unsigned int base, unsigned int modifier, unsigned int min)
68c8e88717SBabu Moger {
69c8e88717SBabu Moger 	if (modifier < 1)
70c8e88717SBabu Moger 		return base;
71c8e88717SBabu Moger 
72c8e88717SBabu Moger 	if (modifier < base)
73c8e88717SBabu Moger 		val /= modifier;
74c8e88717SBabu Moger 	else
75c8e88717SBabu Moger 		val -= modifier;
76c8e88717SBabu Moger 
77c8e88717SBabu Moger 	return max(val, min);
78c8e88717SBabu Moger }
79c8e88717SBabu Moger 
8074545705SRadim Krčmář #define MSR_IA32_CR_PAT_DEFAULT  0x0007040600070406ULL
8174545705SRadim Krčmář 
8240e5f908SSean Christopherson void kvm_service_local_tlb_flush_requests(struct kvm_vcpu *vcpu);
83cb6a32c2SSean Christopherson int kvm_check_nested_events(struct kvm_vcpu *vcpu);
84cb6a32c2SSean Christopherson 
85*7709aba8SSean Christopherson static inline bool kvm_is_exception_pending(struct kvm_vcpu *vcpu)
86*7709aba8SSean Christopherson {
87*7709aba8SSean Christopherson 	return vcpu->arch.exception.pending ||
88*7709aba8SSean Christopherson 	       vcpu->arch.exception_vmexit.pending;
89*7709aba8SSean Christopherson }
90*7709aba8SSean Christopherson 
9126eef70cSAvi Kivity static inline void kvm_clear_exception_queue(struct kvm_vcpu *vcpu)
9226eef70cSAvi Kivity {
935c7d4f9aSLiran Alon 	vcpu->arch.exception.pending = false;
94664f8e26SWanpeng Li 	vcpu->arch.exception.injected = false;
95*7709aba8SSean Christopherson 	vcpu->arch.exception_vmexit.pending = false;
9626eef70cSAvi Kivity }
9726eef70cSAvi Kivity 
9866fd3f7fSGleb Natapov static inline void kvm_queue_interrupt(struct kvm_vcpu *vcpu, u8 vector,
9966fd3f7fSGleb Natapov 	bool soft)
100937a7eaeSAvi Kivity {
10104140b41SLiran Alon 	vcpu->arch.interrupt.injected = true;
10266fd3f7fSGleb Natapov 	vcpu->arch.interrupt.soft = soft;
103937a7eaeSAvi Kivity 	vcpu->arch.interrupt.nr = vector;
104937a7eaeSAvi Kivity }
105937a7eaeSAvi Kivity 
106937a7eaeSAvi Kivity static inline void kvm_clear_interrupt_queue(struct kvm_vcpu *vcpu)
107937a7eaeSAvi Kivity {
10804140b41SLiran Alon 	vcpu->arch.interrupt.injected = false;
109937a7eaeSAvi Kivity }
110937a7eaeSAvi Kivity 
1113298b75cSGleb Natapov static inline bool kvm_event_needs_reinjection(struct kvm_vcpu *vcpu)
1123298b75cSGleb Natapov {
11304140b41SLiran Alon 	return vcpu->arch.exception.injected || vcpu->arch.interrupt.injected ||
1143298b75cSGleb Natapov 		vcpu->arch.nmi_injected;
1153298b75cSGleb Natapov }
11666fd3f7fSGleb Natapov 
11766fd3f7fSGleb Natapov static inline bool kvm_exception_is_soft(unsigned int nr)
11866fd3f7fSGleb Natapov {
11966fd3f7fSGleb Natapov 	return (nr == BP_VECTOR) || (nr == OF_VECTOR);
12066fd3f7fSGleb Natapov }
121fc61b800SGleb Natapov 
1223eeb3288SAvi Kivity static inline bool is_protmode(struct kvm_vcpu *vcpu)
1233eeb3288SAvi Kivity {
1243eeb3288SAvi Kivity 	return kvm_read_cr0_bits(vcpu, X86_CR0_PE);
1253eeb3288SAvi Kivity }
1263eeb3288SAvi Kivity 
127836a1b3cSAvi Kivity static inline int is_long_mode(struct kvm_vcpu *vcpu)
128836a1b3cSAvi Kivity {
129836a1b3cSAvi Kivity #ifdef CONFIG_X86_64
130f6801dffSAvi Kivity 	return vcpu->arch.efer & EFER_LMA;
131836a1b3cSAvi Kivity #else
132836a1b3cSAvi Kivity 	return 0;
133836a1b3cSAvi Kivity #endif
134836a1b3cSAvi Kivity }
135836a1b3cSAvi Kivity 
1365777392eSNadav Amit static inline bool is_64_bit_mode(struct kvm_vcpu *vcpu)
1375777392eSNadav Amit {
1385777392eSNadav Amit 	int cs_db, cs_l;
1395777392eSNadav Amit 
140b5aead00STom Lendacky 	WARN_ON_ONCE(vcpu->arch.guest_state_protected);
141b5aead00STom Lendacky 
1425777392eSNadav Amit 	if (!is_long_mode(vcpu))
1435777392eSNadav Amit 		return false;
144b3646477SJason Baron 	static_call(kvm_x86_get_cs_db_l_bits)(vcpu, &cs_db, &cs_l);
1455777392eSNadav Amit 	return cs_l;
1465777392eSNadav Amit }
1475777392eSNadav Amit 
148b5aead00STom Lendacky static inline bool is_64_bit_hypercall(struct kvm_vcpu *vcpu)
149b5aead00STom Lendacky {
150b5aead00STom Lendacky 	/*
151b5aead00STom Lendacky 	 * If running with protected guest state, the CS register is not
152b5aead00STom Lendacky 	 * accessible. The hypercall register values will have had to been
153b5aead00STom Lendacky 	 * provided in 64-bit mode, so assume the guest is in 64-bit.
154b5aead00STom Lendacky 	 */
155b5aead00STom Lendacky 	return vcpu->arch.guest_state_protected || is_64_bit_mode(vcpu);
156b5aead00STom Lendacky }
157b5aead00STom Lendacky 
1580447378aSMarc Orr static inline bool x86_exception_has_error_code(unsigned int vector)
1590447378aSMarc Orr {
1600447378aSMarc Orr 	static u32 exception_has_error_code = BIT(DF_VECTOR) | BIT(TS_VECTOR) |
1610447378aSMarc Orr 			BIT(NP_VECTOR) | BIT(SS_VECTOR) | BIT(GP_VECTOR) |
1620447378aSMarc Orr 			BIT(PF_VECTOR) | BIT(AC_VECTOR);
1630447378aSMarc Orr 
1640447378aSMarc Orr 	return (1U << vector) & exception_has_error_code;
1650447378aSMarc Orr }
1660447378aSMarc Orr 
1676539e738SJoerg Roedel static inline bool mmu_is_nested(struct kvm_vcpu *vcpu)
1686539e738SJoerg Roedel {
1696539e738SJoerg Roedel 	return vcpu->arch.walk_mmu == &vcpu->arch.nested_mmu;
1706539e738SJoerg Roedel }
1716539e738SJoerg Roedel 
172836a1b3cSAvi Kivity static inline int is_pae(struct kvm_vcpu *vcpu)
173836a1b3cSAvi Kivity {
174836a1b3cSAvi Kivity 	return kvm_read_cr4_bits(vcpu, X86_CR4_PAE);
175836a1b3cSAvi Kivity }
176836a1b3cSAvi Kivity 
177836a1b3cSAvi Kivity static inline int is_pse(struct kvm_vcpu *vcpu)
178836a1b3cSAvi Kivity {
179836a1b3cSAvi Kivity 	return kvm_read_cr4_bits(vcpu, X86_CR4_PSE);
180836a1b3cSAvi Kivity }
181836a1b3cSAvi Kivity 
182836a1b3cSAvi Kivity static inline int is_paging(struct kvm_vcpu *vcpu)
183836a1b3cSAvi Kivity {
184c36fc04eSDavidlohr Bueso 	return likely(kvm_read_cr0_bits(vcpu, X86_CR0_PG));
185836a1b3cSAvi Kivity }
186836a1b3cSAvi Kivity 
187bf03d4f9SPaolo Bonzini static inline bool is_pae_paging(struct kvm_vcpu *vcpu)
188bf03d4f9SPaolo Bonzini {
189bf03d4f9SPaolo Bonzini 	return !is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu);
190bf03d4f9SPaolo Bonzini }
191bf03d4f9SPaolo Bonzini 
192fd8cb433SYu Zhang static inline u8 vcpu_virt_addr_bits(struct kvm_vcpu *vcpu)
193fd8cb433SYu Zhang {
194fd8cb433SYu Zhang 	return kvm_read_cr4_bits(vcpu, X86_CR4_LA57) ? 57 : 48;
195fd8cb433SYu Zhang }
196fd8cb433SYu Zhang 
197fd8cb433SYu Zhang static inline bool is_noncanonical_address(u64 la, struct kvm_vcpu *vcpu)
198fd8cb433SYu Zhang {
1991fb85d06SAdrian Hunter 	return !__is_canonical_address(la, vcpu_virt_addr_bits(vcpu));
200fd8cb433SYu Zhang }
201fd8cb433SYu Zhang 
202bebb106aSXiao Guangrong static inline void vcpu_cache_mmio_info(struct kvm_vcpu *vcpu,
203bebb106aSXiao Guangrong 					gva_t gva, gfn_t gfn, unsigned access)
204bebb106aSXiao Guangrong {
205ddfd1730SSean Christopherson 	u64 gen = kvm_memslots(vcpu->kvm)->generation;
206ddfd1730SSean Christopherson 
207361209e0SSean Christopherson 	if (unlikely(gen & KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS))
208ddfd1730SSean Christopherson 		return;
209ddfd1730SSean Christopherson 
2109034e6e8SPaolo Bonzini 	/*
2119034e6e8SPaolo Bonzini 	 * If this is a shadow nested page table, the "GVA" is
2129034e6e8SPaolo Bonzini 	 * actually a nGPA.
2139034e6e8SPaolo Bonzini 	 */
2149034e6e8SPaolo Bonzini 	vcpu->arch.mmio_gva = mmu_is_nested(vcpu) ? 0 : gva & PAGE_MASK;
215871bd034SSean Christopherson 	vcpu->arch.mmio_access = access;
216bebb106aSXiao Guangrong 	vcpu->arch.mmio_gfn = gfn;
217ddfd1730SSean Christopherson 	vcpu->arch.mmio_gen = gen;
21856f17dd3SDavid Matlack }
21956f17dd3SDavid Matlack 
22056f17dd3SDavid Matlack static inline bool vcpu_match_mmio_gen(struct kvm_vcpu *vcpu)
22156f17dd3SDavid Matlack {
22256f17dd3SDavid Matlack 	return vcpu->arch.mmio_gen == kvm_memslots(vcpu->kvm)->generation;
223bebb106aSXiao Guangrong }
224bebb106aSXiao Guangrong 
225bebb106aSXiao Guangrong /*
22656f17dd3SDavid Matlack  * Clear the mmio cache info for the given gva. If gva is MMIO_GVA_ANY, we
22756f17dd3SDavid Matlack  * clear all mmio cache info.
228bebb106aSXiao Guangrong  */
22956f17dd3SDavid Matlack #define MMIO_GVA_ANY (~(gva_t)0)
23056f17dd3SDavid Matlack 
231bebb106aSXiao Guangrong static inline void vcpu_clear_mmio_info(struct kvm_vcpu *vcpu, gva_t gva)
232bebb106aSXiao Guangrong {
23356f17dd3SDavid Matlack 	if (gva != MMIO_GVA_ANY && vcpu->arch.mmio_gva != (gva & PAGE_MASK))
234bebb106aSXiao Guangrong 		return;
235bebb106aSXiao Guangrong 
236bebb106aSXiao Guangrong 	vcpu->arch.mmio_gva = 0;
237bebb106aSXiao Guangrong }
238bebb106aSXiao Guangrong 
239bebb106aSXiao Guangrong static inline bool vcpu_match_mmio_gva(struct kvm_vcpu *vcpu, unsigned long gva)
240bebb106aSXiao Guangrong {
24156f17dd3SDavid Matlack 	if (vcpu_match_mmio_gen(vcpu) && vcpu->arch.mmio_gva &&
24256f17dd3SDavid Matlack 	      vcpu->arch.mmio_gva == (gva & PAGE_MASK))
243bebb106aSXiao Guangrong 		return true;
244bebb106aSXiao Guangrong 
245bebb106aSXiao Guangrong 	return false;
246bebb106aSXiao Guangrong }
247bebb106aSXiao Guangrong 
248bebb106aSXiao Guangrong static inline bool vcpu_match_mmio_gpa(struct kvm_vcpu *vcpu, gpa_t gpa)
249bebb106aSXiao Guangrong {
25056f17dd3SDavid Matlack 	if (vcpu_match_mmio_gen(vcpu) && vcpu->arch.mmio_gfn &&
25156f17dd3SDavid Matlack 	      vcpu->arch.mmio_gfn == gpa >> PAGE_SHIFT)
252bebb106aSXiao Guangrong 		return true;
253bebb106aSXiao Guangrong 
254bebb106aSXiao Guangrong 	return false;
255bebb106aSXiao Guangrong }
256bebb106aSXiao Guangrong 
25727b4a9c4SSean Christopherson static inline unsigned long kvm_register_read(struct kvm_vcpu *vcpu, int reg)
2585777392eSNadav Amit {
25927b4a9c4SSean Christopherson 	unsigned long val = kvm_register_read_raw(vcpu, reg);
2605777392eSNadav Amit 
2615777392eSNadav Amit 	return is_64_bit_mode(vcpu) ? val : (u32)val;
2625777392eSNadav Amit }
2635777392eSNadav Amit 
26427b4a9c4SSean Christopherson static inline void kvm_register_write(struct kvm_vcpu *vcpu,
265489cbcf0SSean Christopherson 				       int reg, unsigned long val)
26627e6fb5dSNadav Amit {
26727e6fb5dSNadav Amit 	if (!is_64_bit_mode(vcpu))
26827e6fb5dSNadav Amit 		val = (u32)val;
26927b4a9c4SSean Christopherson 	return kvm_register_write_raw(vcpu, reg, val);
27027e6fb5dSNadav Amit }
27127e6fb5dSNadav Amit 
27241dbc6bcSPaolo Bonzini static inline bool kvm_check_has_quirk(struct kvm *kvm, u64 quirk)
27341dbc6bcSPaolo Bonzini {
27441dbc6bcSPaolo Bonzini 	return !(kvm->arch.disabled_quirks & quirk);
27541dbc6bcSPaolo Bonzini }
27641dbc6bcSPaolo Bonzini 
27727cbe7d6SLiran Alon static inline bool kvm_vcpu_latch_init(struct kvm_vcpu *vcpu)
27827cbe7d6SLiran Alon {
279b3646477SJason Baron 	return is_smm(vcpu) || static_call(kvm_x86_apic_init_signal_blocked)(vcpu);
28027cbe7d6SLiran Alon }
28127cbe7d6SLiran Alon 
2829497e1f2SSean Christopherson void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip);
283ff9d07a0SZhang, Yanmin 
284108b249cSPaolo Bonzini u64 get_kvmclock_ns(struct kvm *kvm);
28599e3e30aSZachary Amsden 
286ce14e868SPaolo Bonzini int kvm_read_guest_virt(struct kvm_vcpu *vcpu,
287064aea77SNadav Har'El 	gva_t addr, void *val, unsigned int bytes,
288064aea77SNadav Har'El 	struct x86_exception *exception);
289064aea77SNadav Har'El 
290ce14e868SPaolo Bonzini int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu,
2916a4d7550SNadav Har'El 	gva_t addr, void *val, unsigned int bytes,
2926a4d7550SNadav Har'El 	struct x86_exception *exception);
2936a4d7550SNadav Har'El 
294082d06edSWanpeng Li int handle_ud(struct kvm_vcpu *vcpu);
295082d06edSWanpeng Li 
296d4963e31SSean Christopherson void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu,
297d4963e31SSean Christopherson 				   struct kvm_queued_exception *ex);
298da998b46SJim Mattson 
29919efffa2SXiao Guangrong void kvm_vcpu_mtrr_init(struct kvm_vcpu *vcpu);
300ff53604bSXiao Guangrong u8 kvm_mtrr_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn);
3014566654bSNadav Amit bool kvm_mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data);
302ff53604bSXiao Guangrong int kvm_mtrr_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data);
303ff53604bSXiao Guangrong int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata);
3046a39bbc5SXiao Guangrong bool kvm_mtrr_check_gfn_range_consistency(struct kvm_vcpu *vcpu, gfn_t gfn,
3056a39bbc5SXiao Guangrong 					  int page_num);
30652004014SFeng Wu bool kvm_vector_hashing_enabled(void);
30789786147SMohammed Gamal void kvm_fixup_and_inject_pf_error(struct kvm_vcpu *vcpu, gva_t gva, u16 error_code);
3084aa2691dSWei Huang int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type,
3094aa2691dSWei Huang 				    void *insn, int insn_len);
310736c291cSSean Christopherson int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
311c60658d1SSean Christopherson 			    int emulation_type, void *insn, int insn_len);
312404d5d7bSWanpeng Li fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu);
3134566654bSNadav Amit 
31400b27a3eSAvi Kivity extern u64 host_xcr0;
31586137773STom Lendacky extern u64 host_xss;
316938c8745SSean Christopherson 
317938c8745SSean Christopherson extern struct kvm_caps kvm_caps;
318938c8745SSean Christopherson 
3194732f244SLike Xu extern bool enable_pmu;
3204ff41732SPaolo Bonzini 
321615a4ae1SSean Christopherson static inline bool kvm_mpx_supported(void)
322615a4ae1SSean Christopherson {
323938c8745SSean Christopherson 	return (kvm_caps.supported_xcr0 & (XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR))
324615a4ae1SSean Christopherson 		== (XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR);
325615a4ae1SSean Christopherson }
326615a4ae1SSean Christopherson 
3279ed96e87SMarcelo Tosatti extern unsigned int min_timer_period_us;
3289ed96e87SMarcelo Tosatti 
329c4ae60e4SLiran Alon extern bool enable_vmware_backdoor;
330c4ae60e4SLiran Alon 
3310c5f81daSWanpeng Li extern int pi_inject_timer;
3320c5f81daSWanpeng Li 
333d855066fSLike Xu extern bool report_ignored_msrs;
334d855066fSLike Xu 
335cb00a70bSDavid Matlack extern bool eager_page_split;
336cb00a70bSDavid Matlack 
3378d93c874SMarcelo Tosatti static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec)
3388d93c874SMarcelo Tosatti {
3398d93c874SMarcelo Tosatti 	return pvclock_scale_delta(nsec, vcpu->arch.virtual_tsc_mult,
3408d93c874SMarcelo Tosatti 				   vcpu->arch.virtual_tsc_shift);
3418d93c874SMarcelo Tosatti }
3428d93c874SMarcelo Tosatti 
343b51012deSPaolo Bonzini /* Same "calling convention" as do_div:
344b51012deSPaolo Bonzini  * - divide (n << 32) by base
345b51012deSPaolo Bonzini  * - put result in n
346b51012deSPaolo Bonzini  * - return remainder
347b51012deSPaolo Bonzini  */
348b51012deSPaolo Bonzini #define do_shl32_div32(n, base)					\
349b51012deSPaolo Bonzini 	({							\
350b51012deSPaolo Bonzini 	    u32 __quot, __rem;					\
351b51012deSPaolo Bonzini 	    asm("divl %2" : "=a" (__quot), "=d" (__rem)		\
352b51012deSPaolo Bonzini 			: "rm" (base), "0" (0), "1" ((u32) n));	\
353b51012deSPaolo Bonzini 	    n = __quot;						\
354b51012deSPaolo Bonzini 	    __rem;						\
355b51012deSPaolo Bonzini 	 })
356b51012deSPaolo Bonzini 
3574d5422ceSWanpeng Li static inline bool kvm_mwait_in_guest(struct kvm *kvm)
358668fffa3SMichael S. Tsirkin {
3594d5422ceSWanpeng Li 	return kvm->arch.mwait_in_guest;
360668fffa3SMichael S. Tsirkin }
361668fffa3SMichael S. Tsirkin 
362caa057a2SWanpeng Li static inline bool kvm_hlt_in_guest(struct kvm *kvm)
363caa057a2SWanpeng Li {
364caa057a2SWanpeng Li 	return kvm->arch.hlt_in_guest;
365caa057a2SWanpeng Li }
366caa057a2SWanpeng Li 
367b31c114bSWanpeng Li static inline bool kvm_pause_in_guest(struct kvm *kvm)
368b31c114bSWanpeng Li {
369b31c114bSWanpeng Li 	return kvm->arch.pause_in_guest;
370b31c114bSWanpeng Li }
371b31c114bSWanpeng Li 
372b5170063SWanpeng Li static inline bool kvm_cstate_in_guest(struct kvm *kvm)
373b5170063SWanpeng Li {
374b5170063SWanpeng Li 	return kvm->arch.cstate_in_guest;
375b5170063SWanpeng Li }
376b5170063SWanpeng Li 
3772f4073e0STao Xu static inline bool kvm_notify_vmexit_enabled(struct kvm *kvm)
3782f4073e0STao Xu {
3792f4073e0STao Xu 	return kvm->arch.notify_vmexit_flags & KVM_X86_NOTIFY_VMEXIT_ENABLED;
3802f4073e0STao Xu }
3812f4073e0STao Xu 
382db215756SSean Christopherson enum kvm_intr_type {
383db215756SSean Christopherson 	/* Values are arbitrary, but must be non-zero. */
384db215756SSean Christopherson 	KVM_HANDLING_IRQ = 1,
385db215756SSean Christopherson 	KVM_HANDLING_NMI,
386db215756SSean Christopherson };
387dd60d217SAndi Kleen 
388db215756SSean Christopherson static inline void kvm_before_interrupt(struct kvm_vcpu *vcpu,
389db215756SSean Christopherson 					enum kvm_intr_type intr)
390dd60d217SAndi Kleen {
391db215756SSean Christopherson 	WRITE_ONCE(vcpu->arch.handling_intr_from_guest, (u8)intr);
392dd60d217SAndi Kleen }
393dd60d217SAndi Kleen 
394dd60d217SAndi Kleen static inline void kvm_after_interrupt(struct kvm_vcpu *vcpu)
395dd60d217SAndi Kleen {
39673cd107bSSean Christopherson 	WRITE_ONCE(vcpu->arch.handling_intr_from_guest, 0);
397dd60d217SAndi Kleen }
398dd60d217SAndi Kleen 
39973cd107bSSean Christopherson static inline bool kvm_handling_nmi_from_guest(struct kvm_vcpu *vcpu)
40073cd107bSSean Christopherson {
401db215756SSean Christopherson 	return vcpu->arch.handling_intr_from_guest == KVM_HANDLING_NMI;
40273cd107bSSean Christopherson }
403674ea351SPaolo Bonzini 
404674ea351SPaolo Bonzini static inline bool kvm_pat_valid(u64 data)
405674ea351SPaolo Bonzini {
406674ea351SPaolo Bonzini 	if (data & 0xF8F8F8F8F8F8F8F8ull)
407674ea351SPaolo Bonzini 		return false;
408674ea351SPaolo Bonzini 	/* 0, 1, 4, 5, 6, 7 are valid values.  */
409674ea351SPaolo Bonzini 	return (data | ((data & 0x0202020202020202ull) << 1)) == data;
410674ea351SPaolo Bonzini }
411674ea351SPaolo Bonzini 
4129b5e8532SSean Christopherson static inline bool kvm_dr7_valid(u64 data)
413b91991bfSKrish Sadhukhan {
414b91991bfSKrish Sadhukhan 	/* Bits [63:32] are reserved */
415b91991bfSKrish Sadhukhan 	return !(data >> 32);
416b91991bfSKrish Sadhukhan }
417f5f6145eSKrish Sadhukhan static inline bool kvm_dr6_valid(u64 data)
418f5f6145eSKrish Sadhukhan {
419f5f6145eSKrish Sadhukhan 	/* Bits [63:32] are reserved */
420f5f6145eSKrish Sadhukhan 	return !(data >> 32);
421f5f6145eSKrish Sadhukhan }
422b91991bfSKrish Sadhukhan 
4233f1a18b9SUros Bizjak /*
4243f1a18b9SUros Bizjak  * Trigger machine check on the host. We assume all the MSRs are already set up
4253f1a18b9SUros Bizjak  * by the CPU and that we still run on the same CPU as the MCE occurred on.
4263f1a18b9SUros Bizjak  * We pass a fake environment to the machine check handler because we want
4273f1a18b9SUros Bizjak  * the guest to be always treated like user space, no matter what context
4283f1a18b9SUros Bizjak  * it used internally.
4293f1a18b9SUros Bizjak  */
4303f1a18b9SUros Bizjak static inline void kvm_machine_check(void)
4313f1a18b9SUros Bizjak {
4323f1a18b9SUros Bizjak #if defined(CONFIG_X86_MCE)
4333f1a18b9SUros Bizjak 	struct pt_regs regs = {
4343f1a18b9SUros Bizjak 		.cs = 3, /* Fake ring 3 no matter what the guest ran on */
4353f1a18b9SUros Bizjak 		.flags = X86_EFLAGS_IF,
4363f1a18b9SUros Bizjak 	};
4373f1a18b9SUros Bizjak 
4383f1a18b9SUros Bizjak 	do_machine_check(&regs);
4393f1a18b9SUros Bizjak #endif
4403f1a18b9SUros Bizjak }
4413f1a18b9SUros Bizjak 
442139a12cfSAaron Lewis void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu);
443139a12cfSAaron Lewis void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu);
444841c2be0SMaxim Levitsky int kvm_spec_ctrl_test_value(u64 value);
445c33f6f22SSean Christopherson bool __kvm_is_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
4463f3393b3SBabu Moger int kvm_handle_memory_failure(struct kvm_vcpu *vcpu, int r,
4473f3393b3SBabu Moger 			      struct x86_exception *e);
4489715092fSBabu Moger int kvm_handle_invpcid(struct kvm_vcpu *vcpu, unsigned long type, gva_t gva);
44951de8151SAlexander Graf bool kvm_msr_allowed(struct kvm_vcpu *vcpu, u32 index, u32 type);
450674ea351SPaolo Bonzini 
451cc4cb017SMaxim Levitsky /*
452cc4cb017SMaxim Levitsky  * Internal error codes that are used to indicate that MSR emulation encountered
453cc4cb017SMaxim Levitsky  * an error that should result in #GP in the guest, unless userspace
454cc4cb017SMaxim Levitsky  * handles it.
455cc4cb017SMaxim Levitsky  */
456cc4cb017SMaxim Levitsky #define  KVM_MSR_RET_INVALID	2	/* in-kernel MSR emulation #GP condition */
457cc4cb017SMaxim Levitsky #define  KVM_MSR_RET_FILTERED	3	/* #GP due to userspace MSR filter */
4586abe9c13SPeter Xu 
459b899c132SKrish Sadhukhan #define __cr4_reserved_bits(__cpu_has, __c)             \
460b899c132SKrish Sadhukhan ({                                                      \
461b899c132SKrish Sadhukhan 	u64 __reserved_bits = CR4_RESERVED_BITS;        \
462b899c132SKrish Sadhukhan                                                         \
463b899c132SKrish Sadhukhan 	if (!__cpu_has(__c, X86_FEATURE_XSAVE))         \
464b899c132SKrish Sadhukhan 		__reserved_bits |= X86_CR4_OSXSAVE;     \
465b899c132SKrish Sadhukhan 	if (!__cpu_has(__c, X86_FEATURE_SMEP))          \
466b899c132SKrish Sadhukhan 		__reserved_bits |= X86_CR4_SMEP;        \
467b899c132SKrish Sadhukhan 	if (!__cpu_has(__c, X86_FEATURE_SMAP))          \
468b899c132SKrish Sadhukhan 		__reserved_bits |= X86_CR4_SMAP;        \
469b899c132SKrish Sadhukhan 	if (!__cpu_has(__c, X86_FEATURE_FSGSBASE))      \
470b899c132SKrish Sadhukhan 		__reserved_bits |= X86_CR4_FSGSBASE;    \
471b899c132SKrish Sadhukhan 	if (!__cpu_has(__c, X86_FEATURE_PKU))           \
472b899c132SKrish Sadhukhan 		__reserved_bits |= X86_CR4_PKE;         \
473b899c132SKrish Sadhukhan 	if (!__cpu_has(__c, X86_FEATURE_LA57))          \
474b899c132SKrish Sadhukhan 		__reserved_bits |= X86_CR4_LA57;        \
475b899c132SKrish Sadhukhan 	if (!__cpu_has(__c, X86_FEATURE_UMIP))          \
476b899c132SKrish Sadhukhan 		__reserved_bits |= X86_CR4_UMIP;        \
47753efe527SPaolo Bonzini 	if (!__cpu_has(__c, X86_FEATURE_VMX))           \
47853efe527SPaolo Bonzini 		__reserved_bits |= X86_CR4_VMXE;        \
4794683d758SVitaly Kuznetsov 	if (!__cpu_has(__c, X86_FEATURE_PCID))          \
4804683d758SVitaly Kuznetsov 		__reserved_bits |= X86_CR4_PCIDE;       \
481b899c132SKrish Sadhukhan 	__reserved_bits;                                \
482b899c132SKrish Sadhukhan })
483b899c132SKrish Sadhukhan 
4848f423a80STom Lendacky int kvm_sev_es_mmio_write(struct kvm_vcpu *vcpu, gpa_t src, unsigned int bytes,
4858f423a80STom Lendacky 			  void *dst);
4868f423a80STom Lendacky int kvm_sev_es_mmio_read(struct kvm_vcpu *vcpu, gpa_t src, unsigned int bytes,
4878f423a80STom Lendacky 			 void *dst);
4887ed9abfeSTom Lendacky int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size,
4897ed9abfeSTom Lendacky 			 unsigned int port, void *data,  unsigned int count,
4907ed9abfeSTom Lendacky 			 int in);
4918f423a80STom Lendacky 
49226eef70cSAvi Kivity #endif
493