kvm_nested.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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_EL1_RES1;
  32. if (cptr_el2 & CPTR_EL2_TTA)
  33. cpacr_el1 |= CPACR_EL1_TTA;
  34. if (!(cptr_el2 & CPTR_EL2_TFP))
  35. cpacr_el1 |= CPACR_EL1_FPEN;
  36. if (!(cptr_el2 & CPTR_EL2_TZ))
  37. cpacr_el1 |= CPACR_EL1_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 bool forward_debug_exception(struct kvm_vcpu *vcpu);
  56. extern void kvm_init_nested(struct kvm *kvm);
  57. extern int kvm_vcpu_init_nested(struct kvm_vcpu *vcpu);
  58. extern void kvm_init_nested_s2_mmu(struct kvm_s2_mmu *mmu);
  59. extern struct kvm_s2_mmu *lookup_s2_mmu(struct kvm_vcpu *vcpu);
  60. union tlbi_info;
  61. extern void kvm_s2_mmu_iterate_by_vmid(struct kvm *kvm, u16 vmid,
  62. const union tlbi_info *info,
  63. void (*)(struct kvm_s2_mmu *,
  64. const union tlbi_info *));
  65. extern void kvm_vcpu_load_hw_mmu(struct kvm_vcpu *vcpu);
  66. extern void kvm_vcpu_put_hw_mmu(struct kvm_vcpu *vcpu);
  67. extern void check_nested_vcpu_requests(struct kvm_vcpu *vcpu);
  68. extern void kvm_nested_flush_hwstate(struct kvm_vcpu *vcpu);
  69. extern void kvm_nested_sync_hwstate(struct kvm_vcpu *vcpu);
  70. extern void kvm_nested_setup_mdcr_el2(struct kvm_vcpu *vcpu);
  71. struct kvm_s2_trans {
  72. phys_addr_t output;
  73. unsigned long block_size;
  74. bool writable;
  75. bool readable;
  76. int level;
  77. u32 esr;
  78. u64 desc;
  79. };
  80. static inline phys_addr_t kvm_s2_trans_output(struct kvm_s2_trans *trans)
  81. {
  82. return trans->output;
  83. }
  84. static inline unsigned long kvm_s2_trans_size(struct kvm_s2_trans *trans)
  85. {
  86. return trans->block_size;
  87. }
  88. static inline u32 kvm_s2_trans_esr(struct kvm_s2_trans *trans)
  89. {
  90. return trans->esr;
  91. }
  92. static inline bool kvm_s2_trans_readable(struct kvm_s2_trans *trans)
  93. {
  94. return trans->readable;
  95. }
  96. static inline bool kvm_s2_trans_writable(struct kvm_s2_trans *trans)
  97. {
  98. return trans->writable;
  99. }
  100. static inline bool kvm_has_xnx(struct kvm *kvm)
  101. {
  102. return cpus_have_final_cap(ARM64_HAS_XNX) &&
  103. kvm_has_feat(kvm, ID_AA64MMFR1_EL1, XNX, IMP);
  104. }
  105. static inline bool kvm_s2_trans_exec_el0(struct kvm *kvm, struct kvm_s2_trans *trans)
  106. {
  107. u8 xn = FIELD_GET(KVM_PTE_LEAF_ATTR_HI_S2_XN, trans->desc);
  108. if (!kvm_has_xnx(kvm))
  109. xn &= FIELD_PREP(KVM_PTE_LEAF_ATTR_HI_S2_XN, 0b10);
  110. switch (xn) {
  111. case 0b00:
  112. case 0b01:
  113. return true;
  114. default:
  115. return false;
  116. }
  117. }
  118. static inline bool kvm_s2_trans_exec_el1(struct kvm *kvm, struct kvm_s2_trans *trans)
  119. {
  120. u8 xn = FIELD_GET(KVM_PTE_LEAF_ATTR_HI_S2_XN, trans->desc);
  121. if (!kvm_has_xnx(kvm))
  122. xn &= FIELD_PREP(KVM_PTE_LEAF_ATTR_HI_S2_XN, 0b10);
  123. switch (xn) {
  124. case 0b00:
  125. case 0b11:
  126. return true;
  127. default:
  128. return false;
  129. }
  130. }
  131. extern int kvm_walk_nested_s2(struct kvm_vcpu *vcpu, phys_addr_t gipa,
  132. struct kvm_s2_trans *result);
  133. extern int kvm_s2_handle_perm_fault(struct kvm_vcpu *vcpu,
  134. struct kvm_s2_trans *trans);
  135. extern int kvm_inject_s2_fault(struct kvm_vcpu *vcpu, u64 esr_el2);
  136. extern void kvm_nested_s2_wp(struct kvm *kvm);
  137. extern void kvm_nested_s2_unmap(struct kvm *kvm, bool may_block);
  138. extern void kvm_nested_s2_flush(struct kvm *kvm);
  139. unsigned long compute_tlb_inval_range(struct kvm_s2_mmu *mmu, u64 val);
  140. static inline bool kvm_supported_tlbi_s1e1_op(struct kvm_vcpu *vpcu, u32 instr)
  141. {
  142. struct kvm *kvm = vpcu->kvm;
  143. u8 CRm = sys_reg_CRm(instr);
  144. if (!(sys_reg_Op0(instr) == TLBI_Op0 &&
  145. sys_reg_Op1(instr) == TLBI_Op1_EL1))
  146. return false;
  147. if (!(sys_reg_CRn(instr) == TLBI_CRn_XS ||
  148. (sys_reg_CRn(instr) == TLBI_CRn_nXS &&
  149. kvm_has_feat(kvm, ID_AA64ISAR1_EL1, XS, IMP))))
  150. return false;
  151. if (CRm == TLBI_CRm_nROS &&
  152. !kvm_has_feat(kvm, ID_AA64ISAR0_EL1, TLB, OS))
  153. return false;
  154. if ((CRm == TLBI_CRm_RIS || CRm == TLBI_CRm_ROS ||
  155. CRm == TLBI_CRm_RNS) &&
  156. !kvm_has_feat(kvm, ID_AA64ISAR0_EL1, TLB, RANGE))
  157. return false;
  158. return true;
  159. }
  160. static inline bool kvm_supported_tlbi_s1e2_op(struct kvm_vcpu *vpcu, u32 instr)
  161. {
  162. struct kvm *kvm = vpcu->kvm;
  163. u8 CRm = sys_reg_CRm(instr);
  164. if (!(sys_reg_Op0(instr) == TLBI_Op0 &&
  165. sys_reg_Op1(instr) == TLBI_Op1_EL2))
  166. return false;
  167. if (!(sys_reg_CRn(instr) == TLBI_CRn_XS ||
  168. (sys_reg_CRn(instr) == TLBI_CRn_nXS &&
  169. kvm_has_feat(kvm, ID_AA64ISAR1_EL1, XS, IMP))))
  170. return false;
  171. if (CRm == TLBI_CRm_IPAIS || CRm == TLBI_CRm_IPAONS)
  172. return false;
  173. if (CRm == TLBI_CRm_nROS &&
  174. !kvm_has_feat(kvm, ID_AA64ISAR0_EL1, TLB, OS))
  175. return false;
  176. if ((CRm == TLBI_CRm_RIS || CRm == TLBI_CRm_ROS ||
  177. CRm == TLBI_CRm_RNS) &&
  178. !kvm_has_feat(kvm, ID_AA64ISAR0_EL1, TLB, RANGE))
  179. return false;
  180. return true;
  181. }
  182. int kvm_init_nv_sysregs(struct kvm_vcpu *vcpu);
  183. u64 limit_nv_id_reg(struct kvm *kvm, u32 reg, u64 val);
  184. #ifdef CONFIG_ARM64_PTR_AUTH
  185. bool kvm_auth_eretax(struct kvm_vcpu *vcpu, u64 *elr);
  186. #else
  187. static inline bool kvm_auth_eretax(struct kvm_vcpu *vcpu, u64 *elr)
  188. {
  189. /* We really should never execute this... */
  190. WARN_ON_ONCE(1);
  191. *elr = 0xbad9acc0debadbad;
  192. return false;
  193. }
  194. #endif
  195. #define KVM_NV_GUEST_MAP_SZ (KVM_PGTABLE_PROT_SW1 | KVM_PGTABLE_PROT_SW0)
  196. static inline u64 kvm_encode_nested_level(struct kvm_s2_trans *trans)
  197. {
  198. return FIELD_PREP(KVM_NV_GUEST_MAP_SZ, trans->level);
  199. }
  200. /* Adjust alignment for the contiguous bit as per StageOA() */
  201. #define contiguous_bit_shift(d, wi, l) \
  202. ({ \
  203. u8 shift = 0; \
  204. \
  205. if ((d) & PTE_CONT) { \
  206. switch (BIT((wi)->pgshift)) { \
  207. case SZ_4K: \
  208. shift = 4; \
  209. break; \
  210. case SZ_16K: \
  211. shift = (l) == 2 ? 5 : 7; \
  212. break; \
  213. case SZ_64K: \
  214. shift = 5; \
  215. break; \
  216. } \
  217. } \
  218. \
  219. shift; \
  220. })
  221. static inline u64 decode_range_tlbi(u64 val, u64 *range, u16 *asid)
  222. {
  223. u64 base, tg, num, scale;
  224. int shift;
  225. tg = FIELD_GET(GENMASK(47, 46), val);
  226. switch(tg) {
  227. case 1:
  228. shift = 12;
  229. break;
  230. case 2:
  231. shift = 14;
  232. break;
  233. case 3:
  234. default: /* IMPDEF: handle tg==0 as 64k */
  235. shift = 16;
  236. break;
  237. }
  238. base = (val & GENMASK(36, 0)) << shift;
  239. if (asid)
  240. *asid = FIELD_GET(TLBIR_ASID_MASK, val);
  241. scale = FIELD_GET(GENMASK(45, 44), val);
  242. num = FIELD_GET(GENMASK(43, 39), val);
  243. *range = __TLBI_RANGE_PAGES(num, scale) << shift;
  244. return base;
  245. }
  246. static inline unsigned int ps_to_output_size(unsigned int ps, bool pa52bit)
  247. {
  248. switch (ps) {
  249. case 0: return 32;
  250. case 1: return 36;
  251. case 2: return 40;
  252. case 3: return 42;
  253. case 4: return 44;
  254. case 5: return 48;
  255. case 6: if (pa52bit)
  256. return 52;
  257. fallthrough;
  258. default:
  259. return 48;
  260. }
  261. }
  262. enum trans_regime {
  263. TR_EL10,
  264. TR_EL20,
  265. TR_EL2,
  266. };
  267. struct s1_walk_info;
  268. struct s1_walk_context {
  269. struct s1_walk_info *wi;
  270. u64 table_ipa;
  271. int level;
  272. };
  273. struct s1_walk_filter {
  274. int (*fn)(struct s1_walk_context *, void *);
  275. void *priv;
  276. };
  277. struct s1_walk_info {
  278. struct s1_walk_filter *filter;
  279. u64 baddr;
  280. enum trans_regime regime;
  281. unsigned int max_oa_bits;
  282. unsigned int pgshift;
  283. unsigned int txsz;
  284. int sl;
  285. u8 sh;
  286. bool as_el0;
  287. bool hpd;
  288. bool e0poe;
  289. bool poe;
  290. bool pan;
  291. bool be;
  292. bool s2;
  293. bool pa52bit;
  294. bool ha;
  295. };
  296. struct s1_walk_result {
  297. union {
  298. struct {
  299. u64 desc;
  300. u64 pa;
  301. s8 level;
  302. u8 APTable;
  303. bool nG;
  304. u16 asid;
  305. bool UXNTable;
  306. bool PXNTable;
  307. bool uwxn;
  308. bool uov;
  309. bool ur;
  310. bool uw;
  311. bool ux;
  312. bool pwxn;
  313. bool pov;
  314. bool pr;
  315. bool pw;
  316. bool px;
  317. };
  318. struct {
  319. u8 fst;
  320. bool ptw;
  321. bool s2;
  322. };
  323. };
  324. bool failed;
  325. };
  326. int __kvm_translate_va(struct kvm_vcpu *vcpu, struct s1_walk_info *wi,
  327. struct s1_walk_result *wr, u64 va);
  328. int __kvm_find_s1_desc_level(struct kvm_vcpu *vcpu, u64 va, u64 ipa,
  329. int *level);
  330. /* VNCR management */
  331. int kvm_vcpu_allocate_vncr_tlb(struct kvm_vcpu *vcpu);
  332. int kvm_handle_vncr_abort(struct kvm_vcpu *vcpu);
  333. void kvm_handle_s1e2_tlbi(struct kvm_vcpu *vcpu, u32 inst, u64 val);
  334. u16 get_asid_by_regime(struct kvm_vcpu *vcpu, enum trans_regime regime);
  335. #define vncr_fixmap(c) \
  336. ({ \
  337. u32 __c = (c); \
  338. BUG_ON(__c >= NR_CPUS); \
  339. (FIX_VNCR - __c); \
  340. })
  341. int __kvm_at_swap_desc(struct kvm *kvm, gpa_t ipa, u64 old, u64 new);
  342. #endif /* __ARM64_KVM_NESTED_H */