xref: /onnv-gate/usr/src/uts/common/os/cpu.c (revision 9482:42f3d60af7ca)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51539Scindi  * Common Development and Distribution License (the "License").
61539Scindi  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
228566SMadhavan.Venkataraman@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * Architecture-independent CPU control functions.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/param.h>
320Sstevel@tonic-gate #include <sys/var.h>
330Sstevel@tonic-gate #include <sys/thread.h>
340Sstevel@tonic-gate #include <sys/cpuvar.h>
350Sstevel@tonic-gate #include <sys/kstat.h>
360Sstevel@tonic-gate #include <sys/uadmin.h>
370Sstevel@tonic-gate #include <sys/systm.h>
380Sstevel@tonic-gate #include <sys/errno.h>
390Sstevel@tonic-gate #include <sys/cmn_err.h>
400Sstevel@tonic-gate #include <sys/procset.h>
410Sstevel@tonic-gate #include <sys/processor.h>
420Sstevel@tonic-gate #include <sys/debug.h>
430Sstevel@tonic-gate #include <sys/cpupart.h>
440Sstevel@tonic-gate #include <sys/lgrp.h>
450Sstevel@tonic-gate #include <sys/pset.h>
463434Sesaxe #include <sys/pghw.h>
470Sstevel@tonic-gate #include <sys/kmem.h>
480Sstevel@tonic-gate #include <sys/kmem_impl.h>	/* to set per-cpu kmem_cache offset */
490Sstevel@tonic-gate #include <sys/atomic.h>
500Sstevel@tonic-gate #include <sys/callb.h>
510Sstevel@tonic-gate #include <sys/vtrace.h>
520Sstevel@tonic-gate #include <sys/cyclic.h>
530Sstevel@tonic-gate #include <sys/bitmap.h>
540Sstevel@tonic-gate #include <sys/nvpair.h>
550Sstevel@tonic-gate #include <sys/pool_pset.h>
560Sstevel@tonic-gate #include <sys/msacct.h>
570Sstevel@tonic-gate #include <sys/time.h>
580Sstevel@tonic-gate #include <sys/archsystm.h>
598906SEric.Saxe@Sun.COM #include <sys/sdt.h>
605834Spt157919 #if defined(__x86) || defined(__amd64)
611414Scindi #include <sys/x86_archext.h>
621414Scindi #endif
638048SMadhavan.Venkataraman@Sun.COM #include <sys/callo.h>
640Sstevel@tonic-gate 
650Sstevel@tonic-gate extern int	mp_cpu_start(cpu_t *);
660Sstevel@tonic-gate extern int	mp_cpu_stop(cpu_t *);
670Sstevel@tonic-gate extern int	mp_cpu_poweron(cpu_t *);
680Sstevel@tonic-gate extern int	mp_cpu_poweroff(cpu_t *);
690Sstevel@tonic-gate extern int	mp_cpu_configure(int);
700Sstevel@tonic-gate extern int	mp_cpu_unconfigure(int);
710Sstevel@tonic-gate extern void	mp_cpu_faulted_enter(cpu_t *);
720Sstevel@tonic-gate extern void	mp_cpu_faulted_exit(cpu_t *);
730Sstevel@tonic-gate 
740Sstevel@tonic-gate extern int cmp_cpu_to_chip(processorid_t cpuid);
750Sstevel@tonic-gate #ifdef __sparcv9
760Sstevel@tonic-gate extern char *cpu_fru_fmri(cpu_t *cp);
770Sstevel@tonic-gate #endif
780Sstevel@tonic-gate 
790Sstevel@tonic-gate static void cpu_add_active_internal(cpu_t *cp);
800Sstevel@tonic-gate static void cpu_remove_active(cpu_t *cp);
810Sstevel@tonic-gate static void cpu_info_kstat_create(cpu_t *cp);
820Sstevel@tonic-gate static void cpu_info_kstat_destroy(cpu_t *cp);
830Sstevel@tonic-gate static void cpu_stats_kstat_create(cpu_t *cp);
840Sstevel@tonic-gate static void cpu_stats_kstat_destroy(cpu_t *cp);
850Sstevel@tonic-gate 
860Sstevel@tonic-gate static int cpu_sys_stats_ks_update(kstat_t *ksp, int rw);
870Sstevel@tonic-gate static int cpu_vm_stats_ks_update(kstat_t *ksp, int rw);
880Sstevel@tonic-gate static int cpu_stat_ks_update(kstat_t *ksp, int rw);
890Sstevel@tonic-gate static int cpu_state_change_hooks(int, cpu_setup_t, cpu_setup_t);
900Sstevel@tonic-gate 
910Sstevel@tonic-gate /*
920Sstevel@tonic-gate  * cpu_lock protects ncpus, ncpus_online, cpu_flag, cpu_list, cpu_active,
930Sstevel@tonic-gate  * and dispatch queue reallocations.  The lock ordering with respect to
940Sstevel@tonic-gate  * related locks is:
950Sstevel@tonic-gate  *
960Sstevel@tonic-gate  *	cpu_lock --> thread_free_lock  --->  p_lock  --->  thread_lock()
970Sstevel@tonic-gate  *
980Sstevel@tonic-gate  * Warning:  Certain sections of code do not use the cpu_lock when
990Sstevel@tonic-gate  * traversing the cpu_list (e.g. mutex_vector_enter(), clock()).  Since
1000Sstevel@tonic-gate  * all cpus are paused during modifications to this list, a solution
1010Sstevel@tonic-gate  * to protect the list is too either disable kernel preemption while
1020Sstevel@tonic-gate  * walking the list, *or* recheck the cpu_next pointer at each
1030Sstevel@tonic-gate  * iteration in the loop.  Note that in no cases can any cached
1040Sstevel@tonic-gate  * copies of the cpu pointers be kept as they may become invalid.
1050Sstevel@tonic-gate  */
1060Sstevel@tonic-gate kmutex_t	cpu_lock;
1070Sstevel@tonic-gate cpu_t		*cpu_list;		/* list of all CPUs */
1083792Sakolb cpu_t		*clock_cpu_list;	/* used by clock to walk CPUs */
1090Sstevel@tonic-gate cpu_t		*cpu_active;		/* list of active CPUs */
1100Sstevel@tonic-gate static cpuset_t	cpu_available;		/* set of available CPUs */
1110Sstevel@tonic-gate cpuset_t	cpu_seqid_inuse;	/* which cpu_seqids are in use */
1120Sstevel@tonic-gate 
1138408SEric.Saxe@Sun.COM cpu_t		**cpu_seq;		/* ptrs to CPUs, indexed by seq_id */
1148408SEric.Saxe@Sun.COM 
1150Sstevel@tonic-gate /*
1160Sstevel@tonic-gate  * max_ncpus keeps the max cpus the system can have. Initially
1170Sstevel@tonic-gate  * it's NCPU, but since most archs scan the devtree for cpus
1180Sstevel@tonic-gate  * fairly early on during boot, the real max can be known before
1190Sstevel@tonic-gate  * ncpus is set (useful for early NCPU based allocations).
1200Sstevel@tonic-gate  */
1210Sstevel@tonic-gate int max_ncpus = NCPU;
1220Sstevel@tonic-gate /*
1230Sstevel@tonic-gate  * platforms that set max_ncpus to maxiumum number of cpus that can be
1240Sstevel@tonic-gate  * dynamically added will set boot_max_ncpus to the number of cpus found
1250Sstevel@tonic-gate  * at device tree scan time during boot.
1260Sstevel@tonic-gate  */
1270Sstevel@tonic-gate int boot_max_ncpus = -1;
1286880Sdv142724 int boot_ncpus = -1;
1290Sstevel@tonic-gate /*
1300Sstevel@tonic-gate  * Maximum possible CPU id.  This can never be >= NCPU since NCPU is
1310Sstevel@tonic-gate  * used to size arrays that are indexed by CPU id.
1320Sstevel@tonic-gate  */
1330Sstevel@tonic-gate processorid_t max_cpuid = NCPU - 1;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate int ncpus = 1;
1360Sstevel@tonic-gate int ncpus_online = 1;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate /*
1390Sstevel@tonic-gate  * CPU that we're trying to offline.  Protected by cpu_lock.
1400Sstevel@tonic-gate  */
1410Sstevel@tonic-gate cpu_t *cpu_inmotion;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate /*
1440Sstevel@tonic-gate  * Can be raised to suppress further weakbinding, which are instead
1450Sstevel@tonic-gate  * satisfied by disabling preemption.  Must be raised/lowered under cpu_lock,
1460Sstevel@tonic-gate  * while individual thread weakbinding synchronisation is done under thread
1470Sstevel@tonic-gate  * lock.
1480Sstevel@tonic-gate  */
1490Sstevel@tonic-gate int weakbindingbarrier;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate /*
1520Sstevel@tonic-gate  * Variables used in pause_cpus().
1530Sstevel@tonic-gate  */
1540Sstevel@tonic-gate static volatile char safe_list[NCPU];
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate static struct _cpu_pause_info {
1570Sstevel@tonic-gate 	int		cp_spl;		/* spl saved in pause_cpus() */
1580Sstevel@tonic-gate 	volatile int	cp_go;		/* Go signal sent after all ready */
1590Sstevel@tonic-gate 	int		cp_count;	/* # of CPUs to pause */
1600Sstevel@tonic-gate 	ksema_t		cp_sem;		/* synch pause_cpus & cpu_pause */
1610Sstevel@tonic-gate 	kthread_id_t	cp_paused;
1620Sstevel@tonic-gate } cpu_pause_info;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate static kmutex_t pause_free_mutex;
1650Sstevel@tonic-gate static kcondvar_t pause_free_cv;
1660Sstevel@tonic-gate 
1675295Srandyf void *(*cpu_pause_func)(void *) = NULL;
1685295Srandyf 
1695295Srandyf 
1700Sstevel@tonic-gate static struct cpu_sys_stats_ks_data {
1710Sstevel@tonic-gate 	kstat_named_t cpu_ticks_idle;
1720Sstevel@tonic-gate 	kstat_named_t cpu_ticks_user;
1730Sstevel@tonic-gate 	kstat_named_t cpu_ticks_kernel;
1740Sstevel@tonic-gate 	kstat_named_t cpu_ticks_wait;
1750Sstevel@tonic-gate 	kstat_named_t cpu_nsec_idle;
1760Sstevel@tonic-gate 	kstat_named_t cpu_nsec_user;
1770Sstevel@tonic-gate 	kstat_named_t cpu_nsec_kernel;
1785076Smishra 	kstat_named_t cpu_nsec_intr;
1795076Smishra 	kstat_named_t cpu_load_intr;
1800Sstevel@tonic-gate 	kstat_named_t wait_ticks_io;
1810Sstevel@tonic-gate 	kstat_named_t bread;
1820Sstevel@tonic-gate 	kstat_named_t bwrite;
1830Sstevel@tonic-gate 	kstat_named_t lread;
1840Sstevel@tonic-gate 	kstat_named_t lwrite;
1850Sstevel@tonic-gate 	kstat_named_t phread;
1860Sstevel@tonic-gate 	kstat_named_t phwrite;
1870Sstevel@tonic-gate 	kstat_named_t pswitch;
1880Sstevel@tonic-gate 	kstat_named_t trap;
1890Sstevel@tonic-gate 	kstat_named_t intr;
1900Sstevel@tonic-gate 	kstat_named_t syscall;
1910Sstevel@tonic-gate 	kstat_named_t sysread;
1920Sstevel@tonic-gate 	kstat_named_t syswrite;
1930Sstevel@tonic-gate 	kstat_named_t sysfork;
1940Sstevel@tonic-gate 	kstat_named_t sysvfork;
1950Sstevel@tonic-gate 	kstat_named_t sysexec;
1960Sstevel@tonic-gate 	kstat_named_t readch;
1970Sstevel@tonic-gate 	kstat_named_t writech;
1980Sstevel@tonic-gate 	kstat_named_t rcvint;
1990Sstevel@tonic-gate 	kstat_named_t xmtint;
2000Sstevel@tonic-gate 	kstat_named_t mdmint;
2010Sstevel@tonic-gate 	kstat_named_t rawch;
2020Sstevel@tonic-gate 	kstat_named_t canch;
2030Sstevel@tonic-gate 	kstat_named_t outch;
2040Sstevel@tonic-gate 	kstat_named_t msg;
2050Sstevel@tonic-gate 	kstat_named_t sema;
2060Sstevel@tonic-gate 	kstat_named_t namei;
2070Sstevel@tonic-gate 	kstat_named_t ufsiget;
2080Sstevel@tonic-gate 	kstat_named_t ufsdirblk;
2090Sstevel@tonic-gate 	kstat_named_t ufsipage;
2100Sstevel@tonic-gate 	kstat_named_t ufsinopage;
2110Sstevel@tonic-gate 	kstat_named_t procovf;
2120Sstevel@tonic-gate 	kstat_named_t intrthread;
2130Sstevel@tonic-gate 	kstat_named_t intrblk;
2140Sstevel@tonic-gate 	kstat_named_t intrunpin;
2150Sstevel@tonic-gate 	kstat_named_t idlethread;
2160Sstevel@tonic-gate 	kstat_named_t inv_swtch;
2170Sstevel@tonic-gate 	kstat_named_t nthreads;
2180Sstevel@tonic-gate 	kstat_named_t cpumigrate;
2190Sstevel@tonic-gate 	kstat_named_t xcalls;
2200Sstevel@tonic-gate 	kstat_named_t mutex_adenters;
2210Sstevel@tonic-gate 	kstat_named_t rw_rdfails;
2220Sstevel@tonic-gate 	kstat_named_t rw_wrfails;
2230Sstevel@tonic-gate 	kstat_named_t modload;
2240Sstevel@tonic-gate 	kstat_named_t modunload;
2250Sstevel@tonic-gate 	kstat_named_t bawrite;
2260Sstevel@tonic-gate 	kstat_named_t iowait;
2270Sstevel@tonic-gate } cpu_sys_stats_ks_data_template = {
2280Sstevel@tonic-gate 	{ "cpu_ticks_idle", 	KSTAT_DATA_UINT64 },
2290Sstevel@tonic-gate 	{ "cpu_ticks_user", 	KSTAT_DATA_UINT64 },
2300Sstevel@tonic-gate 	{ "cpu_ticks_kernel", 	KSTAT_DATA_UINT64 },
2310Sstevel@tonic-gate 	{ "cpu_ticks_wait", 	KSTAT_DATA_UINT64 },
2320Sstevel@tonic-gate 	{ "cpu_nsec_idle",	KSTAT_DATA_UINT64 },
2330Sstevel@tonic-gate 	{ "cpu_nsec_user",	KSTAT_DATA_UINT64 },
2340Sstevel@tonic-gate 	{ "cpu_nsec_kernel",	KSTAT_DATA_UINT64 },
2355076Smishra 	{ "cpu_nsec_intr",	KSTAT_DATA_UINT64 },
2365076Smishra 	{ "cpu_load_intr",	KSTAT_DATA_UINT64 },
2370Sstevel@tonic-gate 	{ "wait_ticks_io", 	KSTAT_DATA_UINT64 },
2380Sstevel@tonic-gate 	{ "bread", 		KSTAT_DATA_UINT64 },
2390Sstevel@tonic-gate 	{ "bwrite", 		KSTAT_DATA_UINT64 },
2400Sstevel@tonic-gate 	{ "lread", 		KSTAT_DATA_UINT64 },
2410Sstevel@tonic-gate 	{ "lwrite", 		KSTAT_DATA_UINT64 },
2420Sstevel@tonic-gate 	{ "phread", 		KSTAT_DATA_UINT64 },
2430Sstevel@tonic-gate 	{ "phwrite", 		KSTAT_DATA_UINT64 },
2440Sstevel@tonic-gate 	{ "pswitch", 		KSTAT_DATA_UINT64 },
2450Sstevel@tonic-gate 	{ "trap", 		KSTAT_DATA_UINT64 },
2460Sstevel@tonic-gate 	{ "intr", 		KSTAT_DATA_UINT64 },
2470Sstevel@tonic-gate 	{ "syscall", 		KSTAT_DATA_UINT64 },
2480Sstevel@tonic-gate 	{ "sysread", 		KSTAT_DATA_UINT64 },
2490Sstevel@tonic-gate 	{ "syswrite", 		KSTAT_DATA_UINT64 },
2500Sstevel@tonic-gate 	{ "sysfork", 		KSTAT_DATA_UINT64 },
2510Sstevel@tonic-gate 	{ "sysvfork", 		KSTAT_DATA_UINT64 },
2520Sstevel@tonic-gate 	{ "sysexec", 		KSTAT_DATA_UINT64 },
2530Sstevel@tonic-gate 	{ "readch", 		KSTAT_DATA_UINT64 },
2540Sstevel@tonic-gate 	{ "writech", 		KSTAT_DATA_UINT64 },
2550Sstevel@tonic-gate 	{ "rcvint", 		KSTAT_DATA_UINT64 },
2560Sstevel@tonic-gate 	{ "xmtint", 		KSTAT_DATA_UINT64 },
2570Sstevel@tonic-gate 	{ "mdmint", 		KSTAT_DATA_UINT64 },
2580Sstevel@tonic-gate 	{ "rawch", 		KSTAT_DATA_UINT64 },
2590Sstevel@tonic-gate 	{ "canch", 		KSTAT_DATA_UINT64 },
2600Sstevel@tonic-gate 	{ "outch", 		KSTAT_DATA_UINT64 },
2610Sstevel@tonic-gate 	{ "msg", 		KSTAT_DATA_UINT64 },
2620Sstevel@tonic-gate 	{ "sema", 		KSTAT_DATA_UINT64 },
2630Sstevel@tonic-gate 	{ "namei", 		KSTAT_DATA_UINT64 },
2640Sstevel@tonic-gate 	{ "ufsiget", 		KSTAT_DATA_UINT64 },
2650Sstevel@tonic-gate 	{ "ufsdirblk", 		KSTAT_DATA_UINT64 },
2660Sstevel@tonic-gate 	{ "ufsipage", 		KSTAT_DATA_UINT64 },
2670Sstevel@tonic-gate 	{ "ufsinopage", 	KSTAT_DATA_UINT64 },
2680Sstevel@tonic-gate 	{ "procovf", 		KSTAT_DATA_UINT64 },
2690Sstevel@tonic-gate 	{ "intrthread", 	KSTAT_DATA_UINT64 },
2700Sstevel@tonic-gate 	{ "intrblk", 		KSTAT_DATA_UINT64 },
2710Sstevel@tonic-gate 	{ "intrunpin",		KSTAT_DATA_UINT64 },
2720Sstevel@tonic-gate 	{ "idlethread", 	KSTAT_DATA_UINT64 },
2730Sstevel@tonic-gate 	{ "inv_swtch", 		KSTAT_DATA_UINT64 },
2740Sstevel@tonic-gate 	{ "nthreads", 		KSTAT_DATA_UINT64 },
2750Sstevel@tonic-gate 	{ "cpumigrate", 	KSTAT_DATA_UINT64 },
2760Sstevel@tonic-gate 	{ "xcalls", 		KSTAT_DATA_UINT64 },
2770Sstevel@tonic-gate 	{ "mutex_adenters", 	KSTAT_DATA_UINT64 },
2780Sstevel@tonic-gate 	{ "rw_rdfails", 	KSTAT_DATA_UINT64 },
2790Sstevel@tonic-gate 	{ "rw_wrfails", 	KSTAT_DATA_UINT64 },
2800Sstevel@tonic-gate 	{ "modload", 		KSTAT_DATA_UINT64 },
2810Sstevel@tonic-gate 	{ "modunload", 		KSTAT_DATA_UINT64 },
2820Sstevel@tonic-gate 	{ "bawrite", 		KSTAT_DATA_UINT64 },
2830Sstevel@tonic-gate 	{ "iowait",		KSTAT_DATA_UINT64 },
2840Sstevel@tonic-gate };
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate static struct cpu_vm_stats_ks_data {
2870Sstevel@tonic-gate 	kstat_named_t pgrec;
2880Sstevel@tonic-gate 	kstat_named_t pgfrec;
2890Sstevel@tonic-gate 	kstat_named_t pgin;
2900Sstevel@tonic-gate 	kstat_named_t pgpgin;
2910Sstevel@tonic-gate 	kstat_named_t pgout;
2920Sstevel@tonic-gate 	kstat_named_t pgpgout;
2930Sstevel@tonic-gate 	kstat_named_t swapin;
2940Sstevel@tonic-gate 	kstat_named_t pgswapin;
2950Sstevel@tonic-gate 	kstat_named_t swapout;
2960Sstevel@tonic-gate 	kstat_named_t pgswapout;
2970Sstevel@tonic-gate 	kstat_named_t zfod;
2980Sstevel@tonic-gate 	kstat_named_t dfree;
2990Sstevel@tonic-gate 	kstat_named_t scan;
3000Sstevel@tonic-gate 	kstat_named_t rev;
3010Sstevel@tonic-gate 	kstat_named_t hat_fault;
3020Sstevel@tonic-gate 	kstat_named_t as_fault;
3030Sstevel@tonic-gate 	kstat_named_t maj_fault;
3040Sstevel@tonic-gate 	kstat_named_t cow_fault;
3050Sstevel@tonic-gate 	kstat_named_t prot_fault;
3060Sstevel@tonic-gate 	kstat_named_t softlock;
3070Sstevel@tonic-gate 	kstat_named_t kernel_asflt;
3080Sstevel@tonic-gate 	kstat_named_t pgrrun;
3090Sstevel@tonic-gate 	kstat_named_t execpgin;
3100Sstevel@tonic-gate 	kstat_named_t execpgout;
3110Sstevel@tonic-gate 	kstat_named_t execfree;
3120Sstevel@tonic-gate 	kstat_named_t anonpgin;
3130Sstevel@tonic-gate 	kstat_named_t anonpgout;
3140Sstevel@tonic-gate 	kstat_named_t anonfree;
3150Sstevel@tonic-gate 	kstat_named_t fspgin;
3160Sstevel@tonic-gate 	kstat_named_t fspgout;
3170Sstevel@tonic-gate 	kstat_named_t fsfree;
3180Sstevel@tonic-gate } cpu_vm_stats_ks_data_template = {
3190Sstevel@tonic-gate 	{ "pgrec",		KSTAT_DATA_UINT64 },
3200Sstevel@tonic-gate 	{ "pgfrec",		KSTAT_DATA_UINT64 },
3210Sstevel@tonic-gate 	{ "pgin",		KSTAT_DATA_UINT64 },
3220Sstevel@tonic-gate 	{ "pgpgin",		KSTAT_DATA_UINT64 },
3230Sstevel@tonic-gate 	{ "pgout",		KSTAT_DATA_UINT64 },
3240Sstevel@tonic-gate 	{ "pgpgout",		KSTAT_DATA_UINT64 },
3250Sstevel@tonic-gate 	{ "swapin",		KSTAT_DATA_UINT64 },
3260Sstevel@tonic-gate 	{ "pgswapin",		KSTAT_DATA_UINT64 },
3270Sstevel@tonic-gate 	{ "swapout",		KSTAT_DATA_UINT64 },
3280Sstevel@tonic-gate 	{ "pgswapout",		KSTAT_DATA_UINT64 },
3290Sstevel@tonic-gate 	{ "zfod",		KSTAT_DATA_UINT64 },
3300Sstevel@tonic-gate 	{ "dfree",		KSTAT_DATA_UINT64 },
3310Sstevel@tonic-gate 	{ "scan",		KSTAT_DATA_UINT64 },
3320Sstevel@tonic-gate 	{ "rev",		KSTAT_DATA_UINT64 },
3330Sstevel@tonic-gate 	{ "hat_fault",		KSTAT_DATA_UINT64 },
3340Sstevel@tonic-gate 	{ "as_fault",		KSTAT_DATA_UINT64 },
3350Sstevel@tonic-gate 	{ "maj_fault",		KSTAT_DATA_UINT64 },
3360Sstevel@tonic-gate 	{ "cow_fault",		KSTAT_DATA_UINT64 },
3370Sstevel@tonic-gate 	{ "prot_fault",		KSTAT_DATA_UINT64 },
3380Sstevel@tonic-gate 	{ "softlock",		KSTAT_DATA_UINT64 },
3390Sstevel@tonic-gate 	{ "kernel_asflt",	KSTAT_DATA_UINT64 },
3400Sstevel@tonic-gate 	{ "pgrrun",		KSTAT_DATA_UINT64 },
3410Sstevel@tonic-gate 	{ "execpgin",		KSTAT_DATA_UINT64 },
3420Sstevel@tonic-gate 	{ "execpgout",		KSTAT_DATA_UINT64 },
3430Sstevel@tonic-gate 	{ "execfree",		KSTAT_DATA_UINT64 },
3440Sstevel@tonic-gate 	{ "anonpgin",		KSTAT_DATA_UINT64 },
3450Sstevel@tonic-gate 	{ "anonpgout",		KSTAT_DATA_UINT64 },
3460Sstevel@tonic-gate 	{ "anonfree",		KSTAT_DATA_UINT64 },
3470Sstevel@tonic-gate 	{ "fspgin",		KSTAT_DATA_UINT64 },
3480Sstevel@tonic-gate 	{ "fspgout",		KSTAT_DATA_UINT64 },
3490Sstevel@tonic-gate 	{ "fsfree",		KSTAT_DATA_UINT64 },
3500Sstevel@tonic-gate };
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate /*
3530Sstevel@tonic-gate  * Force the specified thread to migrate to the appropriate processor.
3540Sstevel@tonic-gate  * Called with thread lock held, returns with it dropped.
3550Sstevel@tonic-gate  */
3560Sstevel@tonic-gate static void
3570Sstevel@tonic-gate force_thread_migrate(kthread_id_t tp)
3580Sstevel@tonic-gate {
3590Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(tp));
3600Sstevel@tonic-gate 	if (tp == curthread) {
3610Sstevel@tonic-gate 		THREAD_TRANSITION(tp);
3620Sstevel@tonic-gate 		CL_SETRUN(tp);
3630Sstevel@tonic-gate 		thread_unlock_nopreempt(tp);
3640Sstevel@tonic-gate 		swtch();
3650Sstevel@tonic-gate 	} else {
3660Sstevel@tonic-gate 		if (tp->t_state == TS_ONPROC) {
3670Sstevel@tonic-gate 			cpu_surrender(tp);
3680Sstevel@tonic-gate 		} else if (tp->t_state == TS_RUN) {
3690Sstevel@tonic-gate 			(void) dispdeq(tp);
3700Sstevel@tonic-gate 			setbackdq(tp);
3710Sstevel@tonic-gate 		}
3720Sstevel@tonic-gate 		thread_unlock(tp);
3730Sstevel@tonic-gate 	}
3740Sstevel@tonic-gate }
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate /*
3770Sstevel@tonic-gate  * Set affinity for a specified CPU.
3780Sstevel@tonic-gate  * A reference count is incremented and the affinity is held until the
3790Sstevel@tonic-gate  * reference count is decremented to zero by thread_affinity_clear().
3800Sstevel@tonic-gate  * This is so regions of code requiring affinity can be nested.
3810Sstevel@tonic-gate  * Caller needs to ensure that cpu_id remains valid, which can be
3820Sstevel@tonic-gate  * done by holding cpu_lock across this call, unless the caller
3830Sstevel@tonic-gate  * specifies CPU_CURRENT in which case the cpu_lock will be acquired
3840Sstevel@tonic-gate  * by thread_affinity_set and CPU->cpu_id will be the target CPU.
3850Sstevel@tonic-gate  */
3860Sstevel@tonic-gate void
3870Sstevel@tonic-gate thread_affinity_set(kthread_id_t t, int cpu_id)
3880Sstevel@tonic-gate {
3890Sstevel@tonic-gate 	cpu_t		*cp;
3900Sstevel@tonic-gate 	int		c;
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	ASSERT(!(t == curthread && t->t_weakbound_cpu != NULL));
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	if ((c = cpu_id) == CPU_CURRENT) {
3950Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
3960Sstevel@tonic-gate 		cpu_id = CPU->cpu_id;
3970Sstevel@tonic-gate 	}
3980Sstevel@tonic-gate 	/*
3990Sstevel@tonic-gate 	 * We should be asserting that cpu_lock is held here, but
4000Sstevel@tonic-gate 	 * the NCA code doesn't acquire it.  The following assert
4010Sstevel@tonic-gate 	 * should be uncommented when the NCA code is fixed.
4020Sstevel@tonic-gate 	 *
4030Sstevel@tonic-gate 	 * ASSERT(MUTEX_HELD(&cpu_lock));
4040Sstevel@tonic-gate 	 */
4050Sstevel@tonic-gate 	ASSERT((cpu_id >= 0) && (cpu_id < NCPU));
4060Sstevel@tonic-gate 	cp = cpu[cpu_id];
4070Sstevel@tonic-gate 	ASSERT(cp != NULL);		/* user must provide a good cpu_id */
4080Sstevel@tonic-gate 	/*
4090Sstevel@tonic-gate 	 * If there is already a hard affinity requested, and this affinity
4100Sstevel@tonic-gate 	 * conflicts with that, panic.
4110Sstevel@tonic-gate 	 */
4120Sstevel@tonic-gate 	thread_lock(t);
4130Sstevel@tonic-gate 	if (t->t_affinitycnt > 0 && t->t_bound_cpu != cp) {
4140Sstevel@tonic-gate 		panic("affinity_set: setting %p but already bound to %p",
4150Sstevel@tonic-gate 		    (void *)cp, (void *)t->t_bound_cpu);
4160Sstevel@tonic-gate 	}
4170Sstevel@tonic-gate 	t->t_affinitycnt++;
4180Sstevel@tonic-gate 	t->t_bound_cpu = cp;
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	/*
4210Sstevel@tonic-gate 	 * Make sure we're running on the right CPU.
4220Sstevel@tonic-gate 	 */
4230Sstevel@tonic-gate 	if (cp != t->t_cpu || t != curthread) {
4240Sstevel@tonic-gate 		force_thread_migrate(t);	/* drops thread lock */
4250Sstevel@tonic-gate 	} else {
4260Sstevel@tonic-gate 		thread_unlock(t);
4270Sstevel@tonic-gate 	}
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	if (c == CPU_CURRENT)
4300Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
4310Sstevel@tonic-gate }
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate /*
4340Sstevel@tonic-gate  *	Wrapper for backward compatibility.
4350Sstevel@tonic-gate  */
4360Sstevel@tonic-gate void
4370Sstevel@tonic-gate affinity_set(int cpu_id)
4380Sstevel@tonic-gate {
4390Sstevel@tonic-gate 	thread_affinity_set(curthread, cpu_id);
4400Sstevel@tonic-gate }
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate /*
4430Sstevel@tonic-gate  * Decrement the affinity reservation count and if it becomes zero,
4440Sstevel@tonic-gate  * clear the CPU affinity for the current thread, or set it to the user's
4450Sstevel@tonic-gate  * software binding request.
4460Sstevel@tonic-gate  */
4470Sstevel@tonic-gate void
4480Sstevel@tonic-gate thread_affinity_clear(kthread_id_t t)
4490Sstevel@tonic-gate {
4500Sstevel@tonic-gate 	register processorid_t binding;
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	thread_lock(t);
4530Sstevel@tonic-gate 	if (--t->t_affinitycnt == 0) {
4540Sstevel@tonic-gate 		if ((binding = t->t_bind_cpu) == PBIND_NONE) {
4550Sstevel@tonic-gate 			/*
4560Sstevel@tonic-gate 			 * Adjust disp_max_unbound_pri if necessary.
4570Sstevel@tonic-gate 			 */
4580Sstevel@tonic-gate 			disp_adjust_unbound_pri(t);
4590Sstevel@tonic-gate 			t->t_bound_cpu = NULL;
4600Sstevel@tonic-gate 			if (t->t_cpu->cpu_part != t->t_cpupart) {
4610Sstevel@tonic-gate 				force_thread_migrate(t);
4620Sstevel@tonic-gate 				return;
4630Sstevel@tonic-gate 			}
4640Sstevel@tonic-gate 		} else {
4650Sstevel@tonic-gate 			t->t_bound_cpu = cpu[binding];
4660Sstevel@tonic-gate 			/*
4670Sstevel@tonic-gate 			 * Make sure the thread is running on the bound CPU.
4680Sstevel@tonic-gate 			 */
4690Sstevel@tonic-gate 			if (t->t_cpu != t->t_bound_cpu) {
4700Sstevel@tonic-gate 				force_thread_migrate(t);
4710Sstevel@tonic-gate 				return;		/* already dropped lock */
4720Sstevel@tonic-gate 			}
4730Sstevel@tonic-gate 		}
4740Sstevel@tonic-gate 	}
4750Sstevel@tonic-gate 	thread_unlock(t);
4760Sstevel@tonic-gate }
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate /*
4790Sstevel@tonic-gate  * Wrapper for backward compatibility.
4800Sstevel@tonic-gate  */
4810Sstevel@tonic-gate void
4820Sstevel@tonic-gate affinity_clear(void)
4830Sstevel@tonic-gate {
4840Sstevel@tonic-gate 	thread_affinity_clear(curthread);
4850Sstevel@tonic-gate }
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate /*
4880Sstevel@tonic-gate  * Weak cpu affinity.  Bind to the "current" cpu for short periods
4890Sstevel@tonic-gate  * of time during which the thread must not block (but may be preempted).
4900Sstevel@tonic-gate  * Use this instead of kpreempt_disable() when it is only "no migration"
4910Sstevel@tonic-gate  * rather than "no preemption" semantics that are required - disabling
4920Sstevel@tonic-gate  * preemption holds higher priority threads off of cpu and if the
4930Sstevel@tonic-gate  * operation that is protected is more than momentary this is not good
4940Sstevel@tonic-gate  * for realtime etc.
4950Sstevel@tonic-gate  *
4960Sstevel@tonic-gate  * Weakly bound threads will not prevent a cpu from being offlined -
4970Sstevel@tonic-gate  * we'll only run them on the cpu to which they are weakly bound but
4980Sstevel@tonic-gate  * (because they do not block) we'll always be able to move them on to
4990Sstevel@tonic-gate  * another cpu at offline time if we give them just a short moment to
5000Sstevel@tonic-gate  * run during which they will unbind.  To give a cpu a chance of offlining,
5010Sstevel@tonic-gate  * however, we require a barrier to weak bindings that may be raised for a
5020Sstevel@tonic-gate  * given cpu (offline/move code may set this and then wait a short time for
5030Sstevel@tonic-gate  * existing weak bindings to drop); the cpu_inmotion pointer is that barrier.
5040Sstevel@tonic-gate  *
5050Sstevel@tonic-gate  * There are few restrictions on the calling context of thread_nomigrate.
5060Sstevel@tonic-gate  * The caller must not hold the thread lock.  Calls may be nested.
5070Sstevel@tonic-gate  *
5080Sstevel@tonic-gate  * After weakbinding a thread must not perform actions that may block.
5090Sstevel@tonic-gate  * In particular it must not call thread_affinity_set; calling that when
5100Sstevel@tonic-gate  * already weakbound is nonsensical anyway.
5110Sstevel@tonic-gate  *
5120Sstevel@tonic-gate  * If curthread is prevented from migrating for other reasons
5130Sstevel@tonic-gate  * (kernel preemption disabled; high pil; strongly bound; interrupt thread)
5140Sstevel@tonic-gate  * then the weak binding will succeed even if this cpu is the target of an
5150Sstevel@tonic-gate  * offline/move request.
5160Sstevel@tonic-gate  */
5170Sstevel@tonic-gate void
5180Sstevel@tonic-gate thread_nomigrate(void)
5190Sstevel@tonic-gate {
5200Sstevel@tonic-gate 	cpu_t *cp;
5210Sstevel@tonic-gate 	kthread_id_t t = curthread;
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate again:
5240Sstevel@tonic-gate 	kpreempt_disable();
5250Sstevel@tonic-gate 	cp = CPU;
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	/*
5280Sstevel@tonic-gate 	 * A highlevel interrupt must not modify t_nomigrate or
5290Sstevel@tonic-gate 	 * t_weakbound_cpu of the thread it has interrupted.  A lowlevel
5300Sstevel@tonic-gate 	 * interrupt thread cannot migrate and we can avoid the
5310Sstevel@tonic-gate 	 * thread_lock call below by short-circuiting here.  In either
5320Sstevel@tonic-gate 	 * case we can just return since no migration is possible and
5330Sstevel@tonic-gate 	 * the condition will persist (ie, when we test for these again
5340Sstevel@tonic-gate 	 * in thread_allowmigrate they can't have changed).   Migration
5350Sstevel@tonic-gate 	 * is also impossible if we're at or above DISP_LEVEL pil.
5360Sstevel@tonic-gate 	 */
5370Sstevel@tonic-gate 	if (CPU_ON_INTR(cp) || t->t_flag & T_INTR_THREAD ||
5380Sstevel@tonic-gate 	    getpil() >= DISP_LEVEL) {
5390Sstevel@tonic-gate 		kpreempt_enable();
5400Sstevel@tonic-gate 		return;
5410Sstevel@tonic-gate 	}
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 	/*
5440Sstevel@tonic-gate 	 * We must be consistent with existing weak bindings.  Since we
5450Sstevel@tonic-gate 	 * may be interrupted between the increment of t_nomigrate and
5460Sstevel@tonic-gate 	 * the store to t_weakbound_cpu below we cannot assume that
5470Sstevel@tonic-gate 	 * t_weakbound_cpu will be set if t_nomigrate is.  Note that we
5480Sstevel@tonic-gate 	 * cannot assert t_weakbound_cpu == t_bind_cpu since that is not
5490Sstevel@tonic-gate 	 * always the case.
5500Sstevel@tonic-gate 	 */
5510Sstevel@tonic-gate 	if (t->t_nomigrate && t->t_weakbound_cpu && t->t_weakbound_cpu != cp) {
5520Sstevel@tonic-gate 		if (!panicstr)
5530Sstevel@tonic-gate 			panic("thread_nomigrate: binding to %p but already "
5540Sstevel@tonic-gate 			    "bound to %p", (void *)cp,
5550Sstevel@tonic-gate 			    (void *)t->t_weakbound_cpu);
5560Sstevel@tonic-gate 	}
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	/*
5590Sstevel@tonic-gate 	 * At this point we have preemption disabled and we don't yet hold
5600Sstevel@tonic-gate 	 * the thread lock.  So it's possible that somebody else could
5610Sstevel@tonic-gate 	 * set t_bind_cpu here and not be able to force us across to the
5620Sstevel@tonic-gate 	 * new cpu (since we have preemption disabled).
5630Sstevel@tonic-gate 	 */
5640Sstevel@tonic-gate 	thread_lock(curthread);
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	/*
5670Sstevel@tonic-gate 	 * If further weak bindings are being (temporarily) suppressed then
5680Sstevel@tonic-gate 	 * we'll settle for disabling kernel preemption (which assures
5690Sstevel@tonic-gate 	 * no migration provided the thread does not block which it is
5700Sstevel@tonic-gate 	 * not allowed to if using thread_nomigrate).  We must remember
5710Sstevel@tonic-gate 	 * this disposition so we can take appropriate action in
5720Sstevel@tonic-gate 	 * thread_allowmigrate.  If this is a nested call and the
5730Sstevel@tonic-gate 	 * thread is already weakbound then fall through as normal.
5740Sstevel@tonic-gate 	 * We remember the decision to settle for kpreempt_disable through
5750Sstevel@tonic-gate 	 * negative nesting counting in t_nomigrate.  Once a thread has had one
5760Sstevel@tonic-gate 	 * weakbinding request satisfied in this way any further (nested)
5770Sstevel@tonic-gate 	 * requests will continue to be satisfied in the same way,
5780Sstevel@tonic-gate 	 * even if weak bindings have recommenced.
5790Sstevel@tonic-gate 	 */
5800Sstevel@tonic-gate 	if (t->t_nomigrate < 0 || weakbindingbarrier && t->t_nomigrate == 0) {
5810Sstevel@tonic-gate 		--t->t_nomigrate;
5820Sstevel@tonic-gate 		thread_unlock(curthread);
5830Sstevel@tonic-gate 		return;		/* with kpreempt_disable still active */
5840Sstevel@tonic-gate 	}
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	/*
5870Sstevel@tonic-gate 	 * We hold thread_lock so t_bind_cpu cannot change.  We could,
5880Sstevel@tonic-gate 	 * however, be running on a different cpu to which we are t_bound_cpu
5890Sstevel@tonic-gate 	 * to (as explained above).  If we grant the weak binding request
5900Sstevel@tonic-gate 	 * in that case then the dispatcher must favour our weak binding
5910Sstevel@tonic-gate 	 * over our strong (in which case, just as when preemption is
5920Sstevel@tonic-gate 	 * disabled, we can continue to run on a cpu other than the one to
5930Sstevel@tonic-gate 	 * which we are strongbound; the difference in this case is that
5940Sstevel@tonic-gate 	 * this thread can be preempted and so can appear on the dispatch
5950Sstevel@tonic-gate 	 * queues of a cpu other than the one it is strongbound to).
5960Sstevel@tonic-gate 	 *
5970Sstevel@tonic-gate 	 * If the cpu we are running on does not appear to be a current
5980Sstevel@tonic-gate 	 * offline target (we check cpu_inmotion to determine this - since
5990Sstevel@tonic-gate 	 * we don't hold cpu_lock we may not see a recent store to that,
6000Sstevel@tonic-gate 	 * so it's possible that we at times can grant a weak binding to a
6010Sstevel@tonic-gate 	 * cpu that is an offline target, but that one request will not
6020Sstevel@tonic-gate 	 * prevent the offline from succeeding) then we will always grant
6030Sstevel@tonic-gate 	 * the weak binding request.  This includes the case above where
6040Sstevel@tonic-gate 	 * we grant a weakbinding not commensurate with our strong binding.
6050Sstevel@tonic-gate 	 *
6060Sstevel@tonic-gate 	 * If our cpu does appear to be an offline target then we're inclined
6070Sstevel@tonic-gate 	 * not to grant the weakbinding request just yet - we'd prefer to
6080Sstevel@tonic-gate 	 * migrate to another cpu and grant the request there.  The
6090Sstevel@tonic-gate 	 * exceptions are those cases where going through preemption code
6100Sstevel@tonic-gate 	 * will not result in us changing cpu:
6110Sstevel@tonic-gate 	 *
6120Sstevel@tonic-gate 	 *	. interrupts have already bypassed this case (see above)
6130Sstevel@tonic-gate 	 *	. we are already weakbound to this cpu (dispatcher code will
6140Sstevel@tonic-gate 	 *	  always return us to the weakbound cpu)
6150Sstevel@tonic-gate 	 *	. preemption was disabled even before we disabled it above
6160Sstevel@tonic-gate 	 *	. we are strongbound to this cpu (if we're strongbound to
6170Sstevel@tonic-gate 	 *	another and not yet running there the trip through the
6180Sstevel@tonic-gate 	 *	dispatcher will move us to the strongbound cpu and we
6190Sstevel@tonic-gate 	 *	will grant the weak binding there)
6200Sstevel@tonic-gate 	 */
6210Sstevel@tonic-gate 	if (cp != cpu_inmotion || t->t_nomigrate > 0 || t->t_preempt > 1 ||
6220Sstevel@tonic-gate 	    t->t_bound_cpu == cp) {
6230Sstevel@tonic-gate 		/*
6240Sstevel@tonic-gate 		 * Don't be tempted to store to t_weakbound_cpu only on
6250Sstevel@tonic-gate 		 * the first nested bind request - if we're interrupted
6260Sstevel@tonic-gate 		 * after the increment of t_nomigrate and before the
6270Sstevel@tonic-gate 		 * store to t_weakbound_cpu and the interrupt calls
6280Sstevel@tonic-gate 		 * thread_nomigrate then the assertion in thread_allowmigrate
6290Sstevel@tonic-gate 		 * would fail.
6300Sstevel@tonic-gate 		 */
6310Sstevel@tonic-gate 		t->t_nomigrate++;
6320Sstevel@tonic-gate 		t->t_weakbound_cpu = cp;
6330Sstevel@tonic-gate 		membar_producer();
6340Sstevel@tonic-gate 		thread_unlock(curthread);
6350Sstevel@tonic-gate 		/*
6360Sstevel@tonic-gate 		 * Now that we have dropped the thread_lock another thread
6370Sstevel@tonic-gate 		 * can set our t_weakbound_cpu, and will try to migrate us
6380Sstevel@tonic-gate 		 * to the strongbound cpu (which will not be prevented by
6390Sstevel@tonic-gate 		 * preemption being disabled since we're about to enable
6400Sstevel@tonic-gate 		 * preemption).  We have granted the weakbinding to the current
6410Sstevel@tonic-gate 		 * cpu, so again we are in the position that is is is possible
6420Sstevel@tonic-gate 		 * that our weak and strong bindings differ.  Again this
6430Sstevel@tonic-gate 		 * is catered for by dispatcher code which will favour our
6440Sstevel@tonic-gate 		 * weak binding.
6450Sstevel@tonic-gate 		 */
6460Sstevel@tonic-gate 		kpreempt_enable();
6470Sstevel@tonic-gate 	} else {
6480Sstevel@tonic-gate 		/*
6490Sstevel@tonic-gate 		 * Move to another cpu before granting the request by
6500Sstevel@tonic-gate 		 * forcing this thread through preemption code.  When we
6510Sstevel@tonic-gate 		 * get to set{front,back}dq called from CL_PREEMPT()
6520Sstevel@tonic-gate 		 * cpu_choose() will be used to select a cpu to queue
6530Sstevel@tonic-gate 		 * us on - that will see cpu_inmotion and take
6540Sstevel@tonic-gate 		 * steps to avoid returning us to this cpu.
6550Sstevel@tonic-gate 		 */
6560Sstevel@tonic-gate 		cp->cpu_kprunrun = 1;
6570Sstevel@tonic-gate 		thread_unlock(curthread);
6580Sstevel@tonic-gate 		kpreempt_enable();	/* will call preempt() */
6590Sstevel@tonic-gate 		goto again;
6600Sstevel@tonic-gate 	}
6610Sstevel@tonic-gate }
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate void
6640Sstevel@tonic-gate thread_allowmigrate(void)
6650Sstevel@tonic-gate {
6660Sstevel@tonic-gate 	kthread_id_t t = curthread;
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	ASSERT(t->t_weakbound_cpu == CPU ||
6690Sstevel@tonic-gate 	    (t->t_nomigrate < 0 && t->t_preempt > 0) ||
6700Sstevel@tonic-gate 	    CPU_ON_INTR(CPU) || t->t_flag & T_INTR_THREAD ||
6710Sstevel@tonic-gate 	    getpil() >= DISP_LEVEL);
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	if (CPU_ON_INTR(CPU) || (t->t_flag & T_INTR_THREAD) ||
6740Sstevel@tonic-gate 	    getpil() >= DISP_LEVEL)
6750Sstevel@tonic-gate 		return;
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 	if (t->t_nomigrate < 0) {
6780Sstevel@tonic-gate 		/*
6790Sstevel@tonic-gate 		 * This thread was granted "weak binding" in the
6800Sstevel@tonic-gate 		 * stronger form of kernel preemption disabling.
6810Sstevel@tonic-gate 		 * Undo a level of nesting for both t_nomigrate
6820Sstevel@tonic-gate 		 * and t_preempt.
6830Sstevel@tonic-gate 		 */
6840Sstevel@tonic-gate 		++t->t_nomigrate;
6850Sstevel@tonic-gate 		kpreempt_enable();
6860Sstevel@tonic-gate 	} else if (--t->t_nomigrate == 0) {
6870Sstevel@tonic-gate 		/*
6880Sstevel@tonic-gate 		 * Time to drop the weak binding.  We need to cater
6890Sstevel@tonic-gate 		 * for the case where we're weakbound to a different
6900Sstevel@tonic-gate 		 * cpu than that to which we're strongbound (a very
6910Sstevel@tonic-gate 		 * temporary arrangement that must only persist until
6920Sstevel@tonic-gate 		 * weak binding drops).  We don't acquire thread_lock
6930Sstevel@tonic-gate 		 * here so even as this code executes t_bound_cpu
6940Sstevel@tonic-gate 		 * may be changing.  So we disable preemption and
6950Sstevel@tonic-gate 		 * a) in the case that t_bound_cpu changes while we
6960Sstevel@tonic-gate 		 * have preemption disabled kprunrun will be set
6970Sstevel@tonic-gate 		 * asynchronously, and b) if before disabling
6980Sstevel@tonic-gate 		 * preemption we were already on a different cpu to
6990Sstevel@tonic-gate 		 * our t_bound_cpu then we set kprunrun ourselves
7000Sstevel@tonic-gate 		 * to force a trip through the dispatcher when
7010Sstevel@tonic-gate 		 * preemption is enabled.
7020Sstevel@tonic-gate 		 */
7030Sstevel@tonic-gate 		kpreempt_disable();
7040Sstevel@tonic-gate 		if (t->t_bound_cpu &&
7050Sstevel@tonic-gate 		    t->t_weakbound_cpu != t->t_bound_cpu)
7060Sstevel@tonic-gate 			CPU->cpu_kprunrun = 1;
7070Sstevel@tonic-gate 		t->t_weakbound_cpu = NULL;
7080Sstevel@tonic-gate 		membar_producer();
7090Sstevel@tonic-gate 		kpreempt_enable();
7100Sstevel@tonic-gate 	}
7110Sstevel@tonic-gate }
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate /*
7140Sstevel@tonic-gate  * weakbinding_stop can be used to temporarily cause weakbindings made
7150Sstevel@tonic-gate  * with thread_nomigrate to be satisfied through the stronger action of
7160Sstevel@tonic-gate  * kpreempt_disable.  weakbinding_start recommences normal weakbinding.
7170Sstevel@tonic-gate  */
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate void
7200Sstevel@tonic-gate weakbinding_stop(void)
7210Sstevel@tonic-gate {
7220Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
7230Sstevel@tonic-gate 	weakbindingbarrier = 1;
7240Sstevel@tonic-gate 	membar_producer();	/* make visible before subsequent thread_lock */
7250Sstevel@tonic-gate }
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate void
7280Sstevel@tonic-gate weakbinding_start(void)
7290Sstevel@tonic-gate {
7300Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
7310Sstevel@tonic-gate 	weakbindingbarrier = 0;
7320Sstevel@tonic-gate }
7330Sstevel@tonic-gate 
7345834Spt157919 void
7355834Spt157919 null_xcall(void)
7365834Spt157919 {
7375834Spt157919 }
7385834Spt157919 
7390Sstevel@tonic-gate /*
7400Sstevel@tonic-gate  * This routine is called to place the CPUs in a safe place so that
7410Sstevel@tonic-gate  * one of them can be taken off line or placed on line.  What we are
7420Sstevel@tonic-gate  * trying to do here is prevent a thread from traversing the list
7430Sstevel@tonic-gate  * of active CPUs while we are changing it or from getting placed on
7440Sstevel@tonic-gate  * the run queue of a CPU that has just gone off line.  We do this by
7450Sstevel@tonic-gate  * creating a thread with the highest possible prio for each CPU and
7460Sstevel@tonic-gate  * having it call this routine.  The advantage of this method is that
7470Sstevel@tonic-gate  * we can eliminate all checks for CPU_ACTIVE in the disp routines.
7480Sstevel@tonic-gate  * This makes disp faster at the expense of making p_online() slower
7490Sstevel@tonic-gate  * which is a good trade off.
7500Sstevel@tonic-gate  */
7510Sstevel@tonic-gate static void
7525295Srandyf cpu_pause(int index)
7530Sstevel@tonic-gate {
7540Sstevel@tonic-gate 	int s;
7550Sstevel@tonic-gate 	struct _cpu_pause_info *cpi = &cpu_pause_info;
7565295Srandyf 	volatile char *safe = &safe_list[index];
7575295Srandyf 	long    lindex = index;
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	ASSERT((curthread->t_bound_cpu != NULL) || (*safe == PAUSE_DIE));
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 	while (*safe != PAUSE_DIE) {
7620Sstevel@tonic-gate 		*safe = PAUSE_READY;
7630Sstevel@tonic-gate 		membar_enter();		/* make sure stores are flushed */
7640Sstevel@tonic-gate 		sema_v(&cpi->cp_sem);	/* signal requesting thread */
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 		/*
7670Sstevel@tonic-gate 		 * Wait here until all pause threads are running.  That
7680Sstevel@tonic-gate 		 * indicates that it's safe to do the spl.  Until
7690Sstevel@tonic-gate 		 * cpu_pause_info.cp_go is set, we don't want to spl
7700Sstevel@tonic-gate 		 * because that might block clock interrupts needed
7710Sstevel@tonic-gate 		 * to preempt threads on other CPUs.
7720Sstevel@tonic-gate 		 */
7730Sstevel@tonic-gate 		while (cpi->cp_go == 0)
7740Sstevel@tonic-gate 			;
7750Sstevel@tonic-gate 		/*
7760Sstevel@tonic-gate 		 * Even though we are at the highest disp prio, we need
7770Sstevel@tonic-gate 		 * to block out all interrupts below LOCK_LEVEL so that
7780Sstevel@tonic-gate 		 * an intr doesn't come in, wake up a thread, and call
7790Sstevel@tonic-gate 		 * setbackdq/setfrontdq.
7800Sstevel@tonic-gate 		 */
7810Sstevel@tonic-gate 		s = splhigh();
7825295Srandyf 		/*
7835295Srandyf 		 * if cpu_pause_func() has been set then call it using
7845295Srandyf 		 * index as the argument, currently only used by
7855295Srandyf 		 * cpr_suspend_cpus().  This function is used as the
7865295Srandyf 		 * code to execute on the "paused" cpu's when a machine
7875295Srandyf 		 * comes out of a sleep state and CPU's were powered off.
7885295Srandyf 		 * (could also be used for hotplugging CPU's).
7895295Srandyf 		 */
7905295Srandyf 		if (cpu_pause_func != NULL)
7915295Srandyf 			(*cpu_pause_func)((void *)lindex);
7923446Smrj 
7933446Smrj 		mach_cpu_pause(safe);
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 		splx(s);
7960Sstevel@tonic-gate 		/*
7970Sstevel@tonic-gate 		 * Waiting is at an end. Switch out of cpu_pause
7980Sstevel@tonic-gate 		 * loop and resume useful work.
7990Sstevel@tonic-gate 		 */
8000Sstevel@tonic-gate 		swtch();
8010Sstevel@tonic-gate 	}
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	mutex_enter(&pause_free_mutex);
8040Sstevel@tonic-gate 	*safe = PAUSE_DEAD;
8050Sstevel@tonic-gate 	cv_broadcast(&pause_free_cv);
8060Sstevel@tonic-gate 	mutex_exit(&pause_free_mutex);
8070Sstevel@tonic-gate }
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate /*
8100Sstevel@tonic-gate  * Allow the cpus to start running again.
8110Sstevel@tonic-gate  */
8120Sstevel@tonic-gate void
8130Sstevel@tonic-gate start_cpus()
8140Sstevel@tonic-gate {
8150Sstevel@tonic-gate 	int i;
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
8180Sstevel@tonic-gate 	ASSERT(cpu_pause_info.cp_paused);
8190Sstevel@tonic-gate 	cpu_pause_info.cp_paused = NULL;
8200Sstevel@tonic-gate 	for (i = 0; i < NCPU; i++)
8210Sstevel@tonic-gate 		safe_list[i] = PAUSE_IDLE;
8220Sstevel@tonic-gate 	membar_enter();			/* make sure stores are flushed */
8230Sstevel@tonic-gate 	affinity_clear();
8240Sstevel@tonic-gate 	splx(cpu_pause_info.cp_spl);
8250Sstevel@tonic-gate 	kpreempt_enable();
8260Sstevel@tonic-gate }
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate /*
8290Sstevel@tonic-gate  * Allocate a pause thread for a CPU.
8300Sstevel@tonic-gate  */
8310Sstevel@tonic-gate static void
8320Sstevel@tonic-gate cpu_pause_alloc(cpu_t *cp)
8330Sstevel@tonic-gate {
8340Sstevel@tonic-gate 	kthread_id_t	t;
8355295Srandyf 	long		cpun = cp->cpu_id;
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	/*
8380Sstevel@tonic-gate 	 * Note, v.v_nglobpris will not change value as long as I hold
8390Sstevel@tonic-gate 	 * cpu_lock.
8400Sstevel@tonic-gate 	 */
8415295Srandyf 	t = thread_create(NULL, 0, cpu_pause, (void *)cpun,
8420Sstevel@tonic-gate 	    0, &p0, TS_STOPPED, v.v_nglobpris - 1);
8430Sstevel@tonic-gate 	thread_lock(t);
8440Sstevel@tonic-gate 	t->t_bound_cpu = cp;
8450Sstevel@tonic-gate 	t->t_disp_queue = cp->cpu_disp;
8460Sstevel@tonic-gate 	t->t_affinitycnt = 1;
8470Sstevel@tonic-gate 	t->t_preempt = 1;
8480Sstevel@tonic-gate 	thread_unlock(t);
8490Sstevel@tonic-gate 	cp->cpu_pause_thread = t;
8500Sstevel@tonic-gate 	/*
8510Sstevel@tonic-gate 	 * Registering a thread in the callback table is usually done
8520Sstevel@tonic-gate 	 * in the initialization code of the thread.  In this
8530Sstevel@tonic-gate 	 * case, we do it right after thread creation because the
8540Sstevel@tonic-gate 	 * thread itself may never run, and we need to register the
8550Sstevel@tonic-gate 	 * fact that it is safe for cpr suspend.
8560Sstevel@tonic-gate 	 */
8570Sstevel@tonic-gate 	CALLB_CPR_INIT_SAFE(t, "cpu_pause");
8580Sstevel@tonic-gate }
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate /*
8610Sstevel@tonic-gate  * Free a pause thread for a CPU.
8620Sstevel@tonic-gate  */
8630Sstevel@tonic-gate static void
8640Sstevel@tonic-gate cpu_pause_free(cpu_t *cp)
8650Sstevel@tonic-gate {
8660Sstevel@tonic-gate 	kthread_id_t	t;
8670Sstevel@tonic-gate 	int		cpun = cp->cpu_id;
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
8700Sstevel@tonic-gate 	/*
8710Sstevel@tonic-gate 	 * We have to get the thread and tell him to die.
8720Sstevel@tonic-gate 	 */
8730Sstevel@tonic-gate 	if ((t = cp->cpu_pause_thread) == NULL) {
8740Sstevel@tonic-gate 		ASSERT(safe_list[cpun] == PAUSE_IDLE);
8750Sstevel@tonic-gate 		return;
8760Sstevel@tonic-gate 	}
8770Sstevel@tonic-gate 	thread_lock(t);
8780Sstevel@tonic-gate 	t->t_cpu = CPU;		/* disp gets upset if last cpu is quiesced. */
8790Sstevel@tonic-gate 	t->t_bound_cpu = NULL;	/* Must un-bind; cpu may not be running. */
8800Sstevel@tonic-gate 	t->t_pri = v.v_nglobpris - 1;
8810Sstevel@tonic-gate 	ASSERT(safe_list[cpun] == PAUSE_IDLE);
8820Sstevel@tonic-gate 	safe_list[cpun] = PAUSE_DIE;
8830Sstevel@tonic-gate 	THREAD_TRANSITION(t);
8840Sstevel@tonic-gate 	setbackdq(t);
8850Sstevel@tonic-gate 	thread_unlock_nopreempt(t);
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 	/*
8880Sstevel@tonic-gate 	 * If we don't wait for the thread to actually die, it may try to
8890Sstevel@tonic-gate 	 * run on the wrong cpu as part of an actual call to pause_cpus().
8900Sstevel@tonic-gate 	 */
8910Sstevel@tonic-gate 	mutex_enter(&pause_free_mutex);
8920Sstevel@tonic-gate 	while (safe_list[cpun] != PAUSE_DEAD) {
8930Sstevel@tonic-gate 		cv_wait(&pause_free_cv, &pause_free_mutex);
8940Sstevel@tonic-gate 	}
8950Sstevel@tonic-gate 	mutex_exit(&pause_free_mutex);
8960Sstevel@tonic-gate 	safe_list[cpun] = PAUSE_IDLE;
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 	cp->cpu_pause_thread = NULL;
8990Sstevel@tonic-gate }
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate /*
9020Sstevel@tonic-gate  * Initialize basic structures for pausing CPUs.
9030Sstevel@tonic-gate  */
9040Sstevel@tonic-gate void
9050Sstevel@tonic-gate cpu_pause_init()
9060Sstevel@tonic-gate {
9070Sstevel@tonic-gate 	sema_init(&cpu_pause_info.cp_sem, 0, NULL, SEMA_DEFAULT, NULL);
9080Sstevel@tonic-gate 	/*
9090Sstevel@tonic-gate 	 * Create initial CPU pause thread.
9100Sstevel@tonic-gate 	 */
9110Sstevel@tonic-gate 	cpu_pause_alloc(CPU);
9120Sstevel@tonic-gate }
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate /*
9150Sstevel@tonic-gate  * Start the threads used to pause another CPU.
9160Sstevel@tonic-gate  */
9170Sstevel@tonic-gate static int
9180Sstevel@tonic-gate cpu_pause_start(processorid_t cpu_id)
9190Sstevel@tonic-gate {
9200Sstevel@tonic-gate 	int	i;
9210Sstevel@tonic-gate 	int	cpu_count = 0;
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate 	for (i = 0; i < NCPU; i++) {
9240Sstevel@tonic-gate 		cpu_t		*cp;
9250Sstevel@tonic-gate 		kthread_id_t	t;
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate 		cp = cpu[i];
9280Sstevel@tonic-gate 		if (!CPU_IN_SET(cpu_available, i) || (i == cpu_id)) {
9290Sstevel@tonic-gate 			safe_list[i] = PAUSE_WAIT;
9300Sstevel@tonic-gate 			continue;
9310Sstevel@tonic-gate 		}
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate 		/*
9340Sstevel@tonic-gate 		 * Skip CPU if it is quiesced or not yet started.
9350Sstevel@tonic-gate 		 */
9360Sstevel@tonic-gate 		if ((cp->cpu_flags & (CPU_QUIESCED | CPU_READY)) != CPU_READY) {
9370Sstevel@tonic-gate 			safe_list[i] = PAUSE_WAIT;
9380Sstevel@tonic-gate 			continue;
9390Sstevel@tonic-gate 		}
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 		/*
9420Sstevel@tonic-gate 		 * Start this CPU's pause thread.
9430Sstevel@tonic-gate 		 */
9440Sstevel@tonic-gate 		t = cp->cpu_pause_thread;
9450Sstevel@tonic-gate 		thread_lock(t);
9460Sstevel@tonic-gate 		/*
9470Sstevel@tonic-gate 		 * Reset the priority, since nglobpris may have
9480Sstevel@tonic-gate 		 * changed since the thread was created, if someone
9490Sstevel@tonic-gate 		 * has loaded the RT (or some other) scheduling
9500Sstevel@tonic-gate 		 * class.
9510Sstevel@tonic-gate 		 */
9520Sstevel@tonic-gate 		t->t_pri = v.v_nglobpris - 1;
9530Sstevel@tonic-gate 		THREAD_TRANSITION(t);
9540Sstevel@tonic-gate 		setbackdq(t);
9550Sstevel@tonic-gate 		thread_unlock_nopreempt(t);
9560Sstevel@tonic-gate 		++cpu_count;
9570Sstevel@tonic-gate 	}
9580Sstevel@tonic-gate 	return (cpu_count);
9590Sstevel@tonic-gate }
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate /*
9630Sstevel@tonic-gate  * Pause all of the CPUs except the one we are on by creating a high
9640Sstevel@tonic-gate  * priority thread bound to those CPUs.
9650Sstevel@tonic-gate  *
9660Sstevel@tonic-gate  * Note that one must be extremely careful regarding code
9670Sstevel@tonic-gate  * executed while CPUs are paused.  Since a CPU may be paused
9680Sstevel@tonic-gate  * while a thread scheduling on that CPU is holding an adaptive
9690Sstevel@tonic-gate  * lock, code executed with CPUs paused must not acquire adaptive
9700Sstevel@tonic-gate  * (or low-level spin) locks.  Also, such code must not block,
9710Sstevel@tonic-gate  * since the thread that is supposed to initiate the wakeup may
9720Sstevel@tonic-gate  * never run.
9730Sstevel@tonic-gate  *
9740Sstevel@tonic-gate  * With a few exceptions, the restrictions on code executed with CPUs
9750Sstevel@tonic-gate  * paused match those for code executed at high-level interrupt
9760Sstevel@tonic-gate  * context.
9770Sstevel@tonic-gate  */
9780Sstevel@tonic-gate void
9790Sstevel@tonic-gate pause_cpus(cpu_t *off_cp)
9800Sstevel@tonic-gate {
9810Sstevel@tonic-gate 	processorid_t	cpu_id;
9820Sstevel@tonic-gate 	int		i;
9830Sstevel@tonic-gate 	struct _cpu_pause_info	*cpi = &cpu_pause_info;
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
9860Sstevel@tonic-gate 	ASSERT(cpi->cp_paused == NULL);
9870Sstevel@tonic-gate 	cpi->cp_count = 0;
9880Sstevel@tonic-gate 	cpi->cp_go = 0;
9890Sstevel@tonic-gate 	for (i = 0; i < NCPU; i++)
9900Sstevel@tonic-gate 		safe_list[i] = PAUSE_IDLE;
9910Sstevel@tonic-gate 	kpreempt_disable();
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate 	/*
9940Sstevel@tonic-gate 	 * If running on the cpu that is going offline, get off it.
9950Sstevel@tonic-gate 	 * This is so that it won't be necessary to rechoose a CPU
9960Sstevel@tonic-gate 	 * when done.
9970Sstevel@tonic-gate 	 */
9980Sstevel@tonic-gate 	if (CPU == off_cp)
9990Sstevel@tonic-gate 		cpu_id = off_cp->cpu_next_part->cpu_id;
10000Sstevel@tonic-gate 	else
10010Sstevel@tonic-gate 		cpu_id = CPU->cpu_id;
10020Sstevel@tonic-gate 	affinity_set(cpu_id);
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate 	/*
10050Sstevel@tonic-gate 	 * Start the pause threads and record how many were started
10060Sstevel@tonic-gate 	 */
10070Sstevel@tonic-gate 	cpi->cp_count = cpu_pause_start(cpu_id);
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 	/*
10100Sstevel@tonic-gate 	 * Now wait for all CPUs to be running the pause thread.
10110Sstevel@tonic-gate 	 */
10120Sstevel@tonic-gate 	while (cpi->cp_count > 0) {
10130Sstevel@tonic-gate 		/*
10140Sstevel@tonic-gate 		 * Spin reading the count without grabbing the disp
10150Sstevel@tonic-gate 		 * lock to make sure we don't prevent the pause
10160Sstevel@tonic-gate 		 * threads from getting the lock.
10170Sstevel@tonic-gate 		 */
10180Sstevel@tonic-gate 		while (sema_held(&cpi->cp_sem))
10190Sstevel@tonic-gate 			;
10200Sstevel@tonic-gate 		if (sema_tryp(&cpi->cp_sem))
10210Sstevel@tonic-gate 			--cpi->cp_count;
10220Sstevel@tonic-gate 	}
10230Sstevel@tonic-gate 	cpi->cp_go = 1;			/* all have reached cpu_pause */
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 	/*
10260Sstevel@tonic-gate 	 * Now wait for all CPUs to spl. (Transition from PAUSE_READY
10270Sstevel@tonic-gate 	 * to PAUSE_WAIT.)
10280Sstevel@tonic-gate 	 */
10290Sstevel@tonic-gate 	for (i = 0; i < NCPU; i++) {
10300Sstevel@tonic-gate 		while (safe_list[i] != PAUSE_WAIT)
10310Sstevel@tonic-gate 			;
10320Sstevel@tonic-gate 	}
10330Sstevel@tonic-gate 	cpi->cp_spl = splhigh();	/* block dispatcher on this CPU */
10340Sstevel@tonic-gate 	cpi->cp_paused = curthread;
10350Sstevel@tonic-gate }
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate /*
10380Sstevel@tonic-gate  * Check whether the current thread has CPUs paused
10390Sstevel@tonic-gate  */
10400Sstevel@tonic-gate int
10410Sstevel@tonic-gate cpus_paused(void)
10420Sstevel@tonic-gate {
10430Sstevel@tonic-gate 	if (cpu_pause_info.cp_paused != NULL) {
10440Sstevel@tonic-gate 		ASSERT(cpu_pause_info.cp_paused == curthread);
10450Sstevel@tonic-gate 		return (1);
10460Sstevel@tonic-gate 	}
10470Sstevel@tonic-gate 	return (0);
10480Sstevel@tonic-gate }
10490Sstevel@tonic-gate 
10500Sstevel@tonic-gate static cpu_t *
10510Sstevel@tonic-gate cpu_get_all(processorid_t cpun)
10520Sstevel@tonic-gate {
10530Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 	if (cpun >= NCPU || cpun < 0 || !CPU_IN_SET(cpu_available, cpun))
10560Sstevel@tonic-gate 		return (NULL);
10570Sstevel@tonic-gate 	return (cpu[cpun]);
10580Sstevel@tonic-gate }
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate /*
10610Sstevel@tonic-gate  * Check whether cpun is a valid processor id and whether it should be
10620Sstevel@tonic-gate  * visible from the current zone. If it is, return a pointer to the
10630Sstevel@tonic-gate  * associated CPU structure.
10640Sstevel@tonic-gate  */
10650Sstevel@tonic-gate cpu_t *
10660Sstevel@tonic-gate cpu_get(processorid_t cpun)
10670Sstevel@tonic-gate {
10680Sstevel@tonic-gate 	cpu_t *c;
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
10710Sstevel@tonic-gate 	c = cpu_get_all(cpun);
10720Sstevel@tonic-gate 	if (c != NULL && !INGLOBALZONE(curproc) && pool_pset_enabled() &&
10730Sstevel@tonic-gate 	    zone_pset_get(curproc->p_zone) != cpupart_query_cpu(c))
10740Sstevel@tonic-gate 		return (NULL);
10750Sstevel@tonic-gate 	return (c);
10760Sstevel@tonic-gate }
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate /*
10790Sstevel@tonic-gate  * The following functions should be used to check CPU states in the kernel.
10800Sstevel@tonic-gate  * They should be invoked with cpu_lock held.  Kernel subsystems interested
10810Sstevel@tonic-gate  * in CPU states should *not* use cpu_get_state() and various P_ONLINE/etc
10820Sstevel@tonic-gate  * states.  Those are for user-land (and system call) use only.
10830Sstevel@tonic-gate  */
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate /*
10860Sstevel@tonic-gate  * Determine whether the CPU is online and handling interrupts.
10870Sstevel@tonic-gate  */
10880Sstevel@tonic-gate int
10890Sstevel@tonic-gate cpu_is_online(cpu_t *cpu)
10900Sstevel@tonic-gate {
10910Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
10920Sstevel@tonic-gate 	return (cpu_flagged_online(cpu->cpu_flags));
10930Sstevel@tonic-gate }
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate /*
10960Sstevel@tonic-gate  * Determine whether the CPU is offline (this includes spare and faulted).
10970Sstevel@tonic-gate  */
10980Sstevel@tonic-gate int
10990Sstevel@tonic-gate cpu_is_offline(cpu_t *cpu)
11000Sstevel@tonic-gate {
11010Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
11020Sstevel@tonic-gate 	return (cpu_flagged_offline(cpu->cpu_flags));
11030Sstevel@tonic-gate }
11040Sstevel@tonic-gate 
11050Sstevel@tonic-gate /*
11060Sstevel@tonic-gate  * Determine whether the CPU is powered off.
11070Sstevel@tonic-gate  */
11080Sstevel@tonic-gate int
11090Sstevel@tonic-gate cpu_is_poweredoff(cpu_t *cpu)
11100Sstevel@tonic-gate {
11110Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
11120Sstevel@tonic-gate 	return (cpu_flagged_poweredoff(cpu->cpu_flags));
11130Sstevel@tonic-gate }
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate /*
11160Sstevel@tonic-gate  * Determine whether the CPU is handling interrupts.
11170Sstevel@tonic-gate  */
11180Sstevel@tonic-gate int
11190Sstevel@tonic-gate cpu_is_nointr(cpu_t *cpu)
11200Sstevel@tonic-gate {
11210Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
11220Sstevel@tonic-gate 	return (cpu_flagged_nointr(cpu->cpu_flags));
11230Sstevel@tonic-gate }
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate /*
11260Sstevel@tonic-gate  * Determine whether the CPU is active (scheduling threads).
11270Sstevel@tonic-gate  */
11280Sstevel@tonic-gate int
11290Sstevel@tonic-gate cpu_is_active(cpu_t *cpu)
11300Sstevel@tonic-gate {
11310Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
11320Sstevel@tonic-gate 	return (cpu_flagged_active(cpu->cpu_flags));
11330Sstevel@tonic-gate }
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate /*
11360Sstevel@tonic-gate  * Same as above, but these require cpu_flags instead of cpu_t pointers.
11370Sstevel@tonic-gate  */
11380Sstevel@tonic-gate int
11390Sstevel@tonic-gate cpu_flagged_online(cpu_flag_t cpu_flags)
11400Sstevel@tonic-gate {
11410Sstevel@tonic-gate 	return (cpu_flagged_active(cpu_flags) &&
11420Sstevel@tonic-gate 	    (cpu_flags & CPU_ENABLE));
11430Sstevel@tonic-gate }
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate int
11460Sstevel@tonic-gate cpu_flagged_offline(cpu_flag_t cpu_flags)
11470Sstevel@tonic-gate {
11480Sstevel@tonic-gate 	return (((cpu_flags & CPU_POWEROFF) == 0) &&
11490Sstevel@tonic-gate 	    ((cpu_flags & (CPU_READY | CPU_OFFLINE)) != CPU_READY));
11500Sstevel@tonic-gate }
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate int
11530Sstevel@tonic-gate cpu_flagged_poweredoff(cpu_flag_t cpu_flags)
11540Sstevel@tonic-gate {
11550Sstevel@tonic-gate 	return ((cpu_flags & CPU_POWEROFF) == CPU_POWEROFF);
11560Sstevel@tonic-gate }
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate int
11590Sstevel@tonic-gate cpu_flagged_nointr(cpu_flag_t cpu_flags)
11600Sstevel@tonic-gate {
11610Sstevel@tonic-gate 	return (cpu_flagged_active(cpu_flags) &&
11620Sstevel@tonic-gate 	    (cpu_flags & CPU_ENABLE) == 0);
11630Sstevel@tonic-gate }
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate int
11660Sstevel@tonic-gate cpu_flagged_active(cpu_flag_t cpu_flags)
11670Sstevel@tonic-gate {
11680Sstevel@tonic-gate 	return (((cpu_flags & (CPU_POWEROFF | CPU_FAULTED | CPU_SPARE)) == 0) &&
11690Sstevel@tonic-gate 	    ((cpu_flags & (CPU_READY | CPU_OFFLINE)) == CPU_READY));
11700Sstevel@tonic-gate }
11710Sstevel@tonic-gate 
11720Sstevel@tonic-gate /*
11730Sstevel@tonic-gate  * Bring the indicated CPU online.
11740Sstevel@tonic-gate  */
11750Sstevel@tonic-gate int
11760Sstevel@tonic-gate cpu_online(cpu_t *cp)
11770Sstevel@tonic-gate {
11780Sstevel@tonic-gate 	int	error = 0;
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate 	/*
11810Sstevel@tonic-gate 	 * Handle on-line request.
11820Sstevel@tonic-gate 	 *	This code must put the new CPU on the active list before
11830Sstevel@tonic-gate 	 *	starting it because it will not be paused, and will start
11840Sstevel@tonic-gate 	 * 	using the active list immediately.  The real start occurs
11850Sstevel@tonic-gate 	 *	when the CPU_QUIESCED flag is turned off.
11860Sstevel@tonic-gate 	 */
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 	/*
11910Sstevel@tonic-gate 	 * Put all the cpus into a known safe place.
11920Sstevel@tonic-gate 	 * No mutexes can be entered while CPUs are paused.
11930Sstevel@tonic-gate 	 */
11940Sstevel@tonic-gate 	error = mp_cpu_start(cp);	/* arch-dep hook */
11950Sstevel@tonic-gate 	if (error == 0) {
11963703Sesaxe 		pg_cpupart_in(cp, cp->cpu_part);
11970Sstevel@tonic-gate 		pause_cpus(NULL);
11980Sstevel@tonic-gate 		cpu_add_active_internal(cp);
11990Sstevel@tonic-gate 		if (cp->cpu_flags & CPU_FAULTED) {
12000Sstevel@tonic-gate 			cp->cpu_flags &= ~CPU_FAULTED;
12010Sstevel@tonic-gate 			mp_cpu_faulted_exit(cp);
12020Sstevel@tonic-gate 		}
12030Sstevel@tonic-gate 		cp->cpu_flags &= ~(CPU_QUIESCED | CPU_OFFLINE | CPU_FROZEN |
12040Sstevel@tonic-gate 		    CPU_SPARE);
12050Sstevel@tonic-gate 		start_cpus();
12060Sstevel@tonic-gate 		cpu_stats_kstat_create(cp);
12070Sstevel@tonic-gate 		cpu_create_intrstat(cp);
12080Sstevel@tonic-gate 		lgrp_kstat_create(cp);
12090Sstevel@tonic-gate 		cpu_state_change_notify(cp->cpu_id, CPU_ON);
12100Sstevel@tonic-gate 		cpu_intr_enable(cp);	/* arch-dep hook */
12113446Smrj 		cpu_set_state(cp);
12120Sstevel@tonic-gate 		cyclic_online(cp);
12138048SMadhavan.Venkataraman@Sun.COM 		/*
12148048SMadhavan.Venkataraman@Sun.COM 		 * This has to be called only after cyclic_online(). This
12158048SMadhavan.Venkataraman@Sun.COM 		 * function uses cyclics.
12168048SMadhavan.Venkataraman@Sun.COM 		 */
12178048SMadhavan.Venkataraman@Sun.COM 		callout_cpu_online(cp);
12180Sstevel@tonic-gate 		poke_cpu(cp->cpu_id);
12190Sstevel@tonic-gate 	}
12200Sstevel@tonic-gate 
12210Sstevel@tonic-gate 	return (error);
12220Sstevel@tonic-gate }
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate /*
12250Sstevel@tonic-gate  * Take the indicated CPU offline.
12260Sstevel@tonic-gate  */
12270Sstevel@tonic-gate int
12280Sstevel@tonic-gate cpu_offline(cpu_t *cp, int flags)
12290Sstevel@tonic-gate {
12300Sstevel@tonic-gate 	cpupart_t *pp;
12310Sstevel@tonic-gate 	int	error = 0;
12320Sstevel@tonic-gate 	cpu_t	*ncp;
12330Sstevel@tonic-gate 	int	intr_enable;
12340Sstevel@tonic-gate 	int	cyclic_off = 0;
12358566SMadhavan.Venkataraman@Sun.COM 	int	callout_off = 0;
12360Sstevel@tonic-gate 	int	loop_count;
12370Sstevel@tonic-gate 	int	no_quiesce = 0;
12380Sstevel@tonic-gate 	int	(*bound_func)(struct cpu *, int);
12390Sstevel@tonic-gate 	kthread_t *t;
12400Sstevel@tonic-gate 	lpl_t	*cpu_lpl;
12410Sstevel@tonic-gate 	proc_t	*p;
12420Sstevel@tonic-gate 	int	lgrp_diff_lpl;
12436298Sakolb 	boolean_t unbind_all_threads = (flags & CPU_FORCED) != 0;
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate 	/*
12480Sstevel@tonic-gate 	 * If we're going from faulted or spare to offline, just
12490Sstevel@tonic-gate 	 * clear these flags and update CPU state.
12500Sstevel@tonic-gate 	 */
12510Sstevel@tonic-gate 	if (cp->cpu_flags & (CPU_FAULTED | CPU_SPARE)) {
12520Sstevel@tonic-gate 		if (cp->cpu_flags & CPU_FAULTED) {
12530Sstevel@tonic-gate 			cp->cpu_flags &= ~CPU_FAULTED;
12540Sstevel@tonic-gate 			mp_cpu_faulted_exit(cp);
12550Sstevel@tonic-gate 		}
12560Sstevel@tonic-gate 		cp->cpu_flags &= ~CPU_SPARE;
12570Sstevel@tonic-gate 		cpu_set_state(cp);
12580Sstevel@tonic-gate 		return (0);
12590Sstevel@tonic-gate 	}
12600Sstevel@tonic-gate 
12610Sstevel@tonic-gate 	/*
12620Sstevel@tonic-gate 	 * Handle off-line request.
12630Sstevel@tonic-gate 	 */
12640Sstevel@tonic-gate 	pp = cp->cpu_part;
12650Sstevel@tonic-gate 	/*
12660Sstevel@tonic-gate 	 * Don't offline last online CPU in partition
12670Sstevel@tonic-gate 	 */
12680Sstevel@tonic-gate 	if (ncpus_online <= 1 || pp->cp_ncpus <= 1 || cpu_intr_count(cp) < 2)
12690Sstevel@tonic-gate 		return (EBUSY);
12700Sstevel@tonic-gate 	/*
12716298Sakolb 	 * Unbind all soft-bound threads bound to our CPU and hard bound threads
12726298Sakolb 	 * if we were asked to.
12730Sstevel@tonic-gate 	 */
12746298Sakolb 	error = cpu_unbind(cp->cpu_id, unbind_all_threads);
12756298Sakolb 	if (error != 0)
12760Sstevel@tonic-gate 		return (error);
12770Sstevel@tonic-gate 	/*
12780Sstevel@tonic-gate 	 * We shouldn't be bound to this CPU ourselves.
12790Sstevel@tonic-gate 	 */
12800Sstevel@tonic-gate 	if (curthread->t_bound_cpu == cp)
12810Sstevel@tonic-gate 		return (EBUSY);
12820Sstevel@tonic-gate 
12830Sstevel@tonic-gate 	/*
12840Sstevel@tonic-gate 	 * Tell interested parties that this CPU is going offline.
12850Sstevel@tonic-gate 	 */
12860Sstevel@tonic-gate 	cpu_state_change_notify(cp->cpu_id, CPU_OFF);
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 	/*
12893434Sesaxe 	 * Tell the PG subsystem that the CPU is leaving the partition
12903434Sesaxe 	 */
12913434Sesaxe 	pg_cpupart_out(cp, pp);
12923434Sesaxe 
12933434Sesaxe 	/*
12940Sstevel@tonic-gate 	 * Take the CPU out of interrupt participation so we won't find
12950Sstevel@tonic-gate 	 * bound kernel threads.  If the architecture cannot completely
12960Sstevel@tonic-gate 	 * shut off interrupts on the CPU, don't quiesce it, but don't
12970Sstevel@tonic-gate 	 * run anything but interrupt thread... this is indicated by
12980Sstevel@tonic-gate 	 * the CPU_OFFLINE flag being on but the CPU_QUIESCE flag being
12990Sstevel@tonic-gate 	 * off.
13000Sstevel@tonic-gate 	 */
13010Sstevel@tonic-gate 	intr_enable = cp->cpu_flags & CPU_ENABLE;
13020Sstevel@tonic-gate 	if (intr_enable)
13030Sstevel@tonic-gate 		no_quiesce = cpu_intr_disable(cp);
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate 	/*
13060Sstevel@tonic-gate 	 * Record that we are aiming to offline this cpu.  This acts as
13070Sstevel@tonic-gate 	 * a barrier to further weak binding requests in thread_nomigrate
13080Sstevel@tonic-gate 	 * and also causes cpu_choose, disp_lowpri_cpu and setfrontdq to
13090Sstevel@tonic-gate 	 * lean away from this cpu.  Further strong bindings are already
13100Sstevel@tonic-gate 	 * avoided since we hold cpu_lock.  Since threads that are set
13110Sstevel@tonic-gate 	 * runnable around now and others coming off the target cpu are
13120Sstevel@tonic-gate 	 * directed away from the target, existing strong and weak bindings
13130Sstevel@tonic-gate 	 * (especially the latter) to the target cpu stand maximum chance of
13140Sstevel@tonic-gate 	 * being able to unbind during the short delay loop below (if other
13150Sstevel@tonic-gate 	 * unbound threads compete they may not see cpu in time to unbind
13160Sstevel@tonic-gate 	 * even if they would do so immediately.
13170Sstevel@tonic-gate 	 */
13180Sstevel@tonic-gate 	cpu_inmotion = cp;
13190Sstevel@tonic-gate 	membar_enter();
13200Sstevel@tonic-gate 
13210Sstevel@tonic-gate 	/*
13220Sstevel@tonic-gate 	 * Check for kernel threads (strong or weak) bound to that CPU.
13230Sstevel@tonic-gate 	 * Strongly bound threads may not unbind, and we'll have to return
13240Sstevel@tonic-gate 	 * EBUSY.  Weakly bound threads should always disappear - we've
13250Sstevel@tonic-gate 	 * stopped more weak binding with cpu_inmotion and existing
13260Sstevel@tonic-gate 	 * bindings will drain imminently (they may not block).  Nonetheless
13270Sstevel@tonic-gate 	 * we will wait for a fixed period for all bound threads to disappear.
13280Sstevel@tonic-gate 	 * Inactive interrupt threads are OK (they'll be in TS_FREE
13290Sstevel@tonic-gate 	 * state).  If test finds some bound threads, wait a few ticks
13300Sstevel@tonic-gate 	 * to give short-lived threads (such as interrupts) chance to
13310Sstevel@tonic-gate 	 * complete.  Note that if no_quiesce is set, i.e. this cpu
13320Sstevel@tonic-gate 	 * is required to service interrupts, then we take the route
13330Sstevel@tonic-gate 	 * that permits interrupt threads to be active (or bypassed).
13340Sstevel@tonic-gate 	 */
13350Sstevel@tonic-gate 	bound_func = no_quiesce ? disp_bound_threads : disp_bound_anythreads;
13360Sstevel@tonic-gate 
13370Sstevel@tonic-gate again:	for (loop_count = 0; (*bound_func)(cp, 0); loop_count++) {
13380Sstevel@tonic-gate 		if (loop_count >= 5) {
13390Sstevel@tonic-gate 			error = EBUSY;	/* some threads still bound */
13400Sstevel@tonic-gate 			break;
13410Sstevel@tonic-gate 		}
13420Sstevel@tonic-gate 
13430Sstevel@tonic-gate 		/*
13440Sstevel@tonic-gate 		 * If some threads were assigned, give them
13450Sstevel@tonic-gate 		 * a chance to complete or move.
13460Sstevel@tonic-gate 		 *
13470Sstevel@tonic-gate 		 * This assumes that the clock_thread is not bound
13480Sstevel@tonic-gate 		 * to any CPU, because the clock_thread is needed to
13490Sstevel@tonic-gate 		 * do the delay(hz/100).
13500Sstevel@tonic-gate 		 *
13510Sstevel@tonic-gate 		 * Note: we still hold the cpu_lock while waiting for
13520Sstevel@tonic-gate 		 * the next clock tick.  This is OK since it isn't
13530Sstevel@tonic-gate 		 * needed for anything else except processor_bind(2),
13540Sstevel@tonic-gate 		 * and system initialization.  If we drop the lock,
13550Sstevel@tonic-gate 		 * we would risk another p_online disabling the last
13560Sstevel@tonic-gate 		 * processor.
13570Sstevel@tonic-gate 		 */
13580Sstevel@tonic-gate 		delay(hz/100);
13590Sstevel@tonic-gate 	}
13600Sstevel@tonic-gate 
13618566SMadhavan.Venkataraman@Sun.COM 	if (error == 0 && callout_off == 0) {
13628566SMadhavan.Venkataraman@Sun.COM 		callout_cpu_offline(cp);
13638566SMadhavan.Venkataraman@Sun.COM 		callout_off = 1;
13648566SMadhavan.Venkataraman@Sun.COM 	}
13658566SMadhavan.Venkataraman@Sun.COM 
13660Sstevel@tonic-gate 	if (error == 0 && cyclic_off == 0) {
13670Sstevel@tonic-gate 		if (!cyclic_offline(cp)) {
13680Sstevel@tonic-gate 			/*
13690Sstevel@tonic-gate 			 * We must have bound cyclics...
13700Sstevel@tonic-gate 			 */
13710Sstevel@tonic-gate 			error = EBUSY;
13720Sstevel@tonic-gate 			goto out;
13730Sstevel@tonic-gate 		}
13740Sstevel@tonic-gate 		cyclic_off = 1;
13750Sstevel@tonic-gate 	}
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate 	/*
13780Sstevel@tonic-gate 	 * Call mp_cpu_stop() to perform any special operations
13790Sstevel@tonic-gate 	 * needed for this machine architecture to offline a CPU.
13800Sstevel@tonic-gate 	 */
13810Sstevel@tonic-gate 	if (error == 0)
13820Sstevel@tonic-gate 		error = mp_cpu_stop(cp);	/* arch-dep hook */
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate 	/*
13850Sstevel@tonic-gate 	 * If that all worked, take the CPU offline and decrement
13860Sstevel@tonic-gate 	 * ncpus_online.
13870Sstevel@tonic-gate 	 */
13880Sstevel@tonic-gate 	if (error == 0) {
13890Sstevel@tonic-gate 		/*
13900Sstevel@tonic-gate 		 * Put all the cpus into a known safe place.
13910Sstevel@tonic-gate 		 * No mutexes can be entered while CPUs are paused.
13920Sstevel@tonic-gate 		 */
13930Sstevel@tonic-gate 		pause_cpus(cp);
13940Sstevel@tonic-gate 		/*
13950Sstevel@tonic-gate 		 * Repeat the operation, if necessary, to make sure that
13960Sstevel@tonic-gate 		 * all outstanding low-level interrupts run to completion
13970Sstevel@tonic-gate 		 * before we set the CPU_QUIESCED flag.  It's also possible
13980Sstevel@tonic-gate 		 * that a thread has weak bound to the cpu despite our raising
13990Sstevel@tonic-gate 		 * cpu_inmotion above since it may have loaded that
14000Sstevel@tonic-gate 		 * value before the barrier became visible (this would have
14010Sstevel@tonic-gate 		 * to be the thread that was on the target cpu at the time
14020Sstevel@tonic-gate 		 * we raised the barrier).
14030Sstevel@tonic-gate 		 */
14040Sstevel@tonic-gate 		if ((!no_quiesce && cp->cpu_intr_actv != 0) ||
14050Sstevel@tonic-gate 		    (*bound_func)(cp, 1)) {
14060Sstevel@tonic-gate 			start_cpus();
14070Sstevel@tonic-gate 			(void) mp_cpu_start(cp);
14080Sstevel@tonic-gate 			goto again;
14090Sstevel@tonic-gate 		}
14100Sstevel@tonic-gate 		ncp = cp->cpu_next_part;
14110Sstevel@tonic-gate 		cpu_lpl = cp->cpu_lpl;
14120Sstevel@tonic-gate 		ASSERT(cpu_lpl != NULL);
14130Sstevel@tonic-gate 
14140Sstevel@tonic-gate 		/*
14150Sstevel@tonic-gate 		 * Remove the CPU from the list of active CPUs.
14160Sstevel@tonic-gate 		 */
14170Sstevel@tonic-gate 		cpu_remove_active(cp);
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 		/*
14200Sstevel@tonic-gate 		 * Walk the active process list and look for threads
14210Sstevel@tonic-gate 		 * whose home lgroup needs to be updated, or
14220Sstevel@tonic-gate 		 * the last CPU they run on is the one being offlined now.
14230Sstevel@tonic-gate 		 */
14240Sstevel@tonic-gate 
14250Sstevel@tonic-gate 		ASSERT(curthread->t_cpu != cp);
14260Sstevel@tonic-gate 		for (p = practive; p != NULL; p = p->p_next) {
14270Sstevel@tonic-gate 
14280Sstevel@tonic-gate 			t = p->p_tlist;
14290Sstevel@tonic-gate 
14300Sstevel@tonic-gate 			if (t == NULL)
14310Sstevel@tonic-gate 				continue;
14320Sstevel@tonic-gate 
14330Sstevel@tonic-gate 			lgrp_diff_lpl = 0;
14340Sstevel@tonic-gate 
14350Sstevel@tonic-gate 			do {
14360Sstevel@tonic-gate 				ASSERT(t->t_lpl != NULL);
14370Sstevel@tonic-gate 				/*
14380Sstevel@tonic-gate 				 * Taking last CPU in lpl offline
14390Sstevel@tonic-gate 				 * Rehome thread if it is in this lpl
14400Sstevel@tonic-gate 				 * Otherwise, update the count of how many
14410Sstevel@tonic-gate 				 * threads are in this CPU's lgroup but have
14420Sstevel@tonic-gate 				 * a different lpl.
14430Sstevel@tonic-gate 				 */
14440Sstevel@tonic-gate 
14450Sstevel@tonic-gate 				if (cpu_lpl->lpl_ncpu == 0) {
14460Sstevel@tonic-gate 					if (t->t_lpl == cpu_lpl)
14470Sstevel@tonic-gate 						lgrp_move_thread(t,
14480Sstevel@tonic-gate 						    lgrp_choose(t,
14490Sstevel@tonic-gate 						    t->t_cpupart), 0);
14500Sstevel@tonic-gate 					else if (t->t_lpl->lpl_lgrpid ==
14510Sstevel@tonic-gate 					    cpu_lpl->lpl_lgrpid)
14520Sstevel@tonic-gate 						lgrp_diff_lpl++;
14530Sstevel@tonic-gate 				}
14540Sstevel@tonic-gate 				ASSERT(t->t_lpl->lpl_ncpu > 0);
14550Sstevel@tonic-gate 
14560Sstevel@tonic-gate 				/*
14570Sstevel@tonic-gate 				 * Update CPU last ran on if it was this CPU
14580Sstevel@tonic-gate 				 */
14590Sstevel@tonic-gate 				if (t->t_cpu == cp && t->t_bound_cpu != cp)
14604718Smh27603 					t->t_cpu = disp_lowpri_cpu(ncp,
14614718Smh27603 					    t->t_lpl, t->t_pri, NULL);
14620Sstevel@tonic-gate 				ASSERT(t->t_cpu != cp || t->t_bound_cpu == cp ||
14630Sstevel@tonic-gate 				    t->t_weakbound_cpu == cp);
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate 				t = t->t_forw;
14660Sstevel@tonic-gate 			} while (t != p->p_tlist);
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate 			/*
14690Sstevel@tonic-gate 			 * Didn't find any threads in the same lgroup as this
14700Sstevel@tonic-gate 			 * CPU with a different lpl, so remove the lgroup from
14710Sstevel@tonic-gate 			 * the process lgroup bitmask.
14720Sstevel@tonic-gate 			 */
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate 			if (lgrp_diff_lpl == 0)
14750Sstevel@tonic-gate 				klgrpset_del(p->p_lgrpset, cpu_lpl->lpl_lgrpid);
14760Sstevel@tonic-gate 		}
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate 		/*
14790Sstevel@tonic-gate 		 * Walk thread list looking for threads that need to be
14800Sstevel@tonic-gate 		 * rehomed, since there are some threads that are not in
14810Sstevel@tonic-gate 		 * their process's p_tlist.
14820Sstevel@tonic-gate 		 */
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate 		t = curthread;
14850Sstevel@tonic-gate 		do {
14860Sstevel@tonic-gate 			ASSERT(t != NULL && t->t_lpl != NULL);
14870Sstevel@tonic-gate 
14880Sstevel@tonic-gate 			/*
14890Sstevel@tonic-gate 			 * Rehome threads with same lpl as this CPU when this
14900Sstevel@tonic-gate 			 * is the last CPU in the lpl.
14910Sstevel@tonic-gate 			 */
14920Sstevel@tonic-gate 
14930Sstevel@tonic-gate 			if ((cpu_lpl->lpl_ncpu == 0) && (t->t_lpl == cpu_lpl))
14940Sstevel@tonic-gate 				lgrp_move_thread(t,
14950Sstevel@tonic-gate 				    lgrp_choose(t, t->t_cpupart), 1);
14960Sstevel@tonic-gate 
14970Sstevel@tonic-gate 			ASSERT(t->t_lpl->lpl_ncpu > 0);
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate 			/*
15000Sstevel@tonic-gate 			 * Update CPU last ran on if it was this CPU
15010Sstevel@tonic-gate 			 */
15020Sstevel@tonic-gate 
15030Sstevel@tonic-gate 			if (t->t_cpu == cp && t->t_bound_cpu != cp) {
15040Sstevel@tonic-gate 				t->t_cpu = disp_lowpri_cpu(ncp,
15050Sstevel@tonic-gate 				    t->t_lpl, t->t_pri, NULL);
15060Sstevel@tonic-gate 			}
15070Sstevel@tonic-gate 			ASSERT(t->t_cpu != cp || t->t_bound_cpu == cp ||
15080Sstevel@tonic-gate 			    t->t_weakbound_cpu == cp);
15090Sstevel@tonic-gate 			t = t->t_next;
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate 		} while (t != curthread);
15120Sstevel@tonic-gate 		ASSERT((cp->cpu_flags & (CPU_FAULTED | CPU_SPARE)) == 0);
15130Sstevel@tonic-gate 		cp->cpu_flags |= CPU_OFFLINE;
15140Sstevel@tonic-gate 		disp_cpu_inactive(cp);
15150Sstevel@tonic-gate 		if (!no_quiesce)
15160Sstevel@tonic-gate 			cp->cpu_flags |= CPU_QUIESCED;
15170Sstevel@tonic-gate 		ncpus_online--;
15180Sstevel@tonic-gate 		cpu_set_state(cp);
15190Sstevel@tonic-gate 		cpu_inmotion = NULL;
15200Sstevel@tonic-gate 		start_cpus();
15210Sstevel@tonic-gate 		cpu_stats_kstat_destroy(cp);
15220Sstevel@tonic-gate 		cpu_delete_intrstat(cp);
15230Sstevel@tonic-gate 		lgrp_kstat_destroy(cp);
15240Sstevel@tonic-gate 	}
15250Sstevel@tonic-gate 
15260Sstevel@tonic-gate out:
15270Sstevel@tonic-gate 	cpu_inmotion = NULL;
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate 	/*
15300Sstevel@tonic-gate 	 * If we failed, re-enable interrupts.
15310Sstevel@tonic-gate 	 * Do this even if cpu_intr_disable returned an error, because
15320Sstevel@tonic-gate 	 * it may have partially disabled interrupts.
15330Sstevel@tonic-gate 	 */
15340Sstevel@tonic-gate 	if (error && intr_enable)
15350Sstevel@tonic-gate 		cpu_intr_enable(cp);
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate 	/*
15380Sstevel@tonic-gate 	 * If we failed, but managed to offline the cyclic subsystem on this
15390Sstevel@tonic-gate 	 * CPU, bring it back online.
15400Sstevel@tonic-gate 	 */
15410Sstevel@tonic-gate 	if (error && cyclic_off)
15420Sstevel@tonic-gate 		cyclic_online(cp);
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate 	/*
15458566SMadhavan.Venkataraman@Sun.COM 	 * If we failed, but managed to offline callouts on this CPU,
15468566SMadhavan.Venkataraman@Sun.COM 	 * bring it back online.
15478566SMadhavan.Venkataraman@Sun.COM 	 */
15488566SMadhavan.Venkataraman@Sun.COM 	if (error && callout_off)
15498566SMadhavan.Venkataraman@Sun.COM 		callout_cpu_online(cp);
15508566SMadhavan.Venkataraman@Sun.COM 
15518566SMadhavan.Venkataraman@Sun.COM 	/*
15523434Sesaxe 	 * If we failed, tell the PG subsystem that the CPU is back
15533434Sesaxe 	 */
15543434Sesaxe 	pg_cpupart_in(cp, pp);
15553434Sesaxe 
15563434Sesaxe 	/*
15570Sstevel@tonic-gate 	 * If we failed, we need to notify everyone that this CPU is back on.
15580Sstevel@tonic-gate 	 */
15590Sstevel@tonic-gate 	if (error != 0)
15600Sstevel@tonic-gate 		cpu_state_change_notify(cp->cpu_id, CPU_ON);
15610Sstevel@tonic-gate 
15620Sstevel@tonic-gate 	return (error);
15630Sstevel@tonic-gate }
15640Sstevel@tonic-gate 
15650Sstevel@tonic-gate /*
15660Sstevel@tonic-gate  * Mark the indicated CPU as faulted, taking it offline.
15670Sstevel@tonic-gate  */
15680Sstevel@tonic-gate int
15690Sstevel@tonic-gate cpu_faulted(cpu_t *cp, int flags)
15700Sstevel@tonic-gate {
15710Sstevel@tonic-gate 	int	error = 0;
15720Sstevel@tonic-gate 
15730Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
15740Sstevel@tonic-gate 	ASSERT(!cpu_is_poweredoff(cp));
15750Sstevel@tonic-gate 
15760Sstevel@tonic-gate 	if (cpu_is_offline(cp)) {
15770Sstevel@tonic-gate 		cp->cpu_flags &= ~CPU_SPARE;
15780Sstevel@tonic-gate 		cp->cpu_flags |= CPU_FAULTED;
15790Sstevel@tonic-gate 		mp_cpu_faulted_enter(cp);
15800Sstevel@tonic-gate 		cpu_set_state(cp);
15810Sstevel@tonic-gate 		return (0);
15820Sstevel@tonic-gate 	}
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate 	if ((error = cpu_offline(cp, flags)) == 0) {
15850Sstevel@tonic-gate 		cp->cpu_flags |= CPU_FAULTED;
15860Sstevel@tonic-gate 		mp_cpu_faulted_enter(cp);
15870Sstevel@tonic-gate 		cpu_set_state(cp);
15880Sstevel@tonic-gate 	}
15890Sstevel@tonic-gate 
15900Sstevel@tonic-gate 	return (error);
15910Sstevel@tonic-gate }
15920Sstevel@tonic-gate 
15930Sstevel@tonic-gate /*
15940Sstevel@tonic-gate  * Mark the indicated CPU as a spare, taking it offline.
15950Sstevel@tonic-gate  */
15960Sstevel@tonic-gate int
15970Sstevel@tonic-gate cpu_spare(cpu_t *cp, int flags)
15980Sstevel@tonic-gate {
15990Sstevel@tonic-gate 	int	error = 0;
16000Sstevel@tonic-gate 
16010Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
16020Sstevel@tonic-gate 	ASSERT(!cpu_is_poweredoff(cp));
16030Sstevel@tonic-gate 
16040Sstevel@tonic-gate 	if (cpu_is_offline(cp)) {
16050Sstevel@tonic-gate 		if (cp->cpu_flags & CPU_FAULTED) {
16060Sstevel@tonic-gate 			cp->cpu_flags &= ~CPU_FAULTED;
16070Sstevel@tonic-gate 			mp_cpu_faulted_exit(cp);
16080Sstevel@tonic-gate 		}
16090Sstevel@tonic-gate 		cp->cpu_flags |= CPU_SPARE;
16100Sstevel@tonic-gate 		cpu_set_state(cp);
16110Sstevel@tonic-gate 		return (0);
16120Sstevel@tonic-gate 	}
16130Sstevel@tonic-gate 
16140Sstevel@tonic-gate 	if ((error = cpu_offline(cp, flags)) == 0) {
16150Sstevel@tonic-gate 		cp->cpu_flags |= CPU_SPARE;
16160Sstevel@tonic-gate 		cpu_set_state(cp);
16170Sstevel@tonic-gate 	}
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 	return (error);
16200Sstevel@tonic-gate }
16210Sstevel@tonic-gate 
16220Sstevel@tonic-gate /*
16230Sstevel@tonic-gate  * Take the indicated CPU from poweroff to offline.
16240Sstevel@tonic-gate  */
16250Sstevel@tonic-gate int
16260Sstevel@tonic-gate cpu_poweron(cpu_t *cp)
16270Sstevel@tonic-gate {
16280Sstevel@tonic-gate 	int	error = ENOTSUP;
16290Sstevel@tonic-gate 
16300Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
16310Sstevel@tonic-gate 	ASSERT(cpu_is_poweredoff(cp));
16320Sstevel@tonic-gate 
16330Sstevel@tonic-gate 	error = mp_cpu_poweron(cp);	/* arch-dep hook */
16340Sstevel@tonic-gate 	if (error == 0)
16350Sstevel@tonic-gate 		cpu_set_state(cp);
16360Sstevel@tonic-gate 
16370Sstevel@tonic-gate 	return (error);
16380Sstevel@tonic-gate }
16390Sstevel@tonic-gate 
16400Sstevel@tonic-gate /*
16410Sstevel@tonic-gate  * Take the indicated CPU from any inactive state to powered off.
16420Sstevel@tonic-gate  */
16430Sstevel@tonic-gate int
16440Sstevel@tonic-gate cpu_poweroff(cpu_t *cp)
16450Sstevel@tonic-gate {
16460Sstevel@tonic-gate 	int	error = ENOTSUP;
16470Sstevel@tonic-gate 
16480Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
16490Sstevel@tonic-gate 	ASSERT(cpu_is_offline(cp));
16500Sstevel@tonic-gate 
16510Sstevel@tonic-gate 	if (!(cp->cpu_flags & CPU_QUIESCED))
16520Sstevel@tonic-gate 		return (EBUSY);		/* not completely idle */
16530Sstevel@tonic-gate 
16540Sstevel@tonic-gate 	error = mp_cpu_poweroff(cp);	/* arch-dep hook */
16550Sstevel@tonic-gate 	if (error == 0)
16560Sstevel@tonic-gate 		cpu_set_state(cp);
16570Sstevel@tonic-gate 
16580Sstevel@tonic-gate 	return (error);
16590Sstevel@tonic-gate }
16600Sstevel@tonic-gate 
16610Sstevel@tonic-gate /*
16628408SEric.Saxe@Sun.COM  * Initialize the Sequential CPU id lookup table
16638408SEric.Saxe@Sun.COM  */
16648408SEric.Saxe@Sun.COM void
16658408SEric.Saxe@Sun.COM cpu_seq_tbl_init()
16668408SEric.Saxe@Sun.COM {
16678408SEric.Saxe@Sun.COM 	cpu_t	**tbl;
16688408SEric.Saxe@Sun.COM 
16698408SEric.Saxe@Sun.COM 	tbl = kmem_zalloc(sizeof (struct cpu *) * max_ncpus, KM_SLEEP);
16708408SEric.Saxe@Sun.COM 	tbl[0] = CPU;
16718408SEric.Saxe@Sun.COM 
16728408SEric.Saxe@Sun.COM 	cpu_seq = tbl;
16738408SEric.Saxe@Sun.COM }
16748408SEric.Saxe@Sun.COM 
16758408SEric.Saxe@Sun.COM /*
16760Sstevel@tonic-gate  * Initialize the CPU lists for the first CPU.
16770Sstevel@tonic-gate  */
16780Sstevel@tonic-gate void
16790Sstevel@tonic-gate cpu_list_init(cpu_t *cp)
16800Sstevel@tonic-gate {
16810Sstevel@tonic-gate 	cp->cpu_next = cp;
16820Sstevel@tonic-gate 	cp->cpu_prev = cp;
16830Sstevel@tonic-gate 	cpu_list = cp;
16843792Sakolb 	clock_cpu_list = cp;
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate 	cp->cpu_next_onln = cp;
16870Sstevel@tonic-gate 	cp->cpu_prev_onln = cp;
16880Sstevel@tonic-gate 	cpu_active = cp;
16890Sstevel@tonic-gate 
16900Sstevel@tonic-gate 	cp->cpu_seqid = 0;
16910Sstevel@tonic-gate 	CPUSET_ADD(cpu_seqid_inuse, 0);
16928408SEric.Saxe@Sun.COM 
16938408SEric.Saxe@Sun.COM 	/*
16948408SEric.Saxe@Sun.COM 	 * Bootstrap cpu_seq using cpu_list
16958408SEric.Saxe@Sun.COM 	 * The cpu_seq[] table will be dynamically allocated
16968408SEric.Saxe@Sun.COM 	 * when kmem later becomes available (but before going MP)
16978408SEric.Saxe@Sun.COM 	 */
16988408SEric.Saxe@Sun.COM 	cpu_seq = &cpu_list;
16998408SEric.Saxe@Sun.COM 
17008887SMichael.Corcoran@Sun.COM 	cp->cpu_cache_offset = KMEM_CPU_CACHE_OFFSET(cp->cpu_seqid);
17010Sstevel@tonic-gate 	cp_default.cp_cpulist = cp;
17020Sstevel@tonic-gate 	cp_default.cp_ncpus = 1;
17030Sstevel@tonic-gate 	cp->cpu_next_part = cp;
17040Sstevel@tonic-gate 	cp->cpu_prev_part = cp;
17050Sstevel@tonic-gate 	cp->cpu_part = &cp_default;
17060Sstevel@tonic-gate 
17070Sstevel@tonic-gate 	CPUSET_ADD(cpu_available, cp->cpu_id);
17080Sstevel@tonic-gate }
17090Sstevel@tonic-gate 
17100Sstevel@tonic-gate /*
17110Sstevel@tonic-gate  * Insert a CPU into the list of available CPUs.
17120Sstevel@tonic-gate  */
17130Sstevel@tonic-gate void
17140Sstevel@tonic-gate cpu_add_unit(cpu_t *cp)
17150Sstevel@tonic-gate {
17160Sstevel@tonic-gate 	int seqid;
17170Sstevel@tonic-gate 
17180Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
17190Sstevel@tonic-gate 	ASSERT(cpu_list != NULL);	/* list started in cpu_list_init */
17200Sstevel@tonic-gate 
17210Sstevel@tonic-gate 	lgrp_config(LGRP_CONFIG_CPU_ADD, (uintptr_t)cp, 0);
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate 	/*
17240Sstevel@tonic-gate 	 * Note: most users of the cpu_list will grab the
17250Sstevel@tonic-gate 	 * cpu_lock to insure that it isn't modified.  However,
17260Sstevel@tonic-gate 	 * certain users can't or won't do that.  To allow this
17270Sstevel@tonic-gate 	 * we pause the other cpus.  Users who walk the list
17280Sstevel@tonic-gate 	 * without cpu_lock, must disable kernel preemption
17290Sstevel@tonic-gate 	 * to insure that the list isn't modified underneath
17300Sstevel@tonic-gate 	 * them.  Also, any cached pointers to cpu structures
17310Sstevel@tonic-gate 	 * must be revalidated by checking to see if the
17320Sstevel@tonic-gate 	 * cpu_next pointer points to itself.  This check must
17330Sstevel@tonic-gate 	 * be done with the cpu_lock held or kernel preemption
17340Sstevel@tonic-gate 	 * disabled.  This check relies upon the fact that
17350Sstevel@tonic-gate 	 * old cpu structures are not free'ed or cleared after
17360Sstevel@tonic-gate 	 * then are removed from the cpu_list.
17370Sstevel@tonic-gate 	 *
17380Sstevel@tonic-gate 	 * Note that the clock code walks the cpu list dereferencing
17390Sstevel@tonic-gate 	 * the cpu_part pointer, so we need to initialize it before
17400Sstevel@tonic-gate 	 * adding the cpu to the list.
17410Sstevel@tonic-gate 	 */
17420Sstevel@tonic-gate 	cp->cpu_part = &cp_default;
17430Sstevel@tonic-gate 	(void) pause_cpus(NULL);
17440Sstevel@tonic-gate 	cp->cpu_next = cpu_list;
17450Sstevel@tonic-gate 	cp->cpu_prev = cpu_list->cpu_prev;
17460Sstevel@tonic-gate 	cpu_list->cpu_prev->cpu_next = cp;
17470Sstevel@tonic-gate 	cpu_list->cpu_prev = cp;
17480Sstevel@tonic-gate 	start_cpus();
17490Sstevel@tonic-gate 
17500Sstevel@tonic-gate 	for (seqid = 0; CPU_IN_SET(cpu_seqid_inuse, seqid); seqid++)
17510Sstevel@tonic-gate 		continue;
17520Sstevel@tonic-gate 	CPUSET_ADD(cpu_seqid_inuse, seqid);
17530Sstevel@tonic-gate 	cp->cpu_seqid = seqid;
17540Sstevel@tonic-gate 	ASSERT(ncpus < max_ncpus);
17550Sstevel@tonic-gate 	ncpus++;
17568887SMichael.Corcoran@Sun.COM 	cp->cpu_cache_offset = KMEM_CPU_CACHE_OFFSET(cp->cpu_seqid);
17570Sstevel@tonic-gate 	cpu[cp->cpu_id] = cp;
17580Sstevel@tonic-gate 	CPUSET_ADD(cpu_available, cp->cpu_id);
17598408SEric.Saxe@Sun.COM 	cpu_seq[cp->cpu_seqid] = cp;
17600Sstevel@tonic-gate 
17610Sstevel@tonic-gate 	/*
17620Sstevel@tonic-gate 	 * allocate a pause thread for this CPU.
17630Sstevel@tonic-gate 	 */
17640Sstevel@tonic-gate 	cpu_pause_alloc(cp);
17650Sstevel@tonic-gate 
17660Sstevel@tonic-gate 	/*
17670Sstevel@tonic-gate 	 * So that new CPUs won't have NULL prev_onln and next_onln pointers,
17680Sstevel@tonic-gate 	 * link them into a list of just that CPU.
17690Sstevel@tonic-gate 	 * This is so that disp_lowpri_cpu will work for thread_create in
17700Sstevel@tonic-gate 	 * pause_cpus() when called from the startup thread in a new CPU.
17710Sstevel@tonic-gate 	 */
17720Sstevel@tonic-gate 	cp->cpu_next_onln = cp;
17730Sstevel@tonic-gate 	cp->cpu_prev_onln = cp;
17740Sstevel@tonic-gate 	cpu_info_kstat_create(cp);
17750Sstevel@tonic-gate 	cp->cpu_next_part = cp;
17760Sstevel@tonic-gate 	cp->cpu_prev_part = cp;
17770Sstevel@tonic-gate 
17780Sstevel@tonic-gate 	init_cpu_mstate(cp, CMS_SYSTEM);
17790Sstevel@tonic-gate 
17800Sstevel@tonic-gate 	pool_pset_mod = gethrtime();
17810Sstevel@tonic-gate }
17820Sstevel@tonic-gate 
17830Sstevel@tonic-gate /*
17840Sstevel@tonic-gate  * Do the opposite of cpu_add_unit().
17850Sstevel@tonic-gate  */
17860Sstevel@tonic-gate void
17870Sstevel@tonic-gate cpu_del_unit(int cpuid)
17880Sstevel@tonic-gate {
17890Sstevel@tonic-gate 	struct cpu	*cp, *cpnext;
17900Sstevel@tonic-gate 
17910Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
17920Sstevel@tonic-gate 	cp = cpu[cpuid];
17930Sstevel@tonic-gate 	ASSERT(cp != NULL);
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate 	ASSERT(cp->cpu_next_onln == cp);
17960Sstevel@tonic-gate 	ASSERT(cp->cpu_prev_onln == cp);
17970Sstevel@tonic-gate 	ASSERT(cp->cpu_next_part == cp);
17980Sstevel@tonic-gate 	ASSERT(cp->cpu_prev_part == cp);
17990Sstevel@tonic-gate 
18003434Sesaxe 	/*
18013434Sesaxe 	 * Tear down the CPU's physical ID cache, and update any
18023434Sesaxe 	 * processor groups
18033434Sesaxe 	 */
18043434Sesaxe 	pg_cpu_fini(cp);
18053434Sesaxe 	pghw_physid_destroy(cp);
18060Sstevel@tonic-gate 
18070Sstevel@tonic-gate 	/*
18080Sstevel@tonic-gate 	 * Destroy kstat stuff.
18090Sstevel@tonic-gate 	 */
18100Sstevel@tonic-gate 	cpu_info_kstat_destroy(cp);
18110Sstevel@tonic-gate 	term_cpu_mstate(cp);
18120Sstevel@tonic-gate 	/*
18130Sstevel@tonic-gate 	 * Free up pause thread.
18140Sstevel@tonic-gate 	 */
18150Sstevel@tonic-gate 	cpu_pause_free(cp);
18160Sstevel@tonic-gate 	CPUSET_DEL(cpu_available, cp->cpu_id);
18170Sstevel@tonic-gate 	cpu[cp->cpu_id] = NULL;
18188408SEric.Saxe@Sun.COM 	cpu_seq[cp->cpu_seqid] = NULL;
18198408SEric.Saxe@Sun.COM 
18200Sstevel@tonic-gate 	/*
18210Sstevel@tonic-gate 	 * The clock thread and mutex_vector_enter cannot hold the
18220Sstevel@tonic-gate 	 * cpu_lock while traversing the cpu list, therefore we pause
18230Sstevel@tonic-gate 	 * all other threads by pausing the other cpus. These, and any
18240Sstevel@tonic-gate 	 * other routines holding cpu pointers while possibly sleeping
18250Sstevel@tonic-gate 	 * must be sure to call kpreempt_disable before processing the
18260Sstevel@tonic-gate 	 * list and be sure to check that the cpu has not been deleted
18270Sstevel@tonic-gate 	 * after any sleeps (check cp->cpu_next != NULL). We guarantee
18280Sstevel@tonic-gate 	 * to keep the deleted cpu structure around.
18290Sstevel@tonic-gate 	 *
18300Sstevel@tonic-gate 	 * Note that this MUST be done AFTER cpu_available
18310Sstevel@tonic-gate 	 * has been updated so that we don't waste time
18320Sstevel@tonic-gate 	 * trying to pause the cpu we're trying to delete.
18330Sstevel@tonic-gate 	 */
18340Sstevel@tonic-gate 	(void) pause_cpus(NULL);
18350Sstevel@tonic-gate 
18360Sstevel@tonic-gate 	cpnext = cp->cpu_next;
18370Sstevel@tonic-gate 	cp->cpu_prev->cpu_next = cp->cpu_next;
18380Sstevel@tonic-gate 	cp->cpu_next->cpu_prev = cp->cpu_prev;
18390Sstevel@tonic-gate 	if (cp == cpu_list)
18403792Sakolb 		cpu_list = cpnext;
18410Sstevel@tonic-gate 
18420Sstevel@tonic-gate 	/*
18430Sstevel@tonic-gate 	 * Signals that the cpu has been deleted (see above).
18440Sstevel@tonic-gate 	 */
18450Sstevel@tonic-gate 	cp->cpu_next = NULL;
18460Sstevel@tonic-gate 	cp->cpu_prev = NULL;
18470Sstevel@tonic-gate 
18480Sstevel@tonic-gate 	start_cpus();
18490Sstevel@tonic-gate 
18500Sstevel@tonic-gate 	CPUSET_DEL(cpu_seqid_inuse, cp->cpu_seqid);
18510Sstevel@tonic-gate 	ncpus--;
18520Sstevel@tonic-gate 	lgrp_config(LGRP_CONFIG_CPU_DEL, (uintptr_t)cp, 0);
18530Sstevel@tonic-gate 
18540Sstevel@tonic-gate 	pool_pset_mod = gethrtime();
18550Sstevel@tonic-gate }
18560Sstevel@tonic-gate 
18570Sstevel@tonic-gate /*
18580Sstevel@tonic-gate  * Add a CPU to the list of active CPUs.
18590Sstevel@tonic-gate  *	This routine must not get any locks, because other CPUs are paused.
18600Sstevel@tonic-gate  */
18610Sstevel@tonic-gate static void
18620Sstevel@tonic-gate cpu_add_active_internal(cpu_t *cp)
18630Sstevel@tonic-gate {
18640Sstevel@tonic-gate 	cpupart_t	*pp = cp->cpu_part;
18650Sstevel@tonic-gate 
18660Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
18670Sstevel@tonic-gate 	ASSERT(cpu_list != NULL);	/* list started in cpu_list_init */
18680Sstevel@tonic-gate 
18690Sstevel@tonic-gate 	ncpus_online++;
18700Sstevel@tonic-gate 	cpu_set_state(cp);
18710Sstevel@tonic-gate 	cp->cpu_next_onln = cpu_active;
18720Sstevel@tonic-gate 	cp->cpu_prev_onln = cpu_active->cpu_prev_onln;
18730Sstevel@tonic-gate 	cpu_active->cpu_prev_onln->cpu_next_onln = cp;
18740Sstevel@tonic-gate 	cpu_active->cpu_prev_onln = cp;
18750Sstevel@tonic-gate 
18760Sstevel@tonic-gate 	if (pp->cp_cpulist) {
18770Sstevel@tonic-gate 		cp->cpu_next_part = pp->cp_cpulist;
18780Sstevel@tonic-gate 		cp->cpu_prev_part = pp->cp_cpulist->cpu_prev_part;
18790Sstevel@tonic-gate 		pp->cp_cpulist->cpu_prev_part->cpu_next_part = cp;
18800Sstevel@tonic-gate 		pp->cp_cpulist->cpu_prev_part = cp;
18810Sstevel@tonic-gate 	} else {
18820Sstevel@tonic-gate 		ASSERT(pp->cp_ncpus == 0);
18830Sstevel@tonic-gate 		pp->cp_cpulist = cp->cpu_next_part = cp->cpu_prev_part = cp;
18840Sstevel@tonic-gate 	}
18850Sstevel@tonic-gate 	pp->cp_ncpus++;
18860Sstevel@tonic-gate 	if (pp->cp_ncpus == 1) {
18870Sstevel@tonic-gate 		cp_numparts_nonempty++;
18880Sstevel@tonic-gate 		ASSERT(cp_numparts_nonempty != 0);
18890Sstevel@tonic-gate 	}
18900Sstevel@tonic-gate 
18913434Sesaxe 	pg_cpu_active(cp);
18920Sstevel@tonic-gate 	lgrp_config(LGRP_CONFIG_CPU_ONLINE, (uintptr_t)cp, 0);
18930Sstevel@tonic-gate 
18940Sstevel@tonic-gate 	bzero(&cp->cpu_loadavg, sizeof (cp->cpu_loadavg));
18950Sstevel@tonic-gate }
18960Sstevel@tonic-gate 
18970Sstevel@tonic-gate /*
18980Sstevel@tonic-gate  * Add a CPU to the list of active CPUs.
18990Sstevel@tonic-gate  *	This is called from machine-dependent layers when a new CPU is started.
19000Sstevel@tonic-gate  */
19010Sstevel@tonic-gate void
19020Sstevel@tonic-gate cpu_add_active(cpu_t *cp)
19030Sstevel@tonic-gate {
19043434Sesaxe 	pg_cpupart_in(cp, cp->cpu_part);
19053434Sesaxe 
19060Sstevel@tonic-gate 	pause_cpus(NULL);
19070Sstevel@tonic-gate 	cpu_add_active_internal(cp);
19080Sstevel@tonic-gate 	start_cpus();
19093434Sesaxe 
19100Sstevel@tonic-gate 	cpu_stats_kstat_create(cp);
19110Sstevel@tonic-gate 	cpu_create_intrstat(cp);
19120Sstevel@tonic-gate 	lgrp_kstat_create(cp);
19130Sstevel@tonic-gate 	cpu_state_change_notify(cp->cpu_id, CPU_INIT);
19140Sstevel@tonic-gate }
19150Sstevel@tonic-gate 
19160Sstevel@tonic-gate 
19170Sstevel@tonic-gate /*
19180Sstevel@tonic-gate  * Remove a CPU from the list of active CPUs.
19190Sstevel@tonic-gate  *	This routine must not get any locks, because other CPUs are paused.
19200Sstevel@tonic-gate  */
19210Sstevel@tonic-gate /* ARGSUSED */
19220Sstevel@tonic-gate static void
19230Sstevel@tonic-gate cpu_remove_active(cpu_t *cp)
19240Sstevel@tonic-gate {
19250Sstevel@tonic-gate 	cpupart_t	*pp = cp->cpu_part;
19260Sstevel@tonic-gate 
19270Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
19280Sstevel@tonic-gate 	ASSERT(cp->cpu_next_onln != cp);	/* not the last one */
19290Sstevel@tonic-gate 	ASSERT(cp->cpu_prev_onln != cp);	/* not the last one */
19300Sstevel@tonic-gate 
19313434Sesaxe 	pg_cpu_inactive(cp);
19320Sstevel@tonic-gate 
19330Sstevel@tonic-gate 	lgrp_config(LGRP_CONFIG_CPU_OFFLINE, (uintptr_t)cp, 0);
19340Sstevel@tonic-gate 
19355788Smv143129 	if (cp == clock_cpu_list)
19365788Smv143129 		clock_cpu_list = cp->cpu_next_onln;
19375788Smv143129 
19380Sstevel@tonic-gate 	cp->cpu_prev_onln->cpu_next_onln = cp->cpu_next_onln;
19390Sstevel@tonic-gate 	cp->cpu_next_onln->cpu_prev_onln = cp->cpu_prev_onln;
19400Sstevel@tonic-gate 	if (cpu_active == cp) {
19410Sstevel@tonic-gate 		cpu_active = cp->cpu_next_onln;
19420Sstevel@tonic-gate 	}
19430Sstevel@tonic-gate 	cp->cpu_next_onln = cp;
19440Sstevel@tonic-gate 	cp->cpu_prev_onln = cp;
19450Sstevel@tonic-gate 
19460Sstevel@tonic-gate 	cp->cpu_prev_part->cpu_next_part = cp->cpu_next_part;
19470Sstevel@tonic-gate 	cp->cpu_next_part->cpu_prev_part = cp->cpu_prev_part;
19480Sstevel@tonic-gate 	if (pp->cp_cpulist == cp) {
19490Sstevel@tonic-gate 		pp->cp_cpulist = cp->cpu_next_part;
19500Sstevel@tonic-gate 		ASSERT(pp->cp_cpulist != cp);
19510Sstevel@tonic-gate 	}
19520Sstevel@tonic-gate 	cp->cpu_next_part = cp;
19530Sstevel@tonic-gate 	cp->cpu_prev_part = cp;
19540Sstevel@tonic-gate 	pp->cp_ncpus--;
19550Sstevel@tonic-gate 	if (pp->cp_ncpus == 0) {
19560Sstevel@tonic-gate 		cp_numparts_nonempty--;
19570Sstevel@tonic-gate 		ASSERT(cp_numparts_nonempty != 0);
19580Sstevel@tonic-gate 	}
19590Sstevel@tonic-gate }
19600Sstevel@tonic-gate 
19610Sstevel@tonic-gate /*
19620Sstevel@tonic-gate  * Routine used to setup a newly inserted CPU in preparation for starting
19630Sstevel@tonic-gate  * it running code.
19640Sstevel@tonic-gate  */
19650Sstevel@tonic-gate int
19660Sstevel@tonic-gate cpu_configure(int cpuid)
19670Sstevel@tonic-gate {
19680Sstevel@tonic-gate 	int retval = 0;
19690Sstevel@tonic-gate 
19700Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
19710Sstevel@tonic-gate 
19720Sstevel@tonic-gate 	/*
19730Sstevel@tonic-gate 	 * Some structures are statically allocated based upon
19740Sstevel@tonic-gate 	 * the maximum number of cpus the system supports.  Do not
19750Sstevel@tonic-gate 	 * try to add anything beyond this limit.
19760Sstevel@tonic-gate 	 */
19770Sstevel@tonic-gate 	if (cpuid < 0 || cpuid >= NCPU) {
19780Sstevel@tonic-gate 		return (EINVAL);
19790Sstevel@tonic-gate 	}
19800Sstevel@tonic-gate 
19810Sstevel@tonic-gate 	if ((cpu[cpuid] != NULL) && (cpu[cpuid]->cpu_flags != 0)) {
19820Sstevel@tonic-gate 		return (EALREADY);
19830Sstevel@tonic-gate 	}
19840Sstevel@tonic-gate 
19850Sstevel@tonic-gate 	if ((retval = mp_cpu_configure(cpuid)) != 0) {
19860Sstevel@tonic-gate 		return (retval);
19870Sstevel@tonic-gate 	}
19880Sstevel@tonic-gate 
19890Sstevel@tonic-gate 	cpu[cpuid]->cpu_flags = CPU_QUIESCED | CPU_OFFLINE | CPU_POWEROFF;
19900Sstevel@tonic-gate 	cpu_set_state(cpu[cpuid]);
19910Sstevel@tonic-gate 	retval = cpu_state_change_hooks(cpuid, CPU_CONFIG, CPU_UNCONFIG);
19920Sstevel@tonic-gate 	if (retval != 0)
19930Sstevel@tonic-gate 		(void) mp_cpu_unconfigure(cpuid);
19940Sstevel@tonic-gate 
19950Sstevel@tonic-gate 	return (retval);
19960Sstevel@tonic-gate }
19970Sstevel@tonic-gate 
19980Sstevel@tonic-gate /*
19990Sstevel@tonic-gate  * Routine used to cleanup a CPU that has been powered off.  This will
20000Sstevel@tonic-gate  * destroy all per-cpu information related to this cpu.
20010Sstevel@tonic-gate  */
20020Sstevel@tonic-gate int
20030Sstevel@tonic-gate cpu_unconfigure(int cpuid)
20040Sstevel@tonic-gate {
20050Sstevel@tonic-gate 	int error;
20060Sstevel@tonic-gate 
20070Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
20080Sstevel@tonic-gate 
20090Sstevel@tonic-gate 	if (cpu[cpuid] == NULL) {
20100Sstevel@tonic-gate 		return (ENODEV);
20110Sstevel@tonic-gate 	}
20120Sstevel@tonic-gate 
20130Sstevel@tonic-gate 	if (cpu[cpuid]->cpu_flags == 0) {
20140Sstevel@tonic-gate 		return (EALREADY);
20150Sstevel@tonic-gate 	}
20160Sstevel@tonic-gate 
20170Sstevel@tonic-gate 	if ((cpu[cpuid]->cpu_flags & CPU_POWEROFF) == 0) {
20180Sstevel@tonic-gate 		return (EBUSY);
20190Sstevel@tonic-gate 	}
20200Sstevel@tonic-gate 
20210Sstevel@tonic-gate 	if (cpu[cpuid]->cpu_props != NULL) {
20220Sstevel@tonic-gate 		(void) nvlist_free(cpu[cpuid]->cpu_props);
20230Sstevel@tonic-gate 		cpu[cpuid]->cpu_props = NULL;
20240Sstevel@tonic-gate 	}
20250Sstevel@tonic-gate 
20260Sstevel@tonic-gate 	error = cpu_state_change_hooks(cpuid, CPU_UNCONFIG, CPU_CONFIG);
20270Sstevel@tonic-gate 
20280Sstevel@tonic-gate 	if (error != 0)
20290Sstevel@tonic-gate 		return (error);
20300Sstevel@tonic-gate 
20310Sstevel@tonic-gate 	return (mp_cpu_unconfigure(cpuid));
20320Sstevel@tonic-gate }
20330Sstevel@tonic-gate 
20340Sstevel@tonic-gate /*
20350Sstevel@tonic-gate  * Routines for registering and de-registering cpu_setup callback functions.
20360Sstevel@tonic-gate  *
20370Sstevel@tonic-gate  * Caller's context
20380Sstevel@tonic-gate  *	These routines must not be called from a driver's attach(9E) or
20390Sstevel@tonic-gate  *	detach(9E) entry point.
20400Sstevel@tonic-gate  *
20410Sstevel@tonic-gate  * NOTE: CPU callbacks should not block. They are called with cpu_lock held.
20420Sstevel@tonic-gate  */
20430Sstevel@tonic-gate 
20440Sstevel@tonic-gate /*
20450Sstevel@tonic-gate  * Ideally, these would be dynamically allocated and put into a linked
20460Sstevel@tonic-gate  * list; however that is not feasible because the registration routine
20470Sstevel@tonic-gate  * has to be available before the kmem allocator is working (in fact,
20480Sstevel@tonic-gate  * it is called by the kmem allocator init code).  In any case, there
20490Sstevel@tonic-gate  * are quite a few extra entries for future users.
20500Sstevel@tonic-gate  */
20511020Sjkennedy #define	NCPU_SETUPS	20
20520Sstevel@tonic-gate 
20530Sstevel@tonic-gate struct cpu_setup {
20540Sstevel@tonic-gate 	cpu_setup_func_t *func;
20550Sstevel@tonic-gate 	void *arg;
20560Sstevel@tonic-gate } cpu_setups[NCPU_SETUPS];
20570Sstevel@tonic-gate 
20580Sstevel@tonic-gate void
20590Sstevel@tonic-gate register_cpu_setup_func(cpu_setup_func_t *func, void *arg)
20600Sstevel@tonic-gate {
20610Sstevel@tonic-gate 	int i;
20620Sstevel@tonic-gate 
20630Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
20640Sstevel@tonic-gate 
20650Sstevel@tonic-gate 	for (i = 0; i < NCPU_SETUPS; i++)
20660Sstevel@tonic-gate 		if (cpu_setups[i].func == NULL)
20674718Smh27603 			break;
20680Sstevel@tonic-gate 	if (i >= NCPU_SETUPS)
20690Sstevel@tonic-gate 		cmn_err(CE_PANIC, "Ran out of cpu_setup callback entries");
20700Sstevel@tonic-gate 
20710Sstevel@tonic-gate 	cpu_setups[i].func = func;
20720Sstevel@tonic-gate 	cpu_setups[i].arg = arg;
20730Sstevel@tonic-gate }
20740Sstevel@tonic-gate 
20750Sstevel@tonic-gate void
20760Sstevel@tonic-gate unregister_cpu_setup_func(cpu_setup_func_t *func, void *arg)
20770Sstevel@tonic-gate {
20780Sstevel@tonic-gate 	int i;
20790Sstevel@tonic-gate 
20800Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
20810Sstevel@tonic-gate 
20820Sstevel@tonic-gate 	for (i = 0; i < NCPU_SETUPS; i++)
20830Sstevel@tonic-gate 		if ((cpu_setups[i].func == func) &&
20840Sstevel@tonic-gate 		    (cpu_setups[i].arg == arg))
20854718Smh27603 			break;
20860Sstevel@tonic-gate 	if (i >= NCPU_SETUPS)
20870Sstevel@tonic-gate 		cmn_err(CE_PANIC, "Could not find cpu_setup callback to "
20880Sstevel@tonic-gate 		    "deregister");
20890Sstevel@tonic-gate 
20900Sstevel@tonic-gate 	cpu_setups[i].func = NULL;
20910Sstevel@tonic-gate 	cpu_setups[i].arg = 0;
20920Sstevel@tonic-gate }
20930Sstevel@tonic-gate 
20940Sstevel@tonic-gate /*
20950Sstevel@tonic-gate  * Call any state change hooks for this CPU, ignore any errors.
20960Sstevel@tonic-gate  */
20970Sstevel@tonic-gate void
20980Sstevel@tonic-gate cpu_state_change_notify(int id, cpu_setup_t what)
20990Sstevel@tonic-gate {
21000Sstevel@tonic-gate 	int i;
21010Sstevel@tonic-gate 
21020Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
21030Sstevel@tonic-gate 
21040Sstevel@tonic-gate 	for (i = 0; i < NCPU_SETUPS; i++) {
21050Sstevel@tonic-gate 		if (cpu_setups[i].func != NULL) {
21060Sstevel@tonic-gate 			cpu_setups[i].func(what, id, cpu_setups[i].arg);
21070Sstevel@tonic-gate 		}
21080Sstevel@tonic-gate 	}
21090Sstevel@tonic-gate }
21100Sstevel@tonic-gate 
21110Sstevel@tonic-gate /*
21120Sstevel@tonic-gate  * Call any state change hooks for this CPU, undo it if error found.
21130Sstevel@tonic-gate  */
21140Sstevel@tonic-gate static int
21150Sstevel@tonic-gate cpu_state_change_hooks(int id, cpu_setup_t what, cpu_setup_t undo)
21160Sstevel@tonic-gate {
21170Sstevel@tonic-gate 	int i;
21180Sstevel@tonic-gate 	int retval = 0;
21190Sstevel@tonic-gate 
21200Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
21210Sstevel@tonic-gate 
21220Sstevel@tonic-gate 	for (i = 0; i < NCPU_SETUPS; i++) {
21230Sstevel@tonic-gate 		if (cpu_setups[i].func != NULL) {
21240Sstevel@tonic-gate 			retval = cpu_setups[i].func(what, id,
21250Sstevel@tonic-gate 			    cpu_setups[i].arg);
21260Sstevel@tonic-gate 			if (retval) {
21270Sstevel@tonic-gate 				for (i--; i >= 0; i--) {
21280Sstevel@tonic-gate 					if (cpu_setups[i].func != NULL)
21290Sstevel@tonic-gate 						cpu_setups[i].func(undo,
21300Sstevel@tonic-gate 						    id, cpu_setups[i].arg);
21310Sstevel@tonic-gate 				}
21320Sstevel@tonic-gate 				break;
21330Sstevel@tonic-gate 			}
21340Sstevel@tonic-gate 		}
21350Sstevel@tonic-gate 	}
21360Sstevel@tonic-gate 	return (retval);
21370Sstevel@tonic-gate }
21380Sstevel@tonic-gate 
21390Sstevel@tonic-gate /*
21400Sstevel@tonic-gate  * Export information about this CPU via the kstat mechanism.
21410Sstevel@tonic-gate  */
21420Sstevel@tonic-gate static struct {
21430Sstevel@tonic-gate 	kstat_named_t ci_state;
21440Sstevel@tonic-gate 	kstat_named_t ci_state_begin;
21450Sstevel@tonic-gate 	kstat_named_t ci_cpu_type;
21460Sstevel@tonic-gate 	kstat_named_t ci_fpu_type;
21470Sstevel@tonic-gate 	kstat_named_t ci_clock_MHz;
21480Sstevel@tonic-gate 	kstat_named_t ci_chip_id;
21490Sstevel@tonic-gate 	kstat_named_t ci_implementation;
21501414Scindi 	kstat_named_t ci_brandstr;
21511414Scindi 	kstat_named_t ci_core_id;
21524667Smh27603 	kstat_named_t ci_curr_clock_Hz;
21534667Smh27603 	kstat_named_t ci_supp_freq_Hz;
21541414Scindi #if defined(__sparcv9)
21550Sstevel@tonic-gate 	kstat_named_t ci_device_ID;
21560Sstevel@tonic-gate 	kstat_named_t ci_cpu_fru;
21570Sstevel@tonic-gate #endif
21583446Smrj #if defined(__x86)
21591414Scindi 	kstat_named_t ci_vendorstr;
21601414Scindi 	kstat_named_t ci_family;
21611414Scindi 	kstat_named_t ci_model;
21621414Scindi 	kstat_named_t ci_step;
21631414Scindi 	kstat_named_t ci_clogid;
21645870Sgavinm 	kstat_named_t ci_pkg_core_id;
21655254Sgavinm 	kstat_named_t ci_ncpuperchip;
21665254Sgavinm 	kstat_named_t ci_ncoreperchip;
21678906SEric.Saxe@Sun.COM 	kstat_named_t ci_max_cstates;
21688906SEric.Saxe@Sun.COM 	kstat_named_t ci_curr_cstate;
2169*9482SKuriakose.Kuruvilla@Sun.COM 	kstat_named_t ci_sktstr;
21701414Scindi #endif
21710Sstevel@tonic-gate } cpu_info_template = {
21724667Smh27603 	{ "state",			KSTAT_DATA_CHAR },
21734667Smh27603 	{ "state_begin",		KSTAT_DATA_LONG },
21744667Smh27603 	{ "cpu_type",			KSTAT_DATA_CHAR },
21754667Smh27603 	{ "fpu_type",			KSTAT_DATA_CHAR },
21764667Smh27603 	{ "clock_MHz",			KSTAT_DATA_LONG },
21774667Smh27603 	{ "chip_id",			KSTAT_DATA_LONG },
21784667Smh27603 	{ "implementation",		KSTAT_DATA_STRING },
21794667Smh27603 	{ "brand",			KSTAT_DATA_STRING },
21804667Smh27603 	{ "core_id",			KSTAT_DATA_LONG },
21814667Smh27603 	{ "current_clock_Hz",		KSTAT_DATA_UINT64 },
21824667Smh27603 	{ "supported_frequencies_Hz",	KSTAT_DATA_STRING },
21831414Scindi #if defined(__sparcv9)
21844667Smh27603 	{ "device_ID",			KSTAT_DATA_UINT64 },
21854667Smh27603 	{ "cpu_fru",			KSTAT_DATA_STRING },
21860Sstevel@tonic-gate #endif
21873446Smrj #if defined(__x86)
21884667Smh27603 	{ "vendor_id",			KSTAT_DATA_STRING },
21894667Smh27603 	{ "family",			KSTAT_DATA_INT32 },
21904667Smh27603 	{ "model",			KSTAT_DATA_INT32 },
21914667Smh27603 	{ "stepping",			KSTAT_DATA_INT32 },
21924667Smh27603 	{ "clog_id",			KSTAT_DATA_INT32 },
21935870Sgavinm 	{ "pkg_core_id",		KSTAT_DATA_LONG },
21945254Sgavinm 	{ "ncpu_per_chip",		KSTAT_DATA_INT32 },
21955870Sgavinm 	{ "ncore_per_chip",		KSTAT_DATA_INT32 },
21968906SEric.Saxe@Sun.COM 	{ "supported_max_cstates",	KSTAT_DATA_INT32 },
21978906SEric.Saxe@Sun.COM 	{ "current_cstate",		KSTAT_DATA_INT32 },
2198*9482SKuriakose.Kuruvilla@Sun.COM 	{ "socket_type",		KSTAT_DATA_STRING },
21991414Scindi #endif
22000Sstevel@tonic-gate };
22010Sstevel@tonic-gate 
22020Sstevel@tonic-gate static kmutex_t cpu_info_template_lock;
22030Sstevel@tonic-gate 
22040Sstevel@tonic-gate static int
22050Sstevel@tonic-gate cpu_info_kstat_update(kstat_t *ksp, int rw)
22060Sstevel@tonic-gate {
22070Sstevel@tonic-gate 	cpu_t	*cp = ksp->ks_private;
22080Sstevel@tonic-gate 	const char *pi_state;
22090Sstevel@tonic-gate 
22100Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
22110Sstevel@tonic-gate 		return (EACCES);
22120Sstevel@tonic-gate 
22130Sstevel@tonic-gate 	switch (cp->cpu_type_info.pi_state) {
22140Sstevel@tonic-gate 	case P_ONLINE:
22150Sstevel@tonic-gate 		pi_state = PS_ONLINE;
22160Sstevel@tonic-gate 		break;
22170Sstevel@tonic-gate 	case P_POWEROFF:
22180Sstevel@tonic-gate 		pi_state = PS_POWEROFF;
22190Sstevel@tonic-gate 		break;
22200Sstevel@tonic-gate 	case P_NOINTR:
22210Sstevel@tonic-gate 		pi_state = PS_NOINTR;
22220Sstevel@tonic-gate 		break;
22230Sstevel@tonic-gate 	case P_FAULTED:
22240Sstevel@tonic-gate 		pi_state = PS_FAULTED;
22250Sstevel@tonic-gate 		break;
22260Sstevel@tonic-gate 	case P_SPARE:
22270Sstevel@tonic-gate 		pi_state = PS_SPARE;
22280Sstevel@tonic-gate 		break;
22290Sstevel@tonic-gate 	case P_OFFLINE:
22300Sstevel@tonic-gate 		pi_state = PS_OFFLINE;
22310Sstevel@tonic-gate 		break;
22320Sstevel@tonic-gate 	default:
22330Sstevel@tonic-gate 		pi_state = "unknown";
22340Sstevel@tonic-gate 	}
22350Sstevel@tonic-gate 	(void) strcpy(cpu_info_template.ci_state.value.c, pi_state);
22360Sstevel@tonic-gate 	cpu_info_template.ci_state_begin.value.l = cp->cpu_state_begin;
22370Sstevel@tonic-gate 	(void) strncpy(cpu_info_template.ci_cpu_type.value.c,
22380Sstevel@tonic-gate 	    cp->cpu_type_info.pi_processor_type, 15);
22390Sstevel@tonic-gate 	(void) strncpy(cpu_info_template.ci_fpu_type.value.c,
22400Sstevel@tonic-gate 	    cp->cpu_type_info.pi_fputypes, 15);
22410Sstevel@tonic-gate 	cpu_info_template.ci_clock_MHz.value.l = cp->cpu_type_info.pi_clock;
22423434Sesaxe 	cpu_info_template.ci_chip_id.value.l =
22433434Sesaxe 	    pg_plat_hw_instance_id(cp, PGHW_CHIP);
22440Sstevel@tonic-gate 	kstat_named_setstr(&cpu_info_template.ci_implementation,
22450Sstevel@tonic-gate 	    cp->cpu_idstr);
22461414Scindi 	kstat_named_setstr(&cpu_info_template.ci_brandstr, cp->cpu_brandstr);
22473434Sesaxe 	cpu_info_template.ci_core_id.value.l = pg_plat_get_core_id(cp);
22484667Smh27603 	cpu_info_template.ci_curr_clock_Hz.value.ui64 =
22494718Smh27603 	    cp->cpu_curr_clock;
22504877Smh27603 	kstat_named_setstr(&cpu_info_template.ci_supp_freq_Hz,
22514877Smh27603 	    cp->cpu_supp_freqs);
22521414Scindi #if defined(__sparcv9)
22530Sstevel@tonic-gate 	cpu_info_template.ci_device_ID.value.ui64 =
22540Sstevel@tonic-gate 	    cpunodes[cp->cpu_id].device_id;
22550Sstevel@tonic-gate 	kstat_named_setstr(&cpu_info_template.ci_cpu_fru, cpu_fru_fmri(cp));
22560Sstevel@tonic-gate #endif
22573446Smrj #if defined(__x86)
22581414Scindi 	kstat_named_setstr(&cpu_info_template.ci_vendorstr,
22591414Scindi 	    cpuid_getvendorstr(cp));
22601414Scindi 	cpu_info_template.ci_family.value.l = cpuid_getfamily(cp);
22611414Scindi 	cpu_info_template.ci_model.value.l = cpuid_getmodel(cp);
22621414Scindi 	cpu_info_template.ci_step.value.l = cpuid_getstep(cp);
22633434Sesaxe 	cpu_info_template.ci_clogid.value.l = cpuid_get_clogid(cp);
22645254Sgavinm 	cpu_info_template.ci_ncpuperchip.value.l = cpuid_get_ncpu_per_chip(cp);
22655254Sgavinm 	cpu_info_template.ci_ncoreperchip.value.l =
22665254Sgavinm 	    cpuid_get_ncore_per_chip(cp);
22675870Sgavinm 	cpu_info_template.ci_pkg_core_id.value.l = cpuid_get_pkgcoreid(cp);
22688906SEric.Saxe@Sun.COM 	cpu_info_template.ci_max_cstates.value.l = cp->cpu_m.max_cstates;
22698906SEric.Saxe@Sun.COM 	cpu_info_template.ci_curr_cstate.value.l = cp->cpu_m.curr_cstate;
2270*9482SKuriakose.Kuruvilla@Sun.COM 	kstat_named_setstr(&cpu_info_template.ci_sktstr,
2271*9482SKuriakose.Kuruvilla@Sun.COM 	    cpuid_getsocketstr(cp));
22721414Scindi #endif
22731414Scindi 
22740Sstevel@tonic-gate 	return (0);
22750Sstevel@tonic-gate }
22760Sstevel@tonic-gate 
22770Sstevel@tonic-gate static void
22780Sstevel@tonic-gate cpu_info_kstat_create(cpu_t *cp)
22790Sstevel@tonic-gate {
22800Sstevel@tonic-gate 	zoneid_t zoneid;
22810Sstevel@tonic-gate 
22820Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
22830Sstevel@tonic-gate 
22840Sstevel@tonic-gate 	if (pool_pset_enabled())
22850Sstevel@tonic-gate 		zoneid = GLOBAL_ZONEID;
22860Sstevel@tonic-gate 	else
22870Sstevel@tonic-gate 		zoneid = ALL_ZONES;
22880Sstevel@tonic-gate 	if ((cp->cpu_info_kstat = kstat_create_zone("cpu_info", cp->cpu_id,
22890Sstevel@tonic-gate 	    NULL, "misc", KSTAT_TYPE_NAMED,
22904718Smh27603 	    sizeof (cpu_info_template) / sizeof (kstat_named_t),
22914718Smh27603 	    KSTAT_FLAG_VIRTUAL, zoneid)) != NULL) {
22920Sstevel@tonic-gate 		cp->cpu_info_kstat->ks_data_size += 2 * CPU_IDSTRLEN;
22931414Scindi #if defined(__sparcv9)
22940Sstevel@tonic-gate 		cp->cpu_info_kstat->ks_data_size +=
22950Sstevel@tonic-gate 		    strlen(cpu_fru_fmri(cp)) + 1;
22960Sstevel@tonic-gate #endif
22973446Smrj #if defined(__x86)
22981414Scindi 		cp->cpu_info_kstat->ks_data_size += X86_VENDOR_STRLEN;
22991414Scindi #endif
23005286Smh27603 		if (cp->cpu_supp_freqs != NULL)
23015286Smh27603 			cp->cpu_info_kstat->ks_data_size +=
23025286Smh27603 			    strlen(cp->cpu_supp_freqs) + 1;
23030Sstevel@tonic-gate 		cp->cpu_info_kstat->ks_lock = &cpu_info_template_lock;
23040Sstevel@tonic-gate 		cp->cpu_info_kstat->ks_data = &cpu_info_template;
23050Sstevel@tonic-gate 		cp->cpu_info_kstat->ks_private = cp;
23060Sstevel@tonic-gate 		cp->cpu_info_kstat->ks_update = cpu_info_kstat_update;
23070Sstevel@tonic-gate 		kstat_install(cp->cpu_info_kstat);
23080Sstevel@tonic-gate 	}
23090Sstevel@tonic-gate }
23100Sstevel@tonic-gate 
23110Sstevel@tonic-gate static void
23120Sstevel@tonic-gate cpu_info_kstat_destroy(cpu_t *cp)
23130Sstevel@tonic-gate {
23140Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
23150Sstevel@tonic-gate 
23160Sstevel@tonic-gate 	kstat_delete(cp->cpu_info_kstat);
23170Sstevel@tonic-gate 	cp->cpu_info_kstat = NULL;
23180Sstevel@tonic-gate }
23190Sstevel@tonic-gate 
23200Sstevel@tonic-gate /*
23210Sstevel@tonic-gate  * Create and install kstats for the boot CPU.
23220Sstevel@tonic-gate  */
23230Sstevel@tonic-gate void
23240Sstevel@tonic-gate cpu_kstat_init(cpu_t *cp)
23250Sstevel@tonic-gate {
23260Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
23270Sstevel@tonic-gate 	cpu_info_kstat_create(cp);
23280Sstevel@tonic-gate 	cpu_stats_kstat_create(cp);
23290Sstevel@tonic-gate 	cpu_create_intrstat(cp);
23300Sstevel@tonic-gate 	cpu_set_state(cp);
23310Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
23320Sstevel@tonic-gate }
23330Sstevel@tonic-gate 
23340Sstevel@tonic-gate /*
23350Sstevel@tonic-gate  * Make visible to the zone that subset of the cpu information that would be
23360Sstevel@tonic-gate  * initialized when a cpu is configured (but still offline).
23370Sstevel@tonic-gate  */
23380Sstevel@tonic-gate void
23390Sstevel@tonic-gate cpu_visibility_configure(cpu_t *cp, zone_t *zone)
23400Sstevel@tonic-gate {
23410Sstevel@tonic-gate 	zoneid_t zoneid = zone ? zone->zone_id : ALL_ZONES;
23420Sstevel@tonic-gate 
23430Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
23440Sstevel@tonic-gate 	ASSERT(pool_pset_enabled());
23450Sstevel@tonic-gate 	ASSERT(cp != NULL);
23460Sstevel@tonic-gate 
23470Sstevel@tonic-gate 	if (zoneid != ALL_ZONES && zoneid != GLOBAL_ZONEID) {
23480Sstevel@tonic-gate 		zone->zone_ncpus++;
23490Sstevel@tonic-gate 		ASSERT(zone->zone_ncpus <= ncpus);
23500Sstevel@tonic-gate 	}
23510Sstevel@tonic-gate 	if (cp->cpu_info_kstat != NULL)
23520Sstevel@tonic-gate 		kstat_zone_add(cp->cpu_info_kstat, zoneid);
23530Sstevel@tonic-gate }
23540Sstevel@tonic-gate 
23550Sstevel@tonic-gate /*
23560Sstevel@tonic-gate  * Make visible to the zone that subset of the cpu information that would be
23570Sstevel@tonic-gate  * initialized when a previously configured cpu is onlined.
23580Sstevel@tonic-gate  */
23590Sstevel@tonic-gate void
23600Sstevel@tonic-gate cpu_visibility_online(cpu_t *cp, zone_t *zone)
23610Sstevel@tonic-gate {
23620Sstevel@tonic-gate 	kstat_t *ksp;
23630Sstevel@tonic-gate 	char name[sizeof ("cpu_stat") + 10];	/* enough for 32-bit cpuids */
23640Sstevel@tonic-gate 	zoneid_t zoneid = zone ? zone->zone_id : ALL_ZONES;
23650Sstevel@tonic-gate 	processorid_t cpun;
23660Sstevel@tonic-gate 
23670Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
23680Sstevel@tonic-gate 	ASSERT(pool_pset_enabled());
23690Sstevel@tonic-gate 	ASSERT(cp != NULL);
23700Sstevel@tonic-gate 	ASSERT(cpu_is_active(cp));
23710Sstevel@tonic-gate 
23720Sstevel@tonic-gate 	cpun = cp->cpu_id;
23730Sstevel@tonic-gate 	if (zoneid != ALL_ZONES && zoneid != GLOBAL_ZONEID) {
23740Sstevel@tonic-gate 		zone->zone_ncpus_online++;
23750Sstevel@tonic-gate 		ASSERT(zone->zone_ncpus_online <= ncpus_online);
23760Sstevel@tonic-gate 	}
23770Sstevel@tonic-gate 	(void) snprintf(name, sizeof (name), "cpu_stat%d", cpun);
23780Sstevel@tonic-gate 	if ((ksp = kstat_hold_byname("cpu_stat", cpun, name, ALL_ZONES))
23790Sstevel@tonic-gate 	    != NULL) {
23800Sstevel@tonic-gate 		kstat_zone_add(ksp, zoneid);
23810Sstevel@tonic-gate 		kstat_rele(ksp);
23820Sstevel@tonic-gate 	}
23830Sstevel@tonic-gate 	if ((ksp = kstat_hold_byname("cpu", cpun, "sys", ALL_ZONES)) != NULL) {
23840Sstevel@tonic-gate 		kstat_zone_add(ksp, zoneid);
23850Sstevel@tonic-gate 		kstat_rele(ksp);
23860Sstevel@tonic-gate 	}
23870Sstevel@tonic-gate 	if ((ksp = kstat_hold_byname("cpu", cpun, "vm", ALL_ZONES)) != NULL) {
23880Sstevel@tonic-gate 		kstat_zone_add(ksp, zoneid);
23890Sstevel@tonic-gate 		kstat_rele(ksp);
23900Sstevel@tonic-gate 	}
23910Sstevel@tonic-gate 	if ((ksp = kstat_hold_byname("cpu", cpun, "intrstat", ALL_ZONES)) !=
23920Sstevel@tonic-gate 	    NULL) {
23930Sstevel@tonic-gate 		kstat_zone_add(ksp, zoneid);
23940Sstevel@tonic-gate 		kstat_rele(ksp);
23950Sstevel@tonic-gate 	}
23960Sstevel@tonic-gate }
23970Sstevel@tonic-gate 
23980Sstevel@tonic-gate /*
23990Sstevel@tonic-gate  * Update relevant kstats such that cpu is now visible to processes
24000Sstevel@tonic-gate  * executing in specified zone.
24010Sstevel@tonic-gate  */
24020Sstevel@tonic-gate void
24030Sstevel@tonic-gate cpu_visibility_add(cpu_t *cp, zone_t *zone)
24040Sstevel@tonic-gate {
24050Sstevel@tonic-gate 	cpu_visibility_configure(cp, zone);
24060Sstevel@tonic-gate 	if (cpu_is_active(cp))
24070Sstevel@tonic-gate 		cpu_visibility_online(cp, zone);
24080Sstevel@tonic-gate }
24090Sstevel@tonic-gate 
24100Sstevel@tonic-gate /*
24110Sstevel@tonic-gate  * Make invisible to the zone that subset of the cpu information that would be
24120Sstevel@tonic-gate  * torn down when a previously offlined cpu is unconfigured.
24130Sstevel@tonic-gate  */
24140Sstevel@tonic-gate void
24150Sstevel@tonic-gate cpu_visibility_unconfigure(cpu_t *cp, zone_t *zone)
24160Sstevel@tonic-gate {
24170Sstevel@tonic-gate 	zoneid_t zoneid = zone ? zone->zone_id : ALL_ZONES;
24180Sstevel@tonic-gate 
24190Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
24200Sstevel@tonic-gate 	ASSERT(pool_pset_enabled());
24210Sstevel@tonic-gate 	ASSERT(cp != NULL);
24220Sstevel@tonic-gate 
24230Sstevel@tonic-gate 	if (zoneid != ALL_ZONES && zoneid != GLOBAL_ZONEID) {
24240Sstevel@tonic-gate 		ASSERT(zone->zone_ncpus != 0);
24250Sstevel@tonic-gate 		zone->zone_ncpus--;
24260Sstevel@tonic-gate 	}
24270Sstevel@tonic-gate 	if (cp->cpu_info_kstat)
24280Sstevel@tonic-gate 		kstat_zone_remove(cp->cpu_info_kstat, zoneid);
24290Sstevel@tonic-gate }
24300Sstevel@tonic-gate 
24310Sstevel@tonic-gate /*
24320Sstevel@tonic-gate  * Make invisible to the zone that subset of the cpu information that would be
24330Sstevel@tonic-gate  * torn down when a cpu is offlined (but still configured).
24340Sstevel@tonic-gate  */
24350Sstevel@tonic-gate void
24360Sstevel@tonic-gate cpu_visibility_offline(cpu_t *cp, zone_t *zone)
24370Sstevel@tonic-gate {
24380Sstevel@tonic-gate 	kstat_t *ksp;
24390Sstevel@tonic-gate 	char name[sizeof ("cpu_stat") + 10];	/* enough for 32-bit cpuids */
24400Sstevel@tonic-gate 	zoneid_t zoneid = zone ? zone->zone_id : ALL_ZONES;
24410Sstevel@tonic-gate 	processorid_t cpun;
24420Sstevel@tonic-gate 
24430Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
24440Sstevel@tonic-gate 	ASSERT(pool_pset_enabled());
24450Sstevel@tonic-gate 	ASSERT(cp != NULL);
24460Sstevel@tonic-gate 	ASSERT(cpu_is_active(cp));
24470Sstevel@tonic-gate 
24480Sstevel@tonic-gate 	cpun = cp->cpu_id;
24490Sstevel@tonic-gate 	if (zoneid != ALL_ZONES && zoneid != GLOBAL_ZONEID) {
24500Sstevel@tonic-gate 		ASSERT(zone->zone_ncpus_online != 0);
24510Sstevel@tonic-gate 		zone->zone_ncpus_online--;
24520Sstevel@tonic-gate 	}
24530Sstevel@tonic-gate 
24540Sstevel@tonic-gate 	if ((ksp = kstat_hold_byname("cpu", cpun, "intrstat", ALL_ZONES)) !=
24550Sstevel@tonic-gate 	    NULL) {
24560Sstevel@tonic-gate 		kstat_zone_remove(ksp, zoneid);
24570Sstevel@tonic-gate 		kstat_rele(ksp);
24580Sstevel@tonic-gate 	}
24590Sstevel@tonic-gate 	if ((ksp = kstat_hold_byname("cpu", cpun, "vm", ALL_ZONES)) != NULL) {
24600Sstevel@tonic-gate 		kstat_zone_remove(ksp, zoneid);
24610Sstevel@tonic-gate 		kstat_rele(ksp);
24620Sstevel@tonic-gate 	}
24630Sstevel@tonic-gate 	if ((ksp = kstat_hold_byname("cpu", cpun, "sys", ALL_ZONES)) != NULL) {
24640Sstevel@tonic-gate 		kstat_zone_remove(ksp, zoneid);
24650Sstevel@tonic-gate 		kstat_rele(ksp);
24660Sstevel@tonic-gate 	}
24670Sstevel@tonic-gate 	(void) snprintf(name, sizeof (name), "cpu_stat%d", cpun);
24680Sstevel@tonic-gate 	if ((ksp = kstat_hold_byname("cpu_stat", cpun, name, ALL_ZONES))
24690Sstevel@tonic-gate 	    != NULL) {
24700Sstevel@tonic-gate 		kstat_zone_remove(ksp, zoneid);
24710Sstevel@tonic-gate 		kstat_rele(ksp);
24720Sstevel@tonic-gate 	}
24730Sstevel@tonic-gate }
24740Sstevel@tonic-gate 
24750Sstevel@tonic-gate /*
24760Sstevel@tonic-gate  * Update relevant kstats such that cpu is no longer visible to processes
24770Sstevel@tonic-gate  * executing in specified zone.
24780Sstevel@tonic-gate  */
24790Sstevel@tonic-gate void
24800Sstevel@tonic-gate cpu_visibility_remove(cpu_t *cp, zone_t *zone)
24810Sstevel@tonic-gate {
24820Sstevel@tonic-gate 	if (cpu_is_active(cp))
24830Sstevel@tonic-gate 		cpu_visibility_offline(cp, zone);
24840Sstevel@tonic-gate 	cpu_visibility_unconfigure(cp, zone);
24850Sstevel@tonic-gate }
24860Sstevel@tonic-gate 
24870Sstevel@tonic-gate /*
24880Sstevel@tonic-gate  * Bind a thread to a CPU as requested.
24890Sstevel@tonic-gate  */
24900Sstevel@tonic-gate int
24910Sstevel@tonic-gate cpu_bind_thread(kthread_id_t tp, processorid_t bind, processorid_t *obind,
24920Sstevel@tonic-gate     int *error)
24930Sstevel@tonic-gate {
24940Sstevel@tonic-gate 	processorid_t	binding;
24956298Sakolb 	cpu_t		*cp = NULL;
24960Sstevel@tonic-gate 
24970Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
24980Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ttoproc(tp)->p_lock));
24990Sstevel@tonic-gate 
25000Sstevel@tonic-gate 	thread_lock(tp);
25010Sstevel@tonic-gate 
25020Sstevel@tonic-gate 	/*
25030Sstevel@tonic-gate 	 * Record old binding, but change the obind, which was initialized
25040Sstevel@tonic-gate 	 * to PBIND_NONE, only if this thread has a binding.  This avoids
25050Sstevel@tonic-gate 	 * reporting PBIND_NONE for a process when some LWPs are bound.
25060Sstevel@tonic-gate 	 */
25070Sstevel@tonic-gate 	binding = tp->t_bind_cpu;
25088028SAlexander.Kolbasov@Sun.COM 	if (binding != PBIND_NONE)
25098028SAlexander.Kolbasov@Sun.COM 		*obind = binding;	/* record old binding */
25106298Sakolb 
25116298Sakolb 	switch (bind) {
25126298Sakolb 	case PBIND_QUERY:
25136298Sakolb 		/* Just return the old binding */
25146298Sakolb 		thread_unlock(tp);
25156298Sakolb 		return (0);
25166298Sakolb 
25176298Sakolb 	case PBIND_QUERY_TYPE:
25186298Sakolb 		/* Return the binding type */
25196298Sakolb 		*obind = TB_CPU_IS_SOFT(tp) ? PBIND_SOFT : PBIND_HARD;
25200Sstevel@tonic-gate 		thread_unlock(tp);
25210Sstevel@tonic-gate 		return (0);
25226298Sakolb 
25236298Sakolb 	case PBIND_SOFT:
25246298Sakolb 		/*
25256298Sakolb 		 *  Set soft binding for this thread and return the actual
25266298Sakolb 		 *  binding
25276298Sakolb 		 */
25286298Sakolb 		TB_CPU_SOFT_SET(tp);
25296298Sakolb 		thread_unlock(tp);
25306298Sakolb 		return (0);
25316298Sakolb 
25326298Sakolb 	case PBIND_HARD:
25336298Sakolb 		/*
25346298Sakolb 		 *  Set hard binding for this thread and return the actual
25356298Sakolb 		 *  binding
25366298Sakolb 		 */
25376298Sakolb 		TB_CPU_HARD_SET(tp);
25386298Sakolb 		thread_unlock(tp);
25396298Sakolb 		return (0);
25406298Sakolb 
25416298Sakolb 	default:
25426298Sakolb 		break;
25430Sstevel@tonic-gate 	}
25440Sstevel@tonic-gate 
25450Sstevel@tonic-gate 	/*
25460Sstevel@tonic-gate 	 * If this thread/LWP cannot be bound because of permission
25470Sstevel@tonic-gate 	 * problems, just note that and return success so that the
25480Sstevel@tonic-gate 	 * other threads/LWPs will be bound.  This is the way
25490Sstevel@tonic-gate 	 * processor_bind() is defined to work.
25500Sstevel@tonic-gate 	 *
25510Sstevel@tonic-gate 	 * Binding will get EPERM if the thread is of system class
25520Sstevel@tonic-gate 	 * or hasprocperm() fails.
25530Sstevel@tonic-gate 	 */
25540Sstevel@tonic-gate 	if (tp->t_cid == 0 || !hasprocperm(tp->t_cred, CRED())) {
25550Sstevel@tonic-gate 		*error = EPERM;
25560Sstevel@tonic-gate 		thread_unlock(tp);
25570Sstevel@tonic-gate 		return (0);
25580Sstevel@tonic-gate 	}
25590Sstevel@tonic-gate 
25600Sstevel@tonic-gate 	binding = bind;
25610Sstevel@tonic-gate 	if (binding != PBIND_NONE) {
25626298Sakolb 		cp = cpu_get((processorid_t)binding);
25630Sstevel@tonic-gate 		/*
25646298Sakolb 		 * Make sure binding is valid and is in right partition.
25650Sstevel@tonic-gate 		 */
25666298Sakolb 		if (cp == NULL || tp->t_cpupart != cp->cpu_part) {
25670Sstevel@tonic-gate 			*error = EINVAL;
25680Sstevel@tonic-gate 			thread_unlock(tp);
25690Sstevel@tonic-gate 			return (0);
25700Sstevel@tonic-gate 		}
25710Sstevel@tonic-gate 	}
25720Sstevel@tonic-gate 	tp->t_bind_cpu = binding;	/* set new binding */
25730Sstevel@tonic-gate 
25740Sstevel@tonic-gate 	/*
25750Sstevel@tonic-gate 	 * If there is no system-set reason for affinity, set
25760Sstevel@tonic-gate 	 * the t_bound_cpu field to reflect the binding.
25770Sstevel@tonic-gate 	 */
25780Sstevel@tonic-gate 	if (tp->t_affinitycnt == 0) {
25790Sstevel@tonic-gate 		if (binding == PBIND_NONE) {
25800Sstevel@tonic-gate 			/*
25810Sstevel@tonic-gate 			 * We may need to adjust disp_max_unbound_pri
25820Sstevel@tonic-gate 			 * since we're becoming unbound.
25830Sstevel@tonic-gate 			 */
25840Sstevel@tonic-gate 			disp_adjust_unbound_pri(tp);
25850Sstevel@tonic-gate 
25860Sstevel@tonic-gate 			tp->t_bound_cpu = NULL;	/* set new binding */
25870Sstevel@tonic-gate 
25880Sstevel@tonic-gate 			/*
25890Sstevel@tonic-gate 			 * Move thread to lgroup with strongest affinity
25900Sstevel@tonic-gate 			 * after unbinding
25910Sstevel@tonic-gate 			 */
25920Sstevel@tonic-gate 			if (tp->t_lgrp_affinity)
25930Sstevel@tonic-gate 				lgrp_move_thread(tp,
25940Sstevel@tonic-gate 				    lgrp_choose(tp, tp->t_cpupart), 1);
25950Sstevel@tonic-gate 
25960Sstevel@tonic-gate 			if (tp->t_state == TS_ONPROC &&
25970Sstevel@tonic-gate 			    tp->t_cpu->cpu_part != tp->t_cpupart)
25980Sstevel@tonic-gate 				cpu_surrender(tp);
25990Sstevel@tonic-gate 		} else {
26000Sstevel@tonic-gate 			lpl_t	*lpl;
26010Sstevel@tonic-gate 
26020Sstevel@tonic-gate 			tp->t_bound_cpu = cp;
26030Sstevel@tonic-gate 			ASSERT(cp->cpu_lpl != NULL);
26040Sstevel@tonic-gate 
26050Sstevel@tonic-gate 			/*
26060Sstevel@tonic-gate 			 * Set home to lgroup with most affinity containing CPU
26070Sstevel@tonic-gate 			 * that thread is being bound or minimum bounding
26080Sstevel@tonic-gate 			 * lgroup if no affinities set
26090Sstevel@tonic-gate 			 */
26100Sstevel@tonic-gate 			if (tp->t_lgrp_affinity)
26112988Sjjc 				lpl = lgrp_affinity_best(tp, tp->t_cpupart,
26122988Sjjc 				    LGRP_NONE, B_FALSE);
26130Sstevel@tonic-gate 			else
26140Sstevel@tonic-gate 				lpl = cp->cpu_lpl;
26150Sstevel@tonic-gate 
26160Sstevel@tonic-gate 			if (tp->t_lpl != lpl) {
26170Sstevel@tonic-gate 				/* can't grab cpu_lock */
26180Sstevel@tonic-gate 				lgrp_move_thread(tp, lpl, 1);
26190Sstevel@tonic-gate 			}
26200Sstevel@tonic-gate 
26210Sstevel@tonic-gate 			/*
26220Sstevel@tonic-gate 			 * Make the thread switch to the bound CPU.
26230Sstevel@tonic-gate 			 * If the thread is runnable, we need to
26240Sstevel@tonic-gate 			 * requeue it even if t_cpu is already set
26250Sstevel@tonic-gate 			 * to the right CPU, since it may be on a
26260Sstevel@tonic-gate 			 * kpreempt queue and need to move to a local
26270Sstevel@tonic-gate 			 * queue.  We could check t_disp_queue to
26280Sstevel@tonic-gate 			 * avoid unnecessary overhead if it's already
26290Sstevel@tonic-gate 			 * on the right queue, but since this isn't
26300Sstevel@tonic-gate 			 * a performance-critical operation it doesn't
26310Sstevel@tonic-gate 			 * seem worth the extra code and complexity.
26320Sstevel@tonic-gate 			 *
26330Sstevel@tonic-gate 			 * If the thread is weakbound to the cpu then it will
26340Sstevel@tonic-gate 			 * resist the new binding request until the weak
26350Sstevel@tonic-gate 			 * binding drops.  The cpu_surrender or requeueing
26360Sstevel@tonic-gate 			 * below could be skipped in such cases (since it
26370Sstevel@tonic-gate 			 * will have no effect), but that would require
26380Sstevel@tonic-gate 			 * thread_allowmigrate to acquire thread_lock so
26390Sstevel@tonic-gate 			 * we'll take the very occasional hit here instead.
26400Sstevel@tonic-gate 			 */
26410Sstevel@tonic-gate 			if (tp->t_state == TS_ONPROC) {
26420Sstevel@tonic-gate 				cpu_surrender(tp);
26430Sstevel@tonic-gate 			} else if (tp->t_state == TS_RUN) {
26440Sstevel@tonic-gate 				cpu_t *ocp = tp->t_cpu;
26450Sstevel@tonic-gate 
26460Sstevel@tonic-gate 				(void) dispdeq(tp);
26470Sstevel@tonic-gate 				setbackdq(tp);
26480Sstevel@tonic-gate 				/*
26490Sstevel@tonic-gate 				 * Either on the bound CPU's disp queue now,
26500Sstevel@tonic-gate 				 * or swapped out or on the swap queue.
26510Sstevel@tonic-gate 				 */
26520Sstevel@tonic-gate 				ASSERT(tp->t_disp_queue == cp->cpu_disp ||
26530Sstevel@tonic-gate 				    tp->t_weakbound_cpu == ocp ||
26540Sstevel@tonic-gate 				    (tp->t_schedflag & (TS_LOAD | TS_ON_SWAPQ))
26550Sstevel@tonic-gate 				    != TS_LOAD);
26560Sstevel@tonic-gate 			}
26570Sstevel@tonic-gate 		}
26580Sstevel@tonic-gate 	}
26590Sstevel@tonic-gate 
26600Sstevel@tonic-gate 	/*
26610Sstevel@tonic-gate 	 * Our binding has changed; set TP_CHANGEBIND.
26620Sstevel@tonic-gate 	 */
26630Sstevel@tonic-gate 	tp->t_proc_flag |= TP_CHANGEBIND;
26640Sstevel@tonic-gate 	aston(tp);
26650Sstevel@tonic-gate 
26660Sstevel@tonic-gate 	thread_unlock(tp);
26670Sstevel@tonic-gate 
26680Sstevel@tonic-gate 	return (0);
26690Sstevel@tonic-gate }
26700Sstevel@tonic-gate 
26710Sstevel@tonic-gate #if CPUSET_WORDS > 1
26720Sstevel@tonic-gate 
26730Sstevel@tonic-gate /*
26740Sstevel@tonic-gate  * Functions for implementing cpuset operations when a cpuset is more
26750Sstevel@tonic-gate  * than one word.  On platforms where a cpuset is a single word these
26760Sstevel@tonic-gate  * are implemented as macros in cpuvar.h.
26770Sstevel@tonic-gate  */
26780Sstevel@tonic-gate 
26790Sstevel@tonic-gate void
26800Sstevel@tonic-gate cpuset_all(cpuset_t *s)
26810Sstevel@tonic-gate {
26820Sstevel@tonic-gate 	int i;
26830Sstevel@tonic-gate 
26840Sstevel@tonic-gate 	for (i = 0; i < CPUSET_WORDS; i++)
26850Sstevel@tonic-gate 		s->cpub[i] = ~0UL;
26860Sstevel@tonic-gate }
26870Sstevel@tonic-gate 
26880Sstevel@tonic-gate void
26890Sstevel@tonic-gate cpuset_all_but(cpuset_t *s, uint_t cpu)
26900Sstevel@tonic-gate {
26910Sstevel@tonic-gate 	cpuset_all(s);
26920Sstevel@tonic-gate 	CPUSET_DEL(*s, cpu);
26930Sstevel@tonic-gate }
26940Sstevel@tonic-gate 
26950Sstevel@tonic-gate void
26960Sstevel@tonic-gate cpuset_only(cpuset_t *s, uint_t cpu)
26970Sstevel@tonic-gate {
26980Sstevel@tonic-gate 	CPUSET_ZERO(*s);
26990Sstevel@tonic-gate 	CPUSET_ADD(*s, cpu);
27000Sstevel@tonic-gate }
27010Sstevel@tonic-gate 
27020Sstevel@tonic-gate int
27030Sstevel@tonic-gate cpuset_isnull(cpuset_t *s)
27040Sstevel@tonic-gate {
27050Sstevel@tonic-gate 	int i;
27060Sstevel@tonic-gate 
27070Sstevel@tonic-gate 	for (i = 0; i < CPUSET_WORDS; i++)
27080Sstevel@tonic-gate 		if (s->cpub[i] != 0)
27090Sstevel@tonic-gate 			return (0);
27100Sstevel@tonic-gate 	return (1);
27110Sstevel@tonic-gate }
27120Sstevel@tonic-gate 
27130Sstevel@tonic-gate int
27140Sstevel@tonic-gate cpuset_cmp(cpuset_t *s1, cpuset_t *s2)
27150Sstevel@tonic-gate {
27160Sstevel@tonic-gate 	int i;
27170Sstevel@tonic-gate 
27180Sstevel@tonic-gate 	for (i = 0; i < CPUSET_WORDS; i++)
27190Sstevel@tonic-gate 		if (s1->cpub[i] != s2->cpub[i])
27200Sstevel@tonic-gate 			return (0);
27210Sstevel@tonic-gate 	return (1);
27220Sstevel@tonic-gate }
27230Sstevel@tonic-gate 
27240Sstevel@tonic-gate uint_t
27250Sstevel@tonic-gate cpuset_find(cpuset_t *s)
27260Sstevel@tonic-gate {
27270Sstevel@tonic-gate 
27280Sstevel@tonic-gate 	uint_t	i;
27290Sstevel@tonic-gate 	uint_t	cpu = (uint_t)-1;
27300Sstevel@tonic-gate 
27310Sstevel@tonic-gate 	/*
27320Sstevel@tonic-gate 	 * Find a cpu in the cpuset
27330Sstevel@tonic-gate 	 */
27341772Sjl139090 	for (i = 0; i < CPUSET_WORDS; i++) {
27350Sstevel@tonic-gate 		cpu = (uint_t)(lowbit(s->cpub[i]) - 1);
27361772Sjl139090 		if (cpu != (uint_t)-1) {
27371772Sjl139090 			cpu += i * BT_NBIPUL;
27381772Sjl139090 			break;
27391772Sjl139090 		}
27401772Sjl139090 	}
27410Sstevel@tonic-gate 	return (cpu);
27420Sstevel@tonic-gate }
27430Sstevel@tonic-gate 
27442492Sha137994 void
27452492Sha137994 cpuset_bounds(cpuset_t *s, uint_t *smallestid, uint_t *largestid)
27462492Sha137994 {
27472492Sha137994 	int	i, j;
27482492Sha137994 	uint_t	bit;
27492492Sha137994 
27502492Sha137994 	/*
27512492Sha137994 	 * First, find the smallest cpu id in the set.
27522492Sha137994 	 */
27532492Sha137994 	for (i = 0; i < CPUSET_WORDS; i++) {
27542492Sha137994 		if (s->cpub[i] != 0) {
27552492Sha137994 			bit = (uint_t)(lowbit(s->cpub[i]) - 1);
27562492Sha137994 			ASSERT(bit != (uint_t)-1);
27572492Sha137994 			*smallestid = bit + (i * BT_NBIPUL);
27582492Sha137994 
27592492Sha137994 			/*
27602492Sha137994 			 * Now find the largest cpu id in
27612492Sha137994 			 * the set and return immediately.
27622492Sha137994 			 * Done in an inner loop to avoid
27632492Sha137994 			 * having to break out of the first
27642492Sha137994 			 * loop.
27652492Sha137994 			 */
27662492Sha137994 			for (j = CPUSET_WORDS - 1; j >= i; j--) {
27672492Sha137994 				if (s->cpub[j] != 0) {
27682492Sha137994 					bit = (uint_t)(highbit(s->cpub[j]) - 1);
27692492Sha137994 					ASSERT(bit != (uint_t)-1);
27702492Sha137994 					*largestid = bit + (j * BT_NBIPUL);
27712492Sha137994 					ASSERT(*largestid >= *smallestid);
27722492Sha137994 					return;
27732492Sha137994 				}
27742492Sha137994 			}
27752492Sha137994 
27762492Sha137994 			/*
27772492Sha137994 			 * If this code is reached, a
27782492Sha137994 			 * smallestid was found, but not a
27792492Sha137994 			 * largestid. The cpuset must have
27802492Sha137994 			 * been changed during the course
27812492Sha137994 			 * of this function call.
27822492Sha137994 			 */
27832492Sha137994 			ASSERT(0);
27842492Sha137994 		}
27852492Sha137994 	}
27862492Sha137994 	*smallestid = *largestid = CPUSET_NOTINSET;
27872492Sha137994 }
27882492Sha137994 
27890Sstevel@tonic-gate #endif	/* CPUSET_WORDS */
27900Sstevel@tonic-gate 
27910Sstevel@tonic-gate /*
27926298Sakolb  * Unbind threads bound to specified CPU.
27936298Sakolb  *
27946298Sakolb  * If `unbind_all_threads' is true, unbind all user threads bound to a given
27956298Sakolb  * CPU. Otherwise unbind all soft-bound user threads.
27960Sstevel@tonic-gate  */
27970Sstevel@tonic-gate int
27986298Sakolb cpu_unbind(processorid_t cpu, boolean_t unbind_all_threads)
27990Sstevel@tonic-gate {
28000Sstevel@tonic-gate 	processorid_t obind;
28010Sstevel@tonic-gate 	kthread_t *tp;
28020Sstevel@tonic-gate 	int ret = 0;
28030Sstevel@tonic-gate 	proc_t *pp;
28040Sstevel@tonic-gate 	int err, berr = 0;
28050Sstevel@tonic-gate 
28060Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
28070Sstevel@tonic-gate 
28080Sstevel@tonic-gate 	mutex_enter(&pidlock);
28090Sstevel@tonic-gate 	for (pp = practive; pp != NULL; pp = pp->p_next) {
28100Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
28110Sstevel@tonic-gate 		tp = pp->p_tlist;
28120Sstevel@tonic-gate 		/*
28130Sstevel@tonic-gate 		 * Skip zombies, kernel processes, and processes in
28140Sstevel@tonic-gate 		 * other zones, if called from a non-global zone.
28150Sstevel@tonic-gate 		 */
28160Sstevel@tonic-gate 		if (tp == NULL || (pp->p_flag & SSYS) ||
28170Sstevel@tonic-gate 		    !HASZONEACCESS(curproc, pp->p_zone->zone_id)) {
28180Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
28190Sstevel@tonic-gate 			continue;
28200Sstevel@tonic-gate 		}
28210Sstevel@tonic-gate 		do {
28220Sstevel@tonic-gate 			if (tp->t_bind_cpu != cpu)
28230Sstevel@tonic-gate 				continue;
28246298Sakolb 			/*
28256298Sakolb 			 * Skip threads with hard binding when
28266298Sakolb 			 * `unbind_all_threads' is not specified.
28276298Sakolb 			 */
28286298Sakolb 			if (!unbind_all_threads && TB_CPU_IS_HARD(tp))
28296298Sakolb 				continue;
28300Sstevel@tonic-gate 			err = cpu_bind_thread(tp, PBIND_NONE, &obind, &berr);
28310Sstevel@tonic-gate 			if (ret == 0)
28320Sstevel@tonic-gate 				ret = err;
28330Sstevel@tonic-gate 		} while ((tp = tp->t_forw) != pp->p_tlist);
28340Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
28350Sstevel@tonic-gate 	}
28360Sstevel@tonic-gate 	mutex_exit(&pidlock);
28370Sstevel@tonic-gate 	if (ret == 0)
28380Sstevel@tonic-gate 		ret = berr;
28390Sstevel@tonic-gate 	return (ret);
28400Sstevel@tonic-gate }
28410Sstevel@tonic-gate 
28420Sstevel@tonic-gate 
28430Sstevel@tonic-gate /*
28440Sstevel@tonic-gate  * Destroy all remaining bound threads on a cpu.
28450Sstevel@tonic-gate  */
28460Sstevel@tonic-gate void
28470Sstevel@tonic-gate cpu_destroy_bound_threads(cpu_t *cp)
28480Sstevel@tonic-gate {
28490Sstevel@tonic-gate 	extern id_t syscid;
28500Sstevel@tonic-gate 	register kthread_id_t	t, tlist, tnext;
28510Sstevel@tonic-gate 
28520Sstevel@tonic-gate 	/*
28530Sstevel@tonic-gate 	 * Destroy all remaining bound threads on the cpu.  This
28540Sstevel@tonic-gate 	 * should include both the interrupt threads and the idle thread.
28550Sstevel@tonic-gate 	 * This requires some care, since we need to traverse the
28560Sstevel@tonic-gate 	 * thread list with the pidlock mutex locked, but thread_free
28570Sstevel@tonic-gate 	 * also locks the pidlock mutex.  So, we collect the threads
28580Sstevel@tonic-gate 	 * we're going to reap in a list headed by "tlist", then we
28590Sstevel@tonic-gate 	 * unlock the pidlock mutex and traverse the tlist list,
28600Sstevel@tonic-gate 	 * doing thread_free's on the thread's.	 Simple, n'est pas?
28610Sstevel@tonic-gate 	 * Also, this depends on thread_free not mucking with the
28620Sstevel@tonic-gate 	 * t_next and t_prev links of the thread.
28630Sstevel@tonic-gate 	 */
28640Sstevel@tonic-gate 
28650Sstevel@tonic-gate 	if ((t = curthread) != NULL) {
28660Sstevel@tonic-gate 
28670Sstevel@tonic-gate 		tlist = NULL;
28680Sstevel@tonic-gate 		mutex_enter(&pidlock);
28690Sstevel@tonic-gate 		do {
28700Sstevel@tonic-gate 			tnext = t->t_next;
28710Sstevel@tonic-gate 			if (t->t_bound_cpu == cp) {
28720Sstevel@tonic-gate 
28730Sstevel@tonic-gate 				/*
28740Sstevel@tonic-gate 				 * We've found a bound thread, carefully unlink
28750Sstevel@tonic-gate 				 * it out of the thread list, and add it to
28760Sstevel@tonic-gate 				 * our "tlist".	 We "know" we don't have to
28770Sstevel@tonic-gate 				 * worry about unlinking curthread (the thread
28780Sstevel@tonic-gate 				 * that is executing this code).
28790Sstevel@tonic-gate 				 */
28800Sstevel@tonic-gate 				t->t_next->t_prev = t->t_prev;
28810Sstevel@tonic-gate 				t->t_prev->t_next = t->t_next;
28820Sstevel@tonic-gate 				t->t_next = tlist;
28830Sstevel@tonic-gate 				tlist = t;
28840Sstevel@tonic-gate 				ASSERT(t->t_cid == syscid);
28850Sstevel@tonic-gate 				/* wake up anyone blocked in thread_join */
28860Sstevel@tonic-gate 				cv_broadcast(&t->t_joincv);
28870Sstevel@tonic-gate 				/*
28880Sstevel@tonic-gate 				 * t_lwp set by interrupt threads and not
28890Sstevel@tonic-gate 				 * cleared.
28900Sstevel@tonic-gate 				 */
28910Sstevel@tonic-gate 				t->t_lwp = NULL;
28920Sstevel@tonic-gate 				/*
28930Sstevel@tonic-gate 				 * Pause and idle threads always have
28940Sstevel@tonic-gate 				 * t_state set to TS_ONPROC.
28950Sstevel@tonic-gate 				 */
28960Sstevel@tonic-gate 				t->t_state = TS_FREE;
28970Sstevel@tonic-gate 				t->t_prev = NULL;	/* Just in case */
28980Sstevel@tonic-gate 			}
28990Sstevel@tonic-gate 
29000Sstevel@tonic-gate 		} while ((t = tnext) != curthread);
29010Sstevel@tonic-gate 
29020Sstevel@tonic-gate 		mutex_exit(&pidlock);
29030Sstevel@tonic-gate 
29045834Spt157919 		mutex_sync();
29050Sstevel@tonic-gate 		for (t = tlist; t != NULL; t = tnext) {
29060Sstevel@tonic-gate 			tnext = t->t_next;
29070Sstevel@tonic-gate 			thread_free(t);
29080Sstevel@tonic-gate 		}
29090Sstevel@tonic-gate 	}
29100Sstevel@tonic-gate }
29110Sstevel@tonic-gate 
29120Sstevel@tonic-gate /*
29134877Smh27603  * Update the cpu_supp_freqs of this cpu. This information is returned
29145286Smh27603  * as part of cpu_info kstats. If the cpu_info_kstat exists already, then
29155286Smh27603  * maintain the kstat data size.
29164877Smh27603  */
29174877Smh27603 void
29184877Smh27603 cpu_set_supp_freqs(cpu_t *cp, const char *freqs)
29194877Smh27603 {
29204877Smh27603 	char clkstr[sizeof ("18446744073709551615") + 1]; /* ui64 MAX */
29214877Smh27603 	const char *lfreqs = clkstr;
29225286Smh27603 	boolean_t kstat_exists = B_FALSE;
29235286Smh27603 	kstat_t *ksp;
29245286Smh27603 	size_t len;
29254877Smh27603 
29264877Smh27603 	/*
29274877Smh27603 	 * A NULL pointer means we only support one speed.
29284877Smh27603 	 */
29294877Smh27603 	if (freqs == NULL)
29304877Smh27603 		(void) snprintf(clkstr, sizeof (clkstr), "%"PRIu64,
29314877Smh27603 		    cp->cpu_curr_clock);
29324877Smh27603 	else
29334877Smh27603 		lfreqs = freqs;
29344877Smh27603 
29354877Smh27603 	/*
29364877Smh27603 	 * Make sure the frequency doesn't change while a snapshot is
29374892Smh27603 	 * going on. Of course, we only need to worry about this if
29384892Smh27603 	 * the kstat exists.
29394877Smh27603 	 */
29405286Smh27603 	if ((ksp = cp->cpu_info_kstat) != NULL) {
29415286Smh27603 		mutex_enter(ksp->ks_lock);
29425286Smh27603 		kstat_exists = B_TRUE;
29434892Smh27603 	}
29444877Smh27603 
29454877Smh27603 	/*
29465286Smh27603 	 * Free any previously allocated string and if the kstat
29475286Smh27603 	 * already exists, then update its data size.
29484877Smh27603 	 */
29495286Smh27603 	if (cp->cpu_supp_freqs != NULL) {
29505286Smh27603 		len = strlen(cp->cpu_supp_freqs) + 1;
29515286Smh27603 		kmem_free(cp->cpu_supp_freqs, len);
29525286Smh27603 		if (kstat_exists)
29535286Smh27603 			ksp->ks_data_size -= len;
29545286Smh27603 	}
29554877Smh27603 
29564877Smh27603 	/*
29574877Smh27603 	 * Allocate the new string and set the pointer.
29584877Smh27603 	 */
29595286Smh27603 	len = strlen(lfreqs) + 1;
29605286Smh27603 	cp->cpu_supp_freqs = kmem_alloc(len, KM_SLEEP);
29614877Smh27603 	(void) strcpy(cp->cpu_supp_freqs, lfreqs);
29624877Smh27603 
29634877Smh27603 	/*
29645286Smh27603 	 * If the kstat already exists then update the data size and
29655286Smh27603 	 * free the lock.
29664877Smh27603 	 */
29675286Smh27603 	if (kstat_exists) {
29685286Smh27603 		ksp->ks_data_size += len;
29695286Smh27603 		mutex_exit(ksp->ks_lock);
29705286Smh27603 	}
29714877Smh27603 }
29724877Smh27603 
29734877Smh27603 /*
29748906SEric.Saxe@Sun.COM  * Indicate the current CPU's clock freqency (in Hz).
29758906SEric.Saxe@Sun.COM  * The calling context must be such that CPU references are safe.
29768906SEric.Saxe@Sun.COM  */
29778906SEric.Saxe@Sun.COM void
29788906SEric.Saxe@Sun.COM cpu_set_curr_clock(uint64_t new_clk)
29798906SEric.Saxe@Sun.COM {
29808906SEric.Saxe@Sun.COM 	uint64_t old_clk;
29818906SEric.Saxe@Sun.COM 
29828906SEric.Saxe@Sun.COM 	old_clk = CPU->cpu_curr_clock;
29838906SEric.Saxe@Sun.COM 	CPU->cpu_curr_clock = new_clk;
29848906SEric.Saxe@Sun.COM 
29858906SEric.Saxe@Sun.COM 	/*
29868906SEric.Saxe@Sun.COM 	 * The cpu-change-speed DTrace probe exports the frequency in Hz
29878906SEric.Saxe@Sun.COM 	 */
29888906SEric.Saxe@Sun.COM 	DTRACE_PROBE3(cpu__change__speed, processorid_t, CPU->cpu_id,
29898906SEric.Saxe@Sun.COM 	    uint64_t, old_clk, uint64_t, new_clk);
29908906SEric.Saxe@Sun.COM }
29918906SEric.Saxe@Sun.COM 
29928906SEric.Saxe@Sun.COM /*
29930Sstevel@tonic-gate  * processor_info(2) and p_online(2) status support functions
29940Sstevel@tonic-gate  *   The constants returned by the cpu_get_state() and cpu_get_state_str() are
29950Sstevel@tonic-gate  *   for use in communicating processor state information to userland.  Kernel
29960Sstevel@tonic-gate  *   subsystems should only be using the cpu_flags value directly.  Subsystems
29970Sstevel@tonic-gate  *   modifying cpu_flags should record the state change via a call to the
29980Sstevel@tonic-gate  *   cpu_set_state().
29990Sstevel@tonic-gate  */
30000Sstevel@tonic-gate 
30010Sstevel@tonic-gate /*
30020Sstevel@tonic-gate  * Update the pi_state of this CPU.  This function provides the CPU status for
30030Sstevel@tonic-gate  * the information returned by processor_info(2).
30040Sstevel@tonic-gate  */
30050Sstevel@tonic-gate void
30060Sstevel@tonic-gate cpu_set_state(cpu_t *cpu)
30070Sstevel@tonic-gate {
30080Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
30090Sstevel@tonic-gate 	cpu->cpu_type_info.pi_state = cpu_get_state(cpu);
30100Sstevel@tonic-gate 	cpu->cpu_state_begin = gethrestime_sec();
30110Sstevel@tonic-gate 	pool_cpu_mod = gethrtime();
30120Sstevel@tonic-gate }
30130Sstevel@tonic-gate 
30140Sstevel@tonic-gate /*
30150Sstevel@tonic-gate  * Return offline/online/other status for the indicated CPU.  Use only for
30160Sstevel@tonic-gate  * communication with user applications; cpu_flags provides the in-kernel
30170Sstevel@tonic-gate  * interface.
30180Sstevel@tonic-gate  */
30190Sstevel@tonic-gate int
30200Sstevel@tonic-gate cpu_get_state(cpu_t *cpu)
30210Sstevel@tonic-gate {
30220Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
30230Sstevel@tonic-gate 	if (cpu->cpu_flags & CPU_POWEROFF)
30240Sstevel@tonic-gate 		return (P_POWEROFF);
30250Sstevel@tonic-gate 	else if (cpu->cpu_flags & CPU_FAULTED)
30260Sstevel@tonic-gate 		return (P_FAULTED);
30270Sstevel@tonic-gate 	else if (cpu->cpu_flags & CPU_SPARE)
30280Sstevel@tonic-gate 		return (P_SPARE);
30290Sstevel@tonic-gate 	else if ((cpu->cpu_flags & (CPU_READY | CPU_OFFLINE)) != CPU_READY)
30300Sstevel@tonic-gate 		return (P_OFFLINE);
30310Sstevel@tonic-gate 	else if (cpu->cpu_flags & CPU_ENABLE)
30320Sstevel@tonic-gate 		return (P_ONLINE);
30330Sstevel@tonic-gate 	else
30340Sstevel@tonic-gate 		return (P_NOINTR);
30350Sstevel@tonic-gate }
30360Sstevel@tonic-gate 
30370Sstevel@tonic-gate /*
30380Sstevel@tonic-gate  * Return processor_info(2) state as a string.
30390Sstevel@tonic-gate  */
30400Sstevel@tonic-gate const char *
30410Sstevel@tonic-gate cpu_get_state_str(cpu_t *cpu)
30420Sstevel@tonic-gate {
30430Sstevel@tonic-gate 	const char *string;
30440Sstevel@tonic-gate 
30450Sstevel@tonic-gate 	switch (cpu_get_state(cpu)) {
30460Sstevel@tonic-gate 	case P_ONLINE:
30470Sstevel@tonic-gate 		string = PS_ONLINE;
30480Sstevel@tonic-gate 		break;
30490Sstevel@tonic-gate 	case P_POWEROFF:
30500Sstevel@tonic-gate 		string = PS_POWEROFF;
30510Sstevel@tonic-gate 		break;
30520Sstevel@tonic-gate 	case P_NOINTR:
30530Sstevel@tonic-gate 		string = PS_NOINTR;
30540Sstevel@tonic-gate 		break;
30550Sstevel@tonic-gate 	case P_SPARE:
30560Sstevel@tonic-gate 		string = PS_SPARE;
30570Sstevel@tonic-gate 		break;
30580Sstevel@tonic-gate 	case P_FAULTED:
30590Sstevel@tonic-gate 		string = PS_FAULTED;
30600Sstevel@tonic-gate 		break;
30610Sstevel@tonic-gate 	case P_OFFLINE:
30620Sstevel@tonic-gate 		string = PS_OFFLINE;
30630Sstevel@tonic-gate 		break;
30640Sstevel@tonic-gate 	default:
30650Sstevel@tonic-gate 		string = "unknown";
30660Sstevel@tonic-gate 		break;
30670Sstevel@tonic-gate 	}
30680Sstevel@tonic-gate 	return (string);
30690Sstevel@tonic-gate }
30700Sstevel@tonic-gate 
30710Sstevel@tonic-gate /*
30720Sstevel@tonic-gate  * Export this CPU's statistics (cpu_stat_t and cpu_stats_t) as raw and named
30730Sstevel@tonic-gate  * kstats, respectively.  This is done when a CPU is initialized or placed
30740Sstevel@tonic-gate  * online via p_online(2).
30750Sstevel@tonic-gate  */
30760Sstevel@tonic-gate static void
30770Sstevel@tonic-gate cpu_stats_kstat_create(cpu_t *cp)
30780Sstevel@tonic-gate {
30790Sstevel@tonic-gate 	int 	instance = cp->cpu_id;
30800Sstevel@tonic-gate 	char 	*module = "cpu";
30810Sstevel@tonic-gate 	char 	*class = "misc";
30820Sstevel@tonic-gate 	kstat_t	*ksp;
30830Sstevel@tonic-gate 	zoneid_t zoneid;
30840Sstevel@tonic-gate 
30850Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
30860Sstevel@tonic-gate 
30870Sstevel@tonic-gate 	if (pool_pset_enabled())
30880Sstevel@tonic-gate 		zoneid = GLOBAL_ZONEID;
30890Sstevel@tonic-gate 	else
30900Sstevel@tonic-gate 		zoneid = ALL_ZONES;
30910Sstevel@tonic-gate 	/*
30920Sstevel@tonic-gate 	 * Create named kstats
30930Sstevel@tonic-gate 	 */
30940Sstevel@tonic-gate #define	CPU_STATS_KS_CREATE(name, tsize, update_func)                    \
30950Sstevel@tonic-gate 	ksp = kstat_create_zone(module, instance, (name), class,         \
30960Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED, (tsize) / sizeof (kstat_named_t), 0,       \
30970Sstevel@tonic-gate 	    zoneid);                                                     \
30980Sstevel@tonic-gate 	if (ksp != NULL) {                                               \
30990Sstevel@tonic-gate 		ksp->ks_private = cp;                                    \
31000Sstevel@tonic-gate 		ksp->ks_update = (update_func);                          \
31010Sstevel@tonic-gate 		kstat_install(ksp);                                      \
31020Sstevel@tonic-gate 	} else                                                           \
31030Sstevel@tonic-gate 		cmn_err(CE_WARN, "cpu: unable to create %s:%d:%s kstat", \
31040Sstevel@tonic-gate 		    module, instance, (name));
31050Sstevel@tonic-gate 
31060Sstevel@tonic-gate 	CPU_STATS_KS_CREATE("sys", sizeof (cpu_sys_stats_ks_data_template),
31070Sstevel@tonic-gate 	    cpu_sys_stats_ks_update);
31080Sstevel@tonic-gate 	CPU_STATS_KS_CREATE("vm", sizeof (cpu_vm_stats_ks_data_template),
31090Sstevel@tonic-gate 	    cpu_vm_stats_ks_update);
31100Sstevel@tonic-gate 
31110Sstevel@tonic-gate 	/*
31120Sstevel@tonic-gate 	 * Export the familiar cpu_stat_t KSTAT_TYPE_RAW kstat.
31130Sstevel@tonic-gate 	 */
31140Sstevel@tonic-gate 	ksp = kstat_create_zone("cpu_stat", cp->cpu_id, NULL,
31150Sstevel@tonic-gate 	    "misc", KSTAT_TYPE_RAW, sizeof (cpu_stat_t), 0, zoneid);
31160Sstevel@tonic-gate 	if (ksp != NULL) {
31170Sstevel@tonic-gate 		ksp->ks_update = cpu_stat_ks_update;
31180Sstevel@tonic-gate 		ksp->ks_private = cp;
31190Sstevel@tonic-gate 		kstat_install(ksp);
31200Sstevel@tonic-gate 	}
31210Sstevel@tonic-gate }
31220Sstevel@tonic-gate 
31230Sstevel@tonic-gate static void
31240Sstevel@tonic-gate cpu_stats_kstat_destroy(cpu_t *cp)
31250Sstevel@tonic-gate {
31260Sstevel@tonic-gate 	char ks_name[KSTAT_STRLEN];
31270Sstevel@tonic-gate 
31280Sstevel@tonic-gate 	(void) sprintf(ks_name, "cpu_stat%d", cp->cpu_id);
31290Sstevel@tonic-gate 	kstat_delete_byname("cpu_stat", cp->cpu_id, ks_name);
31300Sstevel@tonic-gate 
31310Sstevel@tonic-gate 	kstat_delete_byname("cpu", cp->cpu_id, "sys");
31320Sstevel@tonic-gate 	kstat_delete_byname("cpu", cp->cpu_id, "vm");
31330Sstevel@tonic-gate }
31340Sstevel@tonic-gate 
31350Sstevel@tonic-gate static int
31360Sstevel@tonic-gate cpu_sys_stats_ks_update(kstat_t *ksp, int rw)
31370Sstevel@tonic-gate {
31380Sstevel@tonic-gate 	cpu_t *cp = (cpu_t *)ksp->ks_private;
31390Sstevel@tonic-gate 	struct cpu_sys_stats_ks_data *csskd;
31400Sstevel@tonic-gate 	cpu_sys_stats_t *css;
3141590Sesolom 	hrtime_t msnsecs[NCMSTATES];
31420Sstevel@tonic-gate 	int	i;
31430Sstevel@tonic-gate 
31440Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
31450Sstevel@tonic-gate 		return (EACCES);
31460Sstevel@tonic-gate 
31470Sstevel@tonic-gate 	csskd = ksp->ks_data;
31480Sstevel@tonic-gate 	css = &cp->cpu_stats.sys;
31490Sstevel@tonic-gate 
3150590Sesolom 	/*
3151590Sesolom 	 * Read CPU mstate, but compare with the last values we
3152590Sesolom 	 * received to make sure that the returned kstats never
3153590Sesolom 	 * decrease.
3154590Sesolom 	 */
3155590Sesolom 
3156590Sesolom 	get_cpu_mstate(cp, msnsecs);
3157590Sesolom 	if (csskd->cpu_nsec_idle.value.ui64 > msnsecs[CMS_IDLE])
3158590Sesolom 		msnsecs[CMS_IDLE] = csskd->cpu_nsec_idle.value.ui64;
3159590Sesolom 	if (csskd->cpu_nsec_user.value.ui64 > msnsecs[CMS_USER])
3160590Sesolom 		msnsecs[CMS_USER] = csskd->cpu_nsec_user.value.ui64;
3161590Sesolom 	if (csskd->cpu_nsec_kernel.value.ui64 > msnsecs[CMS_SYSTEM])
3162590Sesolom 		msnsecs[CMS_SYSTEM] = csskd->cpu_nsec_kernel.value.ui64;
3163590Sesolom 
31640Sstevel@tonic-gate 	bcopy(&cpu_sys_stats_ks_data_template, ksp->ks_data,
31650Sstevel@tonic-gate 	    sizeof (cpu_sys_stats_ks_data_template));
3166590Sesolom 
31670Sstevel@tonic-gate 	csskd->cpu_ticks_wait.value.ui64 = 0;
31680Sstevel@tonic-gate 	csskd->wait_ticks_io.value.ui64 = 0;
3169590Sesolom 
3170590Sesolom 	csskd->cpu_nsec_idle.value.ui64 = msnsecs[CMS_IDLE];
3171590Sesolom 	csskd->cpu_nsec_user.value.ui64 = msnsecs[CMS_USER];
3172590Sesolom 	csskd->cpu_nsec_kernel.value.ui64 = msnsecs[CMS_SYSTEM];
31730Sstevel@tonic-gate 	csskd->cpu_ticks_idle.value.ui64 =
31740Sstevel@tonic-gate 	    NSEC_TO_TICK(csskd->cpu_nsec_idle.value.ui64);
31750Sstevel@tonic-gate 	csskd->cpu_ticks_user.value.ui64 =
31760Sstevel@tonic-gate 	    NSEC_TO_TICK(csskd->cpu_nsec_user.value.ui64);
31770Sstevel@tonic-gate 	csskd->cpu_ticks_kernel.value.ui64 =
31780Sstevel@tonic-gate 	    NSEC_TO_TICK(csskd->cpu_nsec_kernel.value.ui64);
31795076Smishra 	csskd->cpu_nsec_intr.value.ui64 = cp->cpu_intrlast;
31805076Smishra 	csskd->cpu_load_intr.value.ui64 = cp->cpu_intrload;
31810Sstevel@tonic-gate 	csskd->bread.value.ui64 = css->bread;
31820Sstevel@tonic-gate 	csskd->bwrite.value.ui64 = css->bwrite;
31830Sstevel@tonic-gate 	csskd->lread.value.ui64 = css->lread;
31840Sstevel@tonic-gate 	csskd->lwrite.value.ui64 = css->lwrite;
31850Sstevel@tonic-gate 	csskd->phread.value.ui64 = css->phread;
31860Sstevel@tonic-gate 	csskd->phwrite.value.ui64 = css->phwrite;
31870Sstevel@tonic-gate 	csskd->pswitch.value.ui64 = css->pswitch;
31880Sstevel@tonic-gate 	csskd->trap.value.ui64 = css->trap;
31890Sstevel@tonic-gate 	csskd->intr.value.ui64 = 0;
31900Sstevel@tonic-gate 	for (i = 0; i < PIL_MAX; i++)
31910Sstevel@tonic-gate 		csskd->intr.value.ui64 += css->intr[i];
31920Sstevel@tonic-gate 	csskd->syscall.value.ui64 = css->syscall;
31930Sstevel@tonic-gate 	csskd->sysread.value.ui64 = css->sysread;
31940Sstevel@tonic-gate 	csskd->syswrite.value.ui64 = css->syswrite;
31950Sstevel@tonic-gate 	csskd->sysfork.value.ui64 = css->sysfork;
31960Sstevel@tonic-gate 	csskd->sysvfork.value.ui64 = css->sysvfork;
31970Sstevel@tonic-gate 	csskd->sysexec.value.ui64 = css->sysexec;
31980Sstevel@tonic-gate 	csskd->readch.value.ui64 = css->readch;
31990Sstevel@tonic-gate 	csskd->writech.value.ui64 = css->writech;
32000Sstevel@tonic-gate 	csskd->rcvint.value.ui64 = css->rcvint;
32010Sstevel@tonic-gate 	csskd->xmtint.value.ui64 = css->xmtint;
32020Sstevel@tonic-gate 	csskd->mdmint.value.ui64 = css->mdmint;
32030Sstevel@tonic-gate 	csskd->rawch.value.ui64 = css->rawch;
32040Sstevel@tonic-gate 	csskd->canch.value.ui64 = css->canch;
32050Sstevel@tonic-gate 	csskd->outch.value.ui64 = css->outch;
32060Sstevel@tonic-gate 	csskd->msg.value.ui64 = css->msg;
32070Sstevel@tonic-gate 	csskd->sema.value.ui64 = css->sema;
32080Sstevel@tonic-gate 	csskd->namei.value.ui64 = css->namei;
32090Sstevel@tonic-gate 	csskd->ufsiget.value.ui64 = css->ufsiget;
32100Sstevel@tonic-gate 	csskd->ufsdirblk.value.ui64 = css->ufsdirblk;
32110Sstevel@tonic-gate 	csskd->ufsipage.value.ui64 = css->ufsipage;
32120Sstevel@tonic-gate 	csskd->ufsinopage.value.ui64 = css->ufsinopage;
32130Sstevel@tonic-gate 	csskd->procovf.value.ui64 = css->procovf;
32140Sstevel@tonic-gate 	csskd->intrthread.value.ui64 = 0;
32155552Sethindra 	for (i = 0; i < LOCK_LEVEL - 1; i++)
32160Sstevel@tonic-gate 		csskd->intrthread.value.ui64 += css->intr[i];
32170Sstevel@tonic-gate 	csskd->intrblk.value.ui64 = css->intrblk;
32180Sstevel@tonic-gate 	csskd->intrunpin.value.ui64 = css->intrunpin;
32190Sstevel@tonic-gate 	csskd->idlethread.value.ui64 = css->idlethread;
32200Sstevel@tonic-gate 	csskd->inv_swtch.value.ui64 = css->inv_swtch;
32210Sstevel@tonic-gate 	csskd->nthreads.value.ui64 = css->nthreads;
32220Sstevel@tonic-gate 	csskd->cpumigrate.value.ui64 = css->cpumigrate;
32230Sstevel@tonic-gate 	csskd->xcalls.value.ui64 = css->xcalls;
32240Sstevel@tonic-gate 	csskd->mutex_adenters.value.ui64 = css->mutex_adenters;
32250Sstevel@tonic-gate 	csskd->rw_rdfails.value.ui64 = css->rw_rdfails;
32260Sstevel@tonic-gate 	csskd->rw_wrfails.value.ui64 = css->rw_wrfails;
32270Sstevel@tonic-gate 	csskd->modload.value.ui64 = css->modload;
32280Sstevel@tonic-gate 	csskd->modunload.value.ui64 = css->modunload;
32290Sstevel@tonic-gate 	csskd->bawrite.value.ui64 = css->bawrite;
32303446Smrj 	csskd->iowait.value.ui64 = css->iowait;
32310Sstevel@tonic-gate 
32320Sstevel@tonic-gate 	return (0);
32330Sstevel@tonic-gate }
32340Sstevel@tonic-gate 
32350Sstevel@tonic-gate static int
32360Sstevel@tonic-gate cpu_vm_stats_ks_update(kstat_t *ksp, int rw)
32370Sstevel@tonic-gate {
32380Sstevel@tonic-gate 	cpu_t *cp = (cpu_t *)ksp->ks_private;
32390Sstevel@tonic-gate 	struct cpu_vm_stats_ks_data *cvskd;
32400Sstevel@tonic-gate 	cpu_vm_stats_t *cvs;
32410Sstevel@tonic-gate 
32420Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
32430Sstevel@tonic-gate 		return (EACCES);
32440Sstevel@tonic-gate 
32450Sstevel@tonic-gate 	cvs = &cp->cpu_stats.vm;
32460Sstevel@tonic-gate 	cvskd = ksp->ks_data;
32470Sstevel@tonic-gate 
32480Sstevel@tonic-gate 	bcopy(&cpu_vm_stats_ks_data_template, ksp->ks_data,
32490Sstevel@tonic-gate 	    sizeof (cpu_vm_stats_ks_data_template));
32500Sstevel@tonic-gate 	cvskd->pgrec.value.ui64 = cvs->pgrec;
32510Sstevel@tonic-gate 	cvskd->pgfrec.value.ui64 = cvs->pgfrec;
32520Sstevel@tonic-gate 	cvskd->pgin.value.ui64 = cvs->pgin;
32530Sstevel@tonic-gate 	cvskd->pgpgin.value.ui64 = cvs->pgpgin;
32540Sstevel@tonic-gate 	cvskd->pgout.value.ui64 = cvs->pgout;
32550Sstevel@tonic-gate 	cvskd->pgpgout.value.ui64 = cvs->pgpgout;
32560Sstevel@tonic-gate 	cvskd->swapin.value.ui64 = cvs->swapin;
32570Sstevel@tonic-gate 	cvskd->pgswapin.value.ui64 = cvs->pgswapin;
32580Sstevel@tonic-gate 	cvskd->swapout.value.ui64 = cvs->swapout;
32590Sstevel@tonic-gate 	cvskd->pgswapout.value.ui64 = cvs->pgswapout;
32600Sstevel@tonic-gate 	cvskd->zfod.value.ui64 = cvs->zfod;
32610Sstevel@tonic-gate 	cvskd->dfree.value.ui64 = cvs->dfree;
32620Sstevel@tonic-gate 	cvskd->scan.value.ui64 = cvs->scan;
32630Sstevel@tonic-gate 	cvskd->rev.value.ui64 = cvs->rev;
32640Sstevel@tonic-gate 	cvskd->hat_fault.value.ui64 = cvs->hat_fault;
32650Sstevel@tonic-gate 	cvskd->as_fault.value.ui64 = cvs->as_fault;
32660Sstevel@tonic-gate 	cvskd->maj_fault.value.ui64 = cvs->maj_fault;
32670Sstevel@tonic-gate 	cvskd->cow_fault.value.ui64 = cvs->cow_fault;
32680Sstevel@tonic-gate 	cvskd->prot_fault.value.ui64 = cvs->prot_fault;
32690Sstevel@tonic-gate 	cvskd->softlock.value.ui64 = cvs->softlock;
32700Sstevel@tonic-gate 	cvskd->kernel_asflt.value.ui64 = cvs->kernel_asflt;
32710Sstevel@tonic-gate 	cvskd->pgrrun.value.ui64 = cvs->pgrrun;
32720Sstevel@tonic-gate 	cvskd->execpgin.value.ui64 = cvs->execpgin;
32730Sstevel@tonic-gate 	cvskd->execpgout.value.ui64 = cvs->execpgout;
32740Sstevel@tonic-gate 	cvskd->execfree.value.ui64 = cvs->execfree;
32750Sstevel@tonic-gate 	cvskd->anonpgin.value.ui64 = cvs->anonpgin;
32760Sstevel@tonic-gate 	cvskd->anonpgout.value.ui64 = cvs->anonpgout;
32770Sstevel@tonic-gate 	cvskd->anonfree.value.ui64 = cvs->anonfree;
32780Sstevel@tonic-gate 	cvskd->fspgin.value.ui64 = cvs->fspgin;
32790Sstevel@tonic-gate 	cvskd->fspgout.value.ui64 = cvs->fspgout;
32800Sstevel@tonic-gate 	cvskd->fsfree.value.ui64 = cvs->fsfree;
32810Sstevel@tonic-gate 
32820Sstevel@tonic-gate 	return (0);
32830Sstevel@tonic-gate }
32840Sstevel@tonic-gate 
32850Sstevel@tonic-gate static int
32860Sstevel@tonic-gate cpu_stat_ks_update(kstat_t *ksp, int rw)
32870Sstevel@tonic-gate {
32880Sstevel@tonic-gate 	cpu_stat_t *cso;
32890Sstevel@tonic-gate 	cpu_t *cp;
32900Sstevel@tonic-gate 	int i;
3291590Sesolom 	hrtime_t msnsecs[NCMSTATES];
32920Sstevel@tonic-gate 
32930Sstevel@tonic-gate 	cso = (cpu_stat_t *)ksp->ks_data;
32940Sstevel@tonic-gate 	cp = (cpu_t *)ksp->ks_private;
32950Sstevel@tonic-gate 
32960Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
32970Sstevel@tonic-gate 		return (EACCES);
32980Sstevel@tonic-gate 
32990Sstevel@tonic-gate 	/*
3300590Sesolom 	 * Read CPU mstate, but compare with the last values we
3301590Sesolom 	 * received to make sure that the returned kstats never
3302590Sesolom 	 * decrease.
33030Sstevel@tonic-gate 	 */
3304590Sesolom 
3305590Sesolom 	get_cpu_mstate(cp, msnsecs);
3306590Sesolom 	msnsecs[CMS_IDLE] = NSEC_TO_TICK(msnsecs[CMS_IDLE]);
3307590Sesolom 	msnsecs[CMS_USER] = NSEC_TO_TICK(msnsecs[CMS_USER]);
3308590Sesolom 	msnsecs[CMS_SYSTEM] = NSEC_TO_TICK(msnsecs[CMS_SYSTEM]);
3309590Sesolom 	if (cso->cpu_sysinfo.cpu[CPU_IDLE] < msnsecs[CMS_IDLE])
3310590Sesolom 		cso->cpu_sysinfo.cpu[CPU_IDLE] = msnsecs[CMS_IDLE];
3311590Sesolom 	if (cso->cpu_sysinfo.cpu[CPU_USER] < msnsecs[CMS_USER])
3312590Sesolom 		cso->cpu_sysinfo.cpu[CPU_USER] = msnsecs[CMS_USER];
3313590Sesolom 	if (cso->cpu_sysinfo.cpu[CPU_KERNEL] < msnsecs[CMS_SYSTEM])
3314590Sesolom 		cso->cpu_sysinfo.cpu[CPU_KERNEL] = msnsecs[CMS_SYSTEM];
33150Sstevel@tonic-gate 	cso->cpu_sysinfo.cpu[CPU_WAIT] 	= 0;
33160Sstevel@tonic-gate 	cso->cpu_sysinfo.wait[W_IO] 	= 0;
33170Sstevel@tonic-gate 	cso->cpu_sysinfo.wait[W_SWAP]	= 0;
33180Sstevel@tonic-gate 	cso->cpu_sysinfo.wait[W_PIO]	= 0;
33190Sstevel@tonic-gate 	cso->cpu_sysinfo.bread 		= CPU_STATS(cp, sys.bread);
33200Sstevel@tonic-gate 	cso->cpu_sysinfo.bwrite 	= CPU_STATS(cp, sys.bwrite);
33210Sstevel@tonic-gate 	cso->cpu_sysinfo.lread 		= CPU_STATS(cp, sys.lread);
33220Sstevel@tonic-gate 	cso->cpu_sysinfo.lwrite 	= CPU_STATS(cp, sys.lwrite);
33230Sstevel@tonic-gate 	cso->cpu_sysinfo.phread 	= CPU_STATS(cp, sys.phread);
33240Sstevel@tonic-gate 	cso->cpu_sysinfo.phwrite 	= CPU_STATS(cp, sys.phwrite);
33250Sstevel@tonic-gate 	cso->cpu_sysinfo.pswitch 	= CPU_STATS(cp, sys.pswitch);
33260Sstevel@tonic-gate 	cso->cpu_sysinfo.trap 		= CPU_STATS(cp, sys.trap);
33270Sstevel@tonic-gate 	cso->cpu_sysinfo.intr		= 0;
33280Sstevel@tonic-gate 	for (i = 0; i < PIL_MAX; i++)
33290Sstevel@tonic-gate 		cso->cpu_sysinfo.intr += CPU_STATS(cp, sys.intr[i]);
33300Sstevel@tonic-gate 	cso->cpu_sysinfo.syscall	= CPU_STATS(cp, sys.syscall);
33310Sstevel@tonic-gate 	cso->cpu_sysinfo.sysread	= CPU_STATS(cp, sys.sysread);
33320Sstevel@tonic-gate 	cso->cpu_sysinfo.syswrite	= CPU_STATS(cp, sys.syswrite);
33330Sstevel@tonic-gate 	cso->cpu_sysinfo.sysfork	= CPU_STATS(cp, sys.sysfork);
33340Sstevel@tonic-gate 	cso->cpu_sysinfo.sysvfork	= CPU_STATS(cp, sys.sysvfork);
33350Sstevel@tonic-gate 	cso->cpu_sysinfo.sysexec	= CPU_STATS(cp, sys.sysexec);
33360Sstevel@tonic-gate 	cso->cpu_sysinfo.readch		= CPU_STATS(cp, sys.readch);
33370Sstevel@tonic-gate 	cso->cpu_sysinfo.writech	= CPU_STATS(cp, sys.writech);
33380Sstevel@tonic-gate 	cso->cpu_sysinfo.rcvint		= CPU_STATS(cp, sys.rcvint);
33390Sstevel@tonic-gate 	cso->cpu_sysinfo.xmtint		= CPU_STATS(cp, sys.xmtint);
33400Sstevel@tonic-gate 	cso->cpu_sysinfo.mdmint		= CPU_STATS(cp, sys.mdmint);
33410Sstevel@tonic-gate 	cso->cpu_sysinfo.rawch		= CPU_STATS(cp, sys.rawch);
33420Sstevel@tonic-gate 	cso->cpu_sysinfo.canch		= CPU_STATS(cp, sys.canch);
33430Sstevel@tonic-gate 	cso->cpu_sysinfo.outch		= CPU_STATS(cp, sys.outch);
33440Sstevel@tonic-gate 	cso->cpu_sysinfo.msg		= CPU_STATS(cp, sys.msg);
33450Sstevel@tonic-gate 	cso->cpu_sysinfo.sema		= CPU_STATS(cp, sys.sema);
33460Sstevel@tonic-gate 	cso->cpu_sysinfo.namei		= CPU_STATS(cp, sys.namei);
33470Sstevel@tonic-gate 	cso->cpu_sysinfo.ufsiget	= CPU_STATS(cp, sys.ufsiget);
33480Sstevel@tonic-gate 	cso->cpu_sysinfo.ufsdirblk	= CPU_STATS(cp, sys.ufsdirblk);
33490Sstevel@tonic-gate 	cso->cpu_sysinfo.ufsipage	= CPU_STATS(cp, sys.ufsipage);
33500Sstevel@tonic-gate 	cso->cpu_sysinfo.ufsinopage	= CPU_STATS(cp, sys.ufsinopage);
33510Sstevel@tonic-gate 	cso->cpu_sysinfo.inodeovf	= 0;
33520Sstevel@tonic-gate 	cso->cpu_sysinfo.fileovf	= 0;
33530Sstevel@tonic-gate 	cso->cpu_sysinfo.procovf	= CPU_STATS(cp, sys.procovf);
33540Sstevel@tonic-gate 	cso->cpu_sysinfo.intrthread	= 0;
33555552Sethindra 	for (i = 0; i < LOCK_LEVEL - 1; i++)
33560Sstevel@tonic-gate 		cso->cpu_sysinfo.intrthread += CPU_STATS(cp, sys.intr[i]);
33570Sstevel@tonic-gate 	cso->cpu_sysinfo.intrblk	= CPU_STATS(cp, sys.intrblk);
33580Sstevel@tonic-gate 	cso->cpu_sysinfo.idlethread	= CPU_STATS(cp, sys.idlethread);
33590Sstevel@tonic-gate 	cso->cpu_sysinfo.inv_swtch	= CPU_STATS(cp, sys.inv_swtch);
33600Sstevel@tonic-gate 	cso->cpu_sysinfo.nthreads	= CPU_STATS(cp, sys.nthreads);
33610Sstevel@tonic-gate 	cso->cpu_sysinfo.cpumigrate	= CPU_STATS(cp, sys.cpumigrate);
33620Sstevel@tonic-gate 	cso->cpu_sysinfo.xcalls		= CPU_STATS(cp, sys.xcalls);
33630Sstevel@tonic-gate 	cso->cpu_sysinfo.mutex_adenters	= CPU_STATS(cp, sys.mutex_adenters);
33640Sstevel@tonic-gate 	cso->cpu_sysinfo.rw_rdfails	= CPU_STATS(cp, sys.rw_rdfails);
33650Sstevel@tonic-gate 	cso->cpu_sysinfo.rw_wrfails	= CPU_STATS(cp, sys.rw_wrfails);
33660Sstevel@tonic-gate 	cso->cpu_sysinfo.modload	= CPU_STATS(cp, sys.modload);
33670Sstevel@tonic-gate 	cso->cpu_sysinfo.modunload	= CPU_STATS(cp, sys.modunload);
33680Sstevel@tonic-gate 	cso->cpu_sysinfo.bawrite	= CPU_STATS(cp, sys.bawrite);
33690Sstevel@tonic-gate 	cso->cpu_sysinfo.rw_enters	= 0;
33700Sstevel@tonic-gate 	cso->cpu_sysinfo.win_uo_cnt	= 0;
33710Sstevel@tonic-gate 	cso->cpu_sysinfo.win_uu_cnt	= 0;
33720Sstevel@tonic-gate 	cso->cpu_sysinfo.win_so_cnt	= 0;
33730Sstevel@tonic-gate 	cso->cpu_sysinfo.win_su_cnt	= 0;
33740Sstevel@tonic-gate 	cso->cpu_sysinfo.win_suo_cnt	= 0;
33750Sstevel@tonic-gate 
33763446Smrj 	cso->cpu_syswait.iowait		= CPU_STATS(cp, sys.iowait);
33770Sstevel@tonic-gate 	cso->cpu_syswait.swap		= 0;
33780Sstevel@tonic-gate 	cso->cpu_syswait.physio		= 0;
33790Sstevel@tonic-gate 
33800Sstevel@tonic-gate 	cso->cpu_vminfo.pgrec		= CPU_STATS(cp, vm.pgrec);
33810Sstevel@tonic-gate 	cso->cpu_vminfo.pgfrec		= CPU_STATS(cp, vm.pgfrec);
33820Sstevel@tonic-gate 	cso->cpu_vminfo.pgin		= CPU_STATS(cp, vm.pgin);
33830Sstevel@tonic-gate 	cso->cpu_vminfo.pgpgin		= CPU_STATS(cp, vm.pgpgin);
33840Sstevel@tonic-gate 	cso->cpu_vminfo.pgout		= CPU_STATS(cp, vm.pgout);
33850Sstevel@tonic-gate 	cso->cpu_vminfo.pgpgout		= CPU_STATS(cp, vm.pgpgout);
33860Sstevel@tonic-gate 	cso->cpu_vminfo.swapin		= CPU_STATS(cp, vm.swapin);
33870Sstevel@tonic-gate 	cso->cpu_vminfo.pgswapin	= CPU_STATS(cp, vm.pgswapin);
33880Sstevel@tonic-gate 	cso->cpu_vminfo.swapout		= CPU_STATS(cp, vm.swapout);
33890Sstevel@tonic-gate 	cso->cpu_vminfo.pgswapout	= CPU_STATS(cp, vm.pgswapout);
33900Sstevel@tonic-gate 	cso->cpu_vminfo.zfod		= CPU_STATS(cp, vm.zfod);
33910Sstevel@tonic-gate 	cso->cpu_vminfo.dfree		= CPU_STATS(cp, vm.dfree);
33920Sstevel@tonic-gate 	cso->cpu_vminfo.scan		= CPU_STATS(cp, vm.scan);
33930Sstevel@tonic-gate 	cso->cpu_vminfo.rev		= CPU_STATS(cp, vm.rev);
33940Sstevel@tonic-gate 	cso->cpu_vminfo.hat_fault	= CPU_STATS(cp, vm.hat_fault);
33950Sstevel@tonic-gate 	cso->cpu_vminfo.as_fault	= CPU_STATS(cp, vm.as_fault);
33960Sstevel@tonic-gate 	cso->cpu_vminfo.maj_fault	= CPU_STATS(cp, vm.maj_fault);
33970Sstevel@tonic-gate 	cso->cpu_vminfo.cow_fault	= CPU_STATS(cp, vm.cow_fault);
33980Sstevel@tonic-gate 	cso->cpu_vminfo.prot_fault	= CPU_STATS(cp, vm.prot_fault);
33990Sstevel@tonic-gate 	cso->cpu_vminfo.softlock	= CPU_STATS(cp, vm.softlock);
34000Sstevel@tonic-gate 	cso->cpu_vminfo.kernel_asflt	= CPU_STATS(cp, vm.kernel_asflt);
34010Sstevel@tonic-gate 	cso->cpu_vminfo.pgrrun		= CPU_STATS(cp, vm.pgrrun);
34020Sstevel@tonic-gate 	cso->cpu_vminfo.execpgin	= CPU_STATS(cp, vm.execpgin);
34030Sstevel@tonic-gate 	cso->cpu_vminfo.execpgout	= CPU_STATS(cp, vm.execpgout);
34040Sstevel@tonic-gate 	cso->cpu_vminfo.execfree	= CPU_STATS(cp, vm.execfree);
34050Sstevel@tonic-gate 	cso->cpu_vminfo.anonpgin	= CPU_STATS(cp, vm.anonpgin);
34060Sstevel@tonic-gate 	cso->cpu_vminfo.anonpgout	= CPU_STATS(cp, vm.anonpgout);
34070Sstevel@tonic-gate 	cso->cpu_vminfo.anonfree	= CPU_STATS(cp, vm.anonfree);
34080Sstevel@tonic-gate 	cso->cpu_vminfo.fspgin		= CPU_STATS(cp, vm.fspgin);
34090Sstevel@tonic-gate 	cso->cpu_vminfo.fspgout		= CPU_STATS(cp, vm.fspgout);
34100Sstevel@tonic-gate 	cso->cpu_vminfo.fsfree		= CPU_STATS(cp, vm.fsfree);
34110Sstevel@tonic-gate 
34120Sstevel@tonic-gate 	return (0);
34130Sstevel@tonic-gate }
3414