xref: /openbmc/linux/arch/x86/mm/init_64.c (revision 82df5b73)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/arch/x86_64/mm/init.c
4  *
5  *  Copyright (C) 1995  Linus Torvalds
6  *  Copyright (C) 2000  Pavel Machek <pavel@ucw.cz>
7  *  Copyright (C) 2002,2003 Andi Kleen <ak@suse.de>
8  */
9 
10 #include <linux/signal.h>
11 #include <linux/sched.h>
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/string.h>
15 #include <linux/types.h>
16 #include <linux/ptrace.h>
17 #include <linux/mman.h>
18 #include <linux/mm.h>
19 #include <linux/swap.h>
20 #include <linux/smp.h>
21 #include <linux/init.h>
22 #include <linux/initrd.h>
23 #include <linux/pagemap.h>
24 #include <linux/memblock.h>
25 #include <linux/proc_fs.h>
26 #include <linux/pci.h>
27 #include <linux/pfn.h>
28 #include <linux/poison.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/memory.h>
31 #include <linux/memory_hotplug.h>
32 #include <linux/memremap.h>
33 #include <linux/nmi.h>
34 #include <linux/gfp.h>
35 #include <linux/kcore.h>
36 
37 #include <asm/processor.h>
38 #include <asm/bios_ebda.h>
39 #include <linux/uaccess.h>
40 #include <asm/pgalloc.h>
41 #include <asm/dma.h>
42 #include <asm/fixmap.h>
43 #include <asm/e820/api.h>
44 #include <asm/apic.h>
45 #include <asm/tlb.h>
46 #include <asm/mmu_context.h>
47 #include <asm/proto.h>
48 #include <asm/smp.h>
49 #include <asm/sections.h>
50 #include <asm/kdebug.h>
51 #include <asm/numa.h>
52 #include <asm/set_memory.h>
53 #include <asm/init.h>
54 #include <asm/uv/uv.h>
55 #include <asm/setup.h>
56 #include <asm/ftrace.h>
57 
58 #include "mm_internal.h"
59 
60 #include "ident_map.c"
61 
62 #define DEFINE_POPULATE(fname, type1, type2, init)		\
63 static inline void fname##_init(struct mm_struct *mm,		\
64 		type1##_t *arg1, type2##_t *arg2, bool init)	\
65 {								\
66 	if (init)						\
67 		fname##_safe(mm, arg1, arg2);			\
68 	else							\
69 		fname(mm, arg1, arg2);				\
70 }
71 
72 DEFINE_POPULATE(p4d_populate, p4d, pud, init)
73 DEFINE_POPULATE(pgd_populate, pgd, p4d, init)
74 DEFINE_POPULATE(pud_populate, pud, pmd, init)
75 DEFINE_POPULATE(pmd_populate_kernel, pmd, pte, init)
76 
77 #define DEFINE_ENTRY(type1, type2, init)			\
78 static inline void set_##type1##_init(type1##_t *arg1,		\
79 			type2##_t arg2, bool init)		\
80 {								\
81 	if (init)						\
82 		set_##type1##_safe(arg1, arg2);			\
83 	else							\
84 		set_##type1(arg1, arg2);			\
85 }
86 
87 DEFINE_ENTRY(p4d, p4d, init)
88 DEFINE_ENTRY(pud, pud, init)
89 DEFINE_ENTRY(pmd, pmd, init)
90 DEFINE_ENTRY(pte, pte, init)
91 
92 
93 /*
94  * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the
95  * physical space so we can cache the place of the first one and move
96  * around without checking the pgd every time.
97  */
98 
99 /* Bits supported by the hardware: */
100 pteval_t __supported_pte_mask __read_mostly = ~0;
101 /* Bits allowed in normal kernel mappings: */
102 pteval_t __default_kernel_pte_mask __read_mostly = ~0;
103 EXPORT_SYMBOL_GPL(__supported_pte_mask);
104 /* Used in PAGE_KERNEL_* macros which are reasonably used out-of-tree: */
105 EXPORT_SYMBOL(__default_kernel_pte_mask);
106 
107 int force_personality32;
108 
109 /*
110  * noexec32=on|off
111  * Control non executable heap for 32bit processes.
112  * To control the stack too use noexec=off
113  *
114  * on	PROT_READ does not imply PROT_EXEC for 32-bit processes (default)
115  * off	PROT_READ implies PROT_EXEC
116  */
117 static int __init nonx32_setup(char *str)
118 {
119 	if (!strcmp(str, "on"))
120 		force_personality32 &= ~READ_IMPLIES_EXEC;
121 	else if (!strcmp(str, "off"))
122 		force_personality32 |= READ_IMPLIES_EXEC;
123 	return 1;
124 }
125 __setup("noexec32=", nonx32_setup);
126 
127 static void sync_global_pgds_l5(unsigned long start, unsigned long end)
128 {
129 	unsigned long addr;
130 
131 	for (addr = start; addr <= end; addr = ALIGN(addr + 1, PGDIR_SIZE)) {
132 		const pgd_t *pgd_ref = pgd_offset_k(addr);
133 		struct page *page;
134 
135 		/* Check for overflow */
136 		if (addr < start)
137 			break;
138 
139 		if (pgd_none(*pgd_ref))
140 			continue;
141 
142 		spin_lock(&pgd_lock);
143 		list_for_each_entry(page, &pgd_list, lru) {
144 			pgd_t *pgd;
145 			spinlock_t *pgt_lock;
146 
147 			pgd = (pgd_t *)page_address(page) + pgd_index(addr);
148 			/* the pgt_lock only for Xen */
149 			pgt_lock = &pgd_page_get_mm(page)->page_table_lock;
150 			spin_lock(pgt_lock);
151 
152 			if (!pgd_none(*pgd_ref) && !pgd_none(*pgd))
153 				BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
154 
155 			if (pgd_none(*pgd))
156 				set_pgd(pgd, *pgd_ref);
157 
158 			spin_unlock(pgt_lock);
159 		}
160 		spin_unlock(&pgd_lock);
161 	}
162 }
163 
164 static void sync_global_pgds_l4(unsigned long start, unsigned long end)
165 {
166 	unsigned long addr;
167 
168 	for (addr = start; addr <= end; addr = ALIGN(addr + 1, PGDIR_SIZE)) {
169 		pgd_t *pgd_ref = pgd_offset_k(addr);
170 		const p4d_t *p4d_ref;
171 		struct page *page;
172 
173 		/*
174 		 * With folded p4d, pgd_none() is always false, we need to
175 		 * handle synchonization on p4d level.
176 		 */
177 		MAYBE_BUILD_BUG_ON(pgd_none(*pgd_ref));
178 		p4d_ref = p4d_offset(pgd_ref, addr);
179 
180 		if (p4d_none(*p4d_ref))
181 			continue;
182 
183 		spin_lock(&pgd_lock);
184 		list_for_each_entry(page, &pgd_list, lru) {
185 			pgd_t *pgd;
186 			p4d_t *p4d;
187 			spinlock_t *pgt_lock;
188 
189 			pgd = (pgd_t *)page_address(page) + pgd_index(addr);
190 			p4d = p4d_offset(pgd, addr);
191 			/* the pgt_lock only for Xen */
192 			pgt_lock = &pgd_page_get_mm(page)->page_table_lock;
193 			spin_lock(pgt_lock);
194 
195 			if (!p4d_none(*p4d_ref) && !p4d_none(*p4d))
196 				BUG_ON(p4d_page_vaddr(*p4d)
197 				       != p4d_page_vaddr(*p4d_ref));
198 
199 			if (p4d_none(*p4d))
200 				set_p4d(p4d, *p4d_ref);
201 
202 			spin_unlock(pgt_lock);
203 		}
204 		spin_unlock(&pgd_lock);
205 	}
206 }
207 
208 /*
209  * When memory was added make sure all the processes MM have
210  * suitable PGD entries in the local PGD level page.
211  */
212 void sync_global_pgds(unsigned long start, unsigned long end)
213 {
214 	if (pgtable_l5_enabled())
215 		sync_global_pgds_l5(start, end);
216 	else
217 		sync_global_pgds_l4(start, end);
218 }
219 
220 void arch_sync_kernel_mappings(unsigned long start, unsigned long end)
221 {
222 	sync_global_pgds(start, end);
223 }
224 
225 /*
226  * NOTE: This function is marked __ref because it calls __init function
227  * (alloc_bootmem_pages). It's safe to do it ONLY when after_bootmem == 0.
228  */
229 static __ref void *spp_getpage(void)
230 {
231 	void *ptr;
232 
233 	if (after_bootmem)
234 		ptr = (void *) get_zeroed_page(GFP_ATOMIC);
235 	else
236 		ptr = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
237 
238 	if (!ptr || ((unsigned long)ptr & ~PAGE_MASK)) {
239 		panic("set_pte_phys: cannot allocate page data %s\n",
240 			after_bootmem ? "after bootmem" : "");
241 	}
242 
243 	pr_debug("spp_getpage %p\n", ptr);
244 
245 	return ptr;
246 }
247 
248 static p4d_t *fill_p4d(pgd_t *pgd, unsigned long vaddr)
249 {
250 	if (pgd_none(*pgd)) {
251 		p4d_t *p4d = (p4d_t *)spp_getpage();
252 		pgd_populate(&init_mm, pgd, p4d);
253 		if (p4d != p4d_offset(pgd, 0))
254 			printk(KERN_ERR "PAGETABLE BUG #00! %p <-> %p\n",
255 			       p4d, p4d_offset(pgd, 0));
256 	}
257 	return p4d_offset(pgd, vaddr);
258 }
259 
260 static pud_t *fill_pud(p4d_t *p4d, unsigned long vaddr)
261 {
262 	if (p4d_none(*p4d)) {
263 		pud_t *pud = (pud_t *)spp_getpage();
264 		p4d_populate(&init_mm, p4d, pud);
265 		if (pud != pud_offset(p4d, 0))
266 			printk(KERN_ERR "PAGETABLE BUG #01! %p <-> %p\n",
267 			       pud, pud_offset(p4d, 0));
268 	}
269 	return pud_offset(p4d, vaddr);
270 }
271 
272 static pmd_t *fill_pmd(pud_t *pud, unsigned long vaddr)
273 {
274 	if (pud_none(*pud)) {
275 		pmd_t *pmd = (pmd_t *) spp_getpage();
276 		pud_populate(&init_mm, pud, pmd);
277 		if (pmd != pmd_offset(pud, 0))
278 			printk(KERN_ERR "PAGETABLE BUG #02! %p <-> %p\n",
279 			       pmd, pmd_offset(pud, 0));
280 	}
281 	return pmd_offset(pud, vaddr);
282 }
283 
284 static pte_t *fill_pte(pmd_t *pmd, unsigned long vaddr)
285 {
286 	if (pmd_none(*pmd)) {
287 		pte_t *pte = (pte_t *) spp_getpage();
288 		pmd_populate_kernel(&init_mm, pmd, pte);
289 		if (pte != pte_offset_kernel(pmd, 0))
290 			printk(KERN_ERR "PAGETABLE BUG #03!\n");
291 	}
292 	return pte_offset_kernel(pmd, vaddr);
293 }
294 
295 static void __set_pte_vaddr(pud_t *pud, unsigned long vaddr, pte_t new_pte)
296 {
297 	pmd_t *pmd = fill_pmd(pud, vaddr);
298 	pte_t *pte = fill_pte(pmd, vaddr);
299 
300 	set_pte(pte, new_pte);
301 
302 	/*
303 	 * It's enough to flush this one mapping.
304 	 * (PGE mappings get flushed as well)
305 	 */
306 	flush_tlb_one_kernel(vaddr);
307 }
308 
309 void set_pte_vaddr_p4d(p4d_t *p4d_page, unsigned long vaddr, pte_t new_pte)
310 {
311 	p4d_t *p4d = p4d_page + p4d_index(vaddr);
312 	pud_t *pud = fill_pud(p4d, vaddr);
313 
314 	__set_pte_vaddr(pud, vaddr, new_pte);
315 }
316 
317 void set_pte_vaddr_pud(pud_t *pud_page, unsigned long vaddr, pte_t new_pte)
318 {
319 	pud_t *pud = pud_page + pud_index(vaddr);
320 
321 	__set_pte_vaddr(pud, vaddr, new_pte);
322 }
323 
324 void set_pte_vaddr(unsigned long vaddr, pte_t pteval)
325 {
326 	pgd_t *pgd;
327 	p4d_t *p4d_page;
328 
329 	pr_debug("set_pte_vaddr %lx to %lx\n", vaddr, native_pte_val(pteval));
330 
331 	pgd = pgd_offset_k(vaddr);
332 	if (pgd_none(*pgd)) {
333 		printk(KERN_ERR
334 			"PGD FIXMAP MISSING, it should be setup in head.S!\n");
335 		return;
336 	}
337 
338 	p4d_page = p4d_offset(pgd, 0);
339 	set_pte_vaddr_p4d(p4d_page, vaddr, pteval);
340 }
341 
342 pmd_t * __init populate_extra_pmd(unsigned long vaddr)
343 {
344 	pgd_t *pgd;
345 	p4d_t *p4d;
346 	pud_t *pud;
347 
348 	pgd = pgd_offset_k(vaddr);
349 	p4d = fill_p4d(pgd, vaddr);
350 	pud = fill_pud(p4d, vaddr);
351 	return fill_pmd(pud, vaddr);
352 }
353 
354 pte_t * __init populate_extra_pte(unsigned long vaddr)
355 {
356 	pmd_t *pmd;
357 
358 	pmd = populate_extra_pmd(vaddr);
359 	return fill_pte(pmd, vaddr);
360 }
361 
362 /*
363  * Create large page table mappings for a range of physical addresses.
364  */
365 static void __init __init_extra_mapping(unsigned long phys, unsigned long size,
366 					enum page_cache_mode cache)
367 {
368 	pgd_t *pgd;
369 	p4d_t *p4d;
370 	pud_t *pud;
371 	pmd_t *pmd;
372 	pgprot_t prot;
373 
374 	pgprot_val(prot) = pgprot_val(PAGE_KERNEL_LARGE) |
375 		protval_4k_2_large(cachemode2protval(cache));
376 	BUG_ON((phys & ~PMD_MASK) || (size & ~PMD_MASK));
377 	for (; size; phys += PMD_SIZE, size -= PMD_SIZE) {
378 		pgd = pgd_offset_k((unsigned long)__va(phys));
379 		if (pgd_none(*pgd)) {
380 			p4d = (p4d_t *) spp_getpage();
381 			set_pgd(pgd, __pgd(__pa(p4d) | _KERNPG_TABLE |
382 						_PAGE_USER));
383 		}
384 		p4d = p4d_offset(pgd, (unsigned long)__va(phys));
385 		if (p4d_none(*p4d)) {
386 			pud = (pud_t *) spp_getpage();
387 			set_p4d(p4d, __p4d(__pa(pud) | _KERNPG_TABLE |
388 						_PAGE_USER));
389 		}
390 		pud = pud_offset(p4d, (unsigned long)__va(phys));
391 		if (pud_none(*pud)) {
392 			pmd = (pmd_t *) spp_getpage();
393 			set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE |
394 						_PAGE_USER));
395 		}
396 		pmd = pmd_offset(pud, phys);
397 		BUG_ON(!pmd_none(*pmd));
398 		set_pmd(pmd, __pmd(phys | pgprot_val(prot)));
399 	}
400 }
401 
402 void __init init_extra_mapping_wb(unsigned long phys, unsigned long size)
403 {
404 	__init_extra_mapping(phys, size, _PAGE_CACHE_MODE_WB);
405 }
406 
407 void __init init_extra_mapping_uc(unsigned long phys, unsigned long size)
408 {
409 	__init_extra_mapping(phys, size, _PAGE_CACHE_MODE_UC);
410 }
411 
412 /*
413  * The head.S code sets up the kernel high mapping:
414  *
415  *   from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text)
416  *
417  * phys_base holds the negative offset to the kernel, which is added
418  * to the compile time generated pmds. This results in invalid pmds up
419  * to the point where we hit the physaddr 0 mapping.
420  *
421  * We limit the mappings to the region from _text to _brk_end.  _brk_end
422  * is rounded up to the 2MB boundary. This catches the invalid pmds as
423  * well, as they are located before _text:
424  */
425 void __init cleanup_highmap(void)
426 {
427 	unsigned long vaddr = __START_KERNEL_map;
428 	unsigned long vaddr_end = __START_KERNEL_map + KERNEL_IMAGE_SIZE;
429 	unsigned long end = roundup((unsigned long)_brk_end, PMD_SIZE) - 1;
430 	pmd_t *pmd = level2_kernel_pgt;
431 
432 	/*
433 	 * Native path, max_pfn_mapped is not set yet.
434 	 * Xen has valid max_pfn_mapped set in
435 	 *	arch/x86/xen/mmu.c:xen_setup_kernel_pagetable().
436 	 */
437 	if (max_pfn_mapped)
438 		vaddr_end = __START_KERNEL_map + (max_pfn_mapped << PAGE_SHIFT);
439 
440 	for (; vaddr + PMD_SIZE - 1 < vaddr_end; pmd++, vaddr += PMD_SIZE) {
441 		if (pmd_none(*pmd))
442 			continue;
443 		if (vaddr < (unsigned long) _text || vaddr > end)
444 			set_pmd(pmd, __pmd(0));
445 	}
446 }
447 
448 /*
449  * Create PTE level page table mapping for physical addresses.
450  * It returns the last physical address mapped.
451  */
452 static unsigned long __meminit
453 phys_pte_init(pte_t *pte_page, unsigned long paddr, unsigned long paddr_end,
454 	      pgprot_t prot, bool init)
455 {
456 	unsigned long pages = 0, paddr_next;
457 	unsigned long paddr_last = paddr_end;
458 	pte_t *pte;
459 	int i;
460 
461 	pte = pte_page + pte_index(paddr);
462 	i = pte_index(paddr);
463 
464 	for (; i < PTRS_PER_PTE; i++, paddr = paddr_next, pte++) {
465 		paddr_next = (paddr & PAGE_MASK) + PAGE_SIZE;
466 		if (paddr >= paddr_end) {
467 			if (!after_bootmem &&
468 			    !e820__mapped_any(paddr & PAGE_MASK, paddr_next,
469 					     E820_TYPE_RAM) &&
470 			    !e820__mapped_any(paddr & PAGE_MASK, paddr_next,
471 					     E820_TYPE_RESERVED_KERN))
472 				set_pte_init(pte, __pte(0), init);
473 			continue;
474 		}
475 
476 		/*
477 		 * We will re-use the existing mapping.
478 		 * Xen for example has some special requirements, like mapping
479 		 * pagetable pages as RO. So assume someone who pre-setup
480 		 * these mappings are more intelligent.
481 		 */
482 		if (!pte_none(*pte)) {
483 			if (!after_bootmem)
484 				pages++;
485 			continue;
486 		}
487 
488 		if (0)
489 			pr_info("   pte=%p addr=%lx pte=%016lx\n", pte, paddr,
490 				pfn_pte(paddr >> PAGE_SHIFT, PAGE_KERNEL).pte);
491 		pages++;
492 		set_pte_init(pte, pfn_pte(paddr >> PAGE_SHIFT, prot), init);
493 		paddr_last = (paddr & PAGE_MASK) + PAGE_SIZE;
494 	}
495 
496 	update_page_count(PG_LEVEL_4K, pages);
497 
498 	return paddr_last;
499 }
500 
501 /*
502  * Create PMD level page table mapping for physical addresses. The virtual
503  * and physical address have to be aligned at this level.
504  * It returns the last physical address mapped.
505  */
506 static unsigned long __meminit
507 phys_pmd_init(pmd_t *pmd_page, unsigned long paddr, unsigned long paddr_end,
508 	      unsigned long page_size_mask, pgprot_t prot, bool init)
509 {
510 	unsigned long pages = 0, paddr_next;
511 	unsigned long paddr_last = paddr_end;
512 
513 	int i = pmd_index(paddr);
514 
515 	for (; i < PTRS_PER_PMD; i++, paddr = paddr_next) {
516 		pmd_t *pmd = pmd_page + pmd_index(paddr);
517 		pte_t *pte;
518 		pgprot_t new_prot = prot;
519 
520 		paddr_next = (paddr & PMD_MASK) + PMD_SIZE;
521 		if (paddr >= paddr_end) {
522 			if (!after_bootmem &&
523 			    !e820__mapped_any(paddr & PMD_MASK, paddr_next,
524 					     E820_TYPE_RAM) &&
525 			    !e820__mapped_any(paddr & PMD_MASK, paddr_next,
526 					     E820_TYPE_RESERVED_KERN))
527 				set_pmd_init(pmd, __pmd(0), init);
528 			continue;
529 		}
530 
531 		if (!pmd_none(*pmd)) {
532 			if (!pmd_large(*pmd)) {
533 				spin_lock(&init_mm.page_table_lock);
534 				pte = (pte_t *)pmd_page_vaddr(*pmd);
535 				paddr_last = phys_pte_init(pte, paddr,
536 							   paddr_end, prot,
537 							   init);
538 				spin_unlock(&init_mm.page_table_lock);
539 				continue;
540 			}
541 			/*
542 			 * If we are ok with PG_LEVEL_2M mapping, then we will
543 			 * use the existing mapping,
544 			 *
545 			 * Otherwise, we will split the large page mapping but
546 			 * use the same existing protection bits except for
547 			 * large page, so that we don't violate Intel's TLB
548 			 * Application note (317080) which says, while changing
549 			 * the page sizes, new and old translations should
550 			 * not differ with respect to page frame and
551 			 * attributes.
552 			 */
553 			if (page_size_mask & (1 << PG_LEVEL_2M)) {
554 				if (!after_bootmem)
555 					pages++;
556 				paddr_last = paddr_next;
557 				continue;
558 			}
559 			new_prot = pte_pgprot(pte_clrhuge(*(pte_t *)pmd));
560 		}
561 
562 		if (page_size_mask & (1<<PG_LEVEL_2M)) {
563 			pages++;
564 			spin_lock(&init_mm.page_table_lock);
565 			set_pte_init((pte_t *)pmd,
566 				     pfn_pte((paddr & PMD_MASK) >> PAGE_SHIFT,
567 					     __pgprot(pgprot_val(prot) | _PAGE_PSE)),
568 				     init);
569 			spin_unlock(&init_mm.page_table_lock);
570 			paddr_last = paddr_next;
571 			continue;
572 		}
573 
574 		pte = alloc_low_page();
575 		paddr_last = phys_pte_init(pte, paddr, paddr_end, new_prot, init);
576 
577 		spin_lock(&init_mm.page_table_lock);
578 		pmd_populate_kernel_init(&init_mm, pmd, pte, init);
579 		spin_unlock(&init_mm.page_table_lock);
580 	}
581 	update_page_count(PG_LEVEL_2M, pages);
582 	return paddr_last;
583 }
584 
585 /*
586  * Create PUD level page table mapping for physical addresses. The virtual
587  * and physical address do not have to be aligned at this level. KASLR can
588  * randomize virtual addresses up to this level.
589  * It returns the last physical address mapped.
590  */
591 static unsigned long __meminit
592 phys_pud_init(pud_t *pud_page, unsigned long paddr, unsigned long paddr_end,
593 	      unsigned long page_size_mask, pgprot_t _prot, bool init)
594 {
595 	unsigned long pages = 0, paddr_next;
596 	unsigned long paddr_last = paddr_end;
597 	unsigned long vaddr = (unsigned long)__va(paddr);
598 	int i = pud_index(vaddr);
599 
600 	for (; i < PTRS_PER_PUD; i++, paddr = paddr_next) {
601 		pud_t *pud;
602 		pmd_t *pmd;
603 		pgprot_t prot = _prot;
604 
605 		vaddr = (unsigned long)__va(paddr);
606 		pud = pud_page + pud_index(vaddr);
607 		paddr_next = (paddr & PUD_MASK) + PUD_SIZE;
608 
609 		if (paddr >= paddr_end) {
610 			if (!after_bootmem &&
611 			    !e820__mapped_any(paddr & PUD_MASK, paddr_next,
612 					     E820_TYPE_RAM) &&
613 			    !e820__mapped_any(paddr & PUD_MASK, paddr_next,
614 					     E820_TYPE_RESERVED_KERN))
615 				set_pud_init(pud, __pud(0), init);
616 			continue;
617 		}
618 
619 		if (!pud_none(*pud)) {
620 			if (!pud_large(*pud)) {
621 				pmd = pmd_offset(pud, 0);
622 				paddr_last = phys_pmd_init(pmd, paddr,
623 							   paddr_end,
624 							   page_size_mask,
625 							   prot, init);
626 				continue;
627 			}
628 			/*
629 			 * If we are ok with PG_LEVEL_1G mapping, then we will
630 			 * use the existing mapping.
631 			 *
632 			 * Otherwise, we will split the gbpage mapping but use
633 			 * the same existing protection  bits except for large
634 			 * page, so that we don't violate Intel's TLB
635 			 * Application note (317080) which says, while changing
636 			 * the page sizes, new and old translations should
637 			 * not differ with respect to page frame and
638 			 * attributes.
639 			 */
640 			if (page_size_mask & (1 << PG_LEVEL_1G)) {
641 				if (!after_bootmem)
642 					pages++;
643 				paddr_last = paddr_next;
644 				continue;
645 			}
646 			prot = pte_pgprot(pte_clrhuge(*(pte_t *)pud));
647 		}
648 
649 		if (page_size_mask & (1<<PG_LEVEL_1G)) {
650 			pages++;
651 			spin_lock(&init_mm.page_table_lock);
652 
653 			prot = __pgprot(pgprot_val(prot) | __PAGE_KERNEL_LARGE);
654 
655 			set_pte_init((pte_t *)pud,
656 				     pfn_pte((paddr & PUD_MASK) >> PAGE_SHIFT,
657 					     prot),
658 				     init);
659 			spin_unlock(&init_mm.page_table_lock);
660 			paddr_last = paddr_next;
661 			continue;
662 		}
663 
664 		pmd = alloc_low_page();
665 		paddr_last = phys_pmd_init(pmd, paddr, paddr_end,
666 					   page_size_mask, prot, init);
667 
668 		spin_lock(&init_mm.page_table_lock);
669 		pud_populate_init(&init_mm, pud, pmd, init);
670 		spin_unlock(&init_mm.page_table_lock);
671 	}
672 
673 	update_page_count(PG_LEVEL_1G, pages);
674 
675 	return paddr_last;
676 }
677 
678 static unsigned long __meminit
679 phys_p4d_init(p4d_t *p4d_page, unsigned long paddr, unsigned long paddr_end,
680 	      unsigned long page_size_mask, pgprot_t prot, bool init)
681 {
682 	unsigned long vaddr, vaddr_end, vaddr_next, paddr_next, paddr_last;
683 
684 	paddr_last = paddr_end;
685 	vaddr = (unsigned long)__va(paddr);
686 	vaddr_end = (unsigned long)__va(paddr_end);
687 
688 	if (!pgtable_l5_enabled())
689 		return phys_pud_init((pud_t *) p4d_page, paddr, paddr_end,
690 				     page_size_mask, prot, init);
691 
692 	for (; vaddr < vaddr_end; vaddr = vaddr_next) {
693 		p4d_t *p4d = p4d_page + p4d_index(vaddr);
694 		pud_t *pud;
695 
696 		vaddr_next = (vaddr & P4D_MASK) + P4D_SIZE;
697 		paddr = __pa(vaddr);
698 
699 		if (paddr >= paddr_end) {
700 			paddr_next = __pa(vaddr_next);
701 			if (!after_bootmem &&
702 			    !e820__mapped_any(paddr & P4D_MASK, paddr_next,
703 					     E820_TYPE_RAM) &&
704 			    !e820__mapped_any(paddr & P4D_MASK, paddr_next,
705 					     E820_TYPE_RESERVED_KERN))
706 				set_p4d_init(p4d, __p4d(0), init);
707 			continue;
708 		}
709 
710 		if (!p4d_none(*p4d)) {
711 			pud = pud_offset(p4d, 0);
712 			paddr_last = phys_pud_init(pud, paddr, __pa(vaddr_end),
713 					page_size_mask, prot, init);
714 			continue;
715 		}
716 
717 		pud = alloc_low_page();
718 		paddr_last = phys_pud_init(pud, paddr, __pa(vaddr_end),
719 					   page_size_mask, prot, init);
720 
721 		spin_lock(&init_mm.page_table_lock);
722 		p4d_populate_init(&init_mm, p4d, pud, init);
723 		spin_unlock(&init_mm.page_table_lock);
724 	}
725 
726 	return paddr_last;
727 }
728 
729 static unsigned long __meminit
730 __kernel_physical_mapping_init(unsigned long paddr_start,
731 			       unsigned long paddr_end,
732 			       unsigned long page_size_mask,
733 			       pgprot_t prot, bool init)
734 {
735 	bool pgd_changed = false;
736 	unsigned long vaddr, vaddr_start, vaddr_end, vaddr_next, paddr_last;
737 
738 	paddr_last = paddr_end;
739 	vaddr = (unsigned long)__va(paddr_start);
740 	vaddr_end = (unsigned long)__va(paddr_end);
741 	vaddr_start = vaddr;
742 
743 	for (; vaddr < vaddr_end; vaddr = vaddr_next) {
744 		pgd_t *pgd = pgd_offset_k(vaddr);
745 		p4d_t *p4d;
746 
747 		vaddr_next = (vaddr & PGDIR_MASK) + PGDIR_SIZE;
748 
749 		if (pgd_val(*pgd)) {
750 			p4d = (p4d_t *)pgd_page_vaddr(*pgd);
751 			paddr_last = phys_p4d_init(p4d, __pa(vaddr),
752 						   __pa(vaddr_end),
753 						   page_size_mask,
754 						   prot, init);
755 			continue;
756 		}
757 
758 		p4d = alloc_low_page();
759 		paddr_last = phys_p4d_init(p4d, __pa(vaddr), __pa(vaddr_end),
760 					   page_size_mask, prot, init);
761 
762 		spin_lock(&init_mm.page_table_lock);
763 		if (pgtable_l5_enabled())
764 			pgd_populate_init(&init_mm, pgd, p4d, init);
765 		else
766 			p4d_populate_init(&init_mm, p4d_offset(pgd, vaddr),
767 					  (pud_t *) p4d, init);
768 
769 		spin_unlock(&init_mm.page_table_lock);
770 		pgd_changed = true;
771 	}
772 
773 	if (pgd_changed)
774 		sync_global_pgds(vaddr_start, vaddr_end - 1);
775 
776 	return paddr_last;
777 }
778 
779 
780 /*
781  * Create page table mapping for the physical memory for specific physical
782  * addresses. Note that it can only be used to populate non-present entries.
783  * The virtual and physical addresses have to be aligned on PMD level
784  * down. It returns the last physical address mapped.
785  */
786 unsigned long __meminit
787 kernel_physical_mapping_init(unsigned long paddr_start,
788 			     unsigned long paddr_end,
789 			     unsigned long page_size_mask, pgprot_t prot)
790 {
791 	return __kernel_physical_mapping_init(paddr_start, paddr_end,
792 					      page_size_mask, prot, true);
793 }
794 
795 /*
796  * This function is similar to kernel_physical_mapping_init() above with the
797  * exception that it uses set_{pud,pmd}() instead of the set_{pud,pte}_safe()
798  * when updating the mapping. The caller is responsible to flush the TLBs after
799  * the function returns.
800  */
801 unsigned long __meminit
802 kernel_physical_mapping_change(unsigned long paddr_start,
803 			       unsigned long paddr_end,
804 			       unsigned long page_size_mask)
805 {
806 	return __kernel_physical_mapping_init(paddr_start, paddr_end,
807 					      page_size_mask, PAGE_KERNEL,
808 					      false);
809 }
810 
811 #ifndef CONFIG_NUMA
812 void __init initmem_init(void)
813 {
814 	memblock_set_node(0, PHYS_ADDR_MAX, &memblock.memory, 0);
815 }
816 #endif
817 
818 void __init paging_init(void)
819 {
820 	sparse_memory_present_with_active_regions(MAX_NUMNODES);
821 	sparse_init();
822 
823 	/*
824 	 * clear the default setting with node 0
825 	 * note: don't use nodes_clear here, that is really clearing when
826 	 *	 numa support is not compiled in, and later node_set_state
827 	 *	 will not set it back.
828 	 */
829 	node_clear_state(0, N_MEMORY);
830 	node_clear_state(0, N_NORMAL_MEMORY);
831 
832 	zone_sizes_init();
833 }
834 
835 /*
836  * Memory hotplug specific functions
837  */
838 #ifdef CONFIG_MEMORY_HOTPLUG
839 /*
840  * After memory hotplug the variables max_pfn, max_low_pfn and high_memory need
841  * updating.
842  */
843 static void update_end_of_memory_vars(u64 start, u64 size)
844 {
845 	unsigned long end_pfn = PFN_UP(start + size);
846 
847 	if (end_pfn > max_pfn) {
848 		max_pfn = end_pfn;
849 		max_low_pfn = end_pfn;
850 		high_memory = (void *)__va(max_pfn * PAGE_SIZE - 1) + 1;
851 	}
852 }
853 
854 int add_pages(int nid, unsigned long start_pfn, unsigned long nr_pages,
855 	      struct mhp_params *params)
856 {
857 	int ret;
858 
859 	ret = __add_pages(nid, start_pfn, nr_pages, params);
860 	WARN_ON_ONCE(ret);
861 
862 	/* update max_pfn, max_low_pfn and high_memory */
863 	update_end_of_memory_vars(start_pfn << PAGE_SHIFT,
864 				  nr_pages << PAGE_SHIFT);
865 
866 	return ret;
867 }
868 
869 int arch_add_memory(int nid, u64 start, u64 size,
870 		    struct mhp_params *params)
871 {
872 	unsigned long start_pfn = start >> PAGE_SHIFT;
873 	unsigned long nr_pages = size >> PAGE_SHIFT;
874 
875 	init_memory_mapping(start, start + size, params->pgprot);
876 
877 	return add_pages(nid, start_pfn, nr_pages, params);
878 }
879 
880 #define PAGE_INUSE 0xFD
881 
882 static void __meminit free_pagetable(struct page *page, int order)
883 {
884 	unsigned long magic;
885 	unsigned int nr_pages = 1 << order;
886 
887 	/* bootmem page has reserved flag */
888 	if (PageReserved(page)) {
889 		__ClearPageReserved(page);
890 
891 		magic = (unsigned long)page->freelist;
892 		if (magic == SECTION_INFO || magic == MIX_SECTION_INFO) {
893 			while (nr_pages--)
894 				put_page_bootmem(page++);
895 		} else
896 			while (nr_pages--)
897 				free_reserved_page(page++);
898 	} else
899 		free_pages((unsigned long)page_address(page), order);
900 }
901 
902 static void __meminit free_hugepage_table(struct page *page,
903 		struct vmem_altmap *altmap)
904 {
905 	if (altmap)
906 		vmem_altmap_free(altmap, PMD_SIZE / PAGE_SIZE);
907 	else
908 		free_pagetable(page, get_order(PMD_SIZE));
909 }
910 
911 static void __meminit free_pte_table(pte_t *pte_start, pmd_t *pmd)
912 {
913 	pte_t *pte;
914 	int i;
915 
916 	for (i = 0; i < PTRS_PER_PTE; i++) {
917 		pte = pte_start + i;
918 		if (!pte_none(*pte))
919 			return;
920 	}
921 
922 	/* free a pte talbe */
923 	free_pagetable(pmd_page(*pmd), 0);
924 	spin_lock(&init_mm.page_table_lock);
925 	pmd_clear(pmd);
926 	spin_unlock(&init_mm.page_table_lock);
927 }
928 
929 static void __meminit free_pmd_table(pmd_t *pmd_start, pud_t *pud)
930 {
931 	pmd_t *pmd;
932 	int i;
933 
934 	for (i = 0; i < PTRS_PER_PMD; i++) {
935 		pmd = pmd_start + i;
936 		if (!pmd_none(*pmd))
937 			return;
938 	}
939 
940 	/* free a pmd talbe */
941 	free_pagetable(pud_page(*pud), 0);
942 	spin_lock(&init_mm.page_table_lock);
943 	pud_clear(pud);
944 	spin_unlock(&init_mm.page_table_lock);
945 }
946 
947 static void __meminit free_pud_table(pud_t *pud_start, p4d_t *p4d)
948 {
949 	pud_t *pud;
950 	int i;
951 
952 	for (i = 0; i < PTRS_PER_PUD; i++) {
953 		pud = pud_start + i;
954 		if (!pud_none(*pud))
955 			return;
956 	}
957 
958 	/* free a pud talbe */
959 	free_pagetable(p4d_page(*p4d), 0);
960 	spin_lock(&init_mm.page_table_lock);
961 	p4d_clear(p4d);
962 	spin_unlock(&init_mm.page_table_lock);
963 }
964 
965 static void __meminit
966 remove_pte_table(pte_t *pte_start, unsigned long addr, unsigned long end,
967 		 bool direct)
968 {
969 	unsigned long next, pages = 0;
970 	pte_t *pte;
971 	void *page_addr;
972 	phys_addr_t phys_addr;
973 
974 	pte = pte_start + pte_index(addr);
975 	for (; addr < end; addr = next, pte++) {
976 		next = (addr + PAGE_SIZE) & PAGE_MASK;
977 		if (next > end)
978 			next = end;
979 
980 		if (!pte_present(*pte))
981 			continue;
982 
983 		/*
984 		 * We mapped [0,1G) memory as identity mapping when
985 		 * initializing, in arch/x86/kernel/head_64.S. These
986 		 * pagetables cannot be removed.
987 		 */
988 		phys_addr = pte_val(*pte) + (addr & PAGE_MASK);
989 		if (phys_addr < (phys_addr_t)0x40000000)
990 			return;
991 
992 		if (PAGE_ALIGNED(addr) && PAGE_ALIGNED(next)) {
993 			/*
994 			 * Do not free direct mapping pages since they were
995 			 * freed when offlining, or simplely not in use.
996 			 */
997 			if (!direct)
998 				free_pagetable(pte_page(*pte), 0);
999 
1000 			spin_lock(&init_mm.page_table_lock);
1001 			pte_clear(&init_mm, addr, pte);
1002 			spin_unlock(&init_mm.page_table_lock);
1003 
1004 			/* For non-direct mapping, pages means nothing. */
1005 			pages++;
1006 		} else {
1007 			/*
1008 			 * If we are here, we are freeing vmemmap pages since
1009 			 * direct mapped memory ranges to be freed are aligned.
1010 			 *
1011 			 * If we are not removing the whole page, it means
1012 			 * other page structs in this page are being used and
1013 			 * we canot remove them. So fill the unused page_structs
1014 			 * with 0xFD, and remove the page when it is wholly
1015 			 * filled with 0xFD.
1016 			 */
1017 			memset((void *)addr, PAGE_INUSE, next - addr);
1018 
1019 			page_addr = page_address(pte_page(*pte));
1020 			if (!memchr_inv(page_addr, PAGE_INUSE, PAGE_SIZE)) {
1021 				free_pagetable(pte_page(*pte), 0);
1022 
1023 				spin_lock(&init_mm.page_table_lock);
1024 				pte_clear(&init_mm, addr, pte);
1025 				spin_unlock(&init_mm.page_table_lock);
1026 			}
1027 		}
1028 	}
1029 
1030 	/* Call free_pte_table() in remove_pmd_table(). */
1031 	flush_tlb_all();
1032 	if (direct)
1033 		update_page_count(PG_LEVEL_4K, -pages);
1034 }
1035 
1036 static void __meminit
1037 remove_pmd_table(pmd_t *pmd_start, unsigned long addr, unsigned long end,
1038 		 bool direct, struct vmem_altmap *altmap)
1039 {
1040 	unsigned long next, pages = 0;
1041 	pte_t *pte_base;
1042 	pmd_t *pmd;
1043 	void *page_addr;
1044 
1045 	pmd = pmd_start + pmd_index(addr);
1046 	for (; addr < end; addr = next, pmd++) {
1047 		next = pmd_addr_end(addr, end);
1048 
1049 		if (!pmd_present(*pmd))
1050 			continue;
1051 
1052 		if (pmd_large(*pmd)) {
1053 			if (IS_ALIGNED(addr, PMD_SIZE) &&
1054 			    IS_ALIGNED(next, PMD_SIZE)) {
1055 				if (!direct)
1056 					free_hugepage_table(pmd_page(*pmd),
1057 							    altmap);
1058 
1059 				spin_lock(&init_mm.page_table_lock);
1060 				pmd_clear(pmd);
1061 				spin_unlock(&init_mm.page_table_lock);
1062 				pages++;
1063 			} else {
1064 				/* If here, we are freeing vmemmap pages. */
1065 				memset((void *)addr, PAGE_INUSE, next - addr);
1066 
1067 				page_addr = page_address(pmd_page(*pmd));
1068 				if (!memchr_inv(page_addr, PAGE_INUSE,
1069 						PMD_SIZE)) {
1070 					free_hugepage_table(pmd_page(*pmd),
1071 							    altmap);
1072 
1073 					spin_lock(&init_mm.page_table_lock);
1074 					pmd_clear(pmd);
1075 					spin_unlock(&init_mm.page_table_lock);
1076 				}
1077 			}
1078 
1079 			continue;
1080 		}
1081 
1082 		pte_base = (pte_t *)pmd_page_vaddr(*pmd);
1083 		remove_pte_table(pte_base, addr, next, direct);
1084 		free_pte_table(pte_base, pmd);
1085 	}
1086 
1087 	/* Call free_pmd_table() in remove_pud_table(). */
1088 	if (direct)
1089 		update_page_count(PG_LEVEL_2M, -pages);
1090 }
1091 
1092 static void __meminit
1093 remove_pud_table(pud_t *pud_start, unsigned long addr, unsigned long end,
1094 		 struct vmem_altmap *altmap, bool direct)
1095 {
1096 	unsigned long next, pages = 0;
1097 	pmd_t *pmd_base;
1098 	pud_t *pud;
1099 	void *page_addr;
1100 
1101 	pud = pud_start + pud_index(addr);
1102 	for (; addr < end; addr = next, pud++) {
1103 		next = pud_addr_end(addr, end);
1104 
1105 		if (!pud_present(*pud))
1106 			continue;
1107 
1108 		if (pud_large(*pud)) {
1109 			if (IS_ALIGNED(addr, PUD_SIZE) &&
1110 			    IS_ALIGNED(next, PUD_SIZE)) {
1111 				if (!direct)
1112 					free_pagetable(pud_page(*pud),
1113 						       get_order(PUD_SIZE));
1114 
1115 				spin_lock(&init_mm.page_table_lock);
1116 				pud_clear(pud);
1117 				spin_unlock(&init_mm.page_table_lock);
1118 				pages++;
1119 			} else {
1120 				/* If here, we are freeing vmemmap pages. */
1121 				memset((void *)addr, PAGE_INUSE, next - addr);
1122 
1123 				page_addr = page_address(pud_page(*pud));
1124 				if (!memchr_inv(page_addr, PAGE_INUSE,
1125 						PUD_SIZE)) {
1126 					free_pagetable(pud_page(*pud),
1127 						       get_order(PUD_SIZE));
1128 
1129 					spin_lock(&init_mm.page_table_lock);
1130 					pud_clear(pud);
1131 					spin_unlock(&init_mm.page_table_lock);
1132 				}
1133 			}
1134 
1135 			continue;
1136 		}
1137 
1138 		pmd_base = pmd_offset(pud, 0);
1139 		remove_pmd_table(pmd_base, addr, next, direct, altmap);
1140 		free_pmd_table(pmd_base, pud);
1141 	}
1142 
1143 	if (direct)
1144 		update_page_count(PG_LEVEL_1G, -pages);
1145 }
1146 
1147 static void __meminit
1148 remove_p4d_table(p4d_t *p4d_start, unsigned long addr, unsigned long end,
1149 		 struct vmem_altmap *altmap, bool direct)
1150 {
1151 	unsigned long next, pages = 0;
1152 	pud_t *pud_base;
1153 	p4d_t *p4d;
1154 
1155 	p4d = p4d_start + p4d_index(addr);
1156 	for (; addr < end; addr = next, p4d++) {
1157 		next = p4d_addr_end(addr, end);
1158 
1159 		if (!p4d_present(*p4d))
1160 			continue;
1161 
1162 		BUILD_BUG_ON(p4d_large(*p4d));
1163 
1164 		pud_base = pud_offset(p4d, 0);
1165 		remove_pud_table(pud_base, addr, next, altmap, direct);
1166 		/*
1167 		 * For 4-level page tables we do not want to free PUDs, but in the
1168 		 * 5-level case we should free them. This code will have to change
1169 		 * to adapt for boot-time switching between 4 and 5 level page tables.
1170 		 */
1171 		if (pgtable_l5_enabled())
1172 			free_pud_table(pud_base, p4d);
1173 	}
1174 
1175 	if (direct)
1176 		update_page_count(PG_LEVEL_512G, -pages);
1177 }
1178 
1179 /* start and end are both virtual address. */
1180 static void __meminit
1181 remove_pagetable(unsigned long start, unsigned long end, bool direct,
1182 		struct vmem_altmap *altmap)
1183 {
1184 	unsigned long next;
1185 	unsigned long addr;
1186 	pgd_t *pgd;
1187 	p4d_t *p4d;
1188 
1189 	for (addr = start; addr < end; addr = next) {
1190 		next = pgd_addr_end(addr, end);
1191 
1192 		pgd = pgd_offset_k(addr);
1193 		if (!pgd_present(*pgd))
1194 			continue;
1195 
1196 		p4d = p4d_offset(pgd, 0);
1197 		remove_p4d_table(p4d, addr, next, altmap, direct);
1198 	}
1199 
1200 	flush_tlb_all();
1201 }
1202 
1203 void __ref vmemmap_free(unsigned long start, unsigned long end,
1204 		struct vmem_altmap *altmap)
1205 {
1206 	remove_pagetable(start, end, false, altmap);
1207 }
1208 
1209 static void __meminit
1210 kernel_physical_mapping_remove(unsigned long start, unsigned long end)
1211 {
1212 	start = (unsigned long)__va(start);
1213 	end = (unsigned long)__va(end);
1214 
1215 	remove_pagetable(start, end, true, NULL);
1216 }
1217 
1218 void __ref arch_remove_memory(int nid, u64 start, u64 size,
1219 			      struct vmem_altmap *altmap)
1220 {
1221 	unsigned long start_pfn = start >> PAGE_SHIFT;
1222 	unsigned long nr_pages = size >> PAGE_SHIFT;
1223 
1224 	__remove_pages(start_pfn, nr_pages, altmap);
1225 	kernel_physical_mapping_remove(start, start + size);
1226 }
1227 #endif /* CONFIG_MEMORY_HOTPLUG */
1228 
1229 static struct kcore_list kcore_vsyscall;
1230 
1231 static void __init register_page_bootmem_info(void)
1232 {
1233 #ifdef CONFIG_NUMA
1234 	int i;
1235 
1236 	for_each_online_node(i)
1237 		register_page_bootmem_info_node(NODE_DATA(i));
1238 #endif
1239 }
1240 
1241 void __init mem_init(void)
1242 {
1243 	pci_iommu_alloc();
1244 
1245 	/* clear_bss() already clear the empty_zero_page */
1246 
1247 	/* this will put all memory onto the freelists */
1248 	memblock_free_all();
1249 	after_bootmem = 1;
1250 	x86_init.hyper.init_after_bootmem();
1251 
1252 	/*
1253 	 * Must be done after boot memory is put on freelist, because here we
1254 	 * might set fields in deferred struct pages that have not yet been
1255 	 * initialized, and memblock_free_all() initializes all the reserved
1256 	 * deferred pages for us.
1257 	 */
1258 	register_page_bootmem_info();
1259 
1260 	/* Register memory areas for /proc/kcore */
1261 	if (get_gate_vma(&init_mm))
1262 		kclist_add(&kcore_vsyscall, (void *)VSYSCALL_ADDR, PAGE_SIZE, KCORE_USER);
1263 
1264 	mem_init_print_info(NULL);
1265 }
1266 
1267 #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
1268 int __init deferred_page_init_max_threads(const struct cpumask *node_cpumask)
1269 {
1270 	/*
1271 	 * More CPUs always led to greater speedups on tested systems, up to
1272 	 * all the nodes' CPUs.  Use all since the system is otherwise idle
1273 	 * now.
1274 	 */
1275 	return max_t(int, cpumask_weight(node_cpumask), 1);
1276 }
1277 #endif
1278 
1279 int kernel_set_to_readonly;
1280 
1281 void mark_rodata_ro(void)
1282 {
1283 	unsigned long start = PFN_ALIGN(_text);
1284 	unsigned long rodata_start = PFN_ALIGN(__start_rodata);
1285 	unsigned long end = (unsigned long)__end_rodata_hpage_align;
1286 	unsigned long text_end = PFN_ALIGN(_etext);
1287 	unsigned long rodata_end = PFN_ALIGN(__end_rodata);
1288 	unsigned long all_end;
1289 
1290 	printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
1291 	       (end - start) >> 10);
1292 	set_memory_ro(start, (end - start) >> PAGE_SHIFT);
1293 
1294 	kernel_set_to_readonly = 1;
1295 
1296 	/*
1297 	 * The rodata/data/bss/brk section (but not the kernel text!)
1298 	 * should also be not-executable.
1299 	 *
1300 	 * We align all_end to PMD_SIZE because the existing mapping
1301 	 * is a full PMD. If we would align _brk_end to PAGE_SIZE we
1302 	 * split the PMD and the reminder between _brk_end and the end
1303 	 * of the PMD will remain mapped executable.
1304 	 *
1305 	 * Any PMD which was setup after the one which covers _brk_end
1306 	 * has been zapped already via cleanup_highmem().
1307 	 */
1308 	all_end = roundup((unsigned long)_brk_end, PMD_SIZE);
1309 	set_memory_nx(text_end, (all_end - text_end) >> PAGE_SHIFT);
1310 
1311 	set_ftrace_ops_ro();
1312 
1313 #ifdef CONFIG_CPA_DEBUG
1314 	printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end);
1315 	set_memory_rw(start, (end-start) >> PAGE_SHIFT);
1316 
1317 	printk(KERN_INFO "Testing CPA: again\n");
1318 	set_memory_ro(start, (end-start) >> PAGE_SHIFT);
1319 #endif
1320 
1321 	free_kernel_image_pages("unused kernel image (text/rodata gap)",
1322 				(void *)text_end, (void *)rodata_start);
1323 	free_kernel_image_pages("unused kernel image (rodata/data gap)",
1324 				(void *)rodata_end, (void *)_sdata);
1325 
1326 	debug_checkwx();
1327 }
1328 
1329 int kern_addr_valid(unsigned long addr)
1330 {
1331 	unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
1332 	pgd_t *pgd;
1333 	p4d_t *p4d;
1334 	pud_t *pud;
1335 	pmd_t *pmd;
1336 	pte_t *pte;
1337 
1338 	if (above != 0 && above != -1UL)
1339 		return 0;
1340 
1341 	pgd = pgd_offset_k(addr);
1342 	if (pgd_none(*pgd))
1343 		return 0;
1344 
1345 	p4d = p4d_offset(pgd, addr);
1346 	if (p4d_none(*p4d))
1347 		return 0;
1348 
1349 	pud = pud_offset(p4d, addr);
1350 	if (pud_none(*pud))
1351 		return 0;
1352 
1353 	if (pud_large(*pud))
1354 		return pfn_valid(pud_pfn(*pud));
1355 
1356 	pmd = pmd_offset(pud, addr);
1357 	if (pmd_none(*pmd))
1358 		return 0;
1359 
1360 	if (pmd_large(*pmd))
1361 		return pfn_valid(pmd_pfn(*pmd));
1362 
1363 	pte = pte_offset_kernel(pmd, addr);
1364 	if (pte_none(*pte))
1365 		return 0;
1366 
1367 	return pfn_valid(pte_pfn(*pte));
1368 }
1369 
1370 /*
1371  * Block size is the minimum amount of memory which can be hotplugged or
1372  * hotremoved. It must be power of two and must be equal or larger than
1373  * MIN_MEMORY_BLOCK_SIZE.
1374  */
1375 #define MAX_BLOCK_SIZE (2UL << 30)
1376 
1377 /* Amount of ram needed to start using large blocks */
1378 #define MEM_SIZE_FOR_LARGE_BLOCK (64UL << 30)
1379 
1380 /* Adjustable memory block size */
1381 static unsigned long set_memory_block_size;
1382 int __init set_memory_block_size_order(unsigned int order)
1383 {
1384 	unsigned long size = 1UL << order;
1385 
1386 	if (size > MEM_SIZE_FOR_LARGE_BLOCK || size < MIN_MEMORY_BLOCK_SIZE)
1387 		return -EINVAL;
1388 
1389 	set_memory_block_size = size;
1390 	return 0;
1391 }
1392 
1393 static unsigned long probe_memory_block_size(void)
1394 {
1395 	unsigned long boot_mem_end = max_pfn << PAGE_SHIFT;
1396 	unsigned long bz;
1397 
1398 	/* If memory block size has been set, then use it */
1399 	bz = set_memory_block_size;
1400 	if (bz)
1401 		goto done;
1402 
1403 	/* Use regular block if RAM is smaller than MEM_SIZE_FOR_LARGE_BLOCK */
1404 	if (boot_mem_end < MEM_SIZE_FOR_LARGE_BLOCK) {
1405 		bz = MIN_MEMORY_BLOCK_SIZE;
1406 		goto done;
1407 	}
1408 
1409 	/* Find the largest allowed block size that aligns to memory end */
1410 	for (bz = MAX_BLOCK_SIZE; bz > MIN_MEMORY_BLOCK_SIZE; bz >>= 1) {
1411 		if (IS_ALIGNED(boot_mem_end, bz))
1412 			break;
1413 	}
1414 done:
1415 	pr_info("x86/mm: Memory block size: %ldMB\n", bz >> 20);
1416 
1417 	return bz;
1418 }
1419 
1420 static unsigned long memory_block_size_probed;
1421 unsigned long memory_block_size_bytes(void)
1422 {
1423 	if (!memory_block_size_probed)
1424 		memory_block_size_probed = probe_memory_block_size();
1425 
1426 	return memory_block_size_probed;
1427 }
1428 
1429 #ifdef CONFIG_SPARSEMEM_VMEMMAP
1430 /*
1431  * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
1432  */
1433 static long __meminitdata addr_start, addr_end;
1434 static void __meminitdata *p_start, *p_end;
1435 static int __meminitdata node_start;
1436 
1437 static int __meminit vmemmap_populate_hugepages(unsigned long start,
1438 		unsigned long end, int node, struct vmem_altmap *altmap)
1439 {
1440 	unsigned long addr;
1441 	unsigned long next;
1442 	pgd_t *pgd;
1443 	p4d_t *p4d;
1444 	pud_t *pud;
1445 	pmd_t *pmd;
1446 
1447 	for (addr = start; addr < end; addr = next) {
1448 		next = pmd_addr_end(addr, end);
1449 
1450 		pgd = vmemmap_pgd_populate(addr, node);
1451 		if (!pgd)
1452 			return -ENOMEM;
1453 
1454 		p4d = vmemmap_p4d_populate(pgd, addr, node);
1455 		if (!p4d)
1456 			return -ENOMEM;
1457 
1458 		pud = vmemmap_pud_populate(p4d, addr, node);
1459 		if (!pud)
1460 			return -ENOMEM;
1461 
1462 		pmd = pmd_offset(pud, addr);
1463 		if (pmd_none(*pmd)) {
1464 			void *p;
1465 
1466 			if (altmap)
1467 				p = altmap_alloc_block_buf(PMD_SIZE, altmap);
1468 			else
1469 				p = vmemmap_alloc_block_buf(PMD_SIZE, node);
1470 			if (p) {
1471 				pte_t entry;
1472 
1473 				entry = pfn_pte(__pa(p) >> PAGE_SHIFT,
1474 						PAGE_KERNEL_LARGE);
1475 				set_pmd(pmd, __pmd(pte_val(entry)));
1476 
1477 				/* check to see if we have contiguous blocks */
1478 				if (p_end != p || node_start != node) {
1479 					if (p_start)
1480 						pr_debug(" [%lx-%lx] PMD -> [%p-%p] on node %d\n",
1481 						       addr_start, addr_end-1, p_start, p_end-1, node_start);
1482 					addr_start = addr;
1483 					node_start = node;
1484 					p_start = p;
1485 				}
1486 
1487 				addr_end = addr + PMD_SIZE;
1488 				p_end = p + PMD_SIZE;
1489 				continue;
1490 			} else if (altmap)
1491 				return -ENOMEM; /* no fallback */
1492 		} else if (pmd_large(*pmd)) {
1493 			vmemmap_verify((pte_t *)pmd, node, addr, next);
1494 			continue;
1495 		}
1496 		if (vmemmap_populate_basepages(addr, next, node))
1497 			return -ENOMEM;
1498 	}
1499 	return 0;
1500 }
1501 
1502 int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
1503 		struct vmem_altmap *altmap)
1504 {
1505 	int err;
1506 
1507 	if (end - start < PAGES_PER_SECTION * sizeof(struct page))
1508 		err = vmemmap_populate_basepages(start, end, node);
1509 	else if (boot_cpu_has(X86_FEATURE_PSE))
1510 		err = vmemmap_populate_hugepages(start, end, node, altmap);
1511 	else if (altmap) {
1512 		pr_err_once("%s: no cpu support for altmap allocations\n",
1513 				__func__);
1514 		err = -ENOMEM;
1515 	} else
1516 		err = vmemmap_populate_basepages(start, end, node);
1517 	if (!err)
1518 		sync_global_pgds(start, end - 1);
1519 	return err;
1520 }
1521 
1522 #if defined(CONFIG_MEMORY_HOTPLUG_SPARSE) && defined(CONFIG_HAVE_BOOTMEM_INFO_NODE)
1523 void register_page_bootmem_memmap(unsigned long section_nr,
1524 				  struct page *start_page, unsigned long nr_pages)
1525 {
1526 	unsigned long addr = (unsigned long)start_page;
1527 	unsigned long end = (unsigned long)(start_page + nr_pages);
1528 	unsigned long next;
1529 	pgd_t *pgd;
1530 	p4d_t *p4d;
1531 	pud_t *pud;
1532 	pmd_t *pmd;
1533 	unsigned int nr_pmd_pages;
1534 	struct page *page;
1535 
1536 	for (; addr < end; addr = next) {
1537 		pte_t *pte = NULL;
1538 
1539 		pgd = pgd_offset_k(addr);
1540 		if (pgd_none(*pgd)) {
1541 			next = (addr + PAGE_SIZE) & PAGE_MASK;
1542 			continue;
1543 		}
1544 		get_page_bootmem(section_nr, pgd_page(*pgd), MIX_SECTION_INFO);
1545 
1546 		p4d = p4d_offset(pgd, addr);
1547 		if (p4d_none(*p4d)) {
1548 			next = (addr + PAGE_SIZE) & PAGE_MASK;
1549 			continue;
1550 		}
1551 		get_page_bootmem(section_nr, p4d_page(*p4d), MIX_SECTION_INFO);
1552 
1553 		pud = pud_offset(p4d, addr);
1554 		if (pud_none(*pud)) {
1555 			next = (addr + PAGE_SIZE) & PAGE_MASK;
1556 			continue;
1557 		}
1558 		get_page_bootmem(section_nr, pud_page(*pud), MIX_SECTION_INFO);
1559 
1560 		if (!boot_cpu_has(X86_FEATURE_PSE)) {
1561 			next = (addr + PAGE_SIZE) & PAGE_MASK;
1562 			pmd = pmd_offset(pud, addr);
1563 			if (pmd_none(*pmd))
1564 				continue;
1565 			get_page_bootmem(section_nr, pmd_page(*pmd),
1566 					 MIX_SECTION_INFO);
1567 
1568 			pte = pte_offset_kernel(pmd, addr);
1569 			if (pte_none(*pte))
1570 				continue;
1571 			get_page_bootmem(section_nr, pte_page(*pte),
1572 					 SECTION_INFO);
1573 		} else {
1574 			next = pmd_addr_end(addr, end);
1575 
1576 			pmd = pmd_offset(pud, addr);
1577 			if (pmd_none(*pmd))
1578 				continue;
1579 
1580 			nr_pmd_pages = 1 << get_order(PMD_SIZE);
1581 			page = pmd_page(*pmd);
1582 			while (nr_pmd_pages--)
1583 				get_page_bootmem(section_nr, page++,
1584 						 SECTION_INFO);
1585 		}
1586 	}
1587 }
1588 #endif
1589 
1590 void __meminit vmemmap_populate_print_last(void)
1591 {
1592 	if (p_start) {
1593 		pr_debug(" [%lx-%lx] PMD -> [%p-%p] on node %d\n",
1594 			addr_start, addr_end-1, p_start, p_end-1, node_start);
1595 		p_start = NULL;
1596 		p_end = NULL;
1597 		node_start = 0;
1598 	}
1599 }
1600 #endif
1601