pgtable.h 7.7 KB

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