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