1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef ARCH_X86_KVM_X86_H
3 #define ARCH_X86_KVM_X86_H
5 #include <linux/kvm_host.h>
6 #include <asm/pvclock.h>
7 #include "kvm_cache_regs.h"
8 #include "kvm_emulate.h"
10 #define KVM_DEFAULT_PLE_GAP 128
11 #define KVM_VMX_DEFAULT_PLE_WINDOW 4096
12 #define KVM_DEFAULT_PLE_WINDOW_GROW 2
13 #define KVM_DEFAULT_PLE_WINDOW_SHRINK 0
14 #define KVM_VMX_DEFAULT_PLE_WINDOW_MAX UINT_MAX
15 #define KVM_SVM_DEFAULT_PLE_WINDOW_MAX USHRT_MAX
16 #define KVM_SVM_DEFAULT_PLE_WINDOW 3000
18 static inline unsigned int __grow_ple_window(unsigned int val
,
19 unsigned int base
, unsigned int modifier
, unsigned int max
)
31 return min(ret
, (u64
)max
);
34 static inline unsigned int __shrink_ple_window(unsigned int val
,
35 unsigned int base
, unsigned int modifier
, unsigned int min
)
48 #define MSR_IA32_CR_PAT_DEFAULT 0x0007040600070406ULL
50 static inline void kvm_clear_exception_queue(struct kvm_vcpu
*vcpu
)
52 vcpu
->arch
.exception
.pending
= false;
53 vcpu
->arch
.exception
.injected
= false;
56 static inline void kvm_queue_interrupt(struct kvm_vcpu
*vcpu
, u8 vector
,
59 vcpu
->arch
.interrupt
.injected
= true;
60 vcpu
->arch
.interrupt
.soft
= soft
;
61 vcpu
->arch
.interrupt
.nr
= vector
;
64 static inline void kvm_clear_interrupt_queue(struct kvm_vcpu
*vcpu
)
66 vcpu
->arch
.interrupt
.injected
= false;
69 static inline bool kvm_event_needs_reinjection(struct kvm_vcpu
*vcpu
)
71 return vcpu
->arch
.exception
.injected
|| vcpu
->arch
.interrupt
.injected
||
72 vcpu
->arch
.nmi_injected
;
75 static inline bool kvm_exception_is_soft(unsigned int nr
)
77 return (nr
== BP_VECTOR
) || (nr
== OF_VECTOR
);
80 static inline bool is_protmode(struct kvm_vcpu
*vcpu
)
82 return kvm_read_cr0_bits(vcpu
, X86_CR0_PE
);
85 static inline int is_long_mode(struct kvm_vcpu
*vcpu
)
88 return vcpu
->arch
.efer
& EFER_LMA
;
94 static inline bool is_64_bit_mode(struct kvm_vcpu
*vcpu
)
98 if (!is_long_mode(vcpu
))
100 kvm_x86_ops
.get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
104 static inline bool is_la57_mode(struct kvm_vcpu
*vcpu
)
107 return (vcpu
->arch
.efer
& EFER_LMA
) &&
108 kvm_read_cr4_bits(vcpu
, X86_CR4_LA57
);
114 static inline bool x86_exception_has_error_code(unsigned int vector
)
116 static u32 exception_has_error_code
= BIT(DF_VECTOR
) | BIT(TS_VECTOR
) |
117 BIT(NP_VECTOR
) | BIT(SS_VECTOR
) | BIT(GP_VECTOR
) |
118 BIT(PF_VECTOR
) | BIT(AC_VECTOR
);
120 return (1U << vector
) & exception_has_error_code
;
123 static inline bool mmu_is_nested(struct kvm_vcpu
*vcpu
)
125 return vcpu
->arch
.walk_mmu
== &vcpu
->arch
.nested_mmu
;
128 static inline int is_pae(struct kvm_vcpu
*vcpu
)
130 return kvm_read_cr4_bits(vcpu
, X86_CR4_PAE
);
133 static inline int is_pse(struct kvm_vcpu
*vcpu
)
135 return kvm_read_cr4_bits(vcpu
, X86_CR4_PSE
);
138 static inline int is_paging(struct kvm_vcpu
*vcpu
)
140 return likely(kvm_read_cr0_bits(vcpu
, X86_CR0_PG
));
143 static inline bool is_pae_paging(struct kvm_vcpu
*vcpu
)
145 return !is_long_mode(vcpu
) && is_pae(vcpu
) && is_paging(vcpu
);
148 static inline u8
vcpu_virt_addr_bits(struct kvm_vcpu
*vcpu
)
150 return kvm_read_cr4_bits(vcpu
, X86_CR4_LA57
) ? 57 : 48;
153 static inline u64
get_canonical(u64 la
, u8 vaddr_bits
)
155 return ((int64_t)la
<< (64 - vaddr_bits
)) >> (64 - vaddr_bits
);
158 static inline bool is_noncanonical_address(u64 la
, struct kvm_vcpu
*vcpu
)
160 return get_canonical(la
, vcpu_virt_addr_bits(vcpu
)) != la
;
163 static inline void vcpu_cache_mmio_info(struct kvm_vcpu
*vcpu
,
164 gva_t gva
, gfn_t gfn
, unsigned access
)
166 u64 gen
= kvm_memslots(vcpu
->kvm
)->generation
;
168 if (unlikely(gen
& KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS
))
172 * If this is a shadow nested page table, the "GVA" is
175 vcpu
->arch
.mmio_gva
= mmu_is_nested(vcpu
) ? 0 : gva
& PAGE_MASK
;
176 vcpu
->arch
.mmio_access
= access
;
177 vcpu
->arch
.mmio_gfn
= gfn
;
178 vcpu
->arch
.mmio_gen
= gen
;
181 static inline bool vcpu_match_mmio_gen(struct kvm_vcpu
*vcpu
)
183 return vcpu
->arch
.mmio_gen
== kvm_memslots(vcpu
->kvm
)->generation
;
187 * Clear the mmio cache info for the given gva. If gva is MMIO_GVA_ANY, we
188 * clear all mmio cache info.
190 #define MMIO_GVA_ANY (~(gva_t)0)
192 static inline void vcpu_clear_mmio_info(struct kvm_vcpu
*vcpu
, gva_t gva
)
194 if (gva
!= MMIO_GVA_ANY
&& vcpu
->arch
.mmio_gva
!= (gva
& PAGE_MASK
))
197 vcpu
->arch
.mmio_gva
= 0;
200 static inline bool vcpu_match_mmio_gva(struct kvm_vcpu
*vcpu
, unsigned long gva
)
202 if (vcpu_match_mmio_gen(vcpu
) && vcpu
->arch
.mmio_gva
&&
203 vcpu
->arch
.mmio_gva
== (gva
& PAGE_MASK
))
209 static inline bool vcpu_match_mmio_gpa(struct kvm_vcpu
*vcpu
, gpa_t gpa
)
211 if (vcpu_match_mmio_gen(vcpu
) && vcpu
->arch
.mmio_gfn
&&
212 vcpu
->arch
.mmio_gfn
== gpa
>> PAGE_SHIFT
)
218 static inline unsigned long kvm_register_readl(struct kvm_vcpu
*vcpu
, int reg
)
220 unsigned long val
= kvm_register_read(vcpu
, reg
);
222 return is_64_bit_mode(vcpu
) ? val
: (u32
)val
;
225 static inline void kvm_register_writel(struct kvm_vcpu
*vcpu
,
226 int reg
, unsigned long val
)
228 if (!is_64_bit_mode(vcpu
))
230 return kvm_register_write(vcpu
, reg
, val
);
233 static inline bool kvm_check_has_quirk(struct kvm
*kvm
, u64 quirk
)
235 return !(kvm
->arch
.disabled_quirks
& quirk
);
238 static inline bool kvm_vcpu_latch_init(struct kvm_vcpu
*vcpu
)
240 return is_smm(vcpu
) || kvm_x86_ops
.apic_init_signal_blocked(vcpu
);
243 void kvm_set_pending_timer(struct kvm_vcpu
*vcpu
);
244 void kvm_inject_realmode_interrupt(struct kvm_vcpu
*vcpu
, int irq
, int inc_eip
);
246 void kvm_write_tsc(struct kvm_vcpu
*vcpu
, struct msr_data
*msr
);
247 u64
get_kvmclock_ns(struct kvm
*kvm
);
249 int kvm_read_guest_virt(struct kvm_vcpu
*vcpu
,
250 gva_t addr
, void *val
, unsigned int bytes
,
251 struct x86_exception
*exception
);
253 int kvm_write_guest_virt_system(struct kvm_vcpu
*vcpu
,
254 gva_t addr
, void *val
, unsigned int bytes
,
255 struct x86_exception
*exception
);
257 int handle_ud(struct kvm_vcpu
*vcpu
);
259 void kvm_deliver_exception_payload(struct kvm_vcpu
*vcpu
);
261 void kvm_vcpu_mtrr_init(struct kvm_vcpu
*vcpu
);
262 u8
kvm_mtrr_get_guest_memory_type(struct kvm_vcpu
*vcpu
, gfn_t gfn
);
263 bool kvm_mtrr_valid(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
);
264 int kvm_mtrr_set_msr(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
);
265 int kvm_mtrr_get_msr(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
);
266 bool kvm_mtrr_check_gfn_range_consistency(struct kvm_vcpu
*vcpu
, gfn_t gfn
,
268 bool kvm_vector_hashing_enabled(void);
269 int x86_emulate_instruction(struct kvm_vcpu
*vcpu
, gpa_t cr2_or_gpa
,
270 int emulation_type
, void *insn
, int insn_len
);
271 enum exit_fastpath_completion
handle_fastpath_set_msr_irqoff(struct kvm_vcpu
*vcpu
);
273 extern u64 host_xcr0
;
274 extern u64 supported_xcr0
;
275 extern u64 supported_xss
;
277 static inline bool kvm_mpx_supported(void)
279 return (supported_xcr0
& (XFEATURE_MASK_BNDREGS
| XFEATURE_MASK_BNDCSR
))
280 == (XFEATURE_MASK_BNDREGS
| XFEATURE_MASK_BNDCSR
);
283 extern unsigned int min_timer_period_us
;
285 extern bool enable_vmware_backdoor
;
287 extern int pi_inject_timer
;
289 extern struct static_key kvm_no_apic_vcpu
;
291 static inline u64
nsec_to_cycles(struct kvm_vcpu
*vcpu
, u64 nsec
)
293 return pvclock_scale_delta(nsec
, vcpu
->arch
.virtual_tsc_mult
,
294 vcpu
->arch
.virtual_tsc_shift
);
297 /* Same "calling convention" as do_div:
298 * - divide (n << 32) by base
302 #define do_shl32_div32(n, base) \
305 asm("divl %2" : "=a" (__quot), "=d" (__rem) \
306 : "rm" (base), "0" (0), "1" ((u32) n)); \
311 static inline bool kvm_mwait_in_guest(struct kvm
*kvm
)
313 return kvm
->arch
.mwait_in_guest
;
316 static inline bool kvm_hlt_in_guest(struct kvm
*kvm
)
318 return kvm
->arch
.hlt_in_guest
;
321 static inline bool kvm_pause_in_guest(struct kvm
*kvm
)
323 return kvm
->arch
.pause_in_guest
;
326 static inline bool kvm_cstate_in_guest(struct kvm
*kvm
)
328 return kvm
->arch
.cstate_in_guest
;
331 DECLARE_PER_CPU(struct kvm_vcpu
*, current_vcpu
);
333 static inline void kvm_before_interrupt(struct kvm_vcpu
*vcpu
)
335 __this_cpu_write(current_vcpu
, vcpu
);
338 static inline void kvm_after_interrupt(struct kvm_vcpu
*vcpu
)
340 __this_cpu_write(current_vcpu
, NULL
);
344 static inline bool kvm_pat_valid(u64 data
)
346 if (data
& 0xF8F8F8F8F8F8F8F8ull
)
348 /* 0, 1, 4, 5, 6, 7 are valid values. */
349 return (data
| ((data
& 0x0202020202020202ull
) << 1)) == data
;
352 static inline bool kvm_dr7_valid(u64 data
)
354 /* Bits [63:32] are reserved */
355 return !(data
>> 32);
358 void kvm_load_guest_xsave_state(struct kvm_vcpu
*vcpu
);
359 void kvm_load_host_xsave_state(struct kvm_vcpu
*vcpu
);
360 u64
kvm_spec_ctrl_valid_bits(struct kvm_vcpu
*vcpu
);