xref: /titanic_50/usr/src/uts/common/syscall/lwp_create.c (revision 2c5124a1a74b6e8fcb1cf8eb9a8d46b5a583086f)
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
5*2c5124a1SPrashanth Sreenivasa  * Common Development and Distribution License (the "License").
6*2c5124a1SPrashanth 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  */
21*2c5124a1SPrashanth Sreenivasa 
227c478bd9Sstevel@tonic-gate /*
23*2c5124a1SPrashanth 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 #if defined(__i386)
947c478bd9Sstevel@tonic-gate 		/*
957c478bd9Sstevel@tonic-gate 		 * libc stashed thrptr into unused kernel %sp.
967c478bd9Sstevel@tonic-gate 		 * See setup_context() in libc.
977c478bd9Sstevel@tonic-gate 		 */
987c478bd9Sstevel@tonic-gate 		thrptr = (uint32_t)uc.uc_mcontext.gregs[ESP];
997c478bd9Sstevel@tonic-gate #endif
1007c478bd9Sstevel@tonic-gate 	}
1017c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
1027c478bd9Sstevel@tonic-gate 	else {
1037c478bd9Sstevel@tonic-gate 		if (copyin(ucp, &uc32, sizeof (ucontext32_t)))
1047c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
1057c478bd9Sstevel@tonic-gate 		sigutok(&uc32.uc_sigmask, &sigmask);
1067c478bd9Sstevel@tonic-gate #if defined(__sparc)
1077c478bd9Sstevel@tonic-gate 		ucontext_32ton(&uc32, &uc, NULL, NULL);
1087c478bd9Sstevel@tonic-gate #else	/* __amd64 */
1097c478bd9Sstevel@tonic-gate 		ucontext_32ton(&uc32, &uc);
1107c478bd9Sstevel@tonic-gate 		/*
1117c478bd9Sstevel@tonic-gate 		 * libc stashed thrptr into unused kernel %sp.
1127c478bd9Sstevel@tonic-gate 		 * See setup_context() in libc.
1137c478bd9Sstevel@tonic-gate 		 */
1147c478bd9Sstevel@tonic-gate 		thrptr = (uint32_t)uc32.uc_mcontext.gregs[ESP];
1157c478bd9Sstevel@tonic-gate #endif
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
1187c478bd9Sstevel@tonic-gate 
119*2c5124a1SPrashanth Sreenivasa 	/*
120*2c5124a1SPrashanth Sreenivasa 	 * Tell machine specific code that we are creating a new lwp
121*2c5124a1SPrashanth Sreenivasa 	 */
122*2c5124a1SPrashanth Sreenivasa 	LWP_MMODEL_NEWLWP();
123*2c5124a1SPrashanth Sreenivasa 
1247c478bd9Sstevel@tonic-gate 	(void) save_syscall_args();	/* save args for tracing first */
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	mutex_enter(&curproc->p_lock);
1277c478bd9Sstevel@tonic-gate 	pool_barrier_enter();
1287c478bd9Sstevel@tonic-gate 	mutex_exit(&curproc->p_lock);
1297c478bd9Sstevel@tonic-gate 	lwp = lwp_create(lwp_rtt, NULL, NULL, curproc, TS_STOPPED,
1307c478bd9Sstevel@tonic-gate 	    curthread->t_pri, &sigmask, curthread->t_cid, 0);
1317c478bd9Sstevel@tonic-gate 	mutex_enter(&curproc->p_lock);
1327c478bd9Sstevel@tonic-gate 	pool_barrier_exit();
1337c478bd9Sstevel@tonic-gate 	mutex_exit(&curproc->p_lock);
1347c478bd9Sstevel@tonic-gate 	if (lwp == NULL)
1357c478bd9Sstevel@tonic-gate 		return (set_errno(EAGAIN));
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	lwp_load(lwp, uc.uc_mcontext.gregs, thrptr);
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	t = lwptot(lwp);
1407c478bd9Sstevel@tonic-gate 	/*
1417c478bd9Sstevel@tonic-gate 	 * Copy the new lwp's lwpid into the caller's specified buffer.
1427c478bd9Sstevel@tonic-gate 	 */
1437c478bd9Sstevel@tonic-gate 	if (new_lwp && copyout(&t->t_tid, new_lwp, sizeof (id_t))) {
1447c478bd9Sstevel@tonic-gate 		/*
1457c478bd9Sstevel@tonic-gate 		 * caller's buffer is not writable, return
1467c478bd9Sstevel@tonic-gate 		 * EFAULT, and terminate new lwp.
1477c478bd9Sstevel@tonic-gate 		 */
1487c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
1497c478bd9Sstevel@tonic-gate 		t->t_proc_flag |= TP_EXITLWP;
1507c478bd9Sstevel@tonic-gate 		t->t_sig_check = 1;
1517c478bd9Sstevel@tonic-gate 		t->t_sysnum = 0;
1527c478bd9Sstevel@tonic-gate 		t->t_proc_flag &= ~TP_HOLDLWP;
1537c478bd9Sstevel@tonic-gate 		lwp_create_done(t);
1547c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
1557c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	/*
1597c478bd9Sstevel@tonic-gate 	 * clone callers context, if any.  must be invoked
1607c478bd9Sstevel@tonic-gate 	 * while -not- holding p_lock.
1617c478bd9Sstevel@tonic-gate 	 */
1627c478bd9Sstevel@tonic-gate 	if (curthread->t_ctx)
1637c478bd9Sstevel@tonic-gate 		lwp_createctx(curthread, t);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	/*
1667c478bd9Sstevel@tonic-gate 	 * copy current contract templates
1677c478bd9Sstevel@tonic-gate 	 */
1687c478bd9Sstevel@tonic-gate 	lwp_ctmpl_copy(lwp, ttolwp(curthread));
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1717c478bd9Sstevel@tonic-gate 	/*
1727c478bd9Sstevel@tonic-gate 	 * Copy the syscall arguments to the new lwp's arg area
1737c478bd9Sstevel@tonic-gate 	 * for the benefit of debuggers.
1747c478bd9Sstevel@tonic-gate 	 */
1757c478bd9Sstevel@tonic-gate 	t->t_sysnum = SYS_lwp_create;
1767c478bd9Sstevel@tonic-gate 	lwp->lwp_ap = lwp->lwp_arg;
1777c478bd9Sstevel@tonic-gate 	lwp->lwp_arg[0] = (long)ucp;
1787c478bd9Sstevel@tonic-gate 	lwp->lwp_arg[1] = (long)flags;
1797c478bd9Sstevel@tonic-gate 	lwp->lwp_arg[2] = (long)new_lwp;
1807c478bd9Sstevel@tonic-gate 	lwp->lwp_argsaved = 1;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	if (!(flags & (LWP_DETACHED|LWP_DAEMON)))
1837c478bd9Sstevel@tonic-gate 		t->t_proc_flag |= TP_TWAIT;
1847c478bd9Sstevel@tonic-gate 	if (flags & LWP_DAEMON) {
1857c478bd9Sstevel@tonic-gate 		t->t_proc_flag |= TP_DAEMON;
1867c478bd9Sstevel@tonic-gate 		p->p_lwpdaemon++;
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	tid = (int)t->t_tid;	/* for /proc debuggers */
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	/*
1927c478bd9Sstevel@tonic-gate 	 * We now set the newly-created lwp running.
1937c478bd9Sstevel@tonic-gate 	 * If it is being created as LWP_SUSPENDED, we leave its
1947c478bd9Sstevel@tonic-gate 	 * TP_HOLDLWP flag set so it will stop in system call exit.
1957c478bd9Sstevel@tonic-gate 	 */
1967c478bd9Sstevel@tonic-gate 	if (!(flags & LWP_SUSPENDED))
1977c478bd9Sstevel@tonic-gate 		t->t_proc_flag &= ~TP_HOLDLWP;
1987c478bd9Sstevel@tonic-gate 	lwp_create_done(t);
1997c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	return (tid);
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate /*
2057c478bd9Sstevel@tonic-gate  * Exit the calling lwp
2067c478bd9Sstevel@tonic-gate  */
2077c478bd9Sstevel@tonic-gate void
syslwp_exit()2087c478bd9Sstevel@tonic-gate syslwp_exit()
2097c478bd9Sstevel@tonic-gate {
2107c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
2137c478bd9Sstevel@tonic-gate 	lwp_exit();
2147c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
2157c478bd9Sstevel@tonic-gate }
216