fault-armv.c 5.7 KB

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