fault-armv.c 5.4 KB

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