xref: /freebsd/sys/compat/linux/linux_emul.c (revision adfa0adec0b5d7c19c220a85ef6ca729235ed172)
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_UNLOCKED)
67    		EMUL_LOCK(&emul_lock);
68 
69 	em = p->p_emuldata;
70 
71 	if (em == NULL && locked == EMUL_UNLOCKED)
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    		MALLOC(em, struct linux_emuldata *, sizeof *em, M_LINUX, M_WAITOK | M_ZERO);
86 		em->pid = child;
87 		if (flags & CLONE_VM) {
88 		   	/* handled later in the code */
89 		} else {
90 		   	struct linux_emuldata_shared *s;
91 
92    			MALLOC(s, struct linux_emuldata_shared *, sizeof *s, M_LINUX, M_WAITOK | M_ZERO);
93 			em->shared = s;
94 			s->refs = 1;
95 			s->group_pid = child;
96 
97 			LIST_INIT(&s->threads);
98 		}
99 		p = pfind(child);
100 		if (p == NULL)
101 		   	panic("process not found in proc_init\n");
102 		p->p_emuldata = em;
103 		PROC_UNLOCK(p);
104 	} else {
105 		/* lookup the old one */
106 		em = em_find(td->td_proc, EMUL_UNLOCKED);
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 	/* allocate the shared struct only in clone()/fork cases
114 	 * in the case of clone() td = calling proc and child = pid of
115 	 * the newly created proc
116 	 */
117 	if (child != 0) {
118    	   	if (flags & CLONE_VM) {
119    		   	/* lookup the parent */
120 		   	p_em = em_find(td->td_proc, EMUL_LOCKED);
121 			KASSERT(p_em != NULL, ("proc_init: parent emuldata not found for CLONE_VM\n"));
122 			em->shared = p_em->shared;
123 			em->shared->refs++;
124 		} else {
125 		   	/* handled earlier to avoid malloc(M_WAITOK) with rwlock held */
126 		}
127 	}
128 
129 
130 	if (child != 0) {
131 	   	EMUL_SHARED_WLOCK(&emul_shared_lock);
132    	   	LIST_INSERT_HEAD(&em->shared->threads, em, threads);
133 	   	EMUL_SHARED_WUNLOCK(&emul_shared_lock);
134 
135 		p = pfind(child);
136 		PROC_UNLOCK(p);
137 		/* we might have a sleeping linux_schedtail */
138 		wakeup(&p->p_emuldata);
139 	} else
140 	   	EMUL_UNLOCK(&emul_lock);
141 
142    	return (0);
143 }
144 
145 void
146 linux_proc_exit(void *arg __unused, struct proc *p)
147 {
148    	struct linux_emuldata *em;
149 	int error;
150 	struct thread *td = FIRST_THREAD_IN_PROC(p);
151 	int *child_clear_tid;
152 
153 	if (__predict_true(p->p_sysent != &elf_linux_sysvec))
154 	   	return;
155 
156 	/* find the emuldata */
157 	em = em_find(p, EMUL_UNLOCKED);
158 
159 	KASSERT(em != NULL, ("proc_exit: emuldata not found.\n"));
160 
161 	child_clear_tid = em->child_clear_tid;
162 
163 	EMUL_UNLOCK(&emul_lock);
164 
165 	EMUL_SHARED_WLOCK(&emul_shared_lock);
166 	LIST_REMOVE(em, threads);
167 
168 	PROC_LOCK(p);
169 	p->p_emuldata = NULL;
170 	PROC_UNLOCK(p);
171 
172 	em->shared->refs--;
173 	if (em->shared->refs == 0)
174 		FREE(em->shared, M_LINUX);
175 	EMUL_SHARED_WUNLOCK(&emul_shared_lock);
176 
177 	if (child_clear_tid != NULL) {
178 	   	struct linux_sys_futex_args cup;
179 		int null = 0;
180 
181 		error = copyout(&null, child_clear_tid, sizeof(null));
182 		if (error)
183 		   	return;
184 
185 		/* futexes stuff */
186 		cup.uaddr = child_clear_tid;
187 		cup.op = LINUX_FUTEX_WAKE;
188 		cup.val = 0x7fffffff; /* Awake everyone */
189 		cup.timeout = NULL;
190 		cup.uaddr2 = NULL;
191 		cup.val3 = 0;
192 		error = linux_sys_futex(FIRST_THREAD_IN_PROC(p), &cup);
193 		/* this cannot happen at the moment and if this happens
194 		 * it probably mean there is a userspace bug
195 		*/
196 		if (error)
197 		   	printf(LMSG("futex stuff in proc_exit failed.\n"));
198 	}
199 
200 	/* clean the stuff up */
201 	FREE(em, M_LINUX);
202 }
203 
204 /* This is used in a case of transition from FreeBSD binary execing to linux binary
205  * in this case we create linux emuldata proc entry with the pid of the currently running
206  * process.
207  */
208 void linux_proc_exec(void *arg __unused, struct proc *p, struct image_params *imgp)
209 {
210    	if (__predict_false(imgp->sysent == &elf_linux_sysvec
211 		 && p->p_sysent != &elf_linux_sysvec))
212 	   	linux_proc_init(FIRST_THREAD_IN_PROC(p), p->p_pid, 0);
213 	if (__predict_false(imgp->sysent != &elf_linux_sysvec
214 		 && p->p_sysent == &elf_linux_sysvec)) {
215 	   	struct linux_emuldata *em;
216 
217 		em = em_find(p, EMUL_UNLOCKED);
218 
219 		KASSERT(em != NULL, ("proc_exec: emuldata not found.\n"));
220 
221 		EMUL_UNLOCK(&emul_lock);
222 
223 		EMUL_SHARED_WLOCK(&emul_shared_lock);
224 		LIST_REMOVE(em, threads);
225 
226 		PROC_LOCK(p);
227 		p->p_emuldata = NULL;
228 		PROC_UNLOCK(p);
229 
230 		em->shared->refs--;
231 		if (em->shared->refs == 0)
232 		   	FREE(em->shared, M_LINUX);
233 		EMUL_SHARED_WUNLOCK(&emul_shared_lock);
234 
235 		FREE(em, M_LINUX);
236 	}
237 }
238 
239 extern int hz;		/* in subr_param.c */
240 
241 void
242 linux_schedtail(void *arg __unused, struct proc *p)
243 {
244 	struct linux_emuldata *em;
245 	int error = 0;
246 #ifdef	DEBUG
247 	struct thread *td = FIRST_THREAD_IN_PROC(p);
248 #endif
249 	int *child_set_tid;
250 
251 	if (p->p_sysent != &elf_linux_sysvec)
252 	   	return;
253 
254 retry:
255 	/* find the emuldata */
256 	em = em_find(p, EMUL_UNLOCKED);
257 
258 	if (em == NULL) {
259 	   	/* We might have been called before proc_init for this process so
260 		 * tsleep and be woken up by it. We use p->p_emuldata for this
261 		 */
262 
263 	   	error = tsleep(&p->p_emuldata, PLOCK, "linux_schedtail", hz);
264 		if (error == 0)
265 		   	goto retry;
266 	   	panic("no emuldata found for userreting process.\n");
267 	}
268 	child_set_tid = em->child_set_tid;
269 	EMUL_UNLOCK(&emul_lock);
270 
271 	if (child_set_tid != NULL)
272 	   	error = copyout(&p->p_pid, (int *) child_set_tid, sizeof(p->p_pid));
273 
274 	return;
275 }
276 
277 int
278 linux_set_tid_address(struct thread *td, struct linux_set_tid_address_args *args)
279 {
280    	struct linux_emuldata *em;
281 
282 #ifdef DEBUG
283 	if (ldebug(set_tid_address))
284 		printf(ARGS(set_tid_address, "%p"), args->tidptr);
285 #endif
286 
287 	 /* find the emuldata */
288 	em = em_find(td->td_proc, EMUL_UNLOCKED);
289 
290 	KASSERT(em != NULL, ("set_tid_address: emuldata not found.\n"));
291 
292 	em->child_clear_tid = args->tidptr;
293 	td->td_retval[0] = td->td_proc->p_pid;
294 
295 	EMUL_UNLOCK(&emul_lock);
296 	return 0;
297 }
298