/*- * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 1994-1996 Søren Schmidt * Copyright (c) 2018 Turing Robotic Industries Inc. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* cpu_features / cpu_features2 (== Linux HWCAP) */ #include #include /* psl_userstatic */ #ifdef VFP #include #endif MODULE_VERSION(linux64elf, 1); #define LINUX_VDSOPAGE_SIZE PAGE_SIZE * 2 #define LINUX_VDSOPAGE (VM_MAXUSER_ADDRESS - \ LINUX_VDSOPAGE_SIZE) #define LINUX_SHAREDPAGE (LINUX_VDSOPAGE - PAGE_SIZE) /* * PAGE_SIZE - the size * of the native SHAREDPAGE */ #define LINUX_USRSTACK LINUX_SHAREDPAGE #define LINUX_PS_STRINGS (LINUX_USRSTACK - \ sizeof(struct ps_strings)) static int linux_szsigcode; static vm_object_t linux_vdso_obj; static char *linux_vdso_mapping; extern char _binary_linux_vdso_so_o_start; extern char _binary_linux_vdso_so_o_end; extern char _binary_linux_locore_o_start; extern char _binary_linux_locore_o_end; static vm_offset_t linux_vdso_base; extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL]; uintptr_t aux_vec; SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler); static void linux_set_syscall_retval(struct thread *td, int error); static int linux_fetch_syscall_args(struct thread *td); static void linux_exec_sysvec_init(void *param); static int linux_on_exec_vmspace(struct proc *p, struct image_params *imgp); static void linux_exec_setregs(struct thread *td, struct image_params *imgp, uintptr_t stack); static void linux_exec_setregs_funcdesc(struct thread *td, struct image_params *imgp, uintptr_t stack); static void linux_vdso_install(const void *param); static void linux_vdso_deinstall(const void *param); static void linux_vdso_reloc(char *mapping, Elf_Addr offset); LINUX_VDSO_SYM_INTPTR(linux_vdso_sigcode); LINUX_VDSO_SYM_CHAR(linux_platform); LINUX_VDSO_SYM_INTPTR(kern_timekeep_base); static int linux_fetch_syscall_args(struct thread *td) { struct proc *p; struct syscall_args *sa; struct trapframe *frame; //register_t *ap; p = td->td_proc; frame = td->td_frame; sa = &td->td_sa; sa->args[0] = frame->fixreg[LINUX_FIRSTARG]; sa->args[1] = frame->fixreg[LINUX_FIRSTARG+1]; sa->args[2] = frame->fixreg[LINUX_FIRSTARG+2]; sa->args[3] = frame->fixreg[LINUX_FIRSTARG+3]; sa->args[4] = frame->fixreg[LINUX_FIRSTARG+4]; sa->args[5] = frame->fixreg[LINUX_FIRSTARG+5]; sa->code = frame->fixreg[0]; sa->original_code = sa->code; // What to do with cr registers? if (sa->code >= p->p_sysent->sv_size) sa->callp = &nosys_sysent; else sa->callp = &p->p_sysent->sv_table[sa->code]; td->td_retval[0] = 0; td->td_retval[1] = frame->fixreg[LINUX_FIRSTARG+1]; return (0); } static void linux_set_syscall_retval(struct thread *td, int error) { struct trapframe *tf; if (error == EJUSTRETURN) return; tf = td->td_frame; switch (error) { case 0: tf->fixreg[LINUX_FIRSTARG] = td->td_retval[0]; tf->fixreg[LINUX_FIRSTARG + 1] = td->td_retval[1]; tf->cr &= ~0x10000000; /* Unset summary overflow */ break; case ERESTART: /* * Set user's pc back to redo the system call. */ tf->srr0 -= 4; break; default: tf->fixreg[LINUX_FIRSTARG] = bsd_to_linux_errno(error); tf->cr |= 0x10000000; /* Set summary overflow */ break; } } void linux64_arch_copyout_auxargs(struct image_params *imgp, Elf_Auxinfo **pos) { AUXARGS_ENTRY((*pos), LINUX_AT_SYSINFO_EHDR, linux_vdso_base); AUXARGS_ENTRY((*pos), LINUX_AT_HWCAP, *imgp->sysent->sv_hwcap); AUXARGS_ENTRY((*pos), LINUX_AT_HWCAP2, *imgp->sysent->sv_hwcap2); AUXARGS_ENTRY((*pos), LINUX_AT_PLATFORM, PTROUT(linux_platform)); } /* * exec_setregs() implements the FreeBSD entry contract: it reserves a stack * frame below argc and hands _start() its arguments in r3-r8. Linux's * start_thread() instead leaves r1 pointing straight at argc and zeroes the * remaining GPRs, because glibc's _start reads argc and argv off the stack. * Leaving the FreeBSD frame in place makes glibc consume the reserved words * as argc/argv and fault on the first dereference. */ static void linux_exec_setregs(struct thread *td, struct image_params *imgp, uintptr_t stack) { struct trapframe *tf; int i; tf = trapframe(td); exec_setregs(td, imgp, stack); tf->fixreg[1] = stack; for (i = 3; i <= 8; i++) tf->fixreg[i] = 0; } static void linux_exec_setregs_funcdesc(struct thread *td, struct image_params *imgp, uintptr_t stack) { struct trapframe *tf; register_t entry_desc[3]; tf = trapframe(td); linux_exec_setregs(td, imgp, stack); /* * For 64-bit ELFv1, we need to disentangle the function * descriptor * * 0. entry point * 1. TOC value (r2) * 2. Environment pointer (r11) */ (void)copyin((void *)imgp->entry_addr, entry_desc, sizeof(entry_desc)); tf->srr0 = entry_desc[0] + imgp->reloc_base; tf->fixreg[2] = entry_desc[1] + imgp->reloc_base; tf->fixreg[11] = entry_desc[2] + imgp->reloc_base; } int linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args) { struct l_sigframe *frame; struct l_sigcontext lsc; l_sigset_t lsigmask; mcontext_t mctx; sigset_t sigmask; struct trapframe *tf; int error, i; tf = td->td_frame; frame = (struct l_sigframe *)tf->fixreg[1]; /* * Restore from the Linux sigcontext the handler could have * modified, not the FreeBSD ucontext we happened to stash in * frame->f_uc. siglongjmp() and other handlers that mutate * their ucontext depend on this round trip actually taking * effect on return. */ if (copyin(&frame->sf.uc.uc_mcontext, &lsc, sizeof(lsc))) return (EFAULT); if (copyin(&frame->sf.uc.uc_sigmask, &lsigmask, sizeof(lsigmask))) return (EFAULT); /* * Translate the Linux gp_regs slots back into a FreeBSD * mcontext_t and hand it to set_mcontext(), which does the * MSR sanity check and the PCB/FPU/VSX/VEC dance. Preserve * this trapframe's psl_userstatic bits in the reconstructed * MSR so set_mcontext() accepts the context: the user is not * permitted to change privileged MSR bits via sigreturn. */ memset(&mctx, 0, sizeof(mctx)); mctx.mc_vers = _MC_VERSION; mctx.mc_len = sizeof(mctx); for (i = 0; i < 32; i++) mctx.mc_gpr[i] = lsc.gp_regs[PT_R0 + i]; mctx.mc_srr0 = lsc.gp_regs[PT_NIP]; mctx.mc_srr1 = (tf->srr1 & psl_userstatic) | (lsc.gp_regs[PT_MSR] & ~psl_userstatic); mctx.mc_ctr = lsc.gp_regs[PT_CTR]; mctx.mc_lr = lsc.gp_regs[PT_LNK]; mctx.mc_xer = lsc.gp_regs[PT_XER]; mctx.mc_cr = lsc.gp_regs[PT_CCR]; /* * FP regs are always populated in the Linux sigframe (real * values or zeros if the thread never touched FP), so always * hand them back. */ memcpy(&mctx.mc_fpreg[0], &lsc.fp_regs[0], sizeof(mctx.mc_fpreg)); mctx.mc_flags |= _MC_FP_VALID; /* * v_regs is a user pointer into the sigframe's vmx_reserve * area, or NULL if there is no vector state. When present, * read the layout populated by linux_rt_sendsig and reverse * the packing into mc_avec / mc_vscr / mc_vrsave / mc_vsxfpreg. */ if (lsc.v_regs != NULL) { struct { l_elf_vrreg_t vr[LINUX_ELF_NVRREG]; /* 34 */ uint64_t vsx_dw1[32]; } vreg_area; if (copyin(lsc.v_regs, &vreg_area, sizeof(vreg_area))) return (EFAULT); for (i = 0; i < 32; i++) memcpy(&mctx.mc_avec[i * 2], &vreg_area.vr[i], sizeof(l_elf_vrreg_t)); mctx.mc_vscr = vreg_area.vr[32].u[3]; mctx.mc_vrsave = vreg_area.vr[33].u[0]; mctx.mc_flags |= _MC_AV_VALID; for (i = 0; i < 32; i++) mctx.mc_vsxfpreg[i] = vreg_area.vsx_dw1[i]; mctx.mc_flags |= _MC_VS_VALID; } error = set_mcontext(td, &mctx); if (error != 0) return (error); linux_to_bsd_sigset(&lsigmask, &sigmask); kern_sigprocmask(td, SIG_SETMASK, &sigmask, NULL, 0); return (EJUSTRETURN); } static void linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) { struct thread *td; struct proc *p; struct trapframe *tf; struct l_sigframe *fp, *frame; struct l_rt_sigframe *l_frame; l_stack_t uc_stack; ucontext_t f_uc; struct sigacts *psp; uintptr_t uoff; int onstack, sig, issiginfo; unsigned long newsp; td = curthread; p = td->td_proc; PROC_LOCK_ASSERT(p, MA_OWNED); sig = ksi->ksi_signo; psp = p->p_sigacts; mtx_assert(&psp->ps_mtx, MA_OWNED); tf = td->td_frame; onstack = sigonstack(tf->fixreg[1]); issiginfo = SIGISMEMBER(psp->ps_siginfo, sig); fp = (struct l_sigframe *)td->td_frame->fixreg[1]; /* sizeof(*fp) is a multiple of 16, so this keeps r1 quadword aligned. */ fp--; mtx_unlock(&psp->ps_mtx); PROC_UNLOCK(td->td_proc); /* * f_uc is copied out as part of the frame, so zero the members * left untouched below rather than leaking kernel stack. */ bzero(&f_uc, sizeof(f_uc)); get_mcontext(td, &f_uc.uc_mcontext, 0); PROC_LOCK(p); mtx_lock(&psp->ps_mtx); f_uc.uc_sigmask = *mask; uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp); uc_stack.ss_size = td->td_sigstk.ss_size; uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) != 0 ? (onstack ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE; mtx_unlock(&psp->ps_mtx); PROC_UNLOCK(td->td_proc); /* Fill in the frame to copy out */ frame = malloc(sizeof(*frame), M_LINUX, M_WAITOK | M_ZERO); memcpy(&frame->f_uc, &f_uc, sizeof(f_uc)); l_frame = (struct l_rt_sigframe*)frame; /* * The frame is built in a staging copy and copied out to fp, so * every pointer it hands back to userland has to be translated from * the staging address to the corresponding address in fp. */ uoff = (uintptr_t)fp - (uintptr_t)frame; l_frame->pinfo = (l_siginfo_t *)((uintptr_t)&l_frame->info + uoff); l_frame->puc = (void *)((uintptr_t)&l_frame->uc + uoff); /* Create the l_ucontext */ l_frame->uc.uc_flags = 0; memcpy(&l_frame->uc.uc_stack, &uc_stack, sizeof(uc_stack)); l_frame->uc.uc_link = 0; /* gp_regs[] uses Linux ppc64's slot order, not the trapframe's. */ l_frame->uc.uc_mcontext.regs = (struct l_user_pt_regs *) ((uintptr_t)&l_frame->uc.uc_mcontext.gp_regs + uoff); for (int i = 0; i < 32; i++) l_frame->uc.uc_mcontext.gp_regs[PT_R0 + i] = tf->fixreg[i]; l_frame->uc.uc_mcontext.gp_regs[PT_NIP] = tf->srr0; l_frame->uc.uc_mcontext.gp_regs[PT_MSR] = tf->srr1; l_frame->uc.uc_mcontext.gp_regs[PT_CTR] = tf->ctr; l_frame->uc.uc_mcontext.gp_regs[PT_LNK] = tf->lr; l_frame->uc.uc_mcontext.gp_regs[PT_XER] = tf->xer; l_frame->uc.uc_mcontext.gp_regs[PT_CCR] = tf->cr; l_frame->uc.uc_mcontext.gp_regs[PT_SOFTE] = 1; /* * FP registers: fp_regs[0..31] hold the low doubleword of each * VSR[0..31] (i.e. the traditional FPR view); fp_regs[32] is * FPSCR. FreeBSD's mc_fpreg[] uses the same layout, so this * is a straight copy when the thread has valid FP state. */ if (f_uc.uc_mcontext.mc_flags & _MC_FP_VALID) { memcpy(&l_frame->uc.uc_mcontext.fp_regs[0], &f_uc.uc_mcontext.mc_fpreg[0], sizeof(l_frame->uc.uc_mcontext.fp_regs)); } /* * VMX/VSX save. Linux places the vector state after the * fp_regs in an "unstructured" long[] area (vmx_reserve) with * v_regs pointing at the first quadword-aligned position: * * v_regs[0..31] : VR[0..31] (16 bytes each) * v_regs[32] : vscr as the last word of the quadword * v_regs[33] : vrsave as the first word of the quadword * after that : VSR[0..31] high doublewords (32 * 8 bytes) * * If the thread has no live VMX state, hand userland a NULL * v_regs (per Linux's ABI contract). */ if (f_uc.uc_mcontext.mc_flags & _MC_AV_VALID) { uintptr_t k_vmx = (uintptr_t)&frame->sf.uc.uc_mcontext.vmx_reserve[0]; uintptr_t k_vregs = roundup2(k_vmx, 16); l_elf_vrreg_t *kv = (l_elf_vrreg_t *)k_vregs; int i; l_frame->uc.uc_mcontext.v_regs = (l_elf_vrreg_t *)(k_vregs + uoff); for (i = 0; i < 32; i++) memcpy(&kv[i], &f_uc.uc_mcontext.mc_avec[i * 2], sizeof(l_elf_vrreg_t)); memset(&kv[32], 0, sizeof(l_elf_vrreg_t)); kv[32].u[3] = f_uc.uc_mcontext.mc_vscr; memset(&kv[33], 0, sizeof(l_elf_vrreg_t)); kv[33].u[0] = f_uc.uc_mcontext.mc_vrsave; if (f_uc.uc_mcontext.mc_flags & _MC_VS_VALID) { uint64_t *vsx_dw1 = (uint64_t *)&kv[34]; for (i = 0; i < 32; i++) vsx_dw1[i] = f_uc.uc_mcontext.mc_vsxfpreg[i]; } } else { l_frame->uc.uc_mcontext.v_regs = NULL; } sig = bsd_to_linux_signal(sig); l_frame->uc.uc_mcontext.signal = sig; l_frame->uc.uc_mcontext.handler = (unsigned long)catcher; bsd_to_linux_sigset(mask, &l_frame->uc.uc_sigmask); siginfo_to_lsiginfo(&ksi->ksi_info, &l_frame->info, sig); /* Might need to make sure signal handler doesn't get spurious FP exceptions? */ /* Copy the sigframe out to the user's stack. */ if (copyout(frame, fp, sizeof(*fp)) != 0) { /* Process has trashed its stack. Kill it. */ free(frame, M_LINUX); CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp); PROC_LOCK(p); sigexit(td, SIGILL); } free(frame, M_LINUX); /* Set up to return from userspace. */ tf->srr0 = (register_t)linux_vdso_sigcode; /* Allocate a dummy caller frame for the signal handler. */ newsp = (unsigned long)fp - LINUX__SIGNAL_FRAMESIZE; /* For ELFv1 */ /* Handler is *really* a pointer to the function descriptor for * the signal routine. The first entry in the function * descriptor is the entry address of signal and the second * entry is the TOC value we need to use. */ struct l_func_desc *ptr = (struct l_func_desc*)catcher; tf->ctr = (register_t)ptr->addr; tf->fixreg[2] = (register_t)ptr->toc; tf->fixreg[1] = (register_t)newsp; tf->fixreg[3] = (register_t)sig; if (issiginfo) { tf->fixreg[4] = (register_t)&fp->sf.info; tf->fixreg[5] = (register_t)&fp->sf.uc; tf->fixreg[6] = (register_t)&fp->sf; } else { tf->fixreg[4] = (register_t)&fp->sf.uc.uc_mcontext; } PROC_LOCK(p); mtx_lock(&psp->ps_mtx); } struct sysentvec elf_linux_sysvec = { .sv_size = LINUX_SYS_MAXSYSCALL, .sv_table = linux_sysent, .sv_fixup = __elfN(freebsd_fixup), .sv_sendsig = linux_rt_sendsig, .sv_sigcode = &_binary_linux_vdso_so_o_start, .sv_szsigcode = &linux_szsigcode, .sv_name = "Linux ELF64", .sv_coredump = elf64_coredump, .sv_elf_core_osabi = ELFOSABI_NONE, .sv_elf_core_abi_vendor = LINUX_ABI_VENDOR, .sv_elf_core_prepare_notes = linux64_prepare_notes, .sv_minsigstksz = LINUX_MINSIGSTKSZ, .sv_minuser = VM_MIN_ADDRESS, .sv_maxuser = VM_MAXUSER_ADDRESS, .sv_usrstack = LINUX_USRSTACK, .sv_psstrings = LINUX_PS_STRINGS, .sv_psstringssz = sizeof(struct ps_strings), .sv_stackprot = VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE, /* Enabling the execution of stack temporarily for signal trampoline. */ .sv_copyout_auxargs = __linuxN(copyout_auxargs), .sv_copyout_strings = __linuxN(copyout_strings), .sv_setregs = linux_exec_setregs_funcdesc, .sv_fixlimit = NULL, .sv_maxssiz = NULL, .sv_flags = SV_ABI_LINUX | SV_LP64 | SV_SHP | SV_SIG_DISCIGN | SV_SIG_WAITNDQ | SV_TIMEKEEP, .sv_set_syscall_retval = linux_set_syscall_retval, .sv_fetch_syscall_args = linux_fetch_syscall_args, .sv_syscallnames = NULL, .sv_shared_page_base = LINUX_SHAREDPAGE, .sv_shared_page_len = PAGE_SIZE, .sv_schedtail = linux_schedtail, .sv_thread_detach = linux_thread_detach, .sv_trap = NULL, .sv_hwcap = &cpu_features, .sv_hwcap2 = &cpu_features2, .sv_onexec = linux_on_exec_vmspace, .sv_onexit = linux_on_exit, .sv_ontdexit = linux_thread_dtor, .sv_setid_allowed = &linux_setid_allowed_query, }; struct sysentvec elf64_linux_sysvec_v2 = { .sv_size = LINUX_SYS_MAXSYSCALL, .sv_table = linux_sysent, .sv_fixup = __elfN(freebsd_fixup), .sv_sendsig = linux_rt_sendsig, .sv_sigcode = &_binary_linux_vdso_so_o_start, .sv_szsigcode = &linux_szsigcode, .sv_name = "Linux ELF64 V2", .sv_coredump = __elfN(coredump), .sv_elf_core_osabi = ELFOSABI_NONE, .sv_elf_core_abi_vendor = LINUX_ABI_VENDOR, .sv_elf_core_prepare_notes = linux64_prepare_notes, .sv_minsigstksz = MINSIGSTKSZ, .sv_minuser = VM_MIN_ADDRESS, .sv_maxuser = VM_MAXUSER_ADDRESS, .sv_usrstack = LINUX_USRSTACK, .sv_psstrings = LINUX_PS_STRINGS, .sv_psstringssz = sizeof(struct ps_strings), .sv_stackprot = VM_PROT_ALL, .sv_copyout_auxargs = linux64_copyout_auxargs, .sv_copyout_strings = linux64_copyout_strings, .sv_setregs = linux_exec_setregs, .sv_fixlimit = NULL, .sv_maxssiz = NULL, .sv_flags = SV_ABI_LINUX | SV_LP64 | SV_SHP | SV_SIG_DISCIGN | SV_SIG_WAITNDQ | SV_TIMEKEEP, .sv_set_syscall_retval = linux_set_syscall_retval, .sv_fetch_syscall_args = linux_fetch_syscall_args, .sv_syscallnames = NULL, .sv_shared_page_base = LINUX_SHAREDPAGE, .sv_shared_page_len = PAGE_SIZE, .sv_schedtail = linux_schedtail, .sv_thread_detach = linux_thread_detach, .sv_trap = NULL, .sv_hwcap = &cpu_features, .sv_hwcap2 = &cpu_features2, .sv_onexec = linux_on_exec_vmspace, .sv_onexit = linux_on_exit, .sv_ontdexit = linux_thread_dtor, .sv_setid_allowed = &linux_setid_allowed_query, }; static int linux_on_exec_vmspace(struct proc *p, struct image_params *imgp) { int error; error = linux_map_vdso(p, linux_vdso_obj, linux_vdso_base, LINUX_VDSOPAGE_SIZE, imgp); if (error == 0) linux_on_exec(p, imgp); return (0); } /* * linux_vdso_install() and linux_exec_sysvec_init() must be called * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY). * * The ELFv1 and ELFv2 sysvecs share one vDSO image, so both SYSINITs * write the same kern_timekeep_base. That is intentional. */ static void linux_exec_sysvec_init(void *param) { l_uintptr_t *ktimekeep_base; struct sysentvec *sv; ptrdiff_t tkoff; sv = param; exec_sysvec_init(sv); tkoff = kern_timekeep_base - linux_vdso_base; ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff); *ktimekeep_base = sv->sv_shared_page_base + sv->sv_timekeep_offset; } SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY, linux_exec_sysvec_init, &elf_linux_sysvec); SYSINIT(elf_linux_exec_sysvec_init_v2, SI_SUB_EXEC + 1, SI_ORDER_ANY, linux_exec_sysvec_init, &elf64_linux_sysvec_v2); static void linux_vdso_install(const void *param) { char *vdso_start = &_binary_linux_vdso_so_o_start; char *vdso_end = &_binary_linux_vdso_so_o_end; linux_szsigcode = vdso_end - vdso_start; MPASS(linux_szsigcode <= LINUX_VDSOPAGE_SIZE); linux_vdso_base = LINUX_VDSOPAGE; __elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base); linux_vdso_obj = __elfN(linux_shared_page_init) (&linux_vdso_mapping, LINUX_VDSOPAGE_SIZE); bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode); linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base); } SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST, linux_vdso_install, NULL); static void linux_vdso_deinstall(const void *param) { __elfN(linux_shared_page_fini)(linux_vdso_obj, linux_vdso_mapping, LINUX_VDSOPAGE_SIZE); } SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST, linux_vdso_deinstall, NULL); static void linux_vdso_reloc(char *mapping, Elf_Addr offset) { Elf_Size rtype, symidx; const Elf_Rela *rela; const Elf_Shdr *shdr; const Elf_Ehdr *ehdr; Elf_Addr *where; Elf_Addr addr, addend; int i, relacnt; MPASS(offset != 0); relacnt = 0; ehdr = (const Elf_Ehdr *)mapping; shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff); for (i = 0; i < ehdr->e_shnum; i++) { switch (shdr[i].sh_type) { case SHT_REL: printf("Linux Powerpc64 vDSO: unexpected Rel section\n"); break; case SHT_RELA: rela = (const Elf_Rela *)(mapping + shdr[i].sh_offset); relacnt = shdr[i].sh_size / sizeof(*rela); } } for (i = 0; i < relacnt; i++, rela++) { where = (Elf_Addr *)(mapping + rela->r_offset); addend = rela->r_addend; rtype = ELF_R_TYPE(rela->r_info); symidx = ELF_R_SYM(rela->r_info); switch (rtype) { case R_PPC_NONE: /* none */ break; case R_PPC_RELATIVE: /* B + A */ addr = (Elf_Addr)(mapping + addend); if (*where != addr) *where = addr; break; default: printf("Linux Powerpc64 vDSO: unexpected relocation type %ld, " "symbol index %ld\n", rtype, symidx); } } } static bool linux64_elfv1_header_match(const struct image_params *, const int32_t *, const uint32_t *); static bool linux64_elfv2_header_match(const struct image_params *, const int32_t *, const uint32_t *); static Elf_Brandnote linux64_brandnote = { .hdr.n_namesz = sizeof(GNU_ABI_VENDOR), .hdr.n_descsz = 16, .hdr.n_type = 1, .vendor = GNU_ABI_VENDOR, .flags = BN_TRANSLATE_OSREL, .trans_osrel = linux_trans_osrel }; static Elf64_Brandinfo linux_glibc2brand = { .brand = ELFOSABI_LINUX, .machine = EM_PPC64, .compat_3_brand = "Linux", .interp_path = "/lib64/ld64.so.1", .sysvec = &elf_linux_sysvec, .interp_newpath = NULL, .brand_note = &linux64_brandnote, .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE, .header_supported = &linux64_elfv1_header_match }; static Elf64_Brandinfo linux_brand_info_elfv2 = { .brand = ELFOSABI_LINUX, .machine = EM_PPC64, .compat_3_brand = "Linux", .interp_path = "/lib64/ld64.so.2", .sysvec = &elf64_linux_sysvec_v2, .interp_newpath = NULL, .brand_note = &linux64_brandnote, .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE, .header_supported = &linux64_elfv2_header_match }; Elf64_Brandinfo *linux_brandlist[] = { &linux_glibc2brand, &linux_brand_info_elfv2, NULL }; static bool linux64_elfv1_header_match(const struct image_params *params, const int32_t *osrel __unused, const uint32_t *fctl0 __unused) { const Elf64_Ehdr *hdr = (const Elf64_Ehdr *)params->image_header; int abi = (hdr->e_flags & 3); return (abi == 0 || abi == 1); } static bool linux64_elfv2_header_match(const struct image_params *params, const int32_t *osrel __unused, const uint32_t *fctl0 __unused) { const Elf64_Ehdr *hdr = (const Elf64_Ehdr *)params->image_header; int abi = (hdr->e_flags & 3); return (abi == 2); } static int linux64_elf_modevent(module_t mod, int type, void *data) { Elf64_Brandinfo **brandinfo; struct linux_ioctl_handler**lihp; int error; error = 0; switch(type) { case MOD_LOAD: for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; ++brandinfo) if (elf64_insert_brand_entry(*brandinfo) < 0) error = EINVAL; if (error == 0) { SET_FOREACH(lihp, linux_ioctl_handler_set) linux_ioctl_register_handler(*lihp); stclohz = (stathz ? stathz : hz); if (bootverbose) printf("Linux powerpc64 ELF exec handler installed\n"); } break; case MOD_UNLOAD: for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; ++brandinfo) if (elf64_brand_inuse(*brandinfo)) error = EBUSY; if (error == 0) { for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; ++brandinfo) if (elf64_remove_brand_entry(*brandinfo) < 0) error = EINVAL; } if (error == 0) { SET_FOREACH(lihp, linux_ioctl_handler_set) linux_ioctl_unregister_handler(*lihp); if (bootverbose) printf("Linux powerpc64 ELF exec handler removed\n"); } else printf("Could not deinstall Linux powerpc64 ELF interpreter entry\n"); break; default: return (EOPNOTSUPP); } return (error); } static moduledata_t linux64_elf_mod = { "linux64elf", linux64_elf_modevent, 0 }; DECLARE_MODULE_TIED(linux64elf, linux64_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY); MODULE_DEPEND(linux64elf, linux_common, 1, 1, 1); FEATURE(linux64, "Powerpc64 Linux 64bit support");