]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/x86/mm/init_32.c
x86: move init_memory_mapping() to common mm/init.c
[linux-2.6-omap-h63xx.git] / arch / x86 / mm / init_32.c
1 /*
2  *
3  *  Copyright (C) 1995  Linus Torvalds
4  *
5  *  Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
6  */
7
8 #include <linux/module.h>
9 #include <linux/signal.h>
10 #include <linux/sched.h>
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/string.h>
14 #include <linux/types.h>
15 #include <linux/ptrace.h>
16 #include <linux/mman.h>
17 #include <linux/mm.h>
18 #include <linux/hugetlb.h>
19 #include <linux/swap.h>
20 #include <linux/smp.h>
21 #include <linux/init.h>
22 #include <linux/highmem.h>
23 #include <linux/pagemap.h>
24 #include <linux/pci.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/memory_hotplug.h>
31 #include <linux/initrd.h>
32 #include <linux/cpumask.h>
33
34 #include <asm/asm.h>
35 #include <asm/bios_ebda.h>
36 #include <asm/processor.h>
37 #include <asm/system.h>
38 #include <asm/uaccess.h>
39 #include <asm/pgtable.h>
40 #include <asm/dma.h>
41 #include <asm/fixmap.h>
42 #include <asm/e820.h>
43 #include <asm/apic.h>
44 #include <asm/bugs.h>
45 #include <asm/tlb.h>
46 #include <asm/tlbflush.h>
47 #include <asm/pgalloc.h>
48 #include <asm/sections.h>
49 #include <asm/paravirt.h>
50 #include <asm/setup.h>
51 #include <asm/cacheflush.h>
52
53 unsigned long max_low_pfn_mapped;
54 unsigned long max_pfn_mapped;
55
56 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
57 unsigned long highstart_pfn, highend_pfn;
58
59 static noinline int do_test_wp_bit(void);
60
61
62 extern unsigned long __initdata table_start;
63 extern unsigned long __meminitdata table_end;
64 extern unsigned long __meminitdata table_top;
65
66 static __init void *alloc_low_page(void)
67 {
68         unsigned long pfn = table_end++;
69         void *adr;
70
71         if (pfn >= table_top)
72                 panic("alloc_low_page: ran out of memory");
73
74         adr = __va(pfn * PAGE_SIZE);
75         memset(adr, 0, PAGE_SIZE);
76         return adr;
77 }
78
79 /*
80  * Creates a middle page table and puts a pointer to it in the
81  * given global directory entry. This only returns the gd entry
82  * in non-PAE compilation mode, since the middle layer is folded.
83  */
84 static pmd_t * __init one_md_table_init(pgd_t *pgd)
85 {
86         pud_t *pud;
87         pmd_t *pmd_table;
88
89 #ifdef CONFIG_X86_PAE
90         if (!(pgd_val(*pgd) & _PAGE_PRESENT)) {
91                 if (after_bootmem)
92                         pmd_table = (pmd_t *)alloc_bootmem_low_pages(PAGE_SIZE);
93                 else
94                         pmd_table = (pmd_t *)alloc_low_page();
95                 paravirt_alloc_pmd(&init_mm, __pa(pmd_table) >> PAGE_SHIFT);
96                 set_pgd(pgd, __pgd(__pa(pmd_table) | _PAGE_PRESENT));
97                 pud = pud_offset(pgd, 0);
98                 BUG_ON(pmd_table != pmd_offset(pud, 0));
99
100                 return pmd_table;
101         }
102 #endif
103         pud = pud_offset(pgd, 0);
104         pmd_table = pmd_offset(pud, 0);
105
106         return pmd_table;
107 }
108
109 /*
110  * Create a page table and place a pointer to it in a middle page
111  * directory entry:
112  */
113 static pte_t * __init one_page_table_init(pmd_t *pmd)
114 {
115         if (!(pmd_val(*pmd) & _PAGE_PRESENT)) {
116                 pte_t *page_table = NULL;
117
118                 if (after_bootmem) {
119 #ifdef CONFIG_DEBUG_PAGEALLOC
120                         page_table = (pte_t *) alloc_bootmem_pages(PAGE_SIZE);
121 #endif
122                         if (!page_table)
123                                 page_table =
124                                 (pte_t *)alloc_bootmem_low_pages(PAGE_SIZE);
125                 } else
126                         page_table = (pte_t *)alloc_low_page();
127
128                 paravirt_alloc_pte(&init_mm, __pa(page_table) >> PAGE_SHIFT);
129                 set_pmd(pmd, __pmd(__pa(page_table) | _PAGE_TABLE));
130                 BUG_ON(page_table != pte_offset_kernel(pmd, 0));
131         }
132
133         return pte_offset_kernel(pmd, 0);
134 }
135
136 static pte_t *__init page_table_kmap_check(pte_t *pte, pmd_t *pmd,
137                                            unsigned long vaddr, pte_t *lastpte)
138 {
139 #ifdef CONFIG_HIGHMEM
140         /*
141          * Something (early fixmap) may already have put a pte
142          * page here, which causes the page table allocation
143          * to become nonlinear. Attempt to fix it, and if it
144          * is still nonlinear then we have to bug.
145          */
146         int pmd_idx_kmap_begin = fix_to_virt(FIX_KMAP_END) >> PMD_SHIFT;
147         int pmd_idx_kmap_end = fix_to_virt(FIX_KMAP_BEGIN) >> PMD_SHIFT;
148
149         if (pmd_idx_kmap_begin != pmd_idx_kmap_end
150             && (vaddr >> PMD_SHIFT) >= pmd_idx_kmap_begin
151             && (vaddr >> PMD_SHIFT) <= pmd_idx_kmap_end
152             && ((__pa(pte) >> PAGE_SHIFT) < table_start
153                 || (__pa(pte) >> PAGE_SHIFT) >= table_end)) {
154                 pte_t *newpte;
155                 int i;
156
157                 BUG_ON(after_bootmem);
158                 newpte = alloc_low_page();
159                 for (i = 0; i < PTRS_PER_PTE; i++)
160                         set_pte(newpte + i, pte[i]);
161
162                 paravirt_alloc_pte(&init_mm, __pa(newpte) >> PAGE_SHIFT);
163                 set_pmd(pmd, __pmd(__pa(newpte)|_PAGE_TABLE));
164                 BUG_ON(newpte != pte_offset_kernel(pmd, 0));
165                 __flush_tlb_all();
166
167                 paravirt_release_pte(__pa(pte) >> PAGE_SHIFT);
168                 pte = newpte;
169         }
170         BUG_ON(vaddr < fix_to_virt(FIX_KMAP_BEGIN - 1)
171                && vaddr > fix_to_virt(FIX_KMAP_END)
172                && lastpte && lastpte + PTRS_PER_PTE != pte);
173 #endif
174         return pte;
175 }
176
177 /*
178  * This function initializes a certain range of kernel virtual memory
179  * with new bootmem page tables, everywhere page tables are missing in
180  * the given range.
181  *
182  * NOTE: The pagetables are allocated contiguous on the physical space
183  * so we can cache the place of the first one and move around without
184  * checking the pgd every time.
185  */
186 static void __init
187 page_table_range_init(unsigned long start, unsigned long end, pgd_t *pgd_base)
188 {
189         int pgd_idx, pmd_idx;
190         unsigned long vaddr;
191         pgd_t *pgd;
192         pmd_t *pmd;
193         pte_t *pte = NULL;
194
195         vaddr = start;
196         pgd_idx = pgd_index(vaddr);
197         pmd_idx = pmd_index(vaddr);
198         pgd = pgd_base + pgd_idx;
199
200         for ( ; (pgd_idx < PTRS_PER_PGD) && (vaddr != end); pgd++, pgd_idx++) {
201                 pmd = one_md_table_init(pgd);
202                 pmd = pmd + pmd_index(vaddr);
203                 for (; (pmd_idx < PTRS_PER_PMD) && (vaddr != end);
204                                                         pmd++, pmd_idx++) {
205                         pte = page_table_kmap_check(one_page_table_init(pmd),
206                                                     pmd, vaddr, pte);
207
208                         vaddr += PMD_SIZE;
209                 }
210                 pmd_idx = 0;
211         }
212 }
213
214 static inline int is_kernel_text(unsigned long addr)
215 {
216         if (addr >= PAGE_OFFSET && addr <= (unsigned long)__init_end)
217                 return 1;
218         return 0;
219 }
220
221 /*
222  * This maps the physical memory to kernel virtual address space, a total
223  * of max_low_pfn pages, by creating page tables starting from address
224  * PAGE_OFFSET:
225  */
226 void __init kernel_physical_mapping_init(unsigned long start_pfn,
227                                          unsigned long end_pfn,
228                                          int use_pse)
229 {
230         pgd_t *pgd_base = swapper_pg_dir;
231         int pgd_idx, pmd_idx, pte_ofs;
232         unsigned long pfn;
233         pgd_t *pgd;
234         pmd_t *pmd;
235         pte_t *pte;
236         unsigned pages_2m, pages_4k;
237         int mapping_iter;
238
239         /*
240          * First iteration will setup identity mapping using large/small pages
241          * based on use_pse, with other attributes same as set by
242          * the early code in head_32.S
243          *
244          * Second iteration will setup the appropriate attributes (NX, GLOBAL..)
245          * as desired for the kernel identity mapping.
246          *
247          * This two pass mechanism conforms to the TLB app note which says:
248          *
249          *     "Software should not write to a paging-structure entry in a way
250          *      that would change, for any linear address, both the page size
251          *      and either the page frame or attributes."
252          */
253         mapping_iter = 1;
254
255         if (!cpu_has_pse)
256                 use_pse = 0;
257
258 repeat:
259         pages_2m = pages_4k = 0;
260         pfn = start_pfn;
261         pgd_idx = pgd_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
262         pgd = pgd_base + pgd_idx;
263         for (; pgd_idx < PTRS_PER_PGD; pgd++, pgd_idx++) {
264                 pmd = one_md_table_init(pgd);
265
266                 if (pfn >= end_pfn)
267                         continue;
268 #ifdef CONFIG_X86_PAE
269                 pmd_idx = pmd_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
270                 pmd += pmd_idx;
271 #else
272                 pmd_idx = 0;
273 #endif
274                 for (; pmd_idx < PTRS_PER_PMD && pfn < end_pfn;
275                      pmd++, pmd_idx++) {
276                         unsigned int addr = pfn * PAGE_SIZE + PAGE_OFFSET;
277
278                         /*
279                          * Map with big pages if possible, otherwise
280                          * create normal page tables:
281                          */
282                         if (use_pse) {
283                                 unsigned int addr2;
284                                 pgprot_t prot = PAGE_KERNEL_LARGE;
285                                 /*
286                                  * first pass will use the same initial
287                                  * identity mapping attribute + _PAGE_PSE.
288                                  */
289                                 pgprot_t init_prot =
290                                         __pgprot(PTE_IDENT_ATTR |
291                                                  _PAGE_PSE);
292
293                                 addr2 = (pfn + PTRS_PER_PTE-1) * PAGE_SIZE +
294                                         PAGE_OFFSET + PAGE_SIZE-1;
295
296                                 if (is_kernel_text(addr) ||
297                                     is_kernel_text(addr2))
298                                         prot = PAGE_KERNEL_LARGE_EXEC;
299
300                                 pages_2m++;
301                                 if (mapping_iter == 1)
302                                         set_pmd(pmd, pfn_pmd(pfn, init_prot));
303                                 else
304                                         set_pmd(pmd, pfn_pmd(pfn, prot));
305
306                                 pfn += PTRS_PER_PTE;
307                                 continue;
308                         }
309                         pte = one_page_table_init(pmd);
310
311                         pte_ofs = pte_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
312                         pte += pte_ofs;
313                         for (; pte_ofs < PTRS_PER_PTE && pfn < end_pfn;
314                              pte++, pfn++, pte_ofs++, addr += PAGE_SIZE) {
315                                 pgprot_t prot = PAGE_KERNEL;
316                                 /*
317                                  * first pass will use the same initial
318                                  * identity mapping attribute.
319                                  */
320                                 pgprot_t init_prot = __pgprot(PTE_IDENT_ATTR);
321
322                                 if (is_kernel_text(addr))
323                                         prot = PAGE_KERNEL_EXEC;
324
325                                 pages_4k++;
326                                 if (mapping_iter == 1)
327                                         set_pte(pte, pfn_pte(pfn, init_prot));
328                                 else
329                                         set_pte(pte, pfn_pte(pfn, prot));
330                         }
331                 }
332         }
333         if (mapping_iter == 1) {
334                 /*
335                  * update direct mapping page count only in the first
336                  * iteration.
337                  */
338                 update_page_count(PG_LEVEL_2M, pages_2m);
339                 update_page_count(PG_LEVEL_4K, pages_4k);
340
341                 /*
342                  * local global flush tlb, which will flush the previous
343                  * mappings present in both small and large page TLB's.
344                  */
345                 __flush_tlb_all();
346
347                 /*
348                  * Second iteration will set the actual desired PTE attributes.
349                  */
350                 mapping_iter = 2;
351                 goto repeat;
352         }
353 }
354
355 pte_t *kmap_pte;
356 pgprot_t kmap_prot;
357
358 static inline pte_t *kmap_get_fixmap_pte(unsigned long vaddr)
359 {
360         return pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr),
361                         vaddr), vaddr), vaddr);
362 }
363
364 static void __init kmap_init(void)
365 {
366         unsigned long kmap_vstart;
367
368         /*
369          * Cache the first kmap pte:
370          */
371         kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
372         kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
373
374         kmap_prot = PAGE_KERNEL;
375 }
376
377 #ifdef CONFIG_HIGHMEM
378 static void __init permanent_kmaps_init(pgd_t *pgd_base)
379 {
380         unsigned long vaddr;
381         pgd_t *pgd;
382         pud_t *pud;
383         pmd_t *pmd;
384         pte_t *pte;
385
386         vaddr = PKMAP_BASE;
387         page_table_range_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base);
388
389         pgd = swapper_pg_dir + pgd_index(vaddr);
390         pud = pud_offset(pgd, vaddr);
391         pmd = pmd_offset(pud, vaddr);
392         pte = pte_offset_kernel(pmd, vaddr);
393         pkmap_page_table = pte;
394 }
395
396 static void __init add_one_highpage_init(struct page *page, int pfn)
397 {
398         ClearPageReserved(page);
399         init_page_count(page);
400         __free_page(page);
401         totalhigh_pages++;
402 }
403
404 struct add_highpages_data {
405         unsigned long start_pfn;
406         unsigned long end_pfn;
407 };
408
409 static int __init add_highpages_work_fn(unsigned long start_pfn,
410                                          unsigned long end_pfn, void *datax)
411 {
412         int node_pfn;
413         struct page *page;
414         unsigned long final_start_pfn, final_end_pfn;
415         struct add_highpages_data *data;
416
417         data = (struct add_highpages_data *)datax;
418
419         final_start_pfn = max(start_pfn, data->start_pfn);
420         final_end_pfn = min(end_pfn, data->end_pfn);
421         if (final_start_pfn >= final_end_pfn)
422                 return 0;
423
424         for (node_pfn = final_start_pfn; node_pfn < final_end_pfn;
425              node_pfn++) {
426                 if (!pfn_valid(node_pfn))
427                         continue;
428                 page = pfn_to_page(node_pfn);
429                 add_one_highpage_init(page, node_pfn);
430         }
431
432         return 0;
433
434 }
435
436 void __init add_highpages_with_active_regions(int nid, unsigned long start_pfn,
437                                               unsigned long end_pfn)
438 {
439         struct add_highpages_data data;
440
441         data.start_pfn = start_pfn;
442         data.end_pfn = end_pfn;
443
444         work_with_active_regions(nid, add_highpages_work_fn, &data);
445 }
446
447 #else
448 static inline void permanent_kmaps_init(pgd_t *pgd_base)
449 {
450 }
451 #endif /* CONFIG_HIGHMEM */
452
453 void __init native_pagetable_setup_start(pgd_t *base)
454 {
455         unsigned long pfn, va;
456         pgd_t *pgd;
457         pud_t *pud;
458         pmd_t *pmd;
459         pte_t *pte;
460
461         /*
462          * Remove any mappings which extend past the end of physical
463          * memory from the boot time page table:
464          */
465         for (pfn = max_low_pfn + 1; pfn < 1<<(32-PAGE_SHIFT); pfn++) {
466                 va = PAGE_OFFSET + (pfn<<PAGE_SHIFT);
467                 pgd = base + pgd_index(va);
468                 if (!pgd_present(*pgd))
469                         break;
470
471                 pud = pud_offset(pgd, va);
472                 pmd = pmd_offset(pud, va);
473                 if (!pmd_present(*pmd))
474                         break;
475
476                 pte = pte_offset_kernel(pmd, va);
477                 if (!pte_present(*pte))
478                         break;
479
480                 pte_clear(NULL, va, pte);
481         }
482         paravirt_alloc_pmd(&init_mm, __pa(base) >> PAGE_SHIFT);
483 }
484
485 void __init native_pagetable_setup_done(pgd_t *base)
486 {
487 }
488
489 /*
490  * Build a proper pagetable for the kernel mappings.  Up until this
491  * point, we've been running on some set of pagetables constructed by
492  * the boot process.
493  *
494  * If we're booting on native hardware, this will be a pagetable
495  * constructed in arch/x86/kernel/head_32.S.  The root of the
496  * pagetable will be swapper_pg_dir.
497  *
498  * If we're booting paravirtualized under a hypervisor, then there are
499  * more options: we may already be running PAE, and the pagetable may
500  * or may not be based in swapper_pg_dir.  In any case,
501  * paravirt_pagetable_setup_start() will set up swapper_pg_dir
502  * appropriately for the rest of the initialization to work.
503  *
504  * In general, pagetable_init() assumes that the pagetable may already
505  * be partially populated, and so it avoids stomping on any existing
506  * mappings.
507  */
508 void __init early_ioremap_page_table_range_init(void)
509 {
510         pgd_t *pgd_base = swapper_pg_dir;
511         unsigned long vaddr, end;
512
513         /*
514          * Fixed mappings, only the page table structure has to be
515          * created - mappings will be set by set_fixmap():
516          */
517         vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
518         end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK;
519         page_table_range_init(vaddr, end, pgd_base);
520         early_ioremap_reset();
521 }
522
523 static void __init pagetable_init(void)
524 {
525         pgd_t *pgd_base = swapper_pg_dir;
526
527         permanent_kmaps_init(pgd_base);
528 }
529
530 #ifdef CONFIG_ACPI_SLEEP
531 /*
532  * ACPI suspend needs this for resume, because things like the intel-agp
533  * driver might have split up a kernel 4MB mapping.
534  */
535 char swsusp_pg_dir[PAGE_SIZE]
536         __attribute__ ((aligned(PAGE_SIZE)));
537
538 static inline void save_pg_dir(void)
539 {
540         memcpy(swsusp_pg_dir, swapper_pg_dir, PAGE_SIZE);
541 }
542 #else /* !CONFIG_ACPI_SLEEP */
543 static inline void save_pg_dir(void)
544 {
545 }
546 #endif /* !CONFIG_ACPI_SLEEP */
547
548 void zap_low_mappings(void)
549 {
550         int i;
551
552         /*
553          * Zap initial low-memory mappings.
554          *
555          * Note that "pgd_clear()" doesn't do it for
556          * us, because pgd_clear() is a no-op on i386.
557          */
558         for (i = 0; i < KERNEL_PGD_BOUNDARY; i++) {
559 #ifdef CONFIG_X86_PAE
560                 set_pgd(swapper_pg_dir+i, __pgd(1 + __pa(empty_zero_page)));
561 #else
562                 set_pgd(swapper_pg_dir+i, __pgd(0));
563 #endif
564         }
565         flush_tlb_all();
566 }
567
568 int nx_enabled;
569
570 pteval_t __supported_pte_mask __read_mostly = ~(_PAGE_NX | _PAGE_GLOBAL | _PAGE_IOMAP);
571 EXPORT_SYMBOL_GPL(__supported_pte_mask);
572
573 #ifdef CONFIG_X86_PAE
574
575 static int disable_nx __initdata;
576
577 /*
578  * noexec = on|off
579  *
580  * Control non executable mappings.
581  *
582  * on      Enable
583  * off     Disable
584  */
585 static int __init noexec_setup(char *str)
586 {
587         if (!str || !strcmp(str, "on")) {
588                 if (cpu_has_nx) {
589                         __supported_pte_mask |= _PAGE_NX;
590                         disable_nx = 0;
591                 }
592         } else {
593                 if (!strcmp(str, "off")) {
594                         disable_nx = 1;
595                         __supported_pte_mask &= ~_PAGE_NX;
596                 } else {
597                         return -EINVAL;
598                 }
599         }
600
601         return 0;
602 }
603 early_param("noexec", noexec_setup);
604
605 static void __init set_nx(void)
606 {
607         unsigned int v[4], l, h;
608
609         if (cpu_has_pae && (cpuid_eax(0x80000000) > 0x80000001)) {
610                 cpuid(0x80000001, &v[0], &v[1], &v[2], &v[3]);
611
612                 if ((v[3] & (1 << 20)) && !disable_nx) {
613                         rdmsr(MSR_EFER, l, h);
614                         l |= EFER_NX;
615                         wrmsr(MSR_EFER, l, h);
616                         nx_enabled = 1;
617                         __supported_pte_mask |= _PAGE_NX;
618                 }
619         }
620 }
621 #endif
622
623 /* user-defined highmem size */
624 static unsigned int highmem_pages = -1;
625
626 /*
627  * highmem=size forces highmem to be exactly 'size' bytes.
628  * This works even on boxes that have no highmem otherwise.
629  * This also works to reduce highmem size on bigger boxes.
630  */
631 static int __init parse_highmem(char *arg)
632 {
633         if (!arg)
634                 return -EINVAL;
635
636         highmem_pages = memparse(arg, &arg) >> PAGE_SHIFT;
637         return 0;
638 }
639 early_param("highmem", parse_highmem);
640
641 #define MSG_HIGHMEM_TOO_BIG \
642         "highmem size (%luMB) is bigger than pages available (%luMB)!\n"
643
644 #define MSG_LOWMEM_TOO_SMALL \
645         "highmem size (%luMB) results in <64MB lowmem, ignoring it!\n"
646 /*
647  * All of RAM fits into lowmem - but if user wants highmem
648  * artificially via the highmem=x boot parameter then create
649  * it:
650  */
651 void __init lowmem_pfn_init(void)
652 {
653         /* max_low_pfn is 0, we already have early_res support */
654         max_low_pfn = max_pfn;
655
656         if (highmem_pages == -1)
657                 highmem_pages = 0;
658 #ifdef CONFIG_HIGHMEM
659         if (highmem_pages >= max_pfn) {
660                 printk(KERN_ERR MSG_HIGHMEM_TOO_BIG,
661                         pages_to_mb(highmem_pages), pages_to_mb(max_pfn));
662                 highmem_pages = 0;
663         }
664         if (highmem_pages) {
665                 if (max_low_pfn - highmem_pages < 64*1024*1024/PAGE_SIZE) {
666                         printk(KERN_ERR MSG_LOWMEM_TOO_SMALL,
667                                 pages_to_mb(highmem_pages));
668                         highmem_pages = 0;
669                 }
670                 max_low_pfn -= highmem_pages;
671         }
672 #else
673         if (highmem_pages)
674                 printk(KERN_ERR "ignoring highmem size on non-highmem kernel!\n");
675 #endif
676 }
677
678 #define MSG_HIGHMEM_TOO_SMALL \
679         "only %luMB highmem pages available, ignoring highmem size of %luMB!\n"
680
681 #define MSG_HIGHMEM_TRIMMED \
682         "Warning: only 4GB will be used. Use a HIGHMEM64G enabled kernel!\n"
683 /*
684  * We have more RAM than fits into lowmem - we try to put it into
685  * highmem, also taking the highmem=x boot parameter into account:
686  */
687 void __init highmem_pfn_init(void)
688 {
689         max_low_pfn = MAXMEM_PFN;
690
691         if (highmem_pages == -1)
692                 highmem_pages = max_pfn - MAXMEM_PFN;
693
694         if (highmem_pages + MAXMEM_PFN < max_pfn)
695                 max_pfn = MAXMEM_PFN + highmem_pages;
696
697         if (highmem_pages + MAXMEM_PFN > max_pfn) {
698                 printk(KERN_WARNING MSG_HIGHMEM_TOO_SMALL,
699                         pages_to_mb(max_pfn - MAXMEM_PFN),
700                         pages_to_mb(highmem_pages));
701                 highmem_pages = 0;
702         }
703 #ifndef CONFIG_HIGHMEM
704         /* Maximum memory usable is what is directly addressable */
705         printk(KERN_WARNING "Warning only %ldMB will be used.\n", MAXMEM>>20);
706         if (max_pfn > MAX_NONPAE_PFN)
707                 printk(KERN_WARNING "Use a HIGHMEM64G enabled kernel.\n");
708         else
709                 printk(KERN_WARNING "Use a HIGHMEM enabled kernel.\n");
710         max_pfn = MAXMEM_PFN;
711 #else /* !CONFIG_HIGHMEM */
712 #ifndef CONFIG_HIGHMEM64G
713         if (max_pfn > MAX_NONPAE_PFN) {
714                 max_pfn = MAX_NONPAE_PFN;
715                 printk(KERN_WARNING MSG_HIGHMEM_TRIMMED);
716         }
717 #endif /* !CONFIG_HIGHMEM64G */
718 #endif /* !CONFIG_HIGHMEM */
719 }
720
721 /*
722  * Determine low and high memory ranges:
723  */
724 void __init find_low_pfn_range(void)
725 {
726         /* it could update max_pfn */
727
728         if (max_pfn <= MAXMEM_PFN)
729                 lowmem_pfn_init();
730         else
731                 highmem_pfn_init();
732 }
733
734 #ifndef CONFIG_NEED_MULTIPLE_NODES
735 void __init initmem_init(unsigned long start_pfn,
736                                   unsigned long end_pfn)
737 {
738 #ifdef CONFIG_HIGHMEM
739         highstart_pfn = highend_pfn = max_pfn;
740         if (max_pfn > max_low_pfn)
741                 highstart_pfn = max_low_pfn;
742         memory_present(0, 0, highend_pfn);
743         e820_register_active_regions(0, 0, highend_pfn);
744         printk(KERN_NOTICE "%ldMB HIGHMEM available.\n",
745                 pages_to_mb(highend_pfn - highstart_pfn));
746         num_physpages = highend_pfn;
747         high_memory = (void *) __va(highstart_pfn * PAGE_SIZE - 1) + 1;
748 #else
749         memory_present(0, 0, max_low_pfn);
750         e820_register_active_regions(0, 0, max_low_pfn);
751         num_physpages = max_low_pfn;
752         high_memory = (void *) __va(max_low_pfn * PAGE_SIZE - 1) + 1;
753 #endif
754 #ifdef CONFIG_FLATMEM
755         max_mapnr = num_physpages;
756 #endif
757         printk(KERN_NOTICE "%ldMB LOWMEM available.\n",
758                         pages_to_mb(max_low_pfn));
759
760         setup_bootmem_allocator();
761 }
762 #endif /* !CONFIG_NEED_MULTIPLE_NODES */
763
764 static void __init zone_sizes_init(void)
765 {
766         unsigned long max_zone_pfns[MAX_NR_ZONES];
767         memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
768         max_zone_pfns[ZONE_DMA] =
769                 virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
770         max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
771 #ifdef CONFIG_HIGHMEM
772         max_zone_pfns[ZONE_HIGHMEM] = highend_pfn;
773 #endif
774
775         free_area_init_nodes(max_zone_pfns);
776 }
777
778 static unsigned long __init setup_node_bootmem(int nodeid,
779                                  unsigned long start_pfn,
780                                  unsigned long end_pfn,
781                                  unsigned long bootmap)
782 {
783         unsigned long bootmap_size;
784
785         if (start_pfn > max_low_pfn)
786                 return bootmap;
787         if (end_pfn > max_low_pfn)
788                 end_pfn = max_low_pfn;
789
790         /* don't touch min_low_pfn */
791         bootmap_size = init_bootmem_node(NODE_DATA(nodeid),
792                                          bootmap >> PAGE_SHIFT,
793                                          start_pfn, end_pfn);
794         printk(KERN_INFO "  node %d low ram: %08lx - %08lx\n",
795                 nodeid, start_pfn<<PAGE_SHIFT, end_pfn<<PAGE_SHIFT);
796         printk(KERN_INFO "  node %d bootmap %08lx - %08lx\n",
797                  nodeid, bootmap, bootmap + bootmap_size);
798         free_bootmem_with_active_regions(nodeid, end_pfn);
799         early_res_to_bootmem(start_pfn<<PAGE_SHIFT, end_pfn<<PAGE_SHIFT);
800
801         return bootmap + bootmap_size;
802 }
803
804 void __init setup_bootmem_allocator(void)
805 {
806         int nodeid;
807         unsigned long bootmap_size, bootmap;
808         /*
809          * Initialize the boot-time allocator (with low memory only):
810          */
811         bootmap_size = bootmem_bootmap_pages(max_low_pfn)<<PAGE_SHIFT;
812         bootmap = find_e820_area(0, max_pfn_mapped<<PAGE_SHIFT, bootmap_size,
813                                  PAGE_SIZE);
814         if (bootmap == -1L)
815                 panic("Cannot find bootmem map of size %ld\n", bootmap_size);
816         reserve_early(bootmap, bootmap + bootmap_size, "BOOTMAP");
817
818         printk(KERN_INFO "  mapped low ram: 0 - %08lx\n",
819                  max_pfn_mapped<<PAGE_SHIFT);
820         printk(KERN_INFO "  low ram: 0 - %08lx\n", max_low_pfn<<PAGE_SHIFT);
821
822 #ifdef CONFIG_NEED_MULTIPLE_NODES
823         for_each_online_node(nodeid)
824                 bootmap = setup_node_bootmem(nodeid, node_start_pfn[nodeid],
825                                         node_end_pfn[nodeid], bootmap);
826 #else
827         bootmap = setup_node_bootmem(0, 0, max_low_pfn, bootmap);
828 #endif
829
830         after_bootmem = 1;
831 }
832
833 /*
834  * paging_init() sets up the page tables - note that the first 8MB are
835  * already mapped by head.S.
836  *
837  * This routines also unmaps the page at virtual kernel address 0, so
838  * that we can trap those pesky NULL-reference errors in the kernel.
839  */
840 void __init paging_init(void)
841 {
842         pagetable_init();
843
844         __flush_tlb_all();
845
846         kmap_init();
847
848         /*
849          * NOTE: at this point the bootmem allocator is fully available.
850          */
851         sparse_init();
852         zone_sizes_init();
853 }
854
855 /*
856  * Test if the WP bit works in supervisor mode. It isn't supported on 386's
857  * and also on some strange 486's. All 586+'s are OK. This used to involve
858  * black magic jumps to work around some nasty CPU bugs, but fortunately the
859  * switch to using exceptions got rid of all that.
860  */
861 static void __init test_wp_bit(void)
862 {
863         printk(KERN_INFO
864   "Checking if this processor honours the WP bit even in supervisor mode...");
865
866         /* Any page-aligned address will do, the test is non-destructive */
867         __set_fixmap(FIX_WP_TEST, __pa(&swapper_pg_dir), PAGE_READONLY);
868         boot_cpu_data.wp_works_ok = do_test_wp_bit();
869         clear_fixmap(FIX_WP_TEST);
870
871         if (!boot_cpu_data.wp_works_ok) {
872                 printk(KERN_CONT "No.\n");
873 #ifdef CONFIG_X86_WP_WORKS_OK
874                 panic(
875   "This kernel doesn't support CPU's with broken WP. Recompile it for a 386!");
876 #endif
877         } else {
878                 printk(KERN_CONT "Ok.\n");
879         }
880 }
881
882 static struct kcore_list kcore_mem, kcore_vmalloc;
883
884 void __init mem_init(void)
885 {
886         int codesize, reservedpages, datasize, initsize;
887         int tmp;
888
889         pci_iommu_alloc();
890
891 #ifdef CONFIG_FLATMEM
892         BUG_ON(!mem_map);
893 #endif
894         /* this will put all low memory onto the freelists */
895         totalram_pages += free_all_bootmem();
896
897         reservedpages = 0;
898         for (tmp = 0; tmp < max_low_pfn; tmp++)
899                 /*
900                  * Only count reserved RAM pages:
901                  */
902                 if (page_is_ram(tmp) && PageReserved(pfn_to_page(tmp)))
903                         reservedpages++;
904
905         set_highmem_pages_init();
906
907         codesize =  (unsigned long) &_etext - (unsigned long) &_text;
908         datasize =  (unsigned long) &_edata - (unsigned long) &_etext;
909         initsize =  (unsigned long) &__init_end - (unsigned long) &__init_begin;
910
911         kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
912         kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
913                    VMALLOC_END-VMALLOC_START);
914
915         printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, "
916                         "%dk reserved, %dk data, %dk init, %ldk highmem)\n",
917                 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
918                 num_physpages << (PAGE_SHIFT-10),
919                 codesize >> 10,
920                 reservedpages << (PAGE_SHIFT-10),
921                 datasize >> 10,
922                 initsize >> 10,
923                 (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10))
924                );
925
926         printk(KERN_INFO "virtual kernel memory layout:\n"
927                 "    fixmap  : 0x%08lx - 0x%08lx   (%4ld kB)\n"
928 #ifdef CONFIG_HIGHMEM
929                 "    pkmap   : 0x%08lx - 0x%08lx   (%4ld kB)\n"
930 #endif
931                 "    vmalloc : 0x%08lx - 0x%08lx   (%4ld MB)\n"
932                 "    lowmem  : 0x%08lx - 0x%08lx   (%4ld MB)\n"
933                 "      .init : 0x%08lx - 0x%08lx   (%4ld kB)\n"
934                 "      .data : 0x%08lx - 0x%08lx   (%4ld kB)\n"
935                 "      .text : 0x%08lx - 0x%08lx   (%4ld kB)\n",
936                 FIXADDR_START, FIXADDR_TOP,
937                 (FIXADDR_TOP - FIXADDR_START) >> 10,
938
939 #ifdef CONFIG_HIGHMEM
940                 PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE,
941                 (LAST_PKMAP*PAGE_SIZE) >> 10,
942 #endif
943
944                 VMALLOC_START, VMALLOC_END,
945                 (VMALLOC_END - VMALLOC_START) >> 20,
946
947                 (unsigned long)__va(0), (unsigned long)high_memory,
948                 ((unsigned long)high_memory - (unsigned long)__va(0)) >> 20,
949
950                 (unsigned long)&__init_begin, (unsigned long)&__init_end,
951                 ((unsigned long)&__init_end -
952                  (unsigned long)&__init_begin) >> 10,
953
954                 (unsigned long)&_etext, (unsigned long)&_edata,
955                 ((unsigned long)&_edata - (unsigned long)&_etext) >> 10,
956
957                 (unsigned long)&_text, (unsigned long)&_etext,
958                 ((unsigned long)&_etext - (unsigned long)&_text) >> 10);
959
960         /*
961          * Check boundaries twice: Some fundamental inconsistencies can
962          * be detected at build time already.
963          */
964 #define __FIXADDR_TOP (-PAGE_SIZE)
965 #ifdef CONFIG_HIGHMEM
966         BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP*PAGE_SIZE  > FIXADDR_START);
967         BUILD_BUG_ON(VMALLOC_END                        > PKMAP_BASE);
968 #endif
969 #define high_memory (-128UL << 20)
970         BUILD_BUG_ON(VMALLOC_START                      >= VMALLOC_END);
971 #undef high_memory
972 #undef __FIXADDR_TOP
973
974 #ifdef CONFIG_HIGHMEM
975         BUG_ON(PKMAP_BASE + LAST_PKMAP*PAGE_SIZE        > FIXADDR_START);
976         BUG_ON(VMALLOC_END                              > PKMAP_BASE);
977 #endif
978         BUG_ON(VMALLOC_START                            >= VMALLOC_END);
979         BUG_ON((unsigned long)high_memory               > VMALLOC_START);
980
981         if (boot_cpu_data.wp_works_ok < 0)
982                 test_wp_bit();
983
984         save_pg_dir();
985         zap_low_mappings();
986 }
987
988 #ifdef CONFIG_MEMORY_HOTPLUG
989 int arch_add_memory(int nid, u64 start, u64 size)
990 {
991         struct pglist_data *pgdata = NODE_DATA(nid);
992         struct zone *zone = pgdata->node_zones + ZONE_HIGHMEM;
993         unsigned long start_pfn = start >> PAGE_SHIFT;
994         unsigned long nr_pages = size >> PAGE_SHIFT;
995
996         return __add_pages(nid, zone, start_pfn, nr_pages);
997 }
998 #endif
999
1000 /*
1001  * This function cannot be __init, since exceptions don't work in that
1002  * section.  Put this after the callers, so that it cannot be inlined.
1003  */
1004 static noinline int do_test_wp_bit(void)
1005 {
1006         char tmp_reg;
1007         int flag;
1008
1009         __asm__ __volatile__(
1010                 "       movb %0, %1     \n"
1011                 "1:     movb %1, %0     \n"
1012                 "       xorl %2, %2     \n"
1013                 "2:                     \n"
1014                 _ASM_EXTABLE(1b,2b)
1015                 :"=m" (*(char *)fix_to_virt(FIX_WP_TEST)),
1016                  "=q" (tmp_reg),
1017                  "=r" (flag)
1018                 :"2" (1)
1019                 :"memory");
1020
1021         return flag;
1022 }
1023
1024 #ifdef CONFIG_DEBUG_RODATA
1025 const int rodata_test_data = 0xC3;
1026 EXPORT_SYMBOL_GPL(rodata_test_data);
1027
1028 void mark_rodata_ro(void)
1029 {
1030         unsigned long start = PFN_ALIGN(_text);
1031         unsigned long size = PFN_ALIGN(_etext) - start;
1032
1033 #ifndef CONFIG_DYNAMIC_FTRACE
1034         /* Dynamic tracing modifies the kernel text section */
1035         set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
1036         printk(KERN_INFO "Write protecting the kernel text: %luk\n",
1037                 size >> 10);
1038
1039 #ifdef CONFIG_CPA_DEBUG
1040         printk(KERN_INFO "Testing CPA: Reverting %lx-%lx\n",
1041                 start, start+size);
1042         set_pages_rw(virt_to_page(start), size>>PAGE_SHIFT);
1043
1044         printk(KERN_INFO "Testing CPA: write protecting again\n");
1045         set_pages_ro(virt_to_page(start), size>>PAGE_SHIFT);
1046 #endif
1047 #endif /* CONFIG_DYNAMIC_FTRACE */
1048
1049         start += size;
1050         size = (unsigned long)__end_rodata - start;
1051         set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
1052         printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
1053                 size >> 10);
1054         rodata_test();
1055
1056 #ifdef CONFIG_CPA_DEBUG
1057         printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, start + size);
1058         set_pages_rw(virt_to_page(start), size >> PAGE_SHIFT);
1059
1060         printk(KERN_INFO "Testing CPA: write protecting again\n");
1061         set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
1062 #endif
1063 }
1064 #endif
1065
1066 int __init reserve_bootmem_generic(unsigned long phys, unsigned long len,
1067                                    int flags)
1068 {
1069         return reserve_bootmem(phys, len, flags);
1070 }