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