xref: /openbmc/linux/arch/x86/kvm/x86.h (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
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>
655cd57b5SSean Christopherson #include <asm/fpu/xstate.h>
73f1a18b9SUros Bizjak #include <asm/mce.h>
88d93c874SMarcelo Tosatti #include <asm/pvclock.h>
93eeb3288SAvi Kivity #include "kvm_cache_regs.h"
102f728d66SSean Christopherson #include "kvm_emulate.h"
1126eef70cSAvi Kivity 
12938c8745SSean Christopherson struct kvm_caps {
13938c8745SSean Christopherson 	/* control of guest tsc rate supported? */
14938c8745SSean Christopherson 	bool has_tsc_control;
15938c8745SSean Christopherson 	/* maximum supported tsc_khz for guests */
16938c8745SSean Christopherson 	u32  max_guest_tsc_khz;
17938c8745SSean Christopherson 	/* number of bits of the fractional part of the TSC scaling ratio */
18938c8745SSean Christopherson 	u8   tsc_scaling_ratio_frac_bits;
19938c8745SSean Christopherson 	/* maximum allowed value of TSC scaling ratio */
20938c8745SSean Christopherson 	u64  max_tsc_scaling_ratio;
21938c8745SSean Christopherson 	/* 1ull << kvm_caps.tsc_scaling_ratio_frac_bits */
22938c8745SSean Christopherson 	u64  default_tsc_scaling_ratio;
23938c8745SSean Christopherson 	/* bus lock detection supported? */
24938c8745SSean Christopherson 	bool has_bus_lock_exit;
252f4073e0STao Xu 	/* notify VM exit supported? */
262f4073e0STao Xu 	bool has_notify_vmexit;
27938c8745SSean Christopherson 
28938c8745SSean Christopherson 	u64 supported_mce_cap;
29938c8745SSean Christopherson 	u64 supported_xcr0;
30938c8745SSean Christopherson 	u64 supported_xss;
31bec46859SSean Christopherson 	u64 supported_perf_cap;
32938c8745SSean Christopherson };
33938c8745SSean Christopherson 
3465297341SUros Bizjak void kvm_spurious_fault(void);
3565297341SUros Bizjak 
36648fc8aeSSean Christopherson #define KVM_NESTED_VMENTER_CONSISTENCY_CHECK(consistency_check)		\
37648fc8aeSSean Christopherson ({									\
38648fc8aeSSean Christopherson 	bool failed = (consistency_check);				\
39648fc8aeSSean Christopherson 	if (failed)							\
40648fc8aeSSean Christopherson 		trace_kvm_nested_vmenter_failed(#consistency_check, 0);	\
41648fc8aeSSean Christopherson 	failed;								\
42648fc8aeSSean Christopherson })
43648fc8aeSSean Christopherson 
445757f5b9SSean Christopherson /*
455757f5b9SSean Christopherson  * The first...last VMX feature MSRs that are emulated by KVM.  This may or may
465757f5b9SSean Christopherson  * not cover all known VMX MSRs, as KVM doesn't emulate an MSR until there's an
475757f5b9SSean Christopherson  * associated feature that KVM supports for nested virtualization.
485757f5b9SSean Christopherson  */
495757f5b9SSean Christopherson #define KVM_FIRST_EMULATED_VMX_MSR	MSR_IA32_VMX_BASIC
505757f5b9SSean Christopherson #define KVM_LAST_EMULATED_VMX_MSR	MSR_IA32_VMX_VMFUNC
515757f5b9SSean Christopherson 
52c8e88717SBabu Moger #define KVM_DEFAULT_PLE_GAP		128
53c8e88717SBabu Moger #define KVM_VMX_DEFAULT_PLE_WINDOW	4096
54c8e88717SBabu Moger #define KVM_DEFAULT_PLE_WINDOW_GROW	2
55c8e88717SBabu Moger #define KVM_DEFAULT_PLE_WINDOW_SHRINK	0
56c8e88717SBabu Moger #define KVM_VMX_DEFAULT_PLE_WINDOW_MAX	UINT_MAX
578566ac8bSBabu Moger #define KVM_SVM_DEFAULT_PLE_WINDOW_MAX	USHRT_MAX
588566ac8bSBabu Moger #define KVM_SVM_DEFAULT_PLE_WINDOW	3000
59c8e88717SBabu Moger 
__grow_ple_window(unsigned int val,unsigned int base,unsigned int modifier,unsigned int max)60c8e88717SBabu Moger static inline unsigned int __grow_ple_window(unsigned int val,
61c8e88717SBabu Moger 		unsigned int base, unsigned int modifier, unsigned int max)
62c8e88717SBabu Moger {
63c8e88717SBabu Moger 	u64 ret = val;
64c8e88717SBabu Moger 
65c8e88717SBabu Moger 	if (modifier < 1)
66c8e88717SBabu Moger 		return base;
67c8e88717SBabu Moger 
68c8e88717SBabu Moger 	if (modifier < base)
69c8e88717SBabu Moger 		ret *= modifier;
70c8e88717SBabu Moger 	else
71c8e88717SBabu Moger 		ret += modifier;
72c8e88717SBabu Moger 
73c8e88717SBabu Moger 	return min(ret, (u64)max);
74c8e88717SBabu Moger }
75c8e88717SBabu Moger 
__shrink_ple_window(unsigned int val,unsigned int base,unsigned int modifier,unsigned int min)76c8e88717SBabu Moger static inline unsigned int __shrink_ple_window(unsigned int val,
77c8e88717SBabu Moger 		unsigned int base, unsigned int modifier, unsigned int min)
78c8e88717SBabu Moger {
79c8e88717SBabu Moger 	if (modifier < 1)
80c8e88717SBabu Moger 		return base;
81c8e88717SBabu Moger 
82c8e88717SBabu Moger 	if (modifier < base)
83c8e88717SBabu Moger 		val /= modifier;
84c8e88717SBabu Moger 	else
85c8e88717SBabu Moger 		val -= modifier;
86c8e88717SBabu Moger 
87c8e88717SBabu Moger 	return max(val, min);
88c8e88717SBabu Moger }
89c8e88717SBabu Moger 
9074545705SRadim Krčmář #define MSR_IA32_CR_PAT_DEFAULT  0x0007040600070406ULL
9174545705SRadim Krčmář 
9240e5f908SSean Christopherson void kvm_service_local_tlb_flush_requests(struct kvm_vcpu *vcpu);
93cb6a32c2SSean Christopherson int kvm_check_nested_events(struct kvm_vcpu *vcpu);
94cb6a32c2SSean Christopherson 
kvm_vcpu_has_run(struct kvm_vcpu * vcpu)95fb3146b4SSean Christopherson static inline bool kvm_vcpu_has_run(struct kvm_vcpu *vcpu)
96fb3146b4SSean Christopherson {
97fb3146b4SSean Christopherson 	return vcpu->arch.last_vmentry_cpu != -1;
98fb3146b4SSean Christopherson }
99fb3146b4SSean Christopherson 
kvm_is_exception_pending(struct kvm_vcpu * vcpu)1007709aba8SSean Christopherson static inline bool kvm_is_exception_pending(struct kvm_vcpu *vcpu)
1017709aba8SSean Christopherson {
1027709aba8SSean Christopherson 	return vcpu->arch.exception.pending ||
1037055fb11SSean Christopherson 	       vcpu->arch.exception_vmexit.pending ||
1047055fb11SSean Christopherson 	       kvm_test_request(KVM_REQ_TRIPLE_FAULT, vcpu);
1057709aba8SSean Christopherson }
1067709aba8SSean Christopherson 
kvm_clear_exception_queue(struct kvm_vcpu * vcpu)10726eef70cSAvi Kivity static inline void kvm_clear_exception_queue(struct kvm_vcpu *vcpu)
10826eef70cSAvi Kivity {
1095c7d4f9aSLiran Alon 	vcpu->arch.exception.pending = false;
110664f8e26SWanpeng Li 	vcpu->arch.exception.injected = false;
1117709aba8SSean Christopherson 	vcpu->arch.exception_vmexit.pending = false;
11226eef70cSAvi Kivity }
11326eef70cSAvi Kivity 
kvm_queue_interrupt(struct kvm_vcpu * vcpu,u8 vector,bool soft)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 
kvm_clear_interrupt_queue(struct kvm_vcpu * vcpu)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 
kvm_event_needs_reinjection(struct kvm_vcpu * vcpu)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 
kvm_exception_is_soft(unsigned int nr)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 
is_protmode(struct kvm_vcpu * vcpu)1383eeb3288SAvi Kivity static inline bool is_protmode(struct kvm_vcpu *vcpu)
1393eeb3288SAvi Kivity {
140607475cfSBinbin Wu 	return kvm_is_cr0_bit_set(vcpu, X86_CR0_PE);
1413eeb3288SAvi Kivity }
1423eeb3288SAvi Kivity 
is_long_mode(struct kvm_vcpu * vcpu)14368f7c82aSBinbin Wu static inline bool is_long_mode(struct kvm_vcpu *vcpu)
144836a1b3cSAvi Kivity {
145836a1b3cSAvi Kivity #ifdef CONFIG_X86_64
14668f7c82aSBinbin Wu 	return !!(vcpu->arch.efer & EFER_LMA);
147836a1b3cSAvi Kivity #else
14868f7c82aSBinbin Wu 	return false;
149836a1b3cSAvi Kivity #endif
150836a1b3cSAvi Kivity }
151836a1b3cSAvi Kivity 
is_64_bit_mode(struct kvm_vcpu * vcpu)1525777392eSNadav Amit static inline bool is_64_bit_mode(struct kvm_vcpu *vcpu)
1535777392eSNadav Amit {
1545777392eSNadav Amit 	int cs_db, cs_l;
1555777392eSNadav Amit 
156b5aead00STom Lendacky 	WARN_ON_ONCE(vcpu->arch.guest_state_protected);
157b5aead00STom Lendacky 
1585777392eSNadav Amit 	if (!is_long_mode(vcpu))
1595777392eSNadav Amit 		return false;
160b3646477SJason Baron 	static_call(kvm_x86_get_cs_db_l_bits)(vcpu, &cs_db, &cs_l);
1615777392eSNadav Amit 	return cs_l;
1625777392eSNadav Amit }
1635777392eSNadav Amit 
is_64_bit_hypercall(struct kvm_vcpu * vcpu)164b5aead00STom Lendacky static inline bool is_64_bit_hypercall(struct kvm_vcpu *vcpu)
165b5aead00STom Lendacky {
166b5aead00STom Lendacky 	/*
167b5aead00STom Lendacky 	 * If running with protected guest state, the CS register is not
168b5aead00STom Lendacky 	 * accessible. The hypercall register values will have had to been
169b5aead00STom Lendacky 	 * provided in 64-bit mode, so assume the guest is in 64-bit.
170b5aead00STom Lendacky 	 */
171b5aead00STom Lendacky 	return vcpu->arch.guest_state_protected || is_64_bit_mode(vcpu);
172b5aead00STom Lendacky }
173b5aead00STom Lendacky 
x86_exception_has_error_code(unsigned int vector)1740447378aSMarc Orr static inline bool x86_exception_has_error_code(unsigned int vector)
1750447378aSMarc Orr {
1760447378aSMarc Orr 	static u32 exception_has_error_code = BIT(DF_VECTOR) | BIT(TS_VECTOR) |
1770447378aSMarc Orr 			BIT(NP_VECTOR) | BIT(SS_VECTOR) | BIT(GP_VECTOR) |
1780447378aSMarc Orr 			BIT(PF_VECTOR) | BIT(AC_VECTOR);
1790447378aSMarc Orr 
1800447378aSMarc Orr 	return (1U << vector) & exception_has_error_code;
1810447378aSMarc Orr }
1820447378aSMarc Orr 
mmu_is_nested(struct kvm_vcpu * vcpu)1836539e738SJoerg Roedel static inline bool mmu_is_nested(struct kvm_vcpu *vcpu)
1846539e738SJoerg Roedel {
1856539e738SJoerg Roedel 	return vcpu->arch.walk_mmu == &vcpu->arch.nested_mmu;
1866539e738SJoerg Roedel }
1876539e738SJoerg Roedel 
is_pae(struct kvm_vcpu * vcpu)188bede6eb4SBinbin Wu static inline bool is_pae(struct kvm_vcpu *vcpu)
189836a1b3cSAvi Kivity {
190bede6eb4SBinbin Wu 	return kvm_is_cr4_bit_set(vcpu, X86_CR4_PAE);
191836a1b3cSAvi Kivity }
192836a1b3cSAvi Kivity 
is_pse(struct kvm_vcpu * vcpu)193bede6eb4SBinbin Wu static inline bool is_pse(struct kvm_vcpu *vcpu)
194836a1b3cSAvi Kivity {
195bede6eb4SBinbin Wu 	return kvm_is_cr4_bit_set(vcpu, X86_CR4_PSE);
196836a1b3cSAvi Kivity }
197836a1b3cSAvi Kivity 
is_paging(struct kvm_vcpu * vcpu)198bede6eb4SBinbin Wu static inline bool is_paging(struct kvm_vcpu *vcpu)
199836a1b3cSAvi Kivity {
200bede6eb4SBinbin Wu 	return likely(kvm_is_cr0_bit_set(vcpu, X86_CR0_PG));
201836a1b3cSAvi Kivity }
202836a1b3cSAvi Kivity 
is_pae_paging(struct kvm_vcpu * vcpu)203bf03d4f9SPaolo Bonzini static inline bool is_pae_paging(struct kvm_vcpu *vcpu)
204bf03d4f9SPaolo Bonzini {
205bf03d4f9SPaolo Bonzini 	return !is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu);
206bf03d4f9SPaolo Bonzini }
207bf03d4f9SPaolo Bonzini 
vcpu_virt_addr_bits(struct kvm_vcpu * vcpu)208fd8cb433SYu Zhang static inline u8 vcpu_virt_addr_bits(struct kvm_vcpu *vcpu)
209fd8cb433SYu Zhang {
210607475cfSBinbin Wu 	return kvm_is_cr4_bit_set(vcpu, X86_CR4_LA57) ? 57 : 48;
211fd8cb433SYu Zhang }
212fd8cb433SYu Zhang 
is_noncanonical_address(u64 la,struct kvm_vcpu * vcpu)213fd8cb433SYu Zhang static inline bool is_noncanonical_address(u64 la, struct kvm_vcpu *vcpu)
214fd8cb433SYu Zhang {
2151fb85d06SAdrian Hunter 	return !__is_canonical_address(la, vcpu_virt_addr_bits(vcpu));
216fd8cb433SYu Zhang }
217fd8cb433SYu Zhang 
vcpu_cache_mmio_info(struct kvm_vcpu * vcpu,gva_t gva,gfn_t gfn,unsigned access)218bebb106aSXiao Guangrong static inline void vcpu_cache_mmio_info(struct kvm_vcpu *vcpu,
219bebb106aSXiao Guangrong 					gva_t gva, gfn_t gfn, unsigned access)
220bebb106aSXiao Guangrong {
221ddfd1730SSean Christopherson 	u64 gen = kvm_memslots(vcpu->kvm)->generation;
222ddfd1730SSean Christopherson 
223361209e0SSean Christopherson 	if (unlikely(gen & KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS))
224ddfd1730SSean Christopherson 		return;
225ddfd1730SSean Christopherson 
2269034e6e8SPaolo Bonzini 	/*
2279034e6e8SPaolo Bonzini 	 * If this is a shadow nested page table, the "GVA" is
2289034e6e8SPaolo Bonzini 	 * actually a nGPA.
2299034e6e8SPaolo Bonzini 	 */
2309034e6e8SPaolo Bonzini 	vcpu->arch.mmio_gva = mmu_is_nested(vcpu) ? 0 : gva & PAGE_MASK;
231871bd034SSean Christopherson 	vcpu->arch.mmio_access = access;
232bebb106aSXiao Guangrong 	vcpu->arch.mmio_gfn = gfn;
233ddfd1730SSean Christopherson 	vcpu->arch.mmio_gen = gen;
23456f17dd3SDavid Matlack }
23556f17dd3SDavid Matlack 
vcpu_match_mmio_gen(struct kvm_vcpu * vcpu)23656f17dd3SDavid Matlack static inline bool vcpu_match_mmio_gen(struct kvm_vcpu *vcpu)
23756f17dd3SDavid Matlack {
23856f17dd3SDavid Matlack 	return vcpu->arch.mmio_gen == kvm_memslots(vcpu->kvm)->generation;
239bebb106aSXiao Guangrong }
240bebb106aSXiao Guangrong 
241bebb106aSXiao Guangrong /*
24256f17dd3SDavid Matlack  * Clear the mmio cache info for the given gva. If gva is MMIO_GVA_ANY, we
24356f17dd3SDavid Matlack  * clear all mmio cache info.
244bebb106aSXiao Guangrong  */
24556f17dd3SDavid Matlack #define MMIO_GVA_ANY (~(gva_t)0)
24656f17dd3SDavid Matlack 
vcpu_clear_mmio_info(struct kvm_vcpu * vcpu,gva_t gva)247bebb106aSXiao Guangrong static inline void vcpu_clear_mmio_info(struct kvm_vcpu *vcpu, gva_t gva)
248bebb106aSXiao Guangrong {
24956f17dd3SDavid Matlack 	if (gva != MMIO_GVA_ANY && vcpu->arch.mmio_gva != (gva & PAGE_MASK))
250bebb106aSXiao Guangrong 		return;
251bebb106aSXiao Guangrong 
252bebb106aSXiao Guangrong 	vcpu->arch.mmio_gva = 0;
253bebb106aSXiao Guangrong }
254bebb106aSXiao Guangrong 
vcpu_match_mmio_gva(struct kvm_vcpu * vcpu,unsigned long gva)255bebb106aSXiao Guangrong static inline bool vcpu_match_mmio_gva(struct kvm_vcpu *vcpu, unsigned long gva)
256bebb106aSXiao Guangrong {
25756f17dd3SDavid Matlack 	if (vcpu_match_mmio_gen(vcpu) && vcpu->arch.mmio_gva &&
25856f17dd3SDavid Matlack 	      vcpu->arch.mmio_gva == (gva & PAGE_MASK))
259bebb106aSXiao Guangrong 		return true;
260bebb106aSXiao Guangrong 
261bebb106aSXiao Guangrong 	return false;
262bebb106aSXiao Guangrong }
263bebb106aSXiao Guangrong 
vcpu_match_mmio_gpa(struct kvm_vcpu * vcpu,gpa_t gpa)264bebb106aSXiao Guangrong static inline bool vcpu_match_mmio_gpa(struct kvm_vcpu *vcpu, gpa_t gpa)
265bebb106aSXiao Guangrong {
26656f17dd3SDavid Matlack 	if (vcpu_match_mmio_gen(vcpu) && vcpu->arch.mmio_gfn &&
26756f17dd3SDavid Matlack 	      vcpu->arch.mmio_gfn == gpa >> PAGE_SHIFT)
268bebb106aSXiao Guangrong 		return true;
269bebb106aSXiao Guangrong 
270bebb106aSXiao Guangrong 	return false;
271bebb106aSXiao Guangrong }
272bebb106aSXiao Guangrong 
kvm_register_read(struct kvm_vcpu * vcpu,int reg)27327b4a9c4SSean Christopherson static inline unsigned long kvm_register_read(struct kvm_vcpu *vcpu, int reg)
2745777392eSNadav Amit {
27527b4a9c4SSean Christopherson 	unsigned long val = kvm_register_read_raw(vcpu, reg);
2765777392eSNadav Amit 
2775777392eSNadav Amit 	return is_64_bit_mode(vcpu) ? val : (u32)val;
2785777392eSNadav Amit }
2795777392eSNadav Amit 
kvm_register_write(struct kvm_vcpu * vcpu,int reg,unsigned long val)28027b4a9c4SSean Christopherson static inline void kvm_register_write(struct kvm_vcpu *vcpu,
281489cbcf0SSean Christopherson 				       int reg, unsigned long val)
28227e6fb5dSNadav Amit {
28327e6fb5dSNadav Amit 	if (!is_64_bit_mode(vcpu))
28427e6fb5dSNadav Amit 		val = (u32)val;
28527b4a9c4SSean Christopherson 	return kvm_register_write_raw(vcpu, reg, val);
28627e6fb5dSNadav Amit }
28727e6fb5dSNadav Amit 
kvm_check_has_quirk(struct kvm * kvm,u64 quirk)28841dbc6bcSPaolo Bonzini static inline bool kvm_check_has_quirk(struct kvm *kvm, u64 quirk)
28941dbc6bcSPaolo Bonzini {
29041dbc6bcSPaolo Bonzini 	return !(kvm->arch.disabled_quirks & quirk);
29141dbc6bcSPaolo Bonzini }
29241dbc6bcSPaolo Bonzini 
2939497e1f2SSean Christopherson void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip);
294ff9d07a0SZhang, Yanmin 
295108b249cSPaolo Bonzini u64 get_kvmclock_ns(struct kvm *kvm);
29699e3e30aSZachary Amsden 
297ce14e868SPaolo Bonzini int kvm_read_guest_virt(struct kvm_vcpu *vcpu,
298064aea77SNadav Har'El 	gva_t addr, void *val, unsigned int bytes,
299064aea77SNadav Har'El 	struct x86_exception *exception);
300064aea77SNadav Har'El 
301ce14e868SPaolo Bonzini int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu,
3026a4d7550SNadav Har'El 	gva_t addr, void *val, unsigned int bytes,
3036a4d7550SNadav Har'El 	struct x86_exception *exception);
3046a4d7550SNadav Har'El 
305082d06edSWanpeng Li int handle_ud(struct kvm_vcpu *vcpu);
306082d06edSWanpeng Li 
307d4963e31SSean Christopherson void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu,
308d4963e31SSean Christopherson 				   struct kvm_queued_exception *ex);
309da998b46SJim Mattson 
31019efffa2SXiao Guangrong void kvm_vcpu_mtrr_init(struct kvm_vcpu *vcpu);
311ff53604bSXiao Guangrong u8 kvm_mtrr_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn);
312ff53604bSXiao Guangrong int kvm_mtrr_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data);
313ff53604bSXiao Guangrong int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata);
3146a39bbc5SXiao Guangrong bool kvm_mtrr_check_gfn_range_consistency(struct kvm_vcpu *vcpu, gfn_t gfn,
3156a39bbc5SXiao Guangrong 					  int page_num);
31652004014SFeng Wu bool kvm_vector_hashing_enabled(void);
31789786147SMohammed Gamal void kvm_fixup_and_inject_pf_error(struct kvm_vcpu *vcpu, gva_t gva, u16 error_code);
3184aa2691dSWei Huang int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type,
3194aa2691dSWei Huang 				    void *insn, int insn_len);
320736c291cSSean Christopherson int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
321c60658d1SSean Christopherson 			    int emulation_type, void *insn, int insn_len);
322404d5d7bSWanpeng Li fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu);
3234566654bSNadav Amit 
32400b27a3eSAvi Kivity extern u64 host_xcr0;
32586137773STom Lendacky extern u64 host_xss;
326*a2fd5d02SSean Christopherson extern u64 host_arch_capabilities;
327938c8745SSean Christopherson 
328938c8745SSean Christopherson extern struct kvm_caps kvm_caps;
329938c8745SSean Christopherson 
3304732f244SLike Xu extern bool enable_pmu;
3314ff41732SPaolo Bonzini 
3326be3ae45SAaron Lewis /*
3336be3ae45SAaron Lewis  * Get a filtered version of KVM's supported XCR0 that strips out dynamic
3346be3ae45SAaron Lewis  * features for which the current process doesn't (yet) have permission to use.
3356be3ae45SAaron Lewis  * This is intended to be used only when enumerating support to userspace,
3366be3ae45SAaron Lewis  * e.g. in KVM_GET_SUPPORTED_CPUID and KVM_CAP_XSAVE2, it does NOT need to be
3376be3ae45SAaron Lewis  * used to check/restrict guest behavior as KVM rejects KVM_SET_CPUID{2} if
3386be3ae45SAaron Lewis  * userspace attempts to enable unpermitted features.
3396be3ae45SAaron Lewis  */
kvm_get_filtered_xcr0(void)3406be3ae45SAaron Lewis static inline u64 kvm_get_filtered_xcr0(void)
3416be3ae45SAaron Lewis {
34255cd57b5SSean Christopherson 	u64 permitted_xcr0 = kvm_caps.supported_xcr0;
34355cd57b5SSean Christopherson 
34455cd57b5SSean Christopherson 	BUILD_BUG_ON(XFEATURE_MASK_USER_DYNAMIC != XFEATURE_MASK_XTILE_DATA);
34555cd57b5SSean Christopherson 
34655cd57b5SSean Christopherson 	if (permitted_xcr0 & XFEATURE_MASK_USER_DYNAMIC) {
34755cd57b5SSean Christopherson 		permitted_xcr0 &= xstate_get_guest_group_perm();
34855cd57b5SSean Christopherson 
34955cd57b5SSean Christopherson 		/*
35055cd57b5SSean Christopherson 		 * Treat XTILE_CFG as unsupported if the current process isn't
35155cd57b5SSean Christopherson 		 * allowed to use XTILE_DATA, as attempting to set XTILE_CFG in
35255cd57b5SSean Christopherson 		 * XCR0 without setting XTILE_DATA is architecturally illegal.
35355cd57b5SSean Christopherson 		 */
35455cd57b5SSean Christopherson 		if (!(permitted_xcr0 & XFEATURE_MASK_XTILE_DATA))
35555cd57b5SSean Christopherson 			permitted_xcr0 &= ~XFEATURE_MASK_XTILE_CFG;
35655cd57b5SSean Christopherson 	}
35755cd57b5SSean Christopherson 	return permitted_xcr0;
3586be3ae45SAaron Lewis }
3596be3ae45SAaron Lewis 
kvm_mpx_supported(void)360615a4ae1SSean Christopherson static inline bool kvm_mpx_supported(void)
361615a4ae1SSean Christopherson {
362938c8745SSean Christopherson 	return (kvm_caps.supported_xcr0 & (XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR))
363615a4ae1SSean Christopherson 		== (XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR);
364615a4ae1SSean Christopherson }
365615a4ae1SSean Christopherson 
3669ed96e87SMarcelo Tosatti extern unsigned int min_timer_period_us;
3679ed96e87SMarcelo Tosatti 
368c4ae60e4SLiran Alon extern bool enable_vmware_backdoor;
369c4ae60e4SLiran Alon 
3700c5f81daSWanpeng Li extern int pi_inject_timer;
3710c5f81daSWanpeng Li 
372d855066fSLike Xu extern bool report_ignored_msrs;
373d855066fSLike Xu 
374cb00a70bSDavid Matlack extern bool eager_page_split;
375cb00a70bSDavid Matlack 
kvm_pr_unimpl_wrmsr(struct kvm_vcpu * vcpu,u32 msr,u64 data)376e76ae527SSean Christopherson static inline void kvm_pr_unimpl_wrmsr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
377e76ae527SSean Christopherson {
378e76ae527SSean Christopherson 	if (report_ignored_msrs)
379e76ae527SSean Christopherson 		vcpu_unimpl(vcpu, "Unhandled WRMSR(0x%x) = 0x%llx\n", msr, data);
380e76ae527SSean Christopherson }
381e76ae527SSean Christopherson 
kvm_pr_unimpl_rdmsr(struct kvm_vcpu * vcpu,u32 msr)382e76ae527SSean Christopherson static inline void kvm_pr_unimpl_rdmsr(struct kvm_vcpu *vcpu, u32 msr)
383e76ae527SSean Christopherson {
384e76ae527SSean Christopherson 	if (report_ignored_msrs)
385e76ae527SSean Christopherson 		vcpu_unimpl(vcpu, "Unhandled RDMSR(0x%x)\n", msr);
386e76ae527SSean Christopherson }
387e76ae527SSean Christopherson 
nsec_to_cycles(struct kvm_vcpu * vcpu,u64 nsec)3888d93c874SMarcelo Tosatti static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec)
3898d93c874SMarcelo Tosatti {
3908d93c874SMarcelo Tosatti 	return pvclock_scale_delta(nsec, vcpu->arch.virtual_tsc_mult,
3918d93c874SMarcelo Tosatti 				   vcpu->arch.virtual_tsc_shift);
3928d93c874SMarcelo Tosatti }
3938d93c874SMarcelo Tosatti 
394b51012deSPaolo Bonzini /* Same "calling convention" as do_div:
395b51012deSPaolo Bonzini  * - divide (n << 32) by base
396b51012deSPaolo Bonzini  * - put result in n
397b51012deSPaolo Bonzini  * - return remainder
398b51012deSPaolo Bonzini  */
399b51012deSPaolo Bonzini #define do_shl32_div32(n, base)					\
400b51012deSPaolo Bonzini 	({							\
401b51012deSPaolo Bonzini 	    u32 __quot, __rem;					\
402b51012deSPaolo Bonzini 	    asm("divl %2" : "=a" (__quot), "=d" (__rem)		\
403b51012deSPaolo Bonzini 			: "rm" (base), "0" (0), "1" ((u32) n));	\
404b51012deSPaolo Bonzini 	    n = __quot;						\
405b51012deSPaolo Bonzini 	    __rem;						\
406b51012deSPaolo Bonzini 	 })
407b51012deSPaolo Bonzini 
kvm_mwait_in_guest(struct kvm * kvm)4084d5422ceSWanpeng Li static inline bool kvm_mwait_in_guest(struct kvm *kvm)
409668fffa3SMichael S. Tsirkin {
4104d5422ceSWanpeng Li 	return kvm->arch.mwait_in_guest;
411668fffa3SMichael S. Tsirkin }
412668fffa3SMichael S. Tsirkin 
kvm_hlt_in_guest(struct kvm * kvm)413caa057a2SWanpeng Li static inline bool kvm_hlt_in_guest(struct kvm *kvm)
414caa057a2SWanpeng Li {
415caa057a2SWanpeng Li 	return kvm->arch.hlt_in_guest;
416caa057a2SWanpeng Li }
417caa057a2SWanpeng Li 
kvm_pause_in_guest(struct kvm * kvm)418b31c114bSWanpeng Li static inline bool kvm_pause_in_guest(struct kvm *kvm)
419b31c114bSWanpeng Li {
420b31c114bSWanpeng Li 	return kvm->arch.pause_in_guest;
421b31c114bSWanpeng Li }
422b31c114bSWanpeng Li 
kvm_cstate_in_guest(struct kvm * kvm)423b5170063SWanpeng Li static inline bool kvm_cstate_in_guest(struct kvm *kvm)
424b5170063SWanpeng Li {
425b5170063SWanpeng Li 	return kvm->arch.cstate_in_guest;
426b5170063SWanpeng Li }
427b5170063SWanpeng Li 
kvm_notify_vmexit_enabled(struct kvm * kvm)4282f4073e0STao Xu static inline bool kvm_notify_vmexit_enabled(struct kvm *kvm)
4292f4073e0STao Xu {
4302f4073e0STao Xu 	return kvm->arch.notify_vmexit_flags & KVM_X86_NOTIFY_VMEXIT_ENABLED;
4312f4073e0STao Xu }
4322f4073e0STao Xu 
433db215756SSean Christopherson enum kvm_intr_type {
434db215756SSean Christopherson 	/* Values are arbitrary, but must be non-zero. */
435db215756SSean Christopherson 	KVM_HANDLING_IRQ = 1,
436db215756SSean Christopherson 	KVM_HANDLING_NMI,
437db215756SSean Christopherson };
438dd60d217SAndi Kleen 
kvm_before_interrupt(struct kvm_vcpu * vcpu,enum kvm_intr_type intr)43911df586dSSean Christopherson static __always_inline void kvm_before_interrupt(struct kvm_vcpu *vcpu,
440db215756SSean Christopherson 						 enum kvm_intr_type intr)
441dd60d217SAndi Kleen {
442db215756SSean Christopherson 	WRITE_ONCE(vcpu->arch.handling_intr_from_guest, (u8)intr);
443dd60d217SAndi Kleen }
444dd60d217SAndi Kleen 
kvm_after_interrupt(struct kvm_vcpu * vcpu)44511df586dSSean Christopherson static __always_inline void kvm_after_interrupt(struct kvm_vcpu *vcpu)
446dd60d217SAndi Kleen {
44773cd107bSSean Christopherson 	WRITE_ONCE(vcpu->arch.handling_intr_from_guest, 0);
448dd60d217SAndi Kleen }
449dd60d217SAndi Kleen 
kvm_handling_nmi_from_guest(struct kvm_vcpu * vcpu)45073cd107bSSean Christopherson static inline bool kvm_handling_nmi_from_guest(struct kvm_vcpu *vcpu)
45173cd107bSSean Christopherson {
452db215756SSean Christopherson 	return vcpu->arch.handling_intr_from_guest == KVM_HANDLING_NMI;
45373cd107bSSean Christopherson }
454674ea351SPaolo Bonzini 
kvm_pat_valid(u64 data)455674ea351SPaolo Bonzini static inline bool kvm_pat_valid(u64 data)
456674ea351SPaolo Bonzini {
457674ea351SPaolo Bonzini 	if (data & 0xF8F8F8F8F8F8F8F8ull)
458674ea351SPaolo Bonzini 		return false;
459674ea351SPaolo Bonzini 	/* 0, 1, 4, 5, 6, 7 are valid values.  */
460674ea351SPaolo Bonzini 	return (data | ((data & 0x0202020202020202ull) << 1)) == data;
461674ea351SPaolo Bonzini }
462674ea351SPaolo Bonzini 
kvm_dr7_valid(u64 data)4639b5e8532SSean Christopherson static inline bool kvm_dr7_valid(u64 data)
464b91991bfSKrish Sadhukhan {
465b91991bfSKrish Sadhukhan 	/* Bits [63:32] are reserved */
466b91991bfSKrish Sadhukhan 	return !(data >> 32);
467b91991bfSKrish Sadhukhan }
kvm_dr6_valid(u64 data)468f5f6145eSKrish Sadhukhan static inline bool kvm_dr6_valid(u64 data)
469f5f6145eSKrish Sadhukhan {
470f5f6145eSKrish Sadhukhan 	/* Bits [63:32] are reserved */
471f5f6145eSKrish Sadhukhan 	return !(data >> 32);
472f5f6145eSKrish Sadhukhan }
473b91991bfSKrish Sadhukhan 
4743f1a18b9SUros Bizjak /*
4753f1a18b9SUros Bizjak  * Trigger machine check on the host. We assume all the MSRs are already set up
4763f1a18b9SUros Bizjak  * by the CPU and that we still run on the same CPU as the MCE occurred on.
4773f1a18b9SUros Bizjak  * We pass a fake environment to the machine check handler because we want
4783f1a18b9SUros Bizjak  * the guest to be always treated like user space, no matter what context
4793f1a18b9SUros Bizjak  * it used internally.
4803f1a18b9SUros Bizjak  */
kvm_machine_check(void)4813f1a18b9SUros Bizjak static inline void kvm_machine_check(void)
4823f1a18b9SUros Bizjak {
4833f1a18b9SUros Bizjak #if defined(CONFIG_X86_MCE)
4843f1a18b9SUros Bizjak 	struct pt_regs regs = {
4853f1a18b9SUros Bizjak 		.cs = 3, /* Fake ring 3 no matter what the guest ran on */
4863f1a18b9SUros Bizjak 		.flags = X86_EFLAGS_IF,
4873f1a18b9SUros Bizjak 	};
4883f1a18b9SUros Bizjak 
4893f1a18b9SUros Bizjak 	do_machine_check(&regs);
4903f1a18b9SUros Bizjak #endif
4913f1a18b9SUros Bizjak }
4923f1a18b9SUros Bizjak 
493139a12cfSAaron Lewis void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu);
494139a12cfSAaron Lewis void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu);
495841c2be0SMaxim Levitsky int kvm_spec_ctrl_test_value(u64 value);
496c33f6f22SSean Christopherson bool __kvm_is_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
4973f3393b3SBabu Moger int kvm_handle_memory_failure(struct kvm_vcpu *vcpu, int r,
4983f3393b3SBabu Moger 			      struct x86_exception *e);
4999715092fSBabu Moger int kvm_handle_invpcid(struct kvm_vcpu *vcpu, unsigned long type, gva_t gva);
50051de8151SAlexander Graf bool kvm_msr_allowed(struct kvm_vcpu *vcpu, u32 index, u32 type);
501674ea351SPaolo Bonzini 
502cc4cb017SMaxim Levitsky /*
503cc4cb017SMaxim Levitsky  * Internal error codes that are used to indicate that MSR emulation encountered
504cc4cb017SMaxim Levitsky  * an error that should result in #GP in the guest, unless userspace
505cc4cb017SMaxim Levitsky  * handles it.
506cc4cb017SMaxim Levitsky  */
507cc4cb017SMaxim Levitsky #define  KVM_MSR_RET_INVALID	2	/* in-kernel MSR emulation #GP condition */
508cc4cb017SMaxim Levitsky #define  KVM_MSR_RET_FILTERED	3	/* #GP due to userspace MSR filter */
5096abe9c13SPeter Xu 
510b899c132SKrish Sadhukhan #define __cr4_reserved_bits(__cpu_has, __c)             \
511b899c132SKrish Sadhukhan ({                                                      \
512b899c132SKrish Sadhukhan 	u64 __reserved_bits = CR4_RESERVED_BITS;        \
513b899c132SKrish Sadhukhan                                                         \
514b899c132SKrish Sadhukhan 	if (!__cpu_has(__c, X86_FEATURE_XSAVE))         \
515b899c132SKrish Sadhukhan 		__reserved_bits |= X86_CR4_OSXSAVE;     \
516b899c132SKrish Sadhukhan 	if (!__cpu_has(__c, X86_FEATURE_SMEP))          \
517b899c132SKrish Sadhukhan 		__reserved_bits |= X86_CR4_SMEP;        \
518b899c132SKrish Sadhukhan 	if (!__cpu_has(__c, X86_FEATURE_SMAP))          \
519b899c132SKrish Sadhukhan 		__reserved_bits |= X86_CR4_SMAP;        \
520b899c132SKrish Sadhukhan 	if (!__cpu_has(__c, X86_FEATURE_FSGSBASE))      \
521b899c132SKrish Sadhukhan 		__reserved_bits |= X86_CR4_FSGSBASE;    \
522b899c132SKrish Sadhukhan 	if (!__cpu_has(__c, X86_FEATURE_PKU))           \
523b899c132SKrish Sadhukhan 		__reserved_bits |= X86_CR4_PKE;         \
524b899c132SKrish Sadhukhan 	if (!__cpu_has(__c, X86_FEATURE_LA57))          \
525b899c132SKrish Sadhukhan 		__reserved_bits |= X86_CR4_LA57;        \
526b899c132SKrish Sadhukhan 	if (!__cpu_has(__c, X86_FEATURE_UMIP))          \
527b899c132SKrish Sadhukhan 		__reserved_bits |= X86_CR4_UMIP;        \
52853efe527SPaolo Bonzini 	if (!__cpu_has(__c, X86_FEATURE_VMX))           \
52953efe527SPaolo Bonzini 		__reserved_bits |= X86_CR4_VMXE;        \
5304683d758SVitaly Kuznetsov 	if (!__cpu_has(__c, X86_FEATURE_PCID))          \
5314683d758SVitaly Kuznetsov 		__reserved_bits |= X86_CR4_PCIDE;       \
532b899c132SKrish Sadhukhan 	__reserved_bits;                                \
533b899c132SKrish Sadhukhan })
534b899c132SKrish Sadhukhan 
5358f423a80STom Lendacky int kvm_sev_es_mmio_write(struct kvm_vcpu *vcpu, gpa_t src, unsigned int bytes,
5368f423a80STom Lendacky 			  void *dst);
5378f423a80STom Lendacky int kvm_sev_es_mmio_read(struct kvm_vcpu *vcpu, gpa_t src, unsigned int bytes,
5388f423a80STom Lendacky 			 void *dst);
5397ed9abfeSTom Lendacky int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size,
5407ed9abfeSTom Lendacky 			 unsigned int port, void *data,  unsigned int count,
5417ed9abfeSTom Lendacky 			 int in);
5428f423a80STom Lendacky 
54326eef70cSAvi Kivity #endif
544