fault-armv.c 5.3 KB

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