pgtable.h 12 KB

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