pgtable.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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/percpu.h>
  27. #include <linux/hardirq.h>
  28. #include <linux/hugetlb.h>
  29. #include <asm/pgalloc.h>
  30. #include <asm/tlbflush.h>
  31. #include <asm/tlb.h>
  32. static inline int is_exec_fault(void)
  33. {
  34. return current->thread.regs && TRAP(current->thread.regs) == 0x400;
  35. }
  36. /* We only try to do i/d cache coherency on stuff that looks like
  37. * reasonably "normal" PTEs. We currently require a PTE to be present
  38. * and we avoid _PAGE_SPECIAL and cache inhibited pte. We also only do that
  39. * on userspace PTEs
  40. */
  41. static inline int pte_looks_normal(pte_t pte)
  42. {
  43. #if defined(CONFIG_PPC_BOOK3S_64)
  44. if ((pte_val(pte) & (_PAGE_PRESENT | _PAGE_SPECIAL)) == _PAGE_PRESENT) {
  45. if (pte_ci(pte))
  46. return 0;
  47. if (pte_user(pte))
  48. return 1;
  49. }
  50. return 0;
  51. #else
  52. return (pte_val(pte) &
  53. (_PAGE_PRESENT | _PAGE_SPECIAL | _PAGE_NO_CACHE | _PAGE_USER |
  54. _PAGE_PRIVILEGED)) ==
  55. (_PAGE_PRESENT | _PAGE_USER);
  56. #endif
  57. }
  58. static struct page *maybe_pte_to_page(pte_t pte)
  59. {
  60. unsigned long pfn = pte_pfn(pte);
  61. struct page *page;
  62. if (unlikely(!pfn_valid(pfn)))
  63. return NULL;
  64. page = pfn_to_page(pfn);
  65. if (PageReserved(page))
  66. return NULL;
  67. return page;
  68. }
  69. #if defined(CONFIG_PPC_STD_MMU) || _PAGE_EXEC == 0
  70. /* Server-style MMU handles coherency when hashing if HW exec permission
  71. * is supposed per page (currently 64-bit only). If not, then, we always
  72. * flush the cache for valid PTEs in set_pte. Embedded CPU without HW exec
  73. * support falls into the same category.
  74. */
  75. static pte_t set_pte_filter(pte_t pte)
  76. {
  77. if (radix_enabled())
  78. return pte;
  79. pte = __pte(pte_val(pte) & ~_PAGE_HPTEFLAGS);
  80. if (pte_looks_normal(pte) && !(cpu_has_feature(CPU_FTR_COHERENT_ICACHE) ||
  81. cpu_has_feature(CPU_FTR_NOEXECUTE))) {
  82. struct page *pg = maybe_pte_to_page(pte);
  83. if (!pg)
  84. return pte;
  85. if (!test_bit(PG_arch_1, &pg->flags)) {
  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)
  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. /*
  164. * When handling numa faults, we already have the pte marked
  165. * _PAGE_PRESENT, but we can be sure that it is not in hpte.
  166. * Hence we can use set_pte_at for them.
  167. */
  168. VM_WARN_ON(pte_present(*ptep) && !pte_protnone(*ptep));
  169. /* Add the pte bit when trying to set a pte */
  170. pte = __pte(pte_val(pte) | _PAGE_PTE);
  171. /* Note: mm->context.id might not yet have been assigned as
  172. * this context might not have been activated yet when this
  173. * is called.
  174. */
  175. pte = set_pte_filter(pte);
  176. /* Perform the setting of the PTE */
  177. __set_pte_at(mm, addr, ptep, pte, 0);
  178. }
  179. /*
  180. * This is called when relaxing access to a PTE. It's also called in the page
  181. * fault path when we don't hit any of the major fault cases, ie, a minor
  182. * update of _PAGE_ACCESSED, _PAGE_DIRTY, etc... The generic code will have
  183. * handled those two for us, we additionally deal with missing execute
  184. * permission here on some processors
  185. */
  186. int ptep_set_access_flags(struct vm_area_struct *vma, unsigned long address,
  187. pte_t *ptep, pte_t entry, int dirty)
  188. {
  189. int changed;
  190. entry = set_access_flags_filter(entry, vma, dirty);
  191. changed = !pte_same(*(ptep), entry);
  192. if (changed) {
  193. assert_pte_locked(vma->vm_mm, address);
  194. __ptep_set_access_flags(vma, ptep, entry,
  195. address, mmu_virtual_psize);
  196. }
  197. return changed;
  198. }
  199. #ifdef CONFIG_HUGETLB_PAGE
  200. extern int huge_ptep_set_access_flags(struct vm_area_struct *vma,
  201. unsigned long addr, pte_t *ptep,
  202. pte_t pte, int dirty)
  203. {
  204. #ifdef HUGETLB_NEED_PRELOAD
  205. /*
  206. * The "return 1" forces a call of update_mmu_cache, which will write a
  207. * TLB entry. Without this, platforms that don't do a write of the TLB
  208. * entry in the TLB miss handler asm will fault ad infinitum.
  209. */
  210. ptep_set_access_flags(vma, addr, ptep, pte, dirty);
  211. return 1;
  212. #else
  213. int changed, psize;
  214. pte = set_access_flags_filter(pte, vma, dirty);
  215. changed = !pte_same(*(ptep), pte);
  216. if (changed) {
  217. #ifdef CONFIG_PPC_BOOK3S_64
  218. struct hstate *h = hstate_vma(vma);
  219. psize = hstate_get_psize(h);
  220. #ifdef CONFIG_DEBUG_VM
  221. assert_spin_locked(huge_pte_lockptr(h, vma->vm_mm, ptep));
  222. #endif
  223. #else
  224. /*
  225. * Not used on non book3s64 platforms. But 8xx
  226. * can possibly use tsize derived from hstate.
  227. */
  228. psize = 0;
  229. #endif
  230. __ptep_set_access_flags(vma, ptep, pte, addr, psize);
  231. }
  232. return changed;
  233. #endif
  234. }
  235. #endif /* CONFIG_HUGETLB_PAGE */
  236. #ifdef CONFIG_DEBUG_VM
  237. void assert_pte_locked(struct mm_struct *mm, unsigned long addr)
  238. {
  239. pgd_t *pgd;
  240. pud_t *pud;
  241. pmd_t *pmd;
  242. if (mm == &init_mm)
  243. return;
  244. pgd = mm->pgd + pgd_index(addr);
  245. BUG_ON(pgd_none(*pgd));
  246. pud = pud_offset(pgd, addr);
  247. BUG_ON(pud_none(*pud));
  248. pmd = pmd_offset(pud, addr);
  249. /*
  250. * khugepaged to collapse normal pages to hugepage, first set
  251. * pmd to none to force page fault/gup to take mmap_sem. After
  252. * pmd is set to none, we do a pte_clear which does this assertion
  253. * so if we find pmd none, return.
  254. */
  255. if (pmd_none(*pmd))
  256. return;
  257. BUG_ON(!pmd_present(*pmd));
  258. assert_spin_locked(pte_lockptr(mm, pmd));
  259. }
  260. #endif /* CONFIG_DEBUG_VM */
  261. unsigned long vmalloc_to_phys(void *va)
  262. {
  263. unsigned long pfn = vmalloc_to_pfn(va);
  264. BUG_ON(!pfn);
  265. return __pa(pfn_to_kaddr(pfn)) + offset_in_page(va);
  266. }
  267. EXPORT_SYMBOL_GPL(vmalloc_to_phys);