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