Linux 4.11-rc5
[linux/fpc-iii.git] / arch / x86 / mm / init_64.c
blob15173d37f399610caf8969fa942b17b175dd466b
1 /*
2 * linux/arch/x86_64/mm/init.c
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 */
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/memory.h>
31 #include <linux/memory_hotplug.h>
32 #include <linux/memremap.h>
33 #include <linux/nmi.h>
34 #include <linux/gfp.h>
35 #include <linux/kcore.h>
37 #include <asm/processor.h>
38 #include <asm/bios_ebda.h>
39 #include <linux/uaccess.h>
40 #include <asm/pgtable.h>
41 #include <asm/pgalloc.h>
42 #include <asm/dma.h>
43 #include <asm/fixmap.h>
44 #include <asm/e820.h>
45 #include <asm/apic.h>
46 #include <asm/tlb.h>
47 #include <asm/mmu_context.h>
48 #include <asm/proto.h>
49 #include <asm/smp.h>
50 #include <asm/sections.h>
51 #include <asm/kdebug.h>
52 #include <asm/numa.h>
53 #include <asm/cacheflush.h>
54 #include <asm/init.h>
55 #include <asm/uv/uv.h>
56 #include <asm/setup.h>
58 #include "mm_internal.h"
60 #include "ident_map.c"
63 * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the
64 * physical space so we can cache the place of the first one and move
65 * around without checking the pgd every time.
68 pteval_t __supported_pte_mask __read_mostly = ~0;
69 EXPORT_SYMBOL_GPL(__supported_pte_mask);
71 int force_personality32;
74 * noexec32=on|off
75 * Control non executable heap for 32bit processes.
76 * To control the stack too use noexec=off
78 * on PROT_READ does not imply PROT_EXEC for 32-bit processes (default)
79 * off PROT_READ implies PROT_EXEC
81 static int __init nonx32_setup(char *str)
83 if (!strcmp(str, "on"))
84 force_personality32 &= ~READ_IMPLIES_EXEC;
85 else if (!strcmp(str, "off"))
86 force_personality32 |= READ_IMPLIES_EXEC;
87 return 1;
89 __setup("noexec32=", nonx32_setup);
92 * When memory was added make sure all the processes MM have
93 * suitable PGD entries in the local PGD level page.
95 void sync_global_pgds(unsigned long start, unsigned long end)
97 unsigned long address;
99 for (address = start; address <= end; address += PGDIR_SIZE) {
100 const pgd_t *pgd_ref = pgd_offset_k(address);
101 struct page *page;
103 if (pgd_none(*pgd_ref))
104 continue;
106 spin_lock(&pgd_lock);
107 list_for_each_entry(page, &pgd_list, lru) {
108 pgd_t *pgd;
109 spinlock_t *pgt_lock;
111 pgd = (pgd_t *)page_address(page) + pgd_index(address);
112 /* the pgt_lock only for Xen */
113 pgt_lock = &pgd_page_get_mm(page)->page_table_lock;
114 spin_lock(pgt_lock);
116 if (!pgd_none(*pgd_ref) && !pgd_none(*pgd))
117 BUG_ON(pgd_page_vaddr(*pgd)
118 != pgd_page_vaddr(*pgd_ref));
120 if (pgd_none(*pgd))
121 set_pgd(pgd, *pgd_ref);
123 spin_unlock(pgt_lock);
125 spin_unlock(&pgd_lock);
130 * NOTE: This function is marked __ref because it calls __init function
131 * (alloc_bootmem_pages). It's safe to do it ONLY when after_bootmem == 0.
133 static __ref void *spp_getpage(void)
135 void *ptr;
137 if (after_bootmem)
138 ptr = (void *) get_zeroed_page(GFP_ATOMIC | __GFP_NOTRACK);
139 else
140 ptr = alloc_bootmem_pages(PAGE_SIZE);
142 if (!ptr || ((unsigned long)ptr & ~PAGE_MASK)) {
143 panic("set_pte_phys: cannot allocate page data %s\n",
144 after_bootmem ? "after bootmem" : "");
147 pr_debug("spp_getpage %p\n", ptr);
149 return ptr;
152 static pud_t *fill_pud(pgd_t *pgd, unsigned long vaddr)
154 if (pgd_none(*pgd)) {
155 pud_t *pud = (pud_t *)spp_getpage();
156 pgd_populate(&init_mm, pgd, pud);
157 if (pud != pud_offset(pgd, 0))
158 printk(KERN_ERR "PAGETABLE BUG #00! %p <-> %p\n",
159 pud, pud_offset(pgd, 0));
161 return pud_offset(pgd, vaddr);
164 static pmd_t *fill_pmd(pud_t *pud, unsigned long vaddr)
166 if (pud_none(*pud)) {
167 pmd_t *pmd = (pmd_t *) spp_getpage();
168 pud_populate(&init_mm, pud, pmd);
169 if (pmd != pmd_offset(pud, 0))
170 printk(KERN_ERR "PAGETABLE BUG #01! %p <-> %p\n",
171 pmd, pmd_offset(pud, 0));
173 return pmd_offset(pud, vaddr);
176 static pte_t *fill_pte(pmd_t *pmd, unsigned long vaddr)
178 if (pmd_none(*pmd)) {
179 pte_t *pte = (pte_t *) spp_getpage();
180 pmd_populate_kernel(&init_mm, pmd, pte);
181 if (pte != pte_offset_kernel(pmd, 0))
182 printk(KERN_ERR "PAGETABLE BUG #02!\n");
184 return pte_offset_kernel(pmd, vaddr);
187 void set_pte_vaddr_pud(pud_t *pud_page, unsigned long vaddr, pte_t new_pte)
189 pud_t *pud;
190 pmd_t *pmd;
191 pte_t *pte;
193 pud = pud_page + pud_index(vaddr);
194 pmd = fill_pmd(pud, vaddr);
195 pte = fill_pte(pmd, vaddr);
197 set_pte(pte, new_pte);
200 * It's enough to flush this one mapping.
201 * (PGE mappings get flushed as well)
203 __flush_tlb_one(vaddr);
206 void set_pte_vaddr(unsigned long vaddr, pte_t pteval)
208 pgd_t *pgd;
209 pud_t *pud_page;
211 pr_debug("set_pte_vaddr %lx to %lx\n", vaddr, native_pte_val(pteval));
213 pgd = pgd_offset_k(vaddr);
214 if (pgd_none(*pgd)) {
215 printk(KERN_ERR
216 "PGD FIXMAP MISSING, it should be setup in head.S!\n");
217 return;
219 pud_page = (pud_t*)pgd_page_vaddr(*pgd);
220 set_pte_vaddr_pud(pud_page, vaddr, pteval);
223 pmd_t * __init populate_extra_pmd(unsigned long vaddr)
225 pgd_t *pgd;
226 pud_t *pud;
228 pgd = pgd_offset_k(vaddr);
229 pud = fill_pud(pgd, vaddr);
230 return fill_pmd(pud, vaddr);
233 pte_t * __init populate_extra_pte(unsigned long vaddr)
235 pmd_t *pmd;
237 pmd = populate_extra_pmd(vaddr);
238 return fill_pte(pmd, vaddr);
242 * Create large page table mappings for a range of physical addresses.
244 static void __init __init_extra_mapping(unsigned long phys, unsigned long size,
245 enum page_cache_mode cache)
247 pgd_t *pgd;
248 pud_t *pud;
249 pmd_t *pmd;
250 pgprot_t prot;
252 pgprot_val(prot) = pgprot_val(PAGE_KERNEL_LARGE) |
253 pgprot_val(pgprot_4k_2_large(cachemode2pgprot(cache)));
254 BUG_ON((phys & ~PMD_MASK) || (size & ~PMD_MASK));
255 for (; size; phys += PMD_SIZE, size -= PMD_SIZE) {
256 pgd = pgd_offset_k((unsigned long)__va(phys));
257 if (pgd_none(*pgd)) {
258 pud = (pud_t *) spp_getpage();
259 set_pgd(pgd, __pgd(__pa(pud) | _KERNPG_TABLE |
260 _PAGE_USER));
262 pud = pud_offset(pgd, (unsigned long)__va(phys));
263 if (pud_none(*pud)) {
264 pmd = (pmd_t *) spp_getpage();
265 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE |
266 _PAGE_USER));
268 pmd = pmd_offset(pud, phys);
269 BUG_ON(!pmd_none(*pmd));
270 set_pmd(pmd, __pmd(phys | pgprot_val(prot)));
274 void __init init_extra_mapping_wb(unsigned long phys, unsigned long size)
276 __init_extra_mapping(phys, size, _PAGE_CACHE_MODE_WB);
279 void __init init_extra_mapping_uc(unsigned long phys, unsigned long size)
281 __init_extra_mapping(phys, size, _PAGE_CACHE_MODE_UC);
285 * The head.S code sets up the kernel high mapping:
287 * from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text)
289 * phys_base holds the negative offset to the kernel, which is added
290 * to the compile time generated pmds. This results in invalid pmds up
291 * to the point where we hit the physaddr 0 mapping.
293 * We limit the mappings to the region from _text to _brk_end. _brk_end
294 * is rounded up to the 2MB boundary. This catches the invalid pmds as
295 * well, as they are located before _text:
297 void __init cleanup_highmap(void)
299 unsigned long vaddr = __START_KERNEL_map;
300 unsigned long vaddr_end = __START_KERNEL_map + KERNEL_IMAGE_SIZE;
301 unsigned long end = roundup((unsigned long)_brk_end, PMD_SIZE) - 1;
302 pmd_t *pmd = level2_kernel_pgt;
305 * Native path, max_pfn_mapped is not set yet.
306 * Xen has valid max_pfn_mapped set in
307 * arch/x86/xen/mmu.c:xen_setup_kernel_pagetable().
309 if (max_pfn_mapped)
310 vaddr_end = __START_KERNEL_map + (max_pfn_mapped << PAGE_SHIFT);
312 for (; vaddr + PMD_SIZE - 1 < vaddr_end; pmd++, vaddr += PMD_SIZE) {
313 if (pmd_none(*pmd))
314 continue;
315 if (vaddr < (unsigned long) _text || vaddr > end)
316 set_pmd(pmd, __pmd(0));
321 * Create PTE level page table mapping for physical addresses.
322 * It returns the last physical address mapped.
324 static unsigned long __meminit
325 phys_pte_init(pte_t *pte_page, unsigned long paddr, unsigned long paddr_end,
326 pgprot_t prot)
328 unsigned long pages = 0, paddr_next;
329 unsigned long paddr_last = paddr_end;
330 pte_t *pte;
331 int i;
333 pte = pte_page + pte_index(paddr);
334 i = pte_index(paddr);
336 for (; i < PTRS_PER_PTE; i++, paddr = paddr_next, pte++) {
337 paddr_next = (paddr & PAGE_MASK) + PAGE_SIZE;
338 if (paddr >= paddr_end) {
339 if (!after_bootmem &&
340 !e820_any_mapped(paddr & PAGE_MASK, paddr_next,
341 E820_RAM) &&
342 !e820_any_mapped(paddr & PAGE_MASK, paddr_next,
343 E820_RESERVED_KERN))
344 set_pte(pte, __pte(0));
345 continue;
349 * We will re-use the existing mapping.
350 * Xen for example has some special requirements, like mapping
351 * pagetable pages as RO. So assume someone who pre-setup
352 * these mappings are more intelligent.
354 if (!pte_none(*pte)) {
355 if (!after_bootmem)
356 pages++;
357 continue;
360 if (0)
361 pr_info(" pte=%p addr=%lx pte=%016lx\n", pte, paddr,
362 pfn_pte(paddr >> PAGE_SHIFT, PAGE_KERNEL).pte);
363 pages++;
364 set_pte(pte, pfn_pte(paddr >> PAGE_SHIFT, prot));
365 paddr_last = (paddr & PAGE_MASK) + PAGE_SIZE;
368 update_page_count(PG_LEVEL_4K, pages);
370 return paddr_last;
374 * Create PMD level page table mapping for physical addresses. The virtual
375 * and physical address have to be aligned at this level.
376 * It returns the last physical address mapped.
378 static unsigned long __meminit
379 phys_pmd_init(pmd_t *pmd_page, unsigned long paddr, unsigned long paddr_end,
380 unsigned long page_size_mask, pgprot_t prot)
382 unsigned long pages = 0, paddr_next;
383 unsigned long paddr_last = paddr_end;
385 int i = pmd_index(paddr);
387 for (; i < PTRS_PER_PMD; i++, paddr = paddr_next) {
388 pmd_t *pmd = pmd_page + pmd_index(paddr);
389 pte_t *pte;
390 pgprot_t new_prot = prot;
392 paddr_next = (paddr & PMD_MASK) + PMD_SIZE;
393 if (paddr >= paddr_end) {
394 if (!after_bootmem &&
395 !e820_any_mapped(paddr & PMD_MASK, paddr_next,
396 E820_RAM) &&
397 !e820_any_mapped(paddr & PMD_MASK, paddr_next,
398 E820_RESERVED_KERN))
399 set_pmd(pmd, __pmd(0));
400 continue;
403 if (!pmd_none(*pmd)) {
404 if (!pmd_large(*pmd)) {
405 spin_lock(&init_mm.page_table_lock);
406 pte = (pte_t *)pmd_page_vaddr(*pmd);
407 paddr_last = phys_pte_init(pte, paddr,
408 paddr_end, prot);
409 spin_unlock(&init_mm.page_table_lock);
410 continue;
413 * If we are ok with PG_LEVEL_2M mapping, then we will
414 * use the existing mapping,
416 * Otherwise, we will split the large page mapping but
417 * use the same existing protection bits except for
418 * large page, so that we don't violate Intel's TLB
419 * Application note (317080) which says, while changing
420 * the page sizes, new and old translations should
421 * not differ with respect to page frame and
422 * attributes.
424 if (page_size_mask & (1 << PG_LEVEL_2M)) {
425 if (!after_bootmem)
426 pages++;
427 paddr_last = paddr_next;
428 continue;
430 new_prot = pte_pgprot(pte_clrhuge(*(pte_t *)pmd));
433 if (page_size_mask & (1<<PG_LEVEL_2M)) {
434 pages++;
435 spin_lock(&init_mm.page_table_lock);
436 set_pte((pte_t *)pmd,
437 pfn_pte((paddr & PMD_MASK) >> PAGE_SHIFT,
438 __pgprot(pgprot_val(prot) | _PAGE_PSE)));
439 spin_unlock(&init_mm.page_table_lock);
440 paddr_last = paddr_next;
441 continue;
444 pte = alloc_low_page();
445 paddr_last = phys_pte_init(pte, paddr, paddr_end, new_prot);
447 spin_lock(&init_mm.page_table_lock);
448 pmd_populate_kernel(&init_mm, pmd, pte);
449 spin_unlock(&init_mm.page_table_lock);
451 update_page_count(PG_LEVEL_2M, pages);
452 return paddr_last;
456 * Create PUD level page table mapping for physical addresses. The virtual
457 * and physical address do not have to be aligned at this level. KASLR can
458 * randomize virtual addresses up to this level.
459 * It returns the last physical address mapped.
461 static unsigned long __meminit
462 phys_pud_init(pud_t *pud_page, unsigned long paddr, unsigned long paddr_end,
463 unsigned long page_size_mask)
465 unsigned long pages = 0, paddr_next;
466 unsigned long paddr_last = paddr_end;
467 unsigned long vaddr = (unsigned long)__va(paddr);
468 int i = pud_index(vaddr);
470 for (; i < PTRS_PER_PUD; i++, paddr = paddr_next) {
471 pud_t *pud;
472 pmd_t *pmd;
473 pgprot_t prot = PAGE_KERNEL;
475 vaddr = (unsigned long)__va(paddr);
476 pud = pud_page + pud_index(vaddr);
477 paddr_next = (paddr & PUD_MASK) + PUD_SIZE;
479 if (paddr >= paddr_end) {
480 if (!after_bootmem &&
481 !e820_any_mapped(paddr & PUD_MASK, paddr_next,
482 E820_RAM) &&
483 !e820_any_mapped(paddr & PUD_MASK, paddr_next,
484 E820_RESERVED_KERN))
485 set_pud(pud, __pud(0));
486 continue;
489 if (!pud_none(*pud)) {
490 if (!pud_large(*pud)) {
491 pmd = pmd_offset(pud, 0);
492 paddr_last = phys_pmd_init(pmd, paddr,
493 paddr_end,
494 page_size_mask,
495 prot);
496 __flush_tlb_all();
497 continue;
500 * If we are ok with PG_LEVEL_1G mapping, then we will
501 * use the existing mapping.
503 * Otherwise, we will split the gbpage mapping but use
504 * the same existing protection bits except for large
505 * page, so that we don't violate Intel's TLB
506 * Application note (317080) which says, while changing
507 * the page sizes, new and old translations should
508 * not differ with respect to page frame and
509 * attributes.
511 if (page_size_mask & (1 << PG_LEVEL_1G)) {
512 if (!after_bootmem)
513 pages++;
514 paddr_last = paddr_next;
515 continue;
517 prot = pte_pgprot(pte_clrhuge(*(pte_t *)pud));
520 if (page_size_mask & (1<<PG_LEVEL_1G)) {
521 pages++;
522 spin_lock(&init_mm.page_table_lock);
523 set_pte((pte_t *)pud,
524 pfn_pte((paddr & PUD_MASK) >> PAGE_SHIFT,
525 PAGE_KERNEL_LARGE));
526 spin_unlock(&init_mm.page_table_lock);
527 paddr_last = paddr_next;
528 continue;
531 pmd = alloc_low_page();
532 paddr_last = phys_pmd_init(pmd, paddr, paddr_end,
533 page_size_mask, prot);
535 spin_lock(&init_mm.page_table_lock);
536 pud_populate(&init_mm, pud, pmd);
537 spin_unlock(&init_mm.page_table_lock);
539 __flush_tlb_all();
541 update_page_count(PG_LEVEL_1G, pages);
543 return paddr_last;
547 * Create page table mapping for the physical memory for specific physical
548 * addresses. The virtual and physical addresses have to be aligned on PMD level
549 * down. It returns the last physical address mapped.
551 unsigned long __meminit
552 kernel_physical_mapping_init(unsigned long paddr_start,
553 unsigned long paddr_end,
554 unsigned long page_size_mask)
556 bool pgd_changed = false;
557 unsigned long vaddr, vaddr_start, vaddr_end, vaddr_next, paddr_last;
559 paddr_last = paddr_end;
560 vaddr = (unsigned long)__va(paddr_start);
561 vaddr_end = (unsigned long)__va(paddr_end);
562 vaddr_start = vaddr;
564 for (; vaddr < vaddr_end; vaddr = vaddr_next) {
565 pgd_t *pgd = pgd_offset_k(vaddr);
566 pud_t *pud;
568 vaddr_next = (vaddr & PGDIR_MASK) + PGDIR_SIZE;
570 if (pgd_val(*pgd)) {
571 pud = (pud_t *)pgd_page_vaddr(*pgd);
572 paddr_last = phys_pud_init(pud, __pa(vaddr),
573 __pa(vaddr_end),
574 page_size_mask);
575 continue;
578 pud = alloc_low_page();
579 paddr_last = phys_pud_init(pud, __pa(vaddr), __pa(vaddr_end),
580 page_size_mask);
582 spin_lock(&init_mm.page_table_lock);
583 pgd_populate(&init_mm, pgd, pud);
584 spin_unlock(&init_mm.page_table_lock);
585 pgd_changed = true;
588 if (pgd_changed)
589 sync_global_pgds(vaddr_start, vaddr_end - 1);
591 __flush_tlb_all();
593 return paddr_last;
596 #ifndef CONFIG_NUMA
597 void __init initmem_init(void)
599 memblock_set_node(0, (phys_addr_t)ULLONG_MAX, &memblock.memory, 0);
601 #endif
603 void __init paging_init(void)
605 sparse_memory_present_with_active_regions(MAX_NUMNODES);
606 sparse_init();
609 * clear the default setting with node 0
610 * note: don't use nodes_clear here, that is really clearing when
611 * numa support is not compiled in, and later node_set_state
612 * will not set it back.
614 node_clear_state(0, N_MEMORY);
615 if (N_MEMORY != N_NORMAL_MEMORY)
616 node_clear_state(0, N_NORMAL_MEMORY);
618 zone_sizes_init();
622 * Memory hotplug specific functions
624 #ifdef CONFIG_MEMORY_HOTPLUG
626 * After memory hotplug the variables max_pfn, max_low_pfn and high_memory need
627 * updating.
629 static void update_end_of_memory_vars(u64 start, u64 size)
631 unsigned long end_pfn = PFN_UP(start + size);
633 if (end_pfn > max_pfn) {
634 max_pfn = end_pfn;
635 max_low_pfn = end_pfn;
636 high_memory = (void *)__va(max_pfn * PAGE_SIZE - 1) + 1;
641 * Memory is added always to NORMAL zone. This means you will never get
642 * additional DMA/DMA32 memory.
644 int arch_add_memory(int nid, u64 start, u64 size, bool for_device)
646 struct pglist_data *pgdat = NODE_DATA(nid);
647 struct zone *zone = pgdat->node_zones +
648 zone_for_memory(nid, start, size, ZONE_NORMAL, for_device);
649 unsigned long start_pfn = start >> PAGE_SHIFT;
650 unsigned long nr_pages = size >> PAGE_SHIFT;
651 int ret;
653 init_memory_mapping(start, start + size);
655 ret = __add_pages(nid, zone, start_pfn, nr_pages);
656 WARN_ON_ONCE(ret);
658 /* update max_pfn, max_low_pfn and high_memory */
659 update_end_of_memory_vars(start, size);
661 return ret;
663 EXPORT_SYMBOL_GPL(arch_add_memory);
665 #define PAGE_INUSE 0xFD
667 static void __meminit free_pagetable(struct page *page, int order)
669 unsigned long magic;
670 unsigned int nr_pages = 1 << order;
671 struct vmem_altmap *altmap = to_vmem_altmap((unsigned long) page);
673 if (altmap) {
674 vmem_altmap_free(altmap, nr_pages);
675 return;
678 /* bootmem page has reserved flag */
679 if (PageReserved(page)) {
680 __ClearPageReserved(page);
682 magic = (unsigned long)page->freelist;
683 if (magic == SECTION_INFO || magic == MIX_SECTION_INFO) {
684 while (nr_pages--)
685 put_page_bootmem(page++);
686 } else
687 while (nr_pages--)
688 free_reserved_page(page++);
689 } else
690 free_pages((unsigned long)page_address(page), order);
693 static void __meminit free_pte_table(pte_t *pte_start, pmd_t *pmd)
695 pte_t *pte;
696 int i;
698 for (i = 0; i < PTRS_PER_PTE; i++) {
699 pte = pte_start + i;
700 if (!pte_none(*pte))
701 return;
704 /* free a pte talbe */
705 free_pagetable(pmd_page(*pmd), 0);
706 spin_lock(&init_mm.page_table_lock);
707 pmd_clear(pmd);
708 spin_unlock(&init_mm.page_table_lock);
711 static void __meminit free_pmd_table(pmd_t *pmd_start, pud_t *pud)
713 pmd_t *pmd;
714 int i;
716 for (i = 0; i < PTRS_PER_PMD; i++) {
717 pmd = pmd_start + i;
718 if (!pmd_none(*pmd))
719 return;
722 /* free a pmd talbe */
723 free_pagetable(pud_page(*pud), 0);
724 spin_lock(&init_mm.page_table_lock);
725 pud_clear(pud);
726 spin_unlock(&init_mm.page_table_lock);
729 static void __meminit
730 remove_pte_table(pte_t *pte_start, unsigned long addr, unsigned long end,
731 bool direct)
733 unsigned long next, pages = 0;
734 pte_t *pte;
735 void *page_addr;
736 phys_addr_t phys_addr;
738 pte = pte_start + pte_index(addr);
739 for (; addr < end; addr = next, pte++) {
740 next = (addr + PAGE_SIZE) & PAGE_MASK;
741 if (next > end)
742 next = end;
744 if (!pte_present(*pte))
745 continue;
748 * We mapped [0,1G) memory as identity mapping when
749 * initializing, in arch/x86/kernel/head_64.S. These
750 * pagetables cannot be removed.
752 phys_addr = pte_val(*pte) + (addr & PAGE_MASK);
753 if (phys_addr < (phys_addr_t)0x40000000)
754 return;
756 if (PAGE_ALIGNED(addr) && PAGE_ALIGNED(next)) {
758 * Do not free direct mapping pages since they were
759 * freed when offlining, or simplely not in use.
761 if (!direct)
762 free_pagetable(pte_page(*pte), 0);
764 spin_lock(&init_mm.page_table_lock);
765 pte_clear(&init_mm, addr, pte);
766 spin_unlock(&init_mm.page_table_lock);
768 /* For non-direct mapping, pages means nothing. */
769 pages++;
770 } else {
772 * If we are here, we are freeing vmemmap pages since
773 * direct mapped memory ranges to be freed are aligned.
775 * If we are not removing the whole page, it means
776 * other page structs in this page are being used and
777 * we canot remove them. So fill the unused page_structs
778 * with 0xFD, and remove the page when it is wholly
779 * filled with 0xFD.
781 memset((void *)addr, PAGE_INUSE, next - addr);
783 page_addr = page_address(pte_page(*pte));
784 if (!memchr_inv(page_addr, PAGE_INUSE, PAGE_SIZE)) {
785 free_pagetable(pte_page(*pte), 0);
787 spin_lock(&init_mm.page_table_lock);
788 pte_clear(&init_mm, addr, pte);
789 spin_unlock(&init_mm.page_table_lock);
794 /* Call free_pte_table() in remove_pmd_table(). */
795 flush_tlb_all();
796 if (direct)
797 update_page_count(PG_LEVEL_4K, -pages);
800 static void __meminit
801 remove_pmd_table(pmd_t *pmd_start, unsigned long addr, unsigned long end,
802 bool direct)
804 unsigned long next, pages = 0;
805 pte_t *pte_base;
806 pmd_t *pmd;
807 void *page_addr;
809 pmd = pmd_start + pmd_index(addr);
810 for (; addr < end; addr = next, pmd++) {
811 next = pmd_addr_end(addr, end);
813 if (!pmd_present(*pmd))
814 continue;
816 if (pmd_large(*pmd)) {
817 if (IS_ALIGNED(addr, PMD_SIZE) &&
818 IS_ALIGNED(next, PMD_SIZE)) {
819 if (!direct)
820 free_pagetable(pmd_page(*pmd),
821 get_order(PMD_SIZE));
823 spin_lock(&init_mm.page_table_lock);
824 pmd_clear(pmd);
825 spin_unlock(&init_mm.page_table_lock);
826 pages++;
827 } else {
828 /* If here, we are freeing vmemmap pages. */
829 memset((void *)addr, PAGE_INUSE, next - addr);
831 page_addr = page_address(pmd_page(*pmd));
832 if (!memchr_inv(page_addr, PAGE_INUSE,
833 PMD_SIZE)) {
834 free_pagetable(pmd_page(*pmd),
835 get_order(PMD_SIZE));
837 spin_lock(&init_mm.page_table_lock);
838 pmd_clear(pmd);
839 spin_unlock(&init_mm.page_table_lock);
843 continue;
846 pte_base = (pte_t *)pmd_page_vaddr(*pmd);
847 remove_pte_table(pte_base, addr, next, direct);
848 free_pte_table(pte_base, pmd);
851 /* Call free_pmd_table() in remove_pud_table(). */
852 if (direct)
853 update_page_count(PG_LEVEL_2M, -pages);
856 static void __meminit
857 remove_pud_table(pud_t *pud_start, unsigned long addr, unsigned long end,
858 bool direct)
860 unsigned long next, pages = 0;
861 pmd_t *pmd_base;
862 pud_t *pud;
863 void *page_addr;
865 pud = pud_start + pud_index(addr);
866 for (; addr < end; addr = next, pud++) {
867 next = pud_addr_end(addr, end);
869 if (!pud_present(*pud))
870 continue;
872 if (pud_large(*pud)) {
873 if (IS_ALIGNED(addr, PUD_SIZE) &&
874 IS_ALIGNED(next, PUD_SIZE)) {
875 if (!direct)
876 free_pagetable(pud_page(*pud),
877 get_order(PUD_SIZE));
879 spin_lock(&init_mm.page_table_lock);
880 pud_clear(pud);
881 spin_unlock(&init_mm.page_table_lock);
882 pages++;
883 } else {
884 /* If here, we are freeing vmemmap pages. */
885 memset((void *)addr, PAGE_INUSE, next - addr);
887 page_addr = page_address(pud_page(*pud));
888 if (!memchr_inv(page_addr, PAGE_INUSE,
889 PUD_SIZE)) {
890 free_pagetable(pud_page(*pud),
891 get_order(PUD_SIZE));
893 spin_lock(&init_mm.page_table_lock);
894 pud_clear(pud);
895 spin_unlock(&init_mm.page_table_lock);
899 continue;
902 pmd_base = (pmd_t *)pud_page_vaddr(*pud);
903 remove_pmd_table(pmd_base, addr, next, direct);
904 free_pmd_table(pmd_base, pud);
907 if (direct)
908 update_page_count(PG_LEVEL_1G, -pages);
911 /* start and end are both virtual address. */
912 static void __meminit
913 remove_pagetable(unsigned long start, unsigned long end, bool direct)
915 unsigned long next;
916 unsigned long addr;
917 pgd_t *pgd;
918 pud_t *pud;
920 for (addr = start; addr < end; addr = next) {
921 next = pgd_addr_end(addr, end);
923 pgd = pgd_offset_k(addr);
924 if (!pgd_present(*pgd))
925 continue;
927 pud = (pud_t *)pgd_page_vaddr(*pgd);
928 remove_pud_table(pud, addr, next, direct);
931 flush_tlb_all();
934 void __ref vmemmap_free(unsigned long start, unsigned long end)
936 remove_pagetable(start, end, false);
939 #ifdef CONFIG_MEMORY_HOTREMOVE
940 static void __meminit
941 kernel_physical_mapping_remove(unsigned long start, unsigned long end)
943 start = (unsigned long)__va(start);
944 end = (unsigned long)__va(end);
946 remove_pagetable(start, end, true);
949 int __ref arch_remove_memory(u64 start, u64 size)
951 unsigned long start_pfn = start >> PAGE_SHIFT;
952 unsigned long nr_pages = size >> PAGE_SHIFT;
953 struct page *page = pfn_to_page(start_pfn);
954 struct vmem_altmap *altmap;
955 struct zone *zone;
956 int ret;
958 /* With altmap the first mapped page is offset from @start */
959 altmap = to_vmem_altmap((unsigned long) page);
960 if (altmap)
961 page += vmem_altmap_offset(altmap);
962 zone = page_zone(page);
963 ret = __remove_pages(zone, start_pfn, nr_pages);
964 WARN_ON_ONCE(ret);
965 kernel_physical_mapping_remove(start, start + size);
967 return ret;
969 #endif
970 #endif /* CONFIG_MEMORY_HOTPLUG */
972 static struct kcore_list kcore_vsyscall;
974 static void __init register_page_bootmem_info(void)
976 #ifdef CONFIG_NUMA
977 int i;
979 for_each_online_node(i)
980 register_page_bootmem_info_node(NODE_DATA(i));
981 #endif
984 void __init mem_init(void)
986 pci_iommu_alloc();
988 /* clear_bss() already clear the empty_zero_page */
990 register_page_bootmem_info();
992 /* this will put all memory onto the freelists */
993 free_all_bootmem();
994 after_bootmem = 1;
996 /* Register memory areas for /proc/kcore */
997 kclist_add(&kcore_vsyscall, (void *)VSYSCALL_ADDR,
998 PAGE_SIZE, KCORE_OTHER);
1000 mem_init_print_info(NULL);
1003 int kernel_set_to_readonly;
1005 void set_kernel_text_rw(void)
1007 unsigned long start = PFN_ALIGN(_text);
1008 unsigned long end = PFN_ALIGN(__stop___ex_table);
1010 if (!kernel_set_to_readonly)
1011 return;
1013 pr_debug("Set kernel text: %lx - %lx for read write\n",
1014 start, end);
1017 * Make the kernel identity mapping for text RW. Kernel text
1018 * mapping will always be RO. Refer to the comment in
1019 * static_protections() in pageattr.c
1021 set_memory_rw(start, (end - start) >> PAGE_SHIFT);
1024 void set_kernel_text_ro(void)
1026 unsigned long start = PFN_ALIGN(_text);
1027 unsigned long end = PFN_ALIGN(__stop___ex_table);
1029 if (!kernel_set_to_readonly)
1030 return;
1032 pr_debug("Set kernel text: %lx - %lx for read only\n",
1033 start, end);
1036 * Set the kernel identity mapping for text RO.
1038 set_memory_ro(start, (end - start) >> PAGE_SHIFT);
1041 void mark_rodata_ro(void)
1043 unsigned long start = PFN_ALIGN(_text);
1044 unsigned long rodata_start = PFN_ALIGN(__start_rodata);
1045 unsigned long end = (unsigned long) &__end_rodata_hpage_align;
1046 unsigned long text_end = PFN_ALIGN(&__stop___ex_table);
1047 unsigned long rodata_end = PFN_ALIGN(&__end_rodata);
1048 unsigned long all_end;
1050 printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
1051 (end - start) >> 10);
1052 set_memory_ro(start, (end - start) >> PAGE_SHIFT);
1054 kernel_set_to_readonly = 1;
1057 * The rodata/data/bss/brk section (but not the kernel text!)
1058 * should also be not-executable.
1060 * We align all_end to PMD_SIZE because the existing mapping
1061 * is a full PMD. If we would align _brk_end to PAGE_SIZE we
1062 * split the PMD and the reminder between _brk_end and the end
1063 * of the PMD will remain mapped executable.
1065 * Any PMD which was setup after the one which covers _brk_end
1066 * has been zapped already via cleanup_highmem().
1068 all_end = roundup((unsigned long)_brk_end, PMD_SIZE);
1069 set_memory_nx(text_end, (all_end - text_end) >> PAGE_SHIFT);
1071 #ifdef CONFIG_CPA_DEBUG
1072 printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end);
1073 set_memory_rw(start, (end-start) >> PAGE_SHIFT);
1075 printk(KERN_INFO "Testing CPA: again\n");
1076 set_memory_ro(start, (end-start) >> PAGE_SHIFT);
1077 #endif
1079 free_init_pages("unused kernel",
1080 (unsigned long) __va(__pa_symbol(text_end)),
1081 (unsigned long) __va(__pa_symbol(rodata_start)));
1082 free_init_pages("unused kernel",
1083 (unsigned long) __va(__pa_symbol(rodata_end)),
1084 (unsigned long) __va(__pa_symbol(_sdata)));
1086 debug_checkwx();
1089 int kern_addr_valid(unsigned long addr)
1091 unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
1092 pgd_t *pgd;
1093 pud_t *pud;
1094 pmd_t *pmd;
1095 pte_t *pte;
1097 if (above != 0 && above != -1UL)
1098 return 0;
1100 pgd = pgd_offset_k(addr);
1101 if (pgd_none(*pgd))
1102 return 0;
1104 pud = pud_offset(pgd, addr);
1105 if (pud_none(*pud))
1106 return 0;
1108 if (pud_large(*pud))
1109 return pfn_valid(pud_pfn(*pud));
1111 pmd = pmd_offset(pud, addr);
1112 if (pmd_none(*pmd))
1113 return 0;
1115 if (pmd_large(*pmd))
1116 return pfn_valid(pmd_pfn(*pmd));
1118 pte = pte_offset_kernel(pmd, addr);
1119 if (pte_none(*pte))
1120 return 0;
1122 return pfn_valid(pte_pfn(*pte));
1125 static unsigned long probe_memory_block_size(void)
1127 unsigned long bz = MIN_MEMORY_BLOCK_SIZE;
1129 /* if system is UV or has 64GB of RAM or more, use large blocks */
1130 if (is_uv_system() || ((max_pfn << PAGE_SHIFT) >= (64UL << 30)))
1131 bz = 2UL << 30; /* 2GB */
1133 pr_info("x86/mm: Memory block size: %ldMB\n", bz >> 20);
1135 return bz;
1138 static unsigned long memory_block_size_probed;
1139 unsigned long memory_block_size_bytes(void)
1141 if (!memory_block_size_probed)
1142 memory_block_size_probed = probe_memory_block_size();
1144 return memory_block_size_probed;
1147 #ifdef CONFIG_SPARSEMEM_VMEMMAP
1149 * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
1151 static long __meminitdata addr_start, addr_end;
1152 static void __meminitdata *p_start, *p_end;
1153 static int __meminitdata node_start;
1155 static int __meminit vmemmap_populate_hugepages(unsigned long start,
1156 unsigned long end, int node, struct vmem_altmap *altmap)
1158 unsigned long addr;
1159 unsigned long next;
1160 pgd_t *pgd;
1161 pud_t *pud;
1162 pmd_t *pmd;
1164 for (addr = start; addr < end; addr = next) {
1165 next = pmd_addr_end(addr, end);
1167 pgd = vmemmap_pgd_populate(addr, node);
1168 if (!pgd)
1169 return -ENOMEM;
1171 pud = vmemmap_pud_populate(pgd, addr, node);
1172 if (!pud)
1173 return -ENOMEM;
1175 pmd = pmd_offset(pud, addr);
1176 if (pmd_none(*pmd)) {
1177 void *p;
1179 p = __vmemmap_alloc_block_buf(PMD_SIZE, node, altmap);
1180 if (p) {
1181 pte_t entry;
1183 entry = pfn_pte(__pa(p) >> PAGE_SHIFT,
1184 PAGE_KERNEL_LARGE);
1185 set_pmd(pmd, __pmd(pte_val(entry)));
1187 /* check to see if we have contiguous blocks */
1188 if (p_end != p || node_start != node) {
1189 if (p_start)
1190 pr_debug(" [%lx-%lx] PMD -> [%p-%p] on node %d\n",
1191 addr_start, addr_end-1, p_start, p_end-1, node_start);
1192 addr_start = addr;
1193 node_start = node;
1194 p_start = p;
1197 addr_end = addr + PMD_SIZE;
1198 p_end = p + PMD_SIZE;
1199 continue;
1200 } else if (altmap)
1201 return -ENOMEM; /* no fallback */
1202 } else if (pmd_large(*pmd)) {
1203 vmemmap_verify((pte_t *)pmd, node, addr, next);
1204 continue;
1206 pr_warn_once("vmemmap: falling back to regular page backing\n");
1207 if (vmemmap_populate_basepages(addr, next, node))
1208 return -ENOMEM;
1210 return 0;
1213 int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node)
1215 struct vmem_altmap *altmap = to_vmem_altmap(start);
1216 int err;
1218 if (boot_cpu_has(X86_FEATURE_PSE))
1219 err = vmemmap_populate_hugepages(start, end, node, altmap);
1220 else if (altmap) {
1221 pr_err_once("%s: no cpu support for altmap allocations\n",
1222 __func__);
1223 err = -ENOMEM;
1224 } else
1225 err = vmemmap_populate_basepages(start, end, node);
1226 if (!err)
1227 sync_global_pgds(start, end - 1);
1228 return err;
1231 #if defined(CONFIG_MEMORY_HOTPLUG_SPARSE) && defined(CONFIG_HAVE_BOOTMEM_INFO_NODE)
1232 void register_page_bootmem_memmap(unsigned long section_nr,
1233 struct page *start_page, unsigned long size)
1235 unsigned long addr = (unsigned long)start_page;
1236 unsigned long end = (unsigned long)(start_page + size);
1237 unsigned long next;
1238 pgd_t *pgd;
1239 pud_t *pud;
1240 pmd_t *pmd;
1241 unsigned int nr_pages;
1242 struct page *page;
1244 for (; addr < end; addr = next) {
1245 pte_t *pte = NULL;
1247 pgd = pgd_offset_k(addr);
1248 if (pgd_none(*pgd)) {
1249 next = (addr + PAGE_SIZE) & PAGE_MASK;
1250 continue;
1252 get_page_bootmem(section_nr, pgd_page(*pgd), MIX_SECTION_INFO);
1254 pud = pud_offset(pgd, addr);
1255 if (pud_none(*pud)) {
1256 next = (addr + PAGE_SIZE) & PAGE_MASK;
1257 continue;
1259 get_page_bootmem(section_nr, pud_page(*pud), MIX_SECTION_INFO);
1261 if (!boot_cpu_has(X86_FEATURE_PSE)) {
1262 next = (addr + PAGE_SIZE) & PAGE_MASK;
1263 pmd = pmd_offset(pud, addr);
1264 if (pmd_none(*pmd))
1265 continue;
1266 get_page_bootmem(section_nr, pmd_page(*pmd),
1267 MIX_SECTION_INFO);
1269 pte = pte_offset_kernel(pmd, addr);
1270 if (pte_none(*pte))
1271 continue;
1272 get_page_bootmem(section_nr, pte_page(*pte),
1273 SECTION_INFO);
1274 } else {
1275 next = pmd_addr_end(addr, end);
1277 pmd = pmd_offset(pud, addr);
1278 if (pmd_none(*pmd))
1279 continue;
1281 nr_pages = 1 << (get_order(PMD_SIZE));
1282 page = pmd_page(*pmd);
1283 while (nr_pages--)
1284 get_page_bootmem(section_nr, page++,
1285 SECTION_INFO);
1289 #endif
1291 void __meminit vmemmap_populate_print_last(void)
1293 if (p_start) {
1294 pr_debug(" [%lx-%lx] PMD -> [%p-%p] on node %d\n",
1295 addr_start, addr_end-1, p_start, p_end-1, node_start);
1296 p_start = NULL;
1297 p_end = NULL;
1298 node_start = 0;
1301 #endif