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