]> CyberLeo.Net >> Repos - FreeBSD/releng/10.3.git/blob - sys/compat/linux/linux_fork.c
- Copy stable/10@296371 to releng/10.3 in preparation for 10.3-RC1
[FreeBSD/releng/10.3.git] / sys / compat / linux / linux_fork.c
1 /*-
2  * Copyright (c) 2004 Tim J. Robbins
3  * Copyright (c) 2002 Doug Rabson
4  * Copyright (c) 2000 Marcel Moolenaar
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer
12  *    in this position and unchanged.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
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 #include "opt_kdtrace.h"
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/imgact.h>
38 #include <sys/ktr.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 #include <sys/proc.h>
42 #include <sys/racct.h>
43 #include <sys/sched.h>
44 #include <sys/syscallsubr.h>
45 #include <sys/sx.h>
46 #include <sys/unistd.h>
47 #include <sys/wait.h>
48
49 #include <vm/vm.h>
50 #include <vm/pmap.h>
51 #include <vm/vm_map.h>
52
53 #ifdef COMPAT_LINUX32
54 #include <machine/../linux32/linux.h>
55 #include <machine/../linux32/linux32_proto.h>
56 #else
57 #include <machine/../linux/linux.h>
58 #include <machine/../linux/linux_proto.h>
59 #endif
60 #include <compat/linux/linux_emul.h>
61 #include <compat/linux/linux_futex.h>
62 #include <compat/linux/linux_misc.h>
63 #include <compat/linux/linux_util.h>
64
65 int
66 linux_fork(struct thread *td, struct linux_fork_args *args)
67 {
68         int error;
69         struct proc *p2;
70         struct thread *td2;
71
72 #ifdef DEBUG
73         if (ldebug(fork))
74                 printf(ARGS(fork, ""));
75 #endif
76
77         if ((error = fork1(td, RFFDG | RFPROC | RFSTOPPED, 0, &p2, NULL, 0))
78             != 0)
79                 return (error);
80
81         td2 = FIRST_THREAD_IN_PROC(p2);
82
83         linux_proc_init(td, td2, 0);
84
85         td->td_retval[0] = p2->p_pid;
86
87         /*
88          * Make this runnable after we are finished with it.
89          */
90         thread_lock(td2);
91         TD_SET_CAN_RUN(td2);
92         sched_add(td2, SRQ_BORING);
93         thread_unlock(td2);
94
95         return (0);
96 }
97
98 int
99 linux_vfork(struct thread *td, struct linux_vfork_args *args)
100 {
101         int error;
102         struct proc *p2;
103         struct thread *td2;
104
105 #ifdef DEBUG
106         if (ldebug(vfork))
107                 printf(ARGS(vfork, ""));
108 #endif
109
110         if ((error = fork1(td, RFFDG | RFPROC | RFMEM | RFPPWAIT | RFSTOPPED,
111             0, &p2, NULL, 0)) != 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
131 static int
132 linux_clone_proc(struct thread *td, struct linux_clone_args *args)
133 {
134         int error, ff = RFPROC | RFSTOPPED;
135         struct proc *p2;
136         struct thread *td2;
137         int exit_signal;
138         struct linux_emuldata *em;
139
140 #ifdef DEBUG
141         if (ldebug(clone)) {
142                 printf(ARGS(clone, "flags %x, stack %p, parent tid: %p, "
143                     "child tid: %p"), (unsigned)args->flags,
144                     args->stack, args->parent_tidptr, args->child_tidptr);
145         }
146 #endif
147
148         exit_signal = args->flags & 0x000000ff;
149         if (LINUX_SIG_VALID(exit_signal)) {
150                 exit_signal = linux_to_bsd_signal(exit_signal);
151         } else if (exit_signal != 0)
152                 return (EINVAL);
153
154         if (args->flags & LINUX_CLONE_VM)
155                 ff |= RFMEM;
156         if (args->flags & LINUX_CLONE_SIGHAND)
157                 ff |= RFSIGSHARE;
158         /*
159          * XXX: In Linux, sharing of fs info (chroot/cwd/umask)
160          * and open files is independant.  In FreeBSD, its in one
161          * structure but in reality it does not cause any problems
162          * because both of these flags are usually set together.
163          */
164         if (!(args->flags & (LINUX_CLONE_FILES | LINUX_CLONE_FS)))
165                 ff |= RFFDG;
166
167         if (args->flags & LINUX_CLONE_PARENT_SETTID)
168                 if (args->parent_tidptr == NULL)
169                         return (EINVAL);
170
171         if (args->flags & LINUX_CLONE_VFORK)
172                 ff |= RFPPWAIT;
173
174         error = fork1(td, ff, 0, &p2, NULL, 0);
175         if (error)
176                 return (error);
177
178         td2 = FIRST_THREAD_IN_PROC(p2);
179
180         /* create the emuldata */
181         linux_proc_init(td, td2, args->flags);
182
183         em = em_find(td2);
184         KASSERT(em != NULL, ("clone_proc: emuldata not found.\n"));
185
186         if (args->flags & LINUX_CLONE_CHILD_SETTID)
187                 em->child_set_tid = args->child_tidptr;
188         else
189                 em->child_set_tid = NULL;
190
191         if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
192                 em->child_clear_tid = args->child_tidptr;
193         else
194                 em->child_clear_tid = NULL;
195
196         if (args->flags & LINUX_CLONE_PARENT_SETTID) {
197                 error = copyout(&p2->p_pid, args->parent_tidptr,
198                     sizeof(p2->p_pid));
199                 if (error)
200                         printf(LMSG("copyout failed!"));
201         }
202
203         PROC_LOCK(p2);
204         p2->p_sigparent = exit_signal;
205         PROC_UNLOCK(p2);
206         /*
207          * In a case of stack = NULL, we are supposed to COW calling process
208          * stack. This is what normal fork() does, so we just keep tf_rsp arg
209          * intact.
210          */
211         linux_set_upcall_kse(td2, PTROUT(args->stack));
212
213         if (args->flags & LINUX_CLONE_SETTLS)
214                 linux_set_cloned_tls(td2, args->tls);
215
216 #ifdef DEBUG
217         if (ldebug(clone))
218                 printf(LMSG("clone: successful rfork to %d, "
219                     "stack %p sig = %d"), (int)p2->p_pid, args->stack,
220                     exit_signal);
221 #endif
222
223         /*
224          * Make this runnable after we are finished with it.
225          */
226         thread_lock(td2);
227         TD_SET_CAN_RUN(td2);
228         sched_add(td2, SRQ_BORING);
229         thread_unlock(td2);
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 linux_clone_args *args)
238 {
239         struct linux_emuldata *em;
240         struct thread *newtd;
241         struct proc *p;
242         int error;
243
244 #ifdef DEBUG
245         if (ldebug(clone)) {
246                 printf(ARGS(clone, "thread: flags %x, stack %p, parent tid: %p, "
247                     "child tid: %p"), (unsigned)args->flags,
248                     args->stack, args->parent_tidptr, args->child_tidptr);
249         }
250 #endif
251
252         LINUX_CTR4(clone_thread, "thread(%d) flags %x ptid %p ctid %p",
253             td->td_tid, (unsigned)args->flags,
254             args->parent_tidptr, args->child_tidptr);
255
256         if (args->flags & LINUX_CLONE_PARENT_SETTID)
257                 if (args->parent_tidptr == NULL)
258                         return (EINVAL);
259
260         /* Threads should be created with own stack */
261         if (args->stack == NULL)
262                 return (EINVAL);
263
264         p = td->td_proc;
265
266 #ifdef RACCT
267         if (racct_enable) {
268                 PROC_LOCK(p);
269                 error = racct_add(p, RACCT_NTHR, 1);
270                 PROC_UNLOCK(p);
271                 if (error != 0)
272                         return (EPROCLIM);
273         }
274 #endif
275
276         /* Initialize our td */
277         error = kern_thr_alloc(p, 0, &newtd);
278         if (error)
279                 goto fail;
280                                                                                                                 
281         cpu_set_upcall(newtd, td);
282
283         bzero(&newtd->td_startzero,
284             __rangeof(struct thread, td_startzero, td_endzero));
285         bcopy(&td->td_startcopy, &newtd->td_startcopy,
286             __rangeof(struct thread, td_startcopy, td_endcopy));
287
288         newtd->td_proc = p;
289         newtd->td_ucred = crhold(td->td_ucred);
290
291         /* create the emuldata */
292         linux_proc_init(td, newtd, args->flags);
293
294         em = em_find(newtd);
295         KASSERT(em != NULL, ("clone_thread: emuldata not found.\n"));
296
297         if (args->flags & LINUX_CLONE_SETTLS)
298                 linux_set_cloned_tls(newtd, args->tls);
299
300         if (args->flags & LINUX_CLONE_CHILD_SETTID)
301                 em->child_set_tid = args->child_tidptr;
302         else
303                 em->child_set_tid = NULL;
304
305         if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
306                 em->child_clear_tid = args->child_tidptr;
307         else
308                 em->child_clear_tid = NULL;
309
310         cpu_thread_clean(newtd);
311         
312         linux_set_upcall_kse(newtd, PTROUT(args->stack));
313
314         PROC_LOCK(p);
315         p->p_flag |= P_HADTHREADS;
316         bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
317
318         if (args->flags & LINUX_CLONE_PARENT)
319                 thread_link(newtd, p->p_pptr);
320         else
321                 thread_link(newtd, p);
322
323         thread_lock(td);
324         /* let the scheduler know about these things. */
325         sched_fork_thread(td, newtd);
326         thread_unlock(td);
327         if (P_SHOULDSTOP(p))
328                 newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
329         PROC_UNLOCK(p);
330
331         tidhash_add(newtd);
332
333 #ifdef DEBUG
334         if (ldebug(clone))
335                 printf(ARGS(clone, "successful clone to %d, stack %p"),
336                 (int)newtd->td_tid, args->stack);
337 #endif
338
339         LINUX_CTR2(clone_thread, "thread(%d) successful clone to %d",
340             td->td_tid, newtd->td_tid);
341
342         if (args->flags & LINUX_CLONE_PARENT_SETTID) {
343                 error = copyout(&newtd->td_tid, args->parent_tidptr,
344                     sizeof(newtd->td_tid));
345                 if (error)
346                         printf(LMSG("clone_thread: copyout failed!"));
347         }
348
349         /*
350          * Make this runnable after we are finished with it.
351          */
352         thread_lock(newtd);
353         TD_SET_CAN_RUN(newtd);
354         sched_add(newtd, SRQ_BORING);
355         thread_unlock(newtd);
356
357         td->td_retval[0] = newtd->td_tid;
358
359         return (0);
360
361 fail:
362 #ifdef RACCT
363         if (racct_enable) {
364                 PROC_LOCK(p);
365                 racct_sub(p, RACCT_NTHR, 1);
366                 PROC_UNLOCK(p);
367         }
368 #endif
369         return (error);
370 }
371
372 int
373 linux_clone(struct thread *td, struct linux_clone_args *args)
374 {
375
376         if (args->flags & LINUX_CLONE_THREAD)
377                 return (linux_clone_thread(td, args));
378         else
379                 return (linux_clone_proc(td, args));
380 }
381
382 int
383 linux_exit(struct thread *td, struct linux_exit_args *args)
384 {
385         struct linux_emuldata *em;
386
387         em = em_find(td);
388         KASSERT(em != NULL, ("exit: emuldata not found.\n"));
389
390         LINUX_CTR2(exit, "thread(%d) (%d)", em->em_tid, args->rval);
391
392         linux_thread_detach(td);
393
394         /*
395          * XXX. When the last two threads of a process
396          * exit via pthread_exit() try thr_exit() first.
397          */
398         kern_thr_exit(td);
399         exit1(td, W_EXITCODE(args->rval, 0));
400                 /* NOTREACHED */
401 }
402
403 int
404 linux_set_tid_address(struct thread *td, struct linux_set_tid_address_args *args)
405 {
406         struct linux_emuldata *em;
407
408         em = em_find(td);
409         KASSERT(em != NULL, ("set_tid_address: emuldata not found.\n"));
410
411         em->child_clear_tid = args->tidptr;
412
413         td->td_retval[0] = em->em_tid;
414
415         LINUX_CTR3(set_tid_address, "tidptr(%d) %p, returns %d",
416             em->em_tid, args->tidptr, td->td_retval[0]);
417
418         return (0);
419 }
420
421 void
422 linux_thread_detach(struct thread *td)
423 {
424         struct linux_sys_futex_args cup;
425         struct linux_emuldata *em;
426         int *child_clear_tid;
427         int error;
428
429         em = em_find(td);
430         KASSERT(em != NULL, ("thread_detach: emuldata not found.\n"));
431
432         LINUX_CTR1(thread_detach, "thread(%d)", em->em_tid);
433
434         release_futexes(td, em);
435
436         child_clear_tid = em->child_clear_tid;
437
438         if (child_clear_tid != NULL) {
439
440                 LINUX_CTR2(thread_detach, "thread(%d) %p",
441                     em->em_tid, child_clear_tid);
442         
443                 error = suword32(child_clear_tid, 0);
444                 if (error != 0)
445                         return;
446
447                 cup.uaddr = child_clear_tid;
448                 cup.op = LINUX_FUTEX_WAKE;
449                 cup.val = 1;            /* wake one */
450                 cup.timeout = NULL;
451                 cup.uaddr2 = NULL;
452                 cup.val3 = 0;
453                 error = linux_sys_futex(td, &cup);
454                 /*
455                  * this cannot happen at the moment and if this happens it
456                  * probably means there is a user space bug
457                  */
458                 if (error != 0)
459                         linux_msg(td, "futex stuff in thread_detach failed.");
460         }
461 }