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 * Make sure the vDSO gets into every core dump.
269 * Dumping its contents makes post-mortem fully interpretable later
270 * without matching up the same kernel and hardware config to see
271 * what PC values meant.
273 rc = install_special_mapping(mm, vdso_base, vdso_pages << PAGE_SHIFT,
275 VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC|
279 current->mm->context.vdso_base = 0;
283 up_write(&mm->mmap_sem);
287 up_write(&mm->mmap_sem);
291 const char *arch_vma_name(struct vm_area_struct *vma)
293 if (vma->vm_mm && vma->vm_start == vma->vm_mm->context.vdso_base)
300 static void * __init find_section32(Elf32_Ehdr *ehdr, const char *secname,
307 /* Grab section headers and strings so we can tell who is who */
308 sechdrs = (void *)ehdr + ehdr->e_shoff;
309 secnames = (void *)ehdr + sechdrs[ehdr->e_shstrndx].sh_offset;
311 /* Find the section they want */
312 for (i = 1; i < ehdr->e_shnum; i++) {
313 if (strcmp(secnames+sechdrs[i].sh_name, secname) == 0) {
315 *size = sechdrs[i].sh_size;
316 return (void *)ehdr + sechdrs[i].sh_offset;
323 static Elf32_Sym * __init find_symbol32(struct lib32_elfinfo *lib,
327 char name[MAX_SYMNAME], *c;
329 for (i = 0; i < (lib->dynsymsize / sizeof(Elf32_Sym)); i++) {
330 if (lib->dynsym[i].st_name == 0)
332 strlcpy(name, lib->dynstr + lib->dynsym[i].st_name,
334 c = strchr(name, '@');
337 if (strcmp(symname, name) == 0)
338 return &lib->dynsym[i];
343 /* Note that we assume the section is .text and the symbol is relative to
346 static unsigned long __init find_function32(struct lib32_elfinfo *lib,
349 Elf32_Sym *sym = find_symbol32(lib, symname);
352 printk(KERN_WARNING "vDSO32: function %s not found !\n",
356 return sym->st_value - VDSO32_LBASE;
359 static int __init vdso_do_func_patch32(struct lib32_elfinfo *v32,
360 struct lib64_elfinfo *v64,
361 const char *orig, const char *fix)
363 Elf32_Sym *sym32_gen, *sym32_fix;
365 sym32_gen = find_symbol32(v32, orig);
366 if (sym32_gen == NULL) {
367 printk(KERN_ERR "vDSO32: Can't find symbol %s !\n", orig);
371 sym32_gen->st_name = 0;
374 sym32_fix = find_symbol32(v32, fix);
375 if (sym32_fix == NULL) {
376 printk(KERN_ERR "vDSO32: Can't find symbol %s !\n", fix);
379 sym32_gen->st_value = sym32_fix->st_value;
380 sym32_gen->st_size = sym32_fix->st_size;
381 sym32_gen->st_info = sym32_fix->st_info;
382 sym32_gen->st_other = sym32_fix->st_other;
383 sym32_gen->st_shndx = sym32_fix->st_shndx;
391 static void * __init find_section64(Elf64_Ehdr *ehdr, const char *secname,
398 /* Grab section headers and strings so we can tell who is who */
399 sechdrs = (void *)ehdr + ehdr->e_shoff;
400 secnames = (void *)ehdr + sechdrs[ehdr->e_shstrndx].sh_offset;
402 /* Find the section they want */
403 for (i = 1; i < ehdr->e_shnum; i++) {
404 if (strcmp(secnames+sechdrs[i].sh_name, secname) == 0) {
406 *size = sechdrs[i].sh_size;
407 return (void *)ehdr + sechdrs[i].sh_offset;
415 static Elf64_Sym * __init find_symbol64(struct lib64_elfinfo *lib,
419 char name[MAX_SYMNAME], *c;
421 for (i = 0; i < (lib->dynsymsize / sizeof(Elf64_Sym)); i++) {
422 if (lib->dynsym[i].st_name == 0)
424 strlcpy(name, lib->dynstr + lib->dynsym[i].st_name,
426 c = strchr(name, '@');
429 if (strcmp(symname, name) == 0)
430 return &lib->dynsym[i];
435 /* Note that we assume the section is .text and the symbol is relative to
438 static unsigned long __init find_function64(struct lib64_elfinfo *lib,
441 Elf64_Sym *sym = find_symbol64(lib, symname);
444 printk(KERN_WARNING "vDSO64: function %s not found !\n",
448 #ifdef VDS64_HAS_DESCRIPTORS
449 return *((u64 *)(vdso64_kbase + sym->st_value - VDSO64_LBASE)) -
452 return sym->st_value - VDSO64_LBASE;
456 static int __init vdso_do_func_patch64(struct lib32_elfinfo *v32,
457 struct lib64_elfinfo *v64,
458 const char *orig, const char *fix)
460 Elf64_Sym *sym64_gen, *sym64_fix;
462 sym64_gen = find_symbol64(v64, orig);
463 if (sym64_gen == NULL) {
464 printk(KERN_ERR "vDSO64: Can't find symbol %s !\n", orig);
468 sym64_gen->st_name = 0;
471 sym64_fix = find_symbol64(v64, fix);
472 if (sym64_fix == NULL) {
473 printk(KERN_ERR "vDSO64: Can't find symbol %s !\n", fix);
476 sym64_gen->st_value = sym64_fix->st_value;
477 sym64_gen->st_size = sym64_fix->st_size;
478 sym64_gen->st_info = sym64_fix->st_info;
479 sym64_gen->st_other = sym64_fix->st_other;
480 sym64_gen->st_shndx = sym64_fix->st_shndx;
485 #endif /* CONFIG_PPC64 */
488 static __init int vdso_do_find_sections(struct lib32_elfinfo *v32,
489 struct lib64_elfinfo *v64)
494 * Locate symbol tables & text section
497 v32->dynsym = find_section32(v32->hdr, ".dynsym", &v32->dynsymsize);
498 v32->dynstr = find_section32(v32->hdr, ".dynstr", NULL);
499 if (v32->dynsym == NULL || v32->dynstr == NULL) {
500 printk(KERN_ERR "vDSO32: required symbol section not found\n");
503 sect = find_section32(v32->hdr, ".text", NULL);
505 printk(KERN_ERR "vDSO32: the .text section was not found\n");
508 v32->text = sect - vdso32_kbase;
511 v64->dynsym = find_section64(v64->hdr, ".dynsym", &v64->dynsymsize);
512 v64->dynstr = find_section64(v64->hdr, ".dynstr", NULL);
513 if (v64->dynsym == NULL || v64->dynstr == NULL) {
514 printk(KERN_ERR "vDSO64: required symbol section not found\n");
517 sect = find_section64(v64->hdr, ".text", NULL);
519 printk(KERN_ERR "vDSO64: the .text section was not found\n");
522 v64->text = sect - vdso64_kbase;
523 #endif /* CONFIG_PPC64 */
528 static __init void vdso_setup_trampolines(struct lib32_elfinfo *v32,
529 struct lib64_elfinfo *v64)
532 * Find signal trampolines
536 vdso64_rt_sigtramp = find_function64(v64, "__kernel_sigtramp_rt64");
538 vdso32_sigtramp = find_function32(v32, "__kernel_sigtramp32");
539 vdso32_rt_sigtramp = find_function32(v32, "__kernel_sigtramp_rt32");
542 static __init int vdso_fixup_datapage(struct lib32_elfinfo *v32,
543 struct lib64_elfinfo *v64)
549 sym64 = find_symbol64(v64, "__kernel_datapage_offset");
551 printk(KERN_ERR "vDSO64: Can't find symbol "
552 "__kernel_datapage_offset !\n");
555 *((int *)(vdso64_kbase + sym64->st_value - VDSO64_LBASE)) =
556 (vdso64_pages << PAGE_SHIFT) -
557 (sym64->st_value - VDSO64_LBASE);
558 #endif /* CONFIG_PPC64 */
560 sym32 = find_symbol32(v32, "__kernel_datapage_offset");
562 printk(KERN_ERR "vDSO32: Can't find symbol "
563 "__kernel_datapage_offset !\n");
566 *((int *)(vdso32_kbase + (sym32->st_value - VDSO32_LBASE))) =
567 (vdso32_pages << PAGE_SHIFT) -
568 (sym32->st_value - VDSO32_LBASE);
574 static __init int vdso_fixup_features(struct lib32_elfinfo *v32,
575 struct lib64_elfinfo *v64)
578 unsigned long size32;
582 unsigned long size64;
584 start64 = find_section64(v64->hdr, "__ftr_fixup", &size64);
586 do_feature_fixups(cur_cpu_spec->cpu_features,
587 start64, start64 + size64);
589 start64 = find_section64(v64->hdr, "__mmu_ftr_fixup", &size64);
591 do_feature_fixups(cur_cpu_spec->mmu_features,
592 start64, start64 + size64);
594 start64 = find_section64(v64->hdr, "__fw_ftr_fixup", &size64);
596 do_feature_fixups(powerpc_firmware_features,
597 start64, start64 + size64);
599 start64 = find_section64(v64->hdr, "__lwsync_fixup", &size64);
601 do_lwsync_fixups(cur_cpu_spec->cpu_features,
602 start64, start64 + size64);
603 #endif /* CONFIG_PPC64 */
605 start32 = find_section32(v32->hdr, "__ftr_fixup", &size32);
607 do_feature_fixups(cur_cpu_spec->cpu_features,
608 start32, start32 + size32);
610 start32 = find_section32(v32->hdr, "__mmu_ftr_fixup", &size32);
612 do_feature_fixups(cur_cpu_spec->mmu_features,
613 start32, start32 + size32);
616 start32 = find_section32(v32->hdr, "__fw_ftr_fixup", &size32);
618 do_feature_fixups(powerpc_firmware_features,
619 start32, start32 + size32);
620 #endif /* CONFIG_PPC64 */
622 start32 = find_section32(v32->hdr, "__lwsync_fixup", &size32);
624 do_lwsync_fixups(cur_cpu_spec->cpu_features,
625 start32, start32 + size32);
630 static __init int vdso_fixup_alt_funcs(struct lib32_elfinfo *v32,
631 struct lib64_elfinfo *v64)
635 for (i = 0; i < ARRAY_SIZE(vdso_patches); i++) {
636 struct vdso_patch_def *patch = &vdso_patches[i];
637 int match = (cur_cpu_spec->cpu_features & patch->ftr_mask)
642 DBG("replacing %s with %s...\n", patch->gen_name,
643 patch->fix_name ? "NONE" : patch->fix_name);
646 * Patch the 32 bits and 64 bits symbols. Note that we do not
647 * patch the "." symbol on 64 bits.
648 * It would be easy to do, but doesn't seem to be necessary,
649 * patching the OPD symbol is enough.
651 vdso_do_func_patch32(v32, v64, patch->gen_name,
654 vdso_do_func_patch64(v32, v64, patch->gen_name,
656 #endif /* CONFIG_PPC64 */
663 static __init int vdso_setup(void)
665 struct lib32_elfinfo v32;
666 struct lib64_elfinfo v64;
668 v32.hdr = vdso32_kbase;
670 v64.hdr = vdso64_kbase;
672 if (vdso_do_find_sections(&v32, &v64))
675 if (vdso_fixup_datapage(&v32, &v64))
678 if (vdso_fixup_features(&v32, &v64))
681 if (vdso_fixup_alt_funcs(&v32, &v64))
684 vdso_setup_trampolines(&v32, &v64);
690 * Called from setup_arch to initialize the bitmap of available
691 * syscalls in the systemcfg page
693 static void __init vdso_setup_syscall_map(void)
696 extern unsigned long *sys_call_table;
697 extern unsigned long sys_ni_syscall;
700 for (i = 0; i < __NR_syscalls; i++) {
702 if (sys_call_table[i*2] != sys_ni_syscall)
703 vdso_data->syscall_map_64[i >> 5] |=
704 0x80000000UL >> (i & 0x1f);
705 if (sys_call_table[i*2+1] != sys_ni_syscall)
706 vdso_data->syscall_map_32[i >> 5] |=
707 0x80000000UL >> (i & 0x1f);
708 #else /* CONFIG_PPC64 */
709 if (sys_call_table[i] != sys_ni_syscall)
710 vdso_data->syscall_map_32[i >> 5] |=
711 0x80000000UL >> (i & 0x1f);
712 #endif /* CONFIG_PPC64 */
717 static int __init vdso_init(void)
723 * Fill up the "systemcfg" stuff for backward compatibility
725 strcpy((char *)vdso_data->eye_catcher, "SYSTEMCFG:PPC64");
726 vdso_data->version.major = SYSTEMCFG_MAJOR;
727 vdso_data->version.minor = SYSTEMCFG_MINOR;
728 vdso_data->processor = mfspr(SPRN_PVR);
730 * Fake the old platform number for pSeries and iSeries and add
731 * in LPAR bit if necessary
733 vdso_data->platform = machine_is(iseries) ? 0x200 : 0x100;
734 if (firmware_has_feature(FW_FEATURE_LPAR))
735 vdso_data->platform |= 1;
736 vdso_data->physicalMemorySize = memblock_phys_mem_size();
737 vdso_data->dcache_size = ppc64_caches.dsize;
738 vdso_data->dcache_line_size = ppc64_caches.dline_size;
739 vdso_data->icache_size = ppc64_caches.isize;
740 vdso_data->icache_line_size = ppc64_caches.iline_size;
742 /* XXXOJN: Blocks should be added to ppc64_caches and used instead */
743 vdso_data->dcache_block_size = ppc64_caches.dline_size;
744 vdso_data->icache_block_size = ppc64_caches.iline_size;
745 vdso_data->dcache_log_block_size = ppc64_caches.log_dline_size;
746 vdso_data->icache_log_block_size = ppc64_caches.log_iline_size;
749 * Calculate the size of the 64 bits vDSO
751 vdso64_pages = (&vdso64_end - &vdso64_start) >> PAGE_SHIFT;
752 DBG("vdso64_kbase: %p, 0x%x pages\n", vdso64_kbase, vdso64_pages);
754 vdso_data->dcache_block_size = L1_CACHE_BYTES;
755 vdso_data->dcache_log_block_size = L1_CACHE_SHIFT;
756 vdso_data->icache_block_size = L1_CACHE_BYTES;
757 vdso_data->icache_log_block_size = L1_CACHE_SHIFT;
758 #endif /* CONFIG_PPC64 */
762 * Calculate the size of the 32 bits vDSO
764 vdso32_pages = (&vdso32_end - &vdso32_start) >> PAGE_SHIFT;
765 DBG("vdso32_kbase: %p, 0x%x pages\n", vdso32_kbase, vdso32_pages);
769 * Setup the syscall map in the vDOS
771 vdso_setup_syscall_map();
774 * Initialize the vDSO images in memory, that is do necessary
775 * fixups of vDSO symbols, locate trampolines, etc...
778 printk(KERN_ERR "vDSO setup failure, not enabled !\n");
786 /* Make sure pages are in the correct state */
787 vdso32_pagelist = kzalloc(sizeof(struct page *) * (vdso32_pages + 2),
789 BUG_ON(vdso32_pagelist == NULL);
790 for (i = 0; i < vdso32_pages; i++) {
791 struct page *pg = virt_to_page(vdso32_kbase + i*PAGE_SIZE);
792 ClearPageReserved(pg);
794 vdso32_pagelist[i] = pg;
796 vdso32_pagelist[i++] = virt_to_page(vdso_data);
797 vdso32_pagelist[i] = NULL;
800 vdso64_pagelist = kzalloc(sizeof(struct page *) * (vdso64_pages + 2),
802 BUG_ON(vdso64_pagelist == NULL);
803 for (i = 0; i < vdso64_pages; i++) {
804 struct page *pg = virt_to_page(vdso64_kbase + i*PAGE_SIZE);
805 ClearPageReserved(pg);
807 vdso64_pagelist[i] = pg;
809 vdso64_pagelist[i++] = virt_to_page(vdso_data);
810 vdso64_pagelist[i] = NULL;
811 #endif /* CONFIG_PPC64 */
813 get_page(virt_to_page(vdso_data));
820 arch_initcall(vdso_init);
822 int in_gate_area_no_mm(unsigned long addr)
827 int in_gate_area(struct mm_struct *mm, unsigned long addr)
832 struct vm_area_struct *get_gate_vma(struct mm_struct *mm)