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/uv/uv.h>
56 #include <asm/setup.h>
58 #include "mm_internal.h"
60 static void ident_pmd_init(unsigned long pmd_flag
, pmd_t
*pmd_page
,
61 unsigned long addr
, unsigned long end
)
64 for (; addr
< end
; addr
+= PMD_SIZE
) {
65 pmd_t
*pmd
= pmd_page
+ pmd_index(addr
);
67 if (!pmd_present(*pmd
))
68 set_pmd(pmd
, __pmd(addr
| pmd_flag
));
71 static int ident_pud_init(struct x86_mapping_info
*info
, pud_t
*pud_page
,
72 unsigned long addr
, unsigned long end
)
76 for (; addr
< end
; addr
= next
) {
77 pud_t
*pud
= pud_page
+ pud_index(addr
);
80 next
= (addr
& PUD_MASK
) + PUD_SIZE
;
84 if (pud_present(*pud
)) {
85 pmd
= pmd_offset(pud
, 0);
86 ident_pmd_init(info
->pmd_flag
, pmd
, addr
, next
);
89 pmd
= (pmd_t
*)info
->alloc_pgt_page(info
->context
);
92 ident_pmd_init(info
->pmd_flag
, pmd
, addr
, next
);
93 set_pud(pud
, __pud(__pa(pmd
) | _KERNPG_TABLE
));
99 int kernel_ident_mapping_init(struct x86_mapping_info
*info
, pgd_t
*pgd_page
,
100 unsigned long addr
, unsigned long end
)
104 int off
= info
->kernel_mapping
? pgd_index(__PAGE_OFFSET
) : 0;
106 for (; addr
< end
; addr
= next
) {
107 pgd_t
*pgd
= pgd_page
+ pgd_index(addr
) + off
;
110 next
= (addr
& PGDIR_MASK
) + PGDIR_SIZE
;
114 if (pgd_present(*pgd
)) {
115 pud
= pud_offset(pgd
, 0);
116 result
= ident_pud_init(info
, pud
, addr
, next
);
122 pud
= (pud_t
*)info
->alloc_pgt_page(info
->context
);
125 result
= ident_pud_init(info
, pud
, addr
, next
);
128 set_pgd(pgd
, __pgd(__pa(pud
) | _KERNPG_TABLE
));
134 static int __init
parse_direct_gbpages_off(char *arg
)
139 early_param("nogbpages", parse_direct_gbpages_off
);
141 static int __init
parse_direct_gbpages_on(char *arg
)
146 early_param("gbpages", parse_direct_gbpages_on
);
149 * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the
150 * physical space so we can cache the place of the first one and move
151 * around without checking the pgd every time.
154 pteval_t __supported_pte_mask __read_mostly
= ~_PAGE_IOMAP
;
155 EXPORT_SYMBOL_GPL(__supported_pte_mask
);
157 int force_personality32
;
161 * Control non executable heap for 32bit processes.
162 * To control the stack too use noexec=off
164 * on PROT_READ does not imply PROT_EXEC for 32-bit processes (default)
165 * off PROT_READ implies PROT_EXEC
167 static int __init
nonx32_setup(char *str
)
169 if (!strcmp(str
, "on"))
170 force_personality32
&= ~READ_IMPLIES_EXEC
;
171 else if (!strcmp(str
, "off"))
172 force_personality32
|= READ_IMPLIES_EXEC
;
175 __setup("noexec32=", nonx32_setup
);
178 * When memory was added/removed make sure all the processes MM have
179 * suitable PGD entries in the local PGD level page.
181 void sync_global_pgds(unsigned long start
, unsigned long end
)
183 unsigned long address
;
185 for (address
= start
; address
<= end
; address
+= PGDIR_SIZE
) {
186 const pgd_t
*pgd_ref
= pgd_offset_k(address
);
189 if (pgd_none(*pgd_ref
))
192 spin_lock(&pgd_lock
);
193 list_for_each_entry(page
, &pgd_list
, lru
) {
195 spinlock_t
*pgt_lock
;
197 pgd
= (pgd_t
*)page_address(page
) + pgd_index(address
);
198 /* the pgt_lock only for Xen */
199 pgt_lock
= &pgd_page_get_mm(page
)->page_table_lock
;
203 set_pgd(pgd
, *pgd_ref
);
205 BUG_ON(pgd_page_vaddr(*pgd
)
206 != pgd_page_vaddr(*pgd_ref
));
208 spin_unlock(pgt_lock
);
210 spin_unlock(&pgd_lock
);
215 * NOTE: This function is marked __ref because it calls __init function
216 * (alloc_bootmem_pages). It's safe to do it ONLY when after_bootmem == 0.
218 static __ref
void *spp_getpage(void)
223 ptr
= (void *) get_zeroed_page(GFP_ATOMIC
| __GFP_NOTRACK
);
225 ptr
= alloc_bootmem_pages(PAGE_SIZE
);
227 if (!ptr
|| ((unsigned long)ptr
& ~PAGE_MASK
)) {
228 panic("set_pte_phys: cannot allocate page data %s\n",
229 after_bootmem
? "after bootmem" : "");
232 pr_debug("spp_getpage %p\n", ptr
);
237 static pud_t
*fill_pud(pgd_t
*pgd
, unsigned long vaddr
)
239 if (pgd_none(*pgd
)) {
240 pud_t
*pud
= (pud_t
*)spp_getpage();
241 pgd_populate(&init_mm
, pgd
, pud
);
242 if (pud
!= pud_offset(pgd
, 0))
243 printk(KERN_ERR
"PAGETABLE BUG #00! %p <-> %p\n",
244 pud
, pud_offset(pgd
, 0));
246 return pud_offset(pgd
, vaddr
);
249 static pmd_t
*fill_pmd(pud_t
*pud
, unsigned long vaddr
)
251 if (pud_none(*pud
)) {
252 pmd_t
*pmd
= (pmd_t
*) spp_getpage();
253 pud_populate(&init_mm
, pud
, pmd
);
254 if (pmd
!= pmd_offset(pud
, 0))
255 printk(KERN_ERR
"PAGETABLE BUG #01! %p <-> %p\n",
256 pmd
, pmd_offset(pud
, 0));
258 return pmd_offset(pud
, vaddr
);
261 static pte_t
*fill_pte(pmd_t
*pmd
, unsigned long vaddr
)
263 if (pmd_none(*pmd
)) {
264 pte_t
*pte
= (pte_t
*) spp_getpage();
265 pmd_populate_kernel(&init_mm
, pmd
, pte
);
266 if (pte
!= pte_offset_kernel(pmd
, 0))
267 printk(KERN_ERR
"PAGETABLE BUG #02!\n");
269 return pte_offset_kernel(pmd
, vaddr
);
272 void set_pte_vaddr_pud(pud_t
*pud_page
, unsigned long vaddr
, pte_t new_pte
)
278 pud
= pud_page
+ pud_index(vaddr
);
279 pmd
= fill_pmd(pud
, vaddr
);
280 pte
= fill_pte(pmd
, vaddr
);
282 set_pte(pte
, new_pte
);
285 * It's enough to flush this one mapping.
286 * (PGE mappings get flushed as well)
288 __flush_tlb_one(vaddr
);
291 void set_pte_vaddr(unsigned long vaddr
, pte_t pteval
)
296 pr_debug("set_pte_vaddr %lx to %lx\n", vaddr
, native_pte_val(pteval
));
298 pgd
= pgd_offset_k(vaddr
);
299 if (pgd_none(*pgd
)) {
301 "PGD FIXMAP MISSING, it should be setup in head.S!\n");
304 pud_page
= (pud_t
*)pgd_page_vaddr(*pgd
);
305 set_pte_vaddr_pud(pud_page
, vaddr
, pteval
);
308 pmd_t
* __init
populate_extra_pmd(unsigned long vaddr
)
313 pgd
= pgd_offset_k(vaddr
);
314 pud
= fill_pud(pgd
, vaddr
);
315 return fill_pmd(pud
, vaddr
);
318 pte_t
* __init
populate_extra_pte(unsigned long vaddr
)
322 pmd
= populate_extra_pmd(vaddr
);
323 return fill_pte(pmd
, vaddr
);
327 * Create large page table mappings for a range of physical addresses.
329 static void __init
__init_extra_mapping(unsigned long phys
, unsigned long size
,
336 BUG_ON((phys
& ~PMD_MASK
) || (size
& ~PMD_MASK
));
337 for (; size
; phys
+= PMD_SIZE
, size
-= PMD_SIZE
) {
338 pgd
= pgd_offset_k((unsigned long)__va(phys
));
339 if (pgd_none(*pgd
)) {
340 pud
= (pud_t
*) spp_getpage();
341 set_pgd(pgd
, __pgd(__pa(pud
) | _KERNPG_TABLE
|
344 pud
= pud_offset(pgd
, (unsigned long)__va(phys
));
345 if (pud_none(*pud
)) {
346 pmd
= (pmd_t
*) spp_getpage();
347 set_pud(pud
, __pud(__pa(pmd
) | _KERNPG_TABLE
|
350 pmd
= pmd_offset(pud
, phys
);
351 BUG_ON(!pmd_none(*pmd
));
352 set_pmd(pmd
, __pmd(phys
| pgprot_val(prot
)));
356 void __init
init_extra_mapping_wb(unsigned long phys
, unsigned long size
)
358 __init_extra_mapping(phys
, size
, PAGE_KERNEL_LARGE
);
361 void __init
init_extra_mapping_uc(unsigned long phys
, unsigned long size
)
363 __init_extra_mapping(phys
, size
, PAGE_KERNEL_LARGE_NOCACHE
);
367 * The head.S code sets up the kernel high mapping:
369 * from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text)
371 * phys_addr holds the negative offset to the kernel, which is added
372 * to the compile time generated pmds. This results in invalid pmds up
373 * to the point where we hit the physaddr 0 mapping.
375 * We limit the mappings to the region from _text to _brk_end. _brk_end
376 * is rounded up to the 2MB boundary. This catches the invalid pmds as
377 * well, as they are located before _text:
379 void __init
cleanup_highmap(void)
381 unsigned long vaddr
= __START_KERNEL_map
;
382 unsigned long vaddr_end
= __START_KERNEL_map
+ KERNEL_IMAGE_SIZE
;
383 unsigned long end
= roundup((unsigned long)_brk_end
, PMD_SIZE
) - 1;
384 pmd_t
*pmd
= level2_kernel_pgt
;
387 * Native path, max_pfn_mapped is not set yet.
388 * Xen has valid max_pfn_mapped set in
389 * arch/x86/xen/mmu.c:xen_setup_kernel_pagetable().
392 vaddr_end
= __START_KERNEL_map
+ (max_pfn_mapped
<< PAGE_SHIFT
);
394 for (; vaddr
+ PMD_SIZE
- 1 < vaddr_end
; pmd
++, vaddr
+= PMD_SIZE
) {
397 if (vaddr
< (unsigned long) _text
|| vaddr
> end
)
398 set_pmd(pmd
, __pmd(0));
402 static unsigned long __meminit
403 phys_pte_init(pte_t
*pte_page
, unsigned long addr
, unsigned long end
,
406 unsigned long pages
= 0, next
;
407 unsigned long last_map_addr
= end
;
410 pte_t
*pte
= pte_page
+ pte_index(addr
);
412 for (i
= pte_index(addr
); i
< PTRS_PER_PTE
; i
++, addr
= next
, pte
++) {
413 next
= (addr
& PAGE_MASK
) + PAGE_SIZE
;
415 if (!after_bootmem
&&
416 !e820_any_mapped(addr
& PAGE_MASK
, next
, E820_RAM
) &&
417 !e820_any_mapped(addr
& PAGE_MASK
, next
, E820_RESERVED_KERN
))
418 set_pte(pte
, __pte(0));
423 * We will re-use the existing mapping.
424 * Xen for example has some special requirements, like mapping
425 * pagetable pages as RO. So assume someone who pre-setup
426 * these mappings are more intelligent.
435 printk(" pte=%p addr=%lx pte=%016lx\n",
436 pte
, addr
, pfn_pte(addr
>> PAGE_SHIFT
, PAGE_KERNEL
).pte
);
438 set_pte(pte
, pfn_pte(addr
>> PAGE_SHIFT
, prot
));
439 last_map_addr
= (addr
& PAGE_MASK
) + PAGE_SIZE
;
442 update_page_count(PG_LEVEL_4K
, pages
);
444 return last_map_addr
;
447 static unsigned long __meminit
448 phys_pmd_init(pmd_t
*pmd_page
, unsigned long address
, unsigned long end
,
449 unsigned long page_size_mask
, pgprot_t prot
)
451 unsigned long pages
= 0, next
;
452 unsigned long last_map_addr
= end
;
454 int i
= pmd_index(address
);
456 for (; i
< PTRS_PER_PMD
; i
++, address
= next
) {
457 pmd_t
*pmd
= pmd_page
+ pmd_index(address
);
459 pgprot_t new_prot
= prot
;
461 next
= (address
& PMD_MASK
) + PMD_SIZE
;
462 if (address
>= end
) {
463 if (!after_bootmem
&&
464 !e820_any_mapped(address
& PMD_MASK
, next
, E820_RAM
) &&
465 !e820_any_mapped(address
& PMD_MASK
, next
, E820_RESERVED_KERN
))
466 set_pmd(pmd
, __pmd(0));
471 if (!pmd_large(*pmd
)) {
472 spin_lock(&init_mm
.page_table_lock
);
473 pte
= (pte_t
*)pmd_page_vaddr(*pmd
);
474 last_map_addr
= phys_pte_init(pte
, address
,
476 spin_unlock(&init_mm
.page_table_lock
);
480 * If we are ok with PG_LEVEL_2M mapping, then we will
481 * use the existing mapping,
483 * Otherwise, we will split the large page mapping but
484 * use the same existing protection bits except for
485 * large page, so that we don't violate Intel's TLB
486 * Application note (317080) which says, while changing
487 * the page sizes, new and old translations should
488 * not differ with respect to page frame and
491 if (page_size_mask
& (1 << PG_LEVEL_2M
)) {
494 last_map_addr
= next
;
497 new_prot
= pte_pgprot(pte_clrhuge(*(pte_t
*)pmd
));
500 if (page_size_mask
& (1<<PG_LEVEL_2M
)) {
502 spin_lock(&init_mm
.page_table_lock
);
503 set_pte((pte_t
*)pmd
,
504 pfn_pte((address
& PMD_MASK
) >> PAGE_SHIFT
,
505 __pgprot(pgprot_val(prot
) | _PAGE_PSE
)));
506 spin_unlock(&init_mm
.page_table_lock
);
507 last_map_addr
= next
;
511 pte
= alloc_low_page();
512 last_map_addr
= phys_pte_init(pte
, address
, end
, new_prot
);
514 spin_lock(&init_mm
.page_table_lock
);
515 pmd_populate_kernel(&init_mm
, pmd
, pte
);
516 spin_unlock(&init_mm
.page_table_lock
);
518 update_page_count(PG_LEVEL_2M
, pages
);
519 return last_map_addr
;
522 static unsigned long __meminit
523 phys_pud_init(pud_t
*pud_page
, unsigned long addr
, unsigned long end
,
524 unsigned long page_size_mask
)
526 unsigned long pages
= 0, next
;
527 unsigned long last_map_addr
= end
;
528 int i
= pud_index(addr
);
530 for (; i
< PTRS_PER_PUD
; i
++, addr
= next
) {
531 pud_t
*pud
= pud_page
+ pud_index(addr
);
533 pgprot_t prot
= PAGE_KERNEL
;
535 next
= (addr
& PUD_MASK
) + PUD_SIZE
;
537 if (!after_bootmem
&&
538 !e820_any_mapped(addr
& PUD_MASK
, next
, E820_RAM
) &&
539 !e820_any_mapped(addr
& PUD_MASK
, next
, E820_RESERVED_KERN
))
540 set_pud(pud
, __pud(0));
545 if (!pud_large(*pud
)) {
546 pmd
= pmd_offset(pud
, 0);
547 last_map_addr
= phys_pmd_init(pmd
, addr
, end
,
548 page_size_mask
, prot
);
553 * If we are ok with PG_LEVEL_1G mapping, then we will
554 * use the existing mapping.
556 * Otherwise, we will split the gbpage mapping but use
557 * the same existing protection bits except for large
558 * page, so that we don't violate Intel's TLB
559 * Application note (317080) which says, while changing
560 * the page sizes, new and old translations should
561 * not differ with respect to page frame and
564 if (page_size_mask
& (1 << PG_LEVEL_1G
)) {
567 last_map_addr
= next
;
570 prot
= pte_pgprot(pte_clrhuge(*(pte_t
*)pud
));
573 if (page_size_mask
& (1<<PG_LEVEL_1G
)) {
575 spin_lock(&init_mm
.page_table_lock
);
576 set_pte((pte_t
*)pud
,
577 pfn_pte((addr
& PUD_MASK
) >> PAGE_SHIFT
,
579 spin_unlock(&init_mm
.page_table_lock
);
580 last_map_addr
= next
;
584 pmd
= alloc_low_page();
585 last_map_addr
= phys_pmd_init(pmd
, addr
, end
, page_size_mask
,
588 spin_lock(&init_mm
.page_table_lock
);
589 pud_populate(&init_mm
, pud
, pmd
);
590 spin_unlock(&init_mm
.page_table_lock
);
594 update_page_count(PG_LEVEL_1G
, pages
);
596 return last_map_addr
;
599 unsigned long __meminit
600 kernel_physical_mapping_init(unsigned long start
,
602 unsigned long page_size_mask
)
604 bool pgd_changed
= false;
605 unsigned long next
, last_map_addr
= end
;
608 start
= (unsigned long)__va(start
);
609 end
= (unsigned long)__va(end
);
612 for (; start
< end
; start
= next
) {
613 pgd_t
*pgd
= pgd_offset_k(start
);
616 next
= (start
& PGDIR_MASK
) + PGDIR_SIZE
;
619 pud
= (pud_t
*)pgd_page_vaddr(*pgd
);
620 last_map_addr
= phys_pud_init(pud
, __pa(start
),
621 __pa(end
), page_size_mask
);
625 pud
= alloc_low_page();
626 last_map_addr
= phys_pud_init(pud
, __pa(start
), __pa(end
),
629 spin_lock(&init_mm
.page_table_lock
);
630 pgd_populate(&init_mm
, pgd
, pud
);
631 spin_unlock(&init_mm
.page_table_lock
);
636 sync_global_pgds(addr
, end
- 1);
640 return last_map_addr
;
644 void __init
initmem_init(void)
646 memblock_set_node(0, (phys_addr_t
)ULLONG_MAX
, 0);
650 void __init
paging_init(void)
652 sparse_memory_present_with_active_regions(MAX_NUMNODES
);
656 * clear the default setting with node 0
657 * note: don't use nodes_clear here, that is really clearing when
658 * numa support is not compiled in, and later node_set_state
659 * will not set it back.
661 node_clear_state(0, N_MEMORY
);
662 if (N_MEMORY
!= N_NORMAL_MEMORY
)
663 node_clear_state(0, N_NORMAL_MEMORY
);
669 * Memory hotplug specific functions
671 #ifdef CONFIG_MEMORY_HOTPLUG
673 * After memory hotplug the variables max_pfn, max_low_pfn and high_memory need
676 static void update_end_of_memory_vars(u64 start
, u64 size
)
678 unsigned long end_pfn
= PFN_UP(start
+ size
);
680 if (end_pfn
> max_pfn
) {
682 max_low_pfn
= end_pfn
;
683 high_memory
= (void *)__va(max_pfn
* PAGE_SIZE
- 1) + 1;
688 * Memory is added always to NORMAL zone. This means you will never get
689 * additional DMA/DMA32 memory.
691 int arch_add_memory(int nid
, u64 start
, u64 size
)
693 struct pglist_data
*pgdat
= NODE_DATA(nid
);
694 struct zone
*zone
= pgdat
->node_zones
+ ZONE_NORMAL
;
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 bool bootmem
= false;
718 unsigned int nr_pages
= 1 << order
;
720 /* bootmem page has reserved flag */
721 if (PageReserved(page
)) {
722 __ClearPageReserved(page
);
725 magic
= (unsigned long)page
->lru
.next
;
726 if (magic
== SECTION_INFO
|| magic
== MIX_SECTION_INFO
) {
728 put_page_bootmem(page
++);
730 __free_pages_bootmem(page
, order
);
732 free_pages((unsigned long)page_address(page
), order
);
735 * SECTION_INFO pages and MIX_SECTION_INFO pages
736 * are all allocated by bootmem.
739 zone
= page_zone(page
);
740 zone_span_writelock(zone
);
741 zone
->present_pages
+= nr_pages
;
742 zone_span_writeunlock(zone
);
743 totalram_pages
+= nr_pages
;
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
)
994 bool pgd_changed
= false;
996 for (; start
< end
; start
= next
) {
997 next
= pgd_addr_end(start
, end
);
999 pgd
= pgd_offset_k(start
);
1000 if (!pgd_present(*pgd
))
1003 pud
= (pud_t
*)pgd_page_vaddr(*pgd
);
1004 remove_pud_table(pud
, start
, next
, direct
);
1005 if (free_pud_table(pud
, pgd
))
1010 sync_global_pgds(start
, end
- 1);
1015 void __ref
vmemmap_free(unsigned long start
, unsigned long end
)
1017 remove_pagetable(start
, end
, false);
1020 #ifdef CONFIG_MEMORY_HOTREMOVE
1021 static void __meminit
1022 kernel_physical_mapping_remove(unsigned long start
, unsigned long end
)
1024 start
= (unsigned long)__va(start
);
1025 end
= (unsigned long)__va(end
);
1027 remove_pagetable(start
, end
, true);
1030 int __ref
arch_remove_memory(u64 start
, u64 size
)
1032 unsigned long start_pfn
= start
>> PAGE_SHIFT
;
1033 unsigned long nr_pages
= size
>> PAGE_SHIFT
;
1037 zone
= page_zone(pfn_to_page(start_pfn
));
1038 kernel_physical_mapping_remove(start
, start
+ size
);
1039 ret
= __remove_pages(zone
, start_pfn
, nr_pages
);
1045 #endif /* CONFIG_MEMORY_HOTPLUG */
1047 static struct kcore_list kcore_vsyscall
;
1049 static void __init
register_page_bootmem_info(void)
1054 for_each_online_node(i
)
1055 register_page_bootmem_info_node(NODE_DATA(i
));
1059 void __init
mem_init(void)
1061 long codesize
, reservedpages
, datasize
, initsize
;
1062 unsigned long absent_pages
;
1066 /* clear_bss() already clear the empty_zero_page */
1068 register_page_bootmem_info();
1070 /* this will put all memory onto the freelists */
1071 totalram_pages
= free_all_bootmem();
1073 absent_pages
= absent_pages_in_range(0, max_pfn
);
1074 reservedpages
= max_pfn
- totalram_pages
- absent_pages
;
1077 codesize
= (unsigned long) &_etext
- (unsigned long) &_text
;
1078 datasize
= (unsigned long) &_edata
- (unsigned long) &_etext
;
1079 initsize
= (unsigned long) &__init_end
- (unsigned long) &__init_begin
;
1081 /* Register memory areas for /proc/kcore */
1082 kclist_add(&kcore_vsyscall
, (void *)VSYSCALL_START
,
1083 VSYSCALL_END
- VSYSCALL_START
, KCORE_OTHER
);
1085 printk(KERN_INFO
"Memory: %luk/%luk available (%ldk kernel code, "
1086 "%ldk absent, %ldk reserved, %ldk data, %ldk init)\n",
1087 nr_free_pages() << (PAGE_SHIFT
-10),
1088 max_pfn
<< (PAGE_SHIFT
-10),
1090 absent_pages
<< (PAGE_SHIFT
-10),
1091 reservedpages
<< (PAGE_SHIFT
-10),
1096 #ifdef CONFIG_DEBUG_RODATA
1097 const int rodata_test_data
= 0xC3;
1098 EXPORT_SYMBOL_GPL(rodata_test_data
);
1100 int kernel_set_to_readonly
;
1102 void set_kernel_text_rw(void)
1104 unsigned long start
= PFN_ALIGN(_text
);
1105 unsigned long end
= PFN_ALIGN(__stop___ex_table
);
1107 if (!kernel_set_to_readonly
)
1110 pr_debug("Set kernel text: %lx - %lx for read write\n",
1114 * Make the kernel identity mapping for text RW. Kernel text
1115 * mapping will always be RO. Refer to the comment in
1116 * static_protections() in pageattr.c
1118 set_memory_rw(start
, (end
- start
) >> PAGE_SHIFT
);
1121 void set_kernel_text_ro(void)
1123 unsigned long start
= PFN_ALIGN(_text
);
1124 unsigned long end
= PFN_ALIGN(__stop___ex_table
);
1126 if (!kernel_set_to_readonly
)
1129 pr_debug("Set kernel text: %lx - %lx for read only\n",
1133 * Set the kernel identity mapping for text RO.
1135 set_memory_ro(start
, (end
- start
) >> PAGE_SHIFT
);
1138 void mark_rodata_ro(void)
1140 unsigned long start
= PFN_ALIGN(_text
);
1141 unsigned long rodata_start
= PFN_ALIGN(__start_rodata
);
1142 unsigned long end
= (unsigned long) &__end_rodata_hpage_align
;
1143 unsigned long text_end
= PFN_ALIGN(&__stop___ex_table
);
1144 unsigned long rodata_end
= PFN_ALIGN(&__end_rodata
);
1145 unsigned long all_end
= PFN_ALIGN(&_end
);
1147 printk(KERN_INFO
"Write protecting the kernel read-only data: %luk\n",
1148 (end
- start
) >> 10);
1149 set_memory_ro(start
, (end
- start
) >> PAGE_SHIFT
);
1151 kernel_set_to_readonly
= 1;
1154 * The rodata/data/bss/brk section (but not the kernel text!)
1155 * should also be not-executable.
1157 set_memory_nx(rodata_start
, (all_end
- rodata_start
) >> PAGE_SHIFT
);
1161 #ifdef CONFIG_CPA_DEBUG
1162 printk(KERN_INFO
"Testing CPA: undo %lx-%lx\n", start
, end
);
1163 set_memory_rw(start
, (end
-start
) >> PAGE_SHIFT
);
1165 printk(KERN_INFO
"Testing CPA: again\n");
1166 set_memory_ro(start
, (end
-start
) >> PAGE_SHIFT
);
1169 free_init_pages("unused kernel memory",
1170 (unsigned long) __va(__pa_symbol(text_end
)),
1171 (unsigned long) __va(__pa_symbol(rodata_start
)));
1173 free_init_pages("unused kernel memory",
1174 (unsigned long) __va(__pa_symbol(rodata_end
)),
1175 (unsigned long) __va(__pa_symbol(_sdata
)));
1180 int kern_addr_valid(unsigned long addr
)
1182 unsigned long above
= ((long)addr
) >> __VIRTUAL_MASK_SHIFT
;
1188 if (above
!= 0 && above
!= -1UL)
1191 pgd
= pgd_offset_k(addr
);
1195 pud
= pud_offset(pgd
, addr
);
1199 if (pud_large(*pud
))
1200 return pfn_valid(pud_pfn(*pud
));
1202 pmd
= pmd_offset(pud
, addr
);
1206 if (pmd_large(*pmd
))
1207 return pfn_valid(pmd_pfn(*pmd
));
1209 pte
= pte_offset_kernel(pmd
, addr
);
1213 return pfn_valid(pte_pfn(*pte
));
1217 * A pseudo VMA to allow ptrace access for the vsyscall page. This only
1218 * covers the 64bit vsyscall page now. 32bit has a real VMA now and does
1219 * not need special handling anymore:
1221 static struct vm_area_struct gate_vma
= {
1222 .vm_start
= VSYSCALL_START
,
1223 .vm_end
= VSYSCALL_START
+ (VSYSCALL_MAPPED_PAGES
* PAGE_SIZE
),
1224 .vm_page_prot
= PAGE_READONLY_EXEC
,
1225 .vm_flags
= VM_READ
| VM_EXEC
1228 struct vm_area_struct
*get_gate_vma(struct mm_struct
*mm
)
1230 #ifdef CONFIG_IA32_EMULATION
1231 if (!mm
|| mm
->context
.ia32_compat
)
1237 int in_gate_area(struct mm_struct
*mm
, unsigned long addr
)
1239 struct vm_area_struct
*vma
= get_gate_vma(mm
);
1244 return (addr
>= vma
->vm_start
) && (addr
< vma
->vm_end
);
1248 * Use this when you have no reliable mm, typically from interrupt
1249 * context. It is less reliable than using a task's mm and may give
1252 int in_gate_area_no_mm(unsigned long addr
)
1254 return (addr
>= VSYSCALL_START
) && (addr
< VSYSCALL_END
);
1257 const char *arch_vma_name(struct vm_area_struct
*vma
)
1259 if (vma
->vm_mm
&& vma
->vm_start
== (long)vma
->vm_mm
->context
.vdso
)
1261 if (vma
== &gate_vma
)
1262 return "[vsyscall]";
1266 #ifdef CONFIG_X86_UV
1267 unsigned long memory_block_size_bytes(void)
1269 if (is_uv_system()) {
1270 printk(KERN_INFO
"UV: memory block size 2GB\n");
1271 return 2UL * 1024 * 1024 * 1024;
1273 return MIN_MEMORY_BLOCK_SIZE
;
1277 #ifdef CONFIG_SPARSEMEM_VMEMMAP
1279 * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
1281 static long __meminitdata addr_start
, addr_end
;
1282 static void __meminitdata
*p_start
, *p_end
;
1283 static int __meminitdata node_start
;
1285 static int __meminit
vmemmap_populate_hugepages(unsigned long start
,
1286 unsigned long end
, int node
)
1294 for (addr
= start
; addr
< end
; addr
= next
) {
1295 next
= pmd_addr_end(addr
, end
);
1297 pgd
= vmemmap_pgd_populate(addr
, node
);
1301 pud
= vmemmap_pud_populate(pgd
, addr
, node
);
1305 pmd
= pmd_offset(pud
, addr
);
1306 if (pmd_none(*pmd
)) {
1309 p
= vmemmap_alloc_block_buf(PMD_SIZE
, node
);
1313 entry
= pfn_pte(__pa(p
) >> PAGE_SHIFT
,
1315 set_pmd(pmd
, __pmd(pte_val(entry
)));
1317 /* check to see if we have contiguous blocks */
1318 if (p_end
!= p
|| node_start
!= node
) {
1320 printk(KERN_DEBUG
" [%lx-%lx] PMD -> [%p-%p] on node %d\n",
1321 addr_start
, addr_end
-1, p_start
, p_end
-1, node_start
);
1327 addr_end
= addr
+ PMD_SIZE
;
1328 p_end
= p
+ PMD_SIZE
;
1331 } else if (pmd_large(*pmd
)) {
1332 vmemmap_verify((pte_t
*)pmd
, node
, addr
, next
);
1335 pr_warn_once("vmemmap: falling back to regular page backing\n");
1336 if (vmemmap_populate_basepages(addr
, next
, node
))
1342 int __meminit
vmemmap_populate(unsigned long start
, unsigned long end
, int node
)
1347 err
= vmemmap_populate_hugepages(start
, end
, node
);
1349 err
= vmemmap_populate_basepages(start
, end
, node
);
1351 sync_global_pgds(start
, end
- 1);
1355 #if defined(CONFIG_MEMORY_HOTPLUG_SPARSE) && defined(CONFIG_HAVE_BOOTMEM_INFO_NODE)
1356 void register_page_bootmem_memmap(unsigned long section_nr
,
1357 struct page
*start_page
, unsigned long size
)
1359 unsigned long addr
= (unsigned long)start_page
;
1360 unsigned long end
= (unsigned long)(start_page
+ size
);
1365 unsigned int nr_pages
;
1368 for (; addr
< end
; addr
= next
) {
1371 pgd
= pgd_offset_k(addr
);
1372 if (pgd_none(*pgd
)) {
1373 next
= (addr
+ PAGE_SIZE
) & PAGE_MASK
;
1376 get_page_bootmem(section_nr
, pgd_page(*pgd
), MIX_SECTION_INFO
);
1378 pud
= pud_offset(pgd
, addr
);
1379 if (pud_none(*pud
)) {
1380 next
= (addr
+ PAGE_SIZE
) & PAGE_MASK
;
1383 get_page_bootmem(section_nr
, pud_page(*pud
), MIX_SECTION_INFO
);
1386 next
= (addr
+ PAGE_SIZE
) & PAGE_MASK
;
1387 pmd
= pmd_offset(pud
, addr
);
1390 get_page_bootmem(section_nr
, pmd_page(*pmd
),
1393 pte
= pte_offset_kernel(pmd
, addr
);
1396 get_page_bootmem(section_nr
, pte_page(*pte
),
1399 next
= pmd_addr_end(addr
, end
);
1401 pmd
= pmd_offset(pud
, addr
);
1405 nr_pages
= 1 << (get_order(PMD_SIZE
));
1406 page
= pmd_page(*pmd
);
1408 get_page_bootmem(section_nr
, page
++,
1415 void __meminit
vmemmap_populate_print_last(void)
1418 printk(KERN_DEBUG
" [%lx-%lx] PMD -> [%p-%p] on node %d\n",
1419 addr_start
, addr_end
-1, p_start
, p_end
-1, node_start
);