17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52c5124a1SPrashanth Sreenivasa  * Common Development and Distribution License (the "License").
62c5124a1SPrashanth Sreenivasa  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
212c5124a1SPrashanth Sreenivasa 
227c478bd9Sstevel@tonic-gate /*
232c5124a1SPrashanth Sreenivasa  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/param.h>
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
327c478bd9Sstevel@tonic-gate #include <sys/systm.h>
337c478bd9Sstevel@tonic-gate #include <sys/errno.h>
347c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
357c478bd9Sstevel@tonic-gate #include <sys/proc.h>
367c478bd9Sstevel@tonic-gate #include <sys/processor.h>
377c478bd9Sstevel@tonic-gate #include <sys/fault.h>
387c478bd9Sstevel@tonic-gate #include <sys/ucontext.h>
397c478bd9Sstevel@tonic-gate #include <sys/signal.h>
407c478bd9Sstevel@tonic-gate #include <sys/unistd.h>
417c478bd9Sstevel@tonic-gate #include <sys/procfs.h>
427c478bd9Sstevel@tonic-gate #include <sys/prsystm.h>
437c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
447c478bd9Sstevel@tonic-gate #include <sys/debug.h>
457c478bd9Sstevel@tonic-gate #include <sys/klwp.h>
467c478bd9Sstevel@tonic-gate #include <sys/pool.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  * System call to create an lwp.
507c478bd9Sstevel@tonic-gate  *
517c478bd9Sstevel@tonic-gate  * Notes on the LWP_DETACHED and LWP_DAEMON flags:
527c478bd9Sstevel@tonic-gate  *
537c478bd9Sstevel@tonic-gate  * A detached lwp (LWP_DETACHED) cannot be the specific target of
547c478bd9Sstevel@tonic-gate  * lwp_wait() (it is not joinable), but lwp_wait(0, ...) is required
557c478bd9Sstevel@tonic-gate  * to sleep until all non-daemon detached lwps have terminated before
567c478bd9Sstevel@tonic-gate  * returning EDEADLK because a detached lwp might create a non-detached lwp
577c478bd9Sstevel@tonic-gate  * that could then be returned by lwp_wait(0, ...).  See also lwp_detach().
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  * A daemon lwp (LWP_DAEMON) is a detached lwp that has the additional
607c478bd9Sstevel@tonic-gate  * property that it does not affect the termination condition of the
617c478bd9Sstevel@tonic-gate  * process:  The last non-daemon lwp to call lwp_exit() causes the process
627c478bd9Sstevel@tonic-gate  * to exit and lwp_wait(0, ...) does not sleep waiting for daemon lwps
637c478bd9Sstevel@tonic-gate  * to terminate.  See the block comment before lwp_wait().
647c478bd9Sstevel@tonic-gate  */
657c478bd9Sstevel@tonic-gate int
syslwp_create(ucontext_t * ucp,int flags,id_t * new_lwp)667c478bd9Sstevel@tonic-gate syslwp_create(ucontext_t *ucp, int flags, id_t *new_lwp)
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	klwp_t *lwp;
697c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
707c478bd9Sstevel@tonic-gate 	kthread_t *t;
717c478bd9Sstevel@tonic-gate 	ucontext_t uc;
727c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
737c478bd9Sstevel@tonic-gate 	ucontext32_t uc32;
747c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
757c478bd9Sstevel@tonic-gate 	k_sigset_t sigmask;
767c478bd9Sstevel@tonic-gate 	int	tid;
777c478bd9Sstevel@tonic-gate 	model_t model = get_udatamodel();
787c478bd9Sstevel@tonic-gate 	uintptr_t thrptr = 0;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	if (flags & ~(LWP_DAEMON|LWP_DETACHED|LWP_SUSPENDED))
817c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	/*
847c478bd9Sstevel@tonic-gate 	 * lwp_create() is disallowed for the /proc agent lwp.
857c478bd9Sstevel@tonic-gate 	 */
867c478bd9Sstevel@tonic-gate 	if (curthread == p->p_agenttp)
877c478bd9Sstevel@tonic-gate 		return (set_errno(ENOTSUP));
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	if (model == DATAMODEL_NATIVE) {
907c478bd9Sstevel@tonic-gate 		if (copyin(ucp, &uc, sizeof (ucontext_t)))
917c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
927c478bd9Sstevel@tonic-gate 		sigutok(&uc.uc_sigmask, &sigmask);
937c478bd9Sstevel@tonic-gate 	}
947c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
957c478bd9Sstevel@tonic-gate 	else {
967c478bd9Sstevel@tonic-gate 		if (copyin(ucp, &uc32, sizeof (ucontext32_t)))
977c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
987c478bd9Sstevel@tonic-gate 		sigutok(&uc32.uc_sigmask, &sigmask);
997c478bd9Sstevel@tonic-gate #if defined(__sparc)
1007c478bd9Sstevel@tonic-gate 		ucontext_32ton(&uc32, &uc, NULL, NULL);
1017c478bd9Sstevel@tonic-gate #else	/* __amd64 */
1027c478bd9Sstevel@tonic-gate 		ucontext_32ton(&uc32, &uc);
1037c478bd9Sstevel@tonic-gate 		/*
1047c478bd9Sstevel@tonic-gate 		 * libc stashed thrptr into unused kernel %sp.
1057c478bd9Sstevel@tonic-gate 		 * See setup_context() in libc.
1067c478bd9Sstevel@tonic-gate 		 */
1077c478bd9Sstevel@tonic-gate 		thrptr = (uint32_t)uc32.uc_mcontext.gregs[ESP];
1087c478bd9Sstevel@tonic-gate #endif
1097c478bd9Sstevel@tonic-gate 	}
1107c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
1117c478bd9Sstevel@tonic-gate 
1122c5124a1SPrashanth Sreenivasa 	/*
1132c5124a1SPrashanth Sreenivasa 	 * Tell machine specific code that we are creating a new lwp
1142c5124a1SPrashanth Sreenivasa 	 */
1152c5124a1SPrashanth Sreenivasa 	LWP_MMODEL_NEWLWP();
1162c5124a1SPrashanth Sreenivasa 
1177c478bd9Sstevel@tonic-gate 	(void) save_syscall_args();	/* save args for tracing first */
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	mutex_enter(&curproc->p_lock);
1207c478bd9Sstevel@tonic-gate 	pool_barrier_enter();
1217c478bd9Sstevel@tonic-gate 	mutex_exit(&curproc->p_lock);
122*7e12ceb3SToomas Soome 	lwp = lwp_create(lwp_rtt, NULL, 0, curproc, TS_STOPPED,
1232c5124a1SPrashanth Sreenivasa 	    curthread->t_pri, &sigmask, curthread->t_cid, 0);
1247c478bd9Sstevel@tonic-gate 	mutex_enter(&curproc->p_lock);
1257c478bd9Sstevel@tonic-gate 	pool_barrier_exit();
1267c478bd9Sstevel@tonic-gate 	mutex_exit(&curproc->p_lock);
1277c478bd9Sstevel@tonic-gate 	if (lwp == NULL)
1287c478bd9Sstevel@tonic-gate 		return (set_errno(EAGAIN));
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	lwp_load(lwp, uc.uc_mcontext.gregs, thrptr);
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	t = lwptot(lwp);
1337c478bd9Sstevel@tonic-gate 	/*
1347c478bd9Sstevel@tonic-gate 	 * Copy the new lwp's lwpid into the caller's specified buffer.
1357c478bd9Sstevel@tonic-gate 	 */
1367c478bd9Sstevel@tonic-gate 	if (new_lwp && copyout(&t->t_tid, new_lwp, sizeof (id_t))) {
1377c478bd9Sstevel@tonic-gate 		/*
1387c478bd9Sstevel@tonic-gate 		 * caller's buffer is not writable, return
1397c478bd9Sstevel@tonic-gate 		 * EFAULT, and terminate new lwp.
1407c478bd9Sstevel@tonic-gate 		 */
1417c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
1427c478bd9Sstevel@tonic-gate 		t->t_proc_flag |= TP_EXITLWP;
1437c478bd9Sstevel@tonic-gate 		t->t_sig_check = 1;
1447c478bd9Sstevel@tonic-gate 		t->t_sysnum = 0;
1457c478bd9Sstevel@tonic-gate 		t->t_proc_flag &= ~TP_HOLDLWP;
1467c478bd9Sstevel@tonic-gate 		lwp_create_done(t);
1477c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
1487c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
1497c478bd9Sstevel@tonic-gate 	}
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	/*
1527c478bd9Sstevel@tonic-gate 	 * clone callers context, if any.  must be invoked
1537c478bd9Sstevel@tonic-gate 	 * while -not- holding p_lock.
1547c478bd9Sstevel@tonic-gate 	 */
1557c478bd9Sstevel@tonic-gate 	if (curthread->t_ctx)
1567c478bd9Sstevel@tonic-gate 		lwp_createctx(curthread, t);
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	/*
1597c478bd9Sstevel@tonic-gate 	 * copy current contract templates
1607c478bd9Sstevel@tonic-gate 	 */
1617c478bd9Sstevel@tonic-gate 	lwp_ctmpl_copy(lwp, ttolwp(curthread));
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1647c478bd9Sstevel@tonic-gate 	/*
1657c478bd9Sstevel@tonic-gate 	 * Copy the syscall arguments to the new lwp's arg area
1667c478bd9Sstevel@tonic-gate 	 * for the benefit of debuggers.
1677c478bd9Sstevel@tonic-gate 	 */
1687c478bd9Sstevel@tonic-gate 	t->t_sysnum = SYS_lwp_create;
1697c478bd9Sstevel@tonic-gate 	lwp->lwp_ap = lwp->lwp_arg;
1707c478bd9Sstevel@tonic-gate 	lwp->lwp_arg[0] = (long)ucp;
1717c478bd9Sstevel@tonic-gate 	lwp->lwp_arg[1] = (long)flags;
1727c478bd9Sstevel@tonic-gate 	lwp->lwp_arg[2] = (long)new_lwp;
1737c478bd9Sstevel@tonic-gate 	lwp->lwp_argsaved = 1;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	if (!(flags & (LWP_DETACHED|LWP_DAEMON)))
1767c478bd9Sstevel@tonic-gate 		t->t_proc_flag |= TP_TWAIT;
1777c478bd9Sstevel@tonic-gate 	if (flags & LWP_DAEMON) {
1787c478bd9Sstevel@tonic-gate 		t->t_proc_flag |= TP_DAEMON;
1797c478bd9Sstevel@tonic-gate 		p->p_lwpdaemon++;
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	tid = (int)t->t_tid;	/* for /proc debuggers */
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	/*
1857c478bd9Sstevel@tonic-gate 	 * We now set the newly-created lwp running.
1867c478bd9Sstevel@tonic-gate 	 * If it is being created as LWP_SUSPENDED, we leave its
1877c478bd9Sstevel@tonic-gate 	 * TP_HOLDLWP flag set so it will stop in system call exit.
1887c478bd9Sstevel@tonic-gate 	 */
1897c478bd9Sstevel@tonic-gate 	if (!(flags & LWP_SUSPENDED))
1907c478bd9Sstevel@tonic-gate 		t->t_proc_flag &= ~TP_HOLDLWP;
1917c478bd9Sstevel@tonic-gate 	lwp_create_done(t);
1927c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	return (tid);
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate /*
1987c478bd9Sstevel@tonic-gate  * Exit the calling lwp
1997c478bd9Sstevel@tonic-gate  */
2007c478bd9Sstevel@tonic-gate void
syslwp_exit()2017c478bd9Sstevel@tonic-gate syslwp_exit()
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
2067c478bd9Sstevel@tonic-gate 	lwp_exit();
2077c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
2087c478bd9Sstevel@tonic-gate }
209