xref: /onnv-gate/usr/src/uts/common/syscall/lwp_create.c (revision 9351:f85876ac403e)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*9351SPrashanth.Sreenivasa@Sun.COM  * Common Development and Distribution License (the "License").
6*9351SPrashanth.Sreenivasa@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*9351SPrashanth.Sreenivasa@Sun.COM 
220Sstevel@tonic-gate /*
23*9351SPrashanth.Sreenivasa@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/param.h>
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/sysmacros.h>
320Sstevel@tonic-gate #include <sys/systm.h>
330Sstevel@tonic-gate #include <sys/errno.h>
340Sstevel@tonic-gate #include <sys/syscall.h>
350Sstevel@tonic-gate #include <sys/proc.h>
360Sstevel@tonic-gate #include <sys/processor.h>
370Sstevel@tonic-gate #include <sys/fault.h>
380Sstevel@tonic-gate #include <sys/ucontext.h>
390Sstevel@tonic-gate #include <sys/signal.h>
400Sstevel@tonic-gate #include <sys/unistd.h>
410Sstevel@tonic-gate #include <sys/procfs.h>
420Sstevel@tonic-gate #include <sys/prsystm.h>
430Sstevel@tonic-gate #include <sys/cmn_err.h>
440Sstevel@tonic-gate #include <sys/debug.h>
450Sstevel@tonic-gate #include <sys/klwp.h>
460Sstevel@tonic-gate #include <sys/pool.h>
470Sstevel@tonic-gate 
480Sstevel@tonic-gate /*
490Sstevel@tonic-gate  * System call to create an lwp.
500Sstevel@tonic-gate  *
510Sstevel@tonic-gate  * Notes on the LWP_DETACHED and LWP_DAEMON flags:
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * A detached lwp (LWP_DETACHED) cannot be the specific target of
540Sstevel@tonic-gate  * lwp_wait() (it is not joinable), but lwp_wait(0, ...) is required
550Sstevel@tonic-gate  * to sleep until all non-daemon detached lwps have terminated before
560Sstevel@tonic-gate  * returning EDEADLK because a detached lwp might create a non-detached lwp
570Sstevel@tonic-gate  * that could then be returned by lwp_wait(0, ...).  See also lwp_detach().
580Sstevel@tonic-gate  *
590Sstevel@tonic-gate  * A daemon lwp (LWP_DAEMON) is a detached lwp that has the additional
600Sstevel@tonic-gate  * property that it does not affect the termination condition of the
610Sstevel@tonic-gate  * process:  The last non-daemon lwp to call lwp_exit() causes the process
620Sstevel@tonic-gate  * to exit and lwp_wait(0, ...) does not sleep waiting for daemon lwps
630Sstevel@tonic-gate  * to terminate.  See the block comment before lwp_wait().
640Sstevel@tonic-gate  */
650Sstevel@tonic-gate int
syslwp_create(ucontext_t * ucp,int flags,id_t * new_lwp)660Sstevel@tonic-gate syslwp_create(ucontext_t *ucp, int flags, id_t *new_lwp)
670Sstevel@tonic-gate {
680Sstevel@tonic-gate 	klwp_t *lwp;
690Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
700Sstevel@tonic-gate 	kthread_t *t;
710Sstevel@tonic-gate 	ucontext_t uc;
720Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
730Sstevel@tonic-gate 	ucontext32_t uc32;
740Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
750Sstevel@tonic-gate 	k_sigset_t sigmask;
760Sstevel@tonic-gate 	int	tid;
770Sstevel@tonic-gate 	model_t model = get_udatamodel();
780Sstevel@tonic-gate 	uintptr_t thrptr = 0;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	if (flags & ~(LWP_DAEMON|LWP_DETACHED|LWP_SUSPENDED))
810Sstevel@tonic-gate 		return (set_errno(EINVAL));
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	/*
840Sstevel@tonic-gate 	 * lwp_create() is disallowed for the /proc agent lwp.
850Sstevel@tonic-gate 	 */
860Sstevel@tonic-gate 	if (curthread == p->p_agenttp)
870Sstevel@tonic-gate 		return (set_errno(ENOTSUP));
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	if (model == DATAMODEL_NATIVE) {
900Sstevel@tonic-gate 		if (copyin(ucp, &uc, sizeof (ucontext_t)))
910Sstevel@tonic-gate 			return (set_errno(EFAULT));
920Sstevel@tonic-gate 		sigutok(&uc.uc_sigmask, &sigmask);
930Sstevel@tonic-gate #if defined(__i386)
940Sstevel@tonic-gate 		/*
950Sstevel@tonic-gate 		 * libc stashed thrptr into unused kernel %sp.
960Sstevel@tonic-gate 		 * See setup_context() in libc.
970Sstevel@tonic-gate 		 */
980Sstevel@tonic-gate 		thrptr = (uint32_t)uc.uc_mcontext.gregs[ESP];
990Sstevel@tonic-gate #endif
1000Sstevel@tonic-gate 	}
1010Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
1020Sstevel@tonic-gate 	else {
1030Sstevel@tonic-gate 		if (copyin(ucp, &uc32, sizeof (ucontext32_t)))
1040Sstevel@tonic-gate 			return (set_errno(EFAULT));
1050Sstevel@tonic-gate 		sigutok(&uc32.uc_sigmask, &sigmask);
1060Sstevel@tonic-gate #if defined(__sparc)
1070Sstevel@tonic-gate 		ucontext_32ton(&uc32, &uc, NULL, NULL);
1080Sstevel@tonic-gate #else	/* __amd64 */
1090Sstevel@tonic-gate 		ucontext_32ton(&uc32, &uc);
1100Sstevel@tonic-gate 		/*
1110Sstevel@tonic-gate 		 * libc stashed thrptr into unused kernel %sp.
1120Sstevel@tonic-gate 		 * See setup_context() in libc.
1130Sstevel@tonic-gate 		 */
1140Sstevel@tonic-gate 		thrptr = (uint32_t)uc32.uc_mcontext.gregs[ESP];
1150Sstevel@tonic-gate #endif
1160Sstevel@tonic-gate 	}
1170Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
1180Sstevel@tonic-gate 
119*9351SPrashanth.Sreenivasa@Sun.COM 	/*
120*9351SPrashanth.Sreenivasa@Sun.COM 	 * Tell machine specific code that we are creating a new lwp
121*9351SPrashanth.Sreenivasa@Sun.COM 	 */
122*9351SPrashanth.Sreenivasa@Sun.COM 	LWP_MMODEL_NEWLWP();
123*9351SPrashanth.Sreenivasa@Sun.COM 
1240Sstevel@tonic-gate 	(void) save_syscall_args();	/* save args for tracing first */
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	mutex_enter(&curproc->p_lock);
1270Sstevel@tonic-gate 	pool_barrier_enter();
1280Sstevel@tonic-gate 	mutex_exit(&curproc->p_lock);
1290Sstevel@tonic-gate 	lwp = lwp_create(lwp_rtt, NULL, NULL, curproc, TS_STOPPED,
130*9351SPrashanth.Sreenivasa@Sun.COM 	    curthread->t_pri, &sigmask, curthread->t_cid, 0);
1310Sstevel@tonic-gate 	mutex_enter(&curproc->p_lock);
1320Sstevel@tonic-gate 	pool_barrier_exit();
1330Sstevel@tonic-gate 	mutex_exit(&curproc->p_lock);
1340Sstevel@tonic-gate 	if (lwp == NULL)
1350Sstevel@tonic-gate 		return (set_errno(EAGAIN));
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	lwp_load(lwp, uc.uc_mcontext.gregs, thrptr);
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	t = lwptot(lwp);
1400Sstevel@tonic-gate 	/*
1410Sstevel@tonic-gate 	 * Copy the new lwp's lwpid into the caller's specified buffer.
1420Sstevel@tonic-gate 	 */
1430Sstevel@tonic-gate 	if (new_lwp && copyout(&t->t_tid, new_lwp, sizeof (id_t))) {
1440Sstevel@tonic-gate 		/*
1450Sstevel@tonic-gate 		 * caller's buffer is not writable, return
1460Sstevel@tonic-gate 		 * EFAULT, and terminate new lwp.
1470Sstevel@tonic-gate 		 */
1480Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
1490Sstevel@tonic-gate 		t->t_proc_flag |= TP_EXITLWP;
1500Sstevel@tonic-gate 		t->t_sig_check = 1;
1510Sstevel@tonic-gate 		t->t_sysnum = 0;
1520Sstevel@tonic-gate 		t->t_proc_flag &= ~TP_HOLDLWP;
1530Sstevel@tonic-gate 		lwp_create_done(t);
1540Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
1550Sstevel@tonic-gate 		return (set_errno(EFAULT));
1560Sstevel@tonic-gate 	}
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	/*
1590Sstevel@tonic-gate 	 * clone callers context, if any.  must be invoked
1600Sstevel@tonic-gate 	 * while -not- holding p_lock.
1610Sstevel@tonic-gate 	 */
1620Sstevel@tonic-gate 	if (curthread->t_ctx)
1630Sstevel@tonic-gate 		lwp_createctx(curthread, t);
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	/*
1660Sstevel@tonic-gate 	 * copy current contract templates
1670Sstevel@tonic-gate 	 */
1680Sstevel@tonic-gate 	lwp_ctmpl_copy(lwp, ttolwp(curthread));
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1710Sstevel@tonic-gate 	/*
1720Sstevel@tonic-gate 	 * Copy the syscall arguments to the new lwp's arg area
1730Sstevel@tonic-gate 	 * for the benefit of debuggers.
1740Sstevel@tonic-gate 	 */
1750Sstevel@tonic-gate 	t->t_sysnum = SYS_lwp_create;
1760Sstevel@tonic-gate 	lwp->lwp_ap = lwp->lwp_arg;
1770Sstevel@tonic-gate 	lwp->lwp_arg[0] = (long)ucp;
1780Sstevel@tonic-gate 	lwp->lwp_arg[1] = (long)flags;
1790Sstevel@tonic-gate 	lwp->lwp_arg[2] = (long)new_lwp;
1800Sstevel@tonic-gate 	lwp->lwp_argsaved = 1;
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	if (!(flags & (LWP_DETACHED|LWP_DAEMON)))
1830Sstevel@tonic-gate 		t->t_proc_flag |= TP_TWAIT;
1840Sstevel@tonic-gate 	if (flags & LWP_DAEMON) {
1850Sstevel@tonic-gate 		t->t_proc_flag |= TP_DAEMON;
1860Sstevel@tonic-gate 		p->p_lwpdaemon++;
1870Sstevel@tonic-gate 	}
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	tid = (int)t->t_tid;	/* for /proc debuggers */
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	/*
1920Sstevel@tonic-gate 	 * We now set the newly-created lwp running.
1930Sstevel@tonic-gate 	 * If it is being created as LWP_SUSPENDED, we leave its
1940Sstevel@tonic-gate 	 * TP_HOLDLWP flag set so it will stop in system call exit.
1950Sstevel@tonic-gate 	 */
1960Sstevel@tonic-gate 	if (!(flags & LWP_SUSPENDED))
1970Sstevel@tonic-gate 		t->t_proc_flag &= ~TP_HOLDLWP;
1980Sstevel@tonic-gate 	lwp_create_done(t);
1990Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	return (tid);
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate /*
2050Sstevel@tonic-gate  * Exit the calling lwp
2060Sstevel@tonic-gate  */
2070Sstevel@tonic-gate void
syslwp_exit()2080Sstevel@tonic-gate syslwp_exit()
2090Sstevel@tonic-gate {
2100Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
2130Sstevel@tonic-gate 	lwp_exit();
2140Sstevel@tonic-gate 	/* NOTREACHED */
2150Sstevel@tonic-gate }
216