kvm_mmu.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. #define kvm_mk_pmd(ptep) __pmd(__pa(ptep) | PMD_TYPE_TABLE)
  65. #define kvm_mk_pud(pmdp) __pud(__pa(pmdp) | PMD_TYPE_TABLE)
  66. #define kvm_mk_pgd(pudp) ({ BUILD_BUG(); 0; })
  67. static inline pte_t kvm_s2pte_mkwrite(pte_t pte)
  68. {
  69. pte_val(pte) |= L_PTE_S2_RDWR;
  70. return pte;
  71. }
  72. static inline pmd_t kvm_s2pmd_mkwrite(pmd_t pmd)
  73. {
  74. pmd_val(pmd) |= L_PMD_S2_RDWR;
  75. return pmd;
  76. }
  77. static inline pte_t kvm_s2pte_mkexec(pte_t pte)
  78. {
  79. pte_val(pte) &= ~L_PTE_XN;
  80. return pte;
  81. }
  82. static inline pmd_t kvm_s2pmd_mkexec(pmd_t pmd)
  83. {
  84. pmd_val(pmd) &= ~PMD_SECT_XN;
  85. return pmd;
  86. }
  87. static inline void kvm_set_s2pte_readonly(pte_t *pte)
  88. {
  89. pte_val(*pte) = (pte_val(*pte) & ~L_PTE_S2_RDWR) | L_PTE_S2_RDONLY;
  90. }
  91. static inline bool kvm_s2pte_readonly(pte_t *pte)
  92. {
  93. return (pte_val(*pte) & L_PTE_S2_RDWR) == L_PTE_S2_RDONLY;
  94. }
  95. static inline bool kvm_s2pte_exec(pte_t *pte)
  96. {
  97. return !(pte_val(*pte) & L_PTE_XN);
  98. }
  99. static inline void kvm_set_s2pmd_readonly(pmd_t *pmd)
  100. {
  101. pmd_val(*pmd) = (pmd_val(*pmd) & ~L_PMD_S2_RDWR) | L_PMD_S2_RDONLY;
  102. }
  103. static inline bool kvm_s2pmd_readonly(pmd_t *pmd)
  104. {
  105. return (pmd_val(*pmd) & L_PMD_S2_RDWR) == L_PMD_S2_RDONLY;
  106. }
  107. static inline bool kvm_s2pmd_exec(pmd_t *pmd)
  108. {
  109. return !(pmd_val(*pmd) & PMD_SECT_XN);
  110. }
  111. static inline bool kvm_page_empty(void *ptr)
  112. {
  113. struct page *ptr_page = virt_to_page(ptr);
  114. return page_count(ptr_page) == 1;
  115. }
  116. #define kvm_pte_table_empty(kvm, ptep) kvm_page_empty(ptep)
  117. #define kvm_pmd_table_empty(kvm, pmdp) kvm_page_empty(pmdp)
  118. #define kvm_pud_table_empty(kvm, pudp) false
  119. #define hyp_pte_table_empty(ptep) kvm_page_empty(ptep)
  120. #define hyp_pmd_table_empty(pmdp) kvm_page_empty(pmdp)
  121. #define hyp_pud_table_empty(pudp) false
  122. struct kvm;
  123. #define kvm_flush_dcache_to_poc(a,l) __cpuc_flush_dcache_area((a), (l))
  124. static inline bool vcpu_has_cache_enabled(struct kvm_vcpu *vcpu)
  125. {
  126. return (vcpu_cp15(vcpu, c1_SCTLR) & 0b101) == 0b101;
  127. }
  128. static inline void __clean_dcache_guest_page(kvm_pfn_t pfn, unsigned long size)
  129. {
  130. /*
  131. * Clean the dcache to the Point of Coherency.
  132. *
  133. * We need to do this through a kernel mapping (using the
  134. * user-space mapping has proved to be the wrong
  135. * solution). For that, we need to kmap one page at a time,
  136. * and iterate over the range.
  137. */
  138. VM_BUG_ON(size & ~PAGE_MASK);
  139. while (size) {
  140. void *va = kmap_atomic_pfn(pfn);
  141. kvm_flush_dcache_to_poc(va, PAGE_SIZE);
  142. size -= PAGE_SIZE;
  143. pfn++;
  144. kunmap_atomic(va);
  145. }
  146. }
  147. static inline void __invalidate_icache_guest_page(kvm_pfn_t pfn,
  148. unsigned long size)
  149. {
  150. u32 iclsz;
  151. /*
  152. * If we are going to insert an instruction page and the icache is
  153. * either VIPT or PIPT, there is a potential problem where the host
  154. * (or another VM) may have used the same page as this guest, and we
  155. * read incorrect data from the icache. If we're using a PIPT cache,
  156. * we can invalidate just that page, but if we are using a VIPT cache
  157. * we need to invalidate the entire icache - damn shame - as written
  158. * in the ARM ARM (DDI 0406C.b - Page B3-1393).
  159. *
  160. * VIVT caches are tagged using both the ASID and the VMID and doesn't
  161. * need any kind of flushing (DDI 0406C.b - Page B3-1392).
  162. */
  163. VM_BUG_ON(size & ~PAGE_MASK);
  164. if (icache_is_vivt_asid_tagged())
  165. return;
  166. if (!icache_is_pipt()) {
  167. /* any kind of VIPT cache */
  168. __flush_icache_all();
  169. return;
  170. }
  171. /*
  172. * CTR IminLine contains Log2 of the number of words in the
  173. * cache line, so we can get the number of words as
  174. * 2 << (IminLine - 1). To get the number of bytes, we
  175. * multiply by 4 (the number of bytes in a 32-bit word), and
  176. * get 4 << (IminLine).
  177. */
  178. iclsz = 4 << (read_cpuid(CPUID_CACHETYPE) & 0xf);
  179. while (size) {
  180. void *va = kmap_atomic_pfn(pfn);
  181. void *end = va + PAGE_SIZE;
  182. void *addr = va;
  183. do {
  184. write_sysreg(addr, ICIMVAU);
  185. addr += iclsz;
  186. } while (addr < end);
  187. dsb(ishst);
  188. isb();
  189. size -= PAGE_SIZE;
  190. pfn++;
  191. kunmap_atomic(va);
  192. }
  193. /* Check if we need to invalidate the BTB */
  194. if ((read_cpuid_ext(CPUID_EXT_MMFR1) >> 28) != 4) {
  195. write_sysreg(0, BPIALLIS);
  196. dsb(ishst);
  197. isb();
  198. }
  199. }
  200. static inline void __kvm_flush_dcache_pte(pte_t pte)
  201. {
  202. void *va = kmap_atomic(pte_page(pte));
  203. kvm_flush_dcache_to_poc(va, PAGE_SIZE);
  204. kunmap_atomic(va);
  205. }
  206. static inline void __kvm_flush_dcache_pmd(pmd_t pmd)
  207. {
  208. unsigned long size = PMD_SIZE;
  209. kvm_pfn_t pfn = pmd_pfn(pmd);
  210. while (size) {
  211. void *va = kmap_atomic_pfn(pfn);
  212. kvm_flush_dcache_to_poc(va, PAGE_SIZE);
  213. pfn++;
  214. size -= PAGE_SIZE;
  215. kunmap_atomic(va);
  216. }
  217. }
  218. static inline void __kvm_flush_dcache_pud(pud_t pud)
  219. {
  220. }
  221. #define kvm_virt_to_phys(x) virt_to_idmap((unsigned long)(x))
  222. void kvm_set_way_flush(struct kvm_vcpu *vcpu);
  223. void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled);
  224. static inline bool __kvm_cpu_uses_extended_idmap(void)
  225. {
  226. return false;
  227. }
  228. static inline unsigned long __kvm_idmap_ptrs_per_pgd(void)
  229. {
  230. return PTRS_PER_PGD;
  231. }
  232. static inline void __kvm_extend_hypmap(pgd_t *boot_hyp_pgd,
  233. pgd_t *hyp_pgd,
  234. pgd_t *merged_hyp_pgd,
  235. unsigned long hyp_idmap_start) { }
  236. static inline unsigned int kvm_get_vmid_bits(void)
  237. {
  238. return 8;
  239. }
  240. /*
  241. * We are not in the kvm->srcu critical section most of the time, so we take
  242. * the SRCU read lock here. Since we copy the data from the user page, we
  243. * can immediately drop the lock again.
  244. */
  245. static inline int kvm_read_guest_lock(struct kvm *kvm,
  246. gpa_t gpa, void *data, unsigned long len)
  247. {
  248. int srcu_idx = srcu_read_lock(&kvm->srcu);
  249. int ret = kvm_read_guest(kvm, gpa, data, len);
  250. srcu_read_unlock(&kvm->srcu, srcu_idx);
  251. return ret;
  252. }
  253. static inline void *kvm_get_hyp_vector(void)
  254. {
  255. switch(read_cpuid_part()) {
  256. #ifdef CONFIG_HARDEN_BRANCH_PREDICTOR
  257. case ARM_CPU_PART_CORTEX_A12:
  258. case ARM_CPU_PART_CORTEX_A17:
  259. {
  260. extern char __kvm_hyp_vector_bp_inv[];
  261. return kvm_ksym_ref(__kvm_hyp_vector_bp_inv);
  262. }
  263. case ARM_CPU_PART_BRAHMA_B15:
  264. case ARM_CPU_PART_CORTEX_A15:
  265. {
  266. extern char __kvm_hyp_vector_ic_inv[];
  267. return kvm_ksym_ref(__kvm_hyp_vector_ic_inv);
  268. }
  269. #endif
  270. default:
  271. {
  272. extern char __kvm_hyp_vector[];
  273. return kvm_ksym_ref(__kvm_hyp_vector);
  274. }
  275. }
  276. }
  277. static inline int kvm_map_vectors(void)
  278. {
  279. return 0;
  280. }
  281. static inline int hyp_map_aux_data(void)
  282. {
  283. return 0;
  284. }
  285. #define kvm_phys_to_vttbr(addr) (addr)
  286. #endif /* !__ASSEMBLY__ */
  287. #endif /* __ARM_KVM_MMU_H__ */