blob: 7dd7ce49d69bc967f0358a932d0d43200f9cd79f [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>
Pekka Enberg4fcb2082009-03-05 14:55:08 +020051#include <asm/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Yinghai Lu064d25f2008-06-16 19:58:28 -070053/*
Yinghai Lu064d25f2008-06-16 19:58:28 -070054 * end_pfn only includes RAM, while max_pfn_mapped includes all e820 entries.
55 * The direct mapping extends to max_pfn_mapped, so that we can directly access
56 * apertures, ACPI and other tables without having to play with fixmaps.
57 */
Yinghai Luf361a452008-07-10 20:38:26 -070058unsigned long max_low_pfn_mapped;
Yinghai Lu064d25f2008-06-16 19:58:28 -070059unsigned long max_pfn_mapped;
60
Andi Kleene18c6872005-11-05 17:25:53 +010061static unsigned long dma_reserve __initdata;
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
64
Ingo Molnar00d1c5e2008-04-17 17:40:45 +020065static int __init parse_direct_gbpages_off(char *arg)
66{
67 direct_gbpages = 0;
68 return 0;
69}
70early_param("nogbpages", parse_direct_gbpages_off);
71
72static int __init parse_direct_gbpages_on(char *arg)
73{
74 direct_gbpages = 1;
75 return 0;
76}
77early_param("gbpages", parse_direct_gbpages_on);
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/*
80 * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the
81 * physical space so we can cache the place of the first one and move
82 * around without checking the pgd every time.
83 */
84
Jeremy Fitzhardingebe43d722008-09-07 15:21:13 -070085pteval_t __supported_pte_mask __read_mostly = ~_PAGE_IOMAP;
Yinghai Lubd220a22008-09-05 00:58:28 -070086EXPORT_SYMBOL_GPL(__supported_pte_mask);
87
88static int do_not_nx __cpuinitdata;
89
Ingo Molnardeed05b2008-09-05 10:23:26 +020090/*
91 * noexec=on|off
92 * Control non-executable mappings for 64-bit processes.
93 *
94 * on Enable (default)
95 * off Disable
96 */
Yinghai Lubd220a22008-09-05 00:58:28 -070097static int __init nonx_setup(char *str)
98{
99 if (!str)
100 return -EINVAL;
101 if (!strncmp(str, "on", 2)) {
102 __supported_pte_mask |= _PAGE_NX;
103 do_not_nx = 0;
104 } else if (!strncmp(str, "off", 3)) {
105 do_not_nx = 1;
106 __supported_pte_mask &= ~_PAGE_NX;
107 }
108 return 0;
109}
110early_param("noexec", nonx_setup);
111
112void __cpuinit check_efer(void)
113{
114 unsigned long efer;
115
116 rdmsrl(MSR_EFER, efer);
117 if (!(efer & EFER_NX) || do_not_nx)
118 __supported_pte_mask &= ~_PAGE_NX;
119}
120
121int force_personality32;
122
Ingo Molnardeed05b2008-09-05 10:23:26 +0200123/*
124 * noexec32=on|off
125 * Control non executable heap for 32bit processes.
126 * To control the stack too use noexec=off
127 *
128 * on PROT_READ does not imply PROT_EXEC for 32-bit processes (default)
129 * off PROT_READ implies PROT_EXEC
130 */
Yinghai Lubd220a22008-09-05 00:58:28 -0700131static int __init nonx32_setup(char *str)
132{
133 if (!strcmp(str, "on"))
134 force_personality32 &= ~READ_IMPLIES_EXEC;
135 else if (!strcmp(str, "off"))
136 force_personality32 |= READ_IMPLIES_EXEC;
137 return 1;
138}
139__setup("noexec32=", nonx32_setup);
140
Marcin Slusarz8d6ea9672008-08-15 18:32:24 +0200141/*
142 * NOTE: This function is marked __ref because it calls __init function
143 * (alloc_bootmem_pages). It's safe to do it ONLY when after_bootmem == 0.
144 */
145static __ref void *spp_getpage(void)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100146{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 void *ptr;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 if (after_bootmem)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100150 ptr = (void *) get_zeroed_page(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 else
152 ptr = alloc_bootmem_pages(PAGE_SIZE);
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100153
154 if (!ptr || ((unsigned long)ptr & ~PAGE_MASK)) {
155 panic("set_pte_phys: cannot allocate page data %s\n",
156 after_bootmem ? "after bootmem" : "");
157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Ingo Molnar10f22dd2008-01-30 13:34:10 +0100159 pr_debug("spp_getpage %p\n", ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100161 return ptr;
162}
163
Jeremy Fitzhardinged494a962008-06-17 11:41:59 -0700164void
Eduardo Habkost0814e0b2008-06-25 00:19:22 -0400165set_pte_vaddr_pud(pud_t *pud_page, unsigned long vaddr, pte_t new_pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 pud_t *pud;
168 pmd_t *pmd;
Jeremy Fitzhardinged494a962008-06-17 11:41:59 -0700169 pte_t *pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Eduardo Habkost0814e0b2008-06-25 00:19:22 -0400171 pud = pud_page + pud_index(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (pud_none(*pud)) {
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100173 pmd = (pmd_t *) spp_getpage();
Jeremy Fitzhardingebb23e402008-06-25 00:19:02 -0400174 pud_populate(&init_mm, pud, pmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (pmd != pmd_offset(pud, 0)) {
Ingo Molnar10f22dd2008-01-30 13:34:10 +0100176 printk(KERN_ERR "PAGETABLE BUG #01! %p <-> %p\n",
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100177 pmd, pmd_offset(pud, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return;
179 }
180 }
181 pmd = pmd_offset(pud, vaddr);
182 if (pmd_none(*pmd)) {
183 pte = (pte_t *) spp_getpage();
Jeremy Fitzhardingebb23e402008-06-25 00:19:02 -0400184 pmd_populate_kernel(&init_mm, pmd, pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if (pte != pte_offset_kernel(pmd, 0)) {
Ingo Molnar10f22dd2008-01-30 13:34:10 +0100186 printk(KERN_ERR "PAGETABLE BUG #02!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return;
188 }
189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 pte = pte_offset_kernel(pmd, vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 set_pte(pte, new_pte);
193
194 /*
195 * It's enough to flush this one mapping.
196 * (PGE mappings get flushed as well)
197 */
198 __flush_tlb_one(vaddr);
199}
200
Eduardo Habkost0814e0b2008-06-25 00:19:22 -0400201void
202set_pte_vaddr(unsigned long vaddr, pte_t pteval)
203{
204 pgd_t *pgd;
205 pud_t *pud_page;
206
207 pr_debug("set_pte_vaddr %lx to %lx\n", vaddr, native_pte_val(pteval));
208
209 pgd = pgd_offset_k(vaddr);
210 if (pgd_none(*pgd)) {
211 printk(KERN_ERR
212 "PGD FIXMAP MISSING, it should be setup in head.S!\n");
213 return;
214 }
215 pud_page = (pud_t*)pgd_page_vaddr(*pgd);
216 set_pte_vaddr_pud(pud_page, vaddr, pteval);
217}
218
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100219/*
Jack Steiner3a9e1892008-07-01 14:45:32 -0500220 * Create large page table mappings for a range of physical addresses.
221 */
222static void __init __init_extra_mapping(unsigned long phys, unsigned long size,
223 pgprot_t prot)
224{
225 pgd_t *pgd;
226 pud_t *pud;
227 pmd_t *pmd;
228
229 BUG_ON((phys & ~PMD_MASK) || (size & ~PMD_MASK));
230 for (; size; phys += PMD_SIZE, size -= PMD_SIZE) {
231 pgd = pgd_offset_k((unsigned long)__va(phys));
232 if (pgd_none(*pgd)) {
233 pud = (pud_t *) spp_getpage();
234 set_pgd(pgd, __pgd(__pa(pud) | _KERNPG_TABLE |
235 _PAGE_USER));
236 }
237 pud = pud_offset(pgd, (unsigned long)__va(phys));
238 if (pud_none(*pud)) {
239 pmd = (pmd_t *) spp_getpage();
240 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE |
241 _PAGE_USER));
242 }
243 pmd = pmd_offset(pud, phys);
244 BUG_ON(!pmd_none(*pmd));
245 set_pmd(pmd, __pmd(phys | pgprot_val(prot)));
246 }
247}
248
249void __init init_extra_mapping_wb(unsigned long phys, unsigned long size)
250{
251 __init_extra_mapping(phys, size, PAGE_KERNEL_LARGE);
252}
253
254void __init init_extra_mapping_uc(unsigned long phys, unsigned long size)
255{
256 __init_extra_mapping(phys, size, PAGE_KERNEL_LARGE_NOCACHE);
257}
258
259/*
Ingo Molnar88f3aec2008-02-21 11:04:11 +0100260 * The head.S code sets up the kernel high mapping:
261 *
262 * from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text)
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100263 *
264 * phys_addr holds the negative offset to the kernel, which is added
265 * to the compile time generated pmds. This results in invalid pmds up
266 * to the point where we hit the physaddr 0 mapping.
267 *
268 * We limit the mappings to the region from _text to _end. _end is
269 * rounded up to the 2MB boundary. This catches the invalid pmds as
270 * well, as they are located before _text:
271 */
272void __init cleanup_highmap(void)
273{
274 unsigned long vaddr = __START_KERNEL_map;
Joerg Roedeld86bb0d2008-07-25 16:48:57 +0200275 unsigned long end = roundup((unsigned long)_end, PMD_SIZE) - 1;
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100276 pmd_t *pmd = level2_kernel_pgt;
277 pmd_t *last_pmd = pmd + PTRS_PER_PMD;
278
279 for (; pmd < last_pmd; pmd++, vaddr += PMD_SIZE) {
Hugh Dickins2884f112008-05-28 19:36:07 +0100280 if (pmd_none(*pmd))
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100281 continue;
282 if (vaddr < (unsigned long) _text || vaddr > end)
283 set_pmd(pmd, __pmd(0));
284 }
285}
286
Jan Beulich9482ac62008-08-21 14:28:42 +0100287static __ref void *alloc_low_page(unsigned long *phys)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100288{
Pekka Enberg298af9d2009-03-05 14:55:06 +0200289 unsigned long pfn = e820_table_end++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 void *adr;
291
Matt Tolentino44df75e2006-01-17 07:03:41 +0100292 if (after_bootmem) {
293 adr = (void *)get_zeroed_page(GFP_ATOMIC);
294 *phys = __pa(adr);
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100295
Matt Tolentino44df75e2006-01-17 07:03:41 +0100296 return adr;
297 }
298
Pekka Enberg298af9d2009-03-05 14:55:06 +0200299 if (pfn >= e820_table_top)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100300 panic("alloc_low_page: ran out of memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Jeremy Fitzhardinge14941772008-09-07 15:21:15 -0700302 adr = early_memremap(pfn * PAGE_SIZE, PAGE_SIZE);
Vivek Goyaldafe41e2007-05-02 19:27:06 +0200303 memset(adr, 0, PAGE_SIZE);
304 *phys = pfn * PAGE_SIZE;
305 return adr;
306}
307
Jan Beulich9482ac62008-08-21 14:28:42 +0100308static __ref void unmap_low_page(void *adr)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100309{
Matt Tolentino44df75e2006-01-17 07:03:41 +0100310 if (after_bootmem)
311 return;
312
Vivek Goyaldafe41e2007-05-02 19:27:06 +0200313 early_iounmap(adr, PAGE_SIZE);
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100314}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700316static unsigned long __meminit
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700317phys_pte_init(pte_t *pte_page, unsigned long addr, unsigned long end,
318 pgprot_t prot)
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400319{
320 unsigned pages = 0;
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700321 unsigned long last_map_addr = end;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400322 int i;
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700323
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400324 pte_t *pte = pte_page + pte_index(addr);
325
326 for(i = pte_index(addr); i < PTRS_PER_PTE; i++, addr += PAGE_SIZE, pte++) {
327
328 if (addr >= end) {
329 if (!after_bootmem) {
330 for(; i < PTRS_PER_PTE; i++, pte++)
331 set_pte(pte, __pte(0));
332 }
333 break;
334 }
335
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700336 /*
337 * We will re-use the existing mapping.
338 * Xen for example has some special requirements, like mapping
339 * pagetable pages as RO. So assume someone who pre-setup
340 * these mappings are more intelligent.
341 */
Yinghai Lu3afa3942008-10-25 22:58:21 -0700342 if (pte_val(*pte)) {
343 pages++;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400344 continue;
Yinghai Lu3afa3942008-10-25 22:58:21 -0700345 }
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400346
347 if (0)
348 printk(" pte=%p addr=%lx pte=%016lx\n",
349 pte, addr, pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL).pte);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400350 pages++;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700351 set_pte(pte, pfn_pte(addr >> PAGE_SHIFT, prot));
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400352 last_map_addr = (addr & PAGE_MASK) + PAGE_SIZE;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400353 }
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700354
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400355 update_page_count(PG_LEVEL_4K, pages);
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700356
357 return last_map_addr;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400358}
359
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700360static unsigned long __meminit
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700361phys_pte_update(pmd_t *pmd, unsigned long address, unsigned long end,
362 pgprot_t prot)
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400363{
364 pte_t *pte = (pte_t *)pmd_page_vaddr(*pmd);
365
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700366 return phys_pte_init(pte, address, end, prot);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400367}
368
Andi Kleencc615032008-03-12 03:53:28 +0100369static unsigned long __meminit
Yinghai Lub50efd22008-07-08 01:41:05 -0700370phys_pmd_init(pmd_t *pmd_page, unsigned long address, unsigned long end,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700371 unsigned long page_size_mask, pgprot_t prot)
Matt Tolentino44df75e2006-01-17 07:03:41 +0100372{
Andi Kleence0c0e52008-05-02 11:46:49 +0200373 unsigned long pages = 0;
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700374 unsigned long last_map_addr = end;
Andi Kleence0c0e52008-05-02 11:46:49 +0200375
Keith Mannthey6ad91652006-09-26 10:52:36 +0200376 int i = pmd_index(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Keith Mannthey6ad91652006-09-26 10:52:36 +0200378 for (; i < PTRS_PER_PMD; i++, address += PMD_SIZE) {
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400379 unsigned long pte_phys;
Keith Mannthey6ad91652006-09-26 10:52:36 +0200380 pmd_t *pmd = pmd_page + pmd_index(address);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400381 pte_t *pte;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700382 pgprot_t new_prot = prot;
Matt Tolentino44df75e2006-01-17 07:03:41 +0100383
Jan Beulich5f51e132006-06-26 13:59:02 +0200384 if (address >= end) {
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100385 if (!after_bootmem) {
Jan Beulich5f51e132006-06-26 13:59:02 +0200386 for (; i < PTRS_PER_PMD; i++, pmd++)
387 set_pmd(pmd, __pmd(0));
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100388 }
Matt Tolentino44df75e2006-01-17 07:03:41 +0100389 break;
390 }
Keith Mannthey6ad91652006-09-26 10:52:36 +0200391
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400392 if (pmd_val(*pmd)) {
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100393 if (!pmd_large(*pmd)) {
394 spin_lock(&init_mm.page_table_lock);
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700395 last_map_addr = phys_pte_update(pmd, address,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700396 end, prot);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100397 spin_unlock(&init_mm.page_table_lock);
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700398 continue;
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100399 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700400 /*
401 * If we are ok with PG_LEVEL_2M mapping, then we will
402 * use the existing mapping,
403 *
404 * Otherwise, we will split the large page mapping but
405 * use the same existing protection bits except for
406 * large page, so that we don't violate Intel's TLB
407 * Application note (317080) which says, while changing
408 * the page sizes, new and old translations should
409 * not differ with respect to page frame and
410 * attributes.
411 */
Yinghai Lu3afa3942008-10-25 22:58:21 -0700412 if (page_size_mask & (1 << PG_LEVEL_2M)) {
413 pages++;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700414 continue;
Yinghai Lu3afa3942008-10-25 22:58:21 -0700415 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700416 new_prot = pte_pgprot(pte_clrhuge(*(pte_t *)pmd));
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400417 }
Keith Mannthey6ad91652006-09-26 10:52:36 +0200418
Yinghai Lub50efd22008-07-08 01:41:05 -0700419 if (page_size_mask & (1<<PG_LEVEL_2M)) {
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400420 pages++;
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100421 spin_lock(&init_mm.page_table_lock);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400422 set_pte((pte_t *)pmd,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700423 pfn_pte(address >> PAGE_SHIFT,
424 __pgprot(pgprot_val(prot) | _PAGE_PSE)));
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100425 spin_unlock(&init_mm.page_table_lock);
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700426 last_map_addr = (address & PMD_MASK) + PMD_SIZE;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400427 continue;
428 }
429
430 pte = alloc_low_page(&pte_phys);
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700431 last_map_addr = phys_pte_init(pte, address, end, new_prot);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400432 unmap_low_page(pte);
433
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100434 spin_lock(&init_mm.page_table_lock);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400435 pmd_populate_kernel(&init_mm, pmd, __va(pte_phys));
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100436 spin_unlock(&init_mm.page_table_lock);
Matt Tolentino44df75e2006-01-17 07:03:41 +0100437 }
Andi Kleence0c0e52008-05-02 11:46:49 +0200438 update_page_count(PG_LEVEL_2M, pages);
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700439 return last_map_addr;
Matt Tolentino44df75e2006-01-17 07:03:41 +0100440}
441
Andi Kleencc615032008-03-12 03:53:28 +0100442static unsigned long __meminit
Yinghai Lub50efd22008-07-08 01:41:05 -0700443phys_pmd_update(pud_t *pud, unsigned long address, unsigned long end,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700444 unsigned long page_size_mask, pgprot_t prot)
Matt Tolentino44df75e2006-01-17 07:03:41 +0100445{
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100446 pmd_t *pmd = pmd_offset(pud, 0);
Andi Kleencc615032008-03-12 03:53:28 +0100447 unsigned long last_map_addr;
448
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700449 last_map_addr = phys_pmd_init(pmd, address, end, page_size_mask, prot);
Keith Mannthey6ad91652006-09-26 10:52:36 +0200450 __flush_tlb_all();
Andi Kleencc615032008-03-12 03:53:28 +0100451 return last_map_addr;
Matt Tolentino44df75e2006-01-17 07:03:41 +0100452}
453
Andi Kleencc615032008-03-12 03:53:28 +0100454static unsigned long __meminit
Yinghai Lub50efd22008-07-08 01:41:05 -0700455phys_pud_init(pud_t *pud_page, unsigned long addr, unsigned long end,
456 unsigned long page_size_mask)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100457{
Andi Kleence0c0e52008-05-02 11:46:49 +0200458 unsigned long pages = 0;
Andi Kleencc615032008-03-12 03:53:28 +0100459 unsigned long last_map_addr = end;
Keith Mannthey6ad91652006-09-26 10:52:36 +0200460 int i = pud_index(addr);
Matt Tolentino44df75e2006-01-17 07:03:41 +0100461
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100462 for (; i < PTRS_PER_PUD; i++, addr = (addr & PUD_MASK) + PUD_SIZE) {
Keith Mannthey6ad91652006-09-26 10:52:36 +0200463 unsigned long pmd_phys;
464 pud_t *pud = pud_page + pud_index(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 pmd_t *pmd;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700466 pgprot_t prot = PAGE_KERNEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Keith Mannthey6ad91652006-09-26 10:52:36 +0200468 if (addr >= end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100471 if (!after_bootmem &&
472 !e820_any_mapped(addr, addr+PUD_SIZE, 0)) {
473 set_pud(pud, __pud(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 continue;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Keith Mannthey6ad91652006-09-26 10:52:36 +0200477 if (pud_val(*pud)) {
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700478 if (!pud_large(*pud)) {
Yinghai Lub50efd22008-07-08 01:41:05 -0700479 last_map_addr = phys_pmd_update(pud, addr, end,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700480 page_size_mask, prot);
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700481 continue;
482 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700483 /*
484 * If we are ok with PG_LEVEL_1G mapping, then we will
485 * use the existing mapping.
486 *
487 * Otherwise, we will split the gbpage mapping but use
488 * the same existing protection bits except for large
489 * page, so that we don't violate Intel's TLB
490 * Application note (317080) which says, while changing
491 * the page sizes, new and old translations should
492 * not differ with respect to page frame and
493 * attributes.
494 */
Yinghai Lu3afa3942008-10-25 22:58:21 -0700495 if (page_size_mask & (1 << PG_LEVEL_1G)) {
496 pages++;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700497 continue;
Yinghai Lu3afa3942008-10-25 22:58:21 -0700498 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700499 prot = pte_pgprot(pte_clrhuge(*(pte_t *)pud));
Andi Kleenef925762008-04-17 17:40:45 +0200500 }
501
Yinghai Lub50efd22008-07-08 01:41:05 -0700502 if (page_size_mask & (1<<PG_LEVEL_1G)) {
Andi Kleence0c0e52008-05-02 11:46:49 +0200503 pages++;
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100504 spin_lock(&init_mm.page_table_lock);
Andi Kleenef925762008-04-17 17:40:45 +0200505 set_pte((pte_t *)pud,
506 pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL_LARGE));
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100507 spin_unlock(&init_mm.page_table_lock);
Andi Kleencc615032008-03-12 03:53:28 +0100508 last_map_addr = (addr & PUD_MASK) + PUD_SIZE;
Keith Mannthey6ad91652006-09-26 10:52:36 +0200509 continue;
510 }
511
Vivek Goyaldafe41e2007-05-02 19:27:06 +0200512 pmd = alloc_low_page(&pmd_phys);
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700513 last_map_addr = phys_pmd_init(pmd, addr, end, page_size_mask,
514 prot);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400515 unmap_low_page(pmd);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100516
517 spin_lock(&init_mm.page_table_lock);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400518 pud_populate(&init_mm, pud, __va(pmd_phys));
Matt Tolentino44df75e2006-01-17 07:03:41 +0100519 spin_unlock(&init_mm.page_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
Andi Kleen1a2b4412008-01-30 13:33:54 +0100521 __flush_tlb_all();
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700522
Andi Kleence0c0e52008-05-02 11:46:49 +0200523 update_page_count(PG_LEVEL_1G, pages);
Andi Kleencc615032008-03-12 03:53:28 +0100524
Yinghai Lu1a0db382008-06-24 14:56:20 -0700525 return last_map_addr;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100526}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400528static unsigned long __meminit
Yinghai Lub50efd22008-07-08 01:41:05 -0700529phys_pud_update(pgd_t *pgd, unsigned long addr, unsigned long end,
530 unsigned long page_size_mask)
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400531{
532 pud_t *pud;
533
534 pud = (pud_t *)pgd_page_vaddr(*pgd);
535
Yinghai Lub50efd22008-07-08 01:41:05 -0700536 return phys_pud_init(pud, addr, end, page_size_mask);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400537}
538
Pekka Enberge53fb042009-03-05 14:55:07 +0200539unsigned long __init
Pekka Enbergf7650902009-03-05 14:55:05 +0200540kernel_physical_mapping_init(unsigned long start,
541 unsigned long end,
542 unsigned long page_size_mask)
Yinghai Lub50efd22008-07-08 01:41:05 -0700543{
544
545 unsigned long next, last_map_addr = end;
546
547 start = (unsigned long)__va(start);
548 end = (unsigned long)__va(end);
549
550 for (; start < end; start = next) {
551 pgd_t *pgd = pgd_offset_k(start);
552 unsigned long pud_phys;
553 pud_t *pud;
554
Jack Steinere22146e2008-07-16 11:11:59 -0500555 next = (start + PGDIR_SIZE) & PGDIR_MASK;
Yinghai Lub50efd22008-07-08 01:41:05 -0700556 if (next > end)
557 next = end;
558
559 if (pgd_val(*pgd)) {
560 last_map_addr = phys_pud_update(pgd, __pa(start),
561 __pa(end), page_size_mask);
562 continue;
563 }
564
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100565 pud = alloc_low_page(&pud_phys);
Yinghai Lub50efd22008-07-08 01:41:05 -0700566 last_map_addr = phys_pud_init(pud, __pa(start), __pa(next),
567 page_size_mask);
568 unmap_low_page(pud);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100569
570 spin_lock(&init_mm.page_table_lock);
571 pgd_populate(&init_mm, pgd, __va(pud_phys));
572 spin_unlock(&init_mm.page_table_lock);
Yinghai Lub50efd22008-07-08 01:41:05 -0700573 }
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700574 __flush_tlb_all();
Yinghai Lub50efd22008-07-08 01:41:05 -0700575
576 return last_map_addr;
577}
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700578
Matt Tolentino2b976902005-06-23 00:08:06 -0700579#ifndef CONFIG_NUMA
Yinghai Lu1f75d7e2008-06-22 02:44:49 -0700580void __init initmem_init(unsigned long start_pfn, unsigned long end_pfn)
581{
582 unsigned long bootmap_size, bootmap;
583
584 bootmap_size = bootmem_bootmap_pages(end_pfn)<<PAGE_SHIFT;
585 bootmap = find_e820_area(0, end_pfn<<PAGE_SHIFT, bootmap_size,
586 PAGE_SIZE);
587 if (bootmap == -1L)
588 panic("Cannot find bootmem map of size %ld\n", bootmap_size);
Yinghai Lu346cafe2008-06-23 03:06:14 -0700589 /* don't touch min_low_pfn */
590 bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap >> PAGE_SHIFT,
591 0, end_pfn);
Yinghai Lu1f75d7e2008-06-22 02:44:49 -0700592 e820_register_active_regions(0, start_pfn, end_pfn);
593 free_bootmem_with_active_regions(0, end_pfn);
594 early_res_to_bootmem(0, end_pfn<<PAGE_SHIFT);
595 reserve_bootmem(bootmap, bootmap_size, BOOTMEM_DEFAULT);
596}
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598void __init paging_init(void)
599{
Mel Gorman6391af12006-10-11 01:20:39 -0700600 unsigned long max_zone_pfns[MAX_NR_ZONES];
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100601
Mel Gorman6391af12006-10-11 01:20:39 -0700602 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
603 max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
604 max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
Yinghai Luc987d122008-06-24 22:14:09 -0700605 max_zone_pfns[ZONE_NORMAL] = max_pfn;
Mel Gorman6391af12006-10-11 01:20:39 -0700606
Yinghai Luc987d122008-06-24 22:14:09 -0700607 memory_present(0, 0, max_pfn);
Matt Tolentino44df75e2006-01-17 07:03:41 +0100608 sparse_init();
Mel Gorman5cb248a2006-09-27 01:49:52 -0700609 free_area_init_nodes(max_zone_pfns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611#endif
612
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100613/*
Matt Tolentino44df75e2006-01-17 07:03:41 +0100614 * Memory hotplug specific functions
Matt Tolentino44df75e2006-01-17 07:03:41 +0100615 */
Yasunori Gotobc02af92006-06-27 02:53:30 -0700616#ifdef CONFIG_MEMORY_HOTPLUG
617/*
Yasunori Gotobc02af92006-06-27 02:53:30 -0700618 * Memory is added always to NORMAL zone. This means you will never get
619 * additional DMA/DMA32 memory.
620 */
621int arch_add_memory(int nid, u64 start, u64 size)
622{
623 struct pglist_data *pgdat = NODE_DATA(nid);
Christoph Lameter776ed982006-09-25 23:31:09 -0700624 struct zone *zone = pgdat->node_zones + ZONE_NORMAL;
Andi Kleencc615032008-03-12 03:53:28 +0100625 unsigned long last_mapped_pfn, start_pfn = start >> PAGE_SHIFT;
Yasunori Gotobc02af92006-06-27 02:53:30 -0700626 unsigned long nr_pages = size >> PAGE_SHIFT;
627 int ret;
628
Shaohua Li60817c92008-10-27 13:03:18 -0700629 last_mapped_pfn = init_memory_mapping(start, start + size);
Andi Kleencc615032008-03-12 03:53:28 +0100630 if (last_mapped_pfn > max_pfn_mapped)
631 max_pfn_mapped = last_mapped_pfn;
Keith Mannthey45e0b782006-09-30 23:27:09 -0700632
Gary Hadec04fc582009-01-06 14:39:14 -0800633 ret = __add_pages(nid, zone, start_pfn, nr_pages);
Gary Hadefe8b8682008-10-28 16:43:14 -0700634 WARN_ON_ONCE(ret);
Yasunori Gotobc02af92006-06-27 02:53:30 -0700635
Yasunori Gotobc02af92006-06-27 02:53:30 -0700636 return ret;
Yasunori Gotobc02af92006-06-27 02:53:30 -0700637}
638EXPORT_SYMBOL_GPL(arch_add_memory);
639
Yasunori Goto82432292006-11-18 22:19:40 -0800640#if !defined(CONFIG_ACPI_NUMA) && defined(CONFIG_NUMA)
Keith Mannthey4942e992006-09-30 23:27:06 -0700641int memory_add_physaddr_to_nid(u64 start)
642{
643 return 0;
644}
Keith Mannthey8c2676a2006-09-30 23:27:07 -0700645EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
Keith Mannthey4942e992006-09-30 23:27:06 -0700646#endif
647
Keith Mannthey45e0b782006-09-30 23:27:09 -0700648#endif /* CONFIG_MEMORY_HOTPLUG */
649
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100650static struct kcore_list kcore_mem, kcore_vmalloc, kcore_kernel,
651 kcore_modules, kcore_vsyscall;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653void __init mem_init(void)
654{
Andi Kleen0a43e4b2005-09-12 18:49:24 +0200655 long codesize, reservedpages, datasize, initsize;
Yinghai Lu11a6b0c2008-10-14 18:59:18 -0700656 unsigned long absent_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Jon Mason0dc243a2006-06-26 13:58:11 +0200658 pci_iommu_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Yinghai Lu48ddb152008-01-30 13:32:36 +0100660 /* clear_bss() already clear the empty_zero_page */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 reservedpages = 0;
663
664 /* this will put all low memory onto the freelists */
Matt Tolentino2b976902005-06-23 00:08:06 -0700665#ifdef CONFIG_NUMA
Andi Kleen0a43e4b2005-09-12 18:49:24 +0200666 totalram_pages = numa_free_all_bootmem();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667#else
Andi Kleen0a43e4b2005-09-12 18:49:24 +0200668 totalram_pages = free_all_bootmem();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669#endif
Yinghai Lu11a6b0c2008-10-14 18:59:18 -0700670
671 absent_pages = absent_pages_in_range(0, max_pfn);
672 reservedpages = max_pfn - totalram_pages - absent_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 after_bootmem = 1;
674
675 codesize = (unsigned long) &_etext - (unsigned long) &_text;
676 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
677 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
678
679 /* Register memory areas for /proc/kcore */
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100680 kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
681 kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 VMALLOC_END-VMALLOC_START);
683 kclist_add(&kcore_kernel, &_stext, _end - _stext);
684 kclist_add(&kcore_modules, (void *)MODULES_VADDR, MODULES_LEN);
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100685 kclist_add(&kcore_vsyscall, (void *)VSYSCALL_START,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 VSYSCALL_END - VSYSCALL_START);
687
Ingo Molnar10f22dd2008-01-30 13:34:10 +0100688 printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, "
Yinghai Lu11a6b0c2008-10-14 18:59:18 -0700689 "%ldk absent, %ldk reserved, %ldk data, %ldk init)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
Yinghai Luc987d122008-06-24 22:14:09 -0700691 max_pfn << (PAGE_SHIFT-10),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 codesize >> 10,
Yinghai Lu11a6b0c2008-10-14 18:59:18 -0700693 absent_pages << (PAGE_SHIFT-10),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 reservedpages << (PAGE_SHIFT-10),
695 datasize >> 10,
696 initsize >> 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
Arjan van de Ven67df1972006-01-06 00:12:04 -0800699#ifdef CONFIG_DEBUG_RODATA
Arjan van de Venedeed302008-01-30 13:34:08 +0100700const int rodata_test_data = 0xC3;
701EXPORT_SYMBOL_GPL(rodata_test_data);
Arjan van de Ven67df1972006-01-06 00:12:04 -0800702
Arjan van de Ven67df1972006-01-06 00:12:04 -0800703void mark_rodata_ro(void)
704{
Mathieu Desnoyers4e4eee02008-02-02 15:42:20 -0500705 unsigned long start = PFN_ALIGN(_stext), end = PFN_ALIGN(__end_rodata);
Steven Rostedt8f0f9962008-05-12 21:20:56 +0200706 unsigned long rodata_start =
707 ((unsigned long)__start_rodata + PAGE_SIZE - 1) & PAGE_MASK;
708
709#ifdef CONFIG_DYNAMIC_FTRACE
710 /* Dynamic tracing modifies the kernel text section */
711 start = rodata_start;
712#endif
Arjan van de Ven67df1972006-01-06 00:12:04 -0800713
Jan Beulich6fb14752007-05-02 19:27:10 +0200714 printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
Linus Torvaldse3ebadd2007-05-07 08:44:24 -0700715 (end - start) >> 10);
Arjan van de Ven984bb802008-02-06 22:39:45 +0100716 set_memory_ro(start, (end - start) >> PAGE_SHIFT);
717
718 /*
719 * The rodata section (but not the kernel text!) should also be
720 * not-executable.
721 */
Pekka Paalanen72b59d672008-05-12 21:21:01 +0200722 set_memory_nx(rodata_start, (end - rodata_start) >> PAGE_SHIFT);
Arjan van de Ven67df1972006-01-06 00:12:04 -0800723
Arjan van de Ven1a487252008-01-30 13:34:09 +0100724 rodata_test();
725
Andi Kleen0c42f392008-01-30 13:33:42 +0100726#ifdef CONFIG_CPA_DEBUG
Ingo Molnar10f22dd2008-01-30 13:34:10 +0100727 printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end);
Arjan van de Ven6d238cc2008-01-30 13:34:06 +0100728 set_memory_rw(start, (end-start) >> PAGE_SHIFT);
Andi Kleen0c42f392008-01-30 13:33:42 +0100729
Ingo Molnar10f22dd2008-01-30 13:34:10 +0100730 printk(KERN_INFO "Testing CPA: again\n");
Arjan van de Ven6d238cc2008-01-30 13:34:06 +0100731 set_memory_ro(start, (end-start) >> PAGE_SHIFT);
Andi Kleen0c42f392008-01-30 13:33:42 +0100732#endif
Arjan van de Ven67df1972006-01-06 00:12:04 -0800733}
Mathieu Desnoyers4e4eee02008-02-02 15:42:20 -0500734
Arjan van de Ven67df1972006-01-06 00:12:04 -0800735#endif
736
Yinghai Lud2dbf342008-06-13 02:00:56 -0700737int __init reserve_bootmem_generic(unsigned long phys, unsigned long len,
738 int flags)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100739{
Matt Tolentino2b976902005-06-23 00:08:06 -0700740#ifdef CONFIG_NUMA
Yinghai Lu8b3cd09e2008-03-18 12:50:21 -0700741 int nid, next_nid;
Yinghai Lu6a07a0e2008-06-23 14:02:36 -0700742 int ret;
Andi Kleen5e58a022006-11-14 16:57:46 +0100743#endif
744 unsigned long pfn = phys >> PAGE_SHIFT;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100745
Yinghai Luc987d122008-06-24 22:14:09 -0700746 if (pfn >= max_pfn) {
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100747 /*
748 * This can happen with kdump kernels when accessing
749 * firmware tables:
750 */
Thomas Gleixner67794292008-03-21 21:27:10 +0100751 if (pfn < max_pfn_mapped)
Bernhard Walle8b2ef1d2008-06-08 15:46:30 +0200752 return -EFAULT;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100753
Yinghai Lu6a07a0e2008-06-23 14:02:36 -0700754 printk(KERN_ERR "reserve_bootmem: illegal reserve %lx %lu\n",
Andi Kleen5e58a022006-11-14 16:57:46 +0100755 phys, len);
Bernhard Walle8b2ef1d2008-06-08 15:46:30 +0200756 return -EFAULT;
Andi Kleen5e58a022006-11-14 16:57:46 +0100757 }
758
759 /* Should check here against the e820 map to avoid double free */
760#ifdef CONFIG_NUMA
Yinghai Lu8b3cd09e2008-03-18 12:50:21 -0700761 nid = phys_to_nid(phys);
762 next_nid = phys_to_nid(phys + len - 1);
763 if (nid == next_nid)
Bernhard Walle8b2ef1d2008-06-08 15:46:30 +0200764 ret = reserve_bootmem_node(NODE_DATA(nid), phys, len, flags);
Yinghai Lu8b3cd09e2008-03-18 12:50:21 -0700765 else
Bernhard Walle8b2ef1d2008-06-08 15:46:30 +0200766 ret = reserve_bootmem(phys, len, flags);
767
768 if (ret != 0)
769 return ret;
770
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100771#else
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800772 reserve_bootmem(phys, len, BOOTMEM_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773#endif
Yinghai Lu8b3cd09e2008-03-18 12:50:21 -0700774
Mel Gorman0e0b8642006-09-27 01:49:56 -0700775 if (phys+len <= MAX_DMA_PFN*PAGE_SIZE) {
Andi Kleene18c6872005-11-05 17:25:53 +0100776 dma_reserve += len / PAGE_SIZE;
Mel Gorman0e0b8642006-09-27 01:49:56 -0700777 set_dma_reserve(dma_reserve);
778 }
Bernhard Walle8b2ef1d2008-06-08 15:46:30 +0200779
780 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100783int kern_addr_valid(unsigned long addr)
784{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100786 pgd_t *pgd;
787 pud_t *pud;
788 pmd_t *pmd;
789 pte_t *pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 if (above != 0 && above != -1UL)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100792 return 0;
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 pgd = pgd_offset_k(addr);
795 if (pgd_none(*pgd))
796 return 0;
797
798 pud = pud_offset(pgd, addr);
799 if (pud_none(*pud))
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100800 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 pmd = pmd_offset(pud, addr);
803 if (pmd_none(*pmd))
804 return 0;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (pmd_large(*pmd))
807 return pfn_valid(pmd_pfn(*pmd));
808
809 pte = pte_offset_kernel(pmd, addr);
810 if (pte_none(*pte))
811 return 0;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return pfn_valid(pte_pfn(*pte));
814}
815
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100816/*
817 * A pseudo VMA to allow ptrace access for the vsyscall page. This only
818 * covers the 64bit vsyscall page now. 32bit has a real VMA now and does
819 * not need special handling anymore:
820 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821static struct vm_area_struct gate_vma = {
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100822 .vm_start = VSYSCALL_START,
823 .vm_end = VSYSCALL_START + (VSYSCALL_MAPPED_PAGES * PAGE_SIZE),
824 .vm_page_prot = PAGE_READONLY_EXEC,
825 .vm_flags = VM_READ | VM_EXEC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826};
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
829{
830#ifdef CONFIG_IA32_EMULATION
Andi Kleen1e014412005-04-16 15:24:55 -0700831 if (test_tsk_thread_flag(tsk, TIF_IA32))
832 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833#endif
834 return &gate_vma;
835}
836
837int in_gate_area(struct task_struct *task, unsigned long addr)
838{
839 struct vm_area_struct *vma = get_gate_vma(task);
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100840
Andi Kleen1e014412005-04-16 15:24:55 -0700841 if (!vma)
842 return 0;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return (addr >= vma->vm_start) && (addr < vma->vm_end);
845}
846
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100847/*
848 * Use this when you have no reliable task/vma, typically from interrupt
849 * context. It is less reliable than using the task's vma and may give
850 * false positives:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 */
852int in_gate_area_no_task(unsigned long addr)
853{
Andi Kleen1e014412005-04-16 15:24:55 -0700854 return (addr >= VSYSCALL_START) && (addr < VSYSCALL_END);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
Zou Nan hai2e1c49d2007-06-01 00:46:28 -0700856
Andi Kleen2aae9502007-07-21 17:10:01 +0200857const char *arch_vma_name(struct vm_area_struct *vma)
858{
859 if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
860 return "[vdso]";
861 if (vma == &gate_vma)
862 return "[vsyscall]";
863 return NULL;
864}
Christoph Lameter0889eba2007-10-16 01:24:15 -0700865
866#ifdef CONFIG_SPARSEMEM_VMEMMAP
867/*
868 * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
869 */
Yinghai Luc2b91e22008-04-12 01:19:24 -0700870static long __meminitdata addr_start, addr_end;
871static void __meminitdata *p_start, *p_end;
872static int __meminitdata node_start;
873
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100874int __meminit
875vmemmap_populate(struct page *start_page, unsigned long size, int node)
Christoph Lameter0889eba2007-10-16 01:24:15 -0700876{
877 unsigned long addr = (unsigned long)start_page;
878 unsigned long end = (unsigned long)(start_page + size);
879 unsigned long next;
880 pgd_t *pgd;
881 pud_t *pud;
882 pmd_t *pmd;
883
884 for (; addr < end; addr = next) {
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -0400885 void *p = NULL;
Christoph Lameter0889eba2007-10-16 01:24:15 -0700886
887 pgd = vmemmap_pgd_populate(addr, node);
888 if (!pgd)
889 return -ENOMEM;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100890
Christoph Lameter0889eba2007-10-16 01:24:15 -0700891 pud = vmemmap_pud_populate(pgd, addr, node);
892 if (!pud)
893 return -ENOMEM;
894
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -0400895 if (!cpu_has_pse) {
896 next = (addr + PAGE_SIZE) & PAGE_MASK;
897 pmd = vmemmap_pmd_populate(pud, addr, node);
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100898
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -0400899 if (!pmd)
900 return -ENOMEM;
901
902 p = vmemmap_pte_populate(pmd, addr, node);
903
Christoph Lameter0889eba2007-10-16 01:24:15 -0700904 if (!p)
905 return -ENOMEM;
906
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -0400907 addr_end = addr + PAGE_SIZE;
908 p_end = p + PAGE_SIZE;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100909 } else {
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -0400910 next = pmd_addr_end(addr, end);
911
912 pmd = pmd_offset(pud, addr);
913 if (pmd_none(*pmd)) {
914 pte_t entry;
915
916 p = vmemmap_alloc_block(PMD_SIZE, node);
917 if (!p)
918 return -ENOMEM;
919
920 entry = pfn_pte(__pa(p) >> PAGE_SHIFT,
921 PAGE_KERNEL_LARGE);
922 set_pmd(pmd, __pmd(pte_val(entry)));
923
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -0400924 /* check to see if we have contiguous blocks */
925 if (p_end != p || node_start != node) {
926 if (p_start)
927 printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n",
928 addr_start, addr_end-1, p_start, p_end-1, node_start);
929 addr_start = addr;
930 node_start = node;
931 p_start = p;
932 }
Yinghai Lu49c980d2008-07-03 12:29:34 -0700933
934 addr_end = addr + PMD_SIZE;
935 p_end = p + PMD_SIZE;
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -0400936 } else
937 vmemmap_verify((pte_t *)pmd, node, addr, next);
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100938 }
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -0400939
Christoph Lameter0889eba2007-10-16 01:24:15 -0700940 }
Christoph Lameter0889eba2007-10-16 01:24:15 -0700941 return 0;
942}
Yinghai Luc2b91e22008-04-12 01:19:24 -0700943
944void __meminit vmemmap_populate_print_last(void)
945{
946 if (p_start) {
947 printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n",
948 addr_start, addr_end-1, p_start, p_end-1, node_start);
949 p_start = NULL;
950 p_end = NULL;
951 node_start = 0;
952 }
953}
Christoph Lameter0889eba2007-10-16 01:24:15 -0700954#endif