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