2 #include <linux/initrd.h>
3 #include <linux/ioport.h>
4 #include <linux/swap.h>
5 #include <linux/memblock.h>
6 #include <linux/bootmem.h>
8 #include <asm/cacheflush.h>
12 #include <asm/page_types.h>
13 #include <asm/sections.h>
14 #include <asm/setup.h>
15 #include <asm/system.h>
16 #include <asm/tlbflush.h>
18 #include <asm/proto.h>
19 #include <asm/dma.h> /* for MAX_DMA_PFN */
21 unsigned long __meminitdata pgt_buf_start;
22 unsigned long __meminitdata pgt_buf_end;
23 unsigned long __meminitdata pgt_buf_top;
27 #if !defined(CONFIG_XEN)
29 #ifdef CONFIG_DIRECT_GBPAGES
33 #elif defined(CONFIG_X86_32)
34 #define direct_gbpages 0
35 extern unsigned long extend_init_mapping(unsigned long tables_space);
37 extern void xen_finish_init_mapping(void);
40 static void __init find_early_table_space(unsigned long end, int use_pse,
43 unsigned long puds, pmds, ptes, tables;
45 puds = (end + PUD_SIZE - 1) >> PUD_SHIFT;
46 tables = roundup(puds * sizeof(pud_t), PAGE_SIZE);
51 extra = end - ((end>>PUD_SHIFT) << PUD_SHIFT);
52 pmds = (extra + PMD_SIZE - 1) >> PMD_SHIFT;
54 pmds = (end + PMD_SIZE - 1) >> PMD_SHIFT;
56 tables += roundup(pmds * sizeof(pmd_t), PAGE_SIZE);
61 extra = end - ((end>>PMD_SHIFT) << PMD_SHIFT);
65 ptes = (extra + PAGE_SIZE - 1) >> PAGE_SHIFT;
67 ptes = (end + PAGE_SIZE - 1) >> PAGE_SHIFT;
69 tables += roundup(ptes * sizeof(pte_t), PAGE_SIZE);
73 tables += roundup(__end_of_fixed_addresses * sizeof(pte_t), PAGE_SIZE);
75 pgt_buf_start = extend_init_mapping(tables);
76 pgt_buf_end = pgt_buf_start;
77 #else /* CONFIG_X86_64 */
79 pgt_buf_start = (__pa(xen_start_info->pt_base) >> PAGE_SHIFT) +
80 xen_start_info->nr_pt_frames;
81 pgt_buf_end = pgt_buf_start;
84 * [table_start, table_top) gets passed to reserve_early(),
85 * so we must not use table_end here, despite continuing
86 * to allocate from there. table_end possibly being below
87 * table_start is otoh not a problem.
89 pgt_buf_start = pgt_buf_top;
92 if (pgt_buf_start == -1UL)
93 panic("Cannot find space for the kernel page tables");
95 pgt_buf_top = pgt_buf_start + (tables >> PAGE_SHIFT);
97 printk(KERN_DEBUG "kernel direct mapping tables up to %lx @ %lx-%lx\n",
98 end, pgt_buf_start << PAGE_SHIFT, pgt_buf_top << PAGE_SHIFT);
101 void __init xen_pagetable_reserve(u64 start, u64 end)
104 memblock_reserve(start, end - start);
110 unsigned page_size_mask;
114 #define NR_RANGE_MR 3
115 #else /* CONFIG_X86_64 */
116 #define NR_RANGE_MR 5
119 static int __meminit save_mr(struct map_range *mr, int nr_range,
120 unsigned long start_pfn, unsigned long end_pfn,
121 unsigned long page_size_mask)
123 if (start_pfn < end_pfn) {
124 if (nr_range >= NR_RANGE_MR)
125 panic("run out of range for init_memory_mapping\n");
126 mr[nr_range].start = start_pfn<<PAGE_SHIFT;
127 mr[nr_range].end = end_pfn<<PAGE_SHIFT;
128 mr[nr_range].page_size_mask = page_size_mask;
136 * Setup the direct mapping of the physical memory at PAGE_OFFSET.
137 * This runs before bootmem is initialized and gets pages directly from
138 * the physical memory. To access them they are temporarily mapped.
140 unsigned long __init_refok init_memory_mapping(unsigned long start,
143 unsigned long page_size_mask = 0;
144 unsigned long start_pfn, end_pfn;
145 unsigned long ret = 0;
148 struct map_range mr[NR_RANGE_MR];
150 int use_pse, use_gbpages;
152 printk(KERN_INFO "init_memory_mapping: %016lx-%016lx\n", start, end);
154 #if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_KMEMCHECK)
156 * For CONFIG_DEBUG_PAGEALLOC, identity mapping will use small pages.
157 * This will simplify cpa(), which otherwise needs to support splitting
158 * large pages into small in interrupt context, etc.
160 use_pse = use_gbpages = 0;
162 use_pse = cpu_has_pse;
163 use_gbpages = direct_gbpages;
166 /* Enable PSE if available */
168 set_in_cr4(X86_CR4_PSE);
170 /* Enable PGE if available */
172 set_in_cr4(X86_CR4_PGE);
173 __supported_pte_mask |= _PAGE_GLOBAL;
177 page_size_mask |= 1 << PG_LEVEL_1G;
179 page_size_mask |= 1 << PG_LEVEL_2M;
181 memset(mr, 0, sizeof(mr));
184 /* head if not big page alignment ? */
185 start_pfn = start >> PAGE_SHIFT;
186 pos = start_pfn << PAGE_SHIFT;
189 * Don't use a large page for the first 2/4MB of memory
190 * because there are often fixed size MTRRs in there
191 * and overlapping MTRRs into large pages can cause
195 end_pfn = 1<<(PMD_SHIFT - PAGE_SHIFT);
197 end_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
198 << (PMD_SHIFT - PAGE_SHIFT);
199 #else /* CONFIG_X86_64 */
200 end_pfn = ((pos + (PMD_SIZE - 1)) >> PMD_SHIFT)
201 << (PMD_SHIFT - PAGE_SHIFT);
203 if (end_pfn > (end >> PAGE_SHIFT))
204 end_pfn = end >> PAGE_SHIFT;
205 if (start_pfn < end_pfn) {
206 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
207 pos = end_pfn << PAGE_SHIFT;
210 /* big page (2M) range */
211 start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
212 << (PMD_SHIFT - PAGE_SHIFT);
214 end_pfn = (end>>PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
215 #else /* CONFIG_X86_64 */
216 end_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT)
217 << (PUD_SHIFT - PAGE_SHIFT);
218 if (end_pfn > ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT)))
219 end_pfn = ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT));
222 if (start_pfn < end_pfn) {
223 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
224 page_size_mask & (1<<PG_LEVEL_2M));
225 pos = end_pfn << PAGE_SHIFT;
229 /* big page (1G) range */
230 start_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT)
231 << (PUD_SHIFT - PAGE_SHIFT);
232 end_pfn = (end >> PUD_SHIFT) << (PUD_SHIFT - PAGE_SHIFT);
233 if (start_pfn < end_pfn) {
234 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
236 ((1<<PG_LEVEL_2M)|(1<<PG_LEVEL_1G)));
237 pos = end_pfn << PAGE_SHIFT;
240 /* tail is not big page (1G) alignment */
241 start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
242 << (PMD_SHIFT - PAGE_SHIFT);
243 end_pfn = (end >> PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
244 if (start_pfn < end_pfn) {
245 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
246 page_size_mask & (1<<PG_LEVEL_2M));
247 pos = end_pfn << PAGE_SHIFT;
251 /* tail is not big page (2M) alignment */
252 start_pfn = pos>>PAGE_SHIFT;
253 end_pfn = end>>PAGE_SHIFT;
254 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
256 /* try to merge same page size and continuous */
257 for (i = 0; nr_range > 1 && i < nr_range - 1; i++) {
258 unsigned long old_start;
259 if (mr[i].end != mr[i+1].start ||
260 mr[i].page_size_mask != mr[i+1].page_size_mask)
263 old_start = mr[i].start;
264 memmove(&mr[i], &mr[i+1],
265 (nr_range - 1 - i) * sizeof(struct map_range));
266 mr[i--].start = old_start;
270 for (i = 0; i < nr_range; i++)
271 printk(KERN_DEBUG " %010lx - %010lx page %s\n",
272 mr[i].start, mr[i].end,
273 (mr[i].page_size_mask & (1<<PG_LEVEL_1G))?"1G":(
274 (mr[i].page_size_mask & (1<<PG_LEVEL_2M))?"2M":"4k"));
277 * Find space for the kernel direct mapping tables.
279 * Later we should allocate these tables in the local node of the
280 * memory mapped. Unfortunately this is done currently before the
281 * nodes are discovered.
284 find_early_table_space(end, use_pse, use_gbpages);
287 #define addr_to_page(addr) \
289 ((mfn_to_pfn(((addr) & PHYSICAL_PAGE_MASK) >> PAGE_SHIFT) \
290 << PAGE_SHIFT) + __START_KERNEL_map))
293 unsigned long addr, va = __START_KERNEL_map;
294 unsigned long *page = (unsigned long *)init_level4_pgt;
296 /* Kill mapping of memory below _text. */
297 while (va < (unsigned long)&_text) {
298 if (HYPERVISOR_update_va_mapping(va, __pte_ma(0), 0))
303 /* Blow away any spurious initial mappings. */
304 va = __START_KERNEL_map + (pgt_buf_start << PAGE_SHIFT);
306 addr = page[pgd_index(va)];
307 page = addr_to_page(addr);
308 addr = page[pud_index(va)];
309 page = addr_to_page(addr);
310 while (pmd_index(va) | pte_index(va)) {
311 if (pmd_none(*(pmd_t *)&page[pmd_index(va)]))
313 if (HYPERVISOR_update_va_mapping(va, __pte_ma(0), 0))
321 for (i = 0; i < nr_range; i++)
322 ret = kernel_physical_mapping_init(mr[i].start, mr[i].end,
323 mr[i].page_size_mask);
326 early_ioremap_page_table_range_init();
330 BUG_ON(pgt_buf_end > pgt_buf_top);
332 xen_finish_init_mapping();
335 if (pgt_buf_end < pgt_buf_top)
336 /* Disable the 'table_end' allocator. */
337 pgt_buf_top = pgt_buf_end;
342 * Reserve the kernel pagetable pages we used (pgt_buf_start -
343 * pgt_buf_end) and free the other ones (pgt_buf_end - pgt_buf_top)
344 * so that they can be reused for other purposes.
346 * On native it just means calling memblock_reserve, on Xen it also
347 * means marking RW the pagetable pages that we allocated before
348 * but that haven't been used.
350 * In fact on xen we mark RO the whole range pgt_buf_start -
351 * pgt_buf_top, because we have to make sure that when
352 * init_memory_mapping reaches the pagetable pages area, it maps
353 * RO all the pagetable pages, including the ones that are beyond
354 * pgt_buf_end at that time.
356 if (!after_bootmem && pgt_buf_top > pgt_buf_start) {
358 reserve_pgtable_low();
360 x86_init.mapping.pagetable_reserve(PFN_PHYS(pgt_buf_start),
361 PFN_PHYS(pgt_buf_top));
365 early_memtest(start, end);
367 return ret >> PAGE_SHIFT;
372 * devmem_is_allowed() checks to see if /dev/mem access to a certain address
373 * is valid. The argument is a physical page number.
376 * On x86, access has to be given to the first megabyte of ram because that area
377 * contains bios code and data regions used by X and dosemu and similar apps.
378 * Access has to be given to non-kernel-ram areas as well, these contain the PCI
379 * mmio resources as well as potential bios/acpi data regions.
381 int devmem_is_allowed(unsigned long pagenr)
385 if (iomem_is_exclusive(pagenr << PAGE_SHIFT))
387 if (mfn_to_local_pfn(pagenr) >= max_pfn)
392 void free_init_pages(char *what, unsigned long begin, unsigned long end)
395 unsigned long begin_aligned, end_aligned;
397 /* Make sure boundaries are page aligned */
398 begin_aligned = PAGE_ALIGN(begin);
399 end_aligned = end & PAGE_MASK;
401 if (WARN_ON(begin_aligned != begin || end_aligned != end)) {
402 begin = begin_aligned;
412 * If debugging page accesses then do not free this memory but
413 * mark them not present - any buggy init-section access will
414 * create a kernel page fault:
416 #ifdef CONFIG_DEBUG_PAGEALLOC
417 printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n",
419 set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
422 * We just marked the kernel text read only above, now that
423 * we are going to free part of that, we need to make that
424 * writeable and non-executable first.
426 set_memory_nx(begin, (end - begin) >> PAGE_SHIFT);
427 set_memory_rw(begin, (end - begin) >> PAGE_SHIFT);
429 printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
431 for (; addr < end; addr += PAGE_SIZE) {
432 ClearPageReserved(virt_to_page(addr));
433 init_page_count(virt_to_page(addr));
434 memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
436 if (addr >= __START_KERNEL_map) {
437 /* make_readonly() reports all kernel addresses. */
438 if (HYPERVISOR_update_va_mapping((unsigned long)__va(__pa(addr)),
439 pfn_pte(__pa(addr) >> PAGE_SHIFT,
443 if (HYPERVISOR_update_va_mapping(addr, __pte(0), 0))
453 void free_initmem(void)
455 free_init_pages("unused kernel memory",
456 (unsigned long)(&__init_begin),
457 (unsigned long)(&__init_end));
460 #ifdef CONFIG_BLK_DEV_INITRD
461 void free_initrd_mem(unsigned long start, unsigned long end)
464 * end could be not aligned, and We can not align that,
465 * decompresser could be confused by aligned initrd_end
466 * We already reserve the end partial page before in
467 * - i386_start_kernel()
468 * - x86_64_start_kernel()
469 * - relocate_initrd()
470 * So here We can do PAGE_ALIGN() safely to get partial page to be freed
472 #ifdef CONFIG_ACPI_INITRD_TABLE_OVERRIDE
473 if (acpi_initrd_offset)
474 free_init_pages("initrd memory", start - acpi_initrd_offset,
478 free_init_pages("initrd memory", start, PAGE_ALIGN(end));
482 void __init zone_sizes_init(void)
484 unsigned long max_zone_pfns[MAX_NR_ZONES];
486 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
488 #ifdef CONFIG_ZONE_DMA
489 max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
491 #ifdef CONFIG_ZONE_DMA32
492 max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
494 max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
495 #ifdef CONFIG_HIGHMEM
496 max_zone_pfns[ZONE_HIGHMEM] = max_pfn;
499 free_area_init_nodes(max_zone_pfns);