kvm_mmu.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * Copyright (C) 2012,2013 - ARM Ltd
  3. * Author: Marc Zyngier <marc.zyngier@arm.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef __ARM64_KVM_MMU_H__
  18. #define __ARM64_KVM_MMU_H__
  19. #include <asm/page.h>
  20. #include <asm/memory.h>
  21. #include <asm/cpufeature.h>
  22. /*
  23. * As ARMv8.0 only has the TTBR0_EL2 register, we cannot express
  24. * "negative" addresses. This makes it impossible to directly share
  25. * mappings with the kernel.
  26. *
  27. * Instead, give the HYP mode its own VA region at a fixed offset from
  28. * the kernel by just masking the top bits (which are all ones for a
  29. * kernel address). We need to find out how many bits to mask.
  30. *
  31. * We want to build a set of page tables that cover both parts of the
  32. * idmap (the trampoline page used to initialize EL2), and our normal
  33. * runtime VA space, at the same time.
  34. *
  35. * Given that the kernel uses VA_BITS for its entire address space,
  36. * and that half of that space (VA_BITS - 1) is used for the linear
  37. * mapping, we can also limit the EL2 space to (VA_BITS - 1).
  38. *
  39. * The main question is "Within the VA_BITS space, does EL2 use the
  40. * top or the bottom half of that space to shadow the kernel's linear
  41. * mapping?". As we need to idmap the trampoline page, this is
  42. * determined by the range in which this page lives.
  43. *
  44. * If the page is in the bottom half, we have to use the top half. If
  45. * the page is in the top half, we have to use the bottom half:
  46. *
  47. * T = __virt_to_phys(__hyp_idmap_text_start)
  48. * if (T & BIT(VA_BITS - 1))
  49. * HYP_VA_MIN = 0 //idmap in upper half
  50. * else
  51. * HYP_VA_MIN = 1 << (VA_BITS - 1)
  52. * HYP_VA_MAX = HYP_VA_MIN + (1 << (VA_BITS - 1)) - 1
  53. *
  54. * This of course assumes that the trampoline page exists within the
  55. * VA_BITS range. If it doesn't, then it means we're in the odd case
  56. * where the kernel idmap (as well as HYP) uses more levels than the
  57. * kernel runtime page tables (as seen when the kernel is configured
  58. * for 4k pages, 39bits VA, and yet memory lives just above that
  59. * limit, forcing the idmap to use 4 levels of page tables while the
  60. * kernel itself only uses 3). In this particular case, it doesn't
  61. * matter which side of VA_BITS we use, as we're guaranteed not to
  62. * conflict with anything.
  63. *
  64. * When using VHE, there are no separate hyp mappings and all KVM
  65. * functionality is already mapped as part of the main kernel
  66. * mappings, and none of this applies in that case.
  67. */
  68. #define HYP_PAGE_OFFSET_HIGH_MASK ((UL(1) << VA_BITS) - 1)
  69. #define HYP_PAGE_OFFSET_LOW_MASK ((UL(1) << (VA_BITS - 1)) - 1)
  70. #ifdef __ASSEMBLY__
  71. #include <asm/alternative.h>
  72. #include <asm/cpufeature.h>
  73. /*
  74. * Convert a kernel VA into a HYP VA.
  75. * reg: VA to be converted.
  76. *
  77. * This generates the following sequences:
  78. * - High mask:
  79. * and x0, x0, #HYP_PAGE_OFFSET_HIGH_MASK
  80. * nop
  81. * - Low mask:
  82. * and x0, x0, #HYP_PAGE_OFFSET_HIGH_MASK
  83. * and x0, x0, #HYP_PAGE_OFFSET_LOW_MASK
  84. * - VHE:
  85. * nop
  86. * nop
  87. *
  88. * The "low mask" version works because the mask is a strict subset of
  89. * the "high mask", hence performing the first mask for nothing.
  90. * Should be completely invisible on any viable CPU.
  91. */
  92. .macro kern_hyp_va reg
  93. alternative_if_not ARM64_HAS_VIRT_HOST_EXTN
  94. and \reg, \reg, #HYP_PAGE_OFFSET_HIGH_MASK
  95. alternative_else_nop_endif
  96. alternative_if ARM64_HYP_OFFSET_LOW
  97. and \reg, \reg, #HYP_PAGE_OFFSET_LOW_MASK
  98. alternative_else_nop_endif
  99. .endm
  100. #else
  101. #include <asm/pgalloc.h>
  102. #include <asm/cachetype.h>
  103. #include <asm/cacheflush.h>
  104. #include <asm/mmu_context.h>
  105. #include <asm/pgtable.h>
  106. static inline unsigned long __kern_hyp_va(unsigned long v)
  107. {
  108. asm volatile(ALTERNATIVE("and %0, %0, %1",
  109. "nop",
  110. ARM64_HAS_VIRT_HOST_EXTN)
  111. : "+r" (v)
  112. : "i" (HYP_PAGE_OFFSET_HIGH_MASK));
  113. asm volatile(ALTERNATIVE("nop",
  114. "and %0, %0, %1",
  115. ARM64_HYP_OFFSET_LOW)
  116. : "+r" (v)
  117. : "i" (HYP_PAGE_OFFSET_LOW_MASK));
  118. return v;
  119. }
  120. #define kern_hyp_va(v) ((typeof(v))(__kern_hyp_va((unsigned long)(v))))
  121. /*
  122. * Obtain the PC-relative address of a kernel symbol
  123. * s: symbol
  124. *
  125. * The goal of this macro is to return a symbol's address based on a
  126. * PC-relative computation, as opposed to a loading the VA from a
  127. * constant pool or something similar. This works well for HYP, as an
  128. * absolute VA is guaranteed to be wrong. Only use this if trying to
  129. * obtain the address of a symbol (i.e. not something you obtained by
  130. * following a pointer).
  131. */
  132. #define hyp_symbol_addr(s) \
  133. ({ \
  134. typeof(s) *addr; \
  135. asm("adrp %0, %1\n" \
  136. "add %0, %0, :lo12:%1\n" \
  137. : "=r" (addr) : "S" (&s)); \
  138. addr; \
  139. })
  140. /*
  141. * We currently only support a 40bit IPA.
  142. */
  143. #define KVM_PHYS_SHIFT (40)
  144. #define KVM_PHYS_SIZE (1UL << KVM_PHYS_SHIFT)
  145. #define KVM_PHYS_MASK (KVM_PHYS_SIZE - 1UL)
  146. #include <asm/stage2_pgtable.h>
  147. int create_hyp_mappings(void *from, void *to, pgprot_t prot);
  148. int create_hyp_io_mappings(void *from, void *to, phys_addr_t);
  149. void free_hyp_pgds(void);
  150. void stage2_unmap_vm(struct kvm *kvm);
  151. int kvm_alloc_stage2_pgd(struct kvm *kvm);
  152. void kvm_free_stage2_pgd(struct kvm *kvm);
  153. int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
  154. phys_addr_t pa, unsigned long size, bool writable);
  155. int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run);
  156. void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu);
  157. phys_addr_t kvm_mmu_get_httbr(void);
  158. phys_addr_t kvm_get_idmap_vector(void);
  159. phys_addr_t kvm_get_idmap_start(void);
  160. int kvm_mmu_init(void);
  161. void kvm_clear_hyp_idmap(void);
  162. #define kvm_set_pte(ptep, pte) set_pte(ptep, pte)
  163. #define kvm_set_pmd(pmdp, pmd) set_pmd(pmdp, pmd)
  164. static inline pte_t kvm_s2pte_mkwrite(pte_t pte)
  165. {
  166. pte_val(pte) |= PTE_S2_RDWR;
  167. return pte;
  168. }
  169. static inline pmd_t kvm_s2pmd_mkwrite(pmd_t pmd)
  170. {
  171. pmd_val(pmd) |= PMD_S2_RDWR;
  172. return pmd;
  173. }
  174. static inline void kvm_set_s2pte_readonly(pte_t *pte)
  175. {
  176. pteval_t pteval;
  177. unsigned long tmp;
  178. asm volatile("// kvm_set_s2pte_readonly\n"
  179. " prfm pstl1strm, %2\n"
  180. "1: ldxr %0, %2\n"
  181. " and %0, %0, %3 // clear PTE_S2_RDWR\n"
  182. " orr %0, %0, %4 // set PTE_S2_RDONLY\n"
  183. " stxr %w1, %0, %2\n"
  184. " cbnz %w1, 1b\n"
  185. : "=&r" (pteval), "=&r" (tmp), "+Q" (pte_val(*pte))
  186. : "L" (~PTE_S2_RDWR), "L" (PTE_S2_RDONLY));
  187. }
  188. static inline bool kvm_s2pte_readonly(pte_t *pte)
  189. {
  190. return (pte_val(*pte) & PTE_S2_RDWR) == PTE_S2_RDONLY;
  191. }
  192. static inline void kvm_set_s2pmd_readonly(pmd_t *pmd)
  193. {
  194. kvm_set_s2pte_readonly((pte_t *)pmd);
  195. }
  196. static inline bool kvm_s2pmd_readonly(pmd_t *pmd)
  197. {
  198. return kvm_s2pte_readonly((pte_t *)pmd);
  199. }
  200. static inline bool kvm_page_empty(void *ptr)
  201. {
  202. struct page *ptr_page = virt_to_page(ptr);
  203. return page_count(ptr_page) == 1;
  204. }
  205. #define hyp_pte_table_empty(ptep) kvm_page_empty(ptep)
  206. #ifdef __PAGETABLE_PMD_FOLDED
  207. #define hyp_pmd_table_empty(pmdp) (0)
  208. #else
  209. #define hyp_pmd_table_empty(pmdp) kvm_page_empty(pmdp)
  210. #endif
  211. #ifdef __PAGETABLE_PUD_FOLDED
  212. #define hyp_pud_table_empty(pudp) (0)
  213. #else
  214. #define hyp_pud_table_empty(pudp) kvm_page_empty(pudp)
  215. #endif
  216. struct kvm;
  217. #define kvm_flush_dcache_to_poc(a,l) __flush_dcache_area((a), (l))
  218. static inline bool vcpu_has_cache_enabled(struct kvm_vcpu *vcpu)
  219. {
  220. return (vcpu_sys_reg(vcpu, SCTLR_EL1) & 0b101) == 0b101;
  221. }
  222. static inline void __coherent_cache_guest_page(struct kvm_vcpu *vcpu,
  223. kvm_pfn_t pfn,
  224. unsigned long size,
  225. bool ipa_uncached)
  226. {
  227. void *va = page_address(pfn_to_page(pfn));
  228. kvm_flush_dcache_to_poc(va, size);
  229. if (!icache_is_aliasing()) { /* PIPT */
  230. flush_icache_range((unsigned long)va,
  231. (unsigned long)va + size);
  232. } else if (!icache_is_aivivt()) { /* non ASID-tagged VIVT */
  233. /* any kind of VIPT cache */
  234. __flush_icache_all();
  235. }
  236. }
  237. static inline void __kvm_flush_dcache_pte(pte_t pte)
  238. {
  239. struct page *page = pte_page(pte);
  240. kvm_flush_dcache_to_poc(page_address(page), PAGE_SIZE);
  241. }
  242. static inline void __kvm_flush_dcache_pmd(pmd_t pmd)
  243. {
  244. struct page *page = pmd_page(pmd);
  245. kvm_flush_dcache_to_poc(page_address(page), PMD_SIZE);
  246. }
  247. static inline void __kvm_flush_dcache_pud(pud_t pud)
  248. {
  249. struct page *page = pud_page(pud);
  250. kvm_flush_dcache_to_poc(page_address(page), PUD_SIZE);
  251. }
  252. #define kvm_virt_to_phys(x) __virt_to_phys((unsigned long)(x))
  253. void kvm_set_way_flush(struct kvm_vcpu *vcpu);
  254. void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled);
  255. static inline bool __kvm_cpu_uses_extended_idmap(void)
  256. {
  257. return __cpu_uses_extended_idmap();
  258. }
  259. static inline void __kvm_extend_hypmap(pgd_t *boot_hyp_pgd,
  260. pgd_t *hyp_pgd,
  261. pgd_t *merged_hyp_pgd,
  262. unsigned long hyp_idmap_start)
  263. {
  264. int idmap_idx;
  265. /*
  266. * Use the first entry to access the HYP mappings. It is
  267. * guaranteed to be free, otherwise we wouldn't use an
  268. * extended idmap.
  269. */
  270. VM_BUG_ON(pgd_val(merged_hyp_pgd[0]));
  271. merged_hyp_pgd[0] = __pgd(__pa(hyp_pgd) | PMD_TYPE_TABLE);
  272. /*
  273. * Create another extended level entry that points to the boot HYP map,
  274. * which contains an ID mapping of the HYP init code. We essentially
  275. * merge the boot and runtime HYP maps by doing so, but they don't
  276. * overlap anyway, so this is fine.
  277. */
  278. idmap_idx = hyp_idmap_start >> VA_BITS;
  279. VM_BUG_ON(pgd_val(merged_hyp_pgd[idmap_idx]));
  280. merged_hyp_pgd[idmap_idx] = __pgd(__pa(boot_hyp_pgd) | PMD_TYPE_TABLE);
  281. }
  282. static inline unsigned int kvm_get_vmid_bits(void)
  283. {
  284. int reg = read_system_reg(SYS_ID_AA64MMFR1_EL1);
  285. return (cpuid_feature_extract_unsigned_field(reg, ID_AA64MMFR1_VMIDBITS_SHIFT) == 2) ? 16 : 8;
  286. }
  287. /*
  288. * We are not in the kvm->srcu critical section most of the time, so we take
  289. * the SRCU read lock here. Since we copy the data from the user page, we
  290. * can immediately drop the lock again.
  291. */
  292. static inline int kvm_read_guest_lock(struct kvm *kvm,
  293. gpa_t gpa, void *data, unsigned long len)
  294. {
  295. int srcu_idx = srcu_read_lock(&kvm->srcu);
  296. int ret = kvm_read_guest(kvm, gpa, data, len);
  297. srcu_read_unlock(&kvm->srcu, srcu_idx);
  298. return ret;
  299. }
  300. #ifdef CONFIG_HARDEN_BRANCH_PREDICTOR
  301. #include <asm/mmu.h>
  302. static inline void *kvm_get_hyp_vector(void)
  303. {
  304. struct bp_hardening_data *data = arm64_get_bp_hardening_data();
  305. void *vect = kvm_ksym_ref(__kvm_hyp_vector);
  306. if (data->fn) {
  307. vect = __bp_harden_hyp_vecs_start +
  308. data->hyp_vectors_slot * SZ_2K;
  309. if (!cpus_have_const_cap(ARM64_HAS_VIRT_HOST_EXTN))
  310. vect = lm_alias(vect);
  311. }
  312. return vect;
  313. }
  314. static inline int kvm_map_vectors(void)
  315. {
  316. return create_hyp_mappings(kvm_ksym_ref(__bp_harden_hyp_vecs_start),
  317. kvm_ksym_ref(__bp_harden_hyp_vecs_end),
  318. PAGE_HYP_EXEC);
  319. }
  320. #else
  321. static inline void *kvm_get_hyp_vector(void)
  322. {
  323. return kvm_ksym_ref(__kvm_hyp_vector);
  324. }
  325. static inline int kvm_map_vectors(void)
  326. {
  327. return 0;
  328. }
  329. #endif
  330. #ifdef CONFIG_ARM64_SSBD
  331. DECLARE_PER_CPU_READ_MOSTLY(u64, arm64_ssbd_callback_required);
  332. static inline int hyp_map_aux_data(void)
  333. {
  334. int cpu, err;
  335. for_each_possible_cpu(cpu) {
  336. u64 *ptr;
  337. ptr = per_cpu_ptr(&arm64_ssbd_callback_required, cpu);
  338. err = create_hyp_mappings(ptr, ptr + 1, PAGE_HYP);
  339. if (err)
  340. return err;
  341. }
  342. return 0;
  343. }
  344. #else
  345. static inline int hyp_map_aux_data(void)
  346. {
  347. return 0;
  348. }
  349. #endif
  350. #endif /* __ASSEMBLY__ */
  351. #endif /* __ARM64_KVM_MMU_H__ */