2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2006 Roman Divacky
5 * Copyright (c) 2013 Dmitry Chagin
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer
13 * in this position and unchanged.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/imgact.h>
38 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/mutex.h>
45 #include <sys/syscallsubr.h>
46 #include <sys/sysent.h>
48 #include <compat/linux/linux_emul.h>
49 #include <compat/linux/linux_misc.h>
50 #include <compat/linux/linux_persona.h>
51 #include <compat/linux/linux_util.h>
55 * This returns reference to the thread emuldata entry (if found)
57 * Hold PROC_LOCK when referencing emuldata from other threads.
59 struct linux_emuldata *
60 em_find(struct thread *td)
62 struct linux_emuldata *em;
70 * This returns reference to the proc pemuldata entry (if found)
72 * Hold PROC_LOCK when referencing proc pemuldata from other threads.
73 * Hold LINUX_PEM_LOCK wher referencing pemuldata members.
75 struct linux_pemuldata *
76 pem_find(struct proc *p)
78 struct linux_pemuldata *pem;
86 linux_proc_init(struct thread *td, struct thread *newtd, int flags)
88 struct linux_emuldata *em;
89 struct linux_pemuldata *pem;
90 struct epoll_emuldata *emd;
97 em = malloc(sizeof(*em), M_TEMP, M_WAITOK | M_ZERO);
98 if (flags & LINUX_CLONE_THREAD) {
99 LINUX_CTR1(proc_init, "thread newtd(%d)",
102 em->em_tid = newtd->td_tid;
104 LINUX_CTR1(proc_init, "fork newtd(%d)", p->p_pid);
106 em->em_tid = p->p_pid;
108 pem = malloc(sizeof(*pem), M_LINUX, M_WAITOK | M_ZERO);
109 sx_init(&pem->pem_sx, "lpemlk");
112 newtd->td_emuldata = em;
117 LINUX_CTR1(proc_init, "exec newtd(%d)", p->p_pid);
119 /* lookup the old one */
121 KASSERT(em != NULL, ("proc_init: emuldata not found in exec case.\n"));
123 em->em_tid = p->p_pid;
125 em->pdeath_signal = 0;
126 em->robust_futexes = NULL;
127 em->child_clear_tid = NULL;
128 em->child_set_tid = NULL;
130 /* epoll should be destroyed in a case of exec. */
132 KASSERT(pem != NULL, ("proc_exit: proc emuldata not found.\n"));
134 if (pem->epoll != NULL) {
144 linux_proc_exit(void *arg __unused, struct proc *p)
146 struct linux_pemuldata *pem;
147 struct epoll_emuldata *emd;
148 struct thread *td = curthread;
150 if (__predict_false(SV_CURPROC_ABI() != SV_ABI_LINUX))
153 LINUX_CTR3(proc_exit, "thread(%d) proc(%d) p %p",
154 td->td_tid, p->p_pid, p);
159 (p->p_sysent->sv_thread_detach)(td);
161 p->p_emuldata = NULL;
163 if (pem->epoll != NULL) {
169 sx_destroy(&pem->pem_sx);
174 linux_common_execve(struct thread *td, struct image_args *eargs)
176 struct linux_pemuldata *pem;
177 struct epoll_emuldata *emd;
178 struct vmspace *oldvmspace;
179 struct linux_emuldata *em;
185 error = pre_execve(td, &oldvmspace);
189 error = kern_execve(td, eargs, NULL);
190 post_execve(td, error, oldvmspace);
191 if (error != EJUSTRETURN)
195 * In a case of transition from Linux binary execing to
196 * FreeBSD binary we destroy linux emuldata thread & proc entries.
198 if (SV_CURPROC_ABI() != SV_ABI_LINUX) {
201 KASSERT(em != NULL, ("proc_exec: thread emuldata not found.\n"));
202 td->td_emuldata = NULL;
205 KASSERT(pem != NULL, ("proc_exec: proc pemuldata not found.\n"));
206 p->p_emuldata = NULL;
209 if (pem->epoll != NULL) {
218 return (EJUSTRETURN);
222 linux_proc_exec(void *arg __unused, struct proc *p, struct image_params *imgp)
224 struct thread *td = curthread;
225 struct thread *othertd;
226 #if defined(__amd64__)
227 struct linux_pemuldata *pem;
231 * In a case of execing from linux binary properly detach
232 * other threads from the user space.
234 if (__predict_false(SV_PROC_ABI(p) == SV_ABI_LINUX)) {
235 FOREACH_THREAD_IN_PROC(p, othertd) {
237 (p->p_sysent->sv_thread_detach)(othertd);
242 * In a case of execing to linux binary we create linux
243 * emuldata thread entry.
245 if (__predict_false((imgp->sysent->sv_flags & SV_ABI_MASK) ==
248 if (SV_PROC_ABI(p) == SV_ABI_LINUX)
249 linux_proc_init(td, NULL, 0);
251 linux_proc_init(td, td, 0);
252 #if defined(__amd64__)
254 * An IA32 executable which has executable stack will have the
255 * READ_IMPLIES_EXEC personality flag set automatically.
257 if (SV_PROC_FLAG(td->td_proc, SV_ILP32) &&
258 imgp->stack_prot & VM_PROT_EXECUTE) {
260 pem->persona |= LINUX_READ_IMPLIES_EXEC;
267 linux_thread_dtor(void *arg __unused, struct thread *td)
269 struct linux_emuldata *em;
274 td->td_emuldata = NULL;
276 LINUX_CTR1(thread_dtor, "thread(%d)", em->em_tid);
282 linux_schedtail(struct thread *td)
284 struct linux_emuldata *em;
292 KASSERT(em != NULL, ("linux_schedtail: thread emuldata not found.\n"));
293 child_set_tid = em->child_set_tid;
295 if (child_set_tid != NULL) {
296 error = copyout(&em->em_tid, child_set_tid,
298 LINUX_CTR4(schedtail, "thread(%d) %p stored %d error %d",
299 td->td_tid, child_set_tid, em->em_tid, error);
301 LINUX_CTR1(schedtail, "thread(%d)", em->em_tid);