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 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 23*1228Sandrei * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * Architecture-independent CPU control functions. 310Sstevel@tonic-gate */ 320Sstevel@tonic-gate 330Sstevel@tonic-gate #include <sys/types.h> 340Sstevel@tonic-gate #include <sys/param.h> 350Sstevel@tonic-gate #include <sys/var.h> 360Sstevel@tonic-gate #include <sys/thread.h> 370Sstevel@tonic-gate #include <sys/cpuvar.h> 380Sstevel@tonic-gate #include <sys/kstat.h> 390Sstevel@tonic-gate #include <sys/uadmin.h> 400Sstevel@tonic-gate #include <sys/systm.h> 410Sstevel@tonic-gate #include <sys/errno.h> 420Sstevel@tonic-gate #include <sys/cmn_err.h> 430Sstevel@tonic-gate #include <sys/procset.h> 440Sstevel@tonic-gate #include <sys/processor.h> 450Sstevel@tonic-gate #include <sys/debug.h> 460Sstevel@tonic-gate #include <sys/cpupart.h> 470Sstevel@tonic-gate #include <sys/lgrp.h> 480Sstevel@tonic-gate #include <sys/pset.h> 490Sstevel@tonic-gate #include <sys/chip.h> 500Sstevel@tonic-gate #include <sys/kmem.h> 510Sstevel@tonic-gate #include <sys/kmem_impl.h> /* to set per-cpu kmem_cache offset */ 520Sstevel@tonic-gate #include <sys/atomic.h> 530Sstevel@tonic-gate #include <sys/callb.h> 540Sstevel@tonic-gate #include <sys/vtrace.h> 550Sstevel@tonic-gate #include <sys/cyclic.h> 560Sstevel@tonic-gate #include <sys/bitmap.h> 570Sstevel@tonic-gate #include <sys/nvpair.h> 580Sstevel@tonic-gate #include <sys/pool_pset.h> 590Sstevel@tonic-gate #include <sys/msacct.h> 600Sstevel@tonic-gate #include <sys/time.h> 610Sstevel@tonic-gate #include <sys/archsystm.h> 620Sstevel@tonic-gate 630Sstevel@tonic-gate extern int mp_cpu_start(cpu_t *); 640Sstevel@tonic-gate extern int mp_cpu_stop(cpu_t *); 650Sstevel@tonic-gate extern int mp_cpu_poweron(cpu_t *); 660Sstevel@tonic-gate extern int mp_cpu_poweroff(cpu_t *); 670Sstevel@tonic-gate extern int mp_cpu_configure(int); 680Sstevel@tonic-gate extern int mp_cpu_unconfigure(int); 690Sstevel@tonic-gate extern void mp_cpu_faulted_enter(cpu_t *); 700Sstevel@tonic-gate extern void mp_cpu_faulted_exit(cpu_t *); 710Sstevel@tonic-gate 720Sstevel@tonic-gate extern int cmp_cpu_to_chip(processorid_t cpuid); 730Sstevel@tonic-gate #ifdef __sparcv9 740Sstevel@tonic-gate extern char *cpu_fru_fmri(cpu_t *cp); 750Sstevel@tonic-gate #endif 760Sstevel@tonic-gate 770Sstevel@tonic-gate static void cpu_add_active_internal(cpu_t *cp); 780Sstevel@tonic-gate static void cpu_remove_active(cpu_t *cp); 790Sstevel@tonic-gate static void cpu_info_kstat_create(cpu_t *cp); 800Sstevel@tonic-gate static void cpu_info_kstat_destroy(cpu_t *cp); 810Sstevel@tonic-gate static void cpu_stats_kstat_create(cpu_t *cp); 820Sstevel@tonic-gate static void cpu_stats_kstat_destroy(cpu_t *cp); 830Sstevel@tonic-gate 840Sstevel@tonic-gate static int cpu_sys_stats_ks_update(kstat_t *ksp, int rw); 850Sstevel@tonic-gate static int cpu_vm_stats_ks_update(kstat_t *ksp, int rw); 860Sstevel@tonic-gate static int cpu_stat_ks_update(kstat_t *ksp, int rw); 870Sstevel@tonic-gate static int cpu_state_change_hooks(int, cpu_setup_t, cpu_setup_t); 880Sstevel@tonic-gate 890Sstevel@tonic-gate /* 900Sstevel@tonic-gate * cpu_lock protects ncpus, ncpus_online, cpu_flag, cpu_list, cpu_active, 910Sstevel@tonic-gate * and dispatch queue reallocations. The lock ordering with respect to 920Sstevel@tonic-gate * related locks is: 930Sstevel@tonic-gate * 940Sstevel@tonic-gate * cpu_lock --> thread_free_lock ---> p_lock ---> thread_lock() 950Sstevel@tonic-gate * 960Sstevel@tonic-gate * Warning: Certain sections of code do not use the cpu_lock when 970Sstevel@tonic-gate * traversing the cpu_list (e.g. mutex_vector_enter(), clock()). Since 980Sstevel@tonic-gate * all cpus are paused during modifications to this list, a solution 990Sstevel@tonic-gate * to protect the list is too either disable kernel preemption while 1000Sstevel@tonic-gate * walking the list, *or* recheck the cpu_next pointer at each 1010Sstevel@tonic-gate * iteration in the loop. Note that in no cases can any cached 1020Sstevel@tonic-gate * copies of the cpu pointers be kept as they may become invalid. 1030Sstevel@tonic-gate */ 1040Sstevel@tonic-gate kmutex_t cpu_lock; 1050Sstevel@tonic-gate cpu_t *cpu_list; /* list of all CPUs */ 1060Sstevel@tonic-gate cpu_t *cpu_active; /* list of active CPUs */ 1070Sstevel@tonic-gate static cpuset_t cpu_available; /* set of available CPUs */ 1080Sstevel@tonic-gate cpuset_t cpu_seqid_inuse; /* which cpu_seqids are in use */ 1090Sstevel@tonic-gate 1100Sstevel@tonic-gate /* 1110Sstevel@tonic-gate * max_ncpus keeps the max cpus the system can have. Initially 1120Sstevel@tonic-gate * it's NCPU, but since most archs scan the devtree for cpus 1130Sstevel@tonic-gate * fairly early on during boot, the real max can be known before 1140Sstevel@tonic-gate * ncpus is set (useful for early NCPU based allocations). 1150Sstevel@tonic-gate */ 1160Sstevel@tonic-gate int max_ncpus = NCPU; 1170Sstevel@tonic-gate /* 1180Sstevel@tonic-gate * platforms that set max_ncpus to maxiumum number of cpus that can be 1190Sstevel@tonic-gate * dynamically added will set boot_max_ncpus to the number of cpus found 1200Sstevel@tonic-gate * at device tree scan time during boot. 1210Sstevel@tonic-gate */ 1220Sstevel@tonic-gate int boot_max_ncpus = -1; 1230Sstevel@tonic-gate /* 1240Sstevel@tonic-gate * Maximum possible CPU id. This can never be >= NCPU since NCPU is 1250Sstevel@tonic-gate * used to size arrays that are indexed by CPU id. 1260Sstevel@tonic-gate */ 1270Sstevel@tonic-gate processorid_t max_cpuid = NCPU - 1; 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate int ncpus = 1; 1300Sstevel@tonic-gate int ncpus_online = 1; 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate /* 1330Sstevel@tonic-gate * CPU that we're trying to offline. Protected by cpu_lock. 1340Sstevel@tonic-gate */ 1350Sstevel@tonic-gate cpu_t *cpu_inmotion; 1360Sstevel@tonic-gate 1370Sstevel@tonic-gate /* 1380Sstevel@tonic-gate * Can be raised to suppress further weakbinding, which are instead 1390Sstevel@tonic-gate * satisfied by disabling preemption. Must be raised/lowered under cpu_lock, 1400Sstevel@tonic-gate * while individual thread weakbinding synchronisation is done under thread 1410Sstevel@tonic-gate * lock. 1420Sstevel@tonic-gate */ 1430Sstevel@tonic-gate int weakbindingbarrier; 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate /* 1460Sstevel@tonic-gate * values for safe_list. Pause state that CPUs are in. 1470Sstevel@tonic-gate */ 1480Sstevel@tonic-gate #define PAUSE_IDLE 0 /* normal state */ 1490Sstevel@tonic-gate #define PAUSE_READY 1 /* paused thread ready to spl */ 1500Sstevel@tonic-gate #define PAUSE_WAIT 2 /* paused thread is spl-ed high */ 1510Sstevel@tonic-gate #define PAUSE_DIE 3 /* tell pause thread to leave */ 1520Sstevel@tonic-gate #define PAUSE_DEAD 4 /* pause thread has left */ 1530Sstevel@tonic-gate 1540Sstevel@tonic-gate /* 1550Sstevel@tonic-gate * Variables used in pause_cpus(). 1560Sstevel@tonic-gate */ 1570Sstevel@tonic-gate static volatile char safe_list[NCPU]; 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate static struct _cpu_pause_info { 1600Sstevel@tonic-gate int cp_spl; /* spl saved in pause_cpus() */ 1610Sstevel@tonic-gate volatile int cp_go; /* Go signal sent after all ready */ 1620Sstevel@tonic-gate int cp_count; /* # of CPUs to pause */ 1630Sstevel@tonic-gate ksema_t cp_sem; /* synch pause_cpus & cpu_pause */ 1640Sstevel@tonic-gate kthread_id_t cp_paused; 1650Sstevel@tonic-gate } cpu_pause_info; 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate static kmutex_t pause_free_mutex; 1680Sstevel@tonic-gate static kcondvar_t pause_free_cv; 1690Sstevel@tonic-gate 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; 1780Sstevel@tonic-gate kstat_named_t wait_ticks_io; 1790Sstevel@tonic-gate kstat_named_t bread; 1800Sstevel@tonic-gate kstat_named_t bwrite; 1810Sstevel@tonic-gate kstat_named_t lread; 1820Sstevel@tonic-gate kstat_named_t lwrite; 1830Sstevel@tonic-gate kstat_named_t phread; 1840Sstevel@tonic-gate kstat_named_t phwrite; 1850Sstevel@tonic-gate kstat_named_t pswitch; 1860Sstevel@tonic-gate kstat_named_t trap; 1870Sstevel@tonic-gate kstat_named_t intr; 1880Sstevel@tonic-gate kstat_named_t syscall; 1890Sstevel@tonic-gate kstat_named_t sysread; 1900Sstevel@tonic-gate kstat_named_t syswrite; 1910Sstevel@tonic-gate kstat_named_t sysfork; 1920Sstevel@tonic-gate kstat_named_t sysvfork; 1930Sstevel@tonic-gate kstat_named_t sysexec; 1940Sstevel@tonic-gate kstat_named_t readch; 1950Sstevel@tonic-gate kstat_named_t writech; 1960Sstevel@tonic-gate kstat_named_t rcvint; 1970Sstevel@tonic-gate kstat_named_t xmtint; 1980Sstevel@tonic-gate kstat_named_t mdmint; 1990Sstevel@tonic-gate kstat_named_t rawch; 2000Sstevel@tonic-gate kstat_named_t canch; 2010Sstevel@tonic-gate kstat_named_t outch; 2020Sstevel@tonic-gate kstat_named_t msg; 2030Sstevel@tonic-gate kstat_named_t sema; 2040Sstevel@tonic-gate kstat_named_t namei; 2050Sstevel@tonic-gate kstat_named_t ufsiget; 2060Sstevel@tonic-gate kstat_named_t ufsdirblk; 2070Sstevel@tonic-gate kstat_named_t ufsipage; 2080Sstevel@tonic-gate kstat_named_t ufsinopage; 2090Sstevel@tonic-gate kstat_named_t procovf; 2100Sstevel@tonic-gate kstat_named_t intrthread; 2110Sstevel@tonic-gate kstat_named_t intrblk; 2120Sstevel@tonic-gate kstat_named_t intrunpin; 2130Sstevel@tonic-gate kstat_named_t idlethread; 2140Sstevel@tonic-gate kstat_named_t inv_swtch; 2150Sstevel@tonic-gate kstat_named_t nthreads; 2160Sstevel@tonic-gate kstat_named_t cpumigrate; 2170Sstevel@tonic-gate kstat_named_t xcalls; 2180Sstevel@tonic-gate kstat_named_t mutex_adenters; 2190Sstevel@tonic-gate kstat_named_t rw_rdfails; 2200Sstevel@tonic-gate kstat_named_t rw_wrfails; 2210Sstevel@tonic-gate kstat_named_t modload; 2220Sstevel@tonic-gate kstat_named_t modunload; 2230Sstevel@tonic-gate kstat_named_t bawrite; 2240Sstevel@tonic-gate kstat_named_t iowait; 2250Sstevel@tonic-gate } cpu_sys_stats_ks_data_template = { 2260Sstevel@tonic-gate { "cpu_ticks_idle", KSTAT_DATA_UINT64 }, 2270Sstevel@tonic-gate { "cpu_ticks_user", KSTAT_DATA_UINT64 }, 2280Sstevel@tonic-gate { "cpu_ticks_kernel", KSTAT_DATA_UINT64 }, 2290Sstevel@tonic-gate { "cpu_ticks_wait", KSTAT_DATA_UINT64 }, 2300Sstevel@tonic-gate { "cpu_nsec_idle", KSTAT_DATA_UINT64 }, 2310Sstevel@tonic-gate { "cpu_nsec_user", KSTAT_DATA_UINT64 }, 2320Sstevel@tonic-gate { "cpu_nsec_kernel", KSTAT_DATA_UINT64 }, 2330Sstevel@tonic-gate { "wait_ticks_io", KSTAT_DATA_UINT64 }, 2340Sstevel@tonic-gate { "bread", KSTAT_DATA_UINT64 }, 2350Sstevel@tonic-gate { "bwrite", KSTAT_DATA_UINT64 }, 2360Sstevel@tonic-gate { "lread", KSTAT_DATA_UINT64 }, 2370Sstevel@tonic-gate { "lwrite", KSTAT_DATA_UINT64 }, 2380Sstevel@tonic-gate { "phread", KSTAT_DATA_UINT64 }, 2390Sstevel@tonic-gate { "phwrite", KSTAT_DATA_UINT64 }, 2400Sstevel@tonic-gate { "pswitch", KSTAT_DATA_UINT64 }, 2410Sstevel@tonic-gate { "trap", KSTAT_DATA_UINT64 }, 2420Sstevel@tonic-gate { "intr", KSTAT_DATA_UINT64 }, 2430Sstevel@tonic-gate { "syscall", KSTAT_DATA_UINT64 }, 2440Sstevel@tonic-gate { "sysread", KSTAT_DATA_UINT64 }, 2450Sstevel@tonic-gate { "syswrite", KSTAT_DATA_UINT64 }, 2460Sstevel@tonic-gate { "sysfork", KSTAT_DATA_UINT64 }, 2470Sstevel@tonic-gate { "sysvfork", KSTAT_DATA_UINT64 }, 2480Sstevel@tonic-gate { "sysexec", KSTAT_DATA_UINT64 }, 2490Sstevel@tonic-gate { "readch", KSTAT_DATA_UINT64 }, 2500Sstevel@tonic-gate { "writech", KSTAT_DATA_UINT64 }, 2510Sstevel@tonic-gate { "rcvint", KSTAT_DATA_UINT64 }, 2520Sstevel@tonic-gate { "xmtint", KSTAT_DATA_UINT64 }, 2530Sstevel@tonic-gate { "mdmint", KSTAT_DATA_UINT64 }, 2540Sstevel@tonic-gate { "rawch", KSTAT_DATA_UINT64 }, 2550Sstevel@tonic-gate { "canch", KSTAT_DATA_UINT64 }, 2560Sstevel@tonic-gate { "outch", KSTAT_DATA_UINT64 }, 2570Sstevel@tonic-gate { "msg", KSTAT_DATA_UINT64 }, 2580Sstevel@tonic-gate { "sema", KSTAT_DATA_UINT64 }, 2590Sstevel@tonic-gate { "namei", KSTAT_DATA_UINT64 }, 2600Sstevel@tonic-gate { "ufsiget", KSTAT_DATA_UINT64 }, 2610Sstevel@tonic-gate { "ufsdirblk", KSTAT_DATA_UINT64 }, 2620Sstevel@tonic-gate { "ufsipage", KSTAT_DATA_UINT64 }, 2630Sstevel@tonic-gate { "ufsinopage", KSTAT_DATA_UINT64 }, 2640Sstevel@tonic-gate { "procovf", KSTAT_DATA_UINT64 }, 2650Sstevel@tonic-gate { "intrthread", KSTAT_DATA_UINT64 }, 2660Sstevel@tonic-gate { "intrblk", KSTAT_DATA_UINT64 }, 2670Sstevel@tonic-gate { "intrunpin", KSTAT_DATA_UINT64 }, 2680Sstevel@tonic-gate { "idlethread", KSTAT_DATA_UINT64 }, 2690Sstevel@tonic-gate { "inv_swtch", KSTAT_DATA_UINT64 }, 2700Sstevel@tonic-gate { "nthreads", KSTAT_DATA_UINT64 }, 2710Sstevel@tonic-gate { "cpumigrate", KSTAT_DATA_UINT64 }, 2720Sstevel@tonic-gate { "xcalls", KSTAT_DATA_UINT64 }, 2730Sstevel@tonic-gate { "mutex_adenters", KSTAT_DATA_UINT64 }, 2740Sstevel@tonic-gate { "rw_rdfails", KSTAT_DATA_UINT64 }, 2750Sstevel@tonic-gate { "rw_wrfails", KSTAT_DATA_UINT64 }, 2760Sstevel@tonic-gate { "modload", KSTAT_DATA_UINT64 }, 2770Sstevel@tonic-gate { "modunload", KSTAT_DATA_UINT64 }, 2780Sstevel@tonic-gate { "bawrite", KSTAT_DATA_UINT64 }, 2790Sstevel@tonic-gate { "iowait", KSTAT_DATA_UINT64 }, 2800Sstevel@tonic-gate }; 2810Sstevel@tonic-gate 2820Sstevel@tonic-gate static struct cpu_vm_stats_ks_data { 2830Sstevel@tonic-gate kstat_named_t pgrec; 2840Sstevel@tonic-gate kstat_named_t pgfrec; 2850Sstevel@tonic-gate kstat_named_t pgin; 2860Sstevel@tonic-gate kstat_named_t pgpgin; 2870Sstevel@tonic-gate kstat_named_t pgout; 2880Sstevel@tonic-gate kstat_named_t pgpgout; 2890Sstevel@tonic-gate kstat_named_t swapin; 2900Sstevel@tonic-gate kstat_named_t pgswapin; 2910Sstevel@tonic-gate kstat_named_t swapout; 2920Sstevel@tonic-gate kstat_named_t pgswapout; 2930Sstevel@tonic-gate kstat_named_t zfod; 2940Sstevel@tonic-gate kstat_named_t dfree; 2950Sstevel@tonic-gate kstat_named_t scan; 2960Sstevel@tonic-gate kstat_named_t rev; 2970Sstevel@tonic-gate kstat_named_t hat_fault; 2980Sstevel@tonic-gate kstat_named_t as_fault; 2990Sstevel@tonic-gate kstat_named_t maj_fault; 3000Sstevel@tonic-gate kstat_named_t cow_fault; 3010Sstevel@tonic-gate kstat_named_t prot_fault; 3020Sstevel@tonic-gate kstat_named_t softlock; 3030Sstevel@tonic-gate kstat_named_t kernel_asflt; 3040Sstevel@tonic-gate kstat_named_t pgrrun; 3050Sstevel@tonic-gate kstat_named_t execpgin; 3060Sstevel@tonic-gate kstat_named_t execpgout; 3070Sstevel@tonic-gate kstat_named_t execfree; 3080Sstevel@tonic-gate kstat_named_t anonpgin; 3090Sstevel@tonic-gate kstat_named_t anonpgout; 3100Sstevel@tonic-gate kstat_named_t anonfree; 3110Sstevel@tonic-gate kstat_named_t fspgin; 3120Sstevel@tonic-gate kstat_named_t fspgout; 3130Sstevel@tonic-gate kstat_named_t fsfree; 3140Sstevel@tonic-gate } cpu_vm_stats_ks_data_template = { 3150Sstevel@tonic-gate { "pgrec", KSTAT_DATA_UINT64 }, 3160Sstevel@tonic-gate { "pgfrec", KSTAT_DATA_UINT64 }, 3170Sstevel@tonic-gate { "pgin", KSTAT_DATA_UINT64 }, 3180Sstevel@tonic-gate { "pgpgin", KSTAT_DATA_UINT64 }, 3190Sstevel@tonic-gate { "pgout", KSTAT_DATA_UINT64 }, 3200Sstevel@tonic-gate { "pgpgout", KSTAT_DATA_UINT64 }, 3210Sstevel@tonic-gate { "swapin", KSTAT_DATA_UINT64 }, 3220Sstevel@tonic-gate { "pgswapin", KSTAT_DATA_UINT64 }, 3230Sstevel@tonic-gate { "swapout", KSTAT_DATA_UINT64 }, 3240Sstevel@tonic-gate { "pgswapout", KSTAT_DATA_UINT64 }, 3250Sstevel@tonic-gate { "zfod", KSTAT_DATA_UINT64 }, 3260Sstevel@tonic-gate { "dfree", KSTAT_DATA_UINT64 }, 3270Sstevel@tonic-gate { "scan", KSTAT_DATA_UINT64 }, 3280Sstevel@tonic-gate { "rev", KSTAT_DATA_UINT64 }, 3290Sstevel@tonic-gate { "hat_fault", KSTAT_DATA_UINT64 }, 3300Sstevel@tonic-gate { "as_fault", KSTAT_DATA_UINT64 }, 3310Sstevel@tonic-gate { "maj_fault", KSTAT_DATA_UINT64 }, 3320Sstevel@tonic-gate { "cow_fault", KSTAT_DATA_UINT64 }, 3330Sstevel@tonic-gate { "prot_fault", KSTAT_DATA_UINT64 }, 3340Sstevel@tonic-gate { "softlock", KSTAT_DATA_UINT64 }, 3350Sstevel@tonic-gate { "kernel_asflt", KSTAT_DATA_UINT64 }, 3360Sstevel@tonic-gate { "pgrrun", KSTAT_DATA_UINT64 }, 3370Sstevel@tonic-gate { "execpgin", KSTAT_DATA_UINT64 }, 3380Sstevel@tonic-gate { "execpgout", KSTAT_DATA_UINT64 }, 3390Sstevel@tonic-gate { "execfree", KSTAT_DATA_UINT64 }, 3400Sstevel@tonic-gate { "anonpgin", KSTAT_DATA_UINT64 }, 3410Sstevel@tonic-gate { "anonpgout", KSTAT_DATA_UINT64 }, 3420Sstevel@tonic-gate { "anonfree", KSTAT_DATA_UINT64 }, 3430Sstevel@tonic-gate { "fspgin", KSTAT_DATA_UINT64 }, 3440Sstevel@tonic-gate { "fspgout", KSTAT_DATA_UINT64 }, 3450Sstevel@tonic-gate { "fsfree", KSTAT_DATA_UINT64 }, 3460Sstevel@tonic-gate }; 3470Sstevel@tonic-gate 3480Sstevel@tonic-gate /* 3490Sstevel@tonic-gate * Force the specified thread to migrate to the appropriate processor. 3500Sstevel@tonic-gate * Called with thread lock held, returns with it dropped. 3510Sstevel@tonic-gate */ 3520Sstevel@tonic-gate static void 3530Sstevel@tonic-gate force_thread_migrate(kthread_id_t tp) 3540Sstevel@tonic-gate { 3550Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(tp)); 3560Sstevel@tonic-gate if (tp == curthread) { 3570Sstevel@tonic-gate THREAD_TRANSITION(tp); 3580Sstevel@tonic-gate CL_SETRUN(tp); 3590Sstevel@tonic-gate thread_unlock_nopreempt(tp); 3600Sstevel@tonic-gate swtch(); 3610Sstevel@tonic-gate } else { 3620Sstevel@tonic-gate if (tp->t_state == TS_ONPROC) { 3630Sstevel@tonic-gate cpu_surrender(tp); 3640Sstevel@tonic-gate } else if (tp->t_state == TS_RUN) { 3650Sstevel@tonic-gate (void) dispdeq(tp); 3660Sstevel@tonic-gate setbackdq(tp); 3670Sstevel@tonic-gate } 3680Sstevel@tonic-gate thread_unlock(tp); 3690Sstevel@tonic-gate } 3700Sstevel@tonic-gate } 3710Sstevel@tonic-gate 3720Sstevel@tonic-gate /* 3730Sstevel@tonic-gate * Set affinity for a specified CPU. 3740Sstevel@tonic-gate * A reference count is incremented and the affinity is held until the 3750Sstevel@tonic-gate * reference count is decremented to zero by thread_affinity_clear(). 3760Sstevel@tonic-gate * This is so regions of code requiring affinity can be nested. 3770Sstevel@tonic-gate * Caller needs to ensure that cpu_id remains valid, which can be 3780Sstevel@tonic-gate * done by holding cpu_lock across this call, unless the caller 3790Sstevel@tonic-gate * specifies CPU_CURRENT in which case the cpu_lock will be acquired 3800Sstevel@tonic-gate * by thread_affinity_set and CPU->cpu_id will be the target CPU. 3810Sstevel@tonic-gate */ 3820Sstevel@tonic-gate void 3830Sstevel@tonic-gate thread_affinity_set(kthread_id_t t, int cpu_id) 3840Sstevel@tonic-gate { 3850Sstevel@tonic-gate cpu_t *cp; 3860Sstevel@tonic-gate int c; 3870Sstevel@tonic-gate 3880Sstevel@tonic-gate ASSERT(!(t == curthread && t->t_weakbound_cpu != NULL)); 3890Sstevel@tonic-gate 3900Sstevel@tonic-gate if ((c = cpu_id) == CPU_CURRENT) { 3910Sstevel@tonic-gate mutex_enter(&cpu_lock); 3920Sstevel@tonic-gate cpu_id = CPU->cpu_id; 3930Sstevel@tonic-gate } 3940Sstevel@tonic-gate /* 3950Sstevel@tonic-gate * We should be asserting that cpu_lock is held here, but 3960Sstevel@tonic-gate * the NCA code doesn't acquire it. The following assert 3970Sstevel@tonic-gate * should be uncommented when the NCA code is fixed. 3980Sstevel@tonic-gate * 3990Sstevel@tonic-gate * ASSERT(MUTEX_HELD(&cpu_lock)); 4000Sstevel@tonic-gate */ 4010Sstevel@tonic-gate ASSERT((cpu_id >= 0) && (cpu_id < NCPU)); 4020Sstevel@tonic-gate cp = cpu[cpu_id]; 4030Sstevel@tonic-gate ASSERT(cp != NULL); /* user must provide a good cpu_id */ 4040Sstevel@tonic-gate /* 4050Sstevel@tonic-gate * If there is already a hard affinity requested, and this affinity 4060Sstevel@tonic-gate * conflicts with that, panic. 4070Sstevel@tonic-gate */ 4080Sstevel@tonic-gate thread_lock(t); 4090Sstevel@tonic-gate if (t->t_affinitycnt > 0 && t->t_bound_cpu != cp) { 4100Sstevel@tonic-gate panic("affinity_set: setting %p but already bound to %p", 4110Sstevel@tonic-gate (void *)cp, (void *)t->t_bound_cpu); 4120Sstevel@tonic-gate } 4130Sstevel@tonic-gate t->t_affinitycnt++; 4140Sstevel@tonic-gate t->t_bound_cpu = cp; 4150Sstevel@tonic-gate 4160Sstevel@tonic-gate /* 4170Sstevel@tonic-gate * Make sure we're running on the right CPU. 4180Sstevel@tonic-gate */ 4190Sstevel@tonic-gate if (cp != t->t_cpu || t != curthread) { 4200Sstevel@tonic-gate force_thread_migrate(t); /* drops thread lock */ 4210Sstevel@tonic-gate } else { 4220Sstevel@tonic-gate thread_unlock(t); 4230Sstevel@tonic-gate } 4240Sstevel@tonic-gate 4250Sstevel@tonic-gate if (c == CPU_CURRENT) 4260Sstevel@tonic-gate mutex_exit(&cpu_lock); 4270Sstevel@tonic-gate } 4280Sstevel@tonic-gate 4290Sstevel@tonic-gate /* 4300Sstevel@tonic-gate * Wrapper for backward compatibility. 4310Sstevel@tonic-gate */ 4320Sstevel@tonic-gate void 4330Sstevel@tonic-gate affinity_set(int cpu_id) 4340Sstevel@tonic-gate { 4350Sstevel@tonic-gate thread_affinity_set(curthread, cpu_id); 4360Sstevel@tonic-gate } 4370Sstevel@tonic-gate 4380Sstevel@tonic-gate /* 4390Sstevel@tonic-gate * Decrement the affinity reservation count and if it becomes zero, 4400Sstevel@tonic-gate * clear the CPU affinity for the current thread, or set it to the user's 4410Sstevel@tonic-gate * software binding request. 4420Sstevel@tonic-gate */ 4430Sstevel@tonic-gate void 4440Sstevel@tonic-gate thread_affinity_clear(kthread_id_t t) 4450Sstevel@tonic-gate { 4460Sstevel@tonic-gate register processorid_t binding; 4470Sstevel@tonic-gate 4480Sstevel@tonic-gate thread_lock(t); 4490Sstevel@tonic-gate if (--t->t_affinitycnt == 0) { 4500Sstevel@tonic-gate if ((binding = t->t_bind_cpu) == PBIND_NONE) { 4510Sstevel@tonic-gate /* 4520Sstevel@tonic-gate * Adjust disp_max_unbound_pri if necessary. 4530Sstevel@tonic-gate */ 4540Sstevel@tonic-gate disp_adjust_unbound_pri(t); 4550Sstevel@tonic-gate t->t_bound_cpu = NULL; 4560Sstevel@tonic-gate if (t->t_cpu->cpu_part != t->t_cpupart) { 4570Sstevel@tonic-gate force_thread_migrate(t); 4580Sstevel@tonic-gate return; 4590Sstevel@tonic-gate } 4600Sstevel@tonic-gate } else { 4610Sstevel@tonic-gate t->t_bound_cpu = cpu[binding]; 4620Sstevel@tonic-gate /* 4630Sstevel@tonic-gate * Make sure the thread is running on the bound CPU. 4640Sstevel@tonic-gate */ 4650Sstevel@tonic-gate if (t->t_cpu != t->t_bound_cpu) { 4660Sstevel@tonic-gate force_thread_migrate(t); 4670Sstevel@tonic-gate return; /* already dropped lock */ 4680Sstevel@tonic-gate } 4690Sstevel@tonic-gate } 4700Sstevel@tonic-gate } 4710Sstevel@tonic-gate thread_unlock(t); 4720Sstevel@tonic-gate } 4730Sstevel@tonic-gate 4740Sstevel@tonic-gate /* 4750Sstevel@tonic-gate * Wrapper for backward compatibility. 4760Sstevel@tonic-gate */ 4770Sstevel@tonic-gate void 4780Sstevel@tonic-gate affinity_clear(void) 4790Sstevel@tonic-gate { 4800Sstevel@tonic-gate thread_affinity_clear(curthread); 4810Sstevel@tonic-gate } 4820Sstevel@tonic-gate 4830Sstevel@tonic-gate /* 4840Sstevel@tonic-gate * Weak cpu affinity. Bind to the "current" cpu for short periods 4850Sstevel@tonic-gate * of time during which the thread must not block (but may be preempted). 4860Sstevel@tonic-gate * Use this instead of kpreempt_disable() when it is only "no migration" 4870Sstevel@tonic-gate * rather than "no preemption" semantics that are required - disabling 4880Sstevel@tonic-gate * preemption holds higher priority threads off of cpu and if the 4890Sstevel@tonic-gate * operation that is protected is more than momentary this is not good 4900Sstevel@tonic-gate * for realtime etc. 4910Sstevel@tonic-gate * 4920Sstevel@tonic-gate * Weakly bound threads will not prevent a cpu from being offlined - 4930Sstevel@tonic-gate * we'll only run them on the cpu to which they are weakly bound but 4940Sstevel@tonic-gate * (because they do not block) we'll always be able to move them on to 4950Sstevel@tonic-gate * another cpu at offline time if we give them just a short moment to 4960Sstevel@tonic-gate * run during which they will unbind. To give a cpu a chance of offlining, 4970Sstevel@tonic-gate * however, we require a barrier to weak bindings that may be raised for a 4980Sstevel@tonic-gate * given cpu (offline/move code may set this and then wait a short time for 4990Sstevel@tonic-gate * existing weak bindings to drop); the cpu_inmotion pointer is that barrier. 5000Sstevel@tonic-gate * 5010Sstevel@tonic-gate * There are few restrictions on the calling context of thread_nomigrate. 5020Sstevel@tonic-gate * The caller must not hold the thread lock. Calls may be nested. 5030Sstevel@tonic-gate * 5040Sstevel@tonic-gate * After weakbinding a thread must not perform actions that may block. 5050Sstevel@tonic-gate * In particular it must not call thread_affinity_set; calling that when 5060Sstevel@tonic-gate * already weakbound is nonsensical anyway. 5070Sstevel@tonic-gate * 5080Sstevel@tonic-gate * If curthread is prevented from migrating for other reasons 5090Sstevel@tonic-gate * (kernel preemption disabled; high pil; strongly bound; interrupt thread) 5100Sstevel@tonic-gate * then the weak binding will succeed even if this cpu is the target of an 5110Sstevel@tonic-gate * offline/move request. 5120Sstevel@tonic-gate */ 5130Sstevel@tonic-gate void 5140Sstevel@tonic-gate thread_nomigrate(void) 5150Sstevel@tonic-gate { 5160Sstevel@tonic-gate cpu_t *cp; 5170Sstevel@tonic-gate kthread_id_t t = curthread; 5180Sstevel@tonic-gate 5190Sstevel@tonic-gate again: 5200Sstevel@tonic-gate kpreempt_disable(); 5210Sstevel@tonic-gate cp = CPU; 5220Sstevel@tonic-gate 5230Sstevel@tonic-gate /* 5240Sstevel@tonic-gate * A highlevel interrupt must not modify t_nomigrate or 5250Sstevel@tonic-gate * t_weakbound_cpu of the thread it has interrupted. A lowlevel 5260Sstevel@tonic-gate * interrupt thread cannot migrate and we can avoid the 5270Sstevel@tonic-gate * thread_lock call below by short-circuiting here. In either 5280Sstevel@tonic-gate * case we can just return since no migration is possible and 5290Sstevel@tonic-gate * the condition will persist (ie, when we test for these again 5300Sstevel@tonic-gate * in thread_allowmigrate they can't have changed). Migration 5310Sstevel@tonic-gate * is also impossible if we're at or above DISP_LEVEL pil. 5320Sstevel@tonic-gate */ 5330Sstevel@tonic-gate if (CPU_ON_INTR(cp) || t->t_flag & T_INTR_THREAD || 5340Sstevel@tonic-gate getpil() >= DISP_LEVEL) { 5350Sstevel@tonic-gate kpreempt_enable(); 5360Sstevel@tonic-gate return; 5370Sstevel@tonic-gate } 5380Sstevel@tonic-gate 5390Sstevel@tonic-gate /* 5400Sstevel@tonic-gate * We must be consistent with existing weak bindings. Since we 5410Sstevel@tonic-gate * may be interrupted between the increment of t_nomigrate and 5420Sstevel@tonic-gate * the store to t_weakbound_cpu below we cannot assume that 5430Sstevel@tonic-gate * t_weakbound_cpu will be set if t_nomigrate is. Note that we 5440Sstevel@tonic-gate * cannot assert t_weakbound_cpu == t_bind_cpu since that is not 5450Sstevel@tonic-gate * always the case. 5460Sstevel@tonic-gate */ 5470Sstevel@tonic-gate if (t->t_nomigrate && t->t_weakbound_cpu && t->t_weakbound_cpu != cp) { 5480Sstevel@tonic-gate if (!panicstr) 5490Sstevel@tonic-gate panic("thread_nomigrate: binding to %p but already " 5500Sstevel@tonic-gate "bound to %p", (void *)cp, 5510Sstevel@tonic-gate (void *)t->t_weakbound_cpu); 5520Sstevel@tonic-gate } 5530Sstevel@tonic-gate 5540Sstevel@tonic-gate /* 5550Sstevel@tonic-gate * At this point we have preemption disabled and we don't yet hold 5560Sstevel@tonic-gate * the thread lock. So it's possible that somebody else could 5570Sstevel@tonic-gate * set t_bind_cpu here and not be able to force us across to the 5580Sstevel@tonic-gate * new cpu (since we have preemption disabled). 5590Sstevel@tonic-gate */ 5600Sstevel@tonic-gate thread_lock(curthread); 5610Sstevel@tonic-gate 5620Sstevel@tonic-gate /* 5630Sstevel@tonic-gate * If further weak bindings are being (temporarily) suppressed then 5640Sstevel@tonic-gate * we'll settle for disabling kernel preemption (which assures 5650Sstevel@tonic-gate * no migration provided the thread does not block which it is 5660Sstevel@tonic-gate * not allowed to if using thread_nomigrate). We must remember 5670Sstevel@tonic-gate * this disposition so we can take appropriate action in 5680Sstevel@tonic-gate * thread_allowmigrate. If this is a nested call and the 5690Sstevel@tonic-gate * thread is already weakbound then fall through as normal. 5700Sstevel@tonic-gate * We remember the decision to settle for kpreempt_disable through 5710Sstevel@tonic-gate * negative nesting counting in t_nomigrate. Once a thread has had one 5720Sstevel@tonic-gate * weakbinding request satisfied in this way any further (nested) 5730Sstevel@tonic-gate * requests will continue to be satisfied in the same way, 5740Sstevel@tonic-gate * even if weak bindings have recommenced. 5750Sstevel@tonic-gate */ 5760Sstevel@tonic-gate if (t->t_nomigrate < 0 || weakbindingbarrier && t->t_nomigrate == 0) { 5770Sstevel@tonic-gate --t->t_nomigrate; 5780Sstevel@tonic-gate thread_unlock(curthread); 5790Sstevel@tonic-gate return; /* with kpreempt_disable still active */ 5800Sstevel@tonic-gate } 5810Sstevel@tonic-gate 5820Sstevel@tonic-gate /* 5830Sstevel@tonic-gate * We hold thread_lock so t_bind_cpu cannot change. We could, 5840Sstevel@tonic-gate * however, be running on a different cpu to which we are t_bound_cpu 5850Sstevel@tonic-gate * to (as explained above). If we grant the weak binding request 5860Sstevel@tonic-gate * in that case then the dispatcher must favour our weak binding 5870Sstevel@tonic-gate * over our strong (in which case, just as when preemption is 5880Sstevel@tonic-gate * disabled, we can continue to run on a cpu other than the one to 5890Sstevel@tonic-gate * which we are strongbound; the difference in this case is that 5900Sstevel@tonic-gate * this thread can be preempted and so can appear on the dispatch 5910Sstevel@tonic-gate * queues of a cpu other than the one it is strongbound to). 5920Sstevel@tonic-gate * 5930Sstevel@tonic-gate * If the cpu we are running on does not appear to be a current 5940Sstevel@tonic-gate * offline target (we check cpu_inmotion to determine this - since 5950Sstevel@tonic-gate * we don't hold cpu_lock we may not see a recent store to that, 5960Sstevel@tonic-gate * so it's possible that we at times can grant a weak binding to a 5970Sstevel@tonic-gate * cpu that is an offline target, but that one request will not 5980Sstevel@tonic-gate * prevent the offline from succeeding) then we will always grant 5990Sstevel@tonic-gate * the weak binding request. This includes the case above where 6000Sstevel@tonic-gate * we grant a weakbinding not commensurate with our strong binding. 6010Sstevel@tonic-gate * 6020Sstevel@tonic-gate * If our cpu does appear to be an offline target then we're inclined 6030Sstevel@tonic-gate * not to grant the weakbinding request just yet - we'd prefer to 6040Sstevel@tonic-gate * migrate to another cpu and grant the request there. The 6050Sstevel@tonic-gate * exceptions are those cases where going through preemption code 6060Sstevel@tonic-gate * will not result in us changing cpu: 6070Sstevel@tonic-gate * 6080Sstevel@tonic-gate * . interrupts have already bypassed this case (see above) 6090Sstevel@tonic-gate * . we are already weakbound to this cpu (dispatcher code will 6100Sstevel@tonic-gate * always return us to the weakbound cpu) 6110Sstevel@tonic-gate * . preemption was disabled even before we disabled it above 6120Sstevel@tonic-gate * . we are strongbound to this cpu (if we're strongbound to 6130Sstevel@tonic-gate * another and not yet running there the trip through the 6140Sstevel@tonic-gate * dispatcher will move us to the strongbound cpu and we 6150Sstevel@tonic-gate * will grant the weak binding there) 6160Sstevel@tonic-gate */ 6170Sstevel@tonic-gate if (cp != cpu_inmotion || t->t_nomigrate > 0 || t->t_preempt > 1 || 6180Sstevel@tonic-gate t->t_bound_cpu == cp) { 6190Sstevel@tonic-gate /* 6200Sstevel@tonic-gate * Don't be tempted to store to t_weakbound_cpu only on 6210Sstevel@tonic-gate * the first nested bind request - if we're interrupted 6220Sstevel@tonic-gate * after the increment of t_nomigrate and before the 6230Sstevel@tonic-gate * store to t_weakbound_cpu and the interrupt calls 6240Sstevel@tonic-gate * thread_nomigrate then the assertion in thread_allowmigrate 6250Sstevel@tonic-gate * would fail. 6260Sstevel@tonic-gate */ 6270Sstevel@tonic-gate t->t_nomigrate++; 6280Sstevel@tonic-gate t->t_weakbound_cpu = cp; 6290Sstevel@tonic-gate membar_producer(); 6300Sstevel@tonic-gate thread_unlock(curthread); 6310Sstevel@tonic-gate /* 6320Sstevel@tonic-gate * Now that we have dropped the thread_lock another thread 6330Sstevel@tonic-gate * can set our t_weakbound_cpu, and will try to migrate us 6340Sstevel@tonic-gate * to the strongbound cpu (which will not be prevented by 6350Sstevel@tonic-gate * preemption being disabled since we're about to enable 6360Sstevel@tonic-gate * preemption). We have granted the weakbinding to the current 6370Sstevel@tonic-gate * cpu, so again we are in the position that is is is possible 6380Sstevel@tonic-gate * that our weak and strong bindings differ. Again this 6390Sstevel@tonic-gate * is catered for by dispatcher code which will favour our 6400Sstevel@tonic-gate * weak binding. 6410Sstevel@tonic-gate */ 6420Sstevel@tonic-gate kpreempt_enable(); 6430Sstevel@tonic-gate } else { 6440Sstevel@tonic-gate /* 6450Sstevel@tonic-gate * Move to another cpu before granting the request by 6460Sstevel@tonic-gate * forcing this thread through preemption code. When we 6470Sstevel@tonic-gate * get to set{front,back}dq called from CL_PREEMPT() 6480Sstevel@tonic-gate * cpu_choose() will be used to select a cpu to queue 6490Sstevel@tonic-gate * us on - that will see cpu_inmotion and take 6500Sstevel@tonic-gate * steps to avoid returning us to this cpu. 6510Sstevel@tonic-gate */ 6520Sstevel@tonic-gate cp->cpu_kprunrun = 1; 6530Sstevel@tonic-gate thread_unlock(curthread); 6540Sstevel@tonic-gate kpreempt_enable(); /* will call preempt() */ 6550Sstevel@tonic-gate goto again; 6560Sstevel@tonic-gate } 6570Sstevel@tonic-gate } 6580Sstevel@tonic-gate 6590Sstevel@tonic-gate void 6600Sstevel@tonic-gate thread_allowmigrate(void) 6610Sstevel@tonic-gate { 6620Sstevel@tonic-gate kthread_id_t t = curthread; 6630Sstevel@tonic-gate 6640Sstevel@tonic-gate ASSERT(t->t_weakbound_cpu == CPU || 6650Sstevel@tonic-gate (t->t_nomigrate < 0 && t->t_preempt > 0) || 6660Sstevel@tonic-gate CPU_ON_INTR(CPU) || t->t_flag & T_INTR_THREAD || 6670Sstevel@tonic-gate getpil() >= DISP_LEVEL); 6680Sstevel@tonic-gate 6690Sstevel@tonic-gate if (CPU_ON_INTR(CPU) || (t->t_flag & T_INTR_THREAD) || 6700Sstevel@tonic-gate getpil() >= DISP_LEVEL) 6710Sstevel@tonic-gate return; 6720Sstevel@tonic-gate 6730Sstevel@tonic-gate if (t->t_nomigrate < 0) { 6740Sstevel@tonic-gate /* 6750Sstevel@tonic-gate * This thread was granted "weak binding" in the 6760Sstevel@tonic-gate * stronger form of kernel preemption disabling. 6770Sstevel@tonic-gate * Undo a level of nesting for both t_nomigrate 6780Sstevel@tonic-gate * and t_preempt. 6790Sstevel@tonic-gate */ 6800Sstevel@tonic-gate ++t->t_nomigrate; 6810Sstevel@tonic-gate kpreempt_enable(); 6820Sstevel@tonic-gate } else if (--t->t_nomigrate == 0) { 6830Sstevel@tonic-gate /* 6840Sstevel@tonic-gate * Time to drop the weak binding. We need to cater 6850Sstevel@tonic-gate * for the case where we're weakbound to a different 6860Sstevel@tonic-gate * cpu than that to which we're strongbound (a very 6870Sstevel@tonic-gate * temporary arrangement that must only persist until 6880Sstevel@tonic-gate * weak binding drops). We don't acquire thread_lock 6890Sstevel@tonic-gate * here so even as this code executes t_bound_cpu 6900Sstevel@tonic-gate * may be changing. So we disable preemption and 6910Sstevel@tonic-gate * a) in the case that t_bound_cpu changes while we 6920Sstevel@tonic-gate * have preemption disabled kprunrun will be set 6930Sstevel@tonic-gate * asynchronously, and b) if before disabling 6940Sstevel@tonic-gate * preemption we were already on a different cpu to 6950Sstevel@tonic-gate * our t_bound_cpu then we set kprunrun ourselves 6960Sstevel@tonic-gate * to force a trip through the dispatcher when 6970Sstevel@tonic-gate * preemption is enabled. 6980Sstevel@tonic-gate */ 6990Sstevel@tonic-gate kpreempt_disable(); 7000Sstevel@tonic-gate if (t->t_bound_cpu && 7010Sstevel@tonic-gate t->t_weakbound_cpu != t->t_bound_cpu) 7020Sstevel@tonic-gate CPU->cpu_kprunrun = 1; 7030Sstevel@tonic-gate t->t_weakbound_cpu = NULL; 7040Sstevel@tonic-gate membar_producer(); 7050Sstevel@tonic-gate kpreempt_enable(); 7060Sstevel@tonic-gate } 7070Sstevel@tonic-gate } 7080Sstevel@tonic-gate 7090Sstevel@tonic-gate /* 7100Sstevel@tonic-gate * weakbinding_stop can be used to temporarily cause weakbindings made 7110Sstevel@tonic-gate * with thread_nomigrate to be satisfied through the stronger action of 7120Sstevel@tonic-gate * kpreempt_disable. weakbinding_start recommences normal weakbinding. 7130Sstevel@tonic-gate */ 7140Sstevel@tonic-gate 7150Sstevel@tonic-gate void 7160Sstevel@tonic-gate weakbinding_stop(void) 7170Sstevel@tonic-gate { 7180Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 7190Sstevel@tonic-gate weakbindingbarrier = 1; 7200Sstevel@tonic-gate membar_producer(); /* make visible before subsequent thread_lock */ 7210Sstevel@tonic-gate } 7220Sstevel@tonic-gate 7230Sstevel@tonic-gate void 7240Sstevel@tonic-gate weakbinding_start(void) 7250Sstevel@tonic-gate { 7260Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 7270Sstevel@tonic-gate weakbindingbarrier = 0; 7280Sstevel@tonic-gate } 7290Sstevel@tonic-gate 7300Sstevel@tonic-gate /* 7310Sstevel@tonic-gate * This routine is called to place the CPUs in a safe place so that 7320Sstevel@tonic-gate * one of them can be taken off line or placed on line. What we are 7330Sstevel@tonic-gate * trying to do here is prevent a thread from traversing the list 7340Sstevel@tonic-gate * of active CPUs while we are changing it or from getting placed on 7350Sstevel@tonic-gate * the run queue of a CPU that has just gone off line. We do this by 7360Sstevel@tonic-gate * creating a thread with the highest possible prio for each CPU and 7370Sstevel@tonic-gate * having it call this routine. The advantage of this method is that 7380Sstevel@tonic-gate * we can eliminate all checks for CPU_ACTIVE in the disp routines. 7390Sstevel@tonic-gate * This makes disp faster at the expense of making p_online() slower 7400Sstevel@tonic-gate * which is a good trade off. 7410Sstevel@tonic-gate */ 7420Sstevel@tonic-gate static void 7430Sstevel@tonic-gate cpu_pause(volatile char *safe) 7440Sstevel@tonic-gate { 7450Sstevel@tonic-gate int s; 7460Sstevel@tonic-gate struct _cpu_pause_info *cpi = &cpu_pause_info; 7470Sstevel@tonic-gate 7480Sstevel@tonic-gate ASSERT((curthread->t_bound_cpu != NULL) || (*safe == PAUSE_DIE)); 7490Sstevel@tonic-gate 7500Sstevel@tonic-gate while (*safe != PAUSE_DIE) { 7510Sstevel@tonic-gate *safe = PAUSE_READY; 7520Sstevel@tonic-gate membar_enter(); /* make sure stores are flushed */ 7530Sstevel@tonic-gate sema_v(&cpi->cp_sem); /* signal requesting thread */ 7540Sstevel@tonic-gate 7550Sstevel@tonic-gate /* 7560Sstevel@tonic-gate * Wait here until all pause threads are running. That 7570Sstevel@tonic-gate * indicates that it's safe to do the spl. Until 7580Sstevel@tonic-gate * cpu_pause_info.cp_go is set, we don't want to spl 7590Sstevel@tonic-gate * because that might block clock interrupts needed 7600Sstevel@tonic-gate * to preempt threads on other CPUs. 7610Sstevel@tonic-gate */ 7620Sstevel@tonic-gate while (cpi->cp_go == 0) 7630Sstevel@tonic-gate ; 7640Sstevel@tonic-gate /* 7650Sstevel@tonic-gate * Even though we are at the highest disp prio, we need 7660Sstevel@tonic-gate * to block out all interrupts below LOCK_LEVEL so that 7670Sstevel@tonic-gate * an intr doesn't come in, wake up a thread, and call 7680Sstevel@tonic-gate * setbackdq/setfrontdq. 7690Sstevel@tonic-gate */ 7700Sstevel@tonic-gate s = splhigh(); 7710Sstevel@tonic-gate /* 7720Sstevel@tonic-gate * This cpu is now safe. 7730Sstevel@tonic-gate */ 7740Sstevel@tonic-gate *safe = PAUSE_WAIT; 7750Sstevel@tonic-gate membar_enter(); /* make sure stores are flushed */ 7760Sstevel@tonic-gate 7770Sstevel@tonic-gate /* 7780Sstevel@tonic-gate * Now we wait. When we are allowed to continue, safe will 7790Sstevel@tonic-gate * be set to PAUSE_IDLE. 7800Sstevel@tonic-gate */ 7810Sstevel@tonic-gate while (*safe != PAUSE_IDLE) 7820Sstevel@tonic-gate ; 7830Sstevel@tonic-gate 7840Sstevel@tonic-gate splx(s); 7850Sstevel@tonic-gate /* 7860Sstevel@tonic-gate * Waiting is at an end. Switch out of cpu_pause 7870Sstevel@tonic-gate * loop and resume useful work. 7880Sstevel@tonic-gate */ 7890Sstevel@tonic-gate swtch(); 7900Sstevel@tonic-gate } 7910Sstevel@tonic-gate 7920Sstevel@tonic-gate mutex_enter(&pause_free_mutex); 7930Sstevel@tonic-gate *safe = PAUSE_DEAD; 7940Sstevel@tonic-gate cv_broadcast(&pause_free_cv); 7950Sstevel@tonic-gate mutex_exit(&pause_free_mutex); 7960Sstevel@tonic-gate } 7970Sstevel@tonic-gate 7980Sstevel@tonic-gate /* 7990Sstevel@tonic-gate * Allow the cpus to start running again. 8000Sstevel@tonic-gate */ 8010Sstevel@tonic-gate void 8020Sstevel@tonic-gate start_cpus() 8030Sstevel@tonic-gate { 8040Sstevel@tonic-gate int i; 8050Sstevel@tonic-gate 8060Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 8070Sstevel@tonic-gate ASSERT(cpu_pause_info.cp_paused); 8080Sstevel@tonic-gate cpu_pause_info.cp_paused = NULL; 8090Sstevel@tonic-gate for (i = 0; i < NCPU; i++) 8100Sstevel@tonic-gate safe_list[i] = PAUSE_IDLE; 8110Sstevel@tonic-gate membar_enter(); /* make sure stores are flushed */ 8120Sstevel@tonic-gate affinity_clear(); 8130Sstevel@tonic-gate splx(cpu_pause_info.cp_spl); 8140Sstevel@tonic-gate kpreempt_enable(); 8150Sstevel@tonic-gate } 8160Sstevel@tonic-gate 8170Sstevel@tonic-gate /* 8180Sstevel@tonic-gate * Allocate a pause thread for a CPU. 8190Sstevel@tonic-gate */ 8200Sstevel@tonic-gate static void 8210Sstevel@tonic-gate cpu_pause_alloc(cpu_t *cp) 8220Sstevel@tonic-gate { 8230Sstevel@tonic-gate kthread_id_t t; 8240Sstevel@tonic-gate int cpun = cp->cpu_id; 8250Sstevel@tonic-gate 8260Sstevel@tonic-gate /* 8270Sstevel@tonic-gate * Note, v.v_nglobpris will not change value as long as I hold 8280Sstevel@tonic-gate * cpu_lock. 8290Sstevel@tonic-gate */ 8300Sstevel@tonic-gate t = thread_create(NULL, 0, cpu_pause, (caddr_t)&safe_list[cpun], 8310Sstevel@tonic-gate 0, &p0, TS_STOPPED, v.v_nglobpris - 1); 8320Sstevel@tonic-gate thread_lock(t); 8330Sstevel@tonic-gate t->t_bound_cpu = cp; 8340Sstevel@tonic-gate t->t_disp_queue = cp->cpu_disp; 8350Sstevel@tonic-gate t->t_affinitycnt = 1; 8360Sstevel@tonic-gate t->t_preempt = 1; 8370Sstevel@tonic-gate thread_unlock(t); 8380Sstevel@tonic-gate cp->cpu_pause_thread = t; 8390Sstevel@tonic-gate /* 8400Sstevel@tonic-gate * Registering a thread in the callback table is usually done 8410Sstevel@tonic-gate * in the initialization code of the thread. In this 8420Sstevel@tonic-gate * case, we do it right after thread creation because the 8430Sstevel@tonic-gate * thread itself may never run, and we need to register the 8440Sstevel@tonic-gate * fact that it is safe for cpr suspend. 8450Sstevel@tonic-gate */ 8460Sstevel@tonic-gate CALLB_CPR_INIT_SAFE(t, "cpu_pause"); 8470Sstevel@tonic-gate } 8480Sstevel@tonic-gate 8490Sstevel@tonic-gate /* 8500Sstevel@tonic-gate * Free a pause thread for a CPU. 8510Sstevel@tonic-gate */ 8520Sstevel@tonic-gate static void 8530Sstevel@tonic-gate cpu_pause_free(cpu_t *cp) 8540Sstevel@tonic-gate { 8550Sstevel@tonic-gate kthread_id_t t; 8560Sstevel@tonic-gate int cpun = cp->cpu_id; 8570Sstevel@tonic-gate 8580Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 8590Sstevel@tonic-gate /* 8600Sstevel@tonic-gate * We have to get the thread and tell him to die. 8610Sstevel@tonic-gate */ 8620Sstevel@tonic-gate if ((t = cp->cpu_pause_thread) == NULL) { 8630Sstevel@tonic-gate ASSERT(safe_list[cpun] == PAUSE_IDLE); 8640Sstevel@tonic-gate return; 8650Sstevel@tonic-gate } 8660Sstevel@tonic-gate thread_lock(t); 8670Sstevel@tonic-gate t->t_cpu = CPU; /* disp gets upset if last cpu is quiesced. */ 8680Sstevel@tonic-gate t->t_bound_cpu = NULL; /* Must un-bind; cpu may not be running. */ 8690Sstevel@tonic-gate t->t_pri = v.v_nglobpris - 1; 8700Sstevel@tonic-gate ASSERT(safe_list[cpun] == PAUSE_IDLE); 8710Sstevel@tonic-gate safe_list[cpun] = PAUSE_DIE; 8720Sstevel@tonic-gate THREAD_TRANSITION(t); 8730Sstevel@tonic-gate setbackdq(t); 8740Sstevel@tonic-gate thread_unlock_nopreempt(t); 8750Sstevel@tonic-gate 8760Sstevel@tonic-gate /* 8770Sstevel@tonic-gate * If we don't wait for the thread to actually die, it may try to 8780Sstevel@tonic-gate * run on the wrong cpu as part of an actual call to pause_cpus(). 8790Sstevel@tonic-gate */ 8800Sstevel@tonic-gate mutex_enter(&pause_free_mutex); 8810Sstevel@tonic-gate while (safe_list[cpun] != PAUSE_DEAD) { 8820Sstevel@tonic-gate cv_wait(&pause_free_cv, &pause_free_mutex); 8830Sstevel@tonic-gate } 8840Sstevel@tonic-gate mutex_exit(&pause_free_mutex); 8850Sstevel@tonic-gate safe_list[cpun] = PAUSE_IDLE; 8860Sstevel@tonic-gate 8870Sstevel@tonic-gate cp->cpu_pause_thread = NULL; 8880Sstevel@tonic-gate } 8890Sstevel@tonic-gate 8900Sstevel@tonic-gate /* 8910Sstevel@tonic-gate * Initialize basic structures for pausing CPUs. 8920Sstevel@tonic-gate */ 8930Sstevel@tonic-gate void 8940Sstevel@tonic-gate cpu_pause_init() 8950Sstevel@tonic-gate { 8960Sstevel@tonic-gate sema_init(&cpu_pause_info.cp_sem, 0, NULL, SEMA_DEFAULT, NULL); 8970Sstevel@tonic-gate /* 8980Sstevel@tonic-gate * Create initial CPU pause thread. 8990Sstevel@tonic-gate */ 9000Sstevel@tonic-gate cpu_pause_alloc(CPU); 9010Sstevel@tonic-gate } 9020Sstevel@tonic-gate 9030Sstevel@tonic-gate /* 9040Sstevel@tonic-gate * Start the threads used to pause another CPU. 9050Sstevel@tonic-gate */ 9060Sstevel@tonic-gate static int 9070Sstevel@tonic-gate cpu_pause_start(processorid_t cpu_id) 9080Sstevel@tonic-gate { 9090Sstevel@tonic-gate int i; 9100Sstevel@tonic-gate int cpu_count = 0; 9110Sstevel@tonic-gate 9120Sstevel@tonic-gate for (i = 0; i < NCPU; i++) { 9130Sstevel@tonic-gate cpu_t *cp; 9140Sstevel@tonic-gate kthread_id_t t; 9150Sstevel@tonic-gate 9160Sstevel@tonic-gate cp = cpu[i]; 9170Sstevel@tonic-gate if (!CPU_IN_SET(cpu_available, i) || (i == cpu_id)) { 9180Sstevel@tonic-gate safe_list[i] = PAUSE_WAIT; 9190Sstevel@tonic-gate continue; 9200Sstevel@tonic-gate } 9210Sstevel@tonic-gate 9220Sstevel@tonic-gate /* 9230Sstevel@tonic-gate * Skip CPU if it is quiesced or not yet started. 9240Sstevel@tonic-gate */ 9250Sstevel@tonic-gate if ((cp->cpu_flags & (CPU_QUIESCED | CPU_READY)) != CPU_READY) { 9260Sstevel@tonic-gate safe_list[i] = PAUSE_WAIT; 9270Sstevel@tonic-gate continue; 9280Sstevel@tonic-gate } 9290Sstevel@tonic-gate 9300Sstevel@tonic-gate /* 9310Sstevel@tonic-gate * Start this CPU's pause thread. 9320Sstevel@tonic-gate */ 9330Sstevel@tonic-gate t = cp->cpu_pause_thread; 9340Sstevel@tonic-gate thread_lock(t); 9350Sstevel@tonic-gate /* 9360Sstevel@tonic-gate * Reset the priority, since nglobpris may have 9370Sstevel@tonic-gate * changed since the thread was created, if someone 9380Sstevel@tonic-gate * has loaded the RT (or some other) scheduling 9390Sstevel@tonic-gate * class. 9400Sstevel@tonic-gate */ 9410Sstevel@tonic-gate t->t_pri = v.v_nglobpris - 1; 9420Sstevel@tonic-gate THREAD_TRANSITION(t); 9430Sstevel@tonic-gate setbackdq(t); 9440Sstevel@tonic-gate thread_unlock_nopreempt(t); 9450Sstevel@tonic-gate ++cpu_count; 9460Sstevel@tonic-gate } 9470Sstevel@tonic-gate return (cpu_count); 9480Sstevel@tonic-gate } 9490Sstevel@tonic-gate 9500Sstevel@tonic-gate 9510Sstevel@tonic-gate /* 9520Sstevel@tonic-gate * Pause all of the CPUs except the one we are on by creating a high 9530Sstevel@tonic-gate * priority thread bound to those CPUs. 9540Sstevel@tonic-gate * 9550Sstevel@tonic-gate * Note that one must be extremely careful regarding code 9560Sstevel@tonic-gate * executed while CPUs are paused. Since a CPU may be paused 9570Sstevel@tonic-gate * while a thread scheduling on that CPU is holding an adaptive 9580Sstevel@tonic-gate * lock, code executed with CPUs paused must not acquire adaptive 9590Sstevel@tonic-gate * (or low-level spin) locks. Also, such code must not block, 9600Sstevel@tonic-gate * since the thread that is supposed to initiate the wakeup may 9610Sstevel@tonic-gate * never run. 9620Sstevel@tonic-gate * 9630Sstevel@tonic-gate * With a few exceptions, the restrictions on code executed with CPUs 9640Sstevel@tonic-gate * paused match those for code executed at high-level interrupt 9650Sstevel@tonic-gate * context. 9660Sstevel@tonic-gate */ 9670Sstevel@tonic-gate void 9680Sstevel@tonic-gate pause_cpus(cpu_t *off_cp) 9690Sstevel@tonic-gate { 9700Sstevel@tonic-gate processorid_t cpu_id; 9710Sstevel@tonic-gate int i; 9720Sstevel@tonic-gate struct _cpu_pause_info *cpi = &cpu_pause_info; 9730Sstevel@tonic-gate 9740Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 9750Sstevel@tonic-gate ASSERT(cpi->cp_paused == NULL); 9760Sstevel@tonic-gate cpi->cp_count = 0; 9770Sstevel@tonic-gate cpi->cp_go = 0; 9780Sstevel@tonic-gate for (i = 0; i < NCPU; i++) 9790Sstevel@tonic-gate safe_list[i] = PAUSE_IDLE; 9800Sstevel@tonic-gate kpreempt_disable(); 9810Sstevel@tonic-gate 9820Sstevel@tonic-gate /* 9830Sstevel@tonic-gate * If running on the cpu that is going offline, get off it. 9840Sstevel@tonic-gate * This is so that it won't be necessary to rechoose a CPU 9850Sstevel@tonic-gate * when done. 9860Sstevel@tonic-gate */ 9870Sstevel@tonic-gate if (CPU == off_cp) 9880Sstevel@tonic-gate cpu_id = off_cp->cpu_next_part->cpu_id; 9890Sstevel@tonic-gate else 9900Sstevel@tonic-gate cpu_id = CPU->cpu_id; 9910Sstevel@tonic-gate affinity_set(cpu_id); 9920Sstevel@tonic-gate 9930Sstevel@tonic-gate /* 9940Sstevel@tonic-gate * Start the pause threads and record how many were started 9950Sstevel@tonic-gate */ 9960Sstevel@tonic-gate cpi->cp_count = cpu_pause_start(cpu_id); 9970Sstevel@tonic-gate 9980Sstevel@tonic-gate /* 9990Sstevel@tonic-gate * Now wait for all CPUs to be running the pause thread. 10000Sstevel@tonic-gate */ 10010Sstevel@tonic-gate while (cpi->cp_count > 0) { 10020Sstevel@tonic-gate /* 10030Sstevel@tonic-gate * Spin reading the count without grabbing the disp 10040Sstevel@tonic-gate * lock to make sure we don't prevent the pause 10050Sstevel@tonic-gate * threads from getting the lock. 10060Sstevel@tonic-gate */ 10070Sstevel@tonic-gate while (sema_held(&cpi->cp_sem)) 10080Sstevel@tonic-gate ; 10090Sstevel@tonic-gate if (sema_tryp(&cpi->cp_sem)) 10100Sstevel@tonic-gate --cpi->cp_count; 10110Sstevel@tonic-gate } 10120Sstevel@tonic-gate cpi->cp_go = 1; /* all have reached cpu_pause */ 10130Sstevel@tonic-gate 10140Sstevel@tonic-gate /* 10150Sstevel@tonic-gate * Now wait for all CPUs to spl. (Transition from PAUSE_READY 10160Sstevel@tonic-gate * to PAUSE_WAIT.) 10170Sstevel@tonic-gate */ 10180Sstevel@tonic-gate for (i = 0; i < NCPU; i++) { 10190Sstevel@tonic-gate while (safe_list[i] != PAUSE_WAIT) 10200Sstevel@tonic-gate ; 10210Sstevel@tonic-gate } 10220Sstevel@tonic-gate cpi->cp_spl = splhigh(); /* block dispatcher on this CPU */ 10230Sstevel@tonic-gate cpi->cp_paused = curthread; 10240Sstevel@tonic-gate } 10250Sstevel@tonic-gate 10260Sstevel@tonic-gate /* 10270Sstevel@tonic-gate * Check whether the current thread has CPUs paused 10280Sstevel@tonic-gate */ 10290Sstevel@tonic-gate int 10300Sstevel@tonic-gate cpus_paused(void) 10310Sstevel@tonic-gate { 10320Sstevel@tonic-gate if (cpu_pause_info.cp_paused != NULL) { 10330Sstevel@tonic-gate ASSERT(cpu_pause_info.cp_paused == curthread); 10340Sstevel@tonic-gate return (1); 10350Sstevel@tonic-gate } 10360Sstevel@tonic-gate return (0); 10370Sstevel@tonic-gate } 10380Sstevel@tonic-gate 10390Sstevel@tonic-gate static cpu_t * 10400Sstevel@tonic-gate cpu_get_all(processorid_t cpun) 10410Sstevel@tonic-gate { 10420Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 10430Sstevel@tonic-gate 10440Sstevel@tonic-gate if (cpun >= NCPU || cpun < 0 || !CPU_IN_SET(cpu_available, cpun)) 10450Sstevel@tonic-gate return (NULL); 10460Sstevel@tonic-gate return (cpu[cpun]); 10470Sstevel@tonic-gate } 10480Sstevel@tonic-gate 10490Sstevel@tonic-gate /* 10500Sstevel@tonic-gate * Check whether cpun is a valid processor id and whether it should be 10510Sstevel@tonic-gate * visible from the current zone. If it is, return a pointer to the 10520Sstevel@tonic-gate * associated CPU structure. 10530Sstevel@tonic-gate */ 10540Sstevel@tonic-gate cpu_t * 10550Sstevel@tonic-gate cpu_get(processorid_t cpun) 10560Sstevel@tonic-gate { 10570Sstevel@tonic-gate cpu_t *c; 10580Sstevel@tonic-gate 10590Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 10600Sstevel@tonic-gate c = cpu_get_all(cpun); 10610Sstevel@tonic-gate if (c != NULL && !INGLOBALZONE(curproc) && pool_pset_enabled() && 10620Sstevel@tonic-gate zone_pset_get(curproc->p_zone) != cpupart_query_cpu(c)) 10630Sstevel@tonic-gate return (NULL); 10640Sstevel@tonic-gate return (c); 10650Sstevel@tonic-gate } 10660Sstevel@tonic-gate 10670Sstevel@tonic-gate /* 10680Sstevel@tonic-gate * The following functions should be used to check CPU states in the kernel. 10690Sstevel@tonic-gate * They should be invoked with cpu_lock held. Kernel subsystems interested 10700Sstevel@tonic-gate * in CPU states should *not* use cpu_get_state() and various P_ONLINE/etc 10710Sstevel@tonic-gate * states. Those are for user-land (and system call) use only. 10720Sstevel@tonic-gate */ 10730Sstevel@tonic-gate 10740Sstevel@tonic-gate /* 10750Sstevel@tonic-gate * Determine whether the CPU is online and handling interrupts. 10760Sstevel@tonic-gate */ 10770Sstevel@tonic-gate int 10780Sstevel@tonic-gate cpu_is_online(cpu_t *cpu) 10790Sstevel@tonic-gate { 10800Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 10810Sstevel@tonic-gate return (cpu_flagged_online(cpu->cpu_flags)); 10820Sstevel@tonic-gate } 10830Sstevel@tonic-gate 10840Sstevel@tonic-gate /* 10850Sstevel@tonic-gate * Determine whether the CPU is offline (this includes spare and faulted). 10860Sstevel@tonic-gate */ 10870Sstevel@tonic-gate int 10880Sstevel@tonic-gate cpu_is_offline(cpu_t *cpu) 10890Sstevel@tonic-gate { 10900Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 10910Sstevel@tonic-gate return (cpu_flagged_offline(cpu->cpu_flags)); 10920Sstevel@tonic-gate } 10930Sstevel@tonic-gate 10940Sstevel@tonic-gate /* 10950Sstevel@tonic-gate * Determine whether the CPU is powered off. 10960Sstevel@tonic-gate */ 10970Sstevel@tonic-gate int 10980Sstevel@tonic-gate cpu_is_poweredoff(cpu_t *cpu) 10990Sstevel@tonic-gate { 11000Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 11010Sstevel@tonic-gate return (cpu_flagged_poweredoff(cpu->cpu_flags)); 11020Sstevel@tonic-gate } 11030Sstevel@tonic-gate 11040Sstevel@tonic-gate /* 11050Sstevel@tonic-gate * Determine whether the CPU is handling interrupts. 11060Sstevel@tonic-gate */ 11070Sstevel@tonic-gate int 11080Sstevel@tonic-gate cpu_is_nointr(cpu_t *cpu) 11090Sstevel@tonic-gate { 11100Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 11110Sstevel@tonic-gate return (cpu_flagged_nointr(cpu->cpu_flags)); 11120Sstevel@tonic-gate } 11130Sstevel@tonic-gate 11140Sstevel@tonic-gate /* 11150Sstevel@tonic-gate * Determine whether the CPU is active (scheduling threads). 11160Sstevel@tonic-gate */ 11170Sstevel@tonic-gate int 11180Sstevel@tonic-gate cpu_is_active(cpu_t *cpu) 11190Sstevel@tonic-gate { 11200Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 11210Sstevel@tonic-gate return (cpu_flagged_active(cpu->cpu_flags)); 11220Sstevel@tonic-gate } 11230Sstevel@tonic-gate 11240Sstevel@tonic-gate /* 11250Sstevel@tonic-gate * Same as above, but these require cpu_flags instead of cpu_t pointers. 11260Sstevel@tonic-gate */ 11270Sstevel@tonic-gate int 11280Sstevel@tonic-gate cpu_flagged_online(cpu_flag_t cpu_flags) 11290Sstevel@tonic-gate { 11300Sstevel@tonic-gate return (cpu_flagged_active(cpu_flags) && 11310Sstevel@tonic-gate (cpu_flags & CPU_ENABLE)); 11320Sstevel@tonic-gate } 11330Sstevel@tonic-gate 11340Sstevel@tonic-gate int 11350Sstevel@tonic-gate cpu_flagged_offline(cpu_flag_t cpu_flags) 11360Sstevel@tonic-gate { 11370Sstevel@tonic-gate return (((cpu_flags & CPU_POWEROFF) == 0) && 11380Sstevel@tonic-gate ((cpu_flags & (CPU_READY | CPU_OFFLINE)) != CPU_READY)); 11390Sstevel@tonic-gate } 11400Sstevel@tonic-gate 11410Sstevel@tonic-gate int 11420Sstevel@tonic-gate cpu_flagged_poweredoff(cpu_flag_t cpu_flags) 11430Sstevel@tonic-gate { 11440Sstevel@tonic-gate return ((cpu_flags & CPU_POWEROFF) == CPU_POWEROFF); 11450Sstevel@tonic-gate } 11460Sstevel@tonic-gate 11470Sstevel@tonic-gate int 11480Sstevel@tonic-gate cpu_flagged_nointr(cpu_flag_t cpu_flags) 11490Sstevel@tonic-gate { 11500Sstevel@tonic-gate return (cpu_flagged_active(cpu_flags) && 11510Sstevel@tonic-gate (cpu_flags & CPU_ENABLE) == 0); 11520Sstevel@tonic-gate } 11530Sstevel@tonic-gate 11540Sstevel@tonic-gate int 11550Sstevel@tonic-gate cpu_flagged_active(cpu_flag_t cpu_flags) 11560Sstevel@tonic-gate { 11570Sstevel@tonic-gate return (((cpu_flags & (CPU_POWEROFF | CPU_FAULTED | CPU_SPARE)) == 0) && 11580Sstevel@tonic-gate ((cpu_flags & (CPU_READY | CPU_OFFLINE)) == CPU_READY)); 11590Sstevel@tonic-gate } 11600Sstevel@tonic-gate 11610Sstevel@tonic-gate /* 11620Sstevel@tonic-gate * Bring the indicated CPU online. 11630Sstevel@tonic-gate */ 11640Sstevel@tonic-gate int 11650Sstevel@tonic-gate cpu_online(cpu_t *cp) 11660Sstevel@tonic-gate { 11670Sstevel@tonic-gate int error = 0; 11680Sstevel@tonic-gate 11690Sstevel@tonic-gate /* 11700Sstevel@tonic-gate * Handle on-line request. 11710Sstevel@tonic-gate * This code must put the new CPU on the active list before 11720Sstevel@tonic-gate * starting it because it will not be paused, and will start 11730Sstevel@tonic-gate * using the active list immediately. The real start occurs 11740Sstevel@tonic-gate * when the CPU_QUIESCED flag is turned off. 11750Sstevel@tonic-gate */ 11760Sstevel@tonic-gate 11770Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 11780Sstevel@tonic-gate 11790Sstevel@tonic-gate /* 11800Sstevel@tonic-gate * Put all the cpus into a known safe place. 11810Sstevel@tonic-gate * No mutexes can be entered while CPUs are paused. 11820Sstevel@tonic-gate */ 11830Sstevel@tonic-gate error = mp_cpu_start(cp); /* arch-dep hook */ 11840Sstevel@tonic-gate if (error == 0) { 11850Sstevel@tonic-gate pause_cpus(NULL); 11860Sstevel@tonic-gate cpu_add_active_internal(cp); 11870Sstevel@tonic-gate if (cp->cpu_flags & CPU_FAULTED) { 11880Sstevel@tonic-gate cp->cpu_flags &= ~CPU_FAULTED; 11890Sstevel@tonic-gate mp_cpu_faulted_exit(cp); 11900Sstevel@tonic-gate } 11910Sstevel@tonic-gate cp->cpu_flags &= ~(CPU_QUIESCED | CPU_OFFLINE | CPU_FROZEN | 11920Sstevel@tonic-gate CPU_SPARE); 11930Sstevel@tonic-gate start_cpus(); 11940Sstevel@tonic-gate cpu_stats_kstat_create(cp); 11950Sstevel@tonic-gate cpu_create_intrstat(cp); 11960Sstevel@tonic-gate lgrp_kstat_create(cp); 11970Sstevel@tonic-gate cpu_state_change_notify(cp->cpu_id, CPU_ON); 11980Sstevel@tonic-gate cpu_intr_enable(cp); /* arch-dep hook */ 11990Sstevel@tonic-gate cyclic_online(cp); 12000Sstevel@tonic-gate poke_cpu(cp->cpu_id); 12010Sstevel@tonic-gate } 12020Sstevel@tonic-gate 12030Sstevel@tonic-gate return (error); 12040Sstevel@tonic-gate } 12050Sstevel@tonic-gate 12060Sstevel@tonic-gate /* 12070Sstevel@tonic-gate * Take the indicated CPU offline. 12080Sstevel@tonic-gate */ 12090Sstevel@tonic-gate int 12100Sstevel@tonic-gate cpu_offline(cpu_t *cp, int flags) 12110Sstevel@tonic-gate { 12120Sstevel@tonic-gate cpupart_t *pp; 12130Sstevel@tonic-gate int error = 0; 12140Sstevel@tonic-gate cpu_t *ncp; 12150Sstevel@tonic-gate int intr_enable; 12160Sstevel@tonic-gate int cyclic_off = 0; 12170Sstevel@tonic-gate int loop_count; 12180Sstevel@tonic-gate int no_quiesce = 0; 12190Sstevel@tonic-gate int (*bound_func)(struct cpu *, int); 12200Sstevel@tonic-gate kthread_t *t; 12210Sstevel@tonic-gate lpl_t *cpu_lpl; 12220Sstevel@tonic-gate proc_t *p; 12230Sstevel@tonic-gate int lgrp_diff_lpl; 12240Sstevel@tonic-gate 12250Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 12260Sstevel@tonic-gate 12270Sstevel@tonic-gate /* 12280Sstevel@tonic-gate * If we're going from faulted or spare to offline, just 12290Sstevel@tonic-gate * clear these flags and update CPU state. 12300Sstevel@tonic-gate */ 12310Sstevel@tonic-gate if (cp->cpu_flags & (CPU_FAULTED | CPU_SPARE)) { 12320Sstevel@tonic-gate if (cp->cpu_flags & CPU_FAULTED) { 12330Sstevel@tonic-gate cp->cpu_flags &= ~CPU_FAULTED; 12340Sstevel@tonic-gate mp_cpu_faulted_exit(cp); 12350Sstevel@tonic-gate } 12360Sstevel@tonic-gate cp->cpu_flags &= ~CPU_SPARE; 12370Sstevel@tonic-gate cpu_set_state(cp); 12380Sstevel@tonic-gate return (0); 12390Sstevel@tonic-gate } 12400Sstevel@tonic-gate 12410Sstevel@tonic-gate /* 12420Sstevel@tonic-gate * Handle off-line request. 12430Sstevel@tonic-gate */ 12440Sstevel@tonic-gate pp = cp->cpu_part; 12450Sstevel@tonic-gate /* 12460Sstevel@tonic-gate * Don't offline last online CPU in partition 12470Sstevel@tonic-gate */ 12480Sstevel@tonic-gate if (ncpus_online <= 1 || pp->cp_ncpus <= 1 || cpu_intr_count(cp) < 2) 12490Sstevel@tonic-gate return (EBUSY); 12500Sstevel@tonic-gate /* 12510Sstevel@tonic-gate * Unbind all thread bound to our CPU if we were asked to. 12520Sstevel@tonic-gate */ 12530Sstevel@tonic-gate if (flags & CPU_FORCED && (error = cpu_unbind(cp->cpu_id)) != 0) 12540Sstevel@tonic-gate return (error); 12550Sstevel@tonic-gate /* 12560Sstevel@tonic-gate * We shouldn't be bound to this CPU ourselves. 12570Sstevel@tonic-gate */ 12580Sstevel@tonic-gate if (curthread->t_bound_cpu == cp) 12590Sstevel@tonic-gate return (EBUSY); 12600Sstevel@tonic-gate 12610Sstevel@tonic-gate /* 12620Sstevel@tonic-gate * Tell interested parties that this CPU is going offline. 12630Sstevel@tonic-gate */ 12640Sstevel@tonic-gate cpu_state_change_notify(cp->cpu_id, CPU_OFF); 12650Sstevel@tonic-gate 12660Sstevel@tonic-gate /* 12670Sstevel@tonic-gate * Take the CPU out of interrupt participation so we won't find 12680Sstevel@tonic-gate * bound kernel threads. If the architecture cannot completely 12690Sstevel@tonic-gate * shut off interrupts on the CPU, don't quiesce it, but don't 12700Sstevel@tonic-gate * run anything but interrupt thread... this is indicated by 12710Sstevel@tonic-gate * the CPU_OFFLINE flag being on but the CPU_QUIESCE flag being 12720Sstevel@tonic-gate * off. 12730Sstevel@tonic-gate */ 12740Sstevel@tonic-gate intr_enable = cp->cpu_flags & CPU_ENABLE; 12750Sstevel@tonic-gate if (intr_enable) 12760Sstevel@tonic-gate no_quiesce = cpu_intr_disable(cp); 12770Sstevel@tonic-gate 12780Sstevel@tonic-gate /* 12790Sstevel@tonic-gate * Record that we are aiming to offline this cpu. This acts as 12800Sstevel@tonic-gate * a barrier to further weak binding requests in thread_nomigrate 12810Sstevel@tonic-gate * and also causes cpu_choose, disp_lowpri_cpu and setfrontdq to 12820Sstevel@tonic-gate * lean away from this cpu. Further strong bindings are already 12830Sstevel@tonic-gate * avoided since we hold cpu_lock. Since threads that are set 12840Sstevel@tonic-gate * runnable around now and others coming off the target cpu are 12850Sstevel@tonic-gate * directed away from the target, existing strong and weak bindings 12860Sstevel@tonic-gate * (especially the latter) to the target cpu stand maximum chance of 12870Sstevel@tonic-gate * being able to unbind during the short delay loop below (if other 12880Sstevel@tonic-gate * unbound threads compete they may not see cpu in time to unbind 12890Sstevel@tonic-gate * even if they would do so immediately. 12900Sstevel@tonic-gate */ 12910Sstevel@tonic-gate cpu_inmotion = cp; 12920Sstevel@tonic-gate membar_enter(); 12930Sstevel@tonic-gate 12940Sstevel@tonic-gate /* 12950Sstevel@tonic-gate * Check for kernel threads (strong or weak) bound to that CPU. 12960Sstevel@tonic-gate * Strongly bound threads may not unbind, and we'll have to return 12970Sstevel@tonic-gate * EBUSY. Weakly bound threads should always disappear - we've 12980Sstevel@tonic-gate * stopped more weak binding with cpu_inmotion and existing 12990Sstevel@tonic-gate * bindings will drain imminently (they may not block). Nonetheless 13000Sstevel@tonic-gate * we will wait for a fixed period for all bound threads to disappear. 13010Sstevel@tonic-gate * Inactive interrupt threads are OK (they'll be in TS_FREE 13020Sstevel@tonic-gate * state). If test finds some bound threads, wait a few ticks 13030Sstevel@tonic-gate * to give short-lived threads (such as interrupts) chance to 13040Sstevel@tonic-gate * complete. Note that if no_quiesce is set, i.e. this cpu 13050Sstevel@tonic-gate * is required to service interrupts, then we take the route 13060Sstevel@tonic-gate * that permits interrupt threads to be active (or bypassed). 13070Sstevel@tonic-gate */ 13080Sstevel@tonic-gate bound_func = no_quiesce ? disp_bound_threads : disp_bound_anythreads; 13090Sstevel@tonic-gate 13100Sstevel@tonic-gate again: for (loop_count = 0; (*bound_func)(cp, 0); loop_count++) { 13110Sstevel@tonic-gate if (loop_count >= 5) { 13120Sstevel@tonic-gate error = EBUSY; /* some threads still bound */ 13130Sstevel@tonic-gate break; 13140Sstevel@tonic-gate } 13150Sstevel@tonic-gate 13160Sstevel@tonic-gate /* 13170Sstevel@tonic-gate * If some threads were assigned, give them 13180Sstevel@tonic-gate * a chance to complete or move. 13190Sstevel@tonic-gate * 13200Sstevel@tonic-gate * This assumes that the clock_thread is not bound 13210Sstevel@tonic-gate * to any CPU, because the clock_thread is needed to 13220Sstevel@tonic-gate * do the delay(hz/100). 13230Sstevel@tonic-gate * 13240Sstevel@tonic-gate * Note: we still hold the cpu_lock while waiting for 13250Sstevel@tonic-gate * the next clock tick. This is OK since it isn't 13260Sstevel@tonic-gate * needed for anything else except processor_bind(2), 13270Sstevel@tonic-gate * and system initialization. If we drop the lock, 13280Sstevel@tonic-gate * we would risk another p_online disabling the last 13290Sstevel@tonic-gate * processor. 13300Sstevel@tonic-gate */ 13310Sstevel@tonic-gate delay(hz/100); 13320Sstevel@tonic-gate } 13330Sstevel@tonic-gate 13340Sstevel@tonic-gate if (error == 0 && cyclic_off == 0) { 13350Sstevel@tonic-gate if (!cyclic_offline(cp)) { 13360Sstevel@tonic-gate /* 13370Sstevel@tonic-gate * We must have bound cyclics... 13380Sstevel@tonic-gate */ 13390Sstevel@tonic-gate error = EBUSY; 13400Sstevel@tonic-gate goto out; 13410Sstevel@tonic-gate } 13420Sstevel@tonic-gate cyclic_off = 1; 13430Sstevel@tonic-gate } 13440Sstevel@tonic-gate 13450Sstevel@tonic-gate /* 13460Sstevel@tonic-gate * Call mp_cpu_stop() to perform any special operations 13470Sstevel@tonic-gate * needed for this machine architecture to offline a CPU. 13480Sstevel@tonic-gate */ 13490Sstevel@tonic-gate if (error == 0) 13500Sstevel@tonic-gate error = mp_cpu_stop(cp); /* arch-dep hook */ 13510Sstevel@tonic-gate 13520Sstevel@tonic-gate /* 13530Sstevel@tonic-gate * If that all worked, take the CPU offline and decrement 13540Sstevel@tonic-gate * ncpus_online. 13550Sstevel@tonic-gate */ 13560Sstevel@tonic-gate if (error == 0) { 13570Sstevel@tonic-gate /* 13580Sstevel@tonic-gate * Put all the cpus into a known safe place. 13590Sstevel@tonic-gate * No mutexes can be entered while CPUs are paused. 13600Sstevel@tonic-gate */ 13610Sstevel@tonic-gate pause_cpus(cp); 13620Sstevel@tonic-gate /* 13630Sstevel@tonic-gate * Repeat the operation, if necessary, to make sure that 13640Sstevel@tonic-gate * all outstanding low-level interrupts run to completion 13650Sstevel@tonic-gate * before we set the CPU_QUIESCED flag. It's also possible 13660Sstevel@tonic-gate * that a thread has weak bound to the cpu despite our raising 13670Sstevel@tonic-gate * cpu_inmotion above since it may have loaded that 13680Sstevel@tonic-gate * value before the barrier became visible (this would have 13690Sstevel@tonic-gate * to be the thread that was on the target cpu at the time 13700Sstevel@tonic-gate * we raised the barrier). 13710Sstevel@tonic-gate */ 13720Sstevel@tonic-gate if ((!no_quiesce && cp->cpu_intr_actv != 0) || 13730Sstevel@tonic-gate (*bound_func)(cp, 1)) { 13740Sstevel@tonic-gate start_cpus(); 13750Sstevel@tonic-gate (void) mp_cpu_start(cp); 13760Sstevel@tonic-gate goto again; 13770Sstevel@tonic-gate } 13780Sstevel@tonic-gate ncp = cp->cpu_next_part; 13790Sstevel@tonic-gate cpu_lpl = cp->cpu_lpl; 13800Sstevel@tonic-gate ASSERT(cpu_lpl != NULL); 13810Sstevel@tonic-gate 13820Sstevel@tonic-gate /* 13830Sstevel@tonic-gate * Remove the CPU from the list of active CPUs. 13840Sstevel@tonic-gate */ 13850Sstevel@tonic-gate cpu_remove_active(cp); 13860Sstevel@tonic-gate 13870Sstevel@tonic-gate /* 13880Sstevel@tonic-gate * Walk the active process list and look for threads 13890Sstevel@tonic-gate * whose home lgroup needs to be updated, or 13900Sstevel@tonic-gate * the last CPU they run on is the one being offlined now. 13910Sstevel@tonic-gate */ 13920Sstevel@tonic-gate 13930Sstevel@tonic-gate ASSERT(curthread->t_cpu != cp); 13940Sstevel@tonic-gate for (p = practive; p != NULL; p = p->p_next) { 13950Sstevel@tonic-gate 13960Sstevel@tonic-gate t = p->p_tlist; 13970Sstevel@tonic-gate 13980Sstevel@tonic-gate if (t == NULL) 13990Sstevel@tonic-gate continue; 14000Sstevel@tonic-gate 14010Sstevel@tonic-gate lgrp_diff_lpl = 0; 14020Sstevel@tonic-gate 14030Sstevel@tonic-gate do { 14040Sstevel@tonic-gate ASSERT(t->t_lpl != NULL); 14050Sstevel@tonic-gate /* 14060Sstevel@tonic-gate * Taking last CPU in lpl offline 14070Sstevel@tonic-gate * Rehome thread if it is in this lpl 14080Sstevel@tonic-gate * Otherwise, update the count of how many 14090Sstevel@tonic-gate * threads are in this CPU's lgroup but have 14100Sstevel@tonic-gate * a different lpl. 14110Sstevel@tonic-gate */ 14120Sstevel@tonic-gate 14130Sstevel@tonic-gate if (cpu_lpl->lpl_ncpu == 0) { 14140Sstevel@tonic-gate if (t->t_lpl == cpu_lpl) 14150Sstevel@tonic-gate lgrp_move_thread(t, 14160Sstevel@tonic-gate lgrp_choose(t, 14170Sstevel@tonic-gate t->t_cpupart), 0); 14180Sstevel@tonic-gate else if (t->t_lpl->lpl_lgrpid == 14190Sstevel@tonic-gate cpu_lpl->lpl_lgrpid) 14200Sstevel@tonic-gate lgrp_diff_lpl++; 14210Sstevel@tonic-gate } 14220Sstevel@tonic-gate ASSERT(t->t_lpl->lpl_ncpu > 0); 14230Sstevel@tonic-gate 14240Sstevel@tonic-gate /* 14250Sstevel@tonic-gate * Update CPU last ran on if it was this CPU 14260Sstevel@tonic-gate */ 14270Sstevel@tonic-gate if (t->t_cpu == cp && t->t_bound_cpu != cp) 14280Sstevel@tonic-gate t->t_cpu = disp_lowpri_cpu(ncp, t->t_lpl, 14290Sstevel@tonic-gate t->t_pri, NULL); 14300Sstevel@tonic-gate ASSERT(t->t_cpu != cp || t->t_bound_cpu == cp || 14310Sstevel@tonic-gate t->t_weakbound_cpu == cp); 14320Sstevel@tonic-gate 14330Sstevel@tonic-gate t = t->t_forw; 14340Sstevel@tonic-gate } while (t != p->p_tlist); 14350Sstevel@tonic-gate 14360Sstevel@tonic-gate /* 14370Sstevel@tonic-gate * Didn't find any threads in the same lgroup as this 14380Sstevel@tonic-gate * CPU with a different lpl, so remove the lgroup from 14390Sstevel@tonic-gate * the process lgroup bitmask. 14400Sstevel@tonic-gate */ 14410Sstevel@tonic-gate 14420Sstevel@tonic-gate if (lgrp_diff_lpl == 0) 14430Sstevel@tonic-gate klgrpset_del(p->p_lgrpset, cpu_lpl->lpl_lgrpid); 14440Sstevel@tonic-gate } 14450Sstevel@tonic-gate 14460Sstevel@tonic-gate /* 14470Sstevel@tonic-gate * Walk thread list looking for threads that need to be 14480Sstevel@tonic-gate * rehomed, since there are some threads that are not in 14490Sstevel@tonic-gate * their process's p_tlist. 14500Sstevel@tonic-gate */ 14510Sstevel@tonic-gate 14520Sstevel@tonic-gate t = curthread; 14530Sstevel@tonic-gate do { 14540Sstevel@tonic-gate ASSERT(t != NULL && t->t_lpl != NULL); 14550Sstevel@tonic-gate 14560Sstevel@tonic-gate /* 14570Sstevel@tonic-gate * Rehome threads with same lpl as this CPU when this 14580Sstevel@tonic-gate * is the last CPU in the lpl. 14590Sstevel@tonic-gate */ 14600Sstevel@tonic-gate 14610Sstevel@tonic-gate if ((cpu_lpl->lpl_ncpu == 0) && (t->t_lpl == cpu_lpl)) 14620Sstevel@tonic-gate lgrp_move_thread(t, 14630Sstevel@tonic-gate lgrp_choose(t, t->t_cpupart), 1); 14640Sstevel@tonic-gate 14650Sstevel@tonic-gate ASSERT(t->t_lpl->lpl_ncpu > 0); 14660Sstevel@tonic-gate 14670Sstevel@tonic-gate /* 14680Sstevel@tonic-gate * Update CPU last ran on if it was this CPU 14690Sstevel@tonic-gate */ 14700Sstevel@tonic-gate 14710Sstevel@tonic-gate if (t->t_cpu == cp && t->t_bound_cpu != cp) { 14720Sstevel@tonic-gate t->t_cpu = disp_lowpri_cpu(ncp, 14730Sstevel@tonic-gate t->t_lpl, t->t_pri, NULL); 14740Sstevel@tonic-gate } 14750Sstevel@tonic-gate ASSERT(t->t_cpu != cp || t->t_bound_cpu == cp || 14760Sstevel@tonic-gate t->t_weakbound_cpu == cp); 14770Sstevel@tonic-gate t = t->t_next; 14780Sstevel@tonic-gate 14790Sstevel@tonic-gate } while (t != curthread); 14800Sstevel@tonic-gate ASSERT((cp->cpu_flags & (CPU_FAULTED | CPU_SPARE)) == 0); 14810Sstevel@tonic-gate cp->cpu_flags |= CPU_OFFLINE; 14820Sstevel@tonic-gate disp_cpu_inactive(cp); 14830Sstevel@tonic-gate if (!no_quiesce) 14840Sstevel@tonic-gate cp->cpu_flags |= CPU_QUIESCED; 14850Sstevel@tonic-gate ncpus_online--; 14860Sstevel@tonic-gate cpu_set_state(cp); 14870Sstevel@tonic-gate cpu_inmotion = NULL; 14880Sstevel@tonic-gate start_cpus(); 14890Sstevel@tonic-gate cpu_stats_kstat_destroy(cp); 14900Sstevel@tonic-gate cpu_delete_intrstat(cp); 14910Sstevel@tonic-gate lgrp_kstat_destroy(cp); 14920Sstevel@tonic-gate } 14930Sstevel@tonic-gate 14940Sstevel@tonic-gate out: 14950Sstevel@tonic-gate cpu_inmotion = NULL; 14960Sstevel@tonic-gate 14970Sstevel@tonic-gate /* 14980Sstevel@tonic-gate * If we failed, re-enable interrupts. 14990Sstevel@tonic-gate * Do this even if cpu_intr_disable returned an error, because 15000Sstevel@tonic-gate * it may have partially disabled interrupts. 15010Sstevel@tonic-gate */ 15020Sstevel@tonic-gate if (error && intr_enable) 15030Sstevel@tonic-gate cpu_intr_enable(cp); 15040Sstevel@tonic-gate 15050Sstevel@tonic-gate /* 15060Sstevel@tonic-gate * If we failed, but managed to offline the cyclic subsystem on this 15070Sstevel@tonic-gate * CPU, bring it back online. 15080Sstevel@tonic-gate */ 15090Sstevel@tonic-gate if (error && cyclic_off) 15100Sstevel@tonic-gate cyclic_online(cp); 15110Sstevel@tonic-gate 15120Sstevel@tonic-gate /* 15130Sstevel@tonic-gate * If we failed, we need to notify everyone that this CPU is back on. 15140Sstevel@tonic-gate */ 15150Sstevel@tonic-gate if (error != 0) 15160Sstevel@tonic-gate cpu_state_change_notify(cp->cpu_id, CPU_ON); 15170Sstevel@tonic-gate 15180Sstevel@tonic-gate return (error); 15190Sstevel@tonic-gate } 15200Sstevel@tonic-gate 15210Sstevel@tonic-gate /* 15220Sstevel@tonic-gate * Mark the indicated CPU as faulted, taking it offline. 15230Sstevel@tonic-gate */ 15240Sstevel@tonic-gate int 15250Sstevel@tonic-gate cpu_faulted(cpu_t *cp, int flags) 15260Sstevel@tonic-gate { 15270Sstevel@tonic-gate int error = 0; 15280Sstevel@tonic-gate 15290Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 15300Sstevel@tonic-gate ASSERT(!cpu_is_poweredoff(cp)); 15310Sstevel@tonic-gate 15320Sstevel@tonic-gate if (cpu_is_offline(cp)) { 15330Sstevel@tonic-gate cp->cpu_flags &= ~CPU_SPARE; 15340Sstevel@tonic-gate cp->cpu_flags |= CPU_FAULTED; 15350Sstevel@tonic-gate mp_cpu_faulted_enter(cp); 15360Sstevel@tonic-gate cpu_set_state(cp); 15370Sstevel@tonic-gate return (0); 15380Sstevel@tonic-gate } 15390Sstevel@tonic-gate 15400Sstevel@tonic-gate if ((error = cpu_offline(cp, flags)) == 0) { 15410Sstevel@tonic-gate cp->cpu_flags |= CPU_FAULTED; 15420Sstevel@tonic-gate mp_cpu_faulted_enter(cp); 15430Sstevel@tonic-gate cpu_set_state(cp); 15440Sstevel@tonic-gate } 15450Sstevel@tonic-gate 15460Sstevel@tonic-gate return (error); 15470Sstevel@tonic-gate } 15480Sstevel@tonic-gate 15490Sstevel@tonic-gate /* 15500Sstevel@tonic-gate * Mark the indicated CPU as a spare, taking it offline. 15510Sstevel@tonic-gate */ 15520Sstevel@tonic-gate int 15530Sstevel@tonic-gate cpu_spare(cpu_t *cp, int flags) 15540Sstevel@tonic-gate { 15550Sstevel@tonic-gate int error = 0; 15560Sstevel@tonic-gate 15570Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 15580Sstevel@tonic-gate ASSERT(!cpu_is_poweredoff(cp)); 15590Sstevel@tonic-gate 15600Sstevel@tonic-gate if (cpu_is_offline(cp)) { 15610Sstevel@tonic-gate if (cp->cpu_flags & CPU_FAULTED) { 15620Sstevel@tonic-gate cp->cpu_flags &= ~CPU_FAULTED; 15630Sstevel@tonic-gate mp_cpu_faulted_exit(cp); 15640Sstevel@tonic-gate } 15650Sstevel@tonic-gate cp->cpu_flags |= CPU_SPARE; 15660Sstevel@tonic-gate cpu_set_state(cp); 15670Sstevel@tonic-gate return (0); 15680Sstevel@tonic-gate } 15690Sstevel@tonic-gate 15700Sstevel@tonic-gate if ((error = cpu_offline(cp, flags)) == 0) { 15710Sstevel@tonic-gate cp->cpu_flags |= CPU_SPARE; 15720Sstevel@tonic-gate cpu_set_state(cp); 15730Sstevel@tonic-gate } 15740Sstevel@tonic-gate 15750Sstevel@tonic-gate return (error); 15760Sstevel@tonic-gate } 15770Sstevel@tonic-gate 15780Sstevel@tonic-gate /* 15790Sstevel@tonic-gate * Take the indicated CPU from poweroff to offline. 15800Sstevel@tonic-gate */ 15810Sstevel@tonic-gate int 15820Sstevel@tonic-gate cpu_poweron(cpu_t *cp) 15830Sstevel@tonic-gate { 15840Sstevel@tonic-gate int error = ENOTSUP; 15850Sstevel@tonic-gate 15860Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 15870Sstevel@tonic-gate ASSERT(cpu_is_poweredoff(cp)); 15880Sstevel@tonic-gate 15890Sstevel@tonic-gate error = mp_cpu_poweron(cp); /* arch-dep hook */ 15900Sstevel@tonic-gate if (error == 0) 15910Sstevel@tonic-gate cpu_set_state(cp); 15920Sstevel@tonic-gate 15930Sstevel@tonic-gate return (error); 15940Sstevel@tonic-gate } 15950Sstevel@tonic-gate 15960Sstevel@tonic-gate /* 15970Sstevel@tonic-gate * Take the indicated CPU from any inactive state to powered off. 15980Sstevel@tonic-gate */ 15990Sstevel@tonic-gate int 16000Sstevel@tonic-gate cpu_poweroff(cpu_t *cp) 16010Sstevel@tonic-gate { 16020Sstevel@tonic-gate int error = ENOTSUP; 16030Sstevel@tonic-gate 16040Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 16050Sstevel@tonic-gate ASSERT(cpu_is_offline(cp)); 16060Sstevel@tonic-gate 16070Sstevel@tonic-gate if (!(cp->cpu_flags & CPU_QUIESCED)) 16080Sstevel@tonic-gate return (EBUSY); /* not completely idle */ 16090Sstevel@tonic-gate 16100Sstevel@tonic-gate error = mp_cpu_poweroff(cp); /* arch-dep hook */ 16110Sstevel@tonic-gate if (error == 0) 16120Sstevel@tonic-gate cpu_set_state(cp); 16130Sstevel@tonic-gate 16140Sstevel@tonic-gate return (error); 16150Sstevel@tonic-gate } 16160Sstevel@tonic-gate 16170Sstevel@tonic-gate /* 16180Sstevel@tonic-gate * Initialize the CPU lists for the first CPU. 16190Sstevel@tonic-gate */ 16200Sstevel@tonic-gate void 16210Sstevel@tonic-gate cpu_list_init(cpu_t *cp) 16220Sstevel@tonic-gate { 16230Sstevel@tonic-gate cp->cpu_next = cp; 16240Sstevel@tonic-gate cp->cpu_prev = cp; 16250Sstevel@tonic-gate cpu_list = cp; 16260Sstevel@tonic-gate 16270Sstevel@tonic-gate cp->cpu_next_onln = cp; 16280Sstevel@tonic-gate cp->cpu_prev_onln = cp; 16290Sstevel@tonic-gate cpu_active = cp; 16300Sstevel@tonic-gate 16310Sstevel@tonic-gate cp->cpu_seqid = 0; 16320Sstevel@tonic-gate CPUSET_ADD(cpu_seqid_inuse, 0); 16330Sstevel@tonic-gate cp->cpu_cache_offset = KMEM_CACHE_SIZE(cp->cpu_seqid); 16340Sstevel@tonic-gate cp_default.cp_cpulist = cp; 16350Sstevel@tonic-gate cp_default.cp_ncpus = 1; 16360Sstevel@tonic-gate cp->cpu_next_part = cp; 16370Sstevel@tonic-gate cp->cpu_prev_part = cp; 16380Sstevel@tonic-gate cp->cpu_part = &cp_default; 16390Sstevel@tonic-gate 16400Sstevel@tonic-gate CPUSET_ADD(cpu_available, cp->cpu_id); 16410Sstevel@tonic-gate } 16420Sstevel@tonic-gate 16430Sstevel@tonic-gate /* 16440Sstevel@tonic-gate * Insert a CPU into the list of available CPUs. 16450Sstevel@tonic-gate */ 16460Sstevel@tonic-gate void 16470Sstevel@tonic-gate cpu_add_unit(cpu_t *cp) 16480Sstevel@tonic-gate { 16490Sstevel@tonic-gate int seqid; 16500Sstevel@tonic-gate 16510Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 16520Sstevel@tonic-gate ASSERT(cpu_list != NULL); /* list started in cpu_list_init */ 16530Sstevel@tonic-gate 16540Sstevel@tonic-gate lgrp_config(LGRP_CONFIG_CPU_ADD, (uintptr_t)cp, 0); 16550Sstevel@tonic-gate 16560Sstevel@tonic-gate /* 16570Sstevel@tonic-gate * Note: most users of the cpu_list will grab the 16580Sstevel@tonic-gate * cpu_lock to insure that it isn't modified. However, 16590Sstevel@tonic-gate * certain users can't or won't do that. To allow this 16600Sstevel@tonic-gate * we pause the other cpus. Users who walk the list 16610Sstevel@tonic-gate * without cpu_lock, must disable kernel preemption 16620Sstevel@tonic-gate * to insure that the list isn't modified underneath 16630Sstevel@tonic-gate * them. Also, any cached pointers to cpu structures 16640Sstevel@tonic-gate * must be revalidated by checking to see if the 16650Sstevel@tonic-gate * cpu_next pointer points to itself. This check must 16660Sstevel@tonic-gate * be done with the cpu_lock held or kernel preemption 16670Sstevel@tonic-gate * disabled. This check relies upon the fact that 16680Sstevel@tonic-gate * old cpu structures are not free'ed or cleared after 16690Sstevel@tonic-gate * then are removed from the cpu_list. 16700Sstevel@tonic-gate * 16710Sstevel@tonic-gate * Note that the clock code walks the cpu list dereferencing 16720Sstevel@tonic-gate * the cpu_part pointer, so we need to initialize it before 16730Sstevel@tonic-gate * adding the cpu to the list. 16740Sstevel@tonic-gate */ 16750Sstevel@tonic-gate cp->cpu_part = &cp_default; 16760Sstevel@tonic-gate (void) pause_cpus(NULL); 16770Sstevel@tonic-gate cp->cpu_next = cpu_list; 16780Sstevel@tonic-gate cp->cpu_prev = cpu_list->cpu_prev; 16790Sstevel@tonic-gate cpu_list->cpu_prev->cpu_next = cp; 16800Sstevel@tonic-gate cpu_list->cpu_prev = cp; 16810Sstevel@tonic-gate start_cpus(); 16820Sstevel@tonic-gate 16830Sstevel@tonic-gate for (seqid = 0; CPU_IN_SET(cpu_seqid_inuse, seqid); seqid++) 16840Sstevel@tonic-gate continue; 16850Sstevel@tonic-gate CPUSET_ADD(cpu_seqid_inuse, seqid); 16860Sstevel@tonic-gate cp->cpu_seqid = seqid; 16870Sstevel@tonic-gate ASSERT(ncpus < max_ncpus); 16880Sstevel@tonic-gate ncpus++; 16890Sstevel@tonic-gate cp->cpu_cache_offset = KMEM_CACHE_SIZE(cp->cpu_seqid); 16900Sstevel@tonic-gate cpu[cp->cpu_id] = cp; 16910Sstevel@tonic-gate CPUSET_ADD(cpu_available, cp->cpu_id); 16920Sstevel@tonic-gate 16930Sstevel@tonic-gate /* 16940Sstevel@tonic-gate * allocate a pause thread for this CPU. 16950Sstevel@tonic-gate */ 16960Sstevel@tonic-gate cpu_pause_alloc(cp); 16970Sstevel@tonic-gate 16980Sstevel@tonic-gate /* 16990Sstevel@tonic-gate * So that new CPUs won't have NULL prev_onln and next_onln pointers, 17000Sstevel@tonic-gate * link them into a list of just that CPU. 17010Sstevel@tonic-gate * This is so that disp_lowpri_cpu will work for thread_create in 17020Sstevel@tonic-gate * pause_cpus() when called from the startup thread in a new CPU. 17030Sstevel@tonic-gate */ 17040Sstevel@tonic-gate cp->cpu_next_onln = cp; 17050Sstevel@tonic-gate cp->cpu_prev_onln = cp; 17060Sstevel@tonic-gate cpu_info_kstat_create(cp); 17070Sstevel@tonic-gate cp->cpu_next_part = cp; 17080Sstevel@tonic-gate cp->cpu_prev_part = cp; 17090Sstevel@tonic-gate 17100Sstevel@tonic-gate init_cpu_mstate(cp, CMS_SYSTEM); 17110Sstevel@tonic-gate 17120Sstevel@tonic-gate pool_pset_mod = gethrtime(); 17130Sstevel@tonic-gate } 17140Sstevel@tonic-gate 17150Sstevel@tonic-gate /* 17160Sstevel@tonic-gate * Do the opposite of cpu_add_unit(). 17170Sstevel@tonic-gate */ 17180Sstevel@tonic-gate void 17190Sstevel@tonic-gate cpu_del_unit(int cpuid) 17200Sstevel@tonic-gate { 17210Sstevel@tonic-gate struct cpu *cp, *cpnext; 17220Sstevel@tonic-gate 17230Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 17240Sstevel@tonic-gate cp = cpu[cpuid]; 17250Sstevel@tonic-gate ASSERT(cp != NULL); 17260Sstevel@tonic-gate 17270Sstevel@tonic-gate ASSERT(cp->cpu_next_onln == cp); 17280Sstevel@tonic-gate ASSERT(cp->cpu_prev_onln == cp); 17290Sstevel@tonic-gate ASSERT(cp->cpu_next_part == cp); 17300Sstevel@tonic-gate ASSERT(cp->cpu_prev_part == cp); 17310Sstevel@tonic-gate 17320Sstevel@tonic-gate chip_cpu_fini(cp); 17330Sstevel@tonic-gate 17340Sstevel@tonic-gate /* 17350Sstevel@tonic-gate * Destroy kstat stuff. 17360Sstevel@tonic-gate */ 17370Sstevel@tonic-gate cpu_info_kstat_destroy(cp); 17380Sstevel@tonic-gate term_cpu_mstate(cp); 17390Sstevel@tonic-gate /* 17400Sstevel@tonic-gate * Free up pause thread. 17410Sstevel@tonic-gate */ 17420Sstevel@tonic-gate cpu_pause_free(cp); 17430Sstevel@tonic-gate CPUSET_DEL(cpu_available, cp->cpu_id); 17440Sstevel@tonic-gate cpu[cp->cpu_id] = NULL; 17450Sstevel@tonic-gate /* 17460Sstevel@tonic-gate * The clock thread and mutex_vector_enter cannot hold the 17470Sstevel@tonic-gate * cpu_lock while traversing the cpu list, therefore we pause 17480Sstevel@tonic-gate * all other threads by pausing the other cpus. These, and any 17490Sstevel@tonic-gate * other routines holding cpu pointers while possibly sleeping 17500Sstevel@tonic-gate * must be sure to call kpreempt_disable before processing the 17510Sstevel@tonic-gate * list and be sure to check that the cpu has not been deleted 17520Sstevel@tonic-gate * after any sleeps (check cp->cpu_next != NULL). We guarantee 17530Sstevel@tonic-gate * to keep the deleted cpu structure around. 17540Sstevel@tonic-gate * 17550Sstevel@tonic-gate * Note that this MUST be done AFTER cpu_available 17560Sstevel@tonic-gate * has been updated so that we don't waste time 17570Sstevel@tonic-gate * trying to pause the cpu we're trying to delete. 17580Sstevel@tonic-gate */ 17590Sstevel@tonic-gate (void) pause_cpus(NULL); 17600Sstevel@tonic-gate 17610Sstevel@tonic-gate cpnext = cp->cpu_next; 17620Sstevel@tonic-gate cp->cpu_prev->cpu_next = cp->cpu_next; 17630Sstevel@tonic-gate cp->cpu_next->cpu_prev = cp->cpu_prev; 17640Sstevel@tonic-gate if (cp == cpu_list) 17650Sstevel@tonic-gate cpu_list = cpnext; 17660Sstevel@tonic-gate 17670Sstevel@tonic-gate /* 17680Sstevel@tonic-gate * Signals that the cpu has been deleted (see above). 17690Sstevel@tonic-gate */ 17700Sstevel@tonic-gate cp->cpu_next = NULL; 17710Sstevel@tonic-gate cp->cpu_prev = NULL; 17720Sstevel@tonic-gate 17730Sstevel@tonic-gate start_cpus(); 17740Sstevel@tonic-gate 17750Sstevel@tonic-gate CPUSET_DEL(cpu_seqid_inuse, cp->cpu_seqid); 17760Sstevel@tonic-gate ncpus--; 17770Sstevel@tonic-gate lgrp_config(LGRP_CONFIG_CPU_DEL, (uintptr_t)cp, 0); 17780Sstevel@tonic-gate 17790Sstevel@tonic-gate pool_pset_mod = gethrtime(); 17800Sstevel@tonic-gate } 17810Sstevel@tonic-gate 17820Sstevel@tonic-gate /* 17830Sstevel@tonic-gate * Add a CPU to the list of active CPUs. 17840Sstevel@tonic-gate * This routine must not get any locks, because other CPUs are paused. 17850Sstevel@tonic-gate */ 17860Sstevel@tonic-gate static void 17870Sstevel@tonic-gate cpu_add_active_internal(cpu_t *cp) 17880Sstevel@tonic-gate { 17890Sstevel@tonic-gate cpupart_t *pp = cp->cpu_part; 17900Sstevel@tonic-gate 17910Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 17920Sstevel@tonic-gate ASSERT(cpu_list != NULL); /* list started in cpu_list_init */ 17930Sstevel@tonic-gate 17940Sstevel@tonic-gate ncpus_online++; 17950Sstevel@tonic-gate cpu_set_state(cp); 17960Sstevel@tonic-gate cp->cpu_next_onln = cpu_active; 17970Sstevel@tonic-gate cp->cpu_prev_onln = cpu_active->cpu_prev_onln; 17980Sstevel@tonic-gate cpu_active->cpu_prev_onln->cpu_next_onln = cp; 17990Sstevel@tonic-gate cpu_active->cpu_prev_onln = cp; 18000Sstevel@tonic-gate 18010Sstevel@tonic-gate if (pp->cp_cpulist) { 18020Sstevel@tonic-gate cp->cpu_next_part = pp->cp_cpulist; 18030Sstevel@tonic-gate cp->cpu_prev_part = pp->cp_cpulist->cpu_prev_part; 18040Sstevel@tonic-gate pp->cp_cpulist->cpu_prev_part->cpu_next_part = cp; 18050Sstevel@tonic-gate pp->cp_cpulist->cpu_prev_part = cp; 18060Sstevel@tonic-gate } else { 18070Sstevel@tonic-gate ASSERT(pp->cp_ncpus == 0); 18080Sstevel@tonic-gate pp->cp_cpulist = cp->cpu_next_part = cp->cpu_prev_part = cp; 18090Sstevel@tonic-gate } 18100Sstevel@tonic-gate pp->cp_ncpus++; 18110Sstevel@tonic-gate if (pp->cp_ncpus == 1) { 18120Sstevel@tonic-gate cp_numparts_nonempty++; 18130Sstevel@tonic-gate ASSERT(cp_numparts_nonempty != 0); 18140Sstevel@tonic-gate } 18150Sstevel@tonic-gate 18160Sstevel@tonic-gate chip_cpu_assign(cp); 18170Sstevel@tonic-gate 18180Sstevel@tonic-gate lgrp_config(LGRP_CONFIG_CPU_ONLINE, (uintptr_t)cp, 0); 18190Sstevel@tonic-gate 18200Sstevel@tonic-gate bzero(&cp->cpu_loadavg, sizeof (cp->cpu_loadavg)); 18210Sstevel@tonic-gate } 18220Sstevel@tonic-gate 18230Sstevel@tonic-gate /* 18240Sstevel@tonic-gate * Add a CPU to the list of active CPUs. 18250Sstevel@tonic-gate * This is called from machine-dependent layers when a new CPU is started. 18260Sstevel@tonic-gate */ 18270Sstevel@tonic-gate void 18280Sstevel@tonic-gate cpu_add_active(cpu_t *cp) 18290Sstevel@tonic-gate { 18300Sstevel@tonic-gate pause_cpus(NULL); 18310Sstevel@tonic-gate cpu_add_active_internal(cp); 18320Sstevel@tonic-gate start_cpus(); 18330Sstevel@tonic-gate cpu_stats_kstat_create(cp); 18340Sstevel@tonic-gate cpu_create_intrstat(cp); 18350Sstevel@tonic-gate lgrp_kstat_create(cp); 18360Sstevel@tonic-gate cpu_state_change_notify(cp->cpu_id, CPU_INIT); 18370Sstevel@tonic-gate } 18380Sstevel@tonic-gate 18390Sstevel@tonic-gate 18400Sstevel@tonic-gate /* 18410Sstevel@tonic-gate * Remove a CPU from the list of active CPUs. 18420Sstevel@tonic-gate * This routine must not get any locks, because other CPUs are paused. 18430Sstevel@tonic-gate */ 18440Sstevel@tonic-gate /* ARGSUSED */ 18450Sstevel@tonic-gate static void 18460Sstevel@tonic-gate cpu_remove_active(cpu_t *cp) 18470Sstevel@tonic-gate { 18480Sstevel@tonic-gate cpupart_t *pp = cp->cpu_part; 18490Sstevel@tonic-gate 18500Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 18510Sstevel@tonic-gate ASSERT(cp->cpu_next_onln != cp); /* not the last one */ 18520Sstevel@tonic-gate ASSERT(cp->cpu_prev_onln != cp); /* not the last one */ 18530Sstevel@tonic-gate 18540Sstevel@tonic-gate chip_cpu_unassign(cp); 18550Sstevel@tonic-gate 18560Sstevel@tonic-gate lgrp_config(LGRP_CONFIG_CPU_OFFLINE, (uintptr_t)cp, 0); 18570Sstevel@tonic-gate 18580Sstevel@tonic-gate cp->cpu_prev_onln->cpu_next_onln = cp->cpu_next_onln; 18590Sstevel@tonic-gate cp->cpu_next_onln->cpu_prev_onln = cp->cpu_prev_onln; 18600Sstevel@tonic-gate if (cpu_active == cp) { 18610Sstevel@tonic-gate cpu_active = cp->cpu_next_onln; 18620Sstevel@tonic-gate } 18630Sstevel@tonic-gate cp->cpu_next_onln = cp; 18640Sstevel@tonic-gate cp->cpu_prev_onln = cp; 18650Sstevel@tonic-gate 18660Sstevel@tonic-gate cp->cpu_prev_part->cpu_next_part = cp->cpu_next_part; 18670Sstevel@tonic-gate cp->cpu_next_part->cpu_prev_part = cp->cpu_prev_part; 18680Sstevel@tonic-gate if (pp->cp_cpulist == cp) { 18690Sstevel@tonic-gate pp->cp_cpulist = cp->cpu_next_part; 18700Sstevel@tonic-gate ASSERT(pp->cp_cpulist != cp); 18710Sstevel@tonic-gate } 18720Sstevel@tonic-gate cp->cpu_next_part = cp; 18730Sstevel@tonic-gate cp->cpu_prev_part = cp; 18740Sstevel@tonic-gate pp->cp_ncpus--; 18750Sstevel@tonic-gate if (pp->cp_ncpus == 0) { 18760Sstevel@tonic-gate cp_numparts_nonempty--; 18770Sstevel@tonic-gate ASSERT(cp_numparts_nonempty != 0); 18780Sstevel@tonic-gate } 18790Sstevel@tonic-gate } 18800Sstevel@tonic-gate 18810Sstevel@tonic-gate /* 18820Sstevel@tonic-gate * Routine used to setup a newly inserted CPU in preparation for starting 18830Sstevel@tonic-gate * it running code. 18840Sstevel@tonic-gate */ 18850Sstevel@tonic-gate int 18860Sstevel@tonic-gate cpu_configure(int cpuid) 18870Sstevel@tonic-gate { 18880Sstevel@tonic-gate int retval = 0; 18890Sstevel@tonic-gate 18900Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 18910Sstevel@tonic-gate 18920Sstevel@tonic-gate /* 18930Sstevel@tonic-gate * Some structures are statically allocated based upon 18940Sstevel@tonic-gate * the maximum number of cpus the system supports. Do not 18950Sstevel@tonic-gate * try to add anything beyond this limit. 18960Sstevel@tonic-gate */ 18970Sstevel@tonic-gate if (cpuid < 0 || cpuid >= NCPU) { 18980Sstevel@tonic-gate return (EINVAL); 18990Sstevel@tonic-gate } 19000Sstevel@tonic-gate 19010Sstevel@tonic-gate if ((cpu[cpuid] != NULL) && (cpu[cpuid]->cpu_flags != 0)) { 19020Sstevel@tonic-gate return (EALREADY); 19030Sstevel@tonic-gate } 19040Sstevel@tonic-gate 19050Sstevel@tonic-gate if ((retval = mp_cpu_configure(cpuid)) != 0) { 19060Sstevel@tonic-gate return (retval); 19070Sstevel@tonic-gate } 19080Sstevel@tonic-gate 19090Sstevel@tonic-gate cpu[cpuid]->cpu_flags = CPU_QUIESCED | CPU_OFFLINE | CPU_POWEROFF; 19100Sstevel@tonic-gate cpu_set_state(cpu[cpuid]); 19110Sstevel@tonic-gate retval = cpu_state_change_hooks(cpuid, CPU_CONFIG, CPU_UNCONFIG); 19120Sstevel@tonic-gate if (retval != 0) 19130Sstevel@tonic-gate (void) mp_cpu_unconfigure(cpuid); 19140Sstevel@tonic-gate 19150Sstevel@tonic-gate return (retval); 19160Sstevel@tonic-gate } 19170Sstevel@tonic-gate 19180Sstevel@tonic-gate /* 19190Sstevel@tonic-gate * Routine used to cleanup a CPU that has been powered off. This will 19200Sstevel@tonic-gate * destroy all per-cpu information related to this cpu. 19210Sstevel@tonic-gate */ 19220Sstevel@tonic-gate int 19230Sstevel@tonic-gate cpu_unconfigure(int cpuid) 19240Sstevel@tonic-gate { 19250Sstevel@tonic-gate int error; 19260Sstevel@tonic-gate 19270Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 19280Sstevel@tonic-gate 19290Sstevel@tonic-gate if (cpu[cpuid] == NULL) { 19300Sstevel@tonic-gate return (ENODEV); 19310Sstevel@tonic-gate } 19320Sstevel@tonic-gate 19330Sstevel@tonic-gate if (cpu[cpuid]->cpu_flags == 0) { 19340Sstevel@tonic-gate return (EALREADY); 19350Sstevel@tonic-gate } 19360Sstevel@tonic-gate 19370Sstevel@tonic-gate if ((cpu[cpuid]->cpu_flags & CPU_POWEROFF) == 0) { 19380Sstevel@tonic-gate return (EBUSY); 19390Sstevel@tonic-gate } 19400Sstevel@tonic-gate 19410Sstevel@tonic-gate if (cpu[cpuid]->cpu_props != NULL) { 19420Sstevel@tonic-gate (void) nvlist_free(cpu[cpuid]->cpu_props); 19430Sstevel@tonic-gate cpu[cpuid]->cpu_props = NULL; 19440Sstevel@tonic-gate } 19450Sstevel@tonic-gate 19460Sstevel@tonic-gate error = cpu_state_change_hooks(cpuid, CPU_UNCONFIG, CPU_CONFIG); 19470Sstevel@tonic-gate 19480Sstevel@tonic-gate if (error != 0) 19490Sstevel@tonic-gate return (error); 19500Sstevel@tonic-gate 19510Sstevel@tonic-gate return (mp_cpu_unconfigure(cpuid)); 19520Sstevel@tonic-gate } 19530Sstevel@tonic-gate 19540Sstevel@tonic-gate /* 19550Sstevel@tonic-gate * Routines for registering and de-registering cpu_setup callback functions. 19560Sstevel@tonic-gate * 19570Sstevel@tonic-gate * Caller's context 19580Sstevel@tonic-gate * These routines must not be called from a driver's attach(9E) or 19590Sstevel@tonic-gate * detach(9E) entry point. 19600Sstevel@tonic-gate * 19610Sstevel@tonic-gate * NOTE: CPU callbacks should not block. They are called with cpu_lock held. 19620Sstevel@tonic-gate */ 19630Sstevel@tonic-gate 19640Sstevel@tonic-gate /* 19650Sstevel@tonic-gate * Ideally, these would be dynamically allocated and put into a linked 19660Sstevel@tonic-gate * list; however that is not feasible because the registration routine 19670Sstevel@tonic-gate * has to be available before the kmem allocator is working (in fact, 19680Sstevel@tonic-gate * it is called by the kmem allocator init code). In any case, there 19690Sstevel@tonic-gate * are quite a few extra entries for future users. 19700Sstevel@tonic-gate */ 19711020Sjkennedy #define NCPU_SETUPS 20 19720Sstevel@tonic-gate 19730Sstevel@tonic-gate struct cpu_setup { 19740Sstevel@tonic-gate cpu_setup_func_t *func; 19750Sstevel@tonic-gate void *arg; 19760Sstevel@tonic-gate } cpu_setups[NCPU_SETUPS]; 19770Sstevel@tonic-gate 19780Sstevel@tonic-gate void 19790Sstevel@tonic-gate register_cpu_setup_func(cpu_setup_func_t *func, void *arg) 19800Sstevel@tonic-gate { 19810Sstevel@tonic-gate int i; 19820Sstevel@tonic-gate 19830Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 19840Sstevel@tonic-gate 19850Sstevel@tonic-gate for (i = 0; i < NCPU_SETUPS; i++) 19860Sstevel@tonic-gate if (cpu_setups[i].func == NULL) 19870Sstevel@tonic-gate break; 19880Sstevel@tonic-gate if (i >= NCPU_SETUPS) 19890Sstevel@tonic-gate cmn_err(CE_PANIC, "Ran out of cpu_setup callback entries"); 19900Sstevel@tonic-gate 19910Sstevel@tonic-gate cpu_setups[i].func = func; 19920Sstevel@tonic-gate cpu_setups[i].arg = arg; 19930Sstevel@tonic-gate } 19940Sstevel@tonic-gate 19950Sstevel@tonic-gate void 19960Sstevel@tonic-gate unregister_cpu_setup_func(cpu_setup_func_t *func, void *arg) 19970Sstevel@tonic-gate { 19980Sstevel@tonic-gate int i; 19990Sstevel@tonic-gate 20000Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 20010Sstevel@tonic-gate 20020Sstevel@tonic-gate for (i = 0; i < NCPU_SETUPS; i++) 20030Sstevel@tonic-gate if ((cpu_setups[i].func == func) && 20040Sstevel@tonic-gate (cpu_setups[i].arg == arg)) 20050Sstevel@tonic-gate break; 20060Sstevel@tonic-gate if (i >= NCPU_SETUPS) 20070Sstevel@tonic-gate cmn_err(CE_PANIC, "Could not find cpu_setup callback to " 20080Sstevel@tonic-gate "deregister"); 20090Sstevel@tonic-gate 20100Sstevel@tonic-gate cpu_setups[i].func = NULL; 20110Sstevel@tonic-gate cpu_setups[i].arg = 0; 20120Sstevel@tonic-gate } 20130Sstevel@tonic-gate 20140Sstevel@tonic-gate /* 20150Sstevel@tonic-gate * Call any state change hooks for this CPU, ignore any errors. 20160Sstevel@tonic-gate */ 20170Sstevel@tonic-gate void 20180Sstevel@tonic-gate cpu_state_change_notify(int id, cpu_setup_t what) 20190Sstevel@tonic-gate { 20200Sstevel@tonic-gate int i; 20210Sstevel@tonic-gate 20220Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 20230Sstevel@tonic-gate 20240Sstevel@tonic-gate for (i = 0; i < NCPU_SETUPS; i++) { 20250Sstevel@tonic-gate if (cpu_setups[i].func != NULL) { 20260Sstevel@tonic-gate cpu_setups[i].func(what, id, cpu_setups[i].arg); 20270Sstevel@tonic-gate } 20280Sstevel@tonic-gate } 20290Sstevel@tonic-gate } 20300Sstevel@tonic-gate 20310Sstevel@tonic-gate /* 20320Sstevel@tonic-gate * Call any state change hooks for this CPU, undo it if error found. 20330Sstevel@tonic-gate */ 20340Sstevel@tonic-gate static int 20350Sstevel@tonic-gate cpu_state_change_hooks(int id, cpu_setup_t what, cpu_setup_t undo) 20360Sstevel@tonic-gate { 20370Sstevel@tonic-gate int i; 20380Sstevel@tonic-gate int retval = 0; 20390Sstevel@tonic-gate 20400Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 20410Sstevel@tonic-gate 20420Sstevel@tonic-gate for (i = 0; i < NCPU_SETUPS; i++) { 20430Sstevel@tonic-gate if (cpu_setups[i].func != NULL) { 20440Sstevel@tonic-gate retval = cpu_setups[i].func(what, id, 20450Sstevel@tonic-gate cpu_setups[i].arg); 20460Sstevel@tonic-gate if (retval) { 20470Sstevel@tonic-gate for (i--; i >= 0; i--) { 20480Sstevel@tonic-gate if (cpu_setups[i].func != NULL) 20490Sstevel@tonic-gate cpu_setups[i].func(undo, 20500Sstevel@tonic-gate id, cpu_setups[i].arg); 20510Sstevel@tonic-gate } 20520Sstevel@tonic-gate break; 20530Sstevel@tonic-gate } 20540Sstevel@tonic-gate } 20550Sstevel@tonic-gate } 20560Sstevel@tonic-gate return (retval); 20570Sstevel@tonic-gate } 20580Sstevel@tonic-gate 20590Sstevel@tonic-gate /* 20600Sstevel@tonic-gate * Export information about this CPU via the kstat mechanism. 20610Sstevel@tonic-gate */ 20620Sstevel@tonic-gate static struct { 20630Sstevel@tonic-gate kstat_named_t ci_state; 20640Sstevel@tonic-gate kstat_named_t ci_state_begin; 20650Sstevel@tonic-gate kstat_named_t ci_cpu_type; 20660Sstevel@tonic-gate kstat_named_t ci_fpu_type; 20670Sstevel@tonic-gate kstat_named_t ci_clock_MHz; 20680Sstevel@tonic-gate kstat_named_t ci_chip_id; 20690Sstevel@tonic-gate kstat_named_t ci_implementation; 20700Sstevel@tonic-gate #ifdef __sparcv9 20710Sstevel@tonic-gate kstat_named_t ci_device_ID; 20720Sstevel@tonic-gate kstat_named_t ci_cpu_fru; 20730Sstevel@tonic-gate #endif 20740Sstevel@tonic-gate kstat_named_t ci_brandstr; 2075*1228Sandrei kstat_named_t ci_core_id; 20760Sstevel@tonic-gate } cpu_info_template = { 20770Sstevel@tonic-gate { "state", KSTAT_DATA_CHAR }, 20780Sstevel@tonic-gate { "state_begin", KSTAT_DATA_LONG }, 20790Sstevel@tonic-gate { "cpu_type", KSTAT_DATA_CHAR }, 20800Sstevel@tonic-gate { "fpu_type", KSTAT_DATA_CHAR }, 20810Sstevel@tonic-gate { "clock_MHz", KSTAT_DATA_LONG }, 20820Sstevel@tonic-gate { "chip_id", KSTAT_DATA_LONG }, 20830Sstevel@tonic-gate { "implementation", KSTAT_DATA_STRING }, 20840Sstevel@tonic-gate #ifdef __sparcv9 20850Sstevel@tonic-gate { "device_ID", KSTAT_DATA_UINT64 }, 20860Sstevel@tonic-gate { "cpu_fru", KSTAT_DATA_STRING }, 20870Sstevel@tonic-gate #endif 20880Sstevel@tonic-gate { "brand", KSTAT_DATA_STRING }, 2089*1228Sandrei { "core_id", KSTAT_DATA_LONG }, 20900Sstevel@tonic-gate }; 20910Sstevel@tonic-gate 20920Sstevel@tonic-gate static kmutex_t cpu_info_template_lock; 20930Sstevel@tonic-gate 20940Sstevel@tonic-gate static int 20950Sstevel@tonic-gate cpu_info_kstat_update(kstat_t *ksp, int rw) 20960Sstevel@tonic-gate { 20970Sstevel@tonic-gate cpu_t *cp = ksp->ks_private; 20980Sstevel@tonic-gate const char *pi_state; 20990Sstevel@tonic-gate 21000Sstevel@tonic-gate if (rw == KSTAT_WRITE) 21010Sstevel@tonic-gate return (EACCES); 21020Sstevel@tonic-gate 21030Sstevel@tonic-gate switch (cp->cpu_type_info.pi_state) { 21040Sstevel@tonic-gate case P_ONLINE: 21050Sstevel@tonic-gate pi_state = PS_ONLINE; 21060Sstevel@tonic-gate break; 21070Sstevel@tonic-gate case P_POWEROFF: 21080Sstevel@tonic-gate pi_state = PS_POWEROFF; 21090Sstevel@tonic-gate break; 21100Sstevel@tonic-gate case P_NOINTR: 21110Sstevel@tonic-gate pi_state = PS_NOINTR; 21120Sstevel@tonic-gate break; 21130Sstevel@tonic-gate case P_FAULTED: 21140Sstevel@tonic-gate pi_state = PS_FAULTED; 21150Sstevel@tonic-gate break; 21160Sstevel@tonic-gate case P_SPARE: 21170Sstevel@tonic-gate pi_state = PS_SPARE; 21180Sstevel@tonic-gate break; 21190Sstevel@tonic-gate case P_OFFLINE: 21200Sstevel@tonic-gate pi_state = PS_OFFLINE; 21210Sstevel@tonic-gate break; 21220Sstevel@tonic-gate default: 21230Sstevel@tonic-gate pi_state = "unknown"; 21240Sstevel@tonic-gate } 21250Sstevel@tonic-gate (void) strcpy(cpu_info_template.ci_state.value.c, pi_state); 21260Sstevel@tonic-gate cpu_info_template.ci_state_begin.value.l = cp->cpu_state_begin; 21270Sstevel@tonic-gate (void) strncpy(cpu_info_template.ci_cpu_type.value.c, 21280Sstevel@tonic-gate cp->cpu_type_info.pi_processor_type, 15); 21290Sstevel@tonic-gate (void) strncpy(cpu_info_template.ci_fpu_type.value.c, 21300Sstevel@tonic-gate cp->cpu_type_info.pi_fputypes, 15); 21310Sstevel@tonic-gate cpu_info_template.ci_clock_MHz.value.l = cp->cpu_type_info.pi_clock; 21320Sstevel@tonic-gate cpu_info_template.ci_chip_id.value.l = chip_plat_get_chipid(cp); 21330Sstevel@tonic-gate kstat_named_setstr(&cpu_info_template.ci_implementation, 21340Sstevel@tonic-gate cp->cpu_idstr); 21350Sstevel@tonic-gate #ifdef __sparcv9 21360Sstevel@tonic-gate cpu_info_template.ci_device_ID.value.ui64 = 21370Sstevel@tonic-gate cpunodes[cp->cpu_id].device_id; 21380Sstevel@tonic-gate kstat_named_setstr(&cpu_info_template.ci_cpu_fru, cpu_fru_fmri(cp)); 21390Sstevel@tonic-gate #endif 21400Sstevel@tonic-gate kstat_named_setstr(&cpu_info_template.ci_brandstr, cp->cpu_brandstr); 2141*1228Sandrei cpu_info_template.ci_core_id.value.l = chip_plat_get_coreid(cp); 21420Sstevel@tonic-gate return (0); 21430Sstevel@tonic-gate } 21440Sstevel@tonic-gate 21450Sstevel@tonic-gate static void 21460Sstevel@tonic-gate cpu_info_kstat_create(cpu_t *cp) 21470Sstevel@tonic-gate { 21480Sstevel@tonic-gate zoneid_t zoneid; 21490Sstevel@tonic-gate 21500Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 21510Sstevel@tonic-gate 21520Sstevel@tonic-gate if (pool_pset_enabled()) 21530Sstevel@tonic-gate zoneid = GLOBAL_ZONEID; 21540Sstevel@tonic-gate else 21550Sstevel@tonic-gate zoneid = ALL_ZONES; 21560Sstevel@tonic-gate if ((cp->cpu_info_kstat = kstat_create_zone("cpu_info", cp->cpu_id, 21570Sstevel@tonic-gate NULL, "misc", KSTAT_TYPE_NAMED, 21580Sstevel@tonic-gate sizeof (cpu_info_template) / sizeof (kstat_named_t), 21590Sstevel@tonic-gate KSTAT_FLAG_VIRTUAL, zoneid)) != NULL) { 21600Sstevel@tonic-gate cp->cpu_info_kstat->ks_data_size += 2 * CPU_IDSTRLEN; 21610Sstevel@tonic-gate #ifdef __sparcv9 21620Sstevel@tonic-gate cp->cpu_info_kstat->ks_data_size += 21630Sstevel@tonic-gate strlen(cpu_fru_fmri(cp)) + 1; 21640Sstevel@tonic-gate #endif 21650Sstevel@tonic-gate cp->cpu_info_kstat->ks_lock = &cpu_info_template_lock; 21660Sstevel@tonic-gate cp->cpu_info_kstat->ks_data = &cpu_info_template; 21670Sstevel@tonic-gate cp->cpu_info_kstat->ks_private = cp; 21680Sstevel@tonic-gate cp->cpu_info_kstat->ks_update = cpu_info_kstat_update; 21690Sstevel@tonic-gate kstat_install(cp->cpu_info_kstat); 21700Sstevel@tonic-gate } 21710Sstevel@tonic-gate } 21720Sstevel@tonic-gate 21730Sstevel@tonic-gate static void 21740Sstevel@tonic-gate cpu_info_kstat_destroy(cpu_t *cp) 21750Sstevel@tonic-gate { 21760Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 21770Sstevel@tonic-gate 21780Sstevel@tonic-gate kstat_delete(cp->cpu_info_kstat); 21790Sstevel@tonic-gate cp->cpu_info_kstat = NULL; 21800Sstevel@tonic-gate } 21810Sstevel@tonic-gate 21820Sstevel@tonic-gate /* 21830Sstevel@tonic-gate * Create and install kstats for the boot CPU. 21840Sstevel@tonic-gate */ 21850Sstevel@tonic-gate void 21860Sstevel@tonic-gate cpu_kstat_init(cpu_t *cp) 21870Sstevel@tonic-gate { 21880Sstevel@tonic-gate mutex_enter(&cpu_lock); 21890Sstevel@tonic-gate cpu_info_kstat_create(cp); 21900Sstevel@tonic-gate cpu_stats_kstat_create(cp); 21910Sstevel@tonic-gate cpu_create_intrstat(cp); 21920Sstevel@tonic-gate chip_kstat_create(cp->cpu_chip); 21930Sstevel@tonic-gate cpu_set_state(cp); 21940Sstevel@tonic-gate mutex_exit(&cpu_lock); 21950Sstevel@tonic-gate } 21960Sstevel@tonic-gate 21970Sstevel@tonic-gate /* 21980Sstevel@tonic-gate * Make visible to the zone that subset of the cpu information that would be 21990Sstevel@tonic-gate * initialized when a cpu is configured (but still offline). 22000Sstevel@tonic-gate */ 22010Sstevel@tonic-gate void 22020Sstevel@tonic-gate cpu_visibility_configure(cpu_t *cp, zone_t *zone) 22030Sstevel@tonic-gate { 22040Sstevel@tonic-gate zoneid_t zoneid = zone ? zone->zone_id : ALL_ZONES; 22050Sstevel@tonic-gate 22060Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 22070Sstevel@tonic-gate ASSERT(pool_pset_enabled()); 22080Sstevel@tonic-gate ASSERT(cp != NULL); 22090Sstevel@tonic-gate 22100Sstevel@tonic-gate if (zoneid != ALL_ZONES && zoneid != GLOBAL_ZONEID) { 22110Sstevel@tonic-gate zone->zone_ncpus++; 22120Sstevel@tonic-gate ASSERT(zone->zone_ncpus <= ncpus); 22130Sstevel@tonic-gate } 22140Sstevel@tonic-gate if (cp->cpu_info_kstat != NULL) 22150Sstevel@tonic-gate kstat_zone_add(cp->cpu_info_kstat, zoneid); 22160Sstevel@tonic-gate } 22170Sstevel@tonic-gate 22180Sstevel@tonic-gate /* 22190Sstevel@tonic-gate * Make visible to the zone that subset of the cpu information that would be 22200Sstevel@tonic-gate * initialized when a previously configured cpu is onlined. 22210Sstevel@tonic-gate */ 22220Sstevel@tonic-gate void 22230Sstevel@tonic-gate cpu_visibility_online(cpu_t *cp, zone_t *zone) 22240Sstevel@tonic-gate { 22250Sstevel@tonic-gate kstat_t *ksp; 22260Sstevel@tonic-gate char name[sizeof ("cpu_stat") + 10]; /* enough for 32-bit cpuids */ 22270Sstevel@tonic-gate zoneid_t zoneid = zone ? zone->zone_id : ALL_ZONES; 22280Sstevel@tonic-gate processorid_t cpun; 22290Sstevel@tonic-gate 22300Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 22310Sstevel@tonic-gate ASSERT(pool_pset_enabled()); 22320Sstevel@tonic-gate ASSERT(cp != NULL); 22330Sstevel@tonic-gate ASSERT(cpu_is_active(cp)); 22340Sstevel@tonic-gate 22350Sstevel@tonic-gate cpun = cp->cpu_id; 22360Sstevel@tonic-gate if (zoneid != ALL_ZONES && zoneid != GLOBAL_ZONEID) { 22370Sstevel@tonic-gate zone->zone_ncpus_online++; 22380Sstevel@tonic-gate ASSERT(zone->zone_ncpus_online <= ncpus_online); 22390Sstevel@tonic-gate } 22400Sstevel@tonic-gate (void) snprintf(name, sizeof (name), "cpu_stat%d", cpun); 22410Sstevel@tonic-gate if ((ksp = kstat_hold_byname("cpu_stat", cpun, name, ALL_ZONES)) 22420Sstevel@tonic-gate != NULL) { 22430Sstevel@tonic-gate kstat_zone_add(ksp, zoneid); 22440Sstevel@tonic-gate kstat_rele(ksp); 22450Sstevel@tonic-gate } 22460Sstevel@tonic-gate if ((ksp = kstat_hold_byname("cpu", cpun, "sys", ALL_ZONES)) != NULL) { 22470Sstevel@tonic-gate kstat_zone_add(ksp, zoneid); 22480Sstevel@tonic-gate kstat_rele(ksp); 22490Sstevel@tonic-gate } 22500Sstevel@tonic-gate if ((ksp = kstat_hold_byname("cpu", cpun, "vm", ALL_ZONES)) != NULL) { 22510Sstevel@tonic-gate kstat_zone_add(ksp, zoneid); 22520Sstevel@tonic-gate kstat_rele(ksp); 22530Sstevel@tonic-gate } 22540Sstevel@tonic-gate if ((ksp = kstat_hold_byname("cpu", cpun, "intrstat", ALL_ZONES)) != 22550Sstevel@tonic-gate NULL) { 22560Sstevel@tonic-gate kstat_zone_add(ksp, zoneid); 22570Sstevel@tonic-gate kstat_rele(ksp); 22580Sstevel@tonic-gate } 22590Sstevel@tonic-gate } 22600Sstevel@tonic-gate 22610Sstevel@tonic-gate /* 22620Sstevel@tonic-gate * Update relevant kstats such that cpu is now visible to processes 22630Sstevel@tonic-gate * executing in specified zone. 22640Sstevel@tonic-gate */ 22650Sstevel@tonic-gate void 22660Sstevel@tonic-gate cpu_visibility_add(cpu_t *cp, zone_t *zone) 22670Sstevel@tonic-gate { 22680Sstevel@tonic-gate cpu_visibility_configure(cp, zone); 22690Sstevel@tonic-gate if (cpu_is_active(cp)) 22700Sstevel@tonic-gate cpu_visibility_online(cp, zone); 22710Sstevel@tonic-gate } 22720Sstevel@tonic-gate 22730Sstevel@tonic-gate /* 22740Sstevel@tonic-gate * Make invisible to the zone that subset of the cpu information that would be 22750Sstevel@tonic-gate * torn down when a previously offlined cpu is unconfigured. 22760Sstevel@tonic-gate */ 22770Sstevel@tonic-gate void 22780Sstevel@tonic-gate cpu_visibility_unconfigure(cpu_t *cp, zone_t *zone) 22790Sstevel@tonic-gate { 22800Sstevel@tonic-gate zoneid_t zoneid = zone ? zone->zone_id : ALL_ZONES; 22810Sstevel@tonic-gate 22820Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 22830Sstevel@tonic-gate ASSERT(pool_pset_enabled()); 22840Sstevel@tonic-gate ASSERT(cp != NULL); 22850Sstevel@tonic-gate 22860Sstevel@tonic-gate if (zoneid != ALL_ZONES && zoneid != GLOBAL_ZONEID) { 22870Sstevel@tonic-gate ASSERT(zone->zone_ncpus != 0); 22880Sstevel@tonic-gate zone->zone_ncpus--; 22890Sstevel@tonic-gate } 22900Sstevel@tonic-gate if (cp->cpu_info_kstat) 22910Sstevel@tonic-gate kstat_zone_remove(cp->cpu_info_kstat, zoneid); 22920Sstevel@tonic-gate } 22930Sstevel@tonic-gate 22940Sstevel@tonic-gate /* 22950Sstevel@tonic-gate * Make invisible to the zone that subset of the cpu information that would be 22960Sstevel@tonic-gate * torn down when a cpu is offlined (but still configured). 22970Sstevel@tonic-gate */ 22980Sstevel@tonic-gate void 22990Sstevel@tonic-gate cpu_visibility_offline(cpu_t *cp, zone_t *zone) 23000Sstevel@tonic-gate { 23010Sstevel@tonic-gate kstat_t *ksp; 23020Sstevel@tonic-gate char name[sizeof ("cpu_stat") + 10]; /* enough for 32-bit cpuids */ 23030Sstevel@tonic-gate zoneid_t zoneid = zone ? zone->zone_id : ALL_ZONES; 23040Sstevel@tonic-gate processorid_t cpun; 23050Sstevel@tonic-gate 23060Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 23070Sstevel@tonic-gate ASSERT(pool_pset_enabled()); 23080Sstevel@tonic-gate ASSERT(cp != NULL); 23090Sstevel@tonic-gate ASSERT(cpu_is_active(cp)); 23100Sstevel@tonic-gate 23110Sstevel@tonic-gate cpun = cp->cpu_id; 23120Sstevel@tonic-gate if (zoneid != ALL_ZONES && zoneid != GLOBAL_ZONEID) { 23130Sstevel@tonic-gate ASSERT(zone->zone_ncpus_online != 0); 23140Sstevel@tonic-gate zone->zone_ncpus_online--; 23150Sstevel@tonic-gate } 23160Sstevel@tonic-gate 23170Sstevel@tonic-gate if ((ksp = kstat_hold_byname("cpu", cpun, "intrstat", ALL_ZONES)) != 23180Sstevel@tonic-gate NULL) { 23190Sstevel@tonic-gate kstat_zone_remove(ksp, zoneid); 23200Sstevel@tonic-gate kstat_rele(ksp); 23210Sstevel@tonic-gate } 23220Sstevel@tonic-gate if ((ksp = kstat_hold_byname("cpu", cpun, "vm", ALL_ZONES)) != NULL) { 23230Sstevel@tonic-gate kstat_zone_remove(ksp, zoneid); 23240Sstevel@tonic-gate kstat_rele(ksp); 23250Sstevel@tonic-gate } 23260Sstevel@tonic-gate if ((ksp = kstat_hold_byname("cpu", cpun, "sys", ALL_ZONES)) != NULL) { 23270Sstevel@tonic-gate kstat_zone_remove(ksp, zoneid); 23280Sstevel@tonic-gate kstat_rele(ksp); 23290Sstevel@tonic-gate } 23300Sstevel@tonic-gate (void) snprintf(name, sizeof (name), "cpu_stat%d", cpun); 23310Sstevel@tonic-gate if ((ksp = kstat_hold_byname("cpu_stat", cpun, name, ALL_ZONES)) 23320Sstevel@tonic-gate != NULL) { 23330Sstevel@tonic-gate kstat_zone_remove(ksp, zoneid); 23340Sstevel@tonic-gate kstat_rele(ksp); 23350Sstevel@tonic-gate } 23360Sstevel@tonic-gate } 23370Sstevel@tonic-gate 23380Sstevel@tonic-gate /* 23390Sstevel@tonic-gate * Update relevant kstats such that cpu is no longer visible to processes 23400Sstevel@tonic-gate * executing in specified zone. 23410Sstevel@tonic-gate */ 23420Sstevel@tonic-gate void 23430Sstevel@tonic-gate cpu_visibility_remove(cpu_t *cp, zone_t *zone) 23440Sstevel@tonic-gate { 23450Sstevel@tonic-gate if (cpu_is_active(cp)) 23460Sstevel@tonic-gate cpu_visibility_offline(cp, zone); 23470Sstevel@tonic-gate cpu_visibility_unconfigure(cp, zone); 23480Sstevel@tonic-gate } 23490Sstevel@tonic-gate 23500Sstevel@tonic-gate /* 23510Sstevel@tonic-gate * Bind a thread to a CPU as requested. 23520Sstevel@tonic-gate */ 23530Sstevel@tonic-gate int 23540Sstevel@tonic-gate cpu_bind_thread(kthread_id_t tp, processorid_t bind, processorid_t *obind, 23550Sstevel@tonic-gate int *error) 23560Sstevel@tonic-gate { 23570Sstevel@tonic-gate processorid_t binding; 23580Sstevel@tonic-gate cpu_t *cp; 23590Sstevel@tonic-gate 23600Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 23610Sstevel@tonic-gate ASSERT(MUTEX_HELD(&ttoproc(tp)->p_lock)); 23620Sstevel@tonic-gate 23630Sstevel@tonic-gate thread_lock(tp); 23640Sstevel@tonic-gate 23650Sstevel@tonic-gate /* 23660Sstevel@tonic-gate * Record old binding, but change the obind, which was initialized 23670Sstevel@tonic-gate * to PBIND_NONE, only if this thread has a binding. This avoids 23680Sstevel@tonic-gate * reporting PBIND_NONE for a process when some LWPs are bound. 23690Sstevel@tonic-gate */ 23700Sstevel@tonic-gate binding = tp->t_bind_cpu; 23710Sstevel@tonic-gate if (binding != PBIND_NONE) 23720Sstevel@tonic-gate *obind = binding; /* record old binding */ 23730Sstevel@tonic-gate 23740Sstevel@tonic-gate if (bind == PBIND_QUERY) { 23750Sstevel@tonic-gate thread_unlock(tp); 23760Sstevel@tonic-gate return (0); 23770Sstevel@tonic-gate } 23780Sstevel@tonic-gate 23790Sstevel@tonic-gate /* 23800Sstevel@tonic-gate * If this thread/LWP cannot be bound because of permission 23810Sstevel@tonic-gate * problems, just note that and return success so that the 23820Sstevel@tonic-gate * other threads/LWPs will be bound. This is the way 23830Sstevel@tonic-gate * processor_bind() is defined to work. 23840Sstevel@tonic-gate * 23850Sstevel@tonic-gate * Binding will get EPERM if the thread is of system class 23860Sstevel@tonic-gate * or hasprocperm() fails. 23870Sstevel@tonic-gate */ 23880Sstevel@tonic-gate if (tp->t_cid == 0 || !hasprocperm(tp->t_cred, CRED())) { 23890Sstevel@tonic-gate *error = EPERM; 23900Sstevel@tonic-gate thread_unlock(tp); 23910Sstevel@tonic-gate return (0); 23920Sstevel@tonic-gate } 23930Sstevel@tonic-gate 23940Sstevel@tonic-gate binding = bind; 23950Sstevel@tonic-gate if (binding != PBIND_NONE) { 23960Sstevel@tonic-gate cp = cpu[binding]; 23970Sstevel@tonic-gate /* 23980Sstevel@tonic-gate * Make sure binding is in right partition. 23990Sstevel@tonic-gate */ 24000Sstevel@tonic-gate if (tp->t_cpupart != cp->cpu_part) { 24010Sstevel@tonic-gate *error = EINVAL; 24020Sstevel@tonic-gate thread_unlock(tp); 24030Sstevel@tonic-gate return (0); 24040Sstevel@tonic-gate } 24050Sstevel@tonic-gate } 24060Sstevel@tonic-gate tp->t_bind_cpu = binding; /* set new binding */ 24070Sstevel@tonic-gate 24080Sstevel@tonic-gate /* 24090Sstevel@tonic-gate * If there is no system-set reason for affinity, set 24100Sstevel@tonic-gate * the t_bound_cpu field to reflect the binding. 24110Sstevel@tonic-gate */ 24120Sstevel@tonic-gate if (tp->t_affinitycnt == 0) { 24130Sstevel@tonic-gate if (binding == PBIND_NONE) { 24140Sstevel@tonic-gate /* 24150Sstevel@tonic-gate * We may need to adjust disp_max_unbound_pri 24160Sstevel@tonic-gate * since we're becoming unbound. 24170Sstevel@tonic-gate */ 24180Sstevel@tonic-gate disp_adjust_unbound_pri(tp); 24190Sstevel@tonic-gate 24200Sstevel@tonic-gate tp->t_bound_cpu = NULL; /* set new binding */ 24210Sstevel@tonic-gate 24220Sstevel@tonic-gate /* 24230Sstevel@tonic-gate * Move thread to lgroup with strongest affinity 24240Sstevel@tonic-gate * after unbinding 24250Sstevel@tonic-gate */ 24260Sstevel@tonic-gate if (tp->t_lgrp_affinity) 24270Sstevel@tonic-gate lgrp_move_thread(tp, 24280Sstevel@tonic-gate lgrp_choose(tp, tp->t_cpupart), 1); 24290Sstevel@tonic-gate 24300Sstevel@tonic-gate if (tp->t_state == TS_ONPROC && 24310Sstevel@tonic-gate tp->t_cpu->cpu_part != tp->t_cpupart) 24320Sstevel@tonic-gate cpu_surrender(tp); 24330Sstevel@tonic-gate } else { 24340Sstevel@tonic-gate lpl_t *lpl; 24350Sstevel@tonic-gate 24360Sstevel@tonic-gate tp->t_bound_cpu = cp; 24370Sstevel@tonic-gate ASSERT(cp->cpu_lpl != NULL); 24380Sstevel@tonic-gate 24390Sstevel@tonic-gate /* 24400Sstevel@tonic-gate * Set home to lgroup with most affinity containing CPU 24410Sstevel@tonic-gate * that thread is being bound or minimum bounding 24420Sstevel@tonic-gate * lgroup if no affinities set 24430Sstevel@tonic-gate */ 24440Sstevel@tonic-gate if (tp->t_lgrp_affinity) 24450Sstevel@tonic-gate lpl = lgrp_affinity_best(tp, tp->t_cpupart, 0); 24460Sstevel@tonic-gate else 24470Sstevel@tonic-gate lpl = cp->cpu_lpl; 24480Sstevel@tonic-gate 24490Sstevel@tonic-gate if (tp->t_lpl != lpl) { 24500Sstevel@tonic-gate /* can't grab cpu_lock */ 24510Sstevel@tonic-gate lgrp_move_thread(tp, lpl, 1); 24520Sstevel@tonic-gate } 24530Sstevel@tonic-gate 24540Sstevel@tonic-gate /* 24550Sstevel@tonic-gate * Make the thread switch to the bound CPU. 24560Sstevel@tonic-gate * If the thread is runnable, we need to 24570Sstevel@tonic-gate * requeue it even if t_cpu is already set 24580Sstevel@tonic-gate * to the right CPU, since it may be on a 24590Sstevel@tonic-gate * kpreempt queue and need to move to a local 24600Sstevel@tonic-gate * queue. We could check t_disp_queue to 24610Sstevel@tonic-gate * avoid unnecessary overhead if it's already 24620Sstevel@tonic-gate * on the right queue, but since this isn't 24630Sstevel@tonic-gate * a performance-critical operation it doesn't 24640Sstevel@tonic-gate * seem worth the extra code and complexity. 24650Sstevel@tonic-gate * 24660Sstevel@tonic-gate * If the thread is weakbound to the cpu then it will 24670Sstevel@tonic-gate * resist the new binding request until the weak 24680Sstevel@tonic-gate * binding drops. The cpu_surrender or requeueing 24690Sstevel@tonic-gate * below could be skipped in such cases (since it 24700Sstevel@tonic-gate * will have no effect), but that would require 24710Sstevel@tonic-gate * thread_allowmigrate to acquire thread_lock so 24720Sstevel@tonic-gate * we'll take the very occasional hit here instead. 24730Sstevel@tonic-gate */ 24740Sstevel@tonic-gate if (tp->t_state == TS_ONPROC) { 24750Sstevel@tonic-gate cpu_surrender(tp); 24760Sstevel@tonic-gate } else if (tp->t_state == TS_RUN) { 24770Sstevel@tonic-gate cpu_t *ocp = tp->t_cpu; 24780Sstevel@tonic-gate 24790Sstevel@tonic-gate (void) dispdeq(tp); 24800Sstevel@tonic-gate setbackdq(tp); 24810Sstevel@tonic-gate /* 24820Sstevel@tonic-gate * Either on the bound CPU's disp queue now, 24830Sstevel@tonic-gate * or swapped out or on the swap queue. 24840Sstevel@tonic-gate */ 24850Sstevel@tonic-gate ASSERT(tp->t_disp_queue == cp->cpu_disp || 24860Sstevel@tonic-gate tp->t_weakbound_cpu == ocp || 24870Sstevel@tonic-gate (tp->t_schedflag & (TS_LOAD | TS_ON_SWAPQ)) 24880Sstevel@tonic-gate != TS_LOAD); 24890Sstevel@tonic-gate } 24900Sstevel@tonic-gate } 24910Sstevel@tonic-gate } 24920Sstevel@tonic-gate 24930Sstevel@tonic-gate /* 24940Sstevel@tonic-gate * Our binding has changed; set TP_CHANGEBIND. 24950Sstevel@tonic-gate */ 24960Sstevel@tonic-gate tp->t_proc_flag |= TP_CHANGEBIND; 24970Sstevel@tonic-gate aston(tp); 24980Sstevel@tonic-gate 24990Sstevel@tonic-gate thread_unlock(tp); 25000Sstevel@tonic-gate 25010Sstevel@tonic-gate return (0); 25020Sstevel@tonic-gate } 25030Sstevel@tonic-gate 25040Sstevel@tonic-gate #if CPUSET_WORDS > 1 25050Sstevel@tonic-gate 25060Sstevel@tonic-gate /* 25070Sstevel@tonic-gate * Functions for implementing cpuset operations when a cpuset is more 25080Sstevel@tonic-gate * than one word. On platforms where a cpuset is a single word these 25090Sstevel@tonic-gate * are implemented as macros in cpuvar.h. 25100Sstevel@tonic-gate */ 25110Sstevel@tonic-gate 25120Sstevel@tonic-gate void 25130Sstevel@tonic-gate cpuset_all(cpuset_t *s) 25140Sstevel@tonic-gate { 25150Sstevel@tonic-gate int i; 25160Sstevel@tonic-gate 25170Sstevel@tonic-gate for (i = 0; i < CPUSET_WORDS; i++) 25180Sstevel@tonic-gate s->cpub[i] = ~0UL; 25190Sstevel@tonic-gate } 25200Sstevel@tonic-gate 25210Sstevel@tonic-gate void 25220Sstevel@tonic-gate cpuset_all_but(cpuset_t *s, uint_t cpu) 25230Sstevel@tonic-gate { 25240Sstevel@tonic-gate cpuset_all(s); 25250Sstevel@tonic-gate CPUSET_DEL(*s, cpu); 25260Sstevel@tonic-gate } 25270Sstevel@tonic-gate 25280Sstevel@tonic-gate void 25290Sstevel@tonic-gate cpuset_only(cpuset_t *s, uint_t cpu) 25300Sstevel@tonic-gate { 25310Sstevel@tonic-gate CPUSET_ZERO(*s); 25320Sstevel@tonic-gate CPUSET_ADD(*s, cpu); 25330Sstevel@tonic-gate } 25340Sstevel@tonic-gate 25350Sstevel@tonic-gate int 25360Sstevel@tonic-gate cpuset_isnull(cpuset_t *s) 25370Sstevel@tonic-gate { 25380Sstevel@tonic-gate int i; 25390Sstevel@tonic-gate 25400Sstevel@tonic-gate for (i = 0; i < CPUSET_WORDS; i++) 25410Sstevel@tonic-gate if (s->cpub[i] != 0) 25420Sstevel@tonic-gate return (0); 25430Sstevel@tonic-gate return (1); 25440Sstevel@tonic-gate } 25450Sstevel@tonic-gate 25460Sstevel@tonic-gate int 25470Sstevel@tonic-gate cpuset_cmp(cpuset_t *s1, cpuset_t *s2) 25480Sstevel@tonic-gate { 25490Sstevel@tonic-gate int i; 25500Sstevel@tonic-gate 25510Sstevel@tonic-gate for (i = 0; i < CPUSET_WORDS; i++) 25520Sstevel@tonic-gate if (s1->cpub[i] != s2->cpub[i]) 25530Sstevel@tonic-gate return (0); 25540Sstevel@tonic-gate return (1); 25550Sstevel@tonic-gate } 25560Sstevel@tonic-gate 25570Sstevel@tonic-gate uint_t 25580Sstevel@tonic-gate cpuset_find(cpuset_t *s) 25590Sstevel@tonic-gate { 25600Sstevel@tonic-gate 25610Sstevel@tonic-gate uint_t i; 25620Sstevel@tonic-gate uint_t cpu = (uint_t)-1; 25630Sstevel@tonic-gate 25640Sstevel@tonic-gate /* 25650Sstevel@tonic-gate * Find a cpu in the cpuset 25660Sstevel@tonic-gate */ 25670Sstevel@tonic-gate for (i = 0; (i < CPUSET_WORDS && cpu == (uint_t)-1); i++) 25680Sstevel@tonic-gate cpu = (uint_t)(lowbit(s->cpub[i]) - 1); 25690Sstevel@tonic-gate return (cpu); 25700Sstevel@tonic-gate } 25710Sstevel@tonic-gate 25720Sstevel@tonic-gate #endif /* CPUSET_WORDS */ 25730Sstevel@tonic-gate 25740Sstevel@tonic-gate /* 25750Sstevel@tonic-gate * Unbind all user threads bound to a given CPU. 25760Sstevel@tonic-gate */ 25770Sstevel@tonic-gate int 25780Sstevel@tonic-gate cpu_unbind(processorid_t cpu) 25790Sstevel@tonic-gate { 25800Sstevel@tonic-gate processorid_t obind; 25810Sstevel@tonic-gate kthread_t *tp; 25820Sstevel@tonic-gate int ret = 0; 25830Sstevel@tonic-gate proc_t *pp; 25840Sstevel@tonic-gate int err, berr = 0; 25850Sstevel@tonic-gate 25860Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 25870Sstevel@tonic-gate 25880Sstevel@tonic-gate mutex_enter(&pidlock); 25890Sstevel@tonic-gate for (pp = practive; pp != NULL; pp = pp->p_next) { 25900Sstevel@tonic-gate mutex_enter(&pp->p_lock); 25910Sstevel@tonic-gate tp = pp->p_tlist; 25920Sstevel@tonic-gate /* 25930Sstevel@tonic-gate * Skip zombies, kernel processes, and processes in 25940Sstevel@tonic-gate * other zones, if called from a non-global zone. 25950Sstevel@tonic-gate */ 25960Sstevel@tonic-gate if (tp == NULL || (pp->p_flag & SSYS) || 25970Sstevel@tonic-gate !HASZONEACCESS(curproc, pp->p_zone->zone_id)) { 25980Sstevel@tonic-gate mutex_exit(&pp->p_lock); 25990Sstevel@tonic-gate continue; 26000Sstevel@tonic-gate } 26010Sstevel@tonic-gate do { 26020Sstevel@tonic-gate if (tp->t_bind_cpu != cpu) 26030Sstevel@tonic-gate continue; 26040Sstevel@tonic-gate err = cpu_bind_thread(tp, PBIND_NONE, &obind, &berr); 26050Sstevel@tonic-gate if (ret == 0) 26060Sstevel@tonic-gate ret = err; 26070Sstevel@tonic-gate } while ((tp = tp->t_forw) != pp->p_tlist); 26080Sstevel@tonic-gate mutex_exit(&pp->p_lock); 26090Sstevel@tonic-gate } 26100Sstevel@tonic-gate mutex_exit(&pidlock); 26110Sstevel@tonic-gate if (ret == 0) 26120Sstevel@tonic-gate ret = berr; 26130Sstevel@tonic-gate return (ret); 26140Sstevel@tonic-gate } 26150Sstevel@tonic-gate 26160Sstevel@tonic-gate 26170Sstevel@tonic-gate /* 26180Sstevel@tonic-gate * Destroy all remaining bound threads on a cpu. 26190Sstevel@tonic-gate */ 26200Sstevel@tonic-gate void 26210Sstevel@tonic-gate cpu_destroy_bound_threads(cpu_t *cp) 26220Sstevel@tonic-gate { 26230Sstevel@tonic-gate extern id_t syscid; 26240Sstevel@tonic-gate register kthread_id_t t, tlist, tnext; 26250Sstevel@tonic-gate 26260Sstevel@tonic-gate /* 26270Sstevel@tonic-gate * Destroy all remaining bound threads on the cpu. This 26280Sstevel@tonic-gate * should include both the interrupt threads and the idle thread. 26290Sstevel@tonic-gate * This requires some care, since we need to traverse the 26300Sstevel@tonic-gate * thread list with the pidlock mutex locked, but thread_free 26310Sstevel@tonic-gate * also locks the pidlock mutex. So, we collect the threads 26320Sstevel@tonic-gate * we're going to reap in a list headed by "tlist", then we 26330Sstevel@tonic-gate * unlock the pidlock mutex and traverse the tlist list, 26340Sstevel@tonic-gate * doing thread_free's on the thread's. Simple, n'est pas? 26350Sstevel@tonic-gate * Also, this depends on thread_free not mucking with the 26360Sstevel@tonic-gate * t_next and t_prev links of the thread. 26370Sstevel@tonic-gate */ 26380Sstevel@tonic-gate 26390Sstevel@tonic-gate if ((t = curthread) != NULL) { 26400Sstevel@tonic-gate 26410Sstevel@tonic-gate tlist = NULL; 26420Sstevel@tonic-gate mutex_enter(&pidlock); 26430Sstevel@tonic-gate do { 26440Sstevel@tonic-gate tnext = t->t_next; 26450Sstevel@tonic-gate if (t->t_bound_cpu == cp) { 26460Sstevel@tonic-gate 26470Sstevel@tonic-gate /* 26480Sstevel@tonic-gate * We've found a bound thread, carefully unlink 26490Sstevel@tonic-gate * it out of the thread list, and add it to 26500Sstevel@tonic-gate * our "tlist". We "know" we don't have to 26510Sstevel@tonic-gate * worry about unlinking curthread (the thread 26520Sstevel@tonic-gate * that is executing this code). 26530Sstevel@tonic-gate */ 26540Sstevel@tonic-gate t->t_next->t_prev = t->t_prev; 26550Sstevel@tonic-gate t->t_prev->t_next = t->t_next; 26560Sstevel@tonic-gate t->t_next = tlist; 26570Sstevel@tonic-gate tlist = t; 26580Sstevel@tonic-gate ASSERT(t->t_cid == syscid); 26590Sstevel@tonic-gate /* wake up anyone blocked in thread_join */ 26600Sstevel@tonic-gate cv_broadcast(&t->t_joincv); 26610Sstevel@tonic-gate /* 26620Sstevel@tonic-gate * t_lwp set by interrupt threads and not 26630Sstevel@tonic-gate * cleared. 26640Sstevel@tonic-gate */ 26650Sstevel@tonic-gate t->t_lwp = NULL; 26660Sstevel@tonic-gate /* 26670Sstevel@tonic-gate * Pause and idle threads always have 26680Sstevel@tonic-gate * t_state set to TS_ONPROC. 26690Sstevel@tonic-gate */ 26700Sstevel@tonic-gate t->t_state = TS_FREE; 26710Sstevel@tonic-gate t->t_prev = NULL; /* Just in case */ 26720Sstevel@tonic-gate } 26730Sstevel@tonic-gate 26740Sstevel@tonic-gate } while ((t = tnext) != curthread); 26750Sstevel@tonic-gate 26760Sstevel@tonic-gate mutex_exit(&pidlock); 26770Sstevel@tonic-gate 26780Sstevel@tonic-gate 26790Sstevel@tonic-gate for (t = tlist; t != NULL; t = tnext) { 26800Sstevel@tonic-gate tnext = t->t_next; 26810Sstevel@tonic-gate thread_free(t); 26820Sstevel@tonic-gate } 26830Sstevel@tonic-gate } 26840Sstevel@tonic-gate } 26850Sstevel@tonic-gate 26860Sstevel@tonic-gate /* 26870Sstevel@tonic-gate * processor_info(2) and p_online(2) status support functions 26880Sstevel@tonic-gate * The constants returned by the cpu_get_state() and cpu_get_state_str() are 26890Sstevel@tonic-gate * for use in communicating processor state information to userland. Kernel 26900Sstevel@tonic-gate * subsystems should only be using the cpu_flags value directly. Subsystems 26910Sstevel@tonic-gate * modifying cpu_flags should record the state change via a call to the 26920Sstevel@tonic-gate * cpu_set_state(). 26930Sstevel@tonic-gate */ 26940Sstevel@tonic-gate 26950Sstevel@tonic-gate /* 26960Sstevel@tonic-gate * Update the pi_state of this CPU. This function provides the CPU status for 26970Sstevel@tonic-gate * the information returned by processor_info(2). 26980Sstevel@tonic-gate */ 26990Sstevel@tonic-gate void 27000Sstevel@tonic-gate cpu_set_state(cpu_t *cpu) 27010Sstevel@tonic-gate { 27020Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 27030Sstevel@tonic-gate cpu->cpu_type_info.pi_state = cpu_get_state(cpu); 27040Sstevel@tonic-gate cpu->cpu_state_begin = gethrestime_sec(); 27050Sstevel@tonic-gate pool_cpu_mod = gethrtime(); 27060Sstevel@tonic-gate } 27070Sstevel@tonic-gate 27080Sstevel@tonic-gate /* 27090Sstevel@tonic-gate * Return offline/online/other status for the indicated CPU. Use only for 27100Sstevel@tonic-gate * communication with user applications; cpu_flags provides the in-kernel 27110Sstevel@tonic-gate * interface. 27120Sstevel@tonic-gate */ 27130Sstevel@tonic-gate int 27140Sstevel@tonic-gate cpu_get_state(cpu_t *cpu) 27150Sstevel@tonic-gate { 27160Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 27170Sstevel@tonic-gate if (cpu->cpu_flags & CPU_POWEROFF) 27180Sstevel@tonic-gate return (P_POWEROFF); 27190Sstevel@tonic-gate else if (cpu->cpu_flags & CPU_FAULTED) 27200Sstevel@tonic-gate return (P_FAULTED); 27210Sstevel@tonic-gate else if (cpu->cpu_flags & CPU_SPARE) 27220Sstevel@tonic-gate return (P_SPARE); 27230Sstevel@tonic-gate else if ((cpu->cpu_flags & (CPU_READY | CPU_OFFLINE)) != CPU_READY) 27240Sstevel@tonic-gate return (P_OFFLINE); 27250Sstevel@tonic-gate else if (cpu->cpu_flags & CPU_ENABLE) 27260Sstevel@tonic-gate return (P_ONLINE); 27270Sstevel@tonic-gate else 27280Sstevel@tonic-gate return (P_NOINTR); 27290Sstevel@tonic-gate } 27300Sstevel@tonic-gate 27310Sstevel@tonic-gate /* 27320Sstevel@tonic-gate * Return processor_info(2) state as a string. 27330Sstevel@tonic-gate */ 27340Sstevel@tonic-gate const char * 27350Sstevel@tonic-gate cpu_get_state_str(cpu_t *cpu) 27360Sstevel@tonic-gate { 27370Sstevel@tonic-gate const char *string; 27380Sstevel@tonic-gate 27390Sstevel@tonic-gate switch (cpu_get_state(cpu)) { 27400Sstevel@tonic-gate case P_ONLINE: 27410Sstevel@tonic-gate string = PS_ONLINE; 27420Sstevel@tonic-gate break; 27430Sstevel@tonic-gate case P_POWEROFF: 27440Sstevel@tonic-gate string = PS_POWEROFF; 27450Sstevel@tonic-gate break; 27460Sstevel@tonic-gate case P_NOINTR: 27470Sstevel@tonic-gate string = PS_NOINTR; 27480Sstevel@tonic-gate break; 27490Sstevel@tonic-gate case P_SPARE: 27500Sstevel@tonic-gate string = PS_SPARE; 27510Sstevel@tonic-gate break; 27520Sstevel@tonic-gate case P_FAULTED: 27530Sstevel@tonic-gate string = PS_FAULTED; 27540Sstevel@tonic-gate break; 27550Sstevel@tonic-gate case P_OFFLINE: 27560Sstevel@tonic-gate string = PS_OFFLINE; 27570Sstevel@tonic-gate break; 27580Sstevel@tonic-gate default: 27590Sstevel@tonic-gate string = "unknown"; 27600Sstevel@tonic-gate break; 27610Sstevel@tonic-gate } 27620Sstevel@tonic-gate return (string); 27630Sstevel@tonic-gate } 27640Sstevel@tonic-gate 27650Sstevel@tonic-gate /* 27660Sstevel@tonic-gate * Export this CPU's statistics (cpu_stat_t and cpu_stats_t) as raw and named 27670Sstevel@tonic-gate * kstats, respectively. This is done when a CPU is initialized or placed 27680Sstevel@tonic-gate * online via p_online(2). 27690Sstevel@tonic-gate */ 27700Sstevel@tonic-gate static void 27710Sstevel@tonic-gate cpu_stats_kstat_create(cpu_t *cp) 27720Sstevel@tonic-gate { 27730Sstevel@tonic-gate int instance = cp->cpu_id; 27740Sstevel@tonic-gate char *module = "cpu"; 27750Sstevel@tonic-gate char *class = "misc"; 27760Sstevel@tonic-gate kstat_t *ksp; 27770Sstevel@tonic-gate zoneid_t zoneid; 27780Sstevel@tonic-gate 27790Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 27800Sstevel@tonic-gate 27810Sstevel@tonic-gate if (pool_pset_enabled()) 27820Sstevel@tonic-gate zoneid = GLOBAL_ZONEID; 27830Sstevel@tonic-gate else 27840Sstevel@tonic-gate zoneid = ALL_ZONES; 27850Sstevel@tonic-gate /* 27860Sstevel@tonic-gate * Create named kstats 27870Sstevel@tonic-gate */ 27880Sstevel@tonic-gate #define CPU_STATS_KS_CREATE(name, tsize, update_func) \ 27890Sstevel@tonic-gate ksp = kstat_create_zone(module, instance, (name), class, \ 27900Sstevel@tonic-gate KSTAT_TYPE_NAMED, (tsize) / sizeof (kstat_named_t), 0, \ 27910Sstevel@tonic-gate zoneid); \ 27920Sstevel@tonic-gate if (ksp != NULL) { \ 27930Sstevel@tonic-gate ksp->ks_private = cp; \ 27940Sstevel@tonic-gate ksp->ks_update = (update_func); \ 27950Sstevel@tonic-gate kstat_install(ksp); \ 27960Sstevel@tonic-gate } else \ 27970Sstevel@tonic-gate cmn_err(CE_WARN, "cpu: unable to create %s:%d:%s kstat", \ 27980Sstevel@tonic-gate module, instance, (name)); 27990Sstevel@tonic-gate 28000Sstevel@tonic-gate CPU_STATS_KS_CREATE("sys", sizeof (cpu_sys_stats_ks_data_template), 28010Sstevel@tonic-gate cpu_sys_stats_ks_update); 28020Sstevel@tonic-gate CPU_STATS_KS_CREATE("vm", sizeof (cpu_vm_stats_ks_data_template), 28030Sstevel@tonic-gate cpu_vm_stats_ks_update); 28040Sstevel@tonic-gate 28050Sstevel@tonic-gate /* 28060Sstevel@tonic-gate * Export the familiar cpu_stat_t KSTAT_TYPE_RAW kstat. 28070Sstevel@tonic-gate */ 28080Sstevel@tonic-gate ksp = kstat_create_zone("cpu_stat", cp->cpu_id, NULL, 28090Sstevel@tonic-gate "misc", KSTAT_TYPE_RAW, sizeof (cpu_stat_t), 0, zoneid); 28100Sstevel@tonic-gate if (ksp != NULL) { 28110Sstevel@tonic-gate ksp->ks_update = cpu_stat_ks_update; 28120Sstevel@tonic-gate ksp->ks_private = cp; 28130Sstevel@tonic-gate kstat_install(ksp); 28140Sstevel@tonic-gate } 28150Sstevel@tonic-gate } 28160Sstevel@tonic-gate 28170Sstevel@tonic-gate static void 28180Sstevel@tonic-gate cpu_stats_kstat_destroy(cpu_t *cp) 28190Sstevel@tonic-gate { 28200Sstevel@tonic-gate char ks_name[KSTAT_STRLEN]; 28210Sstevel@tonic-gate 28220Sstevel@tonic-gate (void) sprintf(ks_name, "cpu_stat%d", cp->cpu_id); 28230Sstevel@tonic-gate kstat_delete_byname("cpu_stat", cp->cpu_id, ks_name); 28240Sstevel@tonic-gate 28250Sstevel@tonic-gate kstat_delete_byname("cpu", cp->cpu_id, "sys"); 28260Sstevel@tonic-gate kstat_delete_byname("cpu", cp->cpu_id, "vm"); 28270Sstevel@tonic-gate } 28280Sstevel@tonic-gate 28290Sstevel@tonic-gate static int 28300Sstevel@tonic-gate cpu_sys_stats_ks_update(kstat_t *ksp, int rw) 28310Sstevel@tonic-gate { 28320Sstevel@tonic-gate cpu_t *cp = (cpu_t *)ksp->ks_private; 28330Sstevel@tonic-gate struct cpu_sys_stats_ks_data *csskd; 28340Sstevel@tonic-gate cpu_sys_stats_t *css; 2835590Sesolom hrtime_t msnsecs[NCMSTATES]; 28360Sstevel@tonic-gate int i; 28370Sstevel@tonic-gate 28380Sstevel@tonic-gate if (rw == KSTAT_WRITE) 28390Sstevel@tonic-gate return (EACCES); 28400Sstevel@tonic-gate 28410Sstevel@tonic-gate csskd = ksp->ks_data; 28420Sstevel@tonic-gate css = &cp->cpu_stats.sys; 28430Sstevel@tonic-gate 2844590Sesolom /* 2845590Sesolom * Read CPU mstate, but compare with the last values we 2846590Sesolom * received to make sure that the returned kstats never 2847590Sesolom * decrease. 2848590Sesolom */ 2849590Sesolom 2850590Sesolom get_cpu_mstate(cp, msnsecs); 2851590Sesolom if (csskd->cpu_nsec_idle.value.ui64 > msnsecs[CMS_IDLE]) 2852590Sesolom msnsecs[CMS_IDLE] = csskd->cpu_nsec_idle.value.ui64; 2853590Sesolom if (csskd->cpu_nsec_user.value.ui64 > msnsecs[CMS_USER]) 2854590Sesolom msnsecs[CMS_USER] = csskd->cpu_nsec_user.value.ui64; 2855590Sesolom if (csskd->cpu_nsec_kernel.value.ui64 > msnsecs[CMS_SYSTEM]) 2856590Sesolom msnsecs[CMS_SYSTEM] = csskd->cpu_nsec_kernel.value.ui64; 2857590Sesolom 28580Sstevel@tonic-gate bcopy(&cpu_sys_stats_ks_data_template, ksp->ks_data, 28590Sstevel@tonic-gate sizeof (cpu_sys_stats_ks_data_template)); 2860590Sesolom 28610Sstevel@tonic-gate csskd->cpu_ticks_wait.value.ui64 = 0; 28620Sstevel@tonic-gate csskd->wait_ticks_io.value.ui64 = 0; 2863590Sesolom 2864590Sesolom csskd->cpu_nsec_idle.value.ui64 = msnsecs[CMS_IDLE]; 2865590Sesolom csskd->cpu_nsec_user.value.ui64 = msnsecs[CMS_USER]; 2866590Sesolom csskd->cpu_nsec_kernel.value.ui64 = msnsecs[CMS_SYSTEM]; 28670Sstevel@tonic-gate csskd->cpu_ticks_idle.value.ui64 = 28680Sstevel@tonic-gate NSEC_TO_TICK(csskd->cpu_nsec_idle.value.ui64); 28690Sstevel@tonic-gate csskd->cpu_ticks_user.value.ui64 = 28700Sstevel@tonic-gate NSEC_TO_TICK(csskd->cpu_nsec_user.value.ui64); 28710Sstevel@tonic-gate csskd->cpu_ticks_kernel.value.ui64 = 28720Sstevel@tonic-gate NSEC_TO_TICK(csskd->cpu_nsec_kernel.value.ui64); 28730Sstevel@tonic-gate csskd->bread.value.ui64 = css->bread; 28740Sstevel@tonic-gate csskd->bwrite.value.ui64 = css->bwrite; 28750Sstevel@tonic-gate csskd->lread.value.ui64 = css->lread; 28760Sstevel@tonic-gate csskd->lwrite.value.ui64 = css->lwrite; 28770Sstevel@tonic-gate csskd->phread.value.ui64 = css->phread; 28780Sstevel@tonic-gate csskd->phwrite.value.ui64 = css->phwrite; 28790Sstevel@tonic-gate csskd->pswitch.value.ui64 = css->pswitch; 28800Sstevel@tonic-gate csskd->trap.value.ui64 = css->trap; 28810Sstevel@tonic-gate csskd->intr.value.ui64 = 0; 28820Sstevel@tonic-gate for (i = 0; i < PIL_MAX; i++) 28830Sstevel@tonic-gate csskd->intr.value.ui64 += css->intr[i]; 28840Sstevel@tonic-gate csskd->syscall.value.ui64 = css->syscall; 28850Sstevel@tonic-gate csskd->sysread.value.ui64 = css->sysread; 28860Sstevel@tonic-gate csskd->syswrite.value.ui64 = css->syswrite; 28870Sstevel@tonic-gate csskd->sysfork.value.ui64 = css->sysfork; 28880Sstevel@tonic-gate csskd->sysvfork.value.ui64 = css->sysvfork; 28890Sstevel@tonic-gate csskd->sysexec.value.ui64 = css->sysexec; 28900Sstevel@tonic-gate csskd->readch.value.ui64 = css->readch; 28910Sstevel@tonic-gate csskd->writech.value.ui64 = css->writech; 28920Sstevel@tonic-gate csskd->rcvint.value.ui64 = css->rcvint; 28930Sstevel@tonic-gate csskd->xmtint.value.ui64 = css->xmtint; 28940Sstevel@tonic-gate csskd->mdmint.value.ui64 = css->mdmint; 28950Sstevel@tonic-gate csskd->rawch.value.ui64 = css->rawch; 28960Sstevel@tonic-gate csskd->canch.value.ui64 = css->canch; 28970Sstevel@tonic-gate csskd->outch.value.ui64 = css->outch; 28980Sstevel@tonic-gate csskd->msg.value.ui64 = css->msg; 28990Sstevel@tonic-gate csskd->sema.value.ui64 = css->sema; 29000Sstevel@tonic-gate csskd->namei.value.ui64 = css->namei; 29010Sstevel@tonic-gate csskd->ufsiget.value.ui64 = css->ufsiget; 29020Sstevel@tonic-gate csskd->ufsdirblk.value.ui64 = css->ufsdirblk; 29030Sstevel@tonic-gate csskd->ufsipage.value.ui64 = css->ufsipage; 29040Sstevel@tonic-gate csskd->ufsinopage.value.ui64 = css->ufsinopage; 29050Sstevel@tonic-gate csskd->procovf.value.ui64 = css->procovf; 29060Sstevel@tonic-gate csskd->intrthread.value.ui64 = 0; 29070Sstevel@tonic-gate for (i = 0; i < LOCK_LEVEL; i++) 29080Sstevel@tonic-gate csskd->intrthread.value.ui64 += css->intr[i]; 29090Sstevel@tonic-gate csskd->intrblk.value.ui64 = css->intrblk; 29100Sstevel@tonic-gate csskd->intrunpin.value.ui64 = css->intrunpin; 29110Sstevel@tonic-gate csskd->idlethread.value.ui64 = css->idlethread; 29120Sstevel@tonic-gate csskd->inv_swtch.value.ui64 = css->inv_swtch; 29130Sstevel@tonic-gate csskd->nthreads.value.ui64 = css->nthreads; 29140Sstevel@tonic-gate csskd->cpumigrate.value.ui64 = css->cpumigrate; 29150Sstevel@tonic-gate csskd->xcalls.value.ui64 = css->xcalls; 29160Sstevel@tonic-gate csskd->mutex_adenters.value.ui64 = css->mutex_adenters; 29170Sstevel@tonic-gate csskd->rw_rdfails.value.ui64 = css->rw_rdfails; 29180Sstevel@tonic-gate csskd->rw_wrfails.value.ui64 = css->rw_wrfails; 29190Sstevel@tonic-gate csskd->modload.value.ui64 = css->modload; 29200Sstevel@tonic-gate csskd->modunload.value.ui64 = css->modunload; 29210Sstevel@tonic-gate csskd->bawrite.value.ui64 = css->bawrite; 29220Sstevel@tonic-gate csskd->iowait.value.ui64 = 0; 29230Sstevel@tonic-gate 29240Sstevel@tonic-gate return (0); 29250Sstevel@tonic-gate } 29260Sstevel@tonic-gate 29270Sstevel@tonic-gate static int 29280Sstevel@tonic-gate cpu_vm_stats_ks_update(kstat_t *ksp, int rw) 29290Sstevel@tonic-gate { 29300Sstevel@tonic-gate cpu_t *cp = (cpu_t *)ksp->ks_private; 29310Sstevel@tonic-gate struct cpu_vm_stats_ks_data *cvskd; 29320Sstevel@tonic-gate cpu_vm_stats_t *cvs; 29330Sstevel@tonic-gate 29340Sstevel@tonic-gate if (rw == KSTAT_WRITE) 29350Sstevel@tonic-gate return (EACCES); 29360Sstevel@tonic-gate 29370Sstevel@tonic-gate cvs = &cp->cpu_stats.vm; 29380Sstevel@tonic-gate cvskd = ksp->ks_data; 29390Sstevel@tonic-gate 29400Sstevel@tonic-gate bcopy(&cpu_vm_stats_ks_data_template, ksp->ks_data, 29410Sstevel@tonic-gate sizeof (cpu_vm_stats_ks_data_template)); 29420Sstevel@tonic-gate cvskd->pgrec.value.ui64 = cvs->pgrec; 29430Sstevel@tonic-gate cvskd->pgfrec.value.ui64 = cvs->pgfrec; 29440Sstevel@tonic-gate cvskd->pgin.value.ui64 = cvs->pgin; 29450Sstevel@tonic-gate cvskd->pgpgin.value.ui64 = cvs->pgpgin; 29460Sstevel@tonic-gate cvskd->pgout.value.ui64 = cvs->pgout; 29470Sstevel@tonic-gate cvskd->pgpgout.value.ui64 = cvs->pgpgout; 29480Sstevel@tonic-gate cvskd->swapin.value.ui64 = cvs->swapin; 29490Sstevel@tonic-gate cvskd->pgswapin.value.ui64 = cvs->pgswapin; 29500Sstevel@tonic-gate cvskd->swapout.value.ui64 = cvs->swapout; 29510Sstevel@tonic-gate cvskd->pgswapout.value.ui64 = cvs->pgswapout; 29520Sstevel@tonic-gate cvskd->zfod.value.ui64 = cvs->zfod; 29530Sstevel@tonic-gate cvskd->dfree.value.ui64 = cvs->dfree; 29540Sstevel@tonic-gate cvskd->scan.value.ui64 = cvs->scan; 29550Sstevel@tonic-gate cvskd->rev.value.ui64 = cvs->rev; 29560Sstevel@tonic-gate cvskd->hat_fault.value.ui64 = cvs->hat_fault; 29570Sstevel@tonic-gate cvskd->as_fault.value.ui64 = cvs->as_fault; 29580Sstevel@tonic-gate cvskd->maj_fault.value.ui64 = cvs->maj_fault; 29590Sstevel@tonic-gate cvskd->cow_fault.value.ui64 = cvs->cow_fault; 29600Sstevel@tonic-gate cvskd->prot_fault.value.ui64 = cvs->prot_fault; 29610Sstevel@tonic-gate cvskd->softlock.value.ui64 = cvs->softlock; 29620Sstevel@tonic-gate cvskd->kernel_asflt.value.ui64 = cvs->kernel_asflt; 29630Sstevel@tonic-gate cvskd->pgrrun.value.ui64 = cvs->pgrrun; 29640Sstevel@tonic-gate cvskd->execpgin.value.ui64 = cvs->execpgin; 29650Sstevel@tonic-gate cvskd->execpgout.value.ui64 = cvs->execpgout; 29660Sstevel@tonic-gate cvskd->execfree.value.ui64 = cvs->execfree; 29670Sstevel@tonic-gate cvskd->anonpgin.value.ui64 = cvs->anonpgin; 29680Sstevel@tonic-gate cvskd->anonpgout.value.ui64 = cvs->anonpgout; 29690Sstevel@tonic-gate cvskd->anonfree.value.ui64 = cvs->anonfree; 29700Sstevel@tonic-gate cvskd->fspgin.value.ui64 = cvs->fspgin; 29710Sstevel@tonic-gate cvskd->fspgout.value.ui64 = cvs->fspgout; 29720Sstevel@tonic-gate cvskd->fsfree.value.ui64 = cvs->fsfree; 29730Sstevel@tonic-gate 29740Sstevel@tonic-gate return (0); 29750Sstevel@tonic-gate } 29760Sstevel@tonic-gate 29770Sstevel@tonic-gate static int 29780Sstevel@tonic-gate cpu_stat_ks_update(kstat_t *ksp, int rw) 29790Sstevel@tonic-gate { 29800Sstevel@tonic-gate cpu_stat_t *cso; 29810Sstevel@tonic-gate cpu_t *cp; 29820Sstevel@tonic-gate int i; 2983590Sesolom hrtime_t msnsecs[NCMSTATES]; 29840Sstevel@tonic-gate 29850Sstevel@tonic-gate cso = (cpu_stat_t *)ksp->ks_data; 29860Sstevel@tonic-gate cp = (cpu_t *)ksp->ks_private; 29870Sstevel@tonic-gate 29880Sstevel@tonic-gate if (rw == KSTAT_WRITE) 29890Sstevel@tonic-gate return (EACCES); 29900Sstevel@tonic-gate 29910Sstevel@tonic-gate /* 2992590Sesolom * Read CPU mstate, but compare with the last values we 2993590Sesolom * received to make sure that the returned kstats never 2994590Sesolom * decrease. 29950Sstevel@tonic-gate */ 2996590Sesolom 2997590Sesolom get_cpu_mstate(cp, msnsecs); 2998590Sesolom msnsecs[CMS_IDLE] = NSEC_TO_TICK(msnsecs[CMS_IDLE]); 2999590Sesolom msnsecs[CMS_USER] = NSEC_TO_TICK(msnsecs[CMS_USER]); 3000590Sesolom msnsecs[CMS_SYSTEM] = NSEC_TO_TICK(msnsecs[CMS_SYSTEM]); 3001590Sesolom if (cso->cpu_sysinfo.cpu[CPU_IDLE] < msnsecs[CMS_IDLE]) 3002590Sesolom cso->cpu_sysinfo.cpu[CPU_IDLE] = msnsecs[CMS_IDLE]; 3003590Sesolom if (cso->cpu_sysinfo.cpu[CPU_USER] < msnsecs[CMS_USER]) 3004590Sesolom cso->cpu_sysinfo.cpu[CPU_USER] = msnsecs[CMS_USER]; 3005590Sesolom if (cso->cpu_sysinfo.cpu[CPU_KERNEL] < msnsecs[CMS_SYSTEM]) 3006590Sesolom cso->cpu_sysinfo.cpu[CPU_KERNEL] = msnsecs[CMS_SYSTEM]; 30070Sstevel@tonic-gate cso->cpu_sysinfo.cpu[CPU_WAIT] = 0; 30080Sstevel@tonic-gate cso->cpu_sysinfo.wait[W_IO] = 0; 30090Sstevel@tonic-gate cso->cpu_sysinfo.wait[W_SWAP] = 0; 30100Sstevel@tonic-gate cso->cpu_sysinfo.wait[W_PIO] = 0; 30110Sstevel@tonic-gate cso->cpu_sysinfo.bread = CPU_STATS(cp, sys.bread); 30120Sstevel@tonic-gate cso->cpu_sysinfo.bwrite = CPU_STATS(cp, sys.bwrite); 30130Sstevel@tonic-gate cso->cpu_sysinfo.lread = CPU_STATS(cp, sys.lread); 30140Sstevel@tonic-gate cso->cpu_sysinfo.lwrite = CPU_STATS(cp, sys.lwrite); 30150Sstevel@tonic-gate cso->cpu_sysinfo.phread = CPU_STATS(cp, sys.phread); 30160Sstevel@tonic-gate cso->cpu_sysinfo.phwrite = CPU_STATS(cp, sys.phwrite); 30170Sstevel@tonic-gate cso->cpu_sysinfo.pswitch = CPU_STATS(cp, sys.pswitch); 30180Sstevel@tonic-gate cso->cpu_sysinfo.trap = CPU_STATS(cp, sys.trap); 30190Sstevel@tonic-gate cso->cpu_sysinfo.intr = 0; 30200Sstevel@tonic-gate for (i = 0; i < PIL_MAX; i++) 30210Sstevel@tonic-gate cso->cpu_sysinfo.intr += CPU_STATS(cp, sys.intr[i]); 30220Sstevel@tonic-gate cso->cpu_sysinfo.syscall = CPU_STATS(cp, sys.syscall); 30230Sstevel@tonic-gate cso->cpu_sysinfo.sysread = CPU_STATS(cp, sys.sysread); 30240Sstevel@tonic-gate cso->cpu_sysinfo.syswrite = CPU_STATS(cp, sys.syswrite); 30250Sstevel@tonic-gate cso->cpu_sysinfo.sysfork = CPU_STATS(cp, sys.sysfork); 30260Sstevel@tonic-gate cso->cpu_sysinfo.sysvfork = CPU_STATS(cp, sys.sysvfork); 30270Sstevel@tonic-gate cso->cpu_sysinfo.sysexec = CPU_STATS(cp, sys.sysexec); 30280Sstevel@tonic-gate cso->cpu_sysinfo.readch = CPU_STATS(cp, sys.readch); 30290Sstevel@tonic-gate cso->cpu_sysinfo.writech = CPU_STATS(cp, sys.writech); 30300Sstevel@tonic-gate cso->cpu_sysinfo.rcvint = CPU_STATS(cp, sys.rcvint); 30310Sstevel@tonic-gate cso->cpu_sysinfo.xmtint = CPU_STATS(cp, sys.xmtint); 30320Sstevel@tonic-gate cso->cpu_sysinfo.mdmint = CPU_STATS(cp, sys.mdmint); 30330Sstevel@tonic-gate cso->cpu_sysinfo.rawch = CPU_STATS(cp, sys.rawch); 30340Sstevel@tonic-gate cso->cpu_sysinfo.canch = CPU_STATS(cp, sys.canch); 30350Sstevel@tonic-gate cso->cpu_sysinfo.outch = CPU_STATS(cp, sys.outch); 30360Sstevel@tonic-gate cso->cpu_sysinfo.msg = CPU_STATS(cp, sys.msg); 30370Sstevel@tonic-gate cso->cpu_sysinfo.sema = CPU_STATS(cp, sys.sema); 30380Sstevel@tonic-gate cso->cpu_sysinfo.namei = CPU_STATS(cp, sys.namei); 30390Sstevel@tonic-gate cso->cpu_sysinfo.ufsiget = CPU_STATS(cp, sys.ufsiget); 30400Sstevel@tonic-gate cso->cpu_sysinfo.ufsdirblk = CPU_STATS(cp, sys.ufsdirblk); 30410Sstevel@tonic-gate cso->cpu_sysinfo.ufsipage = CPU_STATS(cp, sys.ufsipage); 30420Sstevel@tonic-gate cso->cpu_sysinfo.ufsinopage = CPU_STATS(cp, sys.ufsinopage); 30430Sstevel@tonic-gate cso->cpu_sysinfo.inodeovf = 0; 30440Sstevel@tonic-gate cso->cpu_sysinfo.fileovf = 0; 30450Sstevel@tonic-gate cso->cpu_sysinfo.procovf = CPU_STATS(cp, sys.procovf); 30460Sstevel@tonic-gate cso->cpu_sysinfo.intrthread = 0; 30470Sstevel@tonic-gate for (i = 0; i < LOCK_LEVEL; i++) 30480Sstevel@tonic-gate cso->cpu_sysinfo.intrthread += CPU_STATS(cp, sys.intr[i]); 30490Sstevel@tonic-gate cso->cpu_sysinfo.intrblk = CPU_STATS(cp, sys.intrblk); 30500Sstevel@tonic-gate cso->cpu_sysinfo.idlethread = CPU_STATS(cp, sys.idlethread); 30510Sstevel@tonic-gate cso->cpu_sysinfo.inv_swtch = CPU_STATS(cp, sys.inv_swtch); 30520Sstevel@tonic-gate cso->cpu_sysinfo.nthreads = CPU_STATS(cp, sys.nthreads); 30530Sstevel@tonic-gate cso->cpu_sysinfo.cpumigrate = CPU_STATS(cp, sys.cpumigrate); 30540Sstevel@tonic-gate cso->cpu_sysinfo.xcalls = CPU_STATS(cp, sys.xcalls); 30550Sstevel@tonic-gate cso->cpu_sysinfo.mutex_adenters = CPU_STATS(cp, sys.mutex_adenters); 30560Sstevel@tonic-gate cso->cpu_sysinfo.rw_rdfails = CPU_STATS(cp, sys.rw_rdfails); 30570Sstevel@tonic-gate cso->cpu_sysinfo.rw_wrfails = CPU_STATS(cp, sys.rw_wrfails); 30580Sstevel@tonic-gate cso->cpu_sysinfo.modload = CPU_STATS(cp, sys.modload); 30590Sstevel@tonic-gate cso->cpu_sysinfo.modunload = CPU_STATS(cp, sys.modunload); 30600Sstevel@tonic-gate cso->cpu_sysinfo.bawrite = CPU_STATS(cp, sys.bawrite); 30610Sstevel@tonic-gate cso->cpu_sysinfo.rw_enters = 0; 30620Sstevel@tonic-gate cso->cpu_sysinfo.win_uo_cnt = 0; 30630Sstevel@tonic-gate cso->cpu_sysinfo.win_uu_cnt = 0; 30640Sstevel@tonic-gate cso->cpu_sysinfo.win_so_cnt = 0; 30650Sstevel@tonic-gate cso->cpu_sysinfo.win_su_cnt = 0; 30660Sstevel@tonic-gate cso->cpu_sysinfo.win_suo_cnt = 0; 30670Sstevel@tonic-gate 30680Sstevel@tonic-gate cso->cpu_syswait.iowait = 0; 30690Sstevel@tonic-gate cso->cpu_syswait.swap = 0; 30700Sstevel@tonic-gate cso->cpu_syswait.physio = 0; 30710Sstevel@tonic-gate 30720Sstevel@tonic-gate cso->cpu_vminfo.pgrec = CPU_STATS(cp, vm.pgrec); 30730Sstevel@tonic-gate cso->cpu_vminfo.pgfrec = CPU_STATS(cp, vm.pgfrec); 30740Sstevel@tonic-gate cso->cpu_vminfo.pgin = CPU_STATS(cp, vm.pgin); 30750Sstevel@tonic-gate cso->cpu_vminfo.pgpgin = CPU_STATS(cp, vm.pgpgin); 30760Sstevel@tonic-gate cso->cpu_vminfo.pgout = CPU_STATS(cp, vm.pgout); 30770Sstevel@tonic-gate cso->cpu_vminfo.pgpgout = CPU_STATS(cp, vm.pgpgout); 30780Sstevel@tonic-gate cso->cpu_vminfo.swapin = CPU_STATS(cp, vm.swapin); 30790Sstevel@tonic-gate cso->cpu_vminfo.pgswapin = CPU_STATS(cp, vm.pgswapin); 30800Sstevel@tonic-gate cso->cpu_vminfo.swapout = CPU_STATS(cp, vm.swapout); 30810Sstevel@tonic-gate cso->cpu_vminfo.pgswapout = CPU_STATS(cp, vm.pgswapout); 30820Sstevel@tonic-gate cso->cpu_vminfo.zfod = CPU_STATS(cp, vm.zfod); 30830Sstevel@tonic-gate cso->cpu_vminfo.dfree = CPU_STATS(cp, vm.dfree); 30840Sstevel@tonic-gate cso->cpu_vminfo.scan = CPU_STATS(cp, vm.scan); 30850Sstevel@tonic-gate cso->cpu_vminfo.rev = CPU_STATS(cp, vm.rev); 30860Sstevel@tonic-gate cso->cpu_vminfo.hat_fault = CPU_STATS(cp, vm.hat_fault); 30870Sstevel@tonic-gate cso->cpu_vminfo.as_fault = CPU_STATS(cp, vm.as_fault); 30880Sstevel@tonic-gate cso->cpu_vminfo.maj_fault = CPU_STATS(cp, vm.maj_fault); 30890Sstevel@tonic-gate cso->cpu_vminfo.cow_fault = CPU_STATS(cp, vm.cow_fault); 30900Sstevel@tonic-gate cso->cpu_vminfo.prot_fault = CPU_STATS(cp, vm.prot_fault); 30910Sstevel@tonic-gate cso->cpu_vminfo.softlock = CPU_STATS(cp, vm.softlock); 30920Sstevel@tonic-gate cso->cpu_vminfo.kernel_asflt = CPU_STATS(cp, vm.kernel_asflt); 30930Sstevel@tonic-gate cso->cpu_vminfo.pgrrun = CPU_STATS(cp, vm.pgrrun); 30940Sstevel@tonic-gate cso->cpu_vminfo.execpgin = CPU_STATS(cp, vm.execpgin); 30950Sstevel@tonic-gate cso->cpu_vminfo.execpgout = CPU_STATS(cp, vm.execpgout); 30960Sstevel@tonic-gate cso->cpu_vminfo.execfree = CPU_STATS(cp, vm.execfree); 30970Sstevel@tonic-gate cso->cpu_vminfo.anonpgin = CPU_STATS(cp, vm.anonpgin); 30980Sstevel@tonic-gate cso->cpu_vminfo.anonpgout = CPU_STATS(cp, vm.anonpgout); 30990Sstevel@tonic-gate cso->cpu_vminfo.anonfree = CPU_STATS(cp, vm.anonfree); 31000Sstevel@tonic-gate cso->cpu_vminfo.fspgin = CPU_STATS(cp, vm.fspgin); 31010Sstevel@tonic-gate cso->cpu_vminfo.fspgout = CPU_STATS(cp, vm.fspgout); 31020Sstevel@tonic-gate cso->cpu_vminfo.fsfree = CPU_STATS(cp, vm.fsfree); 31030Sstevel@tonic-gate 31040Sstevel@tonic-gate return (0); 31050Sstevel@tonic-gate } 3106