Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[pandora-kernel.git] / include / asm-x86 / pgtable.h
1 #ifndef _ASM_X86_PGTABLE_H
2 #define _ASM_X86_PGTABLE_H
3
4 #define USER_PTRS_PER_PGD       ((TASK_SIZE-1)/PGDIR_SIZE+1)
5 #define FIRST_USER_ADDRESS      0
6
7 #define _PAGE_BIT_PRESENT       0
8 #define _PAGE_BIT_RW            1
9 #define _PAGE_BIT_USER          2
10 #define _PAGE_BIT_PWT           3
11 #define _PAGE_BIT_PCD           4
12 #define _PAGE_BIT_ACCESSED      5
13 #define _PAGE_BIT_DIRTY         6
14 #define _PAGE_BIT_FILE          6
15 #define _PAGE_BIT_PSE           7       /* 4 MB (or 2MB) page */
16 #define _PAGE_BIT_GLOBAL        8       /* Global TLB entry PPro+ */
17 #define _PAGE_BIT_UNUSED1       9       /* available for programmer */
18 #define _PAGE_BIT_UNUSED2       10
19 #define _PAGE_BIT_UNUSED3       11
20 #define _PAGE_BIT_NX           63       /* No execute: only valid after cpuid check */
21
22 /*
23  * Note: we use _AC(1, L) instead of _AC(1, UL) so that we get a
24  * sign-extended value on 32-bit with all 1's in the upper word,
25  * which preserves the upper pte values on 64-bit ptes:
26  */
27 #define _PAGE_PRESENT   (_AC(1, L)<<_PAGE_BIT_PRESENT)
28 #define _PAGE_RW        (_AC(1, L)<<_PAGE_BIT_RW)
29 #define _PAGE_USER      (_AC(1, L)<<_PAGE_BIT_USER)
30 #define _PAGE_PWT       (_AC(1, L)<<_PAGE_BIT_PWT)
31 #define _PAGE_PCD       (_AC(1, L)<<_PAGE_BIT_PCD)
32 #define _PAGE_ACCESSED  (_AC(1, L)<<_PAGE_BIT_ACCESSED)
33 #define _PAGE_DIRTY     (_AC(1, L)<<_PAGE_BIT_DIRTY)
34 #define _PAGE_PSE       (_AC(1, L)<<_PAGE_BIT_PSE)      /* 2MB page */
35 #define _PAGE_GLOBAL    (_AC(1, L)<<_PAGE_BIT_GLOBAL)   /* Global TLB entry */
36 #define _PAGE_UNUSED1   (_AC(1, L)<<_PAGE_BIT_UNUSED1)
37 #define _PAGE_UNUSED2   (_AC(1, L)<<_PAGE_BIT_UNUSED2)
38 #define _PAGE_UNUSED3   (_AC(1, L)<<_PAGE_BIT_UNUSED3)
39
40 #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
41 #define _PAGE_NX        (_AC(1, ULL) << _PAGE_BIT_NX)
42 #else
43 #define _PAGE_NX        0
44 #endif
45
46 /* If _PAGE_PRESENT is clear, we use these: */
47 #define _PAGE_FILE      _PAGE_DIRTY     /* nonlinear file mapping, saved PTE; unset:swap */
48 #define _PAGE_PROTNONE  _PAGE_PSE       /* if the user mapped it with PROT_NONE;
49                                            pte_present gives true */
50
51 #define _PAGE_TABLE     (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | _PAGE_ACCESSED | _PAGE_DIRTY)
52 #define _KERNPG_TABLE   (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY)
53
54 #define _PAGE_CHG_MASK  (PTE_MASK | _PAGE_ACCESSED | _PAGE_DIRTY)
55
56 #define PAGE_NONE       __pgprot(_PAGE_PROTNONE | _PAGE_ACCESSED)
57 #define PAGE_SHARED     __pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | _PAGE_ACCESSED | _PAGE_NX)
58
59 #define PAGE_SHARED_EXEC        __pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | _PAGE_ACCESSED)
60 #define PAGE_COPY_NOEXEC        __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_ACCESSED | _PAGE_NX)
61 #define PAGE_COPY_EXEC          __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_ACCESSED)
62 #define PAGE_COPY               PAGE_COPY_NOEXEC
63 #define PAGE_READONLY           __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_ACCESSED | _PAGE_NX)
64 #define PAGE_READONLY_EXEC      __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_ACCESSED)
65
66 #ifdef CONFIG_X86_32
67 #define _PAGE_KERNEL_EXEC \
68         (_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY | _PAGE_ACCESSED)
69 #define _PAGE_KERNEL (_PAGE_KERNEL_EXEC | _PAGE_NX)
70
71 #ifndef __ASSEMBLY__
72 extern pteval_t __PAGE_KERNEL, __PAGE_KERNEL_EXEC;
73 #endif  /* __ASSEMBLY__ */
74 #else
75 #define __PAGE_KERNEL_EXEC                                              \
76         (_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY | _PAGE_ACCESSED)
77 #define __PAGE_KERNEL           (__PAGE_KERNEL_EXEC | _PAGE_NX)
78 #endif
79
80 #define __PAGE_KERNEL_RO                (__PAGE_KERNEL & ~_PAGE_RW)
81 #define __PAGE_KERNEL_RX                (__PAGE_KERNEL_EXEC & ~_PAGE_RW)
82 #define __PAGE_KERNEL_EXEC_NOCACHE      (__PAGE_KERNEL_EXEC | _PAGE_PCD | _PAGE_PWT)
83 #define __PAGE_KERNEL_NOCACHE           (__PAGE_KERNEL | _PAGE_PCD | _PAGE_PWT)
84 #define __PAGE_KERNEL_VSYSCALL          (__PAGE_KERNEL_RX | _PAGE_USER)
85 #define __PAGE_KERNEL_VSYSCALL_NOCACHE  (__PAGE_KERNEL_VSYSCALL | _PAGE_PCD | _PAGE_PWT)
86 #define __PAGE_KERNEL_LARGE             (__PAGE_KERNEL | _PAGE_PSE)
87 #define __PAGE_KERNEL_LARGE_EXEC        (__PAGE_KERNEL_EXEC | _PAGE_PSE)
88
89 #ifdef CONFIG_X86_32
90 # define MAKE_GLOBAL(x)                 __pgprot((x))
91 #else
92 # define MAKE_GLOBAL(x)                 __pgprot((x) | _PAGE_GLOBAL)
93 #endif
94
95 #define PAGE_KERNEL                     MAKE_GLOBAL(__PAGE_KERNEL)
96 #define PAGE_KERNEL_RO                  MAKE_GLOBAL(__PAGE_KERNEL_RO)
97 #define PAGE_KERNEL_EXEC                MAKE_GLOBAL(__PAGE_KERNEL_EXEC)
98 #define PAGE_KERNEL_RX                  MAKE_GLOBAL(__PAGE_KERNEL_RX)
99 #define PAGE_KERNEL_NOCACHE             MAKE_GLOBAL(__PAGE_KERNEL_NOCACHE)
100 #define PAGE_KERNEL_EXEC_NOCACHE        MAKE_GLOBAL(__PAGE_KERNEL_EXEC_NOCACHE)
101 #define PAGE_KERNEL_LARGE               MAKE_GLOBAL(__PAGE_KERNEL_LARGE)
102 #define PAGE_KERNEL_LARGE_EXEC          MAKE_GLOBAL(__PAGE_KERNEL_LARGE_EXEC)
103 #define PAGE_KERNEL_VSYSCALL            MAKE_GLOBAL(__PAGE_KERNEL_VSYSCALL)
104 #define PAGE_KERNEL_VSYSCALL_NOCACHE    MAKE_GLOBAL(__PAGE_KERNEL_VSYSCALL_NOCACHE)
105
106 /*         xwr */
107 #define __P000  PAGE_NONE
108 #define __P001  PAGE_READONLY
109 #define __P010  PAGE_COPY
110 #define __P011  PAGE_COPY
111 #define __P100  PAGE_READONLY_EXEC
112 #define __P101  PAGE_READONLY_EXEC
113 #define __P110  PAGE_COPY_EXEC
114 #define __P111  PAGE_COPY_EXEC
115
116 #define __S000  PAGE_NONE
117 #define __S001  PAGE_READONLY
118 #define __S010  PAGE_SHARED
119 #define __S011  PAGE_SHARED
120 #define __S100  PAGE_READONLY_EXEC
121 #define __S101  PAGE_READONLY_EXEC
122 #define __S110  PAGE_SHARED_EXEC
123 #define __S111  PAGE_SHARED_EXEC
124
125 #ifndef __ASSEMBLY__
126
127 /*
128  * ZERO_PAGE is a global shared page that is always zero: used
129  * for zero-mapped memory areas etc..
130  */
131 extern unsigned long empty_zero_page[PAGE_SIZE/sizeof(unsigned long)];
132 #define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
133
134 extern spinlock_t pgd_lock;
135 extern struct list_head pgd_list;
136
137 /*
138  * The following only work if pte_present() is true.
139  * Undefined behaviour if not..
140  */
141 static inline int pte_dirty(pte_t pte)          { return pte_val(pte) & _PAGE_DIRTY; }
142 static inline int pte_young(pte_t pte)          { return pte_val(pte) & _PAGE_ACCESSED; }
143 static inline int pte_write(pte_t pte)          { return pte_val(pte) & _PAGE_RW; }
144 static inline int pte_file(pte_t pte)           { return pte_val(pte) & _PAGE_FILE; }
145 static inline int pte_huge(pte_t pte)           { return pte_val(pte) & _PAGE_PSE; }
146 static inline int pte_global(pte_t pte)         { return pte_val(pte) & _PAGE_GLOBAL; }
147 static inline int pte_exec(pte_t pte)           { return !(pte_val(pte) & _PAGE_NX); }
148
149 static inline int pmd_large(pmd_t pte) {
150         return (pmd_val(pte) & (_PAGE_PSE|_PAGE_PRESENT)) ==
151                 (_PAGE_PSE|_PAGE_PRESENT);
152 }
153
154 static inline pte_t pte_mkclean(pte_t pte)      { return __pte(pte_val(pte) & ~(pteval_t)_PAGE_DIRTY); }
155 static inline pte_t pte_mkold(pte_t pte)        { return __pte(pte_val(pte) & ~(pteval_t)_PAGE_ACCESSED); }
156 static inline pte_t pte_wrprotect(pte_t pte)    { return __pte(pte_val(pte) & ~(pteval_t)_PAGE_RW); }
157 static inline pte_t pte_mkexec(pte_t pte)       { return __pte(pte_val(pte) & ~(pteval_t)_PAGE_NX); }
158 static inline pte_t pte_mkdirty(pte_t pte)      { return __pte(pte_val(pte) | _PAGE_DIRTY); }
159 static inline pte_t pte_mkyoung(pte_t pte)      { return __pte(pte_val(pte) | _PAGE_ACCESSED); }
160 static inline pte_t pte_mkwrite(pte_t pte)      { return __pte(pte_val(pte) | _PAGE_RW); }
161 static inline pte_t pte_mkhuge(pte_t pte)       { return __pte(pte_val(pte) | _PAGE_PSE); }
162 static inline pte_t pte_clrhuge(pte_t pte)      { return __pte(pte_val(pte) & ~(pteval_t)_PAGE_PSE); }
163 static inline pte_t pte_mkglobal(pte_t pte)     { return __pte(pte_val(pte) | _PAGE_GLOBAL); }
164 static inline pte_t pte_clrglobal(pte_t pte)    { return __pte(pte_val(pte) & ~(pteval_t)_PAGE_GLOBAL); }
165
166 extern pteval_t __supported_pte_mask;
167
168 static inline pte_t pfn_pte(unsigned long page_nr, pgprot_t pgprot)
169 {
170         return __pte((((phys_addr_t)page_nr << PAGE_SHIFT) |
171                       pgprot_val(pgprot)) & __supported_pte_mask);
172 }
173
174 static inline pmd_t pfn_pmd(unsigned long page_nr, pgprot_t pgprot)
175 {
176         return __pmd((((phys_addr_t)page_nr << PAGE_SHIFT) |
177                       pgprot_val(pgprot)) & __supported_pte_mask);
178 }
179
180 static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
181 {
182         pteval_t val = pte_val(pte);
183
184         /*
185          * Chop off the NX bit (if present), and add the NX portion of
186          * the newprot (if present):
187          */
188         val &= _PAGE_CHG_MASK & ~_PAGE_NX;
189         val |= pgprot_val(newprot) & __supported_pte_mask;
190
191         return __pte(val);
192 }
193
194 #define pte_pgprot(x) __pgprot(pte_val(x) & (0xfff | _PAGE_NX))
195
196 #define canon_pgprot(p) __pgprot(pgprot_val(p) & __supported_pte_mask)
197
198 #ifdef CONFIG_PARAVIRT
199 #include <asm/paravirt.h>
200 #else  /* !CONFIG_PARAVIRT */
201 #define set_pte(ptep, pte)              native_set_pte(ptep, pte)
202 #define set_pte_at(mm, addr, ptep, pte) native_set_pte_at(mm, addr, ptep, pte)
203
204 #define set_pte_present(mm, addr, ptep, pte)                            \
205         native_set_pte_present(mm, addr, ptep, pte)
206 #define set_pte_atomic(ptep, pte)                                       \
207         native_set_pte_atomic(ptep, pte)
208
209 #define set_pmd(pmdp, pmd)              native_set_pmd(pmdp, pmd)
210
211 #ifndef __PAGETABLE_PUD_FOLDED
212 #define set_pgd(pgdp, pgd)              native_set_pgd(pgdp, pgd)
213 #define pgd_clear(pgd)                  native_pgd_clear(pgd)
214 #endif
215
216 #ifndef set_pud
217 # define set_pud(pudp, pud)             native_set_pud(pudp, pud)
218 #endif
219
220 #ifndef __PAGETABLE_PMD_FOLDED
221 #define pud_clear(pud)                  native_pud_clear(pud)
222 #endif
223
224 #define pte_clear(mm, addr, ptep)       native_pte_clear(mm, addr, ptep)
225 #define pmd_clear(pmd)                  native_pmd_clear(pmd)
226
227 #define pte_update(mm, addr, ptep)              do { } while (0)
228 #define pte_update_defer(mm, addr, ptep)        do { } while (0)
229 #endif  /* CONFIG_PARAVIRT */
230
231 #endif  /* __ASSEMBLY__ */
232
233 #ifdef CONFIG_X86_32
234 # include "pgtable_32.h"
235 #else
236 # include "pgtable_64.h"
237 #endif
238
239 #ifndef __ASSEMBLY__
240
241 enum {
242         PG_LEVEL_NONE,
243         PG_LEVEL_4K,
244         PG_LEVEL_2M,
245         PG_LEVEL_1G,
246 };
247
248 /*
249  * Helper function that returns the kernel pagetable entry controlling
250  * the virtual address 'address'. NULL means no pagetable entry present.
251  * NOTE: the return type is pte_t but if the pmd is PSE then we return it
252  * as a pte too.
253  */
254 extern pte_t *lookup_address(unsigned long address, int *level);
255
256 /* local pte updates need not use xchg for locking */
257 static inline pte_t native_local_ptep_get_and_clear(pte_t *ptep)
258 {
259         pte_t res = *ptep;
260
261         /* Pure native function needs no input for mm, addr */
262         native_pte_clear(NULL, 0, ptep);
263         return res;
264 }
265
266 static inline void native_set_pte_at(struct mm_struct *mm, unsigned long addr,
267                                      pte_t *ptep , pte_t pte)
268 {
269         native_set_pte(ptep, pte);
270 }
271
272 #ifndef CONFIG_PARAVIRT
273 /*
274  * Rules for using pte_update - it must be called after any PTE update which
275  * has not been done using the set_pte / clear_pte interfaces.  It is used by
276  * shadow mode hypervisors to resynchronize the shadow page tables.  Kernel PTE
277  * updates should either be sets, clears, or set_pte_atomic for P->P
278  * transitions, which means this hook should only be called for user PTEs.
279  * This hook implies a P->P protection or access change has taken place, which
280  * requires a subsequent TLB flush.  The notification can optionally be delayed
281  * until the TLB flush event by using the pte_update_defer form of the
282  * interface, but care must be taken to assure that the flush happens while
283  * still holding the same page table lock so that the shadow and primary pages
284  * do not become out of sync on SMP.
285  */
286 #define pte_update(mm, addr, ptep)              do { } while (0)
287 #define pte_update_defer(mm, addr, ptep)        do { } while (0)
288 #endif
289
290 /*
291  * We only update the dirty/accessed state if we set
292  * the dirty bit by hand in the kernel, since the hardware
293  * will do the accessed bit for us, and we don't want to
294  * race with other CPU's that might be updating the dirty
295  * bit at the same time.
296  */
297 #define  __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
298 #define ptep_set_access_flags(vma, address, ptep, entry, dirty)         \
299 ({                                                                      \
300         int __changed = !pte_same(*(ptep), entry);                      \
301         if (__changed && dirty) {                                       \
302                 *ptep = entry;                                          \
303                 pte_update_defer((vma)->vm_mm, (address), (ptep));      \
304                 flush_tlb_page(vma, address);                           \
305         }                                                               \
306         __changed;                                                      \
307 })
308
309 #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
310 #define ptep_test_and_clear_young(vma, addr, ptep) ({                   \
311         int __ret = 0;                                                  \
312         if (pte_young(*(ptep)))                                         \
313                 __ret = test_and_clear_bit(_PAGE_BIT_ACCESSED,          \
314                                            &(ptep)->pte);               \
315         if (__ret)                                                      \
316                 pte_update((vma)->vm_mm, addr, ptep);                   \
317         __ret;                                                          \
318 })
319
320 #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
321 #define ptep_clear_flush_young(vma, address, ptep)                      \
322 ({                                                                      \
323         int __young;                                                    \
324         __young = ptep_test_and_clear_young((vma), (address), (ptep));  \
325         if (__young)                                                    \
326                 flush_tlb_page(vma, address);                           \
327         __young;                                                        \
328 })
329
330 #define __HAVE_ARCH_PTEP_GET_AND_CLEAR
331 static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
332 {
333         pte_t pte = native_ptep_get_and_clear(ptep);
334         pte_update(mm, addr, ptep);
335         return pte;
336 }
337
338 #define __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL
339 static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm, unsigned long addr, pte_t *ptep, int full)
340 {
341         pte_t pte;
342         if (full) {
343                 /*
344                  * Full address destruction in progress; paravirt does not
345                  * care about updates and native needs no locking
346                  */
347                 pte = native_local_ptep_get_and_clear(ptep);
348         } else {
349                 pte = ptep_get_and_clear(mm, addr, ptep);
350         }
351         return pte;
352 }
353
354 #define __HAVE_ARCH_PTEP_SET_WRPROTECT
355 static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
356 {
357         clear_bit(_PAGE_BIT_RW, (unsigned long *)&ptep->pte);
358         pte_update(mm, addr, ptep);
359 }
360
361 #include <asm-generic/pgtable.h>
362 #endif  /* __ASSEMBLY__ */
363
364 #endif  /* _ASM_X86_PGTABLE_H */