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>
44 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
45 unsigned long highstart_pfn, highend_pfn;
47 static int do_test_wp_bit(void);
50 * Creates a middle page table and puts a pointer to it in the
51 * given global directory entry. This only returns the gd entry
52 * in non-PAE compilation mode, since the middle layer is folded.
54 static pmd_t * __init one_md_table_init(pgd_t *pgd)
59 pmd_table = (pmd_t *) alloc_bootmem_low_pages(PAGE_SIZE);
60 set_pgd(pgd, __pgd(__pa(pmd_table) | _PAGE_PRESENT));
61 if (pmd_table != pmd_offset(pgd, 0))
64 pmd_table = pmd_offset(pgd, 0);
71 * Create a page table and place a pointer to it in a middle page
74 static pte_t * __init one_page_table_init(pmd_t *pmd)
77 pte_t *page_table = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE);
78 set_pmd(pmd, __pmd(__pa(page_table) | _PAGE_TABLE));
79 if (page_table != pte_offset_kernel(pmd, 0))
85 return pte_offset_kernel(pmd, 0);
89 * This function initializes a certain range of kernel virtual memory
90 * with new bootmem page tables, everywhere page tables are missing in
95 * NOTE: The pagetables are allocated contiguous on the physical space
96 * so we can cache the place of the first one and move around without
97 * checking the pgd every time.
99 static void __init page_table_range_init (unsigned long start, unsigned long end, pgd_t *pgd_base)
103 int pgd_idx, pmd_idx;
107 pgd_idx = pgd_index(vaddr);
108 pmd_idx = pmd_index(vaddr);
109 pgd = pgd_base + pgd_idx;
111 for ( ; (pgd_idx < PTRS_PER_PGD) && (vaddr != end); pgd++, pgd_idx++) {
113 one_md_table_init(pgd);
115 pmd = pmd_offset(pgd, vaddr);
116 for (; (pmd_idx < PTRS_PER_PMD) && (vaddr != end); pmd++, pmd_idx++) {
118 one_page_table_init(pmd);
127 * This maps the physical memory to kernel virtual address space, a total
128 * of max_low_pfn pages, by creating page tables starting from address
131 static void __init kernel_physical_mapping_init(pgd_t *pgd_base)
137 int pgd_idx, pmd_idx, pte_ofs;
139 pgd_idx = pgd_index(PAGE_OFFSET);
140 pgd = pgd_base + pgd_idx;
143 for (; pgd_idx < PTRS_PER_PGD; pgd++, pgd_idx++) {
144 pmd = one_md_table_init(pgd);
145 if (pfn >= max_low_pfn)
147 for (pmd_idx = 0; pmd_idx < PTRS_PER_PMD && pfn < max_low_pfn; pmd++, pmd_idx++) {
148 /* Map with big pages if possible, otherwise create normal page tables. */
150 set_pmd(pmd, pfn_pmd(pfn, PAGE_KERNEL_LARGE));
153 pte = one_page_table_init(pmd);
155 for (pte_ofs = 0; pte_ofs < PTRS_PER_PTE && pfn < max_low_pfn; pte++, pfn++, pte_ofs++)
156 set_pte(pte, pfn_pte(pfn, PAGE_KERNEL));
162 static inline int page_kills_ppro(unsigned long pagenr)
164 if (pagenr >= 0x70000 && pagenr <= 0x7003F)
169 extern int is_available_memory(efi_memory_desc_t *);
171 static inline int page_is_ram(unsigned long pagenr)
174 unsigned long addr, end;
177 efi_memory_desc_t *md;
179 for (i = 0; i < memmap.nr_map; i++) {
181 if (!is_available_memory(md))
183 addr = (md->phys_addr+PAGE_SIZE-1) >> PAGE_SHIFT;
184 end = (md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT)) >> PAGE_SHIFT;
186 if ((pagenr >= addr) && (pagenr < end))
192 for (i = 0; i < e820.nr_map; i++) {
194 if (e820.map[i].type != E820_RAM) /* not usable memory */
197 * !!!FIXME!!! Some BIOSen report areas as RAM that
198 * are not. Notably the 640->1Mb area. We need a sanity
201 addr = (e820.map[i].addr+PAGE_SIZE-1) >> PAGE_SHIFT;
202 end = (e820.map[i].addr+e820.map[i].size) >> PAGE_SHIFT;
203 if ((pagenr >= addr) && (pagenr < end))
209 #ifdef CONFIG_HIGHMEM
213 EXPORT_SYMBOL(kmap_prot);
214 EXPORT_SYMBOL(kmap_pte);
216 #define kmap_get_fixmap_pte(vaddr) \
217 pte_offset_kernel(pmd_offset(pgd_offset_k(vaddr), (vaddr)), (vaddr))
219 void __init kmap_init(void)
221 unsigned long kmap_vstart;
223 /* cache the first kmap pte */
224 kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
225 kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
227 kmap_prot = PAGE_KERNEL;
230 void __init permanent_kmaps_init(pgd_t *pgd_base)
238 page_table_range_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base);
240 pgd = swapper_pg_dir + pgd_index(vaddr);
241 pmd = pmd_offset(pgd, vaddr);
242 pte = pte_offset_kernel(pmd, vaddr);
243 pkmap_page_table = pte;
246 void __init one_highpage_init(struct page *page, int pfn, int bad_ppro)
248 if (page_is_ram(pfn) && !(bad_ppro && page_kills_ppro(pfn))) {
249 ClearPageReserved(page);
250 set_bit(PG_highmem, &page->flags);
251 set_page_count(page, 1);
255 SetPageReserved(page);
258 #ifndef CONFIG_DISCONTIGMEM
259 void __init set_highmem_pages_init(int bad_ppro)
262 for (pfn = highstart_pfn; pfn < highend_pfn; pfn++)
263 one_highpage_init(pfn_to_page(pfn), pfn, bad_ppro);
264 totalram_pages += totalhigh_pages;
267 extern void set_highmem_pages_init(int);
268 #endif /* !CONFIG_DISCONTIGMEM */
271 #define kmap_init() do { } while (0)
272 #define permanent_kmaps_init(pgd_base) do { } while (0)
273 #define set_highmem_pages_init(bad_ppro) do { } while (0)
274 #endif /* CONFIG_HIGHMEM */
276 unsigned long __PAGE_KERNEL = _PAGE_KERNEL;
278 #ifndef CONFIG_DISCONTIGMEM
279 #define remap_numa_kva() do {} while (0)
281 extern void __init remap_numa_kva(void);
284 static void __init pagetable_init (void)
287 pgd_t *pgd_base = swapper_pg_dir;
289 #ifdef CONFIG_X86_PAE
291 /* Init entries of the first-level page table to the zero page */
292 for (i = 0; i < PTRS_PER_PGD; i++)
293 set_pgd(pgd_base + i, __pgd(__pa(empty_zero_page) | _PAGE_PRESENT));
296 /* Enable PSE if available */
298 set_in_cr4(X86_CR4_PSE);
301 /* Enable PGE if available */
303 set_in_cr4(X86_CR4_PGE);
304 __PAGE_KERNEL |= _PAGE_GLOBAL;
307 kernel_physical_mapping_init(pgd_base);
311 * Fixed mappings, only the page table structure has to be
312 * created - mappings will be set by set_fixmap():
314 vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
315 page_table_range_init(vaddr, 0, pgd_base);
317 permanent_kmaps_init(pgd_base);
319 #ifdef CONFIG_X86_PAE
321 * Add low memory identity-mappings - SMP needs it when
322 * starting up on an AP from real-mode. In the non-PAE
323 * case we already have these mappings through head.S.
324 * All user-space mappings are explicitly cleared after
327 pgd_base[0] = pgd_base[USER_PTRS_PER_PGD];
331 #if defined(CONFIG_PM_DISK) || defined(CONFIG_SOFTWARE_SUSPEND)
333 * Swap suspend & friends need this for resume because things like the intel-agp
334 * driver might have split up a kernel 4MB mapping.
336 char __nosavedata swsusp_pg_dir[PAGE_SIZE]
337 __attribute__ ((aligned (PAGE_SIZE)));
339 static inline void save_pg_dir(void)
341 memcpy(swsusp_pg_dir, swapper_pg_dir, PAGE_SIZE);
344 static inline void save_pg_dir(void)
349 void zap_low_mappings (void)
356 * Zap initial low-memory mappings.
358 * Note that "pgd_clear()" doesn't do it for
359 * us, because pgd_clear() is a no-op on i386.
361 for (i = 0; i < USER_PTRS_PER_PGD; i++)
362 #ifdef CONFIG_X86_PAE
363 set_pgd(swapper_pg_dir+i, __pgd(1 + __pa(empty_zero_page)));
365 set_pgd(swapper_pg_dir+i, __pgd(0));
370 #ifndef CONFIG_DISCONTIGMEM
371 void __init zone_sizes_init(void)
373 unsigned long zones_size[MAX_NR_ZONES] = {0, 0, 0};
374 unsigned int max_dma, high, low;
376 max_dma = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
381 zones_size[ZONE_DMA] = low;
383 zones_size[ZONE_DMA] = max_dma;
384 zones_size[ZONE_NORMAL] = low - max_dma;
385 #ifdef CONFIG_HIGHMEM
386 zones_size[ZONE_HIGHMEM] = high - low;
389 free_area_init(zones_size);
392 extern void zone_sizes_init(void);
393 #endif /* !CONFIG_DISCONTIGMEM */
396 * paging_init() sets up the page tables - note that the first 8MB are
397 * already mapped by head.S.
399 * This routines also unmaps the page at virtual kernel address 0, so
400 * that we can trap those pesky NULL-reference errors in the kernel.
402 void __init paging_init(void)
406 load_cr3(swapper_pg_dir);
408 #ifdef CONFIG_X86_PAE
410 * We will bail out later - printk doesn't work right now so
411 * the user would just see a hanging kernel.
414 set_in_cr4(X86_CR4_PAE);
423 * Test if the WP bit works in supervisor mode. It isn't supported on 386's
424 * and also on some strange 486's (NexGen etc.). All 586+'s are OK. This
425 * used to involve black magic jumps to work around some nasty CPU bugs,
426 * but fortunately the switch to using exceptions got rid of all that.
429 void __init test_wp_bit(void)
431 printk("Checking if this processor honours the WP bit even in supervisor mode... ");
433 /* Any page-aligned address will do, the test is non-destructive */
434 __set_fixmap(FIX_WP_TEST, __pa(&swapper_pg_dir), PAGE_READONLY);
435 boot_cpu_data.wp_works_ok = do_test_wp_bit();
436 clear_fixmap(FIX_WP_TEST);
438 if (!boot_cpu_data.wp_works_ok) {
440 #ifdef CONFIG_X86_WP_WORKS_OK
441 panic("This kernel doesn't support CPU's with broken WP. Recompile it for a 386!");
448 #ifndef CONFIG_DISCONTIGMEM
449 static void __init set_max_mapnr_init(void)
451 #ifdef CONFIG_HIGHMEM
452 highmem_start_page = pfn_to_page(highstart_pfn);
453 max_mapnr = num_physpages = highend_pfn;
455 max_mapnr = num_physpages = max_low_pfn;
458 #define __free_all_bootmem() free_all_bootmem()
460 #define __free_all_bootmem() free_all_bootmem_node(NODE_DATA(0))
461 extern void set_max_mapnr_init(void);
462 #endif /* !CONFIG_DISCONTIGMEM */
464 static struct kcore_list kcore_mem, kcore_vmalloc;
466 void __init mem_init(void)
468 extern int ppro_with_ram_bug(void);
469 int codesize, reservedpages, datasize, initsize;
473 #ifndef CONFIG_DISCONTIGMEM
478 bad_ppro = ppro_with_ram_bug();
480 #ifdef CONFIG_HIGHMEM
481 /* check that fixmap and pkmap do not overlap */
482 if (PKMAP_BASE+LAST_PKMAP*PAGE_SIZE >= FIXADDR_START) {
483 printk(KERN_ERR "fixmap and kmap areas overlap - this will crash\n");
484 printk(KERN_ERR "pkstart: %lxh pkend: %lxh fixstart %lxh\n",
485 PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE, FIXADDR_START);
490 set_max_mapnr_init();
492 #ifdef CONFIG_HIGHMEM
493 high_memory = (void *) __va(highstart_pfn * PAGE_SIZE);
495 high_memory = (void *) __va(max_low_pfn * PAGE_SIZE);
498 /* this will put all low memory onto the freelists */
499 totalram_pages += __free_all_bootmem();
502 for (tmp = 0; tmp < max_low_pfn; tmp++)
504 * Only count reserved RAM pages
506 if (page_is_ram(tmp) && PageReserved(pfn_to_page(tmp)))
509 set_highmem_pages_init(bad_ppro);
511 codesize = (unsigned long) &_etext - (unsigned long) &_text;
512 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
513 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
515 kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
516 kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
517 VMALLOC_END-VMALLOC_START);
519 printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, %dk reserved, %dk data, %dk init, %ldk highmem)\n",
520 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
521 num_physpages << (PAGE_SHIFT-10),
523 reservedpages << (PAGE_SHIFT-10),
526 (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10))
529 #ifdef CONFIG_X86_PAE
531 panic("cannot execute a PAE-enabled kernel on a PAE-less CPU!");
533 if (boot_cpu_data.wp_works_ok < 0)
537 * Subtle. SMP is doing it's boot stuff late (because it has to
538 * fork idle threads) - but it also needs low mappings for the
539 * protected-mode entry to work. We zap these entries only after
540 * the WP-bit has been tested.
547 kmem_cache_t *pgd_cache;
548 kmem_cache_t *pmd_cache;
550 void __init pgtable_cache_init(void)
552 if (PTRS_PER_PMD > 1) {
553 pmd_cache = kmem_cache_create("pmd",
554 PTRS_PER_PMD*sizeof(pmd_t),
555 PTRS_PER_PMD*sizeof(pmd_t),
560 panic("pgtable_cache_init(): cannot create pmd cache");
562 pgd_cache = kmem_cache_create("pgd",
563 PTRS_PER_PGD*sizeof(pgd_t),
564 PTRS_PER_PGD*sizeof(pgd_t),
567 PTRS_PER_PMD == 1 ? pgd_dtor : NULL);
569 panic("pgtable_cache_init(): Cannot create pgd cache");
573 * This function cannot be __init, since exceptions don't work in that
574 * section. Put this after the callers, so that it cannot be inlined.
576 static int do_test_wp_bit(void)
581 __asm__ __volatile__(
586 ".section __ex_table,\"a\"\n"
590 :"=m" (*(char *)fix_to_virt(FIX_WP_TEST)),
599 void free_initmem(void)
603 addr = (unsigned long)(&__init_begin);
604 for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
605 ClearPageReserved(virt_to_page(addr));
606 set_page_count(virt_to_page(addr), 1);
610 printk (KERN_INFO "Freeing unused kernel memory: %dk freed\n", (__init_end - __init_begin) >> 10);
613 #ifdef CONFIG_BLK_DEV_INITRD
614 void free_initrd_mem(unsigned long start, unsigned long end)
617 printk (KERN_INFO "Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
618 for (; start < end; start += PAGE_SIZE) {
619 ClearPageReserved(virt_to_page(start));
620 set_page_count(virt_to_page(start), 1);