x86.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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/fpu/xstate.h>
  6. #include <asm/mce.h>
  7. #include <asm/pvclock.h>
  8. #include "kvm_cache_regs.h"
  9. #include "kvm_emulate.h"
  10. #include "cpuid.h"
  11. struct kvm_caps {
  12. /* control of guest tsc rate supported? */
  13. bool has_tsc_control;
  14. /* maximum supported tsc_khz for guests */
  15. u32 max_guest_tsc_khz;
  16. /* number of bits of the fractional part of the TSC scaling ratio */
  17. u8 tsc_scaling_ratio_frac_bits;
  18. /* maximum allowed value of TSC scaling ratio */
  19. u64 max_tsc_scaling_ratio;
  20. /* 1ull << kvm_caps.tsc_scaling_ratio_frac_bits */
  21. u64 default_tsc_scaling_ratio;
  22. /* bus lock detection supported? */
  23. bool has_bus_lock_exit;
  24. /* notify VM exit supported? */
  25. bool has_notify_vmexit;
  26. /* bit mask of VM types */
  27. u32 supported_vm_types;
  28. u64 supported_mce_cap;
  29. u64 supported_xcr0;
  30. u64 supported_xss;
  31. u64 supported_perf_cap;
  32. };
  33. struct kvm_host_values {
  34. /*
  35. * The host's raw MAXPHYADDR, i.e. the number of non-reserved physical
  36. * address bits irrespective of features that repurpose legal bits,
  37. * e.g. MKTME.
  38. */
  39. u8 maxphyaddr;
  40. u64 efer;
  41. u64 xcr0;
  42. u64 xss;
  43. u64 arch_capabilities;
  44. };
  45. void kvm_spurious_fault(void);
  46. #define KVM_NESTED_VMENTER_CONSISTENCY_CHECK(consistency_check) \
  47. ({ \
  48. bool failed = (consistency_check); \
  49. if (failed) \
  50. trace_kvm_nested_vmenter_failed(#consistency_check, 0); \
  51. failed; \
  52. })
  53. /*
  54. * The first...last VMX feature MSRs that are emulated by KVM. This may or may
  55. * not cover all known VMX MSRs, as KVM doesn't emulate an MSR until there's an
  56. * associated feature that KVM supports for nested virtualization.
  57. */
  58. #define KVM_FIRST_EMULATED_VMX_MSR MSR_IA32_VMX_BASIC
  59. #define KVM_LAST_EMULATED_VMX_MSR MSR_IA32_VMX_VMFUNC
  60. #define KVM_DEFAULT_PLE_GAP 128
  61. #define KVM_VMX_DEFAULT_PLE_WINDOW 4096
  62. #define KVM_DEFAULT_PLE_WINDOW_GROW 2
  63. #define KVM_DEFAULT_PLE_WINDOW_SHRINK 0
  64. #define KVM_VMX_DEFAULT_PLE_WINDOW_MAX UINT_MAX
  65. #define KVM_SVM_DEFAULT_PLE_WINDOW_MAX USHRT_MAX
  66. #define KVM_SVM_DEFAULT_PLE_WINDOW 3000
  67. static inline unsigned int __grow_ple_window(unsigned int val,
  68. unsigned int base, unsigned int modifier, unsigned int max)
  69. {
  70. u64 ret = val;
  71. if (modifier < 1)
  72. return base;
  73. if (modifier < base)
  74. ret *= modifier;
  75. else
  76. ret += modifier;
  77. return min(ret, (u64)max);
  78. }
  79. static inline unsigned int __shrink_ple_window(unsigned int val,
  80. unsigned int base, unsigned int modifier, unsigned int min)
  81. {
  82. if (modifier < 1)
  83. return base;
  84. if (modifier < base)
  85. val /= modifier;
  86. else
  87. val -= modifier;
  88. return max(val, min);
  89. }
  90. #define MSR_IA32_CR_PAT_DEFAULT \
  91. PAT_VALUE(WB, WT, UC_MINUS, UC, WB, WT, UC_MINUS, UC)
  92. void kvm_service_local_tlb_flush_requests(struct kvm_vcpu *vcpu);
  93. int kvm_check_nested_events(struct kvm_vcpu *vcpu);
  94. /* Forcibly leave the nested mode in cases like a vCPU reset */
  95. static inline void kvm_leave_nested(struct kvm_vcpu *vcpu)
  96. {
  97. kvm_x86_ops.nested_ops->leave_nested(vcpu);
  98. }
  99. static inline bool kvm_vcpu_has_run(struct kvm_vcpu *vcpu)
  100. {
  101. return vcpu->arch.last_vmentry_cpu != -1;
  102. }
  103. static inline bool kvm_is_exception_pending(struct kvm_vcpu *vcpu)
  104. {
  105. return vcpu->arch.exception.pending ||
  106. vcpu->arch.exception_vmexit.pending ||
  107. kvm_test_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  108. }
  109. static inline void kvm_clear_exception_queue(struct kvm_vcpu *vcpu)
  110. {
  111. vcpu->arch.exception.pending = false;
  112. vcpu->arch.exception.injected = false;
  113. vcpu->arch.exception_vmexit.pending = false;
  114. }
  115. static inline void kvm_queue_interrupt(struct kvm_vcpu *vcpu, u8 vector,
  116. bool soft)
  117. {
  118. vcpu->arch.interrupt.injected = true;
  119. vcpu->arch.interrupt.soft = soft;
  120. vcpu->arch.interrupt.nr = vector;
  121. }
  122. static inline void kvm_clear_interrupt_queue(struct kvm_vcpu *vcpu)
  123. {
  124. vcpu->arch.interrupt.injected = false;
  125. }
  126. static inline bool kvm_event_needs_reinjection(struct kvm_vcpu *vcpu)
  127. {
  128. return vcpu->arch.exception.injected || vcpu->arch.interrupt.injected ||
  129. vcpu->arch.nmi_injected;
  130. }
  131. static inline bool kvm_exception_is_soft(unsigned int nr)
  132. {
  133. return (nr == BP_VECTOR) || (nr == OF_VECTOR);
  134. }
  135. static inline bool is_protmode(struct kvm_vcpu *vcpu)
  136. {
  137. return kvm_is_cr0_bit_set(vcpu, X86_CR0_PE);
  138. }
  139. static inline bool is_long_mode(struct kvm_vcpu *vcpu)
  140. {
  141. #ifdef CONFIG_X86_64
  142. return !!(vcpu->arch.efer & EFER_LMA);
  143. #else
  144. return false;
  145. #endif
  146. }
  147. static inline bool is_64_bit_mode(struct kvm_vcpu *vcpu)
  148. {
  149. int cs_db, cs_l;
  150. WARN_ON_ONCE(vcpu->arch.guest_state_protected);
  151. if (!is_long_mode(vcpu))
  152. return false;
  153. kvm_x86_call(get_cs_db_l_bits)(vcpu, &cs_db, &cs_l);
  154. return cs_l;
  155. }
  156. static inline bool is_64_bit_hypercall(struct kvm_vcpu *vcpu)
  157. {
  158. /*
  159. * If running with protected guest state, the CS register is not
  160. * accessible. The hypercall register values will have had to been
  161. * provided in 64-bit mode, so assume the guest is in 64-bit.
  162. */
  163. return vcpu->arch.guest_state_protected || is_64_bit_mode(vcpu);
  164. }
  165. static inline bool x86_exception_has_error_code(unsigned int vector)
  166. {
  167. static u32 exception_has_error_code = BIT(DF_VECTOR) | BIT(TS_VECTOR) |
  168. BIT(NP_VECTOR) | BIT(SS_VECTOR) | BIT(GP_VECTOR) |
  169. BIT(PF_VECTOR) | BIT(AC_VECTOR);
  170. return (1U << vector) & exception_has_error_code;
  171. }
  172. static inline bool mmu_is_nested(struct kvm_vcpu *vcpu)
  173. {
  174. return vcpu->arch.walk_mmu == &vcpu->arch.nested_mmu;
  175. }
  176. static inline bool is_pae(struct kvm_vcpu *vcpu)
  177. {
  178. return kvm_is_cr4_bit_set(vcpu, X86_CR4_PAE);
  179. }
  180. static inline bool is_pse(struct kvm_vcpu *vcpu)
  181. {
  182. return kvm_is_cr4_bit_set(vcpu, X86_CR4_PSE);
  183. }
  184. static inline bool is_paging(struct kvm_vcpu *vcpu)
  185. {
  186. return likely(kvm_is_cr0_bit_set(vcpu, X86_CR0_PG));
  187. }
  188. static inline bool is_pae_paging(struct kvm_vcpu *vcpu)
  189. {
  190. return !is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu);
  191. }
  192. static inline u8 vcpu_virt_addr_bits(struct kvm_vcpu *vcpu)
  193. {
  194. return kvm_is_cr4_bit_set(vcpu, X86_CR4_LA57) ? 57 : 48;
  195. }
  196. static inline u8 max_host_virt_addr_bits(void)
  197. {
  198. return kvm_cpu_cap_has(X86_FEATURE_LA57) ? 57 : 48;
  199. }
  200. /*
  201. * x86 MSRs which contain linear addresses, x86 hidden segment bases, and
  202. * IDT/GDT bases have static canonicality checks, the size of which depends
  203. * only on the CPU's support for 5-level paging, rather than on the state of
  204. * CR4.LA57. This applies to both WRMSR and to other instructions that set
  205. * their values, e.g. SGDT.
  206. *
  207. * KVM passes through most of these MSRS and also doesn't intercept the
  208. * instructions that set the hidden segment bases.
  209. *
  210. * Because of this, to be consistent with hardware, even if the guest doesn't
  211. * have LA57 enabled in its CPUID, perform canonicality checks based on *host*
  212. * support for 5 level paging.
  213. *
  214. * Finally, instructions which are related to MMU invalidation of a given
  215. * linear address, also have a similar static canonical check on address.
  216. * This allows for example to invalidate 5-level addresses of a guest from a
  217. * host which uses 4-level paging.
  218. */
  219. static inline bool is_noncanonical_address(u64 la, struct kvm_vcpu *vcpu,
  220. unsigned int flags)
  221. {
  222. if (flags & (X86EMUL_F_INVLPG | X86EMUL_F_MSR | X86EMUL_F_DT_LOAD))
  223. return !__is_canonical_address(la, max_host_virt_addr_bits());
  224. else
  225. return !__is_canonical_address(la, vcpu_virt_addr_bits(vcpu));
  226. }
  227. static inline bool is_noncanonical_msr_address(u64 la, struct kvm_vcpu *vcpu)
  228. {
  229. return is_noncanonical_address(la, vcpu, X86EMUL_F_MSR);
  230. }
  231. static inline bool is_noncanonical_base_address(u64 la, struct kvm_vcpu *vcpu)
  232. {
  233. return is_noncanonical_address(la, vcpu, X86EMUL_F_DT_LOAD);
  234. }
  235. static inline bool is_noncanonical_invlpg_address(u64 la, struct kvm_vcpu *vcpu)
  236. {
  237. return is_noncanonical_address(la, vcpu, X86EMUL_F_INVLPG);
  238. }
  239. static inline void vcpu_cache_mmio_info(struct kvm_vcpu *vcpu,
  240. gva_t gva, gfn_t gfn, unsigned access)
  241. {
  242. u64 gen = kvm_memslots(vcpu->kvm)->generation;
  243. if (unlikely(gen & KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS))
  244. return;
  245. /*
  246. * If this is a shadow nested page table, the "GVA" is
  247. * actually a nGPA.
  248. */
  249. vcpu->arch.mmio_gva = mmu_is_nested(vcpu) ? 0 : gva & PAGE_MASK;
  250. vcpu->arch.mmio_access = access;
  251. vcpu->arch.mmio_gfn = gfn;
  252. vcpu->arch.mmio_gen = gen;
  253. }
  254. static inline bool vcpu_match_mmio_gen(struct kvm_vcpu *vcpu)
  255. {
  256. return vcpu->arch.mmio_gen == kvm_memslots(vcpu->kvm)->generation;
  257. }
  258. /*
  259. * Clear the mmio cache info for the given gva. If gva is MMIO_GVA_ANY, we
  260. * clear all mmio cache info.
  261. */
  262. #define MMIO_GVA_ANY (~(gva_t)0)
  263. static inline void vcpu_clear_mmio_info(struct kvm_vcpu *vcpu, gva_t gva)
  264. {
  265. if (gva != MMIO_GVA_ANY && vcpu->arch.mmio_gva != (gva & PAGE_MASK))
  266. return;
  267. vcpu->arch.mmio_gva = 0;
  268. }
  269. static inline bool vcpu_match_mmio_gva(struct kvm_vcpu *vcpu, unsigned long gva)
  270. {
  271. if (vcpu_match_mmio_gen(vcpu) && vcpu->arch.mmio_gva &&
  272. vcpu->arch.mmio_gva == (gva & PAGE_MASK))
  273. return true;
  274. return false;
  275. }
  276. static inline bool vcpu_match_mmio_gpa(struct kvm_vcpu *vcpu, gpa_t gpa)
  277. {
  278. if (vcpu_match_mmio_gen(vcpu) && vcpu->arch.mmio_gfn &&
  279. vcpu->arch.mmio_gfn == gpa >> PAGE_SHIFT)
  280. return true;
  281. return false;
  282. }
  283. static inline unsigned long kvm_register_read(struct kvm_vcpu *vcpu, int reg)
  284. {
  285. unsigned long val = kvm_register_read_raw(vcpu, reg);
  286. return is_64_bit_mode(vcpu) ? val : (u32)val;
  287. }
  288. static inline void kvm_register_write(struct kvm_vcpu *vcpu,
  289. int reg, unsigned long val)
  290. {
  291. if (!is_64_bit_mode(vcpu))
  292. val = (u32)val;
  293. return kvm_register_write_raw(vcpu, reg, val);
  294. }
  295. static inline bool kvm_check_has_quirk(struct kvm *kvm, u64 quirk)
  296. {
  297. return !(kvm->arch.disabled_quirks & quirk);
  298. }
  299. void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip);
  300. u64 get_kvmclock_ns(struct kvm *kvm);
  301. uint64_t kvm_get_wall_clock_epoch(struct kvm *kvm);
  302. bool kvm_get_monotonic_and_clockread(s64 *kernel_ns, u64 *tsc_timestamp);
  303. int kvm_read_guest_virt(struct kvm_vcpu *vcpu,
  304. gva_t addr, void *val, unsigned int bytes,
  305. struct x86_exception *exception);
  306. int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu,
  307. gva_t addr, void *val, unsigned int bytes,
  308. struct x86_exception *exception);
  309. int handle_ud(struct kvm_vcpu *vcpu);
  310. void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu,
  311. struct kvm_queued_exception *ex);
  312. int kvm_mtrr_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data);
  313. int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata);
  314. bool kvm_vector_hashing_enabled(void);
  315. void kvm_fixup_and_inject_pf_error(struct kvm_vcpu *vcpu, gva_t gva, u16 error_code);
  316. int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type,
  317. void *insn, int insn_len);
  318. int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
  319. int emulation_type, void *insn, int insn_len);
  320. fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu);
  321. fastpath_t handle_fastpath_hlt(struct kvm_vcpu *vcpu);
  322. extern struct kvm_caps kvm_caps;
  323. extern struct kvm_host_values kvm_host;
  324. extern bool enable_pmu;
  325. /*
  326. * Get a filtered version of KVM's supported XCR0 that strips out dynamic
  327. * features for which the current process doesn't (yet) have permission to use.
  328. * This is intended to be used only when enumerating support to userspace,
  329. * e.g. in KVM_GET_SUPPORTED_CPUID and KVM_CAP_XSAVE2, it does NOT need to be
  330. * used to check/restrict guest behavior as KVM rejects KVM_SET_CPUID{2} if
  331. * userspace attempts to enable unpermitted features.
  332. */
  333. static inline u64 kvm_get_filtered_xcr0(void)
  334. {
  335. u64 permitted_xcr0 = kvm_caps.supported_xcr0;
  336. BUILD_BUG_ON(XFEATURE_MASK_USER_DYNAMIC != XFEATURE_MASK_XTILE_DATA);
  337. if (permitted_xcr0 & XFEATURE_MASK_USER_DYNAMIC) {
  338. permitted_xcr0 &= xstate_get_guest_group_perm();
  339. /*
  340. * Treat XTILE_CFG as unsupported if the current process isn't
  341. * allowed to use XTILE_DATA, as attempting to set XTILE_CFG in
  342. * XCR0 without setting XTILE_DATA is architecturally illegal.
  343. */
  344. if (!(permitted_xcr0 & XFEATURE_MASK_XTILE_DATA))
  345. permitted_xcr0 &= ~XFEATURE_MASK_XTILE_CFG;
  346. }
  347. return permitted_xcr0;
  348. }
  349. static inline bool kvm_mpx_supported(void)
  350. {
  351. return (kvm_caps.supported_xcr0 & (XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR))
  352. == (XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR);
  353. }
  354. extern unsigned int min_timer_period_us;
  355. extern bool enable_vmware_backdoor;
  356. extern int pi_inject_timer;
  357. extern bool report_ignored_msrs;
  358. extern bool eager_page_split;
  359. static inline void kvm_pr_unimpl_wrmsr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  360. {
  361. if (report_ignored_msrs)
  362. vcpu_unimpl(vcpu, "Unhandled WRMSR(0x%x) = 0x%llx\n", msr, data);
  363. }
  364. static inline void kvm_pr_unimpl_rdmsr(struct kvm_vcpu *vcpu, u32 msr)
  365. {
  366. if (report_ignored_msrs)
  367. vcpu_unimpl(vcpu, "Unhandled RDMSR(0x%x)\n", msr);
  368. }
  369. static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec)
  370. {
  371. return pvclock_scale_delta(nsec, vcpu->arch.virtual_tsc_mult,
  372. vcpu->arch.virtual_tsc_shift);
  373. }
  374. /* Same "calling convention" as do_div:
  375. * - divide (n << 32) by base
  376. * - put result in n
  377. * - return remainder
  378. */
  379. #define do_shl32_div32(n, base) \
  380. ({ \
  381. u32 __quot, __rem; \
  382. asm("divl %2" : "=a" (__quot), "=d" (__rem) \
  383. : "rm" (base), "0" (0), "1" ((u32) n)); \
  384. n = __quot; \
  385. __rem; \
  386. })
  387. static inline bool kvm_mwait_in_guest(struct kvm *kvm)
  388. {
  389. return kvm->arch.mwait_in_guest;
  390. }
  391. static inline bool kvm_hlt_in_guest(struct kvm *kvm)
  392. {
  393. return kvm->arch.hlt_in_guest;
  394. }
  395. static inline bool kvm_pause_in_guest(struct kvm *kvm)
  396. {
  397. return kvm->arch.pause_in_guest;
  398. }
  399. static inline bool kvm_cstate_in_guest(struct kvm *kvm)
  400. {
  401. return kvm->arch.cstate_in_guest;
  402. }
  403. static inline bool kvm_notify_vmexit_enabled(struct kvm *kvm)
  404. {
  405. return kvm->arch.notify_vmexit_flags & KVM_X86_NOTIFY_VMEXIT_ENABLED;
  406. }
  407. static __always_inline void kvm_before_interrupt(struct kvm_vcpu *vcpu,
  408. enum kvm_intr_type intr)
  409. {
  410. WRITE_ONCE(vcpu->arch.handling_intr_from_guest, (u8)intr);
  411. }
  412. static __always_inline void kvm_after_interrupt(struct kvm_vcpu *vcpu)
  413. {
  414. WRITE_ONCE(vcpu->arch.handling_intr_from_guest, 0);
  415. }
  416. static inline bool kvm_handling_nmi_from_guest(struct kvm_vcpu *vcpu)
  417. {
  418. return vcpu->arch.handling_intr_from_guest == KVM_HANDLING_NMI;
  419. }
  420. static inline bool kvm_pat_valid(u64 data)
  421. {
  422. if (data & 0xF8F8F8F8F8F8F8F8ull)
  423. return false;
  424. /* 0, 1, 4, 5, 6, 7 are valid values. */
  425. return (data | ((data & 0x0202020202020202ull) << 1)) == data;
  426. }
  427. static inline bool kvm_dr7_valid(u64 data)
  428. {
  429. /* Bits [63:32] are reserved */
  430. return !(data >> 32);
  431. }
  432. static inline bool kvm_dr6_valid(u64 data)
  433. {
  434. /* Bits [63:32] are reserved */
  435. return !(data >> 32);
  436. }
  437. /*
  438. * Trigger machine check on the host. We assume all the MSRs are already set up
  439. * by the CPU and that we still run on the same CPU as the MCE occurred on.
  440. * We pass a fake environment to the machine check handler because we want
  441. * the guest to be always treated like user space, no matter what context
  442. * it used internally.
  443. */
  444. static inline void kvm_machine_check(void)
  445. {
  446. #if defined(CONFIG_X86_MCE)
  447. struct pt_regs regs = {
  448. .cs = 3, /* Fake ring 3 no matter what the guest ran on */
  449. .flags = X86_EFLAGS_IF,
  450. };
  451. do_machine_check(&regs);
  452. #endif
  453. }
  454. void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu);
  455. void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu);
  456. int kvm_spec_ctrl_test_value(u64 value);
  457. bool __kvm_is_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
  458. int kvm_handle_memory_failure(struct kvm_vcpu *vcpu, int r,
  459. struct x86_exception *e);
  460. int kvm_handle_invpcid(struct kvm_vcpu *vcpu, unsigned long type, gva_t gva);
  461. bool kvm_msr_allowed(struct kvm_vcpu *vcpu, u32 index, u32 type);
  462. enum kvm_msr_access {
  463. MSR_TYPE_R = BIT(0),
  464. MSR_TYPE_W = BIT(1),
  465. MSR_TYPE_RW = MSR_TYPE_R | MSR_TYPE_W,
  466. };
  467. /*
  468. * Internal error codes that are used to indicate that MSR emulation encountered
  469. * an error that should result in #GP in the guest, unless userspace handles it.
  470. * Note, '1', '0', and negative numbers are off limits, as they are used by KVM
  471. * as part of KVM's lightly documented internal KVM_RUN return codes.
  472. *
  473. * UNSUPPORTED - The MSR isn't supported, either because it is completely
  474. * unknown to KVM, or because the MSR should not exist according
  475. * to the vCPU model.
  476. *
  477. * FILTERED - Access to the MSR is denied by a userspace MSR filter.
  478. */
  479. #define KVM_MSR_RET_UNSUPPORTED 2
  480. #define KVM_MSR_RET_FILTERED 3
  481. #define __cr4_reserved_bits(__cpu_has, __c) \
  482. ({ \
  483. u64 __reserved_bits = CR4_RESERVED_BITS; \
  484. \
  485. if (!__cpu_has(__c, X86_FEATURE_XSAVE)) \
  486. __reserved_bits |= X86_CR4_OSXSAVE; \
  487. if (!__cpu_has(__c, X86_FEATURE_SMEP)) \
  488. __reserved_bits |= X86_CR4_SMEP; \
  489. if (!__cpu_has(__c, X86_FEATURE_SMAP)) \
  490. __reserved_bits |= X86_CR4_SMAP; \
  491. if (!__cpu_has(__c, X86_FEATURE_FSGSBASE)) \
  492. __reserved_bits |= X86_CR4_FSGSBASE; \
  493. if (!__cpu_has(__c, X86_FEATURE_PKU)) \
  494. __reserved_bits |= X86_CR4_PKE; \
  495. if (!__cpu_has(__c, X86_FEATURE_LA57)) \
  496. __reserved_bits |= X86_CR4_LA57; \
  497. if (!__cpu_has(__c, X86_FEATURE_UMIP)) \
  498. __reserved_bits |= X86_CR4_UMIP; \
  499. if (!__cpu_has(__c, X86_FEATURE_VMX)) \
  500. __reserved_bits |= X86_CR4_VMXE; \
  501. if (!__cpu_has(__c, X86_FEATURE_PCID)) \
  502. __reserved_bits |= X86_CR4_PCIDE; \
  503. if (!__cpu_has(__c, X86_FEATURE_LAM)) \
  504. __reserved_bits |= X86_CR4_LAM_SUP; \
  505. __reserved_bits; \
  506. })
  507. int kvm_sev_es_mmio_write(struct kvm_vcpu *vcpu, gpa_t src, unsigned int bytes,
  508. void *dst);
  509. int kvm_sev_es_mmio_read(struct kvm_vcpu *vcpu, gpa_t src, unsigned int bytes,
  510. void *dst);
  511. int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size,
  512. unsigned int port, void *data, unsigned int count,
  513. int in);
  514. #endif