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>
43 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
44 unsigned long highstart_pfn, highend_pfn;
46 static int do_test_wp_bit(void);
49 * Creates a middle page table and puts a pointer to it in the
50 * given global directory entry. This only returns the gd entry
51 * in non-PAE compilation mode, since the middle layer is folded.
53 static pmd_t * __init one_md_table_init(pgd_t *pgd)
58 pmd_table = (pmd_t *) alloc_bootmem_low_pages(PAGE_SIZE);
59 set_pgd(pgd, __pgd(__pa(pmd_table) | _PAGE_PRESENT));
60 if (pmd_table != pmd_offset(pgd, 0))
63 pmd_table = pmd_offset(pgd, 0);
70 * Create a page table and place a pointer to it in a middle page
73 static pte_t * __init one_page_table_init(pmd_t *pmd)
76 pte_t *page_table = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE);
77 set_pmd(pmd, __pmd(__pa(page_table) | _PAGE_TABLE));
78 if (page_table != pte_offset_kernel(pmd, 0))
84 return pte_offset_kernel(pmd, 0);
88 * This function initializes a certain range of kernel virtual memory
89 * with new bootmem page tables, everywhere page tables are missing in
94 * NOTE: The pagetables are allocated contiguous on the physical space
95 * so we can cache the place of the first one and move around without
96 * checking the pgd every time.
98 static void __init page_table_range_init (unsigned long start, unsigned long end, pgd_t *pgd_base)
102 int pgd_idx, pmd_idx;
106 pgd_idx = pgd_index(vaddr);
107 pmd_idx = pmd_index(vaddr);
108 pgd = pgd_base + pgd_idx;
110 for ( ; (pgd_idx < PTRS_PER_PGD) && (vaddr != end); pgd++, pgd_idx++) {
112 one_md_table_init(pgd);
114 pmd = pmd_offset(pgd, vaddr);
115 for (; (pmd_idx < PTRS_PER_PMD) && (vaddr != end); pmd++, pmd_idx++) {
117 one_page_table_init(pmd);
125 static inline int is_kernel_text(unsigned long addr)
127 if (addr >= (unsigned long)_stext && addr <= (unsigned long)__init_end)
133 * This maps the physical memory to kernel virtual address space, a total
134 * of max_low_pfn pages, by creating page tables starting from address
137 static void __init kernel_physical_mapping_init(pgd_t *pgd_base)
143 int pgd_idx, pmd_idx, pte_ofs;
145 pgd_idx = pgd_index(PAGE_OFFSET);
146 pgd = pgd_base + pgd_idx;
149 for (; pgd_idx < PTRS_PER_PGD; pgd++, pgd_idx++) {
150 pmd = one_md_table_init(pgd);
151 if (pfn >= max_low_pfn)
153 for (pmd_idx = 0; pmd_idx < PTRS_PER_PMD && pfn < max_low_pfn; pmd++, pmd_idx++) {
154 unsigned int address = pfn * PAGE_SIZE + PAGE_OFFSET;
156 /* Map with big pages if possible, otherwise create normal page tables. */
158 unsigned int address2 = (pfn + PTRS_PER_PTE - 1) * PAGE_SIZE + PAGE_OFFSET + PAGE_SIZE-1;
160 if (is_kernel_text(address) || is_kernel_text(address2))
161 set_pmd(pmd, pfn_pmd(pfn, PAGE_KERNEL_LARGE_EXEC));
163 set_pmd(pmd, pfn_pmd(pfn, PAGE_KERNEL_LARGE));
166 pte = one_page_table_init(pmd);
168 for (pte_ofs = 0; pte_ofs < PTRS_PER_PTE && pfn < max_low_pfn; pte++, pfn++, pte_ofs++) {
169 if (is_kernel_text(address))
170 set_pte(pte, pfn_pte(pfn, PAGE_KERNEL_EXEC));
172 set_pte(pte, pfn_pte(pfn, PAGE_KERNEL));
179 static inline int page_kills_ppro(unsigned long pagenr)
181 if (pagenr >= 0x70000 && pagenr <= 0x7003F)
186 extern int is_available_memory(efi_memory_desc_t *);
188 static inline int page_is_ram(unsigned long pagenr)
191 unsigned long addr, end;
194 efi_memory_desc_t *md;
196 for (i = 0; i < memmap.nr_map; i++) {
198 if (!is_available_memory(md))
200 addr = (md->phys_addr+PAGE_SIZE-1) >> PAGE_SHIFT;
201 end = (md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT)) >> PAGE_SHIFT;
203 if ((pagenr >= addr) && (pagenr < end))
209 for (i = 0; i < e820.nr_map; i++) {
211 if (e820.map[i].type != E820_RAM) /* not usable memory */
214 * !!!FIXME!!! Some BIOSen report areas as RAM that
215 * are not. Notably the 640->1Mb area. We need a sanity
218 addr = (e820.map[i].addr+PAGE_SIZE-1) >> PAGE_SHIFT;
219 end = (e820.map[i].addr+e820.map[i].size) >> PAGE_SHIFT;
220 if ((pagenr >= addr) && (pagenr < end))
226 #ifdef CONFIG_HIGHMEM
230 EXPORT_SYMBOL(kmap_prot);
231 EXPORT_SYMBOL(kmap_pte);
233 #define kmap_get_fixmap_pte(vaddr) \
234 pte_offset_kernel(pmd_offset(pgd_offset_k(vaddr), (vaddr)), (vaddr))
236 void __init kmap_init(void)
238 unsigned long kmap_vstart;
240 /* cache the first kmap pte */
241 kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
242 kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
244 kmap_prot = PAGE_KERNEL;
247 void __init permanent_kmaps_init(pgd_t *pgd_base)
255 page_table_range_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base);
257 pgd = swapper_pg_dir + pgd_index(vaddr);
258 pmd = pmd_offset(pgd, vaddr);
259 pte = pte_offset_kernel(pmd, vaddr);
260 pkmap_page_table = pte;
263 void __init one_highpage_init(struct page *page, int pfn, int bad_ppro)
265 if (page_is_ram(pfn) && !(bad_ppro && page_kills_ppro(pfn))) {
266 ClearPageReserved(page);
267 set_bit(PG_highmem, &page->flags);
268 set_page_count(page, 1);
272 SetPageReserved(page);
275 #ifndef CONFIG_DISCONTIGMEM
276 void __init set_highmem_pages_init(int bad_ppro)
279 for (pfn = highstart_pfn; pfn < highend_pfn; pfn++)
280 one_highpage_init(pfn_to_page(pfn), pfn, bad_ppro);
281 totalram_pages += totalhigh_pages;
284 extern void set_highmem_pages_init(int);
285 #endif /* !CONFIG_DISCONTIGMEM */
288 #define kmap_init() do { } while (0)
289 #define permanent_kmaps_init(pgd_base) do { } while (0)
290 #define set_highmem_pages_init(bad_ppro) do { } while (0)
291 #endif /* CONFIG_HIGHMEM */
293 unsigned long long __PAGE_KERNEL = _PAGE_KERNEL;
294 unsigned long long __PAGE_KERNEL_EXEC = _PAGE_KERNEL_EXEC;
296 #ifndef CONFIG_DISCONTIGMEM
297 #define remap_numa_kva() do {} while (0)
299 extern void __init remap_numa_kva(void);
302 static void __init pagetable_init (void)
305 pgd_t *pgd_base = swapper_pg_dir;
307 #ifdef CONFIG_X86_PAE
309 /* Init entries of the first-level page table to the zero page */
310 for (i = 0; i < PTRS_PER_PGD; i++)
311 set_pgd(pgd_base + i, __pgd(__pa(empty_zero_page) | _PAGE_PRESENT));
314 /* Enable PSE if available */
316 set_in_cr4(X86_CR4_PSE);
319 /* Enable PGE if available */
321 set_in_cr4(X86_CR4_PGE);
322 __PAGE_KERNEL |= _PAGE_GLOBAL;
323 __PAGE_KERNEL_EXEC |= _PAGE_GLOBAL;
326 kernel_physical_mapping_init(pgd_base);
330 * Fixed mappings, only the page table structure has to be
331 * created - mappings will be set by set_fixmap():
333 vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
334 page_table_range_init(vaddr, 0, pgd_base);
336 permanent_kmaps_init(pgd_base);
338 #ifdef CONFIG_X86_PAE
340 * Add low memory identity-mappings - SMP needs it when
341 * starting up on an AP from real-mode. In the non-PAE
342 * case we already have these mappings through head.S.
343 * All user-space mappings are explicitly cleared after
346 pgd_base[0] = pgd_base[USER_PTRS_PER_PGD];
350 #if defined(CONFIG_PM_DISK) || defined(CONFIG_SOFTWARE_SUSPEND)
352 * Swap suspend & friends need this for resume because things like the intel-agp
353 * driver might have split up a kernel 4MB mapping.
355 char __nosavedata swsusp_pg_dir[PAGE_SIZE]
356 __attribute__ ((aligned (PAGE_SIZE)));
358 static inline void save_pg_dir(void)
360 memcpy(swsusp_pg_dir, swapper_pg_dir, PAGE_SIZE);
363 static inline void save_pg_dir(void)
368 void zap_low_mappings (void)
375 * Zap initial low-memory mappings.
377 * Note that "pgd_clear()" doesn't do it for
378 * us, because pgd_clear() is a no-op on i386.
380 for (i = 0; i < USER_PTRS_PER_PGD; i++)
381 #ifdef CONFIG_X86_PAE
382 set_pgd(swapper_pg_dir+i, __pgd(1 + __pa(empty_zero_page)));
384 set_pgd(swapper_pg_dir+i, __pgd(0));
389 #ifndef CONFIG_DISCONTIGMEM
390 void __init zone_sizes_init(void)
392 unsigned long zones_size[MAX_NR_ZONES] = {0, 0, 0};
393 unsigned int max_dma, high, low;
395 max_dma = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
400 zones_size[ZONE_DMA] = low;
402 zones_size[ZONE_DMA] = max_dma;
403 zones_size[ZONE_NORMAL] = low - max_dma;
404 #ifdef CONFIG_HIGHMEM
405 zones_size[ZONE_HIGHMEM] = high - low;
408 free_area_init(zones_size);
411 extern void zone_sizes_init(void);
412 #endif /* !CONFIG_DISCONTIGMEM */
414 static int disable_nx __initdata = 0;
415 u64 __supported_pte_mask = ~_PAGE_NX;
420 * Control non executable mappings.
425 static int __init noexec_setup(char *str)
427 if (!strncmp(str, "on",2) && cpu_has_nx) {
428 __supported_pte_mask |= _PAGE_NX;
430 } else if (!strncmp(str,"off",3)) {
432 __supported_pte_mask &= ~_PAGE_NX;
437 __setup("noexec=", noexec_setup);
439 #ifdef CONFIG_X86_PAE
442 static void __init set_nx(void)
444 unsigned int v[4], l, h;
446 if (cpu_has_pae && (cpuid_eax(0x80000000) > 0x80000001)) {
447 cpuid(0x80000001, &v[0], &v[1], &v[2], &v[3]);
448 if ((v[3] & (1 << 20)) && !disable_nx) {
449 rdmsr(MSR_EFER, l, h);
451 wrmsr(MSR_EFER, l, h);
453 __supported_pte_mask |= _PAGE_NX;
459 * Enables/disables executability of a given kernel page and
460 * returns the previous setting.
462 int __init set_kernel_exec(unsigned long vaddr, int enable)
470 pte = lookup_address(vaddr);
473 if (!pte_exec_kernel(*pte))
477 pte->pte_high &= ~(1 << (_PAGE_BIT_NX - 32));
479 pte->pte_high |= 1 << (_PAGE_BIT_NX - 32);
488 * paging_init() sets up the page tables - note that the first 8MB are
489 * already mapped by head.S.
491 * This routines also unmaps the page at virtual kernel address 0, so
492 * that we can trap those pesky NULL-reference errors in the kernel.
494 void __init paging_init(void)
496 #ifdef CONFIG_X86_PAE
499 printk("NX (Execute Disable) protection: active\n");
504 load_cr3(swapper_pg_dir);
506 #ifdef CONFIG_X86_PAE
508 * We will bail out later - printk doesn't work right now so
509 * the user would just see a hanging kernel.
512 set_in_cr4(X86_CR4_PAE);
521 * Test if the WP bit works in supervisor mode. It isn't supported on 386's
522 * and also on some strange 486's (NexGen etc.). All 586+'s are OK. This
523 * used to involve black magic jumps to work around some nasty CPU bugs,
524 * but fortunately the switch to using exceptions got rid of all that.
527 void __init test_wp_bit(void)
529 printk("Checking if this processor honours the WP bit even in supervisor mode... ");
531 /* Any page-aligned address will do, the test is non-destructive */
532 __set_fixmap(FIX_WP_TEST, __pa(&swapper_pg_dir), PAGE_READONLY);
533 boot_cpu_data.wp_works_ok = do_test_wp_bit();
534 clear_fixmap(FIX_WP_TEST);
536 if (!boot_cpu_data.wp_works_ok) {
538 #ifdef CONFIG_X86_WP_WORKS_OK
539 panic("This kernel doesn't support CPU's with broken WP. Recompile it for a 386!");
546 #ifndef CONFIG_DISCONTIGMEM
547 static void __init set_max_mapnr_init(void)
549 #ifdef CONFIG_HIGHMEM
550 highmem_start_page = pfn_to_page(highstart_pfn);
551 max_mapnr = num_physpages = highend_pfn;
553 max_mapnr = num_physpages = max_low_pfn;
556 #define __free_all_bootmem() free_all_bootmem()
558 #define __free_all_bootmem() free_all_bootmem_node(NODE_DATA(0))
559 extern void set_max_mapnr_init(void);
560 #endif /* !CONFIG_DISCONTIGMEM */
562 static struct kcore_list kcore_mem, kcore_vmalloc;
564 void __init mem_init(void)
566 extern int ppro_with_ram_bug(void);
567 int codesize, reservedpages, datasize, initsize;
571 #ifndef CONFIG_DISCONTIGMEM
576 bad_ppro = ppro_with_ram_bug();
578 #ifdef CONFIG_HIGHMEM
579 /* check that fixmap and pkmap do not overlap */
580 if (PKMAP_BASE+LAST_PKMAP*PAGE_SIZE >= FIXADDR_START) {
581 printk(KERN_ERR "fixmap and kmap areas overlap - this will crash\n");
582 printk(KERN_ERR "pkstart: %lxh pkend: %lxh fixstart %lxh\n",
583 PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE, FIXADDR_START);
588 set_max_mapnr_init();
590 #ifdef CONFIG_HIGHMEM
591 high_memory = (void *) __va(highstart_pfn * PAGE_SIZE);
593 high_memory = (void *) __va(max_low_pfn * PAGE_SIZE);
596 /* this will put all low memory onto the freelists */
597 totalram_pages += __free_all_bootmem();
600 for (tmp = 0; tmp < max_low_pfn; tmp++)
602 * Only count reserved RAM pages
604 if (page_is_ram(tmp) && PageReserved(pfn_to_page(tmp)))
607 set_highmem_pages_init(bad_ppro);
609 codesize = (unsigned long) &_etext - (unsigned long) &_text;
610 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
611 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
613 kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
614 kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
615 VMALLOC_END-VMALLOC_START);
617 printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, %dk reserved, %dk data, %dk init, %ldk highmem)\n",
618 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
619 num_physpages << (PAGE_SHIFT-10),
621 reservedpages << (PAGE_SHIFT-10),
624 (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10))
627 #ifdef CONFIG_X86_PAE
629 panic("cannot execute a PAE-enabled kernel on a PAE-less CPU!");
631 if (boot_cpu_data.wp_works_ok < 0)
635 * Subtle. SMP is doing it's boot stuff late (because it has to
636 * fork idle threads) - but it also needs low mappings for the
637 * protected-mode entry to work. We zap these entries only after
638 * the WP-bit has been tested.
645 kmem_cache_t *pgd_cache;
646 kmem_cache_t *pmd_cache;
648 void __init pgtable_cache_init(void)
650 if (PTRS_PER_PMD > 1) {
651 pmd_cache = kmem_cache_create("pmd",
652 PTRS_PER_PMD*sizeof(pmd_t),
653 PTRS_PER_PMD*sizeof(pmd_t),
658 panic("pgtable_cache_init(): cannot create pmd cache");
660 pgd_cache = kmem_cache_create("pgd",
661 PTRS_PER_PGD*sizeof(pgd_t),
662 PTRS_PER_PGD*sizeof(pgd_t),
665 PTRS_PER_PMD == 1 ? pgd_dtor : NULL);
667 panic("pgtable_cache_init(): Cannot create pgd cache");
671 * This function cannot be __init, since exceptions don't work in that
672 * section. Put this after the callers, so that it cannot be inlined.
674 static int do_test_wp_bit(void)
679 __asm__ __volatile__(
684 ".section __ex_table,\"a\"\n"
688 :"=m" (*(char *)fix_to_virt(FIX_WP_TEST)),
697 void free_initmem(void)
701 addr = (unsigned long)(&__init_begin);
702 for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
703 ClearPageReserved(virt_to_page(addr));
704 set_page_count(virt_to_page(addr), 1);
708 printk (KERN_INFO "Freeing unused kernel memory: %dk freed\n", (__init_end - __init_begin) >> 10);
711 #ifdef CONFIG_BLK_DEV_INITRD
712 void free_initrd_mem(unsigned long start, unsigned long end)
715 printk (KERN_INFO "Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
716 for (; start < end; start += PAGE_SIZE) {
717 ClearPageReserved(virt_to_page(start));
718 set_page_count(virt_to_page(start), 1);