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 | |
Jeff Dike | 97a1fcb | 2007-07-23 18:43:48 -0700 | [diff] [blame] | 41 | void __init mem_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | { |
Jeff Dike | 60678bb | 2007-02-10 01:44:10 -0800 | [diff] [blame] | 43 | /* clear the zero-page */ |
WANG Cong | c0a9290 | 2008-02-04 22:30:41 -0800 | [diff] [blame] | 44 | memset(empty_zero_page, 0, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | /* Map in the area just after the brk now that kmalloc is about |
| 47 | * to be turned on. |
| 48 | */ |
| 49 | brk_end = (unsigned long) UML_ROUND_UP(sbrk(0)); |
Jeff Dike | ab26a52 | 2008-02-04 22:31:24 -0800 | [diff] [blame] | 50 | 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] | 51 | free_bootmem(__pa(brk_end), uml_reserved - brk_end); |
| 52 | uml_reserved = brk_end; |
| 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | /* this will put all low memory onto the freelists */ |
Jiang Liu | 0c98853 | 2013-07-03 15:03:24 -0700 | [diff] [blame] | 55 | free_all_bootmem(); |
Jason Lunz | af84eab | 2007-03-29 01:20:31 -0700 | [diff] [blame] | 56 | max_low_pfn = totalram_pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | max_pfn = totalram_pages; |
Jiang Liu | 715ee35 | 2013-07-03 15:04:16 -0700 | [diff] [blame] | 58 | mem_init_print_info(NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | kmalloc_ok = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Jeff Dike | 12f4964 | 2005-05-20 13:59:12 -0700 | [diff] [blame] | 62 | /* |
| 63 | * Create a page table and place a pointer to it in a middle page |
| 64 | * directory entry. |
| 65 | */ |
| 66 | static void __init one_page_table_init(pmd_t *pmd) |
| 67 | { |
| 68 | if (pmd_none(*pmd)) { |
| 69 | pte_t *pte = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE); |
| 70 | set_pmd(pmd, __pmd(_KERNPG_TABLE + |
| 71 | (unsigned long) __pa(pte))); |
| 72 | if (pte != pte_offset_kernel(pmd, 0)) |
| 73 | BUG(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | static void __init one_md_table_init(pud_t *pud) |
| 78 | { |
| 79 | #ifdef CONFIG_3_LEVEL_PGTABLES |
| 80 | pmd_t *pmd_table = (pmd_t *) alloc_bootmem_low_pages(PAGE_SIZE); |
| 81 | set_pud(pud, __pud(_KERNPG_TABLE + (unsigned long) __pa(pmd_table))); |
| 82 | if (pmd_table != pmd_offset(pud, 0)) |
| 83 | BUG(); |
| 84 | #endif |
| 85 | } |
| 86 | |
Jeff Dike | 009ec2a9 | 2008-02-04 22:30:53 -0800 | [diff] [blame] | 87 | static void __init fixrange_init(unsigned long start, unsigned long end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | pgd_t *pgd_base) |
| 89 | { |
| 90 | pgd_t *pgd; |
Jeff Dike | 12f4964 | 2005-05-20 13:59:12 -0700 | [diff] [blame] | 91 | pud_t *pud; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | pmd_t *pmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | int i, j; |
| 94 | unsigned long vaddr; |
| 95 | |
| 96 | vaddr = start; |
| 97 | i = pgd_index(vaddr); |
| 98 | j = pmd_index(vaddr); |
| 99 | pgd = pgd_base + i; |
| 100 | |
| 101 | for ( ; (i < PTRS_PER_PGD) && (vaddr < end); pgd++, i++) { |
Jeff Dike | 12f4964 | 2005-05-20 13:59:12 -0700 | [diff] [blame] | 102 | pud = pud_offset(pgd, vaddr); |
| 103 | if (pud_none(*pud)) |
| 104 | one_md_table_init(pud); |
| 105 | pmd = pmd_offset(pud, vaddr); |
Jeff Dike | 655e4ed | 2008-02-04 22:30:55 -0800 | [diff] [blame] | 106 | for (; (j < PTRS_PER_PMD) && (vaddr < end); pmd++, j++) { |
Jeff Dike | 12f4964 | 2005-05-20 13:59:12 -0700 | [diff] [blame] | 107 | one_page_table_init(pmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | vaddr += PMD_SIZE; |
| 109 | } |
| 110 | j = 0; |
| 111 | } |
| 112 | } |
| 113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | static void __init fixaddr_user_init( void) |
| 115 | { |
viro@ZenIV.linux.org.uk | 9a0b386 | 2005-09-07 23:21:11 +0100 | [diff] [blame] | 116 | #ifdef CONFIG_ARCH_REUSE_HOST_VSYSCALL_AREA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | long size = FIXADDR_USER_END - FIXADDR_USER_START; |
| 118 | pgd_t *pgd; |
| 119 | pud_t *pud; |
| 120 | pmd_t *pmd; |
| 121 | pte_t *pte; |
Jeff Dike | 655e4ed | 2008-02-04 22:30:55 -0800 | [diff] [blame] | 122 | phys_t p; |
| 123 | unsigned long v, vaddr = FIXADDR_USER_START; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
Jeff Dike | 655e4ed | 2008-02-04 22:30:55 -0800 | [diff] [blame] | 125 | if (!size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | return; |
| 127 | |
| 128 | fixrange_init( FIXADDR_USER_START, FIXADDR_USER_END, swapper_pg_dir); |
Jeff Dike | 655e4ed | 2008-02-04 22:30:55 -0800 | [diff] [blame] | 129 | v = (unsigned long) alloc_bootmem_low_pages(size); |
| 130 | memcpy((void *) v , (void *) FIXADDR_USER_START, size); |
| 131 | p = __pa(v); |
Jeff Dike | 009ec2a9 | 2008-02-04 22:30:53 -0800 | [diff] [blame] | 132 | for ( ; size > 0; size -= PAGE_SIZE, vaddr += PAGE_SIZE, |
Jeff Dike | 655e4ed | 2008-02-04 22:30:55 -0800 | [diff] [blame] | 133 | p += PAGE_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | pgd = swapper_pg_dir + pgd_index(vaddr); |
| 135 | pud = pud_offset(pgd, vaddr); |
| 136 | pmd = pmd_offset(pud, vaddr); |
| 137 | pte = pte_offset_kernel(pmd, vaddr); |
Jeff Dike | 655e4ed | 2008-02-04 22:30:55 -0800 | [diff] [blame] | 138 | pte_set_val(*pte, p, PAGE_READONLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
| 140 | #endif |
| 141 | } |
| 142 | |
Jeff Dike | 36e4546 | 2007-05-06 14:51:11 -0700 | [diff] [blame] | 143 | void __init paging_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | { |
| 145 | unsigned long zones_size[MAX_NR_ZONES], vaddr; |
| 146 | int i; |
| 147 | |
| 148 | empty_zero_page = (unsigned long *) alloc_bootmem_low_pages(PAGE_SIZE); |
| 149 | empty_bad_page = (unsigned long *) alloc_bootmem_low_pages(PAGE_SIZE); |
Jeff Dike | 009ec2a9 | 2008-02-04 22:30:53 -0800 | [diff] [blame] | 150 | for (i = 0; i < ARRAY_SIZE(zones_size); i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | zones_size[i] = 0; |
Jeff Dike | 91b165c | 2006-09-25 23:33:00 -0700 | [diff] [blame] | 152 | |
Jeff Dike | 0715501 | 2006-09-27 01:50:34 -0700 | [diff] [blame] | 153 | zones_size[ZONE_NORMAL] = (end_iomem >> PAGE_SHIFT) - |
| 154 | (uml_physmem >> PAGE_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | free_area_init(zones_size); |
| 156 | |
| 157 | /* |
| 158 | * Fixed mappings, only the page table structure has to be |
| 159 | * created - mappings will be set by set_fixmap(): |
| 160 | */ |
| 161 | vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK; |
| 162 | fixrange_init(vaddr, FIXADDR_TOP, swapper_pg_dir); |
| 163 | |
| 164 | fixaddr_user_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Jeff Dike | 8192ab4 | 2008-02-04 22:30:53 -0800 | [diff] [blame] | 167 | /* |
| 168 | * 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] | 169 | * since it's not in kernel physical memory. |
| 170 | */ |
| 171 | |
| 172 | void free_initmem(void) |
| 173 | { |
| 174 | } |
| 175 | |
| 176 | #ifdef CONFIG_BLK_DEV_INITRD |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | void free_initrd_mem(unsigned long start, unsigned long end) |
| 178 | { |
Jiang Liu | dbe67df | 2013-07-03 15:02:51 -0700 | [diff] [blame] | 179 | free_reserved_area((void *)start, (void *)end, -1, "initrd"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | #endif |
| 182 | |
Jeff Dike | 8192ab4 | 2008-02-04 22:30:53 -0800 | [diff] [blame] | 183 | /* Allocate and free page tables. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
| 185 | pgd_t *pgd_alloc(struct mm_struct *mm) |
| 186 | { |
| 187 | pgd_t *pgd = (pgd_t *)__get_free_page(GFP_KERNEL); |
| 188 | |
| 189 | if (pgd) { |
| 190 | memset(pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t)); |
Jeff Dike | 009ec2a9 | 2008-02-04 22:30:53 -0800 | [diff] [blame] | 191 | memcpy(pgd + USER_PTRS_PER_PGD, |
| 192 | swapper_pg_dir + USER_PTRS_PER_PGD, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t)); |
| 194 | } |
| 195 | return pgd; |
| 196 | } |
| 197 | |
Benjamin Herrenschmidt | 5e54197 | 2008-02-04 22:29:14 -0800 | [diff] [blame] | 198 | void pgd_free(struct mm_struct *mm, pgd_t *pgd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | { |
| 200 | free_page((unsigned long) pgd); |
| 201 | } |
| 202 | |
| 203 | pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address) |
| 204 | { |
| 205 | pte_t *pte; |
| 206 | |
Michal Hocko | 32d6bd9 | 2016-06-24 14:48:47 -0700 | [diff] [blame] | 207 | pte = (pte_t *)__get_free_page(GFP_KERNEL|__GFP_ZERO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | return pte; |
| 209 | } |
| 210 | |
Martin Schwidefsky | 2f569af | 2008-02-08 04:22:04 -0800 | [diff] [blame] | 211 | pgtable_t pte_alloc_one(struct mm_struct *mm, unsigned long address) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | { |
| 213 | struct page *pte; |
Jeff Dike | 60678bb | 2007-02-10 01:44:10 -0800 | [diff] [blame] | 214 | |
Michal Hocko | 32d6bd9 | 2016-06-24 14:48:47 -0700 | [diff] [blame] | 215 | pte = alloc_page(GFP_KERNEL|__GFP_ZERO); |
Kirill A. Shutemov | 647f884 | 2013-11-14 14:31:44 -0800 | [diff] [blame] | 216 | if (!pte) |
| 217 | return NULL; |
| 218 | if (!pgtable_page_ctor(pte)) { |
| 219 | __free_page(pte); |
| 220 | return NULL; |
| 221 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | return pte; |
| 223 | } |
Jeff Dike | 8192ab4 | 2008-02-04 22:30:53 -0800 | [diff] [blame] | 224 | |
| 225 | #ifdef CONFIG_3_LEVEL_PGTABLES |
| 226 | pmd_t *pmd_alloc_one(struct mm_struct *mm, unsigned long address) |
| 227 | { |
| 228 | pmd_t *pmd = (pmd_t *) __get_free_page(GFP_KERNEL); |
| 229 | |
| 230 | if (pmd) |
| 231 | memset(pmd, 0, PAGE_SIZE); |
| 232 | |
| 233 | return pmd; |
| 234 | } |
| 235 | #endif |
Jeff Dike | 43f5b30 | 2008-05-12 14:01:52 -0700 | [diff] [blame] | 236 | |
| 237 | void *uml_kmalloc(int size, int flags) |
| 238 | { |
| 239 | return kmalloc(size, flags); |
| 240 | } |