kvm_mmu.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. /*
  22. * As we only have the TTBR0_EL2 register, we cannot express
  23. * "negative" addresses. This makes it impossible to directly share
  24. * mappings with the kernel.
  25. *
  26. * Instead, give the HYP mode its own VA region at a fixed offset from
  27. * the kernel by just masking the top bits (which are all ones for a
  28. * kernel address).
  29. */
  30. #define HYP_PAGE_OFFSET_SHIFT VA_BITS
  31. #define HYP_PAGE_OFFSET_MASK ((UL(1) << HYP_PAGE_OFFSET_SHIFT) - 1)
  32. #define HYP_PAGE_OFFSET (PAGE_OFFSET & HYP_PAGE_OFFSET_MASK)
  33. /*
  34. * Our virtual mapping for the idmap-ed MMU-enable code. Must be
  35. * shared across all the page-tables. Conveniently, we use the last
  36. * possible page, where no kernel mapping will ever exist.
  37. */
  38. #define TRAMPOLINE_VA (HYP_PAGE_OFFSET_MASK & PAGE_MASK)
  39. /*
  40. * KVM_MMU_CACHE_MIN_PAGES is the number of stage2 page table translation
  41. * levels in addition to the PGD and potentially the PUD which are
  42. * pre-allocated (we pre-allocate the fake PGD and the PUD when the Stage-2
  43. * tables use one level of tables less than the kernel.
  44. */
  45. #ifdef CONFIG_ARM64_64K_PAGES
  46. #define KVM_MMU_CACHE_MIN_PAGES 1
  47. #else
  48. #define KVM_MMU_CACHE_MIN_PAGES 2
  49. #endif
  50. #ifdef __ASSEMBLY__
  51. /*
  52. * Convert a kernel VA into a HYP VA.
  53. * reg: VA to be converted.
  54. */
  55. .macro kern_hyp_va reg
  56. and \reg, \reg, #HYP_PAGE_OFFSET_MASK
  57. .endm
  58. #else
  59. #include <asm/pgalloc.h>
  60. #include <asm/cachetype.h>
  61. #include <asm/cacheflush.h>
  62. #include <asm/mmu_context.h>
  63. #include <asm/pgtable.h>
  64. #define KERN_TO_HYP(kva) ((unsigned long)kva - PAGE_OFFSET + HYP_PAGE_OFFSET)
  65. /*
  66. * We currently only support a 40bit IPA.
  67. */
  68. #define KVM_PHYS_SHIFT (40)
  69. #define KVM_PHYS_SIZE (1UL << KVM_PHYS_SHIFT)
  70. #define KVM_PHYS_MASK (KVM_PHYS_SIZE - 1UL)
  71. int create_hyp_mappings(void *from, void *to);
  72. int create_hyp_io_mappings(void *from, void *to, phys_addr_t);
  73. void free_boot_hyp_pgd(void);
  74. void free_hyp_pgds(void);
  75. void stage2_unmap_vm(struct kvm *kvm);
  76. int kvm_alloc_stage2_pgd(struct kvm *kvm);
  77. void kvm_free_stage2_pgd(struct kvm *kvm);
  78. int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
  79. phys_addr_t pa, unsigned long size, bool writable);
  80. int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run);
  81. void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu);
  82. phys_addr_t kvm_mmu_get_httbr(void);
  83. phys_addr_t kvm_mmu_get_boot_httbr(void);
  84. phys_addr_t kvm_get_idmap_vector(void);
  85. int kvm_mmu_init(void);
  86. void kvm_clear_hyp_idmap(void);
  87. #define kvm_set_pte(ptep, pte) set_pte(ptep, pte)
  88. #define kvm_set_pmd(pmdp, pmd) set_pmd(pmdp, pmd)
  89. static inline void kvm_clean_pgd(pgd_t *pgd) {}
  90. static inline void kvm_clean_pmd(pmd_t *pmd) {}
  91. static inline void kvm_clean_pmd_entry(pmd_t *pmd) {}
  92. static inline void kvm_clean_pte(pte_t *pte) {}
  93. static inline void kvm_clean_pte_entry(pte_t *pte) {}
  94. static inline void kvm_set_s2pte_writable(pte_t *pte)
  95. {
  96. pte_val(*pte) |= PTE_S2_RDWR;
  97. }
  98. static inline void kvm_set_s2pmd_writable(pmd_t *pmd)
  99. {
  100. pmd_val(*pmd) |= PMD_S2_RDWR;
  101. }
  102. static inline void kvm_set_s2pte_readonly(pte_t *pte)
  103. {
  104. pte_val(*pte) = (pte_val(*pte) & ~PTE_S2_RDWR) | PTE_S2_RDONLY;
  105. }
  106. static inline bool kvm_s2pte_readonly(pte_t *pte)
  107. {
  108. return (pte_val(*pte) & PTE_S2_RDWR) == PTE_S2_RDONLY;
  109. }
  110. static inline void kvm_set_s2pmd_readonly(pmd_t *pmd)
  111. {
  112. pmd_val(*pmd) = (pmd_val(*pmd) & ~PMD_S2_RDWR) | PMD_S2_RDONLY;
  113. }
  114. static inline bool kvm_s2pmd_readonly(pmd_t *pmd)
  115. {
  116. return (pmd_val(*pmd) & PMD_S2_RDWR) == PMD_S2_RDONLY;
  117. }
  118. #define kvm_pgd_addr_end(addr, end) pgd_addr_end(addr, end)
  119. #define kvm_pud_addr_end(addr, end) pud_addr_end(addr, end)
  120. #define kvm_pmd_addr_end(addr, end) pmd_addr_end(addr, end)
  121. /*
  122. * In the case where PGDIR_SHIFT is larger than KVM_PHYS_SHIFT, we can address
  123. * the entire IPA input range with a single pgd entry, and we would only need
  124. * one pgd entry. Note that in this case, the pgd is actually not used by
  125. * the MMU for Stage-2 translations, but is merely a fake pgd used as a data
  126. * structure for the kernel pgtable macros to work.
  127. */
  128. #if PGDIR_SHIFT > KVM_PHYS_SHIFT
  129. #define PTRS_PER_S2_PGD_SHIFT 0
  130. #else
  131. #define PTRS_PER_S2_PGD_SHIFT (KVM_PHYS_SHIFT - PGDIR_SHIFT)
  132. #endif
  133. #define PTRS_PER_S2_PGD (1 << PTRS_PER_S2_PGD_SHIFT)
  134. #define S2_PGD_ORDER get_order(PTRS_PER_S2_PGD * sizeof(pgd_t))
  135. #define kvm_pgd_index(addr) (((addr) >> PGDIR_SHIFT) & (PTRS_PER_S2_PGD - 1))
  136. /*
  137. * If we are concatenating first level stage-2 page tables, we would have less
  138. * than or equal to 16 pointers in the fake PGD, because that's what the
  139. * architecture allows. In this case, (4 - CONFIG_PGTABLE_LEVELS)
  140. * represents the first level for the host, and we add 1 to go to the next
  141. * level (which uses contatenation) for the stage-2 tables.
  142. */
  143. #if PTRS_PER_S2_PGD <= 16
  144. #define KVM_PREALLOC_LEVEL (4 - CONFIG_PGTABLE_LEVELS + 1)
  145. #else
  146. #define KVM_PREALLOC_LEVEL (0)
  147. #endif
  148. static inline void *kvm_get_hwpgd(struct kvm *kvm)
  149. {
  150. pgd_t *pgd = kvm->arch.pgd;
  151. pud_t *pud;
  152. if (KVM_PREALLOC_LEVEL == 0)
  153. return pgd;
  154. pud = pud_offset(pgd, 0);
  155. if (KVM_PREALLOC_LEVEL == 1)
  156. return pud;
  157. BUG_ON(KVM_PREALLOC_LEVEL != 2);
  158. return pmd_offset(pud, 0);
  159. }
  160. static inline unsigned int kvm_get_hwpgd_size(void)
  161. {
  162. if (KVM_PREALLOC_LEVEL > 0)
  163. return PTRS_PER_S2_PGD * PAGE_SIZE;
  164. return PTRS_PER_S2_PGD * sizeof(pgd_t);
  165. }
  166. static inline bool kvm_page_empty(void *ptr)
  167. {
  168. struct page *ptr_page = virt_to_page(ptr);
  169. return page_count(ptr_page) == 1;
  170. }
  171. #define kvm_pte_table_empty(kvm, ptep) kvm_page_empty(ptep)
  172. #ifdef __PAGETABLE_PMD_FOLDED
  173. #define kvm_pmd_table_empty(kvm, pmdp) (0)
  174. #else
  175. #define kvm_pmd_table_empty(kvm, pmdp) \
  176. (kvm_page_empty(pmdp) && (!(kvm) || KVM_PREALLOC_LEVEL < 2))
  177. #endif
  178. #ifdef __PAGETABLE_PUD_FOLDED
  179. #define kvm_pud_table_empty(kvm, pudp) (0)
  180. #else
  181. #define kvm_pud_table_empty(kvm, pudp) \
  182. (kvm_page_empty(pudp) && (!(kvm) || KVM_PREALLOC_LEVEL < 1))
  183. #endif
  184. struct kvm;
  185. #define kvm_flush_dcache_to_poc(a,l) __flush_dcache_area((a), (l))
  186. static inline bool vcpu_has_cache_enabled(struct kvm_vcpu *vcpu)
  187. {
  188. return (vcpu_sys_reg(vcpu, SCTLR_EL1) & 0b101) == 0b101;
  189. }
  190. static inline void __coherent_cache_guest_page(struct kvm_vcpu *vcpu, pfn_t pfn,
  191. unsigned long size,
  192. bool ipa_uncached)
  193. {
  194. void *va = page_address(pfn_to_page(pfn));
  195. kvm_flush_dcache_to_poc(va, size);
  196. if (!icache_is_aliasing()) { /* PIPT */
  197. flush_icache_range((unsigned long)va,
  198. (unsigned long)va + size);
  199. } else if (!icache_is_aivivt()) { /* non ASID-tagged VIVT */
  200. /* any kind of VIPT cache */
  201. __flush_icache_all();
  202. }
  203. }
  204. static inline void __kvm_flush_dcache_pte(pte_t pte)
  205. {
  206. struct page *page = pte_page(pte);
  207. kvm_flush_dcache_to_poc(page_address(page), PAGE_SIZE);
  208. }
  209. static inline void __kvm_flush_dcache_pmd(pmd_t pmd)
  210. {
  211. struct page *page = pmd_page(pmd);
  212. kvm_flush_dcache_to_poc(page_address(page), PMD_SIZE);
  213. }
  214. static inline void __kvm_flush_dcache_pud(pud_t pud)
  215. {
  216. struct page *page = pud_page(pud);
  217. kvm_flush_dcache_to_poc(page_address(page), PUD_SIZE);
  218. }
  219. #define kvm_virt_to_phys(x) __virt_to_phys((unsigned long)(x))
  220. void kvm_set_way_flush(struct kvm_vcpu *vcpu);
  221. void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled);
  222. static inline bool __kvm_cpu_uses_extended_idmap(void)
  223. {
  224. return __cpu_uses_extended_idmap();
  225. }
  226. static inline void __kvm_extend_hypmap(pgd_t *boot_hyp_pgd,
  227. pgd_t *hyp_pgd,
  228. pgd_t *merged_hyp_pgd,
  229. unsigned long hyp_idmap_start)
  230. {
  231. int idmap_idx;
  232. /*
  233. * Use the first entry to access the HYP mappings. It is
  234. * guaranteed to be free, otherwise we wouldn't use an
  235. * extended idmap.
  236. */
  237. VM_BUG_ON(pgd_val(merged_hyp_pgd[0]));
  238. merged_hyp_pgd[0] = __pgd(__pa(hyp_pgd) | PMD_TYPE_TABLE);
  239. /*
  240. * Create another extended level entry that points to the boot HYP map,
  241. * which contains an ID mapping of the HYP init code. We essentially
  242. * merge the boot and runtime HYP maps by doing so, but they don't
  243. * overlap anyway, so this is fine.
  244. */
  245. idmap_idx = hyp_idmap_start >> VA_BITS;
  246. VM_BUG_ON(pgd_val(merged_hyp_pgd[idmap_idx]));
  247. merged_hyp_pgd[idmap_idx] = __pgd(__pa(boot_hyp_pgd) | PMD_TYPE_TABLE);
  248. }
  249. #endif /* __ASSEMBLY__ */
  250. #endif /* __ARM64_KVM_MMU_H__ */