Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/shaggy...
[pandora-kernel.git] / arch / x86 / mm / init_32.c
1 /*
2  *  linux/arch/i386/mm/init.c
3  *
4  *  Copyright (C) 1995  Linus Torvalds
5  *
6  *  Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
7  */
8
9 #include <linux/module.h>
10 #include <linux/signal.h>
11 #include <linux/sched.h>
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/string.h>
15 #include <linux/types.h>
16 #include <linux/ptrace.h>
17 #include <linux/mman.h>
18 #include <linux/mm.h>
19 #include <linux/hugetlb.h>
20 #include <linux/swap.h>
21 #include <linux/smp.h>
22 #include <linux/init.h>
23 #include <linux/highmem.h>
24 #include <linux/pagemap.h>
25 #include <linux/pfn.h>
26 #include <linux/poison.h>
27 #include <linux/bootmem.h>
28 #include <linux/slab.h>
29 #include <linux/proc_fs.h>
30 #include <linux/efi.h>
31 #include <linux/memory_hotplug.h>
32 #include <linux/initrd.h>
33 #include <linux/cpumask.h>
34
35 #include <asm/processor.h>
36 #include <asm/system.h>
37 #include <asm/uaccess.h>
38 #include <asm/pgtable.h>
39 #include <asm/dma.h>
40 #include <asm/fixmap.h>
41 #include <asm/e820.h>
42 #include <asm/apic.h>
43 #include <asm/tlb.h>
44 #include <asm/tlbflush.h>
45 #include <asm/sections.h>
46 #include <asm/paravirt.h>
47
48 unsigned int __VMALLOC_RESERVE = 128 << 20;
49
50 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
51 unsigned long highstart_pfn, highend_pfn;
52
53 static int noinline do_test_wp_bit(void);
54
55 /*
56  * Creates a middle page table and puts a pointer to it in the
57  * given global directory entry. This only returns the gd entry
58  * in non-PAE compilation mode, since the middle layer is folded.
59  */
60 static pmd_t * __init one_md_table_init(pgd_t *pgd)
61 {
62         pud_t *pud;
63         pmd_t *pmd_table;
64                 
65 #ifdef CONFIG_X86_PAE
66         if (!(pgd_val(*pgd) & _PAGE_PRESENT)) {
67                 pmd_table = (pmd_t *) alloc_bootmem_low_pages(PAGE_SIZE);
68
69                 paravirt_alloc_pd(__pa(pmd_table) >> PAGE_SHIFT);
70                 set_pgd(pgd, __pgd(__pa(pmd_table) | _PAGE_PRESENT));
71                 pud = pud_offset(pgd, 0);
72                 if (pmd_table != pmd_offset(pud, 0))
73                         BUG();
74         }
75 #endif
76         pud = pud_offset(pgd, 0);
77         pmd_table = pmd_offset(pud, 0);
78         return pmd_table;
79 }
80
81 /*
82  * Create a page table and place a pointer to it in a middle page
83  * directory entry.
84  */
85 static pte_t * __init one_page_table_init(pmd_t *pmd)
86 {
87         if (!(pmd_val(*pmd) & _PAGE_PRESENT)) {
88                 pte_t *page_table = NULL;
89
90 #ifdef CONFIG_DEBUG_PAGEALLOC
91                 page_table = (pte_t *) alloc_bootmem_pages(PAGE_SIZE);
92 #endif
93                 if (!page_table)
94                         page_table =
95                                 (pte_t *)alloc_bootmem_low_pages(PAGE_SIZE);
96
97                 paravirt_alloc_pt(&init_mm, __pa(page_table) >> PAGE_SHIFT);
98                 set_pmd(pmd, __pmd(__pa(page_table) | _PAGE_TABLE));
99                 BUG_ON(page_table != pte_offset_kernel(pmd, 0));
100         }
101
102         return pte_offset_kernel(pmd, 0);
103 }
104
105 /*
106  * This function initializes a certain range of kernel virtual memory 
107  * with new bootmem page tables, everywhere page tables are missing in
108  * the given range.
109  */
110
111 /*
112  * NOTE: The pagetables are allocated contiguous on the physical space 
113  * so we can cache the place of the first one and move around without 
114  * checking the pgd every time.
115  */
116 static void __init page_table_range_init (unsigned long start, unsigned long end, pgd_t *pgd_base)
117 {
118         pgd_t *pgd;
119         pmd_t *pmd;
120         int pgd_idx, pmd_idx;
121         unsigned long vaddr;
122
123         vaddr = start;
124         pgd_idx = pgd_index(vaddr);
125         pmd_idx = pmd_index(vaddr);
126         pgd = pgd_base + pgd_idx;
127
128         for ( ; (pgd_idx < PTRS_PER_PGD) && (vaddr != end); pgd++, pgd_idx++) {
129                 pmd = one_md_table_init(pgd);
130                 pmd = pmd + pmd_index(vaddr);
131                 for (; (pmd_idx < PTRS_PER_PMD) && (vaddr != end); pmd++, pmd_idx++) {
132                         one_page_table_init(pmd);
133
134                         vaddr += PMD_SIZE;
135                 }
136                 pmd_idx = 0;
137         }
138 }
139
140 static inline int is_kernel_text(unsigned long addr)
141 {
142         if (addr >= PAGE_OFFSET && addr <= (unsigned long)__init_end)
143                 return 1;
144         return 0;
145 }
146
147 /*
148  * This maps the physical memory to kernel virtual address space, a total 
149  * of max_low_pfn pages, by creating page tables starting from address 
150  * PAGE_OFFSET.
151  */
152 static void __init kernel_physical_mapping_init(pgd_t *pgd_base)
153 {
154         unsigned long pfn;
155         pgd_t *pgd;
156         pmd_t *pmd;
157         pte_t *pte;
158         int pgd_idx, pmd_idx, pte_ofs;
159
160         pgd_idx = pgd_index(PAGE_OFFSET);
161         pgd = pgd_base + pgd_idx;
162         pfn = 0;
163
164         for (; pgd_idx < PTRS_PER_PGD; pgd++, pgd_idx++) {
165                 pmd = one_md_table_init(pgd);
166                 if (pfn >= max_low_pfn)
167                         continue;
168                 for (pmd_idx = 0; pmd_idx < PTRS_PER_PMD && pfn < max_low_pfn; pmd++, pmd_idx++) {
169                         unsigned int address = pfn * PAGE_SIZE + PAGE_OFFSET;
170
171                         /* Map with big pages if possible, otherwise create normal page tables. */
172                         if (cpu_has_pse) {
173                                 unsigned int address2 = (pfn + PTRS_PER_PTE - 1) * PAGE_SIZE + PAGE_OFFSET + PAGE_SIZE-1;
174                                 if (is_kernel_text(address) || is_kernel_text(address2))
175                                         set_pmd(pmd, pfn_pmd(pfn, PAGE_KERNEL_LARGE_EXEC));
176                                 else
177                                         set_pmd(pmd, pfn_pmd(pfn, PAGE_KERNEL_LARGE));
178
179                                 pfn += PTRS_PER_PTE;
180                         } else {
181                                 pte = one_page_table_init(pmd);
182
183                                 for (pte_ofs = 0;
184                                      pte_ofs < PTRS_PER_PTE && pfn < max_low_pfn;
185                                      pte++, pfn++, pte_ofs++, address += PAGE_SIZE) {
186                                         if (is_kernel_text(address))
187                                                 set_pte(pte, pfn_pte(pfn, PAGE_KERNEL_EXEC));
188                                         else
189                                                 set_pte(pte, pfn_pte(pfn, PAGE_KERNEL));
190                                 }
191                         }
192                 }
193         }
194 }
195
196 static inline int page_kills_ppro(unsigned long pagenr)
197 {
198         if (pagenr >= 0x70000 && pagenr <= 0x7003F)
199                 return 1;
200         return 0;
201 }
202
203 int page_is_ram(unsigned long pagenr)
204 {
205         int i;
206         unsigned long addr, end;
207
208         if (efi_enabled) {
209                 efi_memory_desc_t *md;
210                 void *p;
211
212                 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
213                         md = p;
214                         if (!is_available_memory(md))
215                                 continue;
216                         addr = (md->phys_addr+PAGE_SIZE-1) >> PAGE_SHIFT;
217                         end = (md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT)) >> PAGE_SHIFT;
218
219                         if ((pagenr >= addr) && (pagenr < end))
220                                 return 1;
221                 }
222                 return 0;
223         }
224
225         for (i = 0; i < e820.nr_map; i++) {
226
227                 if (e820.map[i].type != E820_RAM)       /* not usable memory */
228                         continue;
229                 /*
230                  *      !!!FIXME!!! Some BIOSen report areas as RAM that
231                  *      are not. Notably the 640->1Mb area. We need a sanity
232                  *      check here.
233                  */
234                 addr = (e820.map[i].addr+PAGE_SIZE-1) >> PAGE_SHIFT;
235                 end = (e820.map[i].addr+e820.map[i].size) >> PAGE_SHIFT;
236                 if  ((pagenr >= addr) && (pagenr < end))
237                         return 1;
238         }
239         return 0;
240 }
241
242 #ifdef CONFIG_HIGHMEM
243 pte_t *kmap_pte;
244 pgprot_t kmap_prot;
245
246 #define kmap_get_fixmap_pte(vaddr)                                      \
247         pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr), vaddr), (vaddr)), (vaddr))
248
249 static void __init kmap_init(void)
250 {
251         unsigned long kmap_vstart;
252
253         /* cache the first kmap pte */
254         kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
255         kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
256
257         kmap_prot = PAGE_KERNEL;
258 }
259
260 static void __init permanent_kmaps_init(pgd_t *pgd_base)
261 {
262         pgd_t *pgd;
263         pud_t *pud;
264         pmd_t *pmd;
265         pte_t *pte;
266         unsigned long vaddr;
267
268         vaddr = PKMAP_BASE;
269         page_table_range_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base);
270
271         pgd = swapper_pg_dir + pgd_index(vaddr);
272         pud = pud_offset(pgd, vaddr);
273         pmd = pmd_offset(pud, vaddr);
274         pte = pte_offset_kernel(pmd, vaddr);
275         pkmap_page_table = pte; 
276 }
277
278 static void __meminit free_new_highpage(struct page *page)
279 {
280         init_page_count(page);
281         __free_page(page);
282         totalhigh_pages++;
283 }
284
285 void __init add_one_highpage_init(struct page *page, int pfn, int bad_ppro)
286 {
287         if (page_is_ram(pfn) && !(bad_ppro && page_kills_ppro(pfn))) {
288                 ClearPageReserved(page);
289                 free_new_highpage(page);
290         } else
291                 SetPageReserved(page);
292 }
293
294 static int __meminit add_one_highpage_hotplug(struct page *page, unsigned long pfn)
295 {
296         free_new_highpage(page);
297         totalram_pages++;
298 #ifdef CONFIG_FLATMEM
299         max_mapnr = max(pfn, max_mapnr);
300 #endif
301         num_physpages++;
302         return 0;
303 }
304
305 /*
306  * Not currently handling the NUMA case.
307  * Assuming single node and all memory that
308  * has been added dynamically that would be
309  * onlined here is in HIGHMEM
310  */
311 void __meminit online_page(struct page *page)
312 {
313         ClearPageReserved(page);
314         add_one_highpage_hotplug(page, page_to_pfn(page));
315 }
316
317
318 #ifdef CONFIG_NUMA
319 extern void set_highmem_pages_init(int);
320 #else
321 static void __init set_highmem_pages_init(int bad_ppro)
322 {
323         int pfn;
324         for (pfn = highstart_pfn; pfn < highend_pfn; pfn++) {
325                 /*
326                  * Holes under sparsemem might not have no mem_map[]:
327                  */
328                 if (pfn_valid(pfn))
329                         add_one_highpage_init(pfn_to_page(pfn), pfn, bad_ppro);
330         }
331         totalram_pages += totalhigh_pages;
332 }
333 #endif /* CONFIG_FLATMEM */
334
335 #else
336 #define kmap_init() do { } while (0)
337 #define permanent_kmaps_init(pgd_base) do { } while (0)
338 #define set_highmem_pages_init(bad_ppro) do { } while (0)
339 #endif /* CONFIG_HIGHMEM */
340
341 unsigned long long __PAGE_KERNEL = _PAGE_KERNEL;
342 EXPORT_SYMBOL(__PAGE_KERNEL);
343 unsigned long long __PAGE_KERNEL_EXEC = _PAGE_KERNEL_EXEC;
344
345 #ifdef CONFIG_NUMA
346 extern void __init remap_numa_kva(void);
347 #else
348 #define remap_numa_kva() do {} while (0)
349 #endif
350
351 void __init native_pagetable_setup_start(pgd_t *base)
352 {
353 #ifdef CONFIG_X86_PAE
354         int i;
355
356         /*
357          * Init entries of the first-level page table to the
358          * zero page, if they haven't already been set up.
359          *
360          * In a normal native boot, we'll be running on a
361          * pagetable rooted in swapper_pg_dir, but not in PAE
362          * mode, so this will end up clobbering the mappings
363          * for the lower 24Mbytes of the address space,
364          * without affecting the kernel address space.
365          */
366         for (i = 0; i < USER_PTRS_PER_PGD; i++)
367                 set_pgd(&base[i],
368                         __pgd(__pa(empty_zero_page) | _PAGE_PRESENT));
369
370         /* Make sure kernel address space is empty so that a pagetable
371            will be allocated for it. */
372         memset(&base[USER_PTRS_PER_PGD], 0,
373                KERNEL_PGD_PTRS * sizeof(pgd_t));
374 #else
375         paravirt_alloc_pd(__pa(swapper_pg_dir) >> PAGE_SHIFT);
376 #endif
377 }
378
379 void __init native_pagetable_setup_done(pgd_t *base)
380 {
381 #ifdef CONFIG_X86_PAE
382         /*
383          * Add low memory identity-mappings - SMP needs it when
384          * starting up on an AP from real-mode. In the non-PAE
385          * case we already have these mappings through head.S.
386          * All user-space mappings are explicitly cleared after
387          * SMP startup.
388          */
389         set_pgd(&base[0], base[USER_PTRS_PER_PGD]);
390 #endif
391 }
392
393 /*
394  * Build a proper pagetable for the kernel mappings.  Up until this
395  * point, we've been running on some set of pagetables constructed by
396  * the boot process.
397  *
398  * If we're booting on native hardware, this will be a pagetable
399  * constructed in arch/i386/kernel/head.S, and not running in PAE mode
400  * (even if we'll end up running in PAE).  The root of the pagetable
401  * will be swapper_pg_dir.
402  *
403  * If we're booting paravirtualized under a hypervisor, then there are
404  * more options: we may already be running PAE, and the pagetable may
405  * or may not be based in swapper_pg_dir.  In any case,
406  * paravirt_pagetable_setup_start() will set up swapper_pg_dir
407  * appropriately for the rest of the initialization to work.
408  *
409  * In general, pagetable_init() assumes that the pagetable may already
410  * be partially populated, and so it avoids stomping on any existing
411  * mappings.
412  */
413 static void __init pagetable_init (void)
414 {
415         unsigned long vaddr, end;
416         pgd_t *pgd_base = swapper_pg_dir;
417
418         paravirt_pagetable_setup_start(pgd_base);
419
420         /* Enable PSE if available */
421         if (cpu_has_pse)
422                 set_in_cr4(X86_CR4_PSE);
423
424         /* Enable PGE if available */
425         if (cpu_has_pge) {
426                 set_in_cr4(X86_CR4_PGE);
427                 __PAGE_KERNEL |= _PAGE_GLOBAL;
428                 __PAGE_KERNEL_EXEC |= _PAGE_GLOBAL;
429         }
430
431         kernel_physical_mapping_init(pgd_base);
432         remap_numa_kva();
433
434         /*
435          * Fixed mappings, only the page table structure has to be
436          * created - mappings will be set by set_fixmap():
437          */
438         vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
439         end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK;
440         page_table_range_init(vaddr, end, pgd_base);
441
442         permanent_kmaps_init(pgd_base);
443
444         paravirt_pagetable_setup_done(pgd_base);
445 }
446
447 #if defined(CONFIG_HIBERNATION) || defined(CONFIG_ACPI)
448 /*
449  * Swap suspend & friends need this for resume because things like the intel-agp
450  * driver might have split up a kernel 4MB mapping.
451  */
452 char __nosavedata swsusp_pg_dir[PAGE_SIZE]
453         __attribute__ ((aligned (PAGE_SIZE)));
454
455 static inline void save_pg_dir(void)
456 {
457         memcpy(swsusp_pg_dir, swapper_pg_dir, PAGE_SIZE);
458 }
459 #else
460 static inline void save_pg_dir(void)
461 {
462 }
463 #endif
464
465 void zap_low_mappings (void)
466 {
467         int i;
468
469         save_pg_dir();
470
471         /*
472          * Zap initial low-memory mappings.
473          *
474          * Note that "pgd_clear()" doesn't do it for
475          * us, because pgd_clear() is a no-op on i386.
476          */
477         for (i = 0; i < USER_PTRS_PER_PGD; i++)
478 #ifdef CONFIG_X86_PAE
479                 set_pgd(swapper_pg_dir+i, __pgd(1 + __pa(empty_zero_page)));
480 #else
481                 set_pgd(swapper_pg_dir+i, __pgd(0));
482 #endif
483         flush_tlb_all();
484 }
485
486 int nx_enabled = 0;
487
488 #ifdef CONFIG_X86_PAE
489
490 static int disable_nx __initdata = 0;
491 u64 __supported_pte_mask __read_mostly = ~_PAGE_NX;
492 EXPORT_SYMBOL_GPL(__supported_pte_mask);
493
494 /*
495  * noexec = on|off
496  *
497  * Control non executable mappings.
498  *
499  * on      Enable
500  * off     Disable
501  */
502 static int __init noexec_setup(char *str)
503 {
504         if (!str || !strcmp(str, "on")) {
505                 if (cpu_has_nx) {
506                         __supported_pte_mask |= _PAGE_NX;
507                         disable_nx = 0;
508                 }
509         } else if (!strcmp(str,"off")) {
510                 disable_nx = 1;
511                 __supported_pte_mask &= ~_PAGE_NX;
512         } else
513                 return -EINVAL;
514
515         return 0;
516 }
517 early_param("noexec", noexec_setup);
518
519 static void __init set_nx(void)
520 {
521         unsigned int v[4], l, h;
522
523         if (cpu_has_pae && (cpuid_eax(0x80000000) > 0x80000001)) {
524                 cpuid(0x80000001, &v[0], &v[1], &v[2], &v[3]);
525                 if ((v[3] & (1 << 20)) && !disable_nx) {
526                         rdmsr(MSR_EFER, l, h);
527                         l |= EFER_NX;
528                         wrmsr(MSR_EFER, l, h);
529                         nx_enabled = 1;
530                         __supported_pte_mask |= _PAGE_NX;
531                 }
532         }
533 }
534
535 /*
536  * Enables/disables executability of a given kernel page and
537  * returns the previous setting.
538  */
539 int __init set_kernel_exec(unsigned long vaddr, int enable)
540 {
541         pte_t *pte;
542         int ret = 1;
543
544         if (!nx_enabled)
545                 goto out;
546
547         pte = lookup_address(vaddr);
548         BUG_ON(!pte);
549
550         if (!pte_exec_kernel(*pte))
551                 ret = 0;
552
553         if (enable)
554                 pte->pte_high &= ~(1 << (_PAGE_BIT_NX - 32));
555         else
556                 pte->pte_high |= 1 << (_PAGE_BIT_NX - 32);
557         pte_update_defer(&init_mm, vaddr, pte);
558         __flush_tlb_all();
559 out:
560         return ret;
561 }
562
563 #endif
564
565 /*
566  * paging_init() sets up the page tables - note that the first 8MB are
567  * already mapped by head.S.
568  *
569  * This routines also unmaps the page at virtual kernel address 0, so
570  * that we can trap those pesky NULL-reference errors in the kernel.
571  */
572 void __init paging_init(void)
573 {
574 #ifdef CONFIG_X86_PAE
575         set_nx();
576         if (nx_enabled)
577                 printk("NX (Execute Disable) protection: active\n");
578 #endif
579
580         pagetable_init();
581
582         load_cr3(swapper_pg_dir);
583
584 #ifdef CONFIG_X86_PAE
585         /*
586          * We will bail out later - printk doesn't work right now so
587          * the user would just see a hanging kernel.
588          */
589         if (cpu_has_pae)
590                 set_in_cr4(X86_CR4_PAE);
591 #endif
592         __flush_tlb_all();
593
594         kmap_init();
595 }
596
597 /*
598  * Test if the WP bit works in supervisor mode. It isn't supported on 386's
599  * and also on some strange 486's (NexGen etc.). All 586+'s are OK. This
600  * used to involve black magic jumps to work around some nasty CPU bugs,
601  * but fortunately the switch to using exceptions got rid of all that.
602  */
603
604 static void __init test_wp_bit(void)
605 {
606         printk("Checking if this processor honours the WP bit even in supervisor mode... ");
607
608         /* Any page-aligned address will do, the test is non-destructive */
609         __set_fixmap(FIX_WP_TEST, __pa(&swapper_pg_dir), PAGE_READONLY);
610         boot_cpu_data.wp_works_ok = do_test_wp_bit();
611         clear_fixmap(FIX_WP_TEST);
612
613         if (!boot_cpu_data.wp_works_ok) {
614                 printk("No.\n");
615 #ifdef CONFIG_X86_WP_WORKS_OK
616                 panic("This kernel doesn't support CPU's with broken WP. Recompile it for a 386!");
617 #endif
618         } else {
619                 printk("Ok.\n");
620         }
621 }
622
623 static struct kcore_list kcore_mem, kcore_vmalloc; 
624
625 void __init mem_init(void)
626 {
627         extern int ppro_with_ram_bug(void);
628         int codesize, reservedpages, datasize, initsize;
629         int tmp;
630         int bad_ppro;
631
632 #ifdef CONFIG_FLATMEM
633         BUG_ON(!mem_map);
634 #endif
635         
636         bad_ppro = ppro_with_ram_bug();
637
638 #ifdef CONFIG_HIGHMEM
639         /* check that fixmap and pkmap do not overlap */
640         if (PKMAP_BASE+LAST_PKMAP*PAGE_SIZE >= FIXADDR_START) {
641                 printk(KERN_ERR "fixmap and kmap areas overlap - this will crash\n");
642                 printk(KERN_ERR "pkstart: %lxh pkend: %lxh fixstart %lxh\n",
643                                 PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE, FIXADDR_START);
644                 BUG();
645         }
646 #endif
647  
648         /* this will put all low memory onto the freelists */
649         totalram_pages += free_all_bootmem();
650
651         reservedpages = 0;
652         for (tmp = 0; tmp < max_low_pfn; tmp++)
653                 /*
654                  * Only count reserved RAM pages
655                  */
656                 if (page_is_ram(tmp) && PageReserved(pfn_to_page(tmp)))
657                         reservedpages++;
658
659         set_highmem_pages_init(bad_ppro);
660
661         codesize =  (unsigned long) &_etext - (unsigned long) &_text;
662         datasize =  (unsigned long) &_edata - (unsigned long) &_etext;
663         initsize =  (unsigned long) &__init_end - (unsigned long) &__init_begin;
664
665         kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT); 
666         kclist_add(&kcore_vmalloc, (void *)VMALLOC_START, 
667                    VMALLOC_END-VMALLOC_START);
668
669         printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, %dk reserved, %dk data, %dk init, %ldk highmem)\n",
670                 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
671                 num_physpages << (PAGE_SHIFT-10),
672                 codesize >> 10,
673                 reservedpages << (PAGE_SHIFT-10),
674                 datasize >> 10,
675                 initsize >> 10,
676                 (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10))
677                );
678
679 #if 1 /* double-sanity-check paranoia */
680         printk("virtual kernel memory layout:\n"
681                "    fixmap  : 0x%08lx - 0x%08lx   (%4ld kB)\n"
682 #ifdef CONFIG_HIGHMEM
683                "    pkmap   : 0x%08lx - 0x%08lx   (%4ld kB)\n"
684 #endif
685                "    vmalloc : 0x%08lx - 0x%08lx   (%4ld MB)\n"
686                "    lowmem  : 0x%08lx - 0x%08lx   (%4ld MB)\n"
687                "      .init : 0x%08lx - 0x%08lx   (%4ld kB)\n"
688                "      .data : 0x%08lx - 0x%08lx   (%4ld kB)\n"
689                "      .text : 0x%08lx - 0x%08lx   (%4ld kB)\n",
690                FIXADDR_START, FIXADDR_TOP,
691                (FIXADDR_TOP - FIXADDR_START) >> 10,
692
693 #ifdef CONFIG_HIGHMEM
694                PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE,
695                (LAST_PKMAP*PAGE_SIZE) >> 10,
696 #endif
697
698                VMALLOC_START, VMALLOC_END,
699                (VMALLOC_END - VMALLOC_START) >> 20,
700
701                (unsigned long)__va(0), (unsigned long)high_memory,
702                ((unsigned long)high_memory - (unsigned long)__va(0)) >> 20,
703
704                (unsigned long)&__init_begin, (unsigned long)&__init_end,
705                ((unsigned long)&__init_end - (unsigned long)&__init_begin) >> 10,
706
707                (unsigned long)&_etext, (unsigned long)&_edata,
708                ((unsigned long)&_edata - (unsigned long)&_etext) >> 10,
709
710                (unsigned long)&_text, (unsigned long)&_etext,
711                ((unsigned long)&_etext - (unsigned long)&_text) >> 10);
712
713 #ifdef CONFIG_HIGHMEM
714         BUG_ON(PKMAP_BASE+LAST_PKMAP*PAGE_SIZE > FIXADDR_START);
715         BUG_ON(VMALLOC_END                     > PKMAP_BASE);
716 #endif
717         BUG_ON(VMALLOC_START                   > VMALLOC_END);
718         BUG_ON((unsigned long)high_memory      > VMALLOC_START);
719 #endif /* double-sanity-check paranoia */
720
721 #ifdef CONFIG_X86_PAE
722         if (!cpu_has_pae)
723                 panic("cannot execute a PAE-enabled kernel on a PAE-less CPU!");
724 #endif
725         if (boot_cpu_data.wp_works_ok < 0)
726                 test_wp_bit();
727
728         /*
729          * Subtle. SMP is doing it's boot stuff late (because it has to
730          * fork idle threads) - but it also needs low mappings for the
731          * protected-mode entry to work. We zap these entries only after
732          * the WP-bit has been tested.
733          */
734 #ifndef CONFIG_SMP
735         zap_low_mappings();
736 #endif
737 }
738
739 #ifdef CONFIG_MEMORY_HOTPLUG
740 int arch_add_memory(int nid, u64 start, u64 size)
741 {
742         struct pglist_data *pgdata = NODE_DATA(nid);
743         struct zone *zone = pgdata->node_zones + ZONE_HIGHMEM;
744         unsigned long start_pfn = start >> PAGE_SHIFT;
745         unsigned long nr_pages = size >> PAGE_SHIFT;
746
747         return __add_pages(zone, start_pfn, nr_pages);
748 }
749
750 #endif
751
752 struct kmem_cache *pmd_cache;
753
754 void __init pgtable_cache_init(void)
755 {
756         if (PTRS_PER_PMD > 1)
757                 pmd_cache = kmem_cache_create("pmd",
758                                               PTRS_PER_PMD*sizeof(pmd_t),
759                                               PTRS_PER_PMD*sizeof(pmd_t),
760                                               SLAB_PANIC,
761                                               pmd_ctor);
762 }
763
764 /*
765  * This function cannot be __init, since exceptions don't work in that
766  * section.  Put this after the callers, so that it cannot be inlined.
767  */
768 static int noinline do_test_wp_bit(void)
769 {
770         char tmp_reg;
771         int flag;
772
773         __asm__ __volatile__(
774                 "       movb %0,%1      \n"
775                 "1:     movb %1,%0      \n"
776                 "       xorl %2,%2      \n"
777                 "2:                     \n"
778                 ".section __ex_table,\"a\"\n"
779                 "       .align 4        \n"
780                 "       .long 1b,2b     \n"
781                 ".previous              \n"
782                 :"=m" (*(char *)fix_to_virt(FIX_WP_TEST)),
783                  "=q" (tmp_reg),
784                  "=r" (flag)
785                 :"2" (1)
786                 :"memory");
787         
788         return flag;
789 }
790
791 #ifdef CONFIG_DEBUG_RODATA
792
793 void mark_rodata_ro(void)
794 {
795         unsigned long start = PFN_ALIGN(_text);
796         unsigned long size = PFN_ALIGN(_etext) - start;
797
798 #ifndef CONFIG_KPROBES
799 #ifdef CONFIG_HOTPLUG_CPU
800         /* It must still be possible to apply SMP alternatives. */
801         if (num_possible_cpus() <= 1)
802 #endif
803         {
804                 change_page_attr(virt_to_page(start),
805                                  size >> PAGE_SHIFT, PAGE_KERNEL_RX);
806                 printk("Write protecting the kernel text: %luk\n", size >> 10);
807         }
808 #endif
809         start += size;
810         size = (unsigned long)__end_rodata - start;
811         change_page_attr(virt_to_page(start),
812                          size >> PAGE_SHIFT, PAGE_KERNEL_RO);
813         printk("Write protecting the kernel read-only data: %luk\n",
814                size >> 10);
815
816         /*
817          * change_page_attr() requires a global_flush_tlb() call after it.
818          * We do this after the printk so that if something went wrong in the
819          * change, the printk gets out at least to give a better debug hint
820          * of who is the culprit.
821          */
822         global_flush_tlb();
823 }
824 #endif
825
826 void free_init_pages(char *what, unsigned long begin, unsigned long end)
827 {
828         unsigned long addr;
829
830         for (addr = begin; addr < end; addr += PAGE_SIZE) {
831                 ClearPageReserved(virt_to_page(addr));
832                 init_page_count(virt_to_page(addr));
833                 memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
834                 free_page(addr);
835                 totalram_pages++;
836         }
837         printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
838 }
839
840 void free_initmem(void)
841 {
842         free_init_pages("unused kernel memory",
843                         (unsigned long)(&__init_begin),
844                         (unsigned long)(&__init_end));
845 }
846
847 #ifdef CONFIG_BLK_DEV_INITRD
848 void free_initrd_mem(unsigned long start, unsigned long end)
849 {
850         free_init_pages("initrd memory", start, end);
851 }
852 #endif
853