Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/i386/mm/init.c |
| 3 | * |
| 4 | * Copyright (C) 1995 Linus Torvalds |
| 5 | * |
| 6 | * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999 |
| 7 | */ |
| 8 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/module.h> |
| 10 | #include <linux/signal.h> |
| 11 | #include <linux/sched.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/errno.h> |
| 14 | #include <linux/string.h> |
| 15 | #include <linux/types.h> |
| 16 | #include <linux/ptrace.h> |
| 17 | #include <linux/mman.h> |
| 18 | #include <linux/mm.h> |
| 19 | #include <linux/hugetlb.h> |
| 20 | #include <linux/swap.h> |
| 21 | #include <linux/smp.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/highmem.h> |
| 24 | #include <linux/pagemap.h> |
Jan Beulich | 6fb1475 | 2007-05-02 19:27:10 +0200 | [diff] [blame] | 25 | #include <linux/pfn.h> |
Randy Dunlap | c9cf552 | 2006-06-27 02:53:52 -0700 | [diff] [blame] | 26 | #include <linux/poison.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/bootmem.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/proc_fs.h> |
| 30 | #include <linux/efi.h> |
Dave Hansen | 05039b9 | 2005-10-29 18:16:57 -0700 | [diff] [blame] | 31 | #include <linux/memory_hotplug.h> |
Adrian Bunk | 27d99f7 | 2005-11-13 16:06:51 -0800 | [diff] [blame] | 32 | #include <linux/initrd.h> |
Shaohua Li | 55b2355 | 2006-06-23 02:04:49 -0700 | [diff] [blame] | 33 | #include <linux/cpumask.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | #include <asm/processor.h> |
| 36 | #include <asm/system.h> |
| 37 | #include <asm/uaccess.h> |
| 38 | #include <asm/pgtable.h> |
| 39 | #include <asm/dma.h> |
| 40 | #include <asm/fixmap.h> |
| 41 | #include <asm/e820.h> |
| 42 | #include <asm/apic.h> |
| 43 | #include <asm/tlb.h> |
| 44 | #include <asm/tlbflush.h> |
| 45 | #include <asm/sections.h> |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 46 | #include <asm/paravirt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
| 48 | unsigned int __VMALLOC_RESERVE = 128 << 20; |
| 49 | |
| 50 | DEFINE_PER_CPU(struct mmu_gather, mmu_gathers); |
| 51 | unsigned long highstart_pfn, highend_pfn; |
| 52 | |
| 53 | static int noinline do_test_wp_bit(void); |
| 54 | |
| 55 | /* |
| 56 | * Creates a middle page table and puts a pointer to it in the |
| 57 | * given global directory entry. This only returns the gd entry |
| 58 | * in non-PAE compilation mode, since the middle layer is folded. |
| 59 | */ |
| 60 | static pmd_t * __init one_md_table_init(pgd_t *pgd) |
| 61 | { |
| 62 | pud_t *pud; |
| 63 | pmd_t *pmd_table; |
| 64 | |
| 65 | #ifdef CONFIG_X86_PAE |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 66 | if (!(pgd_val(*pgd) & _PAGE_PRESENT)) { |
| 67 | pmd_table = (pmd_t *) alloc_bootmem_low_pages(PAGE_SIZE); |
| 68 | |
| 69 | paravirt_alloc_pd(__pa(pmd_table) >> PAGE_SHIFT); |
| 70 | set_pgd(pgd, __pgd(__pa(pmd_table) | _PAGE_PRESENT)); |
| 71 | pud = pud_offset(pgd, 0); |
| 72 | if (pmd_table != pmd_offset(pud, 0)) |
| 73 | BUG(); |
| 74 | } |
| 75 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | pud = pud_offset(pgd, 0); |
| 77 | pmd_table = pmd_offset(pud, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | return pmd_table; |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * Create a page table and place a pointer to it in a middle page |
| 83 | * directory entry. |
| 84 | */ |
| 85 | static pte_t * __init one_page_table_init(pmd_t *pmd) |
| 86 | { |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 87 | if (!(pmd_val(*pmd) & _PAGE_PRESENT)) { |
Ingo Molnar | 509a80c | 2007-10-17 18:04:34 +0200 | [diff] [blame] | 88 | pte_t *page_table = NULL; |
| 89 | |
| 90 | #ifdef CONFIG_DEBUG_PAGEALLOC |
| 91 | page_table = (pte_t *) alloc_bootmem_pages(PAGE_SIZE); |
| 92 | #endif |
| 93 | if (!page_table) |
| 94 | page_table = |
| 95 | (pte_t *)alloc_bootmem_low_pages(PAGE_SIZE); |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 96 | |
Jeremy Fitzhardinge | fdb4c33 | 2007-07-17 18:37:03 -0700 | [diff] [blame] | 97 | paravirt_alloc_pt(&init_mm, __pa(page_table) >> PAGE_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | set_pmd(pmd, __pmd(__pa(page_table) | _PAGE_TABLE)); |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 99 | BUG_ON(page_table != pte_offset_kernel(pmd, 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
Ingo Molnar | 509a80c | 2007-10-17 18:04:34 +0200 | [diff] [blame] | 101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | return pte_offset_kernel(pmd, 0); |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | * This function initializes a certain range of kernel virtual memory |
| 107 | * with new bootmem page tables, everywhere page tables are missing in |
| 108 | * the given range. |
| 109 | */ |
| 110 | |
| 111 | /* |
| 112 | * NOTE: The pagetables are allocated contiguous on the physical space |
| 113 | * so we can cache the place of the first one and move around without |
| 114 | * checking the pgd every time. |
| 115 | */ |
| 116 | static void __init page_table_range_init (unsigned long start, unsigned long end, pgd_t *pgd_base) |
| 117 | { |
| 118 | pgd_t *pgd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | pmd_t *pmd; |
| 120 | int pgd_idx, pmd_idx; |
| 121 | unsigned long vaddr; |
| 122 | |
| 123 | vaddr = start; |
| 124 | pgd_idx = pgd_index(vaddr); |
| 125 | pmd_idx = pmd_index(vaddr); |
| 126 | pgd = pgd_base + pgd_idx; |
| 127 | |
| 128 | for ( ; (pgd_idx < PTRS_PER_PGD) && (vaddr != end); pgd++, pgd_idx++) { |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 129 | pmd = one_md_table_init(pgd); |
| 130 | pmd = pmd + pmd_index(vaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | for (; (pmd_idx < PTRS_PER_PMD) && (vaddr != end); pmd++, pmd_idx++) { |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 132 | one_page_table_init(pmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
| 134 | vaddr += PMD_SIZE; |
| 135 | } |
| 136 | pmd_idx = 0; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | static inline int is_kernel_text(unsigned long addr) |
| 141 | { |
| 142 | if (addr >= PAGE_OFFSET && addr <= (unsigned long)__init_end) |
| 143 | return 1; |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * This maps the physical memory to kernel virtual address space, a total |
| 149 | * of max_low_pfn pages, by creating page tables starting from address |
| 150 | * PAGE_OFFSET. |
| 151 | */ |
| 152 | static void __init kernel_physical_mapping_init(pgd_t *pgd_base) |
| 153 | { |
| 154 | unsigned long pfn; |
| 155 | pgd_t *pgd; |
| 156 | pmd_t *pmd; |
| 157 | pte_t *pte; |
| 158 | int pgd_idx, pmd_idx, pte_ofs; |
| 159 | |
| 160 | pgd_idx = pgd_index(PAGE_OFFSET); |
| 161 | pgd = pgd_base + pgd_idx; |
| 162 | pfn = 0; |
| 163 | |
| 164 | for (; pgd_idx < PTRS_PER_PGD; pgd++, pgd_idx++) { |
| 165 | pmd = one_md_table_init(pgd); |
| 166 | if (pfn >= max_low_pfn) |
| 167 | continue; |
| 168 | for (pmd_idx = 0; pmd_idx < PTRS_PER_PMD && pfn < max_low_pfn; pmd++, pmd_idx++) { |
| 169 | unsigned int address = pfn * PAGE_SIZE + PAGE_OFFSET; |
| 170 | |
| 171 | /* Map with big pages if possible, otherwise create normal page tables. */ |
| 172 | if (cpu_has_pse) { |
| 173 | unsigned int address2 = (pfn + PTRS_PER_PTE - 1) * PAGE_SIZE + PAGE_OFFSET + PAGE_SIZE-1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | if (is_kernel_text(address) || is_kernel_text(address2)) |
| 175 | set_pmd(pmd, pfn_pmd(pfn, PAGE_KERNEL_LARGE_EXEC)); |
| 176 | else |
| 177 | set_pmd(pmd, pfn_pmd(pfn, PAGE_KERNEL_LARGE)); |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 178 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | pfn += PTRS_PER_PTE; |
| 180 | } else { |
| 181 | pte = one_page_table_init(pmd); |
| 182 | |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 183 | for (pte_ofs = 0; |
| 184 | pte_ofs < PTRS_PER_PTE && pfn < max_low_pfn; |
| 185 | pte++, pfn++, pte_ofs++, address += PAGE_SIZE) { |
| 186 | if (is_kernel_text(address)) |
| 187 | set_pte(pte, pfn_pte(pfn, PAGE_KERNEL_EXEC)); |
| 188 | else |
| 189 | set_pte(pte, pfn_pte(pfn, PAGE_KERNEL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | static inline int page_kills_ppro(unsigned long pagenr) |
| 197 | { |
| 198 | if (pagenr >= 0x70000 && pagenr <= 0x7003F) |
| 199 | return 1; |
| 200 | return 0; |
| 201 | } |
| 202 | |
Dave Hansen | 5b505b9 | 2005-06-23 00:07:41 -0700 | [diff] [blame] | 203 | int page_is_ram(unsigned long pagenr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | { |
| 205 | int i; |
| 206 | unsigned long addr, end; |
| 207 | |
| 208 | if (efi_enabled) { |
| 209 | efi_memory_desc_t *md; |
Matt Tolentino | 7ae65fd | 2005-09-03 15:56:27 -0700 | [diff] [blame] | 210 | void *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
Matt Tolentino | 7ae65fd | 2005-09-03 15:56:27 -0700 | [diff] [blame] | 212 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
| 213 | md = p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | if (!is_available_memory(md)) |
| 215 | continue; |
| 216 | addr = (md->phys_addr+PAGE_SIZE-1) >> PAGE_SHIFT; |
| 217 | end = (md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT)) >> PAGE_SHIFT; |
| 218 | |
| 219 | if ((pagenr >= addr) && (pagenr < end)) |
| 220 | return 1; |
| 221 | } |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | for (i = 0; i < e820.nr_map; i++) { |
| 226 | |
| 227 | if (e820.map[i].type != E820_RAM) /* not usable memory */ |
| 228 | continue; |
| 229 | /* |
| 230 | * !!!FIXME!!! Some BIOSen report areas as RAM that |
| 231 | * are not. Notably the 640->1Mb area. We need a sanity |
| 232 | * check here. |
| 233 | */ |
| 234 | addr = (e820.map[i].addr+PAGE_SIZE-1) >> PAGE_SHIFT; |
| 235 | end = (e820.map[i].addr+e820.map[i].size) >> PAGE_SHIFT; |
| 236 | if ((pagenr >= addr) && (pagenr < end)) |
| 237 | return 1; |
| 238 | } |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | #ifdef CONFIG_HIGHMEM |
| 243 | pte_t *kmap_pte; |
| 244 | pgprot_t kmap_prot; |
| 245 | |
| 246 | #define kmap_get_fixmap_pte(vaddr) \ |
| 247 | pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr), vaddr), (vaddr)), (vaddr)) |
| 248 | |
| 249 | static void __init kmap_init(void) |
| 250 | { |
| 251 | unsigned long kmap_vstart; |
| 252 | |
| 253 | /* cache the first kmap pte */ |
| 254 | kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN); |
| 255 | kmap_pte = kmap_get_fixmap_pte(kmap_vstart); |
| 256 | |
| 257 | kmap_prot = PAGE_KERNEL; |
| 258 | } |
| 259 | |
| 260 | static void __init permanent_kmaps_init(pgd_t *pgd_base) |
| 261 | { |
| 262 | pgd_t *pgd; |
| 263 | pud_t *pud; |
| 264 | pmd_t *pmd; |
| 265 | pte_t *pte; |
| 266 | unsigned long vaddr; |
| 267 | |
| 268 | vaddr = PKMAP_BASE; |
| 269 | page_table_range_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base); |
| 270 | |
| 271 | pgd = swapper_pg_dir + pgd_index(vaddr); |
| 272 | pud = pud_offset(pgd, vaddr); |
| 273 | pmd = pmd_offset(pud, vaddr); |
| 274 | pte = pte_offset_kernel(pmd, vaddr); |
| 275 | pkmap_page_table = pte; |
| 276 | } |
| 277 | |
Matt Tolentino | c09b424 | 2006-01-17 07:03:44 +0100 | [diff] [blame] | 278 | static void __meminit free_new_highpage(struct page *page) |
Dave Hansen | 05039b9 | 2005-10-29 18:16:57 -0700 | [diff] [blame] | 279 | { |
Nick Piggin | 7835e98 | 2006-03-22 00:08:40 -0800 | [diff] [blame] | 280 | init_page_count(page); |
Dave Hansen | 05039b9 | 2005-10-29 18:16:57 -0700 | [diff] [blame] | 281 | __free_page(page); |
| 282 | totalhigh_pages++; |
| 283 | } |
| 284 | |
| 285 | void __init add_one_highpage_init(struct page *page, int pfn, int bad_ppro) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | { |
| 287 | if (page_is_ram(pfn) && !(bad_ppro && page_kills_ppro(pfn))) { |
| 288 | ClearPageReserved(page); |
Dave Hansen | 05039b9 | 2005-10-29 18:16:57 -0700 | [diff] [blame] | 289 | free_new_highpage(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | } else |
| 291 | SetPageReserved(page); |
| 292 | } |
| 293 | |
Vivek Goyal | 0e0be25 | 2007-01-11 01:52:44 +0100 | [diff] [blame] | 294 | static int __meminit add_one_highpage_hotplug(struct page *page, unsigned long pfn) |
Dave Hansen | 05039b9 | 2005-10-29 18:16:57 -0700 | [diff] [blame] | 295 | { |
| 296 | free_new_highpage(page); |
| 297 | totalram_pages++; |
| 298 | #ifdef CONFIG_FLATMEM |
| 299 | max_mapnr = max(pfn, max_mapnr); |
| 300 | #endif |
| 301 | num_physpages++; |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | /* |
| 306 | * Not currently handling the NUMA case. |
| 307 | * Assuming single node and all memory that |
| 308 | * has been added dynamically that would be |
| 309 | * onlined here is in HIGHMEM |
| 310 | */ |
Vivek Goyal | 0e0be25 | 2007-01-11 01:52:44 +0100 | [diff] [blame] | 311 | void __meminit online_page(struct page *page) |
Dave Hansen | 05039b9 | 2005-10-29 18:16:57 -0700 | [diff] [blame] | 312 | { |
| 313 | ClearPageReserved(page); |
| 314 | add_one_highpage_hotplug(page, page_to_pfn(page)); |
| 315 | } |
| 316 | |
| 317 | |
Andy Whitcroft | 05b79bd | 2005-06-23 00:07:57 -0700 | [diff] [blame] | 318 | #ifdef CONFIG_NUMA |
| 319 | extern void set_highmem_pages_init(int); |
| 320 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | static void __init set_highmem_pages_init(int bad_ppro) |
| 322 | { |
| 323 | int pfn; |
Ingo Molnar | 23be8c7 | 2008-01-15 16:44:37 +0100 | [diff] [blame^] | 324 | for (pfn = highstart_pfn; pfn < highend_pfn; pfn++) { |
| 325 | /* |
| 326 | * Holes under sparsemem might not have no mem_map[]: |
| 327 | */ |
| 328 | if (pfn_valid(pfn)) |
| 329 | add_one_highpage_init(pfn_to_page(pfn), pfn, bad_ppro); |
| 330 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | totalram_pages += totalhigh_pages; |
| 332 | } |
Andy Whitcroft | 05b79bd | 2005-06-23 00:07:57 -0700 | [diff] [blame] | 333 | #endif /* CONFIG_FLATMEM */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
| 335 | #else |
| 336 | #define kmap_init() do { } while (0) |
| 337 | #define permanent_kmaps_init(pgd_base) do { } while (0) |
| 338 | #define set_highmem_pages_init(bad_ppro) do { } while (0) |
| 339 | #endif /* CONFIG_HIGHMEM */ |
| 340 | |
| 341 | unsigned long long __PAGE_KERNEL = _PAGE_KERNEL; |
Alexey Dobriyan | 129f694 | 2005-06-23 00:08:33 -0700 | [diff] [blame] | 342 | EXPORT_SYMBOL(__PAGE_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | unsigned long long __PAGE_KERNEL_EXEC = _PAGE_KERNEL_EXEC; |
| 344 | |
Andy Whitcroft | 05b79bd | 2005-06-23 00:07:57 -0700 | [diff] [blame] | 345 | #ifdef CONFIG_NUMA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | extern void __init remap_numa_kva(void); |
Andy Whitcroft | 05b79bd | 2005-06-23 00:07:57 -0700 | [diff] [blame] | 347 | #else |
| 348 | #define remap_numa_kva() do {} while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | #endif |
| 350 | |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 351 | void __init native_pagetable_setup_start(pgd_t *base) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | #ifdef CONFIG_X86_PAE |
| 354 | int i; |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 355 | |
| 356 | /* |
| 357 | * Init entries of the first-level page table to the |
| 358 | * zero page, if they haven't already been set up. |
| 359 | * |
| 360 | * In a normal native boot, we'll be running on a |
| 361 | * pagetable rooted in swapper_pg_dir, but not in PAE |
| 362 | * mode, so this will end up clobbering the mappings |
| 363 | * for the lower 24Mbytes of the address space, |
| 364 | * without affecting the kernel address space. |
| 365 | */ |
| 366 | for (i = 0; i < USER_PTRS_PER_PGD; i++) |
| 367 | set_pgd(&base[i], |
| 368 | __pgd(__pa(empty_zero_page) | _PAGE_PRESENT)); |
| 369 | |
| 370 | /* Make sure kernel address space is empty so that a pagetable |
| 371 | will be allocated for it. */ |
| 372 | memset(&base[USER_PTRS_PER_PGD], 0, |
| 373 | KERNEL_PGD_PTRS * sizeof(pgd_t)); |
Zachary Amsden | c119ecc | 2007-02-13 13:26:21 +0100 | [diff] [blame] | 374 | #else |
| 375 | paravirt_alloc_pd(__pa(swapper_pg_dir) >> PAGE_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | #endif |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | void __init native_pagetable_setup_done(pgd_t *base) |
| 380 | { |
| 381 | #ifdef CONFIG_X86_PAE |
| 382 | /* |
| 383 | * Add low memory identity-mappings - SMP needs it when |
| 384 | * starting up on an AP from real-mode. In the non-PAE |
| 385 | * case we already have these mappings through head.S. |
| 386 | * All user-space mappings are explicitly cleared after |
| 387 | * SMP startup. |
| 388 | */ |
| 389 | set_pgd(&base[0], base[USER_PTRS_PER_PGD]); |
| 390 | #endif |
| 391 | } |
| 392 | |
| 393 | /* |
| 394 | * Build a proper pagetable for the kernel mappings. Up until this |
| 395 | * point, we've been running on some set of pagetables constructed by |
| 396 | * the boot process. |
| 397 | * |
| 398 | * If we're booting on native hardware, this will be a pagetable |
| 399 | * constructed in arch/i386/kernel/head.S, and not running in PAE mode |
| 400 | * (even if we'll end up running in PAE). The root of the pagetable |
| 401 | * will be swapper_pg_dir. |
| 402 | * |
| 403 | * If we're booting paravirtualized under a hypervisor, then there are |
| 404 | * more options: we may already be running PAE, and the pagetable may |
| 405 | * or may not be based in swapper_pg_dir. In any case, |
| 406 | * paravirt_pagetable_setup_start() will set up swapper_pg_dir |
| 407 | * appropriately for the rest of the initialization to work. |
| 408 | * |
| 409 | * In general, pagetable_init() assumes that the pagetable may already |
| 410 | * be partially populated, and so it avoids stomping on any existing |
| 411 | * mappings. |
| 412 | */ |
| 413 | static void __init pagetable_init (void) |
| 414 | { |
| 415 | unsigned long vaddr, end; |
| 416 | pgd_t *pgd_base = swapper_pg_dir; |
| 417 | |
| 418 | paravirt_pagetable_setup_start(pgd_base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
| 420 | /* Enable PSE if available */ |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 421 | if (cpu_has_pse) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | set_in_cr4(X86_CR4_PSE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | |
| 424 | /* Enable PGE if available */ |
| 425 | if (cpu_has_pge) { |
| 426 | set_in_cr4(X86_CR4_PGE); |
| 427 | __PAGE_KERNEL |= _PAGE_GLOBAL; |
| 428 | __PAGE_KERNEL_EXEC |= _PAGE_GLOBAL; |
| 429 | } |
| 430 | |
| 431 | kernel_physical_mapping_init(pgd_base); |
| 432 | remap_numa_kva(); |
| 433 | |
| 434 | /* |
| 435 | * Fixed mappings, only the page table structure has to be |
| 436 | * created - mappings will be set by set_fixmap(): |
| 437 | */ |
| 438 | vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK; |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 439 | end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK; |
| 440 | page_table_range_init(vaddr, end, pgd_base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | |
| 442 | permanent_kmaps_init(pgd_base); |
| 443 | |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 444 | paravirt_pagetable_setup_done(pgd_base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Rafael J. Wysocki | b0cb1a1 | 2007-07-29 23:24:36 +0200 | [diff] [blame] | 447 | #if defined(CONFIG_HIBERNATION) || defined(CONFIG_ACPI) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | /* |
| 449 | * Swap suspend & friends need this for resume because things like the intel-agp |
| 450 | * driver might have split up a kernel 4MB mapping. |
| 451 | */ |
| 452 | char __nosavedata swsusp_pg_dir[PAGE_SIZE] |
| 453 | __attribute__ ((aligned (PAGE_SIZE))); |
| 454 | |
| 455 | static inline void save_pg_dir(void) |
| 456 | { |
| 457 | memcpy(swsusp_pg_dir, swapper_pg_dir, PAGE_SIZE); |
| 458 | } |
| 459 | #else |
| 460 | static inline void save_pg_dir(void) |
| 461 | { |
| 462 | } |
| 463 | #endif |
| 464 | |
| 465 | void zap_low_mappings (void) |
| 466 | { |
| 467 | int i; |
| 468 | |
| 469 | save_pg_dir(); |
| 470 | |
| 471 | /* |
| 472 | * Zap initial low-memory mappings. |
| 473 | * |
| 474 | * Note that "pgd_clear()" doesn't do it for |
| 475 | * us, because pgd_clear() is a no-op on i386. |
| 476 | */ |
| 477 | for (i = 0; i < USER_PTRS_PER_PGD; i++) |
| 478 | #ifdef CONFIG_X86_PAE |
| 479 | set_pgd(swapper_pg_dir+i, __pgd(1 + __pa(empty_zero_page))); |
| 480 | #else |
| 481 | set_pgd(swapper_pg_dir+i, __pgd(0)); |
| 482 | #endif |
| 483 | flush_tlb_all(); |
| 484 | } |
| 485 | |
Jan Beulich | d5321ab | 2007-07-21 17:10:26 +0200 | [diff] [blame] | 486 | int nx_enabled = 0; |
| 487 | |
| 488 | #ifdef CONFIG_X86_PAE |
| 489 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | static int disable_nx __initdata = 0; |
Ravikiran G Thirumalai | 6c231b7 | 2005-09-06 15:17:45 -0700 | [diff] [blame] | 491 | u64 __supported_pte_mask __read_mostly = ~_PAGE_NX; |
Jeremy Fitzhardinge | bdef40a | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 492 | EXPORT_SYMBOL_GPL(__supported_pte_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | |
| 494 | /* |
| 495 | * noexec = on|off |
| 496 | * |
| 497 | * Control non executable mappings. |
| 498 | * |
| 499 | * on Enable |
| 500 | * off Disable |
| 501 | */ |
Rusty Russell | 1a3f239 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 502 | static int __init noexec_setup(char *str) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | { |
Rusty Russell | 1a3f239 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 504 | if (!str || !strcmp(str, "on")) { |
| 505 | if (cpu_has_nx) { |
| 506 | __supported_pte_mask |= _PAGE_NX; |
| 507 | disable_nx = 0; |
| 508 | } |
| 509 | } else if (!strcmp(str,"off")) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | disable_nx = 1; |
| 511 | __supported_pte_mask &= ~_PAGE_NX; |
Rusty Russell | 1a3f239 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 512 | } else |
| 513 | return -EINVAL; |
| 514 | |
| 515 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | } |
Rusty Russell | 1a3f239 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 517 | early_param("noexec", noexec_setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | static void __init set_nx(void) |
| 520 | { |
| 521 | unsigned int v[4], l, h; |
| 522 | |
| 523 | if (cpu_has_pae && (cpuid_eax(0x80000000) > 0x80000001)) { |
| 524 | cpuid(0x80000001, &v[0], &v[1], &v[2], &v[3]); |
| 525 | if ((v[3] & (1 << 20)) && !disable_nx) { |
| 526 | rdmsr(MSR_EFER, l, h); |
| 527 | l |= EFER_NX; |
| 528 | wrmsr(MSR_EFER, l, h); |
| 529 | nx_enabled = 1; |
| 530 | __supported_pte_mask |= _PAGE_NX; |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | /* |
| 536 | * Enables/disables executability of a given kernel page and |
| 537 | * returns the previous setting. |
| 538 | */ |
| 539 | int __init set_kernel_exec(unsigned long vaddr, int enable) |
| 540 | { |
| 541 | pte_t *pte; |
| 542 | int ret = 1; |
| 543 | |
| 544 | if (!nx_enabled) |
| 545 | goto out; |
| 546 | |
| 547 | pte = lookup_address(vaddr); |
| 548 | BUG_ON(!pte); |
| 549 | |
| 550 | if (!pte_exec_kernel(*pte)) |
| 551 | ret = 0; |
| 552 | |
| 553 | if (enable) |
| 554 | pte->pte_high &= ~(1 << (_PAGE_BIT_NX - 32)); |
| 555 | else |
| 556 | pte->pte_high |= 1 << (_PAGE_BIT_NX - 32); |
Zachary Amsden | 789e6ac | 2006-09-30 23:29:38 -0700 | [diff] [blame] | 557 | pte_update_defer(&init_mm, vaddr, pte); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | __flush_tlb_all(); |
| 559 | out: |
| 560 | return ret; |
| 561 | } |
| 562 | |
| 563 | #endif |
| 564 | |
| 565 | /* |
| 566 | * paging_init() sets up the page tables - note that the first 8MB are |
| 567 | * already mapped by head.S. |
| 568 | * |
| 569 | * This routines also unmaps the page at virtual kernel address 0, so |
| 570 | * that we can trap those pesky NULL-reference errors in the kernel. |
| 571 | */ |
| 572 | void __init paging_init(void) |
| 573 | { |
| 574 | #ifdef CONFIG_X86_PAE |
| 575 | set_nx(); |
| 576 | if (nx_enabled) |
| 577 | printk("NX (Execute Disable) protection: active\n"); |
| 578 | #endif |
| 579 | |
| 580 | pagetable_init(); |
| 581 | |
| 582 | load_cr3(swapper_pg_dir); |
| 583 | |
| 584 | #ifdef CONFIG_X86_PAE |
| 585 | /* |
| 586 | * We will bail out later - printk doesn't work right now so |
| 587 | * the user would just see a hanging kernel. |
| 588 | */ |
| 589 | if (cpu_has_pae) |
| 590 | set_in_cr4(X86_CR4_PAE); |
| 591 | #endif |
| 592 | __flush_tlb_all(); |
| 593 | |
| 594 | kmap_init(); |
| 595 | } |
| 596 | |
| 597 | /* |
| 598 | * Test if the WP bit works in supervisor mode. It isn't supported on 386's |
| 599 | * and also on some strange 486's (NexGen etc.). All 586+'s are OK. This |
| 600 | * used to involve black magic jumps to work around some nasty CPU bugs, |
| 601 | * but fortunately the switch to using exceptions got rid of all that. |
| 602 | */ |
| 603 | |
| 604 | static void __init test_wp_bit(void) |
| 605 | { |
| 606 | printk("Checking if this processor honours the WP bit even in supervisor mode... "); |
| 607 | |
| 608 | /* Any page-aligned address will do, the test is non-destructive */ |
| 609 | __set_fixmap(FIX_WP_TEST, __pa(&swapper_pg_dir), PAGE_READONLY); |
| 610 | boot_cpu_data.wp_works_ok = do_test_wp_bit(); |
| 611 | clear_fixmap(FIX_WP_TEST); |
| 612 | |
| 613 | if (!boot_cpu_data.wp_works_ok) { |
| 614 | printk("No.\n"); |
| 615 | #ifdef CONFIG_X86_WP_WORKS_OK |
| 616 | panic("This kernel doesn't support CPU's with broken WP. Recompile it for a 386!"); |
| 617 | #endif |
| 618 | } else { |
| 619 | printk("Ok.\n"); |
| 620 | } |
| 621 | } |
| 622 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | static struct kcore_list kcore_mem, kcore_vmalloc; |
| 624 | |
| 625 | void __init mem_init(void) |
| 626 | { |
| 627 | extern int ppro_with_ram_bug(void); |
| 628 | int codesize, reservedpages, datasize, initsize; |
| 629 | int tmp; |
| 630 | int bad_ppro; |
| 631 | |
Andy Whitcroft | 05b79bd | 2005-06-23 00:07:57 -0700 | [diff] [blame] | 632 | #ifdef CONFIG_FLATMEM |
Eric Sesterhenn | 8d8f3cb | 2006-10-03 23:34:58 +0200 | [diff] [blame] | 633 | BUG_ON(!mem_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | #endif |
| 635 | |
| 636 | bad_ppro = ppro_with_ram_bug(); |
| 637 | |
| 638 | #ifdef CONFIG_HIGHMEM |
| 639 | /* check that fixmap and pkmap do not overlap */ |
| 640 | if (PKMAP_BASE+LAST_PKMAP*PAGE_SIZE >= FIXADDR_START) { |
| 641 | printk(KERN_ERR "fixmap and kmap areas overlap - this will crash\n"); |
| 642 | printk(KERN_ERR "pkstart: %lxh pkend: %lxh fixstart %lxh\n", |
| 643 | PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE, FIXADDR_START); |
| 644 | BUG(); |
| 645 | } |
| 646 | #endif |
| 647 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | /* this will put all low memory onto the freelists */ |
| 649 | totalram_pages += free_all_bootmem(); |
| 650 | |
| 651 | reservedpages = 0; |
| 652 | for (tmp = 0; tmp < max_low_pfn; tmp++) |
| 653 | /* |
| 654 | * Only count reserved RAM pages |
| 655 | */ |
| 656 | if (page_is_ram(tmp) && PageReserved(pfn_to_page(tmp))) |
| 657 | reservedpages++; |
| 658 | |
| 659 | set_highmem_pages_init(bad_ppro); |
| 660 | |
| 661 | codesize = (unsigned long) &_etext - (unsigned long) &_text; |
| 662 | datasize = (unsigned long) &_edata - (unsigned long) &_etext; |
| 663 | initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin; |
| 664 | |
| 665 | kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT); |
| 666 | kclist_add(&kcore_vmalloc, (void *)VMALLOC_START, |
| 667 | VMALLOC_END-VMALLOC_START); |
| 668 | |
| 669 | printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, %dk reserved, %dk data, %dk init, %ldk highmem)\n", |
| 670 | (unsigned long) nr_free_pages() << (PAGE_SHIFT-10), |
| 671 | num_physpages << (PAGE_SHIFT-10), |
| 672 | codesize >> 10, |
| 673 | reservedpages << (PAGE_SHIFT-10), |
| 674 | datasize >> 10, |
| 675 | initsize >> 10, |
| 676 | (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10)) |
| 677 | ); |
| 678 | |
Jeremy Fitzhardinge | 052e799 | 2006-09-25 23:32:25 -0700 | [diff] [blame] | 679 | #if 1 /* double-sanity-check paranoia */ |
| 680 | printk("virtual kernel memory layout:\n" |
| 681 | " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n" |
| 682 | #ifdef CONFIG_HIGHMEM |
| 683 | " pkmap : 0x%08lx - 0x%08lx (%4ld kB)\n" |
| 684 | #endif |
| 685 | " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n" |
| 686 | " lowmem : 0x%08lx - 0x%08lx (%4ld MB)\n" |
| 687 | " .init : 0x%08lx - 0x%08lx (%4ld kB)\n" |
| 688 | " .data : 0x%08lx - 0x%08lx (%4ld kB)\n" |
| 689 | " .text : 0x%08lx - 0x%08lx (%4ld kB)\n", |
| 690 | FIXADDR_START, FIXADDR_TOP, |
| 691 | (FIXADDR_TOP - FIXADDR_START) >> 10, |
| 692 | |
| 693 | #ifdef CONFIG_HIGHMEM |
| 694 | PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE, |
| 695 | (LAST_PKMAP*PAGE_SIZE) >> 10, |
| 696 | #endif |
| 697 | |
| 698 | VMALLOC_START, VMALLOC_END, |
| 699 | (VMALLOC_END - VMALLOC_START) >> 20, |
| 700 | |
| 701 | (unsigned long)__va(0), (unsigned long)high_memory, |
| 702 | ((unsigned long)high_memory - (unsigned long)__va(0)) >> 20, |
| 703 | |
| 704 | (unsigned long)&__init_begin, (unsigned long)&__init_end, |
| 705 | ((unsigned long)&__init_end - (unsigned long)&__init_begin) >> 10, |
| 706 | |
| 707 | (unsigned long)&_etext, (unsigned long)&_edata, |
| 708 | ((unsigned long)&_edata - (unsigned long)&_etext) >> 10, |
| 709 | |
| 710 | (unsigned long)&_text, (unsigned long)&_etext, |
| 711 | ((unsigned long)&_etext - (unsigned long)&_text) >> 10); |
| 712 | |
| 713 | #ifdef CONFIG_HIGHMEM |
| 714 | BUG_ON(PKMAP_BASE+LAST_PKMAP*PAGE_SIZE > FIXADDR_START); |
| 715 | BUG_ON(VMALLOC_END > PKMAP_BASE); |
| 716 | #endif |
| 717 | BUG_ON(VMALLOC_START > VMALLOC_END); |
| 718 | BUG_ON((unsigned long)high_memory > VMALLOC_START); |
| 719 | #endif /* double-sanity-check paranoia */ |
| 720 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | #ifdef CONFIG_X86_PAE |
| 722 | if (!cpu_has_pae) |
| 723 | panic("cannot execute a PAE-enabled kernel on a PAE-less CPU!"); |
| 724 | #endif |
| 725 | if (boot_cpu_data.wp_works_ok < 0) |
| 726 | test_wp_bit(); |
| 727 | |
| 728 | /* |
| 729 | * Subtle. SMP is doing it's boot stuff late (because it has to |
| 730 | * fork idle threads) - but it also needs low mappings for the |
| 731 | * protected-mode entry to work. We zap these entries only after |
| 732 | * the WP-bit has been tested. |
| 733 | */ |
| 734 | #ifndef CONFIG_SMP |
| 735 | zap_low_mappings(); |
| 736 | #endif |
| 737 | } |
| 738 | |
KAMEZAWA Hiroyuki | ad8f579 | 2006-05-20 15:00:03 -0700 | [diff] [blame] | 739 | #ifdef CONFIG_MEMORY_HOTPLUG |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 740 | int arch_add_memory(int nid, u64 start, u64 size) |
Dave Hansen | 05039b9 | 2005-10-29 18:16:57 -0700 | [diff] [blame] | 741 | { |
Yasunori Goto | 7c7e942 | 2006-12-22 01:11:13 -0800 | [diff] [blame] | 742 | struct pglist_data *pgdata = NODE_DATA(nid); |
Christoph Lameter | 776ed98 | 2006-09-25 23:31:09 -0700 | [diff] [blame] | 743 | struct zone *zone = pgdata->node_zones + ZONE_HIGHMEM; |
Dave Hansen | 05039b9 | 2005-10-29 18:16:57 -0700 | [diff] [blame] | 744 | unsigned long start_pfn = start >> PAGE_SHIFT; |
| 745 | unsigned long nr_pages = size >> PAGE_SHIFT; |
| 746 | |
| 747 | return __add_pages(zone, start_pfn, nr_pages); |
| 748 | } |
| 749 | |
Andi Kleen | 9d99aaa | 2006-04-07 19:49:15 +0200 | [diff] [blame] | 750 | #endif |
Dave Hansen | 05039b9 | 2005-10-29 18:16:57 -0700 | [diff] [blame] | 751 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 752 | struct kmem_cache *pmd_cache; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | |
| 754 | void __init pgtable_cache_init(void) |
| 755 | { |
Jeremy Fitzhardinge | 4f81784 | 2007-10-16 11:51:29 -0700 | [diff] [blame] | 756 | if (PTRS_PER_PMD > 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | pmd_cache = kmem_cache_create("pmd", |
Jeremy Fitzhardinge | 4f81784 | 2007-10-16 11:51:29 -0700 | [diff] [blame] | 758 | PTRS_PER_PMD*sizeof(pmd_t), |
| 759 | PTRS_PER_PMD*sizeof(pmd_t), |
| 760 | SLAB_PANIC, |
| 761 | pmd_ctor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | /* |
| 765 | * This function cannot be __init, since exceptions don't work in that |
| 766 | * section. Put this after the callers, so that it cannot be inlined. |
| 767 | */ |
| 768 | static int noinline do_test_wp_bit(void) |
| 769 | { |
| 770 | char tmp_reg; |
| 771 | int flag; |
| 772 | |
| 773 | __asm__ __volatile__( |
| 774 | " movb %0,%1 \n" |
| 775 | "1: movb %1,%0 \n" |
| 776 | " xorl %2,%2 \n" |
| 777 | "2: \n" |
| 778 | ".section __ex_table,\"a\"\n" |
| 779 | " .align 4 \n" |
| 780 | " .long 1b,2b \n" |
| 781 | ".previous \n" |
| 782 | :"=m" (*(char *)fix_to_virt(FIX_WP_TEST)), |
| 783 | "=q" (tmp_reg), |
| 784 | "=r" (flag) |
| 785 | :"2" (1) |
| 786 | :"memory"); |
| 787 | |
| 788 | return flag; |
| 789 | } |
| 790 | |
Arjan van de Ven | 63aaf30 | 2006-01-06 00:12:02 -0800 | [diff] [blame] | 791 | #ifdef CONFIG_DEBUG_RODATA |
| 792 | |
Arjan van de Ven | 63aaf30 | 2006-01-06 00:12:02 -0800 | [diff] [blame] | 793 | void mark_rodata_ro(void) |
| 794 | { |
Jan Beulich | 6fb1475 | 2007-05-02 19:27:10 +0200 | [diff] [blame] | 795 | unsigned long start = PFN_ALIGN(_text); |
| 796 | unsigned long size = PFN_ALIGN(_etext) - start; |
Arjan van de Ven | 63aaf30 | 2006-01-06 00:12:02 -0800 | [diff] [blame] | 797 | |
Linus Torvalds | 602033e | 2007-07-26 12:07:21 -0700 | [diff] [blame] | 798 | #ifndef CONFIG_KPROBES |
| 799 | #ifdef CONFIG_HOTPLUG_CPU |
| 800 | /* It must still be possible to apply SMP alternatives. */ |
| 801 | if (num_possible_cpus() <= 1) |
| 802 | #endif |
| 803 | { |
| 804 | change_page_attr(virt_to_page(start), |
| 805 | size >> PAGE_SHIFT, PAGE_KERNEL_RX); |
| 806 | printk("Write protecting the kernel text: %luk\n", size >> 10); |
| 807 | } |
| 808 | #endif |
Jan Beulich | 6fb1475 | 2007-05-02 19:27:10 +0200 | [diff] [blame] | 809 | start += size; |
| 810 | size = (unsigned long)__end_rodata - start; |
| 811 | change_page_attr(virt_to_page(start), |
| 812 | size >> PAGE_SHIFT, PAGE_KERNEL_RO); |
| 813 | printk("Write protecting the kernel read-only data: %luk\n", |
| 814 | size >> 10); |
Arjan van de Ven | 63aaf30 | 2006-01-06 00:12:02 -0800 | [diff] [blame] | 815 | |
| 816 | /* |
| 817 | * change_page_attr() requires a global_flush_tlb() call after it. |
| 818 | * We do this after the printk so that if something went wrong in the |
| 819 | * change, the printk gets out at least to give a better debug hint |
| 820 | * of who is the culprit. |
| 821 | */ |
| 822 | global_flush_tlb(); |
| 823 | } |
| 824 | #endif |
| 825 | |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 826 | void free_init_pages(char *what, unsigned long begin, unsigned long end) |
| 827 | { |
| 828 | unsigned long addr; |
| 829 | |
| 830 | for (addr = begin; addr < end; addr += PAGE_SIZE) { |
Linus Torvalds | e3ebadd | 2007-05-07 08:44:24 -0700 | [diff] [blame] | 831 | ClearPageReserved(virt_to_page(addr)); |
| 832 | init_page_count(virt_to_page(addr)); |
| 833 | memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE); |
| 834 | free_page(addr); |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 835 | totalram_pages++; |
| 836 | } |
Jan Beulich | 6fb1475 | 2007-05-02 19:27:10 +0200 | [diff] [blame] | 837 | printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10); |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | void free_initmem(void) |
| 841 | { |
| 842 | free_init_pages("unused kernel memory", |
Linus Torvalds | e3ebadd | 2007-05-07 08:44:24 -0700 | [diff] [blame] | 843 | (unsigned long)(&__init_begin), |
| 844 | (unsigned long)(&__init_end)); |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 845 | } |
Arjan van de Ven | 63aaf30 | 2006-01-06 00:12:02 -0800 | [diff] [blame] | 846 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | #ifdef CONFIG_BLK_DEV_INITRD |
| 848 | void free_initrd_mem(unsigned long start, unsigned long end) |
| 849 | { |
Linus Torvalds | e3ebadd | 2007-05-07 08:44:24 -0700 | [diff] [blame] | 850 | free_init_pages("initrd memory", start, end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | } |
| 852 | #endif |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 853 | |