xref: /linux/arch/x86/mm/init_32.c (revision 634cd4b6afe15dca8df02bcba242b9b0c5e9b5a5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *
4  *  Copyright (C) 1995  Linus Torvalds
5  *
6  *  Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
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/hugetlb.h>
19 #include <linux/swap.h>
20 #include <linux/smp.h>
21 #include <linux/init.h>
22 #include <linux/highmem.h>
23 #include <linux/pagemap.h>
24 #include <linux/pci.h>
25 #include <linux/pfn.h>
26 #include <linux/poison.h>
27 #include <linux/memblock.h>
28 #include <linux/proc_fs.h>
29 #include <linux/memory_hotplug.h>
30 #include <linux/initrd.h>
31 #include <linux/cpumask.h>
32 #include <linux/gfp.h>
33 
34 #include <asm/asm.h>
35 #include <asm/bios_ebda.h>
36 #include <asm/processor.h>
37 #include <linux/uaccess.h>
38 #include <asm/pgtable.h>
39 #include <asm/dma.h>
40 #include <asm/fixmap.h>
41 #include <asm/e820/api.h>
42 #include <asm/apic.h>
43 #include <asm/bugs.h>
44 #include <asm/tlb.h>
45 #include <asm/tlbflush.h>
46 #include <asm/olpc_ofw.h>
47 #include <asm/pgalloc.h>
48 #include <asm/sections.h>
49 #include <asm/paravirt.h>
50 #include <asm/setup.h>
51 #include <asm/set_memory.h>
52 #include <asm/page_types.h>
53 #include <asm/cpu_entry_area.h>
54 #include <asm/init.h>
55 #include <asm/pgtable_areas.h>
56 
57 #include "mm_internal.h"
58 
59 unsigned long highstart_pfn, highend_pfn;
60 
61 bool __read_mostly __vmalloc_start_set = false;
62 
63 /*
64  * Creates a middle page table and puts a pointer to it in the
65  * given global directory entry. This only returns the gd entry
66  * in non-PAE compilation mode, since the middle layer is folded.
67  */
68 static pmd_t * __init one_md_table_init(pgd_t *pgd)
69 {
70 	p4d_t *p4d;
71 	pud_t *pud;
72 	pmd_t *pmd_table;
73 
74 #ifdef CONFIG_X86_PAE
75 	if (!(pgd_val(*pgd) & _PAGE_PRESENT)) {
76 		pmd_table = (pmd_t *)alloc_low_page();
77 		paravirt_alloc_pmd(&init_mm, __pa(pmd_table) >> PAGE_SHIFT);
78 		set_pgd(pgd, __pgd(__pa(pmd_table) | _PAGE_PRESENT));
79 		p4d = p4d_offset(pgd, 0);
80 		pud = pud_offset(p4d, 0);
81 		BUG_ON(pmd_table != pmd_offset(pud, 0));
82 
83 		return pmd_table;
84 	}
85 #endif
86 	p4d = p4d_offset(pgd, 0);
87 	pud = pud_offset(p4d, 0);
88 	pmd_table = pmd_offset(pud, 0);
89 
90 	return pmd_table;
91 }
92 
93 /*
94  * Create a page table and place a pointer to it in a middle page
95  * directory entry:
96  */
97 static pte_t * __init one_page_table_init(pmd_t *pmd)
98 {
99 	if (!(pmd_val(*pmd) & _PAGE_PRESENT)) {
100 		pte_t *page_table = (pte_t *)alloc_low_page();
101 
102 		paravirt_alloc_pte(&init_mm, __pa(page_table) >> PAGE_SHIFT);
103 		set_pmd(pmd, __pmd(__pa(page_table) | _PAGE_TABLE));
104 		BUG_ON(page_table != pte_offset_kernel(pmd, 0));
105 	}
106 
107 	return pte_offset_kernel(pmd, 0);
108 }
109 
110 pmd_t * __init populate_extra_pmd(unsigned long vaddr)
111 {
112 	int pgd_idx = pgd_index(vaddr);
113 	int pmd_idx = pmd_index(vaddr);
114 
115 	return one_md_table_init(swapper_pg_dir + pgd_idx) + pmd_idx;
116 }
117 
118 pte_t * __init populate_extra_pte(unsigned long vaddr)
119 {
120 	int pte_idx = pte_index(vaddr);
121 	pmd_t *pmd;
122 
123 	pmd = populate_extra_pmd(vaddr);
124 	return one_page_table_init(pmd) + pte_idx;
125 }
126 
127 static unsigned long __init
128 page_table_range_init_count(unsigned long start, unsigned long end)
129 {
130 	unsigned long count = 0;
131 #ifdef CONFIG_HIGHMEM
132 	int pmd_idx_kmap_begin = fix_to_virt(FIX_KMAP_END) >> PMD_SHIFT;
133 	int pmd_idx_kmap_end = fix_to_virt(FIX_KMAP_BEGIN) >> PMD_SHIFT;
134 	int pgd_idx, pmd_idx;
135 	unsigned long vaddr;
136 
137 	if (pmd_idx_kmap_begin == pmd_idx_kmap_end)
138 		return 0;
139 
140 	vaddr = start;
141 	pgd_idx = pgd_index(vaddr);
142 	pmd_idx = pmd_index(vaddr);
143 
144 	for ( ; (pgd_idx < PTRS_PER_PGD) && (vaddr != end); pgd_idx++) {
145 		for (; (pmd_idx < PTRS_PER_PMD) && (vaddr != end);
146 							pmd_idx++) {
147 			if ((vaddr >> PMD_SHIFT) >= pmd_idx_kmap_begin &&
148 			    (vaddr >> PMD_SHIFT) <= pmd_idx_kmap_end)
149 				count++;
150 			vaddr += PMD_SIZE;
151 		}
152 		pmd_idx = 0;
153 	}
154 #endif
155 	return count;
156 }
157 
158 static pte_t *__init page_table_kmap_check(pte_t *pte, pmd_t *pmd,
159 					   unsigned long vaddr, pte_t *lastpte,
160 					   void **adr)
161 {
162 #ifdef CONFIG_HIGHMEM
163 	/*
164 	 * Something (early fixmap) may already have put a pte
165 	 * page here, which causes the page table allocation
166 	 * to become nonlinear. Attempt to fix it, and if it
167 	 * is still nonlinear then we have to bug.
168 	 */
169 	int pmd_idx_kmap_begin = fix_to_virt(FIX_KMAP_END) >> PMD_SHIFT;
170 	int pmd_idx_kmap_end = fix_to_virt(FIX_KMAP_BEGIN) >> PMD_SHIFT;
171 
172 	if (pmd_idx_kmap_begin != pmd_idx_kmap_end
173 	    && (vaddr >> PMD_SHIFT) >= pmd_idx_kmap_begin
174 	    && (vaddr >> PMD_SHIFT) <= pmd_idx_kmap_end) {
175 		pte_t *newpte;
176 		int i;
177 
178 		BUG_ON(after_bootmem);
179 		newpte = *adr;
180 		for (i = 0; i < PTRS_PER_PTE; i++)
181 			set_pte(newpte + i, pte[i]);
182 		*adr = (void *)(((unsigned long)(*adr)) + PAGE_SIZE);
183 
184 		paravirt_alloc_pte(&init_mm, __pa(newpte) >> PAGE_SHIFT);
185 		set_pmd(pmd, __pmd(__pa(newpte)|_PAGE_TABLE));
186 		BUG_ON(newpte != pte_offset_kernel(pmd, 0));
187 		__flush_tlb_all();
188 
189 		paravirt_release_pte(__pa(pte) >> PAGE_SHIFT);
190 		pte = newpte;
191 	}
192 	BUG_ON(vaddr < fix_to_virt(FIX_KMAP_BEGIN - 1)
193 	       && vaddr > fix_to_virt(FIX_KMAP_END)
194 	       && lastpte && lastpte + PTRS_PER_PTE != pte);
195 #endif
196 	return pte;
197 }
198 
199 /*
200  * This function initializes a certain range of kernel virtual memory
201  * with new bootmem page tables, everywhere page tables are missing in
202  * the given range.
203  *
204  * NOTE: The pagetables are allocated contiguous on the physical space
205  * so we can cache the place of the first one and move around without
206  * checking the pgd every time.
207  */
208 static void __init
209 page_table_range_init(unsigned long start, unsigned long end, pgd_t *pgd_base)
210 {
211 	int pgd_idx, pmd_idx;
212 	unsigned long vaddr;
213 	pgd_t *pgd;
214 	pmd_t *pmd;
215 	pte_t *pte = NULL;
216 	unsigned long count = page_table_range_init_count(start, end);
217 	void *adr = NULL;
218 
219 	if (count)
220 		adr = alloc_low_pages(count);
221 
222 	vaddr = start;
223 	pgd_idx = pgd_index(vaddr);
224 	pmd_idx = pmd_index(vaddr);
225 	pgd = pgd_base + pgd_idx;
226 
227 	for ( ; (pgd_idx < PTRS_PER_PGD) && (vaddr != end); pgd++, pgd_idx++) {
228 		pmd = one_md_table_init(pgd);
229 		pmd = pmd + pmd_index(vaddr);
230 		for (; (pmd_idx < PTRS_PER_PMD) && (vaddr != end);
231 							pmd++, pmd_idx++) {
232 			pte = page_table_kmap_check(one_page_table_init(pmd),
233 						    pmd, vaddr, pte, &adr);
234 
235 			vaddr += PMD_SIZE;
236 		}
237 		pmd_idx = 0;
238 	}
239 }
240 
241 static inline int is_kernel_text(unsigned long addr)
242 {
243 	if (addr >= (unsigned long)_text && addr <= (unsigned long)__init_end)
244 		return 1;
245 	return 0;
246 }
247 
248 /*
249  * This maps the physical memory to kernel virtual address space, a total
250  * of max_low_pfn pages, by creating page tables starting from address
251  * PAGE_OFFSET:
252  */
253 unsigned long __init
254 kernel_physical_mapping_init(unsigned long start,
255 			     unsigned long end,
256 			     unsigned long page_size_mask)
257 {
258 	int use_pse = page_size_mask == (1<<PG_LEVEL_2M);
259 	unsigned long last_map_addr = end;
260 	unsigned long start_pfn, end_pfn;
261 	pgd_t *pgd_base = swapper_pg_dir;
262 	int pgd_idx, pmd_idx, pte_ofs;
263 	unsigned long pfn;
264 	pgd_t *pgd;
265 	pmd_t *pmd;
266 	pte_t *pte;
267 	unsigned pages_2m, pages_4k;
268 	int mapping_iter;
269 
270 	start_pfn = start >> PAGE_SHIFT;
271 	end_pfn = end >> PAGE_SHIFT;
272 
273 	/*
274 	 * First iteration will setup identity mapping using large/small pages
275 	 * based on use_pse, with other attributes same as set by
276 	 * the early code in head_32.S
277 	 *
278 	 * Second iteration will setup the appropriate attributes (NX, GLOBAL..)
279 	 * as desired for the kernel identity mapping.
280 	 *
281 	 * This two pass mechanism conforms to the TLB app note which says:
282 	 *
283 	 *     "Software should not write to a paging-structure entry in a way
284 	 *      that would change, for any linear address, both the page size
285 	 *      and either the page frame or attributes."
286 	 */
287 	mapping_iter = 1;
288 
289 	if (!boot_cpu_has(X86_FEATURE_PSE))
290 		use_pse = 0;
291 
292 repeat:
293 	pages_2m = pages_4k = 0;
294 	pfn = start_pfn;
295 	pgd_idx = pgd_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
296 	pgd = pgd_base + pgd_idx;
297 	for (; pgd_idx < PTRS_PER_PGD; pgd++, pgd_idx++) {
298 		pmd = one_md_table_init(pgd);
299 
300 		if (pfn >= end_pfn)
301 			continue;
302 #ifdef CONFIG_X86_PAE
303 		pmd_idx = pmd_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
304 		pmd += pmd_idx;
305 #else
306 		pmd_idx = 0;
307 #endif
308 		for (; pmd_idx < PTRS_PER_PMD && pfn < end_pfn;
309 		     pmd++, pmd_idx++) {
310 			unsigned int addr = pfn * PAGE_SIZE + PAGE_OFFSET;
311 
312 			/*
313 			 * Map with big pages if possible, otherwise
314 			 * create normal page tables:
315 			 */
316 			if (use_pse) {
317 				unsigned int addr2;
318 				pgprot_t prot = PAGE_KERNEL_LARGE;
319 				/*
320 				 * first pass will use the same initial
321 				 * identity mapping attribute + _PAGE_PSE.
322 				 */
323 				pgprot_t init_prot =
324 					__pgprot(PTE_IDENT_ATTR |
325 						 _PAGE_PSE);
326 
327 				pfn &= PMD_MASK >> PAGE_SHIFT;
328 				addr2 = (pfn + PTRS_PER_PTE-1) * PAGE_SIZE +
329 					PAGE_OFFSET + PAGE_SIZE-1;
330 
331 				if (is_kernel_text(addr) ||
332 				    is_kernel_text(addr2))
333 					prot = PAGE_KERNEL_LARGE_EXEC;
334 
335 				pages_2m++;
336 				if (mapping_iter == 1)
337 					set_pmd(pmd, pfn_pmd(pfn, init_prot));
338 				else
339 					set_pmd(pmd, pfn_pmd(pfn, prot));
340 
341 				pfn += PTRS_PER_PTE;
342 				continue;
343 			}
344 			pte = one_page_table_init(pmd);
345 
346 			pte_ofs = pte_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
347 			pte += pte_ofs;
348 			for (; pte_ofs < PTRS_PER_PTE && pfn < end_pfn;
349 			     pte++, pfn++, pte_ofs++, addr += PAGE_SIZE) {
350 				pgprot_t prot = PAGE_KERNEL;
351 				/*
352 				 * first pass will use the same initial
353 				 * identity mapping attribute.
354 				 */
355 				pgprot_t init_prot = __pgprot(PTE_IDENT_ATTR);
356 
357 				if (is_kernel_text(addr))
358 					prot = PAGE_KERNEL_EXEC;
359 
360 				pages_4k++;
361 				if (mapping_iter == 1) {
362 					set_pte(pte, pfn_pte(pfn, init_prot));
363 					last_map_addr = (pfn << PAGE_SHIFT) + PAGE_SIZE;
364 				} else
365 					set_pte(pte, pfn_pte(pfn, prot));
366 			}
367 		}
368 	}
369 	if (mapping_iter == 1) {
370 		/*
371 		 * update direct mapping page count only in the first
372 		 * iteration.
373 		 */
374 		update_page_count(PG_LEVEL_2M, pages_2m);
375 		update_page_count(PG_LEVEL_4K, pages_4k);
376 
377 		/*
378 		 * local global flush tlb, which will flush the previous
379 		 * mappings present in both small and large page TLB's.
380 		 */
381 		__flush_tlb_all();
382 
383 		/*
384 		 * Second iteration will set the actual desired PTE attributes.
385 		 */
386 		mapping_iter = 2;
387 		goto repeat;
388 	}
389 	return last_map_addr;
390 }
391 
392 pte_t *kmap_pte;
393 
394 static inline pte_t *kmap_get_fixmap_pte(unsigned long vaddr)
395 {
396 	pgd_t *pgd = pgd_offset_k(vaddr);
397 	p4d_t *p4d = p4d_offset(pgd, vaddr);
398 	pud_t *pud = pud_offset(p4d, vaddr);
399 	pmd_t *pmd = pmd_offset(pud, vaddr);
400 	return pte_offset_kernel(pmd, vaddr);
401 }
402 
403 static void __init kmap_init(void)
404 {
405 	unsigned long kmap_vstart;
406 
407 	/*
408 	 * Cache the first kmap pte:
409 	 */
410 	kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
411 	kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
412 }
413 
414 #ifdef CONFIG_HIGHMEM
415 static void __init permanent_kmaps_init(pgd_t *pgd_base)
416 {
417 	unsigned long vaddr;
418 	pgd_t *pgd;
419 	p4d_t *p4d;
420 	pud_t *pud;
421 	pmd_t *pmd;
422 	pte_t *pte;
423 
424 	vaddr = PKMAP_BASE;
425 	page_table_range_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base);
426 
427 	pgd = swapper_pg_dir + pgd_index(vaddr);
428 	p4d = p4d_offset(pgd, vaddr);
429 	pud = pud_offset(p4d, vaddr);
430 	pmd = pmd_offset(pud, vaddr);
431 	pte = pte_offset_kernel(pmd, vaddr);
432 	pkmap_page_table = pte;
433 }
434 
435 void __init add_highpages_with_active_regions(int nid,
436 			 unsigned long start_pfn, unsigned long end_pfn)
437 {
438 	phys_addr_t start, end;
439 	u64 i;
440 
441 	for_each_free_mem_range(i, nid, MEMBLOCK_NONE, &start, &end, NULL) {
442 		unsigned long pfn = clamp_t(unsigned long, PFN_UP(start),
443 					    start_pfn, end_pfn);
444 		unsigned long e_pfn = clamp_t(unsigned long, PFN_DOWN(end),
445 					      start_pfn, end_pfn);
446 		for ( ; pfn < e_pfn; pfn++)
447 			if (pfn_valid(pfn))
448 				free_highmem_page(pfn_to_page(pfn));
449 	}
450 }
451 #else
452 static inline void permanent_kmaps_init(pgd_t *pgd_base)
453 {
454 }
455 #endif /* CONFIG_HIGHMEM */
456 
457 void __init sync_initial_page_table(void)
458 {
459 	clone_pgd_range(initial_page_table + KERNEL_PGD_BOUNDARY,
460 			swapper_pg_dir     + KERNEL_PGD_BOUNDARY,
461 			KERNEL_PGD_PTRS);
462 
463 	/*
464 	 * sync back low identity map too.  It is used for example
465 	 * in the 32-bit EFI stub.
466 	 */
467 	clone_pgd_range(initial_page_table,
468 			swapper_pg_dir     + KERNEL_PGD_BOUNDARY,
469 			min(KERNEL_PGD_PTRS, KERNEL_PGD_BOUNDARY));
470 }
471 
472 void __init native_pagetable_init(void)
473 {
474 	unsigned long pfn, va;
475 	pgd_t *pgd, *base = swapper_pg_dir;
476 	p4d_t *p4d;
477 	pud_t *pud;
478 	pmd_t *pmd;
479 	pte_t *pte;
480 
481 	/*
482 	 * Remove any mappings which extend past the end of physical
483 	 * memory from the boot time page table.
484 	 * In virtual address space, we should have at least two pages
485 	 * from VMALLOC_END to pkmap or fixmap according to VMALLOC_END
486 	 * definition. And max_low_pfn is set to VMALLOC_END physical
487 	 * address. If initial memory mapping is doing right job, we
488 	 * should have pte used near max_low_pfn or one pmd is not present.
489 	 */
490 	for (pfn = max_low_pfn; pfn < 1<<(32-PAGE_SHIFT); pfn++) {
491 		va = PAGE_OFFSET + (pfn<<PAGE_SHIFT);
492 		pgd = base + pgd_index(va);
493 		if (!pgd_present(*pgd))
494 			break;
495 
496 		p4d = p4d_offset(pgd, va);
497 		pud = pud_offset(p4d, va);
498 		pmd = pmd_offset(pud, va);
499 		if (!pmd_present(*pmd))
500 			break;
501 
502 		/* should not be large page here */
503 		if (pmd_large(*pmd)) {
504 			pr_warn("try to clear pte for ram above max_low_pfn: pfn: %lx pmd: %p pmd phys: %lx, but pmd is big page and is not using pte !\n",
505 				pfn, pmd, __pa(pmd));
506 			BUG_ON(1);
507 		}
508 
509 		pte = pte_offset_kernel(pmd, va);
510 		if (!pte_present(*pte))
511 			break;
512 
513 		printk(KERN_DEBUG "clearing pte for ram above max_low_pfn: pfn: %lx pmd: %p pmd phys: %lx pte: %p pte phys: %lx\n",
514 				pfn, pmd, __pa(pmd), pte, __pa(pte));
515 		pte_clear(NULL, va, pte);
516 	}
517 	paravirt_alloc_pmd(&init_mm, __pa(base) >> PAGE_SHIFT);
518 	paging_init();
519 }
520 
521 /*
522  * Build a proper pagetable for the kernel mappings.  Up until this
523  * point, we've been running on some set of pagetables constructed by
524  * the boot process.
525  *
526  * If we're booting on native hardware, this will be a pagetable
527  * constructed in arch/x86/kernel/head_32.S.  The root of the
528  * pagetable will be swapper_pg_dir.
529  *
530  * If we're booting paravirtualized under a hypervisor, then there are
531  * more options: we may already be running PAE, and the pagetable may
532  * or may not be based in swapper_pg_dir.  In any case,
533  * paravirt_pagetable_init() will set up swapper_pg_dir
534  * appropriately for the rest of the initialization to work.
535  *
536  * In general, pagetable_init() assumes that the pagetable may already
537  * be partially populated, and so it avoids stomping on any existing
538  * mappings.
539  */
540 void __init early_ioremap_page_table_range_init(void)
541 {
542 	pgd_t *pgd_base = swapper_pg_dir;
543 	unsigned long vaddr, end;
544 
545 	/*
546 	 * Fixed mappings, only the page table structure has to be
547 	 * created - mappings will be set by set_fixmap():
548 	 */
549 	vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
550 	end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK;
551 	page_table_range_init(vaddr, end, pgd_base);
552 	early_ioremap_reset();
553 }
554 
555 static void __init pagetable_init(void)
556 {
557 	pgd_t *pgd_base = swapper_pg_dir;
558 
559 	permanent_kmaps_init(pgd_base);
560 }
561 
562 #define DEFAULT_PTE_MASK ~(_PAGE_NX | _PAGE_GLOBAL)
563 /* Bits supported by the hardware: */
564 pteval_t __supported_pte_mask __read_mostly = DEFAULT_PTE_MASK;
565 /* Bits allowed in normal kernel mappings: */
566 pteval_t __default_kernel_pte_mask __read_mostly = DEFAULT_PTE_MASK;
567 EXPORT_SYMBOL_GPL(__supported_pte_mask);
568 /* Used in PAGE_KERNEL_* macros which are reasonably used out-of-tree: */
569 EXPORT_SYMBOL(__default_kernel_pte_mask);
570 
571 /* user-defined highmem size */
572 static unsigned int highmem_pages = -1;
573 
574 /*
575  * highmem=size forces highmem to be exactly 'size' bytes.
576  * This works even on boxes that have no highmem otherwise.
577  * This also works to reduce highmem size on bigger boxes.
578  */
579 static int __init parse_highmem(char *arg)
580 {
581 	if (!arg)
582 		return -EINVAL;
583 
584 	highmem_pages = memparse(arg, &arg) >> PAGE_SHIFT;
585 	return 0;
586 }
587 early_param("highmem", parse_highmem);
588 
589 #define MSG_HIGHMEM_TOO_BIG \
590 	"highmem size (%luMB) is bigger than pages available (%luMB)!\n"
591 
592 #define MSG_LOWMEM_TOO_SMALL \
593 	"highmem size (%luMB) results in <64MB lowmem, ignoring it!\n"
594 /*
595  * All of RAM fits into lowmem - but if user wants highmem
596  * artificially via the highmem=x boot parameter then create
597  * it:
598  */
599 static void __init lowmem_pfn_init(void)
600 {
601 	/* max_low_pfn is 0, we already have early_res support */
602 	max_low_pfn = max_pfn;
603 
604 	if (highmem_pages == -1)
605 		highmem_pages = 0;
606 #ifdef CONFIG_HIGHMEM
607 	if (highmem_pages >= max_pfn) {
608 		printk(KERN_ERR MSG_HIGHMEM_TOO_BIG,
609 			pages_to_mb(highmem_pages), pages_to_mb(max_pfn));
610 		highmem_pages = 0;
611 	}
612 	if (highmem_pages) {
613 		if (max_low_pfn - highmem_pages < 64*1024*1024/PAGE_SIZE) {
614 			printk(KERN_ERR MSG_LOWMEM_TOO_SMALL,
615 				pages_to_mb(highmem_pages));
616 			highmem_pages = 0;
617 		}
618 		max_low_pfn -= highmem_pages;
619 	}
620 #else
621 	if (highmem_pages)
622 		printk(KERN_ERR "ignoring highmem size on non-highmem kernel!\n");
623 #endif
624 }
625 
626 #define MSG_HIGHMEM_TOO_SMALL \
627 	"only %luMB highmem pages available, ignoring highmem size of %luMB!\n"
628 
629 #define MSG_HIGHMEM_TRIMMED \
630 	"Warning: only 4GB will be used. Use a HIGHMEM64G enabled kernel!\n"
631 /*
632  * We have more RAM than fits into lowmem - we try to put it into
633  * highmem, also taking the highmem=x boot parameter into account:
634  */
635 static void __init highmem_pfn_init(void)
636 {
637 	max_low_pfn = MAXMEM_PFN;
638 
639 	if (highmem_pages == -1)
640 		highmem_pages = max_pfn - MAXMEM_PFN;
641 
642 	if (highmem_pages + MAXMEM_PFN < max_pfn)
643 		max_pfn = MAXMEM_PFN + highmem_pages;
644 
645 	if (highmem_pages + MAXMEM_PFN > max_pfn) {
646 		printk(KERN_WARNING MSG_HIGHMEM_TOO_SMALL,
647 			pages_to_mb(max_pfn - MAXMEM_PFN),
648 			pages_to_mb(highmem_pages));
649 		highmem_pages = 0;
650 	}
651 #ifndef CONFIG_HIGHMEM
652 	/* Maximum memory usable is what is directly addressable */
653 	printk(KERN_WARNING "Warning only %ldMB will be used.\n", MAXMEM>>20);
654 	if (max_pfn > MAX_NONPAE_PFN)
655 		printk(KERN_WARNING "Use a HIGHMEM64G enabled kernel.\n");
656 	else
657 		printk(KERN_WARNING "Use a HIGHMEM enabled kernel.\n");
658 	max_pfn = MAXMEM_PFN;
659 #else /* !CONFIG_HIGHMEM */
660 #ifndef CONFIG_HIGHMEM64G
661 	if (max_pfn > MAX_NONPAE_PFN) {
662 		max_pfn = MAX_NONPAE_PFN;
663 		printk(KERN_WARNING MSG_HIGHMEM_TRIMMED);
664 	}
665 #endif /* !CONFIG_HIGHMEM64G */
666 #endif /* !CONFIG_HIGHMEM */
667 }
668 
669 /*
670  * Determine low and high memory ranges:
671  */
672 void __init find_low_pfn_range(void)
673 {
674 	/* it could update max_pfn */
675 
676 	if (max_pfn <= MAXMEM_PFN)
677 		lowmem_pfn_init();
678 	else
679 		highmem_pfn_init();
680 }
681 
682 #ifndef CONFIG_NEED_MULTIPLE_NODES
683 void __init initmem_init(void)
684 {
685 #ifdef CONFIG_HIGHMEM
686 	highstart_pfn = highend_pfn = max_pfn;
687 	if (max_pfn > max_low_pfn)
688 		highstart_pfn = max_low_pfn;
689 	printk(KERN_NOTICE "%ldMB HIGHMEM available.\n",
690 		pages_to_mb(highend_pfn - highstart_pfn));
691 	high_memory = (void *) __va(highstart_pfn * PAGE_SIZE - 1) + 1;
692 #else
693 	high_memory = (void *) __va(max_low_pfn * PAGE_SIZE - 1) + 1;
694 #endif
695 
696 	memblock_set_node(0, PHYS_ADDR_MAX, &memblock.memory, 0);
697 	sparse_memory_present_with_active_regions(0);
698 
699 #ifdef CONFIG_FLATMEM
700 	max_mapnr = IS_ENABLED(CONFIG_HIGHMEM) ? highend_pfn : max_low_pfn;
701 #endif
702 	__vmalloc_start_set = true;
703 
704 	printk(KERN_NOTICE "%ldMB LOWMEM available.\n",
705 			pages_to_mb(max_low_pfn));
706 
707 	setup_bootmem_allocator();
708 }
709 #endif /* !CONFIG_NEED_MULTIPLE_NODES */
710 
711 void __init setup_bootmem_allocator(void)
712 {
713 	printk(KERN_INFO "  mapped low ram: 0 - %08lx\n",
714 		 max_pfn_mapped<<PAGE_SHIFT);
715 	printk(KERN_INFO "  low ram: 0 - %08lx\n", max_low_pfn<<PAGE_SHIFT);
716 }
717 
718 /*
719  * paging_init() sets up the page tables - note that the first 8MB are
720  * already mapped by head.S.
721  *
722  * This routines also unmaps the page at virtual kernel address 0, so
723  * that we can trap those pesky NULL-reference errors in the kernel.
724  */
725 void __init paging_init(void)
726 {
727 	pagetable_init();
728 
729 	__flush_tlb_all();
730 
731 	kmap_init();
732 
733 	/*
734 	 * NOTE: at this point the bootmem allocator is fully available.
735 	 */
736 	olpc_dt_build_devicetree();
737 	sparse_memory_present_with_active_regions(MAX_NUMNODES);
738 	sparse_init();
739 	zone_sizes_init();
740 }
741 
742 /*
743  * Test if the WP bit works in supervisor mode. It isn't supported on 386's
744  * and also on some strange 486's. All 586+'s are OK. This used to involve
745  * black magic jumps to work around some nasty CPU bugs, but fortunately the
746  * switch to using exceptions got rid of all that.
747  */
748 static void __init test_wp_bit(void)
749 {
750 	char z = 0;
751 
752 	printk(KERN_INFO "Checking if this processor honours the WP bit even in supervisor mode...");
753 
754 	__set_fixmap(FIX_WP_TEST, __pa_symbol(empty_zero_page), PAGE_KERNEL_RO);
755 
756 	if (probe_kernel_write((char *)fix_to_virt(FIX_WP_TEST), &z, 1)) {
757 		clear_fixmap(FIX_WP_TEST);
758 		printk(KERN_CONT "Ok.\n");
759 		return;
760 	}
761 
762 	printk(KERN_CONT "No.\n");
763 	panic("Linux doesn't support CPUs with broken WP.");
764 }
765 
766 void __init mem_init(void)
767 {
768 	pci_iommu_alloc();
769 
770 #ifdef CONFIG_FLATMEM
771 	BUG_ON(!mem_map);
772 #endif
773 	/*
774 	 * With CONFIG_DEBUG_PAGEALLOC initialization of highmem pages has to
775 	 * be done before memblock_free_all(). Memblock use free low memory for
776 	 * temporary data (see find_range_array()) and for this purpose can use
777 	 * pages that was already passed to the buddy allocator, hence marked as
778 	 * not accessible in the page tables when compiled with
779 	 * CONFIG_DEBUG_PAGEALLOC. Otherwise order of initialization is not
780 	 * important here.
781 	 */
782 	set_highmem_pages_init();
783 
784 	/* this will put all low memory onto the freelists */
785 	memblock_free_all();
786 
787 	after_bootmem = 1;
788 	x86_init.hyper.init_after_bootmem();
789 
790 	mem_init_print_info(NULL);
791 	printk(KERN_INFO "virtual kernel memory layout:\n"
792 		"    fixmap  : 0x%08lx - 0x%08lx   (%4ld kB)\n"
793 		"  cpu_entry : 0x%08lx - 0x%08lx   (%4ld kB)\n"
794 #ifdef CONFIG_HIGHMEM
795 		"    pkmap   : 0x%08lx - 0x%08lx   (%4ld kB)\n"
796 #endif
797 		"    vmalloc : 0x%08lx - 0x%08lx   (%4ld MB)\n"
798 		"    lowmem  : 0x%08lx - 0x%08lx   (%4ld MB)\n"
799 		"      .init : 0x%08lx - 0x%08lx   (%4ld kB)\n"
800 		"      .data : 0x%08lx - 0x%08lx   (%4ld kB)\n"
801 		"      .text : 0x%08lx - 0x%08lx   (%4ld kB)\n",
802 		FIXADDR_START, FIXADDR_TOP,
803 		(FIXADDR_TOP - FIXADDR_START) >> 10,
804 
805 		CPU_ENTRY_AREA_BASE,
806 		CPU_ENTRY_AREA_BASE + CPU_ENTRY_AREA_MAP_SIZE,
807 		CPU_ENTRY_AREA_MAP_SIZE >> 10,
808 
809 #ifdef CONFIG_HIGHMEM
810 		PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE,
811 		(LAST_PKMAP*PAGE_SIZE) >> 10,
812 #endif
813 
814 		VMALLOC_START, VMALLOC_END,
815 		(VMALLOC_END - VMALLOC_START) >> 20,
816 
817 		(unsigned long)__va(0), (unsigned long)high_memory,
818 		((unsigned long)high_memory - (unsigned long)__va(0)) >> 20,
819 
820 		(unsigned long)&__init_begin, (unsigned long)&__init_end,
821 		((unsigned long)&__init_end -
822 		 (unsigned long)&__init_begin) >> 10,
823 
824 		(unsigned long)&_etext, (unsigned long)&_edata,
825 		((unsigned long)&_edata - (unsigned long)&_etext) >> 10,
826 
827 		(unsigned long)&_text, (unsigned long)&_etext,
828 		((unsigned long)&_etext - (unsigned long)&_text) >> 10);
829 
830 	/*
831 	 * Check boundaries twice: Some fundamental inconsistencies can
832 	 * be detected at build time already.
833 	 */
834 #define __FIXADDR_TOP (-PAGE_SIZE)
835 #ifdef CONFIG_HIGHMEM
836 	BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP*PAGE_SIZE	> FIXADDR_START);
837 	BUILD_BUG_ON(VMALLOC_END			> PKMAP_BASE);
838 #endif
839 #define high_memory (-128UL << 20)
840 	BUILD_BUG_ON(VMALLOC_START			>= VMALLOC_END);
841 #undef high_memory
842 #undef __FIXADDR_TOP
843 
844 #ifdef CONFIG_HIGHMEM
845 	BUG_ON(PKMAP_BASE + LAST_PKMAP*PAGE_SIZE	> FIXADDR_START);
846 	BUG_ON(VMALLOC_END				> PKMAP_BASE);
847 #endif
848 	BUG_ON(VMALLOC_START				>= VMALLOC_END);
849 	BUG_ON((unsigned long)high_memory		> VMALLOC_START);
850 
851 	test_wp_bit();
852 }
853 
854 #ifdef CONFIG_MEMORY_HOTPLUG
855 int arch_add_memory(int nid, u64 start, u64 size,
856 			struct mhp_restrictions *restrictions)
857 {
858 	unsigned long start_pfn = start >> PAGE_SHIFT;
859 	unsigned long nr_pages = size >> PAGE_SHIFT;
860 
861 	return __add_pages(nid, start_pfn, nr_pages, restrictions);
862 }
863 
864 void arch_remove_memory(int nid, u64 start, u64 size,
865 			struct vmem_altmap *altmap)
866 {
867 	unsigned long start_pfn = start >> PAGE_SHIFT;
868 	unsigned long nr_pages = size >> PAGE_SHIFT;
869 
870 	__remove_pages(start_pfn, nr_pages, altmap);
871 }
872 #endif
873 
874 int kernel_set_to_readonly __read_mostly;
875 
876 void set_kernel_text_rw(void)
877 {
878 	unsigned long start = PFN_ALIGN(_text);
879 	unsigned long size = PFN_ALIGN(_etext) - start;
880 
881 	if (!kernel_set_to_readonly)
882 		return;
883 
884 	pr_debug("Set kernel text: %lx - %lx for read write\n",
885 		 start, start+size);
886 
887 	set_pages_rw(virt_to_page(start), size >> PAGE_SHIFT);
888 }
889 
890 void set_kernel_text_ro(void)
891 {
892 	unsigned long start = PFN_ALIGN(_text);
893 	unsigned long size = PFN_ALIGN(_etext) - start;
894 
895 	if (!kernel_set_to_readonly)
896 		return;
897 
898 	pr_debug("Set kernel text: %lx - %lx for read only\n",
899 		 start, start+size);
900 
901 	set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
902 }
903 
904 static void mark_nxdata_nx(void)
905 {
906 	/*
907 	 * When this called, init has already been executed and released,
908 	 * so everything past _etext should be NX.
909 	 */
910 	unsigned long start = PFN_ALIGN(_etext);
911 	/*
912 	 * This comes from is_kernel_text upper limit. Also HPAGE where used:
913 	 */
914 	unsigned long size = (((unsigned long)__init_end + HPAGE_SIZE) & HPAGE_MASK) - start;
915 
916 	if (__supported_pte_mask & _PAGE_NX)
917 		printk(KERN_INFO "NX-protecting the kernel data: %luk\n", size >> 10);
918 	set_memory_nx(start, size >> PAGE_SHIFT);
919 }
920 
921 void mark_rodata_ro(void)
922 {
923 	unsigned long start = PFN_ALIGN(_text);
924 	unsigned long size = (unsigned long)__end_rodata - start;
925 
926 	set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
927 	pr_info("Write protecting kernel text and read-only data: %luk\n",
928 		size >> 10);
929 
930 	kernel_set_to_readonly = 1;
931 
932 #ifdef CONFIG_CPA_DEBUG
933 	pr_info("Testing CPA: Reverting %lx-%lx\n", start, start + size);
934 	set_pages_rw(virt_to_page(start), size >> PAGE_SHIFT);
935 
936 	pr_info("Testing CPA: write protecting again\n");
937 	set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
938 #endif
939 	mark_nxdata_nx();
940 	if (__supported_pte_mask & _PAGE_NX)
941 		debug_checkwx();
942 }
943