xref: /illumos-gate/usr/src/uts/common/disp/disp.c (revision 0f500aa6)
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
5ab761399Sesaxe  * Common Development and Distribution License (the "License").
6ab761399Sesaxe  * 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  */
217c478bd9Sstevel@tonic-gate /*
22057452c6Sjj209869  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* from SVr4.0 1.30 */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/param.h>
347c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
357c478bd9Sstevel@tonic-gate #include <sys/signal.h>
367c478bd9Sstevel@tonic-gate #include <sys/user.h>
377c478bd9Sstevel@tonic-gate #include <sys/systm.h>
387c478bd9Sstevel@tonic-gate #include <sys/sysinfo.h>
397c478bd9Sstevel@tonic-gate #include <sys/var.h>
407c478bd9Sstevel@tonic-gate #include <sys/errno.h>
417c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
427c478bd9Sstevel@tonic-gate #include <sys/debug.h>
437c478bd9Sstevel@tonic-gate #include <sys/inline.h>
447c478bd9Sstevel@tonic-gate #include <sys/disp.h>
457c478bd9Sstevel@tonic-gate #include <sys/class.h>
467c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
477c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
487c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
497c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
507c478bd9Sstevel@tonic-gate #include <sys/tnf.h>
517c478bd9Sstevel@tonic-gate #include <sys/cpupart.h>
527c478bd9Sstevel@tonic-gate #include <sys/lgrp.h>
53fb2f18f8Sesaxe #include <sys/pg.h>
54fb2f18f8Sesaxe #include <sys/cmt.h>
55fb2f18f8Sesaxe #include <sys/bitset.h>
567c478bd9Sstevel@tonic-gate #include <sys/schedctl.h>
577c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
587c478bd9Sstevel@tonic-gate #include <sys/dtrace.h>
597c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
60057452c6Sjj209869 #include <sys/archsystm.h>
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #include <vm/as.h>
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #define	BOUND_CPU	0x1
657c478bd9Sstevel@tonic-gate #define	BOUND_PARTITION	0x2
667c478bd9Sstevel@tonic-gate #define	BOUND_INTR	0x4
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /* Dispatch queue allocation structure and functions */
697c478bd9Sstevel@tonic-gate struct disp_queue_info {
707c478bd9Sstevel@tonic-gate 	disp_t	*dp;
717c478bd9Sstevel@tonic-gate 	dispq_t *olddispq;
727c478bd9Sstevel@tonic-gate 	dispq_t *newdispq;
737c478bd9Sstevel@tonic-gate 	ulong_t	*olddqactmap;
747c478bd9Sstevel@tonic-gate 	ulong_t	*newdqactmap;
757c478bd9Sstevel@tonic-gate 	int	oldnglobpris;
767c478bd9Sstevel@tonic-gate };
777c478bd9Sstevel@tonic-gate static void	disp_dq_alloc(struct disp_queue_info *dptr, int numpris,
787c478bd9Sstevel@tonic-gate     disp_t *dp);
797c478bd9Sstevel@tonic-gate static void	disp_dq_assign(struct disp_queue_info *dptr, int numpris);
807c478bd9Sstevel@tonic-gate static void	disp_dq_free(struct disp_queue_info *dptr);
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /* platform-specific routine to call when processor is idle */
837c478bd9Sstevel@tonic-gate static void	generic_idle_cpu();
847c478bd9Sstevel@tonic-gate void		(*idle_cpu)() = generic_idle_cpu;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /* routines invoked when a CPU enters/exits the idle loop */
877c478bd9Sstevel@tonic-gate static void	idle_enter();
887c478bd9Sstevel@tonic-gate static void	idle_exit();
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /* platform-specific routine to call when thread is enqueued */
917c478bd9Sstevel@tonic-gate static void	generic_enq_thread(cpu_t *, int);
927c478bd9Sstevel@tonic-gate void		(*disp_enq_thread)(cpu_t *, int) = generic_enq_thread;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate pri_t	kpreemptpri;		/* priority where kernel preemption applies */
957c478bd9Sstevel@tonic-gate pri_t	upreemptpri = 0; 	/* priority where normal preemption applies */
967c478bd9Sstevel@tonic-gate pri_t	intr_pri;		/* interrupt thread priority base level */
977c478bd9Sstevel@tonic-gate 
98685679f7Sakolb #define	KPQPRI	-1 		/* pri where cpu affinity is dropped for kpq */
997c478bd9Sstevel@tonic-gate pri_t	kpqpri = KPQPRI; 	/* can be set in /etc/system */
1007c478bd9Sstevel@tonic-gate disp_t	cpu0_disp;		/* boot CPU's dispatch queue */
1017c478bd9Sstevel@tonic-gate disp_lock_t	swapped_lock;	/* lock swapped threads and swap queue */
1027c478bd9Sstevel@tonic-gate int	nswapped;		/* total number of swapped threads */
1037c478bd9Sstevel@tonic-gate void	disp_swapped_enq(kthread_t *tp);
1047c478bd9Sstevel@tonic-gate static void	disp_swapped_setrun(kthread_t *tp);
1057c478bd9Sstevel@tonic-gate static void	cpu_resched(cpu_t *cp, pri_t tpri);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate  * If this is set, only interrupt threads will cause kernel preemptions.
1097c478bd9Sstevel@tonic-gate  * This is done by changing the value of kpreemptpri.  kpreemptpri
1107c478bd9Sstevel@tonic-gate  * will either be the max sysclass pri + 1 or the min interrupt pri.
1117c478bd9Sstevel@tonic-gate  */
1127c478bd9Sstevel@tonic-gate int	only_intr_kpreempt;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate extern void set_idle_cpu(int cpun);
1157c478bd9Sstevel@tonic-gate extern void unset_idle_cpu(int cpun);
1167c478bd9Sstevel@tonic-gate static void setkpdq(kthread_t *tp, int borf);
1177c478bd9Sstevel@tonic-gate #define	SETKP_BACK	0
1187c478bd9Sstevel@tonic-gate #define	SETKP_FRONT	1
1197c478bd9Sstevel@tonic-gate /*
1207c478bd9Sstevel@tonic-gate  * Parameter that determines how recently a thread must have run
1217c478bd9Sstevel@tonic-gate  * on the CPU to be considered loosely-bound to that CPU to reduce
1227c478bd9Sstevel@tonic-gate  * cold cache effects.  The interval is in hertz.
1237c478bd9Sstevel@tonic-gate  */
124fb2f18f8Sesaxe #define	RECHOOSE_INTERVAL 3
1257c478bd9Sstevel@tonic-gate int	rechoose_interval = RECHOOSE_INTERVAL;
1267c478bd9Sstevel@tonic-gate static cpu_t	*cpu_choose(kthread_t *, pri_t);
1277c478bd9Sstevel@tonic-gate 
128685679f7Sakolb /*
129685679f7Sakolb  * Parameter that determines how long (in nanoseconds) a thread must
130685679f7Sakolb  * be sitting on a run queue before it can be stolen by another CPU
131685679f7Sakolb  * to reduce migrations.  The interval is in nanoseconds.
132685679f7Sakolb  *
13381588590Sbholler  * The nosteal_nsec should be set by platform code cmp_set_nosteal_interval()
13481588590Sbholler  * to an appropriate value.  nosteal_nsec is set to NOSTEAL_UNINITIALIZED
13581588590Sbholler  * here indicating it is uninitiallized.
13681588590Sbholler  * Setting nosteal_nsec to 0 effectively disables the nosteal 'protection'.
13781588590Sbholler  *
138685679f7Sakolb  */
13981588590Sbholler #define	NOSTEAL_UNINITIALIZED	(-1)
14081588590Sbholler hrtime_t nosteal_nsec = NOSTEAL_UNINITIALIZED;
14181588590Sbholler extern void cmp_set_nosteal_interval(void);
142685679f7Sakolb 
1437c478bd9Sstevel@tonic-gate id_t	defaultcid;	/* system "default" class; see dispadmin(1M) */
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate disp_lock_t	transition_lock;	/* lock on transitioning threads */
1467c478bd9Sstevel@tonic-gate disp_lock_t	stop_lock;		/* lock on stopped threads */
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate static void	cpu_dispqalloc(int numpris);
1497c478bd9Sstevel@tonic-gate 
150685679f7Sakolb /*
151685679f7Sakolb  * This gets returned by disp_getwork/disp_getbest if we couldn't steal
152685679f7Sakolb  * a thread because it was sitting on its run queue for a very short
153685679f7Sakolb  * period of time.
154685679f7Sakolb  */
155685679f7Sakolb #define	T_DONTSTEAL	(kthread_t *)(-1) /* returned by disp_getwork/getbest */
156685679f7Sakolb 
1577c478bd9Sstevel@tonic-gate static kthread_t	*disp_getwork(cpu_t *to);
1587c478bd9Sstevel@tonic-gate static kthread_t	*disp_getbest(disp_t *from);
1597c478bd9Sstevel@tonic-gate static kthread_t	*disp_ratify(kthread_t *tp, disp_t *kpq);
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate void	swtch_to(kthread_t *);
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate /*
1647c478bd9Sstevel@tonic-gate  * dispatcher and scheduler initialization
1657c478bd9Sstevel@tonic-gate  */
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate /*
1687c478bd9Sstevel@tonic-gate  * disp_setup - Common code to calculate and allocate dispatcher
1697c478bd9Sstevel@tonic-gate  *		variables and structures based on the maximum priority.
1707c478bd9Sstevel@tonic-gate  */
1717c478bd9Sstevel@tonic-gate static void
1727c478bd9Sstevel@tonic-gate disp_setup(pri_t maxglobpri, pri_t oldnglobpris)
1737c478bd9Sstevel@tonic-gate {
1747c478bd9Sstevel@tonic-gate 	pri_t	newnglobpris;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	newnglobpris = maxglobpri + 1 + LOCK_LEVEL;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	if (newnglobpris > oldnglobpris) {
1817c478bd9Sstevel@tonic-gate 		/*
1827c478bd9Sstevel@tonic-gate 		 * Allocate new kp queues for each CPU partition.
1837c478bd9Sstevel@tonic-gate 		 */
1847c478bd9Sstevel@tonic-gate 		cpupart_kpqalloc(newnglobpris);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 		/*
1877c478bd9Sstevel@tonic-gate 		 * Allocate new dispatch queues for each CPU.
1887c478bd9Sstevel@tonic-gate 		 */
1897c478bd9Sstevel@tonic-gate 		cpu_dispqalloc(newnglobpris);
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 		/*
1927c478bd9Sstevel@tonic-gate 		 * compute new interrupt thread base priority
1937c478bd9Sstevel@tonic-gate 		 */
1947c478bd9Sstevel@tonic-gate 		intr_pri = maxglobpri;
1957c478bd9Sstevel@tonic-gate 		if (only_intr_kpreempt) {
1967c478bd9Sstevel@tonic-gate 			kpreemptpri = intr_pri + 1;
1977c478bd9Sstevel@tonic-gate 			if (kpqpri == KPQPRI)
1987c478bd9Sstevel@tonic-gate 				kpqpri = kpreemptpri;
1997c478bd9Sstevel@tonic-gate 		}
2007c478bd9Sstevel@tonic-gate 		v.v_nglobpris = newnglobpris;
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate /*
2057c478bd9Sstevel@tonic-gate  * dispinit - Called to initialize all loaded classes and the
2067c478bd9Sstevel@tonic-gate  *	      dispatcher framework.
2077c478bd9Sstevel@tonic-gate  */
2087c478bd9Sstevel@tonic-gate void
2097c478bd9Sstevel@tonic-gate dispinit(void)
2107c478bd9Sstevel@tonic-gate {
2117c478bd9Sstevel@tonic-gate 	id_t	cid;
2127c478bd9Sstevel@tonic-gate 	pri_t	maxglobpri;
2137c478bd9Sstevel@tonic-gate 	pri_t	cl_maxglobpri;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	maxglobpri = -1;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	/*
2187c478bd9Sstevel@tonic-gate 	 * Initialize transition lock, which will always be set.
2197c478bd9Sstevel@tonic-gate 	 */
2207c478bd9Sstevel@tonic-gate 	DISP_LOCK_INIT(&transition_lock);
2217c478bd9Sstevel@tonic-gate 	disp_lock_enter_high(&transition_lock);
2227c478bd9Sstevel@tonic-gate 	DISP_LOCK_INIT(&stop_lock);
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
2257c478bd9Sstevel@tonic-gate 	CPU->cpu_disp->disp_maxrunpri = -1;
2267c478bd9Sstevel@tonic-gate 	CPU->cpu_disp->disp_max_unbound_pri = -1;
227fb2f18f8Sesaxe 
2287c478bd9Sstevel@tonic-gate 	/*
2297c478bd9Sstevel@tonic-gate 	 * Initialize the default CPU partition.
2307c478bd9Sstevel@tonic-gate 	 */
2317c478bd9Sstevel@tonic-gate 	cpupart_initialize_default();
2327c478bd9Sstevel@tonic-gate 	/*
2337c478bd9Sstevel@tonic-gate 	 * Call the class specific initialization functions for
2347c478bd9Sstevel@tonic-gate 	 * all pre-installed schedulers.
2357c478bd9Sstevel@tonic-gate 	 *
2367c478bd9Sstevel@tonic-gate 	 * We pass the size of a class specific parameter
2377c478bd9Sstevel@tonic-gate 	 * buffer to each of the initialization functions
2387c478bd9Sstevel@tonic-gate 	 * to try to catch problems with backward compatibility
2397c478bd9Sstevel@tonic-gate 	 * of class modules.
2407c478bd9Sstevel@tonic-gate 	 *
2417c478bd9Sstevel@tonic-gate 	 * For example a new class module running on an old system
2427c478bd9Sstevel@tonic-gate 	 * which didn't provide sufficiently large parameter buffers
2437c478bd9Sstevel@tonic-gate 	 * would be bad news. Class initialization modules can check for
2447c478bd9Sstevel@tonic-gate 	 * this and take action if they detect a problem.
2457c478bd9Sstevel@tonic-gate 	 */
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	for (cid = 0; cid < nclass; cid++) {
2487c478bd9Sstevel@tonic-gate 		sclass_t	*sc;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 		sc = &sclass[cid];
2517c478bd9Sstevel@tonic-gate 		if (SCHED_INSTALLED(sc)) {
2527c478bd9Sstevel@tonic-gate 			cl_maxglobpri = sc->cl_init(cid, PC_CLPARMSZ,
2537c478bd9Sstevel@tonic-gate 			    &sc->cl_funcs);
2547c478bd9Sstevel@tonic-gate 			if (cl_maxglobpri > maxglobpri)
2557c478bd9Sstevel@tonic-gate 				maxglobpri = cl_maxglobpri;
2567c478bd9Sstevel@tonic-gate 		}
2577c478bd9Sstevel@tonic-gate 	}
2587c478bd9Sstevel@tonic-gate 	kpreemptpri = (pri_t)v.v_maxsyspri + 1;
2597c478bd9Sstevel@tonic-gate 	if (kpqpri == KPQPRI)
2607c478bd9Sstevel@tonic-gate 		kpqpri = kpreemptpri;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	ASSERT(maxglobpri >= 0);
2637c478bd9Sstevel@tonic-gate 	disp_setup(maxglobpri, 0);
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	/*
26881588590Sbholler 	 * Platform specific sticky scheduler setup.
26981588590Sbholler 	 */
27081588590Sbholler 	if (nosteal_nsec == NOSTEAL_UNINITIALIZED)
27181588590Sbholler 		cmp_set_nosteal_interval();
27281588590Sbholler 
27381588590Sbholler 	/*
2747c478bd9Sstevel@tonic-gate 	 * Get the default class ID; this may be later modified via
2757c478bd9Sstevel@tonic-gate 	 * dispadmin(1M).  This will load the class (normally TS) and that will
2767c478bd9Sstevel@tonic-gate 	 * call disp_add(), which is why we had to drop cpu_lock first.
2777c478bd9Sstevel@tonic-gate 	 */
2787c478bd9Sstevel@tonic-gate 	if (getcid(defaultclass, &defaultcid) != 0) {
2797c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "Couldn't load default scheduling class '%s'",
2807c478bd9Sstevel@tonic-gate 		    defaultclass);
2817c478bd9Sstevel@tonic-gate 	}
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate /*
2857c478bd9Sstevel@tonic-gate  * disp_add - Called with class pointer to initialize the dispatcher
2867c478bd9Sstevel@tonic-gate  *	      for a newly loaded class.
2877c478bd9Sstevel@tonic-gate  */
2887c478bd9Sstevel@tonic-gate void
2897c478bd9Sstevel@tonic-gate disp_add(sclass_t *clp)
2907c478bd9Sstevel@tonic-gate {
2917c478bd9Sstevel@tonic-gate 	pri_t	maxglobpri;
2927c478bd9Sstevel@tonic-gate 	pri_t	cl_maxglobpri;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
2957c478bd9Sstevel@tonic-gate 	/*
2967c478bd9Sstevel@tonic-gate 	 * Initialize the scheduler class.
2977c478bd9Sstevel@tonic-gate 	 */
2987c478bd9Sstevel@tonic-gate 	maxglobpri = (pri_t)(v.v_nglobpris - LOCK_LEVEL - 1);
2997c478bd9Sstevel@tonic-gate 	cl_maxglobpri = clp->cl_init(clp - sclass, PC_CLPARMSZ, &clp->cl_funcs);
3007c478bd9Sstevel@tonic-gate 	if (cl_maxglobpri > maxglobpri)
3017c478bd9Sstevel@tonic-gate 		maxglobpri = cl_maxglobpri;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	/*
3047c478bd9Sstevel@tonic-gate 	 * Save old queue information.  Since we're initializing a
3057c478bd9Sstevel@tonic-gate 	 * new scheduling class which has just been loaded, then
3067c478bd9Sstevel@tonic-gate 	 * the size of the dispq may have changed.  We need to handle
3077c478bd9Sstevel@tonic-gate 	 * that here.
3087c478bd9Sstevel@tonic-gate 	 */
3097c478bd9Sstevel@tonic-gate 	disp_setup(maxglobpri, v.v_nglobpris);
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
3127c478bd9Sstevel@tonic-gate }
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate /*
3167c478bd9Sstevel@tonic-gate  * For each CPU, allocate new dispatch queues
3177c478bd9Sstevel@tonic-gate  * with the stated number of priorities.
3187c478bd9Sstevel@tonic-gate  */
3197c478bd9Sstevel@tonic-gate static void
3207c478bd9Sstevel@tonic-gate cpu_dispqalloc(int numpris)
3217c478bd9Sstevel@tonic-gate {
3227c478bd9Sstevel@tonic-gate 	cpu_t	*cpup;
3237c478bd9Sstevel@tonic-gate 	struct disp_queue_info	*disp_mem;
3247c478bd9Sstevel@tonic-gate 	int i, num;
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	disp_mem = kmem_zalloc(NCPU *
3297c478bd9Sstevel@tonic-gate 	    sizeof (struct disp_queue_info), KM_SLEEP);
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	/*
3327c478bd9Sstevel@tonic-gate 	 * This routine must allocate all of the memory before stopping
3337c478bd9Sstevel@tonic-gate 	 * the cpus because it must not sleep in kmem_alloc while the
3347c478bd9Sstevel@tonic-gate 	 * CPUs are stopped.  Locks they hold will not be freed until they
3357c478bd9Sstevel@tonic-gate 	 * are restarted.
3367c478bd9Sstevel@tonic-gate 	 */
3377c478bd9Sstevel@tonic-gate 	i = 0;
3387c478bd9Sstevel@tonic-gate 	cpup = cpu_list;
3397c478bd9Sstevel@tonic-gate 	do {
3407c478bd9Sstevel@tonic-gate 		disp_dq_alloc(&disp_mem[i], numpris, cpup->cpu_disp);
3417c478bd9Sstevel@tonic-gate 		i++;
3427c478bd9Sstevel@tonic-gate 		cpup = cpup->cpu_next;
3437c478bd9Sstevel@tonic-gate 	} while (cpup != cpu_list);
3447c478bd9Sstevel@tonic-gate 	num = i;
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	pause_cpus(NULL);
3477c478bd9Sstevel@tonic-gate 	for (i = 0; i < num; i++)
3487c478bd9Sstevel@tonic-gate 		disp_dq_assign(&disp_mem[i], numpris);
3497c478bd9Sstevel@tonic-gate 	start_cpus();
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	/*
3527c478bd9Sstevel@tonic-gate 	 * I must free all of the memory after starting the cpus because
3537c478bd9Sstevel@tonic-gate 	 * I can not risk sleeping in kmem_free while the cpus are stopped.
3547c478bd9Sstevel@tonic-gate 	 */
3557c478bd9Sstevel@tonic-gate 	for (i = 0; i < num; i++)
3567c478bd9Sstevel@tonic-gate 		disp_dq_free(&disp_mem[i]);
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	kmem_free(disp_mem, NCPU * sizeof (struct disp_queue_info));
3597c478bd9Sstevel@tonic-gate }
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate static void
3627c478bd9Sstevel@tonic-gate disp_dq_alloc(struct disp_queue_info *dptr, int numpris, disp_t	*dp)
3637c478bd9Sstevel@tonic-gate {
3647c478bd9Sstevel@tonic-gate 	dptr->newdispq = kmem_zalloc(numpris * sizeof (dispq_t), KM_SLEEP);
3657c478bd9Sstevel@tonic-gate 	dptr->newdqactmap = kmem_zalloc(((numpris / BT_NBIPUL) + 1) *
3667c478bd9Sstevel@tonic-gate 	    sizeof (long), KM_SLEEP);
3677c478bd9Sstevel@tonic-gate 	dptr->dp = dp;
3687c478bd9Sstevel@tonic-gate }
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate static void
3717c478bd9Sstevel@tonic-gate disp_dq_assign(struct disp_queue_info *dptr, int numpris)
3727c478bd9Sstevel@tonic-gate {
3737c478bd9Sstevel@tonic-gate 	disp_t	*dp;
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	dp = dptr->dp;
3767c478bd9Sstevel@tonic-gate 	dptr->olddispq = dp->disp_q;
3777c478bd9Sstevel@tonic-gate 	dptr->olddqactmap = dp->disp_qactmap;
3787c478bd9Sstevel@tonic-gate 	dptr->oldnglobpris = dp->disp_npri;
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	ASSERT(dptr->oldnglobpris < numpris);
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	if (dptr->olddispq != NULL) {
3837c478bd9Sstevel@tonic-gate 		/*
3847c478bd9Sstevel@tonic-gate 		 * Use kcopy because bcopy is platform-specific
3857c478bd9Sstevel@tonic-gate 		 * and could block while we might have paused the cpus.
3867c478bd9Sstevel@tonic-gate 		 */
3877c478bd9Sstevel@tonic-gate 		(void) kcopy(dptr->olddispq, dptr->newdispq,
3887c478bd9Sstevel@tonic-gate 		    dptr->oldnglobpris * sizeof (dispq_t));
3897c478bd9Sstevel@tonic-gate 		(void) kcopy(dptr->olddqactmap, dptr->newdqactmap,
3907c478bd9Sstevel@tonic-gate 		    ((dptr->oldnglobpris / BT_NBIPUL) + 1) *
3917c478bd9Sstevel@tonic-gate 		    sizeof (long));
3927c478bd9Sstevel@tonic-gate 	}
3937c478bd9Sstevel@tonic-gate 	dp->disp_q = dptr->newdispq;
3947c478bd9Sstevel@tonic-gate 	dp->disp_qactmap = dptr->newdqactmap;
3957c478bd9Sstevel@tonic-gate 	dp->disp_q_limit = &dptr->newdispq[numpris];
3967c478bd9Sstevel@tonic-gate 	dp->disp_npri = numpris;
3977c478bd9Sstevel@tonic-gate }
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate static void
4007c478bd9Sstevel@tonic-gate disp_dq_free(struct disp_queue_info *dptr)
4017c478bd9Sstevel@tonic-gate {
4027c478bd9Sstevel@tonic-gate 	if (dptr->olddispq != NULL)
4037c478bd9Sstevel@tonic-gate 		kmem_free(dptr->olddispq,
4047c478bd9Sstevel@tonic-gate 		    dptr->oldnglobpris * sizeof (dispq_t));
4057c478bd9Sstevel@tonic-gate 	if (dptr->olddqactmap != NULL)
4067c478bd9Sstevel@tonic-gate 		kmem_free(dptr->olddqactmap,
4077c478bd9Sstevel@tonic-gate 		    ((dptr->oldnglobpris / BT_NBIPUL) + 1) * sizeof (long));
4087c478bd9Sstevel@tonic-gate }
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate /*
4117c478bd9Sstevel@tonic-gate  * For a newly created CPU, initialize the dispatch queue.
4127c478bd9Sstevel@tonic-gate  * This is called before the CPU is known through cpu[] or on any lists.
4137c478bd9Sstevel@tonic-gate  */
4147c478bd9Sstevel@tonic-gate void
4157c478bd9Sstevel@tonic-gate disp_cpu_init(cpu_t *cp)
4167c478bd9Sstevel@tonic-gate {
4177c478bd9Sstevel@tonic-gate 	disp_t	*dp;
4187c478bd9Sstevel@tonic-gate 	dispq_t	*newdispq;
4197c478bd9Sstevel@tonic-gate 	ulong_t	*newdqactmap;
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));	/* protect dispatcher queue sizes */
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	if (cp == cpu0_disp.disp_cpu)
4247c478bd9Sstevel@tonic-gate 		dp = &cpu0_disp;
4257c478bd9Sstevel@tonic-gate 	else
4267c478bd9Sstevel@tonic-gate 		dp = kmem_alloc(sizeof (disp_t), KM_SLEEP);
4277c478bd9Sstevel@tonic-gate 	bzero(dp, sizeof (disp_t));
4287c478bd9Sstevel@tonic-gate 	cp->cpu_disp = dp;
4297c478bd9Sstevel@tonic-gate 	dp->disp_cpu = cp;
4307c478bd9Sstevel@tonic-gate 	dp->disp_maxrunpri = -1;
4317c478bd9Sstevel@tonic-gate 	dp->disp_max_unbound_pri = -1;
4327c478bd9Sstevel@tonic-gate 	DISP_LOCK_INIT(&cp->cpu_thread_lock);
4337c478bd9Sstevel@tonic-gate 	/*
4347c478bd9Sstevel@tonic-gate 	 * Allocate memory for the dispatcher queue headers
4357c478bd9Sstevel@tonic-gate 	 * and the active queue bitmap.
4367c478bd9Sstevel@tonic-gate 	 */
4377c478bd9Sstevel@tonic-gate 	newdispq = kmem_zalloc(v.v_nglobpris * sizeof (dispq_t), KM_SLEEP);
4387c478bd9Sstevel@tonic-gate 	newdqactmap = kmem_zalloc(((v.v_nglobpris / BT_NBIPUL) + 1) *
4397c478bd9Sstevel@tonic-gate 	    sizeof (long), KM_SLEEP);
4407c478bd9Sstevel@tonic-gate 	dp->disp_q = newdispq;
4417c478bd9Sstevel@tonic-gate 	dp->disp_qactmap = newdqactmap;
4427c478bd9Sstevel@tonic-gate 	dp->disp_q_limit = &newdispq[v.v_nglobpris];
4437c478bd9Sstevel@tonic-gate 	dp->disp_npri = v.v_nglobpris;
4447c478bd9Sstevel@tonic-gate }
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate void
4477c478bd9Sstevel@tonic-gate disp_cpu_fini(cpu_t *cp)
4487c478bd9Sstevel@tonic-gate {
4497c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	disp_kp_free(cp->cpu_disp);
4527c478bd9Sstevel@tonic-gate 	if (cp->cpu_disp != &cpu0_disp)
4537c478bd9Sstevel@tonic-gate 		kmem_free(cp->cpu_disp, sizeof (disp_t));
4547c478bd9Sstevel@tonic-gate }
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate /*
4577c478bd9Sstevel@tonic-gate  * Allocate new, larger kpreempt dispatch queue to replace the old one.
4587c478bd9Sstevel@tonic-gate  */
4597c478bd9Sstevel@tonic-gate void
4607c478bd9Sstevel@tonic-gate disp_kp_alloc(disp_t *dq, pri_t npri)
4617c478bd9Sstevel@tonic-gate {
4627c478bd9Sstevel@tonic-gate 	struct disp_queue_info	mem_info;
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	if (npri > dq->disp_npri) {
4657c478bd9Sstevel@tonic-gate 		/*
4667c478bd9Sstevel@tonic-gate 		 * Allocate memory for the new array.
4677c478bd9Sstevel@tonic-gate 		 */
4687c478bd9Sstevel@tonic-gate 		disp_dq_alloc(&mem_info, npri, dq);
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 		/*
4717c478bd9Sstevel@tonic-gate 		 * We need to copy the old structures to the new
4727c478bd9Sstevel@tonic-gate 		 * and free the old.
4737c478bd9Sstevel@tonic-gate 		 */
4747c478bd9Sstevel@tonic-gate 		disp_dq_assign(&mem_info, npri);
4757c478bd9Sstevel@tonic-gate 		disp_dq_free(&mem_info);
4767c478bd9Sstevel@tonic-gate 	}
4777c478bd9Sstevel@tonic-gate }
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate /*
4807c478bd9Sstevel@tonic-gate  * Free dispatch queue.
4817c478bd9Sstevel@tonic-gate  * Used for the kpreempt queues for a removed CPU partition and
4827c478bd9Sstevel@tonic-gate  * for the per-CPU queues of deleted CPUs.
4837c478bd9Sstevel@tonic-gate  */
4847c478bd9Sstevel@tonic-gate void
4857c478bd9Sstevel@tonic-gate disp_kp_free(disp_t *dq)
4867c478bd9Sstevel@tonic-gate {
4877c478bd9Sstevel@tonic-gate 	struct disp_queue_info	mem_info;
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	mem_info.olddispq = dq->disp_q;
4907c478bd9Sstevel@tonic-gate 	mem_info.olddqactmap = dq->disp_qactmap;
4917c478bd9Sstevel@tonic-gate 	mem_info.oldnglobpris = dq->disp_npri;
4927c478bd9Sstevel@tonic-gate 	disp_dq_free(&mem_info);
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate /*
4967c478bd9Sstevel@tonic-gate  * End dispatcher and scheduler initialization.
4977c478bd9Sstevel@tonic-gate  */
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate /*
5007c478bd9Sstevel@tonic-gate  * See if there's anything to do other than remain idle.
5017c478bd9Sstevel@tonic-gate  * Return non-zero if there is.
5027c478bd9Sstevel@tonic-gate  *
5037c478bd9Sstevel@tonic-gate  * This function must be called with high spl, or with
5047c478bd9Sstevel@tonic-gate  * kernel preemption disabled to prevent the partition's
5057c478bd9Sstevel@tonic-gate  * active cpu list from changing while being traversed.
5067c478bd9Sstevel@tonic-gate  *
5077c478bd9Sstevel@tonic-gate  */
5087c478bd9Sstevel@tonic-gate int
5097c478bd9Sstevel@tonic-gate disp_anywork(void)
5107c478bd9Sstevel@tonic-gate {
5117c478bd9Sstevel@tonic-gate 	cpu_t   *cp = CPU;
5127c478bd9Sstevel@tonic-gate 	cpu_t   *ocp;
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	if (cp->cpu_disp->disp_nrunnable != 0)
5157c478bd9Sstevel@tonic-gate 		return (1);
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	if (!(cp->cpu_flags & CPU_OFFLINE)) {
5187c478bd9Sstevel@tonic-gate 		if (CP_MAXRUNPRI(cp->cpu_part) >= 0)
5197c478bd9Sstevel@tonic-gate 			return (1);
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 		/*
5227c478bd9Sstevel@tonic-gate 		 * Work can be taken from another CPU if:
5237c478bd9Sstevel@tonic-gate 		 *	- There is unbound work on the run queue
5247c478bd9Sstevel@tonic-gate 		 *	- That work isn't a thread undergoing a
5257c478bd9Sstevel@tonic-gate 		 *	- context switch on an otherwise empty queue.
5267c478bd9Sstevel@tonic-gate 		 *	- The CPU isn't running the idle loop.
5277c478bd9Sstevel@tonic-gate 		 */
5287c478bd9Sstevel@tonic-gate 		for (ocp = cp->cpu_next_part; ocp != cp;
5297c478bd9Sstevel@tonic-gate 		    ocp = ocp->cpu_next_part) {
5307c478bd9Sstevel@tonic-gate 			ASSERT(CPU_ACTIVE(ocp));
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 			if (ocp->cpu_disp->disp_max_unbound_pri != -1 &&
5337c478bd9Sstevel@tonic-gate 			    !((ocp->cpu_disp_flags & CPU_DISP_DONTSTEAL) &&
5347c478bd9Sstevel@tonic-gate 			    ocp->cpu_disp->disp_nrunnable == 1) &&
5357c478bd9Sstevel@tonic-gate 			    ocp->cpu_dispatch_pri != -1)
5367c478bd9Sstevel@tonic-gate 				return (1);
5377c478bd9Sstevel@tonic-gate 		}
5387c478bd9Sstevel@tonic-gate 	}
5397c478bd9Sstevel@tonic-gate 	return (0);
5407c478bd9Sstevel@tonic-gate }
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate /*
5437c478bd9Sstevel@tonic-gate  * Called when CPU enters the idle loop
5447c478bd9Sstevel@tonic-gate  */
5457c478bd9Sstevel@tonic-gate static void
5467c478bd9Sstevel@tonic-gate idle_enter()
5477c478bd9Sstevel@tonic-gate {
5487c478bd9Sstevel@tonic-gate 	cpu_t		*cp = CPU;
5497c478bd9Sstevel@tonic-gate 
550eda89462Sesolom 	new_cpu_mstate(CMS_IDLE, gethrtime_unscaled());
5517c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(cp, sys, idlethread, 1);
5527c478bd9Sstevel@tonic-gate 	set_idle_cpu(cp->cpu_id);	/* arch-dependent hook */
5537c478bd9Sstevel@tonic-gate }
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate /*
5567c478bd9Sstevel@tonic-gate  * Called when CPU exits the idle loop
5577c478bd9Sstevel@tonic-gate  */
5587c478bd9Sstevel@tonic-gate static void
5597c478bd9Sstevel@tonic-gate idle_exit()
5607c478bd9Sstevel@tonic-gate {
5617c478bd9Sstevel@tonic-gate 	cpu_t		*cp = CPU;
5627c478bd9Sstevel@tonic-gate 
563eda89462Sesolom 	new_cpu_mstate(CMS_SYSTEM, gethrtime_unscaled());
5647c478bd9Sstevel@tonic-gate 	unset_idle_cpu(cp->cpu_id);	/* arch-dependent hook */
5657c478bd9Sstevel@tonic-gate }
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate /*
5687c478bd9Sstevel@tonic-gate  * Idle loop.
5697c478bd9Sstevel@tonic-gate  */
5707c478bd9Sstevel@tonic-gate void
5717c478bd9Sstevel@tonic-gate idle()
5727c478bd9Sstevel@tonic-gate {
5737c478bd9Sstevel@tonic-gate 	struct cpu	*cp = CPU;		/* pointer to this CPU */
5747c478bd9Sstevel@tonic-gate 	kthread_t	*t;			/* taken thread */
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	idle_enter();
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	/*
5797c478bd9Sstevel@tonic-gate 	 * Uniprocessor version of idle loop.
5807c478bd9Sstevel@tonic-gate 	 * Do this until notified that we're on an actual multiprocessor.
5817c478bd9Sstevel@tonic-gate 	 */
5827c478bd9Sstevel@tonic-gate 	while (ncpus == 1) {
5837c478bd9Sstevel@tonic-gate 		if (cp->cpu_disp->disp_nrunnable == 0) {
5847c478bd9Sstevel@tonic-gate 			(*idle_cpu)();
5857c478bd9Sstevel@tonic-gate 			continue;
5867c478bd9Sstevel@tonic-gate 		}
5877c478bd9Sstevel@tonic-gate 		idle_exit();
5887c478bd9Sstevel@tonic-gate 		swtch();
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 		idle_enter(); /* returned from swtch */
5917c478bd9Sstevel@tonic-gate 	}
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	/*
5947c478bd9Sstevel@tonic-gate 	 * Multiprocessor idle loop.
5957c478bd9Sstevel@tonic-gate 	 */
5967c478bd9Sstevel@tonic-gate 	for (;;) {
5977c478bd9Sstevel@tonic-gate 		/*
5987c478bd9Sstevel@tonic-gate 		 * If CPU is completely quiesced by p_online(2), just wait
5997c478bd9Sstevel@tonic-gate 		 * here with minimal bus traffic until put online.
6007c478bd9Sstevel@tonic-gate 		 */
6017c478bd9Sstevel@tonic-gate 		while (cp->cpu_flags & CPU_QUIESCED)
6027c478bd9Sstevel@tonic-gate 			(*idle_cpu)();
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 		if (cp->cpu_disp->disp_nrunnable != 0) {
6057c478bd9Sstevel@tonic-gate 			idle_exit();
6067c478bd9Sstevel@tonic-gate 			swtch();
6077c478bd9Sstevel@tonic-gate 		} else {
6087c478bd9Sstevel@tonic-gate 			if (cp->cpu_flags & CPU_OFFLINE)
6097c478bd9Sstevel@tonic-gate 				continue;
6107c478bd9Sstevel@tonic-gate 			if ((t = disp_getwork(cp)) == NULL) {
6117c478bd9Sstevel@tonic-gate 				if (cp->cpu_chosen_level != -1) {
6127c478bd9Sstevel@tonic-gate 					disp_t *dp = cp->cpu_disp;
6137c478bd9Sstevel@tonic-gate 					disp_t *kpq;
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 					disp_lock_enter(&dp->disp_lock);
6167c478bd9Sstevel@tonic-gate 					/*
6177c478bd9Sstevel@tonic-gate 					 * Set kpq under lock to prevent
6187c478bd9Sstevel@tonic-gate 					 * migration between partitions.
6197c478bd9Sstevel@tonic-gate 					 */
6207c478bd9Sstevel@tonic-gate 					kpq = &cp->cpu_part->cp_kp_queue;
6217c478bd9Sstevel@tonic-gate 					if (kpq->disp_maxrunpri == -1)
6227c478bd9Sstevel@tonic-gate 						cp->cpu_chosen_level = -1;
6237c478bd9Sstevel@tonic-gate 					disp_lock_exit(&dp->disp_lock);
6247c478bd9Sstevel@tonic-gate 				}
6257c478bd9Sstevel@tonic-gate 				(*idle_cpu)();
6267c478bd9Sstevel@tonic-gate 				continue;
6277c478bd9Sstevel@tonic-gate 			}
628685679f7Sakolb 			/*
629685679f7Sakolb 			 * If there was a thread but we couldn't steal
630685679f7Sakolb 			 * it, then keep trying.
631685679f7Sakolb 			 */
632685679f7Sakolb 			if (t == T_DONTSTEAL)
633685679f7Sakolb 				continue;
6347c478bd9Sstevel@tonic-gate 			idle_exit();
6357c478bd9Sstevel@tonic-gate 			swtch_to(t);
6367c478bd9Sstevel@tonic-gate 		}
6377c478bd9Sstevel@tonic-gate 		idle_enter(); /* returned from swtch/swtch_to */
6387c478bd9Sstevel@tonic-gate 	}
6397c478bd9Sstevel@tonic-gate }
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate /*
6437c478bd9Sstevel@tonic-gate  * Preempt the currently running thread in favor of the highest
6447c478bd9Sstevel@tonic-gate  * priority thread.  The class of the current thread controls
6457c478bd9Sstevel@tonic-gate  * where it goes on the dispatcher queues. If panicking, turn
6467c478bd9Sstevel@tonic-gate  * preemption off.
6477c478bd9Sstevel@tonic-gate  */
6487c478bd9Sstevel@tonic-gate void
6497c478bd9Sstevel@tonic-gate preempt()
6507c478bd9Sstevel@tonic-gate {
6517c478bd9Sstevel@tonic-gate 	kthread_t 	*t = curthread;
6527c478bd9Sstevel@tonic-gate 	klwp_t 		*lwp = ttolwp(curthread);
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	if (panicstr)
6557c478bd9Sstevel@tonic-gate 		return;
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_DISP, TR_PREEMPT_START, "preempt_start");
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	thread_lock(t);
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	if (t->t_state != TS_ONPROC || t->t_disp_queue != CPU->cpu_disp) {
6627c478bd9Sstevel@tonic-gate 		/*
6637c478bd9Sstevel@tonic-gate 		 * this thread has already been chosen to be run on
6647c478bd9Sstevel@tonic-gate 		 * another CPU. Clear kprunrun on this CPU since we're
6657c478bd9Sstevel@tonic-gate 		 * already headed for swtch().
6667c478bd9Sstevel@tonic-gate 		 */
6677c478bd9Sstevel@tonic-gate 		CPU->cpu_kprunrun = 0;
6687c478bd9Sstevel@tonic-gate 		thread_unlock_nopreempt(t);
6697c478bd9Sstevel@tonic-gate 		TRACE_0(TR_FAC_DISP, TR_PREEMPT_END, "preempt_end");
6707c478bd9Sstevel@tonic-gate 	} else {
6717c478bd9Sstevel@tonic-gate 		if (lwp != NULL)
6727c478bd9Sstevel@tonic-gate 			lwp->lwp_ru.nivcsw++;
6737c478bd9Sstevel@tonic-gate 		CPU_STATS_ADDQ(CPU, sys, inv_swtch, 1);
6747c478bd9Sstevel@tonic-gate 		THREAD_TRANSITION(t);
6757c478bd9Sstevel@tonic-gate 		CL_PREEMPT(t);
6767c478bd9Sstevel@tonic-gate 		DTRACE_SCHED(preempt);
6777c478bd9Sstevel@tonic-gate 		thread_unlock_nopreempt(t);
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 		TRACE_0(TR_FAC_DISP, TR_PREEMPT_END, "preempt_end");
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 		swtch();		/* clears CPU->cpu_runrun via disp() */
6827c478bd9Sstevel@tonic-gate 	}
6837c478bd9Sstevel@tonic-gate }
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate extern kthread_t *thread_unpin();
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate /*
6887c478bd9Sstevel@tonic-gate  * disp() - find the highest priority thread for this processor to run, and
6897c478bd9Sstevel@tonic-gate  * set it in TS_ONPROC state so that resume() can be called to run it.
6907c478bd9Sstevel@tonic-gate  */
6917c478bd9Sstevel@tonic-gate static kthread_t *
6927c478bd9Sstevel@tonic-gate disp()
6937c478bd9Sstevel@tonic-gate {
6947c478bd9Sstevel@tonic-gate 	cpu_t		*cpup;
6957c478bd9Sstevel@tonic-gate 	disp_t		*dp;
6967c478bd9Sstevel@tonic-gate 	kthread_t	*tp;
6977c478bd9Sstevel@tonic-gate 	dispq_t		*dq;
6987c478bd9Sstevel@tonic-gate 	int		maxrunword;
6997c478bd9Sstevel@tonic-gate 	pri_t		pri;
7007c478bd9Sstevel@tonic-gate 	disp_t		*kpq;
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_DISP, TR_DISP_START, "disp_start");
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	cpup = CPU;
7057c478bd9Sstevel@tonic-gate 	/*
7067c478bd9Sstevel@tonic-gate 	 * Find the highest priority loaded, runnable thread.
7077c478bd9Sstevel@tonic-gate 	 */
7087c478bd9Sstevel@tonic-gate 	dp = cpup->cpu_disp;
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate reschedule:
7117c478bd9Sstevel@tonic-gate 	/*
7127c478bd9Sstevel@tonic-gate 	 * If there is more important work on the global queue with a better
7137c478bd9Sstevel@tonic-gate 	 * priority than the maximum on this CPU, take it now.
7147c478bd9Sstevel@tonic-gate 	 */
7157c478bd9Sstevel@tonic-gate 	kpq = &cpup->cpu_part->cp_kp_queue;
7167c478bd9Sstevel@tonic-gate 	while ((pri = kpq->disp_maxrunpri) >= 0 &&
7177c478bd9Sstevel@tonic-gate 	    pri >= dp->disp_maxrunpri &&
7187c478bd9Sstevel@tonic-gate 	    (cpup->cpu_flags & CPU_OFFLINE) == 0 &&
7197c478bd9Sstevel@tonic-gate 	    (tp = disp_getbest(kpq)) != NULL) {
7207c478bd9Sstevel@tonic-gate 		if (disp_ratify(tp, kpq) != NULL) {
7217c478bd9Sstevel@tonic-gate 			TRACE_1(TR_FAC_DISP, TR_DISP_END,
7227c478bd9Sstevel@tonic-gate 			    "disp_end:tid %p", tp);
7237c478bd9Sstevel@tonic-gate 			return (tp);
7247c478bd9Sstevel@tonic-gate 		}
7257c478bd9Sstevel@tonic-gate 	}
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 	disp_lock_enter(&dp->disp_lock);
7287c478bd9Sstevel@tonic-gate 	pri = dp->disp_maxrunpri;
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	/*
7317c478bd9Sstevel@tonic-gate 	 * If there is nothing to run, look at what's runnable on other queues.
7327c478bd9Sstevel@tonic-gate 	 * Choose the idle thread if the CPU is quiesced.
7337c478bd9Sstevel@tonic-gate 	 * Note that CPUs that have the CPU_OFFLINE flag set can still run
7347c478bd9Sstevel@tonic-gate 	 * interrupt threads, which will be the only threads on the CPU's own
7357c478bd9Sstevel@tonic-gate 	 * queue, but cannot run threads from other queues.
7367c478bd9Sstevel@tonic-gate 	 */
7377c478bd9Sstevel@tonic-gate 	if (pri == -1) {
7387c478bd9Sstevel@tonic-gate 		if (!(cpup->cpu_flags & CPU_OFFLINE)) {
7397c478bd9Sstevel@tonic-gate 			disp_lock_exit(&dp->disp_lock);
740685679f7Sakolb 			if ((tp = disp_getwork(cpup)) == NULL ||
741685679f7Sakolb 			    tp == T_DONTSTEAL) {
7427c478bd9Sstevel@tonic-gate 				tp = cpup->cpu_idle_thread;
7437c478bd9Sstevel@tonic-gate 				(void) splhigh();
7447c478bd9Sstevel@tonic-gate 				THREAD_ONPROC(tp, cpup);
7457c478bd9Sstevel@tonic-gate 				cpup->cpu_dispthread = tp;
7467c478bd9Sstevel@tonic-gate 				cpup->cpu_dispatch_pri = -1;
7477c478bd9Sstevel@tonic-gate 				cpup->cpu_runrun = cpup->cpu_kprunrun = 0;
7487c478bd9Sstevel@tonic-gate 				cpup->cpu_chosen_level = -1;
7497c478bd9Sstevel@tonic-gate 			}
7507c478bd9Sstevel@tonic-gate 		} else {
7517c478bd9Sstevel@tonic-gate 			disp_lock_exit_high(&dp->disp_lock);
7527c478bd9Sstevel@tonic-gate 			tp = cpup->cpu_idle_thread;
7537c478bd9Sstevel@tonic-gate 			THREAD_ONPROC(tp, cpup);
7547c478bd9Sstevel@tonic-gate 			cpup->cpu_dispthread = tp;
7557c478bd9Sstevel@tonic-gate 			cpup->cpu_dispatch_pri = -1;
7567c478bd9Sstevel@tonic-gate 			cpup->cpu_runrun = cpup->cpu_kprunrun = 0;
7577c478bd9Sstevel@tonic-gate 			cpup->cpu_chosen_level = -1;
7587c478bd9Sstevel@tonic-gate 		}
7597c478bd9Sstevel@tonic-gate 		TRACE_1(TR_FAC_DISP, TR_DISP_END,
7607c478bd9Sstevel@tonic-gate 		    "disp_end:tid %p", tp);
7617c478bd9Sstevel@tonic-gate 		return (tp);
7627c478bd9Sstevel@tonic-gate 	}
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	dq = &dp->disp_q[pri];
7657c478bd9Sstevel@tonic-gate 	tp = dq->dq_first;
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	ASSERT(tp != NULL);
7687c478bd9Sstevel@tonic-gate 	ASSERT(tp->t_schedflag & TS_LOAD);	/* thread must be swapped in */
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	DTRACE_SCHED2(dequeue, kthread_t *, tp, disp_t *, dp);
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	/*
7737c478bd9Sstevel@tonic-gate 	 * Found it so remove it from queue.
7747c478bd9Sstevel@tonic-gate 	 */
7757c478bd9Sstevel@tonic-gate 	dp->disp_nrunnable--;
7767c478bd9Sstevel@tonic-gate 	dq->dq_sruncnt--;
7777c478bd9Sstevel@tonic-gate 	if ((dq->dq_first = tp->t_link) == NULL) {
7787c478bd9Sstevel@tonic-gate 		ulong_t	*dqactmap = dp->disp_qactmap;
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 		ASSERT(dq->dq_sruncnt == 0);
7817c478bd9Sstevel@tonic-gate 		dq->dq_last = NULL;
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 		/*
7847c478bd9Sstevel@tonic-gate 		 * The queue is empty, so the corresponding bit needs to be
7857c478bd9Sstevel@tonic-gate 		 * turned off in dqactmap.   If nrunnable != 0 just took the
7867c478bd9Sstevel@tonic-gate 		 * last runnable thread off the
7877c478bd9Sstevel@tonic-gate 		 * highest queue, so recompute disp_maxrunpri.
7887c478bd9Sstevel@tonic-gate 		 */
7897c478bd9Sstevel@tonic-gate 		maxrunword = pri >> BT_ULSHIFT;
7907c478bd9Sstevel@tonic-gate 		dqactmap[maxrunword] &= ~BT_BIW(pri);
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 		if (dp->disp_nrunnable == 0) {
7937c478bd9Sstevel@tonic-gate 			dp->disp_max_unbound_pri = -1;
7947c478bd9Sstevel@tonic-gate 			dp->disp_maxrunpri = -1;
7957c478bd9Sstevel@tonic-gate 		} else {
7967c478bd9Sstevel@tonic-gate 			int ipri;
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 			ipri = bt_gethighbit(dqactmap, maxrunword);
7997c478bd9Sstevel@tonic-gate 			dp->disp_maxrunpri = ipri;
8007c478bd9Sstevel@tonic-gate 			if (ipri < dp->disp_max_unbound_pri)
8017c478bd9Sstevel@tonic-gate 				dp->disp_max_unbound_pri = ipri;
8027c478bd9Sstevel@tonic-gate 		}
8037c478bd9Sstevel@tonic-gate 	} else {
8047c478bd9Sstevel@tonic-gate 		tp->t_link = NULL;
8057c478bd9Sstevel@tonic-gate 	}
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	/*
8087c478bd9Sstevel@tonic-gate 	 * Set TS_DONT_SWAP flag to prevent another processor from swapping
8097c478bd9Sstevel@tonic-gate 	 * out this thread before we have a chance to run it.
8107c478bd9Sstevel@tonic-gate 	 * While running, it is protected against swapping by t_lock.
8117c478bd9Sstevel@tonic-gate 	 */
8127c478bd9Sstevel@tonic-gate 	tp->t_schedflag |= TS_DONT_SWAP;
8137c478bd9Sstevel@tonic-gate 	cpup->cpu_dispthread = tp;		/* protected by spl only */
8147c478bd9Sstevel@tonic-gate 	cpup->cpu_dispatch_pri = pri;
8157c478bd9Sstevel@tonic-gate 	ASSERT(pri == DISP_PRIO(tp));
8167c478bd9Sstevel@tonic-gate 	thread_onproc(tp, cpup);  		/* set t_state to TS_ONPROC */
8177c478bd9Sstevel@tonic-gate 	disp_lock_exit_high(&dp->disp_lock);	/* drop run queue lock */
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	ASSERT(tp != NULL);
8207c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_DISP, TR_DISP_END,
8217c478bd9Sstevel@tonic-gate 	    "disp_end:tid %p", tp);
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	if (disp_ratify(tp, kpq) == NULL)
8247c478bd9Sstevel@tonic-gate 		goto reschedule;
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 	return (tp);
8277c478bd9Sstevel@tonic-gate }
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate /*
8307c478bd9Sstevel@tonic-gate  * swtch()
8317c478bd9Sstevel@tonic-gate  *	Find best runnable thread and run it.
8327c478bd9Sstevel@tonic-gate  *	Called with the current thread already switched to a new state,
8337c478bd9Sstevel@tonic-gate  *	on a sleep queue, run queue, stopped, and not zombied.
8347c478bd9Sstevel@tonic-gate  *	May be called at any spl level less than or equal to LOCK_LEVEL.
8357c478bd9Sstevel@tonic-gate  *	Always drops spl to the base level (spl0()).
8367c478bd9Sstevel@tonic-gate  */
8377c478bd9Sstevel@tonic-gate void
8387c478bd9Sstevel@tonic-gate swtch()
8397c478bd9Sstevel@tonic-gate {
8407c478bd9Sstevel@tonic-gate 	kthread_t	*t = curthread;
8417c478bd9Sstevel@tonic-gate 	kthread_t	*next;
8427c478bd9Sstevel@tonic-gate 	cpu_t		*cp;
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_DISP, TR_SWTCH_START, "swtch_start");
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	if (t->t_flag & T_INTR_THREAD)
8477c478bd9Sstevel@tonic-gate 		cpu_intr_swtch_enter(t);
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	if (t->t_intr != NULL) {
8507c478bd9Sstevel@tonic-gate 		/*
8517c478bd9Sstevel@tonic-gate 		 * We are an interrupt thread.  Setup and return
8527c478bd9Sstevel@tonic-gate 		 * the interrupted thread to be resumed.
8537c478bd9Sstevel@tonic-gate 		 */
8547c478bd9Sstevel@tonic-gate 		(void) splhigh();	/* block other scheduler action */
8557c478bd9Sstevel@tonic-gate 		cp = CPU;		/* now protected against migration */
8567c478bd9Sstevel@tonic-gate 		ASSERT(CPU_ON_INTR(cp) == 0);	/* not called with PIL > 10 */
8577c478bd9Sstevel@tonic-gate 		CPU_STATS_ADDQ(cp, sys, pswitch, 1);
8587c478bd9Sstevel@tonic-gate 		CPU_STATS_ADDQ(cp, sys, intrblk, 1);
8597c478bd9Sstevel@tonic-gate 		next = thread_unpin();
8607c478bd9Sstevel@tonic-gate 		TRACE_0(TR_FAC_DISP, TR_RESUME_START, "resume_start");
8617c478bd9Sstevel@tonic-gate 		resume_from_intr(next);
8627c478bd9Sstevel@tonic-gate 	} else {
8637c478bd9Sstevel@tonic-gate #ifdef	DEBUG
8647c478bd9Sstevel@tonic-gate 		if (t->t_state == TS_ONPROC &&
8657c478bd9Sstevel@tonic-gate 		    t->t_disp_queue->disp_cpu == CPU &&
8667c478bd9Sstevel@tonic-gate 		    t->t_preempt == 0) {
8677c478bd9Sstevel@tonic-gate 			thread_lock(t);
8687c478bd9Sstevel@tonic-gate 			ASSERT(t->t_state != TS_ONPROC ||
8697c478bd9Sstevel@tonic-gate 			    t->t_disp_queue->disp_cpu != CPU ||
8707c478bd9Sstevel@tonic-gate 			    t->t_preempt != 0);	/* cannot migrate */
8717c478bd9Sstevel@tonic-gate 			thread_unlock_nopreempt(t);
8727c478bd9Sstevel@tonic-gate 		}
8737c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
8747c478bd9Sstevel@tonic-gate 		cp = CPU;
8757c478bd9Sstevel@tonic-gate 		next = disp();		/* returns with spl high */
8767c478bd9Sstevel@tonic-gate 		ASSERT(CPU_ON_INTR(cp) == 0);	/* not called with PIL > 10 */
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 		/* OK to steal anything left on run queue */
8797c478bd9Sstevel@tonic-gate 		cp->cpu_disp_flags &= ~CPU_DISP_DONTSTEAL;
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 		if (next != t) {
8827c478bd9Sstevel@tonic-gate 			if (t == cp->cpu_idle_thread) {
883fb2f18f8Sesaxe 				PG_NRUN_UPDATE(cp, 1);
8847c478bd9Sstevel@tonic-gate 			} else if (next == cp->cpu_idle_thread) {
885fb2f18f8Sesaxe 				PG_NRUN_UPDATE(cp, -1);
8867c478bd9Sstevel@tonic-gate 			}
8877c478bd9Sstevel@tonic-gate 
888f2bd4627Sjohansen 			/*
889f2bd4627Sjohansen 			 * If t was previously in the TS_ONPROC state,
890f2bd4627Sjohansen 			 * setfrontdq and setbackdq won't have set its t_waitrq.
891f2bd4627Sjohansen 			 * Since we now finally know that we're switching away
892f2bd4627Sjohansen 			 * from this thread, set its t_waitrq if it is on a run
893f2bd4627Sjohansen 			 * queue.
894f2bd4627Sjohansen 			 */
895f2bd4627Sjohansen 			if ((t->t_state == TS_RUN) && (t->t_waitrq == 0)) {
896f2bd4627Sjohansen 				t->t_waitrq = gethrtime_unscaled();
897f2bd4627Sjohansen 			}
898f2bd4627Sjohansen 
899f2bd4627Sjohansen 			/*
900f2bd4627Sjohansen 			 * restore mstate of thread that we are switching to
901f2bd4627Sjohansen 			 */
902f2bd4627Sjohansen 			restore_mstate(next);
903f2bd4627Sjohansen 
9047c478bd9Sstevel@tonic-gate 			CPU_STATS_ADDQ(cp, sys, pswitch, 1);
9057c478bd9Sstevel@tonic-gate 			cp->cpu_last_swtch = t->t_disp_time = lbolt;
9067c478bd9Sstevel@tonic-gate 			TRACE_0(TR_FAC_DISP, TR_RESUME_START, "resume_start");
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 			if (dtrace_vtime_active)
9097c478bd9Sstevel@tonic-gate 				dtrace_vtime_switch(next);
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 			resume(next);
9127c478bd9Sstevel@tonic-gate 			/*
9137c478bd9Sstevel@tonic-gate 			 * The TR_RESUME_END and TR_SWTCH_END trace points
9147c478bd9Sstevel@tonic-gate 			 * appear at the end of resume(), because we may not
9157c478bd9Sstevel@tonic-gate 			 * return here
9167c478bd9Sstevel@tonic-gate 			 */
9177c478bd9Sstevel@tonic-gate 		} else {
9187c478bd9Sstevel@tonic-gate 			if (t->t_flag & T_INTR_THREAD)
9197c478bd9Sstevel@tonic-gate 				cpu_intr_swtch_exit(t);
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 			DTRACE_SCHED(remain__cpu);
9227c478bd9Sstevel@tonic-gate 			TRACE_0(TR_FAC_DISP, TR_SWTCH_END, "swtch_end");
9237c478bd9Sstevel@tonic-gate 			(void) spl0();
9247c478bd9Sstevel@tonic-gate 		}
9257c478bd9Sstevel@tonic-gate 	}
9267c478bd9Sstevel@tonic-gate }
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate /*
9297c478bd9Sstevel@tonic-gate  * swtch_from_zombie()
9307c478bd9Sstevel@tonic-gate  *	Special case of swtch(), which allows checks for TS_ZOMB to be
9317c478bd9Sstevel@tonic-gate  *	eliminated from normal resume.
9327c478bd9Sstevel@tonic-gate  *	Find best runnable thread and run it.
9337c478bd9Sstevel@tonic-gate  *	Called with the current thread zombied.
9347c478bd9Sstevel@tonic-gate  *	Zombies cannot migrate, so CPU references are safe.
9357c478bd9Sstevel@tonic-gate  */
9367c478bd9Sstevel@tonic-gate void
9377c478bd9Sstevel@tonic-gate swtch_from_zombie()
9387c478bd9Sstevel@tonic-gate {
9397c478bd9Sstevel@tonic-gate 	kthread_t	*next;
9407c478bd9Sstevel@tonic-gate 	cpu_t		*cpu = CPU;
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_DISP, TR_SWTCH_START, "swtch_start");
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 	ASSERT(curthread->t_state == TS_ZOMB);
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 	next = disp();			/* returns with spl high */
9477c478bd9Sstevel@tonic-gate 	ASSERT(CPU_ON_INTR(CPU) == 0);	/* not called with PIL > 10 */
9487c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(CPU, sys, pswitch, 1);
9497c478bd9Sstevel@tonic-gate 	ASSERT(next != curthread);
9507c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_DISP, TR_RESUME_START, "resume_start");
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 	if (next == cpu->cpu_idle_thread)
953fb2f18f8Sesaxe 		PG_NRUN_UPDATE(cpu, -1);
9547c478bd9Sstevel@tonic-gate 
955f2bd4627Sjohansen 	restore_mstate(next);
956f2bd4627Sjohansen 
9577c478bd9Sstevel@tonic-gate 	if (dtrace_vtime_active)
9587c478bd9Sstevel@tonic-gate 		dtrace_vtime_switch(next);
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	resume_from_zombie(next);
9617c478bd9Sstevel@tonic-gate 	/*
9627c478bd9Sstevel@tonic-gate 	 * The TR_RESUME_END and TR_SWTCH_END trace points
9637c478bd9Sstevel@tonic-gate 	 * appear at the end of resume(), because we certainly will not
9647c478bd9Sstevel@tonic-gate 	 * return here
9657c478bd9Sstevel@tonic-gate 	 */
9667c478bd9Sstevel@tonic-gate }
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate #if defined(DEBUG) && (defined(DISP_DEBUG) || defined(lint))
9697c478bd9Sstevel@tonic-gate 
970057452c6Sjj209869 /*
971057452c6Sjj209869  * search_disp_queues()
972057452c6Sjj209869  *	Search the given dispatch queues for thread tp.
973057452c6Sjj209869  *	Return 1 if tp is found, otherwise return 0.
974057452c6Sjj209869  */
975057452c6Sjj209869 static int
976057452c6Sjj209869 search_disp_queues(disp_t *dp, kthread_t *tp)
977057452c6Sjj209869 {
9787c478bd9Sstevel@tonic-gate 	dispq_t		*dq;
9797c478bd9Sstevel@tonic-gate 	dispq_t		*eq;
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 	disp_lock_enter_high(&dp->disp_lock);
982057452c6Sjj209869 
9837c478bd9Sstevel@tonic-gate 	for (dq = dp->disp_q, eq = dp->disp_q_limit; dq < eq; ++dq) {
9847c478bd9Sstevel@tonic-gate 		kthread_t	*rp;
9857c478bd9Sstevel@tonic-gate 
986057452c6Sjj209869 		ASSERT(dq->dq_last == NULL || dq->dq_last->t_link == NULL);
987057452c6Sjj209869 
9887c478bd9Sstevel@tonic-gate 		for (rp = dq->dq_first; rp; rp = rp->t_link)
9897c478bd9Sstevel@tonic-gate 			if (tp == rp) {
9907c478bd9Sstevel@tonic-gate 				disp_lock_exit_high(&dp->disp_lock);
9917c478bd9Sstevel@tonic-gate 				return (1);
9927c478bd9Sstevel@tonic-gate 			}
9937c478bd9Sstevel@tonic-gate 	}
9947c478bd9Sstevel@tonic-gate 	disp_lock_exit_high(&dp->disp_lock);
995057452c6Sjj209869 
9967c478bd9Sstevel@tonic-gate 	return (0);
997057452c6Sjj209869 }
998057452c6Sjj209869 
999057452c6Sjj209869 /*
1000057452c6Sjj209869  * thread_on_queue()
1001057452c6Sjj209869  *	Search all per-CPU dispatch queues and all partition-wide kpreempt
1002057452c6Sjj209869  *	queues for thread tp. Return 1 if tp is found, otherwise return 0.
1003057452c6Sjj209869  */
1004057452c6Sjj209869 static int
1005057452c6Sjj209869 thread_on_queue(kthread_t *tp)
1006057452c6Sjj209869 {
1007057452c6Sjj209869 	cpu_t		*cp;
1008057452c6Sjj209869 	struct cpupart	*part;
1009057452c6Sjj209869 
1010057452c6Sjj209869 	ASSERT(getpil() >= DISP_LEVEL);
1011057452c6Sjj209869 
1012057452c6Sjj209869 	/*
1013057452c6Sjj209869 	 * Search the per-CPU dispatch queues for tp.
1014057452c6Sjj209869 	 */
1015057452c6Sjj209869 	cp = CPU;
1016057452c6Sjj209869 	do {
1017057452c6Sjj209869 		if (search_disp_queues(cp->cpu_disp, tp))
1018057452c6Sjj209869 			return (1);
1019057452c6Sjj209869 	} while ((cp = cp->cpu_next_onln) != CPU);
1020057452c6Sjj209869 
1021057452c6Sjj209869 	/*
1022057452c6Sjj209869 	 * Search the partition-wide kpreempt queues for tp.
1023057452c6Sjj209869 	 */
1024057452c6Sjj209869 	part = CPU->cpu_part;
1025057452c6Sjj209869 	do {
1026057452c6Sjj209869 		if (search_disp_queues(&part->cp_kp_queue, tp))
1027057452c6Sjj209869 			return (1);
1028057452c6Sjj209869 	} while ((part = part->cp_next) != CPU->cpu_part);
1029057452c6Sjj209869 
1030057452c6Sjj209869 	return (0);
1031057452c6Sjj209869 }
1032057452c6Sjj209869 
10337c478bd9Sstevel@tonic-gate #else
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate #define	thread_on_queue(tp)	0	/* ASSERT must be !thread_on_queue */
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate #endif  /* DEBUG */
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate /*
10407c478bd9Sstevel@tonic-gate  * like swtch(), but switch to a specified thread taken from another CPU.
10417c478bd9Sstevel@tonic-gate  *	called with spl high..
10427c478bd9Sstevel@tonic-gate  */
10437c478bd9Sstevel@tonic-gate void
10447c478bd9Sstevel@tonic-gate swtch_to(kthread_t *next)
10457c478bd9Sstevel@tonic-gate {
10467c478bd9Sstevel@tonic-gate 	cpu_t			*cp = CPU;
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_DISP, TR_SWTCH_START, "swtch_start");
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 	/*
10517c478bd9Sstevel@tonic-gate 	 * Update context switch statistics.
10527c478bd9Sstevel@tonic-gate 	 */
10537c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(cp, sys, pswitch, 1);
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_DISP, TR_RESUME_START, "resume_start");
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 	if (curthread == cp->cpu_idle_thread)
1058fb2f18f8Sesaxe 		PG_NRUN_UPDATE(cp, 1);
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	/* OK to steal anything left on run queue */
10617c478bd9Sstevel@tonic-gate 	cp->cpu_disp_flags &= ~CPU_DISP_DONTSTEAL;
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 	/* record last execution time */
10647c478bd9Sstevel@tonic-gate 	cp->cpu_last_swtch = curthread->t_disp_time = lbolt;
10657c478bd9Sstevel@tonic-gate 
1066f2bd4627Sjohansen 	/*
1067f2bd4627Sjohansen 	 * If t was previously in the TS_ONPROC state, setfrontdq and setbackdq
1068f2bd4627Sjohansen 	 * won't have set its t_waitrq.  Since we now finally know that we're
1069f2bd4627Sjohansen 	 * switching away from this thread, set its t_waitrq if it is on a run
1070f2bd4627Sjohansen 	 * queue.
1071f2bd4627Sjohansen 	 */
1072f2bd4627Sjohansen 	if ((curthread->t_state == TS_RUN) && (curthread->t_waitrq == 0)) {
1073f2bd4627Sjohansen 		curthread->t_waitrq = gethrtime_unscaled();
1074f2bd4627Sjohansen 	}
1075f2bd4627Sjohansen 
1076f2bd4627Sjohansen 	/* restore next thread to previously running microstate */
1077f2bd4627Sjohansen 	restore_mstate(next);
1078f2bd4627Sjohansen 
10797c478bd9Sstevel@tonic-gate 	if (dtrace_vtime_active)
10807c478bd9Sstevel@tonic-gate 		dtrace_vtime_switch(next);
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 	resume(next);
10837c478bd9Sstevel@tonic-gate 	/*
10847c478bd9Sstevel@tonic-gate 	 * The TR_RESUME_END and TR_SWTCH_END trace points
10857c478bd9Sstevel@tonic-gate 	 * appear at the end of resume(), because we may not
10867c478bd9Sstevel@tonic-gate 	 * return here
10877c478bd9Sstevel@tonic-gate 	 */
10887c478bd9Sstevel@tonic-gate }
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate #define	CPU_IDLING(pri)	((pri) == -1)
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate static void
10957c478bd9Sstevel@tonic-gate cpu_resched(cpu_t *cp, pri_t tpri)
10967c478bd9Sstevel@tonic-gate {
10977c478bd9Sstevel@tonic-gate 	int	call_poke_cpu = 0;
10987c478bd9Sstevel@tonic-gate 	pri_t   cpupri = cp->cpu_dispatch_pri;
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 	if (!CPU_IDLING(cpupri) && (cpupri < tpri)) {
11017c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_DISP, TR_CPU_RESCHED,
11027c478bd9Sstevel@tonic-gate 		    "CPU_RESCHED:Tpri %d Cpupri %d", tpri, cpupri);
11037c478bd9Sstevel@tonic-gate 		if (tpri >= upreemptpri && cp->cpu_runrun == 0) {
11047c478bd9Sstevel@tonic-gate 			cp->cpu_runrun = 1;
11057c478bd9Sstevel@tonic-gate 			aston(cp->cpu_dispthread);
11067c478bd9Sstevel@tonic-gate 			if (tpri < kpreemptpri && cp != CPU)
11077c478bd9Sstevel@tonic-gate 				call_poke_cpu = 1;
11087c478bd9Sstevel@tonic-gate 		}
11097c478bd9Sstevel@tonic-gate 		if (tpri >= kpreemptpri && cp->cpu_kprunrun == 0) {
11107c478bd9Sstevel@tonic-gate 			cp->cpu_kprunrun = 1;
11117c478bd9Sstevel@tonic-gate 			if (cp != CPU)
11127c478bd9Sstevel@tonic-gate 				call_poke_cpu = 1;
11137c478bd9Sstevel@tonic-gate 		}
11147c478bd9Sstevel@tonic-gate 	}
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 	/*
11177c478bd9Sstevel@tonic-gate 	 * Propagate cpu_runrun, and cpu_kprunrun to global visibility.
11187c478bd9Sstevel@tonic-gate 	 */
11197c478bd9Sstevel@tonic-gate 	membar_enter();
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 	if (call_poke_cpu)
11227c478bd9Sstevel@tonic-gate 		poke_cpu(cp->cpu_id);
11237c478bd9Sstevel@tonic-gate }
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate /*
1126fb2f18f8Sesaxe  * Perform multi-level CMT load balancing of running threads.
1127fb2f18f8Sesaxe  * tp is the thread being enqueued
1128fb2f18f8Sesaxe  * cp is the hint CPU (chosen by cpu_choose()).
11297c478bd9Sstevel@tonic-gate  */
11307c478bd9Sstevel@tonic-gate static cpu_t *
1131fb2f18f8Sesaxe cmt_balance(kthread_t *tp, cpu_t *cp)
11327c478bd9Sstevel@tonic-gate {
1133d129bde2Sesaxe 	int		hint, i, cpu, nsiblings;
1134fb2f18f8Sesaxe 	int		self = 0;
1135fb2f18f8Sesaxe 	group_t		*cmt_pgs, *siblings;
1136fb2f18f8Sesaxe 	pg_cmt_t	*pg, *pg_tmp, *tpg = NULL;
1137fb2f18f8Sesaxe 	int		pg_nrun, tpg_nrun;
1138fb2f18f8Sesaxe 	int		level = 0;
1139fb2f18f8Sesaxe 	cpu_t		*newcp;
11407c478bd9Sstevel@tonic-gate 
1141fb2f18f8Sesaxe 	ASSERT(THREAD_LOCK_HELD(tp));
11427c478bd9Sstevel@tonic-gate 
1143fb2f18f8Sesaxe 	cmt_pgs = &cp->cpu_pg->cmt_pgs;
1144fb2f18f8Sesaxe 
1145fb2f18f8Sesaxe 	if (GROUP_SIZE(cmt_pgs) == 0)
1146fb2f18f8Sesaxe 		return (cp);	/* nothing to do */
1147fb2f18f8Sesaxe 
1148fb2f18f8Sesaxe 	if (tp == curthread)
1149fb2f18f8Sesaxe 		self = 1;
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate 	/*
1152fb2f18f8Sesaxe 	 * Balance across siblings in the CPUs CMT lineage
11537c478bd9Sstevel@tonic-gate 	 */
11547c478bd9Sstevel@tonic-gate 	do {
1155fb2f18f8Sesaxe 		pg = GROUP_ACCESS(cmt_pgs, level);
11567c478bd9Sstevel@tonic-gate 
1157d129bde2Sesaxe 		siblings = pg->cmt_siblings;
1158d129bde2Sesaxe 		nsiblings = GROUP_SIZE(siblings);	/* self inclusive */
1159d129bde2Sesaxe 		if (nsiblings == 1)
1160d129bde2Sesaxe 			continue;	/* nobody to balance against */
1161d129bde2Sesaxe 
1162fb2f18f8Sesaxe 		pg_nrun = pg->cmt_nrunning;
1163fb2f18f8Sesaxe 		if (self &&
1164fb2f18f8Sesaxe 		    bitset_in_set(&pg->cmt_cpus_actv_set, CPU->cpu_seqid))
1165fb2f18f8Sesaxe 			pg_nrun--;	/* Ignore curthread's effect */
1166fb2f18f8Sesaxe 
1167fb2f18f8Sesaxe 		hint = pg->cmt_hint;
11687c478bd9Sstevel@tonic-gate 		/*
1169fb2f18f8Sesaxe 		 * Check for validity of the hint
1170fb2f18f8Sesaxe 		 * It should reference a valid sibling
11717c478bd9Sstevel@tonic-gate 		 */
1172d129bde2Sesaxe 		if (hint >= nsiblings)
1173fb2f18f8Sesaxe 			hint = pg->cmt_hint = 0;
1174fb2f18f8Sesaxe 		else
1175fb2f18f8Sesaxe 			pg->cmt_hint++;
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 		/*
1178fb2f18f8Sesaxe 		 * Find a balancing candidate from among our siblings
1179fb2f18f8Sesaxe 		 * "hint" is a hint for where to start looking
11807c478bd9Sstevel@tonic-gate 		 */
1181fb2f18f8Sesaxe 		i = hint;
1182fb2f18f8Sesaxe 		do {
1183d129bde2Sesaxe 			ASSERT(i < nsiblings);
1184fb2f18f8Sesaxe 			pg_tmp = GROUP_ACCESS(siblings, i);
1185fb2f18f8Sesaxe 
1186fb2f18f8Sesaxe 			/*
1187fb2f18f8Sesaxe 			 * The candidate must not be us, and must
1188fb2f18f8Sesaxe 			 * have some CPU resources in the thread's
1189fb2f18f8Sesaxe 			 * partition
1190fb2f18f8Sesaxe 			 */
1191fb2f18f8Sesaxe 			if (pg_tmp != pg &&
1192fb2f18f8Sesaxe 			    bitset_in_set(&tp->t_cpupart->cp_cmt_pgs,
1193fb2f18f8Sesaxe 			    ((pg_t *)pg_tmp)->pg_id)) {
1194fb2f18f8Sesaxe 				tpg = pg_tmp;
11957c478bd9Sstevel@tonic-gate 				break;
1196fb2f18f8Sesaxe 			}
11977c478bd9Sstevel@tonic-gate 
1198d129bde2Sesaxe 			if (++i >= nsiblings)
1199fb2f18f8Sesaxe 				i = 0;
1200fb2f18f8Sesaxe 		} while (i != hint);
1201fb2f18f8Sesaxe 
1202fb2f18f8Sesaxe 		if (!tpg)
1203fb2f18f8Sesaxe 			continue;	/* no candidates at this level */
1204fb2f18f8Sesaxe 
1205fb2f18f8Sesaxe 		/*
1206fb2f18f8Sesaxe 		 * Check if the balancing target is underloaded
1207fb2f18f8Sesaxe 		 * Decide to balance if the target is running fewer
1208fb2f18f8Sesaxe 		 * threads, or if it's running the same number of threads
1209fb2f18f8Sesaxe 		 * with more online CPUs
1210fb2f18f8Sesaxe 		 */
1211fb2f18f8Sesaxe 		tpg_nrun = tpg->cmt_nrunning;
1212fb2f18f8Sesaxe 		if (pg_nrun > tpg_nrun ||
1213fb2f18f8Sesaxe 		    (pg_nrun == tpg_nrun &&
1214fb2f18f8Sesaxe 		    (GROUP_SIZE(&tpg->cmt_cpus_actv) >
1215fb2f18f8Sesaxe 		    GROUP_SIZE(&pg->cmt_cpus_actv)))) {
1216fb2f18f8Sesaxe 			break;
1217fb2f18f8Sesaxe 		}
1218fb2f18f8Sesaxe 		tpg = NULL;
1219fb2f18f8Sesaxe 	} while (++level < GROUP_SIZE(cmt_pgs));
1220fb2f18f8Sesaxe 
1221fb2f18f8Sesaxe 
1222fb2f18f8Sesaxe 	if (tpg) {
1223fb2f18f8Sesaxe 		/*
1224fb2f18f8Sesaxe 		 * Select an idle CPU from the target PG
1225fb2f18f8Sesaxe 		 */
1226fb2f18f8Sesaxe 		for (cpu = 0; cpu < GROUP_SIZE(&tpg->cmt_cpus_actv); cpu++) {
1227fb2f18f8Sesaxe 			newcp = GROUP_ACCESS(&tpg->cmt_cpus_actv, cpu);
1228fb2f18f8Sesaxe 			if (newcp->cpu_part == tp->t_cpupart &&
1229fb2f18f8Sesaxe 			    newcp->cpu_dispatch_pri == -1) {
1230fb2f18f8Sesaxe 				cp = newcp;
1231fb2f18f8Sesaxe 				break;
1232fb2f18f8Sesaxe 			}
1233fb2f18f8Sesaxe 		}
1234fb2f18f8Sesaxe 	}
1235fb2f18f8Sesaxe 
12367c478bd9Sstevel@tonic-gate 	return (cp);
12377c478bd9Sstevel@tonic-gate }
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate /*
12407c478bd9Sstevel@tonic-gate  * setbackdq() keeps runqs balanced such that the difference in length
12417c478bd9Sstevel@tonic-gate  * between the chosen runq and the next one is no more than RUNQ_MAX_DIFF.
12427c478bd9Sstevel@tonic-gate  * For threads with priorities below RUNQ_MATCH_PRI levels, the runq's lengths
12437c478bd9Sstevel@tonic-gate  * must match.  When per-thread TS_RUNQMATCH flag is set, setbackdq() will
12447c478bd9Sstevel@tonic-gate  * try to keep runqs perfectly balanced regardless of the thread priority.
12457c478bd9Sstevel@tonic-gate  */
12467c478bd9Sstevel@tonic-gate #define	RUNQ_MATCH_PRI	16	/* pri below which queue lengths must match */
12477c478bd9Sstevel@tonic-gate #define	RUNQ_MAX_DIFF	2	/* maximum runq length difference */
12487c478bd9Sstevel@tonic-gate #define	RUNQ_LEN(cp, pri)	((cp)->cpu_disp->disp_q[pri].dq_sruncnt)
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate /*
12517c478bd9Sstevel@tonic-gate  * Put the specified thread on the back of the dispatcher
12527c478bd9Sstevel@tonic-gate  * queue corresponding to its current priority.
12537c478bd9Sstevel@tonic-gate  *
12547c478bd9Sstevel@tonic-gate  * Called with the thread in transition, onproc or stopped state
12557c478bd9Sstevel@tonic-gate  * and locked (transition implies locked) and at high spl.
12567c478bd9Sstevel@tonic-gate  * Returns with the thread in TS_RUN state and still locked.
12577c478bd9Sstevel@tonic-gate  */
12587c478bd9Sstevel@tonic-gate void
12597c478bd9Sstevel@tonic-gate setbackdq(kthread_t *tp)
12607c478bd9Sstevel@tonic-gate {
12617c478bd9Sstevel@tonic-gate 	dispq_t	*dq;
12627c478bd9Sstevel@tonic-gate 	disp_t		*dp;
12637c478bd9Sstevel@tonic-gate 	cpu_t		*cp;
12647c478bd9Sstevel@tonic-gate 	pri_t		tpri;
12657c478bd9Sstevel@tonic-gate 	int		bound;
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(tp));
12687c478bd9Sstevel@tonic-gate 	ASSERT((tp->t_schedflag & TS_ALLSTART) == 0);
12697c478bd9Sstevel@tonic-gate 	ASSERT(!thread_on_queue(tp));	/* make sure tp isn't on a runq */
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 	/*
12727c478bd9Sstevel@tonic-gate 	 * If thread is "swapped" or on the swap queue don't
12737c478bd9Sstevel@tonic-gate 	 * queue it, but wake sched.
12747c478bd9Sstevel@tonic-gate 	 */
12757c478bd9Sstevel@tonic-gate 	if ((tp->t_schedflag & (TS_LOAD | TS_ON_SWAPQ)) != TS_LOAD) {
12767c478bd9Sstevel@tonic-gate 		disp_swapped_setrun(tp);
12777c478bd9Sstevel@tonic-gate 		return;
12787c478bd9Sstevel@tonic-gate 	}
12797c478bd9Sstevel@tonic-gate 
1280abd41583Sgd209917 	if (tp->t_bound_cpu || tp->t_weakbound_cpu)
1281abd41583Sgd209917 		bound = 1;
1282abd41583Sgd209917 	else
1283abd41583Sgd209917 		bound = 0;
1284abd41583Sgd209917 
12857c478bd9Sstevel@tonic-gate 	tpri = DISP_PRIO(tp);
12867c478bd9Sstevel@tonic-gate 	if (ncpus == 1)
12877c478bd9Sstevel@tonic-gate 		cp = tp->t_cpu;
1288abd41583Sgd209917 	else if (!bound) {
12897c478bd9Sstevel@tonic-gate 		if (tpri >= kpqpri) {
12907c478bd9Sstevel@tonic-gate 			setkpdq(tp, SETKP_BACK);
12917c478bd9Sstevel@tonic-gate 			return;
12927c478bd9Sstevel@tonic-gate 		}
12937c478bd9Sstevel@tonic-gate 		/*
12947c478bd9Sstevel@tonic-gate 		 * Let cpu_choose suggest a CPU.
12957c478bd9Sstevel@tonic-gate 		 */
12967c478bd9Sstevel@tonic-gate 		cp = cpu_choose(tp, tpri);
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate 		if (tp->t_cpupart == cp->cpu_part) {
12997c478bd9Sstevel@tonic-gate 			int	qlen;
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate 			/*
1302fb2f18f8Sesaxe 			 * Perform any CMT load balancing
13037c478bd9Sstevel@tonic-gate 			 */
1304fb2f18f8Sesaxe 			cp = cmt_balance(tp, cp);
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 			/*
13077c478bd9Sstevel@tonic-gate 			 * Balance across the run queues
13087c478bd9Sstevel@tonic-gate 			 */
13097c478bd9Sstevel@tonic-gate 			qlen = RUNQ_LEN(cp, tpri);
13107c478bd9Sstevel@tonic-gate 			if (tpri >= RUNQ_MATCH_PRI &&
13117c478bd9Sstevel@tonic-gate 			    !(tp->t_schedflag & TS_RUNQMATCH))
13127c478bd9Sstevel@tonic-gate 				qlen -= RUNQ_MAX_DIFF;
13137c478bd9Sstevel@tonic-gate 			if (qlen > 0) {
1314685679f7Sakolb 				cpu_t *newcp;
13157c478bd9Sstevel@tonic-gate 
1316685679f7Sakolb 				if (tp->t_lpl->lpl_lgrpid == LGRP_ROOTID) {
1317685679f7Sakolb 					newcp = cp->cpu_next_part;
1318685679f7Sakolb 				} else if ((newcp = cp->cpu_next_lpl) == cp) {
1319685679f7Sakolb 					newcp = cp->cpu_next_part;
13207c478bd9Sstevel@tonic-gate 				}
1321685679f7Sakolb 
1322685679f7Sakolb 				if (RUNQ_LEN(newcp, tpri) < qlen) {
1323685679f7Sakolb 					DTRACE_PROBE3(runq__balance,
1324685679f7Sakolb 					    kthread_t *, tp,
1325685679f7Sakolb 					    cpu_t *, cp, cpu_t *, newcp);
1326685679f7Sakolb 					cp = newcp;
1327685679f7Sakolb 				}
13287c478bd9Sstevel@tonic-gate 			}
13297c478bd9Sstevel@tonic-gate 		} else {
13307c478bd9Sstevel@tonic-gate 			/*
13317c478bd9Sstevel@tonic-gate 			 * Migrate to a cpu in the new partition.
13327c478bd9Sstevel@tonic-gate 			 */
13337c478bd9Sstevel@tonic-gate 			cp = disp_lowpri_cpu(tp->t_cpupart->cp_cpulist,
13347c478bd9Sstevel@tonic-gate 			    tp->t_lpl, tp->t_pri, NULL);
13357c478bd9Sstevel@tonic-gate 		}
13367c478bd9Sstevel@tonic-gate 		ASSERT((cp->cpu_flags & CPU_QUIESCED) == 0);
13377c478bd9Sstevel@tonic-gate 	} else {
13387c478bd9Sstevel@tonic-gate 		/*
13397c478bd9Sstevel@tonic-gate 		 * It is possible that t_weakbound_cpu != t_bound_cpu (for
13407c478bd9Sstevel@tonic-gate 		 * a short time until weak binding that existed when the
13417c478bd9Sstevel@tonic-gate 		 * strong binding was established has dropped) so we must
13427c478bd9Sstevel@tonic-gate 		 * favour weak binding over strong.
13437c478bd9Sstevel@tonic-gate 		 */
13447c478bd9Sstevel@tonic-gate 		cp = tp->t_weakbound_cpu ?
13457c478bd9Sstevel@tonic-gate 		    tp->t_weakbound_cpu : tp->t_bound_cpu;
13467c478bd9Sstevel@tonic-gate 	}
1347f2bd4627Sjohansen 	/*
1348f2bd4627Sjohansen 	 * A thread that is ONPROC may be temporarily placed on the run queue
1349f2bd4627Sjohansen 	 * but then chosen to run again by disp.  If the thread we're placing on
1350f2bd4627Sjohansen 	 * the queue is in TS_ONPROC state, don't set its t_waitrq until a
1351f2bd4627Sjohansen 	 * replacement process is actually scheduled in swtch().  In this
1352f2bd4627Sjohansen 	 * situation, curthread is the only thread that could be in the ONPROC
1353f2bd4627Sjohansen 	 * state.
1354f2bd4627Sjohansen 	 */
1355f2bd4627Sjohansen 	if ((tp != curthread) && (tp->t_waitrq == 0)) {
1356f2bd4627Sjohansen 		hrtime_t curtime;
1357f2bd4627Sjohansen 
1358f2bd4627Sjohansen 		curtime = gethrtime_unscaled();
1359f2bd4627Sjohansen 		(void) cpu_update_pct(tp, curtime);
1360f2bd4627Sjohansen 		tp->t_waitrq = curtime;
1361f2bd4627Sjohansen 	} else {
1362f2bd4627Sjohansen 		(void) cpu_update_pct(tp, gethrtime_unscaled());
1363f2bd4627Sjohansen 	}
1364f2bd4627Sjohansen 
13657c478bd9Sstevel@tonic-gate 	dp = cp->cpu_disp;
13667c478bd9Sstevel@tonic-gate 	disp_lock_enter_high(&dp->disp_lock);
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate 	DTRACE_SCHED3(enqueue, kthread_t *, tp, disp_t *, dp, int, 0);
13697c478bd9Sstevel@tonic-gate 	TRACE_3(TR_FAC_DISP, TR_BACKQ, "setbackdq:pri %d cpu %p tid %p",
13707c478bd9Sstevel@tonic-gate 	    tpri, cp, tp);
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate #ifndef NPROBE
13737c478bd9Sstevel@tonic-gate 	/* Kernel probe */
13747c478bd9Sstevel@tonic-gate 	if (tnf_tracing_active)
13757c478bd9Sstevel@tonic-gate 		tnf_thread_queue(tp, cp, tpri);
13767c478bd9Sstevel@tonic-gate #endif /* NPROBE */
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate 	ASSERT(tpri >= 0 && tpri < dp->disp_npri);
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate 	THREAD_RUN(tp, &dp->disp_lock);		/* set t_state to TS_RUN */
13817c478bd9Sstevel@tonic-gate 	tp->t_disp_queue = dp;
13827c478bd9Sstevel@tonic-gate 	tp->t_link = NULL;
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate 	dq = &dp->disp_q[tpri];
13857c478bd9Sstevel@tonic-gate 	dp->disp_nrunnable++;
1386685679f7Sakolb 	if (!bound)
1387685679f7Sakolb 		dp->disp_steal = 0;
13887c478bd9Sstevel@tonic-gate 	membar_enter();
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate 	if (dq->dq_sruncnt++ != 0) {
13917c478bd9Sstevel@tonic-gate 		ASSERT(dq->dq_first != NULL);
13927c478bd9Sstevel@tonic-gate 		dq->dq_last->t_link = tp;
13937c478bd9Sstevel@tonic-gate 		dq->dq_last = tp;
13947c478bd9Sstevel@tonic-gate 	} else {
13957c478bd9Sstevel@tonic-gate 		ASSERT(dq->dq_first == NULL);
13967c478bd9Sstevel@tonic-gate 		ASSERT(dq->dq_last == NULL);
13977c478bd9Sstevel@tonic-gate 		dq->dq_first = dq->dq_last = tp;
13987c478bd9Sstevel@tonic-gate 		BT_SET(dp->disp_qactmap, tpri);
13997c478bd9Sstevel@tonic-gate 		if (tpri > dp->disp_maxrunpri) {
14007c478bd9Sstevel@tonic-gate 			dp->disp_maxrunpri = tpri;
14017c478bd9Sstevel@tonic-gate 			membar_enter();
14027c478bd9Sstevel@tonic-gate 			cpu_resched(cp, tpri);
14037c478bd9Sstevel@tonic-gate 		}
14047c478bd9Sstevel@tonic-gate 	}
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate 	if (!bound && tpri > dp->disp_max_unbound_pri) {
14077c478bd9Sstevel@tonic-gate 		if (tp == curthread && dp->disp_max_unbound_pri == -1 &&
14087c478bd9Sstevel@tonic-gate 		    cp == CPU) {
14097c478bd9Sstevel@tonic-gate 			/*
14107c478bd9Sstevel@tonic-gate 			 * If there are no other unbound threads on the
14117c478bd9Sstevel@tonic-gate 			 * run queue, don't allow other CPUs to steal
14127c478bd9Sstevel@tonic-gate 			 * this thread while we are in the middle of a
14137c478bd9Sstevel@tonic-gate 			 * context switch. We may just switch to it
14147c478bd9Sstevel@tonic-gate 			 * again right away. CPU_DISP_DONTSTEAL is cleared
14157c478bd9Sstevel@tonic-gate 			 * in swtch and swtch_to.
14167c478bd9Sstevel@tonic-gate 			 */
14177c478bd9Sstevel@tonic-gate 			cp->cpu_disp_flags |= CPU_DISP_DONTSTEAL;
14187c478bd9Sstevel@tonic-gate 		}
14197c478bd9Sstevel@tonic-gate 		dp->disp_max_unbound_pri = tpri;
14207c478bd9Sstevel@tonic-gate 	}
14217c478bd9Sstevel@tonic-gate 	(*disp_enq_thread)(cp, bound);
14227c478bd9Sstevel@tonic-gate }
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate /*
14257c478bd9Sstevel@tonic-gate  * Put the specified thread on the front of the dispatcher
14267c478bd9Sstevel@tonic-gate  * queue corresponding to its current priority.
14277c478bd9Sstevel@tonic-gate  *
14287c478bd9Sstevel@tonic-gate  * Called with the thread in transition, onproc or stopped state
14297c478bd9Sstevel@tonic-gate  * and locked (transition implies locked) and at high spl.
14307c478bd9Sstevel@tonic-gate  * Returns with the thread in TS_RUN state and still locked.
14317c478bd9Sstevel@tonic-gate  */
14327c478bd9Sstevel@tonic-gate void
14337c478bd9Sstevel@tonic-gate setfrontdq(kthread_t *tp)
14347c478bd9Sstevel@tonic-gate {
14357c478bd9Sstevel@tonic-gate 	disp_t		*dp;
14367c478bd9Sstevel@tonic-gate 	dispq_t		*dq;
14377c478bd9Sstevel@tonic-gate 	cpu_t		*cp;
14387c478bd9Sstevel@tonic-gate 	pri_t		tpri;
14397c478bd9Sstevel@tonic-gate 	int		bound;
14407c478bd9Sstevel@tonic-gate 
14417c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(tp));
14427c478bd9Sstevel@tonic-gate 	ASSERT((tp->t_schedflag & TS_ALLSTART) == 0);
14437c478bd9Sstevel@tonic-gate 	ASSERT(!thread_on_queue(tp));	/* make sure tp isn't on a runq */
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 	/*
14467c478bd9Sstevel@tonic-gate 	 * If thread is "swapped" or on the swap queue don't
14477c478bd9Sstevel@tonic-gate 	 * queue it, but wake sched.
14487c478bd9Sstevel@tonic-gate 	 */
14497c478bd9Sstevel@tonic-gate 	if ((tp->t_schedflag & (TS_LOAD | TS_ON_SWAPQ)) != TS_LOAD) {
14507c478bd9Sstevel@tonic-gate 		disp_swapped_setrun(tp);
14517c478bd9Sstevel@tonic-gate 		return;
14527c478bd9Sstevel@tonic-gate 	}
14537c478bd9Sstevel@tonic-gate 
1454abd41583Sgd209917 	if (tp->t_bound_cpu || tp->t_weakbound_cpu)
1455abd41583Sgd209917 		bound = 1;
1456abd41583Sgd209917 	else
1457abd41583Sgd209917 		bound = 0;
1458abd41583Sgd209917 
14597c478bd9Sstevel@tonic-gate 	tpri = DISP_PRIO(tp);
14607c478bd9Sstevel@tonic-gate 	if (ncpus == 1)
14617c478bd9Sstevel@tonic-gate 		cp = tp->t_cpu;
1462abd41583Sgd209917 	else if (!bound) {
14637c478bd9Sstevel@tonic-gate 		if (tpri >= kpqpri) {
14647c478bd9Sstevel@tonic-gate 			setkpdq(tp, SETKP_FRONT);
14657c478bd9Sstevel@tonic-gate 			return;
14667c478bd9Sstevel@tonic-gate 		}
14677c478bd9Sstevel@tonic-gate 		cp = tp->t_cpu;
14687c478bd9Sstevel@tonic-gate 		if (tp->t_cpupart == cp->cpu_part) {
14697c478bd9Sstevel@tonic-gate 			/*
14707c478bd9Sstevel@tonic-gate 			 * If we are of higher or equal priority than
14717c478bd9Sstevel@tonic-gate 			 * the highest priority runnable thread of
14727c478bd9Sstevel@tonic-gate 			 * the current CPU, just pick this CPU.  Otherwise
14737c478bd9Sstevel@tonic-gate 			 * Let cpu_choose() select the CPU.  If this cpu
14747c478bd9Sstevel@tonic-gate 			 * is the target of an offline request then do not
14757c478bd9Sstevel@tonic-gate 			 * pick it - a thread_nomigrate() on the in motion
14767c478bd9Sstevel@tonic-gate 			 * cpu relies on this when it forces a preempt.
14777c478bd9Sstevel@tonic-gate 			 */
14787c478bd9Sstevel@tonic-gate 			if (tpri < cp->cpu_disp->disp_maxrunpri ||
14797c478bd9Sstevel@tonic-gate 			    cp == cpu_inmotion)
14807c478bd9Sstevel@tonic-gate 				cp = cpu_choose(tp, tpri);
14817c478bd9Sstevel@tonic-gate 		} else {
14827c478bd9Sstevel@tonic-gate 			/*
14837c478bd9Sstevel@tonic-gate 			 * Migrate to a cpu in the new partition.
14847c478bd9Sstevel@tonic-gate 			 */
14857c478bd9Sstevel@tonic-gate 			cp = disp_lowpri_cpu(tp->t_cpupart->cp_cpulist,
14867c478bd9Sstevel@tonic-gate 			    tp->t_lpl, tp->t_pri, NULL);
14877c478bd9Sstevel@tonic-gate 		}
14887c478bd9Sstevel@tonic-gate 		ASSERT((cp->cpu_flags & CPU_QUIESCED) == 0);
14897c478bd9Sstevel@tonic-gate 	} else {
14907c478bd9Sstevel@tonic-gate 		/*
14917c478bd9Sstevel@tonic-gate 		 * It is possible that t_weakbound_cpu != t_bound_cpu (for
14927c478bd9Sstevel@tonic-gate 		 * a short time until weak binding that existed when the
14937c478bd9Sstevel@tonic-gate 		 * strong binding was established has dropped) so we must
14947c478bd9Sstevel@tonic-gate 		 * favour weak binding over strong.
14957c478bd9Sstevel@tonic-gate 		 */
14967c478bd9Sstevel@tonic-gate 		cp = tp->t_weakbound_cpu ?
14977c478bd9Sstevel@tonic-gate 		    tp->t_weakbound_cpu : tp->t_bound_cpu;
14987c478bd9Sstevel@tonic-gate 	}
1499f2bd4627Sjohansen 
1500f2bd4627Sjohansen 	/*
1501f2bd4627Sjohansen 	 * A thread that is ONPROC may be temporarily placed on the run queue
1502f2bd4627Sjohansen 	 * but then chosen to run again by disp.  If the thread we're placing on
1503f2bd4627Sjohansen 	 * the queue is in TS_ONPROC state, don't set its t_waitrq until a
1504f2bd4627Sjohansen 	 * replacement process is actually scheduled in swtch().  In this
1505f2bd4627Sjohansen 	 * situation, curthread is the only thread that could be in the ONPROC
1506f2bd4627Sjohansen 	 * state.
1507f2bd4627Sjohansen 	 */
1508f2bd4627Sjohansen 	if ((tp != curthread) && (tp->t_waitrq == 0)) {
1509f2bd4627Sjohansen 		hrtime_t curtime;
1510f2bd4627Sjohansen 
1511f2bd4627Sjohansen 		curtime = gethrtime_unscaled();
1512f2bd4627Sjohansen 		(void) cpu_update_pct(tp, curtime);
1513f2bd4627Sjohansen 		tp->t_waitrq = curtime;
1514f2bd4627Sjohansen 	} else {
1515f2bd4627Sjohansen 		(void) cpu_update_pct(tp, gethrtime_unscaled());
1516f2bd4627Sjohansen 	}
1517f2bd4627Sjohansen 
15187c478bd9Sstevel@tonic-gate 	dp = cp->cpu_disp;
15197c478bd9Sstevel@tonic-gate 	disp_lock_enter_high(&dp->disp_lock);
15207c478bd9Sstevel@tonic-gate 
15217c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_DISP, TR_FRONTQ, "frontq:pri %d tid %p", tpri, tp);
15227c478bd9Sstevel@tonic-gate 	DTRACE_SCHED3(enqueue, kthread_t *, tp, disp_t *, dp, int, 1);
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate #ifndef NPROBE
15257c478bd9Sstevel@tonic-gate 	/* Kernel probe */
15267c478bd9Sstevel@tonic-gate 	if (tnf_tracing_active)
15277c478bd9Sstevel@tonic-gate 		tnf_thread_queue(tp, cp, tpri);
15287c478bd9Sstevel@tonic-gate #endif /* NPROBE */
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate 	ASSERT(tpri >= 0 && tpri < dp->disp_npri);
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 	THREAD_RUN(tp, &dp->disp_lock);		/* set TS_RUN state and lock */
15337c478bd9Sstevel@tonic-gate 	tp->t_disp_queue = dp;
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate 	dq = &dp->disp_q[tpri];
15367c478bd9Sstevel@tonic-gate 	dp->disp_nrunnable++;
1537685679f7Sakolb 	if (!bound)
1538685679f7Sakolb 		dp->disp_steal = 0;
15397c478bd9Sstevel@tonic-gate 	membar_enter();
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 	if (dq->dq_sruncnt++ != 0) {
15427c478bd9Sstevel@tonic-gate 		ASSERT(dq->dq_last != NULL);
15437c478bd9Sstevel@tonic-gate 		tp->t_link = dq->dq_first;
15447c478bd9Sstevel@tonic-gate 		dq->dq_first = tp;
15457c478bd9Sstevel@tonic-gate 	} else {
15467c478bd9Sstevel@tonic-gate 		ASSERT(dq->dq_last == NULL);
15477c478bd9Sstevel@tonic-gate 		ASSERT(dq->dq_first == NULL);
15487c478bd9Sstevel@tonic-gate 		tp->t_link = NULL;
15497c478bd9Sstevel@tonic-gate 		dq->dq_first = dq->dq_last = tp;
15507c478bd9Sstevel@tonic-gate 		BT_SET(dp->disp_qactmap, tpri);
15517c478bd9Sstevel@tonic-gate 		if (tpri > dp->disp_maxrunpri) {
15527c478bd9Sstevel@tonic-gate 			dp->disp_maxrunpri = tpri;
15537c478bd9Sstevel@tonic-gate 			membar_enter();
15547c478bd9Sstevel@tonic-gate 			cpu_resched(cp, tpri);
15557c478bd9Sstevel@tonic-gate 		}
15567c478bd9Sstevel@tonic-gate 	}
15577c478bd9Sstevel@tonic-gate 
15587c478bd9Sstevel@tonic-gate 	if (!bound && tpri > dp->disp_max_unbound_pri) {
15597c478bd9Sstevel@tonic-gate 		if (tp == curthread && dp->disp_max_unbound_pri == -1 &&
15607c478bd9Sstevel@tonic-gate 		    cp == CPU) {
15617c478bd9Sstevel@tonic-gate 			/*
15627c478bd9Sstevel@tonic-gate 			 * If there are no other unbound threads on the
15637c478bd9Sstevel@tonic-gate 			 * run queue, don't allow other CPUs to steal
15647c478bd9Sstevel@tonic-gate 			 * this thread while we are in the middle of a
15657c478bd9Sstevel@tonic-gate 			 * context switch. We may just switch to it
15667c478bd9Sstevel@tonic-gate 			 * again right away. CPU_DISP_DONTSTEAL is cleared
15677c478bd9Sstevel@tonic-gate 			 * in swtch and swtch_to.
15687c478bd9Sstevel@tonic-gate 			 */
15697c478bd9Sstevel@tonic-gate 			cp->cpu_disp_flags |= CPU_DISP_DONTSTEAL;
15707c478bd9Sstevel@tonic-gate 		}
15717c478bd9Sstevel@tonic-gate 		dp->disp_max_unbound_pri = tpri;
15727c478bd9Sstevel@tonic-gate 	}
15737c478bd9Sstevel@tonic-gate 	(*disp_enq_thread)(cp, bound);
15747c478bd9Sstevel@tonic-gate }
15757c478bd9Sstevel@tonic-gate 
15767c478bd9Sstevel@tonic-gate /*
15777c478bd9Sstevel@tonic-gate  * Put a high-priority unbound thread on the kp queue
15787c478bd9Sstevel@tonic-gate  */
15797c478bd9Sstevel@tonic-gate static void
15807c478bd9Sstevel@tonic-gate setkpdq(kthread_t *tp, int borf)
15817c478bd9Sstevel@tonic-gate {
15827c478bd9Sstevel@tonic-gate 	dispq_t	*dq;
15837c478bd9Sstevel@tonic-gate 	disp_t	*dp;
15847c478bd9Sstevel@tonic-gate 	cpu_t	*cp;
15857c478bd9Sstevel@tonic-gate 	pri_t	tpri;
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 	tpri = DISP_PRIO(tp);
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate 	dp = &tp->t_cpupart->cp_kp_queue;
15907c478bd9Sstevel@tonic-gate 	disp_lock_enter_high(&dp->disp_lock);
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_DISP, TR_FRONTQ, "frontq:pri %d tid %p", tpri, tp);
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate 	ASSERT(tpri >= 0 && tpri < dp->disp_npri);
15957c478bd9Sstevel@tonic-gate 	DTRACE_SCHED3(enqueue, kthread_t *, tp, disp_t *, dp, int, borf);
15967c478bd9Sstevel@tonic-gate 	THREAD_RUN(tp, &dp->disp_lock);		/* set t_state to TS_RUN */
15977c478bd9Sstevel@tonic-gate 	tp->t_disp_queue = dp;
15987c478bd9Sstevel@tonic-gate 	dp->disp_nrunnable++;
15997c478bd9Sstevel@tonic-gate 	dq = &dp->disp_q[tpri];
16007c478bd9Sstevel@tonic-gate 
16017c478bd9Sstevel@tonic-gate 	if (dq->dq_sruncnt++ != 0) {
16027c478bd9Sstevel@tonic-gate 		if (borf == SETKP_BACK) {
16037c478bd9Sstevel@tonic-gate 			ASSERT(dq->dq_first != NULL);
16047c478bd9Sstevel@tonic-gate 			tp->t_link = NULL;
16057c478bd9Sstevel@tonic-gate 			dq->dq_last->t_link = tp;
16067c478bd9Sstevel@tonic-gate 			dq->dq_last = tp;
16077c478bd9Sstevel@tonic-gate 		} else {
16087c478bd9Sstevel@tonic-gate 			ASSERT(dq->dq_last != NULL);
16097c478bd9Sstevel@tonic-gate 			tp->t_link = dq->dq_first;
16107c478bd9Sstevel@tonic-gate 			dq->dq_first = tp;
16117c478bd9Sstevel@tonic-gate 		}
16127c478bd9Sstevel@tonic-gate 	} else {
16137c478bd9Sstevel@tonic-gate 		if (borf == SETKP_BACK) {
16147c478bd9Sstevel@tonic-gate 			ASSERT(dq->dq_first == NULL);
16157c478bd9Sstevel@tonic-gate 			ASSERT(dq->dq_last == NULL);
16167c478bd9Sstevel@tonic-gate 			dq->dq_first = dq->dq_last = tp;
16177c478bd9Sstevel@tonic-gate 		} else {
16187c478bd9Sstevel@tonic-gate 			ASSERT(dq->dq_last == NULL);
16197c478bd9Sstevel@tonic-gate 			ASSERT(dq->dq_first == NULL);
16207c478bd9Sstevel@tonic-gate 			tp->t_link = NULL;
16217c478bd9Sstevel@tonic-gate 			dq->dq_first = dq->dq_last = tp;
16227c478bd9Sstevel@tonic-gate 		}
16237c478bd9Sstevel@tonic-gate 		BT_SET(dp->disp_qactmap, tpri);
16247c478bd9Sstevel@tonic-gate 		if (tpri > dp->disp_max_unbound_pri)
16257c478bd9Sstevel@tonic-gate 			dp->disp_max_unbound_pri = tpri;
16267c478bd9Sstevel@tonic-gate 		if (tpri > dp->disp_maxrunpri) {
16277c478bd9Sstevel@tonic-gate 			dp->disp_maxrunpri = tpri;
16287c478bd9Sstevel@tonic-gate 			membar_enter();
16297c478bd9Sstevel@tonic-gate 		}
16307c478bd9Sstevel@tonic-gate 	}
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate 	cp = tp->t_cpu;
16337c478bd9Sstevel@tonic-gate 	if (tp->t_cpupart != cp->cpu_part) {
16347c478bd9Sstevel@tonic-gate 		/* migrate to a cpu in the new partition */
16357c478bd9Sstevel@tonic-gate 		cp = tp->t_cpupart->cp_cpulist;
16367c478bd9Sstevel@tonic-gate 	}
16377c478bd9Sstevel@tonic-gate 	cp = disp_lowpri_cpu(cp, tp->t_lpl, tp->t_pri, NULL);
16387c478bd9Sstevel@tonic-gate 	disp_lock_enter_high(&cp->cpu_disp->disp_lock);
16397c478bd9Sstevel@tonic-gate 	ASSERT((cp->cpu_flags & CPU_QUIESCED) == 0);
16407c478bd9Sstevel@tonic-gate 
16417c478bd9Sstevel@tonic-gate #ifndef NPROBE
16427c478bd9Sstevel@tonic-gate 	/* Kernel probe */
16437c478bd9Sstevel@tonic-gate 	if (tnf_tracing_active)
16447c478bd9Sstevel@tonic-gate 		tnf_thread_queue(tp, cp, tpri);
16457c478bd9Sstevel@tonic-gate #endif /* NPROBE */
16467c478bd9Sstevel@tonic-gate 
16477c478bd9Sstevel@tonic-gate 	if (cp->cpu_chosen_level < tpri)
16487c478bd9Sstevel@tonic-gate 		cp->cpu_chosen_level = tpri;
16497c478bd9Sstevel@tonic-gate 	cpu_resched(cp, tpri);
16507c478bd9Sstevel@tonic-gate 	disp_lock_exit_high(&cp->cpu_disp->disp_lock);
16517c478bd9Sstevel@tonic-gate 	(*disp_enq_thread)(cp, 0);
16527c478bd9Sstevel@tonic-gate }
16537c478bd9Sstevel@tonic-gate 
16547c478bd9Sstevel@tonic-gate /*
16557c478bd9Sstevel@tonic-gate  * Remove a thread from the dispatcher queue if it is on it.
16567c478bd9Sstevel@tonic-gate  * It is not an error if it is not found but we return whether
16577c478bd9Sstevel@tonic-gate  * or not it was found in case the caller wants to check.
16587c478bd9Sstevel@tonic-gate  */
16597c478bd9Sstevel@tonic-gate int
16607c478bd9Sstevel@tonic-gate dispdeq(kthread_t *tp)
16617c478bd9Sstevel@tonic-gate {
16627c478bd9Sstevel@tonic-gate 	disp_t		*dp;
16637c478bd9Sstevel@tonic-gate 	dispq_t		*dq;
16647c478bd9Sstevel@tonic-gate 	kthread_t	*rp;
16657c478bd9Sstevel@tonic-gate 	kthread_t	*trp;
16667c478bd9Sstevel@tonic-gate 	kthread_t	**ptp;
16677c478bd9Sstevel@tonic-gate 	int		tpri;
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(tp));
16707c478bd9Sstevel@tonic-gate 
16717c478bd9Sstevel@tonic-gate 	if (tp->t_state != TS_RUN)
16727c478bd9Sstevel@tonic-gate 		return (0);
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 	/*
16757c478bd9Sstevel@tonic-gate 	 * The thread is "swapped" or is on the swap queue and
16767c478bd9Sstevel@tonic-gate 	 * hence no longer on the run queue, so return true.
16777c478bd9Sstevel@tonic-gate 	 */
16787c478bd9Sstevel@tonic-gate 	if ((tp->t_schedflag & (TS_LOAD | TS_ON_SWAPQ)) != TS_LOAD)
16797c478bd9Sstevel@tonic-gate 		return (1);
16807c478bd9Sstevel@tonic-gate 
16817c478bd9Sstevel@tonic-gate 	tpri = DISP_PRIO(tp);
16827c478bd9Sstevel@tonic-gate 	dp = tp->t_disp_queue;
16837c478bd9Sstevel@tonic-gate 	ASSERT(tpri < dp->disp_npri);
16847c478bd9Sstevel@tonic-gate 	dq = &dp->disp_q[tpri];
16857c478bd9Sstevel@tonic-gate 	ptp = &dq->dq_first;
16867c478bd9Sstevel@tonic-gate 	rp = *ptp;
16877c478bd9Sstevel@tonic-gate 	trp = NULL;
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate 	ASSERT(dq->dq_last == NULL || dq->dq_last->t_link == NULL);
16907c478bd9Sstevel@tonic-gate 
16917c478bd9Sstevel@tonic-gate 	/*
16927c478bd9Sstevel@tonic-gate 	 * Search for thread in queue.
16937c478bd9Sstevel@tonic-gate 	 * Double links would simplify this at the expense of disp/setrun.
16947c478bd9Sstevel@tonic-gate 	 */
16957c478bd9Sstevel@tonic-gate 	while (rp != tp && rp != NULL) {
16967c478bd9Sstevel@tonic-gate 		trp = rp;
16977c478bd9Sstevel@tonic-gate 		ptp = &trp->t_link;
16987c478bd9Sstevel@tonic-gate 		rp = trp->t_link;
16997c478bd9Sstevel@tonic-gate 	}
17007c478bd9Sstevel@tonic-gate 
17017c478bd9Sstevel@tonic-gate 	if (rp == NULL) {
17027c478bd9Sstevel@tonic-gate 		panic("dispdeq: thread not on queue");
17037c478bd9Sstevel@tonic-gate 	}
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate 	DTRACE_SCHED2(dequeue, kthread_t *, tp, disp_t *, dp);
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate 	/*
17087c478bd9Sstevel@tonic-gate 	 * Found it so remove it from queue.
17097c478bd9Sstevel@tonic-gate 	 */
17107c478bd9Sstevel@tonic-gate 	if ((*ptp = rp->t_link) == NULL)
17117c478bd9Sstevel@tonic-gate 		dq->dq_last = trp;
17127c478bd9Sstevel@tonic-gate 
17137c478bd9Sstevel@tonic-gate 	dp->disp_nrunnable--;
17147c478bd9Sstevel@tonic-gate 	if (--dq->dq_sruncnt == 0) {
17157c478bd9Sstevel@tonic-gate 		dp->disp_qactmap[tpri >> BT_ULSHIFT] &= ~BT_BIW(tpri);
17167c478bd9Sstevel@tonic-gate 		if (dp->disp_nrunnable == 0) {
17177c478bd9Sstevel@tonic-gate 			dp->disp_max_unbound_pri = -1;
17187c478bd9Sstevel@tonic-gate 			dp->disp_maxrunpri = -1;
17197c478bd9Sstevel@tonic-gate 		} else if (tpri == dp->disp_maxrunpri) {
17207c478bd9Sstevel@tonic-gate 			int ipri;
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate 			ipri = bt_gethighbit(dp->disp_qactmap,
17237c478bd9Sstevel@tonic-gate 			    dp->disp_maxrunpri >> BT_ULSHIFT);
17247c478bd9Sstevel@tonic-gate 			if (ipri < dp->disp_max_unbound_pri)
17257c478bd9Sstevel@tonic-gate 				dp->disp_max_unbound_pri = ipri;
17267c478bd9Sstevel@tonic-gate 			dp->disp_maxrunpri = ipri;
17277c478bd9Sstevel@tonic-gate 		}
17287c478bd9Sstevel@tonic-gate 	}
17297c478bd9Sstevel@tonic-gate 	tp->t_link = NULL;
17307c478bd9Sstevel@tonic-gate 	THREAD_TRANSITION(tp);		/* put in intermediate state */
17317c478bd9Sstevel@tonic-gate 	return (1);
17327c478bd9Sstevel@tonic-gate }
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate /*
17367c478bd9Sstevel@tonic-gate  * dq_sruninc and dq_srundec are public functions for
17377c478bd9Sstevel@tonic-gate  * incrementing/decrementing the sruncnts when a thread on
17387c478bd9Sstevel@tonic-gate  * a dispatcher queue is made schedulable/unschedulable by
17397c478bd9Sstevel@tonic-gate  * resetting the TS_LOAD flag.
17407c478bd9Sstevel@tonic-gate  *
17417c478bd9Sstevel@tonic-gate  * The caller MUST have the thread lock and therefore the dispatcher
17427c478bd9Sstevel@tonic-gate  * queue lock so that the operation which changes
17437c478bd9Sstevel@tonic-gate  * the flag, the operation that checks the status of the thread to
17447c478bd9Sstevel@tonic-gate  * determine if it's on a disp queue AND the call to this function
17457c478bd9Sstevel@tonic-gate  * are one atomic operation with respect to interrupts.
17467c478bd9Sstevel@tonic-gate  */
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate /*
17497c478bd9Sstevel@tonic-gate  * Called by sched AFTER TS_LOAD flag is set on a swapped, runnable thread.
17507c478bd9Sstevel@tonic-gate  */
17517c478bd9Sstevel@tonic-gate void
17527c478bd9Sstevel@tonic-gate dq_sruninc(kthread_t *t)
17537c478bd9Sstevel@tonic-gate {
17547c478bd9Sstevel@tonic-gate 	ASSERT(t->t_state == TS_RUN);
17557c478bd9Sstevel@tonic-gate 	ASSERT(t->t_schedflag & TS_LOAD);
17567c478bd9Sstevel@tonic-gate 
17577c478bd9Sstevel@tonic-gate 	THREAD_TRANSITION(t);
17587c478bd9Sstevel@tonic-gate 	setfrontdq(t);
17597c478bd9Sstevel@tonic-gate }
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate /*
17627c478bd9Sstevel@tonic-gate  * See comment on calling conventions above.
17637c478bd9Sstevel@tonic-gate  * Called by sched BEFORE TS_LOAD flag is cleared on a runnable thread.
17647c478bd9Sstevel@tonic-gate  */
17657c478bd9Sstevel@tonic-gate void
17667c478bd9Sstevel@tonic-gate dq_srundec(kthread_t *t)
17677c478bd9Sstevel@tonic-gate {
17687c478bd9Sstevel@tonic-gate 	ASSERT(t->t_schedflag & TS_LOAD);
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate 	(void) dispdeq(t);
17717c478bd9Sstevel@tonic-gate 	disp_swapped_enq(t);
17727c478bd9Sstevel@tonic-gate }
17737c478bd9Sstevel@tonic-gate 
17747c478bd9Sstevel@tonic-gate /*
17757c478bd9Sstevel@tonic-gate  * Change the dispatcher lock of thread to the "swapped_lock"
17767c478bd9Sstevel@tonic-gate  * and return with thread lock still held.
17777c478bd9Sstevel@tonic-gate  *
17787c478bd9Sstevel@tonic-gate  * Called with thread_lock held, in transition state, and at high spl.
17797c478bd9Sstevel@tonic-gate  */
17807c478bd9Sstevel@tonic-gate void
17817c478bd9Sstevel@tonic-gate disp_swapped_enq(kthread_t *tp)
17827c478bd9Sstevel@tonic-gate {
17837c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(tp));
17847c478bd9Sstevel@tonic-gate 	ASSERT(tp->t_schedflag & TS_LOAD);
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate 	switch (tp->t_state) {
17877c478bd9Sstevel@tonic-gate 	case TS_RUN:
17887c478bd9Sstevel@tonic-gate 		disp_lock_enter_high(&swapped_lock);
17897c478bd9Sstevel@tonic-gate 		THREAD_SWAP(tp, &swapped_lock);	/* set TS_RUN state and lock */
17907c478bd9Sstevel@tonic-gate 		break;
17917c478bd9Sstevel@tonic-gate 	case TS_ONPROC:
17927c478bd9Sstevel@tonic-gate 		disp_lock_enter_high(&swapped_lock);
17937c478bd9Sstevel@tonic-gate 		THREAD_TRANSITION(tp);
17947c478bd9Sstevel@tonic-gate 		wake_sched_sec = 1;		/* tell clock to wake sched */
17957c478bd9Sstevel@tonic-gate 		THREAD_SWAP(tp, &swapped_lock);	/* set TS_RUN state and lock */
17967c478bd9Sstevel@tonic-gate 		break;
17977c478bd9Sstevel@tonic-gate 	default:
17987c478bd9Sstevel@tonic-gate 		panic("disp_swapped: tp: %p bad t_state", (void *)tp);
17997c478bd9Sstevel@tonic-gate 	}
18007c478bd9Sstevel@tonic-gate }
18017c478bd9Sstevel@tonic-gate 
18027c478bd9Sstevel@tonic-gate /*
18037c478bd9Sstevel@tonic-gate  * This routine is called by setbackdq/setfrontdq if the thread is
18047c478bd9Sstevel@tonic-gate  * not loaded or loaded and on the swap queue.
18057c478bd9Sstevel@tonic-gate  *
18067c478bd9Sstevel@tonic-gate  * Thread state TS_SLEEP implies that a swapped thread
18077c478bd9Sstevel@tonic-gate  * has been woken up and needs to be swapped in by the swapper.
18087c478bd9Sstevel@tonic-gate  *
18097c478bd9Sstevel@tonic-gate  * Thread state TS_RUN, it implies that the priority of a swapped
18107c478bd9Sstevel@tonic-gate  * thread is being increased by scheduling class (e.g. ts_update).
18117c478bd9Sstevel@tonic-gate  */
18127c478bd9Sstevel@tonic-gate static void
18137c478bd9Sstevel@tonic-gate disp_swapped_setrun(kthread_t *tp)
18147c478bd9Sstevel@tonic-gate {
18157c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(tp));
18167c478bd9Sstevel@tonic-gate 	ASSERT((tp->t_schedflag & (TS_LOAD | TS_ON_SWAPQ)) != TS_LOAD);
18177c478bd9Sstevel@tonic-gate 
18187c478bd9Sstevel@tonic-gate 	switch (tp->t_state) {
18197c478bd9Sstevel@tonic-gate 	case TS_SLEEP:
18207c478bd9Sstevel@tonic-gate 		disp_lock_enter_high(&swapped_lock);
18217c478bd9Sstevel@tonic-gate 		/*
18227c478bd9Sstevel@tonic-gate 		 * Wakeup sched immediately (i.e., next tick) if the
18237c478bd9Sstevel@tonic-gate 		 * thread priority is above maxclsyspri.
18247c478bd9Sstevel@tonic-gate 		 */
18257c478bd9Sstevel@tonic-gate 		if (DISP_PRIO(tp) > maxclsyspri)
18267c478bd9Sstevel@tonic-gate 			wake_sched = 1;
18277c478bd9Sstevel@tonic-gate 		else
18287c478bd9Sstevel@tonic-gate 			wake_sched_sec = 1;
18297c478bd9Sstevel@tonic-gate 		THREAD_RUN(tp, &swapped_lock); /* set TS_RUN state and lock */
18307c478bd9Sstevel@tonic-gate 		break;
18317c478bd9Sstevel@tonic-gate 	case TS_RUN:				/* called from ts_update */
18327c478bd9Sstevel@tonic-gate 		break;
18337c478bd9Sstevel@tonic-gate 	default:
18347c478bd9Sstevel@tonic-gate 		panic("disp_swapped_setrun: tp: %p bad t_state", tp);
18357c478bd9Sstevel@tonic-gate 	}
18367c478bd9Sstevel@tonic-gate }
18377c478bd9Sstevel@tonic-gate 
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate /*
18407c478bd9Sstevel@tonic-gate  *	Make a thread give up its processor.  Find the processor on
18417c478bd9Sstevel@tonic-gate  *	which this thread is executing, and have that processor
18427c478bd9Sstevel@tonic-gate  *	preempt.
18437c478bd9Sstevel@tonic-gate  */
18447c478bd9Sstevel@tonic-gate void
18457c478bd9Sstevel@tonic-gate cpu_surrender(kthread_t *tp)
18467c478bd9Sstevel@tonic-gate {
18477c478bd9Sstevel@tonic-gate 	cpu_t	*cpup;
18487c478bd9Sstevel@tonic-gate 	int	max_pri;
18497c478bd9Sstevel@tonic-gate 	int	max_run_pri;
18507c478bd9Sstevel@tonic-gate 	klwp_t	*lwp;
18517c478bd9Sstevel@tonic-gate 
18527c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(tp));
18537c478bd9Sstevel@tonic-gate 
18547c478bd9Sstevel@tonic-gate 	if (tp->t_state != TS_ONPROC)
18557c478bd9Sstevel@tonic-gate 		return;
18567c478bd9Sstevel@tonic-gate 	cpup = tp->t_disp_queue->disp_cpu;	/* CPU thread dispatched to */
18577c478bd9Sstevel@tonic-gate 	max_pri = cpup->cpu_disp->disp_maxrunpri; /* best pri of that CPU */
18587c478bd9Sstevel@tonic-gate 	max_run_pri = CP_MAXRUNPRI(cpup->cpu_part);
18597c478bd9Sstevel@tonic-gate 	if (max_pri < max_run_pri)
18607c478bd9Sstevel@tonic-gate 		max_pri = max_run_pri;
18617c478bd9Sstevel@tonic-gate 
18627c478bd9Sstevel@tonic-gate 	cpup->cpu_runrun = 1;
18637c478bd9Sstevel@tonic-gate 	if (max_pri >= kpreemptpri && cpup->cpu_kprunrun == 0) {
18647c478bd9Sstevel@tonic-gate 		cpup->cpu_kprunrun = 1;
18657c478bd9Sstevel@tonic-gate 	}
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate 	/*
18687c478bd9Sstevel@tonic-gate 	 * Propagate cpu_runrun, and cpu_kprunrun to global visibility.
18697c478bd9Sstevel@tonic-gate 	 */
18707c478bd9Sstevel@tonic-gate 	membar_enter();
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate 	DTRACE_SCHED1(surrender, kthread_t *, tp);
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate 	/*
18757c478bd9Sstevel@tonic-gate 	 * Make the target thread take an excursion through trap()
18767c478bd9Sstevel@tonic-gate 	 * to do preempt() (unless we're already in trap or post_syscall,
18777c478bd9Sstevel@tonic-gate 	 * calling cpu_surrender via CL_TRAPRET).
18787c478bd9Sstevel@tonic-gate 	 */
18797c478bd9Sstevel@tonic-gate 	if (tp != curthread || (lwp = tp->t_lwp) == NULL ||
18807c478bd9Sstevel@tonic-gate 	    lwp->lwp_state != LWP_USER) {
18817c478bd9Sstevel@tonic-gate 		aston(tp);
18827c478bd9Sstevel@tonic-gate 		if (cpup != CPU)
18837c478bd9Sstevel@tonic-gate 			poke_cpu(cpup->cpu_id);
18847c478bd9Sstevel@tonic-gate 	}
18857c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_DISP, TR_CPU_SURRENDER,
18867c478bd9Sstevel@tonic-gate 	    "cpu_surrender:tid %p cpu %p", tp, cpup);
18877c478bd9Sstevel@tonic-gate }
18887c478bd9Sstevel@tonic-gate 
18897c478bd9Sstevel@tonic-gate 
18907c478bd9Sstevel@tonic-gate /*
18917c478bd9Sstevel@tonic-gate  * Commit to and ratify a scheduling decision
18927c478bd9Sstevel@tonic-gate  */
18937c478bd9Sstevel@tonic-gate /*ARGSUSED*/
18947c478bd9Sstevel@tonic-gate static kthread_t *
18957c478bd9Sstevel@tonic-gate disp_ratify(kthread_t *tp, disp_t *kpq)
18967c478bd9Sstevel@tonic-gate {
18977c478bd9Sstevel@tonic-gate 	pri_t	tpri, maxpri;
18987c478bd9Sstevel@tonic-gate 	pri_t	maxkpri;
18997c478bd9Sstevel@tonic-gate 	cpu_t	*cpup;
19007c478bd9Sstevel@tonic-gate 
19017c478bd9Sstevel@tonic-gate 	ASSERT(tp != NULL);
19027c478bd9Sstevel@tonic-gate 	/*
19037c478bd9Sstevel@tonic-gate 	 * Commit to, then ratify scheduling decision
19047c478bd9Sstevel@tonic-gate 	 */
19057c478bd9Sstevel@tonic-gate 	cpup = CPU;
19067c478bd9Sstevel@tonic-gate 	if (cpup->cpu_runrun != 0)
19077c478bd9Sstevel@tonic-gate 		cpup->cpu_runrun = 0;
19087c478bd9Sstevel@tonic-gate 	if (cpup->cpu_kprunrun != 0)
19097c478bd9Sstevel@tonic-gate 		cpup->cpu_kprunrun = 0;
19107c478bd9Sstevel@tonic-gate 	if (cpup->cpu_chosen_level != -1)
19117c478bd9Sstevel@tonic-gate 		cpup->cpu_chosen_level = -1;
19127c478bd9Sstevel@tonic-gate 	membar_enter();
19137c478bd9Sstevel@tonic-gate 	tpri = DISP_PRIO(tp);
19147c478bd9Sstevel@tonic-gate 	maxpri = cpup->cpu_disp->disp_maxrunpri;
19157c478bd9Sstevel@tonic-gate 	maxkpri = kpq->disp_maxrunpri;
19167c478bd9Sstevel@tonic-gate 	if (maxpri < maxkpri)
19177c478bd9Sstevel@tonic-gate 		maxpri = maxkpri;
19187c478bd9Sstevel@tonic-gate 	if (tpri < maxpri) {
19197c478bd9Sstevel@tonic-gate 		/*
19207c478bd9Sstevel@tonic-gate 		 * should have done better
19217c478bd9Sstevel@tonic-gate 		 * put this one back and indicate to try again
19227c478bd9Sstevel@tonic-gate 		 */
19237c478bd9Sstevel@tonic-gate 		cpup->cpu_dispthread = curthread;	/* fixup dispthread */
19247c478bd9Sstevel@tonic-gate 		cpup->cpu_dispatch_pri = DISP_PRIO(curthread);
19257c478bd9Sstevel@tonic-gate 		thread_lock_high(tp);
19267c478bd9Sstevel@tonic-gate 		THREAD_TRANSITION(tp);
19277c478bd9Sstevel@tonic-gate 		setfrontdq(tp);
19287c478bd9Sstevel@tonic-gate 		thread_unlock_nopreempt(tp);
19297c478bd9Sstevel@tonic-gate 
19307c478bd9Sstevel@tonic-gate 		tp = NULL;
19317c478bd9Sstevel@tonic-gate 	}
19327c478bd9Sstevel@tonic-gate 	return (tp);
19337c478bd9Sstevel@tonic-gate }
19347c478bd9Sstevel@tonic-gate 
19357c478bd9Sstevel@tonic-gate /*
19367c478bd9Sstevel@tonic-gate  * See if there is any work on the dispatcher queue for other CPUs.
19377c478bd9Sstevel@tonic-gate  * If there is, dequeue the best thread and return.
19387c478bd9Sstevel@tonic-gate  */
19397c478bd9Sstevel@tonic-gate static kthread_t *
19407c478bd9Sstevel@tonic-gate disp_getwork(cpu_t *cp)
19417c478bd9Sstevel@tonic-gate {
19427c478bd9Sstevel@tonic-gate 	cpu_t		*ocp;		/* other CPU */
19437c478bd9Sstevel@tonic-gate 	cpu_t		*ocp_start;
19447c478bd9Sstevel@tonic-gate 	cpu_t		*tcp;		/* target local CPU */
19457c478bd9Sstevel@tonic-gate 	kthread_t	*tp;
1946685679f7Sakolb 	kthread_t	*retval = NULL;
19477c478bd9Sstevel@tonic-gate 	pri_t		maxpri;
19487c478bd9Sstevel@tonic-gate 	disp_t		*kpq;		/* kp queue for this partition */
19497c478bd9Sstevel@tonic-gate 	lpl_t		*lpl, *lpl_leaf;
19507c478bd9Sstevel@tonic-gate 	int		hint, leafidx;
1951685679f7Sakolb 	hrtime_t	stealtime;
19527c478bd9Sstevel@tonic-gate 
19537c478bd9Sstevel@tonic-gate 	maxpri = -1;
19547c478bd9Sstevel@tonic-gate 	tcp = NULL;
19557c478bd9Sstevel@tonic-gate 
19567c478bd9Sstevel@tonic-gate 	kpq = &cp->cpu_part->cp_kp_queue;
19577c478bd9Sstevel@tonic-gate 	while (kpq->disp_maxrunpri >= 0) {
19587c478bd9Sstevel@tonic-gate 		/*
19597c478bd9Sstevel@tonic-gate 		 * Try to take a thread from the kp_queue.
19607c478bd9Sstevel@tonic-gate 		 */
19617c478bd9Sstevel@tonic-gate 		tp = (disp_getbest(kpq));
19627c478bd9Sstevel@tonic-gate 		if (tp)
19637c478bd9Sstevel@tonic-gate 			return (disp_ratify(tp, kpq));
19647c478bd9Sstevel@tonic-gate 	}
19657c478bd9Sstevel@tonic-gate 
1966ab761399Sesaxe 	kpreempt_disable();		/* protect the cpu_active list */
19677c478bd9Sstevel@tonic-gate 
19687c478bd9Sstevel@tonic-gate 	/*
19697c478bd9Sstevel@tonic-gate 	 * Try to find something to do on another CPU's run queue.
19707c478bd9Sstevel@tonic-gate 	 * Loop through all other CPUs looking for the one with the highest
19717c478bd9Sstevel@tonic-gate 	 * priority unbound thread.
19727c478bd9Sstevel@tonic-gate 	 *
19737c478bd9Sstevel@tonic-gate 	 * On NUMA machines, the partition's CPUs are consulted in order of
19747c478bd9Sstevel@tonic-gate 	 * distance from the current CPU. This way, the first available
19757c478bd9Sstevel@tonic-gate 	 * work found is also the closest, and will suffer the least
19767c478bd9Sstevel@tonic-gate 	 * from being migrated.
19777c478bd9Sstevel@tonic-gate 	 */
19787c478bd9Sstevel@tonic-gate 	lpl = lpl_leaf = cp->cpu_lpl;
19797c478bd9Sstevel@tonic-gate 	hint = leafidx = 0;
19807c478bd9Sstevel@tonic-gate 
19817c478bd9Sstevel@tonic-gate 	/*
19827c478bd9Sstevel@tonic-gate 	 * This loop traverses the lpl hierarchy. Higher level lpls represent
19837c478bd9Sstevel@tonic-gate 	 * broader levels of locality
19847c478bd9Sstevel@tonic-gate 	 */
19857c478bd9Sstevel@tonic-gate 	do {
19867c478bd9Sstevel@tonic-gate 		/* This loop iterates over the lpl's leaves */
19877c478bd9Sstevel@tonic-gate 		do {
19887c478bd9Sstevel@tonic-gate 			if (lpl_leaf != cp->cpu_lpl)
19897c478bd9Sstevel@tonic-gate 				ocp = lpl_leaf->lpl_cpus;
19907c478bd9Sstevel@tonic-gate 			else
19917c478bd9Sstevel@tonic-gate 				ocp = cp->cpu_next_lpl;
19927c478bd9Sstevel@tonic-gate 
19937c478bd9Sstevel@tonic-gate 			/* This loop iterates over the CPUs in the leaf */
19947c478bd9Sstevel@tonic-gate 			ocp_start = ocp;
19957c478bd9Sstevel@tonic-gate 			do {
19967c478bd9Sstevel@tonic-gate 				pri_t pri;
19977c478bd9Sstevel@tonic-gate 
19987c478bd9Sstevel@tonic-gate 				ASSERT(CPU_ACTIVE(ocp));
19997c478bd9Sstevel@tonic-gate 
20007c478bd9Sstevel@tonic-gate 				/*
200139bac370Sesaxe 				 * End our stroll around this lpl if:
20027c478bd9Sstevel@tonic-gate 				 *
20037c478bd9Sstevel@tonic-gate 				 * - Something became runnable on the local
200439bac370Sesaxe 				 *   queue...which also ends our stroll around
200539bac370Sesaxe 				 *   the partition.
20067c478bd9Sstevel@tonic-gate 				 *
200739bac370Sesaxe 				 * - We happen across another idle CPU.
200839bac370Sesaxe 				 *   Since it is patrolling the next portion
200939bac370Sesaxe 				 *   of the lpl's list (assuming it's not
201039bac370Sesaxe 				 *   halted), move to the next higher level
201139bac370Sesaxe 				 *   of locality.
20127c478bd9Sstevel@tonic-gate 				 */
201339bac370Sesaxe 				if (cp->cpu_disp->disp_nrunnable != 0) {
201439bac370Sesaxe 					kpreempt_enable();
201539bac370Sesaxe 					return (NULL);
201639bac370Sesaxe 				}
20177c478bd9Sstevel@tonic-gate 				if (ocp->cpu_dispatch_pri == -1) {
20187c478bd9Sstevel@tonic-gate 					if (ocp->cpu_disp_flags &
20197c478bd9Sstevel@tonic-gate 					    CPU_DISP_HALTED)
20207c478bd9Sstevel@tonic-gate 						continue;
202139bac370Sesaxe 					else
20227c478bd9Sstevel@tonic-gate 						break;
20237c478bd9Sstevel@tonic-gate 				}
20247c478bd9Sstevel@tonic-gate 
20257c478bd9Sstevel@tonic-gate 				/*
20267c478bd9Sstevel@tonic-gate 				 * If there's only one thread and the CPU
20277c478bd9Sstevel@tonic-gate 				 * is in the middle of a context switch,
20287c478bd9Sstevel@tonic-gate 				 * or it's currently running the idle thread,
20297c478bd9Sstevel@tonic-gate 				 * don't steal it.
20307c478bd9Sstevel@tonic-gate 				 */
20317c478bd9Sstevel@tonic-gate 				if ((ocp->cpu_disp_flags &
20327c478bd9Sstevel@tonic-gate 				    CPU_DISP_DONTSTEAL) &&
20337c478bd9Sstevel@tonic-gate 				    ocp->cpu_disp->disp_nrunnable == 1)
20347c478bd9Sstevel@tonic-gate 					continue;
20357c478bd9Sstevel@tonic-gate 
20367c478bd9Sstevel@tonic-gate 				pri = ocp->cpu_disp->disp_max_unbound_pri;
20377c478bd9Sstevel@tonic-gate 				if (pri > maxpri) {
2038685679f7Sakolb 					/*
2039685679f7Sakolb 					 * Don't steal threads that we attempted
2040fb2f18f8Sesaxe 					 * to steal recently until they're ready
2041fb2f18f8Sesaxe 					 * to be stolen again.
2042685679f7Sakolb 					 */
2043685679f7Sakolb 					stealtime = ocp->cpu_disp->disp_steal;
2044685679f7Sakolb 					if (stealtime == 0 ||
2045685679f7Sakolb 					    stealtime - gethrtime() <= 0) {
20467c478bd9Sstevel@tonic-gate 						maxpri = pri;
20477c478bd9Sstevel@tonic-gate 						tcp = ocp;
2048685679f7Sakolb 					} else {
2049685679f7Sakolb 						/*
2050685679f7Sakolb 						 * Don't update tcp, just set
2051685679f7Sakolb 						 * the retval to T_DONTSTEAL, so
2052685679f7Sakolb 						 * that if no acceptable CPUs
2053685679f7Sakolb 						 * are found the return value
2054685679f7Sakolb 						 * will be T_DONTSTEAL rather
2055685679f7Sakolb 						 * then NULL.
2056685679f7Sakolb 						 */
2057685679f7Sakolb 						retval = T_DONTSTEAL;
2058685679f7Sakolb 					}
20597c478bd9Sstevel@tonic-gate 				}
20607c478bd9Sstevel@tonic-gate 			} while ((ocp = ocp->cpu_next_lpl) != ocp_start);
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate 			if ((lpl_leaf = lpl->lpl_rset[++leafidx]) == NULL) {
20637c478bd9Sstevel@tonic-gate 				leafidx = 0;
20647c478bd9Sstevel@tonic-gate 				lpl_leaf = lpl->lpl_rset[leafidx];
20657c478bd9Sstevel@tonic-gate 			}
20667c478bd9Sstevel@tonic-gate 		} while (leafidx != hint);
20677c478bd9Sstevel@tonic-gate 
20687c478bd9Sstevel@tonic-gate 		hint = leafidx = lpl->lpl_hint;
20697c478bd9Sstevel@tonic-gate 		if ((lpl = lpl->lpl_parent) != NULL)
20707c478bd9Sstevel@tonic-gate 			lpl_leaf = lpl->lpl_rset[hint];
20717c478bd9Sstevel@tonic-gate 	} while (!tcp && lpl);
20727c478bd9Sstevel@tonic-gate 
2073ab761399Sesaxe 	kpreempt_enable();
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate 	/*
20767c478bd9Sstevel@tonic-gate 	 * If another queue looks good, and there is still nothing on
20777c478bd9Sstevel@tonic-gate 	 * the local queue, try to transfer one or more threads
20787c478bd9Sstevel@tonic-gate 	 * from it to our queue.
20797c478bd9Sstevel@tonic-gate 	 */
20807c478bd9Sstevel@tonic-gate 	if (tcp && cp->cpu_disp->disp_nrunnable == 0) {
2081685679f7Sakolb 		tp = disp_getbest(tcp->cpu_disp);
2082685679f7Sakolb 		if (tp == NULL || tp == T_DONTSTEAL)
2083685679f7Sakolb 			return (tp);
20847c478bd9Sstevel@tonic-gate 		return (disp_ratify(tp, kpq));
20857c478bd9Sstevel@tonic-gate 	}
2086685679f7Sakolb 	return (retval);
20877c478bd9Sstevel@tonic-gate }
20887c478bd9Sstevel@tonic-gate 
20897c478bd9Sstevel@tonic-gate 
20907c478bd9Sstevel@tonic-gate /*
20917c478bd9Sstevel@tonic-gate  * disp_fix_unbound_pri()
20927c478bd9Sstevel@tonic-gate  *	Determines the maximum priority of unbound threads on the queue.
20937c478bd9Sstevel@tonic-gate  *	The priority is kept for the queue, but is only increased, never
20947c478bd9Sstevel@tonic-gate  *	reduced unless some CPU is looking for something on that queue.
20957c478bd9Sstevel@tonic-gate  *
20967c478bd9Sstevel@tonic-gate  *	The priority argument is the known upper limit.
20977c478bd9Sstevel@tonic-gate  *
20987c478bd9Sstevel@tonic-gate  *	Perhaps this should be kept accurately, but that probably means
20997c478bd9Sstevel@tonic-gate  *	separate bitmaps for bound and unbound threads.  Since only idled
21007c478bd9Sstevel@tonic-gate  *	CPUs will have to do this recalculation, it seems better this way.
21017c478bd9Sstevel@tonic-gate  */
21027c478bd9Sstevel@tonic-gate static void
21037c478bd9Sstevel@tonic-gate disp_fix_unbound_pri(disp_t *dp, pri_t pri)
21047c478bd9Sstevel@tonic-gate {
21057c478bd9Sstevel@tonic-gate 	kthread_t	*tp;
21067c478bd9Sstevel@tonic-gate 	dispq_t		*dq;
21077c478bd9Sstevel@tonic-gate 	ulong_t		*dqactmap = dp->disp_qactmap;
21087c478bd9Sstevel@tonic-gate 	ulong_t		mapword;
21097c478bd9Sstevel@tonic-gate 	int		wx;
21107c478bd9Sstevel@tonic-gate 
21117c478bd9Sstevel@tonic-gate 	ASSERT(DISP_LOCK_HELD(&dp->disp_lock));
21127c478bd9Sstevel@tonic-gate 
21137c478bd9Sstevel@tonic-gate 	ASSERT(pri >= 0);			/* checked by caller */
21147c478bd9Sstevel@tonic-gate 
21157c478bd9Sstevel@tonic-gate 	/*
21167c478bd9Sstevel@tonic-gate 	 * Start the search at the next lowest priority below the supplied
21177c478bd9Sstevel@tonic-gate 	 * priority.  This depends on the bitmap implementation.
21187c478bd9Sstevel@tonic-gate 	 */
21197c478bd9Sstevel@tonic-gate 	do {
21207c478bd9Sstevel@tonic-gate 		wx = pri >> BT_ULSHIFT;		/* index of word in map */
21217c478bd9Sstevel@tonic-gate 
21227c478bd9Sstevel@tonic-gate 		/*
21237c478bd9Sstevel@tonic-gate 		 * Form mask for all lower priorities in the word.
21247c478bd9Sstevel@tonic-gate 		 */
21257c478bd9Sstevel@tonic-gate 		mapword = dqactmap[wx] & (BT_BIW(pri) - 1);
21267c478bd9Sstevel@tonic-gate 
21277c478bd9Sstevel@tonic-gate 		/*
21287c478bd9Sstevel@tonic-gate 		 * Get next lower active priority.
21297c478bd9Sstevel@tonic-gate 		 */
21307c478bd9Sstevel@tonic-gate 		if (mapword != 0) {
21317c478bd9Sstevel@tonic-gate 			pri = (wx << BT_ULSHIFT) + highbit(mapword) - 1;
21327c478bd9Sstevel@tonic-gate 		} else if (wx > 0) {
21337c478bd9Sstevel@tonic-gate 			pri = bt_gethighbit(dqactmap, wx - 1); /* sign extend */
21347c478bd9Sstevel@tonic-gate 			if (pri < 0)
21357c478bd9Sstevel@tonic-gate 				break;
21367c478bd9Sstevel@tonic-gate 		} else {
21377c478bd9Sstevel@tonic-gate 			pri = -1;
21387c478bd9Sstevel@tonic-gate 			break;
21397c478bd9Sstevel@tonic-gate 		}
21407c478bd9Sstevel@tonic-gate 
21417c478bd9Sstevel@tonic-gate 		/*
21427c478bd9Sstevel@tonic-gate 		 * Search the queue for unbound, runnable threads.
21437c478bd9Sstevel@tonic-gate 		 */
21447c478bd9Sstevel@tonic-gate 		dq = &dp->disp_q[pri];
21457c478bd9Sstevel@tonic-gate 		tp = dq->dq_first;
21467c478bd9Sstevel@tonic-gate 
21477c478bd9Sstevel@tonic-gate 		while (tp && (tp->t_bound_cpu || tp->t_weakbound_cpu)) {
21487c478bd9Sstevel@tonic-gate 			tp = tp->t_link;
21497c478bd9Sstevel@tonic-gate 		}
21507c478bd9Sstevel@tonic-gate 
21517c478bd9Sstevel@tonic-gate 		/*
21527c478bd9Sstevel@tonic-gate 		 * If a thread was found, set the priority and return.
21537c478bd9Sstevel@tonic-gate 		 */
21547c478bd9Sstevel@tonic-gate 	} while (tp == NULL);
21557c478bd9Sstevel@tonic-gate 
21567c478bd9Sstevel@tonic-gate 	/*
21577c478bd9Sstevel@tonic-gate 	 * pri holds the maximum unbound thread priority or -1.
21587c478bd9Sstevel@tonic-gate 	 */
21597c478bd9Sstevel@tonic-gate 	if (dp->disp_max_unbound_pri != pri)
21607c478bd9Sstevel@tonic-gate 		dp->disp_max_unbound_pri = pri;
21617c478bd9Sstevel@tonic-gate }
21627c478bd9Sstevel@tonic-gate 
21637c478bd9Sstevel@tonic-gate /*
21647c478bd9Sstevel@tonic-gate  * disp_adjust_unbound_pri() - thread is becoming unbound, so we should
21657c478bd9Sstevel@tonic-gate  * 	check if the CPU to which is was previously bound should have
21667c478bd9Sstevel@tonic-gate  * 	its disp_max_unbound_pri increased.
21677c478bd9Sstevel@tonic-gate  */
21687c478bd9Sstevel@tonic-gate void
21697c478bd9Sstevel@tonic-gate disp_adjust_unbound_pri(kthread_t *tp)
21707c478bd9Sstevel@tonic-gate {
21717c478bd9Sstevel@tonic-gate 	disp_t *dp;
21727c478bd9Sstevel@tonic-gate 	pri_t tpri;
21737c478bd9Sstevel@tonic-gate 
21747c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(tp));
21757c478bd9Sstevel@tonic-gate 
21767c478bd9Sstevel@tonic-gate 	/*
21777c478bd9Sstevel@tonic-gate 	 * Don't do anything if the thread is not bound, or
21787c478bd9Sstevel@tonic-gate 	 * currently not runnable or swapped out.
21797c478bd9Sstevel@tonic-gate 	 */
21807c478bd9Sstevel@tonic-gate 	if (tp->t_bound_cpu == NULL ||
21817c478bd9Sstevel@tonic-gate 	    tp->t_state != TS_RUN ||
21827c478bd9Sstevel@tonic-gate 	    tp->t_schedflag & TS_ON_SWAPQ)
21837c478bd9Sstevel@tonic-gate 		return;
21847c478bd9Sstevel@tonic-gate 
21857c478bd9Sstevel@tonic-gate 	tpri = DISP_PRIO(tp);
21867c478bd9Sstevel@tonic-gate 	dp = tp->t_bound_cpu->cpu_disp;
21877c478bd9Sstevel@tonic-gate 	ASSERT(tpri >= 0 && tpri < dp->disp_npri);
21887c478bd9Sstevel@tonic-gate 	if (tpri > dp->disp_max_unbound_pri)
21897c478bd9Sstevel@tonic-gate 		dp->disp_max_unbound_pri = tpri;
21907c478bd9Sstevel@tonic-gate }
21917c478bd9Sstevel@tonic-gate 
21927c478bd9Sstevel@tonic-gate /*
2193685679f7Sakolb  * disp_getbest()
2194685679f7Sakolb  *   De-queue the highest priority unbound runnable thread.
2195685679f7Sakolb  *   Returns with the thread unlocked and onproc but at splhigh (like disp()).
2196685679f7Sakolb  *   Returns NULL if nothing found.
2197685679f7Sakolb  *   Returns T_DONTSTEAL if the thread was not stealable.
2198685679f7Sakolb  *   so that the caller will try again later.
21997c478bd9Sstevel@tonic-gate  *
2200685679f7Sakolb  *   Passed a pointer to a dispatch queue not associated with this CPU, and
2201685679f7Sakolb  *   its type.
22027c478bd9Sstevel@tonic-gate  */
22037c478bd9Sstevel@tonic-gate static kthread_t *
22047c478bd9Sstevel@tonic-gate disp_getbest(disp_t *dp)
22057c478bd9Sstevel@tonic-gate {
22067c478bd9Sstevel@tonic-gate 	kthread_t	*tp;
22077c478bd9Sstevel@tonic-gate 	dispq_t		*dq;
22087c478bd9Sstevel@tonic-gate 	pri_t		pri;
2209685679f7Sakolb 	cpu_t		*cp, *tcp;
2210685679f7Sakolb 	boolean_t	allbound;
22117c478bd9Sstevel@tonic-gate 
22127c478bd9Sstevel@tonic-gate 	disp_lock_enter(&dp->disp_lock);
22137c478bd9Sstevel@tonic-gate 
22147c478bd9Sstevel@tonic-gate 	/*
22157c478bd9Sstevel@tonic-gate 	 * If there is nothing to run, or the CPU is in the middle of a
22167c478bd9Sstevel@tonic-gate 	 * context switch of the only thread, return NULL.
22177c478bd9Sstevel@tonic-gate 	 */
2218685679f7Sakolb 	tcp = dp->disp_cpu;
2219685679f7Sakolb 	cp = CPU;
22207c478bd9Sstevel@tonic-gate 	pri = dp->disp_max_unbound_pri;
22217c478bd9Sstevel@tonic-gate 	if (pri == -1 ||
2222685679f7Sakolb 	    (tcp != NULL && (tcp->cpu_disp_flags & CPU_DISP_DONTSTEAL) &&
2223685679f7Sakolb 	    tcp->cpu_disp->disp_nrunnable == 1)) {
22247c478bd9Sstevel@tonic-gate 		disp_lock_exit_nopreempt(&dp->disp_lock);
22257c478bd9Sstevel@tonic-gate 		return (NULL);
22267c478bd9Sstevel@tonic-gate 	}
22277c478bd9Sstevel@tonic-gate 
22287c478bd9Sstevel@tonic-gate 	dq = &dp->disp_q[pri];
2229685679f7Sakolb 
22307c478bd9Sstevel@tonic-gate 
22317c478bd9Sstevel@tonic-gate 	/*
2232685679f7Sakolb 	 * Assume that all threads are bound on this queue, and change it
2233685679f7Sakolb 	 * later when we find out that it is not the case.
22347c478bd9Sstevel@tonic-gate 	 */
2235685679f7Sakolb 	allbound = B_TRUE;
2236685679f7Sakolb 	for (tp = dq->dq_first; tp != NULL; tp = tp->t_link) {
2237685679f7Sakolb 		hrtime_t now, nosteal, rqtime;
2238685679f7Sakolb 
2239685679f7Sakolb 		/*
2240685679f7Sakolb 		 * Skip over bound threads which could be here even
2241685679f7Sakolb 		 * though disp_max_unbound_pri indicated this level.
2242685679f7Sakolb 		 */
2243685679f7Sakolb 		if (tp->t_bound_cpu || tp->t_weakbound_cpu)
2244685679f7Sakolb 			continue;
2245685679f7Sakolb 
2246685679f7Sakolb 		/*
2247685679f7Sakolb 		 * We've got some unbound threads on this queue, so turn
2248685679f7Sakolb 		 * the allbound flag off now.
2249685679f7Sakolb 		 */
2250685679f7Sakolb 		allbound = B_FALSE;
2251685679f7Sakolb 
2252685679f7Sakolb 		/*
2253685679f7Sakolb 		 * The thread is a candidate for stealing from its run queue. We
2254685679f7Sakolb 		 * don't want to steal threads that became runnable just a
2255685679f7Sakolb 		 * moment ago. This improves CPU affinity for threads that get
2256685679f7Sakolb 		 * preempted for short periods of time and go back on the run
2257685679f7Sakolb 		 * queue.
2258685679f7Sakolb 		 *
2259685679f7Sakolb 		 * We want to let it stay on its run queue if it was only placed
2260685679f7Sakolb 		 * there recently and it was running on the same CPU before that
2261685679f7Sakolb 		 * to preserve its cache investment. For the thread to remain on
2262685679f7Sakolb 		 * its run queue, ALL of the following conditions must be
2263685679f7Sakolb 		 * satisfied:
2264685679f7Sakolb 		 *
2265685679f7Sakolb 		 * - the disp queue should not be the kernel preemption queue
2266685679f7Sakolb 		 * - delayed idle stealing should not be disabled
2267685679f7Sakolb 		 * - nosteal_nsec should be non-zero
2268685679f7Sakolb 		 * - it should run with user priority
2269685679f7Sakolb 		 * - it should be on the run queue of the CPU where it was
2270685679f7Sakolb 		 *   running before being placed on the run queue
2271685679f7Sakolb 		 * - it should be the only thread on the run queue (to prevent
2272685679f7Sakolb 		 *   extra scheduling latency for other threads)
2273685679f7Sakolb 		 * - it should sit on the run queue for less than per-chip
2274685679f7Sakolb 		 *   nosteal interval or global nosteal interval
2275685679f7Sakolb 		 * - in case of CPUs with shared cache it should sit in a run
2276685679f7Sakolb 		 *   queue of a CPU from a different chip
2277685679f7Sakolb 		 *
2278685679f7Sakolb 		 * The checks are arranged so that the ones that are faster are
2279685679f7Sakolb 		 * placed earlier.
2280685679f7Sakolb 		 */
2281685679f7Sakolb 		if (tcp == NULL ||
2282685679f7Sakolb 		    pri >= minclsyspri ||
2283685679f7Sakolb 		    tp->t_cpu != tcp)
2284685679f7Sakolb 			break;
2285685679f7Sakolb 
2286685679f7Sakolb 		/*
2287fb2f18f8Sesaxe 		 * Steal immediately if, due to CMT processor architecture
2288fb2f18f8Sesaxe 		 * migraiton between cp and tcp would incur no performance
2289fb2f18f8Sesaxe 		 * penalty.
2290685679f7Sakolb 		 */
2291fb2f18f8Sesaxe 		if (pg_cmt_can_migrate(cp, tcp))
2292685679f7Sakolb 			break;
2293685679f7Sakolb 
2294fb2f18f8Sesaxe 		nosteal = nosteal_nsec;
2295fb2f18f8Sesaxe 		if (nosteal == 0)
2296685679f7Sakolb 			break;
2297685679f7Sakolb 
2298685679f7Sakolb 		/*
2299685679f7Sakolb 		 * Calculate time spent sitting on run queue
2300685679f7Sakolb 		 */
2301685679f7Sakolb 		now = gethrtime_unscaled();
2302685679f7Sakolb 		rqtime = now - tp->t_waitrq;
2303685679f7Sakolb 		scalehrtime(&rqtime);
2304685679f7Sakolb 
2305685679f7Sakolb 		/*
2306685679f7Sakolb 		 * Steal immediately if the time spent on this run queue is more
2307685679f7Sakolb 		 * than allowed nosteal delay.
2308685679f7Sakolb 		 *
2309685679f7Sakolb 		 * Negative rqtime check is needed here to avoid infinite
2310685679f7Sakolb 		 * stealing delays caused by unlikely but not impossible
2311685679f7Sakolb 		 * drifts between CPU times on different CPUs.
2312685679f7Sakolb 		 */
2313685679f7Sakolb 		if (rqtime > nosteal || rqtime < 0)
2314685679f7Sakolb 			break;
2315685679f7Sakolb 
2316685679f7Sakolb 		DTRACE_PROBE4(nosteal, kthread_t *, tp,
2317685679f7Sakolb 		    cpu_t *, tcp, cpu_t *, cp, hrtime_t, rqtime);
2318685679f7Sakolb 		scalehrtime(&now);
2319685679f7Sakolb 		/*
2320685679f7Sakolb 		 * Calculate when this thread becomes stealable
2321685679f7Sakolb 		 */
2322685679f7Sakolb 		now += (nosteal - rqtime);
2323685679f7Sakolb 
2324685679f7Sakolb 		/*
2325685679f7Sakolb 		 * Calculate time when some thread becomes stealable
2326685679f7Sakolb 		 */
2327685679f7Sakolb 		if (now < dp->disp_steal)
2328685679f7Sakolb 			dp->disp_steal = now;
23297c478bd9Sstevel@tonic-gate 	}
23307c478bd9Sstevel@tonic-gate 
23317c478bd9Sstevel@tonic-gate 	/*
23327c478bd9Sstevel@tonic-gate 	 * If there were no unbound threads on this queue, find the queue
2333685679f7Sakolb 	 * where they are and then return later. The value of
2334685679f7Sakolb 	 * disp_max_unbound_pri is not always accurate because it isn't
2335685679f7Sakolb 	 * reduced until another idle CPU looks for work.
2336685679f7Sakolb 	 */
2337685679f7Sakolb 	if (allbound)
2338685679f7Sakolb 		disp_fix_unbound_pri(dp, pri);
2339685679f7Sakolb 
2340685679f7Sakolb 	/*
2341685679f7Sakolb 	 * If we reached the end of the queue and found no unbound threads
2342685679f7Sakolb 	 * then return NULL so that other CPUs will be considered.  If there
2343685679f7Sakolb 	 * are unbound threads but they cannot yet be stolen, then
2344685679f7Sakolb 	 * return T_DONTSTEAL and try again later.
23457c478bd9Sstevel@tonic-gate 	 */
23467c478bd9Sstevel@tonic-gate 	if (tp == NULL) {
23477c478bd9Sstevel@tonic-gate 		disp_lock_exit_nopreempt(&dp->disp_lock);
2348685679f7Sakolb 		return (allbound ? NULL : T_DONTSTEAL);
23497c478bd9Sstevel@tonic-gate 	}
23507c478bd9Sstevel@tonic-gate 
23517c478bd9Sstevel@tonic-gate 	/*
23527c478bd9Sstevel@tonic-gate 	 * Found a runnable, unbound thread, so remove it from queue.
23537c478bd9Sstevel@tonic-gate 	 * dispdeq() requires that we have the thread locked, and we do,
23547c478bd9Sstevel@tonic-gate 	 * by virtue of holding the dispatch queue lock.  dispdeq() will
23557c478bd9Sstevel@tonic-gate 	 * put the thread in transition state, thereby dropping the dispq
23567c478bd9Sstevel@tonic-gate 	 * lock.
23577c478bd9Sstevel@tonic-gate 	 */
2358685679f7Sakolb 
23597c478bd9Sstevel@tonic-gate #ifdef DEBUG
23607c478bd9Sstevel@tonic-gate 	{
23617c478bd9Sstevel@tonic-gate 		int	thread_was_on_queue;
23627c478bd9Sstevel@tonic-gate 
23637c478bd9Sstevel@tonic-gate 		thread_was_on_queue = dispdeq(tp);	/* drops disp_lock */
23647c478bd9Sstevel@tonic-gate 		ASSERT(thread_was_on_queue);
23657c478bd9Sstevel@tonic-gate 	}
2366685679f7Sakolb 
23677c478bd9Sstevel@tonic-gate #else /* DEBUG */
23687c478bd9Sstevel@tonic-gate 	(void) dispdeq(tp);			/* drops disp_lock */
23697c478bd9Sstevel@tonic-gate #endif /* DEBUG */
23707c478bd9Sstevel@tonic-gate 
2371685679f7Sakolb 	/*
2372685679f7Sakolb 	 * Reset the disp_queue steal time - we do not know what is the smallest
2373685679f7Sakolb 	 * value across the queue is.
2374685679f7Sakolb 	 */
2375685679f7Sakolb 	dp->disp_steal = 0;
2376685679f7Sakolb 
23777c478bd9Sstevel@tonic-gate 	tp->t_schedflag |= TS_DONT_SWAP;
23787c478bd9Sstevel@tonic-gate 
23797c478bd9Sstevel@tonic-gate 	/*
23807c478bd9Sstevel@tonic-gate 	 * Setup thread to run on the current CPU.
23817c478bd9Sstevel@tonic-gate 	 */
23827c478bd9Sstevel@tonic-gate 	tp->t_disp_queue = cp->cpu_disp;
23837c478bd9Sstevel@tonic-gate 
23847c478bd9Sstevel@tonic-gate 	cp->cpu_dispthread = tp;		/* protected by spl only */
23857c478bd9Sstevel@tonic-gate 	cp->cpu_dispatch_pri = pri;
2386*0f500aa6Sbpramod 
2387*0f500aa6Sbpramod 	/*
2388*0f500aa6Sbpramod 	 * There can be a memory synchronization race between disp_getbest()
2389*0f500aa6Sbpramod 	 * and disp_ratify() vs cpu_resched() where cpu_resched() is trying
2390*0f500aa6Sbpramod 	 * to preempt the current thread to run the enqueued thread while
2391*0f500aa6Sbpramod 	 * disp_getbest() and disp_ratify() are changing the current thread
2392*0f500aa6Sbpramod 	 * to the stolen thread. This may lead to a situation where
2393*0f500aa6Sbpramod 	 * cpu_resched() tries to preempt the wrong thread and the
2394*0f500aa6Sbpramod 	 * stolen thread continues to run on the CPU which has been tagged
2395*0f500aa6Sbpramod 	 * for preemption.
2396*0f500aa6Sbpramod 	 * Later the clock thread gets enqueued but doesn't get to run on the
2397*0f500aa6Sbpramod 	 * CPU causing the system to hang.
2398*0f500aa6Sbpramod 	 *
2399*0f500aa6Sbpramod 	 * To avoid this, grabbing and dropping the disp_lock (which does
2400*0f500aa6Sbpramod 	 * a memory barrier) is needed to synchronize the execution of
2401*0f500aa6Sbpramod 	 * cpu_resched() with disp_getbest() and disp_ratify() and
2402*0f500aa6Sbpramod 	 * synchronize the memory read and written by cpu_resched(),
2403*0f500aa6Sbpramod 	 * disp_getbest(), and disp_ratify() with each other.
2404*0f500aa6Sbpramod 	 *  (see CR#6482861 for more details).
2405*0f500aa6Sbpramod 	 */
2406*0f500aa6Sbpramod 	disp_lock_enter_high(&cp->cpu_disp->disp_lock);
2407*0f500aa6Sbpramod 	disp_lock_exit_high(&cp->cpu_disp->disp_lock);
2408*0f500aa6Sbpramod 
24097c478bd9Sstevel@tonic-gate 	ASSERT(pri == DISP_PRIO(tp));
24107c478bd9Sstevel@tonic-gate 
2411685679f7Sakolb 	DTRACE_PROBE3(steal, kthread_t *, tp, cpu_t *, tcp, cpu_t *, cp);
2412685679f7Sakolb 
24137c478bd9Sstevel@tonic-gate 	thread_onproc(tp, cp);			/* set t_state to TS_ONPROC */
24147c478bd9Sstevel@tonic-gate 
24157c478bd9Sstevel@tonic-gate 	/*
24167c478bd9Sstevel@tonic-gate 	 * Return with spl high so that swtch() won't need to raise it.
24177c478bd9Sstevel@tonic-gate 	 * The disp_lock was dropped by dispdeq().
24187c478bd9Sstevel@tonic-gate 	 */
24197c478bd9Sstevel@tonic-gate 
24207c478bd9Sstevel@tonic-gate 	return (tp);
24217c478bd9Sstevel@tonic-gate }
24227c478bd9Sstevel@tonic-gate 
24237c478bd9Sstevel@tonic-gate /*
24247c478bd9Sstevel@tonic-gate  * disp_bound_common() - common routine for higher level functions
24257c478bd9Sstevel@tonic-gate  *	that check for bound threads under certain conditions.
24267c478bd9Sstevel@tonic-gate  *	If 'threadlistsafe' is set then there is no need to acquire
24277c478bd9Sstevel@tonic-gate  *	pidlock to stop the thread list from changing (eg, if
24287c478bd9Sstevel@tonic-gate  *	disp_bound_* is called with cpus paused).
24297c478bd9Sstevel@tonic-gate  */
24307c478bd9Sstevel@tonic-gate static int
24317c478bd9Sstevel@tonic-gate disp_bound_common(cpu_t *cp, int threadlistsafe, int flag)
24327c478bd9Sstevel@tonic-gate {
24337c478bd9Sstevel@tonic-gate 	int		found = 0;
24347c478bd9Sstevel@tonic-gate 	kthread_t	*tp;
24357c478bd9Sstevel@tonic-gate 
24367c478bd9Sstevel@tonic-gate 	ASSERT(flag);
24377c478bd9Sstevel@tonic-gate 
24387c478bd9Sstevel@tonic-gate 	if (!threadlistsafe)
24397c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
24407c478bd9Sstevel@tonic-gate 	tp = curthread;		/* faster than allthreads */
24417c478bd9Sstevel@tonic-gate 	do {
24427c478bd9Sstevel@tonic-gate 		if (tp->t_state != TS_FREE) {
24437c478bd9Sstevel@tonic-gate 			/*
24447c478bd9Sstevel@tonic-gate 			 * If an interrupt thread is busy, but the
24457c478bd9Sstevel@tonic-gate 			 * caller doesn't care (i.e. BOUND_INTR is off),
24467c478bd9Sstevel@tonic-gate 			 * then just ignore it and continue through.
24477c478bd9Sstevel@tonic-gate 			 */
24487c478bd9Sstevel@tonic-gate 			if ((tp->t_flag & T_INTR_THREAD) &&
24497c478bd9Sstevel@tonic-gate 			    !(flag & BOUND_INTR))
24507c478bd9Sstevel@tonic-gate 				continue;
24517c478bd9Sstevel@tonic-gate 
24527c478bd9Sstevel@tonic-gate 			/*
24537c478bd9Sstevel@tonic-gate 			 * Skip the idle thread for the CPU
24547c478bd9Sstevel@tonic-gate 			 * we're about to set offline.
24557c478bd9Sstevel@tonic-gate 			 */
24567c478bd9Sstevel@tonic-gate 			if (tp == cp->cpu_idle_thread)
24577c478bd9Sstevel@tonic-gate 				continue;
24587c478bd9Sstevel@tonic-gate 
24597c478bd9Sstevel@tonic-gate 			/*
24607c478bd9Sstevel@tonic-gate 			 * Skip the pause thread for the CPU
24617c478bd9Sstevel@tonic-gate 			 * we're about to set offline.
24627c478bd9Sstevel@tonic-gate 			 */
24637c478bd9Sstevel@tonic-gate 			if (tp == cp->cpu_pause_thread)
24647c478bd9Sstevel@tonic-gate 				continue;
24657c478bd9Sstevel@tonic-gate 
24667c478bd9Sstevel@tonic-gate 			if ((flag & BOUND_CPU) &&
24677c478bd9Sstevel@tonic-gate 			    (tp->t_bound_cpu == cp ||
24687c478bd9Sstevel@tonic-gate 			    tp->t_bind_cpu == cp->cpu_id ||
24697c478bd9Sstevel@tonic-gate 			    tp->t_weakbound_cpu == cp)) {
24707c478bd9Sstevel@tonic-gate 				found = 1;
24717c478bd9Sstevel@tonic-gate 				break;
24727c478bd9Sstevel@tonic-gate 			}
24737c478bd9Sstevel@tonic-gate 
24747c478bd9Sstevel@tonic-gate 			if ((flag & BOUND_PARTITION) &&
24757c478bd9Sstevel@tonic-gate 			    (tp->t_cpupart == cp->cpu_part)) {
24767c478bd9Sstevel@tonic-gate 				found = 1;
24777c478bd9Sstevel@tonic-gate 				break;
24787c478bd9Sstevel@tonic-gate 			}
24797c478bd9Sstevel@tonic-gate 		}
24807c478bd9Sstevel@tonic-gate 	} while ((tp = tp->t_next) != curthread && found == 0);
24817c478bd9Sstevel@tonic-gate 	if (!threadlistsafe)
24827c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
24837c478bd9Sstevel@tonic-gate 	return (found);
24847c478bd9Sstevel@tonic-gate }
24857c478bd9Sstevel@tonic-gate 
24867c478bd9Sstevel@tonic-gate /*
24877c478bd9Sstevel@tonic-gate  * disp_bound_threads - return nonzero if threads are bound to the processor.
24887c478bd9Sstevel@tonic-gate  *	Called infrequently.  Keep this simple.
24897c478bd9Sstevel@tonic-gate  *	Includes threads that are asleep or stopped but not onproc.
24907c478bd9Sstevel@tonic-gate  */
24917c478bd9Sstevel@tonic-gate int
24927c478bd9Sstevel@tonic-gate disp_bound_threads(cpu_t *cp, int threadlistsafe)
24937c478bd9Sstevel@tonic-gate {
24947c478bd9Sstevel@tonic-gate 	return (disp_bound_common(cp, threadlistsafe, BOUND_CPU));
24957c478bd9Sstevel@tonic-gate }
24967c478bd9Sstevel@tonic-gate 
24977c478bd9Sstevel@tonic-gate /*
24987c478bd9Sstevel@tonic-gate  * disp_bound_anythreads - return nonzero if _any_ threads are bound
24997c478bd9Sstevel@tonic-gate  * to the given processor, including interrupt threads.
25007c478bd9Sstevel@tonic-gate  */
25017c478bd9Sstevel@tonic-gate int
25027c478bd9Sstevel@tonic-gate disp_bound_anythreads(cpu_t *cp, int threadlistsafe)
25037c478bd9Sstevel@tonic-gate {
25047c478bd9Sstevel@tonic-gate 	return (disp_bound_common(cp, threadlistsafe, BOUND_CPU | BOUND_INTR));
25057c478bd9Sstevel@tonic-gate }
25067c478bd9Sstevel@tonic-gate 
25077c478bd9Sstevel@tonic-gate /*
25087c478bd9Sstevel@tonic-gate  * disp_bound_partition - return nonzero if threads are bound to the same
25097c478bd9Sstevel@tonic-gate  * partition as the processor.
25107c478bd9Sstevel@tonic-gate  *	Called infrequently.  Keep this simple.
25117c478bd9Sstevel@tonic-gate  *	Includes threads that are asleep or stopped but not onproc.
25127c478bd9Sstevel@tonic-gate  */
25137c478bd9Sstevel@tonic-gate int
25147c478bd9Sstevel@tonic-gate disp_bound_partition(cpu_t *cp, int threadlistsafe)
25157c478bd9Sstevel@tonic-gate {
25167c478bd9Sstevel@tonic-gate 	return (disp_bound_common(cp, threadlistsafe, BOUND_PARTITION));
25177c478bd9Sstevel@tonic-gate }
25187c478bd9Sstevel@tonic-gate 
25197c478bd9Sstevel@tonic-gate /*
25207c478bd9Sstevel@tonic-gate  * disp_cpu_inactive - make a CPU inactive by moving all of its unbound
25217c478bd9Sstevel@tonic-gate  * threads to other CPUs.
25227c478bd9Sstevel@tonic-gate  */
25237c478bd9Sstevel@tonic-gate void
25247c478bd9Sstevel@tonic-gate disp_cpu_inactive(cpu_t *cp)
25257c478bd9Sstevel@tonic-gate {
25267c478bd9Sstevel@tonic-gate 	kthread_t	*tp;
25277c478bd9Sstevel@tonic-gate 	disp_t		*dp = cp->cpu_disp;
25287c478bd9Sstevel@tonic-gate 	dispq_t		*dq;
25297c478bd9Sstevel@tonic-gate 	pri_t		pri;
25307c478bd9Sstevel@tonic-gate 	int		wasonq;
25317c478bd9Sstevel@tonic-gate 
25327c478bd9Sstevel@tonic-gate 	disp_lock_enter(&dp->disp_lock);
25337c478bd9Sstevel@tonic-gate 	while ((pri = dp->disp_max_unbound_pri) != -1) {
25347c478bd9Sstevel@tonic-gate 		dq = &dp->disp_q[pri];
25357c478bd9Sstevel@tonic-gate 		tp = dq->dq_first;
25367c478bd9Sstevel@tonic-gate 
25377c478bd9Sstevel@tonic-gate 		/*
25387c478bd9Sstevel@tonic-gate 		 * Skip over bound threads.
25397c478bd9Sstevel@tonic-gate 		 */
25407c478bd9Sstevel@tonic-gate 		while (tp != NULL && tp->t_bound_cpu != NULL) {
25417c478bd9Sstevel@tonic-gate 			tp = tp->t_link;
25427c478bd9Sstevel@tonic-gate 		}
25437c478bd9Sstevel@tonic-gate 
25447c478bd9Sstevel@tonic-gate 		if (tp == NULL) {
25457c478bd9Sstevel@tonic-gate 			/* disp_max_unbound_pri must be inaccurate, so fix it */
25467c478bd9Sstevel@tonic-gate 			disp_fix_unbound_pri(dp, pri);
25477c478bd9Sstevel@tonic-gate 			continue;
25487c478bd9Sstevel@tonic-gate 		}
25497c478bd9Sstevel@tonic-gate 
25507c478bd9Sstevel@tonic-gate 		wasonq = dispdeq(tp);		/* drops disp_lock */
25517c478bd9Sstevel@tonic-gate 		ASSERT(wasonq);
25527c478bd9Sstevel@tonic-gate 		ASSERT(tp->t_weakbound_cpu == NULL);
25537c478bd9Sstevel@tonic-gate 
25547c478bd9Sstevel@tonic-gate 		setbackdq(tp);
25557c478bd9Sstevel@tonic-gate 		/*
25567c478bd9Sstevel@tonic-gate 		 * Called from cpu_offline:
25577c478bd9Sstevel@tonic-gate 		 *
25587c478bd9Sstevel@tonic-gate 		 * cp has already been removed from the list of active cpus
25597c478bd9Sstevel@tonic-gate 		 * and tp->t_cpu has been changed so there is no risk of
25607c478bd9Sstevel@tonic-gate 		 * tp ending up back on cp.
25617c478bd9Sstevel@tonic-gate 		 *
25627c478bd9Sstevel@tonic-gate 		 * Called from cpupart_move_cpu:
25637c478bd9Sstevel@tonic-gate 		 *
25647c478bd9Sstevel@tonic-gate 		 * The cpu has moved to a new cpupart.  Any threads that
25657c478bd9Sstevel@tonic-gate 		 * were on it's dispatch queues before the move remain
25667c478bd9Sstevel@tonic-gate 		 * in the old partition and can't run in the new partition.
25677c478bd9Sstevel@tonic-gate 		 */
25687c478bd9Sstevel@tonic-gate 		ASSERT(tp->t_cpu != cp);
25697c478bd9Sstevel@tonic-gate 		thread_unlock(tp);
25707c478bd9Sstevel@tonic-gate 
25717c478bd9Sstevel@tonic-gate 		disp_lock_enter(&dp->disp_lock);
25727c478bd9Sstevel@tonic-gate 	}
25737c478bd9Sstevel@tonic-gate 	disp_lock_exit(&dp->disp_lock);
25747c478bd9Sstevel@tonic-gate }
25757c478bd9Sstevel@tonic-gate 
25767c478bd9Sstevel@tonic-gate /*
25777c478bd9Sstevel@tonic-gate  * disp_lowpri_cpu - find CPU running the lowest priority thread.
25787c478bd9Sstevel@tonic-gate  *	The hint passed in is used as a starting point so we don't favor
25797c478bd9Sstevel@tonic-gate  *	CPU 0 or any other CPU.  The caller should pass in the most recently
25807c478bd9Sstevel@tonic-gate  *	used CPU for the thread.
25817c478bd9Sstevel@tonic-gate  *
25827c478bd9Sstevel@tonic-gate  *	The lgroup and priority are used to determine the best CPU to run on
25837c478bd9Sstevel@tonic-gate  *	in a NUMA machine.  The lgroup specifies which CPUs are closest while
25847c478bd9Sstevel@tonic-gate  *	the thread priority will indicate whether the thread will actually run
25857c478bd9Sstevel@tonic-gate  *	there.  To pick the best CPU, the CPUs inside and outside of the given
25867c478bd9Sstevel@tonic-gate  *	lgroup which are running the lowest priority threads are found.  The
25877c478bd9Sstevel@tonic-gate  *	remote CPU is chosen only if the thread will not run locally on a CPU
25887c478bd9Sstevel@tonic-gate  *	within the lgroup, but will run on the remote CPU. If the thread
25897c478bd9Sstevel@tonic-gate  *	cannot immediately run on any CPU, the best local CPU will be chosen.
25907c478bd9Sstevel@tonic-gate  *
25917c478bd9Sstevel@tonic-gate  *	The lpl specified also identifies the cpu partition from which
25927c478bd9Sstevel@tonic-gate  *	disp_lowpri_cpu should select a CPU.
25937c478bd9Sstevel@tonic-gate  *
25947c478bd9Sstevel@tonic-gate  *	curcpu is used to indicate that disp_lowpri_cpu is being called on
25957c478bd9Sstevel@tonic-gate  *      behalf of the current thread. (curthread is looking for a new cpu)
25967c478bd9Sstevel@tonic-gate  *      In this case, cpu_dispatch_pri for this thread's cpu should be
25977c478bd9Sstevel@tonic-gate  *      ignored.
25987c478bd9Sstevel@tonic-gate  *
25997c478bd9Sstevel@tonic-gate  *      If a cpu is the target of an offline request then try to avoid it.
26007c478bd9Sstevel@tonic-gate  *
26017c478bd9Sstevel@tonic-gate  *	This function must be called at either high SPL, or with preemption
26027c478bd9Sstevel@tonic-gate  *	disabled, so that the "hint" CPU cannot be removed from the online
26037c478bd9Sstevel@tonic-gate  *	CPU list while we are traversing it.
26047c478bd9Sstevel@tonic-gate  */
26057c478bd9Sstevel@tonic-gate cpu_t *
26067c478bd9Sstevel@tonic-gate disp_lowpri_cpu(cpu_t *hint, lpl_t *lpl, pri_t tpri, cpu_t *curcpu)
26077c478bd9Sstevel@tonic-gate {
26087c478bd9Sstevel@tonic-gate 	cpu_t	*bestcpu;
26097c478bd9Sstevel@tonic-gate 	cpu_t	*besthomecpu;
26107c478bd9Sstevel@tonic-gate 	cpu_t   *cp, *cpstart;
26117c478bd9Sstevel@tonic-gate 
26127c478bd9Sstevel@tonic-gate 	pri_t   bestpri;
26137c478bd9Sstevel@tonic-gate 	pri_t   cpupri;
26147c478bd9Sstevel@tonic-gate 
26157c478bd9Sstevel@tonic-gate 	klgrpset_t	done;
26167c478bd9Sstevel@tonic-gate 	klgrpset_t	cur_set;
26177c478bd9Sstevel@tonic-gate 
26187c478bd9Sstevel@tonic-gate 	lpl_t		*lpl_iter, *lpl_leaf;
26197c478bd9Sstevel@tonic-gate 	int		i;
26207c478bd9Sstevel@tonic-gate 
26217c478bd9Sstevel@tonic-gate 	/*
26227c478bd9Sstevel@tonic-gate 	 * Scan for a CPU currently running the lowest priority thread.
26237c478bd9Sstevel@tonic-gate 	 * Cannot get cpu_lock here because it is adaptive.
26247c478bd9Sstevel@tonic-gate 	 * We do not require lock on CPU list.
26257c478bd9Sstevel@tonic-gate 	 */
26267c478bd9Sstevel@tonic-gate 	ASSERT(hint != NULL);
26277c478bd9Sstevel@tonic-gate 	ASSERT(lpl != NULL);
26287c478bd9Sstevel@tonic-gate 	ASSERT(lpl->lpl_ncpu > 0);
26297c478bd9Sstevel@tonic-gate 
26307c478bd9Sstevel@tonic-gate 	/*
26317c478bd9Sstevel@tonic-gate 	 * First examine local CPUs. Note that it's possible the hint CPU
26327c478bd9Sstevel@tonic-gate 	 * passed in in remote to the specified home lgroup. If our priority
26337c478bd9Sstevel@tonic-gate 	 * isn't sufficient enough such that we can run immediately at home,
26347c478bd9Sstevel@tonic-gate 	 * then examine CPUs remote to our home lgroup.
26357c478bd9Sstevel@tonic-gate 	 * We would like to give preference to CPUs closest to "home".
26367c478bd9Sstevel@tonic-gate 	 * If we can't find a CPU where we'll run at a given level
26377c478bd9Sstevel@tonic-gate 	 * of locality, we expand our search to include the next level.
26387c478bd9Sstevel@tonic-gate 	 */
26397c478bd9Sstevel@tonic-gate 	bestcpu = besthomecpu = NULL;
26407c478bd9Sstevel@tonic-gate 	klgrpset_clear(done);
26417c478bd9Sstevel@tonic-gate 	/* start with lpl we were passed */
26427c478bd9Sstevel@tonic-gate 
26437c478bd9Sstevel@tonic-gate 	lpl_iter = lpl;
26447c478bd9Sstevel@tonic-gate 
26457c478bd9Sstevel@tonic-gate 	do {
26467c478bd9Sstevel@tonic-gate 
26477c478bd9Sstevel@tonic-gate 		bestpri = SHRT_MAX;
26487c478bd9Sstevel@tonic-gate 		klgrpset_clear(cur_set);
26497c478bd9Sstevel@tonic-gate 
26507c478bd9Sstevel@tonic-gate 		for (i = 0; i < lpl_iter->lpl_nrset; i++) {
26517c478bd9Sstevel@tonic-gate 			lpl_leaf = lpl_iter->lpl_rset[i];
26527c478bd9Sstevel@tonic-gate 			if (klgrpset_ismember(done, lpl_leaf->lpl_lgrpid))
26537c478bd9Sstevel@tonic-gate 				continue;
26547c478bd9Sstevel@tonic-gate 
26557c478bd9Sstevel@tonic-gate 			klgrpset_add(cur_set, lpl_leaf->lpl_lgrpid);
26567c478bd9Sstevel@tonic-gate 
26577c478bd9Sstevel@tonic-gate 			if (hint->cpu_lpl == lpl_leaf)
26587c478bd9Sstevel@tonic-gate 				cp = cpstart = hint;
26597c478bd9Sstevel@tonic-gate 			else
26607c478bd9Sstevel@tonic-gate 				cp = cpstart = lpl_leaf->lpl_cpus;
26617c478bd9Sstevel@tonic-gate 
26627c478bd9Sstevel@tonic-gate 			do {
26637c478bd9Sstevel@tonic-gate 				if (cp == curcpu)
26647c478bd9Sstevel@tonic-gate 					cpupri = -1;
26657c478bd9Sstevel@tonic-gate 				else if (cp == cpu_inmotion)
26667c478bd9Sstevel@tonic-gate 					cpupri = SHRT_MAX;
26677c478bd9Sstevel@tonic-gate 				else
26687c478bd9Sstevel@tonic-gate 					cpupri = cp->cpu_dispatch_pri;
26697c478bd9Sstevel@tonic-gate 				if (cp->cpu_disp->disp_maxrunpri > cpupri)
26707c478bd9Sstevel@tonic-gate 					cpupri = cp->cpu_disp->disp_maxrunpri;
26717c478bd9Sstevel@tonic-gate 				if (cp->cpu_chosen_level > cpupri)
26727c478bd9Sstevel@tonic-gate 					cpupri = cp->cpu_chosen_level;
26737c478bd9Sstevel@tonic-gate 				if (cpupri < bestpri) {
26747c478bd9Sstevel@tonic-gate 					if (CPU_IDLING(cpupri)) {
26757c478bd9Sstevel@tonic-gate 						ASSERT((cp->cpu_flags &
26767c478bd9Sstevel@tonic-gate 						    CPU_QUIESCED) == 0);
26777c478bd9Sstevel@tonic-gate 						return (cp);
26787c478bd9Sstevel@tonic-gate 					}
26797c478bd9Sstevel@tonic-gate 					bestcpu = cp;
26807c478bd9Sstevel@tonic-gate 					bestpri = cpupri;
26817c478bd9Sstevel@tonic-gate 				}
26827c478bd9Sstevel@tonic-gate 			} while ((cp = cp->cpu_next_lpl) != cpstart);
26837c478bd9Sstevel@tonic-gate 		}
26847c478bd9Sstevel@tonic-gate 
26857c478bd9Sstevel@tonic-gate 		if (bestcpu && (tpri > bestpri)) {
26867c478bd9Sstevel@tonic-gate 			ASSERT((bestcpu->cpu_flags & CPU_QUIESCED) == 0);
26877c478bd9Sstevel@tonic-gate 			return (bestcpu);
26887c478bd9Sstevel@tonic-gate 		}
26897c478bd9Sstevel@tonic-gate 		if (besthomecpu == NULL)
26907c478bd9Sstevel@tonic-gate 			besthomecpu = bestcpu;
26917c478bd9Sstevel@tonic-gate 		/*
26927c478bd9Sstevel@tonic-gate 		 * Add the lgrps we just considered to the "done" set
26937c478bd9Sstevel@tonic-gate 		 */
26947c478bd9Sstevel@tonic-gate 		klgrpset_or(done, cur_set);
26957c478bd9Sstevel@tonic-gate 
26967c478bd9Sstevel@tonic-gate 	} while ((lpl_iter = lpl_iter->lpl_parent) != NULL);
26977c478bd9Sstevel@tonic-gate 
26987c478bd9Sstevel@tonic-gate 	/*
26997c478bd9Sstevel@tonic-gate 	 * The specified priority isn't high enough to run immediately
27007c478bd9Sstevel@tonic-gate 	 * anywhere, so just return the best CPU from the home lgroup.
27017c478bd9Sstevel@tonic-gate 	 */
27027c478bd9Sstevel@tonic-gate 	ASSERT((besthomecpu->cpu_flags & CPU_QUIESCED) == 0);
27037c478bd9Sstevel@tonic-gate 	return (besthomecpu);
27047c478bd9Sstevel@tonic-gate }
27057c478bd9Sstevel@tonic-gate 
27067c478bd9Sstevel@tonic-gate /*
27077c478bd9Sstevel@tonic-gate  * This routine provides the generic idle cpu function for all processors.
27087c478bd9Sstevel@tonic-gate  * If a processor has some specific code to execute when idle (say, to stop
27097c478bd9Sstevel@tonic-gate  * the pipeline and save power) then that routine should be defined in the
27107c478bd9Sstevel@tonic-gate  * processors specific code (module_xx.c) and the global variable idle_cpu
27117c478bd9Sstevel@tonic-gate  * set to that function.
27127c478bd9Sstevel@tonic-gate  */
27137c478bd9Sstevel@tonic-gate static void
27147c478bd9Sstevel@tonic-gate generic_idle_cpu(void)
27157c478bd9Sstevel@tonic-gate {
27167c478bd9Sstevel@tonic-gate }
27177c478bd9Sstevel@tonic-gate 
27187c478bd9Sstevel@tonic-gate /*ARGSUSED*/
27197c478bd9Sstevel@tonic-gate static void
27207c478bd9Sstevel@tonic-gate generic_enq_thread(cpu_t *cpu, int bound)
27217c478bd9Sstevel@tonic-gate {
27227c478bd9Sstevel@tonic-gate }
27237c478bd9Sstevel@tonic-gate 
27247c478bd9Sstevel@tonic-gate /*
27257c478bd9Sstevel@tonic-gate  * Select a CPU for this thread to run on.  Choose t->t_cpu unless:
27267c478bd9Sstevel@tonic-gate  *	- t->t_cpu is not in this thread's assigned lgrp
27277c478bd9Sstevel@tonic-gate  *	- the time since the thread last came off t->t_cpu exceeds the
27287c478bd9Sstevel@tonic-gate  *	  rechoose time for this cpu (ignore this if t is curthread in
27297c478bd9Sstevel@tonic-gate  *	  which case it's on CPU and t->t_disp_time is inaccurate)
27307c478bd9Sstevel@tonic-gate  *	- t->t_cpu is presently the target of an offline or partition move
27317c478bd9Sstevel@tonic-gate  *	  request
27327c478bd9Sstevel@tonic-gate  */
27337c478bd9Sstevel@tonic-gate static cpu_t *
27347c478bd9Sstevel@tonic-gate cpu_choose(kthread_t *t, pri_t tpri)
27357c478bd9Sstevel@tonic-gate {
27367c478bd9Sstevel@tonic-gate 	ASSERT(tpri < kpqpri);
27377c478bd9Sstevel@tonic-gate 
2738fb2f18f8Sesaxe 	if ((((lbolt - t->t_disp_time) > rechoose_interval) &&
27397c478bd9Sstevel@tonic-gate 	    t != curthread) || t->t_cpu == cpu_inmotion) {
27407c478bd9Sstevel@tonic-gate 		return (disp_lowpri_cpu(t->t_cpu, t->t_lpl, tpri, NULL));
27417c478bd9Sstevel@tonic-gate 	}
27427c478bd9Sstevel@tonic-gate 
27437c478bd9Sstevel@tonic-gate 	/*
27447c478bd9Sstevel@tonic-gate 	 * Take a trip through disp_lowpri_cpu() if the thread was
27457c478bd9Sstevel@tonic-gate 	 * running outside it's home lgroup
27467c478bd9Sstevel@tonic-gate 	 */
27477c478bd9Sstevel@tonic-gate 	if (!klgrpset_ismember(t->t_lpl->lpl_lgrp->lgrp_set[LGRP_RSRC_CPU],
27487c478bd9Sstevel@tonic-gate 	    t->t_cpu->cpu_lpl->lpl_lgrpid)) {
27497c478bd9Sstevel@tonic-gate 		return (disp_lowpri_cpu(t->t_cpu, t->t_lpl, tpri,
27507c478bd9Sstevel@tonic-gate 		    (t == curthread) ? t->t_cpu : NULL));
27517c478bd9Sstevel@tonic-gate 	}
27527c478bd9Sstevel@tonic-gate 	return (t->t_cpu);
27537c478bd9Sstevel@tonic-gate }
2754