Home | History | Annotate | Line # | Download | only in aarch64
      1 /*	$NetBSD: linux32_exec_machdep.c,v 1.2 2023/04/09 12:29:26 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1993, 1994, 1996 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: linux32_exec_machdep.c,v 1.2 2023/04/09 12:29:26 riastradh Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/types.h>
     38 #include <sys/exec.h>
     39 #include <sys/lwp.h>
     40 #include <sys/syscallargs.h>
     41 #include <sys/vnode.h>
     42 
     43 #include <machine/vmparam.h>
     44 
     45 #include <compat/linux/common/linux_types.h>
     46 #include <compat/linux/common/linux_exec.h>
     47 #include <compat/linux32/common/linux32_exec.h>
     48 
     49 #ifdef LINUX32_ARM_KUSER_HELPER_ADDR
     50 static int
     51 vmcmd_linux32_kuser_helper_map(struct lwp *l, struct exec_vmcmd *cmd)
     52 {
     53 	const struct proc *p = l->l_proc;
     54 	/* l->l_proc->p_emul still points to emul_netbsd at this point */
     55 	const struct emul *e = &emul_linux32;
     56 	struct uvm_object *uobj;
     57 	vsize_t sz;
     58 	vaddr_t va;
     59 	int error;
     60 
     61 	/*
     62 	 * kuser_helper code share the page prepared for sigcode.
     63 	 * See also sys/compat/linux32/arch/aarch64/linux32_sigcode.S
     64 	 */
     65 	if (e->e_sigobject == NULL)
     66 		return 0;
     67 	uobj = *e->e_sigobject;
     68 	if (uobj == NULL)
     69 		return 0;
     70 
     71 	va = LINUX32_ARM_KUSER_HELPER_ADDR;
     72 	sz = LINUX32_ARM_KUSER_HELPER_SIZE;
     73 
     74 	(*uobj->pgops->pgo_reference)(uobj);
     75 	error = uvm_map(&p->p_vmspace->vm_map, &va, round_page(sz), uobj, 0, 0,
     76 	    UVM_MAPFLAG(UVM_PROT_RX, UVM_PROT_RX,
     77 	    UVM_INH_SHARE, UVM_ADV_RANDOM, 0));
     78 	if (error) {
     79 		(*uobj->pgops->pgo_detach)(uobj);
     80 		return error;
     81 	}
     82 
     83 	return 0;
     84 }
     85 #endif
     86 
     87 int
     88 linux32_exec_setup_stack(struct lwp *l, struct exec_package *epp)
     89 {
     90 	vaddr_t access_linear_min, noaccess_linear_min;
     91 	vsize_t access_size, noaccess_size;
     92 	vsize_t max_stack_size;
     93 
     94 #ifndef LINUX32_USRSTACK
     95 #define LINUX32_USRSTACK	USRSTACK32
     96 #endif
     97 #ifndef LINUX32_MAXSSIZ
     98 #define LINUX32_MAXSSIZ		MAXSSIZ32
     99 #endif
    100 
    101 	KASSERT((epp->ep_flags & EXEC_32) != 0);
    102 	epp->ep_minsaddr = LINUX32_USRSTACK;
    103 	max_stack_size = LINUX32_MAXSSIZ;
    104 
    105 	epp->ep_ssize =
    106 	    MIN(l->l_proc->p_rlimit[RLIMIT_STACK].rlim_cur, max_stack_size);
    107 	epp->ep_maxsaddr =
    108 	    (vaddr_t)STACK_GROW(epp->ep_minsaddr, max_stack_size);
    109 
    110 	l->l_proc->p_stackbase = epp->ep_minsaddr;
    111 
    112 	/*
    113 	 * set up commands for stack.  note that this takes *two*, one to
    114 	 * map the part of the stack which we can access, and one to map
    115 	 * the part which we can't.
    116 	 *
    117 	 * arguably, it could be made into one, but that would require the
    118 	 * addition of another mapping proc, which is unnecessary
    119 	 */
    120 	access_size = epp->ep_ssize;
    121 	access_linear_min = (vaddr_t)STACK_ALLOC(epp->ep_minsaddr, access_size);
    122 	noaccess_size = max_stack_size - access_size;
    123 	noaccess_linear_min = (vaddr_t)STACK_ALLOC(STACK_GROW(epp->ep_minsaddr,
    124 	    access_size), noaccess_size);
    125 
    126 	if (noaccess_size > 0 && noaccess_size <= max_stack_size) {
    127 		NEW_VMCMD2(&epp->ep_vmcmds, vmcmd_map_zero, noaccess_size,
    128 		    noaccess_linear_min, NULLVP, 0, VM_PROT_NONE, VMCMD_STACK);
    129 	}
    130 	KASSERT(access_size > 0);
    131 	KASSERT(access_size <= max_stack_size);
    132 	NEW_VMCMD2(&epp->ep_vmcmds, vmcmd_map_zero, access_size,
    133 	    access_linear_min, NULLVP, 0, VM_PROT_READ | VM_PROT_WRITE,
    134 	    VMCMD_STACK);
    135 
    136 #ifdef LINUX32_ARM_KUSER_HELPER_ADDR
    137 	NEW_VMCMD2(&epp->ep_vmcmds, vmcmd_linux32_kuser_helper_map,
    138 	    LINUX32_ARM_KUSER_HELPER_SIZE, LINUX32_ARM_KUSER_HELPER_ADDR,
    139 	    NULLVP, 0, VM_PROT_READ | VM_PROT_EXECUTE, 0);
    140 #endif /* LINUX32_ARM_KUSER_HELPER_ADDR */
    141 
    142 	return 0;
    143 }
    144