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