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/tlbflush.h>
17 #include <asm/proto.h>
18 #include <asm/dma.h> /* for MAX_DMA_PFN */
20 unsigned long __meminitdata pgt_buf_start;
21 unsigned long __meminitdata pgt_buf_end;
22 unsigned long __meminitdata pgt_buf_top;
26 #if !defined(CONFIG_XEN)
28 #ifdef CONFIG_DIRECT_GBPAGES
32 #elif defined(CONFIG_X86_32)
33 #define direct_gbpages 0
34 extern unsigned long extend_init_mapping(unsigned long tables_space);
36 extern void xen_finish_init_mapping(void);
39 static void __init find_early_table_space(unsigned long end, int use_pse,
42 unsigned long puds, pmds, ptes, tables;
44 puds = (end + PUD_SIZE - 1) >> PUD_SHIFT;
45 tables = roundup(puds * sizeof(pud_t), PAGE_SIZE);
50 extra = end - ((end>>PUD_SHIFT) << PUD_SHIFT);
51 pmds = (extra + PMD_SIZE - 1) >> PMD_SHIFT;
53 pmds = (end + PMD_SIZE - 1) >> PMD_SHIFT;
55 tables += roundup(pmds * sizeof(pmd_t), PAGE_SIZE);
60 extra = end - ((end>>PMD_SHIFT) << PMD_SHIFT);
64 ptes = (extra + PAGE_SIZE - 1) >> PAGE_SHIFT;
66 ptes = (end + PAGE_SIZE - 1) >> PAGE_SHIFT;
68 tables += roundup(ptes * sizeof(pte_t), PAGE_SIZE);
72 tables += roundup(__end_of_fixed_addresses * sizeof(pte_t), PAGE_SIZE);
74 pgt_buf_start = extend_init_mapping(tables);
75 pgt_buf_end = pgt_buf_start;
76 #else /* CONFIG_X86_64 */
78 pgt_buf_start = (__pa(xen_start_info->pt_base) >> PAGE_SHIFT) +
79 xen_start_info->nr_pt_frames;
80 pgt_buf_end = pgt_buf_start;
83 * [table_start, table_top) gets passed to reserve_early(),
84 * so we must not use table_end here, despite continuing
85 * to allocate from there. table_end possibly being below
86 * table_start is otoh not a problem.
88 pgt_buf_start = pgt_buf_top;
91 if (pgt_buf_start == -1UL)
92 panic("Cannot find space for the kernel page tables");
94 pgt_buf_top = pgt_buf_start + (tables >> PAGE_SHIFT);
96 printk(KERN_DEBUG "kernel direct mapping tables up to %lx @ %lx-%lx\n",
97 end, pgt_buf_start << PAGE_SHIFT, pgt_buf_top << PAGE_SHIFT);
100 void __init xen_pagetable_reserve(u64 start, u64 end)
103 memblock_reserve(start, end - start);
109 unsigned page_size_mask;
113 #define NR_RANGE_MR 3
114 #else /* CONFIG_X86_64 */
115 #define NR_RANGE_MR 5
118 static int __meminit save_mr(struct map_range *mr, int nr_range,
119 unsigned long start_pfn, unsigned long end_pfn,
120 unsigned long page_size_mask)
122 if (start_pfn < end_pfn) {
123 if (nr_range >= NR_RANGE_MR)
124 panic("run out of range for init_memory_mapping\n");
125 mr[nr_range].start = start_pfn<<PAGE_SHIFT;
126 mr[nr_range].end = end_pfn<<PAGE_SHIFT;
127 mr[nr_range].page_size_mask = page_size_mask;
135 * Setup the direct mapping of the physical memory at PAGE_OFFSET.
136 * This runs before bootmem is initialized and gets pages directly from
137 * the physical memory. To access them they are temporarily mapped.
139 unsigned long __init_refok init_memory_mapping(unsigned long start,
142 unsigned long page_size_mask = 0;
143 unsigned long start_pfn, end_pfn;
144 unsigned long ret = 0;
147 struct map_range mr[NR_RANGE_MR];
149 int use_pse, use_gbpages;
151 printk(KERN_INFO "init_memory_mapping: %016lx-%016lx\n", start, end);
153 #if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_KMEMCHECK)
155 * For CONFIG_DEBUG_PAGEALLOC, identity mapping will use small pages.
156 * This will simplify cpa(), which otherwise needs to support splitting
157 * large pages into small in interrupt context, etc.
159 use_pse = use_gbpages = 0;
161 use_pse = cpu_has_pse;
162 use_gbpages = direct_gbpages;
165 /* Enable PSE if available */
167 set_in_cr4(X86_CR4_PSE);
169 /* Enable PGE if available */
171 set_in_cr4(X86_CR4_PGE);
172 __supported_pte_mask |= _PAGE_GLOBAL;
176 page_size_mask |= 1 << PG_LEVEL_1G;
178 page_size_mask |= 1 << PG_LEVEL_2M;
180 memset(mr, 0, sizeof(mr));
183 /* head if not big page alignment ? */
184 start_pfn = start >> PAGE_SHIFT;
185 pos = start_pfn << PAGE_SHIFT;
188 * Don't use a large page for the first 2/4MB of memory
189 * because there are often fixed size MTRRs in there
190 * and overlapping MTRRs into large pages can cause
194 end_pfn = 1<<(PMD_SHIFT - PAGE_SHIFT);
196 end_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
197 << (PMD_SHIFT - PAGE_SHIFT);
198 #else /* CONFIG_X86_64 */
199 end_pfn = ((pos + (PMD_SIZE - 1)) >> PMD_SHIFT)
200 << (PMD_SHIFT - PAGE_SHIFT);
202 if (end_pfn > (end >> PAGE_SHIFT))
203 end_pfn = end >> PAGE_SHIFT;
204 if (start_pfn < end_pfn) {
205 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
206 pos = end_pfn << PAGE_SHIFT;
209 /* big page (2M) range */
210 start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
211 << (PMD_SHIFT - PAGE_SHIFT);
213 end_pfn = (end>>PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
214 #else /* CONFIG_X86_64 */
215 end_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT)
216 << (PUD_SHIFT - PAGE_SHIFT);
217 if (end_pfn > ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT)))
218 end_pfn = ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT));
221 if (start_pfn < end_pfn) {
222 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
223 page_size_mask & (1<<PG_LEVEL_2M));
224 pos = end_pfn << PAGE_SHIFT;
228 /* big page (1G) range */
229 start_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT)
230 << (PUD_SHIFT - PAGE_SHIFT);
231 end_pfn = (end >> PUD_SHIFT) << (PUD_SHIFT - PAGE_SHIFT);
232 if (start_pfn < end_pfn) {
233 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
235 ((1<<PG_LEVEL_2M)|(1<<PG_LEVEL_1G)));
236 pos = end_pfn << PAGE_SHIFT;
239 /* tail is not big page (1G) alignment */
240 start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
241 << (PMD_SHIFT - PAGE_SHIFT);
242 end_pfn = (end >> PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
243 if (start_pfn < end_pfn) {
244 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
245 page_size_mask & (1<<PG_LEVEL_2M));
246 pos = end_pfn << PAGE_SHIFT;
250 /* tail is not big page (2M) alignment */
251 start_pfn = pos>>PAGE_SHIFT;
252 end_pfn = end>>PAGE_SHIFT;
253 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
255 /* try to merge same page size and continuous */
256 for (i = 0; nr_range > 1 && i < nr_range - 1; i++) {
257 unsigned long old_start;
258 if (mr[i].end != mr[i+1].start ||
259 mr[i].page_size_mask != mr[i+1].page_size_mask)
262 old_start = mr[i].start;
263 memmove(&mr[i], &mr[i+1],
264 (nr_range - 1 - i) * sizeof(struct map_range));
265 mr[i--].start = old_start;
269 for (i = 0; i < nr_range; i++)
270 printk(KERN_DEBUG " %010lx - %010lx page %s\n",
271 mr[i].start, mr[i].end,
272 (mr[i].page_size_mask & (1<<PG_LEVEL_1G))?"1G":(
273 (mr[i].page_size_mask & (1<<PG_LEVEL_2M))?"2M":"4k"));
276 * Find space for the kernel direct mapping tables.
278 * Later we should allocate these tables in the local node of the
279 * memory mapped. Unfortunately this is done currently before the
280 * nodes are discovered.
283 find_early_table_space(end, use_pse, use_gbpages);
286 #define addr_to_page(addr) \
288 ((mfn_to_pfn(((addr) & PHYSICAL_PAGE_MASK) >> PAGE_SHIFT) \
289 << PAGE_SHIFT) + __START_KERNEL_map))
292 unsigned long addr, va = __START_KERNEL_map;
293 unsigned long *page = (unsigned long *)init_level4_pgt;
295 /* Kill mapping of memory below _text. */
296 while (va < (unsigned long)&_text) {
297 if (HYPERVISOR_update_va_mapping(va, __pte_ma(0), 0))
302 /* Blow away any spurious initial mappings. */
303 va = __START_KERNEL_map + (pgt_buf_start << PAGE_SHIFT);
305 addr = page[pgd_index(va)];
306 page = addr_to_page(addr);
307 addr = page[pud_index(va)];
308 page = addr_to_page(addr);
309 while (pmd_index(va) | pte_index(va)) {
310 if (pmd_none(*(pmd_t *)&page[pmd_index(va)]))
312 if (HYPERVISOR_update_va_mapping(va, __pte_ma(0), 0))
320 for (i = 0; i < nr_range; i++)
321 ret = kernel_physical_mapping_init(mr[i].start, mr[i].end,
322 mr[i].page_size_mask);
325 early_ioremap_page_table_range_init();
329 BUG_ON(pgt_buf_end > pgt_buf_top);
331 xen_finish_init_mapping();
334 if (pgt_buf_end < pgt_buf_top)
335 /* Disable the 'table_end' allocator. */
336 pgt_buf_top = pgt_buf_end;
341 * Reserve the kernel pagetable pages we used (pgt_buf_start -
342 * pgt_buf_end) and free the other ones (pgt_buf_end - pgt_buf_top)
343 * so that they can be reused for other purposes.
345 * On native it just means calling memblock_reserve, on Xen it also
346 * means marking RW the pagetable pages that we allocated before
347 * but that haven't been used.
349 * In fact on xen we mark RO the whole range pgt_buf_start -
350 * pgt_buf_top, because we have to make sure that when
351 * init_memory_mapping reaches the pagetable pages area, it maps
352 * RO all the pagetable pages, including the ones that are beyond
353 * pgt_buf_end at that time.
355 if (!after_bootmem && pgt_buf_top > pgt_buf_start) {
357 reserve_pgtable_low();
359 x86_init.mapping.pagetable_reserve(PFN_PHYS(pgt_buf_start),
360 PFN_PHYS(pgt_buf_top));
364 early_memtest(start, end);
366 return ret >> PAGE_SHIFT;
371 * devmem_is_allowed() checks to see if /dev/mem access to a certain address
372 * is valid. The argument is a physical page number.
375 * On x86, access has to be given to the first megabyte of ram because that area
376 * contains bios code and data regions used by X and dosemu and similar apps.
377 * Access has to be given to non-kernel-ram areas as well, these contain the PCI
378 * mmio resources as well as potential bios/acpi data regions.
380 int devmem_is_allowed(unsigned long pagenr)
384 if (iomem_is_exclusive(pagenr << PAGE_SHIFT))
386 if (mfn_to_local_pfn(pagenr) >= max_pfn)
391 void free_init_pages(char *what, unsigned long begin, unsigned long end)
394 unsigned long begin_aligned, end_aligned;
396 /* Make sure boundaries are page aligned */
397 begin_aligned = PAGE_ALIGN(begin);
398 end_aligned = end & PAGE_MASK;
400 if (WARN_ON(begin_aligned != begin || end_aligned != end)) {
401 begin = begin_aligned;
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",
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
423 * writeable and non-executable first.
425 set_memory_nx(begin, (end - begin) >> PAGE_SHIFT);
426 set_memory_rw(begin, (end - begin) >> PAGE_SHIFT);
428 printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
430 for (; addr < end; addr += PAGE_SIZE) {
431 ClearPageReserved(virt_to_page(addr));
432 init_page_count(virt_to_page(addr));
433 memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
435 if (addr >= __START_KERNEL_map) {
436 /* make_readonly() reports all kernel addresses. */
437 if (HYPERVISOR_update_va_mapping((unsigned long)__va(__pa(addr)),
438 pfn_pte(__pa(addr) >> PAGE_SHIFT,
442 if (HYPERVISOR_update_va_mapping(addr, __pte(0), 0))
452 void free_initmem(void)
454 free_init_pages("unused kernel memory",
455 (unsigned long)(&__init_begin),
456 (unsigned long)(&__init_end));
459 #ifdef CONFIG_BLK_DEV_INITRD
460 void free_initrd_mem(unsigned long start, unsigned long end)
463 * end could be not aligned, and We can not align that,
464 * decompresser could be confused by aligned initrd_end
465 * We already reserve the end partial page before in
466 * - i386_start_kernel()
467 * - x86_64_start_kernel()
468 * - relocate_initrd()
469 * So here We can do PAGE_ALIGN() safely to get partial page to be freed
471 #ifdef CONFIG_ACPI_INITRD_TABLE_OVERRIDE
472 if (acpi_initrd_offset)
473 free_init_pages("initrd memory", start - acpi_initrd_offset,
477 free_init_pages("initrd memory", start, PAGE_ALIGN(end));
481 void __init zone_sizes_init(void)
483 unsigned long max_zone_pfns[MAX_NR_ZONES];
485 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
487 #ifdef CONFIG_ZONE_DMA
488 max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
490 #ifdef CONFIG_ZONE_DMA32
491 max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
493 max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
494 #ifdef CONFIG_HIGHMEM
495 max_zone_pfns[ZONE_HIGHMEM] = max_pfn;
498 free_area_init_nodes(max_zone_pfns);