pgtable.h 10 KB

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