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 #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>
40*a574db85Sraf #include <sys/schedctl.h>
417c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
427c478bd9Sstevel@tonic-gate
43b3383343Smishra static disp_lock_t shuttle_lock; /* lock on shuttle objects */
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate * Place the thread in question on the run q.
477c478bd9Sstevel@tonic-gate */
487c478bd9Sstevel@tonic-gate static void
shuttle_unsleep(kthread_t * t)497c478bd9Sstevel@tonic-gate shuttle_unsleep(kthread_t *t)
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(t));
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate /* Waiting on a shuttle */
547c478bd9Sstevel@tonic-gate ASSERT(t->t_wchan0 == (caddr_t)1 && t->t_wchan == NULL);
557c478bd9Sstevel@tonic-gate t->t_flag &= ~T_WAKEABLE;
567c478bd9Sstevel@tonic-gate t->t_wchan0 = NULL;
577c478bd9Sstevel@tonic-gate t->t_sobj_ops = NULL;
587c478bd9Sstevel@tonic-gate THREAD_TRANSITION(t);
597c478bd9Sstevel@tonic-gate CL_SETRUN(t);
607c478bd9Sstevel@tonic-gate }
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate static kthread_t *
shuttle_owner()637c478bd9Sstevel@tonic-gate shuttle_owner()
647c478bd9Sstevel@tonic-gate {
657c478bd9Sstevel@tonic-gate return (NULL);
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate /*ARGSUSED*/
697c478bd9Sstevel@tonic-gate static void
shuttle_change_pri(kthread_t * t,pri_t p,pri_t * t_prip)707c478bd9Sstevel@tonic-gate shuttle_change_pri(kthread_t *t, pri_t p, pri_t *t_prip)
717c478bd9Sstevel@tonic-gate {
727c478bd9Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(t));
737c478bd9Sstevel@tonic-gate *t_prip = p;
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate static sobj_ops_t shuttle_sobj_ops = {
777c478bd9Sstevel@tonic-gate SOBJ_SHUTTLE, shuttle_owner, shuttle_unsleep, shuttle_change_pri
787c478bd9Sstevel@tonic-gate };
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate * Mark the current thread as sleeping on a shuttle object, and
827c478bd9Sstevel@tonic-gate * resume the specified thread. The 't' thread must be marked as ONPROC.
837c478bd9Sstevel@tonic-gate *
847c478bd9Sstevel@tonic-gate * No locks other than 'l' should be held at this point.
857c478bd9Sstevel@tonic-gate */
867c478bd9Sstevel@tonic-gate void
shuttle_resume(kthread_t * t,kmutex_t * l)877c478bd9Sstevel@tonic-gate shuttle_resume(kthread_t *t, kmutex_t *l)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread);
907c478bd9Sstevel@tonic-gate cpu_t *cp;
91b3383343Smishra disp_lock_t *oldtlp;
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate thread_lock(curthread);
947c478bd9Sstevel@tonic-gate disp_lock_enter_high(&shuttle_lock);
957c478bd9Sstevel@tonic-gate if (lwp != NULL) {
967c478bd9Sstevel@tonic-gate lwp->lwp_asleep = 1; /* /proc */
977c478bd9Sstevel@tonic-gate lwp->lwp_sysabort = 0; /* /proc */
987c478bd9Sstevel@tonic-gate lwp->lwp_ru.nvcsw++;
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate curthread->t_flag |= T_WAKEABLE;
1017c478bd9Sstevel@tonic-gate curthread->t_sobj_ops = &shuttle_sobj_ops;
1027c478bd9Sstevel@tonic-gate /*
1037c478bd9Sstevel@tonic-gate * setting cpu_dispthread before changing thread state
1047c478bd9Sstevel@tonic-gate * so that kernel preemption will be deferred to after swtch_to()
1057c478bd9Sstevel@tonic-gate */
1067c478bd9Sstevel@tonic-gate cp = CPU;
1077c478bd9Sstevel@tonic-gate cp->cpu_dispthread = t;
1087c478bd9Sstevel@tonic-gate cp->cpu_dispatch_pri = DISP_PRIO(t);
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate * Set the wchan0 field so that /proc won't just do a setrun
1117c478bd9Sstevel@tonic-gate * on this thread when trying to stop a process. Instead,
1127c478bd9Sstevel@tonic-gate * /proc will mark the thread as VSTOPPED similar to threads
1137c478bd9Sstevel@tonic-gate * that are blocked on user level condition variables.
1147c478bd9Sstevel@tonic-gate */
1157c478bd9Sstevel@tonic-gate curthread->t_wchan0 = (caddr_t)1;
1167c478bd9Sstevel@tonic-gate CL_INACTIVE(curthread);
1177c478bd9Sstevel@tonic-gate DTRACE_SCHED1(wakeup, kthread_t *, t);
1187c478bd9Sstevel@tonic-gate DTRACE_SCHED(sleep);
1197c478bd9Sstevel@tonic-gate THREAD_SLEEP(curthread, &shuttle_lock);
120b3383343Smishra disp_lock_exit_high(&shuttle_lock);
1217c478bd9Sstevel@tonic-gate
122b3383343Smishra /*
123b3383343Smishra * Update ustate records (there is no waitrq obviously)
124b3383343Smishra */
1257c478bd9Sstevel@tonic-gate (void) new_mstate(curthread, LMS_SLEEP);
126b3383343Smishra
127b3383343Smishra thread_lock_high(t);
128b3383343Smishra oldtlp = t->t_lockp;
129b3383343Smishra
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 /*
135b3383343Smishra * Make sure we end up on the right CPU if we are dealing with bound
136b3383343Smishra * CPU's or processor partitions.
137b3383343Smishra */
138b3383343Smishra if (t->t_bound_cpu != NULL || t->t_cpupart != cp->cpu_part) {
139b3383343Smishra aston(t);
140b3383343Smishra cp->cpu_runrun = 1;
141b3383343Smishra }
142b3383343Smishra
143b3383343Smishra /*
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 /*
153b3383343Smishra * We can't call thread_unlock_high() here because t's thread lock
154b3383343Smishra * could have changed by thread_onproc() call above to point to
155b3383343Smishra * CPU->cpu_thread_lock.
1567c478bd9Sstevel@tonic-gate */
157b3383343Smishra 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 */
164*a574db85Sraf if (lwp && (ISSIG(curthread, JUSTLOOKING) ||
165*a574db85Sraf MUSTRETURN(curproc, curthread) || schedctl_cancel_pending()))
1667c478bd9Sstevel@tonic-gate setrun(curthread);
167b3383343Smishra
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
shuttle_swtch(kmutex_t * l)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);
199*a574db85Sraf if (ISSIG(curthread, JUSTLOOKING) ||
200*a574db85Sraf MUSTRETURN(curproc, curthread) || schedctl_cancel_pending())
2017c478bd9Sstevel@tonic-gate setrun(curthread);
2027c478bd9Sstevel@tonic-gate swtch();
2037c478bd9Sstevel@tonic-gate /*
2047c478bd9Sstevel@tonic-gate * Caller must check for ISSIG/lwp_sysabort conditions
2057c478bd9Sstevel@tonic-gate * and clear lwp->lwp_asleep/lwp->lwp_sysabort
2067c478bd9Sstevel@tonic-gate */
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate /*
2107c478bd9Sstevel@tonic-gate * Mark the specified thread as once again sleeping on a shuttle object. This
2117c478bd9Sstevel@tonic-gate * routine is called to put a server thread -- one that was dequeued but for
2127c478bd9Sstevel@tonic-gate * which shuttle_resume() was _not_ called -- back to sleep on a shuttle
2137c478bd9Sstevel@tonic-gate * object. Because we don't hit the sched:::wakeup DTrace probe until
2147c478bd9Sstevel@tonic-gate * shuttle_resume(), we do _not_ have a sched:::sleep probe here.
2157c478bd9Sstevel@tonic-gate */
2167c478bd9Sstevel@tonic-gate void
shuttle_sleep(kthread_t * t)2177c478bd9Sstevel@tonic-gate shuttle_sleep(kthread_t *t)
2187c478bd9Sstevel@tonic-gate {
2197c478bd9Sstevel@tonic-gate klwp_t *lwp = ttolwp(t);
2207c478bd9Sstevel@tonic-gate proc_t *p = ttoproc(t);
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate thread_lock(t);
2237c478bd9Sstevel@tonic-gate disp_lock_enter_high(&shuttle_lock);
2247c478bd9Sstevel@tonic-gate if (lwp != NULL) {
2257c478bd9Sstevel@tonic-gate lwp->lwp_asleep = 1; /* /proc */
2267c478bd9Sstevel@tonic-gate lwp->lwp_sysabort = 0; /* /proc */
2277c478bd9Sstevel@tonic-gate lwp->lwp_ru.nvcsw++;
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate t->t_flag |= T_WAKEABLE;
2307c478bd9Sstevel@tonic-gate t->t_sobj_ops = &shuttle_sobj_ops;
2317c478bd9Sstevel@tonic-gate t->t_wchan0 = (caddr_t)1;
2327c478bd9Sstevel@tonic-gate CL_INACTIVE(t);
233eda89462Sesolom ASSERT(t->t_mstate == LMS_SLEEP);
2347c478bd9Sstevel@tonic-gate THREAD_SLEEP(t, &shuttle_lock);
2357c478bd9Sstevel@tonic-gate disp_lock_exit_high(&shuttle_lock);
2367c478bd9Sstevel@tonic-gate if (lwp && (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t)))
2377c478bd9Sstevel@tonic-gate setrun(t);
2387c478bd9Sstevel@tonic-gate }
239