Home | History | Annotate | Line # | Download | only in amd64
linux_exec_machdep.c revision 1.16.2.1
      1  1.16.2.1  uebayasi /*	$NetBSD: linux_exec_machdep.c,v 1.16.2.1 2010/04/30 14:42:58 uebayasi Exp $ */
      2       1.1      manu 
      3       1.1      manu /*-
      4       1.1      manu  * Copyright (c) 2005 Emmanuel Dreyfus, all rights reserved
      5       1.1      manu  *
      6       1.1      manu  * Redistribution and use in source and binary forms, with or without
      7       1.1      manu  * modification, are permitted provided that the following conditions
      8       1.1      manu  * are met:
      9       1.1      manu  * 1. Redistributions of source code must retain the above copyright
     10       1.1      manu  *    notice, this list of conditions and the following disclaimer.
     11       1.1      manu  * 2. Redistributions in binary form must reproduce the above copyright
     12       1.1      manu  *    notice, this list of conditions and the following disclaimer in the
     13       1.1      manu  *    documentation and/or other materials provided with the distribution.
     14       1.1      manu  * 3. All advertising materials mentioning features or use of this software
     15       1.1      manu  *    must display the following acknowledgement:
     16       1.1      manu  *	This product includes software developed by Emmanuel Dreyfus
     17       1.1      manu  * 4. The name of the author may not be used to endorse or promote
     18       1.1      manu  *    products derived from this software without specific prior written
     19       1.1      manu  *    permission.
     20       1.1      manu  *
     21       1.1      manu  * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
     22       1.1      manu  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     23       1.1      manu  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24       1.1      manu  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
     25       1.1      manu  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26       1.1      manu  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27       1.1      manu  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28       1.1      manu  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29       1.1      manu  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30       1.1      manu  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31       1.1      manu  * POSSIBILITY OF SUCH DAMAGE.
     32       1.1      manu  */
     33       1.1      manu 
     34       1.1      manu #include <sys/cdefs.h>
     35  1.16.2.1  uebayasi __KERNEL_RCSID(0, "$NetBSD: linux_exec_machdep.c,v 1.16.2.1 2010/04/30 14:42:58 uebayasi Exp $");
     36       1.1      manu 
     37       1.1      manu #ifdef __amd64__
     38       1.1      manu #define ELFSIZE 64
     39       1.1      manu #endif
     40       1.1      manu 
     41       1.1      manu #include <sys/param.h>
     42       1.1      manu #include <sys/systm.h>
     43       1.1      manu #include <sys/kernel.h>
     44       1.1      manu #include <sys/resource.h>
     45       1.1      manu #include <sys/proc.h>
     46       1.1      manu #include <sys/conf.h>
     47       1.1      manu #include <sys/malloc.h>
     48       1.1      manu #include <sys/exec_elf.h>
     49       1.1      manu #include <sys/vnode.h>
     50       1.1      manu #include <sys/lwp.h>
     51       1.1      manu #include <sys/exec.h>
     52       1.1      manu #include <sys/stat.h>
     53       1.7      yamt #include <sys/kauth.h>
     54       1.1      manu 
     55      1.10        ad #include <sys/cpu.h>
     56       1.1      manu #include <machine/vmparam.h>
     57      1.13  christos #include <sys/syscallargs.h>
     58       1.1      manu 
     59       1.1      manu #include <uvm/uvm.h>
     60       1.1      manu 
     61       1.1      manu #include <compat/linux/common/linux_types.h>
     62       1.1      manu #include <compat/linux/common/linux_signal.h>
     63      1.12  christos #include <compat/linux/common/linux_machdep.h>
     64       1.1      manu #include <compat/linux/common/linux_util.h>
     65       1.1      manu #include <compat/linux/common/linux_ioctl.h>
     66       1.1      manu #include <compat/linux/common/linux_hdio.h>
     67       1.1      manu #include <compat/linux/common/linux_exec.h>
     68       1.1      manu #include <compat/linux/common/linux_errno.h>
     69      1.13  christos #include <compat/linux/common/linux_prctl.h>
     70      1.14        ad #include <compat/linux/common/linux_ipc.h>
     71      1.14        ad #include <compat/linux/common/linux_sem.h>
     72      1.13  christos #include <compat/linux/linux_syscallargs.h>
     73       1.1      manu 
     74       1.1      manu int
     75       1.4  christos linux_exec_setup_stack(struct lwp *l, struct exec_package *epp)
     76       1.1      manu {
     77       1.1      manu 	u_long max_stack_size;
     78       1.1      manu 	u_long access_linear_min, access_size;
     79       1.1      manu 	u_long noaccess_linear_min, noaccess_size;
     80       1.1      manu 
     81       1.1      manu #ifndef USRSTACK32
     82       1.1      manu #define USRSTACK32      (0x00000000ffffffffL & ~PGOFSET)
     83       1.1      manu #endif
     84       1.1      manu 
     85       1.1      manu 	if (epp->ep_flags & EXEC_32) {
     86       1.1      manu 		epp->ep_minsaddr = USRSTACK32;
     87       1.1      manu 		max_stack_size = MAXSSIZ;
     88       1.1      manu 		if (epp->ep_minsaddr > LINUX_USRSTACK32)
     89       1.1      manu 			epp->ep_minsaddr = LINUX_USRSTACK32;
     90       1.1      manu 	} else {
     91       1.1      manu 		epp->ep_minsaddr = USRSTACK;
     92       1.1      manu 		max_stack_size = MAXSSIZ;
     93       1.1      manu 		if (epp->ep_minsaddr > LINUX_USRSTACK)
     94       1.1      manu 			epp->ep_minsaddr = LINUX_USRSTACK;
     95       1.1      manu 
     96       1.1      manu 	}
     97       1.1      manu 
     98       1.1      manu 	epp->ep_maxsaddr = (u_long)STACK_GROW(epp->ep_minsaddr,
     99       1.1      manu 		max_stack_size);
    100       1.4  christos 	epp->ep_ssize = l->l_proc->p_rlimit[RLIMIT_STACK].rlim_cur;
    101       1.1      manu 
    102       1.1      manu 	/*
    103       1.1      manu 	 * set up commands for stack.  note that this takes *two*, one to
    104       1.1      manu 	 * map the part of the stack which we can access, and one to map
    105       1.1      manu 	 * the part which we can't.
    106       1.1      manu 	 *
    107       1.1      manu 	 * arguably, it could be made into one, but that would require the
    108       1.1      manu 	 * addition of another mapping proc, which is unnecessary
    109       1.1      manu 	 */
    110       1.1      manu 	access_size = epp->ep_ssize;
    111       1.1      manu 	access_linear_min = (u_long)STACK_ALLOC(epp->ep_minsaddr, access_size);
    112       1.1      manu 	noaccess_size = max_stack_size - access_size;
    113       1.1      manu 	noaccess_linear_min = (u_long)STACK_ALLOC(STACK_GROW(epp->ep_minsaddr,
    114       1.1      manu 	    access_size), noaccess_size);
    115       1.1      manu 	if (noaccess_size > 0) {
    116      1.16       mrg 		NEW_VMCMD2(&epp->ep_vmcmds, vmcmd_map_zero, noaccess_size,
    117      1.16       mrg 		    noaccess_linear_min, NULLVP, 0, VM_PROT_NONE, VMCMD_STACK);
    118       1.1      manu 	}
    119       1.1      manu 	KASSERT(access_size > 0);
    120      1.16       mrg 	NEW_VMCMD2(&epp->ep_vmcmds, vmcmd_map_zero, access_size,
    121      1.16       mrg 	    access_linear_min, NULLVP, 0, VM_PROT_READ | VM_PROT_WRITE,
    122      1.16       mrg 	    VMCMD_STACK);
    123       1.1      manu 
    124       1.1      manu 	return 0;
    125       1.1      manu }
    126       1.1      manu 
    127       1.1      manu int
    128      1.15    cegger ELFNAME2(linux,copyargs)(struct lwp *l, struct exec_package *pack,
    129      1.15    cegger 	struct ps_strings *arginfo, char **stackp, void *argp)
    130       1.1      manu {
    131       1.1      manu 	struct linux_extra_stack_data64 *esdp, esd;
    132       1.1      manu 	struct elf_args *ap;
    133       1.5  christos 	struct vattr *vap;
    134       1.1      manu 	Elf_Ehdr *eh;
    135       1.1      manu 	Elf_Phdr *ph;
    136       1.1      manu 	u_long phsize;
    137       1.1      manu 	Elf_Addr phdr = 0;
    138       1.1      manu 	int error;
    139       1.1      manu 	int i;
    140       1.1      manu 
    141       1.5  christos 	if ((error = copyargs(l, pack, arginfo, stackp, argp)) != 0)
    142       1.1      manu 		return error;
    143       1.1      manu 
    144       1.1      manu 	/*
    145       1.1      manu 	 * Push extra arguments on the stack needed by dynamically
    146       1.1      manu 	 * linked binaries and static binaries as well.
    147       1.1      manu 	 */
    148       1.1      manu 	memset(&esd, 0, sizeof(esd));
    149       1.1      manu 	esdp = (struct linux_extra_stack_data64 *)(*stackp);
    150       1.1      manu 	ap = (struct elf_args *)pack->ep_emul_arg;
    151       1.1      manu 	vap = pack->ep_vap;
    152       1.1      manu 	eh = (Elf_Ehdr *)pack->ep_hdr;
    153       1.1      manu 
    154       1.1      manu 	/*
    155  1.16.2.1  uebayasi 	 * We forgot this, so we need to reload it now. XXX keep track of it?
    156       1.1      manu 	 */
    157       1.1      manu 	if (ap == NULL) {
    158       1.1      manu 		phsize = eh->e_phnum * sizeof(Elf_Phdr);
    159       1.1      manu 		ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
    160       1.5  christos 		error = exec_read_from(l, pack->ep_vp, eh->e_phoff, ph, phsize);
    161       1.1      manu 		if (error != 0) {
    162       1.1      manu 			for (i = 0; i < eh->e_phnum; i++) {
    163       1.1      manu 				if (ph[i].p_type == PT_PHDR) {
    164       1.1      manu 					phdr = ph[i].p_vaddr;
    165       1.1      manu 					break;
    166       1.1      manu 				}
    167       1.1      manu 			}
    168       1.1      manu 		}
    169       1.1      manu 		free(ph, M_TEMP);
    170       1.1      manu 	}
    171       1.1      manu 
    172       1.1      manu 
    173       1.1      manu 	/*
    174       1.1      manu 	 * The exec_package doesn't have a proc pointer and it's not
    175       1.1      manu 	 * exactly trivial to add one since the credentials are
    176       1.1      manu 	 * changing. XXX Linux uses curlwp's credentials.
    177       1.1      manu 	 * Why can't we use them too?
    178       1.1      manu 	 */
    179       1.1      manu 
    180       1.1      manu 	i = 0;
    181       1.1      manu 	esd.ai[i].a_type = LINUX_AT_HWCAP;
    182       1.1      manu 	esd.ai[i++].a_v = rcr4();
    183       1.1      manu 
    184       1.1      manu 	esd.ai[i].a_type = AT_PAGESZ;
    185       1.1      manu 	esd.ai[i++].a_v = PAGE_SIZE;
    186       1.1      manu 
    187       1.1      manu 	esd.ai[i].a_type = LINUX_AT_CLKTCK;
    188       1.1      manu 	esd.ai[i++].a_v = hz;
    189       1.1      manu 
    190       1.1      manu 	esd.ai[i].a_type = AT_PHDR;
    191       1.1      manu 	esd.ai[i++].a_v = (ap ? ap->arg_phaddr: phdr);
    192       1.1      manu 
    193       1.1      manu 	esd.ai[i].a_type = AT_PHENT;
    194       1.1      manu 	esd.ai[i++].a_v = (ap ? ap->arg_phentsize : eh->e_phentsize);
    195       1.1      manu 
    196       1.1      manu 	esd.ai[i].a_type = AT_PHNUM;
    197       1.1      manu 	esd.ai[i++].a_v = (ap ? ap->arg_phnum : eh->e_phnum);
    198       1.1      manu 
    199       1.1      manu 	esd.ai[i].a_type = AT_BASE;
    200       1.1      manu 	esd.ai[i++].a_v = (ap ? ap->arg_interp : 0);
    201       1.1      manu 
    202       1.1      manu 	esd.ai[i].a_type = AT_FLAGS;
    203       1.1      manu 	esd.ai[i++].a_v = 0;
    204       1.1      manu 
    205       1.1      manu 	esd.ai[i].a_type = AT_ENTRY;
    206       1.1      manu 	esd.ai[i++].a_v = (ap ? ap->arg_entry : eh->e_entry);
    207       1.1      manu 
    208       1.1      manu 	esd.ai[i].a_type = LINUX_AT_EGID;
    209       1.8        ad 	esd.ai[i++].a_v = ((vap->va_mode & S_ISGID) ?
    210       1.8        ad 	    vap->va_gid : kauth_cred_getegid(l->l_cred));
    211       1.1      manu 
    212       1.1      manu 	esd.ai[i].a_type = LINUX_AT_GID;
    213       1.8        ad 	esd.ai[i++].a_v = kauth_cred_getgid(l->l_cred);
    214       1.1      manu 
    215       1.1      manu 	esd.ai[i].a_type = LINUX_AT_EUID;
    216       1.8        ad 	esd.ai[i++].a_v = ((vap->va_mode & S_ISUID) ?
    217       1.8        ad 	    vap->va_uid : kauth_cred_geteuid(l->l_cred));
    218       1.1      manu 
    219       1.1      manu 	esd.ai[i].a_type = LINUX_AT_UID;
    220       1.8        ad 	esd.ai[i++].a_v = kauth_cred_getuid(l->l_cred);
    221       1.1      manu 
    222       1.1      manu 	esd.ai[i].a_type = LINUX_AT_SECURE;
    223       1.1      manu 	esd.ai[i++].a_v = 0;
    224       1.1      manu 
    225       1.1      manu 	esd.ai[i].a_type = LINUX_AT_PLATFORM;
    226       1.1      manu 	esd.ai[i++].a_v = (Elf_Addr)&esdp->hw_platform[0];
    227       1.1      manu 
    228       1.1      manu 	esd.ai[i].a_type = AT_NULL;
    229       1.1      manu 	esd.ai[i++].a_v = 0;
    230       1.1      manu 
    231       1.1      manu #ifdef DEBUG_LINUX
    232       1.1      manu 	if (i != LINUX_ELF_AUX_ENTRIES) {
    233       1.1      manu 		printf("linux_elf64_copyargs: %d Aux entries\n", i);
    234       1.1      manu 		return EINVAL;
    235       1.1      manu 	}
    236       1.1      manu #endif
    237       1.1      manu 
    238       1.1      manu 	strcpy(esd.hw_platform, LINUX_PLATFORM);
    239       1.1      manu 
    240       1.1      manu 	if (ap) {
    241       1.1      manu 		free((char *)ap, M_TEMP);
    242       1.1      manu 		pack->ep_emul_arg = NULL;
    243       1.1      manu 	}
    244       1.1      manu 
    245       1.1      manu 	/*
    246       1.1      manu 	 * Copy out the ELF auxiliary table and hw platform name
    247       1.1      manu 	 */
    248       1.1      manu 	if ((error = copyout(&esd, esdp, sizeof(esd))) != 0)
    249       1.1      manu 		return error;
    250       1.1      manu 	*stackp += sizeof(esd);
    251       1.1      manu 
    252       1.1      manu 	return 0;
    253       1.1      manu }
    254      1.11  christos 
    255      1.11  christos #ifdef LINUX_NPTL
    256      1.11  christos int
    257      1.11  christos linux_init_thread_area(struct lwp *l, struct lwp *l2)
    258      1.11  christos {
    259      1.13  christos 	register_t retval;
    260      1.13  christos 	struct linux_sys_arch_prctl_args uap;
    261      1.13  christos 	struct trapframe *tf = l2->l_md.md_regs;
    262      1.13  christos 
    263      1.13  christos 	SCARG(&uap, code) = LINUX_ARCH_SET_FS;
    264      1.13  christos 	SCARG(&uap, addr) = tf->tf_r8;
    265      1.13  christos 	return linux_sys_arch_prctl(l2, &uap, &retval);
    266      1.11  christos }
    267      1.11  christos #endif
    268