x86.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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/mce.h>
  6. #include <asm/pvclock.h>
  7. #include "kvm_cache_regs.h"
  8. #include "kvm_emulate.h"
  9. struct kvm_caps {
  10. /* control of guest tsc rate supported? */
  11. bool has_tsc_control;
  12. /* maximum supported tsc_khz for guests */
  13. u32 max_guest_tsc_khz;
  14. /* number of bits of the fractional part of the TSC scaling ratio */
  15. u8 tsc_scaling_ratio_frac_bits;
  16. /* maximum allowed value of TSC scaling ratio */
  17. u64 max_tsc_scaling_ratio;
  18. /* 1ull << kvm_caps.tsc_scaling_ratio_frac_bits */
  19. u64 default_tsc_scaling_ratio;
  20. /* bus lock detection supported? */
  21. bool has_bus_lock_exit;
  22. /* notify VM exit supported? */
  23. bool has_notify_vmexit;
  24. u64 supported_mce_cap;
  25. u64 supported_xcr0;
  26. u64 supported_xss;
  27. u64 supported_perf_cap;
  28. };
  29. void kvm_spurious_fault(void);
  30. #define KVM_NESTED_VMENTER_CONSISTENCY_CHECK(consistency_check) \
  31. ({ \
  32. bool failed = (consistency_check); \
  33. if (failed) \
  34. trace_kvm_nested_vmenter_failed(#consistency_check, 0); \
  35. failed; \
  36. })
  37. #define KVM_DEFAULT_PLE_GAP 128
  38. #define KVM_VMX_DEFAULT_PLE_WINDOW 4096
  39. #define KVM_DEFAULT_PLE_WINDOW_GROW 2
  40. #define KVM_DEFAULT_PLE_WINDOW_SHRINK 0
  41. #define KVM_VMX_DEFAULT_PLE_WINDOW_MAX UINT_MAX
  42. #define KVM_SVM_DEFAULT_PLE_WINDOW_MAX USHRT_MAX
  43. #define KVM_SVM_DEFAULT_PLE_WINDOW 3000
  44. static inline unsigned int __grow_ple_window(unsigned int val,
  45. unsigned int base, unsigned int modifier, unsigned int max)
  46. {
  47. u64 ret = val;
  48. if (modifier < 1)
  49. return base;
  50. if (modifier < base)
  51. ret *= modifier;
  52. else
  53. ret += modifier;
  54. return min(ret, (u64)max);
  55. }
  56. static inline unsigned int __shrink_ple_window(unsigned int val,
  57. unsigned int base, unsigned int modifier, unsigned int min)
  58. {
  59. if (modifier < 1)
  60. return base;
  61. if (modifier < base)
  62. val /= modifier;
  63. else
  64. val -= modifier;
  65. return max(val, min);
  66. }
  67. #define MSR_IA32_CR_PAT_DEFAULT 0x0007040600070406ULL
  68. void kvm_service_local_tlb_flush_requests(struct kvm_vcpu *vcpu);
  69. int kvm_check_nested_events(struct kvm_vcpu *vcpu);
  70. static inline bool kvm_is_exception_pending(struct kvm_vcpu *vcpu)
  71. {
  72. return vcpu->arch.exception.pending ||
  73. vcpu->arch.exception_vmexit.pending ||
  74. kvm_test_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  75. }
  76. static inline void kvm_clear_exception_queue(struct kvm_vcpu *vcpu)
  77. {
  78. vcpu->arch.exception.pending = false;
  79. vcpu->arch.exception.injected = false;
  80. vcpu->arch.exception_vmexit.pending = false;
  81. }
  82. static inline void kvm_queue_interrupt(struct kvm_vcpu *vcpu, u8 vector,
  83. bool soft)
  84. {
  85. vcpu->arch.interrupt.injected = true;
  86. vcpu->arch.interrupt.soft = soft;
  87. vcpu->arch.interrupt.nr = vector;
  88. }
  89. static inline void kvm_clear_interrupt_queue(struct kvm_vcpu *vcpu)
  90. {
  91. vcpu->arch.interrupt.injected = false;
  92. }
  93. static inline bool kvm_event_needs_reinjection(struct kvm_vcpu *vcpu)
  94. {
  95. return vcpu->arch.exception.injected || vcpu->arch.interrupt.injected ||
  96. vcpu->arch.nmi_injected;
  97. }
  98. static inline bool kvm_exception_is_soft(unsigned int nr)
  99. {
  100. return (nr == BP_VECTOR) || (nr == OF_VECTOR);
  101. }
  102. static inline bool is_protmode(struct kvm_vcpu *vcpu)
  103. {
  104. return kvm_read_cr0_bits(vcpu, X86_CR0_PE);
  105. }
  106. static inline int is_long_mode(struct kvm_vcpu *vcpu)
  107. {
  108. #ifdef CONFIG_X86_64
  109. return vcpu->arch.efer & EFER_LMA;
  110. #else
  111. return 0;
  112. #endif
  113. }
  114. static inline bool is_64_bit_mode(struct kvm_vcpu *vcpu)
  115. {
  116. int cs_db, cs_l;
  117. WARN_ON_ONCE(vcpu->arch.guest_state_protected);
  118. if (!is_long_mode(vcpu))
  119. return false;
  120. static_call(kvm_x86_get_cs_db_l_bits)(vcpu, &cs_db, &cs_l);
  121. return cs_l;
  122. }
  123. static inline bool is_64_bit_hypercall(struct kvm_vcpu *vcpu)
  124. {
  125. /*
  126. * If running with protected guest state, the CS register is not
  127. * accessible. The hypercall register values will have had to been
  128. * provided in 64-bit mode, so assume the guest is in 64-bit.
  129. */
  130. return vcpu->arch.guest_state_protected || is_64_bit_mode(vcpu);
  131. }
  132. static inline bool x86_exception_has_error_code(unsigned int vector)
  133. {
  134. static u32 exception_has_error_code = BIT(DF_VECTOR) | BIT(TS_VECTOR) |
  135. BIT(NP_VECTOR) | BIT(SS_VECTOR) | BIT(GP_VECTOR) |
  136. BIT(PF_VECTOR) | BIT(AC_VECTOR);
  137. return (1U << vector) & exception_has_error_code;
  138. }
  139. static inline bool mmu_is_nested(struct kvm_vcpu *vcpu)
  140. {
  141. return vcpu->arch.walk_mmu == &vcpu->arch.nested_mmu;
  142. }
  143. static inline int is_pae(struct kvm_vcpu *vcpu)
  144. {
  145. return kvm_read_cr4_bits(vcpu, X86_CR4_PAE);
  146. }
  147. static inline int is_pse(struct kvm_vcpu *vcpu)
  148. {
  149. return kvm_read_cr4_bits(vcpu, X86_CR4_PSE);
  150. }
  151. static inline int is_paging(struct kvm_vcpu *vcpu)
  152. {
  153. return likely(kvm_read_cr0_bits(vcpu, X86_CR0_PG));
  154. }
  155. static inline bool is_pae_paging(struct kvm_vcpu *vcpu)
  156. {
  157. return !is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu);
  158. }
  159. static inline u8 vcpu_virt_addr_bits(struct kvm_vcpu *vcpu)
  160. {
  161. return kvm_read_cr4_bits(vcpu, X86_CR4_LA57) ? 57 : 48;
  162. }
  163. static inline bool is_noncanonical_address(u64 la, struct kvm_vcpu *vcpu)
  164. {
  165. return !__is_canonical_address(la, vcpu_virt_addr_bits(vcpu));
  166. }
  167. static inline void vcpu_cache_mmio_info(struct kvm_vcpu *vcpu,
  168. gva_t gva, gfn_t gfn, unsigned access)
  169. {
  170. u64 gen = kvm_memslots(vcpu->kvm)->generation;
  171. if (unlikely(gen & KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS))
  172. return;
  173. /*
  174. * If this is a shadow nested page table, the "GVA" is
  175. * actually a nGPA.
  176. */
  177. vcpu->arch.mmio_gva = mmu_is_nested(vcpu) ? 0 : gva & PAGE_MASK;
  178. vcpu->arch.mmio_access = access;
  179. vcpu->arch.mmio_gfn = gfn;
  180. vcpu->arch.mmio_gen = gen;
  181. }
  182. static inline bool vcpu_match_mmio_gen(struct kvm_vcpu *vcpu)
  183. {
  184. return vcpu->arch.mmio_gen == kvm_memslots(vcpu->kvm)->generation;
  185. }
  186. /*
  187. * Clear the mmio cache info for the given gva. If gva is MMIO_GVA_ANY, we
  188. * clear all mmio cache info.
  189. */
  190. #define MMIO_GVA_ANY (~(gva_t)0)
  191. static inline void vcpu_clear_mmio_info(struct kvm_vcpu *vcpu, gva_t gva)
  192. {
  193. if (gva != MMIO_GVA_ANY && vcpu->arch.mmio_gva != (gva & PAGE_MASK))
  194. return;
  195. vcpu->arch.mmio_gva = 0;
  196. }
  197. static inline bool vcpu_match_mmio_gva(struct kvm_vcpu *vcpu, unsigned long gva)
  198. {
  199. if (vcpu_match_mmio_gen(vcpu) && vcpu->arch.mmio_gva &&
  200. vcpu->arch.mmio_gva == (gva & PAGE_MASK))
  201. return true;
  202. return false;
  203. }
  204. static inline bool vcpu_match_mmio_gpa(struct kvm_vcpu *vcpu, gpa_t gpa)
  205. {
  206. if (vcpu_match_mmio_gen(vcpu) && vcpu->arch.mmio_gfn &&
  207. vcpu->arch.mmio_gfn == gpa >> PAGE_SHIFT)
  208. return true;
  209. return false;
  210. }
  211. static inline unsigned long kvm_register_read(struct kvm_vcpu *vcpu, int reg)
  212. {
  213. unsigned long val = kvm_register_read_raw(vcpu, reg);
  214. return is_64_bit_mode(vcpu) ? val : (u32)val;
  215. }
  216. static inline void kvm_register_write(struct kvm_vcpu *vcpu,
  217. int reg, unsigned long val)
  218. {
  219. if (!is_64_bit_mode(vcpu))
  220. val = (u32)val;
  221. return kvm_register_write_raw(vcpu, reg, val);
  222. }
  223. static inline bool kvm_check_has_quirk(struct kvm *kvm, u64 quirk)
  224. {
  225. return !(kvm->arch.disabled_quirks & quirk);
  226. }
  227. void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip);
  228. u64 get_kvmclock_ns(struct kvm *kvm);
  229. int kvm_read_guest_virt(struct kvm_vcpu *vcpu,
  230. gva_t addr, void *val, unsigned int bytes,
  231. struct x86_exception *exception);
  232. int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu,
  233. gva_t addr, void *val, unsigned int bytes,
  234. struct x86_exception *exception);
  235. int handle_ud(struct kvm_vcpu *vcpu);
  236. void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu,
  237. struct kvm_queued_exception *ex);
  238. void kvm_vcpu_mtrr_init(struct kvm_vcpu *vcpu);
  239. u8 kvm_mtrr_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn);
  240. bool kvm_mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data);
  241. int kvm_mtrr_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data);
  242. int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata);
  243. bool kvm_mtrr_check_gfn_range_consistency(struct kvm_vcpu *vcpu, gfn_t gfn,
  244. int page_num);
  245. bool kvm_vector_hashing_enabled(void);
  246. void kvm_fixup_and_inject_pf_error(struct kvm_vcpu *vcpu, gva_t gva, u16 error_code);
  247. int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type,
  248. void *insn, int insn_len);
  249. int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
  250. int emulation_type, void *insn, int insn_len);
  251. fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu);
  252. extern u64 host_xcr0;
  253. extern u64 host_xss;
  254. extern struct kvm_caps kvm_caps;
  255. extern bool enable_pmu;
  256. static inline bool kvm_mpx_supported(void)
  257. {
  258. return (kvm_caps.supported_xcr0 & (XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR))
  259. == (XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR);
  260. }
  261. extern unsigned int min_timer_period_us;
  262. extern bool enable_vmware_backdoor;
  263. extern int pi_inject_timer;
  264. extern bool report_ignored_msrs;
  265. extern bool eager_page_split;
  266. static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec)
  267. {
  268. return pvclock_scale_delta(nsec, vcpu->arch.virtual_tsc_mult,
  269. vcpu->arch.virtual_tsc_shift);
  270. }
  271. /* Same "calling convention" as do_div:
  272. * - divide (n << 32) by base
  273. * - put result in n
  274. * - return remainder
  275. */
  276. #define do_shl32_div32(n, base) \
  277. ({ \
  278. u32 __quot, __rem; \
  279. asm("divl %2" : "=a" (__quot), "=d" (__rem) \
  280. : "rm" (base), "0" (0), "1" ((u32) n)); \
  281. n = __quot; \
  282. __rem; \
  283. })
  284. static inline bool kvm_mwait_in_guest(struct kvm *kvm)
  285. {
  286. return kvm->arch.mwait_in_guest;
  287. }
  288. static inline bool kvm_hlt_in_guest(struct kvm *kvm)
  289. {
  290. return kvm->arch.hlt_in_guest;
  291. }
  292. static inline bool kvm_pause_in_guest(struct kvm *kvm)
  293. {
  294. return kvm->arch.pause_in_guest;
  295. }
  296. static inline bool kvm_cstate_in_guest(struct kvm *kvm)
  297. {
  298. return kvm->arch.cstate_in_guest;
  299. }
  300. static inline bool kvm_notify_vmexit_enabled(struct kvm *kvm)
  301. {
  302. return kvm->arch.notify_vmexit_flags & KVM_X86_NOTIFY_VMEXIT_ENABLED;
  303. }
  304. enum kvm_intr_type {
  305. /* Values are arbitrary, but must be non-zero. */
  306. KVM_HANDLING_IRQ = 1,
  307. KVM_HANDLING_NMI,
  308. };
  309. static inline void kvm_before_interrupt(struct kvm_vcpu *vcpu,
  310. enum kvm_intr_type intr)
  311. {
  312. WRITE_ONCE(vcpu->arch.handling_intr_from_guest, (u8)intr);
  313. }
  314. static inline void kvm_after_interrupt(struct kvm_vcpu *vcpu)
  315. {
  316. WRITE_ONCE(vcpu->arch.handling_intr_from_guest, 0);
  317. }
  318. static inline bool kvm_handling_nmi_from_guest(struct kvm_vcpu *vcpu)
  319. {
  320. return vcpu->arch.handling_intr_from_guest == KVM_HANDLING_NMI;
  321. }
  322. static inline bool kvm_pat_valid(u64 data)
  323. {
  324. if (data & 0xF8F8F8F8F8F8F8F8ull)
  325. return false;
  326. /* 0, 1, 4, 5, 6, 7 are valid values. */
  327. return (data | ((data & 0x0202020202020202ull) << 1)) == data;
  328. }
  329. static inline bool kvm_dr7_valid(u64 data)
  330. {
  331. /* Bits [63:32] are reserved */
  332. return !(data >> 32);
  333. }
  334. static inline bool kvm_dr6_valid(u64 data)
  335. {
  336. /* Bits [63:32] are reserved */
  337. return !(data >> 32);
  338. }
  339. /*
  340. * Trigger machine check on the host. We assume all the MSRs are already set up
  341. * by the CPU and that we still run on the same CPU as the MCE occurred on.
  342. * We pass a fake environment to the machine check handler because we want
  343. * the guest to be always treated like user space, no matter what context
  344. * it used internally.
  345. */
  346. static inline void kvm_machine_check(void)
  347. {
  348. #if defined(CONFIG_X86_MCE)
  349. struct pt_regs regs = {
  350. .cs = 3, /* Fake ring 3 no matter what the guest ran on */
  351. .flags = X86_EFLAGS_IF,
  352. };
  353. do_machine_check(&regs);
  354. #endif
  355. }
  356. void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu);
  357. void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu);
  358. int kvm_spec_ctrl_test_value(u64 value);
  359. bool __kvm_is_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
  360. int kvm_handle_memory_failure(struct kvm_vcpu *vcpu, int r,
  361. struct x86_exception *e);
  362. int kvm_handle_invpcid(struct kvm_vcpu *vcpu, unsigned long type, gva_t gva);
  363. bool kvm_msr_allowed(struct kvm_vcpu *vcpu, u32 index, u32 type);
  364. /*
  365. * Internal error codes that are used to indicate that MSR emulation encountered
  366. * an error that should result in #GP in the guest, unless userspace
  367. * handles it.
  368. */
  369. #define KVM_MSR_RET_INVALID 2 /* in-kernel MSR emulation #GP condition */
  370. #define KVM_MSR_RET_FILTERED 3 /* #GP due to userspace MSR filter */
  371. #define __cr4_reserved_bits(__cpu_has, __c) \
  372. ({ \
  373. u64 __reserved_bits = CR4_RESERVED_BITS; \
  374. \
  375. if (!__cpu_has(__c, X86_FEATURE_XSAVE)) \
  376. __reserved_bits |= X86_CR4_OSXSAVE; \
  377. if (!__cpu_has(__c, X86_FEATURE_SMEP)) \
  378. __reserved_bits |= X86_CR4_SMEP; \
  379. if (!__cpu_has(__c, X86_FEATURE_SMAP)) \
  380. __reserved_bits |= X86_CR4_SMAP; \
  381. if (!__cpu_has(__c, X86_FEATURE_FSGSBASE)) \
  382. __reserved_bits |= X86_CR4_FSGSBASE; \
  383. if (!__cpu_has(__c, X86_FEATURE_PKU)) \
  384. __reserved_bits |= X86_CR4_PKE; \
  385. if (!__cpu_has(__c, X86_FEATURE_LA57)) \
  386. __reserved_bits |= X86_CR4_LA57; \
  387. if (!__cpu_has(__c, X86_FEATURE_UMIP)) \
  388. __reserved_bits |= X86_CR4_UMIP; \
  389. if (!__cpu_has(__c, X86_FEATURE_VMX)) \
  390. __reserved_bits |= X86_CR4_VMXE; \
  391. if (!__cpu_has(__c, X86_FEATURE_PCID)) \
  392. __reserved_bits |= X86_CR4_PCIDE; \
  393. __reserved_bits; \
  394. })
  395. int kvm_sev_es_mmio_write(struct kvm_vcpu *vcpu, gpa_t src, unsigned int bytes,
  396. void *dst);
  397. int kvm_sev_es_mmio_read(struct kvm_vcpu *vcpu, gpa_t src, unsigned int bytes,
  398. void *dst);
  399. int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size,
  400. unsigned int port, void *data, unsigned int count,
  401. int in);
  402. #endif