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