2 * Copyright (c) 2006 Roman Divacky
3 * Copyright (c) 2013 Dmitry Chagin
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer
11 * in this position and unchanged.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/imgact.h>
36 #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>
48 #include <compat/linux/linux_emul.h>
49 #include <compat/linux/linux_misc.h>
50 #include <compat/linux/linux_util.h>
54 * This returns reference to the thread emuldata entry (if found)
56 * Hold PROC_LOCK when referencing emuldata from other threads.
58 struct linux_emuldata *
59 em_find(struct thread *td)
61 struct linux_emuldata *em;
69 * This returns reference to the proc pemuldata entry (if found)
71 * Hold PROC_LOCK when referencing proc pemuldata from other threads.
72 * Hold LINUX_PEM_LOCK wher referencing pemuldata members.
74 struct linux_pemuldata *
75 pem_find(struct proc *p)
77 struct linux_pemuldata *pem;
85 linux_proc_init(struct thread *td, struct thread *newtd, int flags)
87 struct linux_emuldata *em;
88 struct linux_pemuldata *pem;
92 em = malloc(sizeof(*em), M_TEMP, M_WAITOK | M_ZERO);
93 em->pdeath_signal = 0;
94 em->robust_futexes = NULL;
95 if (flags & LINUX_CLONE_THREAD) {
96 em->em_tid = newtd->td_tid;
99 em->em_tid = newtd->td_proc->p_pid;
101 pem = malloc(sizeof(*pem), M_TEMP, M_WAITOK | M_ZERO);
102 sx_init(&pem->pem_sx, "lpemlk");
103 newtd->td_proc->p_emuldata = pem;
105 newtd->td_emuldata = em;
109 /* lookup the old one */
111 KASSERT(em != NULL, ("proc_init: emuldata not found in exec case.\n"));
113 em->em_tid = td->td_proc->p_pid;
116 em->child_clear_tid = NULL;
117 em->child_set_tid = NULL;
121 linux_proc_exit(void *arg __unused, struct proc *p)
123 struct linux_pemuldata *pem;
124 struct thread *td = curthread;
126 if (__predict_false(SV_CURPROC_ABI() != SV_ABI_LINUX))
132 (p->p_sysent->sv_thread_detach)(td);
134 p->p_emuldata = NULL;
136 sx_destroy(&pem->pem_sx);
141 linux_common_execve(struct thread *td, struct image_args *eargs)
143 struct linux_pemuldata *pem;
144 struct linux_emuldata *em;
151 * Unlike FreeBSD abort all other threads before
155 /* See exit1() comments. */
156 thread_suspend_check(0);
157 while (p->p_flag & P_HADTHREADS) {
158 if (!thread_single(p, SINGLE_EXIT))
160 thread_suspend_check(0);
164 error = kern_execve(td, eargs, NULL);
169 * In a case of transition from Linux binary execing to
170 * FreeBSD binary we destroy linux emuldata thread & proc entries.
172 if (SV_CURPROC_ABI() != SV_ABI_LINUX) {
175 KASSERT(em != NULL, ("proc_exec: thread emuldata not found.\n"));
176 td->td_emuldata = NULL;
179 KASSERT(pem != NULL, ("proc_exec: proc pemuldata not found.\n"));
180 p->p_emuldata = NULL;
190 linux_proc_exec(void *arg __unused, struct proc *p, struct image_params *imgp)
192 struct thread *td = curthread;
195 * In a case of execing to linux binary we create linux
196 * emuldata thread entry.
198 if (__predict_false((imgp->sysent->sv_flags & SV_ABI_MASK) ==
200 if (SV_PROC_ABI(p) == SV_ABI_LINUX)
201 linux_proc_init(td, NULL, 0);
203 linux_proc_init(td, td, 0);
208 linux_thread_dtor(void *arg __unused, struct thread *td)
210 struct linux_emuldata *em;
215 td->td_emuldata = NULL;
217 LINUX_CTR1(exit, "thread dtor(%d)", em->em_tid);
223 linux_schedtail(struct thread *td)
225 struct linux_emuldata *em;
233 KASSERT(em != NULL, ("linux_schedtail: thread emuldata not found.\n"));
234 child_set_tid = em->child_set_tid;
236 if (child_set_tid != NULL) {
237 error = copyout(&em->em_tid, (int *)child_set_tid,
239 LINUX_CTR4(clone, "schedtail(%d) %p stored %d error %d",
240 td->td_tid, child_set_tid, em->em_tid, error);
242 LINUX_CTR1(clone, "schedtail(%d)", em->em_tid);