blob: cb0d2048eca66218e48c3fe3aa43f97b6b41d7c7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jeff Diked83ecf02008-02-04 22:30:47 -08002 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright 2003 PathScale, Inc.
4 * Derived from include/asm-i386/pgtable.h
5 * Licensed under the GPL
6 */
7
8#ifndef __UM_PGTABLE_H
9#define __UM_PGTABLE_H
10
11#include "linux/sched.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13#define _PAGE_PRESENT 0x001
14#define _PAGE_NEWPAGE 0x002
Paolo 'Blaisorblade' Giarrusso9b4ee402005-09-03 15:54:57 -070015#define _PAGE_NEWPROT 0x004
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#define _PAGE_RW 0x020
17#define _PAGE_USER 0x040
18#define _PAGE_ACCESSED 0x080
19#define _PAGE_DIRTY 0x100
Paolo 'Blaisorblade' Giarrusso9b4ee402005-09-03 15:54:57 -070020/* If _PAGE_PRESENT is clear, we use these: */
21#define _PAGE_FILE 0x008 /* nonlinear file mapping, saved PTE; unset:swap */
22#define _PAGE_PROTNONE 0x010 /* if the user mapped it with PROT_NONE;
23 pte_present gives true */
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#ifdef CONFIG_3_LEVEL_PGTABLES
26#include "asm/pgtable-3level.h"
27#else
28#include "asm/pgtable-2level.h"
29#endif
30
31extern pgd_t swapper_pg_dir[PTRS_PER_PGD];
32
33extern void *um_virt_to_phys(struct task_struct *task, unsigned long virt,
34 pte_t *pte_out);
35
36/* zero page used for uninitialized stuff */
37extern unsigned long *empty_zero_page;
38
39#define pgtable_cache_init() do ; while (0)
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/* Just any arbitrary offset to the start of the vmalloc VM area: the
42 * current 8MB value just means that there will be a 8MB "hole" after the
43 * physical memory until the kernel virtual memory starts. That means that
44 * any out-of-bounds memory accesses will hopefully be caught.
45 * The vmalloc() routines leaves a hole of 4kB between each vmalloced
46 * area for the same reason. ;)
47 */
48
49extern unsigned long end_iomem;
50
51#define VMALLOC_OFFSET (__va_space)
52#define VMALLOC_START ((end_iomem + VMALLOC_OFFSET) & ~(VMALLOC_OFFSET-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#ifdef CONFIG_HIGHMEM
54# define VMALLOC_END (PKMAP_BASE-2*PAGE_SIZE)
55#else
56# define VMALLOC_END (FIXADDR_START-2*PAGE_SIZE)
57#endif
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#define _PAGE_TABLE (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | _PAGE_ACCESSED | _PAGE_DIRTY)
60#define _KERNPG_TABLE (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY)
61#define _PAGE_CHG_MASK (PAGE_MASK | _PAGE_ACCESSED | _PAGE_DIRTY)
62
63#define PAGE_NONE __pgprot(_PAGE_PROTNONE | _PAGE_ACCESSED)
64#define PAGE_SHARED __pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | _PAGE_ACCESSED)
65#define PAGE_COPY __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_ACCESSED)
66#define PAGE_READONLY __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_ACCESSED)
67#define PAGE_KERNEL __pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY | _PAGE_ACCESSED)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/*
Jeff Diked83ecf02008-02-04 22:30:47 -080070 * The i386 can't do page protection for execute, and considers that the same
71 * are read.
72 * Also, write permissions imply read permissions. This is the closest we can
73 * get..
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 */
75#define __P000 PAGE_NONE
76#define __P001 PAGE_READONLY
77#define __P010 PAGE_COPY
78#define __P011 PAGE_COPY
79#define __P100 PAGE_READONLY
80#define __P101 PAGE_READONLY
81#define __P110 PAGE_COPY
82#define __P111 PAGE_COPY
83
84#define __S000 PAGE_NONE
85#define __S001 PAGE_READONLY
86#define __S010 PAGE_SHARED
87#define __S011 PAGE_SHARED
88#define __S100 PAGE_READONLY
89#define __S101 PAGE_READONLY
90#define __S110 PAGE_SHARED
91#define __S111 PAGE_SHARED
92
93/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 * ZERO_PAGE is a global shared page that is always zero: used
95 * for zero-mapped memory areas etc..
96 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#define ZERO_PAGE(vaddr) virt_to_page(empty_zero_page)
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#define pte_clear(mm,addr,xp) pte_set_val(*(xp), (phys_t) 0, __pgprot(_PAGE_NEWPAGE))
100
Hugh Dickins705e87c2005-10-29 18:16:27 -0700101#define pmd_none(x) (!((unsigned long)pmd_val(x) & ~_PAGE_NEWPAGE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#define pmd_bad(x) ((pmd_val(x) & (~PAGE_MASK & ~_PAGE_USER)) != _KERNPG_TABLE)
Jeff Diked83ecf02008-02-04 22:30:47 -0800103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#define pmd_present(x) (pmd_val(x) & _PAGE_PRESENT)
105#define pmd_clear(xp) do { pmd_val(*(xp)) = _PAGE_NEWPAGE; } while (0)
106
107#define pmd_newpage(x) (pmd_val(x) & _PAGE_NEWPAGE)
108#define pmd_mkuptodate(x) (pmd_val(x) &= ~_PAGE_NEWPAGE)
109
110#define pud_newpage(x) (pud_val(x) & _PAGE_NEWPAGE)
111#define pud_mkuptodate(x) (pud_val(x) &= ~_PAGE_NEWPAGE)
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#define pmd_page(pmd) phys_to_page(pmd_val(pmd) & PAGE_MASK)
114
Jeff Dike08964c52005-09-03 15:57:41 -0700115#define pte_page(x) pfn_to_page(pte_pfn(x))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Jeff Dike08964c52005-09-03 15:57:41 -0700117#define pte_present(x) pte_get_bits(x, (_PAGE_PRESENT | _PAGE_PROTNONE))
118
119/*
120 * =================================
121 * Flags checking section.
122 * =================================
123 */
124
125static inline int pte_none(pte_t pte)
126{
127 return pte_is_zero(pte);
128}
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130/*
131 * The following only work if pte_present() is true.
132 * Undefined behaviour if not..
133 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134static inline int pte_read(pte_t pte)
135{
136 return((pte_get_bits(pte, _PAGE_USER)) &&
137 !(pte_get_bits(pte, _PAGE_PROTNONE)));
138}
139
140static inline int pte_exec(pte_t pte){
141 return((pte_get_bits(pte, _PAGE_USER)) &&
142 !(pte_get_bits(pte, _PAGE_PROTNONE)));
143}
144
145static inline int pte_write(pte_t pte)
146{
147 return((pte_get_bits(pte, _PAGE_RW)) &&
148 !(pte_get_bits(pte, _PAGE_PROTNONE)));
149}
150
151/*
152 * The following only works if pte_present() is not true.
153 */
154static inline int pte_file(pte_t pte)
155{
156 return pte_get_bits(pte, _PAGE_FILE);
157}
158
159static inline int pte_dirty(pte_t pte)
160{
161 return pte_get_bits(pte, _PAGE_DIRTY);
162}
163
164static inline int pte_young(pte_t pte)
165{
166 return pte_get_bits(pte, _PAGE_ACCESSED);
167}
168
169static inline int pte_newpage(pte_t pte)
170{
171 return pte_get_bits(pte, _PAGE_NEWPAGE);
172}
173
174static inline int pte_newprot(pte_t pte)
175{
176 return(pte_present(pte) && (pte_get_bits(pte, _PAGE_NEWPROT)));
177}
178
Jeff Dike08964c52005-09-03 15:57:41 -0700179/*
180 * =================================
181 * Flags setting section.
182 * =================================
183 */
184
185static inline pte_t pte_mknewprot(pte_t pte)
186{
187 pte_set_bits(pte, _PAGE_NEWPROT);
188 return(pte);
189}
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191static inline pte_t pte_mkclean(pte_t pte)
192{
193 pte_clear_bits(pte, _PAGE_DIRTY);
194 return(pte);
195}
196
197static inline pte_t pte_mkold(pte_t pte)
198{
199 pte_clear_bits(pte, _PAGE_ACCESSED);
200 return(pte);
201}
202
203static inline pte_t pte_wrprotect(pte_t pte)
204{
205 pte_clear_bits(pte, _PAGE_RW);
206 return(pte_mknewprot(pte));
207}
208
209static inline pte_t pte_mkread(pte_t pte)
210{
Jeff Dike1463fdb2007-02-28 20:13:33 -0800211 pte_set_bits(pte, _PAGE_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return(pte_mknewprot(pte));
213}
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215static inline pte_t pte_mkdirty(pte_t pte)
216{
217 pte_set_bits(pte, _PAGE_DIRTY);
218 return(pte);
219}
220
221static inline pte_t pte_mkyoung(pte_t pte)
222{
223 pte_set_bits(pte, _PAGE_ACCESSED);
224 return(pte);
225}
226
227static inline pte_t pte_mkwrite(pte_t pte)
228{
229 pte_set_bits(pte, _PAGE_RW);
230 return(pte_mknewprot(pte));
231}
232
233static inline pte_t pte_mkuptodate(pte_t pte)
234{
235 pte_clear_bits(pte, _PAGE_NEWPAGE);
236 if(pte_present(pte))
237 pte_clear_bits(pte, _PAGE_NEWPROT);
238 return(pte);
239}
240
Jeff Dike08964c52005-09-03 15:57:41 -0700241static inline pte_t pte_mknewpage(pte_t pte)
242{
243 pte_set_bits(pte, _PAGE_NEWPAGE);
244 return(pte);
245}
246
247static inline void set_pte(pte_t *pteptr, pte_t pteval)
248{
249 pte_copy(*pteptr, pteval);
250
251 /* If it's a swap entry, it needs to be marked _PAGE_NEWPAGE so
252 * fix_range knows to unmap it. _PAGE_NEWPROT is specific to
253 * mapped pages.
254 */
255
256 *pteptr = pte_mknewpage(*pteptr);
257 if(pte_present(*pteptr)) *pteptr = pte_mknewprot(*pteptr);
258}
259#define set_pte_at(mm,addr,ptep,pteval) set_pte(ptep,pteval)
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261/*
262 * Conversion functions: convert a page and protection to a page entry,
263 * and a page entry and page directory to the page they refer to.
264 */
265
Paolo 'Blaisorblade' Giarrussod99c4022005-09-10 19:44:56 +0200266#define phys_to_page(phys) pfn_to_page(phys_to_pfn(phys))
267#define __virt_to_page(virt) phys_to_page(__pa(virt))
268#define page_to_phys(page) pfn_to_phys(page_to_pfn(page))
Jeff Diked83ecf02008-02-04 22:30:47 -0800269#define virt_to_page(addr) __virt_to_page((const unsigned long) addr)
Paolo 'Blaisorblade' Giarrussod99c4022005-09-10 19:44:56 +0200270
271#define mk_pte(page, pgprot) \
272 ({ pte_t pte; \
273 \
274 pte_set_val(pte, page_to_phys(page), (pgprot)); \
275 if (pte_present(pte)) \
276 pte_mknewprot(pte_mknewpage(pte)); \
277 pte;})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
280{
281 pte_set_val(pte, (pte_val(pte) & _PAGE_CHG_MASK), newprot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return pte;
283}
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285/*
286 * the pgd page can be thought of an array like this: pgd_t[PTRS_PER_PGD]
287 *
288 * this macro returns the index of the entry in the pgd page which would
289 * control the given virtual address
290 */
291#define pgd_index(address) (((address) >> PGDIR_SHIFT) & (PTRS_PER_PGD-1))
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293/*
294 * pgd_offset() returns a (pgd_t *)
295 * pgd_index() is used get the offset into the pgd page's array of pgd_t's;
296 */
297#define pgd_offset(mm, address) ((mm)->pgd+pgd_index(address))
298
299/*
300 * a shortcut which implies the use of the kernel's pgd, instead
301 * of a process's
302 */
303#define pgd_offset_k(address) pgd_offset(&init_mm, address)
304
305/*
306 * the pmd page can be thought of an array like this: pmd_t[PTRS_PER_PMD]
307 *
308 * this macro returns the index of the entry in the pmd page which would
309 * control the given virtual address
310 */
311#define pmd_index(address) (((address) >> PMD_SHIFT) & (PTRS_PER_PMD-1))
312
313/*
314 * the pte page can be thought of an array like this: pte_t[PTRS_PER_PTE]
315 *
316 * this macro returns the index of the entry in the pte page which would
317 * control the given virtual address
318 */
319#define pte_index(address) (((address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1))
320#define pte_offset_kernel(dir, address) \
Dave McCracken46a82b22006-09-25 23:31:48 -0700321 ((pte_t *) pmd_page_vaddr(*(dir)) + pte_index(address))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322#define pte_offset_map(dir, address) \
323 ((pte_t *)page_address(pmd_page(*(dir))) + pte_index(address))
324#define pte_offset_map_nested(dir, address) pte_offset_map(dir, address)
325#define pte_unmap(pte) do { } while (0)
326#define pte_unmap_nested(pte) do { } while (0)
327
328#define update_mmu_cache(vma,address,pte) do ; while (0)
329
330/* Encode and de-code a swap entry */
331#define __swp_type(x) (((x).val >> 4) & 0x3f)
332#define __swp_offset(x) ((x).val >> 11)
333
334#define __swp_entry(type, offset) \
335 ((swp_entry_t) { ((type) << 4) | ((offset) << 11) })
336#define __pte_to_swp_entry(pte) \
337 ((swp_entry_t) { pte_val(pte_mkuptodate(pte)) })
338#define __swp_entry_to_pte(x) ((pte_t) { (x).val })
339
340#define kern_addr_valid(addr) (1)
341
342#include <asm-generic/pgtable.h>
343
Al Viro04add672007-02-01 13:53:04 +0000344#endif