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