Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * |
| 3 | * Copyright (C) 1995 Linus Torvalds |
| 4 | * |
| 5 | * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999 |
| 6 | */ |
| 7 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Jan Beulich | 6fb1475 | 2007-05-02 19:27:10 +0200 | [diff] [blame] | 24 | #include <linux/pfn.h> |
Randy Dunlap | c9cf552 | 2006-06-27 02:53:52 -0700 | [diff] [blame] | 25 | #include <linux/poison.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/bootmem.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/proc_fs.h> |
Dave Hansen | 05039b9 | 2005-10-29 18:16:57 -0700 | [diff] [blame] | 29 | #include <linux/memory_hotplug.h> |
Adrian Bunk | 27d99f7 | 2005-11-13 16:06:51 -0800 | [diff] [blame] | 30 | #include <linux/initrd.h> |
Shaohua Li | 55b2355 | 2006-06-23 02:04:49 -0700 | [diff] [blame] | 31 | #include <linux/cpumask.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
H. Peter Anvin | f832ff1 | 2008-02-04 16:47:58 +0100 | [diff] [blame] | 33 | #include <asm/asm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <asm/processor.h> |
| 35 | #include <asm/system.h> |
| 36 | #include <asm/uaccess.h> |
| 37 | #include <asm/pgtable.h> |
| 38 | #include <asm/dma.h> |
| 39 | #include <asm/fixmap.h> |
| 40 | #include <asm/e820.h> |
| 41 | #include <asm/apic.h> |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 42 | #include <asm/bugs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <asm/tlb.h> |
| 44 | #include <asm/tlbflush.h> |
Jeremy Fitzhardinge | a5a19c6 | 2008-01-30 13:33:39 +0100 | [diff] [blame] | 45 | #include <asm/pgalloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include <asm/sections.h> |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 47 | #include <asm/paravirt.h> |
Ian Campbell | 551889a | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 48 | #include <asm/setup.h> |
Harvey Harrison | 7bfeab9 | 2008-02-12 12:12:01 -0800 | [diff] [blame] | 49 | #include <asm/cacheflush.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | unsigned int __VMALLOC_RESERVE = 128 << 20; |
| 52 | |
Thomas Gleixner | 6779429 | 2008-03-21 21:27:10 +0100 | [diff] [blame] | 53 | unsigned long max_pfn_mapped; |
Andi Kleen | 7d1116a | 2008-03-12 03:53:27 +0100 | [diff] [blame] | 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | DEFINE_PER_CPU(struct mmu_gather, mmu_gathers); |
| 56 | unsigned long highstart_pfn, highend_pfn; |
| 57 | |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 58 | static noinline int do_test_wp_bit(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
| 60 | /* |
| 61 | * Creates a middle page table and puts a pointer to it in the |
| 62 | * given global directory entry. This only returns the gd entry |
| 63 | * in non-PAE compilation mode, since the middle layer is folded. |
| 64 | */ |
| 65 | static pmd_t * __init one_md_table_init(pgd_t *pgd) |
| 66 | { |
| 67 | pud_t *pud; |
| 68 | pmd_t *pmd_table; |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 69 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | #ifdef CONFIG_X86_PAE |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 71 | if (!(pgd_val(*pgd) & _PAGE_PRESENT)) { |
| 72 | pmd_table = (pmd_t *) alloc_bootmem_low_pages(PAGE_SIZE); |
| 73 | |
Jeremy Fitzhardinge | 6944a9c | 2008-03-17 16:37:01 -0700 | [diff] [blame] | 74 | paravirt_alloc_pmd(&init_mm, __pa(pmd_table) >> PAGE_SHIFT); |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 75 | set_pgd(pgd, __pgd(__pa(pmd_table) | _PAGE_PRESENT)); |
| 76 | pud = pud_offset(pgd, 0); |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 77 | BUG_ON(pmd_table != pmd_offset(pud, 0)); |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 78 | } |
| 79 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | pud = pud_offset(pgd, 0); |
| 81 | pmd_table = pmd_offset(pud, 0); |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 82 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | return pmd_table; |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Create a page table and place a pointer to it in a middle page |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 88 | * directory entry: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | */ |
| 90 | static pte_t * __init one_page_table_init(pmd_t *pmd) |
| 91 | { |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 92 | if (!(pmd_val(*pmd) & _PAGE_PRESENT)) { |
Ingo Molnar | 509a80c | 2007-10-17 18:04:34 +0200 | [diff] [blame] | 93 | pte_t *page_table = NULL; |
| 94 | |
| 95 | #ifdef CONFIG_DEBUG_PAGEALLOC |
| 96 | page_table = (pte_t *) alloc_bootmem_pages(PAGE_SIZE); |
| 97 | #endif |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 98 | if (!page_table) { |
Ingo Molnar | 509a80c | 2007-10-17 18:04:34 +0200 | [diff] [blame] | 99 | page_table = |
| 100 | (pte_t *)alloc_bootmem_low_pages(PAGE_SIZE); |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 101 | } |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 102 | |
Jeremy Fitzhardinge | 6944a9c | 2008-03-17 16:37:01 -0700 | [diff] [blame] | 103 | paravirt_alloc_pte(&init_mm, __pa(page_table) >> PAGE_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | set_pmd(pmd, __pmd(__pa(page_table) | _PAGE_TABLE)); |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 105 | BUG_ON(page_table != pte_offset_kernel(pmd, 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | } |
Ingo Molnar | 509a80c | 2007-10-17 18:04:34 +0200 | [diff] [blame] | 107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | return pte_offset_kernel(pmd, 0); |
| 109 | } |
| 110 | |
| 111 | /* |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 112 | * This function initializes a certain range of kernel virtual memory |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | * with new bootmem page tables, everywhere page tables are missing in |
| 114 | * the given range. |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 115 | * |
| 116 | * NOTE: The pagetables are allocated contiguous on the physical space |
| 117 | * so we can cache the place of the first one and move around without |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | * checking the pgd every time. |
| 119 | */ |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 120 | static void __init |
| 121 | page_table_range_init(unsigned long start, unsigned long end, pgd_t *pgd_base) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | int pgd_idx, pmd_idx; |
| 124 | unsigned long vaddr; |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 125 | pgd_t *pgd; |
| 126 | pmd_t *pmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
| 128 | vaddr = start; |
| 129 | pgd_idx = pgd_index(vaddr); |
| 130 | pmd_idx = pmd_index(vaddr); |
| 131 | pgd = pgd_base + pgd_idx; |
| 132 | |
| 133 | for ( ; (pgd_idx < PTRS_PER_PGD) && (vaddr != end); pgd++, pgd_idx++) { |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 134 | pmd = one_md_table_init(pgd); |
| 135 | pmd = pmd + pmd_index(vaddr); |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 136 | for (; (pmd_idx < PTRS_PER_PMD) && (vaddr != end); |
| 137 | pmd++, pmd_idx++) { |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 138 | one_page_table_init(pmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
| 140 | vaddr += PMD_SIZE; |
| 141 | } |
| 142 | pmd_idx = 0; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | static inline int is_kernel_text(unsigned long addr) |
| 147 | { |
| 148 | if (addr >= PAGE_OFFSET && addr <= (unsigned long)__init_end) |
| 149 | return 1; |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | /* |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 154 | * This maps the physical memory to kernel virtual address space, a total |
| 155 | * of max_low_pfn pages, by creating page tables starting from address |
| 156 | * PAGE_OFFSET: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | */ |
| 158 | static void __init kernel_physical_mapping_init(pgd_t *pgd_base) |
| 159 | { |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 160 | int pgd_idx, pmd_idx, pte_ofs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | unsigned long pfn; |
| 162 | pgd_t *pgd; |
| 163 | pmd_t *pmd; |
| 164 | pte_t *pte; |
Andi Kleen | ce0c0e5 | 2008-05-02 11:46:49 +0200 | [diff] [blame] | 165 | unsigned pages_2m = 0, pages_4k = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
| 167 | pgd_idx = pgd_index(PAGE_OFFSET); |
| 168 | pgd = pgd_base + pgd_idx; |
| 169 | pfn = 0; |
| 170 | |
| 171 | for (; pgd_idx < PTRS_PER_PGD; pgd++, pgd_idx++) { |
| 172 | pmd = one_md_table_init(pgd); |
| 173 | if (pfn >= max_low_pfn) |
| 174 | continue; |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 175 | |
Jeremy Fitzhardinge | f3f20de | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 176 | for (pmd_idx = 0; |
| 177 | pmd_idx < PTRS_PER_PMD && pfn < max_low_pfn; |
| 178 | pmd++, pmd_idx++) { |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 179 | unsigned int addr = pfn * PAGE_SIZE + PAGE_OFFSET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 181 | /* |
| 182 | * Map with big pages if possible, otherwise |
| 183 | * create normal page tables: |
Andi Kleen | f5c24a7 | 2008-03-12 03:53:30 +0100 | [diff] [blame] | 184 | * |
| 185 | * Don't use a large page for the first 2/4MB of memory |
| 186 | * because there are often fixed size MTRRs in there |
| 187 | * and overlapping MTRRs into large pages can cause |
| 188 | * slowdowns. |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 189 | */ |
Andi Kleen | f5c24a7 | 2008-03-12 03:53:30 +0100 | [diff] [blame] | 190 | if (cpu_has_pse && !(pgd_idx == 0 && pmd_idx == 0)) { |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 191 | unsigned int addr2; |
Jeremy Fitzhardinge | f3f20de | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 192 | pgprot_t prot = PAGE_KERNEL_LARGE; |
| 193 | |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 194 | addr2 = (pfn + PTRS_PER_PTE-1) * PAGE_SIZE + |
Jeremy Fitzhardinge | f3f20de | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 195 | PAGE_OFFSET + PAGE_SIZE-1; |
| 196 | |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 197 | if (is_kernel_text(addr) || |
| 198 | is_kernel_text(addr2)) |
Jeremy Fitzhardinge | f3f20de | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 199 | prot = PAGE_KERNEL_LARGE_EXEC; |
| 200 | |
Andi Kleen | ce0c0e5 | 2008-05-02 11:46:49 +0200 | [diff] [blame] | 201 | pages_2m++; |
Jeremy Fitzhardinge | f3f20de | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 202 | set_pmd(pmd, pfn_pmd(pfn, prot)); |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 203 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | pfn += PTRS_PER_PTE; |
Thomas Gleixner | 6779429 | 2008-03-21 21:27:10 +0100 | [diff] [blame] | 205 | max_pfn_mapped = pfn; |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 206 | continue; |
| 207 | } |
| 208 | pte = one_page_table_init(pmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 210 | for (pte_ofs = 0; |
| 211 | pte_ofs < PTRS_PER_PTE && pfn < max_low_pfn; |
| 212 | pte++, pfn++, pte_ofs++, addr += PAGE_SIZE) { |
| 213 | pgprot_t prot = PAGE_KERNEL; |
Jeremy Fitzhardinge | f3f20de | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 214 | |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 215 | if (is_kernel_text(addr)) |
| 216 | prot = PAGE_KERNEL_EXEC; |
Jeremy Fitzhardinge | f3f20de | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 217 | |
Andi Kleen | ce0c0e5 | 2008-05-02 11:46:49 +0200 | [diff] [blame] | 218 | pages_4k++; |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 219 | set_pte(pte, pfn_pte(pfn, prot)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | } |
Thomas Gleixner | 6779429 | 2008-03-21 21:27:10 +0100 | [diff] [blame] | 221 | max_pfn_mapped = pfn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | } |
| 223 | } |
Andi Kleen | ce0c0e5 | 2008-05-02 11:46:49 +0200 | [diff] [blame] | 224 | update_page_count(PG_LEVEL_2M, pages_2m); |
| 225 | update_page_count(PG_LEVEL_4K, pages_4k); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Arjan van de Ven | ae531c2 | 2008-04-24 23:40:47 +0200 | [diff] [blame] | 228 | /* |
| 229 | * devmem_is_allowed() checks to see if /dev/mem access to a certain address |
| 230 | * is valid. The argument is a physical page number. |
| 231 | * |
| 232 | * |
| 233 | * On x86, access has to be given to the first megabyte of ram because that area |
| 234 | * contains bios code and data regions used by X and dosemu and similar apps. |
| 235 | * Access has to be given to non-kernel-ram areas as well, these contain the PCI |
| 236 | * mmio resources as well as potential bios/acpi data regions. |
| 237 | */ |
| 238 | int devmem_is_allowed(unsigned long pagenr) |
| 239 | { |
| 240 | if (pagenr <= 256) |
| 241 | return 1; |
| 242 | if (!page_is_ram(pagenr)) |
| 243 | return 1; |
| 244 | return 0; |
| 245 | } |
| 246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | #ifdef CONFIG_HIGHMEM |
| 248 | pte_t *kmap_pte; |
| 249 | pgprot_t kmap_prot; |
| 250 | |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 251 | static inline pte_t *kmap_get_fixmap_pte(unsigned long vaddr) |
| 252 | { |
| 253 | return pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr), |
| 254 | vaddr), vaddr), vaddr); |
| 255 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
| 257 | static void __init kmap_init(void) |
| 258 | { |
| 259 | unsigned long kmap_vstart; |
| 260 | |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 261 | /* |
| 262 | * Cache the first kmap pte: |
| 263 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN); |
| 265 | kmap_pte = kmap_get_fixmap_pte(kmap_vstart); |
| 266 | |
| 267 | kmap_prot = PAGE_KERNEL; |
| 268 | } |
| 269 | |
| 270 | static void __init permanent_kmaps_init(pgd_t *pgd_base) |
| 271 | { |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 272 | unsigned long vaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | pgd_t *pgd; |
| 274 | pud_t *pud; |
| 275 | pmd_t *pmd; |
| 276 | pte_t *pte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
| 278 | vaddr = PKMAP_BASE; |
| 279 | page_table_range_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base); |
| 280 | |
| 281 | pgd = swapper_pg_dir + pgd_index(vaddr); |
| 282 | pud = pud_offset(pgd, vaddr); |
| 283 | pmd = pmd_offset(pud, vaddr); |
| 284 | pte = pte_offset_kernel(pmd, vaddr); |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 285 | pkmap_page_table = pte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Yinghai Lu | cc9f7a0 | 2008-06-16 16:11:08 -0700 | [diff] [blame] | 288 | static void __init add_one_highpage_init(struct page *page, int pfn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | { |
Yinghai Lu | cc9f7a0 | 2008-06-16 16:11:08 -0700 | [diff] [blame] | 290 | ClearPageReserved(page); |
| 291 | init_page_count(page); |
| 292 | __free_page(page); |
| 293 | totalhigh_pages++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Yinghai Lu | b5bc6c0 | 2008-06-14 18:32:52 -0700 | [diff] [blame] | 296 | struct add_highpages_data { |
| 297 | unsigned long start_pfn; |
| 298 | unsigned long end_pfn; |
Yinghai Lu | b5bc6c0 | 2008-06-14 18:32:52 -0700 | [diff] [blame] | 299 | }; |
| 300 | |
Yinghai Lu | d52d53b | 2008-06-16 20:10:55 -0700 | [diff] [blame] | 301 | static int __init add_highpages_work_fn(unsigned long start_pfn, |
Yinghai Lu | b5bc6c0 | 2008-06-14 18:32:52 -0700 | [diff] [blame] | 302 | unsigned long end_pfn, void *datax) |
| 303 | { |
| 304 | int node_pfn; |
| 305 | struct page *page; |
| 306 | unsigned long final_start_pfn, final_end_pfn; |
| 307 | struct add_highpages_data *data; |
Yinghai Lu | b5bc6c0 | 2008-06-14 18:32:52 -0700 | [diff] [blame] | 308 | |
| 309 | data = (struct add_highpages_data *)datax; |
Yinghai Lu | b5bc6c0 | 2008-06-14 18:32:52 -0700 | [diff] [blame] | 310 | |
| 311 | final_start_pfn = max(start_pfn, data->start_pfn); |
| 312 | final_end_pfn = min(end_pfn, data->end_pfn); |
| 313 | if (final_start_pfn >= final_end_pfn) |
Yinghai Lu | d52d53b | 2008-06-16 20:10:55 -0700 | [diff] [blame] | 314 | return 0; |
Yinghai Lu | b5bc6c0 | 2008-06-14 18:32:52 -0700 | [diff] [blame] | 315 | |
| 316 | for (node_pfn = final_start_pfn; node_pfn < final_end_pfn; |
| 317 | node_pfn++) { |
| 318 | if (!pfn_valid(node_pfn)) |
| 319 | continue; |
| 320 | page = pfn_to_page(node_pfn); |
Yinghai Lu | cc9f7a0 | 2008-06-16 16:11:08 -0700 | [diff] [blame] | 321 | add_one_highpage_init(page, node_pfn); |
Yinghai Lu | b5bc6c0 | 2008-06-14 18:32:52 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Yinghai Lu | d52d53b | 2008-06-16 20:10:55 -0700 | [diff] [blame] | 324 | return 0; |
| 325 | |
Yinghai Lu | b5bc6c0 | 2008-06-14 18:32:52 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | void __init add_highpages_with_active_regions(int nid, unsigned long start_pfn, |
Yinghai Lu | cc9f7a0 | 2008-06-16 16:11:08 -0700 | [diff] [blame] | 329 | unsigned long end_pfn) |
Yinghai Lu | b5bc6c0 | 2008-06-14 18:32:52 -0700 | [diff] [blame] | 330 | { |
| 331 | struct add_highpages_data data; |
| 332 | |
| 333 | data.start_pfn = start_pfn; |
| 334 | data.end_pfn = end_pfn; |
Yinghai Lu | b5bc6c0 | 2008-06-14 18:32:52 -0700 | [diff] [blame] | 335 | |
| 336 | work_with_active_regions(nid, add_highpages_work_fn, &data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 339 | #ifndef CONFIG_NUMA |
Yinghai Lu | cc9f7a0 | 2008-06-16 16:11:08 -0700 | [diff] [blame] | 340 | static void __init set_highmem_pages_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | { |
Yinghai Lu | cc9f7a0 | 2008-06-16 16:11:08 -0700 | [diff] [blame] | 342 | add_highpages_with_active_regions(0, highstart_pfn, highend_pfn); |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 343 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | totalram_pages += totalhigh_pages; |
| 345 | } |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 346 | #endif /* !CONFIG_NUMA */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
| 348 | #else |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 349 | # define kmap_init() do { } while (0) |
| 350 | # define permanent_kmaps_init(pgd_base) do { } while (0) |
Yinghai Lu | cc9f7a0 | 2008-06-16 16:11:08 -0700 | [diff] [blame] | 351 | # define set_highmem_pages_init() do { } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | #endif /* CONFIG_HIGHMEM */ |
| 353 | |
Andi Kleen | c93c82b | 2008-01-30 13:33:50 +0100 | [diff] [blame] | 354 | pteval_t __PAGE_KERNEL = _PAGE_KERNEL; |
Alexey Dobriyan | 129f694 | 2005-06-23 00:08:33 -0700 | [diff] [blame] | 355 | EXPORT_SYMBOL(__PAGE_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 357 | pteval_t __PAGE_KERNEL_EXEC = _PAGE_KERNEL_EXEC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 359 | void __init native_pagetable_setup_start(pgd_t *base) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | { |
Ian Campbell | 551889a | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 361 | unsigned long pfn, va; |
| 362 | pgd_t *pgd; |
| 363 | pud_t *pud; |
| 364 | pmd_t *pmd; |
| 365 | pte_t *pte; |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 366 | |
| 367 | /* |
Ian Campbell | 551889a | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 368 | * Remove any mappings which extend past the end of physical |
| 369 | * memory from the boot time page table: |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 370 | */ |
Ian Campbell | 551889a | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 371 | for (pfn = max_low_pfn + 1; pfn < 1<<(32-PAGE_SHIFT); pfn++) { |
| 372 | va = PAGE_OFFSET + (pfn<<PAGE_SHIFT); |
| 373 | pgd = base + pgd_index(va); |
| 374 | if (!pgd_present(*pgd)) |
| 375 | break; |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 376 | |
Ian Campbell | 551889a | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 377 | pud = pud_offset(pgd, va); |
| 378 | pmd = pmd_offset(pud, va); |
| 379 | if (!pmd_present(*pmd)) |
| 380 | break; |
| 381 | |
| 382 | pte = pte_offset_kernel(pmd, va); |
| 383 | if (!pte_present(*pte)) |
| 384 | break; |
| 385 | |
| 386 | pte_clear(NULL, va, pte); |
| 387 | } |
Jeremy Fitzhardinge | 6944a9c | 2008-03-17 16:37:01 -0700 | [diff] [blame] | 388 | paravirt_alloc_pmd(&init_mm, __pa(base) >> PAGE_SHIFT); |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | void __init native_pagetable_setup_done(pgd_t *base) |
| 392 | { |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | /* |
| 396 | * Build a proper pagetable for the kernel mappings. Up until this |
| 397 | * point, we've been running on some set of pagetables constructed by |
| 398 | * the boot process. |
| 399 | * |
| 400 | * If we're booting on native hardware, this will be a pagetable |
Ian Campbell | 551889a | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 401 | * constructed in arch/x86/kernel/head_32.S. The root of the |
| 402 | * pagetable will be swapper_pg_dir. |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 403 | * |
| 404 | * If we're booting paravirtualized under a hypervisor, then there are |
| 405 | * more options: we may already be running PAE, and the pagetable may |
| 406 | * or may not be based in swapper_pg_dir. In any case, |
| 407 | * paravirt_pagetable_setup_start() will set up swapper_pg_dir |
| 408 | * appropriately for the rest of the initialization to work. |
| 409 | * |
| 410 | * In general, pagetable_init() assumes that the pagetable may already |
| 411 | * be partially populated, and so it avoids stomping on any existing |
| 412 | * mappings. |
| 413 | */ |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 414 | static void __init pagetable_init(void) |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 415 | { |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 416 | pgd_t *pgd_base = swapper_pg_dir; |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 417 | unsigned long vaddr, end; |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 418 | |
| 419 | paravirt_pagetable_setup_start(pgd_base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
| 421 | /* Enable PSE if available */ |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 422 | if (cpu_has_pse) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | set_in_cr4(X86_CR4_PSE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | |
| 425 | /* Enable PGE if available */ |
| 426 | if (cpu_has_pge) { |
| 427 | set_in_cr4(X86_CR4_PGE); |
| 428 | __PAGE_KERNEL |= _PAGE_GLOBAL; |
| 429 | __PAGE_KERNEL_EXEC |= _PAGE_GLOBAL; |
| 430 | } |
| 431 | |
| 432 | kernel_physical_mapping_init(pgd_base); |
| 433 | remap_numa_kva(); |
| 434 | |
| 435 | /* |
| 436 | * Fixed mappings, only the page table structure has to be |
| 437 | * created - mappings will be set by set_fixmap(): |
| 438 | */ |
Huang, Ying | beacfaa | 2008-01-30 13:33:44 +0100 | [diff] [blame] | 439 | early_ioremap_clear(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK; |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 441 | end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK; |
| 442 | page_table_range_init(vaddr, end, pgd_base); |
Huang, Ying | beacfaa | 2008-01-30 13:33:44 +0100 | [diff] [blame] | 443 | early_ioremap_reset(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | |
| 445 | permanent_kmaps_init(pgd_base); |
| 446 | |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 447 | paravirt_pagetable_setup_done(pgd_base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Rafael J. Wysocki | a6eb84b | 2008-02-01 15:28:16 +0100 | [diff] [blame] | 450 | #ifdef CONFIG_ACPI_SLEEP |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | /* |
Rafael J. Wysocki | a6eb84b | 2008-02-01 15:28:16 +0100 | [diff] [blame] | 452 | * ACPI suspend needs this for resume, because things like the intel-agp |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | * driver might have split up a kernel 4MB mapping. |
| 454 | */ |
Rafael J. Wysocki | a6eb84b | 2008-02-01 15:28:16 +0100 | [diff] [blame] | 455 | char swsusp_pg_dir[PAGE_SIZE] |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 456 | __attribute__ ((aligned(PAGE_SIZE))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
| 458 | static inline void save_pg_dir(void) |
| 459 | { |
| 460 | memcpy(swsusp_pg_dir, swapper_pg_dir, PAGE_SIZE); |
| 461 | } |
Rafael J. Wysocki | a6eb84b | 2008-02-01 15:28:16 +0100 | [diff] [blame] | 462 | #else /* !CONFIG_ACPI_SLEEP */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | static inline void save_pg_dir(void) |
| 464 | { |
| 465 | } |
Rafael J. Wysocki | a6eb84b | 2008-02-01 15:28:16 +0100 | [diff] [blame] | 466 | #endif /* !CONFIG_ACPI_SLEEP */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 468 | void zap_low_mappings(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | { |
| 470 | int i; |
| 471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | /* |
| 473 | * Zap initial low-memory mappings. |
| 474 | * |
| 475 | * Note that "pgd_clear()" doesn't do it for |
| 476 | * us, because pgd_clear() is a no-op on i386. |
| 477 | */ |
Jeremy Fitzhardinge | 68db065 | 2008-03-17 16:37:13 -0700 | [diff] [blame] | 478 | for (i = 0; i < KERNEL_PGD_BOUNDARY; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | #ifdef CONFIG_X86_PAE |
| 480 | set_pgd(swapper_pg_dir+i, __pgd(1 + __pa(empty_zero_page))); |
| 481 | #else |
| 482 | set_pgd(swapper_pg_dir+i, __pgd(0)); |
| 483 | #endif |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 484 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | flush_tlb_all(); |
| 486 | } |
| 487 | |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 488 | int nx_enabled; |
Jan Beulich | d5321ab | 2007-07-21 17:10:26 +0200 | [diff] [blame] | 489 | |
Jeremy Fitzhardinge | 6fdc05d | 2008-01-30 13:32:57 +0100 | [diff] [blame] | 490 | pteval_t __supported_pte_mask __read_mostly = ~_PAGE_NX; |
| 491 | EXPORT_SYMBOL_GPL(__supported_pte_mask); |
| 492 | |
Jan Beulich | d5321ab | 2007-07-21 17:10:26 +0200 | [diff] [blame] | 493 | #ifdef CONFIG_X86_PAE |
| 494 | |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 495 | static int disable_nx __initdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | |
| 497 | /* |
| 498 | * noexec = on|off |
| 499 | * |
| 500 | * Control non executable mappings. |
| 501 | * |
| 502 | * on Enable |
| 503 | * off Disable |
| 504 | */ |
Rusty Russell | 1a3f239 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 505 | static int __init noexec_setup(char *str) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | { |
Rusty Russell | 1a3f239 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 507 | if (!str || !strcmp(str, "on")) { |
| 508 | if (cpu_has_nx) { |
| 509 | __supported_pte_mask |= _PAGE_NX; |
| 510 | disable_nx = 0; |
| 511 | } |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 512 | } else { |
| 513 | if (!strcmp(str, "off")) { |
| 514 | disable_nx = 1; |
| 515 | __supported_pte_mask &= ~_PAGE_NX; |
| 516 | } else { |
| 517 | return -EINVAL; |
| 518 | } |
| 519 | } |
Rusty Russell | 1a3f239 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 520 | |
| 521 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | } |
Rusty Russell | 1a3f239 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 523 | early_param("noexec", noexec_setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | static void __init set_nx(void) |
| 526 | { |
| 527 | unsigned int v[4], l, h; |
| 528 | |
| 529 | if (cpu_has_pae && (cpuid_eax(0x80000000) > 0x80000001)) { |
| 530 | cpuid(0x80000001, &v[0], &v[1], &v[2], &v[3]); |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 531 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | if ((v[3] & (1 << 20)) && !disable_nx) { |
| 533 | rdmsr(MSR_EFER, l, h); |
| 534 | l |= EFER_NX; |
| 535 | wrmsr(MSR_EFER, l, h); |
| 536 | nx_enabled = 1; |
| 537 | __supported_pte_mask |= _PAGE_NX; |
| 538 | } |
| 539 | } |
| 540 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | #endif |
| 542 | |
Yinghai Lu | 90d967e | 2008-06-23 21:00:45 +0200 | [diff] [blame] | 543 | /* user-defined highmem size */ |
| 544 | static unsigned int highmem_pages = -1; |
| 545 | |
| 546 | /* |
| 547 | * highmem=size forces highmem to be exactly 'size' bytes. |
| 548 | * This works even on boxes that have no highmem otherwise. |
| 549 | * This also works to reduce highmem size on bigger boxes. |
| 550 | */ |
| 551 | static int __init parse_highmem(char *arg) |
| 552 | { |
| 553 | if (!arg) |
| 554 | return -EINVAL; |
| 555 | |
| 556 | highmem_pages = memparse(arg, &arg) >> PAGE_SHIFT; |
| 557 | return 0; |
| 558 | } |
| 559 | early_param("highmem", parse_highmem); |
| 560 | |
| 561 | /* |
| 562 | * Determine low and high memory ranges: |
| 563 | */ |
Yinghai Lu | 2ec65f8 | 2008-06-23 03:05:30 -0700 | [diff] [blame] | 564 | void __init find_low_pfn_range(void) |
Yinghai Lu | 90d967e | 2008-06-23 21:00:45 +0200 | [diff] [blame] | 565 | { |
Yinghai Lu | 2ec65f8 | 2008-06-23 03:05:30 -0700 | [diff] [blame] | 566 | /* it could update max_pfn */ |
| 567 | |
Yinghai Lu | 346cafe | 2008-06-23 03:06:14 -0700 | [diff] [blame] | 568 | /* max_low_pfn is 0, we already have early_res support */ |
Yinghai Lu | 90d967e | 2008-06-23 21:00:45 +0200 | [diff] [blame] | 569 | |
| 570 | max_low_pfn = max_pfn; |
| 571 | if (max_low_pfn > MAXMEM_PFN) { |
| 572 | if (highmem_pages == -1) |
| 573 | highmem_pages = max_pfn - MAXMEM_PFN; |
| 574 | if (highmem_pages + MAXMEM_PFN < max_pfn) |
| 575 | max_pfn = MAXMEM_PFN + highmem_pages; |
| 576 | if (highmem_pages + MAXMEM_PFN > max_pfn) { |
| 577 | printk(KERN_WARNING "only %luMB highmem pages " |
| 578 | "available, ignoring highmem size of %uMB.\n", |
| 579 | pages_to_mb(max_pfn - MAXMEM_PFN), |
| 580 | pages_to_mb(highmem_pages)); |
| 581 | highmem_pages = 0; |
| 582 | } |
| 583 | max_low_pfn = MAXMEM_PFN; |
| 584 | #ifndef CONFIG_HIGHMEM |
| 585 | /* Maximum memory usable is what is directly addressable */ |
| 586 | printk(KERN_WARNING "Warning only %ldMB will be used.\n", |
| 587 | MAXMEM>>20); |
| 588 | if (max_pfn > MAX_NONPAE_PFN) |
| 589 | printk(KERN_WARNING |
| 590 | "Use a HIGHMEM64G enabled kernel.\n"); |
| 591 | else |
| 592 | printk(KERN_WARNING "Use a HIGHMEM enabled kernel.\n"); |
| 593 | max_pfn = MAXMEM_PFN; |
| 594 | #else /* !CONFIG_HIGHMEM */ |
| 595 | #ifndef CONFIG_HIGHMEM64G |
| 596 | if (max_pfn > MAX_NONPAE_PFN) { |
| 597 | max_pfn = MAX_NONPAE_PFN; |
| 598 | printk(KERN_WARNING "Warning only 4GB will be used." |
| 599 | "Use a HIGHMEM64G enabled kernel.\n"); |
| 600 | } |
| 601 | #endif /* !CONFIG_HIGHMEM64G */ |
| 602 | #endif /* !CONFIG_HIGHMEM */ |
| 603 | } else { |
| 604 | if (highmem_pages == -1) |
| 605 | highmem_pages = 0; |
| 606 | #ifdef CONFIG_HIGHMEM |
| 607 | if (highmem_pages >= max_pfn) { |
| 608 | printk(KERN_ERR "highmem size specified (%uMB) is " |
| 609 | "bigger than pages available (%luMB)!.\n", |
| 610 | pages_to_mb(highmem_pages), |
| 611 | pages_to_mb(max_pfn)); |
| 612 | highmem_pages = 0; |
| 613 | } |
| 614 | if (highmem_pages) { |
| 615 | if (max_low_pfn - highmem_pages < |
| 616 | 64*1024*1024/PAGE_SIZE){ |
| 617 | printk(KERN_ERR "highmem size %uMB results in " |
| 618 | "smaller than 64MB lowmem, ignoring it.\n" |
| 619 | , pages_to_mb(highmem_pages)); |
| 620 | highmem_pages = 0; |
| 621 | } |
| 622 | max_low_pfn -= highmem_pages; |
| 623 | } |
| 624 | #else |
| 625 | if (highmem_pages) |
| 626 | printk(KERN_ERR "ignoring highmem size on non-highmem" |
| 627 | " kernel!\n"); |
| 628 | #endif |
| 629 | } |
Yinghai Lu | 90d967e | 2008-06-23 21:00:45 +0200 | [diff] [blame] | 630 | } |
| 631 | |
Yinghai Lu | b2ac82a | 2008-06-22 02:45:39 -0700 | [diff] [blame] | 632 | #ifndef CONFIG_NEED_MULTIPLE_NODES |
Yinghai Lu | 2ec65f8 | 2008-06-23 03:05:30 -0700 | [diff] [blame] | 633 | void __init initmem_init(unsigned long start_pfn, |
Yinghai Lu | b2ac82a | 2008-06-22 02:45:39 -0700 | [diff] [blame] | 634 | unsigned long end_pfn) |
| 635 | { |
Yinghai Lu | b2ac82a | 2008-06-22 02:45:39 -0700 | [diff] [blame] | 636 | #ifdef CONFIG_HIGHMEM |
| 637 | highstart_pfn = highend_pfn = max_pfn; |
| 638 | if (max_pfn > max_low_pfn) |
| 639 | highstart_pfn = max_low_pfn; |
| 640 | memory_present(0, 0, highend_pfn); |
| 641 | printk(KERN_NOTICE "%ldMB HIGHMEM available.\n", |
| 642 | pages_to_mb(highend_pfn - highstart_pfn)); |
| 643 | num_physpages = highend_pfn; |
| 644 | high_memory = (void *) __va(highstart_pfn * PAGE_SIZE - 1) + 1; |
| 645 | #else |
| 646 | memory_present(0, 0, max_low_pfn); |
| 647 | num_physpages = max_low_pfn; |
| 648 | high_memory = (void *) __va(max_low_pfn * PAGE_SIZE - 1) + 1; |
| 649 | #endif |
| 650 | #ifdef CONFIG_FLATMEM |
| 651 | max_mapnr = num_physpages; |
| 652 | #endif |
| 653 | printk(KERN_NOTICE "%ldMB LOWMEM available.\n", |
| 654 | pages_to_mb(max_low_pfn)); |
| 655 | |
| 656 | setup_bootmem_allocator(); |
Yinghai Lu | b2ac82a | 2008-06-22 02:45:39 -0700 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | void __init zone_sizes_init(void) |
| 660 | { |
| 661 | unsigned long max_zone_pfns[MAX_NR_ZONES]; |
| 662 | memset(max_zone_pfns, 0, sizeof(max_zone_pfns)); |
| 663 | max_zone_pfns[ZONE_DMA] = |
| 664 | virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT; |
| 665 | max_zone_pfns[ZONE_NORMAL] = max_low_pfn; |
| 666 | remove_all_active_ranges(); |
| 667 | #ifdef CONFIG_HIGHMEM |
| 668 | max_zone_pfns[ZONE_HIGHMEM] = highend_pfn; |
| 669 | e820_register_active_regions(0, 0, highend_pfn); |
| 670 | #else |
| 671 | e820_register_active_regions(0, 0, max_low_pfn); |
| 672 | #endif |
| 673 | |
| 674 | free_area_init_nodes(max_zone_pfns); |
| 675 | } |
| 676 | #endif /* !CONFIG_NEED_MULTIPLE_NODES */ |
| 677 | |
Yinghai Lu | b2ac82a | 2008-06-22 02:45:39 -0700 | [diff] [blame] | 678 | void __init setup_bootmem_allocator(void) |
| 679 | { |
| 680 | int i; |
| 681 | unsigned long bootmap_size, bootmap; |
| 682 | /* |
| 683 | * Initialize the boot-time allocator (with low memory only): |
| 684 | */ |
| 685 | bootmap_size = bootmem_bootmap_pages(max_low_pfn)<<PAGE_SHIFT; |
| 686 | bootmap = find_e820_area(min_low_pfn<<PAGE_SHIFT, |
| 687 | max_pfn_mapped<<PAGE_SHIFT, bootmap_size, |
| 688 | PAGE_SIZE); |
| 689 | if (bootmap == -1L) |
| 690 | panic("Cannot find bootmem map of size %ld\n", bootmap_size); |
| 691 | reserve_early(bootmap, bootmap + bootmap_size, "BOOTMAP"); |
Yinghai Lu | 225c37d | 2008-06-22 02:46:58 -0700 | [diff] [blame] | 692 | |
Yinghai Lu | 346cafe | 2008-06-23 03:06:14 -0700 | [diff] [blame] | 693 | /* don't touch min_low_pfn */ |
| 694 | bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap >> PAGE_SHIFT, |
| 695 | min_low_pfn, max_low_pfn); |
Yinghai Lu | b2ac82a | 2008-06-22 02:45:39 -0700 | [diff] [blame] | 696 | printk(KERN_INFO " mapped low ram: 0 - %08lx\n", |
| 697 | max_pfn_mapped<<PAGE_SHIFT); |
| 698 | printk(KERN_INFO " low ram: %08lx - %08lx\n", |
| 699 | min_low_pfn<<PAGE_SHIFT, max_low_pfn<<PAGE_SHIFT); |
| 700 | printk(KERN_INFO " bootmap %08lx - %08lx\n", |
| 701 | bootmap, bootmap + bootmap_size); |
| 702 | for_each_online_node(i) |
| 703 | free_bootmem_with_active_regions(i, max_low_pfn); |
| 704 | early_res_to_bootmem(0, max_low_pfn<<PAGE_SHIFT); |
| 705 | |
| 706 | } |
| 707 | |
| 708 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | /* |
| 710 | * paging_init() sets up the page tables - note that the first 8MB are |
| 711 | * already mapped by head.S. |
| 712 | * |
| 713 | * This routines also unmaps the page at virtual kernel address 0, so |
| 714 | * that we can trap those pesky NULL-reference errors in the kernel. |
| 715 | */ |
| 716 | void __init paging_init(void) |
| 717 | { |
| 718 | #ifdef CONFIG_X86_PAE |
| 719 | set_nx(); |
| 720 | if (nx_enabled) |
Ingo Molnar | d7d119d | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 721 | printk(KERN_INFO "NX (Execute Disable) protection: active\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | pagetable_init(); |
| 724 | |
| 725 | load_cr3(swapper_pg_dir); |
| 726 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | __flush_tlb_all(); |
| 728 | |
| 729 | kmap_init(); |
| 730 | } |
| 731 | |
| 732 | /* |
| 733 | * Test if the WP bit works in supervisor mode. It isn't supported on 386's |
Dmitri Vorobiev | f7f17a6 | 2008-04-21 00:47:55 +0400 | [diff] [blame] | 734 | * and also on some strange 486's. All 586+'s are OK. This used to involve |
| 735 | * black magic jumps to work around some nasty CPU bugs, but fortunately the |
| 736 | * switch to using exceptions got rid of all that. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | static void __init test_wp_bit(void) |
| 739 | { |
Ingo Molnar | d7d119d | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 740 | printk(KERN_INFO |
| 741 | "Checking if this processor honours the WP bit even in supervisor mode..."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | |
| 743 | /* Any page-aligned address will do, the test is non-destructive */ |
| 744 | __set_fixmap(FIX_WP_TEST, __pa(&swapper_pg_dir), PAGE_READONLY); |
| 745 | boot_cpu_data.wp_works_ok = do_test_wp_bit(); |
| 746 | clear_fixmap(FIX_WP_TEST); |
| 747 | |
| 748 | if (!boot_cpu_data.wp_works_ok) { |
Ingo Molnar | d7d119d | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 749 | printk(KERN_CONT "No.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | #ifdef CONFIG_X86_WP_WORKS_OK |
Ingo Molnar | d7d119d | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 751 | panic( |
| 752 | "This kernel doesn't support CPU's with broken WP. Recompile it for a 386!"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | #endif |
| 754 | } else { |
Ingo Molnar | d7d119d | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 755 | printk(KERN_CONT "Ok.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | } |
| 757 | } |
| 758 | |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 759 | static struct kcore_list kcore_mem, kcore_vmalloc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | |
| 761 | void __init mem_init(void) |
| 762 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | int codesize, reservedpages, datasize, initsize; |
Yinghai Lu | cc9f7a0 | 2008-06-16 16:11:08 -0700 | [diff] [blame] | 764 | int tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | |
Andy Whitcroft | 05b79bd | 2005-06-23 00:07:57 -0700 | [diff] [blame] | 766 | #ifdef CONFIG_FLATMEM |
Eric Sesterhenn | 8d8f3cb | 2006-10-03 23:34:58 +0200 | [diff] [blame] | 767 | BUG_ON(!mem_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | /* this will put all low memory onto the freelists */ |
| 770 | totalram_pages += free_all_bootmem(); |
| 771 | |
| 772 | reservedpages = 0; |
| 773 | for (tmp = 0; tmp < max_low_pfn; tmp++) |
| 774 | /* |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 775 | * Only count reserved RAM pages: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | */ |
| 777 | if (page_is_ram(tmp) && PageReserved(pfn_to_page(tmp))) |
| 778 | reservedpages++; |
| 779 | |
Yinghai Lu | cc9f7a0 | 2008-06-16 16:11:08 -0700 | [diff] [blame] | 780 | set_highmem_pages_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | |
| 782 | codesize = (unsigned long) &_etext - (unsigned long) &_text; |
| 783 | datasize = (unsigned long) &_edata - (unsigned long) &_etext; |
| 784 | initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin; |
| 785 | |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 786 | kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT); |
| 787 | kclist_add(&kcore_vmalloc, (void *)VMALLOC_START, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | VMALLOC_END-VMALLOC_START); |
| 789 | |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 790 | printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, " |
| 791 | "%dk reserved, %dk data, %dk init, %ldk highmem)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | (unsigned long) nr_free_pages() << (PAGE_SHIFT-10), |
| 793 | num_physpages << (PAGE_SHIFT-10), |
| 794 | codesize >> 10, |
| 795 | reservedpages << (PAGE_SHIFT-10), |
| 796 | datasize >> 10, |
| 797 | initsize >> 10, |
| 798 | (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10)) |
| 799 | ); |
| 800 | |
Ingo Molnar | d7d119d | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 801 | printk(KERN_INFO "virtual kernel memory layout:\n" |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 802 | " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n" |
Jeremy Fitzhardinge | 052e799 | 2006-09-25 23:32:25 -0700 | [diff] [blame] | 803 | #ifdef CONFIG_HIGHMEM |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 804 | " pkmap : 0x%08lx - 0x%08lx (%4ld kB)\n" |
Jeremy Fitzhardinge | 052e799 | 2006-09-25 23:32:25 -0700 | [diff] [blame] | 805 | #endif |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 806 | " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n" |
| 807 | " lowmem : 0x%08lx - 0x%08lx (%4ld MB)\n" |
| 808 | " .init : 0x%08lx - 0x%08lx (%4ld kB)\n" |
| 809 | " .data : 0x%08lx - 0x%08lx (%4ld kB)\n" |
| 810 | " .text : 0x%08lx - 0x%08lx (%4ld kB)\n", |
| 811 | FIXADDR_START, FIXADDR_TOP, |
| 812 | (FIXADDR_TOP - FIXADDR_START) >> 10, |
Jeremy Fitzhardinge | 052e799 | 2006-09-25 23:32:25 -0700 | [diff] [blame] | 813 | |
| 814 | #ifdef CONFIG_HIGHMEM |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 815 | PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE, |
| 816 | (LAST_PKMAP*PAGE_SIZE) >> 10, |
Jeremy Fitzhardinge | 052e799 | 2006-09-25 23:32:25 -0700 | [diff] [blame] | 817 | #endif |
| 818 | |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 819 | VMALLOC_START, VMALLOC_END, |
| 820 | (VMALLOC_END - VMALLOC_START) >> 20, |
Jeremy Fitzhardinge | 052e799 | 2006-09-25 23:32:25 -0700 | [diff] [blame] | 821 | |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 822 | (unsigned long)__va(0), (unsigned long)high_memory, |
| 823 | ((unsigned long)high_memory - (unsigned long)__va(0)) >> 20, |
Jeremy Fitzhardinge | 052e799 | 2006-09-25 23:32:25 -0700 | [diff] [blame] | 824 | |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 825 | (unsigned long)&__init_begin, (unsigned long)&__init_end, |
| 826 | ((unsigned long)&__init_end - |
| 827 | (unsigned long)&__init_begin) >> 10, |
Jeremy Fitzhardinge | 052e799 | 2006-09-25 23:32:25 -0700 | [diff] [blame] | 828 | |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 829 | (unsigned long)&_etext, (unsigned long)&_edata, |
| 830 | ((unsigned long)&_edata - (unsigned long)&_etext) >> 10, |
Jeremy Fitzhardinge | 052e799 | 2006-09-25 23:32:25 -0700 | [diff] [blame] | 831 | |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 832 | (unsigned long)&_text, (unsigned long)&_etext, |
| 833 | ((unsigned long)&_etext - (unsigned long)&_text) >> 10); |
Jeremy Fitzhardinge | 052e799 | 2006-09-25 23:32:25 -0700 | [diff] [blame] | 834 | |
| 835 | #ifdef CONFIG_HIGHMEM |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 836 | BUG_ON(PKMAP_BASE + LAST_PKMAP*PAGE_SIZE > FIXADDR_START); |
| 837 | BUG_ON(VMALLOC_END > PKMAP_BASE); |
Jeremy Fitzhardinge | 052e799 | 2006-09-25 23:32:25 -0700 | [diff] [blame] | 838 | #endif |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 839 | BUG_ON(VMALLOC_START > VMALLOC_END); |
| 840 | BUG_ON((unsigned long)high_memory > VMALLOC_START); |
Jeremy Fitzhardinge | 052e799 | 2006-09-25 23:32:25 -0700 | [diff] [blame] | 841 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | if (boot_cpu_data.wp_works_ok < 0) |
| 843 | test_wp_bit(); |
| 844 | |
Thomas Gleixner | 76ebd05 | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 845 | cpa_init(); |
Hugh Dickins | 61165d7 | 2008-05-13 14:26:57 +0100 | [diff] [blame] | 846 | save_pg_dir(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | zap_low_mappings(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | } |
| 849 | |
KAMEZAWA Hiroyuki | ad8f579 | 2006-05-20 15:00:03 -0700 | [diff] [blame] | 850 | #ifdef CONFIG_MEMORY_HOTPLUG |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 851 | int arch_add_memory(int nid, u64 start, u64 size) |
Dave Hansen | 05039b9 | 2005-10-29 18:16:57 -0700 | [diff] [blame] | 852 | { |
Yasunori Goto | 7c7e942 | 2006-12-22 01:11:13 -0800 | [diff] [blame] | 853 | struct pglist_data *pgdata = NODE_DATA(nid); |
Christoph Lameter | 776ed98 | 2006-09-25 23:31:09 -0700 | [diff] [blame] | 854 | struct zone *zone = pgdata->node_zones + ZONE_HIGHMEM; |
Dave Hansen | 05039b9 | 2005-10-29 18:16:57 -0700 | [diff] [blame] | 855 | unsigned long start_pfn = start >> PAGE_SHIFT; |
| 856 | unsigned long nr_pages = size >> PAGE_SHIFT; |
| 857 | |
| 858 | return __add_pages(zone, start_pfn, nr_pages); |
| 859 | } |
Andi Kleen | 9d99aaa | 2006-04-07 19:49:15 +0200 | [diff] [blame] | 860 | #endif |
Dave Hansen | 05039b9 | 2005-10-29 18:16:57 -0700 | [diff] [blame] | 861 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | /* |
| 863 | * This function cannot be __init, since exceptions don't work in that |
| 864 | * section. Put this after the callers, so that it cannot be inlined. |
| 865 | */ |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 866 | static noinline int do_test_wp_bit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | { |
| 868 | char tmp_reg; |
| 869 | int flag; |
| 870 | |
| 871 | __asm__ __volatile__( |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 872 | " movb %0, %1 \n" |
| 873 | "1: movb %1, %0 \n" |
| 874 | " xorl %2, %2 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | "2: \n" |
H. Peter Anvin | f832ff1 | 2008-02-04 16:47:58 +0100 | [diff] [blame] | 876 | _ASM_EXTABLE(1b,2b) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | :"=m" (*(char *)fix_to_virt(FIX_WP_TEST)), |
| 878 | "=q" (tmp_reg), |
| 879 | "=r" (flag) |
| 880 | :"2" (1) |
| 881 | :"memory"); |
Ingo Molnar | 8550eb9 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 882 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | return flag; |
| 884 | } |
| 885 | |
Arjan van de Ven | 63aaf30 | 2006-01-06 00:12:02 -0800 | [diff] [blame] | 886 | #ifdef CONFIG_DEBUG_RODATA |
Arjan van de Ven | edeed30 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 887 | const int rodata_test_data = 0xC3; |
| 888 | EXPORT_SYMBOL_GPL(rodata_test_data); |
Arjan van de Ven | 63aaf30 | 2006-01-06 00:12:02 -0800 | [diff] [blame] | 889 | |
Arjan van de Ven | 63aaf30 | 2006-01-06 00:12:02 -0800 | [diff] [blame] | 890 | void mark_rodata_ro(void) |
| 891 | { |
Jan Beulich | 6fb1475 | 2007-05-02 19:27:10 +0200 | [diff] [blame] | 892 | unsigned long start = PFN_ALIGN(_text); |
| 893 | unsigned long size = PFN_ALIGN(_etext) - start; |
Arjan van de Ven | 63aaf30 | 2006-01-06 00:12:02 -0800 | [diff] [blame] | 894 | |
Mathieu Desnoyers | 4e4eee0 | 2008-02-02 15:42:20 -0500 | [diff] [blame] | 895 | set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT); |
| 896 | printk(KERN_INFO "Write protecting the kernel text: %luk\n", |
| 897 | size >> 10); |
Andi Kleen | 0c42f39 | 2008-01-30 13:33:42 +0100 | [diff] [blame] | 898 | |
| 899 | #ifdef CONFIG_CPA_DEBUG |
Mathieu Desnoyers | 4e4eee0 | 2008-02-02 15:42:20 -0500 | [diff] [blame] | 900 | printk(KERN_INFO "Testing CPA: Reverting %lx-%lx\n", |
| 901 | start, start+size); |
| 902 | set_pages_rw(virt_to_page(start), size>>PAGE_SHIFT); |
Andi Kleen | 0c42f39 | 2008-01-30 13:33:42 +0100 | [diff] [blame] | 903 | |
Mathieu Desnoyers | 4e4eee0 | 2008-02-02 15:42:20 -0500 | [diff] [blame] | 904 | printk(KERN_INFO "Testing CPA: write protecting again\n"); |
| 905 | set_pages_ro(virt_to_page(start), size>>PAGE_SHIFT); |
Linus Torvalds | 602033e | 2007-07-26 12:07:21 -0700 | [diff] [blame] | 906 | #endif |
Jan Beulich | 6fb1475 | 2007-05-02 19:27:10 +0200 | [diff] [blame] | 907 | start += size; |
| 908 | size = (unsigned long)__end_rodata - start; |
Arjan van de Ven | 6d238cc | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 909 | set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT); |
Ingo Molnar | d7d119d | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 910 | printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n", |
| 911 | size >> 10); |
Arjan van de Ven | edeed30 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 912 | rodata_test(); |
Arjan van de Ven | 63aaf30 | 2006-01-06 00:12:02 -0800 | [diff] [blame] | 913 | |
Andi Kleen | 0c42f39 | 2008-01-30 13:33:42 +0100 | [diff] [blame] | 914 | #ifdef CONFIG_CPA_DEBUG |
Ingo Molnar | d7d119d | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 915 | printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, start + size); |
Arjan van de Ven | 6d238cc | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 916 | set_pages_rw(virt_to_page(start), size >> PAGE_SHIFT); |
Andi Kleen | 0c42f39 | 2008-01-30 13:33:42 +0100 | [diff] [blame] | 917 | |
Ingo Molnar | d7d119d | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 918 | printk(KERN_INFO "Testing CPA: write protecting again\n"); |
Arjan van de Ven | 6d238cc | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 919 | set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT); |
Andi Kleen | 0c42f39 | 2008-01-30 13:33:42 +0100 | [diff] [blame] | 920 | #endif |
Arjan van de Ven | 63aaf30 | 2006-01-06 00:12:02 -0800 | [diff] [blame] | 921 | } |
| 922 | #endif |
| 923 | |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 924 | void free_init_pages(char *what, unsigned long begin, unsigned long end) |
| 925 | { |
Ingo Molnar | ee01f11 | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 926 | #ifdef CONFIG_DEBUG_PAGEALLOC |
| 927 | /* |
| 928 | * If debugging page accesses then do not free this memory but |
| 929 | * mark them not present - any buggy init-section access will |
| 930 | * create a kernel page fault: |
| 931 | */ |
| 932 | printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n", |
| 933 | begin, PAGE_ALIGN(end)); |
| 934 | set_memory_np(begin, (end - begin) >> PAGE_SHIFT); |
| 935 | #else |
Ingo Molnar | 86f0398 | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 936 | unsigned long addr; |
| 937 | |
Arjan van de Ven | 3c1df68 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 938 | /* |
| 939 | * We just marked the kernel text read only above, now that |
| 940 | * we are going to free part of that, we need to make that |
| 941 | * writeable first. |
| 942 | */ |
| 943 | set_memory_rw(begin, (end - begin) >> PAGE_SHIFT); |
| 944 | |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 945 | for (addr = begin; addr < end; addr += PAGE_SIZE) { |
Linus Torvalds | e3ebadd | 2007-05-07 08:44:24 -0700 | [diff] [blame] | 946 | ClearPageReserved(virt_to_page(addr)); |
| 947 | init_page_count(virt_to_page(addr)); |
| 948 | memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE); |
| 949 | free_page(addr); |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 950 | totalram_pages++; |
| 951 | } |
Jan Beulich | 6fb1475 | 2007-05-02 19:27:10 +0200 | [diff] [blame] | 952 | printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10); |
Ingo Molnar | ee01f11 | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 953 | #endif |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | void free_initmem(void) |
| 957 | { |
| 958 | free_init_pages("unused kernel memory", |
Linus Torvalds | e3ebadd | 2007-05-07 08:44:24 -0700 | [diff] [blame] | 959 | (unsigned long)(&__init_begin), |
| 960 | (unsigned long)(&__init_end)); |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 961 | } |
Arjan van de Ven | 63aaf30 | 2006-01-06 00:12:02 -0800 | [diff] [blame] | 962 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | #ifdef CONFIG_BLK_DEV_INITRD |
| 964 | void free_initrd_mem(unsigned long start, unsigned long end) |
| 965 | { |
Linus Torvalds | e3ebadd | 2007-05-07 08:44:24 -0700 | [diff] [blame] | 966 | free_init_pages("initrd memory", start, end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | } |
| 968 | #endif |
Yinghai Lu | d2dbf34 | 2008-06-13 02:00:56 -0700 | [diff] [blame] | 969 | |
| 970 | int __init reserve_bootmem_generic(unsigned long phys, unsigned long len, |
| 971 | int flags) |
| 972 | { |
| 973 | return reserve_bootmem(phys, len, flags); |
| 974 | } |