pgtable.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. #ifndef _ASM_GENERIC_PGTABLE_H
  2. #define _ASM_GENERIC_PGTABLE_H
  3. #ifndef __ASSEMBLY__
  4. #ifdef CONFIG_MMU
  5. #ifndef __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
  6. /*
  7. * Largely same as above, but only sets the access flags (dirty,
  8. * accessed, and writable). Furthermore, we know it always gets set
  9. * to a "more permissive" setting, which allows most architectures
  10. * to optimize this. We return whether the PTE actually changed, which
  11. * in turn instructs the caller to do things like update__mmu_cache.
  12. * This used to be done in the caller, but sparc needs minor faults to
  13. * force that call on sun4c so we changed this macro slightly
  14. */
  15. #define ptep_set_access_flags(__vma, __address, __ptep, __entry, __dirty) \
  16. ({ \
  17. int __changed = !pte_same(*(__ptep), __entry); \
  18. if (__changed) { \
  19. set_pte_at((__vma)->vm_mm, (__address), __ptep, __entry); \
  20. flush_tlb_page(__vma, __address); \
  21. } \
  22. __changed; \
  23. })
  24. #endif
  25. #ifndef __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
  26. #define ptep_test_and_clear_young(__vma, __address, __ptep) \
  27. ({ \
  28. pte_t __pte = *(__ptep); \
  29. int r = 1; \
  30. if (!pte_young(__pte)) \
  31. r = 0; \
  32. else \
  33. set_pte_at((__vma)->vm_mm, (__address), \
  34. (__ptep), pte_mkold(__pte)); \
  35. r; \
  36. })
  37. #endif
  38. #ifndef __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
  39. #define ptep_clear_flush_young(__vma, __address, __ptep) \
  40. ({ \
  41. int __young; \
  42. __young = ptep_test_and_clear_young(__vma, __address, __ptep); \
  43. if (__young) \
  44. flush_tlb_page(__vma, __address); \
  45. __young; \
  46. })
  47. #endif
  48. #ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR
  49. #define ptep_get_and_clear(__mm, __address, __ptep) \
  50. ({ \
  51. pte_t __pte = *(__ptep); \
  52. pte_clear((__mm), (__address), (__ptep)); \
  53. __pte; \
  54. })
  55. #endif
  56. #ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL
  57. #define ptep_get_and_clear_full(__mm, __address, __ptep, __full) \
  58. ({ \
  59. pte_t __pte; \
  60. __pte = ptep_get_and_clear((__mm), (__address), (__ptep)); \
  61. __pte; \
  62. })
  63. #endif
  64. /*
  65. * Some architectures may be able to avoid expensive synchronization
  66. * primitives when modifications are made to PTE's which are already
  67. * not present, or in the process of an address space destruction.
  68. */
  69. #ifndef __HAVE_ARCH_PTE_CLEAR_NOT_PRESENT_FULL
  70. #define pte_clear_not_present_full(__mm, __address, __ptep, __full) \
  71. do { \
  72. pte_clear((__mm), (__address), (__ptep)); \
  73. } while (0)
  74. #endif
  75. #ifndef __HAVE_ARCH_PTEP_CLEAR_FLUSH
  76. #define ptep_clear_flush(__vma, __address, __ptep) \
  77. ({ \
  78. pte_t __pte; \
  79. __pte = ptep_get_and_clear((__vma)->vm_mm, __address, __ptep); \
  80. flush_tlb_page(__vma, __address); \
  81. __pte; \
  82. })
  83. #endif
  84. #ifndef __HAVE_ARCH_PTEP_SET_WRPROTECT
  85. struct mm_struct;
  86. static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long address, pte_t *ptep)
  87. {
  88. pte_t old_pte = *ptep;
  89. set_pte_at(mm, address, ptep, pte_wrprotect(old_pte));
  90. }
  91. #endif
  92. #ifndef __HAVE_ARCH_PTE_SAME
  93. #define pte_same(A,B) (pte_val(A) == pte_val(B))
  94. #endif
  95. #ifndef __HAVE_ARCH_PAGE_TEST_DIRTY
  96. #define page_test_dirty(page) (0)
  97. #endif
  98. #ifndef __HAVE_ARCH_PAGE_CLEAR_DIRTY
  99. #define page_clear_dirty(page) do { } while (0)
  100. #endif
  101. #ifndef __HAVE_ARCH_PAGE_TEST_DIRTY
  102. #define pte_maybe_dirty(pte) pte_dirty(pte)
  103. #else
  104. #define pte_maybe_dirty(pte) (1)
  105. #endif
  106. #ifndef __HAVE_ARCH_PAGE_TEST_AND_CLEAR_YOUNG
  107. #define page_test_and_clear_young(page) (0)
  108. #endif
  109. #ifndef __HAVE_ARCH_PGD_OFFSET_GATE
  110. #define pgd_offset_gate(mm, addr) pgd_offset(mm, addr)
  111. #endif
  112. #ifndef __HAVE_ARCH_MOVE_PTE
  113. #define move_pte(pte, prot, old_addr, new_addr) (pte)
  114. #endif
  115. #ifndef pgprot_noncached
  116. #define pgprot_noncached(prot) (prot)
  117. #endif
  118. #ifndef pgprot_writecombine
  119. #define pgprot_writecombine pgprot_noncached
  120. #endif
  121. /*
  122. * When walking page tables, get the address of the next boundary,
  123. * or the end address of the range if that comes earlier. Although no
  124. * vma end wraps to 0, rounded up __boundary may wrap to 0 throughout.
  125. */
  126. #define pgd_addr_end(addr, end) \
  127. ({ unsigned long __boundary = ((addr) + PGDIR_SIZE) & PGDIR_MASK; \
  128. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  129. })
  130. #ifndef pud_addr_end
  131. #define pud_addr_end(addr, end) \
  132. ({ unsigned long __boundary = ((addr) + PUD_SIZE) & PUD_MASK; \
  133. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  134. })
  135. #endif
  136. #ifndef pmd_addr_end
  137. #define pmd_addr_end(addr, end) \
  138. ({ unsigned long __boundary = ((addr) + PMD_SIZE) & PMD_MASK; \
  139. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  140. })
  141. #endif
  142. /*
  143. * When walking page tables, we usually want to skip any p?d_none entries;
  144. * and any p?d_bad entries - reporting the error before resetting to none.
  145. * Do the tests inline, but report and clear the bad entry in mm/memory.c.
  146. */
  147. void pgd_clear_bad(pgd_t *);
  148. void pud_clear_bad(pud_t *);
  149. void pmd_clear_bad(pmd_t *);
  150. static inline int pgd_none_or_clear_bad(pgd_t *pgd)
  151. {
  152. if (pgd_none(*pgd))
  153. return 1;
  154. if (unlikely(pgd_bad(*pgd))) {
  155. pgd_clear_bad(pgd);
  156. return 1;
  157. }
  158. return 0;
  159. }
  160. static inline int pud_none_or_clear_bad(pud_t *pud)
  161. {
  162. if (pud_none(*pud))
  163. return 1;
  164. if (unlikely(pud_bad(*pud))) {
  165. pud_clear_bad(pud);
  166. return 1;
  167. }
  168. return 0;
  169. }
  170. static inline int pmd_none_or_clear_bad(pmd_t *pmd)
  171. {
  172. if (pmd_none(*pmd))
  173. return 1;
  174. if (unlikely(pmd_bad(*pmd))) {
  175. pmd_clear_bad(pmd);
  176. return 1;
  177. }
  178. return 0;
  179. }
  180. static inline pte_t __ptep_modify_prot_start(struct mm_struct *mm,
  181. unsigned long addr,
  182. pte_t *ptep)
  183. {
  184. /*
  185. * Get the current pte state, but zero it out to make it
  186. * non-present, preventing the hardware from asynchronously
  187. * updating it.
  188. */
  189. return ptep_get_and_clear(mm, addr, ptep);
  190. }
  191. static inline void __ptep_modify_prot_commit(struct mm_struct *mm,
  192. unsigned long addr,
  193. pte_t *ptep, pte_t pte)
  194. {
  195. /*
  196. * The pte is non-present, so there's no hardware state to
  197. * preserve.
  198. */
  199. set_pte_at(mm, addr, ptep, pte);
  200. }
  201. #ifndef __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION
  202. /*
  203. * Start a pte protection read-modify-write transaction, which
  204. * protects against asynchronous hardware modifications to the pte.
  205. * The intention is not to prevent the hardware from making pte
  206. * updates, but to prevent any updates it may make from being lost.
  207. *
  208. * This does not protect against other software modifications of the
  209. * pte; the appropriate pte lock must be held over the transation.
  210. *
  211. * Note that this interface is intended to be batchable, meaning that
  212. * ptep_modify_prot_commit may not actually update the pte, but merely
  213. * queue the update to be done at some later time. The update must be
  214. * actually committed before the pte lock is released, however.
  215. */
  216. static inline pte_t ptep_modify_prot_start(struct mm_struct *mm,
  217. unsigned long addr,
  218. pte_t *ptep)
  219. {
  220. return __ptep_modify_prot_start(mm, addr, ptep);
  221. }
  222. /*
  223. * Commit an update to a pte, leaving any hardware-controlled bits in
  224. * the PTE unmodified.
  225. */
  226. static inline void ptep_modify_prot_commit(struct mm_struct *mm,
  227. unsigned long addr,
  228. pte_t *ptep, pte_t pte)
  229. {
  230. __ptep_modify_prot_commit(mm, addr, ptep, pte);
  231. }
  232. #endif /* __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION */
  233. #endif /* CONFIG_MMU */
  234. /*
  235. * A facility to provide lazy MMU batching. This allows PTE updates and
  236. * page invalidations to be delayed until a call to leave lazy MMU mode
  237. * is issued. Some architectures may benefit from doing this, and it is
  238. * beneficial for both shadow and direct mode hypervisors, which may batch
  239. * the PTE updates which happen during this window. Note that using this
  240. * interface requires that read hazards be removed from the code. A read
  241. * hazard could result in the direct mode hypervisor case, since the actual
  242. * write to the page tables may not yet have taken place, so reads though
  243. * a raw PTE pointer after it has been modified are not guaranteed to be
  244. * up to date. This mode can only be entered and left under the protection of
  245. * the page table locks for all page tables which may be modified. In the UP
  246. * case, this is required so that preemption is disabled, and in the SMP case,
  247. * it must synchronize the delayed page table writes properly on other CPUs.
  248. */
  249. #ifndef __HAVE_ARCH_ENTER_LAZY_MMU_MODE
  250. #define arch_enter_lazy_mmu_mode() do {} while (0)
  251. #define arch_leave_lazy_mmu_mode() do {} while (0)
  252. #define arch_flush_lazy_mmu_mode() do {} while (0)
  253. #endif
  254. /*
  255. * A facility to provide batching of the reload of page tables and
  256. * other process state with the actual context switch code for
  257. * paravirtualized guests. By convention, only one of the batched
  258. * update (lazy) modes (CPU, MMU) should be active at any given time,
  259. * entry should never be nested, and entry and exits should always be
  260. * paired. This is for sanity of maintaining and reasoning about the
  261. * kernel code. In this case, the exit (end of the context switch) is
  262. * in architecture-specific code, and so doesn't need a generic
  263. * definition.
  264. */
  265. #ifndef __HAVE_ARCH_START_CONTEXT_SWITCH
  266. #define arch_start_context_switch(prev) do {} while (0)
  267. #endif
  268. #ifndef __HAVE_PFNMAP_TRACKING
  269. /*
  270. * Interface that can be used by architecture code to keep track of
  271. * memory type of pfn mappings (remap_pfn_range, vm_insert_pfn)
  272. *
  273. * track_pfn_vma_new is called when a _new_ pfn mapping is being established
  274. * for physical range indicated by pfn and size.
  275. */
  276. static inline int track_pfn_vma_new(struct vm_area_struct *vma, pgprot_t *prot,
  277. unsigned long pfn, unsigned long size)
  278. {
  279. return 0;
  280. }
  281. /*
  282. * Interface that can be used by architecture code to keep track of
  283. * memory type of pfn mappings (remap_pfn_range, vm_insert_pfn)
  284. *
  285. * track_pfn_vma_copy is called when vma that is covering the pfnmap gets
  286. * copied through copy_page_range().
  287. */
  288. static inline int track_pfn_vma_copy(struct vm_area_struct *vma)
  289. {
  290. return 0;
  291. }
  292. /*
  293. * Interface that can be used by architecture code to keep track of
  294. * memory type of pfn mappings (remap_pfn_range, vm_insert_pfn)
  295. *
  296. * untrack_pfn_vma is called while unmapping a pfnmap for a region.
  297. * untrack can be called for a specific region indicated by pfn and size or
  298. * can be for the entire vma (in which case size can be zero).
  299. */
  300. static inline void untrack_pfn_vma(struct vm_area_struct *vma,
  301. unsigned long pfn, unsigned long size)
  302. {
  303. }
  304. #else
  305. extern int track_pfn_vma_new(struct vm_area_struct *vma, pgprot_t *prot,
  306. unsigned long pfn, unsigned long size);
  307. extern int track_pfn_vma_copy(struct vm_area_struct *vma);
  308. extern void untrack_pfn_vma(struct vm_area_struct *vma, unsigned long pfn,
  309. unsigned long size);
  310. #endif
  311. #endif /* !__ASSEMBLY__ */
  312. #endif /* _ASM_GENERIC_PGTABLE_H */