nested.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __KVM_X86_VMX_NESTED_H
  3. #define __KVM_X86_VMX_NESTED_H
  4. #include "kvm_cache_regs.h"
  5. #include "vmcs12.h"
  6. #include "vmx.h"
  7. /*
  8. * Status returned by nested_vmx_enter_non_root_mode():
  9. */
  10. enum nvmx_vmentry_status {
  11. NVMX_VMENTRY_SUCCESS, /* Entered VMX non-root mode */
  12. NVMX_VMENTRY_VMFAIL, /* Consistency check VMFail */
  13. NVMX_VMENTRY_VMEXIT, /* Consistency check VMExit */
  14. NVMX_VMENTRY_KVM_INTERNAL_ERROR,/* KVM internal error */
  15. };
  16. void vmx_leave_nested(struct kvm_vcpu *vcpu);
  17. void nested_vmx_setup_ctls_msrs(struct vmcs_config *vmcs_conf, u32 ept_caps);
  18. void nested_vmx_hardware_unsetup(void);
  19. __init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcpu *));
  20. void nested_vmx_set_vmcs_shadowing_bitmap(void);
  21. void nested_vmx_free_vcpu(struct kvm_vcpu *vcpu);
  22. enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,
  23. bool from_vmentry);
  24. bool nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu);
  25. void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason,
  26. u32 exit_intr_info, unsigned long exit_qualification);
  27. void nested_sync_vmcs12_to_shadow(struct kvm_vcpu *vcpu);
  28. int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data);
  29. int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata);
  30. int get_vmx_mem_address(struct kvm_vcpu *vcpu, unsigned long exit_qualification,
  31. u32 vmx_instruction_info, bool wr, int len, gva_t *ret);
  32. void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu);
  33. bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu, unsigned int port,
  34. int size);
  35. static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
  36. {
  37. return to_vmx(vcpu)->nested.cached_vmcs12;
  38. }
  39. static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
  40. {
  41. return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
  42. }
  43. /*
  44. * Note: the same condition is checked against the state provided by userspace
  45. * in vmx_set_nested_state; if it is satisfied, the nested state must include
  46. * the VMCS12.
  47. */
  48. static inline int vmx_has_valid_vmcs12(struct kvm_vcpu *vcpu)
  49. {
  50. struct vcpu_vmx *vmx = to_vmx(vcpu);
  51. /* 'hv_evmcs_vmptr' can also be EVMPTR_MAP_PENDING here */
  52. return vmx->nested.current_vmptr != -1ull ||
  53. vmx->nested.hv_evmcs_vmptr != EVMPTR_INVALID;
  54. }
  55. static inline u16 nested_get_vpid02(struct kvm_vcpu *vcpu)
  56. {
  57. struct vcpu_vmx *vmx = to_vmx(vcpu);
  58. return vmx->nested.vpid02 ? vmx->nested.vpid02 : vmx->vpid;
  59. }
  60. static inline unsigned long nested_ept_get_eptp(struct kvm_vcpu *vcpu)
  61. {
  62. /* return the page table to be shadowed - in our case, EPT12 */
  63. return get_vmcs12(vcpu)->ept_pointer;
  64. }
  65. static inline bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
  66. {
  67. return nested_ept_get_eptp(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
  68. }
  69. /*
  70. * Return the cr0 value that a nested guest would read. This is a combination
  71. * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
  72. * its hypervisor (cr0_read_shadow).
  73. */
  74. static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
  75. {
  76. return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
  77. (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
  78. }
  79. static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
  80. {
  81. return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
  82. (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
  83. }
  84. static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
  85. {
  86. return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
  87. }
  88. /*
  89. * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
  90. * to modify any valid field of the VMCS, or are the VM-exit
  91. * information fields read-only?
  92. */
  93. static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
  94. {
  95. return to_vmx(vcpu)->nested.msrs.misc_low &
  96. MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
  97. }
  98. static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
  99. {
  100. return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
  101. }
  102. static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
  103. {
  104. return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
  105. CPU_BASED_MONITOR_TRAP_FLAG;
  106. }
  107. static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
  108. {
  109. return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
  110. SECONDARY_EXEC_SHADOW_VMCS;
  111. }
  112. static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
  113. {
  114. return vmcs12->cpu_based_vm_exec_control & bit;
  115. }
  116. static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
  117. {
  118. return (vmcs12->cpu_based_vm_exec_control &
  119. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
  120. (vmcs12->secondary_vm_exec_control & bit);
  121. }
  122. static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
  123. {
  124. return vmcs12->pin_based_vm_exec_control &
  125. PIN_BASED_VMX_PREEMPTION_TIMER;
  126. }
  127. static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
  128. {
  129. return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
  130. }
  131. static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
  132. {
  133. return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
  134. }
  135. static inline int nested_cpu_has_mtf(struct vmcs12 *vmcs12)
  136. {
  137. return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
  138. }
  139. static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
  140. {
  141. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
  142. }
  143. static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
  144. {
  145. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
  146. }
  147. static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
  148. {
  149. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
  150. }
  151. static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
  152. {
  153. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
  154. }
  155. static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
  156. {
  157. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
  158. }
  159. static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
  160. {
  161. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
  162. }
  163. static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
  164. {
  165. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  166. }
  167. static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
  168. {
  169. return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
  170. }
  171. static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
  172. {
  173. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
  174. }
  175. static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
  176. {
  177. return nested_cpu_has_vmfunc(vmcs12) &&
  178. (vmcs12->vm_function_control &
  179. VMX_VMFUNC_EPTP_SWITCHING);
  180. }
  181. static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
  182. {
  183. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
  184. }
  185. static inline bool nested_cpu_has_save_preemption_timer(struct vmcs12 *vmcs12)
  186. {
  187. return vmcs12->vm_exit_controls &
  188. VM_EXIT_SAVE_VMX_PREEMPTION_TIMER;
  189. }
  190. static inline bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
  191. {
  192. return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
  193. }
  194. /*
  195. * In nested virtualization, check if L1 asked to exit on external interrupts.
  196. * For most existing hypervisors, this will always return true.
  197. */
  198. static inline bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
  199. {
  200. return get_vmcs12(vcpu)->pin_based_vm_exec_control &
  201. PIN_BASED_EXT_INTR_MASK;
  202. }
  203. static inline bool nested_cpu_has_encls_exit(struct vmcs12 *vmcs12)
  204. {
  205. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENCLS_EXITING);
  206. }
  207. /*
  208. * if fixed0[i] == 1: val[i] must be 1
  209. * if fixed1[i] == 0: val[i] must be 0
  210. */
  211. static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
  212. {
  213. return ((val & fixed1) | fixed0) == val;
  214. }
  215. static inline bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
  216. {
  217. u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
  218. u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
  219. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  220. if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
  221. SECONDARY_EXEC_UNRESTRICTED_GUEST &&
  222. nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
  223. fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
  224. return fixed_bits_valid(val, fixed0, fixed1);
  225. }
  226. static inline bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
  227. {
  228. u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
  229. u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
  230. return fixed_bits_valid(val, fixed0, fixed1);
  231. }
  232. static inline bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
  233. {
  234. u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
  235. u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
  236. return fixed_bits_valid(val, fixed0, fixed1) &&
  237. __kvm_is_valid_cr4(vcpu, val);
  238. }
  239. /* No difference in the restrictions on guest and host CR4 in VMX operation. */
  240. #define nested_guest_cr4_valid nested_cr4_valid
  241. #define nested_host_cr4_valid nested_cr4_valid
  242. extern struct kvm_x86_nested_ops vmx_nested_ops;
  243. #endif /* __KVM_X86_VMX_NESTED_H */