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