fault-armv.c 6.7 KB

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