blob: 74f0f358b07b739500c610b4f42e3684c74e52f6 [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>
24#include <linux/proc_fs.h>
Andi Kleen59170892005-11-05 17:25:53 +010025#include <linux/pci.h>
Jan Beulich6fb14752007-05-02 19:27:10 +020026#include <linux/pfn.h>
Randy Dunlapc9cf5522006-06-27 02:53:52 -070027#include <linux/poison.h>
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010028#include <linux/dma-mapping.h>
Matt Tolentino44df75e2006-01-17 07:03:41 +010029#include <linux/module.h>
30#include <linux/memory_hotplug.h>
Konrad Rzeszutekae32b122007-05-02 19:27:11 +020031#include <linux/nmi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#include <asm/processor.h>
Ingo Molnar46eaa672008-10-12 15:06:29 +020035#include <asm/bios_ebda.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/system.h>
37#include <asm/uaccess.h>
38#include <asm/pgtable.h>
39#include <asm/pgalloc.h>
40#include <asm/dma.h>
41#include <asm/fixmap.h>
42#include <asm/e820.h>
43#include <asm/apic.h>
44#include <asm/tlb.h>
45#include <asm/mmu_context.h>
46#include <asm/proto.h>
47#include <asm/smp.h>
Andi Kleen2bc04142005-11-05 17:25:53 +010048#include <asm/sections.h>
Thomas Gleixner718fc132008-01-30 13:30:17 +010049#include <asm/kdebug.h>
Thomas Gleixneraaa64e02008-01-30 13:30:17 +010050#include <asm/numa.h>
Harvey Harrison7bfeab92008-02-12 12:12:01 -080051#include <asm/cacheflush.h>
Pekka Enberg4fcb2082009-03-05 14:55:08 +020052#include <asm/init.h>
Shaohui Zhengea085412010-02-02 13:44:16 -080053#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Andi Kleene18c6872005-11-05 17:25:53 +010055static unsigned long dma_reserve __initdata;
56
Ingo Molnar00d1c5e2008-04-17 17:40:45 +020057static int __init parse_direct_gbpages_off(char *arg)
58{
59 direct_gbpages = 0;
60 return 0;
61}
62early_param("nogbpages", parse_direct_gbpages_off);
63
64static int __init parse_direct_gbpages_on(char *arg)
65{
66 direct_gbpages = 1;
67 return 0;
68}
69early_param("gbpages", parse_direct_gbpages_on);
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/*
72 * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the
73 * physical space so we can cache the place of the first one and move
74 * around without checking the pgd every time.
75 */
76
Jeremy Fitzhardingebe43d722008-09-07 15:21:13 -070077pteval_t __supported_pte_mask __read_mostly = ~_PAGE_IOMAP;
Yinghai Lubd220a22008-09-05 00:58:28 -070078EXPORT_SYMBOL_GPL(__supported_pte_mask);
79
Yinghai Lubd220a22008-09-05 00:58:28 -070080int force_personality32;
81
Ingo Molnardeed05b2008-09-05 10:23:26 +020082/*
83 * noexec32=on|off
84 * Control non executable heap for 32bit processes.
85 * To control the stack too use noexec=off
86 *
87 * on PROT_READ does not imply PROT_EXEC for 32-bit processes (default)
88 * off PROT_READ implies PROT_EXEC
89 */
Yinghai Lubd220a22008-09-05 00:58:28 -070090static int __init nonx32_setup(char *str)
91{
92 if (!strcmp(str, "on"))
93 force_personality32 &= ~READ_IMPLIES_EXEC;
94 else if (!strcmp(str, "off"))
95 force_personality32 |= READ_IMPLIES_EXEC;
96 return 1;
97}
98__setup("noexec32=", nonx32_setup);
99
Marcin Slusarz8d6ea9672008-08-15 18:32:24 +0200100/*
Haicheng Li6afb5152010-05-19 17:42:14 +0800101 * When memory was added/removed make sure all the processes MM have
102 * suitable PGD entries in the local PGD level page.
103 */
104void sync_global_pgds(unsigned long start, unsigned long end)
105{
106 unsigned long address;
107
108 for (address = start; address <= end; address += PGDIR_SIZE) {
109 const pgd_t *pgd_ref = pgd_offset_k(address);
110 unsigned long flags;
111 struct page *page;
112
113 if (pgd_none(*pgd_ref))
114 continue;
115
116 spin_lock_irqsave(&pgd_lock, flags);
117 list_for_each_entry(page, &pgd_list, lru) {
118 pgd_t *pgd;
119 pgd = (pgd_t *)page_address(page) + pgd_index(address);
120 if (pgd_none(*pgd))
121 set_pgd(pgd, *pgd_ref);
122 else
123 BUG_ON(pgd_page_vaddr(*pgd)
124 != pgd_page_vaddr(*pgd_ref));
125 }
126 spin_unlock_irqrestore(&pgd_lock, flags);
127 }
128}
129
130/*
Marcin Slusarz8d6ea9672008-08-15 18:32:24 +0200131 * NOTE: This function is marked __ref because it calls __init function
132 * (alloc_bootmem_pages). It's safe to do it ONLY when after_bootmem == 0.
133 */
134static __ref void *spp_getpage(void)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100135{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 void *ptr;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 if (after_bootmem)
Vegard Nossum9e730232009-02-22 11:28:25 +0100139 ptr = (void *) get_zeroed_page(GFP_ATOMIC | __GFP_NOTRACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 else
141 ptr = alloc_bootmem_pages(PAGE_SIZE);
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100142
143 if (!ptr || ((unsigned long)ptr & ~PAGE_MASK)) {
144 panic("set_pte_phys: cannot allocate page data %s\n",
145 after_bootmem ? "after bootmem" : "");
146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Ingo Molnar10f22dd2008-01-30 13:34:10 +0100148 pr_debug("spp_getpage %p\n", ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100150 return ptr;
151}
152
Jeremy Fitzhardingef254f392009-03-03 12:02:57 -0800153static pud_t *fill_pud(pgd_t *pgd, unsigned long vaddr)
Tejun Heo458a3e62009-02-24 11:57:21 +0900154{
155 if (pgd_none(*pgd)) {
156 pud_t *pud = (pud_t *)spp_getpage();
157 pgd_populate(&init_mm, pgd, pud);
158 if (pud != pud_offset(pgd, 0))
159 printk(KERN_ERR "PAGETABLE BUG #00! %p <-> %p\n",
160 pud, pud_offset(pgd, 0));
161 }
162 return pud_offset(pgd, vaddr);
163}
164
Jeremy Fitzhardingef254f392009-03-03 12:02:57 -0800165static pmd_t *fill_pmd(pud_t *pud, unsigned long vaddr)
Tejun Heo458a3e62009-02-24 11:57:21 +0900166{
167 if (pud_none(*pud)) {
168 pmd_t *pmd = (pmd_t *) spp_getpage();
169 pud_populate(&init_mm, pud, pmd);
170 if (pmd != pmd_offset(pud, 0))
171 printk(KERN_ERR "PAGETABLE BUG #01! %p <-> %p\n",
172 pmd, pmd_offset(pud, 0));
173 }
174 return pmd_offset(pud, vaddr);
175}
176
Jeremy Fitzhardingef254f392009-03-03 12:02:57 -0800177static pte_t *fill_pte(pmd_t *pmd, unsigned long vaddr)
Tejun Heo458a3e62009-02-24 11:57:21 +0900178{
179 if (pmd_none(*pmd)) {
180 pte_t *pte = (pte_t *) spp_getpage();
181 pmd_populate_kernel(&init_mm, pmd, pte);
182 if (pte != pte_offset_kernel(pmd, 0))
183 printk(KERN_ERR "PAGETABLE BUG #02!\n");
184 }
185 return pte_offset_kernel(pmd, vaddr);
186}
187
188void set_pte_vaddr_pud(pud_t *pud_page, unsigned long vaddr, pte_t new_pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 pud_t *pud;
191 pmd_t *pmd;
Jeremy Fitzhardinged494a962008-06-17 11:41:59 -0700192 pte_t *pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Eduardo Habkost0814e0b2008-06-25 00:19:22 -0400194 pud = pud_page + pud_index(vaddr);
Tejun Heo458a3e62009-02-24 11:57:21 +0900195 pmd = fill_pmd(pud, vaddr);
196 pte = fill_pte(pmd, vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 set_pte(pte, new_pte);
199
200 /*
201 * It's enough to flush this one mapping.
202 * (PGE mappings get flushed as well)
203 */
204 __flush_tlb_one(vaddr);
205}
206
Tejun Heo458a3e62009-02-24 11:57:21 +0900207void set_pte_vaddr(unsigned long vaddr, pte_t pteval)
Eduardo Habkost0814e0b2008-06-25 00:19:22 -0400208{
209 pgd_t *pgd;
210 pud_t *pud_page;
211
212 pr_debug("set_pte_vaddr %lx to %lx\n", vaddr, native_pte_val(pteval));
213
214 pgd = pgd_offset_k(vaddr);
215 if (pgd_none(*pgd)) {
216 printk(KERN_ERR
217 "PGD FIXMAP MISSING, it should be setup in head.S!\n");
218 return;
219 }
220 pud_page = (pud_t*)pgd_page_vaddr(*pgd);
221 set_pte_vaddr_pud(pud_page, vaddr, pteval);
222}
223
Tejun Heo458a3e62009-02-24 11:57:21 +0900224pmd_t * __init populate_extra_pmd(unsigned long vaddr)
Tejun Heo11124412009-02-20 16:29:09 +0900225{
226 pgd_t *pgd;
227 pud_t *pud;
228
229 pgd = pgd_offset_k(vaddr);
Tejun Heo458a3e62009-02-24 11:57:21 +0900230 pud = fill_pud(pgd, vaddr);
231 return fill_pmd(pud, vaddr);
232}
Tejun Heo11124412009-02-20 16:29:09 +0900233
Tejun Heo458a3e62009-02-24 11:57:21 +0900234pte_t * __init populate_extra_pte(unsigned long vaddr)
235{
236 pmd_t *pmd;
237
238 pmd = populate_extra_pmd(vaddr);
239 return fill_pte(pmd, vaddr);
Tejun Heo11124412009-02-20 16:29:09 +0900240}
241
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100242/*
Jack Steiner3a9e1892008-07-01 14:45:32 -0500243 * Create large page table mappings for a range of physical addresses.
244 */
245static void __init __init_extra_mapping(unsigned long phys, unsigned long size,
246 pgprot_t prot)
247{
248 pgd_t *pgd;
249 pud_t *pud;
250 pmd_t *pmd;
251
252 BUG_ON((phys & ~PMD_MASK) || (size & ~PMD_MASK));
253 for (; size; phys += PMD_SIZE, size -= PMD_SIZE) {
254 pgd = pgd_offset_k((unsigned long)__va(phys));
255 if (pgd_none(*pgd)) {
256 pud = (pud_t *) spp_getpage();
257 set_pgd(pgd, __pgd(__pa(pud) | _KERNPG_TABLE |
258 _PAGE_USER));
259 }
260 pud = pud_offset(pgd, (unsigned long)__va(phys));
261 if (pud_none(*pud)) {
262 pmd = (pmd_t *) spp_getpage();
263 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE |
264 _PAGE_USER));
265 }
266 pmd = pmd_offset(pud, phys);
267 BUG_ON(!pmd_none(*pmd));
268 set_pmd(pmd, __pmd(phys | pgprot_val(prot)));
269 }
270}
271
272void __init init_extra_mapping_wb(unsigned long phys, unsigned long size)
273{
274 __init_extra_mapping(phys, size, PAGE_KERNEL_LARGE);
275}
276
277void __init init_extra_mapping_uc(unsigned long phys, unsigned long size)
278{
279 __init_extra_mapping(phys, size, PAGE_KERNEL_LARGE_NOCACHE);
280}
281
282/*
Ingo Molnar88f3aec2008-02-21 11:04:11 +0100283 * The head.S code sets up the kernel high mapping:
284 *
285 * from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text)
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100286 *
287 * phys_addr holds the negative offset to the kernel, which is added
288 * to the compile time generated pmds. This results in invalid pmds up
289 * to the point where we hit the physaddr 0 mapping.
290 *
291 * We limit the mappings to the region from _text to _end. _end is
292 * rounded up to the 2MB boundary. This catches the invalid pmds as
293 * well, as they are located before _text:
294 */
295void __init cleanup_highmap(void)
296{
297 unsigned long vaddr = __START_KERNEL_map;
Joerg Roedeld86bb0d2008-07-25 16:48:57 +0200298 unsigned long end = roundup((unsigned long)_end, PMD_SIZE) - 1;
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100299 pmd_t *pmd = level2_kernel_pgt;
300 pmd_t *last_pmd = pmd + PTRS_PER_PMD;
301
302 for (; pmd < last_pmd; pmd++, vaddr += PMD_SIZE) {
Hugh Dickins2884f112008-05-28 19:36:07 +0100303 if (pmd_none(*pmd))
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100304 continue;
305 if (vaddr < (unsigned long) _text || vaddr > end)
306 set_pmd(pmd, __pmd(0));
307 }
308}
309
Jan Beulich9482ac62008-08-21 14:28:42 +0100310static __ref void *alloc_low_page(unsigned long *phys)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100311{
Pekka Enberg298af9d2009-03-05 14:55:06 +0200312 unsigned long pfn = e820_table_end++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 void *adr;
314
Matt Tolentino44df75e2006-01-17 07:03:41 +0100315 if (after_bootmem) {
Vegard Nossum9e730232009-02-22 11:28:25 +0100316 adr = (void *)get_zeroed_page(GFP_ATOMIC | __GFP_NOTRACK);
Matt Tolentino44df75e2006-01-17 07:03:41 +0100317 *phys = __pa(adr);
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100318
Matt Tolentino44df75e2006-01-17 07:03:41 +0100319 return adr;
320 }
321
Pekka Enberg298af9d2009-03-05 14:55:06 +0200322 if (pfn >= e820_table_top)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100323 panic("alloc_low_page: ran out of memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Jeremy Fitzhardinge14941772008-09-07 15:21:15 -0700325 adr = early_memremap(pfn * PAGE_SIZE, PAGE_SIZE);
Vivek Goyaldafe41e2007-05-02 19:27:06 +0200326 memset(adr, 0, PAGE_SIZE);
327 *phys = pfn * PAGE_SIZE;
328 return adr;
329}
330
Jan Beulich9482ac62008-08-21 14:28:42 +0100331static __ref void unmap_low_page(void *adr)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100332{
Matt Tolentino44df75e2006-01-17 07:03:41 +0100333 if (after_bootmem)
334 return;
335
Vivek Goyaldafe41e2007-05-02 19:27:06 +0200336 early_iounmap(adr, PAGE_SIZE);
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100337}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700339static unsigned long __meminit
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700340phys_pte_init(pte_t *pte_page, unsigned long addr, unsigned long end,
341 pgprot_t prot)
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400342{
343 unsigned pages = 0;
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700344 unsigned long last_map_addr = end;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400345 int i;
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700346
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400347 pte_t *pte = pte_page + pte_index(addr);
348
349 for(i = pte_index(addr); i < PTRS_PER_PTE; i++, addr += PAGE_SIZE, pte++) {
350
351 if (addr >= end) {
352 if (!after_bootmem) {
353 for(; i < PTRS_PER_PTE; i++, pte++)
354 set_pte(pte, __pte(0));
355 }
356 break;
357 }
358
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700359 /*
360 * We will re-use the existing mapping.
361 * Xen for example has some special requirements, like mapping
362 * pagetable pages as RO. So assume someone who pre-setup
363 * these mappings are more intelligent.
364 */
Yinghai Lu3afa3942008-10-25 22:58:21 -0700365 if (pte_val(*pte)) {
366 pages++;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400367 continue;
Yinghai Lu3afa3942008-10-25 22:58:21 -0700368 }
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400369
370 if (0)
371 printk(" pte=%p addr=%lx pte=%016lx\n",
372 pte, addr, pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL).pte);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400373 pages++;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700374 set_pte(pte, pfn_pte(addr >> PAGE_SHIFT, prot));
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400375 last_map_addr = (addr & PAGE_MASK) + PAGE_SIZE;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400376 }
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700377
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400378 update_page_count(PG_LEVEL_4K, pages);
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700379
380 return last_map_addr;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400381}
382
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700383static unsigned long __meminit
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700384phys_pte_update(pmd_t *pmd, unsigned long address, unsigned long end,
385 pgprot_t prot)
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400386{
387 pte_t *pte = (pte_t *)pmd_page_vaddr(*pmd);
388
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700389 return phys_pte_init(pte, address, end, prot);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400390}
391
Andi Kleencc615032008-03-12 03:53:28 +0100392static unsigned long __meminit
Yinghai Lub50efd22008-07-08 01:41:05 -0700393phys_pmd_init(pmd_t *pmd_page, unsigned long address, unsigned long end,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700394 unsigned long page_size_mask, pgprot_t prot)
Matt Tolentino44df75e2006-01-17 07:03:41 +0100395{
Andi Kleence0c0e52008-05-02 11:46:49 +0200396 unsigned long pages = 0;
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700397 unsigned long last_map_addr = end;
Andi Kleence0c0e52008-05-02 11:46:49 +0200398
Keith Mannthey6ad91652006-09-26 10:52:36 +0200399 int i = pmd_index(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Keith Mannthey6ad91652006-09-26 10:52:36 +0200401 for (; i < PTRS_PER_PMD; i++, address += PMD_SIZE) {
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400402 unsigned long pte_phys;
Keith Mannthey6ad91652006-09-26 10:52:36 +0200403 pmd_t *pmd = pmd_page + pmd_index(address);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400404 pte_t *pte;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700405 pgprot_t new_prot = prot;
Matt Tolentino44df75e2006-01-17 07:03:41 +0100406
Jan Beulich5f51e132006-06-26 13:59:02 +0200407 if (address >= end) {
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100408 if (!after_bootmem) {
Jan Beulich5f51e132006-06-26 13:59:02 +0200409 for (; i < PTRS_PER_PMD; i++, pmd++)
410 set_pmd(pmd, __pmd(0));
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100411 }
Matt Tolentino44df75e2006-01-17 07:03:41 +0100412 break;
413 }
Keith Mannthey6ad91652006-09-26 10:52:36 +0200414
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400415 if (pmd_val(*pmd)) {
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100416 if (!pmd_large(*pmd)) {
417 spin_lock(&init_mm.page_table_lock);
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700418 last_map_addr = phys_pte_update(pmd, address,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700419 end, prot);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100420 spin_unlock(&init_mm.page_table_lock);
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700421 continue;
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100422 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700423 /*
424 * If we are ok with PG_LEVEL_2M mapping, then we will
425 * use the existing mapping,
426 *
427 * Otherwise, we will split the large page mapping but
428 * use the same existing protection bits except for
429 * large page, so that we don't violate Intel's TLB
430 * Application note (317080) which says, while changing
431 * the page sizes, new and old translations should
432 * not differ with respect to page frame and
433 * attributes.
434 */
Yinghai Lu3afa3942008-10-25 22:58:21 -0700435 if (page_size_mask & (1 << PG_LEVEL_2M)) {
436 pages++;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700437 continue;
Yinghai Lu3afa3942008-10-25 22:58:21 -0700438 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700439 new_prot = pte_pgprot(pte_clrhuge(*(pte_t *)pmd));
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400440 }
Keith Mannthey6ad91652006-09-26 10:52:36 +0200441
Yinghai Lub50efd22008-07-08 01:41:05 -0700442 if (page_size_mask & (1<<PG_LEVEL_2M)) {
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400443 pages++;
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100444 spin_lock(&init_mm.page_table_lock);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400445 set_pte((pte_t *)pmd,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700446 pfn_pte(address >> PAGE_SHIFT,
447 __pgprot(pgprot_val(prot) | _PAGE_PSE)));
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100448 spin_unlock(&init_mm.page_table_lock);
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700449 last_map_addr = (address & PMD_MASK) + PMD_SIZE;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400450 continue;
451 }
452
453 pte = alloc_low_page(&pte_phys);
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700454 last_map_addr = phys_pte_init(pte, address, end, new_prot);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400455 unmap_low_page(pte);
456
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100457 spin_lock(&init_mm.page_table_lock);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400458 pmd_populate_kernel(&init_mm, pmd, __va(pte_phys));
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100459 spin_unlock(&init_mm.page_table_lock);
Matt Tolentino44df75e2006-01-17 07:03:41 +0100460 }
Andi Kleence0c0e52008-05-02 11:46:49 +0200461 update_page_count(PG_LEVEL_2M, pages);
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700462 return last_map_addr;
Matt Tolentino44df75e2006-01-17 07:03:41 +0100463}
464
Andi Kleencc615032008-03-12 03:53:28 +0100465static unsigned long __meminit
Yinghai Lub50efd22008-07-08 01:41:05 -0700466phys_pmd_update(pud_t *pud, unsigned long address, unsigned long end,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700467 unsigned long page_size_mask, pgprot_t prot)
Matt Tolentino44df75e2006-01-17 07:03:41 +0100468{
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100469 pmd_t *pmd = pmd_offset(pud, 0);
Andi Kleencc615032008-03-12 03:53:28 +0100470 unsigned long last_map_addr;
471
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700472 last_map_addr = phys_pmd_init(pmd, address, end, page_size_mask, prot);
Keith Mannthey6ad91652006-09-26 10:52:36 +0200473 __flush_tlb_all();
Andi Kleencc615032008-03-12 03:53:28 +0100474 return last_map_addr;
Matt Tolentino44df75e2006-01-17 07:03:41 +0100475}
476
Andi Kleencc615032008-03-12 03:53:28 +0100477static unsigned long __meminit
Yinghai Lub50efd22008-07-08 01:41:05 -0700478phys_pud_init(pud_t *pud_page, unsigned long addr, unsigned long end,
479 unsigned long page_size_mask)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100480{
Andi Kleence0c0e52008-05-02 11:46:49 +0200481 unsigned long pages = 0;
Andi Kleencc615032008-03-12 03:53:28 +0100482 unsigned long last_map_addr = end;
Keith Mannthey6ad91652006-09-26 10:52:36 +0200483 int i = pud_index(addr);
Matt Tolentino44df75e2006-01-17 07:03:41 +0100484
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100485 for (; i < PTRS_PER_PUD; i++, addr = (addr & PUD_MASK) + PUD_SIZE) {
Keith Mannthey6ad91652006-09-26 10:52:36 +0200486 unsigned long pmd_phys;
487 pud_t *pud = pud_page + pud_index(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 pmd_t *pmd;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700489 pgprot_t prot = PAGE_KERNEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Keith Mannthey6ad91652006-09-26 10:52:36 +0200491 if (addr >= end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100494 if (!after_bootmem &&
495 !e820_any_mapped(addr, addr+PUD_SIZE, 0)) {
496 set_pud(pud, __pud(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 continue;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Keith Mannthey6ad91652006-09-26 10:52:36 +0200500 if (pud_val(*pud)) {
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700501 if (!pud_large(*pud)) {
Yinghai Lub50efd22008-07-08 01:41:05 -0700502 last_map_addr = phys_pmd_update(pud, addr, end,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700503 page_size_mask, prot);
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700504 continue;
505 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700506 /*
507 * If we are ok with PG_LEVEL_1G mapping, then we will
508 * use the existing mapping.
509 *
510 * Otherwise, we will split the gbpage mapping but use
511 * the same existing protection bits except for large
512 * page, so that we don't violate Intel's TLB
513 * Application note (317080) which says, while changing
514 * the page sizes, new and old translations should
515 * not differ with respect to page frame and
516 * attributes.
517 */
Yinghai Lu3afa3942008-10-25 22:58:21 -0700518 if (page_size_mask & (1 << PG_LEVEL_1G)) {
519 pages++;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700520 continue;
Yinghai Lu3afa3942008-10-25 22:58:21 -0700521 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700522 prot = pte_pgprot(pte_clrhuge(*(pte_t *)pud));
Andi Kleenef925762008-04-17 17:40:45 +0200523 }
524
Yinghai Lub50efd22008-07-08 01:41:05 -0700525 if (page_size_mask & (1<<PG_LEVEL_1G)) {
Andi Kleence0c0e52008-05-02 11:46:49 +0200526 pages++;
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100527 spin_lock(&init_mm.page_table_lock);
Andi Kleenef925762008-04-17 17:40:45 +0200528 set_pte((pte_t *)pud,
529 pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL_LARGE));
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100530 spin_unlock(&init_mm.page_table_lock);
Andi Kleencc615032008-03-12 03:53:28 +0100531 last_map_addr = (addr & PUD_MASK) + PUD_SIZE;
Keith Mannthey6ad91652006-09-26 10:52:36 +0200532 continue;
533 }
534
Vivek Goyaldafe41e2007-05-02 19:27:06 +0200535 pmd = alloc_low_page(&pmd_phys);
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700536 last_map_addr = phys_pmd_init(pmd, addr, end, page_size_mask,
537 prot);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400538 unmap_low_page(pmd);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100539
540 spin_lock(&init_mm.page_table_lock);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400541 pud_populate(&init_mm, pud, __va(pmd_phys));
Matt Tolentino44df75e2006-01-17 07:03:41 +0100542 spin_unlock(&init_mm.page_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
Andi Kleen1a2b4412008-01-30 13:33:54 +0100544 __flush_tlb_all();
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700545
Andi Kleence0c0e52008-05-02 11:46:49 +0200546 update_page_count(PG_LEVEL_1G, pages);
Andi Kleencc615032008-03-12 03:53:28 +0100547
Yinghai Lu1a0db382008-06-24 14:56:20 -0700548 return last_map_addr;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100549}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400551static unsigned long __meminit
Yinghai Lub50efd22008-07-08 01:41:05 -0700552phys_pud_update(pgd_t *pgd, unsigned long addr, unsigned long end,
553 unsigned long page_size_mask)
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400554{
555 pud_t *pud;
556
557 pud = (pud_t *)pgd_page_vaddr(*pgd);
558
Yinghai Lub50efd22008-07-08 01:41:05 -0700559 return phys_pud_init(pud, addr, end, page_size_mask);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400560}
561
Shaohua Li41d840e2009-06-12 12:57:52 +0800562unsigned long __meminit
Pekka Enbergf7650902009-03-05 14:55:05 +0200563kernel_physical_mapping_init(unsigned long start,
564 unsigned long end,
565 unsigned long page_size_mask)
Yinghai Lub50efd22008-07-08 01:41:05 -0700566{
Haicheng Li9b861522010-08-20 17:50:16 +0800567 bool pgd_changed = false;
Yinghai Lub50efd22008-07-08 01:41:05 -0700568 unsigned long next, last_map_addr = end;
Haicheng Li9b861522010-08-20 17:50:16 +0800569 unsigned long addr;
Yinghai Lub50efd22008-07-08 01:41:05 -0700570
571 start = (unsigned long)__va(start);
572 end = (unsigned long)__va(end);
Wu Fengguang1c5f50e2010-09-03 17:04:07 +0800573 addr = start;
Yinghai Lub50efd22008-07-08 01:41:05 -0700574
575 for (; start < end; start = next) {
576 pgd_t *pgd = pgd_offset_k(start);
577 unsigned long pud_phys;
578 pud_t *pud;
579
Jack Steinere22146e2008-07-16 11:11:59 -0500580 next = (start + PGDIR_SIZE) & PGDIR_MASK;
Yinghai Lub50efd22008-07-08 01:41:05 -0700581 if (next > end)
582 next = end;
583
584 if (pgd_val(*pgd)) {
585 last_map_addr = phys_pud_update(pgd, __pa(start),
586 __pa(end), page_size_mask);
587 continue;
588 }
589
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100590 pud = alloc_low_page(&pud_phys);
Yinghai Lub50efd22008-07-08 01:41:05 -0700591 last_map_addr = phys_pud_init(pud, __pa(start), __pa(next),
592 page_size_mask);
593 unmap_low_page(pud);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100594
595 spin_lock(&init_mm.page_table_lock);
596 pgd_populate(&init_mm, pgd, __va(pud_phys));
597 spin_unlock(&init_mm.page_table_lock);
Haicheng Li9b861522010-08-20 17:50:16 +0800598 pgd_changed = true;
Yinghai Lub50efd22008-07-08 01:41:05 -0700599 }
Haicheng Li9b861522010-08-20 17:50:16 +0800600
601 if (pgd_changed)
602 sync_global_pgds(addr, end);
603
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700604 __flush_tlb_all();
Yinghai Lub50efd22008-07-08 01:41:05 -0700605
606 return last_map_addr;
607}
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700608
Matt Tolentino2b976902005-06-23 00:08:06 -0700609#ifndef CONFIG_NUMA
David Rientjes8ee2deb2009-09-25 15:20:00 -0700610void __init initmem_init(unsigned long start_pfn, unsigned long end_pfn,
611 int acpi, int k8)
Yinghai Lu1f75d7e2008-06-22 02:44:49 -0700612{
Yinghai Lu08677212010-02-10 01:20:20 -0800613#ifndef CONFIG_NO_BOOTMEM
Yinghai Lu1f75d7e2008-06-22 02:44:49 -0700614 unsigned long bootmap_size, bootmap;
615
616 bootmap_size = bootmem_bootmap_pages(end_pfn)<<PAGE_SHIFT;
617 bootmap = find_e820_area(0, end_pfn<<PAGE_SHIFT, bootmap_size,
618 PAGE_SIZE);
619 if (bootmap == -1L)
620 panic("Cannot find bootmem map of size %ld\n", bootmap_size);
Yinghai Lu1842f902010-02-10 01:20:15 -0800621 reserve_early(bootmap, bootmap + bootmap_size, "BOOTMAP");
Yinghai Lu346cafe2008-06-23 03:06:14 -0700622 /* don't touch min_low_pfn */
623 bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap >> PAGE_SHIFT,
624 0, end_pfn);
Yinghai Lu1f75d7e2008-06-22 02:44:49 -0700625 e820_register_active_regions(0, start_pfn, end_pfn);
626 free_bootmem_with_active_regions(0, end_pfn);
Yinghai Lu08677212010-02-10 01:20:20 -0800627#else
628 e820_register_active_regions(0, start_pfn, end_pfn);
629#endif
Yinghai Lu1f75d7e2008-06-22 02:44:49 -0700630}
Pekka Enberg3551f882009-05-07 15:35:41 +0300631#endif
Yinghai Lu1f75d7e2008-06-22 02:44:49 -0700632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633void __init paging_init(void)
634{
Mel Gorman6391af12006-10-11 01:20:39 -0700635 unsigned long max_zone_pfns[MAX_NR_ZONES];
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100636
Mel Gorman6391af12006-10-11 01:20:39 -0700637 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
638 max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
639 max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
Yinghai Luc987d122008-06-24 22:14:09 -0700640 max_zone_pfns[ZONE_NORMAL] = max_pfn;
Mel Gorman6391af12006-10-11 01:20:39 -0700641
Pekka Enberg3551f882009-05-07 15:35:41 +0300642 sparse_memory_present_with_active_regions(MAX_NUMNODES);
Matt Tolentino44df75e2006-01-17 07:03:41 +0100643 sparse_init();
Yinghai Lu44b57282009-07-08 09:50:19 -0700644
645 /*
646 * clear the default setting with node 0
647 * note: don't use nodes_clear here, that is really clearing when
648 * numa support is not compiled in, and later node_set_state
649 * will not set it back.
650 */
651 node_clear_state(0, N_NORMAL_MEMORY);
652
Mel Gorman5cb248a2006-09-27 01:49:52 -0700653 free_area_init_nodes(max_zone_pfns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100656/*
Matt Tolentino44df75e2006-01-17 07:03:41 +0100657 * Memory hotplug specific functions
Matt Tolentino44df75e2006-01-17 07:03:41 +0100658 */
Yasunori Gotobc02af92006-06-27 02:53:30 -0700659#ifdef CONFIG_MEMORY_HOTPLUG
660/*
Shaohui Zhengea085412010-02-02 13:44:16 -0800661 * After memory hotplug the variables max_pfn, max_low_pfn and high_memory need
662 * updating.
663 */
664static void update_end_of_memory_vars(u64 start, u64 size)
665{
666 unsigned long end_pfn = PFN_UP(start + size);
667
668 if (end_pfn > max_pfn) {
669 max_pfn = end_pfn;
670 max_low_pfn = end_pfn;
671 high_memory = (void *)__va(max_pfn * PAGE_SIZE - 1) + 1;
672 }
673}
674
675/*
Yasunori Gotobc02af92006-06-27 02:53:30 -0700676 * Memory is added always to NORMAL zone. This means you will never get
677 * additional DMA/DMA32 memory.
678 */
679int arch_add_memory(int nid, u64 start, u64 size)
680{
681 struct pglist_data *pgdat = NODE_DATA(nid);
Christoph Lameter776ed982006-09-25 23:31:09 -0700682 struct zone *zone = pgdat->node_zones + ZONE_NORMAL;
Andi Kleencc615032008-03-12 03:53:28 +0100683 unsigned long last_mapped_pfn, start_pfn = start >> PAGE_SHIFT;
Yasunori Gotobc02af92006-06-27 02:53:30 -0700684 unsigned long nr_pages = size >> PAGE_SHIFT;
685 int ret;
686
Shaohua Li60817c92008-10-27 13:03:18 -0700687 last_mapped_pfn = init_memory_mapping(start, start + size);
Andi Kleencc615032008-03-12 03:53:28 +0100688 if (last_mapped_pfn > max_pfn_mapped)
689 max_pfn_mapped = last_mapped_pfn;
Keith Mannthey45e0b782006-09-30 23:27:09 -0700690
Gary Hadec04fc582009-01-06 14:39:14 -0800691 ret = __add_pages(nid, zone, start_pfn, nr_pages);
Gary Hadefe8b8682008-10-28 16:43:14 -0700692 WARN_ON_ONCE(ret);
Yasunori Gotobc02af92006-06-27 02:53:30 -0700693
Shaohui Zhengea085412010-02-02 13:44:16 -0800694 /* update max_pfn, max_low_pfn and high_memory */
695 update_end_of_memory_vars(start, size);
696
Yasunori Gotobc02af92006-06-27 02:53:30 -0700697 return ret;
Yasunori Gotobc02af92006-06-27 02:53:30 -0700698}
699EXPORT_SYMBOL_GPL(arch_add_memory);
700
Yasunori Goto82432292006-11-18 22:19:40 -0800701#if !defined(CONFIG_ACPI_NUMA) && defined(CONFIG_NUMA)
Keith Mannthey4942e992006-09-30 23:27:06 -0700702int memory_add_physaddr_to_nid(u64 start)
703{
704 return 0;
705}
Keith Mannthey8c2676a2006-09-30 23:27:07 -0700706EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
Keith Mannthey4942e992006-09-30 23:27:06 -0700707#endif
708
Keith Mannthey45e0b782006-09-30 23:27:09 -0700709#endif /* CONFIG_MEMORY_HOTPLUG */
710
KAMEZAWA Hiroyuki81ac3ad2009-09-22 16:45:49 -0700711static struct kcore_list kcore_vsyscall;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713void __init mem_init(void)
714{
Andi Kleen0a43e4b2005-09-12 18:49:24 +0200715 long codesize, reservedpages, datasize, initsize;
Yinghai Lu11a6b0c2008-10-14 18:59:18 -0700716 unsigned long absent_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Jon Mason0dc243a2006-06-26 13:58:11 +0200718 pci_iommu_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Yinghai Lu48ddb152008-01-30 13:32:36 +0100720 /* clear_bss() already clear the empty_zero_page */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 reservedpages = 0;
723
724 /* this will put all low memory onto the freelists */
Matt Tolentino2b976902005-06-23 00:08:06 -0700725#ifdef CONFIG_NUMA
Andi Kleen0a43e4b2005-09-12 18:49:24 +0200726 totalram_pages = numa_free_all_bootmem();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727#else
Andi Kleen0a43e4b2005-09-12 18:49:24 +0200728 totalram_pages = free_all_bootmem();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729#endif
Yinghai Lu11a6b0c2008-10-14 18:59:18 -0700730
731 absent_pages = absent_pages_in_range(0, max_pfn);
732 reservedpages = max_pfn - totalram_pages - absent_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 after_bootmem = 1;
734
735 codesize = (unsigned long) &_etext - (unsigned long) &_text;
736 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
737 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
738
739 /* Register memory areas for /proc/kcore */
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100740 kclist_add(&kcore_vsyscall, (void *)VSYSCALL_START,
KAMEZAWA Hiroyukic30bb2a2009-09-22 16:45:43 -0700741 VSYSCALL_END - VSYSCALL_START, KCORE_OTHER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Ingo Molnar10f22dd2008-01-30 13:34:10 +0100743 printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, "
Yinghai Lu11a6b0c2008-10-14 18:59:18 -0700744 "%ldk absent, %ldk reserved, %ldk data, %ldk init)\n",
Geert Uytterhoevencc013a82009-09-21 17:02:36 -0700745 nr_free_pages() << (PAGE_SHIFT-10),
Yinghai Luc987d122008-06-24 22:14:09 -0700746 max_pfn << (PAGE_SHIFT-10),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 codesize >> 10,
Yinghai Lu11a6b0c2008-10-14 18:59:18 -0700748 absent_pages << (PAGE_SHIFT-10),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 reservedpages << (PAGE_SHIFT-10),
750 datasize >> 10,
751 initsize >> 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
Arjan van de Ven67df1972006-01-06 00:12:04 -0800754#ifdef CONFIG_DEBUG_RODATA
Arjan van de Venedeed302008-01-30 13:34:08 +0100755const int rodata_test_data = 0xC3;
756EXPORT_SYMBOL_GPL(rodata_test_data);
Arjan van de Ven67df1972006-01-06 00:12:04 -0800757
Suresh Siddha502f6602009-10-28 18:46:56 -0800758int kernel_set_to_readonly;
Steven Rostedt16239632009-02-17 17:57:30 -0500759
760void set_kernel_text_rw(void)
761{
Suresh Siddhab9af7c02009-10-14 14:46:55 -0700762 unsigned long start = PFN_ALIGN(_text);
Suresh Siddhae7d23dd2009-10-28 18:46:58 -0800763 unsigned long end = PFN_ALIGN(__stop___ex_table);
Steven Rostedt16239632009-02-17 17:57:30 -0500764
765 if (!kernel_set_to_readonly)
766 return;
767
768 pr_debug("Set kernel text: %lx - %lx for read write\n",
769 start, end);
770
Suresh Siddhae7d23dd2009-10-28 18:46:58 -0800771 /*
772 * Make the kernel identity mapping for text RW. Kernel text
773 * mapping will always be RO. Refer to the comment in
774 * static_protections() in pageattr.c
775 */
Steven Rostedt16239632009-02-17 17:57:30 -0500776 set_memory_rw(start, (end - start) >> PAGE_SHIFT);
777}
778
779void set_kernel_text_ro(void)
780{
Suresh Siddhab9af7c02009-10-14 14:46:55 -0700781 unsigned long start = PFN_ALIGN(_text);
Suresh Siddhae7d23dd2009-10-28 18:46:58 -0800782 unsigned long end = PFN_ALIGN(__stop___ex_table);
Steven Rostedt16239632009-02-17 17:57:30 -0500783
784 if (!kernel_set_to_readonly)
785 return;
786
787 pr_debug("Set kernel text: %lx - %lx for read only\n",
788 start, end);
789
Suresh Siddhae7d23dd2009-10-28 18:46:58 -0800790 /*
791 * Set the kernel identity mapping for text RO.
792 */
Steven Rostedt16239632009-02-17 17:57:30 -0500793 set_memory_ro(start, (end - start) >> PAGE_SHIFT);
794}
795
Arjan van de Ven67df1972006-01-06 00:12:04 -0800796void mark_rodata_ro(void)
797{
Suresh Siddha74e08172009-10-14 14:46:56 -0700798 unsigned long start = PFN_ALIGN(_text);
Steven Rostedt8f0f9962008-05-12 21:20:56 +0200799 unsigned long rodata_start =
800 ((unsigned long)__start_rodata + PAGE_SIZE - 1) & PAGE_MASK;
Suresh Siddha74e08172009-10-14 14:46:56 -0700801 unsigned long end = (unsigned long) &__end_rodata_hpage_align;
802 unsigned long text_end = PAGE_ALIGN((unsigned long) &__stop___ex_table);
803 unsigned long rodata_end = PAGE_ALIGN((unsigned long) &__end_rodata);
804 unsigned long data_start = (unsigned long) &_sdata;
Steven Rostedt8f0f9962008-05-12 21:20:56 +0200805
Jan Beulich6fb14752007-05-02 19:27:10 +0200806 printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
Linus Torvaldse3ebadd2007-05-07 08:44:24 -0700807 (end - start) >> 10);
Arjan van de Ven984bb802008-02-06 22:39:45 +0100808 set_memory_ro(start, (end - start) >> PAGE_SHIFT);
809
Steven Rostedt16239632009-02-17 17:57:30 -0500810 kernel_set_to_readonly = 1;
811
Arjan van de Ven984bb802008-02-06 22:39:45 +0100812 /*
813 * The rodata section (but not the kernel text!) should also be
814 * not-executable.
815 */
Pekka Paalanen72b59d672008-05-12 21:21:01 +0200816 set_memory_nx(rodata_start, (end - rodata_start) >> PAGE_SHIFT);
Arjan van de Ven67df1972006-01-06 00:12:04 -0800817
Arjan van de Ven1a487252008-01-30 13:34:09 +0100818 rodata_test();
819
Andi Kleen0c42f392008-01-30 13:33:42 +0100820#ifdef CONFIG_CPA_DEBUG
Ingo Molnar10f22dd2008-01-30 13:34:10 +0100821 printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end);
Arjan van de Ven6d238cc2008-01-30 13:34:06 +0100822 set_memory_rw(start, (end-start) >> PAGE_SHIFT);
Andi Kleen0c42f392008-01-30 13:33:42 +0100823
Ingo Molnar10f22dd2008-01-30 13:34:10 +0100824 printk(KERN_INFO "Testing CPA: again\n");
Arjan van de Ven6d238cc2008-01-30 13:34:06 +0100825 set_memory_ro(start, (end-start) >> PAGE_SHIFT);
Andi Kleen0c42f392008-01-30 13:33:42 +0100826#endif
Suresh Siddha74e08172009-10-14 14:46:56 -0700827
828 free_init_pages("unused kernel memory",
829 (unsigned long) page_address(virt_to_page(text_end)),
830 (unsigned long)
831 page_address(virt_to_page(rodata_start)));
832 free_init_pages("unused kernel memory",
833 (unsigned long) page_address(virt_to_page(rodata_end)),
834 (unsigned long) page_address(virt_to_page(data_start)));
Arjan van de Ven67df1972006-01-06 00:12:04 -0800835}
Mathieu Desnoyers4e4eee02008-02-02 15:42:20 -0500836
Arjan van de Ven67df1972006-01-06 00:12:04 -0800837#endif
838
Yinghai Lud2dbf342008-06-13 02:00:56 -0700839int __init reserve_bootmem_generic(unsigned long phys, unsigned long len,
840 int flags)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100841{
Matt Tolentino2b976902005-06-23 00:08:06 -0700842#ifdef CONFIG_NUMA
Yinghai Lu8b3cd09e2008-03-18 12:50:21 -0700843 int nid, next_nid;
Yinghai Lu6a07a0e2008-06-23 14:02:36 -0700844 int ret;
Andi Kleen5e58a022006-11-14 16:57:46 +0100845#endif
846 unsigned long pfn = phys >> PAGE_SHIFT;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100847
Yinghai Luc987d122008-06-24 22:14:09 -0700848 if (pfn >= max_pfn) {
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100849 /*
850 * This can happen with kdump kernels when accessing
851 * firmware tables:
852 */
Thomas Gleixner67794292008-03-21 21:27:10 +0100853 if (pfn < max_pfn_mapped)
Bernhard Walle8b2ef1d2008-06-08 15:46:30 +0200854 return -EFAULT;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100855
Yinghai Lu6a07a0e2008-06-23 14:02:36 -0700856 printk(KERN_ERR "reserve_bootmem: illegal reserve %lx %lu\n",
Andi Kleen5e58a022006-11-14 16:57:46 +0100857 phys, len);
Bernhard Walle8b2ef1d2008-06-08 15:46:30 +0200858 return -EFAULT;
Andi Kleen5e58a022006-11-14 16:57:46 +0100859 }
860
861 /* Should check here against the e820 map to avoid double free */
862#ifdef CONFIG_NUMA
Yinghai Lu8b3cd09e2008-03-18 12:50:21 -0700863 nid = phys_to_nid(phys);
864 next_nid = phys_to_nid(phys + len - 1);
865 if (nid == next_nid)
Bernhard Walle8b2ef1d2008-06-08 15:46:30 +0200866 ret = reserve_bootmem_node(NODE_DATA(nid), phys, len, flags);
Yinghai Lu8b3cd09e2008-03-18 12:50:21 -0700867 else
Bernhard Walle8b2ef1d2008-06-08 15:46:30 +0200868 ret = reserve_bootmem(phys, len, flags);
869
870 if (ret != 0)
871 return ret;
872
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100873#else
Amerigo Wanga6a06f72009-08-21 04:34:45 -0400874 reserve_bootmem(phys, len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875#endif
Yinghai Lu8b3cd09e2008-03-18 12:50:21 -0700876
Mel Gorman0e0b8642006-09-27 01:49:56 -0700877 if (phys+len <= MAX_DMA_PFN*PAGE_SIZE) {
Andi Kleene18c6872005-11-05 17:25:53 +0100878 dma_reserve += len / PAGE_SIZE;
Mel Gorman0e0b8642006-09-27 01:49:56 -0700879 set_dma_reserve(dma_reserve);
880 }
Bernhard Walle8b2ef1d2008-06-08 15:46:30 +0200881
882 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100885int kern_addr_valid(unsigned long addr)
886{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100888 pgd_t *pgd;
889 pud_t *pud;
890 pmd_t *pmd;
891 pte_t *pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 if (above != 0 && above != -1UL)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100894 return 0;
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 pgd = pgd_offset_k(addr);
897 if (pgd_none(*pgd))
898 return 0;
899
900 pud = pud_offset(pgd, addr);
901 if (pud_none(*pud))
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100902 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 pmd = pmd_offset(pud, addr);
905 if (pmd_none(*pmd))
906 return 0;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 if (pmd_large(*pmd))
909 return pfn_valid(pmd_pfn(*pmd));
910
911 pte = pte_offset_kernel(pmd, addr);
912 if (pte_none(*pte))
913 return 0;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return pfn_valid(pte_pfn(*pte));
916}
917
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100918/*
919 * A pseudo VMA to allow ptrace access for the vsyscall page. This only
920 * covers the 64bit vsyscall page now. 32bit has a real VMA now and does
921 * not need special handling anymore:
922 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923static struct vm_area_struct gate_vma = {
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100924 .vm_start = VSYSCALL_START,
925 .vm_end = VSYSCALL_START + (VSYSCALL_MAPPED_PAGES * PAGE_SIZE),
926 .vm_page_prot = PAGE_READONLY_EXEC,
927 .vm_flags = VM_READ | VM_EXEC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928};
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
931{
932#ifdef CONFIG_IA32_EMULATION
Andi Kleen1e014412005-04-16 15:24:55 -0700933 if (test_tsk_thread_flag(tsk, TIF_IA32))
934 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935#endif
936 return &gate_vma;
937}
938
939int in_gate_area(struct task_struct *task, unsigned long addr)
940{
941 struct vm_area_struct *vma = get_gate_vma(task);
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100942
Andi Kleen1e014412005-04-16 15:24:55 -0700943 if (!vma)
944 return 0;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return (addr >= vma->vm_start) && (addr < vma->vm_end);
947}
948
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100949/*
950 * Use this when you have no reliable task/vma, typically from interrupt
951 * context. It is less reliable than using the task's vma and may give
952 * false positives:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 */
954int in_gate_area_no_task(unsigned long addr)
955{
Andi Kleen1e014412005-04-16 15:24:55 -0700956 return (addr >= VSYSCALL_START) && (addr < VSYSCALL_END);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
Zou Nan hai2e1c49d2007-06-01 00:46:28 -0700958
Andi Kleen2aae9502007-07-21 17:10:01 +0200959const char *arch_vma_name(struct vm_area_struct *vma)
960{
961 if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
962 return "[vdso]";
963 if (vma == &gate_vma)
964 return "[vsyscall]";
965 return NULL;
966}
Christoph Lameter0889eba2007-10-16 01:24:15 -0700967
968#ifdef CONFIG_SPARSEMEM_VMEMMAP
969/*
970 * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
971 */
Yinghai Luc2b91e22008-04-12 01:19:24 -0700972static long __meminitdata addr_start, addr_end;
973static void __meminitdata *p_start, *p_end;
974static int __meminitdata node_start;
975
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100976int __meminit
977vmemmap_populate(struct page *start_page, unsigned long size, int node)
Christoph Lameter0889eba2007-10-16 01:24:15 -0700978{
979 unsigned long addr = (unsigned long)start_page;
980 unsigned long end = (unsigned long)(start_page + size);
981 unsigned long next;
982 pgd_t *pgd;
983 pud_t *pud;
984 pmd_t *pmd;
985
986 for (; addr < end; addr = next) {
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -0400987 void *p = NULL;
Christoph Lameter0889eba2007-10-16 01:24:15 -0700988
989 pgd = vmemmap_pgd_populate(addr, node);
990 if (!pgd)
991 return -ENOMEM;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100992
Christoph Lameter0889eba2007-10-16 01:24:15 -0700993 pud = vmemmap_pud_populate(pgd, addr, node);
994 if (!pud)
995 return -ENOMEM;
996
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -0400997 if (!cpu_has_pse) {
998 next = (addr + PAGE_SIZE) & PAGE_MASK;
999 pmd = vmemmap_pmd_populate(pud, addr, node);
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001000
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -04001001 if (!pmd)
1002 return -ENOMEM;
1003
1004 p = vmemmap_pte_populate(pmd, addr, node);
1005
Christoph Lameter0889eba2007-10-16 01:24:15 -07001006 if (!p)
1007 return -ENOMEM;
1008
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -04001009 addr_end = addr + PAGE_SIZE;
1010 p_end = p + PAGE_SIZE;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001011 } else {
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -04001012 next = pmd_addr_end(addr, end);
1013
1014 pmd = pmd_offset(pud, addr);
1015 if (pmd_none(*pmd)) {
1016 pte_t entry;
1017
Yinghai Lu9bdac912010-02-10 01:20:22 -08001018 p = vmemmap_alloc_block_buf(PMD_SIZE, node);
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -04001019 if (!p)
1020 return -ENOMEM;
1021
1022 entry = pfn_pte(__pa(p) >> PAGE_SHIFT,
1023 PAGE_KERNEL_LARGE);
1024 set_pmd(pmd, __pmd(pte_val(entry)));
1025
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -04001026 /* check to see if we have contiguous blocks */
1027 if (p_end != p || node_start != node) {
1028 if (p_start)
1029 printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n",
1030 addr_start, addr_end-1, p_start, p_end-1, node_start);
1031 addr_start = addr;
1032 node_start = node;
1033 p_start = p;
1034 }
Yinghai Lu49c980d2008-07-03 12:29:34 -07001035
1036 addr_end = addr + PMD_SIZE;
1037 p_end = p + PMD_SIZE;
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -04001038 } else
1039 vmemmap_verify((pte_t *)pmd, node, addr, next);
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001040 }
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -04001041
Christoph Lameter0889eba2007-10-16 01:24:15 -07001042 }
Haicheng Li9b861522010-08-20 17:50:16 +08001043 sync_global_pgds((unsigned long)start_page, end);
Christoph Lameter0889eba2007-10-16 01:24:15 -07001044 return 0;
1045}
Yinghai Luc2b91e22008-04-12 01:19:24 -07001046
1047void __meminit vmemmap_populate_print_last(void)
1048{
1049 if (p_start) {
1050 printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n",
1051 addr_start, addr_end-1, p_start, p_end-1, node_start);
1052 p_start = NULL;
1053 p_end = NULL;
1054 node_start = 0;
1055 }
1056}
Christoph Lameter0889eba2007-10-16 01:24:15 -07001057#endif