kvm_mmu.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * Copyright (C) 2012 - Virtual Open Systems and Columbia University
  3. * Author: Christoffer Dall <c.dall@virtualopensystems.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, write to the Free Software
  16. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. */
  18. #ifndef __ARM_KVM_MMU_H__
  19. #define __ARM_KVM_MMU_H__
  20. #include <asm/memory.h>
  21. #include <asm/page.h>
  22. /*
  23. * We directly use the kernel VA for the HYP, as we can directly share
  24. * the mapping (HTTBR "covers" TTBR1).
  25. */
  26. #define kern_hyp_va(kva) (kva)
  27. /* Contrary to arm64, there is no need to generate a PC-relative address */
  28. #define hyp_symbol_addr(s) \
  29. ({ \
  30. typeof(s) *addr = &(s); \
  31. addr; \
  32. })
  33. /*
  34. * KVM_MMU_CACHE_MIN_PAGES is the number of stage2 page table translation levels.
  35. */
  36. #define KVM_MMU_CACHE_MIN_PAGES 2
  37. #ifndef __ASSEMBLY__
  38. #include <linux/highmem.h>
  39. #include <asm/cacheflush.h>
  40. #include <asm/cputype.h>
  41. #include <asm/kvm_hyp.h>
  42. #include <asm/pgalloc.h>
  43. #include <asm/stage2_pgtable.h>
  44. /* Ensure compatibility with arm64 */
  45. #define VA_BITS 32
  46. int create_hyp_mappings(void *from, void *to, pgprot_t prot);
  47. int create_hyp_io_mappings(phys_addr_t phys_addr, size_t size,
  48. void __iomem **kaddr,
  49. void __iomem **haddr);
  50. int create_hyp_exec_mappings(phys_addr_t phys_addr, size_t size,
  51. void **haddr);
  52. void free_hyp_pgds(void);
  53. void stage2_unmap_vm(struct kvm *kvm);
  54. int kvm_alloc_stage2_pgd(struct kvm *kvm);
  55. void kvm_free_stage2_pgd(struct kvm *kvm);
  56. int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
  57. phys_addr_t pa, unsigned long size, bool writable);
  58. int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run);
  59. void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu);
  60. phys_addr_t kvm_mmu_get_httbr(void);
  61. phys_addr_t kvm_get_idmap_vector(void);
  62. int kvm_mmu_init(void);
  63. void kvm_clear_hyp_idmap(void);
  64. static inline void kvm_set_pmd(pmd_t *pmd, pmd_t new_pmd)
  65. {
  66. *pmd = new_pmd;
  67. dsb(ishst);
  68. }
  69. static inline void kvm_set_pte(pte_t *pte, pte_t new_pte)
  70. {
  71. *pte = new_pte;
  72. dsb(ishst);
  73. }
  74. static inline pte_t kvm_s2pte_mkwrite(pte_t pte)
  75. {
  76. pte_val(pte) |= L_PTE_S2_RDWR;
  77. return pte;
  78. }
  79. static inline pmd_t kvm_s2pmd_mkwrite(pmd_t pmd)
  80. {
  81. pmd_val(pmd) |= L_PMD_S2_RDWR;
  82. return pmd;
  83. }
  84. static inline pte_t kvm_s2pte_mkexec(pte_t pte)
  85. {
  86. pte_val(pte) &= ~L_PTE_XN;
  87. return pte;
  88. }
  89. static inline pmd_t kvm_s2pmd_mkexec(pmd_t pmd)
  90. {
  91. pmd_val(pmd) &= ~PMD_SECT_XN;
  92. return pmd;
  93. }
  94. static inline void kvm_set_s2pte_readonly(pte_t *pte)
  95. {
  96. pte_val(*pte) = (pte_val(*pte) & ~L_PTE_S2_RDWR) | L_PTE_S2_RDONLY;
  97. }
  98. static inline bool kvm_s2pte_readonly(pte_t *pte)
  99. {
  100. return (pte_val(*pte) & L_PTE_S2_RDWR) == L_PTE_S2_RDONLY;
  101. }
  102. static inline bool kvm_s2pte_exec(pte_t *pte)
  103. {
  104. return !(pte_val(*pte) & L_PTE_XN);
  105. }
  106. static inline void kvm_set_s2pmd_readonly(pmd_t *pmd)
  107. {
  108. pmd_val(*pmd) = (pmd_val(*pmd) & ~L_PMD_S2_RDWR) | L_PMD_S2_RDONLY;
  109. }
  110. static inline bool kvm_s2pmd_readonly(pmd_t *pmd)
  111. {
  112. return (pmd_val(*pmd) & L_PMD_S2_RDWR) == L_PMD_S2_RDONLY;
  113. }
  114. static inline bool kvm_s2pmd_exec(pmd_t *pmd)
  115. {
  116. return !(pmd_val(*pmd) & PMD_SECT_XN);
  117. }
  118. static inline bool kvm_page_empty(void *ptr)
  119. {
  120. struct page *ptr_page = virt_to_page(ptr);
  121. return page_count(ptr_page) == 1;
  122. }
  123. #define kvm_pte_table_empty(kvm, ptep) kvm_page_empty(ptep)
  124. #define kvm_pmd_table_empty(kvm, pmdp) kvm_page_empty(pmdp)
  125. #define kvm_pud_table_empty(kvm, pudp) false
  126. #define hyp_pte_table_empty(ptep) kvm_page_empty(ptep)
  127. #define hyp_pmd_table_empty(pmdp) kvm_page_empty(pmdp)
  128. #define hyp_pud_table_empty(pudp) false
  129. struct kvm;
  130. #define kvm_flush_dcache_to_poc(a,l) __cpuc_flush_dcache_area((a), (l))
  131. static inline bool vcpu_has_cache_enabled(struct kvm_vcpu *vcpu)
  132. {
  133. return (vcpu_cp15(vcpu, c1_SCTLR) & 0b101) == 0b101;
  134. }
  135. static inline void __clean_dcache_guest_page(kvm_pfn_t pfn, unsigned long size)
  136. {
  137. /*
  138. * Clean the dcache to the Point of Coherency.
  139. *
  140. * We need to do this through a kernel mapping (using the
  141. * user-space mapping has proved to be the wrong
  142. * solution). For that, we need to kmap one page at a time,
  143. * and iterate over the range.
  144. */
  145. VM_BUG_ON(size & ~PAGE_MASK);
  146. while (size) {
  147. void *va = kmap_atomic_pfn(pfn);
  148. kvm_flush_dcache_to_poc(va, PAGE_SIZE);
  149. size -= PAGE_SIZE;
  150. pfn++;
  151. kunmap_atomic(va);
  152. }
  153. }
  154. static inline void __invalidate_icache_guest_page(kvm_pfn_t pfn,
  155. unsigned long size)
  156. {
  157. u32 iclsz;
  158. /*
  159. * If we are going to insert an instruction page and the icache is
  160. * either VIPT or PIPT, there is a potential problem where the host
  161. * (or another VM) may have used the same page as this guest, and we
  162. * read incorrect data from the icache. If we're using a PIPT cache,
  163. * we can invalidate just that page, but if we are using a VIPT cache
  164. * we need to invalidate the entire icache - damn shame - as written
  165. * in the ARM ARM (DDI 0406C.b - Page B3-1393).
  166. *
  167. * VIVT caches are tagged using both the ASID and the VMID and doesn't
  168. * need any kind of flushing (DDI 0406C.b - Page B3-1392).
  169. */
  170. VM_BUG_ON(size & ~PAGE_MASK);
  171. if (icache_is_vivt_asid_tagged())
  172. return;
  173. if (!icache_is_pipt()) {
  174. /* any kind of VIPT cache */
  175. __flush_icache_all();
  176. return;
  177. }
  178. /*
  179. * CTR IminLine contains Log2 of the number of words in the
  180. * cache line, so we can get the number of words as
  181. * 2 << (IminLine - 1). To get the number of bytes, we
  182. * multiply by 4 (the number of bytes in a 32-bit word), and
  183. * get 4 << (IminLine).
  184. */
  185. iclsz = 4 << (read_cpuid(CPUID_CACHETYPE) & 0xf);
  186. while (size) {
  187. void *va = kmap_atomic_pfn(pfn);
  188. void *end = va + PAGE_SIZE;
  189. void *addr = va;
  190. do {
  191. write_sysreg(addr, ICIMVAU);
  192. addr += iclsz;
  193. } while (addr < end);
  194. dsb(ishst);
  195. isb();
  196. size -= PAGE_SIZE;
  197. pfn++;
  198. kunmap_atomic(va);
  199. }
  200. /* Check if we need to invalidate the BTB */
  201. if ((read_cpuid_ext(CPUID_EXT_MMFR1) >> 28) != 4) {
  202. write_sysreg(0, BPIALLIS);
  203. dsb(ishst);
  204. isb();
  205. }
  206. }
  207. static inline void __kvm_flush_dcache_pte(pte_t pte)
  208. {
  209. void *va = kmap_atomic(pte_page(pte));
  210. kvm_flush_dcache_to_poc(va, PAGE_SIZE);
  211. kunmap_atomic(va);
  212. }
  213. static inline void __kvm_flush_dcache_pmd(pmd_t pmd)
  214. {
  215. unsigned long size = PMD_SIZE;
  216. kvm_pfn_t pfn = pmd_pfn(pmd);
  217. while (size) {
  218. void *va = kmap_atomic_pfn(pfn);
  219. kvm_flush_dcache_to_poc(va, PAGE_SIZE);
  220. pfn++;
  221. size -= PAGE_SIZE;
  222. kunmap_atomic(va);
  223. }
  224. }
  225. static inline void __kvm_flush_dcache_pud(pud_t pud)
  226. {
  227. }
  228. #define kvm_virt_to_phys(x) virt_to_idmap((unsigned long)(x))
  229. void kvm_set_way_flush(struct kvm_vcpu *vcpu);
  230. void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled);
  231. static inline bool __kvm_cpu_uses_extended_idmap(void)
  232. {
  233. return false;
  234. }
  235. static inline unsigned long __kvm_idmap_ptrs_per_pgd(void)
  236. {
  237. return PTRS_PER_PGD;
  238. }
  239. static inline void __kvm_extend_hypmap(pgd_t *boot_hyp_pgd,
  240. pgd_t *hyp_pgd,
  241. pgd_t *merged_hyp_pgd,
  242. unsigned long hyp_idmap_start) { }
  243. static inline unsigned int kvm_get_vmid_bits(void)
  244. {
  245. return 8;
  246. }
  247. /*
  248. * We are not in the kvm->srcu critical section most of the time, so we take
  249. * the SRCU read lock here. Since we copy the data from the user page, we
  250. * can immediately drop the lock again.
  251. */
  252. static inline int kvm_read_guest_lock(struct kvm *kvm,
  253. gpa_t gpa, void *data, unsigned long len)
  254. {
  255. int srcu_idx = srcu_read_lock(&kvm->srcu);
  256. int ret = kvm_read_guest(kvm, gpa, data, len);
  257. srcu_read_unlock(&kvm->srcu, srcu_idx);
  258. return ret;
  259. }
  260. static inline void *kvm_get_hyp_vector(void)
  261. {
  262. return kvm_ksym_ref(__kvm_hyp_vector);
  263. }
  264. static inline int kvm_map_vectors(void)
  265. {
  266. return 0;
  267. }
  268. #define kvm_phys_to_vttbr(addr) (addr)
  269. #endif /* !__ASSEMBLY__ */
  270. #endif /* __ARM_KVM_MMU_H__ */