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