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
));
134 * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the
135 * physical space so we can cache the place of the first one and move
136 * around without checking the pgd every time.
139 pteval_t __supported_pte_mask __read_mostly
= ~0;
140 EXPORT_SYMBOL_GPL(__supported_pte_mask
);
142 int force_personality32
;
146 * Control non executable heap for 32bit processes.
147 * To control the stack too use noexec=off
149 * on PROT_READ does not imply PROT_EXEC for 32-bit processes (default)
150 * off PROT_READ implies PROT_EXEC
152 static int __init
nonx32_setup(char *str
)
154 if (!strcmp(str
, "on"))
155 force_personality32
&= ~READ_IMPLIES_EXEC
;
156 else if (!strcmp(str
, "off"))
157 force_personality32
|= READ_IMPLIES_EXEC
;
160 __setup("noexec32=", nonx32_setup
);
163 * When memory was added/removed make sure all the processes MM have
164 * suitable PGD entries in the local PGD level page.
166 void sync_global_pgds(unsigned long start
, unsigned long end
, int removed
)
168 unsigned long address
;
170 for (address
= start
; address
<= end
; address
+= PGDIR_SIZE
) {
171 const pgd_t
*pgd_ref
= pgd_offset_k(address
);
175 * When it is called after memory hot remove, pgd_none()
176 * returns true. In this case (removed == 1), we must clear
177 * the PGD entries in the local PGD level page.
179 if (pgd_none(*pgd_ref
) && !removed
)
182 spin_lock(&pgd_lock
);
183 list_for_each_entry(page
, &pgd_list
, lru
) {
185 spinlock_t
*pgt_lock
;
187 pgd
= (pgd_t
*)page_address(page
) + pgd_index(address
);
188 /* the pgt_lock only for Xen */
189 pgt_lock
= &pgd_page_get_mm(page
)->page_table_lock
;
192 if (!pgd_none(*pgd_ref
) && !pgd_none(*pgd
))
193 BUG_ON(pgd_page_vaddr(*pgd
)
194 != pgd_page_vaddr(*pgd_ref
));
197 if (pgd_none(*pgd_ref
) && !pgd_none(*pgd
))
201 set_pgd(pgd
, *pgd_ref
);
204 spin_unlock(pgt_lock
);
206 spin_unlock(&pgd_lock
);
211 * NOTE: This function is marked __ref because it calls __init function
212 * (alloc_bootmem_pages). It's safe to do it ONLY when after_bootmem == 0.
214 static __ref
void *spp_getpage(void)
219 ptr
= (void *) get_zeroed_page(GFP_ATOMIC
| __GFP_NOTRACK
);
221 ptr
= alloc_bootmem_pages(PAGE_SIZE
);
223 if (!ptr
|| ((unsigned long)ptr
& ~PAGE_MASK
)) {
224 panic("set_pte_phys: cannot allocate page data %s\n",
225 after_bootmem
? "after bootmem" : "");
228 pr_debug("spp_getpage %p\n", ptr
);
233 static pud_t
*fill_pud(pgd_t
*pgd
, unsigned long vaddr
)
235 if (pgd_none(*pgd
)) {
236 pud_t
*pud
= (pud_t
*)spp_getpage();
237 pgd_populate(&init_mm
, pgd
, pud
);
238 if (pud
!= pud_offset(pgd
, 0))
239 printk(KERN_ERR
"PAGETABLE BUG #00! %p <-> %p\n",
240 pud
, pud_offset(pgd
, 0));
242 return pud_offset(pgd
, vaddr
);
245 static pmd_t
*fill_pmd(pud_t
*pud
, unsigned long vaddr
)
247 if (pud_none(*pud
)) {
248 pmd_t
*pmd
= (pmd_t
*) spp_getpage();
249 pud_populate(&init_mm
, pud
, pmd
);
250 if (pmd
!= pmd_offset(pud
, 0))
251 printk(KERN_ERR
"PAGETABLE BUG #01! %p <-> %p\n",
252 pmd
, pmd_offset(pud
, 0));
254 return pmd_offset(pud
, vaddr
);
257 static pte_t
*fill_pte(pmd_t
*pmd
, unsigned long vaddr
)
259 if (pmd_none(*pmd
)) {
260 pte_t
*pte
= (pte_t
*) spp_getpage();
261 pmd_populate_kernel(&init_mm
, pmd
, pte
);
262 if (pte
!= pte_offset_kernel(pmd
, 0))
263 printk(KERN_ERR
"PAGETABLE BUG #02!\n");
265 return pte_offset_kernel(pmd
, vaddr
);
268 void set_pte_vaddr_pud(pud_t
*pud_page
, unsigned long vaddr
, pte_t new_pte
)
274 pud
= pud_page
+ pud_index(vaddr
);
275 pmd
= fill_pmd(pud
, vaddr
);
276 pte
= fill_pte(pmd
, vaddr
);
278 set_pte(pte
, new_pte
);
281 * It's enough to flush this one mapping.
282 * (PGE mappings get flushed as well)
284 __flush_tlb_one(vaddr
);
287 void set_pte_vaddr(unsigned long vaddr
, pte_t pteval
)
292 pr_debug("set_pte_vaddr %lx to %lx\n", vaddr
, native_pte_val(pteval
));
294 pgd
= pgd_offset_k(vaddr
);
295 if (pgd_none(*pgd
)) {
297 "PGD FIXMAP MISSING, it should be setup in head.S!\n");
300 pud_page
= (pud_t
*)pgd_page_vaddr(*pgd
);
301 set_pte_vaddr_pud(pud_page
, vaddr
, pteval
);
304 pmd_t
* __init
populate_extra_pmd(unsigned long vaddr
)
309 pgd
= pgd_offset_k(vaddr
);
310 pud
= fill_pud(pgd
, vaddr
);
311 return fill_pmd(pud
, vaddr
);
314 pte_t
* __init
populate_extra_pte(unsigned long vaddr
)
318 pmd
= populate_extra_pmd(vaddr
);
319 return fill_pte(pmd
, vaddr
);
323 * Create large page table mappings for a range of physical addresses.
325 static void __init
__init_extra_mapping(unsigned long phys
, unsigned long size
,
326 enum page_cache_mode cache
)
333 pgprot_val(prot
) = pgprot_val(PAGE_KERNEL_LARGE
) |
334 pgprot_val(pgprot_4k_2_large(cachemode2pgprot(cache
)));
335 BUG_ON((phys
& ~PMD_MASK
) || (size
& ~PMD_MASK
));
336 for (; size
; phys
+= PMD_SIZE
, size
-= PMD_SIZE
) {
337 pgd
= pgd_offset_k((unsigned long)__va(phys
));
338 if (pgd_none(*pgd
)) {
339 pud
= (pud_t
*) spp_getpage();
340 set_pgd(pgd
, __pgd(__pa(pud
) | _KERNPG_TABLE
|
343 pud
= pud_offset(pgd
, (unsigned long)__va(phys
));
344 if (pud_none(*pud
)) {
345 pmd
= (pmd_t
*) spp_getpage();
346 set_pud(pud
, __pud(__pa(pmd
) | _KERNPG_TABLE
|
349 pmd
= pmd_offset(pud
, phys
);
350 BUG_ON(!pmd_none(*pmd
));
351 set_pmd(pmd
, __pmd(phys
| pgprot_val(prot
)));
355 void __init
init_extra_mapping_wb(unsigned long phys
, unsigned long size
)
357 __init_extra_mapping(phys
, size
, _PAGE_CACHE_MODE_WB
);
360 void __init
init_extra_mapping_uc(unsigned long phys
, unsigned long size
)
362 __init_extra_mapping(phys
, size
, _PAGE_CACHE_MODE_UC
);
366 * The head.S code sets up the kernel high mapping:
368 * from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text)
370 * phys_base holds the negative offset to the kernel, which is added
371 * to the compile time generated pmds. This results in invalid pmds up
372 * to the point where we hit the physaddr 0 mapping.
374 * We limit the mappings to the region from _text to _brk_end. _brk_end
375 * is rounded up to the 2MB boundary. This catches the invalid pmds as
376 * well, as they are located before _text:
378 void __init
cleanup_highmap(void)
380 unsigned long vaddr
= __START_KERNEL_map
;
381 unsigned long vaddr_end
= __START_KERNEL_map
+ KERNEL_IMAGE_SIZE
;
382 unsigned long end
= roundup((unsigned long)_brk_end
, PMD_SIZE
) - 1;
383 pmd_t
*pmd
= level2_kernel_pgt
;
386 * Native path, max_pfn_mapped is not set yet.
387 * Xen has valid max_pfn_mapped set in
388 * arch/x86/xen/mmu.c:xen_setup_kernel_pagetable().
391 vaddr_end
= __START_KERNEL_map
+ (max_pfn_mapped
<< PAGE_SHIFT
);
393 for (; vaddr
+ PMD_SIZE
- 1 < vaddr_end
; pmd
++, vaddr
+= PMD_SIZE
) {
396 if (vaddr
< (unsigned long) _text
|| vaddr
> end
)
397 set_pmd(pmd
, __pmd(0));
401 static unsigned long __meminit
402 phys_pte_init(pte_t
*pte_page
, unsigned long addr
, unsigned long end
,
405 unsigned long pages
= 0, next
;
406 unsigned long last_map_addr
= end
;
409 pte_t
*pte
= pte_page
+ pte_index(addr
);
411 for (i
= pte_index(addr
); i
< PTRS_PER_PTE
; i
++, addr
= next
, pte
++) {
412 next
= (addr
& PAGE_MASK
) + PAGE_SIZE
;
414 if (!after_bootmem
&&
415 !e820_any_mapped(addr
& PAGE_MASK
, next
, E820_RAM
) &&
416 !e820_any_mapped(addr
& PAGE_MASK
, next
, E820_RESERVED_KERN
))
417 set_pte(pte
, __pte(0));
422 * We will re-use the existing mapping.
423 * Xen for example has some special requirements, like mapping
424 * pagetable pages as RO. So assume someone who pre-setup
425 * these mappings are more intelligent.
434 printk(" pte=%p addr=%lx pte=%016lx\n",
435 pte
, addr
, pfn_pte(addr
>> PAGE_SHIFT
, PAGE_KERNEL
).pte
);
437 set_pte(pte
, pfn_pte(addr
>> PAGE_SHIFT
, prot
));
438 last_map_addr
= (addr
& PAGE_MASK
) + PAGE_SIZE
;
441 update_page_count(PG_LEVEL_4K
, pages
);
443 return last_map_addr
;
446 static unsigned long __meminit
447 phys_pmd_init(pmd_t
*pmd_page
, unsigned long address
, unsigned long end
,
448 unsigned long page_size_mask
, pgprot_t prot
)
450 unsigned long pages
= 0, next
;
451 unsigned long last_map_addr
= end
;
453 int i
= pmd_index(address
);
455 for (; i
< PTRS_PER_PMD
; i
++, address
= next
) {
456 pmd_t
*pmd
= pmd_page
+ pmd_index(address
);
458 pgprot_t new_prot
= prot
;
460 next
= (address
& PMD_MASK
) + PMD_SIZE
;
461 if (address
>= end
) {
462 if (!after_bootmem
&&
463 !e820_any_mapped(address
& PMD_MASK
, next
, E820_RAM
) &&
464 !e820_any_mapped(address
& PMD_MASK
, next
, E820_RESERVED_KERN
))
465 set_pmd(pmd
, __pmd(0));
470 if (!pmd_large(*pmd
)) {
471 spin_lock(&init_mm
.page_table_lock
);
472 pte
= (pte_t
*)pmd_page_vaddr(*pmd
);
473 last_map_addr
= phys_pte_init(pte
, address
,
475 spin_unlock(&init_mm
.page_table_lock
);
479 * If we are ok with PG_LEVEL_2M mapping, then we will
480 * use the existing mapping,
482 * Otherwise, we will split the large page mapping but
483 * use the same existing protection bits except for
484 * large page, so that we don't violate Intel's TLB
485 * Application note (317080) which says, while changing
486 * the page sizes, new and old translations should
487 * not differ with respect to page frame and
490 if (page_size_mask
& (1 << PG_LEVEL_2M
)) {
493 last_map_addr
= next
;
496 new_prot
= pte_pgprot(pte_clrhuge(*(pte_t
*)pmd
));
499 if (page_size_mask
& (1<<PG_LEVEL_2M
)) {
501 spin_lock(&init_mm
.page_table_lock
);
502 set_pte((pte_t
*)pmd
,
503 pfn_pte((address
& PMD_MASK
) >> PAGE_SHIFT
,
504 __pgprot(pgprot_val(prot
) | _PAGE_PSE
)));
505 spin_unlock(&init_mm
.page_table_lock
);
506 last_map_addr
= next
;
510 pte
= alloc_low_page();
511 last_map_addr
= phys_pte_init(pte
, address
, end
, new_prot
);
513 spin_lock(&init_mm
.page_table_lock
);
514 pmd_populate_kernel(&init_mm
, pmd
, pte
);
515 spin_unlock(&init_mm
.page_table_lock
);
517 update_page_count(PG_LEVEL_2M
, pages
);
518 return last_map_addr
;
521 static unsigned long __meminit
522 phys_pud_init(pud_t
*pud_page
, unsigned long addr
, unsigned long end
,
523 unsigned long page_size_mask
)
525 unsigned long pages
= 0, next
;
526 unsigned long last_map_addr
= end
;
527 int i
= pud_index(addr
);
529 for (; i
< PTRS_PER_PUD
; i
++, addr
= next
) {
530 pud_t
*pud
= pud_page
+ pud_index(addr
);
532 pgprot_t prot
= PAGE_KERNEL
;
534 next
= (addr
& PUD_MASK
) + PUD_SIZE
;
536 if (!after_bootmem
&&
537 !e820_any_mapped(addr
& PUD_MASK
, next
, E820_RAM
) &&
538 !e820_any_mapped(addr
& PUD_MASK
, next
, E820_RESERVED_KERN
))
539 set_pud(pud
, __pud(0));
544 if (!pud_large(*pud
)) {
545 pmd
= pmd_offset(pud
, 0);
546 last_map_addr
= phys_pmd_init(pmd
, addr
, end
,
547 page_size_mask
, prot
);
552 * If we are ok with PG_LEVEL_1G mapping, then we will
553 * use the existing mapping.
555 * Otherwise, we will split the gbpage mapping but use
556 * the same existing protection bits except for large
557 * page, so that we don't violate Intel's TLB
558 * Application note (317080) which says, while changing
559 * the page sizes, new and old translations should
560 * not differ with respect to page frame and
563 if (page_size_mask
& (1 << PG_LEVEL_1G
)) {
566 last_map_addr
= next
;
569 prot
= pte_pgprot(pte_clrhuge(*(pte_t
*)pud
));
572 if (page_size_mask
& (1<<PG_LEVEL_1G
)) {
574 spin_lock(&init_mm
.page_table_lock
);
575 set_pte((pte_t
*)pud
,
576 pfn_pte((addr
& PUD_MASK
) >> PAGE_SHIFT
,
578 spin_unlock(&init_mm
.page_table_lock
);
579 last_map_addr
= next
;
583 pmd
= alloc_low_page();
584 last_map_addr
= phys_pmd_init(pmd
, addr
, end
, page_size_mask
,
587 spin_lock(&init_mm
.page_table_lock
);
588 pud_populate(&init_mm
, pud
, pmd
);
589 spin_unlock(&init_mm
.page_table_lock
);
593 update_page_count(PG_LEVEL_1G
, pages
);
595 return last_map_addr
;
598 unsigned long __meminit
599 kernel_physical_mapping_init(unsigned long start
,
601 unsigned long page_size_mask
)
603 bool pgd_changed
= false;
604 unsigned long next
, last_map_addr
= end
;
607 start
= (unsigned long)__va(start
);
608 end
= (unsigned long)__va(end
);
611 for (; start
< end
; start
= next
) {
612 pgd_t
*pgd
= pgd_offset_k(start
);
615 next
= (start
& PGDIR_MASK
) + PGDIR_SIZE
;
618 pud
= (pud_t
*)pgd_page_vaddr(*pgd
);
619 last_map_addr
= phys_pud_init(pud
, __pa(start
),
620 __pa(end
), page_size_mask
);
624 pud
= alloc_low_page();
625 last_map_addr
= phys_pud_init(pud
, __pa(start
), __pa(end
),
628 spin_lock(&init_mm
.page_table_lock
);
629 pgd_populate(&init_mm
, pgd
, pud
);
630 spin_unlock(&init_mm
.page_table_lock
);
635 sync_global_pgds(addr
, end
- 1, 0);
639 return last_map_addr
;
643 void __init
initmem_init(void)
645 memblock_set_node(0, (phys_addr_t
)ULLONG_MAX
, &memblock
.memory
, 0);
649 void __init
paging_init(void)
651 sparse_memory_present_with_active_regions(MAX_NUMNODES
);
655 * clear the default setting with node 0
656 * note: don't use nodes_clear here, that is really clearing when
657 * numa support is not compiled in, and later node_set_state
658 * will not set it back.
660 node_clear_state(0, N_MEMORY
);
661 if (N_MEMORY
!= N_NORMAL_MEMORY
)
662 node_clear_state(0, N_NORMAL_MEMORY
);
668 * Memory hotplug specific functions
670 #ifdef CONFIG_MEMORY_HOTPLUG
672 * After memory hotplug the variables max_pfn, max_low_pfn and high_memory need
675 static void update_end_of_memory_vars(u64 start
, u64 size
)
677 unsigned long end_pfn
= PFN_UP(start
+ size
);
679 if (end_pfn
> max_pfn
) {
681 max_low_pfn
= end_pfn
;
682 high_memory
= (void *)__va(max_pfn
* PAGE_SIZE
- 1) + 1;
687 * Memory is added always to NORMAL zone. This means you will never get
688 * additional DMA/DMA32 memory.
690 int arch_add_memory(int nid
, u64 start
, u64 size
, bool for_device
)
692 struct pglist_data
*pgdat
= NODE_DATA(nid
);
693 struct zone
*zone
= pgdat
->node_zones
+
694 zone_for_memory(nid
, start
, size
, ZONE_NORMAL
, for_device
);
695 unsigned long start_pfn
= start
>> PAGE_SHIFT
;
696 unsigned long nr_pages
= size
>> PAGE_SHIFT
;
699 init_memory_mapping(start
, start
+ size
);
701 ret
= __add_pages(nid
, zone
, start_pfn
, nr_pages
);
704 /* update max_pfn, max_low_pfn and high_memory */
705 update_end_of_memory_vars(start
, size
);
709 EXPORT_SYMBOL_GPL(arch_add_memory
);
711 #define PAGE_INUSE 0xFD
713 static void __meminit
free_pagetable(struct page
*page
, int order
)
716 unsigned int nr_pages
= 1 << order
;
718 /* bootmem page has reserved flag */
719 if (PageReserved(page
)) {
720 __ClearPageReserved(page
);
722 magic
= (unsigned long)page
->lru
.next
;
723 if (magic
== SECTION_INFO
|| magic
== MIX_SECTION_INFO
) {
725 put_page_bootmem(page
++);
728 free_reserved_page(page
++);
730 free_pages((unsigned long)page_address(page
), order
);
733 static void __meminit
free_pte_table(pte_t
*pte_start
, pmd_t
*pmd
)
738 for (i
= 0; i
< PTRS_PER_PTE
; i
++) {
744 /* free a pte talbe */
745 free_pagetable(pmd_page(*pmd
), 0);
746 spin_lock(&init_mm
.page_table_lock
);
748 spin_unlock(&init_mm
.page_table_lock
);
751 static void __meminit
free_pmd_table(pmd_t
*pmd_start
, pud_t
*pud
)
756 for (i
= 0; i
< PTRS_PER_PMD
; i
++) {
762 /* free a pmd talbe */
763 free_pagetable(pud_page(*pud
), 0);
764 spin_lock(&init_mm
.page_table_lock
);
766 spin_unlock(&init_mm
.page_table_lock
);
769 /* Return true if pgd is changed, otherwise return false. */
770 static bool __meminit
free_pud_table(pud_t
*pud_start
, pgd_t
*pgd
)
775 for (i
= 0; i
< PTRS_PER_PUD
; i
++) {
781 /* free a pud table */
782 free_pagetable(pgd_page(*pgd
), 0);
783 spin_lock(&init_mm
.page_table_lock
);
785 spin_unlock(&init_mm
.page_table_lock
);
790 static void __meminit
791 remove_pte_table(pte_t
*pte_start
, unsigned long addr
, unsigned long end
,
794 unsigned long next
, pages
= 0;
797 phys_addr_t phys_addr
;
799 pte
= pte_start
+ pte_index(addr
);
800 for (; addr
< end
; addr
= next
, pte
++) {
801 next
= (addr
+ PAGE_SIZE
) & PAGE_MASK
;
805 if (!pte_present(*pte
))
809 * We mapped [0,1G) memory as identity mapping when
810 * initializing, in arch/x86/kernel/head_64.S. These
811 * pagetables cannot be removed.
813 phys_addr
= pte_val(*pte
) + (addr
& PAGE_MASK
);
814 if (phys_addr
< (phys_addr_t
)0x40000000)
817 if (IS_ALIGNED(addr
, PAGE_SIZE
) &&
818 IS_ALIGNED(next
, PAGE_SIZE
)) {
820 * Do not free direct mapping pages since they were
821 * freed when offlining, or simplely not in use.
824 free_pagetable(pte_page(*pte
), 0);
826 spin_lock(&init_mm
.page_table_lock
);
827 pte_clear(&init_mm
, addr
, pte
);
828 spin_unlock(&init_mm
.page_table_lock
);
830 /* For non-direct mapping, pages means nothing. */
834 * If we are here, we are freeing vmemmap pages since
835 * direct mapped memory ranges to be freed are aligned.
837 * If we are not removing the whole page, it means
838 * other page structs in this page are being used and
839 * we canot remove them. So fill the unused page_structs
840 * with 0xFD, and remove the page when it is wholly
843 memset((void *)addr
, PAGE_INUSE
, next
- addr
);
845 page_addr
= page_address(pte_page(*pte
));
846 if (!memchr_inv(page_addr
, PAGE_INUSE
, PAGE_SIZE
)) {
847 free_pagetable(pte_page(*pte
), 0);
849 spin_lock(&init_mm
.page_table_lock
);
850 pte_clear(&init_mm
, addr
, pte
);
851 spin_unlock(&init_mm
.page_table_lock
);
856 /* Call free_pte_table() in remove_pmd_table(). */
859 update_page_count(PG_LEVEL_4K
, -pages
);
862 static void __meminit
863 remove_pmd_table(pmd_t
*pmd_start
, unsigned long addr
, unsigned long end
,
866 unsigned long next
, pages
= 0;
871 pmd
= pmd_start
+ pmd_index(addr
);
872 for (; addr
< end
; addr
= next
, pmd
++) {
873 next
= pmd_addr_end(addr
, end
);
875 if (!pmd_present(*pmd
))
878 if (pmd_large(*pmd
)) {
879 if (IS_ALIGNED(addr
, PMD_SIZE
) &&
880 IS_ALIGNED(next
, PMD_SIZE
)) {
882 free_pagetable(pmd_page(*pmd
),
883 get_order(PMD_SIZE
));
885 spin_lock(&init_mm
.page_table_lock
);
887 spin_unlock(&init_mm
.page_table_lock
);
890 /* If here, we are freeing vmemmap pages. */
891 memset((void *)addr
, PAGE_INUSE
, next
- addr
);
893 page_addr
= page_address(pmd_page(*pmd
));
894 if (!memchr_inv(page_addr
, PAGE_INUSE
,
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
);
908 pte_base
= (pte_t
*)pmd_page_vaddr(*pmd
);
909 remove_pte_table(pte_base
, addr
, next
, direct
);
910 free_pte_table(pte_base
, pmd
);
913 /* Call free_pmd_table() in remove_pud_table(). */
915 update_page_count(PG_LEVEL_2M
, -pages
);
918 static void __meminit
919 remove_pud_table(pud_t
*pud_start
, unsigned long addr
, unsigned long end
,
922 unsigned long next
, pages
= 0;
927 pud
= pud_start
+ pud_index(addr
);
928 for (; addr
< end
; addr
= next
, pud
++) {
929 next
= pud_addr_end(addr
, end
);
931 if (!pud_present(*pud
))
934 if (pud_large(*pud
)) {
935 if (IS_ALIGNED(addr
, PUD_SIZE
) &&
936 IS_ALIGNED(next
, PUD_SIZE
)) {
938 free_pagetable(pud_page(*pud
),
939 get_order(PUD_SIZE
));
941 spin_lock(&init_mm
.page_table_lock
);
943 spin_unlock(&init_mm
.page_table_lock
);
946 /* If here, we are freeing vmemmap pages. */
947 memset((void *)addr
, PAGE_INUSE
, next
- addr
);
949 page_addr
= page_address(pud_page(*pud
));
950 if (!memchr_inv(page_addr
, PAGE_INUSE
,
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
);
964 pmd_base
= (pmd_t
*)pud_page_vaddr(*pud
);
965 remove_pmd_table(pmd_base
, addr
, next
, direct
);
966 free_pmd_table(pmd_base
, pud
);
970 update_page_count(PG_LEVEL_1G
, -pages
);
973 /* start and end are both virtual address. */
974 static void __meminit
975 remove_pagetable(unsigned long start
, unsigned long end
, bool direct
)
981 bool pgd_changed
= false;
983 for (addr
= start
; addr
< end
; addr
= next
) {
984 next
= pgd_addr_end(addr
, end
);
986 pgd
= pgd_offset_k(addr
);
987 if (!pgd_present(*pgd
))
990 pud
= (pud_t
*)pgd_page_vaddr(*pgd
);
991 remove_pud_table(pud
, addr
, next
, direct
);
992 if (free_pud_table(pud
, pgd
))
997 sync_global_pgds(start
, end
- 1, 1);
1002 void __ref
vmemmap_free(unsigned long start
, unsigned long end
)
1004 remove_pagetable(start
, end
, false);
1007 #ifdef CONFIG_MEMORY_HOTREMOVE
1008 static void __meminit
1009 kernel_physical_mapping_remove(unsigned long start
, unsigned long end
)
1011 start
= (unsigned long)__va(start
);
1012 end
= (unsigned long)__va(end
);
1014 remove_pagetable(start
, end
, true);
1017 int __ref
arch_remove_memory(u64 start
, u64 size
)
1019 unsigned long start_pfn
= start
>> PAGE_SHIFT
;
1020 unsigned long nr_pages
= size
>> PAGE_SHIFT
;
1024 zone
= page_zone(pfn_to_page(start_pfn
));
1025 kernel_physical_mapping_remove(start
, start
+ size
);
1026 ret
= __remove_pages(zone
, start_pfn
, nr_pages
);
1032 #endif /* CONFIG_MEMORY_HOTPLUG */
1034 static struct kcore_list kcore_vsyscall
;
1036 static void __init
register_page_bootmem_info(void)
1041 for_each_online_node(i
)
1042 register_page_bootmem_info_node(NODE_DATA(i
));
1046 void __init
mem_init(void)
1050 /* clear_bss() already clear the empty_zero_page */
1052 register_page_bootmem_info();
1054 /* this will put all memory onto the freelists */
1058 /* Register memory areas for /proc/kcore */
1059 kclist_add(&kcore_vsyscall
, (void *)VSYSCALL_ADDR
,
1060 PAGE_SIZE
, KCORE_OTHER
);
1062 mem_init_print_info(NULL
);
1065 #ifdef CONFIG_DEBUG_RODATA
1066 const int rodata_test_data
= 0xC3;
1067 EXPORT_SYMBOL_GPL(rodata_test_data
);
1069 int kernel_set_to_readonly
;
1071 void set_kernel_text_rw(void)
1073 unsigned long start
= PFN_ALIGN(_text
);
1074 unsigned long end
= PFN_ALIGN(__stop___ex_table
);
1076 if (!kernel_set_to_readonly
)
1079 pr_debug("Set kernel text: %lx - %lx for read write\n",
1083 * Make the kernel identity mapping for text RW. Kernel text
1084 * mapping will always be RO. Refer to the comment in
1085 * static_protections() in pageattr.c
1087 set_memory_rw(start
, (end
- start
) >> PAGE_SHIFT
);
1090 void set_kernel_text_ro(void)
1092 unsigned long start
= PFN_ALIGN(_text
);
1093 unsigned long end
= PFN_ALIGN(__stop___ex_table
);
1095 if (!kernel_set_to_readonly
)
1098 pr_debug("Set kernel text: %lx - %lx for read only\n",
1102 * Set the kernel identity mapping for text RO.
1104 set_memory_ro(start
, (end
- start
) >> PAGE_SHIFT
);
1107 void mark_rodata_ro(void)
1109 unsigned long start
= PFN_ALIGN(_text
);
1110 unsigned long rodata_start
= PFN_ALIGN(__start_rodata
);
1111 unsigned long end
= (unsigned long) &__end_rodata_hpage_align
;
1112 unsigned long text_end
= PFN_ALIGN(&__stop___ex_table
);
1113 unsigned long rodata_end
= PFN_ALIGN(&__end_rodata
);
1114 unsigned long all_end
;
1116 printk(KERN_INFO
"Write protecting the kernel read-only data: %luk\n",
1117 (end
- start
) >> 10);
1118 set_memory_ro(start
, (end
- start
) >> PAGE_SHIFT
);
1120 kernel_set_to_readonly
= 1;
1123 * The rodata/data/bss/brk section (but not the kernel text!)
1124 * should also be not-executable.
1126 * We align all_end to PMD_SIZE because the existing mapping
1127 * is a full PMD. If we would align _brk_end to PAGE_SIZE we
1128 * split the PMD and the reminder between _brk_end and the end
1129 * of the PMD will remain mapped executable.
1131 * Any PMD which was setup after the one which covers _brk_end
1132 * has been zapped already via cleanup_highmem().
1134 all_end
= roundup((unsigned long)_brk_end
, PMD_SIZE
);
1135 set_memory_nx(text_end
, (all_end
- text_end
) >> PAGE_SHIFT
);
1139 #ifdef CONFIG_CPA_DEBUG
1140 printk(KERN_INFO
"Testing CPA: undo %lx-%lx\n", start
, end
);
1141 set_memory_rw(start
, (end
-start
) >> PAGE_SHIFT
);
1143 printk(KERN_INFO
"Testing CPA: again\n");
1144 set_memory_ro(start
, (end
-start
) >> PAGE_SHIFT
);
1147 free_init_pages("unused kernel",
1148 (unsigned long) __va(__pa_symbol(text_end
)),
1149 (unsigned long) __va(__pa_symbol(rodata_start
)));
1150 free_init_pages("unused kernel",
1151 (unsigned long) __va(__pa_symbol(rodata_end
)),
1152 (unsigned long) __va(__pa_symbol(_sdata
)));
1159 int kern_addr_valid(unsigned long addr
)
1161 unsigned long above
= ((long)addr
) >> __VIRTUAL_MASK_SHIFT
;
1167 if (above
!= 0 && above
!= -1UL)
1170 pgd
= pgd_offset_k(addr
);
1174 pud
= pud_offset(pgd
, addr
);
1178 if (pud_large(*pud
))
1179 return pfn_valid(pud_pfn(*pud
));
1181 pmd
= pmd_offset(pud
, addr
);
1185 if (pmd_large(*pmd
))
1186 return pfn_valid(pmd_pfn(*pmd
));
1188 pte
= pte_offset_kernel(pmd
, addr
);
1192 return pfn_valid(pte_pfn(*pte
));
1195 static unsigned long probe_memory_block_size(void)
1198 unsigned long bz
= 1UL<<31;
1200 if (totalram_pages
>= (64ULL << (30 - PAGE_SHIFT
))) {
1201 pr_info("Using 2GB memory block size for large-memory system\n");
1202 return 2UL * 1024 * 1024 * 1024;
1205 /* less than 64g installed */
1206 if ((max_pfn
<< PAGE_SHIFT
) < (16UL << 32))
1207 return MIN_MEMORY_BLOCK_SIZE
;
1209 /* get the tail size */
1210 while (bz
> MIN_MEMORY_BLOCK_SIZE
) {
1211 if (!((max_pfn
<< PAGE_SHIFT
) & (bz
- 1)))
1216 printk(KERN_DEBUG
"memory block size : %ldMB\n", bz
>> 20);
1221 static unsigned long memory_block_size_probed
;
1222 unsigned long memory_block_size_bytes(void)
1224 if (!memory_block_size_probed
)
1225 memory_block_size_probed
= probe_memory_block_size();
1227 return memory_block_size_probed
;
1230 #ifdef CONFIG_SPARSEMEM_VMEMMAP
1232 * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
1234 static long __meminitdata addr_start
, addr_end
;
1235 static void __meminitdata
*p_start
, *p_end
;
1236 static int __meminitdata node_start
;
1238 static int __meminit
vmemmap_populate_hugepages(unsigned long start
,
1239 unsigned long end
, int node
)
1247 for (addr
= start
; addr
< end
; addr
= next
) {
1248 next
= pmd_addr_end(addr
, end
);
1250 pgd
= vmemmap_pgd_populate(addr
, node
);
1254 pud
= vmemmap_pud_populate(pgd
, addr
, node
);
1258 pmd
= pmd_offset(pud
, addr
);
1259 if (pmd_none(*pmd
)) {
1262 p
= vmemmap_alloc_block_buf(PMD_SIZE
, node
);
1266 entry
= pfn_pte(__pa(p
) >> PAGE_SHIFT
,
1268 set_pmd(pmd
, __pmd(pte_val(entry
)));
1270 /* check to see if we have contiguous blocks */
1271 if (p_end
!= p
|| node_start
!= node
) {
1273 pr_debug(" [%lx-%lx] PMD -> [%p-%p] on node %d\n",
1274 addr_start
, addr_end
-1, p_start
, p_end
-1, node_start
);
1280 addr_end
= addr
+ PMD_SIZE
;
1281 p_end
= p
+ PMD_SIZE
;
1284 } else if (pmd_large(*pmd
)) {
1285 vmemmap_verify((pte_t
*)pmd
, node
, addr
, next
);
1288 pr_warn_once("vmemmap: falling back to regular page backing\n");
1289 if (vmemmap_populate_basepages(addr
, next
, node
))
1295 int __meminit
vmemmap_populate(unsigned long start
, unsigned long end
, int node
)
1300 err
= vmemmap_populate_hugepages(start
, end
, node
);
1302 err
= vmemmap_populate_basepages(start
, end
, node
);
1304 sync_global_pgds(start
, end
- 1, 0);
1308 #if defined(CONFIG_MEMORY_HOTPLUG_SPARSE) && defined(CONFIG_HAVE_BOOTMEM_INFO_NODE)
1309 void register_page_bootmem_memmap(unsigned long section_nr
,
1310 struct page
*start_page
, unsigned long size
)
1312 unsigned long addr
= (unsigned long)start_page
;
1313 unsigned long end
= (unsigned long)(start_page
+ size
);
1318 unsigned int nr_pages
;
1321 for (; addr
< end
; addr
= next
) {
1324 pgd
= pgd_offset_k(addr
);
1325 if (pgd_none(*pgd
)) {
1326 next
= (addr
+ PAGE_SIZE
) & PAGE_MASK
;
1329 get_page_bootmem(section_nr
, pgd_page(*pgd
), MIX_SECTION_INFO
);
1331 pud
= pud_offset(pgd
, addr
);
1332 if (pud_none(*pud
)) {
1333 next
= (addr
+ PAGE_SIZE
) & PAGE_MASK
;
1336 get_page_bootmem(section_nr
, pud_page(*pud
), MIX_SECTION_INFO
);
1339 next
= (addr
+ PAGE_SIZE
) & PAGE_MASK
;
1340 pmd
= pmd_offset(pud
, addr
);
1343 get_page_bootmem(section_nr
, pmd_page(*pmd
),
1346 pte
= pte_offset_kernel(pmd
, addr
);
1349 get_page_bootmem(section_nr
, pte_page(*pte
),
1352 next
= pmd_addr_end(addr
, end
);
1354 pmd
= pmd_offset(pud
, addr
);
1358 nr_pages
= 1 << (get_order(PMD_SIZE
));
1359 page
= pmd_page(*pmd
);
1361 get_page_bootmem(section_nr
, page
++,
1368 void __meminit
vmemmap_populate_print_last(void)
1371 pr_debug(" [%lx-%lx] PMD -> [%p-%p] on node %d\n",
1372 addr_start
, addr_end
-1, p_start
, p_end
-1, node_start
);