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