xref: /linux/arch/x86/kvm/x86.h (revision cb00a70bd4b7e42dcbd6cd80b3f1697b10cdb44e)
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 
13648fc8aeSSean Christopherson #define KVM_NESTED_VMENTER_CONSISTENCY_CHECK(consistency_check)		\
14648fc8aeSSean Christopherson ({									\
15648fc8aeSSean Christopherson 	bool failed = (consistency_check);				\
16648fc8aeSSean Christopherson 	if (failed)							\
17648fc8aeSSean Christopherson 		trace_kvm_nested_vmenter_failed(#consistency_check, 0);	\
18648fc8aeSSean Christopherson 	failed;								\
19648fc8aeSSean Christopherson })
20648fc8aeSSean Christopherson 
21c8e88717SBabu Moger #define KVM_DEFAULT_PLE_GAP		128
22c8e88717SBabu Moger #define KVM_VMX_DEFAULT_PLE_WINDOW	4096
23c8e88717SBabu Moger #define KVM_DEFAULT_PLE_WINDOW_GROW	2
24c8e88717SBabu Moger #define KVM_DEFAULT_PLE_WINDOW_SHRINK	0
25c8e88717SBabu Moger #define KVM_VMX_DEFAULT_PLE_WINDOW_MAX	UINT_MAX
268566ac8bSBabu Moger #define KVM_SVM_DEFAULT_PLE_WINDOW_MAX	USHRT_MAX
278566ac8bSBabu Moger #define KVM_SVM_DEFAULT_PLE_WINDOW	3000
28c8e88717SBabu Moger 
29c8e88717SBabu Moger static inline unsigned int __grow_ple_window(unsigned int val,
30c8e88717SBabu Moger 		unsigned int base, unsigned int modifier, unsigned int max)
31c8e88717SBabu Moger {
32c8e88717SBabu Moger 	u64 ret = val;
33c8e88717SBabu Moger 
34c8e88717SBabu Moger 	if (modifier < 1)
35c8e88717SBabu Moger 		return base;
36c8e88717SBabu Moger 
37c8e88717SBabu Moger 	if (modifier < base)
38c8e88717SBabu Moger 		ret *= modifier;
39c8e88717SBabu Moger 	else
40c8e88717SBabu Moger 		ret += modifier;
41c8e88717SBabu Moger 
42c8e88717SBabu Moger 	return min(ret, (u64)max);
43c8e88717SBabu Moger }
44c8e88717SBabu Moger 
45c8e88717SBabu Moger static inline unsigned int __shrink_ple_window(unsigned int val,
46c8e88717SBabu Moger 		unsigned int base, unsigned int modifier, unsigned int min)
47c8e88717SBabu Moger {
48c8e88717SBabu Moger 	if (modifier < 1)
49c8e88717SBabu Moger 		return base;
50c8e88717SBabu Moger 
51c8e88717SBabu Moger 	if (modifier < base)
52c8e88717SBabu Moger 		val /= modifier;
53c8e88717SBabu Moger 	else
54c8e88717SBabu Moger 		val -= modifier;
55c8e88717SBabu Moger 
56c8e88717SBabu Moger 	return max(val, min);
57c8e88717SBabu Moger }
58c8e88717SBabu Moger 
5974545705SRadim Krčmář #define MSR_IA32_CR_PAT_DEFAULT  0x0007040600070406ULL
6074545705SRadim Krčmář 
6140e5f908SSean Christopherson void kvm_service_local_tlb_flush_requests(struct kvm_vcpu *vcpu);
62cb6a32c2SSean Christopherson int kvm_check_nested_events(struct kvm_vcpu *vcpu);
63cb6a32c2SSean Christopherson 
6426eef70cSAvi Kivity static inline void kvm_clear_exception_queue(struct kvm_vcpu *vcpu)
6526eef70cSAvi Kivity {
665c7d4f9aSLiran Alon 	vcpu->arch.exception.pending = false;
67664f8e26SWanpeng Li 	vcpu->arch.exception.injected = false;
6826eef70cSAvi Kivity }
6926eef70cSAvi Kivity 
7066fd3f7fSGleb Natapov static inline void kvm_queue_interrupt(struct kvm_vcpu *vcpu, u8 vector,
7166fd3f7fSGleb Natapov 	bool soft)
72937a7eaeSAvi Kivity {
7304140b41SLiran Alon 	vcpu->arch.interrupt.injected = true;
7466fd3f7fSGleb Natapov 	vcpu->arch.interrupt.soft = soft;
75937a7eaeSAvi Kivity 	vcpu->arch.interrupt.nr = vector;
76937a7eaeSAvi Kivity }
77937a7eaeSAvi Kivity 
78937a7eaeSAvi Kivity static inline void kvm_clear_interrupt_queue(struct kvm_vcpu *vcpu)
79937a7eaeSAvi Kivity {
8004140b41SLiran Alon 	vcpu->arch.interrupt.injected = false;
81937a7eaeSAvi Kivity }
82937a7eaeSAvi Kivity 
833298b75cSGleb Natapov static inline bool kvm_event_needs_reinjection(struct kvm_vcpu *vcpu)
843298b75cSGleb Natapov {
8504140b41SLiran Alon 	return vcpu->arch.exception.injected || vcpu->arch.interrupt.injected ||
863298b75cSGleb Natapov 		vcpu->arch.nmi_injected;
873298b75cSGleb Natapov }
8866fd3f7fSGleb Natapov 
8966fd3f7fSGleb Natapov static inline bool kvm_exception_is_soft(unsigned int nr)
9066fd3f7fSGleb Natapov {
9166fd3f7fSGleb Natapov 	return (nr == BP_VECTOR) || (nr == OF_VECTOR);
9266fd3f7fSGleb Natapov }
93fc61b800SGleb Natapov 
943eeb3288SAvi Kivity static inline bool is_protmode(struct kvm_vcpu *vcpu)
953eeb3288SAvi Kivity {
963eeb3288SAvi Kivity 	return kvm_read_cr0_bits(vcpu, X86_CR0_PE);
973eeb3288SAvi Kivity }
983eeb3288SAvi Kivity 
99836a1b3cSAvi Kivity static inline int is_long_mode(struct kvm_vcpu *vcpu)
100836a1b3cSAvi Kivity {
101836a1b3cSAvi Kivity #ifdef CONFIG_X86_64
102f6801dffSAvi Kivity 	return vcpu->arch.efer & EFER_LMA;
103836a1b3cSAvi Kivity #else
104836a1b3cSAvi Kivity 	return 0;
105836a1b3cSAvi Kivity #endif
106836a1b3cSAvi Kivity }
107836a1b3cSAvi Kivity 
1085777392eSNadav Amit static inline bool is_64_bit_mode(struct kvm_vcpu *vcpu)
1095777392eSNadav Amit {
1105777392eSNadav Amit 	int cs_db, cs_l;
1115777392eSNadav Amit 
112b5aead00STom Lendacky 	WARN_ON_ONCE(vcpu->arch.guest_state_protected);
113b5aead00STom Lendacky 
1145777392eSNadav Amit 	if (!is_long_mode(vcpu))
1155777392eSNadav Amit 		return false;
116b3646477SJason Baron 	static_call(kvm_x86_get_cs_db_l_bits)(vcpu, &cs_db, &cs_l);
1175777392eSNadav Amit 	return cs_l;
1185777392eSNadav Amit }
1195777392eSNadav Amit 
120b5aead00STom Lendacky static inline bool is_64_bit_hypercall(struct kvm_vcpu *vcpu)
121b5aead00STom Lendacky {
122b5aead00STom Lendacky 	/*
123b5aead00STom Lendacky 	 * If running with protected guest state, the CS register is not
124b5aead00STom Lendacky 	 * accessible. The hypercall register values will have had to been
125b5aead00STom Lendacky 	 * provided in 64-bit mode, so assume the guest is in 64-bit.
126b5aead00STom Lendacky 	 */
127b5aead00STom Lendacky 	return vcpu->arch.guest_state_protected || is_64_bit_mode(vcpu);
128b5aead00STom Lendacky }
129b5aead00STom Lendacky 
1300447378aSMarc Orr static inline bool x86_exception_has_error_code(unsigned int vector)
1310447378aSMarc Orr {
1320447378aSMarc Orr 	static u32 exception_has_error_code = BIT(DF_VECTOR) | BIT(TS_VECTOR) |
1330447378aSMarc Orr 			BIT(NP_VECTOR) | BIT(SS_VECTOR) | BIT(GP_VECTOR) |
1340447378aSMarc Orr 			BIT(PF_VECTOR) | BIT(AC_VECTOR);
1350447378aSMarc Orr 
1360447378aSMarc Orr 	return (1U << vector) & exception_has_error_code;
1370447378aSMarc Orr }
1380447378aSMarc Orr 
1396539e738SJoerg Roedel static inline bool mmu_is_nested(struct kvm_vcpu *vcpu)
1406539e738SJoerg Roedel {
1416539e738SJoerg Roedel 	return vcpu->arch.walk_mmu == &vcpu->arch.nested_mmu;
1426539e738SJoerg Roedel }
1436539e738SJoerg Roedel 
144836a1b3cSAvi Kivity static inline int is_pae(struct kvm_vcpu *vcpu)
145836a1b3cSAvi Kivity {
146836a1b3cSAvi Kivity 	return kvm_read_cr4_bits(vcpu, X86_CR4_PAE);
147836a1b3cSAvi Kivity }
148836a1b3cSAvi Kivity 
149836a1b3cSAvi Kivity static inline int is_pse(struct kvm_vcpu *vcpu)
150836a1b3cSAvi Kivity {
151836a1b3cSAvi Kivity 	return kvm_read_cr4_bits(vcpu, X86_CR4_PSE);
152836a1b3cSAvi Kivity }
153836a1b3cSAvi Kivity 
154836a1b3cSAvi Kivity static inline int is_paging(struct kvm_vcpu *vcpu)
155836a1b3cSAvi Kivity {
156c36fc04eSDavidlohr Bueso 	return likely(kvm_read_cr0_bits(vcpu, X86_CR0_PG));
157836a1b3cSAvi Kivity }
158836a1b3cSAvi Kivity 
159bf03d4f9SPaolo Bonzini static inline bool is_pae_paging(struct kvm_vcpu *vcpu)
160bf03d4f9SPaolo Bonzini {
161bf03d4f9SPaolo Bonzini 	return !is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu);
162bf03d4f9SPaolo Bonzini }
163bf03d4f9SPaolo Bonzini 
164fd8cb433SYu Zhang static inline u8 vcpu_virt_addr_bits(struct kvm_vcpu *vcpu)
165fd8cb433SYu Zhang {
166fd8cb433SYu Zhang 	return kvm_read_cr4_bits(vcpu, X86_CR4_LA57) ? 57 : 48;
167fd8cb433SYu Zhang }
168fd8cb433SYu Zhang 
169fd8cb433SYu Zhang static inline u64 get_canonical(u64 la, u8 vaddr_bits)
170fd8cb433SYu Zhang {
171fd8cb433SYu Zhang 	return ((int64_t)la << (64 - vaddr_bits)) >> (64 - vaddr_bits);
172fd8cb433SYu Zhang }
173fd8cb433SYu Zhang 
174fd8cb433SYu Zhang static inline bool is_noncanonical_address(u64 la, struct kvm_vcpu *vcpu)
175fd8cb433SYu Zhang {
176fd8cb433SYu Zhang 	return get_canonical(la, vcpu_virt_addr_bits(vcpu)) != la;
177fd8cb433SYu Zhang }
178fd8cb433SYu Zhang 
179bebb106aSXiao Guangrong static inline void vcpu_cache_mmio_info(struct kvm_vcpu *vcpu,
180bebb106aSXiao Guangrong 					gva_t gva, gfn_t gfn, unsigned access)
181bebb106aSXiao Guangrong {
182ddfd1730SSean Christopherson 	u64 gen = kvm_memslots(vcpu->kvm)->generation;
183ddfd1730SSean Christopherson 
184361209e0SSean Christopherson 	if (unlikely(gen & KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS))
185ddfd1730SSean Christopherson 		return;
186ddfd1730SSean Christopherson 
1879034e6e8SPaolo Bonzini 	/*
1889034e6e8SPaolo Bonzini 	 * If this is a shadow nested page table, the "GVA" is
1899034e6e8SPaolo Bonzini 	 * actually a nGPA.
1909034e6e8SPaolo Bonzini 	 */
1919034e6e8SPaolo Bonzini 	vcpu->arch.mmio_gva = mmu_is_nested(vcpu) ? 0 : gva & PAGE_MASK;
192871bd034SSean Christopherson 	vcpu->arch.mmio_access = access;
193bebb106aSXiao Guangrong 	vcpu->arch.mmio_gfn = gfn;
194ddfd1730SSean Christopherson 	vcpu->arch.mmio_gen = gen;
19556f17dd3SDavid Matlack }
19656f17dd3SDavid Matlack 
19756f17dd3SDavid Matlack static inline bool vcpu_match_mmio_gen(struct kvm_vcpu *vcpu)
19856f17dd3SDavid Matlack {
19956f17dd3SDavid Matlack 	return vcpu->arch.mmio_gen == kvm_memslots(vcpu->kvm)->generation;
200bebb106aSXiao Guangrong }
201bebb106aSXiao Guangrong 
202bebb106aSXiao Guangrong /*
20356f17dd3SDavid Matlack  * Clear the mmio cache info for the given gva. If gva is MMIO_GVA_ANY, we
20456f17dd3SDavid Matlack  * clear all mmio cache info.
205bebb106aSXiao Guangrong  */
20656f17dd3SDavid Matlack #define MMIO_GVA_ANY (~(gva_t)0)
20756f17dd3SDavid Matlack 
208bebb106aSXiao Guangrong static inline void vcpu_clear_mmio_info(struct kvm_vcpu *vcpu, gva_t gva)
209bebb106aSXiao Guangrong {
21056f17dd3SDavid Matlack 	if (gva != MMIO_GVA_ANY && vcpu->arch.mmio_gva != (gva & PAGE_MASK))
211bebb106aSXiao Guangrong 		return;
212bebb106aSXiao Guangrong 
213bebb106aSXiao Guangrong 	vcpu->arch.mmio_gva = 0;
214bebb106aSXiao Guangrong }
215bebb106aSXiao Guangrong 
216bebb106aSXiao Guangrong static inline bool vcpu_match_mmio_gva(struct kvm_vcpu *vcpu, unsigned long gva)
217bebb106aSXiao Guangrong {
21856f17dd3SDavid Matlack 	if (vcpu_match_mmio_gen(vcpu) && vcpu->arch.mmio_gva &&
21956f17dd3SDavid Matlack 	      vcpu->arch.mmio_gva == (gva & PAGE_MASK))
220bebb106aSXiao Guangrong 		return true;
221bebb106aSXiao Guangrong 
222bebb106aSXiao Guangrong 	return false;
223bebb106aSXiao Guangrong }
224bebb106aSXiao Guangrong 
225bebb106aSXiao Guangrong static inline bool vcpu_match_mmio_gpa(struct kvm_vcpu *vcpu, gpa_t gpa)
226bebb106aSXiao Guangrong {
22756f17dd3SDavid Matlack 	if (vcpu_match_mmio_gen(vcpu) && vcpu->arch.mmio_gfn &&
22856f17dd3SDavid Matlack 	      vcpu->arch.mmio_gfn == gpa >> PAGE_SHIFT)
229bebb106aSXiao Guangrong 		return true;
230bebb106aSXiao Guangrong 
231bebb106aSXiao Guangrong 	return false;
232bebb106aSXiao Guangrong }
233bebb106aSXiao Guangrong 
23427b4a9c4SSean Christopherson static inline unsigned long kvm_register_read(struct kvm_vcpu *vcpu, int reg)
2355777392eSNadav Amit {
23627b4a9c4SSean Christopherson 	unsigned long val = kvm_register_read_raw(vcpu, reg);
2375777392eSNadav Amit 
2385777392eSNadav Amit 	return is_64_bit_mode(vcpu) ? val : (u32)val;
2395777392eSNadav Amit }
2405777392eSNadav Amit 
24127b4a9c4SSean Christopherson static inline void kvm_register_write(struct kvm_vcpu *vcpu,
242489cbcf0SSean Christopherson 				       int reg, unsigned long val)
24327e6fb5dSNadav Amit {
24427e6fb5dSNadav Amit 	if (!is_64_bit_mode(vcpu))
24527e6fb5dSNadav Amit 		val = (u32)val;
24627b4a9c4SSean Christopherson 	return kvm_register_write_raw(vcpu, reg, val);
24727e6fb5dSNadav Amit }
24827e6fb5dSNadav Amit 
24941dbc6bcSPaolo Bonzini static inline bool kvm_check_has_quirk(struct kvm *kvm, u64 quirk)
25041dbc6bcSPaolo Bonzini {
25141dbc6bcSPaolo Bonzini 	return !(kvm->arch.disabled_quirks & quirk);
25241dbc6bcSPaolo Bonzini }
25341dbc6bcSPaolo Bonzini 
25427cbe7d6SLiran Alon static inline bool kvm_vcpu_latch_init(struct kvm_vcpu *vcpu)
25527cbe7d6SLiran Alon {
256b3646477SJason Baron 	return is_smm(vcpu) || static_call(kvm_x86_apic_init_signal_blocked)(vcpu);
25727cbe7d6SLiran Alon }
25827cbe7d6SLiran Alon 
2599497e1f2SSean Christopherson void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip);
260ff9d07a0SZhang, Yanmin 
261108b249cSPaolo Bonzini u64 get_kvmclock_ns(struct kvm *kvm);
26299e3e30aSZachary Amsden 
263ce14e868SPaolo Bonzini int kvm_read_guest_virt(struct kvm_vcpu *vcpu,
264064aea77SNadav Har'El 	gva_t addr, void *val, unsigned int bytes,
265064aea77SNadav Har'El 	struct x86_exception *exception);
266064aea77SNadav Har'El 
267ce14e868SPaolo Bonzini int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu,
2686a4d7550SNadav Har'El 	gva_t addr, void *val, unsigned int bytes,
2696a4d7550SNadav Har'El 	struct x86_exception *exception);
2706a4d7550SNadav Har'El 
271082d06edSWanpeng Li int handle_ud(struct kvm_vcpu *vcpu);
272082d06edSWanpeng Li 
273da998b46SJim Mattson void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu);
274da998b46SJim Mattson 
27519efffa2SXiao Guangrong void kvm_vcpu_mtrr_init(struct kvm_vcpu *vcpu);
276ff53604bSXiao Guangrong u8 kvm_mtrr_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn);
2774566654bSNadav Amit bool kvm_mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data);
278ff53604bSXiao Guangrong int kvm_mtrr_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data);
279ff53604bSXiao Guangrong int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata);
2806a39bbc5SXiao Guangrong bool kvm_mtrr_check_gfn_range_consistency(struct kvm_vcpu *vcpu, gfn_t gfn,
2816a39bbc5SXiao Guangrong 					  int page_num);
28252004014SFeng Wu bool kvm_vector_hashing_enabled(void);
28389786147SMohammed Gamal void kvm_fixup_and_inject_pf_error(struct kvm_vcpu *vcpu, gva_t gva, u16 error_code);
2844aa2691dSWei Huang int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type,
2854aa2691dSWei Huang 				    void *insn, int insn_len);
286736c291cSSean Christopherson int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
287c60658d1SSean Christopherson 			    int emulation_type, void *insn, int insn_len);
288404d5d7bSWanpeng Li fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu);
2894566654bSNadav Amit 
29000b27a3eSAvi Kivity extern u64 host_xcr0;
291cfc48181SSean Christopherson extern u64 supported_xcr0;
29286137773STom Lendacky extern u64 host_xss;
293408e9a31SPaolo Bonzini extern u64 supported_xss;
2944732f244SLike Xu extern bool enable_pmu;
2954ff41732SPaolo Bonzini 
296615a4ae1SSean Christopherson static inline bool kvm_mpx_supported(void)
297615a4ae1SSean Christopherson {
298615a4ae1SSean Christopherson 	return (supported_xcr0 & (XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR))
299615a4ae1SSean Christopherson 		== (XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR);
300615a4ae1SSean Christopherson }
301615a4ae1SSean Christopherson 
3029ed96e87SMarcelo Tosatti extern unsigned int min_timer_period_us;
3039ed96e87SMarcelo Tosatti 
304c4ae60e4SLiran Alon extern bool enable_vmware_backdoor;
305c4ae60e4SLiran Alon 
3060c5f81daSWanpeng Li extern int pi_inject_timer;
3070c5f81daSWanpeng Li 
308d855066fSLike Xu extern bool report_ignored_msrs;
309d855066fSLike Xu 
310*cb00a70bSDavid Matlack extern bool eager_page_split;
311*cb00a70bSDavid Matlack 
3128d93c874SMarcelo Tosatti static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec)
3138d93c874SMarcelo Tosatti {
3148d93c874SMarcelo Tosatti 	return pvclock_scale_delta(nsec, vcpu->arch.virtual_tsc_mult,
3158d93c874SMarcelo Tosatti 				   vcpu->arch.virtual_tsc_shift);
3168d93c874SMarcelo Tosatti }
3178d93c874SMarcelo Tosatti 
318b51012deSPaolo Bonzini /* Same "calling convention" as do_div:
319b51012deSPaolo Bonzini  * - divide (n << 32) by base
320b51012deSPaolo Bonzini  * - put result in n
321b51012deSPaolo Bonzini  * - return remainder
322b51012deSPaolo Bonzini  */
323b51012deSPaolo Bonzini #define do_shl32_div32(n, base)					\
324b51012deSPaolo Bonzini 	({							\
325b51012deSPaolo Bonzini 	    u32 __quot, __rem;					\
326b51012deSPaolo Bonzini 	    asm("divl %2" : "=a" (__quot), "=d" (__rem)		\
327b51012deSPaolo Bonzini 			: "rm" (base), "0" (0), "1" ((u32) n));	\
328b51012deSPaolo Bonzini 	    n = __quot;						\
329b51012deSPaolo Bonzini 	    __rem;						\
330b51012deSPaolo Bonzini 	 })
331b51012deSPaolo Bonzini 
3324d5422ceSWanpeng Li static inline bool kvm_mwait_in_guest(struct kvm *kvm)
333668fffa3SMichael S. Tsirkin {
3344d5422ceSWanpeng Li 	return kvm->arch.mwait_in_guest;
335668fffa3SMichael S. Tsirkin }
336668fffa3SMichael S. Tsirkin 
337caa057a2SWanpeng Li static inline bool kvm_hlt_in_guest(struct kvm *kvm)
338caa057a2SWanpeng Li {
339caa057a2SWanpeng Li 	return kvm->arch.hlt_in_guest;
340caa057a2SWanpeng Li }
341caa057a2SWanpeng Li 
342b31c114bSWanpeng Li static inline bool kvm_pause_in_guest(struct kvm *kvm)
343b31c114bSWanpeng Li {
344b31c114bSWanpeng Li 	return kvm->arch.pause_in_guest;
345b31c114bSWanpeng Li }
346b31c114bSWanpeng Li 
347b5170063SWanpeng Li static inline bool kvm_cstate_in_guest(struct kvm *kvm)
348b5170063SWanpeng Li {
349b5170063SWanpeng Li 	return kvm->arch.cstate_in_guest;
350b5170063SWanpeng Li }
351b5170063SWanpeng Li 
352db215756SSean Christopherson enum kvm_intr_type {
353db215756SSean Christopherson 	/* Values are arbitrary, but must be non-zero. */
354db215756SSean Christopherson 	KVM_HANDLING_IRQ = 1,
355db215756SSean Christopherson 	KVM_HANDLING_NMI,
356db215756SSean Christopherson };
357dd60d217SAndi Kleen 
358db215756SSean Christopherson static inline void kvm_before_interrupt(struct kvm_vcpu *vcpu,
359db215756SSean Christopherson 					enum kvm_intr_type intr)
360dd60d217SAndi Kleen {
361db215756SSean Christopherson 	WRITE_ONCE(vcpu->arch.handling_intr_from_guest, (u8)intr);
362dd60d217SAndi Kleen }
363dd60d217SAndi Kleen 
364dd60d217SAndi Kleen static inline void kvm_after_interrupt(struct kvm_vcpu *vcpu)
365dd60d217SAndi Kleen {
36673cd107bSSean Christopherson 	WRITE_ONCE(vcpu->arch.handling_intr_from_guest, 0);
367dd60d217SAndi Kleen }
368dd60d217SAndi Kleen 
36973cd107bSSean Christopherson static inline bool kvm_handling_nmi_from_guest(struct kvm_vcpu *vcpu)
37073cd107bSSean Christopherson {
371db215756SSean Christopherson 	return vcpu->arch.handling_intr_from_guest == KVM_HANDLING_NMI;
37273cd107bSSean Christopherson }
373674ea351SPaolo Bonzini 
374674ea351SPaolo Bonzini static inline bool kvm_pat_valid(u64 data)
375674ea351SPaolo Bonzini {
376674ea351SPaolo Bonzini 	if (data & 0xF8F8F8F8F8F8F8F8ull)
377674ea351SPaolo Bonzini 		return false;
378674ea351SPaolo Bonzini 	/* 0, 1, 4, 5, 6, 7 are valid values.  */
379674ea351SPaolo Bonzini 	return (data | ((data & 0x0202020202020202ull) << 1)) == data;
380674ea351SPaolo Bonzini }
381674ea351SPaolo Bonzini 
3829b5e8532SSean Christopherson static inline bool kvm_dr7_valid(u64 data)
383b91991bfSKrish Sadhukhan {
384b91991bfSKrish Sadhukhan 	/* Bits [63:32] are reserved */
385b91991bfSKrish Sadhukhan 	return !(data >> 32);
386b91991bfSKrish Sadhukhan }
387f5f6145eSKrish Sadhukhan static inline bool kvm_dr6_valid(u64 data)
388f5f6145eSKrish Sadhukhan {
389f5f6145eSKrish Sadhukhan 	/* Bits [63:32] are reserved */
390f5f6145eSKrish Sadhukhan 	return !(data >> 32);
391f5f6145eSKrish Sadhukhan }
392b91991bfSKrish Sadhukhan 
3933f1a18b9SUros Bizjak /*
3943f1a18b9SUros Bizjak  * Trigger machine check on the host. We assume all the MSRs are already set up
3953f1a18b9SUros Bizjak  * by the CPU and that we still run on the same CPU as the MCE occurred on.
3963f1a18b9SUros Bizjak  * We pass a fake environment to the machine check handler because we want
3973f1a18b9SUros Bizjak  * the guest to be always treated like user space, no matter what context
3983f1a18b9SUros Bizjak  * it used internally.
3993f1a18b9SUros Bizjak  */
4003f1a18b9SUros Bizjak static inline void kvm_machine_check(void)
4013f1a18b9SUros Bizjak {
4023f1a18b9SUros Bizjak #if defined(CONFIG_X86_MCE)
4033f1a18b9SUros Bizjak 	struct pt_regs regs = {
4043f1a18b9SUros Bizjak 		.cs = 3, /* Fake ring 3 no matter what the guest ran on */
4053f1a18b9SUros Bizjak 		.flags = X86_EFLAGS_IF,
4063f1a18b9SUros Bizjak 	};
4073f1a18b9SUros Bizjak 
4083f1a18b9SUros Bizjak 	do_machine_check(&regs);
4093f1a18b9SUros Bizjak #endif
4103f1a18b9SUros Bizjak }
4113f1a18b9SUros Bizjak 
412139a12cfSAaron Lewis void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu);
413139a12cfSAaron Lewis void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu);
414841c2be0SMaxim Levitsky int kvm_spec_ctrl_test_value(u64 value);
415ee69c92bSSean Christopherson bool kvm_is_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
4163f3393b3SBabu Moger int kvm_handle_memory_failure(struct kvm_vcpu *vcpu, int r,
4173f3393b3SBabu Moger 			      struct x86_exception *e);
4189715092fSBabu Moger int kvm_handle_invpcid(struct kvm_vcpu *vcpu, unsigned long type, gva_t gva);
41951de8151SAlexander Graf bool kvm_msr_allowed(struct kvm_vcpu *vcpu, u32 index, u32 type);
420674ea351SPaolo Bonzini 
421cc4cb017SMaxim Levitsky /*
422cc4cb017SMaxim Levitsky  * Internal error codes that are used to indicate that MSR emulation encountered
423cc4cb017SMaxim Levitsky  * an error that should result in #GP in the guest, unless userspace
424cc4cb017SMaxim Levitsky  * handles it.
425cc4cb017SMaxim Levitsky  */
426cc4cb017SMaxim Levitsky #define  KVM_MSR_RET_INVALID	2	/* in-kernel MSR emulation #GP condition */
427cc4cb017SMaxim Levitsky #define  KVM_MSR_RET_FILTERED	3	/* #GP due to userspace MSR filter */
4286abe9c13SPeter Xu 
429b899c132SKrish Sadhukhan #define __cr4_reserved_bits(__cpu_has, __c)             \
430b899c132SKrish Sadhukhan ({                                                      \
431b899c132SKrish Sadhukhan 	u64 __reserved_bits = CR4_RESERVED_BITS;        \
432b899c132SKrish Sadhukhan                                                         \
433b899c132SKrish Sadhukhan 	if (!__cpu_has(__c, X86_FEATURE_XSAVE))         \
434b899c132SKrish Sadhukhan 		__reserved_bits |= X86_CR4_OSXSAVE;     \
435b899c132SKrish Sadhukhan 	if (!__cpu_has(__c, X86_FEATURE_SMEP))          \
436b899c132SKrish Sadhukhan 		__reserved_bits |= X86_CR4_SMEP;        \
437b899c132SKrish Sadhukhan 	if (!__cpu_has(__c, X86_FEATURE_SMAP))          \
438b899c132SKrish Sadhukhan 		__reserved_bits |= X86_CR4_SMAP;        \
439b899c132SKrish Sadhukhan 	if (!__cpu_has(__c, X86_FEATURE_FSGSBASE))      \
440b899c132SKrish Sadhukhan 		__reserved_bits |= X86_CR4_FSGSBASE;    \
441b899c132SKrish Sadhukhan 	if (!__cpu_has(__c, X86_FEATURE_PKU))           \
442b899c132SKrish Sadhukhan 		__reserved_bits |= X86_CR4_PKE;         \
443b899c132SKrish Sadhukhan 	if (!__cpu_has(__c, X86_FEATURE_LA57))          \
444b899c132SKrish Sadhukhan 		__reserved_bits |= X86_CR4_LA57;        \
445b899c132SKrish Sadhukhan 	if (!__cpu_has(__c, X86_FEATURE_UMIP))          \
446b899c132SKrish Sadhukhan 		__reserved_bits |= X86_CR4_UMIP;        \
44753efe527SPaolo Bonzini 	if (!__cpu_has(__c, X86_FEATURE_VMX))           \
44853efe527SPaolo Bonzini 		__reserved_bits |= X86_CR4_VMXE;        \
4494683d758SVitaly Kuznetsov 	if (!__cpu_has(__c, X86_FEATURE_PCID))          \
4504683d758SVitaly Kuznetsov 		__reserved_bits |= X86_CR4_PCIDE;       \
451b899c132SKrish Sadhukhan 	__reserved_bits;                                \
452b899c132SKrish Sadhukhan })
453b899c132SKrish Sadhukhan 
4548f423a80STom Lendacky int kvm_sev_es_mmio_write(struct kvm_vcpu *vcpu, gpa_t src, unsigned int bytes,
4558f423a80STom Lendacky 			  void *dst);
4568f423a80STom Lendacky int kvm_sev_es_mmio_read(struct kvm_vcpu *vcpu, gpa_t src, unsigned int bytes,
4578f423a80STom Lendacky 			 void *dst);
4587ed9abfeSTom Lendacky int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size,
4597ed9abfeSTom Lendacky 			 unsigned int port, void *data,  unsigned int count,
4607ed9abfeSTom Lendacky 			 int in);
4618f423a80STom Lendacky 
46226eef70cSAvi Kivity #endif
463