1 #include <linux/gfp.h>
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>	/* for max_low_pfn */
7 
8 #include <asm/cacheflush.h>
9 #include <asm/e820.h>
10 #include <asm/init.h>
11 #include <asm/page.h>
12 #include <asm/page_types.h>
13 #include <asm/sections.h>
14 #include <asm/setup.h>
15 #include <asm/tlbflush.h>
16 #include <asm/tlb.h>
17 #include <asm/proto.h>
18 #include <asm/dma.h>		/* for MAX_DMA_PFN */
19 
20 unsigned long __initdata pgt_buf_start;
21 unsigned long __meminitdata pgt_buf_end;
22 unsigned long __meminitdata pgt_buf_top;
23 
24 int after_bootmem;
25 
26 int direct_gbpages
27 #ifdef CONFIG_DIRECT_GBPAGES
28 				= 1
29 #endif
30 ;
31 
32 struct map_range {
33 	unsigned long start;
34 	unsigned long end;
35 	unsigned page_size_mask;
36 };
37 
38 /*
39  * First calculate space needed for kernel direct mapping page tables to cover
40  * mr[0].start to mr[nr_range - 1].end, while accounting for possible 2M and 1GB
41  * pages. Then find enough contiguous space for those page tables.
42  */
find_early_table_space(struct map_range * mr,int nr_range)43 static void __init find_early_table_space(struct map_range *mr, int nr_range)
44 {
45 	int i;
46 	unsigned long puds = 0, pmds = 0, ptes = 0, tables;
47 	unsigned long start = 0, good_end;
48 	unsigned long pgd_extra = 0;
49 	phys_addr_t base;
50 
51 	for (i = 0; i < nr_range; i++) {
52 		unsigned long range, extra;
53 
54 		if ((mr[i].end >> PGDIR_SHIFT) - (mr[i].start >> PGDIR_SHIFT))
55 			pgd_extra++;
56 
57 		range = mr[i].end - mr[i].start;
58 		puds += (range + PUD_SIZE - 1) >> PUD_SHIFT;
59 
60 		if (mr[i].page_size_mask & (1 << PG_LEVEL_1G)) {
61 			extra = range - ((range >> PUD_SHIFT) << PUD_SHIFT);
62 			pmds += (extra + PMD_SIZE - 1) >> PMD_SHIFT;
63 		} else {
64 			pmds += (range + PMD_SIZE - 1) >> PMD_SHIFT;
65 		}
66 
67 		if (mr[i].page_size_mask & (1 << PG_LEVEL_2M)) {
68 			extra = range - ((range >> PMD_SHIFT) << PMD_SHIFT);
69 #ifdef CONFIG_X86_32
70 			extra += PMD_SIZE;
71 #endif
72 			ptes += (extra + PAGE_SIZE - 1) >> PAGE_SHIFT;
73 		} else {
74 			ptes += (range + PAGE_SIZE - 1) >> PAGE_SHIFT;
75 		}
76 	}
77 
78 	tables = roundup(puds * sizeof(pud_t), PAGE_SIZE);
79 	tables += roundup(pmds * sizeof(pmd_t), PAGE_SIZE);
80 	tables += roundup(ptes * sizeof(pte_t), PAGE_SIZE);
81 	tables += (pgd_extra * PAGE_SIZE);
82 
83 #ifdef CONFIG_X86_32
84 	/* for fixmap */
85 	tables += roundup(__end_of_fixed_addresses * sizeof(pte_t), PAGE_SIZE);
86 #endif
87 	good_end = max_pfn_mapped << PAGE_SHIFT;
88 
89 	base = memblock_find_in_range(start, good_end, tables, PAGE_SIZE);
90 	if (!base)
91 		panic("Cannot find space for the kernel page tables");
92 
93 	pgt_buf_start = base >> PAGE_SHIFT;
94 	pgt_buf_end = pgt_buf_start;
95 	pgt_buf_top = pgt_buf_start + (tables >> PAGE_SHIFT);
96 
97  	printk(KERN_DEBUG "kernel direct mapping tables up to %#lx @ [mem %#010lx-%#010lx]\n",
98 		mr[nr_range - 1].end - 1, pgt_buf_start << PAGE_SHIFT,
99  		(pgt_buf_top << PAGE_SHIFT) - 1);
100 }
101 
native_pagetable_reserve(u64 start,u64 end)102 void __init native_pagetable_reserve(u64 start, u64 end)
103 {
104 	memblock_reserve(start, end - start);
105 }
106 
107 #ifdef CONFIG_X86_32
108 #define NR_RANGE_MR 3
109 #else /* CONFIG_X86_64 */
110 #define NR_RANGE_MR 5
111 #endif
112 
save_mr(struct map_range * mr,int nr_range,unsigned long start_pfn,unsigned long end_pfn,unsigned long page_size_mask)113 static int __meminit save_mr(struct map_range *mr, int nr_range,
114 			     unsigned long start_pfn, unsigned long end_pfn,
115 			     unsigned long page_size_mask)
116 {
117 	if (start_pfn < end_pfn) {
118 		if (nr_range >= NR_RANGE_MR)
119 			panic("run out of range for init_memory_mapping\n");
120 		mr[nr_range].start = start_pfn<<PAGE_SHIFT;
121 		mr[nr_range].end   = end_pfn<<PAGE_SHIFT;
122 		mr[nr_range].page_size_mask = page_size_mask;
123 		nr_range++;
124 	}
125 
126 	return nr_range;
127 }
128 
129 /*
130  * Setup the direct mapping of the physical memory at PAGE_OFFSET.
131  * This runs before bootmem is initialized and gets pages directly from
132  * the physical memory. To access them they are temporarily mapped.
133  */
init_memory_mapping(unsigned long start,unsigned long end)134 unsigned long __init_refok init_memory_mapping(unsigned long start,
135 					       unsigned long end)
136 {
137 	unsigned long page_size_mask = 0;
138 	unsigned long start_pfn, end_pfn;
139 	unsigned long ret = 0;
140 	unsigned long pos;
141 
142 	struct map_range mr[NR_RANGE_MR];
143 	int nr_range, i;
144 	int use_pse, use_gbpages;
145 
146 	printk(KERN_INFO "init_memory_mapping: %016lx-%016lx\n", start, end);
147 
148 #if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_KMEMCHECK)
149 	/*
150 	 * For CONFIG_DEBUG_PAGEALLOC, identity mapping will use small pages.
151 	 * This will simplify cpa(), which otherwise needs to support splitting
152 	 * large pages into small in interrupt context, etc.
153 	 */
154 	use_pse = use_gbpages = 0;
155 #else
156 	use_pse = cpu_has_pse;
157 	use_gbpages = direct_gbpages;
158 #endif
159 
160 	/* Enable PSE if available */
161 	if (cpu_has_pse)
162 		set_in_cr4(X86_CR4_PSE);
163 
164 	/* Enable PGE if available */
165 	if (cpu_has_pge) {
166 		set_in_cr4(X86_CR4_PGE);
167 		__supported_pte_mask |= _PAGE_GLOBAL;
168 	}
169 
170 	if (use_gbpages)
171 		page_size_mask |= 1 << PG_LEVEL_1G;
172 	if (use_pse)
173 		page_size_mask |= 1 << PG_LEVEL_2M;
174 
175 	memset(mr, 0, sizeof(mr));
176 	nr_range = 0;
177 
178 	/* head if not big page alignment ? */
179 	start_pfn = start >> PAGE_SHIFT;
180 	pos = start_pfn << PAGE_SHIFT;
181 #ifdef CONFIG_X86_32
182 	/*
183 	 * Don't use a large page for the first 2/4MB of memory
184 	 * because there are often fixed size MTRRs in there
185 	 * and overlapping MTRRs into large pages can cause
186 	 * slowdowns.
187 	 */
188 	if (pos == 0)
189 		end_pfn = 1<<(PMD_SHIFT - PAGE_SHIFT);
190 	else
191 		end_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
192 				 << (PMD_SHIFT - PAGE_SHIFT);
193 #else /* CONFIG_X86_64 */
194 	end_pfn = ((pos + (PMD_SIZE - 1)) >> PMD_SHIFT)
195 			<< (PMD_SHIFT - PAGE_SHIFT);
196 #endif
197 	if (end_pfn > (end >> PAGE_SHIFT))
198 		end_pfn = end >> PAGE_SHIFT;
199 	if (start_pfn < end_pfn) {
200 		nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
201 		pos = end_pfn << PAGE_SHIFT;
202 	}
203 
204 	/* big page (2M) range */
205 	start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
206 			 << (PMD_SHIFT - PAGE_SHIFT);
207 #ifdef CONFIG_X86_32
208 	end_pfn = (end>>PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
209 #else /* CONFIG_X86_64 */
210 	end_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT)
211 			 << (PUD_SHIFT - PAGE_SHIFT);
212 	if (end_pfn > ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT)))
213 		end_pfn = ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT));
214 #endif
215 
216 	if (start_pfn < end_pfn) {
217 		nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
218 				page_size_mask & (1<<PG_LEVEL_2M));
219 		pos = end_pfn << PAGE_SHIFT;
220 	}
221 
222 #ifdef CONFIG_X86_64
223 	/* big page (1G) range */
224 	start_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT)
225 			 << (PUD_SHIFT - PAGE_SHIFT);
226 	end_pfn = (end >> PUD_SHIFT) << (PUD_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 &
230 				 ((1<<PG_LEVEL_2M)|(1<<PG_LEVEL_1G)));
231 		pos = end_pfn << PAGE_SHIFT;
232 	}
233 
234 	/* tail is not big page (1G) alignment */
235 	start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
236 			 << (PMD_SHIFT - PAGE_SHIFT);
237 	end_pfn = (end >> PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
238 	if (start_pfn < end_pfn) {
239 		nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
240 				page_size_mask & (1<<PG_LEVEL_2M));
241 		pos = end_pfn << PAGE_SHIFT;
242 	}
243 #endif
244 
245 	/* tail is not big page (2M) alignment */
246 	start_pfn = pos>>PAGE_SHIFT;
247 	end_pfn = end>>PAGE_SHIFT;
248 	nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
249 
250 	/* try to merge same page size and continuous */
251 	for (i = 0; nr_range > 1 && i < nr_range - 1; i++) {
252 		unsigned long old_start;
253 		if (mr[i].end != mr[i+1].start ||
254 		    mr[i].page_size_mask != mr[i+1].page_size_mask)
255 			continue;
256 		/* move it */
257 		old_start = mr[i].start;
258 		memmove(&mr[i], &mr[i+1],
259 			(nr_range - 1 - i) * sizeof(struct map_range));
260 		mr[i--].start = old_start;
261 		nr_range--;
262 	}
263 
264 	for (i = 0; i < nr_range; i++)
265 		printk(KERN_DEBUG " %010lx - %010lx page %s\n",
266 				mr[i].start, mr[i].end,
267 			(mr[i].page_size_mask & (1<<PG_LEVEL_1G))?"1G":(
268 			 (mr[i].page_size_mask & (1<<PG_LEVEL_2M))?"2M":"4k"));
269 
270 	/*
271 	 * Find space for the kernel direct mapping tables.
272 	 *
273 	 * Later we should allocate these tables in the local node of the
274 	 * memory mapped. Unfortunately this is done currently before the
275 	 * nodes are discovered.
276 	 */
277 	if (!after_bootmem)
278 		find_early_table_space(mr, nr_range);
279 
280 	for (i = 0; i < nr_range; i++)
281 		ret = kernel_physical_mapping_init(mr[i].start, mr[i].end,
282 						   mr[i].page_size_mask);
283 
284 #ifdef CONFIG_X86_32
285 	early_ioremap_page_table_range_init();
286 
287 	load_cr3(swapper_pg_dir);
288 #endif
289 
290 	__flush_tlb_all();
291 
292 	/*
293 	 * Reserve the kernel pagetable pages we used (pgt_buf_start -
294 	 * pgt_buf_end) and free the other ones (pgt_buf_end - pgt_buf_top)
295 	 * so that they can be reused for other purposes.
296 	 *
297 	 * On native it just means calling memblock_reserve, on Xen it also
298 	 * means marking RW the pagetable pages that we allocated before
299 	 * but that haven't been used.
300 	 *
301 	 * In fact on xen we mark RO the whole range pgt_buf_start -
302 	 * pgt_buf_top, because we have to make sure that when
303 	 * init_memory_mapping reaches the pagetable pages area, it maps
304 	 * RO all the pagetable pages, including the ones that are beyond
305 	 * pgt_buf_end at that time.
306 	 */
307 	if (!after_bootmem && pgt_buf_end > pgt_buf_start)
308 		x86_init.mapping.pagetable_reserve(PFN_PHYS(pgt_buf_start),
309 				PFN_PHYS(pgt_buf_end));
310 
311 	if (!after_bootmem)
312 		early_memtest(start, end);
313 
314 	return ret >> PAGE_SHIFT;
315 }
316 
317 
318 /*
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.
321  *
322  *
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.
327  */
devmem_is_allowed(unsigned long pagenr)328 int devmem_is_allowed(unsigned long pagenr)
329 {
330 	if (pagenr <= 256)
331 		return 1;
332 	if (iomem_is_exclusive(pagenr << PAGE_SHIFT))
333 		return 0;
334 	if (!page_is_ram(pagenr))
335 		return 1;
336 	return 0;
337 }
338 
free_init_pages(char * what,unsigned long begin,unsigned long end)339 void free_init_pages(char *what, unsigned long begin, unsigned long end)
340 {
341 	unsigned long addr;
342 	unsigned long begin_aligned, end_aligned;
343 
344 	/* Make sure boundaries are page aligned */
345 	begin_aligned = PAGE_ALIGN(begin);
346 	end_aligned   = end & PAGE_MASK;
347 
348 	if (WARN_ON(begin_aligned != begin || end_aligned != end)) {
349 		begin = begin_aligned;
350 		end   = end_aligned;
351 	}
352 
353 	if (begin >= end)
354 		return;
355 
356 	addr = begin;
357 
358 	/*
359 	 * If debugging page accesses then do not free this memory but
360 	 * mark them not present - any buggy init-section access will
361 	 * create a kernel page fault:
362 	 */
363 #ifdef CONFIG_DEBUG_PAGEALLOC
364 	printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n",
365 		begin, end);
366 	set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
367 #else
368 	/*
369 	 * We just marked the kernel text read only above, now that
370 	 * we are going to free part of that, we need to make that
371 	 * writeable and non-executable first.
372 	 */
373 	set_memory_nx(begin, (end - begin) >> PAGE_SHIFT);
374 	set_memory_rw(begin, (end - begin) >> PAGE_SHIFT);
375 
376 	printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
377 
378 	for (; addr < end; addr += PAGE_SIZE) {
379 		ClearPageReserved(virt_to_page(addr));
380 		init_page_count(virt_to_page(addr));
381 		memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
382 		free_page(addr);
383 		totalram_pages++;
384 	}
385 #endif
386 }
387 
free_initmem(void)388 void free_initmem(void)
389 {
390 	free_init_pages("unused kernel memory",
391 			(unsigned long)(&__init_begin),
392 			(unsigned long)(&__init_end));
393 }
394 
395 #ifdef CONFIG_BLK_DEV_INITRD
free_initrd_mem(unsigned long start,unsigned long end)396 void free_initrd_mem(unsigned long start, unsigned long end)
397 {
398 	/*
399 	 * end could be not aligned, and We can not align that,
400 	 * decompresser could be confused by aligned initrd_end
401 	 * We already reserve the end partial page before in
402 	 *   - i386_start_kernel()
403 	 *   - x86_64_start_kernel()
404 	 *   - relocate_initrd()
405 	 * So here We can do PAGE_ALIGN() safely to get partial page to be freed
406 	 */
407 	free_init_pages("initrd memory", start, PAGE_ALIGN(end));
408 }
409 #endif
410 
zone_sizes_init(void)411 void __init zone_sizes_init(void)
412 {
413 	unsigned long max_zone_pfns[MAX_NR_ZONES];
414 
415 	memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
416 
417 #ifdef CONFIG_ZONE_DMA
418 	max_zone_pfns[ZONE_DMA]		= MAX_DMA_PFN;
419 #endif
420 #ifdef CONFIG_ZONE_DMA32
421 	max_zone_pfns[ZONE_DMA32]	= MAX_DMA32_PFN;
422 #endif
423 	max_zone_pfns[ZONE_NORMAL]	= max_low_pfn;
424 #ifdef CONFIG_HIGHMEM
425 	max_zone_pfns[ZONE_HIGHMEM]	= max_pfn;
426 #endif
427 
428 	free_area_init_nodes(max_zone_pfns);
429 }
430 
431