3 * Copyright (C) 1995 Linus Torvalds
5 * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
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>
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>
24 #include <linux/pci.h>
25 #include <linux/pfn.h>
26 #include <linux/poison.h>
27 #include <linux/bootmem.h>
28 #include <linux/memblock.h>
29 #include <linux/proc_fs.h>
30 #include <linux/memory_hotplug.h>
31 #include <linux/initrd.h>
32 #include <linux/cpumask.h>
33 #include <linux/gfp.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/scatterlist.h>
38 #include <asm/bios_ebda.h>
39 #include <asm/processor.h>
40 #include <asm/uaccess.h>
41 #include <asm/pgtable.h>
43 #include <asm/fixmap.h>
48 #include <asm/tlbflush.h>
49 #include <asm/olpc_ofw.h>
50 #include <asm/pgalloc.h>
51 #include <asm/sections.h>
52 #include <asm/hypervisor.h>
53 #include <asm/swiotlb.h>
54 #include <asm/setup.h>
55 #include <asm/cacheflush.h>
56 #include <asm/page_types.h>
59 unsigned long highstart_pfn, highend_pfn;
61 static noinline int do_test_wp_bit(void);
63 bool __read_mostly __vmalloc_start_set = false;
65 static __init void *alloc_low_page(void)
67 unsigned long pfn = pgt_buf_end++;
70 if (pfn >= pgt_buf_top)
71 panic("alloc_low_page: ran out of memory");
73 adr = __va(pfn * PAGE_SIZE);
79 * Creates a middle page table and puts a pointer to it in the
80 * given global directory entry. This only returns the gd entry
81 * in non-PAE compilation mode, since the middle layer is folded.
83 static pmd_t * __init one_md_table_init(pgd_t *pgd)
89 if (!(__pgd_val(*pgd) & _PAGE_PRESENT)) {
91 pmd_table = (pmd_t *)alloc_bootmem_pages(PAGE_SIZE);
93 pmd_table = (pmd_t *)alloc_low_page();
94 paravirt_alloc_pmd(&init_mm, __pa(pmd_table) >> PAGE_SHIFT);
95 make_lowmem_page_readonly(pmd_table, XENFEAT_writable_page_tables);
96 set_pgd(pgd, __pgd(__pa(pmd_table) | _PAGE_PRESENT));
97 pud = pud_offset(pgd, 0);
98 BUG_ON(pmd_table != pmd_offset(pud, 0));
103 pud = pud_offset(pgd, 0);
104 pmd_table = pmd_offset(pud, 0);
110 * Create a page table and place a pointer to it in a middle page
113 static pte_t * __init one_page_table_init(pmd_t *pmd)
115 #if CONFIG_XEN_COMPAT <= 0x030002
116 if (pmd_none(*pmd)) {
118 if (!(__pmd_val(*pmd) & _PAGE_PRESENT)) {
120 pte_t *page_table = NULL;
123 #if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_KMEMCHECK)
124 page_table = (pte_t *) alloc_bootmem_pages(PAGE_SIZE);
128 (pte_t *)alloc_bootmem_pages(PAGE_SIZE);
130 page_table = (pte_t *)alloc_low_page();
132 paravirt_alloc_pte(&init_mm, __pa(page_table) >> PAGE_SHIFT);
133 make_lowmem_page_readonly(page_table,
134 XENFEAT_writable_page_tables);
135 set_pmd(pmd, __pmd(__pa(page_table) | _PAGE_TABLE));
136 BUG_ON(page_table != pte_offset_kernel(pmd, 0));
139 return pte_offset_kernel(pmd, 0);
142 pmd_t * __init populate_extra_pmd(unsigned long vaddr)
144 int pgd_idx = pgd_index(vaddr);
145 int pmd_idx = pmd_index(vaddr);
147 return one_md_table_init(swapper_pg_dir + pgd_idx) + pmd_idx;
150 pte_t * __init populate_extra_pte(unsigned long vaddr)
152 int pte_idx = pte_index(vaddr);
155 pmd = populate_extra_pmd(vaddr);
156 return one_page_table_init(pmd) + pte_idx;
159 static pte_t *__init page_table_kmap_check(pte_t *pte, pmd_t *pmd,
160 unsigned long vaddr, pte_t *lastpte)
162 #ifdef CONFIG_HIGHMEM
164 * Something (early fixmap) may already have put a pte
165 * page here, which causes the page table allocation
166 * to become nonlinear. Attempt to fix it, and if it
167 * is still nonlinear then we have to bug.
169 int pmd_idx_kmap_begin = fix_to_virt(FIX_KMAP_END) >> PMD_SHIFT;
170 int pmd_idx_kmap_end = fix_to_virt(FIX_KMAP_BEGIN) >> PMD_SHIFT;
172 if (pmd_idx_kmap_begin != pmd_idx_kmap_end
173 && (vaddr >> PMD_SHIFT) >= pmd_idx_kmap_begin
174 && (vaddr >> PMD_SHIFT) <= pmd_idx_kmap_end
175 && ((__pa(pte) >> PAGE_SHIFT) < pgt_buf_start
176 || (__pa(pte) >> PAGE_SHIFT) >= pgt_buf_end)) {
180 BUG_ON(after_bootmem);
181 newpte = alloc_low_page();
182 for (i = 0; i < PTRS_PER_PTE; i++)
183 set_pte(newpte + i, pte[i]);
185 paravirt_alloc_pte(&init_mm, __pa(newpte) >> PAGE_SHIFT);
186 make_lowmem_page_readonly(newpte,
187 XENFEAT_writable_page_tables);
188 set_pmd(pmd, __pmd(__pa(newpte)|_PAGE_TABLE));
189 BUG_ON(newpte != pte_offset_kernel(pmd, 0));
192 paravirt_release_pte(__pa(pte) >> PAGE_SHIFT);
193 make_lowmem_page_writable(pte,
194 XENFEAT_writable_page_tables);
197 BUG_ON(vaddr < fix_to_virt(FIX_KMAP_BEGIN - 1)
198 && vaddr > fix_to_virt(FIX_KMAP_END)
199 && lastpte && lastpte + PTRS_PER_PTE != pte);
205 * This function initializes a certain range of kernel virtual memory
206 * with new bootmem page tables, everywhere page tables are missing in
209 * NOTE: The pagetables are allocated contiguous on the physical space
210 * so we can cache the place of the first one and move around without
211 * checking the pgd every time.
214 page_table_range_init(unsigned long start, unsigned long end, pgd_t *pgd_base)
216 int pgd_idx, pmd_idx;
223 pgd_idx = pgd_index(vaddr);
224 pmd_idx = pmd_index(vaddr);
225 pgd = pgd_base + pgd_idx;
227 for ( ; (pgd_idx < PTRS_PER_PGD) && (vaddr != end); pgd++, pgd_idx++) {
228 pmd = one_md_table_init(pgd);
229 pmd = pmd + pmd_index(vaddr);
230 for (; (pmd_idx < PTRS_PER_PMD) && (vaddr != end);
232 if (vaddr >= hypervisor_virt_start)
234 pte = page_table_kmap_check(one_page_table_init(pmd),
243 static inline int is_kernel_text(unsigned long addr)
245 if (addr >= (unsigned long)_text && addr <= (unsigned long)__init_end)
251 * This maps the physical memory to kernel virtual address space, a total
252 * of max_low_pfn pages, by creating page tables starting from address
256 kernel_physical_mapping_init(unsigned long start,
258 unsigned long page_size_mask)
260 int use_pse = page_size_mask == (1<<PG_LEVEL_2M);
261 unsigned long last_map_addr = end;
262 unsigned long start_pfn, end_pfn;
263 pgd_t *pgd_base = swapper_pg_dir;
264 int pgd_idx, pmd_idx, pte_ofs;
269 unsigned pages_2m, pages_4k;
272 start_pfn = start >> PAGE_SHIFT;
273 end_pfn = end >> PAGE_SHIFT;
276 * First iteration will setup identity mapping using large/small pages
277 * based on use_pse, with other attributes same as set by
278 * the early code in head_32.S
280 * Second iteration will setup the appropriate attributes (NX, GLOBAL..)
281 * as desired for the kernel identity mapping.
283 * This two pass mechanism conforms to the TLB app note which says:
285 * "Software should not write to a paging-structure entry in a way
286 * that would change, for any linear address, both the page size
287 * and either the page frame or attributes."
297 pages_2m = pages_4k = 0;
299 pgd_idx = pgd_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
300 pgd = pgd_base + pgd_idx;
301 for (; pgd_idx < PTRS_PER_PGD; pgd++, pgd_idx++) {
304 * Native linux hasn't PAE-paging enabled yet at this
305 * point. When running as xen domain we are in PAE
306 * mode already, thus we can't simply hook a empty
307 * pmd. That would kill the mappings we are currently
310 pmd = pmd_offset(pud_offset(pgd, PAGE_OFFSET), PAGE_OFFSET);
312 pmd = one_md_table_init(pgd);
317 #ifdef CONFIG_X86_PAE
318 pmd_idx = pmd_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
323 for (; pmd_idx < PTRS_PER_PMD && pfn < end_pfn;
325 unsigned int addr = pfn * PAGE_SIZE + PAGE_OFFSET;
327 if (addr >= hypervisor_virt_start)
331 * Map with big pages if possible, otherwise
332 * create normal page tables:
336 pgprot_t prot = PAGE_KERNEL_LARGE;
338 * first pass will use the same initial
339 * identity mapping attribute + _PAGE_PSE.
342 __pgprot(PTE_IDENT_ATTR |
345 addr2 = (pfn + PTRS_PER_PTE-1) * PAGE_SIZE +
346 PAGE_OFFSET + PAGE_SIZE-1;
348 if (is_kernel_text(addr) ||
349 is_kernel_text(addr2))
350 prot = PAGE_KERNEL_LARGE_EXEC;
353 if (mapping_iter == 1)
354 set_pmd(pmd, pfn_pmd(pfn, init_prot));
356 set_pmd(pmd, pfn_pmd(pfn, prot));
361 pte = one_page_table_init(pmd);
363 pte_ofs = pte_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
365 for (; pte_ofs < PTRS_PER_PTE && pfn < end_pfn;
366 pte++, pfn++, pte_ofs++, addr += PAGE_SIZE) {
367 pgprot_t prot = PAGE_KERNEL;
369 * first pass will use the same initial
370 * identity mapping attribute.
372 pgprot_t init_prot = __pgprot(PTE_IDENT_ATTR);
374 /* XEN: Only map initial RAM allocation. */
375 if (pfn >= xen_start_info->nr_pages || pte_present(*pte))
377 if (is_kernel_text(addr))
378 prot = PAGE_KERNEL_EXEC;
381 if (mapping_iter == 1) {
382 set_pte(pte, pfn_pte(pfn, init_prot));
383 last_map_addr = (pfn << PAGE_SHIFT) + PAGE_SIZE;
385 set_pte(pte, pfn_pte(pfn, prot));
389 if (mapping_iter <= 1) {
391 * update direct mapping page count only in the first
394 update_page_count(PG_LEVEL_2M, pages_2m);
395 update_page_count(PG_LEVEL_4K, pages_4k);
397 if (mapping_iter == 1) {
399 * local global flush tlb, which will flush the previous
400 * mappings present in both small and large page TLB's.
405 * Second iteration will set the actual desired PTE attributes.
410 return last_map_addr;
416 static inline pte_t *kmap_get_fixmap_pte(unsigned long vaddr)
418 return pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr),
419 vaddr), vaddr), vaddr);
422 static void __init kmap_init(void)
424 unsigned long kmap_vstart;
427 * Cache the first kmap pte:
429 kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
430 kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
432 kmap_prot = PAGE_KERNEL;
435 #ifdef CONFIG_HIGHMEM
436 static void __init permanent_kmaps_init(pgd_t *pgd_base)
445 page_table_range_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base);
447 pgd = swapper_pg_dir + pgd_index(vaddr);
448 pud = pud_offset(pgd, vaddr);
449 pmd = pmd_offset(pud, vaddr);
450 pte = pte_offset_kernel(pmd, vaddr);
451 pkmap_page_table = pte;
454 static void __init add_one_highpage_init(struct page *page)
456 ClearPageReserved(page);
457 init_page_count(page);
462 void __init add_highpages_with_active_regions(int nid,
463 unsigned long start_pfn, unsigned long end_pfn)
465 phys_addr_t start, end;
468 for_each_free_mem_range(i, nid, &start, &end, NULL) {
469 unsigned long pfn = clamp_t(unsigned long, PFN_UP(start),
471 unsigned long e_pfn = clamp_t(unsigned long, PFN_DOWN(end),
473 for ( ; pfn < e_pfn; pfn++)
475 add_one_highpage_init(pfn_to_page(pfn));
479 static inline void permanent_kmaps_init(pgd_t *pgd_base)
482 #endif /* CONFIG_HIGHMEM */
484 pgd_t *swapper_pg_dir;
487 * Build a proper pagetable for the kernel mappings. Up until this
488 * point, we've been running on some set of pagetables constructed by
491 * If we're booting on native hardware, this will be a pagetable
492 * constructed in arch/x86/kernel/head_32.S. The root of the
493 * pagetable will be swapper_pg_dir.
495 * If we're booting paravirtualized under a hypervisor, then there are
496 * more options: we may already be running PAE, and the pagetable may
497 * or may not be based in swapper_pg_dir. In any case,
498 * paravirt_pagetable_setup_start() will set up swapper_pg_dir
499 * appropriately for the rest of the initialization to work.
501 * In general, pagetable_init() assumes that the pagetable may already
502 * be partially populated, and so it avoids stomping on any existing
505 void __init early_ioremap_page_table_range_init(void)
507 pgd_t *pgd_base = swapper_pg_dir;
508 unsigned long vaddr, end;
511 * Fixed mappings, only the page table structure has to be
512 * created - mappings will be set by set_fixmap():
514 vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
515 end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK;
516 page_table_range_init(vaddr, end, pgd_base);
517 early_ioremap_reset();
520 static void __init pagetable_init(void)
522 pgd_t *pgd_base = (pgd_t *)xen_start_info->pt_base;
524 permanent_kmaps_init(pgd_base);
527 pteval_t __supported_pte_mask __read_mostly = ~(_PAGE_NX | _PAGE_GLOBAL);
528 EXPORT_SYMBOL_GPL(__supported_pte_mask);
530 /* user-defined highmem size */
531 static unsigned int highmem_pages = -1;
534 * highmem=size forces highmem to be exactly 'size' bytes.
535 * This works even on boxes that have no highmem otherwise.
536 * This also works to reduce highmem size on bigger boxes.
538 static int __init parse_highmem(char *arg)
543 highmem_pages = memparse(arg, &arg) >> PAGE_SHIFT;
546 early_param("highmem", parse_highmem);
548 #define MSG_HIGHMEM_TOO_BIG \
549 "highmem size (%luMB) is bigger than pages available (%luMB)!\n"
551 #define MSG_LOWMEM_TOO_SMALL \
552 "highmem size (%luMB) results in <64MB lowmem, ignoring it!\n"
554 * All of RAM fits into lowmem - but if user wants highmem
555 * artificially via the highmem=x boot parameter then create
558 void __init lowmem_pfn_init(void)
560 /* max_low_pfn is 0, we already have early_res support */
561 max_low_pfn = max_pfn;
563 if (highmem_pages == -1)
565 #ifdef CONFIG_HIGHMEM
566 if (highmem_pages >= max_pfn) {
567 printk(KERN_ERR MSG_HIGHMEM_TOO_BIG,
568 pages_to_mb(highmem_pages), pages_to_mb(max_pfn));
572 if (max_low_pfn - highmem_pages < 64*1024*1024/PAGE_SIZE) {
573 printk(KERN_ERR MSG_LOWMEM_TOO_SMALL,
574 pages_to_mb(highmem_pages));
577 max_low_pfn -= highmem_pages;
581 printk(KERN_ERR "ignoring highmem size on non-highmem kernel!\n");
585 #define MSG_HIGHMEM_TOO_SMALL \
586 "only %luMB highmem pages available, ignoring highmem size of %luMB!\n"
588 #define MSG_HIGHMEM_TRIMMED \
589 "Warning: only 4GB will be used. Use a HIGHMEM64G enabled kernel!\n"
591 * We have more RAM than fits into lowmem - we try to put it into
592 * highmem, also taking the highmem=x boot parameter into account:
594 void __init highmem_pfn_init(void)
596 max_low_pfn = MAXMEM_PFN;
598 if (highmem_pages == -1)
599 highmem_pages = max_pfn - MAXMEM_PFN;
601 if (highmem_pages + MAXMEM_PFN < max_pfn)
602 max_pfn = MAXMEM_PFN + highmem_pages;
604 if (highmem_pages + MAXMEM_PFN > max_pfn) {
605 printk(KERN_WARNING MSG_HIGHMEM_TOO_SMALL,
606 pages_to_mb(max_pfn - MAXMEM_PFN),
607 pages_to_mb(highmem_pages));
610 #ifndef CONFIG_HIGHMEM
611 /* Maximum memory usable is what is directly addressable */
612 printk(KERN_WARNING "Warning only %ldMB will be used.\n", MAXMEM>>20);
613 if (max_pfn > MAX_NONPAE_PFN)
614 printk(KERN_WARNING "Use a HIGHMEM64G enabled kernel.\n");
616 printk(KERN_WARNING "Use a HIGHMEM enabled kernel.\n");
617 max_pfn = MAXMEM_PFN;
618 #else /* !CONFIG_HIGHMEM */
619 #ifndef CONFIG_HIGHMEM64G
620 if (max_pfn > MAX_NONPAE_PFN) {
621 max_pfn = MAX_NONPAE_PFN;
622 printk(KERN_WARNING MSG_HIGHMEM_TRIMMED);
624 #endif /* !CONFIG_HIGHMEM64G */
625 #endif /* !CONFIG_HIGHMEM */
629 * Determine low and high memory ranges:
631 void __init find_low_pfn_range(void)
633 /* it could update max_pfn */
635 if (max_pfn <= MAXMEM_PFN)
641 #ifndef CONFIG_NEED_MULTIPLE_NODES
642 void __init initmem_init(void)
644 #ifdef CONFIG_HIGHMEM
645 highstart_pfn = highend_pfn = max_pfn;
646 if (max_pfn > max_low_pfn)
647 highstart_pfn = max_low_pfn;
648 printk(KERN_NOTICE "%ldMB HIGHMEM available.\n",
649 pages_to_mb(highend_pfn - highstart_pfn));
650 num_physpages = highend_pfn;
651 high_memory = (void *) __va(highstart_pfn * PAGE_SIZE - 1) + 1;
653 num_physpages = max_low_pfn;
654 high_memory = (void *) __va(max_low_pfn * PAGE_SIZE - 1) + 1;
657 memblock_set_node(0, (phys_addr_t)ULLONG_MAX, 0);
658 sparse_memory_present_with_active_regions(0);
660 #ifdef CONFIG_FLATMEM
661 max_mapnr = num_physpages;
663 __vmalloc_start_set = true;
665 printk(KERN_NOTICE "%ldMB LOWMEM available.\n",
666 pages_to_mb(max_low_pfn));
668 setup_bootmem_allocator();
670 #endif /* !CONFIG_NEED_MULTIPLE_NODES */
672 void __init setup_bootmem_allocator(void)
674 printk(KERN_INFO " mapped low ram: 0 - %08lx\n",
675 max_pfn_mapped<<PAGE_SHIFT);
676 printk(KERN_INFO " low ram: 0 - %08lx\n", max_low_pfn<<PAGE_SHIFT);
681 unsigned long __init extend_init_mapping(unsigned long tables_space)
683 unsigned long start_pfn = (__pa(xen_start_info->pt_base) >> PAGE_SHIFT)
684 + xen_start_info->nr_pt_frames;
685 unsigned long start = start_pfn, va = (unsigned long)&_text;
691 /* Ensure init mappings cover kernel text/data and initial tables. */
692 while (va < PAGE_OFFSET + (start_pfn << PAGE_SHIFT) + tables_space) {
693 pgd = pgd_offset_k(va);
694 pud = pud_offset(pgd, va);
695 pmd = pmd_offset(pud, va);
696 if (pmd_none(*pmd)) {
697 unsigned long pa = start_pfn++ << PAGE_SHIFT;
699 clear_page(__va(pa));
700 make_lowmem_page_readonly(__va(pa),
701 XENFEAT_writable_page_tables);
702 xen_l2_entry_update(pmd, __pmd(pa | _KERNPG_TABLE));
704 pte = pte_offset_kernel(pmd, va);
705 if (pte_none(*pte)) {
706 pte_t new_pte = __pte(__pa(va) | _KERNPG_TABLE);
708 if (HYPERVISOR_update_va_mapping(va, new_pte, 0))
714 /* Finally, blow away any spurious initial mappings. */
716 pgd = pgd_offset_k(va);
717 pud = pud_offset(pgd, va);
718 pmd = pmd_offset(pud, va);
721 if (HYPERVISOR_update_va_mapping(va, __pte_ma(0), 0))
726 if (start_pfn > start)
727 memblock_reserve(PFN_PHYS(start), PFN_PHYS(start_pfn - start));
733 * paging_init() sets up the page tables - note that the first 8MB are
734 * already mapped by head.S.
736 * This routines also unmaps the page at virtual kernel address 0, so
737 * that we can trap those pesky NULL-reference errors in the kernel.
739 void __init paging_init(void)
748 * NOTE: at this point the bootmem allocator is fully available.
750 olpc_dt_build_devicetree();
751 sparse_memory_present_with_active_regions(MAX_NUMNODES);
757 * Test if the WP bit works in supervisor mode. It isn't supported on 386's
758 * and also on some strange 486's. All 586+'s are OK. This used to involve
759 * black magic jumps to work around some nasty CPU bugs, but fortunately the
760 * switch to using exceptions got rid of all that.
762 static void __init test_wp_bit(void)
765 "Checking if this processor honours the WP bit even in supervisor mode...");
767 /* Any page-aligned address will do, the test is non-destructive */
768 __set_fixmap(FIX_WP_TEST, __pa(&swapper_pg_dir), PAGE_READONLY);
769 boot_cpu_data.wp_works_ok = do_test_wp_bit();
770 clear_fixmap(FIX_WP_TEST);
772 if (!boot_cpu_data.wp_works_ok) {
773 printk(KERN_CONT "No.\n");
774 #ifdef CONFIG_X86_WP_WORKS_OK
776 "This kernel doesn't support CPU's with broken WP. Recompile it for a 386!");
779 printk(KERN_CONT "Ok.\n");
783 void __init mem_init(void)
785 int codesize, reservedpages, datasize, initsize;
791 #ifdef CONFIG_FLATMEM
795 * With CONFIG_DEBUG_PAGEALLOC initialization of highmem pages has to
796 * be done before free_all_bootmem(). Memblock use free low memory for
797 * temporary data (see find_range_array()) and for this purpose can use
798 * pages that was already passed to the buddy allocator, hence marked as
799 * not accessible in the page tables when compiled with
800 * CONFIG_DEBUG_PAGEALLOC. Otherwise order of initialization is not
803 set_highmem_pages_init();
805 /* this will put all low memory onto the freelists */
806 totalram_pages += free_all_bootmem();
807 /* XEN: init low-mem pages outside initial allocation. */
808 for (pfn = xen_start_info->nr_pages; pfn < max_low_pfn; pfn++) {
809 ClearPageReserved(pfn_to_page(pfn));
810 init_page_count(pfn_to_page(pfn));
814 for (tmp = 0; tmp < max_low_pfn; tmp++)
816 * Only count reserved RAM pages:
818 if (page_is_ram(tmp) && PageReserved(pfn_to_page(tmp)))
821 codesize = (unsigned long) &_etext - (unsigned long) &_text;
822 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
823 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
825 printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, "
826 "%dk reserved, %dk data, %dk init, %ldk highmem)\n",
827 nr_free_pages() << (PAGE_SHIFT-10),
828 num_physpages << (PAGE_SHIFT-10),
830 reservedpages << (PAGE_SHIFT-10),
833 totalhigh_pages << (PAGE_SHIFT-10));
835 printk(KERN_INFO "virtual kernel memory layout:\n"
836 " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
837 #ifdef CONFIG_HIGHMEM
838 " pkmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
840 " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n"
841 " lowmem : 0x%08lx - 0x%08lx (%4ld MB)\n"
842 " .init : 0x%08lx - 0x%08lx (%4ld kB)\n"
843 " .data : 0x%08lx - 0x%08lx (%4ld kB)\n"
844 " .text : 0x%08lx - 0x%08lx (%4ld kB)\n",
845 FIXADDR_START, FIXADDR_TOP,
846 (FIXADDR_TOP - FIXADDR_START) >> 10,
848 #ifdef CONFIG_HIGHMEM
849 PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE,
850 (LAST_PKMAP*PAGE_SIZE) >> 10,
853 VMALLOC_START, VMALLOC_END,
854 (VMALLOC_END - VMALLOC_START) >> 20,
856 (unsigned long)__va(0), (unsigned long)high_memory,
857 ((unsigned long)high_memory - (unsigned long)__va(0)) >> 20,
859 (unsigned long)&__init_begin, (unsigned long)&__init_end,
860 ((unsigned long)&__init_end -
861 (unsigned long)&__init_begin) >> 10,
863 (unsigned long)&_etext, (unsigned long)&_edata,
864 ((unsigned long)&_edata - (unsigned long)&_etext) >> 10,
866 (unsigned long)&_text, (unsigned long)&_etext,
867 ((unsigned long)&_etext - (unsigned long)&_text) >> 10);
870 * Check boundaries twice: Some fundamental inconsistencies can
871 * be detected at build time already.
873 #define __FIXADDR_TOP (-PAGE_SIZE)
874 #ifdef CONFIG_HIGHMEM
875 BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP*PAGE_SIZE > FIXADDR_START);
876 BUILD_BUG_ON(VMALLOC_END > PKMAP_BASE);
878 #define high_memory (-128UL << 20)
879 BUILD_BUG_ON(VMALLOC_START >= VMALLOC_END);
883 #ifdef CONFIG_HIGHMEM
884 BUG_ON(PKMAP_BASE + LAST_PKMAP*PAGE_SIZE > FIXADDR_START);
885 BUG_ON(VMALLOC_END > PKMAP_BASE);
887 BUG_ON(VMALLOC_START >= VMALLOC_END);
888 BUG_ON((unsigned long)high_memory > VMALLOC_START);
890 if (boot_cpu_data.wp_works_ok < 0)
894 #ifdef CONFIG_MEMORY_HOTPLUG
895 int arch_add_memory(int nid, u64 start, u64 size)
897 struct pglist_data *pgdata = NODE_DATA(nid);
898 struct zone *zone = pgdata->node_zones + ZONE_HIGHMEM;
899 unsigned long start_pfn = start >> PAGE_SHIFT;
900 unsigned long nr_pages = size >> PAGE_SHIFT;
902 return __add_pages(nid, zone, start_pfn, nr_pages);
907 * This function cannot be __init, since exceptions don't work in that
908 * section. Put this after the callers, so that it cannot be inlined.
910 static noinline int do_test_wp_bit(void)
915 __asm__ __volatile__(
921 :"=m" (*(char *)fix_to_virt(FIX_WP_TEST)),
930 #ifdef CONFIG_DEBUG_RODATA
931 const int rodata_test_data = 0xC3;
932 EXPORT_SYMBOL_GPL(rodata_test_data);
934 int kernel_set_to_readonly __read_mostly;
936 void set_kernel_text_rw(void)
938 unsigned long start = PFN_ALIGN(_text);
939 unsigned long size = PFN_ALIGN(_etext) - start;
941 if (!kernel_set_to_readonly)
944 pr_debug("Set kernel text: %lx - %lx for read write\n",
947 set_pages_rw(virt_to_page(start), size >> PAGE_SHIFT);
950 void set_kernel_text_ro(void)
952 unsigned long start = PFN_ALIGN(_text);
953 unsigned long size = PFN_ALIGN(_etext) - start;
955 if (!kernel_set_to_readonly)
958 pr_debug("Set kernel text: %lx - %lx for read only\n",
961 set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
964 static void mark_nxdata_nx(void)
967 * When this called, init has already been executed and released,
968 * so everything past _etext should be NX.
970 unsigned long start = PFN_ALIGN(_etext);
972 * This comes from is_kernel_text upper limit. Also HPAGE where used:
974 unsigned long size = (((unsigned long)__init_end + HPAGE_SIZE) & HPAGE_MASK) - start;
976 if (__supported_pte_mask & _PAGE_NX)
977 printk(KERN_INFO "NX-protecting the kernel data: %luk\n", size >> 10);
978 set_pages_nx(virt_to_page(start), size >> PAGE_SHIFT);
981 void mark_rodata_ro(void)
983 unsigned long start = PFN_ALIGN(_text);
984 unsigned long size = PFN_ALIGN(_etext) - start;
986 set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
987 printk(KERN_INFO "Write protecting the kernel text: %luk\n",
990 kernel_set_to_readonly = 1;
992 #ifdef CONFIG_CPA_DEBUG
993 printk(KERN_INFO "Testing CPA: Reverting %lx-%lx\n",
995 set_pages_rw(virt_to_page(start), size>>PAGE_SHIFT);
997 printk(KERN_INFO "Testing CPA: write protecting again\n");
998 set_pages_ro(virt_to_page(start), size>>PAGE_SHIFT);
1002 size = (unsigned long)__end_rodata - start;
1003 set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
1004 printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
1008 #ifdef CONFIG_CPA_DEBUG
1009 printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, start + size);
1010 set_pages_rw(virt_to_page(start), size >> PAGE_SHIFT);
1012 printk(KERN_INFO "Testing CPA: write protecting again\n");
1013 set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);