virt.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2012 ARM Ltd.
  4. * Author: Marc Zyngier <[email protected]>
  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. DECLARE_STATIC_KEY_FALSE(kvm_protected_mode_initialized);
  67. static inline bool is_pkvm_initialized(void)
  68. {
  69. return IS_ENABLED(CONFIG_KVM) &&
  70. static_branch_likely(&kvm_protected_mode_initialized);
  71. }
  72. /* Reports the availability of HYP mode */
  73. static inline bool is_hyp_mode_available(void)
  74. {
  75. /*
  76. * If KVM protected mode is initialized, all CPUs must have been booted
  77. * in EL2. Avoid checking __boot_cpu_mode as CPUs now come up in EL1.
  78. */
  79. if (is_pkvm_initialized())
  80. return true;
  81. return (__boot_cpu_mode[0] == BOOT_CPU_MODE_EL2 &&
  82. __boot_cpu_mode[1] == BOOT_CPU_MODE_EL2);
  83. }
  84. /* Check if the bootloader has booted CPUs in different modes */
  85. static inline bool is_hyp_mode_mismatched(void)
  86. {
  87. /*
  88. * If KVM protected mode is initialized, all CPUs must have been booted
  89. * in EL2. Avoid checking __boot_cpu_mode as CPUs now come up in EL1.
  90. */
  91. if (is_pkvm_initialized())
  92. return false;
  93. return __boot_cpu_mode[0] != __boot_cpu_mode[1];
  94. }
  95. static inline bool is_kernel_in_hyp_mode(void)
  96. {
  97. return read_sysreg(CurrentEL) == CurrentEL_EL2;
  98. }
  99. static __always_inline bool has_vhe(void)
  100. {
  101. /*
  102. * Code only run in VHE/NVHE hyp context can assume VHE is present or
  103. * absent. Otherwise fall back to caps.
  104. * This allows the compiler to discard VHE-specific code from the
  105. * nVHE object, reducing the number of external symbol references
  106. * needed to link.
  107. */
  108. if (is_vhe_hyp_code())
  109. return true;
  110. else if (is_nvhe_hyp_code())
  111. return false;
  112. else
  113. return cpus_have_final_cap(ARM64_HAS_VIRT_HOST_EXTN);
  114. }
  115. static __always_inline bool is_protected_kvm_enabled(void)
  116. {
  117. if (is_vhe_hyp_code())
  118. return false;
  119. else
  120. return cpus_have_final_cap(ARM64_KVM_PROTECTED_MODE);
  121. }
  122. static inline bool is_hyp_nvhe(void)
  123. {
  124. return is_hyp_mode_available() && !is_kernel_in_hyp_mode();
  125. }
  126. #endif /* __ASSEMBLY__ */
  127. #endif /* ! __ASM__VIRT_H */