x86: fix page-present check in cpa_flush_range
[deliverable/linux.git] / arch / x86 / mm / init_64.c
CommitLineData
1da177e4
LT
1/*
2 * linux/arch/x86_64/mm/init.c
3 *
4 * Copyright (C) 1995 Linus Torvalds
5 * Copyright (C) 2000 Pavel Machek <pavel@suse.cz>
6 * Copyright (C) 2002,2003 Andi Kleen <ak@suse.de>
7 */
8
1da177e4
LT
9#include <linux/signal.h>
10#include <linux/sched.h>
11#include <linux/kernel.h>
12#include <linux/errno.h>
13#include <linux/string.h>
14#include <linux/types.h>
15#include <linux/ptrace.h>
16#include <linux/mman.h>
17#include <linux/mm.h>
18#include <linux/swap.h>
19#include <linux/smp.h>
20#include <linux/init.h>
21#include <linux/pagemap.h>
22#include <linux/bootmem.h>
23#include <linux/proc_fs.h>
59170891 24#include <linux/pci.h>
6fb14755 25#include <linux/pfn.h>
c9cf5528 26#include <linux/poison.h>
17a941d8 27#include <linux/dma-mapping.h>
44df75e6
MT
28#include <linux/module.h>
29#include <linux/memory_hotplug.h>
ae32b129 30#include <linux/nmi.h>
1da177e4
LT
31
32#include <asm/processor.h>
33#include <asm/system.h>
34#include <asm/uaccess.h>
35#include <asm/pgtable.h>
36#include <asm/pgalloc.h>
37#include <asm/dma.h>
38#include <asm/fixmap.h>
39#include <asm/e820.h>
40#include <asm/apic.h>
41#include <asm/tlb.h>
42#include <asm/mmu_context.h>
43#include <asm/proto.h>
44#include <asm/smp.h>
2bc0414e 45#include <asm/sections.h>
718fc13b 46#include <asm/kdebug.h>
aaa64e04 47#include <asm/numa.h>
1da177e4 48
14a62c34 49const struct dma_mapping_ops *dma_ops;
17a941d8
MBY
50EXPORT_SYMBOL(dma_ops);
51
e18c6874
AK
52static unsigned long dma_reserve __initdata;
53
1da177e4
LT
54DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
55
56/*
57 * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the
58 * physical space so we can cache the place of the first one and move
59 * around without checking the pgd every time.
60 */
61
62void show_mem(void)
63{
e92343cc
AK
64 long i, total = 0, reserved = 0;
65 long shared = 0, cached = 0;
1da177e4 66 struct page *page;
14a62c34 67 pg_data_t *pgdat;
1da177e4 68
e92343cc 69 printk(KERN_INFO "Mem-info:\n");
1da177e4 70 show_free_areas();
14a62c34
TG
71 printk(KERN_INFO "Free swap: %6ldkB\n",
72 nr_swap_pages << (PAGE_SHIFT-10));
1da177e4 73
ec936fc5 74 for_each_online_pgdat(pgdat) {
14a62c34
TG
75 for (i = 0; i < pgdat->node_spanned_pages; ++i) {
76 /*
77 * This loop can take a while with 256 GB and
78 * 4k pages so defer the NMI watchdog:
79 */
80 if (unlikely(i % MAX_ORDER_NR_PAGES == 0))
ae32b129 81 touch_nmi_watchdog();
14a62c34 82
12710a56
BP
83 if (!pfn_valid(pgdat->node_start_pfn + i))
84 continue;
14a62c34 85
1da177e4
LT
86 page = pfn_to_page(pgdat->node_start_pfn + i);
87 total++;
e92343cc
AK
88 if (PageReserved(page))
89 reserved++;
90 else if (PageSwapCache(page))
91 cached++;
92 else if (page_count(page))
93 shared += page_count(page) - 1;
14a62c34 94 }
1da177e4 95 }
14a62c34
TG
96 printk(KERN_INFO "%lu pages of RAM\n", total);
97 printk(KERN_INFO "%lu reserved pages\n", reserved);
98 printk(KERN_INFO "%lu pages shared\n", shared);
99 printk(KERN_INFO "%lu pages swap cached\n", cached);
1da177e4
LT
100}
101
1da177e4
LT
102int after_bootmem;
103
5f44a669 104static __init void *spp_getpage(void)
14a62c34 105{
1da177e4 106 void *ptr;
14a62c34 107
1da177e4 108 if (after_bootmem)
14a62c34 109 ptr = (void *) get_zeroed_page(GFP_ATOMIC);
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
14a62c34
TG
123static __init void
124set_pte_phys(unsigned long vaddr, unsigned long phys, pgprot_t prot)
1da177e4
LT
125{
126 pgd_t *pgd;
127 pud_t *pud;
128 pmd_t *pmd;
129 pte_t *pte, new_pte;
130
10f22dde 131 pr_debug("set_pte_phys %lx to %lx\n", vaddr, phys);
1da177e4
LT
132
133 pgd = pgd_offset_k(vaddr);
134 if (pgd_none(*pgd)) {
10f22dde
IM
135 printk(KERN_ERR
136 "PGD FIXMAP MISSING, it should be setup in head.S!\n");
1da177e4
LT
137 return;
138 }
139 pud = pud_offset(pgd, vaddr);
140 if (pud_none(*pud)) {
14a62c34 141 pmd = (pmd_t *) spp_getpage();
1da177e4
LT
142 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE | _PAGE_USER));
143 if (pmd != pmd_offset(pud, 0)) {
10f22dde 144 printk(KERN_ERR "PAGETABLE BUG #01! %p <-> %p\n",
14a62c34 145 pmd, pmd_offset(pud, 0));
1da177e4
LT
146 return;
147 }
148 }
149 pmd = pmd_offset(pud, vaddr);
150 if (pmd_none(*pmd)) {
151 pte = (pte_t *) spp_getpage();
152 set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE | _PAGE_USER));
153 if (pte != pte_offset_kernel(pmd, 0)) {
10f22dde 154 printk(KERN_ERR "PAGETABLE BUG #02!\n");
1da177e4
LT
155 return;
156 }
157 }
158 new_pte = pfn_pte(phys >> PAGE_SHIFT, prot);
159
160 pte = pte_offset_kernel(pmd, vaddr);
161 if (!pte_none(*pte) &&
162 pte_val(*pte) != (pte_val(new_pte) & __supported_pte_mask))
163 pte_ERROR(*pte);
164 set_pte(pte, new_pte);
165
166 /*
167 * It's enough to flush this one mapping.
168 * (PGE mappings get flushed as well)
169 */
170 __flush_tlb_one(vaddr);
171}
172
173/* NOTE: this is meant to be run only at boot */
14a62c34
TG
174void __init
175__set_fixmap(enum fixed_addresses idx, unsigned long phys, pgprot_t prot)
1da177e4
LT
176{
177 unsigned long address = __fix_to_virt(idx);
178
179 if (idx >= __end_of_fixed_addresses) {
10f22dde 180 printk(KERN_ERR "Invalid __set_fixmap\n");
1da177e4
LT
181 return;
182 }
183 set_pte_phys(address, phys, prot);
184}
185
75175278
AK
186static unsigned long __initdata table_start;
187static unsigned long __meminitdata table_end;
1da177e4 188
dafe41ee 189static __meminit void *alloc_low_page(unsigned long *phys)
14a62c34 190{
dafe41ee 191 unsigned long pfn = table_end++;
1da177e4
LT
192 void *adr;
193
44df75e6
MT
194 if (after_bootmem) {
195 adr = (void *)get_zeroed_page(GFP_ATOMIC);
196 *phys = __pa(adr);
14a62c34 197
44df75e6
MT
198 return adr;
199 }
200
14a62c34
TG
201 if (pfn >= end_pfn)
202 panic("alloc_low_page: ran out of memory");
dafe41ee
VG
203
204 adr = early_ioremap(pfn * PAGE_SIZE, PAGE_SIZE);
44df75e6 205 memset(adr, 0, PAGE_SIZE);
dafe41ee
VG
206 *phys = pfn * PAGE_SIZE;
207 return adr;
208}
1da177e4 209
dafe41ee 210static __meminit void unmap_low_page(void *adr)
14a62c34 211{
44df75e6
MT
212 if (after_bootmem)
213 return;
214
dafe41ee 215 early_iounmap(adr, PAGE_SIZE);
14a62c34 216}
1da177e4 217
f2d3efed 218/* Must run before zap_low_mappings */
a3142c8e 219__meminit void *early_ioremap(unsigned long addr, unsigned long size)
f2d3efed 220{
dafe41ee 221 pmd_t *pmd, *last_pmd;
14a62c34 222 unsigned long vaddr;
dafe41ee
VG
223 int i, pmds;
224
225 pmds = ((addr & ~PMD_MASK) + size + ~PMD_MASK) / PMD_SIZE;
226 vaddr = __START_KERNEL_map;
227 pmd = level2_kernel_pgt;
228 last_pmd = level2_kernel_pgt + PTRS_PER_PMD - 1;
14a62c34 229
dafe41ee
VG
230 for (; pmd <= last_pmd; pmd++, vaddr += PMD_SIZE) {
231 for (i = 0; i < pmds; i++) {
232 if (pmd_present(pmd[i]))
14a62c34 233 goto continue_outer_loop;
dafe41ee
VG
234 }
235 vaddr += addr & ~PMD_MASK;
236 addr &= PMD_MASK;
14a62c34 237
dafe41ee 238 for (i = 0; i < pmds; i++, addr += PMD_SIZE)
929fd589 239 set_pmd(pmd+i, __pmd(addr | __PAGE_KERNEL_LARGE_EXEC));
1a2b4412 240 __flush_tlb_all();
14a62c34 241
dafe41ee 242 return (void *)vaddr;
14a62c34 243continue_outer_loop:
dafe41ee 244 ;
f2d3efed 245 }
10f22dde 246 printk(KERN_ERR "early_ioremap(0x%lx, %lu) failed\n", addr, size);
14a62c34 247
dafe41ee 248 return NULL;
f2d3efed
AK
249}
250
14a62c34
TG
251/*
252 * To avoid virtual aliases later:
253 */
a3142c8e 254__meminit void early_iounmap(void *addr, unsigned long size)
f2d3efed 255{
dafe41ee
VG
256 unsigned long vaddr;
257 pmd_t *pmd;
258 int i, pmds;
259
260 vaddr = (unsigned long)addr;
261 pmds = ((vaddr & ~PMD_MASK) + size + ~PMD_MASK) / PMD_SIZE;
262 pmd = level2_kernel_pgt + pmd_index(vaddr);
14a62c34 263
dafe41ee
VG
264 for (i = 0; i < pmds; i++)
265 pmd_clear(pmd + i);
14a62c34 266
1a2b4412 267 __flush_tlb_all();
f2d3efed
AK
268}
269
44df75e6 270static void __meminit
6ad91658 271phys_pmd_init(pmd_t *pmd_page, unsigned long address, unsigned long end)
44df75e6 272{
6ad91658 273 int i = pmd_index(address);
44df75e6 274
6ad91658 275 for (; i < PTRS_PER_PMD; i++, address += PMD_SIZE) {
44df75e6 276 unsigned long entry;
6ad91658 277 pmd_t *pmd = pmd_page + pmd_index(address);
44df75e6 278
5f51e139 279 if (address >= end) {
14a62c34 280 if (!after_bootmem) {
5f51e139
JB
281 for (; i < PTRS_PER_PMD; i++, pmd++)
282 set_pmd(pmd, __pmd(0));
14a62c34 283 }
44df75e6
MT
284 break;
285 }
6ad91658
KM
286
287 if (pmd_val(*pmd))
288 continue;
289
40842bf5 290 entry = __PAGE_KERNEL_LARGE|_PAGE_GLOBAL|address;
44df75e6
MT
291 entry &= __supported_pte_mask;
292 set_pmd(pmd, __pmd(entry));
293 }
294}
295
296static void __meminit
297phys_pmd_update(pud_t *pud, unsigned long address, unsigned long end)
298{
14a62c34 299 pmd_t *pmd = pmd_offset(pud, 0);
6ad91658
KM
300 spin_lock(&init_mm.page_table_lock);
301 phys_pmd_init(pmd, address, end);
302 spin_unlock(&init_mm.page_table_lock);
303 __flush_tlb_all();
44df75e6
MT
304}
305
14a62c34
TG
306static void __meminit
307phys_pud_init(pud_t *pud_page, unsigned long addr, unsigned long end)
308{
6ad91658 309 int i = pud_index(addr);
44df75e6 310
14a62c34 311 for (; i < PTRS_PER_PUD; i++, addr = (addr & PUD_MASK) + PUD_SIZE) {
6ad91658
KM
312 unsigned long pmd_phys;
313 pud_t *pud = pud_page + pud_index(addr);
1da177e4
LT
314 pmd_t *pmd;
315
6ad91658 316 if (addr >= end)
1da177e4 317 break;
1da177e4 318
14a62c34
TG
319 if (!after_bootmem &&
320 !e820_any_mapped(addr, addr+PUD_SIZE, 0)) {
321 set_pud(pud, __pud(0));
1da177e4 322 continue;
14a62c34 323 }
1da177e4 324
6ad91658
KM
325 if (pud_val(*pud)) {
326 phys_pmd_update(pud, addr, end);
327 continue;
328 }
329
dafe41ee 330 pmd = alloc_low_page(&pmd_phys);
14a62c34 331
44df75e6 332 spin_lock(&init_mm.page_table_lock);
1da177e4 333 set_pud(pud, __pud(pmd_phys | _KERNPG_TABLE));
6ad91658 334 phys_pmd_init(pmd, addr, end);
44df75e6 335 spin_unlock(&init_mm.page_table_lock);
14a62c34 336
dafe41ee 337 unmap_low_page(pmd);
1da177e4 338 }
1a2b4412 339 __flush_tlb_all();
14a62c34 340}
1da177e4
LT
341
342static void __init find_early_table_space(unsigned long end)
343{
6c5acd16 344 unsigned long puds, pmds, tables, start;
1da177e4
LT
345
346 puds = (end + PUD_SIZE - 1) >> PUD_SHIFT;
347 pmds = (end + PMD_SIZE - 1) >> PMD_SHIFT;
348 tables = round_up(puds * sizeof(pud_t), PAGE_SIZE) +
349 round_up(pmds * sizeof(pmd_t), PAGE_SIZE);
350
14a62c34
TG
351 /*
352 * RED-PEN putting page tables only on node 0 could
353 * cause a hotspot and fill up ZONE_DMA. The page tables
354 * need roughly 0.5KB per GB.
355 */
356 start = 0x8000;
24a5da73 357 table_start = find_e820_area(start, end, tables, PAGE_SIZE);
1da177e4
LT
358 if (table_start == -1UL)
359 panic("Cannot find space for the kernel page tables");
360
361 table_start >>= PAGE_SHIFT;
362 table_end = table_start;
44df75e6
MT
363
364 early_printk("kernel direct mapping tables up to %lx @ %lx-%lx\n",
5f51e139
JB
365 end, table_start << PAGE_SHIFT,
366 (table_start << PAGE_SHIFT) + tables);
1da177e4
LT
367}
368
14a62c34
TG
369/*
370 * Setup the direct mapping of the physical memory at PAGE_OFFSET.
371 * This runs before bootmem is initialized and gets pages directly from
372 * the physical memory. To access them they are temporarily mapped.
373 */
b6fd6ecb 374void __init_refok init_memory_mapping(unsigned long start, unsigned long end)
14a62c34
TG
375{
376 unsigned long next;
1da177e4 377
10f22dde 378 pr_debug("init_memory_mapping\n");
1da177e4 379
14a62c34 380 /*
1da177e4 381 * Find space for the kernel direct mapping tables.
14a62c34
TG
382 *
383 * Later we should allocate these tables in the local node of the
384 * memory mapped. Unfortunately this is done currently before the
385 * nodes are discovered.
1da177e4 386 */
44df75e6
MT
387 if (!after_bootmem)
388 find_early_table_space(end);
1da177e4
LT
389
390 start = (unsigned long)__va(start);
391 end = (unsigned long)__va(end);
392
393 for (; start < end; start = next) {
44df75e6 394 pgd_t *pgd = pgd_offset_k(start);
14a62c34 395 unsigned long pud_phys;
44df75e6
MT
396 pud_t *pud;
397
398 if (after_bootmem)
d2ae5b5f 399 pud = pud_offset(pgd, start & PGDIR_MASK);
44df75e6 400 else
dafe41ee 401 pud = alloc_low_page(&pud_phys);
44df75e6 402
1da177e4 403 next = start + PGDIR_SIZE;
14a62c34
TG
404 if (next > end)
405 next = end;
1da177e4 406 phys_pud_init(pud, __pa(start), __pa(next));
44df75e6
MT
407 if (!after_bootmem)
408 set_pgd(pgd_offset_k(start), mk_kernel_pgd(pud_phys));
dafe41ee 409 unmap_low_page(pud);
14a62c34 410 }
1da177e4 411
44df75e6 412 if (!after_bootmem)
f51c9452 413 mmu_cr4_features = read_cr4();
1da177e4 414 __flush_tlb_all();
75175278 415
24a5da73
YL
416 if (!after_bootmem)
417 reserve_early(table_start << PAGE_SHIFT,
418 table_end << PAGE_SHIFT, "PGTABLE");
1da177e4
LT
419}
420
2b97690f 421#ifndef CONFIG_NUMA
1da177e4
LT
422void __init paging_init(void)
423{
6391af17 424 unsigned long max_zone_pfns[MAX_NR_ZONES];
14a62c34 425
6391af17
MG
426 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
427 max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
428 max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
429 max_zone_pfns[ZONE_NORMAL] = end_pfn;
430
44df75e6
MT
431 memory_present(0, 0, end_pfn);
432 sparse_init();
5cb248ab 433 free_area_init_nodes(max_zone_pfns);
1da177e4
LT
434}
435#endif
436
44df75e6
MT
437/*
438 * Memory hotplug specific functions
44df75e6 439 */
44df75e6
MT
440void online_page(struct page *page)
441{
442 ClearPageReserved(page);
7835e98b 443 init_page_count(page);
44df75e6
MT
444 __free_page(page);
445 totalram_pages++;
446 num_physpages++;
447}
448
bc02af93 449#ifdef CONFIG_MEMORY_HOTPLUG
9d99aaa3
AK
450/*
451 * Memory is added always to NORMAL zone. This means you will never get
452 * additional DMA/DMA32 memory.
453 */
bc02af93 454int arch_add_memory(int nid, u64 start, u64 size)
44df75e6 455{
bc02af93 456 struct pglist_data *pgdat = NODE_DATA(nid);
776ed98b 457 struct zone *zone = pgdat->node_zones + ZONE_NORMAL;
44df75e6
MT
458 unsigned long start_pfn = start >> PAGE_SHIFT;
459 unsigned long nr_pages = size >> PAGE_SHIFT;
460 int ret;
461
14a62c34 462 init_memory_mapping(start, start + size-1);
45e0b78b 463
44df75e6 464 ret = __add_pages(zone, start_pfn, nr_pages);
10f22dde 465 WARN_ON(1);
44df75e6 466
44df75e6 467 return ret;
44df75e6 468}
bc02af93 469EXPORT_SYMBOL_GPL(arch_add_memory);
44df75e6 470
8243229f 471#if !defined(CONFIG_ACPI_NUMA) && defined(CONFIG_NUMA)
4942e998
KM
472int memory_add_physaddr_to_nid(u64 start)
473{
474 return 0;
475}
8c2676a5 476EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
4942e998
KM
477#endif
478
45e0b78b
KM
479#endif /* CONFIG_MEMORY_HOTPLUG */
480
14a62c34
TG
481static struct kcore_list kcore_mem, kcore_vmalloc, kcore_kernel,
482 kcore_modules, kcore_vsyscall;
1da177e4
LT
483
484void __init mem_init(void)
485{
0a43e4bf 486 long codesize, reservedpages, datasize, initsize;
1da177e4 487
0dc243ae 488 pci_iommu_alloc();
1da177e4 489
48ddb154 490 /* clear_bss() already clear the empty_zero_page */
1da177e4 491
f2633105
IM
492 /* temporary debugging - double check it's true: */
493 {
494 int i;
495
496 for (i = 0; i < 1024; i++)
497 WARN_ON_ONCE(empty_zero_page[i]);
498 }
499
1da177e4
LT
500 reservedpages = 0;
501
502 /* this will put all low memory onto the freelists */
2b97690f 503#ifdef CONFIG_NUMA
0a43e4bf 504 totalram_pages = numa_free_all_bootmem();
1da177e4 505#else
0a43e4bf 506 totalram_pages = free_all_bootmem();
1da177e4 507#endif
5cb248ab
MG
508 reservedpages = end_pfn - totalram_pages -
509 absent_pages_in_range(0, end_pfn);
1da177e4
LT
510 after_bootmem = 1;
511
512 codesize = (unsigned long) &_etext - (unsigned long) &_text;
513 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
514 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
515
516 /* Register memory areas for /proc/kcore */
14a62c34
TG
517 kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
518 kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
1da177e4
LT
519 VMALLOC_END-VMALLOC_START);
520 kclist_add(&kcore_kernel, &_stext, _end - _stext);
521 kclist_add(&kcore_modules, (void *)MODULES_VADDR, MODULES_LEN);
14a62c34 522 kclist_add(&kcore_vsyscall, (void *)VSYSCALL_START,
1da177e4
LT
523 VSYSCALL_END - VSYSCALL_START);
524
10f22dde 525 printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, "
14a62c34 526 "%ldk reserved, %ldk data, %ldk init)\n",
1da177e4
LT
527 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
528 end_pfn << (PAGE_SHIFT-10),
529 codesize >> 10,
530 reservedpages << (PAGE_SHIFT-10),
531 datasize >> 10,
532 initsize >> 10);
1da177e4
LT
533}
534
d167a518 535void free_init_pages(char *what, unsigned long begin, unsigned long end)
1da177e4
LT
536{
537 unsigned long addr;
538
d167a518
GH
539 if (begin >= end)
540 return;
541
ee01f112
IM
542 /*
543 * If debugging page accesses then do not free this memory but
544 * mark them not present - any buggy init-section access will
545 * create a kernel page fault:
546 */
547#ifdef CONFIG_DEBUG_PAGEALLOC
548 printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n",
549 begin, PAGE_ALIGN(end));
550 set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
551#else
6fb14755 552 printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
14a62c34 553
d167a518 554 for (addr = begin; addr < end; addr += PAGE_SIZE) {
e3ebadd9
LT
555 ClearPageReserved(virt_to_page(addr));
556 init_page_count(virt_to_page(addr));
557 memset((void *)(addr & ~(PAGE_SIZE-1)),
558 POISON_FREE_INITMEM, PAGE_SIZE);
e3ebadd9 559 free_page(addr);
1da177e4
LT
560 totalram_pages++;
561 }
ee01f112 562#endif
d167a518
GH
563}
564
565void free_initmem(void)
566{
d167a518 567 free_init_pages("unused kernel memory",
e3ebadd9
LT
568 (unsigned long)(&__init_begin),
569 (unsigned long)(&__init_end));
1da177e4
LT
570}
571
67df197b 572#ifdef CONFIG_DEBUG_RODATA
edeed305
AV
573const int rodata_test_data = 0xC3;
574EXPORT_SYMBOL_GPL(rodata_test_data);
67df197b 575
67df197b
AV
576void mark_rodata_ro(void)
577{
e3ebadd9 578 unsigned long start = (unsigned long)_stext, end;
67df197b 579
602033ed
LT
580#ifdef CONFIG_HOTPLUG_CPU
581 /* It must still be possible to apply SMP alternatives. */
582 if (num_possible_cpus() > 1)
583 start = (unsigned long)_etext;
584#endif
585
586#ifdef CONFIG_KPROBES
587 start = (unsigned long)__start_rodata;
588#endif
14a62c34 589
e3ebadd9
LT
590 end = (unsigned long)__end_rodata;
591 start = (start + PAGE_SIZE - 1) & PAGE_MASK;
592 end &= PAGE_MASK;
593 if (end <= start)
594 return;
595
6d238cc4 596 set_memory_ro(start, (end - start) >> PAGE_SHIFT);
67df197b 597
6fb14755 598 printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
e3ebadd9 599 (end - start) >> 10);
67df197b 600
1a487252
AV
601 rodata_test();
602
0c42f392 603#ifdef CONFIG_CPA_DEBUG
10f22dde 604 printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end);
6d238cc4 605 set_memory_rw(start, (end-start) >> PAGE_SHIFT);
0c42f392 606
10f22dde 607 printk(KERN_INFO "Testing CPA: again\n");
6d238cc4 608 set_memory_ro(start, (end-start) >> PAGE_SHIFT);
0c42f392 609#endif
67df197b
AV
610}
611#endif
612
1da177e4
LT
613#ifdef CONFIG_BLK_DEV_INITRD
614void free_initrd_mem(unsigned long start, unsigned long end)
615{
e3ebadd9 616 free_init_pages("initrd memory", start, end);
1da177e4
LT
617}
618#endif
619
14a62c34
TG
620void __init reserve_bootmem_generic(unsigned long phys, unsigned len)
621{
2b97690f 622#ifdef CONFIG_NUMA
1da177e4 623 int nid = phys_to_nid(phys);
5e58a02a
AK
624#endif
625 unsigned long pfn = phys >> PAGE_SHIFT;
14a62c34 626
5e58a02a 627 if (pfn >= end_pfn) {
14a62c34
TG
628 /*
629 * This can happen with kdump kernels when accessing
630 * firmware tables:
631 */
5e58a02a
AK
632 if (pfn < end_pfn_map)
633 return;
14a62c34 634
5e58a02a
AK
635 printk(KERN_ERR "reserve_bootmem: illegal reserve %lx %u\n",
636 phys, len);
637 return;
638 }
639
640 /* Should check here against the e820 map to avoid double free */
641#ifdef CONFIG_NUMA
14a62c34
TG
642 reserve_bootmem_node(NODE_DATA(nid), phys, len);
643#else
644 reserve_bootmem(phys, len);
1da177e4 645#endif
0e0b864e 646 if (phys+len <= MAX_DMA_PFN*PAGE_SIZE) {
e18c6874 647 dma_reserve += len / PAGE_SIZE;
0e0b864e
MG
648 set_dma_reserve(dma_reserve);
649 }
1da177e4
LT
650}
651
14a62c34
TG
652int kern_addr_valid(unsigned long addr)
653{
1da177e4 654 unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
14a62c34
TG
655 pgd_t *pgd;
656 pud_t *pud;
657 pmd_t *pmd;
658 pte_t *pte;
1da177e4
LT
659
660 if (above != 0 && above != -1UL)
14a62c34
TG
661 return 0;
662
1da177e4
LT
663 pgd = pgd_offset_k(addr);
664 if (pgd_none(*pgd))
665 return 0;
666
667 pud = pud_offset(pgd, addr);
668 if (pud_none(*pud))
14a62c34 669 return 0;
1da177e4
LT
670
671 pmd = pmd_offset(pud, addr);
672 if (pmd_none(*pmd))
673 return 0;
14a62c34 674
1da177e4
LT
675 if (pmd_large(*pmd))
676 return pfn_valid(pmd_pfn(*pmd));
677
678 pte = pte_offset_kernel(pmd, addr);
679 if (pte_none(*pte))
680 return 0;
14a62c34 681
1da177e4
LT
682 return pfn_valid(pte_pfn(*pte));
683}
684
14a62c34
TG
685/*
686 * A pseudo VMA to allow ptrace access for the vsyscall page. This only
687 * covers the 64bit vsyscall page now. 32bit has a real VMA now and does
688 * not need special handling anymore:
689 */
1da177e4 690static struct vm_area_struct gate_vma = {
14a62c34
TG
691 .vm_start = VSYSCALL_START,
692 .vm_end = VSYSCALL_START + (VSYSCALL_MAPPED_PAGES * PAGE_SIZE),
693 .vm_page_prot = PAGE_READONLY_EXEC,
694 .vm_flags = VM_READ | VM_EXEC
1da177e4
LT
695};
696
1da177e4
LT
697struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
698{
699#ifdef CONFIG_IA32_EMULATION
1e014410
AK
700 if (test_tsk_thread_flag(tsk, TIF_IA32))
701 return NULL;
1da177e4
LT
702#endif
703 return &gate_vma;
704}
705
706int in_gate_area(struct task_struct *task, unsigned long addr)
707{
708 struct vm_area_struct *vma = get_gate_vma(task);
14a62c34 709
1e014410
AK
710 if (!vma)
711 return 0;
14a62c34 712
1da177e4
LT
713 return (addr >= vma->vm_start) && (addr < vma->vm_end);
714}
715
14a62c34
TG
716/*
717 * Use this when you have no reliable task/vma, typically from interrupt
718 * context. It is less reliable than using the task's vma and may give
719 * false positives:
1da177e4
LT
720 */
721int in_gate_area_no_task(unsigned long addr)
722{
1e014410 723 return (addr >= VSYSCALL_START) && (addr < VSYSCALL_END);
1da177e4 724}
2e1c49db 725
2aae950b
AK
726const char *arch_vma_name(struct vm_area_struct *vma)
727{
728 if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
729 return "[vdso]";
730 if (vma == &gate_vma)
731 return "[vsyscall]";
732 return NULL;
733}
0889eba5
CL
734
735#ifdef CONFIG_SPARSEMEM_VMEMMAP
736/*
737 * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
738 */
14a62c34
TG
739int __meminit
740vmemmap_populate(struct page *start_page, unsigned long size, int node)
0889eba5
CL
741{
742 unsigned long addr = (unsigned long)start_page;
743 unsigned long end = (unsigned long)(start_page + size);
744 unsigned long next;
745 pgd_t *pgd;
746 pud_t *pud;
747 pmd_t *pmd;
748
749 for (; addr < end; addr = next) {
750 next = pmd_addr_end(addr, end);
751
752 pgd = vmemmap_pgd_populate(addr, node);
753 if (!pgd)
754 return -ENOMEM;
14a62c34 755
0889eba5
CL
756 pud = vmemmap_pud_populate(pgd, addr, node);
757 if (!pud)
758 return -ENOMEM;
759
760 pmd = pmd_offset(pud, addr);
761 if (pmd_none(*pmd)) {
762 pte_t entry;
14a62c34
TG
763 void *p;
764
765 p = vmemmap_alloc_block(PMD_SIZE, node);
0889eba5
CL
766 if (!p)
767 return -ENOMEM;
768
14a62c34
TG
769 entry = pfn_pte(__pa(p) >> PAGE_SHIFT,
770 PAGE_KERNEL_LARGE);
0889eba5
CL
771 set_pmd(pmd, __pmd(pte_val(entry)));
772
773 printk(KERN_DEBUG " [%lx-%lx] PMD ->%p on node %d\n",
774 addr, addr + PMD_SIZE - 1, p, node);
14a62c34 775 } else {
0889eba5 776 vmemmap_verify((pte_t *)pmd, node, addr, next);
14a62c34 777 }
0889eba5 778 }
0889eba5
CL
779 return 0;
780}
781#endif
This page took 0.384401 seconds and 5 git commands to generate.