blob: 435e17187eb6a684a72b034be81d45f2125d76e6 [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);
24extern void clear_kernel_mapping(unsigned long addr, unsigned long size);
25
Vivek Goyal9d291e72007-05-02 19:27:06 +020026#endif /* !__ASSEMBLY__ */
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028/*
29 * PGDIR_SHIFT determines what a top-level page table entry can map
30 */
31#define PGDIR_SHIFT 39
32#define PTRS_PER_PGD 512
33
34/*
35 * 3rd level page
36 */
37#define PUD_SHIFT 30
38#define PTRS_PER_PUD 512
39
40/*
41 * PMD_SHIFT determines the size of the area a middle-level
42 * page table can map
43 */
44#define PMD_SHIFT 21
45#define PTRS_PER_PMD 512
46
47/*
48 * entries per page directory level
49 */
50#define PTRS_PER_PTE 512
51
Vivek Goyal9d291e72007-05-02 19:27:06 +020052#ifndef __ASSEMBLY__
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define pte_ERROR(e) \
55 printk("%s:%d: bad pte %p(%016lx).\n", __FILE__, __LINE__, &(e), pte_val(e))
56#define pmd_ERROR(e) \
57 printk("%s:%d: bad pmd %p(%016lx).\n", __FILE__, __LINE__, &(e), pmd_val(e))
58#define pud_ERROR(e) \
59 printk("%s:%d: bad pud %p(%016lx).\n", __FILE__, __LINE__, &(e), pud_val(e))
60#define pgd_ERROR(e) \
61 printk("%s:%d: bad pgd %p(%016lx).\n", __FILE__, __LINE__, &(e), pgd_val(e))
62
63#define pgd_none(x) (!pgd_val(x))
64#define pud_none(x) (!pud_val(x))
65
66static inline void set_pte(pte_t *dst, pte_t val)
67{
Jeremy Fitzhardingec8e53932008-01-30 13:32:57 +010068 *dst = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70#define set_pte_at(mm,addr,ptep,pteval) set_pte(ptep,pteval)
71
72static inline void set_pmd(pmd_t *dst, pmd_t val)
73{
Ingo Molnar7a2389b2008-01-30 13:32:42 +010074 *dst = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
77static inline void set_pud(pud_t *dst, pud_t val)
78{
Ingo Molnar7a2389b2008-01-30 13:32:42 +010079 *dst = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
Adrian Bunk9c0aa0f2005-09-12 18:49:24 +020082static inline void pud_clear (pud_t *pud)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
84 set_pud(pud, __pud(0));
85}
86
87static inline void set_pgd(pgd_t *dst, pgd_t val)
88{
Ingo Molnar7a2389b2008-01-30 13:32:42 +010089 *dst = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
Adrian Bunk9c0aa0f2005-09-12 18:49:24 +020092static inline void pgd_clear (pgd_t * pgd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
94 set_pgd(pgd, __pgd(0));
95}
96
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +010097#define native_ptep_get_and_clear(xp) __pte(xchg(&(xp)->pte, 0))
Zachary Amsden61e06032005-09-03 15:55:06 -070098
Tim Schmielau8c65b4a2005-11-07 00:59:43 -080099struct mm_struct;
100
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +0100101static inline pte_t native_ptep_get_and_clear_full(struct mm_struct *mm, unsigned long addr, pte_t *ptep, int full)
Zachary Amsden61e06032005-09-03 15:55:06 -0700102{
103 pte_t pte;
104 if (full) {
105 pte = *ptep;
106 *ptep = __pte(0);
107 } else {
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +0100108 pte = native_ptep_get_and_clear(ptep);
Zachary Amsden61e06032005-09-03 15:55:06 -0700109 }
110 return pte;
111}
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#define pte_same(a, b) ((a).pte == (b).pte)
114
Arjan van de Venc7282522006-01-06 00:12:03 -0800115#define pte_pgprot(a) (__pgprot((a).pte & ~PHYSICAL_PAGE_MASK))
116
Vivek Goyal9d291e72007-05-02 19:27:06 +0200117#endif /* !__ASSEMBLY__ */
118
119#define PMD_SIZE (_AC(1,UL) << PMD_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120#define PMD_MASK (~(PMD_SIZE-1))
Vivek Goyal9d291e72007-05-02 19:27:06 +0200121#define PUD_SIZE (_AC(1,UL) << PUD_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#define PUD_MASK (~(PUD_SIZE-1))
Vivek Goyal9d291e72007-05-02 19:27:06 +0200123#define PGDIR_SIZE (_AC(1,UL) << PGDIR_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#define PGDIR_MASK (~(PGDIR_SIZE-1))
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Randy Dunlap63f65642007-05-08 00:31:14 -0700127#define MAXMEM _AC(0x3fffffffffff, UL)
128#define VMALLOC_START _AC(0xffffc20000000000, UL)
129#define VMALLOC_END _AC(0xffffe1ffffffffff, UL)
Christoph Lameter0889eba2007-10-16 01:24:15 -0700130#define VMEMMAP_START _AC(0xffffe20000000000, UL)
Randy Dunlap63f65642007-05-08 00:31:14 -0700131#define MODULES_VADDR _AC(0xffffffff88000000, UL)
132#define MODULES_END _AC(0xfffffffffff00000, UL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#define MODULES_LEN (MODULES_END - MODULES_VADDR)
134
Vivek Goyal9d291e72007-05-02 19:27:06 +0200135#ifndef __ASSEMBLY__
136
Jan Beulicheab724e2006-12-07 02:14:09 +0100137static inline unsigned long pgd_bad(pgd_t pgd)
138{
139 return pgd_val(pgd) & ~(PTE_MASK | _KERNPG_TABLE | _PAGE_USER);
140}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142static inline unsigned long pud_bad(pud_t pud)
143{
Jan Beulicheab724e2006-12-07 02:14:09 +0100144 return pud_val(pud) & ~(PTE_MASK | _KERNPG_TABLE | _PAGE_USER);
145}
146
147static inline unsigned long pmd_bad(pmd_t pmd)
148{
149 return pmd_val(pmd) & ~(PTE_MASK | _KERNPG_TABLE | _PAGE_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
152#define pte_none(x) (!pte_val(x))
153#define pte_present(x) (pte_val(x) & (_PAGE_PRESENT | _PAGE_PROTNONE))
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +0100154#define native_pte_clear(mm,addr,xp) do { set_pte_at(mm, addr, xp, __pte(0)); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Joerg Roedel1c6f7032008-01-30 13:31:02 +0100156#define pages_to_mb(x) ((x) >> (20-PAGE_SHIFT)) /* FIXME: is this right? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#define pte_page(x) pfn_to_page(pte_pfn(x))
Andi Kleen6b75aee2005-11-05 17:25:53 +0100158#define pte_pfn(x) ((pte_val(x) & __PHYSICAL_MASK) >> PAGE_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160/*
161 * Macro to mark a page protection value as "uncacheable".
162 */
163#define pgprot_noncached(prot) (__pgprot(pgprot_val(prot) | _PAGE_PCD | _PAGE_PWT))
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166/*
167 * Conversion functions: convert a page and protection to a page entry,
168 * and a page entry and page directory to the page they refer to.
169 */
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171/*
172 * Level 4 access.
173 */
Dave McCracken46a82b22006-09-25 23:31:48 -0700174#define pgd_page_vaddr(pgd) ((unsigned long) __va((unsigned long)pgd_val(pgd) & PTE_MASK))
175#define pgd_page(pgd) (pfn_to_page(pgd_val(pgd) >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176#define pgd_index(address) (((address) >> PGDIR_SHIFT) & (PTRS_PER_PGD-1))
177#define pgd_offset(mm, addr) ((mm)->pgd + pgd_index(addr))
178#define pgd_offset_k(address) (init_level4_pgt + pgd_index(address))
179#define pgd_present(pgd) (pgd_val(pgd) & _PAGE_PRESENT)
180#define mk_kernel_pgd(address) ((pgd_t){ (address) | _KERNPG_TABLE })
181
182/* PUD - Level3 access */
183/* to find an entry in a page-table-directory. */
Dave McCracken46a82b22006-09-25 23:31:48 -0700184#define pud_page_vaddr(pud) ((unsigned long) __va(pud_val(pud) & PHYSICAL_PAGE_MASK))
185#define pud_page(pud) (pfn_to_page(pud_val(pud) >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186#define pud_index(address) (((address) >> PUD_SHIFT) & (PTRS_PER_PUD-1))
Dave McCracken46a82b22006-09-25 23:31:48 -0700187#define pud_offset(pgd, address) ((pud_t *) pgd_page_vaddr(*(pgd)) + pud_index(address))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188#define pud_present(pud) (pud_val(pud) & _PAGE_PRESENT)
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/* PMD - Level 2 access */
Dave McCracken46a82b22006-09-25 23:31:48 -0700191#define pmd_page_vaddr(pmd) ((unsigned long) __va(pmd_val(pmd) & PTE_MASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192#define pmd_page(pmd) (pfn_to_page(pmd_val(pmd) >> PAGE_SHIFT))
193
194#define pmd_index(address) (((address) >> PMD_SHIFT) & (PTRS_PER_PMD-1))
Dave McCracken46a82b22006-09-25 23:31:48 -0700195#define pmd_offset(dir, address) ((pmd_t *) pud_page_vaddr(*(dir)) + \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 pmd_index(address))
197#define pmd_none(x) (!pmd_val(x))
198#define pmd_present(x) (pmd_val(x) & _PAGE_PRESENT)
199#define pmd_clear(xp) do { set_pmd(xp, __pmd(0)); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200#define pfn_pmd(nr,prot) (__pmd(((nr) << PAGE_SHIFT) | pgprot_val(prot)))
Andi Kleen6b75aee2005-11-05 17:25:53 +0100201#define pmd_pfn(x) ((pmd_val(x) & __PHYSICAL_MASK) >> PAGE_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203#define pte_to_pgoff(pte) ((pte_val(pte) & PHYSICAL_PAGE_MASK) >> PAGE_SHIFT)
Jeremy Fitzhardingec8e53932008-01-30 13:32:57 +0100204#define pgoff_to_pte(off) ((pte_t) { .pte = ((off) << PAGE_SHIFT) | _PAGE_FILE })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205#define PTE_FILE_MAX_BITS __PHYSICAL_MASK_SHIFT
206
207/* PTE - Level 1 access. */
208
209/* page, protection -> pte */
210#define mk_pte(page, pgprot) pfn_pte(page_to_pfn(page), (pgprot))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212#define pte_index(address) \
Kirill Korotaev1294b112005-09-30 10:32:19 +0400213 (((address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1))
Dave McCracken46a82b22006-09-25 23:31:48 -0700214#define pte_offset_kernel(dir, address) ((pte_t *) pmd_page_vaddr(*(dir)) + \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 pte_index(address))
216
217/* x86-64 always has all page tables mapped. */
218#define pte_offset_map(dir,address) pte_offset_kernel(dir,address)
219#define pte_offset_map_nested(dir,address) pte_offset_kernel(dir,address)
220#define pte_unmap(pte) /* NOP */
221#define pte_unmap_nested(pte) /* NOP */
222
223#define update_mmu_cache(vma,address,pte) do { } while (0)
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225/* Encode and de-code a swap entry */
226#define __swp_type(x) (((x).val >> 1) & 0x3f)
227#define __swp_offset(x) ((x).val >> 8)
228#define __swp_entry(type, offset) ((swp_entry_t) { ((type) << 1) | ((offset) << 8) })
229#define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
Jeremy Fitzhardingec8e53932008-01-30 13:32:57 +0100230#define __swp_entry_to_pte(x) ((pte_t) { .pte = (x).val })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Jan Beulich8c914cb2006-03-25 16:29:40 +0100232extern spinlock_t pgd_lock;
Christoph Lameter2bff7382007-05-02 19:27:10 +0200233extern struct list_head pgd_list;
Jan Beulich8c914cb2006-03-25 16:29:40 +0100234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235extern int kern_addr_valid(unsigned long addr);
236
Andi Kleen19d36cc2007-07-22 11:12:31 +0200237pte_t *lookup_address(unsigned long addr);
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239#define io_remap_pfn_range(vma, vaddr, pfn, size, prot) \
240 remap_pfn_range(vma, vaddr, pfn, size, prot)
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242#define HAVE_ARCH_UNMAPPED_AREA
Jiri Kosinacc503c12008-01-30 13:31:07 +0100243#define HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245#define pgtable_cache_init() do { } while (0)
Linus Torvaldsda8f1532007-09-21 12:09:41 -0700246#define check_pgt_cache() do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248#define PAGE_AGP PAGE_KERNEL_NOCACHE
249#define HAVE_PAGE_AGP 1
250
251/* fs/proc/kcore.c */
252#define kc_vaddr_to_offset(v) ((v) & __VIRTUAL_MASK)
253#define kc_offset_to_vaddr(o) \
254 (((o) & (1UL << (__VIRTUAL_MASK_SHIFT-1))) ? ((o) | (~__VIRTUAL_MASK)) : (o))
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256#define __HAVE_ARCH_PTE_SAME
Vivek Goyal9d291e72007-05-02 19:27:06 +0200257#endif /* !__ASSEMBLY__ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259#endif /* _X86_64_PGTABLE_H */