virt.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2012 ARM Ltd.
  4. * Author: Marc Zyngier <marc.zyngier@arm.com>
  5. */
  6. #ifndef __ASM__VIRT_H
  7. #define __ASM__VIRT_H
  8. /*
  9. * The arm64 hcall implementation uses x0 to specify the hcall
  10. * number. A value less than HVC_STUB_HCALL_NR indicates a special
  11. * hcall, such as set vector. Any other value is handled in a
  12. * hypervisor specific way.
  13. *
  14. * The hypercall is allowed to clobber any of the caller-saved
  15. * registers (x0-x18), so it is advisable to use it through the
  16. * indirection of a function call (as implemented in hyp-stub.S).
  17. */
  18. /*
  19. * HVC_SET_VECTORS - Set the value of the vbar_el2 register.
  20. *
  21. * @x1: Physical address of the new vector table.
  22. */
  23. #define HVC_SET_VECTORS 0
  24. /*
  25. * HVC_SOFT_RESTART - CPU soft reset, used by the cpu_soft_restart routine.
  26. */
  27. #define HVC_SOFT_RESTART 1
  28. /*
  29. * HVC_RESET_VECTORS - Restore the vectors to the original HYP stubs
  30. */
  31. #define HVC_RESET_VECTORS 2
  32. /*
  33. * HVC_FINALISE_EL2 - Upgrade the CPU from EL1 to EL2, if possible
  34. */
  35. #define HVC_FINALISE_EL2 3
  36. /* Max number of HYP stub hypercalls */
  37. #define HVC_STUB_HCALL_NR 4
  38. /* Error returned when an invalid stub number is passed into x0 */
  39. #define HVC_STUB_ERR 0xbadca11
  40. #define BOOT_CPU_MODE_EL1 (0xe11)
  41. #define BOOT_CPU_MODE_EL2 (0xe12)
  42. /*
  43. * Flags returned together with the boot mode, but not preserved in
  44. * __boot_cpu_mode. Used by the idreg override code to work out the
  45. * boot state.
  46. */
  47. #define BOOT_CPU_FLAG_E2H BIT_ULL(32)
  48. #ifndef __ASSEMBLY__
  49. #include <asm/ptrace.h>
  50. #include <asm/sections.h>
  51. #include <asm/sysreg.h>
  52. #include <asm/cpufeature.h>
  53. /*
  54. * __boot_cpu_mode records what mode CPUs were booted in.
  55. * A correctly-implemented bootloader must start all CPUs in the same mode:
  56. * In this case, both 32bit halves of __boot_cpu_mode will contain the
  57. * same value (either 0 if booted in EL1, BOOT_CPU_MODE_EL2 if booted in EL2).
  58. *
  59. * Should the bootloader fail to do this, the two values will be different.
  60. * This allows the kernel to flag an error when the secondaries have come up.
  61. */
  62. extern u32 __boot_cpu_mode[2];
  63. #define ARM64_VECTOR_TABLE_LEN SZ_2K
  64. void __hyp_set_vectors(phys_addr_t phys_vector_base);
  65. void __hyp_reset_vectors(void);
  66. bool is_kvm_arm_initialised(void);
  67. DECLARE_STATIC_KEY_FALSE(kvm_protected_mode_initialized);
  68. static inline bool is_pkvm_initialized(void)
  69. {
  70. return IS_ENABLED(CONFIG_KVM) &&
  71. static_branch_likely(&kvm_protected_mode_initialized);
  72. }
  73. /* Reports the availability of HYP mode */
  74. static inline bool is_hyp_mode_available(void)
  75. {
  76. /*
  77. * If KVM protected mode is initialized, all CPUs must have been booted
  78. * in EL2. Avoid checking __boot_cpu_mode as CPUs now come up in EL1.
  79. */
  80. if (is_pkvm_initialized())
  81. return true;
  82. return (__boot_cpu_mode[0] == BOOT_CPU_MODE_EL2 &&
  83. __boot_cpu_mode[1] == BOOT_CPU_MODE_EL2);
  84. }
  85. /* Check if the bootloader has booted CPUs in different modes */
  86. static inline bool is_hyp_mode_mismatched(void)
  87. {
  88. /*
  89. * If KVM protected mode is initialized, all CPUs must have been booted
  90. * in EL2. Avoid checking __boot_cpu_mode as CPUs now come up in EL1.
  91. */
  92. if (is_pkvm_initialized())
  93. return false;
  94. return __boot_cpu_mode[0] != __boot_cpu_mode[1];
  95. }
  96. static __always_inline bool is_kernel_in_hyp_mode(void)
  97. {
  98. BUILD_BUG_ON(__is_defined(__KVM_NVHE_HYPERVISOR__) ||
  99. __is_defined(__KVM_VHE_HYPERVISOR__));
  100. return read_sysreg(CurrentEL) == CurrentEL_EL2;
  101. }
  102. static __always_inline bool has_vhe(void)
  103. {
  104. /*
  105. * Code only run in VHE/NVHE hyp context can assume VHE is present or
  106. * absent. Otherwise fall back to caps.
  107. * This allows the compiler to discard VHE-specific code from the
  108. * nVHE object, reducing the number of external symbol references
  109. * needed to link.
  110. */
  111. if (is_vhe_hyp_code())
  112. return true;
  113. else if (is_nvhe_hyp_code())
  114. return false;
  115. else
  116. return cpus_have_final_cap(ARM64_HAS_VIRT_HOST_EXTN);
  117. }
  118. static __always_inline bool is_protected_kvm_enabled(void)
  119. {
  120. if (is_vhe_hyp_code())
  121. return false;
  122. else
  123. return cpus_have_final_cap(ARM64_KVM_PROTECTED_MODE);
  124. }
  125. static __always_inline bool has_hvhe(void)
  126. {
  127. if (is_vhe_hyp_code())
  128. return false;
  129. return cpus_have_final_cap(ARM64_KVM_HVHE);
  130. }
  131. static inline bool is_hyp_nvhe(void)
  132. {
  133. return is_hyp_mode_available() && !is_kernel_in_hyp_mode();
  134. }
  135. #endif /* __ASSEMBLY__ */
  136. #endif /* ! __ASM__VIRT_H */