xref: /netbsd-src/sys/compat/linux32/arch/aarch64/linux32_exec_machdep.c (revision 9234d94be5b1b761d0fd0ea308b152b400f74da4)
1*9234d94bSriastradh /*	$NetBSD: linux32_exec_machdep.c,v 1.2 2023/04/09 12:29:26 riastradh Exp $	*/
22f5e5e73Sryo 
32f5e5e73Sryo /*
42f5e5e73Sryo  * Copyright (c) 1993, 1994, 1996 Christopher G. Demetriou
52f5e5e73Sryo  * All rights reserved.
62f5e5e73Sryo  *
72f5e5e73Sryo  * Redistribution and use in source and binary forms, with or without
82f5e5e73Sryo  * modification, are permitted provided that the following conditions
92f5e5e73Sryo  * are met:
102f5e5e73Sryo  * 1. Redistributions of source code must retain the above copyright
112f5e5e73Sryo  *    notice, this list of conditions and the following disclaimer.
122f5e5e73Sryo  * 2. Redistributions in binary form must reproduce the above copyright
132f5e5e73Sryo  *    notice, this list of conditions and the following disclaimer in the
142f5e5e73Sryo  *    documentation and/or other materials provided with the distribution.
152f5e5e73Sryo  * 3. All advertising materials mentioning features or use of this software
162f5e5e73Sryo  *    must display the following acknowledgement:
172f5e5e73Sryo  *      This product includes software developed by Christopher G. Demetriou.
182f5e5e73Sryo  * 4. The name of the author may not be used to endorse or promote products
192f5e5e73Sryo  *    derived from this software without specific prior written permission
202f5e5e73Sryo  *
212f5e5e73Sryo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
222f5e5e73Sryo  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
232f5e5e73Sryo  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
242f5e5e73Sryo  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
252f5e5e73Sryo  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
262f5e5e73Sryo  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
272f5e5e73Sryo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
282f5e5e73Sryo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
292f5e5e73Sryo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
302f5e5e73Sryo  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
312f5e5e73Sryo  */
322f5e5e73Sryo 
332f5e5e73Sryo #include <sys/cdefs.h>
34*9234d94bSriastradh __KERNEL_RCSID(0, "$NetBSD: linux32_exec_machdep.c,v 1.2 2023/04/09 12:29:26 riastradh Exp $");
352f5e5e73Sryo 
362f5e5e73Sryo #include <sys/param.h>
372f5e5e73Sryo #include <sys/types.h>
382f5e5e73Sryo #include <sys/exec.h>
392f5e5e73Sryo #include <sys/lwp.h>
402f5e5e73Sryo #include <sys/syscallargs.h>
412f5e5e73Sryo #include <sys/vnode.h>
422f5e5e73Sryo 
432f5e5e73Sryo #include <machine/vmparam.h>
442f5e5e73Sryo 
452f5e5e73Sryo #include <compat/linux/common/linux_types.h>
462f5e5e73Sryo #include <compat/linux/common/linux_exec.h>
472f5e5e73Sryo #include <compat/linux32/common/linux32_exec.h>
482f5e5e73Sryo 
492f5e5e73Sryo #ifdef LINUX32_ARM_KUSER_HELPER_ADDR
502f5e5e73Sryo static int
vmcmd_linux32_kuser_helper_map(struct lwp * l,struct exec_vmcmd * cmd)512f5e5e73Sryo vmcmd_linux32_kuser_helper_map(struct lwp *l, struct exec_vmcmd *cmd)
522f5e5e73Sryo {
532f5e5e73Sryo 	const struct proc *p = l->l_proc;
542f5e5e73Sryo 	/* l->l_proc->p_emul still points to emul_netbsd at this point */
552f5e5e73Sryo 	const struct emul *e = &emul_linux32;
562f5e5e73Sryo 	struct uvm_object *uobj;
572f5e5e73Sryo 	vsize_t sz;
582f5e5e73Sryo 	vaddr_t va;
592f5e5e73Sryo 	int error;
602f5e5e73Sryo 
612f5e5e73Sryo 	/*
622f5e5e73Sryo 	 * kuser_helper code share the page prepared for sigcode.
632f5e5e73Sryo 	 * See also sys/compat/linux32/arch/aarch64/linux32_sigcode.S
642f5e5e73Sryo 	 */
652f5e5e73Sryo 	if (e->e_sigobject == NULL)
662f5e5e73Sryo 		return 0;
672f5e5e73Sryo 	uobj = *e->e_sigobject;
682f5e5e73Sryo 	if (uobj == NULL)
692f5e5e73Sryo 		return 0;
702f5e5e73Sryo 
712f5e5e73Sryo 	va = LINUX32_ARM_KUSER_HELPER_ADDR;
722f5e5e73Sryo 	sz = LINUX32_ARM_KUSER_HELPER_SIZE;
732f5e5e73Sryo 
742f5e5e73Sryo 	(*uobj->pgops->pgo_reference)(uobj);
752f5e5e73Sryo 	error = uvm_map(&p->p_vmspace->vm_map, &va, round_page(sz), uobj, 0, 0,
762f5e5e73Sryo 	    UVM_MAPFLAG(UVM_PROT_RX, UVM_PROT_RX,
772f5e5e73Sryo 	    UVM_INH_SHARE, UVM_ADV_RANDOM, 0));
782f5e5e73Sryo 	if (error) {
792f5e5e73Sryo 		(*uobj->pgops->pgo_detach)(uobj);
802f5e5e73Sryo 		return error;
812f5e5e73Sryo 	}
822f5e5e73Sryo 
832f5e5e73Sryo 	return 0;
842f5e5e73Sryo }
852f5e5e73Sryo #endif
862f5e5e73Sryo 
872f5e5e73Sryo int
linux32_exec_setup_stack(struct lwp * l,struct exec_package * epp)882f5e5e73Sryo linux32_exec_setup_stack(struct lwp *l, struct exec_package *epp)
892f5e5e73Sryo {
902f5e5e73Sryo 	vaddr_t access_linear_min, noaccess_linear_min;
912f5e5e73Sryo 	vsize_t access_size, noaccess_size;
922f5e5e73Sryo 	vsize_t max_stack_size;
932f5e5e73Sryo 
942f5e5e73Sryo #ifndef LINUX32_USRSTACK
952f5e5e73Sryo #define LINUX32_USRSTACK	USRSTACK32
962f5e5e73Sryo #endif
972f5e5e73Sryo #ifndef LINUX32_MAXSSIZ
982f5e5e73Sryo #define LINUX32_MAXSSIZ		MAXSSIZ32
992f5e5e73Sryo #endif
1002f5e5e73Sryo 
1012f5e5e73Sryo 	KASSERT((epp->ep_flags & EXEC_32) != 0);
1022f5e5e73Sryo 	epp->ep_minsaddr = LINUX32_USRSTACK;
1032f5e5e73Sryo 	max_stack_size = LINUX32_MAXSSIZ;
1042f5e5e73Sryo 
1052f5e5e73Sryo 	epp->ep_ssize =
1062f5e5e73Sryo 	    MIN(l->l_proc->p_rlimit[RLIMIT_STACK].rlim_cur, max_stack_size);
1072f5e5e73Sryo 	epp->ep_maxsaddr =
1082f5e5e73Sryo 	    (vaddr_t)STACK_GROW(epp->ep_minsaddr, max_stack_size);
1092f5e5e73Sryo 
1102f5e5e73Sryo 	l->l_proc->p_stackbase = epp->ep_minsaddr;
1112f5e5e73Sryo 
1122f5e5e73Sryo 	/*
1132f5e5e73Sryo 	 * set up commands for stack.  note that this takes *two*, one to
1142f5e5e73Sryo 	 * map the part of the stack which we can access, and one to map
1152f5e5e73Sryo 	 * the part which we can't.
1162f5e5e73Sryo 	 *
1172f5e5e73Sryo 	 * arguably, it could be made into one, but that would require the
1182f5e5e73Sryo 	 * addition of another mapping proc, which is unnecessary
1192f5e5e73Sryo 	 */
1202f5e5e73Sryo 	access_size = epp->ep_ssize;
1212f5e5e73Sryo 	access_linear_min = (vaddr_t)STACK_ALLOC(epp->ep_minsaddr, access_size);
1222f5e5e73Sryo 	noaccess_size = max_stack_size - access_size;
1232f5e5e73Sryo 	noaccess_linear_min = (vaddr_t)STACK_ALLOC(STACK_GROW(epp->ep_minsaddr,
1242f5e5e73Sryo 	    access_size), noaccess_size);
1252f5e5e73Sryo 
1262f5e5e73Sryo 	if (noaccess_size > 0 && noaccess_size <= max_stack_size) {
1272f5e5e73Sryo 		NEW_VMCMD2(&epp->ep_vmcmds, vmcmd_map_zero, noaccess_size,
1282f5e5e73Sryo 		    noaccess_linear_min, NULLVP, 0, VM_PROT_NONE, VMCMD_STACK);
1292f5e5e73Sryo 	}
130*9234d94bSriastradh 	KASSERT(access_size > 0);
131*9234d94bSriastradh 	KASSERT(access_size <= max_stack_size);
1322f5e5e73Sryo 	NEW_VMCMD2(&epp->ep_vmcmds, vmcmd_map_zero, access_size,
1332f5e5e73Sryo 	    access_linear_min, NULLVP, 0, VM_PROT_READ | VM_PROT_WRITE,
1342f5e5e73Sryo 	    VMCMD_STACK);
1352f5e5e73Sryo 
1362f5e5e73Sryo #ifdef LINUX32_ARM_KUSER_HELPER_ADDR
1372f5e5e73Sryo 	NEW_VMCMD2(&epp->ep_vmcmds, vmcmd_linux32_kuser_helper_map,
1382f5e5e73Sryo 	    LINUX32_ARM_KUSER_HELPER_SIZE, LINUX32_ARM_KUSER_HELPER_ADDR,
1392f5e5e73Sryo 	    NULLVP, 0, VM_PROT_READ | VM_PROT_EXECUTE, 0);
1402f5e5e73Sryo #endif /* LINUX32_ARM_KUSER_HELPER_ADDR */
1412f5e5e73Sryo 
1422f5e5e73Sryo 	return 0;
1432f5e5e73Sryo }
144