2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1994-1996 Søren Schmidt
5 * Copyright (c) 2006 Roman Divacky
6 * Copyright (c) 2013 Dmitry Chagin
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/fcntl.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>
53 #if BYTE_ORDER == LITTLE_ENDIAN
54 #define SHELLMAGIC 0x2123 /* #! */
56 #define SHELLMAGIC 0x2321
60 * This returns reference to the thread emuldata entry (if found)
62 * Hold PROC_LOCK when referencing emuldata from other threads.
64 struct linux_emuldata *
65 em_find(struct thread *td)
67 struct linux_emuldata *em;
75 * This returns reference to the proc pemuldata entry (if found)
77 * Hold PROC_LOCK when referencing proc pemuldata from other threads.
78 * Hold LINUX_PEM_LOCK wher referencing pemuldata members.
80 struct linux_pemuldata *
81 pem_find(struct proc *p)
83 struct linux_pemuldata *pem;
91 linux_proc_init(struct thread *td, struct thread *newtd, int flags)
93 struct linux_emuldata *em;
94 struct linux_pemuldata *pem;
95 struct epoll_emuldata *emd;
102 em = malloc(sizeof(*em), M_TEMP, M_WAITOK | M_ZERO);
103 if (flags & LINUX_CLONE_THREAD) {
104 LINUX_CTR1(proc_init, "thread newtd(%d)",
107 em->em_tid = newtd->td_tid;
109 LINUX_CTR1(proc_init, "fork newtd(%d)", p->p_pid);
111 em->em_tid = p->p_pid;
113 pem = malloc(sizeof(*pem), M_LINUX, M_WAITOK | M_ZERO);
114 sx_init(&pem->pem_sx, "lpemlk");
117 newtd->td_emuldata = em;
122 LINUX_CTR1(proc_init, "exec newtd(%d)", p->p_pid);
124 /* lookup the old one */
126 KASSERT(em != NULL, ("proc_init: emuldata not found in exec case.\n"));
128 em->em_tid = p->p_pid;
130 em->robust_futexes = NULL;
131 em->child_clear_tid = NULL;
132 em->child_set_tid = NULL;
134 /* epoll should be destroyed in a case of exec. */
136 KASSERT(pem != NULL, ("proc_exit: proc emuldata not found.\n"));
138 if (pem->epoll != NULL) {
148 linux_proc_exit(void *arg __unused, struct proc *p)
150 struct linux_pemuldata *pem;
151 struct epoll_emuldata *emd;
152 struct thread *td = curthread;
154 if (__predict_false(SV_CURPROC_ABI() != SV_ABI_LINUX))
157 LINUX_CTR3(proc_exit, "thread(%d) proc(%d) p %p",
158 td->td_tid, p->p_pid, p);
163 (p->p_sysent->sv_thread_detach)(td);
165 p->p_emuldata = NULL;
167 if (pem->epoll != NULL) {
173 sx_destroy(&pem->pem_sx);
178 * If a Linux binary is exec'ing something, try this image activator
179 * first. We override standard shell script execution in order to
180 * be able to modify the interpreter path. We only do this if a Linux
181 * binary is doing the exec, so we do not create an EXEC module for it.
184 linux_exec_imgact_try(struct image_params *imgp)
186 const char *head = (const char *)imgp->image_header;
191 * The interpreter for shell scripts run from a Linux binary needs
192 * to be located in /compat/linux if possible in order to recursively
193 * maintain Linux path emulation.
195 if (((const short *)head)[0] == SHELLMAGIC) {
197 * Run our normal shell image activator. If it succeeds attempt
198 * to use the alternate path for the interpreter. If an
199 * alternate path is found, use our stringspace to store it.
201 if ((error = exec_shell_imgact(imgp)) == 0) {
202 linux_emul_convpath(FIRST_THREAD_IN_PROC(imgp->proc),
203 imgp->interpreter_name, UIO_SYSSPACE, &rpath, 0,
206 imgp->args->fname_buf =
207 imgp->interpreter_name = rpath;
214 linux_common_execve(struct thread *td, struct image_args *eargs)
216 struct linux_pemuldata *pem;
217 struct epoll_emuldata *emd;
218 struct vmspace *oldvmspace;
219 struct linux_emuldata *em;
225 error = pre_execve(td, &oldvmspace);
229 error = kern_execve(td, eargs, NULL);
230 post_execve(td, error, oldvmspace);
231 if (error != EJUSTRETURN)
235 * In a case of transition from Linux binary execing to
236 * FreeBSD binary we destroy Linux emuldata thread & proc entries.
238 if (SV_CURPROC_ABI() != SV_ABI_LINUX) {
241 KASSERT(em != NULL, ("proc_exec: thread emuldata not found.\n"));
242 td->td_emuldata = NULL;
245 KASSERT(pem != NULL, ("proc_exec: proc pemuldata not found.\n"));
246 p->p_emuldata = NULL;
249 if (pem->epoll != NULL) {
258 return (EJUSTRETURN);
262 linux_proc_exec(void *arg __unused, struct proc *p, struct image_params *imgp)
264 struct thread *td = curthread;
265 struct thread *othertd;
266 #if defined(__amd64__)
267 struct linux_pemuldata *pem;
271 * In a case of execing from Linux binary properly detach
272 * other threads from the user space.
274 if (__predict_false(SV_PROC_ABI(p) == SV_ABI_LINUX)) {
275 FOREACH_THREAD_IN_PROC(p, othertd) {
277 (p->p_sysent->sv_thread_detach)(othertd);
282 * In a case of execing to Linux binary we create Linux
283 * emuldata thread entry.
285 if (__predict_false((imgp->sysent->sv_flags & SV_ABI_MASK) ==
288 if (SV_PROC_ABI(p) == SV_ABI_LINUX)
289 linux_proc_init(td, NULL, 0);
291 linux_proc_init(td, td, 0);
292 #if defined(__amd64__)
294 * An IA32 executable which has executable stack will have the
295 * READ_IMPLIES_EXEC personality flag set automatically.
297 if (SV_PROC_FLAG(td->td_proc, SV_ILP32) &&
298 imgp->stack_prot & VM_PROT_EXECUTE) {
300 pem->persona |= LINUX_READ_IMPLIES_EXEC;
307 linux_thread_dtor(void *arg __unused, struct thread *td)
309 struct linux_emuldata *em;
314 td->td_emuldata = NULL;
316 LINUX_CTR1(thread_dtor, "thread(%d)", em->em_tid);
322 linux_schedtail(struct thread *td)
324 struct linux_emuldata *em;
332 KASSERT(em != NULL, ("linux_schedtail: thread emuldata not found.\n"));
333 child_set_tid = em->child_set_tid;
335 if (child_set_tid != NULL) {
336 error = copyout(&em->em_tid, child_set_tid,
338 LINUX_CTR4(schedtail, "thread(%d) %p stored %d error %d",
339 td->td_tid, child_set_tid, em->em_tid, error);
341 LINUX_CTR1(schedtail, "thread(%d)", em->em_tid);