Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Jeff Dike | d83ecf0 | 2008-02-04 22:30:47 -0800 | [diff] [blame^] | 2 | * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | |
| 13 | #define _PAGE_PRESENT 0x001 |
| 14 | #define _PAGE_NEWPAGE 0x002 |
Paolo 'Blaisorblade' Giarrusso | 9b4ee40 | 2005-09-03 15:54:57 -0700 | [diff] [blame] | 15 | #define _PAGE_NEWPROT 0x004 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #define _PAGE_RW 0x020 |
| 17 | #define _PAGE_USER 0x040 |
| 18 | #define _PAGE_ACCESSED 0x080 |
| 19 | #define _PAGE_DIRTY 0x100 |
Paolo 'Blaisorblade' Giarrusso | 9b4ee40 | 2005-09-03 15:54:57 -0700 | [diff] [blame] | 20 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | #ifdef CONFIG_3_LEVEL_PGTABLES |
| 26 | #include "asm/pgtable-3level.h" |
| 27 | #else |
| 28 | #include "asm/pgtable-2level.h" |
| 29 | #endif |
| 30 | |
| 31 | extern pgd_t swapper_pg_dir[PTRS_PER_PGD]; |
| 32 | |
| 33 | extern 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 */ |
| 37 | extern unsigned long *empty_zero_page; |
| 38 | |
| 39 | #define pgtable_cache_init() do ; while (0) |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | /* 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 | |
| 49 | extern unsigned long end_iomem; |
| 50 | |
| 51 | #define VMALLOC_OFFSET (__va_space) |
| 52 | #define VMALLOC_START ((end_iomem + VMALLOC_OFFSET) & ~(VMALLOC_OFFSET-1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
| 69 | /* |
Jeff Dike | d83ecf0 | 2008-02-04 22:30:47 -0800 | [diff] [blame^] | 70 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | */ |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | * ZERO_PAGE is a global shared page that is always zero: used |
| 95 | * for zero-mapped memory areas etc.. |
| 96 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | #define ZERO_PAGE(vaddr) virt_to_page(empty_zero_page) |
| 98 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | #define pte_clear(mm,addr,xp) pte_set_val(*(xp), (phys_t) 0, __pgprot(_PAGE_NEWPAGE)) |
| 100 | |
Hugh Dickins | 705e87c | 2005-10-29 18:16:27 -0700 | [diff] [blame] | 101 | #define pmd_none(x) (!((unsigned long)pmd_val(x) & ~_PAGE_NEWPAGE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | #define pmd_bad(x) ((pmd_val(x) & (~PAGE_MASK & ~_PAGE_USER)) != _KERNPG_TABLE) |
Jeff Dike | d83ecf0 | 2008-02-04 22:30:47 -0800 | [diff] [blame^] | 103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | #define pmd_page(pmd) phys_to_page(pmd_val(pmd) & PAGE_MASK) |
| 114 | |
Jeff Dike | 08964c5 | 2005-09-03 15:57:41 -0700 | [diff] [blame] | 115 | #define pte_page(x) pfn_to_page(pte_pfn(x)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
Jeff Dike | 08964c5 | 2005-09-03 15:57:41 -0700 | [diff] [blame] | 117 | #define pte_present(x) pte_get_bits(x, (_PAGE_PRESENT | _PAGE_PROTNONE)) |
| 118 | |
| 119 | /* |
| 120 | * ================================= |
| 121 | * Flags checking section. |
| 122 | * ================================= |
| 123 | */ |
| 124 | |
| 125 | static inline int pte_none(pte_t pte) |
| 126 | { |
| 127 | return pte_is_zero(pte); |
| 128 | } |
| 129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | /* |
| 131 | * The following only work if pte_present() is true. |
| 132 | * Undefined behaviour if not.. |
| 133 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | static 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 | |
| 140 | static inline int pte_exec(pte_t pte){ |
| 141 | return((pte_get_bits(pte, _PAGE_USER)) && |
| 142 | !(pte_get_bits(pte, _PAGE_PROTNONE))); |
| 143 | } |
| 144 | |
| 145 | static 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 | */ |
| 154 | static inline int pte_file(pte_t pte) |
| 155 | { |
| 156 | return pte_get_bits(pte, _PAGE_FILE); |
| 157 | } |
| 158 | |
| 159 | static inline int pte_dirty(pte_t pte) |
| 160 | { |
| 161 | return pte_get_bits(pte, _PAGE_DIRTY); |
| 162 | } |
| 163 | |
| 164 | static inline int pte_young(pte_t pte) |
| 165 | { |
| 166 | return pte_get_bits(pte, _PAGE_ACCESSED); |
| 167 | } |
| 168 | |
| 169 | static inline int pte_newpage(pte_t pte) |
| 170 | { |
| 171 | return pte_get_bits(pte, _PAGE_NEWPAGE); |
| 172 | } |
| 173 | |
| 174 | static inline int pte_newprot(pte_t pte) |
| 175 | { |
| 176 | return(pte_present(pte) && (pte_get_bits(pte, _PAGE_NEWPROT))); |
| 177 | } |
| 178 | |
Jeff Dike | 08964c5 | 2005-09-03 15:57:41 -0700 | [diff] [blame] | 179 | /* |
| 180 | * ================================= |
| 181 | * Flags setting section. |
| 182 | * ================================= |
| 183 | */ |
| 184 | |
| 185 | static inline pte_t pte_mknewprot(pte_t pte) |
| 186 | { |
| 187 | pte_set_bits(pte, _PAGE_NEWPROT); |
| 188 | return(pte); |
| 189 | } |
| 190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | static inline pte_t pte_mkclean(pte_t pte) |
| 192 | { |
| 193 | pte_clear_bits(pte, _PAGE_DIRTY); |
| 194 | return(pte); |
| 195 | } |
| 196 | |
| 197 | static inline pte_t pte_mkold(pte_t pte) |
| 198 | { |
| 199 | pte_clear_bits(pte, _PAGE_ACCESSED); |
| 200 | return(pte); |
| 201 | } |
| 202 | |
| 203 | static inline pte_t pte_wrprotect(pte_t pte) |
| 204 | { |
| 205 | pte_clear_bits(pte, _PAGE_RW); |
| 206 | return(pte_mknewprot(pte)); |
| 207 | } |
| 208 | |
| 209 | static inline pte_t pte_mkread(pte_t pte) |
| 210 | { |
Jeff Dike | 1463fdb | 2007-02-28 20:13:33 -0800 | [diff] [blame] | 211 | pte_set_bits(pte, _PAGE_USER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | return(pte_mknewprot(pte)); |
| 213 | } |
| 214 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | static inline pte_t pte_mkdirty(pte_t pte) |
| 216 | { |
| 217 | pte_set_bits(pte, _PAGE_DIRTY); |
| 218 | return(pte); |
| 219 | } |
| 220 | |
| 221 | static inline pte_t pte_mkyoung(pte_t pte) |
| 222 | { |
| 223 | pte_set_bits(pte, _PAGE_ACCESSED); |
| 224 | return(pte); |
| 225 | } |
| 226 | |
| 227 | static inline pte_t pte_mkwrite(pte_t pte) |
| 228 | { |
| 229 | pte_set_bits(pte, _PAGE_RW); |
| 230 | return(pte_mknewprot(pte)); |
| 231 | } |
| 232 | |
| 233 | static 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 Dike | 08964c5 | 2005-09-03 15:57:41 -0700 | [diff] [blame] | 241 | static inline pte_t pte_mknewpage(pte_t pte) |
| 242 | { |
| 243 | pte_set_bits(pte, _PAGE_NEWPAGE); |
| 244 | return(pte); |
| 245 | } |
| 246 | |
| 247 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | /* |
| 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' Giarrusso | d99c402 | 2005-09-10 19:44:56 +0200 | [diff] [blame] | 266 | #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 Dike | d83ecf0 | 2008-02-04 22:30:47 -0800 | [diff] [blame^] | 269 | #define virt_to_page(addr) __virt_to_page((const unsigned long) addr) |
Paolo 'Blaisorblade' Giarrusso | d99c402 | 2005-09-10 19:44:56 +0200 | [diff] [blame] | 270 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | |
| 279 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | return pte; |
| 283 | } |
| 284 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | /* |
| 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 McCracken | 46a82b2 | 2006-09-25 23:31:48 -0700 | [diff] [blame] | 321 | ((pte_t *) pmd_page_vaddr(*(dir)) + pte_index(address)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | #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 Viro | 04add67 | 2007-02-01 13:53:04 +0000 | [diff] [blame] | 344 | #endif |