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>
35 #include <asm/pgalloc.h>
37 #include <asm/fixmap.h>
41 #include <asm/tlbflush.h>
42 #include <asm/sections.h>
45 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
46 unsigned long highstart_pfn, highend_pfn;
48 static int do_test_wp_bit(void);
50 static inline int page_kills_ppro(unsigned long pagenr)
52 if (pagenr >= 0x70000 && pagenr <= 0x7003F)
57 extern int is_available_memory(efi_memory_desc_t *);
59 int page_is_ram(unsigned long pagenr)
62 unsigned long addr, end;
65 efi_memory_desc_t *md;
67 for (i = 0; i < memmap.nr_map; i++) {
69 if (!is_available_memory(md))
71 addr = (md->phys_addr+PAGE_SIZE-1) >> PAGE_SHIFT;
72 end = (md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT)) >> PAGE_SHIFT;
74 if ((pagenr >= addr) && (pagenr < end))
80 for (i = 0; i < e820.nr_map; i++) {
82 if (e820.map[i].type != E820_RAM) /* not usable memory */
85 * !!!FIXME!!! Some BIOSen report areas as RAM that
86 * are not. Notably the 640->1Mb area. We need a sanity
89 addr = (e820.map[i].addr+PAGE_SIZE-1) >> PAGE_SHIFT;
90 end = (e820.map[i].addr+e820.map[i].size) >> PAGE_SHIFT;
91 if ((pagenr >= addr) && (pagenr < end))
99 EXPORT_SYMBOL(kmap_pte);
101 #define kmap_get_fixmap_pte(vaddr) \
102 pte_offset_kernel(pmd_offset(pgd_offset_k(vaddr), (vaddr)), (vaddr))
104 void __init kmap_init(void)
106 kmap_pte = kmap_get_fixmap_pte(__fix_to_virt(FIX_KMAP_BEGIN));
109 void __init one_highpage_init(struct page *page, int pfn, int bad_ppro)
111 if (page_is_ram(pfn) && !(bad_ppro && page_kills_ppro(pfn))) {
112 ClearPageReserved(page);
113 set_bit(PG_highmem, &page->flags);
114 set_page_count(page, 1);
118 SetPageReserved(page);
121 EXPORT_SYMBOL_GPL(page_is_ram);
123 #ifdef CONFIG_HIGHMEM
125 #ifndef CONFIG_DISCONTIGMEM
126 void __init set_highmem_pages_init(int bad_ppro)
129 for (pfn = highstart_pfn; pfn < highend_pfn; pfn++)
130 one_highpage_init(pfn_to_page(pfn), pfn, bad_ppro);
131 totalram_pages += totalhigh_pages;
134 extern void set_highmem_pages_init(int);
135 #endif /* !CONFIG_DISCONTIGMEM */
137 # define set_highmem_pages_init(bad_ppro) do { } while (0)
140 unsigned long __PAGE_KERNEL = _PAGE_KERNEL;
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_t *) 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;
296 #if defined(CONFIG_PM_DISK) || defined(CONFIG_SOFTWARE_SUSPEND)
298 * Swap suspend & friends need this for resume because things like the intel-agp
299 * driver might have split up a kernel 4MB mapping.
301 char __nosavedata swsusp_pg_dir[PAGE_SIZE]
302 __attribute__ ((aligned (PAGE_SIZE)));
304 static inline void save_pg_dir(void)
306 memcpy(swsusp_pg_dir, swapper_pg_dir, PAGE_SIZE);
309 static inline void save_pg_dir(void)
315 * Clear kernel pagetables in a PMD_SIZE-aligned range.
317 static void clear_mappings(pgd_t *pgd_base, unsigned long start, unsigned long end)
326 for (i = 0; i < PTRS_PER_PGD; pgd++, i++) {
327 vaddr = i*PGDIR_SIZE;
328 if (end && (vaddr >= end))
330 pmd = pmd_offset(pgd, 0);
331 for (j = 0; j < PTRS_PER_PMD; pmd++, j++) {
332 vaddr = i*PGDIR_SIZE + j*PMD_SIZE;
333 if (end && (vaddr >= end))
343 void zap_low_mappings(void)
345 printk("zapping low mappings.\n");
350 * Zap initial low-memory mappings.
352 clear_mappings(swapper_pg_dir, 0, 16*1024*1024);
355 #ifndef CONFIG_DISCONTIGMEM
356 void __init zone_sizes_init(void)
358 unsigned long zones_size[MAX_NR_ZONES] = {0, 0, 0};
359 unsigned int max_dma, high, low;
361 max_dma = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
366 zones_size[ZONE_DMA] = low;
368 zones_size[ZONE_DMA] = max_dma;
369 zones_size[ZONE_NORMAL] = low - max_dma;
370 #ifdef CONFIG_HIGHMEM
371 zones_size[ZONE_HIGHMEM] = high - low;
374 free_area_init(zones_size);
377 extern void zone_sizes_init(void);
378 #endif /* !CONFIG_DISCONTIGMEM */
381 * paging_init() sets up the page tables - note that the first 8MB are
382 * already mapped by head.S.
384 * This routines also unmaps the page at virtual kernel address 0, so
385 * that we can trap those pesky NULL-reference errors in the kernel.
387 void __init paging_init(void)
391 load_cr3(swapper_pg_dir);
393 #ifdef CONFIG_X86_PAE
395 * We will bail out later - printk doesn't work right now so
396 * the user would just see a hanging kernel.
399 set_in_cr4(X86_CR4_PAE);
403 * Subtle. SMP is doing it's boot stuff late (because it has to
404 * fork idle threads) - but it also needs low mappings for the
405 * protected-mode entry to work. We zap these entries only after
406 * the WP-bit has been tested.
416 * Test if the WP bit works in supervisor mode. It isn't supported on 386's
417 * and also on some strange 486's (NexGen etc.). All 586+'s are OK. This
418 * used to involve black magic jumps to work around some nasty CPU bugs,
419 * but fortunately the switch to using exceptions got rid of all that.
422 void __init test_wp_bit(void)
424 printk("Checking if this processor honours the WP bit even in supervisor mode... ");
426 /* Any page-aligned address will do, the test is non-destructive */
427 __set_fixmap(FIX_WP_TEST, __pa(&swapper_pg_dir), PAGE_READONLY);
428 boot_cpu_data.wp_works_ok = do_test_wp_bit();
429 clear_fixmap(FIX_WP_TEST);
431 if (!boot_cpu_data.wp_works_ok) {
433 #ifdef CONFIG_X86_WP_WORKS_OK
434 panic("This kernel doesn't support CPU's with broken WP. Recompile it for a 386!");
441 #ifndef CONFIG_DISCONTIGMEM
442 static void __init set_max_mapnr_init(void)
444 #ifdef CONFIG_HIGHMEM
445 highmem_start_page = pfn_to_page(highstart_pfn);
446 max_mapnr = num_physpages = highend_pfn;
448 max_mapnr = num_physpages = max_low_pfn;
451 #define __free_all_bootmem() free_all_bootmem()
453 #define __free_all_bootmem() free_all_bootmem_node(NODE_DATA(0))
454 extern void set_max_mapnr_init(void);
455 #endif /* !CONFIG_DISCONTIGMEM */
457 static struct kcore_list kcore_mem, kcore_vmalloc;
459 void __init mem_init(void)
461 extern int ppro_with_ram_bug(void);
462 int codesize, reservedpages, datasize, initsize;
466 #ifndef CONFIG_DISCONTIGMEM
471 bad_ppro = ppro_with_ram_bug();
473 #ifdef CONFIG_HIGHMEM
474 /* check that fixmap and pkmap do not overlap */
475 if (PKMAP_BASE+LAST_PKMAP*PAGE_SIZE >= FIXADDR_START) {
476 printk(KERN_ERR "fixmap and kmap areas overlap - this will crash\n");
477 printk(KERN_ERR "pkstart: %lxh pkend: %lxh fixstart %lxh\n",
478 PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE, FIXADDR_START);
483 set_max_mapnr_init();
485 #ifdef CONFIG_HIGHMEM
486 high_memory = (void *) __va(highstart_pfn * PAGE_SIZE);
488 high_memory = (void *) __va(max_low_pfn * PAGE_SIZE);
491 /* this will put all low memory onto the freelists */
492 totalram_pages += __free_all_bootmem();
495 for (tmp = 0; tmp < max_low_pfn; tmp++)
497 * Only count reserved RAM pages
499 if (page_is_ram(tmp) && PageReserved(pfn_to_page(tmp)))
502 set_highmem_pages_init(bad_ppro);
504 codesize = (unsigned long) &_etext - (unsigned long) &_text;
505 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
506 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
508 kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
509 kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
510 VMALLOC_END-VMALLOC_START);
512 printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, %dk reserved, %dk data, %dk init, %ldk highmem)\n",
513 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
514 num_physpages << (PAGE_SHIFT-10),
516 reservedpages << (PAGE_SHIFT-10),
519 (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10))
522 #ifdef CONFIG_X86_PAE
524 panic("cannot execute a PAE-enabled kernel on a PAE-less CPU!");
526 if (boot_cpu_data.wp_works_ok < 0)
529 entry_trampoline_setup();
530 default_ldt_page = virt_to_page(default_ldt);
531 load_LDT(&init_mm.context);
534 kmem_cache_t *pgd_cache, *pmd_cache, *kpmd_cache;
536 void __init pgtable_cache_init(void)
538 void (*ctor)(void *, kmem_cache_t *, unsigned long);
539 void (*dtor)(void *, kmem_cache_t *, unsigned long);
541 if (PTRS_PER_PMD > 1) {
542 pmd_cache = kmem_cache_create("pmd",
543 PTRS_PER_PMD*sizeof(pmd_t),
544 PTRS_PER_PMD*sizeof(pmd_t),
549 panic("pgtable_cache_init(): cannot create pmd cache");
551 if (TASK_SIZE > PAGE_OFFSET) {
552 kpmd_cache = kmem_cache_create("kpmd",
553 PTRS_PER_PMD*sizeof(pmd_t),
554 PTRS_PER_PMD*sizeof(pmd_t),
559 panic("pgtable_cache_init(): "
560 "cannot create kpmd cache");
564 if (PTRS_PER_PMD == 1 || TASK_SIZE <= PAGE_OFFSET)
569 if (PTRS_PER_PMD == 1 && TASK_SIZE <= PAGE_OFFSET)
574 pgd_cache = kmem_cache_create("pgd",
575 PTRS_PER_PGD*sizeof(pgd_t),
576 PTRS_PER_PGD*sizeof(pgd_t),
581 panic("pgtable_cache_init(): Cannot create pgd cache");
585 * This function cannot be __init, since exceptions don't work in that
586 * section. Put this after the callers, so that it cannot be inlined.
588 static int noinline do_test_wp_bit(void)
593 __asm__ __volatile__(
598 ".section __ex_table,\"a\"\n"
602 :"=m" (*(char *)fix_to_virt(FIX_WP_TEST)),
611 void free_initmem(void)
615 addr = (unsigned long)(&__init_begin);
616 for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
617 ClearPageReserved(virt_to_page(addr));
618 set_page_count(virt_to_page(addr), 1);
622 printk (KERN_INFO "Freeing unused kernel memory: %dk freed\n", (__init_end - __init_begin) >> 10);
625 #ifdef CONFIG_BLK_DEV_INITRD
626 void free_initrd_mem(unsigned long start, unsigned long end)
629 printk (KERN_INFO "Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
630 for (; start < end; start += PAGE_SIZE) {
631 ClearPageReserved(virt_to_page(start));
632 set_page_count(virt_to_page(start), 1);