pgtable.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #ifndef _ASM_GENERIC_PGTABLE_H
  2. #define _ASM_GENERIC_PGTABLE_H
  3. #ifndef __HAVE_ARCH_PTEP_ESTABLISH
  4. /*
  5. * Establish a new mapping:
  6. * - flush the old one
  7. * - update the page tables
  8. * - inform the TLB about the new one
  9. *
  10. * We hold the mm semaphore for reading, and the pte lock.
  11. *
  12. * Note: the old pte is known to not be writable, so we don't need to
  13. * worry about dirty bits etc getting lost.
  14. */
  15. #ifndef __HAVE_ARCH_SET_PTE_ATOMIC
  16. #define ptep_establish(__vma, __address, __ptep, __entry) \
  17. do { \
  18. set_pte_at((__vma)->vm_mm, (__address), __ptep, __entry); \
  19. flush_tlb_page(__vma, __address); \
  20. } while (0)
  21. #else /* __HAVE_ARCH_SET_PTE_ATOMIC */
  22. #define ptep_establish(__vma, __address, __ptep, __entry) \
  23. do { \
  24. set_pte_atomic(__ptep, __entry); \
  25. flush_tlb_page(__vma, __address); \
  26. } while (0)
  27. #endif /* __HAVE_ARCH_SET_PTE_ATOMIC */
  28. #endif
  29. #ifndef __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
  30. /*
  31. * Largely same as above, but only sets the access flags (dirty,
  32. * accessed, and writable). Furthermore, we know it always gets set
  33. * to a "more permissive" setting, which allows most architectures
  34. * to optimize this.
  35. */
  36. #define ptep_set_access_flags(__vma, __address, __ptep, __entry, __dirty) \
  37. do { \
  38. set_pte_at((__vma)->vm_mm, (__address), __ptep, __entry); \
  39. flush_tlb_page(__vma, __address); \
  40. } while (0)
  41. #endif
  42. #ifndef __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
  43. #define ptep_test_and_clear_young(__vma, __address, __ptep) \
  44. ({ \
  45. pte_t __pte = *(__ptep); \
  46. int r = 1; \
  47. if (!pte_young(__pte)) \
  48. r = 0; \
  49. else \
  50. set_pte_at((__vma)->vm_mm, (__address), \
  51. (__ptep), pte_mkold(__pte)); \
  52. r; \
  53. })
  54. #endif
  55. #ifndef __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
  56. #define ptep_clear_flush_young(__vma, __address, __ptep) \
  57. ({ \
  58. int __young; \
  59. __young = ptep_test_and_clear_young(__vma, __address, __ptep); \
  60. if (__young) \
  61. flush_tlb_page(__vma, __address); \
  62. __young; \
  63. })
  64. #endif
  65. #ifndef __HAVE_ARCH_PTEP_TEST_AND_CLEAR_DIRTY
  66. #define ptep_test_and_clear_dirty(__vma, __address, __ptep) \
  67. ({ \
  68. pte_t __pte = *__ptep; \
  69. int r = 1; \
  70. if (!pte_dirty(__pte)) \
  71. r = 0; \
  72. else \
  73. set_pte_at((__vma)->vm_mm, (__address), (__ptep), \
  74. pte_mkclean(__pte)); \
  75. r; \
  76. })
  77. #endif
  78. #ifndef __HAVE_ARCH_PTEP_CLEAR_DIRTY_FLUSH
  79. #define ptep_clear_flush_dirty(__vma, __address, __ptep) \
  80. ({ \
  81. int __dirty; \
  82. __dirty = ptep_test_and_clear_dirty(__vma, __address, __ptep); \
  83. if (__dirty) \
  84. flush_tlb_page(__vma, __address); \
  85. __dirty; \
  86. })
  87. #endif
  88. #ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR
  89. #define ptep_get_and_clear(__mm, __address, __ptep) \
  90. ({ \
  91. pte_t __pte = *(__ptep); \
  92. pte_clear((__mm), (__address), (__ptep)); \
  93. __pte; \
  94. })
  95. #endif
  96. #ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL
  97. #define ptep_get_and_clear_full(__mm, __address, __ptep, __full) \
  98. ({ \
  99. pte_t __pte; \
  100. __pte = ptep_get_and_clear((__mm), (__address), (__ptep)); \
  101. __pte; \
  102. })
  103. #endif
  104. #ifndef __HAVE_ARCH_PTE_CLEAR_FULL
  105. #define pte_clear_full(__mm, __address, __ptep, __full) \
  106. do { \
  107. pte_clear((__mm), (__address), (__ptep)); \
  108. } while (0)
  109. #endif
  110. #ifndef __HAVE_ARCH_PTEP_CLEAR_FLUSH
  111. #define ptep_clear_flush(__vma, __address, __ptep) \
  112. ({ \
  113. pte_t __pte; \
  114. __pte = ptep_get_and_clear((__vma)->vm_mm, __address, __ptep); \
  115. flush_tlb_page(__vma, __address); \
  116. __pte; \
  117. })
  118. #endif
  119. #ifndef __HAVE_ARCH_PTEP_SET_WRPROTECT
  120. struct mm_struct;
  121. static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long address, pte_t *ptep)
  122. {
  123. pte_t old_pte = *ptep;
  124. set_pte_at(mm, address, ptep, pte_wrprotect(old_pte));
  125. }
  126. #endif
  127. #ifndef __HAVE_ARCH_PTE_SAME
  128. #define pte_same(A,B) (pte_val(A) == pte_val(B))
  129. #endif
  130. #ifndef __HAVE_ARCH_PAGE_TEST_AND_CLEAR_DIRTY
  131. #define page_test_and_clear_dirty(page) (0)
  132. #define pte_maybe_dirty(pte) pte_dirty(pte)
  133. #else
  134. #define pte_maybe_dirty(pte) (1)
  135. #endif
  136. #ifndef __HAVE_ARCH_PAGE_TEST_AND_CLEAR_YOUNG
  137. #define page_test_and_clear_young(page) (0)
  138. #endif
  139. #ifndef __HAVE_ARCH_PGD_OFFSET_GATE
  140. #define pgd_offset_gate(mm, addr) pgd_offset(mm, addr)
  141. #endif
  142. #ifndef __HAVE_ARCH_LAZY_MMU_PROT_UPDATE
  143. #define lazy_mmu_prot_update(pte) do { } while (0)
  144. #endif
  145. #ifndef __HAVE_ARCH_MULTIPLE_ZERO_PAGE
  146. #define move_pte(pte, prot, old_addr, new_addr) (pte)
  147. #else
  148. #define move_pte(pte, prot, old_addr, new_addr) \
  149. ({ \
  150. pte_t newpte = (pte); \
  151. if (pte_present(pte) && pfn_valid(pte_pfn(pte)) && \
  152. pte_page(pte) == ZERO_PAGE(old_addr)) \
  153. newpte = mk_pte(ZERO_PAGE(new_addr), (prot)); \
  154. newpte; \
  155. })
  156. #endif
  157. /*
  158. * When walking page tables, get the address of the next boundary,
  159. * or the end address of the range if that comes earlier. Although no
  160. * vma end wraps to 0, rounded up __boundary may wrap to 0 throughout.
  161. */
  162. #define pgd_addr_end(addr, end) \
  163. ({ unsigned long __boundary = ((addr) + PGDIR_SIZE) & PGDIR_MASK; \
  164. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  165. })
  166. #ifndef pud_addr_end
  167. #define pud_addr_end(addr, end) \
  168. ({ unsigned long __boundary = ((addr) + PUD_SIZE) & PUD_MASK; \
  169. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  170. })
  171. #endif
  172. #ifndef pmd_addr_end
  173. #define pmd_addr_end(addr, end) \
  174. ({ unsigned long __boundary = ((addr) + PMD_SIZE) & PMD_MASK; \
  175. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  176. })
  177. #endif
  178. #ifndef __ASSEMBLY__
  179. /*
  180. * When walking page tables, we usually want to skip any p?d_none entries;
  181. * and any p?d_bad entries - reporting the error before resetting to none.
  182. * Do the tests inline, but report and clear the bad entry in mm/memory.c.
  183. */
  184. void pgd_clear_bad(pgd_t *);
  185. void pud_clear_bad(pud_t *);
  186. void pmd_clear_bad(pmd_t *);
  187. static inline int pgd_none_or_clear_bad(pgd_t *pgd)
  188. {
  189. if (pgd_none(*pgd))
  190. return 1;
  191. if (unlikely(pgd_bad(*pgd))) {
  192. pgd_clear_bad(pgd);
  193. return 1;
  194. }
  195. return 0;
  196. }
  197. static inline int pud_none_or_clear_bad(pud_t *pud)
  198. {
  199. if (pud_none(*pud))
  200. return 1;
  201. if (unlikely(pud_bad(*pud))) {
  202. pud_clear_bad(pud);
  203. return 1;
  204. }
  205. return 0;
  206. }
  207. static inline int pmd_none_or_clear_bad(pmd_t *pmd)
  208. {
  209. if (pmd_none(*pmd))
  210. return 1;
  211. if (unlikely(pmd_bad(*pmd))) {
  212. pmd_clear_bad(pmd);
  213. return 1;
  214. }
  215. return 0;
  216. }
  217. #endif /* !__ASSEMBLY__ */
  218. #endif /* _ASM_GENERIC_PGTABLE_H */