2 * linux/arch/x86_64/mm/init.c
4 * Copyright (C) 1995 Linus Torvalds
5 * Copyright (C) 2000 Pavel Machek <pavel@suse.cz>
6 * Copyright (C) 2002,2003 Andi Kleen <ak@suse.de>
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/swap.h>
19 #include <linux/smp.h>
20 #include <linux/init.h>
21 #include <linux/pagemap.h>
22 #include <linux/bootmem.h>
23 #include <linux/proc_fs.h>
24 #include <linux/pci.h>
25 #include <linux/pfn.h>
26 #include <linux/poison.h>
27 #include <linux/dma-mapping.h>
28 #include <linux/module.h>
29 #include <linux/memory_hotplug.h>
30 #include <linux/nmi.h>
32 #include <asm/processor.h>
33 #include <asm/system.h>
34 #include <asm/uaccess.h>
35 #include <asm/pgtable.h>
36 #include <asm/pgalloc.h>
38 #include <asm/fixmap.h>
42 #include <asm/mmu_context.h>
43 #include <asm/proto.h>
45 #include <asm/sections.h>
46 #include <asm/kdebug.h>
48 #include <asm/cacheflush.h>
50 const struct dma_mapping_ops *dma_ops;
51 EXPORT_SYMBOL(dma_ops);
53 static unsigned long dma_reserve __initdata;
55 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
57 int direct_gbpages __meminitdata
58 #ifdef CONFIG_DIRECT_GBPAGES
63 static int __init parse_direct_gbpages_off(char *arg)
68 early_param("nogbpages", parse_direct_gbpages_off);
70 static int __init parse_direct_gbpages_on(char *arg)
75 early_param("gbpages", parse_direct_gbpages_on);
78 * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the
79 * physical space so we can cache the place of the first one and move
80 * around without checking the pgd every time.
85 long i, total = 0, reserved = 0;
86 long shared = 0, cached = 0;
90 printk(KERN_INFO "Mem-info:\n");
92 for_each_online_pgdat(pgdat) {
93 for (i = 0; i < pgdat->node_spanned_pages; ++i) {
95 * This loop can take a while with 256 GB and
96 * 4k pages so defer the NMI watchdog:
98 if (unlikely(i % MAX_ORDER_NR_PAGES == 0))
101 if (!pfn_valid(pgdat->node_start_pfn + i))
104 page = pfn_to_page(pgdat->node_start_pfn + i);
106 if (PageReserved(page))
108 else if (PageSwapCache(page))
110 else if (page_count(page))
111 shared += page_count(page) - 1;
114 printk(KERN_INFO "%lu pages of RAM\n", total);
115 printk(KERN_INFO "%lu reserved pages\n", reserved);
116 printk(KERN_INFO "%lu pages shared\n", shared);
117 printk(KERN_INFO "%lu pages swap cached\n", cached);
122 static __init void *spp_getpage(void)
127 ptr = (void *) get_zeroed_page(GFP_ATOMIC);
129 ptr = alloc_bootmem_pages(PAGE_SIZE);
131 if (!ptr || ((unsigned long)ptr & ~PAGE_MASK)) {
132 panic("set_pte_phys: cannot allocate page data %s\n",
133 after_bootmem ? "after bootmem" : "");
136 pr_debug("spp_getpage %p\n", ptr);
142 set_pte_phys(unsigned long vaddr, unsigned long phys, pgprot_t prot)
149 pr_debug("set_pte_phys %lx to %lx\n", vaddr, phys);
151 pgd = pgd_offset_k(vaddr);
152 if (pgd_none(*pgd)) {
154 "PGD FIXMAP MISSING, it should be setup in head.S!\n");
157 pud = pud_offset(pgd, vaddr);
158 if (pud_none(*pud)) {
159 pmd = (pmd_t *) spp_getpage();
160 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE | _PAGE_USER));
161 if (pmd != pmd_offset(pud, 0)) {
162 printk(KERN_ERR "PAGETABLE BUG #01! %p <-> %p\n",
163 pmd, pmd_offset(pud, 0));
167 pmd = pmd_offset(pud, vaddr);
168 if (pmd_none(*pmd)) {
169 pte = (pte_t *) spp_getpage();
170 set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE | _PAGE_USER));
171 if (pte != pte_offset_kernel(pmd, 0)) {
172 printk(KERN_ERR "PAGETABLE BUG #02!\n");
176 new_pte = pfn_pte(phys >> PAGE_SHIFT, prot);
178 pte = pte_offset_kernel(pmd, vaddr);
179 if (!pte_none(*pte) &&
180 pte_val(*pte) != (pte_val(new_pte) & __supported_pte_mask))
182 set_pte(pte, new_pte);
185 * It's enough to flush this one mapping.
186 * (PGE mappings get flushed as well)
188 __flush_tlb_one(vaddr);
192 * The head.S code sets up the kernel high mapping:
194 * from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text)
196 * phys_addr holds the negative offset to the kernel, which is added
197 * to the compile time generated pmds. This results in invalid pmds up
198 * to the point where we hit the physaddr 0 mapping.
200 * We limit the mappings to the region from _text to _end. _end is
201 * rounded up to the 2MB boundary. This catches the invalid pmds as
202 * well, as they are located before _text:
204 void __init cleanup_highmap(void)
206 unsigned long vaddr = __START_KERNEL_map;
207 unsigned long end = round_up((unsigned long)_end, PMD_SIZE) - 1;
208 pmd_t *pmd = level2_kernel_pgt;
209 pmd_t *last_pmd = pmd + PTRS_PER_PMD;
211 for (; pmd < last_pmd; pmd++, vaddr += PMD_SIZE) {
212 if (!pmd_present(*pmd))
214 if (vaddr < (unsigned long) _text || vaddr > end)
215 set_pmd(pmd, __pmd(0));
219 /* NOTE: this is meant to be run only at boot */
221 __set_fixmap(enum fixed_addresses idx, unsigned long phys, pgprot_t prot)
223 unsigned long address = __fix_to_virt(idx);
225 if (idx >= __end_of_fixed_addresses) {
226 printk(KERN_ERR "Invalid __set_fixmap\n");
229 set_pte_phys(address, phys, prot);
232 static unsigned long __initdata table_start;
233 static unsigned long __meminitdata table_end;
235 static __meminit void *alloc_low_page(unsigned long *phys)
237 unsigned long pfn = table_end++;
241 adr = (void *)get_zeroed_page(GFP_ATOMIC);
248 panic("alloc_low_page: ran out of memory");
250 adr = early_ioremap(pfn * PAGE_SIZE, PAGE_SIZE);
251 memset(adr, 0, PAGE_SIZE);
252 *phys = pfn * PAGE_SIZE;
256 static __meminit void unmap_low_page(void *adr)
261 early_iounmap(adr, PAGE_SIZE);
264 /* Must run before zap_low_mappings */
265 __meminit void *early_ioremap(unsigned long addr, unsigned long size)
267 pmd_t *pmd, *last_pmd;
271 pmds = ((addr & ~PMD_MASK) + size + ~PMD_MASK) / PMD_SIZE;
272 vaddr = __START_KERNEL_map;
273 pmd = level2_kernel_pgt;
274 last_pmd = level2_kernel_pgt + PTRS_PER_PMD - 1;
276 for (; pmd <= last_pmd; pmd++, vaddr += PMD_SIZE) {
277 for (i = 0; i < pmds; i++) {
278 if (pmd_present(pmd[i]))
279 goto continue_outer_loop;
281 vaddr += addr & ~PMD_MASK;
284 for (i = 0; i < pmds; i++, addr += PMD_SIZE)
285 set_pmd(pmd+i, __pmd(addr | __PAGE_KERNEL_LARGE_EXEC));
288 return (void *)vaddr;
292 printk(KERN_ERR "early_ioremap(0x%lx, %lu) failed\n", addr, size);
298 * To avoid virtual aliases later:
300 __meminit void early_iounmap(void *addr, unsigned long size)
306 vaddr = (unsigned long)addr;
307 pmds = ((vaddr & ~PMD_MASK) + size + ~PMD_MASK) / PMD_SIZE;
308 pmd = level2_kernel_pgt + pmd_index(vaddr);
310 for (i = 0; i < pmds; i++)
316 static void __meminit
317 phys_pmd_init(pmd_t *pmd_page, unsigned long address, unsigned long end)
319 int i = pmd_index(address);
321 for (; i < PTRS_PER_PMD; i++, address += PMD_SIZE) {
322 pmd_t *pmd = pmd_page + pmd_index(address);
324 if (address >= end) {
325 if (!after_bootmem) {
326 for (; i < PTRS_PER_PMD; i++, pmd++)
327 set_pmd(pmd, __pmd(0));
335 set_pte((pte_t *)pmd,
336 pfn_pte(address >> PAGE_SHIFT, PAGE_KERNEL_LARGE));
340 static void __meminit
341 phys_pmd_update(pud_t *pud, unsigned long address, unsigned long end)
343 pmd_t *pmd = pmd_offset(pud, 0);
344 spin_lock(&init_mm.page_table_lock);
345 phys_pmd_init(pmd, address, end);
346 spin_unlock(&init_mm.page_table_lock);
350 static void __meminit
351 phys_pud_init(pud_t *pud_page, unsigned long addr, unsigned long end)
353 int i = pud_index(addr);
355 for (; i < PTRS_PER_PUD; i++, addr = (addr & PUD_MASK) + PUD_SIZE) {
356 unsigned long pmd_phys;
357 pud_t *pud = pud_page + pud_index(addr);
363 if (!after_bootmem &&
364 !e820_any_mapped(addr, addr+PUD_SIZE, 0)) {
365 set_pud(pud, __pud(0));
370 if (!pud_large(*pud))
371 phys_pmd_update(pud, addr, end);
375 if (direct_gbpages) {
376 set_pte((pte_t *)pud,
377 pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL_LARGE));
381 pmd = alloc_low_page(&pmd_phys);
383 spin_lock(&init_mm.page_table_lock);
384 set_pud(pud, __pud(pmd_phys | _KERNPG_TABLE));
385 phys_pmd_init(pmd, addr, end);
386 spin_unlock(&init_mm.page_table_lock);
393 static void __init find_early_table_space(unsigned long end)
395 unsigned long puds, pmds, tables, start;
397 puds = (end + PUD_SIZE - 1) >> PUD_SHIFT;
398 tables = round_up(puds * sizeof(pud_t), PAGE_SIZE);
399 if (!direct_gbpages) {
400 pmds = (end + PMD_SIZE - 1) >> PMD_SHIFT;
401 tables += round_up(pmds * sizeof(pmd_t), PAGE_SIZE);
405 * RED-PEN putting page tables only on node 0 could
406 * cause a hotspot and fill up ZONE_DMA. The page tables
407 * need roughly 0.5KB per GB.
410 table_start = find_e820_area(start, end, tables, PAGE_SIZE);
411 if (table_start == -1UL)
412 panic("Cannot find space for the kernel page tables");
414 table_start >>= PAGE_SHIFT;
415 table_end = table_start;
417 early_printk("kernel direct mapping tables up to %lx @ %lx-%lx\n",
418 end, table_start << PAGE_SHIFT,
419 (table_start << PAGE_SHIFT) + tables);
422 static void __init init_gbpages(void)
424 if (direct_gbpages && cpu_has_gbpages)
425 printk(KERN_INFO "Using GB pages for direct mapping\n");
430 static void __init memtest(unsigned long start_phys, unsigned long size, unsigned pattern)
433 unsigned long *start;
434 unsigned long start_bad;
435 unsigned long last_bad;
437 unsigned long start_phys_aligned;
449 val = 0x5555555555555555UL;
452 val = 0xaaaaaaaaaaaaaaaaUL;
458 incr = sizeof(unsigned long);
459 start_phys_aligned = ALIGN(start_phys, incr);
460 count = (size - (start_phys_aligned - start_phys))/incr;
461 start = __va(start_phys_aligned);
465 for (i = 0; i < count; i++)
467 for (i = 0; i < count; i++, start++, start_phys_aligned += incr) {
469 if (start_phys_aligned == last_bad + incr) {
473 printk(KERN_INFO " %016lxx bad mem addr %016lx - %016lx reserved\n",
474 val, start_bad, last_bad + incr);
475 reserve_early(start_bad, last_bad - start_bad, "BAD RAM");
477 start_bad = last_bad = start_phys_aligned;
482 printk(KERN_INFO " %016lx bad mem addr %016lx - %016lx reserved\n",
483 val, start_bad, last_bad + incr);
484 reserve_early(start_bad, last_bad - start_bad, "BAD RAM");
489 static int __initdata memtest_pattern;
490 static int __init parse_memtest(char *arg)
493 memtest_pattern = simple_strtoul(arg, NULL, 0) + 1;
497 early_param("memtest", parse_memtest);
499 static void __init early_memtest(unsigned long start, unsigned long end)
501 unsigned long t_start, t_size;
505 printk(KERN_INFO "early_memtest: pattern num %d", memtest_pattern);
506 for (pattern = 0; pattern < memtest_pattern; pattern++) {
509 while (t_start < end) {
510 t_start = find_e820_area_size(t_start, &t_size, 1);
515 if (t_start + t_size > end)
516 t_size = end - t_start;
518 printk(KERN_CONT "\n %016lx - %016lx pattern %d",
519 t_start, t_start + t_size, pattern);
521 memtest(t_start, t_size, pattern);
527 printk(KERN_CONT "\n");
531 * Setup the direct mapping of the physical memory at PAGE_OFFSET.
532 * This runs before bootmem is initialized and gets pages directly from
533 * the physical memory. To access them they are temporarily mapped.
535 void __init_refok init_memory_mapping(unsigned long start, unsigned long end)
538 unsigned long start_phys = start, end_phys = end;
540 printk(KERN_INFO "init_memory_mapping\n");
543 * Find space for the kernel direct mapping tables.
545 * Later we should allocate these tables in the local node of the
546 * memory mapped. Unfortunately this is done currently before the
547 * nodes are discovered.
549 if (!after_bootmem) {
551 find_early_table_space(end);
554 start = (unsigned long)__va(start);
555 end = (unsigned long)__va(end);
557 for (; start < end; start = next) {
558 pgd_t *pgd = pgd_offset_k(start);
559 unsigned long pud_phys;
563 pud = pud_offset(pgd, start & PGDIR_MASK);
565 pud = alloc_low_page(&pud_phys);
567 next = start + PGDIR_SIZE;
570 phys_pud_init(pud, __pa(start), __pa(next));
572 set_pgd(pgd_offset_k(start), mk_kernel_pgd(pud_phys));
577 mmu_cr4_features = read_cr4();
581 reserve_early(table_start << PAGE_SHIFT,
582 table_end << PAGE_SHIFT, "PGTABLE");
585 early_memtest(start_phys, end_phys);
589 void __init paging_init(void)
591 unsigned long max_zone_pfns[MAX_NR_ZONES];
593 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
594 max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
595 max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
596 max_zone_pfns[ZONE_NORMAL] = end_pfn;
598 memory_present(0, 0, end_pfn);
600 free_area_init_nodes(max_zone_pfns);
605 * Memory hotplug specific functions
607 void online_page(struct page *page)
609 ClearPageReserved(page);
610 init_page_count(page);
616 #ifdef CONFIG_MEMORY_HOTPLUG
618 * Memory is added always to NORMAL zone. This means you will never get
619 * additional DMA/DMA32 memory.
621 int arch_add_memory(int nid, u64 start, u64 size)
623 struct pglist_data *pgdat = NODE_DATA(nid);
624 struct zone *zone = pgdat->node_zones + ZONE_NORMAL;
625 unsigned long start_pfn = start >> PAGE_SHIFT;
626 unsigned long nr_pages = size >> PAGE_SHIFT;
629 init_memory_mapping(start, start + size-1);
631 ret = __add_pages(zone, start_pfn, nr_pages);
636 EXPORT_SYMBOL_GPL(arch_add_memory);
638 #if !defined(CONFIG_ACPI_NUMA) && defined(CONFIG_NUMA)
639 int memory_add_physaddr_to_nid(u64 start)
643 EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
646 #endif /* CONFIG_MEMORY_HOTPLUG */
648 static struct kcore_list kcore_mem, kcore_vmalloc, kcore_kernel,
649 kcore_modules, kcore_vsyscall;
651 void __init mem_init(void)
653 long codesize, reservedpages, datasize, initsize;
657 /* clear_bss() already clear the empty_zero_page */
661 /* this will put all low memory onto the freelists */
663 totalram_pages = numa_free_all_bootmem();
665 totalram_pages = free_all_bootmem();
667 reservedpages = end_pfn - totalram_pages -
668 absent_pages_in_range(0, end_pfn);
671 codesize = (unsigned long) &_etext - (unsigned long) &_text;
672 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
673 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
675 /* Register memory areas for /proc/kcore */
676 kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
677 kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
678 VMALLOC_END-VMALLOC_START);
679 kclist_add(&kcore_kernel, &_stext, _end - _stext);
680 kclist_add(&kcore_modules, (void *)MODULES_VADDR, MODULES_LEN);
681 kclist_add(&kcore_vsyscall, (void *)VSYSCALL_START,
682 VSYSCALL_END - VSYSCALL_START);
684 printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, "
685 "%ldk reserved, %ldk data, %ldk init)\n",
686 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
687 end_pfn << (PAGE_SHIFT-10),
689 reservedpages << (PAGE_SHIFT-10),
696 void free_init_pages(char *what, unsigned long begin, unsigned long end)
698 unsigned long addr = begin;
704 * If debugging page accesses then do not free this memory but
705 * mark them not present - any buggy init-section access will
706 * create a kernel page fault:
708 #ifdef CONFIG_DEBUG_PAGEALLOC
709 printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n",
710 begin, PAGE_ALIGN(end));
711 set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
713 printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
715 for (; addr < end; addr += PAGE_SIZE) {
716 ClearPageReserved(virt_to_page(addr));
717 init_page_count(virt_to_page(addr));
718 memset((void *)(addr & ~(PAGE_SIZE-1)),
719 POISON_FREE_INITMEM, PAGE_SIZE);
726 void free_initmem(void)
728 free_init_pages("unused kernel memory",
729 (unsigned long)(&__init_begin),
730 (unsigned long)(&__init_end));
733 #ifdef CONFIG_DEBUG_RODATA
734 const int rodata_test_data = 0xC3;
735 EXPORT_SYMBOL_GPL(rodata_test_data);
737 void mark_rodata_ro(void)
739 unsigned long start = PFN_ALIGN(_stext), end = PFN_ALIGN(__end_rodata);
741 printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
742 (end - start) >> 10);
743 set_memory_ro(start, (end - start) >> PAGE_SHIFT);
746 * The rodata section (but not the kernel text!) should also be
749 start = ((unsigned long)__start_rodata + PAGE_SIZE - 1) & PAGE_MASK;
750 set_memory_nx(start, (end - start) >> PAGE_SHIFT);
754 #ifdef CONFIG_CPA_DEBUG
755 printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end);
756 set_memory_rw(start, (end-start) >> PAGE_SHIFT);
758 printk(KERN_INFO "Testing CPA: again\n");
759 set_memory_ro(start, (end-start) >> PAGE_SHIFT);
765 #ifdef CONFIG_BLK_DEV_INITRD
766 void free_initrd_mem(unsigned long start, unsigned long end)
768 free_init_pages("initrd memory", start, end);
772 void __init reserve_bootmem_generic(unsigned long phys, unsigned len)
775 int nid = phys_to_nid(phys);
777 unsigned long pfn = phys >> PAGE_SHIFT;
779 if (pfn >= end_pfn) {
781 * This can happen with kdump kernels when accessing
784 if (pfn < end_pfn_map)
787 printk(KERN_ERR "reserve_bootmem: illegal reserve %lx %u\n",
792 /* Should check here against the e820 map to avoid double free */
794 reserve_bootmem_node(NODE_DATA(nid), phys, len, BOOTMEM_DEFAULT);
796 reserve_bootmem(phys, len, BOOTMEM_DEFAULT);
798 if (phys+len <= MAX_DMA_PFN*PAGE_SIZE) {
799 dma_reserve += len / PAGE_SIZE;
800 set_dma_reserve(dma_reserve);
804 int kern_addr_valid(unsigned long addr)
806 unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
812 if (above != 0 && above != -1UL)
815 pgd = pgd_offset_k(addr);
819 pud = pud_offset(pgd, addr);
823 pmd = pmd_offset(pud, addr);
828 return pfn_valid(pmd_pfn(*pmd));
830 pte = pte_offset_kernel(pmd, addr);
834 return pfn_valid(pte_pfn(*pte));
838 * A pseudo VMA to allow ptrace access for the vsyscall page. This only
839 * covers the 64bit vsyscall page now. 32bit has a real VMA now and does
840 * not need special handling anymore:
842 static struct vm_area_struct gate_vma = {
843 .vm_start = VSYSCALL_START,
844 .vm_end = VSYSCALL_START + (VSYSCALL_MAPPED_PAGES * PAGE_SIZE),
845 .vm_page_prot = PAGE_READONLY_EXEC,
846 .vm_flags = VM_READ | VM_EXEC
849 struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
851 #ifdef CONFIG_IA32_EMULATION
852 if (test_tsk_thread_flag(tsk, TIF_IA32))
858 int in_gate_area(struct task_struct *task, unsigned long addr)
860 struct vm_area_struct *vma = get_gate_vma(task);
865 return (addr >= vma->vm_start) && (addr < vma->vm_end);
869 * Use this when you have no reliable task/vma, typically from interrupt
870 * context. It is less reliable than using the task's vma and may give
873 int in_gate_area_no_task(unsigned long addr)
875 return (addr >= VSYSCALL_START) && (addr < VSYSCALL_END);
878 const char *arch_vma_name(struct vm_area_struct *vma)
880 if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
882 if (vma == &gate_vma)
887 #ifdef CONFIG_SPARSEMEM_VMEMMAP
889 * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
892 vmemmap_populate(struct page *start_page, unsigned long size, int node)
894 unsigned long addr = (unsigned long)start_page;
895 unsigned long end = (unsigned long)(start_page + size);
901 for (; addr < end; addr = next) {
902 next = pmd_addr_end(addr, end);
904 pgd = vmemmap_pgd_populate(addr, node);
908 pud = vmemmap_pud_populate(pgd, addr, node);
912 pmd = pmd_offset(pud, addr);
913 if (pmd_none(*pmd)) {
917 p = vmemmap_alloc_block(PMD_SIZE, node);
921 entry = pfn_pte(__pa(p) >> PAGE_SHIFT,
923 set_pmd(pmd, __pmd(pte_val(entry)));
925 printk(KERN_DEBUG " [%lx-%lx] PMD ->%p on node %d\n",
926 addr, addr + PMD_SIZE - 1, p, node);
928 vmemmap_verify((pte_t *)pmd, node, addr, next);