1 /*        $NetBSD: linux_exec.c,v 1.124 2020/05/03 01:06:56 thorpej Exp $       */
2 
3 /*-
4  * Copyright (c) 1994, 1995, 1998, 2000, 2007, 2008, 2020
5  *     The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Christos Zoulas, Frank van der Linden, Eric Haszlakiewicz and
10  * Thor Lancelot Simon.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: linux_exec.c,v 1.124 2020/05/03 01:06:56 thorpej Exp $");
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/lwp.h>
41 #include <sys/proc.h>
42 #include <sys/namei.h>
43 #include <sys/vnode.h>
44 #include <sys/mount.h>
45 #include <sys/exec.h>
46 #include <sys/exec_elf.h>
47 #include <sys/futex.h>
48 
49 #include <sys/mman.h>
50 #include <sys/syscallargs.h>
51 
52 #include <sys/ptrace.h>       /* For proc_reparent() */
53 
54 #include <uvm/uvm_extern.h>
55 
56 #include <sys/cpu.h>
57 #include <machine/reg.h>
58 
59 #include <compat/linux/common/linux_types.h>
60 #include <compat/linux/common/linux_signal.h>
61 #include <compat/linux/common/linux_util.h>
62 #include <compat/linux/common/linux_sched.h>
63 #include <compat/linux/common/linux_machdep.h>
64 #include <compat/linux/common/linux_misc.h>
65 #include <compat/linux/common/linux_exec.h>
66 #include <compat/linux/common/linux_ipc.h>
67 #include <compat/linux/common/linux_sem.h>
68 
69 #include <compat/linux/linux_syscallargs.h>
70 #include <compat/linux/linux_syscall.h>
71 #include <compat/linux/common/linux_misc.h>
72 #include <compat/linux/common/linux_errno.h>
73 #include <compat/linux/common/linux_emuldata.h>
74 
75 extern struct sysent linux_sysent[];
76 extern const uint32_t linux_sysent_nomodbits[];
77 extern const char * const linux_syscallnames[];
78 extern char linux_sigcode[], linux_esigcode[];
79 
80 /*
81  * Emulation switch.
82  */
83 
84 struct uvm_object *emul_linux_object;
85 
86 struct emul emul_linux = {
87           .e_name =           "linux",
88           .e_path =           "/emul/linux",
89 #ifndef __HAVE_MINIMAL_EMUL
90           .e_flags =                    0,
91           .e_errno =                    native_to_linux_errno,
92           .e_nosys =                    LINUX_SYS_syscall,
93           .e_nsysent =                  LINUX_SYS_NSYSENT,
94 #endif
95           .e_sysent =                   linux_sysent,
96           .e_nomodbits =                linux_sysent_nomodbits,
97           .e_syscallnames =   linux_syscallnames,
98           .e_sendsig =                  linux_sendsig,
99           .e_trapsignal =               linux_trapsignal,
100           .e_sigcode =                  linux_sigcode,
101           .e_esigcode =                 linux_esigcode,
102           .e_sigobject =                &emul_linux_object,
103           .e_setregs =                  linux_setregs,
104           .e_proc_exec =                linux_e_proc_exec,
105           .e_proc_fork =                linux_e_proc_fork,
106           .e_proc_exit =                linux_e_proc_exit,
107           .e_lwp_fork =                 linux_e_lwp_fork,
108           .e_lwp_exit =                 linux_e_lwp_exit,
109 #ifdef __HAVE_SYSCALL_INTERN
110           .e_syscall_intern = linux_syscall_intern,
111 #else
112 #error Implement __HAVE_SYSCALL_INTERN for this platform
113 #endif
114           .e_sysctlovly =               NULL,
115           .e_vm_default_addr =          uvm_default_mapaddr,
116           .e_usertrap =                 linux_usertrap,
117           .e_ucsize =                   0,
118           .e_startlwp =                 NULL
119 };
120 
121 void
linux_e_proc_exec(struct proc * p,struct exec_package * epp)122 linux_e_proc_exec(struct proc *p, struct exec_package *epp)
123 {
124           struct lwp *l;
125 
126           l = LIST_FIRST(&p->p_lwps);
127           if (l->l_emuldata == NULL) {
128                     l->l_emuldata = kmem_zalloc(sizeof(struct linux_emuldata), KM_SLEEP);
129           } else {
130                     memset(l->l_emuldata, 0, sizeof (struct linux_emuldata));
131           }
132 
133           KASSERT(p->p_nlwps == 1);
134 }
135 
136 void
linux_e_proc_exit(struct proc * p)137 linux_e_proc_exit(struct proc *p)
138 {
139           struct lwp *l;
140 
141           KASSERT(p->p_nlwps == 1);
142           l = LIST_FIRST(&p->p_lwps);
143           linux_e_lwp_exit(l);
144 }
145 
146 void
linux_e_proc_fork(struct proc * p2,struct lwp * l1,int flags)147 linux_e_proc_fork(struct proc *p2, struct lwp *l1, int flags)
148 {
149           struct linux_emuldata *led1, *led2;
150           struct lwp *l2;
151 
152           KASSERT(p2->p_nlwps == 1);
153           l2 = LIST_FIRST(&p2->p_lwps);
154           led1 = l1->l_emuldata;
155           led2 = l2->l_emuldata;
156           led2->led_child_tidptr = led1->led_child_tidptr;
157 }
158 
159 void
linux_e_lwp_fork(struct lwp * l1,struct lwp * l2)160 linux_e_lwp_fork(struct lwp *l1, struct lwp *l2)
161 {
162           struct linux_emuldata *led2;
163 
164           led2 = kmem_zalloc(sizeof(*led2), KM_SLEEP);
165           l2->l_emuldata = led2;
166 }
167 
168 void
linux_e_lwp_exit(struct lwp * l)169 linux_e_lwp_exit(struct lwp *l)
170 {
171           struct linux_emuldata *led;
172           register_t retval;
173           int error, zero = 0;
174 
175           led = l->l_emuldata;
176 
177           if (led->led_clear_tid != NULL) {
178                     /* Emulate LINUX_CLONE_CHILD_CLEARTID */
179                     error = copyout(&zero, led->led_clear_tid, sizeof(zero));
180 #ifdef DEBUG_LINUX
181                     if (error != 0)
182                               printf("%s: cannot clear TID\n", __func__);
183 #endif
184 
185                     error = linux_do_futex((int *)led->led_clear_tid, FUTEX_WAKE,
186                         INT_MAX, NULL, NULL, 0, 0, &retval);
187                     if (error)
188                               printf("%s: linux_sys_futex failed\n", __func__);
189           }
190 
191           led = l->l_emuldata;
192           l->l_emuldata = NULL;
193           kmem_free(led, sizeof(*led));
194 }
195