Alex Dewar | 0d1fb0a | 2019-08-25 10:49:17 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jeff Dike | 114069f | 2005-09-16 19:27:51 -0700 | [diff] [blame] | 2 | /* |
Jeff Dike | 009ec2a9 | 2008-02-04 22:30:53 -0800 | [diff] [blame] | 3 | * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Mike Rapoport | 2013288 | 2018-10-30 15:09:21 -0700 | [diff] [blame] | 8 | #include <linux/memblock.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 | 80e3931 | 2008-02-04 22:31:17 -0800 | [diff] [blame] | 25 | |
| 26 | /* |
| 27 | * Initialized during boot, and readonly for initializing page tables |
| 28 | * afterwards |
| 29 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | pgd_t swapper_pg_dir[PTRS_PER_PGD]; |
Jeff Dike | 80e3931 | 2008-02-04 22:31:17 -0800 | [diff] [blame] | 31 | |
| 32 | /* Initialized at boot time, and readonly after that */ |
Jeff Dike | 9902abd | 2006-03-31 02:30:09 -0800 | [diff] [blame] | 33 | unsigned long long highmem; |
Erel Geron | 5d38f32 | 2019-09-11 14:51:20 +0200 | [diff] [blame] | 34 | EXPORT_SYMBOL(highmem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | int kmalloc_ok = 0; |
| 36 | |
Jeff Dike | 80e3931 | 2008-02-04 22:31:17 -0800 | [diff] [blame] | 37 | /* Used during early boot */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | static unsigned long brk_end; |
| 39 | |
Jeff Dike | 97a1fcb | 2007-07-23 18:43:48 -0700 | [diff] [blame] | 40 | void __init mem_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | { |
Jeff Dike | 60678bb | 2007-02-10 01:44:10 -0800 | [diff] [blame] | 42 | /* clear the zero-page */ |
WANG Cong | c0a9290 | 2008-02-04 22:30:41 -0800 | [diff] [blame] | 43 | memset(empty_zero_page, 0, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
| 45 | /* Map in the area just after the brk now that kmalloc is about |
| 46 | * to be turned on. |
| 47 | */ |
| 48 | brk_end = (unsigned long) UML_ROUND_UP(sbrk(0)); |
Jeff Dike | ab26a52 | 2008-02-04 22:31:24 -0800 | [diff] [blame] | 49 | map_memory(brk_end, __pa(brk_end), uml_reserved - brk_end, 1, 1, 0); |
Mike Rapoport | 2013288 | 2018-10-30 15:09:21 -0700 | [diff] [blame] | 50 | memblock_free(__pa(brk_end), uml_reserved - brk_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | uml_reserved = brk_end; |
| 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | /* this will put all low memory onto the freelists */ |
Mike Rapoport | c6ffc5c | 2018-10-30 15:09:30 -0700 | [diff] [blame] | 54 | memblock_free_all(); |
Arun KS | ca79b0c | 2018-12-28 00:34:29 -0800 | [diff] [blame] | 55 | max_low_pfn = totalram_pages(); |
Arun KS | 3d6357d | 2018-12-28 00:34:20 -0800 | [diff] [blame] | 56 | max_pfn = max_low_pfn; |
Jiang Liu | 715ee35 | 2013-07-03 15:04:16 -0700 | [diff] [blame] | 57 | mem_init_print_info(NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | kmalloc_ok = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Jeff Dike | 12f4964 | 2005-05-20 13:59:12 -0700 | [diff] [blame] | 61 | /* |
| 62 | * Create a page table and place a pointer to it in a middle page |
| 63 | * directory entry. |
| 64 | */ |
| 65 | static void __init one_page_table_init(pmd_t *pmd) |
| 66 | { |
| 67 | if (pmd_none(*pmd)) { |
Mike Rapoport | e8625dc | 2018-10-30 15:08:54 -0700 | [diff] [blame] | 68 | pte_t *pte = (pte_t *) memblock_alloc_low(PAGE_SIZE, |
| 69 | PAGE_SIZE); |
Mike Rapoport | 8a7f97b | 2019-03-11 23:30:31 -0700 | [diff] [blame] | 70 | if (!pte) |
| 71 | panic("%s: Failed to allocate %lu bytes align=%lx\n", |
| 72 | __func__, PAGE_SIZE, PAGE_SIZE); |
| 73 | |
Jeff Dike | 12f4964 | 2005-05-20 13:59:12 -0700 | [diff] [blame] | 74 | set_pmd(pmd, __pmd(_KERNPG_TABLE + |
| 75 | (unsigned long) __pa(pte))); |
| 76 | if (pte != pte_offset_kernel(pmd, 0)) |
| 77 | BUG(); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | static void __init one_md_table_init(pud_t *pud) |
| 82 | { |
| 83 | #ifdef CONFIG_3_LEVEL_PGTABLES |
Mike Rapoport | e8625dc | 2018-10-30 15:08:54 -0700 | [diff] [blame] | 84 | pmd_t *pmd_table = (pmd_t *) memblock_alloc_low(PAGE_SIZE, PAGE_SIZE); |
Mike Rapoport | 8a7f97b | 2019-03-11 23:30:31 -0700 | [diff] [blame] | 85 | if (!pmd_table) |
| 86 | panic("%s: Failed to allocate %lu bytes align=%lx\n", |
| 87 | __func__, PAGE_SIZE, PAGE_SIZE); |
| 88 | |
Jeff Dike | 12f4964 | 2005-05-20 13:59:12 -0700 | [diff] [blame] | 89 | set_pud(pud, __pud(_KERNPG_TABLE + (unsigned long) __pa(pmd_table))); |
| 90 | if (pmd_table != pmd_offset(pud, 0)) |
| 91 | BUG(); |
| 92 | #endif |
| 93 | } |
| 94 | |
Jeff Dike | 009ec2a9 | 2008-02-04 22:30:53 -0800 | [diff] [blame] | 95 | static void __init fixrange_init(unsigned long start, unsigned long end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | pgd_t *pgd_base) |
| 97 | { |
| 98 | pgd_t *pgd; |
Mike Rapoport | e19f97e | 2019-12-04 16:54:28 -0800 | [diff] [blame^] | 99 | p4d_t *p4d; |
Jeff Dike | 12f4964 | 2005-05-20 13:59:12 -0700 | [diff] [blame] | 100 | pud_t *pud; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | pmd_t *pmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | int i, j; |
| 103 | unsigned long vaddr; |
| 104 | |
| 105 | vaddr = start; |
| 106 | i = pgd_index(vaddr); |
| 107 | j = pmd_index(vaddr); |
| 108 | pgd = pgd_base + i; |
| 109 | |
| 110 | for ( ; (i < PTRS_PER_PGD) && (vaddr < end); pgd++, i++) { |
Mike Rapoport | e19f97e | 2019-12-04 16:54:28 -0800 | [diff] [blame^] | 111 | p4d = p4d_offset(pgd, vaddr); |
| 112 | pud = pud_offset(p4d, vaddr); |
Jeff Dike | 12f4964 | 2005-05-20 13:59:12 -0700 | [diff] [blame] | 113 | if (pud_none(*pud)) |
| 114 | one_md_table_init(pud); |
| 115 | pmd = pmd_offset(pud, vaddr); |
Jeff Dike | 655e4ed | 2008-02-04 22:30:55 -0800 | [diff] [blame] | 116 | for (; (j < PTRS_PER_PMD) && (vaddr < end); pmd++, j++) { |
Jeff Dike | 12f4964 | 2005-05-20 13:59:12 -0700 | [diff] [blame] | 117 | one_page_table_init(pmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | vaddr += PMD_SIZE; |
| 119 | } |
| 120 | j = 0; |
| 121 | } |
| 122 | } |
| 123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | static void __init fixaddr_user_init( void) |
| 125 | { |
viro@ZenIV.linux.org.uk | 9a0b386 | 2005-09-07 23:21:11 +0100 | [diff] [blame] | 126 | #ifdef CONFIG_ARCH_REUSE_HOST_VSYSCALL_AREA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | long size = FIXADDR_USER_END - FIXADDR_USER_START; |
| 128 | pgd_t *pgd; |
Mike Rapoport | e19f97e | 2019-12-04 16:54:28 -0800 | [diff] [blame^] | 129 | p4d_t *p4d; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | pud_t *pud; |
| 131 | pmd_t *pmd; |
| 132 | pte_t *pte; |
Jeff Dike | 655e4ed | 2008-02-04 22:30:55 -0800 | [diff] [blame] | 133 | phys_t p; |
| 134 | unsigned long v, vaddr = FIXADDR_USER_START; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
Jeff Dike | 655e4ed | 2008-02-04 22:30:55 -0800 | [diff] [blame] | 136 | if (!size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | return; |
| 138 | |
| 139 | fixrange_init( FIXADDR_USER_START, FIXADDR_USER_END, swapper_pg_dir); |
Mike Rapoport | e8625dc | 2018-10-30 15:08:54 -0700 | [diff] [blame] | 140 | v = (unsigned long) memblock_alloc_low(size, PAGE_SIZE); |
Mike Rapoport | 8a7f97b | 2019-03-11 23:30:31 -0700 | [diff] [blame] | 141 | if (!v) |
| 142 | panic("%s: Failed to allocate %lu bytes align=%lx\n", |
| 143 | __func__, size, PAGE_SIZE); |
| 144 | |
Jeff Dike | 655e4ed | 2008-02-04 22:30:55 -0800 | [diff] [blame] | 145 | memcpy((void *) v , (void *) FIXADDR_USER_START, size); |
| 146 | p = __pa(v); |
Jeff Dike | 009ec2a9 | 2008-02-04 22:30:53 -0800 | [diff] [blame] | 147 | for ( ; size > 0; size -= PAGE_SIZE, vaddr += PAGE_SIZE, |
Jeff Dike | 655e4ed | 2008-02-04 22:30:55 -0800 | [diff] [blame] | 148 | p += PAGE_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | pgd = swapper_pg_dir + pgd_index(vaddr); |
Mike Rapoport | e19f97e | 2019-12-04 16:54:28 -0800 | [diff] [blame^] | 150 | p4d = p4d_offset(pgd, vaddr); |
| 151 | pud = pud_offset(p4d, vaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | pmd = pmd_offset(pud, vaddr); |
| 153 | pte = pte_offset_kernel(pmd, vaddr); |
Jeff Dike | 655e4ed | 2008-02-04 22:30:55 -0800 | [diff] [blame] | 154 | pte_set_val(*pte, p, PAGE_READONLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | } |
| 156 | #endif |
| 157 | } |
| 158 | |
Jeff Dike | 36e4546 | 2007-05-06 14:51:11 -0700 | [diff] [blame] | 159 | void __init paging_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | { |
| 161 | unsigned long zones_size[MAX_NR_ZONES], vaddr; |
| 162 | int i; |
| 163 | |
Mike Rapoport | e8625dc | 2018-10-30 15:08:54 -0700 | [diff] [blame] | 164 | empty_zero_page = (unsigned long *) memblock_alloc_low(PAGE_SIZE, |
| 165 | PAGE_SIZE); |
Mike Rapoport | 8a7f97b | 2019-03-11 23:30:31 -0700 | [diff] [blame] | 166 | if (!empty_zero_page) |
| 167 | panic("%s: Failed to allocate %lu bytes align=%lx\n", |
| 168 | __func__, PAGE_SIZE, PAGE_SIZE); |
| 169 | |
Jeff Dike | 009ec2a9 | 2008-02-04 22:30:53 -0800 | [diff] [blame] | 170 | for (i = 0; i < ARRAY_SIZE(zones_size); i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | zones_size[i] = 0; |
Jeff Dike | 91b165c | 2006-09-25 23:33:00 -0700 | [diff] [blame] | 172 | |
Jeff Dike | 0715501 | 2006-09-27 01:50:34 -0700 | [diff] [blame] | 173 | zones_size[ZONE_NORMAL] = (end_iomem >> PAGE_SHIFT) - |
| 174 | (uml_physmem >> PAGE_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | free_area_init(zones_size); |
| 176 | |
| 177 | /* |
| 178 | * Fixed mappings, only the page table structure has to be |
| 179 | * created - mappings will be set by set_fixmap(): |
| 180 | */ |
| 181 | vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK; |
| 182 | fixrange_init(vaddr, FIXADDR_TOP, swapper_pg_dir); |
| 183 | |
| 184 | fixaddr_user_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Jeff Dike | 8192ab4 | 2008-02-04 22:30:53 -0800 | [diff] [blame] | 187 | /* |
| 188 | * 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] | 189 | * since it's not in kernel physical memory. |
| 190 | */ |
| 191 | |
| 192 | void free_initmem(void) |
| 193 | { |
| 194 | } |
| 195 | |
Jeff Dike | 8192ab4 | 2008-02-04 22:30:53 -0800 | [diff] [blame] | 196 | /* Allocate and free page tables. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
| 198 | pgd_t *pgd_alloc(struct mm_struct *mm) |
| 199 | { |
| 200 | pgd_t *pgd = (pgd_t *)__get_free_page(GFP_KERNEL); |
| 201 | |
| 202 | if (pgd) { |
| 203 | memset(pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t)); |
Jeff Dike | 009ec2a9 | 2008-02-04 22:30:53 -0800 | [diff] [blame] | 204 | memcpy(pgd + USER_PTRS_PER_PGD, |
| 205 | swapper_pg_dir + USER_PTRS_PER_PGD, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t)); |
| 207 | } |
| 208 | return pgd; |
| 209 | } |
| 210 | |
Benjamin Herrenschmidt | 5e54197 | 2008-02-04 22:29:14 -0800 | [diff] [blame] | 211 | void pgd_free(struct mm_struct *mm, pgd_t *pgd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | { |
| 213 | free_page((unsigned long) pgd); |
| 214 | } |
| 215 | |
Jeff Dike | 8192ab4 | 2008-02-04 22:30:53 -0800 | [diff] [blame] | 216 | #ifdef CONFIG_3_LEVEL_PGTABLES |
| 217 | pmd_t *pmd_alloc_one(struct mm_struct *mm, unsigned long address) |
| 218 | { |
| 219 | pmd_t *pmd = (pmd_t *) __get_free_page(GFP_KERNEL); |
| 220 | |
| 221 | if (pmd) |
| 222 | memset(pmd, 0, PAGE_SIZE); |
| 223 | |
| 224 | return pmd; |
| 225 | } |
| 226 | #endif |
Jeff Dike | 43f5b30 | 2008-05-12 14:01:52 -0700 | [diff] [blame] | 227 | |
| 228 | void *uml_kmalloc(int size, int flags) |
| 229 | { |
| 230 | return kmalloc(size, flags); |
| 231 | } |