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/system.h>
11 #include <asm/tlbflush.h>
13 unsigned long __initdata e820_table_start;
14 unsigned long __meminitdata e820_table_end;
15 unsigned long __meminitdata e820_table_top;
20 #ifdef CONFIG_DIRECT_GBPAGES
27 #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
28 static int disable_nx __cpuinitdata;
33 * Control non-executable mappings for processes.
38 static int __init noexec_setup(char *str)
42 if (!strncmp(str, "on", 2)) {
43 __supported_pte_mask |= _PAGE_NX;
45 } else if (!strncmp(str, "off", 3)) {
47 __supported_pte_mask &= ~_PAGE_NX;
51 early_param("noexec", noexec_setup);
55 static void __init set_nx(void)
57 unsigned int v[4], l, h;
59 if (cpu_has_pae && (cpuid_eax(0x80000000) > 0x80000001)) {
60 cpuid(0x80000001, &v[0], &v[1], &v[2], &v[3]);
62 if ((v[3] & (1 << 20)) && !disable_nx) {
63 rdmsr(MSR_EFER, l, h);
65 wrmsr(MSR_EFER, l, h);
67 __supported_pte_mask |= _PAGE_NX;
72 static inline void set_nx(void)
78 void __cpuinit check_efer(void)
82 rdmsrl(MSR_EFER, efer);
83 if (!(efer & EFER_NX) || disable_nx)
84 __supported_pte_mask &= ~_PAGE_NX;
88 static void __init find_early_table_space(unsigned long end, int use_pse,
91 unsigned long puds, pmds, ptes, tables, start;
93 puds = (end + PUD_SIZE - 1) >> PUD_SHIFT;
94 tables = roundup(puds * sizeof(pud_t), PAGE_SIZE);
99 extra = end - ((end>>PUD_SHIFT) << PUD_SHIFT);
100 pmds = (extra + PMD_SIZE - 1) >> PMD_SHIFT;
102 pmds = (end + PMD_SIZE - 1) >> PMD_SHIFT;
104 tables += roundup(pmds * sizeof(pmd_t), PAGE_SIZE);
109 extra = end - ((end>>PMD_SHIFT) << PMD_SHIFT);
113 ptes = (extra + PAGE_SIZE - 1) >> PAGE_SHIFT;
115 ptes = (end + PAGE_SIZE - 1) >> PAGE_SHIFT;
117 tables += roundup(ptes * sizeof(pte_t), PAGE_SIZE);
121 tables += roundup(__end_of_fixed_addresses * sizeof(pte_t), PAGE_SIZE);
125 * RED-PEN putting page tables only on node 0 could
126 * cause a hotspot and fill up ZONE_DMA. The page tables
127 * need roughly 0.5KB per GB.
131 e820_table_start = find_e820_area(start, max_pfn_mapped<<PAGE_SHIFT,
133 #else /* CONFIG_X86_64 */
135 e820_table_start = find_e820_area(start, end, tables, PAGE_SIZE);
137 if (e820_table_start == -1UL)
138 panic("Cannot find space for the kernel page tables");
140 e820_table_start >>= PAGE_SHIFT;
141 e820_table_end = e820_table_start;
142 e820_table_top = e820_table_start + (tables >> PAGE_SHIFT);
144 printk(KERN_DEBUG "kernel direct mapping tables up to %lx @ %lx-%lx\n",
145 end, e820_table_start << PAGE_SHIFT, e820_table_top << PAGE_SHIFT);
151 unsigned page_size_mask;
155 #define NR_RANGE_MR 3
156 #else /* CONFIG_X86_64 */
157 #define NR_RANGE_MR 5
160 static int __meminit save_mr(struct map_range *mr, int nr_range,
161 unsigned long start_pfn, unsigned long end_pfn,
162 unsigned long page_size_mask)
164 if (start_pfn < end_pfn) {
165 if (nr_range >= NR_RANGE_MR)
166 panic("run out of range for init_memory_mapping\n");
167 mr[nr_range].start = start_pfn<<PAGE_SHIFT;
168 mr[nr_range].end = end_pfn<<PAGE_SHIFT;
169 mr[nr_range].page_size_mask = page_size_mask;
177 static void __init init_gbpages(void)
179 if (direct_gbpages && cpu_has_gbpages)
180 printk(KERN_INFO "Using GB pages for direct mapping\n");
185 static inline void init_gbpages(void)
191 * Setup the direct mapping of the physical memory at PAGE_OFFSET.
192 * This runs before bootmem is initialized and gets pages directly from
193 * the physical memory. To access them they are temporarily mapped.
195 unsigned long __init_refok init_memory_mapping(unsigned long start,
198 unsigned long page_size_mask = 0;
199 unsigned long start_pfn, end_pfn;
200 unsigned long ret = 0;
203 struct map_range mr[NR_RANGE_MR];
205 int use_pse, use_gbpages;
207 printk(KERN_INFO "init_memory_mapping: %016lx-%016lx\n", start, end);
212 #ifdef CONFIG_DEBUG_PAGEALLOC
214 * For CONFIG_DEBUG_PAGEALLOC, identity mapping will use small pages.
215 * This will simplify cpa(), which otherwise needs to support splitting
216 * large pages into small in interrupt context, etc.
218 use_pse = use_gbpages = 0;
220 use_pse = cpu_has_pse;
221 use_gbpages = direct_gbpages;
226 printk(KERN_INFO "NX (Execute Disable) protection: active\n");
228 /* Enable PSE if available */
230 set_in_cr4(X86_CR4_PSE);
232 /* Enable PGE if available */
234 set_in_cr4(X86_CR4_PGE);
235 __supported_pte_mask |= _PAGE_GLOBAL;
239 page_size_mask |= 1 << PG_LEVEL_1G;
241 page_size_mask |= 1 << PG_LEVEL_2M;
243 memset(mr, 0, sizeof(mr));
246 /* head if not big page alignment ? */
247 start_pfn = start >> PAGE_SHIFT;
248 pos = start_pfn << PAGE_SHIFT;
251 * Don't use a large page for the first 2/4MB of memory
252 * because there are often fixed size MTRRs in there
253 * and overlapping MTRRs into large pages can cause
257 end_pfn = 1<<(PMD_SHIFT - PAGE_SHIFT);
259 end_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
260 << (PMD_SHIFT - PAGE_SHIFT);
261 #else /* CONFIG_X86_64 */
262 end_pfn = ((pos + (PMD_SIZE - 1)) >> PMD_SHIFT)
263 << (PMD_SHIFT - PAGE_SHIFT);
265 if (end_pfn > (end >> PAGE_SHIFT))
266 end_pfn = end >> PAGE_SHIFT;
267 if (start_pfn < end_pfn) {
268 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
269 pos = end_pfn << PAGE_SHIFT;
272 /* big page (2M) range */
273 start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
274 << (PMD_SHIFT - PAGE_SHIFT);
276 end_pfn = (end>>PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
277 #else /* CONFIG_X86_64 */
278 end_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT)
279 << (PUD_SHIFT - PAGE_SHIFT);
280 if (end_pfn > ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT)))
281 end_pfn = ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT));
284 if (start_pfn < end_pfn) {
285 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
286 page_size_mask & (1<<PG_LEVEL_2M));
287 pos = end_pfn << PAGE_SHIFT;
291 /* big page (1G) range */
292 start_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT)
293 << (PUD_SHIFT - PAGE_SHIFT);
294 end_pfn = (end >> PUD_SHIFT) << (PUD_SHIFT - PAGE_SHIFT);
295 if (start_pfn < end_pfn) {
296 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
298 ((1<<PG_LEVEL_2M)|(1<<PG_LEVEL_1G)));
299 pos = end_pfn << PAGE_SHIFT;
302 /* tail is not big page (1G) alignment */
303 start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
304 << (PMD_SHIFT - PAGE_SHIFT);
305 end_pfn = (end >> PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
306 if (start_pfn < end_pfn) {
307 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
308 page_size_mask & (1<<PG_LEVEL_2M));
309 pos = end_pfn << PAGE_SHIFT;
313 /* tail is not big page (2M) alignment */
314 start_pfn = pos>>PAGE_SHIFT;
315 end_pfn = end>>PAGE_SHIFT;
316 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
318 /* try to merge same page size and continuous */
319 for (i = 0; nr_range > 1 && i < nr_range - 1; i++) {
320 unsigned long old_start;
321 if (mr[i].end != mr[i+1].start ||
322 mr[i].page_size_mask != mr[i+1].page_size_mask)
325 old_start = mr[i].start;
326 memmove(&mr[i], &mr[i+1],
327 (nr_range - 1 - i) * sizeof(struct map_range));
328 mr[i--].start = old_start;
332 for (i = 0; i < nr_range; i++)
333 printk(KERN_DEBUG " %010lx - %010lx page %s\n",
334 mr[i].start, mr[i].end,
335 (mr[i].page_size_mask & (1<<PG_LEVEL_1G))?"1G":(
336 (mr[i].page_size_mask & (1<<PG_LEVEL_2M))?"2M":"4k"));
339 * Find space for the kernel direct mapping tables.
341 * Later we should allocate these tables in the local node of the
342 * memory mapped. Unfortunately this is done currently before the
343 * nodes are discovered.
346 find_early_table_space(end, use_pse, use_gbpages);
349 for (i = 0; i < nr_range; i++)
350 kernel_physical_mapping_init(mr[i].start, mr[i].end,
351 mr[i].page_size_mask);
353 #else /* CONFIG_X86_64 */
354 for (i = 0; i < nr_range; i++)
355 ret = kernel_physical_mapping_init(mr[i].start, mr[i].end,
356 mr[i].page_size_mask);
360 early_ioremap_page_table_range_init();
362 load_cr3(swapper_pg_dir);
367 mmu_cr4_features = read_cr4();
371 if (!after_bootmem && e820_table_end > e820_table_start)
372 reserve_early(e820_table_start << PAGE_SHIFT,
373 e820_table_end << PAGE_SHIFT, "PGTABLE");
376 early_memtest(start, end);
378 return ret >> PAGE_SHIFT;
383 * devmem_is_allowed() checks to see if /dev/mem access to a certain address
384 * is valid. The argument is a physical page number.
387 * On x86, access has to be given to the first megabyte of ram because that area
388 * contains bios code and data regions used by X and dosemu and similar apps.
389 * Access has to be given to non-kernel-ram areas as well, these contain the PCI
390 * mmio resources as well as potential bios/acpi data regions.
392 int devmem_is_allowed(unsigned long pagenr)
396 if (iomem_is_exclusive(pagenr << PAGE_SHIFT))
398 if (!page_is_ram(pagenr))
403 void free_init_pages(char *what, unsigned long begin, unsigned long end)
405 unsigned long addr = begin;
411 * If debugging page accesses then do not free this memory but
412 * mark them not present - any buggy init-section access will
413 * create a kernel page fault:
415 #ifdef CONFIG_DEBUG_PAGEALLOC
416 printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n",
417 begin, PAGE_ALIGN(end));
418 set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
421 * We just marked the kernel text read only above, now that
422 * we are going to free part of that, we need to make that
425 set_memory_rw(begin, (end - begin) >> PAGE_SHIFT);
427 printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
429 for (; addr < end; addr += PAGE_SIZE) {
430 ClearPageReserved(virt_to_page(addr));
431 init_page_count(virt_to_page(addr));
432 memset((void *)(addr & ~(PAGE_SIZE-1)),
433 POISON_FREE_INITMEM, PAGE_SIZE);
440 void free_initmem(void)
442 free_init_pages("unused kernel memory",
443 (unsigned long)(&__init_begin),
444 (unsigned long)(&__init_end));
447 #ifdef CONFIG_BLK_DEV_INITRD
448 void free_initrd_mem(unsigned long start, unsigned long end)
450 free_init_pages("initrd memory", start, end);