blob: a32fe075608844cd8ec2f5260e45282c83c87a75 [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
5 * Copyright (C) 2000 Pavel Machek <pavel@suse.cz>
6 * 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <asm/processor.h>
Ingo Molnar46eaa672008-10-12 15:06:29 +020034#include <asm/bios_ebda.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/system.h>
36#include <asm/uaccess.h>
37#include <asm/pgtable.h>
38#include <asm/pgalloc.h>
39#include <asm/dma.h>
40#include <asm/fixmap.h>
41#include <asm/e820.h>
42#include <asm/apic.h>
43#include <asm/tlb.h>
44#include <asm/mmu_context.h>
45#include <asm/proto.h>
46#include <asm/smp.h>
Andi Kleen2bc04142005-11-05 17:25:53 +010047#include <asm/sections.h>
Thomas Gleixner718fc132008-01-30 13:30:17 +010048#include <asm/kdebug.h>
Thomas Gleixneraaa64e02008-01-30 13:30:17 +010049#include <asm/numa.h>
Harvey Harrison7bfeab92008-02-12 12:12:01 -080050#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Yinghai Lu064d25f2008-06-16 19:58:28 -070052/*
Yinghai Lu064d25f2008-06-16 19:58:28 -070053 * end_pfn only includes RAM, while max_pfn_mapped includes all e820 entries.
54 * The direct mapping extends to max_pfn_mapped, so that we can directly access
55 * apertures, ACPI and other tables without having to play with fixmaps.
56 */
Yinghai Luf361a452008-07-10 20:38:26 -070057unsigned long max_low_pfn_mapped;
Yinghai Lu064d25f2008-06-16 19:58:28 -070058unsigned long max_pfn_mapped;
59
Andi Kleene18c6872005-11-05 17:25:53 +010060static unsigned long dma_reserve __initdata;
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
63
Ingo Molnar00d1c5e2008-04-17 17:40:45 +020064static int __init parse_direct_gbpages_off(char *arg)
65{
66 direct_gbpages = 0;
67 return 0;
68}
69early_param("nogbpages", parse_direct_gbpages_off);
70
71static int __init parse_direct_gbpages_on(char *arg)
72{
73 direct_gbpages = 1;
74 return 0;
75}
76early_param("gbpages", parse_direct_gbpages_on);
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/*
79 * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the
80 * physical space so we can cache the place of the first one and move
81 * around without checking the pgd every time.
82 */
83
Jeremy Fitzhardingebe43d722008-09-07 15:21:13 -070084pteval_t __supported_pte_mask __read_mostly = ~_PAGE_IOMAP;
Yinghai Lubd220a22008-09-05 00:58:28 -070085EXPORT_SYMBOL_GPL(__supported_pte_mask);
86
87static int do_not_nx __cpuinitdata;
88
Ingo Molnardeed05b2008-09-05 10:23:26 +020089/*
90 * noexec=on|off
91 * Control non-executable mappings for 64-bit processes.
92 *
93 * on Enable (default)
94 * off Disable
95 */
Yinghai Lubd220a22008-09-05 00:58:28 -070096static int __init nonx_setup(char *str)
97{
98 if (!str)
99 return -EINVAL;
100 if (!strncmp(str, "on", 2)) {
101 __supported_pte_mask |= _PAGE_NX;
102 do_not_nx = 0;
103 } else if (!strncmp(str, "off", 3)) {
104 do_not_nx = 1;
105 __supported_pte_mask &= ~_PAGE_NX;
106 }
107 return 0;
108}
109early_param("noexec", nonx_setup);
110
111void __cpuinit check_efer(void)
112{
113 unsigned long efer;
114
115 rdmsrl(MSR_EFER, efer);
116 if (!(efer & EFER_NX) || do_not_nx)
117 __supported_pte_mask &= ~_PAGE_NX;
118}
119
120int force_personality32;
121
Ingo Molnardeed05b2008-09-05 10:23:26 +0200122/*
123 * noexec32=on|off
124 * Control non executable heap for 32bit processes.
125 * To control the stack too use noexec=off
126 *
127 * on PROT_READ does not imply PROT_EXEC for 32-bit processes (default)
128 * off PROT_READ implies PROT_EXEC
129 */
Yinghai Lubd220a22008-09-05 00:58:28 -0700130static int __init nonx32_setup(char *str)
131{
132 if (!strcmp(str, "on"))
133 force_personality32 &= ~READ_IMPLIES_EXEC;
134 else if (!strcmp(str, "off"))
135 force_personality32 |= READ_IMPLIES_EXEC;
136 return 1;
137}
138__setup("noexec32=", nonx32_setup);
139
Marcin Slusarz8d6ea9672008-08-15 18:32:24 +0200140/*
141 * NOTE: This function is marked __ref because it calls __init function
142 * (alloc_bootmem_pages). It's safe to do it ONLY when after_bootmem == 0.
143 */
144static __ref void *spp_getpage(void)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100145{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 void *ptr;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (after_bootmem)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100149 ptr = (void *) get_zeroed_page(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 else
151 ptr = alloc_bootmem_pages(PAGE_SIZE);
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100152
153 if (!ptr || ((unsigned long)ptr & ~PAGE_MASK)) {
154 panic("set_pte_phys: cannot allocate page data %s\n",
155 after_bootmem ? "after bootmem" : "");
156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Ingo Molnar10f22dd2008-01-30 13:34:10 +0100158 pr_debug("spp_getpage %p\n", ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100160 return ptr;
161}
162
Jeremy Fitzhardinged494a962008-06-17 11:41:59 -0700163void
Eduardo Habkost0814e0b2008-06-25 00:19:22 -0400164set_pte_vaddr_pud(pud_t *pud_page, unsigned long vaddr, pte_t new_pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 pud_t *pud;
167 pmd_t *pmd;
Jeremy Fitzhardinged494a962008-06-17 11:41:59 -0700168 pte_t *pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Eduardo Habkost0814e0b2008-06-25 00:19:22 -0400170 pud = pud_page + pud_index(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if (pud_none(*pud)) {
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100172 pmd = (pmd_t *) spp_getpage();
Jeremy Fitzhardingebb23e402008-06-25 00:19:02 -0400173 pud_populate(&init_mm, pud, pmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (pmd != pmd_offset(pud, 0)) {
Ingo Molnar10f22dd2008-01-30 13:34:10 +0100175 printk(KERN_ERR "PAGETABLE BUG #01! %p <-> %p\n",
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100176 pmd, pmd_offset(pud, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return;
178 }
179 }
180 pmd = pmd_offset(pud, vaddr);
181 if (pmd_none(*pmd)) {
182 pte = (pte_t *) spp_getpage();
Jeremy Fitzhardingebb23e402008-06-25 00:19:02 -0400183 pmd_populate_kernel(&init_mm, pmd, pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (pte != pte_offset_kernel(pmd, 0)) {
Ingo Molnar10f22dd2008-01-30 13:34:10 +0100185 printk(KERN_ERR "PAGETABLE BUG #02!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return;
187 }
188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 pte = pte_offset_kernel(pmd, vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 set_pte(pte, new_pte);
192
193 /*
194 * It's enough to flush this one mapping.
195 * (PGE mappings get flushed as well)
196 */
197 __flush_tlb_one(vaddr);
198}
199
Eduardo Habkost0814e0b2008-06-25 00:19:22 -0400200void
201set_pte_vaddr(unsigned long vaddr, pte_t pteval)
202{
203 pgd_t *pgd;
204 pud_t *pud_page;
205
206 pr_debug("set_pte_vaddr %lx to %lx\n", vaddr, native_pte_val(pteval));
207
208 pgd = pgd_offset_k(vaddr);
209 if (pgd_none(*pgd)) {
210 printk(KERN_ERR
211 "PGD FIXMAP MISSING, it should be setup in head.S!\n");
212 return;
213 }
214 pud_page = (pud_t*)pgd_page_vaddr(*pgd);
215 set_pte_vaddr_pud(pud_page, vaddr, pteval);
216}
217
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100218/*
Jack Steiner3a9e1892008-07-01 14:45:32 -0500219 * Create large page table mappings for a range of physical addresses.
220 */
221static void __init __init_extra_mapping(unsigned long phys, unsigned long size,
222 pgprot_t prot)
223{
224 pgd_t *pgd;
225 pud_t *pud;
226 pmd_t *pmd;
227
228 BUG_ON((phys & ~PMD_MASK) || (size & ~PMD_MASK));
229 for (; size; phys += PMD_SIZE, size -= PMD_SIZE) {
230 pgd = pgd_offset_k((unsigned long)__va(phys));
231 if (pgd_none(*pgd)) {
232 pud = (pud_t *) spp_getpage();
233 set_pgd(pgd, __pgd(__pa(pud) | _KERNPG_TABLE |
234 _PAGE_USER));
235 }
236 pud = pud_offset(pgd, (unsigned long)__va(phys));
237 if (pud_none(*pud)) {
238 pmd = (pmd_t *) spp_getpage();
239 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE |
240 _PAGE_USER));
241 }
242 pmd = pmd_offset(pud, phys);
243 BUG_ON(!pmd_none(*pmd));
244 set_pmd(pmd, __pmd(phys | pgprot_val(prot)));
245 }
246}
247
248void __init init_extra_mapping_wb(unsigned long phys, unsigned long size)
249{
250 __init_extra_mapping(phys, size, PAGE_KERNEL_LARGE);
251}
252
253void __init init_extra_mapping_uc(unsigned long phys, unsigned long size)
254{
255 __init_extra_mapping(phys, size, PAGE_KERNEL_LARGE_NOCACHE);
256}
257
258/*
Ingo Molnar88f3aec2008-02-21 11:04:11 +0100259 * The head.S code sets up the kernel high mapping:
260 *
261 * from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text)
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100262 *
263 * phys_addr holds the negative offset to the kernel, which is added
264 * to the compile time generated pmds. This results in invalid pmds up
265 * to the point where we hit the physaddr 0 mapping.
266 *
267 * We limit the mappings to the region from _text to _end. _end is
268 * rounded up to the 2MB boundary. This catches the invalid pmds as
269 * well, as they are located before _text:
270 */
271void __init cleanup_highmap(void)
272{
273 unsigned long vaddr = __START_KERNEL_map;
Joerg Roedeld86bb0d2008-07-25 16:48:57 +0200274 unsigned long end = roundup((unsigned long)_end, PMD_SIZE) - 1;
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100275 pmd_t *pmd = level2_kernel_pgt;
276 pmd_t *last_pmd = pmd + PTRS_PER_PMD;
277
278 for (; pmd < last_pmd; pmd++, vaddr += PMD_SIZE) {
Hugh Dickins2884f112008-05-28 19:36:07 +0100279 if (pmd_none(*pmd))
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100280 continue;
281 if (vaddr < (unsigned long) _text || vaddr > end)
282 set_pmd(pmd, __pmd(0));
283 }
284}
285
Pekka Enbergf7650902009-03-05 14:55:05 +0200286extern unsigned long __initdata table_start;
287extern unsigned long __meminitdata table_end;
288extern unsigned long __meminitdata table_top;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Jan Beulich9482ac62008-08-21 14:28:42 +0100290static __ref void *alloc_low_page(unsigned long *phys)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100291{
Vivek Goyaldafe41e2007-05-02 19:27:06 +0200292 unsigned long pfn = table_end++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 void *adr;
294
Matt Tolentino44df75e2006-01-17 07:03:41 +0100295 if (after_bootmem) {
296 adr = (void *)get_zeroed_page(GFP_ATOMIC);
297 *phys = __pa(adr);
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100298
Matt Tolentino44df75e2006-01-17 07:03:41 +0100299 return adr;
300 }
301
Yinghai Lud86623a2008-06-24 14:57:29 -0700302 if (pfn >= table_top)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100303 panic("alloc_low_page: ran out of memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Jeremy Fitzhardinge14941772008-09-07 15:21:15 -0700305 adr = early_memremap(pfn * PAGE_SIZE, PAGE_SIZE);
Vivek Goyaldafe41e2007-05-02 19:27:06 +0200306 memset(adr, 0, PAGE_SIZE);
307 *phys = pfn * PAGE_SIZE;
308 return adr;
309}
310
Jan Beulich9482ac62008-08-21 14:28:42 +0100311static __ref void unmap_low_page(void *adr)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100312{
Matt Tolentino44df75e2006-01-17 07:03:41 +0100313 if (after_bootmem)
314 return;
315
Vivek Goyaldafe41e2007-05-02 19:27:06 +0200316 early_iounmap(adr, PAGE_SIZE);
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100317}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700319static unsigned long __meminit
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700320phys_pte_init(pte_t *pte_page, unsigned long addr, unsigned long end,
321 pgprot_t prot)
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400322{
323 unsigned pages = 0;
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700324 unsigned long last_map_addr = end;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400325 int i;
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700326
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400327 pte_t *pte = pte_page + pte_index(addr);
328
329 for(i = pte_index(addr); i < PTRS_PER_PTE; i++, addr += PAGE_SIZE, pte++) {
330
331 if (addr >= end) {
332 if (!after_bootmem) {
333 for(; i < PTRS_PER_PTE; i++, pte++)
334 set_pte(pte, __pte(0));
335 }
336 break;
337 }
338
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700339 /*
340 * We will re-use the existing mapping.
341 * Xen for example has some special requirements, like mapping
342 * pagetable pages as RO. So assume someone who pre-setup
343 * these mappings are more intelligent.
344 */
Yinghai Lu3afa3942008-10-25 22:58:21 -0700345 if (pte_val(*pte)) {
346 pages++;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400347 continue;
Yinghai Lu3afa3942008-10-25 22:58:21 -0700348 }
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400349
350 if (0)
351 printk(" pte=%p addr=%lx pte=%016lx\n",
352 pte, addr, pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL).pte);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400353 pages++;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700354 set_pte(pte, pfn_pte(addr >> PAGE_SHIFT, prot));
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400355 last_map_addr = (addr & PAGE_MASK) + PAGE_SIZE;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400356 }
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700357
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400358 update_page_count(PG_LEVEL_4K, pages);
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700359
360 return last_map_addr;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400361}
362
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700363static unsigned long __meminit
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700364phys_pte_update(pmd_t *pmd, unsigned long address, unsigned long end,
365 pgprot_t prot)
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400366{
367 pte_t *pte = (pte_t *)pmd_page_vaddr(*pmd);
368
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700369 return phys_pte_init(pte, address, end, prot);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400370}
371
Andi Kleencc615032008-03-12 03:53:28 +0100372static unsigned long __meminit
Yinghai Lub50efd22008-07-08 01:41:05 -0700373phys_pmd_init(pmd_t *pmd_page, unsigned long address, unsigned long end,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700374 unsigned long page_size_mask, pgprot_t prot)
Matt Tolentino44df75e2006-01-17 07:03:41 +0100375{
Andi Kleence0c0e52008-05-02 11:46:49 +0200376 unsigned long pages = 0;
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700377 unsigned long last_map_addr = end;
Andi Kleence0c0e52008-05-02 11:46:49 +0200378
Keith Mannthey6ad91652006-09-26 10:52:36 +0200379 int i = pmd_index(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Keith Mannthey6ad91652006-09-26 10:52:36 +0200381 for (; i < PTRS_PER_PMD; i++, address += PMD_SIZE) {
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400382 unsigned long pte_phys;
Keith Mannthey6ad91652006-09-26 10:52:36 +0200383 pmd_t *pmd = pmd_page + pmd_index(address);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400384 pte_t *pte;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700385 pgprot_t new_prot = prot;
Matt Tolentino44df75e2006-01-17 07:03:41 +0100386
Jan Beulich5f51e132006-06-26 13:59:02 +0200387 if (address >= end) {
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100388 if (!after_bootmem) {
Jan Beulich5f51e132006-06-26 13:59:02 +0200389 for (; i < PTRS_PER_PMD; i++, pmd++)
390 set_pmd(pmd, __pmd(0));
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100391 }
Matt Tolentino44df75e2006-01-17 07:03:41 +0100392 break;
393 }
Keith Mannthey6ad91652006-09-26 10:52:36 +0200394
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400395 if (pmd_val(*pmd)) {
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100396 if (!pmd_large(*pmd)) {
397 spin_lock(&init_mm.page_table_lock);
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700398 last_map_addr = phys_pte_update(pmd, address,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700399 end, prot);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100400 spin_unlock(&init_mm.page_table_lock);
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700401 continue;
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100402 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700403 /*
404 * If we are ok with PG_LEVEL_2M mapping, then we will
405 * use the existing mapping,
406 *
407 * Otherwise, we will split the large page mapping but
408 * use the same existing protection bits except for
409 * large page, so that we don't violate Intel's TLB
410 * Application note (317080) which says, while changing
411 * the page sizes, new and old translations should
412 * not differ with respect to page frame and
413 * attributes.
414 */
Yinghai Lu3afa3942008-10-25 22:58:21 -0700415 if (page_size_mask & (1 << PG_LEVEL_2M)) {
416 pages++;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700417 continue;
Yinghai Lu3afa3942008-10-25 22:58:21 -0700418 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700419 new_prot = pte_pgprot(pte_clrhuge(*(pte_t *)pmd));
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400420 }
Keith Mannthey6ad91652006-09-26 10:52:36 +0200421
Yinghai Lub50efd22008-07-08 01:41:05 -0700422 if (page_size_mask & (1<<PG_LEVEL_2M)) {
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400423 pages++;
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100424 spin_lock(&init_mm.page_table_lock);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400425 set_pte((pte_t *)pmd,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700426 pfn_pte(address >> PAGE_SHIFT,
427 __pgprot(pgprot_val(prot) | _PAGE_PSE)));
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100428 spin_unlock(&init_mm.page_table_lock);
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700429 last_map_addr = (address & PMD_MASK) + PMD_SIZE;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400430 continue;
431 }
432
433 pte = alloc_low_page(&pte_phys);
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700434 last_map_addr = phys_pte_init(pte, address, end, new_prot);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400435 unmap_low_page(pte);
436
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100437 spin_lock(&init_mm.page_table_lock);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400438 pmd_populate_kernel(&init_mm, pmd, __va(pte_phys));
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100439 spin_unlock(&init_mm.page_table_lock);
Matt Tolentino44df75e2006-01-17 07:03:41 +0100440 }
Andi Kleence0c0e52008-05-02 11:46:49 +0200441 update_page_count(PG_LEVEL_2M, pages);
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700442 return last_map_addr;
Matt Tolentino44df75e2006-01-17 07:03:41 +0100443}
444
Andi Kleencc615032008-03-12 03:53:28 +0100445static unsigned long __meminit
Yinghai Lub50efd22008-07-08 01:41:05 -0700446phys_pmd_update(pud_t *pud, unsigned long address, unsigned long end,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700447 unsigned long page_size_mask, pgprot_t prot)
Matt Tolentino44df75e2006-01-17 07:03:41 +0100448{
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100449 pmd_t *pmd = pmd_offset(pud, 0);
Andi Kleencc615032008-03-12 03:53:28 +0100450 unsigned long last_map_addr;
451
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700452 last_map_addr = phys_pmd_init(pmd, address, end, page_size_mask, prot);
Keith Mannthey6ad91652006-09-26 10:52:36 +0200453 __flush_tlb_all();
Andi Kleencc615032008-03-12 03:53:28 +0100454 return last_map_addr;
Matt Tolentino44df75e2006-01-17 07:03:41 +0100455}
456
Andi Kleencc615032008-03-12 03:53:28 +0100457static unsigned long __meminit
Yinghai Lub50efd22008-07-08 01:41:05 -0700458phys_pud_init(pud_t *pud_page, unsigned long addr, unsigned long end,
459 unsigned long page_size_mask)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100460{
Andi Kleence0c0e52008-05-02 11:46:49 +0200461 unsigned long pages = 0;
Andi Kleencc615032008-03-12 03:53:28 +0100462 unsigned long last_map_addr = end;
Keith Mannthey6ad91652006-09-26 10:52:36 +0200463 int i = pud_index(addr);
Matt Tolentino44df75e2006-01-17 07:03:41 +0100464
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100465 for (; i < PTRS_PER_PUD; i++, addr = (addr & PUD_MASK) + PUD_SIZE) {
Keith Mannthey6ad91652006-09-26 10:52:36 +0200466 unsigned long pmd_phys;
467 pud_t *pud = pud_page + pud_index(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 pmd_t *pmd;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700469 pgprot_t prot = PAGE_KERNEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Keith Mannthey6ad91652006-09-26 10:52:36 +0200471 if (addr >= end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100474 if (!after_bootmem &&
475 !e820_any_mapped(addr, addr+PUD_SIZE, 0)) {
476 set_pud(pud, __pud(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 continue;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Keith Mannthey6ad91652006-09-26 10:52:36 +0200480 if (pud_val(*pud)) {
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700481 if (!pud_large(*pud)) {
Yinghai Lub50efd22008-07-08 01:41:05 -0700482 last_map_addr = phys_pmd_update(pud, addr, end,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700483 page_size_mask, prot);
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700484 continue;
485 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700486 /*
487 * If we are ok with PG_LEVEL_1G mapping, then we will
488 * use the existing mapping.
489 *
490 * Otherwise, we will split the gbpage mapping but use
491 * the same existing protection bits except for large
492 * page, so that we don't violate Intel's TLB
493 * Application note (317080) which says, while changing
494 * the page sizes, new and old translations should
495 * not differ with respect to page frame and
496 * attributes.
497 */
Yinghai Lu3afa3942008-10-25 22:58:21 -0700498 if (page_size_mask & (1 << PG_LEVEL_1G)) {
499 pages++;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700500 continue;
Yinghai Lu3afa3942008-10-25 22:58:21 -0700501 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700502 prot = pte_pgprot(pte_clrhuge(*(pte_t *)pud));
Andi Kleenef925762008-04-17 17:40:45 +0200503 }
504
Yinghai Lub50efd22008-07-08 01:41:05 -0700505 if (page_size_mask & (1<<PG_LEVEL_1G)) {
Andi Kleence0c0e52008-05-02 11:46:49 +0200506 pages++;
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100507 spin_lock(&init_mm.page_table_lock);
Andi Kleenef925762008-04-17 17:40:45 +0200508 set_pte((pte_t *)pud,
509 pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL_LARGE));
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100510 spin_unlock(&init_mm.page_table_lock);
Andi Kleencc615032008-03-12 03:53:28 +0100511 last_map_addr = (addr & PUD_MASK) + PUD_SIZE;
Keith Mannthey6ad91652006-09-26 10:52:36 +0200512 continue;
513 }
514
Vivek Goyaldafe41e2007-05-02 19:27:06 +0200515 pmd = alloc_low_page(&pmd_phys);
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700516 last_map_addr = phys_pmd_init(pmd, addr, end, page_size_mask,
517 prot);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400518 unmap_low_page(pmd);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100519
520 spin_lock(&init_mm.page_table_lock);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400521 pud_populate(&init_mm, pud, __va(pmd_phys));
Matt Tolentino44df75e2006-01-17 07:03:41 +0100522 spin_unlock(&init_mm.page_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
Andi Kleen1a2b4412008-01-30 13:33:54 +0100524 __flush_tlb_all();
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700525
Andi Kleence0c0e52008-05-02 11:46:49 +0200526 update_page_count(PG_LEVEL_1G, pages);
Andi Kleencc615032008-03-12 03:53:28 +0100527
Yinghai Lu1a0db382008-06-24 14:56:20 -0700528 return last_map_addr;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100529}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400531static unsigned long __meminit
Yinghai Lub50efd22008-07-08 01:41:05 -0700532phys_pud_update(pgd_t *pgd, unsigned long addr, unsigned long end,
533 unsigned long page_size_mask)
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400534{
535 pud_t *pud;
536
537 pud = (pud_t *)pgd_page_vaddr(*pgd);
538
Yinghai Lub50efd22008-07-08 01:41:05 -0700539 return phys_pud_init(pud, addr, end, page_size_mask);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400540}
541
Pekka Enbergf7650902009-03-05 14:55:05 +0200542unsigned long __meminit
543kernel_physical_mapping_init(unsigned long start,
544 unsigned long end,
545 unsigned long page_size_mask)
Yinghai Lub50efd22008-07-08 01:41:05 -0700546{
547
548 unsigned long next, last_map_addr = end;
549
550 start = (unsigned long)__va(start);
551 end = (unsigned long)__va(end);
552
553 for (; start < end; start = next) {
554 pgd_t *pgd = pgd_offset_k(start);
555 unsigned long pud_phys;
556 pud_t *pud;
557
Jack Steinere22146e2008-07-16 11:11:59 -0500558 next = (start + PGDIR_SIZE) & PGDIR_MASK;
Yinghai Lub50efd22008-07-08 01:41:05 -0700559 if (next > end)
560 next = end;
561
562 if (pgd_val(*pgd)) {
563 last_map_addr = phys_pud_update(pgd, __pa(start),
564 __pa(end), page_size_mask);
565 continue;
566 }
567
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100568 pud = alloc_low_page(&pud_phys);
Yinghai Lub50efd22008-07-08 01:41:05 -0700569 last_map_addr = phys_pud_init(pud, __pa(start), __pa(next),
570 page_size_mask);
571 unmap_low_page(pud);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100572
573 spin_lock(&init_mm.page_table_lock);
574 pgd_populate(&init_mm, pgd, __va(pud_phys));
575 spin_unlock(&init_mm.page_table_lock);
Yinghai Lub50efd22008-07-08 01:41:05 -0700576 }
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700577 __flush_tlb_all();
Yinghai Lub50efd22008-07-08 01:41:05 -0700578
579 return last_map_addr;
580}
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700581
Matt Tolentino2b976902005-06-23 00:08:06 -0700582#ifndef CONFIG_NUMA
Yinghai Lu1f75d7e2008-06-22 02:44:49 -0700583void __init initmem_init(unsigned long start_pfn, unsigned long end_pfn)
584{
585 unsigned long bootmap_size, bootmap;
586
587 bootmap_size = bootmem_bootmap_pages(end_pfn)<<PAGE_SHIFT;
588 bootmap = find_e820_area(0, end_pfn<<PAGE_SHIFT, bootmap_size,
589 PAGE_SIZE);
590 if (bootmap == -1L)
591 panic("Cannot find bootmem map of size %ld\n", bootmap_size);
Yinghai Lu346cafe2008-06-23 03:06:14 -0700592 /* don't touch min_low_pfn */
593 bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap >> PAGE_SHIFT,
594 0, end_pfn);
Yinghai Lu1f75d7e2008-06-22 02:44:49 -0700595 e820_register_active_regions(0, start_pfn, end_pfn);
596 free_bootmem_with_active_regions(0, end_pfn);
597 early_res_to_bootmem(0, end_pfn<<PAGE_SHIFT);
598 reserve_bootmem(bootmap, bootmap_size, BOOTMEM_DEFAULT);
599}
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601void __init paging_init(void)
602{
Mel Gorman6391af12006-10-11 01:20:39 -0700603 unsigned long max_zone_pfns[MAX_NR_ZONES];
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100604
Mel Gorman6391af12006-10-11 01:20:39 -0700605 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
606 max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
607 max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
Yinghai Luc987d122008-06-24 22:14:09 -0700608 max_zone_pfns[ZONE_NORMAL] = max_pfn;
Mel Gorman6391af12006-10-11 01:20:39 -0700609
Yinghai Luc987d122008-06-24 22:14:09 -0700610 memory_present(0, 0, max_pfn);
Matt Tolentino44df75e2006-01-17 07:03:41 +0100611 sparse_init();
Mel Gorman5cb248a2006-09-27 01:49:52 -0700612 free_area_init_nodes(max_zone_pfns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614#endif
615
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100616/*
Matt Tolentino44df75e2006-01-17 07:03:41 +0100617 * Memory hotplug specific functions
Matt Tolentino44df75e2006-01-17 07:03:41 +0100618 */
Yasunori Gotobc02af92006-06-27 02:53:30 -0700619#ifdef CONFIG_MEMORY_HOTPLUG
620/*
Yasunori Gotobc02af92006-06-27 02:53:30 -0700621 * Memory is added always to NORMAL zone. This means you will never get
622 * additional DMA/DMA32 memory.
623 */
624int arch_add_memory(int nid, u64 start, u64 size)
625{
626 struct pglist_data *pgdat = NODE_DATA(nid);
Christoph Lameter776ed982006-09-25 23:31:09 -0700627 struct zone *zone = pgdat->node_zones + ZONE_NORMAL;
Andi Kleencc615032008-03-12 03:53:28 +0100628 unsigned long last_mapped_pfn, start_pfn = start >> PAGE_SHIFT;
Yasunori Gotobc02af92006-06-27 02:53:30 -0700629 unsigned long nr_pages = size >> PAGE_SHIFT;
630 int ret;
631
Shaohua Li60817c92008-10-27 13:03:18 -0700632 last_mapped_pfn = init_memory_mapping(start, start + size);
Andi Kleencc615032008-03-12 03:53:28 +0100633 if (last_mapped_pfn > max_pfn_mapped)
634 max_pfn_mapped = last_mapped_pfn;
Keith Mannthey45e0b782006-09-30 23:27:09 -0700635
Gary Hadec04fc582009-01-06 14:39:14 -0800636 ret = __add_pages(nid, zone, start_pfn, nr_pages);
Gary Hadefe8b8682008-10-28 16:43:14 -0700637 WARN_ON_ONCE(ret);
Yasunori Gotobc02af92006-06-27 02:53:30 -0700638
Yasunori Gotobc02af92006-06-27 02:53:30 -0700639 return ret;
Yasunori Gotobc02af92006-06-27 02:53:30 -0700640}
641EXPORT_SYMBOL_GPL(arch_add_memory);
642
Yasunori Goto82432292006-11-18 22:19:40 -0800643#if !defined(CONFIG_ACPI_NUMA) && defined(CONFIG_NUMA)
Keith Mannthey4942e992006-09-30 23:27:06 -0700644int memory_add_physaddr_to_nid(u64 start)
645{
646 return 0;
647}
Keith Mannthey8c2676a2006-09-30 23:27:07 -0700648EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
Keith Mannthey4942e992006-09-30 23:27:06 -0700649#endif
650
Keith Mannthey45e0b782006-09-30 23:27:09 -0700651#endif /* CONFIG_MEMORY_HOTPLUG */
652
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100653static struct kcore_list kcore_mem, kcore_vmalloc, kcore_kernel,
654 kcore_modules, kcore_vsyscall;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656void __init mem_init(void)
657{
Andi Kleen0a43e4b2005-09-12 18:49:24 +0200658 long codesize, reservedpages, datasize, initsize;
Yinghai Lu11a6b0c2008-10-14 18:59:18 -0700659 unsigned long absent_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Jon Mason0dc243a2006-06-26 13:58:11 +0200661 pci_iommu_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Yinghai Lu48ddb152008-01-30 13:32:36 +0100663 /* clear_bss() already clear the empty_zero_page */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 reservedpages = 0;
666
667 /* this will put all low memory onto the freelists */
Matt Tolentino2b976902005-06-23 00:08:06 -0700668#ifdef CONFIG_NUMA
Andi Kleen0a43e4b2005-09-12 18:49:24 +0200669 totalram_pages = numa_free_all_bootmem();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670#else
Andi Kleen0a43e4b2005-09-12 18:49:24 +0200671 totalram_pages = free_all_bootmem();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672#endif
Yinghai Lu11a6b0c2008-10-14 18:59:18 -0700673
674 absent_pages = absent_pages_in_range(0, max_pfn);
675 reservedpages = max_pfn - totalram_pages - absent_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 after_bootmem = 1;
677
678 codesize = (unsigned long) &_etext - (unsigned long) &_text;
679 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
680 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
681
682 /* Register memory areas for /proc/kcore */
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100683 kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
684 kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 VMALLOC_END-VMALLOC_START);
686 kclist_add(&kcore_kernel, &_stext, _end - _stext);
687 kclist_add(&kcore_modules, (void *)MODULES_VADDR, MODULES_LEN);
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100688 kclist_add(&kcore_vsyscall, (void *)VSYSCALL_START,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 VSYSCALL_END - VSYSCALL_START);
690
Ingo Molnar10f22dd2008-01-30 13:34:10 +0100691 printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, "
Yinghai Lu11a6b0c2008-10-14 18:59:18 -0700692 "%ldk absent, %ldk reserved, %ldk data, %ldk init)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
Yinghai Luc987d122008-06-24 22:14:09 -0700694 max_pfn << (PAGE_SHIFT-10),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 codesize >> 10,
Yinghai Lu11a6b0c2008-10-14 18:59:18 -0700696 absent_pages << (PAGE_SHIFT-10),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 reservedpages << (PAGE_SHIFT-10),
698 datasize >> 10,
699 initsize >> 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
Arjan van de Ven67df1972006-01-06 00:12:04 -0800702#ifdef CONFIG_DEBUG_RODATA
Arjan van de Venedeed302008-01-30 13:34:08 +0100703const int rodata_test_data = 0xC3;
704EXPORT_SYMBOL_GPL(rodata_test_data);
Arjan van de Ven67df1972006-01-06 00:12:04 -0800705
Arjan van de Ven67df1972006-01-06 00:12:04 -0800706void mark_rodata_ro(void)
707{
Mathieu Desnoyers4e4eee02008-02-02 15:42:20 -0500708 unsigned long start = PFN_ALIGN(_stext), end = PFN_ALIGN(__end_rodata);
Steven Rostedt8f0f9962008-05-12 21:20:56 +0200709 unsigned long rodata_start =
710 ((unsigned long)__start_rodata + PAGE_SIZE - 1) & PAGE_MASK;
711
712#ifdef CONFIG_DYNAMIC_FTRACE
713 /* Dynamic tracing modifies the kernel text section */
714 start = rodata_start;
715#endif
Arjan van de Ven67df1972006-01-06 00:12:04 -0800716
Jan Beulich6fb14752007-05-02 19:27:10 +0200717 printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
Linus Torvaldse3ebadd2007-05-07 08:44:24 -0700718 (end - start) >> 10);
Arjan van de Ven984bb802008-02-06 22:39:45 +0100719 set_memory_ro(start, (end - start) >> PAGE_SHIFT);
720
721 /*
722 * The rodata section (but not the kernel text!) should also be
723 * not-executable.
724 */
Pekka Paalanen72b59d672008-05-12 21:21:01 +0200725 set_memory_nx(rodata_start, (end - rodata_start) >> PAGE_SHIFT);
Arjan van de Ven67df1972006-01-06 00:12:04 -0800726
Arjan van de Ven1a487252008-01-30 13:34:09 +0100727 rodata_test();
728
Andi Kleen0c42f392008-01-30 13:33:42 +0100729#ifdef CONFIG_CPA_DEBUG
Ingo Molnar10f22dd2008-01-30 13:34:10 +0100730 printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end);
Arjan van de Ven6d238cc2008-01-30 13:34:06 +0100731 set_memory_rw(start, (end-start) >> PAGE_SHIFT);
Andi Kleen0c42f392008-01-30 13:33:42 +0100732
Ingo Molnar10f22dd2008-01-30 13:34:10 +0100733 printk(KERN_INFO "Testing CPA: again\n");
Arjan van de Ven6d238cc2008-01-30 13:34:06 +0100734 set_memory_ro(start, (end-start) >> PAGE_SHIFT);
Andi Kleen0c42f392008-01-30 13:33:42 +0100735#endif
Arjan van de Ven67df1972006-01-06 00:12:04 -0800736}
Mathieu Desnoyers4e4eee02008-02-02 15:42:20 -0500737
Arjan van de Ven67df1972006-01-06 00:12:04 -0800738#endif
739
Yinghai Lud2dbf342008-06-13 02:00:56 -0700740int __init reserve_bootmem_generic(unsigned long phys, unsigned long len,
741 int flags)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100742{
Matt Tolentino2b976902005-06-23 00:08:06 -0700743#ifdef CONFIG_NUMA
Yinghai Lu8b3cd09e2008-03-18 12:50:21 -0700744 int nid, next_nid;
Yinghai Lu6a07a0e2008-06-23 14:02:36 -0700745 int ret;
Andi Kleen5e58a022006-11-14 16:57:46 +0100746#endif
747 unsigned long pfn = phys >> PAGE_SHIFT;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100748
Yinghai Luc987d122008-06-24 22:14:09 -0700749 if (pfn >= max_pfn) {
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100750 /*
751 * This can happen with kdump kernels when accessing
752 * firmware tables:
753 */
Thomas Gleixner67794292008-03-21 21:27:10 +0100754 if (pfn < max_pfn_mapped)
Bernhard Walle8b2ef1d2008-06-08 15:46:30 +0200755 return -EFAULT;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100756
Yinghai Lu6a07a0e2008-06-23 14:02:36 -0700757 printk(KERN_ERR "reserve_bootmem: illegal reserve %lx %lu\n",
Andi Kleen5e58a022006-11-14 16:57:46 +0100758 phys, len);
Bernhard Walle8b2ef1d2008-06-08 15:46:30 +0200759 return -EFAULT;
Andi Kleen5e58a022006-11-14 16:57:46 +0100760 }
761
762 /* Should check here against the e820 map to avoid double free */
763#ifdef CONFIG_NUMA
Yinghai Lu8b3cd09e2008-03-18 12:50:21 -0700764 nid = phys_to_nid(phys);
765 next_nid = phys_to_nid(phys + len - 1);
766 if (nid == next_nid)
Bernhard Walle8b2ef1d2008-06-08 15:46:30 +0200767 ret = reserve_bootmem_node(NODE_DATA(nid), phys, len, flags);
Yinghai Lu8b3cd09e2008-03-18 12:50:21 -0700768 else
Bernhard Walle8b2ef1d2008-06-08 15:46:30 +0200769 ret = reserve_bootmem(phys, len, flags);
770
771 if (ret != 0)
772 return ret;
773
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100774#else
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800775 reserve_bootmem(phys, len, BOOTMEM_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776#endif
Yinghai Lu8b3cd09e2008-03-18 12:50:21 -0700777
Mel Gorman0e0b8642006-09-27 01:49:56 -0700778 if (phys+len <= MAX_DMA_PFN*PAGE_SIZE) {
Andi Kleene18c6872005-11-05 17:25:53 +0100779 dma_reserve += len / PAGE_SIZE;
Mel Gorman0e0b8642006-09-27 01:49:56 -0700780 set_dma_reserve(dma_reserve);
781 }
Bernhard Walle8b2ef1d2008-06-08 15:46:30 +0200782
783 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100786int kern_addr_valid(unsigned long addr)
787{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100789 pgd_t *pgd;
790 pud_t *pud;
791 pmd_t *pmd;
792 pte_t *pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 if (above != 0 && above != -1UL)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100795 return 0;
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 pgd = pgd_offset_k(addr);
798 if (pgd_none(*pgd))
799 return 0;
800
801 pud = pud_offset(pgd, addr);
802 if (pud_none(*pud))
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100803 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 pmd = pmd_offset(pud, addr);
806 if (pmd_none(*pmd))
807 return 0;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (pmd_large(*pmd))
810 return pfn_valid(pmd_pfn(*pmd));
811
812 pte = pte_offset_kernel(pmd, addr);
813 if (pte_none(*pte))
814 return 0;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return pfn_valid(pte_pfn(*pte));
817}
818
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100819/*
820 * A pseudo VMA to allow ptrace access for the vsyscall page. This only
821 * covers the 64bit vsyscall page now. 32bit has a real VMA now and does
822 * not need special handling anymore:
823 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824static struct vm_area_struct gate_vma = {
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100825 .vm_start = VSYSCALL_START,
826 .vm_end = VSYSCALL_START + (VSYSCALL_MAPPED_PAGES * PAGE_SIZE),
827 .vm_page_prot = PAGE_READONLY_EXEC,
828 .vm_flags = VM_READ | VM_EXEC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829};
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
832{
833#ifdef CONFIG_IA32_EMULATION
Andi Kleen1e014412005-04-16 15:24:55 -0700834 if (test_tsk_thread_flag(tsk, TIF_IA32))
835 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836#endif
837 return &gate_vma;
838}
839
840int in_gate_area(struct task_struct *task, unsigned long addr)
841{
842 struct vm_area_struct *vma = get_gate_vma(task);
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100843
Andi Kleen1e014412005-04-16 15:24:55 -0700844 if (!vma)
845 return 0;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return (addr >= vma->vm_start) && (addr < vma->vm_end);
848}
849
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100850/*
851 * Use this when you have no reliable task/vma, typically from interrupt
852 * context. It is less reliable than using the task's vma and may give
853 * false positives:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 */
855int in_gate_area_no_task(unsigned long addr)
856{
Andi Kleen1e014412005-04-16 15:24:55 -0700857 return (addr >= VSYSCALL_START) && (addr < VSYSCALL_END);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
Zou Nan hai2e1c49d2007-06-01 00:46:28 -0700859
Andi Kleen2aae9502007-07-21 17:10:01 +0200860const char *arch_vma_name(struct vm_area_struct *vma)
861{
862 if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
863 return "[vdso]";
864 if (vma == &gate_vma)
865 return "[vsyscall]";
866 return NULL;
867}
Christoph Lameter0889eba2007-10-16 01:24:15 -0700868
869#ifdef CONFIG_SPARSEMEM_VMEMMAP
870/*
871 * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
872 */
Yinghai Luc2b91e22008-04-12 01:19:24 -0700873static long __meminitdata addr_start, addr_end;
874static void __meminitdata *p_start, *p_end;
875static int __meminitdata node_start;
876
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100877int __meminit
878vmemmap_populate(struct page *start_page, unsigned long size, int node)
Christoph Lameter0889eba2007-10-16 01:24:15 -0700879{
880 unsigned long addr = (unsigned long)start_page;
881 unsigned long end = (unsigned long)(start_page + size);
882 unsigned long next;
883 pgd_t *pgd;
884 pud_t *pud;
885 pmd_t *pmd;
886
887 for (; addr < end; addr = next) {
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -0400888 void *p = NULL;
Christoph Lameter0889eba2007-10-16 01:24:15 -0700889
890 pgd = vmemmap_pgd_populate(addr, node);
891 if (!pgd)
892 return -ENOMEM;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100893
Christoph Lameter0889eba2007-10-16 01:24:15 -0700894 pud = vmemmap_pud_populate(pgd, addr, node);
895 if (!pud)
896 return -ENOMEM;
897
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -0400898 if (!cpu_has_pse) {
899 next = (addr + PAGE_SIZE) & PAGE_MASK;
900 pmd = vmemmap_pmd_populate(pud, addr, node);
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100901
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -0400902 if (!pmd)
903 return -ENOMEM;
904
905 p = vmemmap_pte_populate(pmd, addr, node);
906
Christoph Lameter0889eba2007-10-16 01:24:15 -0700907 if (!p)
908 return -ENOMEM;
909
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -0400910 addr_end = addr + PAGE_SIZE;
911 p_end = p + PAGE_SIZE;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100912 } else {
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -0400913 next = pmd_addr_end(addr, end);
914
915 pmd = pmd_offset(pud, addr);
916 if (pmd_none(*pmd)) {
917 pte_t entry;
918
919 p = vmemmap_alloc_block(PMD_SIZE, node);
920 if (!p)
921 return -ENOMEM;
922
923 entry = pfn_pte(__pa(p) >> PAGE_SHIFT,
924 PAGE_KERNEL_LARGE);
925 set_pmd(pmd, __pmd(pte_val(entry)));
926
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -0400927 /* check to see if we have contiguous blocks */
928 if (p_end != p || node_start != node) {
929 if (p_start)
930 printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n",
931 addr_start, addr_end-1, p_start, p_end-1, node_start);
932 addr_start = addr;
933 node_start = node;
934 p_start = p;
935 }
Yinghai Lu49c980d2008-07-03 12:29:34 -0700936
937 addr_end = addr + PMD_SIZE;
938 p_end = p + PMD_SIZE;
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -0400939 } else
940 vmemmap_verify((pte_t *)pmd, node, addr, next);
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100941 }
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -0400942
Christoph Lameter0889eba2007-10-16 01:24:15 -0700943 }
Christoph Lameter0889eba2007-10-16 01:24:15 -0700944 return 0;
945}
Yinghai Luc2b91e22008-04-12 01:19:24 -0700946
947void __meminit vmemmap_populate_print_last(void)
948{
949 if (p_start) {
950 printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n",
951 addr_start, addr_end-1, p_start, p_end-1, node_start);
952 p_start = NULL;
953 p_end = NULL;
954 node_start = 0;
955 }
956}
Christoph Lameter0889eba2007-10-16 01:24:15 -0700957#endif