xref: /titanic_52/usr/src/uts/common/disp/shuttle.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * Routines to support shuttle synchronization objects
31*7c478bd9Sstevel@tonic-gate  */
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/proc.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/thread.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/class.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/sobject.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate /*
44*7c478bd9Sstevel@tonic-gate  * Place the thread in question on the run q.
45*7c478bd9Sstevel@tonic-gate  */
46*7c478bd9Sstevel@tonic-gate static void
47*7c478bd9Sstevel@tonic-gate shuttle_unsleep(kthread_t *t)
48*7c478bd9Sstevel@tonic-gate {
49*7c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate 	/* Waiting on a shuttle */
52*7c478bd9Sstevel@tonic-gate 	ASSERT(t->t_wchan0 == (caddr_t)1 && t->t_wchan == NULL);
53*7c478bd9Sstevel@tonic-gate 	t->t_flag &= ~T_WAKEABLE;
54*7c478bd9Sstevel@tonic-gate 	t->t_wchan0 = NULL;
55*7c478bd9Sstevel@tonic-gate 	t->t_sobj_ops = NULL;
56*7c478bd9Sstevel@tonic-gate 	THREAD_TRANSITION(t);
57*7c478bd9Sstevel@tonic-gate 	CL_SETRUN(t);
58*7c478bd9Sstevel@tonic-gate }
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate static kthread_t *
61*7c478bd9Sstevel@tonic-gate shuttle_owner()
62*7c478bd9Sstevel@tonic-gate {
63*7c478bd9Sstevel@tonic-gate 	return (NULL);
64*7c478bd9Sstevel@tonic-gate }
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
67*7c478bd9Sstevel@tonic-gate static void
68*7c478bd9Sstevel@tonic-gate shuttle_change_pri(kthread_t *t, pri_t p, pri_t *t_prip)
69*7c478bd9Sstevel@tonic-gate {
70*7c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
71*7c478bd9Sstevel@tonic-gate 	*t_prip = p;
72*7c478bd9Sstevel@tonic-gate }
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate static sobj_ops_t shuttle_sobj_ops = {
75*7c478bd9Sstevel@tonic-gate 	SOBJ_SHUTTLE, shuttle_owner, shuttle_unsleep, shuttle_change_pri
76*7c478bd9Sstevel@tonic-gate };
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate /*
79*7c478bd9Sstevel@tonic-gate  * Mark the current thread as sleeping on a shuttle object, and
80*7c478bd9Sstevel@tonic-gate  * resume the specified thread. The 't' thread must be marked as ONPROC.
81*7c478bd9Sstevel@tonic-gate  *
82*7c478bd9Sstevel@tonic-gate  * No locks other than 'l' should be held at this point.
83*7c478bd9Sstevel@tonic-gate  */
84*7c478bd9Sstevel@tonic-gate void
85*7c478bd9Sstevel@tonic-gate shuttle_resume(kthread_t *t, kmutex_t *l)
86*7c478bd9Sstevel@tonic-gate {
87*7c478bd9Sstevel@tonic-gate 	klwp_t	*lwp = ttolwp(curthread);
88*7c478bd9Sstevel@tonic-gate 	cpu_t	*cp;
89*7c478bd9Sstevel@tonic-gate 	extern disp_lock_t	shuttle_lock;
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate 	thread_lock(curthread);
92*7c478bd9Sstevel@tonic-gate 	disp_lock_enter_high(&shuttle_lock);
93*7c478bd9Sstevel@tonic-gate 	if (lwp != NULL) {
94*7c478bd9Sstevel@tonic-gate 		lwp->lwp_asleep = 1;			/* /proc */
95*7c478bd9Sstevel@tonic-gate 		lwp->lwp_sysabort = 0;			/* /proc */
96*7c478bd9Sstevel@tonic-gate 		lwp->lwp_ru.nvcsw++;
97*7c478bd9Sstevel@tonic-gate 	}
98*7c478bd9Sstevel@tonic-gate 	curthread->t_flag |= T_WAKEABLE;
99*7c478bd9Sstevel@tonic-gate 	curthread->t_sobj_ops = &shuttle_sobj_ops;
100*7c478bd9Sstevel@tonic-gate 	/*
101*7c478bd9Sstevel@tonic-gate 	 * setting cpu_dispthread before changing thread state
102*7c478bd9Sstevel@tonic-gate 	 * so that kernel preemption will be deferred to after swtch_to()
103*7c478bd9Sstevel@tonic-gate 	 */
104*7c478bd9Sstevel@tonic-gate 	cp = CPU;
105*7c478bd9Sstevel@tonic-gate 	cp->cpu_dispthread = t;
106*7c478bd9Sstevel@tonic-gate 	cp->cpu_dispatch_pri = DISP_PRIO(t);
107*7c478bd9Sstevel@tonic-gate 	/*
108*7c478bd9Sstevel@tonic-gate 	 * Set the wchan0 field so that /proc won't just do a setrun
109*7c478bd9Sstevel@tonic-gate 	 * on this thread when trying to stop a process. Instead,
110*7c478bd9Sstevel@tonic-gate 	 * /proc will mark the thread as VSTOPPED similar to threads
111*7c478bd9Sstevel@tonic-gate 	 * that are blocked on user level condition variables.
112*7c478bd9Sstevel@tonic-gate 	 */
113*7c478bd9Sstevel@tonic-gate 	curthread->t_wchan0 = (caddr_t)1;
114*7c478bd9Sstevel@tonic-gate 	CL_INACTIVE(curthread);
115*7c478bd9Sstevel@tonic-gate 	DTRACE_SCHED1(wakeup, kthread_t *, t);
116*7c478bd9Sstevel@tonic-gate 	DTRACE_SCHED(sleep);
117*7c478bd9Sstevel@tonic-gate 	THREAD_SLEEP(curthread, &shuttle_lock);
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 	/* Update ustate records (there is no waitrq obviously) */
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 	(void) new_mstate(curthread, LMS_SLEEP);
122*7c478bd9Sstevel@tonic-gate 	restore_mstate(t);
123*7c478bd9Sstevel@tonic-gate 	t->t_flag &= ~T_WAKEABLE;
124*7c478bd9Sstevel@tonic-gate 	t->t_wchan0 = NULL;
125*7c478bd9Sstevel@tonic-gate 	t->t_sobj_ops = NULL;
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 	/*
128*7c478bd9Sstevel@tonic-gate 	 * We re-assign t_disp_queue and t_lockp of 't' here because
129*7c478bd9Sstevel@tonic-gate 	 * 't' could have been preempted.
130*7c478bd9Sstevel@tonic-gate 	 */
131*7c478bd9Sstevel@tonic-gate 	if (t->t_disp_queue != cp->cpu_disp) {
132*7c478bd9Sstevel@tonic-gate 		t->t_disp_queue = cp->cpu_disp;
133*7c478bd9Sstevel@tonic-gate 		thread_onproc(t, cp);
134*7c478bd9Sstevel@tonic-gate 	}
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 	/*
137*7c478bd9Sstevel@tonic-gate 	 * Make sure we end up on the right CPU if we are dealing with bound
138*7c478bd9Sstevel@tonic-gate 	 * CPU's or processor partitions.
139*7c478bd9Sstevel@tonic-gate 	 */
140*7c478bd9Sstevel@tonic-gate 	if (t->t_bound_cpu != NULL || t->t_cpupart != cp->cpu_part) {
141*7c478bd9Sstevel@tonic-gate 		aston(t);
142*7c478bd9Sstevel@tonic-gate 		cp->cpu_runrun = 1;
143*7c478bd9Sstevel@tonic-gate 	}
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 	disp_lock_exit_high(&shuttle_lock);
146*7c478bd9Sstevel@tonic-gate 	mutex_exit(l);
147*7c478bd9Sstevel@tonic-gate 	/*
148*7c478bd9Sstevel@tonic-gate 	 * Make sure we didn't receive any important events while
149*7c478bd9Sstevel@tonic-gate 	 * we weren't looking
150*7c478bd9Sstevel@tonic-gate 	 */
151*7c478bd9Sstevel@tonic-gate 	if (lwp &&
152*7c478bd9Sstevel@tonic-gate 	    (ISSIG(curthread, JUSTLOOKING) || MUSTRETURN(curproc, curthread)))
153*7c478bd9Sstevel@tonic-gate 		setrun(curthread);
154*7c478bd9Sstevel@tonic-gate 	swtch_to(t);
155*7c478bd9Sstevel@tonic-gate 	/*
156*7c478bd9Sstevel@tonic-gate 	 * Caller must check for ISSIG/lwp_sysabort conditions
157*7c478bd9Sstevel@tonic-gate 	 * and clear lwp->lwp_asleep/lwp->lwp_sysabort
158*7c478bd9Sstevel@tonic-gate 	 */
159*7c478bd9Sstevel@tonic-gate }
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate /*
162*7c478bd9Sstevel@tonic-gate  * Mark the current thread as sleeping on a shuttle object, and
163*7c478bd9Sstevel@tonic-gate  * switch to a new thread.
164*7c478bd9Sstevel@tonic-gate  * No locks other than 'l' should be held at this point.
165*7c478bd9Sstevel@tonic-gate  */
166*7c478bd9Sstevel@tonic-gate void
167*7c478bd9Sstevel@tonic-gate shuttle_swtch(kmutex_t *l)
168*7c478bd9Sstevel@tonic-gate {
169*7c478bd9Sstevel@tonic-gate 	klwp_t	*lwp = ttolwp(curthread);
170*7c478bd9Sstevel@tonic-gate 	extern disp_lock_t	shuttle_lock;
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate 	thread_lock(curthread);
173*7c478bd9Sstevel@tonic-gate 	disp_lock_enter_high(&shuttle_lock);
174*7c478bd9Sstevel@tonic-gate 	lwp->lwp_asleep = 1;			/* /proc */
175*7c478bd9Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;			/* /proc */
176*7c478bd9Sstevel@tonic-gate 	lwp->lwp_ru.nvcsw++;
177*7c478bd9Sstevel@tonic-gate 	curthread->t_flag |= T_WAKEABLE;
178*7c478bd9Sstevel@tonic-gate 	curthread->t_sobj_ops = &shuttle_sobj_ops;
179*7c478bd9Sstevel@tonic-gate 	curthread->t_wchan0 = (caddr_t)1;
180*7c478bd9Sstevel@tonic-gate 	CL_INACTIVE(curthread);
181*7c478bd9Sstevel@tonic-gate 	DTRACE_SCHED(sleep);
182*7c478bd9Sstevel@tonic-gate 	THREAD_SLEEP(curthread, &shuttle_lock);
183*7c478bd9Sstevel@tonic-gate 	(void) new_mstate(curthread, LMS_SLEEP);
184*7c478bd9Sstevel@tonic-gate 	disp_lock_exit_high(&shuttle_lock);
185*7c478bd9Sstevel@tonic-gate 	mutex_exit(l);
186*7c478bd9Sstevel@tonic-gate 	if (ISSIG(curthread, JUSTLOOKING) || MUSTRETURN(curproc, curthread))
187*7c478bd9Sstevel@tonic-gate 		setrun(curthread);
188*7c478bd9Sstevel@tonic-gate 	swtch();
189*7c478bd9Sstevel@tonic-gate 	/*
190*7c478bd9Sstevel@tonic-gate 	 * Caller must check for ISSIG/lwp_sysabort conditions
191*7c478bd9Sstevel@tonic-gate 	 * and clear lwp->lwp_asleep/lwp->lwp_sysabort
192*7c478bd9Sstevel@tonic-gate 	 */
193*7c478bd9Sstevel@tonic-gate }
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate /*
196*7c478bd9Sstevel@tonic-gate  * Mark the specified thread as once again sleeping on a shuttle object.  This
197*7c478bd9Sstevel@tonic-gate  * routine is called to put a server thread -- one that was dequeued but for
198*7c478bd9Sstevel@tonic-gate  * which shuttle_resume() was _not_ called -- back to sleep on a shuttle
199*7c478bd9Sstevel@tonic-gate  * object.  Because we don't hit the sched:::wakeup DTrace probe until
200*7c478bd9Sstevel@tonic-gate  * shuttle_resume(), we do _not_ have a sched:::sleep probe here.
201*7c478bd9Sstevel@tonic-gate  */
202*7c478bd9Sstevel@tonic-gate void
203*7c478bd9Sstevel@tonic-gate shuttle_sleep(kthread_t *t)
204*7c478bd9Sstevel@tonic-gate {
205*7c478bd9Sstevel@tonic-gate 	klwp_t	*lwp = ttolwp(t);
206*7c478bd9Sstevel@tonic-gate 	proc_t	*p = ttoproc(t);
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 	extern disp_lock_t	shuttle_lock;
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 	thread_lock(t);
211*7c478bd9Sstevel@tonic-gate 	disp_lock_enter_high(&shuttle_lock);
212*7c478bd9Sstevel@tonic-gate 	if (lwp != NULL) {
213*7c478bd9Sstevel@tonic-gate 		lwp->lwp_asleep = 1;			/* /proc */
214*7c478bd9Sstevel@tonic-gate 		lwp->lwp_sysabort = 0;			/* /proc */
215*7c478bd9Sstevel@tonic-gate 		lwp->lwp_ru.nvcsw++;
216*7c478bd9Sstevel@tonic-gate 	}
217*7c478bd9Sstevel@tonic-gate 	t->t_flag |= T_WAKEABLE;
218*7c478bd9Sstevel@tonic-gate 	t->t_sobj_ops = &shuttle_sobj_ops;
219*7c478bd9Sstevel@tonic-gate 	t->t_wchan0 = (caddr_t)1;
220*7c478bd9Sstevel@tonic-gate 	CL_INACTIVE(t);
221*7c478bd9Sstevel@tonic-gate 	(void) new_mstate(t, LMS_SLEEP);
222*7c478bd9Sstevel@tonic-gate 	THREAD_SLEEP(t, &shuttle_lock);
223*7c478bd9Sstevel@tonic-gate 	disp_lock_exit_high(&shuttle_lock);
224*7c478bd9Sstevel@tonic-gate 	if (lwp && (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t)))
225*7c478bd9Sstevel@tonic-gate 		setrun(t);
226*7c478bd9Sstevel@tonic-gate }
227