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 
7 #include <asm/cacheflush.h>
8 #include <asm/e820.h>
9 #include <asm/init.h>
10 #include <asm/page.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>
16 #include <asm/tlb.h>
17 #include <asm/proto.h>
18 
19 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
20 
21 unsigned long __initdata pgt_buf_start;
22 unsigned long __meminitdata pgt_buf_end;
23 unsigned long __meminitdata pgt_buf_top;
24 
25 int after_bootmem;
26 
27 int direct_gbpages
28 #ifdef CONFIG_DIRECT_GBPAGES
29 				= 1
30 #endif
31 ;
32 
find_early_table_space(unsigned long end,int use_pse,int use_gbpages)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 = 0, good_end = end;
37 	phys_addr_t base;
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 
69 	good_end = max_pfn_mapped << PAGE_SHIFT;
70 #endif
71 
72 	base = memblock_find_in_range(start, good_end, tables, PAGE_SIZE);
73 	if (base == MEMBLOCK_ERROR)
74 		panic("Cannot find space for the kernel page tables");
75 
76 	pgt_buf_start = base >> PAGE_SHIFT;
77 	pgt_buf_end = pgt_buf_start;
78 	pgt_buf_top = pgt_buf_start + (tables >> PAGE_SHIFT);
79 
80 	printk(KERN_DEBUG "kernel direct mapping tables up to %lx @ %lx-%lx\n",
81 		end, pgt_buf_start << PAGE_SHIFT, pgt_buf_top << PAGE_SHIFT);
82 }
83 
native_pagetable_reserve(u64 start,u64 end)84 void __init native_pagetable_reserve(u64 start, u64 end)
85 {
86 	memblock_x86_reserve_range(start, end, "PGTABLE");
87 }
88 
89 struct map_range {
90 	unsigned long start;
91 	unsigned long end;
92 	unsigned page_size_mask;
93 };
94 
95 #ifdef CONFIG_X86_32
96 #define NR_RANGE_MR 3
97 #else /* CONFIG_X86_64 */
98 #define NR_RANGE_MR 5
99 #endif
100 
save_mr(struct map_range * mr,int nr_range,unsigned long start_pfn,unsigned long end_pfn,unsigned long page_size_mask)101 static int __meminit save_mr(struct map_range *mr, int nr_range,
102 			     unsigned long start_pfn, unsigned long end_pfn,
103 			     unsigned long page_size_mask)
104 {
105 	if (start_pfn < end_pfn) {
106 		if (nr_range >= NR_RANGE_MR)
107 			panic("run out of range for init_memory_mapping\n");
108 		mr[nr_range].start = start_pfn<<PAGE_SHIFT;
109 		mr[nr_range].end   = end_pfn<<PAGE_SHIFT;
110 		mr[nr_range].page_size_mask = page_size_mask;
111 		nr_range++;
112 	}
113 
114 	return nr_range;
115 }
116 
117 /*
118  * Setup the direct mapping of the physical memory at PAGE_OFFSET.
119  * This runs before bootmem is initialized and gets pages directly from
120  * the physical memory. To access them they are temporarily mapped.
121  */
init_memory_mapping(unsigned long start,unsigned long end)122 unsigned long __init_refok init_memory_mapping(unsigned long start,
123 					       unsigned long end)
124 {
125 	unsigned long page_size_mask = 0;
126 	unsigned long start_pfn, end_pfn;
127 	unsigned long ret = 0;
128 	unsigned long pos;
129 
130 	struct map_range mr[NR_RANGE_MR];
131 	int nr_range, i;
132 	int use_pse, use_gbpages;
133 
134 	printk(KERN_INFO "init_memory_mapping: %016lx-%016lx\n", start, end);
135 
136 #if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_KMEMCHECK)
137 	/*
138 	 * For CONFIG_DEBUG_PAGEALLOC, identity mapping will use small pages.
139 	 * This will simplify cpa(), which otherwise needs to support splitting
140 	 * large pages into small in interrupt context, etc.
141 	 */
142 	use_pse = use_gbpages = 0;
143 #else
144 	use_pse = cpu_has_pse;
145 	use_gbpages = direct_gbpages;
146 #endif
147 
148 	/* Enable PSE if available */
149 	if (cpu_has_pse)
150 		set_in_cr4(X86_CR4_PSE);
151 
152 	/* Enable PGE if available */
153 	if (cpu_has_pge) {
154 		set_in_cr4(X86_CR4_PGE);
155 		__supported_pte_mask |= _PAGE_GLOBAL;
156 	}
157 
158 	if (use_gbpages)
159 		page_size_mask |= 1 << PG_LEVEL_1G;
160 	if (use_pse)
161 		page_size_mask |= 1 << PG_LEVEL_2M;
162 
163 	memset(mr, 0, sizeof(mr));
164 	nr_range = 0;
165 
166 	/* head if not big page alignment ? */
167 	start_pfn = start >> PAGE_SHIFT;
168 	pos = start_pfn << PAGE_SHIFT;
169 #ifdef CONFIG_X86_32
170 	/*
171 	 * Don't use a large page for the first 2/4MB of memory
172 	 * because there are often fixed size MTRRs in there
173 	 * and overlapping MTRRs into large pages can cause
174 	 * slowdowns.
175 	 */
176 	if (pos == 0)
177 		end_pfn = 1<<(PMD_SHIFT - PAGE_SHIFT);
178 	else
179 		end_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
180 				 << (PMD_SHIFT - PAGE_SHIFT);
181 #else /* CONFIG_X86_64 */
182 	end_pfn = ((pos + (PMD_SIZE - 1)) >> PMD_SHIFT)
183 			<< (PMD_SHIFT - PAGE_SHIFT);
184 #endif
185 	if (end_pfn > (end >> PAGE_SHIFT))
186 		end_pfn = end >> PAGE_SHIFT;
187 	if (start_pfn < end_pfn) {
188 		nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
189 		pos = end_pfn << PAGE_SHIFT;
190 	}
191 
192 	/* big page (2M) range */
193 	start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
194 			 << (PMD_SHIFT - PAGE_SHIFT);
195 #ifdef CONFIG_X86_32
196 	end_pfn = (end>>PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
197 #else /* CONFIG_X86_64 */
198 	end_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT)
199 			 << (PUD_SHIFT - PAGE_SHIFT);
200 	if (end_pfn > ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT)))
201 		end_pfn = ((end>>PMD_SHIFT)<<(PMD_SHIFT - PAGE_SHIFT));
202 #endif
203 
204 	if (start_pfn < end_pfn) {
205 		nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
206 				page_size_mask & (1<<PG_LEVEL_2M));
207 		pos = end_pfn << PAGE_SHIFT;
208 	}
209 
210 #ifdef CONFIG_X86_64
211 	/* big page (1G) range */
212 	start_pfn = ((pos + (PUD_SIZE - 1))>>PUD_SHIFT)
213 			 << (PUD_SHIFT - PAGE_SHIFT);
214 	end_pfn = (end >> PUD_SHIFT) << (PUD_SHIFT - PAGE_SHIFT);
215 	if (start_pfn < end_pfn) {
216 		nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
217 				page_size_mask &
218 				 ((1<<PG_LEVEL_2M)|(1<<PG_LEVEL_1G)));
219 		pos = end_pfn << PAGE_SHIFT;
220 	}
221 
222 	/* tail is not big page (1G) alignment */
223 	start_pfn = ((pos + (PMD_SIZE - 1))>>PMD_SHIFT)
224 			 << (PMD_SHIFT - PAGE_SHIFT);
225 	end_pfn = (end >> PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
226 	if (start_pfn < end_pfn) {
227 		nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
228 				page_size_mask & (1<<PG_LEVEL_2M));
229 		pos = end_pfn << PAGE_SHIFT;
230 	}
231 #endif
232 
233 	/* tail is not big page (2M) alignment */
234 	start_pfn = pos>>PAGE_SHIFT;
235 	end_pfn = end>>PAGE_SHIFT;
236 	nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
237 
238 	/* try to merge same page size and continuous */
239 	for (i = 0; nr_range > 1 && i < nr_range - 1; i++) {
240 		unsigned long old_start;
241 		if (mr[i].end != mr[i+1].start ||
242 		    mr[i].page_size_mask != mr[i+1].page_size_mask)
243 			continue;
244 		/* move it */
245 		old_start = mr[i].start;
246 		memmove(&mr[i], &mr[i+1],
247 			(nr_range - 1 - i) * sizeof(struct map_range));
248 		mr[i--].start = old_start;
249 		nr_range--;
250 	}
251 
252 	for (i = 0; i < nr_range; i++)
253 		printk(KERN_DEBUG " %010lx - %010lx page %s\n",
254 				mr[i].start, mr[i].end,
255 			(mr[i].page_size_mask & (1<<PG_LEVEL_1G))?"1G":(
256 			 (mr[i].page_size_mask & (1<<PG_LEVEL_2M))?"2M":"4k"));
257 
258 	/*
259 	 * Find space for the kernel direct mapping tables.
260 	 *
261 	 * Later we should allocate these tables in the local node of the
262 	 * memory mapped. Unfortunately this is done currently before the
263 	 * nodes are discovered.
264 	 */
265 	if (!after_bootmem)
266 		find_early_table_space(end, use_pse, use_gbpages);
267 
268 	for (i = 0; i < nr_range; i++)
269 		ret = kernel_physical_mapping_init(mr[i].start, mr[i].end,
270 						   mr[i].page_size_mask);
271 
272 #ifdef CONFIG_X86_32
273 	early_ioremap_page_table_range_init();
274 
275 	load_cr3(swapper_pg_dir);
276 #endif
277 
278 	__flush_tlb_all();
279 
280 	/*
281 	 * Reserve the kernel pagetable pages we used (pgt_buf_start -
282 	 * pgt_buf_end) and free the other ones (pgt_buf_end - pgt_buf_top)
283 	 * so that they can be reused for other purposes.
284 	 *
285 	 * On native it just means calling memblock_x86_reserve_range, on Xen it
286 	 * also means marking RW the pagetable pages that we allocated before
287 	 * but that haven't been used.
288 	 *
289 	 * In fact on xen we mark RO the whole range pgt_buf_start -
290 	 * pgt_buf_top, because we have to make sure that when
291 	 * init_memory_mapping reaches the pagetable pages area, it maps
292 	 * RO all the pagetable pages, including the ones that are beyond
293 	 * pgt_buf_end at that time.
294 	 */
295 	if (!after_bootmem && pgt_buf_end > pgt_buf_start)
296 		x86_init.mapping.pagetable_reserve(PFN_PHYS(pgt_buf_start),
297 				PFN_PHYS(pgt_buf_end));
298 
299 	if (!after_bootmem)
300 		early_memtest(start, end);
301 
302 	return ret >> PAGE_SHIFT;
303 }
304 
305 
306 /*
307  * devmem_is_allowed() checks to see if /dev/mem access to a certain address
308  * is valid. The argument is a physical page number.
309  *
310  *
311  * On x86, access has to be given to the first megabyte of ram because that area
312  * contains bios code and data regions used by X and dosemu and similar apps.
313  * Access has to be given to non-kernel-ram areas as well, these contain the PCI
314  * mmio resources as well as potential bios/acpi data regions.
315  */
devmem_is_allowed(unsigned long pagenr)316 int devmem_is_allowed(unsigned long pagenr)
317 {
318 	if (pagenr <= 256)
319 		return 1;
320 	if (iomem_is_exclusive(pagenr << PAGE_SHIFT))
321 		return 0;
322 	if (!page_is_ram(pagenr))
323 		return 1;
324 	return 0;
325 }
326 
free_init_pages(char * what,unsigned long begin,unsigned long end)327 void free_init_pages(char *what, unsigned long begin, unsigned long end)
328 {
329 	unsigned long addr;
330 	unsigned long begin_aligned, end_aligned;
331 
332 	/* Make sure boundaries are page aligned */
333 	begin_aligned = PAGE_ALIGN(begin);
334 	end_aligned   = end & PAGE_MASK;
335 
336 	if (WARN_ON(begin_aligned != begin || end_aligned != end)) {
337 		begin = begin_aligned;
338 		end   = end_aligned;
339 	}
340 
341 	if (begin >= end)
342 		return;
343 
344 	addr = begin;
345 
346 	/*
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:
350 	 */
351 #ifdef CONFIG_DEBUG_PAGEALLOC
352 	printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n",
353 		begin, end);
354 	set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
355 #else
356 	/*
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
359 	 * writeable and non-executable first.
360 	 */
361 	set_memory_nx(begin, (end - begin) >> PAGE_SHIFT);
362 	set_memory_rw(begin, (end - begin) >> PAGE_SHIFT);
363 
364 	printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
365 
366 	for (; addr < end; addr += PAGE_SIZE) {
367 		ClearPageReserved(virt_to_page(addr));
368 		init_page_count(virt_to_page(addr));
369 		memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
370 		free_page(addr);
371 		totalram_pages++;
372 	}
373 #endif
374 }
375 
free_initmem(void)376 void free_initmem(void)
377 {
378 	free_init_pages("unused kernel memory",
379 			(unsigned long)(&__init_begin),
380 			(unsigned long)(&__init_end));
381 }
382 
383 #ifdef CONFIG_BLK_DEV_INITRD
free_initrd_mem(unsigned long start,unsigned long end)384 void free_initrd_mem(unsigned long start, unsigned long end)
385 {
386 	/*
387 	 * end could be not aligned, and We can not align that,
388 	 * decompresser could be confused by aligned initrd_end
389 	 * We already reserve the end partial page before in
390 	 *   - i386_start_kernel()
391 	 *   - x86_64_start_kernel()
392 	 *   - relocate_initrd()
393 	 * So here We can do PAGE_ALIGN() safely to get partial page to be freed
394 	 */
395 	free_init_pages("initrd memory", start, PAGE_ALIGN(end));
396 }
397 #endif
398