kvm_mmu.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. #define KERN_TO_HYP(kva) ((unsigned long)kva - PAGE_OFFSET + HYP_PAGE_OFFSET)
  63. /*
  64. * We currently only support a 40bit IPA.
  65. */
  66. #define KVM_PHYS_SHIFT (40)
  67. #define KVM_PHYS_SIZE (1UL << KVM_PHYS_SHIFT)
  68. #define KVM_PHYS_MASK (KVM_PHYS_SIZE - 1UL)
  69. int create_hyp_mappings(void *from, void *to);
  70. int create_hyp_io_mappings(void *from, void *to, phys_addr_t);
  71. void free_boot_hyp_pgd(void);
  72. void free_hyp_pgds(void);
  73. int kvm_alloc_stage2_pgd(struct kvm *kvm);
  74. void kvm_free_stage2_pgd(struct kvm *kvm);
  75. int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
  76. phys_addr_t pa, unsigned long size, bool writable);
  77. int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run);
  78. void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu);
  79. phys_addr_t kvm_mmu_get_httbr(void);
  80. phys_addr_t kvm_mmu_get_boot_httbr(void);
  81. phys_addr_t kvm_get_idmap_vector(void);
  82. int kvm_mmu_init(void);
  83. void kvm_clear_hyp_idmap(void);
  84. #define kvm_set_pte(ptep, pte) set_pte(ptep, pte)
  85. #define kvm_set_pmd(pmdp, pmd) set_pmd(pmdp, pmd)
  86. static inline void kvm_clean_pgd(pgd_t *pgd) {}
  87. static inline void kvm_clean_pmd(pmd_t *pmd) {}
  88. static inline void kvm_clean_pmd_entry(pmd_t *pmd) {}
  89. static inline void kvm_clean_pte(pte_t *pte) {}
  90. static inline void kvm_clean_pte_entry(pte_t *pte) {}
  91. static inline void kvm_set_s2pte_writable(pte_t *pte)
  92. {
  93. pte_val(*pte) |= PTE_S2_RDWR;
  94. }
  95. static inline void kvm_set_s2pmd_writable(pmd_t *pmd)
  96. {
  97. pmd_val(*pmd) |= PMD_S2_RDWR;
  98. }
  99. #define kvm_pgd_addr_end(addr, end) pgd_addr_end(addr, end)
  100. #define kvm_pud_addr_end(addr, end) pud_addr_end(addr, end)
  101. #define kvm_pmd_addr_end(addr, end) pmd_addr_end(addr, end)
  102. /*
  103. * In the case where PGDIR_SHIFT is larger than KVM_PHYS_SHIFT, we can address
  104. * the entire IPA input range with a single pgd entry, and we would only need
  105. * one pgd entry. Note that in this case, the pgd is actually not used by
  106. * the MMU for Stage-2 translations, but is merely a fake pgd used as a data
  107. * structure for the kernel pgtable macros to work.
  108. */
  109. #if PGDIR_SHIFT > KVM_PHYS_SHIFT
  110. #define PTRS_PER_S2_PGD_SHIFT 0
  111. #else
  112. #define PTRS_PER_S2_PGD_SHIFT (KVM_PHYS_SHIFT - PGDIR_SHIFT)
  113. #endif
  114. #define PTRS_PER_S2_PGD (1 << PTRS_PER_S2_PGD_SHIFT)
  115. #define S2_PGD_ORDER get_order(PTRS_PER_S2_PGD * sizeof(pgd_t))
  116. /*
  117. * If we are concatenating first level stage-2 page tables, we would have less
  118. * than or equal to 16 pointers in the fake PGD, because that's what the
  119. * architecture allows. In this case, (4 - CONFIG_ARM64_PGTABLE_LEVELS)
  120. * represents the first level for the host, and we add 1 to go to the next
  121. * level (which uses contatenation) for the stage-2 tables.
  122. */
  123. #if PTRS_PER_S2_PGD <= 16
  124. #define KVM_PREALLOC_LEVEL (4 - CONFIG_ARM64_PGTABLE_LEVELS + 1)
  125. #else
  126. #define KVM_PREALLOC_LEVEL (0)
  127. #endif
  128. /**
  129. * kvm_prealloc_hwpgd - allocate inital table for VTTBR
  130. * @kvm: The KVM struct pointer for the VM.
  131. * @pgd: The kernel pseudo pgd
  132. *
  133. * When the kernel uses more levels of page tables than the guest, we allocate
  134. * a fake PGD and pre-populate it to point to the next-level page table, which
  135. * will be the real initial page table pointed to by the VTTBR.
  136. *
  137. * When KVM_PREALLOC_LEVEL==2, we allocate a single page for the PMD and
  138. * the kernel will use folded pud. When KVM_PREALLOC_LEVEL==1, we
  139. * allocate 2 consecutive PUD pages.
  140. */
  141. static inline int kvm_prealloc_hwpgd(struct kvm *kvm, pgd_t *pgd)
  142. {
  143. unsigned int i;
  144. unsigned long hwpgd;
  145. if (KVM_PREALLOC_LEVEL == 0)
  146. return 0;
  147. hwpgd = __get_free_pages(GFP_KERNEL | __GFP_ZERO, PTRS_PER_S2_PGD_SHIFT);
  148. if (!hwpgd)
  149. return -ENOMEM;
  150. for (i = 0; i < PTRS_PER_S2_PGD; i++) {
  151. if (KVM_PREALLOC_LEVEL == 1)
  152. pgd_populate(NULL, pgd + i,
  153. (pud_t *)hwpgd + i * PTRS_PER_PUD);
  154. else if (KVM_PREALLOC_LEVEL == 2)
  155. pud_populate(NULL, pud_offset(pgd, 0) + i,
  156. (pmd_t *)hwpgd + i * PTRS_PER_PMD);
  157. }
  158. return 0;
  159. }
  160. static inline void *kvm_get_hwpgd(struct kvm *kvm)
  161. {
  162. pgd_t *pgd = kvm->arch.pgd;
  163. pud_t *pud;
  164. if (KVM_PREALLOC_LEVEL == 0)
  165. return pgd;
  166. pud = pud_offset(pgd, 0);
  167. if (KVM_PREALLOC_LEVEL == 1)
  168. return pud;
  169. BUG_ON(KVM_PREALLOC_LEVEL != 2);
  170. return pmd_offset(pud, 0);
  171. }
  172. static inline void kvm_free_hwpgd(struct kvm *kvm)
  173. {
  174. if (KVM_PREALLOC_LEVEL > 0) {
  175. unsigned long hwpgd = (unsigned long)kvm_get_hwpgd(kvm);
  176. free_pages(hwpgd, PTRS_PER_S2_PGD_SHIFT);
  177. }
  178. }
  179. static inline bool kvm_page_empty(void *ptr)
  180. {
  181. struct page *ptr_page = virt_to_page(ptr);
  182. return page_count(ptr_page) == 1;
  183. }
  184. #define kvm_pte_table_empty(kvm, ptep) kvm_page_empty(ptep)
  185. #ifdef __PAGETABLE_PMD_FOLDED
  186. #define kvm_pmd_table_empty(kvm, pmdp) (0)
  187. #else
  188. #define kvm_pmd_table_empty(kvm, pmdp) \
  189. (kvm_page_empty(pmdp) && (!(kvm) || KVM_PREALLOC_LEVEL < 2))
  190. #endif
  191. #ifdef __PAGETABLE_PUD_FOLDED
  192. #define kvm_pud_table_empty(kvm, pudp) (0)
  193. #else
  194. #define kvm_pud_table_empty(kvm, pudp) \
  195. (kvm_page_empty(pudp) && (!(kvm) || KVM_PREALLOC_LEVEL < 1))
  196. #endif
  197. struct kvm;
  198. #define kvm_flush_dcache_to_poc(a,l) __flush_dcache_area((a), (l))
  199. static inline bool vcpu_has_cache_enabled(struct kvm_vcpu *vcpu)
  200. {
  201. return (vcpu_sys_reg(vcpu, SCTLR_EL1) & 0b101) == 0b101;
  202. }
  203. static inline void coherent_cache_guest_page(struct kvm_vcpu *vcpu, hva_t hva,
  204. unsigned long size)
  205. {
  206. if (!vcpu_has_cache_enabled(vcpu))
  207. kvm_flush_dcache_to_poc((void *)hva, size);
  208. if (!icache_is_aliasing()) { /* PIPT */
  209. flush_icache_range(hva, hva + size);
  210. } else if (!icache_is_aivivt()) { /* non ASID-tagged VIVT */
  211. /* any kind of VIPT cache */
  212. __flush_icache_all();
  213. }
  214. }
  215. #define kvm_virt_to_phys(x) __virt_to_phys((unsigned long)(x))
  216. void stage2_flush_vm(struct kvm *kvm);
  217. #endif /* __ASSEMBLY__ */
  218. #endif /* __ARM64_KVM_MMU_H__ */