3 * Copyright (C) 2004 Benjamin Herrenschmidt, IBM Corp.
4 * <benh@kernel.crashing.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/errno.h>
13 #include <linux/sched.h>
14 #include <linux/kernel.h>
16 #include <linux/smp.h>
17 #include <linux/stddef.h>
18 #include <linux/unistd.h>
19 #include <linux/slab.h>
20 #include <linux/user.h>
21 #include <linux/elf.h>
22 #include <linux/security.h>
23 #include <linux/bootmem.h>
24 #include <linux/memblock.h>
26 #include <asm/pgtable.h>
27 #include <asm/system.h>
28 #include <asm/processor.h>
30 #include <asm/mmu_context.h>
32 #include <asm/machdep.h>
33 #include <asm/cputable.h>
34 #include <asm/sections.h>
35 #include <asm/firmware.h>
37 #include <asm/vdso_datapage.h>
44 #define DBG(fmt...) printk(fmt)
49 /* Max supported size for symbol names */
50 #define MAX_SYMNAME 64
52 /* The alignment of the vDSO */
53 #define VDSO_ALIGNMENT (1 << 16)
55 extern char vdso32_start, vdso32_end;
56 static void *vdso32_kbase = &vdso32_start;
57 static unsigned int vdso32_pages;
58 static struct page **vdso32_pagelist;
59 unsigned long vdso32_sigtramp;
60 unsigned long vdso32_rt_sigtramp;
63 extern char vdso64_start, vdso64_end;
64 static void *vdso64_kbase = &vdso64_start;
65 static unsigned int vdso64_pages;
66 static struct page **vdso64_pagelist;
67 unsigned long vdso64_rt_sigtramp;
68 #endif /* CONFIG_PPC64 */
70 static int vdso_ready;
73 * The vdso data page (aka. systemcfg for old ppc64 fans) is here.
74 * Once the early boot kernel code no longer needs to muck around
75 * with it, it will become dynamically allocated
78 struct vdso_data data;
80 } vdso_data_store __page_aligned_data;
81 struct vdso_data *vdso_data = &vdso_data_store.data;
83 /* Format of the patch table */
86 unsigned long ftr_mask, ftr_value;
91 /* Table of functions to patch based on the CPU type/revision
93 * Currently, we only change sync_dicache to do nothing on processors
94 * with a coherent icache
96 static struct vdso_patch_def vdso_patches[] = {
98 CPU_FTR_COHERENT_ICACHE, CPU_FTR_COHERENT_ICACHE,
99 "__kernel_sync_dicache", "__kernel_sync_dicache_p5"
103 "__kernel_gettimeofday", NULL
107 "__kernel_clock_gettime", NULL
111 "__kernel_clock_getres", NULL
115 "__kernel_get_tbfreq", NULL
120 * Some infos carried around for each of them during parsing at
125 Elf32_Ehdr *hdr; /* ptr to ELF */
126 Elf32_Sym *dynsym; /* ptr to .dynsym section */
127 unsigned long dynsymsize; /* size of .dynsym section */
128 char *dynstr; /* ptr to .dynstr section */
129 unsigned long text; /* offset of .text section in .so */
136 unsigned long dynsymsize;
143 static void dump_one_vdso_page(struct page *pg, struct page *upg)
145 printk("kpg: %p (c:%d,f:%08lx)", __va(page_to_pfn(pg) << PAGE_SHIFT),
148 if (upg && !IS_ERR(upg) /* && pg != upg*/) {
149 printk(" upg: %p (c:%d,f:%08lx)", __va(page_to_pfn(upg)
157 static void dump_vdso_pages(struct vm_area_struct * vma)
161 if (!vma || is_32bit_task()) {
162 printk("vDSO32 @ %016lx:\n", (unsigned long)vdso32_kbase);
163 for (i=0; i<vdso32_pages; i++) {
164 struct page *pg = virt_to_page(vdso32_kbase +
166 struct page *upg = (vma && vma->vm_mm) ?
167 follow_page(vma, vma->vm_start + i*PAGE_SIZE, 0)
169 dump_one_vdso_page(pg, upg);
172 if (!vma || !is_32bit_task()) {
173 printk("vDSO64 @ %016lx:\n", (unsigned long)vdso64_kbase);
174 for (i=0; i<vdso64_pages; i++) {
175 struct page *pg = virt_to_page(vdso64_kbase +
177 struct page *upg = (vma && vma->vm_mm) ?
178 follow_page(vma, vma->vm_start + i*PAGE_SIZE, 0)
180 dump_one_vdso_page(pg, upg);
187 * This is called from binfmt_elf, we create the special vma for the
188 * vDSO and insert it into the mm struct tree
190 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
192 struct mm_struct *mm = current->mm;
193 struct page **vdso_pagelist;
194 unsigned long vdso_pages;
195 unsigned long vdso_base;
202 if (is_32bit_task()) {
203 vdso_pagelist = vdso32_pagelist;
204 vdso_pages = vdso32_pages;
205 vdso_base = VDSO32_MBASE;
207 vdso_pagelist = vdso64_pagelist;
208 vdso_pages = vdso64_pages;
210 * On 64bit we don't have a preferred map address. This
211 * allows get_unmapped_area to find an area near other mmaps
212 * and most likely share a SLB entry.
217 vdso_pagelist = vdso32_pagelist;
218 vdso_pages = vdso32_pages;
219 vdso_base = VDSO32_MBASE;
222 current->mm->context.vdso_base = 0;
224 /* vDSO has a problem and was disabled, just don't "enable" it for the
229 /* Add a page to the vdso size for the data page */
233 * pick a base address for the vDSO in process space. We try to put it
234 * at vdso_base which is the "natural" base for it, but we might fail
235 * and end up putting it elsewhere.
236 * Add enough to the size so that the result can be aligned.
238 down_write(&mm->mmap_sem);
239 vdso_base = get_unmapped_area(NULL, vdso_base,
240 (vdso_pages << PAGE_SHIFT) +
241 ((VDSO_ALIGNMENT - 1) & PAGE_MASK),
243 if (IS_ERR_VALUE(vdso_base)) {
248 /* Add required alignment. */
249 vdso_base = ALIGN(vdso_base, VDSO_ALIGNMENT);
252 * Put vDSO base into mm struct. We need to do this before calling
253 * install_special_mapping or the perf counter mmap tracking code
254 * will fail to recognise it as a vDSO (since arch_vma_name fails).
256 current->mm->context.vdso_base = vdso_base;
259 * our vma flags don't have VM_WRITE so by default, the process isn't
260 * allowed to write those pages.
261 * gdb can break that with ptrace interface, and thus trigger COW on
262 * those pages but it's then your responsibility to never do that on
263 * the "data" page of the vDSO or you'll stop getting kernel updates
264 * and your nice userland gettimeofday will be totally dead.
265 * It's fine to use that for setting breakpoints in the vDSO code
268 rc = install_special_mapping(mm, vdso_base, vdso_pages << PAGE_SHIFT,
270 VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
273 current->mm->context.vdso_base = 0;
277 up_write(&mm->mmap_sem);
281 up_write(&mm->mmap_sem);
285 const char *arch_vma_name(struct vm_area_struct *vma)
287 if (vma->vm_mm && vma->vm_start == vma->vm_mm->context.vdso_base)
294 static void * __init find_section32(Elf32_Ehdr *ehdr, const char *secname,
301 /* Grab section headers and strings so we can tell who is who */
302 sechdrs = (void *)ehdr + ehdr->e_shoff;
303 secnames = (void *)ehdr + sechdrs[ehdr->e_shstrndx].sh_offset;
305 /* Find the section they want */
306 for (i = 1; i < ehdr->e_shnum; i++) {
307 if (strcmp(secnames+sechdrs[i].sh_name, secname) == 0) {
309 *size = sechdrs[i].sh_size;
310 return (void *)ehdr + sechdrs[i].sh_offset;
317 static Elf32_Sym * __init find_symbol32(struct lib32_elfinfo *lib,
321 char name[MAX_SYMNAME], *c;
323 for (i = 0; i < (lib->dynsymsize / sizeof(Elf32_Sym)); i++) {
324 if (lib->dynsym[i].st_name == 0)
326 strlcpy(name, lib->dynstr + lib->dynsym[i].st_name,
328 c = strchr(name, '@');
331 if (strcmp(symname, name) == 0)
332 return &lib->dynsym[i];
337 /* Note that we assume the section is .text and the symbol is relative to
340 static unsigned long __init find_function32(struct lib32_elfinfo *lib,
343 Elf32_Sym *sym = find_symbol32(lib, symname);
346 printk(KERN_WARNING "vDSO32: function %s not found !\n",
350 return sym->st_value - VDSO32_LBASE;
353 static int __init vdso_do_func_patch32(struct lib32_elfinfo *v32,
354 struct lib64_elfinfo *v64,
355 const char *orig, const char *fix)
357 Elf32_Sym *sym32_gen, *sym32_fix;
359 sym32_gen = find_symbol32(v32, orig);
360 if (sym32_gen == NULL) {
361 printk(KERN_ERR "vDSO32: Can't find symbol %s !\n", orig);
365 sym32_gen->st_name = 0;
368 sym32_fix = find_symbol32(v32, fix);
369 if (sym32_fix == NULL) {
370 printk(KERN_ERR "vDSO32: Can't find symbol %s !\n", fix);
373 sym32_gen->st_value = sym32_fix->st_value;
374 sym32_gen->st_size = sym32_fix->st_size;
375 sym32_gen->st_info = sym32_fix->st_info;
376 sym32_gen->st_other = sym32_fix->st_other;
377 sym32_gen->st_shndx = sym32_fix->st_shndx;
385 static void * __init find_section64(Elf64_Ehdr *ehdr, const char *secname,
392 /* Grab section headers and strings so we can tell who is who */
393 sechdrs = (void *)ehdr + ehdr->e_shoff;
394 secnames = (void *)ehdr + sechdrs[ehdr->e_shstrndx].sh_offset;
396 /* Find the section they want */
397 for (i = 1; i < ehdr->e_shnum; i++) {
398 if (strcmp(secnames+sechdrs[i].sh_name, secname) == 0) {
400 *size = sechdrs[i].sh_size;
401 return (void *)ehdr + sechdrs[i].sh_offset;
409 static Elf64_Sym * __init find_symbol64(struct lib64_elfinfo *lib,
413 char name[MAX_SYMNAME], *c;
415 for (i = 0; i < (lib->dynsymsize / sizeof(Elf64_Sym)); i++) {
416 if (lib->dynsym[i].st_name == 0)
418 strlcpy(name, lib->dynstr + lib->dynsym[i].st_name,
420 c = strchr(name, '@');
423 if (strcmp(symname, name) == 0)
424 return &lib->dynsym[i];
429 /* Note that we assume the section is .text and the symbol is relative to
432 static unsigned long __init find_function64(struct lib64_elfinfo *lib,
435 Elf64_Sym *sym = find_symbol64(lib, symname);
438 printk(KERN_WARNING "vDSO64: function %s not found !\n",
442 #ifdef VDS64_HAS_DESCRIPTORS
443 return *((u64 *)(vdso64_kbase + sym->st_value - VDSO64_LBASE)) -
446 return sym->st_value - VDSO64_LBASE;
450 static int __init vdso_do_func_patch64(struct lib32_elfinfo *v32,
451 struct lib64_elfinfo *v64,
452 const char *orig, const char *fix)
454 Elf64_Sym *sym64_gen, *sym64_fix;
456 sym64_gen = find_symbol64(v64, orig);
457 if (sym64_gen == NULL) {
458 printk(KERN_ERR "vDSO64: Can't find symbol %s !\n", orig);
462 sym64_gen->st_name = 0;
465 sym64_fix = find_symbol64(v64, fix);
466 if (sym64_fix == NULL) {
467 printk(KERN_ERR "vDSO64: Can't find symbol %s !\n", fix);
470 sym64_gen->st_value = sym64_fix->st_value;
471 sym64_gen->st_size = sym64_fix->st_size;
472 sym64_gen->st_info = sym64_fix->st_info;
473 sym64_gen->st_other = sym64_fix->st_other;
474 sym64_gen->st_shndx = sym64_fix->st_shndx;
479 #endif /* CONFIG_PPC64 */
482 static __init int vdso_do_find_sections(struct lib32_elfinfo *v32,
483 struct lib64_elfinfo *v64)
488 * Locate symbol tables & text section
491 v32->dynsym = find_section32(v32->hdr, ".dynsym", &v32->dynsymsize);
492 v32->dynstr = find_section32(v32->hdr, ".dynstr", NULL);
493 if (v32->dynsym == NULL || v32->dynstr == NULL) {
494 printk(KERN_ERR "vDSO32: required symbol section not found\n");
497 sect = find_section32(v32->hdr, ".text", NULL);
499 printk(KERN_ERR "vDSO32: the .text section was not found\n");
502 v32->text = sect - vdso32_kbase;
505 v64->dynsym = find_section64(v64->hdr, ".dynsym", &v64->dynsymsize);
506 v64->dynstr = find_section64(v64->hdr, ".dynstr", NULL);
507 if (v64->dynsym == NULL || v64->dynstr == NULL) {
508 printk(KERN_ERR "vDSO64: required symbol section not found\n");
511 sect = find_section64(v64->hdr, ".text", NULL);
513 printk(KERN_ERR "vDSO64: the .text section was not found\n");
516 v64->text = sect - vdso64_kbase;
517 #endif /* CONFIG_PPC64 */
522 static __init void vdso_setup_trampolines(struct lib32_elfinfo *v32,
523 struct lib64_elfinfo *v64)
526 * Find signal trampolines
530 vdso64_rt_sigtramp = find_function64(v64, "__kernel_sigtramp_rt64");
532 vdso32_sigtramp = find_function32(v32, "__kernel_sigtramp32");
533 vdso32_rt_sigtramp = find_function32(v32, "__kernel_sigtramp_rt32");
536 static __init int vdso_fixup_datapage(struct lib32_elfinfo *v32,
537 struct lib64_elfinfo *v64)
543 sym64 = find_symbol64(v64, "__kernel_datapage_offset");
545 printk(KERN_ERR "vDSO64: Can't find symbol "
546 "__kernel_datapage_offset !\n");
549 *((int *)(vdso64_kbase + sym64->st_value - VDSO64_LBASE)) =
550 (vdso64_pages << PAGE_SHIFT) -
551 (sym64->st_value - VDSO64_LBASE);
552 #endif /* CONFIG_PPC64 */
554 sym32 = find_symbol32(v32, "__kernel_datapage_offset");
556 printk(KERN_ERR "vDSO32: Can't find symbol "
557 "__kernel_datapage_offset !\n");
560 *((int *)(vdso32_kbase + (sym32->st_value - VDSO32_LBASE))) =
561 (vdso32_pages << PAGE_SHIFT) -
562 (sym32->st_value - VDSO32_LBASE);
568 static __init int vdso_fixup_features(struct lib32_elfinfo *v32,
569 struct lib64_elfinfo *v64)
572 unsigned long size32;
576 unsigned long size64;
578 start64 = find_section64(v64->hdr, "__ftr_fixup", &size64);
580 do_feature_fixups(cur_cpu_spec->cpu_features,
581 start64, start64 + size64);
583 start64 = find_section64(v64->hdr, "__mmu_ftr_fixup", &size64);
585 do_feature_fixups(cur_cpu_spec->mmu_features,
586 start64, start64 + size64);
588 start64 = find_section64(v64->hdr, "__fw_ftr_fixup", &size64);
590 do_feature_fixups(powerpc_firmware_features,
591 start64, start64 + size64);
593 start64 = find_section64(v64->hdr, "__lwsync_fixup", &size64);
595 do_lwsync_fixups(cur_cpu_spec->cpu_features,
596 start64, start64 + size64);
597 #endif /* CONFIG_PPC64 */
599 start32 = find_section32(v32->hdr, "__ftr_fixup", &size32);
601 do_feature_fixups(cur_cpu_spec->cpu_features,
602 start32, start32 + size32);
604 start32 = find_section32(v32->hdr, "__mmu_ftr_fixup", &size32);
606 do_feature_fixups(cur_cpu_spec->mmu_features,
607 start32, start32 + size32);
610 start32 = find_section32(v32->hdr, "__fw_ftr_fixup", &size32);
612 do_feature_fixups(powerpc_firmware_features,
613 start32, start32 + size32);
614 #endif /* CONFIG_PPC64 */
616 start32 = find_section32(v32->hdr, "__lwsync_fixup", &size32);
618 do_lwsync_fixups(cur_cpu_spec->cpu_features,
619 start32, start32 + size32);
624 static __init int vdso_fixup_alt_funcs(struct lib32_elfinfo *v32,
625 struct lib64_elfinfo *v64)
629 for (i = 0; i < ARRAY_SIZE(vdso_patches); i++) {
630 struct vdso_patch_def *patch = &vdso_patches[i];
631 int match = (cur_cpu_spec->cpu_features & patch->ftr_mask)
636 DBG("replacing %s with %s...\n", patch->gen_name,
637 patch->fix_name ? "NONE" : patch->fix_name);
640 * Patch the 32 bits and 64 bits symbols. Note that we do not
641 * patch the "." symbol on 64 bits.
642 * It would be easy to do, but doesn't seem to be necessary,
643 * patching the OPD symbol is enough.
645 vdso_do_func_patch32(v32, v64, patch->gen_name,
648 vdso_do_func_patch64(v32, v64, patch->gen_name,
650 #endif /* CONFIG_PPC64 */
657 static __init int vdso_setup(void)
659 struct lib32_elfinfo v32;
660 struct lib64_elfinfo v64;
662 v32.hdr = vdso32_kbase;
664 v64.hdr = vdso64_kbase;
666 if (vdso_do_find_sections(&v32, &v64))
669 if (vdso_fixup_datapage(&v32, &v64))
672 if (vdso_fixup_features(&v32, &v64))
675 if (vdso_fixup_alt_funcs(&v32, &v64))
678 vdso_setup_trampolines(&v32, &v64);
684 * Called from setup_arch to initialize the bitmap of available
685 * syscalls in the systemcfg page
687 static void __init vdso_setup_syscall_map(void)
690 extern unsigned long *sys_call_table;
691 extern unsigned long sys_ni_syscall;
694 for (i = 0; i < __NR_syscalls; i++) {
696 if (sys_call_table[i*2] != sys_ni_syscall)
697 vdso_data->syscall_map_64[i >> 5] |=
698 0x80000000UL >> (i & 0x1f);
699 if (sys_call_table[i*2+1] != sys_ni_syscall)
700 vdso_data->syscall_map_32[i >> 5] |=
701 0x80000000UL >> (i & 0x1f);
702 #else /* CONFIG_PPC64 */
703 if (sys_call_table[i] != sys_ni_syscall)
704 vdso_data->syscall_map_32[i >> 5] |=
705 0x80000000UL >> (i & 0x1f);
706 #endif /* CONFIG_PPC64 */
711 static int __init vdso_init(void)
717 * Fill up the "systemcfg" stuff for backward compatibility
719 strcpy((char *)vdso_data->eye_catcher, "SYSTEMCFG:PPC64");
720 vdso_data->version.major = SYSTEMCFG_MAJOR;
721 vdso_data->version.minor = SYSTEMCFG_MINOR;
722 vdso_data->processor = mfspr(SPRN_PVR);
724 * Fake the old platform number for pSeries and iSeries and add
725 * in LPAR bit if necessary
727 vdso_data->platform = machine_is(iseries) ? 0x200 : 0x100;
728 if (firmware_has_feature(FW_FEATURE_LPAR))
729 vdso_data->platform |= 1;
730 vdso_data->physicalMemorySize = memblock_phys_mem_size();
731 vdso_data->dcache_size = ppc64_caches.dsize;
732 vdso_data->dcache_line_size = ppc64_caches.dline_size;
733 vdso_data->icache_size = ppc64_caches.isize;
734 vdso_data->icache_line_size = ppc64_caches.iline_size;
736 /* XXXOJN: Blocks should be added to ppc64_caches and used instead */
737 vdso_data->dcache_block_size = ppc64_caches.dline_size;
738 vdso_data->icache_block_size = ppc64_caches.iline_size;
739 vdso_data->dcache_log_block_size = ppc64_caches.log_dline_size;
740 vdso_data->icache_log_block_size = ppc64_caches.log_iline_size;
743 * Calculate the size of the 64 bits vDSO
745 vdso64_pages = (&vdso64_end - &vdso64_start) >> PAGE_SHIFT;
746 DBG("vdso64_kbase: %p, 0x%x pages\n", vdso64_kbase, vdso64_pages);
748 vdso_data->dcache_block_size = L1_CACHE_BYTES;
749 vdso_data->dcache_log_block_size = L1_CACHE_SHIFT;
750 vdso_data->icache_block_size = L1_CACHE_BYTES;
751 vdso_data->icache_log_block_size = L1_CACHE_SHIFT;
752 #endif /* CONFIG_PPC64 */
756 * Calculate the size of the 32 bits vDSO
758 vdso32_pages = (&vdso32_end - &vdso32_start) >> PAGE_SHIFT;
759 DBG("vdso32_kbase: %p, 0x%x pages\n", vdso32_kbase, vdso32_pages);
763 * Setup the syscall map in the vDOS
765 vdso_setup_syscall_map();
768 * Initialize the vDSO images in memory, that is do necessary
769 * fixups of vDSO symbols, locate trampolines, etc...
772 printk(KERN_ERR "vDSO setup failure, not enabled !\n");
780 /* Make sure pages are in the correct state */
781 vdso32_pagelist = kzalloc(sizeof(struct page *) * (vdso32_pages + 2),
783 BUG_ON(vdso32_pagelist == NULL);
784 for (i = 0; i < vdso32_pages; i++) {
785 struct page *pg = virt_to_page(vdso32_kbase + i*PAGE_SIZE);
786 ClearPageReserved(pg);
788 vdso32_pagelist[i] = pg;
790 vdso32_pagelist[i++] = virt_to_page(vdso_data);
791 vdso32_pagelist[i] = NULL;
794 vdso64_pagelist = kzalloc(sizeof(struct page *) * (vdso64_pages + 2),
796 BUG_ON(vdso64_pagelist == NULL);
797 for (i = 0; i < vdso64_pages; i++) {
798 struct page *pg = virt_to_page(vdso64_kbase + i*PAGE_SIZE);
799 ClearPageReserved(pg);
801 vdso64_pagelist[i] = pg;
803 vdso64_pagelist[i++] = virt_to_page(vdso_data);
804 vdso64_pagelist[i] = NULL;
805 #endif /* CONFIG_PPC64 */
807 get_page(virt_to_page(vdso_data));
814 arch_initcall(vdso_init);
816 int in_gate_area_no_mm(unsigned long addr)
821 int in_gate_area(struct mm_struct *mm, unsigned long addr)
826 struct vm_area_struct *get_gate_vma(struct mm_struct *mm)