xref: /freebsd/sys/compat/linux/linux_emul.c (revision ee7093a6400feef3d8a99579f28577817d70f6ad)
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/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
40 #include <sys/sx.h>
41 #include <sys/proc.h>
42 #include <sys/syscallsubr.h>
43 #include <sys/sysproto.h>
44 #include <sys/unistd.h>
45 
46 #include <compat/linux/linux_emul.h>
47 #include <compat/linux/linux_futex.h>
48 
49 #ifdef COMPAT_LINUX32
50 #include <machine/../linux32/linux.h>
51 #include <machine/../linux32/linux32_proto.h>
52 #else
53 #include <machine/../linux/linux.h>
54 #include <machine/../linux/linux_proto.h>
55 #endif
56 
57 struct sx emul_shared_lock;
58 struct sx emul_lock;
59 
60 /* this returns locked reference to the emuldata entry (if found) */
61 struct linux_emuldata *
62 em_find(struct proc *p, int locked)
63 {
64 	struct linux_emuldata *em;
65 
66 	if (locked == EMUL_DOLOCK)
67 		EMUL_LOCK(&emul_lock);
68 
69 	em = p->p_emuldata;
70 
71 	if (em == NULL && locked == EMUL_DOLOCK)
72 		EMUL_UNLOCK(&emul_lock);
73 
74 	return (em);
75 }
76 
77 int
78 linux_proc_init(struct thread *td, pid_t child, int flags)
79 {
80 	struct linux_emuldata *em, *p_em;
81 	struct proc *p;
82 
83 	if (child != 0) {
84 		/* non-exec call */
85 		em = malloc(sizeof *em, M_LINUX, M_WAITOK | M_ZERO);
86 		em->pid = child;
87 		em->pdeath_signal = 0;
88 		if (flags & CLONE_THREAD) {
89 			/* handled later in the code */
90 		} else {
91 			struct linux_emuldata_shared *s;
92 
93 			s = malloc(sizeof *s, M_LINUX, M_WAITOK | M_ZERO);
94 			em->shared = s;
95 			s->refs = 1;
96 			s->group_pid = child;
97 
98 			LIST_INIT(&s->threads);
99 		}
100 		p = pfind(child);
101 		KASSERT(p != NULL, ("process not found in proc_init\n"));
102 		p->p_emuldata = em;
103 		PROC_UNLOCK(p);
104 		EMUL_LOCK(&emul_lock);
105 	} else {
106 		/* lookup the old one */
107 		em = em_find(td->td_proc, EMUL_DOLOCK);
108 		KASSERT(em != NULL, ("proc_init: emuldata not found in exec case.\n"));
109 	}
110 
111 	em->child_clear_tid = NULL;
112 	em->child_set_tid = NULL;
113 
114 	/*
115 	 * allocate the shared struct only in clone()/fork cases in the case
116 	 * of clone() td = calling proc and child = pid of the newly created
117 	 * proc
118 	 */
119 	if (child != 0) {
120 		if (flags & CLONE_THREAD) {
121 			/* lookup the parent */
122 		   	EMUL_SHARED_WLOCK(&emul_shared_lock);
123 			p_em = em_find(td->td_proc, EMUL_DONTLOCK);
124 			KASSERT(p_em != NULL, ("proc_init: parent emuldata not found for CLONE_THREAD\n"));
125 			em->shared = p_em->shared;
126 			em->shared->refs++;
127 		   	EMUL_SHARED_WUNLOCK(&emul_shared_lock);
128 		} else {
129 			/*
130 			 * handled earlier to avoid malloc(M_WAITOK) with
131 			 * rwlock held
132 			 */
133 		}
134 	}
135 	if (child != 0) {
136 		EMUL_UNLOCK(&emul_lock);
137 		EMUL_SHARED_WLOCK(&emul_shared_lock);
138 		LIST_INSERT_HEAD(&em->shared->threads, em, threads);
139 		EMUL_SHARED_WUNLOCK(&emul_shared_lock);
140 
141 		p = pfind(child);
142 		/* we might have a sleeping linux_schedtail */
143 		wakeup(&p->p_emuldata);
144 		PROC_UNLOCK(p);
145 	} else
146 		EMUL_UNLOCK(&emul_lock);
147 
148 	return (0);
149 }
150 
151 void
152 linux_proc_exit(void *arg __unused, struct proc *p)
153 {
154 	struct linux_emuldata *em;
155 	int error;
156 	struct thread *td = FIRST_THREAD_IN_PROC(p);
157 	int *child_clear_tid;
158 	struct proc *q, *nq;
159 
160 	if (__predict_true(p->p_sysent != &elf_linux_sysvec))
161 		return;
162 
163 	/* find the emuldata */
164 	em = em_find(p, EMUL_DOLOCK);
165 
166 	KASSERT(em != NULL, ("proc_exit: emuldata not found.\n"));
167 
168 	child_clear_tid = em->child_clear_tid;
169 
170 	EMUL_UNLOCK(&emul_lock);
171 
172 	EMUL_SHARED_WLOCK(&emul_shared_lock);
173 	LIST_REMOVE(em, threads);
174 
175 	PROC_LOCK(p);
176 	p->p_emuldata = NULL;
177 	PROC_UNLOCK(p);
178 
179 	em->shared->refs--;
180 	if (em->shared->refs == 0)
181 		free(em->shared, M_LINUX);
182 	EMUL_SHARED_WUNLOCK(&emul_shared_lock);
183 
184 	if (child_clear_tid != NULL) {
185 		struct linux_sys_futex_args cup;
186 		int null = 0;
187 
188 		error = copyout(&null, child_clear_tid, sizeof(null));
189 		if (error) {
190 			free(em, M_LINUX);
191 			return;
192 		}
193 
194 		/* futexes stuff */
195 		cup.uaddr = child_clear_tid;
196 		cup.op = LINUX_FUTEX_WAKE;
197 		cup.val = 0x7fffffff;	/* Awake everyone */
198 		cup.timeout = NULL;
199 		cup.uaddr2 = NULL;
200 		cup.val3 = 0;
201 		error = linux_sys_futex(FIRST_THREAD_IN_PROC(p), &cup);
202 		/*
203 		 * this cannot happen at the moment and if this happens it
204 		 * probably mean there is a userspace bug
205 		 */
206 		if (error)
207 			printf(LMSG("futex stuff in proc_exit failed.\n"));
208 	}
209 
210 	/* clean the stuff up */
211 	free(em, M_LINUX);
212 
213 	/* this is a little weird but rewritten from exit1() */
214 	sx_xlock(&proctree_lock);
215 	q = LIST_FIRST(&p->p_children);
216 	for (; q != NULL; q = nq) {
217 		nq = LIST_NEXT(q, p_sibling);
218 		if (q->p_flag & P_WEXIT)
219 			continue;
220 		if (__predict_false(q->p_sysent != &elf_linux_sysvec))
221 			continue;
222 		em = em_find(q, EMUL_DOLOCK);
223 		KASSERT(em != NULL, ("linux_reparent: emuldata not found: %i\n", q->p_pid));
224 		if (em->pdeath_signal != 0) {
225 			PROC_LOCK(q);
226 			psignal(q, em->pdeath_signal);
227 			PROC_UNLOCK(q);
228 		}
229 		EMUL_UNLOCK(&emul_lock);
230 	}
231 	sx_xunlock(&proctree_lock);
232 }
233 
234 /*
235  * This is used in a case of transition from FreeBSD binary execing to linux binary
236  * in this case we create linux emuldata proc entry with the pid of the currently running
237  * process.
238  */
239 void
240 linux_proc_exec(void *arg __unused, struct proc *p, struct image_params *imgp)
241 {
242 	if (__predict_false(imgp->sysent == &elf_linux_sysvec
243 	    && p->p_sysent != &elf_linux_sysvec))
244 		linux_proc_init(FIRST_THREAD_IN_PROC(p), p->p_pid, 0);
245 	if (__predict_false(imgp->sysent != &elf_linux_sysvec
246 	    && p->p_sysent == &elf_linux_sysvec)) {
247 		struct linux_emuldata *em;
248 
249 		em = em_find(p, EMUL_DOLOCK);
250 
251 		KASSERT(em != NULL, ("proc_exec: emuldata not found.\n"));
252 
253 		EMUL_UNLOCK(&emul_lock);
254 
255 		EMUL_SHARED_WLOCK(&emul_shared_lock);
256 		LIST_REMOVE(em, threads);
257 
258 		PROC_LOCK(p);
259 		p->p_emuldata = NULL;
260 		PROC_UNLOCK(p);
261 
262 		em->shared->refs--;
263 		if (em->shared->refs == 0)
264 			free(em->shared, M_LINUX);
265 		EMUL_SHARED_WUNLOCK(&emul_shared_lock);
266 
267 		free(em, M_LINUX);
268 	}
269 }
270 
271 extern int hz;				/* in subr_param.c */
272 
273 void
274 linux_schedtail(void *arg __unused, struct proc *p)
275 {
276 	struct linux_emuldata *em;
277 	int error = 0;
278 	int *child_set_tid;
279 
280 	if (__predict_true(p->p_sysent != &elf_linux_sysvec))
281 		return;
282 
283 retry:
284 	/* find the emuldata */
285 	em = em_find(p, EMUL_DOLOCK);
286 
287 	if (em == NULL) {
288 		/*
289 		 * We might have been called before proc_init for this
290 		 * process so tsleep and be woken up by it. We use
291 		 * p->p_emuldata for this
292 		 */
293 
294 		error = tsleep(&p->p_emuldata, PLOCK, "linux_schedtail", hz);
295 		if (error == 0)
296 			goto retry;
297 		panic("no emuldata found for userreting process.\n");
298 	}
299 	child_set_tid = em->child_set_tid;
300 	EMUL_UNLOCK(&emul_lock);
301 
302 	if (child_set_tid != NULL)
303 		error = copyout(&p->p_pid, (int *)child_set_tid,
304 		    sizeof(p->p_pid));
305 
306 	return;
307 }
308 
309 int
310 linux_set_tid_address(struct thread *td, struct linux_set_tid_address_args *args)
311 {
312 	struct linux_emuldata *em;
313 
314 #ifdef DEBUG
315 	if (ldebug(set_tid_address))
316 		printf(ARGS(set_tid_address, "%p"), args->tidptr);
317 #endif
318 
319 	/* find the emuldata */
320 	em = em_find(td->td_proc, EMUL_DOLOCK);
321 
322 	KASSERT(em != NULL, ("set_tid_address: emuldata not found.\n"));
323 
324 	em->child_clear_tid = args->tidptr;
325 	td->td_retval[0] = td->td_proc->p_pid;
326 
327 	EMUL_UNLOCK(&emul_lock);
328 	return 0;
329 }
330