1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2004 Tim J. Robbins
5 * Copyright (c) 2002 Doug Rabson
6 * Copyright (c) 2000 Marcel Moolenaar
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer
14 * in this position and unchanged.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include "opt_compat.h"
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/imgact.h>
39 #include <sys/ktr.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/proc.h>
43 #include <sys/ptrace.h>
44 #include <sys/racct.h>
45 #include <sys/sched.h>
46 #include <sys/syscallsubr.h>
47 #include <sys/sx.h>
48 #include <sys/umtx.h>
49 #include <sys/unistd.h>
50 #include <sys/wait.h>
51
52 #include <vm/vm.h>
53 #include <vm/pmap.h>
54 #include <vm/vm_map.h>
55
56 #ifdef COMPAT_LINUX32
57 #include <machine/../linux32/linux.h>
58 #include <machine/../linux32/linux32_proto.h>
59 #else
60 #include <machine/../linux/linux.h>
61 #include <machine/../linux/linux_proto.h>
62 #endif
63 #include <compat/linux/linux_emul.h>
64 #include <compat/linux/linux_futex.h>
65 #include <compat/linux/linux_misc.h>
66 #include <compat/linux/linux_util.h>
67
68 #ifdef LINUX_LEGACY_SYSCALLS
69 int
70 linux_fork(struct thread *td, struct linux_fork_args *args)
71 {
72 struct fork_req fr;
73 int error;
74 struct proc *p2;
75 struct thread *td2;
76
77 bzero(&fr, sizeof(fr));
78 fr.fr_flags = RFFDG | RFPROC | RFSTOPPED;
79 fr.fr_procp = &p2;
80 if ((error = fork1(td, &fr)) != 0)
81 return (error);
82
83 td2 = FIRST_THREAD_IN_PROC(p2);
84
85 linux_proc_init(td, td2, 0);
86
87 td->td_retval[0] = p2->p_pid;
88
89 /*
90 * Make this runnable after we are finished with it.
91 */
92 thread_lock(td2);
93 TD_SET_CAN_RUN(td2);
94 sched_add(td2, SRQ_BORING);
95 thread_unlock(td2);
96
97 return (0);
98 }
99
100 int
101 linux_vfork(struct thread *td, struct linux_vfork_args *args)
102 {
103 struct fork_req fr;
104 int error;
105 struct proc *p2;
106 struct thread *td2;
107
108 bzero(&fr, sizeof(fr));
109 fr.fr_flags = RFFDG | RFPROC | RFMEM | RFPPWAIT | RFSTOPPED;
110 fr.fr_procp = &p2;
111 if ((error = fork1(td, &fr)) != 0)
112 return (error);
113
114 td2 = FIRST_THREAD_IN_PROC(p2);
115
116 linux_proc_init(td, td2, 0);
117
118 td->td_retval[0] = p2->p_pid;
119
120 /*
121 * Make this runnable after we are finished with it.
122 */
123 thread_lock(td2);
124 TD_SET_CAN_RUN(td2);
125 sched_add(td2, SRQ_BORING);
126 thread_unlock(td2);
127
128 return (0);
129 }
130 #endif
131
132 static int
133 linux_clone_proc(struct thread *td, struct linux_clone_args *args)
134 {
135 struct fork_req fr;
136 int error, ff = RFPROC | RFSTOPPED;
137 struct proc *p2;
138 struct thread *td2;
139 int exit_signal;
140 struct linux_emuldata *em;
141
142 exit_signal = args->flags & 0x000000ff;
143 if (LINUX_SIG_VALID(exit_signal)) {
144 exit_signal = linux_to_bsd_signal(exit_signal);
145 } else if (exit_signal != 0)
146 return (EINVAL);
147
148 if (args->flags & LINUX_CLONE_VM)
149 ff |= RFMEM;
150 if (args->flags & LINUX_CLONE_SIGHAND)
151 ff |= RFSIGSHARE;
152 /*
153 * XXX: In Linux, sharing of fs info (chroot/cwd/umask)
154 * and open files is independent. In FreeBSD, its in one
155 * structure but in reality it does not cause any problems
156 * because both of these flags are usually set together.
157 */
158 if (!(args->flags & (LINUX_CLONE_FILES | LINUX_CLONE_FS)))
159 ff |= RFFDG;
160
161 if (args->flags & LINUX_CLONE_PARENT_SETTID)
162 if (args->parent_tidptr == NULL)
163 return (EINVAL);
164
165 if (args->flags & LINUX_CLONE_VFORK)
166 ff |= RFPPWAIT;
167
168 bzero(&fr, sizeof(fr));
169 fr.fr_flags = ff;
170 fr.fr_procp = &p2;
171 error = fork1(td, &fr);
172 if (error)
173 return (error);
174
175 td2 = FIRST_THREAD_IN_PROC(p2);
176
177 /* create the emuldata */
178 linux_proc_init(td, td2, args->flags);
179
180 em = em_find(td2);
181 KASSERT(em != NULL, ("clone_proc: emuldata not found.\n"));
182
183 if (args->flags & LINUX_CLONE_CHILD_SETTID)
184 em->child_set_tid = args->child_tidptr;
185 else
186 em->child_set_tid = NULL;
187
188 if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
189 em->child_clear_tid = args->child_tidptr;
190 else
191 em->child_clear_tid = NULL;
192
193 if (args->flags & LINUX_CLONE_PARENT_SETTID) {
194 error = copyout(&p2->p_pid, args->parent_tidptr,
195 sizeof(p2->p_pid));
196 if (error)
197 linux_msg(td, "copyout p_pid failed!");
198 }
199
200 PROC_LOCK(p2);
201 p2->p_sigparent = exit_signal;
202 PROC_UNLOCK(p2);
203 /*
204 * In a case of stack = NULL, we are supposed to COW calling process
205 * stack. This is what normal fork() does, so we just keep tf_rsp arg
206 * intact.
207 */
208 linux_set_upcall_kse(td2, PTROUT(args->stack));
209
210 if (args->flags & LINUX_CLONE_SETTLS)
211 linux_set_cloned_tls(td2, args->tls);
212
213 /*
214 * If CLONE_PARENT is set, then the parent of the new process will be
215 * the same as that of the calling process.
216 */
217 if (args->flags & LINUX_CLONE_PARENT) {
218 sx_xlock(&proctree_lock);
219 PROC_LOCK(p2);
220 proc_reparent(p2, td->td_proc->p_pptr, true);
221 PROC_UNLOCK(p2);
222 sx_xunlock(&proctree_lock);
223 }
224
225 /*
226 * Make this runnable after we are finished with it.
227 */
228 thread_lock(td2);
229 TD_SET_CAN_RUN(td2);
230 sched_add(td2, SRQ_BORING);
231 thread_unlock(td2);
232
233 td->td_retval[0] = p2->p_pid;
234
235 return (0);
236 }
237
238 static int
239 linux_clone_thread(struct thread *td, struct linux_clone_args *args)
240 {
241 struct linux_emuldata *em;
242 struct thread *newtd;
243 struct proc *p;
244 int error;
245
246 LINUX_CTR4(clone_thread, "thread(%d) flags %x ptid %p ctid %p",
247 td->td_tid, (unsigned)args->flags,
248 args->parent_tidptr, args->child_tidptr);
249
250 if ((args->flags & LINUX_CLONE_PARENT) != 0)
251 return (EINVAL);
252 if (args->flags & LINUX_CLONE_PARENT_SETTID)
253 if (args->parent_tidptr == NULL)
254 return (EINVAL);
255
256 /* Threads should be created with own stack */
257 if (args->stack == NULL)
258 return (EINVAL);
259
260 p = td->td_proc;
261
262 #ifdef RACCT
263 if (racct_enable) {
264 PROC_LOCK(p);
265 error = racct_add(p, RACCT_NTHR, 1);
266 PROC_UNLOCK(p);
267 if (error != 0)
268 return (EPROCLIM);
269 }
270 #endif
271
272 /* Initialize our td */
273 error = kern_thr_alloc(p, 0, &newtd);
274 if (error)
275 goto fail;
276
277 cpu_copy_thread(newtd, td);
278
279 bzero(&newtd->td_startzero,
280 __rangeof(struct thread, td_startzero, td_endzero));
281 newtd->td_pflags2 = 0;
282 newtd->td_errno = 0;
283 bcopy(&td->td_startcopy, &newtd->td_startcopy,
284 __rangeof(struct thread, td_startcopy, td_endcopy));
285
286 newtd->td_proc = p;
287 thread_cow_get(newtd, td);
288
289 /* create the emuldata */
290 linux_proc_init(td, newtd, args->flags);
291
292 em = em_find(newtd);
293 KASSERT(em != NULL, ("clone_thread: emuldata not found.\n"));
294
295 if (args->flags & LINUX_CLONE_SETTLS)
296 linux_set_cloned_tls(newtd, args->tls);
297
298 if (args->flags & LINUX_CLONE_CHILD_SETTID)
299 em->child_set_tid = args->child_tidptr;
300 else
301 em->child_set_tid = NULL;
302
303 if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
304 em->child_clear_tid = args->child_tidptr;
305 else
306 em->child_clear_tid = NULL;
307
308 cpu_thread_clean(newtd);
309
310 linux_set_upcall_kse(newtd, PTROUT(args->stack));
311
312 PROC_LOCK(p);
313 p->p_flag |= P_HADTHREADS;
314 thread_link(newtd, p);
315 bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
316
317 thread_lock(td);
318 /* let the scheduler know about these things. */
319 sched_fork_thread(td, newtd);
320 thread_unlock(td);
321 if (P_SHOULDSTOP(p))
322 newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
323
324 if (p->p_ptevents & PTRACE_LWP)
325 newtd->td_dbgflags |= TDB_BORN;
326 PROC_UNLOCK(p);
327
328 tidhash_add(newtd);
329
330 LINUX_CTR2(clone_thread, "thread(%d) successful clone to %d",
331 td->td_tid, newtd->td_tid);
332
333 if (args->flags & LINUX_CLONE_PARENT_SETTID) {
334 error = copyout(&newtd->td_tid, args->parent_tidptr,
335 sizeof(newtd->td_tid));
336 if (error)
337 linux_msg(td, "clone_thread: copyout td_tid failed!");
338 }
339
340 /*
341 * Make this runnable after we are finished with it.
342 */
343 thread_lock(newtd);
344 TD_SET_CAN_RUN(newtd);
345 sched_add(newtd, SRQ_BORING);
346 thread_unlock(newtd);
347
348 td->td_retval[0] = newtd->td_tid;
349
350 return (0);
351
352 fail:
353 #ifdef RACCT
354 if (racct_enable) {
355 PROC_LOCK(p);
356 racct_sub(p, RACCT_NTHR, 1);
357 PROC_UNLOCK(p);
358 }
359 #endif
360 return (error);
361 }
362
363 int
364 linux_clone(struct thread *td, struct linux_clone_args *args)
365 {
366
367 if (args->flags & LINUX_CLONE_THREAD)
368 return (linux_clone_thread(td, args));
369 else
370 return (linux_clone_proc(td, args));
371 }
372
373 int
374 linux_exit(struct thread *td, struct linux_exit_args *args)
375 {
376 struct linux_emuldata *em;
377
378 em = em_find(td);
379 KASSERT(em != NULL, ("exit: emuldata not found.\n"));
380
381 LINUX_CTR2(exit, "thread(%d) (%d)", em->em_tid, args->rval);
382
383 umtx_thread_exit(td);
384
385 linux_thread_detach(td);
386
387 /*
388 * XXX. When the last two threads of a process
389 * exit via pthread_exit() try thr_exit() first.
390 */
391 kern_thr_exit(td);
392 exit1(td, args->rval, 0);
393 /* NOTREACHED */
394 }
395
396 int
397 linux_set_tid_address(struct thread *td, struct linux_set_tid_address_args *args)
398 {
399 struct linux_emuldata *em;
400
401 em = em_find(td);
402 KASSERT(em != NULL, ("set_tid_address: emuldata not found.\n"));
403
404 em->child_clear_tid = args->tidptr;
405
406 td->td_retval[0] = em->em_tid;
407
408 LINUX_CTR3(set_tid_address, "tidptr(%d) %p, returns %d",
409 em->em_tid, args->tidptr, td->td_retval[0]);
410
411 return (0);
412 }
413
414 void
415 linux_thread_detach(struct thread *td)
416 {
417 struct linux_sys_futex_args cup;
418 struct linux_emuldata *em;
419 int *child_clear_tid;
420 int error;
421
422 em = em_find(td);
423 KASSERT(em != NULL, ("thread_detach: emuldata not found.\n"));
424
425 LINUX_CTR1(thread_detach, "thread(%d)", em->em_tid);
426
427 release_futexes(td, em);
428
429 child_clear_tid = em->child_clear_tid;
430
431 if (child_clear_tid != NULL) {
432
433 LINUX_CTR2(thread_detach, "thread(%d) %p",
434 em->em_tid, child_clear_tid);
435
436 error = suword32(child_clear_tid, 0);
437 if (error != 0)
438 return;
439
440 cup.uaddr = child_clear_tid;
441 cup.op = LINUX_FUTEX_WAKE;
442 cup.val = 1; /* wake one */
443 cup.timeout = NULL;
444 cup.uaddr2 = NULL;
445 cup.val3 = 0;
446 error = linux_sys_futex(td, &cup);
447 /*
448 * this cannot happen at the moment and if this happens it
449 * probably means there is a user space bug
450 */
451 if (error != 0)
452 linux_msg(td, "futex stuff in thread_detach failed.");
453 }
454 }
Cache object: e3273b38351b3e4c7f072b08c51eb6ac
|