Date: Fri, 15 Nov 2019 18:42:13 +0000 (UTC) From: John Baldwin <jhb@FreeBSD.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r354741 - in head/sys: amd64/amd64 arm/arm arm64/arm64 compat/freebsd32 compat/ia32 i386/i386 kern mips/mips powerpc/powerpc riscv/riscv sparc64/sparc64 sys Message-ID: <201911151842.xAFIgDrJ093716@repo.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: jhb Date: Fri Nov 15 18:42:13 2019 New Revision: 354741 URL: https://svnweb.freebsd.org/changeset/base/354741 Log: Add a sv_copyout_auxargs() hook in sysentvec. Change the FreeBSD ELF ABIs to use this new hook to copyout ELF auxv instead of doing it in the sv_fixup hook. In particular, this new hook allows the stack space to be allocated at the same time the auxv values are copied out to userland. This allows us to avoid wasting space for unused auxv entries as well as not having to recalculate where the auxv vector is by walking back up over the argv and environment vectors. Reviewed by: brooks, emaste Tested on: amd64 (amd64 and i386 binaries), i386, mips, mips64 Sponsored by: DARPA Differential Revision: https://reviews.freebsd.org/D22355 Modified: head/sys/amd64/amd64/elf_machdep.c head/sys/arm/arm/elf_machdep.c head/sys/arm64/arm64/elf32_machdep.c head/sys/arm64/arm64/elf_machdep.c head/sys/compat/freebsd32/freebsd32_misc.c head/sys/compat/ia32/ia32_sysvec.c head/sys/i386/i386/elf_machdep.c head/sys/kern/imgact_elf.c head/sys/kern/kern_exec.c head/sys/mips/mips/elf_machdep.c head/sys/mips/mips/freebsd32_machdep.c head/sys/powerpc/powerpc/elf32_machdep.c head/sys/powerpc/powerpc/elf64_machdep.c head/sys/riscv/riscv/elf_machdep.c head/sys/sparc64/sparc64/elf_machdep.c head/sys/sys/imgact_elf.h head/sys/sys/sysent.h Modified: head/sys/amd64/amd64/elf_machdep.c ============================================================================== --- head/sys/amd64/amd64/elf_machdep.c Fri Nov 15 18:34:36 2019 (r354740) +++ head/sys/amd64/amd64/elf_machdep.c Fri Nov 15 18:42:13 2019 (r354741) @@ -68,6 +68,7 @@ struct sysentvec elf64_freebsd_sysvec = { .sv_usrstack = USRSTACK, .sv_psstrings = PS_STRINGS, .sv_stackprot = VM_PROT_ALL, + .sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs), .sv_copyout_strings = exec_copyout_strings, .sv_setregs = exec_setregs, .sv_fixlimit = NULL, Modified: head/sys/arm/arm/elf_machdep.c ============================================================================== --- head/sys/arm/arm/elf_machdep.c Fri Nov 15 18:34:36 2019 (r354740) +++ head/sys/arm/arm/elf_machdep.c Fri Nov 15 18:42:13 2019 (r354741) @@ -75,6 +75,7 @@ struct sysentvec elf32_freebsd_sysvec = { .sv_usrstack = USRSTACK, .sv_psstrings = PS_STRINGS, .sv_stackprot = VM_PROT_ALL, + .sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs), .sv_copyout_strings = exec_copyout_strings, .sv_setregs = exec_setregs, .sv_fixlimit = NULL, Modified: head/sys/arm64/arm64/elf32_machdep.c ============================================================================== --- head/sys/arm64/arm64/elf32_machdep.c Fri Nov 15 18:34:36 2019 (r354740) +++ head/sys/arm64/arm64/elf32_machdep.c Fri Nov 15 18:42:13 2019 (r354741) @@ -92,6 +92,7 @@ static struct sysentvec elf32_freebsd_sysvec = { .sv_usrstack = FREEBSD32_USRSTACK, .sv_psstrings = FREEBSD32_PS_STRINGS, .sv_stackprot = VM_PROT_READ | VM_PROT_WRITE, + .sv_copyout_auxargs = elf32_freebsd_copyout_auxargs, .sv_copyout_strings = freebsd32_copyout_strings, .sv_setregs = freebsd32_setregs, .sv_fixlimit = NULL, // XXX Modified: head/sys/arm64/arm64/elf_machdep.c ============================================================================== --- head/sys/arm64/arm64/elf_machdep.c Fri Nov 15 18:34:36 2019 (r354740) +++ head/sys/arm64/arm64/elf_machdep.c Fri Nov 15 18:42:13 2019 (r354741) @@ -76,6 +76,7 @@ static struct sysentvec elf64_freebsd_sysvec = { .sv_usrstack = USRSTACK, .sv_psstrings = PS_STRINGS, .sv_stackprot = VM_PROT_READ | VM_PROT_WRITE, + .sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs), .sv_copyout_strings = exec_copyout_strings, .sv_setregs = exec_setregs, .sv_fixlimit = NULL, Modified: head/sys/compat/freebsd32/freebsd32_misc.c ============================================================================== --- head/sys/compat/freebsd32/freebsd32_misc.c Fri Nov 15 18:34:36 2019 (r354740) +++ head/sys/compat/freebsd32/freebsd32_misc.c Fri Nov 15 18:42:13 2019 (r354741) @@ -3195,14 +3195,8 @@ freebsd32_copyout_strings(struct image_params *imgp) if (imgp->sysent->sv_stackgap != NULL) imgp->sysent->sv_stackgap(imgp, (u_long *)&vectp); - if (imgp->auxargs) { - /* - * Allocate room on the stack for the ELF auxargs - * array. It has up to AT_COUNT entries. - */ - vectp -= howmany(AT_COUNT * sizeof(Elf32_Auxinfo), - sizeof(*vectp)); - } + if (imgp->auxargs) + imgp->sysent->sv_copyout_auxargs(imgp, (u_long *)&vectp); /* * Allocate room for the argv[] and env vectors including the Modified: head/sys/compat/ia32/ia32_sysvec.c ============================================================================== --- head/sys/compat/ia32/ia32_sysvec.c Fri Nov 15 18:34:36 2019 (r354740) +++ head/sys/compat/ia32/ia32_sysvec.c Fri Nov 15 18:42:13 2019 (r354741) @@ -114,6 +114,7 @@ struct sysentvec ia32_freebsd_sysvec = { .sv_usrstack = FREEBSD32_USRSTACK, .sv_psstrings = FREEBSD32_PS_STRINGS, .sv_stackprot = VM_PROT_ALL, + .sv_copyout_auxargs = elf32_freebsd_copyout_auxargs, .sv_copyout_strings = freebsd32_copyout_strings, .sv_setregs = ia32_setregs, .sv_fixlimit = ia32_fixlimit, Modified: head/sys/i386/i386/elf_machdep.c ============================================================================== --- head/sys/i386/i386/elf_machdep.c Fri Nov 15 18:34:36 2019 (r354740) +++ head/sys/i386/i386/elf_machdep.c Fri Nov 15 18:42:13 2019 (r354741) @@ -70,6 +70,7 @@ struct sysentvec elf32_freebsd_sysvec = { .sv_usrstack = USRSTACK, .sv_psstrings = PS_STRINGS, .sv_stackprot = VM_PROT_ALL, + .sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs), .sv_copyout_strings = exec_copyout_strings, .sv_setregs = exec_setregs, .sv_fixlimit = NULL, Modified: head/sys/kern/imgact_elf.c ============================================================================== --- head/sys/kern/imgact_elf.c Fri Nov 15 18:34:36 2019 (r354740) +++ head/sys/kern/imgact_elf.c Fri Nov 15 18:42:13 2019 (r354741) @@ -1289,7 +1289,7 @@ __CONCAT(exec_, __elfN(imgact))(struct image_params *i addr = et_dyn_addr; /* - * Construct auxargs table (used by the fixup routine) + * Construct auxargs table (used by the copyout_auxargs routine) */ elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_NOWAIT); if (elf_auxargs == NULL) { @@ -1323,16 +1323,13 @@ ret: #define suword __CONCAT(suword, __ELF_WORD_SIZE) -int -__elfN(freebsd_fixup)(register_t **stack_base, struct image_params *imgp) +void +__elfN(freebsd_copyout_auxargs)(struct image_params *imgp, u_long *base) { Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs; Elf_Auxinfo *argarray, *pos; - Elf_Addr *base, *auxbase; - int error; + u_long auxlen; - base = (Elf_Addr *)*stack_base; - auxbase = base + imgp->args->argc + 1 + imgp->args->envc + 1; argarray = pos = malloc(AT_COUNT * sizeof(*pos), M_TEMP, M_WAITOK | M_ZERO); @@ -1376,11 +1373,18 @@ __elfN(freebsd_fixup)(register_t **stack_base, struct imgp->auxargs = NULL; KASSERT(pos - argarray <= AT_COUNT, ("Too many auxargs")); - error = copyout(argarray, auxbase, sizeof(*argarray) * AT_COUNT); + auxlen = sizeof(*argarray) * (pos - argarray); + *base -= auxlen; + copyout(argarray, (void *)*base, auxlen); free(argarray, M_TEMP); - if (error != 0) - return (error); +} +int +__elfN(freebsd_fixup)(register_t **stack_base, struct image_params *imgp) +{ + Elf_Addr *base; + + base = (Elf_Addr *)*stack_base; base--; if (suword(base, imgp->args->argc) == -1) return (EFAULT); Modified: head/sys/kern/kern_exec.c ============================================================================== --- head/sys/kern/kern_exec.c Fri Nov 15 18:34:36 2019 (r354740) +++ head/sys/kern/kern_exec.c Fri Nov 15 18:42:13 2019 (r354741) @@ -1637,14 +1637,8 @@ exec_copyout_strings(struct image_params *imgp) if (imgp->sysent->sv_stackgap != NULL) imgp->sysent->sv_stackgap(imgp, (u_long *)&vectp); - if (imgp->auxargs) { - /* - * Allocate room on the stack for the ELF auxargs - * array. It has up to AT_COUNT entries. - */ - vectp -= howmany(AT_COUNT * sizeof(Elf_Auxinfo), - sizeof(*vectp)); - } + if (imgp->auxargs) + imgp->sysent->sv_copyout_auxargs(imgp, (u_long *)&vectp); /* * Allocate room for the argv[] and env vectors including the Modified: head/sys/mips/mips/elf_machdep.c ============================================================================== --- head/sys/mips/mips/elf_machdep.c Fri Nov 15 18:34:36 2019 (r354740) +++ head/sys/mips/mips/elf_machdep.c Fri Nov 15 18:42:13 2019 (r354741) @@ -71,6 +71,7 @@ struct sysentvec elf64_freebsd_sysvec = { .sv_usrstack = USRSTACK, .sv_psstrings = PS_STRINGS, .sv_stackprot = VM_PROT_ALL, + .sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs), .sv_copyout_strings = exec_copyout_strings, .sv_setregs = exec_setregs, .sv_fixlimit = NULL, @@ -125,6 +126,7 @@ struct sysentvec elf32_freebsd_sysvec = { .sv_usrstack = USRSTACK, .sv_psstrings = PS_STRINGS, .sv_stackprot = VM_PROT_ALL, + .sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs), .sv_copyout_strings = exec_copyout_strings, .sv_setregs = exec_setregs, .sv_fixlimit = NULL, Modified: head/sys/mips/mips/freebsd32_machdep.c ============================================================================== --- head/sys/mips/mips/freebsd32_machdep.c Fri Nov 15 18:34:36 2019 (r354740) +++ head/sys/mips/mips/freebsd32_machdep.c Fri Nov 15 18:42:13 2019 (r354741) @@ -94,6 +94,7 @@ struct sysentvec elf32_freebsd_sysvec = { .sv_usrstack = FREEBSD32_USRSTACK, .sv_psstrings = FREEBSD32_PS_STRINGS, .sv_stackprot = VM_PROT_ALL, + .sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs), .sv_copyout_strings = freebsd32_copyout_strings, .sv_setregs = freebsd32_exec_setregs, .sv_fixlimit = NULL, Modified: head/sys/powerpc/powerpc/elf32_machdep.c ============================================================================== --- head/sys/powerpc/powerpc/elf32_machdep.c Fri Nov 15 18:34:36 2019 (r354740) +++ head/sys/powerpc/powerpc/elf32_machdep.c Fri Nov 15 18:42:13 2019 (r354741) @@ -87,6 +87,7 @@ struct sysentvec elf32_freebsd_sysvec = { .sv_errtbl = NULL, .sv_transtrap = NULL, .sv_fixup = __elfN(freebsd_fixup), + .sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs), .sv_sendsig = sendsig, .sv_sigcode = sigcode32, .sv_szsigcode = &szsigcode32, Modified: head/sys/powerpc/powerpc/elf64_machdep.c ============================================================================== --- head/sys/powerpc/powerpc/elf64_machdep.c Fri Nov 15 18:34:36 2019 (r354740) +++ head/sys/powerpc/powerpc/elf64_machdep.c Fri Nov 15 18:42:13 2019 (r354741) @@ -74,6 +74,7 @@ struct sysentvec elf64_freebsd_sysvec_v1 = { .sv_usrstack = USRSTACK, .sv_psstrings = PS_STRINGS, .sv_stackprot = VM_PROT_ALL, + .sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs), .sv_copyout_strings = exec_copyout_strings, .sv_setregs = exec_setregs_funcdesc, .sv_fixlimit = NULL, @@ -111,6 +112,7 @@ struct sysentvec elf64_freebsd_sysvec_v2 = { .sv_usrstack = USRSTACK, .sv_psstrings = PS_STRINGS, .sv_stackprot = VM_PROT_ALL, + .sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs), .sv_copyout_strings = exec_copyout_strings, .sv_setregs = exec_setregs, .sv_fixlimit = NULL, Modified: head/sys/riscv/riscv/elf_machdep.c ============================================================================== --- head/sys/riscv/riscv/elf_machdep.c Fri Nov 15 18:34:36 2019 (r354740) +++ head/sys/riscv/riscv/elf_machdep.c Fri Nov 15 18:42:13 2019 (r354741) @@ -79,6 +79,7 @@ struct sysentvec elf64_freebsd_sysvec = { .sv_usrstack = USRSTACK, .sv_psstrings = PS_STRINGS, .sv_stackprot = VM_PROT_READ | VM_PROT_WRITE, + .sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs), .sv_copyout_strings = exec_copyout_strings, .sv_setregs = exec_setregs, .sv_fixlimit = NULL, Modified: head/sys/sparc64/sparc64/elf_machdep.c ============================================================================== --- head/sys/sparc64/sparc64/elf_machdep.c Fri Nov 15 18:34:36 2019 (r354740) +++ head/sys/sparc64/sparc64/elf_machdep.c Fri Nov 15 18:42:13 2019 (r354741) @@ -75,6 +75,7 @@ static struct sysentvec elf64_freebsd_sysvec = { .sv_usrstack = USRSTACK, .sv_psstrings = PS_STRINGS, .sv_stackprot = VM_PROT_READ | VM_PROT_WRITE, + .sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs), .sv_copyout_strings = exec_copyout_strings, .sv_setregs = exec_setregs, .sv_fixlimit = NULL, Modified: head/sys/sys/imgact_elf.h ============================================================================== --- head/sys/sys/imgact_elf.h Fri Nov 15 18:34:36 2019 (r354740) +++ head/sys/sys/imgact_elf.h Fri Nov 15 18:42:13 2019 (r354741) @@ -99,6 +99,7 @@ int __elfN(freebsd_fixup)(register_t **, struct image_ int __elfN(coredump)(struct thread *, struct vnode *, off_t, int); size_t __elfN(populate_note)(int, void *, void *, size_t, void **); void __elfN(stackgap)(struct image_params *, u_long *); +void __elfN(freebsd_copyout_auxargs)(struct image_params *, u_long *); /* Machine specific function to dump per-thread information. */ void __elfN(dump_thread)(struct thread *, void *, size_t *); Modified: head/sys/sys/sysent.h ============================================================================== --- head/sys/sys/sysent.h Fri Nov 15 18:34:36 2019 (r354740) +++ head/sys/sys/sysent.h Fri Nov 15 18:42:13 2019 (r354741) @@ -110,6 +110,7 @@ struct sysentvec { /* function to dump core, or NULL */ int (*sv_imgact_try)(struct image_params *); void (*sv_stackgap)(struct image_params *, u_long *); + void (*sv_copyout_auxargs)(struct image_params *, u_long *); int sv_minsigstksz; /* minimum signal stack size */ vm_offset_t sv_minuser; /* VM_MIN_ADDRESS */ vm_offset_t sv_maxuser; /* VM_MAXUSER_ADDRESS */
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201911151842.xAFIgDrJ093716>