x86.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef ARCH_X86_KVM_X86_H
  3. #define ARCH_X86_KVM_X86_H
  4. #include <linux/kvm_host.h>
  5. #include <asm/pvclock.h>
  6. #include "kvm_cache_regs.h"
  7. #define KVM_DEFAULT_PLE_GAP 128
  8. #define KVM_VMX_DEFAULT_PLE_WINDOW 4096
  9. #define KVM_DEFAULT_PLE_WINDOW_GROW 2
  10. #define KVM_DEFAULT_PLE_WINDOW_SHRINK 0
  11. #define KVM_VMX_DEFAULT_PLE_WINDOW_MAX UINT_MAX
  12. #define KVM_SVM_DEFAULT_PLE_WINDOW_MAX USHRT_MAX
  13. #define KVM_SVM_DEFAULT_PLE_WINDOW 3000
  14. static inline unsigned int __grow_ple_window(unsigned int val,
  15. unsigned int base, unsigned int modifier, unsigned int max)
  16. {
  17. u64 ret = val;
  18. if (modifier < 1)
  19. return base;
  20. if (modifier < base)
  21. ret *= modifier;
  22. else
  23. ret += modifier;
  24. return min(ret, (u64)max);
  25. }
  26. static inline unsigned int __shrink_ple_window(unsigned int val,
  27. unsigned int base, unsigned int modifier, unsigned int min)
  28. {
  29. if (modifier < 1)
  30. return base;
  31. if (modifier < base)
  32. val /= modifier;
  33. else
  34. val -= modifier;
  35. return max(val, min);
  36. }
  37. #define MSR_IA32_CR_PAT_DEFAULT 0x0007040600070406ULL
  38. static inline void kvm_clear_exception_queue(struct kvm_vcpu *vcpu)
  39. {
  40. vcpu->arch.exception.pending = false;
  41. vcpu->arch.exception.injected = false;
  42. }
  43. static inline void kvm_queue_interrupt(struct kvm_vcpu *vcpu, u8 vector,
  44. bool soft)
  45. {
  46. vcpu->arch.interrupt.injected = true;
  47. vcpu->arch.interrupt.soft = soft;
  48. vcpu->arch.interrupt.nr = vector;
  49. }
  50. static inline void kvm_clear_interrupt_queue(struct kvm_vcpu *vcpu)
  51. {
  52. vcpu->arch.interrupt.injected = false;
  53. }
  54. static inline bool kvm_event_needs_reinjection(struct kvm_vcpu *vcpu)
  55. {
  56. return vcpu->arch.exception.injected || vcpu->arch.interrupt.injected ||
  57. vcpu->arch.nmi_injected;
  58. }
  59. static inline bool kvm_exception_is_soft(unsigned int nr)
  60. {
  61. return (nr == BP_VECTOR) || (nr == OF_VECTOR);
  62. }
  63. static inline bool is_protmode(struct kvm_vcpu *vcpu)
  64. {
  65. return kvm_read_cr0_bits(vcpu, X86_CR0_PE);
  66. }
  67. static inline int is_long_mode(struct kvm_vcpu *vcpu)
  68. {
  69. #ifdef CONFIG_X86_64
  70. return vcpu->arch.efer & EFER_LMA;
  71. #else
  72. return 0;
  73. #endif
  74. }
  75. static inline bool is_64_bit_mode(struct kvm_vcpu *vcpu)
  76. {
  77. int cs_db, cs_l;
  78. if (!is_long_mode(vcpu))
  79. return false;
  80. kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  81. return cs_l;
  82. }
  83. static inline bool is_la57_mode(struct kvm_vcpu *vcpu)
  84. {
  85. #ifdef CONFIG_X86_64
  86. return (vcpu->arch.efer & EFER_LMA) &&
  87. kvm_read_cr4_bits(vcpu, X86_CR4_LA57);
  88. #else
  89. return 0;
  90. #endif
  91. }
  92. static inline bool x86_exception_has_error_code(unsigned int vector)
  93. {
  94. static u32 exception_has_error_code = BIT(DF_VECTOR) | BIT(TS_VECTOR) |
  95. BIT(NP_VECTOR) | BIT(SS_VECTOR) | BIT(GP_VECTOR) |
  96. BIT(PF_VECTOR) | BIT(AC_VECTOR);
  97. return (1U << vector) & exception_has_error_code;
  98. }
  99. static inline bool mmu_is_nested(struct kvm_vcpu *vcpu)
  100. {
  101. return vcpu->arch.walk_mmu == &vcpu->arch.nested_mmu;
  102. }
  103. static inline int is_pae(struct kvm_vcpu *vcpu)
  104. {
  105. return kvm_read_cr4_bits(vcpu, X86_CR4_PAE);
  106. }
  107. static inline int is_pse(struct kvm_vcpu *vcpu)
  108. {
  109. return kvm_read_cr4_bits(vcpu, X86_CR4_PSE);
  110. }
  111. static inline int is_paging(struct kvm_vcpu *vcpu)
  112. {
  113. return likely(kvm_read_cr0_bits(vcpu, X86_CR0_PG));
  114. }
  115. static inline bool is_pae_paging(struct kvm_vcpu *vcpu)
  116. {
  117. return !is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu);
  118. }
  119. static inline u32 bit(int bitno)
  120. {
  121. return 1 << (bitno & 31);
  122. }
  123. static inline u8 vcpu_virt_addr_bits(struct kvm_vcpu *vcpu)
  124. {
  125. return kvm_read_cr4_bits(vcpu, X86_CR4_LA57) ? 57 : 48;
  126. }
  127. static inline u8 ctxt_virt_addr_bits(struct x86_emulate_ctxt *ctxt)
  128. {
  129. return (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_LA57) ? 57 : 48;
  130. }
  131. static inline u64 get_canonical(u64 la, u8 vaddr_bits)
  132. {
  133. return ((int64_t)la << (64 - vaddr_bits)) >> (64 - vaddr_bits);
  134. }
  135. static inline bool is_noncanonical_address(u64 la, struct kvm_vcpu *vcpu)
  136. {
  137. #ifdef CONFIG_X86_64
  138. return get_canonical(la, vcpu_virt_addr_bits(vcpu)) != la;
  139. #else
  140. return false;
  141. #endif
  142. }
  143. static inline bool emul_is_noncanonical_address(u64 la,
  144. struct x86_emulate_ctxt *ctxt)
  145. {
  146. #ifdef CONFIG_X86_64
  147. return get_canonical(la, ctxt_virt_addr_bits(ctxt)) != la;
  148. #else
  149. return false;
  150. #endif
  151. }
  152. static inline void vcpu_cache_mmio_info(struct kvm_vcpu *vcpu,
  153. gva_t gva, gfn_t gfn, unsigned access)
  154. {
  155. u64 gen = kvm_memslots(vcpu->kvm)->generation;
  156. if (unlikely(gen & 1))
  157. return;
  158. /*
  159. * If this is a shadow nested page table, the "GVA" is
  160. * actually a nGPA.
  161. */
  162. vcpu->arch.mmio_gva = mmu_is_nested(vcpu) ? 0 : gva & PAGE_MASK;
  163. vcpu->arch.access = access;
  164. vcpu->arch.mmio_gfn = gfn;
  165. vcpu->arch.mmio_gen = gen;
  166. }
  167. static inline bool vcpu_match_mmio_gen(struct kvm_vcpu *vcpu)
  168. {
  169. return vcpu->arch.mmio_gen == kvm_memslots(vcpu->kvm)->generation;
  170. }
  171. /*
  172. * Clear the mmio cache info for the given gva. If gva is MMIO_GVA_ANY, we
  173. * clear all mmio cache info.
  174. */
  175. #define MMIO_GVA_ANY (~(gva_t)0)
  176. static inline void vcpu_clear_mmio_info(struct kvm_vcpu *vcpu, gva_t gva)
  177. {
  178. if (gva != MMIO_GVA_ANY && vcpu->arch.mmio_gva != (gva & PAGE_MASK))
  179. return;
  180. vcpu->arch.mmio_gva = 0;
  181. }
  182. static inline bool vcpu_match_mmio_gva(struct kvm_vcpu *vcpu, unsigned long gva)
  183. {
  184. if (vcpu_match_mmio_gen(vcpu) && vcpu->arch.mmio_gva &&
  185. vcpu->arch.mmio_gva == (gva & PAGE_MASK))
  186. return true;
  187. return false;
  188. }
  189. static inline bool vcpu_match_mmio_gpa(struct kvm_vcpu *vcpu, gpa_t gpa)
  190. {
  191. if (vcpu_match_mmio_gen(vcpu) && vcpu->arch.mmio_gfn &&
  192. vcpu->arch.mmio_gfn == gpa >> PAGE_SHIFT)
  193. return true;
  194. return false;
  195. }
  196. static inline unsigned long kvm_register_readl(struct kvm_vcpu *vcpu,
  197. enum kvm_reg reg)
  198. {
  199. unsigned long val = kvm_register_read(vcpu, reg);
  200. return is_64_bit_mode(vcpu) ? val : (u32)val;
  201. }
  202. static inline void kvm_register_writel(struct kvm_vcpu *vcpu,
  203. enum kvm_reg reg,
  204. unsigned long val)
  205. {
  206. if (!is_64_bit_mode(vcpu))
  207. val = (u32)val;
  208. return kvm_register_write(vcpu, reg, val);
  209. }
  210. static inline bool kvm_check_has_quirk(struct kvm *kvm, u64 quirk)
  211. {
  212. return !(kvm->arch.disabled_quirks & quirk);
  213. }
  214. void kvm_set_pending_timer(struct kvm_vcpu *vcpu);
  215. int kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip);
  216. void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr);
  217. u64 get_kvmclock_ns(struct kvm *kvm);
  218. int kvm_read_guest_virt(struct kvm_vcpu *vcpu,
  219. gva_t addr, void *val, unsigned int bytes,
  220. struct x86_exception *exception);
  221. int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu,
  222. gva_t addr, void *val, unsigned int bytes,
  223. struct x86_exception *exception);
  224. int handle_ud(struct kvm_vcpu *vcpu);
  225. void kvm_vcpu_mtrr_init(struct kvm_vcpu *vcpu);
  226. u8 kvm_mtrr_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn);
  227. bool kvm_mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data);
  228. int kvm_mtrr_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data);
  229. int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata);
  230. bool kvm_mtrr_check_gfn_range_consistency(struct kvm_vcpu *vcpu, gfn_t gfn,
  231. int page_num);
  232. bool kvm_vector_hashing_enabled(void);
  233. int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
  234. int emulation_type, void *insn, int insn_len);
  235. #define KVM_SUPPORTED_XCR0 (XFEATURE_MASK_FP | XFEATURE_MASK_SSE \
  236. | XFEATURE_MASK_YMM | XFEATURE_MASK_BNDREGS \
  237. | XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \
  238. | XFEATURE_MASK_PKRU)
  239. extern u64 host_xcr0;
  240. extern u64 kvm_supported_xcr0(void);
  241. extern unsigned int min_timer_period_us;
  242. extern unsigned int lapic_timer_advance_ns;
  243. extern bool enable_vmware_backdoor;
  244. extern struct static_key kvm_no_apic_vcpu;
  245. static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec)
  246. {
  247. return pvclock_scale_delta(nsec, vcpu->arch.virtual_tsc_mult,
  248. vcpu->arch.virtual_tsc_shift);
  249. }
  250. /* Same "calling convention" as do_div:
  251. * - divide (n << 32) by base
  252. * - put result in n
  253. * - return remainder
  254. */
  255. #define do_shl32_div32(n, base) \
  256. ({ \
  257. u32 __quot, __rem; \
  258. asm("divl %2" : "=a" (__quot), "=d" (__rem) \
  259. : "rm" (base), "0" (0), "1" ((u32) n)); \
  260. n = __quot; \
  261. __rem; \
  262. })
  263. static inline bool kvm_mwait_in_guest(struct kvm *kvm)
  264. {
  265. return kvm->arch.mwait_in_guest;
  266. }
  267. static inline bool kvm_hlt_in_guest(struct kvm *kvm)
  268. {
  269. return kvm->arch.hlt_in_guest;
  270. }
  271. static inline bool kvm_pause_in_guest(struct kvm *kvm)
  272. {
  273. return kvm->arch.pause_in_guest;
  274. }
  275. DECLARE_PER_CPU(struct kvm_vcpu *, current_vcpu);
  276. static inline void kvm_before_interrupt(struct kvm_vcpu *vcpu)
  277. {
  278. __this_cpu_write(current_vcpu, vcpu);
  279. }
  280. static inline void kvm_after_interrupt(struct kvm_vcpu *vcpu)
  281. {
  282. __this_cpu_write(current_vcpu, NULL);
  283. }
  284. static inline bool kvm_pat_valid(u64 data)
  285. {
  286. if (data & 0xF8F8F8F8F8F8F8F8ull)
  287. return false;
  288. /* 0, 1, 4, 5, 6, 7 are valid values. */
  289. return (data | ((data & 0x0202020202020202ull) << 1)) == data;
  290. }
  291. void kvm_load_guest_xcr0(struct kvm_vcpu *vcpu);
  292. void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu);
  293. #endif