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/sysproto.h>
45 #include <sys/unistd.h>
48 #include <machine/../linux32/linux.h>
49 #include <machine/../linux32/linux32_proto.h>
51 #include <machine/../linux/linux.h>
52 #include <machine/../linux/linux_proto.h>
55 #include <compat/linux/linux_emul.h>
56 #include <compat/linux/linux_futex.h>
58 struct sx emul_shared_lock;
61 /* this returns locked reference to the emuldata entry (if found) */
62 struct linux_emuldata *
63 em_find(struct proc *p, int locked)
65 struct linux_emuldata *em;
67 if (locked == EMUL_DOLOCK)
68 EMUL_LOCK(&emul_lock);
72 if (em == NULL && locked == EMUL_DOLOCK)
73 EMUL_UNLOCK(&emul_lock);
79 linux_proc_init(struct thread *td, pid_t child, int flags)
81 struct linux_emuldata *em, *p_em;
86 em = malloc(sizeof *em, M_LINUX, M_WAITOK | M_ZERO);
88 em->pdeath_signal = 0;
90 em->robust_futexes = NULL;
91 if (flags & LINUX_CLONE_THREAD) {
92 /* handled later in the code */
94 struct linux_emuldata_shared *s;
96 s = malloc(sizeof *s, M_LINUX, M_WAITOK | M_ZERO);
100 LIST_INIT(&s->threads);
104 /* lookup the old one */
105 em = em_find(td->td_proc, EMUL_DOLOCK);
106 KASSERT(em != NULL, ("proc_init: emuldata not found in exec case.\n"));
109 em->child_clear_tid = NULL;
110 em->child_set_tid = NULL;
113 * allocate the shared struct only in clone()/fork cases in the case
114 * of clone() td = calling proc and child = pid of the newly created
118 if (flags & LINUX_CLONE_THREAD) {
119 /* lookup the parent */
121 * we dont have to lock the p_em because
122 * its waiting for us in linux_clone so
123 * there is no chance of it changing the
124 * p_em->shared address
126 p_em = em_find(td->td_proc, EMUL_DONTLOCK);
127 KASSERT(p_em != NULL, ("proc_init: parent emuldata not found for CLONE_THREAD\n"));
128 em->shared = p_em->shared;
129 EMUL_SHARED_WLOCK(&emul_shared_lock);
131 EMUL_SHARED_WUNLOCK(&emul_shared_lock);
134 * handled earlier to avoid malloc(M_WAITOK) with
140 EMUL_SHARED_WLOCK(&emul_shared_lock);
141 LIST_INSERT_HEAD(&em->shared->threads, em, threads);
142 EMUL_SHARED_WUNLOCK(&emul_shared_lock);
145 KASSERT(p != NULL, ("process not found in proc_init\n"));
149 EMUL_UNLOCK(&emul_lock);
155 linux_proc_exit(void *arg __unused, struct proc *p)
157 struct linux_emuldata *em;
159 struct thread *td = FIRST_THREAD_IN_PROC(p);
160 int *child_clear_tid;
163 if (__predict_true(p->p_sysent != &elf_linux_sysvec))
168 /* find the emuldata */
169 em = em_find(p, EMUL_DOLOCK);
171 KASSERT(em != NULL, ("proc_exit: emuldata not found.\n"));
173 /* reparent all procs that are not a thread leader to initproc */
174 if (em->shared->group_pid != p->p_pid) {
175 child_clear_tid = em->child_clear_tid;
176 EMUL_UNLOCK(&emul_lock);
177 sx_xlock(&proctree_lock);
180 proc_reparent(p, initproc);
181 p->p_sigparent = SIGCHLD;
183 sx_xunlock(&proctree_lock);
185 child_clear_tid = em->child_clear_tid;
186 EMUL_UNLOCK(&emul_lock);
189 EMUL_SHARED_WLOCK(&emul_shared_lock);
190 LIST_REMOVE(em, threads);
193 if (em->shared->refs == 0) {
194 EMUL_SHARED_WUNLOCK(&emul_shared_lock);
195 free(em->shared, M_LINUX);
197 EMUL_SHARED_WUNLOCK(&emul_shared_lock);
199 if (child_clear_tid != NULL) {
200 struct linux_sys_futex_args cup;
203 error = copyout(&null, child_clear_tid, sizeof(null));
210 cup.uaddr = child_clear_tid;
211 cup.op = LINUX_FUTEX_WAKE;
212 cup.val = 0x7fffffff; /* Awake everyone */
216 error = linux_sys_futex(FIRST_THREAD_IN_PROC(p), &cup);
218 * this cannot happen at the moment and if this happens it
219 * probably means there is a user space bug
222 printf(LMSG("futex stuff in proc_exit failed.\n"));
225 /* clean the stuff up */
228 /* this is a little weird but rewritten from exit1() */
229 sx_xlock(&proctree_lock);
230 q = LIST_FIRST(&p->p_children);
231 for (; q != NULL; q = nq) {
232 nq = LIST_NEXT(q, p_sibling);
233 if (q->p_flag & P_WEXIT)
235 if (__predict_false(q->p_sysent != &elf_linux_sysvec))
237 em = em_find(q, EMUL_DOLOCK);
238 KASSERT(em != NULL, ("linux_reparent: emuldata not found: %i\n", q->p_pid));
240 if ((q->p_flag & P_WEXIT) == 0 && em->pdeath_signal != 0) {
241 psignal(q, em->pdeath_signal);
244 EMUL_UNLOCK(&emul_lock);
246 sx_xunlock(&proctree_lock);
250 * This is used in a case of transition from FreeBSD binary execing to linux binary
251 * in this case we create linux emuldata proc entry with the pid of the currently running
255 linux_proc_exec(void *arg __unused, struct proc *p, struct image_params *imgp)
257 if (__predict_false(imgp->sysent == &elf_linux_sysvec
258 && p->p_sysent != &elf_linux_sysvec))
259 linux_proc_init(FIRST_THREAD_IN_PROC(p), p->p_pid, 0);
260 if (__predict_false(imgp->sysent != &elf_linux_sysvec
261 && p->p_sysent == &elf_linux_sysvec)) {
262 struct linux_emuldata *em;
265 * XXX:There's a race because here we assign p->p_emuldata NULL
266 * but the process is still counted as linux one for a short
267 * time so some other process might reference it and try to
268 * access its p->p_emuldata and panicing on a NULL reference.
270 em = em_find(p, EMUL_DONTLOCK);
272 KASSERT(em != NULL, ("proc_exec: emuldata not found.\n"));
274 EMUL_SHARED_WLOCK(&emul_shared_lock);
275 LIST_REMOVE(em, threads);
278 p->p_emuldata = NULL;
282 if (em->shared->refs == 0) {
283 EMUL_SHARED_WUNLOCK(&emul_shared_lock);
284 free(em->shared, M_LINUX);
286 EMUL_SHARED_WUNLOCK(&emul_shared_lock);
293 linux_schedtail(void *arg __unused, struct proc *p)
295 struct linux_emuldata *em;
299 if (__predict_true(p->p_sysent != &elf_linux_sysvec))
302 /* find the emuldata */
303 em = em_find(p, EMUL_DOLOCK);
305 KASSERT(em != NULL, ("linux_schedtail: emuldata not found.\n"));
306 child_set_tid = em->child_set_tid;
307 EMUL_UNLOCK(&emul_lock);
309 if (child_set_tid != NULL)
310 error = copyout(&p->p_pid, (int *)child_set_tid,
317 linux_set_tid_address(struct thread *td, struct linux_set_tid_address_args *args)
319 struct linux_emuldata *em;
322 if (ldebug(set_tid_address))
323 printf(ARGS(set_tid_address, "%p"), args->tidptr);
326 /* find the emuldata */
327 em = em_find(td->td_proc, EMUL_DOLOCK);
329 KASSERT(em != NULL, ("set_tid_address: emuldata not found.\n"));
331 em->child_clear_tid = args->tidptr;
332 td->td_retval[0] = td->td_proc->p_pid;
334 EMUL_UNLOCK(&emul_lock);