virt.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. /*
  37. * HVC_GET_ICH_VTR_EL2 - Retrieve the ICH_VTR_EL2 value
  38. */
  39. #define HVC_GET_ICH_VTR_EL2 4
  40. /* Max number of HYP stub hypercalls */
  41. #define HVC_STUB_HCALL_NR 5
  42. /* Error returned when an invalid stub number is passed into x0 */
  43. #define HVC_STUB_ERR 0xbadca11
  44. #define BOOT_CPU_MODE_EL1 (0xe11)
  45. #define BOOT_CPU_MODE_EL2 (0xe12)
  46. /*
  47. * Flags returned together with the boot mode, but not preserved in
  48. * __boot_cpu_mode. Used by the idreg override code to work out the
  49. * boot state.
  50. */
  51. #define BOOT_CPU_FLAG_E2H BIT_ULL(32)
  52. #ifndef __ASSEMBLER__
  53. #include <asm/ptrace.h>
  54. #include <asm/sections.h>
  55. #include <asm/sysreg.h>
  56. #include <asm/cpufeature.h>
  57. /*
  58. * __boot_cpu_mode records what mode CPUs were booted in.
  59. * A correctly-implemented bootloader must start all CPUs in the same mode:
  60. * In this case, both 32bit halves of __boot_cpu_mode will contain the
  61. * same value (either BOOT_CPU_MODE_EL1 if booted in EL1, BOOT_CPU_MODE_EL2 if
  62. * booted in EL2).
  63. *
  64. * Should the bootloader fail to do this, the two values will be different.
  65. * This allows the kernel to flag an error when the secondaries have come up.
  66. */
  67. extern u32 __boot_cpu_mode[2];
  68. #define ARM64_VECTOR_TABLE_LEN SZ_2K
  69. void __hyp_set_vectors(phys_addr_t phys_vector_base);
  70. void __hyp_reset_vectors(void);
  71. bool is_kvm_arm_initialised(void);
  72. DECLARE_STATIC_KEY_FALSE(kvm_protected_mode_initialized);
  73. static inline bool is_pkvm_initialized(void)
  74. {
  75. return IS_ENABLED(CONFIG_KVM) &&
  76. static_branch_likely(&kvm_protected_mode_initialized);
  77. }
  78. /* Reports the availability of HYP mode */
  79. static inline bool is_hyp_mode_available(void)
  80. {
  81. /*
  82. * If KVM protected mode is initialized, all CPUs must have been booted
  83. * in EL2. Avoid checking __boot_cpu_mode as CPUs now come up in EL1.
  84. */
  85. if (is_pkvm_initialized())
  86. return true;
  87. return (__boot_cpu_mode[0] == BOOT_CPU_MODE_EL2 &&
  88. __boot_cpu_mode[1] == BOOT_CPU_MODE_EL2);
  89. }
  90. /* Check if the bootloader has booted CPUs in different modes */
  91. static inline bool is_hyp_mode_mismatched(void)
  92. {
  93. /*
  94. * If KVM protected mode is initialized, all CPUs must have been booted
  95. * in EL2. Avoid checking __boot_cpu_mode as CPUs now come up in EL1.
  96. */
  97. if (is_pkvm_initialized())
  98. return false;
  99. return __boot_cpu_mode[0] != __boot_cpu_mode[1];
  100. }
  101. static __always_inline bool is_kernel_in_hyp_mode(void)
  102. {
  103. BUILD_BUG_ON(__is_defined(__KVM_NVHE_HYPERVISOR__) ||
  104. __is_defined(__KVM_VHE_HYPERVISOR__));
  105. return read_sysreg(CurrentEL) == CurrentEL_EL2;
  106. }
  107. static __always_inline bool has_vhe(void)
  108. {
  109. /*
  110. * Code only run in VHE/NVHE hyp context can assume VHE is present or
  111. * absent. Otherwise fall back to caps.
  112. * This allows the compiler to discard VHE-specific code from the
  113. * nVHE object, reducing the number of external symbol references
  114. * needed to link.
  115. */
  116. if (is_vhe_hyp_code())
  117. return true;
  118. else if (is_nvhe_hyp_code())
  119. return false;
  120. else
  121. return cpus_have_final_cap(ARM64_HAS_VIRT_HOST_EXTN);
  122. }
  123. static __always_inline bool is_protected_kvm_enabled(void)
  124. {
  125. if (is_vhe_hyp_code())
  126. return false;
  127. else
  128. return cpus_have_final_cap(ARM64_KVM_PROTECTED_MODE);
  129. }
  130. static __always_inline bool has_hvhe(void)
  131. {
  132. if (is_vhe_hyp_code())
  133. return false;
  134. return cpus_have_final_cap(ARM64_KVM_HVHE);
  135. }
  136. static inline bool is_hyp_nvhe(void)
  137. {
  138. return is_hyp_mode_available() && !is_kernel_in_hyp_mode();
  139. }
  140. #endif /* __ASSEMBLER__ */
  141. #endif /* ! __ASM__VIRT_H */