2 #include <linux/initrd.h>
3 #include <linux/ioport.h>
4 #include <linux/swap.h>
5 #include <linux/memblock.h>
7 #include <asm/cacheflush.h>
11 #include <asm/page_types.h>
12 #include <asm/sections.h>
13 #include <asm/setup.h>
14 #include <asm/system.h>
15 #include <asm/tlbflush.h>
17 #include <asm/proto.h>
19 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
21 unsigned long __initdata e820_table_start;
22 unsigned long __meminitdata e820_table_end;
23 unsigned long __meminitdata e820_table_top;
28 #ifdef CONFIG_DIRECT_GBPAGES
33 static void __init find_early_table_space(unsigned long end, int use_pse,
36 unsigned long puds, pmds, ptes, tables, start;
39 puds = (end + PUD_SIZE - 1) >> PUD_SHIFT;
40 tables = roundup(puds * sizeof(pud_t), PAGE_SIZE);
45 extra = end - ((end>>PUD_SHIFT) << PUD_SHIFT);
46 pmds = (extra + PMD_SIZE - 1) >> PMD_SHIFT;
48 pmds = (end + PMD_SIZE - 1) >> PMD_SHIFT;
50 tables += roundup(pmds * sizeof(pmd_t), PAGE_SIZE);
55 extra = end - ((end>>PMD_SHIFT) << PMD_SHIFT);
59 ptes = (extra + PAGE_SIZE - 1) >> PAGE_SHIFT;
61 ptes = (end + PAGE_SIZE - 1) >> PAGE_SHIFT;
63 tables += roundup(ptes * sizeof(pte_t), PAGE_SIZE);
67 tables += roundup(__end_of_fixed_addresses * sizeof(pte_t), PAGE_SIZE);
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.
80 base = memblock_find_in_range(start, max_pfn_mapped<<PAGE_SHIFT,
82 if (base == MEMBLOCK_ERROR)
83 panic("Cannot find space for the kernel page tables");
85 e820_table_start = base >> PAGE_SHIFT;
86 e820_table_end = e820_table_start;
87 e820_table_top = e820_table_start + (tables >> PAGE_SHIFT);
89 printk(KERN_DEBUG "kernel direct mapping tables up to %lx @ %lx-%lx\n",
90 end, e820_table_start << PAGE_SHIFT, e820_table_top << PAGE_SHIFT);
96 unsigned page_size_mask;
100 #define NR_RANGE_MR 3
101 #else /* CONFIG_X86_64 */
102 #define NR_RANGE_MR 5
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)
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;
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.
126 unsigned long __init_refok init_memory_mapping(unsigned long start,
129 unsigned long page_size_mask = 0;
130 unsigned long start_pfn, end_pfn;
131 unsigned long ret = 0;
134 struct map_range mr[NR_RANGE_MR];
136 int use_pse, use_gbpages;
138 printk(KERN_INFO "init_memory_mapping: %016lx-%016lx\n", start, end);
140 #if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_KMEMCHECK)
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.
146 use_pse = use_gbpages = 0;
148 use_pse = cpu_has_pse;
149 use_gbpages = direct_gbpages;
152 /* Enable PSE if available */
154 set_in_cr4(X86_CR4_PSE);
156 /* Enable PGE if available */
158 set_in_cr4(X86_CR4_PGE);
159 __supported_pte_mask |= _PAGE_GLOBAL;
163 page_size_mask |= 1 << PG_LEVEL_1G;
165 page_size_mask |= 1 << PG_LEVEL_2M;
167 memset(mr, 0, sizeof(mr));
170 /* head if not big page alignment ? */
171 start_pfn = start >> PAGE_SHIFT;
172 pos = start_pfn << PAGE_SHIFT;
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
181 end_pfn = 1<<(PMD_SHIFT - PAGE_SHIFT);
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);
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;
196 /* big page (2M) range */
197 start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
198 << (PMD_SHIFT - PAGE_SHIFT);
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));
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;
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,
222 ((1<<PG_LEVEL_2M)|(1<<PG_LEVEL_1G)));
223 pos = end_pfn << PAGE_SHIFT;
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;
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);
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)
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;
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"));
263 * Find space for the kernel direct mapping tables.
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.
270 find_early_table_space(end, use_pse, use_gbpages);
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);
277 early_ioremap_page_table_range_init();
279 load_cr3(swapper_pg_dir);
283 if (!after_bootmem && !start) {
287 mmu_cr4_features = read_cr4();
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.
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))
303 if (!after_bootmem && e820_table_end > e820_table_start)
304 memblock_x86_reserve_range(e820_table_start << PAGE_SHIFT,
305 e820_table_end << PAGE_SHIFT, "PGTABLE");
308 early_memtest(start, end);
310 return ret >> PAGE_SHIFT;
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.
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.
324 int devmem_is_allowed(unsigned long pagenr)
328 if (iomem_is_exclusive(pagenr << PAGE_SHIFT))
330 if (!page_is_ram(pagenr))
335 void free_init_pages(char *what, unsigned long begin, unsigned long end)
338 unsigned long begin_aligned, end_aligned;
340 /* Make sure boundaries are page aligned */
341 begin_aligned = PAGE_ALIGN(begin);
342 end_aligned = end & PAGE_MASK;
344 if (WARN_ON(begin_aligned != begin || end_aligned != end)) {
345 begin = begin_aligned;
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:
359 #ifdef CONFIG_DEBUG_PAGEALLOC
360 printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n",
362 set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
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
369 set_memory_rw(begin, (end - begin) >> PAGE_SHIFT);
371 printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
373 for (; addr < end; addr += PAGE_SIZE) {
374 ClearPageReserved(virt_to_page(addr));
375 init_page_count(virt_to_page(addr));
376 memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
383 void free_initmem(void)
385 free_init_pages("unused kernel memory",
386 (unsigned long)(&__init_begin),
387 (unsigned long)(&__init_end));
390 #ifdef CONFIG_BLK_DEV_INITRD
391 void free_initrd_mem(unsigned long start, unsigned long end)
394 * end could be not aligned, and We can not align that,
395 * decompresser could be confused by aligned initrd_end
396 * We already reserve the end partial page before in
397 * - i386_start_kernel()
398 * - x86_64_start_kernel()
399 * - relocate_initrd()
400 * So here We can do PAGE_ALIGN() safely to get partial page to be freed
402 free_init_pages("initrd memory", start, PAGE_ALIGN(end));