xref: /linux/arch/x86/kvm/x86.h (revision dd60d217062f4527f4a94af8b3a2e9666c26f903)
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 
974545705SRadim Krčmář #define MSR_IA32_CR_PAT_DEFAULT  0x0007040600070406ULL
1074545705SRadim Krčmář 
1126eef70cSAvi Kivity static inline void kvm_clear_exception_queue(struct kvm_vcpu *vcpu)
1226eef70cSAvi Kivity {
135c7d4f9aSLiran Alon 	vcpu->arch.exception.pending = false;
14664f8e26SWanpeng Li 	vcpu->arch.exception.injected = false;
1526eef70cSAvi Kivity }
1626eef70cSAvi Kivity 
1766fd3f7fSGleb Natapov static inline void kvm_queue_interrupt(struct kvm_vcpu *vcpu, u8 vector,
1866fd3f7fSGleb Natapov 	bool soft)
19937a7eaeSAvi Kivity {
20937a7eaeSAvi Kivity 	vcpu->arch.interrupt.pending = true;
2166fd3f7fSGleb Natapov 	vcpu->arch.interrupt.soft = soft;
22937a7eaeSAvi Kivity 	vcpu->arch.interrupt.nr = vector;
23937a7eaeSAvi Kivity }
24937a7eaeSAvi Kivity 
25937a7eaeSAvi Kivity static inline void kvm_clear_interrupt_queue(struct kvm_vcpu *vcpu)
26937a7eaeSAvi Kivity {
27937a7eaeSAvi Kivity 	vcpu->arch.interrupt.pending = false;
28937a7eaeSAvi Kivity }
29937a7eaeSAvi Kivity 
303298b75cSGleb Natapov static inline bool kvm_event_needs_reinjection(struct kvm_vcpu *vcpu)
313298b75cSGleb Natapov {
32664f8e26SWanpeng Li 	return vcpu->arch.exception.injected || vcpu->arch.interrupt.pending ||
333298b75cSGleb Natapov 		vcpu->arch.nmi_injected;
343298b75cSGleb Natapov }
3566fd3f7fSGleb Natapov 
3666fd3f7fSGleb Natapov static inline bool kvm_exception_is_soft(unsigned int nr)
3766fd3f7fSGleb Natapov {
3866fd3f7fSGleb Natapov 	return (nr == BP_VECTOR) || (nr == OF_VECTOR);
3966fd3f7fSGleb Natapov }
40fc61b800SGleb Natapov 
413eeb3288SAvi Kivity static inline bool is_protmode(struct kvm_vcpu *vcpu)
423eeb3288SAvi Kivity {
433eeb3288SAvi Kivity 	return kvm_read_cr0_bits(vcpu, X86_CR0_PE);
443eeb3288SAvi Kivity }
453eeb3288SAvi Kivity 
46836a1b3cSAvi Kivity static inline int is_long_mode(struct kvm_vcpu *vcpu)
47836a1b3cSAvi Kivity {
48836a1b3cSAvi Kivity #ifdef CONFIG_X86_64
49f6801dffSAvi Kivity 	return vcpu->arch.efer & EFER_LMA;
50836a1b3cSAvi Kivity #else
51836a1b3cSAvi Kivity 	return 0;
52836a1b3cSAvi Kivity #endif
53836a1b3cSAvi Kivity }
54836a1b3cSAvi Kivity 
555777392eSNadav Amit static inline bool is_64_bit_mode(struct kvm_vcpu *vcpu)
565777392eSNadav Amit {
575777392eSNadav Amit 	int cs_db, cs_l;
585777392eSNadav Amit 
595777392eSNadav Amit 	if (!is_long_mode(vcpu))
605777392eSNadav Amit 		return false;
615777392eSNadav Amit 	kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
625777392eSNadav Amit 	return cs_l;
635777392eSNadav Amit }
645777392eSNadav Amit 
65855feb67SYu Zhang static inline bool is_la57_mode(struct kvm_vcpu *vcpu)
66855feb67SYu Zhang {
67855feb67SYu Zhang #ifdef CONFIG_X86_64
68855feb67SYu Zhang 	return (vcpu->arch.efer & EFER_LMA) &&
69855feb67SYu Zhang 		 kvm_read_cr4_bits(vcpu, X86_CR4_LA57);
70855feb67SYu Zhang #else
71855feb67SYu Zhang 	return 0;
72855feb67SYu Zhang #endif
73855feb67SYu Zhang }
74855feb67SYu Zhang 
756539e738SJoerg Roedel static inline bool mmu_is_nested(struct kvm_vcpu *vcpu)
766539e738SJoerg Roedel {
776539e738SJoerg Roedel 	return vcpu->arch.walk_mmu == &vcpu->arch.nested_mmu;
786539e738SJoerg Roedel }
796539e738SJoerg Roedel 
80836a1b3cSAvi Kivity static inline int is_pae(struct kvm_vcpu *vcpu)
81836a1b3cSAvi Kivity {
82836a1b3cSAvi Kivity 	return kvm_read_cr4_bits(vcpu, X86_CR4_PAE);
83836a1b3cSAvi Kivity }
84836a1b3cSAvi Kivity 
85836a1b3cSAvi Kivity static inline int is_pse(struct kvm_vcpu *vcpu)
86836a1b3cSAvi Kivity {
87836a1b3cSAvi Kivity 	return kvm_read_cr4_bits(vcpu, X86_CR4_PSE);
88836a1b3cSAvi Kivity }
89836a1b3cSAvi Kivity 
90836a1b3cSAvi Kivity static inline int is_paging(struct kvm_vcpu *vcpu)
91836a1b3cSAvi Kivity {
92c36fc04eSDavidlohr Bueso 	return likely(kvm_read_cr0_bits(vcpu, X86_CR0_PG));
93836a1b3cSAvi Kivity }
94836a1b3cSAvi Kivity 
9524d1b15fSJoerg Roedel static inline u32 bit(int bitno)
9624d1b15fSJoerg Roedel {
9724d1b15fSJoerg Roedel 	return 1 << (bitno & 31);
9824d1b15fSJoerg Roedel }
9924d1b15fSJoerg Roedel 
100fd8cb433SYu Zhang static inline u8 vcpu_virt_addr_bits(struct kvm_vcpu *vcpu)
101fd8cb433SYu Zhang {
102fd8cb433SYu Zhang 	return kvm_read_cr4_bits(vcpu, X86_CR4_LA57) ? 57 : 48;
103fd8cb433SYu Zhang }
104fd8cb433SYu Zhang 
105fd8cb433SYu Zhang static inline u8 ctxt_virt_addr_bits(struct x86_emulate_ctxt *ctxt)
106fd8cb433SYu Zhang {
107fd8cb433SYu Zhang 	return (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_LA57) ? 57 : 48;
108fd8cb433SYu Zhang }
109fd8cb433SYu Zhang 
110fd8cb433SYu Zhang static inline u64 get_canonical(u64 la, u8 vaddr_bits)
111fd8cb433SYu Zhang {
112fd8cb433SYu Zhang 	return ((int64_t)la << (64 - vaddr_bits)) >> (64 - vaddr_bits);
113fd8cb433SYu Zhang }
114fd8cb433SYu Zhang 
115fd8cb433SYu Zhang static inline bool is_noncanonical_address(u64 la, struct kvm_vcpu *vcpu)
116fd8cb433SYu Zhang {
117fd8cb433SYu Zhang #ifdef CONFIG_X86_64
118fd8cb433SYu Zhang 	return get_canonical(la, vcpu_virt_addr_bits(vcpu)) != la;
119fd8cb433SYu Zhang #else
120fd8cb433SYu Zhang 	return false;
121fd8cb433SYu Zhang #endif
122fd8cb433SYu Zhang }
123fd8cb433SYu Zhang 
124fd8cb433SYu Zhang static inline bool emul_is_noncanonical_address(u64 la,
125fd8cb433SYu Zhang 						struct x86_emulate_ctxt *ctxt)
126fd8cb433SYu Zhang {
127fd8cb433SYu Zhang #ifdef CONFIG_X86_64
128fd8cb433SYu Zhang 	return get_canonical(la, ctxt_virt_addr_bits(ctxt)) != la;
129fd8cb433SYu Zhang #else
130fd8cb433SYu Zhang 	return false;
131fd8cb433SYu Zhang #endif
132fd8cb433SYu Zhang }
133fd8cb433SYu Zhang 
134bebb106aSXiao Guangrong static inline void vcpu_cache_mmio_info(struct kvm_vcpu *vcpu,
135bebb106aSXiao Guangrong 					gva_t gva, gfn_t gfn, unsigned access)
136bebb106aSXiao Guangrong {
1379034e6e8SPaolo Bonzini 	/*
1389034e6e8SPaolo Bonzini 	 * If this is a shadow nested page table, the "GVA" is
1399034e6e8SPaolo Bonzini 	 * actually a nGPA.
1409034e6e8SPaolo Bonzini 	 */
1419034e6e8SPaolo Bonzini 	vcpu->arch.mmio_gva = mmu_is_nested(vcpu) ? 0 : gva & PAGE_MASK;
142bebb106aSXiao Guangrong 	vcpu->arch.access = access;
143bebb106aSXiao Guangrong 	vcpu->arch.mmio_gfn = gfn;
14456f17dd3SDavid Matlack 	vcpu->arch.mmio_gen = kvm_memslots(vcpu->kvm)->generation;
14556f17dd3SDavid Matlack }
14656f17dd3SDavid Matlack 
14756f17dd3SDavid Matlack static inline bool vcpu_match_mmio_gen(struct kvm_vcpu *vcpu)
14856f17dd3SDavid Matlack {
14956f17dd3SDavid Matlack 	return vcpu->arch.mmio_gen == kvm_memslots(vcpu->kvm)->generation;
150bebb106aSXiao Guangrong }
151bebb106aSXiao Guangrong 
152bebb106aSXiao Guangrong /*
15356f17dd3SDavid Matlack  * Clear the mmio cache info for the given gva. If gva is MMIO_GVA_ANY, we
15456f17dd3SDavid Matlack  * clear all mmio cache info.
155bebb106aSXiao Guangrong  */
15656f17dd3SDavid Matlack #define MMIO_GVA_ANY (~(gva_t)0)
15756f17dd3SDavid Matlack 
158bebb106aSXiao Guangrong static inline void vcpu_clear_mmio_info(struct kvm_vcpu *vcpu, gva_t gva)
159bebb106aSXiao Guangrong {
16056f17dd3SDavid Matlack 	if (gva != MMIO_GVA_ANY && vcpu->arch.mmio_gva != (gva & PAGE_MASK))
161bebb106aSXiao Guangrong 		return;
162bebb106aSXiao Guangrong 
163bebb106aSXiao Guangrong 	vcpu->arch.mmio_gva = 0;
164bebb106aSXiao Guangrong }
165bebb106aSXiao Guangrong 
166bebb106aSXiao Guangrong static inline bool vcpu_match_mmio_gva(struct kvm_vcpu *vcpu, unsigned long gva)
167bebb106aSXiao Guangrong {
16856f17dd3SDavid Matlack 	if (vcpu_match_mmio_gen(vcpu) && vcpu->arch.mmio_gva &&
16956f17dd3SDavid Matlack 	      vcpu->arch.mmio_gva == (gva & PAGE_MASK))
170bebb106aSXiao Guangrong 		return true;
171bebb106aSXiao Guangrong 
172bebb106aSXiao Guangrong 	return false;
173bebb106aSXiao Guangrong }
174bebb106aSXiao Guangrong 
175bebb106aSXiao Guangrong static inline bool vcpu_match_mmio_gpa(struct kvm_vcpu *vcpu, gpa_t gpa)
176bebb106aSXiao Guangrong {
17756f17dd3SDavid Matlack 	if (vcpu_match_mmio_gen(vcpu) && vcpu->arch.mmio_gfn &&
17856f17dd3SDavid Matlack 	      vcpu->arch.mmio_gfn == gpa >> PAGE_SHIFT)
179bebb106aSXiao Guangrong 		return true;
180bebb106aSXiao Guangrong 
181bebb106aSXiao Guangrong 	return false;
182bebb106aSXiao Guangrong }
183bebb106aSXiao Guangrong 
1845777392eSNadav Amit static inline unsigned long kvm_register_readl(struct kvm_vcpu *vcpu,
1855777392eSNadav Amit 					       enum kvm_reg reg)
1865777392eSNadav Amit {
1875777392eSNadav Amit 	unsigned long val = kvm_register_read(vcpu, reg);
1885777392eSNadav Amit 
1895777392eSNadav Amit 	return is_64_bit_mode(vcpu) ? val : (u32)val;
1905777392eSNadav Amit }
1915777392eSNadav Amit 
19227e6fb5dSNadav Amit static inline void kvm_register_writel(struct kvm_vcpu *vcpu,
19327e6fb5dSNadav Amit 				       enum kvm_reg reg,
19427e6fb5dSNadav Amit 				       unsigned long val)
19527e6fb5dSNadav Amit {
19627e6fb5dSNadav Amit 	if (!is_64_bit_mode(vcpu))
19727e6fb5dSNadav Amit 		val = (u32)val;
19827e6fb5dSNadav Amit 	return kvm_register_write(vcpu, reg, val);
19927e6fb5dSNadav Amit }
20027e6fb5dSNadav Amit 
20141dbc6bcSPaolo Bonzini static inline bool kvm_check_has_quirk(struct kvm *kvm, u64 quirk)
20241dbc6bcSPaolo Bonzini {
20341dbc6bcSPaolo Bonzini 	return !(kvm->arch.disabled_quirks & quirk);
20441dbc6bcSPaolo Bonzini }
20541dbc6bcSPaolo Bonzini 
206bab5bb39SNicholas Krause void kvm_set_pending_timer(struct kvm_vcpu *vcpu);
20771f9833bSSerge E. Hallyn int kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip);
208ff9d07a0SZhang, Yanmin 
2098fe8ab46SWill Auld void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr);
210108b249cSPaolo Bonzini u64 get_kvmclock_ns(struct kvm *kvm);
21199e3e30aSZachary Amsden 
212064aea77SNadav Har'El int kvm_read_guest_virt(struct x86_emulate_ctxt *ctxt,
213064aea77SNadav Har'El 	gva_t addr, void *val, unsigned int bytes,
214064aea77SNadav Har'El 	struct x86_exception *exception);
215064aea77SNadav Har'El 
2166a4d7550SNadav Har'El int kvm_write_guest_virt_system(struct x86_emulate_ctxt *ctxt,
2176a4d7550SNadav Har'El 	gva_t addr, void *val, unsigned int bytes,
2186a4d7550SNadav Har'El 	struct x86_exception *exception);
2196a4d7550SNadav Har'El 
22019efffa2SXiao Guangrong void kvm_vcpu_mtrr_init(struct kvm_vcpu *vcpu);
221ff53604bSXiao Guangrong u8 kvm_mtrr_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn);
2224566654bSNadav Amit bool kvm_mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data);
223ff53604bSXiao Guangrong int kvm_mtrr_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data);
224ff53604bSXiao Guangrong int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata);
2256a39bbc5SXiao Guangrong bool kvm_mtrr_check_gfn_range_consistency(struct kvm_vcpu *vcpu, gfn_t gfn,
2266a39bbc5SXiao Guangrong 					  int page_num);
22752004014SFeng Wu bool kvm_vector_hashing_enabled(void);
2284566654bSNadav Amit 
229d91cab78SDave Hansen #define KVM_SUPPORTED_XCR0     (XFEATURE_MASK_FP | XFEATURE_MASK_SSE \
230d91cab78SDave Hansen 				| XFEATURE_MASK_YMM | XFEATURE_MASK_BNDREGS \
23117a511f8SHuaitong Han 				| XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \
23217a511f8SHuaitong Han 				| XFEATURE_MASK_PKRU)
23300b27a3eSAvi Kivity extern u64 host_xcr0;
23400b27a3eSAvi Kivity 
2354ff41732SPaolo Bonzini extern u64 kvm_supported_xcr0(void);
2364ff41732SPaolo Bonzini 
2379ed96e87SMarcelo Tosatti extern unsigned int min_timer_period_us;
2389ed96e87SMarcelo Tosatti 
239d0659d94SMarcelo Tosatti extern unsigned int lapic_timer_advance_ns;
240d0659d94SMarcelo Tosatti 
241c4ae60e4SLiran Alon extern bool enable_vmware_backdoor;
242c4ae60e4SLiran Alon 
24354e9818fSGleb Natapov extern struct static_key kvm_no_apic_vcpu;
244b51012deSPaolo Bonzini 
2458d93c874SMarcelo Tosatti static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec)
2468d93c874SMarcelo Tosatti {
2478d93c874SMarcelo Tosatti 	return pvclock_scale_delta(nsec, vcpu->arch.virtual_tsc_mult,
2488d93c874SMarcelo Tosatti 				   vcpu->arch.virtual_tsc_shift);
2498d93c874SMarcelo Tosatti }
2508d93c874SMarcelo Tosatti 
251b51012deSPaolo Bonzini /* Same "calling convention" as do_div:
252b51012deSPaolo Bonzini  * - divide (n << 32) by base
253b51012deSPaolo Bonzini  * - put result in n
254b51012deSPaolo Bonzini  * - return remainder
255b51012deSPaolo Bonzini  */
256b51012deSPaolo Bonzini #define do_shl32_div32(n, base)					\
257b51012deSPaolo Bonzini 	({							\
258b51012deSPaolo Bonzini 	    u32 __quot, __rem;					\
259b51012deSPaolo Bonzini 	    asm("divl %2" : "=a" (__quot), "=d" (__rem)		\
260b51012deSPaolo Bonzini 			: "rm" (base), "0" (0), "1" ((u32) n));	\
261b51012deSPaolo Bonzini 	    n = __quot;						\
262b51012deSPaolo Bonzini 	    __rem;						\
263b51012deSPaolo Bonzini 	 })
264b51012deSPaolo Bonzini 
2654d5422ceSWanpeng Li #define KVM_X86_DISABLE_EXITS_MWAIT          (1 << 0)
266caa057a2SWanpeng Li #define KVM_X86_DISABLE_EXITS_HTL            (1 << 1)
267b31c114bSWanpeng Li #define KVM_X86_DISABLE_EXITS_PAUSE          (1 << 2)
268caa057a2SWanpeng Li #define KVM_X86_DISABLE_VALID_EXITS          (KVM_X86_DISABLE_EXITS_MWAIT | \
269b31c114bSWanpeng Li                                               KVM_X86_DISABLE_EXITS_HTL | \
270b31c114bSWanpeng Li                                               KVM_X86_DISABLE_EXITS_PAUSE)
2714d5422ceSWanpeng Li 
2724d5422ceSWanpeng Li static inline bool kvm_mwait_in_guest(struct kvm *kvm)
273668fffa3SMichael S. Tsirkin {
2744d5422ceSWanpeng Li 	return kvm->arch.mwait_in_guest;
275668fffa3SMichael S. Tsirkin }
276668fffa3SMichael S. Tsirkin 
277caa057a2SWanpeng Li static inline bool kvm_hlt_in_guest(struct kvm *kvm)
278caa057a2SWanpeng Li {
279caa057a2SWanpeng Li 	return kvm->arch.hlt_in_guest;
280caa057a2SWanpeng Li }
281caa057a2SWanpeng Li 
282b31c114bSWanpeng Li static inline bool kvm_pause_in_guest(struct kvm *kvm)
283b31c114bSWanpeng Li {
284b31c114bSWanpeng Li 	return kvm->arch.pause_in_guest;
285b31c114bSWanpeng Li }
286b31c114bSWanpeng Li 
287*dd60d217SAndi Kleen DECLARE_PER_CPU(struct kvm_vcpu *, current_vcpu);
288*dd60d217SAndi Kleen 
289*dd60d217SAndi Kleen static inline void kvm_before_interrupt(struct kvm_vcpu *vcpu)
290*dd60d217SAndi Kleen {
291*dd60d217SAndi Kleen 	__this_cpu_write(current_vcpu, vcpu);
292*dd60d217SAndi Kleen }
293*dd60d217SAndi Kleen 
294*dd60d217SAndi Kleen static inline void kvm_after_interrupt(struct kvm_vcpu *vcpu)
295*dd60d217SAndi Kleen {
296*dd60d217SAndi Kleen 	__this_cpu_write(current_vcpu, NULL);
297*dd60d217SAndi Kleen }
298*dd60d217SAndi Kleen 
29926eef70cSAvi Kivity #endif
300