Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/compat/linux/linux_fork.c
39507 views
1
/*-
2
* SPDX-License-Identifier: BSD-2-Clause
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/param.h>
32
#include <sys/ktr.h>
33
#include <sys/lock.h>
34
#include <sys/mutex.h>
35
#include <sys/proc.h>
36
#include <sys/ptrace.h>
37
#include <sys/racct.h>
38
#include <sys/sched.h>
39
#include <sys/syscallsubr.h>
40
#include <sys/sx.h>
41
#include <sys/umtxvar.h>
42
#include <sys/unistd.h>
43
44
#include <vm/vm.h>
45
#include <vm/pmap.h>
46
#include <vm/vm_map.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
#include <compat/linux/linux.h>
56
#include <compat/linux/linux_emul.h>
57
#include <compat/linux/linux_fork.h>
58
#include <compat/linux/linux_futex.h>
59
#include <compat/linux/linux_mib.h>
60
#include <compat/linux/linux_misc.h>
61
#include <compat/linux/linux_util.h>
62
63
#ifdef LINUX_LEGACY_SYSCALLS
64
int
65
linux_fork(struct thread *td, struct linux_fork_args *args)
66
{
67
struct fork_req fr;
68
int error;
69
struct proc *p2;
70
struct thread *td2;
71
72
bzero(&fr, sizeof(fr));
73
fr.fr_flags = RFFDG | RFPROC | RFSTOPPED;
74
fr.fr_procp = &p2;
75
if ((error = fork1(td, &fr)) != 0)
76
return (error);
77
78
td2 = FIRST_THREAD_IN_PROC(p2);
79
80
linux_proc_init(td, td2, false);
81
82
td->td_retval[0] = p2->p_pid;
83
84
/*
85
* Make this runnable after we are finished with it.
86
*/
87
thread_lock(td2);
88
TD_SET_CAN_RUN(td2);
89
sched_add(td2, SRQ_BORING);
90
91
return (0);
92
}
93
94
int
95
linux_vfork(struct thread *td, struct linux_vfork_args *args)
96
{
97
struct fork_req fr;
98
int error;
99
struct proc *p2;
100
struct thread *td2;
101
102
bzero(&fr, sizeof(fr));
103
fr.fr_flags = RFFDG | RFPROC | RFMEM | RFPPWAIT | RFSTOPPED;
104
fr.fr_procp = &p2;
105
if ((error = fork1(td, &fr)) != 0)
106
return (error);
107
108
td2 = FIRST_THREAD_IN_PROC(p2);
109
110
linux_proc_init(td, td2, false);
111
112
td->td_retval[0] = p2->p_pid;
113
114
/*
115
* Make this runnable after we are finished with it.
116
*/
117
thread_lock(td2);
118
TD_SET_CAN_RUN(td2);
119
sched_add(td2, SRQ_BORING);
120
121
return (0);
122
}
123
#endif
124
125
static int
126
linux_clone_proc(struct thread *td, struct l_clone_args *args)
127
{
128
struct fork_req fr;
129
int error, ff, f2;
130
struct proc *p2;
131
struct thread *td2;
132
int exit_signal;
133
struct linux_emuldata *em;
134
135
f2 = 0;
136
ff = RFPROC | RFSTOPPED;
137
if (LINUX_SIG_VALID(args->exit_signal)) {
138
exit_signal = linux_to_bsd_signal(args->exit_signal);
139
} else if (args->exit_signal != 0)
140
return (EINVAL);
141
else
142
exit_signal = 0;
143
144
if (args->flags & LINUX_CLONE_VM)
145
ff |= RFMEM;
146
if (args->flags & LINUX_CLONE_SIGHAND)
147
ff |= RFSIGSHARE;
148
if ((args->flags & LINUX_CLONE_CLEAR_SIGHAND) != 0)
149
f2 |= FR2_DROPSIG_CAUGHT;
150
if (args->flags & LINUX_CLONE_FILES) {
151
if (!(args->flags & LINUX_CLONE_FS))
152
f2 |= FR2_SHARE_PATHS;
153
} else {
154
ff |= RFFDG;
155
if (args->flags & LINUX_CLONE_FS)
156
f2 |= FR2_SHARE_PATHS;
157
}
158
159
if (args->flags & LINUX_CLONE_PARENT_SETTID)
160
if (args->parent_tid == NULL)
161
return (EINVAL);
162
163
if (args->flags & LINUX_CLONE_VFORK)
164
ff |= RFPPWAIT;
165
166
bzero(&fr, sizeof(fr));
167
fr.fr_flags = ff;
168
fr.fr_flags2 = f2;
169
fr.fr_procp = &p2;
170
error = fork1(td, &fr);
171
if (error)
172
return (error);
173
174
td2 = FIRST_THREAD_IN_PROC(p2);
175
176
/* create the emuldata */
177
linux_proc_init(td, td2, false);
178
179
em = em_find(td2);
180
KASSERT(em != NULL, ("clone_proc: emuldata not found.\n"));
181
182
if (args->flags & LINUX_CLONE_CHILD_SETTID)
183
em->child_set_tid = args->child_tid;
184
else
185
em->child_set_tid = NULL;
186
187
if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
188
em->child_clear_tid = args->child_tid;
189
else
190
em->child_clear_tid = NULL;
191
192
if (args->flags & LINUX_CLONE_PARENT_SETTID) {
193
error = copyout(&p2->p_pid, args->parent_tid,
194
sizeof(p2->p_pid));
195
if (error)
196
linux_msg(td, "copyout p_pid failed!");
197
}
198
199
PROC_LOCK(p2);
200
p2->p_sigparent = exit_signal;
201
PROC_UNLOCK(p2);
202
/*
203
* In a case of stack = NULL, we are supposed to COW calling process
204
* stack. This is what normal fork() does, so we just keep tf_rsp arg
205
* intact.
206
*/
207
linux_set_upcall(td2, args->stack);
208
209
if (args->flags & LINUX_CLONE_SETTLS)
210
linux_set_cloned_tls(td2, PTRIN(args->tls));
211
212
/*
213
* If CLONE_PARENT is set, then the parent of the new process will be
214
* the same as that of the calling process.
215
*/
216
if (args->flags & LINUX_CLONE_PARENT) {
217
sx_xlock(&proctree_lock);
218
PROC_LOCK(p2);
219
proc_reparent(p2, td->td_proc->p_pptr, true);
220
PROC_UNLOCK(p2);
221
sx_xunlock(&proctree_lock);
222
}
223
224
/*
225
* Make this runnable after we are finished with it.
226
*/
227
thread_lock(td2);
228
TD_SET_CAN_RUN(td2);
229
sched_add(td2, SRQ_BORING);
230
231
td->td_retval[0] = p2->p_pid;
232
233
return (0);
234
}
235
236
static int
237
linux_clone_thread(struct thread *td, struct l_clone_args *args)
238
{
239
struct linux_emuldata *em;
240
struct thread *newtd;
241
struct proc *p;
242
int error;
243
244
LINUX_CTR4(clone_thread, "thread(%d) flags %x ptid %p ctid %p",
245
td->td_tid, (unsigned)args->flags,
246
args->parent_tid, args->child_tid);
247
248
if ((args->flags & LINUX_CLONE_PARENT) != 0)
249
return (EINVAL);
250
if (args->flags & LINUX_CLONE_PARENT_SETTID)
251
if (args->parent_tid == NULL)
252
return (EINVAL);
253
254
/* Threads should be created with own stack */
255
if (PTRIN(args->stack) == NULL)
256
return (EINVAL);
257
258
p = td->td_proc;
259
260
#ifdef RACCT
261
if (racct_enable) {
262
PROC_LOCK(p);
263
error = racct_add(p, RACCT_NTHR, 1);
264
PROC_UNLOCK(p);
265
if (error != 0)
266
return (EPROCLIM);
267
}
268
#endif
269
270
/* Initialize our td */
271
error = kern_thr_alloc(p, 0, &newtd);
272
if (error)
273
goto fail;
274
275
bzero(&newtd->td_startzero,
276
__rangeof(struct thread, td_startzero, td_endzero));
277
bcopy(&td->td_startcopy, &newtd->td_startcopy,
278
__rangeof(struct thread, td_startcopy, td_endcopy));
279
280
newtd->td_proc = p;
281
thread_cow_get(newtd, td);
282
283
cpu_copy_thread(newtd, td);
284
285
/* create the emuldata */
286
linux_proc_init(td, newtd, true);
287
288
em = em_find(newtd);
289
KASSERT(em != NULL, ("clone_thread: emuldata not found.\n"));
290
291
if (args->flags & LINUX_CLONE_SETTLS)
292
linux_set_cloned_tls(newtd, PTRIN(args->tls));
293
294
if (args->flags & LINUX_CLONE_CHILD_SETTID)
295
em->child_set_tid = args->child_tid;
296
else
297
em->child_set_tid = NULL;
298
299
if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
300
em->child_clear_tid = args->child_tid;
301
else
302
em->child_clear_tid = NULL;
303
304
linux_set_upcall(newtd, args->stack);
305
306
PROC_LOCK(p);
307
p->p_flag |= P_HADTHREADS;
308
thread_link(newtd, p);
309
bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
310
311
thread_lock(td);
312
/* let the scheduler know about these things. */
313
sched_fork_thread(td, newtd);
314
thread_unlock(td);
315
if (P_SHOULDSTOP(p))
316
ast_sched(newtd, TDA_SUSPEND);
317
318
if (p->p_ptevents & PTRACE_LWP)
319
newtd->td_dbgflags |= TDB_BORN;
320
PROC_UNLOCK(p);
321
322
tidhash_add(newtd);
323
324
LINUX_CTR2(clone_thread, "thread(%d) successful clone to %d",
325
td->td_tid, newtd->td_tid);
326
327
if (args->flags & LINUX_CLONE_PARENT_SETTID) {
328
error = copyout(&newtd->td_tid, args->parent_tid,
329
sizeof(newtd->td_tid));
330
if (error)
331
linux_msg(td, "clone_thread: copyout td_tid failed!");
332
}
333
334
/*
335
* Make this runnable after we are finished with it.
336
*/
337
thread_lock(newtd);
338
TD_SET_CAN_RUN(newtd);
339
sched_add(newtd, SRQ_BORING);
340
341
td->td_retval[0] = newtd->td_tid;
342
343
return (0);
344
345
fail:
346
#ifdef RACCT
347
if (racct_enable) {
348
PROC_LOCK(p);
349
racct_sub(p, RACCT_NTHR, 1);
350
PROC_UNLOCK(p);
351
}
352
#endif
353
return (error);
354
}
355
356
int
357
linux_clone(struct thread *td, struct linux_clone_args *args)
358
{
359
struct l_clone_args ca = {
360
.flags = (lower_32_bits(args->flags) & ~LINUX_CSIGNAL),
361
.child_tid = args->child_tidptr,
362
.parent_tid = args->parent_tidptr,
363
.exit_signal = (lower_32_bits(args->flags) & LINUX_CSIGNAL),
364
.stack = args->stack,
365
.tls = args->tls,
366
};
367
368
if (args->flags & LINUX_CLONE_THREAD)
369
return (linux_clone_thread(td, &ca));
370
else
371
return (linux_clone_proc(td, &ca));
372
}
373
374
375
static int
376
linux_clone3_args_valid(struct l_user_clone_args *uca)
377
{
378
379
/* Verify that no unknown flags are passed along. */
380
if ((uca->flags & ~(LINUX_CLONE_LEGACY_FLAGS |
381
LINUX_CLONE_CLEAR_SIGHAND | LINUX_CLONE_INTO_CGROUP)) != 0)
382
return (EINVAL);
383
if ((uca->flags & (LINUX_CLONE_DETACHED | LINUX_CSIGNAL)) != 0)
384
return (EINVAL);
385
386
if ((uca->flags & (LINUX_CLONE_SIGHAND | LINUX_CLONE_CLEAR_SIGHAND)) ==
387
(LINUX_CLONE_SIGHAND | LINUX_CLONE_CLEAR_SIGHAND))
388
return (EINVAL);
389
if ((uca->flags & (LINUX_CLONE_THREAD | LINUX_CLONE_PARENT)) != 0 &&
390
uca->exit_signal != 0)
391
return (EINVAL);
392
393
/* We don't support set_tid, only validate input. */
394
if (uca->set_tid_size > LINUX_MAX_PID_NS_LEVEL)
395
return (EINVAL);
396
if (uca->set_tid == 0 && uca->set_tid_size > 0)
397
return (EINVAL);
398
if (uca->set_tid != 0 && uca->set_tid_size == 0)
399
return (EINVAL);
400
401
if (uca->stack == 0 && uca->stack_size > 0)
402
return (EINVAL);
403
if (uca->stack != 0 && uca->stack_size == 0)
404
return (EINVAL);
405
406
/* Verify that higher 32bits of exit_signal are unset. */
407
if ((uca->exit_signal & ~(uint64_t)LINUX_CSIGNAL) != 0)
408
return (EINVAL);
409
410
/* Verify that no unsupported flags are passed along. */
411
if ((uca->flags & LINUX_CLONE_NEWTIME) != 0) {
412
LINUX_RATELIMIT_MSG("unsupported clone3 option CLONE_NEWTIME");
413
return (ENOSYS);
414
}
415
if ((uca->flags & LINUX_CLONE_INTO_CGROUP) != 0) {
416
LINUX_RATELIMIT_MSG("unsupported clone3 option CLONE_INTO_CGROUP");
417
return (ENOSYS);
418
}
419
if (uca->set_tid != 0 || uca->set_tid_size != 0) {
420
LINUX_RATELIMIT_MSG("unsupported clone3 set_tid");
421
return (ENOSYS);
422
}
423
424
return (0);
425
}
426
427
int
428
linux_clone3(struct thread *td, struct linux_clone3_args *args)
429
{
430
struct l_user_clone_args *uca;
431
struct l_clone_args *ca;
432
size_t size;
433
int error;
434
435
if (args->usize > PAGE_SIZE)
436
return (E2BIG);
437
if (args->usize < LINUX_CLONE_ARGS_SIZE_VER0)
438
return (EINVAL);
439
440
/*
441
* usize can be less than size of struct clone_args, to avoid using
442
* of uninitialized data of struct clone_args, allocate at least
443
* sizeof(struct clone_args) storage and zero it.
444
*/
445
size = max(args->usize, sizeof(*uca));
446
uca = malloc(size, M_LINUX, M_WAITOK | M_ZERO);
447
error = copyin(args->uargs, uca, args->usize);
448
if (error != 0)
449
goto out;
450
error = linux_clone3_args_valid(uca);
451
if (error != 0)
452
goto out;
453
ca = malloc(sizeof(*ca), M_LINUX, M_WAITOK | M_ZERO);
454
ca->flags = uca->flags;
455
ca->child_tid = PTRIN(uca->child_tid);
456
ca->parent_tid = PTRIN(uca->parent_tid);
457
ca->exit_signal = uca->exit_signal;
458
ca->stack = uca->stack + uca->stack_size;
459
ca->stack_size = uca->stack_size;
460
ca->tls = uca->tls;
461
462
if ((ca->flags & LINUX_CLONE_THREAD) != 0)
463
error = linux_clone_thread(td, ca);
464
else
465
error = linux_clone_proc(td, ca);
466
free(ca, M_LINUX);
467
out:
468
free(uca, M_LINUX);
469
return (error);
470
}
471
472
int
473
linux_exit(struct thread *td, struct linux_exit_args *args)
474
{
475
struct linux_emuldata *em __diagused;
476
477
em = em_find(td);
478
KASSERT(em != NULL, ("exit: emuldata not found.\n"));
479
480
LINUX_CTR2(exit, "thread(%d) (%d)", em->em_tid, args->rval);
481
482
linux_thread_detach(td);
483
484
/*
485
* XXX. When the last two threads of a process
486
* exit via pthread_exit() try thr_exit() first.
487
*/
488
kern_thr_exit(td);
489
exit1(td, args->rval, 0);
490
/* NOTREACHED */
491
}
492
493
int
494
linux_set_tid_address(struct thread *td, struct linux_set_tid_address_args *args)
495
{
496
struct linux_emuldata *em;
497
498
em = em_find(td);
499
KASSERT(em != NULL, ("set_tid_address: emuldata not found.\n"));
500
501
em->child_clear_tid = args->tidptr;
502
503
td->td_retval[0] = em->em_tid;
504
505
LINUX_CTR3(set_tid_address, "tidptr(%d) %p, returns %d",
506
em->em_tid, args->tidptr, td->td_retval[0]);
507
508
return (0);
509
}
510
511
void
512
linux_thread_detach(struct thread *td)
513
{
514
struct linux_emuldata *em;
515
int *child_clear_tid;
516
int error;
517
518
em = em_find(td);
519
KASSERT(em != NULL, ("thread_detach: emuldata not found.\n"));
520
521
LINUX_CTR1(thread_detach, "thread(%d)", em->em_tid);
522
523
release_futexes(td, em);
524
525
child_clear_tid = em->child_clear_tid;
526
527
if (child_clear_tid != NULL) {
528
LINUX_CTR2(thread_detach, "thread(%d) %p",
529
em->em_tid, child_clear_tid);
530
531
error = suword32(child_clear_tid, 0);
532
if (error != 0)
533
return;
534
535
error = futex_wake(td, child_clear_tid, 1, false);
536
/*
537
* this cannot happen at the moment and if this happens it
538
* probably means there is a user space bug
539
*/
540
if (error != 0)
541
linux_msg(td, "futex stuff in thread_detach failed.");
542
}
543
544
/*
545
* Do not rely on the robust list which is maintained by userspace,
546
* cleanup remaining pi (if any) after release_futexes anyway.
547
*/
548
umtx_thread_exit(td);
549
}
550
551