virtext.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /* CPU virtualization extensions handling
  3. *
  4. * This should carry the code for handling CPU virtualization extensions
  5. * that needs to live in the kernel core.
  6. *
  7. * Author: Eduardo Habkost <[email protected]>
  8. *
  9. * Copyright (C) 2008, Red Hat Inc.
  10. *
  11. * Contains code from KVM, Copyright (C) 2006 Qumranet, Inc.
  12. */
  13. #ifndef _ASM_X86_VIRTEX_H
  14. #define _ASM_X86_VIRTEX_H
  15. #include <asm/processor.h>
  16. #include <asm/vmx.h>
  17. #include <asm/svm.h>
  18. #include <asm/tlbflush.h>
  19. /*
  20. * VMX functions:
  21. */
  22. static inline int cpu_has_vmx(void)
  23. {
  24. unsigned long ecx = cpuid_ecx(1);
  25. return test_bit(5, &ecx); /* CPUID.1:ECX.VMX[bit 5] -> VT */
  26. }
  27. /**
  28. * cpu_vmxoff() - Disable VMX on the current CPU
  29. *
  30. * Disable VMX and clear CR4.VMXE (even if VMXOFF faults)
  31. *
  32. * Note, VMXOFF causes a #UD if the CPU is !post-VMXON, but it's impossible to
  33. * atomically track post-VMXON state, e.g. this may be called in NMI context.
  34. * Eat all faults as all other faults on VMXOFF faults are mode related, i.e.
  35. * faults are guaranteed to be due to the !post-VMXON check unless the CPU is
  36. * magically in RM, VM86, compat mode, or at CPL>0.
  37. */
  38. static inline int cpu_vmxoff(void)
  39. {
  40. asm_volatile_goto("1: vmxoff\n\t"
  41. _ASM_EXTABLE(1b, %l[fault])
  42. ::: "cc", "memory" : fault);
  43. cr4_clear_bits(X86_CR4_VMXE);
  44. return 0;
  45. fault:
  46. cr4_clear_bits(X86_CR4_VMXE);
  47. return -EIO;
  48. }
  49. static inline int cpu_vmx_enabled(void)
  50. {
  51. return __read_cr4() & X86_CR4_VMXE;
  52. }
  53. /** Disable VMX if it is enabled on the current CPU
  54. *
  55. * You shouldn't call this if cpu_has_vmx() returns 0.
  56. */
  57. static inline void __cpu_emergency_vmxoff(void)
  58. {
  59. if (cpu_vmx_enabled())
  60. cpu_vmxoff();
  61. }
  62. /** Disable VMX if it is supported and enabled on the current CPU
  63. */
  64. static inline void cpu_emergency_vmxoff(void)
  65. {
  66. if (cpu_has_vmx())
  67. __cpu_emergency_vmxoff();
  68. }
  69. /*
  70. * SVM functions:
  71. */
  72. /** Check if the CPU has SVM support
  73. *
  74. * You can use the 'msg' arg to get a message describing the problem,
  75. * if the function returns zero. Simply pass NULL if you are not interested
  76. * on the messages; gcc should take care of not generating code for
  77. * the messages on this case.
  78. */
  79. static inline int cpu_has_svm(const char **msg)
  80. {
  81. if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD &&
  82. boot_cpu_data.x86_vendor != X86_VENDOR_HYGON) {
  83. if (msg)
  84. *msg = "not amd or hygon";
  85. return 0;
  86. }
  87. if (!boot_cpu_has(X86_FEATURE_SVM)) {
  88. if (msg)
  89. *msg = "svm not available";
  90. return 0;
  91. }
  92. return 1;
  93. }
  94. /** Disable SVM on the current CPU
  95. *
  96. * You should call this only if cpu_has_svm() returned true.
  97. */
  98. static inline void cpu_svm_disable(void)
  99. {
  100. uint64_t efer;
  101. wrmsrl(MSR_VM_HSAVE_PA, 0);
  102. rdmsrl(MSR_EFER, efer);
  103. if (efer & EFER_SVME) {
  104. /*
  105. * Force GIF=1 prior to disabling SVM to ensure INIT and NMI
  106. * aren't blocked, e.g. if a fatal error occurred between CLGI
  107. * and STGI. Note, STGI may #UD if SVM is disabled from NMI
  108. * context between reading EFER and executing STGI. In that
  109. * case, GIF must already be set, otherwise the NMI would have
  110. * been blocked, so just eat the fault.
  111. */
  112. asm_volatile_goto("1: stgi\n\t"
  113. _ASM_EXTABLE(1b, %l[fault])
  114. ::: "memory" : fault);
  115. fault:
  116. wrmsrl(MSR_EFER, efer & ~EFER_SVME);
  117. }
  118. }
  119. /** Makes sure SVM is disabled, if it is supported on the CPU
  120. */
  121. static inline void cpu_emergency_svm_disable(void)
  122. {
  123. if (cpu_has_svm(NULL))
  124. cpu_svm_disable();
  125. }
  126. #endif /* _ASM_X86_VIRTEX_H */