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 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))
98 EXPORT_SYMBOL(kmap_pte);
100 #define kmap_get_fixmap_pte(vaddr) \
101 pte_offset_kernel(pmd_offset(pgd_offset_k(vaddr), (vaddr)), (vaddr))
103 void __init kmap_init(void)
105 kmap_pte = kmap_get_fixmap_pte(__fix_to_virt(FIX_KMAP_BEGIN));
108 void __init one_highpage_init(struct page *page, int pfn, int bad_ppro)
110 if (page_is_ram(pfn) && !(bad_ppro && page_kills_ppro(pfn))) {
111 ClearPageReserved(page);
112 set_bit(PG_highmem, &page->flags);
113 set_page_count(page, 1);
117 SetPageReserved(page);
120 EXPORT_SYMBOL_GPL(page_is_ram);
122 #ifdef CONFIG_HIGHMEM
124 #ifndef CONFIG_DISCONTIGMEM
125 void __init set_highmem_pages_init(int bad_ppro)
128 for (pfn = highstart_pfn; pfn < highend_pfn; pfn++)
129 one_highpage_init(pfn_to_page(pfn), pfn, bad_ppro);
130 totalram_pages += totalhigh_pages;
133 extern void set_highmem_pages_init(int);
134 #endif /* !CONFIG_DISCONTIGMEM */
136 # define set_highmem_pages_init(bad_ppro) do { } while (0)
139 unsigned long long __PAGE_KERNEL = _PAGE_KERNEL;
140 unsigned long long __PAGE_KERNEL_EXEC = _PAGE_KERNEL_EXEC;
142 #ifndef CONFIG_DISCONTIGMEM
143 #define remap_numa_kva() do {} while (0)
145 extern void __init remap_numa_kva(void);
148 static __init void prepare_pagetables(pgd_t *pgd_base, unsigned long address)
154 pgd = pgd_base + pgd_index(address);
155 pmd = pmd_offset(pgd, address);
156 if (!pmd_present(*pmd)) {
157 pte = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE);
158 set_pmd(pmd, __pmd(_PAGE_TABLE + __pa(pte)));
162 static void __init fixrange_init (unsigned long start, unsigned long end, pgd_t *pgd_base)
166 for (vaddr = start; vaddr != end; vaddr += PAGE_SIZE)
167 prepare_pagetables(pgd_base, vaddr);
170 void setup_identity_mappings(pgd_t *pgd_base, unsigned long start, unsigned long end)
176 pte_t *pte, *pte_base;
180 for (i = 0; i < PTRS_PER_PGD; pgd++, i++) {
181 vaddr = i*PGDIR_SIZE;
182 if (end && (vaddr >= end))
184 pmd = pmd_offset(pgd, 0);
185 for (j = 0; j < PTRS_PER_PMD; pmd++, j++) {
186 vaddr = i*PGDIR_SIZE + j*PMD_SIZE;
187 if (end && (vaddr >= end))
194 set_in_cr4(X86_CR4_PSE);
195 boot_cpu_data.wp_works_ok = 1;
196 __pe = _KERNPG_TABLE + _PAGE_PSE + vaddr - start;
197 /* Make it "global" too if supported */
199 set_in_cr4(X86_CR4_PGE);
200 #if !defined(CONFIG_X86_SWITCH_PAGETABLES)
201 __pe += _PAGE_GLOBAL;
202 __PAGE_KERNEL |= _PAGE_GLOBAL;
205 set_pmd(pmd, __pmd(__pe));
208 if (!pmd_present(*pmd))
209 pte_base = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE);
211 pte_base = pte_offset_kernel(pmd, 0);
213 for (k = 0; k < PTRS_PER_PTE; pte++, k++) {
214 vaddr = i*PGDIR_SIZE + j*PMD_SIZE + k*PAGE_SIZE;
215 if (end && (vaddr >= end))
219 *pte = mk_pte_phys(vaddr-start, PAGE_KERNEL);
221 set_pmd(pmd, __pmd(_KERNPG_TABLE + __pa(pte_base)));
226 static void __init pagetable_init (void)
228 unsigned long vaddr, end;
230 #ifdef CONFIG_X86_PAE
235 * This can be zero as well - no problem, in that case we exit
236 * the loops anyway due to the PTRS_PER_* conditions.
238 end = (unsigned long)__va(max_low_pfn*PAGE_SIZE);
240 pgd_base = swapper_pg_dir;
241 #ifdef CONFIG_X86_PAE
243 * It causes too many problems if there's no proper pmd set up
244 * for all 4 entries of the PGD - so we allocate all of them.
245 * PAE systems will not miss this extra 4-8K anyway ...
247 for (i = 0; i < PTRS_PER_PGD; i++) {
248 pmd_t *pmd = (pmd_t *) alloc_bootmem_low_pages(PAGE_SIZE);
249 set_pgd(pgd_base + i, __pgd(__pa(pmd) + 0x1));
253 * Set up lowmem-sized identity mappings at PAGE_OFFSET:
255 setup_identity_mappings(pgd_base, PAGE_OFFSET, end);
258 * Add flat-mode identity-mappings - SMP needs it when
259 * starting up on an AP from real-mode. (In the non-PAE
260 * case we already have these mappings through head.S.)
261 * All user-space mappings are explicitly cleared after
264 #if defined(CONFIG_SMP) && defined(CONFIG_X86_PAE)
265 setup_identity_mappings(pgd_base, 0, 16*1024*1024);
270 * Fixed mappings, only the page table structure has to be
271 * created - mappings will be set by set_fixmap():
273 vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
274 fixrange_init(vaddr, 0, pgd_base);
276 #ifdef CONFIG_HIGHMEM
286 fixrange_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base);
288 pgd = swapper_pg_dir + pgd_index(vaddr);
289 pmd = pmd_offset(pgd, vaddr);
290 pte = pte_offset_kernel(pmd, vaddr);
291 pkmap_page_table = pte;
297 * Clear kernel pagetables in a PMD_SIZE-aligned range.
299 static void clear_mappings(pgd_t *pgd_base, unsigned long start, unsigned long end)
308 for (i = 0; i < PTRS_PER_PGD; pgd++, i++) {
309 vaddr = i*PGDIR_SIZE;
310 if (end && (vaddr >= end))
312 pmd = pmd_offset(pgd, 0);
313 for (j = 0; j < PTRS_PER_PMD; pmd++, j++) {
314 vaddr = i*PGDIR_SIZE + j*PMD_SIZE;
315 if (end && (vaddr >= end))
325 void zap_low_mappings(void)
327 printk("zapping low mappings.\n");
329 * Zap initial low-memory mappings.
331 clear_mappings(swapper_pg_dir, 0, 16*1024*1024);
334 #ifndef CONFIG_DISCONTIGMEM
335 void __init zone_sizes_init(void)
337 unsigned long zones_size[MAX_NR_ZONES] = {0, 0, 0};
338 unsigned int max_dma, high, low;
340 max_dma = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
345 zones_size[ZONE_DMA] = low;
347 zones_size[ZONE_DMA] = max_dma;
348 zones_size[ZONE_NORMAL] = low - max_dma;
349 #ifdef CONFIG_HIGHMEM
350 zones_size[ZONE_HIGHMEM] = high - low;
353 free_area_init(zones_size);
356 extern void zone_sizes_init(void);
357 #endif /* !CONFIG_DISCONTIGMEM */
359 static int disable_nx __initdata = 0;
360 u64 __supported_pte_mask = ~_PAGE_NX;
365 * Control non executable mappings.
368 * off Disable (disables exec-shield too)
370 static int __init noexec_setup(char *str)
372 if (!strncmp(str, "on",2) && cpu_has_nx) {
373 __supported_pte_mask |= _PAGE_NX;
375 } else if (!strncmp(str,"off",3)) {
377 __supported_pte_mask &= ~_PAGE_NX;
383 __setup("noexec=", noexec_setup);
385 #ifdef CONFIG_X86_PAE
388 static void __init set_nx(void)
390 unsigned int v[4], l, h;
392 if (cpu_has_pae && (cpuid_eax(0x80000000) > 0x80000001)) {
393 cpuid(0x80000001, &v[0], &v[1], &v[2], &v[3]);
394 if ((v[3] & (1 << 20)) && !disable_nx) {
395 rdmsr(MSR_EFER, l, h);
397 wrmsr(MSR_EFER, l, h);
399 __supported_pte_mask |= _PAGE_NX;
404 * Enables/disables executability of a given kernel page and
405 * returns the previous setting.
407 int __init set_kernel_exec(unsigned long vaddr, int enable)
415 pte = lookup_address(vaddr);
418 if (!pte_exec_kernel(*pte))
422 pte->pte_high &= ~(1 << (_PAGE_BIT_NX - 32));
424 pte->pte_high |= 1 << (_PAGE_BIT_NX - 32);
433 * paging_init() sets up the page tables - note that the first 8MB are
434 * already mapped by head.S.
436 * This routines also unmaps the page at virtual kernel address 0, so
437 * that we can trap those pesky NULL-reference errors in the kernel.
439 void __init paging_init(void)
441 #ifdef CONFIG_X86_PAE
444 printk("NX (Execute Disable) protection: active\n");
449 load_cr3(swapper_pg_dir);
451 #ifdef CONFIG_X86_PAE
453 * We will bail out later - printk doesn't work right now so
454 * the user would just see a hanging kernel.
457 set_in_cr4(X86_CR4_PAE);
461 * Subtle. SMP is doing it's boot stuff late (because it has to
462 * fork idle threads) - but it also needs low mappings for the
463 * protected-mode entry to work. We zap these entries only after
464 * the WP-bit has been tested.
474 * Test if the WP bit works in supervisor mode. It isn't supported on 386's
475 * and also on some strange 486's (NexGen etc.). All 586+'s are OK. This
476 * used to involve black magic jumps to work around some nasty CPU bugs,
477 * but fortunately the switch to using exceptions got rid of all that.
480 void __init test_wp_bit(void)
482 printk("Checking if this processor honours the WP bit even in supervisor mode... ");
484 /* Any page-aligned address will do, the test is non-destructive */
485 __set_fixmap(FIX_WP_TEST, __pa(&swapper_pg_dir), PAGE_READONLY);
486 boot_cpu_data.wp_works_ok = do_test_wp_bit();
487 clear_fixmap(FIX_WP_TEST);
489 if (!boot_cpu_data.wp_works_ok) {
491 #ifdef CONFIG_X86_WP_WORKS_OK
492 panic("This kernel doesn't support CPU's with broken WP. Recompile it for a 386!");
499 #ifndef CONFIG_DISCONTIGMEM
500 static void __init set_max_mapnr_init(void)
502 #ifdef CONFIG_HIGHMEM
503 highmem_start_page = pfn_to_page(highstart_pfn);
504 max_mapnr = num_physpages = highend_pfn;
506 max_mapnr = num_physpages = max_low_pfn;
509 #define __free_all_bootmem() free_all_bootmem()
511 #define __free_all_bootmem() free_all_bootmem_node(NODE_DATA(0))
512 extern void set_max_mapnr_init(void);
513 #endif /* !CONFIG_DISCONTIGMEM */
515 static struct kcore_list kcore_mem, kcore_vmalloc;
517 void __init mem_init(void)
519 extern int ppro_with_ram_bug(void);
520 int codesize, reservedpages, datasize, initsize;
524 #ifndef CONFIG_DISCONTIGMEM
529 bad_ppro = ppro_with_ram_bug();
531 #ifdef CONFIG_HIGHMEM
532 /* check that fixmap and pkmap do not overlap */
533 if (PKMAP_BASE+LAST_PKMAP*PAGE_SIZE >= FIXADDR_START) {
534 printk(KERN_ERR "fixmap and kmap areas overlap - this will crash\n");
535 printk(KERN_ERR "pkstart: %lxh pkend: %lxh fixstart %lxh\n",
536 PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE, FIXADDR_START);
541 set_max_mapnr_init();
543 #ifdef CONFIG_HIGHMEM
544 high_memory = (void *) __va(highstart_pfn * PAGE_SIZE);
546 high_memory = (void *) __va(max_low_pfn * PAGE_SIZE);
549 /* this will put all low memory onto the freelists */
550 totalram_pages += __free_all_bootmem();
553 for (tmp = 0; tmp < max_low_pfn; tmp++)
555 * Only count reserved RAM pages
557 if (page_is_ram(tmp) && PageReserved(pfn_to_page(tmp)))
560 set_highmem_pages_init(bad_ppro);
562 codesize = (unsigned long) &_etext - (unsigned long) &_text;
563 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
564 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
566 kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
567 kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
568 VMALLOC_END-VMALLOC_START);
570 printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, %dk reserved, %dk data, %dk init, %ldk highmem)\n",
571 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
572 num_physpages << (PAGE_SHIFT-10),
574 reservedpages << (PAGE_SHIFT-10),
577 (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10))
580 #ifdef CONFIG_X86_PAE
582 panic("cannot execute a PAE-enabled kernel on a PAE-less CPU!");
584 if (boot_cpu_data.wp_works_ok < 0)
587 entry_trampoline_setup();
588 default_ldt_page = virt_to_page(default_ldt);
589 load_LDT(&init_mm.context);
592 kmem_cache_t *pgd_cache, *pmd_cache, *kpmd_cache;
594 void __init pgtable_cache_init(void)
596 void (*ctor)(void *, kmem_cache_t *, unsigned long);
597 void (*dtor)(void *, kmem_cache_t *, unsigned long);
599 if (PTRS_PER_PMD > 1) {
600 pmd_cache = kmem_cache_create("pmd",
601 PTRS_PER_PMD*sizeof(pmd_t),
602 PTRS_PER_PMD*sizeof(pmd_t),
607 panic("pgtable_cache_init(): cannot create pmd cache");
609 if (TASK_SIZE > PAGE_OFFSET) {
610 kpmd_cache = kmem_cache_create("kpmd",
611 PTRS_PER_PMD*sizeof(pmd_t),
612 PTRS_PER_PMD*sizeof(pmd_t),
617 panic("pgtable_cache_init(): "
618 "cannot create kpmd cache");
622 if (PTRS_PER_PMD == 1 || TASK_SIZE <= PAGE_OFFSET)
627 if (PTRS_PER_PMD == 1 && TASK_SIZE <= PAGE_OFFSET)
632 pgd_cache = kmem_cache_create("pgd",
633 PTRS_PER_PGD*sizeof(pgd_t),
634 PTRS_PER_PGD*sizeof(pgd_t),
639 panic("pgtable_cache_init(): Cannot create pgd cache");
643 * This function cannot be __init, since exceptions don't work in that
644 * section. Put this after the callers, so that it cannot be inlined.
646 static int noinline do_test_wp_bit(void)
651 __asm__ __volatile__(
656 ".section __ex_table,\"a\"\n"
660 :"=m" (*(char *)fix_to_virt(FIX_WP_TEST)),
669 void free_initmem(void)
673 addr = (unsigned long)(&__init_begin);
674 for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
675 ClearPageReserved(virt_to_page(addr));
676 set_page_count(virt_to_page(addr), 1);
680 printk (KERN_INFO "Freeing unused kernel memory: %dk freed\n", (__init_end - __init_begin) >> 10);
683 #ifdef CONFIG_BLK_DEV_INITRD
684 void free_initrd_mem(unsigned long start, unsigned long end)
687 printk (KERN_INFO "Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
688 for (; start < end; start += PAGE_SIZE) {
689 ClearPageReserved(virt_to_page(start));
690 set_page_count(virt_to_page(start), 1);