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/initrd.h>
22 #include <linux/pagemap.h>
23 #include <linux/bootmem.h>
24 #include <linux/proc_fs.h>
25 #include <linux/pci.h>
26 #include <linux/pfn.h>
27 #include <linux/poison.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/module.h>
30 #include <linux/memory_hotplug.h>
31 #include <linux/nmi.h>
33 #include <asm/processor.h>
34 #include <asm/system.h>
35 #include <asm/uaccess.h>
36 #include <asm/pgtable.h>
37 #include <asm/pgalloc.h>
39 #include <asm/fixmap.h>
43 #include <asm/mmu_context.h>
44 #include <asm/proto.h>
46 #include <asm/sections.h>
47 #include <asm/kdebug.h>
49 #include <asm/cacheflush.h>
52 * end_pfn only includes RAM, while max_pfn_mapped includes all e820 entries.
53 * The direct mapping extends to max_pfn_mapped, so that we can directly access
54 * apertures, ACPI and other tables without having to play with fixmaps.
56 unsigned long max_low_pfn_mapped;
57 unsigned long max_pfn_mapped;
59 static unsigned long dma_reserve __initdata;
61 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
63 int direct_gbpages __meminitdata
64 #ifdef CONFIG_DIRECT_GBPAGES
69 static int __init parse_direct_gbpages_off(char *arg)
74 early_param("nogbpages", parse_direct_gbpages_off);
76 static int __init parse_direct_gbpages_on(char *arg)
81 early_param("gbpages", parse_direct_gbpages_on);
84 * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the
85 * physical space so we can cache the place of the first one and move
86 * around without checking the pgd every time.
91 long i, total = 0, reserved = 0;
92 long shared = 0, cached = 0;
96 printk(KERN_INFO "Mem-info:\n");
98 for_each_online_pgdat(pgdat) {
99 for (i = 0; i < pgdat->node_spanned_pages; ++i) {
101 * This loop can take a while with 256 GB and
102 * 4k pages so defer the NMI watchdog:
104 if (unlikely(i % MAX_ORDER_NR_PAGES == 0))
105 touch_nmi_watchdog();
107 if (!pfn_valid(pgdat->node_start_pfn + i))
110 page = pfn_to_page(pgdat->node_start_pfn + i);
112 if (PageReserved(page))
114 else if (PageSwapCache(page))
116 else if (page_count(page))
117 shared += page_count(page) - 1;
120 printk(KERN_INFO "%lu pages of RAM\n", total);
121 printk(KERN_INFO "%lu reserved pages\n", reserved);
122 printk(KERN_INFO "%lu pages shared\n", shared);
123 printk(KERN_INFO "%lu pages swap cached\n", cached);
128 static __init void *spp_getpage(void)
133 ptr = (void *) get_zeroed_page(GFP_ATOMIC);
135 ptr = alloc_bootmem_pages(PAGE_SIZE);
137 if (!ptr || ((unsigned long)ptr & ~PAGE_MASK)) {
138 panic("set_pte_phys: cannot allocate page data %s\n",
139 after_bootmem ? "after bootmem" : "");
142 pr_debug("spp_getpage %p\n", ptr);
148 set_pte_vaddr_pud(pud_t *pud_page, unsigned long vaddr, pte_t new_pte)
154 pud = pud_page + pud_index(vaddr);
155 if (pud_none(*pud)) {
156 pmd = (pmd_t *) spp_getpage();
157 pud_populate(&init_mm, pud, pmd);
158 if (pmd != pmd_offset(pud, 0)) {
159 printk(KERN_ERR "PAGETABLE BUG #01! %p <-> %p\n",
160 pmd, pmd_offset(pud, 0));
164 pmd = pmd_offset(pud, vaddr);
165 if (pmd_none(*pmd)) {
166 pte = (pte_t *) spp_getpage();
167 pmd_populate_kernel(&init_mm, pmd, pte);
168 if (pte != pte_offset_kernel(pmd, 0)) {
169 printk(KERN_ERR "PAGETABLE BUG #02!\n");
174 pte = pte_offset_kernel(pmd, vaddr);
175 if (!pte_none(*pte) && pte_val(new_pte) &&
176 pte_val(*pte) != (pte_val(new_pte) & __supported_pte_mask))
178 set_pte(pte, new_pte);
181 * It's enough to flush this one mapping.
182 * (PGE mappings get flushed as well)
184 __flush_tlb_one(vaddr);
188 set_pte_vaddr(unsigned long vaddr, pte_t pteval)
193 pr_debug("set_pte_vaddr %lx to %lx\n", vaddr, native_pte_val(pteval));
195 pgd = pgd_offset_k(vaddr);
196 if (pgd_none(*pgd)) {
198 "PGD FIXMAP MISSING, it should be setup in head.S!\n");
201 pud_page = (pud_t*)pgd_page_vaddr(*pgd);
202 set_pte_vaddr_pud(pud_page, vaddr, pteval);
206 * Create large page table mappings for a range of physical addresses.
208 static void __init __init_extra_mapping(unsigned long phys, unsigned long size,
215 BUG_ON((phys & ~PMD_MASK) || (size & ~PMD_MASK));
216 for (; size; phys += PMD_SIZE, size -= PMD_SIZE) {
217 pgd = pgd_offset_k((unsigned long)__va(phys));
218 if (pgd_none(*pgd)) {
219 pud = (pud_t *) spp_getpage();
220 set_pgd(pgd, __pgd(__pa(pud) | _KERNPG_TABLE |
223 pud = pud_offset(pgd, (unsigned long)__va(phys));
224 if (pud_none(*pud)) {
225 pmd = (pmd_t *) spp_getpage();
226 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE |
229 pmd = pmd_offset(pud, phys);
230 BUG_ON(!pmd_none(*pmd));
231 set_pmd(pmd, __pmd(phys | pgprot_val(prot)));
235 void __init init_extra_mapping_wb(unsigned long phys, unsigned long size)
237 __init_extra_mapping(phys, size, PAGE_KERNEL_LARGE);
240 void __init init_extra_mapping_uc(unsigned long phys, unsigned long size)
242 __init_extra_mapping(phys, size, PAGE_KERNEL_LARGE_NOCACHE);
246 * The head.S code sets up the kernel high mapping:
248 * from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text)
250 * phys_addr holds the negative offset to the kernel, which is added
251 * to the compile time generated pmds. This results in invalid pmds up
252 * to the point where we hit the physaddr 0 mapping.
254 * We limit the mappings to the region from _text to _end. _end is
255 * rounded up to the 2MB boundary. This catches the invalid pmds as
256 * well, as they are located before _text:
258 void __init cleanup_highmap(void)
260 unsigned long vaddr = __START_KERNEL_map;
261 unsigned long end = round_up((unsigned long)_end, PMD_SIZE) - 1;
262 pmd_t *pmd = level2_kernel_pgt;
263 pmd_t *last_pmd = pmd + PTRS_PER_PMD;
265 for (; pmd < last_pmd; pmd++, vaddr += PMD_SIZE) {
268 if (vaddr < (unsigned long) _text || vaddr > end)
269 set_pmd(pmd, __pmd(0));
273 static unsigned long __initdata table_start;
274 static unsigned long __meminitdata table_end;
275 static unsigned long __meminitdata table_top;
277 static __meminit void *alloc_low_page(unsigned long *phys)
279 unsigned long pfn = table_end++;
283 adr = (void *)get_zeroed_page(GFP_ATOMIC);
289 if (pfn >= table_top)
290 panic("alloc_low_page: ran out of memory");
292 adr = early_ioremap(pfn * PAGE_SIZE, PAGE_SIZE);
293 memset(adr, 0, PAGE_SIZE);
294 *phys = pfn * PAGE_SIZE;
298 static __meminit void unmap_low_page(void *adr)
303 early_iounmap(adr, PAGE_SIZE);
306 static unsigned long __meminit
307 phys_pte_init(pte_t *pte_page, unsigned long addr, unsigned long end)
310 unsigned long last_map_addr = end;
313 pte_t *pte = pte_page + pte_index(addr);
315 for(i = pte_index(addr); i < PTRS_PER_PTE; i++, addr += PAGE_SIZE, pte++) {
318 if (!after_bootmem) {
319 for(; i < PTRS_PER_PTE; i++, pte++)
320 set_pte(pte, __pte(0));
329 printk(" pte=%p addr=%lx pte=%016lx\n",
330 pte, addr, pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL).pte);
331 set_pte(pte, pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL));
332 last_map_addr = (addr & PAGE_MASK) + PAGE_SIZE;
335 update_page_count(PG_LEVEL_4K, pages);
337 return last_map_addr;
340 static unsigned long __meminit
341 phys_pte_update(pmd_t *pmd, unsigned long address, unsigned long end)
343 pte_t *pte = (pte_t *)pmd_page_vaddr(*pmd);
345 return phys_pte_init(pte, address, end);
348 static unsigned long __meminit
349 phys_pmd_init(pmd_t *pmd_page, unsigned long address, unsigned long end,
350 unsigned long page_size_mask)
352 unsigned long pages = 0;
353 unsigned long last_map_addr = end;
355 int i = pmd_index(address);
357 for (; i < PTRS_PER_PMD; i++, address += PMD_SIZE) {
358 unsigned long pte_phys;
359 pmd_t *pmd = pmd_page + pmd_index(address);
362 if (address >= end) {
363 if (!after_bootmem) {
364 for (; i < PTRS_PER_PMD; i++, pmd++)
365 set_pmd(pmd, __pmd(0));
371 if (!pmd_large(*pmd))
372 last_map_addr = phys_pte_update(pmd, address,
377 if (page_size_mask & (1<<PG_LEVEL_2M)) {
379 set_pte((pte_t *)pmd,
380 pfn_pte(address >> PAGE_SHIFT, PAGE_KERNEL_LARGE));
381 last_map_addr = (address & PMD_MASK) + PMD_SIZE;
385 pte = alloc_low_page(&pte_phys);
386 last_map_addr = phys_pte_init(pte, address, end);
389 pmd_populate_kernel(&init_mm, pmd, __va(pte_phys));
391 update_page_count(PG_LEVEL_2M, pages);
392 return last_map_addr;
395 static unsigned long __meminit
396 phys_pmd_update(pud_t *pud, unsigned long address, unsigned long end,
397 unsigned long page_size_mask)
399 pmd_t *pmd = pmd_offset(pud, 0);
400 unsigned long last_map_addr;
402 spin_lock(&init_mm.page_table_lock);
403 last_map_addr = phys_pmd_init(pmd, address, end, page_size_mask);
404 spin_unlock(&init_mm.page_table_lock);
406 return last_map_addr;
409 static unsigned long __meminit
410 phys_pud_init(pud_t *pud_page, unsigned long addr, unsigned long end,
411 unsigned long page_size_mask)
413 unsigned long pages = 0;
414 unsigned long last_map_addr = end;
415 int i = pud_index(addr);
417 for (; i < PTRS_PER_PUD; i++, addr = (addr & PUD_MASK) + PUD_SIZE) {
418 unsigned long pmd_phys;
419 pud_t *pud = pud_page + pud_index(addr);
425 if (!after_bootmem &&
426 !e820_any_mapped(addr, addr+PUD_SIZE, 0)) {
427 set_pud(pud, __pud(0));
432 if (!pud_large(*pud))
433 last_map_addr = phys_pmd_update(pud, addr, end,
438 if (page_size_mask & (1<<PG_LEVEL_1G)) {
440 set_pte((pte_t *)pud,
441 pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL_LARGE));
442 last_map_addr = (addr & PUD_MASK) + PUD_SIZE;
446 pmd = alloc_low_page(&pmd_phys);
448 spin_lock(&init_mm.page_table_lock);
449 last_map_addr = phys_pmd_init(pmd, addr, end, page_size_mask);
451 pud_populate(&init_mm, pud, __va(pmd_phys));
452 spin_unlock(&init_mm.page_table_lock);
456 update_page_count(PG_LEVEL_1G, pages);
458 return last_map_addr;
461 static unsigned long __meminit
462 phys_pud_update(pgd_t *pgd, unsigned long addr, unsigned long end,
463 unsigned long page_size_mask)
467 pud = (pud_t *)pgd_page_vaddr(*pgd);
469 return phys_pud_init(pud, addr, end, page_size_mask);
472 static void __init find_early_table_space(unsigned long end)
474 unsigned long puds, pmds, ptes, tables, start;
476 puds = (end + PUD_SIZE - 1) >> PUD_SHIFT;
477 tables = round_up(puds * sizeof(pud_t), PAGE_SIZE);
478 if (direct_gbpages) {
480 extra = end - ((end>>PUD_SHIFT) << PUD_SHIFT);
481 pmds = (extra + PMD_SIZE - 1) >> PMD_SHIFT;
483 pmds = (end + PMD_SIZE - 1) >> PMD_SHIFT;
484 tables += round_up(pmds * sizeof(pmd_t), PAGE_SIZE);
488 extra = end - ((end>>PMD_SHIFT) << PMD_SHIFT);
489 ptes = (extra + PAGE_SIZE - 1) >> PAGE_SHIFT;
491 ptes = (end + PAGE_SIZE - 1) >> PAGE_SHIFT;
492 tables += round_up(ptes * sizeof(pte_t), PAGE_SIZE);
495 * RED-PEN putting page tables only on node 0 could
496 * cause a hotspot and fill up ZONE_DMA. The page tables
497 * need roughly 0.5KB per GB.
500 table_start = find_e820_area(start, end, tables, PAGE_SIZE);
501 if (table_start == -1UL)
502 panic("Cannot find space for the kernel page tables");
504 table_start >>= PAGE_SHIFT;
505 table_end = table_start;
506 table_top = table_start + (tables >> PAGE_SHIFT);
508 printk(KERN_DEBUG "kernel direct mapping tables up to %lx @ %lx-%lx\n",
509 end, table_start << PAGE_SHIFT, table_top << PAGE_SHIFT);
512 static void __init init_gbpages(void)
514 if (direct_gbpages && cpu_has_gbpages)
515 printk(KERN_INFO "Using GB pages for direct mapping\n");
520 #ifdef CONFIG_MEMTEST
522 static void __init memtest(unsigned long start_phys, unsigned long size,
526 unsigned long *start;
527 unsigned long start_bad;
528 unsigned long last_bad;
530 unsigned long start_phys_aligned;
542 val = 0x5555555555555555UL;
545 val = 0xaaaaaaaaaaaaaaaaUL;
551 incr = sizeof(unsigned long);
552 start_phys_aligned = ALIGN(start_phys, incr);
553 count = (size - (start_phys_aligned - start_phys))/incr;
554 start = __va(start_phys_aligned);
558 for (i = 0; i < count; i++)
560 for (i = 0; i < count; i++, start++, start_phys_aligned += incr) {
562 if (start_phys_aligned == last_bad + incr) {
566 printk(KERN_CONT "\n %016lx bad mem addr %016lx - %016lx reserved",
567 val, start_bad, last_bad + incr);
568 reserve_early(start_bad, last_bad - start_bad, "BAD RAM");
570 start_bad = last_bad = start_phys_aligned;
575 printk(KERN_CONT "\n %016lx bad mem addr %016lx - %016lx reserved",
576 val, start_bad, last_bad + incr);
577 reserve_early(start_bad, last_bad - start_bad, "BAD RAM");
582 /* default is disabled */
583 static int memtest_pattern __initdata;
585 static int __init parse_memtest(char *arg)
588 memtest_pattern = simple_strtoul(arg, NULL, 0);
592 early_param("memtest", parse_memtest);
594 static void __init early_memtest(unsigned long start, unsigned long end)
599 if (!memtest_pattern)
602 printk(KERN_INFO "early_memtest: pattern num %d", memtest_pattern);
603 for (pattern = 0; pattern < memtest_pattern; pattern++) {
606 while (t_start < end) {
607 t_start = find_e820_area_size(t_start, &t_size, 1);
612 if (t_start + t_size > end)
613 t_size = end - t_start;
615 printk(KERN_CONT "\n %016llx - %016llx pattern %d",
616 (unsigned long long)t_start,
617 (unsigned long long)t_start + t_size, pattern);
619 memtest(t_start, t_size, pattern);
624 printk(KERN_CONT "\n");
627 static void __init early_memtest(unsigned long start, unsigned long end)
632 static unsigned long __init kernel_physical_mapping_init(unsigned long start,
634 unsigned long page_size_mask)
637 unsigned long next, last_map_addr = end;
639 start = (unsigned long)__va(start);
640 end = (unsigned long)__va(end);
642 for (; start < end; start = next) {
643 pgd_t *pgd = pgd_offset_k(start);
644 unsigned long pud_phys;
647 next = start + PGDIR_SIZE;
652 last_map_addr = phys_pud_update(pgd, __pa(start),
653 __pa(end), page_size_mask);
658 pud = pud_offset(pgd, start & PGDIR_MASK);
660 pud = alloc_low_page(&pud_phys);
662 last_map_addr = phys_pud_init(pud, __pa(start), __pa(next),
665 pgd_populate(&init_mm, pgd_offset_k(start),
669 return last_map_addr;
675 unsigned page_size_mask;
678 #define NR_RANGE_MR 5
680 static int save_mr(struct map_range *mr, int nr_range,
681 unsigned long start_pfn, unsigned long end_pfn,
682 unsigned long page_size_mask)
685 if (start_pfn < end_pfn) {
686 if (nr_range >= NR_RANGE_MR)
687 panic("run out of range for init_memory_mapping\n");
688 mr[nr_range].start = start_pfn<<PAGE_SHIFT;
689 mr[nr_range].end = end_pfn<<PAGE_SHIFT;
690 mr[nr_range].page_size_mask = page_size_mask;
698 * Setup the direct mapping of the physical memory at PAGE_OFFSET.
699 * This runs before bootmem is initialized and gets pages directly from
700 * the physical memory. To access them they are temporarily mapped.
702 unsigned long __init_refok init_memory_mapping(unsigned long start,
705 unsigned long last_map_addr = 0;
706 unsigned long page_size_mask = 0;
707 unsigned long start_pfn, end_pfn;
709 struct map_range mr[NR_RANGE_MR];
712 printk(KERN_INFO "init_memory_mapping\n");
715 * Find space for the kernel direct mapping tables.
717 * Later we should allocate these tables in the local node of the
718 * memory mapped. Unfortunately this is done currently before the
719 * nodes are discovered.
725 page_size_mask |= 1 << PG_LEVEL_1G;
727 page_size_mask |= 1 << PG_LEVEL_2M;
729 memset(mr, 0, sizeof(mr));
732 /* head if not big page alignment ?*/
733 start_pfn = start >> PAGE_SHIFT;
734 end_pfn = ((start + (PMD_SIZE - 1)) >> PMD_SHIFT)
735 << (PMD_SHIFT - PAGE_SHIFT);
736 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
738 /* big page (2M) range*/
739 start_pfn = ((start + (PMD_SIZE - 1))>>PMD_SHIFT)
740 << (PMD_SHIFT - PAGE_SHIFT);
741 end_pfn = ((start + (PUD_SIZE - 1))>>PUD_SHIFT)
742 << (PUD_SHIFT - PAGE_SHIFT);
743 if (end_pfn > ((end>>PUD_SHIFT)<<(PUD_SHIFT - PAGE_SHIFT)))
744 end_pfn = ((end>>PUD_SHIFT)<<(PUD_SHIFT - PAGE_SHIFT));
745 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
746 page_size_mask & (1<<PG_LEVEL_2M));
748 /* big page (1G) range */
750 end_pfn = (end>>PUD_SHIFT) << (PUD_SHIFT - PAGE_SHIFT);
751 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
753 ((1<<PG_LEVEL_2M)|(1<<PG_LEVEL_1G)));
755 /* tail is not big page (1G) alignment */
757 end_pfn = (end>>PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
758 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
759 page_size_mask & (1<<PG_LEVEL_2M));
761 /* tail is not big page (2M) alignment */
763 end_pfn = end>>PAGE_SHIFT;
764 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
766 /* try to merge same page size and continuous */
767 for (i = 0; nr_range > 1 && i < nr_range - 1; i++) {
768 unsigned long old_start;
769 if (mr[i].end != mr[i+1].start ||
770 mr[i].page_size_mask != mr[i+1].page_size_mask)
773 old_start = mr[i].start;
774 memmove(&mr[i], &mr[i+1],
775 (nr_range - 1 - i) * sizeof (struct map_range));
776 mr[i].start = old_start;
780 for (i = 0; i < nr_range; i++)
781 printk(KERN_DEBUG " %010lx - %010lx page %s\n",
782 mr[i].start, mr[i].end,
783 (mr[i].page_size_mask & (1<<PG_LEVEL_1G))?"1G":(
784 (mr[i].page_size_mask & (1<<PG_LEVEL_2M))?"2M":"4k"));
787 find_early_table_space(end);
789 for (i = 0; i < nr_range; i++)
790 last_map_addr = kernel_physical_mapping_init(
791 mr[i].start, mr[i].end,
792 mr[i].page_size_mask);
795 mmu_cr4_features = read_cr4();
798 if (!after_bootmem && table_end > table_start)
799 reserve_early(table_start << PAGE_SHIFT,
800 table_end << PAGE_SHIFT, "PGTABLE");
802 printk(KERN_INFO "last_map_addr: %lx end: %lx\n",
806 early_memtest(start, end);
808 return last_map_addr >> PAGE_SHIFT;
812 void __init initmem_init(unsigned long start_pfn, unsigned long end_pfn)
814 unsigned long bootmap_size, bootmap;
816 bootmap_size = bootmem_bootmap_pages(end_pfn)<<PAGE_SHIFT;
817 bootmap = find_e820_area(0, end_pfn<<PAGE_SHIFT, bootmap_size,
820 panic("Cannot find bootmem map of size %ld\n", bootmap_size);
821 /* don't touch min_low_pfn */
822 bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap >> PAGE_SHIFT,
824 e820_register_active_regions(0, start_pfn, end_pfn);
825 free_bootmem_with_active_regions(0, end_pfn);
826 early_res_to_bootmem(0, end_pfn<<PAGE_SHIFT);
827 reserve_bootmem(bootmap, bootmap_size, BOOTMEM_DEFAULT);
830 void __init paging_init(void)
832 unsigned long max_zone_pfns[MAX_NR_ZONES];
834 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
835 max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
836 max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
837 max_zone_pfns[ZONE_NORMAL] = max_pfn;
839 memory_present(0, 0, max_pfn);
841 free_area_init_nodes(max_zone_pfns);
846 * Memory hotplug specific functions
848 #ifdef CONFIG_MEMORY_HOTPLUG
850 * Memory is added always to NORMAL zone. This means you will never get
851 * additional DMA/DMA32 memory.
853 int arch_add_memory(int nid, u64 start, u64 size)
855 struct pglist_data *pgdat = NODE_DATA(nid);
856 struct zone *zone = pgdat->node_zones + ZONE_NORMAL;
857 unsigned long last_mapped_pfn, start_pfn = start >> PAGE_SHIFT;
858 unsigned long nr_pages = size >> PAGE_SHIFT;
861 last_mapped_pfn = init_memory_mapping(start, start + size-1);
862 if (last_mapped_pfn > max_pfn_mapped)
863 max_pfn_mapped = last_mapped_pfn;
865 ret = __add_pages(zone, start_pfn, nr_pages);
870 EXPORT_SYMBOL_GPL(arch_add_memory);
872 #if !defined(CONFIG_ACPI_NUMA) && defined(CONFIG_NUMA)
873 int memory_add_physaddr_to_nid(u64 start)
877 EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
880 #endif /* CONFIG_MEMORY_HOTPLUG */
883 * devmem_is_allowed() checks to see if /dev/mem access to a certain address
884 * is valid. The argument is a physical page number.
887 * On x86, access has to be given to the first megabyte of ram because that area
888 * contains bios code and data regions used by X and dosemu and similar apps.
889 * Access has to be given to non-kernel-ram areas as well, these contain the PCI
890 * mmio resources as well as potential bios/acpi data regions.
892 int devmem_is_allowed(unsigned long pagenr)
896 if (!page_is_ram(pagenr))
902 static struct kcore_list kcore_mem, kcore_vmalloc, kcore_kernel,
903 kcore_modules, kcore_vsyscall;
905 void __init mem_init(void)
907 long codesize, reservedpages, datasize, initsize;
911 /* clear_bss() already clear the empty_zero_page */
915 /* this will put all low memory onto the freelists */
917 totalram_pages = numa_free_all_bootmem();
919 totalram_pages = free_all_bootmem();
921 reservedpages = max_pfn - totalram_pages -
922 absent_pages_in_range(0, max_pfn);
925 codesize = (unsigned long) &_etext - (unsigned long) &_text;
926 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
927 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
929 /* Register memory areas for /proc/kcore */
930 kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
931 kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
932 VMALLOC_END-VMALLOC_START);
933 kclist_add(&kcore_kernel, &_stext, _end - _stext);
934 kclist_add(&kcore_modules, (void *)MODULES_VADDR, MODULES_LEN);
935 kclist_add(&kcore_vsyscall, (void *)VSYSCALL_START,
936 VSYSCALL_END - VSYSCALL_START);
938 printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, "
939 "%ldk reserved, %ldk data, %ldk init)\n",
940 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
941 max_pfn << (PAGE_SHIFT-10),
943 reservedpages << (PAGE_SHIFT-10),
950 void free_init_pages(char *what, unsigned long begin, unsigned long end)
952 unsigned long addr = begin;
958 * If debugging page accesses then do not free this memory but
959 * mark them not present - any buggy init-section access will
960 * create a kernel page fault:
962 #ifdef CONFIG_DEBUG_PAGEALLOC
963 printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n",
964 begin, PAGE_ALIGN(end));
965 set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
967 printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
969 for (; addr < end; addr += PAGE_SIZE) {
970 ClearPageReserved(virt_to_page(addr));
971 init_page_count(virt_to_page(addr));
972 memset((void *)(addr & ~(PAGE_SIZE-1)),
973 POISON_FREE_INITMEM, PAGE_SIZE);
980 void free_initmem(void)
982 free_init_pages("unused kernel memory",
983 (unsigned long)(&__init_begin),
984 (unsigned long)(&__init_end));
987 #ifdef CONFIG_DEBUG_RODATA
988 const int rodata_test_data = 0xC3;
989 EXPORT_SYMBOL_GPL(rodata_test_data);
991 void mark_rodata_ro(void)
993 unsigned long start = PFN_ALIGN(_stext), end = PFN_ALIGN(__end_rodata);
994 unsigned long rodata_start =
995 ((unsigned long)__start_rodata + PAGE_SIZE - 1) & PAGE_MASK;
997 #ifdef CONFIG_DYNAMIC_FTRACE
998 /* Dynamic tracing modifies the kernel text section */
999 start = rodata_start;
1002 printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
1003 (end - start) >> 10);
1004 set_memory_ro(start, (end - start) >> PAGE_SHIFT);
1007 * The rodata section (but not the kernel text!) should also be
1010 set_memory_nx(rodata_start, (end - rodata_start) >> PAGE_SHIFT);
1014 #ifdef CONFIG_CPA_DEBUG
1015 printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end);
1016 set_memory_rw(start, (end-start) >> PAGE_SHIFT);
1018 printk(KERN_INFO "Testing CPA: again\n");
1019 set_memory_ro(start, (end-start) >> PAGE_SHIFT);
1025 #ifdef CONFIG_BLK_DEV_INITRD
1026 void free_initrd_mem(unsigned long start, unsigned long end)
1028 free_init_pages("initrd memory", start, end);
1032 int __init reserve_bootmem_generic(unsigned long phys, unsigned long len,
1039 unsigned long pfn = phys >> PAGE_SHIFT;
1041 if (pfn >= max_pfn) {
1043 * This can happen with kdump kernels when accessing
1046 if (pfn < max_pfn_mapped)
1049 printk(KERN_ERR "reserve_bootmem: illegal reserve %lx %lu\n",
1054 /* Should check here against the e820 map to avoid double free */
1056 nid = phys_to_nid(phys);
1057 next_nid = phys_to_nid(phys + len - 1);
1058 if (nid == next_nid)
1059 ret = reserve_bootmem_node(NODE_DATA(nid), phys, len, flags);
1061 ret = reserve_bootmem(phys, len, flags);
1067 reserve_bootmem(phys, len, BOOTMEM_DEFAULT);
1070 if (phys+len <= MAX_DMA_PFN*PAGE_SIZE) {
1071 dma_reserve += len / PAGE_SIZE;
1072 set_dma_reserve(dma_reserve);
1078 int kern_addr_valid(unsigned long addr)
1080 unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
1086 if (above != 0 && above != -1UL)
1089 pgd = pgd_offset_k(addr);
1093 pud = pud_offset(pgd, addr);
1097 pmd = pmd_offset(pud, addr);
1101 if (pmd_large(*pmd))
1102 return pfn_valid(pmd_pfn(*pmd));
1104 pte = pte_offset_kernel(pmd, addr);
1108 return pfn_valid(pte_pfn(*pte));
1112 * A pseudo VMA to allow ptrace access for the vsyscall page. This only
1113 * covers the 64bit vsyscall page now. 32bit has a real VMA now and does
1114 * not need special handling anymore:
1116 static struct vm_area_struct gate_vma = {
1117 .vm_start = VSYSCALL_START,
1118 .vm_end = VSYSCALL_START + (VSYSCALL_MAPPED_PAGES * PAGE_SIZE),
1119 .vm_page_prot = PAGE_READONLY_EXEC,
1120 .vm_flags = VM_READ | VM_EXEC
1123 struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
1125 #ifdef CONFIG_IA32_EMULATION
1126 if (test_tsk_thread_flag(tsk, TIF_IA32))
1132 int in_gate_area(struct task_struct *task, unsigned long addr)
1134 struct vm_area_struct *vma = get_gate_vma(task);
1139 return (addr >= vma->vm_start) && (addr < vma->vm_end);
1143 * Use this when you have no reliable task/vma, typically from interrupt
1144 * context. It is less reliable than using the task's vma and may give
1147 int in_gate_area_no_task(unsigned long addr)
1149 return (addr >= VSYSCALL_START) && (addr < VSYSCALL_END);
1152 const char *arch_vma_name(struct vm_area_struct *vma)
1154 if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
1156 if (vma == &gate_vma)
1157 return "[vsyscall]";
1161 #ifdef CONFIG_SPARSEMEM_VMEMMAP
1163 * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
1165 static long __meminitdata addr_start, addr_end;
1166 static void __meminitdata *p_start, *p_end;
1167 static int __meminitdata node_start;
1170 vmemmap_populate(struct page *start_page, unsigned long size, int node)
1172 unsigned long addr = (unsigned long)start_page;
1173 unsigned long end = (unsigned long)(start_page + size);
1179 for (; addr < end; addr = next) {
1182 pgd = vmemmap_pgd_populate(addr, node);
1186 pud = vmemmap_pud_populate(pgd, addr, node);
1191 next = (addr + PAGE_SIZE) & PAGE_MASK;
1192 pmd = vmemmap_pmd_populate(pud, addr, node);
1197 p = vmemmap_pte_populate(pmd, addr, node);
1202 addr_end = addr + PAGE_SIZE;
1203 p_end = p + PAGE_SIZE;
1205 next = pmd_addr_end(addr, end);
1207 pmd = pmd_offset(pud, addr);
1208 if (pmd_none(*pmd)) {
1211 p = vmemmap_alloc_block(PMD_SIZE, node);
1215 entry = pfn_pte(__pa(p) >> PAGE_SHIFT,
1217 set_pmd(pmd, __pmd(pte_val(entry)));
1219 /* check to see if we have contiguous blocks */
1220 if (p_end != p || node_start != node) {
1222 printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n",
1223 addr_start, addr_end-1, p_start, p_end-1, node_start);
1229 addr_end = addr + PMD_SIZE;
1230 p_end = p + PMD_SIZE;
1232 vmemmap_verify((pte_t *)pmd, node, addr, next);
1239 void __meminit vmemmap_populate_print_last(void)
1242 printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n",
1243 addr_start, addr_end-1, p_start, p_end-1, node_start);