1 #include <linux/initrd.h>
2 #include <linux/ioport.h>
3 #include <linux/swap.h>
5 #include <asm/cacheflush.h>
9 #include <asm/page_types.h>
10 #include <asm/sections.h>
11 #include <asm/setup.h>
12 #include <asm/system.h>
13 #include <asm/tlbflush.h>
15 #include <asm/proto.h>
17 DEFINE_PER_CPU(struct mmu_gather
, mmu_gathers
);
19 unsigned long __initdata e820_table_start
;
20 unsigned long __meminitdata e820_table_end
;
21 unsigned long __meminitdata e820_table_top
;
26 #ifdef CONFIG_DIRECT_GBPAGES
31 static void __init
find_early_table_space(unsigned long end
, int use_pse
,
34 unsigned long puds
, pmds
, ptes
, tables
, start
;
36 puds
= (end
+ PUD_SIZE
- 1) >> PUD_SHIFT
;
37 tables
= roundup(puds
* sizeof(pud_t
), PAGE_SIZE
);
42 extra
= end
- ((end
>>PUD_SHIFT
) << PUD_SHIFT
);
43 pmds
= (extra
+ PMD_SIZE
- 1) >> PMD_SHIFT
;
45 pmds
= (end
+ PMD_SIZE
- 1) >> PMD_SHIFT
;
47 tables
+= roundup(pmds
* sizeof(pmd_t
), PAGE_SIZE
);
52 extra
= end
- ((end
>>PMD_SHIFT
) << PMD_SHIFT
);
56 ptes
= (extra
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
58 ptes
= (end
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
60 tables
+= roundup(ptes
* sizeof(pte_t
), PAGE_SIZE
);
64 tables
+= roundup(__end_of_fixed_addresses
* sizeof(pte_t
), PAGE_SIZE
);
68 * RED-PEN putting page tables only on node 0 could
69 * cause a hotspot and fill up ZONE_DMA. The page tables
70 * need roughly 0.5KB per GB.
77 e820_table_start
= find_e820_area(start
, max_pfn_mapped
<<PAGE_SHIFT
,
79 if (e820_table_start
== -1UL)
80 panic("Cannot find space for the kernel page tables");
82 e820_table_start
>>= PAGE_SHIFT
;
83 e820_table_end
= e820_table_start
;
84 e820_table_top
= e820_table_start
+ (tables
>> PAGE_SHIFT
);
86 printk(KERN_DEBUG
"kernel direct mapping tables up to %lx @ %lx-%lx\n",
87 end
, e820_table_start
<< PAGE_SHIFT
, e820_table_top
<< PAGE_SHIFT
);
93 unsigned page_size_mask
;
98 #else /* CONFIG_X86_64 */
102 static int __meminit
save_mr(struct map_range
*mr
, int nr_range
,
103 unsigned long start_pfn
, unsigned long end_pfn
,
104 unsigned long page_size_mask
)
106 if (start_pfn
< end_pfn
) {
107 if (nr_range
>= NR_RANGE_MR
)
108 panic("run out of range for init_memory_mapping\n");
109 mr
[nr_range
].start
= start_pfn
<<PAGE_SHIFT
;
110 mr
[nr_range
].end
= end_pfn
<<PAGE_SHIFT
;
111 mr
[nr_range
].page_size_mask
= page_size_mask
;
119 * Setup the direct mapping of the physical memory at PAGE_OFFSET.
120 * This runs before bootmem is initialized and gets pages directly from
121 * the physical memory. To access them they are temporarily mapped.
123 unsigned long __init_refok
init_memory_mapping(unsigned long start
,
126 unsigned long page_size_mask
= 0;
127 unsigned long start_pfn
, end_pfn
;
128 unsigned long ret
= 0;
131 struct map_range mr
[NR_RANGE_MR
];
133 int use_pse
, use_gbpages
;
135 printk(KERN_INFO
"init_memory_mapping: %016lx-%016lx\n", start
, end
);
137 #if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_KMEMCHECK)
139 * For CONFIG_DEBUG_PAGEALLOC, identity mapping will use small pages.
140 * This will simplify cpa(), which otherwise needs to support splitting
141 * large pages into small in interrupt context, etc.
143 use_pse
= use_gbpages
= 0;
145 use_pse
= cpu_has_pse
;
146 use_gbpages
= direct_gbpages
;
149 /* Enable PSE if available */
151 set_in_cr4(X86_CR4_PSE
);
153 /* Enable PGE if available */
155 set_in_cr4(X86_CR4_PGE
);
156 __supported_pte_mask
|= _PAGE_GLOBAL
;
160 page_size_mask
|= 1 << PG_LEVEL_1G
;
162 page_size_mask
|= 1 << PG_LEVEL_2M
;
164 memset(mr
, 0, sizeof(mr
));
167 /* head if not big page alignment ? */
168 start_pfn
= start
>> PAGE_SHIFT
;
169 pos
= start_pfn
<< PAGE_SHIFT
;
172 * Don't use a large page for the first 2/4MB of memory
173 * because there are often fixed size MTRRs in there
174 * and overlapping MTRRs into large pages can cause
178 end_pfn
= 1<<(PMD_SHIFT
- PAGE_SHIFT
);
180 end_pfn
= ((pos
+ (PMD_SIZE
- 1))>>PMD_SHIFT
)
181 << (PMD_SHIFT
- PAGE_SHIFT
);
182 #else /* CONFIG_X86_64 */
183 end_pfn
= ((pos
+ (PMD_SIZE
- 1)) >> PMD_SHIFT
)
184 << (PMD_SHIFT
- PAGE_SHIFT
);
186 if (end_pfn
> (end
>> PAGE_SHIFT
))
187 end_pfn
= end
>> PAGE_SHIFT
;
188 if (start_pfn
< end_pfn
) {
189 nr_range
= save_mr(mr
, nr_range
, start_pfn
, end_pfn
, 0);
190 pos
= end_pfn
<< PAGE_SHIFT
;
193 /* big page (2M) range */
194 start_pfn
= ((pos
+ (PMD_SIZE
- 1))>>PMD_SHIFT
)
195 << (PMD_SHIFT
- PAGE_SHIFT
);
197 end_pfn
= (end
>>PMD_SHIFT
) << (PMD_SHIFT
- PAGE_SHIFT
);
198 #else /* CONFIG_X86_64 */
199 end_pfn
= ((pos
+ (PUD_SIZE
- 1))>>PUD_SHIFT
)
200 << (PUD_SHIFT
- PAGE_SHIFT
);
201 if (end_pfn
> ((end
>>PMD_SHIFT
)<<(PMD_SHIFT
- PAGE_SHIFT
)))
202 end_pfn
= ((end
>>PMD_SHIFT
)<<(PMD_SHIFT
- PAGE_SHIFT
));
205 if (start_pfn
< end_pfn
) {
206 nr_range
= save_mr(mr
, nr_range
, start_pfn
, end_pfn
,
207 page_size_mask
& (1<<PG_LEVEL_2M
));
208 pos
= end_pfn
<< PAGE_SHIFT
;
212 /* big page (1G) range */
213 start_pfn
= ((pos
+ (PUD_SIZE
- 1))>>PUD_SHIFT
)
214 << (PUD_SHIFT
- PAGE_SHIFT
);
215 end_pfn
= (end
>> PUD_SHIFT
) << (PUD_SHIFT
- PAGE_SHIFT
);
216 if (start_pfn
< end_pfn
) {
217 nr_range
= save_mr(mr
, nr_range
, start_pfn
, end_pfn
,
219 ((1<<PG_LEVEL_2M
)|(1<<PG_LEVEL_1G
)));
220 pos
= end_pfn
<< PAGE_SHIFT
;
223 /* tail is not big page (1G) alignment */
224 start_pfn
= ((pos
+ (PMD_SIZE
- 1))>>PMD_SHIFT
)
225 << (PMD_SHIFT
- PAGE_SHIFT
);
226 end_pfn
= (end
>> PMD_SHIFT
) << (PMD_SHIFT
- PAGE_SHIFT
);
227 if (start_pfn
< end_pfn
) {
228 nr_range
= save_mr(mr
, nr_range
, start_pfn
, end_pfn
,
229 page_size_mask
& (1<<PG_LEVEL_2M
));
230 pos
= end_pfn
<< PAGE_SHIFT
;
234 /* tail is not big page (2M) alignment */
235 start_pfn
= pos
>>PAGE_SHIFT
;
236 end_pfn
= end
>>PAGE_SHIFT
;
237 nr_range
= save_mr(mr
, nr_range
, start_pfn
, end_pfn
, 0);
239 /* try to merge same page size and continuous */
240 for (i
= 0; nr_range
> 1 && i
< nr_range
- 1; i
++) {
241 unsigned long old_start
;
242 if (mr
[i
].end
!= mr
[i
+1].start
||
243 mr
[i
].page_size_mask
!= mr
[i
+1].page_size_mask
)
246 old_start
= mr
[i
].start
;
247 memmove(&mr
[i
], &mr
[i
+1],
248 (nr_range
- 1 - i
) * sizeof(struct map_range
));
249 mr
[i
--].start
= old_start
;
253 for (i
= 0; i
< nr_range
; i
++)
254 printk(KERN_DEBUG
" %010lx - %010lx page %s\n",
255 mr
[i
].start
, mr
[i
].end
,
256 (mr
[i
].page_size_mask
& (1<<PG_LEVEL_1G
))?"1G":(
257 (mr
[i
].page_size_mask
& (1<<PG_LEVEL_2M
))?"2M":"4k"));
260 * Find space for the kernel direct mapping tables.
262 * Later we should allocate these tables in the local node of the
263 * memory mapped. Unfortunately this is done currently before the
264 * nodes are discovered.
267 find_early_table_space(end
, use_pse
, use_gbpages
);
270 for (i
= 0; i
< nr_range
; i
++)
271 kernel_physical_mapping_init(mr
[i
].start
, mr
[i
].end
,
272 mr
[i
].page_size_mask
);
274 #else /* CONFIG_X86_64 */
275 for (i
= 0; i
< nr_range
; i
++)
276 ret
= kernel_physical_mapping_init(mr
[i
].start
, mr
[i
].end
,
277 mr
[i
].page_size_mask
);
281 early_ioremap_page_table_range_init();
283 load_cr3(swapper_pg_dir
);
287 if (!after_bootmem
&& !start
) {
291 mmu_cr4_features
= read_cr4();
294 * _brk_end cannot change anymore, but it and _end may be
295 * located on different 2M pages. cleanup_highmap(), however,
296 * can only consider _end when it runs, so destroy any
297 * mappings beyond _brk_end here.
299 pud
= pud_offset(pgd_offset_k(_brk_end
), _brk_end
);
300 pmd
= pmd_offset(pud
, _brk_end
- 1);
301 while (++pmd
<= pmd_offset(pud
, (unsigned long)_end
- 1))
307 if (!after_bootmem
&& e820_table_end
> e820_table_start
)
308 reserve_early(e820_table_start
<< PAGE_SHIFT
,
309 e820_table_end
<< PAGE_SHIFT
, "PGTABLE");
312 early_memtest(start
, end
);
314 return ret
>> PAGE_SHIFT
;
319 * devmem_is_allowed() checks to see if /dev/mem access to a certain address
320 * is valid. The argument is a physical page number.
323 * On x86, access has to be given to the first megabyte of ram because that area
324 * contains bios code and data regions used by X and dosemu and similar apps.
325 * Access has to be given to non-kernel-ram areas as well, these contain the PCI
326 * mmio resources as well as potential bios/acpi data regions.
328 int devmem_is_allowed(unsigned long pagenr
)
332 if (iomem_is_exclusive(pagenr
<< PAGE_SHIFT
))
334 if (!page_is_ram(pagenr
))
339 void free_init_pages(char *what
, unsigned long begin
, unsigned long end
)
341 unsigned long addr
= begin
;
347 * If debugging page accesses then do not free this memory but
348 * mark them not present - any buggy init-section access will
349 * create a kernel page fault:
351 #ifdef CONFIG_DEBUG_PAGEALLOC
352 printk(KERN_INFO
"debug: unmapping init memory %08lx..%08lx\n",
353 begin
, PAGE_ALIGN(end
));
354 set_memory_np(begin
, (end
- begin
) >> PAGE_SHIFT
);
357 * We just marked the kernel text read only above, now that
358 * we are going to free part of that, we need to make that
361 set_memory_rw(begin
, (end
- begin
) >> PAGE_SHIFT
);
363 printk(KERN_INFO
"Freeing %s: %luk freed\n", what
, (end
- begin
) >> 10);
365 for (; addr
< end
; addr
+= PAGE_SIZE
) {
366 ClearPageReserved(virt_to_page(addr
));
367 init_page_count(virt_to_page(addr
));
368 memset((void *)(addr
& ~(PAGE_SIZE
-1)),
369 POISON_FREE_INITMEM
, PAGE_SIZE
);
376 void free_initmem(void)
378 free_init_pages("unused kernel memory",
379 (unsigned long)(&__init_begin
),
380 (unsigned long)(&__init_end
));
383 #ifdef CONFIG_BLK_DEV_INITRD
384 void free_initrd_mem(unsigned long start
, unsigned long end
)
386 free_init_pages("initrd memory", start
, end
);