xref: /illumos-gate/usr/src/uts/common/disp/shuttle.c (revision 2d6eb4a5)
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
5f2bd4627Sjohansen  * Common Development and Distribution License (the "License").
6f2bd4627Sjohansen  * 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*a574db85Sraf 
227c478bd9Sstevel@tonic-gate /*
23*a574db85Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * Routines to support shuttle synchronization objects
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/proc.h>
337c478bd9Sstevel@tonic-gate #include <sys/thread.h>
347c478bd9Sstevel@tonic-gate #include <sys/class.h>
357c478bd9Sstevel@tonic-gate #include <sys/debug.h>
367c478bd9Sstevel@tonic-gate #include <sys/sobject.h>
377c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
38*a574db85Sraf #include <sys/schedctl.h>
397c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
407c478bd9Sstevel@tonic-gate 
41b3383343Smishra static	disp_lock_t	shuttle_lock;	/* lock on shuttle objects */
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  * Place the thread in question on the run q.
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate static void
shuttle_unsleep(kthread_t * t)477c478bd9Sstevel@tonic-gate shuttle_unsleep(kthread_t *t)
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate 	/* Waiting on a shuttle */
527c478bd9Sstevel@tonic-gate 	ASSERT(t->t_wchan0 == (caddr_t)1 && t->t_wchan == NULL);
537c478bd9Sstevel@tonic-gate 	t->t_flag &= ~T_WAKEABLE;
547c478bd9Sstevel@tonic-gate 	t->t_wchan0 = NULL;
557c478bd9Sstevel@tonic-gate 	t->t_sobj_ops = NULL;
567c478bd9Sstevel@tonic-gate 	THREAD_TRANSITION(t);
577c478bd9Sstevel@tonic-gate 	CL_SETRUN(t);
587c478bd9Sstevel@tonic-gate }
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate static kthread_t *
shuttle_owner()617c478bd9Sstevel@tonic-gate shuttle_owner()
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate 	return (NULL);
647c478bd9Sstevel@tonic-gate }
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /*ARGSUSED*/
677c478bd9Sstevel@tonic-gate static void
shuttle_change_pri(kthread_t * t,pri_t p,pri_t * t_prip)687c478bd9Sstevel@tonic-gate shuttle_change_pri(kthread_t *t, pri_t p, pri_t *t_prip)
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
717c478bd9Sstevel@tonic-gate 	*t_prip = p;
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate static sobj_ops_t shuttle_sobj_ops = {
757c478bd9Sstevel@tonic-gate 	SOBJ_SHUTTLE, shuttle_owner, shuttle_unsleep, shuttle_change_pri
767c478bd9Sstevel@tonic-gate };
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate  * Mark the current thread as sleeping on a shuttle object, and
807c478bd9Sstevel@tonic-gate  * resume the specified thread. The 't' thread must be marked as ONPROC.
817c478bd9Sstevel@tonic-gate  *
827c478bd9Sstevel@tonic-gate  * No locks other than 'l' should be held at this point.
837c478bd9Sstevel@tonic-gate  */
847c478bd9Sstevel@tonic-gate void
shuttle_resume(kthread_t * t,kmutex_t * l)857c478bd9Sstevel@tonic-gate shuttle_resume(kthread_t *t, kmutex_t *l)
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate 	klwp_t	*lwp = ttolwp(curthread);
887c478bd9Sstevel@tonic-gate 	cpu_t	*cp;
89b3383343Smishra 	disp_lock_t *oldtlp;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	thread_lock(curthread);
927c478bd9Sstevel@tonic-gate 	disp_lock_enter_high(&shuttle_lock);
937c478bd9Sstevel@tonic-gate 	if (lwp != NULL) {
947c478bd9Sstevel@tonic-gate 		lwp->lwp_asleep = 1;			/* /proc */
957c478bd9Sstevel@tonic-gate 		lwp->lwp_sysabort = 0;			/* /proc */
967c478bd9Sstevel@tonic-gate 		lwp->lwp_ru.nvcsw++;
977c478bd9Sstevel@tonic-gate 	}
987c478bd9Sstevel@tonic-gate 	curthread->t_flag |= T_WAKEABLE;
997c478bd9Sstevel@tonic-gate 	curthread->t_sobj_ops = &shuttle_sobj_ops;
1007c478bd9Sstevel@tonic-gate 	/*
1017c478bd9Sstevel@tonic-gate 	 * setting cpu_dispthread before changing thread state
1027c478bd9Sstevel@tonic-gate 	 * so that kernel preemption will be deferred to after swtch_to()
1037c478bd9Sstevel@tonic-gate 	 */
1047c478bd9Sstevel@tonic-gate 	cp = CPU;
1057c478bd9Sstevel@tonic-gate 	cp->cpu_dispthread = t;
1067c478bd9Sstevel@tonic-gate 	cp->cpu_dispatch_pri = DISP_PRIO(t);
1077c478bd9Sstevel@tonic-gate 	/*
1087c478bd9Sstevel@tonic-gate 	 * Set the wchan0 field so that /proc won't just do a setrun
1097c478bd9Sstevel@tonic-gate 	 * on this thread when trying to stop a process. Instead,
1107c478bd9Sstevel@tonic-gate 	 * /proc will mark the thread as VSTOPPED similar to threads
1117c478bd9Sstevel@tonic-gate 	 * that are blocked on user level condition variables.
1127c478bd9Sstevel@tonic-gate 	 */
1137c478bd9Sstevel@tonic-gate 	curthread->t_wchan0 = (caddr_t)1;
1147c478bd9Sstevel@tonic-gate 	CL_INACTIVE(curthread);
1157c478bd9Sstevel@tonic-gate 	DTRACE_SCHED1(wakeup, kthread_t *, t);
1167c478bd9Sstevel@tonic-gate 	DTRACE_SCHED(sleep);
1177c478bd9Sstevel@tonic-gate 	THREAD_SLEEP(curthread, &shuttle_lock);
118b3383343Smishra 	disp_lock_exit_high(&shuttle_lock);
1197c478bd9Sstevel@tonic-gate 
120b3383343Smishra 	/*
121b3383343Smishra 	 * Update ustate records (there is no waitrq obviously)
122b3383343Smishra 	 */
1237c478bd9Sstevel@tonic-gate 	(void) new_mstate(curthread, LMS_SLEEP);
124b3383343Smishra 
125b3383343Smishra 	thread_lock_high(t);
126b3383343Smishra 	oldtlp = t->t_lockp;
127b3383343Smishra 
1287c478bd9Sstevel@tonic-gate 	t->t_flag &= ~T_WAKEABLE;
1297c478bd9Sstevel@tonic-gate 	t->t_wchan0 = NULL;
1307c478bd9Sstevel@tonic-gate 	t->t_sobj_ops = NULL;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	/*
133b3383343Smishra 	 * Make sure we end up on the right CPU if we are dealing with bound
134b3383343Smishra 	 * CPU's or processor partitions.
135b3383343Smishra 	 */
136b3383343Smishra 	if (t->t_bound_cpu != NULL || t->t_cpupart != cp->cpu_part) {
137b3383343Smishra 		aston(t);
138b3383343Smishra 		cp->cpu_runrun = 1;
139b3383343Smishra 	}
140b3383343Smishra 
141b3383343Smishra 	/*
1427c478bd9Sstevel@tonic-gate 	 * We re-assign t_disp_queue and t_lockp of 't' here because
1437c478bd9Sstevel@tonic-gate 	 * 't' could have been preempted.
1447c478bd9Sstevel@tonic-gate 	 */
1457c478bd9Sstevel@tonic-gate 	if (t->t_disp_queue != cp->cpu_disp) {
1467c478bd9Sstevel@tonic-gate 		t->t_disp_queue = cp->cpu_disp;
1477c478bd9Sstevel@tonic-gate 		thread_onproc(t, cp);
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	/*
151b3383343Smishra 	 * We can't call thread_unlock_high() here because t's thread lock
152b3383343Smishra 	 * could have changed by thread_onproc() call above to point to
153b3383343Smishra 	 * CPU->cpu_thread_lock.
1547c478bd9Sstevel@tonic-gate 	 */
155b3383343Smishra 	disp_lock_exit_high(oldtlp);
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	mutex_exit(l);
1587c478bd9Sstevel@tonic-gate 	/*
1597c478bd9Sstevel@tonic-gate 	 * Make sure we didn't receive any important events while
1607c478bd9Sstevel@tonic-gate 	 * we weren't looking
1617c478bd9Sstevel@tonic-gate 	 */
162*a574db85Sraf 	if (lwp && (ISSIG(curthread, JUSTLOOKING) ||
163*a574db85Sraf 	    MUSTRETURN(curproc, curthread) || schedctl_cancel_pending()))
1647c478bd9Sstevel@tonic-gate 		setrun(curthread);
165b3383343Smishra 
1667c478bd9Sstevel@tonic-gate 	swtch_to(t);
1677c478bd9Sstevel@tonic-gate 	/*
1687c478bd9Sstevel@tonic-gate 	 * Caller must check for ISSIG/lwp_sysabort conditions
1697c478bd9Sstevel@tonic-gate 	 * and clear lwp->lwp_asleep/lwp->lwp_sysabort
1707c478bd9Sstevel@tonic-gate 	 */
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate /*
1747c478bd9Sstevel@tonic-gate  * Mark the current thread as sleeping on a shuttle object, and
1757c478bd9Sstevel@tonic-gate  * switch to a new thread.
1767c478bd9Sstevel@tonic-gate  * No locks other than 'l' should be held at this point.
1777c478bd9Sstevel@tonic-gate  */
1787c478bd9Sstevel@tonic-gate void
shuttle_swtch(kmutex_t * l)1797c478bd9Sstevel@tonic-gate shuttle_swtch(kmutex_t *l)
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate 	klwp_t	*lwp = ttolwp(curthread);
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	thread_lock(curthread);
1847c478bd9Sstevel@tonic-gate 	disp_lock_enter_high(&shuttle_lock);
1857c478bd9Sstevel@tonic-gate 	lwp->lwp_asleep = 1;			/* /proc */
1867c478bd9Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;			/* /proc */
1877c478bd9Sstevel@tonic-gate 	lwp->lwp_ru.nvcsw++;
1887c478bd9Sstevel@tonic-gate 	curthread->t_flag |= T_WAKEABLE;
1897c478bd9Sstevel@tonic-gate 	curthread->t_sobj_ops = &shuttle_sobj_ops;
1907c478bd9Sstevel@tonic-gate 	curthread->t_wchan0 = (caddr_t)1;
1917c478bd9Sstevel@tonic-gate 	CL_INACTIVE(curthread);
1927c478bd9Sstevel@tonic-gate 	DTRACE_SCHED(sleep);
1937c478bd9Sstevel@tonic-gate 	THREAD_SLEEP(curthread, &shuttle_lock);
1947c478bd9Sstevel@tonic-gate 	(void) new_mstate(curthread, LMS_SLEEP);
1957c478bd9Sstevel@tonic-gate 	disp_lock_exit_high(&shuttle_lock);
1967c478bd9Sstevel@tonic-gate 	mutex_exit(l);
197*a574db85Sraf 	if (ISSIG(curthread, JUSTLOOKING) ||
198*a574db85Sraf 	    MUSTRETURN(curproc, curthread) || schedctl_cancel_pending())
1997c478bd9Sstevel@tonic-gate 		setrun(curthread);
2007c478bd9Sstevel@tonic-gate 	swtch();
2017c478bd9Sstevel@tonic-gate 	/*
2027c478bd9Sstevel@tonic-gate 	 * Caller must check for ISSIG/lwp_sysabort conditions
2037c478bd9Sstevel@tonic-gate 	 * and clear lwp->lwp_asleep/lwp->lwp_sysabort
2047c478bd9Sstevel@tonic-gate 	 */
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate /*
2087c478bd9Sstevel@tonic-gate  * Mark the specified thread as once again sleeping on a shuttle object.  This
2097c478bd9Sstevel@tonic-gate  * routine is called to put a server thread -- one that was dequeued but for
2107c478bd9Sstevel@tonic-gate  * which shuttle_resume() was _not_ called -- back to sleep on a shuttle
2117c478bd9Sstevel@tonic-gate  * object.  Because we don't hit the sched:::wakeup DTrace probe until
2127c478bd9Sstevel@tonic-gate  * shuttle_resume(), we do _not_ have a sched:::sleep probe here.
2137c478bd9Sstevel@tonic-gate  */
2147c478bd9Sstevel@tonic-gate void
shuttle_sleep(kthread_t * t)2157c478bd9Sstevel@tonic-gate shuttle_sleep(kthread_t *t)
2167c478bd9Sstevel@tonic-gate {
2177c478bd9Sstevel@tonic-gate 	klwp_t	*lwp = ttolwp(t);
2187c478bd9Sstevel@tonic-gate 	proc_t	*p = ttoproc(t);
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	thread_lock(t);
2217c478bd9Sstevel@tonic-gate 	disp_lock_enter_high(&shuttle_lock);
2227c478bd9Sstevel@tonic-gate 	if (lwp != NULL) {
2237c478bd9Sstevel@tonic-gate 		lwp->lwp_asleep = 1;			/* /proc */
2247c478bd9Sstevel@tonic-gate 		lwp->lwp_sysabort = 0;			/* /proc */
2257c478bd9Sstevel@tonic-gate 		lwp->lwp_ru.nvcsw++;
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate 	t->t_flag |= T_WAKEABLE;
2287c478bd9Sstevel@tonic-gate 	t->t_sobj_ops = &shuttle_sobj_ops;
2297c478bd9Sstevel@tonic-gate 	t->t_wchan0 = (caddr_t)1;
2307c478bd9Sstevel@tonic-gate 	CL_INACTIVE(t);
231eda89462Sesolom 	ASSERT(t->t_mstate == LMS_SLEEP);
2327c478bd9Sstevel@tonic-gate 	THREAD_SLEEP(t, &shuttle_lock);
2337c478bd9Sstevel@tonic-gate 	disp_lock_exit_high(&shuttle_lock);
2347c478bd9Sstevel@tonic-gate 	if (lwp && (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t)))
2357c478bd9Sstevel@tonic-gate 		setrun(t);
2367c478bd9Sstevel@tonic-gate }
237