1 #ifndef _ASM_X86_PGTABLE_3LEVEL_H
2 #define _ASM_X86_PGTABLE_3LEVEL_H
4 #include <asm/atomic64_32.h>
7 * Intel Physical Address Extension (PAE) Mode - three-level page
8 * tables on PPro+ CPUs.
10 * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
13 #define pte_ERROR(e) \
14 pr_err("%s:%d: bad pte %p(%08lx%08lx)\n", \
15 __FILE__, __LINE__, &(e), (e).pte_high, (e).pte_low)
16 #define pmd_ERROR(e) \
17 pr_err("%s:%d: bad pmd %p(%016Lx)\n", \
18 __FILE__, __LINE__, &(e), pmd_val(e))
19 #define pgd_ERROR(e) \
20 pr_err("%s:%d: bad pgd %p(%016Lx)\n", \
21 __FILE__, __LINE__, &(e), pgd_val(e))
23 /* Rules for using set_pte: the pte being assigned *must* be
24 * either not present or in a state where the hardware will
25 * not attempt to update the pte. In places where this is
26 * not possible, use pte_get_and_clear to obtain the old pte
27 * value and then use set_pte to update it. -ben
29 static inline void native_set_pte(pte_t *ptep, pte_t pte)
31 ptep->pte_high = pte.pte_high;
33 ptep->pte_low = pte.pte_low;
36 #define pmd_read_atomic pmd_read_atomic
38 * pte_offset_map_lock on 32bit PAE kernels was reading the pmd_t with
39 * a "*pmdp" dereference done by gcc. Problem is, in certain places
40 * where pte_offset_map_lock is called, concurrent page faults are
41 * allowed, if the mmap_sem is hold for reading. An example is mincore
42 * vs page faults vs MADV_DONTNEED. On the page fault side
43 * pmd_populate rightfully does a set_64bit, but if we're reading the
44 * pmd_t with a "*pmdp" on the mincore side, a SMP race can happen
45 * because gcc will not read the 64bit of the pmd atomically. To fix
46 * this all places running pmd_offset_map_lock() while holding the
47 * mmap_sem in read mode, shall read the pmdp pointer using this
48 * function to know if the pmd is null nor not, and in turn to know if
49 * they can run pmd_offset_map_lock or pmd_trans_huge or other pmd
52 * Without THP if the mmap_sem is hold for reading, the pmd can only
53 * transition from null to not null while pmd_read_atomic runs. So
54 * we can always return atomic pmd values with this function.
56 * With THP if the mmap_sem is hold for reading, the pmd can become
57 * trans_huge or none or point to a pte (and in turn become "stable")
58 * at any time under pmd_read_atomic. We could read it really
59 * atomically here with a atomic64_read for the THP enabled case (and
60 * it would be a whole lot simpler), but to avoid using cmpxchg8b we
61 * only return an atomic pmdval if the low part of the pmdval is later
62 * found stable (i.e. pointing to a pte). And we're returning a none
63 * pmdval if the low part of the pmd is none. In some cases the high
64 * and low part of the pmdval returned may not be consistent if THP is
65 * enabled (the low part may point to previously mapped hugepage,
66 * while the high part may point to a more recently mapped hugepage),
67 * but pmd_none_or_trans_huge_or_clear_bad() only needs the low part
68 * of the pmd to be read atomically to decide if the pmd is unstable
69 * or not, with the only exception of when the low part of the pmd is
70 * zero in which case we return a none pmd.
72 static inline pmd_t pmd_read_atomic(pmd_t *pmdp)
75 u32 *tmp = (u32 *)pmdp;
77 ret = (pmdval_t) (*tmp);
80 * If the low part is null, we must not read the high part
81 * or we can end up with a partial pmd.
84 ret |= ((pmdval_t)*(tmp + 1)) << 32;
87 return (pmd_t) { ret };
90 static inline void native_set_pte_atomic(pte_t *ptep, pte_t pte)
92 set_64bit((unsigned long long *)(ptep), native_pte_val(pte));
95 static inline void native_set_pmd(pmd_t *pmdp, pmd_t pmd)
97 set_64bit((unsigned long long *)(pmdp), native_pmd_val(pmd));
100 static inline void native_set_pud(pud_t *pudp, pud_t pud)
102 set_64bit((unsigned long long *)(pudp), native_pud_val(pud));
106 * For PTEs and PDEs, we must clear the P-bit first when clearing a page table
107 * entry, so clear the bottom half first and enforce ordering with a compiler
110 static inline void native_pte_clear(struct mm_struct *mm, unsigned long addr,
118 static inline void native_pmd_clear(pmd_t *pmd)
120 u32 *tmp = (u32 *)pmd;
126 static inline void pud_clear(pud_t *pudp)
128 set_pud(pudp, __pud(0));
131 * According to Intel App note "TLBs, Paging-Structure Caches,
132 * and Their Invalidation", April 2007, document 317080-001,
133 * section 8.1: in PAE mode we explicitly have to flush the
134 * TLB via cr3 if the top-level pgd is changed...
136 * Currently all places where pud_clear() is called either have
137 * flush_tlb_mm() followed or don't need TLB flush (x86_64 code or
138 * pud_clear_bad()), so we don't need TLB flush here.
143 static inline pte_t native_ptep_get_and_clear(pte_t *ptep)
147 res.pte = (pteval_t)atomic64_xchg((atomic64_t *)ptep, 0);
152 #define native_ptep_get_and_clear(xp) native_local_ptep_get_and_clear(xp)
163 static inline pmd_t native_pmdp_get_and_clear(pmd_t *pmdp)
165 union split_pmd res, *orig = (union split_pmd *)pmdp;
167 /* xchg acts as a barrier before setting of the high bits */
168 res.pmd_low = xchg(&orig->pmd_low, 0);
169 res.pmd_high = orig->pmd_high;
175 #define native_pmdp_get_and_clear(xp) native_local_pmdp_get_and_clear(xp)
178 /* Encode and de-code a swap entry */
179 #define SWP_TYPE_BITS 5
181 #define SWP_OFFSET_FIRST_BIT (_PAGE_BIT_PROTNONE + 1)
183 /* We always extract/encode the offset by shifting it all the way up, and then down again */
184 #define SWP_OFFSET_SHIFT (SWP_OFFSET_FIRST_BIT + SWP_TYPE_BITS)
186 #define MAX_SWAPFILES_CHECK() BUILD_BUG_ON(MAX_SWAPFILES_SHIFT > 5)
187 #define __swp_type(x) (((x).val) & 0x1f)
188 #define __swp_offset(x) ((x).val >> 5)
189 #define __swp_entry(type, offset) ((swp_entry_t){(type) | (offset) << 5})
192 * Normally, __swp_entry() converts from arch-independent swp_entry_t to
193 * arch-dependent swp_entry_t, and __swp_entry_to_pte() just stores the result
194 * to pte. But here we have 32bit swp_entry_t and 64bit pte, and need to use the
195 * whole 64 bits. Thus, we shift the "real" arch-dependent conversion to
196 * __swp_entry_to_pte() through the following helper macro based on 64bit
199 #define __swp_pteval_entry(type, offset) ((pteval_t) { \
200 (~(pteval_t)(offset) << SWP_OFFSET_SHIFT >> SWP_TYPE_BITS) \
201 | ((pteval_t)(type) << (64 - SWP_TYPE_BITS)) })
203 #define __swp_entry_to_pte(x) ((pte_t){ .pte = \
204 __swp_pteval_entry(__swp_type(x), __swp_offset(x)) })
206 * Analogically, __pte_to_swp_entry() doesn't just extract the arch-dependent
207 * swp_entry_t, but also has to convert it from 64bit to the 32bit
208 * intermediate representation, using the following macros based on 64bit
209 * __swp_type() and __swp_offset().
211 #define __pteval_swp_type(x) ((unsigned long)((x).pte >> (64 - SWP_TYPE_BITS)))
212 #define __pteval_swp_offset(x) ((unsigned long)(~((x).pte) << SWP_TYPE_BITS >> SWP_OFFSET_SHIFT))
214 #define __pte_to_swp_entry(pte) (__swp_entry(__pteval_swp_type(pte), \
215 __pteval_swp_offset(pte)))
217 #include <asm/pgtable-invert.h>
219 #endif /* _ASM_X86_PGTABLE_3LEVEL_H */