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