xref: /titanic_41/usr/src/uts/common/os/cpu.c (revision 05b96de272314c66a0e27fe7df41c9a9e1038c5a)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Architecture-independent CPU control functions.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/var.h>
36 #include <sys/thread.h>
37 #include <sys/cpuvar.h>
38 #include <sys/kstat.h>
39 #include <sys/uadmin.h>
40 #include <sys/systm.h>
41 #include <sys/errno.h>
42 #include <sys/cmn_err.h>
43 #include <sys/procset.h>
44 #include <sys/processor.h>
45 #include <sys/debug.h>
46 #include <sys/cpupart.h>
47 #include <sys/lgrp.h>
48 #include <sys/pset.h>
49 #include <sys/chip.h>
50 #include <sys/kmem.h>
51 #include <sys/kmem_impl.h>	/* to set per-cpu kmem_cache offset */
52 #include <sys/atomic.h>
53 #include <sys/callb.h>
54 #include <sys/vtrace.h>
55 #include <sys/cyclic.h>
56 #include <sys/bitmap.h>
57 #include <sys/nvpair.h>
58 #include <sys/pool_pset.h>
59 #include <sys/msacct.h>
60 #include <sys/time.h>
61 #include <sys/archsystm.h>
62 
63 extern int	mp_cpu_start(cpu_t *);
64 extern int	mp_cpu_stop(cpu_t *);
65 extern int	mp_cpu_poweron(cpu_t *);
66 extern int	mp_cpu_poweroff(cpu_t *);
67 extern int	mp_cpu_configure(int);
68 extern int	mp_cpu_unconfigure(int);
69 extern void	mp_cpu_faulted_enter(cpu_t *);
70 extern void	mp_cpu_faulted_exit(cpu_t *);
71 
72 extern int cmp_cpu_to_chip(processorid_t cpuid);
73 #ifdef __sparcv9
74 extern char *cpu_fru_fmri(cpu_t *cp);
75 #endif
76 
77 static void cpu_add_active_internal(cpu_t *cp);
78 static void cpu_remove_active(cpu_t *cp);
79 static void cpu_info_kstat_create(cpu_t *cp);
80 static void cpu_info_kstat_destroy(cpu_t *cp);
81 static void cpu_stats_kstat_create(cpu_t *cp);
82 static void cpu_stats_kstat_destroy(cpu_t *cp);
83 
84 static int cpu_sys_stats_ks_update(kstat_t *ksp, int rw);
85 static int cpu_vm_stats_ks_update(kstat_t *ksp, int rw);
86 static int cpu_stat_ks_update(kstat_t *ksp, int rw);
87 static int cpu_state_change_hooks(int, cpu_setup_t, cpu_setup_t);
88 
89 /*
90  * cpu_lock protects ncpus, ncpus_online, cpu_flag, cpu_list, cpu_active,
91  * and dispatch queue reallocations.  The lock ordering with respect to
92  * related locks is:
93  *
94  *	cpu_lock --> thread_free_lock  --->  p_lock  --->  thread_lock()
95  *
96  * Warning:  Certain sections of code do not use the cpu_lock when
97  * traversing the cpu_list (e.g. mutex_vector_enter(), clock()).  Since
98  * all cpus are paused during modifications to this list, a solution
99  * to protect the list is too either disable kernel preemption while
100  * walking the list, *or* recheck the cpu_next pointer at each
101  * iteration in the loop.  Note that in no cases can any cached
102  * copies of the cpu pointers be kept as they may become invalid.
103  */
104 kmutex_t	cpu_lock;
105 cpu_t		*cpu_list;		/* list of all CPUs */
106 cpu_t		*cpu_active;		/* list of active CPUs */
107 static cpuset_t	cpu_available;		/* set of available CPUs */
108 cpuset_t	cpu_seqid_inuse;	/* which cpu_seqids are in use */
109 
110 /*
111  * max_ncpus keeps the max cpus the system can have. Initially
112  * it's NCPU, but since most archs scan the devtree for cpus
113  * fairly early on during boot, the real max can be known before
114  * ncpus is set (useful for early NCPU based allocations).
115  */
116 int max_ncpus = NCPU;
117 /*
118  * platforms that set max_ncpus to maxiumum number of cpus that can be
119  * dynamically added will set boot_max_ncpus to the number of cpus found
120  * at device tree scan time during boot.
121  */
122 int boot_max_ncpus = -1;
123 /*
124  * Maximum possible CPU id.  This can never be >= NCPU since NCPU is
125  * used to size arrays that are indexed by CPU id.
126  */
127 processorid_t max_cpuid = NCPU - 1;
128 
129 int ncpus = 1;
130 int ncpus_online = 1;
131 
132 /*
133  * CPU that we're trying to offline.  Protected by cpu_lock.
134  */
135 cpu_t *cpu_inmotion;
136 
137 /*
138  * Can be raised to suppress further weakbinding, which are instead
139  * satisfied by disabling preemption.  Must be raised/lowered under cpu_lock,
140  * while individual thread weakbinding synchronisation is done under thread
141  * lock.
142  */
143 int weakbindingbarrier;
144 
145 /*
146  * values for safe_list.  Pause state that CPUs are in.
147  */
148 #define	PAUSE_IDLE	0		/* normal state */
149 #define	PAUSE_READY	1		/* paused thread ready to spl */
150 #define	PAUSE_WAIT	2		/* paused thread is spl-ed high */
151 #define	PAUSE_DIE	3		/* tell pause thread to leave */
152 #define	PAUSE_DEAD	4		/* pause thread has left */
153 
154 /*
155  * Variables used in pause_cpus().
156  */
157 static volatile char safe_list[NCPU];
158 
159 static struct _cpu_pause_info {
160 	int		cp_spl;		/* spl saved in pause_cpus() */
161 	volatile int	cp_go;		/* Go signal sent after all ready */
162 	int		cp_count;	/* # of CPUs to pause */
163 	ksema_t		cp_sem;		/* synch pause_cpus & cpu_pause */
164 	kthread_id_t	cp_paused;
165 } cpu_pause_info;
166 
167 static kmutex_t pause_free_mutex;
168 static kcondvar_t pause_free_cv;
169 
170 static struct cpu_sys_stats_ks_data {
171 	kstat_named_t cpu_ticks_idle;
172 	kstat_named_t cpu_ticks_user;
173 	kstat_named_t cpu_ticks_kernel;
174 	kstat_named_t cpu_ticks_wait;
175 	kstat_named_t cpu_nsec_idle;
176 	kstat_named_t cpu_nsec_user;
177 	kstat_named_t cpu_nsec_kernel;
178 	kstat_named_t wait_ticks_io;
179 	kstat_named_t bread;
180 	kstat_named_t bwrite;
181 	kstat_named_t lread;
182 	kstat_named_t lwrite;
183 	kstat_named_t phread;
184 	kstat_named_t phwrite;
185 	kstat_named_t pswitch;
186 	kstat_named_t trap;
187 	kstat_named_t intr;
188 	kstat_named_t syscall;
189 	kstat_named_t sysread;
190 	kstat_named_t syswrite;
191 	kstat_named_t sysfork;
192 	kstat_named_t sysvfork;
193 	kstat_named_t sysexec;
194 	kstat_named_t readch;
195 	kstat_named_t writech;
196 	kstat_named_t rcvint;
197 	kstat_named_t xmtint;
198 	kstat_named_t mdmint;
199 	kstat_named_t rawch;
200 	kstat_named_t canch;
201 	kstat_named_t outch;
202 	kstat_named_t msg;
203 	kstat_named_t sema;
204 	kstat_named_t namei;
205 	kstat_named_t ufsiget;
206 	kstat_named_t ufsdirblk;
207 	kstat_named_t ufsipage;
208 	kstat_named_t ufsinopage;
209 	kstat_named_t procovf;
210 	kstat_named_t intrthread;
211 	kstat_named_t intrblk;
212 	kstat_named_t intrunpin;
213 	kstat_named_t idlethread;
214 	kstat_named_t inv_swtch;
215 	kstat_named_t nthreads;
216 	kstat_named_t cpumigrate;
217 	kstat_named_t xcalls;
218 	kstat_named_t mutex_adenters;
219 	kstat_named_t rw_rdfails;
220 	kstat_named_t rw_wrfails;
221 	kstat_named_t modload;
222 	kstat_named_t modunload;
223 	kstat_named_t bawrite;
224 	kstat_named_t iowait;
225 } cpu_sys_stats_ks_data_template = {
226 	{ "cpu_ticks_idle", 	KSTAT_DATA_UINT64 },
227 	{ "cpu_ticks_user", 	KSTAT_DATA_UINT64 },
228 	{ "cpu_ticks_kernel", 	KSTAT_DATA_UINT64 },
229 	{ "cpu_ticks_wait", 	KSTAT_DATA_UINT64 },
230 	{ "cpu_nsec_idle",	KSTAT_DATA_UINT64 },
231 	{ "cpu_nsec_user",	KSTAT_DATA_UINT64 },
232 	{ "cpu_nsec_kernel",	KSTAT_DATA_UINT64 },
233 	{ "wait_ticks_io", 	KSTAT_DATA_UINT64 },
234 	{ "bread", 		KSTAT_DATA_UINT64 },
235 	{ "bwrite", 		KSTAT_DATA_UINT64 },
236 	{ "lread", 		KSTAT_DATA_UINT64 },
237 	{ "lwrite", 		KSTAT_DATA_UINT64 },
238 	{ "phread", 		KSTAT_DATA_UINT64 },
239 	{ "phwrite", 		KSTAT_DATA_UINT64 },
240 	{ "pswitch", 		KSTAT_DATA_UINT64 },
241 	{ "trap", 		KSTAT_DATA_UINT64 },
242 	{ "intr", 		KSTAT_DATA_UINT64 },
243 	{ "syscall", 		KSTAT_DATA_UINT64 },
244 	{ "sysread", 		KSTAT_DATA_UINT64 },
245 	{ "syswrite", 		KSTAT_DATA_UINT64 },
246 	{ "sysfork", 		KSTAT_DATA_UINT64 },
247 	{ "sysvfork", 		KSTAT_DATA_UINT64 },
248 	{ "sysexec", 		KSTAT_DATA_UINT64 },
249 	{ "readch", 		KSTAT_DATA_UINT64 },
250 	{ "writech", 		KSTAT_DATA_UINT64 },
251 	{ "rcvint", 		KSTAT_DATA_UINT64 },
252 	{ "xmtint", 		KSTAT_DATA_UINT64 },
253 	{ "mdmint", 		KSTAT_DATA_UINT64 },
254 	{ "rawch", 		KSTAT_DATA_UINT64 },
255 	{ "canch", 		KSTAT_DATA_UINT64 },
256 	{ "outch", 		KSTAT_DATA_UINT64 },
257 	{ "msg", 		KSTAT_DATA_UINT64 },
258 	{ "sema", 		KSTAT_DATA_UINT64 },
259 	{ "namei", 		KSTAT_DATA_UINT64 },
260 	{ "ufsiget", 		KSTAT_DATA_UINT64 },
261 	{ "ufsdirblk", 		KSTAT_DATA_UINT64 },
262 	{ "ufsipage", 		KSTAT_DATA_UINT64 },
263 	{ "ufsinopage", 	KSTAT_DATA_UINT64 },
264 	{ "procovf", 		KSTAT_DATA_UINT64 },
265 	{ "intrthread", 	KSTAT_DATA_UINT64 },
266 	{ "intrblk", 		KSTAT_DATA_UINT64 },
267 	{ "intrunpin",		KSTAT_DATA_UINT64 },
268 	{ "idlethread", 	KSTAT_DATA_UINT64 },
269 	{ "inv_swtch", 		KSTAT_DATA_UINT64 },
270 	{ "nthreads", 		KSTAT_DATA_UINT64 },
271 	{ "cpumigrate", 	KSTAT_DATA_UINT64 },
272 	{ "xcalls", 		KSTAT_DATA_UINT64 },
273 	{ "mutex_adenters", 	KSTAT_DATA_UINT64 },
274 	{ "rw_rdfails", 	KSTAT_DATA_UINT64 },
275 	{ "rw_wrfails", 	KSTAT_DATA_UINT64 },
276 	{ "modload", 		KSTAT_DATA_UINT64 },
277 	{ "modunload", 		KSTAT_DATA_UINT64 },
278 	{ "bawrite", 		KSTAT_DATA_UINT64 },
279 	{ "iowait",		KSTAT_DATA_UINT64 },
280 };
281 
282 static struct cpu_vm_stats_ks_data {
283 	kstat_named_t pgrec;
284 	kstat_named_t pgfrec;
285 	kstat_named_t pgin;
286 	kstat_named_t pgpgin;
287 	kstat_named_t pgout;
288 	kstat_named_t pgpgout;
289 	kstat_named_t swapin;
290 	kstat_named_t pgswapin;
291 	kstat_named_t swapout;
292 	kstat_named_t pgswapout;
293 	kstat_named_t zfod;
294 	kstat_named_t dfree;
295 	kstat_named_t scan;
296 	kstat_named_t rev;
297 	kstat_named_t hat_fault;
298 	kstat_named_t as_fault;
299 	kstat_named_t maj_fault;
300 	kstat_named_t cow_fault;
301 	kstat_named_t prot_fault;
302 	kstat_named_t softlock;
303 	kstat_named_t kernel_asflt;
304 	kstat_named_t pgrrun;
305 	kstat_named_t execpgin;
306 	kstat_named_t execpgout;
307 	kstat_named_t execfree;
308 	kstat_named_t anonpgin;
309 	kstat_named_t anonpgout;
310 	kstat_named_t anonfree;
311 	kstat_named_t fspgin;
312 	kstat_named_t fspgout;
313 	kstat_named_t fsfree;
314 } cpu_vm_stats_ks_data_template = {
315 	{ "pgrec",		KSTAT_DATA_UINT64 },
316 	{ "pgfrec",		KSTAT_DATA_UINT64 },
317 	{ "pgin",		KSTAT_DATA_UINT64 },
318 	{ "pgpgin",		KSTAT_DATA_UINT64 },
319 	{ "pgout",		KSTAT_DATA_UINT64 },
320 	{ "pgpgout",		KSTAT_DATA_UINT64 },
321 	{ "swapin",		KSTAT_DATA_UINT64 },
322 	{ "pgswapin",		KSTAT_DATA_UINT64 },
323 	{ "swapout",		KSTAT_DATA_UINT64 },
324 	{ "pgswapout",		KSTAT_DATA_UINT64 },
325 	{ "zfod",		KSTAT_DATA_UINT64 },
326 	{ "dfree",		KSTAT_DATA_UINT64 },
327 	{ "scan",		KSTAT_DATA_UINT64 },
328 	{ "rev",		KSTAT_DATA_UINT64 },
329 	{ "hat_fault",		KSTAT_DATA_UINT64 },
330 	{ "as_fault",		KSTAT_DATA_UINT64 },
331 	{ "maj_fault",		KSTAT_DATA_UINT64 },
332 	{ "cow_fault",		KSTAT_DATA_UINT64 },
333 	{ "prot_fault",		KSTAT_DATA_UINT64 },
334 	{ "softlock",		KSTAT_DATA_UINT64 },
335 	{ "kernel_asflt",	KSTAT_DATA_UINT64 },
336 	{ "pgrrun",		KSTAT_DATA_UINT64 },
337 	{ "execpgin",		KSTAT_DATA_UINT64 },
338 	{ "execpgout",		KSTAT_DATA_UINT64 },
339 	{ "execfree",		KSTAT_DATA_UINT64 },
340 	{ "anonpgin",		KSTAT_DATA_UINT64 },
341 	{ "anonpgout",		KSTAT_DATA_UINT64 },
342 	{ "anonfree",		KSTAT_DATA_UINT64 },
343 	{ "fspgin",		KSTAT_DATA_UINT64 },
344 	{ "fspgout",		KSTAT_DATA_UINT64 },
345 	{ "fsfree",		KSTAT_DATA_UINT64 },
346 };
347 
348 /*
349  * Force the specified thread to migrate to the appropriate processor.
350  * Called with thread lock held, returns with it dropped.
351  */
352 static void
353 force_thread_migrate(kthread_id_t tp)
354 {
355 	ASSERT(THREAD_LOCK_HELD(tp));
356 	if (tp == curthread) {
357 		THREAD_TRANSITION(tp);
358 		CL_SETRUN(tp);
359 		thread_unlock_nopreempt(tp);
360 		swtch();
361 	} else {
362 		if (tp->t_state == TS_ONPROC) {
363 			cpu_surrender(tp);
364 		} else if (tp->t_state == TS_RUN) {
365 			(void) dispdeq(tp);
366 			setbackdq(tp);
367 		}
368 		thread_unlock(tp);
369 	}
370 }
371 
372 /*
373  * Set affinity for a specified CPU.
374  * A reference count is incremented and the affinity is held until the
375  * reference count is decremented to zero by thread_affinity_clear().
376  * This is so regions of code requiring affinity can be nested.
377  * Caller needs to ensure that cpu_id remains valid, which can be
378  * done by holding cpu_lock across this call, unless the caller
379  * specifies CPU_CURRENT in which case the cpu_lock will be acquired
380  * by thread_affinity_set and CPU->cpu_id will be the target CPU.
381  */
382 void
383 thread_affinity_set(kthread_id_t t, int cpu_id)
384 {
385 	cpu_t		*cp;
386 	int		c;
387 
388 	ASSERT(!(t == curthread && t->t_weakbound_cpu != NULL));
389 
390 	if ((c = cpu_id) == CPU_CURRENT) {
391 		mutex_enter(&cpu_lock);
392 		cpu_id = CPU->cpu_id;
393 	}
394 	/*
395 	 * We should be asserting that cpu_lock is held here, but
396 	 * the NCA code doesn't acquire it.  The following assert
397 	 * should be uncommented when the NCA code is fixed.
398 	 *
399 	 * ASSERT(MUTEX_HELD(&cpu_lock));
400 	 */
401 	ASSERT((cpu_id >= 0) && (cpu_id < NCPU));
402 	cp = cpu[cpu_id];
403 	ASSERT(cp != NULL);		/* user must provide a good cpu_id */
404 	/*
405 	 * If there is already a hard affinity requested, and this affinity
406 	 * conflicts with that, panic.
407 	 */
408 	thread_lock(t);
409 	if (t->t_affinitycnt > 0 && t->t_bound_cpu != cp) {
410 		panic("affinity_set: setting %p but already bound to %p",
411 		    (void *)cp, (void *)t->t_bound_cpu);
412 	}
413 	t->t_affinitycnt++;
414 	t->t_bound_cpu = cp;
415 
416 	/*
417 	 * Make sure we're running on the right CPU.
418 	 */
419 	if (cp != t->t_cpu || t != curthread) {
420 		force_thread_migrate(t);	/* drops thread lock */
421 	} else {
422 		thread_unlock(t);
423 	}
424 
425 	if (c == CPU_CURRENT)
426 		mutex_exit(&cpu_lock);
427 }
428 
429 /*
430  *	Wrapper for backward compatibility.
431  */
432 void
433 affinity_set(int cpu_id)
434 {
435 	thread_affinity_set(curthread, cpu_id);
436 }
437 
438 /*
439  * Decrement the affinity reservation count and if it becomes zero,
440  * clear the CPU affinity for the current thread, or set it to the user's
441  * software binding request.
442  */
443 void
444 thread_affinity_clear(kthread_id_t t)
445 {
446 	register processorid_t binding;
447 
448 	thread_lock(t);
449 	if (--t->t_affinitycnt == 0) {
450 		if ((binding = t->t_bind_cpu) == PBIND_NONE) {
451 			/*
452 			 * Adjust disp_max_unbound_pri if necessary.
453 			 */
454 			disp_adjust_unbound_pri(t);
455 			t->t_bound_cpu = NULL;
456 			if (t->t_cpu->cpu_part != t->t_cpupart) {
457 				force_thread_migrate(t);
458 				return;
459 			}
460 		} else {
461 			t->t_bound_cpu = cpu[binding];
462 			/*
463 			 * Make sure the thread is running on the bound CPU.
464 			 */
465 			if (t->t_cpu != t->t_bound_cpu) {
466 				force_thread_migrate(t);
467 				return;		/* already dropped lock */
468 			}
469 		}
470 	}
471 	thread_unlock(t);
472 }
473 
474 /*
475  * Wrapper for backward compatibility.
476  */
477 void
478 affinity_clear(void)
479 {
480 	thread_affinity_clear(curthread);
481 }
482 
483 /*
484  * Weak cpu affinity.  Bind to the "current" cpu for short periods
485  * of time during which the thread must not block (but may be preempted).
486  * Use this instead of kpreempt_disable() when it is only "no migration"
487  * rather than "no preemption" semantics that are required - disabling
488  * preemption holds higher priority threads off of cpu and if the
489  * operation that is protected is more than momentary this is not good
490  * for realtime etc.
491  *
492  * Weakly bound threads will not prevent a cpu from being offlined -
493  * we'll only run them on the cpu to which they are weakly bound but
494  * (because they do not block) we'll always be able to move them on to
495  * another cpu at offline time if we give them just a short moment to
496  * run during which they will unbind.  To give a cpu a chance of offlining,
497  * however, we require a barrier to weak bindings that may be raised for a
498  * given cpu (offline/move code may set this and then wait a short time for
499  * existing weak bindings to drop); the cpu_inmotion pointer is that barrier.
500  *
501  * There are few restrictions on the calling context of thread_nomigrate.
502  * The caller must not hold the thread lock.  Calls may be nested.
503  *
504  * After weakbinding a thread must not perform actions that may block.
505  * In particular it must not call thread_affinity_set; calling that when
506  * already weakbound is nonsensical anyway.
507  *
508  * If curthread is prevented from migrating for other reasons
509  * (kernel preemption disabled; high pil; strongly bound; interrupt thread)
510  * then the weak binding will succeed even if this cpu is the target of an
511  * offline/move request.
512  */
513 void
514 thread_nomigrate(void)
515 {
516 	cpu_t *cp;
517 	kthread_id_t t = curthread;
518 
519 again:
520 	kpreempt_disable();
521 	cp = CPU;
522 
523 	/*
524 	 * A highlevel interrupt must not modify t_nomigrate or
525 	 * t_weakbound_cpu of the thread it has interrupted.  A lowlevel
526 	 * interrupt thread cannot migrate and we can avoid the
527 	 * thread_lock call below by short-circuiting here.  In either
528 	 * case we can just return since no migration is possible and
529 	 * the condition will persist (ie, when we test for these again
530 	 * in thread_allowmigrate they can't have changed).   Migration
531 	 * is also impossible if we're at or above DISP_LEVEL pil.
532 	 */
533 	if (CPU_ON_INTR(cp) || t->t_flag & T_INTR_THREAD ||
534 	    getpil() >= DISP_LEVEL) {
535 		kpreempt_enable();
536 		return;
537 	}
538 
539 	/*
540 	 * We must be consistent with existing weak bindings.  Since we
541 	 * may be interrupted between the increment of t_nomigrate and
542 	 * the store to t_weakbound_cpu below we cannot assume that
543 	 * t_weakbound_cpu will be set if t_nomigrate is.  Note that we
544 	 * cannot assert t_weakbound_cpu == t_bind_cpu since that is not
545 	 * always the case.
546 	 */
547 	if (t->t_nomigrate && t->t_weakbound_cpu && t->t_weakbound_cpu != cp) {
548 		if (!panicstr)
549 			panic("thread_nomigrate: binding to %p but already "
550 			    "bound to %p", (void *)cp,
551 			    (void *)t->t_weakbound_cpu);
552 	}
553 
554 	/*
555 	 * At this point we have preemption disabled and we don't yet hold
556 	 * the thread lock.  So it's possible that somebody else could
557 	 * set t_bind_cpu here and not be able to force us across to the
558 	 * new cpu (since we have preemption disabled).
559 	 */
560 	thread_lock(curthread);
561 
562 	/*
563 	 * If further weak bindings are being (temporarily) suppressed then
564 	 * we'll settle for disabling kernel preemption (which assures
565 	 * no migration provided the thread does not block which it is
566 	 * not allowed to if using thread_nomigrate).  We must remember
567 	 * this disposition so we can take appropriate action in
568 	 * thread_allowmigrate.  If this is a nested call and the
569 	 * thread is already weakbound then fall through as normal.
570 	 * We remember the decision to settle for kpreempt_disable through
571 	 * negative nesting counting in t_nomigrate.  Once a thread has had one
572 	 * weakbinding request satisfied in this way any further (nested)
573 	 * requests will continue to be satisfied in the same way,
574 	 * even if weak bindings have recommenced.
575 	 */
576 	if (t->t_nomigrate < 0 || weakbindingbarrier && t->t_nomigrate == 0) {
577 		--t->t_nomigrate;
578 		thread_unlock(curthread);
579 		return;		/* with kpreempt_disable still active */
580 	}
581 
582 	/*
583 	 * We hold thread_lock so t_bind_cpu cannot change.  We could,
584 	 * however, be running on a different cpu to which we are t_bound_cpu
585 	 * to (as explained above).  If we grant the weak binding request
586 	 * in that case then the dispatcher must favour our weak binding
587 	 * over our strong (in which case, just as when preemption is
588 	 * disabled, we can continue to run on a cpu other than the one to
589 	 * which we are strongbound; the difference in this case is that
590 	 * this thread can be preempted and so can appear on the dispatch
591 	 * queues of a cpu other than the one it is strongbound to).
592 	 *
593 	 * If the cpu we are running on does not appear to be a current
594 	 * offline target (we check cpu_inmotion to determine this - since
595 	 * we don't hold cpu_lock we may not see a recent store to that,
596 	 * so it's possible that we at times can grant a weak binding to a
597 	 * cpu that is an offline target, but that one request will not
598 	 * prevent the offline from succeeding) then we will always grant
599 	 * the weak binding request.  This includes the case above where
600 	 * we grant a weakbinding not commensurate with our strong binding.
601 	 *
602 	 * If our cpu does appear to be an offline target then we're inclined
603 	 * not to grant the weakbinding request just yet - we'd prefer to
604 	 * migrate to another cpu and grant the request there.  The
605 	 * exceptions are those cases where going through preemption code
606 	 * will not result in us changing cpu:
607 	 *
608 	 *	. interrupts have already bypassed this case (see above)
609 	 *	. we are already weakbound to this cpu (dispatcher code will
610 	 *	  always return us to the weakbound cpu)
611 	 *	. preemption was disabled even before we disabled it above
612 	 *	. we are strongbound to this cpu (if we're strongbound to
613 	 *	another and not yet running there the trip through the
614 	 *	dispatcher will move us to the strongbound cpu and we
615 	 *	will grant the weak binding there)
616 	 */
617 	if (cp != cpu_inmotion || t->t_nomigrate > 0 || t->t_preempt > 1 ||
618 	    t->t_bound_cpu == cp) {
619 		/*
620 		 * Don't be tempted to store to t_weakbound_cpu only on
621 		 * the first nested bind request - if we're interrupted
622 		 * after the increment of t_nomigrate and before the
623 		 * store to t_weakbound_cpu and the interrupt calls
624 		 * thread_nomigrate then the assertion in thread_allowmigrate
625 		 * would fail.
626 		 */
627 		t->t_nomigrate++;
628 		t->t_weakbound_cpu = cp;
629 		membar_producer();
630 		thread_unlock(curthread);
631 		/*
632 		 * Now that we have dropped the thread_lock another thread
633 		 * can set our t_weakbound_cpu, and will try to migrate us
634 		 * to the strongbound cpu (which will not be prevented by
635 		 * preemption being disabled since we're about to enable
636 		 * preemption).  We have granted the weakbinding to the current
637 		 * cpu, so again we are in the position that is is is possible
638 		 * that our weak and strong bindings differ.  Again this
639 		 * is catered for by dispatcher code which will favour our
640 		 * weak binding.
641 		 */
642 		kpreempt_enable();
643 	} else {
644 		/*
645 		 * Move to another cpu before granting the request by
646 		 * forcing this thread through preemption code.  When we
647 		 * get to set{front,back}dq called from CL_PREEMPT()
648 		 * cpu_choose() will be used to select a cpu to queue
649 		 * us on - that will see cpu_inmotion and take
650 		 * steps to avoid returning us to this cpu.
651 		 */
652 		cp->cpu_kprunrun = 1;
653 		thread_unlock(curthread);
654 		kpreempt_enable();	/* will call preempt() */
655 		goto again;
656 	}
657 }
658 
659 void
660 thread_allowmigrate(void)
661 {
662 	kthread_id_t t = curthread;
663 
664 	ASSERT(t->t_weakbound_cpu == CPU ||
665 	    (t->t_nomigrate < 0 && t->t_preempt > 0) ||
666 	    CPU_ON_INTR(CPU) || t->t_flag & T_INTR_THREAD ||
667 	    getpil() >= DISP_LEVEL);
668 
669 	if (CPU_ON_INTR(CPU) || (t->t_flag & T_INTR_THREAD) ||
670 	    getpil() >= DISP_LEVEL)
671 		return;
672 
673 	if (t->t_nomigrate < 0) {
674 		/*
675 		 * This thread was granted "weak binding" in the
676 		 * stronger form of kernel preemption disabling.
677 		 * Undo a level of nesting for both t_nomigrate
678 		 * and t_preempt.
679 		 */
680 		++t->t_nomigrate;
681 		kpreempt_enable();
682 	} else if (--t->t_nomigrate == 0) {
683 		/*
684 		 * Time to drop the weak binding.  We need to cater
685 		 * for the case where we're weakbound to a different
686 		 * cpu than that to which we're strongbound (a very
687 		 * temporary arrangement that must only persist until
688 		 * weak binding drops).  We don't acquire thread_lock
689 		 * here so even as this code executes t_bound_cpu
690 		 * may be changing.  So we disable preemption and
691 		 * a) in the case that t_bound_cpu changes while we
692 		 * have preemption disabled kprunrun will be set
693 		 * asynchronously, and b) if before disabling
694 		 * preemption we were already on a different cpu to
695 		 * our t_bound_cpu then we set kprunrun ourselves
696 		 * to force a trip through the dispatcher when
697 		 * preemption is enabled.
698 		 */
699 		kpreempt_disable();
700 		if (t->t_bound_cpu &&
701 		    t->t_weakbound_cpu != t->t_bound_cpu)
702 			CPU->cpu_kprunrun = 1;
703 		t->t_weakbound_cpu = NULL;
704 		membar_producer();
705 		kpreempt_enable();
706 	}
707 }
708 
709 /*
710  * weakbinding_stop can be used to temporarily cause weakbindings made
711  * with thread_nomigrate to be satisfied through the stronger action of
712  * kpreempt_disable.  weakbinding_start recommences normal weakbinding.
713  */
714 
715 void
716 weakbinding_stop(void)
717 {
718 	ASSERT(MUTEX_HELD(&cpu_lock));
719 	weakbindingbarrier = 1;
720 	membar_producer();	/* make visible before subsequent thread_lock */
721 }
722 
723 void
724 weakbinding_start(void)
725 {
726 	ASSERT(MUTEX_HELD(&cpu_lock));
727 	weakbindingbarrier = 0;
728 }
729 
730 /*
731  * This routine is called to place the CPUs in a safe place so that
732  * one of them can be taken off line or placed on line.  What we are
733  * trying to do here is prevent a thread from traversing the list
734  * of active CPUs while we are changing it or from getting placed on
735  * the run queue of a CPU that has just gone off line.  We do this by
736  * creating a thread with the highest possible prio for each CPU and
737  * having it call this routine.  The advantage of this method is that
738  * we can eliminate all checks for CPU_ACTIVE in the disp routines.
739  * This makes disp faster at the expense of making p_online() slower
740  * which is a good trade off.
741  */
742 static void
743 cpu_pause(volatile char *safe)
744 {
745 	int s;
746 	struct _cpu_pause_info *cpi = &cpu_pause_info;
747 
748 	ASSERT((curthread->t_bound_cpu != NULL) || (*safe == PAUSE_DIE));
749 
750 	while (*safe != PAUSE_DIE) {
751 		*safe = PAUSE_READY;
752 		membar_enter();		/* make sure stores are flushed */
753 		sema_v(&cpi->cp_sem);	/* signal requesting thread */
754 
755 		/*
756 		 * Wait here until all pause threads are running.  That
757 		 * indicates that it's safe to do the spl.  Until
758 		 * cpu_pause_info.cp_go is set, we don't want to spl
759 		 * because that might block clock interrupts needed
760 		 * to preempt threads on other CPUs.
761 		 */
762 		while (cpi->cp_go == 0)
763 			;
764 		/*
765 		 * Even though we are at the highest disp prio, we need
766 		 * to block out all interrupts below LOCK_LEVEL so that
767 		 * an intr doesn't come in, wake up a thread, and call
768 		 * setbackdq/setfrontdq.
769 		 */
770 		s = splhigh();
771 		/*
772 		 * This cpu is now safe.
773 		 */
774 		*safe = PAUSE_WAIT;
775 		membar_enter();		/* make sure stores are flushed */
776 
777 		/*
778 		 * Now we wait.  When we are allowed to continue, safe will
779 		 * be set to PAUSE_IDLE.
780 		 */
781 		while (*safe != PAUSE_IDLE)
782 			;
783 
784 		splx(s);
785 		/*
786 		 * Waiting is at an end. Switch out of cpu_pause
787 		 * loop and resume useful work.
788 		 */
789 		swtch();
790 	}
791 
792 	mutex_enter(&pause_free_mutex);
793 	*safe = PAUSE_DEAD;
794 	cv_broadcast(&pause_free_cv);
795 	mutex_exit(&pause_free_mutex);
796 }
797 
798 /*
799  * Allow the cpus to start running again.
800  */
801 void
802 start_cpus()
803 {
804 	int i;
805 
806 	ASSERT(MUTEX_HELD(&cpu_lock));
807 	ASSERT(cpu_pause_info.cp_paused);
808 	cpu_pause_info.cp_paused = NULL;
809 	for (i = 0; i < NCPU; i++)
810 		safe_list[i] = PAUSE_IDLE;
811 	membar_enter();			/* make sure stores are flushed */
812 	affinity_clear();
813 	splx(cpu_pause_info.cp_spl);
814 	kpreempt_enable();
815 }
816 
817 /*
818  * Allocate a pause thread for a CPU.
819  */
820 static void
821 cpu_pause_alloc(cpu_t *cp)
822 {
823 	kthread_id_t	t;
824 	int		cpun = cp->cpu_id;
825 
826 	/*
827 	 * Note, v.v_nglobpris will not change value as long as I hold
828 	 * cpu_lock.
829 	 */
830 	t = thread_create(NULL, 0, cpu_pause, (caddr_t)&safe_list[cpun],
831 	    0, &p0, TS_STOPPED, v.v_nglobpris - 1);
832 	thread_lock(t);
833 	t->t_bound_cpu = cp;
834 	t->t_disp_queue = cp->cpu_disp;
835 	t->t_affinitycnt = 1;
836 	t->t_preempt = 1;
837 	thread_unlock(t);
838 	cp->cpu_pause_thread = t;
839 	/*
840 	 * Registering a thread in the callback table is usually done
841 	 * in the initialization code of the thread.  In this
842 	 * case, we do it right after thread creation because the
843 	 * thread itself may never run, and we need to register the
844 	 * fact that it is safe for cpr suspend.
845 	 */
846 	CALLB_CPR_INIT_SAFE(t, "cpu_pause");
847 }
848 
849 /*
850  * Free a pause thread for a CPU.
851  */
852 static void
853 cpu_pause_free(cpu_t *cp)
854 {
855 	kthread_id_t	t;
856 	int		cpun = cp->cpu_id;
857 
858 	ASSERT(MUTEX_HELD(&cpu_lock));
859 	/*
860 	 * We have to get the thread and tell him to die.
861 	 */
862 	if ((t = cp->cpu_pause_thread) == NULL) {
863 		ASSERT(safe_list[cpun] == PAUSE_IDLE);
864 		return;
865 	}
866 	thread_lock(t);
867 	t->t_cpu = CPU;		/* disp gets upset if last cpu is quiesced. */
868 	t->t_bound_cpu = NULL;	/* Must un-bind; cpu may not be running. */
869 	t->t_pri = v.v_nglobpris - 1;
870 	ASSERT(safe_list[cpun] == PAUSE_IDLE);
871 	safe_list[cpun] = PAUSE_DIE;
872 	THREAD_TRANSITION(t);
873 	setbackdq(t);
874 	thread_unlock_nopreempt(t);
875 
876 	/*
877 	 * If we don't wait for the thread to actually die, it may try to
878 	 * run on the wrong cpu as part of an actual call to pause_cpus().
879 	 */
880 	mutex_enter(&pause_free_mutex);
881 	while (safe_list[cpun] != PAUSE_DEAD) {
882 		cv_wait(&pause_free_cv, &pause_free_mutex);
883 	}
884 	mutex_exit(&pause_free_mutex);
885 	safe_list[cpun] = PAUSE_IDLE;
886 
887 	cp->cpu_pause_thread = NULL;
888 }
889 
890 /*
891  * Initialize basic structures for pausing CPUs.
892  */
893 void
894 cpu_pause_init()
895 {
896 	sema_init(&cpu_pause_info.cp_sem, 0, NULL, SEMA_DEFAULT, NULL);
897 	/*
898 	 * Create initial CPU pause thread.
899 	 */
900 	cpu_pause_alloc(CPU);
901 }
902 
903 /*
904  * Start the threads used to pause another CPU.
905  */
906 static int
907 cpu_pause_start(processorid_t cpu_id)
908 {
909 	int	i;
910 	int	cpu_count = 0;
911 
912 	for (i = 0; i < NCPU; i++) {
913 		cpu_t		*cp;
914 		kthread_id_t	t;
915 
916 		cp = cpu[i];
917 		if (!CPU_IN_SET(cpu_available, i) || (i == cpu_id)) {
918 			safe_list[i] = PAUSE_WAIT;
919 			continue;
920 		}
921 
922 		/*
923 		 * Skip CPU if it is quiesced or not yet started.
924 		 */
925 		if ((cp->cpu_flags & (CPU_QUIESCED | CPU_READY)) != CPU_READY) {
926 			safe_list[i] = PAUSE_WAIT;
927 			continue;
928 		}
929 
930 		/*
931 		 * Start this CPU's pause thread.
932 		 */
933 		t = cp->cpu_pause_thread;
934 		thread_lock(t);
935 		/*
936 		 * Reset the priority, since nglobpris may have
937 		 * changed since the thread was created, if someone
938 		 * has loaded the RT (or some other) scheduling
939 		 * class.
940 		 */
941 		t->t_pri = v.v_nglobpris - 1;
942 		THREAD_TRANSITION(t);
943 		setbackdq(t);
944 		thread_unlock_nopreempt(t);
945 		++cpu_count;
946 	}
947 	return (cpu_count);
948 }
949 
950 
951 /*
952  * Pause all of the CPUs except the one we are on by creating a high
953  * priority thread bound to those CPUs.
954  *
955  * Note that one must be extremely careful regarding code
956  * executed while CPUs are paused.  Since a CPU may be paused
957  * while a thread scheduling on that CPU is holding an adaptive
958  * lock, code executed with CPUs paused must not acquire adaptive
959  * (or low-level spin) locks.  Also, such code must not block,
960  * since the thread that is supposed to initiate the wakeup may
961  * never run.
962  *
963  * With a few exceptions, the restrictions on code executed with CPUs
964  * paused match those for code executed at high-level interrupt
965  * context.
966  */
967 void
968 pause_cpus(cpu_t *off_cp)
969 {
970 	processorid_t	cpu_id;
971 	int		i;
972 	struct _cpu_pause_info	*cpi = &cpu_pause_info;
973 
974 	ASSERT(MUTEX_HELD(&cpu_lock));
975 	ASSERT(cpi->cp_paused == NULL);
976 	cpi->cp_count = 0;
977 	cpi->cp_go = 0;
978 	for (i = 0; i < NCPU; i++)
979 		safe_list[i] = PAUSE_IDLE;
980 	kpreempt_disable();
981 
982 	/*
983 	 * If running on the cpu that is going offline, get off it.
984 	 * This is so that it won't be necessary to rechoose a CPU
985 	 * when done.
986 	 */
987 	if (CPU == off_cp)
988 		cpu_id = off_cp->cpu_next_part->cpu_id;
989 	else
990 		cpu_id = CPU->cpu_id;
991 	affinity_set(cpu_id);
992 
993 	/*
994 	 * Start the pause threads and record how many were started
995 	 */
996 	cpi->cp_count = cpu_pause_start(cpu_id);
997 
998 	/*
999 	 * Now wait for all CPUs to be running the pause thread.
1000 	 */
1001 	while (cpi->cp_count > 0) {
1002 		/*
1003 		 * Spin reading the count without grabbing the disp
1004 		 * lock to make sure we don't prevent the pause
1005 		 * threads from getting the lock.
1006 		 */
1007 		while (sema_held(&cpi->cp_sem))
1008 			;
1009 		if (sema_tryp(&cpi->cp_sem))
1010 			--cpi->cp_count;
1011 	}
1012 	cpi->cp_go = 1;			/* all have reached cpu_pause */
1013 
1014 	/*
1015 	 * Now wait for all CPUs to spl. (Transition from PAUSE_READY
1016 	 * to PAUSE_WAIT.)
1017 	 */
1018 	for (i = 0; i < NCPU; i++) {
1019 		while (safe_list[i] != PAUSE_WAIT)
1020 			;
1021 	}
1022 	cpi->cp_spl = splhigh();	/* block dispatcher on this CPU */
1023 	cpi->cp_paused = curthread;
1024 }
1025 
1026 /*
1027  * Check whether the current thread has CPUs paused
1028  */
1029 int
1030 cpus_paused(void)
1031 {
1032 	if (cpu_pause_info.cp_paused != NULL) {
1033 		ASSERT(cpu_pause_info.cp_paused == curthread);
1034 		return (1);
1035 	}
1036 	return (0);
1037 }
1038 
1039 static cpu_t *
1040 cpu_get_all(processorid_t cpun)
1041 {
1042 	ASSERT(MUTEX_HELD(&cpu_lock));
1043 
1044 	if (cpun >= NCPU || cpun < 0 || !CPU_IN_SET(cpu_available, cpun))
1045 		return (NULL);
1046 	return (cpu[cpun]);
1047 }
1048 
1049 /*
1050  * Check whether cpun is a valid processor id and whether it should be
1051  * visible from the current zone. If it is, return a pointer to the
1052  * associated CPU structure.
1053  */
1054 cpu_t *
1055 cpu_get(processorid_t cpun)
1056 {
1057 	cpu_t *c;
1058 
1059 	ASSERT(MUTEX_HELD(&cpu_lock));
1060 	c = cpu_get_all(cpun);
1061 	if (c != NULL && !INGLOBALZONE(curproc) && pool_pset_enabled() &&
1062 	    zone_pset_get(curproc->p_zone) != cpupart_query_cpu(c))
1063 		return (NULL);
1064 	return (c);
1065 }
1066 
1067 /*
1068  * The following functions should be used to check CPU states in the kernel.
1069  * They should be invoked with cpu_lock held.  Kernel subsystems interested
1070  * in CPU states should *not* use cpu_get_state() and various P_ONLINE/etc
1071  * states.  Those are for user-land (and system call) use only.
1072  */
1073 
1074 /*
1075  * Determine whether the CPU is online and handling interrupts.
1076  */
1077 int
1078 cpu_is_online(cpu_t *cpu)
1079 {
1080 	ASSERT(MUTEX_HELD(&cpu_lock));
1081 	return (cpu_flagged_online(cpu->cpu_flags));
1082 }
1083 
1084 /*
1085  * Determine whether the CPU is offline (this includes spare and faulted).
1086  */
1087 int
1088 cpu_is_offline(cpu_t *cpu)
1089 {
1090 	ASSERT(MUTEX_HELD(&cpu_lock));
1091 	return (cpu_flagged_offline(cpu->cpu_flags));
1092 }
1093 
1094 /*
1095  * Determine whether the CPU is powered off.
1096  */
1097 int
1098 cpu_is_poweredoff(cpu_t *cpu)
1099 {
1100 	ASSERT(MUTEX_HELD(&cpu_lock));
1101 	return (cpu_flagged_poweredoff(cpu->cpu_flags));
1102 }
1103 
1104 /*
1105  * Determine whether the CPU is handling interrupts.
1106  */
1107 int
1108 cpu_is_nointr(cpu_t *cpu)
1109 {
1110 	ASSERT(MUTEX_HELD(&cpu_lock));
1111 	return (cpu_flagged_nointr(cpu->cpu_flags));
1112 }
1113 
1114 /*
1115  * Determine whether the CPU is active (scheduling threads).
1116  */
1117 int
1118 cpu_is_active(cpu_t *cpu)
1119 {
1120 	ASSERT(MUTEX_HELD(&cpu_lock));
1121 	return (cpu_flagged_active(cpu->cpu_flags));
1122 }
1123 
1124 /*
1125  * Same as above, but these require cpu_flags instead of cpu_t pointers.
1126  */
1127 int
1128 cpu_flagged_online(cpu_flag_t cpu_flags)
1129 {
1130 	return (cpu_flagged_active(cpu_flags) &&
1131 	    (cpu_flags & CPU_ENABLE));
1132 }
1133 
1134 int
1135 cpu_flagged_offline(cpu_flag_t cpu_flags)
1136 {
1137 	return (((cpu_flags & CPU_POWEROFF) == 0) &&
1138 	    ((cpu_flags & (CPU_READY | CPU_OFFLINE)) != CPU_READY));
1139 }
1140 
1141 int
1142 cpu_flagged_poweredoff(cpu_flag_t cpu_flags)
1143 {
1144 	return ((cpu_flags & CPU_POWEROFF) == CPU_POWEROFF);
1145 }
1146 
1147 int
1148 cpu_flagged_nointr(cpu_flag_t cpu_flags)
1149 {
1150 	return (cpu_flagged_active(cpu_flags) &&
1151 	    (cpu_flags & CPU_ENABLE) == 0);
1152 }
1153 
1154 int
1155 cpu_flagged_active(cpu_flag_t cpu_flags)
1156 {
1157 	return (((cpu_flags & (CPU_POWEROFF | CPU_FAULTED | CPU_SPARE)) == 0) &&
1158 	    ((cpu_flags & (CPU_READY | CPU_OFFLINE)) == CPU_READY));
1159 }
1160 
1161 /*
1162  * Bring the indicated CPU online.
1163  */
1164 int
1165 cpu_online(cpu_t *cp)
1166 {
1167 	int	error = 0;
1168 
1169 	/*
1170 	 * Handle on-line request.
1171 	 *	This code must put the new CPU on the active list before
1172 	 *	starting it because it will not be paused, and will start
1173 	 * 	using the active list immediately.  The real start occurs
1174 	 *	when the CPU_QUIESCED flag is turned off.
1175 	 */
1176 
1177 	ASSERT(MUTEX_HELD(&cpu_lock));
1178 
1179 	/*
1180 	 * Put all the cpus into a known safe place.
1181 	 * No mutexes can be entered while CPUs are paused.
1182 	 */
1183 	error = mp_cpu_start(cp);	/* arch-dep hook */
1184 	if (error == 0) {
1185 		pause_cpus(NULL);
1186 		cpu_add_active_internal(cp);
1187 		if (cp->cpu_flags & CPU_FAULTED) {
1188 			cp->cpu_flags &= ~CPU_FAULTED;
1189 			mp_cpu_faulted_exit(cp);
1190 		}
1191 		cp->cpu_flags &= ~(CPU_QUIESCED | CPU_OFFLINE | CPU_FROZEN |
1192 		    CPU_SPARE);
1193 		start_cpus();
1194 		cpu_stats_kstat_create(cp);
1195 		cpu_create_intrstat(cp);
1196 		lgrp_kstat_create(cp);
1197 		cpu_state_change_notify(cp->cpu_id, CPU_ON);
1198 		cpu_intr_enable(cp);	/* arch-dep hook */
1199 		cyclic_online(cp);
1200 		poke_cpu(cp->cpu_id);
1201 	}
1202 
1203 	return (error);
1204 }
1205 
1206 /*
1207  * Take the indicated CPU offline.
1208  */
1209 int
1210 cpu_offline(cpu_t *cp, int flags)
1211 {
1212 	cpupart_t *pp;
1213 	int	error = 0;
1214 	cpu_t	*ncp;
1215 	int	intr_enable;
1216 	int	cyclic_off = 0;
1217 	int	loop_count;
1218 	int	no_quiesce = 0;
1219 	int	(*bound_func)(struct cpu *, int);
1220 	kthread_t *t;
1221 	lpl_t	*cpu_lpl;
1222 	proc_t	*p;
1223 	int	lgrp_diff_lpl;
1224 
1225 	ASSERT(MUTEX_HELD(&cpu_lock));
1226 
1227 	/*
1228 	 * If we're going from faulted or spare to offline, just
1229 	 * clear these flags and update CPU state.
1230 	 */
1231 	if (cp->cpu_flags & (CPU_FAULTED | CPU_SPARE)) {
1232 		if (cp->cpu_flags & CPU_FAULTED) {
1233 			cp->cpu_flags &= ~CPU_FAULTED;
1234 			mp_cpu_faulted_exit(cp);
1235 		}
1236 		cp->cpu_flags &= ~CPU_SPARE;
1237 		cpu_set_state(cp);
1238 		return (0);
1239 	}
1240 
1241 	/*
1242 	 * Handle off-line request.
1243 	 */
1244 	pp = cp->cpu_part;
1245 	/*
1246 	 * Don't offline last online CPU in partition
1247 	 */
1248 	if (ncpus_online <= 1 || pp->cp_ncpus <= 1 || cpu_intr_count(cp) < 2)
1249 		return (EBUSY);
1250 	/*
1251 	 * Unbind all thread bound to our CPU if we were asked to.
1252 	 */
1253 	if (flags & CPU_FORCED && (error = cpu_unbind(cp->cpu_id)) != 0)
1254 		return (error);
1255 	/*
1256 	 * We shouldn't be bound to this CPU ourselves.
1257 	 */
1258 	if (curthread->t_bound_cpu == cp)
1259 		return (EBUSY);
1260 
1261 	/*
1262 	 * Tell interested parties that this CPU is going offline.
1263 	 */
1264 	cpu_state_change_notify(cp->cpu_id, CPU_OFF);
1265 
1266 	/*
1267 	 * Take the CPU out of interrupt participation so we won't find
1268 	 * bound kernel threads.  If the architecture cannot completely
1269 	 * shut off interrupts on the CPU, don't quiesce it, but don't
1270 	 * run anything but interrupt thread... this is indicated by
1271 	 * the CPU_OFFLINE flag being on but the CPU_QUIESCE flag being
1272 	 * off.
1273 	 */
1274 	intr_enable = cp->cpu_flags & CPU_ENABLE;
1275 	if (intr_enable)
1276 		no_quiesce = cpu_intr_disable(cp);
1277 
1278 	/*
1279 	 * Record that we are aiming to offline this cpu.  This acts as
1280 	 * a barrier to further weak binding requests in thread_nomigrate
1281 	 * and also causes cpu_choose, disp_lowpri_cpu and setfrontdq to
1282 	 * lean away from this cpu.  Further strong bindings are already
1283 	 * avoided since we hold cpu_lock.  Since threads that are set
1284 	 * runnable around now and others coming off the target cpu are
1285 	 * directed away from the target, existing strong and weak bindings
1286 	 * (especially the latter) to the target cpu stand maximum chance of
1287 	 * being able to unbind during the short delay loop below (if other
1288 	 * unbound threads compete they may not see cpu in time to unbind
1289 	 * even if they would do so immediately.
1290 	 */
1291 	cpu_inmotion = cp;
1292 	membar_enter();
1293 
1294 	/*
1295 	 * Check for kernel threads (strong or weak) bound to that CPU.
1296 	 * Strongly bound threads may not unbind, and we'll have to return
1297 	 * EBUSY.  Weakly bound threads should always disappear - we've
1298 	 * stopped more weak binding with cpu_inmotion and existing
1299 	 * bindings will drain imminently (they may not block).  Nonetheless
1300 	 * we will wait for a fixed period for all bound threads to disappear.
1301 	 * Inactive interrupt threads are OK (they'll be in TS_FREE
1302 	 * state).  If test finds some bound threads, wait a few ticks
1303 	 * to give short-lived threads (such as interrupts) chance to
1304 	 * complete.  Note that if no_quiesce is set, i.e. this cpu
1305 	 * is required to service interrupts, then we take the route
1306 	 * that permits interrupt threads to be active (or bypassed).
1307 	 */
1308 	bound_func = no_quiesce ? disp_bound_threads : disp_bound_anythreads;
1309 
1310 again:	for (loop_count = 0; (*bound_func)(cp, 0); loop_count++) {
1311 		if (loop_count >= 5) {
1312 			error = EBUSY;	/* some threads still bound */
1313 			break;
1314 		}
1315 
1316 		/*
1317 		 * If some threads were assigned, give them
1318 		 * a chance to complete or move.
1319 		 *
1320 		 * This assumes that the clock_thread is not bound
1321 		 * to any CPU, because the clock_thread is needed to
1322 		 * do the delay(hz/100).
1323 		 *
1324 		 * Note: we still hold the cpu_lock while waiting for
1325 		 * the next clock tick.  This is OK since it isn't
1326 		 * needed for anything else except processor_bind(2),
1327 		 * and system initialization.  If we drop the lock,
1328 		 * we would risk another p_online disabling the last
1329 		 * processor.
1330 		 */
1331 		delay(hz/100);
1332 	}
1333 
1334 	if (error == 0 && cyclic_off == 0) {
1335 		if (!cyclic_offline(cp)) {
1336 			/*
1337 			 * We must have bound cyclics...
1338 			 */
1339 			error = EBUSY;
1340 			goto out;
1341 		}
1342 		cyclic_off = 1;
1343 	}
1344 
1345 	/*
1346 	 * Call mp_cpu_stop() to perform any special operations
1347 	 * needed for this machine architecture to offline a CPU.
1348 	 */
1349 	if (error == 0)
1350 		error = mp_cpu_stop(cp);	/* arch-dep hook */
1351 
1352 	/*
1353 	 * If that all worked, take the CPU offline and decrement
1354 	 * ncpus_online.
1355 	 */
1356 	if (error == 0) {
1357 		/*
1358 		 * Put all the cpus into a known safe place.
1359 		 * No mutexes can be entered while CPUs are paused.
1360 		 */
1361 		pause_cpus(cp);
1362 		/*
1363 		 * Repeat the operation, if necessary, to make sure that
1364 		 * all outstanding low-level interrupts run to completion
1365 		 * before we set the CPU_QUIESCED flag.  It's also possible
1366 		 * that a thread has weak bound to the cpu despite our raising
1367 		 * cpu_inmotion above since it may have loaded that
1368 		 * value before the barrier became visible (this would have
1369 		 * to be the thread that was on the target cpu at the time
1370 		 * we raised the barrier).
1371 		 */
1372 		if ((!no_quiesce && cp->cpu_intr_actv != 0) ||
1373 		    (*bound_func)(cp, 1)) {
1374 			start_cpus();
1375 			(void) mp_cpu_start(cp);
1376 			goto again;
1377 		}
1378 		ncp = cp->cpu_next_part;
1379 		cpu_lpl = cp->cpu_lpl;
1380 		ASSERT(cpu_lpl != NULL);
1381 
1382 		/*
1383 		 * Remove the CPU from the list of active CPUs.
1384 		 */
1385 		cpu_remove_active(cp);
1386 
1387 		/*
1388 		 * Walk the active process list and look for threads
1389 		 * whose home lgroup needs to be updated, or
1390 		 * the last CPU they run on is the one being offlined now.
1391 		 */
1392 
1393 		ASSERT(curthread->t_cpu != cp);
1394 		for (p = practive; p != NULL; p = p->p_next) {
1395 
1396 			t = p->p_tlist;
1397 
1398 			if (t == NULL)
1399 				continue;
1400 
1401 			lgrp_diff_lpl = 0;
1402 
1403 			do {
1404 				ASSERT(t->t_lpl != NULL);
1405 				/*
1406 				 * Taking last CPU in lpl offline
1407 				 * Rehome thread if it is in this lpl
1408 				 * Otherwise, update the count of how many
1409 				 * threads are in this CPU's lgroup but have
1410 				 * a different lpl.
1411 				 */
1412 
1413 				if (cpu_lpl->lpl_ncpu == 0) {
1414 					if (t->t_lpl == cpu_lpl)
1415 						lgrp_move_thread(t,
1416 						    lgrp_choose(t,
1417 						    t->t_cpupart), 0);
1418 					else if (t->t_lpl->lpl_lgrpid ==
1419 					    cpu_lpl->lpl_lgrpid)
1420 						lgrp_diff_lpl++;
1421 				}
1422 				ASSERT(t->t_lpl->lpl_ncpu > 0);
1423 
1424 				/*
1425 				 * Update CPU last ran on if it was this CPU
1426 				 */
1427 				if (t->t_cpu == cp && t->t_bound_cpu != cp)
1428 				    t->t_cpu = disp_lowpri_cpu(ncp, t->t_lpl,
1429 					t->t_pri, NULL);
1430 				ASSERT(t->t_cpu != cp || t->t_bound_cpu == cp ||
1431 				    t->t_weakbound_cpu == cp);
1432 
1433 				t = t->t_forw;
1434 			} while (t != p->p_tlist);
1435 
1436 			/*
1437 			 * Didn't find any threads in the same lgroup as this
1438 			 * CPU with a different lpl, so remove the lgroup from
1439 			 * the process lgroup bitmask.
1440 			 */
1441 
1442 			if (lgrp_diff_lpl == 0)
1443 				klgrpset_del(p->p_lgrpset, cpu_lpl->lpl_lgrpid);
1444 		}
1445 
1446 		/*
1447 		 * Walk thread list looking for threads that need to be
1448 		 * rehomed, since there are some threads that are not in
1449 		 * their process's p_tlist.
1450 		 */
1451 
1452 		t = curthread;
1453 		do {
1454 			ASSERT(t != NULL && t->t_lpl != NULL);
1455 
1456 			/*
1457 			 * Rehome threads with same lpl as this CPU when this
1458 			 * is the last CPU in the lpl.
1459 			 */
1460 
1461 			if ((cpu_lpl->lpl_ncpu == 0) && (t->t_lpl == cpu_lpl))
1462 				lgrp_move_thread(t,
1463 				    lgrp_choose(t, t->t_cpupart), 1);
1464 
1465 			ASSERT(t->t_lpl->lpl_ncpu > 0);
1466 
1467 			/*
1468 			 * Update CPU last ran on if it was this CPU
1469 			 */
1470 
1471 			if (t->t_cpu == cp && t->t_bound_cpu != cp) {
1472 				t->t_cpu = disp_lowpri_cpu(ncp,
1473 				    t->t_lpl, t->t_pri, NULL);
1474 			}
1475 			ASSERT(t->t_cpu != cp || t->t_bound_cpu == cp ||
1476 			    t->t_weakbound_cpu == cp);
1477 			t = t->t_next;
1478 
1479 		} while (t != curthread);
1480 		ASSERT((cp->cpu_flags & (CPU_FAULTED | CPU_SPARE)) == 0);
1481 		cp->cpu_flags |= CPU_OFFLINE;
1482 		disp_cpu_inactive(cp);
1483 		if (!no_quiesce)
1484 			cp->cpu_flags |= CPU_QUIESCED;
1485 		ncpus_online--;
1486 		cpu_set_state(cp);
1487 		cpu_inmotion = NULL;
1488 		start_cpus();
1489 		cpu_stats_kstat_destroy(cp);
1490 		cpu_delete_intrstat(cp);
1491 		lgrp_kstat_destroy(cp);
1492 	}
1493 
1494 out:
1495 	cpu_inmotion = NULL;
1496 
1497 	/*
1498 	 * If we failed, re-enable interrupts.
1499 	 * Do this even if cpu_intr_disable returned an error, because
1500 	 * it may have partially disabled interrupts.
1501 	 */
1502 	if (error && intr_enable)
1503 		cpu_intr_enable(cp);
1504 
1505 	/*
1506 	 * If we failed, but managed to offline the cyclic subsystem on this
1507 	 * CPU, bring it back online.
1508 	 */
1509 	if (error && cyclic_off)
1510 		cyclic_online(cp);
1511 
1512 	/*
1513 	 * If we failed, we need to notify everyone that this CPU is back on.
1514 	 */
1515 	if (error != 0)
1516 		cpu_state_change_notify(cp->cpu_id, CPU_ON);
1517 
1518 	return (error);
1519 }
1520 
1521 /*
1522  * Mark the indicated CPU as faulted, taking it offline.
1523  */
1524 int
1525 cpu_faulted(cpu_t *cp, int flags)
1526 {
1527 	int	error = 0;
1528 
1529 	ASSERT(MUTEX_HELD(&cpu_lock));
1530 	ASSERT(!cpu_is_poweredoff(cp));
1531 
1532 	if (cpu_is_offline(cp)) {
1533 		cp->cpu_flags &= ~CPU_SPARE;
1534 		cp->cpu_flags |= CPU_FAULTED;
1535 		mp_cpu_faulted_enter(cp);
1536 		cpu_set_state(cp);
1537 		return (0);
1538 	}
1539 
1540 	if ((error = cpu_offline(cp, flags)) == 0) {
1541 		cp->cpu_flags |= CPU_FAULTED;
1542 		mp_cpu_faulted_enter(cp);
1543 		cpu_set_state(cp);
1544 	}
1545 
1546 	return (error);
1547 }
1548 
1549 /*
1550  * Mark the indicated CPU as a spare, taking it offline.
1551  */
1552 int
1553 cpu_spare(cpu_t *cp, int flags)
1554 {
1555 	int	error = 0;
1556 
1557 	ASSERT(MUTEX_HELD(&cpu_lock));
1558 	ASSERT(!cpu_is_poweredoff(cp));
1559 
1560 	if (cpu_is_offline(cp)) {
1561 		if (cp->cpu_flags & CPU_FAULTED) {
1562 			cp->cpu_flags &= ~CPU_FAULTED;
1563 			mp_cpu_faulted_exit(cp);
1564 		}
1565 		cp->cpu_flags |= CPU_SPARE;
1566 		cpu_set_state(cp);
1567 		return (0);
1568 	}
1569 
1570 	if ((error = cpu_offline(cp, flags)) == 0) {
1571 		cp->cpu_flags |= CPU_SPARE;
1572 		cpu_set_state(cp);
1573 	}
1574 
1575 	return (error);
1576 }
1577 
1578 /*
1579  * Take the indicated CPU from poweroff to offline.
1580  */
1581 int
1582 cpu_poweron(cpu_t *cp)
1583 {
1584 	int	error = ENOTSUP;
1585 
1586 	ASSERT(MUTEX_HELD(&cpu_lock));
1587 	ASSERT(cpu_is_poweredoff(cp));
1588 
1589 	error = mp_cpu_poweron(cp);	/* arch-dep hook */
1590 	if (error == 0)
1591 		cpu_set_state(cp);
1592 
1593 	return (error);
1594 }
1595 
1596 /*
1597  * Take the indicated CPU from any inactive state to powered off.
1598  */
1599 int
1600 cpu_poweroff(cpu_t *cp)
1601 {
1602 	int	error = ENOTSUP;
1603 
1604 	ASSERT(MUTEX_HELD(&cpu_lock));
1605 	ASSERT(cpu_is_offline(cp));
1606 
1607 	if (!(cp->cpu_flags & CPU_QUIESCED))
1608 		return (EBUSY);		/* not completely idle */
1609 
1610 	error = mp_cpu_poweroff(cp);	/* arch-dep hook */
1611 	if (error == 0)
1612 		cpu_set_state(cp);
1613 
1614 	return (error);
1615 }
1616 
1617 /*
1618  * Initialize the CPU lists for the first CPU.
1619  */
1620 void
1621 cpu_list_init(cpu_t *cp)
1622 {
1623 	cp->cpu_next = cp;
1624 	cp->cpu_prev = cp;
1625 	cpu_list = cp;
1626 
1627 	cp->cpu_next_onln = cp;
1628 	cp->cpu_prev_onln = cp;
1629 	cpu_active = cp;
1630 
1631 	cp->cpu_seqid = 0;
1632 	CPUSET_ADD(cpu_seqid_inuse, 0);
1633 	cp->cpu_cache_offset = KMEM_CACHE_SIZE(cp->cpu_seqid);
1634 	cp_default.cp_cpulist = cp;
1635 	cp_default.cp_ncpus = 1;
1636 	cp->cpu_next_part = cp;
1637 	cp->cpu_prev_part = cp;
1638 	cp->cpu_part = &cp_default;
1639 
1640 	CPUSET_ADD(cpu_available, cp->cpu_id);
1641 }
1642 
1643 /*
1644  * Insert a CPU into the list of available CPUs.
1645  */
1646 void
1647 cpu_add_unit(cpu_t *cp)
1648 {
1649 	int seqid;
1650 
1651 	ASSERT(MUTEX_HELD(&cpu_lock));
1652 	ASSERT(cpu_list != NULL);	/* list started in cpu_list_init */
1653 
1654 	lgrp_config(LGRP_CONFIG_CPU_ADD, (uintptr_t)cp, 0);
1655 
1656 	/*
1657 	 * Note: most users of the cpu_list will grab the
1658 	 * cpu_lock to insure that it isn't modified.  However,
1659 	 * certain users can't or won't do that.  To allow this
1660 	 * we pause the other cpus.  Users who walk the list
1661 	 * without cpu_lock, must disable kernel preemption
1662 	 * to insure that the list isn't modified underneath
1663 	 * them.  Also, any cached pointers to cpu structures
1664 	 * must be revalidated by checking to see if the
1665 	 * cpu_next pointer points to itself.  This check must
1666 	 * be done with the cpu_lock held or kernel preemption
1667 	 * disabled.  This check relies upon the fact that
1668 	 * old cpu structures are not free'ed or cleared after
1669 	 * then are removed from the cpu_list.
1670 	 *
1671 	 * Note that the clock code walks the cpu list dereferencing
1672 	 * the cpu_part pointer, so we need to initialize it before
1673 	 * adding the cpu to the list.
1674 	 */
1675 	cp->cpu_part = &cp_default;
1676 	(void) pause_cpus(NULL);
1677 	cp->cpu_next = cpu_list;
1678 	cp->cpu_prev = cpu_list->cpu_prev;
1679 	cpu_list->cpu_prev->cpu_next = cp;
1680 	cpu_list->cpu_prev = cp;
1681 	start_cpus();
1682 
1683 	for (seqid = 0; CPU_IN_SET(cpu_seqid_inuse, seqid); seqid++)
1684 		continue;
1685 	CPUSET_ADD(cpu_seqid_inuse, seqid);
1686 	cp->cpu_seqid = seqid;
1687 	ASSERT(ncpus < max_ncpus);
1688 	ncpus++;
1689 	cp->cpu_cache_offset = KMEM_CACHE_SIZE(cp->cpu_seqid);
1690 	cpu[cp->cpu_id] = cp;
1691 	CPUSET_ADD(cpu_available, cp->cpu_id);
1692 
1693 	/*
1694 	 * allocate a pause thread for this CPU.
1695 	 */
1696 	cpu_pause_alloc(cp);
1697 
1698 	/*
1699 	 * So that new CPUs won't have NULL prev_onln and next_onln pointers,
1700 	 * link them into a list of just that CPU.
1701 	 * This is so that disp_lowpri_cpu will work for thread_create in
1702 	 * pause_cpus() when called from the startup thread in a new CPU.
1703 	 */
1704 	cp->cpu_next_onln = cp;
1705 	cp->cpu_prev_onln = cp;
1706 	cpu_info_kstat_create(cp);
1707 	cp->cpu_next_part = cp;
1708 	cp->cpu_prev_part = cp;
1709 
1710 	init_cpu_mstate(cp, CMS_SYSTEM);
1711 
1712 	pool_pset_mod = gethrtime();
1713 }
1714 
1715 /*
1716  * Do the opposite of cpu_add_unit().
1717  */
1718 void
1719 cpu_del_unit(int cpuid)
1720 {
1721 	struct cpu	*cp, *cpnext;
1722 
1723 	ASSERT(MUTEX_HELD(&cpu_lock));
1724 	cp = cpu[cpuid];
1725 	ASSERT(cp != NULL);
1726 
1727 	ASSERT(cp->cpu_next_onln == cp);
1728 	ASSERT(cp->cpu_prev_onln == cp);
1729 	ASSERT(cp->cpu_next_part == cp);
1730 	ASSERT(cp->cpu_prev_part == cp);
1731 
1732 	chip_cpu_fini(cp);
1733 
1734 	/*
1735 	 * Destroy kstat stuff.
1736 	 */
1737 	cpu_info_kstat_destroy(cp);
1738 	term_cpu_mstate(cp);
1739 	/*
1740 	 * Free up pause thread.
1741 	 */
1742 	cpu_pause_free(cp);
1743 	CPUSET_DEL(cpu_available, cp->cpu_id);
1744 	cpu[cp->cpu_id] = NULL;
1745 	/*
1746 	 * The clock thread and mutex_vector_enter cannot hold the
1747 	 * cpu_lock while traversing the cpu list, therefore we pause
1748 	 * all other threads by pausing the other cpus. These, and any
1749 	 * other routines holding cpu pointers while possibly sleeping
1750 	 * must be sure to call kpreempt_disable before processing the
1751 	 * list and be sure to check that the cpu has not been deleted
1752 	 * after any sleeps (check cp->cpu_next != NULL). We guarantee
1753 	 * to keep the deleted cpu structure around.
1754 	 *
1755 	 * Note that this MUST be done AFTER cpu_available
1756 	 * has been updated so that we don't waste time
1757 	 * trying to pause the cpu we're trying to delete.
1758 	 */
1759 	(void) pause_cpus(NULL);
1760 
1761 	cpnext = cp->cpu_next;
1762 	cp->cpu_prev->cpu_next = cp->cpu_next;
1763 	cp->cpu_next->cpu_prev = cp->cpu_prev;
1764 	if (cp == cpu_list)
1765 	    cpu_list = cpnext;
1766 
1767 	/*
1768 	 * Signals that the cpu has been deleted (see above).
1769 	 */
1770 	cp->cpu_next = NULL;
1771 	cp->cpu_prev = NULL;
1772 
1773 	start_cpus();
1774 
1775 	CPUSET_DEL(cpu_seqid_inuse, cp->cpu_seqid);
1776 	ncpus--;
1777 	lgrp_config(LGRP_CONFIG_CPU_DEL, (uintptr_t)cp, 0);
1778 
1779 	pool_pset_mod = gethrtime();
1780 }
1781 
1782 /*
1783  * Add a CPU to the list of active CPUs.
1784  *	This routine must not get any locks, because other CPUs are paused.
1785  */
1786 static void
1787 cpu_add_active_internal(cpu_t *cp)
1788 {
1789 	cpupart_t	*pp = cp->cpu_part;
1790 
1791 	ASSERT(MUTEX_HELD(&cpu_lock));
1792 	ASSERT(cpu_list != NULL);	/* list started in cpu_list_init */
1793 
1794 	ncpus_online++;
1795 	cpu_set_state(cp);
1796 	cp->cpu_next_onln = cpu_active;
1797 	cp->cpu_prev_onln = cpu_active->cpu_prev_onln;
1798 	cpu_active->cpu_prev_onln->cpu_next_onln = cp;
1799 	cpu_active->cpu_prev_onln = cp;
1800 
1801 	if (pp->cp_cpulist) {
1802 		cp->cpu_next_part = pp->cp_cpulist;
1803 		cp->cpu_prev_part = pp->cp_cpulist->cpu_prev_part;
1804 		pp->cp_cpulist->cpu_prev_part->cpu_next_part = cp;
1805 		pp->cp_cpulist->cpu_prev_part = cp;
1806 	} else {
1807 		ASSERT(pp->cp_ncpus == 0);
1808 		pp->cp_cpulist = cp->cpu_next_part = cp->cpu_prev_part = cp;
1809 	}
1810 	pp->cp_ncpus++;
1811 	if (pp->cp_ncpus == 1) {
1812 		cp_numparts_nonempty++;
1813 		ASSERT(cp_numparts_nonempty != 0);
1814 	}
1815 
1816 	chip_cpu_assign(cp);
1817 
1818 	lgrp_config(LGRP_CONFIG_CPU_ONLINE, (uintptr_t)cp, 0);
1819 
1820 	bzero(&cp->cpu_loadavg, sizeof (cp->cpu_loadavg));
1821 }
1822 
1823 /*
1824  * Add a CPU to the list of active CPUs.
1825  *	This is called from machine-dependent layers when a new CPU is started.
1826  */
1827 void
1828 cpu_add_active(cpu_t *cp)
1829 {
1830 	pause_cpus(NULL);
1831 	cpu_add_active_internal(cp);
1832 	start_cpus();
1833 	cpu_stats_kstat_create(cp);
1834 	cpu_create_intrstat(cp);
1835 	lgrp_kstat_create(cp);
1836 	cpu_state_change_notify(cp->cpu_id, CPU_INIT);
1837 }
1838 
1839 
1840 /*
1841  * Remove a CPU from the list of active CPUs.
1842  *	This routine must not get any locks, because other CPUs are paused.
1843  */
1844 /* ARGSUSED */
1845 static void
1846 cpu_remove_active(cpu_t *cp)
1847 {
1848 	cpupart_t	*pp = cp->cpu_part;
1849 
1850 	ASSERT(MUTEX_HELD(&cpu_lock));
1851 	ASSERT(cp->cpu_next_onln != cp);	/* not the last one */
1852 	ASSERT(cp->cpu_prev_onln != cp);	/* not the last one */
1853 
1854 	chip_cpu_unassign(cp);
1855 
1856 	lgrp_config(LGRP_CONFIG_CPU_OFFLINE, (uintptr_t)cp, 0);
1857 
1858 	cp->cpu_prev_onln->cpu_next_onln = cp->cpu_next_onln;
1859 	cp->cpu_next_onln->cpu_prev_onln = cp->cpu_prev_onln;
1860 	if (cpu_active == cp) {
1861 		cpu_active = cp->cpu_next_onln;
1862 	}
1863 	cp->cpu_next_onln = cp;
1864 	cp->cpu_prev_onln = cp;
1865 
1866 	cp->cpu_prev_part->cpu_next_part = cp->cpu_next_part;
1867 	cp->cpu_next_part->cpu_prev_part = cp->cpu_prev_part;
1868 	if (pp->cp_cpulist == cp) {
1869 		pp->cp_cpulist = cp->cpu_next_part;
1870 		ASSERT(pp->cp_cpulist != cp);
1871 	}
1872 	cp->cpu_next_part = cp;
1873 	cp->cpu_prev_part = cp;
1874 	pp->cp_ncpus--;
1875 	if (pp->cp_ncpus == 0) {
1876 		cp_numparts_nonempty--;
1877 		ASSERT(cp_numparts_nonempty != 0);
1878 	}
1879 }
1880 
1881 /*
1882  * Routine used to setup a newly inserted CPU in preparation for starting
1883  * it running code.
1884  */
1885 int
1886 cpu_configure(int cpuid)
1887 {
1888 	int retval = 0;
1889 
1890 	ASSERT(MUTEX_HELD(&cpu_lock));
1891 
1892 	/*
1893 	 * Some structures are statically allocated based upon
1894 	 * the maximum number of cpus the system supports.  Do not
1895 	 * try to add anything beyond this limit.
1896 	 */
1897 	if (cpuid < 0 || cpuid >= NCPU) {
1898 		return (EINVAL);
1899 	}
1900 
1901 	if ((cpu[cpuid] != NULL) && (cpu[cpuid]->cpu_flags != 0)) {
1902 		return (EALREADY);
1903 	}
1904 
1905 	if ((retval = mp_cpu_configure(cpuid)) != 0) {
1906 		return (retval);
1907 	}
1908 
1909 	cpu[cpuid]->cpu_flags = CPU_QUIESCED | CPU_OFFLINE | CPU_POWEROFF;
1910 	cpu_set_state(cpu[cpuid]);
1911 	retval = cpu_state_change_hooks(cpuid, CPU_CONFIG, CPU_UNCONFIG);
1912 	if (retval != 0)
1913 		(void) mp_cpu_unconfigure(cpuid);
1914 
1915 	return (retval);
1916 }
1917 
1918 /*
1919  * Routine used to cleanup a CPU that has been powered off.  This will
1920  * destroy all per-cpu information related to this cpu.
1921  */
1922 int
1923 cpu_unconfigure(int cpuid)
1924 {
1925 	int error;
1926 
1927 	ASSERT(MUTEX_HELD(&cpu_lock));
1928 
1929 	if (cpu[cpuid] == NULL) {
1930 		return (ENODEV);
1931 	}
1932 
1933 	if (cpu[cpuid]->cpu_flags == 0) {
1934 		return (EALREADY);
1935 	}
1936 
1937 	if ((cpu[cpuid]->cpu_flags & CPU_POWEROFF) == 0) {
1938 		return (EBUSY);
1939 	}
1940 
1941 	if (cpu[cpuid]->cpu_props != NULL) {
1942 		(void) nvlist_free(cpu[cpuid]->cpu_props);
1943 		cpu[cpuid]->cpu_props = NULL;
1944 	}
1945 
1946 	error = cpu_state_change_hooks(cpuid, CPU_UNCONFIG, CPU_CONFIG);
1947 
1948 	if (error != 0)
1949 		return (error);
1950 
1951 	return (mp_cpu_unconfigure(cpuid));
1952 }
1953 
1954 /*
1955  * Routines for registering and de-registering cpu_setup callback functions.
1956  *
1957  * Caller's context
1958  *	These routines must not be called from a driver's attach(9E) or
1959  *	detach(9E) entry point.
1960  *
1961  * NOTE: CPU callbacks should not block. They are called with cpu_lock held.
1962  */
1963 
1964 /*
1965  * Ideally, these would be dynamically allocated and put into a linked
1966  * list; however that is not feasible because the registration routine
1967  * has to be available before the kmem allocator is working (in fact,
1968  * it is called by the kmem allocator init code).  In any case, there
1969  * are quite a few extra entries for future users.
1970  */
1971 #define	NCPU_SETUPS	20
1972 
1973 struct cpu_setup {
1974 	cpu_setup_func_t *func;
1975 	void *arg;
1976 } cpu_setups[NCPU_SETUPS];
1977 
1978 void
1979 register_cpu_setup_func(cpu_setup_func_t *func, void *arg)
1980 {
1981 	int i;
1982 
1983 	ASSERT(MUTEX_HELD(&cpu_lock));
1984 
1985 	for (i = 0; i < NCPU_SETUPS; i++)
1986 		if (cpu_setups[i].func == NULL)
1987 		    break;
1988 	if (i >= NCPU_SETUPS)
1989 		cmn_err(CE_PANIC, "Ran out of cpu_setup callback entries");
1990 
1991 	cpu_setups[i].func = func;
1992 	cpu_setups[i].arg = arg;
1993 }
1994 
1995 void
1996 unregister_cpu_setup_func(cpu_setup_func_t *func, void *arg)
1997 {
1998 	int i;
1999 
2000 	ASSERT(MUTEX_HELD(&cpu_lock));
2001 
2002 	for (i = 0; i < NCPU_SETUPS; i++)
2003 		if ((cpu_setups[i].func == func) &&
2004 		    (cpu_setups[i].arg == arg))
2005 		    break;
2006 	if (i >= NCPU_SETUPS)
2007 		cmn_err(CE_PANIC, "Could not find cpu_setup callback to "
2008 		    "deregister");
2009 
2010 	cpu_setups[i].func = NULL;
2011 	cpu_setups[i].arg = 0;
2012 }
2013 
2014 /*
2015  * Call any state change hooks for this CPU, ignore any errors.
2016  */
2017 void
2018 cpu_state_change_notify(int id, cpu_setup_t what)
2019 {
2020 	int i;
2021 
2022 	ASSERT(MUTEX_HELD(&cpu_lock));
2023 
2024 	for (i = 0; i < NCPU_SETUPS; i++) {
2025 		if (cpu_setups[i].func != NULL) {
2026 			cpu_setups[i].func(what, id, cpu_setups[i].arg);
2027 		}
2028 	}
2029 }
2030 
2031 /*
2032  * Call any state change hooks for this CPU, undo it if error found.
2033  */
2034 static int
2035 cpu_state_change_hooks(int id, cpu_setup_t what, cpu_setup_t undo)
2036 {
2037 	int i;
2038 	int retval = 0;
2039 
2040 	ASSERT(MUTEX_HELD(&cpu_lock));
2041 
2042 	for (i = 0; i < NCPU_SETUPS; i++) {
2043 		if (cpu_setups[i].func != NULL) {
2044 			retval = cpu_setups[i].func(what, id,
2045 			    cpu_setups[i].arg);
2046 			if (retval) {
2047 				for (i--; i >= 0; i--) {
2048 					if (cpu_setups[i].func != NULL)
2049 						cpu_setups[i].func(undo,
2050 						    id, cpu_setups[i].arg);
2051 				}
2052 				break;
2053 			}
2054 		}
2055 	}
2056 	return (retval);
2057 }
2058 
2059 /*
2060  * Export information about this CPU via the kstat mechanism.
2061  */
2062 static struct {
2063 	kstat_named_t ci_state;
2064 	kstat_named_t ci_state_begin;
2065 	kstat_named_t ci_cpu_type;
2066 	kstat_named_t ci_fpu_type;
2067 	kstat_named_t ci_clock_MHz;
2068 	kstat_named_t ci_chip_id;
2069 	kstat_named_t ci_implementation;
2070 #ifdef __sparcv9
2071 	kstat_named_t ci_device_ID;
2072 	kstat_named_t ci_cpu_fru;
2073 #endif
2074 	kstat_named_t ci_brandstr;
2075 	kstat_named_t ci_core_id;
2076 } cpu_info_template = {
2077 	{ "state",		KSTAT_DATA_CHAR },
2078 	{ "state_begin",	KSTAT_DATA_LONG },
2079 	{ "cpu_type",		KSTAT_DATA_CHAR },
2080 	{ "fpu_type",		KSTAT_DATA_CHAR },
2081 	{ "clock_MHz",		KSTAT_DATA_LONG },
2082 	{ "chip_id",		KSTAT_DATA_LONG },
2083 	{ "implementation",	KSTAT_DATA_STRING },
2084 #ifdef __sparcv9
2085 	{ "device_ID",		KSTAT_DATA_UINT64 },
2086 	{ "cpu_fru",		KSTAT_DATA_STRING },
2087 #endif
2088 	{ "brand",		KSTAT_DATA_STRING },
2089 	{ "core_id",		KSTAT_DATA_LONG },
2090 };
2091 
2092 static kmutex_t cpu_info_template_lock;
2093 
2094 static int
2095 cpu_info_kstat_update(kstat_t *ksp, int rw)
2096 {
2097 	cpu_t	*cp = ksp->ks_private;
2098 	const char *pi_state;
2099 
2100 	if (rw == KSTAT_WRITE)
2101 		return (EACCES);
2102 
2103 	switch (cp->cpu_type_info.pi_state) {
2104 	case P_ONLINE:
2105 		pi_state = PS_ONLINE;
2106 		break;
2107 	case P_POWEROFF:
2108 		pi_state = PS_POWEROFF;
2109 		break;
2110 	case P_NOINTR:
2111 		pi_state = PS_NOINTR;
2112 		break;
2113 	case P_FAULTED:
2114 		pi_state = PS_FAULTED;
2115 		break;
2116 	case P_SPARE:
2117 		pi_state = PS_SPARE;
2118 		break;
2119 	case P_OFFLINE:
2120 		pi_state = PS_OFFLINE;
2121 		break;
2122 	default:
2123 		pi_state = "unknown";
2124 	}
2125 	(void) strcpy(cpu_info_template.ci_state.value.c, pi_state);
2126 	cpu_info_template.ci_state_begin.value.l = cp->cpu_state_begin;
2127 	(void) strncpy(cpu_info_template.ci_cpu_type.value.c,
2128 	    cp->cpu_type_info.pi_processor_type, 15);
2129 	(void) strncpy(cpu_info_template.ci_fpu_type.value.c,
2130 	    cp->cpu_type_info.pi_fputypes, 15);
2131 	cpu_info_template.ci_clock_MHz.value.l = cp->cpu_type_info.pi_clock;
2132 	cpu_info_template.ci_chip_id.value.l = chip_plat_get_chipid(cp);
2133 	kstat_named_setstr(&cpu_info_template.ci_implementation,
2134 	    cp->cpu_idstr);
2135 #ifdef __sparcv9
2136 	cpu_info_template.ci_device_ID.value.ui64 =
2137 	    cpunodes[cp->cpu_id].device_id;
2138 	kstat_named_setstr(&cpu_info_template.ci_cpu_fru, cpu_fru_fmri(cp));
2139 #endif
2140 	kstat_named_setstr(&cpu_info_template.ci_brandstr, cp->cpu_brandstr);
2141 	cpu_info_template.ci_core_id.value.l = chip_plat_get_coreid(cp);
2142 	return (0);
2143 }
2144 
2145 static void
2146 cpu_info_kstat_create(cpu_t *cp)
2147 {
2148 	zoneid_t zoneid;
2149 
2150 	ASSERT(MUTEX_HELD(&cpu_lock));
2151 
2152 	if (pool_pset_enabled())
2153 		zoneid = GLOBAL_ZONEID;
2154 	else
2155 		zoneid = ALL_ZONES;
2156 	if ((cp->cpu_info_kstat = kstat_create_zone("cpu_info", cp->cpu_id,
2157 	    NULL, "misc", KSTAT_TYPE_NAMED,
2158 	    sizeof (cpu_info_template) / sizeof (kstat_named_t),
2159 	    KSTAT_FLAG_VIRTUAL, zoneid)) != NULL) {
2160 		cp->cpu_info_kstat->ks_data_size += 2 * CPU_IDSTRLEN;
2161 #ifdef __sparcv9
2162 		cp->cpu_info_kstat->ks_data_size +=
2163 		    strlen(cpu_fru_fmri(cp)) + 1;
2164 #endif
2165 		cp->cpu_info_kstat->ks_lock = &cpu_info_template_lock;
2166 		cp->cpu_info_kstat->ks_data = &cpu_info_template;
2167 		cp->cpu_info_kstat->ks_private = cp;
2168 		cp->cpu_info_kstat->ks_update = cpu_info_kstat_update;
2169 		kstat_install(cp->cpu_info_kstat);
2170 	}
2171 }
2172 
2173 static void
2174 cpu_info_kstat_destroy(cpu_t *cp)
2175 {
2176 	ASSERT(MUTEX_HELD(&cpu_lock));
2177 
2178 	kstat_delete(cp->cpu_info_kstat);
2179 	cp->cpu_info_kstat = NULL;
2180 }
2181 
2182 /*
2183  * Create and install kstats for the boot CPU.
2184  */
2185 void
2186 cpu_kstat_init(cpu_t *cp)
2187 {
2188 	mutex_enter(&cpu_lock);
2189 	cpu_info_kstat_create(cp);
2190 	cpu_stats_kstat_create(cp);
2191 	cpu_create_intrstat(cp);
2192 	chip_kstat_create(cp->cpu_chip);
2193 	cpu_set_state(cp);
2194 	mutex_exit(&cpu_lock);
2195 }
2196 
2197 /*
2198  * Make visible to the zone that subset of the cpu information that would be
2199  * initialized when a cpu is configured (but still offline).
2200  */
2201 void
2202 cpu_visibility_configure(cpu_t *cp, zone_t *zone)
2203 {
2204 	zoneid_t zoneid = zone ? zone->zone_id : ALL_ZONES;
2205 
2206 	ASSERT(MUTEX_HELD(&cpu_lock));
2207 	ASSERT(pool_pset_enabled());
2208 	ASSERT(cp != NULL);
2209 
2210 	if (zoneid != ALL_ZONES && zoneid != GLOBAL_ZONEID) {
2211 		zone->zone_ncpus++;
2212 		ASSERT(zone->zone_ncpus <= ncpus);
2213 	}
2214 	if (cp->cpu_info_kstat != NULL)
2215 		kstat_zone_add(cp->cpu_info_kstat, zoneid);
2216 }
2217 
2218 /*
2219  * Make visible to the zone that subset of the cpu information that would be
2220  * initialized when a previously configured cpu is onlined.
2221  */
2222 void
2223 cpu_visibility_online(cpu_t *cp, zone_t *zone)
2224 {
2225 	kstat_t *ksp;
2226 	char name[sizeof ("cpu_stat") + 10];	/* enough for 32-bit cpuids */
2227 	zoneid_t zoneid = zone ? zone->zone_id : ALL_ZONES;
2228 	processorid_t cpun;
2229 
2230 	ASSERT(MUTEX_HELD(&cpu_lock));
2231 	ASSERT(pool_pset_enabled());
2232 	ASSERT(cp != NULL);
2233 	ASSERT(cpu_is_active(cp));
2234 
2235 	cpun = cp->cpu_id;
2236 	if (zoneid != ALL_ZONES && zoneid != GLOBAL_ZONEID) {
2237 		zone->zone_ncpus_online++;
2238 		ASSERT(zone->zone_ncpus_online <= ncpus_online);
2239 	}
2240 	(void) snprintf(name, sizeof (name), "cpu_stat%d", cpun);
2241 	if ((ksp = kstat_hold_byname("cpu_stat", cpun, name, ALL_ZONES))
2242 	    != NULL) {
2243 		kstat_zone_add(ksp, zoneid);
2244 		kstat_rele(ksp);
2245 	}
2246 	if ((ksp = kstat_hold_byname("cpu", cpun, "sys", ALL_ZONES)) != NULL) {
2247 		kstat_zone_add(ksp, zoneid);
2248 		kstat_rele(ksp);
2249 	}
2250 	if ((ksp = kstat_hold_byname("cpu", cpun, "vm", ALL_ZONES)) != NULL) {
2251 		kstat_zone_add(ksp, zoneid);
2252 		kstat_rele(ksp);
2253 	}
2254 	if ((ksp = kstat_hold_byname("cpu", cpun, "intrstat", ALL_ZONES)) !=
2255 	    NULL) {
2256 		kstat_zone_add(ksp, zoneid);
2257 		kstat_rele(ksp);
2258 	}
2259 }
2260 
2261 /*
2262  * Update relevant kstats such that cpu is now visible to processes
2263  * executing in specified zone.
2264  */
2265 void
2266 cpu_visibility_add(cpu_t *cp, zone_t *zone)
2267 {
2268 	cpu_visibility_configure(cp, zone);
2269 	if (cpu_is_active(cp))
2270 		cpu_visibility_online(cp, zone);
2271 }
2272 
2273 /*
2274  * Make invisible to the zone that subset of the cpu information that would be
2275  * torn down when a previously offlined cpu is unconfigured.
2276  */
2277 void
2278 cpu_visibility_unconfigure(cpu_t *cp, zone_t *zone)
2279 {
2280 	zoneid_t zoneid = zone ? zone->zone_id : ALL_ZONES;
2281 
2282 	ASSERT(MUTEX_HELD(&cpu_lock));
2283 	ASSERT(pool_pset_enabled());
2284 	ASSERT(cp != NULL);
2285 
2286 	if (zoneid != ALL_ZONES && zoneid != GLOBAL_ZONEID) {
2287 		ASSERT(zone->zone_ncpus != 0);
2288 		zone->zone_ncpus--;
2289 	}
2290 	if (cp->cpu_info_kstat)
2291 		kstat_zone_remove(cp->cpu_info_kstat, zoneid);
2292 }
2293 
2294 /*
2295  * Make invisible to the zone that subset of the cpu information that would be
2296  * torn down when a cpu is offlined (but still configured).
2297  */
2298 void
2299 cpu_visibility_offline(cpu_t *cp, zone_t *zone)
2300 {
2301 	kstat_t *ksp;
2302 	char name[sizeof ("cpu_stat") + 10];	/* enough for 32-bit cpuids */
2303 	zoneid_t zoneid = zone ? zone->zone_id : ALL_ZONES;
2304 	processorid_t cpun;
2305 
2306 	ASSERT(MUTEX_HELD(&cpu_lock));
2307 	ASSERT(pool_pset_enabled());
2308 	ASSERT(cp != NULL);
2309 	ASSERT(cpu_is_active(cp));
2310 
2311 	cpun = cp->cpu_id;
2312 	if (zoneid != ALL_ZONES && zoneid != GLOBAL_ZONEID) {
2313 		ASSERT(zone->zone_ncpus_online != 0);
2314 		zone->zone_ncpus_online--;
2315 	}
2316 
2317 	if ((ksp = kstat_hold_byname("cpu", cpun, "intrstat", ALL_ZONES)) !=
2318 	    NULL) {
2319 		kstat_zone_remove(ksp, zoneid);
2320 		kstat_rele(ksp);
2321 	}
2322 	if ((ksp = kstat_hold_byname("cpu", cpun, "vm", ALL_ZONES)) != NULL) {
2323 		kstat_zone_remove(ksp, zoneid);
2324 		kstat_rele(ksp);
2325 	}
2326 	if ((ksp = kstat_hold_byname("cpu", cpun, "sys", ALL_ZONES)) != NULL) {
2327 		kstat_zone_remove(ksp, zoneid);
2328 		kstat_rele(ksp);
2329 	}
2330 	(void) snprintf(name, sizeof (name), "cpu_stat%d", cpun);
2331 	if ((ksp = kstat_hold_byname("cpu_stat", cpun, name, ALL_ZONES))
2332 	    != NULL) {
2333 		kstat_zone_remove(ksp, zoneid);
2334 		kstat_rele(ksp);
2335 	}
2336 }
2337 
2338 /*
2339  * Update relevant kstats such that cpu is no longer visible to processes
2340  * executing in specified zone.
2341  */
2342 void
2343 cpu_visibility_remove(cpu_t *cp, zone_t *zone)
2344 {
2345 	if (cpu_is_active(cp))
2346 		cpu_visibility_offline(cp, zone);
2347 	cpu_visibility_unconfigure(cp, zone);
2348 }
2349 
2350 /*
2351  * Bind a thread to a CPU as requested.
2352  */
2353 int
2354 cpu_bind_thread(kthread_id_t tp, processorid_t bind, processorid_t *obind,
2355     int *error)
2356 {
2357 	processorid_t	binding;
2358 	cpu_t		*cp;
2359 
2360 	ASSERT(MUTEX_HELD(&cpu_lock));
2361 	ASSERT(MUTEX_HELD(&ttoproc(tp)->p_lock));
2362 
2363 	thread_lock(tp);
2364 
2365 	/*
2366 	 * Record old binding, but change the obind, which was initialized
2367 	 * to PBIND_NONE, only if this thread has a binding.  This avoids
2368 	 * reporting PBIND_NONE for a process when some LWPs are bound.
2369 	 */
2370 	binding = tp->t_bind_cpu;
2371 	if (binding != PBIND_NONE)
2372 		*obind = binding;	/* record old binding */
2373 
2374 	if (bind == PBIND_QUERY) {
2375 		thread_unlock(tp);
2376 		return (0);
2377 	}
2378 
2379 	/*
2380 	 * If this thread/LWP cannot be bound because of permission
2381 	 * problems, just note that and return success so that the
2382 	 * other threads/LWPs will be bound.  This is the way
2383 	 * processor_bind() is defined to work.
2384 	 *
2385 	 * Binding will get EPERM if the thread is of system class
2386 	 * or hasprocperm() fails.
2387 	 */
2388 	if (tp->t_cid == 0 || !hasprocperm(tp->t_cred, CRED())) {
2389 		*error = EPERM;
2390 		thread_unlock(tp);
2391 		return (0);
2392 	}
2393 
2394 	binding = bind;
2395 	if (binding != PBIND_NONE) {
2396 		cp = cpu[binding];
2397 		/*
2398 		 * Make sure binding is in right partition.
2399 		 */
2400 		if (tp->t_cpupart != cp->cpu_part) {
2401 			*error = EINVAL;
2402 			thread_unlock(tp);
2403 			return (0);
2404 		}
2405 	}
2406 	tp->t_bind_cpu = binding;	/* set new binding */
2407 
2408 	/*
2409 	 * If there is no system-set reason for affinity, set
2410 	 * the t_bound_cpu field to reflect the binding.
2411 	 */
2412 	if (tp->t_affinitycnt == 0) {
2413 		if (binding == PBIND_NONE) {
2414 			/*
2415 			 * We may need to adjust disp_max_unbound_pri
2416 			 * since we're becoming unbound.
2417 			 */
2418 			disp_adjust_unbound_pri(tp);
2419 
2420 			tp->t_bound_cpu = NULL;	/* set new binding */
2421 
2422 			/*
2423 			 * Move thread to lgroup with strongest affinity
2424 			 * after unbinding
2425 			 */
2426 			if (tp->t_lgrp_affinity)
2427 				lgrp_move_thread(tp,
2428 				    lgrp_choose(tp, tp->t_cpupart), 1);
2429 
2430 			if (tp->t_state == TS_ONPROC &&
2431 			    tp->t_cpu->cpu_part != tp->t_cpupart)
2432 				cpu_surrender(tp);
2433 		} else {
2434 			lpl_t	*lpl;
2435 
2436 			tp->t_bound_cpu = cp;
2437 			ASSERT(cp->cpu_lpl != NULL);
2438 
2439 			/*
2440 			 * Set home to lgroup with most affinity containing CPU
2441 			 * that thread is being bound or minimum bounding
2442 			 * lgroup if no affinities set
2443 			 */
2444 			if (tp->t_lgrp_affinity)
2445 				lpl = lgrp_affinity_best(tp, tp->t_cpupart, 0);
2446 			else
2447 				lpl = cp->cpu_lpl;
2448 
2449 			if (tp->t_lpl != lpl) {
2450 				/* can't grab cpu_lock */
2451 				lgrp_move_thread(tp, lpl, 1);
2452 			}
2453 
2454 			/*
2455 			 * Make the thread switch to the bound CPU.
2456 			 * If the thread is runnable, we need to
2457 			 * requeue it even if t_cpu is already set
2458 			 * to the right CPU, since it may be on a
2459 			 * kpreempt queue and need to move to a local
2460 			 * queue.  We could check t_disp_queue to
2461 			 * avoid unnecessary overhead if it's already
2462 			 * on the right queue, but since this isn't
2463 			 * a performance-critical operation it doesn't
2464 			 * seem worth the extra code and complexity.
2465 			 *
2466 			 * If the thread is weakbound to the cpu then it will
2467 			 * resist the new binding request until the weak
2468 			 * binding drops.  The cpu_surrender or requeueing
2469 			 * below could be skipped in such cases (since it
2470 			 * will have no effect), but that would require
2471 			 * thread_allowmigrate to acquire thread_lock so
2472 			 * we'll take the very occasional hit here instead.
2473 			 */
2474 			if (tp->t_state == TS_ONPROC) {
2475 				cpu_surrender(tp);
2476 			} else if (tp->t_state == TS_RUN) {
2477 				cpu_t *ocp = tp->t_cpu;
2478 
2479 				(void) dispdeq(tp);
2480 				setbackdq(tp);
2481 				/*
2482 				 * Either on the bound CPU's disp queue now,
2483 				 * or swapped out or on the swap queue.
2484 				 */
2485 				ASSERT(tp->t_disp_queue == cp->cpu_disp ||
2486 				    tp->t_weakbound_cpu == ocp ||
2487 				    (tp->t_schedflag & (TS_LOAD | TS_ON_SWAPQ))
2488 				    != TS_LOAD);
2489 			}
2490 		}
2491 	}
2492 
2493 	/*
2494 	 * Our binding has changed; set TP_CHANGEBIND.
2495 	 */
2496 	tp->t_proc_flag |= TP_CHANGEBIND;
2497 	aston(tp);
2498 
2499 	thread_unlock(tp);
2500 
2501 	return (0);
2502 }
2503 
2504 #if CPUSET_WORDS > 1
2505 
2506 /*
2507  * Functions for implementing cpuset operations when a cpuset is more
2508  * than one word.  On platforms where a cpuset is a single word these
2509  * are implemented as macros in cpuvar.h.
2510  */
2511 
2512 void
2513 cpuset_all(cpuset_t *s)
2514 {
2515 	int i;
2516 
2517 	for (i = 0; i < CPUSET_WORDS; i++)
2518 		s->cpub[i] = ~0UL;
2519 }
2520 
2521 void
2522 cpuset_all_but(cpuset_t *s, uint_t cpu)
2523 {
2524 	cpuset_all(s);
2525 	CPUSET_DEL(*s, cpu);
2526 }
2527 
2528 void
2529 cpuset_only(cpuset_t *s, uint_t cpu)
2530 {
2531 	CPUSET_ZERO(*s);
2532 	CPUSET_ADD(*s, cpu);
2533 }
2534 
2535 int
2536 cpuset_isnull(cpuset_t *s)
2537 {
2538 	int i;
2539 
2540 	for (i = 0; i < CPUSET_WORDS; i++)
2541 		if (s->cpub[i] != 0)
2542 			return (0);
2543 	return (1);
2544 }
2545 
2546 int
2547 cpuset_cmp(cpuset_t *s1, cpuset_t *s2)
2548 {
2549 	int i;
2550 
2551 	for (i = 0; i < CPUSET_WORDS; i++)
2552 		if (s1->cpub[i] != s2->cpub[i])
2553 			return (0);
2554 	return (1);
2555 }
2556 
2557 uint_t
2558 cpuset_find(cpuset_t *s)
2559 {
2560 
2561 	uint_t	i;
2562 	uint_t	cpu = (uint_t)-1;
2563 
2564 	/*
2565 	 * Find a cpu in the cpuset
2566 	 */
2567 	for (i = 0; (i < CPUSET_WORDS && cpu == (uint_t)-1); i++)
2568 		cpu = (uint_t)(lowbit(s->cpub[i]) - 1);
2569 	return (cpu);
2570 }
2571 
2572 #endif	/* CPUSET_WORDS */
2573 
2574 /*
2575  * Unbind all user threads bound to a given CPU.
2576  */
2577 int
2578 cpu_unbind(processorid_t cpu)
2579 {
2580 	processorid_t obind;
2581 	kthread_t *tp;
2582 	int ret = 0;
2583 	proc_t *pp;
2584 	int err, berr = 0;
2585 
2586 	ASSERT(MUTEX_HELD(&cpu_lock));
2587 
2588 	mutex_enter(&pidlock);
2589 	for (pp = practive; pp != NULL; pp = pp->p_next) {
2590 		mutex_enter(&pp->p_lock);
2591 		tp = pp->p_tlist;
2592 		/*
2593 		 * Skip zombies, kernel processes, and processes in
2594 		 * other zones, if called from a non-global zone.
2595 		 */
2596 		if (tp == NULL || (pp->p_flag & SSYS) ||
2597 		    !HASZONEACCESS(curproc, pp->p_zone->zone_id)) {
2598 			mutex_exit(&pp->p_lock);
2599 			continue;
2600 		}
2601 		do {
2602 			if (tp->t_bind_cpu != cpu)
2603 				continue;
2604 			err = cpu_bind_thread(tp, PBIND_NONE, &obind, &berr);
2605 			if (ret == 0)
2606 				ret = err;
2607 		} while ((tp = tp->t_forw) != pp->p_tlist);
2608 		mutex_exit(&pp->p_lock);
2609 	}
2610 	mutex_exit(&pidlock);
2611 	if (ret == 0)
2612 		ret = berr;
2613 	return (ret);
2614 }
2615 
2616 
2617 /*
2618  * Destroy all remaining bound threads on a cpu.
2619  */
2620 void
2621 cpu_destroy_bound_threads(cpu_t *cp)
2622 {
2623 	extern id_t syscid;
2624 	register kthread_id_t	t, tlist, tnext;
2625 
2626 	/*
2627 	 * Destroy all remaining bound threads on the cpu.  This
2628 	 * should include both the interrupt threads and the idle thread.
2629 	 * This requires some care, since we need to traverse the
2630 	 * thread list with the pidlock mutex locked, but thread_free
2631 	 * also locks the pidlock mutex.  So, we collect the threads
2632 	 * we're going to reap in a list headed by "tlist", then we
2633 	 * unlock the pidlock mutex and traverse the tlist list,
2634 	 * doing thread_free's on the thread's.	 Simple, n'est pas?
2635 	 * Also, this depends on thread_free not mucking with the
2636 	 * t_next and t_prev links of the thread.
2637 	 */
2638 
2639 	if ((t = curthread) != NULL) {
2640 
2641 		tlist = NULL;
2642 		mutex_enter(&pidlock);
2643 		do {
2644 			tnext = t->t_next;
2645 			if (t->t_bound_cpu == cp) {
2646 
2647 				/*
2648 				 * We've found a bound thread, carefully unlink
2649 				 * it out of the thread list, and add it to
2650 				 * our "tlist".	 We "know" we don't have to
2651 				 * worry about unlinking curthread (the thread
2652 				 * that is executing this code).
2653 				 */
2654 				t->t_next->t_prev = t->t_prev;
2655 				t->t_prev->t_next = t->t_next;
2656 				t->t_next = tlist;
2657 				tlist = t;
2658 				ASSERT(t->t_cid == syscid);
2659 				/* wake up anyone blocked in thread_join */
2660 				cv_broadcast(&t->t_joincv);
2661 				/*
2662 				 * t_lwp set by interrupt threads and not
2663 				 * cleared.
2664 				 */
2665 				t->t_lwp = NULL;
2666 				/*
2667 				 * Pause and idle threads always have
2668 				 * t_state set to TS_ONPROC.
2669 				 */
2670 				t->t_state = TS_FREE;
2671 				t->t_prev = NULL;	/* Just in case */
2672 			}
2673 
2674 		} while ((t = tnext) != curthread);
2675 
2676 		mutex_exit(&pidlock);
2677 
2678 
2679 		for (t = tlist; t != NULL; t = tnext) {
2680 			tnext = t->t_next;
2681 			thread_free(t);
2682 		}
2683 	}
2684 }
2685 
2686 /*
2687  * processor_info(2) and p_online(2) status support functions
2688  *   The constants returned by the cpu_get_state() and cpu_get_state_str() are
2689  *   for use in communicating processor state information to userland.  Kernel
2690  *   subsystems should only be using the cpu_flags value directly.  Subsystems
2691  *   modifying cpu_flags should record the state change via a call to the
2692  *   cpu_set_state().
2693  */
2694 
2695 /*
2696  * Update the pi_state of this CPU.  This function provides the CPU status for
2697  * the information returned by processor_info(2).
2698  */
2699 void
2700 cpu_set_state(cpu_t *cpu)
2701 {
2702 	ASSERT(MUTEX_HELD(&cpu_lock));
2703 	cpu->cpu_type_info.pi_state = cpu_get_state(cpu);
2704 	cpu->cpu_state_begin = gethrestime_sec();
2705 	pool_cpu_mod = gethrtime();
2706 }
2707 
2708 /*
2709  * Return offline/online/other status for the indicated CPU.  Use only for
2710  * communication with user applications; cpu_flags provides the in-kernel
2711  * interface.
2712  */
2713 int
2714 cpu_get_state(cpu_t *cpu)
2715 {
2716 	ASSERT(MUTEX_HELD(&cpu_lock));
2717 	if (cpu->cpu_flags & CPU_POWEROFF)
2718 		return (P_POWEROFF);
2719 	else if (cpu->cpu_flags & CPU_FAULTED)
2720 		return (P_FAULTED);
2721 	else if (cpu->cpu_flags & CPU_SPARE)
2722 		return (P_SPARE);
2723 	else if ((cpu->cpu_flags & (CPU_READY | CPU_OFFLINE)) != CPU_READY)
2724 		return (P_OFFLINE);
2725 	else if (cpu->cpu_flags & CPU_ENABLE)
2726 		return (P_ONLINE);
2727 	else
2728 		return (P_NOINTR);
2729 }
2730 
2731 /*
2732  * Return processor_info(2) state as a string.
2733  */
2734 const char *
2735 cpu_get_state_str(cpu_t *cpu)
2736 {
2737 	const char *string;
2738 
2739 	switch (cpu_get_state(cpu)) {
2740 	case P_ONLINE:
2741 		string = PS_ONLINE;
2742 		break;
2743 	case P_POWEROFF:
2744 		string = PS_POWEROFF;
2745 		break;
2746 	case P_NOINTR:
2747 		string = PS_NOINTR;
2748 		break;
2749 	case P_SPARE:
2750 		string = PS_SPARE;
2751 		break;
2752 	case P_FAULTED:
2753 		string = PS_FAULTED;
2754 		break;
2755 	case P_OFFLINE:
2756 		string = PS_OFFLINE;
2757 		break;
2758 	default:
2759 		string = "unknown";
2760 		break;
2761 	}
2762 	return (string);
2763 }
2764 
2765 /*
2766  * Export this CPU's statistics (cpu_stat_t and cpu_stats_t) as raw and named
2767  * kstats, respectively.  This is done when a CPU is initialized or placed
2768  * online via p_online(2).
2769  */
2770 static void
2771 cpu_stats_kstat_create(cpu_t *cp)
2772 {
2773 	int 	instance = cp->cpu_id;
2774 	char 	*module = "cpu";
2775 	char 	*class = "misc";
2776 	kstat_t	*ksp;
2777 	zoneid_t zoneid;
2778 
2779 	ASSERT(MUTEX_HELD(&cpu_lock));
2780 
2781 	if (pool_pset_enabled())
2782 		zoneid = GLOBAL_ZONEID;
2783 	else
2784 		zoneid = ALL_ZONES;
2785 	/*
2786 	 * Create named kstats
2787 	 */
2788 #define	CPU_STATS_KS_CREATE(name, tsize, update_func)                    \
2789 	ksp = kstat_create_zone(module, instance, (name), class,         \
2790 	    KSTAT_TYPE_NAMED, (tsize) / sizeof (kstat_named_t), 0,       \
2791 	    zoneid);                                                     \
2792 	if (ksp != NULL) {                                               \
2793 		ksp->ks_private = cp;                                    \
2794 		ksp->ks_update = (update_func);                          \
2795 		kstat_install(ksp);                                      \
2796 	} else                                                           \
2797 		cmn_err(CE_WARN, "cpu: unable to create %s:%d:%s kstat", \
2798 		    module, instance, (name));
2799 
2800 	CPU_STATS_KS_CREATE("sys", sizeof (cpu_sys_stats_ks_data_template),
2801 	    cpu_sys_stats_ks_update);
2802 	CPU_STATS_KS_CREATE("vm", sizeof (cpu_vm_stats_ks_data_template),
2803 	    cpu_vm_stats_ks_update);
2804 
2805 	/*
2806 	 * Export the familiar cpu_stat_t KSTAT_TYPE_RAW kstat.
2807 	 */
2808 	ksp = kstat_create_zone("cpu_stat", cp->cpu_id, NULL,
2809 	    "misc", KSTAT_TYPE_RAW, sizeof (cpu_stat_t), 0, zoneid);
2810 	if (ksp != NULL) {
2811 		ksp->ks_update = cpu_stat_ks_update;
2812 		ksp->ks_private = cp;
2813 		kstat_install(ksp);
2814 	}
2815 }
2816 
2817 static void
2818 cpu_stats_kstat_destroy(cpu_t *cp)
2819 {
2820 	char ks_name[KSTAT_STRLEN];
2821 
2822 	(void) sprintf(ks_name, "cpu_stat%d", cp->cpu_id);
2823 	kstat_delete_byname("cpu_stat", cp->cpu_id, ks_name);
2824 
2825 	kstat_delete_byname("cpu", cp->cpu_id, "sys");
2826 	kstat_delete_byname("cpu", cp->cpu_id, "vm");
2827 }
2828 
2829 static int
2830 cpu_sys_stats_ks_update(kstat_t *ksp, int rw)
2831 {
2832 	cpu_t *cp = (cpu_t *)ksp->ks_private;
2833 	struct cpu_sys_stats_ks_data *csskd;
2834 	cpu_sys_stats_t *css;
2835 	hrtime_t msnsecs[NCMSTATES];
2836 	int	i;
2837 
2838 	if (rw == KSTAT_WRITE)
2839 		return (EACCES);
2840 
2841 	csskd = ksp->ks_data;
2842 	css = &cp->cpu_stats.sys;
2843 
2844 	/*
2845 	 * Read CPU mstate, but compare with the last values we
2846 	 * received to make sure that the returned kstats never
2847 	 * decrease.
2848 	 */
2849 
2850 	get_cpu_mstate(cp, msnsecs);
2851 	if (csskd->cpu_nsec_idle.value.ui64 > msnsecs[CMS_IDLE])
2852 		msnsecs[CMS_IDLE] = csskd->cpu_nsec_idle.value.ui64;
2853 	if (csskd->cpu_nsec_user.value.ui64 > msnsecs[CMS_USER])
2854 		msnsecs[CMS_USER] = csskd->cpu_nsec_user.value.ui64;
2855 	if (csskd->cpu_nsec_kernel.value.ui64 > msnsecs[CMS_SYSTEM])
2856 		msnsecs[CMS_SYSTEM] = csskd->cpu_nsec_kernel.value.ui64;
2857 
2858 	bcopy(&cpu_sys_stats_ks_data_template, ksp->ks_data,
2859 	    sizeof (cpu_sys_stats_ks_data_template));
2860 
2861 	csskd->cpu_ticks_wait.value.ui64 = 0;
2862 	csskd->wait_ticks_io.value.ui64 = 0;
2863 
2864 	csskd->cpu_nsec_idle.value.ui64 = msnsecs[CMS_IDLE];
2865 	csskd->cpu_nsec_user.value.ui64 = msnsecs[CMS_USER];
2866 	csskd->cpu_nsec_kernel.value.ui64 = msnsecs[CMS_SYSTEM];
2867 	csskd->cpu_ticks_idle.value.ui64 =
2868 	    NSEC_TO_TICK(csskd->cpu_nsec_idle.value.ui64);
2869 	csskd->cpu_ticks_user.value.ui64 =
2870 	    NSEC_TO_TICK(csskd->cpu_nsec_user.value.ui64);
2871 	csskd->cpu_ticks_kernel.value.ui64 =
2872 	    NSEC_TO_TICK(csskd->cpu_nsec_kernel.value.ui64);
2873 	csskd->bread.value.ui64 = css->bread;
2874 	csskd->bwrite.value.ui64 = css->bwrite;
2875 	csskd->lread.value.ui64 = css->lread;
2876 	csskd->lwrite.value.ui64 = css->lwrite;
2877 	csskd->phread.value.ui64 = css->phread;
2878 	csskd->phwrite.value.ui64 = css->phwrite;
2879 	csskd->pswitch.value.ui64 = css->pswitch;
2880 	csskd->trap.value.ui64 = css->trap;
2881 	csskd->intr.value.ui64 = 0;
2882 	for (i = 0; i < PIL_MAX; i++)
2883 		csskd->intr.value.ui64 += css->intr[i];
2884 	csskd->syscall.value.ui64 = css->syscall;
2885 	csskd->sysread.value.ui64 = css->sysread;
2886 	csskd->syswrite.value.ui64 = css->syswrite;
2887 	csskd->sysfork.value.ui64 = css->sysfork;
2888 	csskd->sysvfork.value.ui64 = css->sysvfork;
2889 	csskd->sysexec.value.ui64 = css->sysexec;
2890 	csskd->readch.value.ui64 = css->readch;
2891 	csskd->writech.value.ui64 = css->writech;
2892 	csskd->rcvint.value.ui64 = css->rcvint;
2893 	csskd->xmtint.value.ui64 = css->xmtint;
2894 	csskd->mdmint.value.ui64 = css->mdmint;
2895 	csskd->rawch.value.ui64 = css->rawch;
2896 	csskd->canch.value.ui64 = css->canch;
2897 	csskd->outch.value.ui64 = css->outch;
2898 	csskd->msg.value.ui64 = css->msg;
2899 	csskd->sema.value.ui64 = css->sema;
2900 	csskd->namei.value.ui64 = css->namei;
2901 	csskd->ufsiget.value.ui64 = css->ufsiget;
2902 	csskd->ufsdirblk.value.ui64 = css->ufsdirblk;
2903 	csskd->ufsipage.value.ui64 = css->ufsipage;
2904 	csskd->ufsinopage.value.ui64 = css->ufsinopage;
2905 	csskd->procovf.value.ui64 = css->procovf;
2906 	csskd->intrthread.value.ui64 = 0;
2907 	for (i = 0; i < LOCK_LEVEL; i++)
2908 		csskd->intrthread.value.ui64 += css->intr[i];
2909 	csskd->intrblk.value.ui64 = css->intrblk;
2910 	csskd->intrunpin.value.ui64 = css->intrunpin;
2911 	csskd->idlethread.value.ui64 = css->idlethread;
2912 	csskd->inv_swtch.value.ui64 = css->inv_swtch;
2913 	csskd->nthreads.value.ui64 = css->nthreads;
2914 	csskd->cpumigrate.value.ui64 = css->cpumigrate;
2915 	csskd->xcalls.value.ui64 = css->xcalls;
2916 	csskd->mutex_adenters.value.ui64 = css->mutex_adenters;
2917 	csskd->rw_rdfails.value.ui64 = css->rw_rdfails;
2918 	csskd->rw_wrfails.value.ui64 = css->rw_wrfails;
2919 	csskd->modload.value.ui64 = css->modload;
2920 	csskd->modunload.value.ui64 = css->modunload;
2921 	csskd->bawrite.value.ui64 = css->bawrite;
2922 	csskd->iowait.value.ui64 = 0;
2923 
2924 	return (0);
2925 }
2926 
2927 static int
2928 cpu_vm_stats_ks_update(kstat_t *ksp, int rw)
2929 {
2930 	cpu_t *cp = (cpu_t *)ksp->ks_private;
2931 	struct cpu_vm_stats_ks_data *cvskd;
2932 	cpu_vm_stats_t *cvs;
2933 
2934 	if (rw == KSTAT_WRITE)
2935 		return (EACCES);
2936 
2937 	cvs = &cp->cpu_stats.vm;
2938 	cvskd = ksp->ks_data;
2939 
2940 	bcopy(&cpu_vm_stats_ks_data_template, ksp->ks_data,
2941 	    sizeof (cpu_vm_stats_ks_data_template));
2942 	cvskd->pgrec.value.ui64 = cvs->pgrec;
2943 	cvskd->pgfrec.value.ui64 = cvs->pgfrec;
2944 	cvskd->pgin.value.ui64 = cvs->pgin;
2945 	cvskd->pgpgin.value.ui64 = cvs->pgpgin;
2946 	cvskd->pgout.value.ui64 = cvs->pgout;
2947 	cvskd->pgpgout.value.ui64 = cvs->pgpgout;
2948 	cvskd->swapin.value.ui64 = cvs->swapin;
2949 	cvskd->pgswapin.value.ui64 = cvs->pgswapin;
2950 	cvskd->swapout.value.ui64 = cvs->swapout;
2951 	cvskd->pgswapout.value.ui64 = cvs->pgswapout;
2952 	cvskd->zfod.value.ui64 = cvs->zfod;
2953 	cvskd->dfree.value.ui64 = cvs->dfree;
2954 	cvskd->scan.value.ui64 = cvs->scan;
2955 	cvskd->rev.value.ui64 = cvs->rev;
2956 	cvskd->hat_fault.value.ui64 = cvs->hat_fault;
2957 	cvskd->as_fault.value.ui64 = cvs->as_fault;
2958 	cvskd->maj_fault.value.ui64 = cvs->maj_fault;
2959 	cvskd->cow_fault.value.ui64 = cvs->cow_fault;
2960 	cvskd->prot_fault.value.ui64 = cvs->prot_fault;
2961 	cvskd->softlock.value.ui64 = cvs->softlock;
2962 	cvskd->kernel_asflt.value.ui64 = cvs->kernel_asflt;
2963 	cvskd->pgrrun.value.ui64 = cvs->pgrrun;
2964 	cvskd->execpgin.value.ui64 = cvs->execpgin;
2965 	cvskd->execpgout.value.ui64 = cvs->execpgout;
2966 	cvskd->execfree.value.ui64 = cvs->execfree;
2967 	cvskd->anonpgin.value.ui64 = cvs->anonpgin;
2968 	cvskd->anonpgout.value.ui64 = cvs->anonpgout;
2969 	cvskd->anonfree.value.ui64 = cvs->anonfree;
2970 	cvskd->fspgin.value.ui64 = cvs->fspgin;
2971 	cvskd->fspgout.value.ui64 = cvs->fspgout;
2972 	cvskd->fsfree.value.ui64 = cvs->fsfree;
2973 
2974 	return (0);
2975 }
2976 
2977 static int
2978 cpu_stat_ks_update(kstat_t *ksp, int rw)
2979 {
2980 	cpu_stat_t *cso;
2981 	cpu_t *cp;
2982 	int i;
2983 	hrtime_t msnsecs[NCMSTATES];
2984 
2985 	cso = (cpu_stat_t *)ksp->ks_data;
2986 	cp = (cpu_t *)ksp->ks_private;
2987 
2988 	if (rw == KSTAT_WRITE)
2989 		return (EACCES);
2990 
2991 	/*
2992 	 * Read CPU mstate, but compare with the last values we
2993 	 * received to make sure that the returned kstats never
2994 	 * decrease.
2995 	 */
2996 
2997 	get_cpu_mstate(cp, msnsecs);
2998 	msnsecs[CMS_IDLE] = NSEC_TO_TICK(msnsecs[CMS_IDLE]);
2999 	msnsecs[CMS_USER] = NSEC_TO_TICK(msnsecs[CMS_USER]);
3000 	msnsecs[CMS_SYSTEM] = NSEC_TO_TICK(msnsecs[CMS_SYSTEM]);
3001 	if (cso->cpu_sysinfo.cpu[CPU_IDLE] < msnsecs[CMS_IDLE])
3002 		cso->cpu_sysinfo.cpu[CPU_IDLE] = msnsecs[CMS_IDLE];
3003 	if (cso->cpu_sysinfo.cpu[CPU_USER] < msnsecs[CMS_USER])
3004 		cso->cpu_sysinfo.cpu[CPU_USER] = msnsecs[CMS_USER];
3005 	if (cso->cpu_sysinfo.cpu[CPU_KERNEL] < msnsecs[CMS_SYSTEM])
3006 		cso->cpu_sysinfo.cpu[CPU_KERNEL] = msnsecs[CMS_SYSTEM];
3007 	cso->cpu_sysinfo.cpu[CPU_WAIT] 	= 0;
3008 	cso->cpu_sysinfo.wait[W_IO] 	= 0;
3009 	cso->cpu_sysinfo.wait[W_SWAP]	= 0;
3010 	cso->cpu_sysinfo.wait[W_PIO]	= 0;
3011 	cso->cpu_sysinfo.bread 		= CPU_STATS(cp, sys.bread);
3012 	cso->cpu_sysinfo.bwrite 	= CPU_STATS(cp, sys.bwrite);
3013 	cso->cpu_sysinfo.lread 		= CPU_STATS(cp, sys.lread);
3014 	cso->cpu_sysinfo.lwrite 	= CPU_STATS(cp, sys.lwrite);
3015 	cso->cpu_sysinfo.phread 	= CPU_STATS(cp, sys.phread);
3016 	cso->cpu_sysinfo.phwrite 	= CPU_STATS(cp, sys.phwrite);
3017 	cso->cpu_sysinfo.pswitch 	= CPU_STATS(cp, sys.pswitch);
3018 	cso->cpu_sysinfo.trap 		= CPU_STATS(cp, sys.trap);
3019 	cso->cpu_sysinfo.intr		= 0;
3020 	for (i = 0; i < PIL_MAX; i++)
3021 		cso->cpu_sysinfo.intr += CPU_STATS(cp, sys.intr[i]);
3022 	cso->cpu_sysinfo.syscall	= CPU_STATS(cp, sys.syscall);
3023 	cso->cpu_sysinfo.sysread	= CPU_STATS(cp, sys.sysread);
3024 	cso->cpu_sysinfo.syswrite	= CPU_STATS(cp, sys.syswrite);
3025 	cso->cpu_sysinfo.sysfork	= CPU_STATS(cp, sys.sysfork);
3026 	cso->cpu_sysinfo.sysvfork	= CPU_STATS(cp, sys.sysvfork);
3027 	cso->cpu_sysinfo.sysexec	= CPU_STATS(cp, sys.sysexec);
3028 	cso->cpu_sysinfo.readch		= CPU_STATS(cp, sys.readch);
3029 	cso->cpu_sysinfo.writech	= CPU_STATS(cp, sys.writech);
3030 	cso->cpu_sysinfo.rcvint		= CPU_STATS(cp, sys.rcvint);
3031 	cso->cpu_sysinfo.xmtint		= CPU_STATS(cp, sys.xmtint);
3032 	cso->cpu_sysinfo.mdmint		= CPU_STATS(cp, sys.mdmint);
3033 	cso->cpu_sysinfo.rawch		= CPU_STATS(cp, sys.rawch);
3034 	cso->cpu_sysinfo.canch		= CPU_STATS(cp, sys.canch);
3035 	cso->cpu_sysinfo.outch		= CPU_STATS(cp, sys.outch);
3036 	cso->cpu_sysinfo.msg		= CPU_STATS(cp, sys.msg);
3037 	cso->cpu_sysinfo.sema		= CPU_STATS(cp, sys.sema);
3038 	cso->cpu_sysinfo.namei		= CPU_STATS(cp, sys.namei);
3039 	cso->cpu_sysinfo.ufsiget	= CPU_STATS(cp, sys.ufsiget);
3040 	cso->cpu_sysinfo.ufsdirblk	= CPU_STATS(cp, sys.ufsdirblk);
3041 	cso->cpu_sysinfo.ufsipage	= CPU_STATS(cp, sys.ufsipage);
3042 	cso->cpu_sysinfo.ufsinopage	= CPU_STATS(cp, sys.ufsinopage);
3043 	cso->cpu_sysinfo.inodeovf	= 0;
3044 	cso->cpu_sysinfo.fileovf	= 0;
3045 	cso->cpu_sysinfo.procovf	= CPU_STATS(cp, sys.procovf);
3046 	cso->cpu_sysinfo.intrthread	= 0;
3047 	for (i = 0; i < LOCK_LEVEL; i++)
3048 		cso->cpu_sysinfo.intrthread += CPU_STATS(cp, sys.intr[i]);
3049 	cso->cpu_sysinfo.intrblk	= CPU_STATS(cp, sys.intrblk);
3050 	cso->cpu_sysinfo.idlethread	= CPU_STATS(cp, sys.idlethread);
3051 	cso->cpu_sysinfo.inv_swtch	= CPU_STATS(cp, sys.inv_swtch);
3052 	cso->cpu_sysinfo.nthreads	= CPU_STATS(cp, sys.nthreads);
3053 	cso->cpu_sysinfo.cpumigrate	= CPU_STATS(cp, sys.cpumigrate);
3054 	cso->cpu_sysinfo.xcalls		= CPU_STATS(cp, sys.xcalls);
3055 	cso->cpu_sysinfo.mutex_adenters	= CPU_STATS(cp, sys.mutex_adenters);
3056 	cso->cpu_sysinfo.rw_rdfails	= CPU_STATS(cp, sys.rw_rdfails);
3057 	cso->cpu_sysinfo.rw_wrfails	= CPU_STATS(cp, sys.rw_wrfails);
3058 	cso->cpu_sysinfo.modload	= CPU_STATS(cp, sys.modload);
3059 	cso->cpu_sysinfo.modunload	= CPU_STATS(cp, sys.modunload);
3060 	cso->cpu_sysinfo.bawrite	= CPU_STATS(cp, sys.bawrite);
3061 	cso->cpu_sysinfo.rw_enters	= 0;
3062 	cso->cpu_sysinfo.win_uo_cnt	= 0;
3063 	cso->cpu_sysinfo.win_uu_cnt	= 0;
3064 	cso->cpu_sysinfo.win_so_cnt	= 0;
3065 	cso->cpu_sysinfo.win_su_cnt	= 0;
3066 	cso->cpu_sysinfo.win_suo_cnt	= 0;
3067 
3068 	cso->cpu_syswait.iowait		= 0;
3069 	cso->cpu_syswait.swap		= 0;
3070 	cso->cpu_syswait.physio		= 0;
3071 
3072 	cso->cpu_vminfo.pgrec		= CPU_STATS(cp, vm.pgrec);
3073 	cso->cpu_vminfo.pgfrec		= CPU_STATS(cp, vm.pgfrec);
3074 	cso->cpu_vminfo.pgin		= CPU_STATS(cp, vm.pgin);
3075 	cso->cpu_vminfo.pgpgin		= CPU_STATS(cp, vm.pgpgin);
3076 	cso->cpu_vminfo.pgout		= CPU_STATS(cp, vm.pgout);
3077 	cso->cpu_vminfo.pgpgout		= CPU_STATS(cp, vm.pgpgout);
3078 	cso->cpu_vminfo.swapin		= CPU_STATS(cp, vm.swapin);
3079 	cso->cpu_vminfo.pgswapin	= CPU_STATS(cp, vm.pgswapin);
3080 	cso->cpu_vminfo.swapout		= CPU_STATS(cp, vm.swapout);
3081 	cso->cpu_vminfo.pgswapout	= CPU_STATS(cp, vm.pgswapout);
3082 	cso->cpu_vminfo.zfod		= CPU_STATS(cp, vm.zfod);
3083 	cso->cpu_vminfo.dfree		= CPU_STATS(cp, vm.dfree);
3084 	cso->cpu_vminfo.scan		= CPU_STATS(cp, vm.scan);
3085 	cso->cpu_vminfo.rev		= CPU_STATS(cp, vm.rev);
3086 	cso->cpu_vminfo.hat_fault	= CPU_STATS(cp, vm.hat_fault);
3087 	cso->cpu_vminfo.as_fault	= CPU_STATS(cp, vm.as_fault);
3088 	cso->cpu_vminfo.maj_fault	= CPU_STATS(cp, vm.maj_fault);
3089 	cso->cpu_vminfo.cow_fault	= CPU_STATS(cp, vm.cow_fault);
3090 	cso->cpu_vminfo.prot_fault	= CPU_STATS(cp, vm.prot_fault);
3091 	cso->cpu_vminfo.softlock	= CPU_STATS(cp, vm.softlock);
3092 	cso->cpu_vminfo.kernel_asflt	= CPU_STATS(cp, vm.kernel_asflt);
3093 	cso->cpu_vminfo.pgrrun		= CPU_STATS(cp, vm.pgrrun);
3094 	cso->cpu_vminfo.execpgin	= CPU_STATS(cp, vm.execpgin);
3095 	cso->cpu_vminfo.execpgout	= CPU_STATS(cp, vm.execpgout);
3096 	cso->cpu_vminfo.execfree	= CPU_STATS(cp, vm.execfree);
3097 	cso->cpu_vminfo.anonpgin	= CPU_STATS(cp, vm.anonpgin);
3098 	cso->cpu_vminfo.anonpgout	= CPU_STATS(cp, vm.anonpgout);
3099 	cso->cpu_vminfo.anonfree	= CPU_STATS(cp, vm.anonfree);
3100 	cso->cpu_vminfo.fspgin		= CPU_STATS(cp, vm.fspgin);
3101 	cso->cpu_vminfo.fspgout		= CPU_STATS(cp, vm.fspgout);
3102 	cso->cpu_vminfo.fsfree		= CPU_STATS(cp, vm.fsfree);
3103 
3104 	return (0);
3105 }
3106