Home | History | Annotate | Line # | Download | only in kern
exec_aout.c revision 1.35.14.1
      1 /*	$NetBSD: exec_aout.c,v 1.35.14.1 2014/08/20 00:04:28 tls Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1993, 1994 Christopher G. Demetriou
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by Christopher G. Demetriou.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: exec_aout.c,v 1.35.14.1 2014/08/20 00:04:28 tls Exp $");
     35 
     36 #ifdef _KERNEL_OPT
     37 #include "opt_coredump.h"
     38 #endif
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/proc.h>
     43 #include <sys/vnode.h>
     44 #include <sys/exec.h>
     45 #include <sys/exec_aout.h>
     46 #include <sys/resourcevar.h>
     47 #include <sys/module.h>
     48 
     49 #include <uvm/uvm_extern.h>
     50 
     51 #ifdef COREDUMP
     52 #define	DEP	"coredump"
     53 #else
     54 #define	DEP	NULL
     55 #endif
     56 
     57 MODULE(MODULE_CLASS_EXEC, exec_aout, DEP);
     58 
     59 static struct execsw exec_aout_execsw = {
     60 	.es_hdrsz = sizeof(struct exec),
     61 	.es_makecmds = exec_aout_makecmds,
     62 	.u = {
     63 		.elf_probe_func = NULL,
     64 	},
     65 	.es_emul = &emul_netbsd,
     66 	.es_prio = EXECSW_PRIO_ANY,
     67 	.es_arglen = 0,
     68 	.es_copyargs = copyargs,
     69 	.es_setregs = NULL,
     70 	.es_coredump = coredump_netbsd,
     71 	.es_setup_stack = exec_setup_stack,
     72 };
     73 
     74 static int
     75 exec_aout_modcmd(modcmd_t cmd, void *arg)
     76 {
     77 
     78 	switch (cmd) {
     79 	case MODULE_CMD_INIT:
     80 		return exec_add(&exec_aout_execsw, 1);
     81 
     82 	case MODULE_CMD_FINI:
     83 		return exec_remove(&exec_aout_execsw, 1);
     84 
     85 	default:
     86 		return ENOTTY;
     87         }
     88 }
     89 
     90 /*
     91  * exec_aout_makecmds(): Check if it's an a.out-format executable.
     92  *
     93  * Given a lwp pointer and an exec package pointer, see if the referent
     94  * of the epp is in a.out format.  First check 'standard' magic numbers for
     95  * this architecture.  If that fails, try a CPU-dependent hook.
     96  *
     97  * This function, in the former case, or the hook, in the latter, is
     98  * responsible for creating a set of vmcmds which can be used to build
     99  * the process's vm space and inserting them into the exec package.
    100  */
    101 
    102 int
    103 exec_aout_makecmds(struct lwp *l, struct exec_package *epp)
    104 {
    105 	u_long midmag, magic;
    106 	u_short mid;
    107 	int error;
    108 	struct exec *execp = epp->ep_hdr;
    109 
    110 	if (epp->ep_hdrvalid < sizeof(struct exec))
    111 		return ENOEXEC;
    112 
    113 	midmag = ntohl(execp->a_midmag);
    114 	mid = (midmag >> 16) & 0x3ff;
    115 	magic = midmag & 0xffff;
    116 
    117 	midmag = mid << 16 | magic;
    118 
    119 	switch (midmag) {
    120 	case (MID_MACHINE << 16) | ZMAGIC:
    121 		error = exec_aout_prep_zmagic(l, epp);
    122 		break;
    123 	case (MID_MACHINE << 16) | NMAGIC:
    124 		error = exec_aout_prep_nmagic(l, epp);
    125 		break;
    126 	case (MID_MACHINE << 16) | OMAGIC:
    127 		error = exec_aout_prep_omagic(l, epp);
    128 		break;
    129 	default:
    130 		error = cpu_exec_aout_makecmds(l, epp);
    131 	}
    132 
    133 	if (error)
    134 		kill_vmcmds(&epp->ep_vmcmds);
    135 	else
    136 		epp->ep_flags &= ~EXEC_TOPDOWN_VM;
    137 
    138 	return error;
    139 }
    140 
    141 /*
    142  * exec_aout_prep_zmagic(): Prepare a 'native' ZMAGIC binary's exec package
    143  *
    144  * First, set of the various offsets/lengths in the exec package.
    145  *
    146  * Then, mark the text image busy (so it can be demand paged) or error
    147  * out if this is not possible.  Finally, set up vmcmds for the
    148  * text, data, bss, and stack segments.
    149  */
    150 
    151 int
    152 exec_aout_prep_zmagic(struct lwp *l, struct exec_package *epp)
    153 {
    154 	struct exec *execp = epp->ep_hdr;
    155 	int error;
    156 
    157 	epp->ep_taddr = AOUT_LDPGSZ;
    158 	epp->ep_tsize = execp->a_text;
    159 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
    160 	epp->ep_dsize = execp->a_data + execp->a_bss;
    161 	epp->ep_entry = execp->a_entry;
    162 
    163 	error = vn_marktext(epp->ep_vp);
    164 	if (error)
    165 		return (error);
    166 
    167 	/* set up command for text segment */
    168 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, round_page(execp->a_text),
    169 	    epp->ep_taddr, epp->ep_vp, 0, VM_PROT_READ|VM_PROT_EXECUTE);
    170 
    171 	/* set up command for data segment */
    172 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, round_page(execp->a_data),
    173 	    epp->ep_daddr, epp->ep_vp, execp->a_text,
    174 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    175 
    176 	/* set up command for bss segment */
    177 	if (execp->a_bss > 0)
    178 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
    179 		    epp->ep_daddr + execp->a_data, NULLVP, 0,
    180 		    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    181 
    182 	return (*epp->ep_esch->es_setup_stack)(l, epp);
    183 }
    184 
    185 /*
    186  * exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's exec package
    187  */
    188 
    189 int
    190 exec_aout_prep_nmagic(struct lwp *l, struct exec_package *epp)
    191 {
    192 	struct exec *execp = epp->ep_hdr;
    193 	long bsize, baddr;
    194 
    195 	epp->ep_taddr = AOUT_LDPGSZ;
    196 	epp->ep_tsize = execp->a_text;
    197 	epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, AOUT_LDPGSZ);
    198 	epp->ep_dsize = execp->a_data + execp->a_bss;
    199 	epp->ep_entry = execp->a_entry;
    200 
    201 	/* set up command for text segment */
    202 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
    203 	    epp->ep_taddr, epp->ep_vp, sizeof(struct exec),
    204 	    VM_PROT_READ|VM_PROT_EXECUTE);
    205 
    206 	/* set up command for data segment */
    207 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
    208 	    epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec),
    209 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    210 
    211 	/* set up command for bss segment */
    212 	baddr = round_page(epp->ep_daddr + execp->a_data);
    213 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    214 	if (bsize > 0)
    215 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    216 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    217 
    218 	return (*epp->ep_esch->es_setup_stack)(l, epp);
    219 }
    220 
    221 /*
    222  * exec_aout_prep_omagic(): Prepare a 'native' OMAGIC binary's exec package
    223  */
    224 
    225 int
    226 exec_aout_prep_omagic(struct lwp *l, struct exec_package *epp)
    227 {
    228 	struct exec *execp = epp->ep_hdr;
    229 	long dsize, bsize, baddr;
    230 
    231 	epp->ep_taddr = AOUT_LDPGSZ;
    232 	epp->ep_tsize = execp->a_text;
    233 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
    234 	epp->ep_dsize = execp->a_data + execp->a_bss;
    235 	epp->ep_entry = execp->a_entry;
    236 
    237 	/* set up command for text and data segments */
    238 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
    239 	    execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
    240 	    sizeof(struct exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    241 
    242 	/* set up command for bss segment */
    243 	baddr = round_page(epp->ep_daddr + execp->a_data);
    244 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    245 	if (bsize > 0)
    246 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    247 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    248 
    249 	/*
    250 	 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
    251 	 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
    252 	 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
    253 	 * respectively to page boundaries.
    254 	 * Compensate `ep_dsize' for the amount of data covered by the last
    255 	 * text page.
    256 	 */
    257 	dsize = epp->ep_dsize + execp->a_text - round_page(execp->a_text);
    258 	epp->ep_dsize = (dsize > 0) ? dsize : 0;
    259 	return (*epp->ep_esch->es_setup_stack)(l, epp);
    260 }
    261