pgtable.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. #ifndef _ASM_GENERIC_PGTABLE_H
  2. #define _ASM_GENERIC_PGTABLE_H
  3. #ifndef __ASSEMBLY__
  4. #ifdef CONFIG_MMU
  5. #include <linux/mm_types.h>
  6. #ifndef __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
  7. extern int ptep_set_access_flags(struct vm_area_struct *vma,
  8. unsigned long address, pte_t *ptep,
  9. pte_t entry, int dirty);
  10. #endif
  11. #ifndef __HAVE_ARCH_PMDP_SET_ACCESS_FLAGS
  12. extern int pmdp_set_access_flags(struct vm_area_struct *vma,
  13. unsigned long address, pmd_t *pmdp,
  14. pmd_t entry, int dirty);
  15. #endif
  16. #ifndef __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
  17. static inline int ptep_test_and_clear_young(struct vm_area_struct *vma,
  18. unsigned long address,
  19. pte_t *ptep)
  20. {
  21. pte_t pte = *ptep;
  22. int r = 1;
  23. if (!pte_young(pte))
  24. r = 0;
  25. else
  26. set_pte_at(vma->vm_mm, address, ptep, pte_mkold(pte));
  27. return r;
  28. }
  29. #endif
  30. #ifndef __HAVE_ARCH_PMDP_TEST_AND_CLEAR_YOUNG
  31. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  32. static inline int pmdp_test_and_clear_young(struct vm_area_struct *vma,
  33. unsigned long address,
  34. pmd_t *pmdp)
  35. {
  36. pmd_t pmd = *pmdp;
  37. int r = 1;
  38. if (!pmd_young(pmd))
  39. r = 0;
  40. else
  41. set_pmd_at(vma->vm_mm, address, pmdp, pmd_mkold(pmd));
  42. return r;
  43. }
  44. #else /* CONFIG_TRANSPARENT_HUGEPAGE */
  45. static inline int pmdp_test_and_clear_young(struct vm_area_struct *vma,
  46. unsigned long address,
  47. pmd_t *pmdp)
  48. {
  49. BUG();
  50. return 0;
  51. }
  52. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  53. #endif
  54. #ifndef __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
  55. int ptep_clear_flush_young(struct vm_area_struct *vma,
  56. unsigned long address, pte_t *ptep);
  57. #endif
  58. #ifndef __HAVE_ARCH_PMDP_CLEAR_YOUNG_FLUSH
  59. int pmdp_clear_flush_young(struct vm_area_struct *vma,
  60. unsigned long address, pmd_t *pmdp);
  61. #endif
  62. #ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR
  63. static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
  64. unsigned long address,
  65. pte_t *ptep)
  66. {
  67. pte_t pte = *ptep;
  68. pte_clear(mm, address, ptep);
  69. return pte;
  70. }
  71. #endif
  72. #ifndef __HAVE_ARCH_PMDP_GET_AND_CLEAR
  73. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  74. static inline pmd_t pmdp_get_and_clear(struct mm_struct *mm,
  75. unsigned long address,
  76. pmd_t *pmdp)
  77. {
  78. pmd_t pmd = *pmdp;
  79. pmd_clear(mm, address, pmdp);
  80. return pmd;
  81. }
  82. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  83. #endif
  84. #ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL
  85. static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm,
  86. unsigned long address, pte_t *ptep,
  87. int full)
  88. {
  89. pte_t pte;
  90. pte = ptep_get_and_clear(mm, address, ptep);
  91. return pte;
  92. }
  93. #endif
  94. /*
  95. * Some architectures may be able to avoid expensive synchronization
  96. * primitives when modifications are made to PTE's which are already
  97. * not present, or in the process of an address space destruction.
  98. */
  99. #ifndef __HAVE_ARCH_PTE_CLEAR_NOT_PRESENT_FULL
  100. static inline void pte_clear_not_present_full(struct mm_struct *mm,
  101. unsigned long address,
  102. pte_t *ptep,
  103. int full)
  104. {
  105. pte_clear(mm, address, ptep);
  106. }
  107. #endif
  108. #ifndef __HAVE_ARCH_PTEP_CLEAR_FLUSH
  109. extern pte_t ptep_clear_flush(struct vm_area_struct *vma,
  110. unsigned long address,
  111. pte_t *ptep);
  112. #endif
  113. #ifndef __HAVE_ARCH_PMDP_CLEAR_FLUSH
  114. extern pmd_t pmdp_clear_flush(struct vm_area_struct *vma,
  115. unsigned long address,
  116. pmd_t *pmdp);
  117. #endif
  118. #ifndef __HAVE_ARCH_PTEP_SET_WRPROTECT
  119. struct mm_struct;
  120. static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long address, pte_t *ptep)
  121. {
  122. pte_t old_pte = *ptep;
  123. set_pte_at(mm, address, ptep, pte_wrprotect(old_pte));
  124. }
  125. #endif
  126. #ifndef __HAVE_ARCH_PMDP_SET_WRPROTECT
  127. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  128. static inline void pmdp_set_wrprotect(struct mm_struct *mm,
  129. unsigned long address, pmd_t *pmdp)
  130. {
  131. pmd_t old_pmd = *pmdp;
  132. set_pmd_at(mm, address, pmdp, pmd_wrprotect(old_pmd));
  133. }
  134. #else /* CONFIG_TRANSPARENT_HUGEPAGE */
  135. static inline void pmdp_set_wrprotect(struct mm_struct *mm,
  136. unsigned long address, pmd_t *pmdp)
  137. {
  138. BUG();
  139. }
  140. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  141. #endif
  142. #ifndef __HAVE_ARCH_PMDP_SPLITTING_FLUSH
  143. extern pmd_t pmdp_splitting_flush(struct vm_area_struct *vma,
  144. unsigned long address,
  145. pmd_t *pmdp);
  146. #endif
  147. #ifndef __HAVE_ARCH_PTE_SAME
  148. static inline int pte_same(pte_t pte_a, pte_t pte_b)
  149. {
  150. return pte_val(pte_a) == pte_val(pte_b);
  151. }
  152. #endif
  153. #ifndef __HAVE_ARCH_PMD_SAME
  154. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  155. static inline int pmd_same(pmd_t pmd_a, pmd_t pmd_b)
  156. {
  157. return pmd_val(pmd_a) == pmd_val(pmd_b);
  158. }
  159. #else /* CONFIG_TRANSPARENT_HUGEPAGE */
  160. static inline int pmd_same(pmd_t pmd_a, pmd_t pmd_b)
  161. {
  162. BUG();
  163. return 0;
  164. }
  165. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  166. #endif
  167. #ifndef __HAVE_ARCH_PAGE_TEST_AND_CLEAR_DIRTY
  168. #define page_test_and_clear_dirty(pfn, mapped) (0)
  169. #endif
  170. #ifndef __HAVE_ARCH_PAGE_TEST_AND_CLEAR_DIRTY
  171. #define pte_maybe_dirty(pte) pte_dirty(pte)
  172. #else
  173. #define pte_maybe_dirty(pte) (1)
  174. #endif
  175. #ifndef __HAVE_ARCH_PAGE_TEST_AND_CLEAR_YOUNG
  176. #define page_test_and_clear_young(pfn) (0)
  177. #endif
  178. #ifndef __HAVE_ARCH_PGD_OFFSET_GATE
  179. #define pgd_offset_gate(mm, addr) pgd_offset(mm, addr)
  180. #endif
  181. #ifndef __HAVE_ARCH_MOVE_PTE
  182. #define move_pte(pte, prot, old_addr, new_addr) (pte)
  183. #endif
  184. #ifndef flush_tlb_fix_spurious_fault
  185. #define flush_tlb_fix_spurious_fault(vma, address) flush_tlb_page(vma, address)
  186. #endif
  187. #ifndef pgprot_noncached
  188. #define pgprot_noncached(prot) (prot)
  189. #endif
  190. #ifndef pgprot_writecombine
  191. #define pgprot_writecombine pgprot_noncached
  192. #endif
  193. /*
  194. * When walking page tables, get the address of the next boundary,
  195. * or the end address of the range if that comes earlier. Although no
  196. * vma end wraps to 0, rounded up __boundary may wrap to 0 throughout.
  197. */
  198. #define pgd_addr_end(addr, end) \
  199. ({ unsigned long __boundary = ((addr) + PGDIR_SIZE) & PGDIR_MASK; \
  200. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  201. })
  202. #ifndef pud_addr_end
  203. #define pud_addr_end(addr, end) \
  204. ({ unsigned long __boundary = ((addr) + PUD_SIZE) & PUD_MASK; \
  205. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  206. })
  207. #endif
  208. #ifndef pmd_addr_end
  209. #define pmd_addr_end(addr, end) \
  210. ({ unsigned long __boundary = ((addr) + PMD_SIZE) & PMD_MASK; \
  211. (__boundary - 1 < (end) - 1)? __boundary: (end); \
  212. })
  213. #endif
  214. /*
  215. * When walking page tables, we usually want to skip any p?d_none entries;
  216. * and any p?d_bad entries - reporting the error before resetting to none.
  217. * Do the tests inline, but report and clear the bad entry in mm/memory.c.
  218. */
  219. void pgd_clear_bad(pgd_t *);
  220. void pud_clear_bad(pud_t *);
  221. void pmd_clear_bad(pmd_t *);
  222. static inline int pgd_none_or_clear_bad(pgd_t *pgd)
  223. {
  224. if (pgd_none(*pgd))
  225. return 1;
  226. if (unlikely(pgd_bad(*pgd))) {
  227. pgd_clear_bad(pgd);
  228. return 1;
  229. }
  230. return 0;
  231. }
  232. static inline int pud_none_or_clear_bad(pud_t *pud)
  233. {
  234. if (pud_none(*pud))
  235. return 1;
  236. if (unlikely(pud_bad(*pud))) {
  237. pud_clear_bad(pud);
  238. return 1;
  239. }
  240. return 0;
  241. }
  242. static inline int pmd_none_or_clear_bad(pmd_t *pmd)
  243. {
  244. if (pmd_none(*pmd))
  245. return 1;
  246. if (unlikely(pmd_bad(*pmd))) {
  247. pmd_clear_bad(pmd);
  248. return 1;
  249. }
  250. return 0;
  251. }
  252. static inline pte_t __ptep_modify_prot_start(struct mm_struct *mm,
  253. unsigned long addr,
  254. pte_t *ptep)
  255. {
  256. /*
  257. * Get the current pte state, but zero it out to make it
  258. * non-present, preventing the hardware from asynchronously
  259. * updating it.
  260. */
  261. return ptep_get_and_clear(mm, addr, ptep);
  262. }
  263. static inline void __ptep_modify_prot_commit(struct mm_struct *mm,
  264. unsigned long addr,
  265. pte_t *ptep, pte_t pte)
  266. {
  267. /*
  268. * The pte is non-present, so there's no hardware state to
  269. * preserve.
  270. */
  271. set_pte_at(mm, addr, ptep, pte);
  272. }
  273. #ifndef __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION
  274. /*
  275. * Start a pte protection read-modify-write transaction, which
  276. * protects against asynchronous hardware modifications to the pte.
  277. * The intention is not to prevent the hardware from making pte
  278. * updates, but to prevent any updates it may make from being lost.
  279. *
  280. * This does not protect against other software modifications of the
  281. * pte; the appropriate pte lock must be held over the transation.
  282. *
  283. * Note that this interface is intended to be batchable, meaning that
  284. * ptep_modify_prot_commit may not actually update the pte, but merely
  285. * queue the update to be done at some later time. The update must be
  286. * actually committed before the pte lock is released, however.
  287. */
  288. static inline pte_t ptep_modify_prot_start(struct mm_struct *mm,
  289. unsigned long addr,
  290. pte_t *ptep)
  291. {
  292. return __ptep_modify_prot_start(mm, addr, ptep);
  293. }
  294. /*
  295. * Commit an update to a pte, leaving any hardware-controlled bits in
  296. * the PTE unmodified.
  297. */
  298. static inline void ptep_modify_prot_commit(struct mm_struct *mm,
  299. unsigned long addr,
  300. pte_t *ptep, pte_t pte)
  301. {
  302. __ptep_modify_prot_commit(mm, addr, ptep, pte);
  303. }
  304. #endif /* __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION */
  305. #endif /* CONFIG_MMU */
  306. /*
  307. * A facility to provide lazy MMU batching. This allows PTE updates and
  308. * page invalidations to be delayed until a call to leave lazy MMU mode
  309. * is issued. Some architectures may benefit from doing this, and it is
  310. * beneficial for both shadow and direct mode hypervisors, which may batch
  311. * the PTE updates which happen during this window. Note that using this
  312. * interface requires that read hazards be removed from the code. A read
  313. * hazard could result in the direct mode hypervisor case, since the actual
  314. * write to the page tables may not yet have taken place, so reads though
  315. * a raw PTE pointer after it has been modified are not guaranteed to be
  316. * up to date. This mode can only be entered and left under the protection of
  317. * the page table locks for all page tables which may be modified. In the UP
  318. * case, this is required so that preemption is disabled, and in the SMP case,
  319. * it must synchronize the delayed page table writes properly on other CPUs.
  320. */
  321. #ifndef __HAVE_ARCH_ENTER_LAZY_MMU_MODE
  322. #define arch_enter_lazy_mmu_mode() do {} while (0)
  323. #define arch_leave_lazy_mmu_mode() do {} while (0)
  324. #define arch_flush_lazy_mmu_mode() do {} while (0)
  325. #endif
  326. /*
  327. * A facility to provide batching of the reload of page tables and
  328. * other process state with the actual context switch code for
  329. * paravirtualized guests. By convention, only one of the batched
  330. * update (lazy) modes (CPU, MMU) should be active at any given time,
  331. * entry should never be nested, and entry and exits should always be
  332. * paired. This is for sanity of maintaining and reasoning about the
  333. * kernel code. In this case, the exit (end of the context switch) is
  334. * in architecture-specific code, and so doesn't need a generic
  335. * definition.
  336. */
  337. #ifndef __HAVE_ARCH_START_CONTEXT_SWITCH
  338. #define arch_start_context_switch(prev) do {} while (0)
  339. #endif
  340. #ifndef __HAVE_PFNMAP_TRACKING
  341. /*
  342. * Interface that can be used by architecture code to keep track of
  343. * memory type of pfn mappings (remap_pfn_range, vm_insert_pfn)
  344. *
  345. * track_pfn_vma_new is called when a _new_ pfn mapping is being established
  346. * for physical range indicated by pfn and size.
  347. */
  348. static inline int track_pfn_vma_new(struct vm_area_struct *vma, pgprot_t *prot,
  349. unsigned long pfn, unsigned long size)
  350. {
  351. return 0;
  352. }
  353. /*
  354. * Interface that can be used by architecture code to keep track of
  355. * memory type of pfn mappings (remap_pfn_range, vm_insert_pfn)
  356. *
  357. * track_pfn_vma_copy is called when vma that is covering the pfnmap gets
  358. * copied through copy_page_range().
  359. */
  360. static inline int track_pfn_vma_copy(struct vm_area_struct *vma)
  361. {
  362. return 0;
  363. }
  364. /*
  365. * Interface that can be used by architecture code to keep track of
  366. * memory type of pfn mappings (remap_pfn_range, vm_insert_pfn)
  367. *
  368. * untrack_pfn_vma is called while unmapping a pfnmap for a region.
  369. * untrack can be called for a specific region indicated by pfn and size or
  370. * can be for the entire vma (in which case size can be zero).
  371. */
  372. static inline void untrack_pfn_vma(struct vm_area_struct *vma,
  373. unsigned long pfn, unsigned long size)
  374. {
  375. }
  376. #else
  377. extern int track_pfn_vma_new(struct vm_area_struct *vma, pgprot_t *prot,
  378. unsigned long pfn, unsigned long size);
  379. extern int track_pfn_vma_copy(struct vm_area_struct *vma);
  380. extern void untrack_pfn_vma(struct vm_area_struct *vma, unsigned long pfn,
  381. unsigned long size);
  382. #endif
  383. #ifndef CONFIG_TRANSPARENT_HUGEPAGE
  384. static inline int pmd_trans_huge(pmd_t pmd)
  385. {
  386. return 0;
  387. }
  388. static inline int pmd_trans_splitting(pmd_t pmd)
  389. {
  390. return 0;
  391. }
  392. #ifndef __HAVE_ARCH_PMD_WRITE
  393. static inline int pmd_write(pmd_t pmd)
  394. {
  395. BUG();
  396. return 0;
  397. }
  398. #endif /* __HAVE_ARCH_PMD_WRITE */
  399. #endif
  400. #endif /* !__ASSEMBLY__ */
  401. #endif /* _ASM_GENERIC_PGTABLE_H */