2 * linux/arch/i386/mm/init.c
4 * Copyright (C) 1995 Linus Torvalds
6 * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
9 #include <linux/config.h>
10 #include <linux/module.h>
11 #include <linux/signal.h>
12 #include <linux/sched.h>
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/string.h>
16 #include <linux/types.h>
17 #include <linux/ptrace.h>
18 #include <linux/mman.h>
20 #include <linux/hugetlb.h>
21 #include <linux/swap.h>
22 #include <linux/smp.h>
23 #include <linux/init.h>
24 #include <linux/highmem.h>
25 #include <linux/pagemap.h>
26 #include <linux/bootmem.h>
27 #include <linux/slab.h>
28 #include <linux/proc_fs.h>
29 #include <linux/efi.h>
31 #include <asm/processor.h>
32 #include <asm/system.h>
33 #include <asm/uaccess.h>
34 #include <asm/pgtable.h>
36 #include <asm/fixmap.h>
40 #include <asm/tlbflush.h>
41 #include <asm/sections.h>
44 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
45 unsigned long highstart_pfn, highend_pfn;
47 static int do_test_wp_bit(void);
49 static inline int page_kills_ppro(unsigned long pagenr)
51 if (pagenr >= 0x70000 && pagenr <= 0x7003F)
56 extern int is_available_memory(efi_memory_desc_t *);
58 static inline int page_is_ram(unsigned long pagenr)
61 unsigned long addr, end;
64 efi_memory_desc_t *md;
66 for (i = 0; i < memmap.nr_map; i++) {
68 if (!is_available_memory(md))
70 addr = (md->phys_addr+PAGE_SIZE-1) >> PAGE_SHIFT;
71 end = (md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT)) >> PAGE_SHIFT;
73 if ((pagenr >= addr) && (pagenr < end))
79 for (i = 0; i < e820.nr_map; i++) {
81 if (e820.map[i].type != E820_RAM) /* not usable memory */
84 * !!!FIXME!!! Some BIOSen report areas as RAM that
85 * are not. Notably the 640->1Mb area. We need a sanity
88 addr = (e820.map[i].addr+PAGE_SIZE-1) >> PAGE_SHIFT;
89 end = (e820.map[i].addr+e820.map[i].size) >> PAGE_SHIFT;
90 if ((pagenr >= addr) && (pagenr < end))
96 /* To enable modules to check if a page is in RAM */
97 int pfn_is_ram(unsigned long pfn)
99 return (page_is_ram(pfn));
104 * devmem_is_allowed() checks to see if /dev/mem access to a certain address is
105 * valid. The argument is a physical page number.
108 * On x86, access has to be given to the first megabyte of ram because that area
109 * contains bios code and data regions used by X and dosemu and similar apps.
110 * Access has to be given to non-kernel-ram areas as well, these contain the PCI
111 * mmio resources as well as potential bios/acpi data regions.
113 int devmem_is_allowed(unsigned long pagenr)
117 if (!page_is_ram(pagenr))
125 EXPORT_SYMBOL(kmap_pte);
127 #define kmap_get_fixmap_pte(vaddr) \
128 pte_offset_kernel(pmd_offset(pgd_offset_k(vaddr), (vaddr)), (vaddr))
130 void __init kmap_init(void)
132 kmap_pte = kmap_get_fixmap_pte(__fix_to_virt(FIX_KMAP_BEGIN));
135 void __init one_highpage_init(struct page *page, int pfn, int bad_ppro)
137 if (page_is_ram(pfn) && !(bad_ppro && page_kills_ppro(pfn))) {
138 ClearPageReserved(page);
139 set_bit(PG_highmem, &page->flags);
140 set_page_count(page, 1);
144 SetPageReserved(page);
147 EXPORT_SYMBOL_GPL(page_is_ram);
149 #ifdef CONFIG_HIGHMEM
151 #ifndef CONFIG_DISCONTIGMEM
152 void __init set_highmem_pages_init(int bad_ppro)
155 for (pfn = highstart_pfn; pfn < highend_pfn; pfn++)
156 one_highpage_init(pfn_to_page(pfn), pfn, bad_ppro);
157 totalram_pages += totalhigh_pages;
160 extern void set_highmem_pages_init(int);
161 #endif /* !CONFIG_DISCONTIGMEM */
163 # define set_highmem_pages_init(bad_ppro) do { } while (0)
166 unsigned long long __PAGE_KERNEL = _PAGE_KERNEL;
167 unsigned long long __PAGE_KERNEL_EXEC = _PAGE_KERNEL_EXEC;
169 #ifndef CONFIG_DISCONTIGMEM
170 #define remap_numa_kva() do {} while (0)
172 extern void __init remap_numa_kva(void);
175 static __init void prepare_pagetables(pgd_t *pgd_base, unsigned long address)
181 pgd = pgd_base + pgd_index(address);
182 pmd = pmd_offset(pgd, address);
183 if (!pmd_present(*pmd)) {
184 pte = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE);
185 set_pmd(pmd, __pmd(_PAGE_TABLE + __pa(pte)));
189 static void __init fixrange_init (unsigned long start, unsigned long end, pgd_t *pgd_base)
193 for (vaddr = start; vaddr != end; vaddr += PAGE_SIZE)
194 prepare_pagetables(pgd_base, vaddr);
197 void setup_identity_mappings(pgd_t *pgd_base, unsigned long start, unsigned long end)
203 pte_t *pte, *pte_base;
207 for (i = 0; i < PTRS_PER_PGD; pgd++, i++) {
208 vaddr = i*PGDIR_SIZE;
209 if (end && (vaddr >= end))
211 pmd = pmd_offset(pgd, 0);
212 for (j = 0; j < PTRS_PER_PMD; pmd++, j++) {
213 vaddr = i*PGDIR_SIZE + j*PMD_SIZE;
214 if (end && (vaddr >= end))
221 set_in_cr4(X86_CR4_PSE);
222 boot_cpu_data.wp_works_ok = 1;
223 __pe = _KERNPG_TABLE + _PAGE_PSE + vaddr - start;
224 /* Make it "global" too if supported */
226 set_in_cr4(X86_CR4_PGE);
227 #if !defined(CONFIG_X86_SWITCH_PAGETABLES)
228 __pe += _PAGE_GLOBAL;
229 __PAGE_KERNEL |= _PAGE_GLOBAL;
232 set_pmd(pmd, __pmd(__pe));
235 if (!pmd_present(*pmd))
236 pte_base = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE);
238 pte_base = pte_offset_kernel(pmd, 0);
240 for (k = 0; k < PTRS_PER_PTE; pte++, k++) {
241 vaddr = i*PGDIR_SIZE + j*PMD_SIZE + k*PAGE_SIZE;
242 if (end && (vaddr >= end))
246 *pte = mk_pte_phys(vaddr-start, PAGE_KERNEL);
248 set_pmd(pmd, __pmd(_KERNPG_TABLE + __pa(pte_base)));
253 static void __init pagetable_init (void)
255 unsigned long vaddr, end;
257 #ifdef CONFIG_X86_PAE
262 * This can be zero as well - no problem, in that case we exit
263 * the loops anyway due to the PTRS_PER_* conditions.
265 end = (unsigned long)__va(max_low_pfn*PAGE_SIZE);
267 pgd_base = swapper_pg_dir;
268 #ifdef CONFIG_X86_PAE
270 * It causes too many problems if there's no proper pmd set up
271 * for all 4 entries of the PGD - so we allocate all of them.
272 * PAE systems will not miss this extra 4-8K anyway ...
274 for (i = 0; i < PTRS_PER_PGD; i++) {
275 pmd_t *pmd = (pmd_t *) alloc_bootmem_low_pages(PAGE_SIZE);
276 set_pgd(pgd_base + i, __pgd(__pa(pmd) + 0x1));
280 * Set up lowmem-sized identity mappings at PAGE_OFFSET:
282 setup_identity_mappings(pgd_base, PAGE_OFFSET, end);
285 * Add flat-mode identity-mappings - SMP needs it when
286 * starting up on an AP from real-mode. (In the non-PAE
287 * case we already have these mappings through head.S.)
288 * All user-space mappings are explicitly cleared after
291 #if defined(CONFIG_SMP) && defined(CONFIG_X86_PAE)
292 setup_identity_mappings(pgd_base, 0, 16*1024*1024);
297 * Fixed mappings, only the page table structure has to be
298 * created - mappings will be set by set_fixmap():
300 vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
301 fixrange_init(vaddr, 0, pgd_base);
303 #ifdef CONFIG_HIGHMEM
313 fixrange_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base);
315 pgd = swapper_pg_dir + pgd_index(vaddr);
316 pmd = pmd_offset(pgd, vaddr);
317 pte = pte_offset_kernel(pmd, vaddr);
318 pkmap_page_table = pte;
324 * Clear kernel pagetables in a PMD_SIZE-aligned range.
326 static void clear_mappings(pgd_t *pgd_base, unsigned long start, unsigned long end)
335 for (i = 0; i < PTRS_PER_PGD; pgd++, i++) {
336 vaddr = i*PGDIR_SIZE;
337 if (end && (vaddr >= end))
339 pmd = pmd_offset(pgd, 0);
340 for (j = 0; j < PTRS_PER_PMD; pmd++, j++) {
341 vaddr = i*PGDIR_SIZE + j*PMD_SIZE;
342 if (end && (vaddr >= end))
352 void zap_low_mappings(void)
354 printk("zapping low mappings.\n");
356 * Zap initial low-memory mappings.
358 clear_mappings(swapper_pg_dir, 0, 16*1024*1024);
361 #ifndef CONFIG_DISCONTIGMEM
362 void __init zone_sizes_init(void)
364 unsigned long zones_size[MAX_NR_ZONES] = {0, 0, 0};
365 unsigned int max_dma, high, low;
367 max_dma = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
372 zones_size[ZONE_DMA] = low;
374 zones_size[ZONE_DMA] = max_dma;
375 zones_size[ZONE_NORMAL] = low - max_dma;
376 #ifdef CONFIG_HIGHMEM
377 zones_size[ZONE_HIGHMEM] = high - low;
380 free_area_init(zones_size);
383 extern void zone_sizes_init(void);
384 #endif /* !CONFIG_DISCONTIGMEM */
386 static int disable_nx __initdata = 0;
387 u64 __supported_pte_mask = ~_PAGE_NX;
392 * Control non executable mappings.
395 * off Disable (disables exec-shield too)
397 static int __init noexec_setup(char *str)
399 if (!strncmp(str, "on",2) && cpu_has_nx) {
400 __supported_pte_mask |= _PAGE_NX;
402 } else if (!strncmp(str,"off",3)) {
404 __supported_pte_mask &= ~_PAGE_NX;
410 __setup("noexec=", noexec_setup);
412 #ifdef CONFIG_X86_PAE
415 static void __init set_nx(void)
417 unsigned int v[4], l, h;
419 if (cpu_has_pae && (cpuid_eax(0x80000000) > 0x80000001)) {
420 cpuid(0x80000001, &v[0], &v[1], &v[2], &v[3]);
421 if ((v[3] & (1 << 20)) && !disable_nx) {
422 rdmsr(MSR_EFER, l, h);
424 wrmsr(MSR_EFER, l, h);
426 __supported_pte_mask |= _PAGE_NX;
431 * Enables/disables executability of a given kernel page and
432 * returns the previous setting.
434 int __init set_kernel_exec(unsigned long vaddr, int enable)
442 pte = lookup_address(vaddr);
445 if (!pte_exec_kernel(*pte))
449 pte->pte_high &= ~(1 << (_PAGE_BIT_NX - 32));
451 pte->pte_high |= 1 << (_PAGE_BIT_NX - 32);
460 * paging_init() sets up the page tables - note that the first 8MB are
461 * already mapped by head.S.
463 * This routines also unmaps the page at virtual kernel address 0, so
464 * that we can trap those pesky NULL-reference errors in the kernel.
466 void __init paging_init(void)
468 #ifdef CONFIG_X86_PAE
471 printk("NX (Execute Disable) protection: active\n");
476 load_cr3(swapper_pg_dir);
478 #ifdef CONFIG_X86_PAE
480 * We will bail out later - printk doesn't work right now so
481 * the user would just see a hanging kernel.
484 set_in_cr4(X86_CR4_PAE);
488 * Subtle. SMP is doing it's boot stuff late (because it has to
489 * fork idle threads) - but it also needs low mappings for the
490 * protected-mode entry to work. We zap these entries only after
491 * the WP-bit has been tested.
501 * Test if the WP bit works in supervisor mode. It isn't supported on 386's
502 * and also on some strange 486's (NexGen etc.). All 586+'s are OK. This
503 * used to involve black magic jumps to work around some nasty CPU bugs,
504 * but fortunately the switch to using exceptions got rid of all that.
507 void __init test_wp_bit(void)
509 printk("Checking if this processor honours the WP bit even in supervisor mode... ");
511 /* Any page-aligned address will do, the test is non-destructive */
512 __set_fixmap(FIX_WP_TEST, __pa(&swapper_pg_dir), PAGE_READONLY);
513 boot_cpu_data.wp_works_ok = do_test_wp_bit();
514 clear_fixmap(FIX_WP_TEST);
516 if (!boot_cpu_data.wp_works_ok) {
518 #ifdef CONFIG_X86_WP_WORKS_OK
519 panic("This kernel doesn't support CPU's with broken WP. Recompile it for a 386!");
526 #ifndef CONFIG_DISCONTIGMEM
527 static void __init set_max_mapnr_init(void)
529 #ifdef CONFIG_HIGHMEM
530 highmem_start_page = pfn_to_page(highstart_pfn);
531 max_mapnr = num_physpages = highend_pfn;
533 max_mapnr = num_physpages = max_low_pfn;
536 #define __free_all_bootmem() free_all_bootmem()
538 #define __free_all_bootmem() free_all_bootmem_node(NODE_DATA(0))
539 extern void set_max_mapnr_init(void);
540 #endif /* !CONFIG_DISCONTIGMEM */
542 static struct kcore_list kcore_mem, kcore_vmalloc;
544 void __init mem_init(void)
546 extern int ppro_with_ram_bug(void);
547 int codesize, reservedpages, datasize, initsize;
551 #ifndef CONFIG_DISCONTIGMEM
556 bad_ppro = ppro_with_ram_bug();
558 #ifdef CONFIG_HIGHMEM
559 /* check that fixmap and pkmap do not overlap */
560 if (PKMAP_BASE+LAST_PKMAP*PAGE_SIZE >= FIXADDR_START) {
561 printk(KERN_ERR "fixmap and kmap areas overlap - this will crash\n");
562 printk(KERN_ERR "pkstart: %lxh pkend: %lxh fixstart %lxh\n",
563 PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE, FIXADDR_START);
568 set_max_mapnr_init();
570 #ifdef CONFIG_HIGHMEM
571 high_memory = (void *) __va(highstart_pfn * PAGE_SIZE);
573 high_memory = (void *) __va(max_low_pfn * PAGE_SIZE);
576 /* this will put all low memory onto the freelists */
577 totalram_pages += __free_all_bootmem();
580 for (tmp = 0; tmp < max_low_pfn; tmp++)
582 * Only count reserved RAM pages
584 if (page_is_ram(tmp) && PageReserved(pfn_to_page(tmp)))
587 set_highmem_pages_init(bad_ppro);
589 codesize = (unsigned long) &_etext - (unsigned long) &_text;
590 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
591 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
593 kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
594 kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
595 VMALLOC_END-VMALLOC_START);
597 printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, %dk reserved, %dk data, %dk init, %ldk highmem)\n",
598 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
599 num_physpages << (PAGE_SHIFT-10),
601 reservedpages << (PAGE_SHIFT-10),
604 (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10))
607 #ifdef CONFIG_X86_PAE
609 panic("cannot execute a PAE-enabled kernel on a PAE-less CPU!");
611 if (boot_cpu_data.wp_works_ok < 0)
614 entry_trampoline_setup();
615 default_ldt_page = virt_to_page(default_ldt);
616 load_LDT(&init_mm.context);
619 kmem_cache_t *pgd_cache, *pmd_cache, *kpmd_cache;
621 void __init pgtable_cache_init(void)
623 void (*ctor)(void *, kmem_cache_t *, unsigned long);
624 void (*dtor)(void *, kmem_cache_t *, unsigned long);
626 if (PTRS_PER_PMD > 1) {
627 pmd_cache = kmem_cache_create("pmd",
628 PTRS_PER_PMD*sizeof(pmd_t),
629 PTRS_PER_PMD*sizeof(pmd_t),
634 panic("pgtable_cache_init(): cannot create pmd cache");
636 if (TASK_SIZE > PAGE_OFFSET) {
637 kpmd_cache = kmem_cache_create("kpmd",
638 PTRS_PER_PMD*sizeof(pmd_t),
639 PTRS_PER_PMD*sizeof(pmd_t),
644 panic("pgtable_cache_init(): "
645 "cannot create kpmd cache");
649 if (PTRS_PER_PMD == 1 || TASK_SIZE <= PAGE_OFFSET)
654 if (PTRS_PER_PMD == 1 && TASK_SIZE <= PAGE_OFFSET)
659 pgd_cache = kmem_cache_create("pgd",
660 PTRS_PER_PGD*sizeof(pgd_t),
661 PTRS_PER_PGD*sizeof(pgd_t),
666 panic("pgtable_cache_init(): Cannot create pgd cache");
670 * This function cannot be __init, since exceptions don't work in that
671 * section. Put this after the callers, so that it cannot be inlined.
673 static int noinline do_test_wp_bit(void)
678 __asm__ __volatile__(
683 ".section __ex_table,\"a\"\n"
687 :"=m" (*(char *)fix_to_virt(FIX_WP_TEST)),
696 void free_initmem(void)
700 addr = (unsigned long)(&__init_begin);
701 for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
702 ClearPageReserved(virt_to_page(addr));
703 set_page_count(virt_to_page(addr), 1);
707 printk (KERN_INFO "Freeing unused kernel memory: %dk freed\n", (__init_end - __init_begin) >> 10);
710 #ifdef CONFIG_BLK_DEV_INITRD
711 void free_initrd_mem(unsigned long start, unsigned long end)
714 printk (KERN_INFO "Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
715 for (; start < end; start += PAGE_SIZE) {
716 ClearPageReserved(virt_to_page(start));
717 set_page_count(virt_to_page(start), 1);