pgtable.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. /*
  106. * Some architectures may be able to avoid expensive synchronization
  107. * primitives when modifications are made to PTE's which are already
  108. * not present, or in the process of an address space destruction.
  109. */
  110. #ifndef __HAVE_ARCH_PTE_CLEAR_NOT_PRESENT_FULL
  111. #define pte_clear_not_present_full(__mm, __address, __ptep, __full) \
  112. do { \
  113. pte_clear((__mm), (__address), (__ptep)); \
  114. } while (0)
  115. #endif
  116. #ifndef __HAVE_ARCH_PTEP_CLEAR_FLUSH
  117. #define ptep_clear_flush(__vma, __address, __ptep) \
  118. ({ \
  119. pte_t __pte; \
  120. __pte = ptep_get_and_clear((__vma)->vm_mm, __address, __ptep); \
  121. flush_tlb_page(__vma, __address); \
  122. __pte; \
  123. })
  124. #endif
  125. #ifndef __HAVE_ARCH_PTEP_SET_WRPROTECT
  126. struct mm_struct;
  127. static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long address, pte_t *ptep)
  128. {
  129. pte_t old_pte = *ptep;
  130. set_pte_at(mm, address, ptep, pte_wrprotect(old_pte));
  131. }
  132. #endif
  133. #ifndef __HAVE_ARCH_PTE_SAME
  134. #define pte_same(A,B) (pte_val(A) == pte_val(B))
  135. #endif
  136. #ifndef __HAVE_ARCH_PAGE_TEST_AND_CLEAR_DIRTY
  137. #define page_test_and_clear_dirty(page) (0)
  138. #define pte_maybe_dirty(pte) pte_dirty(pte)
  139. #else
  140. #define pte_maybe_dirty(pte) (1)
  141. #endif
  142. #ifndef __HAVE_ARCH_PAGE_TEST_AND_CLEAR_YOUNG
  143. #define page_test_and_clear_young(page) (0)
  144. #endif
  145. #ifndef __HAVE_ARCH_PGD_OFFSET_GATE
  146. #define pgd_offset_gate(mm, addr) pgd_offset(mm, addr)
  147. #endif
  148. #ifndef __HAVE_ARCH_LAZY_MMU_PROT_UPDATE
  149. #define lazy_mmu_prot_update(pte) do { } while (0)
  150. #endif
  151. #ifndef __HAVE_ARCH_MOVE_PTE
  152. #define move_pte(pte, prot, old_addr, new_addr) (pte)
  153. #endif
  154. /*
  155. * When walking page tables, get the address of the next boundary,
  156. * or the end address of the range if that comes earlier. Although no
  157. * vma end wraps to 0, rounded up __boundary may wrap to 0 throughout.
  158. */
  159. #define pgd_addr_end(addr, end) \
  160. ({ unsigned long __boundary = ((addr) + PGDIR_SIZE) & PGDIR_MASK; \
  161. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  162. })
  163. #ifndef pud_addr_end
  164. #define pud_addr_end(addr, end) \
  165. ({ unsigned long __boundary = ((addr) + PUD_SIZE) & PUD_MASK; \
  166. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  167. })
  168. #endif
  169. #ifndef pmd_addr_end
  170. #define pmd_addr_end(addr, end) \
  171. ({ unsigned long __boundary = ((addr) + PMD_SIZE) & PMD_MASK; \
  172. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  173. })
  174. #endif
  175. /*
  176. * When walking page tables, we usually want to skip any p?d_none entries;
  177. * and any p?d_bad entries - reporting the error before resetting to none.
  178. * Do the tests inline, but report and clear the bad entry in mm/memory.c.
  179. */
  180. void pgd_clear_bad(pgd_t *);
  181. void pud_clear_bad(pud_t *);
  182. void pmd_clear_bad(pmd_t *);
  183. static inline int pgd_none_or_clear_bad(pgd_t *pgd)
  184. {
  185. if (pgd_none(*pgd))
  186. return 1;
  187. if (unlikely(pgd_bad(*pgd))) {
  188. pgd_clear_bad(pgd);
  189. return 1;
  190. }
  191. return 0;
  192. }
  193. static inline int pud_none_or_clear_bad(pud_t *pud)
  194. {
  195. if (pud_none(*pud))
  196. return 1;
  197. if (unlikely(pud_bad(*pud))) {
  198. pud_clear_bad(pud);
  199. return 1;
  200. }
  201. return 0;
  202. }
  203. static inline int pmd_none_or_clear_bad(pmd_t *pmd)
  204. {
  205. if (pmd_none(*pmd))
  206. return 1;
  207. if (unlikely(pmd_bad(*pmd))) {
  208. pmd_clear_bad(pmd);
  209. return 1;
  210. }
  211. return 0;
  212. }
  213. #endif /* !__ASSEMBLY__ */
  214. #endif /* _ASM_GENERIC_PGTABLE_H */