2 * Copyright (c) 2006 Roman Divacky
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "opt_compat.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/imgact.h>
37 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/mutex.h>
43 #include <sys/syscallsubr.h>
44 #include <sys/sysent.h>
45 #include <sys/sysproto.h>
46 #include <sys/unistd.h>
49 #include <machine/../linux32/linux.h>
50 #include <machine/../linux32/linux32_proto.h>
52 #include <machine/../linux/linux.h>
53 #include <machine/../linux/linux_proto.h>
56 #include <compat/linux/linux_emul.h>
57 #include <compat/linux/linux_futex.h>
59 struct sx emul_shared_lock;
62 /* this returns locked reference to the emuldata entry (if found) */
63 struct linux_emuldata *
64 em_find(struct proc *p, int locked)
66 struct linux_emuldata *em;
68 if (locked == EMUL_DOLOCK)
69 EMUL_LOCK(&emul_lock);
73 if (em == NULL && locked == EMUL_DOLOCK)
74 EMUL_UNLOCK(&emul_lock);
80 linux_proc_init(struct thread *td, pid_t child, int flags)
82 struct linux_emuldata *em, *p_em;
87 em = malloc(sizeof *em, M_LINUX, M_WAITOK | M_ZERO);
89 em->pdeath_signal = 0;
91 em->robust_futexes = NULL;
92 if (flags & LINUX_CLONE_THREAD) {
93 /* handled later in the code */
95 struct linux_emuldata_shared *s;
97 s = malloc(sizeof *s, M_LINUX, M_WAITOK | M_ZERO);
101 LIST_INIT(&s->threads);
105 /* lookup the old one */
106 em = em_find(td->td_proc, EMUL_DOLOCK);
107 KASSERT(em != NULL, ("proc_init: emuldata not found in exec case.\n"));
110 em->child_clear_tid = NULL;
111 em->child_set_tid = NULL;
114 * allocate the shared struct only in clone()/fork cases in the case
115 * of clone() td = calling proc and child = pid of the newly created
119 if (flags & LINUX_CLONE_THREAD) {
120 /* lookup the parent */
122 * we dont have to lock the p_em because
123 * its waiting for us in linux_clone so
124 * there is no chance of it changing the
125 * p_em->shared address
127 p_em = em_find(td->td_proc, EMUL_DONTLOCK);
128 KASSERT(p_em != NULL, ("proc_init: parent emuldata not found for CLONE_THREAD\n"));
129 em->shared = p_em->shared;
130 EMUL_SHARED_WLOCK(&emul_shared_lock);
132 EMUL_SHARED_WUNLOCK(&emul_shared_lock);
135 * handled earlier to avoid malloc(M_WAITOK) with
141 EMUL_SHARED_WLOCK(&emul_shared_lock);
142 LIST_INSERT_HEAD(&em->shared->threads, em, threads);
143 EMUL_SHARED_WUNLOCK(&emul_shared_lock);
146 KASSERT(p != NULL, ("process not found in proc_init\n"));
150 EMUL_UNLOCK(&emul_lock);
156 linux_proc_exit(void *arg __unused, struct proc *p)
158 struct linux_emuldata *em;
159 int error, shared_flags, shared_xstat;
160 struct thread *td = FIRST_THREAD_IN_PROC(p);
161 int *child_clear_tid;
164 if (__predict_true(p->p_sysent != &elf_linux_sysvec))
169 /* find the emuldata */
170 em = em_find(p, EMUL_DOLOCK);
172 KASSERT(em != NULL, ("proc_exit: emuldata not found.\n"));
174 /* reparent all procs that are not a thread leader to initproc */
175 if (em->shared->group_pid != p->p_pid) {
176 child_clear_tid = em->child_clear_tid;
177 EMUL_UNLOCK(&emul_lock);
178 sx_xlock(&proctree_lock);
181 proc_reparent(p, initproc);
182 p->p_sigparent = SIGCHLD;
184 sx_xunlock(&proctree_lock);
186 child_clear_tid = em->child_clear_tid;
187 EMUL_UNLOCK(&emul_lock);
190 EMUL_SHARED_WLOCK(&emul_shared_lock);
191 shared_flags = em->shared->flags;
192 shared_xstat = em->shared->xstat;
193 LIST_REMOVE(em, threads);
196 if (em->shared->refs == 0) {
197 EMUL_SHARED_WUNLOCK(&emul_shared_lock);
198 free(em->shared, M_LINUX);
200 EMUL_SHARED_WUNLOCK(&emul_shared_lock);
202 if ((shared_flags & EMUL_SHARED_HASXSTAT) != 0)
203 p->p_xstat = shared_xstat;
205 if (child_clear_tid != NULL) {
206 struct linux_sys_futex_args cup;
209 error = copyout(&null, child_clear_tid, sizeof(null));
216 cup.uaddr = child_clear_tid;
217 cup.op = LINUX_FUTEX_WAKE;
218 cup.val = 0x7fffffff; /* Awake everyone */
222 error = linux_sys_futex(FIRST_THREAD_IN_PROC(p), &cup);
224 * this cannot happen at the moment and if this happens it
225 * probably means there is a user space bug
228 printf(LMSG("futex stuff in proc_exit failed.\n"));
231 /* clean the stuff up */
234 /* this is a little weird but rewritten from exit1() */
235 sx_xlock(&proctree_lock);
236 q = LIST_FIRST(&p->p_children);
237 for (; q != NULL; q = nq) {
238 nq = LIST_NEXT(q, p_sibling);
239 if (q->p_flag & P_WEXIT)
241 if (__predict_false(q->p_sysent != &elf_linux_sysvec))
243 em = em_find(q, EMUL_DOLOCK);
244 KASSERT(em != NULL, ("linux_reparent: emuldata not found: %i\n", q->p_pid));
246 if ((q->p_flag & P_WEXIT) == 0 && em->pdeath_signal != 0) {
247 kern_psignal(q, em->pdeath_signal);
250 EMUL_UNLOCK(&emul_lock);
252 sx_xunlock(&proctree_lock);
256 * This is used in a case of transition from FreeBSD binary execing to linux binary
257 * in this case we create linux emuldata proc entry with the pid of the currently running
261 linux_proc_exec(void *arg __unused, struct proc *p, struct image_params *imgp)
263 if (__predict_false(imgp->sysent == &elf_linux_sysvec
264 && p->p_sysent != &elf_linux_sysvec))
265 linux_proc_init(FIRST_THREAD_IN_PROC(p), p->p_pid, 0);
266 if (__predict_false((p->p_sysent->sv_flags & SV_ABI_MASK) ==
268 /* Kill threads regardless of imgp->sysent value */
269 linux_kill_threads(FIRST_THREAD_IN_PROC(p), SIGKILL);
270 if (__predict_false(imgp->sysent != &elf_linux_sysvec
271 && p->p_sysent == &elf_linux_sysvec)) {
272 struct linux_emuldata *em;
275 * XXX:There's a race because here we assign p->p_emuldata NULL
276 * but the process is still counted as linux one for a short
277 * time so some other process might reference it and try to
278 * access its p->p_emuldata and panicing on a NULL reference.
280 em = em_find(p, EMUL_DONTLOCK);
282 KASSERT(em != NULL, ("proc_exec: emuldata not found.\n"));
284 EMUL_SHARED_WLOCK(&emul_shared_lock);
285 LIST_REMOVE(em, threads);
288 p->p_emuldata = NULL;
292 if (em->shared->refs == 0) {
293 EMUL_SHARED_WUNLOCK(&emul_shared_lock);
294 free(em->shared, M_LINUX);
296 EMUL_SHARED_WUNLOCK(&emul_shared_lock);
303 linux_schedtail(struct thread *td)
305 struct linux_emuldata *em;
312 /* find the emuldata */
313 em = em_find(p, EMUL_DOLOCK);
315 KASSERT(em != NULL, ("linux_schedtail: emuldata not found.\n"));
316 child_set_tid = em->child_set_tid;
317 EMUL_UNLOCK(&emul_lock);
319 if (child_set_tid != NULL)
320 error = copyout(&p->p_pid, (int *)child_set_tid,
327 linux_set_tid_address(struct thread *td, struct linux_set_tid_address_args *args)
329 struct linux_emuldata *em;
332 if (ldebug(set_tid_address))
333 printf(ARGS(set_tid_address, "%p"), args->tidptr);
336 /* find the emuldata */
337 em = em_find(td->td_proc, EMUL_DOLOCK);
339 KASSERT(em != NULL, ("set_tid_address: emuldata not found.\n"));
341 em->child_clear_tid = args->tidptr;
342 td->td_retval[0] = td->td_proc->p_pid;
344 EMUL_UNLOCK(&emul_lock);
349 linux_kill_threads(struct thread *td, int sig)
351 struct linux_emuldata *em, *td_em, *tmp_em;
354 td_em = em_find(td->td_proc, EMUL_DONTLOCK);
356 KASSERT(td_em != NULL, ("linux_kill_threads: emuldata not found.\n"));
358 EMUL_SHARED_RLOCK(&emul_shared_lock);
359 LIST_FOREACH_SAFE(em, &td_em->shared->threads, threads, tmp_em) {
360 if (em->pid == td_em->pid)
364 if ((sp->p_flag & P_WEXIT) == 0)
365 kern_psignal(sp, sig);
368 printf(LMSG("linux_kill_threads: kill PID %d\n"), em->pid);
371 EMUL_SHARED_RUNLOCK(&emul_shared_lock);