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