fault-armv.c 6.7 KB

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