Merge commit 'v2.6.36-rc3' into x86/memblock
[pandora-kernel.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@ucw.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/memblock.h>
25 #include <linux/proc_fs.h>
26 #include <linux/pci.h>
27 #include <linux/pfn.h>
28 #include <linux/poison.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/module.h>
31 #include <linux/memory_hotplug.h>
32 #include <linux/nmi.h>
33 #include <linux/gfp.h>
34
35 #include <asm/processor.h>
36 #include <asm/bios_ebda.h>
37 #include <asm/system.h>
38 #include <asm/uaccess.h>
39 #include <asm/pgtable.h>
40 #include <asm/pgalloc.h>
41 #include <asm/dma.h>
42 #include <asm/fixmap.h>
43 #include <asm/e820.h>
44 #include <asm/apic.h>
45 #include <asm/tlb.h>
46 #include <asm/mmu_context.h>
47 #include <asm/proto.h>
48 #include <asm/smp.h>
49 #include <asm/sections.h>
50 #include <asm/kdebug.h>
51 #include <asm/numa.h>
52 #include <asm/cacheflush.h>
53 #include <asm/init.h>
54 #include <linux/bootmem.h>
55
56 static int __init parse_direct_gbpages_off(char *arg)
57 {
58         direct_gbpages = 0;
59         return 0;
60 }
61 early_param("nogbpages", parse_direct_gbpages_off);
62
63 static int __init parse_direct_gbpages_on(char *arg)
64 {
65         direct_gbpages = 1;
66         return 0;
67 }
68 early_param("gbpages", parse_direct_gbpages_on);
69
70 /*
71  * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the
72  * physical space so we can cache the place of the first one and move
73  * around without checking the pgd every time.
74  */
75
76 pteval_t __supported_pte_mask __read_mostly = ~_PAGE_IOMAP;
77 EXPORT_SYMBOL_GPL(__supported_pte_mask);
78
79 int force_personality32;
80
81 /*
82  * noexec32=on|off
83  * Control non executable heap for 32bit processes.
84  * To control the stack too use noexec=off
85  *
86  * on   PROT_READ does not imply PROT_EXEC for 32-bit processes (default)
87  * off  PROT_READ implies PROT_EXEC
88  */
89 static int __init nonx32_setup(char *str)
90 {
91         if (!strcmp(str, "on"))
92                 force_personality32 &= ~READ_IMPLIES_EXEC;
93         else if (!strcmp(str, "off"))
94                 force_personality32 |= READ_IMPLIES_EXEC;
95         return 1;
96 }
97 __setup("noexec32=", nonx32_setup);
98
99 /*
100  * NOTE: This function is marked __ref because it calls __init function
101  * (alloc_bootmem_pages). It's safe to do it ONLY when after_bootmem == 0.
102  */
103 static __ref void *spp_getpage(void)
104 {
105         void *ptr;
106
107         if (after_bootmem)
108                 ptr = (void *) get_zeroed_page(GFP_ATOMIC | __GFP_NOTRACK);
109         else
110                 ptr = alloc_bootmem_pages(PAGE_SIZE);
111
112         if (!ptr || ((unsigned long)ptr & ~PAGE_MASK)) {
113                 panic("set_pte_phys: cannot allocate page data %s\n",
114                         after_bootmem ? "after bootmem" : "");
115         }
116
117         pr_debug("spp_getpage %p\n", ptr);
118
119         return ptr;
120 }
121
122 static pud_t *fill_pud(pgd_t *pgd, unsigned long vaddr)
123 {
124         if (pgd_none(*pgd)) {
125                 pud_t *pud = (pud_t *)spp_getpage();
126                 pgd_populate(&init_mm, pgd, pud);
127                 if (pud != pud_offset(pgd, 0))
128                         printk(KERN_ERR "PAGETABLE BUG #00! %p <-> %p\n",
129                                pud, pud_offset(pgd, 0));
130         }
131         return pud_offset(pgd, vaddr);
132 }
133
134 static pmd_t *fill_pmd(pud_t *pud, unsigned long vaddr)
135 {
136         if (pud_none(*pud)) {
137                 pmd_t *pmd = (pmd_t *) spp_getpage();
138                 pud_populate(&init_mm, pud, pmd);
139                 if (pmd != pmd_offset(pud, 0))
140                         printk(KERN_ERR "PAGETABLE BUG #01! %p <-> %p\n",
141                                pmd, pmd_offset(pud, 0));
142         }
143         return pmd_offset(pud, vaddr);
144 }
145
146 static pte_t *fill_pte(pmd_t *pmd, unsigned long vaddr)
147 {
148         if (pmd_none(*pmd)) {
149                 pte_t *pte = (pte_t *) spp_getpage();
150                 pmd_populate_kernel(&init_mm, pmd, pte);
151                 if (pte != pte_offset_kernel(pmd, 0))
152                         printk(KERN_ERR "PAGETABLE BUG #02!\n");
153         }
154         return pte_offset_kernel(pmd, vaddr);
155 }
156
157 void set_pte_vaddr_pud(pud_t *pud_page, unsigned long vaddr, pte_t new_pte)
158 {
159         pud_t *pud;
160         pmd_t *pmd;
161         pte_t *pte;
162
163         pud = pud_page + pud_index(vaddr);
164         pmd = fill_pmd(pud, vaddr);
165         pte = fill_pte(pmd, vaddr);
166
167         set_pte(pte, new_pte);
168
169         /*
170          * It's enough to flush this one mapping.
171          * (PGE mappings get flushed as well)
172          */
173         __flush_tlb_one(vaddr);
174 }
175
176 void set_pte_vaddr(unsigned long vaddr, pte_t pteval)
177 {
178         pgd_t *pgd;
179         pud_t *pud_page;
180
181         pr_debug("set_pte_vaddr %lx to %lx\n", vaddr, native_pte_val(pteval));
182
183         pgd = pgd_offset_k(vaddr);
184         if (pgd_none(*pgd)) {
185                 printk(KERN_ERR
186                         "PGD FIXMAP MISSING, it should be setup in head.S!\n");
187                 return;
188         }
189         pud_page = (pud_t*)pgd_page_vaddr(*pgd);
190         set_pte_vaddr_pud(pud_page, vaddr, pteval);
191 }
192
193 pmd_t * __init populate_extra_pmd(unsigned long vaddr)
194 {
195         pgd_t *pgd;
196         pud_t *pud;
197
198         pgd = pgd_offset_k(vaddr);
199         pud = fill_pud(pgd, vaddr);
200         return fill_pmd(pud, vaddr);
201 }
202
203 pte_t * __init populate_extra_pte(unsigned long vaddr)
204 {
205         pmd_t *pmd;
206
207         pmd = populate_extra_pmd(vaddr);
208         return fill_pte(pmd, vaddr);
209 }
210
211 /*
212  * Create large page table mappings for a range of physical addresses.
213  */
214 static void __init __init_extra_mapping(unsigned long phys, unsigned long size,
215                                                 pgprot_t prot)
216 {
217         pgd_t *pgd;
218         pud_t *pud;
219         pmd_t *pmd;
220
221         BUG_ON((phys & ~PMD_MASK) || (size & ~PMD_MASK));
222         for (; size; phys += PMD_SIZE, size -= PMD_SIZE) {
223                 pgd = pgd_offset_k((unsigned long)__va(phys));
224                 if (pgd_none(*pgd)) {
225                         pud = (pud_t *) spp_getpage();
226                         set_pgd(pgd, __pgd(__pa(pud) | _KERNPG_TABLE |
227                                                 _PAGE_USER));
228                 }
229                 pud = pud_offset(pgd, (unsigned long)__va(phys));
230                 if (pud_none(*pud)) {
231                         pmd = (pmd_t *) spp_getpage();
232                         set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE |
233                                                 _PAGE_USER));
234                 }
235                 pmd = pmd_offset(pud, phys);
236                 BUG_ON(!pmd_none(*pmd));
237                 set_pmd(pmd, __pmd(phys | pgprot_val(prot)));
238         }
239 }
240
241 void __init init_extra_mapping_wb(unsigned long phys, unsigned long size)
242 {
243         __init_extra_mapping(phys, size, PAGE_KERNEL_LARGE);
244 }
245
246 void __init init_extra_mapping_uc(unsigned long phys, unsigned long size)
247 {
248         __init_extra_mapping(phys, size, PAGE_KERNEL_LARGE_NOCACHE);
249 }
250
251 /*
252  * The head.S code sets up the kernel high mapping:
253  *
254  *   from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text)
255  *
256  * phys_addr holds the negative offset to the kernel, which is added
257  * to the compile time generated pmds. This results in invalid pmds up
258  * to the point where we hit the physaddr 0 mapping.
259  *
260  * We limit the mappings to the region from _text to _end.  _end is
261  * rounded up to the 2MB boundary. This catches the invalid pmds as
262  * well, as they are located before _text:
263  */
264 void __init cleanup_highmap(void)
265 {
266         unsigned long vaddr = __START_KERNEL_map;
267         unsigned long end = roundup((unsigned long)_end, PMD_SIZE) - 1;
268         pmd_t *pmd = level2_kernel_pgt;
269         pmd_t *last_pmd = pmd + PTRS_PER_PMD;
270
271         for (; pmd < last_pmd; pmd++, vaddr += PMD_SIZE) {
272                 if (pmd_none(*pmd))
273                         continue;
274                 if (vaddr < (unsigned long) _text || vaddr > end)
275                         set_pmd(pmd, __pmd(0));
276         }
277 }
278
279 static __ref void *alloc_low_page(unsigned long *phys)
280 {
281         unsigned long pfn = e820_table_end++;
282         void *adr;
283
284         if (after_bootmem) {
285                 adr = (void *)get_zeroed_page(GFP_ATOMIC | __GFP_NOTRACK);
286                 *phys = __pa(adr);
287
288                 return adr;
289         }
290
291         if (pfn >= e820_table_top)
292                 panic("alloc_low_page: ran out of memory");
293
294         adr = early_memremap(pfn * PAGE_SIZE, PAGE_SIZE);
295         memset(adr, 0, PAGE_SIZE);
296         *phys  = pfn * PAGE_SIZE;
297         return adr;
298 }
299
300 static __ref void unmap_low_page(void *adr)
301 {
302         if (after_bootmem)
303                 return;
304
305         early_iounmap(adr, PAGE_SIZE);
306 }
307
308 static unsigned long __meminit
309 phys_pte_init(pte_t *pte_page, unsigned long addr, unsigned long end,
310               pgprot_t prot)
311 {
312         unsigned pages = 0;
313         unsigned long last_map_addr = end;
314         int i;
315
316         pte_t *pte = pte_page + pte_index(addr);
317
318         for(i = pte_index(addr); i < PTRS_PER_PTE; i++, addr += PAGE_SIZE, pte++) {
319
320                 if (addr >= end) {
321                         if (!after_bootmem) {
322                                 for(; i < PTRS_PER_PTE; i++, pte++)
323                                         set_pte(pte, __pte(0));
324                         }
325                         break;
326                 }
327
328                 /*
329                  * We will re-use the existing mapping.
330                  * Xen for example has some special requirements, like mapping
331                  * pagetable pages as RO. So assume someone who pre-setup
332                  * these mappings are more intelligent.
333                  */
334                 if (pte_val(*pte)) {
335                         pages++;
336                         continue;
337                 }
338
339                 if (0)
340                         printk("   pte=%p addr=%lx pte=%016lx\n",
341                                pte, addr, pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL).pte);
342                 pages++;
343                 set_pte(pte, pfn_pte(addr >> PAGE_SHIFT, prot));
344                 last_map_addr = (addr & PAGE_MASK) + PAGE_SIZE;
345         }
346
347         update_page_count(PG_LEVEL_4K, pages);
348
349         return last_map_addr;
350 }
351
352 static unsigned long __meminit
353 phys_pte_update(pmd_t *pmd, unsigned long address, unsigned long end,
354                 pgprot_t prot)
355 {
356         pte_t *pte = (pte_t *)pmd_page_vaddr(*pmd);
357
358         return phys_pte_init(pte, address, end, prot);
359 }
360
361 static unsigned long __meminit
362 phys_pmd_init(pmd_t *pmd_page, unsigned long address, unsigned long end,
363               unsigned long page_size_mask, pgprot_t prot)
364 {
365         unsigned long pages = 0;
366         unsigned long last_map_addr = end;
367
368         int i = pmd_index(address);
369
370         for (; i < PTRS_PER_PMD; i++, address += PMD_SIZE) {
371                 unsigned long pte_phys;
372                 pmd_t *pmd = pmd_page + pmd_index(address);
373                 pte_t *pte;
374                 pgprot_t new_prot = prot;
375
376                 if (address >= end) {
377                         if (!after_bootmem) {
378                                 for (; i < PTRS_PER_PMD; i++, pmd++)
379                                         set_pmd(pmd, __pmd(0));
380                         }
381                         break;
382                 }
383
384                 if (pmd_val(*pmd)) {
385                         if (!pmd_large(*pmd)) {
386                                 spin_lock(&init_mm.page_table_lock);
387                                 last_map_addr = phys_pte_update(pmd, address,
388                                                                 end, prot);
389                                 spin_unlock(&init_mm.page_table_lock);
390                                 continue;
391                         }
392                         /*
393                          * If we are ok with PG_LEVEL_2M mapping, then we will
394                          * use the existing mapping,
395                          *
396                          * Otherwise, we will split the large page mapping but
397                          * use the same existing protection bits except for
398                          * large page, so that we don't violate Intel's TLB
399                          * Application note (317080) which says, while changing
400                          * the page sizes, new and old translations should
401                          * not differ with respect to page frame and
402                          * attributes.
403                          */
404                         if (page_size_mask & (1 << PG_LEVEL_2M)) {
405                                 pages++;
406                                 continue;
407                         }
408                         new_prot = pte_pgprot(pte_clrhuge(*(pte_t *)pmd));
409                 }
410
411                 if (page_size_mask & (1<<PG_LEVEL_2M)) {
412                         pages++;
413                         spin_lock(&init_mm.page_table_lock);
414                         set_pte((pte_t *)pmd,
415                                 pfn_pte(address >> PAGE_SHIFT,
416                                         __pgprot(pgprot_val(prot) | _PAGE_PSE)));
417                         spin_unlock(&init_mm.page_table_lock);
418                         last_map_addr = (address & PMD_MASK) + PMD_SIZE;
419                         continue;
420                 }
421
422                 pte = alloc_low_page(&pte_phys);
423                 last_map_addr = phys_pte_init(pte, address, end, new_prot);
424                 unmap_low_page(pte);
425
426                 spin_lock(&init_mm.page_table_lock);
427                 pmd_populate_kernel(&init_mm, pmd, __va(pte_phys));
428                 spin_unlock(&init_mm.page_table_lock);
429         }
430         update_page_count(PG_LEVEL_2M, pages);
431         return last_map_addr;
432 }
433
434 static unsigned long __meminit
435 phys_pmd_update(pud_t *pud, unsigned long address, unsigned long end,
436                 unsigned long page_size_mask, pgprot_t prot)
437 {
438         pmd_t *pmd = pmd_offset(pud, 0);
439         unsigned long last_map_addr;
440
441         last_map_addr = phys_pmd_init(pmd, address, end, page_size_mask, prot);
442         __flush_tlb_all();
443         return last_map_addr;
444 }
445
446 static unsigned long __meminit
447 phys_pud_init(pud_t *pud_page, unsigned long addr, unsigned long end,
448                          unsigned long page_size_mask)
449 {
450         unsigned long pages = 0;
451         unsigned long last_map_addr = end;
452         int i = pud_index(addr);
453
454         for (; i < PTRS_PER_PUD; i++, addr = (addr & PUD_MASK) + PUD_SIZE) {
455                 unsigned long pmd_phys;
456                 pud_t *pud = pud_page + pud_index(addr);
457                 pmd_t *pmd;
458                 pgprot_t prot = PAGE_KERNEL;
459
460                 if (addr >= end)
461                         break;
462
463                 if (!after_bootmem &&
464                                 !e820_any_mapped(addr, addr+PUD_SIZE, 0)) {
465                         set_pud(pud, __pud(0));
466                         continue;
467                 }
468
469                 if (pud_val(*pud)) {
470                         if (!pud_large(*pud)) {
471                                 last_map_addr = phys_pmd_update(pud, addr, end,
472                                                          page_size_mask, prot);
473                                 continue;
474                         }
475                         /*
476                          * If we are ok with PG_LEVEL_1G mapping, then we will
477                          * use the existing mapping.
478                          *
479                          * Otherwise, we will split the gbpage mapping but use
480                          * the same existing protection  bits except for large
481                          * page, so that we don't violate Intel's TLB
482                          * Application note (317080) which says, while changing
483                          * the page sizes, new and old translations should
484                          * not differ with respect to page frame and
485                          * attributes.
486                          */
487                         if (page_size_mask & (1 << PG_LEVEL_1G)) {
488                                 pages++;
489                                 continue;
490                         }
491                         prot = pte_pgprot(pte_clrhuge(*(pte_t *)pud));
492                 }
493
494                 if (page_size_mask & (1<<PG_LEVEL_1G)) {
495                         pages++;
496                         spin_lock(&init_mm.page_table_lock);
497                         set_pte((pte_t *)pud,
498                                 pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL_LARGE));
499                         spin_unlock(&init_mm.page_table_lock);
500                         last_map_addr = (addr & PUD_MASK) + PUD_SIZE;
501                         continue;
502                 }
503
504                 pmd = alloc_low_page(&pmd_phys);
505                 last_map_addr = phys_pmd_init(pmd, addr, end, page_size_mask,
506                                               prot);
507                 unmap_low_page(pmd);
508
509                 spin_lock(&init_mm.page_table_lock);
510                 pud_populate(&init_mm, pud, __va(pmd_phys));
511                 spin_unlock(&init_mm.page_table_lock);
512         }
513         __flush_tlb_all();
514
515         update_page_count(PG_LEVEL_1G, pages);
516
517         return last_map_addr;
518 }
519
520 static unsigned long __meminit
521 phys_pud_update(pgd_t *pgd, unsigned long addr, unsigned long end,
522                  unsigned long page_size_mask)
523 {
524         pud_t *pud;
525
526         pud = (pud_t *)pgd_page_vaddr(*pgd);
527
528         return phys_pud_init(pud, addr, end, page_size_mask);
529 }
530
531 unsigned long __meminit
532 kernel_physical_mapping_init(unsigned long start,
533                              unsigned long end,
534                              unsigned long page_size_mask)
535 {
536
537         unsigned long next, last_map_addr = end;
538
539         start = (unsigned long)__va(start);
540         end = (unsigned long)__va(end);
541
542         for (; start < end; start = next) {
543                 pgd_t *pgd = pgd_offset_k(start);
544                 unsigned long pud_phys;
545                 pud_t *pud;
546
547                 next = (start + PGDIR_SIZE) & PGDIR_MASK;
548                 if (next > end)
549                         next = end;
550
551                 if (pgd_val(*pgd)) {
552                         last_map_addr = phys_pud_update(pgd, __pa(start),
553                                                  __pa(end), page_size_mask);
554                         continue;
555                 }
556
557                 pud = alloc_low_page(&pud_phys);
558                 last_map_addr = phys_pud_init(pud, __pa(start), __pa(next),
559                                                  page_size_mask);
560                 unmap_low_page(pud);
561
562                 spin_lock(&init_mm.page_table_lock);
563                 pgd_populate(&init_mm, pgd, __va(pud_phys));
564                 spin_unlock(&init_mm.page_table_lock);
565         }
566         __flush_tlb_all();
567
568         return last_map_addr;
569 }
570
571 #ifndef CONFIG_NUMA
572 void __init initmem_init(unsigned long start_pfn, unsigned long end_pfn,
573                                 int acpi, int k8)
574 {
575         memblock_x86_register_active_regions(0, start_pfn, end_pfn);
576 }
577 #endif
578
579 void __init paging_init(void)
580 {
581         unsigned long max_zone_pfns[MAX_NR_ZONES];
582
583         memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
584         max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
585         max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
586         max_zone_pfns[ZONE_NORMAL] = max_pfn;
587
588         sparse_memory_present_with_active_regions(MAX_NUMNODES);
589         sparse_init();
590
591         /*
592          * clear the default setting with node 0
593          * note: don't use nodes_clear here, that is really clearing when
594          *       numa support is not compiled in, and later node_set_state
595          *       will not set it back.
596          */
597         node_clear_state(0, N_NORMAL_MEMORY);
598
599         free_area_init_nodes(max_zone_pfns);
600 }
601
602 /*
603  * Memory hotplug specific functions
604  */
605 #ifdef CONFIG_MEMORY_HOTPLUG
606 /*
607  * After memory hotplug the variables max_pfn, max_low_pfn and high_memory need
608  * updating.
609  */
610 static void  update_end_of_memory_vars(u64 start, u64 size)
611 {
612         unsigned long end_pfn = PFN_UP(start + size);
613
614         if (end_pfn > max_pfn) {
615                 max_pfn = end_pfn;
616                 max_low_pfn = end_pfn;
617                 high_memory = (void *)__va(max_pfn * PAGE_SIZE - 1) + 1;
618         }
619 }
620
621 /*
622  * Memory is added always to NORMAL zone. This means you will never get
623  * additional DMA/DMA32 memory.
624  */
625 int arch_add_memory(int nid, u64 start, u64 size)
626 {
627         struct pglist_data *pgdat = NODE_DATA(nid);
628         struct zone *zone = pgdat->node_zones + ZONE_NORMAL;
629         unsigned long last_mapped_pfn, start_pfn = start >> PAGE_SHIFT;
630         unsigned long nr_pages = size >> PAGE_SHIFT;
631         int ret;
632
633         last_mapped_pfn = init_memory_mapping(start, start + size);
634         if (last_mapped_pfn > max_pfn_mapped)
635                 max_pfn_mapped = last_mapped_pfn;
636
637         ret = __add_pages(nid, zone, start_pfn, nr_pages);
638         WARN_ON_ONCE(ret);
639
640         /* update max_pfn, max_low_pfn and high_memory */
641         update_end_of_memory_vars(start, size);
642
643         return ret;
644 }
645 EXPORT_SYMBOL_GPL(arch_add_memory);
646
647 #if !defined(CONFIG_ACPI_NUMA) && defined(CONFIG_NUMA)
648 int memory_add_physaddr_to_nid(u64 start)
649 {
650         return 0;
651 }
652 EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
653 #endif
654
655 #endif /* CONFIG_MEMORY_HOTPLUG */
656
657 static struct kcore_list kcore_vsyscall;
658
659 void __init mem_init(void)
660 {
661         long codesize, reservedpages, datasize, initsize;
662         unsigned long absent_pages;
663
664         pci_iommu_alloc();
665
666         /* clear_bss() already clear the empty_zero_page */
667
668         reservedpages = 0;
669
670         /* this will put all low memory onto the freelists */
671 #ifdef CONFIG_NUMA
672         totalram_pages = numa_free_all_bootmem();
673 #else
674         totalram_pages = free_all_bootmem();
675 #endif
676
677         absent_pages = absent_pages_in_range(0, max_pfn);
678         reservedpages = max_pfn - totalram_pages - absent_pages;
679         after_bootmem = 1;
680
681         codesize =  (unsigned long) &_etext - (unsigned long) &_text;
682         datasize =  (unsigned long) &_edata - (unsigned long) &_etext;
683         initsize =  (unsigned long) &__init_end - (unsigned long) &__init_begin;
684
685         /* Register memory areas for /proc/kcore */
686         kclist_add(&kcore_vsyscall, (void *)VSYSCALL_START,
687                          VSYSCALL_END - VSYSCALL_START, KCORE_OTHER);
688
689         printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, "
690                          "%ldk absent, %ldk reserved, %ldk data, %ldk init)\n",
691                 nr_free_pages() << (PAGE_SHIFT-10),
692                 max_pfn << (PAGE_SHIFT-10),
693                 codesize >> 10,
694                 absent_pages << (PAGE_SHIFT-10),
695                 reservedpages << (PAGE_SHIFT-10),
696                 datasize >> 10,
697                 initsize >> 10);
698 }
699
700 #ifdef CONFIG_DEBUG_RODATA
701 const int rodata_test_data = 0xC3;
702 EXPORT_SYMBOL_GPL(rodata_test_data);
703
704 int kernel_set_to_readonly;
705
706 void set_kernel_text_rw(void)
707 {
708         unsigned long start = PFN_ALIGN(_text);
709         unsigned long end = PFN_ALIGN(__stop___ex_table);
710
711         if (!kernel_set_to_readonly)
712                 return;
713
714         pr_debug("Set kernel text: %lx - %lx for read write\n",
715                  start, end);
716
717         /*
718          * Make the kernel identity mapping for text RW. Kernel text
719          * mapping will always be RO. Refer to the comment in
720          * static_protections() in pageattr.c
721          */
722         set_memory_rw(start, (end - start) >> PAGE_SHIFT);
723 }
724
725 void set_kernel_text_ro(void)
726 {
727         unsigned long start = PFN_ALIGN(_text);
728         unsigned long end = PFN_ALIGN(__stop___ex_table);
729
730         if (!kernel_set_to_readonly)
731                 return;
732
733         pr_debug("Set kernel text: %lx - %lx for read only\n",
734                  start, end);
735
736         /*
737          * Set the kernel identity mapping for text RO.
738          */
739         set_memory_ro(start, (end - start) >> PAGE_SHIFT);
740 }
741
742 void mark_rodata_ro(void)
743 {
744         unsigned long start = PFN_ALIGN(_text);
745         unsigned long rodata_start =
746                 ((unsigned long)__start_rodata + PAGE_SIZE - 1) & PAGE_MASK;
747         unsigned long end = (unsigned long) &__end_rodata_hpage_align;
748         unsigned long text_end = PAGE_ALIGN((unsigned long) &__stop___ex_table);
749         unsigned long rodata_end = PAGE_ALIGN((unsigned long) &__end_rodata);
750         unsigned long data_start = (unsigned long) &_sdata;
751
752         printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
753                (end - start) >> 10);
754         set_memory_ro(start, (end - start) >> PAGE_SHIFT);
755
756         kernel_set_to_readonly = 1;
757
758         /*
759          * The rodata section (but not the kernel text!) should also be
760          * not-executable.
761          */
762         set_memory_nx(rodata_start, (end - rodata_start) >> PAGE_SHIFT);
763
764         rodata_test();
765
766 #ifdef CONFIG_CPA_DEBUG
767         printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end);
768         set_memory_rw(start, (end-start) >> PAGE_SHIFT);
769
770         printk(KERN_INFO "Testing CPA: again\n");
771         set_memory_ro(start, (end-start) >> PAGE_SHIFT);
772 #endif
773
774         free_init_pages("unused kernel memory",
775                         (unsigned long) page_address(virt_to_page(text_end)),
776                         (unsigned long)
777                                  page_address(virt_to_page(rodata_start)));
778         free_init_pages("unused kernel memory",
779                         (unsigned long) page_address(virt_to_page(rodata_end)),
780                         (unsigned long) page_address(virt_to_page(data_start)));
781 }
782
783 #endif
784
785 int kern_addr_valid(unsigned long addr)
786 {
787         unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
788         pgd_t *pgd;
789         pud_t *pud;
790         pmd_t *pmd;
791         pte_t *pte;
792
793         if (above != 0 && above != -1UL)
794                 return 0;
795
796         pgd = pgd_offset_k(addr);
797         if (pgd_none(*pgd))
798                 return 0;
799
800         pud = pud_offset(pgd, addr);
801         if (pud_none(*pud))
802                 return 0;
803
804         pmd = pmd_offset(pud, addr);
805         if (pmd_none(*pmd))
806                 return 0;
807
808         if (pmd_large(*pmd))
809                 return pfn_valid(pmd_pfn(*pmd));
810
811         pte = pte_offset_kernel(pmd, addr);
812         if (pte_none(*pte))
813                 return 0;
814
815         return pfn_valid(pte_pfn(*pte));
816 }
817
818 /*
819  * A pseudo VMA to allow ptrace access for the vsyscall page.  This only
820  * covers the 64bit vsyscall page now. 32bit has a real VMA now and does
821  * not need special handling anymore:
822  */
823 static struct vm_area_struct gate_vma = {
824         .vm_start       = VSYSCALL_START,
825         .vm_end         = VSYSCALL_START + (VSYSCALL_MAPPED_PAGES * PAGE_SIZE),
826         .vm_page_prot   = PAGE_READONLY_EXEC,
827         .vm_flags       = VM_READ | VM_EXEC
828 };
829
830 struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
831 {
832 #ifdef CONFIG_IA32_EMULATION
833         if (test_tsk_thread_flag(tsk, TIF_IA32))
834                 return NULL;
835 #endif
836         return &gate_vma;
837 }
838
839 int in_gate_area(struct task_struct *task, unsigned long addr)
840 {
841         struct vm_area_struct *vma = get_gate_vma(task);
842
843         if (!vma)
844                 return 0;
845
846         return (addr >= vma->vm_start) && (addr < vma->vm_end);
847 }
848
849 /*
850  * Use this when you have no reliable task/vma, typically from interrupt
851  * context. It is less reliable than using the task's vma and may give
852  * false positives:
853  */
854 int in_gate_area_no_task(unsigned long addr)
855 {
856         return (addr >= VSYSCALL_START) && (addr < VSYSCALL_END);
857 }
858
859 const char *arch_vma_name(struct vm_area_struct *vma)
860 {
861         if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
862                 return "[vdso]";
863         if (vma == &gate_vma)
864                 return "[vsyscall]";
865         return NULL;
866 }
867
868 #ifdef CONFIG_SPARSEMEM_VMEMMAP
869 /*
870  * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
871  */
872 static long __meminitdata addr_start, addr_end;
873 static void __meminitdata *p_start, *p_end;
874 static int __meminitdata node_start;
875
876 int __meminit
877 vmemmap_populate(struct page *start_page, unsigned long size, int node)
878 {
879         unsigned long addr = (unsigned long)start_page;
880         unsigned long end = (unsigned long)(start_page + size);
881         unsigned long next;
882         pgd_t *pgd;
883         pud_t *pud;
884         pmd_t *pmd;
885
886         for (; addr < end; addr = next) {
887                 void *p = NULL;
888
889                 pgd = vmemmap_pgd_populate(addr, node);
890                 if (!pgd)
891                         return -ENOMEM;
892
893                 pud = vmemmap_pud_populate(pgd, addr, node);
894                 if (!pud)
895                         return -ENOMEM;
896
897                 if (!cpu_has_pse) {
898                         next = (addr + PAGE_SIZE) & PAGE_MASK;
899                         pmd = vmemmap_pmd_populate(pud, addr, node);
900
901                         if (!pmd)
902                                 return -ENOMEM;
903
904                         p = vmemmap_pte_populate(pmd, addr, node);
905
906                         if (!p)
907                                 return -ENOMEM;
908
909                         addr_end = addr + PAGE_SIZE;
910                         p_end = p + PAGE_SIZE;
911                 } else {
912                         next = pmd_addr_end(addr, end);
913
914                         pmd = pmd_offset(pud, addr);
915                         if (pmd_none(*pmd)) {
916                                 pte_t entry;
917
918                                 p = vmemmap_alloc_block_buf(PMD_SIZE, node);
919                                 if (!p)
920                                         return -ENOMEM;
921
922                                 entry = pfn_pte(__pa(p) >> PAGE_SHIFT,
923                                                 PAGE_KERNEL_LARGE);
924                                 set_pmd(pmd, __pmd(pte_val(entry)));
925
926                                 /* check to see if we have contiguous blocks */
927                                 if (p_end != p || node_start != node) {
928                                         if (p_start)
929                                                 printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n",
930                                                        addr_start, addr_end-1, p_start, p_end-1, node_start);
931                                         addr_start = addr;
932                                         node_start = node;
933                                         p_start = p;
934                                 }
935
936                                 addr_end = addr + PMD_SIZE;
937                                 p_end = p + PMD_SIZE;
938                         } else
939                                 vmemmap_verify((pte_t *)pmd, node, addr, next);
940                 }
941
942         }
943         return 0;
944 }
945
946 void __meminit vmemmap_populate_print_last(void)
947 {
948         if (p_start) {
949                 printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n",
950                         addr_start, addr_end-1, p_start, p_end-1, node_start);
951                 p_start = NULL;
952                 p_end = NULL;
953                 node_start = 0;
954         }
955 }
956 #endif