pgtable.h 5.5 KB

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