kvm_nested.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __ARM64_KVM_NESTED_H
  3. #define __ARM64_KVM_NESTED_H
  4. #include <linux/bitfield.h>
  5. #include <linux/kvm_host.h>
  6. #include <asm/kvm_emulate.h>
  7. #include <asm/kvm_pgtable.h>
  8. static inline bool vcpu_has_nv(const struct kvm_vcpu *vcpu)
  9. {
  10. return (!__is_defined(__KVM_NVHE_HYPERVISOR__) &&
  11. cpus_have_final_cap(ARM64_HAS_NESTED_VIRT) &&
  12. vcpu_has_feature(vcpu, KVM_ARM_VCPU_HAS_EL2));
  13. }
  14. /* Translation helpers from non-VHE EL2 to EL1 */
  15. static inline u64 tcr_el2_ps_to_tcr_el1_ips(u64 tcr_el2)
  16. {
  17. return (u64)FIELD_GET(TCR_EL2_PS_MASK, tcr_el2) << TCR_IPS_SHIFT;
  18. }
  19. static inline u64 translate_tcr_el2_to_tcr_el1(u64 tcr)
  20. {
  21. return TCR_EPD1_MASK | /* disable TTBR1_EL1 */
  22. ((tcr & TCR_EL2_TBI) ? TCR_TBI0 : 0) |
  23. tcr_el2_ps_to_tcr_el1_ips(tcr) |
  24. (tcr & TCR_EL2_TG0_MASK) |
  25. (tcr & TCR_EL2_ORGN0_MASK) |
  26. (tcr & TCR_EL2_IRGN0_MASK) |
  27. (tcr & TCR_EL2_T0SZ_MASK);
  28. }
  29. static inline u64 translate_cptr_el2_to_cpacr_el1(u64 cptr_el2)
  30. {
  31. u64 cpacr_el1 = CPACR_ELx_RES1;
  32. if (cptr_el2 & CPTR_EL2_TTA)
  33. cpacr_el1 |= CPACR_ELx_TTA;
  34. if (!(cptr_el2 & CPTR_EL2_TFP))
  35. cpacr_el1 |= CPACR_ELx_FPEN;
  36. if (!(cptr_el2 & CPTR_EL2_TZ))
  37. cpacr_el1 |= CPACR_ELx_ZEN;
  38. cpacr_el1 |= cptr_el2 & (CPTR_EL2_TCPAC | CPTR_EL2_TAM);
  39. return cpacr_el1;
  40. }
  41. static inline u64 translate_sctlr_el2_to_sctlr_el1(u64 val)
  42. {
  43. /* Only preserve the minimal set of bits we support */
  44. val &= (SCTLR_ELx_M | SCTLR_ELx_A | SCTLR_ELx_C | SCTLR_ELx_SA |
  45. SCTLR_ELx_I | SCTLR_ELx_IESB | SCTLR_ELx_WXN | SCTLR_ELx_EE);
  46. val |= SCTLR_EL1_RES1;
  47. return val;
  48. }
  49. static inline u64 translate_ttbr0_el2_to_ttbr0_el1(u64 ttbr0)
  50. {
  51. /* Clear the ASID field */
  52. return ttbr0 & ~GENMASK_ULL(63, 48);
  53. }
  54. extern bool forward_smc_trap(struct kvm_vcpu *vcpu);
  55. extern void kvm_init_nested(struct kvm *kvm);
  56. extern int kvm_vcpu_init_nested(struct kvm_vcpu *vcpu);
  57. extern void kvm_init_nested_s2_mmu(struct kvm_s2_mmu *mmu);
  58. extern struct kvm_s2_mmu *lookup_s2_mmu(struct kvm_vcpu *vcpu);
  59. union tlbi_info;
  60. extern void kvm_s2_mmu_iterate_by_vmid(struct kvm *kvm, u16 vmid,
  61. const union tlbi_info *info,
  62. void (*)(struct kvm_s2_mmu *,
  63. const union tlbi_info *));
  64. extern void kvm_vcpu_load_hw_mmu(struct kvm_vcpu *vcpu);
  65. extern void kvm_vcpu_put_hw_mmu(struct kvm_vcpu *vcpu);
  66. extern void check_nested_vcpu_requests(struct kvm_vcpu *vcpu);
  67. struct kvm_s2_trans {
  68. phys_addr_t output;
  69. unsigned long block_size;
  70. bool writable;
  71. bool readable;
  72. int level;
  73. u32 esr;
  74. u64 desc;
  75. };
  76. static inline phys_addr_t kvm_s2_trans_output(struct kvm_s2_trans *trans)
  77. {
  78. return trans->output;
  79. }
  80. static inline unsigned long kvm_s2_trans_size(struct kvm_s2_trans *trans)
  81. {
  82. return trans->block_size;
  83. }
  84. static inline u32 kvm_s2_trans_esr(struct kvm_s2_trans *trans)
  85. {
  86. return trans->esr;
  87. }
  88. static inline bool kvm_s2_trans_readable(struct kvm_s2_trans *trans)
  89. {
  90. return trans->readable;
  91. }
  92. static inline bool kvm_s2_trans_writable(struct kvm_s2_trans *trans)
  93. {
  94. return trans->writable;
  95. }
  96. static inline bool kvm_s2_trans_executable(struct kvm_s2_trans *trans)
  97. {
  98. return !(trans->desc & BIT(54));
  99. }
  100. extern int kvm_walk_nested_s2(struct kvm_vcpu *vcpu, phys_addr_t gipa,
  101. struct kvm_s2_trans *result);
  102. extern int kvm_s2_handle_perm_fault(struct kvm_vcpu *vcpu,
  103. struct kvm_s2_trans *trans);
  104. extern int kvm_inject_s2_fault(struct kvm_vcpu *vcpu, u64 esr_el2);
  105. extern void kvm_nested_s2_wp(struct kvm *kvm);
  106. extern void kvm_nested_s2_unmap(struct kvm *kvm, bool may_block);
  107. extern void kvm_nested_s2_flush(struct kvm *kvm);
  108. unsigned long compute_tlb_inval_range(struct kvm_s2_mmu *mmu, u64 val);
  109. static inline bool kvm_supported_tlbi_s1e1_op(struct kvm_vcpu *vpcu, u32 instr)
  110. {
  111. struct kvm *kvm = vpcu->kvm;
  112. u8 CRm = sys_reg_CRm(instr);
  113. if (!(sys_reg_Op0(instr) == TLBI_Op0 &&
  114. sys_reg_Op1(instr) == TLBI_Op1_EL1))
  115. return false;
  116. if (!(sys_reg_CRn(instr) == TLBI_CRn_XS ||
  117. (sys_reg_CRn(instr) == TLBI_CRn_nXS &&
  118. kvm_has_feat(kvm, ID_AA64ISAR1_EL1, XS, IMP))))
  119. return false;
  120. if (CRm == TLBI_CRm_nROS &&
  121. !kvm_has_feat(kvm, ID_AA64ISAR0_EL1, TLB, OS))
  122. return false;
  123. if ((CRm == TLBI_CRm_RIS || CRm == TLBI_CRm_ROS ||
  124. CRm == TLBI_CRm_RNS) &&
  125. !kvm_has_feat(kvm, ID_AA64ISAR0_EL1, TLB, RANGE))
  126. return false;
  127. return true;
  128. }
  129. static inline bool kvm_supported_tlbi_s1e2_op(struct kvm_vcpu *vpcu, u32 instr)
  130. {
  131. struct kvm *kvm = vpcu->kvm;
  132. u8 CRm = sys_reg_CRm(instr);
  133. if (!(sys_reg_Op0(instr) == TLBI_Op0 &&
  134. sys_reg_Op1(instr) == TLBI_Op1_EL2))
  135. return false;
  136. if (!(sys_reg_CRn(instr) == TLBI_CRn_XS ||
  137. (sys_reg_CRn(instr) == TLBI_CRn_nXS &&
  138. kvm_has_feat(kvm, ID_AA64ISAR1_EL1, XS, IMP))))
  139. return false;
  140. if (CRm == TLBI_CRm_IPAIS || CRm == TLBI_CRm_IPAONS)
  141. return false;
  142. if (CRm == TLBI_CRm_nROS &&
  143. !kvm_has_feat(kvm, ID_AA64ISAR0_EL1, TLB, OS))
  144. return false;
  145. if ((CRm == TLBI_CRm_RIS || CRm == TLBI_CRm_ROS ||
  146. CRm == TLBI_CRm_RNS) &&
  147. !kvm_has_feat(kvm, ID_AA64ISAR0_EL1, TLB, RANGE))
  148. return false;
  149. return true;
  150. }
  151. int kvm_init_nv_sysregs(struct kvm *kvm);
  152. #ifdef CONFIG_ARM64_PTR_AUTH
  153. bool kvm_auth_eretax(struct kvm_vcpu *vcpu, u64 *elr);
  154. #else
  155. static inline bool kvm_auth_eretax(struct kvm_vcpu *vcpu, u64 *elr)
  156. {
  157. /* We really should never execute this... */
  158. WARN_ON_ONCE(1);
  159. *elr = 0xbad9acc0debadbad;
  160. return false;
  161. }
  162. #endif
  163. #define KVM_NV_GUEST_MAP_SZ (KVM_PGTABLE_PROT_SW1 | KVM_PGTABLE_PROT_SW0)
  164. static inline u64 kvm_encode_nested_level(struct kvm_s2_trans *trans)
  165. {
  166. return FIELD_PREP(KVM_NV_GUEST_MAP_SZ, trans->level);
  167. }
  168. /* Adjust alignment for the contiguous bit as per StageOA() */
  169. #define contiguous_bit_shift(d, wi, l) \
  170. ({ \
  171. u8 shift = 0; \
  172. \
  173. if ((d) & PTE_CONT) { \
  174. switch (BIT((wi)->pgshift)) { \
  175. case SZ_4K: \
  176. shift = 4; \
  177. break; \
  178. case SZ_16K: \
  179. shift = (l) == 2 ? 5 : 7; \
  180. break; \
  181. case SZ_64K: \
  182. shift = 5; \
  183. break; \
  184. } \
  185. } \
  186. \
  187. shift; \
  188. })
  189. static inline unsigned int ps_to_output_size(unsigned int ps)
  190. {
  191. switch (ps) {
  192. case 0: return 32;
  193. case 1: return 36;
  194. case 2: return 40;
  195. case 3: return 42;
  196. case 4: return 44;
  197. case 5:
  198. default:
  199. return 48;
  200. }
  201. }
  202. #endif /* __ARM64_KVM_NESTED_H */