blob: fa7208b483cada7c9d735f8381c503b289a02d86 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _X86_64_PGTABLE_H
2#define _X86_64_PGTABLE_H
3
Randy Dunlap6df95fd2007-05-08 00:31:11 -07004#include <linux/const.h>
Vivek Goyal9d291e72007-05-02 19:27:06 +02005#ifndef __ASSEMBLY__
6
Linus Torvalds1da177e2005-04-16 15:20:36 -07007/*
8 * This file contains the functions and defines necessary to modify and use
9 * the x86-64 page table tree.
10 */
11#include <asm/processor.h>
Jiri Slaby1977f032007-10-18 23:40:25 -070012#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/threads.h>
14#include <asm/pda.h>
15
16extern pud_t level3_kernel_pgt[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -070017extern pud_t level3_ident_pgt[512];
18extern pmd_t level2_kernel_pgt[512];
19extern pgd_t init_level4_pgt[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Linus Torvaldse3ebadd2007-05-07 08:44:24 -070021#define swapper_pg_dir init_level4_pgt
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023extern void paging_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Vivek Goyal9d291e72007-05-02 19:27:06 +020025#endif /* !__ASSEMBLY__ */
26
Jeremy Fitzhardinge85958b42008-03-17 16:37:14 -070027#define SHARED_KERNEL_PMD 0
Ingo Molnare4b71dc2008-01-30 13:34:04 +010028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/*
30 * PGDIR_SHIFT determines what a top-level page table entry can map
31 */
32#define PGDIR_SHIFT 39
33#define PTRS_PER_PGD 512
34
35/*
36 * 3rd level page
37 */
38#define PUD_SHIFT 30
39#define PTRS_PER_PUD 512
40
41/*
42 * PMD_SHIFT determines the size of the area a middle-level
43 * page table can map
44 */
45#define PMD_SHIFT 21
46#define PTRS_PER_PMD 512
47
48/*
49 * entries per page directory level
50 */
51#define PTRS_PER_PTE 512
52
Vivek Goyal9d291e72007-05-02 19:27:06 +020053#ifndef __ASSEMBLY__
54
Joe Perches7f944012008-03-23 01:03:11 -070055#define pte_ERROR(e) \
56 printk("%s:%d: bad pte %p(%016lx).\n", \
57 __FILE__, __LINE__, &(e), pte_val(e))
58#define pmd_ERROR(e) \
59 printk("%s:%d: bad pmd %p(%016lx).\n", \
60 __FILE__, __LINE__, &(e), pmd_val(e))
61#define pud_ERROR(e) \
62 printk("%s:%d: bad pud %p(%016lx).\n", \
63 __FILE__, __LINE__, &(e), pud_val(e))
64#define pgd_ERROR(e) \
65 printk("%s:%d: bad pgd %p(%016lx).\n", \
66 __FILE__, __LINE__, &(e), pgd_val(e))
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68#define pgd_none(x) (!pgd_val(x))
69#define pud_none(x) (!pud_val(x))
70
Tim Schmielau8c65b4a2005-11-07 00:59:43 -080071struct mm_struct;
72
Eduardo Habkost0814e0b2008-06-25 00:19:22 -040073void set_pte_vaddr_pud(pud_t *pud_page, unsigned long vaddr, pte_t new_pte);
74
75
Jeremy Fitzhardinge48916452008-01-30 13:32:58 +010076static inline void native_pte_clear(struct mm_struct *mm, unsigned long addr,
77 pte_t *ptep)
Zachary Amsden61e06032005-09-03 15:55:06 -070078{
Jeremy Fitzhardinge48916452008-01-30 13:32:58 +010079 *ptep = native_make_pte(0);
80}
81
82static inline void native_set_pte(pte_t *ptep, pte_t pte)
83{
84 *ptep = pte;
85}
86
Ingo Molnarb65e6392008-01-30 13:34:01 +010087static inline void native_set_pte_atomic(pte_t *ptep, pte_t pte)
88{
89 native_set_pte(ptep, pte);
90}
91
Jeremy Fitzhardinge48916452008-01-30 13:32:58 +010092static inline pte_t native_ptep_get_and_clear(pte_t *xp)
93{
94#ifdef CONFIG_SMP
95 return native_make_pte(xchg(&xp->pte, 0));
96#else
Joe Perches7f944012008-03-23 01:03:11 -070097 /* native_local_ptep_get_and_clear,
98 but duplicated because of cyclic dependency */
Jeremy Fitzhardinge48916452008-01-30 13:32:58 +010099 pte_t ret = *xp;
100 native_pte_clear(NULL, 0, xp);
101 return ret;
102#endif
103}
104
105static inline void native_set_pmd(pmd_t *pmdp, pmd_t pmd)
106{
107 *pmdp = pmd;
108}
109
110static inline void native_pmd_clear(pmd_t *pmd)
111{
112 native_set_pmd(pmd, native_make_pmd(0));
113}
114
115static inline void native_set_pud(pud_t *pudp, pud_t pud)
116{
117 *pudp = pud;
118}
119
120static inline void native_pud_clear(pud_t *pud)
121{
122 native_set_pud(pud, native_make_pud(0));
123}
124
125static inline void native_set_pgd(pgd_t *pgdp, pgd_t pgd)
126{
127 *pgdp = pgd;
128}
129
Joe Perches7f944012008-03-23 01:03:11 -0700130static inline void native_pgd_clear(pgd_t *pgd)
Jeremy Fitzhardinge48916452008-01-30 13:32:58 +0100131{
132 native_set_pgd(pgd, native_make_pgd(0));
Zachary Amsden61e06032005-09-03 15:55:06 -0700133}
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#define pte_same(a, b) ((a).pte == (b).pte)
136
Vivek Goyal9d291e72007-05-02 19:27:06 +0200137#endif /* !__ASSEMBLY__ */
138
Joe Perches7f944012008-03-23 01:03:11 -0700139#define PMD_SIZE (_AC(1, UL) << PMD_SHIFT)
140#define PMD_MASK (~(PMD_SIZE - 1))
141#define PUD_SIZE (_AC(1, UL) << PUD_SHIFT)
142#define PUD_MASK (~(PUD_SIZE - 1))
143#define PGDIR_SIZE (_AC(1, UL) << PGDIR_SHIFT)
144#define PGDIR_MASK (~(PGDIR_SIZE - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Joe Perches7f944012008-03-23 01:03:11 -0700147#define MAXMEM _AC(0x00003fffffffffff, UL)
Randy Dunlap63f65642007-05-08 00:31:14 -0700148#define VMALLOC_START _AC(0xffffc20000000000, UL)
149#define VMALLOC_END _AC(0xffffe1ffffffffff, UL)
Christoph Lameter0889eba2007-10-16 01:24:15 -0700150#define VMEMMAP_START _AC(0xffffe20000000000, UL)
Ingo Molnar85eb69a2008-02-21 12:50:51 +0100151#define MODULES_VADDR _AC(0xffffffffa0000000, UL)
Randy Dunlap63f65642007-05-08 00:31:14 -0700152#define MODULES_END _AC(0xfffffffffff00000, UL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153#define MODULES_LEN (MODULES_END - MODULES_VADDR)
154
Vivek Goyal9d291e72007-05-02 19:27:06 +0200155#ifndef __ASSEMBLY__
156
Hugh Dickinsa8375bd2008-05-20 13:59:47 +0100157static inline int pgd_bad(pgd_t pgd)
Jan Beulicheab724e2006-12-07 02:14:09 +0100158{
Hugh Dickinsa8375bd2008-05-20 13:59:47 +0100159 return (pgd_val(pgd) & ~(PTE_MASK | _PAGE_USER)) != _KERNPG_TABLE;
Jan Beulicheab724e2006-12-07 02:14:09 +0100160}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Hugh Dickinsa8375bd2008-05-20 13:59:47 +0100162static inline int pud_bad(pud_t pud)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Hugh Dickinsa8375bd2008-05-20 13:59:47 +0100164 return (pud_val(pud) & ~(PTE_MASK | _PAGE_USER)) != _KERNPG_TABLE;
Jan Beulicheab724e2006-12-07 02:14:09 +0100165}
166
Hugh Dickinsa8375bd2008-05-20 13:59:47 +0100167static inline int pmd_bad(pmd_t pmd)
Jan Beulicheab724e2006-12-07 02:14:09 +0100168{
Hugh Dickinsa8375bd2008-05-20 13:59:47 +0100169 return (pmd_val(pmd) & ~(PTE_MASK | _PAGE_USER)) != _KERNPG_TABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
Joe Perches7f944012008-03-23 01:03:11 -0700172#define pte_none(x) (!pte_val((x)))
173#define pte_present(x) (pte_val((x)) & (_PAGE_PRESENT | _PAGE_PROTNONE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Joe Perches7f944012008-03-23 01:03:11 -0700175#define pages_to_mb(x) ((x) >> (20 - PAGE_SHIFT)) /* FIXME: is this right? */
176#define pte_page(x) pfn_to_page(pte_pfn((x)))
177#define pte_pfn(x) ((pte_val((x)) & __PHYSICAL_MASK) >> PAGE_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179/*
180 * Macro to mark a page protection value as "uncacheable".
181 */
Joe Perches7f944012008-03-23 01:03:11 -0700182#define pgprot_noncached(prot) \
183 (__pgprot(pgprot_val((prot)) | _PAGE_PCD | _PAGE_PWT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185/*
186 * Conversion functions: convert a page and protection to a page entry,
187 * and a page entry and page directory to the page they refer to.
188 */
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/*
191 * Level 4 access.
192 */
Joe Perches7f944012008-03-23 01:03:11 -0700193#define pgd_page_vaddr(pgd) \
194 ((unsigned long)__va((unsigned long)pgd_val((pgd)) & PTE_MASK))
195#define pgd_page(pgd) (pfn_to_page(pgd_val((pgd)) >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196#define pgd_present(pgd) (pgd_val(pgd) & _PAGE_PRESENT)
H. Peter Anvine00fc5422008-02-19 16:18:32 +0100197static inline int pgd_large(pgd_t pgd) { return 0; }
Eduardo Habkoste7a9b0b2008-06-25 00:19:05 -0400198#define mk_kernel_pgd(address) __pgd((address) | _KERNPG_TABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200/* PUD - Level3 access */
201/* to find an entry in a page-table-directory. */
Joe Perches7f944012008-03-23 01:03:11 -0700202#define pud_page_vaddr(pud) \
203 ((unsigned long)__va(pud_val((pud)) & PHYSICAL_PAGE_MASK))
204#define pud_page(pud) (pfn_to_page(pud_val((pud)) >> PAGE_SHIFT))
205#define pud_index(address) (((address) >> PUD_SHIFT) & (PTRS_PER_PUD - 1))
206#define pud_offset(pgd, address) \
207 ((pud_t *)pgd_page_vaddr(*(pgd)) + pud_index((address)))
208#define pud_present(pud) (pud_val((pud)) & _PAGE_PRESENT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Andi Kleen61e19a32008-02-04 16:48:09 +0100210static inline int pud_large(pud_t pte)
211{
Joe Perches7f944012008-03-23 01:03:11 -0700212 return (pud_val(pte) & (_PAGE_PSE | _PAGE_PRESENT)) ==
213 (_PAGE_PSE | _PAGE_PRESENT);
Andi Kleen61e19a32008-02-04 16:48:09 +0100214}
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/* PMD - Level 2 access */
Joe Perches7f944012008-03-23 01:03:11 -0700217#define pmd_page_vaddr(pmd) ((unsigned long) __va(pmd_val((pmd)) & PTE_MASK))
218#define pmd_page(pmd) (pfn_to_page(pmd_val((pmd)) >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Joe Perches7f944012008-03-23 01:03:11 -0700220#define pmd_index(address) (((address) >> PMD_SHIFT) & (PTRS_PER_PMD - 1))
221#define pmd_offset(dir, address) ((pmd_t *)pud_page_vaddr(*(dir)) + \
222 pmd_index(address))
223#define pmd_none(x) (!pmd_val((x)))
224#define pmd_present(x) (pmd_val((x)) & _PAGE_PRESENT)
225#define pfn_pmd(nr, prot) (__pmd(((nr) << PAGE_SHIFT) | pgprot_val((prot))))
226#define pmd_pfn(x) ((pmd_val((x)) & __PHYSICAL_MASK) >> PAGE_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Joe Perches7f944012008-03-23 01:03:11 -0700228#define pte_to_pgoff(pte) ((pte_val((pte)) & PHYSICAL_PAGE_MASK) >> PAGE_SHIFT)
229#define pgoff_to_pte(off) ((pte_t) { .pte = ((off) << PAGE_SHIFT) | \
230 _PAGE_FILE })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231#define PTE_FILE_MAX_BITS __PHYSICAL_MASK_SHIFT
232
233/* PTE - Level 1 access. */
234
235/* page, protection -> pte */
Joe Perches7f944012008-03-23 01:03:11 -0700236#define mk_pte(page, pgprot) pfn_pte(page_to_pfn((page)), (pgprot))
237
238#define pte_index(address) (((address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1))
Dave McCracken46a82b22006-09-25 23:31:48 -0700239#define pte_offset_kernel(dir, address) ((pte_t *) pmd_page_vaddr(*(dir)) + \
Joe Perches7f944012008-03-23 01:03:11 -0700240 pte_index((address)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242/* x86-64 always has all page tables mapped. */
Joe Perches7f944012008-03-23 01:03:11 -0700243#define pte_offset_map(dir, address) pte_offset_kernel((dir), (address))
244#define pte_offset_map_nested(dir, address) pte_offset_kernel((dir), (address))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245#define pte_unmap(pte) /* NOP */
Joe Perches7f944012008-03-23 01:03:11 -0700246#define pte_unmap_nested(pte) /* NOP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Joe Perches7f944012008-03-23 01:03:11 -0700248#define update_mmu_cache(vma, address, pte) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Ingo Molnar00d1c5e2008-04-17 17:40:45 +0200250extern int direct_gbpages;
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252/* Encode and de-code a swap entry */
253#define __swp_type(x) (((x).val >> 1) & 0x3f)
254#define __swp_offset(x) ((x).val >> 8)
Joe Perches7f944012008-03-23 01:03:11 -0700255#define __swp_entry(type, offset) ((swp_entry_t) { ((type) << 1) | \
256 ((offset) << 8) })
257#define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val((pte)) })
Jeremy Fitzhardingec8e53932008-01-30 13:32:57 +0100258#define __swp_entry_to_pte(x) ((pte_t) { .pte = (x).val })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Joe Perches7f944012008-03-23 01:03:11 -0700260extern int kern_addr_valid(unsigned long addr);
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100261extern void cleanup_highmap(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Joe Perches7f944012008-03-23 01:03:11 -0700263#define io_remap_pfn_range(vma, vaddr, pfn, size, prot) \
264 remap_pfn_range(vma, vaddr, pfn, size, prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266#define HAVE_ARCH_UNMAPPED_AREA
Jiri Kosinacc503c12008-01-30 13:31:07 +0100267#define HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269#define pgtable_cache_init() do { } while (0)
Linus Torvaldsda8f1532007-09-21 12:09:41 -0700270#define check_pgt_cache() do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272#define PAGE_AGP PAGE_KERNEL_NOCACHE
273#define HAVE_PAGE_AGP 1
274
275/* fs/proc/kcore.c */
276#define kc_vaddr_to_offset(v) ((v) & __VIRTUAL_MASK)
Joe Perches7f944012008-03-23 01:03:11 -0700277#define kc_offset_to_vaddr(o) \
278 (((o) & (1UL << (__VIRTUAL_MASK_SHIFT - 1))) \
279 ? ((o) | ~__VIRTUAL_MASK) \
280 : (o))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282#define __HAVE_ARCH_PTE_SAME
Vivek Goyal9d291e72007-05-02 19:27:06 +0200283#endif /* !__ASSEMBLY__ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285#endif /* _X86_64_PGTABLE_H */