linux_fork.c revision 293492
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: stable/10/sys/compat/linux/linux_fork.c 293492 2016-01-09 15:12:31Z dchagin $"); 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/lock.h> 39#include <sys/mutex.h> 40#include <sys/proc.h> 41#include <sys/sched.h> 42#include <sys/sdt.h> 43#include <sys/sx.h> 44#include <sys/unistd.h> 45#include <sys/wait.h> 46 47#ifdef COMPAT_LINUX32 48#include <machine/../linux32/linux.h> 49#include <machine/../linux32/linux32_proto.h> 50#else 51#include <machine/../linux/linux.h> 52#include <machine/../linux/linux_proto.h> 53#endif 54#include <compat/linux/linux_dtrace.h> 55#include <compat/linux/linux_signal.h> 56#include <compat/linux/linux_emul.h> 57#include <compat/linux/linux_misc.h> 58 59/* DTrace init */ 60LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE); 61 62/* Linuxulator-global DTrace probes */ 63LIN_SDT_PROBE_DECLARE(locks, emul_lock, locked); 64LIN_SDT_PROBE_DECLARE(locks, emul_lock, unlock); 65 66 67int 68linux_fork(struct thread *td, struct linux_fork_args *args) 69{ 70 int error; 71 struct proc *p2; 72 struct thread *td2; 73 74#ifdef DEBUG 75 if (ldebug(fork)) 76 printf(ARGS(fork, "")); 77#endif 78 79 if ((error = fork1(td, RFFDG | RFPROC | RFSTOPPED, 0, &p2, NULL, 0)) 80 != 0) 81 return (error); 82 83 td->td_retval[0] = p2->p_pid; 84 td->td_retval[1] = 0; 85 86 error = linux_proc_init(td, td->td_retval[0], 0); 87 if (error) 88 return (error); 89 90 td2 = FIRST_THREAD_IN_PROC(p2); 91 92 /* 93 * Make this runnable after we are finished with it. 94 */ 95 thread_lock(td2); 96 TD_SET_CAN_RUN(td2); 97 sched_add(td2, SRQ_BORING); 98 thread_unlock(td2); 99 100 return (0); 101} 102 103int 104linux_vfork(struct thread *td, struct linux_vfork_args *args) 105{ 106 int error; 107 struct proc *p2; 108 struct thread *td2; 109 110#ifdef DEBUG 111 if (ldebug(vfork)) 112 printf(ARGS(vfork, "")); 113#endif 114 115 /* Exclude RFPPWAIT */ 116 if ((error = fork1(td, RFFDG | RFPROC | RFMEM | RFSTOPPED, 0, &p2, 117 NULL, 0)) != 0) 118 return (error); 119 120 td->td_retval[0] = p2->p_pid; 121 122 error = linux_proc_init(td, td->td_retval[0], 0); 123 if (error) 124 return (error); 125 126 PROC_LOCK(p2); 127 p2->p_flag |= P_PPWAIT; 128 PROC_UNLOCK(p2); 129 130 td2 = FIRST_THREAD_IN_PROC(p2); 131 132 /* 133 * Make this runnable after we are finished with it. 134 */ 135 thread_lock(td2); 136 TD_SET_CAN_RUN(td2); 137 sched_add(td2, SRQ_BORING); 138 thread_unlock(td2); 139 140 /* wait for the children to exit, ie. emulate vfork */ 141 PROC_LOCK(p2); 142 while (p2->p_flag & P_PPWAIT) 143 cv_wait(&p2->p_pwait, &p2->p_mtx); 144 PROC_UNLOCK(p2); 145 146 return (0); 147} 148 149int 150linux_clone(struct thread *td, struct linux_clone_args *args) 151{ 152 int error, ff = RFPROC | RFSTOPPED; 153 struct proc *p2; 154 struct thread *td2; 155 int exit_signal; 156 struct linux_emuldata *em; 157 158#ifdef DEBUG 159 if (ldebug(clone)) { 160 printf(ARGS(clone, "flags %x, stack %p, parent tid: %p, " 161 "child tid: %p"), (unsigned)args->flags, 162 args->stack, args->parent_tidptr, args->child_tidptr); 163 } 164#endif 165 166 exit_signal = args->flags & 0x000000ff; 167 if (LINUX_SIG_VALID(exit_signal)) { 168 if (exit_signal <= LINUX_SIGTBLSZ) 169 exit_signal = 170 linux_to_bsd_signal[_SIG_IDX(exit_signal)]; 171 } else if (exit_signal != 0) 172 return (EINVAL); 173 174 if (args->flags & LINUX_CLONE_VM) 175 ff |= RFMEM; 176 if (args->flags & LINUX_CLONE_SIGHAND) 177 ff |= RFSIGSHARE; 178 /* 179 * XXX: In Linux, sharing of fs info (chroot/cwd/umask) 180 * and open files is independant. In FreeBSD, its in one 181 * structure but in reality it does not cause any problems 182 * because both of these flags are usually set together. 183 */ 184 if (!(args->flags & (LINUX_CLONE_FILES | LINUX_CLONE_FS))) 185 ff |= RFFDG; 186 187 /* 188 * Attempt to detect when linux_clone(2) is used for creating 189 * kernel threads. Unfortunately despite the existence of the 190 * CLONE_THREAD flag, version of linuxthreads package used in 191 * most popular distros as of beginning of 2005 doesn't make 192 * any use of it. Therefore, this detection relies on 193 * empirical observation that linuxthreads sets certain 194 * combination of flags, so that we can make more or less 195 * precise detection and notify the FreeBSD kernel that several 196 * processes are in fact part of the same threading group, so 197 * that special treatment is necessary for signal delivery 198 * between those processes and fd locking. 199 */ 200 if ((args->flags & 0xffffff00) == LINUX_THREADING_FLAGS) 201 ff |= RFTHREAD; 202 203 if (args->flags & LINUX_CLONE_PARENT_SETTID) 204 if (args->parent_tidptr == NULL) 205 return (EINVAL); 206 207 error = fork1(td, ff, 0, &p2, NULL, 0); 208 if (error) 209 return (error); 210 211 if (args->flags & (LINUX_CLONE_PARENT | LINUX_CLONE_THREAD)) { 212 sx_xlock(&proctree_lock); 213 PROC_LOCK(p2); 214 proc_reparent(p2, td->td_proc->p_pptr); 215 PROC_UNLOCK(p2); 216 sx_xunlock(&proctree_lock); 217 } 218 219 /* create the emuldata */ 220 error = linux_proc_init(td, p2->p_pid, args->flags); 221 /* reference it - no need to check this */ 222 em = em_find(p2, EMUL_DOLOCK); 223 KASSERT(em != NULL, ("clone: emuldata not found.")); 224 /* and adjust it */ 225 226 if (args->flags & LINUX_CLONE_THREAD) { 227#ifdef notyet 228 PROC_LOCK(p2); 229 p2->p_pgrp = td->td_proc->p_pgrp; 230 PROC_UNLOCK(p2); 231#endif 232 exit_signal = 0; 233 } 234 235 if (args->flags & LINUX_CLONE_CHILD_SETTID) 236 em->child_set_tid = args->child_tidptr; 237 else 238 em->child_set_tid = NULL; 239 240 if (args->flags & LINUX_CLONE_CHILD_CLEARTID) 241 em->child_clear_tid = args->child_tidptr; 242 else 243 em->child_clear_tid = NULL; 244 245 EMUL_UNLOCK(&emul_lock); 246 247 if (args->flags & LINUX_CLONE_PARENT_SETTID) { 248 error = copyout(&p2->p_pid, args->parent_tidptr, 249 sizeof(p2->p_pid)); 250 if (error) 251 printf(LMSG("copyout failed!")); 252 } 253 254 PROC_LOCK(p2); 255 p2->p_sigparent = exit_signal; 256 PROC_UNLOCK(p2); 257 td2 = FIRST_THREAD_IN_PROC(p2); 258 /* 259 * In a case of stack = NULL, we are supposed to COW calling process 260 * stack. This is what normal fork() does, so we just keep tf_rsp arg 261 * intact. 262 */ 263 if (args->stack) 264 linux_set_upcall_kse(td2, PTROUT(args->stack)); 265 266 if (args->flags & LINUX_CLONE_SETTLS) 267 linux_set_cloned_tls(td2, args->tls); 268 269#ifdef DEBUG 270 if (ldebug(clone)) 271 printf(LMSG("clone: successful rfork to %d, " 272 "stack %p sig = %d"), (int)p2->p_pid, args->stack, 273 exit_signal); 274#endif 275 if (args->flags & LINUX_CLONE_VFORK) { 276 PROC_LOCK(p2); 277 p2->p_flag |= P_PPWAIT; 278 PROC_UNLOCK(p2); 279 } 280 281 /* 282 * Make this runnable after we are finished with it. 283 */ 284 thread_lock(td2); 285 TD_SET_CAN_RUN(td2); 286 sched_add(td2, SRQ_BORING); 287 thread_unlock(td2); 288 289 td->td_retval[0] = p2->p_pid; 290 td->td_retval[1] = 0; 291 292 if (args->flags & LINUX_CLONE_VFORK) { 293 /* wait for the children to exit, ie. emulate vfork */ 294 PROC_LOCK(p2); 295 while (p2->p_flag & P_PPWAIT) 296 cv_wait(&p2->p_pwait, &p2->p_mtx); 297 PROC_UNLOCK(p2); 298 } 299 300 return (0); 301} 302 303int 304linux_exit(struct thread *td, struct linux_exit_args *args) 305{ 306 307#ifdef DEBUG 308 if (ldebug(exit)) 309 printf(ARGS(exit, "%d"), args->rval); 310#endif 311 312 exit1(td, W_EXITCODE(args->rval, 0)); 313 /* NOTREACHED */ 314} 315