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