pgtable.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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_MOVE_PTE
  146. #define move_pte(pte, prot, old_addr, new_addr) (pte)
  147. #endif
  148. /*
  149. * When walking page tables, get the address of the next boundary,
  150. * or the end address of the range if that comes earlier. Although no
  151. * vma end wraps to 0, rounded up __boundary may wrap to 0 throughout.
  152. */
  153. #define pgd_addr_end(addr, end) \
  154. ({ unsigned long __boundary = ((addr) + PGDIR_SIZE) & PGDIR_MASK; \
  155. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  156. })
  157. #ifndef pud_addr_end
  158. #define pud_addr_end(addr, end) \
  159. ({ unsigned long __boundary = ((addr) + PUD_SIZE) & PUD_MASK; \
  160. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  161. })
  162. #endif
  163. #ifndef pmd_addr_end
  164. #define pmd_addr_end(addr, end) \
  165. ({ unsigned long __boundary = ((addr) + PMD_SIZE) & PMD_MASK; \
  166. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  167. })
  168. #endif
  169. #ifndef __ASSEMBLY__
  170. /*
  171. * When walking page tables, we usually want to skip any p?d_none entries;
  172. * and any p?d_bad entries - reporting the error before resetting to none.
  173. * Do the tests inline, but report and clear the bad entry in mm/memory.c.
  174. */
  175. void pgd_clear_bad(pgd_t *);
  176. void pud_clear_bad(pud_t *);
  177. void pmd_clear_bad(pmd_t *);
  178. static inline int pgd_none_or_clear_bad(pgd_t *pgd)
  179. {
  180. if (pgd_none(*pgd))
  181. return 1;
  182. if (unlikely(pgd_bad(*pgd))) {
  183. pgd_clear_bad(pgd);
  184. return 1;
  185. }
  186. return 0;
  187. }
  188. static inline int pud_none_or_clear_bad(pud_t *pud)
  189. {
  190. if (pud_none(*pud))
  191. return 1;
  192. if (unlikely(pud_bad(*pud))) {
  193. pud_clear_bad(pud);
  194. return 1;
  195. }
  196. return 0;
  197. }
  198. static inline int pmd_none_or_clear_bad(pmd_t *pmd)
  199. {
  200. if (pmd_none(*pmd))
  201. return 1;
  202. if (unlikely(pmd_bad(*pmd))) {
  203. pmd_clear_bad(pmd);
  204. return 1;
  205. }
  206. return 0;
  207. }
  208. #endif /* !__ASSEMBLY__ */
  209. #endif /* _ASM_GENERIC_PGTABLE_H */