kvm_mmu.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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/pgalloc.h>
  41. #include <asm/stage2_pgtable.h>
  42. int create_hyp_mappings(void *from, void *to, pgprot_t prot);
  43. int create_hyp_io_mappings(void *from, void *to, phys_addr_t);
  44. void free_hyp_pgds(void);
  45. void stage2_unmap_vm(struct kvm *kvm);
  46. int kvm_alloc_stage2_pgd(struct kvm *kvm);
  47. void kvm_free_stage2_pgd(struct kvm *kvm);
  48. int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
  49. phys_addr_t pa, unsigned long size, bool writable);
  50. int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run);
  51. void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu);
  52. phys_addr_t kvm_mmu_get_httbr(void);
  53. phys_addr_t kvm_get_idmap_vector(void);
  54. phys_addr_t kvm_get_idmap_start(void);
  55. int kvm_mmu_init(void);
  56. void kvm_clear_hyp_idmap(void);
  57. static inline void kvm_set_pmd(pmd_t *pmd, pmd_t new_pmd)
  58. {
  59. *pmd = new_pmd;
  60. dsb(ishst);
  61. }
  62. static inline void kvm_set_pte(pte_t *pte, pte_t new_pte)
  63. {
  64. *pte = new_pte;
  65. dsb(ishst);
  66. }
  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 void kvm_set_s2pte_readonly(pte_t *pte)
  78. {
  79. pte_val(*pte) = (pte_val(*pte) & ~L_PTE_S2_RDWR) | L_PTE_S2_RDONLY;
  80. }
  81. static inline bool kvm_s2pte_readonly(pte_t *pte)
  82. {
  83. return (pte_val(*pte) & L_PTE_S2_RDWR) == L_PTE_S2_RDONLY;
  84. }
  85. static inline void kvm_set_s2pmd_readonly(pmd_t *pmd)
  86. {
  87. pmd_val(*pmd) = (pmd_val(*pmd) & ~L_PMD_S2_RDWR) | L_PMD_S2_RDONLY;
  88. }
  89. static inline bool kvm_s2pmd_readonly(pmd_t *pmd)
  90. {
  91. return (pmd_val(*pmd) & L_PMD_S2_RDWR) == L_PMD_S2_RDONLY;
  92. }
  93. static inline bool kvm_page_empty(void *ptr)
  94. {
  95. struct page *ptr_page = virt_to_page(ptr);
  96. return page_count(ptr_page) == 1;
  97. }
  98. #define kvm_pte_table_empty(kvm, ptep) kvm_page_empty(ptep)
  99. #define kvm_pmd_table_empty(kvm, pmdp) kvm_page_empty(pmdp)
  100. #define kvm_pud_table_empty(kvm, pudp) false
  101. #define hyp_pte_table_empty(ptep) kvm_page_empty(ptep)
  102. #define hyp_pmd_table_empty(pmdp) kvm_page_empty(pmdp)
  103. #define hyp_pud_table_empty(pudp) false
  104. struct kvm;
  105. #define kvm_flush_dcache_to_poc(a,l) __cpuc_flush_dcache_area((a), (l))
  106. static inline bool vcpu_has_cache_enabled(struct kvm_vcpu *vcpu)
  107. {
  108. return (vcpu_cp15(vcpu, c1_SCTLR) & 0b101) == 0b101;
  109. }
  110. static inline void __coherent_cache_guest_page(struct kvm_vcpu *vcpu,
  111. kvm_pfn_t pfn,
  112. unsigned long size,
  113. bool ipa_uncached)
  114. {
  115. /*
  116. * If we are going to insert an instruction page and the icache is
  117. * either VIPT or PIPT, there is a potential problem where the host
  118. * (or another VM) may have used the same page as this guest, and we
  119. * read incorrect data from the icache. If we're using a PIPT cache,
  120. * we can invalidate just that page, but if we are using a VIPT cache
  121. * we need to invalidate the entire icache - damn shame - as written
  122. * in the ARM ARM (DDI 0406C.b - Page B3-1393).
  123. *
  124. * VIVT caches are tagged using both the ASID and the VMID and doesn't
  125. * need any kind of flushing (DDI 0406C.b - Page B3-1392).
  126. *
  127. * We need to do this through a kernel mapping (using the
  128. * user-space mapping has proved to be the wrong
  129. * solution). For that, we need to kmap one page at a time,
  130. * and iterate over the range.
  131. */
  132. VM_BUG_ON(size & ~PAGE_MASK);
  133. while (size) {
  134. void *va = kmap_atomic_pfn(pfn);
  135. kvm_flush_dcache_to_poc(va, PAGE_SIZE);
  136. if (icache_is_pipt())
  137. __cpuc_coherent_user_range((unsigned long)va,
  138. (unsigned long)va + PAGE_SIZE);
  139. size -= PAGE_SIZE;
  140. pfn++;
  141. kunmap_atomic(va);
  142. }
  143. if (!icache_is_pipt() && !icache_is_vivt_asid_tagged()) {
  144. /* any kind of VIPT cache */
  145. __flush_icache_all();
  146. }
  147. }
  148. static inline void __kvm_flush_dcache_pte(pte_t pte)
  149. {
  150. void *va = kmap_atomic(pte_page(pte));
  151. kvm_flush_dcache_to_poc(va, PAGE_SIZE);
  152. kunmap_atomic(va);
  153. }
  154. static inline void __kvm_flush_dcache_pmd(pmd_t pmd)
  155. {
  156. unsigned long size = PMD_SIZE;
  157. kvm_pfn_t pfn = pmd_pfn(pmd);
  158. while (size) {
  159. void *va = kmap_atomic_pfn(pfn);
  160. kvm_flush_dcache_to_poc(va, PAGE_SIZE);
  161. pfn++;
  162. size -= PAGE_SIZE;
  163. kunmap_atomic(va);
  164. }
  165. }
  166. static inline void __kvm_flush_dcache_pud(pud_t pud)
  167. {
  168. }
  169. #define kvm_virt_to_phys(x) virt_to_idmap((unsigned long)(x))
  170. void kvm_set_way_flush(struct kvm_vcpu *vcpu);
  171. void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled);
  172. static inline bool __kvm_cpu_uses_extended_idmap(void)
  173. {
  174. return false;
  175. }
  176. static inline void __kvm_extend_hypmap(pgd_t *boot_hyp_pgd,
  177. pgd_t *hyp_pgd,
  178. pgd_t *merged_hyp_pgd,
  179. unsigned long hyp_idmap_start) { }
  180. static inline unsigned int kvm_get_vmid_bits(void)
  181. {
  182. return 8;
  183. }
  184. /*
  185. * We are not in the kvm->srcu critical section most of the time, so we take
  186. * the SRCU read lock here. Since we copy the data from the user page, we
  187. * can immediately drop the lock again.
  188. */
  189. static inline int kvm_read_guest_lock(struct kvm *kvm,
  190. gpa_t gpa, void *data, unsigned long len)
  191. {
  192. int srcu_idx = srcu_read_lock(&kvm->srcu);
  193. int ret = kvm_read_guest(kvm, gpa, data, len);
  194. srcu_read_unlock(&kvm->srcu, srcu_idx);
  195. return ret;
  196. }
  197. static inline void *kvm_get_hyp_vector(void)
  198. {
  199. return kvm_ksym_ref(__kvm_hyp_vector);
  200. }
  201. static inline int kvm_map_vectors(void)
  202. {
  203. return 0;
  204. }
  205. static inline int hyp_map_aux_data(void)
  206. {
  207. return 0;
  208. }
  209. #endif /* !__ASSEMBLY__ */
  210. #endif /* __ARM_KVM_MMU_H__ */