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