ARM: dma-api: fix max_pfn off-by-one error in __dma_supported()
[linux/fpc-iii.git] / arch / x86 / mm / init_32.c
blob23df4885bbede4a72763e95b5dbab74bcfb1c3a5
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
4 * Copyright (C) 1995 Linus Torvalds
6 * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
7 */
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>
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>
57 #include "mm_internal.h"
59 unsigned long highstart_pfn, highend_pfn;
61 bool __read_mostly __vmalloc_start_set = false;
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.
68 static pmd_t * __init one_md_table_init(pgd_t *pgd)
70 p4d_t *p4d;
71 pud_t *pud;
72 pmd_t *pmd_table;
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));
83 return pmd_table;
85 #endif
86 p4d = p4d_offset(pgd, 0);
87 pud = pud_offset(p4d, 0);
88 pmd_table = pmd_offset(pud, 0);
90 return pmd_table;
94 * Create a page table and place a pointer to it in a middle page
95 * directory entry:
97 static pte_t * __init one_page_table_init(pmd_t *pmd)
99 if (!(pmd_val(*pmd) & _PAGE_PRESENT)) {
100 pte_t *page_table = (pte_t *)alloc_low_page();
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));
107 return pte_offset_kernel(pmd, 0);
110 pmd_t * __init populate_extra_pmd(unsigned long vaddr)
112 int pgd_idx = pgd_index(vaddr);
113 int pmd_idx = pmd_index(vaddr);
115 return one_md_table_init(swapper_pg_dir + pgd_idx) + pmd_idx;
118 pte_t * __init populate_extra_pte(unsigned long vaddr)
120 int pte_idx = pte_index(vaddr);
121 pmd_t *pmd;
123 pmd = populate_extra_pmd(vaddr);
124 return one_page_table_init(pmd) + pte_idx;
127 static unsigned long __init
128 page_table_range_init_count(unsigned long start, unsigned long end)
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;
137 if (pmd_idx_kmap_begin == pmd_idx_kmap_end)
138 return 0;
140 vaddr = start;
141 pgd_idx = pgd_index(vaddr);
142 pmd_idx = pmd_index(vaddr);
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;
152 pmd_idx = 0;
154 #endif
155 return count;
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)
162 #ifdef CONFIG_HIGHMEM
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.
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;
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;
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);
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();
189 paravirt_release_pte(__pa(pte) >> PAGE_SHIFT);
190 pte = newpte;
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;
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.
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.
208 static void __init
209 page_table_range_init(unsigned long start, unsigned long end, pgd_t *pgd_base)
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;
219 if (count)
220 adr = alloc_low_pages(count);
222 vaddr = start;
223 pgd_idx = pgd_index(vaddr);
224 pmd_idx = pmd_index(vaddr);
225 pgd = pgd_base + pgd_idx;
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);
235 vaddr += PMD_SIZE;
237 pmd_idx = 0;
241 static inline int is_kernel_text(unsigned long addr)
243 if (addr >= (unsigned long)_text && addr <= (unsigned long)__init_end)
244 return 1;
245 return 0;
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:
253 unsigned long __init
254 kernel_physical_mapping_init(unsigned long start,
255 unsigned long end,
256 unsigned long page_size_mask)
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;
270 start_pfn = start >> PAGE_SHIFT;
271 end_pfn = end >> PAGE_SHIFT;
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
278 * Second iteration will setup the appropriate attributes (NX, GLOBAL..)
279 * as desired for the kernel identity mapping.
281 * This two pass mechanism conforms to the TLB app note which says:
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."
287 mapping_iter = 1;
289 if (!boot_cpu_has(X86_FEATURE_PSE))
290 use_pse = 0;
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);
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;
313 * Map with big pages if possible, otherwise
314 * create normal page tables:
316 if (use_pse) {
317 unsigned int addr2;
318 pgprot_t prot = PAGE_KERNEL_LARGE;
320 * first pass will use the same initial
321 * identity mapping attribute + _PAGE_PSE.
323 pgprot_t init_prot =
324 __pgprot(PTE_IDENT_ATTR |
325 _PAGE_PSE);
327 pfn &= PMD_MASK >> PAGE_SHIFT;
328 addr2 = (pfn + PTRS_PER_PTE-1) * PAGE_SIZE +
329 PAGE_OFFSET + PAGE_SIZE-1;
331 if (is_kernel_text(addr) ||
332 is_kernel_text(addr2))
333 prot = PAGE_KERNEL_LARGE_EXEC;
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));
341 pfn += PTRS_PER_PTE;
342 continue;
344 pte = one_page_table_init(pmd);
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;
352 * first pass will use the same initial
353 * identity mapping attribute.
355 pgprot_t init_prot = __pgprot(PTE_IDENT_ATTR);
357 if (is_kernel_text(addr))
358 prot = PAGE_KERNEL_EXEC;
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));
369 if (mapping_iter == 1) {
371 * update direct mapping page count only in the first
372 * iteration.
374 update_page_count(PG_LEVEL_2M, pages_2m);
375 update_page_count(PG_LEVEL_4K, pages_4k);
378 * local global flush tlb, which will flush the previous
379 * mappings present in both small and large page TLB's.
381 __flush_tlb_all();
384 * Second iteration will set the actual desired PTE attributes.
386 mapping_iter = 2;
387 goto repeat;
389 return last_map_addr;
392 pte_t *kmap_pte;
394 static inline pte_t *kmap_get_fixmap_pte(unsigned long vaddr)
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);
403 static void __init kmap_init(void)
405 unsigned long kmap_vstart;
408 * Cache the first kmap pte:
410 kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
411 kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
414 #ifdef CONFIG_HIGHMEM
415 static void __init permanent_kmaps_init(pgd_t *pgd_base)
417 unsigned long vaddr;
418 pgd_t *pgd;
419 p4d_t *p4d;
420 pud_t *pud;
421 pmd_t *pmd;
422 pte_t *pte;
424 vaddr = PKMAP_BASE;
425 page_table_range_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base);
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;
435 void __init add_highpages_with_active_regions(int nid,
436 unsigned long start_pfn, unsigned long end_pfn)
438 phys_addr_t start, end;
439 u64 i;
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));
451 #else
452 static inline void permanent_kmaps_init(pgd_t *pgd_base)
455 #endif /* CONFIG_HIGHMEM */
457 void __init sync_initial_page_table(void)
459 clone_pgd_range(initial_page_table + KERNEL_PGD_BOUNDARY,
460 swapper_pg_dir + KERNEL_PGD_BOUNDARY,
461 KERNEL_PGD_PTRS);
464 * sync back low identity map too. It is used for example
465 * in the 32-bit EFI stub.
467 clone_pgd_range(initial_page_table,
468 swapper_pg_dir + KERNEL_PGD_BOUNDARY,
469 min(KERNEL_PGD_PTRS, KERNEL_PGD_BOUNDARY));
472 void __init native_pagetable_init(void)
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;
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.
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;
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;
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);
509 pte = pte_offset_kernel(pmd, va);
510 if (!pte_present(*pte))
511 break;
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);
517 paravirt_alloc_pmd(&init_mm, __pa(base) >> PAGE_SHIFT);
518 paging_init();
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.
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.
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.
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.
540 void __init early_ioremap_page_table_range_init(void)
542 pgd_t *pgd_base = swapper_pg_dir;
543 unsigned long vaddr, end;
546 * Fixed mappings, only the page table structure has to be
547 * created - mappings will be set by set_fixmap():
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();
555 static void __init pagetable_init(void)
557 pgd_t *pgd_base = swapper_pg_dir;
559 permanent_kmaps_init(pgd_base);
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);
571 /* user-defined highmem size */
572 static unsigned int highmem_pages = -1;
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.
579 static int __init parse_highmem(char *arg)
581 if (!arg)
582 return -EINVAL;
584 highmem_pages = memparse(arg, &arg) >> PAGE_SHIFT;
585 return 0;
587 early_param("highmem", parse_highmem);
589 #define MSG_HIGHMEM_TOO_BIG \
590 "highmem size (%luMB) is bigger than pages available (%luMB)!\n"
592 #define MSG_LOWMEM_TOO_SMALL \
593 "highmem size (%luMB) results in <64MB lowmem, ignoring it!\n"
595 * All of RAM fits into lowmem - but if user wants highmem
596 * artificially via the highmem=x boot parameter then create
597 * it:
599 static void __init lowmem_pfn_init(void)
601 /* max_low_pfn is 0, we already have early_res support */
602 max_low_pfn = max_pfn;
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;
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;
618 max_low_pfn -= highmem_pages;
620 #else
621 if (highmem_pages)
622 printk(KERN_ERR "ignoring highmem size on non-highmem kernel!\n");
623 #endif
626 #define MSG_HIGHMEM_TOO_SMALL \
627 "only %luMB highmem pages available, ignoring highmem size of %luMB!\n"
629 #define MSG_HIGHMEM_TRIMMED \
630 "Warning: only 4GB will be used. Use a HIGHMEM64G enabled kernel!\n"
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:
635 static void __init highmem_pfn_init(void)
637 max_low_pfn = MAXMEM_PFN;
639 if (highmem_pages == -1)
640 highmem_pages = max_pfn - MAXMEM_PFN;
642 if (highmem_pages + MAXMEM_PFN < max_pfn)
643 max_pfn = MAXMEM_PFN + highmem_pages;
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;
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);
665 #endif /* !CONFIG_HIGHMEM64G */
666 #endif /* !CONFIG_HIGHMEM */
670 * Determine low and high memory ranges:
672 void __init find_low_pfn_range(void)
674 /* it could update max_pfn */
676 if (max_pfn <= MAXMEM_PFN)
677 lowmem_pfn_init();
678 else
679 highmem_pfn_init();
682 #ifndef CONFIG_NEED_MULTIPLE_NODES
683 void __init initmem_init(void)
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
696 memblock_set_node(0, PHYS_ADDR_MAX, &memblock.memory, 0);
697 sparse_memory_present_with_active_regions(0);
699 #ifdef CONFIG_FLATMEM
700 max_mapnr = IS_ENABLED(CONFIG_HIGHMEM) ? highend_pfn : max_low_pfn;
701 #endif
702 __vmalloc_start_set = true;
704 printk(KERN_NOTICE "%ldMB LOWMEM available.\n",
705 pages_to_mb(max_low_pfn));
707 setup_bootmem_allocator();
709 #endif /* !CONFIG_NEED_MULTIPLE_NODES */
711 void __init setup_bootmem_allocator(void)
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);
719 * paging_init() sets up the page tables - note that the first 8MB are
720 * already mapped by head.S.
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.
725 void __init paging_init(void)
727 pagetable_init();
729 __flush_tlb_all();
731 kmap_init();
734 * NOTE: at this point the bootmem allocator is fully available.
736 olpc_dt_build_devicetree();
737 sparse_memory_present_with_active_regions(MAX_NUMNODES);
738 sparse_init();
739 zone_sizes_init();
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.
748 static void __init test_wp_bit(void)
750 char z = 0;
752 printk(KERN_INFO "Checking if this processor honours the WP bit even in supervisor mode...");
754 __set_fixmap(FIX_WP_TEST, __pa_symbol(empty_zero_page), PAGE_KERNEL_RO);
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;
762 printk(KERN_CONT "No.\n");
763 panic("Linux doesn't support CPUs with broken WP.");
766 void __init mem_init(void)
768 pci_iommu_alloc();
770 #ifdef CONFIG_FLATMEM
771 BUG_ON(!mem_map);
772 #endif
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.
782 set_highmem_pages_init();
784 /* this will put all low memory onto the freelists */
785 memblock_free_all();
787 after_bootmem = 1;
788 x86_init.hyper.init_after_bootmem();
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,
805 CPU_ENTRY_AREA_BASE,
806 CPU_ENTRY_AREA_BASE + CPU_ENTRY_AREA_MAP_SIZE,
807 CPU_ENTRY_AREA_MAP_SIZE >> 10,
809 #ifdef CONFIG_HIGHMEM
810 PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE,
811 (LAST_PKMAP*PAGE_SIZE) >> 10,
812 #endif
814 VMALLOC_START, VMALLOC_END,
815 (VMALLOC_END - VMALLOC_START) >> 20,
817 (unsigned long)__va(0), (unsigned long)high_memory,
818 ((unsigned long)high_memory - (unsigned long)__va(0)) >> 20,
820 (unsigned long)&__init_begin, (unsigned long)&__init_end,
821 ((unsigned long)&__init_end -
822 (unsigned long)&__init_begin) >> 10,
824 (unsigned long)&_etext, (unsigned long)&_edata,
825 ((unsigned long)&_edata - (unsigned long)&_etext) >> 10,
827 (unsigned long)&_text, (unsigned long)&_etext,
828 ((unsigned long)&_etext - (unsigned long)&_text) >> 10);
831 * Check boundaries twice: Some fundamental inconsistencies can
832 * be detected at build time already.
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
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);
851 test_wp_bit();
854 #ifdef CONFIG_MEMORY_HOTPLUG
855 int arch_add_memory(int nid, u64 start, u64 size,
856 struct mhp_restrictions *restrictions)
858 unsigned long start_pfn = start >> PAGE_SHIFT;
859 unsigned long nr_pages = size >> PAGE_SHIFT;
861 return __add_pages(nid, start_pfn, nr_pages, restrictions);
864 void arch_remove_memory(int nid, u64 start, u64 size,
865 struct vmem_altmap *altmap)
867 unsigned long start_pfn = start >> PAGE_SHIFT;
868 unsigned long nr_pages = size >> PAGE_SHIFT;
870 __remove_pages(start_pfn, nr_pages, altmap);
872 #endif
874 int kernel_set_to_readonly __read_mostly;
876 static void mark_nxdata_nx(void)
879 * When this called, init has already been executed and released,
880 * so everything past _etext should be NX.
882 unsigned long start = PFN_ALIGN(_etext);
884 * This comes from is_kernel_text upper limit. Also HPAGE where used:
886 unsigned long size = (((unsigned long)__init_end + HPAGE_SIZE) & HPAGE_MASK) - start;
888 if (__supported_pte_mask & _PAGE_NX)
889 printk(KERN_INFO "NX-protecting the kernel data: %luk\n", size >> 10);
890 set_memory_nx(start, size >> PAGE_SHIFT);
893 void mark_rodata_ro(void)
895 unsigned long start = PFN_ALIGN(_text);
896 unsigned long size = (unsigned long)__end_rodata - start;
898 set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
899 pr_info("Write protecting kernel text and read-only data: %luk\n",
900 size >> 10);
902 kernel_set_to_readonly = 1;
904 #ifdef CONFIG_CPA_DEBUG
905 pr_info("Testing CPA: Reverting %lx-%lx\n", start, start + size);
906 set_pages_rw(virt_to_page(start), size >> PAGE_SHIFT);
908 pr_info("Testing CPA: write protecting again\n");
909 set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
910 #endif
911 mark_nxdata_nx();
912 if (__supported_pte_mask & _PAGE_NX)
913 debug_checkwx();