1 #include <linux/ioport.h>
2 #include <linux/swap.h>
4 #include <asm/cacheflush.h>
8 #include <asm/page_types.h>
9 #include <asm/sections.h>
10 #include <asm/setup.h>
11 #include <asm/system.h>
12 #include <asm/tlbflush.h>
15 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
17 unsigned long __initdata e820_table_start;
18 unsigned long __meminitdata e820_table_end;
19 unsigned long __meminitdata e820_table_top;
24 #ifdef CONFIG_DIRECT_GBPAGES
31 #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
32 static int disable_nx __cpuinitdata;
37 * Control non-executable mappings for processes.
42 static int __init noexec_setup(char *str)
46 if (!strncmp(str, "on", 2)) {
47 __supported_pte_mask |= _PAGE_NX;
49 } else if (!strncmp(str, "off", 3)) {
51 __supported_pte_mask &= ~_PAGE_NX;
55 early_param("noexec", noexec_setup);
59 static void __init set_nx(void)
61 unsigned int v[4], l, h;
63 if (cpu_has_pae && (cpuid_eax(0x80000000) > 0x80000001)) {
64 cpuid(0x80000001, &v[0], &v[1], &v[2], &v[3]);
66 if ((v[3] & (1 << 20)) && !disable_nx) {
67 rdmsr(MSR_EFER, l, h);
69 wrmsr(MSR_EFER, l, h);
71 __supported_pte_mask |= _PAGE_NX;
76 static inline void set_nx(void)
82 void __cpuinit check_efer(void)
86 rdmsrl(MSR_EFER, efer);
87 if (!(efer & EFER_NX) || disable_nx)
88 __supported_pte_mask &= ~_PAGE_NX;
92 static void __init find_early_table_space(unsigned long end, int use_pse,
95 unsigned long puds, pmds, ptes, tables, start;
97 puds = (end + PUD_SIZE - 1) >> PUD_SHIFT;
98 tables = roundup(puds * sizeof(pud_t), PAGE_SIZE);
103 extra = end - ((end>>PUD_SHIFT) << PUD_SHIFT);
104 pmds = (extra + PMD_SIZE - 1) >> PMD_SHIFT;
106 pmds = (end + PMD_SIZE - 1) >> PMD_SHIFT;
108 tables += roundup(pmds * sizeof(pmd_t), PAGE_SIZE);
113 extra = end - ((end>>PMD_SHIFT) << PMD_SHIFT);
117 ptes = (extra + PAGE_SIZE - 1) >> PAGE_SHIFT;
119 ptes = (end + PAGE_SIZE - 1) >> PAGE_SHIFT;
121 tables += roundup(ptes * sizeof(pte_t), PAGE_SIZE);
125 tables += roundup(__end_of_fixed_addresses * sizeof(pte_t), PAGE_SIZE);
129 * RED-PEN putting page tables only on node 0 could
130 * cause a hotspot and fill up ZONE_DMA. The page tables
131 * need roughly 0.5KB per GB.
138 e820_table_start = find_e820_area(start, max_pfn_mapped<<PAGE_SHIFT,
140 if (e820_table_start == -1UL)
141 panic("Cannot find space for the kernel page tables");
143 e820_table_start >>= PAGE_SHIFT;
144 e820_table_end = e820_table_start;
145 e820_table_top = e820_table_start + (tables >> PAGE_SHIFT);
147 printk(KERN_DEBUG "kernel direct mapping tables up to %lx @ %lx-%lx\n",
148 end, e820_table_start << PAGE_SHIFT, e820_table_top << PAGE_SHIFT);
154 unsigned page_size_mask;
158 #define NR_RANGE_MR 3
159 #else /* CONFIG_X86_64 */
160 #define NR_RANGE_MR 5
163 static int __meminit save_mr(struct map_range *mr, int nr_range,
164 unsigned long start_pfn, unsigned long end_pfn,
165 unsigned long page_size_mask)
167 if (start_pfn < end_pfn) {
168 if (nr_range >= NR_RANGE_MR)
169 panic("run out of range for init_memory_mapping\n");
170 mr[nr_range].start = start_pfn<<PAGE_SHIFT;
171 mr[nr_range].end = end_pfn<<PAGE_SHIFT;
172 mr[nr_range].page_size_mask = page_size_mask;
180 static void __init init_gbpages(void)
182 if (direct_gbpages && cpu_has_gbpages)
183 printk(KERN_INFO "Using GB pages for direct mapping\n");
188 static inline void init_gbpages(void)
194 * Setup the direct mapping of the physical memory at PAGE_OFFSET.
195 * This runs before bootmem is initialized and gets pages directly from
196 * the physical memory. To access them they are temporarily mapped.
198 unsigned long __init_refok init_memory_mapping(unsigned long start,
201 unsigned long page_size_mask = 0;
202 unsigned long start_pfn, end_pfn;
203 unsigned long ret = 0;
206 struct map_range mr[NR_RANGE_MR];
208 int use_pse, use_gbpages;
210 printk(KERN_INFO "init_memory_mapping: %016lx-%016lx\n", start, end);
215 #ifdef CONFIG_DEBUG_PAGEALLOC
217 * For CONFIG_DEBUG_PAGEALLOC, identity mapping will use small pages.
218 * This will simplify cpa(), which otherwise needs to support splitting
219 * large pages into small in interrupt context, etc.
221 use_pse = use_gbpages = 0;
223 use_pse = cpu_has_pse;
224 use_gbpages = direct_gbpages;
229 printk(KERN_INFO "NX (Execute Disable) protection: active\n");
231 /* Enable PSE if available */
233 set_in_cr4(X86_CR4_PSE);
235 /* Enable PGE if available */
237 set_in_cr4(X86_CR4_PGE);
238 __supported_pte_mask |= _PAGE_GLOBAL;
242 page_size_mask |= 1 << PG_LEVEL_1G;
244 page_size_mask |= 1 << PG_LEVEL_2M;
246 memset(mr, 0, sizeof(mr));
249 /* head if not big page alignment ? */
250 start_pfn = start >> PAGE_SHIFT;
251 pos = start_pfn << PAGE_SHIFT;
254 * Don't use a large page for the first 2/4MB of memory
255 * because there are often fixed size MTRRs in there
256 * and overlapping MTRRs into large pages can cause
260 end_pfn = 1<<(PMD_SHIFT - PAGE_SHIFT);
262 end_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
263 << (PMD_SHIFT - PAGE_SHIFT);
264 #else /* CONFIG_X86_64 */
265 end_pfn = ((pos + (PMD_SIZE - 1)) >> PMD_SHIFT)
266 << (PMD_SHIFT - PAGE_SHIFT);
268 if (end_pfn > (end >> PAGE_SHIFT))
269 end_pfn = end >> PAGE_SHIFT;
270 if (start_pfn < end_pfn) {
271 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
272 pos = end_pfn << PAGE_SHIFT;
275 /* big page (2M) range */
276 start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
277 << (PMD_SHIFT - PAGE_SHIFT);
279 end_pfn = (end>>PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
280 #else /* CONFIG_X86_64 */
281 end_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT)
282 << (PUD_SHIFT - PAGE_SHIFT);
283 if (end_pfn > ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT)))
284 end_pfn = ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT));
287 if (start_pfn < end_pfn) {
288 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
289 page_size_mask & (1<<PG_LEVEL_2M));
290 pos = end_pfn << PAGE_SHIFT;
294 /* big page (1G) range */
295 start_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT)
296 << (PUD_SHIFT - PAGE_SHIFT);
297 end_pfn = (end >> PUD_SHIFT) << (PUD_SHIFT - PAGE_SHIFT);
298 if (start_pfn < end_pfn) {
299 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
301 ((1<<PG_LEVEL_2M)|(1<<PG_LEVEL_1G)));
302 pos = end_pfn << PAGE_SHIFT;
305 /* tail is not big page (1G) alignment */
306 start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
307 << (PMD_SHIFT - PAGE_SHIFT);
308 end_pfn = (end >> PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
309 if (start_pfn < end_pfn) {
310 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
311 page_size_mask & (1<<PG_LEVEL_2M));
312 pos = end_pfn << PAGE_SHIFT;
316 /* tail is not big page (2M) alignment */
317 start_pfn = pos>>PAGE_SHIFT;
318 end_pfn = end>>PAGE_SHIFT;
319 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
321 /* try to merge same page size and continuous */
322 for (i = 0; nr_range > 1 && i < nr_range - 1; i++) {
323 unsigned long old_start;
324 if (mr[i].end != mr[i+1].start ||
325 mr[i].page_size_mask != mr[i+1].page_size_mask)
328 old_start = mr[i].start;
329 memmove(&mr[i], &mr[i+1],
330 (nr_range - 1 - i) * sizeof(struct map_range));
331 mr[i--].start = old_start;
335 for (i = 0; i < nr_range; i++)
336 printk(KERN_DEBUG " %010lx - %010lx page %s\n",
337 mr[i].start, mr[i].end,
338 (mr[i].page_size_mask & (1<<PG_LEVEL_1G))?"1G":(
339 (mr[i].page_size_mask & (1<<PG_LEVEL_2M))?"2M":"4k"));
342 * Find space for the kernel direct mapping tables.
344 * Later we should allocate these tables in the local node of the
345 * memory mapped. Unfortunately this is done currently before the
346 * nodes are discovered.
349 find_early_table_space(end, use_pse, use_gbpages);
352 for (i = 0; i < nr_range; i++)
353 kernel_physical_mapping_init(mr[i].start, mr[i].end,
354 mr[i].page_size_mask);
356 #else /* CONFIG_X86_64 */
357 for (i = 0; i < nr_range; i++)
358 ret = kernel_physical_mapping_init(mr[i].start, mr[i].end,
359 mr[i].page_size_mask);
363 early_ioremap_page_table_range_init();
365 load_cr3(swapper_pg_dir);
369 if (!after_bootmem && !start) {
373 mmu_cr4_features = read_cr4();
376 * _brk_end cannot change anymore, but it and _end may be
377 * located on different 2M pages. cleanup_highmap(), however,
378 * can only consider _end when it runs, so destroy any
379 * mappings beyond _brk_end here.
381 pud = pud_offset(pgd_offset_k(_brk_end), _brk_end);
382 pmd = pmd_offset(pud, _brk_end - 1);
383 while (++pmd <= pmd_offset(pud, (unsigned long)_end - 1))
389 if (!after_bootmem && e820_table_end > e820_table_start)
390 reserve_early(e820_table_start << PAGE_SHIFT,
391 e820_table_end << PAGE_SHIFT, "PGTABLE");
394 early_memtest(start, end);
396 return ret >> PAGE_SHIFT;
401 * devmem_is_allowed() checks to see if /dev/mem access to a certain address
402 * is valid. The argument is a physical page number.
405 * On x86, access has to be given to the first megabyte of ram because that area
406 * contains bios code and data regions used by X and dosemu and similar apps.
407 * Access has to be given to non-kernel-ram areas as well, these contain the PCI
408 * mmio resources as well as potential bios/acpi data regions.
410 int devmem_is_allowed(unsigned long pagenr)
414 if (iomem_is_exclusive(pagenr << PAGE_SHIFT))
416 if (!page_is_ram(pagenr))
421 void free_init_pages(char *what, unsigned long begin, unsigned long end)
423 unsigned long addr = begin;
429 * If debugging page accesses then do not free this memory but
430 * mark them not present - any buggy init-section access will
431 * create a kernel page fault:
433 #ifdef CONFIG_DEBUG_PAGEALLOC
434 printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n",
435 begin, PAGE_ALIGN(end));
436 set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
439 * We just marked the kernel text read only above, now that
440 * we are going to free part of that, we need to make that
443 set_memory_rw(begin, (end - begin) >> PAGE_SHIFT);
445 printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
447 for (; addr < end; addr += PAGE_SIZE) {
448 ClearPageReserved(virt_to_page(addr));
449 init_page_count(virt_to_page(addr));
450 memset((void *)(addr & ~(PAGE_SIZE-1)),
451 POISON_FREE_INITMEM, PAGE_SIZE);
458 void free_initmem(void)
460 free_init_pages("unused kernel memory",
461 (unsigned long)(&__init_begin),
462 (unsigned long)(&__init_end));
465 #ifdef CONFIG_BLK_DEV_INITRD
466 void free_initrd_mem(unsigned long start, unsigned long end)
468 free_init_pages("initrd memory", start, end);