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 unsigned long __initdata pgt_buf_start;
20 unsigned long __meminitdata pgt_buf_end;
21 unsigned long __meminitdata pgt_buf_top;
26 #ifdef CONFIG_DIRECT_GBPAGES
34 unsigned page_size_mask;
38 * First calculate space needed for kernel direct mapping page tables to cover
39 * mr[0].start to mr[nr_range - 1].end, while accounting for possible 2M and 1GB
40 * pages. Then find enough contiguous space for those page tables.
42 static void __init find_early_table_space(struct map_range *mr, int nr_range)
45 unsigned long puds = 0, pmds = 0, ptes = 0, tables;
46 unsigned long start = 0, good_end;
49 for (i = 0; i < nr_range; i++) {
50 unsigned long range, extra;
52 range = mr[i].end - mr[i].start;
53 puds += (range + PUD_SIZE - 1) >> PUD_SHIFT;
55 if (mr[i].page_size_mask & (1 << PG_LEVEL_1G)) {
56 extra = range - ((range >> PUD_SHIFT) << PUD_SHIFT);
57 pmds += (extra + PMD_SIZE - 1) >> PMD_SHIFT;
59 pmds += (range + PMD_SIZE - 1) >> PMD_SHIFT;
62 if (mr[i].page_size_mask & (1 << PG_LEVEL_2M)) {
63 extra = range - ((range >> PMD_SHIFT) << PMD_SHIFT);
67 /* The first 2/4M doesn't use large pages. */
68 if (mr[i].start < PMD_SIZE)
71 ptes += (extra + PAGE_SIZE - 1) >> PAGE_SHIFT;
73 ptes += (range + PAGE_SIZE - 1) >> PAGE_SHIFT;
77 tables = roundup(puds * sizeof(pud_t), PAGE_SIZE);
78 tables += roundup(pmds * sizeof(pmd_t), PAGE_SIZE);
79 tables += roundup(ptes * sizeof(pte_t), PAGE_SIZE);
83 tables += roundup(__end_of_fixed_addresses * sizeof(pte_t), PAGE_SIZE);
85 good_end = max_pfn_mapped << PAGE_SHIFT;
87 base = memblock_find_in_range(start, good_end, tables, PAGE_SIZE);
88 if (base == MEMBLOCK_ERROR)
89 panic("Cannot find space for the kernel page tables");
91 pgt_buf_start = base >> PAGE_SHIFT;
92 pgt_buf_end = pgt_buf_start;
93 pgt_buf_top = pgt_buf_start + (tables >> PAGE_SHIFT);
95 printk(KERN_DEBUG "kernel direct mapping tables up to %lx @ %lx-%lx\n",
96 mr[nr_range - 1].end, pgt_buf_start << PAGE_SHIFT,
97 pgt_buf_top << PAGE_SHIFT);
100 void __init native_pagetable_reserve(u64 start, u64 end)
102 memblock_x86_reserve_range(start, end, "PGTABLE");
106 #define NR_RANGE_MR 3
107 #else /* CONFIG_X86_64 */
108 #define NR_RANGE_MR 5
111 static int __meminit save_mr(struct map_range *mr, int nr_range,
112 unsigned long start_pfn, unsigned long end_pfn,
113 unsigned long page_size_mask)
115 if (start_pfn < end_pfn) {
116 if (nr_range >= NR_RANGE_MR)
117 panic("run out of range for init_memory_mapping\n");
118 mr[nr_range].start = start_pfn<<PAGE_SHIFT;
119 mr[nr_range].end = end_pfn<<PAGE_SHIFT;
120 mr[nr_range].page_size_mask = page_size_mask;
128 * Setup the direct mapping of the physical memory at PAGE_OFFSET.
129 * This runs before bootmem is initialized and gets pages directly from
130 * the physical memory. To access them they are temporarily mapped.
132 unsigned long __init_refok init_memory_mapping(unsigned long start,
135 unsigned long page_size_mask = 0;
136 unsigned long start_pfn, end_pfn;
137 unsigned long ret = 0;
140 struct map_range mr[NR_RANGE_MR];
142 int use_pse, use_gbpages;
144 printk(KERN_INFO "init_memory_mapping: %016lx-%016lx\n", start, end);
146 #if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_KMEMCHECK)
148 * For CONFIG_DEBUG_PAGEALLOC, identity mapping will use small pages.
149 * This will simplify cpa(), which otherwise needs to support splitting
150 * large pages into small in interrupt context, etc.
152 use_pse = use_gbpages = 0;
154 use_pse = cpu_has_pse;
155 use_gbpages = direct_gbpages;
158 /* Enable PSE if available */
160 set_in_cr4(X86_CR4_PSE);
162 /* Enable PGE if available */
164 set_in_cr4(X86_CR4_PGE);
165 __supported_pte_mask |= _PAGE_GLOBAL;
169 page_size_mask |= 1 << PG_LEVEL_1G;
171 page_size_mask |= 1 << PG_LEVEL_2M;
173 memset(mr, 0, sizeof(mr));
176 /* head if not big page alignment ? */
177 start_pfn = start >> PAGE_SHIFT;
178 pos = start_pfn << PAGE_SHIFT;
181 * Don't use a large page for the first 2/4MB of memory
182 * because there are often fixed size MTRRs in there
183 * and overlapping MTRRs into large pages can cause
187 end_pfn = 1<<(PMD_SHIFT - PAGE_SHIFT);
189 end_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
190 << (PMD_SHIFT - PAGE_SHIFT);
191 #else /* CONFIG_X86_64 */
192 end_pfn = ((pos + (PMD_SIZE - 1)) >> PMD_SHIFT)
193 << (PMD_SHIFT - PAGE_SHIFT);
195 if (end_pfn > (end >> PAGE_SHIFT))
196 end_pfn = end >> PAGE_SHIFT;
197 if (start_pfn < end_pfn) {
198 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
199 pos = end_pfn << PAGE_SHIFT;
202 /* big page (2M) range */
203 start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
204 << (PMD_SHIFT - PAGE_SHIFT);
206 end_pfn = (end>>PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
207 #else /* CONFIG_X86_64 */
208 end_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT)
209 << (PUD_SHIFT - PAGE_SHIFT);
210 if (end_pfn > ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT)))
211 end_pfn = ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT));
214 if (start_pfn < end_pfn) {
215 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
216 page_size_mask & (1<<PG_LEVEL_2M));
217 pos = end_pfn << PAGE_SHIFT;
221 /* big page (1G) range */
222 start_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT)
223 << (PUD_SHIFT - PAGE_SHIFT);
224 end_pfn = (end >> PUD_SHIFT) << (PUD_SHIFT - PAGE_SHIFT);
225 if (start_pfn < end_pfn) {
226 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
228 ((1<<PG_LEVEL_2M)|(1<<PG_LEVEL_1G)));
229 pos = end_pfn << PAGE_SHIFT;
232 /* tail is not big page (1G) alignment */
233 start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
234 << (PMD_SHIFT - PAGE_SHIFT);
235 end_pfn = (end >> PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
236 if (start_pfn < end_pfn) {
237 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
238 page_size_mask & (1<<PG_LEVEL_2M));
239 pos = end_pfn << PAGE_SHIFT;
243 /* tail is not big page (2M) alignment */
244 start_pfn = pos>>PAGE_SHIFT;
245 end_pfn = end>>PAGE_SHIFT;
246 nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
248 /* try to merge same page size and continuous */
249 for (i = 0; nr_range > 1 && i < nr_range - 1; i++) {
250 unsigned long old_start;
251 if (mr[i].end != mr[i+1].start ||
252 mr[i].page_size_mask != mr[i+1].page_size_mask)
255 old_start = mr[i].start;
256 memmove(&mr[i], &mr[i+1],
257 (nr_range - 1 - i) * sizeof(struct map_range));
258 mr[i--].start = old_start;
262 for (i = 0; i < nr_range; i++)
263 printk(KERN_DEBUG " %010lx - %010lx page %s\n",
264 mr[i].start, mr[i].end,
265 (mr[i].page_size_mask & (1<<PG_LEVEL_1G))?"1G":(
266 (mr[i].page_size_mask & (1<<PG_LEVEL_2M))?"2M":"4k"));
269 * Find space for the kernel direct mapping tables.
271 * Later we should allocate these tables in the local node of the
272 * memory mapped. Unfortunately this is done currently before the
273 * nodes are discovered.
276 find_early_table_space(mr, nr_range);
278 for (i = 0; i < nr_range; i++)
279 ret = kernel_physical_mapping_init(mr[i].start, mr[i].end,
280 mr[i].page_size_mask);
283 early_ioremap_page_table_range_init();
285 load_cr3(swapper_pg_dir);
291 * Reserve the kernel pagetable pages we used (pgt_buf_start -
292 * pgt_buf_end) and free the other ones (pgt_buf_end - pgt_buf_top)
293 * so that they can be reused for other purposes.
295 * On native it just means calling memblock_x86_reserve_range, on Xen it
296 * also means marking RW the pagetable pages that we allocated before
297 * but that haven't been used.
299 * In fact on xen we mark RO the whole range pgt_buf_start -
300 * pgt_buf_top, because we have to make sure that when
301 * init_memory_mapping reaches the pagetable pages area, it maps
302 * RO all the pagetable pages, including the ones that are beyond
303 * pgt_buf_end at that time.
305 if (!after_bootmem && pgt_buf_end > pgt_buf_start)
306 x86_init.mapping.pagetable_reserve(PFN_PHYS(pgt_buf_start),
307 PFN_PHYS(pgt_buf_end));
310 early_memtest(start, end);
312 return ret >> PAGE_SHIFT;
317 * devmem_is_allowed() checks to see if /dev/mem access to a certain address
318 * is valid. The argument is a physical page number.
321 * On x86, access has to be given to the first megabyte of ram because that area
322 * contains bios code and data regions used by X and dosemu and similar apps.
323 * Access has to be given to non-kernel-ram areas as well, these contain the PCI
324 * mmio resources as well as potential bios/acpi data regions.
326 int devmem_is_allowed(unsigned long pagenr)
330 if (iomem_is_exclusive(pagenr << PAGE_SHIFT))
332 if (!page_is_ram(pagenr))
337 void free_init_pages(char *what, unsigned long begin, unsigned long end)
340 unsigned long begin_aligned, end_aligned;
342 /* Make sure boundaries are page aligned */
343 begin_aligned = PAGE_ALIGN(begin);
344 end_aligned = end & PAGE_MASK;
346 if (WARN_ON(begin_aligned != begin || end_aligned != end)) {
347 begin = begin_aligned;
357 * If debugging page accesses then do not free this memory but
358 * mark them not present - any buggy init-section access will
359 * create a kernel page fault:
361 #ifdef CONFIG_DEBUG_PAGEALLOC
362 printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n",
364 set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
367 * We just marked the kernel text read only above, now that
368 * we are going to free part of that, we need to make that
369 * writeable and non-executable first.
371 set_memory_nx(begin, (end - begin) >> PAGE_SHIFT);
372 set_memory_rw(begin, (end - begin) >> PAGE_SHIFT);
374 printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
376 for (; addr < end; addr += PAGE_SIZE) {
377 ClearPageReserved(virt_to_page(addr));
378 init_page_count(virt_to_page(addr));
379 memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
386 void free_initmem(void)
388 free_init_pages("unused kernel memory",
389 (unsigned long)(&__init_begin),
390 (unsigned long)(&__init_end));
393 #ifdef CONFIG_BLK_DEV_INITRD
394 void free_initrd_mem(unsigned long start, unsigned long end)
397 * end could be not aligned, and We can not align that,
398 * decompresser could be confused by aligned initrd_end
399 * We already reserve the end partial page before in
400 * - i386_start_kernel()
401 * - x86_64_start_kernel()
402 * - relocate_initrd()
403 * So here We can do PAGE_ALIGN() safely to get partial page to be freed
405 free_init_pages("initrd memory", start, PAGE_ALIGN(end));