pgtable.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #ifndef _ASM_GENERIC_PGTABLE_H
  2. #define _ASM_GENERIC_PGTABLE_H
  3. #ifndef __ASSEMBLY__
  4. #ifndef __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
  5. /*
  6. * Largely same as above, but only sets the access flags (dirty,
  7. * accessed, and writable). Furthermore, we know it always gets set
  8. * to a "more permissive" setting, which allows most architectures
  9. * to optimize this. We return whether the PTE actually changed, which
  10. * in turn instructs the caller to do things like update__mmu_cache.
  11. * This used to be done in the caller, but sparc needs minor faults to
  12. * force that call on sun4c so we changed this macro slightly
  13. */
  14. #define ptep_set_access_flags(__vma, __address, __ptep, __entry, __dirty) \
  15. ({ \
  16. int __changed = !pte_same(*(__ptep), __entry); \
  17. if (__changed) { \
  18. set_pte_at((__vma)->vm_mm, (__address), __ptep, __entry); \
  19. flush_tlb_page(__vma, __address); \
  20. } \
  21. __changed; \
  22. })
  23. #endif
  24. #ifndef __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
  25. #define ptep_test_and_clear_young(__vma, __address, __ptep) \
  26. ({ \
  27. pte_t __pte = *(__ptep); \
  28. int r = 1; \
  29. if (!pte_young(__pte)) \
  30. r = 0; \
  31. else \
  32. set_pte_at((__vma)->vm_mm, (__address), \
  33. (__ptep), pte_mkold(__pte)); \
  34. r; \
  35. })
  36. #endif
  37. #ifndef __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
  38. #define ptep_clear_flush_young(__vma, __address, __ptep) \
  39. ({ \
  40. int __young; \
  41. __young = ptep_test_and_clear_young(__vma, __address, __ptep); \
  42. if (__young) \
  43. flush_tlb_page(__vma, __address); \
  44. __young; \
  45. })
  46. #endif
  47. #ifndef __HAVE_ARCH_PTEP_TEST_AND_CLEAR_DIRTY
  48. #define ptep_test_and_clear_dirty(__vma, __address, __ptep) \
  49. ({ \
  50. pte_t __pte = *__ptep; \
  51. int r = 1; \
  52. if (!pte_dirty(__pte)) \
  53. r = 0; \
  54. else \
  55. set_pte_at((__vma)->vm_mm, (__address), (__ptep), \
  56. pte_mkclean(__pte)); \
  57. r; \
  58. })
  59. #endif
  60. #ifndef __HAVE_ARCH_PTEP_CLEAR_DIRTY_FLUSH
  61. #define ptep_clear_flush_dirty(__vma, __address, __ptep) \
  62. ({ \
  63. int __dirty; \
  64. __dirty = ptep_test_and_clear_dirty(__vma, __address, __ptep); \
  65. if (__dirty) \
  66. flush_tlb_page(__vma, __address); \
  67. __dirty; \
  68. })
  69. #endif
  70. #ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR
  71. #define ptep_get_and_clear(__mm, __address, __ptep) \
  72. ({ \
  73. pte_t __pte = *(__ptep); \
  74. pte_clear((__mm), (__address), (__ptep)); \
  75. __pte; \
  76. })
  77. #endif
  78. #ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL
  79. #define ptep_get_and_clear_full(__mm, __address, __ptep, __full) \
  80. ({ \
  81. pte_t __pte; \
  82. __pte = ptep_get_and_clear((__mm), (__address), (__ptep)); \
  83. __pte; \
  84. })
  85. #endif
  86. /*
  87. * Some architectures may be able to avoid expensive synchronization
  88. * primitives when modifications are made to PTE's which are already
  89. * not present, or in the process of an address space destruction.
  90. */
  91. #ifndef __HAVE_ARCH_PTE_CLEAR_NOT_PRESENT_FULL
  92. #define pte_clear_not_present_full(__mm, __address, __ptep, __full) \
  93. do { \
  94. pte_clear((__mm), (__address), (__ptep)); \
  95. } while (0)
  96. #endif
  97. #ifndef __HAVE_ARCH_PTEP_CLEAR_FLUSH
  98. #define ptep_clear_flush(__vma, __address, __ptep) \
  99. ({ \
  100. pte_t __pte; \
  101. __pte = ptep_get_and_clear((__vma)->vm_mm, __address, __ptep); \
  102. flush_tlb_page(__vma, __address); \
  103. __pte; \
  104. })
  105. #endif
  106. #ifndef __HAVE_ARCH_PTEP_SET_WRPROTECT
  107. struct mm_struct;
  108. static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long address, pte_t *ptep)
  109. {
  110. pte_t old_pte = *ptep;
  111. set_pte_at(mm, address, ptep, pte_wrprotect(old_pte));
  112. }
  113. #endif
  114. #ifndef __HAVE_ARCH_PTE_SAME
  115. #define pte_same(A,B) (pte_val(A) == pte_val(B))
  116. #endif
  117. #ifndef __HAVE_ARCH_PAGE_TEST_DIRTY
  118. #define page_test_dirty(page) (0)
  119. #endif
  120. #ifndef __HAVE_ARCH_PAGE_CLEAR_DIRTY
  121. #define page_clear_dirty(page) do { } while (0)
  122. #endif
  123. #ifndef __HAVE_ARCH_PAGE_TEST_DIRTY
  124. #define pte_maybe_dirty(pte) pte_dirty(pte)
  125. #else
  126. #define pte_maybe_dirty(pte) (1)
  127. #endif
  128. #ifndef __HAVE_ARCH_PAGE_TEST_AND_CLEAR_YOUNG
  129. #define page_test_and_clear_young(page) (0)
  130. #endif
  131. #ifndef __HAVE_ARCH_PGD_OFFSET_GATE
  132. #define pgd_offset_gate(mm, addr) pgd_offset(mm, addr)
  133. #endif
  134. #ifndef __HAVE_ARCH_LAZY_MMU_PROT_UPDATE
  135. #define lazy_mmu_prot_update(pte) do { } while (0)
  136. #endif
  137. #ifndef __HAVE_ARCH_MOVE_PTE
  138. #define move_pte(pte, prot, old_addr, new_addr) (pte)
  139. #endif
  140. /*
  141. * A facility to provide lazy MMU batching. This allows PTE updates and
  142. * page invalidations to be delayed until a call to leave lazy MMU mode
  143. * is issued. Some architectures may benefit from doing this, and it is
  144. * beneficial for both shadow and direct mode hypervisors, which may batch
  145. * the PTE updates which happen during this window. Note that using this
  146. * interface requires that read hazards be removed from the code. A read
  147. * hazard could result in the direct mode hypervisor case, since the actual
  148. * write to the page tables may not yet have taken place, so reads though
  149. * a raw PTE pointer after it has been modified are not guaranteed to be
  150. * up to date. This mode can only be entered and left under the protection of
  151. * the page table locks for all page tables which may be modified. In the UP
  152. * case, this is required so that preemption is disabled, and in the SMP case,
  153. * it must synchronize the delayed page table writes properly on other CPUs.
  154. */
  155. #ifndef __HAVE_ARCH_ENTER_LAZY_MMU_MODE
  156. #define arch_enter_lazy_mmu_mode() do {} while (0)
  157. #define arch_leave_lazy_mmu_mode() do {} while (0)
  158. #define arch_flush_lazy_mmu_mode() do {} while (0)
  159. #endif
  160. /*
  161. * A facility to provide batching of the reload of page tables with the
  162. * actual context switch code for paravirtualized guests. By convention,
  163. * only one of the lazy modes (CPU, MMU) should be active at any given
  164. * time, entry should never be nested, and entry and exits should always
  165. * be paired. This is for sanity of maintaining and reasoning about the
  166. * kernel code.
  167. */
  168. #ifndef __HAVE_ARCH_ENTER_LAZY_CPU_MODE
  169. #define arch_enter_lazy_cpu_mode() do {} while (0)
  170. #define arch_leave_lazy_cpu_mode() do {} while (0)
  171. #define arch_flush_lazy_cpu_mode() do {} while (0)
  172. #endif
  173. /*
  174. * When walking page tables, get the address of the next boundary,
  175. * or the end address of the range if that comes earlier. Although no
  176. * vma end wraps to 0, rounded up __boundary may wrap to 0 throughout.
  177. */
  178. #define pgd_addr_end(addr, end) \
  179. ({ unsigned long __boundary = ((addr) + PGDIR_SIZE) & PGDIR_MASK; \
  180. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  181. })
  182. #ifndef pud_addr_end
  183. #define pud_addr_end(addr, end) \
  184. ({ unsigned long __boundary = ((addr) + PUD_SIZE) & PUD_MASK; \
  185. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  186. })
  187. #endif
  188. #ifndef pmd_addr_end
  189. #define pmd_addr_end(addr, end) \
  190. ({ unsigned long __boundary = ((addr) + PMD_SIZE) & PMD_MASK; \
  191. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  192. })
  193. #endif
  194. /*
  195. * When walking page tables, we usually want to skip any p?d_none entries;
  196. * and any p?d_bad entries - reporting the error before resetting to none.
  197. * Do the tests inline, but report and clear the bad entry in mm/memory.c.
  198. */
  199. void pgd_clear_bad(pgd_t *);
  200. void pud_clear_bad(pud_t *);
  201. void pmd_clear_bad(pmd_t *);
  202. static inline int pgd_none_or_clear_bad(pgd_t *pgd)
  203. {
  204. if (pgd_none(*pgd))
  205. return 1;
  206. if (unlikely(pgd_bad(*pgd))) {
  207. pgd_clear_bad(pgd);
  208. return 1;
  209. }
  210. return 0;
  211. }
  212. static inline int pud_none_or_clear_bad(pud_t *pud)
  213. {
  214. if (pud_none(*pud))
  215. return 1;
  216. if (unlikely(pud_bad(*pud))) {
  217. pud_clear_bad(pud);
  218. return 1;
  219. }
  220. return 0;
  221. }
  222. static inline int pmd_none_or_clear_bad(pmd_t *pmd)
  223. {
  224. if (pmd_none(*pmd))
  225. return 1;
  226. if (unlikely(pmd_bad(*pmd))) {
  227. pmd_clear_bad(pmd);
  228. return 1;
  229. }
  230. return 0;
  231. }
  232. #endif /* !__ASSEMBLY__ */
  233. #endif /* _ASM_GENERIC_PGTABLE_H */