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"
33 #include "opt_kdtrace.h"
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/imgact.h>
38 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/mutex.h>
45 #include <sys/syscallsubr.h>
46 #include <sys/sysent.h>
47 #include <sys/sysproto.h>
48 #include <sys/unistd.h>
51 #include <machine/../linux32/linux.h>
52 #include <machine/../linux32/linux32_proto.h>
54 #include <machine/../linux/linux.h>
55 #include <machine/../linux/linux_proto.h>
58 #include <compat/linux/linux_dtrace.h>
59 #include <compat/linux/linux_emul.h>
60 #include <compat/linux/linux_futex.h>
61 #include <compat/linux/linux_misc.h>
64 * Special DTrace provider for the linuxulator.
66 * In this file we define the provider for the entire linuxulator. All
67 * modules (= files of the linuxulator) use it.
69 * We define a different name depending on the emulated bitsize, see
70 * ../../<ARCH>/linux{,32}/linux.h, e.g.:
71 * native bitsize = linuxulator
72 * amd64, 32bit emulation = linuxulator32
74 LIN_SDT_PROVIDER_DEFINE(LINUX_DTRACE);
77 * Special DTrace module "locks", it covers some linuxulator internal
80 LIN_SDT_PROBE_DEFINE1(locks, emul_lock, locked, "struct mtx *");
81 LIN_SDT_PROBE_DEFINE1(locks, emul_lock, unlock, "struct mtx *");
82 LIN_SDT_PROBE_DEFINE1(locks, emul_shared_rlock, locked, "struct sx *");
83 LIN_SDT_PROBE_DEFINE1(locks, emul_shared_rlock, unlock, "struct sx *");
84 LIN_SDT_PROBE_DEFINE1(locks, emul_shared_wlock, locked, "struct sx *");
85 LIN_SDT_PROBE_DEFINE1(locks, emul_shared_wlock, unlock, "struct sx *");
88 * DTrace probes in this module.
90 LIN_SDT_PROBE_DEFINE2(emul, em_find, entry, "struct proc *", "int");
91 LIN_SDT_PROBE_DEFINE0(emul, em_find, return);
92 LIN_SDT_PROBE_DEFINE3(emul, proc_init, entry, "struct thread *", "pid_t",
94 LIN_SDT_PROBE_DEFINE0(emul, proc_init, create_thread);
95 LIN_SDT_PROBE_DEFINE0(emul, proc_init, fork);
96 LIN_SDT_PROBE_DEFINE0(emul, proc_init, exec);
97 LIN_SDT_PROBE_DEFINE0(emul, proc_init, return);
98 LIN_SDT_PROBE_DEFINE1(emul, proc_exit, entry, "struct proc *");
99 LIN_SDT_PROBE_DEFINE0(emul, proc_exit, futex_failed);
100 LIN_SDT_PROBE_DEFINE3(emul, proc_exit, reparent, "pid_t", "pid_t",
102 LIN_SDT_PROBE_DEFINE1(emul, proc_exit, child_clear_tid_error, "int");
103 LIN_SDT_PROBE_DEFINE0(emul, proc_exit, return);
104 LIN_SDT_PROBE_DEFINE2(emul, proc_exec, entry, "struct proc *",
105 "struct image_params *");
106 LIN_SDT_PROBE_DEFINE0(emul, proc_exec, return);
107 LIN_SDT_PROBE_DEFINE0(emul, linux_schedtail, entry);
108 LIN_SDT_PROBE_DEFINE1(emul, linux_schedtail, copyout_error, "int");
109 LIN_SDT_PROBE_DEFINE0(emul, linux_schedtail, return);
110 LIN_SDT_PROBE_DEFINE1(emul, linux_set_tid_address, entry, "int *");
111 LIN_SDT_PROBE_DEFINE0(emul, linux_set_tid_address, return);
112 LIN_SDT_PROBE_DEFINE2(emul, linux_kill_threads, entry, "struct thread *",
114 LIN_SDT_PROBE_DEFINE1(emul, linux_kill_threads, kill, "pid_t");
115 LIN_SDT_PROBE_DEFINE0(emul, linux_kill_threads, return);
117 struct sx emul_shared_lock;
118 struct mtx emul_lock;
120 /* this returns locked reference to the emuldata entry (if found) */
121 struct linux_emuldata *
122 em_find(struct proc *p, int locked)
124 struct linux_emuldata *em;
126 LIN_SDT_PROBE2(emul, em_find, entry, p, locked);
128 if (locked == EMUL_DOLOCK)
129 EMUL_LOCK(&emul_lock);
133 if (em == NULL && locked == EMUL_DOLOCK)
134 EMUL_UNLOCK(&emul_lock);
136 LIN_SDT_PROBE1(emul, em_find, return, em);
141 linux_proc_init(struct thread *td, pid_t child, int flags)
143 struct linux_emuldata *em, *p_em;
146 LIN_SDT_PROBE3(emul, proc_init, entry, td, child, flags);
149 /* fork or create a thread */
150 em = malloc(sizeof *em, M_LINUX, M_WAITOK | M_ZERO);
152 em->pdeath_signal = 0;
154 em->robust_futexes = NULL;
155 if (flags & LINUX_CLONE_THREAD) {
156 /* handled later in the code */
157 LIN_SDT_PROBE0(emul, proc_init, create_thread);
159 struct linux_emuldata_shared *s;
161 LIN_SDT_PROBE0(emul, proc_init, fork);
163 s = malloc(sizeof *s, M_LINUX, M_WAITOK | M_ZERO);
165 s->group_pid = child;
167 LIST_INIT(&s->threads);
172 LIN_SDT_PROBE0(emul, proc_init, exec);
174 /* lookup the old one */
175 em = em_find(td->td_proc, EMUL_DOLOCK);
176 KASSERT(em != NULL, ("proc_init: emuldata not found in exec case.\n"));
179 em->child_clear_tid = NULL;
180 em->child_set_tid = NULL;
183 * allocate the shared struct only in clone()/fork cases in the case
184 * of clone() td = calling proc and child = pid of the newly created
188 if (flags & LINUX_CLONE_THREAD) {
189 /* lookup the parent */
191 * we dont have to lock the p_em because
192 * its waiting for us in linux_clone so
193 * there is no chance of it changing the
194 * p_em->shared address
196 p_em = em_find(td->td_proc, EMUL_DONTLOCK);
197 KASSERT(p_em != NULL, ("proc_init: parent emuldata not found for CLONE_THREAD\n"));
198 em->shared = p_em->shared;
199 EMUL_SHARED_WLOCK(&emul_shared_lock);
201 EMUL_SHARED_WUNLOCK(&emul_shared_lock);
204 * handled earlier to avoid malloc(M_WAITOK) with
209 EMUL_SHARED_WLOCK(&emul_shared_lock);
210 LIST_INSERT_HEAD(&em->shared->threads, em, threads);
211 EMUL_SHARED_WUNLOCK(&emul_shared_lock);
214 KASSERT(p != NULL, ("process not found in proc_init\n"));
218 EMUL_UNLOCK(&emul_lock);
220 LIN_SDT_PROBE0(emul, proc_init, return);
225 linux_proc_exit(void *arg __unused, struct proc *p)
227 struct linux_emuldata *em;
228 int error, shared_flags, shared_xstat;
229 struct thread *td = FIRST_THREAD_IN_PROC(p);
230 int *child_clear_tid;
233 if (__predict_true(p->p_sysent != &elf_linux_sysvec))
236 LIN_SDT_PROBE1(emul, proc_exit, entry, p);
240 /* find the emuldata */
241 em = em_find(p, EMUL_DOLOCK);
243 KASSERT(em != NULL, ("proc_exit: emuldata not found.\n"));
245 /* reparent all procs that are not a thread leader to initproc */
246 if (em->shared->group_pid != p->p_pid) {
247 LIN_SDT_PROBE3(emul, proc_exit, reparent,
248 em->shared->group_pid, p->p_pid, p);
250 child_clear_tid = em->child_clear_tid;
251 EMUL_UNLOCK(&emul_lock);
252 sx_xlock(&proctree_lock);
255 proc_reparent(p, initproc);
256 p->p_sigparent = SIGCHLD;
258 sx_xunlock(&proctree_lock);
260 child_clear_tid = em->child_clear_tid;
261 EMUL_UNLOCK(&emul_lock);
264 EMUL_SHARED_WLOCK(&emul_shared_lock);
265 shared_flags = em->shared->flags;
266 shared_xstat = em->shared->xstat;
267 LIST_REMOVE(em, threads);
270 if (em->shared->refs == 0) {
271 EMUL_SHARED_WUNLOCK(&emul_shared_lock);
272 free(em->shared, M_LINUX);
274 EMUL_SHARED_WUNLOCK(&emul_shared_lock);
276 if ((shared_flags & EMUL_SHARED_HASXSTAT) != 0)
277 p->p_xstat = shared_xstat;
279 if (child_clear_tid != NULL) {
280 struct linux_sys_futex_args cup;
283 error = copyout(&null, child_clear_tid, sizeof(null));
285 LIN_SDT_PROBE1(emul, proc_exit,
286 child_clear_tid_error, error);
290 LIN_SDT_PROBE0(emul, proc_exit, return);
295 cup.uaddr = child_clear_tid;
296 cup.op = LINUX_FUTEX_WAKE;
297 cup.val = 0x7fffffff; /* Awake everyone */
301 error = linux_sys_futex(FIRST_THREAD_IN_PROC(p), &cup);
303 * this cannot happen at the moment and if this happens it
304 * probably means there is a user space bug
307 LIN_SDT_PROBE0(emul, proc_exit, futex_failed);
308 printf(LMSG("futex stuff in proc_exit failed.\n"));
312 /* clean the stuff up */
315 /* this is a little weird but rewritten from exit1() */
316 sx_xlock(&proctree_lock);
317 q = LIST_FIRST(&p->p_children);
318 for (; q != NULL; q = nq) {
319 nq = LIST_NEXT(q, p_sibling);
320 if (q->p_flag & P_WEXIT)
322 if (__predict_false(q->p_sysent != &elf_linux_sysvec))
324 em = em_find(q, EMUL_DOLOCK);
325 KASSERT(em != NULL, ("linux_reparent: emuldata not found: %i\n", q->p_pid));
327 if ((q->p_flag & P_WEXIT) == 0 && em->pdeath_signal != 0) {
328 kern_psignal(q, em->pdeath_signal);
331 EMUL_UNLOCK(&emul_lock);
333 sx_xunlock(&proctree_lock);
335 LIN_SDT_PROBE0(emul, proc_exit, return);
339 * This is used in a case of transition from FreeBSD binary execing to linux binary
340 * in this case we create linux emuldata proc entry with the pid of the currently running
344 linux_proc_exec(void *arg __unused, struct proc *p, struct image_params *imgp)
346 if (__predict_false(imgp->sysent == &elf_linux_sysvec)) {
347 LIN_SDT_PROBE2(emul, proc_exec, entry, p, imgp);
349 if (__predict_false(imgp->sysent == &elf_linux_sysvec
350 && p->p_sysent != &elf_linux_sysvec))
351 linux_proc_init(FIRST_THREAD_IN_PROC(p), p->p_pid, 0);
352 if (__predict_false((p->p_sysent->sv_flags & SV_ABI_MASK) ==
354 /* Kill threads regardless of imgp->sysent value */
355 linux_kill_threads(FIRST_THREAD_IN_PROC(p), SIGKILL);
356 if (__predict_false(imgp->sysent != &elf_linux_sysvec
357 && p->p_sysent == &elf_linux_sysvec)) {
358 struct linux_emuldata *em;
361 * XXX:There's a race because here we assign p->p_emuldata NULL
362 * but the process is still counted as linux one for a short
363 * time so some other process might reference it and try to
364 * access its p->p_emuldata and panicing on a NULL reference.
366 em = em_find(p, EMUL_DONTLOCK);
368 KASSERT(em != NULL, ("proc_exec: emuldata not found.\n"));
370 EMUL_SHARED_WLOCK(&emul_shared_lock);
371 LIST_REMOVE(em, threads);
374 p->p_emuldata = NULL;
378 if (em->shared->refs == 0) {
379 EMUL_SHARED_WUNLOCK(&emul_shared_lock);
380 free(em->shared, M_LINUX);
382 EMUL_SHARED_WUNLOCK(&emul_shared_lock);
387 if (__predict_false(imgp->sysent == &elf_linux_sysvec)) {
388 LIN_SDT_PROBE0(emul, proc_exec, return);
393 linux_schedtail(struct thread *td)
395 struct linux_emuldata *em;
402 LIN_SDT_PROBE1(emul, linux_schedtail, entry, p);
404 /* find the emuldata */
405 em = em_find(p, EMUL_DOLOCK);
407 KASSERT(em != NULL, ("linux_schedtail: emuldata not found.\n"));
408 child_set_tid = em->child_set_tid;
409 EMUL_UNLOCK(&emul_lock);
411 if (child_set_tid != NULL) {
412 error = copyout(&p->p_pid, (int *)child_set_tid,
416 LIN_SDT_PROBE1(emul, linux_schedtail, copyout_error,
421 LIN_SDT_PROBE0(emul, linux_schedtail, return);
427 linux_set_tid_address(struct thread *td, struct linux_set_tid_address_args *args)
429 struct linux_emuldata *em;
431 LIN_SDT_PROBE1(emul, linux_set_tid_address, entry, args->tidptr);
433 /* find the emuldata */
434 em = em_find(td->td_proc, EMUL_DOLOCK);
436 KASSERT(em != NULL, ("set_tid_address: emuldata not found.\n"));
438 em->child_clear_tid = args->tidptr;
439 td->td_retval[0] = td->td_proc->p_pid;
441 EMUL_UNLOCK(&emul_lock);
443 LIN_SDT_PROBE0(emul, linux_set_tid_address, return);
448 linux_kill_threads(struct thread *td, int sig)
450 struct linux_emuldata *em, *td_em, *tmp_em;
453 LIN_SDT_PROBE2(emul, linux_kill_threads, entry, td, sig);
455 td_em = em_find(td->td_proc, EMUL_DONTLOCK);
457 KASSERT(td_em != NULL, ("linux_kill_threads: emuldata not found.\n"));
459 EMUL_SHARED_RLOCK(&emul_shared_lock);
460 LIST_FOREACH_SAFE(em, &td_em->shared->threads, threads, tmp_em) {
461 if (em->pid == td_em->pid)
465 if ((sp->p_flag & P_WEXIT) == 0)
466 kern_psignal(sp, sig);
469 LIN_SDT_PROBE1(emul, linux_kill_threads, kill, em->pid);
471 EMUL_SHARED_RUNLOCK(&emul_shared_lock);
473 LIN_SDT_PROBE0(emul, linux_kill_threads, return);