xref: /titanic_51/usr/src/uts/common/os/cpu.c (revision 1aa15ad6fefa3d2776c09907cffab769c717f05f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Architecture-independent CPU control functions.
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <sys/param.h>
357c478bd9Sstevel@tonic-gate #include <sys/var.h>
367c478bd9Sstevel@tonic-gate #include <sys/thread.h>
377c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
387c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
397c478bd9Sstevel@tonic-gate #include <sys/uadmin.h>
407c478bd9Sstevel@tonic-gate #include <sys/systm.h>
417c478bd9Sstevel@tonic-gate #include <sys/errno.h>
427c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
437c478bd9Sstevel@tonic-gate #include <sys/procset.h>
447c478bd9Sstevel@tonic-gate #include <sys/processor.h>
457c478bd9Sstevel@tonic-gate #include <sys/debug.h>
467c478bd9Sstevel@tonic-gate #include <sys/cpupart.h>
477c478bd9Sstevel@tonic-gate #include <sys/lgrp.h>
487c478bd9Sstevel@tonic-gate #include <sys/pset.h>
497c478bd9Sstevel@tonic-gate #include <sys/chip.h>
507c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
517c478bd9Sstevel@tonic-gate #include <sys/kmem_impl.h>	/* to set per-cpu kmem_cache offset */
527c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
537c478bd9Sstevel@tonic-gate #include <sys/callb.h>
547c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
557c478bd9Sstevel@tonic-gate #include <sys/cyclic.h>
567c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
577c478bd9Sstevel@tonic-gate #include <sys/nvpair.h>
587c478bd9Sstevel@tonic-gate #include <sys/pool_pset.h>
597c478bd9Sstevel@tonic-gate #include <sys/msacct.h>
607c478bd9Sstevel@tonic-gate #include <sys/time.h>
617c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate extern int	mp_cpu_start(cpu_t *);
647c478bd9Sstevel@tonic-gate extern int	mp_cpu_stop(cpu_t *);
657c478bd9Sstevel@tonic-gate extern int	mp_cpu_poweron(cpu_t *);
667c478bd9Sstevel@tonic-gate extern int	mp_cpu_poweroff(cpu_t *);
677c478bd9Sstevel@tonic-gate extern int	mp_cpu_configure(int);
687c478bd9Sstevel@tonic-gate extern int	mp_cpu_unconfigure(int);
697c478bd9Sstevel@tonic-gate extern void	mp_cpu_faulted_enter(cpu_t *);
707c478bd9Sstevel@tonic-gate extern void	mp_cpu_faulted_exit(cpu_t *);
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate extern int cmp_cpu_to_chip(processorid_t cpuid);
737c478bd9Sstevel@tonic-gate #ifdef __sparcv9
747c478bd9Sstevel@tonic-gate extern char *cpu_fru_fmri(cpu_t *cp);
757c478bd9Sstevel@tonic-gate #endif
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate static void cpu_add_active_internal(cpu_t *cp);
787c478bd9Sstevel@tonic-gate static void cpu_remove_active(cpu_t *cp);
797c478bd9Sstevel@tonic-gate static void cpu_info_kstat_create(cpu_t *cp);
807c478bd9Sstevel@tonic-gate static void cpu_info_kstat_destroy(cpu_t *cp);
817c478bd9Sstevel@tonic-gate static void cpu_stats_kstat_create(cpu_t *cp);
827c478bd9Sstevel@tonic-gate static void cpu_stats_kstat_destroy(cpu_t *cp);
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate static int cpu_sys_stats_ks_update(kstat_t *ksp, int rw);
857c478bd9Sstevel@tonic-gate static int cpu_vm_stats_ks_update(kstat_t *ksp, int rw);
867c478bd9Sstevel@tonic-gate static int cpu_stat_ks_update(kstat_t *ksp, int rw);
877c478bd9Sstevel@tonic-gate static int cpu_state_change_hooks(int, cpu_setup_t, cpu_setup_t);
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate /*
907c478bd9Sstevel@tonic-gate  * cpu_lock protects ncpus, ncpus_online, cpu_flag, cpu_list, cpu_active,
917c478bd9Sstevel@tonic-gate  * and dispatch queue reallocations.  The lock ordering with respect to
927c478bd9Sstevel@tonic-gate  * related locks is:
937c478bd9Sstevel@tonic-gate  *
947c478bd9Sstevel@tonic-gate  *	cpu_lock --> thread_free_lock  --->  p_lock  --->  thread_lock()
957c478bd9Sstevel@tonic-gate  *
967c478bd9Sstevel@tonic-gate  * Warning:  Certain sections of code do not use the cpu_lock when
977c478bd9Sstevel@tonic-gate  * traversing the cpu_list (e.g. mutex_vector_enter(), clock()).  Since
987c478bd9Sstevel@tonic-gate  * all cpus are paused during modifications to this list, a solution
997c478bd9Sstevel@tonic-gate  * to protect the list is too either disable kernel preemption while
1007c478bd9Sstevel@tonic-gate  * walking the list, *or* recheck the cpu_next pointer at each
1017c478bd9Sstevel@tonic-gate  * iteration in the loop.  Note that in no cases can any cached
1027c478bd9Sstevel@tonic-gate  * copies of the cpu pointers be kept as they may become invalid.
1037c478bd9Sstevel@tonic-gate  */
1047c478bd9Sstevel@tonic-gate kmutex_t	cpu_lock;
1057c478bd9Sstevel@tonic-gate cpu_t		*cpu_list;		/* list of all CPUs */
1067c478bd9Sstevel@tonic-gate cpu_t		*cpu_active;		/* list of active CPUs */
1077c478bd9Sstevel@tonic-gate static cpuset_t	cpu_available;		/* set of available CPUs */
1087c478bd9Sstevel@tonic-gate cpuset_t	cpu_seqid_inuse;	/* which cpu_seqids are in use */
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate /*
1117c478bd9Sstevel@tonic-gate  * max_ncpus keeps the max cpus the system can have. Initially
1127c478bd9Sstevel@tonic-gate  * it's NCPU, but since most archs scan the devtree for cpus
1137c478bd9Sstevel@tonic-gate  * fairly early on during boot, the real max can be known before
1147c478bd9Sstevel@tonic-gate  * ncpus is set (useful for early NCPU based allocations).
1157c478bd9Sstevel@tonic-gate  */
1167c478bd9Sstevel@tonic-gate int max_ncpus = NCPU;
1177c478bd9Sstevel@tonic-gate /*
1187c478bd9Sstevel@tonic-gate  * platforms that set max_ncpus to maxiumum number of cpus that can be
1197c478bd9Sstevel@tonic-gate  * dynamically added will set boot_max_ncpus to the number of cpus found
1207c478bd9Sstevel@tonic-gate  * at device tree scan time during boot.
1217c478bd9Sstevel@tonic-gate  */
1227c478bd9Sstevel@tonic-gate int boot_max_ncpus = -1;
1237c478bd9Sstevel@tonic-gate /*
1247c478bd9Sstevel@tonic-gate  * Maximum possible CPU id.  This can never be >= NCPU since NCPU is
1257c478bd9Sstevel@tonic-gate  * used to size arrays that are indexed by CPU id.
1267c478bd9Sstevel@tonic-gate  */
1277c478bd9Sstevel@tonic-gate processorid_t max_cpuid = NCPU - 1;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate int ncpus = 1;
1307c478bd9Sstevel@tonic-gate int ncpus_online = 1;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate /*
1337c478bd9Sstevel@tonic-gate  * CPU that we're trying to offline.  Protected by cpu_lock.
1347c478bd9Sstevel@tonic-gate  */
1357c478bd9Sstevel@tonic-gate cpu_t *cpu_inmotion;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate /*
1387c478bd9Sstevel@tonic-gate  * Can be raised to suppress further weakbinding, which are instead
1397c478bd9Sstevel@tonic-gate  * satisfied by disabling preemption.  Must be raised/lowered under cpu_lock,
1407c478bd9Sstevel@tonic-gate  * while individual thread weakbinding synchronisation is done under thread
1417c478bd9Sstevel@tonic-gate  * lock.
1427c478bd9Sstevel@tonic-gate  */
1437c478bd9Sstevel@tonic-gate int weakbindingbarrier;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate /*
1467c478bd9Sstevel@tonic-gate  * values for safe_list.  Pause state that CPUs are in.
1477c478bd9Sstevel@tonic-gate  */
1487c478bd9Sstevel@tonic-gate #define	PAUSE_IDLE	0		/* normal state */
1497c478bd9Sstevel@tonic-gate #define	PAUSE_READY	1		/* paused thread ready to spl */
1507c478bd9Sstevel@tonic-gate #define	PAUSE_WAIT	2		/* paused thread is spl-ed high */
1517c478bd9Sstevel@tonic-gate #define	PAUSE_DIE	3		/* tell pause thread to leave */
1527c478bd9Sstevel@tonic-gate #define	PAUSE_DEAD	4		/* pause thread has left */
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate /*
1557c478bd9Sstevel@tonic-gate  * Variables used in pause_cpus().
1567c478bd9Sstevel@tonic-gate  */
1577c478bd9Sstevel@tonic-gate static volatile char safe_list[NCPU];
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate static struct _cpu_pause_info {
1607c478bd9Sstevel@tonic-gate 	int		cp_spl;		/* spl saved in pause_cpus() */
1617c478bd9Sstevel@tonic-gate 	volatile int	cp_go;		/* Go signal sent after all ready */
1627c478bd9Sstevel@tonic-gate 	int		cp_count;	/* # of CPUs to pause */
1637c478bd9Sstevel@tonic-gate 	ksema_t		cp_sem;		/* synch pause_cpus & cpu_pause */
1647c478bd9Sstevel@tonic-gate 	kthread_id_t	cp_paused;
1657c478bd9Sstevel@tonic-gate } cpu_pause_info;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate static kmutex_t pause_free_mutex;
1687c478bd9Sstevel@tonic-gate static kcondvar_t pause_free_cv;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate static struct cpu_sys_stats_ks_data {
1717c478bd9Sstevel@tonic-gate 	kstat_named_t cpu_ticks_idle;
1727c478bd9Sstevel@tonic-gate 	kstat_named_t cpu_ticks_user;
1737c478bd9Sstevel@tonic-gate 	kstat_named_t cpu_ticks_kernel;
1747c478bd9Sstevel@tonic-gate 	kstat_named_t cpu_ticks_wait;
1757c478bd9Sstevel@tonic-gate 	kstat_named_t cpu_nsec_idle;
1767c478bd9Sstevel@tonic-gate 	kstat_named_t cpu_nsec_user;
1777c478bd9Sstevel@tonic-gate 	kstat_named_t cpu_nsec_kernel;
1787c478bd9Sstevel@tonic-gate 	kstat_named_t wait_ticks_io;
1797c478bd9Sstevel@tonic-gate 	kstat_named_t bread;
1807c478bd9Sstevel@tonic-gate 	kstat_named_t bwrite;
1817c478bd9Sstevel@tonic-gate 	kstat_named_t lread;
1827c478bd9Sstevel@tonic-gate 	kstat_named_t lwrite;
1837c478bd9Sstevel@tonic-gate 	kstat_named_t phread;
1847c478bd9Sstevel@tonic-gate 	kstat_named_t phwrite;
1857c478bd9Sstevel@tonic-gate 	kstat_named_t pswitch;
1867c478bd9Sstevel@tonic-gate 	kstat_named_t trap;
1877c478bd9Sstevel@tonic-gate 	kstat_named_t intr;
1887c478bd9Sstevel@tonic-gate 	kstat_named_t syscall;
1897c478bd9Sstevel@tonic-gate 	kstat_named_t sysread;
1907c478bd9Sstevel@tonic-gate 	kstat_named_t syswrite;
1917c478bd9Sstevel@tonic-gate 	kstat_named_t sysfork;
1927c478bd9Sstevel@tonic-gate 	kstat_named_t sysvfork;
1937c478bd9Sstevel@tonic-gate 	kstat_named_t sysexec;
1947c478bd9Sstevel@tonic-gate 	kstat_named_t readch;
1957c478bd9Sstevel@tonic-gate 	kstat_named_t writech;
1967c478bd9Sstevel@tonic-gate 	kstat_named_t rcvint;
1977c478bd9Sstevel@tonic-gate 	kstat_named_t xmtint;
1987c478bd9Sstevel@tonic-gate 	kstat_named_t mdmint;
1997c478bd9Sstevel@tonic-gate 	kstat_named_t rawch;
2007c478bd9Sstevel@tonic-gate 	kstat_named_t canch;
2017c478bd9Sstevel@tonic-gate 	kstat_named_t outch;
2027c478bd9Sstevel@tonic-gate 	kstat_named_t msg;
2037c478bd9Sstevel@tonic-gate 	kstat_named_t sema;
2047c478bd9Sstevel@tonic-gate 	kstat_named_t namei;
2057c478bd9Sstevel@tonic-gate 	kstat_named_t ufsiget;
2067c478bd9Sstevel@tonic-gate 	kstat_named_t ufsdirblk;
2077c478bd9Sstevel@tonic-gate 	kstat_named_t ufsipage;
2087c478bd9Sstevel@tonic-gate 	kstat_named_t ufsinopage;
2097c478bd9Sstevel@tonic-gate 	kstat_named_t procovf;
2107c478bd9Sstevel@tonic-gate 	kstat_named_t intrthread;
2117c478bd9Sstevel@tonic-gate 	kstat_named_t intrblk;
2127c478bd9Sstevel@tonic-gate 	kstat_named_t intrunpin;
2137c478bd9Sstevel@tonic-gate 	kstat_named_t idlethread;
2147c478bd9Sstevel@tonic-gate 	kstat_named_t inv_swtch;
2157c478bd9Sstevel@tonic-gate 	kstat_named_t nthreads;
2167c478bd9Sstevel@tonic-gate 	kstat_named_t cpumigrate;
2177c478bd9Sstevel@tonic-gate 	kstat_named_t xcalls;
2187c478bd9Sstevel@tonic-gate 	kstat_named_t mutex_adenters;
2197c478bd9Sstevel@tonic-gate 	kstat_named_t rw_rdfails;
2207c478bd9Sstevel@tonic-gate 	kstat_named_t rw_wrfails;
2217c478bd9Sstevel@tonic-gate 	kstat_named_t modload;
2227c478bd9Sstevel@tonic-gate 	kstat_named_t modunload;
2237c478bd9Sstevel@tonic-gate 	kstat_named_t bawrite;
2247c478bd9Sstevel@tonic-gate 	kstat_named_t iowait;
2257c478bd9Sstevel@tonic-gate } cpu_sys_stats_ks_data_template = {
2267c478bd9Sstevel@tonic-gate 	{ "cpu_ticks_idle", 	KSTAT_DATA_UINT64 },
2277c478bd9Sstevel@tonic-gate 	{ "cpu_ticks_user", 	KSTAT_DATA_UINT64 },
2287c478bd9Sstevel@tonic-gate 	{ "cpu_ticks_kernel", 	KSTAT_DATA_UINT64 },
2297c478bd9Sstevel@tonic-gate 	{ "cpu_ticks_wait", 	KSTAT_DATA_UINT64 },
2307c478bd9Sstevel@tonic-gate 	{ "cpu_nsec_idle",	KSTAT_DATA_UINT64 },
2317c478bd9Sstevel@tonic-gate 	{ "cpu_nsec_user",	KSTAT_DATA_UINT64 },
2327c478bd9Sstevel@tonic-gate 	{ "cpu_nsec_kernel",	KSTAT_DATA_UINT64 },
2337c478bd9Sstevel@tonic-gate 	{ "wait_ticks_io", 	KSTAT_DATA_UINT64 },
2347c478bd9Sstevel@tonic-gate 	{ "bread", 		KSTAT_DATA_UINT64 },
2357c478bd9Sstevel@tonic-gate 	{ "bwrite", 		KSTAT_DATA_UINT64 },
2367c478bd9Sstevel@tonic-gate 	{ "lread", 		KSTAT_DATA_UINT64 },
2377c478bd9Sstevel@tonic-gate 	{ "lwrite", 		KSTAT_DATA_UINT64 },
2387c478bd9Sstevel@tonic-gate 	{ "phread", 		KSTAT_DATA_UINT64 },
2397c478bd9Sstevel@tonic-gate 	{ "phwrite", 		KSTAT_DATA_UINT64 },
2407c478bd9Sstevel@tonic-gate 	{ "pswitch", 		KSTAT_DATA_UINT64 },
2417c478bd9Sstevel@tonic-gate 	{ "trap", 		KSTAT_DATA_UINT64 },
2427c478bd9Sstevel@tonic-gate 	{ "intr", 		KSTAT_DATA_UINT64 },
2437c478bd9Sstevel@tonic-gate 	{ "syscall", 		KSTAT_DATA_UINT64 },
2447c478bd9Sstevel@tonic-gate 	{ "sysread", 		KSTAT_DATA_UINT64 },
2457c478bd9Sstevel@tonic-gate 	{ "syswrite", 		KSTAT_DATA_UINT64 },
2467c478bd9Sstevel@tonic-gate 	{ "sysfork", 		KSTAT_DATA_UINT64 },
2477c478bd9Sstevel@tonic-gate 	{ "sysvfork", 		KSTAT_DATA_UINT64 },
2487c478bd9Sstevel@tonic-gate 	{ "sysexec", 		KSTAT_DATA_UINT64 },
2497c478bd9Sstevel@tonic-gate 	{ "readch", 		KSTAT_DATA_UINT64 },
2507c478bd9Sstevel@tonic-gate 	{ "writech", 		KSTAT_DATA_UINT64 },
2517c478bd9Sstevel@tonic-gate 	{ "rcvint", 		KSTAT_DATA_UINT64 },
2527c478bd9Sstevel@tonic-gate 	{ "xmtint", 		KSTAT_DATA_UINT64 },
2537c478bd9Sstevel@tonic-gate 	{ "mdmint", 		KSTAT_DATA_UINT64 },
2547c478bd9Sstevel@tonic-gate 	{ "rawch", 		KSTAT_DATA_UINT64 },
2557c478bd9Sstevel@tonic-gate 	{ "canch", 		KSTAT_DATA_UINT64 },
2567c478bd9Sstevel@tonic-gate 	{ "outch", 		KSTAT_DATA_UINT64 },
2577c478bd9Sstevel@tonic-gate 	{ "msg", 		KSTAT_DATA_UINT64 },
2587c478bd9Sstevel@tonic-gate 	{ "sema", 		KSTAT_DATA_UINT64 },
2597c478bd9Sstevel@tonic-gate 	{ "namei", 		KSTAT_DATA_UINT64 },
2607c478bd9Sstevel@tonic-gate 	{ "ufsiget", 		KSTAT_DATA_UINT64 },
2617c478bd9Sstevel@tonic-gate 	{ "ufsdirblk", 		KSTAT_DATA_UINT64 },
2627c478bd9Sstevel@tonic-gate 	{ "ufsipage", 		KSTAT_DATA_UINT64 },
2637c478bd9Sstevel@tonic-gate 	{ "ufsinopage", 	KSTAT_DATA_UINT64 },
2647c478bd9Sstevel@tonic-gate 	{ "procovf", 		KSTAT_DATA_UINT64 },
2657c478bd9Sstevel@tonic-gate 	{ "intrthread", 	KSTAT_DATA_UINT64 },
2667c478bd9Sstevel@tonic-gate 	{ "intrblk", 		KSTAT_DATA_UINT64 },
2677c478bd9Sstevel@tonic-gate 	{ "intrunpin",		KSTAT_DATA_UINT64 },
2687c478bd9Sstevel@tonic-gate 	{ "idlethread", 	KSTAT_DATA_UINT64 },
2697c478bd9Sstevel@tonic-gate 	{ "inv_swtch", 		KSTAT_DATA_UINT64 },
2707c478bd9Sstevel@tonic-gate 	{ "nthreads", 		KSTAT_DATA_UINT64 },
2717c478bd9Sstevel@tonic-gate 	{ "cpumigrate", 	KSTAT_DATA_UINT64 },
2727c478bd9Sstevel@tonic-gate 	{ "xcalls", 		KSTAT_DATA_UINT64 },
2737c478bd9Sstevel@tonic-gate 	{ "mutex_adenters", 	KSTAT_DATA_UINT64 },
2747c478bd9Sstevel@tonic-gate 	{ "rw_rdfails", 	KSTAT_DATA_UINT64 },
2757c478bd9Sstevel@tonic-gate 	{ "rw_wrfails", 	KSTAT_DATA_UINT64 },
2767c478bd9Sstevel@tonic-gate 	{ "modload", 		KSTAT_DATA_UINT64 },
2777c478bd9Sstevel@tonic-gate 	{ "modunload", 		KSTAT_DATA_UINT64 },
2787c478bd9Sstevel@tonic-gate 	{ "bawrite", 		KSTAT_DATA_UINT64 },
2797c478bd9Sstevel@tonic-gate 	{ "iowait",		KSTAT_DATA_UINT64 },
2807c478bd9Sstevel@tonic-gate };
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate static struct cpu_vm_stats_ks_data {
2837c478bd9Sstevel@tonic-gate 	kstat_named_t pgrec;
2847c478bd9Sstevel@tonic-gate 	kstat_named_t pgfrec;
2857c478bd9Sstevel@tonic-gate 	kstat_named_t pgin;
2867c478bd9Sstevel@tonic-gate 	kstat_named_t pgpgin;
2877c478bd9Sstevel@tonic-gate 	kstat_named_t pgout;
2887c478bd9Sstevel@tonic-gate 	kstat_named_t pgpgout;
2897c478bd9Sstevel@tonic-gate 	kstat_named_t swapin;
2907c478bd9Sstevel@tonic-gate 	kstat_named_t pgswapin;
2917c478bd9Sstevel@tonic-gate 	kstat_named_t swapout;
2927c478bd9Sstevel@tonic-gate 	kstat_named_t pgswapout;
2937c478bd9Sstevel@tonic-gate 	kstat_named_t zfod;
2947c478bd9Sstevel@tonic-gate 	kstat_named_t dfree;
2957c478bd9Sstevel@tonic-gate 	kstat_named_t scan;
2967c478bd9Sstevel@tonic-gate 	kstat_named_t rev;
2977c478bd9Sstevel@tonic-gate 	kstat_named_t hat_fault;
2987c478bd9Sstevel@tonic-gate 	kstat_named_t as_fault;
2997c478bd9Sstevel@tonic-gate 	kstat_named_t maj_fault;
3007c478bd9Sstevel@tonic-gate 	kstat_named_t cow_fault;
3017c478bd9Sstevel@tonic-gate 	kstat_named_t prot_fault;
3027c478bd9Sstevel@tonic-gate 	kstat_named_t softlock;
3037c478bd9Sstevel@tonic-gate 	kstat_named_t kernel_asflt;
3047c478bd9Sstevel@tonic-gate 	kstat_named_t pgrrun;
3057c478bd9Sstevel@tonic-gate 	kstat_named_t execpgin;
3067c478bd9Sstevel@tonic-gate 	kstat_named_t execpgout;
3077c478bd9Sstevel@tonic-gate 	kstat_named_t execfree;
3087c478bd9Sstevel@tonic-gate 	kstat_named_t anonpgin;
3097c478bd9Sstevel@tonic-gate 	kstat_named_t anonpgout;
3107c478bd9Sstevel@tonic-gate 	kstat_named_t anonfree;
3117c478bd9Sstevel@tonic-gate 	kstat_named_t fspgin;
3127c478bd9Sstevel@tonic-gate 	kstat_named_t fspgout;
3137c478bd9Sstevel@tonic-gate 	kstat_named_t fsfree;
3147c478bd9Sstevel@tonic-gate } cpu_vm_stats_ks_data_template = {
3157c478bd9Sstevel@tonic-gate 	{ "pgrec",		KSTAT_DATA_UINT64 },
3167c478bd9Sstevel@tonic-gate 	{ "pgfrec",		KSTAT_DATA_UINT64 },
3177c478bd9Sstevel@tonic-gate 	{ "pgin",		KSTAT_DATA_UINT64 },
3187c478bd9Sstevel@tonic-gate 	{ "pgpgin",		KSTAT_DATA_UINT64 },
3197c478bd9Sstevel@tonic-gate 	{ "pgout",		KSTAT_DATA_UINT64 },
3207c478bd9Sstevel@tonic-gate 	{ "pgpgout",		KSTAT_DATA_UINT64 },
3217c478bd9Sstevel@tonic-gate 	{ "swapin",		KSTAT_DATA_UINT64 },
3227c478bd9Sstevel@tonic-gate 	{ "pgswapin",		KSTAT_DATA_UINT64 },
3237c478bd9Sstevel@tonic-gate 	{ "swapout",		KSTAT_DATA_UINT64 },
3247c478bd9Sstevel@tonic-gate 	{ "pgswapout",		KSTAT_DATA_UINT64 },
3257c478bd9Sstevel@tonic-gate 	{ "zfod",		KSTAT_DATA_UINT64 },
3267c478bd9Sstevel@tonic-gate 	{ "dfree",		KSTAT_DATA_UINT64 },
3277c478bd9Sstevel@tonic-gate 	{ "scan",		KSTAT_DATA_UINT64 },
3287c478bd9Sstevel@tonic-gate 	{ "rev",		KSTAT_DATA_UINT64 },
3297c478bd9Sstevel@tonic-gate 	{ "hat_fault",		KSTAT_DATA_UINT64 },
3307c478bd9Sstevel@tonic-gate 	{ "as_fault",		KSTAT_DATA_UINT64 },
3317c478bd9Sstevel@tonic-gate 	{ "maj_fault",		KSTAT_DATA_UINT64 },
3327c478bd9Sstevel@tonic-gate 	{ "cow_fault",		KSTAT_DATA_UINT64 },
3337c478bd9Sstevel@tonic-gate 	{ "prot_fault",		KSTAT_DATA_UINT64 },
3347c478bd9Sstevel@tonic-gate 	{ "softlock",		KSTAT_DATA_UINT64 },
3357c478bd9Sstevel@tonic-gate 	{ "kernel_asflt",	KSTAT_DATA_UINT64 },
3367c478bd9Sstevel@tonic-gate 	{ "pgrrun",		KSTAT_DATA_UINT64 },
3377c478bd9Sstevel@tonic-gate 	{ "execpgin",		KSTAT_DATA_UINT64 },
3387c478bd9Sstevel@tonic-gate 	{ "execpgout",		KSTAT_DATA_UINT64 },
3397c478bd9Sstevel@tonic-gate 	{ "execfree",		KSTAT_DATA_UINT64 },
3407c478bd9Sstevel@tonic-gate 	{ "anonpgin",		KSTAT_DATA_UINT64 },
3417c478bd9Sstevel@tonic-gate 	{ "anonpgout",		KSTAT_DATA_UINT64 },
3427c478bd9Sstevel@tonic-gate 	{ "anonfree",		KSTAT_DATA_UINT64 },
3437c478bd9Sstevel@tonic-gate 	{ "fspgin",		KSTAT_DATA_UINT64 },
3447c478bd9Sstevel@tonic-gate 	{ "fspgout",		KSTAT_DATA_UINT64 },
3457c478bd9Sstevel@tonic-gate 	{ "fsfree",		KSTAT_DATA_UINT64 },
3467c478bd9Sstevel@tonic-gate };
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate /*
3497c478bd9Sstevel@tonic-gate  * Force the specified thread to migrate to the appropriate processor.
3507c478bd9Sstevel@tonic-gate  * Called with thread lock held, returns with it dropped.
3517c478bd9Sstevel@tonic-gate  */
3527c478bd9Sstevel@tonic-gate static void
3537c478bd9Sstevel@tonic-gate force_thread_migrate(kthread_id_t tp)
3547c478bd9Sstevel@tonic-gate {
3557c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(tp));
3567c478bd9Sstevel@tonic-gate 	if (tp == curthread) {
3577c478bd9Sstevel@tonic-gate 		THREAD_TRANSITION(tp);
3587c478bd9Sstevel@tonic-gate 		CL_SETRUN(tp);
3597c478bd9Sstevel@tonic-gate 		thread_unlock_nopreempt(tp);
3607c478bd9Sstevel@tonic-gate 		swtch();
3617c478bd9Sstevel@tonic-gate 	} else {
3627c478bd9Sstevel@tonic-gate 		if (tp->t_state == TS_ONPROC) {
3637c478bd9Sstevel@tonic-gate 			cpu_surrender(tp);
3647c478bd9Sstevel@tonic-gate 		} else if (tp->t_state == TS_RUN) {
3657c478bd9Sstevel@tonic-gate 			(void) dispdeq(tp);
3667c478bd9Sstevel@tonic-gate 			setbackdq(tp);
3677c478bd9Sstevel@tonic-gate 		}
3687c478bd9Sstevel@tonic-gate 		thread_unlock(tp);
3697c478bd9Sstevel@tonic-gate 	}
3707c478bd9Sstevel@tonic-gate }
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate /*
3737c478bd9Sstevel@tonic-gate  * Set affinity for a specified CPU.
3747c478bd9Sstevel@tonic-gate  * A reference count is incremented and the affinity is held until the
3757c478bd9Sstevel@tonic-gate  * reference count is decremented to zero by thread_affinity_clear().
3767c478bd9Sstevel@tonic-gate  * This is so regions of code requiring affinity can be nested.
3777c478bd9Sstevel@tonic-gate  * Caller needs to ensure that cpu_id remains valid, which can be
3787c478bd9Sstevel@tonic-gate  * done by holding cpu_lock across this call, unless the caller
3797c478bd9Sstevel@tonic-gate  * specifies CPU_CURRENT in which case the cpu_lock will be acquired
3807c478bd9Sstevel@tonic-gate  * by thread_affinity_set and CPU->cpu_id will be the target CPU.
3817c478bd9Sstevel@tonic-gate  */
3827c478bd9Sstevel@tonic-gate void
3837c478bd9Sstevel@tonic-gate thread_affinity_set(kthread_id_t t, int cpu_id)
3847c478bd9Sstevel@tonic-gate {
3857c478bd9Sstevel@tonic-gate 	cpu_t		*cp;
3867c478bd9Sstevel@tonic-gate 	int		c;
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	ASSERT(!(t == curthread && t->t_weakbound_cpu != NULL));
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	if ((c = cpu_id) == CPU_CURRENT) {
3917c478bd9Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
3927c478bd9Sstevel@tonic-gate 		cpu_id = CPU->cpu_id;
3937c478bd9Sstevel@tonic-gate 	}
3947c478bd9Sstevel@tonic-gate 	/*
3957c478bd9Sstevel@tonic-gate 	 * We should be asserting that cpu_lock is held here, but
3967c478bd9Sstevel@tonic-gate 	 * the NCA code doesn't acquire it.  The following assert
3977c478bd9Sstevel@tonic-gate 	 * should be uncommented when the NCA code is fixed.
3987c478bd9Sstevel@tonic-gate 	 *
3997c478bd9Sstevel@tonic-gate 	 * ASSERT(MUTEX_HELD(&cpu_lock));
4007c478bd9Sstevel@tonic-gate 	 */
4017c478bd9Sstevel@tonic-gate 	ASSERT((cpu_id >= 0) && (cpu_id < NCPU));
4027c478bd9Sstevel@tonic-gate 	cp = cpu[cpu_id];
4037c478bd9Sstevel@tonic-gate 	ASSERT(cp != NULL);		/* user must provide a good cpu_id */
4047c478bd9Sstevel@tonic-gate 	/*
4057c478bd9Sstevel@tonic-gate 	 * If there is already a hard affinity requested, and this affinity
4067c478bd9Sstevel@tonic-gate 	 * conflicts with that, panic.
4077c478bd9Sstevel@tonic-gate 	 */
4087c478bd9Sstevel@tonic-gate 	thread_lock(t);
4097c478bd9Sstevel@tonic-gate 	if (t->t_affinitycnt > 0 && t->t_bound_cpu != cp) {
4107c478bd9Sstevel@tonic-gate 		panic("affinity_set: setting %p but already bound to %p",
4117c478bd9Sstevel@tonic-gate 		    (void *)cp, (void *)t->t_bound_cpu);
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate 	t->t_affinitycnt++;
4147c478bd9Sstevel@tonic-gate 	t->t_bound_cpu = cp;
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	/*
4177c478bd9Sstevel@tonic-gate 	 * Make sure we're running on the right CPU.
4187c478bd9Sstevel@tonic-gate 	 */
4197c478bd9Sstevel@tonic-gate 	if (cp != t->t_cpu || t != curthread) {
4207c478bd9Sstevel@tonic-gate 		force_thread_migrate(t);	/* drops thread lock */
4217c478bd9Sstevel@tonic-gate 	} else {
4227c478bd9Sstevel@tonic-gate 		thread_unlock(t);
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	if (c == CPU_CURRENT)
4267c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
4277c478bd9Sstevel@tonic-gate }
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate /*
4307c478bd9Sstevel@tonic-gate  *	Wrapper for backward compatibility.
4317c478bd9Sstevel@tonic-gate  */
4327c478bd9Sstevel@tonic-gate void
4337c478bd9Sstevel@tonic-gate affinity_set(int cpu_id)
4347c478bd9Sstevel@tonic-gate {
4357c478bd9Sstevel@tonic-gate 	thread_affinity_set(curthread, cpu_id);
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate /*
4397c478bd9Sstevel@tonic-gate  * Decrement the affinity reservation count and if it becomes zero,
4407c478bd9Sstevel@tonic-gate  * clear the CPU affinity for the current thread, or set it to the user's
4417c478bd9Sstevel@tonic-gate  * software binding request.
4427c478bd9Sstevel@tonic-gate  */
4437c478bd9Sstevel@tonic-gate void
4447c478bd9Sstevel@tonic-gate thread_affinity_clear(kthread_id_t t)
4457c478bd9Sstevel@tonic-gate {
4467c478bd9Sstevel@tonic-gate 	register processorid_t binding;
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	thread_lock(t);
4497c478bd9Sstevel@tonic-gate 	if (--t->t_affinitycnt == 0) {
4507c478bd9Sstevel@tonic-gate 		if ((binding = t->t_bind_cpu) == PBIND_NONE) {
4517c478bd9Sstevel@tonic-gate 			/*
4527c478bd9Sstevel@tonic-gate 			 * Adjust disp_max_unbound_pri if necessary.
4537c478bd9Sstevel@tonic-gate 			 */
4547c478bd9Sstevel@tonic-gate 			disp_adjust_unbound_pri(t);
4557c478bd9Sstevel@tonic-gate 			t->t_bound_cpu = NULL;
4567c478bd9Sstevel@tonic-gate 			if (t->t_cpu->cpu_part != t->t_cpupart) {
4577c478bd9Sstevel@tonic-gate 				force_thread_migrate(t);
4587c478bd9Sstevel@tonic-gate 				return;
4597c478bd9Sstevel@tonic-gate 			}
4607c478bd9Sstevel@tonic-gate 		} else {
4617c478bd9Sstevel@tonic-gate 			t->t_bound_cpu = cpu[binding];
4627c478bd9Sstevel@tonic-gate 			/*
4637c478bd9Sstevel@tonic-gate 			 * Make sure the thread is running on the bound CPU.
4647c478bd9Sstevel@tonic-gate 			 */
4657c478bd9Sstevel@tonic-gate 			if (t->t_cpu != t->t_bound_cpu) {
4667c478bd9Sstevel@tonic-gate 				force_thread_migrate(t);
4677c478bd9Sstevel@tonic-gate 				return;		/* already dropped lock */
4687c478bd9Sstevel@tonic-gate 			}
4697c478bd9Sstevel@tonic-gate 		}
4707c478bd9Sstevel@tonic-gate 	}
4717c478bd9Sstevel@tonic-gate 	thread_unlock(t);
4727c478bd9Sstevel@tonic-gate }
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate /*
4757c478bd9Sstevel@tonic-gate  * Wrapper for backward compatibility.
4767c478bd9Sstevel@tonic-gate  */
4777c478bd9Sstevel@tonic-gate void
4787c478bd9Sstevel@tonic-gate affinity_clear(void)
4797c478bd9Sstevel@tonic-gate {
4807c478bd9Sstevel@tonic-gate 	thread_affinity_clear(curthread);
4817c478bd9Sstevel@tonic-gate }
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate /*
4847c478bd9Sstevel@tonic-gate  * Weak cpu affinity.  Bind to the "current" cpu for short periods
4857c478bd9Sstevel@tonic-gate  * of time during which the thread must not block (but may be preempted).
4867c478bd9Sstevel@tonic-gate  * Use this instead of kpreempt_disable() when it is only "no migration"
4877c478bd9Sstevel@tonic-gate  * rather than "no preemption" semantics that are required - disabling
4887c478bd9Sstevel@tonic-gate  * preemption holds higher priority threads off of cpu and if the
4897c478bd9Sstevel@tonic-gate  * operation that is protected is more than momentary this is not good
4907c478bd9Sstevel@tonic-gate  * for realtime etc.
4917c478bd9Sstevel@tonic-gate  *
4927c478bd9Sstevel@tonic-gate  * Weakly bound threads will not prevent a cpu from being offlined -
4937c478bd9Sstevel@tonic-gate  * we'll only run them on the cpu to which they are weakly bound but
4947c478bd9Sstevel@tonic-gate  * (because they do not block) we'll always be able to move them on to
4957c478bd9Sstevel@tonic-gate  * another cpu at offline time if we give them just a short moment to
4967c478bd9Sstevel@tonic-gate  * run during which they will unbind.  To give a cpu a chance of offlining,
4977c478bd9Sstevel@tonic-gate  * however, we require a barrier to weak bindings that may be raised for a
4987c478bd9Sstevel@tonic-gate  * given cpu (offline/move code may set this and then wait a short time for
4997c478bd9Sstevel@tonic-gate  * existing weak bindings to drop); the cpu_inmotion pointer is that barrier.
5007c478bd9Sstevel@tonic-gate  *
5017c478bd9Sstevel@tonic-gate  * There are few restrictions on the calling context of thread_nomigrate.
5027c478bd9Sstevel@tonic-gate  * The caller must not hold the thread lock.  Calls may be nested.
5037c478bd9Sstevel@tonic-gate  *
5047c478bd9Sstevel@tonic-gate  * After weakbinding a thread must not perform actions that may block.
5057c478bd9Sstevel@tonic-gate  * In particular it must not call thread_affinity_set; calling that when
5067c478bd9Sstevel@tonic-gate  * already weakbound is nonsensical anyway.
5077c478bd9Sstevel@tonic-gate  *
5087c478bd9Sstevel@tonic-gate  * If curthread is prevented from migrating for other reasons
5097c478bd9Sstevel@tonic-gate  * (kernel preemption disabled; high pil; strongly bound; interrupt thread)
5107c478bd9Sstevel@tonic-gate  * then the weak binding will succeed even if this cpu is the target of an
5117c478bd9Sstevel@tonic-gate  * offline/move request.
5127c478bd9Sstevel@tonic-gate  */
5137c478bd9Sstevel@tonic-gate void
5147c478bd9Sstevel@tonic-gate thread_nomigrate(void)
5157c478bd9Sstevel@tonic-gate {
5167c478bd9Sstevel@tonic-gate 	cpu_t *cp;
5177c478bd9Sstevel@tonic-gate 	kthread_id_t t = curthread;
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate again:
5207c478bd9Sstevel@tonic-gate 	kpreempt_disable();
5217c478bd9Sstevel@tonic-gate 	cp = CPU;
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	/*
5247c478bd9Sstevel@tonic-gate 	 * A highlevel interrupt must not modify t_nomigrate or
5257c478bd9Sstevel@tonic-gate 	 * t_weakbound_cpu of the thread it has interrupted.  A lowlevel
5267c478bd9Sstevel@tonic-gate 	 * interrupt thread cannot migrate and we can avoid the
5277c478bd9Sstevel@tonic-gate 	 * thread_lock call below by short-circuiting here.  In either
5287c478bd9Sstevel@tonic-gate 	 * case we can just return since no migration is possible and
5297c478bd9Sstevel@tonic-gate 	 * the condition will persist (ie, when we test for these again
5307c478bd9Sstevel@tonic-gate 	 * in thread_allowmigrate they can't have changed).   Migration
5317c478bd9Sstevel@tonic-gate 	 * is also impossible if we're at or above DISP_LEVEL pil.
5327c478bd9Sstevel@tonic-gate 	 */
5337c478bd9Sstevel@tonic-gate 	if (CPU_ON_INTR(cp) || t->t_flag & T_INTR_THREAD ||
5347c478bd9Sstevel@tonic-gate 	    getpil() >= DISP_LEVEL) {
5357c478bd9Sstevel@tonic-gate 		kpreempt_enable();
5367c478bd9Sstevel@tonic-gate 		return;
5377c478bd9Sstevel@tonic-gate 	}
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	/*
5407c478bd9Sstevel@tonic-gate 	 * We must be consistent with existing weak bindings.  Since we
5417c478bd9Sstevel@tonic-gate 	 * may be interrupted between the increment of t_nomigrate and
5427c478bd9Sstevel@tonic-gate 	 * the store to t_weakbound_cpu below we cannot assume that
5437c478bd9Sstevel@tonic-gate 	 * t_weakbound_cpu will be set if t_nomigrate is.  Note that we
5447c478bd9Sstevel@tonic-gate 	 * cannot assert t_weakbound_cpu == t_bind_cpu since that is not
5457c478bd9Sstevel@tonic-gate 	 * always the case.
5467c478bd9Sstevel@tonic-gate 	 */
5477c478bd9Sstevel@tonic-gate 	if (t->t_nomigrate && t->t_weakbound_cpu && t->t_weakbound_cpu != cp) {
5487c478bd9Sstevel@tonic-gate 		if (!panicstr)
5497c478bd9Sstevel@tonic-gate 			panic("thread_nomigrate: binding to %p but already "
5507c478bd9Sstevel@tonic-gate 			    "bound to %p", (void *)cp,
5517c478bd9Sstevel@tonic-gate 			    (void *)t->t_weakbound_cpu);
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	/*
5557c478bd9Sstevel@tonic-gate 	 * At this point we have preemption disabled and we don't yet hold
5567c478bd9Sstevel@tonic-gate 	 * the thread lock.  So it's possible that somebody else could
5577c478bd9Sstevel@tonic-gate 	 * set t_bind_cpu here and not be able to force us across to the
5587c478bd9Sstevel@tonic-gate 	 * new cpu (since we have preemption disabled).
5597c478bd9Sstevel@tonic-gate 	 */
5607c478bd9Sstevel@tonic-gate 	thread_lock(curthread);
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	/*
5637c478bd9Sstevel@tonic-gate 	 * If further weak bindings are being (temporarily) suppressed then
5647c478bd9Sstevel@tonic-gate 	 * we'll settle for disabling kernel preemption (which assures
5657c478bd9Sstevel@tonic-gate 	 * no migration provided the thread does not block which it is
5667c478bd9Sstevel@tonic-gate 	 * not allowed to if using thread_nomigrate).  We must remember
5677c478bd9Sstevel@tonic-gate 	 * this disposition so we can take appropriate action in
5687c478bd9Sstevel@tonic-gate 	 * thread_allowmigrate.  If this is a nested call and the
5697c478bd9Sstevel@tonic-gate 	 * thread is already weakbound then fall through as normal.
5707c478bd9Sstevel@tonic-gate 	 * We remember the decision to settle for kpreempt_disable through
5717c478bd9Sstevel@tonic-gate 	 * negative nesting counting in t_nomigrate.  Once a thread has had one
5727c478bd9Sstevel@tonic-gate 	 * weakbinding request satisfied in this way any further (nested)
5737c478bd9Sstevel@tonic-gate 	 * requests will continue to be satisfied in the same way,
5747c478bd9Sstevel@tonic-gate 	 * even if weak bindings have recommenced.
5757c478bd9Sstevel@tonic-gate 	 */
5767c478bd9Sstevel@tonic-gate 	if (t->t_nomigrate < 0 || weakbindingbarrier && t->t_nomigrate == 0) {
5777c478bd9Sstevel@tonic-gate 		--t->t_nomigrate;
5787c478bd9Sstevel@tonic-gate 		thread_unlock(curthread);
5797c478bd9Sstevel@tonic-gate 		return;		/* with kpreempt_disable still active */
5807c478bd9Sstevel@tonic-gate 	}
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	/*
5837c478bd9Sstevel@tonic-gate 	 * We hold thread_lock so t_bind_cpu cannot change.  We could,
5847c478bd9Sstevel@tonic-gate 	 * however, be running on a different cpu to which we are t_bound_cpu
5857c478bd9Sstevel@tonic-gate 	 * to (as explained above).  If we grant the weak binding request
5867c478bd9Sstevel@tonic-gate 	 * in that case then the dispatcher must favour our weak binding
5877c478bd9Sstevel@tonic-gate 	 * over our strong (in which case, just as when preemption is
5887c478bd9Sstevel@tonic-gate 	 * disabled, we can continue to run on a cpu other than the one to
5897c478bd9Sstevel@tonic-gate 	 * which we are strongbound; the difference in this case is that
5907c478bd9Sstevel@tonic-gate 	 * this thread can be preempted and so can appear on the dispatch
5917c478bd9Sstevel@tonic-gate 	 * queues of a cpu other than the one it is strongbound to).
5927c478bd9Sstevel@tonic-gate 	 *
5937c478bd9Sstevel@tonic-gate 	 * If the cpu we are running on does not appear to be a current
5947c478bd9Sstevel@tonic-gate 	 * offline target (we check cpu_inmotion to determine this - since
5957c478bd9Sstevel@tonic-gate 	 * we don't hold cpu_lock we may not see a recent store to that,
5967c478bd9Sstevel@tonic-gate 	 * so it's possible that we at times can grant a weak binding to a
5977c478bd9Sstevel@tonic-gate 	 * cpu that is an offline target, but that one request will not
5987c478bd9Sstevel@tonic-gate 	 * prevent the offline from succeeding) then we will always grant
5997c478bd9Sstevel@tonic-gate 	 * the weak binding request.  This includes the case above where
6007c478bd9Sstevel@tonic-gate 	 * we grant a weakbinding not commensurate with our strong binding.
6017c478bd9Sstevel@tonic-gate 	 *
6027c478bd9Sstevel@tonic-gate 	 * If our cpu does appear to be an offline target then we're inclined
6037c478bd9Sstevel@tonic-gate 	 * not to grant the weakbinding request just yet - we'd prefer to
6047c478bd9Sstevel@tonic-gate 	 * migrate to another cpu and grant the request there.  The
6057c478bd9Sstevel@tonic-gate 	 * exceptions are those cases where going through preemption code
6067c478bd9Sstevel@tonic-gate 	 * will not result in us changing cpu:
6077c478bd9Sstevel@tonic-gate 	 *
6087c478bd9Sstevel@tonic-gate 	 *	. interrupts have already bypassed this case (see above)
6097c478bd9Sstevel@tonic-gate 	 *	. we are already weakbound to this cpu (dispatcher code will
6107c478bd9Sstevel@tonic-gate 	 *	  always return us to the weakbound cpu)
6117c478bd9Sstevel@tonic-gate 	 *	. preemption was disabled even before we disabled it above
6127c478bd9Sstevel@tonic-gate 	 *	. we are strongbound to this cpu (if we're strongbound to
6137c478bd9Sstevel@tonic-gate 	 *	another and not yet running there the trip through the
6147c478bd9Sstevel@tonic-gate 	 *	dispatcher will move us to the strongbound cpu and we
6157c478bd9Sstevel@tonic-gate 	 *	will grant the weak binding there)
6167c478bd9Sstevel@tonic-gate 	 */
6177c478bd9Sstevel@tonic-gate 	if (cp != cpu_inmotion || t->t_nomigrate > 0 || t->t_preempt > 1 ||
6187c478bd9Sstevel@tonic-gate 	    t->t_bound_cpu == cp) {
6197c478bd9Sstevel@tonic-gate 		/*
6207c478bd9Sstevel@tonic-gate 		 * Don't be tempted to store to t_weakbound_cpu only on
6217c478bd9Sstevel@tonic-gate 		 * the first nested bind request - if we're interrupted
6227c478bd9Sstevel@tonic-gate 		 * after the increment of t_nomigrate and before the
6237c478bd9Sstevel@tonic-gate 		 * store to t_weakbound_cpu and the interrupt calls
6247c478bd9Sstevel@tonic-gate 		 * thread_nomigrate then the assertion in thread_allowmigrate
6257c478bd9Sstevel@tonic-gate 		 * would fail.
6267c478bd9Sstevel@tonic-gate 		 */
6277c478bd9Sstevel@tonic-gate 		t->t_nomigrate++;
6287c478bd9Sstevel@tonic-gate 		t->t_weakbound_cpu = cp;
6297c478bd9Sstevel@tonic-gate 		membar_producer();
6307c478bd9Sstevel@tonic-gate 		thread_unlock(curthread);
6317c478bd9Sstevel@tonic-gate 		/*
6327c478bd9Sstevel@tonic-gate 		 * Now that we have dropped the thread_lock another thread
6337c478bd9Sstevel@tonic-gate 		 * can set our t_weakbound_cpu, and will try to migrate us
6347c478bd9Sstevel@tonic-gate 		 * to the strongbound cpu (which will not be prevented by
6357c478bd9Sstevel@tonic-gate 		 * preemption being disabled since we're about to enable
6367c478bd9Sstevel@tonic-gate 		 * preemption).  We have granted the weakbinding to the current
6377c478bd9Sstevel@tonic-gate 		 * cpu, so again we are in the position that is is is possible
6387c478bd9Sstevel@tonic-gate 		 * that our weak and strong bindings differ.  Again this
6397c478bd9Sstevel@tonic-gate 		 * is catered for by dispatcher code which will favour our
6407c478bd9Sstevel@tonic-gate 		 * weak binding.
6417c478bd9Sstevel@tonic-gate 		 */
6427c478bd9Sstevel@tonic-gate 		kpreempt_enable();
6437c478bd9Sstevel@tonic-gate 	} else {
6447c478bd9Sstevel@tonic-gate 		/*
6457c478bd9Sstevel@tonic-gate 		 * Move to another cpu before granting the request by
6467c478bd9Sstevel@tonic-gate 		 * forcing this thread through preemption code.  When we
6477c478bd9Sstevel@tonic-gate 		 * get to set{front,back}dq called from CL_PREEMPT()
6487c478bd9Sstevel@tonic-gate 		 * cpu_choose() will be used to select a cpu to queue
6497c478bd9Sstevel@tonic-gate 		 * us on - that will see cpu_inmotion and take
6507c478bd9Sstevel@tonic-gate 		 * steps to avoid returning us to this cpu.
6517c478bd9Sstevel@tonic-gate 		 */
6527c478bd9Sstevel@tonic-gate 		cp->cpu_kprunrun = 1;
6537c478bd9Sstevel@tonic-gate 		thread_unlock(curthread);
6547c478bd9Sstevel@tonic-gate 		kpreempt_enable();	/* will call preempt() */
6557c478bd9Sstevel@tonic-gate 		goto again;
6567c478bd9Sstevel@tonic-gate 	}
6577c478bd9Sstevel@tonic-gate }
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate void
6607c478bd9Sstevel@tonic-gate thread_allowmigrate(void)
6617c478bd9Sstevel@tonic-gate {
6627c478bd9Sstevel@tonic-gate 	kthread_id_t t = curthread;
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	ASSERT(t->t_weakbound_cpu == CPU ||
6657c478bd9Sstevel@tonic-gate 	    (t->t_nomigrate < 0 && t->t_preempt > 0) ||
6667c478bd9Sstevel@tonic-gate 	    CPU_ON_INTR(CPU) || t->t_flag & T_INTR_THREAD ||
6677c478bd9Sstevel@tonic-gate 	    getpil() >= DISP_LEVEL);
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	if (CPU_ON_INTR(CPU) || (t->t_flag & T_INTR_THREAD) ||
6707c478bd9Sstevel@tonic-gate 	    getpil() >= DISP_LEVEL)
6717c478bd9Sstevel@tonic-gate 		return;
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	if (t->t_nomigrate < 0) {
6747c478bd9Sstevel@tonic-gate 		/*
6757c478bd9Sstevel@tonic-gate 		 * This thread was granted "weak binding" in the
6767c478bd9Sstevel@tonic-gate 		 * stronger form of kernel preemption disabling.
6777c478bd9Sstevel@tonic-gate 		 * Undo a level of nesting for both t_nomigrate
6787c478bd9Sstevel@tonic-gate 		 * and t_preempt.
6797c478bd9Sstevel@tonic-gate 		 */
6807c478bd9Sstevel@tonic-gate 		++t->t_nomigrate;
6817c478bd9Sstevel@tonic-gate 		kpreempt_enable();
6827c478bd9Sstevel@tonic-gate 	} else if (--t->t_nomigrate == 0) {
6837c478bd9Sstevel@tonic-gate 		/*
6847c478bd9Sstevel@tonic-gate 		 * Time to drop the weak binding.  We need to cater
6857c478bd9Sstevel@tonic-gate 		 * for the case where we're weakbound to a different
6867c478bd9Sstevel@tonic-gate 		 * cpu than that to which we're strongbound (a very
6877c478bd9Sstevel@tonic-gate 		 * temporary arrangement that must only persist until
6887c478bd9Sstevel@tonic-gate 		 * weak binding drops).  We don't acquire thread_lock
6897c478bd9Sstevel@tonic-gate 		 * here so even as this code executes t_bound_cpu
6907c478bd9Sstevel@tonic-gate 		 * may be changing.  So we disable preemption and
6917c478bd9Sstevel@tonic-gate 		 * a) in the case that t_bound_cpu changes while we
6927c478bd9Sstevel@tonic-gate 		 * have preemption disabled kprunrun will be set
6937c478bd9Sstevel@tonic-gate 		 * asynchronously, and b) if before disabling
6947c478bd9Sstevel@tonic-gate 		 * preemption we were already on a different cpu to
6957c478bd9Sstevel@tonic-gate 		 * our t_bound_cpu then we set kprunrun ourselves
6967c478bd9Sstevel@tonic-gate 		 * to force a trip through the dispatcher when
6977c478bd9Sstevel@tonic-gate 		 * preemption is enabled.
6987c478bd9Sstevel@tonic-gate 		 */
6997c478bd9Sstevel@tonic-gate 		kpreempt_disable();
7007c478bd9Sstevel@tonic-gate 		if (t->t_bound_cpu &&
7017c478bd9Sstevel@tonic-gate 		    t->t_weakbound_cpu != t->t_bound_cpu)
7027c478bd9Sstevel@tonic-gate 			CPU->cpu_kprunrun = 1;
7037c478bd9Sstevel@tonic-gate 		t->t_weakbound_cpu = NULL;
7047c478bd9Sstevel@tonic-gate 		membar_producer();
7057c478bd9Sstevel@tonic-gate 		kpreempt_enable();
7067c478bd9Sstevel@tonic-gate 	}
7077c478bd9Sstevel@tonic-gate }
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate /*
7107c478bd9Sstevel@tonic-gate  * weakbinding_stop can be used to temporarily cause weakbindings made
7117c478bd9Sstevel@tonic-gate  * with thread_nomigrate to be satisfied through the stronger action of
7127c478bd9Sstevel@tonic-gate  * kpreempt_disable.  weakbinding_start recommences normal weakbinding.
7137c478bd9Sstevel@tonic-gate  */
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate void
7167c478bd9Sstevel@tonic-gate weakbinding_stop(void)
7177c478bd9Sstevel@tonic-gate {
7187c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
7197c478bd9Sstevel@tonic-gate 	weakbindingbarrier = 1;
7207c478bd9Sstevel@tonic-gate 	membar_producer();	/* make visible before subsequent thread_lock */
7217c478bd9Sstevel@tonic-gate }
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate void
7247c478bd9Sstevel@tonic-gate weakbinding_start(void)
7257c478bd9Sstevel@tonic-gate {
7267c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
7277c478bd9Sstevel@tonic-gate 	weakbindingbarrier = 0;
7287c478bd9Sstevel@tonic-gate }
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate /*
7317c478bd9Sstevel@tonic-gate  * This routine is called to place the CPUs in a safe place so that
7327c478bd9Sstevel@tonic-gate  * one of them can be taken off line or placed on line.  What we are
7337c478bd9Sstevel@tonic-gate  * trying to do here is prevent a thread from traversing the list
7347c478bd9Sstevel@tonic-gate  * of active CPUs while we are changing it or from getting placed on
7357c478bd9Sstevel@tonic-gate  * the run queue of a CPU that has just gone off line.  We do this by
7367c478bd9Sstevel@tonic-gate  * creating a thread with the highest possible prio for each CPU and
7377c478bd9Sstevel@tonic-gate  * having it call this routine.  The advantage of this method is that
7387c478bd9Sstevel@tonic-gate  * we can eliminate all checks for CPU_ACTIVE in the disp routines.
7397c478bd9Sstevel@tonic-gate  * This makes disp faster at the expense of making p_online() slower
7407c478bd9Sstevel@tonic-gate  * which is a good trade off.
7417c478bd9Sstevel@tonic-gate  */
7427c478bd9Sstevel@tonic-gate static void
7437c478bd9Sstevel@tonic-gate cpu_pause(volatile char *safe)
7447c478bd9Sstevel@tonic-gate {
7457c478bd9Sstevel@tonic-gate 	int s;
7467c478bd9Sstevel@tonic-gate 	struct _cpu_pause_info *cpi = &cpu_pause_info;
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	ASSERT((curthread->t_bound_cpu != NULL) || (*safe == PAUSE_DIE));
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	while (*safe != PAUSE_DIE) {
7517c478bd9Sstevel@tonic-gate 		*safe = PAUSE_READY;
7527c478bd9Sstevel@tonic-gate 		membar_enter();		/* make sure stores are flushed */
7537c478bd9Sstevel@tonic-gate 		sema_v(&cpi->cp_sem);	/* signal requesting thread */
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 		/*
7567c478bd9Sstevel@tonic-gate 		 * Wait here until all pause threads are running.  That
7577c478bd9Sstevel@tonic-gate 		 * indicates that it's safe to do the spl.  Until
7587c478bd9Sstevel@tonic-gate 		 * cpu_pause_info.cp_go is set, we don't want to spl
7597c478bd9Sstevel@tonic-gate 		 * because that might block clock interrupts needed
7607c478bd9Sstevel@tonic-gate 		 * to preempt threads on other CPUs.
7617c478bd9Sstevel@tonic-gate 		 */
7627c478bd9Sstevel@tonic-gate 		while (cpi->cp_go == 0)
7637c478bd9Sstevel@tonic-gate 			;
7647c478bd9Sstevel@tonic-gate 		/*
7657c478bd9Sstevel@tonic-gate 		 * Even though we are at the highest disp prio, we need
7667c478bd9Sstevel@tonic-gate 		 * to block out all interrupts below LOCK_LEVEL so that
7677c478bd9Sstevel@tonic-gate 		 * an intr doesn't come in, wake up a thread, and call
7687c478bd9Sstevel@tonic-gate 		 * setbackdq/setfrontdq.
7697c478bd9Sstevel@tonic-gate 		 */
7707c478bd9Sstevel@tonic-gate 		s = splhigh();
7717c478bd9Sstevel@tonic-gate 		/*
7727c478bd9Sstevel@tonic-gate 		 * This cpu is now safe.
7737c478bd9Sstevel@tonic-gate 		 */
7747c478bd9Sstevel@tonic-gate 		*safe = PAUSE_WAIT;
7757c478bd9Sstevel@tonic-gate 		membar_enter();		/* make sure stores are flushed */
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 		/*
7787c478bd9Sstevel@tonic-gate 		 * Now we wait.  When we are allowed to continue, safe will
7797c478bd9Sstevel@tonic-gate 		 * be set to PAUSE_IDLE.
7807c478bd9Sstevel@tonic-gate 		 */
7817c478bd9Sstevel@tonic-gate 		while (*safe != PAUSE_IDLE)
7827c478bd9Sstevel@tonic-gate 			;
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 		splx(s);
7857c478bd9Sstevel@tonic-gate 		/*
7867c478bd9Sstevel@tonic-gate 		 * Waiting is at an end. Switch out of cpu_pause
7877c478bd9Sstevel@tonic-gate 		 * loop and resume useful work.
7887c478bd9Sstevel@tonic-gate 		 */
7897c478bd9Sstevel@tonic-gate 		swtch();
7907c478bd9Sstevel@tonic-gate 	}
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	mutex_enter(&pause_free_mutex);
7937c478bd9Sstevel@tonic-gate 	*safe = PAUSE_DEAD;
7947c478bd9Sstevel@tonic-gate 	cv_broadcast(&pause_free_cv);
7957c478bd9Sstevel@tonic-gate 	mutex_exit(&pause_free_mutex);
7967c478bd9Sstevel@tonic-gate }
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate /*
7997c478bd9Sstevel@tonic-gate  * Allow the cpus to start running again.
8007c478bd9Sstevel@tonic-gate  */
8017c478bd9Sstevel@tonic-gate void
8027c478bd9Sstevel@tonic-gate start_cpus()
8037c478bd9Sstevel@tonic-gate {
8047c478bd9Sstevel@tonic-gate 	int i;
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
8077c478bd9Sstevel@tonic-gate 	ASSERT(cpu_pause_info.cp_paused);
8087c478bd9Sstevel@tonic-gate 	cpu_pause_info.cp_paused = NULL;
8097c478bd9Sstevel@tonic-gate 	for (i = 0; i < NCPU; i++)
8107c478bd9Sstevel@tonic-gate 		safe_list[i] = PAUSE_IDLE;
8117c478bd9Sstevel@tonic-gate 	membar_enter();			/* make sure stores are flushed */
8127c478bd9Sstevel@tonic-gate 	affinity_clear();
8137c478bd9Sstevel@tonic-gate 	splx(cpu_pause_info.cp_spl);
8147c478bd9Sstevel@tonic-gate 	kpreempt_enable();
8157c478bd9Sstevel@tonic-gate }
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate /*
8187c478bd9Sstevel@tonic-gate  * Allocate a pause thread for a CPU.
8197c478bd9Sstevel@tonic-gate  */
8207c478bd9Sstevel@tonic-gate static void
8217c478bd9Sstevel@tonic-gate cpu_pause_alloc(cpu_t *cp)
8227c478bd9Sstevel@tonic-gate {
8237c478bd9Sstevel@tonic-gate 	kthread_id_t	t;
8247c478bd9Sstevel@tonic-gate 	int		cpun = cp->cpu_id;
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 	/*
8277c478bd9Sstevel@tonic-gate 	 * Note, v.v_nglobpris will not change value as long as I hold
8287c478bd9Sstevel@tonic-gate 	 * cpu_lock.
8297c478bd9Sstevel@tonic-gate 	 */
8307c478bd9Sstevel@tonic-gate 	t = thread_create(NULL, 0, cpu_pause, (caddr_t)&safe_list[cpun],
8317c478bd9Sstevel@tonic-gate 	    0, &p0, TS_STOPPED, v.v_nglobpris - 1);
8327c478bd9Sstevel@tonic-gate 	thread_lock(t);
8337c478bd9Sstevel@tonic-gate 	t->t_bound_cpu = cp;
8347c478bd9Sstevel@tonic-gate 	t->t_disp_queue = cp->cpu_disp;
8357c478bd9Sstevel@tonic-gate 	t->t_affinitycnt = 1;
8367c478bd9Sstevel@tonic-gate 	t->t_preempt = 1;
8377c478bd9Sstevel@tonic-gate 	thread_unlock(t);
8387c478bd9Sstevel@tonic-gate 	cp->cpu_pause_thread = t;
8397c478bd9Sstevel@tonic-gate 	/*
8407c478bd9Sstevel@tonic-gate 	 * Registering a thread in the callback table is usually done
8417c478bd9Sstevel@tonic-gate 	 * in the initialization code of the thread.  In this
8427c478bd9Sstevel@tonic-gate 	 * case, we do it right after thread creation because the
8437c478bd9Sstevel@tonic-gate 	 * thread itself may never run, and we need to register the
8447c478bd9Sstevel@tonic-gate 	 * fact that it is safe for cpr suspend.
8457c478bd9Sstevel@tonic-gate 	 */
8467c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT_SAFE(t, "cpu_pause");
8477c478bd9Sstevel@tonic-gate }
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate /*
8507c478bd9Sstevel@tonic-gate  * Free a pause thread for a CPU.
8517c478bd9Sstevel@tonic-gate  */
8527c478bd9Sstevel@tonic-gate static void
8537c478bd9Sstevel@tonic-gate cpu_pause_free(cpu_t *cp)
8547c478bd9Sstevel@tonic-gate {
8557c478bd9Sstevel@tonic-gate 	kthread_id_t	t;
8567c478bd9Sstevel@tonic-gate 	int		cpun = cp->cpu_id;
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
8597c478bd9Sstevel@tonic-gate 	/*
8607c478bd9Sstevel@tonic-gate 	 * We have to get the thread and tell him to die.
8617c478bd9Sstevel@tonic-gate 	 */
8627c478bd9Sstevel@tonic-gate 	if ((t = cp->cpu_pause_thread) == NULL) {
8637c478bd9Sstevel@tonic-gate 		ASSERT(safe_list[cpun] == PAUSE_IDLE);
8647c478bd9Sstevel@tonic-gate 		return;
8657c478bd9Sstevel@tonic-gate 	}
8667c478bd9Sstevel@tonic-gate 	thread_lock(t);
8677c478bd9Sstevel@tonic-gate 	t->t_cpu = CPU;		/* disp gets upset if last cpu is quiesced. */
8687c478bd9Sstevel@tonic-gate 	t->t_bound_cpu = NULL;	/* Must un-bind; cpu may not be running. */
8697c478bd9Sstevel@tonic-gate 	t->t_pri = v.v_nglobpris - 1;
8707c478bd9Sstevel@tonic-gate 	ASSERT(safe_list[cpun] == PAUSE_IDLE);
8717c478bd9Sstevel@tonic-gate 	safe_list[cpun] = PAUSE_DIE;
8727c478bd9Sstevel@tonic-gate 	THREAD_TRANSITION(t);
8737c478bd9Sstevel@tonic-gate 	setbackdq(t);
8747c478bd9Sstevel@tonic-gate 	thread_unlock_nopreempt(t);
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 	/*
8777c478bd9Sstevel@tonic-gate 	 * If we don't wait for the thread to actually die, it may try to
8787c478bd9Sstevel@tonic-gate 	 * run on the wrong cpu as part of an actual call to pause_cpus().
8797c478bd9Sstevel@tonic-gate 	 */
8807c478bd9Sstevel@tonic-gate 	mutex_enter(&pause_free_mutex);
8817c478bd9Sstevel@tonic-gate 	while (safe_list[cpun] != PAUSE_DEAD) {
8827c478bd9Sstevel@tonic-gate 		cv_wait(&pause_free_cv, &pause_free_mutex);
8837c478bd9Sstevel@tonic-gate 	}
8847c478bd9Sstevel@tonic-gate 	mutex_exit(&pause_free_mutex);
8857c478bd9Sstevel@tonic-gate 	safe_list[cpun] = PAUSE_IDLE;
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	cp->cpu_pause_thread = NULL;
8887c478bd9Sstevel@tonic-gate }
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate /*
8917c478bd9Sstevel@tonic-gate  * Initialize basic structures for pausing CPUs.
8927c478bd9Sstevel@tonic-gate  */
8937c478bd9Sstevel@tonic-gate void
8947c478bd9Sstevel@tonic-gate cpu_pause_init()
8957c478bd9Sstevel@tonic-gate {
8967c478bd9Sstevel@tonic-gate 	sema_init(&cpu_pause_info.cp_sem, 0, NULL, SEMA_DEFAULT, NULL);
8977c478bd9Sstevel@tonic-gate 	/*
8987c478bd9Sstevel@tonic-gate 	 * Create initial CPU pause thread.
8997c478bd9Sstevel@tonic-gate 	 */
9007c478bd9Sstevel@tonic-gate 	cpu_pause_alloc(CPU);
9017c478bd9Sstevel@tonic-gate }
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate /*
9047c478bd9Sstevel@tonic-gate  * Start the threads used to pause another CPU.
9057c478bd9Sstevel@tonic-gate  */
9067c478bd9Sstevel@tonic-gate static int
9077c478bd9Sstevel@tonic-gate cpu_pause_start(processorid_t cpu_id)
9087c478bd9Sstevel@tonic-gate {
9097c478bd9Sstevel@tonic-gate 	int	i;
9107c478bd9Sstevel@tonic-gate 	int	cpu_count = 0;
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	for (i = 0; i < NCPU; i++) {
9137c478bd9Sstevel@tonic-gate 		cpu_t		*cp;
9147c478bd9Sstevel@tonic-gate 		kthread_id_t	t;
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 		cp = cpu[i];
9177c478bd9Sstevel@tonic-gate 		if (!CPU_IN_SET(cpu_available, i) || (i == cpu_id)) {
9187c478bd9Sstevel@tonic-gate 			safe_list[i] = PAUSE_WAIT;
9197c478bd9Sstevel@tonic-gate 			continue;
9207c478bd9Sstevel@tonic-gate 		}
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 		/*
9237c478bd9Sstevel@tonic-gate 		 * Skip CPU if it is quiesced or not yet started.
9247c478bd9Sstevel@tonic-gate 		 */
9257c478bd9Sstevel@tonic-gate 		if ((cp->cpu_flags & (CPU_QUIESCED | CPU_READY)) != CPU_READY) {
9267c478bd9Sstevel@tonic-gate 			safe_list[i] = PAUSE_WAIT;
9277c478bd9Sstevel@tonic-gate 			continue;
9287c478bd9Sstevel@tonic-gate 		}
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 		/*
9317c478bd9Sstevel@tonic-gate 		 * Start this CPU's pause thread.
9327c478bd9Sstevel@tonic-gate 		 */
9337c478bd9Sstevel@tonic-gate 		t = cp->cpu_pause_thread;
9347c478bd9Sstevel@tonic-gate 		thread_lock(t);
9357c478bd9Sstevel@tonic-gate 		/*
9367c478bd9Sstevel@tonic-gate 		 * Reset the priority, since nglobpris may have
9377c478bd9Sstevel@tonic-gate 		 * changed since the thread was created, if someone
9387c478bd9Sstevel@tonic-gate 		 * has loaded the RT (or some other) scheduling
9397c478bd9Sstevel@tonic-gate 		 * class.
9407c478bd9Sstevel@tonic-gate 		 */
9417c478bd9Sstevel@tonic-gate 		t->t_pri = v.v_nglobpris - 1;
9427c478bd9Sstevel@tonic-gate 		THREAD_TRANSITION(t);
9437c478bd9Sstevel@tonic-gate 		setbackdq(t);
9447c478bd9Sstevel@tonic-gate 		thread_unlock_nopreempt(t);
9457c478bd9Sstevel@tonic-gate 		++cpu_count;
9467c478bd9Sstevel@tonic-gate 	}
9477c478bd9Sstevel@tonic-gate 	return (cpu_count);
9487c478bd9Sstevel@tonic-gate }
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate /*
9527c478bd9Sstevel@tonic-gate  * Pause all of the CPUs except the one we are on by creating a high
9537c478bd9Sstevel@tonic-gate  * priority thread bound to those CPUs.
9547c478bd9Sstevel@tonic-gate  *
9557c478bd9Sstevel@tonic-gate  * Note that one must be extremely careful regarding code
9567c478bd9Sstevel@tonic-gate  * executed while CPUs are paused.  Since a CPU may be paused
9577c478bd9Sstevel@tonic-gate  * while a thread scheduling on that CPU is holding an adaptive
9587c478bd9Sstevel@tonic-gate  * lock, code executed with CPUs paused must not acquire adaptive
9597c478bd9Sstevel@tonic-gate  * (or low-level spin) locks.  Also, such code must not block,
9607c478bd9Sstevel@tonic-gate  * since the thread that is supposed to initiate the wakeup may
9617c478bd9Sstevel@tonic-gate  * never run.
9627c478bd9Sstevel@tonic-gate  *
9637c478bd9Sstevel@tonic-gate  * With a few exceptions, the restrictions on code executed with CPUs
9647c478bd9Sstevel@tonic-gate  * paused match those for code executed at high-level interrupt
9657c478bd9Sstevel@tonic-gate  * context.
9667c478bd9Sstevel@tonic-gate  */
9677c478bd9Sstevel@tonic-gate void
9687c478bd9Sstevel@tonic-gate pause_cpus(cpu_t *off_cp)
9697c478bd9Sstevel@tonic-gate {
9707c478bd9Sstevel@tonic-gate 	processorid_t	cpu_id;
9717c478bd9Sstevel@tonic-gate 	int		i;
9727c478bd9Sstevel@tonic-gate 	struct _cpu_pause_info	*cpi = &cpu_pause_info;
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
9757c478bd9Sstevel@tonic-gate 	ASSERT(cpi->cp_paused == NULL);
9767c478bd9Sstevel@tonic-gate 	cpi->cp_count = 0;
9777c478bd9Sstevel@tonic-gate 	cpi->cp_go = 0;
9787c478bd9Sstevel@tonic-gate 	for (i = 0; i < NCPU; i++)
9797c478bd9Sstevel@tonic-gate 		safe_list[i] = PAUSE_IDLE;
9807c478bd9Sstevel@tonic-gate 	kpreempt_disable();
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 	/*
9837c478bd9Sstevel@tonic-gate 	 * If running on the cpu that is going offline, get off it.
9847c478bd9Sstevel@tonic-gate 	 * This is so that it won't be necessary to rechoose a CPU
9857c478bd9Sstevel@tonic-gate 	 * when done.
9867c478bd9Sstevel@tonic-gate 	 */
9877c478bd9Sstevel@tonic-gate 	if (CPU == off_cp)
9887c478bd9Sstevel@tonic-gate 		cpu_id = off_cp->cpu_next_part->cpu_id;
9897c478bd9Sstevel@tonic-gate 	else
9907c478bd9Sstevel@tonic-gate 		cpu_id = CPU->cpu_id;
9917c478bd9Sstevel@tonic-gate 	affinity_set(cpu_id);
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	/*
9947c478bd9Sstevel@tonic-gate 	 * Start the pause threads and record how many were started
9957c478bd9Sstevel@tonic-gate 	 */
9967c478bd9Sstevel@tonic-gate 	cpi->cp_count = cpu_pause_start(cpu_id);
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 	/*
9997c478bd9Sstevel@tonic-gate 	 * Now wait for all CPUs to be running the pause thread.
10007c478bd9Sstevel@tonic-gate 	 */
10017c478bd9Sstevel@tonic-gate 	while (cpi->cp_count > 0) {
10027c478bd9Sstevel@tonic-gate 		/*
10037c478bd9Sstevel@tonic-gate 		 * Spin reading the count without grabbing the disp
10047c478bd9Sstevel@tonic-gate 		 * lock to make sure we don't prevent the pause
10057c478bd9Sstevel@tonic-gate 		 * threads from getting the lock.
10067c478bd9Sstevel@tonic-gate 		 */
10077c478bd9Sstevel@tonic-gate 		while (sema_held(&cpi->cp_sem))
10087c478bd9Sstevel@tonic-gate 			;
10097c478bd9Sstevel@tonic-gate 		if (sema_tryp(&cpi->cp_sem))
10107c478bd9Sstevel@tonic-gate 			--cpi->cp_count;
10117c478bd9Sstevel@tonic-gate 	}
10127c478bd9Sstevel@tonic-gate 	cpi->cp_go = 1;			/* all have reached cpu_pause */
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 	/*
10157c478bd9Sstevel@tonic-gate 	 * Now wait for all CPUs to spl. (Transition from PAUSE_READY
10167c478bd9Sstevel@tonic-gate 	 * to PAUSE_WAIT.)
10177c478bd9Sstevel@tonic-gate 	 */
10187c478bd9Sstevel@tonic-gate 	for (i = 0; i < NCPU; i++) {
10197c478bd9Sstevel@tonic-gate 		while (safe_list[i] != PAUSE_WAIT)
10207c478bd9Sstevel@tonic-gate 			;
10217c478bd9Sstevel@tonic-gate 	}
10227c478bd9Sstevel@tonic-gate 	cpi->cp_spl = splhigh();	/* block dispatcher on this CPU */
10237c478bd9Sstevel@tonic-gate 	cpi->cp_paused = curthread;
10247c478bd9Sstevel@tonic-gate }
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate /*
10277c478bd9Sstevel@tonic-gate  * Check whether the current thread has CPUs paused
10287c478bd9Sstevel@tonic-gate  */
10297c478bd9Sstevel@tonic-gate int
10307c478bd9Sstevel@tonic-gate cpus_paused(void)
10317c478bd9Sstevel@tonic-gate {
10327c478bd9Sstevel@tonic-gate 	if (cpu_pause_info.cp_paused != NULL) {
10337c478bd9Sstevel@tonic-gate 		ASSERT(cpu_pause_info.cp_paused == curthread);
10347c478bd9Sstevel@tonic-gate 		return (1);
10357c478bd9Sstevel@tonic-gate 	}
10367c478bd9Sstevel@tonic-gate 	return (0);
10377c478bd9Sstevel@tonic-gate }
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate static cpu_t *
10407c478bd9Sstevel@tonic-gate cpu_get_all(processorid_t cpun)
10417c478bd9Sstevel@tonic-gate {
10427c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 	if (cpun >= NCPU || cpun < 0 || !CPU_IN_SET(cpu_available, cpun))
10457c478bd9Sstevel@tonic-gate 		return (NULL);
10467c478bd9Sstevel@tonic-gate 	return (cpu[cpun]);
10477c478bd9Sstevel@tonic-gate }
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate /*
10507c478bd9Sstevel@tonic-gate  * Check whether cpun is a valid processor id and whether it should be
10517c478bd9Sstevel@tonic-gate  * visible from the current zone. If it is, return a pointer to the
10527c478bd9Sstevel@tonic-gate  * associated CPU structure.
10537c478bd9Sstevel@tonic-gate  */
10547c478bd9Sstevel@tonic-gate cpu_t *
10557c478bd9Sstevel@tonic-gate cpu_get(processorid_t cpun)
10567c478bd9Sstevel@tonic-gate {
10577c478bd9Sstevel@tonic-gate 	cpu_t *c;
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
10607c478bd9Sstevel@tonic-gate 	c = cpu_get_all(cpun);
10617c478bd9Sstevel@tonic-gate 	if (c != NULL && !INGLOBALZONE(curproc) && pool_pset_enabled() &&
10627c478bd9Sstevel@tonic-gate 	    zone_pset_get(curproc->p_zone) != cpupart_query_cpu(c))
10637c478bd9Sstevel@tonic-gate 		return (NULL);
10647c478bd9Sstevel@tonic-gate 	return (c);
10657c478bd9Sstevel@tonic-gate }
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate /*
10687c478bd9Sstevel@tonic-gate  * The following functions should be used to check CPU states in the kernel.
10697c478bd9Sstevel@tonic-gate  * They should be invoked with cpu_lock held.  Kernel subsystems interested
10707c478bd9Sstevel@tonic-gate  * in CPU states should *not* use cpu_get_state() and various P_ONLINE/etc
10717c478bd9Sstevel@tonic-gate  * states.  Those are for user-land (and system call) use only.
10727c478bd9Sstevel@tonic-gate  */
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate /*
10757c478bd9Sstevel@tonic-gate  * Determine whether the CPU is online and handling interrupts.
10767c478bd9Sstevel@tonic-gate  */
10777c478bd9Sstevel@tonic-gate int
10787c478bd9Sstevel@tonic-gate cpu_is_online(cpu_t *cpu)
10797c478bd9Sstevel@tonic-gate {
10807c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
10817c478bd9Sstevel@tonic-gate 	return (cpu_flagged_online(cpu->cpu_flags));
10827c478bd9Sstevel@tonic-gate }
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate /*
10857c478bd9Sstevel@tonic-gate  * Determine whether the CPU is offline (this includes spare and faulted).
10867c478bd9Sstevel@tonic-gate  */
10877c478bd9Sstevel@tonic-gate int
10887c478bd9Sstevel@tonic-gate cpu_is_offline(cpu_t *cpu)
10897c478bd9Sstevel@tonic-gate {
10907c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
10917c478bd9Sstevel@tonic-gate 	return (cpu_flagged_offline(cpu->cpu_flags));
10927c478bd9Sstevel@tonic-gate }
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate /*
10957c478bd9Sstevel@tonic-gate  * Determine whether the CPU is powered off.
10967c478bd9Sstevel@tonic-gate  */
10977c478bd9Sstevel@tonic-gate int
10987c478bd9Sstevel@tonic-gate cpu_is_poweredoff(cpu_t *cpu)
10997c478bd9Sstevel@tonic-gate {
11007c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
11017c478bd9Sstevel@tonic-gate 	return (cpu_flagged_poweredoff(cpu->cpu_flags));
11027c478bd9Sstevel@tonic-gate }
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate /*
11057c478bd9Sstevel@tonic-gate  * Determine whether the CPU is handling interrupts.
11067c478bd9Sstevel@tonic-gate  */
11077c478bd9Sstevel@tonic-gate int
11087c478bd9Sstevel@tonic-gate cpu_is_nointr(cpu_t *cpu)
11097c478bd9Sstevel@tonic-gate {
11107c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
11117c478bd9Sstevel@tonic-gate 	return (cpu_flagged_nointr(cpu->cpu_flags));
11127c478bd9Sstevel@tonic-gate }
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate /*
11157c478bd9Sstevel@tonic-gate  * Determine whether the CPU is active (scheduling threads).
11167c478bd9Sstevel@tonic-gate  */
11177c478bd9Sstevel@tonic-gate int
11187c478bd9Sstevel@tonic-gate cpu_is_active(cpu_t *cpu)
11197c478bd9Sstevel@tonic-gate {
11207c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
11217c478bd9Sstevel@tonic-gate 	return (cpu_flagged_active(cpu->cpu_flags));
11227c478bd9Sstevel@tonic-gate }
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate /*
11257c478bd9Sstevel@tonic-gate  * Same as above, but these require cpu_flags instead of cpu_t pointers.
11267c478bd9Sstevel@tonic-gate  */
11277c478bd9Sstevel@tonic-gate int
11287c478bd9Sstevel@tonic-gate cpu_flagged_online(cpu_flag_t cpu_flags)
11297c478bd9Sstevel@tonic-gate {
11307c478bd9Sstevel@tonic-gate 	return (cpu_flagged_active(cpu_flags) &&
11317c478bd9Sstevel@tonic-gate 	    (cpu_flags & CPU_ENABLE));
11327c478bd9Sstevel@tonic-gate }
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate int
11357c478bd9Sstevel@tonic-gate cpu_flagged_offline(cpu_flag_t cpu_flags)
11367c478bd9Sstevel@tonic-gate {
11377c478bd9Sstevel@tonic-gate 	return (((cpu_flags & CPU_POWEROFF) == 0) &&
11387c478bd9Sstevel@tonic-gate 	    ((cpu_flags & (CPU_READY | CPU_OFFLINE)) != CPU_READY));
11397c478bd9Sstevel@tonic-gate }
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate int
11427c478bd9Sstevel@tonic-gate cpu_flagged_poweredoff(cpu_flag_t cpu_flags)
11437c478bd9Sstevel@tonic-gate {
11447c478bd9Sstevel@tonic-gate 	return ((cpu_flags & CPU_POWEROFF) == CPU_POWEROFF);
11457c478bd9Sstevel@tonic-gate }
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate int
11487c478bd9Sstevel@tonic-gate cpu_flagged_nointr(cpu_flag_t cpu_flags)
11497c478bd9Sstevel@tonic-gate {
11507c478bd9Sstevel@tonic-gate 	return (cpu_flagged_active(cpu_flags) &&
11517c478bd9Sstevel@tonic-gate 	    (cpu_flags & CPU_ENABLE) == 0);
11527c478bd9Sstevel@tonic-gate }
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate int
11557c478bd9Sstevel@tonic-gate cpu_flagged_active(cpu_flag_t cpu_flags)
11567c478bd9Sstevel@tonic-gate {
11577c478bd9Sstevel@tonic-gate 	return (((cpu_flags & (CPU_POWEROFF | CPU_FAULTED | CPU_SPARE)) == 0) &&
11587c478bd9Sstevel@tonic-gate 	    ((cpu_flags & (CPU_READY | CPU_OFFLINE)) == CPU_READY));
11597c478bd9Sstevel@tonic-gate }
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate /*
11627c478bd9Sstevel@tonic-gate  * Bring the indicated CPU online.
11637c478bd9Sstevel@tonic-gate  */
11647c478bd9Sstevel@tonic-gate int
11657c478bd9Sstevel@tonic-gate cpu_online(cpu_t *cp)
11667c478bd9Sstevel@tonic-gate {
11677c478bd9Sstevel@tonic-gate 	int	error = 0;
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 	/*
11707c478bd9Sstevel@tonic-gate 	 * Handle on-line request.
11717c478bd9Sstevel@tonic-gate 	 *	This code must put the new CPU on the active list before
11727c478bd9Sstevel@tonic-gate 	 *	starting it because it will not be paused, and will start
11737c478bd9Sstevel@tonic-gate 	 * 	using the active list immediately.  The real start occurs
11747c478bd9Sstevel@tonic-gate 	 *	when the CPU_QUIESCED flag is turned off.
11757c478bd9Sstevel@tonic-gate 	 */
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 	/*
11807c478bd9Sstevel@tonic-gate 	 * Put all the cpus into a known safe place.
11817c478bd9Sstevel@tonic-gate 	 * No mutexes can be entered while CPUs are paused.
11827c478bd9Sstevel@tonic-gate 	 */
11837c478bd9Sstevel@tonic-gate 	error = mp_cpu_start(cp);	/* arch-dep hook */
11847c478bd9Sstevel@tonic-gate 	if (error == 0) {
11857c478bd9Sstevel@tonic-gate 		pause_cpus(NULL);
11867c478bd9Sstevel@tonic-gate 		cpu_add_active_internal(cp);
11877c478bd9Sstevel@tonic-gate 		if (cp->cpu_flags & CPU_FAULTED) {
11887c478bd9Sstevel@tonic-gate 			cp->cpu_flags &= ~CPU_FAULTED;
11897c478bd9Sstevel@tonic-gate 			mp_cpu_faulted_exit(cp);
11907c478bd9Sstevel@tonic-gate 		}
11917c478bd9Sstevel@tonic-gate 		cp->cpu_flags &= ~(CPU_QUIESCED | CPU_OFFLINE | CPU_FROZEN |
11927c478bd9Sstevel@tonic-gate 		    CPU_SPARE);
11937c478bd9Sstevel@tonic-gate 		start_cpus();
11947c478bd9Sstevel@tonic-gate 		cpu_stats_kstat_create(cp);
11957c478bd9Sstevel@tonic-gate 		cpu_create_intrstat(cp);
11967c478bd9Sstevel@tonic-gate 		lgrp_kstat_create(cp);
11977c478bd9Sstevel@tonic-gate 		cpu_state_change_notify(cp->cpu_id, CPU_ON);
11987c478bd9Sstevel@tonic-gate 		cpu_intr_enable(cp);	/* arch-dep hook */
11997c478bd9Sstevel@tonic-gate 		cyclic_online(cp);
12007c478bd9Sstevel@tonic-gate 		poke_cpu(cp->cpu_id);
12017c478bd9Sstevel@tonic-gate 	}
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate 	return (error);
12047c478bd9Sstevel@tonic-gate }
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate /*
12077c478bd9Sstevel@tonic-gate  * Take the indicated CPU offline.
12087c478bd9Sstevel@tonic-gate  */
12097c478bd9Sstevel@tonic-gate int
12107c478bd9Sstevel@tonic-gate cpu_offline(cpu_t *cp, int flags)
12117c478bd9Sstevel@tonic-gate {
12127c478bd9Sstevel@tonic-gate 	cpupart_t *pp;
12137c478bd9Sstevel@tonic-gate 	int	error = 0;
12147c478bd9Sstevel@tonic-gate 	cpu_t	*ncp;
12157c478bd9Sstevel@tonic-gate 	int	intr_enable;
12167c478bd9Sstevel@tonic-gate 	int	cyclic_off = 0;
12177c478bd9Sstevel@tonic-gate 	int	loop_count;
12187c478bd9Sstevel@tonic-gate 	int	no_quiesce = 0;
12197c478bd9Sstevel@tonic-gate 	int	(*bound_func)(struct cpu *, int);
12207c478bd9Sstevel@tonic-gate 	kthread_t *t;
12217c478bd9Sstevel@tonic-gate 	lpl_t	*cpu_lpl;
12227c478bd9Sstevel@tonic-gate 	proc_t	*p;
12237c478bd9Sstevel@tonic-gate 	int	lgrp_diff_lpl;
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 	/*
12287c478bd9Sstevel@tonic-gate 	 * If we're going from faulted or spare to offline, just
12297c478bd9Sstevel@tonic-gate 	 * clear these flags and update CPU state.
12307c478bd9Sstevel@tonic-gate 	 */
12317c478bd9Sstevel@tonic-gate 	if (cp->cpu_flags & (CPU_FAULTED | CPU_SPARE)) {
12327c478bd9Sstevel@tonic-gate 		if (cp->cpu_flags & CPU_FAULTED) {
12337c478bd9Sstevel@tonic-gate 			cp->cpu_flags &= ~CPU_FAULTED;
12347c478bd9Sstevel@tonic-gate 			mp_cpu_faulted_exit(cp);
12357c478bd9Sstevel@tonic-gate 		}
12367c478bd9Sstevel@tonic-gate 		cp->cpu_flags &= ~CPU_SPARE;
12377c478bd9Sstevel@tonic-gate 		cpu_set_state(cp);
12387c478bd9Sstevel@tonic-gate 		return (0);
12397c478bd9Sstevel@tonic-gate 	}
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate 	/*
12427c478bd9Sstevel@tonic-gate 	 * Handle off-line request.
12437c478bd9Sstevel@tonic-gate 	 */
12447c478bd9Sstevel@tonic-gate 	pp = cp->cpu_part;
12457c478bd9Sstevel@tonic-gate 	/*
12467c478bd9Sstevel@tonic-gate 	 * Don't offline last online CPU in partition
12477c478bd9Sstevel@tonic-gate 	 */
12487c478bd9Sstevel@tonic-gate 	if (ncpus_online <= 1 || pp->cp_ncpus <= 1 || cpu_intr_count(cp) < 2)
12497c478bd9Sstevel@tonic-gate 		return (EBUSY);
12507c478bd9Sstevel@tonic-gate 	/*
12517c478bd9Sstevel@tonic-gate 	 * Unbind all thread bound to our CPU if we were asked to.
12527c478bd9Sstevel@tonic-gate 	 */
12537c478bd9Sstevel@tonic-gate 	if (flags & CPU_FORCED && (error = cpu_unbind(cp->cpu_id)) != 0)
12547c478bd9Sstevel@tonic-gate 		return (error);
12557c478bd9Sstevel@tonic-gate 	/*
12567c478bd9Sstevel@tonic-gate 	 * We shouldn't be bound to this CPU ourselves.
12577c478bd9Sstevel@tonic-gate 	 */
12587c478bd9Sstevel@tonic-gate 	if (curthread->t_bound_cpu == cp)
12597c478bd9Sstevel@tonic-gate 		return (EBUSY);
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 	/*
12627c478bd9Sstevel@tonic-gate 	 * Tell interested parties that this CPU is going offline.
12637c478bd9Sstevel@tonic-gate 	 */
12647c478bd9Sstevel@tonic-gate 	cpu_state_change_notify(cp->cpu_id, CPU_OFF);
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate 	/*
12677c478bd9Sstevel@tonic-gate 	 * Take the CPU out of interrupt participation so we won't find
12687c478bd9Sstevel@tonic-gate 	 * bound kernel threads.  If the architecture cannot completely
12697c478bd9Sstevel@tonic-gate 	 * shut off interrupts on the CPU, don't quiesce it, but don't
12707c478bd9Sstevel@tonic-gate 	 * run anything but interrupt thread... this is indicated by
12717c478bd9Sstevel@tonic-gate 	 * the CPU_OFFLINE flag being on but the CPU_QUIESCE flag being
12727c478bd9Sstevel@tonic-gate 	 * off.
12737c478bd9Sstevel@tonic-gate 	 */
12747c478bd9Sstevel@tonic-gate 	intr_enable = cp->cpu_flags & CPU_ENABLE;
12757c478bd9Sstevel@tonic-gate 	if (intr_enable)
12767c478bd9Sstevel@tonic-gate 		no_quiesce = cpu_intr_disable(cp);
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 	/*
12797c478bd9Sstevel@tonic-gate 	 * Record that we are aiming to offline this cpu.  This acts as
12807c478bd9Sstevel@tonic-gate 	 * a barrier to further weak binding requests in thread_nomigrate
12817c478bd9Sstevel@tonic-gate 	 * and also causes cpu_choose, disp_lowpri_cpu and setfrontdq to
12827c478bd9Sstevel@tonic-gate 	 * lean away from this cpu.  Further strong bindings are already
12837c478bd9Sstevel@tonic-gate 	 * avoided since we hold cpu_lock.  Since threads that are set
12847c478bd9Sstevel@tonic-gate 	 * runnable around now and others coming off the target cpu are
12857c478bd9Sstevel@tonic-gate 	 * directed away from the target, existing strong and weak bindings
12867c478bd9Sstevel@tonic-gate 	 * (especially the latter) to the target cpu stand maximum chance of
12877c478bd9Sstevel@tonic-gate 	 * being able to unbind during the short delay loop below (if other
12887c478bd9Sstevel@tonic-gate 	 * unbound threads compete they may not see cpu in time to unbind
12897c478bd9Sstevel@tonic-gate 	 * even if they would do so immediately.
12907c478bd9Sstevel@tonic-gate 	 */
12917c478bd9Sstevel@tonic-gate 	cpu_inmotion = cp;
12927c478bd9Sstevel@tonic-gate 	membar_enter();
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate 	/*
12957c478bd9Sstevel@tonic-gate 	 * Check for kernel threads (strong or weak) bound to that CPU.
12967c478bd9Sstevel@tonic-gate 	 * Strongly bound threads may not unbind, and we'll have to return
12977c478bd9Sstevel@tonic-gate 	 * EBUSY.  Weakly bound threads should always disappear - we've
12987c478bd9Sstevel@tonic-gate 	 * stopped more weak binding with cpu_inmotion and existing
12997c478bd9Sstevel@tonic-gate 	 * bindings will drain imminently (they may not block).  Nonetheless
13007c478bd9Sstevel@tonic-gate 	 * we will wait for a fixed period for all bound threads to disappear.
13017c478bd9Sstevel@tonic-gate 	 * Inactive interrupt threads are OK (they'll be in TS_FREE
13027c478bd9Sstevel@tonic-gate 	 * state).  If test finds some bound threads, wait a few ticks
13037c478bd9Sstevel@tonic-gate 	 * to give short-lived threads (such as interrupts) chance to
13047c478bd9Sstevel@tonic-gate 	 * complete.  Note that if no_quiesce is set, i.e. this cpu
13057c478bd9Sstevel@tonic-gate 	 * is required to service interrupts, then we take the route
13067c478bd9Sstevel@tonic-gate 	 * that permits interrupt threads to be active (or bypassed).
13077c478bd9Sstevel@tonic-gate 	 */
13087c478bd9Sstevel@tonic-gate 	bound_func = no_quiesce ? disp_bound_threads : disp_bound_anythreads;
13097c478bd9Sstevel@tonic-gate 
13107c478bd9Sstevel@tonic-gate again:	for (loop_count = 0; (*bound_func)(cp, 0); loop_count++) {
13117c478bd9Sstevel@tonic-gate 		if (loop_count >= 5) {
13127c478bd9Sstevel@tonic-gate 			error = EBUSY;	/* some threads still bound */
13137c478bd9Sstevel@tonic-gate 			break;
13147c478bd9Sstevel@tonic-gate 		}
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 		/*
13177c478bd9Sstevel@tonic-gate 		 * If some threads were assigned, give them
13187c478bd9Sstevel@tonic-gate 		 * a chance to complete or move.
13197c478bd9Sstevel@tonic-gate 		 *
13207c478bd9Sstevel@tonic-gate 		 * This assumes that the clock_thread is not bound
13217c478bd9Sstevel@tonic-gate 		 * to any CPU, because the clock_thread is needed to
13227c478bd9Sstevel@tonic-gate 		 * do the delay(hz/100).
13237c478bd9Sstevel@tonic-gate 		 *
13247c478bd9Sstevel@tonic-gate 		 * Note: we still hold the cpu_lock while waiting for
13257c478bd9Sstevel@tonic-gate 		 * the next clock tick.  This is OK since it isn't
13267c478bd9Sstevel@tonic-gate 		 * needed for anything else except processor_bind(2),
13277c478bd9Sstevel@tonic-gate 		 * and system initialization.  If we drop the lock,
13287c478bd9Sstevel@tonic-gate 		 * we would risk another p_online disabling the last
13297c478bd9Sstevel@tonic-gate 		 * processor.
13307c478bd9Sstevel@tonic-gate 		 */
13317c478bd9Sstevel@tonic-gate 		delay(hz/100);
13327c478bd9Sstevel@tonic-gate 	}
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate 	if (error == 0 && cyclic_off == 0) {
13357c478bd9Sstevel@tonic-gate 		if (!cyclic_offline(cp)) {
13367c478bd9Sstevel@tonic-gate 			/*
13377c478bd9Sstevel@tonic-gate 			 * We must have bound cyclics...
13387c478bd9Sstevel@tonic-gate 			 */
13397c478bd9Sstevel@tonic-gate 			error = EBUSY;
13407c478bd9Sstevel@tonic-gate 			goto out;
13417c478bd9Sstevel@tonic-gate 		}
13427c478bd9Sstevel@tonic-gate 		cyclic_off = 1;
13437c478bd9Sstevel@tonic-gate 	}
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate 	/*
13467c478bd9Sstevel@tonic-gate 	 * Call mp_cpu_stop() to perform any special operations
13477c478bd9Sstevel@tonic-gate 	 * needed for this machine architecture to offline a CPU.
13487c478bd9Sstevel@tonic-gate 	 */
13497c478bd9Sstevel@tonic-gate 	if (error == 0)
13507c478bd9Sstevel@tonic-gate 		error = mp_cpu_stop(cp);	/* arch-dep hook */
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 	/*
13537c478bd9Sstevel@tonic-gate 	 * If that all worked, take the CPU offline and decrement
13547c478bd9Sstevel@tonic-gate 	 * ncpus_online.
13557c478bd9Sstevel@tonic-gate 	 */
13567c478bd9Sstevel@tonic-gate 	if (error == 0) {
13577c478bd9Sstevel@tonic-gate 		/*
13587c478bd9Sstevel@tonic-gate 		 * Put all the cpus into a known safe place.
13597c478bd9Sstevel@tonic-gate 		 * No mutexes can be entered while CPUs are paused.
13607c478bd9Sstevel@tonic-gate 		 */
13617c478bd9Sstevel@tonic-gate 		pause_cpus(cp);
13627c478bd9Sstevel@tonic-gate 		/*
13637c478bd9Sstevel@tonic-gate 		 * Repeat the operation, if necessary, to make sure that
13647c478bd9Sstevel@tonic-gate 		 * all outstanding low-level interrupts run to completion
13657c478bd9Sstevel@tonic-gate 		 * before we set the CPU_QUIESCED flag.  It's also possible
13667c478bd9Sstevel@tonic-gate 		 * that a thread has weak bound to the cpu despite our raising
13677c478bd9Sstevel@tonic-gate 		 * cpu_inmotion above since it may have loaded that
13687c478bd9Sstevel@tonic-gate 		 * value before the barrier became visible (this would have
13697c478bd9Sstevel@tonic-gate 		 * to be the thread that was on the target cpu at the time
13707c478bd9Sstevel@tonic-gate 		 * we raised the barrier).
13717c478bd9Sstevel@tonic-gate 		 */
13727c478bd9Sstevel@tonic-gate 		if ((!no_quiesce && cp->cpu_intr_actv != 0) ||
13737c478bd9Sstevel@tonic-gate 		    (*bound_func)(cp, 1)) {
13747c478bd9Sstevel@tonic-gate 			start_cpus();
13757c478bd9Sstevel@tonic-gate 			(void) mp_cpu_start(cp);
13767c478bd9Sstevel@tonic-gate 			goto again;
13777c478bd9Sstevel@tonic-gate 		}
13787c478bd9Sstevel@tonic-gate 		ncp = cp->cpu_next_part;
13797c478bd9Sstevel@tonic-gate 		cpu_lpl = cp->cpu_lpl;
13807c478bd9Sstevel@tonic-gate 		ASSERT(cpu_lpl != NULL);
13817c478bd9Sstevel@tonic-gate 
13827c478bd9Sstevel@tonic-gate 		/*
13837c478bd9Sstevel@tonic-gate 		 * Remove the CPU from the list of active CPUs.
13847c478bd9Sstevel@tonic-gate 		 */
13857c478bd9Sstevel@tonic-gate 		cpu_remove_active(cp);
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate 		/*
13887c478bd9Sstevel@tonic-gate 		 * Walk the active process list and look for threads
13897c478bd9Sstevel@tonic-gate 		 * whose home lgroup needs to be updated, or
13907c478bd9Sstevel@tonic-gate 		 * the last CPU they run on is the one being offlined now.
13917c478bd9Sstevel@tonic-gate 		 */
13927c478bd9Sstevel@tonic-gate 
13937c478bd9Sstevel@tonic-gate 		ASSERT(curthread->t_cpu != cp);
13947c478bd9Sstevel@tonic-gate 		for (p = practive; p != NULL; p = p->p_next) {
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate 			t = p->p_tlist;
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate 			if (t == NULL)
13997c478bd9Sstevel@tonic-gate 				continue;
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate 			lgrp_diff_lpl = 0;
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate 			do {
14047c478bd9Sstevel@tonic-gate 				ASSERT(t->t_lpl != NULL);
14057c478bd9Sstevel@tonic-gate 				/*
14067c478bd9Sstevel@tonic-gate 				 * Taking last CPU in lpl offline
14077c478bd9Sstevel@tonic-gate 				 * Rehome thread if it is in this lpl
14087c478bd9Sstevel@tonic-gate 				 * Otherwise, update the count of how many
14097c478bd9Sstevel@tonic-gate 				 * threads are in this CPU's lgroup but have
14107c478bd9Sstevel@tonic-gate 				 * a different lpl.
14117c478bd9Sstevel@tonic-gate 				 */
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate 				if (cpu_lpl->lpl_ncpu == 0) {
14147c478bd9Sstevel@tonic-gate 					if (t->t_lpl == cpu_lpl)
14157c478bd9Sstevel@tonic-gate 						lgrp_move_thread(t,
14167c478bd9Sstevel@tonic-gate 						    lgrp_choose(t,
14177c478bd9Sstevel@tonic-gate 						    t->t_cpupart), 0);
14187c478bd9Sstevel@tonic-gate 					else if (t->t_lpl->lpl_lgrpid ==
14197c478bd9Sstevel@tonic-gate 					    cpu_lpl->lpl_lgrpid)
14207c478bd9Sstevel@tonic-gate 						lgrp_diff_lpl++;
14217c478bd9Sstevel@tonic-gate 				}
14227c478bd9Sstevel@tonic-gate 				ASSERT(t->t_lpl->lpl_ncpu > 0);
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate 				/*
14257c478bd9Sstevel@tonic-gate 				 * Update CPU last ran on if it was this CPU
14267c478bd9Sstevel@tonic-gate 				 */
14277c478bd9Sstevel@tonic-gate 				if (t->t_cpu == cp && t->t_bound_cpu != cp)
14287c478bd9Sstevel@tonic-gate 				    t->t_cpu = disp_lowpri_cpu(ncp, t->t_lpl,
14297c478bd9Sstevel@tonic-gate 					t->t_pri, NULL);
14307c478bd9Sstevel@tonic-gate 				ASSERT(t->t_cpu != cp || t->t_bound_cpu == cp ||
14317c478bd9Sstevel@tonic-gate 				    t->t_weakbound_cpu == cp);
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate 				t = t->t_forw;
14347c478bd9Sstevel@tonic-gate 			} while (t != p->p_tlist);
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 			/*
14377c478bd9Sstevel@tonic-gate 			 * Didn't find any threads in the same lgroup as this
14387c478bd9Sstevel@tonic-gate 			 * CPU with a different lpl, so remove the lgroup from
14397c478bd9Sstevel@tonic-gate 			 * the process lgroup bitmask.
14407c478bd9Sstevel@tonic-gate 			 */
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate 			if (lgrp_diff_lpl == 0)
14437c478bd9Sstevel@tonic-gate 				klgrpset_del(p->p_lgrpset, cpu_lpl->lpl_lgrpid);
14447c478bd9Sstevel@tonic-gate 		}
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate 		/*
14477c478bd9Sstevel@tonic-gate 		 * Walk thread list looking for threads that need to be
14487c478bd9Sstevel@tonic-gate 		 * rehomed, since there are some threads that are not in
14497c478bd9Sstevel@tonic-gate 		 * their process's p_tlist.
14507c478bd9Sstevel@tonic-gate 		 */
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate 		t = curthread;
14537c478bd9Sstevel@tonic-gate 		do {
14547c478bd9Sstevel@tonic-gate 			ASSERT(t != NULL && t->t_lpl != NULL);
14557c478bd9Sstevel@tonic-gate 
14567c478bd9Sstevel@tonic-gate 			/*
14577c478bd9Sstevel@tonic-gate 			 * Rehome threads with same lpl as this CPU when this
14587c478bd9Sstevel@tonic-gate 			 * is the last CPU in the lpl.
14597c478bd9Sstevel@tonic-gate 			 */
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 			if ((cpu_lpl->lpl_ncpu == 0) && (t->t_lpl == cpu_lpl))
14627c478bd9Sstevel@tonic-gate 				lgrp_move_thread(t,
14637c478bd9Sstevel@tonic-gate 				    lgrp_choose(t, t->t_cpupart), 1);
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate 			ASSERT(t->t_lpl->lpl_ncpu > 0);
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate 			/*
14687c478bd9Sstevel@tonic-gate 			 * Update CPU last ran on if it was this CPU
14697c478bd9Sstevel@tonic-gate 			 */
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate 			if (t->t_cpu == cp && t->t_bound_cpu != cp) {
14727c478bd9Sstevel@tonic-gate 				t->t_cpu = disp_lowpri_cpu(ncp,
14737c478bd9Sstevel@tonic-gate 				    t->t_lpl, t->t_pri, NULL);
14747c478bd9Sstevel@tonic-gate 			}
14757c478bd9Sstevel@tonic-gate 			ASSERT(t->t_cpu != cp || t->t_bound_cpu == cp ||
14767c478bd9Sstevel@tonic-gate 			    t->t_weakbound_cpu == cp);
14777c478bd9Sstevel@tonic-gate 			t = t->t_next;
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate 		} while (t != curthread);
14807c478bd9Sstevel@tonic-gate 		ASSERT((cp->cpu_flags & (CPU_FAULTED | CPU_SPARE)) == 0);
14817c478bd9Sstevel@tonic-gate 		cp->cpu_flags |= CPU_OFFLINE;
14827c478bd9Sstevel@tonic-gate 		disp_cpu_inactive(cp);
14837c478bd9Sstevel@tonic-gate 		if (!no_quiesce)
14847c478bd9Sstevel@tonic-gate 			cp->cpu_flags |= CPU_QUIESCED;
14857c478bd9Sstevel@tonic-gate 		ncpus_online--;
14867c478bd9Sstevel@tonic-gate 		cpu_set_state(cp);
14877c478bd9Sstevel@tonic-gate 		cpu_inmotion = NULL;
14887c478bd9Sstevel@tonic-gate 		start_cpus();
14897c478bd9Sstevel@tonic-gate 		cpu_stats_kstat_destroy(cp);
14907c478bd9Sstevel@tonic-gate 		cpu_delete_intrstat(cp);
14917c478bd9Sstevel@tonic-gate 		lgrp_kstat_destroy(cp);
14927c478bd9Sstevel@tonic-gate 	}
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate out:
14957c478bd9Sstevel@tonic-gate 	cpu_inmotion = NULL;
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate 	/*
14987c478bd9Sstevel@tonic-gate 	 * If we failed, re-enable interrupts.
14997c478bd9Sstevel@tonic-gate 	 * Do this even if cpu_intr_disable returned an error, because
15007c478bd9Sstevel@tonic-gate 	 * it may have partially disabled interrupts.
15017c478bd9Sstevel@tonic-gate 	 */
15027c478bd9Sstevel@tonic-gate 	if (error && intr_enable)
15037c478bd9Sstevel@tonic-gate 		cpu_intr_enable(cp);
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 	/*
15067c478bd9Sstevel@tonic-gate 	 * If we failed, but managed to offline the cyclic subsystem on this
15077c478bd9Sstevel@tonic-gate 	 * CPU, bring it back online.
15087c478bd9Sstevel@tonic-gate 	 */
15097c478bd9Sstevel@tonic-gate 	if (error && cyclic_off)
15107c478bd9Sstevel@tonic-gate 		cyclic_online(cp);
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate 	/*
15137c478bd9Sstevel@tonic-gate 	 * If we failed, we need to notify everyone that this CPU is back on.
15147c478bd9Sstevel@tonic-gate 	 */
15157c478bd9Sstevel@tonic-gate 	if (error != 0)
15167c478bd9Sstevel@tonic-gate 		cpu_state_change_notify(cp->cpu_id, CPU_ON);
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate 	return (error);
15197c478bd9Sstevel@tonic-gate }
15207c478bd9Sstevel@tonic-gate 
15217c478bd9Sstevel@tonic-gate /*
15227c478bd9Sstevel@tonic-gate  * Mark the indicated CPU as faulted, taking it offline.
15237c478bd9Sstevel@tonic-gate  */
15247c478bd9Sstevel@tonic-gate int
15257c478bd9Sstevel@tonic-gate cpu_faulted(cpu_t *cp, int flags)
15267c478bd9Sstevel@tonic-gate {
15277c478bd9Sstevel@tonic-gate 	int	error = 0;
15287c478bd9Sstevel@tonic-gate 
15297c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
15307c478bd9Sstevel@tonic-gate 	ASSERT(!cpu_is_poweredoff(cp));
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 	if (cpu_is_offline(cp)) {
15337c478bd9Sstevel@tonic-gate 		cp->cpu_flags &= ~CPU_SPARE;
15347c478bd9Sstevel@tonic-gate 		cp->cpu_flags |= CPU_FAULTED;
15357c478bd9Sstevel@tonic-gate 		mp_cpu_faulted_enter(cp);
15367c478bd9Sstevel@tonic-gate 		cpu_set_state(cp);
15377c478bd9Sstevel@tonic-gate 		return (0);
15387c478bd9Sstevel@tonic-gate 	}
15397c478bd9Sstevel@tonic-gate 
15407c478bd9Sstevel@tonic-gate 	if ((error = cpu_offline(cp, flags)) == 0) {
15417c478bd9Sstevel@tonic-gate 		cp->cpu_flags |= CPU_FAULTED;
15427c478bd9Sstevel@tonic-gate 		mp_cpu_faulted_enter(cp);
15437c478bd9Sstevel@tonic-gate 		cpu_set_state(cp);
15447c478bd9Sstevel@tonic-gate 	}
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate 	return (error);
15477c478bd9Sstevel@tonic-gate }
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate /*
15507c478bd9Sstevel@tonic-gate  * Mark the indicated CPU as a spare, taking it offline.
15517c478bd9Sstevel@tonic-gate  */
15527c478bd9Sstevel@tonic-gate int
15537c478bd9Sstevel@tonic-gate cpu_spare(cpu_t *cp, int flags)
15547c478bd9Sstevel@tonic-gate {
15557c478bd9Sstevel@tonic-gate 	int	error = 0;
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
15587c478bd9Sstevel@tonic-gate 	ASSERT(!cpu_is_poweredoff(cp));
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate 	if (cpu_is_offline(cp)) {
15617c478bd9Sstevel@tonic-gate 		if (cp->cpu_flags & CPU_FAULTED) {
15627c478bd9Sstevel@tonic-gate 			cp->cpu_flags &= ~CPU_FAULTED;
15637c478bd9Sstevel@tonic-gate 			mp_cpu_faulted_exit(cp);
15647c478bd9Sstevel@tonic-gate 		}
15657c478bd9Sstevel@tonic-gate 		cp->cpu_flags |= CPU_SPARE;
15667c478bd9Sstevel@tonic-gate 		cpu_set_state(cp);
15677c478bd9Sstevel@tonic-gate 		return (0);
15687c478bd9Sstevel@tonic-gate 	}
15697c478bd9Sstevel@tonic-gate 
15707c478bd9Sstevel@tonic-gate 	if ((error = cpu_offline(cp, flags)) == 0) {
15717c478bd9Sstevel@tonic-gate 		cp->cpu_flags |= CPU_SPARE;
15727c478bd9Sstevel@tonic-gate 		cpu_set_state(cp);
15737c478bd9Sstevel@tonic-gate 	}
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate 	return (error);
15767c478bd9Sstevel@tonic-gate }
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate /*
15797c478bd9Sstevel@tonic-gate  * Take the indicated CPU from poweroff to offline.
15807c478bd9Sstevel@tonic-gate  */
15817c478bd9Sstevel@tonic-gate int
15827c478bd9Sstevel@tonic-gate cpu_poweron(cpu_t *cp)
15837c478bd9Sstevel@tonic-gate {
15847c478bd9Sstevel@tonic-gate 	int	error = ENOTSUP;
15857c478bd9Sstevel@tonic-gate 
15867c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
15877c478bd9Sstevel@tonic-gate 	ASSERT(cpu_is_poweredoff(cp));
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate 	error = mp_cpu_poweron(cp);	/* arch-dep hook */
15907c478bd9Sstevel@tonic-gate 	if (error == 0)
15917c478bd9Sstevel@tonic-gate 		cpu_set_state(cp);
15927c478bd9Sstevel@tonic-gate 
15937c478bd9Sstevel@tonic-gate 	return (error);
15947c478bd9Sstevel@tonic-gate }
15957c478bd9Sstevel@tonic-gate 
15967c478bd9Sstevel@tonic-gate /*
15977c478bd9Sstevel@tonic-gate  * Take the indicated CPU from any inactive state to powered off.
15987c478bd9Sstevel@tonic-gate  */
15997c478bd9Sstevel@tonic-gate int
16007c478bd9Sstevel@tonic-gate cpu_poweroff(cpu_t *cp)
16017c478bd9Sstevel@tonic-gate {
16027c478bd9Sstevel@tonic-gate 	int	error = ENOTSUP;
16037c478bd9Sstevel@tonic-gate 
16047c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
16057c478bd9Sstevel@tonic-gate 	ASSERT(cpu_is_offline(cp));
16067c478bd9Sstevel@tonic-gate 
16077c478bd9Sstevel@tonic-gate 	if (!(cp->cpu_flags & CPU_QUIESCED))
16087c478bd9Sstevel@tonic-gate 		return (EBUSY);		/* not completely idle */
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate 	error = mp_cpu_poweroff(cp);	/* arch-dep hook */
16117c478bd9Sstevel@tonic-gate 	if (error == 0)
16127c478bd9Sstevel@tonic-gate 		cpu_set_state(cp);
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate 	return (error);
16157c478bd9Sstevel@tonic-gate }
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate /*
16187c478bd9Sstevel@tonic-gate  * Initialize the CPU lists for the first CPU.
16197c478bd9Sstevel@tonic-gate  */
16207c478bd9Sstevel@tonic-gate void
16217c478bd9Sstevel@tonic-gate cpu_list_init(cpu_t *cp)
16227c478bd9Sstevel@tonic-gate {
16237c478bd9Sstevel@tonic-gate 	cp->cpu_next = cp;
16247c478bd9Sstevel@tonic-gate 	cp->cpu_prev = cp;
16257c478bd9Sstevel@tonic-gate 	cpu_list = cp;
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 	cp->cpu_next_onln = cp;
16287c478bd9Sstevel@tonic-gate 	cp->cpu_prev_onln = cp;
16297c478bd9Sstevel@tonic-gate 	cpu_active = cp;
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate 	cp->cpu_seqid = 0;
16327c478bd9Sstevel@tonic-gate 	CPUSET_ADD(cpu_seqid_inuse, 0);
16337c478bd9Sstevel@tonic-gate 	cp->cpu_cache_offset = KMEM_CACHE_SIZE(cp->cpu_seqid);
16347c478bd9Sstevel@tonic-gate 	cp_default.cp_cpulist = cp;
16357c478bd9Sstevel@tonic-gate 	cp_default.cp_ncpus = 1;
16367c478bd9Sstevel@tonic-gate 	cp->cpu_next_part = cp;
16377c478bd9Sstevel@tonic-gate 	cp->cpu_prev_part = cp;
16387c478bd9Sstevel@tonic-gate 	cp->cpu_part = &cp_default;
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 	CPUSET_ADD(cpu_available, cp->cpu_id);
16417c478bd9Sstevel@tonic-gate }
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate /*
16447c478bd9Sstevel@tonic-gate  * Insert a CPU into the list of available CPUs.
16457c478bd9Sstevel@tonic-gate  */
16467c478bd9Sstevel@tonic-gate void
16477c478bd9Sstevel@tonic-gate cpu_add_unit(cpu_t *cp)
16487c478bd9Sstevel@tonic-gate {
16497c478bd9Sstevel@tonic-gate 	int seqid;
16507c478bd9Sstevel@tonic-gate 
16517c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
16527c478bd9Sstevel@tonic-gate 	ASSERT(cpu_list != NULL);	/* list started in cpu_list_init */
16537c478bd9Sstevel@tonic-gate 
16547c478bd9Sstevel@tonic-gate 	lgrp_config(LGRP_CONFIG_CPU_ADD, (uintptr_t)cp, 0);
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate 	/*
16577c478bd9Sstevel@tonic-gate 	 * Note: most users of the cpu_list will grab the
16587c478bd9Sstevel@tonic-gate 	 * cpu_lock to insure that it isn't modified.  However,
16597c478bd9Sstevel@tonic-gate 	 * certain users can't or won't do that.  To allow this
16607c478bd9Sstevel@tonic-gate 	 * we pause the other cpus.  Users who walk the list
16617c478bd9Sstevel@tonic-gate 	 * without cpu_lock, must disable kernel preemption
16627c478bd9Sstevel@tonic-gate 	 * to insure that the list isn't modified underneath
16637c478bd9Sstevel@tonic-gate 	 * them.  Also, any cached pointers to cpu structures
16647c478bd9Sstevel@tonic-gate 	 * must be revalidated by checking to see if the
16657c478bd9Sstevel@tonic-gate 	 * cpu_next pointer points to itself.  This check must
16667c478bd9Sstevel@tonic-gate 	 * be done with the cpu_lock held or kernel preemption
16677c478bd9Sstevel@tonic-gate 	 * disabled.  This check relies upon the fact that
16687c478bd9Sstevel@tonic-gate 	 * old cpu structures are not free'ed or cleared after
16697c478bd9Sstevel@tonic-gate 	 * then are removed from the cpu_list.
16707c478bd9Sstevel@tonic-gate 	 *
16717c478bd9Sstevel@tonic-gate 	 * Note that the clock code walks the cpu list dereferencing
16727c478bd9Sstevel@tonic-gate 	 * the cpu_part pointer, so we need to initialize it before
16737c478bd9Sstevel@tonic-gate 	 * adding the cpu to the list.
16747c478bd9Sstevel@tonic-gate 	 */
16757c478bd9Sstevel@tonic-gate 	cp->cpu_part = &cp_default;
16767c478bd9Sstevel@tonic-gate 	(void) pause_cpus(NULL);
16777c478bd9Sstevel@tonic-gate 	cp->cpu_next = cpu_list;
16787c478bd9Sstevel@tonic-gate 	cp->cpu_prev = cpu_list->cpu_prev;
16797c478bd9Sstevel@tonic-gate 	cpu_list->cpu_prev->cpu_next = cp;
16807c478bd9Sstevel@tonic-gate 	cpu_list->cpu_prev = cp;
16817c478bd9Sstevel@tonic-gate 	start_cpus();
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate 	for (seqid = 0; CPU_IN_SET(cpu_seqid_inuse, seqid); seqid++)
16847c478bd9Sstevel@tonic-gate 		continue;
16857c478bd9Sstevel@tonic-gate 	CPUSET_ADD(cpu_seqid_inuse, seqid);
16867c478bd9Sstevel@tonic-gate 	cp->cpu_seqid = seqid;
16877c478bd9Sstevel@tonic-gate 	ASSERT(ncpus < max_ncpus);
16887c478bd9Sstevel@tonic-gate 	ncpus++;
16897c478bd9Sstevel@tonic-gate 	cp->cpu_cache_offset = KMEM_CACHE_SIZE(cp->cpu_seqid);
16907c478bd9Sstevel@tonic-gate 	cpu[cp->cpu_id] = cp;
16917c478bd9Sstevel@tonic-gate 	CPUSET_ADD(cpu_available, cp->cpu_id);
16927c478bd9Sstevel@tonic-gate 
16937c478bd9Sstevel@tonic-gate 	/*
16947c478bd9Sstevel@tonic-gate 	 * allocate a pause thread for this CPU.
16957c478bd9Sstevel@tonic-gate 	 */
16967c478bd9Sstevel@tonic-gate 	cpu_pause_alloc(cp);
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate 	/*
16997c478bd9Sstevel@tonic-gate 	 * So that new CPUs won't have NULL prev_onln and next_onln pointers,
17007c478bd9Sstevel@tonic-gate 	 * link them into a list of just that CPU.
17017c478bd9Sstevel@tonic-gate 	 * This is so that disp_lowpri_cpu will work for thread_create in
17027c478bd9Sstevel@tonic-gate 	 * pause_cpus() when called from the startup thread in a new CPU.
17037c478bd9Sstevel@tonic-gate 	 */
17047c478bd9Sstevel@tonic-gate 	cp->cpu_next_onln = cp;
17057c478bd9Sstevel@tonic-gate 	cp->cpu_prev_onln = cp;
17067c478bd9Sstevel@tonic-gate 	cpu_info_kstat_create(cp);
17077c478bd9Sstevel@tonic-gate 	cp->cpu_next_part = cp;
17087c478bd9Sstevel@tonic-gate 	cp->cpu_prev_part = cp;
17097c478bd9Sstevel@tonic-gate 
17107c478bd9Sstevel@tonic-gate 	init_cpu_mstate(cp, CMS_SYSTEM);
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate 	pool_pset_mod = gethrtime();
17137c478bd9Sstevel@tonic-gate }
17147c478bd9Sstevel@tonic-gate 
17157c478bd9Sstevel@tonic-gate /*
17167c478bd9Sstevel@tonic-gate  * Do the opposite of cpu_add_unit().
17177c478bd9Sstevel@tonic-gate  */
17187c478bd9Sstevel@tonic-gate void
17197c478bd9Sstevel@tonic-gate cpu_del_unit(int cpuid)
17207c478bd9Sstevel@tonic-gate {
17217c478bd9Sstevel@tonic-gate 	struct cpu	*cp, *cpnext;
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
17247c478bd9Sstevel@tonic-gate 	cp = cpu[cpuid];
17257c478bd9Sstevel@tonic-gate 	ASSERT(cp != NULL);
17267c478bd9Sstevel@tonic-gate 
17277c478bd9Sstevel@tonic-gate 	ASSERT(cp->cpu_next_onln == cp);
17287c478bd9Sstevel@tonic-gate 	ASSERT(cp->cpu_prev_onln == cp);
17297c478bd9Sstevel@tonic-gate 	ASSERT(cp->cpu_next_part == cp);
17307c478bd9Sstevel@tonic-gate 	ASSERT(cp->cpu_prev_part == cp);
17317c478bd9Sstevel@tonic-gate 
17327c478bd9Sstevel@tonic-gate 	chip_cpu_fini(cp);
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate 	/*
17357c478bd9Sstevel@tonic-gate 	 * Destroy kstat stuff.
17367c478bd9Sstevel@tonic-gate 	 */
17377c478bd9Sstevel@tonic-gate 	cpu_info_kstat_destroy(cp);
17387c478bd9Sstevel@tonic-gate 	term_cpu_mstate(cp);
17397c478bd9Sstevel@tonic-gate 	/*
17407c478bd9Sstevel@tonic-gate 	 * Free up pause thread.
17417c478bd9Sstevel@tonic-gate 	 */
17427c478bd9Sstevel@tonic-gate 	cpu_pause_free(cp);
17437c478bd9Sstevel@tonic-gate 	CPUSET_DEL(cpu_available, cp->cpu_id);
17447c478bd9Sstevel@tonic-gate 	cpu[cp->cpu_id] = NULL;
17457c478bd9Sstevel@tonic-gate 	/*
17467c478bd9Sstevel@tonic-gate 	 * The clock thread and mutex_vector_enter cannot hold the
17477c478bd9Sstevel@tonic-gate 	 * cpu_lock while traversing the cpu list, therefore we pause
17487c478bd9Sstevel@tonic-gate 	 * all other threads by pausing the other cpus. These, and any
17497c478bd9Sstevel@tonic-gate 	 * other routines holding cpu pointers while possibly sleeping
17507c478bd9Sstevel@tonic-gate 	 * must be sure to call kpreempt_disable before processing the
17517c478bd9Sstevel@tonic-gate 	 * list and be sure to check that the cpu has not been deleted
17527c478bd9Sstevel@tonic-gate 	 * after any sleeps (check cp->cpu_next != NULL). We guarantee
17537c478bd9Sstevel@tonic-gate 	 * to keep the deleted cpu structure around.
17547c478bd9Sstevel@tonic-gate 	 *
17557c478bd9Sstevel@tonic-gate 	 * Note that this MUST be done AFTER cpu_available
17567c478bd9Sstevel@tonic-gate 	 * has been updated so that we don't waste time
17577c478bd9Sstevel@tonic-gate 	 * trying to pause the cpu we're trying to delete.
17587c478bd9Sstevel@tonic-gate 	 */
17597c478bd9Sstevel@tonic-gate 	(void) pause_cpus(NULL);
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate 	cpnext = cp->cpu_next;
17627c478bd9Sstevel@tonic-gate 	cp->cpu_prev->cpu_next = cp->cpu_next;
17637c478bd9Sstevel@tonic-gate 	cp->cpu_next->cpu_prev = cp->cpu_prev;
17647c478bd9Sstevel@tonic-gate 	if (cp == cpu_list)
17657c478bd9Sstevel@tonic-gate 	    cpu_list = cpnext;
17667c478bd9Sstevel@tonic-gate 
17677c478bd9Sstevel@tonic-gate 	/*
17687c478bd9Sstevel@tonic-gate 	 * Signals that the cpu has been deleted (see above).
17697c478bd9Sstevel@tonic-gate 	 */
17707c478bd9Sstevel@tonic-gate 	cp->cpu_next = NULL;
17717c478bd9Sstevel@tonic-gate 	cp->cpu_prev = NULL;
17727c478bd9Sstevel@tonic-gate 
17737c478bd9Sstevel@tonic-gate 	start_cpus();
17747c478bd9Sstevel@tonic-gate 
17757c478bd9Sstevel@tonic-gate 	CPUSET_DEL(cpu_seqid_inuse, cp->cpu_seqid);
17767c478bd9Sstevel@tonic-gate 	ncpus--;
17777c478bd9Sstevel@tonic-gate 	lgrp_config(LGRP_CONFIG_CPU_DEL, (uintptr_t)cp, 0);
17787c478bd9Sstevel@tonic-gate 
17797c478bd9Sstevel@tonic-gate 	pool_pset_mod = gethrtime();
17807c478bd9Sstevel@tonic-gate }
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate /*
17837c478bd9Sstevel@tonic-gate  * Add a CPU to the list of active CPUs.
17847c478bd9Sstevel@tonic-gate  *	This routine must not get any locks, because other CPUs are paused.
17857c478bd9Sstevel@tonic-gate  */
17867c478bd9Sstevel@tonic-gate static void
17877c478bd9Sstevel@tonic-gate cpu_add_active_internal(cpu_t *cp)
17887c478bd9Sstevel@tonic-gate {
17897c478bd9Sstevel@tonic-gate 	cpupart_t	*pp = cp->cpu_part;
17907c478bd9Sstevel@tonic-gate 
17917c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
17927c478bd9Sstevel@tonic-gate 	ASSERT(cpu_list != NULL);	/* list started in cpu_list_init */
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate 	ncpus_online++;
17957c478bd9Sstevel@tonic-gate 	cpu_set_state(cp);
17967c478bd9Sstevel@tonic-gate 	cp->cpu_next_onln = cpu_active;
17977c478bd9Sstevel@tonic-gate 	cp->cpu_prev_onln = cpu_active->cpu_prev_onln;
17987c478bd9Sstevel@tonic-gate 	cpu_active->cpu_prev_onln->cpu_next_onln = cp;
17997c478bd9Sstevel@tonic-gate 	cpu_active->cpu_prev_onln = cp;
18007c478bd9Sstevel@tonic-gate 
18017c478bd9Sstevel@tonic-gate 	if (pp->cp_cpulist) {
18027c478bd9Sstevel@tonic-gate 		cp->cpu_next_part = pp->cp_cpulist;
18037c478bd9Sstevel@tonic-gate 		cp->cpu_prev_part = pp->cp_cpulist->cpu_prev_part;
18047c478bd9Sstevel@tonic-gate 		pp->cp_cpulist->cpu_prev_part->cpu_next_part = cp;
18057c478bd9Sstevel@tonic-gate 		pp->cp_cpulist->cpu_prev_part = cp;
18067c478bd9Sstevel@tonic-gate 	} else {
18077c478bd9Sstevel@tonic-gate 		ASSERT(pp->cp_ncpus == 0);
18087c478bd9Sstevel@tonic-gate 		pp->cp_cpulist = cp->cpu_next_part = cp->cpu_prev_part = cp;
18097c478bd9Sstevel@tonic-gate 	}
18107c478bd9Sstevel@tonic-gate 	pp->cp_ncpus++;
18117c478bd9Sstevel@tonic-gate 	if (pp->cp_ncpus == 1) {
18127c478bd9Sstevel@tonic-gate 		cp_numparts_nonempty++;
18137c478bd9Sstevel@tonic-gate 		ASSERT(cp_numparts_nonempty != 0);
18147c478bd9Sstevel@tonic-gate 	}
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate 	chip_cpu_assign(cp);
18177c478bd9Sstevel@tonic-gate 
18187c478bd9Sstevel@tonic-gate 	lgrp_config(LGRP_CONFIG_CPU_ONLINE, (uintptr_t)cp, 0);
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate 	bzero(&cp->cpu_loadavg, sizeof (cp->cpu_loadavg));
18217c478bd9Sstevel@tonic-gate }
18227c478bd9Sstevel@tonic-gate 
18237c478bd9Sstevel@tonic-gate /*
18247c478bd9Sstevel@tonic-gate  * Add a CPU to the list of active CPUs.
18257c478bd9Sstevel@tonic-gate  *	This is called from machine-dependent layers when a new CPU is started.
18267c478bd9Sstevel@tonic-gate  */
18277c478bd9Sstevel@tonic-gate void
18287c478bd9Sstevel@tonic-gate cpu_add_active(cpu_t *cp)
18297c478bd9Sstevel@tonic-gate {
18307c478bd9Sstevel@tonic-gate 	pause_cpus(NULL);
18317c478bd9Sstevel@tonic-gate 	cpu_add_active_internal(cp);
18327c478bd9Sstevel@tonic-gate 	start_cpus();
18337c478bd9Sstevel@tonic-gate 	cpu_stats_kstat_create(cp);
18347c478bd9Sstevel@tonic-gate 	cpu_create_intrstat(cp);
18357c478bd9Sstevel@tonic-gate 	lgrp_kstat_create(cp);
18367c478bd9Sstevel@tonic-gate 	cpu_state_change_notify(cp->cpu_id, CPU_INIT);
18377c478bd9Sstevel@tonic-gate }
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate 
18407c478bd9Sstevel@tonic-gate /*
18417c478bd9Sstevel@tonic-gate  * Remove a CPU from the list of active CPUs.
18427c478bd9Sstevel@tonic-gate  *	This routine must not get any locks, because other CPUs are paused.
18437c478bd9Sstevel@tonic-gate  */
18447c478bd9Sstevel@tonic-gate /* ARGSUSED */
18457c478bd9Sstevel@tonic-gate static void
18467c478bd9Sstevel@tonic-gate cpu_remove_active(cpu_t *cp)
18477c478bd9Sstevel@tonic-gate {
18487c478bd9Sstevel@tonic-gate 	cpupart_t	*pp = cp->cpu_part;
18497c478bd9Sstevel@tonic-gate 
18507c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
18517c478bd9Sstevel@tonic-gate 	ASSERT(cp->cpu_next_onln != cp);	/* not the last one */
18527c478bd9Sstevel@tonic-gate 	ASSERT(cp->cpu_prev_onln != cp);	/* not the last one */
18537c478bd9Sstevel@tonic-gate 
18547c478bd9Sstevel@tonic-gate 	chip_cpu_unassign(cp);
18557c478bd9Sstevel@tonic-gate 
18567c478bd9Sstevel@tonic-gate 	lgrp_config(LGRP_CONFIG_CPU_OFFLINE, (uintptr_t)cp, 0);
18577c478bd9Sstevel@tonic-gate 
18587c478bd9Sstevel@tonic-gate 	cp->cpu_prev_onln->cpu_next_onln = cp->cpu_next_onln;
18597c478bd9Sstevel@tonic-gate 	cp->cpu_next_onln->cpu_prev_onln = cp->cpu_prev_onln;
18607c478bd9Sstevel@tonic-gate 	if (cpu_active == cp) {
18617c478bd9Sstevel@tonic-gate 		cpu_active = cp->cpu_next_onln;
18627c478bd9Sstevel@tonic-gate 	}
18637c478bd9Sstevel@tonic-gate 	cp->cpu_next_onln = cp;
18647c478bd9Sstevel@tonic-gate 	cp->cpu_prev_onln = cp;
18657c478bd9Sstevel@tonic-gate 
18667c478bd9Sstevel@tonic-gate 	cp->cpu_prev_part->cpu_next_part = cp->cpu_next_part;
18677c478bd9Sstevel@tonic-gate 	cp->cpu_next_part->cpu_prev_part = cp->cpu_prev_part;
18687c478bd9Sstevel@tonic-gate 	if (pp->cp_cpulist == cp) {
18697c478bd9Sstevel@tonic-gate 		pp->cp_cpulist = cp->cpu_next_part;
18707c478bd9Sstevel@tonic-gate 		ASSERT(pp->cp_cpulist != cp);
18717c478bd9Sstevel@tonic-gate 	}
18727c478bd9Sstevel@tonic-gate 	cp->cpu_next_part = cp;
18737c478bd9Sstevel@tonic-gate 	cp->cpu_prev_part = cp;
18747c478bd9Sstevel@tonic-gate 	pp->cp_ncpus--;
18757c478bd9Sstevel@tonic-gate 	if (pp->cp_ncpus == 0) {
18767c478bd9Sstevel@tonic-gate 		cp_numparts_nonempty--;
18777c478bd9Sstevel@tonic-gate 		ASSERT(cp_numparts_nonempty != 0);
18787c478bd9Sstevel@tonic-gate 	}
18797c478bd9Sstevel@tonic-gate }
18807c478bd9Sstevel@tonic-gate 
18817c478bd9Sstevel@tonic-gate /*
18827c478bd9Sstevel@tonic-gate  * Routine used to setup a newly inserted CPU in preparation for starting
18837c478bd9Sstevel@tonic-gate  * it running code.
18847c478bd9Sstevel@tonic-gate  */
18857c478bd9Sstevel@tonic-gate int
18867c478bd9Sstevel@tonic-gate cpu_configure(int cpuid)
18877c478bd9Sstevel@tonic-gate {
18887c478bd9Sstevel@tonic-gate 	int retval = 0;
18897c478bd9Sstevel@tonic-gate 
18907c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
18917c478bd9Sstevel@tonic-gate 
18927c478bd9Sstevel@tonic-gate 	/*
18937c478bd9Sstevel@tonic-gate 	 * Some structures are statically allocated based upon
18947c478bd9Sstevel@tonic-gate 	 * the maximum number of cpus the system supports.  Do not
18957c478bd9Sstevel@tonic-gate 	 * try to add anything beyond this limit.
18967c478bd9Sstevel@tonic-gate 	 */
18977c478bd9Sstevel@tonic-gate 	if (cpuid < 0 || cpuid >= NCPU) {
18987c478bd9Sstevel@tonic-gate 		return (EINVAL);
18997c478bd9Sstevel@tonic-gate 	}
19007c478bd9Sstevel@tonic-gate 
19017c478bd9Sstevel@tonic-gate 	if ((cpu[cpuid] != NULL) && (cpu[cpuid]->cpu_flags != 0)) {
19027c478bd9Sstevel@tonic-gate 		return (EALREADY);
19037c478bd9Sstevel@tonic-gate 	}
19047c478bd9Sstevel@tonic-gate 
19057c478bd9Sstevel@tonic-gate 	if ((retval = mp_cpu_configure(cpuid)) != 0) {
19067c478bd9Sstevel@tonic-gate 		return (retval);
19077c478bd9Sstevel@tonic-gate 	}
19087c478bd9Sstevel@tonic-gate 
19097c478bd9Sstevel@tonic-gate 	cpu[cpuid]->cpu_flags = CPU_QUIESCED | CPU_OFFLINE | CPU_POWEROFF;
19107c478bd9Sstevel@tonic-gate 	cpu_set_state(cpu[cpuid]);
19117c478bd9Sstevel@tonic-gate 	retval = cpu_state_change_hooks(cpuid, CPU_CONFIG, CPU_UNCONFIG);
19127c478bd9Sstevel@tonic-gate 	if (retval != 0)
19137c478bd9Sstevel@tonic-gate 		(void) mp_cpu_unconfigure(cpuid);
19147c478bd9Sstevel@tonic-gate 
19157c478bd9Sstevel@tonic-gate 	return (retval);
19167c478bd9Sstevel@tonic-gate }
19177c478bd9Sstevel@tonic-gate 
19187c478bd9Sstevel@tonic-gate /*
19197c478bd9Sstevel@tonic-gate  * Routine used to cleanup a CPU that has been powered off.  This will
19207c478bd9Sstevel@tonic-gate  * destroy all per-cpu information related to this cpu.
19217c478bd9Sstevel@tonic-gate  */
19227c478bd9Sstevel@tonic-gate int
19237c478bd9Sstevel@tonic-gate cpu_unconfigure(int cpuid)
19247c478bd9Sstevel@tonic-gate {
19257c478bd9Sstevel@tonic-gate 	int error;
19267c478bd9Sstevel@tonic-gate 
19277c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
19287c478bd9Sstevel@tonic-gate 
19297c478bd9Sstevel@tonic-gate 	if (cpu[cpuid] == NULL) {
19307c478bd9Sstevel@tonic-gate 		return (ENODEV);
19317c478bd9Sstevel@tonic-gate 	}
19327c478bd9Sstevel@tonic-gate 
19337c478bd9Sstevel@tonic-gate 	if (cpu[cpuid]->cpu_flags == 0) {
19347c478bd9Sstevel@tonic-gate 		return (EALREADY);
19357c478bd9Sstevel@tonic-gate 	}
19367c478bd9Sstevel@tonic-gate 
19377c478bd9Sstevel@tonic-gate 	if ((cpu[cpuid]->cpu_flags & CPU_POWEROFF) == 0) {
19387c478bd9Sstevel@tonic-gate 		return (EBUSY);
19397c478bd9Sstevel@tonic-gate 	}
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate 	if (cpu[cpuid]->cpu_props != NULL) {
19427c478bd9Sstevel@tonic-gate 		(void) nvlist_free(cpu[cpuid]->cpu_props);
19437c478bd9Sstevel@tonic-gate 		cpu[cpuid]->cpu_props = NULL;
19447c478bd9Sstevel@tonic-gate 	}
19457c478bd9Sstevel@tonic-gate 
19467c478bd9Sstevel@tonic-gate 	error = cpu_state_change_hooks(cpuid, CPU_UNCONFIG, CPU_CONFIG);
19477c478bd9Sstevel@tonic-gate 
19487c478bd9Sstevel@tonic-gate 	if (error != 0)
19497c478bd9Sstevel@tonic-gate 		return (error);
19507c478bd9Sstevel@tonic-gate 
19517c478bd9Sstevel@tonic-gate 	return (mp_cpu_unconfigure(cpuid));
19527c478bd9Sstevel@tonic-gate }
19537c478bd9Sstevel@tonic-gate 
19547c478bd9Sstevel@tonic-gate /*
19557c478bd9Sstevel@tonic-gate  * Routines for registering and de-registering cpu_setup callback functions.
19567c478bd9Sstevel@tonic-gate  *
19577c478bd9Sstevel@tonic-gate  * Caller's context
19587c478bd9Sstevel@tonic-gate  *	These routines must not be called from a driver's attach(9E) or
19597c478bd9Sstevel@tonic-gate  *	detach(9E) entry point.
19607c478bd9Sstevel@tonic-gate  *
19617c478bd9Sstevel@tonic-gate  * NOTE: CPU callbacks should not block. They are called with cpu_lock held.
19627c478bd9Sstevel@tonic-gate  */
19637c478bd9Sstevel@tonic-gate 
19647c478bd9Sstevel@tonic-gate /*
19657c478bd9Sstevel@tonic-gate  * Ideally, these would be dynamically allocated and put into a linked
19667c478bd9Sstevel@tonic-gate  * list; however that is not feasible because the registration routine
19677c478bd9Sstevel@tonic-gate  * has to be available before the kmem allocator is working (in fact,
19687c478bd9Sstevel@tonic-gate  * it is called by the kmem allocator init code).  In any case, there
19697c478bd9Sstevel@tonic-gate  * are quite a few extra entries for future users.
19707c478bd9Sstevel@tonic-gate  */
1971*1aa15ad6Sjkennedy #define	NCPU_SETUPS	20
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate struct cpu_setup {
19747c478bd9Sstevel@tonic-gate 	cpu_setup_func_t *func;
19757c478bd9Sstevel@tonic-gate 	void *arg;
19767c478bd9Sstevel@tonic-gate } cpu_setups[NCPU_SETUPS];
19777c478bd9Sstevel@tonic-gate 
19787c478bd9Sstevel@tonic-gate void
19797c478bd9Sstevel@tonic-gate register_cpu_setup_func(cpu_setup_func_t *func, void *arg)
19807c478bd9Sstevel@tonic-gate {
19817c478bd9Sstevel@tonic-gate 	int i;
19827c478bd9Sstevel@tonic-gate 
19837c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate 	for (i = 0; i < NCPU_SETUPS; i++)
19867c478bd9Sstevel@tonic-gate 		if (cpu_setups[i].func == NULL)
19877c478bd9Sstevel@tonic-gate 		    break;
19887c478bd9Sstevel@tonic-gate 	if (i >= NCPU_SETUPS)
19897c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "Ran out of cpu_setup callback entries");
19907c478bd9Sstevel@tonic-gate 
19917c478bd9Sstevel@tonic-gate 	cpu_setups[i].func = func;
19927c478bd9Sstevel@tonic-gate 	cpu_setups[i].arg = arg;
19937c478bd9Sstevel@tonic-gate }
19947c478bd9Sstevel@tonic-gate 
19957c478bd9Sstevel@tonic-gate void
19967c478bd9Sstevel@tonic-gate unregister_cpu_setup_func(cpu_setup_func_t *func, void *arg)
19977c478bd9Sstevel@tonic-gate {
19987c478bd9Sstevel@tonic-gate 	int i;
19997c478bd9Sstevel@tonic-gate 
20007c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
20017c478bd9Sstevel@tonic-gate 
20027c478bd9Sstevel@tonic-gate 	for (i = 0; i < NCPU_SETUPS; i++)
20037c478bd9Sstevel@tonic-gate 		if ((cpu_setups[i].func == func) &&
20047c478bd9Sstevel@tonic-gate 		    (cpu_setups[i].arg == arg))
20057c478bd9Sstevel@tonic-gate 		    break;
20067c478bd9Sstevel@tonic-gate 	if (i >= NCPU_SETUPS)
20077c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "Could not find cpu_setup callback to "
20087c478bd9Sstevel@tonic-gate 		    "deregister");
20097c478bd9Sstevel@tonic-gate 
20107c478bd9Sstevel@tonic-gate 	cpu_setups[i].func = NULL;
20117c478bd9Sstevel@tonic-gate 	cpu_setups[i].arg = 0;
20127c478bd9Sstevel@tonic-gate }
20137c478bd9Sstevel@tonic-gate 
20147c478bd9Sstevel@tonic-gate /*
20157c478bd9Sstevel@tonic-gate  * Call any state change hooks for this CPU, ignore any errors.
20167c478bd9Sstevel@tonic-gate  */
20177c478bd9Sstevel@tonic-gate void
20187c478bd9Sstevel@tonic-gate cpu_state_change_notify(int id, cpu_setup_t what)
20197c478bd9Sstevel@tonic-gate {
20207c478bd9Sstevel@tonic-gate 	int i;
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
20237c478bd9Sstevel@tonic-gate 
20247c478bd9Sstevel@tonic-gate 	for (i = 0; i < NCPU_SETUPS; i++) {
20257c478bd9Sstevel@tonic-gate 		if (cpu_setups[i].func != NULL) {
20267c478bd9Sstevel@tonic-gate 			cpu_setups[i].func(what, id, cpu_setups[i].arg);
20277c478bd9Sstevel@tonic-gate 		}
20287c478bd9Sstevel@tonic-gate 	}
20297c478bd9Sstevel@tonic-gate }
20307c478bd9Sstevel@tonic-gate 
20317c478bd9Sstevel@tonic-gate /*
20327c478bd9Sstevel@tonic-gate  * Call any state change hooks for this CPU, undo it if error found.
20337c478bd9Sstevel@tonic-gate  */
20347c478bd9Sstevel@tonic-gate static int
20357c478bd9Sstevel@tonic-gate cpu_state_change_hooks(int id, cpu_setup_t what, cpu_setup_t undo)
20367c478bd9Sstevel@tonic-gate {
20377c478bd9Sstevel@tonic-gate 	int i;
20387c478bd9Sstevel@tonic-gate 	int retval = 0;
20397c478bd9Sstevel@tonic-gate 
20407c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
20417c478bd9Sstevel@tonic-gate 
20427c478bd9Sstevel@tonic-gate 	for (i = 0; i < NCPU_SETUPS; i++) {
20437c478bd9Sstevel@tonic-gate 		if (cpu_setups[i].func != NULL) {
20447c478bd9Sstevel@tonic-gate 			retval = cpu_setups[i].func(what, id,
20457c478bd9Sstevel@tonic-gate 			    cpu_setups[i].arg);
20467c478bd9Sstevel@tonic-gate 			if (retval) {
20477c478bd9Sstevel@tonic-gate 				for (i--; i >= 0; i--) {
20487c478bd9Sstevel@tonic-gate 					if (cpu_setups[i].func != NULL)
20497c478bd9Sstevel@tonic-gate 						cpu_setups[i].func(undo,
20507c478bd9Sstevel@tonic-gate 						    id, cpu_setups[i].arg);
20517c478bd9Sstevel@tonic-gate 				}
20527c478bd9Sstevel@tonic-gate 				break;
20537c478bd9Sstevel@tonic-gate 			}
20547c478bd9Sstevel@tonic-gate 		}
20557c478bd9Sstevel@tonic-gate 	}
20567c478bd9Sstevel@tonic-gate 	return (retval);
20577c478bd9Sstevel@tonic-gate }
20587c478bd9Sstevel@tonic-gate 
20597c478bd9Sstevel@tonic-gate /*
20607c478bd9Sstevel@tonic-gate  * Export information about this CPU via the kstat mechanism.
20617c478bd9Sstevel@tonic-gate  */
20627c478bd9Sstevel@tonic-gate static struct {
20637c478bd9Sstevel@tonic-gate 	kstat_named_t ci_state;
20647c478bd9Sstevel@tonic-gate 	kstat_named_t ci_state_begin;
20657c478bd9Sstevel@tonic-gate 	kstat_named_t ci_cpu_type;
20667c478bd9Sstevel@tonic-gate 	kstat_named_t ci_fpu_type;
20677c478bd9Sstevel@tonic-gate 	kstat_named_t ci_clock_MHz;
20687c478bd9Sstevel@tonic-gate 	kstat_named_t ci_chip_id;
20697c478bd9Sstevel@tonic-gate 	kstat_named_t ci_implementation;
20707c478bd9Sstevel@tonic-gate #ifdef __sparcv9
20717c478bd9Sstevel@tonic-gate 	kstat_named_t ci_device_ID;
20727c478bd9Sstevel@tonic-gate 	kstat_named_t ci_cpu_fru;
20737c478bd9Sstevel@tonic-gate #endif
20747c478bd9Sstevel@tonic-gate 	kstat_named_t ci_brandstr;
20757c478bd9Sstevel@tonic-gate } cpu_info_template = {
20767c478bd9Sstevel@tonic-gate 	{ "state",		KSTAT_DATA_CHAR },
20777c478bd9Sstevel@tonic-gate 	{ "state_begin",	KSTAT_DATA_LONG },
20787c478bd9Sstevel@tonic-gate 	{ "cpu_type",		KSTAT_DATA_CHAR },
20797c478bd9Sstevel@tonic-gate 	{ "fpu_type",		KSTAT_DATA_CHAR },
20807c478bd9Sstevel@tonic-gate 	{ "clock_MHz",		KSTAT_DATA_LONG },
20817c478bd9Sstevel@tonic-gate 	{ "chip_id",		KSTAT_DATA_LONG },
20827c478bd9Sstevel@tonic-gate 	{ "implementation",	KSTAT_DATA_STRING },
20837c478bd9Sstevel@tonic-gate #ifdef __sparcv9
20847c478bd9Sstevel@tonic-gate 	{ "device_ID",		KSTAT_DATA_UINT64 },
20857c478bd9Sstevel@tonic-gate 	{ "cpu_fru",		KSTAT_DATA_STRING },
20867c478bd9Sstevel@tonic-gate #endif
20877c478bd9Sstevel@tonic-gate 	{ "brand",		KSTAT_DATA_STRING },
20887c478bd9Sstevel@tonic-gate };
20897c478bd9Sstevel@tonic-gate 
20907c478bd9Sstevel@tonic-gate static kmutex_t cpu_info_template_lock;
20917c478bd9Sstevel@tonic-gate 
20927c478bd9Sstevel@tonic-gate static int
20937c478bd9Sstevel@tonic-gate cpu_info_kstat_update(kstat_t *ksp, int rw)
20947c478bd9Sstevel@tonic-gate {
20957c478bd9Sstevel@tonic-gate 	cpu_t	*cp = ksp->ks_private;
20967c478bd9Sstevel@tonic-gate 	const char *pi_state;
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
20997c478bd9Sstevel@tonic-gate 		return (EACCES);
21007c478bd9Sstevel@tonic-gate 
21017c478bd9Sstevel@tonic-gate 	switch (cp->cpu_type_info.pi_state) {
21027c478bd9Sstevel@tonic-gate 	case P_ONLINE:
21037c478bd9Sstevel@tonic-gate 		pi_state = PS_ONLINE;
21047c478bd9Sstevel@tonic-gate 		break;
21057c478bd9Sstevel@tonic-gate 	case P_POWEROFF:
21067c478bd9Sstevel@tonic-gate 		pi_state = PS_POWEROFF;
21077c478bd9Sstevel@tonic-gate 		break;
21087c478bd9Sstevel@tonic-gate 	case P_NOINTR:
21097c478bd9Sstevel@tonic-gate 		pi_state = PS_NOINTR;
21107c478bd9Sstevel@tonic-gate 		break;
21117c478bd9Sstevel@tonic-gate 	case P_FAULTED:
21127c478bd9Sstevel@tonic-gate 		pi_state = PS_FAULTED;
21137c478bd9Sstevel@tonic-gate 		break;
21147c478bd9Sstevel@tonic-gate 	case P_SPARE:
21157c478bd9Sstevel@tonic-gate 		pi_state = PS_SPARE;
21167c478bd9Sstevel@tonic-gate 		break;
21177c478bd9Sstevel@tonic-gate 	case P_OFFLINE:
21187c478bd9Sstevel@tonic-gate 		pi_state = PS_OFFLINE;
21197c478bd9Sstevel@tonic-gate 		break;
21207c478bd9Sstevel@tonic-gate 	default:
21217c478bd9Sstevel@tonic-gate 		pi_state = "unknown";
21227c478bd9Sstevel@tonic-gate 	}
21237c478bd9Sstevel@tonic-gate 	(void) strcpy(cpu_info_template.ci_state.value.c, pi_state);
21247c478bd9Sstevel@tonic-gate 	cpu_info_template.ci_state_begin.value.l = cp->cpu_state_begin;
21257c478bd9Sstevel@tonic-gate 	(void) strncpy(cpu_info_template.ci_cpu_type.value.c,
21267c478bd9Sstevel@tonic-gate 	    cp->cpu_type_info.pi_processor_type, 15);
21277c478bd9Sstevel@tonic-gate 	(void) strncpy(cpu_info_template.ci_fpu_type.value.c,
21287c478bd9Sstevel@tonic-gate 	    cp->cpu_type_info.pi_fputypes, 15);
21297c478bd9Sstevel@tonic-gate 	cpu_info_template.ci_clock_MHz.value.l = cp->cpu_type_info.pi_clock;
21307c478bd9Sstevel@tonic-gate 	cpu_info_template.ci_chip_id.value.l = chip_plat_get_chipid(cp);
21317c478bd9Sstevel@tonic-gate 	kstat_named_setstr(&cpu_info_template.ci_implementation,
21327c478bd9Sstevel@tonic-gate 	    cp->cpu_idstr);
21337c478bd9Sstevel@tonic-gate #ifdef __sparcv9
21347c478bd9Sstevel@tonic-gate 	cpu_info_template.ci_device_ID.value.ui64 =
21357c478bd9Sstevel@tonic-gate 	    cpunodes[cp->cpu_id].device_id;
21367c478bd9Sstevel@tonic-gate 	kstat_named_setstr(&cpu_info_template.ci_cpu_fru, cpu_fru_fmri(cp));
21377c478bd9Sstevel@tonic-gate #endif
21387c478bd9Sstevel@tonic-gate 	kstat_named_setstr(&cpu_info_template.ci_brandstr, cp->cpu_brandstr);
21397c478bd9Sstevel@tonic-gate 	return (0);
21407c478bd9Sstevel@tonic-gate }
21417c478bd9Sstevel@tonic-gate 
21427c478bd9Sstevel@tonic-gate static void
21437c478bd9Sstevel@tonic-gate cpu_info_kstat_create(cpu_t *cp)
21447c478bd9Sstevel@tonic-gate {
21457c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
21467c478bd9Sstevel@tonic-gate 
21477c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
21487c478bd9Sstevel@tonic-gate 
21497c478bd9Sstevel@tonic-gate 	if (pool_pset_enabled())
21507c478bd9Sstevel@tonic-gate 		zoneid = GLOBAL_ZONEID;
21517c478bd9Sstevel@tonic-gate 	else
21527c478bd9Sstevel@tonic-gate 		zoneid = ALL_ZONES;
21537c478bd9Sstevel@tonic-gate 	if ((cp->cpu_info_kstat = kstat_create_zone("cpu_info", cp->cpu_id,
21547c478bd9Sstevel@tonic-gate 	    NULL, "misc", KSTAT_TYPE_NAMED,
21557c478bd9Sstevel@tonic-gate 	    sizeof (cpu_info_template) / sizeof (kstat_named_t),
21567c478bd9Sstevel@tonic-gate 	    KSTAT_FLAG_VIRTUAL, zoneid)) != NULL) {
21577c478bd9Sstevel@tonic-gate 		cp->cpu_info_kstat->ks_data_size += 2 * CPU_IDSTRLEN;
21587c478bd9Sstevel@tonic-gate #ifdef __sparcv9
21597c478bd9Sstevel@tonic-gate 		cp->cpu_info_kstat->ks_data_size +=
21607c478bd9Sstevel@tonic-gate 		    strlen(cpu_fru_fmri(cp)) + 1;
21617c478bd9Sstevel@tonic-gate #endif
21627c478bd9Sstevel@tonic-gate 		cp->cpu_info_kstat->ks_lock = &cpu_info_template_lock;
21637c478bd9Sstevel@tonic-gate 		cp->cpu_info_kstat->ks_data = &cpu_info_template;
21647c478bd9Sstevel@tonic-gate 		cp->cpu_info_kstat->ks_private = cp;
21657c478bd9Sstevel@tonic-gate 		cp->cpu_info_kstat->ks_update = cpu_info_kstat_update;
21667c478bd9Sstevel@tonic-gate 		kstat_install(cp->cpu_info_kstat);
21677c478bd9Sstevel@tonic-gate 	}
21687c478bd9Sstevel@tonic-gate }
21697c478bd9Sstevel@tonic-gate 
21707c478bd9Sstevel@tonic-gate static void
21717c478bd9Sstevel@tonic-gate cpu_info_kstat_destroy(cpu_t *cp)
21727c478bd9Sstevel@tonic-gate {
21737c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
21747c478bd9Sstevel@tonic-gate 
21757c478bd9Sstevel@tonic-gate 	kstat_delete(cp->cpu_info_kstat);
21767c478bd9Sstevel@tonic-gate 	cp->cpu_info_kstat = NULL;
21777c478bd9Sstevel@tonic-gate }
21787c478bd9Sstevel@tonic-gate 
21797c478bd9Sstevel@tonic-gate /*
21807c478bd9Sstevel@tonic-gate  * Create and install kstats for the boot CPU.
21817c478bd9Sstevel@tonic-gate  */
21827c478bd9Sstevel@tonic-gate void
21837c478bd9Sstevel@tonic-gate cpu_kstat_init(cpu_t *cp)
21847c478bd9Sstevel@tonic-gate {
21857c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
21867c478bd9Sstevel@tonic-gate 	cpu_info_kstat_create(cp);
21877c478bd9Sstevel@tonic-gate 	cpu_stats_kstat_create(cp);
21887c478bd9Sstevel@tonic-gate 	cpu_create_intrstat(cp);
21897c478bd9Sstevel@tonic-gate 	chip_kstat_create(cp->cpu_chip);
21907c478bd9Sstevel@tonic-gate 	cpu_set_state(cp);
21917c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
21927c478bd9Sstevel@tonic-gate }
21937c478bd9Sstevel@tonic-gate 
21947c478bd9Sstevel@tonic-gate /*
21957c478bd9Sstevel@tonic-gate  * Make visible to the zone that subset of the cpu information that would be
21967c478bd9Sstevel@tonic-gate  * initialized when a cpu is configured (but still offline).
21977c478bd9Sstevel@tonic-gate  */
21987c478bd9Sstevel@tonic-gate void
21997c478bd9Sstevel@tonic-gate cpu_visibility_configure(cpu_t *cp, zone_t *zone)
22007c478bd9Sstevel@tonic-gate {
22017c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = zone ? zone->zone_id : ALL_ZONES;
22027c478bd9Sstevel@tonic-gate 
22037c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
22047c478bd9Sstevel@tonic-gate 	ASSERT(pool_pset_enabled());
22057c478bd9Sstevel@tonic-gate 	ASSERT(cp != NULL);
22067c478bd9Sstevel@tonic-gate 
22077c478bd9Sstevel@tonic-gate 	if (zoneid != ALL_ZONES && zoneid != GLOBAL_ZONEID) {
22087c478bd9Sstevel@tonic-gate 		zone->zone_ncpus++;
22097c478bd9Sstevel@tonic-gate 		ASSERT(zone->zone_ncpus <= ncpus);
22107c478bd9Sstevel@tonic-gate 	}
22117c478bd9Sstevel@tonic-gate 	if (cp->cpu_info_kstat != NULL)
22127c478bd9Sstevel@tonic-gate 		kstat_zone_add(cp->cpu_info_kstat, zoneid);
22137c478bd9Sstevel@tonic-gate }
22147c478bd9Sstevel@tonic-gate 
22157c478bd9Sstevel@tonic-gate /*
22167c478bd9Sstevel@tonic-gate  * Make visible to the zone that subset of the cpu information that would be
22177c478bd9Sstevel@tonic-gate  * initialized when a previously configured cpu is onlined.
22187c478bd9Sstevel@tonic-gate  */
22197c478bd9Sstevel@tonic-gate void
22207c478bd9Sstevel@tonic-gate cpu_visibility_online(cpu_t *cp, zone_t *zone)
22217c478bd9Sstevel@tonic-gate {
22227c478bd9Sstevel@tonic-gate 	kstat_t *ksp;
22237c478bd9Sstevel@tonic-gate 	char name[sizeof ("cpu_stat") + 10];	/* enough for 32-bit cpuids */
22247c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = zone ? zone->zone_id : ALL_ZONES;
22257c478bd9Sstevel@tonic-gate 	processorid_t cpun;
22267c478bd9Sstevel@tonic-gate 
22277c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
22287c478bd9Sstevel@tonic-gate 	ASSERT(pool_pset_enabled());
22297c478bd9Sstevel@tonic-gate 	ASSERT(cp != NULL);
22307c478bd9Sstevel@tonic-gate 	ASSERT(cpu_is_active(cp));
22317c478bd9Sstevel@tonic-gate 
22327c478bd9Sstevel@tonic-gate 	cpun = cp->cpu_id;
22337c478bd9Sstevel@tonic-gate 	if (zoneid != ALL_ZONES && zoneid != GLOBAL_ZONEID) {
22347c478bd9Sstevel@tonic-gate 		zone->zone_ncpus_online++;
22357c478bd9Sstevel@tonic-gate 		ASSERT(zone->zone_ncpus_online <= ncpus_online);
22367c478bd9Sstevel@tonic-gate 	}
22377c478bd9Sstevel@tonic-gate 	(void) snprintf(name, sizeof (name), "cpu_stat%d", cpun);
22387c478bd9Sstevel@tonic-gate 	if ((ksp = kstat_hold_byname("cpu_stat", cpun, name, ALL_ZONES))
22397c478bd9Sstevel@tonic-gate 	    != NULL) {
22407c478bd9Sstevel@tonic-gate 		kstat_zone_add(ksp, zoneid);
22417c478bd9Sstevel@tonic-gate 		kstat_rele(ksp);
22427c478bd9Sstevel@tonic-gate 	}
22437c478bd9Sstevel@tonic-gate 	if ((ksp = kstat_hold_byname("cpu", cpun, "sys", ALL_ZONES)) != NULL) {
22447c478bd9Sstevel@tonic-gate 		kstat_zone_add(ksp, zoneid);
22457c478bd9Sstevel@tonic-gate 		kstat_rele(ksp);
22467c478bd9Sstevel@tonic-gate 	}
22477c478bd9Sstevel@tonic-gate 	if ((ksp = kstat_hold_byname("cpu", cpun, "vm", ALL_ZONES)) != NULL) {
22487c478bd9Sstevel@tonic-gate 		kstat_zone_add(ksp, zoneid);
22497c478bd9Sstevel@tonic-gate 		kstat_rele(ksp);
22507c478bd9Sstevel@tonic-gate 	}
22517c478bd9Sstevel@tonic-gate 	if ((ksp = kstat_hold_byname("cpu", cpun, "intrstat", ALL_ZONES)) !=
22527c478bd9Sstevel@tonic-gate 	    NULL) {
22537c478bd9Sstevel@tonic-gate 		kstat_zone_add(ksp, zoneid);
22547c478bd9Sstevel@tonic-gate 		kstat_rele(ksp);
22557c478bd9Sstevel@tonic-gate 	}
22567c478bd9Sstevel@tonic-gate }
22577c478bd9Sstevel@tonic-gate 
22587c478bd9Sstevel@tonic-gate /*
22597c478bd9Sstevel@tonic-gate  * Update relevant kstats such that cpu is now visible to processes
22607c478bd9Sstevel@tonic-gate  * executing in specified zone.
22617c478bd9Sstevel@tonic-gate  */
22627c478bd9Sstevel@tonic-gate void
22637c478bd9Sstevel@tonic-gate cpu_visibility_add(cpu_t *cp, zone_t *zone)
22647c478bd9Sstevel@tonic-gate {
22657c478bd9Sstevel@tonic-gate 	cpu_visibility_configure(cp, zone);
22667c478bd9Sstevel@tonic-gate 	if (cpu_is_active(cp))
22677c478bd9Sstevel@tonic-gate 		cpu_visibility_online(cp, zone);
22687c478bd9Sstevel@tonic-gate }
22697c478bd9Sstevel@tonic-gate 
22707c478bd9Sstevel@tonic-gate /*
22717c478bd9Sstevel@tonic-gate  * Make invisible to the zone that subset of the cpu information that would be
22727c478bd9Sstevel@tonic-gate  * torn down when a previously offlined cpu is unconfigured.
22737c478bd9Sstevel@tonic-gate  */
22747c478bd9Sstevel@tonic-gate void
22757c478bd9Sstevel@tonic-gate cpu_visibility_unconfigure(cpu_t *cp, zone_t *zone)
22767c478bd9Sstevel@tonic-gate {
22777c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = zone ? zone->zone_id : ALL_ZONES;
22787c478bd9Sstevel@tonic-gate 
22797c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
22807c478bd9Sstevel@tonic-gate 	ASSERT(pool_pset_enabled());
22817c478bd9Sstevel@tonic-gate 	ASSERT(cp != NULL);
22827c478bd9Sstevel@tonic-gate 
22837c478bd9Sstevel@tonic-gate 	if (zoneid != ALL_ZONES && zoneid != GLOBAL_ZONEID) {
22847c478bd9Sstevel@tonic-gate 		ASSERT(zone->zone_ncpus != 0);
22857c478bd9Sstevel@tonic-gate 		zone->zone_ncpus--;
22867c478bd9Sstevel@tonic-gate 	}
22877c478bd9Sstevel@tonic-gate 	if (cp->cpu_info_kstat)
22887c478bd9Sstevel@tonic-gate 		kstat_zone_remove(cp->cpu_info_kstat, zoneid);
22897c478bd9Sstevel@tonic-gate }
22907c478bd9Sstevel@tonic-gate 
22917c478bd9Sstevel@tonic-gate /*
22927c478bd9Sstevel@tonic-gate  * Make invisible to the zone that subset of the cpu information that would be
22937c478bd9Sstevel@tonic-gate  * torn down when a cpu is offlined (but still configured).
22947c478bd9Sstevel@tonic-gate  */
22957c478bd9Sstevel@tonic-gate void
22967c478bd9Sstevel@tonic-gate cpu_visibility_offline(cpu_t *cp, zone_t *zone)
22977c478bd9Sstevel@tonic-gate {
22987c478bd9Sstevel@tonic-gate 	kstat_t *ksp;
22997c478bd9Sstevel@tonic-gate 	char name[sizeof ("cpu_stat") + 10];	/* enough for 32-bit cpuids */
23007c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = zone ? zone->zone_id : ALL_ZONES;
23017c478bd9Sstevel@tonic-gate 	processorid_t cpun;
23027c478bd9Sstevel@tonic-gate 
23037c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
23047c478bd9Sstevel@tonic-gate 	ASSERT(pool_pset_enabled());
23057c478bd9Sstevel@tonic-gate 	ASSERT(cp != NULL);
23067c478bd9Sstevel@tonic-gate 	ASSERT(cpu_is_active(cp));
23077c478bd9Sstevel@tonic-gate 
23087c478bd9Sstevel@tonic-gate 	cpun = cp->cpu_id;
23097c478bd9Sstevel@tonic-gate 	if (zoneid != ALL_ZONES && zoneid != GLOBAL_ZONEID) {
23107c478bd9Sstevel@tonic-gate 		ASSERT(zone->zone_ncpus_online != 0);
23117c478bd9Sstevel@tonic-gate 		zone->zone_ncpus_online--;
23127c478bd9Sstevel@tonic-gate 	}
23137c478bd9Sstevel@tonic-gate 
23147c478bd9Sstevel@tonic-gate 	if ((ksp = kstat_hold_byname("cpu", cpun, "intrstat", ALL_ZONES)) !=
23157c478bd9Sstevel@tonic-gate 	    NULL) {
23167c478bd9Sstevel@tonic-gate 		kstat_zone_remove(ksp, zoneid);
23177c478bd9Sstevel@tonic-gate 		kstat_rele(ksp);
23187c478bd9Sstevel@tonic-gate 	}
23197c478bd9Sstevel@tonic-gate 	if ((ksp = kstat_hold_byname("cpu", cpun, "vm", ALL_ZONES)) != NULL) {
23207c478bd9Sstevel@tonic-gate 		kstat_zone_remove(ksp, zoneid);
23217c478bd9Sstevel@tonic-gate 		kstat_rele(ksp);
23227c478bd9Sstevel@tonic-gate 	}
23237c478bd9Sstevel@tonic-gate 	if ((ksp = kstat_hold_byname("cpu", cpun, "sys", ALL_ZONES)) != NULL) {
23247c478bd9Sstevel@tonic-gate 		kstat_zone_remove(ksp, zoneid);
23257c478bd9Sstevel@tonic-gate 		kstat_rele(ksp);
23267c478bd9Sstevel@tonic-gate 	}
23277c478bd9Sstevel@tonic-gate 	(void) snprintf(name, sizeof (name), "cpu_stat%d", cpun);
23287c478bd9Sstevel@tonic-gate 	if ((ksp = kstat_hold_byname("cpu_stat", cpun, name, ALL_ZONES))
23297c478bd9Sstevel@tonic-gate 	    != NULL) {
23307c478bd9Sstevel@tonic-gate 		kstat_zone_remove(ksp, zoneid);
23317c478bd9Sstevel@tonic-gate 		kstat_rele(ksp);
23327c478bd9Sstevel@tonic-gate 	}
23337c478bd9Sstevel@tonic-gate }
23347c478bd9Sstevel@tonic-gate 
23357c478bd9Sstevel@tonic-gate /*
23367c478bd9Sstevel@tonic-gate  * Update relevant kstats such that cpu is no longer visible to processes
23377c478bd9Sstevel@tonic-gate  * executing in specified zone.
23387c478bd9Sstevel@tonic-gate  */
23397c478bd9Sstevel@tonic-gate void
23407c478bd9Sstevel@tonic-gate cpu_visibility_remove(cpu_t *cp, zone_t *zone)
23417c478bd9Sstevel@tonic-gate {
23427c478bd9Sstevel@tonic-gate 	if (cpu_is_active(cp))
23437c478bd9Sstevel@tonic-gate 		cpu_visibility_offline(cp, zone);
23447c478bd9Sstevel@tonic-gate 	cpu_visibility_unconfigure(cp, zone);
23457c478bd9Sstevel@tonic-gate }
23467c478bd9Sstevel@tonic-gate 
23477c478bd9Sstevel@tonic-gate /*
23487c478bd9Sstevel@tonic-gate  * Bind a thread to a CPU as requested.
23497c478bd9Sstevel@tonic-gate  */
23507c478bd9Sstevel@tonic-gate int
23517c478bd9Sstevel@tonic-gate cpu_bind_thread(kthread_id_t tp, processorid_t bind, processorid_t *obind,
23527c478bd9Sstevel@tonic-gate     int *error)
23537c478bd9Sstevel@tonic-gate {
23547c478bd9Sstevel@tonic-gate 	processorid_t	binding;
23557c478bd9Sstevel@tonic-gate 	cpu_t		*cp;
23567c478bd9Sstevel@tonic-gate 
23577c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
23587c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ttoproc(tp)->p_lock));
23597c478bd9Sstevel@tonic-gate 
23607c478bd9Sstevel@tonic-gate 	thread_lock(tp);
23617c478bd9Sstevel@tonic-gate 
23627c478bd9Sstevel@tonic-gate 	/*
23637c478bd9Sstevel@tonic-gate 	 * Record old binding, but change the obind, which was initialized
23647c478bd9Sstevel@tonic-gate 	 * to PBIND_NONE, only if this thread has a binding.  This avoids
23657c478bd9Sstevel@tonic-gate 	 * reporting PBIND_NONE for a process when some LWPs are bound.
23667c478bd9Sstevel@tonic-gate 	 */
23677c478bd9Sstevel@tonic-gate 	binding = tp->t_bind_cpu;
23687c478bd9Sstevel@tonic-gate 	if (binding != PBIND_NONE)
23697c478bd9Sstevel@tonic-gate 		*obind = binding;	/* record old binding */
23707c478bd9Sstevel@tonic-gate 
23717c478bd9Sstevel@tonic-gate 	if (bind == PBIND_QUERY) {
23727c478bd9Sstevel@tonic-gate 		thread_unlock(tp);
23737c478bd9Sstevel@tonic-gate 		return (0);
23747c478bd9Sstevel@tonic-gate 	}
23757c478bd9Sstevel@tonic-gate 
23767c478bd9Sstevel@tonic-gate 	/*
23777c478bd9Sstevel@tonic-gate 	 * If this thread/LWP cannot be bound because of permission
23787c478bd9Sstevel@tonic-gate 	 * problems, just note that and return success so that the
23797c478bd9Sstevel@tonic-gate 	 * other threads/LWPs will be bound.  This is the way
23807c478bd9Sstevel@tonic-gate 	 * processor_bind() is defined to work.
23817c478bd9Sstevel@tonic-gate 	 *
23827c478bd9Sstevel@tonic-gate 	 * Binding will get EPERM if the thread is of system class
23837c478bd9Sstevel@tonic-gate 	 * or hasprocperm() fails.
23847c478bd9Sstevel@tonic-gate 	 */
23857c478bd9Sstevel@tonic-gate 	if (tp->t_cid == 0 || !hasprocperm(tp->t_cred, CRED())) {
23867c478bd9Sstevel@tonic-gate 		*error = EPERM;
23877c478bd9Sstevel@tonic-gate 		thread_unlock(tp);
23887c478bd9Sstevel@tonic-gate 		return (0);
23897c478bd9Sstevel@tonic-gate 	}
23907c478bd9Sstevel@tonic-gate 
23917c478bd9Sstevel@tonic-gate 	binding = bind;
23927c478bd9Sstevel@tonic-gate 	if (binding != PBIND_NONE) {
23937c478bd9Sstevel@tonic-gate 		cp = cpu[binding];
23947c478bd9Sstevel@tonic-gate 		/*
23957c478bd9Sstevel@tonic-gate 		 * Make sure binding is in right partition.
23967c478bd9Sstevel@tonic-gate 		 */
23977c478bd9Sstevel@tonic-gate 		if (tp->t_cpupart != cp->cpu_part) {
23987c478bd9Sstevel@tonic-gate 			*error = EINVAL;
23997c478bd9Sstevel@tonic-gate 			thread_unlock(tp);
24007c478bd9Sstevel@tonic-gate 			return (0);
24017c478bd9Sstevel@tonic-gate 		}
24027c478bd9Sstevel@tonic-gate 	}
24037c478bd9Sstevel@tonic-gate 	tp->t_bind_cpu = binding;	/* set new binding */
24047c478bd9Sstevel@tonic-gate 
24057c478bd9Sstevel@tonic-gate 	/*
24067c478bd9Sstevel@tonic-gate 	 * If there is no system-set reason for affinity, set
24077c478bd9Sstevel@tonic-gate 	 * the t_bound_cpu field to reflect the binding.
24087c478bd9Sstevel@tonic-gate 	 */
24097c478bd9Sstevel@tonic-gate 	if (tp->t_affinitycnt == 0) {
24107c478bd9Sstevel@tonic-gate 		if (binding == PBIND_NONE) {
24117c478bd9Sstevel@tonic-gate 			/*
24127c478bd9Sstevel@tonic-gate 			 * We may need to adjust disp_max_unbound_pri
24137c478bd9Sstevel@tonic-gate 			 * since we're becoming unbound.
24147c478bd9Sstevel@tonic-gate 			 */
24157c478bd9Sstevel@tonic-gate 			disp_adjust_unbound_pri(tp);
24167c478bd9Sstevel@tonic-gate 
24177c478bd9Sstevel@tonic-gate 			tp->t_bound_cpu = NULL;	/* set new binding */
24187c478bd9Sstevel@tonic-gate 
24197c478bd9Sstevel@tonic-gate 			/*
24207c478bd9Sstevel@tonic-gate 			 * Move thread to lgroup with strongest affinity
24217c478bd9Sstevel@tonic-gate 			 * after unbinding
24227c478bd9Sstevel@tonic-gate 			 */
24237c478bd9Sstevel@tonic-gate 			if (tp->t_lgrp_affinity)
24247c478bd9Sstevel@tonic-gate 				lgrp_move_thread(tp,
24257c478bd9Sstevel@tonic-gate 				    lgrp_choose(tp, tp->t_cpupart), 1);
24267c478bd9Sstevel@tonic-gate 
24277c478bd9Sstevel@tonic-gate 			if (tp->t_state == TS_ONPROC &&
24287c478bd9Sstevel@tonic-gate 			    tp->t_cpu->cpu_part != tp->t_cpupart)
24297c478bd9Sstevel@tonic-gate 				cpu_surrender(tp);
24307c478bd9Sstevel@tonic-gate 		} else {
24317c478bd9Sstevel@tonic-gate 			lpl_t	*lpl;
24327c478bd9Sstevel@tonic-gate 
24337c478bd9Sstevel@tonic-gate 			tp->t_bound_cpu = cp;
24347c478bd9Sstevel@tonic-gate 			ASSERT(cp->cpu_lpl != NULL);
24357c478bd9Sstevel@tonic-gate 
24367c478bd9Sstevel@tonic-gate 			/*
24377c478bd9Sstevel@tonic-gate 			 * Set home to lgroup with most affinity containing CPU
24387c478bd9Sstevel@tonic-gate 			 * that thread is being bound or minimum bounding
24397c478bd9Sstevel@tonic-gate 			 * lgroup if no affinities set
24407c478bd9Sstevel@tonic-gate 			 */
24417c478bd9Sstevel@tonic-gate 			if (tp->t_lgrp_affinity)
24427c478bd9Sstevel@tonic-gate 				lpl = lgrp_affinity_best(tp, tp->t_cpupart, 0);
24437c478bd9Sstevel@tonic-gate 			else
24447c478bd9Sstevel@tonic-gate 				lpl = cp->cpu_lpl;
24457c478bd9Sstevel@tonic-gate 
24467c478bd9Sstevel@tonic-gate 			if (tp->t_lpl != lpl) {
24477c478bd9Sstevel@tonic-gate 				/* can't grab cpu_lock */
24487c478bd9Sstevel@tonic-gate 				lgrp_move_thread(tp, lpl, 1);
24497c478bd9Sstevel@tonic-gate 			}
24507c478bd9Sstevel@tonic-gate 
24517c478bd9Sstevel@tonic-gate 			/*
24527c478bd9Sstevel@tonic-gate 			 * Make the thread switch to the bound CPU.
24537c478bd9Sstevel@tonic-gate 			 * If the thread is runnable, we need to
24547c478bd9Sstevel@tonic-gate 			 * requeue it even if t_cpu is already set
24557c478bd9Sstevel@tonic-gate 			 * to the right CPU, since it may be on a
24567c478bd9Sstevel@tonic-gate 			 * kpreempt queue and need to move to a local
24577c478bd9Sstevel@tonic-gate 			 * queue.  We could check t_disp_queue to
24587c478bd9Sstevel@tonic-gate 			 * avoid unnecessary overhead if it's already
24597c478bd9Sstevel@tonic-gate 			 * on the right queue, but since this isn't
24607c478bd9Sstevel@tonic-gate 			 * a performance-critical operation it doesn't
24617c478bd9Sstevel@tonic-gate 			 * seem worth the extra code and complexity.
24627c478bd9Sstevel@tonic-gate 			 *
24637c478bd9Sstevel@tonic-gate 			 * If the thread is weakbound to the cpu then it will
24647c478bd9Sstevel@tonic-gate 			 * resist the new binding request until the weak
24657c478bd9Sstevel@tonic-gate 			 * binding drops.  The cpu_surrender or requeueing
24667c478bd9Sstevel@tonic-gate 			 * below could be skipped in such cases (since it
24677c478bd9Sstevel@tonic-gate 			 * will have no effect), but that would require
24687c478bd9Sstevel@tonic-gate 			 * thread_allowmigrate to acquire thread_lock so
24697c478bd9Sstevel@tonic-gate 			 * we'll take the very occasional hit here instead.
24707c478bd9Sstevel@tonic-gate 			 */
24717c478bd9Sstevel@tonic-gate 			if (tp->t_state == TS_ONPROC) {
24727c478bd9Sstevel@tonic-gate 				cpu_surrender(tp);
24737c478bd9Sstevel@tonic-gate 			} else if (tp->t_state == TS_RUN) {
24747c478bd9Sstevel@tonic-gate 				cpu_t *ocp = tp->t_cpu;
24757c478bd9Sstevel@tonic-gate 
24767c478bd9Sstevel@tonic-gate 				(void) dispdeq(tp);
24777c478bd9Sstevel@tonic-gate 				setbackdq(tp);
24787c478bd9Sstevel@tonic-gate 				/*
24797c478bd9Sstevel@tonic-gate 				 * Either on the bound CPU's disp queue now,
24807c478bd9Sstevel@tonic-gate 				 * or swapped out or on the swap queue.
24817c478bd9Sstevel@tonic-gate 				 */
24827c478bd9Sstevel@tonic-gate 				ASSERT(tp->t_disp_queue == cp->cpu_disp ||
24837c478bd9Sstevel@tonic-gate 				    tp->t_weakbound_cpu == ocp ||
24847c478bd9Sstevel@tonic-gate 				    (tp->t_schedflag & (TS_LOAD | TS_ON_SWAPQ))
24857c478bd9Sstevel@tonic-gate 				    != TS_LOAD);
24867c478bd9Sstevel@tonic-gate 			}
24877c478bd9Sstevel@tonic-gate 		}
24887c478bd9Sstevel@tonic-gate 	}
24897c478bd9Sstevel@tonic-gate 
24907c478bd9Sstevel@tonic-gate 	/*
24917c478bd9Sstevel@tonic-gate 	 * Our binding has changed; set TP_CHANGEBIND.
24927c478bd9Sstevel@tonic-gate 	 */
24937c478bd9Sstevel@tonic-gate 	tp->t_proc_flag |= TP_CHANGEBIND;
24947c478bd9Sstevel@tonic-gate 	aston(tp);
24957c478bd9Sstevel@tonic-gate 
24967c478bd9Sstevel@tonic-gate 	thread_unlock(tp);
24977c478bd9Sstevel@tonic-gate 
24987c478bd9Sstevel@tonic-gate 	return (0);
24997c478bd9Sstevel@tonic-gate }
25007c478bd9Sstevel@tonic-gate 
25017c478bd9Sstevel@tonic-gate #if CPUSET_WORDS > 1
25027c478bd9Sstevel@tonic-gate 
25037c478bd9Sstevel@tonic-gate /*
25047c478bd9Sstevel@tonic-gate  * Functions for implementing cpuset operations when a cpuset is more
25057c478bd9Sstevel@tonic-gate  * than one word.  On platforms where a cpuset is a single word these
25067c478bd9Sstevel@tonic-gate  * are implemented as macros in cpuvar.h.
25077c478bd9Sstevel@tonic-gate  */
25087c478bd9Sstevel@tonic-gate 
25097c478bd9Sstevel@tonic-gate void
25107c478bd9Sstevel@tonic-gate cpuset_all(cpuset_t *s)
25117c478bd9Sstevel@tonic-gate {
25127c478bd9Sstevel@tonic-gate 	int i;
25137c478bd9Sstevel@tonic-gate 
25147c478bd9Sstevel@tonic-gate 	for (i = 0; i < CPUSET_WORDS; i++)
25157c478bd9Sstevel@tonic-gate 		s->cpub[i] = ~0UL;
25167c478bd9Sstevel@tonic-gate }
25177c478bd9Sstevel@tonic-gate 
25187c478bd9Sstevel@tonic-gate void
25197c478bd9Sstevel@tonic-gate cpuset_all_but(cpuset_t *s, uint_t cpu)
25207c478bd9Sstevel@tonic-gate {
25217c478bd9Sstevel@tonic-gate 	cpuset_all(s);
25227c478bd9Sstevel@tonic-gate 	CPUSET_DEL(*s, cpu);
25237c478bd9Sstevel@tonic-gate }
25247c478bd9Sstevel@tonic-gate 
25257c478bd9Sstevel@tonic-gate void
25267c478bd9Sstevel@tonic-gate cpuset_only(cpuset_t *s, uint_t cpu)
25277c478bd9Sstevel@tonic-gate {
25287c478bd9Sstevel@tonic-gate 	CPUSET_ZERO(*s);
25297c478bd9Sstevel@tonic-gate 	CPUSET_ADD(*s, cpu);
25307c478bd9Sstevel@tonic-gate }
25317c478bd9Sstevel@tonic-gate 
25327c478bd9Sstevel@tonic-gate int
25337c478bd9Sstevel@tonic-gate cpuset_isnull(cpuset_t *s)
25347c478bd9Sstevel@tonic-gate {
25357c478bd9Sstevel@tonic-gate 	int i;
25367c478bd9Sstevel@tonic-gate 
25377c478bd9Sstevel@tonic-gate 	for (i = 0; i < CPUSET_WORDS; i++)
25387c478bd9Sstevel@tonic-gate 		if (s->cpub[i] != 0)
25397c478bd9Sstevel@tonic-gate 			return (0);
25407c478bd9Sstevel@tonic-gate 	return (1);
25417c478bd9Sstevel@tonic-gate }
25427c478bd9Sstevel@tonic-gate 
25437c478bd9Sstevel@tonic-gate int
25447c478bd9Sstevel@tonic-gate cpuset_cmp(cpuset_t *s1, cpuset_t *s2)
25457c478bd9Sstevel@tonic-gate {
25467c478bd9Sstevel@tonic-gate 	int i;
25477c478bd9Sstevel@tonic-gate 
25487c478bd9Sstevel@tonic-gate 	for (i = 0; i < CPUSET_WORDS; i++)
25497c478bd9Sstevel@tonic-gate 		if (s1->cpub[i] != s2->cpub[i])
25507c478bd9Sstevel@tonic-gate 			return (0);
25517c478bd9Sstevel@tonic-gate 	return (1);
25527c478bd9Sstevel@tonic-gate }
25537c478bd9Sstevel@tonic-gate 
25547c478bd9Sstevel@tonic-gate uint_t
25557c478bd9Sstevel@tonic-gate cpuset_find(cpuset_t *s)
25567c478bd9Sstevel@tonic-gate {
25577c478bd9Sstevel@tonic-gate 
25587c478bd9Sstevel@tonic-gate 	uint_t	i;
25597c478bd9Sstevel@tonic-gate 	uint_t	cpu = (uint_t)-1;
25607c478bd9Sstevel@tonic-gate 
25617c478bd9Sstevel@tonic-gate 	/*
25627c478bd9Sstevel@tonic-gate 	 * Find a cpu in the cpuset
25637c478bd9Sstevel@tonic-gate 	 */
25647c478bd9Sstevel@tonic-gate 	for (i = 0; (i < CPUSET_WORDS && cpu == (uint_t)-1); i++)
25657c478bd9Sstevel@tonic-gate 		cpu = (uint_t)(lowbit(s->cpub[i]) - 1);
25667c478bd9Sstevel@tonic-gate 	return (cpu);
25677c478bd9Sstevel@tonic-gate }
25687c478bd9Sstevel@tonic-gate 
25697c478bd9Sstevel@tonic-gate #endif	/* CPUSET_WORDS */
25707c478bd9Sstevel@tonic-gate 
25717c478bd9Sstevel@tonic-gate /*
25727c478bd9Sstevel@tonic-gate  * Unbind all user threads bound to a given CPU.
25737c478bd9Sstevel@tonic-gate  */
25747c478bd9Sstevel@tonic-gate int
25757c478bd9Sstevel@tonic-gate cpu_unbind(processorid_t cpu)
25767c478bd9Sstevel@tonic-gate {
25777c478bd9Sstevel@tonic-gate 	processorid_t obind;
25787c478bd9Sstevel@tonic-gate 	kthread_t *tp;
25797c478bd9Sstevel@tonic-gate 	int ret = 0;
25807c478bd9Sstevel@tonic-gate 	proc_t *pp;
25817c478bd9Sstevel@tonic-gate 	int err, berr = 0;
25827c478bd9Sstevel@tonic-gate 
25837c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
25847c478bd9Sstevel@tonic-gate 
25857c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
25867c478bd9Sstevel@tonic-gate 	for (pp = practive; pp != NULL; pp = pp->p_next) {
25877c478bd9Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
25887c478bd9Sstevel@tonic-gate 		tp = pp->p_tlist;
25897c478bd9Sstevel@tonic-gate 		/*
25907c478bd9Sstevel@tonic-gate 		 * Skip zombies, kernel processes, and processes in
25917c478bd9Sstevel@tonic-gate 		 * other zones, if called from a non-global zone.
25927c478bd9Sstevel@tonic-gate 		 */
25937c478bd9Sstevel@tonic-gate 		if (tp == NULL || (pp->p_flag & SSYS) ||
25947c478bd9Sstevel@tonic-gate 		    !HASZONEACCESS(curproc, pp->p_zone->zone_id)) {
25957c478bd9Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
25967c478bd9Sstevel@tonic-gate 			continue;
25977c478bd9Sstevel@tonic-gate 		}
25987c478bd9Sstevel@tonic-gate 		do {
25997c478bd9Sstevel@tonic-gate 			if (tp->t_bind_cpu != cpu)
26007c478bd9Sstevel@tonic-gate 				continue;
26017c478bd9Sstevel@tonic-gate 			err = cpu_bind_thread(tp, PBIND_NONE, &obind, &berr);
26027c478bd9Sstevel@tonic-gate 			if (ret == 0)
26037c478bd9Sstevel@tonic-gate 				ret = err;
26047c478bd9Sstevel@tonic-gate 		} while ((tp = tp->t_forw) != pp->p_tlist);
26057c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
26067c478bd9Sstevel@tonic-gate 	}
26077c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
26087c478bd9Sstevel@tonic-gate 	if (ret == 0)
26097c478bd9Sstevel@tonic-gate 		ret = berr;
26107c478bd9Sstevel@tonic-gate 	return (ret);
26117c478bd9Sstevel@tonic-gate }
26127c478bd9Sstevel@tonic-gate 
26137c478bd9Sstevel@tonic-gate 
26147c478bd9Sstevel@tonic-gate /*
26157c478bd9Sstevel@tonic-gate  * Destroy all remaining bound threads on a cpu.
26167c478bd9Sstevel@tonic-gate  */
26177c478bd9Sstevel@tonic-gate void
26187c478bd9Sstevel@tonic-gate cpu_destroy_bound_threads(cpu_t *cp)
26197c478bd9Sstevel@tonic-gate {
26207c478bd9Sstevel@tonic-gate 	extern id_t syscid;
26217c478bd9Sstevel@tonic-gate 	register kthread_id_t	t, tlist, tnext;
26227c478bd9Sstevel@tonic-gate 
26237c478bd9Sstevel@tonic-gate 	/*
26247c478bd9Sstevel@tonic-gate 	 * Destroy all remaining bound threads on the cpu.  This
26257c478bd9Sstevel@tonic-gate 	 * should include both the interrupt threads and the idle thread.
26267c478bd9Sstevel@tonic-gate 	 * This requires some care, since we need to traverse the
26277c478bd9Sstevel@tonic-gate 	 * thread list with the pidlock mutex locked, but thread_free
26287c478bd9Sstevel@tonic-gate 	 * also locks the pidlock mutex.  So, we collect the threads
26297c478bd9Sstevel@tonic-gate 	 * we're going to reap in a list headed by "tlist", then we
26307c478bd9Sstevel@tonic-gate 	 * unlock the pidlock mutex and traverse the tlist list,
26317c478bd9Sstevel@tonic-gate 	 * doing thread_free's on the thread's.	 Simple, n'est pas?
26327c478bd9Sstevel@tonic-gate 	 * Also, this depends on thread_free not mucking with the
26337c478bd9Sstevel@tonic-gate 	 * t_next and t_prev links of the thread.
26347c478bd9Sstevel@tonic-gate 	 */
26357c478bd9Sstevel@tonic-gate 
26367c478bd9Sstevel@tonic-gate 	if ((t = curthread) != NULL) {
26377c478bd9Sstevel@tonic-gate 
26387c478bd9Sstevel@tonic-gate 		tlist = NULL;
26397c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
26407c478bd9Sstevel@tonic-gate 		do {
26417c478bd9Sstevel@tonic-gate 			tnext = t->t_next;
26427c478bd9Sstevel@tonic-gate 			if (t->t_bound_cpu == cp) {
26437c478bd9Sstevel@tonic-gate 
26447c478bd9Sstevel@tonic-gate 				/*
26457c478bd9Sstevel@tonic-gate 				 * We've found a bound thread, carefully unlink
26467c478bd9Sstevel@tonic-gate 				 * it out of the thread list, and add it to
26477c478bd9Sstevel@tonic-gate 				 * our "tlist".	 We "know" we don't have to
26487c478bd9Sstevel@tonic-gate 				 * worry about unlinking curthread (the thread
26497c478bd9Sstevel@tonic-gate 				 * that is executing this code).
26507c478bd9Sstevel@tonic-gate 				 */
26517c478bd9Sstevel@tonic-gate 				t->t_next->t_prev = t->t_prev;
26527c478bd9Sstevel@tonic-gate 				t->t_prev->t_next = t->t_next;
26537c478bd9Sstevel@tonic-gate 				t->t_next = tlist;
26547c478bd9Sstevel@tonic-gate 				tlist = t;
26557c478bd9Sstevel@tonic-gate 				ASSERT(t->t_cid == syscid);
26567c478bd9Sstevel@tonic-gate 				/* wake up anyone blocked in thread_join */
26577c478bd9Sstevel@tonic-gate 				cv_broadcast(&t->t_joincv);
26587c478bd9Sstevel@tonic-gate 				/*
26597c478bd9Sstevel@tonic-gate 				 * t_lwp set by interrupt threads and not
26607c478bd9Sstevel@tonic-gate 				 * cleared.
26617c478bd9Sstevel@tonic-gate 				 */
26627c478bd9Sstevel@tonic-gate 				t->t_lwp = NULL;
26637c478bd9Sstevel@tonic-gate 				/*
26647c478bd9Sstevel@tonic-gate 				 * Pause and idle threads always have
26657c478bd9Sstevel@tonic-gate 				 * t_state set to TS_ONPROC.
26667c478bd9Sstevel@tonic-gate 				 */
26677c478bd9Sstevel@tonic-gate 				t->t_state = TS_FREE;
26687c478bd9Sstevel@tonic-gate 				t->t_prev = NULL;	/* Just in case */
26697c478bd9Sstevel@tonic-gate 			}
26707c478bd9Sstevel@tonic-gate 
26717c478bd9Sstevel@tonic-gate 		} while ((t = tnext) != curthread);
26727c478bd9Sstevel@tonic-gate 
26737c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
26747c478bd9Sstevel@tonic-gate 
26757c478bd9Sstevel@tonic-gate 
26767c478bd9Sstevel@tonic-gate 		for (t = tlist; t != NULL; t = tnext) {
26777c478bd9Sstevel@tonic-gate 			tnext = t->t_next;
26787c478bd9Sstevel@tonic-gate 			thread_free(t);
26797c478bd9Sstevel@tonic-gate 		}
26807c478bd9Sstevel@tonic-gate 	}
26817c478bd9Sstevel@tonic-gate }
26827c478bd9Sstevel@tonic-gate 
26837c478bd9Sstevel@tonic-gate /*
26847c478bd9Sstevel@tonic-gate  * processor_info(2) and p_online(2) status support functions
26857c478bd9Sstevel@tonic-gate  *   The constants returned by the cpu_get_state() and cpu_get_state_str() are
26867c478bd9Sstevel@tonic-gate  *   for use in communicating processor state information to userland.  Kernel
26877c478bd9Sstevel@tonic-gate  *   subsystems should only be using the cpu_flags value directly.  Subsystems
26887c478bd9Sstevel@tonic-gate  *   modifying cpu_flags should record the state change via a call to the
26897c478bd9Sstevel@tonic-gate  *   cpu_set_state().
26907c478bd9Sstevel@tonic-gate  */
26917c478bd9Sstevel@tonic-gate 
26927c478bd9Sstevel@tonic-gate /*
26937c478bd9Sstevel@tonic-gate  * Update the pi_state of this CPU.  This function provides the CPU status for
26947c478bd9Sstevel@tonic-gate  * the information returned by processor_info(2).
26957c478bd9Sstevel@tonic-gate  */
26967c478bd9Sstevel@tonic-gate void
26977c478bd9Sstevel@tonic-gate cpu_set_state(cpu_t *cpu)
26987c478bd9Sstevel@tonic-gate {
26997c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
27007c478bd9Sstevel@tonic-gate 	cpu->cpu_type_info.pi_state = cpu_get_state(cpu);
27017c478bd9Sstevel@tonic-gate 	cpu->cpu_state_begin = gethrestime_sec();
27027c478bd9Sstevel@tonic-gate 	pool_cpu_mod = gethrtime();
27037c478bd9Sstevel@tonic-gate }
27047c478bd9Sstevel@tonic-gate 
27057c478bd9Sstevel@tonic-gate /*
27067c478bd9Sstevel@tonic-gate  * Return offline/online/other status for the indicated CPU.  Use only for
27077c478bd9Sstevel@tonic-gate  * communication with user applications; cpu_flags provides the in-kernel
27087c478bd9Sstevel@tonic-gate  * interface.
27097c478bd9Sstevel@tonic-gate  */
27107c478bd9Sstevel@tonic-gate int
27117c478bd9Sstevel@tonic-gate cpu_get_state(cpu_t *cpu)
27127c478bd9Sstevel@tonic-gate {
27137c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
27147c478bd9Sstevel@tonic-gate 	if (cpu->cpu_flags & CPU_POWEROFF)
27157c478bd9Sstevel@tonic-gate 		return (P_POWEROFF);
27167c478bd9Sstevel@tonic-gate 	else if (cpu->cpu_flags & CPU_FAULTED)
27177c478bd9Sstevel@tonic-gate 		return (P_FAULTED);
27187c478bd9Sstevel@tonic-gate 	else if (cpu->cpu_flags & CPU_SPARE)
27197c478bd9Sstevel@tonic-gate 		return (P_SPARE);
27207c478bd9Sstevel@tonic-gate 	else if ((cpu->cpu_flags & (CPU_READY | CPU_OFFLINE)) != CPU_READY)
27217c478bd9Sstevel@tonic-gate 		return (P_OFFLINE);
27227c478bd9Sstevel@tonic-gate 	else if (cpu->cpu_flags & CPU_ENABLE)
27237c478bd9Sstevel@tonic-gate 		return (P_ONLINE);
27247c478bd9Sstevel@tonic-gate 	else
27257c478bd9Sstevel@tonic-gate 		return (P_NOINTR);
27267c478bd9Sstevel@tonic-gate }
27277c478bd9Sstevel@tonic-gate 
27287c478bd9Sstevel@tonic-gate /*
27297c478bd9Sstevel@tonic-gate  * Return processor_info(2) state as a string.
27307c478bd9Sstevel@tonic-gate  */
27317c478bd9Sstevel@tonic-gate const char *
27327c478bd9Sstevel@tonic-gate cpu_get_state_str(cpu_t *cpu)
27337c478bd9Sstevel@tonic-gate {
27347c478bd9Sstevel@tonic-gate 	const char *string;
27357c478bd9Sstevel@tonic-gate 
27367c478bd9Sstevel@tonic-gate 	switch (cpu_get_state(cpu)) {
27377c478bd9Sstevel@tonic-gate 	case P_ONLINE:
27387c478bd9Sstevel@tonic-gate 		string = PS_ONLINE;
27397c478bd9Sstevel@tonic-gate 		break;
27407c478bd9Sstevel@tonic-gate 	case P_POWEROFF:
27417c478bd9Sstevel@tonic-gate 		string = PS_POWEROFF;
27427c478bd9Sstevel@tonic-gate 		break;
27437c478bd9Sstevel@tonic-gate 	case P_NOINTR:
27447c478bd9Sstevel@tonic-gate 		string = PS_NOINTR;
27457c478bd9Sstevel@tonic-gate 		break;
27467c478bd9Sstevel@tonic-gate 	case P_SPARE:
27477c478bd9Sstevel@tonic-gate 		string = PS_SPARE;
27487c478bd9Sstevel@tonic-gate 		break;
27497c478bd9Sstevel@tonic-gate 	case P_FAULTED:
27507c478bd9Sstevel@tonic-gate 		string = PS_FAULTED;
27517c478bd9Sstevel@tonic-gate 		break;
27527c478bd9Sstevel@tonic-gate 	case P_OFFLINE:
27537c478bd9Sstevel@tonic-gate 		string = PS_OFFLINE;
27547c478bd9Sstevel@tonic-gate 		break;
27557c478bd9Sstevel@tonic-gate 	default:
27567c478bd9Sstevel@tonic-gate 		string = "unknown";
27577c478bd9Sstevel@tonic-gate 		break;
27587c478bd9Sstevel@tonic-gate 	}
27597c478bd9Sstevel@tonic-gate 	return (string);
27607c478bd9Sstevel@tonic-gate }
27617c478bd9Sstevel@tonic-gate 
27627c478bd9Sstevel@tonic-gate /*
27637c478bd9Sstevel@tonic-gate  * Export this CPU's statistics (cpu_stat_t and cpu_stats_t) as raw and named
27647c478bd9Sstevel@tonic-gate  * kstats, respectively.  This is done when a CPU is initialized or placed
27657c478bd9Sstevel@tonic-gate  * online via p_online(2).
27667c478bd9Sstevel@tonic-gate  */
27677c478bd9Sstevel@tonic-gate static void
27687c478bd9Sstevel@tonic-gate cpu_stats_kstat_create(cpu_t *cp)
27697c478bd9Sstevel@tonic-gate {
27707c478bd9Sstevel@tonic-gate 	int 	instance = cp->cpu_id;
27717c478bd9Sstevel@tonic-gate 	char 	*module = "cpu";
27727c478bd9Sstevel@tonic-gate 	char 	*class = "misc";
27737c478bd9Sstevel@tonic-gate 	kstat_t	*ksp;
27747c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
27757c478bd9Sstevel@tonic-gate 
27767c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
27777c478bd9Sstevel@tonic-gate 
27787c478bd9Sstevel@tonic-gate 	if (pool_pset_enabled())
27797c478bd9Sstevel@tonic-gate 		zoneid = GLOBAL_ZONEID;
27807c478bd9Sstevel@tonic-gate 	else
27817c478bd9Sstevel@tonic-gate 		zoneid = ALL_ZONES;
27827c478bd9Sstevel@tonic-gate 	/*
27837c478bd9Sstevel@tonic-gate 	 * Create named kstats
27847c478bd9Sstevel@tonic-gate 	 */
27857c478bd9Sstevel@tonic-gate #define	CPU_STATS_KS_CREATE(name, tsize, update_func)                    \
27867c478bd9Sstevel@tonic-gate 	ksp = kstat_create_zone(module, instance, (name), class,         \
27877c478bd9Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED, (tsize) / sizeof (kstat_named_t), 0,       \
27887c478bd9Sstevel@tonic-gate 	    zoneid);                                                     \
27897c478bd9Sstevel@tonic-gate 	if (ksp != NULL) {                                               \
27907c478bd9Sstevel@tonic-gate 		ksp->ks_private = cp;                                    \
27917c478bd9Sstevel@tonic-gate 		ksp->ks_update = (update_func);                          \
27927c478bd9Sstevel@tonic-gate 		kstat_install(ksp);                                      \
27937c478bd9Sstevel@tonic-gate 	} else                                                           \
27947c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "cpu: unable to create %s:%d:%s kstat", \
27957c478bd9Sstevel@tonic-gate 		    module, instance, (name));
27967c478bd9Sstevel@tonic-gate 
27977c478bd9Sstevel@tonic-gate 	CPU_STATS_KS_CREATE("sys", sizeof (cpu_sys_stats_ks_data_template),
27987c478bd9Sstevel@tonic-gate 	    cpu_sys_stats_ks_update);
27997c478bd9Sstevel@tonic-gate 	CPU_STATS_KS_CREATE("vm", sizeof (cpu_vm_stats_ks_data_template),
28007c478bd9Sstevel@tonic-gate 	    cpu_vm_stats_ks_update);
28017c478bd9Sstevel@tonic-gate 
28027c478bd9Sstevel@tonic-gate 	/*
28037c478bd9Sstevel@tonic-gate 	 * Export the familiar cpu_stat_t KSTAT_TYPE_RAW kstat.
28047c478bd9Sstevel@tonic-gate 	 */
28057c478bd9Sstevel@tonic-gate 	ksp = kstat_create_zone("cpu_stat", cp->cpu_id, NULL,
28067c478bd9Sstevel@tonic-gate 	    "misc", KSTAT_TYPE_RAW, sizeof (cpu_stat_t), 0, zoneid);
28077c478bd9Sstevel@tonic-gate 	if (ksp != NULL) {
28087c478bd9Sstevel@tonic-gate 		ksp->ks_update = cpu_stat_ks_update;
28097c478bd9Sstevel@tonic-gate 		ksp->ks_private = cp;
28107c478bd9Sstevel@tonic-gate 		kstat_install(ksp);
28117c478bd9Sstevel@tonic-gate 	}
28127c478bd9Sstevel@tonic-gate }
28137c478bd9Sstevel@tonic-gate 
28147c478bd9Sstevel@tonic-gate static void
28157c478bd9Sstevel@tonic-gate cpu_stats_kstat_destroy(cpu_t *cp)
28167c478bd9Sstevel@tonic-gate {
28177c478bd9Sstevel@tonic-gate 	char ks_name[KSTAT_STRLEN];
28187c478bd9Sstevel@tonic-gate 
28197c478bd9Sstevel@tonic-gate 	(void) sprintf(ks_name, "cpu_stat%d", cp->cpu_id);
28207c478bd9Sstevel@tonic-gate 	kstat_delete_byname("cpu_stat", cp->cpu_id, ks_name);
28217c478bd9Sstevel@tonic-gate 
28227c478bd9Sstevel@tonic-gate 	kstat_delete_byname("cpu", cp->cpu_id, "sys");
28237c478bd9Sstevel@tonic-gate 	kstat_delete_byname("cpu", cp->cpu_id, "vm");
28247c478bd9Sstevel@tonic-gate }
28257c478bd9Sstevel@tonic-gate 
28267c478bd9Sstevel@tonic-gate static int
28277c478bd9Sstevel@tonic-gate cpu_sys_stats_ks_update(kstat_t *ksp, int rw)
28287c478bd9Sstevel@tonic-gate {
28297c478bd9Sstevel@tonic-gate 	cpu_t *cp = (cpu_t *)ksp->ks_private;
28307c478bd9Sstevel@tonic-gate 	struct cpu_sys_stats_ks_data *csskd;
28317c478bd9Sstevel@tonic-gate 	cpu_sys_stats_t *css;
2832eda89462Sesolom 	hrtime_t msnsecs[NCMSTATES];
28337c478bd9Sstevel@tonic-gate 	int	i;
28347c478bd9Sstevel@tonic-gate 
28357c478bd9Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
28367c478bd9Sstevel@tonic-gate 		return (EACCES);
28377c478bd9Sstevel@tonic-gate 
28387c478bd9Sstevel@tonic-gate 	csskd = ksp->ks_data;
28397c478bd9Sstevel@tonic-gate 	css = &cp->cpu_stats.sys;
28407c478bd9Sstevel@tonic-gate 
2841eda89462Sesolom 	/*
2842eda89462Sesolom 	 * Read CPU mstate, but compare with the last values we
2843eda89462Sesolom 	 * received to make sure that the returned kstats never
2844eda89462Sesolom 	 * decrease.
2845eda89462Sesolom 	 */
2846eda89462Sesolom 
2847eda89462Sesolom 	get_cpu_mstate(cp, msnsecs);
2848eda89462Sesolom 	if (csskd->cpu_nsec_idle.value.ui64 > msnsecs[CMS_IDLE])
2849eda89462Sesolom 		msnsecs[CMS_IDLE] = csskd->cpu_nsec_idle.value.ui64;
2850eda89462Sesolom 	if (csskd->cpu_nsec_user.value.ui64 > msnsecs[CMS_USER])
2851eda89462Sesolom 		msnsecs[CMS_USER] = csskd->cpu_nsec_user.value.ui64;
2852eda89462Sesolom 	if (csskd->cpu_nsec_kernel.value.ui64 > msnsecs[CMS_SYSTEM])
2853eda89462Sesolom 		msnsecs[CMS_SYSTEM] = csskd->cpu_nsec_kernel.value.ui64;
2854eda89462Sesolom 
28557c478bd9Sstevel@tonic-gate 	bcopy(&cpu_sys_stats_ks_data_template, ksp->ks_data,
28567c478bd9Sstevel@tonic-gate 	    sizeof (cpu_sys_stats_ks_data_template));
2857eda89462Sesolom 
28587c478bd9Sstevel@tonic-gate 	csskd->cpu_ticks_wait.value.ui64 = 0;
28597c478bd9Sstevel@tonic-gate 	csskd->wait_ticks_io.value.ui64 = 0;
28607c478bd9Sstevel@tonic-gate 
2861eda89462Sesolom 	csskd->cpu_nsec_idle.value.ui64 = msnsecs[CMS_IDLE];
2862eda89462Sesolom 	csskd->cpu_nsec_user.value.ui64 = msnsecs[CMS_USER];
2863eda89462Sesolom 	csskd->cpu_nsec_kernel.value.ui64 = msnsecs[CMS_SYSTEM];
28647c478bd9Sstevel@tonic-gate 	csskd->cpu_ticks_idle.value.ui64 =
28657c478bd9Sstevel@tonic-gate 	    NSEC_TO_TICK(csskd->cpu_nsec_idle.value.ui64);
28667c478bd9Sstevel@tonic-gate 	csskd->cpu_ticks_user.value.ui64 =
28677c478bd9Sstevel@tonic-gate 	    NSEC_TO_TICK(csskd->cpu_nsec_user.value.ui64);
28687c478bd9Sstevel@tonic-gate 	csskd->cpu_ticks_kernel.value.ui64 =
28697c478bd9Sstevel@tonic-gate 	    NSEC_TO_TICK(csskd->cpu_nsec_kernel.value.ui64);
28707c478bd9Sstevel@tonic-gate 	csskd->bread.value.ui64 = css->bread;
28717c478bd9Sstevel@tonic-gate 	csskd->bwrite.value.ui64 = css->bwrite;
28727c478bd9Sstevel@tonic-gate 	csskd->lread.value.ui64 = css->lread;
28737c478bd9Sstevel@tonic-gate 	csskd->lwrite.value.ui64 = css->lwrite;
28747c478bd9Sstevel@tonic-gate 	csskd->phread.value.ui64 = css->phread;
28757c478bd9Sstevel@tonic-gate 	csskd->phwrite.value.ui64 = css->phwrite;
28767c478bd9Sstevel@tonic-gate 	csskd->pswitch.value.ui64 = css->pswitch;
28777c478bd9Sstevel@tonic-gate 	csskd->trap.value.ui64 = css->trap;
28787c478bd9Sstevel@tonic-gate 	csskd->intr.value.ui64 = 0;
28797c478bd9Sstevel@tonic-gate 	for (i = 0; i < PIL_MAX; i++)
28807c478bd9Sstevel@tonic-gate 		csskd->intr.value.ui64 += css->intr[i];
28817c478bd9Sstevel@tonic-gate 	csskd->syscall.value.ui64 = css->syscall;
28827c478bd9Sstevel@tonic-gate 	csskd->sysread.value.ui64 = css->sysread;
28837c478bd9Sstevel@tonic-gate 	csskd->syswrite.value.ui64 = css->syswrite;
28847c478bd9Sstevel@tonic-gate 	csskd->sysfork.value.ui64 = css->sysfork;
28857c478bd9Sstevel@tonic-gate 	csskd->sysvfork.value.ui64 = css->sysvfork;
28867c478bd9Sstevel@tonic-gate 	csskd->sysexec.value.ui64 = css->sysexec;
28877c478bd9Sstevel@tonic-gate 	csskd->readch.value.ui64 = css->readch;
28887c478bd9Sstevel@tonic-gate 	csskd->writech.value.ui64 = css->writech;
28897c478bd9Sstevel@tonic-gate 	csskd->rcvint.value.ui64 = css->rcvint;
28907c478bd9Sstevel@tonic-gate 	csskd->xmtint.value.ui64 = css->xmtint;
28917c478bd9Sstevel@tonic-gate 	csskd->mdmint.value.ui64 = css->mdmint;
28927c478bd9Sstevel@tonic-gate 	csskd->rawch.value.ui64 = css->rawch;
28937c478bd9Sstevel@tonic-gate 	csskd->canch.value.ui64 = css->canch;
28947c478bd9Sstevel@tonic-gate 	csskd->outch.value.ui64 = css->outch;
28957c478bd9Sstevel@tonic-gate 	csskd->msg.value.ui64 = css->msg;
28967c478bd9Sstevel@tonic-gate 	csskd->sema.value.ui64 = css->sema;
28977c478bd9Sstevel@tonic-gate 	csskd->namei.value.ui64 = css->namei;
28987c478bd9Sstevel@tonic-gate 	csskd->ufsiget.value.ui64 = css->ufsiget;
28997c478bd9Sstevel@tonic-gate 	csskd->ufsdirblk.value.ui64 = css->ufsdirblk;
29007c478bd9Sstevel@tonic-gate 	csskd->ufsipage.value.ui64 = css->ufsipage;
29017c478bd9Sstevel@tonic-gate 	csskd->ufsinopage.value.ui64 = css->ufsinopage;
29027c478bd9Sstevel@tonic-gate 	csskd->procovf.value.ui64 = css->procovf;
29037c478bd9Sstevel@tonic-gate 	csskd->intrthread.value.ui64 = 0;
29047c478bd9Sstevel@tonic-gate 	for (i = 0; i < LOCK_LEVEL; i++)
29057c478bd9Sstevel@tonic-gate 		csskd->intrthread.value.ui64 += css->intr[i];
29067c478bd9Sstevel@tonic-gate 	csskd->intrblk.value.ui64 = css->intrblk;
29077c478bd9Sstevel@tonic-gate 	csskd->intrunpin.value.ui64 = css->intrunpin;
29087c478bd9Sstevel@tonic-gate 	csskd->idlethread.value.ui64 = css->idlethread;
29097c478bd9Sstevel@tonic-gate 	csskd->inv_swtch.value.ui64 = css->inv_swtch;
29107c478bd9Sstevel@tonic-gate 	csskd->nthreads.value.ui64 = css->nthreads;
29117c478bd9Sstevel@tonic-gate 	csskd->cpumigrate.value.ui64 = css->cpumigrate;
29127c478bd9Sstevel@tonic-gate 	csskd->xcalls.value.ui64 = css->xcalls;
29137c478bd9Sstevel@tonic-gate 	csskd->mutex_adenters.value.ui64 = css->mutex_adenters;
29147c478bd9Sstevel@tonic-gate 	csskd->rw_rdfails.value.ui64 = css->rw_rdfails;
29157c478bd9Sstevel@tonic-gate 	csskd->rw_wrfails.value.ui64 = css->rw_wrfails;
29167c478bd9Sstevel@tonic-gate 	csskd->modload.value.ui64 = css->modload;
29177c478bd9Sstevel@tonic-gate 	csskd->modunload.value.ui64 = css->modunload;
29187c478bd9Sstevel@tonic-gate 	csskd->bawrite.value.ui64 = css->bawrite;
29197c478bd9Sstevel@tonic-gate 	csskd->iowait.value.ui64 = 0;
29207c478bd9Sstevel@tonic-gate 
29217c478bd9Sstevel@tonic-gate 	return (0);
29227c478bd9Sstevel@tonic-gate }
29237c478bd9Sstevel@tonic-gate 
29247c478bd9Sstevel@tonic-gate static int
29257c478bd9Sstevel@tonic-gate cpu_vm_stats_ks_update(kstat_t *ksp, int rw)
29267c478bd9Sstevel@tonic-gate {
29277c478bd9Sstevel@tonic-gate 	cpu_t *cp = (cpu_t *)ksp->ks_private;
29287c478bd9Sstevel@tonic-gate 	struct cpu_vm_stats_ks_data *cvskd;
29297c478bd9Sstevel@tonic-gate 	cpu_vm_stats_t *cvs;
29307c478bd9Sstevel@tonic-gate 
29317c478bd9Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
29327c478bd9Sstevel@tonic-gate 		return (EACCES);
29337c478bd9Sstevel@tonic-gate 
29347c478bd9Sstevel@tonic-gate 	cvs = &cp->cpu_stats.vm;
29357c478bd9Sstevel@tonic-gate 	cvskd = ksp->ks_data;
29367c478bd9Sstevel@tonic-gate 
29377c478bd9Sstevel@tonic-gate 	bcopy(&cpu_vm_stats_ks_data_template, ksp->ks_data,
29387c478bd9Sstevel@tonic-gate 	    sizeof (cpu_vm_stats_ks_data_template));
29397c478bd9Sstevel@tonic-gate 	cvskd->pgrec.value.ui64 = cvs->pgrec;
29407c478bd9Sstevel@tonic-gate 	cvskd->pgfrec.value.ui64 = cvs->pgfrec;
29417c478bd9Sstevel@tonic-gate 	cvskd->pgin.value.ui64 = cvs->pgin;
29427c478bd9Sstevel@tonic-gate 	cvskd->pgpgin.value.ui64 = cvs->pgpgin;
29437c478bd9Sstevel@tonic-gate 	cvskd->pgout.value.ui64 = cvs->pgout;
29447c478bd9Sstevel@tonic-gate 	cvskd->pgpgout.value.ui64 = cvs->pgpgout;
29457c478bd9Sstevel@tonic-gate 	cvskd->swapin.value.ui64 = cvs->swapin;
29467c478bd9Sstevel@tonic-gate 	cvskd->pgswapin.value.ui64 = cvs->pgswapin;
29477c478bd9Sstevel@tonic-gate 	cvskd->swapout.value.ui64 = cvs->swapout;
29487c478bd9Sstevel@tonic-gate 	cvskd->pgswapout.value.ui64 = cvs->pgswapout;
29497c478bd9Sstevel@tonic-gate 	cvskd->zfod.value.ui64 = cvs->zfod;
29507c478bd9Sstevel@tonic-gate 	cvskd->dfree.value.ui64 = cvs->dfree;
29517c478bd9Sstevel@tonic-gate 	cvskd->scan.value.ui64 = cvs->scan;
29527c478bd9Sstevel@tonic-gate 	cvskd->rev.value.ui64 = cvs->rev;
29537c478bd9Sstevel@tonic-gate 	cvskd->hat_fault.value.ui64 = cvs->hat_fault;
29547c478bd9Sstevel@tonic-gate 	cvskd->as_fault.value.ui64 = cvs->as_fault;
29557c478bd9Sstevel@tonic-gate 	cvskd->maj_fault.value.ui64 = cvs->maj_fault;
29567c478bd9Sstevel@tonic-gate 	cvskd->cow_fault.value.ui64 = cvs->cow_fault;
29577c478bd9Sstevel@tonic-gate 	cvskd->prot_fault.value.ui64 = cvs->prot_fault;
29587c478bd9Sstevel@tonic-gate 	cvskd->softlock.value.ui64 = cvs->softlock;
29597c478bd9Sstevel@tonic-gate 	cvskd->kernel_asflt.value.ui64 = cvs->kernel_asflt;
29607c478bd9Sstevel@tonic-gate 	cvskd->pgrrun.value.ui64 = cvs->pgrrun;
29617c478bd9Sstevel@tonic-gate 	cvskd->execpgin.value.ui64 = cvs->execpgin;
29627c478bd9Sstevel@tonic-gate 	cvskd->execpgout.value.ui64 = cvs->execpgout;
29637c478bd9Sstevel@tonic-gate 	cvskd->execfree.value.ui64 = cvs->execfree;
29647c478bd9Sstevel@tonic-gate 	cvskd->anonpgin.value.ui64 = cvs->anonpgin;
29657c478bd9Sstevel@tonic-gate 	cvskd->anonpgout.value.ui64 = cvs->anonpgout;
29667c478bd9Sstevel@tonic-gate 	cvskd->anonfree.value.ui64 = cvs->anonfree;
29677c478bd9Sstevel@tonic-gate 	cvskd->fspgin.value.ui64 = cvs->fspgin;
29687c478bd9Sstevel@tonic-gate 	cvskd->fspgout.value.ui64 = cvs->fspgout;
29697c478bd9Sstevel@tonic-gate 	cvskd->fsfree.value.ui64 = cvs->fsfree;
29707c478bd9Sstevel@tonic-gate 
29717c478bd9Sstevel@tonic-gate 	return (0);
29727c478bd9Sstevel@tonic-gate }
29737c478bd9Sstevel@tonic-gate 
29747c478bd9Sstevel@tonic-gate static int
29757c478bd9Sstevel@tonic-gate cpu_stat_ks_update(kstat_t *ksp, int rw)
29767c478bd9Sstevel@tonic-gate {
29777c478bd9Sstevel@tonic-gate 	cpu_stat_t *cso;
29787c478bd9Sstevel@tonic-gate 	cpu_t *cp;
29797c478bd9Sstevel@tonic-gate 	int i;
2980eda89462Sesolom 	hrtime_t msnsecs[NCMSTATES];
29817c478bd9Sstevel@tonic-gate 
29827c478bd9Sstevel@tonic-gate 	cso = (cpu_stat_t *)ksp->ks_data;
29837c478bd9Sstevel@tonic-gate 	cp = (cpu_t *)ksp->ks_private;
29847c478bd9Sstevel@tonic-gate 
29857c478bd9Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
29867c478bd9Sstevel@tonic-gate 		return (EACCES);
29877c478bd9Sstevel@tonic-gate 
29887c478bd9Sstevel@tonic-gate 	/*
2989eda89462Sesolom 	 * Read CPU mstate, but compare with the last values we
2990eda89462Sesolom 	 * received to make sure that the returned kstats never
2991eda89462Sesolom 	 * decrease.
29927c478bd9Sstevel@tonic-gate 	 */
29937c478bd9Sstevel@tonic-gate 
2994eda89462Sesolom 	get_cpu_mstate(cp, msnsecs);
2995eda89462Sesolom 	msnsecs[CMS_IDLE] = NSEC_TO_TICK(msnsecs[CMS_IDLE]);
2996eda89462Sesolom 	msnsecs[CMS_USER] = NSEC_TO_TICK(msnsecs[CMS_USER]);
2997eda89462Sesolom 	msnsecs[CMS_SYSTEM] = NSEC_TO_TICK(msnsecs[CMS_SYSTEM]);
2998eda89462Sesolom 	if (cso->cpu_sysinfo.cpu[CPU_IDLE] < msnsecs[CMS_IDLE])
2999eda89462Sesolom 		cso->cpu_sysinfo.cpu[CPU_IDLE] = msnsecs[CMS_IDLE];
3000eda89462Sesolom 	if (cso->cpu_sysinfo.cpu[CPU_USER] < msnsecs[CMS_USER])
3001eda89462Sesolom 		cso->cpu_sysinfo.cpu[CPU_USER] = msnsecs[CMS_USER];
3002eda89462Sesolom 	if (cso->cpu_sysinfo.cpu[CPU_KERNEL] < msnsecs[CMS_SYSTEM])
3003eda89462Sesolom 		cso->cpu_sysinfo.cpu[CPU_KERNEL] = msnsecs[CMS_SYSTEM];
30047c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.cpu[CPU_WAIT] 	= 0;
30057c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.wait[W_IO] 	= 0;
30067c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.wait[W_SWAP]	= 0;
30077c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.wait[W_PIO]	= 0;
30087c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.bread 		= CPU_STATS(cp, sys.bread);
30097c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.bwrite 	= CPU_STATS(cp, sys.bwrite);
30107c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.lread 		= CPU_STATS(cp, sys.lread);
30117c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.lwrite 	= CPU_STATS(cp, sys.lwrite);
30127c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.phread 	= CPU_STATS(cp, sys.phread);
30137c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.phwrite 	= CPU_STATS(cp, sys.phwrite);
30147c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.pswitch 	= CPU_STATS(cp, sys.pswitch);
30157c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.trap 		= CPU_STATS(cp, sys.trap);
30167c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.intr		= 0;
30177c478bd9Sstevel@tonic-gate 	for (i = 0; i < PIL_MAX; i++)
30187c478bd9Sstevel@tonic-gate 		cso->cpu_sysinfo.intr += CPU_STATS(cp, sys.intr[i]);
30197c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.syscall	= CPU_STATS(cp, sys.syscall);
30207c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.sysread	= CPU_STATS(cp, sys.sysread);
30217c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.syswrite	= CPU_STATS(cp, sys.syswrite);
30227c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.sysfork	= CPU_STATS(cp, sys.sysfork);
30237c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.sysvfork	= CPU_STATS(cp, sys.sysvfork);
30247c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.sysexec	= CPU_STATS(cp, sys.sysexec);
30257c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.readch		= CPU_STATS(cp, sys.readch);
30267c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.writech	= CPU_STATS(cp, sys.writech);
30277c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.rcvint		= CPU_STATS(cp, sys.rcvint);
30287c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.xmtint		= CPU_STATS(cp, sys.xmtint);
30297c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.mdmint		= CPU_STATS(cp, sys.mdmint);
30307c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.rawch		= CPU_STATS(cp, sys.rawch);
30317c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.canch		= CPU_STATS(cp, sys.canch);
30327c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.outch		= CPU_STATS(cp, sys.outch);
30337c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.msg		= CPU_STATS(cp, sys.msg);
30347c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.sema		= CPU_STATS(cp, sys.sema);
30357c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.namei		= CPU_STATS(cp, sys.namei);
30367c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.ufsiget	= CPU_STATS(cp, sys.ufsiget);
30377c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.ufsdirblk	= CPU_STATS(cp, sys.ufsdirblk);
30387c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.ufsipage	= CPU_STATS(cp, sys.ufsipage);
30397c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.ufsinopage	= CPU_STATS(cp, sys.ufsinopage);
30407c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.inodeovf	= 0;
30417c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.fileovf	= 0;
30427c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.procovf	= CPU_STATS(cp, sys.procovf);
30437c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.intrthread	= 0;
30447c478bd9Sstevel@tonic-gate 	for (i = 0; i < LOCK_LEVEL; i++)
30457c478bd9Sstevel@tonic-gate 		cso->cpu_sysinfo.intrthread += CPU_STATS(cp, sys.intr[i]);
30467c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.intrblk	= CPU_STATS(cp, sys.intrblk);
30477c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.idlethread	= CPU_STATS(cp, sys.idlethread);
30487c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.inv_swtch	= CPU_STATS(cp, sys.inv_swtch);
30497c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.nthreads	= CPU_STATS(cp, sys.nthreads);
30507c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.cpumigrate	= CPU_STATS(cp, sys.cpumigrate);
30517c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.xcalls		= CPU_STATS(cp, sys.xcalls);
30527c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.mutex_adenters	= CPU_STATS(cp, sys.mutex_adenters);
30537c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.rw_rdfails	= CPU_STATS(cp, sys.rw_rdfails);
30547c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.rw_wrfails	= CPU_STATS(cp, sys.rw_wrfails);
30557c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.modload	= CPU_STATS(cp, sys.modload);
30567c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.modunload	= CPU_STATS(cp, sys.modunload);
30577c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.bawrite	= CPU_STATS(cp, sys.bawrite);
30587c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.rw_enters	= 0;
30597c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.win_uo_cnt	= 0;
30607c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.win_uu_cnt	= 0;
30617c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.win_so_cnt	= 0;
30627c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.win_su_cnt	= 0;
30637c478bd9Sstevel@tonic-gate 	cso->cpu_sysinfo.win_suo_cnt	= 0;
30647c478bd9Sstevel@tonic-gate 
30657c478bd9Sstevel@tonic-gate 	cso->cpu_syswait.iowait		= 0;
30667c478bd9Sstevel@tonic-gate 	cso->cpu_syswait.swap		= 0;
30677c478bd9Sstevel@tonic-gate 	cso->cpu_syswait.physio		= 0;
30687c478bd9Sstevel@tonic-gate 
30697c478bd9Sstevel@tonic-gate 	cso->cpu_vminfo.pgrec		= CPU_STATS(cp, vm.pgrec);
30707c478bd9Sstevel@tonic-gate 	cso->cpu_vminfo.pgfrec		= CPU_STATS(cp, vm.pgfrec);
30717c478bd9Sstevel@tonic-gate 	cso->cpu_vminfo.pgin		= CPU_STATS(cp, vm.pgin);
30727c478bd9Sstevel@tonic-gate 	cso->cpu_vminfo.pgpgin		= CPU_STATS(cp, vm.pgpgin);
30737c478bd9Sstevel@tonic-gate 	cso->cpu_vminfo.pgout		= CPU_STATS(cp, vm.pgout);
30747c478bd9Sstevel@tonic-gate 	cso->cpu_vminfo.pgpgout		= CPU_STATS(cp, vm.pgpgout);
30757c478bd9Sstevel@tonic-gate 	cso->cpu_vminfo.swapin		= CPU_STATS(cp, vm.swapin);
30767c478bd9Sstevel@tonic-gate 	cso->cpu_vminfo.pgswapin	= CPU_STATS(cp, vm.pgswapin);
30777c478bd9Sstevel@tonic-gate 	cso->cpu_vminfo.swapout		= CPU_STATS(cp, vm.swapout);
30787c478bd9Sstevel@tonic-gate 	cso->cpu_vminfo.pgswapout	= CPU_STATS(cp, vm.pgswapout);
30797c478bd9Sstevel@tonic-gate 	cso->cpu_vminfo.zfod		= CPU_STATS(cp, vm.zfod);
30807c478bd9Sstevel@tonic-gate 	cso->cpu_vminfo.dfree		= CPU_STATS(cp, vm.dfree);
30817c478bd9Sstevel@tonic-gate 	cso->cpu_vminfo.scan		= CPU_STATS(cp, vm.scan);
30827c478bd9Sstevel@tonic-gate 	cso->cpu_vminfo.rev		= CPU_STATS(cp, vm.rev);
30837c478bd9Sstevel@tonic-gate 	cso->cpu_vminfo.hat_fault	= CPU_STATS(cp, vm.hat_fault);
30847c478bd9Sstevel@tonic-gate 	cso->cpu_vminfo.as_fault	= CPU_STATS(cp, vm.as_fault);
30857c478bd9Sstevel@tonic-gate 	cso->cpu_vminfo.maj_fault	= CPU_STATS(cp, vm.maj_fault);
30867c478bd9Sstevel@tonic-gate 	cso->cpu_vminfo.cow_fault	= CPU_STATS(cp, vm.cow_fault);
30877c478bd9Sstevel@tonic-gate 	cso->cpu_vminfo.prot_fault	= CPU_STATS(cp, vm.prot_fault);
30887c478bd9Sstevel@tonic-gate 	cso->cpu_vminfo.softlock	= CPU_STATS(cp, vm.softlock);
30897c478bd9Sstevel@tonic-gate 	cso->cpu_vminfo.kernel_asflt	= CPU_STATS(cp, vm.kernel_asflt);
30907c478bd9Sstevel@tonic-gate 	cso->cpu_vminfo.pgrrun		= CPU_STATS(cp, vm.pgrrun);
30917c478bd9Sstevel@tonic-gate 	cso->cpu_vminfo.execpgin	= CPU_STATS(cp, vm.execpgin);
30927c478bd9Sstevel@tonic-gate 	cso->cpu_vminfo.execpgout	= CPU_STATS(cp, vm.execpgout);
30937c478bd9Sstevel@tonic-gate 	cso->cpu_vminfo.execfree	= CPU_STATS(cp, vm.execfree);
30947c478bd9Sstevel@tonic-gate 	cso->cpu_vminfo.anonpgin	= CPU_STATS(cp, vm.anonpgin);
30957c478bd9Sstevel@tonic-gate 	cso->cpu_vminfo.anonpgout	= CPU_STATS(cp, vm.anonpgout);
30967c478bd9Sstevel@tonic-gate 	cso->cpu_vminfo.anonfree	= CPU_STATS(cp, vm.anonfree);
30977c478bd9Sstevel@tonic-gate 	cso->cpu_vminfo.fspgin		= CPU_STATS(cp, vm.fspgin);
30987c478bd9Sstevel@tonic-gate 	cso->cpu_vminfo.fspgout		= CPU_STATS(cp, vm.fspgout);
30997c478bd9Sstevel@tonic-gate 	cso->cpu_vminfo.fsfree		= CPU_STATS(cp, vm.fsfree);
31007c478bd9Sstevel@tonic-gate 
31017c478bd9Sstevel@tonic-gate 	return (0);
31027c478bd9Sstevel@tonic-gate }
3103