pgtable.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 vma->vm_mm->page_table_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. static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long address, pte_t *ptep)
  121. {
  122. pte_t old_pte = *ptep;
  123. set_pte_at(mm, address, ptep, pte_wrprotect(old_pte));
  124. }
  125. #endif
  126. #ifndef __HAVE_ARCH_PTE_SAME
  127. #define pte_same(A,B) (pte_val(A) == pte_val(B))
  128. #endif
  129. #ifndef __HAVE_ARCH_PAGE_TEST_AND_CLEAR_DIRTY
  130. #define page_test_and_clear_dirty(page) (0)
  131. #define pte_maybe_dirty(pte) pte_dirty(pte)
  132. #else
  133. #define pte_maybe_dirty(pte) (1)
  134. #endif
  135. #ifndef __HAVE_ARCH_PAGE_TEST_AND_CLEAR_YOUNG
  136. #define page_test_and_clear_young(page) (0)
  137. #endif
  138. #ifndef __HAVE_ARCH_PGD_OFFSET_GATE
  139. #define pgd_offset_gate(mm, addr) pgd_offset(mm, addr)
  140. #endif
  141. #ifndef __HAVE_ARCH_LAZY_MMU_PROT_UPDATE
  142. #define lazy_mmu_prot_update(pte) do { } while (0)
  143. #endif
  144. /*
  145. * When walking page tables, get the address of the next boundary,
  146. * or the end address of the range if that comes earlier. Although no
  147. * vma end wraps to 0, rounded up __boundary may wrap to 0 throughout.
  148. */
  149. #define pgd_addr_end(addr, end) \
  150. ({ unsigned long __boundary = ((addr) + PGDIR_SIZE) & PGDIR_MASK; \
  151. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  152. })
  153. #ifndef pud_addr_end
  154. #define pud_addr_end(addr, end) \
  155. ({ unsigned long __boundary = ((addr) + PUD_SIZE) & PUD_MASK; \
  156. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  157. })
  158. #endif
  159. #ifndef pmd_addr_end
  160. #define pmd_addr_end(addr, end) \
  161. ({ unsigned long __boundary = ((addr) + PMD_SIZE) & PMD_MASK; \
  162. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  163. })
  164. #endif
  165. #ifndef __ASSEMBLY__
  166. /*
  167. * When walking page tables, we usually want to skip any p?d_none entries;
  168. * and any p?d_bad entries - reporting the error before resetting to none.
  169. * Do the tests inline, but report and clear the bad entry in mm/memory.c.
  170. */
  171. void pgd_clear_bad(pgd_t *);
  172. void pud_clear_bad(pud_t *);
  173. void pmd_clear_bad(pmd_t *);
  174. static inline int pgd_none_or_clear_bad(pgd_t *pgd)
  175. {
  176. if (pgd_none(*pgd))
  177. return 1;
  178. if (unlikely(pgd_bad(*pgd))) {
  179. pgd_clear_bad(pgd);
  180. return 1;
  181. }
  182. return 0;
  183. }
  184. static inline int pud_none_or_clear_bad(pud_t *pud)
  185. {
  186. if (pud_none(*pud))
  187. return 1;
  188. if (unlikely(pud_bad(*pud))) {
  189. pud_clear_bad(pud);
  190. return 1;
  191. }
  192. return 0;
  193. }
  194. static inline int pmd_none_or_clear_bad(pmd_t *pmd)
  195. {
  196. if (pmd_none(*pmd))
  197. return 1;
  198. if (unlikely(pmd_bad(*pmd))) {
  199. pmd_clear_bad(pmd);
  200. return 1;
  201. }
  202. return 0;
  203. }
  204. #endif /* !__ASSEMBLY__ */
  205. #endif /* _ASM_GENERIC_PGTABLE_H */