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>
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>
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/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 <asm/uaccess.h>
40 #include <asm/pgtable.h>
41 #include <asm/pgalloc.h>
43 #include <asm/fixmap.h>
47 #include <asm/mmu_context.h>
48 #include <asm/proto.h>
50 #include <asm/sections.h>
51 #include <asm/kdebug.h>
53 #include <asm/cacheflush.h>
55 #include <asm/setup.h>
57 #include "mm_internal.h"
59 static void ident_pmd_init(unsigned long pmd_flag
, pmd_t
*pmd_page
,
60 unsigned long addr
, unsigned long end
)
63 for (; addr
< end
; addr
+= PMD_SIZE
) {
64 pmd_t
*pmd
= pmd_page
+ pmd_index(addr
);
66 if (!pmd_present(*pmd
))
67 set_pmd(pmd
, __pmd(addr
| pmd_flag
));
70 static int ident_pud_init(struct x86_mapping_info
*info
, pud_t
*pud_page
,
71 unsigned long addr
, unsigned long end
)
75 for (; addr
< end
; addr
= next
) {
76 pud_t
*pud
= pud_page
+ pud_index(addr
);
79 next
= (addr
& PUD_MASK
) + PUD_SIZE
;
83 if (pud_present(*pud
)) {
84 pmd
= pmd_offset(pud
, 0);
85 ident_pmd_init(info
->pmd_flag
, pmd
, addr
, next
);
88 pmd
= (pmd_t
*)info
->alloc_pgt_page(info
->context
);
91 ident_pmd_init(info
->pmd_flag
, pmd
, addr
, next
);
92 set_pud(pud
, __pud(__pa(pmd
) | _KERNPG_TABLE
));
98 int kernel_ident_mapping_init(struct x86_mapping_info
*info
, pgd_t
*pgd_page
,
99 unsigned long addr
, unsigned long end
)
103 int off
= info
->kernel_mapping
? pgd_index(__PAGE_OFFSET
) : 0;
105 for (; addr
< end
; addr
= next
) {
106 pgd_t
*pgd
= pgd_page
+ pgd_index(addr
) + off
;
109 next
= (addr
& PGDIR_MASK
) + PGDIR_SIZE
;
113 if (pgd_present(*pgd
)) {
114 pud
= pud_offset(pgd
, 0);
115 result
= ident_pud_init(info
, pud
, addr
, next
);
121 pud
= (pud_t
*)info
->alloc_pgt_page(info
->context
);
124 result
= ident_pud_init(info
, pud
, addr
, next
);
127 set_pgd(pgd
, __pgd(__pa(pud
) | _KERNPG_TABLE
));
133 static int __init
parse_direct_gbpages_off(char *arg
)
138 early_param("nogbpages", parse_direct_gbpages_off
);
140 static int __init
parse_direct_gbpages_on(char *arg
)
145 early_param("gbpages", parse_direct_gbpages_on
);
148 * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the
149 * physical space so we can cache the place of the first one and move
150 * around without checking the pgd every time.
153 pteval_t __supported_pte_mask __read_mostly
= ~0;
154 EXPORT_SYMBOL_GPL(__supported_pte_mask
);
156 int force_personality32
;
160 * Control non executable heap for 32bit processes.
161 * To control the stack too use noexec=off
163 * on PROT_READ does not imply PROT_EXEC for 32-bit processes (default)
164 * off PROT_READ implies PROT_EXEC
166 static int __init
nonx32_setup(char *str
)
168 if (!strcmp(str
, "on"))
169 force_personality32
&= ~READ_IMPLIES_EXEC
;
170 else if (!strcmp(str
, "off"))
171 force_personality32
|= READ_IMPLIES_EXEC
;
174 __setup("noexec32=", nonx32_setup
);
177 * When memory was added/removed make sure all the processes MM have
178 * suitable PGD entries in the local PGD level page.
180 void sync_global_pgds(unsigned long start
, unsigned long end
, int removed
)
182 unsigned long address
;
184 for (address
= start
; address
<= end
; address
+= PGDIR_SIZE
) {
185 const pgd_t
*pgd_ref
= pgd_offset_k(address
);
189 * When it is called after memory hot remove, pgd_none()
190 * returns true. In this case (removed == 1), we must clear
191 * the PGD entries in the local PGD level page.
193 if (pgd_none(*pgd_ref
) && !removed
)
196 spin_lock(&pgd_lock
);
197 list_for_each_entry(page
, &pgd_list
, lru
) {
199 spinlock_t
*pgt_lock
;
201 pgd
= (pgd_t
*)page_address(page
) + pgd_index(address
);
202 /* the pgt_lock only for Xen */
203 pgt_lock
= &pgd_page_get_mm(page
)->page_table_lock
;
206 if (!pgd_none(*pgd_ref
) && !pgd_none(*pgd
))
207 BUG_ON(pgd_page_vaddr(*pgd
)
208 != pgd_page_vaddr(*pgd_ref
));
211 if (pgd_none(*pgd_ref
) && !pgd_none(*pgd
))
215 set_pgd(pgd
, *pgd_ref
);
218 spin_unlock(pgt_lock
);
220 spin_unlock(&pgd_lock
);
225 * NOTE: This function is marked __ref because it calls __init function
226 * (alloc_bootmem_pages). It's safe to do it ONLY when after_bootmem == 0.
228 static __ref
void *spp_getpage(void)
233 ptr
= (void *) get_zeroed_page(GFP_ATOMIC
| __GFP_NOTRACK
);
235 ptr
= alloc_bootmem_pages(PAGE_SIZE
);
237 if (!ptr
|| ((unsigned long)ptr
& ~PAGE_MASK
)) {
238 panic("set_pte_phys: cannot allocate page data %s\n",
239 after_bootmem
? "after bootmem" : "");
242 pr_debug("spp_getpage %p\n", ptr
);
247 static pud_t
*fill_pud(pgd_t
*pgd
, unsigned long vaddr
)
249 if (pgd_none(*pgd
)) {
250 pud_t
*pud
= (pud_t
*)spp_getpage();
251 pgd_populate(&init_mm
, pgd
, pud
);
252 if (pud
!= pud_offset(pgd
, 0))
253 printk(KERN_ERR
"PAGETABLE BUG #00! %p <-> %p\n",
254 pud
, pud_offset(pgd
, 0));
256 return pud_offset(pgd
, vaddr
);
259 static pmd_t
*fill_pmd(pud_t
*pud
, unsigned long vaddr
)
261 if (pud_none(*pud
)) {
262 pmd_t
*pmd
= (pmd_t
*) spp_getpage();
263 pud_populate(&init_mm
, pud
, pmd
);
264 if (pmd
!= pmd_offset(pud
, 0))
265 printk(KERN_ERR
"PAGETABLE BUG #01! %p <-> %p\n",
266 pmd
, pmd_offset(pud
, 0));
268 return pmd_offset(pud
, vaddr
);
271 static pte_t
*fill_pte(pmd_t
*pmd
, unsigned long vaddr
)
273 if (pmd_none(*pmd
)) {
274 pte_t
*pte
= (pte_t
*) spp_getpage();
275 pmd_populate_kernel(&init_mm
, pmd
, pte
);
276 if (pte
!= pte_offset_kernel(pmd
, 0))
277 printk(KERN_ERR
"PAGETABLE BUG #02!\n");
279 return pte_offset_kernel(pmd
, vaddr
);
282 void set_pte_vaddr_pud(pud_t
*pud_page
, unsigned long vaddr
, pte_t new_pte
)
288 pud
= pud_page
+ pud_index(vaddr
);
289 pmd
= fill_pmd(pud
, vaddr
);
290 pte
= fill_pte(pmd
, vaddr
);
292 set_pte(pte
, new_pte
);
295 * It's enough to flush this one mapping.
296 * (PGE mappings get flushed as well)
298 __flush_tlb_one(vaddr
);
301 void set_pte_vaddr(unsigned long vaddr
, pte_t pteval
)
306 pr_debug("set_pte_vaddr %lx to %lx\n", vaddr
, native_pte_val(pteval
));
308 pgd
= pgd_offset_k(vaddr
);
309 if (pgd_none(*pgd
)) {
311 "PGD FIXMAP MISSING, it should be setup in head.S!\n");
314 pud_page
= (pud_t
*)pgd_page_vaddr(*pgd
);
315 set_pte_vaddr_pud(pud_page
, vaddr
, pteval
);
318 pmd_t
* __init
populate_extra_pmd(unsigned long vaddr
)
323 pgd
= pgd_offset_k(vaddr
);
324 pud
= fill_pud(pgd
, vaddr
);
325 return fill_pmd(pud
, vaddr
);
328 pte_t
* __init
populate_extra_pte(unsigned long vaddr
)
332 pmd
= populate_extra_pmd(vaddr
);
333 return fill_pte(pmd
, vaddr
);
337 * Create large page table mappings for a range of physical addresses.
339 static void __init
__init_extra_mapping(unsigned long phys
, unsigned long size
,
340 enum page_cache_mode cache
)
347 pgprot_val(prot
) = pgprot_val(PAGE_KERNEL_LARGE
) |
348 pgprot_val(pgprot_4k_2_large(cachemode2pgprot(cache
)));
349 BUG_ON((phys
& ~PMD_MASK
) || (size
& ~PMD_MASK
));
350 for (; size
; phys
+= PMD_SIZE
, size
-= PMD_SIZE
) {
351 pgd
= pgd_offset_k((unsigned long)__va(phys
));
352 if (pgd_none(*pgd
)) {
353 pud
= (pud_t
*) spp_getpage();
354 set_pgd(pgd
, __pgd(__pa(pud
) | _KERNPG_TABLE
|
357 pud
= pud_offset(pgd
, (unsigned long)__va(phys
));
358 if (pud_none(*pud
)) {
359 pmd
= (pmd_t
*) spp_getpage();
360 set_pud(pud
, __pud(__pa(pmd
) | _KERNPG_TABLE
|
363 pmd
= pmd_offset(pud
, phys
);
364 BUG_ON(!pmd_none(*pmd
));
365 set_pmd(pmd
, __pmd(phys
| pgprot_val(prot
)));
369 void __init
init_extra_mapping_wb(unsigned long phys
, unsigned long size
)
371 __init_extra_mapping(phys
, size
, _PAGE_CACHE_MODE_WB
);
374 void __init
init_extra_mapping_uc(unsigned long phys
, unsigned long size
)
376 __init_extra_mapping(phys
, size
, _PAGE_CACHE_MODE_UC
);
380 * The head.S code sets up the kernel high mapping:
382 * from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text)
384 * phys_base holds the negative offset to the kernel, which is added
385 * to the compile time generated pmds. This results in invalid pmds up
386 * to the point where we hit the physaddr 0 mapping.
388 * We limit the mappings to the region from _text to _brk_end. _brk_end
389 * is rounded up to the 2MB boundary. This catches the invalid pmds as
390 * well, as they are located before _text:
392 void __init
cleanup_highmap(void)
394 unsigned long vaddr
= __START_KERNEL_map
;
395 unsigned long vaddr_end
= __START_KERNEL_map
+ KERNEL_IMAGE_SIZE
;
396 unsigned long end
= roundup((unsigned long)_brk_end
, PMD_SIZE
) - 1;
397 pmd_t
*pmd
= level2_kernel_pgt
;
400 * Native path, max_pfn_mapped is not set yet.
401 * Xen has valid max_pfn_mapped set in
402 * arch/x86/xen/mmu.c:xen_setup_kernel_pagetable().
405 vaddr_end
= __START_KERNEL_map
+ (max_pfn_mapped
<< PAGE_SHIFT
);
407 for (; vaddr
+ PMD_SIZE
- 1 < vaddr_end
; pmd
++, vaddr
+= PMD_SIZE
) {
410 if (vaddr
< (unsigned long) _text
|| vaddr
> end
)
411 set_pmd(pmd
, __pmd(0));
415 static unsigned long __meminit
416 phys_pte_init(pte_t
*pte_page
, unsigned long addr
, unsigned long end
,
419 unsigned long pages
= 0, next
;
420 unsigned long last_map_addr
= end
;
423 pte_t
*pte
= pte_page
+ pte_index(addr
);
425 for (i
= pte_index(addr
); i
< PTRS_PER_PTE
; i
++, addr
= next
, pte
++) {
426 next
= (addr
& PAGE_MASK
) + PAGE_SIZE
;
428 if (!after_bootmem
&&
429 !e820_any_mapped(addr
& PAGE_MASK
, next
, E820_RAM
) &&
430 !e820_any_mapped(addr
& PAGE_MASK
, next
, E820_RESERVED_KERN
))
431 set_pte(pte
, __pte(0));
436 * We will re-use the existing mapping.
437 * Xen for example has some special requirements, like mapping
438 * pagetable pages as RO. So assume someone who pre-setup
439 * these mappings are more intelligent.
448 printk(" pte=%p addr=%lx pte=%016lx\n",
449 pte
, addr
, pfn_pte(addr
>> PAGE_SHIFT
, PAGE_KERNEL
).pte
);
451 set_pte(pte
, pfn_pte(addr
>> PAGE_SHIFT
, prot
));
452 last_map_addr
= (addr
& PAGE_MASK
) + PAGE_SIZE
;
455 update_page_count(PG_LEVEL_4K
, pages
);
457 return last_map_addr
;
460 static unsigned long __meminit
461 phys_pmd_init(pmd_t
*pmd_page
, unsigned long address
, unsigned long end
,
462 unsigned long page_size_mask
, pgprot_t prot
)
464 unsigned long pages
= 0, next
;
465 unsigned long last_map_addr
= end
;
467 int i
= pmd_index(address
);
469 for (; i
< PTRS_PER_PMD
; i
++, address
= next
) {
470 pmd_t
*pmd
= pmd_page
+ pmd_index(address
);
472 pgprot_t new_prot
= prot
;
474 next
= (address
& PMD_MASK
) + PMD_SIZE
;
475 if (address
>= end
) {
476 if (!after_bootmem
&&
477 !e820_any_mapped(address
& PMD_MASK
, next
, E820_RAM
) &&
478 !e820_any_mapped(address
& PMD_MASK
, next
, E820_RESERVED_KERN
))
479 set_pmd(pmd
, __pmd(0));
484 if (!pmd_large(*pmd
)) {
485 spin_lock(&init_mm
.page_table_lock
);
486 pte
= (pte_t
*)pmd_page_vaddr(*pmd
);
487 last_map_addr
= phys_pte_init(pte
, address
,
489 spin_unlock(&init_mm
.page_table_lock
);
493 * If we are ok with PG_LEVEL_2M mapping, then we will
494 * use the existing mapping,
496 * Otherwise, we will split the large page mapping but
497 * use the same existing protection bits except for
498 * large page, so that we don't violate Intel's TLB
499 * Application note (317080) which says, while changing
500 * the page sizes, new and old translations should
501 * not differ with respect to page frame and
504 if (page_size_mask
& (1 << PG_LEVEL_2M
)) {
507 last_map_addr
= next
;
510 new_prot
= pte_pgprot(pte_clrhuge(*(pte_t
*)pmd
));
513 if (page_size_mask
& (1<<PG_LEVEL_2M
)) {
515 spin_lock(&init_mm
.page_table_lock
);
516 set_pte((pte_t
*)pmd
,
517 pfn_pte((address
& PMD_MASK
) >> PAGE_SHIFT
,
518 __pgprot(pgprot_val(prot
) | _PAGE_PSE
)));
519 spin_unlock(&init_mm
.page_table_lock
);
520 last_map_addr
= next
;
524 pte
= alloc_low_page();
525 last_map_addr
= phys_pte_init(pte
, address
, end
, new_prot
);
527 spin_lock(&init_mm
.page_table_lock
);
528 pmd_populate_kernel(&init_mm
, pmd
, pte
);
529 spin_unlock(&init_mm
.page_table_lock
);
531 update_page_count(PG_LEVEL_2M
, pages
);
532 return last_map_addr
;
535 static unsigned long __meminit
536 phys_pud_init(pud_t
*pud_page
, unsigned long addr
, unsigned long end
,
537 unsigned long page_size_mask
)
539 unsigned long pages
= 0, next
;
540 unsigned long last_map_addr
= end
;
541 int i
= pud_index(addr
);
543 for (; i
< PTRS_PER_PUD
; i
++, addr
= next
) {
544 pud_t
*pud
= pud_page
+ pud_index(addr
);
546 pgprot_t prot
= PAGE_KERNEL
;
548 next
= (addr
& PUD_MASK
) + PUD_SIZE
;
550 if (!after_bootmem
&&
551 !e820_any_mapped(addr
& PUD_MASK
, next
, E820_RAM
) &&
552 !e820_any_mapped(addr
& PUD_MASK
, next
, E820_RESERVED_KERN
))
553 set_pud(pud
, __pud(0));
558 if (!pud_large(*pud
)) {
559 pmd
= pmd_offset(pud
, 0);
560 last_map_addr
= phys_pmd_init(pmd
, addr
, end
,
561 page_size_mask
, prot
);
566 * If we are ok with PG_LEVEL_1G mapping, then we will
567 * use the existing mapping.
569 * Otherwise, we will split the gbpage mapping but use
570 * the same existing protection bits except for large
571 * page, so that we don't violate Intel's TLB
572 * Application note (317080) which says, while changing
573 * the page sizes, new and old translations should
574 * not differ with respect to page frame and
577 if (page_size_mask
& (1 << PG_LEVEL_1G
)) {
580 last_map_addr
= next
;
583 prot
= pte_pgprot(pte_clrhuge(*(pte_t
*)pud
));
586 if (page_size_mask
& (1<<PG_LEVEL_1G
)) {
588 spin_lock(&init_mm
.page_table_lock
);
589 set_pte((pte_t
*)pud
,
590 pfn_pte((addr
& PUD_MASK
) >> PAGE_SHIFT
,
592 spin_unlock(&init_mm
.page_table_lock
);
593 last_map_addr
= next
;
597 pmd
= alloc_low_page();
598 last_map_addr
= phys_pmd_init(pmd
, addr
, end
, page_size_mask
,
601 spin_lock(&init_mm
.page_table_lock
);
602 pud_populate(&init_mm
, pud
, pmd
);
603 spin_unlock(&init_mm
.page_table_lock
);
607 update_page_count(PG_LEVEL_1G
, pages
);
609 return last_map_addr
;
612 unsigned long __meminit
613 kernel_physical_mapping_init(unsigned long start
,
615 unsigned long page_size_mask
)
617 bool pgd_changed
= false;
618 unsigned long next
, last_map_addr
= end
;
621 start
= (unsigned long)__va(start
);
622 end
= (unsigned long)__va(end
);
625 for (; start
< end
; start
= next
) {
626 pgd_t
*pgd
= pgd_offset_k(start
);
629 next
= (start
& PGDIR_MASK
) + PGDIR_SIZE
;
632 pud
= (pud_t
*)pgd_page_vaddr(*pgd
);
633 last_map_addr
= phys_pud_init(pud
, __pa(start
),
634 __pa(end
), page_size_mask
);
638 pud
= alloc_low_page();
639 last_map_addr
= phys_pud_init(pud
, __pa(start
), __pa(end
),
642 spin_lock(&init_mm
.page_table_lock
);
643 pgd_populate(&init_mm
, pgd
, pud
);
644 spin_unlock(&init_mm
.page_table_lock
);
649 sync_global_pgds(addr
, end
- 1, 0);
653 return last_map_addr
;
657 void __init
initmem_init(void)
659 memblock_set_node(0, (phys_addr_t
)ULLONG_MAX
, &memblock
.memory
, 0);
663 void __init
paging_init(void)
665 sparse_memory_present_with_active_regions(MAX_NUMNODES
);
669 * clear the default setting with node 0
670 * note: don't use nodes_clear here, that is really clearing when
671 * numa support is not compiled in, and later node_set_state
672 * will not set it back.
674 node_clear_state(0, N_MEMORY
);
675 if (N_MEMORY
!= N_NORMAL_MEMORY
)
676 node_clear_state(0, N_NORMAL_MEMORY
);
682 * Memory hotplug specific functions
684 #ifdef CONFIG_MEMORY_HOTPLUG
686 * After memory hotplug the variables max_pfn, max_low_pfn and high_memory need
689 static void update_end_of_memory_vars(u64 start
, u64 size
)
691 unsigned long end_pfn
= PFN_UP(start
+ size
);
693 if (end_pfn
> max_pfn
) {
695 max_low_pfn
= end_pfn
;
696 high_memory
= (void *)__va(max_pfn
* PAGE_SIZE
- 1) + 1;
701 * Memory is added always to NORMAL zone. This means you will never get
702 * additional DMA/DMA32 memory.
704 int arch_add_memory(int nid
, u64 start
, u64 size
)
706 struct pglist_data
*pgdat
= NODE_DATA(nid
);
707 struct zone
*zone
= pgdat
->node_zones
+
708 zone_for_memory(nid
, start
, size
, ZONE_NORMAL
);
709 unsigned long start_pfn
= start
>> PAGE_SHIFT
;
710 unsigned long nr_pages
= size
>> PAGE_SHIFT
;
713 init_memory_mapping(start
, start
+ size
);
715 ret
= __add_pages(nid
, zone
, start_pfn
, nr_pages
);
718 /* update max_pfn, max_low_pfn and high_memory */
719 update_end_of_memory_vars(start
, size
);
723 EXPORT_SYMBOL_GPL(arch_add_memory
);
725 #define PAGE_INUSE 0xFD
727 static void __meminit
free_pagetable(struct page
*page
, int order
)
730 unsigned int nr_pages
= 1 << order
;
732 /* bootmem page has reserved flag */
733 if (PageReserved(page
)) {
734 __ClearPageReserved(page
);
736 magic
= (unsigned long)page
->lru
.next
;
737 if (magic
== SECTION_INFO
|| magic
== MIX_SECTION_INFO
) {
739 put_page_bootmem(page
++);
742 free_reserved_page(page
++);
744 free_pages((unsigned long)page_address(page
), order
);
747 static void __meminit
free_pte_table(pte_t
*pte_start
, pmd_t
*pmd
)
752 for (i
= 0; i
< PTRS_PER_PTE
; i
++) {
758 /* free a pte talbe */
759 free_pagetable(pmd_page(*pmd
), 0);
760 spin_lock(&init_mm
.page_table_lock
);
762 spin_unlock(&init_mm
.page_table_lock
);
765 static void __meminit
free_pmd_table(pmd_t
*pmd_start
, pud_t
*pud
)
770 for (i
= 0; i
< PTRS_PER_PMD
; i
++) {
776 /* free a pmd talbe */
777 free_pagetable(pud_page(*pud
), 0);
778 spin_lock(&init_mm
.page_table_lock
);
780 spin_unlock(&init_mm
.page_table_lock
);
783 /* Return true if pgd is changed, otherwise return false. */
784 static bool __meminit
free_pud_table(pud_t
*pud_start
, pgd_t
*pgd
)
789 for (i
= 0; i
< PTRS_PER_PUD
; i
++) {
795 /* free a pud table */
796 free_pagetable(pgd_page(*pgd
), 0);
797 spin_lock(&init_mm
.page_table_lock
);
799 spin_unlock(&init_mm
.page_table_lock
);
804 static void __meminit
805 remove_pte_table(pte_t
*pte_start
, unsigned long addr
, unsigned long end
,
808 unsigned long next
, pages
= 0;
811 phys_addr_t phys_addr
;
813 pte
= pte_start
+ pte_index(addr
);
814 for (; addr
< end
; addr
= next
, pte
++) {
815 next
= (addr
+ PAGE_SIZE
) & PAGE_MASK
;
819 if (!pte_present(*pte
))
823 * We mapped [0,1G) memory as identity mapping when
824 * initializing, in arch/x86/kernel/head_64.S. These
825 * pagetables cannot be removed.
827 phys_addr
= pte_val(*pte
) + (addr
& PAGE_MASK
);
828 if (phys_addr
< (phys_addr_t
)0x40000000)
831 if (IS_ALIGNED(addr
, PAGE_SIZE
) &&
832 IS_ALIGNED(next
, PAGE_SIZE
)) {
834 * Do not free direct mapping pages since they were
835 * freed when offlining, or simplely not in use.
838 free_pagetable(pte_page(*pte
), 0);
840 spin_lock(&init_mm
.page_table_lock
);
841 pte_clear(&init_mm
, addr
, pte
);
842 spin_unlock(&init_mm
.page_table_lock
);
844 /* For non-direct mapping, pages means nothing. */
848 * If we are here, we are freeing vmemmap pages since
849 * direct mapped memory ranges to be freed are aligned.
851 * If we are not removing the whole page, it means
852 * other page structs in this page are being used and
853 * we canot remove them. So fill the unused page_structs
854 * with 0xFD, and remove the page when it is wholly
857 memset((void *)addr
, PAGE_INUSE
, next
- addr
);
859 page_addr
= page_address(pte_page(*pte
));
860 if (!memchr_inv(page_addr
, PAGE_INUSE
, PAGE_SIZE
)) {
861 free_pagetable(pte_page(*pte
), 0);
863 spin_lock(&init_mm
.page_table_lock
);
864 pte_clear(&init_mm
, addr
, pte
);
865 spin_unlock(&init_mm
.page_table_lock
);
870 /* Call free_pte_table() in remove_pmd_table(). */
873 update_page_count(PG_LEVEL_4K
, -pages
);
876 static void __meminit
877 remove_pmd_table(pmd_t
*pmd_start
, unsigned long addr
, unsigned long end
,
880 unsigned long next
, pages
= 0;
885 pmd
= pmd_start
+ pmd_index(addr
);
886 for (; addr
< end
; addr
= next
, pmd
++) {
887 next
= pmd_addr_end(addr
, end
);
889 if (!pmd_present(*pmd
))
892 if (pmd_large(*pmd
)) {
893 if (IS_ALIGNED(addr
, PMD_SIZE
) &&
894 IS_ALIGNED(next
, PMD_SIZE
)) {
896 free_pagetable(pmd_page(*pmd
),
897 get_order(PMD_SIZE
));
899 spin_lock(&init_mm
.page_table_lock
);
901 spin_unlock(&init_mm
.page_table_lock
);
904 /* If here, we are freeing vmemmap pages. */
905 memset((void *)addr
, PAGE_INUSE
, next
- addr
);
907 page_addr
= page_address(pmd_page(*pmd
));
908 if (!memchr_inv(page_addr
, PAGE_INUSE
,
910 free_pagetable(pmd_page(*pmd
),
911 get_order(PMD_SIZE
));
913 spin_lock(&init_mm
.page_table_lock
);
915 spin_unlock(&init_mm
.page_table_lock
);
922 pte_base
= (pte_t
*)pmd_page_vaddr(*pmd
);
923 remove_pte_table(pte_base
, addr
, next
, direct
);
924 free_pte_table(pte_base
, pmd
);
927 /* Call free_pmd_table() in remove_pud_table(). */
929 update_page_count(PG_LEVEL_2M
, -pages
);
932 static void __meminit
933 remove_pud_table(pud_t
*pud_start
, unsigned long addr
, unsigned long end
,
936 unsigned long next
, pages
= 0;
941 pud
= pud_start
+ pud_index(addr
);
942 for (; addr
< end
; addr
= next
, pud
++) {
943 next
= pud_addr_end(addr
, end
);
945 if (!pud_present(*pud
))
948 if (pud_large(*pud
)) {
949 if (IS_ALIGNED(addr
, PUD_SIZE
) &&
950 IS_ALIGNED(next
, PUD_SIZE
)) {
952 free_pagetable(pud_page(*pud
),
953 get_order(PUD_SIZE
));
955 spin_lock(&init_mm
.page_table_lock
);
957 spin_unlock(&init_mm
.page_table_lock
);
960 /* If here, we are freeing vmemmap pages. */
961 memset((void *)addr
, PAGE_INUSE
, next
- addr
);
963 page_addr
= page_address(pud_page(*pud
));
964 if (!memchr_inv(page_addr
, PAGE_INUSE
,
966 free_pagetable(pud_page(*pud
),
967 get_order(PUD_SIZE
));
969 spin_lock(&init_mm
.page_table_lock
);
971 spin_unlock(&init_mm
.page_table_lock
);
978 pmd_base
= (pmd_t
*)pud_page_vaddr(*pud
);
979 remove_pmd_table(pmd_base
, addr
, next
, direct
);
980 free_pmd_table(pmd_base
, pud
);
984 update_page_count(PG_LEVEL_1G
, -pages
);
987 /* start and end are both virtual address. */
988 static void __meminit
989 remove_pagetable(unsigned long start
, unsigned long end
, bool direct
)
995 bool pgd_changed
= false;
997 for (addr
= start
; addr
< end
; addr
= next
) {
998 next
= pgd_addr_end(addr
, end
);
1000 pgd
= pgd_offset_k(addr
);
1001 if (!pgd_present(*pgd
))
1004 pud
= (pud_t
*)pgd_page_vaddr(*pgd
);
1005 remove_pud_table(pud
, addr
, next
, direct
);
1006 if (free_pud_table(pud
, pgd
))
1011 sync_global_pgds(start
, end
- 1, 1);
1016 void __ref
vmemmap_free(unsigned long start
, unsigned long end
)
1018 remove_pagetable(start
, end
, false);
1021 #ifdef CONFIG_MEMORY_HOTREMOVE
1022 static void __meminit
1023 kernel_physical_mapping_remove(unsigned long start
, unsigned long end
)
1025 start
= (unsigned long)__va(start
);
1026 end
= (unsigned long)__va(end
);
1028 remove_pagetable(start
, end
, true);
1031 int __ref
arch_remove_memory(u64 start
, u64 size
)
1033 unsigned long start_pfn
= start
>> PAGE_SHIFT
;
1034 unsigned long nr_pages
= size
>> PAGE_SHIFT
;
1038 zone
= page_zone(pfn_to_page(start_pfn
));
1039 kernel_physical_mapping_remove(start
, start
+ size
);
1040 ret
= __remove_pages(zone
, start_pfn
, nr_pages
);
1046 #endif /* CONFIG_MEMORY_HOTPLUG */
1048 static struct kcore_list kcore_vsyscall
;
1050 static void __init
register_page_bootmem_info(void)
1055 for_each_online_node(i
)
1056 register_page_bootmem_info_node(NODE_DATA(i
));
1060 void __init
mem_init(void)
1064 /* clear_bss() already clear the empty_zero_page */
1066 register_page_bootmem_info();
1068 /* this will put all memory onto the freelists */
1072 /* Register memory areas for /proc/kcore */
1073 kclist_add(&kcore_vsyscall
, (void *)VSYSCALL_ADDR
,
1074 PAGE_SIZE
, KCORE_OTHER
);
1076 mem_init_print_info(NULL
);
1079 #ifdef CONFIG_DEBUG_RODATA
1080 const int rodata_test_data
= 0xC3;
1081 EXPORT_SYMBOL_GPL(rodata_test_data
);
1083 int kernel_set_to_readonly
;
1085 void set_kernel_text_rw(void)
1087 unsigned long start
= PFN_ALIGN(_text
);
1088 unsigned long end
= PFN_ALIGN(__stop___ex_table
);
1090 if (!kernel_set_to_readonly
)
1093 pr_debug("Set kernel text: %lx - %lx for read write\n",
1097 * Make the kernel identity mapping for text RW. Kernel text
1098 * mapping will always be RO. Refer to the comment in
1099 * static_protections() in pageattr.c
1101 set_memory_rw(start
, (end
- start
) >> PAGE_SHIFT
);
1104 void set_kernel_text_ro(void)
1106 unsigned long start
= PFN_ALIGN(_text
);
1107 unsigned long end
= PFN_ALIGN(__stop___ex_table
);
1109 if (!kernel_set_to_readonly
)
1112 pr_debug("Set kernel text: %lx - %lx for read only\n",
1116 * Set the kernel identity mapping for text RO.
1118 set_memory_ro(start
, (end
- start
) >> PAGE_SHIFT
);
1121 void mark_rodata_ro(void)
1123 unsigned long start
= PFN_ALIGN(_text
);
1124 unsigned long rodata_start
= PFN_ALIGN(__start_rodata
);
1125 unsigned long end
= (unsigned long) &__end_rodata_hpage_align
;
1126 unsigned long text_end
= PFN_ALIGN(&__stop___ex_table
);
1127 unsigned long rodata_end
= PFN_ALIGN(&__end_rodata
);
1128 unsigned long all_end
;
1130 printk(KERN_INFO
"Write protecting the kernel read-only data: %luk\n",
1131 (end
- start
) >> 10);
1132 set_memory_ro(start
, (end
- start
) >> PAGE_SHIFT
);
1134 kernel_set_to_readonly
= 1;
1137 * The rodata/data/bss/brk section (but not the kernel text!)
1138 * should also be not-executable.
1140 * We align all_end to PMD_SIZE because the existing mapping
1141 * is a full PMD. If we would align _brk_end to PAGE_SIZE we
1142 * split the PMD and the reminder between _brk_end and the end
1143 * of the PMD will remain mapped executable.
1145 * Any PMD which was setup after the one which covers _brk_end
1146 * has been zapped already via cleanup_highmem().
1148 all_end
= roundup((unsigned long)_brk_end
, PMD_SIZE
);
1149 set_memory_nx(rodata_start
, (all_end
- rodata_start
) >> PAGE_SHIFT
);
1153 #ifdef CONFIG_CPA_DEBUG
1154 printk(KERN_INFO
"Testing CPA: undo %lx-%lx\n", start
, end
);
1155 set_memory_rw(start
, (end
-start
) >> PAGE_SHIFT
);
1157 printk(KERN_INFO
"Testing CPA: again\n");
1158 set_memory_ro(start
, (end
-start
) >> PAGE_SHIFT
);
1161 free_init_pages("unused kernel",
1162 (unsigned long) __va(__pa_symbol(text_end
)),
1163 (unsigned long) __va(__pa_symbol(rodata_start
)));
1164 free_init_pages("unused kernel",
1165 (unsigned long) __va(__pa_symbol(rodata_end
)),
1166 (unsigned long) __va(__pa_symbol(_sdata
)));
1171 int kern_addr_valid(unsigned long addr
)
1173 unsigned long above
= ((long)addr
) >> __VIRTUAL_MASK_SHIFT
;
1179 if (above
!= 0 && above
!= -1UL)
1182 pgd
= pgd_offset_k(addr
);
1186 pud
= pud_offset(pgd
, addr
);
1190 if (pud_large(*pud
))
1191 return pfn_valid(pud_pfn(*pud
));
1193 pmd
= pmd_offset(pud
, addr
);
1197 if (pmd_large(*pmd
))
1198 return pfn_valid(pmd_pfn(*pmd
));
1200 pte
= pte_offset_kernel(pmd
, addr
);
1204 return pfn_valid(pte_pfn(*pte
));
1207 static unsigned long probe_memory_block_size(void)
1210 unsigned long bz
= 1UL<<31;
1212 if (totalram_pages
>= (64ULL << (30 - PAGE_SHIFT
))) {
1213 pr_info("Using 2GB memory block size for large-memory system\n");
1214 return 2UL * 1024 * 1024 * 1024;
1217 /* less than 64g installed */
1218 if ((max_pfn
<< PAGE_SHIFT
) < (16UL << 32))
1219 return MIN_MEMORY_BLOCK_SIZE
;
1221 /* get the tail size */
1222 while (bz
> MIN_MEMORY_BLOCK_SIZE
) {
1223 if (!((max_pfn
<< PAGE_SHIFT
) & (bz
- 1)))
1228 printk(KERN_DEBUG
"memory block size : %ldMB\n", bz
>> 20);
1233 static unsigned long memory_block_size_probed
;
1234 unsigned long memory_block_size_bytes(void)
1236 if (!memory_block_size_probed
)
1237 memory_block_size_probed
= probe_memory_block_size();
1239 return memory_block_size_probed
;
1242 #ifdef CONFIG_SPARSEMEM_VMEMMAP
1244 * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
1246 static long __meminitdata addr_start
, addr_end
;
1247 static void __meminitdata
*p_start
, *p_end
;
1248 static int __meminitdata node_start
;
1250 static int __meminit
vmemmap_populate_hugepages(unsigned long start
,
1251 unsigned long end
, int node
)
1259 for (addr
= start
; addr
< end
; addr
= next
) {
1260 next
= pmd_addr_end(addr
, end
);
1262 pgd
= vmemmap_pgd_populate(addr
, node
);
1266 pud
= vmemmap_pud_populate(pgd
, addr
, node
);
1270 pmd
= pmd_offset(pud
, addr
);
1271 if (pmd_none(*pmd
)) {
1274 p
= vmemmap_alloc_block_buf(PMD_SIZE
, node
);
1278 entry
= pfn_pte(__pa(p
) >> PAGE_SHIFT
,
1280 set_pmd(pmd
, __pmd(pte_val(entry
)));
1282 /* check to see if we have contiguous blocks */
1283 if (p_end
!= p
|| node_start
!= node
) {
1285 printk(KERN_DEBUG
" [%lx-%lx] PMD -> [%p-%p] on node %d\n",
1286 addr_start
, addr_end
-1, p_start
, p_end
-1, node_start
);
1292 addr_end
= addr
+ PMD_SIZE
;
1293 p_end
= p
+ PMD_SIZE
;
1296 } else if (pmd_large(*pmd
)) {
1297 vmemmap_verify((pte_t
*)pmd
, node
, addr
, next
);
1300 pr_warn_once("vmemmap: falling back to regular page backing\n");
1301 if (vmemmap_populate_basepages(addr
, next
, node
))
1307 int __meminit
vmemmap_populate(unsigned long start
, unsigned long end
, int node
)
1312 err
= vmemmap_populate_hugepages(start
, end
, node
);
1314 err
= vmemmap_populate_basepages(start
, end
, node
);
1316 sync_global_pgds(start
, end
- 1, 0);
1320 #if defined(CONFIG_MEMORY_HOTPLUG_SPARSE) && defined(CONFIG_HAVE_BOOTMEM_INFO_NODE)
1321 void register_page_bootmem_memmap(unsigned long section_nr
,
1322 struct page
*start_page
, unsigned long size
)
1324 unsigned long addr
= (unsigned long)start_page
;
1325 unsigned long end
= (unsigned long)(start_page
+ size
);
1330 unsigned int nr_pages
;
1333 for (; addr
< end
; addr
= next
) {
1336 pgd
= pgd_offset_k(addr
);
1337 if (pgd_none(*pgd
)) {
1338 next
= (addr
+ PAGE_SIZE
) & PAGE_MASK
;
1341 get_page_bootmem(section_nr
, pgd_page(*pgd
), MIX_SECTION_INFO
);
1343 pud
= pud_offset(pgd
, addr
);
1344 if (pud_none(*pud
)) {
1345 next
= (addr
+ PAGE_SIZE
) & PAGE_MASK
;
1348 get_page_bootmem(section_nr
, pud_page(*pud
), MIX_SECTION_INFO
);
1351 next
= (addr
+ PAGE_SIZE
) & PAGE_MASK
;
1352 pmd
= pmd_offset(pud
, addr
);
1355 get_page_bootmem(section_nr
, pmd_page(*pmd
),
1358 pte
= pte_offset_kernel(pmd
, addr
);
1361 get_page_bootmem(section_nr
, pte_page(*pte
),
1364 next
= pmd_addr_end(addr
, end
);
1366 pmd
= pmd_offset(pud
, addr
);
1370 nr_pages
= 1 << (get_order(PMD_SIZE
));
1371 page
= pmd_page(*pmd
);
1373 get_page_bootmem(section_nr
, page
++,
1380 void __meminit
vmemmap_populate_print_last(void)
1383 printk(KERN_DEBUG
" [%lx-%lx] PMD -> [%p-%p] on node %d\n",
1384 addr_start
, addr_end
-1, p_start
, p_end
-1, node_start
);