kvm_mmu.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. void stage2_unmap_vm(struct kvm *kvm);
  74. int kvm_alloc_stage2_pgd(struct kvm *kvm);
  75. void kvm_free_stage2_pgd(struct kvm *kvm);
  76. int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
  77. phys_addr_t pa, unsigned long size, bool writable);
  78. int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run);
  79. void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu);
  80. phys_addr_t kvm_mmu_get_httbr(void);
  81. phys_addr_t kvm_mmu_get_boot_httbr(void);
  82. phys_addr_t kvm_get_idmap_vector(void);
  83. int kvm_mmu_init(void);
  84. void kvm_clear_hyp_idmap(void);
  85. #define kvm_set_pte(ptep, pte) set_pte(ptep, pte)
  86. #define kvm_set_pmd(pmdp, pmd) set_pmd(pmdp, pmd)
  87. static inline void kvm_clean_pgd(pgd_t *pgd) {}
  88. static inline void kvm_clean_pmd(pmd_t *pmd) {}
  89. static inline void kvm_clean_pmd_entry(pmd_t *pmd) {}
  90. static inline void kvm_clean_pte(pte_t *pte) {}
  91. static inline void kvm_clean_pte_entry(pte_t *pte) {}
  92. static inline void kvm_set_s2pte_writable(pte_t *pte)
  93. {
  94. pte_val(*pte) |= PTE_S2_RDWR;
  95. }
  96. static inline void kvm_set_s2pmd_writable(pmd_t *pmd)
  97. {
  98. pmd_val(*pmd) |= PMD_S2_RDWR;
  99. }
  100. #define kvm_pgd_addr_end(addr, end) pgd_addr_end(addr, end)
  101. #define kvm_pud_addr_end(addr, end) pud_addr_end(addr, end)
  102. #define kvm_pmd_addr_end(addr, end) pmd_addr_end(addr, end)
  103. /*
  104. * In the case where PGDIR_SHIFT is larger than KVM_PHYS_SHIFT, we can address
  105. * the entire IPA input range with a single pgd entry, and we would only need
  106. * one pgd entry. Note that in this case, the pgd is actually not used by
  107. * the MMU for Stage-2 translations, but is merely a fake pgd used as a data
  108. * structure for the kernel pgtable macros to work.
  109. */
  110. #if PGDIR_SHIFT > KVM_PHYS_SHIFT
  111. #define PTRS_PER_S2_PGD_SHIFT 0
  112. #else
  113. #define PTRS_PER_S2_PGD_SHIFT (KVM_PHYS_SHIFT - PGDIR_SHIFT)
  114. #endif
  115. #define PTRS_PER_S2_PGD (1 << PTRS_PER_S2_PGD_SHIFT)
  116. #define S2_PGD_ORDER get_order(PTRS_PER_S2_PGD * sizeof(pgd_t))
  117. #define kvm_pgd_index(addr) (((addr) >> PGDIR_SHIFT) & (PTRS_PER_S2_PGD - 1))
  118. /*
  119. * If we are concatenating first level stage-2 page tables, we would have less
  120. * than or equal to 16 pointers in the fake PGD, because that's what the
  121. * architecture allows. In this case, (4 - CONFIG_ARM64_PGTABLE_LEVELS)
  122. * represents the first level for the host, and we add 1 to go to the next
  123. * level (which uses contatenation) for the stage-2 tables.
  124. */
  125. #if PTRS_PER_S2_PGD <= 16
  126. #define KVM_PREALLOC_LEVEL (4 - CONFIG_ARM64_PGTABLE_LEVELS + 1)
  127. #else
  128. #define KVM_PREALLOC_LEVEL (0)
  129. #endif
  130. static inline void *kvm_get_hwpgd(struct kvm *kvm)
  131. {
  132. pgd_t *pgd = kvm->arch.pgd;
  133. pud_t *pud;
  134. if (KVM_PREALLOC_LEVEL == 0)
  135. return pgd;
  136. pud = pud_offset(pgd, 0);
  137. if (KVM_PREALLOC_LEVEL == 1)
  138. return pud;
  139. BUG_ON(KVM_PREALLOC_LEVEL != 2);
  140. return pmd_offset(pud, 0);
  141. }
  142. static inline unsigned int kvm_get_hwpgd_size(void)
  143. {
  144. if (KVM_PREALLOC_LEVEL > 0)
  145. return PTRS_PER_S2_PGD * PAGE_SIZE;
  146. return PTRS_PER_S2_PGD * sizeof(pgd_t);
  147. }
  148. static inline bool kvm_page_empty(void *ptr)
  149. {
  150. struct page *ptr_page = virt_to_page(ptr);
  151. return page_count(ptr_page) == 1;
  152. }
  153. #define kvm_pte_table_empty(kvm, ptep) kvm_page_empty(ptep)
  154. #ifdef __PAGETABLE_PMD_FOLDED
  155. #define kvm_pmd_table_empty(kvm, pmdp) (0)
  156. #else
  157. #define kvm_pmd_table_empty(kvm, pmdp) \
  158. (kvm_page_empty(pmdp) && (!(kvm) || KVM_PREALLOC_LEVEL < 2))
  159. #endif
  160. #ifdef __PAGETABLE_PUD_FOLDED
  161. #define kvm_pud_table_empty(kvm, pudp) (0)
  162. #else
  163. #define kvm_pud_table_empty(kvm, pudp) \
  164. (kvm_page_empty(pudp) && (!(kvm) || KVM_PREALLOC_LEVEL < 1))
  165. #endif
  166. struct kvm;
  167. #define kvm_flush_dcache_to_poc(a,l) __flush_dcache_area((a), (l))
  168. static inline bool vcpu_has_cache_enabled(struct kvm_vcpu *vcpu)
  169. {
  170. return (vcpu_sys_reg(vcpu, SCTLR_EL1) & 0b101) == 0b101;
  171. }
  172. static inline void __coherent_cache_guest_page(struct kvm_vcpu *vcpu, pfn_t pfn,
  173. unsigned long size,
  174. bool ipa_uncached)
  175. {
  176. void *va = page_address(pfn_to_page(pfn));
  177. if (!vcpu_has_cache_enabled(vcpu) || ipa_uncached)
  178. kvm_flush_dcache_to_poc(va, size);
  179. if (!icache_is_aliasing()) { /* PIPT */
  180. flush_icache_range((unsigned long)va,
  181. (unsigned long)va + size);
  182. } else if (!icache_is_aivivt()) { /* non ASID-tagged VIVT */
  183. /* any kind of VIPT cache */
  184. __flush_icache_all();
  185. }
  186. }
  187. static inline void __kvm_flush_dcache_pte(pte_t pte)
  188. {
  189. struct page *page = pte_page(pte);
  190. kvm_flush_dcache_to_poc(page_address(page), PAGE_SIZE);
  191. }
  192. static inline void __kvm_flush_dcache_pmd(pmd_t pmd)
  193. {
  194. struct page *page = pmd_page(pmd);
  195. kvm_flush_dcache_to_poc(page_address(page), PMD_SIZE);
  196. }
  197. static inline void __kvm_flush_dcache_pud(pud_t pud)
  198. {
  199. struct page *page = pud_page(pud);
  200. kvm_flush_dcache_to_poc(page_address(page), PUD_SIZE);
  201. }
  202. #define kvm_virt_to_phys(x) __virt_to_phys((unsigned long)(x))
  203. void stage2_flush_vm(struct kvm *kvm);
  204. #endif /* __ASSEMBLY__ */
  205. #endif /* __ARM64_KVM_MMU_H__ */