]> bbs.cooldavid.org Git - net-next-2.6.git/blame - arch/x86/mm/init_32.c
x86: convert to the new dynamic percpu allocator
[net-next-2.6.git] / arch / x86 / mm / init_32.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 *
3 * Copyright (C) 1995 Linus Torvalds
4 *
5 * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
6 */
7
1da177e4
LT
8#include <linux/module.h>
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/hugetlb.h>
19#include <linux/swap.h>
20#include <linux/smp.h>
21#include <linux/init.h>
22#include <linux/highmem.h>
23#include <linux/pagemap.h>
cfb80c9e 24#include <linux/pci.h>
6fb14755 25#include <linux/pfn.h>
c9cf5528 26#include <linux/poison.h>
1da177e4
LT
27#include <linux/bootmem.h>
28#include <linux/slab.h>
29#include <linux/proc_fs.h>
05039b92 30#include <linux/memory_hotplug.h>
27d99f7e 31#include <linux/initrd.h>
55b2355e 32#include <linux/cpumask.h>
1da177e4 33
f832ff18 34#include <asm/asm.h>
46eaa670 35#include <asm/bios_ebda.h>
1da177e4
LT
36#include <asm/processor.h>
37#include <asm/system.h>
38#include <asm/uaccess.h>
39#include <asm/pgtable.h>
40#include <asm/dma.h>
41#include <asm/fixmap.h>
42#include <asm/e820.h>
43#include <asm/apic.h>
8550eb99 44#include <asm/bugs.h>
1da177e4
LT
45#include <asm/tlb.h>
46#include <asm/tlbflush.h>
a5a19c63 47#include <asm/pgalloc.h>
1da177e4 48#include <asm/sections.h>
b239fb25 49#include <asm/paravirt.h>
551889a6 50#include <asm/setup.h>
7bfeab9a 51#include <asm/cacheflush.h>
1da177e4
LT
52
53unsigned int __VMALLOC_RESERVE = 128 << 20;
54
f361a450 55unsigned long max_low_pfn_mapped;
67794292 56unsigned long max_pfn_mapped;
7d1116a9 57
1da177e4
LT
58DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
59unsigned long highstart_pfn, highend_pfn;
60
8550eb99 61static noinline int do_test_wp_bit(void);
1da177e4 62
4e29684c
YL
63
64static unsigned long __initdata table_start;
65static unsigned long __meminitdata table_end;
66static unsigned long __meminitdata table_top;
67
68static int __initdata after_init_bootmem;
69
d6be89ad 70static __init void *alloc_low_page(void)
4e29684c
YL
71{
72 unsigned long pfn = table_end++;
73 void *adr;
74
75 if (pfn >= table_top)
76 panic("alloc_low_page: ran out of memory");
77
78 adr = __va(pfn * PAGE_SIZE);
79 memset(adr, 0, PAGE_SIZE);
4e29684c
YL
80 return adr;
81}
82
1da177e4
LT
83/*
84 * Creates a middle page table and puts a pointer to it in the
85 * given global directory entry. This only returns the gd entry
86 * in non-PAE compilation mode, since the middle layer is folded.
87 */
88static pmd_t * __init one_md_table_init(pgd_t *pgd)
89{
90 pud_t *pud;
91 pmd_t *pmd_table;
8550eb99 92
1da177e4 93#ifdef CONFIG_X86_PAE
b239fb25 94 if (!(pgd_val(*pgd) & _PAGE_PRESENT)) {
4e29684c
YL
95 if (after_init_bootmem)
96 pmd_table = (pmd_t *)alloc_bootmem_low_pages(PAGE_SIZE);
97 else
d6be89ad 98 pmd_table = (pmd_t *)alloc_low_page();
6944a9c8 99 paravirt_alloc_pmd(&init_mm, __pa(pmd_table) >> PAGE_SHIFT);
b239fb25
JF
100 set_pgd(pgd, __pgd(__pa(pmd_table) | _PAGE_PRESENT));
101 pud = pud_offset(pgd, 0);
8550eb99 102 BUG_ON(pmd_table != pmd_offset(pud, 0));
a376f30a
Z
103
104 return pmd_table;
b239fb25
JF
105 }
106#endif
1da177e4
LT
107 pud = pud_offset(pgd, 0);
108 pmd_table = pmd_offset(pud, 0);
8550eb99 109
1da177e4
LT
110 return pmd_table;
111}
112
113/*
114 * Create a page table and place a pointer to it in a middle page
8550eb99 115 * directory entry:
1da177e4
LT
116 */
117static pte_t * __init one_page_table_init(pmd_t *pmd)
118{
b239fb25 119 if (!(pmd_val(*pmd) & _PAGE_PRESENT)) {
509a80c4
IM
120 pte_t *page_table = NULL;
121
4e29684c 122 if (after_init_bootmem) {
509a80c4 123#ifdef CONFIG_DEBUG_PAGEALLOC
4e29684c 124 page_table = (pte_t *) alloc_bootmem_pages(PAGE_SIZE);
509a80c4 125#endif
4e29684c
YL
126 if (!page_table)
127 page_table =
509a80c4 128 (pte_t *)alloc_bootmem_low_pages(PAGE_SIZE);
d6be89ad
JB
129 } else
130 page_table = (pte_t *)alloc_low_page();
b239fb25 131
6944a9c8 132 paravirt_alloc_pte(&init_mm, __pa(page_table) >> PAGE_SHIFT);
1da177e4 133 set_pmd(pmd, __pmd(__pa(page_table) | _PAGE_TABLE));
b239fb25 134 BUG_ON(page_table != pte_offset_kernel(pmd, 0));
1da177e4 135 }
509a80c4 136
1da177e4
LT
137 return pte_offset_kernel(pmd, 0);
138}
139
11124411
TH
140void __init populate_extra_pte(unsigned long vaddr)
141{
142 int pgd_idx = pgd_index(vaddr);
143 int pmd_idx = pmd_index(vaddr);
144 pmd_t *pmd;
145
146 pmd = one_md_table_init(swapper_pg_dir + pgd_idx);
147 one_page_table_init(pmd + pmd_idx);
148}
149
a3c6018e
JB
150static pte_t *__init page_table_kmap_check(pte_t *pte, pmd_t *pmd,
151 unsigned long vaddr, pte_t *lastpte)
152{
153#ifdef CONFIG_HIGHMEM
154 /*
155 * Something (early fixmap) may already have put a pte
156 * page here, which causes the page table allocation
157 * to become nonlinear. Attempt to fix it, and if it
158 * is still nonlinear then we have to bug.
159 */
160 int pmd_idx_kmap_begin = fix_to_virt(FIX_KMAP_END) >> PMD_SHIFT;
161 int pmd_idx_kmap_end = fix_to_virt(FIX_KMAP_BEGIN) >> PMD_SHIFT;
162
163 if (pmd_idx_kmap_begin != pmd_idx_kmap_end
164 && (vaddr >> PMD_SHIFT) >= pmd_idx_kmap_begin
165 && (vaddr >> PMD_SHIFT) <= pmd_idx_kmap_end
166 && ((__pa(pte) >> PAGE_SHIFT) < table_start
167 || (__pa(pte) >> PAGE_SHIFT) >= table_end)) {
168 pte_t *newpte;
169 int i;
170
171 BUG_ON(after_init_bootmem);
172 newpte = alloc_low_page();
173 for (i = 0; i < PTRS_PER_PTE; i++)
174 set_pte(newpte + i, pte[i]);
175
176 paravirt_alloc_pte(&init_mm, __pa(newpte) >> PAGE_SHIFT);
177 set_pmd(pmd, __pmd(__pa(newpte)|_PAGE_TABLE));
178 BUG_ON(newpte != pte_offset_kernel(pmd, 0));
179 __flush_tlb_all();
180
181 paravirt_release_pte(__pa(pte) >> PAGE_SHIFT);
182 pte = newpte;
183 }
184 BUG_ON(vaddr < fix_to_virt(FIX_KMAP_BEGIN - 1)
185 && vaddr > fix_to_virt(FIX_KMAP_END)
186 && lastpte && lastpte + PTRS_PER_PTE != pte);
187#endif
188 return pte;
189}
190
1da177e4 191/*
8550eb99 192 * This function initializes a certain range of kernel virtual memory
1da177e4
LT
193 * with new bootmem page tables, everywhere page tables are missing in
194 * the given range.
8550eb99
IM
195 *
196 * NOTE: The pagetables are allocated contiguous on the physical space
197 * so we can cache the place of the first one and move around without
1da177e4
LT
198 * checking the pgd every time.
199 */
8550eb99
IM
200static void __init
201page_table_range_init(unsigned long start, unsigned long end, pgd_t *pgd_base)
1da177e4 202{
1da177e4
LT
203 int pgd_idx, pmd_idx;
204 unsigned long vaddr;
8550eb99
IM
205 pgd_t *pgd;
206 pmd_t *pmd;
a3c6018e 207 pte_t *pte = NULL;
1da177e4
LT
208
209 vaddr = start;
210 pgd_idx = pgd_index(vaddr);
211 pmd_idx = pmd_index(vaddr);
212 pgd = pgd_base + pgd_idx;
213
214 for ( ; (pgd_idx < PTRS_PER_PGD) && (vaddr != end); pgd++, pgd_idx++) {
b239fb25
JF
215 pmd = one_md_table_init(pgd);
216 pmd = pmd + pmd_index(vaddr);
8550eb99
IM
217 for (; (pmd_idx < PTRS_PER_PMD) && (vaddr != end);
218 pmd++, pmd_idx++) {
a3c6018e
JB
219 pte = page_table_kmap_check(one_page_table_init(pmd),
220 pmd, vaddr, pte);
1da177e4
LT
221
222 vaddr += PMD_SIZE;
223 }
224 pmd_idx = 0;
225 }
226}
227
228static inline int is_kernel_text(unsigned long addr)
229{
230 if (addr >= PAGE_OFFSET && addr <= (unsigned long)__init_end)
231 return 1;
232 return 0;
233}
234
235/*
8550eb99
IM
236 * This maps the physical memory to kernel virtual address space, a total
237 * of max_low_pfn pages, by creating page tables starting from address
238 * PAGE_OFFSET:
1da177e4 239 */
4e29684c 240static void __init kernel_physical_mapping_init(pgd_t *pgd_base,
a04ad82d
YL
241 unsigned long start_pfn,
242 unsigned long end_pfn,
243 int use_pse)
1da177e4 244{
8550eb99 245 int pgd_idx, pmd_idx, pte_ofs;
1da177e4
LT
246 unsigned long pfn;
247 pgd_t *pgd;
248 pmd_t *pmd;
249 pte_t *pte;
a2699e47
SS
250 unsigned pages_2m, pages_4k;
251 int mapping_iter;
252
253 /*
254 * First iteration will setup identity mapping using large/small pages
255 * based on use_pse, with other attributes same as set by
256 * the early code in head_32.S
257 *
258 * Second iteration will setup the appropriate attributes (NX, GLOBAL..)
259 * as desired for the kernel identity mapping.
260 *
261 * This two pass mechanism conforms to the TLB app note which says:
262 *
263 * "Software should not write to a paging-structure entry in a way
264 * that would change, for any linear address, both the page size
265 * and either the page frame or attributes."
266 */
267 mapping_iter = 1;
1da177e4 268
a04ad82d
YL
269 if (!cpu_has_pse)
270 use_pse = 0;
1da177e4 271
a2699e47
SS
272repeat:
273 pages_2m = pages_4k = 0;
a04ad82d
YL
274 pfn = start_pfn;
275 pgd_idx = pgd_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
276 pgd = pgd_base + pgd_idx;
1da177e4
LT
277 for (; pgd_idx < PTRS_PER_PGD; pgd++, pgd_idx++) {
278 pmd = one_md_table_init(pgd);
8550eb99 279
a04ad82d
YL
280 if (pfn >= end_pfn)
281 continue;
282#ifdef CONFIG_X86_PAE
283 pmd_idx = pmd_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
284 pmd += pmd_idx;
285#else
286 pmd_idx = 0;
287#endif
288 for (; pmd_idx < PTRS_PER_PMD && pfn < end_pfn;
f3f20de8 289 pmd++, pmd_idx++) {
8550eb99 290 unsigned int addr = pfn * PAGE_SIZE + PAGE_OFFSET;
1da177e4 291
8550eb99
IM
292 /*
293 * Map with big pages if possible, otherwise
294 * create normal page tables:
295 */
a04ad82d 296 if (use_pse) {
8550eb99 297 unsigned int addr2;
f3f20de8 298 pgprot_t prot = PAGE_KERNEL_LARGE;
a2699e47
SS
299 /*
300 * first pass will use the same initial
301 * identity mapping attribute + _PAGE_PSE.
302 */
303 pgprot_t init_prot =
304 __pgprot(PTE_IDENT_ATTR |
305 _PAGE_PSE);
f3f20de8 306
8550eb99 307 addr2 = (pfn + PTRS_PER_PTE-1) * PAGE_SIZE +
f3f20de8
JF
308 PAGE_OFFSET + PAGE_SIZE-1;
309
8550eb99
IM
310 if (is_kernel_text(addr) ||
311 is_kernel_text(addr2))
f3f20de8
JF
312 prot = PAGE_KERNEL_LARGE_EXEC;
313
ce0c0e50 314 pages_2m++;
a2699e47
SS
315 if (mapping_iter == 1)
316 set_pmd(pmd, pfn_pmd(pfn, init_prot));
317 else
318 set_pmd(pmd, pfn_pmd(pfn, prot));
b239fb25 319
1da177e4 320 pfn += PTRS_PER_PTE;
8550eb99
IM
321 continue;
322 }
323 pte = one_page_table_init(pmd);
1da177e4 324
a04ad82d
YL
325 pte_ofs = pte_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
326 pte += pte_ofs;
327 for (; pte_ofs < PTRS_PER_PTE && pfn < end_pfn;
8550eb99
IM
328 pte++, pfn++, pte_ofs++, addr += PAGE_SIZE) {
329 pgprot_t prot = PAGE_KERNEL;
a2699e47
SS
330 /*
331 * first pass will use the same initial
332 * identity mapping attribute.
333 */
334 pgprot_t init_prot = __pgprot(PTE_IDENT_ATTR);
f3f20de8 335
8550eb99
IM
336 if (is_kernel_text(addr))
337 prot = PAGE_KERNEL_EXEC;
f3f20de8 338
ce0c0e50 339 pages_4k++;
a2699e47
SS
340 if (mapping_iter == 1)
341 set_pte(pte, pfn_pte(pfn, init_prot));
342 else
343 set_pte(pte, pfn_pte(pfn, prot));
1da177e4
LT
344 }
345 }
346 }
a2699e47
SS
347 if (mapping_iter == 1) {
348 /*
349 * update direct mapping page count only in the first
350 * iteration.
351 */
352 update_page_count(PG_LEVEL_2M, pages_2m);
353 update_page_count(PG_LEVEL_4K, pages_4k);
354
355 /*
356 * local global flush tlb, which will flush the previous
357 * mappings present in both small and large page TLB's.
358 */
359 __flush_tlb_all();
360
361 /*
362 * Second iteration will set the actual desired PTE attributes.
363 */
364 mapping_iter = 2;
365 goto repeat;
366 }
1da177e4
LT
367}
368
ae531c26
AV
369/*
370 * devmem_is_allowed() checks to see if /dev/mem access to a certain address
371 * is valid. The argument is a physical page number.
372 *
373 *
374 * On x86, access has to be given to the first megabyte of ram because that area
375 * contains bios code and data regions used by X and dosemu and similar apps.
376 * Access has to be given to non-kernel-ram areas as well, these contain the PCI
377 * mmio resources as well as potential bios/acpi data regions.
378 */
379int devmem_is_allowed(unsigned long pagenr)
380{
381 if (pagenr <= 256)
382 return 1;
e8de1481
AV
383 if (iomem_is_exclusive(pagenr << PAGE_SHIFT))
384 return 0;
ae531c26
AV
385 if (!page_is_ram(pagenr))
386 return 1;
387 return 0;
388}
389
1da177e4
LT
390pte_t *kmap_pte;
391pgprot_t kmap_prot;
392
8550eb99
IM
393static inline pte_t *kmap_get_fixmap_pte(unsigned long vaddr)
394{
395 return pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr),
396 vaddr), vaddr), vaddr);
397}
1da177e4
LT
398
399static void __init kmap_init(void)
400{
401 unsigned long kmap_vstart;
402
8550eb99
IM
403 /*
404 * Cache the first kmap pte:
405 */
1da177e4
LT
406 kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
407 kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
408
409 kmap_prot = PAGE_KERNEL;
410}
411
fd940934 412#ifdef CONFIG_HIGHMEM
1da177e4
LT
413static void __init permanent_kmaps_init(pgd_t *pgd_base)
414{
8550eb99 415 unsigned long vaddr;
1da177e4
LT
416 pgd_t *pgd;
417 pud_t *pud;
418 pmd_t *pmd;
419 pte_t *pte;
1da177e4
LT
420
421 vaddr = PKMAP_BASE;
422 page_table_range_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base);
423
424 pgd = swapper_pg_dir + pgd_index(vaddr);
425 pud = pud_offset(pgd, vaddr);
426 pmd = pmd_offset(pud, vaddr);
427 pte = pte_offset_kernel(pmd, vaddr);
8550eb99 428 pkmap_page_table = pte;
1da177e4
LT
429}
430
cc9f7a0c 431static void __init add_one_highpage_init(struct page *page, int pfn)
1da177e4 432{
cc9f7a0c
YL
433 ClearPageReserved(page);
434 init_page_count(page);
435 __free_page(page);
436 totalhigh_pages++;
1da177e4
LT
437}
438
b5bc6c0e
YL
439struct add_highpages_data {
440 unsigned long start_pfn;
441 unsigned long end_pfn;
b5bc6c0e
YL
442};
443
d52d53b8 444static int __init add_highpages_work_fn(unsigned long start_pfn,
b5bc6c0e 445 unsigned long end_pfn, void *datax)
1da177e4 446{
b5bc6c0e
YL
447 int node_pfn;
448 struct page *page;
449 unsigned long final_start_pfn, final_end_pfn;
450 struct add_highpages_data *data;
8550eb99 451
b5bc6c0e 452 data = (struct add_highpages_data *)datax;
b5bc6c0e
YL
453
454 final_start_pfn = max(start_pfn, data->start_pfn);
455 final_end_pfn = min(end_pfn, data->end_pfn);
456 if (final_start_pfn >= final_end_pfn)
d52d53b8 457 return 0;
b5bc6c0e
YL
458
459 for (node_pfn = final_start_pfn; node_pfn < final_end_pfn;
460 node_pfn++) {
461 if (!pfn_valid(node_pfn))
462 continue;
463 page = pfn_to_page(node_pfn);
cc9f7a0c 464 add_one_highpage_init(page, node_pfn);
23be8c7d 465 }
b5bc6c0e 466
d52d53b8
YL
467 return 0;
468
b5bc6c0e
YL
469}
470
471void __init add_highpages_with_active_regions(int nid, unsigned long start_pfn,
cc9f7a0c 472 unsigned long end_pfn)
b5bc6c0e
YL
473{
474 struct add_highpages_data data;
475
476 data.start_pfn = start_pfn;
477 data.end_pfn = end_pfn;
b5bc6c0e
YL
478
479 work_with_active_regions(nid, add_highpages_work_fn, &data);
480}
481
8550eb99 482#ifndef CONFIG_NUMA
cc9f7a0c 483static void __init set_highmem_pages_init(void)
1da177e4 484{
cc9f7a0c 485 add_highpages_with_active_regions(0, highstart_pfn, highend_pfn);
b5bc6c0e 486
1da177e4
LT
487 totalram_pages += totalhigh_pages;
488}
8550eb99 489#endif /* !CONFIG_NUMA */
1da177e4
LT
490
491#else
e8e32326
IB
492static inline void permanent_kmaps_init(pgd_t *pgd_base)
493{
494}
495static inline void set_highmem_pages_init(void)
496{
497}
1da177e4
LT
498#endif /* CONFIG_HIGHMEM */
499
b239fb25 500void __init native_pagetable_setup_start(pgd_t *base)
1da177e4 501{
551889a6
IC
502 unsigned long pfn, va;
503 pgd_t *pgd;
504 pud_t *pud;
505 pmd_t *pmd;
506 pte_t *pte;
b239fb25
JF
507
508 /*
551889a6
IC
509 * Remove any mappings which extend past the end of physical
510 * memory from the boot time page table:
b239fb25 511 */
551889a6
IC
512 for (pfn = max_low_pfn + 1; pfn < 1<<(32-PAGE_SHIFT); pfn++) {
513 va = PAGE_OFFSET + (pfn<<PAGE_SHIFT);
514 pgd = base + pgd_index(va);
515 if (!pgd_present(*pgd))
516 break;
517
518 pud = pud_offset(pgd, va);
519 pmd = pmd_offset(pud, va);
520 if (!pmd_present(*pmd))
521 break;
522
523 pte = pte_offset_kernel(pmd, va);
524 if (!pte_present(*pte))
525 break;
526
527 pte_clear(NULL, va, pte);
528 }
6944a9c8 529 paravirt_alloc_pmd(&init_mm, __pa(base) >> PAGE_SHIFT);
b239fb25
JF
530}
531
532void __init native_pagetable_setup_done(pgd_t *base)
533{
b239fb25
JF
534}
535
536/*
537 * Build a proper pagetable for the kernel mappings. Up until this
538 * point, we've been running on some set of pagetables constructed by
539 * the boot process.
540 *
541 * If we're booting on native hardware, this will be a pagetable
551889a6
IC
542 * constructed in arch/x86/kernel/head_32.S. The root of the
543 * pagetable will be swapper_pg_dir.
b239fb25
JF
544 *
545 * If we're booting paravirtualized under a hypervisor, then there are
546 * more options: we may already be running PAE, and the pagetable may
547 * or may not be based in swapper_pg_dir. In any case,
548 * paravirt_pagetable_setup_start() will set up swapper_pg_dir
549 * appropriately for the rest of the initialization to work.
550 *
551 * In general, pagetable_init() assumes that the pagetable may already
552 * be partially populated, and so it avoids stomping on any existing
553 * mappings.
554 */
e7b37895 555static void __init early_ioremap_page_table_range_init(pgd_t *pgd_base)
b239fb25 556{
8550eb99 557 unsigned long vaddr, end;
b239fb25 558
1da177e4
LT
559 /*
560 * Fixed mappings, only the page table structure has to be
561 * created - mappings will be set by set_fixmap():
562 */
563 vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
b239fb25
JF
564 end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK;
565 page_table_range_init(vaddr, end, pgd_base);
beacfaac 566 early_ioremap_reset();
e7b37895
YL
567}
568
569static void __init pagetable_init(void)
570{
571 pgd_t *pgd_base = swapper_pg_dir;
572
1da177e4 573 permanent_kmaps_init(pgd_base);
1da177e4
LT
574}
575
a6eb84bc 576#ifdef CONFIG_ACPI_SLEEP
1da177e4 577/*
a6eb84bc 578 * ACPI suspend needs this for resume, because things like the intel-agp
1da177e4
LT
579 * driver might have split up a kernel 4MB mapping.
580 */
a6eb84bc 581char swsusp_pg_dir[PAGE_SIZE]
8550eb99 582 __attribute__ ((aligned(PAGE_SIZE)));
1da177e4
LT
583
584static inline void save_pg_dir(void)
585{
586 memcpy(swsusp_pg_dir, swapper_pg_dir, PAGE_SIZE);
587}
a6eb84bc 588#else /* !CONFIG_ACPI_SLEEP */
1da177e4
LT
589static inline void save_pg_dir(void)
590{
591}
a6eb84bc 592#endif /* !CONFIG_ACPI_SLEEP */
1da177e4 593
8550eb99 594void zap_low_mappings(void)
1da177e4
LT
595{
596 int i;
597
1da177e4
LT
598 /*
599 * Zap initial low-memory mappings.
600 *
601 * Note that "pgd_clear()" doesn't do it for
602 * us, because pgd_clear() is a no-op on i386.
603 */
68db065c 604 for (i = 0; i < KERNEL_PGD_BOUNDARY; i++) {
1da177e4
LT
605#ifdef CONFIG_X86_PAE
606 set_pgd(swapper_pg_dir+i, __pgd(1 + __pa(empty_zero_page)));
607#else
608 set_pgd(swapper_pg_dir+i, __pgd(0));
609#endif
8550eb99 610 }
1da177e4
LT
611 flush_tlb_all();
612}
613
8550eb99 614int nx_enabled;
d5321abe 615
be43d728 616pteval_t __supported_pte_mask __read_mostly = ~(_PAGE_NX | _PAGE_GLOBAL | _PAGE_IOMAP);
6fdc05d4
JF
617EXPORT_SYMBOL_GPL(__supported_pte_mask);
618
d5321abe
JB
619#ifdef CONFIG_X86_PAE
620
8550eb99 621static int disable_nx __initdata;
1da177e4
LT
622
623/*
624 * noexec = on|off
625 *
626 * Control non executable mappings.
627 *
628 * on Enable
629 * off Disable
630 */
1a3f239d 631static int __init noexec_setup(char *str)
1da177e4 632{
1a3f239d
RR
633 if (!str || !strcmp(str, "on")) {
634 if (cpu_has_nx) {
635 __supported_pte_mask |= _PAGE_NX;
636 disable_nx = 0;
637 }
8550eb99
IM
638 } else {
639 if (!strcmp(str, "off")) {
640 disable_nx = 1;
641 __supported_pte_mask &= ~_PAGE_NX;
642 } else {
643 return -EINVAL;
644 }
645 }
1a3f239d
RR
646
647 return 0;
1da177e4 648}
1a3f239d 649early_param("noexec", noexec_setup);
1da177e4 650
1da177e4
LT
651static void __init set_nx(void)
652{
653 unsigned int v[4], l, h;
654
655 if (cpu_has_pae && (cpuid_eax(0x80000000) > 0x80000001)) {
656 cpuid(0x80000001, &v[0], &v[1], &v[2], &v[3]);
8550eb99 657
1da177e4
LT
658 if ((v[3] & (1 << 20)) && !disable_nx) {
659 rdmsr(MSR_EFER, l, h);
660 l |= EFER_NX;
661 wrmsr(MSR_EFER, l, h);
662 nx_enabled = 1;
663 __supported_pte_mask |= _PAGE_NX;
664 }
665 }
666}
1da177e4
LT
667#endif
668
90d967e0
YL
669/* user-defined highmem size */
670static unsigned int highmem_pages = -1;
671
672/*
673 * highmem=size forces highmem to be exactly 'size' bytes.
674 * This works even on boxes that have no highmem otherwise.
675 * This also works to reduce highmem size on bigger boxes.
676 */
677static int __init parse_highmem(char *arg)
678{
679 if (!arg)
680 return -EINVAL;
681
682 highmem_pages = memparse(arg, &arg) >> PAGE_SHIFT;
683 return 0;
684}
685early_param("highmem", parse_highmem);
686
687/*
688 * Determine low and high memory ranges:
689 */
2ec65f8b 690void __init find_low_pfn_range(void)
90d967e0 691{
2ec65f8b
YL
692 /* it could update max_pfn */
693
346cafec 694 /* max_low_pfn is 0, we already have early_res support */
90d967e0
YL
695
696 max_low_pfn = max_pfn;
697 if (max_low_pfn > MAXMEM_PFN) {
698 if (highmem_pages == -1)
699 highmem_pages = max_pfn - MAXMEM_PFN;
700 if (highmem_pages + MAXMEM_PFN < max_pfn)
701 max_pfn = MAXMEM_PFN + highmem_pages;
702 if (highmem_pages + MAXMEM_PFN > max_pfn) {
703 printk(KERN_WARNING "only %luMB highmem pages "
704 "available, ignoring highmem size of %uMB.\n",
705 pages_to_mb(max_pfn - MAXMEM_PFN),
706 pages_to_mb(highmem_pages));
707 highmem_pages = 0;
708 }
709 max_low_pfn = MAXMEM_PFN;
710#ifndef CONFIG_HIGHMEM
711 /* Maximum memory usable is what is directly addressable */
712 printk(KERN_WARNING "Warning only %ldMB will be used.\n",
713 MAXMEM>>20);
714 if (max_pfn > MAX_NONPAE_PFN)
715 printk(KERN_WARNING
716 "Use a HIGHMEM64G enabled kernel.\n");
717 else
718 printk(KERN_WARNING "Use a HIGHMEM enabled kernel.\n");
719 max_pfn = MAXMEM_PFN;
720#else /* !CONFIG_HIGHMEM */
721#ifndef CONFIG_HIGHMEM64G
722 if (max_pfn > MAX_NONPAE_PFN) {
723 max_pfn = MAX_NONPAE_PFN;
724 printk(KERN_WARNING "Warning only 4GB will be used."
725 "Use a HIGHMEM64G enabled kernel.\n");
726 }
727#endif /* !CONFIG_HIGHMEM64G */
728#endif /* !CONFIG_HIGHMEM */
729 } else {
730 if (highmem_pages == -1)
731 highmem_pages = 0;
732#ifdef CONFIG_HIGHMEM
733 if (highmem_pages >= max_pfn) {
734 printk(KERN_ERR "highmem size specified (%uMB) is "
735 "bigger than pages available (%luMB)!.\n",
736 pages_to_mb(highmem_pages),
737 pages_to_mb(max_pfn));
738 highmem_pages = 0;
739 }
740 if (highmem_pages) {
741 if (max_low_pfn - highmem_pages <
742 64*1024*1024/PAGE_SIZE){
743 printk(KERN_ERR "highmem size %uMB results in "
744 "smaller than 64MB lowmem, ignoring it.\n"
745 , pages_to_mb(highmem_pages));
746 highmem_pages = 0;
747 }
748 max_low_pfn -= highmem_pages;
749 }
750#else
751 if (highmem_pages)
752 printk(KERN_ERR "ignoring highmem size on non-highmem"
753 " kernel!\n");
754#endif
755 }
90d967e0
YL
756}
757
b2ac82a0 758#ifndef CONFIG_NEED_MULTIPLE_NODES
2ec65f8b 759void __init initmem_init(unsigned long start_pfn,
b2ac82a0
YL
760 unsigned long end_pfn)
761{
b2ac82a0
YL
762#ifdef CONFIG_HIGHMEM
763 highstart_pfn = highend_pfn = max_pfn;
764 if (max_pfn > max_low_pfn)
765 highstart_pfn = max_low_pfn;
766 memory_present(0, 0, highend_pfn);
cb95a13a 767 e820_register_active_regions(0, 0, highend_pfn);
b2ac82a0
YL
768 printk(KERN_NOTICE "%ldMB HIGHMEM available.\n",
769 pages_to_mb(highend_pfn - highstart_pfn));
770 num_physpages = highend_pfn;
771 high_memory = (void *) __va(highstart_pfn * PAGE_SIZE - 1) + 1;
772#else
773 memory_present(0, 0, max_low_pfn);
cb95a13a 774 e820_register_active_regions(0, 0, max_low_pfn);
b2ac82a0
YL
775 num_physpages = max_low_pfn;
776 high_memory = (void *) __va(max_low_pfn * PAGE_SIZE - 1) + 1;
777#endif
778#ifdef CONFIG_FLATMEM
779 max_mapnr = num_physpages;
780#endif
781 printk(KERN_NOTICE "%ldMB LOWMEM available.\n",
782 pages_to_mb(max_low_pfn));
783
784 setup_bootmem_allocator();
b2ac82a0 785}
cb95a13a 786#endif /* !CONFIG_NEED_MULTIPLE_NODES */
b2ac82a0 787
cb95a13a 788static void __init zone_sizes_init(void)
b2ac82a0
YL
789{
790 unsigned long max_zone_pfns[MAX_NR_ZONES];
791 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
792 max_zone_pfns[ZONE_DMA] =
793 virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
794 max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
b2ac82a0
YL
795#ifdef CONFIG_HIGHMEM
796 max_zone_pfns[ZONE_HIGHMEM] = highend_pfn;
b2ac82a0
YL
797#endif
798
799 free_area_init_nodes(max_zone_pfns);
800}
b2ac82a0 801
b2ac82a0
YL
802void __init setup_bootmem_allocator(void)
803{
804 int i;
805 unsigned long bootmap_size, bootmap;
806 /*
807 * Initialize the boot-time allocator (with low memory only):
808 */
809 bootmap_size = bootmem_bootmap_pages(max_low_pfn)<<PAGE_SHIFT;
810 bootmap = find_e820_area(min_low_pfn<<PAGE_SHIFT,
811 max_pfn_mapped<<PAGE_SHIFT, bootmap_size,
812 PAGE_SIZE);
813 if (bootmap == -1L)
814 panic("Cannot find bootmem map of size %ld\n", bootmap_size);
815 reserve_early(bootmap, bootmap + bootmap_size, "BOOTMAP");
225c37d7 816
346cafec
YL
817 /* don't touch min_low_pfn */
818 bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap >> PAGE_SHIFT,
819 min_low_pfn, max_low_pfn);
b2ac82a0
YL
820 printk(KERN_INFO " mapped low ram: 0 - %08lx\n",
821 max_pfn_mapped<<PAGE_SHIFT);
822 printk(KERN_INFO " low ram: %08lx - %08lx\n",
823 min_low_pfn<<PAGE_SHIFT, max_low_pfn<<PAGE_SHIFT);
824 printk(KERN_INFO " bootmap %08lx - %08lx\n",
825 bootmap, bootmap + bootmap_size);
826 for_each_online_node(i)
827 free_bootmem_with_active_regions(i, max_low_pfn);
828 early_res_to_bootmem(0, max_low_pfn<<PAGE_SHIFT);
829
4e29684c 830 after_init_bootmem = 1;
b2ac82a0
YL
831}
832
0b8fdcbc 833static void __init find_early_table_space(unsigned long end, int use_pse)
4e29684c 834{
7482b0e9 835 unsigned long puds, pmds, ptes, tables, start;
4e29684c
YL
836
837 puds = (end + PUD_SIZE - 1) >> PUD_SHIFT;
838 tables = PAGE_ALIGN(puds * sizeof(pud_t));
839
840 pmds = (end + PMD_SIZE - 1) >> PMD_SHIFT;
841 tables += PAGE_ALIGN(pmds * sizeof(pmd_t));
842
0b8fdcbc 843 if (use_pse) {
7482b0e9 844 unsigned long extra;
a04ad82d
YL
845
846 extra = end - ((end>>PMD_SHIFT) << PMD_SHIFT);
847 extra += PMD_SIZE;
7482b0e9
YL
848 ptes = (extra + PAGE_SIZE - 1) >> PAGE_SHIFT;
849 } else
850 ptes = (end + PAGE_SIZE - 1) >> PAGE_SHIFT;
851
852 tables += PAGE_ALIGN(ptes * sizeof(pte_t));
8207c257 853
a04ad82d 854 /* for fixmap */
a3c6018e 855 tables += PAGE_ALIGN(__end_of_fixed_addresses * sizeof(pte_t));
a04ad82d 856
4e29684c
YL
857 /*
858 * RED-PEN putting page tables only on node 0 could
859 * cause a hotspot and fill up ZONE_DMA. The page tables
860 * need roughly 0.5KB per GB.
861 */
862 start = 0x7000;
863 table_start = find_e820_area(start, max_pfn_mapped<<PAGE_SHIFT,
864 tables, PAGE_SIZE);
865 if (table_start == -1UL)
866 panic("Cannot find space for the kernel page tables");
867
868 table_start >>= PAGE_SHIFT;
869 table_end = table_start;
870 table_top = table_start + (tables>>PAGE_SHIFT);
871
872 printk(KERN_DEBUG "kernel direct mapping tables up to %lx @ %lx-%lx\n",
873 end, table_start << PAGE_SHIFT,
874 (table_start << PAGE_SHIFT) + tables);
875}
876
877unsigned long __init_refok init_memory_mapping(unsigned long start,
878 unsigned long end)
879{
880 pgd_t *pgd_base = swapper_pg_dir;
a04ad82d
YL
881 unsigned long start_pfn, end_pfn;
882 unsigned long big_page_start;
0b8fdcbc
SS
883#ifdef CONFIG_DEBUG_PAGEALLOC
884 /*
885 * For CONFIG_DEBUG_PAGEALLOC, identity mapping will use small pages.
886 * This will simplify cpa(), which otherwise needs to support splitting
887 * large pages into small in interrupt context, etc.
888 */
889 int use_pse = 0;
890#else
891 int use_pse = cpu_has_pse;
892#endif
4e29684c
YL
893
894 /*
895 * Find space for the kernel direct mapping tables.
896 */
897 if (!after_init_bootmem)
0b8fdcbc 898 find_early_table_space(end, use_pse);
4e29684c
YL
899
900#ifdef CONFIG_X86_PAE
901 set_nx();
902 if (nx_enabled)
903 printk(KERN_INFO "NX (Execute Disable) protection: active\n");
904#endif
905
906 /* Enable PSE if available */
907 if (cpu_has_pse)
908 set_in_cr4(X86_CR4_PSE);
909
910 /* Enable PGE if available */
911 if (cpu_has_pge) {
912 set_in_cr4(X86_CR4_PGE);
ef5e94af 913 __supported_pte_mask |= _PAGE_GLOBAL;
4e29684c
YL
914 }
915
a04ad82d
YL
916 /*
917 * Don't use a large page for the first 2/4MB of memory
918 * because there are often fixed size MTRRs in there
919 * and overlapping MTRRs into large pages can cause
920 * slowdowns.
921 */
922 big_page_start = PMD_SIZE;
923
924 if (start < big_page_start) {
925 start_pfn = start >> PAGE_SHIFT;
926 end_pfn = min(big_page_start>>PAGE_SHIFT, end>>PAGE_SHIFT);
927 } else {
928 /* head is not big page alignment ? */
929 start_pfn = start >> PAGE_SHIFT;
930 end_pfn = ((start + (PMD_SIZE - 1))>>PMD_SHIFT)
931 << (PMD_SHIFT - PAGE_SHIFT);
932 }
933 if (start_pfn < end_pfn)
934 kernel_physical_mapping_init(pgd_base, start_pfn, end_pfn, 0);
935
936 /* big page range */
937 start_pfn = ((start + (PMD_SIZE - 1))>>PMD_SHIFT)
938 << (PMD_SHIFT - PAGE_SHIFT);
939 if (start_pfn < (big_page_start >> PAGE_SHIFT))
940 start_pfn = big_page_start >> PAGE_SHIFT;
941 end_pfn = (end>>PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
942 if (start_pfn < end_pfn)
943 kernel_physical_mapping_init(pgd_base, start_pfn, end_pfn,
0b8fdcbc 944 use_pse);
a04ad82d
YL
945
946 /* tail is not big page alignment ? */
947 start_pfn = end_pfn;
948 if (start_pfn > (big_page_start>>PAGE_SHIFT)) {
949 end_pfn = end >> PAGE_SHIFT;
950 if (start_pfn < end_pfn)
951 kernel_physical_mapping_init(pgd_base, start_pfn,
952 end_pfn, 0);
953 }
4e29684c 954
e7b37895
YL
955 early_ioremap_page_table_range_init(pgd_base);
956
4e29684c
YL
957 load_cr3(swapper_pg_dir);
958
959 __flush_tlb_all();
960
961 if (!after_init_bootmem)
962 reserve_early(table_start << PAGE_SHIFT,
963 table_end << PAGE_SHIFT, "PGTABLE");
964
caadbdce
YL
965 if (!after_init_bootmem)
966 early_memtest(start, end);
967
4e29684c
YL
968 return end >> PAGE_SHIFT;
969}
970
e7b37895 971
1da177e4
LT
972/*
973 * paging_init() sets up the page tables - note that the first 8MB are
974 * already mapped by head.S.
975 *
976 * This routines also unmaps the page at virtual kernel address 0, so
977 * that we can trap those pesky NULL-reference errors in the kernel.
978 */
979void __init paging_init(void)
980{
1da177e4
LT
981 pagetable_init();
982
1da177e4
LT
983 __flush_tlb_all();
984
985 kmap_init();
11cd0bc1
YL
986
987 /*
988 * NOTE: at this point the bootmem allocator is fully available.
989 */
11cd0bc1
YL
990 sparse_init();
991 zone_sizes_init();
1da177e4
LT
992}
993
994/*
995 * Test if the WP bit works in supervisor mode. It isn't supported on 386's
f7f17a67
DV
996 * and also on some strange 486's. All 586+'s are OK. This used to involve
997 * black magic jumps to work around some nasty CPU bugs, but fortunately the
998 * switch to using exceptions got rid of all that.
1da177e4 999 */
1da177e4
LT
1000static void __init test_wp_bit(void)
1001{
d7d119d7
IM
1002 printk(KERN_INFO
1003 "Checking if this processor honours the WP bit even in supervisor mode...");
1da177e4
LT
1004
1005 /* Any page-aligned address will do, the test is non-destructive */
1006 __set_fixmap(FIX_WP_TEST, __pa(&swapper_pg_dir), PAGE_READONLY);
1007 boot_cpu_data.wp_works_ok = do_test_wp_bit();
1008 clear_fixmap(FIX_WP_TEST);
1009
1010 if (!boot_cpu_data.wp_works_ok) {
d7d119d7 1011 printk(KERN_CONT "No.\n");
1da177e4 1012#ifdef CONFIG_X86_WP_WORKS_OK
d7d119d7
IM
1013 panic(
1014 "This kernel doesn't support CPU's with broken WP. Recompile it for a 386!");
1da177e4
LT
1015#endif
1016 } else {
d7d119d7 1017 printk(KERN_CONT "Ok.\n");
1da177e4
LT
1018 }
1019}
1020
8550eb99 1021static struct kcore_list kcore_mem, kcore_vmalloc;
1da177e4
LT
1022
1023void __init mem_init(void)
1024{
1da177e4 1025 int codesize, reservedpages, datasize, initsize;
cc9f7a0c 1026 int tmp;
1da177e4 1027
cfb80c9e
JF
1028 pci_iommu_alloc();
1029
05b79bdc 1030#ifdef CONFIG_FLATMEM
8d8f3cbe 1031 BUG_ON(!mem_map);
1da177e4 1032#endif
1da177e4
LT
1033 /* this will put all low memory onto the freelists */
1034 totalram_pages += free_all_bootmem();
1035
1036 reservedpages = 0;
1037 for (tmp = 0; tmp < max_low_pfn; tmp++)
1038 /*
8550eb99 1039 * Only count reserved RAM pages:
1da177e4
LT
1040 */
1041 if (page_is_ram(tmp) && PageReserved(pfn_to_page(tmp)))
1042 reservedpages++;
1043
cc9f7a0c 1044 set_highmem_pages_init();
1da177e4
LT
1045
1046 codesize = (unsigned long) &_etext - (unsigned long) &_text;
1047 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
1048 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
1049
8550eb99
IM
1050 kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
1051 kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
1da177e4
LT
1052 VMALLOC_END-VMALLOC_START);
1053
8550eb99
IM
1054 printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, "
1055 "%dk reserved, %dk data, %dk init, %ldk highmem)\n",
1da177e4
LT
1056 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
1057 num_physpages << (PAGE_SHIFT-10),
1058 codesize >> 10,
1059 reservedpages << (PAGE_SHIFT-10),
1060 datasize >> 10,
1061 initsize >> 10,
1062 (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10))
1063 );
1064
d7d119d7 1065 printk(KERN_INFO "virtual kernel memory layout:\n"
8550eb99 1066 " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
052e7994 1067#ifdef CONFIG_HIGHMEM
8550eb99 1068 " pkmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
052e7994 1069#endif
8550eb99
IM
1070 " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n"
1071 " lowmem : 0x%08lx - 0x%08lx (%4ld MB)\n"
1072 " .init : 0x%08lx - 0x%08lx (%4ld kB)\n"
1073 " .data : 0x%08lx - 0x%08lx (%4ld kB)\n"
1074 " .text : 0x%08lx - 0x%08lx (%4ld kB)\n",
1075 FIXADDR_START, FIXADDR_TOP,
1076 (FIXADDR_TOP - FIXADDR_START) >> 10,
052e7994
JF
1077
1078#ifdef CONFIG_HIGHMEM
8550eb99
IM
1079 PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE,
1080 (LAST_PKMAP*PAGE_SIZE) >> 10,
052e7994
JF
1081#endif
1082
8550eb99
IM
1083 VMALLOC_START, VMALLOC_END,
1084 (VMALLOC_END - VMALLOC_START) >> 20,
052e7994 1085
8550eb99
IM
1086 (unsigned long)__va(0), (unsigned long)high_memory,
1087 ((unsigned long)high_memory - (unsigned long)__va(0)) >> 20,
052e7994 1088
8550eb99
IM
1089 (unsigned long)&__init_begin, (unsigned long)&__init_end,
1090 ((unsigned long)&__init_end -
1091 (unsigned long)&__init_begin) >> 10,
052e7994 1092
8550eb99
IM
1093 (unsigned long)&_etext, (unsigned long)&_edata,
1094 ((unsigned long)&_edata - (unsigned long)&_etext) >> 10,
052e7994 1095
8550eb99
IM
1096 (unsigned long)&_text, (unsigned long)&_etext,
1097 ((unsigned long)&_etext - (unsigned long)&_text) >> 10);
052e7994 1098
beeb4195
JB
1099 /*
1100 * Check boundaries twice: Some fundamental inconsistencies can
1101 * be detected at build time already.
1102 */
1103#define __FIXADDR_TOP (-PAGE_SIZE)
1104#ifdef CONFIG_HIGHMEM
1105 BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP*PAGE_SIZE > FIXADDR_START);
1106 BUILD_BUG_ON(VMALLOC_END > PKMAP_BASE);
1107#endif
1108#define high_memory (-128UL << 20)
1109 BUILD_BUG_ON(VMALLOC_START >= VMALLOC_END);
1110#undef high_memory
1111#undef __FIXADDR_TOP
1112
052e7994 1113#ifdef CONFIG_HIGHMEM
8550eb99
IM
1114 BUG_ON(PKMAP_BASE + LAST_PKMAP*PAGE_SIZE > FIXADDR_START);
1115 BUG_ON(VMALLOC_END > PKMAP_BASE);
052e7994 1116#endif
beeb4195 1117 BUG_ON(VMALLOC_START >= VMALLOC_END);
8550eb99 1118 BUG_ON((unsigned long)high_memory > VMALLOC_START);
052e7994 1119
1da177e4
LT
1120 if (boot_cpu_data.wp_works_ok < 0)
1121 test_wp_bit();
1122
61165d7a 1123 save_pg_dir();
1da177e4 1124 zap_low_mappings();
1da177e4
LT
1125}
1126
ad8f5797 1127#ifdef CONFIG_MEMORY_HOTPLUG
bc02af93 1128int arch_add_memory(int nid, u64 start, u64 size)
05039b92 1129{
7c7e9425 1130 struct pglist_data *pgdata = NODE_DATA(nid);
776ed98b 1131 struct zone *zone = pgdata->node_zones + ZONE_HIGHMEM;
05039b92
DH
1132 unsigned long start_pfn = start >> PAGE_SHIFT;
1133 unsigned long nr_pages = size >> PAGE_SHIFT;
1134
c04fc586 1135 return __add_pages(nid, zone, start_pfn, nr_pages);
05039b92 1136}
9d99aaa3 1137#endif
05039b92 1138
1da177e4
LT
1139/*
1140 * This function cannot be __init, since exceptions don't work in that
1141 * section. Put this after the callers, so that it cannot be inlined.
1142 */
8550eb99 1143static noinline int do_test_wp_bit(void)
1da177e4
LT
1144{
1145 char tmp_reg;
1146 int flag;
1147
1148 __asm__ __volatile__(
8550eb99
IM
1149 " movb %0, %1 \n"
1150 "1: movb %1, %0 \n"
1151 " xorl %2, %2 \n"
1da177e4 1152 "2: \n"
f832ff18 1153 _ASM_EXTABLE(1b,2b)
1da177e4
LT
1154 :"=m" (*(char *)fix_to_virt(FIX_WP_TEST)),
1155 "=q" (tmp_reg),
1156 "=r" (flag)
1157 :"2" (1)
1158 :"memory");
8550eb99 1159
1da177e4
LT
1160 return flag;
1161}
1162
63aaf308 1163#ifdef CONFIG_DEBUG_RODATA
edeed305
AV
1164const int rodata_test_data = 0xC3;
1165EXPORT_SYMBOL_GPL(rodata_test_data);
63aaf308 1166
63aaf308
AV
1167void mark_rodata_ro(void)
1168{
6fb14755
JB
1169 unsigned long start = PFN_ALIGN(_text);
1170 unsigned long size = PFN_ALIGN(_etext) - start;
63aaf308 1171
8f0f996e
SR
1172#ifndef CONFIG_DYNAMIC_FTRACE
1173 /* Dynamic tracing modifies the kernel text section */
4e4eee0e
MD
1174 set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
1175 printk(KERN_INFO "Write protecting the kernel text: %luk\n",
1176 size >> 10);
0c42f392
AK
1177
1178#ifdef CONFIG_CPA_DEBUG
4e4eee0e
MD
1179 printk(KERN_INFO "Testing CPA: Reverting %lx-%lx\n",
1180 start, start+size);
1181 set_pages_rw(virt_to_page(start), size>>PAGE_SHIFT);
0c42f392 1182
4e4eee0e
MD
1183 printk(KERN_INFO "Testing CPA: write protecting again\n");
1184 set_pages_ro(virt_to_page(start), size>>PAGE_SHIFT);
602033ed 1185#endif
8f0f996e
SR
1186#endif /* CONFIG_DYNAMIC_FTRACE */
1187
6fb14755
JB
1188 start += size;
1189 size = (unsigned long)__end_rodata - start;
6d238cc4 1190 set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
d7d119d7
IM
1191 printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
1192 size >> 10);
edeed305 1193 rodata_test();
63aaf308 1194
0c42f392 1195#ifdef CONFIG_CPA_DEBUG
d7d119d7 1196 printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, start + size);
6d238cc4 1197 set_pages_rw(virt_to_page(start), size >> PAGE_SHIFT);
0c42f392 1198
d7d119d7 1199 printk(KERN_INFO "Testing CPA: write protecting again\n");
6d238cc4 1200 set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
0c42f392 1201#endif
63aaf308
AV
1202}
1203#endif
1204
9a0b5817
GH
1205void free_init_pages(char *what, unsigned long begin, unsigned long end)
1206{
ee01f112
IM
1207#ifdef CONFIG_DEBUG_PAGEALLOC
1208 /*
1209 * If debugging page accesses then do not free this memory but
1210 * mark them not present - any buggy init-section access will
1211 * create a kernel page fault:
1212 */
1213 printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n",
1214 begin, PAGE_ALIGN(end));
1215 set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
1216#else
86f03989
IM
1217 unsigned long addr;
1218
3c1df68b
AV
1219 /*
1220 * We just marked the kernel text read only above, now that
1221 * we are going to free part of that, we need to make that
1222 * writeable first.
1223 */
1224 set_memory_rw(begin, (end - begin) >> PAGE_SHIFT);
1225
9a0b5817 1226 for (addr = begin; addr < end; addr += PAGE_SIZE) {
e3ebadd9
LT
1227 ClearPageReserved(virt_to_page(addr));
1228 init_page_count(virt_to_page(addr));
1229 memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
1230 free_page(addr);
9a0b5817
GH
1231 totalram_pages++;
1232 }
6fb14755 1233 printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
ee01f112 1234#endif
9a0b5817
GH
1235}
1236
1237void free_initmem(void)
1238{
1239 free_init_pages("unused kernel memory",
e3ebadd9
LT
1240 (unsigned long)(&__init_begin),
1241 (unsigned long)(&__init_end));
9a0b5817 1242}
63aaf308 1243
1da177e4
LT
1244#ifdef CONFIG_BLK_DEV_INITRD
1245void free_initrd_mem(unsigned long start, unsigned long end)
1246{
e3ebadd9 1247 free_init_pages("initrd memory", start, end);
1da177e4
LT
1248}
1249#endif
d2dbf343
YL
1250
1251int __init reserve_bootmem_generic(unsigned long phys, unsigned long len,
1252 int flags)
1253{
1254 return reserve_bootmem(phys, len, flags);
1255}