xref: /illumos-gate/usr/src/uts/common/syscall/lwpsys.c (revision c6f039c7)
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
547eb4d1eSsl  * Common Development and Distribution License (the "License").
647eb4d1eSsl  * 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  */
216eb30ec3SRoger A. Faulkner 
227c478bd9Sstevel@tonic-gate /*
236eb30ec3SRoger A. Faulkner  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
2548bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/param.h>
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
337c478bd9Sstevel@tonic-gate #include <sys/systm.h>
347c478bd9Sstevel@tonic-gate #include <sys/prsystm.h>
357c478bd9Sstevel@tonic-gate #include <sys/cred.h>
367c478bd9Sstevel@tonic-gate #include <sys/errno.h>
377c478bd9Sstevel@tonic-gate #include <sys/proc.h>
387c478bd9Sstevel@tonic-gate #include <sys/signal.h>
397c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
407c478bd9Sstevel@tonic-gate #include <sys/unistd.h>
417c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
427c478bd9Sstevel@tonic-gate #include <sys/schedctl.h>
437c478bd9Sstevel@tonic-gate #include <sys/debug.h>
447c478bd9Sstevel@tonic-gate #include <sys/contract/process_impl.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate kthread_t *
idtot(proc_t * p,id_t lwpid)477c478bd9Sstevel@tonic-gate idtot(proc_t *p, id_t lwpid)
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate 	lwpdir_t *ldp;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate 	if ((ldp = lwp_hash_lookup(p, lwpid)) != NULL)
527c478bd9Sstevel@tonic-gate 		return (ldp->ld_entry->le_thread);
537c478bd9Sstevel@tonic-gate 	return (NULL);
547c478bd9Sstevel@tonic-gate }
557c478bd9Sstevel@tonic-gate 
566eb30ec3SRoger A. Faulkner /*
576eb30ec3SRoger A. Faulkner  * Same as idtot(), but acquire and return
586eb30ec3SRoger A. Faulkner  * the tid hash table entry lock on success.
596eb30ec3SRoger A. Faulkner  * This allows lwp_unpark() to do its job without acquiring
606eb30ec3SRoger A. Faulkner  * p->p_lock (and thereby causing congestion problems when
616eb30ec3SRoger A. Faulkner  * the application calls lwp_unpark() too often).
626eb30ec3SRoger A. Faulkner  */
636eb30ec3SRoger A. Faulkner static kthread_t *
idtot_and_lock(proc_t * p,id_t lwpid,kmutex_t ** mpp)646eb30ec3SRoger A. Faulkner idtot_and_lock(proc_t *p, id_t lwpid, kmutex_t **mpp)
656eb30ec3SRoger A. Faulkner {
666eb30ec3SRoger A. Faulkner 	lwpdir_t *ldp;
676eb30ec3SRoger A. Faulkner 	kthread_t *t;
686eb30ec3SRoger A. Faulkner 
696eb30ec3SRoger A. Faulkner 	if ((ldp = lwp_hash_lookup_and_lock(p, lwpid, mpp)) != NULL) {
706eb30ec3SRoger A. Faulkner 		if ((t = ldp->ld_entry->le_thread) == NULL)
716eb30ec3SRoger A. Faulkner 			mutex_exit(*mpp);
726eb30ec3SRoger A. Faulkner 		return (t);
736eb30ec3SRoger A. Faulkner 	}
746eb30ec3SRoger A. Faulkner 	return (NULL);
756eb30ec3SRoger A. Faulkner }
766eb30ec3SRoger A. Faulkner 
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate  * Stop an lwp of the current process
797c478bd9Sstevel@tonic-gate  */
807c478bd9Sstevel@tonic-gate int
syslwp_suspend(id_t lwpid)817c478bd9Sstevel@tonic-gate syslwp_suspend(id_t lwpid)
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate 	kthread_t *t;
847c478bd9Sstevel@tonic-gate 	int error;
857c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
887c478bd9Sstevel@tonic-gate 	if ((t = idtot(p, lwpid)) == NULL)
897c478bd9Sstevel@tonic-gate 		error = ESRCH;
907c478bd9Sstevel@tonic-gate 	else
917c478bd9Sstevel@tonic-gate 		error = lwp_suspend(t);
927c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
937c478bd9Sstevel@tonic-gate 	if (error)
947c478bd9Sstevel@tonic-gate 		return (set_errno(error));
957c478bd9Sstevel@tonic-gate 	return (0);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate int
syslwp_continue(id_t lwpid)997c478bd9Sstevel@tonic-gate syslwp_continue(id_t lwpid)
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	kthread_t *t;
1027c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1057c478bd9Sstevel@tonic-gate 	if ((t = idtot(p, lwpid)) == NULL) {
1067c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
1077c478bd9Sstevel@tonic-gate 		return (set_errno(ESRCH));
1087c478bd9Sstevel@tonic-gate 	}
1097c478bd9Sstevel@tonic-gate 	lwp_continue(t);
1107c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1117c478bd9Sstevel@tonic-gate 	return (0);
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate int
lwp_kill(id_t lwpid,int sig)1157c478bd9Sstevel@tonic-gate lwp_kill(id_t lwpid, int sig)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate 	sigqueue_t *sqp;
1187c478bd9Sstevel@tonic-gate 	kthread_t *t;
1197c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	if (sig < 0 || sig >= NSIG)
1227c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
1237c478bd9Sstevel@tonic-gate 	if (sig != 0)
1247c478bd9Sstevel@tonic-gate 		sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
1257c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1267c478bd9Sstevel@tonic-gate 	if ((t = idtot(p, lwpid)) == NULL) {
1277c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
1287c478bd9Sstevel@tonic-gate 		if (sig != 0)
1297c478bd9Sstevel@tonic-gate 			kmem_free(sqp, sizeof (sigqueue_t));
1307c478bd9Sstevel@tonic-gate 		return (set_errno(ESRCH));
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 	if (sig == 0) {
1337c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
1347c478bd9Sstevel@tonic-gate 		return (0);
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 	sqp->sq_info.si_signo = sig;
1377c478bd9Sstevel@tonic-gate 	sqp->sq_info.si_code = SI_LWP;
1387c478bd9Sstevel@tonic-gate 	sqp->sq_info.si_pid = p->p_pid;
1397c478bd9Sstevel@tonic-gate 	sqp->sq_info.si_ctid = PRCTID(p);
1407c478bd9Sstevel@tonic-gate 	sqp->sq_info.si_zoneid = getzoneid();
1417c478bd9Sstevel@tonic-gate 	sqp->sq_info.si_uid = crgetruid(CRED());
1427c478bd9Sstevel@tonic-gate 	sigaddqa(p, t, sqp);
1437c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1447c478bd9Sstevel@tonic-gate 	return (0);
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate  * This is the specification of lwp_wait() from the _lwp_wait(2) manual page:
1497c478bd9Sstevel@tonic-gate  *
1507c478bd9Sstevel@tonic-gate  * The lwp_wait() function blocks the current lwp until the lwp specified
1517c478bd9Sstevel@tonic-gate  * by 'lwpid' terminates.  If the specified lwp terminated prior to the call
1527c478bd9Sstevel@tonic-gate  * to lwp_wait(), then lwp_wait() returns immediately.  If 'lwpid' is zero,
1537c478bd9Sstevel@tonic-gate  * then lwp_wait() waits for any undetached lwp in the current process.
1547c478bd9Sstevel@tonic-gate  * If 'lwpid' is not zero, then it must specify an undetached lwp in the
1557c478bd9Sstevel@tonic-gate  * current process.  If 'departed' is not NULL, then it points to a location
1567c478bd9Sstevel@tonic-gate  * where the id of the exited lwp is stored.
1577c478bd9Sstevel@tonic-gate  *
1587c478bd9Sstevel@tonic-gate  * When an lwp exits and there are one or more lwps in the process waiting
1597c478bd9Sstevel@tonic-gate  * for this specific lwp to exit, then one of the waiting lwps is unblocked
1607c478bd9Sstevel@tonic-gate  * and it returns from lwp_wait() successfully.  Any other lwps waiting for
1617c478bd9Sstevel@tonic-gate  * this same lwp to exit are also unblocked, however, they return from
1627c478bd9Sstevel@tonic-gate  * lwp_wait() with the error ESRCH.  If there are no lwps in the process
1637c478bd9Sstevel@tonic-gate  * waiting for this specific lwp to exit but there are one or more lwps
1647c478bd9Sstevel@tonic-gate  * waiting for any lwp to exit, then one of the waiting lwps is unblocked
1657c478bd9Sstevel@tonic-gate  * and it returns from lwp_wait() successfully.
1667c478bd9Sstevel@tonic-gate  *
1677c478bd9Sstevel@tonic-gate  * If an lwp is waiting for any lwp to exit, it blocks until an undetached
1687c478bd9Sstevel@tonic-gate  * lwp for which no other lwp is waiting terminates, at which time it returns
1697c478bd9Sstevel@tonic-gate  * successfully, or until all other lwps in the process are either daemon
1707c478bd9Sstevel@tonic-gate  * lwps or lwps waiting in lwp_wait(), in which case it returns EDEADLK.
1717c478bd9Sstevel@tonic-gate  */
1727c478bd9Sstevel@tonic-gate int
lwp_wait(id_t lwpid,id_t * departed)1737c478bd9Sstevel@tonic-gate lwp_wait(id_t lwpid, id_t *departed)
1747c478bd9Sstevel@tonic-gate {
1757c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
1767c478bd9Sstevel@tonic-gate 	int error = 0;
1777c478bd9Sstevel@tonic-gate 	int daemon = (curthread->t_proc_flag & TP_DAEMON)? 1 : 0;
1787c478bd9Sstevel@tonic-gate 	lwpent_t *target_lep;
1797c478bd9Sstevel@tonic-gate 	lwpdir_t *ldp;
1807c478bd9Sstevel@tonic-gate 	lwpent_t *lep;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	/*
1837c478bd9Sstevel@tonic-gate 	 * lwp_wait() is not supported for the /proc agent lwp.
1847c478bd9Sstevel@tonic-gate 	 */
1857c478bd9Sstevel@tonic-gate 	if (curthread == p->p_agenttp)
1867c478bd9Sstevel@tonic-gate 		return (set_errno(ENOTSUP));
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1897c478bd9Sstevel@tonic-gate 	prbarrier(p);
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	curthread->t_waitfor = lwpid;
1927c478bd9Sstevel@tonic-gate 	p->p_lwpwait++;
1937c478bd9Sstevel@tonic-gate 	p->p_lwpdwait += daemon;
194*c6f039c7SToomas Soome 	target_lep = NULL;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	if (lwpid != 0) {
1977c478bd9Sstevel@tonic-gate 		if ((ldp = lwp_hash_lookup(p, lwpid)) == NULL)
1987c478bd9Sstevel@tonic-gate 			target_lep = NULL;
1997c478bd9Sstevel@tonic-gate 		else {
2007c478bd9Sstevel@tonic-gate 			target_lep = ldp->ld_entry;
2017c478bd9Sstevel@tonic-gate 			target_lep->le_waiters++;
2027c478bd9Sstevel@tonic-gate 			target_lep->le_dwaiters += daemon;
2037c478bd9Sstevel@tonic-gate 		}
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	while (error == 0) {
2077c478bd9Sstevel@tonic-gate 		kthread_t *t;
2087c478bd9Sstevel@tonic-gate 		id_t tid;
2097c478bd9Sstevel@tonic-gate 		int i;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 		if (lwpid != 0) {
2127c478bd9Sstevel@tonic-gate 			/*
2137c478bd9Sstevel@tonic-gate 			 * Look for a specific zombie lwp.
2147c478bd9Sstevel@tonic-gate 			 */
2157c478bd9Sstevel@tonic-gate 			if (target_lep == NULL)
2167c478bd9Sstevel@tonic-gate 				error = ESRCH;
2177c478bd9Sstevel@tonic-gate 			else if ((t = target_lep->le_thread) != NULL) {
2187c478bd9Sstevel@tonic-gate 				if (!(t->t_proc_flag & TP_TWAIT))
2197c478bd9Sstevel@tonic-gate 					error = EINVAL;
2207c478bd9Sstevel@tonic-gate 			} else {
2217c478bd9Sstevel@tonic-gate 				/*
2227c478bd9Sstevel@tonic-gate 				 * We found the zombie we are waiting for.
2237c478bd9Sstevel@tonic-gate 				 */
2247c478bd9Sstevel@tonic-gate 				ASSERT(p->p_zombcnt > 0);
2257c478bd9Sstevel@tonic-gate 				p->p_zombcnt--;
2267c478bd9Sstevel@tonic-gate 				p->p_lwpwait--;
2277c478bd9Sstevel@tonic-gate 				p->p_lwpdwait -= daemon;
2287c478bd9Sstevel@tonic-gate 				curthread->t_waitfor = -1;
2297c478bd9Sstevel@tonic-gate 				lwp_hash_out(p, lwpid);
2307c478bd9Sstevel@tonic-gate 				mutex_exit(&p->p_lock);
2317c478bd9Sstevel@tonic-gate 				if (departed != NULL &&
2327c478bd9Sstevel@tonic-gate 				    copyout(&lwpid, departed, sizeof (id_t)))
2337c478bd9Sstevel@tonic-gate 					return (set_errno(EFAULT));
2347c478bd9Sstevel@tonic-gate 				return (0);
2357c478bd9Sstevel@tonic-gate 			}
2367c478bd9Sstevel@tonic-gate 		} else {
2377c478bd9Sstevel@tonic-gate 			/*
2387c478bd9Sstevel@tonic-gate 			 * Look for any zombie lwp.
2397c478bd9Sstevel@tonic-gate 			 */
2407c478bd9Sstevel@tonic-gate 			int some_non_daemon_will_return = 0;
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 			/* for each entry in the lwp directory... */
2437c478bd9Sstevel@tonic-gate 			ldp = p->p_lwpdir;
2447c478bd9Sstevel@tonic-gate 			for (i = 0; i < p->p_lwpdir_sz; i++, ldp++) {
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 				if ((lep = ldp->ld_entry) == NULL ||
2477c478bd9Sstevel@tonic-gate 				    lep->le_thread != NULL)
2487c478bd9Sstevel@tonic-gate 					continue;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 				/*
2517c478bd9Sstevel@tonic-gate 				 * We found a zombie lwp.  If there is some
2527c478bd9Sstevel@tonic-gate 				 * other thread waiting specifically for the
2537c478bd9Sstevel@tonic-gate 				 * zombie we just found, then defer to the other
2547c478bd9Sstevel@tonic-gate 				 * waiting thread and continue searching for
2557c478bd9Sstevel@tonic-gate 				 * another zombie.  Also check to see if there
2567c478bd9Sstevel@tonic-gate 				 * is some non-daemon thread sleeping here in
2577c478bd9Sstevel@tonic-gate 				 * lwp_wait() that will succeed and return when
2587c478bd9Sstevel@tonic-gate 				 * we drop p->p_lock.  This is tested below.
2597c478bd9Sstevel@tonic-gate 				 */
2607c478bd9Sstevel@tonic-gate 				tid = lep->le_lwpid;
2617c478bd9Sstevel@tonic-gate 				if (lep->le_waiters != 0) {
2627c478bd9Sstevel@tonic-gate 					if (lep->le_waiters - lep->le_dwaiters)
2637c478bd9Sstevel@tonic-gate 						some_non_daemon_will_return = 1;
2647c478bd9Sstevel@tonic-gate 					continue;
2657c478bd9Sstevel@tonic-gate 				}
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 				/*
2687c478bd9Sstevel@tonic-gate 				 * We found a zombie that no one else
2697c478bd9Sstevel@tonic-gate 				 * is specifically waiting for.
2707c478bd9Sstevel@tonic-gate 				 */
2717c478bd9Sstevel@tonic-gate 				ASSERT(p->p_zombcnt > 0);
2727c478bd9Sstevel@tonic-gate 				p->p_zombcnt--;
2737c478bd9Sstevel@tonic-gate 				p->p_lwpwait--;
2747c478bd9Sstevel@tonic-gate 				p->p_lwpdwait -= daemon;
2757c478bd9Sstevel@tonic-gate 				curthread->t_waitfor = -1;
2767c478bd9Sstevel@tonic-gate 				lwp_hash_out(p, tid);
2777c478bd9Sstevel@tonic-gate 				mutex_exit(&p->p_lock);
2787c478bd9Sstevel@tonic-gate 				if (departed != NULL &&
2797c478bd9Sstevel@tonic-gate 				    copyout(&tid, departed, sizeof (id_t)))
2807c478bd9Sstevel@tonic-gate 					return (set_errno(EFAULT));
2817c478bd9Sstevel@tonic-gate 				return (0);
2827c478bd9Sstevel@tonic-gate 			}
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 			/*
2857c478bd9Sstevel@tonic-gate 			 * We are waiting for anyone.  If all non-daemon lwps
2867c478bd9Sstevel@tonic-gate 			 * are waiting here, and if we determined above that
2877c478bd9Sstevel@tonic-gate 			 * no non-daemon lwp will return, we have deadlock.
2887c478bd9Sstevel@tonic-gate 			 */
2897c478bd9Sstevel@tonic-gate 			if (!some_non_daemon_will_return &&
2907c478bd9Sstevel@tonic-gate 			    p->p_lwpcnt == p->p_lwpdaemon +
2917c478bd9Sstevel@tonic-gate 			    (p->p_lwpwait - p->p_lwpdwait))
2927c478bd9Sstevel@tonic-gate 				error = EDEADLK;
2937c478bd9Sstevel@tonic-gate 		}
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 		if (error == 0 && lwpid != 0) {
2967c478bd9Sstevel@tonic-gate 			/*
2977c478bd9Sstevel@tonic-gate 			 * We are waiting for a specific non-zombie lwp.
2987c478bd9Sstevel@tonic-gate 			 * Fail if there is a deadlock loop.
2997c478bd9Sstevel@tonic-gate 			 */
3007c478bd9Sstevel@tonic-gate 			for (;;) {
3017c478bd9Sstevel@tonic-gate 				if (t == curthread) {
3027c478bd9Sstevel@tonic-gate 					error = EDEADLK;
3037c478bd9Sstevel@tonic-gate 					break;
3047c478bd9Sstevel@tonic-gate 				}
30548bbca81SDaniel Hoffman 				/* who are they waiting for? */
3067c478bd9Sstevel@tonic-gate 				if ((tid = t->t_waitfor) == -1)
3077c478bd9Sstevel@tonic-gate 					break;
3087c478bd9Sstevel@tonic-gate 				if (tid == 0) {
3097c478bd9Sstevel@tonic-gate 					/*
3107c478bd9Sstevel@tonic-gate 					 * The lwp we are waiting for is
3117c478bd9Sstevel@tonic-gate 					 * waiting for anyone (transitively).
3127c478bd9Sstevel@tonic-gate 					 * If there are no zombies right now
3137c478bd9Sstevel@tonic-gate 					 * and if we would have deadlock due
3147c478bd9Sstevel@tonic-gate 					 * to all non-daemon lwps waiting here,
3157c478bd9Sstevel@tonic-gate 					 * wake up the lwp that is waiting for
3167c478bd9Sstevel@tonic-gate 					 * anyone so it can return EDEADLK.
3177c478bd9Sstevel@tonic-gate 					 */
3187c478bd9Sstevel@tonic-gate 					if (p->p_zombcnt == 0 &&
3197c478bd9Sstevel@tonic-gate 					    p->p_lwpcnt == p->p_lwpdaemon +
3207c478bd9Sstevel@tonic-gate 					    p->p_lwpwait - p->p_lwpdwait)
3217c478bd9Sstevel@tonic-gate 						cv_broadcast(&p->p_lwpexit);
3227c478bd9Sstevel@tonic-gate 					break;
3237c478bd9Sstevel@tonic-gate 				}
3247c478bd9Sstevel@tonic-gate 				if ((ldp = lwp_hash_lookup(p, tid)) == NULL ||
3257c478bd9Sstevel@tonic-gate 				    (t = ldp->ld_entry->le_thread) == NULL)
3267c478bd9Sstevel@tonic-gate 					break;
3277c478bd9Sstevel@tonic-gate 			}
3287c478bd9Sstevel@tonic-gate 		}
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 		if (error)
3317c478bd9Sstevel@tonic-gate 			break;
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 		/*
3347c478bd9Sstevel@tonic-gate 		 * Wait for some lwp to terminate.
3357c478bd9Sstevel@tonic-gate 		 */
3367c478bd9Sstevel@tonic-gate 		if (!cv_wait_sig(&p->p_lwpexit, &p->p_lock))
3377c478bd9Sstevel@tonic-gate 			error = EINTR;
3387c478bd9Sstevel@tonic-gate 		prbarrier(p);
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 		if (lwpid != 0) {
3417c478bd9Sstevel@tonic-gate 			if ((ldp = lwp_hash_lookup(p, lwpid)) == NULL)
3427c478bd9Sstevel@tonic-gate 				target_lep = NULL;
3437c478bd9Sstevel@tonic-gate 			else
3447c478bd9Sstevel@tonic-gate 				target_lep = ldp->ld_entry;
3457c478bd9Sstevel@tonic-gate 		}
3467c478bd9Sstevel@tonic-gate 	}
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	if (lwpid != 0 && target_lep != NULL) {
3497c478bd9Sstevel@tonic-gate 		target_lep->le_waiters--;
3507c478bd9Sstevel@tonic-gate 		target_lep->le_dwaiters -= daemon;
3517c478bd9Sstevel@tonic-gate 	}
3527c478bd9Sstevel@tonic-gate 	p->p_lwpwait--;
3537c478bd9Sstevel@tonic-gate 	p->p_lwpdwait -= daemon;
3547c478bd9Sstevel@tonic-gate 	curthread->t_waitfor = -1;
3557c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
3567c478bd9Sstevel@tonic-gate 	return (set_errno(error));
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate int
lwp_detach(id_t lwpid)3607c478bd9Sstevel@tonic-gate lwp_detach(id_t lwpid)
3617c478bd9Sstevel@tonic-gate {
3627c478bd9Sstevel@tonic-gate 	kthread_t *t;
3637c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
3647c478bd9Sstevel@tonic-gate 	lwpdir_t *ldp;
3657c478bd9Sstevel@tonic-gate 	int error = 0;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
3687c478bd9Sstevel@tonic-gate 	prbarrier(p);
3697c478bd9Sstevel@tonic-gate 	if ((ldp = lwp_hash_lookup(p, lwpid)) == NULL)
3707c478bd9Sstevel@tonic-gate 		error = ESRCH;
3717c478bd9Sstevel@tonic-gate 	else if ((t = ldp->ld_entry->le_thread) != NULL) {
3727c478bd9Sstevel@tonic-gate 		if (!(t->t_proc_flag & TP_TWAIT))
3737c478bd9Sstevel@tonic-gate 			error = EINVAL;
3747c478bd9Sstevel@tonic-gate 		else {
3757c478bd9Sstevel@tonic-gate 			t->t_proc_flag &= ~TP_TWAIT;
3767c478bd9Sstevel@tonic-gate 			cv_broadcast(&p->p_lwpexit);
3777c478bd9Sstevel@tonic-gate 		}
3787c478bd9Sstevel@tonic-gate 	} else {
3797c478bd9Sstevel@tonic-gate 		ASSERT(p->p_zombcnt > 0);
3807c478bd9Sstevel@tonic-gate 		p->p_zombcnt--;
3817c478bd9Sstevel@tonic-gate 		lwp_hash_out(p, lwpid);
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	if (error)
3867c478bd9Sstevel@tonic-gate 		return (set_errno(error));
3877c478bd9Sstevel@tonic-gate 	return (0);
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate /*
3917c478bd9Sstevel@tonic-gate  * Unpark the specified lwp.
3927c478bd9Sstevel@tonic-gate  */
3937c478bd9Sstevel@tonic-gate static int
lwp_unpark(id_t lwpid)3947c478bd9Sstevel@tonic-gate lwp_unpark(id_t lwpid)
3957c478bd9Sstevel@tonic-gate {
3967c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
3977c478bd9Sstevel@tonic-gate 	kthread_t *t;
3986eb30ec3SRoger A. Faulkner 	kmutex_t *mp;
3997c478bd9Sstevel@tonic-gate 	int error = 0;
4007c478bd9Sstevel@tonic-gate 
4016eb30ec3SRoger A. Faulkner 	if ((t = idtot_and_lock(p, lwpid, &mp)) == NULL) {
4027c478bd9Sstevel@tonic-gate 		error = ESRCH;
4036eb30ec3SRoger A. Faulkner 	} else {
4047c478bd9Sstevel@tonic-gate 		mutex_enter(&t->t_delay_lock);
4057c478bd9Sstevel@tonic-gate 		t->t_unpark = 1;
4067c478bd9Sstevel@tonic-gate 		cv_signal(&t->t_delay_cv);
4077c478bd9Sstevel@tonic-gate 		mutex_exit(&t->t_delay_lock);
4086eb30ec3SRoger A. Faulkner 		mutex_exit(mp);
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 	return (error);
4117c478bd9Sstevel@tonic-gate }
4127c478bd9Sstevel@tonic-gate 
41347eb4d1eSsl /*
41447eb4d1eSsl  * Cancel a previous unpark for the specified lwp.
41547eb4d1eSsl  *
41647eb4d1eSsl  * This interface exists ONLY to support older versions of libthread, which
41747eb4d1eSsl  * called lwp_unpark(self) to force calls to lwp_park(self) to return
41847eb4d1eSsl  * immediately.  These older libthreads required a mechanism to cancel the
41947eb4d1eSsl  * lwp_unpark(self).
42047eb4d1eSsl  *
42147eb4d1eSsl  * libc does not call this interface.  Instead, the sc_park flag in the
42247eb4d1eSsl  * schedctl page is cleared to force calls to lwp_park() to return
42347eb4d1eSsl  * immediately.
42447eb4d1eSsl  */
42547eb4d1eSsl static int
lwp_unpark_cancel(id_t lwpid)42647eb4d1eSsl lwp_unpark_cancel(id_t lwpid)
42747eb4d1eSsl {
42847eb4d1eSsl 	proc_t *p = ttoproc(curthread);
42947eb4d1eSsl 	kthread_t *t;
4306eb30ec3SRoger A. Faulkner 	kmutex_t *mp;
43147eb4d1eSsl 	int error = 0;
43247eb4d1eSsl 
4336eb30ec3SRoger A. Faulkner 	if ((t = idtot_and_lock(p, lwpid, &mp)) == NULL) {
43447eb4d1eSsl 		error = ESRCH;
43547eb4d1eSsl 	} else {
43647eb4d1eSsl 		mutex_enter(&t->t_delay_lock);
43747eb4d1eSsl 		t->t_unpark = 0;
43847eb4d1eSsl 		mutex_exit(&t->t_delay_lock);
4396eb30ec3SRoger A. Faulkner 		mutex_exit(mp);
44047eb4d1eSsl 	}
44147eb4d1eSsl 	return (error);
44247eb4d1eSsl }
44347eb4d1eSsl 
4447c478bd9Sstevel@tonic-gate /*
4457c478bd9Sstevel@tonic-gate  * Sleep until we are set running by lwp_unpark() or until we are
4467c478bd9Sstevel@tonic-gate  * interrupted by a signal or until we exhaust our timeout.
4477c478bd9Sstevel@tonic-gate  * timeoutp is an in/out parameter.  On entry, it contains the relative
4487c478bd9Sstevel@tonic-gate  * time until timeout.  On exit, we copyout the residual time left to it.
4497c478bd9Sstevel@tonic-gate  */
4507c478bd9Sstevel@tonic-gate static int
lwp_park(timespec_t * timeoutp,id_t lwpid)4517c478bd9Sstevel@tonic-gate lwp_park(timespec_t *timeoutp, id_t lwpid)
4527c478bd9Sstevel@tonic-gate {
4537c478bd9Sstevel@tonic-gate 	timespec_t rqtime;
4547c478bd9Sstevel@tonic-gate 	timespec_t rmtime;
4557c478bd9Sstevel@tonic-gate 	timespec_t now;
4567c478bd9Sstevel@tonic-gate 	timespec_t *rqtp = NULL;
4577c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
4583348528fSdm 	int timecheck = 0;
4597c478bd9Sstevel@tonic-gate 	int error = 0;
4607c478bd9Sstevel@tonic-gate 	model_t datamodel = ttoproc(t)->p_model;
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	if (lwpid != 0)		/* unpark the other lwp, if any */
4637c478bd9Sstevel@tonic-gate 		(void) lwp_unpark(lwpid);
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	if (timeoutp) {
4663348528fSdm 		timecheck = timechanged;
4677c478bd9Sstevel@tonic-gate 		gethrestime(&now);
4687c478bd9Sstevel@tonic-gate 		if (datamodel == DATAMODEL_NATIVE) {
4697c478bd9Sstevel@tonic-gate 			if (copyin(timeoutp, &rqtime, sizeof (timespec_t))) {
4707c478bd9Sstevel@tonic-gate 				error = EFAULT;
4717c478bd9Sstevel@tonic-gate 				goto out;
4727c478bd9Sstevel@tonic-gate 			}
4737c478bd9Sstevel@tonic-gate 		} else {
4747c478bd9Sstevel@tonic-gate 			timespec32_t timeout32;
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 			if (copyin(timeoutp, &timeout32, sizeof (timeout32))) {
4777c478bd9Sstevel@tonic-gate 				error = EFAULT;
4787c478bd9Sstevel@tonic-gate 				goto out;
4797c478bd9Sstevel@tonic-gate 			}
4807c478bd9Sstevel@tonic-gate 			TIMESPEC32_TO_TIMESPEC(&rqtime, &timeout32)
4817c478bd9Sstevel@tonic-gate 		}
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 		if (itimerspecfix(&rqtime)) {
4847c478bd9Sstevel@tonic-gate 			error = EINVAL;
4857c478bd9Sstevel@tonic-gate 			goto out;
4867c478bd9Sstevel@tonic-gate 		}
4877c478bd9Sstevel@tonic-gate 		/*
4887c478bd9Sstevel@tonic-gate 		 * Convert the timespec value into absolute time.
4897c478bd9Sstevel@tonic-gate 		 */
4907c478bd9Sstevel@tonic-gate 		timespecadd(&rqtime, &now);
4917c478bd9Sstevel@tonic-gate 		rqtp = &rqtime;
4927c478bd9Sstevel@tonic-gate 	}
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	(void) new_mstate(t, LMS_USER_LOCK);
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	mutex_enter(&t->t_delay_lock);
4977c478bd9Sstevel@tonic-gate 	if (!schedctl_is_park())
4987c478bd9Sstevel@tonic-gate 		error = EINTR;
4997c478bd9Sstevel@tonic-gate 	while (error == 0 && t->t_unpark == 0) {
5007c478bd9Sstevel@tonic-gate 		switch (cv_waituntil_sig(&t->t_delay_cv,
5013348528fSdm 		    &t->t_delay_lock, rqtp, timecheck)) {
5027c478bd9Sstevel@tonic-gate 		case 0:
5037c478bd9Sstevel@tonic-gate 			error = EINTR;
5047c478bd9Sstevel@tonic-gate 			break;
5057c478bd9Sstevel@tonic-gate 		case -1:
5067c478bd9Sstevel@tonic-gate 			error = ETIME;
5077c478bd9Sstevel@tonic-gate 			break;
5087c478bd9Sstevel@tonic-gate 		}
5097c478bd9Sstevel@tonic-gate 	}
5107c478bd9Sstevel@tonic-gate 	t->t_unpark = 0;
5117c478bd9Sstevel@tonic-gate 	mutex_exit(&t->t_delay_lock);
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	if (timeoutp != NULL) {
5147c478bd9Sstevel@tonic-gate 		rmtime.tv_sec = rmtime.tv_nsec = 0;
5157c478bd9Sstevel@tonic-gate 		if (error != ETIME) {
5167c478bd9Sstevel@tonic-gate 			gethrestime(&now);
5177c478bd9Sstevel@tonic-gate 			if ((now.tv_sec < rqtime.tv_sec) ||
5187c478bd9Sstevel@tonic-gate 			    ((now.tv_sec == rqtime.tv_sec) &&
5197c478bd9Sstevel@tonic-gate 			    (now.tv_nsec < rqtime.tv_nsec))) {
5207c478bd9Sstevel@tonic-gate 				rmtime = rqtime;
5217c478bd9Sstevel@tonic-gate 				timespecsub(&rmtime, &now);
5227c478bd9Sstevel@tonic-gate 			}
5237c478bd9Sstevel@tonic-gate 		}
5247c478bd9Sstevel@tonic-gate 		if (datamodel == DATAMODEL_NATIVE) {
5257c478bd9Sstevel@tonic-gate 			if (copyout(&rmtime, timeoutp, sizeof (rmtime)))
5267c478bd9Sstevel@tonic-gate 				error = EFAULT;
5277c478bd9Sstevel@tonic-gate 		} else {
5287c478bd9Sstevel@tonic-gate 			timespec32_t rmtime32;
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 			TIMESPEC_TO_TIMESPEC32(&rmtime32, &rmtime);
5317c478bd9Sstevel@tonic-gate 			if (copyout(&rmtime32, timeoutp, sizeof (rmtime32)))
5327c478bd9Sstevel@tonic-gate 				error = EFAULT;
5337c478bd9Sstevel@tonic-gate 		}
5347c478bd9Sstevel@tonic-gate 	}
5357c478bd9Sstevel@tonic-gate out:
5367c478bd9Sstevel@tonic-gate 	schedctl_unpark();
5377c478bd9Sstevel@tonic-gate 	if (t->t_mstate == LMS_USER_LOCK)
5387c478bd9Sstevel@tonic-gate 		(void) new_mstate(t, LMS_SYSTEM);
5397c478bd9Sstevel@tonic-gate 	return (error);
5407c478bd9Sstevel@tonic-gate }
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate #define	MAXLWPIDS	1024
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate /*
5457c478bd9Sstevel@tonic-gate  * Unpark all of the specified lwps.
5467c478bd9Sstevel@tonic-gate  * Do it in chunks of MAXLWPIDS to avoid allocating too much memory.
5477c478bd9Sstevel@tonic-gate  */
5487c478bd9Sstevel@tonic-gate static int
lwp_unpark_all(id_t * lwpidp,int nids)5497c478bd9Sstevel@tonic-gate lwp_unpark_all(id_t *lwpidp, int nids)
5507c478bd9Sstevel@tonic-gate {
5517c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
5527c478bd9Sstevel@tonic-gate 	kthread_t *t;
5536eb30ec3SRoger A. Faulkner 	kmutex_t *mp;
5547c478bd9Sstevel@tonic-gate 	int error = 0;
5557c478bd9Sstevel@tonic-gate 	id_t *lwpid;
5567c478bd9Sstevel@tonic-gate 	size_t lwpidsz;
5577c478bd9Sstevel@tonic-gate 	int n;
5587c478bd9Sstevel@tonic-gate 	int i;
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	if (nids <= 0)
5617c478bd9Sstevel@tonic-gate 		return (EINVAL);
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	lwpidsz = MIN(nids, MAXLWPIDS) * sizeof (id_t);
5647c478bd9Sstevel@tonic-gate 	lwpid = kmem_alloc(lwpidsz, KM_SLEEP);
5657c478bd9Sstevel@tonic-gate 	while (nids > 0) {
5667c478bd9Sstevel@tonic-gate 		n = MIN(nids, MAXLWPIDS);
5677c478bd9Sstevel@tonic-gate 		if (copyin(lwpidp, lwpid, n * sizeof (id_t))) {
5687c478bd9Sstevel@tonic-gate 			error = EFAULT;
5697c478bd9Sstevel@tonic-gate 			break;
5707c478bd9Sstevel@tonic-gate 		}
5717c478bd9Sstevel@tonic-gate 		for (i = 0; i < n; i++) {
5726eb30ec3SRoger A. Faulkner 			if ((t = idtot_and_lock(p, lwpid[i], &mp)) == NULL) {
5737c478bd9Sstevel@tonic-gate 				error = ESRCH;
5746eb30ec3SRoger A. Faulkner 			} else {
5757c478bd9Sstevel@tonic-gate 				mutex_enter(&t->t_delay_lock);
5767c478bd9Sstevel@tonic-gate 				t->t_unpark = 1;
5777c478bd9Sstevel@tonic-gate 				cv_signal(&t->t_delay_cv);
5787c478bd9Sstevel@tonic-gate 				mutex_exit(&t->t_delay_lock);
5796eb30ec3SRoger A. Faulkner 				mutex_exit(mp);
5807c478bd9Sstevel@tonic-gate 			}
5817c478bd9Sstevel@tonic-gate 		}
5827c478bd9Sstevel@tonic-gate 		lwpidp += n;
5837c478bd9Sstevel@tonic-gate 		nids -= n;
5847c478bd9Sstevel@tonic-gate 	}
5857c478bd9Sstevel@tonic-gate 	kmem_free(lwpid, lwpidsz);
5867c478bd9Sstevel@tonic-gate 	return (error);
5877c478bd9Sstevel@tonic-gate }
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate /*
5907c478bd9Sstevel@tonic-gate  * SYS_lwp_park() system call.
5917c478bd9Sstevel@tonic-gate  */
5927c478bd9Sstevel@tonic-gate int
syslwp_park(int which,uintptr_t arg1,uintptr_t arg2)5937c478bd9Sstevel@tonic-gate syslwp_park(int which, uintptr_t arg1, uintptr_t arg2)
5947c478bd9Sstevel@tonic-gate {
5957c478bd9Sstevel@tonic-gate 	int error;
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	switch (which) {
5987c478bd9Sstevel@tonic-gate 	case 0:
5997c478bd9Sstevel@tonic-gate 		error = lwp_park((timespec_t *)arg1, (id_t)arg2);
6007c478bd9Sstevel@tonic-gate 		break;
6017c478bd9Sstevel@tonic-gate 	case 1:
6027c478bd9Sstevel@tonic-gate 		error = lwp_unpark((id_t)arg1);
6037c478bd9Sstevel@tonic-gate 		break;
6047c478bd9Sstevel@tonic-gate 	case 2:
6057c478bd9Sstevel@tonic-gate 		error = lwp_unpark_all((id_t *)arg1, (int)arg2);
6067c478bd9Sstevel@tonic-gate 		break;
60747eb4d1eSsl 	case 3:
60847eb4d1eSsl 		/*
60947eb4d1eSsl 		 * This subcode is not used by libc.  It exists ONLY to
61047eb4d1eSsl 		 * support older versions of libthread which do not use
61147eb4d1eSsl 		 * the sc_park flag in the schedctl page.
61247eb4d1eSsl 		 *
61347eb4d1eSsl 		 * These versions of libthread need to be modifed or emulated
61447eb4d1eSsl 		 * to change calls to syslwp_park(1, tid, 0) to
61547eb4d1eSsl 		 * syslwp_park(3, tid).
61647eb4d1eSsl 		 */
61747eb4d1eSsl 		error = lwp_unpark_cancel((id_t)arg1);
61847eb4d1eSsl 		break;
61947eb4d1eSsl 	case 4:
62047eb4d1eSsl 		/*
62147eb4d1eSsl 		 * This subcode is not used by libc.  It exists ONLY to
62247eb4d1eSsl 		 * support older versions of libthread which do not use
62347eb4d1eSsl 		 * the sc_park flag in the schedctl page.
62447eb4d1eSsl 		 *
62547eb4d1eSsl 		 * These versions of libthread need to be modified or emulated
62647eb4d1eSsl 		 * to change calls to syslwp_park(0, ts, tid) to
62747eb4d1eSsl 		 * syslwp_park(4, ts, tid).
62847eb4d1eSsl 		 */
62947eb4d1eSsl 		schedctl_set_park();
63047eb4d1eSsl 		error = lwp_park((timespec_t *)arg1, (id_t)arg2);
63147eb4d1eSsl 		break;
6327c478bd9Sstevel@tonic-gate 	default:
6337c478bd9Sstevel@tonic-gate 		error = EINVAL;
6347c478bd9Sstevel@tonic-gate 		break;
6357c478bd9Sstevel@tonic-gate 	}
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	if (error)
6387c478bd9Sstevel@tonic-gate 		return (set_errno(error));
6397c478bd9Sstevel@tonic-gate 	return (0);
6407c478bd9Sstevel@tonic-gate }
641