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_persona.h>
49 #include <compat/linux/linux_util.h>
53 * This returns reference to the thread emuldata entry (if found)
55 * Hold PROC_LOCK when referencing emuldata from other threads.
57 struct linux_emuldata *
58 em_find(struct thread *td)
60 struct linux_emuldata *em;
68 * This returns reference to the proc pemuldata entry (if found)
70 * Hold PROC_LOCK when referencing proc pemuldata from other threads.
71 * Hold LINUX_PEM_LOCK wher referencing pemuldata members.
73 struct linux_pemuldata *
74 pem_find(struct proc *p)
76 struct linux_pemuldata *pem;
84 linux_proc_init(struct thread *td, struct thread *newtd, int flags)
86 struct linux_emuldata *em;
87 struct linux_pemuldata *pem;
88 struct epoll_emuldata *emd;
95 em = malloc(sizeof(*em), M_TEMP, M_WAITOK | M_ZERO);
96 if (flags & LINUX_CLONE_THREAD) {
97 LINUX_CTR1(proc_init, "thread newtd(%d)",
100 em->em_tid = newtd->td_tid;
102 LINUX_CTR1(proc_init, "fork newtd(%d)", p->p_pid);
104 em->em_tid = p->p_pid;
106 pem = malloc(sizeof(*pem), M_LINUX, M_WAITOK | M_ZERO);
107 sx_init(&pem->pem_sx, "lpemlk");
110 newtd->td_emuldata = em;
115 LINUX_CTR1(proc_init, "exec newtd(%d)", p->p_pid);
117 /* lookup the old one */
119 KASSERT(em != NULL, ("proc_init: emuldata not found in exec case.\n"));
121 em->em_tid = p->p_pid;
123 em->pdeath_signal = 0;
124 em->robust_futexes = NULL;
125 em->child_clear_tid = NULL;
126 em->child_set_tid = NULL;
128 /* epoll should be destroyed in a case of exec. */
130 KASSERT(pem != NULL, ("proc_exit: proc emuldata not found.\n"));
132 if (pem->epoll != NULL) {
142 linux_proc_exit(void *arg __unused, struct proc *p)
144 struct linux_pemuldata *pem;
145 struct epoll_emuldata *emd;
146 struct thread *td = curthread;
148 if (__predict_false(SV_CURPROC_ABI() != SV_ABI_LINUX))
151 LINUX_CTR3(proc_exit, "thread(%d) proc(%d) p %p",
152 td->td_tid, p->p_pid, p);
157 (p->p_sysent->sv_thread_detach)(td);
159 p->p_emuldata = NULL;
161 if (pem->epoll != NULL) {
167 sx_destroy(&pem->pem_sx);
172 linux_common_execve(struct thread *td, struct image_args *eargs)
174 struct linux_pemuldata *pem;
175 struct epoll_emuldata *emd;
176 struct vmspace *oldvmspace;
177 struct linux_emuldata *em;
183 error = pre_execve(td, &oldvmspace);
187 error = kern_execve(td, eargs, NULL);
188 post_execve(td, error, oldvmspace);
193 * In a case of transition from Linux binary execing to
194 * FreeBSD binary we destroy linux emuldata thread & proc entries.
196 if (SV_CURPROC_ABI() != SV_ABI_LINUX) {
199 KASSERT(em != NULL, ("proc_exec: thread emuldata not found.\n"));
200 td->td_emuldata = NULL;
203 KASSERT(pem != NULL, ("proc_exec: proc pemuldata not found.\n"));
204 p->p_emuldata = NULL;
207 if (pem->epoll != NULL) {
220 linux_proc_exec(void *arg __unused, struct proc *p, struct image_params *imgp)
222 struct thread *td = curthread;
223 struct thread *othertd;
224 #if defined(__amd64__)
225 struct linux_pemuldata *pem;
229 * In a case of execing from linux binary properly detach
230 * other threads from the user space.
232 if (__predict_false(SV_PROC_ABI(p) == SV_ABI_LINUX)) {
233 FOREACH_THREAD_IN_PROC(p, othertd) {
235 (p->p_sysent->sv_thread_detach)(othertd);
240 * In a case of execing to linux binary we create linux
241 * emuldata thread entry.
243 if (__predict_false((imgp->sysent->sv_flags & SV_ABI_MASK) ==
246 if (SV_PROC_ABI(p) == SV_ABI_LINUX)
247 linux_proc_init(td, NULL, 0);
249 linux_proc_init(td, td, 0);
250 #if defined(__amd64__)
252 * An IA32 executable which has executable stack will have the
253 * READ_IMPLIES_EXEC personality flag set automatically.
255 if (SV_PROC_FLAG(td->td_proc, SV_ILP32) &&
256 imgp->stack_prot & VM_PROT_EXECUTE) {
258 pem->persona |= LINUX_READ_IMPLIES_EXEC;
265 linux_thread_dtor(void *arg __unused, struct thread *td)
267 struct linux_emuldata *em;
272 td->td_emuldata = NULL;
274 LINUX_CTR1(thread_dtor, "thread(%d)", em->em_tid);
280 linux_schedtail(struct thread *td)
282 struct linux_emuldata *em;
290 KASSERT(em != NULL, ("linux_schedtail: thread emuldata not found.\n"));
291 child_set_tid = em->child_set_tid;
293 if (child_set_tid != NULL) {
294 error = copyout(&em->em_tid, child_set_tid,
296 LINUX_CTR4(schedtail, "thread(%d) %p stored %d error %d",
297 td->td_tid, child_set_tid, em->em_tid, error);
299 LINUX_CTR1(schedtail, "thread(%d)", em->em_tid);