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