fault-armv.c 5.5 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. flush_cache_page(vma, address, pte_pfn(entry));
  62. pte_val(entry) &= ~L_PTE_MT_MASK;
  63. pte_val(entry) |= shared_pte_mask;
  64. set_pte_at(vma->vm_mm, address, pte, entry);
  65. flush_tlb_page(vma, address);
  66. }
  67. pte_unmap(pte);
  68. return ret;
  69. bad_pgd:
  70. pgd_ERROR(*pgd);
  71. pgd_clear(pgd);
  72. no_pgd:
  73. return 0;
  74. bad_pmd:
  75. pmd_ERROR(*pmd);
  76. pmd_clear(pmd);
  77. no_pmd:
  78. return 0;
  79. }
  80. static void
  81. make_coherent(struct address_space *mapping, struct vm_area_struct *vma, unsigned long addr, unsigned long pfn)
  82. {
  83. struct mm_struct *mm = vma->vm_mm;
  84. struct vm_area_struct *mpnt;
  85. struct prio_tree_iter iter;
  86. unsigned long offset;
  87. pgoff_t pgoff;
  88. int aliases = 0;
  89. pgoff = vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT);
  90. /*
  91. * If we have any shared mappings that are in the same mm
  92. * space, then we need to handle them specially to maintain
  93. * cache coherency.
  94. */
  95. flush_dcache_mmap_lock(mapping);
  96. vma_prio_tree_foreach(mpnt, &iter, &mapping->i_mmap, pgoff, pgoff) {
  97. /*
  98. * If this VMA is not in our MM, we can ignore it.
  99. * Note that we intentionally mask out the VMA
  100. * that we are fixing up.
  101. */
  102. if (mpnt->vm_mm != mm || mpnt == vma)
  103. continue;
  104. if (!(mpnt->vm_flags & VM_MAYSHARE))
  105. continue;
  106. offset = (pgoff - mpnt->vm_pgoff) << PAGE_SHIFT;
  107. aliases += adjust_pte(mpnt, mpnt->vm_start + offset);
  108. }
  109. flush_dcache_mmap_unlock(mapping);
  110. if (aliases)
  111. adjust_pte(vma, addr);
  112. else
  113. flush_cache_page(vma, addr, pfn);
  114. }
  115. /*
  116. * Take care of architecture specific things when placing a new PTE into
  117. * a page table, or changing an existing PTE. Basically, there are two
  118. * things that we need to take care of:
  119. *
  120. * 1. If PG_dcache_dirty is set for the page, we need to ensure
  121. * that any cache entries for the kernels virtual memory
  122. * range are written back to the page.
  123. * 2. If we have multiple shared mappings of the same space in
  124. * an object, we need to deal with the cache aliasing issues.
  125. *
  126. * Note that the pte lock will be held.
  127. */
  128. void update_mmu_cache(struct vm_area_struct *vma, unsigned long addr, pte_t pte)
  129. {
  130. unsigned long pfn = pte_pfn(pte);
  131. struct address_space *mapping;
  132. struct page *page;
  133. if (!pfn_valid(pfn))
  134. return;
  135. page = pfn_to_page(pfn);
  136. mapping = page_mapping(page);
  137. if (mapping) {
  138. #ifndef CONFIG_SMP
  139. int dirty = test_and_clear_bit(PG_dcache_dirty, &page->flags);
  140. if (dirty)
  141. __flush_dcache_page(mapping, page);
  142. #endif
  143. if (cache_is_vivt())
  144. make_coherent(mapping, vma, addr, pfn);
  145. else if (vma->vm_flags & VM_EXEC)
  146. __flush_icache_all();
  147. }
  148. }
  149. /*
  150. * Check whether the write buffer has physical address aliasing
  151. * issues. If it has, we need to avoid them for the case where
  152. * we have several shared mappings of the same object in user
  153. * space.
  154. */
  155. static int __init check_writebuffer(unsigned long *p1, unsigned long *p2)
  156. {
  157. register unsigned long zero = 0, one = 1, val;
  158. local_irq_disable();
  159. mb();
  160. *p1 = one;
  161. mb();
  162. *p2 = zero;
  163. mb();
  164. val = *p1;
  165. mb();
  166. local_irq_enable();
  167. return val != zero;
  168. }
  169. void __init check_writebuffer_bugs(void)
  170. {
  171. struct page *page;
  172. const char *reason;
  173. unsigned long v = 1;
  174. printk(KERN_INFO "CPU: Testing write buffer coherency: ");
  175. page = alloc_page(GFP_KERNEL);
  176. if (page) {
  177. unsigned long *p1, *p2;
  178. pgprot_t prot = __pgprot(L_PTE_PRESENT|L_PTE_YOUNG|
  179. L_PTE_DIRTY|L_PTE_WRITE|
  180. L_PTE_MT_BUFFERABLE);
  181. p1 = vmap(&page, 1, VM_IOREMAP, prot);
  182. p2 = vmap(&page, 1, VM_IOREMAP, prot);
  183. if (p1 && p2) {
  184. v = check_writebuffer(p1, p2);
  185. reason = "enabling work-around";
  186. } else {
  187. reason = "unable to map memory\n";
  188. }
  189. vunmap(p1);
  190. vunmap(p2);
  191. put_page(page);
  192. } else {
  193. reason = "unable to grab page\n";
  194. }
  195. if (v) {
  196. printk("failed, %s\n", reason);
  197. shared_pte_mask = L_PTE_MT_UNCACHED;
  198. } else {
  199. printk("ok\n");
  200. }
  201. }