blob: 3186e68360362f14d6890cdaa6ee4bbb1374ce37 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/x86_64/mm/init.c
3 *
4 * Copyright (C) 1995 Linus Torvalds
Pavel Macheka2531292010-07-18 14:27:13 +02005 * Copyright (C) 2000 Pavel Machek <pavel@ucw.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copyright (C) 2002,2003 Andi Kleen <ak@suse.de>
7 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#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 Gleixner11034d52008-05-12 15:43:36 +020021#include <linux/initrd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/pagemap.h>
23#include <linux/bootmem.h>
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070024#include <linux/memblock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/proc_fs.h>
Andi Kleen59170892005-11-05 17:25:53 +010026#include <linux/pci.h>
Jan Beulich6fb14752007-05-02 19:27:10 +020027#include <linux/pfn.h>
Randy Dunlapc9cf5522006-06-27 02:53:52 -070028#include <linux/poison.h>
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010029#include <linux/dma-mapping.h>
Benjamin Herrenschmidta63fdc52011-06-14 10:57:50 +100030#include <linux/memory.h>
Matt Tolentino44df75e2006-01-17 07:03:41 +010031#include <linux/memory_hotplug.h>
Dan Williams4b94ffd2016-01-15 16:56:22 -080032#include <linux/memremap.h>
Konrad Rzeszutekae32b122007-05-02 19:27:11 +020033#include <linux/nmi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/gfp.h>
David Howells2f96b8c2013-04-12 00:10:25 +010035#include <linux/kcore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include <asm/processor.h>
Ingo Molnar46eaa672008-10-12 15:06:29 +020038#include <asm/bios_ebda.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080039#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/pgtable.h>
41#include <asm/pgalloc.h>
42#include <asm/dma.h>
43#include <asm/fixmap.h>
Ingo Molnar66441bd2017-01-27 10:27:10 +010044#include <asm/e820/api.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#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 Kleen2bc04142005-11-05 17:25:53 +010050#include <asm/sections.h>
Thomas Gleixner718fc132008-01-30 13:30:17 +010051#include <asm/kdebug.h>
Thomas Gleixneraaa64e02008-01-30 13:30:17 +010052#include <asm/numa.h>
Laura Abbottd1163652017-05-08 15:58:11 -070053#include <asm/set_memory.h>
Pekka Enberg4fcb2082009-03-05 14:55:08 +020054#include <asm/init.h>
Seth Jennings43c75f92015-11-30 10:47:43 -060055#include <asm/uv/uv.h>
Yinghai Lue5f15b42011-02-18 11:30:30 +000056#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Yinghai Lu5c51bdb2012-11-16 19:39:01 -080058#include "mm_internal.h"
59
Yinghai Lucf4fb152016-05-06 15:01:34 -070060#include "ident_map.c"
Yinghai Luaece2782013-01-24 12:19:48 -080061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/*
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 Vrabelf9553712014-01-07 17:03:06 +000068pteval_t __supported_pte_mask __read_mostly = ~0;
Yinghai Lubd220a22008-09-05 00:58:28 -070069EXPORT_SYMBOL_GPL(__supported_pte_mask);
70
Yinghai Lubd220a22008-09-05 00:58:28 -070071int force_personality32;
72
Ingo Molnardeed05b2008-09-05 10:23:26 +020073/*
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 Lubd220a22008-09-05 00:58:28 -070081static 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
Kirill A. Shutemov91f606a2018-02-14 21:25:41 +030091static void sync_global_pgds_l5(unsigned long start, unsigned long end)
Kirill A. Shutemov141efad2017-06-06 14:31:29 +030092{
93 unsigned long addr;
94
95 for (addr = start; addr <= end; addr = ALIGN(addr + 1, PGDIR_SIZE)) {
96 const pgd_t *pgd_ref = pgd_offset_k(addr);
97 struct page *page;
98
99 /* Check for overflow */
100 if (addr < start)
101 break;
102
103 if (pgd_none(*pgd_ref))
104 continue;
105
106 spin_lock(&pgd_lock);
107 list_for_each_entry(page, &pgd_list, lru) {
108 pgd_t *pgd;
109 spinlock_t *pgt_lock;
110
111 pgd = (pgd_t *)page_address(page) + pgd_index(addr);
112 /* the pgt_lock only for Xen */
113 pgt_lock = &pgd_page_get_mm(page)->page_table_lock;
114 spin_lock(pgt_lock);
115
116 if (!pgd_none(*pgd_ref) && !pgd_none(*pgd))
117 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
118
119 if (pgd_none(*pgd))
120 set_pgd(pgd, *pgd_ref);
121
122 spin_unlock(pgt_lock);
123 }
124 spin_unlock(&pgd_lock);
125 }
126}
Kirill A. Shutemov91f606a2018-02-14 21:25:41 +0300127
128static void sync_global_pgds_l4(unsigned long start, unsigned long end)
Haicheng Li6afb5152010-05-19 17:42:14 +0800129{
Baoquan Hefc5f9d52017-05-04 10:25:47 +0800130 unsigned long addr;
Haicheng Li6afb5152010-05-19 17:42:14 +0800131
Baoquan Hefc5f9d52017-05-04 10:25:47 +0800132 for (addr = start; addr <= end; addr = ALIGN(addr + 1, PGDIR_SIZE)) {
133 pgd_t *pgd_ref = pgd_offset_k(addr);
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300134 const p4d_t *p4d_ref;
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700135 struct page *page;
Haicheng Li6afb5152010-05-19 17:42:14 +0800136
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300137 /*
138 * With folded p4d, pgd_none() is always false, we need to
139 * handle synchonization on p4d level.
140 */
Kirill A. Shutemovc65e7742018-02-14 14:16:53 +0300141 MAYBE_BUILD_BUG_ON(pgd_none(*pgd_ref));
Baoquan Hefc5f9d52017-05-04 10:25:47 +0800142 p4d_ref = p4d_offset(pgd_ref, addr);
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300143
144 if (p4d_none(*p4d_ref))
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700145 continue;
Haicheng Li6afb5152010-05-19 17:42:14 +0800146
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800147 spin_lock(&pgd_lock);
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700148 list_for_each_entry(page, &pgd_list, lru) {
Linus Torvaldsbe354f42012-12-15 12:29:54 -0800149 pgd_t *pgd;
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300150 p4d_t *p4d;
Jeremy Fitzhardinge617d34d2010-09-21 12:01:51 -0700151 spinlock_t *pgt_lock;
152
Baoquan Hefc5f9d52017-05-04 10:25:47 +0800153 pgd = (pgd_t *)page_address(page) + pgd_index(addr);
154 p4d = p4d_offset(pgd, addr);
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800155 /* the pgt_lock only for Xen */
Jeremy Fitzhardinge617d34d2010-09-21 12:01:51 -0700156 pgt_lock = &pgd_page_get_mm(page)->page_table_lock;
157 spin_lock(pgt_lock);
158
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300159 if (!p4d_none(*p4d_ref) && !p4d_none(*p4d))
160 BUG_ON(p4d_page_vaddr(*p4d)
161 != p4d_page_vaddr(*p4d_ref));
Jeremy Fitzhardinge617d34d2010-09-21 12:01:51 -0700162
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300163 if (p4d_none(*p4d))
164 set_p4d(p4d, *p4d_ref);
Yasuaki Ishimatsu9661d5b2014-08-22 13:27:34 -0700165
Jeremy Fitzhardinge617d34d2010-09-21 12:01:51 -0700166 spin_unlock(pgt_lock);
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700167 }
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800168 spin_unlock(&pgd_lock);
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700169 }
Haicheng Li6afb5152010-05-19 17:42:14 +0800170}
Kirill A. Shutemov91f606a2018-02-14 21:25:41 +0300171
172/*
173 * When memory was added make sure all the processes MM have
174 * suitable PGD entries in the local PGD level page.
175 */
176void sync_global_pgds(unsigned long start, unsigned long end)
177{
178 if (pgtable_l5_enabled)
179 sync_global_pgds_l5(start, end);
180 else
181 sync_global_pgds_l4(start, end);
182}
Haicheng Li6afb5152010-05-19 17:42:14 +0800183
184/*
Marcin Slusarz8d6ea9672008-08-15 18:32:24 +0200185 * NOTE: This function is marked __ref because it calls __init function
186 * (alloc_bootmem_pages). It's safe to do it ONLY when after_bootmem == 0.
187 */
188static __ref void *spp_getpage(void)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100189{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 void *ptr;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (after_bootmem)
Levin, Alexander (Sasha Levin)75f296d2017-11-15 17:35:54 -0800193 ptr = (void *) get_zeroed_page(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 else
195 ptr = alloc_bootmem_pages(PAGE_SIZE);
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100196
197 if (!ptr || ((unsigned long)ptr & ~PAGE_MASK)) {
198 panic("set_pte_phys: cannot allocate page data %s\n",
199 after_bootmem ? "after bootmem" : "");
200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Ingo Molnar10f22dd2008-01-30 13:34:10 +0100202 pr_debug("spp_getpage %p\n", ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100204 return ptr;
205}
206
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300207static p4d_t *fill_p4d(pgd_t *pgd, unsigned long vaddr)
Tejun Heo458a3e62009-02-24 11:57:21 +0900208{
209 if (pgd_none(*pgd)) {
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300210 p4d_t *p4d = (p4d_t *)spp_getpage();
211 pgd_populate(&init_mm, pgd, p4d);
212 if (p4d != p4d_offset(pgd, 0))
Tejun Heo458a3e62009-02-24 11:57:21 +0900213 printk(KERN_ERR "PAGETABLE BUG #00! %p <-> %p\n",
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300214 p4d, p4d_offset(pgd, 0));
Tejun Heo458a3e62009-02-24 11:57:21 +0900215 }
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300216 return p4d_offset(pgd, vaddr);
217}
218
219static pud_t *fill_pud(p4d_t *p4d, unsigned long vaddr)
220{
221 if (p4d_none(*p4d)) {
222 pud_t *pud = (pud_t *)spp_getpage();
223 p4d_populate(&init_mm, p4d, pud);
224 if (pud != pud_offset(p4d, 0))
225 printk(KERN_ERR "PAGETABLE BUG #01! %p <-> %p\n",
226 pud, pud_offset(p4d, 0));
227 }
228 return pud_offset(p4d, vaddr);
Tejun Heo458a3e62009-02-24 11:57:21 +0900229}
230
Jeremy Fitzhardingef254f392009-03-03 12:02:57 -0800231static pmd_t *fill_pmd(pud_t *pud, unsigned long vaddr)
Tejun Heo458a3e62009-02-24 11:57:21 +0900232{
233 if (pud_none(*pud)) {
234 pmd_t *pmd = (pmd_t *) spp_getpage();
235 pud_populate(&init_mm, pud, pmd);
236 if (pmd != pmd_offset(pud, 0))
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300237 printk(KERN_ERR "PAGETABLE BUG #02! %p <-> %p\n",
Tejun Heo458a3e62009-02-24 11:57:21 +0900238 pmd, pmd_offset(pud, 0));
239 }
240 return pmd_offset(pud, vaddr);
241}
242
Jeremy Fitzhardingef254f392009-03-03 12:02:57 -0800243static pte_t *fill_pte(pmd_t *pmd, unsigned long vaddr)
Tejun Heo458a3e62009-02-24 11:57:21 +0900244{
245 if (pmd_none(*pmd)) {
246 pte_t *pte = (pte_t *) spp_getpage();
247 pmd_populate_kernel(&init_mm, pmd, pte);
248 if (pte != pte_offset_kernel(pmd, 0))
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300249 printk(KERN_ERR "PAGETABLE BUG #03!\n");
Tejun Heo458a3e62009-02-24 11:57:21 +0900250 }
251 return pte_offset_kernel(pmd, vaddr);
252}
253
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300254static void __set_pte_vaddr(pud_t *pud, unsigned long vaddr, pte_t new_pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300256 pmd_t *pmd = fill_pmd(pud, vaddr);
257 pte_t *pte = fill_pte(pmd, vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 set_pte(pte, new_pte);
260
261 /*
262 * It's enough to flush this one mapping.
263 * (PGE mappings get flushed as well)
264 */
265 __flush_tlb_one(vaddr);
266}
267
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300268void set_pte_vaddr_p4d(p4d_t *p4d_page, unsigned long vaddr, pte_t new_pte)
269{
270 p4d_t *p4d = p4d_page + p4d_index(vaddr);
271 pud_t *pud = fill_pud(p4d, vaddr);
272
273 __set_pte_vaddr(pud, vaddr, new_pte);
274}
275
276void set_pte_vaddr_pud(pud_t *pud_page, unsigned long vaddr, pte_t new_pte)
277{
278 pud_t *pud = pud_page + pud_index(vaddr);
279
280 __set_pte_vaddr(pud, vaddr, new_pte);
281}
282
Tejun Heo458a3e62009-02-24 11:57:21 +0900283void set_pte_vaddr(unsigned long vaddr, pte_t pteval)
Eduardo Habkost0814e0b2008-06-25 00:19:22 -0400284{
285 pgd_t *pgd;
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300286 p4d_t *p4d_page;
Eduardo Habkost0814e0b2008-06-25 00:19:22 -0400287
288 pr_debug("set_pte_vaddr %lx to %lx\n", vaddr, native_pte_val(pteval));
289
290 pgd = pgd_offset_k(vaddr);
291 if (pgd_none(*pgd)) {
292 printk(KERN_ERR
293 "PGD FIXMAP MISSING, it should be setup in head.S!\n");
294 return;
295 }
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300296
297 p4d_page = p4d_offset(pgd, 0);
298 set_pte_vaddr_p4d(p4d_page, vaddr, pteval);
Eduardo Habkost0814e0b2008-06-25 00:19:22 -0400299}
300
Tejun Heo458a3e62009-02-24 11:57:21 +0900301pmd_t * __init populate_extra_pmd(unsigned long vaddr)
Tejun Heo11124412009-02-20 16:29:09 +0900302{
303 pgd_t *pgd;
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300304 p4d_t *p4d;
Tejun Heo11124412009-02-20 16:29:09 +0900305 pud_t *pud;
306
307 pgd = pgd_offset_k(vaddr);
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300308 p4d = fill_p4d(pgd, vaddr);
309 pud = fill_pud(p4d, vaddr);
Tejun Heo458a3e62009-02-24 11:57:21 +0900310 return fill_pmd(pud, vaddr);
311}
Tejun Heo11124412009-02-20 16:29:09 +0900312
Tejun Heo458a3e62009-02-24 11:57:21 +0900313pte_t * __init populate_extra_pte(unsigned long vaddr)
314{
315 pmd_t *pmd;
316
317 pmd = populate_extra_pmd(vaddr);
318 return fill_pte(pmd, vaddr);
Tejun Heo11124412009-02-20 16:29:09 +0900319}
320
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100321/*
Jack Steiner3a9e1892008-07-01 14:45:32 -0500322 * Create large page table mappings for a range of physical addresses.
323 */
324static void __init __init_extra_mapping(unsigned long phys, unsigned long size,
Juergen Gross2df58b62014-11-03 14:01:52 +0100325 enum page_cache_mode cache)
Jack Steiner3a9e1892008-07-01 14:45:32 -0500326{
327 pgd_t *pgd;
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300328 p4d_t *p4d;
Jack Steiner3a9e1892008-07-01 14:45:32 -0500329 pud_t *pud;
330 pmd_t *pmd;
Juergen Gross2df58b62014-11-03 14:01:52 +0100331 pgprot_t prot;
Jack Steiner3a9e1892008-07-01 14:45:32 -0500332
Juergen Gross2df58b62014-11-03 14:01:52 +0100333 pgprot_val(prot) = pgprot_val(PAGE_KERNEL_LARGE) |
334 pgprot_val(pgprot_4k_2_large(cachemode2pgprot(cache)));
Jack Steiner3a9e1892008-07-01 14:45:32 -0500335 BUG_ON((phys & ~PMD_MASK) || (size & ~PMD_MASK));
336 for (; size; phys += PMD_SIZE, size -= PMD_SIZE) {
337 pgd = pgd_offset_k((unsigned long)__va(phys));
338 if (pgd_none(*pgd)) {
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300339 p4d = (p4d_t *) spp_getpage();
340 set_pgd(pgd, __pgd(__pa(p4d) | _KERNPG_TABLE |
Jack Steiner3a9e1892008-07-01 14:45:32 -0500341 _PAGE_USER));
342 }
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300343 p4d = p4d_offset(pgd, (unsigned long)__va(phys));
344 if (p4d_none(*p4d)) {
345 pud = (pud_t *) spp_getpage();
346 set_p4d(p4d, __p4d(__pa(pud) | _KERNPG_TABLE |
347 _PAGE_USER));
348 }
349 pud = pud_offset(p4d, (unsigned long)__va(phys));
Jack Steiner3a9e1892008-07-01 14:45:32 -0500350 if (pud_none(*pud)) {
351 pmd = (pmd_t *) spp_getpage();
352 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE |
353 _PAGE_USER));
354 }
355 pmd = pmd_offset(pud, phys);
356 BUG_ON(!pmd_none(*pmd));
357 set_pmd(pmd, __pmd(phys | pgprot_val(prot)));
358 }
359}
360
361void __init init_extra_mapping_wb(unsigned long phys, unsigned long size)
362{
Juergen Gross2df58b62014-11-03 14:01:52 +0100363 __init_extra_mapping(phys, size, _PAGE_CACHE_MODE_WB);
Jack Steiner3a9e1892008-07-01 14:45:32 -0500364}
365
366void __init init_extra_mapping_uc(unsigned long phys, unsigned long size)
367{
Juergen Gross2df58b62014-11-03 14:01:52 +0100368 __init_extra_mapping(phys, size, _PAGE_CACHE_MODE_UC);
Jack Steiner3a9e1892008-07-01 14:45:32 -0500369}
370
371/*
Ingo Molnar88f3aec2008-02-21 11:04:11 +0100372 * The head.S code sets up the kernel high mapping:
373 *
374 * from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text)
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100375 *
Zhang Yanfei1e3b3082013-05-15 10:58:07 +0800376 * phys_base holds the negative offset to the kernel, which is added
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100377 * to the compile time generated pmds. This results in invalid pmds up
378 * to the point where we hit the physaddr 0 mapping.
379 *
Yinghai Lue5f15b42011-02-18 11:30:30 +0000380 * We limit the mappings to the region from _text to _brk_end. _brk_end
381 * is rounded up to the 2MB boundary. This catches the invalid pmds as
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100382 * well, as they are located before _text:
383 */
384void __init cleanup_highmap(void)
385{
386 unsigned long vaddr = __START_KERNEL_map;
Yinghai Lu10054232013-01-24 12:19:54 -0800387 unsigned long vaddr_end = __START_KERNEL_map + KERNEL_IMAGE_SIZE;
Yinghai Lue5f15b42011-02-18 11:30:30 +0000388 unsigned long end = roundup((unsigned long)_brk_end, PMD_SIZE) - 1;
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100389 pmd_t *pmd = level2_kernel_pgt;
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100390
Yinghai Lu10054232013-01-24 12:19:54 -0800391 /*
392 * Native path, max_pfn_mapped is not set yet.
393 * Xen has valid max_pfn_mapped set in
394 * arch/x86/xen/mmu.c:xen_setup_kernel_pagetable().
395 */
396 if (max_pfn_mapped)
397 vaddr_end = __START_KERNEL_map + (max_pfn_mapped << PAGE_SHIFT);
398
Yinghai Lue5f15b42011-02-18 11:30:30 +0000399 for (; vaddr + PMD_SIZE - 1 < vaddr_end; pmd++, vaddr += PMD_SIZE) {
Hugh Dickins2884f112008-05-28 19:36:07 +0100400 if (pmd_none(*pmd))
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100401 continue;
402 if (vaddr < (unsigned long) _text || vaddr > end)
403 set_pmd(pmd, __pmd(0));
404 }
405}
406
Thomas Garnier59b3d022016-06-21 17:46:59 -0700407/*
408 * Create PTE level page table mapping for physical addresses.
409 * It returns the last physical address mapped.
410 */
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700411static unsigned long __meminit
Thomas Garnier59b3d022016-06-21 17:46:59 -0700412phys_pte_init(pte_t *pte_page, unsigned long paddr, unsigned long paddr_end,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700413 pgprot_t prot)
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400414{
Thomas Garnier59b3d022016-06-21 17:46:59 -0700415 unsigned long pages = 0, paddr_next;
416 unsigned long paddr_last = paddr_end;
417 pte_t *pte;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400418 int i;
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700419
Thomas Garnier59b3d022016-06-21 17:46:59 -0700420 pte = pte_page + pte_index(paddr);
421 i = pte_index(paddr);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400422
Thomas Garnier59b3d022016-06-21 17:46:59 -0700423 for (; i < PTRS_PER_PTE; i++, paddr = paddr_next, pte++) {
424 paddr_next = (paddr & PAGE_MASK) + PAGE_SIZE;
425 if (paddr >= paddr_end) {
Yinghai Lueceb3632012-11-16 19:38:56 -0800426 if (!after_bootmem &&
Ingo Molnar3bce64f2017-01-28 14:14:25 +0100427 !e820__mapped_any(paddr & PAGE_MASK, paddr_next,
Ingo Molnar09821ff2017-01-28 17:09:33 +0100428 E820_TYPE_RAM) &&
Ingo Molnar3bce64f2017-01-28 14:14:25 +0100429 !e820__mapped_any(paddr & PAGE_MASK, paddr_next,
Ingo Molnar09821ff2017-01-28 17:09:33 +0100430 E820_TYPE_RESERVED_KERN))
Yinghai Lueceb3632012-11-16 19:38:56 -0800431 set_pte(pte, __pte(0));
432 continue;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400433 }
434
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700435 /*
436 * We will re-use the existing mapping.
437 * Xen for example has some special requirements, like mapping
438 * pagetable pages as RO. So assume someone who pre-setup
439 * these mappings are more intelligent.
440 */
Dave Hansendcb32d92016-07-07 17:19:15 -0700441 if (!pte_none(*pte)) {
Jan Beulich876ee612012-10-04 14:48:10 +0100442 if (!after_bootmem)
443 pages++;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400444 continue;
Yinghai Lu3afa3942008-10-25 22:58:21 -0700445 }
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400446
447 if (0)
Thomas Garnier59b3d022016-06-21 17:46:59 -0700448 pr_info(" pte=%p addr=%lx pte=%016lx\n", pte, paddr,
449 pfn_pte(paddr >> PAGE_SHIFT, PAGE_KERNEL).pte);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400450 pages++;
Thomas Garnier59b3d022016-06-21 17:46:59 -0700451 set_pte(pte, pfn_pte(paddr >> PAGE_SHIFT, prot));
452 paddr_last = (paddr & PAGE_MASK) + PAGE_SIZE;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400453 }
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700454
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400455 update_page_count(PG_LEVEL_4K, pages);
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700456
Thomas Garnier59b3d022016-06-21 17:46:59 -0700457 return paddr_last;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400458}
459
Thomas Garnier59b3d022016-06-21 17:46:59 -0700460/*
461 * Create PMD level page table mapping for physical addresses. The virtual
462 * and physical address have to be aligned at this level.
463 * It returns the last physical address mapped.
464 */
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700465static unsigned long __meminit
Thomas Garnier59b3d022016-06-21 17:46:59 -0700466phys_pmd_init(pmd_t *pmd_page, unsigned long paddr, unsigned long paddr_end,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700467 unsigned long page_size_mask, pgprot_t prot)
Matt Tolentino44df75e2006-01-17 07:03:41 +0100468{
Thomas Garnier59b3d022016-06-21 17:46:59 -0700469 unsigned long pages = 0, paddr_next;
470 unsigned long paddr_last = paddr_end;
Andi Kleence0c0e52008-05-02 11:46:49 +0200471
Thomas Garnier59b3d022016-06-21 17:46:59 -0700472 int i = pmd_index(paddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Thomas Garnier59b3d022016-06-21 17:46:59 -0700474 for (; i < PTRS_PER_PMD; i++, paddr = paddr_next) {
475 pmd_t *pmd = pmd_page + pmd_index(paddr);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400476 pte_t *pte;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700477 pgprot_t new_prot = prot;
Matt Tolentino44df75e2006-01-17 07:03:41 +0100478
Thomas Garnier59b3d022016-06-21 17:46:59 -0700479 paddr_next = (paddr & PMD_MASK) + PMD_SIZE;
480 if (paddr >= paddr_end) {
Yinghai Lueceb3632012-11-16 19:38:56 -0800481 if (!after_bootmem &&
Ingo Molnar3bce64f2017-01-28 14:14:25 +0100482 !e820__mapped_any(paddr & PMD_MASK, paddr_next,
Ingo Molnar09821ff2017-01-28 17:09:33 +0100483 E820_TYPE_RAM) &&
Ingo Molnar3bce64f2017-01-28 14:14:25 +0100484 !e820__mapped_any(paddr & PMD_MASK, paddr_next,
Ingo Molnar09821ff2017-01-28 17:09:33 +0100485 E820_TYPE_RESERVED_KERN))
Yinghai Lueceb3632012-11-16 19:38:56 -0800486 set_pmd(pmd, __pmd(0));
487 continue;
488 }
Jan Beulich20167d32012-05-16 14:06:26 +0100489
Dave Hansendcb32d92016-07-07 17:19:15 -0700490 if (!pmd_none(*pmd)) {
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100491 if (!pmd_large(*pmd)) {
492 spin_lock(&init_mm.page_table_lock);
Yinghai Lu973dc4f2012-11-16 19:38:59 -0800493 pte = (pte_t *)pmd_page_vaddr(*pmd);
Thomas Garnier59b3d022016-06-21 17:46:59 -0700494 paddr_last = phys_pte_init(pte, paddr,
495 paddr_end, prot);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100496 spin_unlock(&init_mm.page_table_lock);
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700497 continue;
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100498 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700499 /*
500 * If we are ok with PG_LEVEL_2M mapping, then we will
501 * use the existing mapping,
502 *
503 * Otherwise, we will split the large page mapping but
504 * use the same existing protection bits except for
505 * large 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 Lu3afa3942008-10-25 22:58:21 -0700511 if (page_size_mask & (1 << PG_LEVEL_2M)) {
Jan Beulich876ee612012-10-04 14:48:10 +0100512 if (!after_bootmem)
513 pages++;
Thomas Garnier59b3d022016-06-21 17:46:59 -0700514 paddr_last = paddr_next;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700515 continue;
Yinghai Lu3afa3942008-10-25 22:58:21 -0700516 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700517 new_prot = pte_pgprot(pte_clrhuge(*(pte_t *)pmd));
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400518 }
Keith Mannthey6ad91652006-09-26 10:52:36 +0200519
Yinghai Lub50efd22008-07-08 01:41:05 -0700520 if (page_size_mask & (1<<PG_LEVEL_2M)) {
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400521 pages++;
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100522 spin_lock(&init_mm.page_table_lock);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400523 set_pte((pte_t *)pmd,
Thomas Garnier59b3d022016-06-21 17:46:59 -0700524 pfn_pte((paddr & PMD_MASK) >> PAGE_SHIFT,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700525 __pgprot(pgprot_val(prot) | _PAGE_PSE)));
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100526 spin_unlock(&init_mm.page_table_lock);
Thomas Garnier59b3d022016-06-21 17:46:59 -0700527 paddr_last = paddr_next;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400528 continue;
529 }
530
Yinghai Lu868bf4d2012-11-16 19:39:00 -0800531 pte = alloc_low_page();
Thomas Garnier59b3d022016-06-21 17:46:59 -0700532 paddr_last = phys_pte_init(pte, paddr, paddr_end, new_prot);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400533
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100534 spin_lock(&init_mm.page_table_lock);
Yinghai Lu868bf4d2012-11-16 19:39:00 -0800535 pmd_populate_kernel(&init_mm, pmd, pte);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100536 spin_unlock(&init_mm.page_table_lock);
Matt Tolentino44df75e2006-01-17 07:03:41 +0100537 }
Andi Kleence0c0e52008-05-02 11:46:49 +0200538 update_page_count(PG_LEVEL_2M, pages);
Thomas Garnier59b3d022016-06-21 17:46:59 -0700539 return paddr_last;
Matt Tolentino44df75e2006-01-17 07:03:41 +0100540}
541
Thomas Garnier59b3d022016-06-21 17:46:59 -0700542/*
543 * Create PUD level page table mapping for physical addresses. The virtual
Thomas Garnierfaa37932016-06-21 17:47:00 -0700544 * and physical address do not have to be aligned at this level. KASLR can
545 * randomize virtual addresses up to this level.
Thomas Garnier59b3d022016-06-21 17:46:59 -0700546 * It returns the last physical address mapped.
547 */
Andi Kleencc615032008-03-12 03:53:28 +0100548static unsigned long __meminit
Thomas Garnier59b3d022016-06-21 17:46:59 -0700549phys_pud_init(pud_t *pud_page, unsigned long paddr, unsigned long paddr_end,
550 unsigned long page_size_mask)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100551{
Thomas Garnier59b3d022016-06-21 17:46:59 -0700552 unsigned long pages = 0, paddr_next;
553 unsigned long paddr_last = paddr_end;
Thomas Garnierfaa37932016-06-21 17:47:00 -0700554 unsigned long vaddr = (unsigned long)__va(paddr);
555 int i = pud_index(vaddr);
Matt Tolentino44df75e2006-01-17 07:03:41 +0100556
Thomas Garnier59b3d022016-06-21 17:46:59 -0700557 for (; i < PTRS_PER_PUD; i++, paddr = paddr_next) {
Thomas Garnierfaa37932016-06-21 17:47:00 -0700558 pud_t *pud;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 pmd_t *pmd;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700560 pgprot_t prot = PAGE_KERNEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Thomas Garnierfaa37932016-06-21 17:47:00 -0700562 vaddr = (unsigned long)__va(paddr);
563 pud = pud_page + pud_index(vaddr);
Thomas Garnier59b3d022016-06-21 17:46:59 -0700564 paddr_next = (paddr & PUD_MASK) + PUD_SIZE;
Thomas Garnierfaa37932016-06-21 17:47:00 -0700565
Thomas Garnier59b3d022016-06-21 17:46:59 -0700566 if (paddr >= paddr_end) {
Yinghai Lueceb3632012-11-16 19:38:56 -0800567 if (!after_bootmem &&
Ingo Molnar3bce64f2017-01-28 14:14:25 +0100568 !e820__mapped_any(paddr & PUD_MASK, paddr_next,
Ingo Molnar09821ff2017-01-28 17:09:33 +0100569 E820_TYPE_RAM) &&
Ingo Molnar3bce64f2017-01-28 14:14:25 +0100570 !e820__mapped_any(paddr & PUD_MASK, paddr_next,
Ingo Molnar09821ff2017-01-28 17:09:33 +0100571 E820_TYPE_RESERVED_KERN))
Yinghai Lueceb3632012-11-16 19:38:56 -0800572 set_pud(pud, __pud(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 continue;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Dave Hansendcb32d92016-07-07 17:19:15 -0700576 if (!pud_none(*pud)) {
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700577 if (!pud_large(*pud)) {
Yinghai Lu973dc4f2012-11-16 19:38:59 -0800578 pmd = pmd_offset(pud, 0);
Thomas Garnier59b3d022016-06-21 17:46:59 -0700579 paddr_last = phys_pmd_init(pmd, paddr,
580 paddr_end,
581 page_size_mask,
582 prot);
Yinghai Lu4b239f42010-12-17 16:58:28 -0800583 __flush_tlb_all();
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700584 continue;
585 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700586 /*
587 * If we are ok with PG_LEVEL_1G mapping, then we will
588 * use the existing mapping.
589 *
590 * Otherwise, we will split the gbpage mapping but use
591 * the same existing protection bits except for large
592 * page, so that we don't violate Intel's TLB
593 * Application note (317080) which says, while changing
594 * the page sizes, new and old translations should
595 * not differ with respect to page frame and
596 * attributes.
597 */
Yinghai Lu3afa3942008-10-25 22:58:21 -0700598 if (page_size_mask & (1 << PG_LEVEL_1G)) {
Jan Beulich876ee612012-10-04 14:48:10 +0100599 if (!after_bootmem)
600 pages++;
Thomas Garnier59b3d022016-06-21 17:46:59 -0700601 paddr_last = paddr_next;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700602 continue;
Yinghai Lu3afa3942008-10-25 22:58:21 -0700603 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700604 prot = pte_pgprot(pte_clrhuge(*(pte_t *)pud));
Andi Kleenef925762008-04-17 17:40:45 +0200605 }
606
Yinghai Lub50efd22008-07-08 01:41:05 -0700607 if (page_size_mask & (1<<PG_LEVEL_1G)) {
Andi Kleence0c0e52008-05-02 11:46:49 +0200608 pages++;
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100609 spin_lock(&init_mm.page_table_lock);
Andi Kleenef925762008-04-17 17:40:45 +0200610 set_pte((pte_t *)pud,
Thomas Garnier59b3d022016-06-21 17:46:59 -0700611 pfn_pte((paddr & PUD_MASK) >> PAGE_SHIFT,
Yinghai Lu960ddb4f2012-11-16 19:38:54 -0800612 PAGE_KERNEL_LARGE));
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100613 spin_unlock(&init_mm.page_table_lock);
Thomas Garnier59b3d022016-06-21 17:46:59 -0700614 paddr_last = paddr_next;
Keith Mannthey6ad91652006-09-26 10:52:36 +0200615 continue;
616 }
617
Yinghai Lu868bf4d2012-11-16 19:39:00 -0800618 pmd = alloc_low_page();
Thomas Garnier59b3d022016-06-21 17:46:59 -0700619 paddr_last = phys_pmd_init(pmd, paddr, paddr_end,
620 page_size_mask, prot);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100621
622 spin_lock(&init_mm.page_table_lock);
Yinghai Lu868bf4d2012-11-16 19:39:00 -0800623 pud_populate(&init_mm, pud, pmd);
Matt Tolentino44df75e2006-01-17 07:03:41 +0100624 spin_unlock(&init_mm.page_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
Andi Kleen1a2b4412008-01-30 13:33:54 +0100626 __flush_tlb_all();
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700627
Andi Kleence0c0e52008-05-02 11:46:49 +0200628 update_page_count(PG_LEVEL_1G, pages);
Andi Kleencc615032008-03-12 03:53:28 +0100629
Thomas Garnier59b3d022016-06-21 17:46:59 -0700630 return paddr_last;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100631}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Kirill A. Shutemov7e82ea92017-06-06 14:31:30 +0300633static unsigned long __meminit
634phys_p4d_init(p4d_t *p4d_page, unsigned long paddr, unsigned long paddr_end,
635 unsigned long page_size_mask)
636{
637 unsigned long paddr_next, paddr_last = paddr_end;
638 unsigned long vaddr = (unsigned long)__va(paddr);
639 int i = p4d_index(vaddr);
640
Kirill A. Shutemov91f606a2018-02-14 21:25:41 +0300641 if (!pgtable_l5_enabled)
Kirill A. Shutemov7e82ea92017-06-06 14:31:30 +0300642 return phys_pud_init((pud_t *) p4d_page, paddr, paddr_end, page_size_mask);
643
644 for (; i < PTRS_PER_P4D; i++, paddr = paddr_next) {
645 p4d_t *p4d;
646 pud_t *pud;
647
648 vaddr = (unsigned long)__va(paddr);
649 p4d = p4d_page + p4d_index(vaddr);
650 paddr_next = (paddr & P4D_MASK) + P4D_SIZE;
651
652 if (paddr >= paddr_end) {
653 if (!after_bootmem &&
654 !e820__mapped_any(paddr & P4D_MASK, paddr_next,
655 E820_TYPE_RAM) &&
656 !e820__mapped_any(paddr & P4D_MASK, paddr_next,
657 E820_TYPE_RESERVED_KERN))
658 set_p4d(p4d, __p4d(0));
659 continue;
660 }
661
662 if (!p4d_none(*p4d)) {
663 pud = pud_offset(p4d, 0);
664 paddr_last = phys_pud_init(pud, paddr,
665 paddr_end,
666 page_size_mask);
667 __flush_tlb_all();
668 continue;
669 }
670
671 pud = alloc_low_page();
672 paddr_last = phys_pud_init(pud, paddr, paddr_end,
673 page_size_mask);
674
675 spin_lock(&init_mm.page_table_lock);
676 p4d_populate(&init_mm, p4d, pud);
677 spin_unlock(&init_mm.page_table_lock);
678 }
679 __flush_tlb_all();
680
681 return paddr_last;
682}
683
Thomas Garnier59b3d022016-06-21 17:46:59 -0700684/*
685 * Create page table mapping for the physical memory for specific physical
Thomas Garnierfaa37932016-06-21 17:47:00 -0700686 * addresses. The virtual and physical addresses have to be aligned on PMD level
Thomas Garnier59b3d022016-06-21 17:46:59 -0700687 * down. It returns the last physical address mapped.
688 */
Shaohua Li41d840e2009-06-12 12:57:52 +0800689unsigned long __meminit
Thomas Garnier59b3d022016-06-21 17:46:59 -0700690kernel_physical_mapping_init(unsigned long paddr_start,
691 unsigned long paddr_end,
Pekka Enbergf7650902009-03-05 14:55:05 +0200692 unsigned long page_size_mask)
Yinghai Lub50efd22008-07-08 01:41:05 -0700693{
Haicheng Li9b861522010-08-20 17:50:16 +0800694 bool pgd_changed = false;
Thomas Garnier59b3d022016-06-21 17:46:59 -0700695 unsigned long vaddr, vaddr_start, vaddr_end, vaddr_next, paddr_last;
Yinghai Lub50efd22008-07-08 01:41:05 -0700696
Thomas Garnier59b3d022016-06-21 17:46:59 -0700697 paddr_last = paddr_end;
698 vaddr = (unsigned long)__va(paddr_start);
699 vaddr_end = (unsigned long)__va(paddr_end);
700 vaddr_start = vaddr;
Yinghai Lub50efd22008-07-08 01:41:05 -0700701
Thomas Garnier59b3d022016-06-21 17:46:59 -0700702 for (; vaddr < vaddr_end; vaddr = vaddr_next) {
703 pgd_t *pgd = pgd_offset_k(vaddr);
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300704 p4d_t *p4d;
Yinghai Lub50efd22008-07-08 01:41:05 -0700705
Thomas Garnier59b3d022016-06-21 17:46:59 -0700706 vaddr_next = (vaddr & PGDIR_MASK) + PGDIR_SIZE;
Yinghai Lub50efd22008-07-08 01:41:05 -0700707
Kirill A. Shutemov7e82ea92017-06-06 14:31:30 +0300708 if (pgd_val(*pgd)) {
709 p4d = (p4d_t *)pgd_page_vaddr(*pgd);
710 paddr_last = phys_p4d_init(p4d, __pa(vaddr),
Thomas Garnier59b3d022016-06-21 17:46:59 -0700711 __pa(vaddr_end),
712 page_size_mask);
Yinghai Lub50efd22008-07-08 01:41:05 -0700713 continue;
714 }
715
Kirill A. Shutemov7e82ea92017-06-06 14:31:30 +0300716 p4d = alloc_low_page();
717 paddr_last = phys_p4d_init(p4d, __pa(vaddr), __pa(vaddr_end),
Thomas Garnier59b3d022016-06-21 17:46:59 -0700718 page_size_mask);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100719
720 spin_lock(&init_mm.page_table_lock);
Kirill A. Shutemov91f606a2018-02-14 21:25:41 +0300721 if (pgtable_l5_enabled)
Kirill A. Shutemov7e82ea92017-06-06 14:31:30 +0300722 pgd_populate(&init_mm, pgd, p4d);
723 else
724 p4d_populate(&init_mm, p4d_offset(pgd, vaddr), (pud_t *) p4d);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100725 spin_unlock(&init_mm.page_table_lock);
Haicheng Li9b861522010-08-20 17:50:16 +0800726 pgd_changed = true;
Yinghai Lub50efd22008-07-08 01:41:05 -0700727 }
Haicheng Li9b861522010-08-20 17:50:16 +0800728
729 if (pgd_changed)
Kirill A. Shutemov5372e152016-12-15 02:44:03 +0300730 sync_global_pgds(vaddr_start, vaddr_end - 1);
Haicheng Li9b861522010-08-20 17:50:16 +0800731
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700732 __flush_tlb_all();
Yinghai Lub50efd22008-07-08 01:41:05 -0700733
Thomas Garnier59b3d022016-06-21 17:46:59 -0700734 return paddr_last;
Yinghai Lub50efd22008-07-08 01:41:05 -0700735}
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700736
Matt Tolentino2b976902005-06-23 00:08:06 -0700737#ifndef CONFIG_NUMA
Tejun Heod8fc3af2011-02-16 12:13:06 +0100738void __init initmem_init(void)
Yinghai Lu1f75d7e2008-06-22 02:44:49 -0700739{
Tang Chene7e8de52014-01-21 15:49:26 -0800740 memblock_set_node(0, (phys_addr_t)ULLONG_MAX, &memblock.memory, 0);
Yinghai Lu1f75d7e2008-06-22 02:44:49 -0700741}
Pekka Enberg3551f882009-05-07 15:35:41 +0300742#endif
Yinghai Lu1f75d7e2008-06-22 02:44:49 -0700743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744void __init paging_init(void)
745{
Pekka Enberg3551f882009-05-07 15:35:41 +0300746 sparse_memory_present_with_active_regions(MAX_NUMNODES);
Matt Tolentino44df75e2006-01-17 07:03:41 +0100747 sparse_init();
Yinghai Lu44b57282009-07-08 09:50:19 -0700748
749 /*
750 * clear the default setting with node 0
751 * note: don't use nodes_clear here, that is really clearing when
752 * numa support is not compiled in, and later node_set_state
753 * will not set it back.
754 */
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -0800755 node_clear_state(0, N_MEMORY);
756 if (N_MEMORY != N_NORMAL_MEMORY)
757 node_clear_state(0, N_NORMAL_MEMORY);
Yinghai Lu44b57282009-07-08 09:50:19 -0700758
Pekka Enberg4c0b2e52011-11-01 15:58:17 +0200759 zone_sizes_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100762/*
Matt Tolentino44df75e2006-01-17 07:03:41 +0100763 * Memory hotplug specific functions
Matt Tolentino44df75e2006-01-17 07:03:41 +0100764 */
Yasunori Gotobc02af92006-06-27 02:53:30 -0700765#ifdef CONFIG_MEMORY_HOTPLUG
766/*
Shaohui Zhengea085412010-02-02 13:44:16 -0800767 * After memory hotplug the variables max_pfn, max_low_pfn and high_memory need
768 * updating.
769 */
Michal Hocko3072e412017-09-08 16:11:39 -0700770static void update_end_of_memory_vars(u64 start, u64 size)
Shaohui Zhengea085412010-02-02 13:44:16 -0800771{
772 unsigned long end_pfn = PFN_UP(start + size);
773
774 if (end_pfn > max_pfn) {
775 max_pfn = end_pfn;
776 max_low_pfn = end_pfn;
777 high_memory = (void *)__va(max_pfn * PAGE_SIZE - 1) + 1;
778 }
779}
780
Christoph Hellwig24e6d5a2017-12-29 08:53:53 +0100781int add_pages(int nid, unsigned long start_pfn, unsigned long nr_pages,
782 struct vmem_altmap *altmap, bool want_memblock)
Yasunori Gotobc02af92006-06-27 02:53:30 -0700783{
Yasunori Gotobc02af92006-06-27 02:53:30 -0700784 int ret;
785
Christoph Hellwig24e6d5a2017-12-29 08:53:53 +0100786 ret = __add_pages(nid, start_pfn, nr_pages, altmap, want_memblock);
Gary Hadefe8b8682008-10-28 16:43:14 -0700787 WARN_ON_ONCE(ret);
Yasunori Gotobc02af92006-06-27 02:53:30 -0700788
Shaohui Zhengea085412010-02-02 13:44:16 -0800789 /* update max_pfn, max_low_pfn and high_memory */
Michal Hocko3072e412017-09-08 16:11:39 -0700790 update_end_of_memory_vars(start_pfn << PAGE_SHIFT,
791 nr_pages << PAGE_SHIFT);
Shaohui Zhengea085412010-02-02 13:44:16 -0800792
Yasunori Gotobc02af92006-06-27 02:53:30 -0700793 return ret;
Yasunori Gotobc02af92006-06-27 02:53:30 -0700794}
Michal Hocko3072e412017-09-08 16:11:39 -0700795
Christoph Hellwig24e6d5a2017-12-29 08:53:53 +0100796int arch_add_memory(int nid, u64 start, u64 size, struct vmem_altmap *altmap,
797 bool want_memblock)
Michal Hocko3072e412017-09-08 16:11:39 -0700798{
799 unsigned long start_pfn = start >> PAGE_SHIFT;
800 unsigned long nr_pages = size >> PAGE_SHIFT;
801
802 init_memory_mapping(start, start + size);
803
Christoph Hellwig24e6d5a2017-12-29 08:53:53 +0100804 return add_pages(nid, start_pfn, nr_pages, altmap, want_memblock);
Michal Hocko3072e412017-09-08 16:11:39 -0700805}
Yasunori Gotobc02af92006-06-27 02:53:30 -0700806
Wen Congyangae9aae92013-02-22 16:33:04 -0800807#define PAGE_INUSE 0xFD
808
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100809static void __meminit free_pagetable(struct page *page, int order,
810 struct vmem_altmap *altmap)
Wen Congyangae9aae92013-02-22 16:33:04 -0800811{
Wen Congyangae9aae92013-02-22 16:33:04 -0800812 unsigned long magic;
813 unsigned int nr_pages = 1 << order;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800814
815 if (altmap) {
816 vmem_altmap_free(altmap, nr_pages);
817 return;
818 }
Wen Congyangae9aae92013-02-22 16:33:04 -0800819
820 /* bootmem page has reserved flag */
821 if (PageReserved(page)) {
822 __ClearPageReserved(page);
Wen Congyangae9aae92013-02-22 16:33:04 -0800823
Yasuaki Ishimatsuddffe982017-02-22 15:45:13 -0800824 magic = (unsigned long)page->freelist;
Wen Congyangae9aae92013-02-22 16:33:04 -0800825 if (magic == SECTION_INFO || magic == MIX_SECTION_INFO) {
826 while (nr_pages--)
827 put_page_bootmem(page++);
828 } else
Jiang Liu170a5a72013-07-03 15:03:17 -0700829 while (nr_pages--)
830 free_reserved_page(page++);
Wen Congyangae9aae92013-02-22 16:33:04 -0800831 } else
832 free_pages((unsigned long)page_address(page), order);
Wen Congyangae9aae92013-02-22 16:33:04 -0800833}
834
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100835static void __meminit free_pte_table(pte_t *pte_start, pmd_t *pmd,
836 struct vmem_altmap *altmap)
Wen Congyangae9aae92013-02-22 16:33:04 -0800837{
838 pte_t *pte;
839 int i;
840
841 for (i = 0; i < PTRS_PER_PTE; i++) {
842 pte = pte_start + i;
Dave Hansendcb32d92016-07-07 17:19:15 -0700843 if (!pte_none(*pte))
Wen Congyangae9aae92013-02-22 16:33:04 -0800844 return;
845 }
846
847 /* free a pte talbe */
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100848 free_pagetable(pmd_page(*pmd), 0, altmap);
Wen Congyangae9aae92013-02-22 16:33:04 -0800849 spin_lock(&init_mm.page_table_lock);
850 pmd_clear(pmd);
851 spin_unlock(&init_mm.page_table_lock);
852}
853
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100854static void __meminit free_pmd_table(pmd_t *pmd_start, pud_t *pud,
855 struct vmem_altmap *altmap)
Wen Congyangae9aae92013-02-22 16:33:04 -0800856{
857 pmd_t *pmd;
858 int i;
859
860 for (i = 0; i < PTRS_PER_PMD; i++) {
861 pmd = pmd_start + i;
Dave Hansendcb32d92016-07-07 17:19:15 -0700862 if (!pmd_none(*pmd))
Wen Congyangae9aae92013-02-22 16:33:04 -0800863 return;
864 }
865
866 /* free a pmd talbe */
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100867 free_pagetable(pud_page(*pud), 0, altmap);
Wen Congyangae9aae92013-02-22 16:33:04 -0800868 spin_lock(&init_mm.page_table_lock);
869 pud_clear(pud);
870 spin_unlock(&init_mm.page_table_lock);
871}
872
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100873static void __meminit free_pud_table(pud_t *pud_start, p4d_t *p4d,
874 struct vmem_altmap *altmap)
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300875{
876 pud_t *pud;
877 int i;
878
879 for (i = 0; i < PTRS_PER_PUD; i++) {
880 pud = pud_start + i;
881 if (!pud_none(*pud))
882 return;
883 }
884
885 /* free a pud talbe */
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100886 free_pagetable(p4d_page(*p4d), 0, altmap);
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300887 spin_lock(&init_mm.page_table_lock);
888 p4d_clear(p4d);
889 spin_unlock(&init_mm.page_table_lock);
890}
891
Wen Congyangae9aae92013-02-22 16:33:04 -0800892static void __meminit
893remove_pte_table(pte_t *pte_start, unsigned long addr, unsigned long end,
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100894 struct vmem_altmap *altmap, bool direct)
Wen Congyangae9aae92013-02-22 16:33:04 -0800895{
896 unsigned long next, pages = 0;
897 pte_t *pte;
898 void *page_addr;
899 phys_addr_t phys_addr;
900
901 pte = pte_start + pte_index(addr);
902 for (; addr < end; addr = next, pte++) {
903 next = (addr + PAGE_SIZE) & PAGE_MASK;
904 if (next > end)
905 next = end;
906
907 if (!pte_present(*pte))
908 continue;
909
910 /*
911 * We mapped [0,1G) memory as identity mapping when
912 * initializing, in arch/x86/kernel/head_64.S. These
913 * pagetables cannot be removed.
914 */
915 phys_addr = pte_val(*pte) + (addr & PAGE_MASK);
916 if (phys_addr < (phys_addr_t)0x40000000)
917 return;
918
Kefeng Wangb500f772016-01-12 10:19:30 +0800919 if (PAGE_ALIGNED(addr) && PAGE_ALIGNED(next)) {
Wen Congyangae9aae92013-02-22 16:33:04 -0800920 /*
921 * Do not free direct mapping pages since they were
922 * freed when offlining, or simplely not in use.
923 */
924 if (!direct)
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100925 free_pagetable(pte_page(*pte), 0, altmap);
Wen Congyangae9aae92013-02-22 16:33:04 -0800926
927 spin_lock(&init_mm.page_table_lock);
928 pte_clear(&init_mm, addr, pte);
929 spin_unlock(&init_mm.page_table_lock);
930
931 /* For non-direct mapping, pages means nothing. */
932 pages++;
933 } else {
934 /*
935 * If we are here, we are freeing vmemmap pages since
936 * direct mapped memory ranges to be freed are aligned.
937 *
938 * If we are not removing the whole page, it means
939 * other page structs in this page are being used and
940 * we canot remove them. So fill the unused page_structs
941 * with 0xFD, and remove the page when it is wholly
942 * filled with 0xFD.
943 */
944 memset((void *)addr, PAGE_INUSE, next - addr);
945
946 page_addr = page_address(pte_page(*pte));
947 if (!memchr_inv(page_addr, PAGE_INUSE, PAGE_SIZE)) {
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100948 free_pagetable(pte_page(*pte), 0, altmap);
Wen Congyangae9aae92013-02-22 16:33:04 -0800949
950 spin_lock(&init_mm.page_table_lock);
951 pte_clear(&init_mm, addr, pte);
952 spin_unlock(&init_mm.page_table_lock);
953 }
954 }
955 }
956
957 /* Call free_pte_table() in remove_pmd_table(). */
958 flush_tlb_all();
959 if (direct)
960 update_page_count(PG_LEVEL_4K, -pages);
961}
962
963static void __meminit
964remove_pmd_table(pmd_t *pmd_start, unsigned long addr, unsigned long end,
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100965 bool direct, struct vmem_altmap *altmap)
Wen Congyangae9aae92013-02-22 16:33:04 -0800966{
967 unsigned long next, pages = 0;
968 pte_t *pte_base;
969 pmd_t *pmd;
970 void *page_addr;
971
972 pmd = pmd_start + pmd_index(addr);
973 for (; addr < end; addr = next, pmd++) {
974 next = pmd_addr_end(addr, end);
975
976 if (!pmd_present(*pmd))
977 continue;
978
979 if (pmd_large(*pmd)) {
980 if (IS_ALIGNED(addr, PMD_SIZE) &&
981 IS_ALIGNED(next, PMD_SIZE)) {
982 if (!direct)
983 free_pagetable(pmd_page(*pmd),
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100984 get_order(PMD_SIZE),
985 altmap);
Wen Congyangae9aae92013-02-22 16:33:04 -0800986
987 spin_lock(&init_mm.page_table_lock);
988 pmd_clear(pmd);
989 spin_unlock(&init_mm.page_table_lock);
990 pages++;
991 } else {
992 /* If here, we are freeing vmemmap pages. */
993 memset((void *)addr, PAGE_INUSE, next - addr);
994
995 page_addr = page_address(pmd_page(*pmd));
996 if (!memchr_inv(page_addr, PAGE_INUSE,
997 PMD_SIZE)) {
998 free_pagetable(pmd_page(*pmd),
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100999 get_order(PMD_SIZE),
1000 altmap);
Wen Congyangae9aae92013-02-22 16:33:04 -08001001
1002 spin_lock(&init_mm.page_table_lock);
1003 pmd_clear(pmd);
1004 spin_unlock(&init_mm.page_table_lock);
1005 }
1006 }
1007
1008 continue;
1009 }
1010
1011 pte_base = (pte_t *)pmd_page_vaddr(*pmd);
Christoph Hellwig24b6d412017-12-29 08:53:56 +01001012 remove_pte_table(pte_base, addr, next, altmap, direct);
1013 free_pte_table(pte_base, pmd, altmap);
Wen Congyangae9aae92013-02-22 16:33:04 -08001014 }
1015
1016 /* Call free_pmd_table() in remove_pud_table(). */
1017 if (direct)
1018 update_page_count(PG_LEVEL_2M, -pages);
1019}
1020
1021static void __meminit
1022remove_pud_table(pud_t *pud_start, unsigned long addr, unsigned long end,
Christoph Hellwig24b6d412017-12-29 08:53:56 +01001023 struct vmem_altmap *altmap, bool direct)
Wen Congyangae9aae92013-02-22 16:33:04 -08001024{
1025 unsigned long next, pages = 0;
1026 pmd_t *pmd_base;
1027 pud_t *pud;
1028 void *page_addr;
1029
1030 pud = pud_start + pud_index(addr);
1031 for (; addr < end; addr = next, pud++) {
1032 next = pud_addr_end(addr, end);
1033
1034 if (!pud_present(*pud))
1035 continue;
1036
1037 if (pud_large(*pud)) {
1038 if (IS_ALIGNED(addr, PUD_SIZE) &&
1039 IS_ALIGNED(next, PUD_SIZE)) {
1040 if (!direct)
1041 free_pagetable(pud_page(*pud),
Christoph Hellwig24b6d412017-12-29 08:53:56 +01001042 get_order(PUD_SIZE),
1043 altmap);
Wen Congyangae9aae92013-02-22 16:33:04 -08001044
1045 spin_lock(&init_mm.page_table_lock);
1046 pud_clear(pud);
1047 spin_unlock(&init_mm.page_table_lock);
1048 pages++;
1049 } else {
1050 /* If here, we are freeing vmemmap pages. */
1051 memset((void *)addr, PAGE_INUSE, next - addr);
1052
1053 page_addr = page_address(pud_page(*pud));
1054 if (!memchr_inv(page_addr, PAGE_INUSE,
1055 PUD_SIZE)) {
1056 free_pagetable(pud_page(*pud),
Christoph Hellwig24b6d412017-12-29 08:53:56 +01001057 get_order(PUD_SIZE),
1058 altmap);
Wen Congyangae9aae92013-02-22 16:33:04 -08001059
1060 spin_lock(&init_mm.page_table_lock);
1061 pud_clear(pud);
1062 spin_unlock(&init_mm.page_table_lock);
1063 }
1064 }
1065
1066 continue;
1067 }
1068
Kirill A. Shutemove6ab9c42017-04-25 12:25:57 +03001069 pmd_base = pmd_offset(pud, 0);
Christoph Hellwig24b6d412017-12-29 08:53:56 +01001070 remove_pmd_table(pmd_base, addr, next, direct, altmap);
1071 free_pmd_table(pmd_base, pud, altmap);
Wen Congyangae9aae92013-02-22 16:33:04 -08001072 }
1073
1074 if (direct)
1075 update_page_count(PG_LEVEL_1G, -pages);
1076}
1077
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +03001078static void __meminit
1079remove_p4d_table(p4d_t *p4d_start, unsigned long addr, unsigned long end,
Christoph Hellwig24b6d412017-12-29 08:53:56 +01001080 struct vmem_altmap *altmap, bool direct)
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +03001081{
1082 unsigned long next, pages = 0;
1083 pud_t *pud_base;
1084 p4d_t *p4d;
1085
1086 p4d = p4d_start + p4d_index(addr);
1087 for (; addr < end; addr = next, p4d++) {
1088 next = p4d_addr_end(addr, end);
1089
1090 if (!p4d_present(*p4d))
1091 continue;
1092
1093 BUILD_BUG_ON(p4d_large(*p4d));
1094
Kirill A. Shutemove6ab9c42017-04-25 12:25:57 +03001095 pud_base = pud_offset(p4d, 0);
Christoph Hellwig24b6d412017-12-29 08:53:56 +01001096 remove_pud_table(pud_base, addr, next, altmap, direct);
Jérôme Glisse98fe3632017-06-24 14:05:14 -04001097 /*
1098 * For 4-level page tables we do not want to free PUDs, but in the
1099 * 5-level case we should free them. This code will have to change
1100 * to adapt for boot-time switching between 4 and 5 level page tables.
1101 */
Kirill A. Shutemov91f606a2018-02-14 21:25:41 +03001102 if (pgtable_l5_enabled)
Christoph Hellwig24b6d412017-12-29 08:53:56 +01001103 free_pud_table(pud_base, p4d, altmap);
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +03001104 }
1105
1106 if (direct)
1107 update_page_count(PG_LEVEL_512G, -pages);
1108}
1109
Wen Congyangae9aae92013-02-22 16:33:04 -08001110/* start and end are both virtual address. */
1111static void __meminit
Christoph Hellwig24b6d412017-12-29 08:53:56 +01001112remove_pagetable(unsigned long start, unsigned long end, bool direct,
1113 struct vmem_altmap *altmap)
Wen Congyangae9aae92013-02-22 16:33:04 -08001114{
1115 unsigned long next;
Yasuaki Ishimatsu5255e0a2014-08-22 13:27:31 -07001116 unsigned long addr;
Wen Congyangae9aae92013-02-22 16:33:04 -08001117 pgd_t *pgd;
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +03001118 p4d_t *p4d;
Wen Congyangae9aae92013-02-22 16:33:04 -08001119
Yasuaki Ishimatsu5255e0a2014-08-22 13:27:31 -07001120 for (addr = start; addr < end; addr = next) {
1121 next = pgd_addr_end(addr, end);
Wen Congyangae9aae92013-02-22 16:33:04 -08001122
Yasuaki Ishimatsu5255e0a2014-08-22 13:27:31 -07001123 pgd = pgd_offset_k(addr);
Wen Congyangae9aae92013-02-22 16:33:04 -08001124 if (!pgd_present(*pgd))
1125 continue;
1126
Kirill A. Shutemove6ab9c42017-04-25 12:25:57 +03001127 p4d = p4d_offset(pgd, 0);
Christoph Hellwig24b6d412017-12-29 08:53:56 +01001128 remove_p4d_table(p4d, addr, next, altmap, direct);
Wen Congyangae9aae92013-02-22 16:33:04 -08001129 }
1130
Wen Congyangae9aae92013-02-22 16:33:04 -08001131 flush_tlb_all();
1132}
1133
Christoph Hellwig24b6d412017-12-29 08:53:56 +01001134void __ref vmemmap_free(unsigned long start, unsigned long end,
1135 struct vmem_altmap *altmap)
Tang Chen01975182013-02-22 16:33:08 -08001136{
Christoph Hellwig24b6d412017-12-29 08:53:56 +01001137 remove_pagetable(start, end, false, altmap);
Tang Chen01975182013-02-22 16:33:08 -08001138}
1139
Tang Chen587ff8c2013-04-15 17:46:46 +08001140#ifdef CONFIG_MEMORY_HOTREMOVE
Tang Chenbbcab872013-02-22 16:33:06 -08001141static void __meminit
1142kernel_physical_mapping_remove(unsigned long start, unsigned long end)
1143{
1144 start = (unsigned long)__va(start);
1145 end = (unsigned long)__va(end);
1146
Christoph Hellwig24b6d412017-12-29 08:53:56 +01001147 remove_pagetable(start, end, true, NULL);
Tang Chenbbcab872013-02-22 16:33:06 -08001148}
1149
Christoph Hellwigda024512017-12-29 08:53:55 +01001150int __ref arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap)
Wen Congyang24d335c2013-02-22 16:32:58 -08001151{
1152 unsigned long start_pfn = start >> PAGE_SHIFT;
1153 unsigned long nr_pages = size >> PAGE_SHIFT;
Dan Williams4b94ffd2016-01-15 16:56:22 -08001154 struct page *page = pfn_to_page(start_pfn);
Wen Congyang24d335c2013-02-22 16:32:58 -08001155 struct zone *zone;
1156 int ret;
1157
Dan Williams4b94ffd2016-01-15 16:56:22 -08001158 /* With altmap the first mapped page is offset from @start */
Dan Williams4b94ffd2016-01-15 16:56:22 -08001159 if (altmap)
1160 page += vmem_altmap_offset(altmap);
1161 zone = page_zone(page);
Christoph Hellwigda024512017-12-29 08:53:55 +01001162 ret = __remove_pages(zone, start_pfn, nr_pages, altmap);
Wen Congyang24d335c2013-02-22 16:32:58 -08001163 WARN_ON_ONCE(ret);
Dan Williams4b94ffd2016-01-15 16:56:22 -08001164 kernel_physical_mapping_remove(start, start + size);
Wen Congyang24d335c2013-02-22 16:32:58 -08001165
1166 return ret;
1167}
1168#endif
Keith Mannthey45e0b782006-09-30 23:27:09 -07001169#endif /* CONFIG_MEMORY_HOTPLUG */
1170
KAMEZAWA Hiroyuki81ac3ad2009-09-22 16:45:49 -07001171static struct kcore_list kcore_vsyscall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Yinghai Lu94b43c32012-11-16 19:39:19 -08001173static void __init register_page_bootmem_info(void)
1174{
1175#ifdef CONFIG_NUMA
1176 int i;
1177
1178 for_each_online_node(i)
1179 register_page_bootmem_info_node(NODE_DATA(i));
1180#endif
1181}
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183void __init mem_init(void)
1184{
Jon Mason0dc243a2006-06-26 13:58:11 +02001185 pci_iommu_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Yinghai Lu48ddb152008-01-30 13:32:36 +01001187 /* clear_bss() already clear the empty_zero_page */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Jiang Liubced0e32013-04-29 15:06:53 -07001189 /* this will put all memory onto the freelists */
Jiang Liu0c988532013-07-03 15:03:24 -07001190 free_all_bootmem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 after_bootmem = 1;
1192
Pavel Tatashin353b1e72017-11-15 17:36:14 -08001193 /*
1194 * Must be done after boot memory is put on freelist, because here we
1195 * might set fields in deferred struct pages that have not yet been
1196 * initialized, and free_all_bootmem() initializes all the reserved
1197 * deferred pages for us.
1198 */
1199 register_page_bootmem_info();
1200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 /* Register memory areas for /proc/kcore */
Andy Lutomirskif40c3302014-05-05 12:19:36 -07001202 kclist_add(&kcore_vsyscall, (void *)VSYSCALL_ADDR,
1203 PAGE_SIZE, KCORE_OTHER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Jiang Liu46a84132013-07-03 15:04:19 -07001205 mem_init_print_info(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206}
1207
Suresh Siddha502f6602009-10-28 18:46:56 -08001208int kernel_set_to_readonly;
Steven Rostedt16239632009-02-17 17:57:30 -05001209
1210void set_kernel_text_rw(void)
1211{
Suresh Siddhab9af7c02009-10-14 14:46:55 -07001212 unsigned long start = PFN_ALIGN(_text);
Suresh Siddhae7d23dd2009-10-28 18:46:58 -08001213 unsigned long end = PFN_ALIGN(__stop___ex_table);
Steven Rostedt16239632009-02-17 17:57:30 -05001214
1215 if (!kernel_set_to_readonly)
1216 return;
1217
1218 pr_debug("Set kernel text: %lx - %lx for read write\n",
1219 start, end);
1220
Suresh Siddhae7d23dd2009-10-28 18:46:58 -08001221 /*
1222 * Make the kernel identity mapping for text RW. Kernel text
1223 * mapping will always be RO. Refer to the comment in
1224 * static_protections() in pageattr.c
1225 */
Steven Rostedt16239632009-02-17 17:57:30 -05001226 set_memory_rw(start, (end - start) >> PAGE_SHIFT);
1227}
1228
1229void set_kernel_text_ro(void)
1230{
Suresh Siddhab9af7c02009-10-14 14:46:55 -07001231 unsigned long start = PFN_ALIGN(_text);
Suresh Siddhae7d23dd2009-10-28 18:46:58 -08001232 unsigned long end = PFN_ALIGN(__stop___ex_table);
Steven Rostedt16239632009-02-17 17:57:30 -05001233
1234 if (!kernel_set_to_readonly)
1235 return;
1236
1237 pr_debug("Set kernel text: %lx - %lx for read only\n",
1238 start, end);
1239
Suresh Siddhae7d23dd2009-10-28 18:46:58 -08001240 /*
1241 * Set the kernel identity mapping for text RO.
1242 */
Steven Rostedt16239632009-02-17 17:57:30 -05001243 set_memory_ro(start, (end - start) >> PAGE_SHIFT);
1244}
1245
Arjan van de Ven67df1972006-01-06 00:12:04 -08001246void mark_rodata_ro(void)
1247{
Suresh Siddha74e08172009-10-14 14:46:56 -07001248 unsigned long start = PFN_ALIGN(_text);
Alexander Duyckfc8d7822012-11-16 13:57:13 -08001249 unsigned long rodata_start = PFN_ALIGN(__start_rodata);
Suresh Siddha74e08172009-10-14 14:46:56 -07001250 unsigned long end = (unsigned long) &__end_rodata_hpage_align;
Alexander Duyckfc8d7822012-11-16 13:57:13 -08001251 unsigned long text_end = PFN_ALIGN(&__stop___ex_table);
1252 unsigned long rodata_end = PFN_ALIGN(&__end_rodata);
Kees Cook45e2a9d2014-11-14 11:47:37 -08001253 unsigned long all_end;
Steven Rostedt8f0f9962008-05-12 21:20:56 +02001254
Jan Beulich6fb14752007-05-02 19:27:10 +02001255 printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
Linus Torvaldse3ebadd2007-05-07 08:44:24 -07001256 (end - start) >> 10);
Arjan van de Ven984bb802008-02-06 22:39:45 +01001257 set_memory_ro(start, (end - start) >> PAGE_SHIFT);
1258
Steven Rostedt16239632009-02-17 17:57:30 -05001259 kernel_set_to_readonly = 1;
1260
Arjan van de Ven984bb802008-02-06 22:39:45 +01001261 /*
Yinghai Lu72212672013-01-24 12:20:13 -08001262 * The rodata/data/bss/brk section (but not the kernel text!)
1263 * should also be not-executable.
Kees Cook45e2a9d2014-11-14 11:47:37 -08001264 *
1265 * We align all_end to PMD_SIZE because the existing mapping
1266 * is a full PMD. If we would align _brk_end to PAGE_SIZE we
1267 * split the PMD and the reminder between _brk_end and the end
1268 * of the PMD will remain mapped executable.
1269 *
1270 * Any PMD which was setup after the one which covers _brk_end
1271 * has been zapped already via cleanup_highmem().
Arjan van de Ven984bb802008-02-06 22:39:45 +01001272 */
Kees Cook45e2a9d2014-11-14 11:47:37 -08001273 all_end = roundup((unsigned long)_brk_end, PMD_SIZE);
Stephen Smalleyab76f7b2015-10-01 09:04:22 -04001274 set_memory_nx(text_end, (all_end - text_end) >> PAGE_SHIFT);
Arjan van de Ven67df1972006-01-06 00:12:04 -08001275
Andi Kleen0c42f392008-01-30 13:33:42 +01001276#ifdef CONFIG_CPA_DEBUG
Ingo Molnar10f22dd2008-01-30 13:34:10 +01001277 printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end);
Arjan van de Ven6d238cc2008-01-30 13:34:06 +01001278 set_memory_rw(start, (end-start) >> PAGE_SHIFT);
Andi Kleen0c42f392008-01-30 13:33:42 +01001279
Ingo Molnar10f22dd2008-01-30 13:34:10 +01001280 printk(KERN_INFO "Testing CPA: again\n");
Arjan van de Ven6d238cc2008-01-30 13:34:06 +01001281 set_memory_ro(start, (end-start) >> PAGE_SHIFT);
Andi Kleen0c42f392008-01-30 13:33:42 +01001282#endif
Suresh Siddha74e08172009-10-14 14:46:56 -07001283
Jiang Liuc88442e2013-07-03 15:02:58 -07001284 free_init_pages("unused kernel",
Alexander Duyckfc8d7822012-11-16 13:57:13 -08001285 (unsigned long) __va(__pa_symbol(text_end)),
1286 (unsigned long) __va(__pa_symbol(rodata_start)));
Jiang Liuc88442e2013-07-03 15:02:58 -07001287 free_init_pages("unused kernel",
Alexander Duyckfc8d7822012-11-16 13:57:13 -08001288 (unsigned long) __va(__pa_symbol(rodata_end)),
1289 (unsigned long) __va(__pa_symbol(_sdata)));
Stephen Smalleye1a58322015-10-05 12:55:20 -04001290
1291 debug_checkwx();
Arjan van de Ven67df1972006-01-06 00:12:04 -08001292}
Mathieu Desnoyers4e4eee02008-02-02 15:42:20 -05001293
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001294int kern_addr_valid(unsigned long addr)
1295{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001297 pgd_t *pgd;
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +03001298 p4d_t *p4d;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001299 pud_t *pud;
1300 pmd_t *pmd;
1301 pte_t *pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 if (above != 0 && above != -1UL)
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001304 return 0;
1305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 pgd = pgd_offset_k(addr);
1307 if (pgd_none(*pgd))
1308 return 0;
1309
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +03001310 p4d = p4d_offset(pgd, addr);
1311 if (p4d_none(*p4d))
1312 return 0;
1313
1314 pud = pud_offset(p4d, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 if (pud_none(*pud))
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001316 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Mel Gorman0ee364e2013-02-11 14:52:36 +00001318 if (pud_large(*pud))
1319 return pfn_valid(pud_pfn(*pud));
1320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 pmd = pmd_offset(pud, addr);
1322 if (pmd_none(*pmd))
1323 return 0;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 if (pmd_large(*pmd))
1326 return pfn_valid(pmd_pfn(*pmd));
1327
1328 pte = pte_offset_kernel(pmd, addr);
1329 if (pte_none(*pte))
1330 return 0;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 return pfn_valid(pte_pfn(*pte));
1333}
1334
Yinghai Lu982792c2014-06-04 16:06:32 -07001335static unsigned long probe_memory_block_size(void)
Nathan Fontenot1dc41aa2011-01-20 10:46:15 -06001336{
Seth Jennings43c75f92015-11-30 10:47:43 -06001337 unsigned long bz = MIN_MEMORY_BLOCK_SIZE;
Yinghai Lu982792c2014-06-04 16:06:32 -07001338
Seth Jennings43c75f92015-11-30 10:47:43 -06001339 /* if system is UV or has 64GB of RAM or more, use large blocks */
1340 if (is_uv_system() || ((max_pfn << PAGE_SHIFT) >= (64UL << 30)))
1341 bz = 2UL << 30; /* 2GB */
Nathan Fontenot1dc41aa2011-01-20 10:46:15 -06001342
Seth Jennings43c75f92015-11-30 10:47:43 -06001343 pr_info("x86/mm: Memory block size: %ldMB\n", bz >> 20);
Yinghai Lu982792c2014-06-04 16:06:32 -07001344
1345 return bz;
1346}
1347
1348static unsigned long memory_block_size_probed;
1349unsigned long memory_block_size_bytes(void)
1350{
1351 if (!memory_block_size_probed)
1352 memory_block_size_probed = probe_memory_block_size();
1353
1354 return memory_block_size_probed;
1355}
1356
Christoph Lameter0889eba2007-10-16 01:24:15 -07001357#ifdef CONFIG_SPARSEMEM_VMEMMAP
1358/*
1359 * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
1360 */
Yinghai Luc2b91e22008-04-12 01:19:24 -07001361static long __meminitdata addr_start, addr_end;
1362static void __meminitdata *p_start, *p_end;
1363static int __meminitdata node_start;
1364
Johannes Weinere8216da2013-04-29 15:07:54 -07001365static int __meminit vmemmap_populate_hugepages(unsigned long start,
Dan Williams4b94ffd2016-01-15 16:56:22 -08001366 unsigned long end, int node, struct vmem_altmap *altmap)
Christoph Lameter0889eba2007-10-16 01:24:15 -07001367{
Johannes Weiner0aad8182013-04-29 15:07:50 -07001368 unsigned long addr;
Christoph Lameter0889eba2007-10-16 01:24:15 -07001369 unsigned long next;
1370 pgd_t *pgd;
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +03001371 p4d_t *p4d;
Christoph Lameter0889eba2007-10-16 01:24:15 -07001372 pud_t *pud;
1373 pmd_t *pmd;
1374
Johannes Weiner0aad8182013-04-29 15:07:50 -07001375 for (addr = start; addr < end; addr = next) {
Johannes Weinere8216da2013-04-29 15:07:54 -07001376 next = pmd_addr_end(addr, end);
Christoph Lameter0889eba2007-10-16 01:24:15 -07001377
1378 pgd = vmemmap_pgd_populate(addr, node);
1379 if (!pgd)
1380 return -ENOMEM;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001381
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +03001382 p4d = vmemmap_p4d_populate(pgd, addr, node);
1383 if (!p4d)
1384 return -ENOMEM;
1385
1386 pud = vmemmap_pud_populate(p4d, addr, node);
Christoph Lameter0889eba2007-10-16 01:24:15 -07001387 if (!pud)
1388 return -ENOMEM;
1389
Johannes Weinere8216da2013-04-29 15:07:54 -07001390 pmd = pmd_offset(pud, addr);
1391 if (pmd_none(*pmd)) {
Johannes Weinere8216da2013-04-29 15:07:54 -07001392 void *p;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001393
Christoph Hellwiga8fc3572017-12-29 08:53:58 +01001394 if (altmap)
1395 p = altmap_alloc_block_buf(PMD_SIZE, altmap);
1396 else
1397 p = vmemmap_alloc_block_buf(PMD_SIZE, node);
Johannes Weiner8e2cdbc2013-04-29 15:07:56 -07001398 if (p) {
1399 pte_t entry;
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -04001400
Johannes Weiner8e2cdbc2013-04-29 15:07:56 -07001401 entry = pfn_pte(__pa(p) >> PAGE_SHIFT,
1402 PAGE_KERNEL_LARGE);
1403 set_pmd(pmd, __pmd(pte_val(entry)));
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -04001404
Johannes Weiner8e2cdbc2013-04-29 15:07:56 -07001405 /* check to see if we have contiguous blocks */
1406 if (p_end != p || node_start != node) {
1407 if (p_start)
Dan Williamsc9cdaeb2015-09-17 16:27:57 -04001408 pr_debug(" [%lx-%lx] PMD -> [%p-%p] on node %d\n",
Johannes Weiner8e2cdbc2013-04-29 15:07:56 -07001409 addr_start, addr_end-1, p_start, p_end-1, node_start);
1410 addr_start = addr;
1411 node_start = node;
1412 p_start = p;
1413 }
1414
1415 addr_end = addr + PMD_SIZE;
1416 p_end = p + PMD_SIZE;
1417 continue;
Dan Williams4b94ffd2016-01-15 16:56:22 -08001418 } else if (altmap)
1419 return -ENOMEM; /* no fallback */
Johannes Weiner8e2cdbc2013-04-29 15:07:56 -07001420 } else if (pmd_large(*pmd)) {
Johannes Weinere8216da2013-04-29 15:07:54 -07001421 vmemmap_verify((pte_t *)pmd, node, addr, next);
Johannes Weiner8e2cdbc2013-04-29 15:07:56 -07001422 continue;
1423 }
Johannes Weiner8e2cdbc2013-04-29 15:07:56 -07001424 if (vmemmap_populate_basepages(addr, next, node))
1425 return -ENOMEM;
Christoph Lameter0889eba2007-10-16 01:24:15 -07001426 }
Christoph Lameter0889eba2007-10-16 01:24:15 -07001427 return 0;
1428}
Yinghai Luc2b91e22008-04-12 01:19:24 -07001429
Christoph Hellwig7b73d972017-12-29 08:53:54 +01001430int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
1431 struct vmem_altmap *altmap)
Johannes Weinere8216da2013-04-29 15:07:54 -07001432{
1433 int err;
1434
Borislav Petkov16bf9222016-03-29 17:42:03 +02001435 if (boot_cpu_has(X86_FEATURE_PSE))
Dan Williams4b94ffd2016-01-15 16:56:22 -08001436 err = vmemmap_populate_hugepages(start, end, node, altmap);
1437 else if (altmap) {
1438 pr_err_once("%s: no cpu support for altmap allocations\n",
1439 __func__);
1440 err = -ENOMEM;
1441 } else
Johannes Weinere8216da2013-04-29 15:07:54 -07001442 err = vmemmap_populate_basepages(start, end, node);
1443 if (!err)
Kirill A. Shutemov5372e152016-12-15 02:44:03 +03001444 sync_global_pgds(start, end - 1);
Johannes Weinere8216da2013-04-29 15:07:54 -07001445 return err;
1446}
1447
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -08001448#if defined(CONFIG_MEMORY_HOTPLUG_SPARSE) && defined(CONFIG_HAVE_BOOTMEM_INFO_NODE)
1449void register_page_bootmem_memmap(unsigned long section_nr,
Baoquan He15670bf2017-10-28 09:30:38 +08001450 struct page *start_page, unsigned long nr_pages)
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -08001451{
1452 unsigned long addr = (unsigned long)start_page;
Baoquan He15670bf2017-10-28 09:30:38 +08001453 unsigned long end = (unsigned long)(start_page + nr_pages);
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -08001454 unsigned long next;
1455 pgd_t *pgd;
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +03001456 p4d_t *p4d;
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -08001457 pud_t *pud;
1458 pmd_t *pmd;
Baoquan He15670bf2017-10-28 09:30:38 +08001459 unsigned int nr_pmd_pages;
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -08001460 struct page *page;
1461
1462 for (; addr < end; addr = next) {
1463 pte_t *pte = NULL;
1464
1465 pgd = pgd_offset_k(addr);
1466 if (pgd_none(*pgd)) {
1467 next = (addr + PAGE_SIZE) & PAGE_MASK;
1468 continue;
1469 }
1470 get_page_bootmem(section_nr, pgd_page(*pgd), MIX_SECTION_INFO);
1471
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +03001472 p4d = p4d_offset(pgd, addr);
1473 if (p4d_none(*p4d)) {
1474 next = (addr + PAGE_SIZE) & PAGE_MASK;
1475 continue;
1476 }
1477 get_page_bootmem(section_nr, p4d_page(*p4d), MIX_SECTION_INFO);
1478
1479 pud = pud_offset(p4d, addr);
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -08001480 if (pud_none(*pud)) {
1481 next = (addr + PAGE_SIZE) & PAGE_MASK;
1482 continue;
1483 }
1484 get_page_bootmem(section_nr, pud_page(*pud), MIX_SECTION_INFO);
1485
Borislav Petkov16bf9222016-03-29 17:42:03 +02001486 if (!boot_cpu_has(X86_FEATURE_PSE)) {
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -08001487 next = (addr + PAGE_SIZE) & PAGE_MASK;
1488 pmd = pmd_offset(pud, addr);
1489 if (pmd_none(*pmd))
1490 continue;
1491 get_page_bootmem(section_nr, pmd_page(*pmd),
1492 MIX_SECTION_INFO);
1493
1494 pte = pte_offset_kernel(pmd, addr);
1495 if (pte_none(*pte))
1496 continue;
1497 get_page_bootmem(section_nr, pte_page(*pte),
1498 SECTION_INFO);
1499 } else {
1500 next = pmd_addr_end(addr, end);
1501
1502 pmd = pmd_offset(pud, addr);
1503 if (pmd_none(*pmd))
1504 continue;
1505
Baoquan He15670bf2017-10-28 09:30:38 +08001506 nr_pmd_pages = 1 << get_order(PMD_SIZE);
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -08001507 page = pmd_page(*pmd);
Baoquan He15670bf2017-10-28 09:30:38 +08001508 while (nr_pmd_pages--)
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -08001509 get_page_bootmem(section_nr, page++,
1510 SECTION_INFO);
1511 }
1512 }
1513}
1514#endif
1515
Yinghai Luc2b91e22008-04-12 01:19:24 -07001516void __meminit vmemmap_populate_print_last(void)
1517{
1518 if (p_start) {
Dan Williamsc9cdaeb2015-09-17 16:27:57 -04001519 pr_debug(" [%lx-%lx] PMD -> [%p-%p] on node %d\n",
Yinghai Luc2b91e22008-04-12 01:19:24 -07001520 addr_start, addr_end-1, p_start, p_end-1, node_start);
1521 p_start = NULL;
1522 p_end = NULL;
1523 node_start = 0;
1524 }
1525}
Christoph Lameter0889eba2007-10-16 01:24:15 -07001526#endif