1 #ifndef _ASM_X86_PGTABLE_3LEVEL_H
2 #define _ASM_X86_PGTABLE_3LEVEL_H
3
4 #include <asm/atomic64_32.h>
5
6 /*
7 * Intel Physical Address Extension (PAE) Mode - three-level page
8 * tables on PPro+ CPUs.
9 *
10 * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
11 */
12
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))
22
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
28 */
native_set_pte(pte_t * ptep,pte_t pte)29 static inline void native_set_pte(pte_t *ptep, pte_t pte)
30 {
31 ptep->pte_high = pte.pte_high;
32 smp_wmb();
33 ptep->pte_low = pte.pte_low;
34 }
35
36 #define pmd_read_atomic pmd_read_atomic
37 /*
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
50 * operations.
51 *
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.
55 *
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.
71 */
pmd_read_atomic(pmd_t * pmdp)72 static inline pmd_t pmd_read_atomic(pmd_t *pmdp)
73 {
74 pmdval_t ret;
75 u32 *tmp = (u32 *)pmdp;
76
77 ret = (pmdval_t) (*tmp);
78 if (ret) {
79 /*
80 * If the low part is null, we must not read the high part
81 * or we can end up with a partial pmd.
82 */
83 smp_rmb();
84 ret |= ((pmdval_t)*(tmp + 1)) << 32;
85 }
86
87 return (pmd_t) { ret };
88 }
89
native_set_pte_atomic(pte_t * ptep,pte_t pte)90 static inline void native_set_pte_atomic(pte_t *ptep, pte_t pte)
91 {
92 set_64bit((unsigned long long *)(ptep), native_pte_val(pte));
93 }
94
native_set_pmd(pmd_t * pmdp,pmd_t pmd)95 static inline void native_set_pmd(pmd_t *pmdp, pmd_t pmd)
96 {
97 set_64bit((unsigned long long *)(pmdp), native_pmd_val(pmd));
98 }
99
native_set_pud(pud_t * pudp,pud_t pud)100 static inline void native_set_pud(pud_t *pudp, pud_t pud)
101 {
102 set_64bit((unsigned long long *)(pudp), native_pud_val(pud));
103 }
104
105 /*
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
108 * barrier.
109 */
native_pte_clear(struct mm_struct * mm,unsigned long addr,pte_t * ptep)110 static inline void native_pte_clear(struct mm_struct *mm, unsigned long addr,
111 pte_t *ptep)
112 {
113 ptep->pte_low = 0;
114 smp_wmb();
115 ptep->pte_high = 0;
116 }
117
native_pmd_clear(pmd_t * pmd)118 static inline void native_pmd_clear(pmd_t *pmd)
119 {
120 u32 *tmp = (u32 *)pmd;
121 *tmp = 0;
122 smp_wmb();
123 *(tmp + 1) = 0;
124 }
125
pud_clear(pud_t * pudp)126 static inline void pud_clear(pud_t *pudp)
127 {
128 set_pud(pudp, __pud(0));
129
130 /*
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...
135 *
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.
139 */
140 }
141
142 #ifdef CONFIG_SMP
native_ptep_get_and_clear(pte_t * ptep)143 static inline pte_t native_ptep_get_and_clear(pte_t *ptep)
144 {
145 pte_t res;
146
147 res.pte = (pteval_t)atomic64_xchg((atomic64_t *)ptep, 0);
148
149 return res;
150 }
151 #else
152 #define native_ptep_get_and_clear(xp) native_local_ptep_get_and_clear(xp)
153 #endif
154
155 #ifdef CONFIG_SMP
156 union split_pmd {
157 struct {
158 u32 pmd_low;
159 u32 pmd_high;
160 };
161 pmd_t pmd;
162 };
native_pmdp_get_and_clear(pmd_t * pmdp)163 static inline pmd_t native_pmdp_get_and_clear(pmd_t *pmdp)
164 {
165 union split_pmd res, *orig = (union split_pmd *)pmdp;
166
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;
170 orig->pmd_high = 0;
171
172 return res.pmd;
173 }
174 #else
175 #define native_pmdp_get_and_clear(xp) native_local_pmdp_get_and_clear(xp)
176 #endif
177
178 /* Encode and de-code a swap entry */
179 #define SWP_TYPE_BITS 5
180
181 #define SWP_OFFSET_FIRST_BIT (_PAGE_BIT_PROTNONE + 1)
182
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)
185
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})
190
191 /*
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
197 * __swp_entry().
198 */
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)) })
202
203 #define __swp_entry_to_pte(x) ((pte_t){ .pte = \
204 __swp_pteval_entry(__swp_type(x), __swp_offset(x)) })
205 /*
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().
210 */
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))
213
214 #define __pte_to_swp_entry(pte) (__swp_entry(__pteval_swp_type(pte), \
215 __pteval_swp_offset(pte)))
216
217 #include <asm/pgtable-invert.h>
218
219 #endif /* _ASM_X86_PGTABLE_3LEVEL_H */
220