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