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