pgtable.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * This file contains common routines for dealing with free of page tables
  3. * Along with common page table handling code
  4. *
  5. * Derived from arch/powerpc/mm/tlb_64.c:
  6. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  7. *
  8. * Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
  9. * and Cort Dougan (PReP) (cort@cs.nmt.edu)
  10. * Copyright (C) 1996 Paul Mackerras
  11. *
  12. * Derived from "arch/i386/mm/init.c"
  13. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  14. *
  15. * Dave Engebretsen <engebret@us.ibm.com>
  16. * Rework for PPC64 port.
  17. *
  18. * This program is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU General Public License
  20. * as published by the Free Software Foundation; either version
  21. * 2 of the License, or (at your option) any later version.
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/gfp.h>
  25. #include <linux/mm.h>
  26. #include <linux/init.h>
  27. #include <linux/percpu.h>
  28. #include <linux/hardirq.h>
  29. #include <linux/hugetlb.h>
  30. #include <asm/pgalloc.h>
  31. #include <asm/tlbflush.h>
  32. #include <asm/tlb.h>
  33. #include "mmu_decl.h"
  34. static inline int is_exec_fault(void)
  35. {
  36. return current->thread.regs && TRAP(current->thread.regs) == 0x400;
  37. }
  38. /* We only try to do i/d cache coherency on stuff that looks like
  39. * reasonably "normal" PTEs. We currently require a PTE to be present
  40. * and we avoid _PAGE_SPECIAL and _PAGE_NO_CACHE. We also only do that
  41. * on userspace PTEs
  42. */
  43. static inline int pte_looks_normal(pte_t pte)
  44. {
  45. return (pte_val(pte) &
  46. (_PAGE_PRESENT | _PAGE_SPECIAL | _PAGE_NO_CACHE | _PAGE_USER)) ==
  47. (_PAGE_PRESENT | _PAGE_USER);
  48. }
  49. struct page * maybe_pte_to_page(pte_t pte)
  50. {
  51. unsigned long pfn = pte_pfn(pte);
  52. struct page *page;
  53. if (unlikely(!pfn_valid(pfn)))
  54. return NULL;
  55. page = pfn_to_page(pfn);
  56. if (PageReserved(page))
  57. return NULL;
  58. return page;
  59. }
  60. #if defined(CONFIG_PPC_STD_MMU) || _PAGE_EXEC == 0
  61. /* Server-style MMU handles coherency when hashing if HW exec permission
  62. * is supposed per page (currently 64-bit only). If not, then, we always
  63. * flush the cache for valid PTEs in set_pte. Embedded CPU without HW exec
  64. * support falls into the same category.
  65. */
  66. static pte_t set_pte_filter(pte_t pte, unsigned long addr)
  67. {
  68. pte = __pte(pte_val(pte) & ~_PAGE_HPTEFLAGS);
  69. if (pte_looks_normal(pte) && !(cpu_has_feature(CPU_FTR_COHERENT_ICACHE) ||
  70. cpu_has_feature(CPU_FTR_NOEXECUTE))) {
  71. struct page *pg = maybe_pte_to_page(pte);
  72. if (!pg)
  73. return pte;
  74. if (!test_bit(PG_arch_1, &pg->flags)) {
  75. #ifdef CONFIG_8xx
  76. /* On 8xx, cache control instructions (particularly
  77. * "dcbst" from flush_dcache_icache) fault as write
  78. * operation if there is an unpopulated TLB entry
  79. * for the address in question. To workaround that,
  80. * we invalidate the TLB here, thus avoiding dcbst
  81. * misbehaviour.
  82. */
  83. /* 8xx doesn't care about PID, size or ind args */
  84. _tlbil_va(addr, 0, 0, 0);
  85. #endif /* CONFIG_8xx */
  86. flush_dcache_icache_page(pg);
  87. set_bit(PG_arch_1, &pg->flags);
  88. }
  89. }
  90. return pte;
  91. }
  92. static pte_t set_access_flags_filter(pte_t pte, struct vm_area_struct *vma,
  93. int dirty)
  94. {
  95. return pte;
  96. }
  97. #else /* defined(CONFIG_PPC_STD_MMU) || _PAGE_EXEC == 0 */
  98. /* Embedded type MMU with HW exec support. This is a bit more complicated
  99. * as we don't have two bits to spare for _PAGE_EXEC and _PAGE_HWEXEC so
  100. * instead we "filter out" the exec permission for non clean pages.
  101. */
  102. static pte_t set_pte_filter(pte_t pte, unsigned long addr)
  103. {
  104. struct page *pg;
  105. /* No exec permission in the first place, move on */
  106. if (!(pte_val(pte) & _PAGE_EXEC) || !pte_looks_normal(pte))
  107. return pte;
  108. /* If you set _PAGE_EXEC on weird pages you're on your own */
  109. pg = maybe_pte_to_page(pte);
  110. if (unlikely(!pg))
  111. return pte;
  112. /* If the page clean, we move on */
  113. if (test_bit(PG_arch_1, &pg->flags))
  114. return pte;
  115. /* If it's an exec fault, we flush the cache and make it clean */
  116. if (is_exec_fault()) {
  117. flush_dcache_icache_page(pg);
  118. set_bit(PG_arch_1, &pg->flags);
  119. return pte;
  120. }
  121. /* Else, we filter out _PAGE_EXEC */
  122. return __pte(pte_val(pte) & ~_PAGE_EXEC);
  123. }
  124. static pte_t set_access_flags_filter(pte_t pte, struct vm_area_struct *vma,
  125. int dirty)
  126. {
  127. struct page *pg;
  128. /* So here, we only care about exec faults, as we use them
  129. * to recover lost _PAGE_EXEC and perform I$/D$ coherency
  130. * if necessary. Also if _PAGE_EXEC is already set, same deal,
  131. * we just bail out
  132. */
  133. if (dirty || (pte_val(pte) & _PAGE_EXEC) || !is_exec_fault())
  134. return pte;
  135. #ifdef CONFIG_DEBUG_VM
  136. /* So this is an exec fault, _PAGE_EXEC is not set. If it was
  137. * an error we would have bailed out earlier in do_page_fault()
  138. * but let's make sure of it
  139. */
  140. if (WARN_ON(!(vma->vm_flags & VM_EXEC)))
  141. return pte;
  142. #endif /* CONFIG_DEBUG_VM */
  143. /* If you set _PAGE_EXEC on weird pages you're on your own */
  144. pg = maybe_pte_to_page(pte);
  145. if (unlikely(!pg))
  146. goto bail;
  147. /* If the page is already clean, we move on */
  148. if (test_bit(PG_arch_1, &pg->flags))
  149. goto bail;
  150. /* Clean the page and set PG_arch_1 */
  151. flush_dcache_icache_page(pg);
  152. set_bit(PG_arch_1, &pg->flags);
  153. bail:
  154. return __pte(pte_val(pte) | _PAGE_EXEC);
  155. }
  156. #endif /* !(defined(CONFIG_PPC_STD_MMU) || _PAGE_EXEC == 0) */
  157. /*
  158. * set_pte stores a linux PTE into the linux page table.
  159. */
  160. void set_pte_at(struct mm_struct *mm, unsigned long addr, pte_t *ptep,
  161. pte_t pte)
  162. {
  163. #ifdef CONFIG_DEBUG_VM
  164. WARN_ON(pte_present(*ptep));
  165. #endif
  166. /* Note: mm->context.id might not yet have been assigned as
  167. * this context might not have been activated yet when this
  168. * is called.
  169. */
  170. pte = set_pte_filter(pte, addr);
  171. /* Perform the setting of the PTE */
  172. __set_pte_at(mm, addr, ptep, pte, 0);
  173. }
  174. /*
  175. * This is called when relaxing access to a PTE. It's also called in the page
  176. * fault path when we don't hit any of the major fault cases, ie, a minor
  177. * update of _PAGE_ACCESSED, _PAGE_DIRTY, etc... The generic code will have
  178. * handled those two for us, we additionally deal with missing execute
  179. * permission here on some processors
  180. */
  181. int ptep_set_access_flags(struct vm_area_struct *vma, unsigned long address,
  182. pte_t *ptep, pte_t entry, int dirty)
  183. {
  184. int changed;
  185. entry = set_access_flags_filter(entry, vma, dirty);
  186. changed = !pte_same(*(ptep), entry);
  187. if (changed) {
  188. if (!is_vm_hugetlb_page(vma))
  189. assert_pte_locked(vma->vm_mm, address);
  190. __ptep_set_access_flags(ptep, entry);
  191. flush_tlb_page_nohash(vma, address);
  192. }
  193. return changed;
  194. }
  195. #ifdef CONFIG_DEBUG_VM
  196. void assert_pte_locked(struct mm_struct *mm, unsigned long addr)
  197. {
  198. pgd_t *pgd;
  199. pud_t *pud;
  200. pmd_t *pmd;
  201. if (mm == &init_mm)
  202. return;
  203. pgd = mm->pgd + pgd_index(addr);
  204. BUG_ON(pgd_none(*pgd));
  205. pud = pud_offset(pgd, addr);
  206. BUG_ON(pud_none(*pud));
  207. pmd = pmd_offset(pud, addr);
  208. BUG_ON(!pmd_present(*pmd));
  209. assert_spin_locked(pte_lockptr(mm, pmd));
  210. }
  211. #endif /* CONFIG_DEBUG_VM */