2 * Copyright (C) 2004 Benjamin Herrenschmidt, IBM Corp.
3 * <benh@kernel.crashing.org>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
11 #include <linux/module.h>
12 #include <linux/errno.h>
13 #include <linux/sched.h>
14 #include <linux/kernel.h>
16 #include <linux/smp.h>
17 #include <linux/smp_lock.h>
18 #include <linux/stddef.h>
19 #include <linux/unistd.h>
20 #include <linux/slab.h>
21 #include <linux/user.h>
22 #include <linux/elf.h>
23 #include <linux/security.h>
24 #include <linux/bootmem.h>
25 #include <linux/vs_memory.h>
27 #include <asm/pgtable.h>
28 #include <asm/system.h>
29 #include <asm/processor.h>
31 #include <asm/mmu_context.h>
33 #include <asm/machdep.h>
34 #include <asm/cputable.h>
35 #include <asm/sections.h>
36 #include <asm/firmware.h>
38 #include <asm/vdso_datapage.h>
45 #define DBG(fmt...) printk(fmt)
50 /* Max supported size for symbol names */
51 #define MAX_SYMNAME 64
53 extern char vdso32_start, vdso32_end;
54 static void *vdso32_kbase = &vdso32_start;
55 unsigned int vdso32_pages;
56 unsigned long vdso32_sigtramp;
57 unsigned long vdso32_rt_sigtramp;
60 extern char vdso64_start, vdso64_end;
61 static void *vdso64_kbase = &vdso64_start;
62 unsigned int vdso64_pages;
63 unsigned long vdso64_rt_sigtramp;
64 #endif /* CONFIG_PPC64 */
67 * The vdso data page (aka. systemcfg for old ppc64 fans) is here.
68 * Once the early boot kernel code no longer needs to muck around
69 * with it, it will become dynamically allocated
72 struct vdso_data data;
74 } vdso_data_store __attribute__((__section__(".data.page_aligned")));
75 struct vdso_data *vdso_data = &vdso_data_store.data;
77 /* Format of the patch table */
80 unsigned long ftr_mask, ftr_value;
85 /* Table of functions to patch based on the CPU type/revision
87 * Currently, we only change sync_dicache to do nothing on processors
88 * with a coherent icache
90 static struct vdso_patch_def vdso_patches[] = {
92 CPU_FTR_COHERENT_ICACHE, CPU_FTR_COHERENT_ICACHE,
93 "__kernel_sync_dicache", "__kernel_sync_dicache_p5"
97 "__kernel_gettimeofday", NULL
102 * Some infos carried around for each of them during parsing at
107 Elf32_Ehdr *hdr; /* ptr to ELF */
108 Elf32_Sym *dynsym; /* ptr to .dynsym section */
109 unsigned long dynsymsize; /* size of .dynsym section */
110 char *dynstr; /* ptr to .dynstr section */
111 unsigned long text; /* offset of .text section in .so */
118 unsigned long dynsymsize;
125 static void dump_one_vdso_page(struct page *pg, struct page *upg)
127 printk("kpg: %p (c:%d,f:%08lx)", __va(page_to_pfn(pg) << PAGE_SHIFT),
130 if (upg/* && pg != upg*/) {
131 printk(" upg: %p (c:%d,f:%08lx)", __va(page_to_pfn(upg)
139 static void dump_vdso_pages(struct vm_area_struct * vma)
143 if (!vma || test_thread_flag(TIF_32BIT)) {
144 printk("vDSO32 @ %016lx:\n", (unsigned long)vdso32_kbase);
145 for (i=0; i<vdso32_pages; i++) {
146 struct page *pg = virt_to_page(vdso32_kbase +
148 struct page *upg = (vma && vma->vm_mm) ?
149 follow_page(vma, vma->vm_start + i*PAGE_SIZE, 0)
151 dump_one_vdso_page(pg, upg);
154 if (!vma || !test_thread_flag(TIF_32BIT)) {
155 printk("vDSO64 @ %016lx:\n", (unsigned long)vdso64_kbase);
156 for (i=0; i<vdso64_pages; i++) {
157 struct page *pg = virt_to_page(vdso64_kbase +
159 struct page *upg = (vma && vma->vm_mm) ?
160 follow_page(vma, vma->vm_start + i*PAGE_SIZE, 0)
162 dump_one_vdso_page(pg, upg);
169 * Keep a dummy vma_close for now, it will prevent VMA merging.
171 static void vdso_vma_close(struct vm_area_struct * vma)
176 * Our nopage() function, maps in the actual vDSO kernel pages, they will
177 * be mapped read-only by do_no_page(), and eventually COW'ed, either
178 * right away for an initial write access, or by do_wp_page().
180 static struct page * vdso_vma_nopage(struct vm_area_struct * vma,
181 unsigned long address, int *type)
183 unsigned long offset = address - vma->vm_start;
186 void *vbase = (vma->vm_mm->task_size > TASK_SIZE_USER32) ?
187 vdso64_kbase : vdso32_kbase;
189 void *vbase = vdso32_kbase;
192 DBG("vdso_vma_nopage(current: %s, address: %016lx, off: %lx)\n",
193 current->comm, address, offset);
195 if (address < vma->vm_start || address > vma->vm_end)
196 return NOPAGE_SIGBUS;
199 * Last page is systemcfg.
201 if ((vma->vm_end - address) <= PAGE_SIZE)
202 pg = virt_to_page(vdso_data);
204 pg = virt_to_page(vbase + offset);
207 DBG(" ->page count: %d\n", page_count(pg));
212 static struct vm_operations_struct vdso_vmops = {
213 .close = vdso_vma_close,
214 .nopage = vdso_vma_nopage,
218 * This is called from binfmt_elf, we create the special vma for the
219 * vDSO and insert it into the mm struct tree
221 int arch_setup_additional_pages(struct linux_binprm *bprm,
222 int executable_stack, unsigned long start_code,
223 unsigned long interp_map_address)
225 struct mm_struct *mm = current->mm;
226 struct vm_area_struct *vma;
227 unsigned long vdso_pages;
228 unsigned long vdso_base;
232 if (test_thread_flag(TIF_32BIT)) {
233 vdso_pages = vdso32_pages;
234 vdso_base = VDSO32_MBASE;
236 vdso_pages = vdso64_pages;
237 vdso_base = VDSO64_MBASE;
240 vdso_pages = vdso32_pages;
241 vdso_base = VDSO32_MBASE;
244 current->mm->context.vdso_base = 0;
246 /* vDSO has a problem and was disabled, just don't "enable" it for the
251 /* Add a page to the vdso size for the data page */
255 * pick a base address for the vDSO in process space. We try to put it
256 * at vdso_base which is the "natural" base for it, but we might fail
257 * and end up putting it elsewhere.
259 down_write(&mm->mmap_sem);
260 vdso_base = get_unmapped_area(NULL, vdso_base,
261 vdso_pages << PAGE_SHIFT, 0, 0);
262 if (IS_ERR_VALUE(vdso_base)) {
268 /* Allocate a VMA structure and fill it up */
269 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
275 vma->vm_start = vdso_base;
276 vma->vm_end = vma->vm_start + (vdso_pages << PAGE_SHIFT);
279 * our vma flags don't have VM_WRITE so by default, the process isn't
280 * allowed to write those pages.
281 * gdb can break that with ptrace interface, and thus trigger COW on
282 * those pages but it's then your responsibility to never do that on
283 * the "data" page of the vDSO or you'll stop getting kernel updates
284 * and your nice userland gettimeofday will be totally dead.
285 * It's fine to use that for setting breakpoints in the vDSO code
288 vma->vm_flags = VM_READ|VM_EXEC|VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC;
290 * Make sure the vDSO gets into every core dump.
291 * Dumping its contents makes post-mortem fully interpretable later
292 * without matching up the same kernel and hardware config to see
293 * what PC values meant.
295 vma->vm_flags |= VM_ALWAYSDUMP;
296 vma->vm_flags |= mm->def_flags;
297 vma->vm_page_prot = protection_map[vma->vm_flags & 0x7];
298 vma->vm_ops = &vdso_vmops;
301 rc = insert_vm_struct(mm, vma);
305 /* Put vDSO base into mm struct and account for memory usage */
306 current->mm->context.vdso_base = vdso_base;
307 vx_vmpages_add(mm, (vma->vm_end - vma->vm_start) >> PAGE_SHIFT);
308 up_write(&mm->mmap_sem);
312 kmem_cache_free(vm_area_cachep, vma);
314 up_write(&mm->mmap_sem);
318 const char *arch_vma_name(struct vm_area_struct *vma)
320 if (vma->vm_mm && vma->vm_start == vma->vm_mm->context.vdso_base)
327 static void * __init find_section32(Elf32_Ehdr *ehdr, const char *secname,
334 /* Grab section headers and strings so we can tell who is who */
335 sechdrs = (void *)ehdr + ehdr->e_shoff;
336 secnames = (void *)ehdr + sechdrs[ehdr->e_shstrndx].sh_offset;
338 /* Find the section they want */
339 for (i = 1; i < ehdr->e_shnum; i++) {
340 if (strcmp(secnames+sechdrs[i].sh_name, secname) == 0) {
342 *size = sechdrs[i].sh_size;
343 return (void *)ehdr + sechdrs[i].sh_offset;
350 static Elf32_Sym * __init find_symbol32(struct lib32_elfinfo *lib,
354 char name[MAX_SYMNAME], *c;
356 for (i = 0; i < (lib->dynsymsize / sizeof(Elf32_Sym)); i++) {
357 if (lib->dynsym[i].st_name == 0)
359 strlcpy(name, lib->dynstr + lib->dynsym[i].st_name,
361 c = strchr(name, '@');
364 if (strcmp(symname, name) == 0)
365 return &lib->dynsym[i];
370 /* Note that we assume the section is .text and the symbol is relative to
373 static unsigned long __init find_function32(struct lib32_elfinfo *lib,
376 Elf32_Sym *sym = find_symbol32(lib, symname);
379 printk(KERN_WARNING "vDSO32: function %s not found !\n",
383 return sym->st_value - VDSO32_LBASE;
386 static int vdso_do_func_patch32(struct lib32_elfinfo *v32,
387 struct lib64_elfinfo *v64,
388 const char *orig, const char *fix)
390 Elf32_Sym *sym32_gen, *sym32_fix;
392 sym32_gen = find_symbol32(v32, orig);
393 if (sym32_gen == NULL) {
394 printk(KERN_ERR "vDSO32: Can't find symbol %s !\n", orig);
398 sym32_gen->st_name = 0;
401 sym32_fix = find_symbol32(v32, fix);
402 if (sym32_fix == NULL) {
403 printk(KERN_ERR "vDSO32: Can't find symbol %s !\n", fix);
406 sym32_gen->st_value = sym32_fix->st_value;
407 sym32_gen->st_size = sym32_fix->st_size;
408 sym32_gen->st_info = sym32_fix->st_info;
409 sym32_gen->st_other = sym32_fix->st_other;
410 sym32_gen->st_shndx = sym32_fix->st_shndx;
418 static void * __init find_section64(Elf64_Ehdr *ehdr, const char *secname,
425 /* Grab section headers and strings so we can tell who is who */
426 sechdrs = (void *)ehdr + ehdr->e_shoff;
427 secnames = (void *)ehdr + sechdrs[ehdr->e_shstrndx].sh_offset;
429 /* Find the section they want */
430 for (i = 1; i < ehdr->e_shnum; i++) {
431 if (strcmp(secnames+sechdrs[i].sh_name, secname) == 0) {
433 *size = sechdrs[i].sh_size;
434 return (void *)ehdr + sechdrs[i].sh_offset;
442 static Elf64_Sym * __init find_symbol64(struct lib64_elfinfo *lib,
446 char name[MAX_SYMNAME], *c;
448 for (i = 0; i < (lib->dynsymsize / sizeof(Elf64_Sym)); i++) {
449 if (lib->dynsym[i].st_name == 0)
451 strlcpy(name, lib->dynstr + lib->dynsym[i].st_name,
453 c = strchr(name, '@');
456 if (strcmp(symname, name) == 0)
457 return &lib->dynsym[i];
462 /* Note that we assume the section is .text and the symbol is relative to
465 static unsigned long __init find_function64(struct lib64_elfinfo *lib,
468 Elf64_Sym *sym = find_symbol64(lib, symname);
471 printk(KERN_WARNING "vDSO64: function %s not found !\n",
475 #ifdef VDS64_HAS_DESCRIPTORS
476 return *((u64 *)(vdso64_kbase + sym->st_value - VDSO64_LBASE)) -
479 return sym->st_value - VDSO64_LBASE;
483 static int vdso_do_func_patch64(struct lib32_elfinfo *v32,
484 struct lib64_elfinfo *v64,
485 const char *orig, const char *fix)
487 Elf64_Sym *sym64_gen, *sym64_fix;
489 sym64_gen = find_symbol64(v64, orig);
490 if (sym64_gen == NULL) {
491 printk(KERN_ERR "vDSO64: Can't find symbol %s !\n", orig);
495 sym64_gen->st_name = 0;
498 sym64_fix = find_symbol64(v64, fix);
499 if (sym64_fix == NULL) {
500 printk(KERN_ERR "vDSO64: Can't find symbol %s !\n", fix);
503 sym64_gen->st_value = sym64_fix->st_value;
504 sym64_gen->st_size = sym64_fix->st_size;
505 sym64_gen->st_info = sym64_fix->st_info;
506 sym64_gen->st_other = sym64_fix->st_other;
507 sym64_gen->st_shndx = sym64_fix->st_shndx;
512 #endif /* CONFIG_PPC64 */
515 static __init int vdso_do_find_sections(struct lib32_elfinfo *v32,
516 struct lib64_elfinfo *v64)
521 * Locate symbol tables & text section
524 v32->dynsym = find_section32(v32->hdr, ".dynsym", &v32->dynsymsize);
525 v32->dynstr = find_section32(v32->hdr, ".dynstr", NULL);
526 if (v32->dynsym == NULL || v32->dynstr == NULL) {
527 printk(KERN_ERR "vDSO32: required symbol section not found\n");
530 sect = find_section32(v32->hdr, ".text", NULL);
532 printk(KERN_ERR "vDSO32: the .text section was not found\n");
535 v32->text = sect - vdso32_kbase;
538 v64->dynsym = find_section64(v64->hdr, ".dynsym", &v64->dynsymsize);
539 v64->dynstr = find_section64(v64->hdr, ".dynstr", NULL);
540 if (v64->dynsym == NULL || v64->dynstr == NULL) {
541 printk(KERN_ERR "vDSO64: required symbol section not found\n");
544 sect = find_section64(v64->hdr, ".text", NULL);
546 printk(KERN_ERR "vDSO64: the .text section was not found\n");
549 v64->text = sect - vdso64_kbase;
550 #endif /* CONFIG_PPC64 */
555 static __init void vdso_setup_trampolines(struct lib32_elfinfo *v32,
556 struct lib64_elfinfo *v64)
559 * Find signal trampolines
563 vdso64_rt_sigtramp = find_function64(v64, "__kernel_sigtramp_rt64");
565 vdso32_sigtramp = find_function32(v32, "__kernel_sigtramp32");
566 vdso32_rt_sigtramp = find_function32(v32, "__kernel_sigtramp_rt32");
569 static __init int vdso_fixup_datapage(struct lib32_elfinfo *v32,
570 struct lib64_elfinfo *v64)
576 sym64 = find_symbol64(v64, "__kernel_datapage_offset");
578 printk(KERN_ERR "vDSO64: Can't find symbol "
579 "__kernel_datapage_offset !\n");
582 *((int *)(vdso64_kbase + sym64->st_value - VDSO64_LBASE)) =
583 (vdso64_pages << PAGE_SHIFT) -
584 (sym64->st_value - VDSO64_LBASE);
585 #endif /* CONFIG_PPC64 */
587 sym32 = find_symbol32(v32, "__kernel_datapage_offset");
589 printk(KERN_ERR "vDSO32: Can't find symbol "
590 "__kernel_datapage_offset !\n");
593 *((int *)(vdso32_kbase + (sym32->st_value - VDSO32_LBASE))) =
594 (vdso32_pages << PAGE_SHIFT) -
595 (sym32->st_value - VDSO32_LBASE);
601 static __init int vdso_fixup_features(struct lib32_elfinfo *v32,
602 struct lib64_elfinfo *v64)
605 unsigned long size32;
609 unsigned long size64;
611 start64 = find_section64(v64->hdr, "__ftr_fixup", &size64);
613 do_feature_fixups(cur_cpu_spec->cpu_features,
614 start64, start64 + size64);
616 start64 = find_section64(v64->hdr, "__fw_ftr_fixup", &size64);
618 do_feature_fixups(powerpc_firmware_features,
619 start64, start64 + size64);
620 #endif /* CONFIG_PPC64 */
622 start32 = find_section32(v32->hdr, "__ftr_fixup", &size32);
624 do_feature_fixups(cur_cpu_spec->cpu_features,
625 start32, start32 + size32);
628 start32 = find_section32(v32->hdr, "__fw_ftr_fixup", &size32);
630 do_feature_fixups(powerpc_firmware_features,
631 start32, start32 + size32);
632 #endif /* CONFIG_PPC64 */
637 static __init int vdso_fixup_alt_funcs(struct lib32_elfinfo *v32,
638 struct lib64_elfinfo *v64)
642 for (i = 0; i < ARRAY_SIZE(vdso_patches); i++) {
643 struct vdso_patch_def *patch = &vdso_patches[i];
644 int match = (cur_cpu_spec->cpu_features & patch->ftr_mask)
649 DBG("replacing %s with %s...\n", patch->gen_name,
650 patch->fix_name ? "NONE" : patch->fix_name);
653 * Patch the 32 bits and 64 bits symbols. Note that we do not
654 * patch the "." symbol on 64 bits.
655 * It would be easy to do, but doesn't seem to be necessary,
656 * patching the OPD symbol is enough.
658 vdso_do_func_patch32(v32, v64, patch->gen_name,
661 vdso_do_func_patch64(v32, v64, patch->gen_name,
663 #endif /* CONFIG_PPC64 */
670 static __init int vdso_setup(void)
672 struct lib32_elfinfo v32;
673 struct lib64_elfinfo v64;
675 v32.hdr = vdso32_kbase;
677 v64.hdr = vdso64_kbase;
679 if (vdso_do_find_sections(&v32, &v64))
682 if (vdso_fixup_datapage(&v32, &v64))
685 if (vdso_fixup_features(&v32, &v64))
688 if (vdso_fixup_alt_funcs(&v32, &v64))
691 vdso_setup_trampolines(&v32, &v64);
697 * Called from setup_arch to initialize the bitmap of available
698 * syscalls in the systemcfg page
700 static void __init vdso_setup_syscall_map(void)
703 extern unsigned long *sys_call_table;
704 extern unsigned long sys_ni_syscall;
707 for (i = 0; i < __NR_syscalls; i++) {
709 if (sys_call_table[i*2] != sys_ni_syscall)
710 vdso_data->syscall_map_64[i >> 5] |=
711 0x80000000UL >> (i & 0x1f);
712 if (sys_call_table[i*2+1] != sys_ni_syscall)
713 vdso_data->syscall_map_32[i >> 5] |=
714 0x80000000UL >> (i & 0x1f);
715 #else /* CONFIG_PPC64 */
716 if (sys_call_table[i] != sys_ni_syscall)
717 vdso_data->syscall_map_32[i >> 5] |=
718 0x80000000UL >> (i & 0x1f);
719 #endif /* CONFIG_PPC64 */
724 void __init vdso_init(void)
730 * Fill up the "systemcfg" stuff for backward compatiblity
732 strcpy(vdso_data->eye_catcher, "SYSTEMCFG:PPC64");
733 vdso_data->version.major = SYSTEMCFG_MAJOR;
734 vdso_data->version.minor = SYSTEMCFG_MINOR;
735 vdso_data->processor = mfspr(SPRN_PVR);
737 * Fake the old platform number for pSeries and iSeries and add
738 * in LPAR bit if necessary
740 vdso_data->platform = machine_is(iseries) ? 0x200 : 0x100;
741 if (firmware_has_feature(FW_FEATURE_LPAR))
742 vdso_data->platform |= 1;
743 vdso_data->physicalMemorySize = lmb_phys_mem_size();
744 vdso_data->dcache_size = ppc64_caches.dsize;
745 vdso_data->dcache_line_size = ppc64_caches.dline_size;
746 vdso_data->icache_size = ppc64_caches.isize;
747 vdso_data->icache_line_size = ppc64_caches.iline_size;
750 * Calculate the size of the 64 bits vDSO
752 vdso64_pages = (&vdso64_end - &vdso64_start) >> PAGE_SHIFT;
753 DBG("vdso64_kbase: %p, 0x%x pages\n", vdso64_kbase, vdso64_pages);
754 #endif /* CONFIG_PPC64 */
758 * Calculate the size of the 32 bits vDSO
760 vdso32_pages = (&vdso32_end - &vdso32_start) >> PAGE_SHIFT;
761 DBG("vdso32_kbase: %p, 0x%x pages\n", vdso32_kbase, vdso32_pages);
765 * Setup the syscall map in the vDOS
767 vdso_setup_syscall_map();
770 * Initialize the vDSO images in memory, that is do necessary
771 * fixups of vDSO symbols, locate trampolines, etc...
774 printk(KERN_ERR "vDSO setup failure, not enabled !\n");
782 /* Make sure pages are in the correct state */
783 for (i = 0; i < vdso32_pages; i++) {
784 struct page *pg = virt_to_page(vdso32_kbase + i*PAGE_SIZE);
785 ClearPageReserved(pg);
790 for (i = 0; i < vdso64_pages; i++) {
791 struct page *pg = virt_to_page(vdso64_kbase + i*PAGE_SIZE);
792 ClearPageReserved(pg);
795 #endif /* CONFIG_PPC64 */
797 get_page(virt_to_page(vdso_data));
800 int in_gate_area_no_task(unsigned long addr)
805 int in_gate_area(struct task_struct *task, unsigned long addr)
810 struct vm_area_struct *get_gate_vma(struct task_struct *tsk)