blob: a688617c727e1ec3558e158156c98b4632b0d9a2 [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
Dave Hansen8a57f482018-04-06 13:55:06 -070068/* Bits supported by the hardware: */
David Vrabelf9553712014-01-07 17:03:06 +000069pteval_t __supported_pte_mask __read_mostly = ~0;
Dave Hansen8a57f482018-04-06 13:55:06 -070070/* Bits allowed in normal kernel mappings: */
71pteval_t __default_kernel_pte_mask __read_mostly = ~0;
Yinghai Lubd220a22008-09-05 00:58:28 -070072EXPORT_SYMBOL_GPL(__supported_pte_mask);
Dave Hansen8a57f482018-04-06 13:55:06 -070073/* Used in PAGE_KERNEL_* macros which are reasonably used out-of-tree: */
74EXPORT_SYMBOL(__default_kernel_pte_mask);
Yinghai Lubd220a22008-09-05 00:58:28 -070075
Yinghai Lubd220a22008-09-05 00:58:28 -070076int force_personality32;
77
Ingo Molnardeed05b2008-09-05 10:23:26 +020078/*
79 * noexec32=on|off
80 * Control non executable heap for 32bit processes.
81 * To control the stack too use noexec=off
82 *
83 * on PROT_READ does not imply PROT_EXEC for 32-bit processes (default)
84 * off PROT_READ implies PROT_EXEC
85 */
Yinghai Lubd220a22008-09-05 00:58:28 -070086static int __init nonx32_setup(char *str)
87{
88 if (!strcmp(str, "on"))
89 force_personality32 &= ~READ_IMPLIES_EXEC;
90 else if (!strcmp(str, "off"))
91 force_personality32 |= READ_IMPLIES_EXEC;
92 return 1;
93}
94__setup("noexec32=", nonx32_setup);
95
Kirill A. Shutemov91f606a2018-02-14 21:25:41 +030096static void sync_global_pgds_l5(unsigned long start, unsigned long end)
Kirill A. Shutemov141efad2017-06-06 14:31:29 +030097{
98 unsigned long addr;
99
100 for (addr = start; addr <= end; addr = ALIGN(addr + 1, PGDIR_SIZE)) {
101 const pgd_t *pgd_ref = pgd_offset_k(addr);
102 struct page *page;
103
104 /* Check for overflow */
105 if (addr < start)
106 break;
107
108 if (pgd_none(*pgd_ref))
109 continue;
110
111 spin_lock(&pgd_lock);
112 list_for_each_entry(page, &pgd_list, lru) {
113 pgd_t *pgd;
114 spinlock_t *pgt_lock;
115
116 pgd = (pgd_t *)page_address(page) + pgd_index(addr);
117 /* the pgt_lock only for Xen */
118 pgt_lock = &pgd_page_get_mm(page)->page_table_lock;
119 spin_lock(pgt_lock);
120
121 if (!pgd_none(*pgd_ref) && !pgd_none(*pgd))
122 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
123
124 if (pgd_none(*pgd))
125 set_pgd(pgd, *pgd_ref);
126
127 spin_unlock(pgt_lock);
128 }
129 spin_unlock(&pgd_lock);
130 }
131}
Kirill A. Shutemov91f606a2018-02-14 21:25:41 +0300132
133static void sync_global_pgds_l4(unsigned long start, unsigned long end)
Haicheng Li6afb5152010-05-19 17:42:14 +0800134{
Baoquan Hefc5f9d52017-05-04 10:25:47 +0800135 unsigned long addr;
Haicheng Li6afb5152010-05-19 17:42:14 +0800136
Baoquan Hefc5f9d52017-05-04 10:25:47 +0800137 for (addr = start; addr <= end; addr = ALIGN(addr + 1, PGDIR_SIZE)) {
138 pgd_t *pgd_ref = pgd_offset_k(addr);
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300139 const p4d_t *p4d_ref;
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700140 struct page *page;
Haicheng Li6afb5152010-05-19 17:42:14 +0800141
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300142 /*
143 * With folded p4d, pgd_none() is always false, we need to
144 * handle synchonization on p4d level.
145 */
Kirill A. Shutemovc65e7742018-02-14 14:16:53 +0300146 MAYBE_BUILD_BUG_ON(pgd_none(*pgd_ref));
Baoquan Hefc5f9d52017-05-04 10:25:47 +0800147 p4d_ref = p4d_offset(pgd_ref, addr);
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300148
149 if (p4d_none(*p4d_ref))
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700150 continue;
Haicheng Li6afb5152010-05-19 17:42:14 +0800151
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800152 spin_lock(&pgd_lock);
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700153 list_for_each_entry(page, &pgd_list, lru) {
Linus Torvaldsbe354f42012-12-15 12:29:54 -0800154 pgd_t *pgd;
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300155 p4d_t *p4d;
Jeremy Fitzhardinge617d34d2010-09-21 12:01:51 -0700156 spinlock_t *pgt_lock;
157
Baoquan Hefc5f9d52017-05-04 10:25:47 +0800158 pgd = (pgd_t *)page_address(page) + pgd_index(addr);
159 p4d = p4d_offset(pgd, addr);
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800160 /* the pgt_lock only for Xen */
Jeremy Fitzhardinge617d34d2010-09-21 12:01:51 -0700161 pgt_lock = &pgd_page_get_mm(page)->page_table_lock;
162 spin_lock(pgt_lock);
163
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300164 if (!p4d_none(*p4d_ref) && !p4d_none(*p4d))
165 BUG_ON(p4d_page_vaddr(*p4d)
166 != p4d_page_vaddr(*p4d_ref));
Jeremy Fitzhardinge617d34d2010-09-21 12:01:51 -0700167
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300168 if (p4d_none(*p4d))
169 set_p4d(p4d, *p4d_ref);
Yasuaki Ishimatsu9661d5b2014-08-22 13:27:34 -0700170
Jeremy Fitzhardinge617d34d2010-09-21 12:01:51 -0700171 spin_unlock(pgt_lock);
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700172 }
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800173 spin_unlock(&pgd_lock);
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700174 }
Haicheng Li6afb5152010-05-19 17:42:14 +0800175}
Kirill A. Shutemov91f606a2018-02-14 21:25:41 +0300176
177/*
178 * When memory was added make sure all the processes MM have
179 * suitable PGD entries in the local PGD level page.
180 */
181void sync_global_pgds(unsigned long start, unsigned long end)
182{
Kirill A. Shutemoved7588d2018-05-18 13:35:24 +0300183 if (pgtable_l5_enabled())
Kirill A. Shutemov91f606a2018-02-14 21:25:41 +0300184 sync_global_pgds_l5(start, end);
185 else
186 sync_global_pgds_l4(start, end);
187}
Haicheng Li6afb5152010-05-19 17:42:14 +0800188
189/*
Marcin Slusarz8d6ea9672008-08-15 18:32:24 +0200190 * NOTE: This function is marked __ref because it calls __init function
191 * (alloc_bootmem_pages). It's safe to do it ONLY when after_bootmem == 0.
192 */
193static __ref void *spp_getpage(void)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100194{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 void *ptr;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (after_bootmem)
Levin, Alexander (Sasha Levin)75f296d2017-11-15 17:35:54 -0800198 ptr = (void *) get_zeroed_page(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 else
200 ptr = alloc_bootmem_pages(PAGE_SIZE);
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100201
202 if (!ptr || ((unsigned long)ptr & ~PAGE_MASK)) {
203 panic("set_pte_phys: cannot allocate page data %s\n",
204 after_bootmem ? "after bootmem" : "");
205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Ingo Molnar10f22dd2008-01-30 13:34:10 +0100207 pr_debug("spp_getpage %p\n", ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100209 return ptr;
210}
211
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300212static p4d_t *fill_p4d(pgd_t *pgd, unsigned long vaddr)
Tejun Heo458a3e62009-02-24 11:57:21 +0900213{
214 if (pgd_none(*pgd)) {
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300215 p4d_t *p4d = (p4d_t *)spp_getpage();
216 pgd_populate(&init_mm, pgd, p4d);
217 if (p4d != p4d_offset(pgd, 0))
Tejun Heo458a3e62009-02-24 11:57:21 +0900218 printk(KERN_ERR "PAGETABLE BUG #00! %p <-> %p\n",
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300219 p4d, p4d_offset(pgd, 0));
Tejun Heo458a3e62009-02-24 11:57:21 +0900220 }
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300221 return p4d_offset(pgd, vaddr);
222}
223
224static pud_t *fill_pud(p4d_t *p4d, unsigned long vaddr)
225{
226 if (p4d_none(*p4d)) {
227 pud_t *pud = (pud_t *)spp_getpage();
228 p4d_populate(&init_mm, p4d, pud);
229 if (pud != pud_offset(p4d, 0))
230 printk(KERN_ERR "PAGETABLE BUG #01! %p <-> %p\n",
231 pud, pud_offset(p4d, 0));
232 }
233 return pud_offset(p4d, vaddr);
Tejun Heo458a3e62009-02-24 11:57:21 +0900234}
235
Jeremy Fitzhardingef254f392009-03-03 12:02:57 -0800236static pmd_t *fill_pmd(pud_t *pud, unsigned long vaddr)
Tejun Heo458a3e62009-02-24 11:57:21 +0900237{
238 if (pud_none(*pud)) {
239 pmd_t *pmd = (pmd_t *) spp_getpage();
240 pud_populate(&init_mm, pud, pmd);
241 if (pmd != pmd_offset(pud, 0))
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300242 printk(KERN_ERR "PAGETABLE BUG #02! %p <-> %p\n",
Tejun Heo458a3e62009-02-24 11:57:21 +0900243 pmd, pmd_offset(pud, 0));
244 }
245 return pmd_offset(pud, vaddr);
246}
247
Jeremy Fitzhardingef254f392009-03-03 12:02:57 -0800248static pte_t *fill_pte(pmd_t *pmd, unsigned long vaddr)
Tejun Heo458a3e62009-02-24 11:57:21 +0900249{
250 if (pmd_none(*pmd)) {
251 pte_t *pte = (pte_t *) spp_getpage();
252 pmd_populate_kernel(&init_mm, pmd, pte);
253 if (pte != pte_offset_kernel(pmd, 0))
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300254 printk(KERN_ERR "PAGETABLE BUG #03!\n");
Tejun Heo458a3e62009-02-24 11:57:21 +0900255 }
256 return pte_offset_kernel(pmd, vaddr);
257}
258
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300259static void __set_pte_vaddr(pud_t *pud, unsigned long vaddr, pte_t new_pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300261 pmd_t *pmd = fill_pmd(pud, vaddr);
262 pte_t *pte = fill_pte(pmd, vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 set_pte(pte, new_pte);
265
266 /*
267 * It's enough to flush this one mapping.
268 * (PGE mappings get flushed as well)
269 */
Andy Lutomirski1299ef12018-01-31 08:03:10 -0800270 __flush_tlb_one_kernel(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300273void set_pte_vaddr_p4d(p4d_t *p4d_page, unsigned long vaddr, pte_t new_pte)
274{
275 p4d_t *p4d = p4d_page + p4d_index(vaddr);
276 pud_t *pud = fill_pud(p4d, vaddr);
277
278 __set_pte_vaddr(pud, vaddr, new_pte);
279}
280
281void set_pte_vaddr_pud(pud_t *pud_page, unsigned long vaddr, pte_t new_pte)
282{
283 pud_t *pud = pud_page + pud_index(vaddr);
284
285 __set_pte_vaddr(pud, vaddr, new_pte);
286}
287
Tejun Heo458a3e62009-02-24 11:57:21 +0900288void set_pte_vaddr(unsigned long vaddr, pte_t pteval)
Eduardo Habkost0814e0b2008-06-25 00:19:22 -0400289{
290 pgd_t *pgd;
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300291 p4d_t *p4d_page;
Eduardo Habkost0814e0b2008-06-25 00:19:22 -0400292
293 pr_debug("set_pte_vaddr %lx to %lx\n", vaddr, native_pte_val(pteval));
294
295 pgd = pgd_offset_k(vaddr);
296 if (pgd_none(*pgd)) {
297 printk(KERN_ERR
298 "PGD FIXMAP MISSING, it should be setup in head.S!\n");
299 return;
300 }
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300301
302 p4d_page = p4d_offset(pgd, 0);
303 set_pte_vaddr_p4d(p4d_page, vaddr, pteval);
Eduardo Habkost0814e0b2008-06-25 00:19:22 -0400304}
305
Tejun Heo458a3e62009-02-24 11:57:21 +0900306pmd_t * __init populate_extra_pmd(unsigned long vaddr)
Tejun Heo11124412009-02-20 16:29:09 +0900307{
308 pgd_t *pgd;
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300309 p4d_t *p4d;
Tejun Heo11124412009-02-20 16:29:09 +0900310 pud_t *pud;
311
312 pgd = pgd_offset_k(vaddr);
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300313 p4d = fill_p4d(pgd, vaddr);
314 pud = fill_pud(p4d, vaddr);
Tejun Heo458a3e62009-02-24 11:57:21 +0900315 return fill_pmd(pud, vaddr);
316}
Tejun Heo11124412009-02-20 16:29:09 +0900317
Tejun Heo458a3e62009-02-24 11:57:21 +0900318pte_t * __init populate_extra_pte(unsigned long vaddr)
319{
320 pmd_t *pmd;
321
322 pmd = populate_extra_pmd(vaddr);
323 return fill_pte(pmd, vaddr);
Tejun Heo11124412009-02-20 16:29:09 +0900324}
325
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100326/*
Jack Steiner3a9e1892008-07-01 14:45:32 -0500327 * Create large page table mappings for a range of physical addresses.
328 */
329static void __init __init_extra_mapping(unsigned long phys, unsigned long size,
Juergen Gross2df58b62014-11-03 14:01:52 +0100330 enum page_cache_mode cache)
Jack Steiner3a9e1892008-07-01 14:45:32 -0500331{
332 pgd_t *pgd;
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300333 p4d_t *p4d;
Jack Steiner3a9e1892008-07-01 14:45:32 -0500334 pud_t *pud;
335 pmd_t *pmd;
Juergen Gross2df58b62014-11-03 14:01:52 +0100336 pgprot_t prot;
Jack Steiner3a9e1892008-07-01 14:45:32 -0500337
Juergen Gross2df58b62014-11-03 14:01:52 +0100338 pgprot_val(prot) = pgprot_val(PAGE_KERNEL_LARGE) |
339 pgprot_val(pgprot_4k_2_large(cachemode2pgprot(cache)));
Jack Steiner3a9e1892008-07-01 14:45:32 -0500340 BUG_ON((phys & ~PMD_MASK) || (size & ~PMD_MASK));
341 for (; size; phys += PMD_SIZE, size -= PMD_SIZE) {
342 pgd = pgd_offset_k((unsigned long)__va(phys));
343 if (pgd_none(*pgd)) {
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300344 p4d = (p4d_t *) spp_getpage();
345 set_pgd(pgd, __pgd(__pa(p4d) | _KERNPG_TABLE |
Jack Steiner3a9e1892008-07-01 14:45:32 -0500346 _PAGE_USER));
347 }
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300348 p4d = p4d_offset(pgd, (unsigned long)__va(phys));
349 if (p4d_none(*p4d)) {
350 pud = (pud_t *) spp_getpage();
351 set_p4d(p4d, __p4d(__pa(pud) | _KERNPG_TABLE |
352 _PAGE_USER));
353 }
354 pud = pud_offset(p4d, (unsigned long)__va(phys));
Jack Steiner3a9e1892008-07-01 14:45:32 -0500355 if (pud_none(*pud)) {
356 pmd = (pmd_t *) spp_getpage();
357 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE |
358 _PAGE_USER));
359 }
360 pmd = pmd_offset(pud, phys);
361 BUG_ON(!pmd_none(*pmd));
362 set_pmd(pmd, __pmd(phys | pgprot_val(prot)));
363 }
364}
365
366void __init init_extra_mapping_wb(unsigned long phys, unsigned long size)
367{
Juergen Gross2df58b62014-11-03 14:01:52 +0100368 __init_extra_mapping(phys, size, _PAGE_CACHE_MODE_WB);
Jack Steiner3a9e1892008-07-01 14:45:32 -0500369}
370
371void __init init_extra_mapping_uc(unsigned long phys, unsigned long size)
372{
Juergen Gross2df58b62014-11-03 14:01:52 +0100373 __init_extra_mapping(phys, size, _PAGE_CACHE_MODE_UC);
Jack Steiner3a9e1892008-07-01 14:45:32 -0500374}
375
376/*
Ingo Molnar88f3aec2008-02-21 11:04:11 +0100377 * The head.S code sets up the kernel high mapping:
378 *
379 * from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text)
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100380 *
Zhang Yanfei1e3b3082013-05-15 10:58:07 +0800381 * phys_base holds the negative offset to the kernel, which is added
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100382 * to the compile time generated pmds. This results in invalid pmds up
383 * to the point where we hit the physaddr 0 mapping.
384 *
Yinghai Lue5f15b42011-02-18 11:30:30 +0000385 * We limit the mappings to the region from _text to _brk_end. _brk_end
386 * is rounded up to the 2MB boundary. This catches the invalid pmds as
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100387 * well, as they are located before _text:
388 */
389void __init cleanup_highmap(void)
390{
391 unsigned long vaddr = __START_KERNEL_map;
Yinghai Lu10054232013-01-24 12:19:54 -0800392 unsigned long vaddr_end = __START_KERNEL_map + KERNEL_IMAGE_SIZE;
Yinghai Lue5f15b42011-02-18 11:30:30 +0000393 unsigned long end = roundup((unsigned long)_brk_end, PMD_SIZE) - 1;
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100394 pmd_t *pmd = level2_kernel_pgt;
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100395
Yinghai Lu10054232013-01-24 12:19:54 -0800396 /*
397 * Native path, max_pfn_mapped is not set yet.
398 * Xen has valid max_pfn_mapped set in
399 * arch/x86/xen/mmu.c:xen_setup_kernel_pagetable().
400 */
401 if (max_pfn_mapped)
402 vaddr_end = __START_KERNEL_map + (max_pfn_mapped << PAGE_SHIFT);
403
Yinghai Lue5f15b42011-02-18 11:30:30 +0000404 for (; vaddr + PMD_SIZE - 1 < vaddr_end; pmd++, vaddr += PMD_SIZE) {
Hugh Dickins2884f112008-05-28 19:36:07 +0100405 if (pmd_none(*pmd))
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100406 continue;
407 if (vaddr < (unsigned long) _text || vaddr > end)
408 set_pmd(pmd, __pmd(0));
409 }
410}
411
Thomas Garnier59b3d022016-06-21 17:46:59 -0700412/*
413 * Create PTE level page table mapping for physical addresses.
414 * It returns the last physical address mapped.
415 */
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700416static unsigned long __meminit
Thomas Garnier59b3d022016-06-21 17:46:59 -0700417phys_pte_init(pte_t *pte_page, unsigned long paddr, unsigned long paddr_end,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700418 pgprot_t prot)
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400419{
Thomas Garnier59b3d022016-06-21 17:46:59 -0700420 unsigned long pages = 0, paddr_next;
421 unsigned long paddr_last = paddr_end;
422 pte_t *pte;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400423 int i;
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700424
Thomas Garnier59b3d022016-06-21 17:46:59 -0700425 pte = pte_page + pte_index(paddr);
426 i = pte_index(paddr);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400427
Thomas Garnier59b3d022016-06-21 17:46:59 -0700428 for (; i < PTRS_PER_PTE; i++, paddr = paddr_next, pte++) {
429 paddr_next = (paddr & PAGE_MASK) + PAGE_SIZE;
430 if (paddr >= paddr_end) {
Yinghai Lueceb3632012-11-16 19:38:56 -0800431 if (!after_bootmem &&
Ingo Molnar3bce64f2017-01-28 14:14:25 +0100432 !e820__mapped_any(paddr & PAGE_MASK, paddr_next,
Ingo Molnar09821ff2017-01-28 17:09:33 +0100433 E820_TYPE_RAM) &&
Ingo Molnar3bce64f2017-01-28 14:14:25 +0100434 !e820__mapped_any(paddr & PAGE_MASK, paddr_next,
Ingo Molnar09821ff2017-01-28 17:09:33 +0100435 E820_TYPE_RESERVED_KERN))
Yinghai Lueceb3632012-11-16 19:38:56 -0800436 set_pte(pte, __pte(0));
437 continue;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400438 }
439
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700440 /*
441 * We will re-use the existing mapping.
442 * Xen for example has some special requirements, like mapping
443 * pagetable pages as RO. So assume someone who pre-setup
444 * these mappings are more intelligent.
445 */
Dave Hansendcb32d92016-07-07 17:19:15 -0700446 if (!pte_none(*pte)) {
Jan Beulich876ee612012-10-04 14:48:10 +0100447 if (!after_bootmem)
448 pages++;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400449 continue;
Yinghai Lu3afa3942008-10-25 22:58:21 -0700450 }
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400451
452 if (0)
Thomas Garnier59b3d022016-06-21 17:46:59 -0700453 pr_info(" pte=%p addr=%lx pte=%016lx\n", pte, paddr,
454 pfn_pte(paddr >> PAGE_SHIFT, PAGE_KERNEL).pte);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400455 pages++;
Thomas Garnier59b3d022016-06-21 17:46:59 -0700456 set_pte(pte, pfn_pte(paddr >> PAGE_SHIFT, prot));
457 paddr_last = (paddr & PAGE_MASK) + PAGE_SIZE;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400458 }
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700459
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400460 update_page_count(PG_LEVEL_4K, pages);
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700461
Thomas Garnier59b3d022016-06-21 17:46:59 -0700462 return paddr_last;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400463}
464
Thomas Garnier59b3d022016-06-21 17:46:59 -0700465/*
466 * Create PMD level page table mapping for physical addresses. The virtual
467 * and physical address have to be aligned at this level.
468 * It returns the last physical address mapped.
469 */
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700470static unsigned long __meminit
Thomas Garnier59b3d022016-06-21 17:46:59 -0700471phys_pmd_init(pmd_t *pmd_page, unsigned long paddr, unsigned long paddr_end,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700472 unsigned long page_size_mask, pgprot_t prot)
Matt Tolentino44df75e2006-01-17 07:03:41 +0100473{
Thomas Garnier59b3d022016-06-21 17:46:59 -0700474 unsigned long pages = 0, paddr_next;
475 unsigned long paddr_last = paddr_end;
Andi Kleence0c0e52008-05-02 11:46:49 +0200476
Thomas Garnier59b3d022016-06-21 17:46:59 -0700477 int i = pmd_index(paddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Thomas Garnier59b3d022016-06-21 17:46:59 -0700479 for (; i < PTRS_PER_PMD; i++, paddr = paddr_next) {
480 pmd_t *pmd = pmd_page + pmd_index(paddr);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400481 pte_t *pte;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700482 pgprot_t new_prot = prot;
Matt Tolentino44df75e2006-01-17 07:03:41 +0100483
Thomas Garnier59b3d022016-06-21 17:46:59 -0700484 paddr_next = (paddr & PMD_MASK) + PMD_SIZE;
485 if (paddr >= paddr_end) {
Yinghai Lueceb3632012-11-16 19:38:56 -0800486 if (!after_bootmem &&
Ingo Molnar3bce64f2017-01-28 14:14:25 +0100487 !e820__mapped_any(paddr & PMD_MASK, paddr_next,
Ingo Molnar09821ff2017-01-28 17:09:33 +0100488 E820_TYPE_RAM) &&
Ingo Molnar3bce64f2017-01-28 14:14:25 +0100489 !e820__mapped_any(paddr & PMD_MASK, paddr_next,
Ingo Molnar09821ff2017-01-28 17:09:33 +0100490 E820_TYPE_RESERVED_KERN))
Yinghai Lueceb3632012-11-16 19:38:56 -0800491 set_pmd(pmd, __pmd(0));
492 continue;
493 }
Jan Beulich20167d32012-05-16 14:06:26 +0100494
Dave Hansendcb32d92016-07-07 17:19:15 -0700495 if (!pmd_none(*pmd)) {
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100496 if (!pmd_large(*pmd)) {
497 spin_lock(&init_mm.page_table_lock);
Yinghai Lu973dc4f2012-11-16 19:38:59 -0800498 pte = (pte_t *)pmd_page_vaddr(*pmd);
Thomas Garnier59b3d022016-06-21 17:46:59 -0700499 paddr_last = phys_pte_init(pte, paddr,
500 paddr_end, prot);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100501 spin_unlock(&init_mm.page_table_lock);
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700502 continue;
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100503 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700504 /*
505 * If we are ok with PG_LEVEL_2M mapping, then we will
506 * use the existing mapping,
507 *
508 * Otherwise, we will split the large page mapping but
509 * use the same existing protection bits except for
510 * large page, so that we don't violate Intel's TLB
511 * Application note (317080) which says, while changing
512 * the page sizes, new and old translations should
513 * not differ with respect to page frame and
514 * attributes.
515 */
Yinghai Lu3afa3942008-10-25 22:58:21 -0700516 if (page_size_mask & (1 << PG_LEVEL_2M)) {
Jan Beulich876ee612012-10-04 14:48:10 +0100517 if (!after_bootmem)
518 pages++;
Thomas Garnier59b3d022016-06-21 17:46:59 -0700519 paddr_last = paddr_next;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700520 continue;
Yinghai Lu3afa3942008-10-25 22:58:21 -0700521 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700522 new_prot = pte_pgprot(pte_clrhuge(*(pte_t *)pmd));
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400523 }
Keith Mannthey6ad91652006-09-26 10:52:36 +0200524
Yinghai Lub50efd22008-07-08 01:41:05 -0700525 if (page_size_mask & (1<<PG_LEVEL_2M)) {
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400526 pages++;
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100527 spin_lock(&init_mm.page_table_lock);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400528 set_pte((pte_t *)pmd,
Thomas Garnier59b3d022016-06-21 17:46:59 -0700529 pfn_pte((paddr & PMD_MASK) >> PAGE_SHIFT,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700530 __pgprot(pgprot_val(prot) | _PAGE_PSE)));
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100531 spin_unlock(&init_mm.page_table_lock);
Thomas Garnier59b3d022016-06-21 17:46:59 -0700532 paddr_last = paddr_next;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400533 continue;
534 }
535
Yinghai Lu868bf4d2012-11-16 19:39:00 -0800536 pte = alloc_low_page();
Thomas Garnier59b3d022016-06-21 17:46:59 -0700537 paddr_last = phys_pte_init(pte, paddr, paddr_end, new_prot);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400538
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100539 spin_lock(&init_mm.page_table_lock);
Yinghai Lu868bf4d2012-11-16 19:39:00 -0800540 pmd_populate_kernel(&init_mm, pmd, pte);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100541 spin_unlock(&init_mm.page_table_lock);
Matt Tolentino44df75e2006-01-17 07:03:41 +0100542 }
Andi Kleence0c0e52008-05-02 11:46:49 +0200543 update_page_count(PG_LEVEL_2M, pages);
Thomas Garnier59b3d022016-06-21 17:46:59 -0700544 return paddr_last;
Matt Tolentino44df75e2006-01-17 07:03:41 +0100545}
546
Thomas Garnier59b3d022016-06-21 17:46:59 -0700547/*
548 * Create PUD level page table mapping for physical addresses. The virtual
Thomas Garnierfaa37932016-06-21 17:47:00 -0700549 * and physical address do not have to be aligned at this level. KASLR can
550 * randomize virtual addresses up to this level.
Thomas Garnier59b3d022016-06-21 17:46:59 -0700551 * It returns the last physical address mapped.
552 */
Andi Kleencc615032008-03-12 03:53:28 +0100553static unsigned long __meminit
Thomas Garnier59b3d022016-06-21 17:46:59 -0700554phys_pud_init(pud_t *pud_page, unsigned long paddr, unsigned long paddr_end,
555 unsigned long page_size_mask)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100556{
Thomas Garnier59b3d022016-06-21 17:46:59 -0700557 unsigned long pages = 0, paddr_next;
558 unsigned long paddr_last = paddr_end;
Thomas Garnierfaa37932016-06-21 17:47:00 -0700559 unsigned long vaddr = (unsigned long)__va(paddr);
560 int i = pud_index(vaddr);
Matt Tolentino44df75e2006-01-17 07:03:41 +0100561
Thomas Garnier59b3d022016-06-21 17:46:59 -0700562 for (; i < PTRS_PER_PUD; i++, paddr = paddr_next) {
Thomas Garnierfaa37932016-06-21 17:47:00 -0700563 pud_t *pud;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 pmd_t *pmd;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700565 pgprot_t prot = PAGE_KERNEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Thomas Garnierfaa37932016-06-21 17:47:00 -0700567 vaddr = (unsigned long)__va(paddr);
568 pud = pud_page + pud_index(vaddr);
Thomas Garnier59b3d022016-06-21 17:46:59 -0700569 paddr_next = (paddr & PUD_MASK) + PUD_SIZE;
Thomas Garnierfaa37932016-06-21 17:47:00 -0700570
Thomas Garnier59b3d022016-06-21 17:46:59 -0700571 if (paddr >= paddr_end) {
Yinghai Lueceb3632012-11-16 19:38:56 -0800572 if (!after_bootmem &&
Ingo Molnar3bce64f2017-01-28 14:14:25 +0100573 !e820__mapped_any(paddr & PUD_MASK, paddr_next,
Ingo Molnar09821ff2017-01-28 17:09:33 +0100574 E820_TYPE_RAM) &&
Ingo Molnar3bce64f2017-01-28 14:14:25 +0100575 !e820__mapped_any(paddr & PUD_MASK, paddr_next,
Ingo Molnar09821ff2017-01-28 17:09:33 +0100576 E820_TYPE_RESERVED_KERN))
Yinghai Lueceb3632012-11-16 19:38:56 -0800577 set_pud(pud, __pud(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 continue;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Dave Hansendcb32d92016-07-07 17:19:15 -0700581 if (!pud_none(*pud)) {
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700582 if (!pud_large(*pud)) {
Yinghai Lu973dc4f2012-11-16 19:38:59 -0800583 pmd = pmd_offset(pud, 0);
Thomas Garnier59b3d022016-06-21 17:46:59 -0700584 paddr_last = phys_pmd_init(pmd, paddr,
585 paddr_end,
586 page_size_mask,
587 prot);
Yinghai Lu4b239f42010-12-17 16:58:28 -0800588 __flush_tlb_all();
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700589 continue;
590 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700591 /*
592 * If we are ok with PG_LEVEL_1G mapping, then we will
593 * use the existing mapping.
594 *
595 * Otherwise, we will split the gbpage mapping but use
596 * the same existing protection bits except for large
597 * page, so that we don't violate Intel's TLB
598 * Application note (317080) which says, while changing
599 * the page sizes, new and old translations should
600 * not differ with respect to page frame and
601 * attributes.
602 */
Yinghai Lu3afa3942008-10-25 22:58:21 -0700603 if (page_size_mask & (1 << PG_LEVEL_1G)) {
Jan Beulich876ee612012-10-04 14:48:10 +0100604 if (!after_bootmem)
605 pages++;
Thomas Garnier59b3d022016-06-21 17:46:59 -0700606 paddr_last = paddr_next;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700607 continue;
Yinghai Lu3afa3942008-10-25 22:58:21 -0700608 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700609 prot = pte_pgprot(pte_clrhuge(*(pte_t *)pud));
Andi Kleenef925762008-04-17 17:40:45 +0200610 }
611
Yinghai Lub50efd22008-07-08 01:41:05 -0700612 if (page_size_mask & (1<<PG_LEVEL_1G)) {
Andi Kleence0c0e52008-05-02 11:46:49 +0200613 pages++;
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100614 spin_lock(&init_mm.page_table_lock);
Andi Kleenef925762008-04-17 17:40:45 +0200615 set_pte((pte_t *)pud,
Thomas Garnier59b3d022016-06-21 17:46:59 -0700616 pfn_pte((paddr & PUD_MASK) >> PAGE_SHIFT,
Yinghai Lu960ddb4f2012-11-16 19:38:54 -0800617 PAGE_KERNEL_LARGE));
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100618 spin_unlock(&init_mm.page_table_lock);
Thomas Garnier59b3d022016-06-21 17:46:59 -0700619 paddr_last = paddr_next;
Keith Mannthey6ad91652006-09-26 10:52:36 +0200620 continue;
621 }
622
Yinghai Lu868bf4d2012-11-16 19:39:00 -0800623 pmd = alloc_low_page();
Thomas Garnier59b3d022016-06-21 17:46:59 -0700624 paddr_last = phys_pmd_init(pmd, paddr, paddr_end,
625 page_size_mask, prot);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100626
627 spin_lock(&init_mm.page_table_lock);
Yinghai Lu868bf4d2012-11-16 19:39:00 -0800628 pud_populate(&init_mm, pud, pmd);
Matt Tolentino44df75e2006-01-17 07:03:41 +0100629 spin_unlock(&init_mm.page_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
Andi Kleen1a2b4412008-01-30 13:33:54 +0100631 __flush_tlb_all();
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700632
Andi Kleence0c0e52008-05-02 11:46:49 +0200633 update_page_count(PG_LEVEL_1G, pages);
Andi Kleencc615032008-03-12 03:53:28 +0100634
Thomas Garnier59b3d022016-06-21 17:46:59 -0700635 return paddr_last;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100636}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Kirill A. Shutemov7e82ea92017-06-06 14:31:30 +0300638static unsigned long __meminit
639phys_p4d_init(p4d_t *p4d_page, unsigned long paddr, unsigned long paddr_end,
640 unsigned long page_size_mask)
641{
642 unsigned long paddr_next, paddr_last = paddr_end;
643 unsigned long vaddr = (unsigned long)__va(paddr);
644 int i = p4d_index(vaddr);
645
Kirill A. Shutemoved7588d2018-05-18 13:35:24 +0300646 if (!pgtable_l5_enabled())
Kirill A. Shutemov7e82ea92017-06-06 14:31:30 +0300647 return phys_pud_init((pud_t *) p4d_page, paddr, paddr_end, page_size_mask);
648
649 for (; i < PTRS_PER_P4D; i++, paddr = paddr_next) {
650 p4d_t *p4d;
651 pud_t *pud;
652
653 vaddr = (unsigned long)__va(paddr);
654 p4d = p4d_page + p4d_index(vaddr);
655 paddr_next = (paddr & P4D_MASK) + P4D_SIZE;
656
657 if (paddr >= paddr_end) {
658 if (!after_bootmem &&
659 !e820__mapped_any(paddr & P4D_MASK, paddr_next,
660 E820_TYPE_RAM) &&
661 !e820__mapped_any(paddr & P4D_MASK, paddr_next,
662 E820_TYPE_RESERVED_KERN))
663 set_p4d(p4d, __p4d(0));
664 continue;
665 }
666
667 if (!p4d_none(*p4d)) {
668 pud = pud_offset(p4d, 0);
669 paddr_last = phys_pud_init(pud, paddr,
670 paddr_end,
671 page_size_mask);
672 __flush_tlb_all();
673 continue;
674 }
675
676 pud = alloc_low_page();
677 paddr_last = phys_pud_init(pud, paddr, paddr_end,
678 page_size_mask);
679
680 spin_lock(&init_mm.page_table_lock);
681 p4d_populate(&init_mm, p4d, pud);
682 spin_unlock(&init_mm.page_table_lock);
683 }
684 __flush_tlb_all();
685
686 return paddr_last;
687}
688
Thomas Garnier59b3d022016-06-21 17:46:59 -0700689/*
690 * Create page table mapping for the physical memory for specific physical
Thomas Garnierfaa37932016-06-21 17:47:00 -0700691 * addresses. The virtual and physical addresses have to be aligned on PMD level
Thomas Garnier59b3d022016-06-21 17:46:59 -0700692 * down. It returns the last physical address mapped.
693 */
Shaohua Li41d840e2009-06-12 12:57:52 +0800694unsigned long __meminit
Thomas Garnier59b3d022016-06-21 17:46:59 -0700695kernel_physical_mapping_init(unsigned long paddr_start,
696 unsigned long paddr_end,
Pekka Enbergf7650902009-03-05 14:55:05 +0200697 unsigned long page_size_mask)
Yinghai Lub50efd22008-07-08 01:41:05 -0700698{
Haicheng Li9b861522010-08-20 17:50:16 +0800699 bool pgd_changed = false;
Thomas Garnier59b3d022016-06-21 17:46:59 -0700700 unsigned long vaddr, vaddr_start, vaddr_end, vaddr_next, paddr_last;
Yinghai Lub50efd22008-07-08 01:41:05 -0700701
Thomas Garnier59b3d022016-06-21 17:46:59 -0700702 paddr_last = paddr_end;
703 vaddr = (unsigned long)__va(paddr_start);
704 vaddr_end = (unsigned long)__va(paddr_end);
705 vaddr_start = vaddr;
Yinghai Lub50efd22008-07-08 01:41:05 -0700706
Thomas Garnier59b3d022016-06-21 17:46:59 -0700707 for (; vaddr < vaddr_end; vaddr = vaddr_next) {
708 pgd_t *pgd = pgd_offset_k(vaddr);
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300709 p4d_t *p4d;
Yinghai Lub50efd22008-07-08 01:41:05 -0700710
Thomas Garnier59b3d022016-06-21 17:46:59 -0700711 vaddr_next = (vaddr & PGDIR_MASK) + PGDIR_SIZE;
Yinghai Lub50efd22008-07-08 01:41:05 -0700712
Kirill A. Shutemov7e82ea92017-06-06 14:31:30 +0300713 if (pgd_val(*pgd)) {
714 p4d = (p4d_t *)pgd_page_vaddr(*pgd);
715 paddr_last = phys_p4d_init(p4d, __pa(vaddr),
Thomas Garnier59b3d022016-06-21 17:46:59 -0700716 __pa(vaddr_end),
717 page_size_mask);
Yinghai Lub50efd22008-07-08 01:41:05 -0700718 continue;
719 }
720
Kirill A. Shutemov7e82ea92017-06-06 14:31:30 +0300721 p4d = alloc_low_page();
722 paddr_last = phys_p4d_init(p4d, __pa(vaddr), __pa(vaddr_end),
Thomas Garnier59b3d022016-06-21 17:46:59 -0700723 page_size_mask);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100724
725 spin_lock(&init_mm.page_table_lock);
Kirill A. Shutemoved7588d2018-05-18 13:35:24 +0300726 if (pgtable_l5_enabled())
Kirill A. Shutemov7e82ea92017-06-06 14:31:30 +0300727 pgd_populate(&init_mm, pgd, p4d);
728 else
729 p4d_populate(&init_mm, p4d_offset(pgd, vaddr), (pud_t *) p4d);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100730 spin_unlock(&init_mm.page_table_lock);
Haicheng Li9b861522010-08-20 17:50:16 +0800731 pgd_changed = true;
Yinghai Lub50efd22008-07-08 01:41:05 -0700732 }
Haicheng Li9b861522010-08-20 17:50:16 +0800733
734 if (pgd_changed)
Kirill A. Shutemov5372e152016-12-15 02:44:03 +0300735 sync_global_pgds(vaddr_start, vaddr_end - 1);
Haicheng Li9b861522010-08-20 17:50:16 +0800736
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700737 __flush_tlb_all();
Yinghai Lub50efd22008-07-08 01:41:05 -0700738
Thomas Garnier59b3d022016-06-21 17:46:59 -0700739 return paddr_last;
Yinghai Lub50efd22008-07-08 01:41:05 -0700740}
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700741
Matt Tolentino2b976902005-06-23 00:08:06 -0700742#ifndef CONFIG_NUMA
Tejun Heod8fc3af2011-02-16 12:13:06 +0100743void __init initmem_init(void)
Yinghai Lu1f75d7e2008-06-22 02:44:49 -0700744{
Stefan Agnerd7dc8992018-06-14 15:28:02 -0700745 memblock_set_node(0, PHYS_ADDR_MAX, &memblock.memory, 0);
Yinghai Lu1f75d7e2008-06-22 02:44:49 -0700746}
Pekka Enberg3551f882009-05-07 15:35:41 +0300747#endif
Yinghai Lu1f75d7e2008-06-22 02:44:49 -0700748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749void __init paging_init(void)
750{
Pekka Enberg3551f882009-05-07 15:35:41 +0300751 sparse_memory_present_with_active_regions(MAX_NUMNODES);
Matt Tolentino44df75e2006-01-17 07:03:41 +0100752 sparse_init();
Yinghai Lu44b57282009-07-08 09:50:19 -0700753
754 /*
755 * clear the default setting with node 0
756 * note: don't use nodes_clear here, that is really clearing when
757 * numa support is not compiled in, and later node_set_state
758 * will not set it back.
759 */
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -0800760 node_clear_state(0, N_MEMORY);
761 if (N_MEMORY != N_NORMAL_MEMORY)
762 node_clear_state(0, N_NORMAL_MEMORY);
Yinghai Lu44b57282009-07-08 09:50:19 -0700763
Pekka Enberg4c0b2e52011-11-01 15:58:17 +0200764 zone_sizes_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100767/*
Matt Tolentino44df75e2006-01-17 07:03:41 +0100768 * Memory hotplug specific functions
Matt Tolentino44df75e2006-01-17 07:03:41 +0100769 */
Yasunori Gotobc02af92006-06-27 02:53:30 -0700770#ifdef CONFIG_MEMORY_HOTPLUG
771/*
Shaohui Zhengea085412010-02-02 13:44:16 -0800772 * After memory hotplug the variables max_pfn, max_low_pfn and high_memory need
773 * updating.
774 */
Michal Hocko3072e412017-09-08 16:11:39 -0700775static void update_end_of_memory_vars(u64 start, u64 size)
Shaohui Zhengea085412010-02-02 13:44:16 -0800776{
777 unsigned long end_pfn = PFN_UP(start + size);
778
779 if (end_pfn > max_pfn) {
780 max_pfn = end_pfn;
781 max_low_pfn = end_pfn;
782 high_memory = (void *)__va(max_pfn * PAGE_SIZE - 1) + 1;
783 }
784}
785
Christoph Hellwig24e6d5a2017-12-29 08:53:53 +0100786int add_pages(int nid, unsigned long start_pfn, unsigned long nr_pages,
787 struct vmem_altmap *altmap, bool want_memblock)
Yasunori Gotobc02af92006-06-27 02:53:30 -0700788{
Yasunori Gotobc02af92006-06-27 02:53:30 -0700789 int ret;
790
Christoph Hellwig24e6d5a2017-12-29 08:53:53 +0100791 ret = __add_pages(nid, start_pfn, nr_pages, altmap, want_memblock);
Gary Hadefe8b8682008-10-28 16:43:14 -0700792 WARN_ON_ONCE(ret);
Yasunori Gotobc02af92006-06-27 02:53:30 -0700793
Shaohui Zhengea085412010-02-02 13:44:16 -0800794 /* update max_pfn, max_low_pfn and high_memory */
Michal Hocko3072e412017-09-08 16:11:39 -0700795 update_end_of_memory_vars(start_pfn << PAGE_SHIFT,
796 nr_pages << PAGE_SHIFT);
Shaohui Zhengea085412010-02-02 13:44:16 -0800797
Yasunori Gotobc02af92006-06-27 02:53:30 -0700798 return ret;
Yasunori Gotobc02af92006-06-27 02:53:30 -0700799}
Michal Hocko3072e412017-09-08 16:11:39 -0700800
Christoph Hellwig24e6d5a2017-12-29 08:53:53 +0100801int arch_add_memory(int nid, u64 start, u64 size, struct vmem_altmap *altmap,
802 bool want_memblock)
Michal Hocko3072e412017-09-08 16:11:39 -0700803{
804 unsigned long start_pfn = start >> PAGE_SHIFT;
805 unsigned long nr_pages = size >> PAGE_SHIFT;
806
807 init_memory_mapping(start, start + size);
808
Christoph Hellwig24e6d5a2017-12-29 08:53:53 +0100809 return add_pages(nid, start_pfn, nr_pages, altmap, want_memblock);
Michal Hocko3072e412017-09-08 16:11:39 -0700810}
Yasunori Gotobc02af92006-06-27 02:53:30 -0700811
Wen Congyangae9aae92013-02-22 16:33:04 -0800812#define PAGE_INUSE 0xFD
813
Dan Williamsa7e6c702018-03-13 21:36:22 -0700814static void __meminit free_pagetable(struct page *page, int order)
Wen Congyangae9aae92013-02-22 16:33:04 -0800815{
Wen Congyangae9aae92013-02-22 16:33:04 -0800816 unsigned long magic;
817 unsigned int nr_pages = 1 << order;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800818
Wen Congyangae9aae92013-02-22 16:33:04 -0800819 /* bootmem page has reserved flag */
820 if (PageReserved(page)) {
821 __ClearPageReserved(page);
Wen Congyangae9aae92013-02-22 16:33:04 -0800822
Yasuaki Ishimatsuddffe982017-02-22 15:45:13 -0800823 magic = (unsigned long)page->freelist;
Wen Congyangae9aae92013-02-22 16:33:04 -0800824 if (magic == SECTION_INFO || magic == MIX_SECTION_INFO) {
825 while (nr_pages--)
826 put_page_bootmem(page++);
827 } else
Jiang Liu170a5a72013-07-03 15:03:17 -0700828 while (nr_pages--)
829 free_reserved_page(page++);
Wen Congyangae9aae92013-02-22 16:33:04 -0800830 } else
831 free_pages((unsigned long)page_address(page), order);
Wen Congyangae9aae92013-02-22 16:33:04 -0800832}
833
Dan Williamsa7e6c702018-03-13 21:36:22 -0700834static void __meminit free_hugepage_table(struct page *page,
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100835 struct vmem_altmap *altmap)
Wen Congyangae9aae92013-02-22 16:33:04 -0800836{
Dan Williamsa7e6c702018-03-13 21:36:22 -0700837 if (altmap)
838 vmem_altmap_free(altmap, PMD_SIZE / PAGE_SIZE);
839 else
840 free_pagetable(page, get_order(PMD_SIZE));
841}
842
843static void __meminit free_pte_table(pte_t *pte_start, pmd_t *pmd)
844{
Wen Congyangae9aae92013-02-22 16:33:04 -0800845 pte_t *pte;
846 int i;
847
848 for (i = 0; i < PTRS_PER_PTE; i++) {
849 pte = pte_start + i;
Dave Hansendcb32d92016-07-07 17:19:15 -0700850 if (!pte_none(*pte))
Wen Congyangae9aae92013-02-22 16:33:04 -0800851 return;
852 }
853
854 /* free a pte talbe */
Dan Williamsa7e6c702018-03-13 21:36:22 -0700855 free_pagetable(pmd_page(*pmd), 0);
Wen Congyangae9aae92013-02-22 16:33:04 -0800856 spin_lock(&init_mm.page_table_lock);
857 pmd_clear(pmd);
858 spin_unlock(&init_mm.page_table_lock);
859}
860
Dan Williamsa7e6c702018-03-13 21:36:22 -0700861static void __meminit free_pmd_table(pmd_t *pmd_start, pud_t *pud)
Wen Congyangae9aae92013-02-22 16:33:04 -0800862{
863 pmd_t *pmd;
864 int i;
865
866 for (i = 0; i < PTRS_PER_PMD; i++) {
867 pmd = pmd_start + i;
Dave Hansendcb32d92016-07-07 17:19:15 -0700868 if (!pmd_none(*pmd))
Wen Congyangae9aae92013-02-22 16:33:04 -0800869 return;
870 }
871
872 /* free a pmd talbe */
Dan Williamsa7e6c702018-03-13 21:36:22 -0700873 free_pagetable(pud_page(*pud), 0);
Wen Congyangae9aae92013-02-22 16:33:04 -0800874 spin_lock(&init_mm.page_table_lock);
875 pud_clear(pud);
876 spin_unlock(&init_mm.page_table_lock);
877}
878
Dan Williamsa7e6c702018-03-13 21:36:22 -0700879static void __meminit free_pud_table(pud_t *pud_start, p4d_t *p4d)
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300880{
881 pud_t *pud;
882 int i;
883
884 for (i = 0; i < PTRS_PER_PUD; i++) {
885 pud = pud_start + i;
886 if (!pud_none(*pud))
887 return;
888 }
889
890 /* free a pud talbe */
Dan Williamsa7e6c702018-03-13 21:36:22 -0700891 free_pagetable(p4d_page(*p4d), 0);
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300892 spin_lock(&init_mm.page_table_lock);
893 p4d_clear(p4d);
894 spin_unlock(&init_mm.page_table_lock);
895}
896
Wen Congyangae9aae92013-02-22 16:33:04 -0800897static void __meminit
898remove_pte_table(pte_t *pte_start, unsigned long addr, unsigned long end,
Dan Williamsa7e6c702018-03-13 21:36:22 -0700899 bool direct)
Wen Congyangae9aae92013-02-22 16:33:04 -0800900{
901 unsigned long next, pages = 0;
902 pte_t *pte;
903 void *page_addr;
904 phys_addr_t phys_addr;
905
906 pte = pte_start + pte_index(addr);
907 for (; addr < end; addr = next, pte++) {
908 next = (addr + PAGE_SIZE) & PAGE_MASK;
909 if (next > end)
910 next = end;
911
912 if (!pte_present(*pte))
913 continue;
914
915 /*
916 * We mapped [0,1G) memory as identity mapping when
917 * initializing, in arch/x86/kernel/head_64.S. These
918 * pagetables cannot be removed.
919 */
920 phys_addr = pte_val(*pte) + (addr & PAGE_MASK);
921 if (phys_addr < (phys_addr_t)0x40000000)
922 return;
923
Kefeng Wangb500f772016-01-12 10:19:30 +0800924 if (PAGE_ALIGNED(addr) && PAGE_ALIGNED(next)) {
Wen Congyangae9aae92013-02-22 16:33:04 -0800925 /*
926 * Do not free direct mapping pages since they were
927 * freed when offlining, or simplely not in use.
928 */
929 if (!direct)
Dan Williamsa7e6c702018-03-13 21:36:22 -0700930 free_pagetable(pte_page(*pte), 0);
Wen Congyangae9aae92013-02-22 16:33:04 -0800931
932 spin_lock(&init_mm.page_table_lock);
933 pte_clear(&init_mm, addr, pte);
934 spin_unlock(&init_mm.page_table_lock);
935
936 /* For non-direct mapping, pages means nothing. */
937 pages++;
938 } else {
939 /*
940 * If we are here, we are freeing vmemmap pages since
941 * direct mapped memory ranges to be freed are aligned.
942 *
943 * If we are not removing the whole page, it means
944 * other page structs in this page are being used and
945 * we canot remove them. So fill the unused page_structs
946 * with 0xFD, and remove the page when it is wholly
947 * filled with 0xFD.
948 */
949 memset((void *)addr, PAGE_INUSE, next - addr);
950
951 page_addr = page_address(pte_page(*pte));
952 if (!memchr_inv(page_addr, PAGE_INUSE, PAGE_SIZE)) {
Dan Williamsa7e6c702018-03-13 21:36:22 -0700953 free_pagetable(pte_page(*pte), 0);
Wen Congyangae9aae92013-02-22 16:33:04 -0800954
955 spin_lock(&init_mm.page_table_lock);
956 pte_clear(&init_mm, addr, pte);
957 spin_unlock(&init_mm.page_table_lock);
958 }
959 }
960 }
961
962 /* Call free_pte_table() in remove_pmd_table(). */
963 flush_tlb_all();
964 if (direct)
965 update_page_count(PG_LEVEL_4K, -pages);
966}
967
968static void __meminit
969remove_pmd_table(pmd_t *pmd_start, unsigned long addr, unsigned long end,
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100970 bool direct, struct vmem_altmap *altmap)
Wen Congyangae9aae92013-02-22 16:33:04 -0800971{
972 unsigned long next, pages = 0;
973 pte_t *pte_base;
974 pmd_t *pmd;
975 void *page_addr;
976
977 pmd = pmd_start + pmd_index(addr);
978 for (; addr < end; addr = next, pmd++) {
979 next = pmd_addr_end(addr, end);
980
981 if (!pmd_present(*pmd))
982 continue;
983
984 if (pmd_large(*pmd)) {
985 if (IS_ALIGNED(addr, PMD_SIZE) &&
986 IS_ALIGNED(next, PMD_SIZE)) {
987 if (!direct)
Dan Williamsa7e6c702018-03-13 21:36:22 -0700988 free_hugepage_table(pmd_page(*pmd),
989 altmap);
Wen Congyangae9aae92013-02-22 16:33:04 -0800990
991 spin_lock(&init_mm.page_table_lock);
992 pmd_clear(pmd);
993 spin_unlock(&init_mm.page_table_lock);
994 pages++;
995 } else {
996 /* If here, we are freeing vmemmap pages. */
997 memset((void *)addr, PAGE_INUSE, next - addr);
998
999 page_addr = page_address(pmd_page(*pmd));
1000 if (!memchr_inv(page_addr, PAGE_INUSE,
1001 PMD_SIZE)) {
Dan Williamsa7e6c702018-03-13 21:36:22 -07001002 free_hugepage_table(pmd_page(*pmd),
1003 altmap);
Wen Congyangae9aae92013-02-22 16:33:04 -08001004
1005 spin_lock(&init_mm.page_table_lock);
1006 pmd_clear(pmd);
1007 spin_unlock(&init_mm.page_table_lock);
1008 }
1009 }
1010
1011 continue;
1012 }
1013
1014 pte_base = (pte_t *)pmd_page_vaddr(*pmd);
Dan Williamsa7e6c702018-03-13 21:36:22 -07001015 remove_pte_table(pte_base, addr, next, direct);
1016 free_pte_table(pte_base, pmd);
Wen Congyangae9aae92013-02-22 16:33:04 -08001017 }
1018
1019 /* Call free_pmd_table() in remove_pud_table(). */
1020 if (direct)
1021 update_page_count(PG_LEVEL_2M, -pages);
1022}
1023
1024static void __meminit
1025remove_pud_table(pud_t *pud_start, unsigned long addr, unsigned long end,
Christoph Hellwig24b6d412017-12-29 08:53:56 +01001026 struct vmem_altmap *altmap, bool direct)
Wen Congyangae9aae92013-02-22 16:33:04 -08001027{
1028 unsigned long next, pages = 0;
1029 pmd_t *pmd_base;
1030 pud_t *pud;
1031 void *page_addr;
1032
1033 pud = pud_start + pud_index(addr);
1034 for (; addr < end; addr = next, pud++) {
1035 next = pud_addr_end(addr, end);
1036
1037 if (!pud_present(*pud))
1038 continue;
1039
1040 if (pud_large(*pud)) {
1041 if (IS_ALIGNED(addr, PUD_SIZE) &&
1042 IS_ALIGNED(next, PUD_SIZE)) {
1043 if (!direct)
1044 free_pagetable(pud_page(*pud),
Dan Williamsa7e6c702018-03-13 21:36:22 -07001045 get_order(PUD_SIZE));
Wen Congyangae9aae92013-02-22 16:33:04 -08001046
1047 spin_lock(&init_mm.page_table_lock);
1048 pud_clear(pud);
1049 spin_unlock(&init_mm.page_table_lock);
1050 pages++;
1051 } else {
1052 /* If here, we are freeing vmemmap pages. */
1053 memset((void *)addr, PAGE_INUSE, next - addr);
1054
1055 page_addr = page_address(pud_page(*pud));
1056 if (!memchr_inv(page_addr, PAGE_INUSE,
1057 PUD_SIZE)) {
1058 free_pagetable(pud_page(*pud),
Dan Williamsa7e6c702018-03-13 21:36:22 -07001059 get_order(PUD_SIZE));
Wen Congyangae9aae92013-02-22 16:33:04 -08001060
1061 spin_lock(&init_mm.page_table_lock);
1062 pud_clear(pud);
1063 spin_unlock(&init_mm.page_table_lock);
1064 }
1065 }
1066
1067 continue;
1068 }
1069
Kirill A. Shutemove6ab9c42017-04-25 12:25:57 +03001070 pmd_base = pmd_offset(pud, 0);
Christoph Hellwig24b6d412017-12-29 08:53:56 +01001071 remove_pmd_table(pmd_base, addr, next, direct, altmap);
Dan Williamsa7e6c702018-03-13 21:36:22 -07001072 free_pmd_table(pmd_base, pud);
Wen Congyangae9aae92013-02-22 16:33:04 -08001073 }
1074
1075 if (direct)
1076 update_page_count(PG_LEVEL_1G, -pages);
1077}
1078
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +03001079static void __meminit
1080remove_p4d_table(p4d_t *p4d_start, unsigned long addr, unsigned long end,
Christoph Hellwig24b6d412017-12-29 08:53:56 +01001081 struct vmem_altmap *altmap, bool direct)
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +03001082{
1083 unsigned long next, pages = 0;
1084 pud_t *pud_base;
1085 p4d_t *p4d;
1086
1087 p4d = p4d_start + p4d_index(addr);
1088 for (; addr < end; addr = next, p4d++) {
1089 next = p4d_addr_end(addr, end);
1090
1091 if (!p4d_present(*p4d))
1092 continue;
1093
1094 BUILD_BUG_ON(p4d_large(*p4d));
1095
Kirill A. Shutemove6ab9c42017-04-25 12:25:57 +03001096 pud_base = pud_offset(p4d, 0);
Christoph Hellwig24b6d412017-12-29 08:53:56 +01001097 remove_pud_table(pud_base, addr, next, altmap, direct);
Jérôme Glisse98fe3632017-06-24 14:05:14 -04001098 /*
1099 * For 4-level page tables we do not want to free PUDs, but in the
1100 * 5-level case we should free them. This code will have to change
1101 * to adapt for boot-time switching between 4 and 5 level page tables.
1102 */
Kirill A. Shutemoved7588d2018-05-18 13:35:24 +03001103 if (pgtable_l5_enabled())
Dan Williamsa7e6c702018-03-13 21:36:22 -07001104 free_pud_table(pud_base, p4d);
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +03001105 }
1106
1107 if (direct)
1108 update_page_count(PG_LEVEL_512G, -pages);
1109}
1110
Wen Congyangae9aae92013-02-22 16:33:04 -08001111/* start and end are both virtual address. */
1112static void __meminit
Christoph Hellwig24b6d412017-12-29 08:53:56 +01001113remove_pagetable(unsigned long start, unsigned long end, bool direct,
1114 struct vmem_altmap *altmap)
Wen Congyangae9aae92013-02-22 16:33:04 -08001115{
1116 unsigned long next;
Yasuaki Ishimatsu5255e0a2014-08-22 13:27:31 -07001117 unsigned long addr;
Wen Congyangae9aae92013-02-22 16:33:04 -08001118 pgd_t *pgd;
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +03001119 p4d_t *p4d;
Wen Congyangae9aae92013-02-22 16:33:04 -08001120
Yasuaki Ishimatsu5255e0a2014-08-22 13:27:31 -07001121 for (addr = start; addr < end; addr = next) {
1122 next = pgd_addr_end(addr, end);
Wen Congyangae9aae92013-02-22 16:33:04 -08001123
Yasuaki Ishimatsu5255e0a2014-08-22 13:27:31 -07001124 pgd = pgd_offset_k(addr);
Wen Congyangae9aae92013-02-22 16:33:04 -08001125 if (!pgd_present(*pgd))
1126 continue;
1127
Kirill A. Shutemove6ab9c42017-04-25 12:25:57 +03001128 p4d = p4d_offset(pgd, 0);
Christoph Hellwig24b6d412017-12-29 08:53:56 +01001129 remove_p4d_table(p4d, addr, next, altmap, direct);
Wen Congyangae9aae92013-02-22 16:33:04 -08001130 }
1131
Wen Congyangae9aae92013-02-22 16:33:04 -08001132 flush_tlb_all();
1133}
1134
Christoph Hellwig24b6d412017-12-29 08:53:56 +01001135void __ref vmemmap_free(unsigned long start, unsigned long end,
1136 struct vmem_altmap *altmap)
Tang Chen01975182013-02-22 16:33:08 -08001137{
Christoph Hellwig24b6d412017-12-29 08:53:56 +01001138 remove_pagetable(start, end, false, altmap);
Tang Chen01975182013-02-22 16:33:08 -08001139}
1140
Tang Chen587ff8c2013-04-15 17:46:46 +08001141#ifdef CONFIG_MEMORY_HOTREMOVE
Tang Chenbbcab872013-02-22 16:33:06 -08001142static void __meminit
1143kernel_physical_mapping_remove(unsigned long start, unsigned long end)
1144{
1145 start = (unsigned long)__va(start);
1146 end = (unsigned long)__va(end);
1147
Christoph Hellwig24b6d412017-12-29 08:53:56 +01001148 remove_pagetable(start, end, true, NULL);
Tang Chenbbcab872013-02-22 16:33:06 -08001149}
1150
Christoph Hellwigda024512017-12-29 08:53:55 +01001151int __ref arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap)
Wen Congyang24d335c2013-02-22 16:32:58 -08001152{
1153 unsigned long start_pfn = start >> PAGE_SHIFT;
1154 unsigned long nr_pages = size >> PAGE_SHIFT;
Dan Williams4b94ffd2016-01-15 16:56:22 -08001155 struct page *page = pfn_to_page(start_pfn);
Wen Congyang24d335c2013-02-22 16:32:58 -08001156 struct zone *zone;
1157 int ret;
1158
Dan Williams4b94ffd2016-01-15 16:56:22 -08001159 /* With altmap the first mapped page is offset from @start */
Dan Williams4b94ffd2016-01-15 16:56:22 -08001160 if (altmap)
1161 page += vmem_altmap_offset(altmap);
1162 zone = page_zone(page);
Christoph Hellwigda024512017-12-29 08:53:55 +01001163 ret = __remove_pages(zone, start_pfn, nr_pages, altmap);
Wen Congyang24d335c2013-02-22 16:32:58 -08001164 WARN_ON_ONCE(ret);
Dan Williams4b94ffd2016-01-15 16:56:22 -08001165 kernel_physical_mapping_remove(start, start + size);
Wen Congyang24d335c2013-02-22 16:32:58 -08001166
1167 return ret;
1168}
1169#endif
Keith Mannthey45e0b782006-09-30 23:27:09 -07001170#endif /* CONFIG_MEMORY_HOTPLUG */
1171
KAMEZAWA Hiroyuki81ac3ad2009-09-22 16:45:49 -07001172static struct kcore_list kcore_vsyscall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Yinghai Lu94b43c32012-11-16 19:39:19 -08001174static void __init register_page_bootmem_info(void)
1175{
1176#ifdef CONFIG_NUMA
1177 int i;
1178
1179 for_each_online_node(i)
1180 register_page_bootmem_info_node(NODE_DATA(i));
1181#endif
1182}
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184void __init mem_init(void)
1185{
Jon Mason0dc243a2006-06-26 13:58:11 +02001186 pci_iommu_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Yinghai Lu48ddb152008-01-30 13:32:36 +01001188 /* clear_bss() already clear the empty_zero_page */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Jiang Liubced0e32013-04-29 15:06:53 -07001190 /* this will put all memory onto the freelists */
Jiang Liu0c988532013-07-03 15:03:24 -07001191 free_all_bootmem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 after_bootmem = 1;
Pavel Tatashin6f84f8d2018-04-10 16:36:10 -07001193 x86_init.hyper.init_after_bootmem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Pavel Tatashin353b1e72017-11-15 17:36:14 -08001195 /*
1196 * Must be done after boot memory is put on freelist, because here we
1197 * might set fields in deferred struct pages that have not yet been
1198 * initialized, and free_all_bootmem() initializes all the reserved
1199 * deferred pages for us.
1200 */
1201 register_page_bootmem_info();
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 /* Register memory areas for /proc/kcore */
Jia Zhangcd026ca2018-02-12 22:44:54 +08001204 if (get_gate_vma(&init_mm))
1205 kclist_add(&kcore_vsyscall, (void *)VSYSCALL_ADDR, PAGE_SIZE, KCORE_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Jiang Liu46a84132013-07-03 15:04:19 -07001207 mem_init_print_info(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
Suresh Siddha502f6602009-10-28 18:46:56 -08001210int kernel_set_to_readonly;
Steven Rostedt16239632009-02-17 17:57:30 -05001211
1212void set_kernel_text_rw(void)
1213{
Suresh Siddhab9af7c02009-10-14 14:46:55 -07001214 unsigned long start = PFN_ALIGN(_text);
Suresh Siddhae7d23dd2009-10-28 18:46:58 -08001215 unsigned long end = PFN_ALIGN(__stop___ex_table);
Steven Rostedt16239632009-02-17 17:57:30 -05001216
1217 if (!kernel_set_to_readonly)
1218 return;
1219
1220 pr_debug("Set kernel text: %lx - %lx for read write\n",
1221 start, end);
1222
Suresh Siddhae7d23dd2009-10-28 18:46:58 -08001223 /*
1224 * Make the kernel identity mapping for text RW. Kernel text
1225 * mapping will always be RO. Refer to the comment in
1226 * static_protections() in pageattr.c
1227 */
Steven Rostedt16239632009-02-17 17:57:30 -05001228 set_memory_rw(start, (end - start) >> PAGE_SHIFT);
1229}
1230
1231void set_kernel_text_ro(void)
1232{
Suresh Siddhab9af7c02009-10-14 14:46:55 -07001233 unsigned long start = PFN_ALIGN(_text);
Suresh Siddhae7d23dd2009-10-28 18:46:58 -08001234 unsigned long end = PFN_ALIGN(__stop___ex_table);
Steven Rostedt16239632009-02-17 17:57:30 -05001235
1236 if (!kernel_set_to_readonly)
1237 return;
1238
1239 pr_debug("Set kernel text: %lx - %lx for read only\n",
1240 start, end);
1241
Suresh Siddhae7d23dd2009-10-28 18:46:58 -08001242 /*
1243 * Set the kernel identity mapping for text RO.
1244 */
Steven Rostedt16239632009-02-17 17:57:30 -05001245 set_memory_ro(start, (end - start) >> PAGE_SHIFT);
1246}
1247
Arjan van de Ven67df1972006-01-06 00:12:04 -08001248void mark_rodata_ro(void)
1249{
Suresh Siddha74e08172009-10-14 14:46:56 -07001250 unsigned long start = PFN_ALIGN(_text);
Alexander Duyckfc8d7822012-11-16 13:57:13 -08001251 unsigned long rodata_start = PFN_ALIGN(__start_rodata);
Suresh Siddha74e08172009-10-14 14:46:56 -07001252 unsigned long end = (unsigned long) &__end_rodata_hpage_align;
Alexander Duyckfc8d7822012-11-16 13:57:13 -08001253 unsigned long text_end = PFN_ALIGN(&__stop___ex_table);
1254 unsigned long rodata_end = PFN_ALIGN(&__end_rodata);
Kees Cook45e2a9d2014-11-14 11:47:37 -08001255 unsigned long all_end;
Steven Rostedt8f0f9962008-05-12 21:20:56 +02001256
Jan Beulich6fb14752007-05-02 19:27:10 +02001257 printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
Linus Torvaldse3ebadd2007-05-07 08:44:24 -07001258 (end - start) >> 10);
Arjan van de Ven984bb802008-02-06 22:39:45 +01001259 set_memory_ro(start, (end - start) >> PAGE_SHIFT);
1260
Steven Rostedt16239632009-02-17 17:57:30 -05001261 kernel_set_to_readonly = 1;
1262
Arjan van de Ven984bb802008-02-06 22:39:45 +01001263 /*
Yinghai Lu72212672013-01-24 12:20:13 -08001264 * The rodata/data/bss/brk section (but not the kernel text!)
1265 * should also be not-executable.
Kees Cook45e2a9d2014-11-14 11:47:37 -08001266 *
1267 * We align all_end to PMD_SIZE because the existing mapping
1268 * is a full PMD. If we would align _brk_end to PAGE_SIZE we
1269 * split the PMD and the reminder between _brk_end and the end
1270 * of the PMD will remain mapped executable.
1271 *
1272 * Any PMD which was setup after the one which covers _brk_end
1273 * has been zapped already via cleanup_highmem().
Arjan van de Ven984bb802008-02-06 22:39:45 +01001274 */
Kees Cook45e2a9d2014-11-14 11:47:37 -08001275 all_end = roundup((unsigned long)_brk_end, PMD_SIZE);
Stephen Smalleyab76f7b2015-10-01 09:04:22 -04001276 set_memory_nx(text_end, (all_end - text_end) >> PAGE_SHIFT);
Arjan van de Ven67df1972006-01-06 00:12:04 -08001277
Andi Kleen0c42f392008-01-30 13:33:42 +01001278#ifdef CONFIG_CPA_DEBUG
Ingo Molnar10f22dd2008-01-30 13:34:10 +01001279 printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end);
Arjan van de Ven6d238cc2008-01-30 13:34:06 +01001280 set_memory_rw(start, (end-start) >> PAGE_SHIFT);
Andi Kleen0c42f392008-01-30 13:33:42 +01001281
Ingo Molnar10f22dd2008-01-30 13:34:10 +01001282 printk(KERN_INFO "Testing CPA: again\n");
Arjan van de Ven6d238cc2008-01-30 13:34:06 +01001283 set_memory_ro(start, (end-start) >> PAGE_SHIFT);
Andi Kleen0c42f392008-01-30 13:33:42 +01001284#endif
Suresh Siddha74e08172009-10-14 14:46:56 -07001285
Jiang Liuc88442e2013-07-03 15:02:58 -07001286 free_init_pages("unused kernel",
Alexander Duyckfc8d7822012-11-16 13:57:13 -08001287 (unsigned long) __va(__pa_symbol(text_end)),
1288 (unsigned long) __va(__pa_symbol(rodata_start)));
Jiang Liuc88442e2013-07-03 15:02:58 -07001289 free_init_pages("unused kernel",
Alexander Duyckfc8d7822012-11-16 13:57:13 -08001290 (unsigned long) __va(__pa_symbol(rodata_end)),
1291 (unsigned long) __va(__pa_symbol(_sdata)));
Stephen Smalleye1a58322015-10-05 12:55:20 -04001292
1293 debug_checkwx();
Dave Hansen8c06c772018-04-06 13:55:18 -07001294
1295 /*
1296 * Do this after all of the manipulation of the
1297 * kernel text page tables are complete.
1298 */
1299 pti_clone_kernel_text();
Arjan van de Ven67df1972006-01-06 00:12:04 -08001300}
Mathieu Desnoyers4e4eee02008-02-02 15:42:20 -05001301
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001302int kern_addr_valid(unsigned long addr)
1303{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001305 pgd_t *pgd;
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +03001306 p4d_t *p4d;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001307 pud_t *pud;
1308 pmd_t *pmd;
1309 pte_t *pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 if (above != 0 && above != -1UL)
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001312 return 0;
1313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 pgd = pgd_offset_k(addr);
1315 if (pgd_none(*pgd))
1316 return 0;
1317
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +03001318 p4d = p4d_offset(pgd, addr);
1319 if (p4d_none(*p4d))
1320 return 0;
1321
1322 pud = pud_offset(p4d, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 if (pud_none(*pud))
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001324 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Mel Gorman0ee364e2013-02-11 14:52:36 +00001326 if (pud_large(*pud))
1327 return pfn_valid(pud_pfn(*pud));
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 pmd = pmd_offset(pud, addr);
1330 if (pmd_none(*pmd))
1331 return 0;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 if (pmd_large(*pmd))
1334 return pfn_valid(pmd_pfn(*pmd));
1335
1336 pte = pte_offset_kernel(pmd, addr);
1337 if (pte_none(*pte))
1338 return 0;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 return pfn_valid(pte_pfn(*pte));
1341}
1342
Pavel Tatashin078eb6a2018-04-05 16:22:43 -07001343/*
1344 * Block size is the minimum amount of memory which can be hotplugged or
1345 * hotremoved. It must be power of two and must be equal or larger than
1346 * MIN_MEMORY_BLOCK_SIZE.
1347 */
1348#define MAX_BLOCK_SIZE (2UL << 30)
1349
1350/* Amount of ram needed to start using large blocks */
1351#define MEM_SIZE_FOR_LARGE_BLOCK (64UL << 30)
1352
mike.travis@hpe.comf642fb52018-05-24 15:17:12 -05001353/* Adjustable memory block size */
1354static unsigned long set_memory_block_size;
1355int __init set_memory_block_size_order(unsigned int order)
1356{
1357 unsigned long size = 1UL << order;
1358
1359 if (size > MEM_SIZE_FOR_LARGE_BLOCK || size < MIN_MEMORY_BLOCK_SIZE)
1360 return -EINVAL;
1361
1362 set_memory_block_size = size;
1363 return 0;
1364}
1365
Yinghai Lu982792c2014-06-04 16:06:32 -07001366static unsigned long probe_memory_block_size(void)
Nathan Fontenot1dc41aa2011-01-20 10:46:15 -06001367{
Pavel Tatashin078eb6a2018-04-05 16:22:43 -07001368 unsigned long boot_mem_end = max_pfn << PAGE_SHIFT;
1369 unsigned long bz;
Yinghai Lu982792c2014-06-04 16:06:32 -07001370
mike.travis@hpe.comf642fb52018-05-24 15:17:12 -05001371 /* If memory block size has been set, then use it */
1372 bz = set_memory_block_size;
1373 if (bz)
Pavel Tatashin078eb6a2018-04-05 16:22:43 -07001374 goto done;
Nathan Fontenot1dc41aa2011-01-20 10:46:15 -06001375
Pavel Tatashin078eb6a2018-04-05 16:22:43 -07001376 /* Use regular block if RAM is smaller than MEM_SIZE_FOR_LARGE_BLOCK */
1377 if (boot_mem_end < MEM_SIZE_FOR_LARGE_BLOCK) {
1378 bz = MIN_MEMORY_BLOCK_SIZE;
1379 goto done;
1380 }
1381
1382 /* Find the largest allowed block size that aligns to memory end */
1383 for (bz = MAX_BLOCK_SIZE; bz > MIN_MEMORY_BLOCK_SIZE; bz >>= 1) {
1384 if (IS_ALIGNED(boot_mem_end, bz))
1385 break;
1386 }
1387done:
Seth Jennings43c75f92015-11-30 10:47:43 -06001388 pr_info("x86/mm: Memory block size: %ldMB\n", bz >> 20);
Yinghai Lu982792c2014-06-04 16:06:32 -07001389
1390 return bz;
1391}
1392
1393static unsigned long memory_block_size_probed;
1394unsigned long memory_block_size_bytes(void)
1395{
1396 if (!memory_block_size_probed)
1397 memory_block_size_probed = probe_memory_block_size();
1398
1399 return memory_block_size_probed;
1400}
1401
Christoph Lameter0889eba2007-10-16 01:24:15 -07001402#ifdef CONFIG_SPARSEMEM_VMEMMAP
1403/*
1404 * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
1405 */
Yinghai Luc2b91e22008-04-12 01:19:24 -07001406static long __meminitdata addr_start, addr_end;
1407static void __meminitdata *p_start, *p_end;
1408static int __meminitdata node_start;
1409
Johannes Weinere8216da2013-04-29 15:07:54 -07001410static int __meminit vmemmap_populate_hugepages(unsigned long start,
Dan Williams4b94ffd2016-01-15 16:56:22 -08001411 unsigned long end, int node, struct vmem_altmap *altmap)
Christoph Lameter0889eba2007-10-16 01:24:15 -07001412{
Johannes Weiner0aad8182013-04-29 15:07:50 -07001413 unsigned long addr;
Christoph Lameter0889eba2007-10-16 01:24:15 -07001414 unsigned long next;
1415 pgd_t *pgd;
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +03001416 p4d_t *p4d;
Christoph Lameter0889eba2007-10-16 01:24:15 -07001417 pud_t *pud;
1418 pmd_t *pmd;
1419
Johannes Weiner0aad8182013-04-29 15:07:50 -07001420 for (addr = start; addr < end; addr = next) {
Johannes Weinere8216da2013-04-29 15:07:54 -07001421 next = pmd_addr_end(addr, end);
Christoph Lameter0889eba2007-10-16 01:24:15 -07001422
1423 pgd = vmemmap_pgd_populate(addr, node);
1424 if (!pgd)
1425 return -ENOMEM;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001426
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +03001427 p4d = vmemmap_p4d_populate(pgd, addr, node);
1428 if (!p4d)
1429 return -ENOMEM;
1430
1431 pud = vmemmap_pud_populate(p4d, addr, node);
Christoph Lameter0889eba2007-10-16 01:24:15 -07001432 if (!pud)
1433 return -ENOMEM;
1434
Johannes Weinere8216da2013-04-29 15:07:54 -07001435 pmd = pmd_offset(pud, addr);
1436 if (pmd_none(*pmd)) {
Johannes Weinere8216da2013-04-29 15:07:54 -07001437 void *p;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001438
Christoph Hellwiga8fc3572017-12-29 08:53:58 +01001439 if (altmap)
1440 p = altmap_alloc_block_buf(PMD_SIZE, altmap);
1441 else
1442 p = vmemmap_alloc_block_buf(PMD_SIZE, node);
Johannes Weiner8e2cdbc2013-04-29 15:07:56 -07001443 if (p) {
1444 pte_t entry;
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -04001445
Johannes Weiner8e2cdbc2013-04-29 15:07:56 -07001446 entry = pfn_pte(__pa(p) >> PAGE_SHIFT,
1447 PAGE_KERNEL_LARGE);
1448 set_pmd(pmd, __pmd(pte_val(entry)));
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -04001449
Johannes Weiner8e2cdbc2013-04-29 15:07:56 -07001450 /* check to see if we have contiguous blocks */
1451 if (p_end != p || node_start != node) {
1452 if (p_start)
Dan Williamsc9cdaeb2015-09-17 16:27:57 -04001453 pr_debug(" [%lx-%lx] PMD -> [%p-%p] on node %d\n",
Johannes Weiner8e2cdbc2013-04-29 15:07:56 -07001454 addr_start, addr_end-1, p_start, p_end-1, node_start);
1455 addr_start = addr;
1456 node_start = node;
1457 p_start = p;
1458 }
1459
1460 addr_end = addr + PMD_SIZE;
1461 p_end = p + PMD_SIZE;
1462 continue;
Dan Williams4b94ffd2016-01-15 16:56:22 -08001463 } else if (altmap)
1464 return -ENOMEM; /* no fallback */
Johannes Weiner8e2cdbc2013-04-29 15:07:56 -07001465 } else if (pmd_large(*pmd)) {
Johannes Weinere8216da2013-04-29 15:07:54 -07001466 vmemmap_verify((pte_t *)pmd, node, addr, next);
Johannes Weiner8e2cdbc2013-04-29 15:07:56 -07001467 continue;
1468 }
Johannes Weiner8e2cdbc2013-04-29 15:07:56 -07001469 if (vmemmap_populate_basepages(addr, next, node))
1470 return -ENOMEM;
Christoph Lameter0889eba2007-10-16 01:24:15 -07001471 }
Christoph Lameter0889eba2007-10-16 01:24:15 -07001472 return 0;
1473}
Yinghai Luc2b91e22008-04-12 01:19:24 -07001474
Christoph Hellwig7b73d972017-12-29 08:53:54 +01001475int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
1476 struct vmem_altmap *altmap)
Johannes Weinere8216da2013-04-29 15:07:54 -07001477{
1478 int err;
1479
Borislav Petkov16bf9222016-03-29 17:42:03 +02001480 if (boot_cpu_has(X86_FEATURE_PSE))
Dan Williams4b94ffd2016-01-15 16:56:22 -08001481 err = vmemmap_populate_hugepages(start, end, node, altmap);
1482 else if (altmap) {
1483 pr_err_once("%s: no cpu support for altmap allocations\n",
1484 __func__);
1485 err = -ENOMEM;
1486 } else
Johannes Weinere8216da2013-04-29 15:07:54 -07001487 err = vmemmap_populate_basepages(start, end, node);
1488 if (!err)
Kirill A. Shutemov5372e152016-12-15 02:44:03 +03001489 sync_global_pgds(start, end - 1);
Johannes Weinere8216da2013-04-29 15:07:54 -07001490 return err;
1491}
1492
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -08001493#if defined(CONFIG_MEMORY_HOTPLUG_SPARSE) && defined(CONFIG_HAVE_BOOTMEM_INFO_NODE)
1494void register_page_bootmem_memmap(unsigned long section_nr,
Baoquan He15670bf2017-10-28 09:30:38 +08001495 struct page *start_page, unsigned long nr_pages)
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -08001496{
1497 unsigned long addr = (unsigned long)start_page;
Baoquan He15670bf2017-10-28 09:30:38 +08001498 unsigned long end = (unsigned long)(start_page + nr_pages);
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -08001499 unsigned long next;
1500 pgd_t *pgd;
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +03001501 p4d_t *p4d;
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -08001502 pud_t *pud;
1503 pmd_t *pmd;
Baoquan He15670bf2017-10-28 09:30:38 +08001504 unsigned int nr_pmd_pages;
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -08001505 struct page *page;
1506
1507 for (; addr < end; addr = next) {
1508 pte_t *pte = NULL;
1509
1510 pgd = pgd_offset_k(addr);
1511 if (pgd_none(*pgd)) {
1512 next = (addr + PAGE_SIZE) & PAGE_MASK;
1513 continue;
1514 }
1515 get_page_bootmem(section_nr, pgd_page(*pgd), MIX_SECTION_INFO);
1516
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +03001517 p4d = p4d_offset(pgd, addr);
1518 if (p4d_none(*p4d)) {
1519 next = (addr + PAGE_SIZE) & PAGE_MASK;
1520 continue;
1521 }
1522 get_page_bootmem(section_nr, p4d_page(*p4d), MIX_SECTION_INFO);
1523
1524 pud = pud_offset(p4d, addr);
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -08001525 if (pud_none(*pud)) {
1526 next = (addr + PAGE_SIZE) & PAGE_MASK;
1527 continue;
1528 }
1529 get_page_bootmem(section_nr, pud_page(*pud), MIX_SECTION_INFO);
1530
Borislav Petkov16bf9222016-03-29 17:42:03 +02001531 if (!boot_cpu_has(X86_FEATURE_PSE)) {
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -08001532 next = (addr + PAGE_SIZE) & PAGE_MASK;
1533 pmd = pmd_offset(pud, addr);
1534 if (pmd_none(*pmd))
1535 continue;
1536 get_page_bootmem(section_nr, pmd_page(*pmd),
1537 MIX_SECTION_INFO);
1538
1539 pte = pte_offset_kernel(pmd, addr);
1540 if (pte_none(*pte))
1541 continue;
1542 get_page_bootmem(section_nr, pte_page(*pte),
1543 SECTION_INFO);
1544 } else {
1545 next = pmd_addr_end(addr, end);
1546
1547 pmd = pmd_offset(pud, addr);
1548 if (pmd_none(*pmd))
1549 continue;
1550
Baoquan He15670bf2017-10-28 09:30:38 +08001551 nr_pmd_pages = 1 << get_order(PMD_SIZE);
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -08001552 page = pmd_page(*pmd);
Baoquan He15670bf2017-10-28 09:30:38 +08001553 while (nr_pmd_pages--)
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -08001554 get_page_bootmem(section_nr, page++,
1555 SECTION_INFO);
1556 }
1557 }
1558}
1559#endif
1560
Yinghai Luc2b91e22008-04-12 01:19:24 -07001561void __meminit vmemmap_populate_print_last(void)
1562{
1563 if (p_start) {
Dan Williamsc9cdaeb2015-09-17 16:27:57 -04001564 pr_debug(" [%lx-%lx] PMD -> [%p-%p] on node %d\n",
Yinghai Luc2b91e22008-04-12 01:19:24 -07001565 addr_start, addr_end-1, p_start, p_end-1, node_start);
1566 p_start = NULL;
1567 p_end = NULL;
1568 node_start = 0;
1569 }
1570}
Christoph Lameter0889eba2007-10-16 01:24:15 -07001571#endif