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