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