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/pfn.h>
25 #include <linux/poison.h>
26 #include <linux/bootmem.h>
27 #include <linux/slab.h>
28 #include <linux/proc_fs.h>
29 #include <linux/memory_hotplug.h>
30 #include <linux/initrd.h>
31 #include <linux/cpumask.h>
34 #include <asm/processor.h>
35 #include <asm/system.h>
36 #include <asm/uaccess.h>
37 #include <asm/pgtable.h>
39 #include <asm/fixmap.h>
44 #include <asm/tlbflush.h>
45 #include <asm/pgalloc.h>
46 #include <asm/sections.h>
47 #include <asm/paravirt.h>
48 #include <asm/setup.h>
49 #include <asm/cacheflush.h>
51 unsigned int __VMALLOC_RESERVE = 128 << 20;
53 unsigned long max_pfn_mapped;
55 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
56 unsigned long highstart_pfn, highend_pfn;
58 static noinline int do_test_wp_bit(void);
61 * Creates a middle page table and puts a pointer to it in the
62 * given global directory entry. This only returns the gd entry
63 * in non-PAE compilation mode, since the middle layer is folded.
65 static pmd_t * __init one_md_table_init(pgd_t *pgd)
71 if (!(pgd_val(*pgd) & _PAGE_PRESENT)) {
72 pmd_table = (pmd_t *) alloc_bootmem_low_pages(PAGE_SIZE);
74 paravirt_alloc_pmd(&init_mm, __pa(pmd_table) >> PAGE_SHIFT);
75 set_pgd(pgd, __pgd(__pa(pmd_table) | _PAGE_PRESENT));
76 pud = pud_offset(pgd, 0);
77 BUG_ON(pmd_table != pmd_offset(pud, 0));
80 pud = pud_offset(pgd, 0);
81 pmd_table = pmd_offset(pud, 0);
87 * Create a page table and place a pointer to it in a middle page
90 static pte_t * __init one_page_table_init(pmd_t *pmd)
92 if (!(pmd_val(*pmd) & _PAGE_PRESENT)) {
93 pte_t *page_table = NULL;
95 #ifdef CONFIG_DEBUG_PAGEALLOC
96 page_table = (pte_t *) alloc_bootmem_pages(PAGE_SIZE);
100 (pte_t *)alloc_bootmem_low_pages(PAGE_SIZE);
103 paravirt_alloc_pte(&init_mm, __pa(page_table) >> PAGE_SHIFT);
104 set_pmd(pmd, __pmd(__pa(page_table) | _PAGE_TABLE));
105 BUG_ON(page_table != pte_offset_kernel(pmd, 0));
108 return pte_offset_kernel(pmd, 0);
112 * This function initializes a certain range of kernel virtual memory
113 * with new bootmem page tables, everywhere page tables are missing in
116 * NOTE: The pagetables are allocated contiguous on the physical space
117 * so we can cache the place of the first one and move around without
118 * checking the pgd every time.
121 page_table_range_init(unsigned long start, unsigned long end, pgd_t *pgd_base)
123 int pgd_idx, pmd_idx;
129 pgd_idx = pgd_index(vaddr);
130 pmd_idx = pmd_index(vaddr);
131 pgd = pgd_base + pgd_idx;
133 for ( ; (pgd_idx < PTRS_PER_PGD) && (vaddr != end); pgd++, pgd_idx++) {
134 pmd = one_md_table_init(pgd);
135 pmd = pmd + pmd_index(vaddr);
136 for (; (pmd_idx < PTRS_PER_PMD) && (vaddr != end);
138 one_page_table_init(pmd);
146 static inline int is_kernel_text(unsigned long addr)
148 if (addr >= PAGE_OFFSET && addr <= (unsigned long)__init_end)
154 * This maps the physical memory to kernel virtual address space, a total
155 * of max_low_pfn pages, by creating page tables starting from address
158 static void __init kernel_physical_mapping_init(pgd_t *pgd_base)
160 int pgd_idx, pmd_idx, pte_ofs;
166 pgd_idx = pgd_index(PAGE_OFFSET);
167 pgd = pgd_base + pgd_idx;
170 for (; pgd_idx < PTRS_PER_PGD; pgd++, pgd_idx++) {
171 pmd = one_md_table_init(pgd);
172 if (pfn >= max_low_pfn)
176 pmd_idx < PTRS_PER_PMD && pfn < max_low_pfn;
178 unsigned int addr = pfn * PAGE_SIZE + PAGE_OFFSET;
181 * Map with big pages if possible, otherwise
182 * create normal page tables:
184 * Don't use a large page for the first 2/4MB of memory
185 * because there are often fixed size MTRRs in there
186 * and overlapping MTRRs into large pages can cause
189 if (cpu_has_pse && !(pgd_idx == 0 && pmd_idx == 0)) {
191 pgprot_t prot = PAGE_KERNEL_LARGE;
193 addr2 = (pfn + PTRS_PER_PTE-1) * PAGE_SIZE +
194 PAGE_OFFSET + PAGE_SIZE-1;
196 if (is_kernel_text(addr) ||
197 is_kernel_text(addr2))
198 prot = PAGE_KERNEL_LARGE_EXEC;
200 set_pmd(pmd, pfn_pmd(pfn, prot));
203 max_pfn_mapped = pfn;
206 pte = one_page_table_init(pmd);
209 pte_ofs < PTRS_PER_PTE && pfn < max_low_pfn;
210 pte++, pfn++, pte_ofs++, addr += PAGE_SIZE) {
211 pgprot_t prot = PAGE_KERNEL;
213 if (is_kernel_text(addr))
214 prot = PAGE_KERNEL_EXEC;
216 set_pte(pte, pfn_pte(pfn, prot));
218 max_pfn_mapped = pfn;
224 * devmem_is_allowed() checks to see if /dev/mem access to a certain address
225 * is valid. The argument is a physical page number.
228 * On x86, access has to be given to the first megabyte of ram because that area
229 * contains bios code and data regions used by X and dosemu and similar apps.
230 * Access has to be given to non-kernel-ram areas as well, these contain the PCI
231 * mmio resources as well as potential bios/acpi data regions.
233 int devmem_is_allowed(unsigned long pagenr)
237 if (!page_is_ram(pagenr))
242 #ifdef CONFIG_HIGHMEM
246 static inline pte_t *kmap_get_fixmap_pte(unsigned long vaddr)
248 return pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr),
249 vaddr), vaddr), vaddr);
252 static void __init kmap_init(void)
254 unsigned long kmap_vstart;
257 * Cache the first kmap pte:
259 kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
260 kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
262 kmap_prot = PAGE_KERNEL;
265 static void __init permanent_kmaps_init(pgd_t *pgd_base)
274 page_table_range_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base);
276 pgd = swapper_pg_dir + pgd_index(vaddr);
277 pud = pud_offset(pgd, vaddr);
278 pmd = pmd_offset(pud, vaddr);
279 pte = pte_offset_kernel(pmd, vaddr);
280 pkmap_page_table = pte;
283 static void __init add_one_highpage_init(struct page *page, int pfn)
285 ClearPageReserved(page);
286 init_page_count(page);
291 struct add_highpages_data {
292 unsigned long start_pfn;
293 unsigned long end_pfn;
296 static void __init add_highpages_work_fn(unsigned long start_pfn,
297 unsigned long end_pfn, void *datax)
301 unsigned long final_start_pfn, final_end_pfn;
302 struct add_highpages_data *data;
304 data = (struct add_highpages_data *)datax;
306 final_start_pfn = max(start_pfn, data->start_pfn);
307 final_end_pfn = min(end_pfn, data->end_pfn);
308 if (final_start_pfn >= final_end_pfn)
311 for (node_pfn = final_start_pfn; node_pfn < final_end_pfn;
313 if (!pfn_valid(node_pfn))
315 page = pfn_to_page(node_pfn);
316 add_one_highpage_init(page, node_pfn);
321 void __init add_highpages_with_active_regions(int nid, unsigned long start_pfn,
322 unsigned long end_pfn)
324 struct add_highpages_data data;
326 data.start_pfn = start_pfn;
327 data.end_pfn = end_pfn;
329 work_with_active_regions(nid, add_highpages_work_fn, &data);
333 static void __init set_highmem_pages_init(void)
335 add_highpages_with_active_regions(0, highstart_pfn, highend_pfn);
337 totalram_pages += totalhigh_pages;
339 #endif /* !CONFIG_NUMA */
342 # define kmap_init() do { } while (0)
343 # define permanent_kmaps_init(pgd_base) do { } while (0)
344 # define set_highmem_pages_init() do { } while (0)
345 #endif /* CONFIG_HIGHMEM */
347 pteval_t __PAGE_KERNEL = _PAGE_KERNEL;
348 EXPORT_SYMBOL(__PAGE_KERNEL);
350 pteval_t __PAGE_KERNEL_EXEC = _PAGE_KERNEL_EXEC;
352 void __init native_pagetable_setup_start(pgd_t *base)
354 unsigned long pfn, va;
361 * Remove any mappings which extend past the end of physical
362 * memory from the boot time page table:
364 for (pfn = max_low_pfn + 1; pfn < 1<<(32-PAGE_SHIFT); pfn++) {
365 va = PAGE_OFFSET + (pfn<<PAGE_SHIFT);
366 pgd = base + pgd_index(va);
367 if (!pgd_present(*pgd))
370 pud = pud_offset(pgd, va);
371 pmd = pmd_offset(pud, va);
372 if (!pmd_present(*pmd))
375 pte = pte_offset_kernel(pmd, va);
376 if (!pte_present(*pte))
379 pte_clear(NULL, va, pte);
381 paravirt_alloc_pmd(&init_mm, __pa(base) >> PAGE_SHIFT);
384 void __init native_pagetable_setup_done(pgd_t *base)
389 * Build a proper pagetable for the kernel mappings. Up until this
390 * point, we've been running on some set of pagetables constructed by
393 * If we're booting on native hardware, this will be a pagetable
394 * constructed in arch/x86/kernel/head_32.S. The root of the
395 * pagetable will be swapper_pg_dir.
397 * If we're booting paravirtualized under a hypervisor, then there are
398 * more options: we may already be running PAE, and the pagetable may
399 * or may not be based in swapper_pg_dir. In any case,
400 * paravirt_pagetable_setup_start() will set up swapper_pg_dir
401 * appropriately for the rest of the initialization to work.
403 * In general, pagetable_init() assumes that the pagetable may already
404 * be partially populated, and so it avoids stomping on any existing
407 static void __init pagetable_init(void)
409 pgd_t *pgd_base = swapper_pg_dir;
410 unsigned long vaddr, end;
412 paravirt_pagetable_setup_start(pgd_base);
414 /* Enable PSE if available */
416 set_in_cr4(X86_CR4_PSE);
418 /* Enable PGE if available */
420 set_in_cr4(X86_CR4_PGE);
421 __PAGE_KERNEL |= _PAGE_GLOBAL;
422 __PAGE_KERNEL_EXEC |= _PAGE_GLOBAL;
425 kernel_physical_mapping_init(pgd_base);
429 * Fixed mappings, only the page table structure has to be
430 * created - mappings will be set by set_fixmap():
432 early_ioremap_clear();
433 vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
434 end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK;
435 page_table_range_init(vaddr, end, pgd_base);
436 early_ioremap_reset();
438 permanent_kmaps_init(pgd_base);
440 paravirt_pagetable_setup_done(pgd_base);
443 #ifdef CONFIG_ACPI_SLEEP
445 * ACPI suspend needs this for resume, because things like the intel-agp
446 * driver might have split up a kernel 4MB mapping.
448 char swsusp_pg_dir[PAGE_SIZE]
449 __attribute__ ((aligned(PAGE_SIZE)));
451 static inline void save_pg_dir(void)
453 memcpy(swsusp_pg_dir, swapper_pg_dir, PAGE_SIZE);
455 #else /* !CONFIG_ACPI_SLEEP */
456 static inline void save_pg_dir(void)
459 #endif /* !CONFIG_ACPI_SLEEP */
461 void zap_low_mappings(void)
466 * Zap initial low-memory mappings.
468 * Note that "pgd_clear()" doesn't do it for
469 * us, because pgd_clear() is a no-op on i386.
471 for (i = 0; i < KERNEL_PGD_BOUNDARY; i++) {
472 #ifdef CONFIG_X86_PAE
473 set_pgd(swapper_pg_dir+i, __pgd(1 + __pa(empty_zero_page)));
475 set_pgd(swapper_pg_dir+i, __pgd(0));
483 pteval_t __supported_pte_mask __read_mostly = ~_PAGE_NX;
484 EXPORT_SYMBOL_GPL(__supported_pte_mask);
486 #ifdef CONFIG_X86_PAE
488 static int disable_nx __initdata;
493 * Control non executable mappings.
498 static int __init noexec_setup(char *str)
500 if (!str || !strcmp(str, "on")) {
502 __supported_pte_mask |= _PAGE_NX;
506 if (!strcmp(str, "off")) {
508 __supported_pte_mask &= ~_PAGE_NX;
516 early_param("noexec", noexec_setup);
518 static void __init set_nx(void)
520 unsigned int v[4], l, h;
522 if (cpu_has_pae && (cpuid_eax(0x80000000) > 0x80000001)) {
523 cpuid(0x80000001, &v[0], &v[1], &v[2], &v[3]);
525 if ((v[3] & (1 << 20)) && !disable_nx) {
526 rdmsr(MSR_EFER, l, h);
528 wrmsr(MSR_EFER, l, h);
530 __supported_pte_mask |= _PAGE_NX;
537 * paging_init() sets up the page tables - note that the first 8MB are
538 * already mapped by head.S.
540 * This routines also unmaps the page at virtual kernel address 0, so
541 * that we can trap those pesky NULL-reference errors in the kernel.
543 void __init paging_init(void)
545 #ifdef CONFIG_X86_PAE
548 printk(KERN_INFO "NX (Execute Disable) protection: active\n");
552 load_cr3(swapper_pg_dir);
560 * Test if the WP bit works in supervisor mode. It isn't supported on 386's
561 * and also on some strange 486's. All 586+'s are OK. This used to involve
562 * black magic jumps to work around some nasty CPU bugs, but fortunately the
563 * switch to using exceptions got rid of all that.
565 static void __init test_wp_bit(void)
568 "Checking if this processor honours the WP bit even in supervisor mode...");
570 /* Any page-aligned address will do, the test is non-destructive */
571 __set_fixmap(FIX_WP_TEST, __pa(&swapper_pg_dir), PAGE_READONLY);
572 boot_cpu_data.wp_works_ok = do_test_wp_bit();
573 clear_fixmap(FIX_WP_TEST);
575 if (!boot_cpu_data.wp_works_ok) {
576 printk(KERN_CONT "No.\n");
577 #ifdef CONFIG_X86_WP_WORKS_OK
579 "This kernel doesn't support CPU's with broken WP. Recompile it for a 386!");
582 printk(KERN_CONT "Ok.\n");
586 static struct kcore_list kcore_mem, kcore_vmalloc;
588 void __init mem_init(void)
590 int codesize, reservedpages, datasize, initsize;
593 #ifdef CONFIG_FLATMEM
596 #ifdef CONFIG_HIGHMEM
597 /* check that fixmap and pkmap do not overlap */
598 if (PKMAP_BASE + LAST_PKMAP*PAGE_SIZE >= FIXADDR_START) {
600 "fixmap and kmap areas overlap - this will crash\n");
601 printk(KERN_ERR "pkstart: %lxh pkend: %lxh fixstart %lxh\n",
602 PKMAP_BASE, PKMAP_BASE + LAST_PKMAP*PAGE_SIZE,
607 /* this will put all low memory onto the freelists */
608 totalram_pages += free_all_bootmem();
611 for (tmp = 0; tmp < max_low_pfn; tmp++)
613 * Only count reserved RAM pages:
615 if (page_is_ram(tmp) && PageReserved(pfn_to_page(tmp)))
618 set_highmem_pages_init();
620 codesize = (unsigned long) &_etext - (unsigned long) &_text;
621 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
622 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
624 kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
625 kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
626 VMALLOC_END-VMALLOC_START);
628 printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, "
629 "%dk reserved, %dk data, %dk init, %ldk highmem)\n",
630 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
631 num_physpages << (PAGE_SHIFT-10),
633 reservedpages << (PAGE_SHIFT-10),
636 (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10))
639 #if 1 /* double-sanity-check paranoia */
640 printk(KERN_INFO "virtual kernel memory layout:\n"
641 " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
642 #ifdef CONFIG_HIGHMEM
643 " pkmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
645 " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n"
646 " lowmem : 0x%08lx - 0x%08lx (%4ld MB)\n"
647 " .init : 0x%08lx - 0x%08lx (%4ld kB)\n"
648 " .data : 0x%08lx - 0x%08lx (%4ld kB)\n"
649 " .text : 0x%08lx - 0x%08lx (%4ld kB)\n",
650 FIXADDR_START, FIXADDR_TOP,
651 (FIXADDR_TOP - FIXADDR_START) >> 10,
653 #ifdef CONFIG_HIGHMEM
654 PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE,
655 (LAST_PKMAP*PAGE_SIZE) >> 10,
658 VMALLOC_START, VMALLOC_END,
659 (VMALLOC_END - VMALLOC_START) >> 20,
661 (unsigned long)__va(0), (unsigned long)high_memory,
662 ((unsigned long)high_memory - (unsigned long)__va(0)) >> 20,
664 (unsigned long)&__init_begin, (unsigned long)&__init_end,
665 ((unsigned long)&__init_end -
666 (unsigned long)&__init_begin) >> 10,
668 (unsigned long)&_etext, (unsigned long)&_edata,
669 ((unsigned long)&_edata - (unsigned long)&_etext) >> 10,
671 (unsigned long)&_text, (unsigned long)&_etext,
672 ((unsigned long)&_etext - (unsigned long)&_text) >> 10);
674 #ifdef CONFIG_HIGHMEM
675 BUG_ON(PKMAP_BASE + LAST_PKMAP*PAGE_SIZE > FIXADDR_START);
676 BUG_ON(VMALLOC_END > PKMAP_BASE);
678 BUG_ON(VMALLOC_START > VMALLOC_END);
679 BUG_ON((unsigned long)high_memory > VMALLOC_START);
680 #endif /* double-sanity-check paranoia */
682 if (boot_cpu_data.wp_works_ok < 0)
690 #ifdef CONFIG_MEMORY_HOTPLUG
691 int arch_add_memory(int nid, u64 start, u64 size)
693 struct pglist_data *pgdata = NODE_DATA(nid);
694 struct zone *zone = pgdata->node_zones + ZONE_HIGHMEM;
695 unsigned long start_pfn = start >> PAGE_SHIFT;
696 unsigned long nr_pages = size >> PAGE_SHIFT;
698 return __add_pages(zone, start_pfn, nr_pages);
703 * This function cannot be __init, since exceptions don't work in that
704 * section. Put this after the callers, so that it cannot be inlined.
706 static noinline int do_test_wp_bit(void)
711 __asm__ __volatile__(
717 :"=m" (*(char *)fix_to_virt(FIX_WP_TEST)),
726 #ifdef CONFIG_DEBUG_RODATA
727 const int rodata_test_data = 0xC3;
728 EXPORT_SYMBOL_GPL(rodata_test_data);
730 void mark_rodata_ro(void)
732 unsigned long start = PFN_ALIGN(_text);
733 unsigned long size = PFN_ALIGN(_etext) - start;
735 set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
736 printk(KERN_INFO "Write protecting the kernel text: %luk\n",
739 #ifdef CONFIG_CPA_DEBUG
740 printk(KERN_INFO "Testing CPA: Reverting %lx-%lx\n",
742 set_pages_rw(virt_to_page(start), size>>PAGE_SHIFT);
744 printk(KERN_INFO "Testing CPA: write protecting again\n");
745 set_pages_ro(virt_to_page(start), size>>PAGE_SHIFT);
748 size = (unsigned long)__end_rodata - start;
749 set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
750 printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
754 #ifdef CONFIG_CPA_DEBUG
755 printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, start + size);
756 set_pages_rw(virt_to_page(start), size >> PAGE_SHIFT);
758 printk(KERN_INFO "Testing CPA: write protecting again\n");
759 set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
764 void free_init_pages(char *what, unsigned long begin, unsigned long end)
766 #ifdef CONFIG_DEBUG_PAGEALLOC
768 * If debugging page accesses then do not free this memory but
769 * mark them not present - any buggy init-section access will
770 * create a kernel page fault:
772 printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n",
773 begin, PAGE_ALIGN(end));
774 set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
779 * We just marked the kernel text read only above, now that
780 * we are going to free part of that, we need to make that
783 set_memory_rw(begin, (end - begin) >> PAGE_SHIFT);
785 for (addr = begin; addr < end; addr += PAGE_SIZE) {
786 ClearPageReserved(virt_to_page(addr));
787 init_page_count(virt_to_page(addr));
788 memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
792 printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
796 void free_initmem(void)
798 free_init_pages("unused kernel memory",
799 (unsigned long)(&__init_begin),
800 (unsigned long)(&__init_end));
803 #ifdef CONFIG_BLK_DEV_INITRD
804 void free_initrd_mem(unsigned long start, unsigned long end)
806 free_init_pages("initrd memory", start, end);
810 int __init reserve_bootmem_generic(unsigned long phys, unsigned long len,
813 return reserve_bootmem(phys, len, flags);