blob: 06afa84ac0a0100f8221907359ebe07f092d0861 [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
Marcin Slusarz8d6ea9672008-08-15 18:32:24 +020091/*
Kirill A. Shutemov5372e152016-12-15 02:44:03 +030092 * When memory was added make sure all the processes MM have
Haicheng Li6afb5152010-05-19 17:42:14 +080093 * suitable PGD entries in the local PGD level page.
94 */
Kirill A. Shutemov141efad2017-06-06 14:31:29 +030095#ifdef CONFIG_X86_5LEVEL
96void sync_global_pgds(unsigned long start, unsigned long end)
97{
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}
132#else
Kirill A. Shutemov5372e152016-12-15 02:44:03 +0300133void sync_global_pgds(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 */
146 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. Shutemov141efad2017-06-06 14:31:29 +0300176#endif
Haicheng Li6afb5152010-05-19 17:42:14 +0800177
178/*
Marcin Slusarz8d6ea9672008-08-15 18:32:24 +0200179 * NOTE: This function is marked __ref because it calls __init function
180 * (alloc_bootmem_pages). It's safe to do it ONLY when after_bootmem == 0.
181 */
182static __ref void *spp_getpage(void)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100183{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 void *ptr;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if (after_bootmem)
Vegard Nossum9e730232009-02-22 11:28:25 +0100187 ptr = (void *) get_zeroed_page(GFP_ATOMIC | __GFP_NOTRACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 else
189 ptr = alloc_bootmem_pages(PAGE_SIZE);
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100190
191 if (!ptr || ((unsigned long)ptr & ~PAGE_MASK)) {
192 panic("set_pte_phys: cannot allocate page data %s\n",
193 after_bootmem ? "after bootmem" : "");
194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Ingo Molnar10f22dd2008-01-30 13:34:10 +0100196 pr_debug("spp_getpage %p\n", ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100198 return ptr;
199}
200
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300201static p4d_t *fill_p4d(pgd_t *pgd, unsigned long vaddr)
Tejun Heo458a3e62009-02-24 11:57:21 +0900202{
203 if (pgd_none(*pgd)) {
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300204 p4d_t *p4d = (p4d_t *)spp_getpage();
205 pgd_populate(&init_mm, pgd, p4d);
206 if (p4d != p4d_offset(pgd, 0))
Tejun Heo458a3e62009-02-24 11:57:21 +0900207 printk(KERN_ERR "PAGETABLE BUG #00! %p <-> %p\n",
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300208 p4d, p4d_offset(pgd, 0));
Tejun Heo458a3e62009-02-24 11:57:21 +0900209 }
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300210 return p4d_offset(pgd, vaddr);
211}
212
213static pud_t *fill_pud(p4d_t *p4d, unsigned long vaddr)
214{
215 if (p4d_none(*p4d)) {
216 pud_t *pud = (pud_t *)spp_getpage();
217 p4d_populate(&init_mm, p4d, pud);
218 if (pud != pud_offset(p4d, 0))
219 printk(KERN_ERR "PAGETABLE BUG #01! %p <-> %p\n",
220 pud, pud_offset(p4d, 0));
221 }
222 return pud_offset(p4d, vaddr);
Tejun Heo458a3e62009-02-24 11:57:21 +0900223}
224
Jeremy Fitzhardingef254f392009-03-03 12:02:57 -0800225static pmd_t *fill_pmd(pud_t *pud, unsigned long vaddr)
Tejun Heo458a3e62009-02-24 11:57:21 +0900226{
227 if (pud_none(*pud)) {
228 pmd_t *pmd = (pmd_t *) spp_getpage();
229 pud_populate(&init_mm, pud, pmd);
230 if (pmd != pmd_offset(pud, 0))
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300231 printk(KERN_ERR "PAGETABLE BUG #02! %p <-> %p\n",
Tejun Heo458a3e62009-02-24 11:57:21 +0900232 pmd, pmd_offset(pud, 0));
233 }
234 return pmd_offset(pud, vaddr);
235}
236
Jeremy Fitzhardingef254f392009-03-03 12:02:57 -0800237static pte_t *fill_pte(pmd_t *pmd, unsigned long vaddr)
Tejun Heo458a3e62009-02-24 11:57:21 +0900238{
239 if (pmd_none(*pmd)) {
240 pte_t *pte = (pte_t *) spp_getpage();
241 pmd_populate_kernel(&init_mm, pmd, pte);
242 if (pte != pte_offset_kernel(pmd, 0))
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300243 printk(KERN_ERR "PAGETABLE BUG #03!\n");
Tejun Heo458a3e62009-02-24 11:57:21 +0900244 }
245 return pte_offset_kernel(pmd, vaddr);
246}
247
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300248static void __set_pte_vaddr(pud_t *pud, unsigned long vaddr, pte_t new_pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300250 pmd_t *pmd = fill_pmd(pud, vaddr);
251 pte_t *pte = fill_pte(pmd, vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 set_pte(pte, new_pte);
254
255 /*
256 * It's enough to flush this one mapping.
257 * (PGE mappings get flushed as well)
258 */
259 __flush_tlb_one(vaddr);
260}
261
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300262void set_pte_vaddr_p4d(p4d_t *p4d_page, unsigned long vaddr, pte_t new_pte)
263{
264 p4d_t *p4d = p4d_page + p4d_index(vaddr);
265 pud_t *pud = fill_pud(p4d, vaddr);
266
267 __set_pte_vaddr(pud, vaddr, new_pte);
268}
269
270void set_pte_vaddr_pud(pud_t *pud_page, unsigned long vaddr, pte_t new_pte)
271{
272 pud_t *pud = pud_page + pud_index(vaddr);
273
274 __set_pte_vaddr(pud, vaddr, new_pte);
275}
276
Tejun Heo458a3e62009-02-24 11:57:21 +0900277void set_pte_vaddr(unsigned long vaddr, pte_t pteval)
Eduardo Habkost0814e0b2008-06-25 00:19:22 -0400278{
279 pgd_t *pgd;
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300280 p4d_t *p4d_page;
Eduardo Habkost0814e0b2008-06-25 00:19:22 -0400281
282 pr_debug("set_pte_vaddr %lx to %lx\n", vaddr, native_pte_val(pteval));
283
284 pgd = pgd_offset_k(vaddr);
285 if (pgd_none(*pgd)) {
286 printk(KERN_ERR
287 "PGD FIXMAP MISSING, it should be setup in head.S!\n");
288 return;
289 }
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300290
291 p4d_page = p4d_offset(pgd, 0);
292 set_pte_vaddr_p4d(p4d_page, vaddr, pteval);
Eduardo Habkost0814e0b2008-06-25 00:19:22 -0400293}
294
Tejun Heo458a3e62009-02-24 11:57:21 +0900295pmd_t * __init populate_extra_pmd(unsigned long vaddr)
Tejun Heo11124412009-02-20 16:29:09 +0900296{
297 pgd_t *pgd;
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300298 p4d_t *p4d;
Tejun Heo11124412009-02-20 16:29:09 +0900299 pud_t *pud;
300
301 pgd = pgd_offset_k(vaddr);
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300302 p4d = fill_p4d(pgd, vaddr);
303 pud = fill_pud(p4d, vaddr);
Tejun Heo458a3e62009-02-24 11:57:21 +0900304 return fill_pmd(pud, vaddr);
305}
Tejun Heo11124412009-02-20 16:29:09 +0900306
Tejun Heo458a3e62009-02-24 11:57:21 +0900307pte_t * __init populate_extra_pte(unsigned long vaddr)
308{
309 pmd_t *pmd;
310
311 pmd = populate_extra_pmd(vaddr);
312 return fill_pte(pmd, vaddr);
Tejun Heo11124412009-02-20 16:29:09 +0900313}
314
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100315/*
Jack Steiner3a9e1892008-07-01 14:45:32 -0500316 * Create large page table mappings for a range of physical addresses.
317 */
318static void __init __init_extra_mapping(unsigned long phys, unsigned long size,
Juergen Gross2df58b62014-11-03 14:01:52 +0100319 enum page_cache_mode cache)
Jack Steiner3a9e1892008-07-01 14:45:32 -0500320{
321 pgd_t *pgd;
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300322 p4d_t *p4d;
Jack Steiner3a9e1892008-07-01 14:45:32 -0500323 pud_t *pud;
324 pmd_t *pmd;
Juergen Gross2df58b62014-11-03 14:01:52 +0100325 pgprot_t prot;
Jack Steiner3a9e1892008-07-01 14:45:32 -0500326
Juergen Gross2df58b62014-11-03 14:01:52 +0100327 pgprot_val(prot) = pgprot_val(PAGE_KERNEL_LARGE) |
328 pgprot_val(pgprot_4k_2_large(cachemode2pgprot(cache)));
Jack Steiner3a9e1892008-07-01 14:45:32 -0500329 BUG_ON((phys & ~PMD_MASK) || (size & ~PMD_MASK));
330 for (; size; phys += PMD_SIZE, size -= PMD_SIZE) {
331 pgd = pgd_offset_k((unsigned long)__va(phys));
332 if (pgd_none(*pgd)) {
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300333 p4d = (p4d_t *) spp_getpage();
334 set_pgd(pgd, __pgd(__pa(p4d) | _KERNPG_TABLE |
Jack Steiner3a9e1892008-07-01 14:45:32 -0500335 _PAGE_USER));
336 }
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300337 p4d = p4d_offset(pgd, (unsigned long)__va(phys));
338 if (p4d_none(*p4d)) {
339 pud = (pud_t *) spp_getpage();
340 set_p4d(p4d, __p4d(__pa(pud) | _KERNPG_TABLE |
341 _PAGE_USER));
342 }
343 pud = pud_offset(p4d, (unsigned long)__va(phys));
Jack Steiner3a9e1892008-07-01 14:45:32 -0500344 if (pud_none(*pud)) {
345 pmd = (pmd_t *) spp_getpage();
346 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE |
347 _PAGE_USER));
348 }
349 pmd = pmd_offset(pud, phys);
350 BUG_ON(!pmd_none(*pmd));
351 set_pmd(pmd, __pmd(phys | pgprot_val(prot)));
352 }
353}
354
355void __init init_extra_mapping_wb(unsigned long phys, unsigned long size)
356{
Juergen Gross2df58b62014-11-03 14:01:52 +0100357 __init_extra_mapping(phys, size, _PAGE_CACHE_MODE_WB);
Jack Steiner3a9e1892008-07-01 14:45:32 -0500358}
359
360void __init init_extra_mapping_uc(unsigned long phys, unsigned long size)
361{
Juergen Gross2df58b62014-11-03 14:01:52 +0100362 __init_extra_mapping(phys, size, _PAGE_CACHE_MODE_UC);
Jack Steiner3a9e1892008-07-01 14:45:32 -0500363}
364
365/*
Ingo Molnar88f3aec2008-02-21 11:04:11 +0100366 * The head.S code sets up the kernel high mapping:
367 *
368 * from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text)
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100369 *
Zhang Yanfei1e3b3082013-05-15 10:58:07 +0800370 * phys_base holds the negative offset to the kernel, which is added
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100371 * to the compile time generated pmds. This results in invalid pmds up
372 * to the point where we hit the physaddr 0 mapping.
373 *
Yinghai Lue5f15b42011-02-18 11:30:30 +0000374 * We limit the mappings to the region from _text to _brk_end. _brk_end
375 * is rounded up to the 2MB boundary. This catches the invalid pmds as
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100376 * well, as they are located before _text:
377 */
378void __init cleanup_highmap(void)
379{
380 unsigned long vaddr = __START_KERNEL_map;
Yinghai Lu10054232013-01-24 12:19:54 -0800381 unsigned long vaddr_end = __START_KERNEL_map + KERNEL_IMAGE_SIZE;
Yinghai Lue5f15b42011-02-18 11:30:30 +0000382 unsigned long end = roundup((unsigned long)_brk_end, PMD_SIZE) - 1;
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100383 pmd_t *pmd = level2_kernel_pgt;
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100384
Yinghai Lu10054232013-01-24 12:19:54 -0800385 /*
386 * Native path, max_pfn_mapped is not set yet.
387 * Xen has valid max_pfn_mapped set in
388 * arch/x86/xen/mmu.c:xen_setup_kernel_pagetable().
389 */
390 if (max_pfn_mapped)
391 vaddr_end = __START_KERNEL_map + (max_pfn_mapped << PAGE_SHIFT);
392
Yinghai Lue5f15b42011-02-18 11:30:30 +0000393 for (; vaddr + PMD_SIZE - 1 < vaddr_end; pmd++, vaddr += PMD_SIZE) {
Hugh Dickins2884f112008-05-28 19:36:07 +0100394 if (pmd_none(*pmd))
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100395 continue;
396 if (vaddr < (unsigned long) _text || vaddr > end)
397 set_pmd(pmd, __pmd(0));
398 }
399}
400
Thomas Garnier59b3d022016-06-21 17:46:59 -0700401/*
402 * Create PTE level page table mapping for physical addresses.
403 * It returns the last physical address mapped.
404 */
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700405static unsigned long __meminit
Thomas Garnier59b3d022016-06-21 17:46:59 -0700406phys_pte_init(pte_t *pte_page, unsigned long paddr, unsigned long paddr_end,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700407 pgprot_t prot)
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400408{
Thomas Garnier59b3d022016-06-21 17:46:59 -0700409 unsigned long pages = 0, paddr_next;
410 unsigned long paddr_last = paddr_end;
411 pte_t *pte;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400412 int i;
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700413
Thomas Garnier59b3d022016-06-21 17:46:59 -0700414 pte = pte_page + pte_index(paddr);
415 i = pte_index(paddr);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400416
Thomas Garnier59b3d022016-06-21 17:46:59 -0700417 for (; i < PTRS_PER_PTE; i++, paddr = paddr_next, pte++) {
418 paddr_next = (paddr & PAGE_MASK) + PAGE_SIZE;
419 if (paddr >= paddr_end) {
Yinghai Lueceb3632012-11-16 19:38:56 -0800420 if (!after_bootmem &&
Ingo Molnar3bce64f2017-01-28 14:14:25 +0100421 !e820__mapped_any(paddr & PAGE_MASK, paddr_next,
Ingo Molnar09821ff2017-01-28 17:09:33 +0100422 E820_TYPE_RAM) &&
Ingo Molnar3bce64f2017-01-28 14:14:25 +0100423 !e820__mapped_any(paddr & PAGE_MASK, paddr_next,
Ingo Molnar09821ff2017-01-28 17:09:33 +0100424 E820_TYPE_RESERVED_KERN))
Yinghai Lueceb3632012-11-16 19:38:56 -0800425 set_pte(pte, __pte(0));
426 continue;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400427 }
428
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700429 /*
430 * We will re-use the existing mapping.
431 * Xen for example has some special requirements, like mapping
432 * pagetable pages as RO. So assume someone who pre-setup
433 * these mappings are more intelligent.
434 */
Dave Hansendcb32d92016-07-07 17:19:15 -0700435 if (!pte_none(*pte)) {
Jan Beulich876ee612012-10-04 14:48:10 +0100436 if (!after_bootmem)
437 pages++;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400438 continue;
Yinghai Lu3afa3942008-10-25 22:58:21 -0700439 }
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400440
441 if (0)
Thomas Garnier59b3d022016-06-21 17:46:59 -0700442 pr_info(" pte=%p addr=%lx pte=%016lx\n", pte, paddr,
443 pfn_pte(paddr >> PAGE_SHIFT, PAGE_KERNEL).pte);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400444 pages++;
Thomas Garnier59b3d022016-06-21 17:46:59 -0700445 set_pte(pte, pfn_pte(paddr >> PAGE_SHIFT, prot));
446 paddr_last = (paddr & PAGE_MASK) + PAGE_SIZE;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400447 }
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700448
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400449 update_page_count(PG_LEVEL_4K, pages);
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700450
Thomas Garnier59b3d022016-06-21 17:46:59 -0700451 return paddr_last;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400452}
453
Thomas Garnier59b3d022016-06-21 17:46:59 -0700454/*
455 * Create PMD level page table mapping for physical addresses. The virtual
456 * and physical address have to be aligned at this level.
457 * It returns the last physical address mapped.
458 */
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700459static unsigned long __meminit
Thomas Garnier59b3d022016-06-21 17:46:59 -0700460phys_pmd_init(pmd_t *pmd_page, unsigned long paddr, unsigned long paddr_end,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700461 unsigned long page_size_mask, pgprot_t prot)
Matt Tolentino44df75e2006-01-17 07:03:41 +0100462{
Thomas Garnier59b3d022016-06-21 17:46:59 -0700463 unsigned long pages = 0, paddr_next;
464 unsigned long paddr_last = paddr_end;
Andi Kleence0c0e52008-05-02 11:46:49 +0200465
Thomas Garnier59b3d022016-06-21 17:46:59 -0700466 int i = pmd_index(paddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Thomas Garnier59b3d022016-06-21 17:46:59 -0700468 for (; i < PTRS_PER_PMD; i++, paddr = paddr_next) {
469 pmd_t *pmd = pmd_page + pmd_index(paddr);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400470 pte_t *pte;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700471 pgprot_t new_prot = prot;
Matt Tolentino44df75e2006-01-17 07:03:41 +0100472
Thomas Garnier59b3d022016-06-21 17:46:59 -0700473 paddr_next = (paddr & PMD_MASK) + PMD_SIZE;
474 if (paddr >= paddr_end) {
Yinghai Lueceb3632012-11-16 19:38:56 -0800475 if (!after_bootmem &&
Ingo Molnar3bce64f2017-01-28 14:14:25 +0100476 !e820__mapped_any(paddr & PMD_MASK, paddr_next,
Ingo Molnar09821ff2017-01-28 17:09:33 +0100477 E820_TYPE_RAM) &&
Ingo Molnar3bce64f2017-01-28 14:14:25 +0100478 !e820__mapped_any(paddr & PMD_MASK, paddr_next,
Ingo Molnar09821ff2017-01-28 17:09:33 +0100479 E820_TYPE_RESERVED_KERN))
Yinghai Lueceb3632012-11-16 19:38:56 -0800480 set_pmd(pmd, __pmd(0));
481 continue;
482 }
Jan Beulich20167d32012-05-16 14:06:26 +0100483
Dave Hansendcb32d92016-07-07 17:19:15 -0700484 if (!pmd_none(*pmd)) {
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100485 if (!pmd_large(*pmd)) {
486 spin_lock(&init_mm.page_table_lock);
Yinghai Lu973dc4f2012-11-16 19:38:59 -0800487 pte = (pte_t *)pmd_page_vaddr(*pmd);
Thomas Garnier59b3d022016-06-21 17:46:59 -0700488 paddr_last = phys_pte_init(pte, paddr,
489 paddr_end, prot);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100490 spin_unlock(&init_mm.page_table_lock);
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700491 continue;
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100492 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700493 /*
494 * If we are ok with PG_LEVEL_2M mapping, then we will
495 * use the existing mapping,
496 *
497 * Otherwise, we will split the large page mapping but
498 * use the same existing protection bits except for
499 * large page, so that we don't violate Intel's TLB
500 * Application note (317080) which says, while changing
501 * the page sizes, new and old translations should
502 * not differ with respect to page frame and
503 * attributes.
504 */
Yinghai Lu3afa3942008-10-25 22:58:21 -0700505 if (page_size_mask & (1 << PG_LEVEL_2M)) {
Jan Beulich876ee612012-10-04 14:48:10 +0100506 if (!after_bootmem)
507 pages++;
Thomas Garnier59b3d022016-06-21 17:46:59 -0700508 paddr_last = paddr_next;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700509 continue;
Yinghai Lu3afa3942008-10-25 22:58:21 -0700510 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700511 new_prot = pte_pgprot(pte_clrhuge(*(pte_t *)pmd));
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400512 }
Keith Mannthey6ad91652006-09-26 10:52:36 +0200513
Yinghai Lub50efd22008-07-08 01:41:05 -0700514 if (page_size_mask & (1<<PG_LEVEL_2M)) {
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400515 pages++;
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100516 spin_lock(&init_mm.page_table_lock);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400517 set_pte((pte_t *)pmd,
Thomas Garnier59b3d022016-06-21 17:46:59 -0700518 pfn_pte((paddr & PMD_MASK) >> PAGE_SHIFT,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700519 __pgprot(pgprot_val(prot) | _PAGE_PSE)));
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100520 spin_unlock(&init_mm.page_table_lock);
Thomas Garnier59b3d022016-06-21 17:46:59 -0700521 paddr_last = paddr_next;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400522 continue;
523 }
524
Yinghai Lu868bf4d2012-11-16 19:39:00 -0800525 pte = alloc_low_page();
Thomas Garnier59b3d022016-06-21 17:46:59 -0700526 paddr_last = phys_pte_init(pte, paddr, paddr_end, new_prot);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400527
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100528 spin_lock(&init_mm.page_table_lock);
Yinghai Lu868bf4d2012-11-16 19:39:00 -0800529 pmd_populate_kernel(&init_mm, pmd, pte);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100530 spin_unlock(&init_mm.page_table_lock);
Matt Tolentino44df75e2006-01-17 07:03:41 +0100531 }
Andi Kleence0c0e52008-05-02 11:46:49 +0200532 update_page_count(PG_LEVEL_2M, pages);
Thomas Garnier59b3d022016-06-21 17:46:59 -0700533 return paddr_last;
Matt Tolentino44df75e2006-01-17 07:03:41 +0100534}
535
Thomas Garnier59b3d022016-06-21 17:46:59 -0700536/*
537 * Create PUD level page table mapping for physical addresses. The virtual
Thomas Garnierfaa37932016-06-21 17:47:00 -0700538 * and physical address do not have to be aligned at this level. KASLR can
539 * randomize virtual addresses up to this level.
Thomas Garnier59b3d022016-06-21 17:46:59 -0700540 * It returns the last physical address mapped.
541 */
Andi Kleencc615032008-03-12 03:53:28 +0100542static unsigned long __meminit
Thomas Garnier59b3d022016-06-21 17:46:59 -0700543phys_pud_init(pud_t *pud_page, unsigned long paddr, unsigned long paddr_end,
544 unsigned long page_size_mask)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100545{
Thomas Garnier59b3d022016-06-21 17:46:59 -0700546 unsigned long pages = 0, paddr_next;
547 unsigned long paddr_last = paddr_end;
Thomas Garnierfaa37932016-06-21 17:47:00 -0700548 unsigned long vaddr = (unsigned long)__va(paddr);
549 int i = pud_index(vaddr);
Matt Tolentino44df75e2006-01-17 07:03:41 +0100550
Thomas Garnier59b3d022016-06-21 17:46:59 -0700551 for (; i < PTRS_PER_PUD; i++, paddr = paddr_next) {
Thomas Garnierfaa37932016-06-21 17:47:00 -0700552 pud_t *pud;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 pmd_t *pmd;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700554 pgprot_t prot = PAGE_KERNEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Thomas Garnierfaa37932016-06-21 17:47:00 -0700556 vaddr = (unsigned long)__va(paddr);
557 pud = pud_page + pud_index(vaddr);
Thomas Garnier59b3d022016-06-21 17:46:59 -0700558 paddr_next = (paddr & PUD_MASK) + PUD_SIZE;
Thomas Garnierfaa37932016-06-21 17:47:00 -0700559
Thomas Garnier59b3d022016-06-21 17:46:59 -0700560 if (paddr >= paddr_end) {
Yinghai Lueceb3632012-11-16 19:38:56 -0800561 if (!after_bootmem &&
Ingo Molnar3bce64f2017-01-28 14:14:25 +0100562 !e820__mapped_any(paddr & PUD_MASK, paddr_next,
Ingo Molnar09821ff2017-01-28 17:09:33 +0100563 E820_TYPE_RAM) &&
Ingo Molnar3bce64f2017-01-28 14:14:25 +0100564 !e820__mapped_any(paddr & PUD_MASK, paddr_next,
Ingo Molnar09821ff2017-01-28 17:09:33 +0100565 E820_TYPE_RESERVED_KERN))
Yinghai Lueceb3632012-11-16 19:38:56 -0800566 set_pud(pud, __pud(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 continue;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Dave Hansendcb32d92016-07-07 17:19:15 -0700570 if (!pud_none(*pud)) {
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700571 if (!pud_large(*pud)) {
Yinghai Lu973dc4f2012-11-16 19:38:59 -0800572 pmd = pmd_offset(pud, 0);
Thomas Garnier59b3d022016-06-21 17:46:59 -0700573 paddr_last = phys_pmd_init(pmd, paddr,
574 paddr_end,
575 page_size_mask,
576 prot);
Yinghai Lu4b239f42010-12-17 16:58:28 -0800577 __flush_tlb_all();
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700578 continue;
579 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700580 /*
581 * If we are ok with PG_LEVEL_1G mapping, then we will
582 * use the existing mapping.
583 *
584 * Otherwise, we will split the gbpage mapping but use
585 * the same existing protection bits except for large
586 * page, so that we don't violate Intel's TLB
587 * Application note (317080) which says, while changing
588 * the page sizes, new and old translations should
589 * not differ with respect to page frame and
590 * attributes.
591 */
Yinghai Lu3afa3942008-10-25 22:58:21 -0700592 if (page_size_mask & (1 << PG_LEVEL_1G)) {
Jan Beulich876ee612012-10-04 14:48:10 +0100593 if (!after_bootmem)
594 pages++;
Thomas Garnier59b3d022016-06-21 17:46:59 -0700595 paddr_last = paddr_next;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700596 continue;
Yinghai Lu3afa3942008-10-25 22:58:21 -0700597 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700598 prot = pte_pgprot(pte_clrhuge(*(pte_t *)pud));
Andi Kleenef925762008-04-17 17:40:45 +0200599 }
600
Yinghai Lub50efd22008-07-08 01:41:05 -0700601 if (page_size_mask & (1<<PG_LEVEL_1G)) {
Andi Kleence0c0e52008-05-02 11:46:49 +0200602 pages++;
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100603 spin_lock(&init_mm.page_table_lock);
Andi Kleenef925762008-04-17 17:40:45 +0200604 set_pte((pte_t *)pud,
Thomas Garnier59b3d022016-06-21 17:46:59 -0700605 pfn_pte((paddr & PUD_MASK) >> PAGE_SHIFT,
Yinghai Lu960ddb4f2012-11-16 19:38:54 -0800606 PAGE_KERNEL_LARGE));
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100607 spin_unlock(&init_mm.page_table_lock);
Thomas Garnier59b3d022016-06-21 17:46:59 -0700608 paddr_last = paddr_next;
Keith Mannthey6ad91652006-09-26 10:52:36 +0200609 continue;
610 }
611
Yinghai Lu868bf4d2012-11-16 19:39:00 -0800612 pmd = alloc_low_page();
Thomas Garnier59b3d022016-06-21 17:46:59 -0700613 paddr_last = phys_pmd_init(pmd, paddr, paddr_end,
614 page_size_mask, prot);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100615
616 spin_lock(&init_mm.page_table_lock);
Yinghai Lu868bf4d2012-11-16 19:39:00 -0800617 pud_populate(&init_mm, pud, pmd);
Matt Tolentino44df75e2006-01-17 07:03:41 +0100618 spin_unlock(&init_mm.page_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
Andi Kleen1a2b4412008-01-30 13:33:54 +0100620 __flush_tlb_all();
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700621
Andi Kleence0c0e52008-05-02 11:46:49 +0200622 update_page_count(PG_LEVEL_1G, pages);
Andi Kleencc615032008-03-12 03:53:28 +0100623
Thomas Garnier59b3d022016-06-21 17:46:59 -0700624 return paddr_last;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100625}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Kirill A. Shutemov7e82ea92017-06-06 14:31:30 +0300627static unsigned long __meminit
628phys_p4d_init(p4d_t *p4d_page, unsigned long paddr, unsigned long paddr_end,
629 unsigned long page_size_mask)
630{
631 unsigned long paddr_next, paddr_last = paddr_end;
632 unsigned long vaddr = (unsigned long)__va(paddr);
633 int i = p4d_index(vaddr);
634
635 if (!IS_ENABLED(CONFIG_X86_5LEVEL))
636 return phys_pud_init((pud_t *) p4d_page, paddr, paddr_end, page_size_mask);
637
638 for (; i < PTRS_PER_P4D; i++, paddr = paddr_next) {
639 p4d_t *p4d;
640 pud_t *pud;
641
642 vaddr = (unsigned long)__va(paddr);
643 p4d = p4d_page + p4d_index(vaddr);
644 paddr_next = (paddr & P4D_MASK) + P4D_SIZE;
645
646 if (paddr >= paddr_end) {
647 if (!after_bootmem &&
648 !e820__mapped_any(paddr & P4D_MASK, paddr_next,
649 E820_TYPE_RAM) &&
650 !e820__mapped_any(paddr & P4D_MASK, paddr_next,
651 E820_TYPE_RESERVED_KERN))
652 set_p4d(p4d, __p4d(0));
653 continue;
654 }
655
656 if (!p4d_none(*p4d)) {
657 pud = pud_offset(p4d, 0);
658 paddr_last = phys_pud_init(pud, paddr,
659 paddr_end,
660 page_size_mask);
661 __flush_tlb_all();
662 continue;
663 }
664
665 pud = alloc_low_page();
666 paddr_last = phys_pud_init(pud, paddr, paddr_end,
667 page_size_mask);
668
669 spin_lock(&init_mm.page_table_lock);
670 p4d_populate(&init_mm, p4d, pud);
671 spin_unlock(&init_mm.page_table_lock);
672 }
673 __flush_tlb_all();
674
675 return paddr_last;
676}
677
Thomas Garnier59b3d022016-06-21 17:46:59 -0700678/*
679 * Create page table mapping for the physical memory for specific physical
Thomas Garnierfaa37932016-06-21 17:47:00 -0700680 * addresses. The virtual and physical addresses have to be aligned on PMD level
Thomas Garnier59b3d022016-06-21 17:46:59 -0700681 * down. It returns the last physical address mapped.
682 */
Shaohua Li41d840e2009-06-12 12:57:52 +0800683unsigned long __meminit
Thomas Garnier59b3d022016-06-21 17:46:59 -0700684kernel_physical_mapping_init(unsigned long paddr_start,
685 unsigned long paddr_end,
Pekka Enbergf7650902009-03-05 14:55:05 +0200686 unsigned long page_size_mask)
Yinghai Lub50efd22008-07-08 01:41:05 -0700687{
Haicheng Li9b861522010-08-20 17:50:16 +0800688 bool pgd_changed = false;
Thomas Garnier59b3d022016-06-21 17:46:59 -0700689 unsigned long vaddr, vaddr_start, vaddr_end, vaddr_next, paddr_last;
Yinghai Lub50efd22008-07-08 01:41:05 -0700690
Thomas Garnier59b3d022016-06-21 17:46:59 -0700691 paddr_last = paddr_end;
692 vaddr = (unsigned long)__va(paddr_start);
693 vaddr_end = (unsigned long)__va(paddr_end);
694 vaddr_start = vaddr;
Yinghai Lub50efd22008-07-08 01:41:05 -0700695
Thomas Garnier59b3d022016-06-21 17:46:59 -0700696 for (; vaddr < vaddr_end; vaddr = vaddr_next) {
697 pgd_t *pgd = pgd_offset_k(vaddr);
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300698 p4d_t *p4d;
Yinghai Lub50efd22008-07-08 01:41:05 -0700699
Thomas Garnier59b3d022016-06-21 17:46:59 -0700700 vaddr_next = (vaddr & PGDIR_MASK) + PGDIR_SIZE;
Yinghai Lub50efd22008-07-08 01:41:05 -0700701
Kirill A. Shutemov7e82ea92017-06-06 14:31:30 +0300702 if (pgd_val(*pgd)) {
703 p4d = (p4d_t *)pgd_page_vaddr(*pgd);
704 paddr_last = phys_p4d_init(p4d, __pa(vaddr),
Thomas Garnier59b3d022016-06-21 17:46:59 -0700705 __pa(vaddr_end),
706 page_size_mask);
Yinghai Lub50efd22008-07-08 01:41:05 -0700707 continue;
708 }
709
Kirill A. Shutemov7e82ea92017-06-06 14:31:30 +0300710 p4d = alloc_low_page();
711 paddr_last = phys_p4d_init(p4d, __pa(vaddr), __pa(vaddr_end),
Thomas Garnier59b3d022016-06-21 17:46:59 -0700712 page_size_mask);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100713
714 spin_lock(&init_mm.page_table_lock);
Kirill A. Shutemov7e82ea92017-06-06 14:31:30 +0300715 if (IS_ENABLED(CONFIG_X86_5LEVEL))
716 pgd_populate(&init_mm, pgd, p4d);
717 else
718 p4d_populate(&init_mm, p4d_offset(pgd, vaddr), (pud_t *) p4d);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100719 spin_unlock(&init_mm.page_table_lock);
Haicheng Li9b861522010-08-20 17:50:16 +0800720 pgd_changed = true;
Yinghai Lub50efd22008-07-08 01:41:05 -0700721 }
Haicheng Li9b861522010-08-20 17:50:16 +0800722
723 if (pgd_changed)
Kirill A. Shutemov5372e152016-12-15 02:44:03 +0300724 sync_global_pgds(vaddr_start, vaddr_end - 1);
Haicheng Li9b861522010-08-20 17:50:16 +0800725
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700726 __flush_tlb_all();
Yinghai Lub50efd22008-07-08 01:41:05 -0700727
Thomas Garnier59b3d022016-06-21 17:46:59 -0700728 return paddr_last;
Yinghai Lub50efd22008-07-08 01:41:05 -0700729}
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700730
Matt Tolentino2b976902005-06-23 00:08:06 -0700731#ifndef CONFIG_NUMA
Tejun Heod8fc3af2011-02-16 12:13:06 +0100732void __init initmem_init(void)
Yinghai Lu1f75d7e2008-06-22 02:44:49 -0700733{
Tang Chene7e8de52014-01-21 15:49:26 -0800734 memblock_set_node(0, (phys_addr_t)ULLONG_MAX, &memblock.memory, 0);
Yinghai Lu1f75d7e2008-06-22 02:44:49 -0700735}
Pekka Enberg3551f882009-05-07 15:35:41 +0300736#endif
Yinghai Lu1f75d7e2008-06-22 02:44:49 -0700737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738void __init paging_init(void)
739{
Pekka Enberg3551f882009-05-07 15:35:41 +0300740 sparse_memory_present_with_active_regions(MAX_NUMNODES);
Matt Tolentino44df75e2006-01-17 07:03:41 +0100741 sparse_init();
Yinghai Lu44b57282009-07-08 09:50:19 -0700742
743 /*
744 * clear the default setting with node 0
745 * note: don't use nodes_clear here, that is really clearing when
746 * numa support is not compiled in, and later node_set_state
747 * will not set it back.
748 */
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -0800749 node_clear_state(0, N_MEMORY);
750 if (N_MEMORY != N_NORMAL_MEMORY)
751 node_clear_state(0, N_NORMAL_MEMORY);
Yinghai Lu44b57282009-07-08 09:50:19 -0700752
Pekka Enberg4c0b2e52011-11-01 15:58:17 +0200753 zone_sizes_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100756/*
Matt Tolentino44df75e2006-01-17 07:03:41 +0100757 * Memory hotplug specific functions
Matt Tolentino44df75e2006-01-17 07:03:41 +0100758 */
Yasunori Gotobc02af92006-06-27 02:53:30 -0700759#ifdef CONFIG_MEMORY_HOTPLUG
760/*
Shaohui Zhengea085412010-02-02 13:44:16 -0800761 * After memory hotplug the variables max_pfn, max_low_pfn and high_memory need
762 * updating.
763 */
764static void update_end_of_memory_vars(u64 start, u64 size)
765{
766 unsigned long end_pfn = PFN_UP(start + size);
767
768 if (end_pfn > max_pfn) {
769 max_pfn = end_pfn;
770 max_low_pfn = end_pfn;
771 high_memory = (void *)__va(max_pfn * PAGE_SIZE - 1) + 1;
772 }
773}
774
Dan Williams033fbae2015-08-09 15:29:06 -0400775int arch_add_memory(int nid, u64 start, u64 size, bool for_device)
Yasunori Gotobc02af92006-06-27 02:53:30 -0700776{
Jacob Shin66520eb2012-11-16 19:38:52 -0800777 unsigned long start_pfn = start >> PAGE_SHIFT;
Yasunori Gotobc02af92006-06-27 02:53:30 -0700778 unsigned long nr_pages = size >> PAGE_SHIFT;
779 int ret;
780
Jacob Shin66520eb2012-11-16 19:38:52 -0800781 init_memory_mapping(start, start + size);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700782
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700783 ret = __add_pages(nid, start_pfn, nr_pages, !for_device);
Gary Hadefe8b8682008-10-28 16:43:14 -0700784 WARN_ON_ONCE(ret);
Yasunori Gotobc02af92006-06-27 02:53:30 -0700785
Shaohui Zhengea085412010-02-02 13:44:16 -0800786 /* update max_pfn, max_low_pfn and high_memory */
787 update_end_of_memory_vars(start, size);
788
Yasunori Gotobc02af92006-06-27 02:53:30 -0700789 return ret;
Yasunori Gotobc02af92006-06-27 02:53:30 -0700790}
791EXPORT_SYMBOL_GPL(arch_add_memory);
792
Wen Congyangae9aae92013-02-22 16:33:04 -0800793#define PAGE_INUSE 0xFD
794
795static void __meminit free_pagetable(struct page *page, int order)
796{
Wen Congyangae9aae92013-02-22 16:33:04 -0800797 unsigned long magic;
798 unsigned int nr_pages = 1 << order;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800799 struct vmem_altmap *altmap = to_vmem_altmap((unsigned long) page);
800
801 if (altmap) {
802 vmem_altmap_free(altmap, nr_pages);
803 return;
804 }
Wen Congyangae9aae92013-02-22 16:33:04 -0800805
806 /* bootmem page has reserved flag */
807 if (PageReserved(page)) {
808 __ClearPageReserved(page);
Wen Congyangae9aae92013-02-22 16:33:04 -0800809
Yasuaki Ishimatsuddffe982017-02-22 15:45:13 -0800810 magic = (unsigned long)page->freelist;
Wen Congyangae9aae92013-02-22 16:33:04 -0800811 if (magic == SECTION_INFO || magic == MIX_SECTION_INFO) {
812 while (nr_pages--)
813 put_page_bootmem(page++);
814 } else
Jiang Liu170a5a72013-07-03 15:03:17 -0700815 while (nr_pages--)
816 free_reserved_page(page++);
Wen Congyangae9aae92013-02-22 16:33:04 -0800817 } else
818 free_pages((unsigned long)page_address(page), order);
Wen Congyangae9aae92013-02-22 16:33:04 -0800819}
820
821static void __meminit free_pte_table(pte_t *pte_start, pmd_t *pmd)
822{
823 pte_t *pte;
824 int i;
825
826 for (i = 0; i < PTRS_PER_PTE; i++) {
827 pte = pte_start + i;
Dave Hansendcb32d92016-07-07 17:19:15 -0700828 if (!pte_none(*pte))
Wen Congyangae9aae92013-02-22 16:33:04 -0800829 return;
830 }
831
832 /* free a pte talbe */
833 free_pagetable(pmd_page(*pmd), 0);
834 spin_lock(&init_mm.page_table_lock);
835 pmd_clear(pmd);
836 spin_unlock(&init_mm.page_table_lock);
837}
838
839static void __meminit free_pmd_table(pmd_t *pmd_start, pud_t *pud)
840{
841 pmd_t *pmd;
842 int i;
843
844 for (i = 0; i < PTRS_PER_PMD; i++) {
845 pmd = pmd_start + i;
Dave Hansendcb32d92016-07-07 17:19:15 -0700846 if (!pmd_none(*pmd))
Wen Congyangae9aae92013-02-22 16:33:04 -0800847 return;
848 }
849
850 /* free a pmd talbe */
851 free_pagetable(pud_page(*pud), 0);
852 spin_lock(&init_mm.page_table_lock);
853 pud_clear(pud);
854 spin_unlock(&init_mm.page_table_lock);
855}
856
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300857static void __meminit free_pud_table(pud_t *pud_start, p4d_t *p4d)
858{
859 pud_t *pud;
860 int i;
861
862 for (i = 0; i < PTRS_PER_PUD; i++) {
863 pud = pud_start + i;
864 if (!pud_none(*pud))
865 return;
866 }
867
868 /* free a pud talbe */
869 free_pagetable(p4d_page(*p4d), 0);
870 spin_lock(&init_mm.page_table_lock);
871 p4d_clear(p4d);
872 spin_unlock(&init_mm.page_table_lock);
873}
874
Wen Congyangae9aae92013-02-22 16:33:04 -0800875static void __meminit
876remove_pte_table(pte_t *pte_start, unsigned long addr, unsigned long end,
877 bool direct)
878{
879 unsigned long next, pages = 0;
880 pte_t *pte;
881 void *page_addr;
882 phys_addr_t phys_addr;
883
884 pte = pte_start + pte_index(addr);
885 for (; addr < end; addr = next, pte++) {
886 next = (addr + PAGE_SIZE) & PAGE_MASK;
887 if (next > end)
888 next = end;
889
890 if (!pte_present(*pte))
891 continue;
892
893 /*
894 * We mapped [0,1G) memory as identity mapping when
895 * initializing, in arch/x86/kernel/head_64.S. These
896 * pagetables cannot be removed.
897 */
898 phys_addr = pte_val(*pte) + (addr & PAGE_MASK);
899 if (phys_addr < (phys_addr_t)0x40000000)
900 return;
901
Kefeng Wangb500f772016-01-12 10:19:30 +0800902 if (PAGE_ALIGNED(addr) && PAGE_ALIGNED(next)) {
Wen Congyangae9aae92013-02-22 16:33:04 -0800903 /*
904 * Do not free direct mapping pages since they were
905 * freed when offlining, or simplely not in use.
906 */
907 if (!direct)
908 free_pagetable(pte_page(*pte), 0);
909
910 spin_lock(&init_mm.page_table_lock);
911 pte_clear(&init_mm, addr, pte);
912 spin_unlock(&init_mm.page_table_lock);
913
914 /* For non-direct mapping, pages means nothing. */
915 pages++;
916 } else {
917 /*
918 * If we are here, we are freeing vmemmap pages since
919 * direct mapped memory ranges to be freed are aligned.
920 *
921 * If we are not removing the whole page, it means
922 * other page structs in this page are being used and
923 * we canot remove them. So fill the unused page_structs
924 * with 0xFD, and remove the page when it is wholly
925 * filled with 0xFD.
926 */
927 memset((void *)addr, PAGE_INUSE, next - addr);
928
929 page_addr = page_address(pte_page(*pte));
930 if (!memchr_inv(page_addr, PAGE_INUSE, PAGE_SIZE)) {
931 free_pagetable(pte_page(*pte), 0);
932
933 spin_lock(&init_mm.page_table_lock);
934 pte_clear(&init_mm, addr, pte);
935 spin_unlock(&init_mm.page_table_lock);
936 }
937 }
938 }
939
940 /* Call free_pte_table() in remove_pmd_table(). */
941 flush_tlb_all();
942 if (direct)
943 update_page_count(PG_LEVEL_4K, -pages);
944}
945
946static void __meminit
947remove_pmd_table(pmd_t *pmd_start, unsigned long addr, unsigned long end,
948 bool direct)
949{
950 unsigned long next, pages = 0;
951 pte_t *pte_base;
952 pmd_t *pmd;
953 void *page_addr;
954
955 pmd = pmd_start + pmd_index(addr);
956 for (; addr < end; addr = next, pmd++) {
957 next = pmd_addr_end(addr, end);
958
959 if (!pmd_present(*pmd))
960 continue;
961
962 if (pmd_large(*pmd)) {
963 if (IS_ALIGNED(addr, PMD_SIZE) &&
964 IS_ALIGNED(next, PMD_SIZE)) {
965 if (!direct)
966 free_pagetable(pmd_page(*pmd),
967 get_order(PMD_SIZE));
968
969 spin_lock(&init_mm.page_table_lock);
970 pmd_clear(pmd);
971 spin_unlock(&init_mm.page_table_lock);
972 pages++;
973 } else {
974 /* If here, we are freeing vmemmap pages. */
975 memset((void *)addr, PAGE_INUSE, next - addr);
976
977 page_addr = page_address(pmd_page(*pmd));
978 if (!memchr_inv(page_addr, PAGE_INUSE,
979 PMD_SIZE)) {
980 free_pagetable(pmd_page(*pmd),
981 get_order(PMD_SIZE));
982
983 spin_lock(&init_mm.page_table_lock);
984 pmd_clear(pmd);
985 spin_unlock(&init_mm.page_table_lock);
986 }
987 }
988
989 continue;
990 }
991
992 pte_base = (pte_t *)pmd_page_vaddr(*pmd);
993 remove_pte_table(pte_base, addr, next, direct);
994 free_pte_table(pte_base, pmd);
995 }
996
997 /* Call free_pmd_table() in remove_pud_table(). */
998 if (direct)
999 update_page_count(PG_LEVEL_2M, -pages);
1000}
1001
1002static void __meminit
1003remove_pud_table(pud_t *pud_start, unsigned long addr, unsigned long end,
1004 bool direct)
1005{
1006 unsigned long next, pages = 0;
1007 pmd_t *pmd_base;
1008 pud_t *pud;
1009 void *page_addr;
1010
1011 pud = pud_start + pud_index(addr);
1012 for (; addr < end; addr = next, pud++) {
1013 next = pud_addr_end(addr, end);
1014
1015 if (!pud_present(*pud))
1016 continue;
1017
1018 if (pud_large(*pud)) {
1019 if (IS_ALIGNED(addr, PUD_SIZE) &&
1020 IS_ALIGNED(next, PUD_SIZE)) {
1021 if (!direct)
1022 free_pagetable(pud_page(*pud),
1023 get_order(PUD_SIZE));
1024
1025 spin_lock(&init_mm.page_table_lock);
1026 pud_clear(pud);
1027 spin_unlock(&init_mm.page_table_lock);
1028 pages++;
1029 } else {
1030 /* If here, we are freeing vmemmap pages. */
1031 memset((void *)addr, PAGE_INUSE, next - addr);
1032
1033 page_addr = page_address(pud_page(*pud));
1034 if (!memchr_inv(page_addr, PAGE_INUSE,
1035 PUD_SIZE)) {
1036 free_pagetable(pud_page(*pud),
1037 get_order(PUD_SIZE));
1038
1039 spin_lock(&init_mm.page_table_lock);
1040 pud_clear(pud);
1041 spin_unlock(&init_mm.page_table_lock);
1042 }
1043 }
1044
1045 continue;
1046 }
1047
Kirill A. Shutemove6ab9c42017-04-25 12:25:57 +03001048 pmd_base = pmd_offset(pud, 0);
Wen Congyangae9aae92013-02-22 16:33:04 -08001049 remove_pmd_table(pmd_base, addr, next, direct);
1050 free_pmd_table(pmd_base, pud);
1051 }
1052
1053 if (direct)
1054 update_page_count(PG_LEVEL_1G, -pages);
1055}
1056
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +03001057static void __meminit
1058remove_p4d_table(p4d_t *p4d_start, unsigned long addr, unsigned long end,
1059 bool direct)
1060{
1061 unsigned long next, pages = 0;
1062 pud_t *pud_base;
1063 p4d_t *p4d;
1064
1065 p4d = p4d_start + p4d_index(addr);
1066 for (; addr < end; addr = next, p4d++) {
1067 next = p4d_addr_end(addr, end);
1068
1069 if (!p4d_present(*p4d))
1070 continue;
1071
1072 BUILD_BUG_ON(p4d_large(*p4d));
1073
Kirill A. Shutemove6ab9c42017-04-25 12:25:57 +03001074 pud_base = pud_offset(p4d, 0);
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +03001075 remove_pud_table(pud_base, addr, next, direct);
Jérôme Glisse98fe3632017-06-24 14:05:14 -04001076 /*
1077 * For 4-level page tables we do not want to free PUDs, but in the
1078 * 5-level case we should free them. This code will have to change
1079 * to adapt for boot-time switching between 4 and 5 level page tables.
1080 */
1081 if (CONFIG_PGTABLE_LEVELS == 5)
1082 free_pud_table(pud_base, p4d);
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +03001083 }
1084
1085 if (direct)
1086 update_page_count(PG_LEVEL_512G, -pages);
1087}
1088
Wen Congyangae9aae92013-02-22 16:33:04 -08001089/* start and end are both virtual address. */
1090static void __meminit
1091remove_pagetable(unsigned long start, unsigned long end, bool direct)
1092{
1093 unsigned long next;
Yasuaki Ishimatsu5255e0a2014-08-22 13:27:31 -07001094 unsigned long addr;
Wen Congyangae9aae92013-02-22 16:33:04 -08001095 pgd_t *pgd;
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +03001096 p4d_t *p4d;
Wen Congyangae9aae92013-02-22 16:33:04 -08001097
Yasuaki Ishimatsu5255e0a2014-08-22 13:27:31 -07001098 for (addr = start; addr < end; addr = next) {
1099 next = pgd_addr_end(addr, end);
Wen Congyangae9aae92013-02-22 16:33:04 -08001100
Yasuaki Ishimatsu5255e0a2014-08-22 13:27:31 -07001101 pgd = pgd_offset_k(addr);
Wen Congyangae9aae92013-02-22 16:33:04 -08001102 if (!pgd_present(*pgd))
1103 continue;
1104
Kirill A. Shutemove6ab9c42017-04-25 12:25:57 +03001105 p4d = p4d_offset(pgd, 0);
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +03001106 remove_p4d_table(p4d, addr, next, direct);
Wen Congyangae9aae92013-02-22 16:33:04 -08001107 }
1108
Wen Congyangae9aae92013-02-22 16:33:04 -08001109 flush_tlb_all();
1110}
1111
Johannes Weiner0aad8182013-04-29 15:07:50 -07001112void __ref vmemmap_free(unsigned long start, unsigned long end)
Tang Chen01975182013-02-22 16:33:08 -08001113{
Tang Chen01975182013-02-22 16:33:08 -08001114 remove_pagetable(start, end, false);
1115}
1116
Tang Chen587ff8c2013-04-15 17:46:46 +08001117#ifdef CONFIG_MEMORY_HOTREMOVE
Tang Chenbbcab872013-02-22 16:33:06 -08001118static void __meminit
1119kernel_physical_mapping_remove(unsigned long start, unsigned long end)
1120{
1121 start = (unsigned long)__va(start);
1122 end = (unsigned long)__va(end);
1123
1124 remove_pagetable(start, end, true);
1125}
1126
Wen Congyang24d335c2013-02-22 16:32:58 -08001127int __ref arch_remove_memory(u64 start, u64 size)
1128{
1129 unsigned long start_pfn = start >> PAGE_SHIFT;
1130 unsigned long nr_pages = size >> PAGE_SHIFT;
Dan Williams4b94ffd2016-01-15 16:56:22 -08001131 struct page *page = pfn_to_page(start_pfn);
1132 struct vmem_altmap *altmap;
Wen Congyang24d335c2013-02-22 16:32:58 -08001133 struct zone *zone;
1134 int ret;
1135
Dan Williams4b94ffd2016-01-15 16:56:22 -08001136 /* With altmap the first mapped page is offset from @start */
1137 altmap = to_vmem_altmap((unsigned long) page);
1138 if (altmap)
1139 page += vmem_altmap_offset(altmap);
1140 zone = page_zone(page);
Wen Congyang24d335c2013-02-22 16:32:58 -08001141 ret = __remove_pages(zone, start_pfn, nr_pages);
1142 WARN_ON_ONCE(ret);
Dan Williams4b94ffd2016-01-15 16:56:22 -08001143 kernel_physical_mapping_remove(start, start + size);
Wen Congyang24d335c2013-02-22 16:32:58 -08001144
1145 return ret;
1146}
1147#endif
Keith Mannthey45e0b782006-09-30 23:27:09 -07001148#endif /* CONFIG_MEMORY_HOTPLUG */
1149
KAMEZAWA Hiroyuki81ac3ad2009-09-22 16:45:49 -07001150static struct kcore_list kcore_vsyscall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Yinghai Lu94b43c32012-11-16 19:39:19 -08001152static void __init register_page_bootmem_info(void)
1153{
1154#ifdef CONFIG_NUMA
1155 int i;
1156
1157 for_each_online_node(i)
1158 register_page_bootmem_info_node(NODE_DATA(i));
1159#endif
1160}
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162void __init mem_init(void)
1163{
Jon Mason0dc243a2006-06-26 13:58:11 +02001164 pci_iommu_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Yinghai Lu48ddb152008-01-30 13:32:36 +01001166 /* clear_bss() already clear the empty_zero_page */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Yinghai Lu94b43c32012-11-16 19:39:19 -08001168 register_page_bootmem_info();
Jiang Liubced0e32013-04-29 15:06:53 -07001169
1170 /* this will put all memory onto the freelists */
Jiang Liu0c988532013-07-03 15:03:24 -07001171 free_all_bootmem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 after_bootmem = 1;
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 /* Register memory areas for /proc/kcore */
Andy Lutomirskif40c3302014-05-05 12:19:36 -07001175 kclist_add(&kcore_vsyscall, (void *)VSYSCALL_ADDR,
1176 PAGE_SIZE, KCORE_OTHER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Jiang Liu46a84132013-07-03 15:04:19 -07001178 mem_init_print_info(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
1180
Suresh Siddha502f6602009-10-28 18:46:56 -08001181int kernel_set_to_readonly;
Steven Rostedt16239632009-02-17 17:57:30 -05001182
1183void set_kernel_text_rw(void)
1184{
Suresh Siddhab9af7c02009-10-14 14:46:55 -07001185 unsigned long start = PFN_ALIGN(_text);
Suresh Siddhae7d23dd2009-10-28 18:46:58 -08001186 unsigned long end = PFN_ALIGN(__stop___ex_table);
Steven Rostedt16239632009-02-17 17:57:30 -05001187
1188 if (!kernel_set_to_readonly)
1189 return;
1190
1191 pr_debug("Set kernel text: %lx - %lx for read write\n",
1192 start, end);
1193
Suresh Siddhae7d23dd2009-10-28 18:46:58 -08001194 /*
1195 * Make the kernel identity mapping for text RW. Kernel text
1196 * mapping will always be RO. Refer to the comment in
1197 * static_protections() in pageattr.c
1198 */
Steven Rostedt16239632009-02-17 17:57:30 -05001199 set_memory_rw(start, (end - start) >> PAGE_SHIFT);
1200}
1201
1202void set_kernel_text_ro(void)
1203{
Suresh Siddhab9af7c02009-10-14 14:46:55 -07001204 unsigned long start = PFN_ALIGN(_text);
Suresh Siddhae7d23dd2009-10-28 18:46:58 -08001205 unsigned long end = PFN_ALIGN(__stop___ex_table);
Steven Rostedt16239632009-02-17 17:57:30 -05001206
1207 if (!kernel_set_to_readonly)
1208 return;
1209
1210 pr_debug("Set kernel text: %lx - %lx for read only\n",
1211 start, end);
1212
Suresh Siddhae7d23dd2009-10-28 18:46:58 -08001213 /*
1214 * Set the kernel identity mapping for text RO.
1215 */
Steven Rostedt16239632009-02-17 17:57:30 -05001216 set_memory_ro(start, (end - start) >> PAGE_SHIFT);
1217}
1218
Arjan van de Ven67df1972006-01-06 00:12:04 -08001219void mark_rodata_ro(void)
1220{
Suresh Siddha74e08172009-10-14 14:46:56 -07001221 unsigned long start = PFN_ALIGN(_text);
Alexander Duyckfc8d7822012-11-16 13:57:13 -08001222 unsigned long rodata_start = PFN_ALIGN(__start_rodata);
Suresh Siddha74e08172009-10-14 14:46:56 -07001223 unsigned long end = (unsigned long) &__end_rodata_hpage_align;
Alexander Duyckfc8d7822012-11-16 13:57:13 -08001224 unsigned long text_end = PFN_ALIGN(&__stop___ex_table);
1225 unsigned long rodata_end = PFN_ALIGN(&__end_rodata);
Kees Cook45e2a9d2014-11-14 11:47:37 -08001226 unsigned long all_end;
Steven Rostedt8f0f9962008-05-12 21:20:56 +02001227
Jan Beulich6fb14752007-05-02 19:27:10 +02001228 printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
Linus Torvaldse3ebadd2007-05-07 08:44:24 -07001229 (end - start) >> 10);
Arjan van de Ven984bb802008-02-06 22:39:45 +01001230 set_memory_ro(start, (end - start) >> PAGE_SHIFT);
1231
Steven Rostedt16239632009-02-17 17:57:30 -05001232 kernel_set_to_readonly = 1;
1233
Arjan van de Ven984bb802008-02-06 22:39:45 +01001234 /*
Yinghai Lu72212672013-01-24 12:20:13 -08001235 * The rodata/data/bss/brk section (but not the kernel text!)
1236 * should also be not-executable.
Kees Cook45e2a9d2014-11-14 11:47:37 -08001237 *
1238 * We align all_end to PMD_SIZE because the existing mapping
1239 * is a full PMD. If we would align _brk_end to PAGE_SIZE we
1240 * split the PMD and the reminder between _brk_end and the end
1241 * of the PMD will remain mapped executable.
1242 *
1243 * Any PMD which was setup after the one which covers _brk_end
1244 * has been zapped already via cleanup_highmem().
Arjan van de Ven984bb802008-02-06 22:39:45 +01001245 */
Kees Cook45e2a9d2014-11-14 11:47:37 -08001246 all_end = roundup((unsigned long)_brk_end, PMD_SIZE);
Stephen Smalleyab76f7b2015-10-01 09:04:22 -04001247 set_memory_nx(text_end, (all_end - text_end) >> PAGE_SHIFT);
Arjan van de Ven67df1972006-01-06 00:12:04 -08001248
Andi Kleen0c42f392008-01-30 13:33:42 +01001249#ifdef CONFIG_CPA_DEBUG
Ingo Molnar10f22dd2008-01-30 13:34:10 +01001250 printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end);
Arjan van de Ven6d238cc2008-01-30 13:34:06 +01001251 set_memory_rw(start, (end-start) >> PAGE_SHIFT);
Andi Kleen0c42f392008-01-30 13:33:42 +01001252
Ingo Molnar10f22dd2008-01-30 13:34:10 +01001253 printk(KERN_INFO "Testing CPA: again\n");
Arjan van de Ven6d238cc2008-01-30 13:34:06 +01001254 set_memory_ro(start, (end-start) >> PAGE_SHIFT);
Andi Kleen0c42f392008-01-30 13:33:42 +01001255#endif
Suresh Siddha74e08172009-10-14 14:46:56 -07001256
Jiang Liuc88442e2013-07-03 15:02:58 -07001257 free_init_pages("unused kernel",
Alexander Duyckfc8d7822012-11-16 13:57:13 -08001258 (unsigned long) __va(__pa_symbol(text_end)),
1259 (unsigned long) __va(__pa_symbol(rodata_start)));
Jiang Liuc88442e2013-07-03 15:02:58 -07001260 free_init_pages("unused kernel",
Alexander Duyckfc8d7822012-11-16 13:57:13 -08001261 (unsigned long) __va(__pa_symbol(rodata_end)),
1262 (unsigned long) __va(__pa_symbol(_sdata)));
Stephen Smalleye1a58322015-10-05 12:55:20 -04001263
1264 debug_checkwx();
Arjan van de Ven67df1972006-01-06 00:12:04 -08001265}
Mathieu Desnoyers4e4eee02008-02-02 15:42:20 -05001266
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001267int kern_addr_valid(unsigned long addr)
1268{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001270 pgd_t *pgd;
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +03001271 p4d_t *p4d;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001272 pud_t *pud;
1273 pmd_t *pmd;
1274 pte_t *pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 if (above != 0 && above != -1UL)
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001277 return 0;
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 pgd = pgd_offset_k(addr);
1280 if (pgd_none(*pgd))
1281 return 0;
1282
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +03001283 p4d = p4d_offset(pgd, addr);
1284 if (p4d_none(*p4d))
1285 return 0;
1286
1287 pud = pud_offset(p4d, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 if (pud_none(*pud))
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001289 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Mel Gorman0ee364e2013-02-11 14:52:36 +00001291 if (pud_large(*pud))
1292 return pfn_valid(pud_pfn(*pud));
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 pmd = pmd_offset(pud, addr);
1295 if (pmd_none(*pmd))
1296 return 0;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 if (pmd_large(*pmd))
1299 return pfn_valid(pmd_pfn(*pmd));
1300
1301 pte = pte_offset_kernel(pmd, addr);
1302 if (pte_none(*pte))
1303 return 0;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 return pfn_valid(pte_pfn(*pte));
1306}
1307
Yinghai Lu982792c2014-06-04 16:06:32 -07001308static unsigned long probe_memory_block_size(void)
Nathan Fontenot1dc41aa2011-01-20 10:46:15 -06001309{
Seth Jennings43c75f92015-11-30 10:47:43 -06001310 unsigned long bz = MIN_MEMORY_BLOCK_SIZE;
Yinghai Lu982792c2014-06-04 16:06:32 -07001311
Seth Jennings43c75f92015-11-30 10:47:43 -06001312 /* if system is UV or has 64GB of RAM or more, use large blocks */
1313 if (is_uv_system() || ((max_pfn << PAGE_SHIFT) >= (64UL << 30)))
1314 bz = 2UL << 30; /* 2GB */
Nathan Fontenot1dc41aa2011-01-20 10:46:15 -06001315
Seth Jennings43c75f92015-11-30 10:47:43 -06001316 pr_info("x86/mm: Memory block size: %ldMB\n", bz >> 20);
Yinghai Lu982792c2014-06-04 16:06:32 -07001317
1318 return bz;
1319}
1320
1321static unsigned long memory_block_size_probed;
1322unsigned long memory_block_size_bytes(void)
1323{
1324 if (!memory_block_size_probed)
1325 memory_block_size_probed = probe_memory_block_size();
1326
1327 return memory_block_size_probed;
1328}
1329
Christoph Lameter0889eba2007-10-16 01:24:15 -07001330#ifdef CONFIG_SPARSEMEM_VMEMMAP
1331/*
1332 * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
1333 */
Yinghai Luc2b91e22008-04-12 01:19:24 -07001334static long __meminitdata addr_start, addr_end;
1335static void __meminitdata *p_start, *p_end;
1336static int __meminitdata node_start;
1337
Johannes Weinere8216da2013-04-29 15:07:54 -07001338static int __meminit vmemmap_populate_hugepages(unsigned long start,
Dan Williams4b94ffd2016-01-15 16:56:22 -08001339 unsigned long end, int node, struct vmem_altmap *altmap)
Christoph Lameter0889eba2007-10-16 01:24:15 -07001340{
Johannes Weiner0aad8182013-04-29 15:07:50 -07001341 unsigned long addr;
Christoph Lameter0889eba2007-10-16 01:24:15 -07001342 unsigned long next;
1343 pgd_t *pgd;
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +03001344 p4d_t *p4d;
Christoph Lameter0889eba2007-10-16 01:24:15 -07001345 pud_t *pud;
1346 pmd_t *pmd;
1347
Johannes Weiner0aad8182013-04-29 15:07:50 -07001348 for (addr = start; addr < end; addr = next) {
Johannes Weinere8216da2013-04-29 15:07:54 -07001349 next = pmd_addr_end(addr, end);
Christoph Lameter0889eba2007-10-16 01:24:15 -07001350
1351 pgd = vmemmap_pgd_populate(addr, node);
1352 if (!pgd)
1353 return -ENOMEM;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001354
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +03001355 p4d = vmemmap_p4d_populate(pgd, addr, node);
1356 if (!p4d)
1357 return -ENOMEM;
1358
1359 pud = vmemmap_pud_populate(p4d, addr, node);
Christoph Lameter0889eba2007-10-16 01:24:15 -07001360 if (!pud)
1361 return -ENOMEM;
1362
Johannes Weinere8216da2013-04-29 15:07:54 -07001363 pmd = pmd_offset(pud, addr);
1364 if (pmd_none(*pmd)) {
Johannes Weinere8216da2013-04-29 15:07:54 -07001365 void *p;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001366
Dan Williams4b94ffd2016-01-15 16:56:22 -08001367 p = __vmemmap_alloc_block_buf(PMD_SIZE, node, altmap);
Johannes Weiner8e2cdbc2013-04-29 15:07:56 -07001368 if (p) {
1369 pte_t entry;
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -04001370
Johannes Weiner8e2cdbc2013-04-29 15:07:56 -07001371 entry = pfn_pte(__pa(p) >> PAGE_SHIFT,
1372 PAGE_KERNEL_LARGE);
1373 set_pmd(pmd, __pmd(pte_val(entry)));
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -04001374
Johannes Weiner8e2cdbc2013-04-29 15:07:56 -07001375 /* check to see if we have contiguous blocks */
1376 if (p_end != p || node_start != node) {
1377 if (p_start)
Dan Williamsc9cdaeb2015-09-17 16:27:57 -04001378 pr_debug(" [%lx-%lx] PMD -> [%p-%p] on node %d\n",
Johannes Weiner8e2cdbc2013-04-29 15:07:56 -07001379 addr_start, addr_end-1, p_start, p_end-1, node_start);
1380 addr_start = addr;
1381 node_start = node;
1382 p_start = p;
1383 }
1384
1385 addr_end = addr + PMD_SIZE;
1386 p_end = p + PMD_SIZE;
1387 continue;
Dan Williams4b94ffd2016-01-15 16:56:22 -08001388 } else if (altmap)
1389 return -ENOMEM; /* no fallback */
Johannes Weiner8e2cdbc2013-04-29 15:07:56 -07001390 } else if (pmd_large(*pmd)) {
Johannes Weinere8216da2013-04-29 15:07:54 -07001391 vmemmap_verify((pte_t *)pmd, node, addr, next);
Johannes Weiner8e2cdbc2013-04-29 15:07:56 -07001392 continue;
1393 }
1394 pr_warn_once("vmemmap: falling back to regular page backing\n");
1395 if (vmemmap_populate_basepages(addr, next, node))
1396 return -ENOMEM;
Christoph Lameter0889eba2007-10-16 01:24:15 -07001397 }
Christoph Lameter0889eba2007-10-16 01:24:15 -07001398 return 0;
1399}
Yinghai Luc2b91e22008-04-12 01:19:24 -07001400
Johannes Weinere8216da2013-04-29 15:07:54 -07001401int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node)
1402{
Dan Williams4b94ffd2016-01-15 16:56:22 -08001403 struct vmem_altmap *altmap = to_vmem_altmap(start);
Johannes Weinere8216da2013-04-29 15:07:54 -07001404 int err;
1405
Borislav Petkov16bf9222016-03-29 17:42:03 +02001406 if (boot_cpu_has(X86_FEATURE_PSE))
Dan Williams4b94ffd2016-01-15 16:56:22 -08001407 err = vmemmap_populate_hugepages(start, end, node, altmap);
1408 else if (altmap) {
1409 pr_err_once("%s: no cpu support for altmap allocations\n",
1410 __func__);
1411 err = -ENOMEM;
1412 } else
Johannes Weinere8216da2013-04-29 15:07:54 -07001413 err = vmemmap_populate_basepages(start, end, node);
1414 if (!err)
Kirill A. Shutemov5372e152016-12-15 02:44:03 +03001415 sync_global_pgds(start, end - 1);
Johannes Weinere8216da2013-04-29 15:07:54 -07001416 return err;
1417}
1418
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -08001419#if defined(CONFIG_MEMORY_HOTPLUG_SPARSE) && defined(CONFIG_HAVE_BOOTMEM_INFO_NODE)
1420void register_page_bootmem_memmap(unsigned long section_nr,
1421 struct page *start_page, unsigned long size)
1422{
1423 unsigned long addr = (unsigned long)start_page;
1424 unsigned long end = (unsigned long)(start_page + size);
1425 unsigned long next;
1426 pgd_t *pgd;
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +03001427 p4d_t *p4d;
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -08001428 pud_t *pud;
1429 pmd_t *pmd;
1430 unsigned int nr_pages;
1431 struct page *page;
1432
1433 for (; addr < end; addr = next) {
1434 pte_t *pte = NULL;
1435
1436 pgd = pgd_offset_k(addr);
1437 if (pgd_none(*pgd)) {
1438 next = (addr + PAGE_SIZE) & PAGE_MASK;
1439 continue;
1440 }
1441 get_page_bootmem(section_nr, pgd_page(*pgd), MIX_SECTION_INFO);
1442
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +03001443 p4d = p4d_offset(pgd, addr);
1444 if (p4d_none(*p4d)) {
1445 next = (addr + PAGE_SIZE) & PAGE_MASK;
1446 continue;
1447 }
1448 get_page_bootmem(section_nr, p4d_page(*p4d), MIX_SECTION_INFO);
1449
1450 pud = pud_offset(p4d, addr);
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -08001451 if (pud_none(*pud)) {
1452 next = (addr + PAGE_SIZE) & PAGE_MASK;
1453 continue;
1454 }
1455 get_page_bootmem(section_nr, pud_page(*pud), MIX_SECTION_INFO);
1456
Borislav Petkov16bf9222016-03-29 17:42:03 +02001457 if (!boot_cpu_has(X86_FEATURE_PSE)) {
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -08001458 next = (addr + PAGE_SIZE) & PAGE_MASK;
1459 pmd = pmd_offset(pud, addr);
1460 if (pmd_none(*pmd))
1461 continue;
1462 get_page_bootmem(section_nr, pmd_page(*pmd),
1463 MIX_SECTION_INFO);
1464
1465 pte = pte_offset_kernel(pmd, addr);
1466 if (pte_none(*pte))
1467 continue;
1468 get_page_bootmem(section_nr, pte_page(*pte),
1469 SECTION_INFO);
1470 } else {
1471 next = pmd_addr_end(addr, end);
1472
1473 pmd = pmd_offset(pud, addr);
1474 if (pmd_none(*pmd))
1475 continue;
1476
1477 nr_pages = 1 << (get_order(PMD_SIZE));
1478 page = pmd_page(*pmd);
1479 while (nr_pages--)
1480 get_page_bootmem(section_nr, page++,
1481 SECTION_INFO);
1482 }
1483 }
1484}
1485#endif
1486
Yinghai Luc2b91e22008-04-12 01:19:24 -07001487void __meminit vmemmap_populate_print_last(void)
1488{
1489 if (p_start) {
Dan Williamsc9cdaeb2015-09-17 16:27:57 -04001490 pr_debug(" [%lx-%lx] PMD -> [%p-%p] on node %d\n",
Yinghai Luc2b91e22008-04-12 01:19:24 -07001491 addr_start, addr_end-1, p_start, p_end-1, node_start);
1492 p_start = NULL;
1493 p_end = NULL;
1494 node_start = 0;
1495 }
1496}
Christoph Lameter0889eba2007-10-16 01:24:15 -07001497#endif