1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gate #include <sys/param.h> 32*0Sstevel@tonic-gate #include <sys/types.h> 33*0Sstevel@tonic-gate #include <sys/sysmacros.h> 34*0Sstevel@tonic-gate #include <sys/systm.h> 35*0Sstevel@tonic-gate #include <sys/errno.h> 36*0Sstevel@tonic-gate #include <sys/syscall.h> 37*0Sstevel@tonic-gate #include <sys/proc.h> 38*0Sstevel@tonic-gate #include <sys/processor.h> 39*0Sstevel@tonic-gate #include <sys/fault.h> 40*0Sstevel@tonic-gate #include <sys/ucontext.h> 41*0Sstevel@tonic-gate #include <sys/signal.h> 42*0Sstevel@tonic-gate #include <sys/unistd.h> 43*0Sstevel@tonic-gate #include <sys/procfs.h> 44*0Sstevel@tonic-gate #include <sys/prsystm.h> 45*0Sstevel@tonic-gate #include <sys/cmn_err.h> 46*0Sstevel@tonic-gate #include <sys/debug.h> 47*0Sstevel@tonic-gate #include <sys/klwp.h> 48*0Sstevel@tonic-gate #include <sys/pool.h> 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gate /* 51*0Sstevel@tonic-gate * System call to create an lwp. 52*0Sstevel@tonic-gate * 53*0Sstevel@tonic-gate * Notes on the LWP_DETACHED and LWP_DAEMON flags: 54*0Sstevel@tonic-gate * 55*0Sstevel@tonic-gate * A detached lwp (LWP_DETACHED) cannot be the specific target of 56*0Sstevel@tonic-gate * lwp_wait() (it is not joinable), but lwp_wait(0, ...) is required 57*0Sstevel@tonic-gate * to sleep until all non-daemon detached lwps have terminated before 58*0Sstevel@tonic-gate * returning EDEADLK because a detached lwp might create a non-detached lwp 59*0Sstevel@tonic-gate * that could then be returned by lwp_wait(0, ...). See also lwp_detach(). 60*0Sstevel@tonic-gate * 61*0Sstevel@tonic-gate * A daemon lwp (LWP_DAEMON) is a detached lwp that has the additional 62*0Sstevel@tonic-gate * property that it does not affect the termination condition of the 63*0Sstevel@tonic-gate * process: The last non-daemon lwp to call lwp_exit() causes the process 64*0Sstevel@tonic-gate * to exit and lwp_wait(0, ...) does not sleep waiting for daemon lwps 65*0Sstevel@tonic-gate * to terminate. See the block comment before lwp_wait(). 66*0Sstevel@tonic-gate */ 67*0Sstevel@tonic-gate int 68*0Sstevel@tonic-gate syslwp_create(ucontext_t *ucp, int flags, id_t *new_lwp) 69*0Sstevel@tonic-gate { 70*0Sstevel@tonic-gate klwp_t *lwp; 71*0Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 72*0Sstevel@tonic-gate kthread_t *t; 73*0Sstevel@tonic-gate ucontext_t uc; 74*0Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 75*0Sstevel@tonic-gate ucontext32_t uc32; 76*0Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */ 77*0Sstevel@tonic-gate k_sigset_t sigmask; 78*0Sstevel@tonic-gate int tid; 79*0Sstevel@tonic-gate model_t model = get_udatamodel(); 80*0Sstevel@tonic-gate uintptr_t thrptr = 0; 81*0Sstevel@tonic-gate 82*0Sstevel@tonic-gate if (flags & ~(LWP_DAEMON|LWP_DETACHED|LWP_SUSPENDED)) 83*0Sstevel@tonic-gate return (set_errno(EINVAL)); 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gate /* 86*0Sstevel@tonic-gate * lwp_create() is disallowed for the /proc agent lwp. 87*0Sstevel@tonic-gate */ 88*0Sstevel@tonic-gate if (curthread == p->p_agenttp) 89*0Sstevel@tonic-gate return (set_errno(ENOTSUP)); 90*0Sstevel@tonic-gate 91*0Sstevel@tonic-gate if (model == DATAMODEL_NATIVE) { 92*0Sstevel@tonic-gate if (copyin(ucp, &uc, sizeof (ucontext_t))) 93*0Sstevel@tonic-gate return (set_errno(EFAULT)); 94*0Sstevel@tonic-gate sigutok(&uc.uc_sigmask, &sigmask); 95*0Sstevel@tonic-gate #if defined(__i386) 96*0Sstevel@tonic-gate /* 97*0Sstevel@tonic-gate * libc stashed thrptr into unused kernel %sp. 98*0Sstevel@tonic-gate * See setup_context() in libc. 99*0Sstevel@tonic-gate */ 100*0Sstevel@tonic-gate thrptr = (uint32_t)uc.uc_mcontext.gregs[ESP]; 101*0Sstevel@tonic-gate #endif 102*0Sstevel@tonic-gate } 103*0Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 104*0Sstevel@tonic-gate else { 105*0Sstevel@tonic-gate if (copyin(ucp, &uc32, sizeof (ucontext32_t))) 106*0Sstevel@tonic-gate return (set_errno(EFAULT)); 107*0Sstevel@tonic-gate sigutok(&uc32.uc_sigmask, &sigmask); 108*0Sstevel@tonic-gate #if defined(__sparc) 109*0Sstevel@tonic-gate ucontext_32ton(&uc32, &uc, NULL, NULL); 110*0Sstevel@tonic-gate #else /* __amd64 */ 111*0Sstevel@tonic-gate ucontext_32ton(&uc32, &uc); 112*0Sstevel@tonic-gate /* 113*0Sstevel@tonic-gate * libc stashed thrptr into unused kernel %sp. 114*0Sstevel@tonic-gate * See setup_context() in libc. 115*0Sstevel@tonic-gate */ 116*0Sstevel@tonic-gate thrptr = (uint32_t)uc32.uc_mcontext.gregs[ESP]; 117*0Sstevel@tonic-gate #endif 118*0Sstevel@tonic-gate } 119*0Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */ 120*0Sstevel@tonic-gate 121*0Sstevel@tonic-gate (void) save_syscall_args(); /* save args for tracing first */ 122*0Sstevel@tonic-gate 123*0Sstevel@tonic-gate mutex_enter(&curproc->p_lock); 124*0Sstevel@tonic-gate pool_barrier_enter(); 125*0Sstevel@tonic-gate mutex_exit(&curproc->p_lock); 126*0Sstevel@tonic-gate lwp = lwp_create(lwp_rtt, NULL, NULL, curproc, TS_STOPPED, 127*0Sstevel@tonic-gate curthread->t_pri, &sigmask, curthread->t_cid, 0); 128*0Sstevel@tonic-gate mutex_enter(&curproc->p_lock); 129*0Sstevel@tonic-gate pool_barrier_exit(); 130*0Sstevel@tonic-gate mutex_exit(&curproc->p_lock); 131*0Sstevel@tonic-gate if (lwp == NULL) 132*0Sstevel@tonic-gate return (set_errno(EAGAIN)); 133*0Sstevel@tonic-gate 134*0Sstevel@tonic-gate lwp_load(lwp, uc.uc_mcontext.gregs, thrptr); 135*0Sstevel@tonic-gate 136*0Sstevel@tonic-gate t = lwptot(lwp); 137*0Sstevel@tonic-gate /* 138*0Sstevel@tonic-gate * Copy the new lwp's lwpid into the caller's specified buffer. 139*0Sstevel@tonic-gate */ 140*0Sstevel@tonic-gate if (new_lwp && copyout(&t->t_tid, new_lwp, sizeof (id_t))) { 141*0Sstevel@tonic-gate /* 142*0Sstevel@tonic-gate * caller's buffer is not writable, return 143*0Sstevel@tonic-gate * EFAULT, and terminate new lwp. 144*0Sstevel@tonic-gate */ 145*0Sstevel@tonic-gate mutex_enter(&p->p_lock); 146*0Sstevel@tonic-gate t->t_proc_flag |= TP_EXITLWP; 147*0Sstevel@tonic-gate t->t_sig_check = 1; 148*0Sstevel@tonic-gate t->t_sysnum = 0; 149*0Sstevel@tonic-gate t->t_proc_flag &= ~TP_HOLDLWP; 150*0Sstevel@tonic-gate lwp_create_done(t); 151*0Sstevel@tonic-gate mutex_exit(&p->p_lock); 152*0Sstevel@tonic-gate return (set_errno(EFAULT)); 153*0Sstevel@tonic-gate } 154*0Sstevel@tonic-gate 155*0Sstevel@tonic-gate /* 156*0Sstevel@tonic-gate * clone callers context, if any. must be invoked 157*0Sstevel@tonic-gate * while -not- holding p_lock. 158*0Sstevel@tonic-gate */ 159*0Sstevel@tonic-gate if (curthread->t_ctx) 160*0Sstevel@tonic-gate lwp_createctx(curthread, t); 161*0Sstevel@tonic-gate 162*0Sstevel@tonic-gate /* 163*0Sstevel@tonic-gate * copy current contract templates 164*0Sstevel@tonic-gate */ 165*0Sstevel@tonic-gate lwp_ctmpl_copy(lwp, ttolwp(curthread)); 166*0Sstevel@tonic-gate 167*0Sstevel@tonic-gate mutex_enter(&p->p_lock); 168*0Sstevel@tonic-gate /* 169*0Sstevel@tonic-gate * Copy the syscall arguments to the new lwp's arg area 170*0Sstevel@tonic-gate * for the benefit of debuggers. 171*0Sstevel@tonic-gate */ 172*0Sstevel@tonic-gate t->t_sysnum = SYS_lwp_create; 173*0Sstevel@tonic-gate lwp->lwp_ap = lwp->lwp_arg; 174*0Sstevel@tonic-gate lwp->lwp_arg[0] = (long)ucp; 175*0Sstevel@tonic-gate lwp->lwp_arg[1] = (long)flags; 176*0Sstevel@tonic-gate lwp->lwp_arg[2] = (long)new_lwp; 177*0Sstevel@tonic-gate lwp->lwp_argsaved = 1; 178*0Sstevel@tonic-gate 179*0Sstevel@tonic-gate if (!(flags & (LWP_DETACHED|LWP_DAEMON))) 180*0Sstevel@tonic-gate t->t_proc_flag |= TP_TWAIT; 181*0Sstevel@tonic-gate if (flags & LWP_DAEMON) { 182*0Sstevel@tonic-gate t->t_proc_flag |= TP_DAEMON; 183*0Sstevel@tonic-gate p->p_lwpdaemon++; 184*0Sstevel@tonic-gate } 185*0Sstevel@tonic-gate 186*0Sstevel@tonic-gate tid = (int)t->t_tid; /* for /proc debuggers */ 187*0Sstevel@tonic-gate 188*0Sstevel@tonic-gate /* 189*0Sstevel@tonic-gate * We now set the newly-created lwp running. 190*0Sstevel@tonic-gate * If it is being created as LWP_SUSPENDED, we leave its 191*0Sstevel@tonic-gate * TP_HOLDLWP flag set so it will stop in system call exit. 192*0Sstevel@tonic-gate */ 193*0Sstevel@tonic-gate if (!(flags & LWP_SUSPENDED)) 194*0Sstevel@tonic-gate t->t_proc_flag &= ~TP_HOLDLWP; 195*0Sstevel@tonic-gate lwp_create_done(t); 196*0Sstevel@tonic-gate mutex_exit(&p->p_lock); 197*0Sstevel@tonic-gate 198*0Sstevel@tonic-gate return (tid); 199*0Sstevel@tonic-gate } 200*0Sstevel@tonic-gate 201*0Sstevel@tonic-gate /* 202*0Sstevel@tonic-gate * Exit the calling lwp 203*0Sstevel@tonic-gate */ 204*0Sstevel@tonic-gate void 205*0Sstevel@tonic-gate syslwp_exit() 206*0Sstevel@tonic-gate { 207*0Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 208*0Sstevel@tonic-gate 209*0Sstevel@tonic-gate mutex_enter(&p->p_lock); 210*0Sstevel@tonic-gate lwp_exit(); 211*0Sstevel@tonic-gate /* NOTREACHED */ 212*0Sstevel@tonic-gate } 213