10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
53426Sjohansen * Common Development and Distribution License (the "License").
63426Sjohansen * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21*5891Sraf
220Sstevel@tonic-gate /*
23*5891Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate * Routines to support shuttle synchronization objects
310Sstevel@tonic-gate */
320Sstevel@tonic-gate
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <sys/proc.h>
350Sstevel@tonic-gate #include <sys/thread.h>
360Sstevel@tonic-gate #include <sys/class.h>
370Sstevel@tonic-gate #include <sys/debug.h>
380Sstevel@tonic-gate #include <sys/sobject.h>
390Sstevel@tonic-gate #include <sys/cpuvar.h>
40*5891Sraf #include <sys/schedctl.h>
410Sstevel@tonic-gate #include <sys/sdt.h>
420Sstevel@tonic-gate
43477Smishra static disp_lock_t shuttle_lock; /* lock on shuttle objects */
440Sstevel@tonic-gate
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate * Place the thread in question on the run q.
470Sstevel@tonic-gate */
480Sstevel@tonic-gate static void
shuttle_unsleep(kthread_t * t)490Sstevel@tonic-gate shuttle_unsleep(kthread_t *t)
500Sstevel@tonic-gate {
510Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(t));
520Sstevel@tonic-gate
530Sstevel@tonic-gate /* Waiting on a shuttle */
540Sstevel@tonic-gate ASSERT(t->t_wchan0 == (caddr_t)1 && t->t_wchan == NULL);
550Sstevel@tonic-gate t->t_flag &= ~T_WAKEABLE;
560Sstevel@tonic-gate t->t_wchan0 = NULL;
570Sstevel@tonic-gate t->t_sobj_ops = NULL;
580Sstevel@tonic-gate THREAD_TRANSITION(t);
590Sstevel@tonic-gate CL_SETRUN(t);
600Sstevel@tonic-gate }
610Sstevel@tonic-gate
620Sstevel@tonic-gate static kthread_t *
shuttle_owner()630Sstevel@tonic-gate shuttle_owner()
640Sstevel@tonic-gate {
650Sstevel@tonic-gate return (NULL);
660Sstevel@tonic-gate }
670Sstevel@tonic-gate
680Sstevel@tonic-gate /*ARGSUSED*/
690Sstevel@tonic-gate static void
shuttle_change_pri(kthread_t * t,pri_t p,pri_t * t_prip)700Sstevel@tonic-gate shuttle_change_pri(kthread_t *t, pri_t p, pri_t *t_prip)
710Sstevel@tonic-gate {
720Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(t));
730Sstevel@tonic-gate *t_prip = p;
740Sstevel@tonic-gate }
750Sstevel@tonic-gate
760Sstevel@tonic-gate static sobj_ops_t shuttle_sobj_ops = {
770Sstevel@tonic-gate SOBJ_SHUTTLE, shuttle_owner, shuttle_unsleep, shuttle_change_pri
780Sstevel@tonic-gate };
790Sstevel@tonic-gate
800Sstevel@tonic-gate /*
810Sstevel@tonic-gate * Mark the current thread as sleeping on a shuttle object, and
820Sstevel@tonic-gate * resume the specified thread. The 't' thread must be marked as ONPROC.
830Sstevel@tonic-gate *
840Sstevel@tonic-gate * No locks other than 'l' should be held at this point.
850Sstevel@tonic-gate */
860Sstevel@tonic-gate void
shuttle_resume(kthread_t * t,kmutex_t * l)870Sstevel@tonic-gate shuttle_resume(kthread_t *t, kmutex_t *l)
880Sstevel@tonic-gate {
890Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread);
900Sstevel@tonic-gate cpu_t *cp;
91477Smishra disp_lock_t *oldtlp;
920Sstevel@tonic-gate
930Sstevel@tonic-gate thread_lock(curthread);
940Sstevel@tonic-gate disp_lock_enter_high(&shuttle_lock);
950Sstevel@tonic-gate if (lwp != NULL) {
960Sstevel@tonic-gate lwp->lwp_asleep = 1; /* /proc */
970Sstevel@tonic-gate lwp->lwp_sysabort = 0; /* /proc */
980Sstevel@tonic-gate lwp->lwp_ru.nvcsw++;
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate curthread->t_flag |= T_WAKEABLE;
1010Sstevel@tonic-gate curthread->t_sobj_ops = &shuttle_sobj_ops;
1020Sstevel@tonic-gate /*
1030Sstevel@tonic-gate * setting cpu_dispthread before changing thread state
1040Sstevel@tonic-gate * so that kernel preemption will be deferred to after swtch_to()
1050Sstevel@tonic-gate */
1060Sstevel@tonic-gate cp = CPU;
1070Sstevel@tonic-gate cp->cpu_dispthread = t;
1080Sstevel@tonic-gate cp->cpu_dispatch_pri = DISP_PRIO(t);
1090Sstevel@tonic-gate /*
1100Sstevel@tonic-gate * Set the wchan0 field so that /proc won't just do a setrun
1110Sstevel@tonic-gate * on this thread when trying to stop a process. Instead,
1120Sstevel@tonic-gate * /proc will mark the thread as VSTOPPED similar to threads
1130Sstevel@tonic-gate * that are blocked on user level condition variables.
1140Sstevel@tonic-gate */
1150Sstevel@tonic-gate curthread->t_wchan0 = (caddr_t)1;
1160Sstevel@tonic-gate CL_INACTIVE(curthread);
1170Sstevel@tonic-gate DTRACE_SCHED1(wakeup, kthread_t *, t);
1180Sstevel@tonic-gate DTRACE_SCHED(sleep);
1190Sstevel@tonic-gate THREAD_SLEEP(curthread, &shuttle_lock);
120477Smishra disp_lock_exit_high(&shuttle_lock);
1210Sstevel@tonic-gate
122477Smishra /*
123477Smishra * Update ustate records (there is no waitrq obviously)
124477Smishra */
125477Smishra (void) new_mstate(curthread, LMS_SLEEP);
1260Sstevel@tonic-gate
127477Smishra thread_lock_high(t);
128477Smishra oldtlp = t->t_lockp;
129477Smishra
1300Sstevel@tonic-gate t->t_flag &= ~T_WAKEABLE;
1310Sstevel@tonic-gate t->t_wchan0 = NULL;
1320Sstevel@tonic-gate t->t_sobj_ops = NULL;
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate /*
135477Smishra * Make sure we end up on the right CPU if we are dealing with bound
136477Smishra * CPU's or processor partitions.
137477Smishra */
138477Smishra if (t->t_bound_cpu != NULL || t->t_cpupart != cp->cpu_part) {
139477Smishra aston(t);
140477Smishra cp->cpu_runrun = 1;
141477Smishra }
142477Smishra
143477Smishra /*
1440Sstevel@tonic-gate * We re-assign t_disp_queue and t_lockp of 't' here because
1450Sstevel@tonic-gate * 't' could have been preempted.
1460Sstevel@tonic-gate */
1470Sstevel@tonic-gate if (t->t_disp_queue != cp->cpu_disp) {
1480Sstevel@tonic-gate t->t_disp_queue = cp->cpu_disp;
1490Sstevel@tonic-gate thread_onproc(t, cp);
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate /*
153477Smishra * We can't call thread_unlock_high() here because t's thread lock
154477Smishra * could have changed by thread_onproc() call above to point to
155477Smishra * CPU->cpu_thread_lock.
1560Sstevel@tonic-gate */
157477Smishra disp_lock_exit_high(oldtlp);
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate mutex_exit(l);
1600Sstevel@tonic-gate /*
1610Sstevel@tonic-gate * Make sure we didn't receive any important events while
1620Sstevel@tonic-gate * we weren't looking
1630Sstevel@tonic-gate */
164*5891Sraf if (lwp && (ISSIG(curthread, JUSTLOOKING) ||
165*5891Sraf MUSTRETURN(curproc, curthread) || schedctl_cancel_pending()))
1660Sstevel@tonic-gate setrun(curthread);
167477Smishra
1680Sstevel@tonic-gate swtch_to(t);
1690Sstevel@tonic-gate /*
1700Sstevel@tonic-gate * Caller must check for ISSIG/lwp_sysabort conditions
1710Sstevel@tonic-gate * and clear lwp->lwp_asleep/lwp->lwp_sysabort
1720Sstevel@tonic-gate */
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate /*
1760Sstevel@tonic-gate * Mark the current thread as sleeping on a shuttle object, and
1770Sstevel@tonic-gate * switch to a new thread.
1780Sstevel@tonic-gate * No locks other than 'l' should be held at this point.
1790Sstevel@tonic-gate */
1800Sstevel@tonic-gate void
shuttle_swtch(kmutex_t * l)1810Sstevel@tonic-gate shuttle_swtch(kmutex_t *l)
1820Sstevel@tonic-gate {
1830Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread);
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate thread_lock(curthread);
1860Sstevel@tonic-gate disp_lock_enter_high(&shuttle_lock);
1870Sstevel@tonic-gate lwp->lwp_asleep = 1; /* /proc */
1880Sstevel@tonic-gate lwp->lwp_sysabort = 0; /* /proc */
1890Sstevel@tonic-gate lwp->lwp_ru.nvcsw++;
1900Sstevel@tonic-gate curthread->t_flag |= T_WAKEABLE;
1910Sstevel@tonic-gate curthread->t_sobj_ops = &shuttle_sobj_ops;
1920Sstevel@tonic-gate curthread->t_wchan0 = (caddr_t)1;
1930Sstevel@tonic-gate CL_INACTIVE(curthread);
1940Sstevel@tonic-gate DTRACE_SCHED(sleep);
1950Sstevel@tonic-gate THREAD_SLEEP(curthread, &shuttle_lock);
1960Sstevel@tonic-gate (void) new_mstate(curthread, LMS_SLEEP);
1970Sstevel@tonic-gate disp_lock_exit_high(&shuttle_lock);
1980Sstevel@tonic-gate mutex_exit(l);
199*5891Sraf if (ISSIG(curthread, JUSTLOOKING) ||
200*5891Sraf MUSTRETURN(curproc, curthread) || schedctl_cancel_pending())
2010Sstevel@tonic-gate setrun(curthread);
2020Sstevel@tonic-gate swtch();
2030Sstevel@tonic-gate /*
2040Sstevel@tonic-gate * Caller must check for ISSIG/lwp_sysabort conditions
2050Sstevel@tonic-gate * and clear lwp->lwp_asleep/lwp->lwp_sysabort
2060Sstevel@tonic-gate */
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate /*
2100Sstevel@tonic-gate * Mark the specified thread as once again sleeping on a shuttle object. This
2110Sstevel@tonic-gate * routine is called to put a server thread -- one that was dequeued but for
2120Sstevel@tonic-gate * which shuttle_resume() was _not_ called -- back to sleep on a shuttle
2130Sstevel@tonic-gate * object. Because we don't hit the sched:::wakeup DTrace probe until
2140Sstevel@tonic-gate * shuttle_resume(), we do _not_ have a sched:::sleep probe here.
2150Sstevel@tonic-gate */
2160Sstevel@tonic-gate void
shuttle_sleep(kthread_t * t)2170Sstevel@tonic-gate shuttle_sleep(kthread_t *t)
2180Sstevel@tonic-gate {
2190Sstevel@tonic-gate klwp_t *lwp = ttolwp(t);
2200Sstevel@tonic-gate proc_t *p = ttoproc(t);
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate thread_lock(t);
2230Sstevel@tonic-gate disp_lock_enter_high(&shuttle_lock);
2240Sstevel@tonic-gate if (lwp != NULL) {
2250Sstevel@tonic-gate lwp->lwp_asleep = 1; /* /proc */
2260Sstevel@tonic-gate lwp->lwp_sysabort = 0; /* /proc */
2270Sstevel@tonic-gate lwp->lwp_ru.nvcsw++;
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate t->t_flag |= T_WAKEABLE;
2300Sstevel@tonic-gate t->t_sobj_ops = &shuttle_sobj_ops;
2310Sstevel@tonic-gate t->t_wchan0 = (caddr_t)1;
2320Sstevel@tonic-gate CL_INACTIVE(t);
233590Sesolom ASSERT(t->t_mstate == LMS_SLEEP);
2340Sstevel@tonic-gate THREAD_SLEEP(t, &shuttle_lock);
2350Sstevel@tonic-gate disp_lock_exit_high(&shuttle_lock);
2360Sstevel@tonic-gate if (lwp && (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t)))
2370Sstevel@tonic-gate setrun(t);
2380Sstevel@tonic-gate }
239