pgtable-3level.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_X86_PGTABLE_3LEVEL_H
  3. #define _ASM_X86_PGTABLE_3LEVEL_H
  4. #include <asm/atomic64_32.h>
  5. /*
  6. * Intel Physical Address Extension (PAE) Mode - three-level page
  7. * tables on PPro+ CPUs.
  8. *
  9. * Copyright (C) 1999 Ingo Molnar <[email protected]>
  10. */
  11. #define pte_ERROR(e) \
  12. pr_err("%s:%d: bad pte %p(%08lx%08lx)\n", \
  13. __FILE__, __LINE__, &(e), (e).pte_high, (e).pte_low)
  14. #define pmd_ERROR(e) \
  15. pr_err("%s:%d: bad pmd %p(%016Lx)\n", \
  16. __FILE__, __LINE__, &(e), pmd_val(e))
  17. #define pgd_ERROR(e) \
  18. pr_err("%s:%d: bad pgd %p(%016Lx)\n", \
  19. __FILE__, __LINE__, &(e), pgd_val(e))
  20. /* Rules for using set_pte: the pte being assigned *must* be
  21. * either not present or in a state where the hardware will
  22. * not attempt to update the pte. In places where this is
  23. * not possible, use pte_get_and_clear to obtain the old pte
  24. * value and then use set_pte to update it. -ben
  25. */
  26. static inline void native_set_pte(pte_t *ptep, pte_t pte)
  27. {
  28. ptep->pte_high = pte.pte_high;
  29. smp_wmb();
  30. ptep->pte_low = pte.pte_low;
  31. }
  32. #define pmd_read_atomic pmd_read_atomic
  33. /*
  34. * pte_offset_map_lock() on 32-bit PAE kernels was reading the pmd_t with
  35. * a "*pmdp" dereference done by GCC. Problem is, in certain places
  36. * where pte_offset_map_lock() is called, concurrent page faults are
  37. * allowed, if the mmap_lock is hold for reading. An example is mincore
  38. * vs page faults vs MADV_DONTNEED. On the page fault side
  39. * pmd_populate() rightfully does a set_64bit(), but if we're reading the
  40. * pmd_t with a "*pmdp" on the mincore side, a SMP race can happen
  41. * because GCC will not read the 64-bit value of the pmd atomically.
  42. *
  43. * To fix this all places running pte_offset_map_lock() while holding the
  44. * mmap_lock in read mode, shall read the pmdp pointer using this
  45. * function to know if the pmd is null or not, and in turn to know if
  46. * they can run pte_offset_map_lock() or pmd_trans_huge() or other pmd
  47. * operations.
  48. *
  49. * Without THP if the mmap_lock is held for reading, the pmd can only
  50. * transition from null to not null while pmd_read_atomic() runs. So
  51. * we can always return atomic pmd values with this function.
  52. *
  53. * With THP if the mmap_lock is held for reading, the pmd can become
  54. * trans_huge or none or point to a pte (and in turn become "stable")
  55. * at any time under pmd_read_atomic(). We could read it truly
  56. * atomically here with an atomic64_read() for the THP enabled case (and
  57. * it would be a whole lot simpler), but to avoid using cmpxchg8b we
  58. * only return an atomic pmdval if the low part of the pmdval is later
  59. * found to be stable (i.e. pointing to a pte). We are also returning a
  60. * 'none' (zero) pmdval if the low part of the pmd is zero.
  61. *
  62. * In some cases the high and low part of the pmdval returned may not be
  63. * consistent if THP is enabled (the low part may point to previously
  64. * mapped hugepage, while the high part may point to a more recently
  65. * mapped hugepage), but pmd_none_or_trans_huge_or_clear_bad() only
  66. * needs the low part of the pmd to be read atomically to decide if the
  67. * pmd is unstable or not, with the only exception when the low part
  68. * of the pmd is zero, in which case we return a 'none' pmd.
  69. */
  70. static inline pmd_t pmd_read_atomic(pmd_t *pmdp)
  71. {
  72. pmdval_t ret;
  73. u32 *tmp = (u32 *)pmdp;
  74. ret = (pmdval_t) (*tmp);
  75. if (ret) {
  76. /*
  77. * If the low part is null, we must not read the high part
  78. * or we can end up with a partial pmd.
  79. */
  80. smp_rmb();
  81. ret |= ((pmdval_t)*(tmp + 1)) << 32;
  82. }
  83. return (pmd_t) { ret };
  84. }
  85. static inline void native_set_pte_atomic(pte_t *ptep, pte_t pte)
  86. {
  87. set_64bit((unsigned long long *)(ptep), native_pte_val(pte));
  88. }
  89. static inline void native_set_pmd(pmd_t *pmdp, pmd_t pmd)
  90. {
  91. set_64bit((unsigned long long *)(pmdp), native_pmd_val(pmd));
  92. }
  93. static inline void native_set_pud(pud_t *pudp, pud_t pud)
  94. {
  95. #ifdef CONFIG_PAGE_TABLE_ISOLATION
  96. pud.p4d.pgd = pti_set_user_pgtbl(&pudp->p4d.pgd, pud.p4d.pgd);
  97. #endif
  98. set_64bit((unsigned long long *)(pudp), native_pud_val(pud));
  99. }
  100. /*
  101. * For PTEs and PDEs, we must clear the P-bit first when clearing a page table
  102. * entry, so clear the bottom half first and enforce ordering with a compiler
  103. * barrier.
  104. */
  105. static inline void native_pte_clear(struct mm_struct *mm, unsigned long addr,
  106. pte_t *ptep)
  107. {
  108. ptep->pte_low = 0;
  109. smp_wmb();
  110. ptep->pte_high = 0;
  111. }
  112. static inline void native_pmd_clear(pmd_t *pmd)
  113. {
  114. u32 *tmp = (u32 *)pmd;
  115. *tmp = 0;
  116. smp_wmb();
  117. *(tmp + 1) = 0;
  118. }
  119. static inline void native_pud_clear(pud_t *pudp)
  120. {
  121. }
  122. static inline void pud_clear(pud_t *pudp)
  123. {
  124. set_pud(pudp, __pud(0));
  125. /*
  126. * According to Intel App note "TLBs, Paging-Structure Caches,
  127. * and Their Invalidation", April 2007, document 317080-001,
  128. * section 8.1: in PAE mode we explicitly have to flush the
  129. * TLB via cr3 if the top-level pgd is changed...
  130. *
  131. * Currently all places where pud_clear() is called either have
  132. * flush_tlb_mm() followed or don't need TLB flush (x86_64 code or
  133. * pud_clear_bad()), so we don't need TLB flush here.
  134. */
  135. }
  136. #ifdef CONFIG_SMP
  137. static inline pte_t native_ptep_get_and_clear(pte_t *ptep)
  138. {
  139. pte_t res;
  140. res.pte = (pteval_t)arch_atomic64_xchg((atomic64_t *)ptep, 0);
  141. return res;
  142. }
  143. #else
  144. #define native_ptep_get_and_clear(xp) native_local_ptep_get_and_clear(xp)
  145. #endif
  146. union split_pmd {
  147. struct {
  148. u32 pmd_low;
  149. u32 pmd_high;
  150. };
  151. pmd_t pmd;
  152. };
  153. #ifdef CONFIG_SMP
  154. static inline pmd_t native_pmdp_get_and_clear(pmd_t *pmdp)
  155. {
  156. union split_pmd res, *orig = (union split_pmd *)pmdp;
  157. /* xchg acts as a barrier before setting of the high bits */
  158. res.pmd_low = xchg(&orig->pmd_low, 0);
  159. res.pmd_high = orig->pmd_high;
  160. orig->pmd_high = 0;
  161. return res.pmd;
  162. }
  163. #else
  164. #define native_pmdp_get_and_clear(xp) native_local_pmdp_get_and_clear(xp)
  165. #endif
  166. #ifndef pmdp_establish
  167. #define pmdp_establish pmdp_establish
  168. static inline pmd_t pmdp_establish(struct vm_area_struct *vma,
  169. unsigned long address, pmd_t *pmdp, pmd_t pmd)
  170. {
  171. pmd_t old;
  172. /*
  173. * If pmd has present bit cleared we can get away without expensive
  174. * cmpxchg64: we can update pmdp half-by-half without racing with
  175. * anybody.
  176. */
  177. if (!(pmd_val(pmd) & _PAGE_PRESENT)) {
  178. union split_pmd old, new, *ptr;
  179. ptr = (union split_pmd *)pmdp;
  180. new.pmd = pmd;
  181. /* xchg acts as a barrier before setting of the high bits */
  182. old.pmd_low = xchg(&ptr->pmd_low, new.pmd_low);
  183. old.pmd_high = ptr->pmd_high;
  184. ptr->pmd_high = new.pmd_high;
  185. return old.pmd;
  186. }
  187. do {
  188. old = *pmdp;
  189. } while (cmpxchg64(&pmdp->pmd, old.pmd, pmd.pmd) != old.pmd);
  190. return old;
  191. }
  192. #endif
  193. #ifdef CONFIG_SMP
  194. union split_pud {
  195. struct {
  196. u32 pud_low;
  197. u32 pud_high;
  198. };
  199. pud_t pud;
  200. };
  201. static inline pud_t native_pudp_get_and_clear(pud_t *pudp)
  202. {
  203. union split_pud res, *orig = (union split_pud *)pudp;
  204. #ifdef CONFIG_PAGE_TABLE_ISOLATION
  205. pti_set_user_pgtbl(&pudp->p4d.pgd, __pgd(0));
  206. #endif
  207. /* xchg acts as a barrier before setting of the high bits */
  208. res.pud_low = xchg(&orig->pud_low, 0);
  209. res.pud_high = orig->pud_high;
  210. orig->pud_high = 0;
  211. return res.pud;
  212. }
  213. #else
  214. #define native_pudp_get_and_clear(xp) native_local_pudp_get_and_clear(xp)
  215. #endif
  216. /* Encode and de-code a swap entry */
  217. #define SWP_TYPE_BITS 5
  218. #define SWP_OFFSET_FIRST_BIT (_PAGE_BIT_PROTNONE + 1)
  219. /* We always extract/encode the offset by shifting it all the way up, and then down again */
  220. #define SWP_OFFSET_SHIFT (SWP_OFFSET_FIRST_BIT + SWP_TYPE_BITS)
  221. #define MAX_SWAPFILES_CHECK() BUILD_BUG_ON(MAX_SWAPFILES_SHIFT > SWP_TYPE_BITS)
  222. #define __swp_type(x) (((x).val) & ((1UL << SWP_TYPE_BITS) - 1))
  223. #define __swp_offset(x) ((x).val >> SWP_TYPE_BITS)
  224. #define __swp_entry(type, offset) ((swp_entry_t){(type) | (offset) << SWP_TYPE_BITS})
  225. /*
  226. * Normally, __swp_entry() converts from arch-independent swp_entry_t to
  227. * arch-dependent swp_entry_t, and __swp_entry_to_pte() just stores the result
  228. * to pte. But here we have 32bit swp_entry_t and 64bit pte, and need to use the
  229. * whole 64 bits. Thus, we shift the "real" arch-dependent conversion to
  230. * __swp_entry_to_pte() through the following helper macro based on 64bit
  231. * __swp_entry().
  232. */
  233. #define __swp_pteval_entry(type, offset) ((pteval_t) { \
  234. (~(pteval_t)(offset) << SWP_OFFSET_SHIFT >> SWP_TYPE_BITS) \
  235. | ((pteval_t)(type) << (64 - SWP_TYPE_BITS)) })
  236. #define __swp_entry_to_pte(x) ((pte_t){ .pte = \
  237. __swp_pteval_entry(__swp_type(x), __swp_offset(x)) })
  238. /*
  239. * Analogically, __pte_to_swp_entry() doesn't just extract the arch-dependent
  240. * swp_entry_t, but also has to convert it from 64bit to the 32bit
  241. * intermediate representation, using the following macros based on 64bit
  242. * __swp_type() and __swp_offset().
  243. */
  244. #define __pteval_swp_type(x) ((unsigned long)((x).pte >> (64 - SWP_TYPE_BITS)))
  245. #define __pteval_swp_offset(x) ((unsigned long)(~((x).pte) << SWP_TYPE_BITS >> SWP_OFFSET_SHIFT))
  246. #define __pte_to_swp_entry(pte) (__swp_entry(__pteval_swp_type(pte), \
  247. __pteval_swp_offset(pte)))
  248. #include <asm/pgtable-invert.h>
  249. #endif /* _ASM_X86_PGTABLE_3LEVEL_H */