xref: /freebsd/sys/compat/linux/linux_emul.c (revision bb15ca603fa442c72dde3f3cb8b46db6970e3950)
1 /*-
2  * Copyright (c) 2006 Roman Divacky
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_compat.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/imgact.h>
37 #include <sys/kernel.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 #include <sys/mutex.h>
41 #include <sys/sx.h>
42 #include <sys/proc.h>
43 #include <sys/syscallsubr.h>
44 #include <sys/sysent.h>
45 #include <sys/sysproto.h>
46 #include <sys/unistd.h>
47 
48 #ifdef COMPAT_LINUX32
49 #include <machine/../linux32/linux.h>
50 #include <machine/../linux32/linux32_proto.h>
51 #else
52 #include <machine/../linux/linux.h>
53 #include <machine/../linux/linux_proto.h>
54 #endif
55 
56 #include <compat/linux/linux_emul.h>
57 #include <compat/linux/linux_futex.h>
58 
59 struct sx	emul_shared_lock;
60 struct mtx	emul_lock;
61 
62 /* this returns locked reference to the emuldata entry (if found) */
63 struct linux_emuldata *
64 em_find(struct proc *p, int locked)
65 {
66 	struct linux_emuldata *em;
67 
68 	if (locked == EMUL_DOLOCK)
69 		EMUL_LOCK(&emul_lock);
70 
71 	em = p->p_emuldata;
72 
73 	if (em == NULL && locked == EMUL_DOLOCK)
74 		EMUL_UNLOCK(&emul_lock);
75 
76 	return (em);
77 }
78 
79 int
80 linux_proc_init(struct thread *td, pid_t child, int flags)
81 {
82 	struct linux_emuldata *em, *p_em;
83 	struct proc *p;
84 
85 	if (child != 0) {
86 		/* non-exec call */
87 		em = malloc(sizeof *em, M_LINUX, M_WAITOK | M_ZERO);
88 		em->pid = child;
89 		em->pdeath_signal = 0;
90 		em->flags = 0;
91 		em->robust_futexes = NULL;
92 		if (flags & LINUX_CLONE_THREAD) {
93 			/* handled later in the code */
94 		} else {
95 			struct linux_emuldata_shared *s;
96 
97 			s = malloc(sizeof *s, M_LINUX, M_WAITOK | M_ZERO);
98 			s->refs = 1;
99 			s->group_pid = child;
100 
101 			LIST_INIT(&s->threads);
102 			em->shared = s;
103 		}
104 	} else {
105 		/* lookup the old one */
106 		em = em_find(td->td_proc, EMUL_DOLOCK);
107 		KASSERT(em != NULL, ("proc_init: emuldata not found in exec case.\n"));
108 	}
109 
110 	em->child_clear_tid = NULL;
111 	em->child_set_tid = NULL;
112 
113 	/*
114 	 * allocate the shared struct only in clone()/fork cases in the case
115 	 * of clone() td = calling proc and child = pid of the newly created
116 	 * proc
117 	 */
118 	if (child != 0) {
119 		if (flags & LINUX_CLONE_THREAD) {
120 			/* lookup the parent */
121 			/*
122 			 * we dont have to lock the p_em because
123 			 * its waiting for us in linux_clone so
124 			 * there is no chance of it changing the
125 			 * p_em->shared address
126 			 */
127 			p_em = em_find(td->td_proc, EMUL_DONTLOCK);
128 			KASSERT(p_em != NULL, ("proc_init: parent emuldata not found for CLONE_THREAD\n"));
129 			em->shared = p_em->shared;
130 			EMUL_SHARED_WLOCK(&emul_shared_lock);
131 			em->shared->refs++;
132 			EMUL_SHARED_WUNLOCK(&emul_shared_lock);
133 		} else {
134 			/*
135 			 * handled earlier to avoid malloc(M_WAITOK) with
136 			 * rwlock held
137 			 */
138 		}
139 	}
140 	if (child != 0) {
141 		EMUL_SHARED_WLOCK(&emul_shared_lock);
142 		LIST_INSERT_HEAD(&em->shared->threads, em, threads);
143 		EMUL_SHARED_WUNLOCK(&emul_shared_lock);
144 
145 		p = pfind(child);
146 		KASSERT(p != NULL, ("process not found in proc_init\n"));
147 		p->p_emuldata = em;
148 		PROC_UNLOCK(p);
149 	} else
150 		EMUL_UNLOCK(&emul_lock);
151 
152 	return (0);
153 }
154 
155 void
156 linux_proc_exit(void *arg __unused, struct proc *p)
157 {
158 	struct linux_emuldata *em;
159 	int error, shared_flags, shared_xstat;
160 	struct thread *td = FIRST_THREAD_IN_PROC(p);
161 	int *child_clear_tid;
162 	struct proc *q, *nq;
163 
164 	if (__predict_true(p->p_sysent != &elf_linux_sysvec))
165 		return;
166 
167 	release_futexes(p);
168 
169 	/* find the emuldata */
170 	em = em_find(p, EMUL_DOLOCK);
171 
172 	KASSERT(em != NULL, ("proc_exit: emuldata not found.\n"));
173 
174 	/* reparent all procs that are not a thread leader to initproc */
175 	if (em->shared->group_pid != p->p_pid) {
176 		child_clear_tid = em->child_clear_tid;
177 		EMUL_UNLOCK(&emul_lock);
178 		sx_xlock(&proctree_lock);
179 		wakeup(initproc);
180 		PROC_LOCK(p);
181 		proc_reparent(p, initproc);
182 		p->p_sigparent = SIGCHLD;
183 		PROC_UNLOCK(p);
184 		sx_xunlock(&proctree_lock);
185 	} else {
186 		child_clear_tid = em->child_clear_tid;
187 		EMUL_UNLOCK(&emul_lock);
188 	}
189 
190 	EMUL_SHARED_WLOCK(&emul_shared_lock);
191 	shared_flags = em->shared->flags;
192 	shared_xstat = em->shared->xstat;
193 	LIST_REMOVE(em, threads);
194 
195 	em->shared->refs--;
196 	if (em->shared->refs == 0) {
197 		EMUL_SHARED_WUNLOCK(&emul_shared_lock);
198 		free(em->shared, M_LINUX);
199 	} else
200 		EMUL_SHARED_WUNLOCK(&emul_shared_lock);
201 
202 	if ((shared_flags & EMUL_SHARED_HASXSTAT) != 0)
203 		p->p_xstat = shared_xstat;
204 
205 	if (child_clear_tid != NULL) {
206 		struct linux_sys_futex_args cup;
207 		int null = 0;
208 
209 		error = copyout(&null, child_clear_tid, sizeof(null));
210 		if (error) {
211 			free(em, M_LINUX);
212 			return;
213 		}
214 
215 		/* futexes stuff */
216 		cup.uaddr = child_clear_tid;
217 		cup.op = LINUX_FUTEX_WAKE;
218 		cup.val = 0x7fffffff;	/* Awake everyone */
219 		cup.timeout = NULL;
220 		cup.uaddr2 = NULL;
221 		cup.val3 = 0;
222 		error = linux_sys_futex(FIRST_THREAD_IN_PROC(p), &cup);
223 		/*
224 		 * this cannot happen at the moment and if this happens it
225 		 * probably means there is a user space bug
226 		 */
227 		if (error)
228 			printf(LMSG("futex stuff in proc_exit failed.\n"));
229 	}
230 
231 	/* clean the stuff up */
232 	free(em, M_LINUX);
233 
234 	/* this is a little weird but rewritten from exit1() */
235 	sx_xlock(&proctree_lock);
236 	q = LIST_FIRST(&p->p_children);
237 	for (; q != NULL; q = nq) {
238 		nq = LIST_NEXT(q, p_sibling);
239 		if (q->p_flag & P_WEXIT)
240 			continue;
241 		if (__predict_false(q->p_sysent != &elf_linux_sysvec))
242 			continue;
243 		em = em_find(q, EMUL_DOLOCK);
244 		KASSERT(em != NULL, ("linux_reparent: emuldata not found: %i\n", q->p_pid));
245 		PROC_LOCK(q);
246 		if ((q->p_flag & P_WEXIT) == 0 && em->pdeath_signal != 0) {
247 			kern_psignal(q, em->pdeath_signal);
248 		}
249 		PROC_UNLOCK(q);
250 		EMUL_UNLOCK(&emul_lock);
251 	}
252 	sx_xunlock(&proctree_lock);
253 }
254 
255 /*
256  * This is used in a case of transition from FreeBSD binary execing to linux binary
257  * in this case we create linux emuldata proc entry with the pid of the currently running
258  * process.
259  */
260 void
261 linux_proc_exec(void *arg __unused, struct proc *p, struct image_params *imgp)
262 {
263 	if (__predict_false(imgp->sysent == &elf_linux_sysvec
264 	    && p->p_sysent != &elf_linux_sysvec))
265 		linux_proc_init(FIRST_THREAD_IN_PROC(p), p->p_pid, 0);
266 	if (__predict_false((p->p_sysent->sv_flags & SV_ABI_MASK) ==
267 	    SV_ABI_LINUX))
268 		/* Kill threads regardless of imgp->sysent value */
269 		linux_kill_threads(FIRST_THREAD_IN_PROC(p), SIGKILL);
270 	if (__predict_false(imgp->sysent != &elf_linux_sysvec
271 	    && p->p_sysent == &elf_linux_sysvec)) {
272 		struct linux_emuldata *em;
273 
274 		/*
275 		 * XXX:There's a race because here we assign p->p_emuldata NULL
276 		 * but the process is still counted as linux one for a short
277  		 * time so some other process might reference it and try to
278  		 * access its p->p_emuldata and panicing on a NULL reference.
279 		 */
280 		em = em_find(p, EMUL_DONTLOCK);
281 
282 		KASSERT(em != NULL, ("proc_exec: emuldata not found.\n"));
283 
284 		EMUL_SHARED_WLOCK(&emul_shared_lock);
285 		LIST_REMOVE(em, threads);
286 
287 		PROC_LOCK(p);
288 		p->p_emuldata = NULL;
289 		PROC_UNLOCK(p);
290 
291 		em->shared->refs--;
292 		if (em->shared->refs == 0) {
293 			EMUL_SHARED_WUNLOCK(&emul_shared_lock);
294 			free(em->shared, M_LINUX);
295 		} else
296 			EMUL_SHARED_WUNLOCK(&emul_shared_lock);
297 
298 		free(em, M_LINUX);
299 	}
300 }
301 
302 void
303 linux_schedtail(struct thread *td)
304 {
305 	struct linux_emuldata *em;
306 	struct proc *p;
307 	int error = 0;
308 	int *child_set_tid;
309 
310 	p = td->td_proc;
311 
312 	/* find the emuldata */
313 	em = em_find(p, EMUL_DOLOCK);
314 
315 	KASSERT(em != NULL, ("linux_schedtail: emuldata not found.\n"));
316 	child_set_tid = em->child_set_tid;
317 	EMUL_UNLOCK(&emul_lock);
318 
319 	if (child_set_tid != NULL)
320 		error = copyout(&p->p_pid, (int *)child_set_tid,
321 		    sizeof(p->p_pid));
322 
323 	return;
324 }
325 
326 int
327 linux_set_tid_address(struct thread *td, struct linux_set_tid_address_args *args)
328 {
329 	struct linux_emuldata *em;
330 
331 #ifdef DEBUG
332 	if (ldebug(set_tid_address))
333 		printf(ARGS(set_tid_address, "%p"), args->tidptr);
334 #endif
335 
336 	/* find the emuldata */
337 	em = em_find(td->td_proc, EMUL_DOLOCK);
338 
339 	KASSERT(em != NULL, ("set_tid_address: emuldata not found.\n"));
340 
341 	em->child_clear_tid = args->tidptr;
342 	td->td_retval[0] = td->td_proc->p_pid;
343 
344 	EMUL_UNLOCK(&emul_lock);
345 	return 0;
346 }
347 
348 void
349 linux_kill_threads(struct thread *td, int sig)
350 {
351 	struct linux_emuldata *em, *td_em, *tmp_em;
352 	struct proc *sp;
353 
354 	td_em = em_find(td->td_proc, EMUL_DONTLOCK);
355 
356 	KASSERT(td_em != NULL, ("linux_kill_threads: emuldata not found.\n"));
357 
358 	EMUL_SHARED_RLOCK(&emul_shared_lock);
359 	LIST_FOREACH_SAFE(em, &td_em->shared->threads, threads, tmp_em) {
360 		if (em->pid == td_em->pid)
361 			continue;
362 
363 		sp = pfind(em->pid);
364 		if ((sp->p_flag & P_WEXIT) == 0)
365 			kern_psignal(sp, sig);
366 		PROC_UNLOCK(sp);
367 #ifdef DEBUG
368 		printf(LMSG("linux_kill_threads: kill PID %d\n"), em->pid);
369 #endif
370 	}
371 	EMUL_SHARED_RUNLOCK(&emul_shared_lock);
372 }
373