pgtable.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #ifndef _ASM_GENERIC_PGTABLE_H
  2. #define _ASM_GENERIC_PGTABLE_H
  3. #ifndef __ASSEMBLY__
  4. #ifndef __HAVE_ARCH_PTEP_ESTABLISH
  5. /*
  6. * Establish a new mapping:
  7. * - flush the old one
  8. * - update the page tables
  9. * - inform the TLB about the new one
  10. *
  11. * We hold the mm semaphore for reading, and the pte lock.
  12. *
  13. * Note: the old pte is known to not be writable, so we don't need to
  14. * worry about dirty bits etc getting lost.
  15. */
  16. #ifndef __HAVE_ARCH_SET_PTE_ATOMIC
  17. #define ptep_establish(__vma, __address, __ptep, __entry) \
  18. do { \
  19. set_pte_at((__vma)->vm_mm, (__address), __ptep, __entry); \
  20. flush_tlb_page(__vma, __address); \
  21. } while (0)
  22. #else /* __HAVE_ARCH_SET_PTE_ATOMIC */
  23. #define ptep_establish(__vma, __address, __ptep, __entry) \
  24. do { \
  25. set_pte_atomic(__ptep, __entry); \
  26. flush_tlb_page(__vma, __address); \
  27. } while (0)
  28. #endif /* __HAVE_ARCH_SET_PTE_ATOMIC */
  29. #endif
  30. #ifndef __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
  31. /*
  32. * Largely same as above, but only sets the access flags (dirty,
  33. * accessed, and writable). Furthermore, we know it always gets set
  34. * to a "more permissive" setting, which allows most architectures
  35. * to optimize this.
  36. */
  37. #define ptep_set_access_flags(__vma, __address, __ptep, __entry, __dirty) \
  38. do { \
  39. set_pte_at((__vma)->vm_mm, (__address), __ptep, __entry); \
  40. flush_tlb_page(__vma, __address); \
  41. } while (0)
  42. #endif
  43. #ifndef __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
  44. #define ptep_test_and_clear_young(__vma, __address, __ptep) \
  45. ({ \
  46. pte_t __pte = *(__ptep); \
  47. int r = 1; \
  48. if (!pte_young(__pte)) \
  49. r = 0; \
  50. else \
  51. set_pte_at((__vma)->vm_mm, (__address), \
  52. (__ptep), pte_mkold(__pte)); \
  53. r; \
  54. })
  55. #endif
  56. #ifndef __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
  57. #define ptep_clear_flush_young(__vma, __address, __ptep) \
  58. ({ \
  59. int __young; \
  60. __young = ptep_test_and_clear_young(__vma, __address, __ptep); \
  61. if (__young) \
  62. flush_tlb_page(__vma, __address); \
  63. __young; \
  64. })
  65. #endif
  66. #ifndef __HAVE_ARCH_PTEP_TEST_AND_CLEAR_DIRTY
  67. #define ptep_test_and_clear_dirty(__vma, __address, __ptep) \
  68. ({ \
  69. pte_t __pte = *__ptep; \
  70. int r = 1; \
  71. if (!pte_dirty(__pte)) \
  72. r = 0; \
  73. else \
  74. set_pte_at((__vma)->vm_mm, (__address), (__ptep), \
  75. pte_mkclean(__pte)); \
  76. r; \
  77. })
  78. #endif
  79. #ifndef __HAVE_ARCH_PTEP_CLEAR_DIRTY_FLUSH
  80. #define ptep_clear_flush_dirty(__vma, __address, __ptep) \
  81. ({ \
  82. int __dirty; \
  83. __dirty = ptep_test_and_clear_dirty(__vma, __address, __ptep); \
  84. if (__dirty) \
  85. flush_tlb_page(__vma, __address); \
  86. __dirty; \
  87. })
  88. #endif
  89. #ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR
  90. #define ptep_get_and_clear(__mm, __address, __ptep) \
  91. ({ \
  92. pte_t __pte = *(__ptep); \
  93. pte_clear((__mm), (__address), (__ptep)); \
  94. __pte; \
  95. })
  96. #endif
  97. #ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL
  98. #define ptep_get_and_clear_full(__mm, __address, __ptep, __full) \
  99. ({ \
  100. pte_t __pte; \
  101. __pte = ptep_get_and_clear((__mm), (__address), (__ptep)); \
  102. __pte; \
  103. })
  104. #endif
  105. #ifndef __HAVE_ARCH_PTE_CLEAR_FULL
  106. #define pte_clear_full(__mm, __address, __ptep, __full) \
  107. do { \
  108. pte_clear((__mm), (__address), (__ptep)); \
  109. } while (0)
  110. #endif
  111. #ifndef __HAVE_ARCH_PTEP_CLEAR_FLUSH
  112. #define ptep_clear_flush(__vma, __address, __ptep) \
  113. ({ \
  114. pte_t __pte; \
  115. __pte = ptep_get_and_clear((__vma)->vm_mm, __address, __ptep); \
  116. flush_tlb_page(__vma, __address); \
  117. __pte; \
  118. })
  119. #endif
  120. #ifndef __HAVE_ARCH_PTEP_SET_WRPROTECT
  121. struct mm_struct;
  122. static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long address, pte_t *ptep)
  123. {
  124. pte_t old_pte = *ptep;
  125. set_pte_at(mm, address, ptep, pte_wrprotect(old_pte));
  126. }
  127. #endif
  128. #ifndef __HAVE_ARCH_PTE_SAME
  129. #define pte_same(A,B) (pte_val(A) == pte_val(B))
  130. #endif
  131. #ifndef __HAVE_ARCH_PAGE_TEST_AND_CLEAR_DIRTY
  132. #define page_test_and_clear_dirty(page) (0)
  133. #define pte_maybe_dirty(pte) pte_dirty(pte)
  134. #else
  135. #define pte_maybe_dirty(pte) (1)
  136. #endif
  137. #ifndef __HAVE_ARCH_PAGE_TEST_AND_CLEAR_YOUNG
  138. #define page_test_and_clear_young(page) (0)
  139. #endif
  140. #ifndef __HAVE_ARCH_PGD_OFFSET_GATE
  141. #define pgd_offset_gate(mm, addr) pgd_offset(mm, addr)
  142. #endif
  143. #ifndef __HAVE_ARCH_LAZY_MMU_PROT_UPDATE
  144. #define lazy_mmu_prot_update(pte) do { } while (0)
  145. #endif
  146. #ifndef __HAVE_ARCH_MOVE_PTE
  147. #define move_pte(pte, prot, old_addr, new_addr) (pte)
  148. #endif
  149. /*
  150. * When walking page tables, get the address of the next boundary,
  151. * or the end address of the range if that comes earlier. Although no
  152. * vma end wraps to 0, rounded up __boundary may wrap to 0 throughout.
  153. */
  154. #define pgd_addr_end(addr, end) \
  155. ({ unsigned long __boundary = ((addr) + PGDIR_SIZE) & PGDIR_MASK; \
  156. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  157. })
  158. #ifndef pud_addr_end
  159. #define pud_addr_end(addr, end) \
  160. ({ unsigned long __boundary = ((addr) + PUD_SIZE) & PUD_MASK; \
  161. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  162. })
  163. #endif
  164. #ifndef pmd_addr_end
  165. #define pmd_addr_end(addr, end) \
  166. ({ unsigned long __boundary = ((addr) + PMD_SIZE) & PMD_MASK; \
  167. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  168. })
  169. #endif
  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 */