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