1 1.2 riastrad /* $NetBSD: linux32_exec_machdep.c,v 1.2 2023/04/09 12:29:26 riastradh Exp $ */ 2 1.1 ryo 3 1.1 ryo /* 4 1.1 ryo * Copyright (c) 1993, 1994, 1996 Christopher G. Demetriou 5 1.1 ryo * All rights reserved. 6 1.1 ryo * 7 1.1 ryo * Redistribution and use in source and binary forms, with or without 8 1.1 ryo * modification, are permitted provided that the following conditions 9 1.1 ryo * are met: 10 1.1 ryo * 1. Redistributions of source code must retain the above copyright 11 1.1 ryo * notice, this list of conditions and the following disclaimer. 12 1.1 ryo * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 ryo * notice, this list of conditions and the following disclaimer in the 14 1.1 ryo * documentation and/or other materials provided with the distribution. 15 1.1 ryo * 3. All advertising materials mentioning features or use of this software 16 1.1 ryo * must display the following acknowledgement: 17 1.1 ryo * This product includes software developed by Christopher G. Demetriou. 18 1.1 ryo * 4. The name of the author may not be used to endorse or promote products 19 1.1 ryo * derived from this software without specific prior written permission 20 1.1 ryo * 21 1.1 ryo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 1.1 ryo * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 1.1 ryo * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 1.1 ryo * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 1.1 ryo * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 1.1 ryo * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 1.1 ryo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 1.1 ryo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 1.1 ryo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 1.1 ryo * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 1.1 ryo */ 32 1.1 ryo 33 1.1 ryo #include <sys/cdefs.h> 34 1.2 riastrad __KERNEL_RCSID(0, "$NetBSD: linux32_exec_machdep.c,v 1.2 2023/04/09 12:29:26 riastradh Exp $"); 35 1.1 ryo 36 1.1 ryo #include <sys/param.h> 37 1.1 ryo #include <sys/types.h> 38 1.1 ryo #include <sys/exec.h> 39 1.1 ryo #include <sys/lwp.h> 40 1.1 ryo #include <sys/syscallargs.h> 41 1.1 ryo #include <sys/vnode.h> 42 1.1 ryo 43 1.1 ryo #include <machine/vmparam.h> 44 1.1 ryo 45 1.1 ryo #include <compat/linux/common/linux_types.h> 46 1.1 ryo #include <compat/linux/common/linux_exec.h> 47 1.1 ryo #include <compat/linux32/common/linux32_exec.h> 48 1.1 ryo 49 1.1 ryo #ifdef LINUX32_ARM_KUSER_HELPER_ADDR 50 1.1 ryo static int 51 1.1 ryo vmcmd_linux32_kuser_helper_map(struct lwp *l, struct exec_vmcmd *cmd) 52 1.1 ryo { 53 1.1 ryo const struct proc *p = l->l_proc; 54 1.1 ryo /* l->l_proc->p_emul still points to emul_netbsd at this point */ 55 1.1 ryo const struct emul *e = &emul_linux32; 56 1.1 ryo struct uvm_object *uobj; 57 1.1 ryo vsize_t sz; 58 1.1 ryo vaddr_t va; 59 1.1 ryo int error; 60 1.1 ryo 61 1.1 ryo /* 62 1.1 ryo * kuser_helper code share the page prepared for sigcode. 63 1.1 ryo * See also sys/compat/linux32/arch/aarch64/linux32_sigcode.S 64 1.1 ryo */ 65 1.1 ryo if (e->e_sigobject == NULL) 66 1.1 ryo return 0; 67 1.1 ryo uobj = *e->e_sigobject; 68 1.1 ryo if (uobj == NULL) 69 1.1 ryo return 0; 70 1.1 ryo 71 1.1 ryo va = LINUX32_ARM_KUSER_HELPER_ADDR; 72 1.1 ryo sz = LINUX32_ARM_KUSER_HELPER_SIZE; 73 1.1 ryo 74 1.1 ryo (*uobj->pgops->pgo_reference)(uobj); 75 1.1 ryo error = uvm_map(&p->p_vmspace->vm_map, &va, round_page(sz), uobj, 0, 0, 76 1.1 ryo UVM_MAPFLAG(UVM_PROT_RX, UVM_PROT_RX, 77 1.1 ryo UVM_INH_SHARE, UVM_ADV_RANDOM, 0)); 78 1.1 ryo if (error) { 79 1.1 ryo (*uobj->pgops->pgo_detach)(uobj); 80 1.1 ryo return error; 81 1.1 ryo } 82 1.1 ryo 83 1.1 ryo return 0; 84 1.1 ryo } 85 1.1 ryo #endif 86 1.1 ryo 87 1.1 ryo int 88 1.1 ryo linux32_exec_setup_stack(struct lwp *l, struct exec_package *epp) 89 1.1 ryo { 90 1.1 ryo vaddr_t access_linear_min, noaccess_linear_min; 91 1.1 ryo vsize_t access_size, noaccess_size; 92 1.1 ryo vsize_t max_stack_size; 93 1.1 ryo 94 1.1 ryo #ifndef LINUX32_USRSTACK 95 1.1 ryo #define LINUX32_USRSTACK USRSTACK32 96 1.1 ryo #endif 97 1.1 ryo #ifndef LINUX32_MAXSSIZ 98 1.1 ryo #define LINUX32_MAXSSIZ MAXSSIZ32 99 1.1 ryo #endif 100 1.1 ryo 101 1.1 ryo KASSERT((epp->ep_flags & EXEC_32) != 0); 102 1.1 ryo epp->ep_minsaddr = LINUX32_USRSTACK; 103 1.1 ryo max_stack_size = LINUX32_MAXSSIZ; 104 1.1 ryo 105 1.1 ryo epp->ep_ssize = 106 1.1 ryo MIN(l->l_proc->p_rlimit[RLIMIT_STACK].rlim_cur, max_stack_size); 107 1.1 ryo epp->ep_maxsaddr = 108 1.1 ryo (vaddr_t)STACK_GROW(epp->ep_minsaddr, max_stack_size); 109 1.1 ryo 110 1.1 ryo l->l_proc->p_stackbase = epp->ep_minsaddr; 111 1.1 ryo 112 1.1 ryo /* 113 1.1 ryo * set up commands for stack. note that this takes *two*, one to 114 1.1 ryo * map the part of the stack which we can access, and one to map 115 1.1 ryo * the part which we can't. 116 1.1 ryo * 117 1.1 ryo * arguably, it could be made into one, but that would require the 118 1.1 ryo * addition of another mapping proc, which is unnecessary 119 1.1 ryo */ 120 1.1 ryo access_size = epp->ep_ssize; 121 1.1 ryo access_linear_min = (vaddr_t)STACK_ALLOC(epp->ep_minsaddr, access_size); 122 1.1 ryo noaccess_size = max_stack_size - access_size; 123 1.1 ryo noaccess_linear_min = (vaddr_t)STACK_ALLOC(STACK_GROW(epp->ep_minsaddr, 124 1.1 ryo access_size), noaccess_size); 125 1.1 ryo 126 1.1 ryo if (noaccess_size > 0 && noaccess_size <= max_stack_size) { 127 1.1 ryo NEW_VMCMD2(&epp->ep_vmcmds, vmcmd_map_zero, noaccess_size, 128 1.1 ryo noaccess_linear_min, NULLVP, 0, VM_PROT_NONE, VMCMD_STACK); 129 1.1 ryo } 130 1.2 riastrad KASSERT(access_size > 0); 131 1.2 riastrad KASSERT(access_size <= max_stack_size); 132 1.1 ryo NEW_VMCMD2(&epp->ep_vmcmds, vmcmd_map_zero, access_size, 133 1.1 ryo access_linear_min, NULLVP, 0, VM_PROT_READ | VM_PROT_WRITE, 134 1.1 ryo VMCMD_STACK); 135 1.1 ryo 136 1.1 ryo #ifdef LINUX32_ARM_KUSER_HELPER_ADDR 137 1.1 ryo NEW_VMCMD2(&epp->ep_vmcmds, vmcmd_linux32_kuser_helper_map, 138 1.1 ryo LINUX32_ARM_KUSER_HELPER_SIZE, LINUX32_ARM_KUSER_HELPER_ADDR, 139 1.1 ryo NULLVP, 0, VM_PROT_READ | VM_PROT_EXECUTE, 0); 140 1.1 ryo #endif /* LINUX32_ARM_KUSER_HELPER_ADDR */ 141 1.1 ryo 142 1.1 ryo return 0; 143 1.1 ryo } 144