3 * Copyright (C) 1995 Linus Torvalds
5 * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
8 #include <linux/module.h>
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>
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/bootmem.h>
28 #include <linux/slab.h>
29 #include <linux/proc_fs.h>
30 #include <linux/memory_hotplug.h>
31 #include <linux/initrd.h>
32 #include <linux/cpumask.h>
35 #include <asm/bios_ebda.h>
36 #include <asm/processor.h>
37 #include <asm/system.h>
38 #include <asm/uaccess.h>
39 #include <asm/pgtable.h>
41 #include <asm/fixmap.h>
46 #include <asm/tlbflush.h>
47 #include <asm/pgalloc.h>
48 #include <asm/sections.h>
49 #include <asm/paravirt.h>
50 #include <asm/setup.h>
51 #include <asm/cacheflush.h>
53 unsigned long max_low_pfn_mapped;
54 unsigned long max_pfn_mapped;
56 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
57 unsigned long highstart_pfn, highend_pfn;
59 static noinline int do_test_wp_bit(void);
62 extern unsigned long __initdata table_start;
63 extern unsigned long __meminitdata table_end;
64 extern unsigned long __meminitdata table_top;
66 static __init void *alloc_low_page(void)
68 unsigned long pfn = table_end++;
72 panic("alloc_low_page: ran out of memory");
74 adr = __va(pfn * PAGE_SIZE);
75 memset(adr, 0, PAGE_SIZE);
80 * Creates a middle page table and puts a pointer to it in the
81 * given global directory entry. This only returns the gd entry
82 * in non-PAE compilation mode, since the middle layer is folded.
84 static pmd_t * __init one_md_table_init(pgd_t *pgd)
90 if (!(pgd_val(*pgd) & _PAGE_PRESENT)) {
92 pmd_table = (pmd_t *)alloc_bootmem_low_pages(PAGE_SIZE);
94 pmd_table = (pmd_t *)alloc_low_page();
95 paravirt_alloc_pmd(&init_mm, __pa(pmd_table) >> PAGE_SHIFT);
96 set_pgd(pgd, __pgd(__pa(pmd_table) | _PAGE_PRESENT));
97 pud = pud_offset(pgd, 0);
98 BUG_ON(pmd_table != pmd_offset(pud, 0));
103 pud = pud_offset(pgd, 0);
104 pmd_table = pmd_offset(pud, 0);
110 * Create a page table and place a pointer to it in a middle page
113 static pte_t * __init one_page_table_init(pmd_t *pmd)
115 if (!(pmd_val(*pmd) & _PAGE_PRESENT)) {
116 pte_t *page_table = NULL;
119 #ifdef CONFIG_DEBUG_PAGEALLOC
120 page_table = (pte_t *) alloc_bootmem_pages(PAGE_SIZE);
124 (pte_t *)alloc_bootmem_low_pages(PAGE_SIZE);
126 page_table = (pte_t *)alloc_low_page();
128 paravirt_alloc_pte(&init_mm, __pa(page_table) >> PAGE_SHIFT);
129 set_pmd(pmd, __pmd(__pa(page_table) | _PAGE_TABLE));
130 BUG_ON(page_table != pte_offset_kernel(pmd, 0));
133 return pte_offset_kernel(pmd, 0);
136 static pte_t *__init page_table_kmap_check(pte_t *pte, pmd_t *pmd,
137 unsigned long vaddr, pte_t *lastpte)
139 #ifdef CONFIG_HIGHMEM
141 * Something (early fixmap) may already have put a pte
142 * page here, which causes the page table allocation
143 * to become nonlinear. Attempt to fix it, and if it
144 * is still nonlinear then we have to bug.
146 int pmd_idx_kmap_begin = fix_to_virt(FIX_KMAP_END) >> PMD_SHIFT;
147 int pmd_idx_kmap_end = fix_to_virt(FIX_KMAP_BEGIN) >> PMD_SHIFT;
149 if (pmd_idx_kmap_begin != pmd_idx_kmap_end
150 && (vaddr >> PMD_SHIFT) >= pmd_idx_kmap_begin
151 && (vaddr >> PMD_SHIFT) <= pmd_idx_kmap_end
152 && ((__pa(pte) >> PAGE_SHIFT) < table_start
153 || (__pa(pte) >> PAGE_SHIFT) >= table_end)) {
157 BUG_ON(after_bootmem);
158 newpte = alloc_low_page();
159 for (i = 0; i < PTRS_PER_PTE; i++)
160 set_pte(newpte + i, pte[i]);
162 paravirt_alloc_pte(&init_mm, __pa(newpte) >> PAGE_SHIFT);
163 set_pmd(pmd, __pmd(__pa(newpte)|_PAGE_TABLE));
164 BUG_ON(newpte != pte_offset_kernel(pmd, 0));
167 paravirt_release_pte(__pa(pte) >> PAGE_SHIFT);
170 BUG_ON(vaddr < fix_to_virt(FIX_KMAP_BEGIN - 1)
171 && vaddr > fix_to_virt(FIX_KMAP_END)
172 && lastpte && lastpte + PTRS_PER_PTE != pte);
178 * This function initializes a certain range of kernel virtual memory
179 * with new bootmem page tables, everywhere page tables are missing in
182 * NOTE: The pagetables are allocated contiguous on the physical space
183 * so we can cache the place of the first one and move around without
184 * checking the pgd every time.
187 page_table_range_init(unsigned long start, unsigned long end, pgd_t *pgd_base)
189 int pgd_idx, pmd_idx;
196 pgd_idx = pgd_index(vaddr);
197 pmd_idx = pmd_index(vaddr);
198 pgd = pgd_base + pgd_idx;
200 for ( ; (pgd_idx < PTRS_PER_PGD) && (vaddr != end); pgd++, pgd_idx++) {
201 pmd = one_md_table_init(pgd);
202 pmd = pmd + pmd_index(vaddr);
203 for (; (pmd_idx < PTRS_PER_PMD) && (vaddr != end);
205 pte = page_table_kmap_check(one_page_table_init(pmd),
214 static inline int is_kernel_text(unsigned long addr)
216 if (addr >= PAGE_OFFSET && addr <= (unsigned long)__init_end)
222 * This maps the physical memory to kernel virtual address space, a total
223 * of max_low_pfn pages, by creating page tables starting from address
226 void __init kernel_physical_mapping_init(unsigned long start_pfn,
227 unsigned long end_pfn,
230 pgd_t *pgd_base = swapper_pg_dir;
231 int pgd_idx, pmd_idx, pte_ofs;
236 unsigned pages_2m, pages_4k;
240 * First iteration will setup identity mapping using large/small pages
241 * based on use_pse, with other attributes same as set by
242 * the early code in head_32.S
244 * Second iteration will setup the appropriate attributes (NX, GLOBAL..)
245 * as desired for the kernel identity mapping.
247 * This two pass mechanism conforms to the TLB app note which says:
249 * "Software should not write to a paging-structure entry in a way
250 * that would change, for any linear address, both the page size
251 * and either the page frame or attributes."
259 pages_2m = pages_4k = 0;
261 pgd_idx = pgd_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
262 pgd = pgd_base + pgd_idx;
263 for (; pgd_idx < PTRS_PER_PGD; pgd++, pgd_idx++) {
264 pmd = one_md_table_init(pgd);
268 #ifdef CONFIG_X86_PAE
269 pmd_idx = pmd_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
274 for (; pmd_idx < PTRS_PER_PMD && pfn < end_pfn;
276 unsigned int addr = pfn * PAGE_SIZE + PAGE_OFFSET;
279 * Map with big pages if possible, otherwise
280 * create normal page tables:
284 pgprot_t prot = PAGE_KERNEL_LARGE;
286 * first pass will use the same initial
287 * identity mapping attribute + _PAGE_PSE.
290 __pgprot(PTE_IDENT_ATTR |
293 addr2 = (pfn + PTRS_PER_PTE-1) * PAGE_SIZE +
294 PAGE_OFFSET + PAGE_SIZE-1;
296 if (is_kernel_text(addr) ||
297 is_kernel_text(addr2))
298 prot = PAGE_KERNEL_LARGE_EXEC;
301 if (mapping_iter == 1)
302 set_pmd(pmd, pfn_pmd(pfn, init_prot));
304 set_pmd(pmd, pfn_pmd(pfn, prot));
309 pte = one_page_table_init(pmd);
311 pte_ofs = pte_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
313 for (; pte_ofs < PTRS_PER_PTE && pfn < end_pfn;
314 pte++, pfn++, pte_ofs++, addr += PAGE_SIZE) {
315 pgprot_t prot = PAGE_KERNEL;
317 * first pass will use the same initial
318 * identity mapping attribute.
320 pgprot_t init_prot = __pgprot(PTE_IDENT_ATTR);
322 if (is_kernel_text(addr))
323 prot = PAGE_KERNEL_EXEC;
326 if (mapping_iter == 1)
327 set_pte(pte, pfn_pte(pfn, init_prot));
329 set_pte(pte, pfn_pte(pfn, prot));
333 if (mapping_iter == 1) {
335 * update direct mapping page count only in the first
338 update_page_count(PG_LEVEL_2M, pages_2m);
339 update_page_count(PG_LEVEL_4K, pages_4k);
342 * local global flush tlb, which will flush the previous
343 * mappings present in both small and large page TLB's.
348 * Second iteration will set the actual desired PTE attributes.
358 static inline pte_t *kmap_get_fixmap_pte(unsigned long vaddr)
360 return pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr),
361 vaddr), vaddr), vaddr);
364 static void __init kmap_init(void)
366 unsigned long kmap_vstart;
369 * Cache the first kmap pte:
371 kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
372 kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
374 kmap_prot = PAGE_KERNEL;
377 #ifdef CONFIG_HIGHMEM
378 static void __init permanent_kmaps_init(pgd_t *pgd_base)
387 page_table_range_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base);
389 pgd = swapper_pg_dir + pgd_index(vaddr);
390 pud = pud_offset(pgd, vaddr);
391 pmd = pmd_offset(pud, vaddr);
392 pte = pte_offset_kernel(pmd, vaddr);
393 pkmap_page_table = pte;
396 static void __init add_one_highpage_init(struct page *page, int pfn)
398 ClearPageReserved(page);
399 init_page_count(page);
404 struct add_highpages_data {
405 unsigned long start_pfn;
406 unsigned long end_pfn;
409 static int __init add_highpages_work_fn(unsigned long start_pfn,
410 unsigned long end_pfn, void *datax)
414 unsigned long final_start_pfn, final_end_pfn;
415 struct add_highpages_data *data;
417 data = (struct add_highpages_data *)datax;
419 final_start_pfn = max(start_pfn, data->start_pfn);
420 final_end_pfn = min(end_pfn, data->end_pfn);
421 if (final_start_pfn >= final_end_pfn)
424 for (node_pfn = final_start_pfn; node_pfn < final_end_pfn;
426 if (!pfn_valid(node_pfn))
428 page = pfn_to_page(node_pfn);
429 add_one_highpage_init(page, node_pfn);
436 void __init add_highpages_with_active_regions(int nid, unsigned long start_pfn,
437 unsigned long end_pfn)
439 struct add_highpages_data data;
441 data.start_pfn = start_pfn;
442 data.end_pfn = end_pfn;
444 work_with_active_regions(nid, add_highpages_work_fn, &data);
448 static inline void permanent_kmaps_init(pgd_t *pgd_base)
451 #endif /* CONFIG_HIGHMEM */
453 void __init native_pagetable_setup_start(pgd_t *base)
455 unsigned long pfn, va;
462 * Remove any mappings which extend past the end of physical
463 * memory from the boot time page table:
465 for (pfn = max_low_pfn + 1; pfn < 1<<(32-PAGE_SHIFT); pfn++) {
466 va = PAGE_OFFSET + (pfn<<PAGE_SHIFT);
467 pgd = base + pgd_index(va);
468 if (!pgd_present(*pgd))
471 pud = pud_offset(pgd, va);
472 pmd = pmd_offset(pud, va);
473 if (!pmd_present(*pmd))
476 pte = pte_offset_kernel(pmd, va);
477 if (!pte_present(*pte))
480 pte_clear(NULL, va, pte);
482 paravirt_alloc_pmd(&init_mm, __pa(base) >> PAGE_SHIFT);
485 void __init native_pagetable_setup_done(pgd_t *base)
490 * Build a proper pagetable for the kernel mappings. Up until this
491 * point, we've been running on some set of pagetables constructed by
494 * If we're booting on native hardware, this will be a pagetable
495 * constructed in arch/x86/kernel/head_32.S. The root of the
496 * pagetable will be swapper_pg_dir.
498 * If we're booting paravirtualized under a hypervisor, then there are
499 * more options: we may already be running PAE, and the pagetable may
500 * or may not be based in swapper_pg_dir. In any case,
501 * paravirt_pagetable_setup_start() will set up swapper_pg_dir
502 * appropriately for the rest of the initialization to work.
504 * In general, pagetable_init() assumes that the pagetable may already
505 * be partially populated, and so it avoids stomping on any existing
508 void __init early_ioremap_page_table_range_init(void)
510 pgd_t *pgd_base = swapper_pg_dir;
511 unsigned long vaddr, end;
514 * Fixed mappings, only the page table structure has to be
515 * created - mappings will be set by set_fixmap():
517 vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
518 end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK;
519 page_table_range_init(vaddr, end, pgd_base);
520 early_ioremap_reset();
523 static void __init pagetable_init(void)
525 pgd_t *pgd_base = swapper_pg_dir;
527 permanent_kmaps_init(pgd_base);
530 #ifdef CONFIG_ACPI_SLEEP
532 * ACPI suspend needs this for resume, because things like the intel-agp
533 * driver might have split up a kernel 4MB mapping.
535 char swsusp_pg_dir[PAGE_SIZE]
536 __attribute__ ((aligned(PAGE_SIZE)));
538 static inline void save_pg_dir(void)
540 memcpy(swsusp_pg_dir, swapper_pg_dir, PAGE_SIZE);
542 #else /* !CONFIG_ACPI_SLEEP */
543 static inline void save_pg_dir(void)
546 #endif /* !CONFIG_ACPI_SLEEP */
548 void zap_low_mappings(void)
553 * Zap initial low-memory mappings.
555 * Note that "pgd_clear()" doesn't do it for
556 * us, because pgd_clear() is a no-op on i386.
558 for (i = 0; i < KERNEL_PGD_BOUNDARY; i++) {
559 #ifdef CONFIG_X86_PAE
560 set_pgd(swapper_pg_dir+i, __pgd(1 + __pa(empty_zero_page)));
562 set_pgd(swapper_pg_dir+i, __pgd(0));
570 pteval_t __supported_pte_mask __read_mostly = ~(_PAGE_NX | _PAGE_GLOBAL | _PAGE_IOMAP);
571 EXPORT_SYMBOL_GPL(__supported_pte_mask);
573 #ifdef CONFIG_X86_PAE
575 static int disable_nx __initdata;
580 * Control non executable mappings.
585 static int __init noexec_setup(char *str)
587 if (!str || !strcmp(str, "on")) {
589 __supported_pte_mask |= _PAGE_NX;
593 if (!strcmp(str, "off")) {
595 __supported_pte_mask &= ~_PAGE_NX;
603 early_param("noexec", noexec_setup);
605 static void __init set_nx(void)
607 unsigned int v[4], l, h;
609 if (cpu_has_pae && (cpuid_eax(0x80000000) > 0x80000001)) {
610 cpuid(0x80000001, &v[0], &v[1], &v[2], &v[3]);
612 if ((v[3] & (1 << 20)) && !disable_nx) {
613 rdmsr(MSR_EFER, l, h);
615 wrmsr(MSR_EFER, l, h);
617 __supported_pte_mask |= _PAGE_NX;
623 /* user-defined highmem size */
624 static unsigned int highmem_pages = -1;
627 * highmem=size forces highmem to be exactly 'size' bytes.
628 * This works even on boxes that have no highmem otherwise.
629 * This also works to reduce highmem size on bigger boxes.
631 static int __init parse_highmem(char *arg)
636 highmem_pages = memparse(arg, &arg) >> PAGE_SHIFT;
639 early_param("highmem", parse_highmem);
641 #define MSG_HIGHMEM_TOO_BIG \
642 "highmem size (%luMB) is bigger than pages available (%luMB)!\n"
644 #define MSG_LOWMEM_TOO_SMALL \
645 "highmem size (%luMB) results in <64MB lowmem, ignoring it!\n"
647 * All of RAM fits into lowmem - but if user wants highmem
648 * artificially via the highmem=x boot parameter then create
651 void __init lowmem_pfn_init(void)
653 /* max_low_pfn is 0, we already have early_res support */
654 max_low_pfn = max_pfn;
656 if (highmem_pages == -1)
658 #ifdef CONFIG_HIGHMEM
659 if (highmem_pages >= max_pfn) {
660 printk(KERN_ERR MSG_HIGHMEM_TOO_BIG,
661 pages_to_mb(highmem_pages), pages_to_mb(max_pfn));
665 if (max_low_pfn - highmem_pages < 64*1024*1024/PAGE_SIZE) {
666 printk(KERN_ERR MSG_LOWMEM_TOO_SMALL,
667 pages_to_mb(highmem_pages));
670 max_low_pfn -= highmem_pages;
674 printk(KERN_ERR "ignoring highmem size on non-highmem kernel!\n");
678 #define MSG_HIGHMEM_TOO_SMALL \
679 "only %luMB highmem pages available, ignoring highmem size of %luMB!\n"
681 #define MSG_HIGHMEM_TRIMMED \
682 "Warning: only 4GB will be used. Use a HIGHMEM64G enabled kernel!\n"
684 * We have more RAM than fits into lowmem - we try to put it into
685 * highmem, also taking the highmem=x boot parameter into account:
687 void __init highmem_pfn_init(void)
689 max_low_pfn = MAXMEM_PFN;
691 if (highmem_pages == -1)
692 highmem_pages = max_pfn - MAXMEM_PFN;
694 if (highmem_pages + MAXMEM_PFN < max_pfn)
695 max_pfn = MAXMEM_PFN + highmem_pages;
697 if (highmem_pages + MAXMEM_PFN > max_pfn) {
698 printk(KERN_WARNING MSG_HIGHMEM_TOO_SMALL,
699 pages_to_mb(max_pfn - MAXMEM_PFN),
700 pages_to_mb(highmem_pages));
703 #ifndef CONFIG_HIGHMEM
704 /* Maximum memory usable is what is directly addressable */
705 printk(KERN_WARNING "Warning only %ldMB will be used.\n", MAXMEM>>20);
706 if (max_pfn > MAX_NONPAE_PFN)
707 printk(KERN_WARNING "Use a HIGHMEM64G enabled kernel.\n");
709 printk(KERN_WARNING "Use a HIGHMEM enabled kernel.\n");
710 max_pfn = MAXMEM_PFN;
711 #else /* !CONFIG_HIGHMEM */
712 #ifndef CONFIG_HIGHMEM64G
713 if (max_pfn > MAX_NONPAE_PFN) {
714 max_pfn = MAX_NONPAE_PFN;
715 printk(KERN_WARNING MSG_HIGHMEM_TRIMMED);
717 #endif /* !CONFIG_HIGHMEM64G */
718 #endif /* !CONFIG_HIGHMEM */
722 * Determine low and high memory ranges:
724 void __init find_low_pfn_range(void)
726 /* it could update max_pfn */
728 if (max_pfn <= MAXMEM_PFN)
734 #ifndef CONFIG_NEED_MULTIPLE_NODES
735 void __init initmem_init(unsigned long start_pfn,
736 unsigned long end_pfn)
738 #ifdef CONFIG_HIGHMEM
739 highstart_pfn = highend_pfn = max_pfn;
740 if (max_pfn > max_low_pfn)
741 highstart_pfn = max_low_pfn;
742 memory_present(0, 0, highend_pfn);
743 e820_register_active_regions(0, 0, highend_pfn);
744 printk(KERN_NOTICE "%ldMB HIGHMEM available.\n",
745 pages_to_mb(highend_pfn - highstart_pfn));
746 num_physpages = highend_pfn;
747 high_memory = (void *) __va(highstart_pfn * PAGE_SIZE - 1) + 1;
749 memory_present(0, 0, max_low_pfn);
750 e820_register_active_regions(0, 0, max_low_pfn);
751 num_physpages = max_low_pfn;
752 high_memory = (void *) __va(max_low_pfn * PAGE_SIZE - 1) + 1;
754 #ifdef CONFIG_FLATMEM
755 max_mapnr = num_physpages;
757 printk(KERN_NOTICE "%ldMB LOWMEM available.\n",
758 pages_to_mb(max_low_pfn));
760 setup_bootmem_allocator();
762 #endif /* !CONFIG_NEED_MULTIPLE_NODES */
764 static void __init zone_sizes_init(void)
766 unsigned long max_zone_pfns[MAX_NR_ZONES];
767 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
768 max_zone_pfns[ZONE_DMA] =
769 virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
770 max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
771 #ifdef CONFIG_HIGHMEM
772 max_zone_pfns[ZONE_HIGHMEM] = highend_pfn;
775 free_area_init_nodes(max_zone_pfns);
778 static unsigned long __init setup_node_bootmem(int nodeid,
779 unsigned long start_pfn,
780 unsigned long end_pfn,
781 unsigned long bootmap)
783 unsigned long bootmap_size;
785 if (start_pfn > max_low_pfn)
787 if (end_pfn > max_low_pfn)
788 end_pfn = max_low_pfn;
790 /* don't touch min_low_pfn */
791 bootmap_size = init_bootmem_node(NODE_DATA(nodeid),
792 bootmap >> PAGE_SHIFT,
794 printk(KERN_INFO " node %d low ram: %08lx - %08lx\n",
795 nodeid, start_pfn<<PAGE_SHIFT, end_pfn<<PAGE_SHIFT);
796 printk(KERN_INFO " node %d bootmap %08lx - %08lx\n",
797 nodeid, bootmap, bootmap + bootmap_size);
798 free_bootmem_with_active_regions(nodeid, end_pfn);
799 early_res_to_bootmem(start_pfn<<PAGE_SHIFT, end_pfn<<PAGE_SHIFT);
801 return bootmap + bootmap_size;
804 void __init setup_bootmem_allocator(void)
807 unsigned long bootmap_size, bootmap;
809 * Initialize the boot-time allocator (with low memory only):
811 bootmap_size = bootmem_bootmap_pages(max_low_pfn)<<PAGE_SHIFT;
812 bootmap = find_e820_area(0, max_pfn_mapped<<PAGE_SHIFT, bootmap_size,
815 panic("Cannot find bootmem map of size %ld\n", bootmap_size);
816 reserve_early(bootmap, bootmap + bootmap_size, "BOOTMAP");
818 printk(KERN_INFO " mapped low ram: 0 - %08lx\n",
819 max_pfn_mapped<<PAGE_SHIFT);
820 printk(KERN_INFO " low ram: 0 - %08lx\n", max_low_pfn<<PAGE_SHIFT);
822 #ifdef CONFIG_NEED_MULTIPLE_NODES
823 for_each_online_node(nodeid)
824 bootmap = setup_node_bootmem(nodeid, node_start_pfn[nodeid],
825 node_end_pfn[nodeid], bootmap);
827 bootmap = setup_node_bootmem(0, 0, max_low_pfn, bootmap);
834 * paging_init() sets up the page tables - note that the first 8MB are
835 * already mapped by head.S.
837 * This routines also unmaps the page at virtual kernel address 0, so
838 * that we can trap those pesky NULL-reference errors in the kernel.
840 void __init paging_init(void)
849 * NOTE: at this point the bootmem allocator is fully available.
856 * Test if the WP bit works in supervisor mode. It isn't supported on 386's
857 * and also on some strange 486's. All 586+'s are OK. This used to involve
858 * black magic jumps to work around some nasty CPU bugs, but fortunately the
859 * switch to using exceptions got rid of all that.
861 static void __init test_wp_bit(void)
864 "Checking if this processor honours the WP bit even in supervisor mode...");
866 /* Any page-aligned address will do, the test is non-destructive */
867 __set_fixmap(FIX_WP_TEST, __pa(&swapper_pg_dir), PAGE_READONLY);
868 boot_cpu_data.wp_works_ok = do_test_wp_bit();
869 clear_fixmap(FIX_WP_TEST);
871 if (!boot_cpu_data.wp_works_ok) {
872 printk(KERN_CONT "No.\n");
873 #ifdef CONFIG_X86_WP_WORKS_OK
875 "This kernel doesn't support CPU's with broken WP. Recompile it for a 386!");
878 printk(KERN_CONT "Ok.\n");
882 static struct kcore_list kcore_mem, kcore_vmalloc;
884 void __init mem_init(void)
886 int codesize, reservedpages, datasize, initsize;
891 #ifdef CONFIG_FLATMEM
894 /* this will put all low memory onto the freelists */
895 totalram_pages += free_all_bootmem();
898 for (tmp = 0; tmp < max_low_pfn; tmp++)
900 * Only count reserved RAM pages:
902 if (page_is_ram(tmp) && PageReserved(pfn_to_page(tmp)))
905 set_highmem_pages_init();
907 codesize = (unsigned long) &_etext - (unsigned long) &_text;
908 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
909 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
911 kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
912 kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
913 VMALLOC_END-VMALLOC_START);
915 printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, "
916 "%dk reserved, %dk data, %dk init, %ldk highmem)\n",
917 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
918 num_physpages << (PAGE_SHIFT-10),
920 reservedpages << (PAGE_SHIFT-10),
923 (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10))
926 printk(KERN_INFO "virtual kernel memory layout:\n"
927 " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
928 #ifdef CONFIG_HIGHMEM
929 " pkmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
931 " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n"
932 " lowmem : 0x%08lx - 0x%08lx (%4ld MB)\n"
933 " .init : 0x%08lx - 0x%08lx (%4ld kB)\n"
934 " .data : 0x%08lx - 0x%08lx (%4ld kB)\n"
935 " .text : 0x%08lx - 0x%08lx (%4ld kB)\n",
936 FIXADDR_START, FIXADDR_TOP,
937 (FIXADDR_TOP - FIXADDR_START) >> 10,
939 #ifdef CONFIG_HIGHMEM
940 PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE,
941 (LAST_PKMAP*PAGE_SIZE) >> 10,
944 VMALLOC_START, VMALLOC_END,
945 (VMALLOC_END - VMALLOC_START) >> 20,
947 (unsigned long)__va(0), (unsigned long)high_memory,
948 ((unsigned long)high_memory - (unsigned long)__va(0)) >> 20,
950 (unsigned long)&__init_begin, (unsigned long)&__init_end,
951 ((unsigned long)&__init_end -
952 (unsigned long)&__init_begin) >> 10,
954 (unsigned long)&_etext, (unsigned long)&_edata,
955 ((unsigned long)&_edata - (unsigned long)&_etext) >> 10,
957 (unsigned long)&_text, (unsigned long)&_etext,
958 ((unsigned long)&_etext - (unsigned long)&_text) >> 10);
961 * Check boundaries twice: Some fundamental inconsistencies can
962 * be detected at build time already.
964 #define __FIXADDR_TOP (-PAGE_SIZE)
965 #ifdef CONFIG_HIGHMEM
966 BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP*PAGE_SIZE > FIXADDR_START);
967 BUILD_BUG_ON(VMALLOC_END > PKMAP_BASE);
969 #define high_memory (-128UL << 20)
970 BUILD_BUG_ON(VMALLOC_START >= VMALLOC_END);
974 #ifdef CONFIG_HIGHMEM
975 BUG_ON(PKMAP_BASE + LAST_PKMAP*PAGE_SIZE > FIXADDR_START);
976 BUG_ON(VMALLOC_END > PKMAP_BASE);
978 BUG_ON(VMALLOC_START >= VMALLOC_END);
979 BUG_ON((unsigned long)high_memory > VMALLOC_START);
981 if (boot_cpu_data.wp_works_ok < 0)
988 #ifdef CONFIG_MEMORY_HOTPLUG
989 int arch_add_memory(int nid, u64 start, u64 size)
991 struct pglist_data *pgdata = NODE_DATA(nid);
992 struct zone *zone = pgdata->node_zones + ZONE_HIGHMEM;
993 unsigned long start_pfn = start >> PAGE_SHIFT;
994 unsigned long nr_pages = size >> PAGE_SHIFT;
996 return __add_pages(nid, zone, start_pfn, nr_pages);
1001 * This function cannot be __init, since exceptions don't work in that
1002 * section. Put this after the callers, so that it cannot be inlined.
1004 static noinline int do_test_wp_bit(void)
1009 __asm__ __volatile__(
1015 :"=m" (*(char *)fix_to_virt(FIX_WP_TEST)),
1024 #ifdef CONFIG_DEBUG_RODATA
1025 const int rodata_test_data = 0xC3;
1026 EXPORT_SYMBOL_GPL(rodata_test_data);
1028 void mark_rodata_ro(void)
1030 unsigned long start = PFN_ALIGN(_text);
1031 unsigned long size = PFN_ALIGN(_etext) - start;
1033 #ifndef CONFIG_DYNAMIC_FTRACE
1034 /* Dynamic tracing modifies the kernel text section */
1035 set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
1036 printk(KERN_INFO "Write protecting the kernel text: %luk\n",
1039 #ifdef CONFIG_CPA_DEBUG
1040 printk(KERN_INFO "Testing CPA: Reverting %lx-%lx\n",
1042 set_pages_rw(virt_to_page(start), size>>PAGE_SHIFT);
1044 printk(KERN_INFO "Testing CPA: write protecting again\n");
1045 set_pages_ro(virt_to_page(start), size>>PAGE_SHIFT);
1047 #endif /* CONFIG_DYNAMIC_FTRACE */
1050 size = (unsigned long)__end_rodata - start;
1051 set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
1052 printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
1056 #ifdef CONFIG_CPA_DEBUG
1057 printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, start + size);
1058 set_pages_rw(virt_to_page(start), size >> PAGE_SHIFT);
1060 printk(KERN_INFO "Testing CPA: write protecting again\n");
1061 set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
1066 int __init reserve_bootmem_generic(unsigned long phys, unsigned long len,
1069 return reserve_bootmem(phys, len, flags);