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