xref: /linux/arch/x86/kvm/x86.h (revision 0447378a4a793da008451fad50bc0f93e9675ae6)
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>
68d93c874SMarcelo Tosatti #include <asm/pvclock.h>
73eeb3288SAvi Kivity #include "kvm_cache_regs.h"
826eef70cSAvi Kivity 
9c8e88717SBabu Moger #define KVM_DEFAULT_PLE_GAP		128
10c8e88717SBabu Moger #define KVM_VMX_DEFAULT_PLE_WINDOW	4096
11c8e88717SBabu Moger #define KVM_DEFAULT_PLE_WINDOW_GROW	2
12c8e88717SBabu Moger #define KVM_DEFAULT_PLE_WINDOW_SHRINK	0
13c8e88717SBabu Moger #define KVM_VMX_DEFAULT_PLE_WINDOW_MAX	UINT_MAX
148566ac8bSBabu Moger #define KVM_SVM_DEFAULT_PLE_WINDOW_MAX	USHRT_MAX
158566ac8bSBabu Moger #define KVM_SVM_DEFAULT_PLE_WINDOW	3000
16c8e88717SBabu Moger 
17c8e88717SBabu Moger static inline unsigned int __grow_ple_window(unsigned int val,
18c8e88717SBabu Moger 		unsigned int base, unsigned int modifier, unsigned int max)
19c8e88717SBabu Moger {
20c8e88717SBabu Moger 	u64 ret = val;
21c8e88717SBabu Moger 
22c8e88717SBabu Moger 	if (modifier < 1)
23c8e88717SBabu Moger 		return base;
24c8e88717SBabu Moger 
25c8e88717SBabu Moger 	if (modifier < base)
26c8e88717SBabu Moger 		ret *= modifier;
27c8e88717SBabu Moger 	else
28c8e88717SBabu Moger 		ret += modifier;
29c8e88717SBabu Moger 
30c8e88717SBabu Moger 	return min(ret, (u64)max);
31c8e88717SBabu Moger }
32c8e88717SBabu Moger 
33c8e88717SBabu Moger static inline unsigned int __shrink_ple_window(unsigned int val,
34c8e88717SBabu Moger 		unsigned int base, unsigned int modifier, unsigned int min)
35c8e88717SBabu Moger {
36c8e88717SBabu Moger 	if (modifier < 1)
37c8e88717SBabu Moger 		return base;
38c8e88717SBabu Moger 
39c8e88717SBabu Moger 	if (modifier < base)
40c8e88717SBabu Moger 		val /= modifier;
41c8e88717SBabu Moger 	else
42c8e88717SBabu Moger 		val -= modifier;
43c8e88717SBabu Moger 
44c8e88717SBabu Moger 	return max(val, min);
45c8e88717SBabu Moger }
46c8e88717SBabu Moger 
4774545705SRadim Krčmář #define MSR_IA32_CR_PAT_DEFAULT  0x0007040600070406ULL
4874545705SRadim Krčmář 
4926eef70cSAvi Kivity static inline void kvm_clear_exception_queue(struct kvm_vcpu *vcpu)
5026eef70cSAvi Kivity {
515c7d4f9aSLiran Alon 	vcpu->arch.exception.pending = false;
52664f8e26SWanpeng Li 	vcpu->arch.exception.injected = false;
5326eef70cSAvi Kivity }
5426eef70cSAvi Kivity 
5566fd3f7fSGleb Natapov static inline void kvm_queue_interrupt(struct kvm_vcpu *vcpu, u8 vector,
5666fd3f7fSGleb Natapov 	bool soft)
57937a7eaeSAvi Kivity {
5804140b41SLiran Alon 	vcpu->arch.interrupt.injected = true;
5966fd3f7fSGleb Natapov 	vcpu->arch.interrupt.soft = soft;
60937a7eaeSAvi Kivity 	vcpu->arch.interrupt.nr = vector;
61937a7eaeSAvi Kivity }
62937a7eaeSAvi Kivity 
63937a7eaeSAvi Kivity static inline void kvm_clear_interrupt_queue(struct kvm_vcpu *vcpu)
64937a7eaeSAvi Kivity {
6504140b41SLiran Alon 	vcpu->arch.interrupt.injected = false;
66937a7eaeSAvi Kivity }
67937a7eaeSAvi Kivity 
683298b75cSGleb Natapov static inline bool kvm_event_needs_reinjection(struct kvm_vcpu *vcpu)
693298b75cSGleb Natapov {
7004140b41SLiran Alon 	return vcpu->arch.exception.injected || vcpu->arch.interrupt.injected ||
713298b75cSGleb Natapov 		vcpu->arch.nmi_injected;
723298b75cSGleb Natapov }
7366fd3f7fSGleb Natapov 
7466fd3f7fSGleb Natapov static inline bool kvm_exception_is_soft(unsigned int nr)
7566fd3f7fSGleb Natapov {
7666fd3f7fSGleb Natapov 	return (nr == BP_VECTOR) || (nr == OF_VECTOR);
7766fd3f7fSGleb Natapov }
78fc61b800SGleb Natapov 
793eeb3288SAvi Kivity static inline bool is_protmode(struct kvm_vcpu *vcpu)
803eeb3288SAvi Kivity {
813eeb3288SAvi Kivity 	return kvm_read_cr0_bits(vcpu, X86_CR0_PE);
823eeb3288SAvi Kivity }
833eeb3288SAvi Kivity 
84836a1b3cSAvi Kivity static inline int is_long_mode(struct kvm_vcpu *vcpu)
85836a1b3cSAvi Kivity {
86836a1b3cSAvi Kivity #ifdef CONFIG_X86_64
87f6801dffSAvi Kivity 	return vcpu->arch.efer & EFER_LMA;
88836a1b3cSAvi Kivity #else
89836a1b3cSAvi Kivity 	return 0;
90836a1b3cSAvi Kivity #endif
91836a1b3cSAvi Kivity }
92836a1b3cSAvi Kivity 
935777392eSNadav Amit static inline bool is_64_bit_mode(struct kvm_vcpu *vcpu)
945777392eSNadav Amit {
955777392eSNadav Amit 	int cs_db, cs_l;
965777392eSNadav Amit 
975777392eSNadav Amit 	if (!is_long_mode(vcpu))
985777392eSNadav Amit 		return false;
995777392eSNadav Amit 	kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1005777392eSNadav Amit 	return cs_l;
1015777392eSNadav Amit }
1025777392eSNadav Amit 
103855feb67SYu Zhang static inline bool is_la57_mode(struct kvm_vcpu *vcpu)
104855feb67SYu Zhang {
105855feb67SYu Zhang #ifdef CONFIG_X86_64
106855feb67SYu Zhang 	return (vcpu->arch.efer & EFER_LMA) &&
107855feb67SYu Zhang 		 kvm_read_cr4_bits(vcpu, X86_CR4_LA57);
108855feb67SYu Zhang #else
109855feb67SYu Zhang 	return 0;
110855feb67SYu Zhang #endif
111855feb67SYu Zhang }
112855feb67SYu Zhang 
113*0447378aSMarc Orr static inline bool x86_exception_has_error_code(unsigned int vector)
114*0447378aSMarc Orr {
115*0447378aSMarc Orr 	static u32 exception_has_error_code = BIT(DF_VECTOR) | BIT(TS_VECTOR) |
116*0447378aSMarc Orr 			BIT(NP_VECTOR) | BIT(SS_VECTOR) | BIT(GP_VECTOR) |
117*0447378aSMarc Orr 			BIT(PF_VECTOR) | BIT(AC_VECTOR);
118*0447378aSMarc Orr 
119*0447378aSMarc Orr 	return (1U << vector) & exception_has_error_code;
120*0447378aSMarc Orr }
121*0447378aSMarc Orr 
1226539e738SJoerg Roedel static inline bool mmu_is_nested(struct kvm_vcpu *vcpu)
1236539e738SJoerg Roedel {
1246539e738SJoerg Roedel 	return vcpu->arch.walk_mmu == &vcpu->arch.nested_mmu;
1256539e738SJoerg Roedel }
1266539e738SJoerg Roedel 
127836a1b3cSAvi Kivity static inline int is_pae(struct kvm_vcpu *vcpu)
128836a1b3cSAvi Kivity {
129836a1b3cSAvi Kivity 	return kvm_read_cr4_bits(vcpu, X86_CR4_PAE);
130836a1b3cSAvi Kivity }
131836a1b3cSAvi Kivity 
132836a1b3cSAvi Kivity static inline int is_pse(struct kvm_vcpu *vcpu)
133836a1b3cSAvi Kivity {
134836a1b3cSAvi Kivity 	return kvm_read_cr4_bits(vcpu, X86_CR4_PSE);
135836a1b3cSAvi Kivity }
136836a1b3cSAvi Kivity 
137836a1b3cSAvi Kivity static inline int is_paging(struct kvm_vcpu *vcpu)
138836a1b3cSAvi Kivity {
139c36fc04eSDavidlohr Bueso 	return likely(kvm_read_cr0_bits(vcpu, X86_CR0_PG));
140836a1b3cSAvi Kivity }
141836a1b3cSAvi Kivity 
14224d1b15fSJoerg Roedel static inline u32 bit(int bitno)
14324d1b15fSJoerg Roedel {
14424d1b15fSJoerg Roedel 	return 1 << (bitno & 31);
14524d1b15fSJoerg Roedel }
14624d1b15fSJoerg Roedel 
147fd8cb433SYu Zhang static inline u8 vcpu_virt_addr_bits(struct kvm_vcpu *vcpu)
148fd8cb433SYu Zhang {
149fd8cb433SYu Zhang 	return kvm_read_cr4_bits(vcpu, X86_CR4_LA57) ? 57 : 48;
150fd8cb433SYu Zhang }
151fd8cb433SYu Zhang 
152fd8cb433SYu Zhang static inline u8 ctxt_virt_addr_bits(struct x86_emulate_ctxt *ctxt)
153fd8cb433SYu Zhang {
154fd8cb433SYu Zhang 	return (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_LA57) ? 57 : 48;
155fd8cb433SYu Zhang }
156fd8cb433SYu Zhang 
157fd8cb433SYu Zhang static inline u64 get_canonical(u64 la, u8 vaddr_bits)
158fd8cb433SYu Zhang {
159fd8cb433SYu Zhang 	return ((int64_t)la << (64 - vaddr_bits)) >> (64 - vaddr_bits);
160fd8cb433SYu Zhang }
161fd8cb433SYu Zhang 
162fd8cb433SYu Zhang static inline bool is_noncanonical_address(u64 la, struct kvm_vcpu *vcpu)
163fd8cb433SYu Zhang {
164fd8cb433SYu Zhang #ifdef CONFIG_X86_64
165fd8cb433SYu Zhang 	return get_canonical(la, vcpu_virt_addr_bits(vcpu)) != la;
166fd8cb433SYu Zhang #else
167fd8cb433SYu Zhang 	return false;
168fd8cb433SYu Zhang #endif
169fd8cb433SYu Zhang }
170fd8cb433SYu Zhang 
171fd8cb433SYu Zhang static inline bool emul_is_noncanonical_address(u64 la,
172fd8cb433SYu Zhang 						struct x86_emulate_ctxt *ctxt)
173fd8cb433SYu Zhang {
174fd8cb433SYu Zhang #ifdef CONFIG_X86_64
175fd8cb433SYu Zhang 	return get_canonical(la, ctxt_virt_addr_bits(ctxt)) != la;
176fd8cb433SYu Zhang #else
177fd8cb433SYu Zhang 	return false;
178fd8cb433SYu Zhang #endif
179fd8cb433SYu Zhang }
180fd8cb433SYu Zhang 
181bebb106aSXiao Guangrong static inline void vcpu_cache_mmio_info(struct kvm_vcpu *vcpu,
182bebb106aSXiao Guangrong 					gva_t gva, gfn_t gfn, unsigned access)
183bebb106aSXiao Guangrong {
1849034e6e8SPaolo Bonzini 	/*
1859034e6e8SPaolo Bonzini 	 * If this is a shadow nested page table, the "GVA" is
1869034e6e8SPaolo Bonzini 	 * actually a nGPA.
1879034e6e8SPaolo Bonzini 	 */
1889034e6e8SPaolo Bonzini 	vcpu->arch.mmio_gva = mmu_is_nested(vcpu) ? 0 : gva & PAGE_MASK;
189bebb106aSXiao Guangrong 	vcpu->arch.access = access;
190bebb106aSXiao Guangrong 	vcpu->arch.mmio_gfn = gfn;
19156f17dd3SDavid Matlack 	vcpu->arch.mmio_gen = kvm_memslots(vcpu->kvm)->generation;
19256f17dd3SDavid Matlack }
19356f17dd3SDavid Matlack 
19456f17dd3SDavid Matlack static inline bool vcpu_match_mmio_gen(struct kvm_vcpu *vcpu)
19556f17dd3SDavid Matlack {
19656f17dd3SDavid Matlack 	return vcpu->arch.mmio_gen == kvm_memslots(vcpu->kvm)->generation;
197bebb106aSXiao Guangrong }
198bebb106aSXiao Guangrong 
199bebb106aSXiao Guangrong /*
20056f17dd3SDavid Matlack  * Clear the mmio cache info for the given gva. If gva is MMIO_GVA_ANY, we
20156f17dd3SDavid Matlack  * clear all mmio cache info.
202bebb106aSXiao Guangrong  */
20356f17dd3SDavid Matlack #define MMIO_GVA_ANY (~(gva_t)0)
20456f17dd3SDavid Matlack 
205bebb106aSXiao Guangrong static inline void vcpu_clear_mmio_info(struct kvm_vcpu *vcpu, gva_t gva)
206bebb106aSXiao Guangrong {
20756f17dd3SDavid Matlack 	if (gva != MMIO_GVA_ANY && vcpu->arch.mmio_gva != (gva & PAGE_MASK))
208bebb106aSXiao Guangrong 		return;
209bebb106aSXiao Guangrong 
210bebb106aSXiao Guangrong 	vcpu->arch.mmio_gva = 0;
211bebb106aSXiao Guangrong }
212bebb106aSXiao Guangrong 
213bebb106aSXiao Guangrong static inline bool vcpu_match_mmio_gva(struct kvm_vcpu *vcpu, unsigned long gva)
214bebb106aSXiao Guangrong {
21556f17dd3SDavid Matlack 	if (vcpu_match_mmio_gen(vcpu) && vcpu->arch.mmio_gva &&
21656f17dd3SDavid Matlack 	      vcpu->arch.mmio_gva == (gva & PAGE_MASK))
217bebb106aSXiao Guangrong 		return true;
218bebb106aSXiao Guangrong 
219bebb106aSXiao Guangrong 	return false;
220bebb106aSXiao Guangrong }
221bebb106aSXiao Guangrong 
222bebb106aSXiao Guangrong static inline bool vcpu_match_mmio_gpa(struct kvm_vcpu *vcpu, gpa_t gpa)
223bebb106aSXiao Guangrong {
22456f17dd3SDavid Matlack 	if (vcpu_match_mmio_gen(vcpu) && vcpu->arch.mmio_gfn &&
22556f17dd3SDavid Matlack 	      vcpu->arch.mmio_gfn == gpa >> PAGE_SHIFT)
226bebb106aSXiao Guangrong 		return true;
227bebb106aSXiao Guangrong 
228bebb106aSXiao Guangrong 	return false;
229bebb106aSXiao Guangrong }
230bebb106aSXiao Guangrong 
2315777392eSNadav Amit static inline unsigned long kvm_register_readl(struct kvm_vcpu *vcpu,
2325777392eSNadav Amit 					       enum kvm_reg reg)
2335777392eSNadav Amit {
2345777392eSNadav Amit 	unsigned long val = kvm_register_read(vcpu, reg);
2355777392eSNadav Amit 
2365777392eSNadav Amit 	return is_64_bit_mode(vcpu) ? val : (u32)val;
2375777392eSNadav Amit }
2385777392eSNadav Amit 
23927e6fb5dSNadav Amit static inline void kvm_register_writel(struct kvm_vcpu *vcpu,
24027e6fb5dSNadav Amit 				       enum kvm_reg reg,
24127e6fb5dSNadav Amit 				       unsigned long val)
24227e6fb5dSNadav Amit {
24327e6fb5dSNadav Amit 	if (!is_64_bit_mode(vcpu))
24427e6fb5dSNadav Amit 		val = (u32)val;
24527e6fb5dSNadav Amit 	return kvm_register_write(vcpu, reg, val);
24627e6fb5dSNadav Amit }
24727e6fb5dSNadav Amit 
24841dbc6bcSPaolo Bonzini static inline bool kvm_check_has_quirk(struct kvm *kvm, u64 quirk)
24941dbc6bcSPaolo Bonzini {
25041dbc6bcSPaolo Bonzini 	return !(kvm->arch.disabled_quirks & quirk);
25141dbc6bcSPaolo Bonzini }
25241dbc6bcSPaolo Bonzini 
253bab5bb39SNicholas Krause void kvm_set_pending_timer(struct kvm_vcpu *vcpu);
25471f9833bSSerge E. Hallyn int kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip);
255ff9d07a0SZhang, Yanmin 
2568fe8ab46SWill Auld void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr);
257108b249cSPaolo Bonzini u64 get_kvmclock_ns(struct kvm *kvm);
25899e3e30aSZachary Amsden 
259ce14e868SPaolo Bonzini int kvm_read_guest_virt(struct kvm_vcpu *vcpu,
260064aea77SNadav Har'El 	gva_t addr, void *val, unsigned int bytes,
261064aea77SNadav Har'El 	struct x86_exception *exception);
262064aea77SNadav Har'El 
263ce14e868SPaolo Bonzini int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu,
2646a4d7550SNadav Har'El 	gva_t addr, void *val, unsigned int bytes,
2656a4d7550SNadav Har'El 	struct x86_exception *exception);
2666a4d7550SNadav Har'El 
267082d06edSWanpeng Li int handle_ud(struct kvm_vcpu *vcpu);
268082d06edSWanpeng Li 
26919efffa2SXiao Guangrong void kvm_vcpu_mtrr_init(struct kvm_vcpu *vcpu);
270ff53604bSXiao Guangrong u8 kvm_mtrr_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn);
2714566654bSNadav Amit bool kvm_mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data);
272ff53604bSXiao Guangrong int kvm_mtrr_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data);
273ff53604bSXiao Guangrong int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata);
2746a39bbc5SXiao Guangrong bool kvm_mtrr_check_gfn_range_consistency(struct kvm_vcpu *vcpu, gfn_t gfn,
2756a39bbc5SXiao Guangrong 					  int page_num);
27652004014SFeng Wu bool kvm_vector_hashing_enabled(void);
2774566654bSNadav Amit 
278d91cab78SDave Hansen #define KVM_SUPPORTED_XCR0     (XFEATURE_MASK_FP | XFEATURE_MASK_SSE \
279d91cab78SDave Hansen 				| XFEATURE_MASK_YMM | XFEATURE_MASK_BNDREGS \
28017a511f8SHuaitong Han 				| XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \
28117a511f8SHuaitong Han 				| XFEATURE_MASK_PKRU)
28200b27a3eSAvi Kivity extern u64 host_xcr0;
28300b27a3eSAvi Kivity 
2844ff41732SPaolo Bonzini extern u64 kvm_supported_xcr0(void);
2854ff41732SPaolo Bonzini 
2869ed96e87SMarcelo Tosatti extern unsigned int min_timer_period_us;
2879ed96e87SMarcelo Tosatti 
288d0659d94SMarcelo Tosatti extern unsigned int lapic_timer_advance_ns;
289d0659d94SMarcelo Tosatti 
290c4ae60e4SLiran Alon extern bool enable_vmware_backdoor;
291c4ae60e4SLiran Alon 
29254e9818fSGleb Natapov extern struct static_key kvm_no_apic_vcpu;
293b51012deSPaolo Bonzini 
2948d93c874SMarcelo Tosatti static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec)
2958d93c874SMarcelo Tosatti {
2968d93c874SMarcelo Tosatti 	return pvclock_scale_delta(nsec, vcpu->arch.virtual_tsc_mult,
2978d93c874SMarcelo Tosatti 				   vcpu->arch.virtual_tsc_shift);
2988d93c874SMarcelo Tosatti }
2998d93c874SMarcelo Tosatti 
300b51012deSPaolo Bonzini /* Same "calling convention" as do_div:
301b51012deSPaolo Bonzini  * - divide (n << 32) by base
302b51012deSPaolo Bonzini  * - put result in n
303b51012deSPaolo Bonzini  * - return remainder
304b51012deSPaolo Bonzini  */
305b51012deSPaolo Bonzini #define do_shl32_div32(n, base)					\
306b51012deSPaolo Bonzini 	({							\
307b51012deSPaolo Bonzini 	    u32 __quot, __rem;					\
308b51012deSPaolo Bonzini 	    asm("divl %2" : "=a" (__quot), "=d" (__rem)		\
309b51012deSPaolo Bonzini 			: "rm" (base), "0" (0), "1" ((u32) n));	\
310b51012deSPaolo Bonzini 	    n = __quot;						\
311b51012deSPaolo Bonzini 	    __rem;						\
312b51012deSPaolo Bonzini 	 })
313b51012deSPaolo Bonzini 
3144d5422ceSWanpeng Li static inline bool kvm_mwait_in_guest(struct kvm *kvm)
315668fffa3SMichael S. Tsirkin {
3164d5422ceSWanpeng Li 	return kvm->arch.mwait_in_guest;
317668fffa3SMichael S. Tsirkin }
318668fffa3SMichael S. Tsirkin 
319caa057a2SWanpeng Li static inline bool kvm_hlt_in_guest(struct kvm *kvm)
320caa057a2SWanpeng Li {
321caa057a2SWanpeng Li 	return kvm->arch.hlt_in_guest;
322caa057a2SWanpeng Li }
323caa057a2SWanpeng Li 
324b31c114bSWanpeng Li static inline bool kvm_pause_in_guest(struct kvm *kvm)
325b31c114bSWanpeng Li {
326b31c114bSWanpeng Li 	return kvm->arch.pause_in_guest;
327b31c114bSWanpeng Li }
328b31c114bSWanpeng Li 
329dd60d217SAndi Kleen DECLARE_PER_CPU(struct kvm_vcpu *, current_vcpu);
330dd60d217SAndi Kleen 
331dd60d217SAndi Kleen static inline void kvm_before_interrupt(struct kvm_vcpu *vcpu)
332dd60d217SAndi Kleen {
333dd60d217SAndi Kleen 	__this_cpu_write(current_vcpu, vcpu);
334dd60d217SAndi Kleen }
335dd60d217SAndi Kleen 
336dd60d217SAndi Kleen static inline void kvm_after_interrupt(struct kvm_vcpu *vcpu)
337dd60d217SAndi Kleen {
338dd60d217SAndi Kleen 	__this_cpu_write(current_vcpu, NULL);
339dd60d217SAndi Kleen }
340dd60d217SAndi Kleen 
34126eef70cSAvi Kivity #endif
342