blob: 9f6347c468b005f9fa0239815e3726d16ad89baf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/x86_64/mm/init.c
3 *
4 * Copyright (C) 1995 Linus Torvalds
Pavel Macheka2531292010-07-18 14:27:13 +02005 * Copyright (C) 2000 Pavel Machek <pavel@ucw.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copyright (C) 2002,2003 Andi Kleen <ak@suse.de>
7 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/signal.h>
10#include <linux/sched.h>
11#include <linux/kernel.h>
12#include <linux/errno.h>
13#include <linux/string.h>
14#include <linux/types.h>
15#include <linux/ptrace.h>
16#include <linux/mman.h>
17#include <linux/mm.h>
18#include <linux/swap.h>
19#include <linux/smp.h>
20#include <linux/init.h>
Thomas Gleixner11034d52008-05-12 15:43:36 +020021#include <linux/initrd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/pagemap.h>
23#include <linux/bootmem.h>
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070024#include <linux/memblock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/proc_fs.h>
Andi Kleen59170892005-11-05 17:25:53 +010026#include <linux/pci.h>
Jan Beulich6fb14752007-05-02 19:27:10 +020027#include <linux/pfn.h>
Randy Dunlapc9cf5522006-06-27 02:53:52 -070028#include <linux/poison.h>
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010029#include <linux/dma-mapping.h>
Matt Tolentino44df75e2006-01-17 07:03:41 +010030#include <linux/module.h>
Benjamin Herrenschmidta63fdc52011-06-14 10:57:50 +100031#include <linux/memory.h>
Matt Tolentino44df75e2006-01-17 07:03:41 +010032#include <linux/memory_hotplug.h>
Konrad Rzeszutekae32b122007-05-02 19:27:11 +020033#include <linux/nmi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include <asm/processor.h>
Ingo Molnar46eaa672008-10-12 15:06:29 +020037#include <asm/bios_ebda.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/uaccess.h>
39#include <asm/pgtable.h>
40#include <asm/pgalloc.h>
41#include <asm/dma.h>
42#include <asm/fixmap.h>
43#include <asm/e820.h>
44#include <asm/apic.h>
45#include <asm/tlb.h>
46#include <asm/mmu_context.h>
47#include <asm/proto.h>
48#include <asm/smp.h>
Andi Kleen2bc04142005-11-05 17:25:53 +010049#include <asm/sections.h>
Thomas Gleixner718fc132008-01-30 13:30:17 +010050#include <asm/kdebug.h>
Thomas Gleixneraaa64e02008-01-30 13:30:17 +010051#include <asm/numa.h>
Harvey Harrison7bfeab92008-02-12 12:12:01 -080052#include <asm/cacheflush.h>
Pekka Enberg4fcb2082009-03-05 14:55:08 +020053#include <asm/init.h>
Nathan Fontenot1dc41aa2011-01-20 10:46:15 -060054#include <asm/uv/uv.h>
Yinghai Lue5f15b42011-02-18 11:30:30 +000055#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Yinghai Lu5c51bdb2012-11-16 19:39:01 -080057#include "mm_internal.h"
58
Yinghai Luaece2782013-01-24 12:19:48 -080059static void ident_pmd_init(unsigned long pmd_flag, pmd_t *pmd_page,
60 unsigned long addr, unsigned long end)
61{
62 addr &= PMD_MASK;
63 for (; addr < end; addr += PMD_SIZE) {
64 pmd_t *pmd = pmd_page + pmd_index(addr);
65
66 if (!pmd_present(*pmd))
67 set_pmd(pmd, __pmd(addr | pmd_flag));
68 }
69}
70static int ident_pud_init(struct x86_mapping_info *info, pud_t *pud_page,
71 unsigned long addr, unsigned long end)
72{
73 unsigned long next;
74
75 for (; addr < end; addr = next) {
76 pud_t *pud = pud_page + pud_index(addr);
77 pmd_t *pmd;
78
79 next = (addr & PUD_MASK) + PUD_SIZE;
80 if (next > end)
81 next = end;
82
83 if (pud_present(*pud)) {
84 pmd = pmd_offset(pud, 0);
85 ident_pmd_init(info->pmd_flag, pmd, addr, next);
86 continue;
87 }
88 pmd = (pmd_t *)info->alloc_pgt_page(info->context);
89 if (!pmd)
90 return -ENOMEM;
91 ident_pmd_init(info->pmd_flag, pmd, addr, next);
92 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE));
93 }
94
95 return 0;
96}
97
98int kernel_ident_mapping_init(struct x86_mapping_info *info, pgd_t *pgd_page,
99 unsigned long addr, unsigned long end)
100{
101 unsigned long next;
102 int result;
103 int off = info->kernel_mapping ? pgd_index(__PAGE_OFFSET) : 0;
104
105 for (; addr < end; addr = next) {
106 pgd_t *pgd = pgd_page + pgd_index(addr) + off;
107 pud_t *pud;
108
109 next = (addr & PGDIR_MASK) + PGDIR_SIZE;
110 if (next > end)
111 next = end;
112
113 if (pgd_present(*pgd)) {
114 pud = pud_offset(pgd, 0);
115 result = ident_pud_init(info, pud, addr, next);
116 if (result)
117 return result;
118 continue;
119 }
120
121 pud = (pud_t *)info->alloc_pgt_page(info->context);
122 if (!pud)
123 return -ENOMEM;
124 result = ident_pud_init(info, pud, addr, next);
125 if (result)
126 return result;
127 set_pgd(pgd, __pgd(__pa(pud) | _KERNPG_TABLE));
128 }
129
130 return 0;
131}
132
Ingo Molnar00d1c5e2008-04-17 17:40:45 +0200133static int __init parse_direct_gbpages_off(char *arg)
134{
135 direct_gbpages = 0;
136 return 0;
137}
138early_param("nogbpages", parse_direct_gbpages_off);
139
140static int __init parse_direct_gbpages_on(char *arg)
141{
142 direct_gbpages = 1;
143 return 0;
144}
145early_param("gbpages", parse_direct_gbpages_on);
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147/*
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.
151 */
152
Jeremy Fitzhardingebe43d722008-09-07 15:21:13 -0700153pteval_t __supported_pte_mask __read_mostly = ~_PAGE_IOMAP;
Yinghai Lubd220a22008-09-05 00:58:28 -0700154EXPORT_SYMBOL_GPL(__supported_pte_mask);
155
Yinghai Lubd220a22008-09-05 00:58:28 -0700156int force_personality32;
157
Ingo Molnardeed05b2008-09-05 10:23:26 +0200158/*
159 * noexec32=on|off
160 * Control non executable heap for 32bit processes.
161 * To control the stack too use noexec=off
162 *
163 * on PROT_READ does not imply PROT_EXEC for 32-bit processes (default)
164 * off PROT_READ implies PROT_EXEC
165 */
Yinghai Lubd220a22008-09-05 00:58:28 -0700166static int __init nonx32_setup(char *str)
167{
168 if (!strcmp(str, "on"))
169 force_personality32 &= ~READ_IMPLIES_EXEC;
170 else if (!strcmp(str, "off"))
171 force_personality32 |= READ_IMPLIES_EXEC;
172 return 1;
173}
174__setup("noexec32=", nonx32_setup);
175
Marcin Slusarz8d6ea9672008-08-15 18:32:24 +0200176/*
Haicheng Li6afb5152010-05-19 17:42:14 +0800177 * When memory was added/removed make sure all the processes MM have
178 * suitable PGD entries in the local PGD level page.
179 */
180void sync_global_pgds(unsigned long start, unsigned long end)
181{
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700182 unsigned long address;
Haicheng Li6afb5152010-05-19 17:42:14 +0800183
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700184 for (address = start; address <= end; address += PGDIR_SIZE) {
185 const pgd_t *pgd_ref = pgd_offset_k(address);
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700186 struct page *page;
Haicheng Li6afb5152010-05-19 17:42:14 +0800187
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700188 if (pgd_none(*pgd_ref))
189 continue;
Haicheng Li6afb5152010-05-19 17:42:14 +0800190
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800191 spin_lock(&pgd_lock);
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700192 list_for_each_entry(page, &pgd_list, lru) {
Linus Torvaldsbe354f42012-12-15 12:29:54 -0800193 pgd_t *pgd;
Jeremy Fitzhardinge617d34d2010-09-21 12:01:51 -0700194 spinlock_t *pgt_lock;
195
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700196 pgd = (pgd_t *)page_address(page) + pgd_index(address);
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800197 /* the pgt_lock only for Xen */
Jeremy Fitzhardinge617d34d2010-09-21 12:01:51 -0700198 pgt_lock = &pgd_page_get_mm(page)->page_table_lock;
199 spin_lock(pgt_lock);
200
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700201 if (pgd_none(*pgd))
202 set_pgd(pgd, *pgd_ref);
203 else
204 BUG_ON(pgd_page_vaddr(*pgd)
205 != pgd_page_vaddr(*pgd_ref));
Jeremy Fitzhardinge617d34d2010-09-21 12:01:51 -0700206
207 spin_unlock(pgt_lock);
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700208 }
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800209 spin_unlock(&pgd_lock);
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700210 }
Haicheng Li6afb5152010-05-19 17:42:14 +0800211}
212
213/*
Marcin Slusarz8d6ea9672008-08-15 18:32:24 +0200214 * NOTE: This function is marked __ref because it calls __init function
215 * (alloc_bootmem_pages). It's safe to do it ONLY when after_bootmem == 0.
216 */
217static __ref void *spp_getpage(void)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100218{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 void *ptr;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if (after_bootmem)
Vegard Nossum9e730232009-02-22 11:28:25 +0100222 ptr = (void *) get_zeroed_page(GFP_ATOMIC | __GFP_NOTRACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 else
224 ptr = alloc_bootmem_pages(PAGE_SIZE);
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100225
226 if (!ptr || ((unsigned long)ptr & ~PAGE_MASK)) {
227 panic("set_pte_phys: cannot allocate page data %s\n",
228 after_bootmem ? "after bootmem" : "");
229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Ingo Molnar10f22dd2008-01-30 13:34:10 +0100231 pr_debug("spp_getpage %p\n", ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100233 return ptr;
234}
235
Jeremy Fitzhardingef254f392009-03-03 12:02:57 -0800236static pud_t *fill_pud(pgd_t *pgd, unsigned long vaddr)
Tejun Heo458a3e62009-02-24 11:57:21 +0900237{
238 if (pgd_none(*pgd)) {
239 pud_t *pud = (pud_t *)spp_getpage();
240 pgd_populate(&init_mm, pgd, pud);
241 if (pud != pud_offset(pgd, 0))
242 printk(KERN_ERR "PAGETABLE BUG #00! %p <-> %p\n",
243 pud, pud_offset(pgd, 0));
244 }
245 return pud_offset(pgd, vaddr);
246}
247
Jeremy Fitzhardingef254f392009-03-03 12:02:57 -0800248static pmd_t *fill_pmd(pud_t *pud, unsigned long vaddr)
Tejun Heo458a3e62009-02-24 11:57:21 +0900249{
250 if (pud_none(*pud)) {
251 pmd_t *pmd = (pmd_t *) spp_getpage();
252 pud_populate(&init_mm, pud, pmd);
253 if (pmd != pmd_offset(pud, 0))
254 printk(KERN_ERR "PAGETABLE BUG #01! %p <-> %p\n",
255 pmd, pmd_offset(pud, 0));
256 }
257 return pmd_offset(pud, vaddr);
258}
259
Jeremy Fitzhardingef254f392009-03-03 12:02:57 -0800260static pte_t *fill_pte(pmd_t *pmd, unsigned long vaddr)
Tejun Heo458a3e62009-02-24 11:57:21 +0900261{
262 if (pmd_none(*pmd)) {
263 pte_t *pte = (pte_t *) spp_getpage();
264 pmd_populate_kernel(&init_mm, pmd, pte);
265 if (pte != pte_offset_kernel(pmd, 0))
266 printk(KERN_ERR "PAGETABLE BUG #02!\n");
267 }
268 return pte_offset_kernel(pmd, vaddr);
269}
270
271void set_pte_vaddr_pud(pud_t *pud_page, unsigned long vaddr, pte_t new_pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 pud_t *pud;
274 pmd_t *pmd;
Jeremy Fitzhardinged494a962008-06-17 11:41:59 -0700275 pte_t *pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Eduardo Habkost0814e0b2008-06-25 00:19:22 -0400277 pud = pud_page + pud_index(vaddr);
Tejun Heo458a3e62009-02-24 11:57:21 +0900278 pmd = fill_pmd(pud, vaddr);
279 pte = fill_pte(pmd, vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 set_pte(pte, new_pte);
282
283 /*
284 * It's enough to flush this one mapping.
285 * (PGE mappings get flushed as well)
286 */
287 __flush_tlb_one(vaddr);
288}
289
Tejun Heo458a3e62009-02-24 11:57:21 +0900290void set_pte_vaddr(unsigned long vaddr, pte_t pteval)
Eduardo Habkost0814e0b2008-06-25 00:19:22 -0400291{
292 pgd_t *pgd;
293 pud_t *pud_page;
294
295 pr_debug("set_pte_vaddr %lx to %lx\n", vaddr, native_pte_val(pteval));
296
297 pgd = pgd_offset_k(vaddr);
298 if (pgd_none(*pgd)) {
299 printk(KERN_ERR
300 "PGD FIXMAP MISSING, it should be setup in head.S!\n");
301 return;
302 }
303 pud_page = (pud_t*)pgd_page_vaddr(*pgd);
304 set_pte_vaddr_pud(pud_page, vaddr, pteval);
305}
306
Tejun Heo458a3e62009-02-24 11:57:21 +0900307pmd_t * __init populate_extra_pmd(unsigned long vaddr)
Tejun Heo11124412009-02-20 16:29:09 +0900308{
309 pgd_t *pgd;
310 pud_t *pud;
311
312 pgd = pgd_offset_k(vaddr);
Tejun Heo458a3e62009-02-24 11:57:21 +0900313 pud = fill_pud(pgd, vaddr);
314 return fill_pmd(pud, vaddr);
315}
Tejun Heo11124412009-02-20 16:29:09 +0900316
Tejun Heo458a3e62009-02-24 11:57:21 +0900317pte_t * __init populate_extra_pte(unsigned long vaddr)
318{
319 pmd_t *pmd;
320
321 pmd = populate_extra_pmd(vaddr);
322 return fill_pte(pmd, vaddr);
Tejun Heo11124412009-02-20 16:29:09 +0900323}
324
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100325/*
Jack Steiner3a9e1892008-07-01 14:45:32 -0500326 * Create large page table mappings for a range of physical addresses.
327 */
328static void __init __init_extra_mapping(unsigned long phys, unsigned long size,
329 pgprot_t prot)
330{
331 pgd_t *pgd;
332 pud_t *pud;
333 pmd_t *pmd;
334
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 |
341 _PAGE_USER));
342 }
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 |
347 _PAGE_USER));
348 }
349 pmd = pmd_offset(pud, phys);
350 BUG_ON(!pmd_none(*pmd));
351 set_pmd(pmd, __pmd(phys | pgprot_val(prot)));
352 }
353}
354
355void __init init_extra_mapping_wb(unsigned long phys, unsigned long size)
356{
357 __init_extra_mapping(phys, size, PAGE_KERNEL_LARGE);
358}
359
360void __init init_extra_mapping_uc(unsigned long phys, unsigned long size)
361{
362 __init_extra_mapping(phys, size, PAGE_KERNEL_LARGE_NOCACHE);
363}
364
365/*
Ingo Molnar88f3aec2008-02-21 11:04:11 +0100366 * The head.S code sets up the kernel high mapping:
367 *
368 * from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text)
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100369 *
370 * phys_addr 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.
373 *
Yinghai Lue5f15b42011-02-18 11:30:30 +0000374 * 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
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100376 * well, as they are located before _text:
377 */
378void __init cleanup_highmap(void)
379{
380 unsigned long vaddr = __START_KERNEL_map;
Yinghai Lu10054232013-01-24 12:19:54 -0800381 unsigned long vaddr_end = __START_KERNEL_map + KERNEL_IMAGE_SIZE;
Yinghai Lue5f15b42011-02-18 11:30:30 +0000382 unsigned long end = roundup((unsigned long)_brk_end, PMD_SIZE) - 1;
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100383 pmd_t *pmd = level2_kernel_pgt;
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100384
Yinghai Lu10054232013-01-24 12:19:54 -0800385 /*
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().
389 */
390 if (max_pfn_mapped)
391 vaddr_end = __START_KERNEL_map + (max_pfn_mapped << PAGE_SHIFT);
392
Yinghai Lue5f15b42011-02-18 11:30:30 +0000393 for (; vaddr + PMD_SIZE - 1 < vaddr_end; pmd++, vaddr += PMD_SIZE) {
Hugh Dickins2884f112008-05-28 19:36:07 +0100394 if (pmd_none(*pmd))
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100395 continue;
396 if (vaddr < (unsigned long) _text || vaddr > end)
397 set_pmd(pmd, __pmd(0));
398 }
399}
400
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700401static unsigned long __meminit
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700402phys_pte_init(pte_t *pte_page, unsigned long addr, unsigned long end,
403 pgprot_t prot)
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400404{
Yinghai Lueceb3632012-11-16 19:38:56 -0800405 unsigned long pages = 0, next;
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700406 unsigned long last_map_addr = end;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400407 int i;
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700408
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400409 pte_t *pte = pte_page + pte_index(addr);
410
Yinghai Lueceb3632012-11-16 19:38:56 -0800411 for (i = pte_index(addr); i < PTRS_PER_PTE; i++, addr = next, pte++) {
412 next = (addr & PAGE_MASK) + PAGE_SIZE;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400413 if (addr >= end) {
Yinghai Lueceb3632012-11-16 19:38:56 -0800414 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));
418 continue;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400419 }
420
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700421 /*
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.
426 */
Yinghai Lu3afa3942008-10-25 22:58:21 -0700427 if (pte_val(*pte)) {
Jan Beulich876ee612012-10-04 14:48:10 +0100428 if (!after_bootmem)
429 pages++;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400430 continue;
Yinghai Lu3afa3942008-10-25 22:58:21 -0700431 }
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400432
433 if (0)
434 printk(" pte=%p addr=%lx pte=%016lx\n",
435 pte, addr, pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL).pte);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400436 pages++;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700437 set_pte(pte, pfn_pte(addr >> PAGE_SHIFT, prot));
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400438 last_map_addr = (addr & PAGE_MASK) + PAGE_SIZE;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400439 }
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700440
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400441 update_page_count(PG_LEVEL_4K, pages);
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700442
443 return last_map_addr;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400444}
445
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700446static unsigned long __meminit
Yinghai Lub50efd22008-07-08 01:41:05 -0700447phys_pmd_init(pmd_t *pmd_page, unsigned long address, unsigned long end,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700448 unsigned long page_size_mask, pgprot_t prot)
Matt Tolentino44df75e2006-01-17 07:03:41 +0100449{
Jan Beulich20167d32012-05-16 14:06:26 +0100450 unsigned long pages = 0, next;
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700451 unsigned long last_map_addr = end;
Andi Kleence0c0e52008-05-02 11:46:49 +0200452
Keith Mannthey6ad91652006-09-26 10:52:36 +0200453 int i = pmd_index(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Jan Beulich20167d32012-05-16 14:06:26 +0100455 for (; i < PTRS_PER_PMD; i++, address = next) {
Keith Mannthey6ad91652006-09-26 10:52:36 +0200456 pmd_t *pmd = pmd_page + pmd_index(address);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400457 pte_t *pte;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700458 pgprot_t new_prot = prot;
Matt Tolentino44df75e2006-01-17 07:03:41 +0100459
Jan Beulich20167d32012-05-16 14:06:26 +0100460 next = (address & PMD_MASK) + PMD_SIZE;
Yinghai Lueceb3632012-11-16 19:38:56 -0800461 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));
466 continue;
467 }
Jan Beulich20167d32012-05-16 14:06:26 +0100468
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400469 if (pmd_val(*pmd)) {
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100470 if (!pmd_large(*pmd)) {
471 spin_lock(&init_mm.page_table_lock);
Yinghai Lu973dc4f2012-11-16 19:38:59 -0800472 pte = (pte_t *)pmd_page_vaddr(*pmd);
Yinghai Lu4b239f42010-12-17 16:58:28 -0800473 last_map_addr = phys_pte_init(pte, address,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700474 end, prot);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100475 spin_unlock(&init_mm.page_table_lock);
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700476 continue;
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100477 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700478 /*
479 * If we are ok with PG_LEVEL_2M mapping, then we will
480 * use the existing mapping,
481 *
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
488 * attributes.
489 */
Yinghai Lu3afa3942008-10-25 22:58:21 -0700490 if (page_size_mask & (1 << PG_LEVEL_2M)) {
Jan Beulich876ee612012-10-04 14:48:10 +0100491 if (!after_bootmem)
492 pages++;
Jan Beulich20167d32012-05-16 14:06:26 +0100493 last_map_addr = next;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700494 continue;
Yinghai Lu3afa3942008-10-25 22:58:21 -0700495 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700496 new_prot = pte_pgprot(pte_clrhuge(*(pte_t *)pmd));
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400497 }
Keith Mannthey6ad91652006-09-26 10:52:36 +0200498
Yinghai Lub50efd22008-07-08 01:41:05 -0700499 if (page_size_mask & (1<<PG_LEVEL_2M)) {
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400500 pages++;
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100501 spin_lock(&init_mm.page_table_lock);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400502 set_pte((pte_t *)pmd,
Yinghai Lu960ddb4f2012-11-16 19:38:54 -0800503 pfn_pte((address & PMD_MASK) >> PAGE_SHIFT,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700504 __pgprot(pgprot_val(prot) | _PAGE_PSE)));
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100505 spin_unlock(&init_mm.page_table_lock);
Jan Beulich20167d32012-05-16 14:06:26 +0100506 last_map_addr = next;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400507 continue;
508 }
509
Yinghai Lu868bf4d2012-11-16 19:39:00 -0800510 pte = alloc_low_page();
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700511 last_map_addr = phys_pte_init(pte, address, end, new_prot);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400512
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100513 spin_lock(&init_mm.page_table_lock);
Yinghai Lu868bf4d2012-11-16 19:39:00 -0800514 pmd_populate_kernel(&init_mm, pmd, pte);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100515 spin_unlock(&init_mm.page_table_lock);
Matt Tolentino44df75e2006-01-17 07:03:41 +0100516 }
Andi Kleence0c0e52008-05-02 11:46:49 +0200517 update_page_count(PG_LEVEL_2M, pages);
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700518 return last_map_addr;
Matt Tolentino44df75e2006-01-17 07:03:41 +0100519}
520
Andi Kleencc615032008-03-12 03:53:28 +0100521static unsigned long __meminit
Yinghai Lub50efd22008-07-08 01:41:05 -0700522phys_pud_init(pud_t *pud_page, unsigned long addr, unsigned long end,
523 unsigned long page_size_mask)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100524{
Jan Beulich20167d32012-05-16 14:06:26 +0100525 unsigned long pages = 0, next;
Andi Kleencc615032008-03-12 03:53:28 +0100526 unsigned long last_map_addr = end;
Keith Mannthey6ad91652006-09-26 10:52:36 +0200527 int i = pud_index(addr);
Matt Tolentino44df75e2006-01-17 07:03:41 +0100528
Jan Beulich20167d32012-05-16 14:06:26 +0100529 for (; i < PTRS_PER_PUD; i++, addr = next) {
Keith Mannthey6ad91652006-09-26 10:52:36 +0200530 pud_t *pud = pud_page + pud_index(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 pmd_t *pmd;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700532 pgprot_t prot = PAGE_KERNEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Jan Beulich20167d32012-05-16 14:06:26 +0100534 next = (addr & PUD_MASK) + PUD_SIZE;
Yinghai Lueceb3632012-11-16 19:38:56 -0800535 if (addr >= end) {
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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 continue;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Keith Mannthey6ad91652006-09-26 10:52:36 +0200543 if (pud_val(*pud)) {
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700544 if (!pud_large(*pud)) {
Yinghai Lu973dc4f2012-11-16 19:38:59 -0800545 pmd = pmd_offset(pud, 0);
Yinghai Lu4b239f42010-12-17 16:58:28 -0800546 last_map_addr = phys_pmd_init(pmd, addr, end,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700547 page_size_mask, prot);
Yinghai Lu4b239f42010-12-17 16:58:28 -0800548 __flush_tlb_all();
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700549 continue;
550 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700551 /*
552 * If we are ok with PG_LEVEL_1G mapping, then we will
553 * use the existing mapping.
554 *
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
561 * attributes.
562 */
Yinghai Lu3afa3942008-10-25 22:58:21 -0700563 if (page_size_mask & (1 << PG_LEVEL_1G)) {
Jan Beulich876ee612012-10-04 14:48:10 +0100564 if (!after_bootmem)
565 pages++;
Jan Beulich20167d32012-05-16 14:06:26 +0100566 last_map_addr = next;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700567 continue;
Yinghai Lu3afa3942008-10-25 22:58:21 -0700568 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700569 prot = pte_pgprot(pte_clrhuge(*(pte_t *)pud));
Andi Kleenef925762008-04-17 17:40:45 +0200570 }
571
Yinghai Lub50efd22008-07-08 01:41:05 -0700572 if (page_size_mask & (1<<PG_LEVEL_1G)) {
Andi Kleence0c0e52008-05-02 11:46:49 +0200573 pages++;
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100574 spin_lock(&init_mm.page_table_lock);
Andi Kleenef925762008-04-17 17:40:45 +0200575 set_pte((pte_t *)pud,
Yinghai Lu960ddb4f2012-11-16 19:38:54 -0800576 pfn_pte((addr & PUD_MASK) >> PAGE_SHIFT,
577 PAGE_KERNEL_LARGE));
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100578 spin_unlock(&init_mm.page_table_lock);
Jan Beulich20167d32012-05-16 14:06:26 +0100579 last_map_addr = next;
Keith Mannthey6ad91652006-09-26 10:52:36 +0200580 continue;
581 }
582
Yinghai Lu868bf4d2012-11-16 19:39:00 -0800583 pmd = alloc_low_page();
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700584 last_map_addr = phys_pmd_init(pmd, addr, end, page_size_mask,
585 prot);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100586
587 spin_lock(&init_mm.page_table_lock);
Yinghai Lu868bf4d2012-11-16 19:39:00 -0800588 pud_populate(&init_mm, pud, pmd);
Matt Tolentino44df75e2006-01-17 07:03:41 +0100589 spin_unlock(&init_mm.page_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
Andi Kleen1a2b4412008-01-30 13:33:54 +0100591 __flush_tlb_all();
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700592
Andi Kleence0c0e52008-05-02 11:46:49 +0200593 update_page_count(PG_LEVEL_1G, pages);
Andi Kleencc615032008-03-12 03:53:28 +0100594
Yinghai Lu1a0db382008-06-24 14:56:20 -0700595 return last_map_addr;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100596}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Shaohua Li41d840e2009-06-12 12:57:52 +0800598unsigned long __meminit
Pekka Enbergf7650902009-03-05 14:55:05 +0200599kernel_physical_mapping_init(unsigned long start,
600 unsigned long end,
601 unsigned long page_size_mask)
Yinghai Lub50efd22008-07-08 01:41:05 -0700602{
Haicheng Li9b861522010-08-20 17:50:16 +0800603 bool pgd_changed = false;
Yinghai Lub50efd22008-07-08 01:41:05 -0700604 unsigned long next, last_map_addr = end;
Haicheng Li9b861522010-08-20 17:50:16 +0800605 unsigned long addr;
Yinghai Lub50efd22008-07-08 01:41:05 -0700606
607 start = (unsigned long)__va(start);
608 end = (unsigned long)__va(end);
Wu Fengguang1c5f50e2010-09-03 17:04:07 +0800609 addr = start;
Yinghai Lub50efd22008-07-08 01:41:05 -0700610
611 for (; start < end; start = next) {
612 pgd_t *pgd = pgd_offset_k(start);
Yinghai Lub50efd22008-07-08 01:41:05 -0700613 pud_t *pud;
614
Yinghai Luc2bdee52013-01-24 12:19:46 -0800615 next = (start & PGDIR_MASK) + PGDIR_SIZE;
Yinghai Lub50efd22008-07-08 01:41:05 -0700616
617 if (pgd_val(*pgd)) {
Yinghai Lu973dc4f2012-11-16 19:38:59 -0800618 pud = (pud_t *)pgd_page_vaddr(*pgd);
Yinghai Lu4b239f42010-12-17 16:58:28 -0800619 last_map_addr = phys_pud_init(pud, __pa(start),
Yinghai Lub50efd22008-07-08 01:41:05 -0700620 __pa(end), page_size_mask);
621 continue;
622 }
623
Yinghai Lu868bf4d2012-11-16 19:39:00 -0800624 pud = alloc_low_page();
Yinghai Luc2bdee52013-01-24 12:19:46 -0800625 last_map_addr = phys_pud_init(pud, __pa(start), __pa(end),
Yinghai Lub50efd22008-07-08 01:41:05 -0700626 page_size_mask);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100627
628 spin_lock(&init_mm.page_table_lock);
Yinghai Lu868bf4d2012-11-16 19:39:00 -0800629 pgd_populate(&init_mm, pgd, pud);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100630 spin_unlock(&init_mm.page_table_lock);
Haicheng Li9b861522010-08-20 17:50:16 +0800631 pgd_changed = true;
Yinghai Lub50efd22008-07-08 01:41:05 -0700632 }
Haicheng Li9b861522010-08-20 17:50:16 +0800633
634 if (pgd_changed)
Wen Congyangf73568a2012-12-18 12:22:21 -0800635 sync_global_pgds(addr, end - 1);
Haicheng Li9b861522010-08-20 17:50:16 +0800636
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700637 __flush_tlb_all();
Yinghai Lub50efd22008-07-08 01:41:05 -0700638
639 return last_map_addr;
640}
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700641
Matt Tolentino2b976902005-06-23 00:08:06 -0700642#ifndef CONFIG_NUMA
Tejun Heod8fc3af2011-02-16 12:13:06 +0100643void __init initmem_init(void)
Yinghai Lu1f75d7e2008-06-22 02:44:49 -0700644{
Tejun Heo0608f702011-07-14 11:44:23 +0200645 memblock_set_node(0, (phys_addr_t)ULLONG_MAX, 0);
Yinghai Lu1f75d7e2008-06-22 02:44:49 -0700646}
Pekka Enberg3551f882009-05-07 15:35:41 +0300647#endif
Yinghai Lu1f75d7e2008-06-22 02:44:49 -0700648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649void __init paging_init(void)
650{
Pekka Enberg3551f882009-05-07 15:35:41 +0300651 sparse_memory_present_with_active_regions(MAX_NUMNODES);
Matt Tolentino44df75e2006-01-17 07:03:41 +0100652 sparse_init();
Yinghai Lu44b57282009-07-08 09:50:19 -0700653
654 /*
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.
659 */
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -0800660 node_clear_state(0, N_MEMORY);
661 if (N_MEMORY != N_NORMAL_MEMORY)
662 node_clear_state(0, N_NORMAL_MEMORY);
Yinghai Lu44b57282009-07-08 09:50:19 -0700663
Pekka Enberg4c0b2e52011-11-01 15:58:17 +0200664 zone_sizes_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100667/*
Matt Tolentino44df75e2006-01-17 07:03:41 +0100668 * Memory hotplug specific functions
Matt Tolentino44df75e2006-01-17 07:03:41 +0100669 */
Yasunori Gotobc02af92006-06-27 02:53:30 -0700670#ifdef CONFIG_MEMORY_HOTPLUG
671/*
Shaohui Zhengea085412010-02-02 13:44:16 -0800672 * After memory hotplug the variables max_pfn, max_low_pfn and high_memory need
673 * updating.
674 */
675static void update_end_of_memory_vars(u64 start, u64 size)
676{
677 unsigned long end_pfn = PFN_UP(start + size);
678
679 if (end_pfn > max_pfn) {
680 max_pfn = end_pfn;
681 max_low_pfn = end_pfn;
682 high_memory = (void *)__va(max_pfn * PAGE_SIZE - 1) + 1;
683 }
684}
685
686/*
Yasunori Gotobc02af92006-06-27 02:53:30 -0700687 * Memory is added always to NORMAL zone. This means you will never get
688 * additional DMA/DMA32 memory.
689 */
690int arch_add_memory(int nid, u64 start, u64 size)
691{
692 struct pglist_data *pgdat = NODE_DATA(nid);
Christoph Lameter776ed982006-09-25 23:31:09 -0700693 struct zone *zone = pgdat->node_zones + ZONE_NORMAL;
Jacob Shin66520eb2012-11-16 19:38:52 -0800694 unsigned long start_pfn = start >> PAGE_SHIFT;
Yasunori Gotobc02af92006-06-27 02:53:30 -0700695 unsigned long nr_pages = size >> PAGE_SHIFT;
696 int ret;
697
Jacob Shin66520eb2012-11-16 19:38:52 -0800698 init_memory_mapping(start, start + size);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700699
Gary Hadec04fc582009-01-06 14:39:14 -0800700 ret = __add_pages(nid, zone, start_pfn, nr_pages);
Gary Hadefe8b8682008-10-28 16:43:14 -0700701 WARN_ON_ONCE(ret);
Yasunori Gotobc02af92006-06-27 02:53:30 -0700702
Shaohui Zhengea085412010-02-02 13:44:16 -0800703 /* update max_pfn, max_low_pfn and high_memory */
704 update_end_of_memory_vars(start, size);
705
Yasunori Gotobc02af92006-06-27 02:53:30 -0700706 return ret;
Yasunori Gotobc02af92006-06-27 02:53:30 -0700707}
708EXPORT_SYMBOL_GPL(arch_add_memory);
709
Wen Congyangae9aae92013-02-22 16:33:04 -0800710#define PAGE_INUSE 0xFD
711
712static void __meminit free_pagetable(struct page *page, int order)
713{
714 struct zone *zone;
715 bool bootmem = false;
716 unsigned long magic;
717 unsigned int nr_pages = 1 << order;
718
719 /* bootmem page has reserved flag */
720 if (PageReserved(page)) {
721 __ClearPageReserved(page);
722 bootmem = true;
723
724 magic = (unsigned long)page->lru.next;
725 if (magic == SECTION_INFO || magic == MIX_SECTION_INFO) {
726 while (nr_pages--)
727 put_page_bootmem(page++);
728 } else
729 __free_pages_bootmem(page, order);
730 } else
731 free_pages((unsigned long)page_address(page), order);
732
733 /*
734 * SECTION_INFO pages and MIX_SECTION_INFO pages
735 * are all allocated by bootmem.
736 */
737 if (bootmem) {
738 zone = page_zone(page);
739 zone_span_writelock(zone);
740 zone->present_pages += nr_pages;
741 zone_span_writeunlock(zone);
742 totalram_pages += nr_pages;
743 }
744}
745
746static void __meminit free_pte_table(pte_t *pte_start, pmd_t *pmd)
747{
748 pte_t *pte;
749 int i;
750
751 for (i = 0; i < PTRS_PER_PTE; i++) {
752 pte = pte_start + i;
753 if (pte_val(*pte))
754 return;
755 }
756
757 /* free a pte talbe */
758 free_pagetable(pmd_page(*pmd), 0);
759 spin_lock(&init_mm.page_table_lock);
760 pmd_clear(pmd);
761 spin_unlock(&init_mm.page_table_lock);
762}
763
764static void __meminit free_pmd_table(pmd_t *pmd_start, pud_t *pud)
765{
766 pmd_t *pmd;
767 int i;
768
769 for (i = 0; i < PTRS_PER_PMD; i++) {
770 pmd = pmd_start + i;
771 if (pmd_val(*pmd))
772 return;
773 }
774
775 /* free a pmd talbe */
776 free_pagetable(pud_page(*pud), 0);
777 spin_lock(&init_mm.page_table_lock);
778 pud_clear(pud);
779 spin_unlock(&init_mm.page_table_lock);
780}
781
782/* Return true if pgd is changed, otherwise return false. */
783static bool __meminit free_pud_table(pud_t *pud_start, pgd_t *pgd)
784{
785 pud_t *pud;
786 int i;
787
788 for (i = 0; i < PTRS_PER_PUD; i++) {
789 pud = pud_start + i;
790 if (pud_val(*pud))
791 return false;
792 }
793
794 /* free a pud table */
795 free_pagetable(pgd_page(*pgd), 0);
796 spin_lock(&init_mm.page_table_lock);
797 pgd_clear(pgd);
798 spin_unlock(&init_mm.page_table_lock);
799
800 return true;
801}
802
803static void __meminit
804remove_pte_table(pte_t *pte_start, unsigned long addr, unsigned long end,
805 bool direct)
806{
807 unsigned long next, pages = 0;
808 pte_t *pte;
809 void *page_addr;
810 phys_addr_t phys_addr;
811
812 pte = pte_start + pte_index(addr);
813 for (; addr < end; addr = next, pte++) {
814 next = (addr + PAGE_SIZE) & PAGE_MASK;
815 if (next > end)
816 next = end;
817
818 if (!pte_present(*pte))
819 continue;
820
821 /*
822 * We mapped [0,1G) memory as identity mapping when
823 * initializing, in arch/x86/kernel/head_64.S. These
824 * pagetables cannot be removed.
825 */
826 phys_addr = pte_val(*pte) + (addr & PAGE_MASK);
827 if (phys_addr < (phys_addr_t)0x40000000)
828 return;
829
830 if (IS_ALIGNED(addr, PAGE_SIZE) &&
831 IS_ALIGNED(next, PAGE_SIZE)) {
832 /*
833 * Do not free direct mapping pages since they were
834 * freed when offlining, or simplely not in use.
835 */
836 if (!direct)
837 free_pagetable(pte_page(*pte), 0);
838
839 spin_lock(&init_mm.page_table_lock);
840 pte_clear(&init_mm, addr, pte);
841 spin_unlock(&init_mm.page_table_lock);
842
843 /* For non-direct mapping, pages means nothing. */
844 pages++;
845 } else {
846 /*
847 * If we are here, we are freeing vmemmap pages since
848 * direct mapped memory ranges to be freed are aligned.
849 *
850 * If we are not removing the whole page, it means
851 * other page structs in this page are being used and
852 * we canot remove them. So fill the unused page_structs
853 * with 0xFD, and remove the page when it is wholly
854 * filled with 0xFD.
855 */
856 memset((void *)addr, PAGE_INUSE, next - addr);
857
858 page_addr = page_address(pte_page(*pte));
859 if (!memchr_inv(page_addr, PAGE_INUSE, PAGE_SIZE)) {
860 free_pagetable(pte_page(*pte), 0);
861
862 spin_lock(&init_mm.page_table_lock);
863 pte_clear(&init_mm, addr, pte);
864 spin_unlock(&init_mm.page_table_lock);
865 }
866 }
867 }
868
869 /* Call free_pte_table() in remove_pmd_table(). */
870 flush_tlb_all();
871 if (direct)
872 update_page_count(PG_LEVEL_4K, -pages);
873}
874
875static void __meminit
876remove_pmd_table(pmd_t *pmd_start, unsigned long addr, unsigned long end,
877 bool direct)
878{
879 unsigned long next, pages = 0;
880 pte_t *pte_base;
881 pmd_t *pmd;
882 void *page_addr;
883
884 pmd = pmd_start + pmd_index(addr);
885 for (; addr < end; addr = next, pmd++) {
886 next = pmd_addr_end(addr, end);
887
888 if (!pmd_present(*pmd))
889 continue;
890
891 if (pmd_large(*pmd)) {
892 if (IS_ALIGNED(addr, PMD_SIZE) &&
893 IS_ALIGNED(next, PMD_SIZE)) {
894 if (!direct)
895 free_pagetable(pmd_page(*pmd),
896 get_order(PMD_SIZE));
897
898 spin_lock(&init_mm.page_table_lock);
899 pmd_clear(pmd);
900 spin_unlock(&init_mm.page_table_lock);
901 pages++;
902 } else {
903 /* If here, we are freeing vmemmap pages. */
904 memset((void *)addr, PAGE_INUSE, next - addr);
905
906 page_addr = page_address(pmd_page(*pmd));
907 if (!memchr_inv(page_addr, PAGE_INUSE,
908 PMD_SIZE)) {
909 free_pagetable(pmd_page(*pmd),
910 get_order(PMD_SIZE));
911
912 spin_lock(&init_mm.page_table_lock);
913 pmd_clear(pmd);
914 spin_unlock(&init_mm.page_table_lock);
915 }
916 }
917
918 continue;
919 }
920
921 pte_base = (pte_t *)pmd_page_vaddr(*pmd);
922 remove_pte_table(pte_base, addr, next, direct);
923 free_pte_table(pte_base, pmd);
924 }
925
926 /* Call free_pmd_table() in remove_pud_table(). */
927 if (direct)
928 update_page_count(PG_LEVEL_2M, -pages);
929}
930
931static void __meminit
932remove_pud_table(pud_t *pud_start, unsigned long addr, unsigned long end,
933 bool direct)
934{
935 unsigned long next, pages = 0;
936 pmd_t *pmd_base;
937 pud_t *pud;
938 void *page_addr;
939
940 pud = pud_start + pud_index(addr);
941 for (; addr < end; addr = next, pud++) {
942 next = pud_addr_end(addr, end);
943
944 if (!pud_present(*pud))
945 continue;
946
947 if (pud_large(*pud)) {
948 if (IS_ALIGNED(addr, PUD_SIZE) &&
949 IS_ALIGNED(next, PUD_SIZE)) {
950 if (!direct)
951 free_pagetable(pud_page(*pud),
952 get_order(PUD_SIZE));
953
954 spin_lock(&init_mm.page_table_lock);
955 pud_clear(pud);
956 spin_unlock(&init_mm.page_table_lock);
957 pages++;
958 } else {
959 /* If here, we are freeing vmemmap pages. */
960 memset((void *)addr, PAGE_INUSE, next - addr);
961
962 page_addr = page_address(pud_page(*pud));
963 if (!memchr_inv(page_addr, PAGE_INUSE,
964 PUD_SIZE)) {
965 free_pagetable(pud_page(*pud),
966 get_order(PUD_SIZE));
967
968 spin_lock(&init_mm.page_table_lock);
969 pud_clear(pud);
970 spin_unlock(&init_mm.page_table_lock);
971 }
972 }
973
974 continue;
975 }
976
977 pmd_base = (pmd_t *)pud_page_vaddr(*pud);
978 remove_pmd_table(pmd_base, addr, next, direct);
979 free_pmd_table(pmd_base, pud);
980 }
981
982 if (direct)
983 update_page_count(PG_LEVEL_1G, -pages);
984}
985
986/* start and end are both virtual address. */
987static void __meminit
988remove_pagetable(unsigned long start, unsigned long end, bool direct)
989{
990 unsigned long next;
991 pgd_t *pgd;
992 pud_t *pud;
993 bool pgd_changed = false;
994
995 for (; start < end; start = next) {
996 next = pgd_addr_end(start, end);
997
998 pgd = pgd_offset_k(start);
999 if (!pgd_present(*pgd))
1000 continue;
1001
1002 pud = (pud_t *)pgd_page_vaddr(*pgd);
1003 remove_pud_table(pud, start, next, direct);
1004 if (free_pud_table(pud, pgd))
1005 pgd_changed = true;
1006 }
1007
1008 if (pgd_changed)
1009 sync_global_pgds(start, end - 1);
1010
1011 flush_tlb_all();
1012}
1013
Johannes Weiner0aad8182013-04-29 15:07:50 -07001014void __ref vmemmap_free(unsigned long start, unsigned long end)
Tang Chen01975182013-02-22 16:33:08 -08001015{
Tang Chen01975182013-02-22 16:33:08 -08001016 remove_pagetable(start, end, false);
1017}
1018
Tang Chenbbcab872013-02-22 16:33:06 -08001019static void __meminit
1020kernel_physical_mapping_remove(unsigned long start, unsigned long end)
1021{
1022 start = (unsigned long)__va(start);
1023 end = (unsigned long)__va(end);
1024
1025 remove_pagetable(start, end, true);
1026}
1027
Wen Congyang24d335c2013-02-22 16:32:58 -08001028#ifdef CONFIG_MEMORY_HOTREMOVE
1029int __ref arch_remove_memory(u64 start, u64 size)
1030{
1031 unsigned long start_pfn = start >> PAGE_SHIFT;
1032 unsigned long nr_pages = size >> PAGE_SHIFT;
1033 struct zone *zone;
1034 int ret;
1035
1036 zone = page_zone(pfn_to_page(start_pfn));
Tang Chenbbcab872013-02-22 16:33:06 -08001037 kernel_physical_mapping_remove(start, start + size);
Wen Congyang24d335c2013-02-22 16:32:58 -08001038 ret = __remove_pages(zone, start_pfn, nr_pages);
1039 WARN_ON_ONCE(ret);
1040
1041 return ret;
1042}
1043#endif
Keith Mannthey45e0b782006-09-30 23:27:09 -07001044#endif /* CONFIG_MEMORY_HOTPLUG */
1045
KAMEZAWA Hiroyuki81ac3ad2009-09-22 16:45:49 -07001046static struct kcore_list kcore_vsyscall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Yinghai Lu94b43c32012-11-16 19:39:19 -08001048static void __init register_page_bootmem_info(void)
1049{
1050#ifdef CONFIG_NUMA
1051 int i;
1052
1053 for_each_online_node(i)
1054 register_page_bootmem_info_node(NODE_DATA(i));
1055#endif
1056}
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058void __init mem_init(void)
1059{
Andi Kleen0a43e4b2005-09-12 18:49:24 +02001060 long codesize, reservedpages, datasize, initsize;
Yinghai Lu11a6b0c2008-10-14 18:59:18 -07001061 unsigned long absent_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Jon Mason0dc243a2006-06-26 13:58:11 +02001063 pci_iommu_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Yinghai Lu48ddb152008-01-30 13:32:36 +01001065 /* clear_bss() already clear the empty_zero_page */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Yinghai Lu94b43c32012-11-16 19:39:19 -08001067 register_page_bootmem_info();
Jiang Liubced0e32013-04-29 15:06:53 -07001068
1069 /* this will put all memory onto the freelists */
Andi Kleen0a43e4b2005-09-12 18:49:24 +02001070 totalram_pages = free_all_bootmem();
Yinghai Lu11a6b0c2008-10-14 18:59:18 -07001071
1072 absent_pages = absent_pages_in_range(0, max_pfn);
1073 reservedpages = max_pfn - totalram_pages - absent_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 after_bootmem = 1;
1075
1076 codesize = (unsigned long) &_etext - (unsigned long) &_text;
1077 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
1078 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
1079
1080 /* Register memory areas for /proc/kcore */
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001081 kclist_add(&kcore_vsyscall, (void *)VSYSCALL_START,
KAMEZAWA Hiroyukic30bb2a2009-09-22 16:45:43 -07001082 VSYSCALL_END - VSYSCALL_START, KCORE_OTHER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Ingo Molnar10f22dd2008-01-30 13:34:10 +01001084 printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, "
Yinghai Lu11a6b0c2008-10-14 18:59:18 -07001085 "%ldk absent, %ldk reserved, %ldk data, %ldk init)\n",
Geert Uytterhoevencc013a82009-09-21 17:02:36 -07001086 nr_free_pages() << (PAGE_SHIFT-10),
Yinghai Luc987d122008-06-24 22:14:09 -07001087 max_pfn << (PAGE_SHIFT-10),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 codesize >> 10,
Yinghai Lu11a6b0c2008-10-14 18:59:18 -07001089 absent_pages << (PAGE_SHIFT-10),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 reservedpages << (PAGE_SHIFT-10),
1091 datasize >> 10,
1092 initsize >> 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
1094
Arjan van de Ven67df1972006-01-06 00:12:04 -08001095#ifdef CONFIG_DEBUG_RODATA
Arjan van de Venedeed302008-01-30 13:34:08 +01001096const int rodata_test_data = 0xC3;
1097EXPORT_SYMBOL_GPL(rodata_test_data);
Arjan van de Ven67df1972006-01-06 00:12:04 -08001098
Suresh Siddha502f6602009-10-28 18:46:56 -08001099int kernel_set_to_readonly;
Steven Rostedt16239632009-02-17 17:57:30 -05001100
1101void set_kernel_text_rw(void)
1102{
Suresh Siddhab9af7c02009-10-14 14:46:55 -07001103 unsigned long start = PFN_ALIGN(_text);
Suresh Siddhae7d23dd2009-10-28 18:46:58 -08001104 unsigned long end = PFN_ALIGN(__stop___ex_table);
Steven Rostedt16239632009-02-17 17:57:30 -05001105
1106 if (!kernel_set_to_readonly)
1107 return;
1108
1109 pr_debug("Set kernel text: %lx - %lx for read write\n",
1110 start, end);
1111
Suresh Siddhae7d23dd2009-10-28 18:46:58 -08001112 /*
1113 * Make the kernel identity mapping for text RW. Kernel text
1114 * mapping will always be RO. Refer to the comment in
1115 * static_protections() in pageattr.c
1116 */
Steven Rostedt16239632009-02-17 17:57:30 -05001117 set_memory_rw(start, (end - start) >> PAGE_SHIFT);
1118}
1119
1120void set_kernel_text_ro(void)
1121{
Suresh Siddhab9af7c02009-10-14 14:46:55 -07001122 unsigned long start = PFN_ALIGN(_text);
Suresh Siddhae7d23dd2009-10-28 18:46:58 -08001123 unsigned long end = PFN_ALIGN(__stop___ex_table);
Steven Rostedt16239632009-02-17 17:57:30 -05001124
1125 if (!kernel_set_to_readonly)
1126 return;
1127
1128 pr_debug("Set kernel text: %lx - %lx for read only\n",
1129 start, end);
1130
Suresh Siddhae7d23dd2009-10-28 18:46:58 -08001131 /*
1132 * Set the kernel identity mapping for text RO.
1133 */
Steven Rostedt16239632009-02-17 17:57:30 -05001134 set_memory_ro(start, (end - start) >> PAGE_SHIFT);
1135}
1136
Arjan van de Ven67df1972006-01-06 00:12:04 -08001137void mark_rodata_ro(void)
1138{
Suresh Siddha74e08172009-10-14 14:46:56 -07001139 unsigned long start = PFN_ALIGN(_text);
Alexander Duyckfc8d7822012-11-16 13:57:13 -08001140 unsigned long rodata_start = PFN_ALIGN(__start_rodata);
Suresh Siddha74e08172009-10-14 14:46:56 -07001141 unsigned long end = (unsigned long) &__end_rodata_hpage_align;
Alexander Duyckfc8d7822012-11-16 13:57:13 -08001142 unsigned long text_end = PFN_ALIGN(&__stop___ex_table);
1143 unsigned long rodata_end = PFN_ALIGN(&__end_rodata);
Yinghai Lu72212672013-01-24 12:20:13 -08001144 unsigned long all_end = PFN_ALIGN(&_end);
Steven Rostedt8f0f9962008-05-12 21:20:56 +02001145
Jan Beulich6fb14752007-05-02 19:27:10 +02001146 printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
Linus Torvaldse3ebadd2007-05-07 08:44:24 -07001147 (end - start) >> 10);
Arjan van de Ven984bb802008-02-06 22:39:45 +01001148 set_memory_ro(start, (end - start) >> PAGE_SHIFT);
1149
Steven Rostedt16239632009-02-17 17:57:30 -05001150 kernel_set_to_readonly = 1;
1151
Arjan van de Ven984bb802008-02-06 22:39:45 +01001152 /*
Yinghai Lu72212672013-01-24 12:20:13 -08001153 * The rodata/data/bss/brk section (but not the kernel text!)
1154 * should also be not-executable.
Arjan van de Ven984bb802008-02-06 22:39:45 +01001155 */
Yinghai Lu72212672013-01-24 12:20:13 -08001156 set_memory_nx(rodata_start, (all_end - rodata_start) >> PAGE_SHIFT);
Arjan van de Ven67df1972006-01-06 00:12:04 -08001157
Arjan van de Ven1a487252008-01-30 13:34:09 +01001158 rodata_test();
1159
Andi Kleen0c42f392008-01-30 13:33:42 +01001160#ifdef CONFIG_CPA_DEBUG
Ingo Molnar10f22dd2008-01-30 13:34:10 +01001161 printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end);
Arjan van de Ven6d238cc2008-01-30 13:34:06 +01001162 set_memory_rw(start, (end-start) >> PAGE_SHIFT);
Andi Kleen0c42f392008-01-30 13:33:42 +01001163
Ingo Molnar10f22dd2008-01-30 13:34:10 +01001164 printk(KERN_INFO "Testing CPA: again\n");
Arjan van de Ven6d238cc2008-01-30 13:34:06 +01001165 set_memory_ro(start, (end-start) >> PAGE_SHIFT);
Andi Kleen0c42f392008-01-30 13:33:42 +01001166#endif
Suresh Siddha74e08172009-10-14 14:46:56 -07001167
1168 free_init_pages("unused kernel memory",
Alexander Duyckfc8d7822012-11-16 13:57:13 -08001169 (unsigned long) __va(__pa_symbol(text_end)),
1170 (unsigned long) __va(__pa_symbol(rodata_start)));
1171
Suresh Siddha74e08172009-10-14 14:46:56 -07001172 free_init_pages("unused kernel memory",
Alexander Duyckfc8d7822012-11-16 13:57:13 -08001173 (unsigned long) __va(__pa_symbol(rodata_end)),
1174 (unsigned long) __va(__pa_symbol(_sdata)));
Arjan van de Ven67df1972006-01-06 00:12:04 -08001175}
Mathieu Desnoyers4e4eee02008-02-02 15:42:20 -05001176
Arjan van de Ven67df1972006-01-06 00:12:04 -08001177#endif
1178
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001179int kern_addr_valid(unsigned long addr)
1180{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001182 pgd_t *pgd;
1183 pud_t *pud;
1184 pmd_t *pmd;
1185 pte_t *pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187 if (above != 0 && above != -1UL)
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001188 return 0;
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 pgd = pgd_offset_k(addr);
1191 if (pgd_none(*pgd))
1192 return 0;
1193
1194 pud = pud_offset(pgd, addr);
1195 if (pud_none(*pud))
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001196 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Mel Gorman0ee364e2013-02-11 14:52:36 +00001198 if (pud_large(*pud))
1199 return pfn_valid(pud_pfn(*pud));
1200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 pmd = pmd_offset(pud, addr);
1202 if (pmd_none(*pmd))
1203 return 0;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 if (pmd_large(*pmd))
1206 return pfn_valid(pmd_pfn(*pmd));
1207
1208 pte = pte_offset_kernel(pmd, addr);
1209 if (pte_none(*pte))
1210 return 0;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 return pfn_valid(pte_pfn(*pte));
1213}
1214
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001215/*
1216 * A pseudo VMA to allow ptrace access for the vsyscall page. This only
1217 * covers the 64bit vsyscall page now. 32bit has a real VMA now and does
1218 * not need special handling anymore:
1219 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220static struct vm_area_struct gate_vma = {
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001221 .vm_start = VSYSCALL_START,
1222 .vm_end = VSYSCALL_START + (VSYSCALL_MAPPED_PAGES * PAGE_SIZE),
1223 .vm_page_prot = PAGE_READONLY_EXEC,
1224 .vm_flags = VM_READ | VM_EXEC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225};
1226
Stephen Wilson31db58b2011-03-13 15:49:15 -04001227struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
1229#ifdef CONFIG_IA32_EMULATION
Stephen Wilson31db58b2011-03-13 15:49:15 -04001230 if (!mm || mm->context.ia32_compat)
Andi Kleen1e014412005-04-16 15:24:55 -07001231 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232#endif
1233 return &gate_vma;
1234}
1235
Stephen Wilson83b964b2011-03-13 15:49:16 -04001236int in_gate_area(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
Stephen Wilson83b964b2011-03-13 15:49:16 -04001238 struct vm_area_struct *vma = get_gate_vma(mm);
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001239
Andi Kleen1e014412005-04-16 15:24:55 -07001240 if (!vma)
1241 return 0;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return (addr >= vma->vm_start) && (addr < vma->vm_end);
1244}
1245
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001246/*
Stephen Wilsoncae5d392011-03-13 15:49:17 -04001247 * Use this when you have no reliable mm, typically from interrupt
1248 * context. It is less reliable than using a task's mm and may give
1249 * false positives.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 */
Stephen Wilsoncae5d392011-03-13 15:49:17 -04001251int in_gate_area_no_mm(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
Andi Kleen1e014412005-04-16 15:24:55 -07001253 return (addr >= VSYSCALL_START) && (addr < VSYSCALL_END);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254}
Zou Nan hai2e1c49d2007-06-01 00:46:28 -07001255
Andi Kleen2aae9502007-07-21 17:10:01 +02001256const char *arch_vma_name(struct vm_area_struct *vma)
1257{
1258 if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
1259 return "[vdso]";
1260 if (vma == &gate_vma)
1261 return "[vsyscall]";
1262 return NULL;
1263}
Christoph Lameter0889eba2007-10-16 01:24:15 -07001264
Nathan Fontenot1dc41aa2011-01-20 10:46:15 -06001265#ifdef CONFIG_X86_UV
Nathan Fontenot1dc41aa2011-01-20 10:46:15 -06001266unsigned long memory_block_size_bytes(void)
1267{
1268 if (is_uv_system()) {
1269 printk(KERN_INFO "UV: memory block size 2GB\n");
1270 return 2UL * 1024 * 1024 * 1024;
1271 }
1272 return MIN_MEMORY_BLOCK_SIZE;
1273}
1274#endif
1275
Christoph Lameter0889eba2007-10-16 01:24:15 -07001276#ifdef CONFIG_SPARSEMEM_VMEMMAP
1277/*
1278 * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
1279 */
Yinghai Luc2b91e22008-04-12 01:19:24 -07001280static long __meminitdata addr_start, addr_end;
1281static void __meminitdata *p_start, *p_end;
1282static int __meminitdata node_start;
1283
Johannes Weinere8216da2013-04-29 15:07:54 -07001284static int __meminit vmemmap_populate_hugepages(unsigned long start,
1285 unsigned long end, int node)
Christoph Lameter0889eba2007-10-16 01:24:15 -07001286{
Johannes Weiner0aad8182013-04-29 15:07:50 -07001287 unsigned long addr;
Christoph Lameter0889eba2007-10-16 01:24:15 -07001288 unsigned long next;
1289 pgd_t *pgd;
1290 pud_t *pud;
1291 pmd_t *pmd;
1292
Johannes Weiner0aad8182013-04-29 15:07:50 -07001293 for (addr = start; addr < end; addr = next) {
Johannes Weinere8216da2013-04-29 15:07:54 -07001294 next = pmd_addr_end(addr, end);
Christoph Lameter0889eba2007-10-16 01:24:15 -07001295
1296 pgd = vmemmap_pgd_populate(addr, node);
1297 if (!pgd)
1298 return -ENOMEM;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001299
Christoph Lameter0889eba2007-10-16 01:24:15 -07001300 pud = vmemmap_pud_populate(pgd, addr, node);
1301 if (!pud)
1302 return -ENOMEM;
1303
Johannes Weinere8216da2013-04-29 15:07:54 -07001304 pmd = pmd_offset(pud, addr);
1305 if (pmd_none(*pmd)) {
1306 pte_t entry;
1307 void *p;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001308
Johannes Weinere8216da2013-04-29 15:07:54 -07001309 p = vmemmap_alloc_block_buf(PMD_SIZE, node);
Christoph Lameter0889eba2007-10-16 01:24:15 -07001310 if (!p)
1311 return -ENOMEM;
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -04001312
Johannes Weinere8216da2013-04-29 15:07:54 -07001313 entry = pfn_pte(__pa(p) >> PAGE_SHIFT,
1314 PAGE_KERNEL_LARGE);
1315 set_pmd(pmd, __pmd(pte_val(entry)));
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -04001316
Johannes Weinere8216da2013-04-29 15:07:54 -07001317 /* check to see if we have contiguous blocks */
1318 if (p_end != p || node_start != node) {
1319 if (p_start)
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);
1322 addr_start = addr;
1323 node_start = node;
1324 p_start = p;
1325 }
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -04001326
Johannes Weinere8216da2013-04-29 15:07:54 -07001327 addr_end = addr + PMD_SIZE;
1328 p_end = p + PMD_SIZE;
1329 } else
1330 vmemmap_verify((pte_t *)pmd, node, addr, next);
Christoph Lameter0889eba2007-10-16 01:24:15 -07001331 }
Christoph Lameter0889eba2007-10-16 01:24:15 -07001332 return 0;
1333}
Yinghai Luc2b91e22008-04-12 01:19:24 -07001334
Johannes Weinere8216da2013-04-29 15:07:54 -07001335int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node)
1336{
1337 int err;
1338
1339 if (cpu_has_pse)
1340 err = vmemmap_populate_hugepages(start, end, node);
1341 else
1342 err = vmemmap_populate_basepages(start, end, node);
1343 if (!err)
1344 sync_global_pgds(start, end - 1);
1345 return err;
1346}
1347
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -08001348#if defined(CONFIG_MEMORY_HOTPLUG_SPARSE) && defined(CONFIG_HAVE_BOOTMEM_INFO_NODE)
1349void register_page_bootmem_memmap(unsigned long section_nr,
1350 struct page *start_page, unsigned long size)
1351{
1352 unsigned long addr = (unsigned long)start_page;
1353 unsigned long end = (unsigned long)(start_page + size);
1354 unsigned long next;
1355 pgd_t *pgd;
1356 pud_t *pud;
1357 pmd_t *pmd;
1358 unsigned int nr_pages;
1359 struct page *page;
1360
1361 for (; addr < end; addr = next) {
1362 pte_t *pte = NULL;
1363
1364 pgd = pgd_offset_k(addr);
1365 if (pgd_none(*pgd)) {
1366 next = (addr + PAGE_SIZE) & PAGE_MASK;
1367 continue;
1368 }
1369 get_page_bootmem(section_nr, pgd_page(*pgd), MIX_SECTION_INFO);
1370
1371 pud = pud_offset(pgd, addr);
1372 if (pud_none(*pud)) {
1373 next = (addr + PAGE_SIZE) & PAGE_MASK;
1374 continue;
1375 }
1376 get_page_bootmem(section_nr, pud_page(*pud), MIX_SECTION_INFO);
1377
1378 if (!cpu_has_pse) {
1379 next = (addr + PAGE_SIZE) & PAGE_MASK;
1380 pmd = pmd_offset(pud, addr);
1381 if (pmd_none(*pmd))
1382 continue;
1383 get_page_bootmem(section_nr, pmd_page(*pmd),
1384 MIX_SECTION_INFO);
1385
1386 pte = pte_offset_kernel(pmd, addr);
1387 if (pte_none(*pte))
1388 continue;
1389 get_page_bootmem(section_nr, pte_page(*pte),
1390 SECTION_INFO);
1391 } else {
1392 next = pmd_addr_end(addr, end);
1393
1394 pmd = pmd_offset(pud, addr);
1395 if (pmd_none(*pmd))
1396 continue;
1397
1398 nr_pages = 1 << (get_order(PMD_SIZE));
1399 page = pmd_page(*pmd);
1400 while (nr_pages--)
1401 get_page_bootmem(section_nr, page++,
1402 SECTION_INFO);
1403 }
1404 }
1405}
1406#endif
1407
Yinghai Luc2b91e22008-04-12 01:19:24 -07001408void __meminit vmemmap_populate_print_last(void)
1409{
1410 if (p_start) {
1411 printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n",
1412 addr_start, addr_end-1, p_start, p_end-1, node_start);
1413 p_start = NULL;
1414 p_end = NULL;
1415 node_start = 0;
1416 }
1417}
Christoph Lameter0889eba2007-10-16 01:24:15 -07001418#endif