]>
Commit | Line | Data |
---|---|---|
5a0e3ad6 | 1 | #include <linux/gfp.h> |
2c1b284e | 2 | #include <linux/initrd.h> |
540aca06 | 3 | #include <linux/ioport.h> |
e5b2bb55 | 4 | #include <linux/swap.h> |
a9ce6bc1 | 5 | #include <linux/memblock.h> |
540aca06 | 6 | |
e5b2bb55 | 7 | #include <asm/cacheflush.h> |
f765090a | 8 | #include <asm/e820.h> |
4fcb2083 | 9 | #include <asm/init.h> |
e5b2bb55 | 10 | #include <asm/page.h> |
540aca06 | 11 | #include <asm/page_types.h> |
e5b2bb55 | 12 | #include <asm/sections.h> |
49834396 | 13 | #include <asm/setup.h> |
e5b2bb55 | 14 | #include <asm/system.h> |
f765090a | 15 | #include <asm/tlbflush.h> |
9518e0e4 | 16 | #include <asm/tlb.h> |
76c06927 | 17 | #include <asm/proto.h> |
9518e0e4 PE |
18 | |
19 | DEFINE_PER_CPU(struct mmu_gather, mmu_gathers); | |
f765090a | 20 | |
298af9d8 PE |
21 | unsigned long __initdata e820_table_start; |
22 | unsigned long __meminitdata e820_table_end; | |
23 | unsigned long __meminitdata e820_table_top; | |
f765090a PE |
24 | |
25 | int after_bootmem; | |
26 | ||
27 | int direct_gbpages | |
28 | #ifdef CONFIG_DIRECT_GBPAGES | |
29 | = 1 | |
30 | #endif | |
31 | ; | |
32 | ||
33 | static void __init find_early_table_space(unsigned long end, int use_pse, | |
34 | int use_gbpages) | |
35 | { | |
36 | unsigned long puds, pmds, ptes, tables, start; | |
a9ce6bc1 | 37 | phys_addr_t base; |
f765090a PE |
38 | |
39 | puds = (end + PUD_SIZE - 1) >> PUD_SHIFT; | |
40 | tables = roundup(puds * sizeof(pud_t), PAGE_SIZE); | |
41 | ||
42 | if (use_gbpages) { | |
43 | unsigned long extra; | |
44 | ||
45 | extra = end - ((end>>PUD_SHIFT) << PUD_SHIFT); | |
46 | pmds = (extra + PMD_SIZE - 1) >> PMD_SHIFT; | |
47 | } else | |
48 | pmds = (end + PMD_SIZE - 1) >> PMD_SHIFT; | |
49 | ||
50 | tables += roundup(pmds * sizeof(pmd_t), PAGE_SIZE); | |
51 | ||
52 | if (use_pse) { | |
53 | unsigned long extra; | |
54 | ||
55 | extra = end - ((end>>PMD_SHIFT) << PMD_SHIFT); | |
56 | #ifdef CONFIG_X86_32 | |
57 | extra += PMD_SIZE; | |
58 | #endif | |
59 | ptes = (extra + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
60 | } else | |
61 | ptes = (end + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
62 | ||
63 | tables += roundup(ptes * sizeof(pte_t), PAGE_SIZE); | |
64 | ||
65 | #ifdef CONFIG_X86_32 | |
66 | /* for fixmap */ | |
67 | tables += roundup(__end_of_fixed_addresses * sizeof(pte_t), PAGE_SIZE); | |
68 | #endif | |
69 | ||
70 | /* | |
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. | |
74 | */ | |
75 | #ifdef CONFIG_X86_32 | |
76 | start = 0x7000; | |
80989ce0 | 77 | #else |
f765090a | 78 | start = 0x8000; |
f765090a | 79 | #endif |
a9ce6bc1 | 80 | base = memblock_find_in_range(start, max_pfn_mapped<<PAGE_SHIFT, |
80989ce0 | 81 | tables, PAGE_SIZE); |
a9ce6bc1 | 82 | if (base == MEMBLOCK_ERROR) |
f765090a PE |
83 | panic("Cannot find space for the kernel page tables"); |
84 | ||
a9ce6bc1 | 85 | e820_table_start = base >> PAGE_SHIFT; |
298af9d8 PE |
86 | e820_table_end = e820_table_start; |
87 | e820_table_top = e820_table_start + (tables >> PAGE_SHIFT); | |
f765090a PE |
88 | |
89 | printk(KERN_DEBUG "kernel direct mapping tables up to %lx @ %lx-%lx\n", | |
298af9d8 | 90 | end, e820_table_start << PAGE_SHIFT, e820_table_top << PAGE_SHIFT); |
f765090a PE |
91 | } |
92 | ||
93 | struct map_range { | |
94 | unsigned long start; | |
95 | unsigned long end; | |
96 | unsigned page_size_mask; | |
97 | }; | |
98 | ||
99 | #ifdef CONFIG_X86_32 | |
100 | #define NR_RANGE_MR 3 | |
101 | #else /* CONFIG_X86_64 */ | |
102 | #define NR_RANGE_MR 5 | |
103 | #endif | |
104 | ||
dc9dd5cc JB |
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) | |
f765090a PE |
108 | { |
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; | |
115 | nr_range++; | |
116 | } | |
117 | ||
118 | return nr_range; | |
119 | } | |
120 | ||
f765090a PE |
121 | /* |
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. | |
125 | */ | |
126 | unsigned long __init_refok init_memory_mapping(unsigned long start, | |
127 | unsigned long end) | |
128 | { | |
129 | unsigned long page_size_mask = 0; | |
130 | unsigned long start_pfn, end_pfn; | |
c77a3b59 | 131 | unsigned long ret = 0; |
f765090a | 132 | unsigned long pos; |
f765090a PE |
133 | |
134 | struct map_range mr[NR_RANGE_MR]; | |
135 | int nr_range, i; | |
136 | int use_pse, use_gbpages; | |
137 | ||
138 | printk(KERN_INFO "init_memory_mapping: %016lx-%016lx\n", start, end); | |
139 | ||
f8561296 | 140 | #if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_KMEMCHECK) |
f765090a PE |
141 | /* |
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. | |
145 | */ | |
146 | use_pse = use_gbpages = 0; | |
147 | #else | |
148 | use_pse = cpu_has_pse; | |
149 | use_gbpages = direct_gbpages; | |
150 | #endif | |
151 | ||
f765090a PE |
152 | /* Enable PSE if available */ |
153 | if (cpu_has_pse) | |
154 | set_in_cr4(X86_CR4_PSE); | |
155 | ||
156 | /* Enable PGE if available */ | |
157 | if (cpu_has_pge) { | |
158 | set_in_cr4(X86_CR4_PGE); | |
159 | __supported_pte_mask |= _PAGE_GLOBAL; | |
160 | } | |
f765090a PE |
161 | |
162 | if (use_gbpages) | |
163 | page_size_mask |= 1 << PG_LEVEL_1G; | |
164 | if (use_pse) | |
165 | page_size_mask |= 1 << PG_LEVEL_2M; | |
166 | ||
167 | memset(mr, 0, sizeof(mr)); | |
168 | nr_range = 0; | |
169 | ||
170 | /* head if not big page alignment ? */ | |
171 | start_pfn = start >> PAGE_SHIFT; | |
172 | pos = start_pfn << PAGE_SHIFT; | |
173 | #ifdef CONFIG_X86_32 | |
174 | /* | |
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 | |
178 | * slowdowns. | |
179 | */ | |
180 | if (pos == 0) | |
181 | end_pfn = 1<<(PMD_SHIFT - PAGE_SHIFT); | |
182 | else | |
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); | |
188 | #endif | |
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; | |
194 | } | |
195 | ||
196 | /* big page (2M) range */ | |
197 | start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT) | |
198 | << (PMD_SHIFT - PAGE_SHIFT); | |
199 | #ifdef CONFIG_X86_32 | |
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)); | |
206 | #endif | |
207 | ||
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; | |
212 | } | |
213 | ||
214 | #ifdef CONFIG_X86_64 | |
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, | |
221 | page_size_mask & | |
222 | ((1<<PG_LEVEL_2M)|(1<<PG_LEVEL_1G))); | |
223 | pos = end_pfn << PAGE_SHIFT; | |
224 | } | |
225 | ||
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; | |
234 | } | |
235 | #endif | |
236 | ||
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); | |
241 | ||
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) | |
247 | continue; | |
248 | /* move it */ | |
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; | |
253 | nr_range--; | |
254 | } | |
255 | ||
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")); | |
261 | ||
262 | /* | |
263 | * Find space for the kernel direct mapping tables. | |
264 | * | |
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. | |
268 | */ | |
269 | if (!after_bootmem) | |
270 | find_early_table_space(end, use_pse, use_gbpages); | |
271 | ||
f765090a PE |
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); | |
f765090a PE |
275 | |
276 | #ifdef CONFIG_X86_32 | |
277 | early_ioremap_page_table_range_init(); | |
278 | ||
279 | load_cr3(swapper_pg_dir); | |
280 | #endif | |
281 | ||
282 | #ifdef CONFIG_X86_64 | |
49834396 JB |
283 | if (!after_bootmem && !start) { |
284 | pud_t *pud; | |
285 | pmd_t *pmd; | |
286 | ||
f765090a | 287 | mmu_cr4_features = read_cr4(); |
49834396 JB |
288 | |
289 | /* | |
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. | |
294 | */ | |
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)) | |
298 | pmd_clear(pmd); | |
299 | } | |
f765090a PE |
300 | #endif |
301 | __flush_tlb_all(); | |
302 | ||
298af9d8 | 303 | if (!after_bootmem && e820_table_end > e820_table_start) |
a9ce6bc1 | 304 | memblock_x86_reserve_range(e820_table_start << PAGE_SHIFT, |
298af9d8 | 305 | e820_table_end << PAGE_SHIFT, "PGTABLE"); |
f765090a PE |
306 | |
307 | if (!after_bootmem) | |
308 | early_memtest(start, end); | |
309 | ||
310 | return ret >> PAGE_SHIFT; | |
311 | } | |
312 | ||
e5b2bb55 | 313 | |
540aca06 PE |
314 | /* |
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. | |
317 | * | |
318 | * | |
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. | |
323 | */ | |
324 | int devmem_is_allowed(unsigned long pagenr) | |
325 | { | |
326 | if (pagenr <= 256) | |
327 | return 1; | |
328 | if (iomem_is_exclusive(pagenr << PAGE_SHIFT)) | |
329 | return 0; | |
330 | if (!page_is_ram(pagenr)) | |
331 | return 1; | |
332 | return 0; | |
333 | } | |
334 | ||
e5b2bb55 PE |
335 | void free_init_pages(char *what, unsigned long begin, unsigned long end) |
336 | { | |
c967da6a YL |
337 | unsigned long addr; |
338 | unsigned long begin_aligned, end_aligned; | |
e5b2bb55 | 339 | |
c967da6a YL |
340 | /* Make sure boundaries are page aligned */ |
341 | begin_aligned = PAGE_ALIGN(begin); | |
342 | end_aligned = end & PAGE_MASK; | |
343 | ||
344 | if (WARN_ON(begin_aligned != begin || end_aligned != end)) { | |
345 | begin = begin_aligned; | |
346 | end = end_aligned; | |
347 | } | |
348 | ||
349 | if (begin >= end) | |
e5b2bb55 PE |
350 | return; |
351 | ||
c967da6a YL |
352 | addr = begin; |
353 | ||
e5b2bb55 PE |
354 | /* |
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: | |
358 | */ | |
359 | #ifdef CONFIG_DEBUG_PAGEALLOC | |
360 | printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n", | |
c967da6a | 361 | begin, end); |
e5b2bb55 PE |
362 | set_memory_np(begin, (end - begin) >> PAGE_SHIFT); |
363 | #else | |
364 | /* | |
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 | |
367 | * writeable first. | |
368 | */ | |
369 | set_memory_rw(begin, (end - begin) >> PAGE_SHIFT); | |
370 | ||
371 | printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10); | |
372 | ||
373 | for (; addr < end; addr += PAGE_SIZE) { | |
374 | ClearPageReserved(virt_to_page(addr)); | |
375 | init_page_count(virt_to_page(addr)); | |
c967da6a | 376 | memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE); |
e5b2bb55 PE |
377 | free_page(addr); |
378 | totalram_pages++; | |
379 | } | |
380 | #endif | |
381 | } | |
382 | ||
383 | void free_initmem(void) | |
384 | { | |
385 | free_init_pages("unused kernel memory", | |
386 | (unsigned long)(&__init_begin), | |
387 | (unsigned long)(&__init_end)); | |
388 | } | |
731ddea6 PE |
389 | |
390 | #ifdef CONFIG_BLK_DEV_INITRD | |
391 | void free_initrd_mem(unsigned long start, unsigned long end) | |
392 | { | |
c967da6a YL |
393 | /* |
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 | |
401 | */ | |
402 | free_init_pages("initrd memory", start, PAGE_ALIGN(end)); | |
731ddea6 PE |
403 | } |
404 | #endif |