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