[NETLINK]: w1_int.c: fix default netlink group
[linux-2.6/verdex.git] / arch / ppc64 / kernel / vdso.c
blob4777676365fe2a60917683cfa713b0f0b5728289
1 /*
2 * linux/arch/ppc64/kernel/vdso.c
4 * Copyright (C) 2004 Benjamin Herrenschmidt, IBM Corp.
5 * <benh@kernel.crashing.org>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
13 #include <linux/config.h>
14 #include <linux/module.h>
15 #include <linux/errno.h>
16 #include <linux/sched.h>
17 #include <linux/kernel.h>
18 #include <linux/mm.h>
19 #include <linux/smp.h>
20 #include <linux/smp_lock.h>
21 #include <linux/stddef.h>
22 #include <linux/unistd.h>
23 #include <linux/slab.h>
24 #include <linux/user.h>
25 #include <linux/elf.h>
26 #include <linux/security.h>
27 #include <linux/bootmem.h>
29 #include <asm/pgtable.h>
30 #include <asm/system.h>
31 #include <asm/processor.h>
32 #include <asm/mmu.h>
33 #include <asm/mmu_context.h>
34 #include <asm/machdep.h>
35 #include <asm/cputable.h>
36 #include <asm/sections.h>
37 #include <asm/vdso.h>
39 #undef DEBUG
41 #ifdef DEBUG
42 #define DBG(fmt...) printk(fmt)
43 #else
44 #define DBG(fmt...)
45 #endif
49 * The vDSOs themselves are here
51 extern char vdso64_start, vdso64_end;
52 extern char vdso32_start, vdso32_end;
54 static void *vdso64_kbase = &vdso64_start;
55 static void *vdso32_kbase = &vdso32_start;
57 unsigned int vdso64_pages;
58 unsigned int vdso32_pages;
60 /* Signal trampolines user addresses */
62 unsigned long vdso64_rt_sigtramp;
63 unsigned long vdso32_sigtramp;
64 unsigned long vdso32_rt_sigtramp;
66 /* Format of the patch table */
67 struct vdso_patch_def
69 u32 pvr_mask, pvr_value;
70 const char *gen_name;
71 const char *fix_name;
74 /* Table of functions to patch based on the CPU type/revision
76 * TODO: Improve by adding whole lists for each entry
78 static struct vdso_patch_def vdso_patches[] = {
80 0xffff0000, 0x003a0000, /* POWER5 */
81 "__kernel_sync_dicache", "__kernel_sync_dicache_p5"
84 0xffff0000, 0x003b0000, /* POWER5 */
85 "__kernel_sync_dicache", "__kernel_sync_dicache_p5"
90 * Some infos carried around for each of them during parsing at
91 * boot time.
93 struct lib32_elfinfo
95 Elf32_Ehdr *hdr; /* ptr to ELF */
96 Elf32_Sym *dynsym; /* ptr to .dynsym section */
97 unsigned long dynsymsize; /* size of .dynsym section */
98 char *dynstr; /* ptr to .dynstr section */
99 unsigned long text; /* offset of .text section in .so */
102 struct lib64_elfinfo
104 Elf64_Ehdr *hdr;
105 Elf64_Sym *dynsym;
106 unsigned long dynsymsize;
107 char *dynstr;
108 unsigned long text;
112 #ifdef __DEBUG
113 static void dump_one_vdso_page(struct page *pg, struct page *upg)
115 printk("kpg: %p (c:%d,f:%08lx)", __va(page_to_pfn(pg) << PAGE_SHIFT),
116 page_count(pg),
117 pg->flags);
118 if (upg/* && pg != upg*/) {
119 printk(" upg: %p (c:%d,f:%08lx)", __va(page_to_pfn(upg) << PAGE_SHIFT),
120 page_count(upg),
121 upg->flags);
123 printk("\n");
126 static void dump_vdso_pages(struct vm_area_struct * vma)
128 int i;
130 if (!vma || test_thread_flag(TIF_32BIT)) {
131 printk("vDSO32 @ %016lx:\n", (unsigned long)vdso32_kbase);
132 for (i=0; i<vdso32_pages; i++) {
133 struct page *pg = virt_to_page(vdso32_kbase + i*PAGE_SIZE);
134 struct page *upg = (vma && vma->vm_mm) ?
135 follow_page(vma->vm_mm, vma->vm_start + i*PAGE_SIZE, 0)
136 : NULL;
137 dump_one_vdso_page(pg, upg);
140 if (!vma || !test_thread_flag(TIF_32BIT)) {
141 printk("vDSO64 @ %016lx:\n", (unsigned long)vdso64_kbase);
142 for (i=0; i<vdso64_pages; i++) {
143 struct page *pg = virt_to_page(vdso64_kbase + i*PAGE_SIZE);
144 struct page *upg = (vma && vma->vm_mm) ?
145 follow_page(vma->vm_mm, vma->vm_start + i*PAGE_SIZE, 0)
146 : NULL;
147 dump_one_vdso_page(pg, upg);
151 #endif /* DEBUG */
154 * Keep a dummy vma_close for now, it will prevent VMA merging.
156 static void vdso_vma_close(struct vm_area_struct * vma)
161 * Our nopage() function, maps in the actual vDSO kernel pages, they will
162 * be mapped read-only by do_no_page(), and eventually COW'ed, either
163 * right away for an initial write access, or by do_wp_page().
165 static struct page * vdso_vma_nopage(struct vm_area_struct * vma,
166 unsigned long address, int *type)
168 unsigned long offset = address - vma->vm_start;
169 struct page *pg;
170 void *vbase = test_thread_flag(TIF_32BIT) ? vdso32_kbase : vdso64_kbase;
172 DBG("vdso_vma_nopage(current: %s, address: %016lx, off: %lx)\n",
173 current->comm, address, offset);
175 if (address < vma->vm_start || address > vma->vm_end)
176 return NOPAGE_SIGBUS;
179 * Last page is systemcfg, special handling here, no get_page() a
180 * this is a reserved page
182 if ((vma->vm_end - address) <= PAGE_SIZE)
183 return virt_to_page(systemcfg);
185 pg = virt_to_page(vbase + offset);
186 get_page(pg);
187 DBG(" ->page count: %d\n", page_count(pg));
189 return pg;
192 static struct vm_operations_struct vdso_vmops = {
193 .close = vdso_vma_close,
194 .nopage = vdso_vma_nopage,
198 * This is called from binfmt_elf, we create the special vma for the
199 * vDSO and insert it into the mm struct tree
201 int arch_setup_additional_pages(struct linux_binprm *bprm, int executable_stack)
203 struct mm_struct *mm = current->mm;
204 struct vm_area_struct *vma;
205 unsigned long vdso_pages;
206 unsigned long vdso_base;
208 if (test_thread_flag(TIF_32BIT)) {
209 vdso_pages = vdso32_pages;
210 vdso_base = VDSO32_MBASE;
211 } else {
212 vdso_pages = vdso64_pages;
213 vdso_base = VDSO64_MBASE;
216 current->thread.vdso_base = 0;
218 /* vDSO has a problem and was disabled, just don't "enable" it for the
219 * process
221 if (vdso_pages == 0)
222 return 0;
224 vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
225 if (vma == NULL)
226 return -ENOMEM;
227 if (security_vm_enough_memory(vdso_pages)) {
228 kmem_cache_free(vm_area_cachep, vma);
229 return -ENOMEM;
231 memset(vma, 0, sizeof(*vma));
234 * pick a base address for the vDSO in process space. We try to put it
235 * at vdso_base which is the "natural" base for it, but we might fail
236 * and end up putting it elsewhere.
238 vdso_base = get_unmapped_area(NULL, vdso_base,
239 vdso_pages << PAGE_SHIFT, 0, 0);
240 if (vdso_base & ~PAGE_MASK)
241 return (int)vdso_base;
243 current->thread.vdso_base = vdso_base;
245 vma->vm_mm = mm;
246 vma->vm_start = current->thread.vdso_base;
249 * the VMA size is one page more than the vDSO since systemcfg
250 * is mapped in the last one
252 vma->vm_end = vma->vm_start + ((vdso_pages + 1) << PAGE_SHIFT);
255 * our vma flags don't have VM_WRITE so by default, the process isn't allowed
256 * to write those pages.
257 * gdb can break that with ptrace interface, and thus trigger COW on those
258 * pages but it's then your responsibility to never do that on the "data" page
259 * of the vDSO or you'll stop getting kernel updates and your nice userland
260 * gettimeofday will be totally dead. It's fine to use that for setting
261 * breakpoints in the vDSO code pages though
263 vma->vm_flags = VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
264 vma->vm_flags |= mm->def_flags;
265 vma->vm_page_prot = protection_map[vma->vm_flags & 0x7];
266 vma->vm_ops = &vdso_vmops;
268 down_write(&mm->mmap_sem);
269 insert_vm_struct(mm, vma);
270 mm->total_vm += (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
271 up_write(&mm->mmap_sem);
273 return 0;
276 static void * __init find_section32(Elf32_Ehdr *ehdr, const char *secname,
277 unsigned long *size)
279 Elf32_Shdr *sechdrs;
280 unsigned int i;
281 char *secnames;
283 /* Grab section headers and strings so we can tell who is who */
284 sechdrs = (void *)ehdr + ehdr->e_shoff;
285 secnames = (void *)ehdr + sechdrs[ehdr->e_shstrndx].sh_offset;
287 /* Find the section they want */
288 for (i = 1; i < ehdr->e_shnum; i++) {
289 if (strcmp(secnames+sechdrs[i].sh_name, secname) == 0) {
290 if (size)
291 *size = sechdrs[i].sh_size;
292 return (void *)ehdr + sechdrs[i].sh_offset;
295 *size = 0;
296 return NULL;
299 static void * __init find_section64(Elf64_Ehdr *ehdr, const char *secname,
300 unsigned long *size)
302 Elf64_Shdr *sechdrs;
303 unsigned int i;
304 char *secnames;
306 /* Grab section headers and strings so we can tell who is who */
307 sechdrs = (void *)ehdr + ehdr->e_shoff;
308 secnames = (void *)ehdr + sechdrs[ehdr->e_shstrndx].sh_offset;
310 /* Find the section they want */
311 for (i = 1; i < ehdr->e_shnum; i++) {
312 if (strcmp(secnames+sechdrs[i].sh_name, secname) == 0) {
313 if (size)
314 *size = sechdrs[i].sh_size;
315 return (void *)ehdr + sechdrs[i].sh_offset;
318 if (size)
319 *size = 0;
320 return NULL;
323 static Elf32_Sym * __init find_symbol32(struct lib32_elfinfo *lib, const char *symname)
325 unsigned int i;
326 char name[32], *c;
328 for (i = 0; i < (lib->dynsymsize / sizeof(Elf32_Sym)); i++) {
329 if (lib->dynsym[i].st_name == 0)
330 continue;
331 strlcpy(name, lib->dynstr + lib->dynsym[i].st_name, 32);
332 c = strchr(name, '@');
333 if (c)
334 *c = 0;
335 if (strcmp(symname, name) == 0)
336 return &lib->dynsym[i];
338 return NULL;
341 static Elf64_Sym * __init find_symbol64(struct lib64_elfinfo *lib, const char *symname)
343 unsigned int i;
344 char name[32], *c;
346 for (i = 0; i < (lib->dynsymsize / sizeof(Elf64_Sym)); i++) {
347 if (lib->dynsym[i].st_name == 0)
348 continue;
349 strlcpy(name, lib->dynstr + lib->dynsym[i].st_name, 32);
350 c = strchr(name, '@');
351 if (c)
352 *c = 0;
353 if (strcmp(symname, name) == 0)
354 return &lib->dynsym[i];
356 return NULL;
359 /* Note that we assume the section is .text and the symbol is relative to
360 * the library base
362 static unsigned long __init find_function32(struct lib32_elfinfo *lib, const char *symname)
364 Elf32_Sym *sym = find_symbol32(lib, symname);
366 if (sym == NULL) {
367 printk(KERN_WARNING "vDSO32: function %s not found !\n", symname);
368 return 0;
370 return sym->st_value - VDSO32_LBASE;
373 /* Note that we assume the section is .text and the symbol is relative to
374 * the library base
376 static unsigned long __init find_function64(struct lib64_elfinfo *lib, const char *symname)
378 Elf64_Sym *sym = find_symbol64(lib, symname);
380 if (sym == NULL) {
381 printk(KERN_WARNING "vDSO64: function %s not found !\n", symname);
382 return 0;
384 #ifdef VDS64_HAS_DESCRIPTORS
385 return *((u64 *)(vdso64_kbase + sym->st_value - VDSO64_LBASE)) - VDSO64_LBASE;
386 #else
387 return sym->st_value - VDSO64_LBASE;
388 #endif
392 static __init int vdso_do_find_sections(struct lib32_elfinfo *v32,
393 struct lib64_elfinfo *v64)
395 void *sect;
398 * Locate symbol tables & text section
401 v32->dynsym = find_section32(v32->hdr, ".dynsym", &v32->dynsymsize);
402 v32->dynstr = find_section32(v32->hdr, ".dynstr", NULL);
403 if (v32->dynsym == NULL || v32->dynstr == NULL) {
404 printk(KERN_ERR "vDSO32: a required symbol section was not found\n");
405 return -1;
407 sect = find_section32(v32->hdr, ".text", NULL);
408 if (sect == NULL) {
409 printk(KERN_ERR "vDSO32: the .text section was not found\n");
410 return -1;
412 v32->text = sect - vdso32_kbase;
414 v64->dynsym = find_section64(v64->hdr, ".dynsym", &v64->dynsymsize);
415 v64->dynstr = find_section64(v64->hdr, ".dynstr", NULL);
416 if (v64->dynsym == NULL || v64->dynstr == NULL) {
417 printk(KERN_ERR "vDSO64: a required symbol section was not found\n");
418 return -1;
420 sect = find_section64(v64->hdr, ".text", NULL);
421 if (sect == NULL) {
422 printk(KERN_ERR "vDSO64: the .text section was not found\n");
423 return -1;
425 v64->text = sect - vdso64_kbase;
427 return 0;
430 static __init void vdso_setup_trampolines(struct lib32_elfinfo *v32,
431 struct lib64_elfinfo *v64)
434 * Find signal trampolines
437 vdso64_rt_sigtramp = find_function64(v64, "__kernel_sigtramp_rt64");
438 vdso32_sigtramp = find_function32(v32, "__kernel_sigtramp32");
439 vdso32_rt_sigtramp = find_function32(v32, "__kernel_sigtramp_rt32");
442 static __init int vdso_fixup_datapage(struct lib32_elfinfo *v32,
443 struct lib64_elfinfo *v64)
445 Elf32_Sym *sym32;
446 Elf64_Sym *sym64;
448 sym32 = find_symbol32(v32, "__kernel_datapage_offset");
449 if (sym32 == NULL) {
450 printk(KERN_ERR "vDSO32: Can't find symbol __kernel_datapage_offset !\n");
451 return -1;
453 *((int *)(vdso32_kbase + (sym32->st_value - VDSO32_LBASE))) =
454 (vdso32_pages << PAGE_SHIFT) - (sym32->st_value - VDSO32_LBASE);
456 sym64 = find_symbol64(v64, "__kernel_datapage_offset");
457 if (sym64 == NULL) {
458 printk(KERN_ERR "vDSO64: Can't find symbol __kernel_datapage_offset !\n");
459 return -1;
461 *((int *)(vdso64_kbase + sym64->st_value - VDSO64_LBASE)) =
462 (vdso64_pages << PAGE_SHIFT) - (sym64->st_value - VDSO64_LBASE);
464 return 0;
467 static int vdso_do_func_patch32(struct lib32_elfinfo *v32,
468 struct lib64_elfinfo *v64,
469 const char *orig, const char *fix)
471 Elf32_Sym *sym32_gen, *sym32_fix;
473 sym32_gen = find_symbol32(v32, orig);
474 if (sym32_gen == NULL) {
475 printk(KERN_ERR "vDSO32: Can't find symbol %s !\n", orig);
476 return -1;
478 sym32_fix = find_symbol32(v32, fix);
479 if (sym32_fix == NULL) {
480 printk(KERN_ERR "vDSO32: Can't find symbol %s !\n", fix);
481 return -1;
483 sym32_gen->st_value = sym32_fix->st_value;
484 sym32_gen->st_size = sym32_fix->st_size;
485 sym32_gen->st_info = sym32_fix->st_info;
486 sym32_gen->st_other = sym32_fix->st_other;
487 sym32_gen->st_shndx = sym32_fix->st_shndx;
489 return 0;
492 static int vdso_do_func_patch64(struct lib32_elfinfo *v32,
493 struct lib64_elfinfo *v64,
494 const char *orig, const char *fix)
496 Elf64_Sym *sym64_gen, *sym64_fix;
498 sym64_gen = find_symbol64(v64, orig);
499 if (sym64_gen == NULL) {
500 printk(KERN_ERR "vDSO64: Can't find symbol %s !\n", orig);
501 return -1;
503 sym64_fix = find_symbol64(v64, fix);
504 if (sym64_fix == NULL) {
505 printk(KERN_ERR "vDSO64: Can't find symbol %s !\n", fix);
506 return -1;
508 sym64_gen->st_value = sym64_fix->st_value;
509 sym64_gen->st_size = sym64_fix->st_size;
510 sym64_gen->st_info = sym64_fix->st_info;
511 sym64_gen->st_other = sym64_fix->st_other;
512 sym64_gen->st_shndx = sym64_fix->st_shndx;
514 return 0;
517 static __init int vdso_fixup_alt_funcs(struct lib32_elfinfo *v32,
518 struct lib64_elfinfo *v64)
520 u32 pvr;
521 int i;
523 pvr = mfspr(SPRN_PVR);
524 for (i = 0; i < ARRAY_SIZE(vdso_patches); i++) {
525 struct vdso_patch_def *patch = &vdso_patches[i];
526 int match = (pvr & patch->pvr_mask) == patch->pvr_value;
528 DBG("patch %d (mask: %x, pvr: %x) : %s\n",
529 i, patch->pvr_mask, patch->pvr_value, match ? "match" : "skip");
531 if (!match)
532 continue;
534 DBG("replacing %s with %s...\n", patch->gen_name, patch->fix_name);
537 * Patch the 32 bits and 64 bits symbols. Note that we do not patch
538 * the "." symbol on 64 bits. It would be easy to do, but doesn't
539 * seem to be necessary, patching the OPD symbol is enough.
541 vdso_do_func_patch32(v32, v64, patch->gen_name, patch->fix_name);
542 vdso_do_func_patch64(v32, v64, patch->gen_name, patch->fix_name);
545 return 0;
549 static __init int vdso_setup(void)
551 struct lib32_elfinfo v32;
552 struct lib64_elfinfo v64;
554 v32.hdr = vdso32_kbase;
555 v64.hdr = vdso64_kbase;
557 if (vdso_do_find_sections(&v32, &v64))
558 return -1;
560 if (vdso_fixup_datapage(&v32, &v64))
561 return -1;
563 if (vdso_fixup_alt_funcs(&v32, &v64))
564 return -1;
566 vdso_setup_trampolines(&v32, &v64);
568 return 0;
571 void __init vdso_init(void)
573 int i;
575 vdso64_pages = (&vdso64_end - &vdso64_start) >> PAGE_SHIFT;
576 vdso32_pages = (&vdso32_end - &vdso32_start) >> PAGE_SHIFT;
578 DBG("vdso64_kbase: %p, 0x%x pages, vdso32_kbase: %p, 0x%x pages\n",
579 vdso64_kbase, vdso64_pages, vdso32_kbase, vdso32_pages);
582 * Initialize the vDSO images in memory, that is do necessary
583 * fixups of vDSO symbols, locate trampolines, etc...
585 if (vdso_setup()) {
586 printk(KERN_ERR "vDSO setup failure, not enabled !\n");
587 /* XXX should free pages here ? */
588 vdso64_pages = vdso32_pages = 0;
589 return;
592 /* Make sure pages are in the correct state */
593 for (i = 0; i < vdso64_pages; i++) {
594 struct page *pg = virt_to_page(vdso64_kbase + i*PAGE_SIZE);
595 ClearPageReserved(pg);
596 get_page(pg);
598 for (i = 0; i < vdso32_pages; i++) {
599 struct page *pg = virt_to_page(vdso32_kbase + i*PAGE_SIZE);
600 ClearPageReserved(pg);
601 get_page(pg);
605 int in_gate_area_no_task(unsigned long addr)
607 return 0;
610 int in_gate_area(struct task_struct *task, unsigned long addr)
612 return 0;
615 struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
617 return NULL;