vmx_ops.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __KVM_X86_VMX_INSN_H
  3. #define __KVM_X86_VMX_INSN_H
  4. #include <linux/nospec.h>
  5. #include <asm/vmx.h>
  6. #include "evmcs.h"
  7. #include "vmcs.h"
  8. #include "../x86.h"
  9. void vmread_error(unsigned long field, bool fault);
  10. __attribute__((regparm(0))) void vmread_error_trampoline(unsigned long field,
  11. bool fault);
  12. void vmwrite_error(unsigned long field, unsigned long value);
  13. void vmclear_error(struct vmcs *vmcs, u64 phys_addr);
  14. void vmptrld_error(struct vmcs *vmcs, u64 phys_addr);
  15. void invvpid_error(unsigned long ext, u16 vpid, gva_t gva);
  16. void invept_error(unsigned long ext, u64 eptp, gpa_t gpa);
  17. static __always_inline void vmcs_check16(unsigned long field)
  18. {
  19. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
  20. "16-bit accessor invalid for 64-bit field");
  21. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
  22. "16-bit accessor invalid for 64-bit high field");
  23. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
  24. "16-bit accessor invalid for 32-bit high field");
  25. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
  26. "16-bit accessor invalid for natural width field");
  27. }
  28. static __always_inline void vmcs_check32(unsigned long field)
  29. {
  30. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
  31. "32-bit accessor invalid for 16-bit field");
  32. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
  33. "32-bit accessor invalid for 64-bit field");
  34. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
  35. "32-bit accessor invalid for 64-bit high field");
  36. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
  37. "32-bit accessor invalid for natural width field");
  38. }
  39. static __always_inline void vmcs_check64(unsigned long field)
  40. {
  41. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
  42. "64-bit accessor invalid for 16-bit field");
  43. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
  44. "64-bit accessor invalid for 64-bit high field");
  45. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
  46. "64-bit accessor invalid for 32-bit field");
  47. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
  48. "64-bit accessor invalid for natural width field");
  49. }
  50. static __always_inline void vmcs_checkl(unsigned long field)
  51. {
  52. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
  53. "Natural width accessor invalid for 16-bit field");
  54. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
  55. "Natural width accessor invalid for 64-bit field");
  56. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
  57. "Natural width accessor invalid for 64-bit high field");
  58. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
  59. "Natural width accessor invalid for 32-bit field");
  60. }
  61. static __always_inline unsigned long __vmcs_readl(unsigned long field)
  62. {
  63. unsigned long value;
  64. #ifdef CONFIG_CC_HAS_ASM_GOTO_OUTPUT
  65. asm_volatile_goto("1: vmread %[field], %[output]\n\t"
  66. "jna %l[do_fail]\n\t"
  67. _ASM_EXTABLE(1b, %l[do_exception])
  68. : [output] "=r" (value)
  69. : [field] "r" (field)
  70. : "cc"
  71. : do_fail, do_exception);
  72. return value;
  73. do_fail:
  74. WARN_ONCE(1, "kvm: vmread failed: field=%lx\n", field);
  75. pr_warn_ratelimited("kvm: vmread failed: field=%lx\n", field);
  76. return 0;
  77. do_exception:
  78. kvm_spurious_fault();
  79. return 0;
  80. #else /* !CONFIG_CC_HAS_ASM_GOTO_OUTPUT */
  81. asm volatile("1: vmread %2, %1\n\t"
  82. ".byte 0x3e\n\t" /* branch taken hint */
  83. "ja 3f\n\t"
  84. /*
  85. * VMREAD failed. Push '0' for @fault, push the failing
  86. * @field, and bounce through the trampoline to preserve
  87. * volatile registers.
  88. */
  89. "xorl %k1, %k1\n\t"
  90. "2:\n\t"
  91. "push %1\n\t"
  92. "push %2\n\t"
  93. "call vmread_error_trampoline\n\t"
  94. /*
  95. * Unwind the stack. Note, the trampoline zeros out the
  96. * memory for @fault so that the result is '0' on error.
  97. */
  98. "pop %2\n\t"
  99. "pop %1\n\t"
  100. "3:\n\t"
  101. /* VMREAD faulted. As above, except push '1' for @fault. */
  102. _ASM_EXTABLE_TYPE_REG(1b, 2b, EX_TYPE_ONE_REG, %1)
  103. : ASM_CALL_CONSTRAINT, "=&r"(value) : "r"(field) : "cc");
  104. return value;
  105. #endif /* CONFIG_CC_HAS_ASM_GOTO_OUTPUT */
  106. }
  107. static __always_inline u16 vmcs_read16(unsigned long field)
  108. {
  109. vmcs_check16(field);
  110. if (static_branch_unlikely(&enable_evmcs))
  111. return evmcs_read16(field);
  112. return __vmcs_readl(field);
  113. }
  114. static __always_inline u32 vmcs_read32(unsigned long field)
  115. {
  116. vmcs_check32(field);
  117. if (static_branch_unlikely(&enable_evmcs))
  118. return evmcs_read32(field);
  119. return __vmcs_readl(field);
  120. }
  121. static __always_inline u64 vmcs_read64(unsigned long field)
  122. {
  123. vmcs_check64(field);
  124. if (static_branch_unlikely(&enable_evmcs))
  125. return evmcs_read64(field);
  126. #ifdef CONFIG_X86_64
  127. return __vmcs_readl(field);
  128. #else
  129. return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
  130. #endif
  131. }
  132. static __always_inline unsigned long vmcs_readl(unsigned long field)
  133. {
  134. vmcs_checkl(field);
  135. if (static_branch_unlikely(&enable_evmcs))
  136. return evmcs_read64(field);
  137. return __vmcs_readl(field);
  138. }
  139. #define vmx_asm1(insn, op1, error_args...) \
  140. do { \
  141. asm_volatile_goto("1: " __stringify(insn) " %0\n\t" \
  142. ".byte 0x2e\n\t" /* branch not taken hint */ \
  143. "jna %l[error]\n\t" \
  144. _ASM_EXTABLE(1b, %l[fault]) \
  145. : : op1 : "cc" : error, fault); \
  146. return; \
  147. error: \
  148. instrumentation_begin(); \
  149. insn##_error(error_args); \
  150. instrumentation_end(); \
  151. return; \
  152. fault: \
  153. kvm_spurious_fault(); \
  154. } while (0)
  155. #define vmx_asm2(insn, op1, op2, error_args...) \
  156. do { \
  157. asm_volatile_goto("1: " __stringify(insn) " %1, %0\n\t" \
  158. ".byte 0x2e\n\t" /* branch not taken hint */ \
  159. "jna %l[error]\n\t" \
  160. _ASM_EXTABLE(1b, %l[fault]) \
  161. : : op1, op2 : "cc" : error, fault); \
  162. return; \
  163. error: \
  164. instrumentation_begin(); \
  165. insn##_error(error_args); \
  166. instrumentation_end(); \
  167. return; \
  168. fault: \
  169. kvm_spurious_fault(); \
  170. } while (0)
  171. static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
  172. {
  173. vmx_asm2(vmwrite, "r"(field), "rm"(value), field, value);
  174. }
  175. static __always_inline void vmcs_write16(unsigned long field, u16 value)
  176. {
  177. vmcs_check16(field);
  178. if (static_branch_unlikely(&enable_evmcs))
  179. return evmcs_write16(field, value);
  180. __vmcs_writel(field, value);
  181. }
  182. static __always_inline void vmcs_write32(unsigned long field, u32 value)
  183. {
  184. vmcs_check32(field);
  185. if (static_branch_unlikely(&enable_evmcs))
  186. return evmcs_write32(field, value);
  187. __vmcs_writel(field, value);
  188. }
  189. static __always_inline void vmcs_write64(unsigned long field, u64 value)
  190. {
  191. vmcs_check64(field);
  192. if (static_branch_unlikely(&enable_evmcs))
  193. return evmcs_write64(field, value);
  194. __vmcs_writel(field, value);
  195. #ifndef CONFIG_X86_64
  196. __vmcs_writel(field+1, value >> 32);
  197. #endif
  198. }
  199. static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
  200. {
  201. vmcs_checkl(field);
  202. if (static_branch_unlikely(&enable_evmcs))
  203. return evmcs_write64(field, value);
  204. __vmcs_writel(field, value);
  205. }
  206. static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
  207. {
  208. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
  209. "vmcs_clear_bits does not support 64-bit fields");
  210. if (static_branch_unlikely(&enable_evmcs))
  211. return evmcs_write32(field, evmcs_read32(field) & ~mask);
  212. __vmcs_writel(field, __vmcs_readl(field) & ~mask);
  213. }
  214. static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
  215. {
  216. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
  217. "vmcs_set_bits does not support 64-bit fields");
  218. if (static_branch_unlikely(&enable_evmcs))
  219. return evmcs_write32(field, evmcs_read32(field) | mask);
  220. __vmcs_writel(field, __vmcs_readl(field) | mask);
  221. }
  222. static inline void vmcs_clear(struct vmcs *vmcs)
  223. {
  224. u64 phys_addr = __pa(vmcs);
  225. vmx_asm1(vmclear, "m"(phys_addr), vmcs, phys_addr);
  226. }
  227. static inline void vmcs_load(struct vmcs *vmcs)
  228. {
  229. u64 phys_addr = __pa(vmcs);
  230. if (static_branch_unlikely(&enable_evmcs))
  231. return evmcs_load(phys_addr);
  232. vmx_asm1(vmptrld, "m"(phys_addr), vmcs, phys_addr);
  233. }
  234. static inline void __invvpid(unsigned long ext, u16 vpid, gva_t gva)
  235. {
  236. struct {
  237. u64 vpid : 16;
  238. u64 rsvd : 48;
  239. u64 gva;
  240. } operand = { vpid, 0, gva };
  241. vmx_asm2(invvpid, "r"(ext), "m"(operand), ext, vpid, gva);
  242. }
  243. static inline void __invept(unsigned long ext, u64 eptp, gpa_t gpa)
  244. {
  245. struct {
  246. u64 eptp, gpa;
  247. } operand = {eptp, gpa};
  248. vmx_asm2(invept, "r"(ext), "m"(operand), ext, eptp, gpa);
  249. }
  250. static inline void vpid_sync_vcpu_single(int vpid)
  251. {
  252. if (vpid == 0)
  253. return;
  254. __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
  255. }
  256. static inline void vpid_sync_vcpu_global(void)
  257. {
  258. __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
  259. }
  260. static inline void vpid_sync_context(int vpid)
  261. {
  262. if (cpu_has_vmx_invvpid_single())
  263. vpid_sync_vcpu_single(vpid);
  264. else if (vpid != 0)
  265. vpid_sync_vcpu_global();
  266. }
  267. static inline void vpid_sync_vcpu_addr(int vpid, gva_t addr)
  268. {
  269. if (vpid == 0)
  270. return;
  271. if (cpu_has_vmx_invvpid_individual_addr())
  272. __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
  273. else
  274. vpid_sync_context(vpid);
  275. }
  276. static inline void ept_sync_global(void)
  277. {
  278. __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
  279. }
  280. static inline void ept_sync_context(u64 eptp)
  281. {
  282. if (cpu_has_vmx_invept_context())
  283. __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
  284. else
  285. ept_sync_global();
  286. }
  287. #endif /* __KVM_X86_VMX_INSN_H */