kvm_mmu.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. * We currently only support a 40bit IPA.
  123. */
  124. #define KVM_PHYS_SHIFT (40)
  125. #define KVM_PHYS_SIZE (1UL << KVM_PHYS_SHIFT)
  126. #define KVM_PHYS_MASK (KVM_PHYS_SIZE - 1UL)
  127. #include <asm/stage2_pgtable.h>
  128. int create_hyp_mappings(void *from, void *to, pgprot_t prot);
  129. int create_hyp_io_mappings(void *from, void *to, phys_addr_t);
  130. void free_hyp_pgds(void);
  131. void stage2_unmap_vm(struct kvm *kvm);
  132. int kvm_alloc_stage2_pgd(struct kvm *kvm);
  133. void kvm_free_stage2_pgd(struct kvm *kvm);
  134. int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
  135. phys_addr_t pa, unsigned long size, bool writable);
  136. int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run);
  137. void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu);
  138. phys_addr_t kvm_mmu_get_httbr(void);
  139. phys_addr_t kvm_get_idmap_vector(void);
  140. phys_addr_t kvm_get_idmap_start(void);
  141. int kvm_mmu_init(void);
  142. void kvm_clear_hyp_idmap(void);
  143. #define kvm_set_pte(ptep, pte) set_pte(ptep, pte)
  144. #define kvm_set_pmd(pmdp, pmd) set_pmd(pmdp, pmd)
  145. static inline pte_t kvm_s2pte_mkwrite(pte_t pte)
  146. {
  147. pte_val(pte) |= PTE_S2_RDWR;
  148. return pte;
  149. }
  150. static inline pmd_t kvm_s2pmd_mkwrite(pmd_t pmd)
  151. {
  152. pmd_val(pmd) |= PMD_S2_RDWR;
  153. return pmd;
  154. }
  155. static inline void kvm_set_s2pte_readonly(pte_t *pte)
  156. {
  157. pteval_t pteval;
  158. unsigned long tmp;
  159. asm volatile("// kvm_set_s2pte_readonly\n"
  160. " prfm pstl1strm, %2\n"
  161. "1: ldxr %0, %2\n"
  162. " and %0, %0, %3 // clear PTE_S2_RDWR\n"
  163. " orr %0, %0, %4 // set PTE_S2_RDONLY\n"
  164. " stxr %w1, %0, %2\n"
  165. " cbnz %w1, 1b\n"
  166. : "=&r" (pteval), "=&r" (tmp), "+Q" (pte_val(*pte))
  167. : "L" (~PTE_S2_RDWR), "L" (PTE_S2_RDONLY));
  168. }
  169. static inline bool kvm_s2pte_readonly(pte_t *pte)
  170. {
  171. return (pte_val(*pte) & PTE_S2_RDWR) == PTE_S2_RDONLY;
  172. }
  173. static inline void kvm_set_s2pmd_readonly(pmd_t *pmd)
  174. {
  175. kvm_set_s2pte_readonly((pte_t *)pmd);
  176. }
  177. static inline bool kvm_s2pmd_readonly(pmd_t *pmd)
  178. {
  179. return kvm_s2pte_readonly((pte_t *)pmd);
  180. }
  181. static inline bool kvm_page_empty(void *ptr)
  182. {
  183. struct page *ptr_page = virt_to_page(ptr);
  184. return page_count(ptr_page) == 1;
  185. }
  186. #define hyp_pte_table_empty(ptep) kvm_page_empty(ptep)
  187. #ifdef __PAGETABLE_PMD_FOLDED
  188. #define hyp_pmd_table_empty(pmdp) (0)
  189. #else
  190. #define hyp_pmd_table_empty(pmdp) kvm_page_empty(pmdp)
  191. #endif
  192. #ifdef __PAGETABLE_PUD_FOLDED
  193. #define hyp_pud_table_empty(pudp) (0)
  194. #else
  195. #define hyp_pud_table_empty(pudp) kvm_page_empty(pudp)
  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,
  204. kvm_pfn_t pfn,
  205. unsigned long size,
  206. bool ipa_uncached)
  207. {
  208. void *va = page_address(pfn_to_page(pfn));
  209. if (!vcpu_has_cache_enabled(vcpu) || ipa_uncached)
  210. kvm_flush_dcache_to_poc(va, size);
  211. if (!icache_is_aliasing()) { /* PIPT */
  212. flush_icache_range((unsigned long)va,
  213. (unsigned long)va + size);
  214. } else if (!icache_is_aivivt()) { /* non ASID-tagged VIVT */
  215. /* any kind of VIPT cache */
  216. __flush_icache_all();
  217. }
  218. }
  219. static inline void __kvm_flush_dcache_pte(pte_t pte)
  220. {
  221. struct page *page = pte_page(pte);
  222. kvm_flush_dcache_to_poc(page_address(page), PAGE_SIZE);
  223. }
  224. static inline void __kvm_flush_dcache_pmd(pmd_t pmd)
  225. {
  226. struct page *page = pmd_page(pmd);
  227. kvm_flush_dcache_to_poc(page_address(page), PMD_SIZE);
  228. }
  229. static inline void __kvm_flush_dcache_pud(pud_t pud)
  230. {
  231. struct page *page = pud_page(pud);
  232. kvm_flush_dcache_to_poc(page_address(page), PUD_SIZE);
  233. }
  234. #define kvm_virt_to_phys(x) __virt_to_phys((unsigned long)(x))
  235. void kvm_set_way_flush(struct kvm_vcpu *vcpu);
  236. void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled);
  237. static inline bool __kvm_cpu_uses_extended_idmap(void)
  238. {
  239. return __cpu_uses_extended_idmap();
  240. }
  241. static inline void __kvm_extend_hypmap(pgd_t *boot_hyp_pgd,
  242. pgd_t *hyp_pgd,
  243. pgd_t *merged_hyp_pgd,
  244. unsigned long hyp_idmap_start)
  245. {
  246. int idmap_idx;
  247. /*
  248. * Use the first entry to access the HYP mappings. It is
  249. * guaranteed to be free, otherwise we wouldn't use an
  250. * extended idmap.
  251. */
  252. VM_BUG_ON(pgd_val(merged_hyp_pgd[0]));
  253. merged_hyp_pgd[0] = __pgd(__pa(hyp_pgd) | PMD_TYPE_TABLE);
  254. /*
  255. * Create another extended level entry that points to the boot HYP map,
  256. * which contains an ID mapping of the HYP init code. We essentially
  257. * merge the boot and runtime HYP maps by doing so, but they don't
  258. * overlap anyway, so this is fine.
  259. */
  260. idmap_idx = hyp_idmap_start >> VA_BITS;
  261. VM_BUG_ON(pgd_val(merged_hyp_pgd[idmap_idx]));
  262. merged_hyp_pgd[idmap_idx] = __pgd(__pa(boot_hyp_pgd) | PMD_TYPE_TABLE);
  263. }
  264. static inline unsigned int kvm_get_vmid_bits(void)
  265. {
  266. int reg = read_system_reg(SYS_ID_AA64MMFR1_EL1);
  267. return (cpuid_feature_extract_unsigned_field(reg, ID_AA64MMFR1_VMIDBITS_SHIFT) == 2) ? 16 : 8;
  268. }
  269. #endif /* __ASSEMBLY__ */
  270. #endif /* __ARM64_KVM_MMU_H__ */