xref: /illumos-gate/usr/src/uts/common/disp/shuttle.c (revision b3383343)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*b3383343Smishra  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Routines to support shuttle synchronization objects
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <sys/proc.h>
357c478bd9Sstevel@tonic-gate #include <sys/thread.h>
367c478bd9Sstevel@tonic-gate #include <sys/class.h>
377c478bd9Sstevel@tonic-gate #include <sys/debug.h>
387c478bd9Sstevel@tonic-gate #include <sys/sobject.h>
397c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
407c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
417c478bd9Sstevel@tonic-gate 
42*b3383343Smishra static	disp_lock_t	shuttle_lock;	/* lock on shuttle objects */
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate  * Place the thread in question on the run q.
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate static void
487c478bd9Sstevel@tonic-gate shuttle_unsleep(kthread_t *t)
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	/* Waiting on a shuttle */
537c478bd9Sstevel@tonic-gate 	ASSERT(t->t_wchan0 == (caddr_t)1 && t->t_wchan == NULL);
547c478bd9Sstevel@tonic-gate 	t->t_flag &= ~T_WAKEABLE;
557c478bd9Sstevel@tonic-gate 	t->t_wchan0 = NULL;
567c478bd9Sstevel@tonic-gate 	t->t_sobj_ops = NULL;
577c478bd9Sstevel@tonic-gate 	THREAD_TRANSITION(t);
587c478bd9Sstevel@tonic-gate 	CL_SETRUN(t);
597c478bd9Sstevel@tonic-gate }
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate static kthread_t *
627c478bd9Sstevel@tonic-gate shuttle_owner()
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate 	return (NULL);
657c478bd9Sstevel@tonic-gate }
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*ARGSUSED*/
687c478bd9Sstevel@tonic-gate static void
697c478bd9Sstevel@tonic-gate shuttle_change_pri(kthread_t *t, pri_t p, pri_t *t_prip)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
727c478bd9Sstevel@tonic-gate 	*t_prip = p;
737c478bd9Sstevel@tonic-gate }
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate static sobj_ops_t shuttle_sobj_ops = {
767c478bd9Sstevel@tonic-gate 	SOBJ_SHUTTLE, shuttle_owner, shuttle_unsleep, shuttle_change_pri
777c478bd9Sstevel@tonic-gate };
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate  * Mark the current thread as sleeping on a shuttle object, and
817c478bd9Sstevel@tonic-gate  * resume the specified thread. The 't' thread must be marked as ONPROC.
827c478bd9Sstevel@tonic-gate  *
837c478bd9Sstevel@tonic-gate  * No locks other than 'l' should be held at this point.
847c478bd9Sstevel@tonic-gate  */
857c478bd9Sstevel@tonic-gate void
867c478bd9Sstevel@tonic-gate shuttle_resume(kthread_t *t, kmutex_t *l)
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate 	klwp_t	*lwp = ttolwp(curthread);
897c478bd9Sstevel@tonic-gate 	cpu_t	*cp;
90*b3383343Smishra 	disp_lock_t *oldtlp;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	thread_lock(curthread);
937c478bd9Sstevel@tonic-gate 	disp_lock_enter_high(&shuttle_lock);
947c478bd9Sstevel@tonic-gate 	if (lwp != NULL) {
957c478bd9Sstevel@tonic-gate 		lwp->lwp_asleep = 1;			/* /proc */
967c478bd9Sstevel@tonic-gate 		lwp->lwp_sysabort = 0;			/* /proc */
977c478bd9Sstevel@tonic-gate 		lwp->lwp_ru.nvcsw++;
987c478bd9Sstevel@tonic-gate 	}
997c478bd9Sstevel@tonic-gate 	curthread->t_flag |= T_WAKEABLE;
1007c478bd9Sstevel@tonic-gate 	curthread->t_sobj_ops = &shuttle_sobj_ops;
1017c478bd9Sstevel@tonic-gate 	/*
1027c478bd9Sstevel@tonic-gate 	 * setting cpu_dispthread before changing thread state
1037c478bd9Sstevel@tonic-gate 	 * so that kernel preemption will be deferred to after swtch_to()
1047c478bd9Sstevel@tonic-gate 	 */
1057c478bd9Sstevel@tonic-gate 	cp = CPU;
1067c478bd9Sstevel@tonic-gate 	cp->cpu_dispthread = t;
1077c478bd9Sstevel@tonic-gate 	cp->cpu_dispatch_pri = DISP_PRIO(t);
1087c478bd9Sstevel@tonic-gate 	/*
1097c478bd9Sstevel@tonic-gate 	 * Set the wchan0 field so that /proc won't just do a setrun
1107c478bd9Sstevel@tonic-gate 	 * on this thread when trying to stop a process. Instead,
1117c478bd9Sstevel@tonic-gate 	 * /proc will mark the thread as VSTOPPED similar to threads
1127c478bd9Sstevel@tonic-gate 	 * that are blocked on user level condition variables.
1137c478bd9Sstevel@tonic-gate 	 */
1147c478bd9Sstevel@tonic-gate 	curthread->t_wchan0 = (caddr_t)1;
1157c478bd9Sstevel@tonic-gate 	CL_INACTIVE(curthread);
1167c478bd9Sstevel@tonic-gate 	DTRACE_SCHED1(wakeup, kthread_t *, t);
1177c478bd9Sstevel@tonic-gate 	DTRACE_SCHED(sleep);
1187c478bd9Sstevel@tonic-gate 	THREAD_SLEEP(curthread, &shuttle_lock);
119*b3383343Smishra 	disp_lock_exit_high(&shuttle_lock);
1207c478bd9Sstevel@tonic-gate 
121*b3383343Smishra 	/*
122*b3383343Smishra 	 * Update ustate records (there is no waitrq obviously)
123*b3383343Smishra 	 */
1247c478bd9Sstevel@tonic-gate 	(void) new_mstate(curthread, LMS_SLEEP);
125*b3383343Smishra 
126*b3383343Smishra 	thread_lock_high(t);
127*b3383343Smishra 	oldtlp = t->t_lockp;
128*b3383343Smishra 
1297c478bd9Sstevel@tonic-gate 	restore_mstate(t);
1307c478bd9Sstevel@tonic-gate 	t->t_flag &= ~T_WAKEABLE;
1317c478bd9Sstevel@tonic-gate 	t->t_wchan0 = NULL;
1327c478bd9Sstevel@tonic-gate 	t->t_sobj_ops = NULL;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	/*
135*b3383343Smishra 	 * Make sure we end up on the right CPU if we are dealing with bound
136*b3383343Smishra 	 * CPU's or processor partitions.
137*b3383343Smishra 	 */
138*b3383343Smishra 	if (t->t_bound_cpu != NULL || t->t_cpupart != cp->cpu_part) {
139*b3383343Smishra 		aston(t);
140*b3383343Smishra 		cp->cpu_runrun = 1;
141*b3383343Smishra 	}
142*b3383343Smishra 
143*b3383343Smishra 	/*
1447c478bd9Sstevel@tonic-gate 	 * We re-assign t_disp_queue and t_lockp of 't' here because
1457c478bd9Sstevel@tonic-gate 	 * 't' could have been preempted.
1467c478bd9Sstevel@tonic-gate 	 */
1477c478bd9Sstevel@tonic-gate 	if (t->t_disp_queue != cp->cpu_disp) {
1487c478bd9Sstevel@tonic-gate 		t->t_disp_queue = cp->cpu_disp;
1497c478bd9Sstevel@tonic-gate 		thread_onproc(t, cp);
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	/*
153*b3383343Smishra 	 * We can't call thread_unlock_high() here because t's thread lock
154*b3383343Smishra 	 * could have changed by thread_onproc() call above to point to
155*b3383343Smishra 	 * CPU->cpu_thread_lock.
1567c478bd9Sstevel@tonic-gate 	 */
157*b3383343Smishra 	disp_lock_exit_high(oldtlp);
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	mutex_exit(l);
1607c478bd9Sstevel@tonic-gate 	/*
1617c478bd9Sstevel@tonic-gate 	 * Make sure we didn't receive any important events while
1627c478bd9Sstevel@tonic-gate 	 * we weren't looking
1637c478bd9Sstevel@tonic-gate 	 */
1647c478bd9Sstevel@tonic-gate 	if (lwp &&
1657c478bd9Sstevel@tonic-gate 	    (ISSIG(curthread, JUSTLOOKING) || MUSTRETURN(curproc, curthread)))
1667c478bd9Sstevel@tonic-gate 		setrun(curthread);
167*b3383343Smishra 
1687c478bd9Sstevel@tonic-gate 	swtch_to(t);
1697c478bd9Sstevel@tonic-gate 	/*
1707c478bd9Sstevel@tonic-gate 	 * Caller must check for ISSIG/lwp_sysabort conditions
1717c478bd9Sstevel@tonic-gate 	 * and clear lwp->lwp_asleep/lwp->lwp_sysabort
1727c478bd9Sstevel@tonic-gate 	 */
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate /*
1767c478bd9Sstevel@tonic-gate  * Mark the current thread as sleeping on a shuttle object, and
1777c478bd9Sstevel@tonic-gate  * switch to a new thread.
1787c478bd9Sstevel@tonic-gate  * No locks other than 'l' should be held at this point.
1797c478bd9Sstevel@tonic-gate  */
1807c478bd9Sstevel@tonic-gate void
1817c478bd9Sstevel@tonic-gate shuttle_swtch(kmutex_t *l)
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	klwp_t	*lwp = ttolwp(curthread);
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	thread_lock(curthread);
1867c478bd9Sstevel@tonic-gate 	disp_lock_enter_high(&shuttle_lock);
1877c478bd9Sstevel@tonic-gate 	lwp->lwp_asleep = 1;			/* /proc */
1887c478bd9Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;			/* /proc */
1897c478bd9Sstevel@tonic-gate 	lwp->lwp_ru.nvcsw++;
1907c478bd9Sstevel@tonic-gate 	curthread->t_flag |= T_WAKEABLE;
1917c478bd9Sstevel@tonic-gate 	curthread->t_sobj_ops = &shuttle_sobj_ops;
1927c478bd9Sstevel@tonic-gate 	curthread->t_wchan0 = (caddr_t)1;
1937c478bd9Sstevel@tonic-gate 	CL_INACTIVE(curthread);
1947c478bd9Sstevel@tonic-gate 	DTRACE_SCHED(sleep);
1957c478bd9Sstevel@tonic-gate 	THREAD_SLEEP(curthread, &shuttle_lock);
1967c478bd9Sstevel@tonic-gate 	(void) new_mstate(curthread, LMS_SLEEP);
1977c478bd9Sstevel@tonic-gate 	disp_lock_exit_high(&shuttle_lock);
1987c478bd9Sstevel@tonic-gate 	mutex_exit(l);
1997c478bd9Sstevel@tonic-gate 	if (ISSIG(curthread, JUSTLOOKING) || MUSTRETURN(curproc, curthread))
2007c478bd9Sstevel@tonic-gate 		setrun(curthread);
2017c478bd9Sstevel@tonic-gate 	swtch();
2027c478bd9Sstevel@tonic-gate 	/*
2037c478bd9Sstevel@tonic-gate 	 * Caller must check for ISSIG/lwp_sysabort conditions
2047c478bd9Sstevel@tonic-gate 	 * and clear lwp->lwp_asleep/lwp->lwp_sysabort
2057c478bd9Sstevel@tonic-gate 	 */
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate /*
2097c478bd9Sstevel@tonic-gate  * Mark the specified thread as once again sleeping on a shuttle object.  This
2107c478bd9Sstevel@tonic-gate  * routine is called to put a server thread -- one that was dequeued but for
2117c478bd9Sstevel@tonic-gate  * which shuttle_resume() was _not_ called -- back to sleep on a shuttle
2127c478bd9Sstevel@tonic-gate  * object.  Because we don't hit the sched:::wakeup DTrace probe until
2137c478bd9Sstevel@tonic-gate  * shuttle_resume(), we do _not_ have a sched:::sleep probe here.
2147c478bd9Sstevel@tonic-gate  */
2157c478bd9Sstevel@tonic-gate void
2167c478bd9Sstevel@tonic-gate shuttle_sleep(kthread_t *t)
2177c478bd9Sstevel@tonic-gate {
2187c478bd9Sstevel@tonic-gate 	klwp_t	*lwp = ttolwp(t);
2197c478bd9Sstevel@tonic-gate 	proc_t	*p = ttoproc(t);
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	thread_lock(t);
2227c478bd9Sstevel@tonic-gate 	disp_lock_enter_high(&shuttle_lock);
2237c478bd9Sstevel@tonic-gate 	if (lwp != NULL) {
2247c478bd9Sstevel@tonic-gate 		lwp->lwp_asleep = 1;			/* /proc */
2257c478bd9Sstevel@tonic-gate 		lwp->lwp_sysabort = 0;			/* /proc */
2267c478bd9Sstevel@tonic-gate 		lwp->lwp_ru.nvcsw++;
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate 	t->t_flag |= T_WAKEABLE;
2297c478bd9Sstevel@tonic-gate 	t->t_sobj_ops = &shuttle_sobj_ops;
2307c478bd9Sstevel@tonic-gate 	t->t_wchan0 = (caddr_t)1;
2317c478bd9Sstevel@tonic-gate 	CL_INACTIVE(t);
2327c478bd9Sstevel@tonic-gate 	(void) new_mstate(t, LMS_SLEEP);
2337c478bd9Sstevel@tonic-gate 	THREAD_SLEEP(t, &shuttle_lock);
2347c478bd9Sstevel@tonic-gate 	disp_lock_exit_high(&shuttle_lock);
2357c478bd9Sstevel@tonic-gate 	if (lwp && (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t)))
2367c478bd9Sstevel@tonic-gate 		setrun(t);
2377c478bd9Sstevel@tonic-gate }
238