Path: blob/main/sys/amd64/linux32/linux32_sysvec.c
39536 views
/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 2004 Tim J. Robbins4* Copyright (c) 2003 Peter Wemm5* Copyright (c) 2002 Doug Rabson6* Copyright (c) 1998-1999 Andrew Gallatin7* Copyright (c) 1994-1996 Søren Schmidt8* All rights reserved.9*10* Redistribution and use in source and binary forms, with or without11* modification, are permitted provided that the following conditions12* are met:13* 1. Redistributions of source code must retain the above copyright14* notice, this list of conditions and the following disclaimer15* in this position and unchanged.16* 2. Redistributions in binary form must reproduce the above copyright17* notice, this list of conditions and the following disclaimer in the18* documentation and/or other materials provided with the distribution.19* 3. The name of the author may not be used to endorse or promote products20* derived from this software without specific prior written permission21*22* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR23* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES24* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.25* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,26* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT27* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,28* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY29* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT30* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF31* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.32*/3334#ifndef COMPAT_FREEBSD3235#error "Unable to compile Linux-emulator due to missing COMPAT_FREEBSD32 option!"36#endif3738#define __ELF_WORD_SIZE 323940#include <sys/param.h>41#include <sys/exec.h>42#include <sys/imgact.h>43#include <sys/imgact_elf.h>44#include <sys/kernel.h>45#include <sys/lock.h>46#include <sys/module.h>47#include <sys/mutex.h>48#include <sys/proc.h>49#include <sys/stddef.h>50#include <sys/syscallsubr.h>51#include <sys/sysctl.h>52#include <sys/sysent.h>5354#include <vm/pmap.h>55#include <vm/vm.h>56#include <vm/vm_map.h>57#include <vm/vm_param.h>5859#include <machine/md_var.h>60#include <machine/trap.h>6162#include <x86/linux/linux_x86.h>63#include <amd64/linux32/linux.h>64#include <amd64/linux32/linux32_proto.h>65#include <compat/linux/linux_elf.h>66#include <compat/linux/linux_emul.h>67#include <compat/linux/linux_fork.h>68#include <compat/linux/linux_ioctl.h>69#include <compat/linux/linux_mib.h>70#include <compat/linux/linux_misc.h>71#include <compat/linux/linux_signal.h>72#include <compat/linux/linux_util.h>73#include <compat/linux/linux_vdso.h>7475#include <x86/linux/linux_x86_sigframe.h>7677MODULE_VERSION(linux, 1);7879#define LINUX32_MAXUSER ((1ul << 32) - PAGE_SIZE)80#define LINUX32_VDSOPAGE_SIZE PAGE_SIZE * 281#define LINUX32_VDSOPAGE (LINUX32_MAXUSER - LINUX32_VDSOPAGE_SIZE)82#define LINUX32_SHAREDPAGE (LINUX32_VDSOPAGE - PAGE_SIZE)83/*84* PAGE_SIZE - the size85* of the native SHAREDPAGE86*/87#define LINUX32_USRSTACK LINUX32_SHAREDPAGE8889static int linux_szsigcode;90static vm_object_t linux_vdso_obj;91static char *linux_vdso_mapping;92extern char _binary_linux32_vdso_so_o_start;93extern char _binary_linux32_vdso_so_o_end;94static vm_offset_t linux_vdso_base;9596extern struct sysent linux32_sysent[LINUX32_SYS_MAXSYSCALL];97extern const char *linux32_syscallnames[];9899SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);100101static int linux_copyout_strings(struct image_params *imgp,102uintptr_t *stack_base);103static void linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask);104static void linux_exec_setregs(struct thread *td,105struct image_params *imgp, uintptr_t stack);106static void linux_exec_sysvec_init(void *param);107static int linux_on_exec_vmspace(struct proc *p,108struct image_params *imgp);109static void linux32_fixlimit(struct rlimit *rl, int which);110static void linux_vdso_install(const void *param);111static void linux_vdso_deinstall(const void *param);112static void linux_vdso_reloc(char *mapping, Elf_Addr offset);113static void linux32_set_fork_retval(struct thread *td);114static void linux32_set_syscall_retval(struct thread *td, int error);115116struct linux32_ps_strings {117u_int32_t ps_argvstr; /* first of 0 or more argument strings */118u_int ps_nargvstr; /* the number of argument strings */119u_int32_t ps_envstr; /* first of 0 or more environment strings */120u_int ps_nenvstr; /* the number of environment strings */121};122#define LINUX32_PS_STRINGS (LINUX32_USRSTACK - \123sizeof(struct linux32_ps_strings))124125LINUX_VDSO_SYM_INTPTR(__kernel_vsyscall);126LINUX_VDSO_SYM_INTPTR(linux32_vdso_sigcode);127LINUX_VDSO_SYM_INTPTR(linux32_vdso_rt_sigcode);128LINUX_VDSO_SYM_INTPTR(kern_timekeep_base);129LINUX_VDSO_SYM_INTPTR(kern_tsc_selector);130LINUX_VDSO_SYM_INTPTR(kern_cpu_selector);131LINUX_VDSO_SYM_CHAR(linux_platform);132133void134linux32_arch_copyout_auxargs(struct image_params *imgp, Elf_Auxinfo **pos)135{136137AUXARGS_ENTRY((*pos), LINUX_AT_SYSINFO, __kernel_vsyscall);138AUXARGS_ENTRY((*pos), LINUX_AT_SYSINFO_EHDR, linux_vdso_base);139AUXARGS_ENTRY((*pos), LINUX_AT_HWCAP, cpu_feature);140AUXARGS_ENTRY((*pos), LINUX_AT_HWCAP2, linux_x86_elf_hwcap2());141AUXARGS_ENTRY((*pos), LINUX_AT_PLATFORM, PTROUT(linux_platform));142}143144static void145linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)146{147struct thread *td = curthread;148struct proc *p = td->td_proc;149struct sigacts *psp;150struct trapframe *regs;151struct l_rt_sigframe *fp, frame;152int oonstack;153int sig;154int code;155156sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno);157code = ksi->ksi_code;158PROC_LOCK_ASSERT(p, MA_OWNED);159psp = p->p_sigacts;160mtx_assert(&psp->ps_mtx, MA_OWNED);161regs = td->td_frame;162oonstack = sigonstack(regs->tf_rsp);163164/* Allocate space for the signal handler context. */165if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&166SIGISMEMBER(psp->ps_sigonstack, sig)) {167fp = (struct l_rt_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +168td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe));169} else170fp = (struct l_rt_sigframe *)regs->tf_rsp - 1;171mtx_unlock(&psp->ps_mtx);172173/* Build the argument list for the signal handler. */174sig = bsd_to_linux_signal(sig);175176bzero(&frame, sizeof(frame));177178frame.sf_sig = sig;179frame.sf_siginfo = PTROUT(&fp->sf_si);180frame.sf_ucontext = PTROUT(&fp->sf_uc);181182/* Fill in POSIX parts. */183siginfo_to_lsiginfo(&ksi->ksi_info, &frame.sf_si, sig);184185/*186* Build the signal context to be used by sigreturn and libgcc unwind.187*/188frame.sf_uc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);189frame.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;190frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)191? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;192PROC_UNLOCK(p);193194bsd_to_linux_sigset(mask, &frame.sf_uc.uc_sigmask);195196frame.sf_uc.uc_mcontext.sc_mask = frame.sf_uc.uc_sigmask.__mask;197frame.sf_uc.uc_mcontext.sc_edi = regs->tf_rdi;198frame.sf_uc.uc_mcontext.sc_esi = regs->tf_rsi;199frame.sf_uc.uc_mcontext.sc_ebp = regs->tf_rbp;200frame.sf_uc.uc_mcontext.sc_ebx = regs->tf_rbx;201frame.sf_uc.uc_mcontext.sc_esp = regs->tf_rsp;202frame.sf_uc.uc_mcontext.sc_edx = regs->tf_rdx;203frame.sf_uc.uc_mcontext.sc_ecx = regs->tf_rcx;204frame.sf_uc.uc_mcontext.sc_eax = regs->tf_rax;205frame.sf_uc.uc_mcontext.sc_eip = regs->tf_rip;206frame.sf_uc.uc_mcontext.sc_cs = regs->tf_cs;207frame.sf_uc.uc_mcontext.sc_gs = regs->tf_gs;208frame.sf_uc.uc_mcontext.sc_fs = regs->tf_fs;209frame.sf_uc.uc_mcontext.sc_es = regs->tf_es;210frame.sf_uc.uc_mcontext.sc_ds = regs->tf_ds;211frame.sf_uc.uc_mcontext.sc_eflags = regs->tf_rflags;212frame.sf_uc.uc_mcontext.sc_esp_at_signal = regs->tf_rsp;213frame.sf_uc.uc_mcontext.sc_ss = regs->tf_ss;214frame.sf_uc.uc_mcontext.sc_err = regs->tf_err;215frame.sf_uc.uc_mcontext.sc_cr2 = (u_int32_t)(uintptr_t)ksi->ksi_addr;216frame.sf_uc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);217218if (copyout(&frame, fp, sizeof(frame)) != 0) {219/*220* Process has trashed its stack; give it an illegal221* instruction to halt it in its tracks.222*/223PROC_LOCK(p);224sigexit(td, SIGILL);225}226227/* Build context to run handler in. */228regs->tf_rsp = PTROUT(fp);229regs->tf_rip = linux32_vdso_rt_sigcode;230regs->tf_rdi = PTROUT(catcher);231regs->tf_rflags &= ~(PSL_T | PSL_D);232regs->tf_cs = _ucode32sel;233regs->tf_ss = _udatasel;234regs->tf_ds = _udatasel;235regs->tf_es = _udatasel;236regs->tf_fs = _ufssel;237regs->tf_gs = _ugssel;238regs->tf_flags = TF_HASSEGS;239set_pcb_flags(td->td_pcb, PCB_FULL_IRET);240PROC_LOCK(p);241mtx_lock(&psp->ps_mtx);242}243244/*245* Send an interrupt to process.246*247* Stack is set up to allow sigcode stored248* in u. to call routine, followed by kcall249* to sigreturn routine below. After sigreturn250* resets the signal mask, the stack, and the251* frame pointer, it returns to the user252* specified pc, psl.253*/254static void255linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)256{257struct thread *td = curthread;258struct proc *p = td->td_proc;259struct sigacts *psp;260struct trapframe *regs;261struct l_sigframe *fp, frame;262l_sigset_t lmask;263int oonstack;264int sig, code;265266sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno);267code = ksi->ksi_code;268PROC_LOCK_ASSERT(p, MA_OWNED);269psp = p->p_sigacts;270mtx_assert(&psp->ps_mtx, MA_OWNED);271if (SIGISMEMBER(psp->ps_siginfo, sig)) {272/* Signal handler installed with SA_SIGINFO. */273linux_rt_sendsig(catcher, ksi, mask);274return;275}276277regs = td->td_frame;278oonstack = sigonstack(regs->tf_rsp);279280/* Allocate space for the signal handler context. */281if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&282SIGISMEMBER(psp->ps_sigonstack, sig)) {283fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +284td->td_sigstk.ss_size - sizeof(struct l_sigframe));285} else286fp = (struct l_sigframe *)regs->tf_rsp - 1;287mtx_unlock(&psp->ps_mtx);288PROC_UNLOCK(p);289290/* Build the argument list for the signal handler. */291sig = bsd_to_linux_signal(sig);292293bzero(&frame, sizeof(frame));294295frame.sf_sig = sig;296frame.sf_sigmask = *mask;297bsd_to_linux_sigset(mask, &lmask);298299/* Build the signal context to be used by sigreturn. */300frame.sf_sc.sc_mask = lmask.__mask;301frame.sf_sc.sc_gs = regs->tf_gs;302frame.sf_sc.sc_fs = regs->tf_fs;303frame.sf_sc.sc_es = regs->tf_es;304frame.sf_sc.sc_ds = regs->tf_ds;305frame.sf_sc.sc_edi = regs->tf_rdi;306frame.sf_sc.sc_esi = regs->tf_rsi;307frame.sf_sc.sc_ebp = regs->tf_rbp;308frame.sf_sc.sc_ebx = regs->tf_rbx;309frame.sf_sc.sc_esp = regs->tf_rsp;310frame.sf_sc.sc_edx = regs->tf_rdx;311frame.sf_sc.sc_ecx = regs->tf_rcx;312frame.sf_sc.sc_eax = regs->tf_rax;313frame.sf_sc.sc_eip = regs->tf_rip;314frame.sf_sc.sc_cs = regs->tf_cs;315frame.sf_sc.sc_eflags = regs->tf_rflags;316frame.sf_sc.sc_esp_at_signal = regs->tf_rsp;317frame.sf_sc.sc_ss = regs->tf_ss;318frame.sf_sc.sc_err = regs->tf_err;319frame.sf_sc.sc_cr2 = (u_int32_t)(uintptr_t)ksi->ksi_addr;320frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(code);321322if (copyout(&frame, fp, sizeof(frame)) != 0) {323/*324* Process has trashed its stack; give it an illegal325* instruction to halt it in its tracks.326*/327PROC_LOCK(p);328sigexit(td, SIGILL);329}330331/* Build context to run handler in. */332regs->tf_rsp = PTROUT(fp);333regs->tf_rip = linux32_vdso_sigcode;334regs->tf_rdi = PTROUT(catcher);335regs->tf_rflags &= ~(PSL_T | PSL_D);336regs->tf_cs = _ucode32sel;337regs->tf_ss = _udatasel;338regs->tf_ds = _udatasel;339regs->tf_es = _udatasel;340regs->tf_fs = _ufssel;341regs->tf_gs = _ugssel;342regs->tf_flags = TF_HASSEGS;343set_pcb_flags(td->td_pcb, PCB_FULL_IRET);344PROC_LOCK(p);345mtx_lock(&psp->ps_mtx);346}347348/*349* System call to cleanup state after a signal350* has been taken. Reset signal mask and351* stack state from context left by sendsig (above).352* Return to previous pc and psl as specified by353* context left by sendsig. Check carefully to354* make sure that the user has not modified the355* psl to gain improper privileges or to cause356* a machine fault.357*/358int359linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args)360{361struct l_sigframe frame;362struct trapframe *regs;363int eflags;364ksiginfo_t ksi;365366regs = td->td_frame;367368/*369* The trampoline code hands us the sigframe.370* It is unsafe to keep track of it ourselves, in the event that a371* program jumps out of a signal handler.372*/373if (copyin(args->sfp, &frame, sizeof(frame)) != 0)374return (EFAULT);375376/* Check for security violations. */377eflags = frame.sf_sc.sc_eflags;378if (!EFL_SECURE(eflags, regs->tf_rflags))379return(EINVAL);380381/*382* Don't allow users to load a valid privileged %cs. Let the383* hardware check for invalid selectors, excess privilege in384* other selectors, invalid %eip's and invalid %esp's.385*/386if (!CS_SECURE(frame.sf_sc.sc_cs)) {387ksiginfo_init_trap(&ksi);388ksi.ksi_signo = SIGBUS;389ksi.ksi_code = BUS_OBJERR;390ksi.ksi_trapno = T_PROTFLT;391ksi.ksi_addr = (void *)regs->tf_rip;392trapsignal(td, &ksi);393return(EINVAL);394}395396kern_sigprocmask(td, SIG_SETMASK, &frame.sf_sigmask, NULL, 0);397398/* Restore signal context. */399regs->tf_rdi = frame.sf_sc.sc_edi;400regs->tf_rsi = frame.sf_sc.sc_esi;401regs->tf_rbp = frame.sf_sc.sc_ebp;402regs->tf_rbx = frame.sf_sc.sc_ebx;403regs->tf_rdx = frame.sf_sc.sc_edx;404regs->tf_rcx = frame.sf_sc.sc_ecx;405regs->tf_rax = frame.sf_sc.sc_eax;406regs->tf_rip = frame.sf_sc.sc_eip;407regs->tf_cs = frame.sf_sc.sc_cs;408regs->tf_ds = frame.sf_sc.sc_ds;409regs->tf_es = frame.sf_sc.sc_es;410regs->tf_fs = frame.sf_sc.sc_fs;411regs->tf_gs = frame.sf_sc.sc_gs;412regs->tf_rflags = eflags;413regs->tf_rsp = frame.sf_sc.sc_esp_at_signal;414regs->tf_ss = frame.sf_sc.sc_ss;415set_pcb_flags(td->td_pcb, PCB_FULL_IRET);416417return (EJUSTRETURN);418}419420/*421* System call to cleanup state after a signal422* has been taken. Reset signal mask and423* stack state from context left by rt_sendsig (above).424* Return to previous pc and psl as specified by425* context left by sendsig. Check carefully to426* make sure that the user has not modified the427* psl to gain improper privileges or to cause428* a machine fault.429*/430int431linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)432{433struct l_ucontext uc;434struct l_sigcontext *context;435sigset_t bmask;436l_stack_t *lss;437stack_t ss;438struct trapframe *regs;439int eflags;440ksiginfo_t ksi;441442regs = td->td_frame;443444/*445* The trampoline code hands us the ucontext.446* It is unsafe to keep track of it ourselves, in the event that a447* program jumps out of a signal handler.448*/449if (copyin(args->ucp, &uc, sizeof(uc)) != 0)450return (EFAULT);451452context = &uc.uc_mcontext;453454/* Check for security violations. */455eflags = context->sc_eflags;456if (!EFL_SECURE(eflags, regs->tf_rflags))457return(EINVAL);458459/*460* Don't allow users to load a valid privileged %cs. Let the461* hardware check for invalid selectors, excess privilege in462* other selectors, invalid %eip's and invalid %esp's.463*/464if (!CS_SECURE(context->sc_cs)) {465ksiginfo_init_trap(&ksi);466ksi.ksi_signo = SIGBUS;467ksi.ksi_code = BUS_OBJERR;468ksi.ksi_trapno = T_PROTFLT;469ksi.ksi_addr = (void *)regs->tf_rip;470trapsignal(td, &ksi);471return(EINVAL);472}473474linux_to_bsd_sigset(&uc.uc_sigmask, &bmask);475kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);476477/*478* Restore signal context479*/480regs->tf_gs = context->sc_gs;481regs->tf_fs = context->sc_fs;482regs->tf_es = context->sc_es;483regs->tf_ds = context->sc_ds;484regs->tf_rdi = context->sc_edi;485regs->tf_rsi = context->sc_esi;486regs->tf_rbp = context->sc_ebp;487regs->tf_rbx = context->sc_ebx;488regs->tf_rdx = context->sc_edx;489regs->tf_rcx = context->sc_ecx;490regs->tf_rax = context->sc_eax;491regs->tf_rip = context->sc_eip;492regs->tf_cs = context->sc_cs;493regs->tf_rflags = eflags;494regs->tf_rsp = context->sc_esp_at_signal;495regs->tf_ss = context->sc_ss;496set_pcb_flags(td->td_pcb, PCB_FULL_IRET);497498/*499* call sigaltstack & ignore results..500*/501lss = &uc.uc_stack;502ss.ss_sp = PTRIN(lss->ss_sp);503ss.ss_size = lss->ss_size;504ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags);505506(void)kern_sigaltstack(td, &ss, NULL);507508return (EJUSTRETURN);509}510511static int512linux32_fetch_syscall_args(struct thread *td)513{514struct proc *p;515struct trapframe *frame;516struct syscall_args *sa;517518p = td->td_proc;519frame = td->td_frame;520sa = &td->td_sa;521522sa->args[0] = frame->tf_rbx;523sa->args[1] = frame->tf_rcx;524sa->args[2] = frame->tf_rdx;525sa->args[3] = frame->tf_rsi;526sa->args[4] = frame->tf_rdi;527sa->args[5] = frame->tf_rbp;528sa->code = frame->tf_rax;529sa->original_code = sa->code;530531if (sa->code >= p->p_sysent->sv_size)532/* nosys */533sa->callp = &nosys_sysent;534else535sa->callp = &p->p_sysent->sv_table[sa->code];536537td->td_retval[0] = 0;538td->td_retval[1] = frame->tf_rdx;539540return (0);541}542543static void544linux32_set_syscall_retval(struct thread *td, int error)545{546struct trapframe *frame = td->td_frame;547548cpu_set_syscall_retval(td, error);549550if (__predict_false(error != 0)) {551if (error != ERESTART && error != EJUSTRETURN)552frame->tf_rax = bsd_to_linux_errno(error);553}554}555556static void557linux32_set_fork_retval(struct thread *td)558{559struct trapframe *frame = td->td_frame;560561frame->tf_rax = 0;562}563564/*565* Clear registers on exec566* XXX copied from ia32_signal.c.567*/568static void569linux_exec_setregs(struct thread *td, struct image_params *imgp,570uintptr_t stack)571{572struct trapframe *regs = td->td_frame;573struct pcb *pcb = td->td_pcb;574register_t saved_rflags;575576regs = td->td_frame;577pcb = td->td_pcb;578579if (td->td_proc->p_md.md_ldt != NULL)580user_ldt_free(td);581582/* Do full restore on return so that we can change to a different %cs */583set_pcb_flags(pcb, PCB_32BIT | PCB_FULL_IRET);584clear_pcb_flags(pcb, PCB_TLSBASE);585586critical_enter();587wrmsr(MSR_FSBASE, 0);588wrmsr(MSR_KGSBASE, 0); /* User value while we're in the kernel */589pcb->pcb_fsbase = 0;590pcb->pcb_gsbase = 0;591critical_exit();592pcb->pcb_initial_fpucw = __LINUX_NPXCW__;593594saved_rflags = regs->tf_rflags & PSL_T;595bzero((char *)regs, sizeof(struct trapframe));596regs->tf_rip = imgp->entry_addr;597regs->tf_rsp = stack;598regs->tf_rflags = PSL_USER | saved_rflags;599regs->tf_gs = _ugssel;600regs->tf_fs = _ufssel;601regs->tf_es = _udatasel;602regs->tf_ds = _udatasel;603regs->tf_ss = _udatasel;604regs->tf_flags = TF_HASSEGS;605regs->tf_cs = _ucode32sel;606regs->tf_rbx = (register_t)imgp->ps_strings;607608x86_clear_dbregs(pcb);609610fpstate_drop(td);611}612613/*614* XXX copied from ia32_sysvec.c.615*/616static int617linux_copyout_strings(struct image_params *imgp, uintptr_t *stack_base)618{619int argc, envc, error;620u_int32_t *vectp;621char *stringp;622uintptr_t destp, ustringp;623struct linux32_ps_strings *arginfo;624char canary[LINUX_AT_RANDOM_LEN];625size_t execpath_len;626627arginfo = (struct linux32_ps_strings *)PROC_PS_STRINGS(imgp->proc);628destp = (uintptr_t)arginfo;629630if (imgp->execpath != NULL && imgp->auxargs != NULL) {631execpath_len = strlen(imgp->execpath) + 1;632destp -= execpath_len;633destp = rounddown2(destp, sizeof(uint32_t));634imgp->execpathp = (void *)destp;635error = copyout(imgp->execpath, imgp->execpathp, execpath_len);636if (error != 0)637return (error);638}639640/* Prepare the canary for SSP. */641arc4rand(canary, sizeof(canary), 0);642destp -= roundup(sizeof(canary), sizeof(uint32_t));643imgp->canary = (void *)destp;644error = copyout(canary, imgp->canary, sizeof(canary));645if (error != 0)646return (error);647648/* Allocate room for the argument and environment strings. */649destp -= ARG_MAX - imgp->args->stringspace;650destp = rounddown2(destp, sizeof(uint32_t));651ustringp = destp;652653if (imgp->auxargs) {654/*655* Allocate room on the stack for the ELF auxargs656* array. It has LINUX_AT_COUNT entries.657*/658destp -= LINUX_AT_COUNT * sizeof(Elf32_Auxinfo);659destp = rounddown2(destp, sizeof(uint32_t));660}661662vectp = (uint32_t *)destp;663664/*665* Allocate room for the argv[] and env vectors including the666* terminating NULL pointers.667*/668vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;669670/* vectp also becomes our initial stack base. */671*stack_base = (uintptr_t)vectp;672673stringp = imgp->args->begin_argv;674argc = imgp->args->argc;675envc = imgp->args->envc;676677/* Copy out strings - arguments and environment. */678error = copyout(stringp, (void *)ustringp,679ARG_MAX - imgp->args->stringspace);680if (error != 0)681return (error);682683/* Fill in "ps_strings" struct for ps, w, etc. */684if (suword32(&arginfo->ps_argvstr, (uint32_t)(intptr_t)vectp) != 0 ||685suword32(&arginfo->ps_nargvstr, argc) != 0)686return (EFAULT);687688/* Fill in argument portion of vector table. */689for (; argc > 0; --argc) {690if (suword32(vectp++, ustringp) != 0)691return (EFAULT);692while (*stringp++ != 0)693ustringp++;694ustringp++;695}696697/* A null vector table pointer separates the argp's from the envp's. */698if (suword32(vectp++, 0) != 0)699return (EFAULT);700701if (suword32(&arginfo->ps_envstr, (uint32_t)(intptr_t)vectp) != 0 ||702suword32(&arginfo->ps_nenvstr, envc) != 0)703return (EFAULT);704705/* Fill in environment portion of vector table. */706for (; envc > 0; --envc) {707if (suword32(vectp++, ustringp) != 0)708return (EFAULT);709while (*stringp++ != 0)710ustringp++;711ustringp++;712}713714/* The end of the vector table is a null pointer. */715if (suword32(vectp, 0) != 0)716return (EFAULT);717718if (imgp->auxargs) {719vectp++;720error = imgp->sysent->sv_copyout_auxargs(imgp,721(uintptr_t)vectp);722if (error != 0)723return (error);724}725726return (0);727}728729static SYSCTL_NODE(_compat, OID_AUTO, linux32, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,730"32-bit Linux emulation");731732static u_long linux32_maxdsiz = LINUX32_MAXDSIZ;733SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxdsiz, CTLFLAG_RW,734&linux32_maxdsiz, 0, "");735static u_long linux32_maxssiz = LINUX32_MAXSSIZ;736SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxssiz, CTLFLAG_RW,737&linux32_maxssiz, 0, "");738static u_long linux32_maxvmem = LINUX32_MAXVMEM;739SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxvmem, CTLFLAG_RW,740&linux32_maxvmem, 0, "");741bool linux32_emulate_i386 = false;742SYSCTL_BOOL(_compat_linux32, OID_AUTO, emulate_i386, CTLFLAG_RWTUN,743&linux32_emulate_i386, 0, "Emulate the real i386");744745static void746linux32_fixlimit(struct rlimit *rl, int which)747{748749switch (which) {750case RLIMIT_DATA:751if (linux32_maxdsiz != 0) {752if (rl->rlim_cur > linux32_maxdsiz)753rl->rlim_cur = linux32_maxdsiz;754if (rl->rlim_max > linux32_maxdsiz)755rl->rlim_max = linux32_maxdsiz;756}757break;758case RLIMIT_STACK:759if (linux32_maxssiz != 0) {760if (rl->rlim_cur > linux32_maxssiz)761rl->rlim_cur = linux32_maxssiz;762if (rl->rlim_max > linux32_maxssiz)763rl->rlim_max = linux32_maxssiz;764}765break;766case RLIMIT_VMEM:767if (linux32_maxvmem != 0) {768if (rl->rlim_cur > linux32_maxvmem)769rl->rlim_cur = linux32_maxvmem;770if (rl->rlim_max > linux32_maxvmem)771rl->rlim_max = linux32_maxvmem;772}773break;774}775}776777struct sysentvec elf_linux_sysvec = {778.sv_size = LINUX32_SYS_MAXSYSCALL,779.sv_table = linux32_sysent,780.sv_fixup = elf32_freebsd_fixup,781.sv_sendsig = linux_sendsig,782.sv_sigcode = &_binary_linux32_vdso_so_o_start,783.sv_szsigcode = &linux_szsigcode,784.sv_name = "Linux ELF32",785.sv_coredump = elf32_coredump,786.sv_elf_core_osabi = ELFOSABI_NONE,787.sv_elf_core_abi_vendor = LINUX_ABI_VENDOR,788.sv_elf_core_prepare_notes = linux32_prepare_notes,789.sv_minsigstksz = LINUX_MINSIGSTKSZ,790.sv_minuser = VM_MIN_ADDRESS,791.sv_maxuser = LINUX32_MAXUSER,792.sv_usrstack = LINUX32_USRSTACK,793.sv_psstrings = LINUX32_PS_STRINGS,794.sv_psstringssz = sizeof(struct linux32_ps_strings),795.sv_stackprot = VM_PROT_ALL,796.sv_copyout_auxargs = __linuxN(copyout_auxargs),797.sv_copyout_strings = linux_copyout_strings,798.sv_setregs = linux_exec_setregs,799.sv_fixlimit = linux32_fixlimit,800.sv_maxssiz = &linux32_maxssiz,801.sv_flags = SV_ABI_LINUX | SV_ILP32 | SV_SHP |802SV_SIG_DISCIGN | SV_SIG_WAITNDQ | SV_TIMEKEEP,803.sv_set_syscall_retval = linux32_set_syscall_retval,804.sv_fetch_syscall_args = linux32_fetch_syscall_args,805.sv_syscallnames = linux32_syscallnames,806.sv_shared_page_base = LINUX32_SHAREDPAGE,807.sv_shared_page_len = PAGE_SIZE,808.sv_schedtail = linux_schedtail,809.sv_thread_detach = linux_thread_detach,810.sv_trap = NULL,811.sv_hwcap = NULL,812.sv_hwcap2 = NULL,813.sv_hwcap3 = NULL,814.sv_hwcap4 = NULL,815.sv_onexec = linux_on_exec_vmspace,816.sv_onexit = linux_on_exit,817.sv_ontdexit = linux_thread_dtor,818.sv_setid_allowed = &linux_setid_allowed_query,819.sv_set_fork_retval = linux32_set_fork_retval,820};821822static int823linux_on_exec_vmspace(struct proc *p, struct image_params *imgp)824{825int error;826827error = linux_map_vdso(p, linux_vdso_obj, linux_vdso_base,828LINUX32_VDSOPAGE_SIZE, imgp);829if (error == 0)830error = linux_on_exec(p, imgp);831return (error);832}833834/*835* linux_vdso_install() and linux_exec_sysvec_init() must be called836* after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY).837*/838static void839linux_exec_sysvec_init(void *param)840{841l_uintptr_t *ktimekeep_base, *ktsc_selector;842struct sysentvec *sv;843ptrdiff_t tkoff;844845sv = param;846/* Fill timekeep_base */847exec_sysvec_init(sv);848849tkoff = kern_timekeep_base - linux_vdso_base;850ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff);851*ktimekeep_base = sv->sv_shared_page_base + sv->sv_timekeep_offset;852853tkoff = kern_tsc_selector - linux_vdso_base;854ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);855*ktsc_selector = linux_vdso_tsc_selector_idx();856if (bootverbose)857printf("Linux i386 vDSO tsc_selector: %u\n", *ktsc_selector);858859tkoff = kern_cpu_selector - linux_vdso_base;860ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);861*ktsc_selector = linux_vdso_cpu_selector_idx();862if (bootverbose)863printf("Linux i386 vDSO cpu_selector: %u\n", *ktsc_selector);864}865SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY,866linux_exec_sysvec_init, &elf_linux_sysvec);867868static void869linux_vdso_install(const void *param)870{871char *vdso_start = &_binary_linux32_vdso_so_o_start;872char *vdso_end = &_binary_linux32_vdso_so_o_end;873874linux_szsigcode = vdso_end - vdso_start;875MPASS(linux_szsigcode <= LINUX32_VDSOPAGE_SIZE);876877linux_vdso_base = LINUX32_VDSOPAGE;878879__elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base);880881linux_vdso_obj = __elfN(linux_shared_page_init)882(&linux_vdso_mapping, LINUX32_VDSOPAGE_SIZE);883bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode);884885linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base);886}887SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST,888linux_vdso_install, NULL);889890static void891linux_vdso_deinstall(const void *param)892{893894__elfN(linux_shared_page_fini)(linux_vdso_obj,895linux_vdso_mapping, LINUX32_VDSOPAGE_SIZE);896}897SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,898linux_vdso_deinstall, NULL);899900static void901linux_vdso_reloc(char *mapping, Elf_Addr offset)902{903const Elf_Shdr *shdr;904const Elf_Rel *rel;905const Elf_Ehdr *ehdr;906Elf32_Addr *where;907Elf_Size rtype, symidx;908Elf32_Addr addr, addend;909int i, relcnt;910911MPASS(offset != 0);912913relcnt = 0;914ehdr = (const Elf_Ehdr *)mapping;915shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff);916for (i = 0; i < ehdr->e_shnum; i++)917{918switch (shdr[i].sh_type) {919case SHT_REL:920rel = (const Elf_Rel *)(mapping + shdr[i].sh_offset);921relcnt = shdr[i].sh_size / sizeof(*rel);922break;923case SHT_RELA:924printf("Linux i386 vDSO: unexpected Rela section\n");925break;926}927}928929for (i = 0; i < relcnt; i++, rel++) {930where = (Elf32_Addr *)(mapping + rel->r_offset);931addend = *where;932rtype = ELF_R_TYPE(rel->r_info);933symidx = ELF_R_SYM(rel->r_info);934935switch (rtype) {936case R_386_NONE: /* none */937break;938939case R_386_RELATIVE: /* B + A */940addr = (Elf32_Addr)PTROUT(offset + addend);941if (*where != addr)942*where = addr;943break;944945case R_386_IRELATIVE:946printf("Linux i386 vDSO: unexpected ifunc relocation, "947"symbol index %ld\n", (intmax_t)symidx);948break;949default:950printf("Linux i386 vDSO: unexpected relocation type %ld, "951"symbol index %ld\n", (intmax_t)rtype, (intmax_t)symidx);952}953}954}955956static Elf_Brandnote linux32_brandnote = {957.hdr.n_namesz = sizeof(GNU_ABI_VENDOR),958.hdr.n_descsz = 16, /* XXX at least 16 */959.hdr.n_type = 1,960.vendor = GNU_ABI_VENDOR,961.flags = BN_TRANSLATE_OSREL,962.trans_osrel = linux_trans_osrel963};964965static Elf32_Brandinfo linux_brand = {966.brand = ELFOSABI_LINUX,967.machine = EM_386,968.compat_3_brand = "Linux",969.interp_path = "/lib/ld-linux.so.1",970.sysvec = &elf_linux_sysvec,971.interp_newpath = NULL,972.brand_note = &linux32_brandnote,973.flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE974};975976static Elf32_Brandinfo linux_glibc2brand = {977.brand = ELFOSABI_LINUX,978.machine = EM_386,979.compat_3_brand = "Linux",980.interp_path = "/lib/ld-linux.so.2",981.sysvec = &elf_linux_sysvec,982.interp_newpath = NULL,983.brand_note = &linux32_brandnote,984.flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE985};986987static Elf32_Brandinfo linux_muslbrand = {988.brand = ELFOSABI_LINUX,989.machine = EM_386,990.compat_3_brand = "Linux",991.interp_path = "/lib/ld-musl-i386.so.1",992.sysvec = &elf_linux_sysvec,993.interp_newpath = NULL,994.brand_note = &linux32_brandnote,995.flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE |996LINUX_BI_FUTEX_REQUEUE997};998999static Elf32_Brandinfo *linux_brandlist[] = {1000&linux_brand,1001&linux_glibc2brand,1002&linux_muslbrand,1003NULL1004};10051006static int1007linux_elf_modevent(module_t mod, int type, void *data)1008{1009Elf32_Brandinfo **brandinfo;1010int error;1011struct linux_ioctl_handler **lihp;10121013error = 0;10141015switch(type) {1016case MOD_LOAD:1017for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;1018++brandinfo)1019if (elf32_insert_brand_entry(*brandinfo) < 0)1020error = EINVAL;1021if (error == 0) {1022SET_FOREACH(lihp, linux_ioctl_handler_set)1023linux32_ioctl_register_handler(*lihp);1024stclohz = (stathz ? stathz : hz);1025if (bootverbose)1026printf("Linux i386 ELF exec handler installed\n");1027} else1028printf("cannot insert Linux i386 ELF brand handler\n");1029break;1030case MOD_UNLOAD:1031for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;1032++brandinfo)1033if (elf32_brand_inuse(*brandinfo))1034error = EBUSY;1035if (error == 0) {1036for (brandinfo = &linux_brandlist[0];1037*brandinfo != NULL; ++brandinfo)1038if (elf32_remove_brand_entry(*brandinfo) < 0)1039error = EINVAL;1040}1041if (error == 0) {1042SET_FOREACH(lihp, linux_ioctl_handler_set)1043linux32_ioctl_unregister_handler(*lihp);1044if (bootverbose)1045printf("Linux i386 ELF exec handler removed\n");1046} else1047printf("Could not deinstall Linux i386 ELF interpreter entry\n");1048break;1049default:1050return (EOPNOTSUPP);1051}1052return (error);1053}10541055static moduledata_t linux_elf_mod = {1056"linuxelf",1057linux_elf_modevent,105801059};10601061DECLARE_MODULE_TIED(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);1062MODULE_DEPEND(linuxelf, linux_common, 1, 1, 1);1063FEATURE(linux, "Linux 32bit support");106410651066