306049edd55322a3c5aec4c7acdf3e38e224eda6
[linux-2.6.git] / arch / x86 / mm / init_64.c
1 /*
2  *  linux/arch/x86_64/mm/init.c
3  *
4  *  Copyright (C) 1995  Linus Torvalds
5  *  Copyright (C) 2000  Pavel Machek <pavel@suse.cz>
6  *  Copyright (C) 2002,2003 Andi Kleen <ak@suse.de>
7  */
8
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/swap.h>
19 #include <linux/smp.h>
20 #include <linux/init.h>
21 #include <linux/initrd.h>
22 #include <linux/pagemap.h>
23 #include <linux/bootmem.h>
24 #include <linux/proc_fs.h>
25 #include <linux/pci.h>
26 #include <linux/pfn.h>
27 #include <linux/poison.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/module.h>
30 #include <linux/memory_hotplug.h>
31 #include <linux/nmi.h>
32
33 #include <asm/processor.h>
34 #include <asm/system.h>
35 #include <asm/uaccess.h>
36 #include <asm/pgtable.h>
37 #include <asm/pgalloc.h>
38 #include <asm/dma.h>
39 #include <asm/fixmap.h>
40 #include <asm/e820.h>
41 #include <asm/apic.h>
42 #include <asm/tlb.h>
43 #include <asm/mmu_context.h>
44 #include <asm/proto.h>
45 #include <asm/smp.h>
46 #include <asm/sections.h>
47 #include <asm/kdebug.h>
48 #include <asm/numa.h>
49 #include <asm/cacheflush.h>
50
51 /*
52  * end_pfn only includes RAM, while max_pfn_mapped includes all e820 entries.
53  * The direct mapping extends to max_pfn_mapped, so that we can directly access
54  * apertures, ACPI and other tables without having to play with fixmaps.
55  */
56 unsigned long max_low_pfn_mapped;
57 unsigned long max_pfn_mapped;
58
59 static unsigned long dma_reserve __initdata;
60
61 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
62
63 int direct_gbpages __meminitdata
64 #ifdef CONFIG_DIRECT_GBPAGES
65                                 = 1
66 #endif
67 ;
68
69 static int __init parse_direct_gbpages_off(char *arg)
70 {
71         direct_gbpages = 0;
72         return 0;
73 }
74 early_param("nogbpages", parse_direct_gbpages_off);
75
76 static int __init parse_direct_gbpages_on(char *arg)
77 {
78         direct_gbpages = 1;
79         return 0;
80 }
81 early_param("gbpages", parse_direct_gbpages_on);
82
83 /*
84  * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the
85  * physical space so we can cache the place of the first one and move
86  * around without checking the pgd every time.
87  */
88
89 void show_mem(void)
90 {
91         long i, total = 0, reserved = 0;
92         long shared = 0, cached = 0;
93         struct page *page;
94         pg_data_t *pgdat;
95
96         printk(KERN_INFO "Mem-info:\n");
97         show_free_areas();
98         for_each_online_pgdat(pgdat) {
99                 for (i = 0; i < pgdat->node_spanned_pages; ++i) {
100                         /*
101                          * This loop can take a while with 256 GB and
102                          * 4k pages so defer the NMI watchdog:
103                          */
104                         if (unlikely(i % MAX_ORDER_NR_PAGES == 0))
105                                 touch_nmi_watchdog();
106
107                         if (!pfn_valid(pgdat->node_start_pfn + i))
108                                 continue;
109
110                         page = pfn_to_page(pgdat->node_start_pfn + i);
111                         total++;
112                         if (PageReserved(page))
113                                 reserved++;
114                         else if (PageSwapCache(page))
115                                 cached++;
116                         else if (page_count(page))
117                                 shared += page_count(page) - 1;
118                 }
119         }
120         printk(KERN_INFO "%lu pages of RAM\n",          total);
121         printk(KERN_INFO "%lu reserved pages\n",        reserved);
122         printk(KERN_INFO "%lu pages shared\n",          shared);
123         printk(KERN_INFO "%lu pages swap cached\n",     cached);
124 }
125
126 int after_bootmem;
127
128 static __init void *spp_getpage(void)
129 {
130         void *ptr;
131
132         if (after_bootmem)
133                 ptr = (void *) get_zeroed_page(GFP_ATOMIC);
134         else
135                 ptr = alloc_bootmem_pages(PAGE_SIZE);
136
137         if (!ptr || ((unsigned long)ptr & ~PAGE_MASK)) {
138                 panic("set_pte_phys: cannot allocate page data %s\n",
139                         after_bootmem ? "after bootmem" : "");
140         }
141
142         pr_debug("spp_getpage %p\n", ptr);
143
144         return ptr;
145 }
146
147 void
148 set_pte_vaddr_pud(pud_t *pud_page, unsigned long vaddr, pte_t new_pte)
149 {
150         pud_t *pud;
151         pmd_t *pmd;
152         pte_t *pte;
153
154         pud = pud_page + pud_index(vaddr);
155         if (pud_none(*pud)) {
156                 pmd = (pmd_t *) spp_getpage();
157                 pud_populate(&init_mm, pud, pmd);
158                 if (pmd != pmd_offset(pud, 0)) {
159                         printk(KERN_ERR "PAGETABLE BUG #01! %p <-> %p\n",
160                                 pmd, pmd_offset(pud, 0));
161                         return;
162                 }
163         }
164         pmd = pmd_offset(pud, vaddr);
165         if (pmd_none(*pmd)) {
166                 pte = (pte_t *) spp_getpage();
167                 pmd_populate_kernel(&init_mm, pmd, pte);
168                 if (pte != pte_offset_kernel(pmd, 0)) {
169                         printk(KERN_ERR "PAGETABLE BUG #02!\n");
170                         return;
171                 }
172         }
173
174         pte = pte_offset_kernel(pmd, vaddr);
175         if (!pte_none(*pte) && pte_val(new_pte) &&
176             pte_val(*pte) != (pte_val(new_pte) & __supported_pte_mask))
177                 pte_ERROR(*pte);
178         set_pte(pte, new_pte);
179
180         /*
181          * It's enough to flush this one mapping.
182          * (PGE mappings get flushed as well)
183          */
184         __flush_tlb_one(vaddr);
185 }
186
187 void
188 set_pte_vaddr(unsigned long vaddr, pte_t pteval)
189 {
190         pgd_t *pgd;
191         pud_t *pud_page;
192
193         pr_debug("set_pte_vaddr %lx to %lx\n", vaddr, native_pte_val(pteval));
194
195         pgd = pgd_offset_k(vaddr);
196         if (pgd_none(*pgd)) {
197                 printk(KERN_ERR
198                         "PGD FIXMAP MISSING, it should be setup in head.S!\n");
199                 return;
200         }
201         pud_page = (pud_t*)pgd_page_vaddr(*pgd);
202         set_pte_vaddr_pud(pud_page, vaddr, pteval);
203 }
204
205 /*
206  * Create large page table mappings for a range of physical addresses.
207  */
208 static void __init __init_extra_mapping(unsigned long phys, unsigned long size,
209                                                 pgprot_t prot)
210 {
211         pgd_t *pgd;
212         pud_t *pud;
213         pmd_t *pmd;
214
215         BUG_ON((phys & ~PMD_MASK) || (size & ~PMD_MASK));
216         for (; size; phys += PMD_SIZE, size -= PMD_SIZE) {
217                 pgd = pgd_offset_k((unsigned long)__va(phys));
218                 if (pgd_none(*pgd)) {
219                         pud = (pud_t *) spp_getpage();
220                         set_pgd(pgd, __pgd(__pa(pud) | _KERNPG_TABLE |
221                                                 _PAGE_USER));
222                 }
223                 pud = pud_offset(pgd, (unsigned long)__va(phys));
224                 if (pud_none(*pud)) {
225                         pmd = (pmd_t *) spp_getpage();
226                         set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE |
227                                                 _PAGE_USER));
228                 }
229                 pmd = pmd_offset(pud, phys);
230                 BUG_ON(!pmd_none(*pmd));
231                 set_pmd(pmd, __pmd(phys | pgprot_val(prot)));
232         }
233 }
234
235 void __init init_extra_mapping_wb(unsigned long phys, unsigned long size)
236 {
237         __init_extra_mapping(phys, size, PAGE_KERNEL_LARGE);
238 }
239
240 void __init init_extra_mapping_uc(unsigned long phys, unsigned long size)
241 {
242         __init_extra_mapping(phys, size, PAGE_KERNEL_LARGE_NOCACHE);
243 }
244
245 /*
246  * The head.S code sets up the kernel high mapping:
247  *
248  *   from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text)
249  *
250  * phys_addr holds the negative offset to the kernel, which is added
251  * to the compile time generated pmds. This results in invalid pmds up
252  * to the point where we hit the physaddr 0 mapping.
253  *
254  * We limit the mappings to the region from _text to _end.  _end is
255  * rounded up to the 2MB boundary. This catches the invalid pmds as
256  * well, as they are located before _text:
257  */
258 void __init cleanup_highmap(void)
259 {
260         unsigned long vaddr = __START_KERNEL_map;
261         unsigned long end = round_up((unsigned long)_end, PMD_SIZE) - 1;
262         pmd_t *pmd = level2_kernel_pgt;
263         pmd_t *last_pmd = pmd + PTRS_PER_PMD;
264
265         for (; pmd < last_pmd; pmd++, vaddr += PMD_SIZE) {
266                 if (pmd_none(*pmd))
267                         continue;
268                 if (vaddr < (unsigned long) _text || vaddr > end)
269                         set_pmd(pmd, __pmd(0));
270         }
271 }
272
273 static unsigned long __initdata table_start;
274 static unsigned long __meminitdata table_end;
275 static unsigned long __meminitdata table_top;
276
277 static __meminit void *alloc_low_page(unsigned long *phys)
278 {
279         unsigned long pfn = table_end++;
280         void *adr;
281
282         if (after_bootmem) {
283                 adr = (void *)get_zeroed_page(GFP_ATOMIC);
284                 *phys = __pa(adr);
285
286                 return adr;
287         }
288
289         if (pfn >= table_top)
290                 panic("alloc_low_page: ran out of memory");
291
292         adr = early_ioremap(pfn * PAGE_SIZE, PAGE_SIZE);
293         memset(adr, 0, PAGE_SIZE);
294         *phys  = pfn * PAGE_SIZE;
295         return adr;
296 }
297
298 static __meminit void unmap_low_page(void *adr)
299 {
300         if (after_bootmem)
301                 return;
302
303         early_iounmap(adr, PAGE_SIZE);
304 }
305
306 static unsigned long __meminit
307 phys_pte_init(pte_t *pte_page, unsigned long addr, unsigned long end)
308 {
309         unsigned pages = 0;
310         unsigned long last_map_addr = end;
311         int i;
312
313         pte_t *pte = pte_page + pte_index(addr);
314
315         for(i = pte_index(addr); i < PTRS_PER_PTE; i++, addr += PAGE_SIZE, pte++) {
316
317                 if (addr >= end) {
318                         if (!after_bootmem) {
319                                 for(; i < PTRS_PER_PTE; i++, pte++)
320                                         set_pte(pte, __pte(0));
321                         }
322                         break;
323                 }
324
325                 if (pte_val(*pte))
326                         continue;
327
328                 if (0)
329                         printk("   pte=%p addr=%lx pte=%016lx\n",
330                                pte, addr, pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL).pte);
331                 set_pte(pte, pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL));
332                 last_map_addr = (addr & PAGE_MASK) + PAGE_SIZE;
333                 pages++;
334         }
335         update_page_count(PG_LEVEL_4K, pages);
336
337         return last_map_addr;
338 }
339
340 static unsigned long __meminit
341 phys_pte_update(pmd_t *pmd, unsigned long address, unsigned long end)
342 {
343         pte_t *pte = (pte_t *)pmd_page_vaddr(*pmd);
344
345         return phys_pte_init(pte, address, end);
346 }
347
348 static unsigned long __meminit
349 phys_pmd_init(pmd_t *pmd_page, unsigned long address, unsigned long end,
350                          unsigned long page_size_mask)
351 {
352         unsigned long pages = 0;
353         unsigned long last_map_addr = end;
354
355         int i = pmd_index(address);
356
357         for (; i < PTRS_PER_PMD; i++, address += PMD_SIZE) {
358                 unsigned long pte_phys;
359                 pmd_t *pmd = pmd_page + pmd_index(address);
360                 pte_t *pte;
361
362                 if (address >= end) {
363                         if (!after_bootmem) {
364                                 for (; i < PTRS_PER_PMD; i++, pmd++)
365                                         set_pmd(pmd, __pmd(0));
366                         }
367                         break;
368                 }
369
370                 if (pmd_val(*pmd)) {
371                         if (!pmd_large(*pmd))
372                                 last_map_addr = phys_pte_update(pmd, address,
373                                                                  end);
374                         continue;
375                 }
376
377                 if (page_size_mask & (1<<PG_LEVEL_2M)) {
378                         pages++;
379                         set_pte((pte_t *)pmd,
380                                 pfn_pte(address >> PAGE_SHIFT, PAGE_KERNEL_LARGE));
381                         last_map_addr = (address & PMD_MASK) + PMD_SIZE;
382                         continue;
383                 }
384
385                 pte = alloc_low_page(&pte_phys);
386                 last_map_addr = phys_pte_init(pte, address, end);
387                 unmap_low_page(pte);
388
389                 pmd_populate_kernel(&init_mm, pmd, __va(pte_phys));
390         }
391         update_page_count(PG_LEVEL_2M, pages);
392         return last_map_addr;
393 }
394
395 static unsigned long __meminit
396 phys_pmd_update(pud_t *pud, unsigned long address, unsigned long end,
397                          unsigned long page_size_mask)
398 {
399         pmd_t *pmd = pmd_offset(pud, 0);
400         unsigned long last_map_addr;
401
402         spin_lock(&init_mm.page_table_lock);
403         last_map_addr = phys_pmd_init(pmd, address, end, page_size_mask);
404         spin_unlock(&init_mm.page_table_lock);
405         __flush_tlb_all();
406         return last_map_addr;
407 }
408
409 static unsigned long __meminit
410 phys_pud_init(pud_t *pud_page, unsigned long addr, unsigned long end,
411                          unsigned long page_size_mask)
412 {
413         unsigned long pages = 0;
414         unsigned long last_map_addr = end;
415         int i = pud_index(addr);
416
417         for (; i < PTRS_PER_PUD; i++, addr = (addr & PUD_MASK) + PUD_SIZE) {
418                 unsigned long pmd_phys;
419                 pud_t *pud = pud_page + pud_index(addr);
420                 pmd_t *pmd;
421
422                 if (addr >= end)
423                         break;
424
425                 if (!after_bootmem &&
426                                 !e820_any_mapped(addr, addr+PUD_SIZE, 0)) {
427                         set_pud(pud, __pud(0));
428                         continue;
429                 }
430
431                 if (pud_val(*pud)) {
432                         if (!pud_large(*pud))
433                                 last_map_addr = phys_pmd_update(pud, addr, end,
434                                                          page_size_mask);
435                         continue;
436                 }
437
438                 if (page_size_mask & (1<<PG_LEVEL_1G)) {
439                         pages++;
440                         set_pte((pte_t *)pud,
441                                 pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL_LARGE));
442                         last_map_addr = (addr & PUD_MASK) + PUD_SIZE;
443                         continue;
444                 }
445
446                 pmd = alloc_low_page(&pmd_phys);
447
448                 spin_lock(&init_mm.page_table_lock);
449                 last_map_addr = phys_pmd_init(pmd, addr, end, page_size_mask);
450                 unmap_low_page(pmd);
451                 pud_populate(&init_mm, pud, __va(pmd_phys));
452                 spin_unlock(&init_mm.page_table_lock);
453
454         }
455         __flush_tlb_all();
456         update_page_count(PG_LEVEL_1G, pages);
457
458         return last_map_addr;
459 }
460
461 static unsigned long __meminit
462 phys_pud_update(pgd_t *pgd, unsigned long addr, unsigned long end,
463                  unsigned long page_size_mask)
464 {
465         pud_t *pud;
466
467         pud = (pud_t *)pgd_page_vaddr(*pgd);
468
469         return phys_pud_init(pud, addr, end, page_size_mask);
470 }
471
472 static void __init find_early_table_space(unsigned long end)
473 {
474         unsigned long puds, pmds, ptes, tables, start;
475
476         puds = (end + PUD_SIZE - 1) >> PUD_SHIFT;
477         tables = round_up(puds * sizeof(pud_t), PAGE_SIZE);
478         if (direct_gbpages) {
479                 unsigned long extra;
480                 extra = end - ((end>>PUD_SHIFT) << PUD_SHIFT);
481                 pmds = (extra + PMD_SIZE - 1) >> PMD_SHIFT;
482         } else
483                 pmds = (end + PMD_SIZE - 1) >> PMD_SHIFT;
484         tables += round_up(pmds * sizeof(pmd_t), PAGE_SIZE);
485
486         if (cpu_has_pse) {
487                 unsigned long extra;
488                 extra = end - ((end>>PMD_SHIFT) << PMD_SHIFT);
489                 ptes = (extra + PAGE_SIZE - 1) >> PAGE_SHIFT;
490         } else
491                 ptes = (end + PAGE_SIZE - 1) >> PAGE_SHIFT;
492         tables += round_up(ptes * sizeof(pte_t), PAGE_SIZE);
493
494         /*
495          * RED-PEN putting page tables only on node 0 could
496          * cause a hotspot and fill up ZONE_DMA. The page tables
497          * need roughly 0.5KB per GB.
498          */
499         start = 0x8000;
500         table_start = find_e820_area(start, end, tables, PAGE_SIZE);
501         if (table_start == -1UL)
502                 panic("Cannot find space for the kernel page tables");
503
504         table_start >>= PAGE_SHIFT;
505         table_end = table_start;
506         table_top = table_start + (tables >> PAGE_SHIFT);
507
508         printk(KERN_DEBUG "kernel direct mapping tables up to %lx @ %lx-%lx\n",
509                 end, table_start << PAGE_SHIFT, table_top << PAGE_SHIFT);
510 }
511
512 static void __init init_gbpages(void)
513 {
514         if (direct_gbpages && cpu_has_gbpages)
515                 printk(KERN_INFO "Using GB pages for direct mapping\n");
516         else
517                 direct_gbpages = 0;
518 }
519
520 #ifdef CONFIG_MEMTEST
521
522 static void __init memtest(unsigned long start_phys, unsigned long size,
523                                  unsigned pattern)
524 {
525         unsigned long i;
526         unsigned long *start;
527         unsigned long start_bad;
528         unsigned long last_bad;
529         unsigned long val;
530         unsigned long start_phys_aligned;
531         unsigned long count;
532         unsigned long incr;
533
534         switch (pattern) {
535         case 0:
536                 val = 0UL;
537                 break;
538         case 1:
539                 val = -1UL;
540                 break;
541         case 2:
542                 val = 0x5555555555555555UL;
543                 break;
544         case 3:
545                 val = 0xaaaaaaaaaaaaaaaaUL;
546                 break;
547         default:
548                 return;
549         }
550
551         incr = sizeof(unsigned long);
552         start_phys_aligned = ALIGN(start_phys, incr);
553         count = (size - (start_phys_aligned - start_phys))/incr;
554         start = __va(start_phys_aligned);
555         start_bad = 0;
556         last_bad = 0;
557
558         for (i = 0; i < count; i++)
559                 start[i] = val;
560         for (i = 0; i < count; i++, start++, start_phys_aligned += incr) {
561                 if (*start != val) {
562                         if (start_phys_aligned == last_bad + incr) {
563                                 last_bad += incr;
564                         } else {
565                                 if (start_bad) {
566                                         printk(KERN_CONT "\n  %016lx bad mem addr %016lx - %016lx reserved",
567                                                 val, start_bad, last_bad + incr);
568                                         reserve_early(start_bad, last_bad - start_bad, "BAD RAM");
569                                 }
570                                 start_bad = last_bad = start_phys_aligned;
571                         }
572                 }
573         }
574         if (start_bad) {
575                 printk(KERN_CONT "\n  %016lx bad mem addr %016lx - %016lx reserved",
576                         val, start_bad, last_bad + incr);
577                 reserve_early(start_bad, last_bad - start_bad, "BAD RAM");
578         }
579
580 }
581
582 /* default is disabled */
583 static int memtest_pattern __initdata;
584
585 static int __init parse_memtest(char *arg)
586 {
587         if (arg)
588                 memtest_pattern = simple_strtoul(arg, NULL, 0);
589         return 0;
590 }
591
592 early_param("memtest", parse_memtest);
593
594 static void __init early_memtest(unsigned long start, unsigned long end)
595 {
596         u64 t_start, t_size;
597         unsigned pattern;
598
599         if (!memtest_pattern)
600                 return;
601
602         printk(KERN_INFO "early_memtest: pattern num %d", memtest_pattern);
603         for (pattern = 0; pattern < memtest_pattern; pattern++) {
604                 t_start = start;
605                 t_size = 0;
606                 while (t_start < end) {
607                         t_start = find_e820_area_size(t_start, &t_size, 1);
608
609                         /* done ? */
610                         if (t_start >= end)
611                                 break;
612                         if (t_start + t_size > end)
613                                 t_size = end - t_start;
614
615                         printk(KERN_CONT "\n  %016llx - %016llx pattern %d",
616                                 (unsigned long long)t_start,
617                                 (unsigned long long)t_start + t_size, pattern);
618
619                         memtest(t_start, t_size, pattern);
620
621                         t_start += t_size;
622                 }
623         }
624         printk(KERN_CONT "\n");
625 }
626 #else
627 static void __init early_memtest(unsigned long start, unsigned long end)
628 {
629 }
630 #endif
631
632 static unsigned long __init kernel_physical_mapping_init(unsigned long start,
633                                                 unsigned long end,
634                                                 unsigned long page_size_mask)
635 {
636
637         unsigned long next, last_map_addr = end;
638
639         start = (unsigned long)__va(start);
640         end = (unsigned long)__va(end);
641
642         for (; start < end; start = next) {
643                 pgd_t *pgd = pgd_offset_k(start);
644                 unsigned long pud_phys;
645                 pud_t *pud;
646
647                 next = (start + PGDIR_SIZE) & PGDIR_MASK;
648                 if (next > end)
649                         next = end;
650
651                 if (pgd_val(*pgd)) {
652                         last_map_addr = phys_pud_update(pgd, __pa(start),
653                                                  __pa(end), page_size_mask);
654                         continue;
655                 }
656
657                 if (after_bootmem)
658                         pud = pud_offset(pgd, start & PGDIR_MASK);
659                 else
660                         pud = alloc_low_page(&pud_phys);
661
662                 last_map_addr = phys_pud_init(pud, __pa(start), __pa(next),
663                                                  page_size_mask);
664                 unmap_low_page(pud);
665                 pgd_populate(&init_mm, pgd_offset_k(start),
666                              __va(pud_phys));
667         }
668
669         return last_map_addr;
670 }
671
672 struct map_range {
673         unsigned long start;
674         unsigned long end;
675         unsigned page_size_mask;
676 };
677
678 #define NR_RANGE_MR 5
679
680 static int save_mr(struct map_range *mr, int nr_range,
681                    unsigned long start_pfn, unsigned long end_pfn,
682                    unsigned long page_size_mask)
683 {
684
685         if (start_pfn < end_pfn) {
686                 if (nr_range >= NR_RANGE_MR)
687                         panic("run out of range for init_memory_mapping\n");
688                 mr[nr_range].start = start_pfn<<PAGE_SHIFT;
689                 mr[nr_range].end   = end_pfn<<PAGE_SHIFT;
690                 mr[nr_range].page_size_mask = page_size_mask;
691                 nr_range++;
692         }
693
694         return nr_range;
695 }
696
697 /*
698  * Setup the direct mapping of the physical memory at PAGE_OFFSET.
699  * This runs before bootmem is initialized and gets pages directly from
700  * the physical memory. To access them they are temporarily mapped.
701  */
702 unsigned long __init_refok init_memory_mapping(unsigned long start,
703                                                unsigned long end)
704 {
705         unsigned long last_map_addr = 0;
706         unsigned long page_size_mask = 0;
707         unsigned long start_pfn, end_pfn;
708
709         struct map_range mr[NR_RANGE_MR];
710         int nr_range, i;
711
712         printk(KERN_INFO "init_memory_mapping\n");
713
714         /*
715          * Find space for the kernel direct mapping tables.
716          *
717          * Later we should allocate these tables in the local node of the
718          * memory mapped. Unfortunately this is done currently before the
719          * nodes are discovered.
720          */
721         if (!after_bootmem)
722                 init_gbpages();
723
724         if (direct_gbpages)
725                 page_size_mask |= 1 << PG_LEVEL_1G;
726         if (cpu_has_pse)
727                 page_size_mask |= 1 << PG_LEVEL_2M;
728
729         memset(mr, 0, sizeof(mr));
730         nr_range = 0;
731
732         /* head if not big page alignment ?*/
733         start_pfn = start >> PAGE_SHIFT;
734         end_pfn = ((start + (PMD_SIZE - 1)) >> PMD_SHIFT)
735                         << (PMD_SHIFT - PAGE_SHIFT);
736         nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
737
738         /* big page (2M) range*/
739         start_pfn = ((start + (PMD_SIZE - 1))>>PMD_SHIFT)
740                          << (PMD_SHIFT - PAGE_SHIFT);
741         end_pfn = ((start + (PUD_SIZE - 1))>>PUD_SHIFT)
742                          << (PUD_SHIFT - PAGE_SHIFT);
743         if (end_pfn > ((end>>PUD_SHIFT)<<(PUD_SHIFT - PAGE_SHIFT)))
744                 end_pfn = ((end>>PUD_SHIFT)<<(PUD_SHIFT - PAGE_SHIFT));
745         nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
746                         page_size_mask & (1<<PG_LEVEL_2M));
747
748         /* big page (1G) range */
749         start_pfn = end_pfn;
750         end_pfn = (end>>PUD_SHIFT) << (PUD_SHIFT - PAGE_SHIFT);
751         nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
752                                 page_size_mask &
753                                  ((1<<PG_LEVEL_2M)|(1<<PG_LEVEL_1G)));
754
755         /* tail is not big page (1G) alignment */
756         start_pfn = end_pfn;
757         end_pfn = (end>>PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
758         nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
759                         page_size_mask & (1<<PG_LEVEL_2M));
760
761         /* tail is not big page (2M) alignment */
762         start_pfn = end_pfn;
763         end_pfn = end>>PAGE_SHIFT;
764         nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
765
766         /* try to merge same page size and continuous */
767         for (i = 0; nr_range > 1 && i < nr_range - 1; i++) {
768                 unsigned long old_start;
769                 if (mr[i].end != mr[i+1].start ||
770                     mr[i].page_size_mask != mr[i+1].page_size_mask)
771                         continue;
772                 /* move it */
773                 old_start = mr[i].start;
774                 memmove(&mr[i], &mr[i+1],
775                          (nr_range - 1 - i) * sizeof (struct map_range));
776                 mr[i].start = old_start;
777                 nr_range--;
778         }
779
780         for (i = 0; i < nr_range; i++)
781                 printk(KERN_DEBUG " %010lx - %010lx page %s\n",
782                                 mr[i].start, mr[i].end,
783                         (mr[i].page_size_mask & (1<<PG_LEVEL_1G))?"1G":(
784                          (mr[i].page_size_mask & (1<<PG_LEVEL_2M))?"2M":"4k"));
785
786         if (!after_bootmem)
787                 find_early_table_space(end);
788
789         for (i = 0; i < nr_range; i++)
790                 last_map_addr = kernel_physical_mapping_init(
791                                         mr[i].start, mr[i].end,
792                                         mr[i].page_size_mask);
793
794         if (!after_bootmem)
795                 mmu_cr4_features = read_cr4();
796         __flush_tlb_all();
797
798         if (!after_bootmem && table_end > table_start)
799                 reserve_early(table_start << PAGE_SHIFT,
800                                  table_end << PAGE_SHIFT, "PGTABLE");
801
802         printk(KERN_INFO "last_map_addr: %lx end: %lx\n",
803                          last_map_addr, end);
804
805         if (!after_bootmem)
806                 early_memtest(start, end);
807
808         return last_map_addr >> PAGE_SHIFT;
809 }
810
811 #ifndef CONFIG_NUMA
812 void __init initmem_init(unsigned long start_pfn, unsigned long end_pfn)
813 {
814         unsigned long bootmap_size, bootmap;
815
816         bootmap_size = bootmem_bootmap_pages(end_pfn)<<PAGE_SHIFT;
817         bootmap = find_e820_area(0, end_pfn<<PAGE_SHIFT, bootmap_size,
818                                  PAGE_SIZE);
819         if (bootmap == -1L)
820                 panic("Cannot find bootmem map of size %ld\n", bootmap_size);
821         /* don't touch min_low_pfn */
822         bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap >> PAGE_SHIFT,
823                                          0, end_pfn);
824         e820_register_active_regions(0, start_pfn, end_pfn);
825         free_bootmem_with_active_regions(0, end_pfn);
826         early_res_to_bootmem(0, end_pfn<<PAGE_SHIFT);
827         reserve_bootmem(bootmap, bootmap_size, BOOTMEM_DEFAULT);
828 }
829
830 void __init paging_init(void)
831 {
832         unsigned long max_zone_pfns[MAX_NR_ZONES];
833
834         memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
835         max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
836         max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
837         max_zone_pfns[ZONE_NORMAL] = max_pfn;
838
839         memory_present(0, 0, max_pfn);
840         sparse_init();
841         free_area_init_nodes(max_zone_pfns);
842 }
843 #endif
844
845 /*
846  * Memory hotplug specific functions
847  */
848 #ifdef CONFIG_MEMORY_HOTPLUG
849 /*
850  * Memory is added always to NORMAL zone. This means you will never get
851  * additional DMA/DMA32 memory.
852  */
853 int arch_add_memory(int nid, u64 start, u64 size)
854 {
855         struct pglist_data *pgdat = NODE_DATA(nid);
856         struct zone *zone = pgdat->node_zones + ZONE_NORMAL;
857         unsigned long last_mapped_pfn, start_pfn = start >> PAGE_SHIFT;
858         unsigned long nr_pages = size >> PAGE_SHIFT;
859         int ret;
860
861         last_mapped_pfn = init_memory_mapping(start, start + size-1);
862         if (last_mapped_pfn > max_pfn_mapped)
863                 max_pfn_mapped = last_mapped_pfn;
864
865         ret = __add_pages(zone, start_pfn, nr_pages);
866         WARN_ON(1);
867
868         return ret;
869 }
870 EXPORT_SYMBOL_GPL(arch_add_memory);
871
872 #if !defined(CONFIG_ACPI_NUMA) && defined(CONFIG_NUMA)
873 int memory_add_physaddr_to_nid(u64 start)
874 {
875         return 0;
876 }
877 EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
878 #endif
879
880 #endif /* CONFIG_MEMORY_HOTPLUG */
881
882 /*
883  * devmem_is_allowed() checks to see if /dev/mem access to a certain address
884  * is valid. The argument is a physical page number.
885  *
886  *
887  * On x86, access has to be given to the first megabyte of ram because that area
888  * contains bios code and data regions used by X and dosemu and similar apps.
889  * Access has to be given to non-kernel-ram areas as well, these contain the PCI
890  * mmio resources as well as potential bios/acpi data regions.
891  */
892 int devmem_is_allowed(unsigned long pagenr)
893 {
894         if (pagenr <= 256)
895                 return 1;
896         if (!page_is_ram(pagenr))
897                 return 1;
898         return 0;
899 }
900
901
902 static struct kcore_list kcore_mem, kcore_vmalloc, kcore_kernel,
903                          kcore_modules, kcore_vsyscall;
904
905 void __init mem_init(void)
906 {
907         long codesize, reservedpages, datasize, initsize;
908
909         pci_iommu_alloc();
910
911         /* clear_bss() already clear the empty_zero_page */
912
913         reservedpages = 0;
914
915         /* this will put all low memory onto the freelists */
916 #ifdef CONFIG_NUMA
917         totalram_pages = numa_free_all_bootmem();
918 #else
919         totalram_pages = free_all_bootmem();
920 #endif
921         reservedpages = max_pfn - totalram_pages -
922                                         absent_pages_in_range(0, max_pfn);
923         after_bootmem = 1;
924
925         codesize =  (unsigned long) &_etext - (unsigned long) &_text;
926         datasize =  (unsigned long) &_edata - (unsigned long) &_etext;
927         initsize =  (unsigned long) &__init_end - (unsigned long) &__init_begin;
928
929         /* Register memory areas for /proc/kcore */
930         kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
931         kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
932                    VMALLOC_END-VMALLOC_START);
933         kclist_add(&kcore_kernel, &_stext, _end - _stext);
934         kclist_add(&kcore_modules, (void *)MODULES_VADDR, MODULES_LEN);
935         kclist_add(&kcore_vsyscall, (void *)VSYSCALL_START,
936                                  VSYSCALL_END - VSYSCALL_START);
937
938         printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, "
939                                 "%ldk reserved, %ldk data, %ldk init)\n",
940                 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
941                 max_pfn << (PAGE_SHIFT-10),
942                 codesize >> 10,
943                 reservedpages << (PAGE_SHIFT-10),
944                 datasize >> 10,
945                 initsize >> 10);
946
947         cpa_init();
948 }
949
950 void free_init_pages(char *what, unsigned long begin, unsigned long end)
951 {
952         unsigned long addr = begin;
953
954         if (addr >= end)
955                 return;
956
957         /*
958          * If debugging page accesses then do not free this memory but
959          * mark them not present - any buggy init-section access will
960          * create a kernel page fault:
961          */
962 #ifdef CONFIG_DEBUG_PAGEALLOC
963         printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n",
964                 begin, PAGE_ALIGN(end));
965         set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
966 #else
967         printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
968
969         for (; addr < end; addr += PAGE_SIZE) {
970                 ClearPageReserved(virt_to_page(addr));
971                 init_page_count(virt_to_page(addr));
972                 memset((void *)(addr & ~(PAGE_SIZE-1)),
973                         POISON_FREE_INITMEM, PAGE_SIZE);
974                 free_page(addr);
975                 totalram_pages++;
976         }
977 #endif
978 }
979
980 void free_initmem(void)
981 {
982         free_init_pages("unused kernel memory",
983                         (unsigned long)(&__init_begin),
984                         (unsigned long)(&__init_end));
985 }
986
987 #ifdef CONFIG_DEBUG_RODATA
988 const int rodata_test_data = 0xC3;
989 EXPORT_SYMBOL_GPL(rodata_test_data);
990
991 void mark_rodata_ro(void)
992 {
993         unsigned long start = PFN_ALIGN(_stext), end = PFN_ALIGN(__end_rodata);
994         unsigned long rodata_start =
995                 ((unsigned long)__start_rodata + PAGE_SIZE - 1) & PAGE_MASK;
996
997 #ifdef CONFIG_DYNAMIC_FTRACE
998         /* Dynamic tracing modifies the kernel text section */
999         start = rodata_start;
1000 #endif
1001
1002         printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
1003                (end - start) >> 10);
1004         set_memory_ro(start, (end - start) >> PAGE_SHIFT);
1005
1006         /*
1007          * The rodata section (but not the kernel text!) should also be
1008          * not-executable.
1009          */
1010         set_memory_nx(rodata_start, (end - rodata_start) >> PAGE_SHIFT);
1011
1012         rodata_test();
1013
1014 #ifdef CONFIG_CPA_DEBUG
1015         printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end);
1016         set_memory_rw(start, (end-start) >> PAGE_SHIFT);
1017
1018         printk(KERN_INFO "Testing CPA: again\n");
1019         set_memory_ro(start, (end-start) >> PAGE_SHIFT);
1020 #endif
1021 }
1022
1023 #endif
1024
1025 #ifdef CONFIG_BLK_DEV_INITRD
1026 void free_initrd_mem(unsigned long start, unsigned long end)
1027 {
1028         free_init_pages("initrd memory", start, end);
1029 }
1030 #endif
1031
1032 int __init reserve_bootmem_generic(unsigned long phys, unsigned long len,
1033                                    int flags)
1034 {
1035 #ifdef CONFIG_NUMA
1036         int nid, next_nid;
1037         int ret;
1038 #endif
1039         unsigned long pfn = phys >> PAGE_SHIFT;
1040
1041         if (pfn >= max_pfn) {
1042                 /*
1043                  * This can happen with kdump kernels when accessing
1044                  * firmware tables:
1045                  */
1046                 if (pfn < max_pfn_mapped)
1047                         return -EFAULT;
1048
1049                 printk(KERN_ERR "reserve_bootmem: illegal reserve %lx %lu\n",
1050                                 phys, len);
1051                 return -EFAULT;
1052         }
1053
1054         /* Should check here against the e820 map to avoid double free */
1055 #ifdef CONFIG_NUMA
1056         nid = phys_to_nid(phys);
1057         next_nid = phys_to_nid(phys + len - 1);
1058         if (nid == next_nid)
1059                 ret = reserve_bootmem_node(NODE_DATA(nid), phys, len, flags);
1060         else
1061                 ret = reserve_bootmem(phys, len, flags);
1062
1063         if (ret != 0)
1064                 return ret;
1065
1066 #else
1067         reserve_bootmem(phys, len, BOOTMEM_DEFAULT);
1068 #endif
1069
1070         if (phys+len <= MAX_DMA_PFN*PAGE_SIZE) {
1071                 dma_reserve += len / PAGE_SIZE;
1072                 set_dma_reserve(dma_reserve);
1073         }
1074
1075         return 0;
1076 }
1077
1078 int kern_addr_valid(unsigned long addr)
1079 {
1080         unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
1081         pgd_t *pgd;
1082         pud_t *pud;
1083         pmd_t *pmd;
1084         pte_t *pte;
1085
1086         if (above != 0 && above != -1UL)
1087                 return 0;
1088
1089         pgd = pgd_offset_k(addr);
1090         if (pgd_none(*pgd))
1091                 return 0;
1092
1093         pud = pud_offset(pgd, addr);
1094         if (pud_none(*pud))
1095                 return 0;
1096
1097         pmd = pmd_offset(pud, addr);
1098         if (pmd_none(*pmd))
1099                 return 0;
1100
1101         if (pmd_large(*pmd))
1102                 return pfn_valid(pmd_pfn(*pmd));
1103
1104         pte = pte_offset_kernel(pmd, addr);
1105         if (pte_none(*pte))
1106                 return 0;
1107
1108         return pfn_valid(pte_pfn(*pte));
1109 }
1110
1111 /*
1112  * A pseudo VMA to allow ptrace access for the vsyscall page.  This only
1113  * covers the 64bit vsyscall page now. 32bit has a real VMA now and does
1114  * not need special handling anymore:
1115  */
1116 static struct vm_area_struct gate_vma = {
1117         .vm_start       = VSYSCALL_START,
1118         .vm_end         = VSYSCALL_START + (VSYSCALL_MAPPED_PAGES * PAGE_SIZE),
1119         .vm_page_prot   = PAGE_READONLY_EXEC,
1120         .vm_flags       = VM_READ | VM_EXEC
1121 };
1122
1123 struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
1124 {
1125 #ifdef CONFIG_IA32_EMULATION
1126         if (test_tsk_thread_flag(tsk, TIF_IA32))
1127                 return NULL;
1128 #endif
1129         return &gate_vma;
1130 }
1131
1132 int in_gate_area(struct task_struct *task, unsigned long addr)
1133 {
1134         struct vm_area_struct *vma = get_gate_vma(task);
1135
1136         if (!vma)
1137                 return 0;
1138
1139         return (addr >= vma->vm_start) && (addr < vma->vm_end);
1140 }
1141
1142 /*
1143  * Use this when you have no reliable task/vma, typically from interrupt
1144  * context. It is less reliable than using the task's vma and may give
1145  * false positives:
1146  */
1147 int in_gate_area_no_task(unsigned long addr)
1148 {
1149         return (addr >= VSYSCALL_START) && (addr < VSYSCALL_END);
1150 }
1151
1152 const char *arch_vma_name(struct vm_area_struct *vma)
1153 {
1154         if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
1155                 return "[vdso]";
1156         if (vma == &gate_vma)
1157                 return "[vsyscall]";
1158         return NULL;
1159 }
1160
1161 #ifdef CONFIG_SPARSEMEM_VMEMMAP
1162 /*
1163  * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
1164  */
1165 static long __meminitdata addr_start, addr_end;
1166 static void __meminitdata *p_start, *p_end;
1167 static int __meminitdata node_start;
1168
1169 int __meminit
1170 vmemmap_populate(struct page *start_page, unsigned long size, int node)
1171 {
1172         unsigned long addr = (unsigned long)start_page;
1173         unsigned long end = (unsigned long)(start_page + size);
1174         unsigned long next;
1175         pgd_t *pgd;
1176         pud_t *pud;
1177         pmd_t *pmd;
1178
1179         for (; addr < end; addr = next) {
1180                 void *p = NULL;
1181
1182                 pgd = vmemmap_pgd_populate(addr, node);
1183                 if (!pgd)
1184                         return -ENOMEM;
1185
1186                 pud = vmemmap_pud_populate(pgd, addr, node);
1187                 if (!pud)
1188                         return -ENOMEM;
1189
1190                 if (!cpu_has_pse) {
1191                         next = (addr + PAGE_SIZE) & PAGE_MASK;
1192                         pmd = vmemmap_pmd_populate(pud, addr, node);
1193
1194                         if (!pmd)
1195                                 return -ENOMEM;
1196
1197                         p = vmemmap_pte_populate(pmd, addr, node);
1198
1199                         if (!p)
1200                                 return -ENOMEM;
1201
1202                         addr_end = addr + PAGE_SIZE;
1203                         p_end = p + PAGE_SIZE;
1204                 } else {
1205                         next = pmd_addr_end(addr, end);
1206
1207                         pmd = pmd_offset(pud, addr);
1208                         if (pmd_none(*pmd)) {
1209                                 pte_t entry;
1210
1211                                 p = vmemmap_alloc_block(PMD_SIZE, node);
1212                                 if (!p)
1213                                         return -ENOMEM;
1214
1215                                 entry = pfn_pte(__pa(p) >> PAGE_SHIFT,
1216                                                 PAGE_KERNEL_LARGE);
1217                                 set_pmd(pmd, __pmd(pte_val(entry)));
1218
1219                                 /* check to see if we have contiguous blocks */
1220                                 if (p_end != p || node_start != node) {
1221                                         if (p_start)
1222                                                 printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n",
1223                                                        addr_start, addr_end-1, p_start, p_end-1, node_start);
1224                                         addr_start = addr;
1225                                         node_start = node;
1226                                         p_start = p;
1227                                 }
1228
1229                                 addr_end = addr + PMD_SIZE;
1230                                 p_end = p + PMD_SIZE;
1231                         } else
1232                                 vmemmap_verify((pte_t *)pmd, node, addr, next);
1233                 }
1234
1235         }
1236         return 0;
1237 }
1238
1239 void __meminit vmemmap_populate_print_last(void)
1240 {
1241         if (p_start) {
1242                 printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n",
1243                         addr_start, addr_end-1, p_start, p_end-1, node_start);
1244                 p_start = NULL;
1245                 p_end = NULL;
1246                 node_start = 0;
1247         }
1248 }
1249 #endif