linux_fork.c revision 218617
190075Sobrien/*- 290075Sobrien * Copyright (c) 2004 Tim J. Robbins 3117395Skan * Copyright (c) 2002 Doug Rabson 490075Sobrien * Copyright (c) 2000 Marcel Moolenaar 590075Sobrien * All rights reserved. 690075Sobrien * 790075Sobrien * Redistribution and use in source and binary forms, with or without 890075Sobrien * modification, are permitted provided that the following conditions 990075Sobrien * are met: 1090075Sobrien * 1. Redistributions of source code must retain the above copyright 1190075Sobrien * notice, this list of conditions and the following disclaimer 1290075Sobrien * in this position and unchanged. 1390075Sobrien * 2. Redistributions in binary form must reproduce the above copyright 1490075Sobrien * notice, this list of conditions and the following disclaimer in the 1590075Sobrien * documentation and/or other materials provided with the distribution. 1690075Sobrien * 1790075Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1890075Sobrien * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1990075Sobrien * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2090075Sobrien * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2190075Sobrien * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2290075Sobrien * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2390075Sobrien * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2490075Sobrien * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2590075Sobrien * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26117395Skan * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2790075Sobrien */ 2890075Sobrien 2990075Sobrien#include <sys/cdefs.h> 3090075Sobrien__FBSDID("$FreeBSD: head/sys/compat/linux/linux_fork.c 218617 2011-02-12 19:14:57Z dchagin $"); 3190075Sobrien 3290075Sobrien#include "opt_compat.h" 3390075Sobrien 3490075Sobrien#include <sys/param.h> 3590075Sobrien#include <sys/systm.h> 3690075Sobrien#include <sys/imgact.h> 3790075Sobrien#include <sys/lock.h> 3890075Sobrien#include <sys/mutex.h> 3990075Sobrien#include <sys/proc.h> 4090075Sobrien#include <sys/sched.h> 4190075Sobrien#include <sys/sx.h> 4290075Sobrien#include <sys/unistd.h> 4390075Sobrien 4490075Sobrien#ifdef COMPAT_LINUX32 4590075Sobrien#include <machine/../linux32/linux.h> 46117395Skan#include <machine/../linux32/linux32_proto.h> 4790075Sobrien#else 4890075Sobrien#include <machine/../linux/linux.h> 49117395Skan#include <machine/../linux/linux_proto.h> 5090075Sobrien#endif 51117395Skan#include <compat/linux/linux_signal.h> 5290075Sobrien#include <compat/linux/linux_emul.h> 5390075Sobrien 5490075Sobrien 5590075Sobrienint 5690075Sobrienlinux_fork(struct thread *td, struct linux_fork_args *args) 5790075Sobrien{ 5890075Sobrien int error; 5990075Sobrien struct proc *p2; 6090075Sobrien struct thread *td2; 6190075Sobrien 6290075Sobrien#ifdef DEBUG 6390075Sobrien if (ldebug(fork)) 6490075Sobrien printf(ARGS(fork, "")); 6590075Sobrien#endif 6690075Sobrien 6790075Sobrien if ((error = fork1(td, RFFDG | RFPROC | RFSTOPPED, 0, &p2)) != 0) 6890075Sobrien return (error); 6990075Sobrien 7090075Sobrien if (error == 0) { 7190075Sobrien td->td_retval[0] = p2->p_pid; 7290075Sobrien td->td_retval[1] = 0; 7390075Sobrien } 7490075Sobrien 7590075Sobrien if (td->td_retval[1] == 1) 7690075Sobrien td->td_retval[0] = 0; 7790075Sobrien error = linux_proc_init(td, td->td_retval[0], 0); 7890075Sobrien if (error) 7990075Sobrien return (error); 8090075Sobrien 8190075Sobrien td2 = FIRST_THREAD_IN_PROC(p2); 8290075Sobrien 8390075Sobrien /* 8490075Sobrien * Make this runnable after we are finished with it. 8590075Sobrien */ 8690075Sobrien thread_lock(td2); 8790075Sobrien TD_SET_CAN_RUN(td2); 8896263Sobrien sched_add(td2, SRQ_BORING); 8996263Sobrien thread_unlock(td2); 9090075Sobrien 9190075Sobrien return (0); 9290075Sobrien} 9390075Sobrien 9490075Sobrienint 9590075Sobrienlinux_vfork(struct thread *td, struct linux_vfork_args *args) 9690075Sobrien{ 9790075Sobrien int error; 98117395Skan struct proc *p2; 9990075Sobrien struct thread *td2; 10090075Sobrien 10190075Sobrien#ifdef DEBUG 102117395Skan if (ldebug(vfork)) 10390075Sobrien printf(ARGS(vfork, "")); 10490075Sobrien#endif 10590075Sobrien 10690075Sobrien /* Exclude RFPPWAIT */ 10790075Sobrien if ((error = fork1(td, RFFDG | RFPROC | RFMEM | RFSTOPPED, 0, &p2)) != 0) 10890075Sobrien return (error); 10990075Sobrien if (error == 0) { 11090075Sobrien td->td_retval[0] = p2->p_pid; 11190075Sobrien td->td_retval[1] = 0; 11290075Sobrien } 11390075Sobrien /* Are we the child? */ 11490075Sobrien if (td->td_retval[1] == 1) 11590075Sobrien td->td_retval[0] = 0; 11690075Sobrien error = linux_proc_init(td, td->td_retval[0], 0); 11790075Sobrien if (error) 11890075Sobrien return (error); 11990075Sobrien 12090075Sobrien PROC_LOCK(p2); 12190075Sobrien p2->p_flag |= P_PPWAIT; 12290075Sobrien PROC_UNLOCK(p2); 12396263Sobrien 12496263Sobrien td2 = FIRST_THREAD_IN_PROC(p2); 12590075Sobrien 12696263Sobrien /* 12790075Sobrien * Make this runnable after we are finished with it. 12896263Sobrien */ 12996263Sobrien thread_lock(td2); 13096263Sobrien TD_SET_CAN_RUN(td2); 13190075Sobrien sched_add(td2, SRQ_BORING); 13296263Sobrien thread_unlock(td2); 13396263Sobrien 13490075Sobrien /* wait for the children to exit, ie. emulate vfork */ 13590075Sobrien PROC_LOCK(p2); 13690075Sobrien while (p2->p_flag & P_PPWAIT) 13790075Sobrien cv_wait(&p2->p_pwait, &p2->p_mtx); 13890075Sobrien PROC_UNLOCK(p2); 13990075Sobrien 14090075Sobrien return (0); 14190075Sobrien} 14290075Sobrien 14390075Sobrienint 14490075Sobrienlinux_clone(struct thread *td, struct linux_clone_args *args) 14590075Sobrien{ 14690075Sobrien int error, ff = RFPROC | RFSTOPPED; 14790075Sobrien struct proc *p2; 14890075Sobrien struct thread *td2; 149117395Skan int exit_signal; 150117395Skan struct linux_emuldata *em; 151117395Skan 15290075Sobrien#ifdef DEBUG 15390075Sobrien if (ldebug(clone)) { 15490075Sobrien printf(ARGS(clone, "flags %x, stack %p, parent tid: %p, " 15590075Sobrien "child tid: %p"), (unsigned)args->flags, 15690075Sobrien args->stack, args->parent_tidptr, args->child_tidptr); 15790075Sobrien } 15890075Sobrien#endif 15990075Sobrien 16090075Sobrien exit_signal = args->flags & 0x000000ff; 16190075Sobrien if (LINUX_SIG_VALID(exit_signal)) { 16290075Sobrien if (exit_signal <= LINUX_SIGTBLSZ) 16390075Sobrien exit_signal = 16490075Sobrien linux_to_bsd_signal[_SIG_IDX(exit_signal)]; 16590075Sobrien } else if (exit_signal != 0) 16690075Sobrien return (EINVAL); 16790075Sobrien 16890075Sobrien if (args->flags & LINUX_CLONE_VM) 16990075Sobrien ff |= RFMEM; 17090075Sobrien if (args->flags & LINUX_CLONE_SIGHAND) 17190075Sobrien ff |= RFSIGSHARE; 17290075Sobrien /* 17390075Sobrien * XXX: In Linux, sharing of fs info (chroot/cwd/umask) 17490075Sobrien * and open files is independant. In FreeBSD, its in one 17590075Sobrien * structure but in reality it does not cause any problems 17690075Sobrien * because both of these flags are usually set together. 17790075Sobrien */ 17890075Sobrien if (!(args->flags & (LINUX_CLONE_FILES | LINUX_CLONE_FS))) 17990075Sobrien ff |= RFFDG; 18090075Sobrien 18190075Sobrien /* 18290075Sobrien * Attempt to detect when linux_clone(2) is used for creating 18390075Sobrien * kernel threads. Unfortunately despite the existence of the 18490075Sobrien * CLONE_THREAD flag, version of linuxthreads package used in 18590075Sobrien * most popular distros as of beginning of 2005 doesn't make 18690075Sobrien * any use of it. Therefore, this detection relies on 18790075Sobrien * empirical observation that linuxthreads sets certain 18890075Sobrien * combination of flags, so that we can make more or less 18990075Sobrien * precise detection and notify the FreeBSD kernel that several 19090075Sobrien * processes are in fact part of the same threading group, so 19190075Sobrien * that special treatment is necessary for signal delivery 19290075Sobrien * between those processes and fd locking. 19390075Sobrien */ 19490075Sobrien if ((args->flags & 0xffffff00) == LINUX_THREADING_FLAGS) 19590075Sobrien ff |= RFTHREAD; 19690075Sobrien 19790075Sobrien if (args->flags & LINUX_CLONE_PARENT_SETTID) 19890075Sobrien if (args->parent_tidptr == NULL) 19990075Sobrien return (EINVAL); 20090075Sobrien 20190075Sobrien error = fork1(td, ff, 0, &p2); 20290075Sobrien if (error) 20390075Sobrien return (error); 20490075Sobrien 20590075Sobrien if (args->flags & (LINUX_CLONE_PARENT | LINUX_CLONE_THREAD)) { 20690075Sobrien sx_xlock(&proctree_lock); 20790075Sobrien PROC_LOCK(p2); 20890075Sobrien proc_reparent(p2, td->td_proc->p_pptr); 20990075Sobrien PROC_UNLOCK(p2); 21090075Sobrien sx_xunlock(&proctree_lock); 21190075Sobrien } 21290075Sobrien 21390075Sobrien /* create the emuldata */ 21490075Sobrien error = linux_proc_init(td, p2->p_pid, args->flags); 21590075Sobrien /* reference it - no need to check this */ 21690075Sobrien em = em_find(p2, EMUL_DOLOCK); 21790075Sobrien KASSERT(em != NULL, ("clone: emuldata not found.")); 21890075Sobrien /* and adjust it */ 21990075Sobrien 22090075Sobrien if (args->flags & LINUX_CLONE_THREAD) { 22190075Sobrien#ifdef notyet 22290075Sobrien PROC_LOCK(p2); 22390075Sobrien p2->p_pgrp = td->td_proc->p_pgrp; 224117395Skan PROC_UNLOCK(p2); 225117395Skan#endif 226117395Skan exit_signal = 0; 227117395Skan } 228117395Skan 229117395Skan if (args->flags & LINUX_CLONE_CHILD_SETTID) 230117395Skan em->child_set_tid = args->child_tidptr; 231117395Skan else 232117395Skan em->child_set_tid = NULL; 233117395Skan 234117395Skan if (args->flags & LINUX_CLONE_CHILD_CLEARTID) 235117395Skan em->child_clear_tid = args->child_tidptr; 236117395Skan else 237117395Skan em->child_clear_tid = NULL; 238117395Skan 239117395Skan EMUL_UNLOCK(&emul_lock); 240117395Skan 241117395Skan if (args->flags & LINUX_CLONE_PARENT_SETTID) { 242117395Skan error = copyout(&p2->p_pid, args->parent_tidptr, 243117395Skan sizeof(p2->p_pid)); 244117395Skan if (error) 245117395Skan printf(LMSG("copyout failed!")); 246117395Skan } 247117395Skan 248117395Skan PROC_LOCK(p2); 24990075Sobrien p2->p_sigparent = exit_signal; 25090075Sobrien PROC_UNLOCK(p2); 25190075Sobrien td2 = FIRST_THREAD_IN_PROC(p2); 25290075Sobrien /* 25390075Sobrien * In a case of stack = NULL, we are supposed to COW calling process 25490075Sobrien * stack. This is what normal fork() does, so we just keep tf_rsp arg 25590075Sobrien * intact. 25690075Sobrien */ 25790075Sobrien if (args->stack) 25890075Sobrien linux_set_upcall_kse(td2, PTROUT(args->stack)); 25990075Sobrien 26090075Sobrien if (args->flags & LINUX_CLONE_SETTLS) 26190075Sobrien linux_set_cloned_tls(td2, args->tls); 26290075Sobrien 26390075Sobrien#ifdef DEBUG 26490075Sobrien if (ldebug(clone)) 26590075Sobrien printf(LMSG("clone: successful rfork to %d, " 26690075Sobrien "stack %p sig = %d"), (int)p2->p_pid, args->stack, 26790075Sobrien exit_signal); 26890075Sobrien#endif 26990075Sobrien if (args->flags & LINUX_CLONE_VFORK) { 27090075Sobrien PROC_LOCK(p2); 27190075Sobrien p2->p_flag |= P_PPWAIT; 27290075Sobrien PROC_UNLOCK(p2); 27390075Sobrien } 27490075Sobrien 27590075Sobrien /* 27690075Sobrien * Make this runnable after we are finished with it. 27790075Sobrien */ 27890075Sobrien thread_lock(td2); 27990075Sobrien TD_SET_CAN_RUN(td2); 28090075Sobrien sched_add(td2, SRQ_BORING); 28190075Sobrien thread_unlock(td2); 28290075Sobrien 28390075Sobrien td->td_retval[0] = p2->p_pid; 28490075Sobrien td->td_retval[1] = 0; 28590075Sobrien 28690075Sobrien if (args->flags & LINUX_CLONE_VFORK) { 28790075Sobrien /* wait for the children to exit, ie. emulate vfork */ 28890075Sobrien PROC_LOCK(p2); 28990075Sobrien while (p2->p_flag & P_PPWAIT) 29090075Sobrien cv_wait(&p2->p_pwait, &p2->p_mtx); 29190075Sobrien PROC_UNLOCK(p2); 29290075Sobrien } 29390075Sobrien 29490075Sobrien return (0); 29590075Sobrien} 29690075Sobrien