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