a15abaae5ba41d3e4614eb4a98d1b581dc0cfc56
[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         unsigned long bootmap_size, bootmap;
576
577         bootmap_size = bootmem_bootmap_pages(end_pfn)<<PAGE_SHIFT;
578         bootmap = find_e820_area(0, end_pfn<<PAGE_SHIFT, bootmap_size,
579                                  PAGE_SIZE);
580         if (bootmap == -1L)
581                 panic("Cannot find bootmem map of size %ld\n", bootmap_size);
582         reserve_early(bootmap, bootmap + bootmap_size, "BOOTMAP");
583         /* don't touch min_low_pfn */
584         bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap >> PAGE_SHIFT,
585                                          0, end_pfn);
586         e820_register_active_regions(0, start_pfn, end_pfn);
587         free_bootmem_with_active_regions(0, end_pfn);
588 }
589 #endif
590
591 void __init paging_init(void)
592 {
593         unsigned long max_zone_pfns[MAX_NR_ZONES];
594
595         memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
596         max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
597         max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
598         max_zone_pfns[ZONE_NORMAL] = max_pfn;
599
600         sparse_memory_present_with_active_regions(MAX_NUMNODES);
601         sparse_init();
602
603         /*
604          * clear the default setting with node 0
605          * note: don't use nodes_clear here, that is really clearing when
606          *       numa support is not compiled in, and later node_set_state
607          *       will not set it back.
608          */
609         node_clear_state(0, N_NORMAL_MEMORY);
610
611         free_area_init_nodes(max_zone_pfns);
612 }
613
614 /*
615  * Memory hotplug specific functions
616  */
617 #ifdef CONFIG_MEMORY_HOTPLUG
618 /*
619  * After memory hotplug the variables max_pfn, max_low_pfn and high_memory need
620  * updating.
621  */
622 static void  update_end_of_memory_vars(u64 start, u64 size)
623 {
624         unsigned long end_pfn = PFN_UP(start + size);
625
626         if (end_pfn > max_pfn) {
627                 max_pfn = end_pfn;
628                 max_low_pfn = end_pfn;
629                 high_memory = (void *)__va(max_pfn * PAGE_SIZE - 1) + 1;
630         }
631 }
632
633 /*
634  * Memory is added always to NORMAL zone. This means you will never get
635  * additional DMA/DMA32 memory.
636  */
637 int arch_add_memory(int nid, u64 start, u64 size)
638 {
639         struct pglist_data *pgdat = NODE_DATA(nid);
640         struct zone *zone = pgdat->node_zones + ZONE_NORMAL;
641         unsigned long last_mapped_pfn, start_pfn = start >> PAGE_SHIFT;
642         unsigned long nr_pages = size >> PAGE_SHIFT;
643         int ret;
644
645         last_mapped_pfn = init_memory_mapping(start, start + size);
646         if (last_mapped_pfn > max_pfn_mapped)
647                 max_pfn_mapped = last_mapped_pfn;
648
649         ret = __add_pages(nid, zone, start_pfn, nr_pages);
650         WARN_ON_ONCE(ret);
651
652         /* update max_pfn, max_low_pfn and high_memory */
653         update_end_of_memory_vars(start, size);
654
655         return ret;
656 }
657 EXPORT_SYMBOL_GPL(arch_add_memory);
658
659 #if !defined(CONFIG_ACPI_NUMA) && defined(CONFIG_NUMA)
660 int memory_add_physaddr_to_nid(u64 start)
661 {
662         return 0;
663 }
664 EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
665 #endif
666
667 #endif /* CONFIG_MEMORY_HOTPLUG */
668
669 static struct kcore_list kcore_vsyscall;
670
671 void __init mem_init(void)
672 {
673         long codesize, reservedpages, datasize, initsize;
674         unsigned long absent_pages;
675
676         pci_iommu_alloc();
677
678         /* clear_bss() already clear the empty_zero_page */
679
680         reservedpages = 0;
681
682         /* this will put all low memory onto the freelists */
683 #ifdef CONFIG_NUMA
684         totalram_pages = numa_free_all_bootmem();
685 #else
686         totalram_pages = free_all_bootmem();
687 #endif
688
689         absent_pages = absent_pages_in_range(0, max_pfn);
690         reservedpages = max_pfn - totalram_pages - absent_pages;
691         after_bootmem = 1;
692
693         codesize =  (unsigned long) &_etext - (unsigned long) &_text;
694         datasize =  (unsigned long) &_edata - (unsigned long) &_etext;
695         initsize =  (unsigned long) &__init_end - (unsigned long) &__init_begin;
696
697         /* Register memory areas for /proc/kcore */
698         kclist_add(&kcore_vsyscall, (void *)VSYSCALL_START,
699                          VSYSCALL_END - VSYSCALL_START, KCORE_OTHER);
700
701         printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, "
702                          "%ldk absent, %ldk reserved, %ldk data, %ldk init)\n",
703                 nr_free_pages() << (PAGE_SHIFT-10),
704                 max_pfn << (PAGE_SHIFT-10),
705                 codesize >> 10,
706                 absent_pages << (PAGE_SHIFT-10),
707                 reservedpages << (PAGE_SHIFT-10),
708                 datasize >> 10,
709                 initsize >> 10);
710 }
711
712 #ifdef CONFIG_DEBUG_RODATA
713 const int rodata_test_data = 0xC3;
714 EXPORT_SYMBOL_GPL(rodata_test_data);
715
716 int kernel_set_to_readonly;
717
718 void set_kernel_text_rw(void)
719 {
720         unsigned long start = PFN_ALIGN(_text);
721         unsigned long end = PFN_ALIGN(__stop___ex_table);
722
723         if (!kernel_set_to_readonly)
724                 return;
725
726         pr_debug("Set kernel text: %lx - %lx for read write\n",
727                  start, end);
728
729         /*
730          * Make the kernel identity mapping for text RW. Kernel text
731          * mapping will always be RO. Refer to the comment in
732          * static_protections() in pageattr.c
733          */
734         set_memory_rw(start, (end - start) >> PAGE_SHIFT);
735 }
736
737 void set_kernel_text_ro(void)
738 {
739         unsigned long start = PFN_ALIGN(_text);
740         unsigned long end = PFN_ALIGN(__stop___ex_table);
741
742         if (!kernel_set_to_readonly)
743                 return;
744
745         pr_debug("Set kernel text: %lx - %lx for read only\n",
746                  start, end);
747
748         /*
749          * Set the kernel identity mapping for text RO.
750          */
751         set_memory_ro(start, (end - start) >> PAGE_SHIFT);
752 }
753
754 void mark_rodata_ro(void)
755 {
756         unsigned long start = PFN_ALIGN(_text);
757         unsigned long rodata_start =
758                 ((unsigned long)__start_rodata + PAGE_SIZE - 1) & PAGE_MASK;
759         unsigned long end = (unsigned long) &__end_rodata_hpage_align;
760         unsigned long text_end = PAGE_ALIGN((unsigned long) &__stop___ex_table);
761         unsigned long rodata_end = PAGE_ALIGN((unsigned long) &__end_rodata);
762         unsigned long data_start = (unsigned long) &_sdata;
763
764         printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
765                (end - start) >> 10);
766         set_memory_ro(start, (end - start) >> PAGE_SHIFT);
767
768         kernel_set_to_readonly = 1;
769
770         /*
771          * The rodata section (but not the kernel text!) should also be
772          * not-executable.
773          */
774         set_memory_nx(rodata_start, (end - rodata_start) >> PAGE_SHIFT);
775
776         rodata_test();
777
778 #ifdef CONFIG_CPA_DEBUG
779         printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end);
780         set_memory_rw(start, (end-start) >> PAGE_SHIFT);
781
782         printk(KERN_INFO "Testing CPA: again\n");
783         set_memory_ro(start, (end-start) >> PAGE_SHIFT);
784 #endif
785
786         free_init_pages("unused kernel memory",
787                         (unsigned long) page_address(virt_to_page(text_end)),
788                         (unsigned long)
789                                  page_address(virt_to_page(rodata_start)));
790         free_init_pages("unused kernel memory",
791                         (unsigned long) page_address(virt_to_page(rodata_end)),
792                         (unsigned long) page_address(virt_to_page(data_start)));
793 }
794
795 #endif
796
797 int __init reserve_bootmem_generic(unsigned long phys, unsigned long len,
798                                    int flags)
799 {
800 #ifdef CONFIG_NUMA
801         int nid, next_nid;
802         int ret;
803 #endif
804         unsigned long pfn = phys >> PAGE_SHIFT;
805
806         if (pfn >= max_pfn) {
807                 /*
808                  * This can happen with kdump kernels when accessing
809                  * firmware tables:
810                  */
811                 if (pfn < max_pfn_mapped)
812                         return -EFAULT;
813
814                 printk(KERN_ERR "reserve_bootmem: illegal reserve %lx %lu\n",
815                                 phys, len);
816                 return -EFAULT;
817         }
818
819         /* Should check here against the e820 map to avoid double free */
820 #ifdef CONFIG_NUMA
821         nid = phys_to_nid(phys);
822         next_nid = phys_to_nid(phys + len - 1);
823         if (nid == next_nid)
824                 ret = reserve_bootmem_node(NODE_DATA(nid), phys, len, flags);
825         else
826                 ret = reserve_bootmem(phys, len, flags);
827
828         if (ret != 0)
829                 return ret;
830
831 #else
832         reserve_bootmem(phys, len, flags);
833 #endif
834
835         if (phys+len <= MAX_DMA_PFN*PAGE_SIZE) {
836                 dma_reserve += len / PAGE_SIZE;
837                 set_dma_reserve(dma_reserve);
838         }
839
840         return 0;
841 }
842
843 int kern_addr_valid(unsigned long addr)
844 {
845         unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
846         pgd_t *pgd;
847         pud_t *pud;
848         pmd_t *pmd;
849         pte_t *pte;
850
851         if (above != 0 && above != -1UL)
852                 return 0;
853
854         pgd = pgd_offset_k(addr);
855         if (pgd_none(*pgd))
856                 return 0;
857
858         pud = pud_offset(pgd, addr);
859         if (pud_none(*pud))
860                 return 0;
861
862         pmd = pmd_offset(pud, addr);
863         if (pmd_none(*pmd))
864                 return 0;
865
866         if (pmd_large(*pmd))
867                 return pfn_valid(pmd_pfn(*pmd));
868
869         pte = pte_offset_kernel(pmd, addr);
870         if (pte_none(*pte))
871                 return 0;
872
873         return pfn_valid(pte_pfn(*pte));
874 }
875
876 /*
877  * A pseudo VMA to allow ptrace access for the vsyscall page.  This only
878  * covers the 64bit vsyscall page now. 32bit has a real VMA now and does
879  * not need special handling anymore:
880  */
881 static struct vm_area_struct gate_vma = {
882         .vm_start       = VSYSCALL_START,
883         .vm_end         = VSYSCALL_START + (VSYSCALL_MAPPED_PAGES * PAGE_SIZE),
884         .vm_page_prot   = PAGE_READONLY_EXEC,
885         .vm_flags       = VM_READ | VM_EXEC
886 };
887
888 struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
889 {
890 #ifdef CONFIG_IA32_EMULATION
891         if (test_tsk_thread_flag(tsk, TIF_IA32))
892                 return NULL;
893 #endif
894         return &gate_vma;
895 }
896
897 int in_gate_area(struct task_struct *task, unsigned long addr)
898 {
899         struct vm_area_struct *vma = get_gate_vma(task);
900
901         if (!vma)
902                 return 0;
903
904         return (addr >= vma->vm_start) && (addr < vma->vm_end);
905 }
906
907 /*
908  * Use this when you have no reliable task/vma, typically from interrupt
909  * context. It is less reliable than using the task's vma and may give
910  * false positives:
911  */
912 int in_gate_area_no_task(unsigned long addr)
913 {
914         return (addr >= VSYSCALL_START) && (addr < VSYSCALL_END);
915 }
916
917 const char *arch_vma_name(struct vm_area_struct *vma)
918 {
919         if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
920                 return "[vdso]";
921         if (vma == &gate_vma)
922                 return "[vsyscall]";
923         return NULL;
924 }
925
926 #ifdef CONFIG_SPARSEMEM_VMEMMAP
927 /*
928  * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
929  */
930 static long __meminitdata addr_start, addr_end;
931 static void __meminitdata *p_start, *p_end;
932 static int __meminitdata node_start;
933
934 int __meminit
935 vmemmap_populate(struct page *start_page, unsigned long size, int node)
936 {
937         unsigned long addr = (unsigned long)start_page;
938         unsigned long end = (unsigned long)(start_page + size);
939         unsigned long next;
940         pgd_t *pgd;
941         pud_t *pud;
942         pmd_t *pmd;
943
944         for (; addr < end; addr = next) {
945                 void *p = NULL;
946
947                 pgd = vmemmap_pgd_populate(addr, node);
948                 if (!pgd)
949                         return -ENOMEM;
950
951                 pud = vmemmap_pud_populate(pgd, addr, node);
952                 if (!pud)
953                         return -ENOMEM;
954
955                 if (!cpu_has_pse) {
956                         next = (addr + PAGE_SIZE) & PAGE_MASK;
957                         pmd = vmemmap_pmd_populate(pud, addr, node);
958
959                         if (!pmd)
960                                 return -ENOMEM;
961
962                         p = vmemmap_pte_populate(pmd, addr, node);
963
964                         if (!p)
965                                 return -ENOMEM;
966
967                         addr_end = addr + PAGE_SIZE;
968                         p_end = p + PAGE_SIZE;
969                 } else {
970                         next = pmd_addr_end(addr, end);
971
972                         pmd = pmd_offset(pud, addr);
973                         if (pmd_none(*pmd)) {
974                                 pte_t entry;
975
976                                 p = vmemmap_alloc_block(PMD_SIZE, node);
977                                 if (!p)
978                                         return -ENOMEM;
979
980                                 entry = pfn_pte(__pa(p) >> PAGE_SHIFT,
981                                                 PAGE_KERNEL_LARGE);
982                                 set_pmd(pmd, __pmd(pte_val(entry)));
983
984                                 /* check to see if we have contiguous blocks */
985                                 if (p_end != p || node_start != node) {
986                                         if (p_start)
987                                                 printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n",
988                                                        addr_start, addr_end-1, p_start, p_end-1, node_start);
989                                         addr_start = addr;
990                                         node_start = node;
991                                         p_start = p;
992                                 }
993
994                                 addr_end = addr + PMD_SIZE;
995                                 p_end = p + PMD_SIZE;
996                         } else
997                                 vmemmap_verify((pte_t *)pmd, node, addr, next);
998                 }
999
1000         }
1001         return 0;
1002 }
1003
1004 void __meminit vmemmap_populate_print_last(void)
1005 {
1006         if (p_start) {
1007                 printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n",
1008                         addr_start, addr_end-1, p_start, p_end-1, node_start);
1009                 p_start = NULL;
1010                 p_end = NULL;
1011                 node_start = 0;
1012         }
1013 }
1014 #endif