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