fault-armv.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/arch/arm/mm/fault-armv.c
  4. *
  5. * Copyright (C) 1995 Linus Torvalds
  6. * Modifications for ARM processor (c) 1995-2002 Russell King
  7. */
  8. #include <linux/sched.h>
  9. #include <linux/kernel.h>
  10. #include <linux/mm.h>
  11. #include <linux/bitops.h>
  12. #include <linux/vmalloc.h>
  13. #include <linux/init.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/gfp.h>
  16. #include <asm/bugs.h>
  17. #include <asm/cacheflush.h>
  18. #include <asm/cachetype.h>
  19. #include <asm/tlbflush.h>
  20. #include "mm.h"
  21. static pteval_t shared_pte_mask = L_PTE_MT_BUFFERABLE;
  22. #if __LINUX_ARM_ARCH__ < 6
  23. /*
  24. * We take the easy way out of this problem - we make the
  25. * PTE uncacheable. However, we leave the write buffer on.
  26. *
  27. * Note that the pte lock held when calling update_mmu_cache must also
  28. * guard the pte (somewhere else in the same mm) that we modify here.
  29. * Therefore those configurations which might call adjust_pte (those
  30. * without CONFIG_CPU_CACHE_VIPT) cannot support split page_table_lock.
  31. */
  32. static int do_adjust_pte(struct vm_area_struct *vma, unsigned long address,
  33. unsigned long pfn, pte_t *ptep)
  34. {
  35. pte_t entry = *ptep;
  36. int ret;
  37. /*
  38. * If this page is present, it's actually being shared.
  39. */
  40. ret = pte_present(entry);
  41. /*
  42. * If this page isn't present, or is already setup to
  43. * fault (ie, is old), we can safely ignore any issues.
  44. */
  45. if (ret && (pte_val(entry) & L_PTE_MT_MASK) != shared_pte_mask) {
  46. flush_cache_page(vma, address, pfn);
  47. outer_flush_range((pfn << PAGE_SHIFT),
  48. (pfn << PAGE_SHIFT) + PAGE_SIZE);
  49. pte_val(entry) &= ~L_PTE_MT_MASK;
  50. pte_val(entry) |= shared_pte_mask;
  51. set_pte_at(vma->vm_mm, address, ptep, entry);
  52. flush_tlb_page(vma, address);
  53. }
  54. return ret;
  55. }
  56. #if defined(CONFIG_SPLIT_PTE_PTLOCKS)
  57. /*
  58. * If we are using split PTE locks, then we need to take the page
  59. * lock here. Otherwise we are using shared mm->page_table_lock
  60. * which is already locked, thus cannot take it.
  61. */
  62. static inline void do_pte_lock(spinlock_t *ptl)
  63. {
  64. /*
  65. * Use nested version here to indicate that we are already
  66. * holding one similar spinlock.
  67. */
  68. spin_lock_nested(ptl, SINGLE_DEPTH_NESTING);
  69. }
  70. static inline void do_pte_unlock(spinlock_t *ptl)
  71. {
  72. spin_unlock(ptl);
  73. }
  74. #else /* !defined(CONFIG_SPLIT_PTE_PTLOCKS) */
  75. static inline void do_pte_lock(spinlock_t *ptl) {}
  76. static inline void do_pte_unlock(spinlock_t *ptl) {}
  77. #endif /* defined(CONFIG_SPLIT_PTE_PTLOCKS) */
  78. static int adjust_pte(struct vm_area_struct *vma, unsigned long address,
  79. unsigned long pfn)
  80. {
  81. spinlock_t *ptl;
  82. pgd_t *pgd;
  83. p4d_t *p4d;
  84. pud_t *pud;
  85. pmd_t *pmd;
  86. pte_t *pte;
  87. int ret;
  88. pgd = pgd_offset(vma->vm_mm, address);
  89. if (pgd_none_or_clear_bad(pgd))
  90. return 0;
  91. p4d = p4d_offset(pgd, address);
  92. if (p4d_none_or_clear_bad(p4d))
  93. return 0;
  94. pud = pud_offset(p4d, address);
  95. if (pud_none_or_clear_bad(pud))
  96. return 0;
  97. pmd = pmd_offset(pud, address);
  98. if (pmd_none_or_clear_bad(pmd))
  99. return 0;
  100. /*
  101. * This is called while another page table is mapped, so we
  102. * must use the nested version. This also means we need to
  103. * open-code the spin-locking.
  104. */
  105. pte = pte_offset_map_nolock(vma->vm_mm, pmd, address, &ptl);
  106. if (!pte)
  107. return 0;
  108. do_pte_lock(ptl);
  109. ret = do_adjust_pte(vma, address, pfn, pte);
  110. do_pte_unlock(ptl);
  111. pte_unmap(pte);
  112. return ret;
  113. }
  114. static void
  115. make_coherent(struct address_space *mapping, struct vm_area_struct *vma,
  116. unsigned long addr, pte_t *ptep, unsigned long pfn)
  117. {
  118. struct mm_struct *mm = vma->vm_mm;
  119. struct vm_area_struct *mpnt;
  120. unsigned long offset;
  121. pgoff_t pgoff;
  122. int aliases = 0;
  123. pgoff = vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT);
  124. /*
  125. * If we have any shared mappings that are in the same mm
  126. * space, then we need to handle them specially to maintain
  127. * cache coherency.
  128. */
  129. flush_dcache_mmap_lock(mapping);
  130. vma_interval_tree_foreach(mpnt, &mapping->i_mmap, pgoff, pgoff) {
  131. /*
  132. * If this VMA is not in our MM, we can ignore it.
  133. * Note that we intentionally mask out the VMA
  134. * that we are fixing up.
  135. */
  136. if (mpnt->vm_mm != mm || mpnt == vma)
  137. continue;
  138. if (!(mpnt->vm_flags & VM_MAYSHARE))
  139. continue;
  140. offset = (pgoff - mpnt->vm_pgoff) << PAGE_SHIFT;
  141. aliases += adjust_pte(mpnt, mpnt->vm_start + offset, pfn);
  142. }
  143. flush_dcache_mmap_unlock(mapping);
  144. if (aliases)
  145. do_adjust_pte(vma, addr, pfn, ptep);
  146. }
  147. /*
  148. * Take care of architecture specific things when placing a new PTE into
  149. * a page table, or changing an existing PTE. Basically, there are two
  150. * things that we need to take care of:
  151. *
  152. * 1. If PG_dcache_clean is not set for the page, we need to ensure
  153. * that any cache entries for the kernels virtual memory
  154. * range are written back to the page.
  155. * 2. If we have multiple shared mappings of the same space in
  156. * an object, we need to deal with the cache aliasing issues.
  157. *
  158. * Note that the pte lock will be held.
  159. */
  160. void update_mmu_cache_range(struct vm_fault *vmf, struct vm_area_struct *vma,
  161. unsigned long addr, pte_t *ptep, unsigned int nr)
  162. {
  163. unsigned long pfn = pte_pfn(*ptep);
  164. struct address_space *mapping;
  165. struct folio *folio;
  166. if (!pfn_valid(pfn))
  167. return;
  168. /*
  169. * The zero page is never written to, so never has any dirty
  170. * cache lines, and therefore never needs to be flushed.
  171. */
  172. if (is_zero_pfn(pfn))
  173. return;
  174. folio = page_folio(pfn_to_page(pfn));
  175. mapping = folio_flush_mapping(folio);
  176. if (!test_and_set_bit(PG_dcache_clean, &folio->flags))
  177. __flush_dcache_folio(mapping, folio);
  178. if (mapping) {
  179. if (cache_is_vivt())
  180. make_coherent(mapping, vma, addr, ptep, pfn);
  181. else if (vma->vm_flags & VM_EXEC)
  182. __flush_icache_all();
  183. }
  184. }
  185. #endif /* __LINUX_ARM_ARCH__ < 6 */
  186. /*
  187. * Check whether the write buffer has physical address aliasing
  188. * issues. If it has, we need to avoid them for the case where
  189. * we have several shared mappings of the same object in user
  190. * space.
  191. */
  192. static int __init check_writebuffer(unsigned long *p1, unsigned long *p2)
  193. {
  194. register unsigned long zero = 0, one = 1, val;
  195. local_irq_disable();
  196. mb();
  197. *p1 = one;
  198. mb();
  199. *p2 = zero;
  200. mb();
  201. val = *p1;
  202. mb();
  203. local_irq_enable();
  204. return val != zero;
  205. }
  206. void __init check_writebuffer_bugs(void)
  207. {
  208. struct page *page;
  209. const char *reason;
  210. unsigned long v = 1;
  211. pr_info("CPU: Testing write buffer coherency: ");
  212. page = alloc_page(GFP_KERNEL);
  213. if (page) {
  214. unsigned long *p1, *p2;
  215. pgprot_t prot = __pgprot_modify(PAGE_KERNEL,
  216. L_PTE_MT_MASK, L_PTE_MT_BUFFERABLE);
  217. p1 = vmap(&page, 1, VM_IOREMAP, prot);
  218. p2 = vmap(&page, 1, VM_IOREMAP, prot);
  219. if (p1 && p2) {
  220. v = check_writebuffer(p1, p2);
  221. reason = "enabling work-around";
  222. } else {
  223. reason = "unable to map memory\n";
  224. }
  225. vunmap(p1);
  226. vunmap(p2);
  227. put_page(page);
  228. } else {
  229. reason = "unable to grab page\n";
  230. }
  231. if (v) {
  232. pr_cont("failed, %s\n", reason);
  233. shared_pte_mask = L_PTE_MT_UNCACHED;
  234. } else {
  235. pr_cont("ok\n");
  236. }
  237. }