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))
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));
105 EXPORT_SYMBOL(kmap_pte);
107 #define kmap_get_fixmap_pte(vaddr) \
108 pte_offset_kernel(pmd_offset(pgd_offset_k(vaddr), (vaddr)), (vaddr))
110 void __init kmap_init(void)
112 kmap_pte = kmap_get_fixmap_pte(__fix_to_virt(FIX_KMAP_BEGIN));
115 void __init one_highpage_init(struct page *page, int pfn, int bad_ppro)
117 if (page_is_ram(pfn) && !(bad_ppro && page_kills_ppro(pfn))) {
118 ClearPageReserved(page);
119 set_bit(PG_highmem, &page->flags);
120 set_page_count(page, 1);
124 SetPageReserved(page);
127 EXPORT_SYMBOL_GPL(page_is_ram);
129 #ifdef CONFIG_HIGHMEM
131 #ifndef CONFIG_DISCONTIGMEM
132 void __init set_highmem_pages_init(int bad_ppro)
135 for (pfn = highstart_pfn; pfn < highend_pfn; pfn++)
136 one_highpage_init(pfn_to_page(pfn), pfn, bad_ppro);
137 totalram_pages += totalhigh_pages;
140 extern void set_highmem_pages_init(int);
141 #endif /* !CONFIG_DISCONTIGMEM */
143 # define set_highmem_pages_init(bad_ppro) do { } while (0)
146 unsigned long long __PAGE_KERNEL = _PAGE_KERNEL;
147 unsigned long long __PAGE_KERNEL_EXEC = _PAGE_KERNEL_EXEC;
149 #ifndef CONFIG_DISCONTIGMEM
150 #define remap_numa_kva() do {} while (0)
152 extern void __init remap_numa_kva(void);
155 static __init void prepare_pagetables(pgd_t *pgd_base, unsigned long address)
161 pgd = pgd_base + pgd_index(address);
162 pmd = pmd_offset(pgd, address);
163 if (!pmd_present(*pmd)) {
164 pte = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE);
165 set_pmd(pmd, __pmd(_PAGE_TABLE + __pa(pte)));
169 static void __init fixrange_init (unsigned long start, unsigned long end, pgd_t *pgd_base)
173 for (vaddr = start; vaddr != end; vaddr += PAGE_SIZE)
174 prepare_pagetables(pgd_base, vaddr);
177 void setup_identity_mappings(pgd_t *pgd_base, unsigned long start, unsigned long end)
183 pte_t *pte, *pte_base;
187 for (i = 0; i < PTRS_PER_PGD; pgd++, i++) {
188 vaddr = i*PGDIR_SIZE;
189 if (end && (vaddr >= end))
191 pmd = pmd_offset(pgd, 0);
192 for (j = 0; j < PTRS_PER_PMD; pmd++, j++) {
193 vaddr = i*PGDIR_SIZE + j*PMD_SIZE;
194 if (end && (vaddr >= end))
201 set_in_cr4(X86_CR4_PSE);
202 boot_cpu_data.wp_works_ok = 1;
203 __pe = _KERNPG_TABLE + _PAGE_PSE + vaddr - start;
204 /* Make it "global" too if supported */
206 set_in_cr4(X86_CR4_PGE);
207 #if !defined(CONFIG_X86_SWITCH_PAGETABLES)
208 __pe += _PAGE_GLOBAL;
209 __PAGE_KERNEL |= _PAGE_GLOBAL;
212 set_pmd(pmd, __pmd(__pe));
215 if (!pmd_present(*pmd))
216 pte_base = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE);
218 pte_base = pte_offset_kernel(pmd, 0);
220 for (k = 0; k < PTRS_PER_PTE; pte++, k++) {
221 vaddr = i*PGDIR_SIZE + j*PMD_SIZE + k*PAGE_SIZE;
222 if (end && (vaddr >= end))
226 *pte = mk_pte_phys(vaddr-start, PAGE_KERNEL);
228 set_pmd(pmd, __pmd(_KERNPG_TABLE + __pa(pte_base)));
233 static void __init pagetable_init (void)
235 unsigned long vaddr, end;
237 #ifdef CONFIG_X86_PAE
242 * This can be zero as well - no problem, in that case we exit
243 * the loops anyway due to the PTRS_PER_* conditions.
245 end = (unsigned long)__va(max_low_pfn*PAGE_SIZE);
247 pgd_base = swapper_pg_dir;
248 #ifdef CONFIG_X86_PAE
250 * It causes too many problems if there's no proper pmd set up
251 * for all 4 entries of the PGD - so we allocate all of them.
252 * PAE systems will not miss this extra 4-8K anyway ...
254 for (i = 0; i < PTRS_PER_PGD; i++) {
255 pmd_t *pmd = (pmd_t *) alloc_bootmem_low_pages(PAGE_SIZE);
256 set_pgd(pgd_base + i, __pgd(__pa(pmd) + 0x1));
260 * Set up lowmem-sized identity mappings at PAGE_OFFSET:
262 setup_identity_mappings(pgd_base, PAGE_OFFSET, end);
265 * Add flat-mode identity-mappings - SMP needs it when
266 * starting up on an AP from real-mode. (In the non-PAE
267 * case we already have these mappings through head.S.)
268 * All user-space mappings are explicitly cleared after
271 #if defined(CONFIG_SMP) && defined(CONFIG_X86_PAE)
272 setup_identity_mappings(pgd_base, 0, 16*1024*1024);
277 * Fixed mappings, only the page table structure has to be
278 * created - mappings will be set by set_fixmap():
280 vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
281 fixrange_init(vaddr, 0, pgd_base);
283 #ifdef CONFIG_HIGHMEM
293 fixrange_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base);
295 pgd = swapper_pg_dir + pgd_index(vaddr);
296 pmd = pmd_offset(pgd, vaddr);
297 pte = pte_offset_kernel(pmd, vaddr);
298 pkmap_page_table = pte;
304 * Clear kernel pagetables in a PMD_SIZE-aligned range.
306 static void clear_mappings(pgd_t *pgd_base, unsigned long start, unsigned long end)
315 for (i = 0; i < PTRS_PER_PGD; pgd++, i++) {
316 vaddr = i*PGDIR_SIZE;
317 if (end && (vaddr >= end))
319 pmd = pmd_offset(pgd, 0);
320 for (j = 0; j < PTRS_PER_PMD; pmd++, j++) {
321 vaddr = i*PGDIR_SIZE + j*PMD_SIZE;
322 if (end && (vaddr >= end))
332 void zap_low_mappings(void)
334 printk("zapping low mappings.\n");
336 * Zap initial low-memory mappings.
338 clear_mappings(swapper_pg_dir, 0, 16*1024*1024);
341 #ifndef CONFIG_DISCONTIGMEM
342 void __init zone_sizes_init(void)
344 unsigned long zones_size[MAX_NR_ZONES] = {0, 0, 0};
345 unsigned int max_dma, high, low;
347 max_dma = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
352 zones_size[ZONE_DMA] = low;
354 zones_size[ZONE_DMA] = max_dma;
355 zones_size[ZONE_NORMAL] = low - max_dma;
356 #ifdef CONFIG_HIGHMEM
357 zones_size[ZONE_HIGHMEM] = high - low;
360 free_area_init(zones_size);
363 extern void zone_sizes_init(void);
364 #endif /* !CONFIG_DISCONTIGMEM */
366 static int disable_nx __initdata = 0;
367 u64 __supported_pte_mask = ~_PAGE_NX;
372 * Control non executable mappings.
375 * off Disable (disables exec-shield too)
377 static int __init noexec_setup(char *str)
379 if (!strncmp(str, "on",2) && cpu_has_nx) {
380 __supported_pte_mask |= _PAGE_NX;
382 } else if (!strncmp(str,"off",3)) {
384 __supported_pte_mask &= ~_PAGE_NX;
390 __setup("noexec=", noexec_setup);
392 #ifdef CONFIG_X86_PAE
395 static void __init set_nx(void)
397 unsigned int v[4], l, h;
399 if (cpu_has_pae && (cpuid_eax(0x80000000) > 0x80000001)) {
400 cpuid(0x80000001, &v[0], &v[1], &v[2], &v[3]);
401 if ((v[3] & (1 << 20)) && !disable_nx) {
402 rdmsr(MSR_EFER, l, h);
404 wrmsr(MSR_EFER, l, h);
406 __supported_pte_mask |= _PAGE_NX;
411 * Enables/disables executability of a given kernel page and
412 * returns the previous setting.
414 int __init set_kernel_exec(unsigned long vaddr, int enable)
422 pte = lookup_address(vaddr);
425 if (!pte_exec_kernel(*pte))
429 pte->pte_high &= ~(1 << (_PAGE_BIT_NX - 32));
431 pte->pte_high |= 1 << (_PAGE_BIT_NX - 32);
440 * paging_init() sets up the page tables - note that the first 8MB are
441 * already mapped by head.S.
443 * This routines also unmaps the page at virtual kernel address 0, so
444 * that we can trap those pesky NULL-reference errors in the kernel.
446 void __init paging_init(void)
448 #ifdef CONFIG_X86_PAE
451 printk("NX (Execute Disable) protection: active\n");
456 load_cr3(swapper_pg_dir);
458 #ifdef CONFIG_X86_PAE
460 * We will bail out later - printk doesn't work right now so
461 * the user would just see a hanging kernel.
464 set_in_cr4(X86_CR4_PAE);
468 * Subtle. SMP is doing it's boot stuff late (because it has to
469 * fork idle threads) - but it also needs low mappings for the
470 * protected-mode entry to work. We zap these entries only after
471 * the WP-bit has been tested.
481 * Test if the WP bit works in supervisor mode. It isn't supported on 386's
482 * and also on some strange 486's (NexGen etc.). All 586+'s are OK. This
483 * used to involve black magic jumps to work around some nasty CPU bugs,
484 * but fortunately the switch to using exceptions got rid of all that.
487 void __init test_wp_bit(void)
489 printk("Checking if this processor honours the WP bit even in supervisor mode... ");
491 /* Any page-aligned address will do, the test is non-destructive */
492 __set_fixmap(FIX_WP_TEST, __pa(&swapper_pg_dir), PAGE_READONLY);
493 boot_cpu_data.wp_works_ok = do_test_wp_bit();
494 clear_fixmap(FIX_WP_TEST);
496 if (!boot_cpu_data.wp_works_ok) {
498 #ifdef CONFIG_X86_WP_WORKS_OK
499 panic("This kernel doesn't support CPU's with broken WP. Recompile it for a 386!");
506 #ifndef CONFIG_DISCONTIGMEM
507 static void __init set_max_mapnr_init(void)
509 #ifdef CONFIG_HIGHMEM
510 highmem_start_page = pfn_to_page(highstart_pfn);
511 max_mapnr = num_physpages = highend_pfn;
513 max_mapnr = num_physpages = max_low_pfn;
516 #define __free_all_bootmem() free_all_bootmem()
518 #define __free_all_bootmem() free_all_bootmem_node(NODE_DATA(0))
519 extern void set_max_mapnr_init(void);
520 #endif /* !CONFIG_DISCONTIGMEM */
522 static struct kcore_list kcore_mem, kcore_vmalloc;
524 void __init mem_init(void)
526 extern int ppro_with_ram_bug(void);
527 int codesize, reservedpages, datasize, initsize;
531 #ifndef CONFIG_DISCONTIGMEM
536 bad_ppro = ppro_with_ram_bug();
538 #ifdef CONFIG_HIGHMEM
539 /* check that fixmap and pkmap do not overlap */
540 if (PKMAP_BASE+LAST_PKMAP*PAGE_SIZE >= FIXADDR_START) {
541 printk(KERN_ERR "fixmap and kmap areas overlap - this will crash\n");
542 printk(KERN_ERR "pkstart: %lxh pkend: %lxh fixstart %lxh\n",
543 PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE, FIXADDR_START);
548 set_max_mapnr_init();
550 #ifdef CONFIG_HIGHMEM
551 high_memory = (void *) __va(highstart_pfn * PAGE_SIZE);
553 high_memory = (void *) __va(max_low_pfn * PAGE_SIZE);
556 /* this will put all low memory onto the freelists */
557 totalram_pages += __free_all_bootmem();
560 for (tmp = 0; tmp < max_low_pfn; tmp++)
562 * Only count reserved RAM pages
564 if (page_is_ram(tmp) && PageReserved(pfn_to_page(tmp)))
567 set_highmem_pages_init(bad_ppro);
569 codesize = (unsigned long) &_etext - (unsigned long) &_text;
570 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
571 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
573 kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
574 kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
575 VMALLOC_END-VMALLOC_START);
577 printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, %dk reserved, %dk data, %dk init, %ldk highmem)\n",
578 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
579 num_physpages << (PAGE_SHIFT-10),
581 reservedpages << (PAGE_SHIFT-10),
584 (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10))
587 #ifdef CONFIG_X86_PAE
589 panic("cannot execute a PAE-enabled kernel on a PAE-less CPU!");
591 if (boot_cpu_data.wp_works_ok < 0)
594 entry_trampoline_setup();
595 default_ldt_page = virt_to_page(default_ldt);
596 load_LDT(&init_mm.context);
599 kmem_cache_t *pgd_cache, *pmd_cache, *kpmd_cache;
601 void __init pgtable_cache_init(void)
603 void (*ctor)(void *, kmem_cache_t *, unsigned long);
604 void (*dtor)(void *, kmem_cache_t *, unsigned long);
606 if (PTRS_PER_PMD > 1) {
607 pmd_cache = kmem_cache_create("pmd",
608 PTRS_PER_PMD*sizeof(pmd_t),
609 PTRS_PER_PMD*sizeof(pmd_t),
614 panic("pgtable_cache_init(): cannot create pmd cache");
616 if (TASK_SIZE > PAGE_OFFSET) {
617 kpmd_cache = kmem_cache_create("kpmd",
618 PTRS_PER_PMD*sizeof(pmd_t),
619 PTRS_PER_PMD*sizeof(pmd_t),
624 panic("pgtable_cache_init(): "
625 "cannot create kpmd cache");
629 if (PTRS_PER_PMD == 1 || TASK_SIZE <= PAGE_OFFSET)
634 if (PTRS_PER_PMD == 1 && TASK_SIZE <= PAGE_OFFSET)
639 pgd_cache = kmem_cache_create("pgd",
640 PTRS_PER_PGD*sizeof(pgd_t),
641 PTRS_PER_PGD*sizeof(pgd_t),
646 panic("pgtable_cache_init(): Cannot create pgd cache");
650 * This function cannot be __init, since exceptions don't work in that
651 * section. Put this after the callers, so that it cannot be inlined.
653 static int noinline do_test_wp_bit(void)
658 __asm__ __volatile__(
663 ".section __ex_table,\"a\"\n"
667 :"=m" (*(char *)fix_to_virt(FIX_WP_TEST)),
676 void free_initmem(void)
680 addr = (unsigned long)(&__init_begin);
681 for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
682 ClearPageReserved(virt_to_page(addr));
683 set_page_count(virt_to_page(addr), 1);
687 printk (KERN_INFO "Freeing unused kernel memory: %dk freed\n", (__init_end - __init_begin) >> 10);
690 #ifdef CONFIG_BLK_DEV_INITRD
691 void free_initrd_mem(unsigned long start, unsigned long end)
694 printk (KERN_INFO "Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
695 for (; start < end; start += PAGE_SIZE) {
696 ClearPageReserved(virt_to_page(start));
697 set_page_count(virt_to_page(start), 1);