Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 1 | #include <linux/gfp.h> |
Jaswinder Singh Rajput | 2c1b284 | 2009-04-11 00:03:10 +0530 | [diff] [blame] | 2 | #include <linux/initrd.h> |
Pekka Enberg | 540aca0 | 2009-03-04 11:46:40 +0200 | [diff] [blame] | 3 | #include <linux/ioport.h> |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 4 | #include <linux/swap.h> |
Yinghai Lu | a9ce6bc | 2010-08-25 13:39:17 -0700 | [diff] [blame] | 5 | #include <linux/memblock.h> |
Pekka Enberg | 540aca0 | 2009-03-04 11:46:40 +0200 | [diff] [blame] | 6 | |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 7 | #include <asm/cacheflush.h> |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 8 | #include <asm/e820.h> |
Pekka Enberg | 4fcb208 | 2009-03-05 14:55:08 +0200 | [diff] [blame] | 9 | #include <asm/init.h> |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 10 | #include <asm/page.h> |
Pekka Enberg | 540aca0 | 2009-03-04 11:46:40 +0200 | [diff] [blame] | 11 | #include <asm/page_types.h> |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 12 | #include <asm/sections.h> |
Jan Beulich | 4983439 | 2009-05-06 13:06:47 +0100 | [diff] [blame] | 13 | #include <asm/setup.h> |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 14 | #include <asm/system.h> |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 15 | #include <asm/tlbflush.h> |
Pekka Enberg | 9518e0e | 2009-04-28 16:00:50 +0300 | [diff] [blame] | 16 | #include <asm/tlb.h> |
Jaswinder Singh Rajput | 76c0692 | 2009-07-01 19:54:23 +0530 | [diff] [blame] | 17 | #include <asm/proto.h> |
Pekka Enberg | 9518e0e | 2009-04-28 16:00:50 +0300 | [diff] [blame] | 18 | |
| 19 | DEFINE_PER_CPU(struct mmu_gather, mmu_gathers); |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 20 | |
Pekka Enberg | 298af9d | 2009-03-05 14:55:06 +0200 | [diff] [blame] | 21 | unsigned long __initdata e820_table_start; |
| 22 | unsigned long __meminitdata e820_table_end; |
| 23 | unsigned long __meminitdata e820_table_top; |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 24 | |
| 25 | int after_bootmem; |
| 26 | |
| 27 | int direct_gbpages |
| 28 | #ifdef CONFIG_DIRECT_GBPAGES |
| 29 | = 1 |
| 30 | #endif |
| 31 | ; |
| 32 | |
| 33 | static void __init find_early_table_space(unsigned long end, int use_pse, |
| 34 | int use_gbpages) |
| 35 | { |
| 36 | unsigned long puds, pmds, ptes, tables, start; |
Yinghai Lu | a9ce6bc | 2010-08-25 13:39:17 -0700 | [diff] [blame] | 37 | phys_addr_t base; |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 38 | |
| 39 | puds = (end + PUD_SIZE - 1) >> PUD_SHIFT; |
| 40 | tables = roundup(puds * sizeof(pud_t), PAGE_SIZE); |
| 41 | |
| 42 | if (use_gbpages) { |
| 43 | unsigned long extra; |
| 44 | |
| 45 | extra = end - ((end>>PUD_SHIFT) << PUD_SHIFT); |
| 46 | pmds = (extra + PMD_SIZE - 1) >> PMD_SHIFT; |
| 47 | } else |
| 48 | pmds = (end + PMD_SIZE - 1) >> PMD_SHIFT; |
| 49 | |
| 50 | tables += roundup(pmds * sizeof(pmd_t), PAGE_SIZE); |
| 51 | |
| 52 | if (use_pse) { |
| 53 | unsigned long extra; |
| 54 | |
| 55 | extra = end - ((end>>PMD_SHIFT) << PMD_SHIFT); |
| 56 | #ifdef CONFIG_X86_32 |
| 57 | extra += PMD_SIZE; |
| 58 | #endif |
| 59 | ptes = (extra + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 60 | } else |
| 61 | ptes = (end + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 62 | |
| 63 | tables += roundup(ptes * sizeof(pte_t), PAGE_SIZE); |
| 64 | |
| 65 | #ifdef CONFIG_X86_32 |
| 66 | /* for fixmap */ |
| 67 | tables += roundup(__end_of_fixed_addresses * sizeof(pte_t), PAGE_SIZE); |
| 68 | #endif |
| 69 | |
| 70 | /* |
| 71 | * RED-PEN putting page tables only on node 0 could |
| 72 | * cause a hotspot and fill up ZONE_DMA. The page tables |
| 73 | * need roughly 0.5KB per GB. |
| 74 | */ |
| 75 | #ifdef CONFIG_X86_32 |
| 76 | start = 0x7000; |
Yinghai Lu | 80989ce | 2009-05-09 23:47:42 -0700 | [diff] [blame] | 77 | #else |
| 78 | start = 0x8000; |
| 79 | #endif |
Yinghai Lu | a9ce6bc | 2010-08-25 13:39:17 -0700 | [diff] [blame] | 80 | base = memblock_find_in_range(start, max_pfn_mapped<<PAGE_SHIFT, |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 81 | tables, PAGE_SIZE); |
Yinghai Lu | a9ce6bc | 2010-08-25 13:39:17 -0700 | [diff] [blame] | 82 | if (base == MEMBLOCK_ERROR) |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 83 | panic("Cannot find space for the kernel page tables"); |
| 84 | |
Yinghai Lu | a9ce6bc | 2010-08-25 13:39:17 -0700 | [diff] [blame] | 85 | e820_table_start = base >> PAGE_SHIFT; |
Pekka Enberg | 298af9d | 2009-03-05 14:55:06 +0200 | [diff] [blame] | 86 | e820_table_end = e820_table_start; |
| 87 | e820_table_top = e820_table_start + (tables >> PAGE_SHIFT); |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 88 | |
| 89 | printk(KERN_DEBUG "kernel direct mapping tables up to %lx @ %lx-%lx\n", |
Pekka Enberg | 298af9d | 2009-03-05 14:55:06 +0200 | [diff] [blame] | 90 | end, e820_table_start << PAGE_SHIFT, e820_table_top << PAGE_SHIFT); |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | struct map_range { |
| 94 | unsigned long start; |
| 95 | unsigned long end; |
| 96 | unsigned page_size_mask; |
| 97 | }; |
| 98 | |
| 99 | #ifdef CONFIG_X86_32 |
| 100 | #define NR_RANGE_MR 3 |
| 101 | #else /* CONFIG_X86_64 */ |
| 102 | #define NR_RANGE_MR 5 |
| 103 | #endif |
| 104 | |
Jan Beulich | dc9dd5c | 2009-03-12 12:40:06 +0000 | [diff] [blame] | 105 | static int __meminit save_mr(struct map_range *mr, int nr_range, |
| 106 | unsigned long start_pfn, unsigned long end_pfn, |
| 107 | unsigned long page_size_mask) |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 108 | { |
| 109 | if (start_pfn < end_pfn) { |
| 110 | if (nr_range >= NR_RANGE_MR) |
| 111 | panic("run out of range for init_memory_mapping\n"); |
| 112 | mr[nr_range].start = start_pfn<<PAGE_SHIFT; |
| 113 | mr[nr_range].end = end_pfn<<PAGE_SHIFT; |
| 114 | mr[nr_range].page_size_mask = page_size_mask; |
| 115 | nr_range++; |
| 116 | } |
| 117 | |
| 118 | return nr_range; |
| 119 | } |
| 120 | |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 121 | /* |
| 122 | * Setup the direct mapping of the physical memory at PAGE_OFFSET. |
| 123 | * This runs before bootmem is initialized and gets pages directly from |
| 124 | * the physical memory. To access them they are temporarily mapped. |
| 125 | */ |
| 126 | unsigned long __init_refok init_memory_mapping(unsigned long start, |
| 127 | unsigned long end) |
| 128 | { |
| 129 | unsigned long page_size_mask = 0; |
| 130 | unsigned long start_pfn, end_pfn; |
Pekka Enberg | c77a3b5 | 2009-03-05 17:04:26 +0200 | [diff] [blame] | 131 | unsigned long ret = 0; |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 132 | unsigned long pos; |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 133 | |
| 134 | struct map_range mr[NR_RANGE_MR]; |
| 135 | int nr_range, i; |
| 136 | int use_pse, use_gbpages; |
| 137 | |
| 138 | printk(KERN_INFO "init_memory_mapping: %016lx-%016lx\n", start, end); |
| 139 | |
Vegard Nossum | f856129 | 2008-04-04 00:53:23 +0200 | [diff] [blame] | 140 | #if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_KMEMCHECK) |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 141 | /* |
| 142 | * For CONFIG_DEBUG_PAGEALLOC, identity mapping will use small pages. |
| 143 | * This will simplify cpa(), which otherwise needs to support splitting |
| 144 | * large pages into small in interrupt context, etc. |
| 145 | */ |
| 146 | use_pse = use_gbpages = 0; |
| 147 | #else |
| 148 | use_pse = cpu_has_pse; |
| 149 | use_gbpages = direct_gbpages; |
| 150 | #endif |
| 151 | |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 152 | /* Enable PSE if available */ |
| 153 | if (cpu_has_pse) |
| 154 | set_in_cr4(X86_CR4_PSE); |
| 155 | |
| 156 | /* Enable PGE if available */ |
| 157 | if (cpu_has_pge) { |
| 158 | set_in_cr4(X86_CR4_PGE); |
| 159 | __supported_pte_mask |= _PAGE_GLOBAL; |
| 160 | } |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 161 | |
| 162 | if (use_gbpages) |
| 163 | page_size_mask |= 1 << PG_LEVEL_1G; |
| 164 | if (use_pse) |
| 165 | page_size_mask |= 1 << PG_LEVEL_2M; |
| 166 | |
| 167 | memset(mr, 0, sizeof(mr)); |
| 168 | nr_range = 0; |
| 169 | |
| 170 | /* head if not big page alignment ? */ |
| 171 | start_pfn = start >> PAGE_SHIFT; |
| 172 | pos = start_pfn << PAGE_SHIFT; |
| 173 | #ifdef CONFIG_X86_32 |
| 174 | /* |
| 175 | * Don't use a large page for the first 2/4MB of memory |
| 176 | * because there are often fixed size MTRRs in there |
| 177 | * and overlapping MTRRs into large pages can cause |
| 178 | * slowdowns. |
| 179 | */ |
| 180 | if (pos == 0) |
| 181 | end_pfn = 1<<(PMD_SHIFT - PAGE_SHIFT); |
| 182 | else |
| 183 | end_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT) |
| 184 | << (PMD_SHIFT - PAGE_SHIFT); |
| 185 | #else /* CONFIG_X86_64 */ |
| 186 | end_pfn = ((pos + (PMD_SIZE - 1)) >> PMD_SHIFT) |
| 187 | << (PMD_SHIFT - PAGE_SHIFT); |
| 188 | #endif |
| 189 | if (end_pfn > (end >> PAGE_SHIFT)) |
| 190 | end_pfn = end >> PAGE_SHIFT; |
| 191 | if (start_pfn < end_pfn) { |
| 192 | nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0); |
| 193 | pos = end_pfn << PAGE_SHIFT; |
| 194 | } |
| 195 | |
| 196 | /* big page (2M) range */ |
| 197 | start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT) |
| 198 | << (PMD_SHIFT - PAGE_SHIFT); |
| 199 | #ifdef CONFIG_X86_32 |
| 200 | end_pfn = (end>>PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT); |
| 201 | #else /* CONFIG_X86_64 */ |
| 202 | end_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT) |
| 203 | << (PUD_SHIFT - PAGE_SHIFT); |
| 204 | if (end_pfn > ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT))) |
| 205 | end_pfn = ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT)); |
| 206 | #endif |
| 207 | |
| 208 | if (start_pfn < end_pfn) { |
| 209 | nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, |
| 210 | page_size_mask & (1<<PG_LEVEL_2M)); |
| 211 | pos = end_pfn << PAGE_SHIFT; |
| 212 | } |
| 213 | |
| 214 | #ifdef CONFIG_X86_64 |
| 215 | /* big page (1G) range */ |
| 216 | start_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT) |
| 217 | << (PUD_SHIFT - PAGE_SHIFT); |
| 218 | end_pfn = (end >> PUD_SHIFT) << (PUD_SHIFT - PAGE_SHIFT); |
| 219 | if (start_pfn < end_pfn) { |
| 220 | nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, |
| 221 | page_size_mask & |
| 222 | ((1<<PG_LEVEL_2M)|(1<<PG_LEVEL_1G))); |
| 223 | pos = end_pfn << PAGE_SHIFT; |
| 224 | } |
| 225 | |
| 226 | /* tail is not big page (1G) alignment */ |
| 227 | start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT) |
| 228 | << (PMD_SHIFT - PAGE_SHIFT); |
| 229 | end_pfn = (end >> PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT); |
| 230 | if (start_pfn < end_pfn) { |
| 231 | nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, |
| 232 | page_size_mask & (1<<PG_LEVEL_2M)); |
| 233 | pos = end_pfn << PAGE_SHIFT; |
| 234 | } |
| 235 | #endif |
| 236 | |
| 237 | /* tail is not big page (2M) alignment */ |
| 238 | start_pfn = pos>>PAGE_SHIFT; |
| 239 | end_pfn = end>>PAGE_SHIFT; |
| 240 | nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0); |
| 241 | |
| 242 | /* try to merge same page size and continuous */ |
| 243 | for (i = 0; nr_range > 1 && i < nr_range - 1; i++) { |
| 244 | unsigned long old_start; |
| 245 | if (mr[i].end != mr[i+1].start || |
| 246 | mr[i].page_size_mask != mr[i+1].page_size_mask) |
| 247 | continue; |
| 248 | /* move it */ |
| 249 | old_start = mr[i].start; |
| 250 | memmove(&mr[i], &mr[i+1], |
| 251 | (nr_range - 1 - i) * sizeof(struct map_range)); |
| 252 | mr[i--].start = old_start; |
| 253 | nr_range--; |
| 254 | } |
| 255 | |
| 256 | for (i = 0; i < nr_range; i++) |
| 257 | printk(KERN_DEBUG " %010lx - %010lx page %s\n", |
| 258 | mr[i].start, mr[i].end, |
| 259 | (mr[i].page_size_mask & (1<<PG_LEVEL_1G))?"1G":( |
| 260 | (mr[i].page_size_mask & (1<<PG_LEVEL_2M))?"2M":"4k")); |
| 261 | |
| 262 | /* |
| 263 | * Find space for the kernel direct mapping tables. |
| 264 | * |
| 265 | * Later we should allocate these tables in the local node of the |
| 266 | * memory mapped. Unfortunately this is done currently before the |
| 267 | * nodes are discovered. |
| 268 | */ |
| 269 | if (!after_bootmem) |
| 270 | find_early_table_space(end, use_pse, use_gbpages); |
| 271 | |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 272 | for (i = 0; i < nr_range; i++) |
| 273 | ret = kernel_physical_mapping_init(mr[i].start, mr[i].end, |
| 274 | mr[i].page_size_mask); |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 275 | |
| 276 | #ifdef CONFIG_X86_32 |
| 277 | early_ioremap_page_table_range_init(); |
| 278 | |
| 279 | load_cr3(swapper_pg_dir); |
| 280 | #endif |
| 281 | |
| 282 | #ifdef CONFIG_X86_64 |
Jan Beulich | 4983439 | 2009-05-06 13:06:47 +0100 | [diff] [blame] | 283 | if (!after_bootmem && !start) { |
| 284 | pud_t *pud; |
| 285 | pmd_t *pmd; |
| 286 | |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 287 | mmu_cr4_features = read_cr4(); |
Jan Beulich | 4983439 | 2009-05-06 13:06:47 +0100 | [diff] [blame] | 288 | |
| 289 | /* |
| 290 | * _brk_end cannot change anymore, but it and _end may be |
| 291 | * located on different 2M pages. cleanup_highmap(), however, |
| 292 | * can only consider _end when it runs, so destroy any |
| 293 | * mappings beyond _brk_end here. |
| 294 | */ |
| 295 | pud = pud_offset(pgd_offset_k(_brk_end), _brk_end); |
| 296 | pmd = pmd_offset(pud, _brk_end - 1); |
| 297 | while (++pmd <= pmd_offset(pud, (unsigned long)_end - 1)) |
| 298 | pmd_clear(pmd); |
| 299 | } |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 300 | #endif |
| 301 | __flush_tlb_all(); |
| 302 | |
Pekka Enberg | 298af9d | 2009-03-05 14:55:06 +0200 | [diff] [blame] | 303 | if (!after_bootmem && e820_table_end > e820_table_start) |
Yinghai Lu | a9ce6bc | 2010-08-25 13:39:17 -0700 | [diff] [blame] | 304 | memblock_x86_reserve_range(e820_table_start << PAGE_SHIFT, |
Pekka Enberg | 298af9d | 2009-03-05 14:55:06 +0200 | [diff] [blame] | 305 | e820_table_end << PAGE_SHIFT, "PGTABLE"); |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 306 | |
| 307 | if (!after_bootmem) |
| 308 | early_memtest(start, end); |
| 309 | |
| 310 | return ret >> PAGE_SHIFT; |
| 311 | } |
| 312 | |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 313 | |
Pekka Enberg | 540aca0 | 2009-03-04 11:46:40 +0200 | [diff] [blame] | 314 | /* |
| 315 | * devmem_is_allowed() checks to see if /dev/mem access to a certain address |
| 316 | * is valid. The argument is a physical page number. |
| 317 | * |
| 318 | * |
| 319 | * On x86, access has to be given to the first megabyte of ram because that area |
| 320 | * contains bios code and data regions used by X and dosemu and similar apps. |
| 321 | * Access has to be given to non-kernel-ram areas as well, these contain the PCI |
| 322 | * mmio resources as well as potential bios/acpi data regions. |
| 323 | */ |
| 324 | int devmem_is_allowed(unsigned long pagenr) |
| 325 | { |
| 326 | if (pagenr <= 256) |
| 327 | return 1; |
| 328 | if (iomem_is_exclusive(pagenr << PAGE_SHIFT)) |
| 329 | return 0; |
| 330 | if (!page_is_ram(pagenr)) |
| 331 | return 1; |
| 332 | return 0; |
| 333 | } |
| 334 | |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 335 | void free_init_pages(char *what, unsigned long begin, unsigned long end) |
| 336 | { |
Yinghai Lu | c967da6 | 2010-03-28 19:42:55 -0700 | [diff] [blame] | 337 | unsigned long addr; |
| 338 | unsigned long begin_aligned, end_aligned; |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 339 | |
Yinghai Lu | c967da6 | 2010-03-28 19:42:55 -0700 | [diff] [blame] | 340 | /* Make sure boundaries are page aligned */ |
| 341 | begin_aligned = PAGE_ALIGN(begin); |
| 342 | end_aligned = end & PAGE_MASK; |
| 343 | |
| 344 | if (WARN_ON(begin_aligned != begin || end_aligned != end)) { |
| 345 | begin = begin_aligned; |
| 346 | end = end_aligned; |
| 347 | } |
| 348 | |
| 349 | if (begin >= end) |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 350 | return; |
| 351 | |
Yinghai Lu | c967da6 | 2010-03-28 19:42:55 -0700 | [diff] [blame] | 352 | addr = begin; |
| 353 | |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 354 | /* |
| 355 | * If debugging page accesses then do not free this memory but |
| 356 | * mark them not present - any buggy init-section access will |
| 357 | * create a kernel page fault: |
| 358 | */ |
| 359 | #ifdef CONFIG_DEBUG_PAGEALLOC |
| 360 | printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n", |
Yinghai Lu | c967da6 | 2010-03-28 19:42:55 -0700 | [diff] [blame] | 361 | begin, end); |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 362 | set_memory_np(begin, (end - begin) >> PAGE_SHIFT); |
| 363 | #else |
| 364 | /* |
| 365 | * We just marked the kernel text read only above, now that |
| 366 | * we are going to free part of that, we need to make that |
Matthieu Castet | 5bd5a45 | 2010-11-16 22:31:26 +0100 | [diff] [blame^] | 367 | * writeable and non-executable first. |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 368 | */ |
Matthieu Castet | 5bd5a45 | 2010-11-16 22:31:26 +0100 | [diff] [blame^] | 369 | set_memory_nx(begin, (end - begin) >> PAGE_SHIFT); |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 370 | set_memory_rw(begin, (end - begin) >> PAGE_SHIFT); |
| 371 | |
| 372 | printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10); |
| 373 | |
| 374 | for (; addr < end; addr += PAGE_SIZE) { |
| 375 | ClearPageReserved(virt_to_page(addr)); |
| 376 | init_page_count(virt_to_page(addr)); |
Yinghai Lu | c967da6 | 2010-03-28 19:42:55 -0700 | [diff] [blame] | 377 | memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE); |
Pekka Enberg | e5b2bb5 | 2009-03-03 13:15:06 +0200 | [diff] [blame] | 378 | free_page(addr); |
| 379 | totalram_pages++; |
| 380 | } |
| 381 | #endif |
| 382 | } |
| 383 | |
| 384 | void free_initmem(void) |
| 385 | { |
| 386 | free_init_pages("unused kernel memory", |
| 387 | (unsigned long)(&__init_begin), |
| 388 | (unsigned long)(&__init_end)); |
| 389 | } |
Pekka Enberg | 731ddea | 2009-03-04 11:13:40 +0200 | [diff] [blame] | 390 | |
| 391 | #ifdef CONFIG_BLK_DEV_INITRD |
| 392 | void free_initrd_mem(unsigned long start, unsigned long end) |
| 393 | { |
Yinghai Lu | c967da6 | 2010-03-28 19:42:55 -0700 | [diff] [blame] | 394 | /* |
| 395 | * end could be not aligned, and We can not align that, |
| 396 | * decompresser could be confused by aligned initrd_end |
| 397 | * We already reserve the end partial page before in |
| 398 | * - i386_start_kernel() |
| 399 | * - x86_64_start_kernel() |
| 400 | * - relocate_initrd() |
| 401 | * So here We can do PAGE_ALIGN() safely to get partial page to be freed |
| 402 | */ |
| 403 | free_init_pages("initrd memory", start, PAGE_ALIGN(end)); |
Pekka Enberg | 731ddea | 2009-03-04 11:13:40 +0200 | [diff] [blame] | 404 | } |
| 405 | #endif |