]> bbs.cooldavid.org Git - net-next-2.6.git/blame - arch/x86/mm/init_64.c
Remove multiple KERN_ prefixes from printk formats
[net-next-2.6.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();
66918dcd
YL
601 /* clear the default setting with node 0 */
602 nodes_clear(node_states[N_NORMAL_MEMORY]);
5cb248ab 603 free_area_init_nodes(max_zone_pfns);
1da177e4 604}
1da177e4 605
44df75e6
MT
606/*
607 * Memory hotplug specific functions
44df75e6 608 */
bc02af93 609#ifdef CONFIG_MEMORY_HOTPLUG
9d99aaa3
AK
610/*
611 * Memory is added always to NORMAL zone. This means you will never get
612 * additional DMA/DMA32 memory.
613 */
bc02af93 614int arch_add_memory(int nid, u64 start, u64 size)
44df75e6 615{
bc02af93 616 struct pglist_data *pgdat = NODE_DATA(nid);
776ed98b 617 struct zone *zone = pgdat->node_zones + ZONE_NORMAL;
cc615032 618 unsigned long last_mapped_pfn, start_pfn = start >> PAGE_SHIFT;
44df75e6
MT
619 unsigned long nr_pages = size >> PAGE_SHIFT;
620 int ret;
621
60817c9b 622 last_mapped_pfn = init_memory_mapping(start, start + size);
cc615032
AK
623 if (last_mapped_pfn > max_pfn_mapped)
624 max_pfn_mapped = last_mapped_pfn;
45e0b78b 625
c04fc586 626 ret = __add_pages(nid, zone, start_pfn, nr_pages);
fe8b868e 627 WARN_ON_ONCE(ret);
44df75e6 628
44df75e6 629 return ret;
44df75e6 630}
bc02af93 631EXPORT_SYMBOL_GPL(arch_add_memory);
44df75e6 632
8243229f 633#if !defined(CONFIG_ACPI_NUMA) && defined(CONFIG_NUMA)
4942e998
KM
634int memory_add_physaddr_to_nid(u64 start)
635{
636 return 0;
637}
8c2676a5 638EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
4942e998
KM
639#endif
640
45e0b78b
KM
641#endif /* CONFIG_MEMORY_HOTPLUG */
642
14a62c34
TG
643static struct kcore_list kcore_mem, kcore_vmalloc, kcore_kernel,
644 kcore_modules, kcore_vsyscall;
1da177e4
LT
645
646void __init mem_init(void)
647{
0a43e4bf 648 long codesize, reservedpages, datasize, initsize;
11a6b0c9 649 unsigned long absent_pages;
1da177e4 650
0dc243ae 651 pci_iommu_alloc();
1da177e4 652
48ddb154 653 /* clear_bss() already clear the empty_zero_page */
1da177e4
LT
654
655 reservedpages = 0;
656
657 /* this will put all low memory onto the freelists */
2b97690f 658#ifdef CONFIG_NUMA
0a43e4bf 659 totalram_pages = numa_free_all_bootmem();
1da177e4 660#else
0a43e4bf 661 totalram_pages = free_all_bootmem();
1da177e4 662#endif
11a6b0c9
YL
663
664 absent_pages = absent_pages_in_range(0, max_pfn);
665 reservedpages = max_pfn - totalram_pages - absent_pages;
1da177e4
LT
666 after_bootmem = 1;
667
668 codesize = (unsigned long) &_etext - (unsigned long) &_text;
669 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
670 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
671
672 /* Register memory areas for /proc/kcore */
14a62c34
TG
673 kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
674 kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
1da177e4
LT
675 VMALLOC_END-VMALLOC_START);
676 kclist_add(&kcore_kernel, &_stext, _end - _stext);
677 kclist_add(&kcore_modules, (void *)MODULES_VADDR, MODULES_LEN);
14a62c34 678 kclist_add(&kcore_vsyscall, (void *)VSYSCALL_START,
1da177e4
LT
679 VSYSCALL_END - VSYSCALL_START);
680
10f22dde 681 printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, "
11a6b0c9 682 "%ldk absent, %ldk reserved, %ldk data, %ldk init)\n",
1da177e4 683 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
c987d12f 684 max_pfn << (PAGE_SHIFT-10),
1da177e4 685 codesize >> 10,
11a6b0c9 686 absent_pages << (PAGE_SHIFT-10),
1da177e4
LT
687 reservedpages << (PAGE_SHIFT-10),
688 datasize >> 10,
689 initsize >> 10);
1da177e4
LT
690}
691
67df197b 692#ifdef CONFIG_DEBUG_RODATA
edeed305
AV
693const int rodata_test_data = 0xC3;
694EXPORT_SYMBOL_GPL(rodata_test_data);
67df197b 695
16239630
SR
696static int kernel_set_to_readonly;
697
698void set_kernel_text_rw(void)
699{
700 unsigned long start = PFN_ALIGN(_stext);
701 unsigned long end = PFN_ALIGN(__start_rodata);
702
703 if (!kernel_set_to_readonly)
704 return;
705
706 pr_debug("Set kernel text: %lx - %lx for read write\n",
707 start, end);
708
709 set_memory_rw(start, (end - start) >> PAGE_SHIFT);
710}
711
712void set_kernel_text_ro(void)
713{
714 unsigned long start = PFN_ALIGN(_stext);
715 unsigned long end = PFN_ALIGN(__start_rodata);
716
717 if (!kernel_set_to_readonly)
718 return;
719
720 pr_debug("Set kernel text: %lx - %lx for read only\n",
721 start, end);
722
723 set_memory_ro(start, (end - start) >> PAGE_SHIFT);
724}
725
67df197b
AV
726void mark_rodata_ro(void)
727{
4e4eee0e 728 unsigned long start = PFN_ALIGN(_stext), end = PFN_ALIGN(__end_rodata);
8f0f996e
SR
729 unsigned long rodata_start =
730 ((unsigned long)__start_rodata + PAGE_SIZE - 1) & PAGE_MASK;
731
6fb14755 732 printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
e3ebadd9 733 (end - start) >> 10);
984bb80d
AV
734 set_memory_ro(start, (end - start) >> PAGE_SHIFT);
735
16239630
SR
736 kernel_set_to_readonly = 1;
737
984bb80d
AV
738 /*
739 * The rodata section (but not the kernel text!) should also be
740 * not-executable.
741 */
72b59d67 742 set_memory_nx(rodata_start, (end - rodata_start) >> PAGE_SHIFT);
67df197b 743
1a487252
AV
744 rodata_test();
745
0c42f392 746#ifdef CONFIG_CPA_DEBUG
10f22dde 747 printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end);
6d238cc4 748 set_memory_rw(start, (end-start) >> PAGE_SHIFT);
0c42f392 749
10f22dde 750 printk(KERN_INFO "Testing CPA: again\n");
6d238cc4 751 set_memory_ro(start, (end-start) >> PAGE_SHIFT);
0c42f392 752#endif
67df197b 753}
4e4eee0e 754
67df197b
AV
755#endif
756
d2dbf343
YL
757int __init reserve_bootmem_generic(unsigned long phys, unsigned long len,
758 int flags)
14a62c34 759{
2b97690f 760#ifdef CONFIG_NUMA
8b3cd09e 761 int nid, next_nid;
6a07a0ed 762 int ret;
5e58a02a
AK
763#endif
764 unsigned long pfn = phys >> PAGE_SHIFT;
14a62c34 765
c987d12f 766 if (pfn >= max_pfn) {
14a62c34
TG
767 /*
768 * This can happen with kdump kernels when accessing
769 * firmware tables:
770 */
67794292 771 if (pfn < max_pfn_mapped)
8b2ef1d7 772 return -EFAULT;
14a62c34 773
6a07a0ed 774 printk(KERN_ERR "reserve_bootmem: illegal reserve %lx %lu\n",
5e58a02a 775 phys, len);
8b2ef1d7 776 return -EFAULT;
5e58a02a
AK
777 }
778
779 /* Should check here against the e820 map to avoid double free */
780#ifdef CONFIG_NUMA
8b3cd09e
YL
781 nid = phys_to_nid(phys);
782 next_nid = phys_to_nid(phys + len - 1);
783 if (nid == next_nid)
8b2ef1d7 784 ret = reserve_bootmem_node(NODE_DATA(nid), phys, len, flags);
8b3cd09e 785 else
8b2ef1d7
BW
786 ret = reserve_bootmem(phys, len, flags);
787
788 if (ret != 0)
789 return ret;
790
14a62c34 791#else
72a7fe39 792 reserve_bootmem(phys, len, BOOTMEM_DEFAULT);
1da177e4 793#endif
8b3cd09e 794
0e0b864e 795 if (phys+len <= MAX_DMA_PFN*PAGE_SIZE) {
e18c6874 796 dma_reserve += len / PAGE_SIZE;
0e0b864e
MG
797 set_dma_reserve(dma_reserve);
798 }
8b2ef1d7
BW
799
800 return 0;
1da177e4
LT
801}
802
14a62c34
TG
803int kern_addr_valid(unsigned long addr)
804{
1da177e4 805 unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
14a62c34
TG
806 pgd_t *pgd;
807 pud_t *pud;
808 pmd_t *pmd;
809 pte_t *pte;
1da177e4
LT
810
811 if (above != 0 && above != -1UL)
14a62c34
TG
812 return 0;
813
1da177e4
LT
814 pgd = pgd_offset_k(addr);
815 if (pgd_none(*pgd))
816 return 0;
817
818 pud = pud_offset(pgd, addr);
819 if (pud_none(*pud))
14a62c34 820 return 0;
1da177e4
LT
821
822 pmd = pmd_offset(pud, addr);
823 if (pmd_none(*pmd))
824 return 0;
14a62c34 825
1da177e4
LT
826 if (pmd_large(*pmd))
827 return pfn_valid(pmd_pfn(*pmd));
828
829 pte = pte_offset_kernel(pmd, addr);
830 if (pte_none(*pte))
831 return 0;
14a62c34 832
1da177e4
LT
833 return pfn_valid(pte_pfn(*pte));
834}
835
14a62c34
TG
836/*
837 * A pseudo VMA to allow ptrace access for the vsyscall page. This only
838 * covers the 64bit vsyscall page now. 32bit has a real VMA now and does
839 * not need special handling anymore:
840 */
1da177e4 841static struct vm_area_struct gate_vma = {
14a62c34
TG
842 .vm_start = VSYSCALL_START,
843 .vm_end = VSYSCALL_START + (VSYSCALL_MAPPED_PAGES * PAGE_SIZE),
844 .vm_page_prot = PAGE_READONLY_EXEC,
845 .vm_flags = VM_READ | VM_EXEC
1da177e4
LT
846};
847
1da177e4
LT
848struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
849{
850#ifdef CONFIG_IA32_EMULATION
1e014410
AK
851 if (test_tsk_thread_flag(tsk, TIF_IA32))
852 return NULL;
1da177e4
LT
853#endif
854 return &gate_vma;
855}
856
857int in_gate_area(struct task_struct *task, unsigned long addr)
858{
859 struct vm_area_struct *vma = get_gate_vma(task);
14a62c34 860
1e014410
AK
861 if (!vma)
862 return 0;
14a62c34 863
1da177e4
LT
864 return (addr >= vma->vm_start) && (addr < vma->vm_end);
865}
866
14a62c34
TG
867/*
868 * Use this when you have no reliable task/vma, typically from interrupt
869 * context. It is less reliable than using the task's vma and may give
870 * false positives:
1da177e4
LT
871 */
872int in_gate_area_no_task(unsigned long addr)
873{
1e014410 874 return (addr >= VSYSCALL_START) && (addr < VSYSCALL_END);
1da177e4 875}
2e1c49db 876
2aae950b
AK
877const char *arch_vma_name(struct vm_area_struct *vma)
878{
879 if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
880 return "[vdso]";
881 if (vma == &gate_vma)
882 return "[vsyscall]";
883 return NULL;
884}
0889eba5
CL
885
886#ifdef CONFIG_SPARSEMEM_VMEMMAP
887/*
888 * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
889 */
c2b91e2e
YL
890static long __meminitdata addr_start, addr_end;
891static void __meminitdata *p_start, *p_end;
892static int __meminitdata node_start;
893
14a62c34
TG
894int __meminit
895vmemmap_populate(struct page *start_page, unsigned long size, int node)
0889eba5
CL
896{
897 unsigned long addr = (unsigned long)start_page;
898 unsigned long end = (unsigned long)(start_page + size);
899 unsigned long next;
900 pgd_t *pgd;
901 pud_t *pud;
902 pmd_t *pmd;
903
904 for (; addr < end; addr = next) {
7c934d39 905 void *p = NULL;
0889eba5
CL
906
907 pgd = vmemmap_pgd_populate(addr, node);
908 if (!pgd)
909 return -ENOMEM;
14a62c34 910
0889eba5
CL
911 pud = vmemmap_pud_populate(pgd, addr, node);
912 if (!pud)
913 return -ENOMEM;
914
7c934d39
JF
915 if (!cpu_has_pse) {
916 next = (addr + PAGE_SIZE) & PAGE_MASK;
917 pmd = vmemmap_pmd_populate(pud, addr, node);
918
919 if (!pmd)
920 return -ENOMEM;
921
922 p = vmemmap_pte_populate(pmd, addr, node);
14a62c34 923
0889eba5
CL
924 if (!p)
925 return -ENOMEM;
926
7c934d39
JF
927 addr_end = addr + PAGE_SIZE;
928 p_end = p + PAGE_SIZE;
14a62c34 929 } else {
7c934d39
JF
930 next = pmd_addr_end(addr, end);
931
932 pmd = pmd_offset(pud, addr);
933 if (pmd_none(*pmd)) {
934 pte_t entry;
935
936 p = vmemmap_alloc_block(PMD_SIZE, node);
937 if (!p)
938 return -ENOMEM;
939
940 entry = pfn_pte(__pa(p) >> PAGE_SHIFT,
941 PAGE_KERNEL_LARGE);
942 set_pmd(pmd, __pmd(pte_val(entry)));
943
7c934d39
JF
944 /* check to see if we have contiguous blocks */
945 if (p_end != p || node_start != node) {
946 if (p_start)
947 printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n",
948 addr_start, addr_end-1, p_start, p_end-1, node_start);
949 addr_start = addr;
950 node_start = node;
951 p_start = p;
952 }
49c980df
YL
953
954 addr_end = addr + PMD_SIZE;
955 p_end = p + PMD_SIZE;
7c934d39
JF
956 } else
957 vmemmap_verify((pte_t *)pmd, node, addr, next);
14a62c34 958 }
7c934d39 959
0889eba5 960 }
0889eba5
CL
961 return 0;
962}
c2b91e2e
YL
963
964void __meminit vmemmap_populate_print_last(void)
965{
966 if (p_start) {
967 printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n",
968 addr_start, addr_end-1, p_start, p_end-1, node_start);
969 p_start = NULL;
970 p_end = NULL;
971 node_start = 0;
972 }
973}
0889eba5 974#endif