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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
| 36 | #include <asm/processor.h> |
Ingo Molnar | 46eaa67 | 2008-10-12 15:06:29 +0200 | [diff] [blame] | 37 | #include <asm/bios_ebda.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <asm/uaccess.h> |
| 39 | #include <asm/pgtable.h> |
| 40 | #include <asm/pgalloc.h> |
| 41 | #include <asm/dma.h> |
| 42 | #include <asm/fixmap.h> |
| 43 | #include <asm/e820.h> |
| 44 | #include <asm/apic.h> |
| 45 | #include <asm/tlb.h> |
| 46 | #include <asm/mmu_context.h> |
| 47 | #include <asm/proto.h> |
| 48 | #include <asm/smp.h> |
Andi Kleen | 2bc0414 | 2005-11-05 17:25:53 +0100 | [diff] [blame] | 49 | #include <asm/sections.h> |
Thomas Gleixner | 718fc13 | 2008-01-30 13:30:17 +0100 | [diff] [blame] | 50 | #include <asm/kdebug.h> |
Thomas Gleixner | aaa64e0 | 2008-01-30 13:30:17 +0100 | [diff] [blame] | 51 | #include <asm/numa.h> |
Harvey Harrison | 7bfeab9 | 2008-02-12 12:12:01 -0800 | [diff] [blame] | 52 | #include <asm/cacheflush.h> |
Pekka Enberg | 4fcb208 | 2009-03-05 14:55:08 +0200 | [diff] [blame] | 53 | #include <asm/init.h> |
Nathan Fontenot | 1dc41aa | 2011-01-20 10:46:15 -0600 | [diff] [blame] | 54 | #include <asm/uv/uv.h> |
Yinghai Lu | e5f15b4 | 2011-02-18 11:30:30 +0000 | [diff] [blame] | 55 | #include <asm/setup.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Yinghai Lu | 5c51bdb | 2012-11-16 19:39:01 -0800 | [diff] [blame] | 57 | #include "mm_internal.h" |
| 58 | |
Yinghai Lu | aece278 | 2013-01-24 12:19:48 -0800 | [diff] [blame] | 59 | static void ident_pmd_init(unsigned long pmd_flag, pmd_t *pmd_page, |
| 60 | unsigned long addr, unsigned long end) |
| 61 | { |
| 62 | addr &= PMD_MASK; |
| 63 | for (; addr < end; addr += PMD_SIZE) { |
| 64 | pmd_t *pmd = pmd_page + pmd_index(addr); |
| 65 | |
| 66 | if (!pmd_present(*pmd)) |
| 67 | set_pmd(pmd, __pmd(addr | pmd_flag)); |
| 68 | } |
| 69 | } |
| 70 | static int ident_pud_init(struct x86_mapping_info *info, pud_t *pud_page, |
| 71 | unsigned long addr, unsigned long end) |
| 72 | { |
| 73 | unsigned long next; |
| 74 | |
| 75 | for (; addr < end; addr = next) { |
| 76 | pud_t *pud = pud_page + pud_index(addr); |
| 77 | pmd_t *pmd; |
| 78 | |
| 79 | next = (addr & PUD_MASK) + PUD_SIZE; |
| 80 | if (next > end) |
| 81 | next = end; |
| 82 | |
| 83 | if (pud_present(*pud)) { |
| 84 | pmd = pmd_offset(pud, 0); |
| 85 | ident_pmd_init(info->pmd_flag, pmd, addr, next); |
| 86 | continue; |
| 87 | } |
| 88 | pmd = (pmd_t *)info->alloc_pgt_page(info->context); |
| 89 | if (!pmd) |
| 90 | return -ENOMEM; |
| 91 | ident_pmd_init(info->pmd_flag, pmd, addr, next); |
| 92 | set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE)); |
| 93 | } |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | int kernel_ident_mapping_init(struct x86_mapping_info *info, pgd_t *pgd_page, |
| 99 | unsigned long addr, unsigned long end) |
| 100 | { |
| 101 | unsigned long next; |
| 102 | int result; |
| 103 | int off = info->kernel_mapping ? pgd_index(__PAGE_OFFSET) : 0; |
| 104 | |
| 105 | for (; addr < end; addr = next) { |
| 106 | pgd_t *pgd = pgd_page + pgd_index(addr) + off; |
| 107 | pud_t *pud; |
| 108 | |
| 109 | next = (addr & PGDIR_MASK) + PGDIR_SIZE; |
| 110 | if (next > end) |
| 111 | next = end; |
| 112 | |
| 113 | if (pgd_present(*pgd)) { |
| 114 | pud = pud_offset(pgd, 0); |
| 115 | result = ident_pud_init(info, pud, addr, next); |
| 116 | if (result) |
| 117 | return result; |
| 118 | continue; |
| 119 | } |
| 120 | |
| 121 | pud = (pud_t *)info->alloc_pgt_page(info->context); |
| 122 | if (!pud) |
| 123 | return -ENOMEM; |
| 124 | result = ident_pud_init(info, pud, addr, next); |
| 125 | if (result) |
| 126 | return result; |
| 127 | set_pgd(pgd, __pgd(__pa(pud) | _KERNPG_TABLE)); |
| 128 | } |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
Ingo Molnar | 00d1c5e | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 133 | static int __init parse_direct_gbpages_off(char *arg) |
| 134 | { |
| 135 | direct_gbpages = 0; |
| 136 | return 0; |
| 137 | } |
| 138 | early_param("nogbpages", parse_direct_gbpages_off); |
| 139 | |
| 140 | static int __init parse_direct_gbpages_on(char *arg) |
| 141 | { |
| 142 | direct_gbpages = 1; |
| 143 | return 0; |
| 144 | } |
| 145 | early_param("gbpages", parse_direct_gbpages_on); |
| 146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | /* |
| 148 | * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the |
| 149 | * physical space so we can cache the place of the first one and move |
| 150 | * around without checking the pgd every time. |
| 151 | */ |
| 152 | |
Jeremy Fitzhardinge | be43d72 | 2008-09-07 15:21:13 -0700 | [diff] [blame] | 153 | pteval_t __supported_pte_mask __read_mostly = ~_PAGE_IOMAP; |
Yinghai Lu | bd220a2 | 2008-09-05 00:58:28 -0700 | [diff] [blame] | 154 | EXPORT_SYMBOL_GPL(__supported_pte_mask); |
| 155 | |
Yinghai Lu | bd220a2 | 2008-09-05 00:58:28 -0700 | [diff] [blame] | 156 | int force_personality32; |
| 157 | |
Ingo Molnar | deed05b | 2008-09-05 10:23:26 +0200 | [diff] [blame] | 158 | /* |
| 159 | * noexec32=on|off |
| 160 | * Control non executable heap for 32bit processes. |
| 161 | * To control the stack too use noexec=off |
| 162 | * |
| 163 | * on PROT_READ does not imply PROT_EXEC for 32-bit processes (default) |
| 164 | * off PROT_READ implies PROT_EXEC |
| 165 | */ |
Yinghai Lu | bd220a2 | 2008-09-05 00:58:28 -0700 | [diff] [blame] | 166 | static int __init nonx32_setup(char *str) |
| 167 | { |
| 168 | if (!strcmp(str, "on")) |
| 169 | force_personality32 &= ~READ_IMPLIES_EXEC; |
| 170 | else if (!strcmp(str, "off")) |
| 171 | force_personality32 |= READ_IMPLIES_EXEC; |
| 172 | return 1; |
| 173 | } |
| 174 | __setup("noexec32=", nonx32_setup); |
| 175 | |
Marcin Slusarz | 8d6ea967 | 2008-08-15 18:32:24 +0200 | [diff] [blame] | 176 | /* |
Haicheng Li | 6afb515 | 2010-05-19 17:42:14 +0800 | [diff] [blame] | 177 | * When memory was added/removed make sure all the processes MM have |
| 178 | * suitable PGD entries in the local PGD level page. |
| 179 | */ |
| 180 | void sync_global_pgds(unsigned long start, unsigned long end) |
| 181 | { |
Jeremy Fitzhardinge | 44235dc | 2010-10-14 17:04:59 -0700 | [diff] [blame] | 182 | unsigned long address; |
Haicheng Li | 6afb515 | 2010-05-19 17:42:14 +0800 | [diff] [blame] | 183 | |
Jeremy Fitzhardinge | 44235dc | 2010-10-14 17:04:59 -0700 | [diff] [blame] | 184 | for (address = start; address <= end; address += PGDIR_SIZE) { |
| 185 | const pgd_t *pgd_ref = pgd_offset_k(address); |
Jeremy Fitzhardinge | 44235dc | 2010-10-14 17:04:59 -0700 | [diff] [blame] | 186 | struct page *page; |
Haicheng Li | 6afb515 | 2010-05-19 17:42:14 +0800 | [diff] [blame] | 187 | |
Jeremy Fitzhardinge | 44235dc | 2010-10-14 17:04:59 -0700 | [diff] [blame] | 188 | if (pgd_none(*pgd_ref)) |
| 189 | continue; |
Haicheng Li | 6afb515 | 2010-05-19 17:42:14 +0800 | [diff] [blame] | 190 | |
Andrea Arcangeli | a79e53d | 2011-02-16 15:45:22 -0800 | [diff] [blame] | 191 | spin_lock(&pgd_lock); |
Jeremy Fitzhardinge | 44235dc | 2010-10-14 17:04:59 -0700 | [diff] [blame] | 192 | list_for_each_entry(page, &pgd_list, lru) { |
| 193 | pgd_t *pgd; |
Jeremy Fitzhardinge | 617d34d | 2010-09-21 12:01:51 -0700 | [diff] [blame] | 194 | spinlock_t *pgt_lock; |
| 195 | |
Jeremy Fitzhardinge | 44235dc | 2010-10-14 17:04:59 -0700 | [diff] [blame] | 196 | pgd = (pgd_t *)page_address(page) + pgd_index(address); |
Andrea Arcangeli | a79e53d | 2011-02-16 15:45:22 -0800 | [diff] [blame] | 197 | /* the pgt_lock only for Xen */ |
Jeremy Fitzhardinge | 617d34d | 2010-09-21 12:01:51 -0700 | [diff] [blame] | 198 | pgt_lock = &pgd_page_get_mm(page)->page_table_lock; |
| 199 | spin_lock(pgt_lock); |
| 200 | |
Jeremy Fitzhardinge | 44235dc | 2010-10-14 17:04:59 -0700 | [diff] [blame] | 201 | if (pgd_none(*pgd)) |
| 202 | set_pgd(pgd, *pgd_ref); |
| 203 | else |
| 204 | BUG_ON(pgd_page_vaddr(*pgd) |
| 205 | != pgd_page_vaddr(*pgd_ref)); |
Jeremy Fitzhardinge | 617d34d | 2010-09-21 12:01:51 -0700 | [diff] [blame] | 206 | |
| 207 | spin_unlock(pgt_lock); |
Jeremy Fitzhardinge | 44235dc | 2010-10-14 17:04:59 -0700 | [diff] [blame] | 208 | } |
Andrea Arcangeli | a79e53d | 2011-02-16 15:45:22 -0800 | [diff] [blame] | 209 | spin_unlock(&pgd_lock); |
Jeremy Fitzhardinge | 44235dc | 2010-10-14 17:04:59 -0700 | [diff] [blame] | 210 | } |
Haicheng Li | 6afb515 | 2010-05-19 17:42:14 +0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /* |
Marcin Slusarz | 8d6ea967 | 2008-08-15 18:32:24 +0200 | [diff] [blame] | 214 | * NOTE: This function is marked __ref because it calls __init function |
| 215 | * (alloc_bootmem_pages). It's safe to do it ONLY when after_bootmem == 0. |
| 216 | */ |
| 217 | static __ref void *spp_getpage(void) |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 218 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | void *ptr; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | if (after_bootmem) |
Vegard Nossum | 9e73023 | 2009-02-22 11:28:25 +0100 | [diff] [blame] | 222 | ptr = (void *) get_zeroed_page(GFP_ATOMIC | __GFP_NOTRACK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | else |
| 224 | ptr = alloc_bootmem_pages(PAGE_SIZE); |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 225 | |
| 226 | if (!ptr || ((unsigned long)ptr & ~PAGE_MASK)) { |
| 227 | panic("set_pte_phys: cannot allocate page data %s\n", |
| 228 | after_bootmem ? "after bootmem" : ""); |
| 229 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
Ingo Molnar | 10f22dd | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 231 | pr_debug("spp_getpage %p\n", ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 233 | return ptr; |
| 234 | } |
| 235 | |
Jeremy Fitzhardinge | f254f39 | 2009-03-03 12:02:57 -0800 | [diff] [blame] | 236 | static pud_t *fill_pud(pgd_t *pgd, unsigned long vaddr) |
Tejun Heo | 458a3e6 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 237 | { |
| 238 | if (pgd_none(*pgd)) { |
| 239 | pud_t *pud = (pud_t *)spp_getpage(); |
| 240 | pgd_populate(&init_mm, pgd, pud); |
| 241 | if (pud != pud_offset(pgd, 0)) |
| 242 | printk(KERN_ERR "PAGETABLE BUG #00! %p <-> %p\n", |
| 243 | pud, pud_offset(pgd, 0)); |
| 244 | } |
| 245 | return pud_offset(pgd, vaddr); |
| 246 | } |
| 247 | |
Jeremy Fitzhardinge | f254f39 | 2009-03-03 12:02:57 -0800 | [diff] [blame] | 248 | static pmd_t *fill_pmd(pud_t *pud, unsigned long vaddr) |
Tejun Heo | 458a3e6 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 249 | { |
| 250 | if (pud_none(*pud)) { |
| 251 | pmd_t *pmd = (pmd_t *) spp_getpage(); |
| 252 | pud_populate(&init_mm, pud, pmd); |
| 253 | if (pmd != pmd_offset(pud, 0)) |
| 254 | printk(KERN_ERR "PAGETABLE BUG #01! %p <-> %p\n", |
| 255 | pmd, pmd_offset(pud, 0)); |
| 256 | } |
| 257 | return pmd_offset(pud, vaddr); |
| 258 | } |
| 259 | |
Jeremy Fitzhardinge | f254f39 | 2009-03-03 12:02:57 -0800 | [diff] [blame] | 260 | static pte_t *fill_pte(pmd_t *pmd, unsigned long vaddr) |
Tejun Heo | 458a3e6 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 261 | { |
| 262 | if (pmd_none(*pmd)) { |
| 263 | pte_t *pte = (pte_t *) spp_getpage(); |
| 264 | pmd_populate_kernel(&init_mm, pmd, pte); |
| 265 | if (pte != pte_offset_kernel(pmd, 0)) |
| 266 | printk(KERN_ERR "PAGETABLE BUG #02!\n"); |
| 267 | } |
| 268 | return pte_offset_kernel(pmd, vaddr); |
| 269 | } |
| 270 | |
| 271 | 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] | 272 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | pud_t *pud; |
| 274 | pmd_t *pmd; |
Jeremy Fitzhardinge | d494a96 | 2008-06-17 11:41:59 -0700 | [diff] [blame] | 275 | pte_t *pte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
Eduardo Habkost | 0814e0b | 2008-06-25 00:19:22 -0400 | [diff] [blame] | 277 | pud = pud_page + pud_index(vaddr); |
Tejun Heo | 458a3e6 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 278 | pmd = fill_pmd(pud, vaddr); |
| 279 | pte = fill_pte(pmd, vaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | set_pte(pte, new_pte); |
| 282 | |
| 283 | /* |
| 284 | * It's enough to flush this one mapping. |
| 285 | * (PGE mappings get flushed as well) |
| 286 | */ |
| 287 | __flush_tlb_one(vaddr); |
| 288 | } |
| 289 | |
Tejun Heo | 458a3e6 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 290 | void set_pte_vaddr(unsigned long vaddr, pte_t pteval) |
Eduardo Habkost | 0814e0b | 2008-06-25 00:19:22 -0400 | [diff] [blame] | 291 | { |
| 292 | pgd_t *pgd; |
| 293 | pud_t *pud_page; |
| 294 | |
| 295 | pr_debug("set_pte_vaddr %lx to %lx\n", vaddr, native_pte_val(pteval)); |
| 296 | |
| 297 | pgd = pgd_offset_k(vaddr); |
| 298 | if (pgd_none(*pgd)) { |
| 299 | printk(KERN_ERR |
| 300 | "PGD FIXMAP MISSING, it should be setup in head.S!\n"); |
| 301 | return; |
| 302 | } |
| 303 | pud_page = (pud_t*)pgd_page_vaddr(*pgd); |
| 304 | set_pte_vaddr_pud(pud_page, vaddr, pteval); |
| 305 | } |
| 306 | |
Tejun Heo | 458a3e6 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 307 | pmd_t * __init populate_extra_pmd(unsigned long vaddr) |
Tejun Heo | 1112441 | 2009-02-20 16:29:09 +0900 | [diff] [blame] | 308 | { |
| 309 | pgd_t *pgd; |
| 310 | pud_t *pud; |
| 311 | |
| 312 | pgd = pgd_offset_k(vaddr); |
Tejun Heo | 458a3e6 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 313 | pud = fill_pud(pgd, vaddr); |
| 314 | return fill_pmd(pud, vaddr); |
| 315 | } |
Tejun Heo | 1112441 | 2009-02-20 16:29:09 +0900 | [diff] [blame] | 316 | |
Tejun Heo | 458a3e6 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 317 | pte_t * __init populate_extra_pte(unsigned long vaddr) |
| 318 | { |
| 319 | pmd_t *pmd; |
| 320 | |
| 321 | pmd = populate_extra_pmd(vaddr); |
| 322 | return fill_pte(pmd, vaddr); |
Tejun Heo | 1112441 | 2009-02-20 16:29:09 +0900 | [diff] [blame] | 323 | } |
| 324 | |
Thomas Gleixner | 31eedd8 | 2008-02-15 17:29:12 +0100 | [diff] [blame] | 325 | /* |
Jack Steiner | 3a9e189 | 2008-07-01 14:45:32 -0500 | [diff] [blame] | 326 | * Create large page table mappings for a range of physical addresses. |
| 327 | */ |
| 328 | static void __init __init_extra_mapping(unsigned long phys, unsigned long size, |
| 329 | pgprot_t prot) |
| 330 | { |
| 331 | pgd_t *pgd; |
| 332 | pud_t *pud; |
| 333 | pmd_t *pmd; |
| 334 | |
| 335 | BUG_ON((phys & ~PMD_MASK) || (size & ~PMD_MASK)); |
| 336 | for (; size; phys += PMD_SIZE, size -= PMD_SIZE) { |
| 337 | pgd = pgd_offset_k((unsigned long)__va(phys)); |
| 338 | if (pgd_none(*pgd)) { |
| 339 | pud = (pud_t *) spp_getpage(); |
| 340 | set_pgd(pgd, __pgd(__pa(pud) | _KERNPG_TABLE | |
| 341 | _PAGE_USER)); |
| 342 | } |
| 343 | pud = pud_offset(pgd, (unsigned long)__va(phys)); |
| 344 | if (pud_none(*pud)) { |
| 345 | pmd = (pmd_t *) spp_getpage(); |
| 346 | set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE | |
| 347 | _PAGE_USER)); |
| 348 | } |
| 349 | pmd = pmd_offset(pud, phys); |
| 350 | BUG_ON(!pmd_none(*pmd)); |
| 351 | set_pmd(pmd, __pmd(phys | pgprot_val(prot))); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | void __init init_extra_mapping_wb(unsigned long phys, unsigned long size) |
| 356 | { |
| 357 | __init_extra_mapping(phys, size, PAGE_KERNEL_LARGE); |
| 358 | } |
| 359 | |
| 360 | void __init init_extra_mapping_uc(unsigned long phys, unsigned long size) |
| 361 | { |
| 362 | __init_extra_mapping(phys, size, PAGE_KERNEL_LARGE_NOCACHE); |
| 363 | } |
| 364 | |
| 365 | /* |
Ingo Molnar | 88f3aec | 2008-02-21 11:04:11 +0100 | [diff] [blame] | 366 | * The head.S code sets up the kernel high mapping: |
| 367 | * |
| 368 | * from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text) |
Thomas Gleixner | 31eedd8 | 2008-02-15 17:29:12 +0100 | [diff] [blame] | 369 | * |
| 370 | * phys_addr holds the negative offset to the kernel, which is added |
| 371 | * to the compile time generated pmds. This results in invalid pmds up |
| 372 | * to the point where we hit the physaddr 0 mapping. |
| 373 | * |
Yinghai Lu | e5f15b4 | 2011-02-18 11:30:30 +0000 | [diff] [blame] | 374 | * We limit the mappings to the region from _text to _brk_end. _brk_end |
| 375 | * 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] | 376 | * well, as they are located before _text: |
| 377 | */ |
| 378 | void __init cleanup_highmap(void) |
| 379 | { |
| 380 | unsigned long vaddr = __START_KERNEL_map; |
Yinghai Lu | 1005423 | 2013-01-24 12:19:54 -0800 | [diff] [blame] | 381 | unsigned long vaddr_end = __START_KERNEL_map + KERNEL_IMAGE_SIZE; |
Yinghai Lu | e5f15b4 | 2011-02-18 11:30:30 +0000 | [diff] [blame] | 382 | unsigned long end = roundup((unsigned long)_brk_end, PMD_SIZE) - 1; |
Thomas Gleixner | 31eedd8 | 2008-02-15 17:29:12 +0100 | [diff] [blame] | 383 | pmd_t *pmd = level2_kernel_pgt; |
Thomas Gleixner | 31eedd8 | 2008-02-15 17:29:12 +0100 | [diff] [blame] | 384 | |
Yinghai Lu | 1005423 | 2013-01-24 12:19:54 -0800 | [diff] [blame] | 385 | /* |
| 386 | * Native path, max_pfn_mapped is not set yet. |
| 387 | * Xen has valid max_pfn_mapped set in |
| 388 | * arch/x86/xen/mmu.c:xen_setup_kernel_pagetable(). |
| 389 | */ |
| 390 | if (max_pfn_mapped) |
| 391 | vaddr_end = __START_KERNEL_map + (max_pfn_mapped << PAGE_SHIFT); |
| 392 | |
Yinghai Lu | e5f15b4 | 2011-02-18 11:30:30 +0000 | [diff] [blame] | 393 | for (; vaddr + PMD_SIZE - 1 < vaddr_end; pmd++, vaddr += PMD_SIZE) { |
Hugh Dickins | 2884f11 | 2008-05-28 19:36:07 +0100 | [diff] [blame] | 394 | if (pmd_none(*pmd)) |
Thomas Gleixner | 31eedd8 | 2008-02-15 17:29:12 +0100 | [diff] [blame] | 395 | continue; |
| 396 | if (vaddr < (unsigned long) _text || vaddr > end) |
| 397 | set_pmd(pmd, __pmd(0)); |
| 398 | } |
| 399 | } |
| 400 | |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 401 | static unsigned long __meminit |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 402 | phys_pte_init(pte_t *pte_page, unsigned long addr, unsigned long end, |
| 403 | pgprot_t prot) |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 404 | { |
Yinghai Lu | eceb363 | 2012-11-16 19:38:56 -0800 | [diff] [blame] | 405 | unsigned long pages = 0, next; |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 406 | unsigned long last_map_addr = end; |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 407 | int i; |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 408 | |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 409 | pte_t *pte = pte_page + pte_index(addr); |
| 410 | |
Yinghai Lu | eceb363 | 2012-11-16 19:38:56 -0800 | [diff] [blame] | 411 | for (i = pte_index(addr); i < PTRS_PER_PTE; i++, addr = next, pte++) { |
| 412 | next = (addr & PAGE_MASK) + PAGE_SIZE; |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 413 | if (addr >= end) { |
Yinghai Lu | eceb363 | 2012-11-16 19:38:56 -0800 | [diff] [blame] | 414 | if (!after_bootmem && |
| 415 | !e820_any_mapped(addr & PAGE_MASK, next, E820_RAM) && |
| 416 | !e820_any_mapped(addr & PAGE_MASK, next, E820_RESERVED_KERN)) |
| 417 | set_pte(pte, __pte(0)); |
| 418 | continue; |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 419 | } |
| 420 | |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 421 | /* |
| 422 | * We will re-use the existing mapping. |
| 423 | * Xen for example has some special requirements, like mapping |
| 424 | * pagetable pages as RO. So assume someone who pre-setup |
| 425 | * these mappings are more intelligent. |
| 426 | */ |
Yinghai Lu | 3afa394 | 2008-10-25 22:58:21 -0700 | [diff] [blame] | 427 | if (pte_val(*pte)) { |
Jan Beulich | 876ee61 | 2012-10-04 14:48:10 +0100 | [diff] [blame] | 428 | if (!after_bootmem) |
| 429 | pages++; |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 430 | continue; |
Yinghai Lu | 3afa394 | 2008-10-25 22:58:21 -0700 | [diff] [blame] | 431 | } |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 432 | |
| 433 | if (0) |
| 434 | printk(" pte=%p addr=%lx pte=%016lx\n", |
| 435 | pte, addr, pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL).pte); |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 436 | pages++; |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 437 | set_pte(pte, pfn_pte(addr >> PAGE_SHIFT, prot)); |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 438 | last_map_addr = (addr & PAGE_MASK) + PAGE_SIZE; |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 439 | } |
Suresh Siddha | a2699e4 | 2008-09-23 14:00:38 -0700 | [diff] [blame] | 440 | |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 441 | update_page_count(PG_LEVEL_4K, pages); |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 442 | |
| 443 | return last_map_addr; |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 444 | } |
| 445 | |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 446 | static unsigned long __meminit |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 447 | 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] | 448 | unsigned long page_size_mask, pgprot_t prot) |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 449 | { |
Jan Beulich | 20167d3 | 2012-05-16 14:06:26 +0100 | [diff] [blame] | 450 | unsigned long pages = 0, next; |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 451 | unsigned long last_map_addr = end; |
Andi Kleen | ce0c0e5 | 2008-05-02 11:46:49 +0200 | [diff] [blame] | 452 | |
Keith Mannthey | 6ad9165 | 2006-09-26 10:52:36 +0200 | [diff] [blame] | 453 | int i = pmd_index(address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | |
Jan Beulich | 20167d3 | 2012-05-16 14:06:26 +0100 | [diff] [blame] | 455 | for (; i < PTRS_PER_PMD; i++, address = next) { |
Keith Mannthey | 6ad9165 | 2006-09-26 10:52:36 +0200 | [diff] [blame] | 456 | pmd_t *pmd = pmd_page + pmd_index(address); |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 457 | pte_t *pte; |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 458 | pgprot_t new_prot = prot; |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 459 | |
Jan Beulich | 20167d3 | 2012-05-16 14:06:26 +0100 | [diff] [blame] | 460 | next = (address & PMD_MASK) + PMD_SIZE; |
Yinghai Lu | eceb363 | 2012-11-16 19:38:56 -0800 | [diff] [blame] | 461 | if (address >= end) { |
| 462 | if (!after_bootmem && |
| 463 | !e820_any_mapped(address & PMD_MASK, next, E820_RAM) && |
| 464 | !e820_any_mapped(address & PMD_MASK, next, E820_RESERVED_KERN)) |
| 465 | set_pmd(pmd, __pmd(0)); |
| 466 | continue; |
| 467 | } |
Jan Beulich | 20167d3 | 2012-05-16 14:06:26 +0100 | [diff] [blame] | 468 | |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 469 | if (pmd_val(*pmd)) { |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 470 | if (!pmd_large(*pmd)) { |
| 471 | spin_lock(&init_mm.page_table_lock); |
Yinghai Lu | 973dc4f | 2012-11-16 19:38:59 -0800 | [diff] [blame] | 472 | pte = (pte_t *)pmd_page_vaddr(*pmd); |
Yinghai Lu | 4b239f4 | 2010-12-17 16:58:28 -0800 | [diff] [blame] | 473 | last_map_addr = phys_pte_init(pte, address, |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 474 | end, prot); |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 475 | spin_unlock(&init_mm.page_table_lock); |
Suresh Siddha | a2699e4 | 2008-09-23 14:00:38 -0700 | [diff] [blame] | 476 | continue; |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 477 | } |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 478 | /* |
| 479 | * If we are ok with PG_LEVEL_2M mapping, then we will |
| 480 | * use the existing mapping, |
| 481 | * |
| 482 | * Otherwise, we will split the large page mapping but |
| 483 | * use the same existing protection bits except for |
| 484 | * large page, so that we don't violate Intel's TLB |
| 485 | * Application note (317080) which says, while changing |
| 486 | * the page sizes, new and old translations should |
| 487 | * not differ with respect to page frame and |
| 488 | * attributes. |
| 489 | */ |
Yinghai Lu | 3afa394 | 2008-10-25 22:58:21 -0700 | [diff] [blame] | 490 | if (page_size_mask & (1 << PG_LEVEL_2M)) { |
Jan Beulich | 876ee61 | 2012-10-04 14:48:10 +0100 | [diff] [blame] | 491 | if (!after_bootmem) |
| 492 | pages++; |
Jan Beulich | 20167d3 | 2012-05-16 14:06:26 +0100 | [diff] [blame] | 493 | last_map_addr = next; |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 494 | continue; |
Yinghai Lu | 3afa394 | 2008-10-25 22:58:21 -0700 | [diff] [blame] | 495 | } |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 496 | new_prot = pte_pgprot(pte_clrhuge(*(pte_t *)pmd)); |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 497 | } |
Keith Mannthey | 6ad9165 | 2006-09-26 10:52:36 +0200 | [diff] [blame] | 498 | |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 499 | if (page_size_mask & (1<<PG_LEVEL_2M)) { |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 500 | pages++; |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 501 | spin_lock(&init_mm.page_table_lock); |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 502 | set_pte((pte_t *)pmd, |
Yinghai Lu | 960ddb4f | 2012-11-16 19:38:54 -0800 | [diff] [blame] | 503 | pfn_pte((address & PMD_MASK) >> PAGE_SHIFT, |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 504 | __pgprot(pgprot_val(prot) | _PAGE_PSE))); |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 505 | spin_unlock(&init_mm.page_table_lock); |
Jan Beulich | 20167d3 | 2012-05-16 14:06:26 +0100 | [diff] [blame] | 506 | last_map_addr = next; |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 507 | continue; |
| 508 | } |
| 509 | |
Yinghai Lu | 868bf4d | 2012-11-16 19:39:00 -0800 | [diff] [blame] | 510 | pte = alloc_low_page(); |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 511 | last_map_addr = phys_pte_init(pte, address, end, new_prot); |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 512 | |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 513 | spin_lock(&init_mm.page_table_lock); |
Yinghai Lu | 868bf4d | 2012-11-16 19:39:00 -0800 | [diff] [blame] | 514 | pmd_populate_kernel(&init_mm, pmd, pte); |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 515 | spin_unlock(&init_mm.page_table_lock); |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 516 | } |
Andi Kleen | ce0c0e5 | 2008-05-02 11:46:49 +0200 | [diff] [blame] | 517 | update_page_count(PG_LEVEL_2M, pages); |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 518 | return last_map_addr; |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 519 | } |
| 520 | |
Andi Kleen | cc61503 | 2008-03-12 03:53:28 +0100 | [diff] [blame] | 521 | static unsigned long __meminit |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 522 | phys_pud_init(pud_t *pud_page, unsigned long addr, unsigned long end, |
| 523 | unsigned long page_size_mask) |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 524 | { |
Jan Beulich | 20167d3 | 2012-05-16 14:06:26 +0100 | [diff] [blame] | 525 | unsigned long pages = 0, next; |
Andi Kleen | cc61503 | 2008-03-12 03:53:28 +0100 | [diff] [blame] | 526 | unsigned long last_map_addr = end; |
Keith Mannthey | 6ad9165 | 2006-09-26 10:52:36 +0200 | [diff] [blame] | 527 | int i = pud_index(addr); |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 528 | |
Jan Beulich | 20167d3 | 2012-05-16 14:06:26 +0100 | [diff] [blame] | 529 | for (; i < PTRS_PER_PUD; i++, addr = next) { |
Keith Mannthey | 6ad9165 | 2006-09-26 10:52:36 +0200 | [diff] [blame] | 530 | pud_t *pud = pud_page + pud_index(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | pmd_t *pmd; |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 532 | pgprot_t prot = PAGE_KERNEL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | |
Jan Beulich | 20167d3 | 2012-05-16 14:06:26 +0100 | [diff] [blame] | 534 | next = (addr & PUD_MASK) + PUD_SIZE; |
Yinghai Lu | eceb363 | 2012-11-16 19:38:56 -0800 | [diff] [blame] | 535 | if (addr >= end) { |
| 536 | if (!after_bootmem && |
| 537 | !e820_any_mapped(addr & PUD_MASK, next, E820_RAM) && |
| 538 | !e820_any_mapped(addr & PUD_MASK, next, E820_RESERVED_KERN)) |
| 539 | set_pud(pud, __pud(0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | continue; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 541 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | |
Keith Mannthey | 6ad9165 | 2006-09-26 10:52:36 +0200 | [diff] [blame] | 543 | if (pud_val(*pud)) { |
Suresh Siddha | a2699e4 | 2008-09-23 14:00:38 -0700 | [diff] [blame] | 544 | if (!pud_large(*pud)) { |
Yinghai Lu | 973dc4f | 2012-11-16 19:38:59 -0800 | [diff] [blame] | 545 | pmd = pmd_offset(pud, 0); |
Yinghai Lu | 4b239f4 | 2010-12-17 16:58:28 -0800 | [diff] [blame] | 546 | last_map_addr = phys_pmd_init(pmd, addr, end, |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 547 | page_size_mask, prot); |
Yinghai Lu | 4b239f4 | 2010-12-17 16:58:28 -0800 | [diff] [blame] | 548 | __flush_tlb_all(); |
Suresh Siddha | a2699e4 | 2008-09-23 14:00:38 -0700 | [diff] [blame] | 549 | continue; |
| 550 | } |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 551 | /* |
| 552 | * If we are ok with PG_LEVEL_1G mapping, then we will |
| 553 | * use the existing mapping. |
| 554 | * |
| 555 | * Otherwise, we will split the gbpage mapping but use |
| 556 | * the same existing protection bits except for large |
| 557 | * page, so that we don't violate Intel's TLB |
| 558 | * Application note (317080) which says, while changing |
| 559 | * the page sizes, new and old translations should |
| 560 | * not differ with respect to page frame and |
| 561 | * attributes. |
| 562 | */ |
Yinghai Lu | 3afa394 | 2008-10-25 22:58:21 -0700 | [diff] [blame] | 563 | if (page_size_mask & (1 << PG_LEVEL_1G)) { |
Jan Beulich | 876ee61 | 2012-10-04 14:48:10 +0100 | [diff] [blame] | 564 | if (!after_bootmem) |
| 565 | pages++; |
Jan Beulich | 20167d3 | 2012-05-16 14:06:26 +0100 | [diff] [blame] | 566 | last_map_addr = next; |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 567 | continue; |
Yinghai Lu | 3afa394 | 2008-10-25 22:58:21 -0700 | [diff] [blame] | 568 | } |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 569 | prot = pte_pgprot(pte_clrhuge(*(pte_t *)pud)); |
Andi Kleen | ef92576 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 570 | } |
| 571 | |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 572 | if (page_size_mask & (1<<PG_LEVEL_1G)) { |
Andi Kleen | ce0c0e5 | 2008-05-02 11:46:49 +0200 | [diff] [blame] | 573 | pages++; |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 574 | spin_lock(&init_mm.page_table_lock); |
Andi Kleen | ef92576 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 575 | set_pte((pte_t *)pud, |
Yinghai Lu | 960ddb4f | 2012-11-16 19:38:54 -0800 | [diff] [blame] | 576 | pfn_pte((addr & PUD_MASK) >> PAGE_SHIFT, |
| 577 | PAGE_KERNEL_LARGE)); |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 578 | spin_unlock(&init_mm.page_table_lock); |
Jan Beulich | 20167d3 | 2012-05-16 14:06:26 +0100 | [diff] [blame] | 579 | last_map_addr = next; |
Keith Mannthey | 6ad9165 | 2006-09-26 10:52:36 +0200 | [diff] [blame] | 580 | continue; |
| 581 | } |
| 582 | |
Yinghai Lu | 868bf4d | 2012-11-16 19:39:00 -0800 | [diff] [blame] | 583 | pmd = alloc_low_page(); |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 584 | last_map_addr = phys_pmd_init(pmd, addr, end, page_size_mask, |
| 585 | prot); |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 586 | |
| 587 | spin_lock(&init_mm.page_table_lock); |
Yinghai Lu | 868bf4d | 2012-11-16 19:39:00 -0800 | [diff] [blame] | 588 | pud_populate(&init_mm, pud, pmd); |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 589 | spin_unlock(&init_mm.page_table_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | } |
Andi Kleen | 1a2b441 | 2008-01-30 13:33:54 +0100 | [diff] [blame] | 591 | __flush_tlb_all(); |
Suresh Siddha | a2699e4 | 2008-09-23 14:00:38 -0700 | [diff] [blame] | 592 | |
Andi Kleen | ce0c0e5 | 2008-05-02 11:46:49 +0200 | [diff] [blame] | 593 | update_page_count(PG_LEVEL_1G, pages); |
Andi Kleen | cc61503 | 2008-03-12 03:53:28 +0100 | [diff] [blame] | 594 | |
Yinghai Lu | 1a0db38 | 2008-06-24 14:56:20 -0700 | [diff] [blame] | 595 | return last_map_addr; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 596 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | |
Shaohua Li | 41d840e | 2009-06-12 12:57:52 +0800 | [diff] [blame] | 598 | unsigned long __meminit |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 599 | kernel_physical_mapping_init(unsigned long start, |
| 600 | unsigned long end, |
| 601 | unsigned long page_size_mask) |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 602 | { |
Haicheng Li | 9b86152 | 2010-08-20 17:50:16 +0800 | [diff] [blame] | 603 | bool pgd_changed = false; |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 604 | unsigned long next, last_map_addr = end; |
Haicheng Li | 9b86152 | 2010-08-20 17:50:16 +0800 | [diff] [blame] | 605 | unsigned long addr; |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 606 | |
| 607 | start = (unsigned long)__va(start); |
| 608 | end = (unsigned long)__va(end); |
Wu Fengguang | 1c5f50e | 2010-09-03 17:04:07 +0800 | [diff] [blame] | 609 | addr = start; |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 610 | |
| 611 | for (; start < end; start = next) { |
| 612 | pgd_t *pgd = pgd_offset_k(start); |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 613 | pud_t *pud; |
| 614 | |
Yinghai Lu | c2bdee5 | 2013-01-24 12:19:46 -0800 | [diff] [blame] | 615 | next = (start & PGDIR_MASK) + PGDIR_SIZE; |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 616 | |
| 617 | if (pgd_val(*pgd)) { |
Yinghai Lu | 973dc4f | 2012-11-16 19:38:59 -0800 | [diff] [blame] | 618 | pud = (pud_t *)pgd_page_vaddr(*pgd); |
Yinghai Lu | 4b239f4 | 2010-12-17 16:58:28 -0800 | [diff] [blame] | 619 | last_map_addr = phys_pud_init(pud, __pa(start), |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 620 | __pa(end), page_size_mask); |
| 621 | continue; |
| 622 | } |
| 623 | |
Yinghai Lu | 868bf4d | 2012-11-16 19:39:00 -0800 | [diff] [blame] | 624 | pud = alloc_low_page(); |
Yinghai Lu | c2bdee5 | 2013-01-24 12:19:46 -0800 | [diff] [blame] | 625 | last_map_addr = phys_pud_init(pud, __pa(start), __pa(end), |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 626 | page_size_mask); |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 627 | |
| 628 | spin_lock(&init_mm.page_table_lock); |
Yinghai Lu | 868bf4d | 2012-11-16 19:39:00 -0800 | [diff] [blame] | 629 | pgd_populate(&init_mm, pgd, pud); |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 630 | spin_unlock(&init_mm.page_table_lock); |
Haicheng Li | 9b86152 | 2010-08-20 17:50:16 +0800 | [diff] [blame] | 631 | pgd_changed = true; |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 632 | } |
Haicheng Li | 9b86152 | 2010-08-20 17:50:16 +0800 | [diff] [blame] | 633 | |
| 634 | if (pgd_changed) |
| 635 | sync_global_pgds(addr, end); |
| 636 | |
Suresh Siddha | a2699e4 | 2008-09-23 14:00:38 -0700 | [diff] [blame] | 637 | __flush_tlb_all(); |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 638 | |
| 639 | return last_map_addr; |
| 640 | } |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 641 | |
Matt Tolentino | 2b97690 | 2005-06-23 00:08:06 -0700 | [diff] [blame] | 642 | #ifndef CONFIG_NUMA |
Tejun Heo | d8fc3af | 2011-02-16 12:13:06 +0100 | [diff] [blame] | 643 | void __init initmem_init(void) |
Yinghai Lu | 1f75d7e | 2008-06-22 02:44:49 -0700 | [diff] [blame] | 644 | { |
Tejun Heo | 0608f70 | 2011-07-14 11:44:23 +0200 | [diff] [blame] | 645 | memblock_set_node(0, (phys_addr_t)ULLONG_MAX, 0); |
Yinghai Lu | 1f75d7e | 2008-06-22 02:44:49 -0700 | [diff] [blame] | 646 | } |
Pekka Enberg | 3551f88 | 2009-05-07 15:35:41 +0300 | [diff] [blame] | 647 | #endif |
Yinghai Lu | 1f75d7e | 2008-06-22 02:44:49 -0700 | [diff] [blame] | 648 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | void __init paging_init(void) |
| 650 | { |
Pekka Enberg | 3551f88 | 2009-05-07 15:35:41 +0300 | [diff] [blame] | 651 | sparse_memory_present_with_active_regions(MAX_NUMNODES); |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 652 | sparse_init(); |
Yinghai Lu | 44b5728 | 2009-07-08 09:50:19 -0700 | [diff] [blame] | 653 | |
| 654 | /* |
| 655 | * clear the default setting with node 0 |
| 656 | * note: don't use nodes_clear here, that is really clearing when |
| 657 | * numa support is not compiled in, and later node_set_state |
| 658 | * will not set it back. |
| 659 | */ |
Lai Jiangshan | 4b0ef1fe | 2012-12-12 13:51:46 -0800 | [diff] [blame] | 660 | node_clear_state(0, N_MEMORY); |
| 661 | if (N_MEMORY != N_NORMAL_MEMORY) |
| 662 | node_clear_state(0, N_NORMAL_MEMORY); |
Yinghai Lu | 44b5728 | 2009-07-08 09:50:19 -0700 | [diff] [blame] | 663 | |
Pekka Enberg | 4c0b2e5 | 2011-11-01 15:58:17 +0200 | [diff] [blame] | 664 | zone_sizes_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 667 | /* |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 668 | * Memory hotplug specific functions |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 669 | */ |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 670 | #ifdef CONFIG_MEMORY_HOTPLUG |
| 671 | /* |
Shaohui Zheng | ea08541 | 2010-02-02 13:44:16 -0800 | [diff] [blame] | 672 | * After memory hotplug the variables max_pfn, max_low_pfn and high_memory need |
| 673 | * updating. |
| 674 | */ |
| 675 | static void update_end_of_memory_vars(u64 start, u64 size) |
| 676 | { |
| 677 | unsigned long end_pfn = PFN_UP(start + size); |
| 678 | |
| 679 | if (end_pfn > max_pfn) { |
| 680 | max_pfn = end_pfn; |
| 681 | max_low_pfn = end_pfn; |
| 682 | high_memory = (void *)__va(max_pfn * PAGE_SIZE - 1) + 1; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | /* |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 687 | * Memory is added always to NORMAL zone. This means you will never get |
| 688 | * additional DMA/DMA32 memory. |
| 689 | */ |
| 690 | int arch_add_memory(int nid, u64 start, u64 size) |
| 691 | { |
| 692 | struct pglist_data *pgdat = NODE_DATA(nid); |
Christoph Lameter | 776ed98 | 2006-09-25 23:31:09 -0700 | [diff] [blame] | 693 | struct zone *zone = pgdat->node_zones + ZONE_NORMAL; |
Jacob Shin | 66520eb | 2012-11-16 19:38:52 -0800 | [diff] [blame] | 694 | unsigned long start_pfn = start >> PAGE_SHIFT; |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 695 | unsigned long nr_pages = size >> PAGE_SHIFT; |
| 696 | int ret; |
| 697 | |
Jacob Shin | 66520eb | 2012-11-16 19:38:52 -0800 | [diff] [blame] | 698 | init_memory_mapping(start, start + size); |
Keith Mannthey | 45e0b78 | 2006-09-30 23:27:09 -0700 | [diff] [blame] | 699 | |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 700 | ret = __add_pages(nid, zone, start_pfn, nr_pages); |
Gary Hade | fe8b868 | 2008-10-28 16:43:14 -0700 | [diff] [blame] | 701 | WARN_ON_ONCE(ret); |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 702 | |
Shaohui Zheng | ea08541 | 2010-02-02 13:44:16 -0800 | [diff] [blame] | 703 | /* update max_pfn, max_low_pfn and high_memory */ |
| 704 | update_end_of_memory_vars(start, size); |
| 705 | |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 706 | return ret; |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 707 | } |
| 708 | EXPORT_SYMBOL_GPL(arch_add_memory); |
| 709 | |
Keith Mannthey | 45e0b78 | 2006-09-30 23:27:09 -0700 | [diff] [blame] | 710 | #endif /* CONFIG_MEMORY_HOTPLUG */ |
| 711 | |
KAMEZAWA Hiroyuki | 81ac3ad | 2009-09-22 16:45:49 -0700 | [diff] [blame] | 712 | static struct kcore_list kcore_vsyscall; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | |
Yinghai Lu | 94b43c3 | 2012-11-16 19:39:19 -0800 | [diff] [blame] | 714 | static void __init register_page_bootmem_info(void) |
| 715 | { |
| 716 | #ifdef CONFIG_NUMA |
| 717 | int i; |
| 718 | |
| 719 | for_each_online_node(i) |
| 720 | register_page_bootmem_info_node(NODE_DATA(i)); |
| 721 | #endif |
| 722 | } |
| 723 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | void __init mem_init(void) |
| 725 | { |
Andi Kleen | 0a43e4b | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 726 | long codesize, reservedpages, datasize, initsize; |
Yinghai Lu | 11a6b0c | 2008-10-14 18:59:18 -0700 | [diff] [blame] | 727 | unsigned long absent_pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | |
Jon Mason | 0dc243a | 2006-06-26 13:58:11 +0200 | [diff] [blame] | 729 | pci_iommu_alloc(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | |
Yinghai Lu | 48ddb15 | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 731 | /* clear_bss() already clear the empty_zero_page */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | |
| 733 | reservedpages = 0; |
| 734 | |
| 735 | /* this will put all low memory onto the freelists */ |
Yinghai Lu | 94b43c3 | 2012-11-16 19:39:19 -0800 | [diff] [blame] | 736 | register_page_bootmem_info(); |
Andi Kleen | 0a43e4b | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 737 | totalram_pages = free_all_bootmem(); |
Yinghai Lu | 11a6b0c | 2008-10-14 18:59:18 -0700 | [diff] [blame] | 738 | |
| 739 | absent_pages = absent_pages_in_range(0, max_pfn); |
| 740 | reservedpages = max_pfn - totalram_pages - absent_pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | after_bootmem = 1; |
| 742 | |
| 743 | codesize = (unsigned long) &_etext - (unsigned long) &_text; |
| 744 | datasize = (unsigned long) &_edata - (unsigned long) &_etext; |
| 745 | initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin; |
| 746 | |
| 747 | /* Register memory areas for /proc/kcore */ |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 748 | kclist_add(&kcore_vsyscall, (void *)VSYSCALL_START, |
KAMEZAWA Hiroyuki | c30bb2a | 2009-09-22 16:45:43 -0700 | [diff] [blame] | 749 | VSYSCALL_END - VSYSCALL_START, KCORE_OTHER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | |
Ingo Molnar | 10f22dd | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 751 | printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, " |
Yinghai Lu | 11a6b0c | 2008-10-14 18:59:18 -0700 | [diff] [blame] | 752 | "%ldk absent, %ldk reserved, %ldk data, %ldk init)\n", |
Geert Uytterhoeven | cc013a8 | 2009-09-21 17:02:36 -0700 | [diff] [blame] | 753 | nr_free_pages() << (PAGE_SHIFT-10), |
Yinghai Lu | c987d12 | 2008-06-24 22:14:09 -0700 | [diff] [blame] | 754 | max_pfn << (PAGE_SHIFT-10), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | codesize >> 10, |
Yinghai Lu | 11a6b0c | 2008-10-14 18:59:18 -0700 | [diff] [blame] | 756 | absent_pages << (PAGE_SHIFT-10), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | reservedpages << (PAGE_SHIFT-10), |
| 758 | datasize >> 10, |
| 759 | initsize >> 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | } |
| 761 | |
Arjan van de Ven | 67df197 | 2006-01-06 00:12:04 -0800 | [diff] [blame] | 762 | #ifdef CONFIG_DEBUG_RODATA |
Arjan van de Ven | edeed30 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 763 | const int rodata_test_data = 0xC3; |
| 764 | EXPORT_SYMBOL_GPL(rodata_test_data); |
Arjan van de Ven | 67df197 | 2006-01-06 00:12:04 -0800 | [diff] [blame] | 765 | |
Suresh Siddha | 502f660 | 2009-10-28 18:46:56 -0800 | [diff] [blame] | 766 | int kernel_set_to_readonly; |
Steven Rostedt | 1623963 | 2009-02-17 17:57:30 -0500 | [diff] [blame] | 767 | |
| 768 | void set_kernel_text_rw(void) |
| 769 | { |
Suresh Siddha | b9af7c0 | 2009-10-14 14:46:55 -0700 | [diff] [blame] | 770 | unsigned long start = PFN_ALIGN(_text); |
Suresh Siddha | e7d23dd | 2009-10-28 18:46:58 -0800 | [diff] [blame] | 771 | unsigned long end = PFN_ALIGN(__stop___ex_table); |
Steven Rostedt | 1623963 | 2009-02-17 17:57:30 -0500 | [diff] [blame] | 772 | |
| 773 | if (!kernel_set_to_readonly) |
| 774 | return; |
| 775 | |
| 776 | pr_debug("Set kernel text: %lx - %lx for read write\n", |
| 777 | start, end); |
| 778 | |
Suresh Siddha | e7d23dd | 2009-10-28 18:46:58 -0800 | [diff] [blame] | 779 | /* |
| 780 | * Make the kernel identity mapping for text RW. Kernel text |
| 781 | * mapping will always be RO. Refer to the comment in |
| 782 | * static_protections() in pageattr.c |
| 783 | */ |
Steven Rostedt | 1623963 | 2009-02-17 17:57:30 -0500 | [diff] [blame] | 784 | set_memory_rw(start, (end - start) >> PAGE_SHIFT); |
| 785 | } |
| 786 | |
| 787 | void set_kernel_text_ro(void) |
| 788 | { |
Suresh Siddha | b9af7c0 | 2009-10-14 14:46:55 -0700 | [diff] [blame] | 789 | unsigned long start = PFN_ALIGN(_text); |
Suresh Siddha | e7d23dd | 2009-10-28 18:46:58 -0800 | [diff] [blame] | 790 | unsigned long end = PFN_ALIGN(__stop___ex_table); |
Steven Rostedt | 1623963 | 2009-02-17 17:57:30 -0500 | [diff] [blame] | 791 | |
| 792 | if (!kernel_set_to_readonly) |
| 793 | return; |
| 794 | |
| 795 | pr_debug("Set kernel text: %lx - %lx for read only\n", |
| 796 | start, end); |
| 797 | |
Suresh Siddha | e7d23dd | 2009-10-28 18:46:58 -0800 | [diff] [blame] | 798 | /* |
| 799 | * Set the kernel identity mapping for text RO. |
| 800 | */ |
Steven Rostedt | 1623963 | 2009-02-17 17:57:30 -0500 | [diff] [blame] | 801 | set_memory_ro(start, (end - start) >> PAGE_SHIFT); |
| 802 | } |
| 803 | |
Arjan van de Ven | 67df197 | 2006-01-06 00:12:04 -0800 | [diff] [blame] | 804 | void mark_rodata_ro(void) |
| 805 | { |
Suresh Siddha | 74e0817 | 2009-10-14 14:46:56 -0700 | [diff] [blame] | 806 | unsigned long start = PFN_ALIGN(_text); |
Steven Rostedt | 8f0f996 | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 807 | unsigned long rodata_start = |
| 808 | ((unsigned long)__start_rodata + PAGE_SIZE - 1) & PAGE_MASK; |
Suresh Siddha | 74e0817 | 2009-10-14 14:46:56 -0700 | [diff] [blame] | 809 | unsigned long end = (unsigned long) &__end_rodata_hpage_align; |
| 810 | unsigned long text_end = PAGE_ALIGN((unsigned long) &__stop___ex_table); |
| 811 | unsigned long rodata_end = PAGE_ALIGN((unsigned long) &__end_rodata); |
| 812 | unsigned long data_start = (unsigned long) &_sdata; |
Yinghai Lu | 7221267 | 2013-01-24 12:20:13 -0800 | [diff] [blame^] | 813 | unsigned long all_end = PFN_ALIGN(&_end); |
Steven Rostedt | 8f0f996 | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 814 | |
Jan Beulich | 6fb1475 | 2007-05-02 19:27:10 +0200 | [diff] [blame] | 815 | printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n", |
Linus Torvalds | e3ebadd | 2007-05-07 08:44:24 -0700 | [diff] [blame] | 816 | (end - start) >> 10); |
Arjan van de Ven | 984bb80 | 2008-02-06 22:39:45 +0100 | [diff] [blame] | 817 | set_memory_ro(start, (end - start) >> PAGE_SHIFT); |
| 818 | |
Steven Rostedt | 1623963 | 2009-02-17 17:57:30 -0500 | [diff] [blame] | 819 | kernel_set_to_readonly = 1; |
| 820 | |
Arjan van de Ven | 984bb80 | 2008-02-06 22:39:45 +0100 | [diff] [blame] | 821 | /* |
Yinghai Lu | 7221267 | 2013-01-24 12:20:13 -0800 | [diff] [blame^] | 822 | * The rodata/data/bss/brk section (but not the kernel text!) |
| 823 | * should also be not-executable. |
Arjan van de Ven | 984bb80 | 2008-02-06 22:39:45 +0100 | [diff] [blame] | 824 | */ |
Yinghai Lu | 7221267 | 2013-01-24 12:20:13 -0800 | [diff] [blame^] | 825 | 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] | 826 | |
Arjan van de Ven | 1a48725 | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 827 | rodata_test(); |
| 828 | |
Andi Kleen | 0c42f39 | 2008-01-30 13:33:42 +0100 | [diff] [blame] | 829 | #ifdef CONFIG_CPA_DEBUG |
Ingo Molnar | 10f22dd | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 830 | 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] | 831 | set_memory_rw(start, (end-start) >> PAGE_SHIFT); |
Andi Kleen | 0c42f39 | 2008-01-30 13:33:42 +0100 | [diff] [blame] | 832 | |
Ingo Molnar | 10f22dd | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 833 | printk(KERN_INFO "Testing CPA: again\n"); |
Arjan van de Ven | 6d238cc | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 834 | set_memory_ro(start, (end-start) >> PAGE_SHIFT); |
Andi Kleen | 0c42f39 | 2008-01-30 13:33:42 +0100 | [diff] [blame] | 835 | #endif |
Suresh Siddha | 74e0817 | 2009-10-14 14:46:56 -0700 | [diff] [blame] | 836 | |
| 837 | free_init_pages("unused kernel memory", |
| 838 | (unsigned long) page_address(virt_to_page(text_end)), |
| 839 | (unsigned long) |
| 840 | page_address(virt_to_page(rodata_start))); |
| 841 | free_init_pages("unused kernel memory", |
| 842 | (unsigned long) page_address(virt_to_page(rodata_end)), |
| 843 | (unsigned long) page_address(virt_to_page(data_start))); |
Arjan van de Ven | 67df197 | 2006-01-06 00:12:04 -0800 | [diff] [blame] | 844 | } |
Mathieu Desnoyers | 4e4eee0 | 2008-02-02 15:42:20 -0500 | [diff] [blame] | 845 | |
Arjan van de Ven | 67df197 | 2006-01-06 00:12:04 -0800 | [diff] [blame] | 846 | #endif |
| 847 | |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 848 | int kern_addr_valid(unsigned long addr) |
| 849 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 851 | pgd_t *pgd; |
| 852 | pud_t *pud; |
| 853 | pmd_t *pmd; |
| 854 | pte_t *pte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | |
| 856 | if (above != 0 && above != -1UL) |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 857 | return 0; |
| 858 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | pgd = pgd_offset_k(addr); |
| 860 | if (pgd_none(*pgd)) |
| 861 | return 0; |
| 862 | |
| 863 | pud = pud_offset(pgd, addr); |
| 864 | if (pud_none(*pud)) |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 865 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | |
| 867 | pmd = pmd_offset(pud, addr); |
| 868 | if (pmd_none(*pmd)) |
| 869 | return 0; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 870 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | if (pmd_large(*pmd)) |
| 872 | return pfn_valid(pmd_pfn(*pmd)); |
| 873 | |
| 874 | pte = pte_offset_kernel(pmd, addr); |
| 875 | if (pte_none(*pte)) |
| 876 | return 0; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 877 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | return pfn_valid(pte_pfn(*pte)); |
| 879 | } |
| 880 | |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 881 | /* |
| 882 | * A pseudo VMA to allow ptrace access for the vsyscall page. This only |
| 883 | * covers the 64bit vsyscall page now. 32bit has a real VMA now and does |
| 884 | * not need special handling anymore: |
| 885 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | static struct vm_area_struct gate_vma = { |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 887 | .vm_start = VSYSCALL_START, |
| 888 | .vm_end = VSYSCALL_START + (VSYSCALL_MAPPED_PAGES * PAGE_SIZE), |
| 889 | .vm_page_prot = PAGE_READONLY_EXEC, |
| 890 | .vm_flags = VM_READ | VM_EXEC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | }; |
| 892 | |
Stephen Wilson | 31db58b | 2011-03-13 15:49:15 -0400 | [diff] [blame] | 893 | struct vm_area_struct *get_gate_vma(struct mm_struct *mm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | { |
| 895 | #ifdef CONFIG_IA32_EMULATION |
Stephen Wilson | 31db58b | 2011-03-13 15:49:15 -0400 | [diff] [blame] | 896 | if (!mm || mm->context.ia32_compat) |
Andi Kleen | 1e01441 | 2005-04-16 15:24:55 -0700 | [diff] [blame] | 897 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | #endif |
| 899 | return &gate_vma; |
| 900 | } |
| 901 | |
Stephen Wilson | 83b964b | 2011-03-13 15:49:16 -0400 | [diff] [blame] | 902 | int in_gate_area(struct mm_struct *mm, unsigned long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | { |
Stephen Wilson | 83b964b | 2011-03-13 15:49:16 -0400 | [diff] [blame] | 904 | struct vm_area_struct *vma = get_gate_vma(mm); |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 905 | |
Andi Kleen | 1e01441 | 2005-04-16 15:24:55 -0700 | [diff] [blame] | 906 | if (!vma) |
| 907 | return 0; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 908 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | return (addr >= vma->vm_start) && (addr < vma->vm_end); |
| 910 | } |
| 911 | |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 912 | /* |
Stephen Wilson | cae5d39 | 2011-03-13 15:49:17 -0400 | [diff] [blame] | 913 | * Use this when you have no reliable mm, typically from interrupt |
| 914 | * context. It is less reliable than using a task's mm and may give |
| 915 | * false positives. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | */ |
Stephen Wilson | cae5d39 | 2011-03-13 15:49:17 -0400 | [diff] [blame] | 917 | int in_gate_area_no_mm(unsigned long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | { |
Andi Kleen | 1e01441 | 2005-04-16 15:24:55 -0700 | [diff] [blame] | 919 | return (addr >= VSYSCALL_START) && (addr < VSYSCALL_END); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | } |
Zou Nan hai | 2e1c49d | 2007-06-01 00:46:28 -0700 | [diff] [blame] | 921 | |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 922 | const char *arch_vma_name(struct vm_area_struct *vma) |
| 923 | { |
| 924 | if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso) |
| 925 | return "[vdso]"; |
| 926 | if (vma == &gate_vma) |
| 927 | return "[vsyscall]"; |
| 928 | return NULL; |
| 929 | } |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 930 | |
Nathan Fontenot | 1dc41aa | 2011-01-20 10:46:15 -0600 | [diff] [blame] | 931 | #ifdef CONFIG_X86_UV |
Nathan Fontenot | 1dc41aa | 2011-01-20 10:46:15 -0600 | [diff] [blame] | 932 | unsigned long memory_block_size_bytes(void) |
| 933 | { |
| 934 | if (is_uv_system()) { |
| 935 | printk(KERN_INFO "UV: memory block size 2GB\n"); |
| 936 | return 2UL * 1024 * 1024 * 1024; |
| 937 | } |
| 938 | return MIN_MEMORY_BLOCK_SIZE; |
| 939 | } |
| 940 | #endif |
| 941 | |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 942 | #ifdef CONFIG_SPARSEMEM_VMEMMAP |
| 943 | /* |
| 944 | * Initialise the sparsemem vmemmap using huge-pages at the PMD level. |
| 945 | */ |
Yinghai Lu | c2b91e2 | 2008-04-12 01:19:24 -0700 | [diff] [blame] | 946 | static long __meminitdata addr_start, addr_end; |
| 947 | static void __meminitdata *p_start, *p_end; |
| 948 | static int __meminitdata node_start; |
| 949 | |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 950 | int __meminit |
| 951 | vmemmap_populate(struct page *start_page, unsigned long size, int node) |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 952 | { |
| 953 | unsigned long addr = (unsigned long)start_page; |
| 954 | unsigned long end = (unsigned long)(start_page + size); |
| 955 | unsigned long next; |
| 956 | pgd_t *pgd; |
| 957 | pud_t *pud; |
| 958 | pmd_t *pmd; |
| 959 | |
| 960 | for (; addr < end; addr = next) { |
Jeremy Fitzhardinge | 7c934d3 | 2008-06-25 00:19:20 -0400 | [diff] [blame] | 961 | void *p = NULL; |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 962 | |
| 963 | pgd = vmemmap_pgd_populate(addr, node); |
| 964 | if (!pgd) |
| 965 | return -ENOMEM; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 966 | |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 967 | pud = vmemmap_pud_populate(pgd, addr, node); |
| 968 | if (!pud) |
| 969 | return -ENOMEM; |
| 970 | |
Jeremy Fitzhardinge | 7c934d3 | 2008-06-25 00:19:20 -0400 | [diff] [blame] | 971 | if (!cpu_has_pse) { |
| 972 | next = (addr + PAGE_SIZE) & PAGE_MASK; |
| 973 | pmd = vmemmap_pmd_populate(pud, addr, node); |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 974 | |
Jeremy Fitzhardinge | 7c934d3 | 2008-06-25 00:19:20 -0400 | [diff] [blame] | 975 | if (!pmd) |
| 976 | return -ENOMEM; |
| 977 | |
| 978 | p = vmemmap_pte_populate(pmd, addr, node); |
| 979 | |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 980 | if (!p) |
| 981 | return -ENOMEM; |
| 982 | |
Jeremy Fitzhardinge | 7c934d3 | 2008-06-25 00:19:20 -0400 | [diff] [blame] | 983 | addr_end = addr + PAGE_SIZE; |
| 984 | p_end = p + PAGE_SIZE; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 985 | } else { |
Jeremy Fitzhardinge | 7c934d3 | 2008-06-25 00:19:20 -0400 | [diff] [blame] | 986 | next = pmd_addr_end(addr, end); |
| 987 | |
| 988 | pmd = pmd_offset(pud, addr); |
| 989 | if (pmd_none(*pmd)) { |
| 990 | pte_t entry; |
| 991 | |
Yinghai Lu | 9bdac91 | 2010-02-10 01:20:22 -0800 | [diff] [blame] | 992 | p = vmemmap_alloc_block_buf(PMD_SIZE, node); |
Jeremy Fitzhardinge | 7c934d3 | 2008-06-25 00:19:20 -0400 | [diff] [blame] | 993 | if (!p) |
| 994 | return -ENOMEM; |
| 995 | |
| 996 | entry = pfn_pte(__pa(p) >> PAGE_SHIFT, |
| 997 | PAGE_KERNEL_LARGE); |
| 998 | set_pmd(pmd, __pmd(pte_val(entry))); |
| 999 | |
Jeremy Fitzhardinge | 7c934d3 | 2008-06-25 00:19:20 -0400 | [diff] [blame] | 1000 | /* check to see if we have contiguous blocks */ |
| 1001 | if (p_end != p || node_start != node) { |
| 1002 | if (p_start) |
| 1003 | printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n", |
| 1004 | addr_start, addr_end-1, p_start, p_end-1, node_start); |
| 1005 | addr_start = addr; |
| 1006 | node_start = node; |
| 1007 | p_start = p; |
| 1008 | } |
Yinghai Lu | 49c980d | 2008-07-03 12:29:34 -0700 | [diff] [blame] | 1009 | |
| 1010 | addr_end = addr + PMD_SIZE; |
| 1011 | p_end = p + PMD_SIZE; |
Jeremy Fitzhardinge | 7c934d3 | 2008-06-25 00:19:20 -0400 | [diff] [blame] | 1012 | } else |
| 1013 | vmemmap_verify((pte_t *)pmd, node, addr, next); |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1014 | } |
Jeremy Fitzhardinge | 7c934d3 | 2008-06-25 00:19:20 -0400 | [diff] [blame] | 1015 | |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 1016 | } |
Haicheng Li | 9b86152 | 2010-08-20 17:50:16 +0800 | [diff] [blame] | 1017 | sync_global_pgds((unsigned long)start_page, end); |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 1018 | return 0; |
| 1019 | } |
Yinghai Lu | c2b91e2 | 2008-04-12 01:19:24 -0700 | [diff] [blame] | 1020 | |
| 1021 | void __meminit vmemmap_populate_print_last(void) |
| 1022 | { |
| 1023 | if (p_start) { |
| 1024 | printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n", |
| 1025 | addr_start, addr_end-1, p_start, p_end-1, node_start); |
| 1026 | p_start = NULL; |
| 1027 | p_end = NULL; |
| 1028 | node_start = 0; |
| 1029 | } |
| 1030 | } |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 1031 | #endif |