svm.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Kernel-based Virtual Machine driver for Linux
  4. *
  5. * AMD SVM support
  6. *
  7. * Copyright (C) 2006 Qumranet, Inc.
  8. * Copyright 2010 Red Hat, Inc. and/or its affiliates.
  9. *
  10. * Authors:
  11. * Yaniv Kamay <[email protected]>
  12. * Avi Kivity <[email protected]>
  13. */
  14. #ifndef __SVM_SVM_H
  15. #define __SVM_SVM_H
  16. #include <linux/kvm_types.h>
  17. #include <linux/kvm_host.h>
  18. #include <linux/bits.h>
  19. #include <asm/svm.h>
  20. #include <asm/sev-common.h>
  21. #include "kvm_cache_regs.h"
  22. #define __sme_page_pa(x) __sme_set(page_to_pfn(x) << PAGE_SHIFT)
  23. #define IOPM_SIZE PAGE_SIZE * 3
  24. #define MSRPM_SIZE PAGE_SIZE * 2
  25. #define MAX_DIRECT_ACCESS_MSRS 46
  26. #define MSRPM_OFFSETS 32
  27. extern u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
  28. extern bool npt_enabled;
  29. extern int vgif;
  30. extern bool intercept_smi;
  31. enum avic_modes {
  32. AVIC_MODE_NONE = 0,
  33. AVIC_MODE_X1,
  34. AVIC_MODE_X2,
  35. };
  36. extern enum avic_modes avic_mode;
  37. /*
  38. * Clean bits in VMCB.
  39. * VMCB_ALL_CLEAN_MASK might also need to
  40. * be updated if this enum is modified.
  41. */
  42. enum {
  43. VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
  44. pause filter count */
  45. VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
  46. VMCB_ASID, /* ASID */
  47. VMCB_INTR, /* int_ctl, int_vector */
  48. VMCB_NPT, /* npt_en, nCR3, gPAT */
  49. VMCB_CR, /* CR0, CR3, CR4, EFER */
  50. VMCB_DR, /* DR6, DR7 */
  51. VMCB_DT, /* GDT, IDT */
  52. VMCB_SEG, /* CS, DS, SS, ES, CPL */
  53. VMCB_CR2, /* CR2 only */
  54. VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
  55. VMCB_AVIC, /* AVIC APIC_BAR, AVIC APIC_BACKING_PAGE,
  56. * AVIC PHYSICAL_TABLE pointer,
  57. * AVIC LOGICAL_TABLE pointer
  58. */
  59. VMCB_SW = 31, /* Reserved for hypervisor/software use */
  60. };
  61. #define VMCB_ALL_CLEAN_MASK ( \
  62. (1U << VMCB_INTERCEPTS) | (1U << VMCB_PERM_MAP) | \
  63. (1U << VMCB_ASID) | (1U << VMCB_INTR) | \
  64. (1U << VMCB_NPT) | (1U << VMCB_CR) | (1U << VMCB_DR) | \
  65. (1U << VMCB_DT) | (1U << VMCB_SEG) | (1U << VMCB_CR2) | \
  66. (1U << VMCB_LBR) | (1U << VMCB_AVIC) | \
  67. (1U << VMCB_SW))
  68. /* TPR and CR2 are always written before VMRUN */
  69. #define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
  70. struct kvm_sev_info {
  71. bool active; /* SEV enabled guest */
  72. bool es_active; /* SEV-ES enabled guest */
  73. unsigned int asid; /* ASID used for this guest */
  74. unsigned int handle; /* SEV firmware handle */
  75. int fd; /* SEV device fd */
  76. unsigned long pages_locked; /* Number of pages locked */
  77. struct list_head regions_list; /* List of registered regions */
  78. u64 ap_jump_table; /* SEV-ES AP Jump Table address */
  79. struct kvm *enc_context_owner; /* Owner of copied encryption context */
  80. struct list_head mirror_vms; /* List of VMs mirroring */
  81. struct list_head mirror_entry; /* Use as a list entry of mirrors */
  82. struct misc_cg *misc_cg; /* For misc cgroup accounting */
  83. atomic_t migration_in_progress;
  84. };
  85. struct kvm_svm {
  86. struct kvm kvm;
  87. /* Struct members for AVIC */
  88. u32 avic_vm_id;
  89. struct page *avic_logical_id_table_page;
  90. struct page *avic_physical_id_table_page;
  91. struct hlist_node hnode;
  92. struct kvm_sev_info sev_info;
  93. };
  94. struct kvm_vcpu;
  95. struct kvm_vmcb_info {
  96. struct vmcb *ptr;
  97. unsigned long pa;
  98. int cpu;
  99. uint64_t asid_generation;
  100. };
  101. struct vmcb_save_area_cached {
  102. u64 efer;
  103. u64 cr4;
  104. u64 cr3;
  105. u64 cr0;
  106. u64 dr7;
  107. u64 dr6;
  108. };
  109. struct vmcb_ctrl_area_cached {
  110. u32 intercepts[MAX_INTERCEPT];
  111. u16 pause_filter_thresh;
  112. u16 pause_filter_count;
  113. u64 iopm_base_pa;
  114. u64 msrpm_base_pa;
  115. u64 tsc_offset;
  116. u32 asid;
  117. u8 tlb_ctl;
  118. u32 int_ctl;
  119. u32 int_vector;
  120. u32 int_state;
  121. u32 exit_code;
  122. u32 exit_code_hi;
  123. u64 exit_info_1;
  124. u64 exit_info_2;
  125. u32 exit_int_info;
  126. u32 exit_int_info_err;
  127. u64 nested_ctl;
  128. u32 event_inj;
  129. u32 event_inj_err;
  130. u64 next_rip;
  131. u64 nested_cr3;
  132. u64 virt_ext;
  133. u32 clean;
  134. union {
  135. struct hv_vmcb_enlightenments hv_enlightenments;
  136. u8 reserved_sw[32];
  137. };
  138. };
  139. struct svm_nested_state {
  140. struct kvm_vmcb_info vmcb02;
  141. u64 hsave_msr;
  142. u64 vm_cr_msr;
  143. u64 vmcb12_gpa;
  144. u64 last_vmcb12_gpa;
  145. /* These are the merged vectors */
  146. u32 *msrpm;
  147. /* A VMRUN has started but has not yet been performed, so
  148. * we cannot inject a nested vmexit yet. */
  149. bool nested_run_pending;
  150. /* cache for control fields of the guest */
  151. struct vmcb_ctrl_area_cached ctl;
  152. /*
  153. * Note: this struct is not kept up-to-date while L2 runs; it is only
  154. * valid within nested_svm_vmrun.
  155. */
  156. struct vmcb_save_area_cached save;
  157. bool initialized;
  158. /*
  159. * Indicates whether MSR bitmap for L2 needs to be rebuilt due to
  160. * changes in MSR bitmap for L1 or switching to a different L2. Note,
  161. * this flag can only be used reliably in conjunction with a paravirt L1
  162. * which informs L0 whether any changes to MSR bitmap for L2 were done
  163. * on its side.
  164. */
  165. bool force_msr_bitmap_recalc;
  166. };
  167. struct vcpu_sev_es_state {
  168. /* SEV-ES support */
  169. struct sev_es_save_area *vmsa;
  170. struct ghcb *ghcb;
  171. u8 valid_bitmap[16];
  172. struct kvm_host_map ghcb_map;
  173. bool received_first_sipi;
  174. /* SEV-ES scratch area support */
  175. u64 sw_scratch;
  176. void *ghcb_sa;
  177. u32 ghcb_sa_len;
  178. bool ghcb_sa_sync;
  179. bool ghcb_sa_free;
  180. };
  181. struct vcpu_svm {
  182. struct kvm_vcpu vcpu;
  183. /* vmcb always points at current_vmcb->ptr, it's purely a shorthand. */
  184. struct vmcb *vmcb;
  185. struct kvm_vmcb_info vmcb01;
  186. struct kvm_vmcb_info *current_vmcb;
  187. u32 asid;
  188. u32 sysenter_esp_hi;
  189. u32 sysenter_eip_hi;
  190. uint64_t tsc_aux;
  191. u64 msr_decfg;
  192. u64 next_rip;
  193. u64 spec_ctrl;
  194. u64 tsc_ratio_msr;
  195. /*
  196. * Contains guest-controlled bits of VIRT_SPEC_CTRL, which will be
  197. * translated into the appropriate L2_CFG bits on the host to
  198. * perform speculative control.
  199. */
  200. u64 virt_spec_ctrl;
  201. u32 *msrpm;
  202. ulong nmi_iret_rip;
  203. struct svm_nested_state nested;
  204. bool nmi_singlestep;
  205. u64 nmi_singlestep_guest_rflags;
  206. bool nmi_l1_to_l2;
  207. unsigned long soft_int_csbase;
  208. unsigned long soft_int_old_rip;
  209. unsigned long soft_int_next_rip;
  210. bool soft_int_injected;
  211. /* optional nested SVM features that are enabled for this guest */
  212. bool nrips_enabled : 1;
  213. bool tsc_scaling_enabled : 1;
  214. bool v_vmload_vmsave_enabled : 1;
  215. bool lbrv_enabled : 1;
  216. bool pause_filter_enabled : 1;
  217. bool pause_threshold_enabled : 1;
  218. bool vgif_enabled : 1;
  219. u32 ldr_reg;
  220. u32 dfr_reg;
  221. struct page *avic_backing_page;
  222. u64 *avic_physical_id_cache;
  223. /*
  224. * Per-vcpu list of struct amd_svm_iommu_ir:
  225. * This is used mainly to store interrupt remapping information used
  226. * when update the vcpu affinity. This avoids the need to scan for
  227. * IRTE and try to match ga_tag in the IOMMU driver.
  228. */
  229. struct list_head ir_list;
  230. spinlock_t ir_list_lock;
  231. /* Save desired MSR intercept (read: pass-through) state */
  232. struct {
  233. DECLARE_BITMAP(read, MAX_DIRECT_ACCESS_MSRS);
  234. DECLARE_BITMAP(write, MAX_DIRECT_ACCESS_MSRS);
  235. } shadow_msr_intercept;
  236. struct vcpu_sev_es_state sev_es;
  237. bool guest_state_loaded;
  238. bool x2avic_msrs_intercepted;
  239. };
  240. struct svm_cpu_data {
  241. u64 asid_generation;
  242. u32 max_asid;
  243. u32 next_asid;
  244. u32 min_asid;
  245. struct kvm_ldttss_desc *tss_desc;
  246. struct page *save_area;
  247. unsigned long save_area_pa;
  248. struct vmcb *current_vmcb;
  249. /* index = sev_asid, value = vmcb pointer */
  250. struct vmcb **sev_vmcbs;
  251. };
  252. DECLARE_PER_CPU(struct svm_cpu_data, svm_data);
  253. void recalc_intercepts(struct vcpu_svm *svm);
  254. static __always_inline struct kvm_svm *to_kvm_svm(struct kvm *kvm)
  255. {
  256. return container_of(kvm, struct kvm_svm, kvm);
  257. }
  258. static __always_inline bool sev_guest(struct kvm *kvm)
  259. {
  260. #ifdef CONFIG_KVM_AMD_SEV
  261. struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
  262. return sev->active;
  263. #else
  264. return false;
  265. #endif
  266. }
  267. static __always_inline bool sev_es_guest(struct kvm *kvm)
  268. {
  269. #ifdef CONFIG_KVM_AMD_SEV
  270. struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
  271. return sev->es_active && !WARN_ON_ONCE(!sev->active);
  272. #else
  273. return false;
  274. #endif
  275. }
  276. static inline void vmcb_mark_all_dirty(struct vmcb *vmcb)
  277. {
  278. vmcb->control.clean = 0;
  279. }
  280. static inline void vmcb_mark_all_clean(struct vmcb *vmcb)
  281. {
  282. vmcb->control.clean = VMCB_ALL_CLEAN_MASK
  283. & ~VMCB_ALWAYS_DIRTY_MASK;
  284. }
  285. static inline void vmcb_mark_dirty(struct vmcb *vmcb, int bit)
  286. {
  287. vmcb->control.clean &= ~(1 << bit);
  288. }
  289. static inline bool vmcb_is_dirty(struct vmcb *vmcb, int bit)
  290. {
  291. return !test_bit(bit, (unsigned long *)&vmcb->control.clean);
  292. }
  293. static __always_inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
  294. {
  295. return container_of(vcpu, struct vcpu_svm, vcpu);
  296. }
  297. /*
  298. * Only the PDPTRs are loaded on demand into the shadow MMU. All other
  299. * fields are synchronized on VM-Exit, because accessing the VMCB is cheap.
  300. *
  301. * CR3 might be out of date in the VMCB but it is not marked dirty; instead,
  302. * KVM_REQ_LOAD_MMU_PGD is always requested when the cached vcpu->arch.cr3
  303. * is changed. svm_load_mmu_pgd() then syncs the new CR3 value into the VMCB.
  304. */
  305. #define SVM_REGS_LAZY_LOAD_SET (1 << VCPU_EXREG_PDPTR)
  306. static inline void vmcb_set_intercept(struct vmcb_control_area *control, u32 bit)
  307. {
  308. WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT);
  309. __set_bit(bit, (unsigned long *)&control->intercepts);
  310. }
  311. static inline void vmcb_clr_intercept(struct vmcb_control_area *control, u32 bit)
  312. {
  313. WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT);
  314. __clear_bit(bit, (unsigned long *)&control->intercepts);
  315. }
  316. static inline bool vmcb_is_intercept(struct vmcb_control_area *control, u32 bit)
  317. {
  318. WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT);
  319. return test_bit(bit, (unsigned long *)&control->intercepts);
  320. }
  321. static inline bool vmcb12_is_intercept(struct vmcb_ctrl_area_cached *control, u32 bit)
  322. {
  323. WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT);
  324. return test_bit(bit, (unsigned long *)&control->intercepts);
  325. }
  326. static inline void set_dr_intercepts(struct vcpu_svm *svm)
  327. {
  328. struct vmcb *vmcb = svm->vmcb01.ptr;
  329. if (!sev_es_guest(svm->vcpu.kvm)) {
  330. vmcb_set_intercept(&vmcb->control, INTERCEPT_DR0_READ);
  331. vmcb_set_intercept(&vmcb->control, INTERCEPT_DR1_READ);
  332. vmcb_set_intercept(&vmcb->control, INTERCEPT_DR2_READ);
  333. vmcb_set_intercept(&vmcb->control, INTERCEPT_DR3_READ);
  334. vmcb_set_intercept(&vmcb->control, INTERCEPT_DR4_READ);
  335. vmcb_set_intercept(&vmcb->control, INTERCEPT_DR5_READ);
  336. vmcb_set_intercept(&vmcb->control, INTERCEPT_DR6_READ);
  337. vmcb_set_intercept(&vmcb->control, INTERCEPT_DR0_WRITE);
  338. vmcb_set_intercept(&vmcb->control, INTERCEPT_DR1_WRITE);
  339. vmcb_set_intercept(&vmcb->control, INTERCEPT_DR2_WRITE);
  340. vmcb_set_intercept(&vmcb->control, INTERCEPT_DR3_WRITE);
  341. vmcb_set_intercept(&vmcb->control, INTERCEPT_DR4_WRITE);
  342. vmcb_set_intercept(&vmcb->control, INTERCEPT_DR5_WRITE);
  343. vmcb_set_intercept(&vmcb->control, INTERCEPT_DR6_WRITE);
  344. }
  345. vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_READ);
  346. vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_WRITE);
  347. recalc_intercepts(svm);
  348. }
  349. static inline void clr_dr_intercepts(struct vcpu_svm *svm)
  350. {
  351. struct vmcb *vmcb = svm->vmcb01.ptr;
  352. vmcb->control.intercepts[INTERCEPT_DR] = 0;
  353. /* DR7 access must remain intercepted for an SEV-ES guest */
  354. if (sev_es_guest(svm->vcpu.kvm)) {
  355. vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_READ);
  356. vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_WRITE);
  357. }
  358. recalc_intercepts(svm);
  359. }
  360. static inline void set_exception_intercept(struct vcpu_svm *svm, u32 bit)
  361. {
  362. struct vmcb *vmcb = svm->vmcb01.ptr;
  363. WARN_ON_ONCE(bit >= 32);
  364. vmcb_set_intercept(&vmcb->control, INTERCEPT_EXCEPTION_OFFSET + bit);
  365. recalc_intercepts(svm);
  366. }
  367. static inline void clr_exception_intercept(struct vcpu_svm *svm, u32 bit)
  368. {
  369. struct vmcb *vmcb = svm->vmcb01.ptr;
  370. WARN_ON_ONCE(bit >= 32);
  371. vmcb_clr_intercept(&vmcb->control, INTERCEPT_EXCEPTION_OFFSET + bit);
  372. recalc_intercepts(svm);
  373. }
  374. static inline void svm_set_intercept(struct vcpu_svm *svm, int bit)
  375. {
  376. struct vmcb *vmcb = svm->vmcb01.ptr;
  377. vmcb_set_intercept(&vmcb->control, bit);
  378. recalc_intercepts(svm);
  379. }
  380. static inline void svm_clr_intercept(struct vcpu_svm *svm, int bit)
  381. {
  382. struct vmcb *vmcb = svm->vmcb01.ptr;
  383. vmcb_clr_intercept(&vmcb->control, bit);
  384. recalc_intercepts(svm);
  385. }
  386. static inline bool svm_is_intercept(struct vcpu_svm *svm, int bit)
  387. {
  388. return vmcb_is_intercept(&svm->vmcb->control, bit);
  389. }
  390. static inline bool nested_vgif_enabled(struct vcpu_svm *svm)
  391. {
  392. return svm->vgif_enabled && (svm->nested.ctl.int_ctl & V_GIF_ENABLE_MASK);
  393. }
  394. static inline struct vmcb *get_vgif_vmcb(struct vcpu_svm *svm)
  395. {
  396. if (!vgif)
  397. return NULL;
  398. if (is_guest_mode(&svm->vcpu) && !nested_vgif_enabled(svm))
  399. return svm->nested.vmcb02.ptr;
  400. else
  401. return svm->vmcb01.ptr;
  402. }
  403. static inline void enable_gif(struct vcpu_svm *svm)
  404. {
  405. struct vmcb *vmcb = get_vgif_vmcb(svm);
  406. if (vmcb)
  407. vmcb->control.int_ctl |= V_GIF_MASK;
  408. else
  409. svm->vcpu.arch.hflags |= HF_GIF_MASK;
  410. }
  411. static inline void disable_gif(struct vcpu_svm *svm)
  412. {
  413. struct vmcb *vmcb = get_vgif_vmcb(svm);
  414. if (vmcb)
  415. vmcb->control.int_ctl &= ~V_GIF_MASK;
  416. else
  417. svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
  418. }
  419. static inline bool gif_set(struct vcpu_svm *svm)
  420. {
  421. struct vmcb *vmcb = get_vgif_vmcb(svm);
  422. if (vmcb)
  423. return !!(vmcb->control.int_ctl & V_GIF_MASK);
  424. else
  425. return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
  426. }
  427. static inline bool nested_npt_enabled(struct vcpu_svm *svm)
  428. {
  429. return svm->nested.ctl.nested_ctl & SVM_NESTED_CTL_NP_ENABLE;
  430. }
  431. static inline bool is_x2apic_msrpm_offset(u32 offset)
  432. {
  433. /* 4 msrs per u8, and 4 u8 in u32 */
  434. u32 msr = offset * 16;
  435. return (msr >= APIC_BASE_MSR) &&
  436. (msr < (APIC_BASE_MSR + 0x100));
  437. }
  438. /* svm.c */
  439. #define MSR_INVALID 0xffffffffU
  440. #define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
  441. extern bool dump_invalid_vmcb;
  442. u32 svm_msrpm_offset(u32 msr);
  443. u32 *svm_vcpu_alloc_msrpm(void);
  444. void svm_vcpu_init_msrpm(struct kvm_vcpu *vcpu, u32 *msrpm);
  445. void svm_vcpu_free_msrpm(u32 *msrpm);
  446. void svm_copy_lbrs(struct vmcb *to_vmcb, struct vmcb *from_vmcb);
  447. void svm_update_lbrv(struct kvm_vcpu *vcpu);
  448. int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer);
  449. void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
  450. void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
  451. void disable_nmi_singlestep(struct vcpu_svm *svm);
  452. bool svm_smi_blocked(struct kvm_vcpu *vcpu);
  453. bool svm_nmi_blocked(struct kvm_vcpu *vcpu);
  454. bool svm_interrupt_blocked(struct kvm_vcpu *vcpu);
  455. void svm_set_gif(struct vcpu_svm *svm, bool value);
  456. int svm_invoke_exit_handler(struct kvm_vcpu *vcpu, u64 exit_code);
  457. void set_msr_interception(struct kvm_vcpu *vcpu, u32 *msrpm, u32 msr,
  458. int read, int write);
  459. void svm_set_x2apic_msr_interception(struct vcpu_svm *svm, bool disable);
  460. void svm_complete_interrupt_delivery(struct kvm_vcpu *vcpu, int delivery_mode,
  461. int trig_mode, int vec);
  462. /* nested.c */
  463. #define NESTED_EXIT_HOST 0 /* Exit handled on host level */
  464. #define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
  465. #define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
  466. static inline bool nested_svm_virtualize_tpr(struct kvm_vcpu *vcpu)
  467. {
  468. struct vcpu_svm *svm = to_svm(vcpu);
  469. return is_guest_mode(vcpu) && (svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK);
  470. }
  471. static inline bool nested_exit_on_smi(struct vcpu_svm *svm)
  472. {
  473. return vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_SMI);
  474. }
  475. static inline bool nested_exit_on_intr(struct vcpu_svm *svm)
  476. {
  477. return vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_INTR);
  478. }
  479. static inline bool nested_exit_on_nmi(struct vcpu_svm *svm)
  480. {
  481. return vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_NMI);
  482. }
  483. int enter_svm_guest_mode(struct kvm_vcpu *vcpu,
  484. u64 vmcb_gpa, struct vmcb *vmcb12, bool from_vmrun);
  485. void svm_leave_nested(struct kvm_vcpu *vcpu);
  486. void svm_free_nested(struct vcpu_svm *svm);
  487. int svm_allocate_nested(struct vcpu_svm *svm);
  488. int nested_svm_vmrun(struct kvm_vcpu *vcpu);
  489. void svm_copy_vmrun_state(struct vmcb_save_area *to_save,
  490. struct vmcb_save_area *from_save);
  491. void svm_copy_vmloadsave_state(struct vmcb *to_vmcb, struct vmcb *from_vmcb);
  492. int nested_svm_vmexit(struct vcpu_svm *svm);
  493. static inline int nested_svm_simple_vmexit(struct vcpu_svm *svm, u32 exit_code)
  494. {
  495. svm->vmcb->control.exit_code = exit_code;
  496. svm->vmcb->control.exit_info_1 = 0;
  497. svm->vmcb->control.exit_info_2 = 0;
  498. return nested_svm_vmexit(svm);
  499. }
  500. int nested_svm_exit_handled(struct vcpu_svm *svm);
  501. int nested_svm_check_permissions(struct kvm_vcpu *vcpu);
  502. int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
  503. bool has_error_code, u32 error_code);
  504. int nested_svm_exit_special(struct vcpu_svm *svm);
  505. void nested_svm_update_tsc_ratio_msr(struct kvm_vcpu *vcpu);
  506. void __svm_write_tsc_multiplier(u64 multiplier);
  507. void nested_copy_vmcb_control_to_cache(struct vcpu_svm *svm,
  508. struct vmcb_control_area *control);
  509. void nested_copy_vmcb_save_to_cache(struct vcpu_svm *svm,
  510. struct vmcb_save_area *save);
  511. void nested_sync_control_from_vmcb02(struct vcpu_svm *svm);
  512. void nested_vmcb02_compute_g_pat(struct vcpu_svm *svm);
  513. void svm_switch_vmcb(struct vcpu_svm *svm, struct kvm_vmcb_info *target_vmcb);
  514. extern struct kvm_x86_nested_ops svm_nested_ops;
  515. /* avic.c */
  516. bool avic_hardware_setup(struct kvm_x86_ops *ops);
  517. int avic_ga_log_notifier(u32 ga_tag);
  518. void avic_vm_destroy(struct kvm *kvm);
  519. int avic_vm_init(struct kvm *kvm);
  520. void avic_init_vmcb(struct vcpu_svm *svm, struct vmcb *vmcb);
  521. int avic_incomplete_ipi_interception(struct kvm_vcpu *vcpu);
  522. int avic_unaccelerated_access_interception(struct kvm_vcpu *vcpu);
  523. int avic_init_vcpu(struct vcpu_svm *svm);
  524. void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
  525. void avic_vcpu_put(struct kvm_vcpu *vcpu);
  526. void avic_apicv_post_state_restore(struct kvm_vcpu *vcpu);
  527. void avic_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu);
  528. bool avic_check_apicv_inhibit_reasons(enum kvm_apicv_inhibit reason);
  529. int avic_pi_update_irte(struct kvm *kvm, unsigned int host_irq,
  530. uint32_t guest_irq, bool set);
  531. void avic_vcpu_blocking(struct kvm_vcpu *vcpu);
  532. void avic_vcpu_unblocking(struct kvm_vcpu *vcpu);
  533. void avic_ring_doorbell(struct kvm_vcpu *vcpu);
  534. unsigned long avic_vcpu_get_apicv_inhibit_reasons(struct kvm_vcpu *vcpu);
  535. void avic_refresh_virtual_apic_mode(struct kvm_vcpu *vcpu);
  536. /* sev.c */
  537. #define GHCB_VERSION_MAX 1ULL
  538. #define GHCB_VERSION_MIN 1ULL
  539. extern unsigned int max_sev_asid;
  540. void sev_vm_destroy(struct kvm *kvm);
  541. int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp);
  542. int sev_mem_enc_register_region(struct kvm *kvm,
  543. struct kvm_enc_region *range);
  544. int sev_mem_enc_unregister_region(struct kvm *kvm,
  545. struct kvm_enc_region *range);
  546. int sev_vm_copy_enc_context_from(struct kvm *kvm, unsigned int source_fd);
  547. int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd);
  548. void sev_guest_memory_reclaimed(struct kvm *kvm);
  549. void pre_sev_run(struct vcpu_svm *svm, int cpu);
  550. void __init sev_set_cpu_caps(void);
  551. void __init sev_hardware_setup(void);
  552. void sev_hardware_unsetup(void);
  553. int sev_cpu_init(struct svm_cpu_data *sd);
  554. void sev_init_vmcb(struct vcpu_svm *svm);
  555. void sev_vcpu_after_set_cpuid(struct vcpu_svm *svm);
  556. void sev_free_vcpu(struct kvm_vcpu *vcpu);
  557. int sev_handle_vmgexit(struct kvm_vcpu *vcpu);
  558. int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in);
  559. void sev_es_vcpu_reset(struct vcpu_svm *svm);
  560. void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector);
  561. void sev_es_prepare_switch_to_guest(struct sev_es_save_area *hostsa);
  562. void sev_es_unmap_ghcb(struct vcpu_svm *svm);
  563. /* vmenter.S */
  564. void __svm_sev_es_vcpu_run(struct vcpu_svm *svm, bool spec_ctrl_intercepted);
  565. void __svm_vcpu_run(struct vcpu_svm *svm, bool spec_ctrl_intercepted);
  566. #define DEFINE_KVM_GHCB_ACCESSORS(field) \
  567. static __always_inline bool kvm_ghcb_##field##_is_valid(const struct vcpu_svm *svm) \
  568. { \
  569. return test_bit(GHCB_BITMAP_IDX(field), \
  570. (unsigned long *)&svm->sev_es.valid_bitmap); \
  571. } \
  572. \
  573. static __always_inline u64 kvm_ghcb_get_##field##_if_valid(struct vcpu_svm *svm, struct ghcb *ghcb) \
  574. { \
  575. return kvm_ghcb_##field##_is_valid(svm) ? ghcb->save.field : 0; \
  576. } \
  577. DEFINE_KVM_GHCB_ACCESSORS(cpl)
  578. DEFINE_KVM_GHCB_ACCESSORS(rax)
  579. DEFINE_KVM_GHCB_ACCESSORS(rcx)
  580. DEFINE_KVM_GHCB_ACCESSORS(rdx)
  581. DEFINE_KVM_GHCB_ACCESSORS(rbx)
  582. DEFINE_KVM_GHCB_ACCESSORS(rsi)
  583. DEFINE_KVM_GHCB_ACCESSORS(sw_exit_code)
  584. DEFINE_KVM_GHCB_ACCESSORS(sw_exit_info_1)
  585. DEFINE_KVM_GHCB_ACCESSORS(sw_exit_info_2)
  586. DEFINE_KVM_GHCB_ACCESSORS(sw_scratch)
  587. DEFINE_KVM_GHCB_ACCESSORS(xcr0)
  588. #endif