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