Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[sfrench/cifs-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/pagemap.h>
22 #include <linux/bootmem.h>
23 #include <linux/proc_fs.h>
24 #include <linux/pci.h>
25 #include <linux/pfn.h>
26 #include <linux/poison.h>
27 #include <linux/dma-mapping.h>
28 #include <linux/module.h>
29 #include <linux/memory_hotplug.h>
30 #include <linux/nmi.h>
31
32 #include <asm/processor.h>
33 #include <asm/system.h>
34 #include <asm/uaccess.h>
35 #include <asm/pgtable.h>
36 #include <asm/pgalloc.h>
37 #include <asm/dma.h>
38 #include <asm/fixmap.h>
39 #include <asm/e820.h>
40 #include <asm/apic.h>
41 #include <asm/tlb.h>
42 #include <asm/mmu_context.h>
43 #include <asm/proto.h>
44 #include <asm/smp.h>
45 #include <asm/sections.h>
46 #include <asm/kdebug.h>
47 #include <asm/numa.h>
48 #include <asm/cacheflush.h>
49
50 const struct dma_mapping_ops *dma_ops;
51 EXPORT_SYMBOL(dma_ops);
52
53 static unsigned long dma_reserve __initdata;
54
55 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
56
57 /*
58  * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the
59  * physical space so we can cache the place of the first one and move
60  * around without checking the pgd every time.
61  */
62
63 void show_mem(void)
64 {
65         long i, total = 0, reserved = 0;
66         long shared = 0, cached = 0;
67         struct page *page;
68         pg_data_t *pgdat;
69
70         printk(KERN_INFO "Mem-info:\n");
71         show_free_areas();
72         printk(KERN_INFO "Free swap:       %6ldkB\n",
73                 nr_swap_pages << (PAGE_SHIFT-10));
74
75         for_each_online_pgdat(pgdat) {
76                 for (i = 0; i < pgdat->node_spanned_pages; ++i) {
77                         /*
78                          * This loop can take a while with 256 GB and
79                          * 4k pages so defer the NMI watchdog:
80                          */
81                         if (unlikely(i % MAX_ORDER_NR_PAGES == 0))
82                                 touch_nmi_watchdog();
83
84                         if (!pfn_valid(pgdat->node_start_pfn + i))
85                                 continue;
86
87                         page = pfn_to_page(pgdat->node_start_pfn + i);
88                         total++;
89                         if (PageReserved(page))
90                                 reserved++;
91                         else if (PageSwapCache(page))
92                                 cached++;
93                         else if (page_count(page))
94                                 shared += page_count(page) - 1;
95                 }
96         }
97         printk(KERN_INFO "%lu pages of RAM\n",          total);
98         printk(KERN_INFO "%lu reserved pages\n",        reserved);
99         printk(KERN_INFO "%lu pages shared\n",          shared);
100         printk(KERN_INFO "%lu pages swap cached\n",     cached);
101 }
102
103 int after_bootmem;
104
105 static __init void *spp_getpage(void)
106 {
107         void *ptr;
108
109         if (after_bootmem)
110                 ptr = (void *) get_zeroed_page(GFP_ATOMIC);
111         else
112                 ptr = alloc_bootmem_pages(PAGE_SIZE);
113
114         if (!ptr || ((unsigned long)ptr & ~PAGE_MASK)) {
115                 panic("set_pte_phys: cannot allocate page data %s\n",
116                         after_bootmem ? "after bootmem" : "");
117         }
118
119         pr_debug("spp_getpage %p\n", ptr);
120
121         return ptr;
122 }
123
124 static __init void
125 set_pte_phys(unsigned long vaddr, unsigned long phys, pgprot_t prot)
126 {
127         pgd_t *pgd;
128         pud_t *pud;
129         pmd_t *pmd;
130         pte_t *pte, new_pte;
131
132         pr_debug("set_pte_phys %lx to %lx\n", vaddr, phys);
133
134         pgd = pgd_offset_k(vaddr);
135         if (pgd_none(*pgd)) {
136                 printk(KERN_ERR
137                         "PGD FIXMAP MISSING, it should be setup in head.S!\n");
138                 return;
139         }
140         pud = pud_offset(pgd, vaddr);
141         if (pud_none(*pud)) {
142                 pmd = (pmd_t *) spp_getpage();
143                 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE | _PAGE_USER));
144                 if (pmd != pmd_offset(pud, 0)) {
145                         printk(KERN_ERR "PAGETABLE BUG #01! %p <-> %p\n",
146                                 pmd, pmd_offset(pud, 0));
147                         return;
148                 }
149         }
150         pmd = pmd_offset(pud, vaddr);
151         if (pmd_none(*pmd)) {
152                 pte = (pte_t *) spp_getpage();
153                 set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE | _PAGE_USER));
154                 if (pte != pte_offset_kernel(pmd, 0)) {
155                         printk(KERN_ERR "PAGETABLE BUG #02!\n");
156                         return;
157                 }
158         }
159         new_pte = pfn_pte(phys >> PAGE_SHIFT, prot);
160
161         pte = pte_offset_kernel(pmd, vaddr);
162         if (!pte_none(*pte) &&
163             pte_val(*pte) != (pte_val(new_pte) & __supported_pte_mask))
164                 pte_ERROR(*pte);
165         set_pte(pte, new_pte);
166
167         /*
168          * It's enough to flush this one mapping.
169          * (PGE mappings get flushed as well)
170          */
171         __flush_tlb_one(vaddr);
172 }
173
174 /*
175  * The head.S code sets up the kernel high mapping:
176  *
177  *   from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text)
178  *
179  * phys_addr holds the negative offset to the kernel, which is added
180  * to the compile time generated pmds. This results in invalid pmds up
181  * to the point where we hit the physaddr 0 mapping.
182  *
183  * We limit the mappings to the region from _text to _end.  _end is
184  * rounded up to the 2MB boundary. This catches the invalid pmds as
185  * well, as they are located before _text:
186  */
187 void __init cleanup_highmap(void)
188 {
189         unsigned long vaddr = __START_KERNEL_map;
190         unsigned long end = round_up((unsigned long)_end, PMD_SIZE) - 1;
191         pmd_t *pmd = level2_kernel_pgt;
192         pmd_t *last_pmd = pmd + PTRS_PER_PMD;
193
194         for (; pmd < last_pmd; pmd++, vaddr += PMD_SIZE) {
195                 if (!pmd_present(*pmd))
196                         continue;
197                 if (vaddr < (unsigned long) _text || vaddr > end)
198                         set_pmd(pmd, __pmd(0));
199         }
200 }
201
202 /* NOTE: this is meant to be run only at boot */
203 void __init
204 __set_fixmap(enum fixed_addresses idx, unsigned long phys, pgprot_t prot)
205 {
206         unsigned long address = __fix_to_virt(idx);
207
208         if (idx >= __end_of_fixed_addresses) {
209                 printk(KERN_ERR "Invalid __set_fixmap\n");
210                 return;
211         }
212         set_pte_phys(address, phys, prot);
213 }
214
215 static unsigned long __initdata table_start;
216 static unsigned long __meminitdata table_end;
217
218 static __meminit void *alloc_low_page(unsigned long *phys)
219 {
220         unsigned long pfn = table_end++;
221         void *adr;
222
223         if (after_bootmem) {
224                 adr = (void *)get_zeroed_page(GFP_ATOMIC);
225                 *phys = __pa(adr);
226
227                 return adr;
228         }
229
230         if (pfn >= end_pfn)
231                 panic("alloc_low_page: ran out of memory");
232
233         adr = early_ioremap(pfn * PAGE_SIZE, PAGE_SIZE);
234         memset(adr, 0, PAGE_SIZE);
235         *phys  = pfn * PAGE_SIZE;
236         return adr;
237 }
238
239 static __meminit void unmap_low_page(void *adr)
240 {
241         if (after_bootmem)
242                 return;
243
244         early_iounmap(adr, PAGE_SIZE);
245 }
246
247 /* Must run before zap_low_mappings */
248 __meminit void *early_ioremap(unsigned long addr, unsigned long size)
249 {
250         pmd_t *pmd, *last_pmd;
251         unsigned long vaddr;
252         int i, pmds;
253
254         pmds = ((addr & ~PMD_MASK) + size + ~PMD_MASK) / PMD_SIZE;
255         vaddr = __START_KERNEL_map;
256         pmd = level2_kernel_pgt;
257         last_pmd = level2_kernel_pgt + PTRS_PER_PMD - 1;
258
259         for (; pmd <= last_pmd; pmd++, vaddr += PMD_SIZE) {
260                 for (i = 0; i < pmds; i++) {
261                         if (pmd_present(pmd[i]))
262                                 goto continue_outer_loop;
263                 }
264                 vaddr += addr & ~PMD_MASK;
265                 addr &= PMD_MASK;
266
267                 for (i = 0; i < pmds; i++, addr += PMD_SIZE)
268                         set_pmd(pmd+i, __pmd(addr | __PAGE_KERNEL_LARGE_EXEC));
269                 __flush_tlb_all();
270
271                 return (void *)vaddr;
272 continue_outer_loop:
273                 ;
274         }
275         printk(KERN_ERR "early_ioremap(0x%lx, %lu) failed\n", addr, size);
276
277         return NULL;
278 }
279
280 /*
281  * To avoid virtual aliases later:
282  */
283 __meminit void early_iounmap(void *addr, unsigned long size)
284 {
285         unsigned long vaddr;
286         pmd_t *pmd;
287         int i, pmds;
288
289         vaddr = (unsigned long)addr;
290         pmds = ((vaddr & ~PMD_MASK) + size + ~PMD_MASK) / PMD_SIZE;
291         pmd = level2_kernel_pgt + pmd_index(vaddr);
292
293         for (i = 0; i < pmds; i++)
294                 pmd_clear(pmd + i);
295
296         __flush_tlb_all();
297 }
298
299 static void __meminit
300 phys_pmd_init(pmd_t *pmd_page, unsigned long address, unsigned long end)
301 {
302         int i = pmd_index(address);
303
304         for (; i < PTRS_PER_PMD; i++, address += PMD_SIZE) {
305                 pmd_t *pmd = pmd_page + pmd_index(address);
306
307                 if (address >= end) {
308                         if (!after_bootmem) {
309                                 for (; i < PTRS_PER_PMD; i++, pmd++)
310                                         set_pmd(pmd, __pmd(0));
311                         }
312                         break;
313                 }
314
315                 if (pmd_val(*pmd))
316                         continue;
317
318                 set_pte((pte_t *)pmd,
319                         pfn_pte(address >> PAGE_SHIFT, PAGE_KERNEL_LARGE));
320         }
321 }
322
323 static void __meminit
324 phys_pmd_update(pud_t *pud, unsigned long address, unsigned long end)
325 {
326         pmd_t *pmd = pmd_offset(pud, 0);
327         spin_lock(&init_mm.page_table_lock);
328         phys_pmd_init(pmd, address, end);
329         spin_unlock(&init_mm.page_table_lock);
330         __flush_tlb_all();
331 }
332
333 static void __meminit
334 phys_pud_init(pud_t *pud_page, unsigned long addr, unsigned long end)
335 {
336         int i = pud_index(addr);
337
338         for (; i < PTRS_PER_PUD; i++, addr = (addr & PUD_MASK) + PUD_SIZE) {
339                 unsigned long pmd_phys;
340                 pud_t *pud = pud_page + pud_index(addr);
341                 pmd_t *pmd;
342
343                 if (addr >= end)
344                         break;
345
346                 if (!after_bootmem &&
347                                 !e820_any_mapped(addr, addr+PUD_SIZE, 0)) {
348                         set_pud(pud, __pud(0));
349                         continue;
350                 }
351
352                 if (pud_val(*pud)) {
353                         phys_pmd_update(pud, addr, end);
354                         continue;
355                 }
356
357                 pmd = alloc_low_page(&pmd_phys);
358
359                 spin_lock(&init_mm.page_table_lock);
360                 set_pud(pud, __pud(pmd_phys | _KERNPG_TABLE));
361                 phys_pmd_init(pmd, addr, end);
362                 spin_unlock(&init_mm.page_table_lock);
363
364                 unmap_low_page(pmd);
365         }
366         __flush_tlb_all();
367 }
368
369 static void __init find_early_table_space(unsigned long end)
370 {
371         unsigned long puds, pmds, tables, start;
372
373         puds = (end + PUD_SIZE - 1) >> PUD_SHIFT;
374         pmds = (end + PMD_SIZE - 1) >> PMD_SHIFT;
375         tables = round_up(puds * sizeof(pud_t), PAGE_SIZE) +
376                  round_up(pmds * sizeof(pmd_t), PAGE_SIZE);
377
378         /*
379          * RED-PEN putting page tables only on node 0 could
380          * cause a hotspot and fill up ZONE_DMA. The page tables
381          * need roughly 0.5KB per GB.
382          */
383         start = 0x8000;
384         table_start = find_e820_area(start, end, tables, PAGE_SIZE);
385         if (table_start == -1UL)
386                 panic("Cannot find space for the kernel page tables");
387
388         table_start >>= PAGE_SHIFT;
389         table_end = table_start;
390
391         early_printk("kernel direct mapping tables up to %lx @ %lx-%lx\n",
392                 end, table_start << PAGE_SHIFT,
393                 (table_start << PAGE_SHIFT) + tables);
394 }
395
396 /*
397  * Setup the direct mapping of the physical memory at PAGE_OFFSET.
398  * This runs before bootmem is initialized and gets pages directly from
399  * the physical memory. To access them they are temporarily mapped.
400  */
401 void __init_refok init_memory_mapping(unsigned long start, unsigned long end)
402 {
403         unsigned long next;
404
405         pr_debug("init_memory_mapping\n");
406
407         /*
408          * Find space for the kernel direct mapping tables.
409          *
410          * Later we should allocate these tables in the local node of the
411          * memory mapped. Unfortunately this is done currently before the
412          * nodes are discovered.
413          */
414         if (!after_bootmem)
415                 find_early_table_space(end);
416
417         start = (unsigned long)__va(start);
418         end = (unsigned long)__va(end);
419
420         for (; start < end; start = next) {
421                 pgd_t *pgd = pgd_offset_k(start);
422                 unsigned long pud_phys;
423                 pud_t *pud;
424
425                 if (after_bootmem)
426                         pud = pud_offset(pgd, start & PGDIR_MASK);
427                 else
428                         pud = alloc_low_page(&pud_phys);
429
430                 next = start + PGDIR_SIZE;
431                 if (next > end)
432                         next = end;
433                 phys_pud_init(pud, __pa(start), __pa(next));
434                 if (!after_bootmem)
435                         set_pgd(pgd_offset_k(start), mk_kernel_pgd(pud_phys));
436                 unmap_low_page(pud);
437         }
438
439         if (!after_bootmem)
440                 mmu_cr4_features = read_cr4();
441         __flush_tlb_all();
442
443         if (!after_bootmem)
444                 reserve_early(table_start << PAGE_SHIFT,
445                                  table_end << PAGE_SHIFT, "PGTABLE");
446 }
447
448 #ifndef CONFIG_NUMA
449 void __init paging_init(void)
450 {
451         unsigned long max_zone_pfns[MAX_NR_ZONES];
452
453         memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
454         max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
455         max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
456         max_zone_pfns[ZONE_NORMAL] = end_pfn;
457
458         memory_present(0, 0, end_pfn);
459         sparse_init();
460         free_area_init_nodes(max_zone_pfns);
461 }
462 #endif
463
464 /*
465  * Memory hotplug specific functions
466  */
467 void online_page(struct page *page)
468 {
469         ClearPageReserved(page);
470         init_page_count(page);
471         __free_page(page);
472         totalram_pages++;
473         num_physpages++;
474 }
475
476 #ifdef CONFIG_MEMORY_HOTPLUG
477 /*
478  * Memory is added always to NORMAL zone. This means you will never get
479  * additional DMA/DMA32 memory.
480  */
481 int arch_add_memory(int nid, u64 start, u64 size)
482 {
483         struct pglist_data *pgdat = NODE_DATA(nid);
484         struct zone *zone = pgdat->node_zones + ZONE_NORMAL;
485         unsigned long start_pfn = start >> PAGE_SHIFT;
486         unsigned long nr_pages = size >> PAGE_SHIFT;
487         int ret;
488
489         init_memory_mapping(start, start + size-1);
490
491         ret = __add_pages(zone, start_pfn, nr_pages);
492         WARN_ON(1);
493
494         return ret;
495 }
496 EXPORT_SYMBOL_GPL(arch_add_memory);
497
498 #if !defined(CONFIG_ACPI_NUMA) && defined(CONFIG_NUMA)
499 int memory_add_physaddr_to_nid(u64 start)
500 {
501         return 0;
502 }
503 EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
504 #endif
505
506 #endif /* CONFIG_MEMORY_HOTPLUG */
507
508 static struct kcore_list kcore_mem, kcore_vmalloc, kcore_kernel,
509                          kcore_modules, kcore_vsyscall;
510
511 void __init mem_init(void)
512 {
513         long codesize, reservedpages, datasize, initsize;
514
515         pci_iommu_alloc();
516
517         /* clear_bss() already clear the empty_zero_page */
518
519         reservedpages = 0;
520
521         /* this will put all low memory onto the freelists */
522 #ifdef CONFIG_NUMA
523         totalram_pages = numa_free_all_bootmem();
524 #else
525         totalram_pages = free_all_bootmem();
526 #endif
527         reservedpages = end_pfn - totalram_pages -
528                                         absent_pages_in_range(0, end_pfn);
529         after_bootmem = 1;
530
531         codesize =  (unsigned long) &_etext - (unsigned long) &_text;
532         datasize =  (unsigned long) &_edata - (unsigned long) &_etext;
533         initsize =  (unsigned long) &__init_end - (unsigned long) &__init_begin;
534
535         /* Register memory areas for /proc/kcore */
536         kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
537         kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
538                    VMALLOC_END-VMALLOC_START);
539         kclist_add(&kcore_kernel, &_stext, _end - _stext);
540         kclist_add(&kcore_modules, (void *)MODULES_VADDR, MODULES_LEN);
541         kclist_add(&kcore_vsyscall, (void *)VSYSCALL_START,
542                                  VSYSCALL_END - VSYSCALL_START);
543
544         printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, "
545                                 "%ldk reserved, %ldk data, %ldk init)\n",
546                 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
547                 end_pfn << (PAGE_SHIFT-10),
548                 codesize >> 10,
549                 reservedpages << (PAGE_SHIFT-10),
550                 datasize >> 10,
551                 initsize >> 10);
552
553         cpa_init();
554 }
555
556 void free_init_pages(char *what, unsigned long begin, unsigned long end)
557 {
558         unsigned long addr = begin;
559
560         if (addr >= end)
561                 return;
562
563         /*
564          * If debugging page accesses then do not free this memory but
565          * mark them not present - any buggy init-section access will
566          * create a kernel page fault:
567          */
568 #ifdef CONFIG_DEBUG_PAGEALLOC
569         printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n",
570                 begin, PAGE_ALIGN(end));
571         set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
572 #else
573         printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
574
575         for (; addr < end; addr += PAGE_SIZE) {
576                 ClearPageReserved(virt_to_page(addr));
577                 init_page_count(virt_to_page(addr));
578                 memset((void *)(addr & ~(PAGE_SIZE-1)),
579                         POISON_FREE_INITMEM, PAGE_SIZE);
580                 free_page(addr);
581                 totalram_pages++;
582         }
583 #endif
584 }
585
586 void free_initmem(void)
587 {
588         free_init_pages("unused kernel memory",
589                         (unsigned long)(&__init_begin),
590                         (unsigned long)(&__init_end));
591 }
592
593 #ifdef CONFIG_DEBUG_RODATA
594 const int rodata_test_data = 0xC3;
595 EXPORT_SYMBOL_GPL(rodata_test_data);
596
597 void mark_rodata_ro(void)
598 {
599         unsigned long start = (unsigned long)_stext, end;
600
601 #ifdef CONFIG_HOTPLUG_CPU
602         /* It must still be possible to apply SMP alternatives. */
603         if (num_possible_cpus() > 1)
604                 start = (unsigned long)_etext;
605 #endif
606
607 #ifdef CONFIG_KPROBES
608         start = (unsigned long)__start_rodata;
609 #endif
610
611         end = (unsigned long)__end_rodata;
612         start = (start + PAGE_SIZE - 1) & PAGE_MASK;
613         end &= PAGE_MASK;
614         if (end <= start)
615                 return;
616
617
618         printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
619                (end - start) >> 10);
620         set_memory_ro(start, (end - start) >> PAGE_SHIFT);
621
622         /*
623          * The rodata section (but not the kernel text!) should also be
624          * not-executable.
625          */
626         start = ((unsigned long)__start_rodata + PAGE_SIZE - 1) & PAGE_MASK;
627         set_memory_nx(start, (end - start) >> PAGE_SHIFT);
628
629         rodata_test();
630
631 #ifdef CONFIG_CPA_DEBUG
632         printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end);
633         set_memory_rw(start, (end-start) >> PAGE_SHIFT);
634
635         printk(KERN_INFO "Testing CPA: again\n");
636         set_memory_ro(start, (end-start) >> PAGE_SHIFT);
637 #endif
638 }
639 #endif
640
641 #ifdef CONFIG_BLK_DEV_INITRD
642 void free_initrd_mem(unsigned long start, unsigned long end)
643 {
644         free_init_pages("initrd memory", start, end);
645 }
646 #endif
647
648 void __init reserve_bootmem_generic(unsigned long phys, unsigned len)
649 {
650 #ifdef CONFIG_NUMA
651         int nid = phys_to_nid(phys);
652 #endif
653         unsigned long pfn = phys >> PAGE_SHIFT;
654
655         if (pfn >= end_pfn) {
656                 /*
657                  * This can happen with kdump kernels when accessing
658                  * firmware tables:
659                  */
660                 if (pfn < end_pfn_map)
661                         return;
662
663                 printk(KERN_ERR "reserve_bootmem: illegal reserve %lx %u\n",
664                                 phys, len);
665                 return;
666         }
667
668         /* Should check here against the e820 map to avoid double free */
669 #ifdef CONFIG_NUMA
670         reserve_bootmem_node(NODE_DATA(nid), phys, len, BOOTMEM_DEFAULT);
671 #else
672         reserve_bootmem(phys, len, BOOTMEM_DEFAULT);
673 #endif
674         if (phys+len <= MAX_DMA_PFN*PAGE_SIZE) {
675                 dma_reserve += len / PAGE_SIZE;
676                 set_dma_reserve(dma_reserve);
677         }
678 }
679
680 int kern_addr_valid(unsigned long addr)
681 {
682         unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
683         pgd_t *pgd;
684         pud_t *pud;
685         pmd_t *pmd;
686         pte_t *pte;
687
688         if (above != 0 && above != -1UL)
689                 return 0;
690
691         pgd = pgd_offset_k(addr);
692         if (pgd_none(*pgd))
693                 return 0;
694
695         pud = pud_offset(pgd, addr);
696         if (pud_none(*pud))
697                 return 0;
698
699         pmd = pmd_offset(pud, addr);
700         if (pmd_none(*pmd))
701                 return 0;
702
703         if (pmd_large(*pmd))
704                 return pfn_valid(pmd_pfn(*pmd));
705
706         pte = pte_offset_kernel(pmd, addr);
707         if (pte_none(*pte))
708                 return 0;
709
710         return pfn_valid(pte_pfn(*pte));
711 }
712
713 /*
714  * A pseudo VMA to allow ptrace access for the vsyscall page.  This only
715  * covers the 64bit vsyscall page now. 32bit has a real VMA now and does
716  * not need special handling anymore:
717  */
718 static struct vm_area_struct gate_vma = {
719         .vm_start       = VSYSCALL_START,
720         .vm_end         = VSYSCALL_START + (VSYSCALL_MAPPED_PAGES * PAGE_SIZE),
721         .vm_page_prot   = PAGE_READONLY_EXEC,
722         .vm_flags       = VM_READ | VM_EXEC
723 };
724
725 struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
726 {
727 #ifdef CONFIG_IA32_EMULATION
728         if (test_tsk_thread_flag(tsk, TIF_IA32))
729                 return NULL;
730 #endif
731         return &gate_vma;
732 }
733
734 int in_gate_area(struct task_struct *task, unsigned long addr)
735 {
736         struct vm_area_struct *vma = get_gate_vma(task);
737
738         if (!vma)
739                 return 0;
740
741         return (addr >= vma->vm_start) && (addr < vma->vm_end);
742 }
743
744 /*
745  * Use this when you have no reliable task/vma, typically from interrupt
746  * context. It is less reliable than using the task's vma and may give
747  * false positives:
748  */
749 int in_gate_area_no_task(unsigned long addr)
750 {
751         return (addr >= VSYSCALL_START) && (addr < VSYSCALL_END);
752 }
753
754 const char *arch_vma_name(struct vm_area_struct *vma)
755 {
756         if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
757                 return "[vdso]";
758         if (vma == &gate_vma)
759                 return "[vsyscall]";
760         return NULL;
761 }
762
763 #ifdef CONFIG_SPARSEMEM_VMEMMAP
764 /*
765  * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
766  */
767 int __meminit
768 vmemmap_populate(struct page *start_page, unsigned long size, int node)
769 {
770         unsigned long addr = (unsigned long)start_page;
771         unsigned long end = (unsigned long)(start_page + size);
772         unsigned long next;
773         pgd_t *pgd;
774         pud_t *pud;
775         pmd_t *pmd;
776
777         for (; addr < end; addr = next) {
778                 next = pmd_addr_end(addr, end);
779
780                 pgd = vmemmap_pgd_populate(addr, node);
781                 if (!pgd)
782                         return -ENOMEM;
783
784                 pud = vmemmap_pud_populate(pgd, addr, node);
785                 if (!pud)
786                         return -ENOMEM;
787
788                 pmd = pmd_offset(pud, addr);
789                 if (pmd_none(*pmd)) {
790                         pte_t entry;
791                         void *p;
792
793                         p = vmemmap_alloc_block(PMD_SIZE, node);
794                         if (!p)
795                                 return -ENOMEM;
796
797                         entry = pfn_pte(__pa(p) >> PAGE_SHIFT,
798                                                         PAGE_KERNEL_LARGE);
799                         set_pmd(pmd, __pmd(pte_val(entry)));
800
801                         printk(KERN_DEBUG " [%lx-%lx] PMD ->%p on node %d\n",
802                                 addr, addr + PMD_SIZE - 1, p, node);
803                 } else {
804                         vmemmap_verify((pte_t *)pmd, node, addr, next);
805                 }
806         }
807         return 0;
808 }
809 #endif