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