]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | ||
1da177e4 LT |
9 | #include <linux/signal.h> |
10 | #include <linux/sched.h> | |
11 | #include <linux/kernel.h> | |
12 | #include <linux/errno.h> | |
13 | #include <linux/string.h> | |
14 | #include <linux/types.h> | |
15 | #include <linux/ptrace.h> | |
16 | #include <linux/mman.h> | |
17 | #include <linux/mm.h> | |
18 | #include <linux/swap.h> | |
19 | #include <linux/smp.h> | |
20 | #include <linux/init.h> | |
11034d55 | 21 | #include <linux/initrd.h> |
1da177e4 LT |
22 | #include <linux/pagemap.h> |
23 | #include <linux/bootmem.h> | |
24 | #include <linux/proc_fs.h> | |
59170891 | 25 | #include <linux/pci.h> |
6fb14755 | 26 | #include <linux/pfn.h> |
c9cf5528 | 27 | #include <linux/poison.h> |
17a941d8 | 28 | #include <linux/dma-mapping.h> |
44df75e6 MT |
29 | #include <linux/module.h> |
30 | #include <linux/memory_hotplug.h> | |
ae32b129 | 31 | #include <linux/nmi.h> |
1da177e4 LT |
32 | |
33 | #include <asm/processor.h> | |
34 | #include <asm/system.h> | |
35 | #include <asm/uaccess.h> | |
36 | #include <asm/pgtable.h> | |
37 | #include <asm/pgalloc.h> | |
38 | #include <asm/dma.h> | |
39 | #include <asm/fixmap.h> | |
40 | #include <asm/e820.h> | |
41 | #include <asm/apic.h> | |
42 | #include <asm/tlb.h> | |
43 | #include <asm/mmu_context.h> | |
44 | #include <asm/proto.h> | |
45 | #include <asm/smp.h> | |
2bc0414e | 46 | #include <asm/sections.h> |
718fc13b | 47 | #include <asm/kdebug.h> |
aaa64e04 | 48 | #include <asm/numa.h> |
7bfeab9a | 49 | #include <asm/cacheflush.h> |
1da177e4 | 50 | |
064d25f1 YL |
51 | /* |
52 | * end_pfn only includes RAM, while max_pfn_mapped includes all e820 entries. | |
53 | * The direct mapping extends to max_pfn_mapped, so that we can directly access | |
54 | * apertures, ACPI and other tables without having to play with fixmaps. | |
55 | */ | |
f361a450 | 56 | unsigned long max_low_pfn_mapped; |
064d25f1 YL |
57 | unsigned long max_pfn_mapped; |
58 | ||
e18c6874 AK |
59 | static unsigned long dma_reserve __initdata; |
60 | ||
1da177e4 LT |
61 | DEFINE_PER_CPU(struct mmu_gather, mmu_gathers); |
62 | ||
a06de630 | 63 | int direct_gbpages |
00d1c5e0 IM |
64 | #ifdef CONFIG_DIRECT_GBPAGES |
65 | = 1 | |
66 | #endif | |
67 | ; | |
68 | ||
69 | static int __init parse_direct_gbpages_off(char *arg) | |
70 | { | |
71 | direct_gbpages = 0; | |
72 | return 0; | |
73 | } | |
74 | early_param("nogbpages", parse_direct_gbpages_off); | |
75 | ||
76 | static int __init parse_direct_gbpages_on(char *arg) | |
77 | { | |
78 | direct_gbpages = 1; | |
79 | return 0; | |
80 | } | |
81 | early_param("gbpages", parse_direct_gbpages_on); | |
82 | ||
1da177e4 LT |
83 | /* |
84 | * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the | |
85 | * physical space so we can cache the place of the first one and move | |
86 | * around without checking the pgd every time. | |
87 | */ | |
88 | ||
1da177e4 LT |
89 | int after_bootmem; |
90 | ||
5f44a669 | 91 | static __init void *spp_getpage(void) |
14a62c34 | 92 | { |
1da177e4 | 93 | void *ptr; |
14a62c34 | 94 | |
1da177e4 | 95 | if (after_bootmem) |
14a62c34 | 96 | ptr = (void *) get_zeroed_page(GFP_ATOMIC); |
1da177e4 LT |
97 | else |
98 | ptr = alloc_bootmem_pages(PAGE_SIZE); | |
14a62c34 TG |
99 | |
100 | if (!ptr || ((unsigned long)ptr & ~PAGE_MASK)) { | |
101 | panic("set_pte_phys: cannot allocate page data %s\n", | |
102 | after_bootmem ? "after bootmem" : ""); | |
103 | } | |
1da177e4 | 104 | |
10f22dde | 105 | pr_debug("spp_getpage %p\n", ptr); |
14a62c34 | 106 | |
1da177e4 | 107 | return ptr; |
14a62c34 | 108 | } |
1da177e4 | 109 | |
d494a961 | 110 | void |
0814e0ba | 111 | set_pte_vaddr_pud(pud_t *pud_page, unsigned long vaddr, pte_t new_pte) |
1da177e4 | 112 | { |
1da177e4 LT |
113 | pud_t *pud; |
114 | pmd_t *pmd; | |
d494a961 | 115 | pte_t *pte; |
1da177e4 | 116 | |
0814e0ba | 117 | pud = pud_page + pud_index(vaddr); |
1da177e4 | 118 | if (pud_none(*pud)) { |
14a62c34 | 119 | pmd = (pmd_t *) spp_getpage(); |
bb23e403 | 120 | pud_populate(&init_mm, pud, pmd); |
1da177e4 | 121 | if (pmd != pmd_offset(pud, 0)) { |
10f22dde | 122 | printk(KERN_ERR "PAGETABLE BUG #01! %p <-> %p\n", |
14a62c34 | 123 | pmd, pmd_offset(pud, 0)); |
1da177e4 LT |
124 | return; |
125 | } | |
126 | } | |
127 | pmd = pmd_offset(pud, vaddr); | |
128 | if (pmd_none(*pmd)) { | |
129 | pte = (pte_t *) spp_getpage(); | |
bb23e403 | 130 | pmd_populate_kernel(&init_mm, pmd, pte); |
1da177e4 | 131 | if (pte != pte_offset_kernel(pmd, 0)) { |
10f22dde | 132 | printk(KERN_ERR "PAGETABLE BUG #02!\n"); |
1da177e4 LT |
133 | return; |
134 | } | |
135 | } | |
1da177e4 LT |
136 | |
137 | pte = pte_offset_kernel(pmd, vaddr); | |
70c9f590 | 138 | if (!pte_none(*pte) && pte_val(new_pte) && |
1da177e4 LT |
139 | pte_val(*pte) != (pte_val(new_pte) & __supported_pte_mask)) |
140 | pte_ERROR(*pte); | |
141 | set_pte(pte, new_pte); | |
142 | ||
143 | /* | |
144 | * It's enough to flush this one mapping. | |
145 | * (PGE mappings get flushed as well) | |
146 | */ | |
147 | __flush_tlb_one(vaddr); | |
148 | } | |
149 | ||
0814e0ba EH |
150 | void |
151 | set_pte_vaddr(unsigned long vaddr, pte_t pteval) | |
152 | { | |
153 | pgd_t *pgd; | |
154 | pud_t *pud_page; | |
155 | ||
156 | pr_debug("set_pte_vaddr %lx to %lx\n", vaddr, native_pte_val(pteval)); | |
157 | ||
158 | pgd = pgd_offset_k(vaddr); | |
159 | if (pgd_none(*pgd)) { | |
160 | printk(KERN_ERR | |
161 | "PGD FIXMAP MISSING, it should be setup in head.S!\n"); | |
162 | return; | |
163 | } | |
164 | pud_page = (pud_t*)pgd_page_vaddr(*pgd); | |
165 | set_pte_vaddr_pud(pud_page, vaddr, pteval); | |
166 | } | |
167 | ||
3a9e189d JS |
168 | /* |
169 | * Create large page table mappings for a range of physical addresses. | |
170 | */ | |
171 | static void __init __init_extra_mapping(unsigned long phys, unsigned long size, | |
172 | pgprot_t prot) | |
173 | { | |
174 | pgd_t *pgd; | |
175 | pud_t *pud; | |
176 | pmd_t *pmd; | |
177 | ||
178 | BUG_ON((phys & ~PMD_MASK) || (size & ~PMD_MASK)); | |
179 | for (; size; phys += PMD_SIZE, size -= PMD_SIZE) { | |
180 | pgd = pgd_offset_k((unsigned long)__va(phys)); | |
181 | if (pgd_none(*pgd)) { | |
182 | pud = (pud_t *) spp_getpage(); | |
183 | set_pgd(pgd, __pgd(__pa(pud) | _KERNPG_TABLE | | |
184 | _PAGE_USER)); | |
185 | } | |
186 | pud = pud_offset(pgd, (unsigned long)__va(phys)); | |
187 | if (pud_none(*pud)) { | |
188 | pmd = (pmd_t *) spp_getpage(); | |
189 | set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE | | |
190 | _PAGE_USER)); | |
191 | } | |
192 | pmd = pmd_offset(pud, phys); | |
193 | BUG_ON(!pmd_none(*pmd)); | |
194 | set_pmd(pmd, __pmd(phys | pgprot_val(prot))); | |
195 | } | |
196 | } | |
197 | ||
198 | void __init init_extra_mapping_wb(unsigned long phys, unsigned long size) | |
199 | { | |
200 | __init_extra_mapping(phys, size, PAGE_KERNEL_LARGE); | |
201 | } | |
202 | ||
203 | void __init init_extra_mapping_uc(unsigned long phys, unsigned long size) | |
204 | { | |
205 | __init_extra_mapping(phys, size, PAGE_KERNEL_LARGE_NOCACHE); | |
206 | } | |
207 | ||
31eedd82 | 208 | /* |
88f3aec7 IM |
209 | * The head.S code sets up the kernel high mapping: |
210 | * | |
211 | * from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text) | |
31eedd82 TG |
212 | * |
213 | * phys_addr holds the negative offset to the kernel, which is added | |
214 | * to the compile time generated pmds. This results in invalid pmds up | |
215 | * to the point where we hit the physaddr 0 mapping. | |
216 | * | |
217 | * We limit the mappings to the region from _text to _end. _end is | |
218 | * rounded up to the 2MB boundary. This catches the invalid pmds as | |
219 | * well, as they are located before _text: | |
220 | */ | |
221 | void __init cleanup_highmap(void) | |
222 | { | |
223 | unsigned long vaddr = __START_KERNEL_map; | |
224 | unsigned long end = round_up((unsigned long)_end, PMD_SIZE) - 1; | |
225 | pmd_t *pmd = level2_kernel_pgt; | |
226 | pmd_t *last_pmd = pmd + PTRS_PER_PMD; | |
227 | ||
228 | for (; pmd < last_pmd; pmd++, vaddr += PMD_SIZE) { | |
2884f110 | 229 | if (pmd_none(*pmd)) |
31eedd82 TG |
230 | continue; |
231 | if (vaddr < (unsigned long) _text || vaddr > end) | |
232 | set_pmd(pmd, __pmd(0)); | |
233 | } | |
234 | } | |
235 | ||
75175278 AK |
236 | static unsigned long __initdata table_start; |
237 | static unsigned long __meminitdata table_end; | |
d86623a0 | 238 | static unsigned long __meminitdata table_top; |
1da177e4 | 239 | |
dafe41ee | 240 | static __meminit void *alloc_low_page(unsigned long *phys) |
14a62c34 | 241 | { |
dafe41ee | 242 | unsigned long pfn = table_end++; |
1da177e4 LT |
243 | void *adr; |
244 | ||
44df75e6 MT |
245 | if (after_bootmem) { |
246 | adr = (void *)get_zeroed_page(GFP_ATOMIC); | |
247 | *phys = __pa(adr); | |
14a62c34 | 248 | |
44df75e6 MT |
249 | return adr; |
250 | } | |
251 | ||
d86623a0 | 252 | if (pfn >= table_top) |
14a62c34 | 253 | panic("alloc_low_page: ran out of memory"); |
dafe41ee VG |
254 | |
255 | adr = early_ioremap(pfn * PAGE_SIZE, PAGE_SIZE); | |
44df75e6 | 256 | memset(adr, 0, PAGE_SIZE); |
dafe41ee VG |
257 | *phys = pfn * PAGE_SIZE; |
258 | return adr; | |
259 | } | |
1da177e4 | 260 | |
dafe41ee | 261 | static __meminit void unmap_low_page(void *adr) |
14a62c34 | 262 | { |
44df75e6 MT |
263 | if (after_bootmem) |
264 | return; | |
265 | ||
dafe41ee | 266 | early_iounmap(adr, PAGE_SIZE); |
14a62c34 | 267 | } |
1da177e4 | 268 | |
7b16eb89 | 269 | static unsigned long __meminit |
4f9c11dd JF |
270 | phys_pte_init(pte_t *pte_page, unsigned long addr, unsigned long end) |
271 | { | |
272 | unsigned pages = 0; | |
7b16eb89 | 273 | unsigned long last_map_addr = end; |
4f9c11dd | 274 | int i; |
7b16eb89 | 275 | |
4f9c11dd JF |
276 | pte_t *pte = pte_page + pte_index(addr); |
277 | ||
278 | for(i = pte_index(addr); i < PTRS_PER_PTE; i++, addr += PAGE_SIZE, pte++) { | |
279 | ||
280 | if (addr >= end) { | |
281 | if (!after_bootmem) { | |
282 | for(; i < PTRS_PER_PTE; i++, pte++) | |
283 | set_pte(pte, __pte(0)); | |
284 | } | |
285 | break; | |
286 | } | |
287 | ||
288 | if (pte_val(*pte)) | |
289 | continue; | |
290 | ||
291 | if (0) | |
292 | printk(" pte=%p addr=%lx pte=%016lx\n", | |
293 | pte, addr, pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL).pte); | |
294 | set_pte(pte, pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL)); | |
7b16eb89 | 295 | last_map_addr = (addr & PAGE_MASK) + PAGE_SIZE; |
4f9c11dd JF |
296 | pages++; |
297 | } | |
298 | update_page_count(PG_LEVEL_4K, pages); | |
7b16eb89 YL |
299 | |
300 | return last_map_addr; | |
4f9c11dd JF |
301 | } |
302 | ||
7b16eb89 | 303 | static unsigned long __meminit |
4f9c11dd JF |
304 | phys_pte_update(pmd_t *pmd, unsigned long address, unsigned long end) |
305 | { | |
306 | pte_t *pte = (pte_t *)pmd_page_vaddr(*pmd); | |
307 | ||
7b16eb89 | 308 | return phys_pte_init(pte, address, end); |
4f9c11dd JF |
309 | } |
310 | ||
cc615032 | 311 | static unsigned long __meminit |
b50efd2a YL |
312 | phys_pmd_init(pmd_t *pmd_page, unsigned long address, unsigned long end, |
313 | unsigned long page_size_mask) | |
44df75e6 | 314 | { |
ce0c0e50 | 315 | unsigned long pages = 0; |
7b16eb89 | 316 | unsigned long last_map_addr = end; |
a06de630 | 317 | unsigned long start = address; |
ce0c0e50 | 318 | |
6ad91658 | 319 | int i = pmd_index(address); |
44df75e6 | 320 | |
6ad91658 | 321 | for (; i < PTRS_PER_PMD; i++, address += PMD_SIZE) { |
4f9c11dd | 322 | unsigned long pte_phys; |
6ad91658 | 323 | pmd_t *pmd = pmd_page + pmd_index(address); |
4f9c11dd | 324 | pte_t *pte; |
44df75e6 | 325 | |
5f51e139 | 326 | if (address >= end) { |
14a62c34 | 327 | if (!after_bootmem) { |
5f51e139 JB |
328 | for (; i < PTRS_PER_PMD; i++, pmd++) |
329 | set_pmd(pmd, __pmd(0)); | |
14a62c34 | 330 | } |
44df75e6 MT |
331 | break; |
332 | } | |
6ad91658 | 333 | |
4f9c11dd | 334 | if (pmd_val(*pmd)) { |
22b45144 | 335 | if (!pmd_large(*pmd)) |
7b16eb89 YL |
336 | last_map_addr = phys_pte_update(pmd, address, |
337 | end); | |
a06de630 HD |
338 | /* Count entries we're using from level2_ident_pgt */ |
339 | if (start == 0) | |
340 | pages++; | |
4f9c11dd JF |
341 | continue; |
342 | } | |
343 | ||
b50efd2a | 344 | if (page_size_mask & (1<<PG_LEVEL_2M)) { |
4f9c11dd JF |
345 | pages++; |
346 | set_pte((pte_t *)pmd, | |
347 | pfn_pte(address >> PAGE_SHIFT, PAGE_KERNEL_LARGE)); | |
7b16eb89 | 348 | last_map_addr = (address & PMD_MASK) + PMD_SIZE; |
6ad91658 | 349 | continue; |
4f9c11dd | 350 | } |
6ad91658 | 351 | |
4f9c11dd | 352 | pte = alloc_low_page(&pte_phys); |
7b16eb89 | 353 | last_map_addr = phys_pte_init(pte, address, end); |
4f9c11dd JF |
354 | unmap_low_page(pte); |
355 | ||
356 | pmd_populate_kernel(&init_mm, pmd, __va(pte_phys)); | |
44df75e6 | 357 | } |
ce0c0e50 | 358 | update_page_count(PG_LEVEL_2M, pages); |
7b16eb89 | 359 | return last_map_addr; |
44df75e6 MT |
360 | } |
361 | ||
cc615032 | 362 | static unsigned long __meminit |
b50efd2a YL |
363 | phys_pmd_update(pud_t *pud, unsigned long address, unsigned long end, |
364 | unsigned long page_size_mask) | |
44df75e6 | 365 | { |
14a62c34 | 366 | pmd_t *pmd = pmd_offset(pud, 0); |
cc615032 AK |
367 | unsigned long last_map_addr; |
368 | ||
6ad91658 | 369 | spin_lock(&init_mm.page_table_lock); |
b50efd2a | 370 | last_map_addr = phys_pmd_init(pmd, address, end, page_size_mask); |
6ad91658 KM |
371 | spin_unlock(&init_mm.page_table_lock); |
372 | __flush_tlb_all(); | |
cc615032 | 373 | return last_map_addr; |
44df75e6 MT |
374 | } |
375 | ||
cc615032 | 376 | static unsigned long __meminit |
b50efd2a YL |
377 | phys_pud_init(pud_t *pud_page, unsigned long addr, unsigned long end, |
378 | unsigned long page_size_mask) | |
14a62c34 | 379 | { |
ce0c0e50 | 380 | unsigned long pages = 0; |
cc615032 | 381 | unsigned long last_map_addr = end; |
6ad91658 | 382 | int i = pud_index(addr); |
44df75e6 | 383 | |
14a62c34 | 384 | for (; i < PTRS_PER_PUD; i++, addr = (addr & PUD_MASK) + PUD_SIZE) { |
6ad91658 KM |
385 | unsigned long pmd_phys; |
386 | pud_t *pud = pud_page + pud_index(addr); | |
1da177e4 LT |
387 | pmd_t *pmd; |
388 | ||
6ad91658 | 389 | if (addr >= end) |
1da177e4 | 390 | break; |
1da177e4 | 391 | |
14a62c34 TG |
392 | if (!after_bootmem && |
393 | !e820_any_mapped(addr, addr+PUD_SIZE, 0)) { | |
394 | set_pud(pud, __pud(0)); | |
1da177e4 | 395 | continue; |
14a62c34 | 396 | } |
1da177e4 | 397 | |
6ad91658 | 398 | if (pud_val(*pud)) { |
ef925766 | 399 | if (!pud_large(*pud)) |
b50efd2a YL |
400 | last_map_addr = phys_pmd_update(pud, addr, end, |
401 | page_size_mask); | |
ef925766 AK |
402 | continue; |
403 | } | |
404 | ||
b50efd2a | 405 | if (page_size_mask & (1<<PG_LEVEL_1G)) { |
ce0c0e50 | 406 | pages++; |
ef925766 AK |
407 | set_pte((pte_t *)pud, |
408 | pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL_LARGE)); | |
cc615032 | 409 | last_map_addr = (addr & PUD_MASK) + PUD_SIZE; |
6ad91658 KM |
410 | continue; |
411 | } | |
412 | ||
dafe41ee | 413 | pmd = alloc_low_page(&pmd_phys); |
14a62c34 | 414 | |
44df75e6 | 415 | spin_lock(&init_mm.page_table_lock); |
b50efd2a | 416 | last_map_addr = phys_pmd_init(pmd, addr, end, page_size_mask); |
4f9c11dd JF |
417 | unmap_low_page(pmd); |
418 | pud_populate(&init_mm, pud, __va(pmd_phys)); | |
44df75e6 | 419 | spin_unlock(&init_mm.page_table_lock); |
14a62c34 | 420 | |
1da177e4 | 421 | } |
1a2b4412 | 422 | __flush_tlb_all(); |
ce0c0e50 | 423 | update_page_count(PG_LEVEL_1G, pages); |
cc615032 | 424 | |
1a0db38e | 425 | return last_map_addr; |
14a62c34 | 426 | } |
1da177e4 | 427 | |
4f9c11dd | 428 | static unsigned long __meminit |
b50efd2a YL |
429 | phys_pud_update(pgd_t *pgd, unsigned long addr, unsigned long end, |
430 | unsigned long page_size_mask) | |
4f9c11dd JF |
431 | { |
432 | pud_t *pud; | |
433 | ||
434 | pud = (pud_t *)pgd_page_vaddr(*pgd); | |
435 | ||
b50efd2a | 436 | return phys_pud_init(pud, addr, end, page_size_mask); |
4f9c11dd JF |
437 | } |
438 | ||
1da177e4 LT |
439 | static void __init find_early_table_space(unsigned long end) |
440 | { | |
c2e6d65b | 441 | unsigned long puds, pmds, ptes, tables, start; |
1da177e4 LT |
442 | |
443 | puds = (end + PUD_SIZE - 1) >> PUD_SHIFT; | |
ef925766 | 444 | tables = round_up(puds * sizeof(pud_t), PAGE_SIZE); |
c2e6d65b YL |
445 | if (direct_gbpages) { |
446 | unsigned long extra; | |
447 | extra = end - ((end>>PUD_SHIFT) << PUD_SHIFT); | |
448 | pmds = (extra + PMD_SIZE - 1) >> PMD_SHIFT; | |
449 | } else | |
450 | pmds = (end + PMD_SIZE - 1) >> PMD_SHIFT; | |
451 | tables += round_up(pmds * sizeof(pmd_t), PAGE_SIZE); | |
452 | ||
453 | if (cpu_has_pse) { | |
454 | unsigned long extra; | |
455 | extra = end - ((end>>PMD_SHIFT) << PMD_SHIFT); | |
456 | ptes = (extra + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
457 | } else | |
458 | ptes = (end + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
459 | tables += round_up(ptes * sizeof(pte_t), PAGE_SIZE); | |
1da177e4 | 460 | |
14a62c34 TG |
461 | /* |
462 | * RED-PEN putting page tables only on node 0 could | |
463 | * cause a hotspot and fill up ZONE_DMA. The page tables | |
464 | * need roughly 0.5KB per GB. | |
465 | */ | |
466 | start = 0x8000; | |
24a5da73 | 467 | table_start = find_e820_area(start, end, tables, PAGE_SIZE); |
1da177e4 LT |
468 | if (table_start == -1UL) |
469 | panic("Cannot find space for the kernel page tables"); | |
470 | ||
471 | table_start >>= PAGE_SHIFT; | |
472 | table_end = table_start; | |
d86623a0 | 473 | table_top = table_start + (tables >> PAGE_SHIFT); |
44df75e6 | 474 | |
d86623a0 YL |
475 | printk(KERN_DEBUG "kernel direct mapping tables up to %lx @ %lx-%lx\n", |
476 | end, table_start << PAGE_SHIFT, table_top << PAGE_SHIFT); | |
1da177e4 LT |
477 | } |
478 | ||
ef925766 AK |
479 | static void __init init_gbpages(void) |
480 | { | |
481 | if (direct_gbpages && cpu_has_gbpages) | |
482 | printk(KERN_INFO "Using GB pages for direct mapping\n"); | |
483 | else | |
484 | direct_gbpages = 0; | |
485 | } | |
486 | ||
b50efd2a YL |
487 | static unsigned long __init kernel_physical_mapping_init(unsigned long start, |
488 | unsigned long end, | |
489 | unsigned long page_size_mask) | |
14a62c34 | 490 | { |
1da177e4 | 491 | |
b50efd2a | 492 | unsigned long next, last_map_addr = end; |
1da177e4 LT |
493 | |
494 | start = (unsigned long)__va(start); | |
495 | end = (unsigned long)__va(end); | |
496 | ||
497 | for (; start < end; start = next) { | |
44df75e6 | 498 | pgd_t *pgd = pgd_offset_k(start); |
14a62c34 | 499 | unsigned long pud_phys; |
44df75e6 MT |
500 | pud_t *pud; |
501 | ||
e22146e6 | 502 | next = (start + PGDIR_SIZE) & PGDIR_MASK; |
4f9c11dd JF |
503 | if (next > end) |
504 | next = end; | |
505 | ||
506 | if (pgd_val(*pgd)) { | |
b50efd2a YL |
507 | last_map_addr = phys_pud_update(pgd, __pa(start), |
508 | __pa(end), page_size_mask); | |
4f9c11dd JF |
509 | continue; |
510 | } | |
511 | ||
44df75e6 | 512 | if (after_bootmem) |
d2ae5b5f | 513 | pud = pud_offset(pgd, start & PGDIR_MASK); |
44df75e6 | 514 | else |
dafe41ee | 515 | pud = alloc_low_page(&pud_phys); |
44df75e6 | 516 | |
b50efd2a YL |
517 | last_map_addr = phys_pud_init(pud, __pa(start), __pa(next), |
518 | page_size_mask); | |
4f9c11dd | 519 | unmap_low_page(pud); |
574977a2 JF |
520 | pgd_populate(&init_mm, pgd_offset_k(start), |
521 | __va(pud_phys)); | |
14a62c34 | 522 | } |
1da177e4 | 523 | |
b50efd2a YL |
524 | return last_map_addr; |
525 | } | |
7b16eb89 YL |
526 | |
527 | struct map_range { | |
528 | unsigned long start; | |
529 | unsigned long end; | |
530 | unsigned page_size_mask; | |
531 | }; | |
532 | ||
533 | #define NR_RANGE_MR 5 | |
534 | ||
535 | static int save_mr(struct map_range *mr, int nr_range, | |
536 | unsigned long start_pfn, unsigned long end_pfn, | |
537 | unsigned long page_size_mask) | |
538 | { | |
539 | ||
540 | if (start_pfn < end_pfn) { | |
541 | if (nr_range >= NR_RANGE_MR) | |
542 | panic("run out of range for init_memory_mapping\n"); | |
543 | mr[nr_range].start = start_pfn<<PAGE_SHIFT; | |
544 | mr[nr_range].end = end_pfn<<PAGE_SHIFT; | |
545 | mr[nr_range].page_size_mask = page_size_mask; | |
546 | nr_range++; | |
547 | } | |
548 | ||
549 | return nr_range; | |
550 | } | |
551 | ||
b50efd2a YL |
552 | /* |
553 | * Setup the direct mapping of the physical memory at PAGE_OFFSET. | |
554 | * This runs before bootmem is initialized and gets pages directly from | |
555 | * the physical memory. To access them they are temporarily mapped. | |
556 | */ | |
557 | unsigned long __init_refok init_memory_mapping(unsigned long start, | |
558 | unsigned long end) | |
559 | { | |
7b16eb89 | 560 | unsigned long last_map_addr = 0; |
b50efd2a | 561 | unsigned long page_size_mask = 0; |
c2e6d65b | 562 | unsigned long start_pfn, end_pfn; |
b50efd2a | 563 | |
7b16eb89 YL |
564 | struct map_range mr[NR_RANGE_MR]; |
565 | int nr_range, i; | |
566 | ||
b50efd2a YL |
567 | printk(KERN_INFO "init_memory_mapping\n"); |
568 | ||
569 | /* | |
570 | * Find space for the kernel direct mapping tables. | |
571 | * | |
572 | * Later we should allocate these tables in the local node of the | |
573 | * memory mapped. Unfortunately this is done currently before the | |
574 | * nodes are discovered. | |
575 | */ | |
7b16eb89 | 576 | if (!after_bootmem) |
b50efd2a | 577 | init_gbpages(); |
b50efd2a YL |
578 | |
579 | if (direct_gbpages) | |
580 | page_size_mask |= 1 << PG_LEVEL_1G; | |
581 | if (cpu_has_pse) | |
582 | page_size_mask |= 1 << PG_LEVEL_2M; | |
583 | ||
7b16eb89 YL |
584 | memset(mr, 0, sizeof(mr)); |
585 | nr_range = 0; | |
586 | ||
587 | /* head if not big page alignment ?*/ | |
c2e6d65b YL |
588 | start_pfn = start >> PAGE_SHIFT; |
589 | end_pfn = ((start + (PMD_SIZE - 1)) >> PMD_SHIFT) | |
590 | << (PMD_SHIFT - PAGE_SHIFT); | |
7b16eb89 | 591 | nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0); |
c2e6d65b YL |
592 | |
593 | /* big page (2M) range*/ | |
594 | start_pfn = ((start + (PMD_SIZE - 1))>>PMD_SHIFT) | |
595 | << (PMD_SHIFT - PAGE_SHIFT); | |
596 | end_pfn = ((start + (PUD_SIZE - 1))>>PUD_SHIFT) | |
597 | << (PUD_SHIFT - PAGE_SHIFT); | |
598 | if (end_pfn > ((end>>PUD_SHIFT)<<(PUD_SHIFT - PAGE_SHIFT))) | |
599 | end_pfn = ((end>>PUD_SHIFT)<<(PUD_SHIFT - PAGE_SHIFT)); | |
7b16eb89 YL |
600 | nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, |
601 | page_size_mask & (1<<PG_LEVEL_2M)); | |
c2e6d65b YL |
602 | |
603 | /* big page (1G) range */ | |
604 | start_pfn = end_pfn; | |
605 | end_pfn = (end>>PUD_SHIFT) << (PUD_SHIFT - PAGE_SHIFT); | |
7b16eb89 YL |
606 | nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, |
607 | page_size_mask & | |
608 | ((1<<PG_LEVEL_2M)|(1<<PG_LEVEL_1G))); | |
c2e6d65b YL |
609 | |
610 | /* tail is not big page (1G) alignment */ | |
611 | start_pfn = end_pfn; | |
612 | end_pfn = (end>>PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT); | |
7b16eb89 YL |
613 | nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, |
614 | page_size_mask & (1<<PG_LEVEL_2M)); | |
615 | ||
c2e6d65b YL |
616 | /* tail is not big page (2M) alignment */ |
617 | start_pfn = end_pfn; | |
618 | end_pfn = end>>PAGE_SHIFT; | |
7b16eb89 YL |
619 | nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0); |
620 | ||
9958e810 YL |
621 | /* try to merge same page size and continuous */ |
622 | for (i = 0; nr_range > 1 && i < nr_range - 1; i++) { | |
623 | unsigned long old_start; | |
624 | if (mr[i].end != mr[i+1].start || | |
625 | mr[i].page_size_mask != mr[i+1].page_size_mask) | |
626 | continue; | |
627 | /* move it */ | |
628 | old_start = mr[i].start; | |
629 | memmove(&mr[i], &mr[i+1], | |
630 | (nr_range - 1 - i) * sizeof (struct map_range)); | |
631 | mr[i].start = old_start; | |
632 | nr_range--; | |
633 | } | |
634 | ||
7b16eb89 YL |
635 | for (i = 0; i < nr_range; i++) |
636 | printk(KERN_DEBUG " %010lx - %010lx page %s\n", | |
637 | mr[i].start, mr[i].end, | |
638 | (mr[i].page_size_mask & (1<<PG_LEVEL_1G))?"1G":( | |
639 | (mr[i].page_size_mask & (1<<PG_LEVEL_2M))?"2M":"4k")); | |
640 | ||
641 | if (!after_bootmem) | |
642 | find_early_table_space(end); | |
643 | ||
644 | for (i = 0; i < nr_range; i++) | |
c2e6d65b | 645 | last_map_addr = kernel_physical_mapping_init( |
7b16eb89 YL |
646 | mr[i].start, mr[i].end, |
647 | mr[i].page_size_mask); | |
b50efd2a | 648 | |
44df75e6 | 649 | if (!after_bootmem) |
f51c9452 | 650 | mmu_cr4_features = read_cr4(); |
1da177e4 | 651 | __flush_tlb_all(); |
75175278 | 652 | |
b50efd2a | 653 | if (!after_bootmem && table_end > table_start) |
24a5da73 YL |
654 | reserve_early(table_start << PAGE_SHIFT, |
655 | table_end << PAGE_SHIFT, "PGTABLE"); | |
272b9cad | 656 | |
b50efd2a YL |
657 | printk(KERN_INFO "last_map_addr: %lx end: %lx\n", |
658 | last_map_addr, end); | |
659 | ||
272b9cad | 660 | if (!after_bootmem) |
b50efd2a | 661 | early_memtest(start, end); |
cc615032 | 662 | |
1a0db38e | 663 | return last_map_addr >> PAGE_SHIFT; |
1da177e4 LT |
664 | } |
665 | ||
2b97690f | 666 | #ifndef CONFIG_NUMA |
1f75d7e3 YL |
667 | void __init initmem_init(unsigned long start_pfn, unsigned long end_pfn) |
668 | { | |
669 | unsigned long bootmap_size, bootmap; | |
670 | ||
671 | bootmap_size = bootmem_bootmap_pages(end_pfn)<<PAGE_SHIFT; | |
672 | bootmap = find_e820_area(0, end_pfn<<PAGE_SHIFT, bootmap_size, | |
673 | PAGE_SIZE); | |
674 | if (bootmap == -1L) | |
675 | panic("Cannot find bootmem map of size %ld\n", bootmap_size); | |
346cafec YL |
676 | /* don't touch min_low_pfn */ |
677 | bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap >> PAGE_SHIFT, | |
678 | 0, end_pfn); | |
1f75d7e3 YL |
679 | e820_register_active_regions(0, start_pfn, end_pfn); |
680 | free_bootmem_with_active_regions(0, end_pfn); | |
681 | early_res_to_bootmem(0, end_pfn<<PAGE_SHIFT); | |
682 | reserve_bootmem(bootmap, bootmap_size, BOOTMEM_DEFAULT); | |
683 | } | |
684 | ||
1da177e4 LT |
685 | void __init paging_init(void) |
686 | { | |
6391af17 | 687 | unsigned long max_zone_pfns[MAX_NR_ZONES]; |
14a62c34 | 688 | |
6391af17 MG |
689 | memset(max_zone_pfns, 0, sizeof(max_zone_pfns)); |
690 | max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN; | |
691 | max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN; | |
c987d12f | 692 | max_zone_pfns[ZONE_NORMAL] = max_pfn; |
6391af17 | 693 | |
c987d12f | 694 | memory_present(0, 0, max_pfn); |
44df75e6 | 695 | sparse_init(); |
5cb248ab | 696 | free_area_init_nodes(max_zone_pfns); |
1da177e4 LT |
697 | } |
698 | #endif | |
699 | ||
44df75e6 MT |
700 | /* |
701 | * Memory hotplug specific functions | |
44df75e6 | 702 | */ |
bc02af93 | 703 | #ifdef CONFIG_MEMORY_HOTPLUG |
9d99aaa3 AK |
704 | /* |
705 | * Memory is added always to NORMAL zone. This means you will never get | |
706 | * additional DMA/DMA32 memory. | |
707 | */ | |
bc02af93 | 708 | int arch_add_memory(int nid, u64 start, u64 size) |
44df75e6 | 709 | { |
bc02af93 | 710 | struct pglist_data *pgdat = NODE_DATA(nid); |
776ed98b | 711 | struct zone *zone = pgdat->node_zones + ZONE_NORMAL; |
cc615032 | 712 | unsigned long last_mapped_pfn, start_pfn = start >> PAGE_SHIFT; |
44df75e6 MT |
713 | unsigned long nr_pages = size >> PAGE_SHIFT; |
714 | int ret; | |
715 | ||
cc615032 AK |
716 | last_mapped_pfn = init_memory_mapping(start, start + size-1); |
717 | if (last_mapped_pfn > max_pfn_mapped) | |
718 | max_pfn_mapped = last_mapped_pfn; | |
45e0b78b | 719 | |
44df75e6 | 720 | ret = __add_pages(zone, start_pfn, nr_pages); |
10f22dde | 721 | WARN_ON(1); |
44df75e6 | 722 | |
44df75e6 | 723 | return ret; |
44df75e6 | 724 | } |
bc02af93 | 725 | EXPORT_SYMBOL_GPL(arch_add_memory); |
44df75e6 | 726 | |
8243229f | 727 | #if !defined(CONFIG_ACPI_NUMA) && defined(CONFIG_NUMA) |
4942e998 KM |
728 | int memory_add_physaddr_to_nid(u64 start) |
729 | { | |
730 | return 0; | |
731 | } | |
8c2676a5 | 732 | EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid); |
4942e998 KM |
733 | #endif |
734 | ||
45e0b78b KM |
735 | #endif /* CONFIG_MEMORY_HOTPLUG */ |
736 | ||
ae531c26 AV |
737 | /* |
738 | * devmem_is_allowed() checks to see if /dev/mem access to a certain address | |
739 | * is valid. The argument is a physical page number. | |
740 | * | |
741 | * | |
742 | * On x86, access has to be given to the first megabyte of ram because that area | |
743 | * contains bios code and data regions used by X and dosemu and similar apps. | |
744 | * Access has to be given to non-kernel-ram areas as well, these contain the PCI | |
745 | * mmio resources as well as potential bios/acpi data regions. | |
746 | */ | |
747 | int devmem_is_allowed(unsigned long pagenr) | |
748 | { | |
749 | if (pagenr <= 256) | |
750 | return 1; | |
751 | if (!page_is_ram(pagenr)) | |
752 | return 1; | |
753 | return 0; | |
754 | } | |
755 | ||
756 | ||
14a62c34 TG |
757 | static struct kcore_list kcore_mem, kcore_vmalloc, kcore_kernel, |
758 | kcore_modules, kcore_vsyscall; | |
1da177e4 LT |
759 | |
760 | void __init mem_init(void) | |
761 | { | |
0a43e4bf | 762 | long codesize, reservedpages, datasize, initsize; |
1da177e4 | 763 | |
0dc243ae | 764 | pci_iommu_alloc(); |
1da177e4 | 765 | |
48ddb154 | 766 | /* clear_bss() already clear the empty_zero_page */ |
1da177e4 LT |
767 | |
768 | reservedpages = 0; | |
769 | ||
770 | /* this will put all low memory onto the freelists */ | |
2b97690f | 771 | #ifdef CONFIG_NUMA |
0a43e4bf | 772 | totalram_pages = numa_free_all_bootmem(); |
1da177e4 | 773 | #else |
0a43e4bf | 774 | totalram_pages = free_all_bootmem(); |
1da177e4 | 775 | #endif |
c987d12f YL |
776 | reservedpages = max_pfn - totalram_pages - |
777 | absent_pages_in_range(0, max_pfn); | |
1da177e4 LT |
778 | after_bootmem = 1; |
779 | ||
780 | codesize = (unsigned long) &_etext - (unsigned long) &_text; | |
781 | datasize = (unsigned long) &_edata - (unsigned long) &_etext; | |
782 | initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin; | |
783 | ||
784 | /* Register memory areas for /proc/kcore */ | |
14a62c34 TG |
785 | kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT); |
786 | kclist_add(&kcore_vmalloc, (void *)VMALLOC_START, | |
1da177e4 LT |
787 | VMALLOC_END-VMALLOC_START); |
788 | kclist_add(&kcore_kernel, &_stext, _end - _stext); | |
789 | kclist_add(&kcore_modules, (void *)MODULES_VADDR, MODULES_LEN); | |
14a62c34 | 790 | kclist_add(&kcore_vsyscall, (void *)VSYSCALL_START, |
1da177e4 LT |
791 | VSYSCALL_END - VSYSCALL_START); |
792 | ||
10f22dde | 793 | printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, " |
14a62c34 | 794 | "%ldk reserved, %ldk data, %ldk init)\n", |
1da177e4 | 795 | (unsigned long) nr_free_pages() << (PAGE_SHIFT-10), |
c987d12f | 796 | max_pfn << (PAGE_SHIFT-10), |
1da177e4 LT |
797 | codesize >> 10, |
798 | reservedpages << (PAGE_SHIFT-10), | |
799 | datasize >> 10, | |
800 | initsize >> 10); | |
76ebd054 TG |
801 | |
802 | cpa_init(); | |
1da177e4 LT |
803 | } |
804 | ||
d167a518 | 805 | void free_init_pages(char *what, unsigned long begin, unsigned long end) |
1da177e4 | 806 | { |
bfc734b2 | 807 | unsigned long addr = begin; |
1da177e4 | 808 | |
bfc734b2 | 809 | if (addr >= end) |
d167a518 GH |
810 | return; |
811 | ||
ee01f112 IM |
812 | /* |
813 | * If debugging page accesses then do not free this memory but | |
814 | * mark them not present - any buggy init-section access will | |
815 | * create a kernel page fault: | |
816 | */ | |
817 | #ifdef CONFIG_DEBUG_PAGEALLOC | |
818 | printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n", | |
819 | begin, PAGE_ALIGN(end)); | |
820 | set_memory_np(begin, (end - begin) >> PAGE_SHIFT); | |
821 | #else | |
6fb14755 | 822 | printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10); |
14a62c34 | 823 | |
bfc734b2 | 824 | for (; addr < end; addr += PAGE_SIZE) { |
e3ebadd9 LT |
825 | ClearPageReserved(virt_to_page(addr)); |
826 | init_page_count(virt_to_page(addr)); | |
827 | memset((void *)(addr & ~(PAGE_SIZE-1)), | |
828 | POISON_FREE_INITMEM, PAGE_SIZE); | |
e3ebadd9 | 829 | free_page(addr); |
1da177e4 LT |
830 | totalram_pages++; |
831 | } | |
ee01f112 | 832 | #endif |
d167a518 GH |
833 | } |
834 | ||
835 | void free_initmem(void) | |
836 | { | |
d167a518 | 837 | free_init_pages("unused kernel memory", |
e3ebadd9 LT |
838 | (unsigned long)(&__init_begin), |
839 | (unsigned long)(&__init_end)); | |
1da177e4 LT |
840 | } |
841 | ||
67df197b | 842 | #ifdef CONFIG_DEBUG_RODATA |
edeed305 AV |
843 | const int rodata_test_data = 0xC3; |
844 | EXPORT_SYMBOL_GPL(rodata_test_data); | |
67df197b | 845 | |
67df197b AV |
846 | void mark_rodata_ro(void) |
847 | { | |
4e4eee0e | 848 | unsigned long start = PFN_ALIGN(_stext), end = PFN_ALIGN(__end_rodata); |
8f0f996e SR |
849 | unsigned long rodata_start = |
850 | ((unsigned long)__start_rodata + PAGE_SIZE - 1) & PAGE_MASK; | |
851 | ||
852 | #ifdef CONFIG_DYNAMIC_FTRACE | |
853 | /* Dynamic tracing modifies the kernel text section */ | |
854 | start = rodata_start; | |
855 | #endif | |
67df197b | 856 | |
6fb14755 | 857 | printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n", |
e3ebadd9 | 858 | (end - start) >> 10); |
984bb80d AV |
859 | set_memory_ro(start, (end - start) >> PAGE_SHIFT); |
860 | ||
861 | /* | |
862 | * The rodata section (but not the kernel text!) should also be | |
863 | * not-executable. | |
864 | */ | |
72b59d67 | 865 | set_memory_nx(rodata_start, (end - rodata_start) >> PAGE_SHIFT); |
67df197b | 866 | |
1a487252 AV |
867 | rodata_test(); |
868 | ||
0c42f392 | 869 | #ifdef CONFIG_CPA_DEBUG |
10f22dde | 870 | printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end); |
6d238cc4 | 871 | set_memory_rw(start, (end-start) >> PAGE_SHIFT); |
0c42f392 | 872 | |
10f22dde | 873 | printk(KERN_INFO "Testing CPA: again\n"); |
6d238cc4 | 874 | set_memory_ro(start, (end-start) >> PAGE_SHIFT); |
0c42f392 | 875 | #endif |
67df197b | 876 | } |
4e4eee0e | 877 | |
67df197b AV |
878 | #endif |
879 | ||
1da177e4 LT |
880 | #ifdef CONFIG_BLK_DEV_INITRD |
881 | void free_initrd_mem(unsigned long start, unsigned long end) | |
882 | { | |
e3ebadd9 | 883 | free_init_pages("initrd memory", start, end); |
1da177e4 LT |
884 | } |
885 | #endif | |
886 | ||
d2dbf343 YL |
887 | int __init reserve_bootmem_generic(unsigned long phys, unsigned long len, |
888 | int flags) | |
14a62c34 | 889 | { |
2b97690f | 890 | #ifdef CONFIG_NUMA |
8b3cd09e | 891 | int nid, next_nid; |
6a07a0ed | 892 | int ret; |
5e58a02a AK |
893 | #endif |
894 | unsigned long pfn = phys >> PAGE_SHIFT; | |
14a62c34 | 895 | |
c987d12f | 896 | if (pfn >= max_pfn) { |
14a62c34 TG |
897 | /* |
898 | * This can happen with kdump kernels when accessing | |
899 | * firmware tables: | |
900 | */ | |
67794292 | 901 | if (pfn < max_pfn_mapped) |
8b2ef1d7 | 902 | return -EFAULT; |
14a62c34 | 903 | |
6a07a0ed | 904 | printk(KERN_ERR "reserve_bootmem: illegal reserve %lx %lu\n", |
5e58a02a | 905 | phys, len); |
8b2ef1d7 | 906 | return -EFAULT; |
5e58a02a AK |
907 | } |
908 | ||
909 | /* Should check here against the e820 map to avoid double free */ | |
910 | #ifdef CONFIG_NUMA | |
8b3cd09e YL |
911 | nid = phys_to_nid(phys); |
912 | next_nid = phys_to_nid(phys + len - 1); | |
913 | if (nid == next_nid) | |
8b2ef1d7 | 914 | ret = reserve_bootmem_node(NODE_DATA(nid), phys, len, flags); |
8b3cd09e | 915 | else |
8b2ef1d7 BW |
916 | ret = reserve_bootmem(phys, len, flags); |
917 | ||
918 | if (ret != 0) | |
919 | return ret; | |
920 | ||
14a62c34 | 921 | #else |
72a7fe39 | 922 | reserve_bootmem(phys, len, BOOTMEM_DEFAULT); |
1da177e4 | 923 | #endif |
8b3cd09e | 924 | |
0e0b864e | 925 | if (phys+len <= MAX_DMA_PFN*PAGE_SIZE) { |
e18c6874 | 926 | dma_reserve += len / PAGE_SIZE; |
0e0b864e MG |
927 | set_dma_reserve(dma_reserve); |
928 | } | |
8b2ef1d7 BW |
929 | |
930 | return 0; | |
1da177e4 LT |
931 | } |
932 | ||
14a62c34 TG |
933 | int kern_addr_valid(unsigned long addr) |
934 | { | |
1da177e4 | 935 | unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT; |
14a62c34 TG |
936 | pgd_t *pgd; |
937 | pud_t *pud; | |
938 | pmd_t *pmd; | |
939 | pte_t *pte; | |
1da177e4 LT |
940 | |
941 | if (above != 0 && above != -1UL) | |
14a62c34 TG |
942 | return 0; |
943 | ||
1da177e4 LT |
944 | pgd = pgd_offset_k(addr); |
945 | if (pgd_none(*pgd)) | |
946 | return 0; | |
947 | ||
948 | pud = pud_offset(pgd, addr); | |
949 | if (pud_none(*pud)) | |
14a62c34 | 950 | return 0; |
1da177e4 LT |
951 | |
952 | pmd = pmd_offset(pud, addr); | |
953 | if (pmd_none(*pmd)) | |
954 | return 0; | |
14a62c34 | 955 | |
1da177e4 LT |
956 | if (pmd_large(*pmd)) |
957 | return pfn_valid(pmd_pfn(*pmd)); | |
958 | ||
959 | pte = pte_offset_kernel(pmd, addr); | |
960 | if (pte_none(*pte)) | |
961 | return 0; | |
14a62c34 | 962 | |
1da177e4 LT |
963 | return pfn_valid(pte_pfn(*pte)); |
964 | } | |
965 | ||
14a62c34 TG |
966 | /* |
967 | * A pseudo VMA to allow ptrace access for the vsyscall page. This only | |
968 | * covers the 64bit vsyscall page now. 32bit has a real VMA now and does | |
969 | * not need special handling anymore: | |
970 | */ | |
1da177e4 | 971 | static struct vm_area_struct gate_vma = { |
14a62c34 TG |
972 | .vm_start = VSYSCALL_START, |
973 | .vm_end = VSYSCALL_START + (VSYSCALL_MAPPED_PAGES * PAGE_SIZE), | |
974 | .vm_page_prot = PAGE_READONLY_EXEC, | |
975 | .vm_flags = VM_READ | VM_EXEC | |
1da177e4 LT |
976 | }; |
977 | ||
1da177e4 LT |
978 | struct vm_area_struct *get_gate_vma(struct task_struct *tsk) |
979 | { | |
980 | #ifdef CONFIG_IA32_EMULATION | |
1e014410 AK |
981 | if (test_tsk_thread_flag(tsk, TIF_IA32)) |
982 | return NULL; | |
1da177e4 LT |
983 | #endif |
984 | return &gate_vma; | |
985 | } | |
986 | ||
987 | int in_gate_area(struct task_struct *task, unsigned long addr) | |
988 | { | |
989 | struct vm_area_struct *vma = get_gate_vma(task); | |
14a62c34 | 990 | |
1e014410 AK |
991 | if (!vma) |
992 | return 0; | |
14a62c34 | 993 | |
1da177e4 LT |
994 | return (addr >= vma->vm_start) && (addr < vma->vm_end); |
995 | } | |
996 | ||
14a62c34 TG |
997 | /* |
998 | * Use this when you have no reliable task/vma, typically from interrupt | |
999 | * context. It is less reliable than using the task's vma and may give | |
1000 | * false positives: | |
1da177e4 LT |
1001 | */ |
1002 | int in_gate_area_no_task(unsigned long addr) | |
1003 | { | |
1e014410 | 1004 | return (addr >= VSYSCALL_START) && (addr < VSYSCALL_END); |
1da177e4 | 1005 | } |
2e1c49db | 1006 | |
2aae950b AK |
1007 | const char *arch_vma_name(struct vm_area_struct *vma) |
1008 | { | |
1009 | if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso) | |
1010 | return "[vdso]"; | |
1011 | if (vma == &gate_vma) | |
1012 | return "[vsyscall]"; | |
1013 | return NULL; | |
1014 | } | |
0889eba5 CL |
1015 | |
1016 | #ifdef CONFIG_SPARSEMEM_VMEMMAP | |
1017 | /* | |
1018 | * Initialise the sparsemem vmemmap using huge-pages at the PMD level. | |
1019 | */ | |
c2b91e2e YL |
1020 | static long __meminitdata addr_start, addr_end; |
1021 | static void __meminitdata *p_start, *p_end; | |
1022 | static int __meminitdata node_start; | |
1023 | ||
14a62c34 TG |
1024 | int __meminit |
1025 | vmemmap_populate(struct page *start_page, unsigned long size, int node) | |
0889eba5 CL |
1026 | { |
1027 | unsigned long addr = (unsigned long)start_page; | |
1028 | unsigned long end = (unsigned long)(start_page + size); | |
1029 | unsigned long next; | |
1030 | pgd_t *pgd; | |
1031 | pud_t *pud; | |
1032 | pmd_t *pmd; | |
1033 | ||
1034 | for (; addr < end; addr = next) { | |
7c934d39 | 1035 | void *p = NULL; |
0889eba5 CL |
1036 | |
1037 | pgd = vmemmap_pgd_populate(addr, node); | |
1038 | if (!pgd) | |
1039 | return -ENOMEM; | |
14a62c34 | 1040 | |
0889eba5 CL |
1041 | pud = vmemmap_pud_populate(pgd, addr, node); |
1042 | if (!pud) | |
1043 | return -ENOMEM; | |
1044 | ||
7c934d39 JF |
1045 | if (!cpu_has_pse) { |
1046 | next = (addr + PAGE_SIZE) & PAGE_MASK; | |
1047 | pmd = vmemmap_pmd_populate(pud, addr, node); | |
1048 | ||
1049 | if (!pmd) | |
1050 | return -ENOMEM; | |
1051 | ||
1052 | p = vmemmap_pte_populate(pmd, addr, node); | |
14a62c34 | 1053 | |
0889eba5 CL |
1054 | if (!p) |
1055 | return -ENOMEM; | |
1056 | ||
7c934d39 JF |
1057 | addr_end = addr + PAGE_SIZE; |
1058 | p_end = p + PAGE_SIZE; | |
14a62c34 | 1059 | } else { |
7c934d39 JF |
1060 | next = pmd_addr_end(addr, end); |
1061 | ||
1062 | pmd = pmd_offset(pud, addr); | |
1063 | if (pmd_none(*pmd)) { | |
1064 | pte_t entry; | |
1065 | ||
1066 | p = vmemmap_alloc_block(PMD_SIZE, node); | |
1067 | if (!p) | |
1068 | return -ENOMEM; | |
1069 | ||
1070 | entry = pfn_pte(__pa(p) >> PAGE_SHIFT, | |
1071 | PAGE_KERNEL_LARGE); | |
1072 | set_pmd(pmd, __pmd(pte_val(entry))); | |
1073 | ||
7c934d39 JF |
1074 | /* check to see if we have contiguous blocks */ |
1075 | if (p_end != p || node_start != node) { | |
1076 | if (p_start) | |
1077 | printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n", | |
1078 | addr_start, addr_end-1, p_start, p_end-1, node_start); | |
1079 | addr_start = addr; | |
1080 | node_start = node; | |
1081 | p_start = p; | |
1082 | } | |
49c980df YL |
1083 | |
1084 | addr_end = addr + PMD_SIZE; | |
1085 | p_end = p + PMD_SIZE; | |
7c934d39 JF |
1086 | } else |
1087 | vmemmap_verify((pte_t *)pmd, node, addr, next); | |
14a62c34 | 1088 | } |
7c934d39 | 1089 | |
0889eba5 | 1090 | } |
0889eba5 CL |
1091 | return 0; |
1092 | } | |
c2b91e2e YL |
1093 | |
1094 | void __meminit vmemmap_populate_print_last(void) | |
1095 | { | |
1096 | if (p_start) { | |
1097 | printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n", | |
1098 | addr_start, addr_end-1, p_start, p_end-1, node_start); | |
1099 | p_start = NULL; | |
1100 | p_end = NULL; | |
1101 | node_start = 0; | |
1102 | } | |
1103 | } | |
0889eba5 | 1104 | #endif |