fault-armv.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 <asm/bugs.h>
  20. #include <asm/cacheflush.h>
  21. #include <asm/cachetype.h>
  22. #include <asm/pgtable.h>
  23. #include <asm/tlbflush.h>
  24. static unsigned long shared_pte_mask = L_PTE_MT_BUFFERABLE;
  25. /*
  26. * We take the easy way out of this problem - we make the
  27. * PTE uncacheable. However, we leave the write buffer on.
  28. *
  29. * Note that the pte lock held when calling update_mmu_cache must also
  30. * guard the pte (somewhere else in the same mm) that we modify here.
  31. * Therefore those configurations which might call adjust_pte (those
  32. * without CONFIG_CPU_CACHE_VIPT) cannot support split page_table_lock.
  33. */
  34. static int adjust_pte(struct vm_area_struct *vma, unsigned long address)
  35. {
  36. pgd_t *pgd;
  37. pmd_t *pmd;
  38. pte_t *pte, entry;
  39. int ret;
  40. pgd = pgd_offset(vma->vm_mm, address);
  41. if (pgd_none(*pgd))
  42. goto no_pgd;
  43. if (pgd_bad(*pgd))
  44. goto bad_pgd;
  45. pmd = pmd_offset(pgd, address);
  46. if (pmd_none(*pmd))
  47. goto no_pmd;
  48. if (pmd_bad(*pmd))
  49. goto bad_pmd;
  50. pte = pte_offset_map(pmd, address);
  51. entry = *pte;
  52. /*
  53. * If this page is present, it's actually being shared.
  54. */
  55. ret = pte_present(entry);
  56. /*
  57. * If this page isn't present, or is already setup to
  58. * fault (ie, is old), we can safely ignore any issues.
  59. */
  60. if (ret && (pte_val(entry) & L_PTE_MT_MASK) != shared_pte_mask) {
  61. unsigned long pfn = pte_pfn(entry);
  62. flush_cache_page(vma, address, pfn);
  63. outer_flush_range((pfn << PAGE_SHIFT),
  64. (pfn << PAGE_SHIFT) + PAGE_SIZE);
  65. pte_val(entry) &= ~L_PTE_MT_MASK;
  66. pte_val(entry) |= shared_pte_mask;
  67. set_pte_at(vma->vm_mm, address, pte, entry);
  68. flush_tlb_page(vma, address);
  69. }
  70. pte_unmap(pte);
  71. return ret;
  72. bad_pgd:
  73. pgd_ERROR(*pgd);
  74. pgd_clear(pgd);
  75. no_pgd:
  76. return 0;
  77. bad_pmd:
  78. pmd_ERROR(*pmd);
  79. pmd_clear(pmd);
  80. no_pmd:
  81. return 0;
  82. }
  83. static void
  84. make_coherent(struct address_space *mapping, struct vm_area_struct *vma, unsigned long addr, unsigned long pfn)
  85. {
  86. struct mm_struct *mm = vma->vm_mm;
  87. struct vm_area_struct *mpnt;
  88. struct prio_tree_iter iter;
  89. unsigned long offset;
  90. pgoff_t pgoff;
  91. int aliases = 0;
  92. pgoff = vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT);
  93. /*
  94. * If we have any shared mappings that are in the same mm
  95. * space, then we need to handle them specially to maintain
  96. * cache coherency.
  97. */
  98. flush_dcache_mmap_lock(mapping);
  99. vma_prio_tree_foreach(mpnt, &iter, &mapping->i_mmap, pgoff, pgoff) {
  100. /*
  101. * If this VMA is not in our MM, we can ignore it.
  102. * Note that we intentionally mask out the VMA
  103. * that we are fixing up.
  104. */
  105. if (mpnt->vm_mm != mm || mpnt == vma)
  106. continue;
  107. if (!(mpnt->vm_flags & VM_MAYSHARE))
  108. continue;
  109. offset = (pgoff - mpnt->vm_pgoff) << PAGE_SHIFT;
  110. aliases += adjust_pte(mpnt, mpnt->vm_start + offset);
  111. }
  112. flush_dcache_mmap_unlock(mapping);
  113. if (aliases)
  114. adjust_pte(vma, addr);
  115. else
  116. flush_cache_page(vma, addr, pfn);
  117. }
  118. /*
  119. * Take care of architecture specific things when placing a new PTE into
  120. * a page table, or changing an existing PTE. Basically, there are two
  121. * things that we need to take care of:
  122. *
  123. * 1. If PG_dcache_dirty is set for the page, we need to ensure
  124. * that any cache entries for the kernels virtual memory
  125. * range are written back to the page.
  126. * 2. If we have multiple shared mappings of the same space in
  127. * an object, we need to deal with the cache aliasing issues.
  128. *
  129. * Note that the pte lock will be held.
  130. */
  131. void update_mmu_cache(struct vm_area_struct *vma, unsigned long addr, pte_t pte)
  132. {
  133. unsigned long pfn = pte_pfn(pte);
  134. struct address_space *mapping;
  135. struct page *page;
  136. if (!pfn_valid(pfn))
  137. return;
  138. page = pfn_to_page(pfn);
  139. mapping = page_mapping(page);
  140. #ifndef CONFIG_SMP
  141. if (test_and_clear_bit(PG_dcache_dirty, &page->flags))
  142. __flush_dcache_page(mapping, page);
  143. #endif
  144. if (mapping) {
  145. if (cache_is_vivt())
  146. make_coherent(mapping, vma, addr, pfn);
  147. else if (vma->vm_flags & VM_EXEC)
  148. __flush_icache_all();
  149. }
  150. }
  151. /*
  152. * Check whether the write buffer has physical address aliasing
  153. * issues. If it has, we need to avoid them for the case where
  154. * we have several shared mappings of the same object in user
  155. * space.
  156. */
  157. static int __init check_writebuffer(unsigned long *p1, unsigned long *p2)
  158. {
  159. register unsigned long zero = 0, one = 1, val;
  160. local_irq_disable();
  161. mb();
  162. *p1 = one;
  163. mb();
  164. *p2 = zero;
  165. mb();
  166. val = *p1;
  167. mb();
  168. local_irq_enable();
  169. return val != zero;
  170. }
  171. void __init check_writebuffer_bugs(void)
  172. {
  173. struct page *page;
  174. const char *reason;
  175. unsigned long v = 1;
  176. printk(KERN_INFO "CPU: Testing write buffer coherency: ");
  177. page = alloc_page(GFP_KERNEL);
  178. if (page) {
  179. unsigned long *p1, *p2;
  180. pgprot_t prot = __pgprot(L_PTE_PRESENT|L_PTE_YOUNG|
  181. L_PTE_DIRTY|L_PTE_WRITE|
  182. L_PTE_MT_BUFFERABLE);
  183. p1 = vmap(&page, 1, VM_IOREMAP, prot);
  184. p2 = vmap(&page, 1, VM_IOREMAP, prot);
  185. if (p1 && p2) {
  186. v = check_writebuffer(p1, p2);
  187. reason = "enabling work-around";
  188. } else {
  189. reason = "unable to map memory\n";
  190. }
  191. vunmap(p1);
  192. vunmap(p2);
  193. put_page(page);
  194. } else {
  195. reason = "unable to grab page\n";
  196. }
  197. if (v) {
  198. printk("failed, %s\n", reason);
  199. shared_pte_mask = L_PTE_MT_UNCACHED;
  200. } else {
  201. printk("ok\n");
  202. }
  203. }