pgtable.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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, mapped) 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 flush_tlb_fix_spurious_fault
  116. #define flush_tlb_fix_spurious_fault(vma, address) flush_tlb_page(vma, address)
  117. #endif
  118. #ifndef pgprot_noncached
  119. #define pgprot_noncached(prot) (prot)
  120. #endif
  121. #ifndef pgprot_writecombine
  122. #define pgprot_writecombine pgprot_noncached
  123. #endif
  124. /*
  125. * When walking page tables, get the address of the next boundary,
  126. * or the end address of the range if that comes earlier. Although no
  127. * vma end wraps to 0, rounded up __boundary may wrap to 0 throughout.
  128. */
  129. #define pgd_addr_end(addr, end) \
  130. ({ unsigned long __boundary = ((addr) + PGDIR_SIZE) & PGDIR_MASK; \
  131. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  132. })
  133. #ifndef pud_addr_end
  134. #define pud_addr_end(addr, end) \
  135. ({ unsigned long __boundary = ((addr) + PUD_SIZE) & PUD_MASK; \
  136. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  137. })
  138. #endif
  139. #ifndef pmd_addr_end
  140. #define pmd_addr_end(addr, end) \
  141. ({ unsigned long __boundary = ((addr) + PMD_SIZE) & PMD_MASK; \
  142. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  143. })
  144. #endif
  145. /*
  146. * When walking page tables, we usually want to skip any p?d_none entries;
  147. * and any p?d_bad entries - reporting the error before resetting to none.
  148. * Do the tests inline, but report and clear the bad entry in mm/memory.c.
  149. */
  150. void pgd_clear_bad(pgd_t *);
  151. void pud_clear_bad(pud_t *);
  152. void pmd_clear_bad(pmd_t *);
  153. static inline int pgd_none_or_clear_bad(pgd_t *pgd)
  154. {
  155. if (pgd_none(*pgd))
  156. return 1;
  157. if (unlikely(pgd_bad(*pgd))) {
  158. pgd_clear_bad(pgd);
  159. return 1;
  160. }
  161. return 0;
  162. }
  163. static inline int pud_none_or_clear_bad(pud_t *pud)
  164. {
  165. if (pud_none(*pud))
  166. return 1;
  167. if (unlikely(pud_bad(*pud))) {
  168. pud_clear_bad(pud);
  169. return 1;
  170. }
  171. return 0;
  172. }
  173. static inline int pmd_none_or_clear_bad(pmd_t *pmd)
  174. {
  175. if (pmd_none(*pmd))
  176. return 1;
  177. if (unlikely(pmd_bad(*pmd))) {
  178. pmd_clear_bad(pmd);
  179. return 1;
  180. }
  181. return 0;
  182. }
  183. static inline pte_t __ptep_modify_prot_start(struct mm_struct *mm,
  184. unsigned long addr,
  185. pte_t *ptep)
  186. {
  187. /*
  188. * Get the current pte state, but zero it out to make it
  189. * non-present, preventing the hardware from asynchronously
  190. * updating it.
  191. */
  192. return ptep_get_and_clear(mm, addr, ptep);
  193. }
  194. static inline void __ptep_modify_prot_commit(struct mm_struct *mm,
  195. unsigned long addr,
  196. pte_t *ptep, pte_t pte)
  197. {
  198. /*
  199. * The pte is non-present, so there's no hardware state to
  200. * preserve.
  201. */
  202. set_pte_at(mm, addr, ptep, pte);
  203. }
  204. #ifndef __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION
  205. /*
  206. * Start a pte protection read-modify-write transaction, which
  207. * protects against asynchronous hardware modifications to the pte.
  208. * The intention is not to prevent the hardware from making pte
  209. * updates, but to prevent any updates it may make from being lost.
  210. *
  211. * This does not protect against other software modifications of the
  212. * pte; the appropriate pte lock must be held over the transation.
  213. *
  214. * Note that this interface is intended to be batchable, meaning that
  215. * ptep_modify_prot_commit may not actually update the pte, but merely
  216. * queue the update to be done at some later time. The update must be
  217. * actually committed before the pte lock is released, however.
  218. */
  219. static inline pte_t ptep_modify_prot_start(struct mm_struct *mm,
  220. unsigned long addr,
  221. pte_t *ptep)
  222. {
  223. return __ptep_modify_prot_start(mm, addr, ptep);
  224. }
  225. /*
  226. * Commit an update to a pte, leaving any hardware-controlled bits in
  227. * the PTE unmodified.
  228. */
  229. static inline void ptep_modify_prot_commit(struct mm_struct *mm,
  230. unsigned long addr,
  231. pte_t *ptep, pte_t pte)
  232. {
  233. __ptep_modify_prot_commit(mm, addr, ptep, pte);
  234. }
  235. #endif /* __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION */
  236. #endif /* CONFIG_MMU */
  237. /*
  238. * A facility to provide lazy MMU batching. This allows PTE updates and
  239. * page invalidations to be delayed until a call to leave lazy MMU mode
  240. * is issued. Some architectures may benefit from doing this, and it is
  241. * beneficial for both shadow and direct mode hypervisors, which may batch
  242. * the PTE updates which happen during this window. Note that using this
  243. * interface requires that read hazards be removed from the code. A read
  244. * hazard could result in the direct mode hypervisor case, since the actual
  245. * write to the page tables may not yet have taken place, so reads though
  246. * a raw PTE pointer after it has been modified are not guaranteed to be
  247. * up to date. This mode can only be entered and left under the protection of
  248. * the page table locks for all page tables which may be modified. In the UP
  249. * case, this is required so that preemption is disabled, and in the SMP case,
  250. * it must synchronize the delayed page table writes properly on other CPUs.
  251. */
  252. #ifndef __HAVE_ARCH_ENTER_LAZY_MMU_MODE
  253. #define arch_enter_lazy_mmu_mode() do {} while (0)
  254. #define arch_leave_lazy_mmu_mode() do {} while (0)
  255. #define arch_flush_lazy_mmu_mode() do {} while (0)
  256. #endif
  257. /*
  258. * A facility to provide batching of the reload of page tables and
  259. * other process state with the actual context switch code for
  260. * paravirtualized guests. By convention, only one of the batched
  261. * update (lazy) modes (CPU, MMU) should be active at any given time,
  262. * entry should never be nested, and entry and exits should always be
  263. * paired. This is for sanity of maintaining and reasoning about the
  264. * kernel code. In this case, the exit (end of the context switch) is
  265. * in architecture-specific code, and so doesn't need a generic
  266. * definition.
  267. */
  268. #ifndef __HAVE_ARCH_START_CONTEXT_SWITCH
  269. #define arch_start_context_switch(prev) do {} while (0)
  270. #endif
  271. #ifndef __HAVE_PFNMAP_TRACKING
  272. /*
  273. * Interface that can be used by architecture code to keep track of
  274. * memory type of pfn mappings (remap_pfn_range, vm_insert_pfn)
  275. *
  276. * track_pfn_vma_new is called when a _new_ pfn mapping is being established
  277. * for physical range indicated by pfn and size.
  278. */
  279. static inline int track_pfn_vma_new(struct vm_area_struct *vma, pgprot_t *prot,
  280. unsigned long pfn, unsigned long size)
  281. {
  282. return 0;
  283. }
  284. /*
  285. * Interface that can be used by architecture code to keep track of
  286. * memory type of pfn mappings (remap_pfn_range, vm_insert_pfn)
  287. *
  288. * track_pfn_vma_copy is called when vma that is covering the pfnmap gets
  289. * copied through copy_page_range().
  290. */
  291. static inline int track_pfn_vma_copy(struct vm_area_struct *vma)
  292. {
  293. return 0;
  294. }
  295. /*
  296. * Interface that can be used by architecture code to keep track of
  297. * memory type of pfn mappings (remap_pfn_range, vm_insert_pfn)
  298. *
  299. * untrack_pfn_vma is called while unmapping a pfnmap for a region.
  300. * untrack can be called for a specific region indicated by pfn and size or
  301. * can be for the entire vma (in which case size can be zero).
  302. */
  303. static inline void untrack_pfn_vma(struct vm_area_struct *vma,
  304. unsigned long pfn, unsigned long size)
  305. {
  306. }
  307. #else
  308. extern int track_pfn_vma_new(struct vm_area_struct *vma, pgprot_t *prot,
  309. unsigned long pfn, unsigned long size);
  310. extern int track_pfn_vma_copy(struct vm_area_struct *vma);
  311. extern void untrack_pfn_vma(struct vm_area_struct *vma, unsigned long pfn,
  312. unsigned long size);
  313. #endif
  314. #endif /* !__ASSEMBLY__ */
  315. #endif /* _ASM_GENERIC_PGTABLE_H */