Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/x86_64/mm/init.c |
| 3 | * |
| 4 | * Copyright (C) 1995 Linus Torvalds |
Pavel Machek | a253129 | 2010-07-18 14:27:13 +0200 | [diff] [blame] | 5 | * Copyright (C) 2000 Pavel Machek <pavel@ucw.cz> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * Copyright (C) 2002,2003 Andi Kleen <ak@suse.de> |
| 7 | */ |
| 8 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/signal.h> |
| 10 | #include <linux/sched.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/errno.h> |
| 13 | #include <linux/string.h> |
| 14 | #include <linux/types.h> |
| 15 | #include <linux/ptrace.h> |
| 16 | #include <linux/mman.h> |
| 17 | #include <linux/mm.h> |
| 18 | #include <linux/swap.h> |
| 19 | #include <linux/smp.h> |
| 20 | #include <linux/init.h> |
Thomas Gleixner | 11034d5 | 2008-05-12 15:43:36 +0200 | [diff] [blame] | 21 | #include <linux/initrd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/pagemap.h> |
| 23 | #include <linux/bootmem.h> |
Yinghai Lu | a9ce6bc | 2010-08-25 13:39:17 -0700 | [diff] [blame] | 24 | #include <linux/memblock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/proc_fs.h> |
Andi Kleen | 5917089 | 2005-11-05 17:25:53 +0100 | [diff] [blame] | 26 | #include <linux/pci.h> |
Jan Beulich | 6fb1475 | 2007-05-02 19:27:10 +0200 | [diff] [blame] | 27 | #include <linux/pfn.h> |
Randy Dunlap | c9cf552 | 2006-06-27 02:53:52 -0700 | [diff] [blame] | 28 | #include <linux/poison.h> |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 29 | #include <linux/dma-mapping.h> |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 30 | #include <linux/module.h> |
Benjamin Herrenschmidt | a63fdc5 | 2011-06-14 10:57:50 +1000 | [diff] [blame] | 31 | #include <linux/memory.h> |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 32 | #include <linux/memory_hotplug.h> |
Konrad Rzeszutek | ae32b12 | 2007-05-02 19:27:11 +0200 | [diff] [blame] | 33 | #include <linux/nmi.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 34 | #include <linux/gfp.h> |
David Howells | 2f96b8c | 2013-04-12 00:10:25 +0100 | [diff] [blame] | 35 | #include <linux/kcore.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | #include <asm/processor.h> |
Ingo Molnar | 46eaa67 | 2008-10-12 15:06:29 +0200 | [diff] [blame] | 38 | #include <asm/bios_ebda.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <asm/uaccess.h> |
| 40 | #include <asm/pgtable.h> |
| 41 | #include <asm/pgalloc.h> |
| 42 | #include <asm/dma.h> |
| 43 | #include <asm/fixmap.h> |
| 44 | #include <asm/e820.h> |
| 45 | #include <asm/apic.h> |
| 46 | #include <asm/tlb.h> |
| 47 | #include <asm/mmu_context.h> |
| 48 | #include <asm/proto.h> |
| 49 | #include <asm/smp.h> |
Andi Kleen | 2bc0414 | 2005-11-05 17:25:53 +0100 | [diff] [blame] | 50 | #include <asm/sections.h> |
Thomas Gleixner | 718fc13 | 2008-01-30 13:30:17 +0100 | [diff] [blame] | 51 | #include <asm/kdebug.h> |
Thomas Gleixner | aaa64e0 | 2008-01-30 13:30:17 +0100 | [diff] [blame] | 52 | #include <asm/numa.h> |
Harvey Harrison | 7bfeab9 | 2008-02-12 12:12:01 -0800 | [diff] [blame] | 53 | #include <asm/cacheflush.h> |
Pekka Enberg | 4fcb208 | 2009-03-05 14:55:08 +0200 | [diff] [blame] | 54 | #include <asm/init.h> |
Nathan Fontenot | 1dc41aa | 2011-01-20 10:46:15 -0600 | [diff] [blame] | 55 | #include <asm/uv/uv.h> |
Yinghai Lu | e5f15b4 | 2011-02-18 11:30:30 +0000 | [diff] [blame] | 56 | #include <asm/setup.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Yinghai Lu | 5c51bdb | 2012-11-16 19:39:01 -0800 | [diff] [blame] | 58 | #include "mm_internal.h" |
| 59 | |
Yinghai Lu | aece278 | 2013-01-24 12:19:48 -0800 | [diff] [blame] | 60 | static void ident_pmd_init(unsigned long pmd_flag, pmd_t *pmd_page, |
| 61 | unsigned long addr, unsigned long end) |
| 62 | { |
| 63 | addr &= PMD_MASK; |
| 64 | for (; addr < end; addr += PMD_SIZE) { |
| 65 | pmd_t *pmd = pmd_page + pmd_index(addr); |
| 66 | |
| 67 | if (!pmd_present(*pmd)) |
| 68 | set_pmd(pmd, __pmd(addr | pmd_flag)); |
| 69 | } |
| 70 | } |
| 71 | static int ident_pud_init(struct x86_mapping_info *info, pud_t *pud_page, |
| 72 | unsigned long addr, unsigned long end) |
| 73 | { |
| 74 | unsigned long next; |
| 75 | |
| 76 | for (; addr < end; addr = next) { |
| 77 | pud_t *pud = pud_page + pud_index(addr); |
| 78 | pmd_t *pmd; |
| 79 | |
| 80 | next = (addr & PUD_MASK) + PUD_SIZE; |
| 81 | if (next > end) |
| 82 | next = end; |
| 83 | |
| 84 | if (pud_present(*pud)) { |
| 85 | pmd = pmd_offset(pud, 0); |
| 86 | ident_pmd_init(info->pmd_flag, pmd, addr, next); |
| 87 | continue; |
| 88 | } |
| 89 | pmd = (pmd_t *)info->alloc_pgt_page(info->context); |
| 90 | if (!pmd) |
| 91 | return -ENOMEM; |
| 92 | ident_pmd_init(info->pmd_flag, pmd, addr, next); |
| 93 | set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE)); |
| 94 | } |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | int kernel_ident_mapping_init(struct x86_mapping_info *info, pgd_t *pgd_page, |
| 100 | unsigned long addr, unsigned long end) |
| 101 | { |
| 102 | unsigned long next; |
| 103 | int result; |
| 104 | int off = info->kernel_mapping ? pgd_index(__PAGE_OFFSET) : 0; |
| 105 | |
| 106 | for (; addr < end; addr = next) { |
| 107 | pgd_t *pgd = pgd_page + pgd_index(addr) + off; |
| 108 | pud_t *pud; |
| 109 | |
| 110 | next = (addr & PGDIR_MASK) + PGDIR_SIZE; |
| 111 | if (next > end) |
| 112 | next = end; |
| 113 | |
| 114 | if (pgd_present(*pgd)) { |
| 115 | pud = pud_offset(pgd, 0); |
| 116 | result = ident_pud_init(info, pud, addr, next); |
| 117 | if (result) |
| 118 | return result; |
| 119 | continue; |
| 120 | } |
| 121 | |
| 122 | pud = (pud_t *)info->alloc_pgt_page(info->context); |
| 123 | if (!pud) |
| 124 | return -ENOMEM; |
| 125 | result = ident_pud_init(info, pud, addr, next); |
| 126 | if (result) |
| 127 | return result; |
| 128 | set_pgd(pgd, __pgd(__pa(pud) | _KERNPG_TABLE)); |
| 129 | } |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
Ingo Molnar | 00d1c5e | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 134 | static int __init parse_direct_gbpages_off(char *arg) |
| 135 | { |
| 136 | direct_gbpages = 0; |
| 137 | return 0; |
| 138 | } |
| 139 | early_param("nogbpages", parse_direct_gbpages_off); |
| 140 | |
| 141 | static int __init parse_direct_gbpages_on(char *arg) |
| 142 | { |
| 143 | direct_gbpages = 1; |
| 144 | return 0; |
| 145 | } |
| 146 | early_param("gbpages", parse_direct_gbpages_on); |
| 147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | /* |
| 149 | * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the |
| 150 | * physical space so we can cache the place of the first one and move |
| 151 | * around without checking the pgd every time. |
| 152 | */ |
| 153 | |
Jeremy Fitzhardinge | be43d72 | 2008-09-07 15:21:13 -0700 | [diff] [blame] | 154 | pteval_t __supported_pte_mask __read_mostly = ~_PAGE_IOMAP; |
Yinghai Lu | bd220a2 | 2008-09-05 00:58:28 -0700 | [diff] [blame] | 155 | EXPORT_SYMBOL_GPL(__supported_pte_mask); |
| 156 | |
Yinghai Lu | bd220a2 | 2008-09-05 00:58:28 -0700 | [diff] [blame] | 157 | int force_personality32; |
| 158 | |
Ingo Molnar | deed05b | 2008-09-05 10:23:26 +0200 | [diff] [blame] | 159 | /* |
| 160 | * noexec32=on|off |
| 161 | * Control non executable heap for 32bit processes. |
| 162 | * To control the stack too use noexec=off |
| 163 | * |
| 164 | * on PROT_READ does not imply PROT_EXEC for 32-bit processes (default) |
| 165 | * off PROT_READ implies PROT_EXEC |
| 166 | */ |
Yinghai Lu | bd220a2 | 2008-09-05 00:58:28 -0700 | [diff] [blame] | 167 | static int __init nonx32_setup(char *str) |
| 168 | { |
| 169 | if (!strcmp(str, "on")) |
| 170 | force_personality32 &= ~READ_IMPLIES_EXEC; |
| 171 | else if (!strcmp(str, "off")) |
| 172 | force_personality32 |= READ_IMPLIES_EXEC; |
| 173 | return 1; |
| 174 | } |
| 175 | __setup("noexec32=", nonx32_setup); |
| 176 | |
Marcin Slusarz | 8d6ea967 | 2008-08-15 18:32:24 +0200 | [diff] [blame] | 177 | /* |
Haicheng Li | 6afb515 | 2010-05-19 17:42:14 +0800 | [diff] [blame] | 178 | * When memory was added/removed make sure all the processes MM have |
| 179 | * suitable PGD entries in the local PGD level page. |
| 180 | */ |
| 181 | void sync_global_pgds(unsigned long start, unsigned long end) |
| 182 | { |
Jeremy Fitzhardinge | 44235dc | 2010-10-14 17:04:59 -0700 | [diff] [blame] | 183 | unsigned long address; |
Haicheng Li | 6afb515 | 2010-05-19 17:42:14 +0800 | [diff] [blame] | 184 | |
Jeremy Fitzhardinge | 44235dc | 2010-10-14 17:04:59 -0700 | [diff] [blame] | 185 | for (address = start; address <= end; address += PGDIR_SIZE) { |
| 186 | const pgd_t *pgd_ref = pgd_offset_k(address); |
Jeremy Fitzhardinge | 44235dc | 2010-10-14 17:04:59 -0700 | [diff] [blame] | 187 | struct page *page; |
Haicheng Li | 6afb515 | 2010-05-19 17:42:14 +0800 | [diff] [blame] | 188 | |
Jeremy Fitzhardinge | 44235dc | 2010-10-14 17:04:59 -0700 | [diff] [blame] | 189 | if (pgd_none(*pgd_ref)) |
| 190 | continue; |
Haicheng Li | 6afb515 | 2010-05-19 17:42:14 +0800 | [diff] [blame] | 191 | |
Andrea Arcangeli | a79e53d | 2011-02-16 15:45:22 -0800 | [diff] [blame] | 192 | spin_lock(&pgd_lock); |
Jeremy Fitzhardinge | 44235dc | 2010-10-14 17:04:59 -0700 | [diff] [blame] | 193 | list_for_each_entry(page, &pgd_list, lru) { |
Linus Torvalds | be354f4 | 2012-12-15 12:29:54 -0800 | [diff] [blame] | 194 | pgd_t *pgd; |
Jeremy Fitzhardinge | 617d34d | 2010-09-21 12:01:51 -0700 | [diff] [blame] | 195 | spinlock_t *pgt_lock; |
| 196 | |
Jeremy Fitzhardinge | 44235dc | 2010-10-14 17:04:59 -0700 | [diff] [blame] | 197 | pgd = (pgd_t *)page_address(page) + pgd_index(address); |
Andrea Arcangeli | a79e53d | 2011-02-16 15:45:22 -0800 | [diff] [blame] | 198 | /* the pgt_lock only for Xen */ |
Jeremy Fitzhardinge | 617d34d | 2010-09-21 12:01:51 -0700 | [diff] [blame] | 199 | pgt_lock = &pgd_page_get_mm(page)->page_table_lock; |
| 200 | spin_lock(pgt_lock); |
| 201 | |
Jeremy Fitzhardinge | 44235dc | 2010-10-14 17:04:59 -0700 | [diff] [blame] | 202 | if (pgd_none(*pgd)) |
| 203 | set_pgd(pgd, *pgd_ref); |
| 204 | else |
| 205 | BUG_ON(pgd_page_vaddr(*pgd) |
| 206 | != pgd_page_vaddr(*pgd_ref)); |
Jeremy Fitzhardinge | 617d34d | 2010-09-21 12:01:51 -0700 | [diff] [blame] | 207 | |
| 208 | spin_unlock(pgt_lock); |
Jeremy Fitzhardinge | 44235dc | 2010-10-14 17:04:59 -0700 | [diff] [blame] | 209 | } |
Andrea Arcangeli | a79e53d | 2011-02-16 15:45:22 -0800 | [diff] [blame] | 210 | spin_unlock(&pgd_lock); |
Jeremy Fitzhardinge | 44235dc | 2010-10-14 17:04:59 -0700 | [diff] [blame] | 211 | } |
Haicheng Li | 6afb515 | 2010-05-19 17:42:14 +0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /* |
Marcin Slusarz | 8d6ea967 | 2008-08-15 18:32:24 +0200 | [diff] [blame] | 215 | * NOTE: This function is marked __ref because it calls __init function |
| 216 | * (alloc_bootmem_pages). It's safe to do it ONLY when after_bootmem == 0. |
| 217 | */ |
| 218 | static __ref void *spp_getpage(void) |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 219 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | void *ptr; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 221 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | if (after_bootmem) |
Vegard Nossum | 9e73023 | 2009-02-22 11:28:25 +0100 | [diff] [blame] | 223 | ptr = (void *) get_zeroed_page(GFP_ATOMIC | __GFP_NOTRACK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | else |
| 225 | ptr = alloc_bootmem_pages(PAGE_SIZE); |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 226 | |
| 227 | if (!ptr || ((unsigned long)ptr & ~PAGE_MASK)) { |
| 228 | panic("set_pte_phys: cannot allocate page data %s\n", |
| 229 | after_bootmem ? "after bootmem" : ""); |
| 230 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
Ingo Molnar | 10f22dd | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 232 | pr_debug("spp_getpage %p\n", ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 234 | return ptr; |
| 235 | } |
| 236 | |
Jeremy Fitzhardinge | f254f39 | 2009-03-03 12:02:57 -0800 | [diff] [blame] | 237 | static pud_t *fill_pud(pgd_t *pgd, unsigned long vaddr) |
Tejun Heo | 458a3e6 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 238 | { |
| 239 | if (pgd_none(*pgd)) { |
| 240 | pud_t *pud = (pud_t *)spp_getpage(); |
| 241 | pgd_populate(&init_mm, pgd, pud); |
| 242 | if (pud != pud_offset(pgd, 0)) |
| 243 | printk(KERN_ERR "PAGETABLE BUG #00! %p <-> %p\n", |
| 244 | pud, pud_offset(pgd, 0)); |
| 245 | } |
| 246 | return pud_offset(pgd, vaddr); |
| 247 | } |
| 248 | |
Jeremy Fitzhardinge | f254f39 | 2009-03-03 12:02:57 -0800 | [diff] [blame] | 249 | static pmd_t *fill_pmd(pud_t *pud, unsigned long vaddr) |
Tejun Heo | 458a3e6 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 250 | { |
| 251 | if (pud_none(*pud)) { |
| 252 | pmd_t *pmd = (pmd_t *) spp_getpage(); |
| 253 | pud_populate(&init_mm, pud, pmd); |
| 254 | if (pmd != pmd_offset(pud, 0)) |
| 255 | printk(KERN_ERR "PAGETABLE BUG #01! %p <-> %p\n", |
| 256 | pmd, pmd_offset(pud, 0)); |
| 257 | } |
| 258 | return pmd_offset(pud, vaddr); |
| 259 | } |
| 260 | |
Jeremy Fitzhardinge | f254f39 | 2009-03-03 12:02:57 -0800 | [diff] [blame] | 261 | static pte_t *fill_pte(pmd_t *pmd, unsigned long vaddr) |
Tejun Heo | 458a3e6 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 262 | { |
| 263 | if (pmd_none(*pmd)) { |
| 264 | pte_t *pte = (pte_t *) spp_getpage(); |
| 265 | pmd_populate_kernel(&init_mm, pmd, pte); |
| 266 | if (pte != pte_offset_kernel(pmd, 0)) |
| 267 | printk(KERN_ERR "PAGETABLE BUG #02!\n"); |
| 268 | } |
| 269 | return pte_offset_kernel(pmd, vaddr); |
| 270 | } |
| 271 | |
| 272 | void set_pte_vaddr_pud(pud_t *pud_page, unsigned long vaddr, pte_t new_pte) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | pud_t *pud; |
| 275 | pmd_t *pmd; |
Jeremy Fitzhardinge | d494a96 | 2008-06-17 11:41:59 -0700 | [diff] [blame] | 276 | pte_t *pte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
Eduardo Habkost | 0814e0b | 2008-06-25 00:19:22 -0400 | [diff] [blame] | 278 | pud = pud_page + pud_index(vaddr); |
Tejun Heo | 458a3e6 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 279 | pmd = fill_pmd(pud, vaddr); |
| 280 | pte = fill_pte(pmd, vaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | set_pte(pte, new_pte); |
| 283 | |
| 284 | /* |
| 285 | * It's enough to flush this one mapping. |
| 286 | * (PGE mappings get flushed as well) |
| 287 | */ |
| 288 | __flush_tlb_one(vaddr); |
| 289 | } |
| 290 | |
Tejun Heo | 458a3e6 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 291 | void set_pte_vaddr(unsigned long vaddr, pte_t pteval) |
Eduardo Habkost | 0814e0b | 2008-06-25 00:19:22 -0400 | [diff] [blame] | 292 | { |
| 293 | pgd_t *pgd; |
| 294 | pud_t *pud_page; |
| 295 | |
| 296 | pr_debug("set_pte_vaddr %lx to %lx\n", vaddr, native_pte_val(pteval)); |
| 297 | |
| 298 | pgd = pgd_offset_k(vaddr); |
| 299 | if (pgd_none(*pgd)) { |
| 300 | printk(KERN_ERR |
| 301 | "PGD FIXMAP MISSING, it should be setup in head.S!\n"); |
| 302 | return; |
| 303 | } |
| 304 | pud_page = (pud_t*)pgd_page_vaddr(*pgd); |
| 305 | set_pte_vaddr_pud(pud_page, vaddr, pteval); |
| 306 | } |
| 307 | |
Tejun Heo | 458a3e6 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 308 | pmd_t * __init populate_extra_pmd(unsigned long vaddr) |
Tejun Heo | 1112441 | 2009-02-20 16:29:09 +0900 | [diff] [blame] | 309 | { |
| 310 | pgd_t *pgd; |
| 311 | pud_t *pud; |
| 312 | |
| 313 | pgd = pgd_offset_k(vaddr); |
Tejun Heo | 458a3e6 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 314 | pud = fill_pud(pgd, vaddr); |
| 315 | return fill_pmd(pud, vaddr); |
| 316 | } |
Tejun Heo | 1112441 | 2009-02-20 16:29:09 +0900 | [diff] [blame] | 317 | |
Tejun Heo | 458a3e6 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 318 | pte_t * __init populate_extra_pte(unsigned long vaddr) |
| 319 | { |
| 320 | pmd_t *pmd; |
| 321 | |
| 322 | pmd = populate_extra_pmd(vaddr); |
| 323 | return fill_pte(pmd, vaddr); |
Tejun Heo | 1112441 | 2009-02-20 16:29:09 +0900 | [diff] [blame] | 324 | } |
| 325 | |
Thomas Gleixner | 31eedd8 | 2008-02-15 17:29:12 +0100 | [diff] [blame] | 326 | /* |
Jack Steiner | 3a9e189 | 2008-07-01 14:45:32 -0500 | [diff] [blame] | 327 | * Create large page table mappings for a range of physical addresses. |
| 328 | */ |
| 329 | static void __init __init_extra_mapping(unsigned long phys, unsigned long size, |
| 330 | pgprot_t prot) |
| 331 | { |
| 332 | pgd_t *pgd; |
| 333 | pud_t *pud; |
| 334 | pmd_t *pmd; |
| 335 | |
| 336 | BUG_ON((phys & ~PMD_MASK) || (size & ~PMD_MASK)); |
| 337 | for (; size; phys += PMD_SIZE, size -= PMD_SIZE) { |
| 338 | pgd = pgd_offset_k((unsigned long)__va(phys)); |
| 339 | if (pgd_none(*pgd)) { |
| 340 | pud = (pud_t *) spp_getpage(); |
| 341 | set_pgd(pgd, __pgd(__pa(pud) | _KERNPG_TABLE | |
| 342 | _PAGE_USER)); |
| 343 | } |
| 344 | pud = pud_offset(pgd, (unsigned long)__va(phys)); |
| 345 | if (pud_none(*pud)) { |
| 346 | pmd = (pmd_t *) spp_getpage(); |
| 347 | set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE | |
| 348 | _PAGE_USER)); |
| 349 | } |
| 350 | pmd = pmd_offset(pud, phys); |
| 351 | BUG_ON(!pmd_none(*pmd)); |
| 352 | set_pmd(pmd, __pmd(phys | pgprot_val(prot))); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | void __init init_extra_mapping_wb(unsigned long phys, unsigned long size) |
| 357 | { |
| 358 | __init_extra_mapping(phys, size, PAGE_KERNEL_LARGE); |
| 359 | } |
| 360 | |
| 361 | void __init init_extra_mapping_uc(unsigned long phys, unsigned long size) |
| 362 | { |
| 363 | __init_extra_mapping(phys, size, PAGE_KERNEL_LARGE_NOCACHE); |
| 364 | } |
| 365 | |
| 366 | /* |
Ingo Molnar | 88f3aec | 2008-02-21 11:04:11 +0100 | [diff] [blame] | 367 | * The head.S code sets up the kernel high mapping: |
| 368 | * |
| 369 | * from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text) |
Thomas Gleixner | 31eedd8 | 2008-02-15 17:29:12 +0100 | [diff] [blame] | 370 | * |
Zhang Yanfei | 1e3b308 | 2013-05-15 10:58:07 +0800 | [diff] [blame^] | 371 | * phys_base holds the negative offset to the kernel, which is added |
Thomas Gleixner | 31eedd8 | 2008-02-15 17:29:12 +0100 | [diff] [blame] | 372 | * to the compile time generated pmds. This results in invalid pmds up |
| 373 | * to the point where we hit the physaddr 0 mapping. |
| 374 | * |
Yinghai Lu | e5f15b4 | 2011-02-18 11:30:30 +0000 | [diff] [blame] | 375 | * We limit the mappings to the region from _text to _brk_end. _brk_end |
| 376 | * is rounded up to the 2MB boundary. This catches the invalid pmds as |
Thomas Gleixner | 31eedd8 | 2008-02-15 17:29:12 +0100 | [diff] [blame] | 377 | * well, as they are located before _text: |
| 378 | */ |
| 379 | void __init cleanup_highmap(void) |
| 380 | { |
| 381 | unsigned long vaddr = __START_KERNEL_map; |
Yinghai Lu | 1005423 | 2013-01-24 12:19:54 -0800 | [diff] [blame] | 382 | unsigned long vaddr_end = __START_KERNEL_map + KERNEL_IMAGE_SIZE; |
Yinghai Lu | e5f15b4 | 2011-02-18 11:30:30 +0000 | [diff] [blame] | 383 | unsigned long end = roundup((unsigned long)_brk_end, PMD_SIZE) - 1; |
Thomas Gleixner | 31eedd8 | 2008-02-15 17:29:12 +0100 | [diff] [blame] | 384 | pmd_t *pmd = level2_kernel_pgt; |
Thomas Gleixner | 31eedd8 | 2008-02-15 17:29:12 +0100 | [diff] [blame] | 385 | |
Yinghai Lu | 1005423 | 2013-01-24 12:19:54 -0800 | [diff] [blame] | 386 | /* |
| 387 | * Native path, max_pfn_mapped is not set yet. |
| 388 | * Xen has valid max_pfn_mapped set in |
| 389 | * arch/x86/xen/mmu.c:xen_setup_kernel_pagetable(). |
| 390 | */ |
| 391 | if (max_pfn_mapped) |
| 392 | vaddr_end = __START_KERNEL_map + (max_pfn_mapped << PAGE_SHIFT); |
| 393 | |
Yinghai Lu | e5f15b4 | 2011-02-18 11:30:30 +0000 | [diff] [blame] | 394 | for (; vaddr + PMD_SIZE - 1 < vaddr_end; pmd++, vaddr += PMD_SIZE) { |
Hugh Dickins | 2884f11 | 2008-05-28 19:36:07 +0100 | [diff] [blame] | 395 | if (pmd_none(*pmd)) |
Thomas Gleixner | 31eedd8 | 2008-02-15 17:29:12 +0100 | [diff] [blame] | 396 | continue; |
| 397 | if (vaddr < (unsigned long) _text || vaddr > end) |
| 398 | set_pmd(pmd, __pmd(0)); |
| 399 | } |
| 400 | } |
| 401 | |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 402 | static unsigned long __meminit |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 403 | phys_pte_init(pte_t *pte_page, unsigned long addr, unsigned long end, |
| 404 | pgprot_t prot) |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 405 | { |
Yinghai Lu | eceb363 | 2012-11-16 19:38:56 -0800 | [diff] [blame] | 406 | unsigned long pages = 0, next; |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 407 | unsigned long last_map_addr = end; |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 408 | int i; |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 409 | |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 410 | pte_t *pte = pte_page + pte_index(addr); |
| 411 | |
Yinghai Lu | eceb363 | 2012-11-16 19:38:56 -0800 | [diff] [blame] | 412 | for (i = pte_index(addr); i < PTRS_PER_PTE; i++, addr = next, pte++) { |
| 413 | next = (addr & PAGE_MASK) + PAGE_SIZE; |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 414 | if (addr >= end) { |
Yinghai Lu | eceb363 | 2012-11-16 19:38:56 -0800 | [diff] [blame] | 415 | if (!after_bootmem && |
| 416 | !e820_any_mapped(addr & PAGE_MASK, next, E820_RAM) && |
| 417 | !e820_any_mapped(addr & PAGE_MASK, next, E820_RESERVED_KERN)) |
| 418 | set_pte(pte, __pte(0)); |
| 419 | continue; |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 420 | } |
| 421 | |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 422 | /* |
| 423 | * We will re-use the existing mapping. |
| 424 | * Xen for example has some special requirements, like mapping |
| 425 | * pagetable pages as RO. So assume someone who pre-setup |
| 426 | * these mappings are more intelligent. |
| 427 | */ |
Yinghai Lu | 3afa394 | 2008-10-25 22:58:21 -0700 | [diff] [blame] | 428 | if (pte_val(*pte)) { |
Jan Beulich | 876ee61 | 2012-10-04 14:48:10 +0100 | [diff] [blame] | 429 | if (!after_bootmem) |
| 430 | pages++; |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 431 | continue; |
Yinghai Lu | 3afa394 | 2008-10-25 22:58:21 -0700 | [diff] [blame] | 432 | } |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 433 | |
| 434 | if (0) |
| 435 | printk(" pte=%p addr=%lx pte=%016lx\n", |
| 436 | pte, addr, pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL).pte); |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 437 | pages++; |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 438 | set_pte(pte, pfn_pte(addr >> PAGE_SHIFT, prot)); |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 439 | last_map_addr = (addr & PAGE_MASK) + PAGE_SIZE; |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 440 | } |
Suresh Siddha | a2699e4 | 2008-09-23 14:00:38 -0700 | [diff] [blame] | 441 | |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 442 | update_page_count(PG_LEVEL_4K, pages); |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 443 | |
| 444 | return last_map_addr; |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 445 | } |
| 446 | |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 447 | static unsigned long __meminit |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 448 | phys_pmd_init(pmd_t *pmd_page, unsigned long address, unsigned long end, |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 449 | unsigned long page_size_mask, pgprot_t prot) |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 450 | { |
Jan Beulich | 20167d3 | 2012-05-16 14:06:26 +0100 | [diff] [blame] | 451 | unsigned long pages = 0, next; |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 452 | unsigned long last_map_addr = end; |
Andi Kleen | ce0c0e5 | 2008-05-02 11:46:49 +0200 | [diff] [blame] | 453 | |
Keith Mannthey | 6ad9165 | 2006-09-26 10:52:36 +0200 | [diff] [blame] | 454 | int i = pmd_index(address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | |
Jan Beulich | 20167d3 | 2012-05-16 14:06:26 +0100 | [diff] [blame] | 456 | for (; i < PTRS_PER_PMD; i++, address = next) { |
Keith Mannthey | 6ad9165 | 2006-09-26 10:52:36 +0200 | [diff] [blame] | 457 | pmd_t *pmd = pmd_page + pmd_index(address); |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 458 | pte_t *pte; |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 459 | pgprot_t new_prot = prot; |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 460 | |
Jan Beulich | 20167d3 | 2012-05-16 14:06:26 +0100 | [diff] [blame] | 461 | next = (address & PMD_MASK) + PMD_SIZE; |
Yinghai Lu | eceb363 | 2012-11-16 19:38:56 -0800 | [diff] [blame] | 462 | if (address >= end) { |
| 463 | if (!after_bootmem && |
| 464 | !e820_any_mapped(address & PMD_MASK, next, E820_RAM) && |
| 465 | !e820_any_mapped(address & PMD_MASK, next, E820_RESERVED_KERN)) |
| 466 | set_pmd(pmd, __pmd(0)); |
| 467 | continue; |
| 468 | } |
Jan Beulich | 20167d3 | 2012-05-16 14:06:26 +0100 | [diff] [blame] | 469 | |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 470 | if (pmd_val(*pmd)) { |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 471 | if (!pmd_large(*pmd)) { |
| 472 | spin_lock(&init_mm.page_table_lock); |
Yinghai Lu | 973dc4f | 2012-11-16 19:38:59 -0800 | [diff] [blame] | 473 | pte = (pte_t *)pmd_page_vaddr(*pmd); |
Yinghai Lu | 4b239f4 | 2010-12-17 16:58:28 -0800 | [diff] [blame] | 474 | last_map_addr = phys_pte_init(pte, address, |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 475 | end, prot); |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 476 | spin_unlock(&init_mm.page_table_lock); |
Suresh Siddha | a2699e4 | 2008-09-23 14:00:38 -0700 | [diff] [blame] | 477 | continue; |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 478 | } |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 479 | /* |
| 480 | * If we are ok with PG_LEVEL_2M mapping, then we will |
| 481 | * use the existing mapping, |
| 482 | * |
| 483 | * Otherwise, we will split the large page mapping but |
| 484 | * use the same existing protection bits except for |
| 485 | * large page, so that we don't violate Intel's TLB |
| 486 | * Application note (317080) which says, while changing |
| 487 | * the page sizes, new and old translations should |
| 488 | * not differ with respect to page frame and |
| 489 | * attributes. |
| 490 | */ |
Yinghai Lu | 3afa394 | 2008-10-25 22:58:21 -0700 | [diff] [blame] | 491 | if (page_size_mask & (1 << PG_LEVEL_2M)) { |
Jan Beulich | 876ee61 | 2012-10-04 14:48:10 +0100 | [diff] [blame] | 492 | if (!after_bootmem) |
| 493 | pages++; |
Jan Beulich | 20167d3 | 2012-05-16 14:06:26 +0100 | [diff] [blame] | 494 | last_map_addr = next; |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 495 | continue; |
Yinghai Lu | 3afa394 | 2008-10-25 22:58:21 -0700 | [diff] [blame] | 496 | } |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 497 | new_prot = pte_pgprot(pte_clrhuge(*(pte_t *)pmd)); |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 498 | } |
Keith Mannthey | 6ad9165 | 2006-09-26 10:52:36 +0200 | [diff] [blame] | 499 | |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 500 | if (page_size_mask & (1<<PG_LEVEL_2M)) { |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 501 | pages++; |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 502 | spin_lock(&init_mm.page_table_lock); |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 503 | set_pte((pte_t *)pmd, |
Yinghai Lu | 960ddb4f | 2012-11-16 19:38:54 -0800 | [diff] [blame] | 504 | pfn_pte((address & PMD_MASK) >> PAGE_SHIFT, |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 505 | __pgprot(pgprot_val(prot) | _PAGE_PSE))); |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 506 | spin_unlock(&init_mm.page_table_lock); |
Jan Beulich | 20167d3 | 2012-05-16 14:06:26 +0100 | [diff] [blame] | 507 | last_map_addr = next; |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 508 | continue; |
| 509 | } |
| 510 | |
Yinghai Lu | 868bf4d | 2012-11-16 19:39:00 -0800 | [diff] [blame] | 511 | pte = alloc_low_page(); |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 512 | last_map_addr = phys_pte_init(pte, address, end, new_prot); |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 513 | |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 514 | spin_lock(&init_mm.page_table_lock); |
Yinghai Lu | 868bf4d | 2012-11-16 19:39:00 -0800 | [diff] [blame] | 515 | pmd_populate_kernel(&init_mm, pmd, pte); |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 516 | spin_unlock(&init_mm.page_table_lock); |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 517 | } |
Andi Kleen | ce0c0e5 | 2008-05-02 11:46:49 +0200 | [diff] [blame] | 518 | update_page_count(PG_LEVEL_2M, pages); |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 519 | return last_map_addr; |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 520 | } |
| 521 | |
Andi Kleen | cc61503 | 2008-03-12 03:53:28 +0100 | [diff] [blame] | 522 | static unsigned long __meminit |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 523 | phys_pud_init(pud_t *pud_page, unsigned long addr, unsigned long end, |
| 524 | unsigned long page_size_mask) |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 525 | { |
Jan Beulich | 20167d3 | 2012-05-16 14:06:26 +0100 | [diff] [blame] | 526 | unsigned long pages = 0, next; |
Andi Kleen | cc61503 | 2008-03-12 03:53:28 +0100 | [diff] [blame] | 527 | unsigned long last_map_addr = end; |
Keith Mannthey | 6ad9165 | 2006-09-26 10:52:36 +0200 | [diff] [blame] | 528 | int i = pud_index(addr); |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 529 | |
Jan Beulich | 20167d3 | 2012-05-16 14:06:26 +0100 | [diff] [blame] | 530 | for (; i < PTRS_PER_PUD; i++, addr = next) { |
Keith Mannthey | 6ad9165 | 2006-09-26 10:52:36 +0200 | [diff] [blame] | 531 | pud_t *pud = pud_page + pud_index(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | pmd_t *pmd; |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 533 | pgprot_t prot = PAGE_KERNEL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | |
Jan Beulich | 20167d3 | 2012-05-16 14:06:26 +0100 | [diff] [blame] | 535 | next = (addr & PUD_MASK) + PUD_SIZE; |
Yinghai Lu | eceb363 | 2012-11-16 19:38:56 -0800 | [diff] [blame] | 536 | if (addr >= end) { |
| 537 | if (!after_bootmem && |
| 538 | !e820_any_mapped(addr & PUD_MASK, next, E820_RAM) && |
| 539 | !e820_any_mapped(addr & PUD_MASK, next, E820_RESERVED_KERN)) |
| 540 | set_pud(pud, __pud(0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | continue; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 542 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | |
Keith Mannthey | 6ad9165 | 2006-09-26 10:52:36 +0200 | [diff] [blame] | 544 | if (pud_val(*pud)) { |
Suresh Siddha | a2699e4 | 2008-09-23 14:00:38 -0700 | [diff] [blame] | 545 | if (!pud_large(*pud)) { |
Yinghai Lu | 973dc4f | 2012-11-16 19:38:59 -0800 | [diff] [blame] | 546 | pmd = pmd_offset(pud, 0); |
Yinghai Lu | 4b239f4 | 2010-12-17 16:58:28 -0800 | [diff] [blame] | 547 | last_map_addr = phys_pmd_init(pmd, addr, end, |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 548 | page_size_mask, prot); |
Yinghai Lu | 4b239f4 | 2010-12-17 16:58:28 -0800 | [diff] [blame] | 549 | __flush_tlb_all(); |
Suresh Siddha | a2699e4 | 2008-09-23 14:00:38 -0700 | [diff] [blame] | 550 | continue; |
| 551 | } |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 552 | /* |
| 553 | * If we are ok with PG_LEVEL_1G mapping, then we will |
| 554 | * use the existing mapping. |
| 555 | * |
| 556 | * Otherwise, we will split the gbpage mapping but use |
| 557 | * the same existing protection bits except for large |
| 558 | * page, so that we don't violate Intel's TLB |
| 559 | * Application note (317080) which says, while changing |
| 560 | * the page sizes, new and old translations should |
| 561 | * not differ with respect to page frame and |
| 562 | * attributes. |
| 563 | */ |
Yinghai Lu | 3afa394 | 2008-10-25 22:58:21 -0700 | [diff] [blame] | 564 | if (page_size_mask & (1 << PG_LEVEL_1G)) { |
Jan Beulich | 876ee61 | 2012-10-04 14:48:10 +0100 | [diff] [blame] | 565 | if (!after_bootmem) |
| 566 | pages++; |
Jan Beulich | 20167d3 | 2012-05-16 14:06:26 +0100 | [diff] [blame] | 567 | last_map_addr = next; |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 568 | continue; |
Yinghai Lu | 3afa394 | 2008-10-25 22:58:21 -0700 | [diff] [blame] | 569 | } |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 570 | prot = pte_pgprot(pte_clrhuge(*(pte_t *)pud)); |
Andi Kleen | ef92576 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 571 | } |
| 572 | |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 573 | if (page_size_mask & (1<<PG_LEVEL_1G)) { |
Andi Kleen | ce0c0e5 | 2008-05-02 11:46:49 +0200 | [diff] [blame] | 574 | pages++; |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 575 | spin_lock(&init_mm.page_table_lock); |
Andi Kleen | ef92576 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 576 | set_pte((pte_t *)pud, |
Yinghai Lu | 960ddb4f | 2012-11-16 19:38:54 -0800 | [diff] [blame] | 577 | pfn_pte((addr & PUD_MASK) >> PAGE_SHIFT, |
| 578 | PAGE_KERNEL_LARGE)); |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 579 | spin_unlock(&init_mm.page_table_lock); |
Jan Beulich | 20167d3 | 2012-05-16 14:06:26 +0100 | [diff] [blame] | 580 | last_map_addr = next; |
Keith Mannthey | 6ad9165 | 2006-09-26 10:52:36 +0200 | [diff] [blame] | 581 | continue; |
| 582 | } |
| 583 | |
Yinghai Lu | 868bf4d | 2012-11-16 19:39:00 -0800 | [diff] [blame] | 584 | pmd = alloc_low_page(); |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 585 | last_map_addr = phys_pmd_init(pmd, addr, end, page_size_mask, |
| 586 | prot); |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 587 | |
| 588 | spin_lock(&init_mm.page_table_lock); |
Yinghai Lu | 868bf4d | 2012-11-16 19:39:00 -0800 | [diff] [blame] | 589 | pud_populate(&init_mm, pud, pmd); |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 590 | spin_unlock(&init_mm.page_table_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | } |
Andi Kleen | 1a2b441 | 2008-01-30 13:33:54 +0100 | [diff] [blame] | 592 | __flush_tlb_all(); |
Suresh Siddha | a2699e4 | 2008-09-23 14:00:38 -0700 | [diff] [blame] | 593 | |
Andi Kleen | ce0c0e5 | 2008-05-02 11:46:49 +0200 | [diff] [blame] | 594 | update_page_count(PG_LEVEL_1G, pages); |
Andi Kleen | cc61503 | 2008-03-12 03:53:28 +0100 | [diff] [blame] | 595 | |
Yinghai Lu | 1a0db38 | 2008-06-24 14:56:20 -0700 | [diff] [blame] | 596 | return last_map_addr; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 597 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | |
Shaohua Li | 41d840e | 2009-06-12 12:57:52 +0800 | [diff] [blame] | 599 | unsigned long __meminit |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 600 | kernel_physical_mapping_init(unsigned long start, |
| 601 | unsigned long end, |
| 602 | unsigned long page_size_mask) |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 603 | { |
Haicheng Li | 9b86152 | 2010-08-20 17:50:16 +0800 | [diff] [blame] | 604 | bool pgd_changed = false; |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 605 | unsigned long next, last_map_addr = end; |
Haicheng Li | 9b86152 | 2010-08-20 17:50:16 +0800 | [diff] [blame] | 606 | unsigned long addr; |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 607 | |
| 608 | start = (unsigned long)__va(start); |
| 609 | end = (unsigned long)__va(end); |
Wu Fengguang | 1c5f50e | 2010-09-03 17:04:07 +0800 | [diff] [blame] | 610 | addr = start; |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 611 | |
| 612 | for (; start < end; start = next) { |
| 613 | pgd_t *pgd = pgd_offset_k(start); |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 614 | pud_t *pud; |
| 615 | |
Yinghai Lu | c2bdee5 | 2013-01-24 12:19:46 -0800 | [diff] [blame] | 616 | next = (start & PGDIR_MASK) + PGDIR_SIZE; |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 617 | |
| 618 | if (pgd_val(*pgd)) { |
Yinghai Lu | 973dc4f | 2012-11-16 19:38:59 -0800 | [diff] [blame] | 619 | pud = (pud_t *)pgd_page_vaddr(*pgd); |
Yinghai Lu | 4b239f4 | 2010-12-17 16:58:28 -0800 | [diff] [blame] | 620 | last_map_addr = phys_pud_init(pud, __pa(start), |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 621 | __pa(end), page_size_mask); |
| 622 | continue; |
| 623 | } |
| 624 | |
Yinghai Lu | 868bf4d | 2012-11-16 19:39:00 -0800 | [diff] [blame] | 625 | pud = alloc_low_page(); |
Yinghai Lu | c2bdee5 | 2013-01-24 12:19:46 -0800 | [diff] [blame] | 626 | last_map_addr = phys_pud_init(pud, __pa(start), __pa(end), |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 627 | page_size_mask); |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 628 | |
| 629 | spin_lock(&init_mm.page_table_lock); |
Yinghai Lu | 868bf4d | 2012-11-16 19:39:00 -0800 | [diff] [blame] | 630 | pgd_populate(&init_mm, pgd, pud); |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 631 | spin_unlock(&init_mm.page_table_lock); |
Haicheng Li | 9b86152 | 2010-08-20 17:50:16 +0800 | [diff] [blame] | 632 | pgd_changed = true; |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 633 | } |
Haicheng Li | 9b86152 | 2010-08-20 17:50:16 +0800 | [diff] [blame] | 634 | |
| 635 | if (pgd_changed) |
Wen Congyang | f73568a | 2012-12-18 12:22:21 -0800 | [diff] [blame] | 636 | sync_global_pgds(addr, end - 1); |
Haicheng Li | 9b86152 | 2010-08-20 17:50:16 +0800 | [diff] [blame] | 637 | |
Suresh Siddha | a2699e4 | 2008-09-23 14:00:38 -0700 | [diff] [blame] | 638 | __flush_tlb_all(); |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 639 | |
| 640 | return last_map_addr; |
| 641 | } |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 642 | |
Matt Tolentino | 2b97690 | 2005-06-23 00:08:06 -0700 | [diff] [blame] | 643 | #ifndef CONFIG_NUMA |
Tejun Heo | d8fc3af | 2011-02-16 12:13:06 +0100 | [diff] [blame] | 644 | void __init initmem_init(void) |
Yinghai Lu | 1f75d7e | 2008-06-22 02:44:49 -0700 | [diff] [blame] | 645 | { |
Tejun Heo | 0608f70 | 2011-07-14 11:44:23 +0200 | [diff] [blame] | 646 | memblock_set_node(0, (phys_addr_t)ULLONG_MAX, 0); |
Yinghai Lu | 1f75d7e | 2008-06-22 02:44:49 -0700 | [diff] [blame] | 647 | } |
Pekka Enberg | 3551f88 | 2009-05-07 15:35:41 +0300 | [diff] [blame] | 648 | #endif |
Yinghai Lu | 1f75d7e | 2008-06-22 02:44:49 -0700 | [diff] [blame] | 649 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | void __init paging_init(void) |
| 651 | { |
Pekka Enberg | 3551f88 | 2009-05-07 15:35:41 +0300 | [diff] [blame] | 652 | sparse_memory_present_with_active_regions(MAX_NUMNODES); |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 653 | sparse_init(); |
Yinghai Lu | 44b5728 | 2009-07-08 09:50:19 -0700 | [diff] [blame] | 654 | |
| 655 | /* |
| 656 | * clear the default setting with node 0 |
| 657 | * note: don't use nodes_clear here, that is really clearing when |
| 658 | * numa support is not compiled in, and later node_set_state |
| 659 | * will not set it back. |
| 660 | */ |
Lai Jiangshan | 4b0ef1fe | 2012-12-12 13:51:46 -0800 | [diff] [blame] | 661 | node_clear_state(0, N_MEMORY); |
| 662 | if (N_MEMORY != N_NORMAL_MEMORY) |
| 663 | node_clear_state(0, N_NORMAL_MEMORY); |
Yinghai Lu | 44b5728 | 2009-07-08 09:50:19 -0700 | [diff] [blame] | 664 | |
Pekka Enberg | 4c0b2e5 | 2011-11-01 15:58:17 +0200 | [diff] [blame] | 665 | zone_sizes_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 668 | /* |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 669 | * Memory hotplug specific functions |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 670 | */ |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 671 | #ifdef CONFIG_MEMORY_HOTPLUG |
| 672 | /* |
Shaohui Zheng | ea08541 | 2010-02-02 13:44:16 -0800 | [diff] [blame] | 673 | * After memory hotplug the variables max_pfn, max_low_pfn and high_memory need |
| 674 | * updating. |
| 675 | */ |
| 676 | static void update_end_of_memory_vars(u64 start, u64 size) |
| 677 | { |
| 678 | unsigned long end_pfn = PFN_UP(start + size); |
| 679 | |
| 680 | if (end_pfn > max_pfn) { |
| 681 | max_pfn = end_pfn; |
| 682 | max_low_pfn = end_pfn; |
| 683 | high_memory = (void *)__va(max_pfn * PAGE_SIZE - 1) + 1; |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | /* |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 688 | * Memory is added always to NORMAL zone. This means you will never get |
| 689 | * additional DMA/DMA32 memory. |
| 690 | */ |
| 691 | int arch_add_memory(int nid, u64 start, u64 size) |
| 692 | { |
| 693 | struct pglist_data *pgdat = NODE_DATA(nid); |
Christoph Lameter | 776ed98 | 2006-09-25 23:31:09 -0700 | [diff] [blame] | 694 | struct zone *zone = pgdat->node_zones + ZONE_NORMAL; |
Jacob Shin | 66520eb | 2012-11-16 19:38:52 -0800 | [diff] [blame] | 695 | unsigned long start_pfn = start >> PAGE_SHIFT; |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 696 | unsigned long nr_pages = size >> PAGE_SHIFT; |
| 697 | int ret; |
| 698 | |
Jacob Shin | 66520eb | 2012-11-16 19:38:52 -0800 | [diff] [blame] | 699 | init_memory_mapping(start, start + size); |
Keith Mannthey | 45e0b78 | 2006-09-30 23:27:09 -0700 | [diff] [blame] | 700 | |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 701 | ret = __add_pages(nid, zone, start_pfn, nr_pages); |
Gary Hade | fe8b868 | 2008-10-28 16:43:14 -0700 | [diff] [blame] | 702 | WARN_ON_ONCE(ret); |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 703 | |
Shaohui Zheng | ea08541 | 2010-02-02 13:44:16 -0800 | [diff] [blame] | 704 | /* update max_pfn, max_low_pfn and high_memory */ |
| 705 | update_end_of_memory_vars(start, size); |
| 706 | |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 707 | return ret; |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 708 | } |
| 709 | EXPORT_SYMBOL_GPL(arch_add_memory); |
| 710 | |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 711 | #define PAGE_INUSE 0xFD |
| 712 | |
| 713 | static void __meminit free_pagetable(struct page *page, int order) |
| 714 | { |
| 715 | struct zone *zone; |
| 716 | bool bootmem = false; |
| 717 | unsigned long magic; |
| 718 | unsigned int nr_pages = 1 << order; |
| 719 | |
| 720 | /* bootmem page has reserved flag */ |
| 721 | if (PageReserved(page)) { |
| 722 | __ClearPageReserved(page); |
| 723 | bootmem = true; |
| 724 | |
| 725 | magic = (unsigned long)page->lru.next; |
| 726 | if (magic == SECTION_INFO || magic == MIX_SECTION_INFO) { |
| 727 | while (nr_pages--) |
| 728 | put_page_bootmem(page++); |
| 729 | } else |
| 730 | __free_pages_bootmem(page, order); |
| 731 | } else |
| 732 | free_pages((unsigned long)page_address(page), order); |
| 733 | |
| 734 | /* |
| 735 | * SECTION_INFO pages and MIX_SECTION_INFO pages |
| 736 | * are all allocated by bootmem. |
| 737 | */ |
| 738 | if (bootmem) { |
| 739 | zone = page_zone(page); |
| 740 | zone_span_writelock(zone); |
| 741 | zone->present_pages += nr_pages; |
| 742 | zone_span_writeunlock(zone); |
| 743 | totalram_pages += nr_pages; |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | static void __meminit free_pte_table(pte_t *pte_start, pmd_t *pmd) |
| 748 | { |
| 749 | pte_t *pte; |
| 750 | int i; |
| 751 | |
| 752 | for (i = 0; i < PTRS_PER_PTE; i++) { |
| 753 | pte = pte_start + i; |
| 754 | if (pte_val(*pte)) |
| 755 | return; |
| 756 | } |
| 757 | |
| 758 | /* free a pte talbe */ |
| 759 | free_pagetable(pmd_page(*pmd), 0); |
| 760 | spin_lock(&init_mm.page_table_lock); |
| 761 | pmd_clear(pmd); |
| 762 | spin_unlock(&init_mm.page_table_lock); |
| 763 | } |
| 764 | |
| 765 | static void __meminit free_pmd_table(pmd_t *pmd_start, pud_t *pud) |
| 766 | { |
| 767 | pmd_t *pmd; |
| 768 | int i; |
| 769 | |
| 770 | for (i = 0; i < PTRS_PER_PMD; i++) { |
| 771 | pmd = pmd_start + i; |
| 772 | if (pmd_val(*pmd)) |
| 773 | return; |
| 774 | } |
| 775 | |
| 776 | /* free a pmd talbe */ |
| 777 | free_pagetable(pud_page(*pud), 0); |
| 778 | spin_lock(&init_mm.page_table_lock); |
| 779 | pud_clear(pud); |
| 780 | spin_unlock(&init_mm.page_table_lock); |
| 781 | } |
| 782 | |
| 783 | /* Return true if pgd is changed, otherwise return false. */ |
| 784 | static bool __meminit free_pud_table(pud_t *pud_start, pgd_t *pgd) |
| 785 | { |
| 786 | pud_t *pud; |
| 787 | int i; |
| 788 | |
| 789 | for (i = 0; i < PTRS_PER_PUD; i++) { |
| 790 | pud = pud_start + i; |
| 791 | if (pud_val(*pud)) |
| 792 | return false; |
| 793 | } |
| 794 | |
| 795 | /* free a pud table */ |
| 796 | free_pagetable(pgd_page(*pgd), 0); |
| 797 | spin_lock(&init_mm.page_table_lock); |
| 798 | pgd_clear(pgd); |
| 799 | spin_unlock(&init_mm.page_table_lock); |
| 800 | |
| 801 | return true; |
| 802 | } |
| 803 | |
| 804 | static void __meminit |
| 805 | remove_pte_table(pte_t *pte_start, unsigned long addr, unsigned long end, |
| 806 | bool direct) |
| 807 | { |
| 808 | unsigned long next, pages = 0; |
| 809 | pte_t *pte; |
| 810 | void *page_addr; |
| 811 | phys_addr_t phys_addr; |
| 812 | |
| 813 | pte = pte_start + pte_index(addr); |
| 814 | for (; addr < end; addr = next, pte++) { |
| 815 | next = (addr + PAGE_SIZE) & PAGE_MASK; |
| 816 | if (next > end) |
| 817 | next = end; |
| 818 | |
| 819 | if (!pte_present(*pte)) |
| 820 | continue; |
| 821 | |
| 822 | /* |
| 823 | * We mapped [0,1G) memory as identity mapping when |
| 824 | * initializing, in arch/x86/kernel/head_64.S. These |
| 825 | * pagetables cannot be removed. |
| 826 | */ |
| 827 | phys_addr = pte_val(*pte) + (addr & PAGE_MASK); |
| 828 | if (phys_addr < (phys_addr_t)0x40000000) |
| 829 | return; |
| 830 | |
| 831 | if (IS_ALIGNED(addr, PAGE_SIZE) && |
| 832 | IS_ALIGNED(next, PAGE_SIZE)) { |
| 833 | /* |
| 834 | * Do not free direct mapping pages since they were |
| 835 | * freed when offlining, or simplely not in use. |
| 836 | */ |
| 837 | if (!direct) |
| 838 | free_pagetable(pte_page(*pte), 0); |
| 839 | |
| 840 | spin_lock(&init_mm.page_table_lock); |
| 841 | pte_clear(&init_mm, addr, pte); |
| 842 | spin_unlock(&init_mm.page_table_lock); |
| 843 | |
| 844 | /* For non-direct mapping, pages means nothing. */ |
| 845 | pages++; |
| 846 | } else { |
| 847 | /* |
| 848 | * If we are here, we are freeing vmemmap pages since |
| 849 | * direct mapped memory ranges to be freed are aligned. |
| 850 | * |
| 851 | * If we are not removing the whole page, it means |
| 852 | * other page structs in this page are being used and |
| 853 | * we canot remove them. So fill the unused page_structs |
| 854 | * with 0xFD, and remove the page when it is wholly |
| 855 | * filled with 0xFD. |
| 856 | */ |
| 857 | memset((void *)addr, PAGE_INUSE, next - addr); |
| 858 | |
| 859 | page_addr = page_address(pte_page(*pte)); |
| 860 | if (!memchr_inv(page_addr, PAGE_INUSE, PAGE_SIZE)) { |
| 861 | free_pagetable(pte_page(*pte), 0); |
| 862 | |
| 863 | spin_lock(&init_mm.page_table_lock); |
| 864 | pte_clear(&init_mm, addr, pte); |
| 865 | spin_unlock(&init_mm.page_table_lock); |
| 866 | } |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | /* Call free_pte_table() in remove_pmd_table(). */ |
| 871 | flush_tlb_all(); |
| 872 | if (direct) |
| 873 | update_page_count(PG_LEVEL_4K, -pages); |
| 874 | } |
| 875 | |
| 876 | static void __meminit |
| 877 | remove_pmd_table(pmd_t *pmd_start, unsigned long addr, unsigned long end, |
| 878 | bool direct) |
| 879 | { |
| 880 | unsigned long next, pages = 0; |
| 881 | pte_t *pte_base; |
| 882 | pmd_t *pmd; |
| 883 | void *page_addr; |
| 884 | |
| 885 | pmd = pmd_start + pmd_index(addr); |
| 886 | for (; addr < end; addr = next, pmd++) { |
| 887 | next = pmd_addr_end(addr, end); |
| 888 | |
| 889 | if (!pmd_present(*pmd)) |
| 890 | continue; |
| 891 | |
| 892 | if (pmd_large(*pmd)) { |
| 893 | if (IS_ALIGNED(addr, PMD_SIZE) && |
| 894 | IS_ALIGNED(next, PMD_SIZE)) { |
| 895 | if (!direct) |
| 896 | free_pagetable(pmd_page(*pmd), |
| 897 | get_order(PMD_SIZE)); |
| 898 | |
| 899 | spin_lock(&init_mm.page_table_lock); |
| 900 | pmd_clear(pmd); |
| 901 | spin_unlock(&init_mm.page_table_lock); |
| 902 | pages++; |
| 903 | } else { |
| 904 | /* If here, we are freeing vmemmap pages. */ |
| 905 | memset((void *)addr, PAGE_INUSE, next - addr); |
| 906 | |
| 907 | page_addr = page_address(pmd_page(*pmd)); |
| 908 | if (!memchr_inv(page_addr, PAGE_INUSE, |
| 909 | PMD_SIZE)) { |
| 910 | free_pagetable(pmd_page(*pmd), |
| 911 | get_order(PMD_SIZE)); |
| 912 | |
| 913 | spin_lock(&init_mm.page_table_lock); |
| 914 | pmd_clear(pmd); |
| 915 | spin_unlock(&init_mm.page_table_lock); |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | continue; |
| 920 | } |
| 921 | |
| 922 | pte_base = (pte_t *)pmd_page_vaddr(*pmd); |
| 923 | remove_pte_table(pte_base, addr, next, direct); |
| 924 | free_pte_table(pte_base, pmd); |
| 925 | } |
| 926 | |
| 927 | /* Call free_pmd_table() in remove_pud_table(). */ |
| 928 | if (direct) |
| 929 | update_page_count(PG_LEVEL_2M, -pages); |
| 930 | } |
| 931 | |
| 932 | static void __meminit |
| 933 | remove_pud_table(pud_t *pud_start, unsigned long addr, unsigned long end, |
| 934 | bool direct) |
| 935 | { |
| 936 | unsigned long next, pages = 0; |
| 937 | pmd_t *pmd_base; |
| 938 | pud_t *pud; |
| 939 | void *page_addr; |
| 940 | |
| 941 | pud = pud_start + pud_index(addr); |
| 942 | for (; addr < end; addr = next, pud++) { |
| 943 | next = pud_addr_end(addr, end); |
| 944 | |
| 945 | if (!pud_present(*pud)) |
| 946 | continue; |
| 947 | |
| 948 | if (pud_large(*pud)) { |
| 949 | if (IS_ALIGNED(addr, PUD_SIZE) && |
| 950 | IS_ALIGNED(next, PUD_SIZE)) { |
| 951 | if (!direct) |
| 952 | free_pagetable(pud_page(*pud), |
| 953 | get_order(PUD_SIZE)); |
| 954 | |
| 955 | spin_lock(&init_mm.page_table_lock); |
| 956 | pud_clear(pud); |
| 957 | spin_unlock(&init_mm.page_table_lock); |
| 958 | pages++; |
| 959 | } else { |
| 960 | /* If here, we are freeing vmemmap pages. */ |
| 961 | memset((void *)addr, PAGE_INUSE, next - addr); |
| 962 | |
| 963 | page_addr = page_address(pud_page(*pud)); |
| 964 | if (!memchr_inv(page_addr, PAGE_INUSE, |
| 965 | PUD_SIZE)) { |
| 966 | free_pagetable(pud_page(*pud), |
| 967 | get_order(PUD_SIZE)); |
| 968 | |
| 969 | spin_lock(&init_mm.page_table_lock); |
| 970 | pud_clear(pud); |
| 971 | spin_unlock(&init_mm.page_table_lock); |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | continue; |
| 976 | } |
| 977 | |
| 978 | pmd_base = (pmd_t *)pud_page_vaddr(*pud); |
| 979 | remove_pmd_table(pmd_base, addr, next, direct); |
| 980 | free_pmd_table(pmd_base, pud); |
| 981 | } |
| 982 | |
| 983 | if (direct) |
| 984 | update_page_count(PG_LEVEL_1G, -pages); |
| 985 | } |
| 986 | |
| 987 | /* start and end are both virtual address. */ |
| 988 | static void __meminit |
| 989 | remove_pagetable(unsigned long start, unsigned long end, bool direct) |
| 990 | { |
| 991 | unsigned long next; |
| 992 | pgd_t *pgd; |
| 993 | pud_t *pud; |
| 994 | bool pgd_changed = false; |
| 995 | |
| 996 | for (; start < end; start = next) { |
| 997 | next = pgd_addr_end(start, end); |
| 998 | |
| 999 | pgd = pgd_offset_k(start); |
| 1000 | if (!pgd_present(*pgd)) |
| 1001 | continue; |
| 1002 | |
| 1003 | pud = (pud_t *)pgd_page_vaddr(*pgd); |
| 1004 | remove_pud_table(pud, start, next, direct); |
| 1005 | if (free_pud_table(pud, pgd)) |
| 1006 | pgd_changed = true; |
| 1007 | } |
| 1008 | |
| 1009 | if (pgd_changed) |
| 1010 | sync_global_pgds(start, end - 1); |
| 1011 | |
| 1012 | flush_tlb_all(); |
| 1013 | } |
| 1014 | |
Johannes Weiner | 0aad818 | 2013-04-29 15:07:50 -0700 | [diff] [blame] | 1015 | void __ref vmemmap_free(unsigned long start, unsigned long end) |
Tang Chen | 0197518 | 2013-02-22 16:33:08 -0800 | [diff] [blame] | 1016 | { |
Tang Chen | 0197518 | 2013-02-22 16:33:08 -0800 | [diff] [blame] | 1017 | remove_pagetable(start, end, false); |
| 1018 | } |
| 1019 | |
Tang Chen | 587ff8c | 2013-04-15 17:46:46 +0800 | [diff] [blame] | 1020 | #ifdef CONFIG_MEMORY_HOTREMOVE |
Tang Chen | bbcab87 | 2013-02-22 16:33:06 -0800 | [diff] [blame] | 1021 | static void __meminit |
| 1022 | kernel_physical_mapping_remove(unsigned long start, unsigned long end) |
| 1023 | { |
| 1024 | start = (unsigned long)__va(start); |
| 1025 | end = (unsigned long)__va(end); |
| 1026 | |
| 1027 | remove_pagetable(start, end, true); |
| 1028 | } |
| 1029 | |
Wen Congyang | 24d335c | 2013-02-22 16:32:58 -0800 | [diff] [blame] | 1030 | int __ref arch_remove_memory(u64 start, u64 size) |
| 1031 | { |
| 1032 | unsigned long start_pfn = start >> PAGE_SHIFT; |
| 1033 | unsigned long nr_pages = size >> PAGE_SHIFT; |
| 1034 | struct zone *zone; |
| 1035 | int ret; |
| 1036 | |
| 1037 | zone = page_zone(pfn_to_page(start_pfn)); |
Tang Chen | bbcab87 | 2013-02-22 16:33:06 -0800 | [diff] [blame] | 1038 | kernel_physical_mapping_remove(start, start + size); |
Wen Congyang | 24d335c | 2013-02-22 16:32:58 -0800 | [diff] [blame] | 1039 | ret = __remove_pages(zone, start_pfn, nr_pages); |
| 1040 | WARN_ON_ONCE(ret); |
| 1041 | |
| 1042 | return ret; |
| 1043 | } |
| 1044 | #endif |
Keith Mannthey | 45e0b78 | 2006-09-30 23:27:09 -0700 | [diff] [blame] | 1045 | #endif /* CONFIG_MEMORY_HOTPLUG */ |
| 1046 | |
KAMEZAWA Hiroyuki | 81ac3ad | 2009-09-22 16:45:49 -0700 | [diff] [blame] | 1047 | static struct kcore_list kcore_vsyscall; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | |
Yinghai Lu | 94b43c3 | 2012-11-16 19:39:19 -0800 | [diff] [blame] | 1049 | static void __init register_page_bootmem_info(void) |
| 1050 | { |
| 1051 | #ifdef CONFIG_NUMA |
| 1052 | int i; |
| 1053 | |
| 1054 | for_each_online_node(i) |
| 1055 | register_page_bootmem_info_node(NODE_DATA(i)); |
| 1056 | #endif |
| 1057 | } |
| 1058 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | void __init mem_init(void) |
| 1060 | { |
Andi Kleen | 0a43e4b | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 1061 | long codesize, reservedpages, datasize, initsize; |
Yinghai Lu | 11a6b0c | 2008-10-14 18:59:18 -0700 | [diff] [blame] | 1062 | unsigned long absent_pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | |
Jon Mason | 0dc243a | 2006-06-26 13:58:11 +0200 | [diff] [blame] | 1064 | pci_iommu_alloc(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | |
Yinghai Lu | 48ddb15 | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 1066 | /* clear_bss() already clear the empty_zero_page */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | |
Yinghai Lu | 94b43c3 | 2012-11-16 19:39:19 -0800 | [diff] [blame] | 1068 | register_page_bootmem_info(); |
Jiang Liu | bced0e3 | 2013-04-29 15:06:53 -0700 | [diff] [blame] | 1069 | |
| 1070 | /* this will put all memory onto the freelists */ |
Andi Kleen | 0a43e4b | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 1071 | totalram_pages = free_all_bootmem(); |
Yinghai Lu | 11a6b0c | 2008-10-14 18:59:18 -0700 | [diff] [blame] | 1072 | |
| 1073 | absent_pages = absent_pages_in_range(0, max_pfn); |
| 1074 | reservedpages = max_pfn - totalram_pages - absent_pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | after_bootmem = 1; |
| 1076 | |
| 1077 | codesize = (unsigned long) &_etext - (unsigned long) &_text; |
| 1078 | datasize = (unsigned long) &_edata - (unsigned long) &_etext; |
| 1079 | initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin; |
| 1080 | |
| 1081 | /* Register memory areas for /proc/kcore */ |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1082 | kclist_add(&kcore_vsyscall, (void *)VSYSCALL_START, |
KAMEZAWA Hiroyuki | c30bb2a | 2009-09-22 16:45:43 -0700 | [diff] [blame] | 1083 | VSYSCALL_END - VSYSCALL_START, KCORE_OTHER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | |
Ingo Molnar | 10f22dd | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1085 | printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, " |
Yinghai Lu | 11a6b0c | 2008-10-14 18:59:18 -0700 | [diff] [blame] | 1086 | "%ldk absent, %ldk reserved, %ldk data, %ldk init)\n", |
Geert Uytterhoeven | cc013a8 | 2009-09-21 17:02:36 -0700 | [diff] [blame] | 1087 | nr_free_pages() << (PAGE_SHIFT-10), |
Yinghai Lu | c987d12 | 2008-06-24 22:14:09 -0700 | [diff] [blame] | 1088 | max_pfn << (PAGE_SHIFT-10), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | codesize >> 10, |
Yinghai Lu | 11a6b0c | 2008-10-14 18:59:18 -0700 | [diff] [blame] | 1090 | absent_pages << (PAGE_SHIFT-10), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | reservedpages << (PAGE_SHIFT-10), |
| 1092 | datasize >> 10, |
| 1093 | initsize >> 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | } |
| 1095 | |
Arjan van de Ven | 67df197 | 2006-01-06 00:12:04 -0800 | [diff] [blame] | 1096 | #ifdef CONFIG_DEBUG_RODATA |
Arjan van de Ven | edeed30 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 1097 | const int rodata_test_data = 0xC3; |
| 1098 | EXPORT_SYMBOL_GPL(rodata_test_data); |
Arjan van de Ven | 67df197 | 2006-01-06 00:12:04 -0800 | [diff] [blame] | 1099 | |
Suresh Siddha | 502f660 | 2009-10-28 18:46:56 -0800 | [diff] [blame] | 1100 | int kernel_set_to_readonly; |
Steven Rostedt | 1623963 | 2009-02-17 17:57:30 -0500 | [diff] [blame] | 1101 | |
| 1102 | void set_kernel_text_rw(void) |
| 1103 | { |
Suresh Siddha | b9af7c0 | 2009-10-14 14:46:55 -0700 | [diff] [blame] | 1104 | unsigned long start = PFN_ALIGN(_text); |
Suresh Siddha | e7d23dd | 2009-10-28 18:46:58 -0800 | [diff] [blame] | 1105 | unsigned long end = PFN_ALIGN(__stop___ex_table); |
Steven Rostedt | 1623963 | 2009-02-17 17:57:30 -0500 | [diff] [blame] | 1106 | |
| 1107 | if (!kernel_set_to_readonly) |
| 1108 | return; |
| 1109 | |
| 1110 | pr_debug("Set kernel text: %lx - %lx for read write\n", |
| 1111 | start, end); |
| 1112 | |
Suresh Siddha | e7d23dd | 2009-10-28 18:46:58 -0800 | [diff] [blame] | 1113 | /* |
| 1114 | * Make the kernel identity mapping for text RW. Kernel text |
| 1115 | * mapping will always be RO. Refer to the comment in |
| 1116 | * static_protections() in pageattr.c |
| 1117 | */ |
Steven Rostedt | 1623963 | 2009-02-17 17:57:30 -0500 | [diff] [blame] | 1118 | set_memory_rw(start, (end - start) >> PAGE_SHIFT); |
| 1119 | } |
| 1120 | |
| 1121 | void set_kernel_text_ro(void) |
| 1122 | { |
Suresh Siddha | b9af7c0 | 2009-10-14 14:46:55 -0700 | [diff] [blame] | 1123 | unsigned long start = PFN_ALIGN(_text); |
Suresh Siddha | e7d23dd | 2009-10-28 18:46:58 -0800 | [diff] [blame] | 1124 | unsigned long end = PFN_ALIGN(__stop___ex_table); |
Steven Rostedt | 1623963 | 2009-02-17 17:57:30 -0500 | [diff] [blame] | 1125 | |
| 1126 | if (!kernel_set_to_readonly) |
| 1127 | return; |
| 1128 | |
| 1129 | pr_debug("Set kernel text: %lx - %lx for read only\n", |
| 1130 | start, end); |
| 1131 | |
Suresh Siddha | e7d23dd | 2009-10-28 18:46:58 -0800 | [diff] [blame] | 1132 | /* |
| 1133 | * Set the kernel identity mapping for text RO. |
| 1134 | */ |
Steven Rostedt | 1623963 | 2009-02-17 17:57:30 -0500 | [diff] [blame] | 1135 | set_memory_ro(start, (end - start) >> PAGE_SHIFT); |
| 1136 | } |
| 1137 | |
Arjan van de Ven | 67df197 | 2006-01-06 00:12:04 -0800 | [diff] [blame] | 1138 | void mark_rodata_ro(void) |
| 1139 | { |
Suresh Siddha | 74e0817 | 2009-10-14 14:46:56 -0700 | [diff] [blame] | 1140 | unsigned long start = PFN_ALIGN(_text); |
Alexander Duyck | fc8d782 | 2012-11-16 13:57:13 -0800 | [diff] [blame] | 1141 | unsigned long rodata_start = PFN_ALIGN(__start_rodata); |
Suresh Siddha | 74e0817 | 2009-10-14 14:46:56 -0700 | [diff] [blame] | 1142 | unsigned long end = (unsigned long) &__end_rodata_hpage_align; |
Alexander Duyck | fc8d782 | 2012-11-16 13:57:13 -0800 | [diff] [blame] | 1143 | unsigned long text_end = PFN_ALIGN(&__stop___ex_table); |
| 1144 | unsigned long rodata_end = PFN_ALIGN(&__end_rodata); |
Yinghai Lu | 7221267 | 2013-01-24 12:20:13 -0800 | [diff] [blame] | 1145 | unsigned long all_end = PFN_ALIGN(&_end); |
Steven Rostedt | 8f0f996 | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 1146 | |
Jan Beulich | 6fb1475 | 2007-05-02 19:27:10 +0200 | [diff] [blame] | 1147 | printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n", |
Linus Torvalds | e3ebadd | 2007-05-07 08:44:24 -0700 | [diff] [blame] | 1148 | (end - start) >> 10); |
Arjan van de Ven | 984bb80 | 2008-02-06 22:39:45 +0100 | [diff] [blame] | 1149 | set_memory_ro(start, (end - start) >> PAGE_SHIFT); |
| 1150 | |
Steven Rostedt | 1623963 | 2009-02-17 17:57:30 -0500 | [diff] [blame] | 1151 | kernel_set_to_readonly = 1; |
| 1152 | |
Arjan van de Ven | 984bb80 | 2008-02-06 22:39:45 +0100 | [diff] [blame] | 1153 | /* |
Yinghai Lu | 7221267 | 2013-01-24 12:20:13 -0800 | [diff] [blame] | 1154 | * The rodata/data/bss/brk section (but not the kernel text!) |
| 1155 | * should also be not-executable. |
Arjan van de Ven | 984bb80 | 2008-02-06 22:39:45 +0100 | [diff] [blame] | 1156 | */ |
Yinghai Lu | 7221267 | 2013-01-24 12:20:13 -0800 | [diff] [blame] | 1157 | set_memory_nx(rodata_start, (all_end - rodata_start) >> PAGE_SHIFT); |
Arjan van de Ven | 67df197 | 2006-01-06 00:12:04 -0800 | [diff] [blame] | 1158 | |
Arjan van de Ven | 1a48725 | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 1159 | rodata_test(); |
| 1160 | |
Andi Kleen | 0c42f39 | 2008-01-30 13:33:42 +0100 | [diff] [blame] | 1161 | #ifdef CONFIG_CPA_DEBUG |
Ingo Molnar | 10f22dd | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1162 | printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end); |
Arjan van de Ven | 6d238cc | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 1163 | set_memory_rw(start, (end-start) >> PAGE_SHIFT); |
Andi Kleen | 0c42f39 | 2008-01-30 13:33:42 +0100 | [diff] [blame] | 1164 | |
Ingo Molnar | 10f22dd | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1165 | printk(KERN_INFO "Testing CPA: again\n"); |
Arjan van de Ven | 6d238cc | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 1166 | set_memory_ro(start, (end-start) >> PAGE_SHIFT); |
Andi Kleen | 0c42f39 | 2008-01-30 13:33:42 +0100 | [diff] [blame] | 1167 | #endif |
Suresh Siddha | 74e0817 | 2009-10-14 14:46:56 -0700 | [diff] [blame] | 1168 | |
| 1169 | free_init_pages("unused kernel memory", |
Alexander Duyck | fc8d782 | 2012-11-16 13:57:13 -0800 | [diff] [blame] | 1170 | (unsigned long) __va(__pa_symbol(text_end)), |
| 1171 | (unsigned long) __va(__pa_symbol(rodata_start))); |
| 1172 | |
Suresh Siddha | 74e0817 | 2009-10-14 14:46:56 -0700 | [diff] [blame] | 1173 | free_init_pages("unused kernel memory", |
Alexander Duyck | fc8d782 | 2012-11-16 13:57:13 -0800 | [diff] [blame] | 1174 | (unsigned long) __va(__pa_symbol(rodata_end)), |
| 1175 | (unsigned long) __va(__pa_symbol(_sdata))); |
Arjan van de Ven | 67df197 | 2006-01-06 00:12:04 -0800 | [diff] [blame] | 1176 | } |
Mathieu Desnoyers | 4e4eee0 | 2008-02-02 15:42:20 -0500 | [diff] [blame] | 1177 | |
Arjan van de Ven | 67df197 | 2006-01-06 00:12:04 -0800 | [diff] [blame] | 1178 | #endif |
| 1179 | |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1180 | int kern_addr_valid(unsigned long addr) |
| 1181 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1183 | pgd_t *pgd; |
| 1184 | pud_t *pud; |
| 1185 | pmd_t *pmd; |
| 1186 | pte_t *pte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | |
| 1188 | if (above != 0 && above != -1UL) |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1189 | return 0; |
| 1190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | pgd = pgd_offset_k(addr); |
| 1192 | if (pgd_none(*pgd)) |
| 1193 | return 0; |
| 1194 | |
| 1195 | pud = pud_offset(pgd, addr); |
| 1196 | if (pud_none(*pud)) |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1197 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | |
Mel Gorman | 0ee364e | 2013-02-11 14:52:36 +0000 | [diff] [blame] | 1199 | if (pud_large(*pud)) |
| 1200 | return pfn_valid(pud_pfn(*pud)); |
| 1201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | pmd = pmd_offset(pud, addr); |
| 1203 | if (pmd_none(*pmd)) |
| 1204 | return 0; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | if (pmd_large(*pmd)) |
| 1207 | return pfn_valid(pmd_pfn(*pmd)); |
| 1208 | |
| 1209 | pte = pte_offset_kernel(pmd, addr); |
| 1210 | if (pte_none(*pte)) |
| 1211 | return 0; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | return pfn_valid(pte_pfn(*pte)); |
| 1214 | } |
| 1215 | |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1216 | /* |
| 1217 | * A pseudo VMA to allow ptrace access for the vsyscall page. This only |
| 1218 | * covers the 64bit vsyscall page now. 32bit has a real VMA now and does |
| 1219 | * not need special handling anymore: |
| 1220 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | static struct vm_area_struct gate_vma = { |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1222 | .vm_start = VSYSCALL_START, |
| 1223 | .vm_end = VSYSCALL_START + (VSYSCALL_MAPPED_PAGES * PAGE_SIZE), |
| 1224 | .vm_page_prot = PAGE_READONLY_EXEC, |
| 1225 | .vm_flags = VM_READ | VM_EXEC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | }; |
| 1227 | |
Stephen Wilson | 31db58b | 2011-03-13 15:49:15 -0400 | [diff] [blame] | 1228 | struct vm_area_struct *get_gate_vma(struct mm_struct *mm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | { |
| 1230 | #ifdef CONFIG_IA32_EMULATION |
Stephen Wilson | 31db58b | 2011-03-13 15:49:15 -0400 | [diff] [blame] | 1231 | if (!mm || mm->context.ia32_compat) |
Andi Kleen | 1e01441 | 2005-04-16 15:24:55 -0700 | [diff] [blame] | 1232 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | #endif |
| 1234 | return &gate_vma; |
| 1235 | } |
| 1236 | |
Stephen Wilson | 83b964b | 2011-03-13 15:49:16 -0400 | [diff] [blame] | 1237 | int in_gate_area(struct mm_struct *mm, unsigned long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | { |
Stephen Wilson | 83b964b | 2011-03-13 15:49:16 -0400 | [diff] [blame] | 1239 | struct vm_area_struct *vma = get_gate_vma(mm); |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1240 | |
Andi Kleen | 1e01441 | 2005-04-16 15:24:55 -0700 | [diff] [blame] | 1241 | if (!vma) |
| 1242 | return 0; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | return (addr >= vma->vm_start) && (addr < vma->vm_end); |
| 1245 | } |
| 1246 | |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1247 | /* |
Stephen Wilson | cae5d39 | 2011-03-13 15:49:17 -0400 | [diff] [blame] | 1248 | * Use this when you have no reliable mm, typically from interrupt |
| 1249 | * context. It is less reliable than using a task's mm and may give |
| 1250 | * false positives. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | */ |
Stephen Wilson | cae5d39 | 2011-03-13 15:49:17 -0400 | [diff] [blame] | 1252 | int in_gate_area_no_mm(unsigned long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | { |
Andi Kleen | 1e01441 | 2005-04-16 15:24:55 -0700 | [diff] [blame] | 1254 | return (addr >= VSYSCALL_START) && (addr < VSYSCALL_END); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | } |
Zou Nan hai | 2e1c49d | 2007-06-01 00:46:28 -0700 | [diff] [blame] | 1256 | |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 1257 | const char *arch_vma_name(struct vm_area_struct *vma) |
| 1258 | { |
| 1259 | if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso) |
| 1260 | return "[vdso]"; |
| 1261 | if (vma == &gate_vma) |
| 1262 | return "[vsyscall]"; |
| 1263 | return NULL; |
| 1264 | } |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 1265 | |
Nathan Fontenot | 1dc41aa | 2011-01-20 10:46:15 -0600 | [diff] [blame] | 1266 | #ifdef CONFIG_X86_UV |
Nathan Fontenot | 1dc41aa | 2011-01-20 10:46:15 -0600 | [diff] [blame] | 1267 | unsigned long memory_block_size_bytes(void) |
| 1268 | { |
| 1269 | if (is_uv_system()) { |
| 1270 | printk(KERN_INFO "UV: memory block size 2GB\n"); |
| 1271 | return 2UL * 1024 * 1024 * 1024; |
| 1272 | } |
| 1273 | return MIN_MEMORY_BLOCK_SIZE; |
| 1274 | } |
| 1275 | #endif |
| 1276 | |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 1277 | #ifdef CONFIG_SPARSEMEM_VMEMMAP |
| 1278 | /* |
| 1279 | * Initialise the sparsemem vmemmap using huge-pages at the PMD level. |
| 1280 | */ |
Yinghai Lu | c2b91e2 | 2008-04-12 01:19:24 -0700 | [diff] [blame] | 1281 | static long __meminitdata addr_start, addr_end; |
| 1282 | static void __meminitdata *p_start, *p_end; |
| 1283 | static int __meminitdata node_start; |
| 1284 | |
Johannes Weiner | e8216da | 2013-04-29 15:07:54 -0700 | [diff] [blame] | 1285 | static int __meminit vmemmap_populate_hugepages(unsigned long start, |
| 1286 | unsigned long end, int node) |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 1287 | { |
Johannes Weiner | 0aad818 | 2013-04-29 15:07:50 -0700 | [diff] [blame] | 1288 | unsigned long addr; |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 1289 | unsigned long next; |
| 1290 | pgd_t *pgd; |
| 1291 | pud_t *pud; |
| 1292 | pmd_t *pmd; |
| 1293 | |
Johannes Weiner | 0aad818 | 2013-04-29 15:07:50 -0700 | [diff] [blame] | 1294 | for (addr = start; addr < end; addr = next) { |
Johannes Weiner | e8216da | 2013-04-29 15:07:54 -0700 | [diff] [blame] | 1295 | next = pmd_addr_end(addr, end); |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 1296 | |
| 1297 | pgd = vmemmap_pgd_populate(addr, node); |
| 1298 | if (!pgd) |
| 1299 | return -ENOMEM; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1300 | |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 1301 | pud = vmemmap_pud_populate(pgd, addr, node); |
| 1302 | if (!pud) |
| 1303 | return -ENOMEM; |
| 1304 | |
Johannes Weiner | e8216da | 2013-04-29 15:07:54 -0700 | [diff] [blame] | 1305 | pmd = pmd_offset(pud, addr); |
| 1306 | if (pmd_none(*pmd)) { |
Johannes Weiner | e8216da | 2013-04-29 15:07:54 -0700 | [diff] [blame] | 1307 | void *p; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1308 | |
Johannes Weiner | e8216da | 2013-04-29 15:07:54 -0700 | [diff] [blame] | 1309 | p = vmemmap_alloc_block_buf(PMD_SIZE, node); |
Johannes Weiner | 8e2cdbc | 2013-04-29 15:07:56 -0700 | [diff] [blame] | 1310 | if (p) { |
| 1311 | pte_t entry; |
Jeremy Fitzhardinge | 7c934d3 | 2008-06-25 00:19:20 -0400 | [diff] [blame] | 1312 | |
Johannes Weiner | 8e2cdbc | 2013-04-29 15:07:56 -0700 | [diff] [blame] | 1313 | entry = pfn_pte(__pa(p) >> PAGE_SHIFT, |
| 1314 | PAGE_KERNEL_LARGE); |
| 1315 | set_pmd(pmd, __pmd(pte_val(entry))); |
Jeremy Fitzhardinge | 7c934d3 | 2008-06-25 00:19:20 -0400 | [diff] [blame] | 1316 | |
Johannes Weiner | 8e2cdbc | 2013-04-29 15:07:56 -0700 | [diff] [blame] | 1317 | /* check to see if we have contiguous blocks */ |
| 1318 | if (p_end != p || node_start != node) { |
| 1319 | if (p_start) |
| 1320 | printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n", |
| 1321 | addr_start, addr_end-1, p_start, p_end-1, node_start); |
| 1322 | addr_start = addr; |
| 1323 | node_start = node; |
| 1324 | p_start = p; |
| 1325 | } |
| 1326 | |
| 1327 | addr_end = addr + PMD_SIZE; |
| 1328 | p_end = p + PMD_SIZE; |
| 1329 | continue; |
Johannes Weiner | e8216da | 2013-04-29 15:07:54 -0700 | [diff] [blame] | 1330 | } |
Johannes Weiner | 8e2cdbc | 2013-04-29 15:07:56 -0700 | [diff] [blame] | 1331 | } else if (pmd_large(*pmd)) { |
Johannes Weiner | e8216da | 2013-04-29 15:07:54 -0700 | [diff] [blame] | 1332 | vmemmap_verify((pte_t *)pmd, node, addr, next); |
Johannes Weiner | 8e2cdbc | 2013-04-29 15:07:56 -0700 | [diff] [blame] | 1333 | continue; |
| 1334 | } |
| 1335 | pr_warn_once("vmemmap: falling back to regular page backing\n"); |
| 1336 | if (vmemmap_populate_basepages(addr, next, node)) |
| 1337 | return -ENOMEM; |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 1338 | } |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 1339 | return 0; |
| 1340 | } |
Yinghai Lu | c2b91e2 | 2008-04-12 01:19:24 -0700 | [diff] [blame] | 1341 | |
Johannes Weiner | e8216da | 2013-04-29 15:07:54 -0700 | [diff] [blame] | 1342 | int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node) |
| 1343 | { |
| 1344 | int err; |
| 1345 | |
| 1346 | if (cpu_has_pse) |
| 1347 | err = vmemmap_populate_hugepages(start, end, node); |
| 1348 | else |
| 1349 | err = vmemmap_populate_basepages(start, end, node); |
| 1350 | if (!err) |
| 1351 | sync_global_pgds(start, end - 1); |
| 1352 | return err; |
| 1353 | } |
| 1354 | |
Yasuaki Ishimatsu | 46723bf | 2013-02-22 16:33:00 -0800 | [diff] [blame] | 1355 | #if defined(CONFIG_MEMORY_HOTPLUG_SPARSE) && defined(CONFIG_HAVE_BOOTMEM_INFO_NODE) |
| 1356 | void register_page_bootmem_memmap(unsigned long section_nr, |
| 1357 | struct page *start_page, unsigned long size) |
| 1358 | { |
| 1359 | unsigned long addr = (unsigned long)start_page; |
| 1360 | unsigned long end = (unsigned long)(start_page + size); |
| 1361 | unsigned long next; |
| 1362 | pgd_t *pgd; |
| 1363 | pud_t *pud; |
| 1364 | pmd_t *pmd; |
| 1365 | unsigned int nr_pages; |
| 1366 | struct page *page; |
| 1367 | |
| 1368 | for (; addr < end; addr = next) { |
| 1369 | pte_t *pte = NULL; |
| 1370 | |
| 1371 | pgd = pgd_offset_k(addr); |
| 1372 | if (pgd_none(*pgd)) { |
| 1373 | next = (addr + PAGE_SIZE) & PAGE_MASK; |
| 1374 | continue; |
| 1375 | } |
| 1376 | get_page_bootmem(section_nr, pgd_page(*pgd), MIX_SECTION_INFO); |
| 1377 | |
| 1378 | pud = pud_offset(pgd, addr); |
| 1379 | if (pud_none(*pud)) { |
| 1380 | next = (addr + PAGE_SIZE) & PAGE_MASK; |
| 1381 | continue; |
| 1382 | } |
| 1383 | get_page_bootmem(section_nr, pud_page(*pud), MIX_SECTION_INFO); |
| 1384 | |
| 1385 | if (!cpu_has_pse) { |
| 1386 | next = (addr + PAGE_SIZE) & PAGE_MASK; |
| 1387 | pmd = pmd_offset(pud, addr); |
| 1388 | if (pmd_none(*pmd)) |
| 1389 | continue; |
| 1390 | get_page_bootmem(section_nr, pmd_page(*pmd), |
| 1391 | MIX_SECTION_INFO); |
| 1392 | |
| 1393 | pte = pte_offset_kernel(pmd, addr); |
| 1394 | if (pte_none(*pte)) |
| 1395 | continue; |
| 1396 | get_page_bootmem(section_nr, pte_page(*pte), |
| 1397 | SECTION_INFO); |
| 1398 | } else { |
| 1399 | next = pmd_addr_end(addr, end); |
| 1400 | |
| 1401 | pmd = pmd_offset(pud, addr); |
| 1402 | if (pmd_none(*pmd)) |
| 1403 | continue; |
| 1404 | |
| 1405 | nr_pages = 1 << (get_order(PMD_SIZE)); |
| 1406 | page = pmd_page(*pmd); |
| 1407 | while (nr_pages--) |
| 1408 | get_page_bootmem(section_nr, page++, |
| 1409 | SECTION_INFO); |
| 1410 | } |
| 1411 | } |
| 1412 | } |
| 1413 | #endif |
| 1414 | |
Yinghai Lu | c2b91e2 | 2008-04-12 01:19:24 -0700 | [diff] [blame] | 1415 | void __meminit vmemmap_populate_print_last(void) |
| 1416 | { |
| 1417 | if (p_start) { |
| 1418 | printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n", |
| 1419 | addr_start, addr_end-1, p_start, p_end-1, node_start); |
| 1420 | p_start = NULL; |
| 1421 | p_end = NULL; |
| 1422 | node_start = 0; |
| 1423 | } |
| 1424 | } |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 1425 | #endif |