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>
46 #include <compat/linux/linux_emul.h>
47 #include <compat/linux/linux_misc.h>
48 #include <compat/linux/linux_util.h>
52 * This returns reference to the thread emuldata entry (if found)
54 * Hold PROC_LOCK when referencing emuldata from other threads.
56 struct linux_emuldata *
57 em_find(struct thread *td)
59 struct linux_emuldata *em;
67 * This returns reference to the proc pemuldata entry (if found)
69 * Hold PROC_LOCK when referencing proc pemuldata from other threads.
70 * Hold LINUX_PEM_LOCK wher referencing pemuldata members.
72 struct linux_pemuldata *
73 pem_find(struct proc *p)
75 struct linux_pemuldata *pem;
83 linux_proc_init(struct thread *td, struct thread *newtd, int flags)
85 struct linux_emuldata *em;
86 struct linux_pemuldata *pem;
87 struct epoll_emuldata *emd;
94 em = malloc(sizeof(*em), M_TEMP, M_WAITOK | M_ZERO);
95 if (flags & LINUX_CLONE_THREAD) {
96 LINUX_CTR1(proc_init, "thread newtd(%d)",
99 em->em_tid = newtd->td_tid;
101 LINUX_CTR1(proc_init, "fork newtd(%d)", p->p_pid);
103 em->em_tid = p->p_pid;
105 pem = malloc(sizeof(*pem), M_LINUX, M_WAITOK | M_ZERO);
106 sx_init(&pem->pem_sx, "lpemlk");
109 newtd->td_emuldata = em;
114 LINUX_CTR1(proc_init, "exec newtd(%d)", p->p_pid);
116 /* lookup the old one */
118 KASSERT(em != NULL, ("proc_init: emuldata not found in exec case.\n"));
120 em->em_tid = p->p_pid;
122 em->pdeath_signal = 0;
123 em->robust_futexes = NULL;
124 em->child_clear_tid = NULL;
125 em->child_set_tid = NULL;
127 /* epoll should be destroyed in a case of exec. */
129 KASSERT(pem != NULL, ("proc_exit: proc emuldata not found.\n"));
131 if (pem->epoll != NULL) {
141 linux_proc_exit(void *arg __unused, struct proc *p)
143 struct linux_pemuldata *pem;
144 struct epoll_emuldata *emd;
145 struct thread *td = curthread;
147 if (__predict_false(SV_CURPROC_ABI() != SV_ABI_LINUX))
150 LINUX_CTR3(proc_exit, "thread(%d) proc(%d) p %p",
151 td->td_tid, p->p_pid, p);
156 (p->p_sysent->sv_thread_detach)(td);
158 p->p_emuldata = NULL;
160 if (pem->epoll != NULL) {
166 sx_destroy(&pem->pem_sx);
171 linux_common_execve(struct thread *td, struct image_args *eargs)
173 struct linux_pemuldata *pem;
174 struct epoll_emuldata *emd;
175 struct linux_emuldata *em;
182 * Unlike FreeBSD abort all other threads before
186 /* See exit1() comments. */
187 thread_suspend_check(0);
188 while (p->p_flag & P_HADTHREADS) {
189 if (!thread_single(p, SINGLE_EXIT))
191 thread_suspend_check(0);
195 error = kern_execve(td, eargs, NULL);
200 * In a case of transition from Linux binary execing to
201 * FreeBSD binary we destroy linux emuldata thread & proc entries.
203 if (SV_CURPROC_ABI() != SV_ABI_LINUX) {
206 KASSERT(em != NULL, ("proc_exec: thread emuldata not found.\n"));
207 td->td_emuldata = NULL;
210 KASSERT(pem != NULL, ("proc_exec: proc pemuldata not found.\n"));
211 p->p_emuldata = NULL;
214 if (pem->epoll != NULL) {
227 linux_proc_exec(void *arg __unused, struct proc *p, struct image_params *imgp)
229 struct thread *td = curthread;
232 * In a case of execing to linux binary we create linux
233 * emuldata thread entry.
235 if (__predict_false((imgp->sysent->sv_flags & SV_ABI_MASK) ==
238 if (SV_PROC_ABI(p) == SV_ABI_LINUX)
239 linux_proc_init(td, NULL, 0);
241 linux_proc_init(td, td, 0);
246 linux_thread_dtor(void *arg __unused, struct thread *td)
248 struct linux_emuldata *em;
253 td->td_emuldata = NULL;
255 LINUX_CTR1(thread_dtor, "thread(%d)", em->em_tid);
261 linux_schedtail(struct thread *td)
263 struct linux_emuldata *em;
271 KASSERT(em != NULL, ("linux_schedtail: thread emuldata not found.\n"));
272 child_set_tid = em->child_set_tid;
274 if (child_set_tid != NULL) {
275 error = copyout(&em->em_tid, child_set_tid,
277 LINUX_CTR4(schedtail, "thread(%d) %p stored %d error %d",
278 td->td_tid, child_set_tid, em->em_tid, error);
280 LINUX_CTR1(schedtail, "thread(%d)", em->em_tid);