xref: /openbmc/linux/arch/x86/mm/init_64.c (revision 0883c2c0)
1 /*
2  *  linux/arch/x86_64/mm/init.c
3  *
4  *  Copyright (C) 1995  Linus Torvalds
5  *  Copyright (C) 2000  Pavel Machek <pavel@ucw.cz>
6  *  Copyright (C) 2002,2003 Andi Kleen <ak@suse.de>
7  */
8 
9 #include <linux/signal.h>
10 #include <linux/sched.h>
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/string.h>
14 #include <linux/types.h>
15 #include <linux/ptrace.h>
16 #include <linux/mman.h>
17 #include <linux/mm.h>
18 #include <linux/swap.h>
19 #include <linux/smp.h>
20 #include <linux/init.h>
21 #include <linux/initrd.h>
22 #include <linux/pagemap.h>
23 #include <linux/bootmem.h>
24 #include <linux/memblock.h>
25 #include <linux/proc_fs.h>
26 #include <linux/pci.h>
27 #include <linux/pfn.h>
28 #include <linux/poison.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/module.h>
31 #include <linux/memory.h>
32 #include <linux/memory_hotplug.h>
33 #include <linux/memremap.h>
34 #include <linux/nmi.h>
35 #include <linux/gfp.h>
36 #include <linux/kcore.h>
37 
38 #include <asm/processor.h>
39 #include <asm/bios_ebda.h>
40 #include <asm/uaccess.h>
41 #include <asm/pgtable.h>
42 #include <asm/pgalloc.h>
43 #include <asm/dma.h>
44 #include <asm/fixmap.h>
45 #include <asm/e820.h>
46 #include <asm/apic.h>
47 #include <asm/tlb.h>
48 #include <asm/mmu_context.h>
49 #include <asm/proto.h>
50 #include <asm/smp.h>
51 #include <asm/sections.h>
52 #include <asm/kdebug.h>
53 #include <asm/numa.h>
54 #include <asm/cacheflush.h>
55 #include <asm/init.h>
56 #include <asm/uv/uv.h>
57 #include <asm/setup.h>
58 
59 #include "mm_internal.h"
60 
61 #include "ident_map.c"
62 
63 /*
64  * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the
65  * physical space so we can cache the place of the first one and move
66  * around without checking the pgd every time.
67  */
68 
69 pteval_t __supported_pte_mask __read_mostly = ~0;
70 EXPORT_SYMBOL_GPL(__supported_pte_mask);
71 
72 int force_personality32;
73 
74 /*
75  * noexec32=on|off
76  * Control non executable heap for 32bit processes.
77  * To control the stack too use noexec=off
78  *
79  * on	PROT_READ does not imply PROT_EXEC for 32-bit processes (default)
80  * off	PROT_READ implies PROT_EXEC
81  */
82 static int __init nonx32_setup(char *str)
83 {
84 	if (!strcmp(str, "on"))
85 		force_personality32 &= ~READ_IMPLIES_EXEC;
86 	else if (!strcmp(str, "off"))
87 		force_personality32 |= READ_IMPLIES_EXEC;
88 	return 1;
89 }
90 __setup("noexec32=", nonx32_setup);
91 
92 /*
93  * When memory was added/removed make sure all the processes MM have
94  * suitable PGD entries in the local PGD level page.
95  */
96 void sync_global_pgds(unsigned long start, unsigned long end, int removed)
97 {
98 	unsigned long address;
99 
100 	for (address = start; address <= end; address += PGDIR_SIZE) {
101 		const pgd_t *pgd_ref = pgd_offset_k(address);
102 		struct page *page;
103 
104 		/*
105 		 * When it is called after memory hot remove, pgd_none()
106 		 * returns true. In this case (removed == 1), we must clear
107 		 * the PGD entries in the local PGD level page.
108 		 */
109 		if (pgd_none(*pgd_ref) && !removed)
110 			continue;
111 
112 		spin_lock(&pgd_lock);
113 		list_for_each_entry(page, &pgd_list, lru) {
114 			pgd_t *pgd;
115 			spinlock_t *pgt_lock;
116 
117 			pgd = (pgd_t *)page_address(page) + pgd_index(address);
118 			/* the pgt_lock only for Xen */
119 			pgt_lock = &pgd_page_get_mm(page)->page_table_lock;
120 			spin_lock(pgt_lock);
121 
122 			if (!pgd_none(*pgd_ref) && !pgd_none(*pgd))
123 				BUG_ON(pgd_page_vaddr(*pgd)
124 				       != pgd_page_vaddr(*pgd_ref));
125 
126 			if (removed) {
127 				if (pgd_none(*pgd_ref) && !pgd_none(*pgd))
128 					pgd_clear(pgd);
129 			} else {
130 				if (pgd_none(*pgd))
131 					set_pgd(pgd, *pgd_ref);
132 			}
133 
134 			spin_unlock(pgt_lock);
135 		}
136 		spin_unlock(&pgd_lock);
137 	}
138 }
139 
140 /*
141  * NOTE: This function is marked __ref because it calls __init function
142  * (alloc_bootmem_pages). It's safe to do it ONLY when after_bootmem == 0.
143  */
144 static __ref void *spp_getpage(void)
145 {
146 	void *ptr;
147 
148 	if (after_bootmem)
149 		ptr = (void *) get_zeroed_page(GFP_ATOMIC | __GFP_NOTRACK);
150 	else
151 		ptr = alloc_bootmem_pages(PAGE_SIZE);
152 
153 	if (!ptr || ((unsigned long)ptr & ~PAGE_MASK)) {
154 		panic("set_pte_phys: cannot allocate page data %s\n",
155 			after_bootmem ? "after bootmem" : "");
156 	}
157 
158 	pr_debug("spp_getpage %p\n", ptr);
159 
160 	return ptr;
161 }
162 
163 static pud_t *fill_pud(pgd_t *pgd, unsigned long vaddr)
164 {
165 	if (pgd_none(*pgd)) {
166 		pud_t *pud = (pud_t *)spp_getpage();
167 		pgd_populate(&init_mm, pgd, pud);
168 		if (pud != pud_offset(pgd, 0))
169 			printk(KERN_ERR "PAGETABLE BUG #00! %p <-> %p\n",
170 			       pud, pud_offset(pgd, 0));
171 	}
172 	return pud_offset(pgd, vaddr);
173 }
174 
175 static pmd_t *fill_pmd(pud_t *pud, unsigned long vaddr)
176 {
177 	if (pud_none(*pud)) {
178 		pmd_t *pmd = (pmd_t *) spp_getpage();
179 		pud_populate(&init_mm, pud, pmd);
180 		if (pmd != pmd_offset(pud, 0))
181 			printk(KERN_ERR "PAGETABLE BUG #01! %p <-> %p\n",
182 			       pmd, pmd_offset(pud, 0));
183 	}
184 	return pmd_offset(pud, vaddr);
185 }
186 
187 static pte_t *fill_pte(pmd_t *pmd, unsigned long vaddr)
188 {
189 	if (pmd_none(*pmd)) {
190 		pte_t *pte = (pte_t *) spp_getpage();
191 		pmd_populate_kernel(&init_mm, pmd, pte);
192 		if (pte != pte_offset_kernel(pmd, 0))
193 			printk(KERN_ERR "PAGETABLE BUG #02!\n");
194 	}
195 	return pte_offset_kernel(pmd, vaddr);
196 }
197 
198 void set_pte_vaddr_pud(pud_t *pud_page, unsigned long vaddr, pte_t new_pte)
199 {
200 	pud_t *pud;
201 	pmd_t *pmd;
202 	pte_t *pte;
203 
204 	pud = pud_page + pud_index(vaddr);
205 	pmd = fill_pmd(pud, vaddr);
206 	pte = fill_pte(pmd, vaddr);
207 
208 	set_pte(pte, new_pte);
209 
210 	/*
211 	 * It's enough to flush this one mapping.
212 	 * (PGE mappings get flushed as well)
213 	 */
214 	__flush_tlb_one(vaddr);
215 }
216 
217 void set_pte_vaddr(unsigned long vaddr, pte_t pteval)
218 {
219 	pgd_t *pgd;
220 	pud_t *pud_page;
221 
222 	pr_debug("set_pte_vaddr %lx to %lx\n", vaddr, native_pte_val(pteval));
223 
224 	pgd = pgd_offset_k(vaddr);
225 	if (pgd_none(*pgd)) {
226 		printk(KERN_ERR
227 			"PGD FIXMAP MISSING, it should be setup in head.S!\n");
228 		return;
229 	}
230 	pud_page = (pud_t*)pgd_page_vaddr(*pgd);
231 	set_pte_vaddr_pud(pud_page, vaddr, pteval);
232 }
233 
234 pmd_t * __init populate_extra_pmd(unsigned long vaddr)
235 {
236 	pgd_t *pgd;
237 	pud_t *pud;
238 
239 	pgd = pgd_offset_k(vaddr);
240 	pud = fill_pud(pgd, vaddr);
241 	return fill_pmd(pud, vaddr);
242 }
243 
244 pte_t * __init populate_extra_pte(unsigned long vaddr)
245 {
246 	pmd_t *pmd;
247 
248 	pmd = populate_extra_pmd(vaddr);
249 	return fill_pte(pmd, vaddr);
250 }
251 
252 /*
253  * Create large page table mappings for a range of physical addresses.
254  */
255 static void __init __init_extra_mapping(unsigned long phys, unsigned long size,
256 					enum page_cache_mode cache)
257 {
258 	pgd_t *pgd;
259 	pud_t *pud;
260 	pmd_t *pmd;
261 	pgprot_t prot;
262 
263 	pgprot_val(prot) = pgprot_val(PAGE_KERNEL_LARGE) |
264 		pgprot_val(pgprot_4k_2_large(cachemode2pgprot(cache)));
265 	BUG_ON((phys & ~PMD_MASK) || (size & ~PMD_MASK));
266 	for (; size; phys += PMD_SIZE, size -= PMD_SIZE) {
267 		pgd = pgd_offset_k((unsigned long)__va(phys));
268 		if (pgd_none(*pgd)) {
269 			pud = (pud_t *) spp_getpage();
270 			set_pgd(pgd, __pgd(__pa(pud) | _KERNPG_TABLE |
271 						_PAGE_USER));
272 		}
273 		pud = pud_offset(pgd, (unsigned long)__va(phys));
274 		if (pud_none(*pud)) {
275 			pmd = (pmd_t *) spp_getpage();
276 			set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE |
277 						_PAGE_USER));
278 		}
279 		pmd = pmd_offset(pud, phys);
280 		BUG_ON(!pmd_none(*pmd));
281 		set_pmd(pmd, __pmd(phys | pgprot_val(prot)));
282 	}
283 }
284 
285 void __init init_extra_mapping_wb(unsigned long phys, unsigned long size)
286 {
287 	__init_extra_mapping(phys, size, _PAGE_CACHE_MODE_WB);
288 }
289 
290 void __init init_extra_mapping_uc(unsigned long phys, unsigned long size)
291 {
292 	__init_extra_mapping(phys, size, _PAGE_CACHE_MODE_UC);
293 }
294 
295 /*
296  * The head.S code sets up the kernel high mapping:
297  *
298  *   from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text)
299  *
300  * phys_base holds the negative offset to the kernel, which is added
301  * to the compile time generated pmds. This results in invalid pmds up
302  * to the point where we hit the physaddr 0 mapping.
303  *
304  * We limit the mappings to the region from _text to _brk_end.  _brk_end
305  * is rounded up to the 2MB boundary. This catches the invalid pmds as
306  * well, as they are located before _text:
307  */
308 void __init cleanup_highmap(void)
309 {
310 	unsigned long vaddr = __START_KERNEL_map;
311 	unsigned long vaddr_end = __START_KERNEL_map + KERNEL_IMAGE_SIZE;
312 	unsigned long end = roundup((unsigned long)_brk_end, PMD_SIZE) - 1;
313 	pmd_t *pmd = level2_kernel_pgt;
314 
315 	/*
316 	 * Native path, max_pfn_mapped is not set yet.
317 	 * Xen has valid max_pfn_mapped set in
318 	 *	arch/x86/xen/mmu.c:xen_setup_kernel_pagetable().
319 	 */
320 	if (max_pfn_mapped)
321 		vaddr_end = __START_KERNEL_map + (max_pfn_mapped << PAGE_SHIFT);
322 
323 	for (; vaddr + PMD_SIZE - 1 < vaddr_end; pmd++, vaddr += PMD_SIZE) {
324 		if (pmd_none(*pmd))
325 			continue;
326 		if (vaddr < (unsigned long) _text || vaddr > end)
327 			set_pmd(pmd, __pmd(0));
328 	}
329 }
330 
331 static unsigned long __meminit
332 phys_pte_init(pte_t *pte_page, unsigned long addr, unsigned long end,
333 	      pgprot_t prot)
334 {
335 	unsigned long pages = 0, next;
336 	unsigned long last_map_addr = end;
337 	int i;
338 
339 	pte_t *pte = pte_page + pte_index(addr);
340 
341 	for (i = pte_index(addr); i < PTRS_PER_PTE; i++, addr = next, pte++) {
342 		next = (addr & PAGE_MASK) + PAGE_SIZE;
343 		if (addr >= end) {
344 			if (!after_bootmem &&
345 			    !e820_any_mapped(addr & PAGE_MASK, next, E820_RAM) &&
346 			    !e820_any_mapped(addr & PAGE_MASK, next, E820_RESERVED_KERN))
347 				set_pte(pte, __pte(0));
348 			continue;
349 		}
350 
351 		/*
352 		 * We will re-use the existing mapping.
353 		 * Xen for example has some special requirements, like mapping
354 		 * pagetable pages as RO. So assume someone who pre-setup
355 		 * these mappings are more intelligent.
356 		 */
357 		if (pte_val(*pte)) {
358 			if (!after_bootmem)
359 				pages++;
360 			continue;
361 		}
362 
363 		if (0)
364 			printk("   pte=%p addr=%lx pte=%016lx\n",
365 			       pte, addr, pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL).pte);
366 		pages++;
367 		set_pte(pte, pfn_pte(addr >> PAGE_SHIFT, prot));
368 		last_map_addr = (addr & PAGE_MASK) + PAGE_SIZE;
369 	}
370 
371 	update_page_count(PG_LEVEL_4K, pages);
372 
373 	return last_map_addr;
374 }
375 
376 static unsigned long __meminit
377 phys_pmd_init(pmd_t *pmd_page, unsigned long address, unsigned long end,
378 	      unsigned long page_size_mask, pgprot_t prot)
379 {
380 	unsigned long pages = 0, next;
381 	unsigned long last_map_addr = end;
382 
383 	int i = pmd_index(address);
384 
385 	for (; i < PTRS_PER_PMD; i++, address = next) {
386 		pmd_t *pmd = pmd_page + pmd_index(address);
387 		pte_t *pte;
388 		pgprot_t new_prot = prot;
389 
390 		next = (address & PMD_MASK) + PMD_SIZE;
391 		if (address >= end) {
392 			if (!after_bootmem &&
393 			    !e820_any_mapped(address & PMD_MASK, next, E820_RAM) &&
394 			    !e820_any_mapped(address & PMD_MASK, next, E820_RESERVED_KERN))
395 				set_pmd(pmd, __pmd(0));
396 			continue;
397 		}
398 
399 		if (pmd_val(*pmd)) {
400 			if (!pmd_large(*pmd)) {
401 				spin_lock(&init_mm.page_table_lock);
402 				pte = (pte_t *)pmd_page_vaddr(*pmd);
403 				last_map_addr = phys_pte_init(pte, address,
404 								end, prot);
405 				spin_unlock(&init_mm.page_table_lock);
406 				continue;
407 			}
408 			/*
409 			 * If we are ok with PG_LEVEL_2M mapping, then we will
410 			 * use the existing mapping,
411 			 *
412 			 * Otherwise, we will split the large page mapping but
413 			 * use the same existing protection bits except for
414 			 * large page, so that we don't violate Intel's TLB
415 			 * Application note (317080) which says, while changing
416 			 * the page sizes, new and old translations should
417 			 * not differ with respect to page frame and
418 			 * attributes.
419 			 */
420 			if (page_size_mask & (1 << PG_LEVEL_2M)) {
421 				if (!after_bootmem)
422 					pages++;
423 				last_map_addr = next;
424 				continue;
425 			}
426 			new_prot = pte_pgprot(pte_clrhuge(*(pte_t *)pmd));
427 		}
428 
429 		if (page_size_mask & (1<<PG_LEVEL_2M)) {
430 			pages++;
431 			spin_lock(&init_mm.page_table_lock);
432 			set_pte((pte_t *)pmd,
433 				pfn_pte((address & PMD_MASK) >> PAGE_SHIFT,
434 					__pgprot(pgprot_val(prot) | _PAGE_PSE)));
435 			spin_unlock(&init_mm.page_table_lock);
436 			last_map_addr = next;
437 			continue;
438 		}
439 
440 		pte = alloc_low_page();
441 		last_map_addr = phys_pte_init(pte, address, end, new_prot);
442 
443 		spin_lock(&init_mm.page_table_lock);
444 		pmd_populate_kernel(&init_mm, pmd, pte);
445 		spin_unlock(&init_mm.page_table_lock);
446 	}
447 	update_page_count(PG_LEVEL_2M, pages);
448 	return last_map_addr;
449 }
450 
451 static unsigned long __meminit
452 phys_pud_init(pud_t *pud_page, unsigned long addr, unsigned long end,
453 			 unsigned long page_size_mask)
454 {
455 	unsigned long pages = 0, next;
456 	unsigned long last_map_addr = end;
457 	int i = pud_index(addr);
458 
459 	for (; i < PTRS_PER_PUD; i++, addr = next) {
460 		pud_t *pud = pud_page + pud_index(addr);
461 		pmd_t *pmd;
462 		pgprot_t prot = PAGE_KERNEL;
463 
464 		next = (addr & PUD_MASK) + PUD_SIZE;
465 		if (addr >= end) {
466 			if (!after_bootmem &&
467 			    !e820_any_mapped(addr & PUD_MASK, next, E820_RAM) &&
468 			    !e820_any_mapped(addr & PUD_MASK, next, E820_RESERVED_KERN))
469 				set_pud(pud, __pud(0));
470 			continue;
471 		}
472 
473 		if (pud_val(*pud)) {
474 			if (!pud_large(*pud)) {
475 				pmd = pmd_offset(pud, 0);
476 				last_map_addr = phys_pmd_init(pmd, addr, end,
477 							 page_size_mask, prot);
478 				__flush_tlb_all();
479 				continue;
480 			}
481 			/*
482 			 * If we are ok with PG_LEVEL_1G mapping, then we will
483 			 * use the existing mapping.
484 			 *
485 			 * Otherwise, we will split the gbpage mapping but use
486 			 * the same existing protection  bits except for large
487 			 * page, so that we don't violate Intel's TLB
488 			 * Application note (317080) which says, while changing
489 			 * the page sizes, new and old translations should
490 			 * not differ with respect to page frame and
491 			 * attributes.
492 			 */
493 			if (page_size_mask & (1 << PG_LEVEL_1G)) {
494 				if (!after_bootmem)
495 					pages++;
496 				last_map_addr = next;
497 				continue;
498 			}
499 			prot = pte_pgprot(pte_clrhuge(*(pte_t *)pud));
500 		}
501 
502 		if (page_size_mask & (1<<PG_LEVEL_1G)) {
503 			pages++;
504 			spin_lock(&init_mm.page_table_lock);
505 			set_pte((pte_t *)pud,
506 				pfn_pte((addr & PUD_MASK) >> PAGE_SHIFT,
507 					PAGE_KERNEL_LARGE));
508 			spin_unlock(&init_mm.page_table_lock);
509 			last_map_addr = next;
510 			continue;
511 		}
512 
513 		pmd = alloc_low_page();
514 		last_map_addr = phys_pmd_init(pmd, addr, end, page_size_mask,
515 					      prot);
516 
517 		spin_lock(&init_mm.page_table_lock);
518 		pud_populate(&init_mm, pud, pmd);
519 		spin_unlock(&init_mm.page_table_lock);
520 	}
521 	__flush_tlb_all();
522 
523 	update_page_count(PG_LEVEL_1G, pages);
524 
525 	return last_map_addr;
526 }
527 
528 unsigned long __meminit
529 kernel_physical_mapping_init(unsigned long start,
530 			     unsigned long end,
531 			     unsigned long page_size_mask)
532 {
533 	bool pgd_changed = false;
534 	unsigned long next, last_map_addr = end;
535 	unsigned long addr;
536 
537 	start = (unsigned long)__va(start);
538 	end = (unsigned long)__va(end);
539 	addr = start;
540 
541 	for (; start < end; start = next) {
542 		pgd_t *pgd = pgd_offset_k(start);
543 		pud_t *pud;
544 
545 		next = (start & PGDIR_MASK) + PGDIR_SIZE;
546 
547 		if (pgd_val(*pgd)) {
548 			pud = (pud_t *)pgd_page_vaddr(*pgd);
549 			last_map_addr = phys_pud_init(pud, __pa(start),
550 						 __pa(end), page_size_mask);
551 			continue;
552 		}
553 
554 		pud = alloc_low_page();
555 		last_map_addr = phys_pud_init(pud, __pa(start), __pa(end),
556 						 page_size_mask);
557 
558 		spin_lock(&init_mm.page_table_lock);
559 		pgd_populate(&init_mm, pgd, pud);
560 		spin_unlock(&init_mm.page_table_lock);
561 		pgd_changed = true;
562 	}
563 
564 	if (pgd_changed)
565 		sync_global_pgds(addr, end - 1, 0);
566 
567 	__flush_tlb_all();
568 
569 	return last_map_addr;
570 }
571 
572 #ifndef CONFIG_NUMA
573 void __init initmem_init(void)
574 {
575 	memblock_set_node(0, (phys_addr_t)ULLONG_MAX, &memblock.memory, 0);
576 }
577 #endif
578 
579 void __init paging_init(void)
580 {
581 	sparse_memory_present_with_active_regions(MAX_NUMNODES);
582 	sparse_init();
583 
584 	/*
585 	 * clear the default setting with node 0
586 	 * note: don't use nodes_clear here, that is really clearing when
587 	 *	 numa support is not compiled in, and later node_set_state
588 	 *	 will not set it back.
589 	 */
590 	node_clear_state(0, N_MEMORY);
591 	if (N_MEMORY != N_NORMAL_MEMORY)
592 		node_clear_state(0, N_NORMAL_MEMORY);
593 
594 	zone_sizes_init();
595 }
596 
597 /*
598  * Memory hotplug specific functions
599  */
600 #ifdef CONFIG_MEMORY_HOTPLUG
601 /*
602  * After memory hotplug the variables max_pfn, max_low_pfn and high_memory need
603  * updating.
604  */
605 static void  update_end_of_memory_vars(u64 start, u64 size)
606 {
607 	unsigned long end_pfn = PFN_UP(start + size);
608 
609 	if (end_pfn > max_pfn) {
610 		max_pfn = end_pfn;
611 		max_low_pfn = end_pfn;
612 		high_memory = (void *)__va(max_pfn * PAGE_SIZE - 1) + 1;
613 	}
614 }
615 
616 /*
617  * Memory is added always to NORMAL zone. This means you will never get
618  * additional DMA/DMA32 memory.
619  */
620 int arch_add_memory(int nid, u64 start, u64 size, bool for_device)
621 {
622 	struct pglist_data *pgdat = NODE_DATA(nid);
623 	struct zone *zone = pgdat->node_zones +
624 		zone_for_memory(nid, start, size, ZONE_NORMAL, for_device);
625 	unsigned long start_pfn = start >> PAGE_SHIFT;
626 	unsigned long nr_pages = size >> PAGE_SHIFT;
627 	int ret;
628 
629 	init_memory_mapping(start, start + size);
630 
631 	ret = __add_pages(nid, zone, start_pfn, nr_pages);
632 	WARN_ON_ONCE(ret);
633 
634 	/* update max_pfn, max_low_pfn and high_memory */
635 	update_end_of_memory_vars(start, size);
636 
637 	return ret;
638 }
639 EXPORT_SYMBOL_GPL(arch_add_memory);
640 
641 #define PAGE_INUSE 0xFD
642 
643 static void __meminit free_pagetable(struct page *page, int order)
644 {
645 	unsigned long magic;
646 	unsigned int nr_pages = 1 << order;
647 	struct vmem_altmap *altmap = to_vmem_altmap((unsigned long) page);
648 
649 	if (altmap) {
650 		vmem_altmap_free(altmap, nr_pages);
651 		return;
652 	}
653 
654 	/* bootmem page has reserved flag */
655 	if (PageReserved(page)) {
656 		__ClearPageReserved(page);
657 
658 		magic = (unsigned long)page->lru.next;
659 		if (magic == SECTION_INFO || magic == MIX_SECTION_INFO) {
660 			while (nr_pages--)
661 				put_page_bootmem(page++);
662 		} else
663 			while (nr_pages--)
664 				free_reserved_page(page++);
665 	} else
666 		free_pages((unsigned long)page_address(page), order);
667 }
668 
669 static void __meminit free_pte_table(pte_t *pte_start, pmd_t *pmd)
670 {
671 	pte_t *pte;
672 	int i;
673 
674 	for (i = 0; i < PTRS_PER_PTE; i++) {
675 		pte = pte_start + i;
676 		if (pte_val(*pte))
677 			return;
678 	}
679 
680 	/* free a pte talbe */
681 	free_pagetable(pmd_page(*pmd), 0);
682 	spin_lock(&init_mm.page_table_lock);
683 	pmd_clear(pmd);
684 	spin_unlock(&init_mm.page_table_lock);
685 }
686 
687 static void __meminit free_pmd_table(pmd_t *pmd_start, pud_t *pud)
688 {
689 	pmd_t *pmd;
690 	int i;
691 
692 	for (i = 0; i < PTRS_PER_PMD; i++) {
693 		pmd = pmd_start + i;
694 		if (pmd_val(*pmd))
695 			return;
696 	}
697 
698 	/* free a pmd talbe */
699 	free_pagetable(pud_page(*pud), 0);
700 	spin_lock(&init_mm.page_table_lock);
701 	pud_clear(pud);
702 	spin_unlock(&init_mm.page_table_lock);
703 }
704 
705 /* Return true if pgd is changed, otherwise return false. */
706 static bool __meminit free_pud_table(pud_t *pud_start, pgd_t *pgd)
707 {
708 	pud_t *pud;
709 	int i;
710 
711 	for (i = 0; i < PTRS_PER_PUD; i++) {
712 		pud = pud_start + i;
713 		if (pud_val(*pud))
714 			return false;
715 	}
716 
717 	/* free a pud table */
718 	free_pagetable(pgd_page(*pgd), 0);
719 	spin_lock(&init_mm.page_table_lock);
720 	pgd_clear(pgd);
721 	spin_unlock(&init_mm.page_table_lock);
722 
723 	return true;
724 }
725 
726 static void __meminit
727 remove_pte_table(pte_t *pte_start, unsigned long addr, unsigned long end,
728 		 bool direct)
729 {
730 	unsigned long next, pages = 0;
731 	pte_t *pte;
732 	void *page_addr;
733 	phys_addr_t phys_addr;
734 
735 	pte = pte_start + pte_index(addr);
736 	for (; addr < end; addr = next, pte++) {
737 		next = (addr + PAGE_SIZE) & PAGE_MASK;
738 		if (next > end)
739 			next = end;
740 
741 		if (!pte_present(*pte))
742 			continue;
743 
744 		/*
745 		 * We mapped [0,1G) memory as identity mapping when
746 		 * initializing, in arch/x86/kernel/head_64.S. These
747 		 * pagetables cannot be removed.
748 		 */
749 		phys_addr = pte_val(*pte) + (addr & PAGE_MASK);
750 		if (phys_addr < (phys_addr_t)0x40000000)
751 			return;
752 
753 		if (PAGE_ALIGNED(addr) && PAGE_ALIGNED(next)) {
754 			/*
755 			 * Do not free direct mapping pages since they were
756 			 * freed when offlining, or simplely not in use.
757 			 */
758 			if (!direct)
759 				free_pagetable(pte_page(*pte), 0);
760 
761 			spin_lock(&init_mm.page_table_lock);
762 			pte_clear(&init_mm, addr, pte);
763 			spin_unlock(&init_mm.page_table_lock);
764 
765 			/* For non-direct mapping, pages means nothing. */
766 			pages++;
767 		} else {
768 			/*
769 			 * If we are here, we are freeing vmemmap pages since
770 			 * direct mapped memory ranges to be freed are aligned.
771 			 *
772 			 * If we are not removing the whole page, it means
773 			 * other page structs in this page are being used and
774 			 * we canot remove them. So fill the unused page_structs
775 			 * with 0xFD, and remove the page when it is wholly
776 			 * filled with 0xFD.
777 			 */
778 			memset((void *)addr, PAGE_INUSE, next - addr);
779 
780 			page_addr = page_address(pte_page(*pte));
781 			if (!memchr_inv(page_addr, PAGE_INUSE, PAGE_SIZE)) {
782 				free_pagetable(pte_page(*pte), 0);
783 
784 				spin_lock(&init_mm.page_table_lock);
785 				pte_clear(&init_mm, addr, pte);
786 				spin_unlock(&init_mm.page_table_lock);
787 			}
788 		}
789 	}
790 
791 	/* Call free_pte_table() in remove_pmd_table(). */
792 	flush_tlb_all();
793 	if (direct)
794 		update_page_count(PG_LEVEL_4K, -pages);
795 }
796 
797 static void __meminit
798 remove_pmd_table(pmd_t *pmd_start, unsigned long addr, unsigned long end,
799 		 bool direct)
800 {
801 	unsigned long next, pages = 0;
802 	pte_t *pte_base;
803 	pmd_t *pmd;
804 	void *page_addr;
805 
806 	pmd = pmd_start + pmd_index(addr);
807 	for (; addr < end; addr = next, pmd++) {
808 		next = pmd_addr_end(addr, end);
809 
810 		if (!pmd_present(*pmd))
811 			continue;
812 
813 		if (pmd_large(*pmd)) {
814 			if (IS_ALIGNED(addr, PMD_SIZE) &&
815 			    IS_ALIGNED(next, PMD_SIZE)) {
816 				if (!direct)
817 					free_pagetable(pmd_page(*pmd),
818 						       get_order(PMD_SIZE));
819 
820 				spin_lock(&init_mm.page_table_lock);
821 				pmd_clear(pmd);
822 				spin_unlock(&init_mm.page_table_lock);
823 				pages++;
824 			} else {
825 				/* If here, we are freeing vmemmap pages. */
826 				memset((void *)addr, PAGE_INUSE, next - addr);
827 
828 				page_addr = page_address(pmd_page(*pmd));
829 				if (!memchr_inv(page_addr, PAGE_INUSE,
830 						PMD_SIZE)) {
831 					free_pagetable(pmd_page(*pmd),
832 						       get_order(PMD_SIZE));
833 
834 					spin_lock(&init_mm.page_table_lock);
835 					pmd_clear(pmd);
836 					spin_unlock(&init_mm.page_table_lock);
837 				}
838 			}
839 
840 			continue;
841 		}
842 
843 		pte_base = (pte_t *)pmd_page_vaddr(*pmd);
844 		remove_pte_table(pte_base, addr, next, direct);
845 		free_pte_table(pte_base, pmd);
846 	}
847 
848 	/* Call free_pmd_table() in remove_pud_table(). */
849 	if (direct)
850 		update_page_count(PG_LEVEL_2M, -pages);
851 }
852 
853 static void __meminit
854 remove_pud_table(pud_t *pud_start, unsigned long addr, unsigned long end,
855 		 bool direct)
856 {
857 	unsigned long next, pages = 0;
858 	pmd_t *pmd_base;
859 	pud_t *pud;
860 	void *page_addr;
861 
862 	pud = pud_start + pud_index(addr);
863 	for (; addr < end; addr = next, pud++) {
864 		next = pud_addr_end(addr, end);
865 
866 		if (!pud_present(*pud))
867 			continue;
868 
869 		if (pud_large(*pud)) {
870 			if (IS_ALIGNED(addr, PUD_SIZE) &&
871 			    IS_ALIGNED(next, PUD_SIZE)) {
872 				if (!direct)
873 					free_pagetable(pud_page(*pud),
874 						       get_order(PUD_SIZE));
875 
876 				spin_lock(&init_mm.page_table_lock);
877 				pud_clear(pud);
878 				spin_unlock(&init_mm.page_table_lock);
879 				pages++;
880 			} else {
881 				/* If here, we are freeing vmemmap pages. */
882 				memset((void *)addr, PAGE_INUSE, next - addr);
883 
884 				page_addr = page_address(pud_page(*pud));
885 				if (!memchr_inv(page_addr, PAGE_INUSE,
886 						PUD_SIZE)) {
887 					free_pagetable(pud_page(*pud),
888 						       get_order(PUD_SIZE));
889 
890 					spin_lock(&init_mm.page_table_lock);
891 					pud_clear(pud);
892 					spin_unlock(&init_mm.page_table_lock);
893 				}
894 			}
895 
896 			continue;
897 		}
898 
899 		pmd_base = (pmd_t *)pud_page_vaddr(*pud);
900 		remove_pmd_table(pmd_base, addr, next, direct);
901 		free_pmd_table(pmd_base, pud);
902 	}
903 
904 	if (direct)
905 		update_page_count(PG_LEVEL_1G, -pages);
906 }
907 
908 /* start and end are both virtual address. */
909 static void __meminit
910 remove_pagetable(unsigned long start, unsigned long end, bool direct)
911 {
912 	unsigned long next;
913 	unsigned long addr;
914 	pgd_t *pgd;
915 	pud_t *pud;
916 	bool pgd_changed = false;
917 
918 	for (addr = start; addr < end; addr = next) {
919 		next = pgd_addr_end(addr, end);
920 
921 		pgd = pgd_offset_k(addr);
922 		if (!pgd_present(*pgd))
923 			continue;
924 
925 		pud = (pud_t *)pgd_page_vaddr(*pgd);
926 		remove_pud_table(pud, addr, next, direct);
927 		if (free_pud_table(pud, pgd))
928 			pgd_changed = true;
929 	}
930 
931 	if (pgd_changed)
932 		sync_global_pgds(start, end - 1, 1);
933 
934 	flush_tlb_all();
935 }
936 
937 void __ref vmemmap_free(unsigned long start, unsigned long end)
938 {
939 	remove_pagetable(start, end, false);
940 }
941 
942 #ifdef CONFIG_MEMORY_HOTREMOVE
943 static void __meminit
944 kernel_physical_mapping_remove(unsigned long start, unsigned long end)
945 {
946 	start = (unsigned long)__va(start);
947 	end = (unsigned long)__va(end);
948 
949 	remove_pagetable(start, end, true);
950 }
951 
952 int __ref arch_remove_memory(u64 start, u64 size)
953 {
954 	unsigned long start_pfn = start >> PAGE_SHIFT;
955 	unsigned long nr_pages = size >> PAGE_SHIFT;
956 	struct page *page = pfn_to_page(start_pfn);
957 	struct vmem_altmap *altmap;
958 	struct zone *zone;
959 	int ret;
960 
961 	/* With altmap the first mapped page is offset from @start */
962 	altmap = to_vmem_altmap((unsigned long) page);
963 	if (altmap)
964 		page += vmem_altmap_offset(altmap);
965 	zone = page_zone(page);
966 	ret = __remove_pages(zone, start_pfn, nr_pages);
967 	WARN_ON_ONCE(ret);
968 	kernel_physical_mapping_remove(start, start + size);
969 
970 	return ret;
971 }
972 #endif
973 #endif /* CONFIG_MEMORY_HOTPLUG */
974 
975 static struct kcore_list kcore_vsyscall;
976 
977 static void __init register_page_bootmem_info(void)
978 {
979 #ifdef CONFIG_NUMA
980 	int i;
981 
982 	for_each_online_node(i)
983 		register_page_bootmem_info_node(NODE_DATA(i));
984 #endif
985 }
986 
987 void __init mem_init(void)
988 {
989 	pci_iommu_alloc();
990 
991 	/* clear_bss() already clear the empty_zero_page */
992 
993 	register_page_bootmem_info();
994 
995 	/* this will put all memory onto the freelists */
996 	free_all_bootmem();
997 	after_bootmem = 1;
998 
999 	/* Register memory areas for /proc/kcore */
1000 	kclist_add(&kcore_vsyscall, (void *)VSYSCALL_ADDR,
1001 			 PAGE_SIZE, KCORE_OTHER);
1002 
1003 	mem_init_print_info(NULL);
1004 }
1005 
1006 const int rodata_test_data = 0xC3;
1007 EXPORT_SYMBOL_GPL(rodata_test_data);
1008 
1009 int kernel_set_to_readonly;
1010 
1011 void set_kernel_text_rw(void)
1012 {
1013 	unsigned long start = PFN_ALIGN(_text);
1014 	unsigned long end = PFN_ALIGN(__stop___ex_table);
1015 
1016 	if (!kernel_set_to_readonly)
1017 		return;
1018 
1019 	pr_debug("Set kernel text: %lx - %lx for read write\n",
1020 		 start, end);
1021 
1022 	/*
1023 	 * Make the kernel identity mapping for text RW. Kernel text
1024 	 * mapping will always be RO. Refer to the comment in
1025 	 * static_protections() in pageattr.c
1026 	 */
1027 	set_memory_rw(start, (end - start) >> PAGE_SHIFT);
1028 }
1029 
1030 void set_kernel_text_ro(void)
1031 {
1032 	unsigned long start = PFN_ALIGN(_text);
1033 	unsigned long end = PFN_ALIGN(__stop___ex_table);
1034 
1035 	if (!kernel_set_to_readonly)
1036 		return;
1037 
1038 	pr_debug("Set kernel text: %lx - %lx for read only\n",
1039 		 start, end);
1040 
1041 	/*
1042 	 * Set the kernel identity mapping for text RO.
1043 	 */
1044 	set_memory_ro(start, (end - start) >> PAGE_SHIFT);
1045 }
1046 
1047 void mark_rodata_ro(void)
1048 {
1049 	unsigned long start = PFN_ALIGN(_text);
1050 	unsigned long rodata_start = PFN_ALIGN(__start_rodata);
1051 	unsigned long end = (unsigned long) &__end_rodata_hpage_align;
1052 	unsigned long text_end = PFN_ALIGN(&__stop___ex_table);
1053 	unsigned long rodata_end = PFN_ALIGN(&__end_rodata);
1054 	unsigned long all_end;
1055 
1056 	printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
1057 	       (end - start) >> 10);
1058 	set_memory_ro(start, (end - start) >> PAGE_SHIFT);
1059 
1060 	kernel_set_to_readonly = 1;
1061 
1062 	/*
1063 	 * The rodata/data/bss/brk section (but not the kernel text!)
1064 	 * should also be not-executable.
1065 	 *
1066 	 * We align all_end to PMD_SIZE because the existing mapping
1067 	 * is a full PMD. If we would align _brk_end to PAGE_SIZE we
1068 	 * split the PMD and the reminder between _brk_end and the end
1069 	 * of the PMD will remain mapped executable.
1070 	 *
1071 	 * Any PMD which was setup after the one which covers _brk_end
1072 	 * has been zapped already via cleanup_highmem().
1073 	 */
1074 	all_end = roundup((unsigned long)_brk_end, PMD_SIZE);
1075 	set_memory_nx(text_end, (all_end - text_end) >> PAGE_SHIFT);
1076 
1077 	rodata_test();
1078 
1079 #ifdef CONFIG_CPA_DEBUG
1080 	printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end);
1081 	set_memory_rw(start, (end-start) >> PAGE_SHIFT);
1082 
1083 	printk(KERN_INFO "Testing CPA: again\n");
1084 	set_memory_ro(start, (end-start) >> PAGE_SHIFT);
1085 #endif
1086 
1087 	free_init_pages("unused kernel",
1088 			(unsigned long) __va(__pa_symbol(text_end)),
1089 			(unsigned long) __va(__pa_symbol(rodata_start)));
1090 	free_init_pages("unused kernel",
1091 			(unsigned long) __va(__pa_symbol(rodata_end)),
1092 			(unsigned long) __va(__pa_symbol(_sdata)));
1093 
1094 	debug_checkwx();
1095 }
1096 
1097 int kern_addr_valid(unsigned long addr)
1098 {
1099 	unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
1100 	pgd_t *pgd;
1101 	pud_t *pud;
1102 	pmd_t *pmd;
1103 	pte_t *pte;
1104 
1105 	if (above != 0 && above != -1UL)
1106 		return 0;
1107 
1108 	pgd = pgd_offset_k(addr);
1109 	if (pgd_none(*pgd))
1110 		return 0;
1111 
1112 	pud = pud_offset(pgd, addr);
1113 	if (pud_none(*pud))
1114 		return 0;
1115 
1116 	if (pud_large(*pud))
1117 		return pfn_valid(pud_pfn(*pud));
1118 
1119 	pmd = pmd_offset(pud, addr);
1120 	if (pmd_none(*pmd))
1121 		return 0;
1122 
1123 	if (pmd_large(*pmd))
1124 		return pfn_valid(pmd_pfn(*pmd));
1125 
1126 	pte = pte_offset_kernel(pmd, addr);
1127 	if (pte_none(*pte))
1128 		return 0;
1129 
1130 	return pfn_valid(pte_pfn(*pte));
1131 }
1132 
1133 static unsigned long probe_memory_block_size(void)
1134 {
1135 	unsigned long bz = MIN_MEMORY_BLOCK_SIZE;
1136 
1137 	/* if system is UV or has 64GB of RAM or more, use large blocks */
1138 	if (is_uv_system() || ((max_pfn << PAGE_SHIFT) >= (64UL << 30)))
1139 		bz = 2UL << 30; /* 2GB */
1140 
1141 	pr_info("x86/mm: Memory block size: %ldMB\n", bz >> 20);
1142 
1143 	return bz;
1144 }
1145 
1146 static unsigned long memory_block_size_probed;
1147 unsigned long memory_block_size_bytes(void)
1148 {
1149 	if (!memory_block_size_probed)
1150 		memory_block_size_probed = probe_memory_block_size();
1151 
1152 	return memory_block_size_probed;
1153 }
1154 
1155 #ifdef CONFIG_SPARSEMEM_VMEMMAP
1156 /*
1157  * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
1158  */
1159 static long __meminitdata addr_start, addr_end;
1160 static void __meminitdata *p_start, *p_end;
1161 static int __meminitdata node_start;
1162 
1163 static int __meminit vmemmap_populate_hugepages(unsigned long start,
1164 		unsigned long end, int node, struct vmem_altmap *altmap)
1165 {
1166 	unsigned long addr;
1167 	unsigned long next;
1168 	pgd_t *pgd;
1169 	pud_t *pud;
1170 	pmd_t *pmd;
1171 
1172 	for (addr = start; addr < end; addr = next) {
1173 		next = pmd_addr_end(addr, end);
1174 
1175 		pgd = vmemmap_pgd_populate(addr, node);
1176 		if (!pgd)
1177 			return -ENOMEM;
1178 
1179 		pud = vmemmap_pud_populate(pgd, addr, node);
1180 		if (!pud)
1181 			return -ENOMEM;
1182 
1183 		pmd = pmd_offset(pud, addr);
1184 		if (pmd_none(*pmd)) {
1185 			void *p;
1186 
1187 			p = __vmemmap_alloc_block_buf(PMD_SIZE, node, altmap);
1188 			if (p) {
1189 				pte_t entry;
1190 
1191 				entry = pfn_pte(__pa(p) >> PAGE_SHIFT,
1192 						PAGE_KERNEL_LARGE);
1193 				set_pmd(pmd, __pmd(pte_val(entry)));
1194 
1195 				/* check to see if we have contiguous blocks */
1196 				if (p_end != p || node_start != node) {
1197 					if (p_start)
1198 						pr_debug(" [%lx-%lx] PMD -> [%p-%p] on node %d\n",
1199 						       addr_start, addr_end-1, p_start, p_end-1, node_start);
1200 					addr_start = addr;
1201 					node_start = node;
1202 					p_start = p;
1203 				}
1204 
1205 				addr_end = addr + PMD_SIZE;
1206 				p_end = p + PMD_SIZE;
1207 				continue;
1208 			} else if (altmap)
1209 				return -ENOMEM; /* no fallback */
1210 		} else if (pmd_large(*pmd)) {
1211 			vmemmap_verify((pte_t *)pmd, node, addr, next);
1212 			continue;
1213 		}
1214 		pr_warn_once("vmemmap: falling back to regular page backing\n");
1215 		if (vmemmap_populate_basepages(addr, next, node))
1216 			return -ENOMEM;
1217 	}
1218 	return 0;
1219 }
1220 
1221 int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node)
1222 {
1223 	struct vmem_altmap *altmap = to_vmem_altmap(start);
1224 	int err;
1225 
1226 	if (boot_cpu_has(X86_FEATURE_PSE))
1227 		err = vmemmap_populate_hugepages(start, end, node, altmap);
1228 	else if (altmap) {
1229 		pr_err_once("%s: no cpu support for altmap allocations\n",
1230 				__func__);
1231 		err = -ENOMEM;
1232 	} else
1233 		err = vmemmap_populate_basepages(start, end, node);
1234 	if (!err)
1235 		sync_global_pgds(start, end - 1, 0);
1236 	return err;
1237 }
1238 
1239 #if defined(CONFIG_MEMORY_HOTPLUG_SPARSE) && defined(CONFIG_HAVE_BOOTMEM_INFO_NODE)
1240 void register_page_bootmem_memmap(unsigned long section_nr,
1241 				  struct page *start_page, unsigned long size)
1242 {
1243 	unsigned long addr = (unsigned long)start_page;
1244 	unsigned long end = (unsigned long)(start_page + size);
1245 	unsigned long next;
1246 	pgd_t *pgd;
1247 	pud_t *pud;
1248 	pmd_t *pmd;
1249 	unsigned int nr_pages;
1250 	struct page *page;
1251 
1252 	for (; addr < end; addr = next) {
1253 		pte_t *pte = NULL;
1254 
1255 		pgd = pgd_offset_k(addr);
1256 		if (pgd_none(*pgd)) {
1257 			next = (addr + PAGE_SIZE) & PAGE_MASK;
1258 			continue;
1259 		}
1260 		get_page_bootmem(section_nr, pgd_page(*pgd), MIX_SECTION_INFO);
1261 
1262 		pud = pud_offset(pgd, addr);
1263 		if (pud_none(*pud)) {
1264 			next = (addr + PAGE_SIZE) & PAGE_MASK;
1265 			continue;
1266 		}
1267 		get_page_bootmem(section_nr, pud_page(*pud), MIX_SECTION_INFO);
1268 
1269 		if (!boot_cpu_has(X86_FEATURE_PSE)) {
1270 			next = (addr + PAGE_SIZE) & PAGE_MASK;
1271 			pmd = pmd_offset(pud, addr);
1272 			if (pmd_none(*pmd))
1273 				continue;
1274 			get_page_bootmem(section_nr, pmd_page(*pmd),
1275 					 MIX_SECTION_INFO);
1276 
1277 			pte = pte_offset_kernel(pmd, addr);
1278 			if (pte_none(*pte))
1279 				continue;
1280 			get_page_bootmem(section_nr, pte_page(*pte),
1281 					 SECTION_INFO);
1282 		} else {
1283 			next = pmd_addr_end(addr, end);
1284 
1285 			pmd = pmd_offset(pud, addr);
1286 			if (pmd_none(*pmd))
1287 				continue;
1288 
1289 			nr_pages = 1 << (get_order(PMD_SIZE));
1290 			page = pmd_page(*pmd);
1291 			while (nr_pages--)
1292 				get_page_bootmem(section_nr, page++,
1293 						 SECTION_INFO);
1294 		}
1295 	}
1296 }
1297 #endif
1298 
1299 void __meminit vmemmap_populate_print_last(void)
1300 {
1301 	if (p_start) {
1302 		pr_debug(" [%lx-%lx] PMD -> [%p-%p] on node %d\n",
1303 			addr_start, addr_end-1, p_start, p_end-1, node_start);
1304 		p_start = NULL;
1305 		p_end = NULL;
1306 		node_start = 0;
1307 	}
1308 }
1309 #endif
1310