Jeff Dike | 114069f | 2005-09-16 19:27:51 -0700 | [diff] [blame] | 1 | /* |
Jeff Dike | 009ec2a9 | 2008-02-04 22:30:53 -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 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
Jeff Dike | 009ec2a9 | 2008-02-04 22:30:53 -0800 | [diff] [blame] | 6 | #include <linux/stddef.h> |
Al Viro | 73395a0 | 2011-08-18 20:14:10 +0100 | [diff] [blame] | 7 | #include <linux/module.h> |
Jeff Dike | 009ec2a9 | 2008-02-04 22:30:53 -0800 | [diff] [blame] | 8 | #include <linux/bootmem.h> |
Jeff Dike | 009ec2a9 | 2008-02-04 22:30:53 -0800 | [diff] [blame] | 9 | #include <linux/highmem.h> |
| 10 | #include <linux/mm.h> |
| 11 | #include <linux/swap.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/slab.h> |
Jeff Dike | 009ec2a9 | 2008-02-04 22:30:53 -0800 | [diff] [blame] | 13 | #include <asm/fixmap.h> |
| 14 | #include <asm/page.h> |
Al Viro | 37185b3 | 2012-10-08 03:27:32 +0100 | [diff] [blame] | 15 | #include <as-layout.h> |
| 16 | #include <init.h> |
| 17 | #include <kern.h> |
| 18 | #include <kern_util.h> |
| 19 | #include <mem_user.h> |
| 20 | #include <os.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Jeff Dike | 6bf7948 | 2007-02-10 01:44:18 -0800 | [diff] [blame] | 22 | /* allocated in paging_init, zeroed in mem_init, and unchanged thereafter */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | unsigned long *empty_zero_page = NULL; |
Al Viro | 73395a0 | 2011-08-18 20:14:10 +0100 | [diff] [blame] | 24 | EXPORT_SYMBOL(empty_zero_page); |
Jeff Dike | 6bf7948 | 2007-02-10 01:44:18 -0800 | [diff] [blame] | 25 | /* allocated in paging_init and unchanged thereafter */ |
WANG Cong | 4a56758 | 2008-07-23 21:28:49 -0700 | [diff] [blame] | 26 | static unsigned long *empty_bad_page = NULL; |
Jeff Dike | 80e3931 | 2008-02-04 22:31:17 -0800 | [diff] [blame] | 27 | |
| 28 | /* |
| 29 | * Initialized during boot, and readonly for initializing page tables |
| 30 | * afterwards |
| 31 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | pgd_t swapper_pg_dir[PTRS_PER_PGD]; |
Jeff Dike | 80e3931 | 2008-02-04 22:31:17 -0800 | [diff] [blame] | 33 | |
| 34 | /* Initialized at boot time, and readonly after that */ |
Jeff Dike | 9902abd | 2006-03-31 02:30:09 -0800 | [diff] [blame] | 35 | unsigned long long highmem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | int kmalloc_ok = 0; |
| 37 | |
Jeff Dike | 80e3931 | 2008-02-04 22:31:17 -0800 | [diff] [blame] | 38 | /* Used during early boot */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | static unsigned long brk_end; |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #ifdef CONFIG_HIGHMEM |
| 42 | static void setup_highmem(unsigned long highmem_start, |
| 43 | unsigned long highmem_len) |
| 44 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | unsigned long highmem_pfn; |
| 46 | int i; |
| 47 | |
| 48 | highmem_pfn = __pa(highmem_start) >> PAGE_SHIFT; |
Jiang Liu | 5b472b2 | 2013-04-29 15:07:11 -0700 | [diff] [blame] | 49 | for (i = 0; i < highmem_len >> PAGE_SHIFT; i++) |
| 50 | free_highmem_page(&mem_map[highmem_pfn + i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | } |
| 52 | #endif |
| 53 | |
Jeff Dike | 97a1fcb | 2007-07-23 18:43:48 -0700 | [diff] [blame] | 54 | void __init mem_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | { |
Jeff Dike | 60678bb | 2007-02-10 01:44:10 -0800 | [diff] [blame] | 56 | /* clear the zero-page */ |
WANG Cong | c0a9290 | 2008-02-04 22:30:41 -0800 | [diff] [blame] | 57 | memset(empty_zero_page, 0, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | /* Map in the area just after the brk now that kmalloc is about |
| 60 | * to be turned on. |
| 61 | */ |
| 62 | brk_end = (unsigned long) UML_ROUND_UP(sbrk(0)); |
Jeff Dike | ab26a52 | 2008-02-04 22:31:24 -0800 | [diff] [blame] | 63 | map_memory(brk_end, __pa(brk_end), uml_reserved - brk_end, 1, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | free_bootmem(__pa(brk_end), uml_reserved - brk_end); |
| 65 | uml_reserved = brk_end; |
| 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | /* this will put all low memory onto the freelists */ |
| 68 | totalram_pages = free_all_bootmem(); |
Jason Lunz | af84eab | 2007-03-29 01:20:31 -0700 | [diff] [blame] | 69 | max_low_pfn = totalram_pages; |
Christoph Lameter | c1f60a5 | 2006-09-25 23:31:11 -0700 | [diff] [blame] | 70 | #ifdef CONFIG_HIGHMEM |
Jiang Liu | 5b472b2 | 2013-04-29 15:07:11 -0700 | [diff] [blame] | 71 | setup_highmem(end_iomem, highmem); |
Christoph Lameter | c1f60a5 | 2006-09-25 23:31:11 -0700 | [diff] [blame] | 72 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | num_physpages = totalram_pages; |
| 74 | max_pfn = totalram_pages; |
Jeff Dike | 009ec2a9 | 2008-02-04 22:30:53 -0800 | [diff] [blame] | 75 | printk(KERN_INFO "Memory: %luk available\n", |
Geert Uytterhoeven | cc013a8 | 2009-09-21 17:02:36 -0700 | [diff] [blame] | 76 | nr_free_pages() << (PAGE_SHIFT-10)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | kmalloc_ok = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Jeff Dike | 12f4964 | 2005-05-20 13:59:12 -0700 | [diff] [blame] | 80 | /* |
| 81 | * Create a page table and place a pointer to it in a middle page |
| 82 | * directory entry. |
| 83 | */ |
| 84 | static void __init one_page_table_init(pmd_t *pmd) |
| 85 | { |
| 86 | if (pmd_none(*pmd)) { |
| 87 | pte_t *pte = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE); |
| 88 | set_pmd(pmd, __pmd(_KERNPG_TABLE + |
| 89 | (unsigned long) __pa(pte))); |
| 90 | if (pte != pte_offset_kernel(pmd, 0)) |
| 91 | BUG(); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | static void __init one_md_table_init(pud_t *pud) |
| 96 | { |
| 97 | #ifdef CONFIG_3_LEVEL_PGTABLES |
| 98 | pmd_t *pmd_table = (pmd_t *) alloc_bootmem_low_pages(PAGE_SIZE); |
| 99 | set_pud(pud, __pud(_KERNPG_TABLE + (unsigned long) __pa(pmd_table))); |
| 100 | if (pmd_table != pmd_offset(pud, 0)) |
| 101 | BUG(); |
| 102 | #endif |
| 103 | } |
| 104 | |
Jeff Dike | 009ec2a9 | 2008-02-04 22:30:53 -0800 | [diff] [blame] | 105 | static void __init fixrange_init(unsigned long start, unsigned long end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | pgd_t *pgd_base) |
| 107 | { |
| 108 | pgd_t *pgd; |
Jeff Dike | 12f4964 | 2005-05-20 13:59:12 -0700 | [diff] [blame] | 109 | pud_t *pud; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | pmd_t *pmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | int i, j; |
| 112 | unsigned long vaddr; |
| 113 | |
| 114 | vaddr = start; |
| 115 | i = pgd_index(vaddr); |
| 116 | j = pmd_index(vaddr); |
| 117 | pgd = pgd_base + i; |
| 118 | |
| 119 | for ( ; (i < PTRS_PER_PGD) && (vaddr < end); pgd++, i++) { |
Jeff Dike | 12f4964 | 2005-05-20 13:59:12 -0700 | [diff] [blame] | 120 | pud = pud_offset(pgd, vaddr); |
| 121 | if (pud_none(*pud)) |
| 122 | one_md_table_init(pud); |
| 123 | pmd = pmd_offset(pud, vaddr); |
Jeff Dike | 655e4ed | 2008-02-04 22:30:55 -0800 | [diff] [blame] | 124 | for (; (j < PTRS_PER_PMD) && (vaddr < end); pmd++, j++) { |
Jeff Dike | 12f4964 | 2005-05-20 13:59:12 -0700 | [diff] [blame] | 125 | one_page_table_init(pmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | vaddr += PMD_SIZE; |
| 127 | } |
| 128 | j = 0; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | #ifdef CONFIG_HIGHMEM |
| 133 | pte_t *kmap_pte; |
| 134 | pgprot_t kmap_prot; |
| 135 | |
| 136 | #define kmap_get_fixmap_pte(vaddr) \ |
| 137 | pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr), (vaddr)),\ |
Jeff Dike | 8192ab4 | 2008-02-04 22:30:53 -0800 | [diff] [blame] | 138 | (vaddr)), (vaddr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
| 140 | static void __init kmap_init(void) |
| 141 | { |
| 142 | unsigned long kmap_vstart; |
| 143 | |
| 144 | /* cache the first kmap pte */ |
| 145 | kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN); |
| 146 | kmap_pte = kmap_get_fixmap_pte(kmap_vstart); |
| 147 | |
| 148 | kmap_prot = PAGE_KERNEL; |
| 149 | } |
| 150 | |
Al Viro | b4a08a1 | 2007-10-29 04:36:10 +0000 | [diff] [blame] | 151 | static void __init init_highmem(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | { |
| 153 | pgd_t *pgd; |
| 154 | pud_t *pud; |
| 155 | pmd_t *pmd; |
| 156 | pte_t *pte; |
| 157 | unsigned long vaddr; |
| 158 | |
| 159 | /* |
| 160 | * Permanent kmaps: |
| 161 | */ |
| 162 | vaddr = PKMAP_BASE; |
| 163 | fixrange_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, swapper_pg_dir); |
| 164 | |
| 165 | pgd = swapper_pg_dir + pgd_index(vaddr); |
| 166 | pud = pud_offset(pgd, vaddr); |
| 167 | pmd = pmd_offset(pud, vaddr); |
| 168 | pte = pte_offset_kernel(pmd, vaddr); |
| 169 | pkmap_page_table = pte; |
| 170 | |
| 171 | kmap_init(); |
| 172 | } |
| 173 | #endif /* CONFIG_HIGHMEM */ |
| 174 | |
| 175 | static void __init fixaddr_user_init( void) |
| 176 | { |
viro@ZenIV.linux.org.uk | 9a0b386 | 2005-09-07 23:21:11 +0100 | [diff] [blame] | 177 | #ifdef CONFIG_ARCH_REUSE_HOST_VSYSCALL_AREA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | long size = FIXADDR_USER_END - FIXADDR_USER_START; |
| 179 | pgd_t *pgd; |
| 180 | pud_t *pud; |
| 181 | pmd_t *pmd; |
| 182 | pte_t *pte; |
Jeff Dike | 655e4ed | 2008-02-04 22:30:55 -0800 | [diff] [blame] | 183 | phys_t p; |
| 184 | unsigned long v, vaddr = FIXADDR_USER_START; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
Jeff Dike | 655e4ed | 2008-02-04 22:30:55 -0800 | [diff] [blame] | 186 | if (!size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | return; |
| 188 | |
| 189 | fixrange_init( FIXADDR_USER_START, FIXADDR_USER_END, swapper_pg_dir); |
Jeff Dike | 655e4ed | 2008-02-04 22:30:55 -0800 | [diff] [blame] | 190 | v = (unsigned long) alloc_bootmem_low_pages(size); |
| 191 | memcpy((void *) v , (void *) FIXADDR_USER_START, size); |
| 192 | p = __pa(v); |
Jeff Dike | 009ec2a9 | 2008-02-04 22:30:53 -0800 | [diff] [blame] | 193 | for ( ; size > 0; size -= PAGE_SIZE, vaddr += PAGE_SIZE, |
Jeff Dike | 655e4ed | 2008-02-04 22:30:55 -0800 | [diff] [blame] | 194 | p += PAGE_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | pgd = swapper_pg_dir + pgd_index(vaddr); |
| 196 | pud = pud_offset(pgd, vaddr); |
| 197 | pmd = pmd_offset(pud, vaddr); |
| 198 | pte = pte_offset_kernel(pmd, vaddr); |
Jeff Dike | 655e4ed | 2008-02-04 22:30:55 -0800 | [diff] [blame] | 199 | pte_set_val(*pte, p, PAGE_READONLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | } |
| 201 | #endif |
| 202 | } |
| 203 | |
Jeff Dike | 36e4546 | 2007-05-06 14:51:11 -0700 | [diff] [blame] | 204 | void __init paging_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | { |
| 206 | unsigned long zones_size[MAX_NR_ZONES], vaddr; |
| 207 | int i; |
| 208 | |
| 209 | empty_zero_page = (unsigned long *) alloc_bootmem_low_pages(PAGE_SIZE); |
| 210 | empty_bad_page = (unsigned long *) alloc_bootmem_low_pages(PAGE_SIZE); |
Jeff Dike | 009ec2a9 | 2008-02-04 22:30:53 -0800 | [diff] [blame] | 211 | for (i = 0; i < ARRAY_SIZE(zones_size); i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | zones_size[i] = 0; |
Jeff Dike | 91b165c | 2006-09-25 23:33:00 -0700 | [diff] [blame] | 213 | |
Jeff Dike | 0715501 | 2006-09-27 01:50:34 -0700 | [diff] [blame] | 214 | zones_size[ZONE_NORMAL] = (end_iomem >> PAGE_SHIFT) - |
| 215 | (uml_physmem >> PAGE_SHIFT); |
Christoph Lameter | e53ef38 | 2006-09-25 23:31:14 -0700 | [diff] [blame] | 216 | #ifdef CONFIG_HIGHMEM |
Paolo 'Blaisorblade' Giarrusso | 353f8d1 | 2005-11-07 00:58:58 -0800 | [diff] [blame] | 217 | zones_size[ZONE_HIGHMEM] = highmem >> PAGE_SHIFT; |
Christoph Lameter | e53ef38 | 2006-09-25 23:31:14 -0700 | [diff] [blame] | 218 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | free_area_init(zones_size); |
| 220 | |
| 221 | /* |
| 222 | * Fixed mappings, only the page table structure has to be |
| 223 | * created - mappings will be set by set_fixmap(): |
| 224 | */ |
| 225 | vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK; |
| 226 | fixrange_init(vaddr, FIXADDR_TOP, swapper_pg_dir); |
| 227 | |
| 228 | fixaddr_user_init(); |
| 229 | |
| 230 | #ifdef CONFIG_HIGHMEM |
| 231 | init_highmem(); |
| 232 | #endif |
| 233 | } |
| 234 | |
Jeff Dike | 8192ab4 | 2008-02-04 22:30:53 -0800 | [diff] [blame] | 235 | /* |
| 236 | * This can't do anything because nothing in the kernel image can be freed |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | * since it's not in kernel physical memory. |
| 238 | */ |
| 239 | |
| 240 | void free_initmem(void) |
| 241 | { |
| 242 | } |
| 243 | |
| 244 | #ifdef CONFIG_BLK_DEV_INITRD |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | void free_initrd_mem(unsigned long start, unsigned long end) |
| 246 | { |
Jiang Liu | 1119969 | 2013-07-03 15:02:48 -0700 | [diff] [blame^] | 247 | free_reserved_area((void *)start, (void *)end, 0, "initrd"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | #endif |
| 250 | |
Jeff Dike | 8192ab4 | 2008-02-04 22:30:53 -0800 | [diff] [blame] | 251 | /* Allocate and free page tables. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
| 253 | pgd_t *pgd_alloc(struct mm_struct *mm) |
| 254 | { |
| 255 | pgd_t *pgd = (pgd_t *)__get_free_page(GFP_KERNEL); |
| 256 | |
| 257 | if (pgd) { |
| 258 | memset(pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t)); |
Jeff Dike | 009ec2a9 | 2008-02-04 22:30:53 -0800 | [diff] [blame] | 259 | memcpy(pgd + USER_PTRS_PER_PGD, |
| 260 | swapper_pg_dir + USER_PTRS_PER_PGD, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t)); |
| 262 | } |
| 263 | return pgd; |
| 264 | } |
| 265 | |
Benjamin Herrenschmidt | 5e54197 | 2008-02-04 22:29:14 -0800 | [diff] [blame] | 266 | void pgd_free(struct mm_struct *mm, pgd_t *pgd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | { |
| 268 | free_page((unsigned long) pgd); |
| 269 | } |
| 270 | |
| 271 | pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address) |
| 272 | { |
| 273 | pte_t *pte; |
| 274 | |
| 275 | pte = (pte_t *)__get_free_page(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO); |
| 276 | return pte; |
| 277 | } |
| 278 | |
Martin Schwidefsky | 2f569af | 2008-02-08 04:22:04 -0800 | [diff] [blame] | 279 | pgtable_t pte_alloc_one(struct mm_struct *mm, unsigned long address) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | { |
| 281 | struct page *pte; |
Jeff Dike | 60678bb | 2007-02-10 01:44:10 -0800 | [diff] [blame] | 282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | pte = alloc_page(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO); |
Martin Schwidefsky | 2f569af | 2008-02-08 04:22:04 -0800 | [diff] [blame] | 284 | if (pte) |
| 285 | pgtable_page_ctor(pte); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | return pte; |
| 287 | } |
Jeff Dike | 8192ab4 | 2008-02-04 22:30:53 -0800 | [diff] [blame] | 288 | |
| 289 | #ifdef CONFIG_3_LEVEL_PGTABLES |
| 290 | pmd_t *pmd_alloc_one(struct mm_struct *mm, unsigned long address) |
| 291 | { |
| 292 | pmd_t *pmd = (pmd_t *) __get_free_page(GFP_KERNEL); |
| 293 | |
| 294 | if (pmd) |
| 295 | memset(pmd, 0, PAGE_SIZE); |
| 296 | |
| 297 | return pmd; |
| 298 | } |
| 299 | #endif |
Jeff Dike | 43f5b30 | 2008-05-12 14:01:52 -0700 | [diff] [blame] | 300 | |
| 301 | void *uml_kmalloc(int size, int flags) |
| 302 | { |
| 303 | return kmalloc(size, flags); |
| 304 | } |