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 /* 23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* 28 * Copyright 2019 Joyent, Inc. 29 */ 30 31 #ifndef _SYS_THREAD_H 32 #define _SYS_THREAD_H 33 34 35 #include <sys/types.h> 36 #include <sys/t_lock.h> 37 #include <sys/klwp.h> 38 #include <sys/time.h> 39 #include <sys/signal.h> 40 #include <sys/kcpc.h> 41 #if defined(__GNUC__) && defined(_ASM_INLINES) && defined(_KERNEL) 42 #include <asm/thread.h> 43 #endif 44 45 #ifdef __cplusplus 46 extern "C" { 47 #endif 48 49 /* 50 * The thread object, its states, and the methods by which it 51 * is accessed. 52 */ 53 54 /* 55 * Values that t_state may assume. Note that t_state cannot have more 56 * than one of these flags set at a time. 57 */ 58 #define TS_FREE 0x00 /* Thread at loose ends */ 59 #define TS_SLEEP 0x01 /* Awaiting an event */ 60 #define TS_RUN 0x02 /* Runnable, but not yet on a processor */ 61 #define TS_ONPROC 0x04 /* Thread is being run on a processor */ 62 #define TS_ZOMB 0x08 /* Thread has died but hasn't been reaped */ 63 #define TS_STOPPED 0x10 /* Stopped, initial state */ 64 #define TS_WAIT 0x20 /* Waiting to become runnable */ 65 66 typedef struct ctxop { 67 void (*save_op)(void *); /* function to invoke to save context */ 68 void (*restore_op)(void *); /* function to invoke to restore ctx */ 69 void (*fork_op)(void *, void *); /* invoke to fork context */ 70 void (*lwp_create_op)(void *, void *); /* lwp_create context */ 71 void (*exit_op)(void *); /* invoked during {thread,lwp}_exit() */ 72 void (*free_op)(void *, int); /* function which frees the context */ 73 void *arg; /* argument to above functions, ctx pointer */ 74 struct ctxop *next; /* next context ops */ 75 } ctxop_t; 76 77 /* 78 * The active file descriptor table. 79 * Each member of a_fd[] not equalling -1 represents an active fd. 80 * The structure is initialized on first use; all zeros means uninitialized. 81 */ 82 typedef struct { 83 kmutex_t a_fdlock; /* protects a_fd and a_nfd */ 84 int *a_fd; /* pointer to list of fds */ 85 int a_nfd; /* number of entries in *a_fd */ 86 int a_stale; /* one of the active fds is being closed */ 87 int a_buf[2]; /* buffer to which a_fd initially refers */ 88 } afd_t; 89 90 /* 91 * An lwpchan provides uniqueness when sleeping on user-level 92 * synchronization primitives. The lc_wchan member is used 93 * for sleeping on kernel synchronization primitives. 94 */ 95 typedef struct { 96 caddr_t lc_wchan0; 97 caddr_t lc_wchan; 98 } lwpchan_t; 99 100 typedef struct _kthread *kthread_id_t; 101 102 struct turnstile; 103 struct panic_trap_info; 104 struct upimutex; 105 struct kproject; 106 struct on_trap_data; 107 struct waitq; 108 struct _kcpc_ctx; 109 struct _kcpc_set; 110 111 /* Definition for kernel thread identifier type */ 112 typedef uint64_t kt_did_t; 113 114 typedef struct _kthread { 115 struct _kthread *t_link; /* dispq, sleepq, and free queue link */ 116 117 caddr_t t_stk; /* base of stack (kernel sp value to use) */ 118 void (*t_startpc)(void); /* PC where thread started */ 119 struct cpu *t_bound_cpu; /* cpu bound to, or NULL if not bound */ 120 short t_affinitycnt; /* nesting level of kernel affinity-setting */ 121 short t_bind_cpu; /* user-specified CPU binding (-1 if none) */ 122 ushort_t t_flag; /* modified only by current thread */ 123 ushort_t t_proc_flag; /* modified holding ttproc(t)->p_lock */ 124 ushort_t t_schedflag; /* modified holding thread_lock(t) */ 125 volatile char t_preempt; /* don't preempt thread if set */ 126 volatile char t_preempt_lk; 127 uint_t t_state; /* thread state (protected by thread_lock) */ 128 pri_t t_pri; /* assigned thread priority */ 129 pri_t t_epri; /* inherited thread priority */ 130 pri_t t_cpri; /* thread scheduling class priority */ 131 char t_writer; /* sleeping in lwp_rwlock_lock(RW_WRITE_LOCK) */ 132 uchar_t t_bindflag; /* CPU and pset binding type */ 133 label_t t_pcb; /* pcb, save area when switching */ 134 lwpchan_t t_lwpchan; /* reason for blocking */ 135 #define t_wchan0 t_lwpchan.lc_wchan0 136 #define t_wchan t_lwpchan.lc_wchan 137 struct _sobj_ops *t_sobj_ops; 138 id_t t_cid; /* scheduling class id */ 139 struct thread_ops *t_clfuncs; /* scheduling class ops vector */ 140 void *t_cldata; /* per scheduling class specific data */ 141 ctxop_t *t_ctx; /* thread context */ 142 uintptr_t t_lofault; /* ret pc for failed page faults */ 143 label_t *t_onfault; /* on_fault() setjmp buf */ 144 struct on_trap_data *t_ontrap; /* on_trap() protection data */ 145 caddr_t t_swap; /* the bottom of the stack, if from segkp */ 146 lock_t t_lock; /* used to resume() a thread */ 147 uint8_t t_lockstat; /* set while thread is in lockstat code */ 148 uint8_t t_pil; /* interrupt thread PIL */ 149 disp_lock_t t_pi_lock; /* lock protecting t_prioinv list */ 150 char t_nomigrate; /* do not migrate if set */ 151 struct cpu *t_cpu; /* CPU that thread last ran on */ 152 struct cpu *t_weakbound_cpu; /* cpu weakly bound to */ 153 struct lgrp_ld *t_lpl; /* load average for home lgroup */ 154 void *t_lgrp_reserv[2]; /* reserved for future */ 155 struct _kthread *t_intr; /* interrupted (pinned) thread */ 156 uint64_t t_intr_start; /* timestamp when time slice began */ 157 kt_did_t t_did; /* thread id for kernel debuggers */ 158 caddr_t t_tnf_tpdp; /* Trace facility data pointer */ 159 struct _kcpc_ctx *t_cpc_ctx; /* performance counter context */ 160 struct _kcpc_set *t_cpc_set; /* set this thread has bound */ 161 162 /* 163 * non swappable part of the lwp state. 164 */ 165 id_t t_tid; /* lwp's id */ 166 id_t t_waitfor; /* target lwp id in lwp_wait() */ 167 struct sigqueue *t_sigqueue; /* queue of siginfo structs */ 168 k_sigset_t t_sig; /* signals pending to this process */ 169 k_sigset_t t_extsig; /* signals sent from another contract */ 170 k_sigset_t t_hold; /* hold signal bit mask */ 171 k_sigset_t t_sigwait; /* sigtimedwait/sigfd accepting these */ 172 struct _kthread *t_forw; /* process's forward thread link */ 173 struct _kthread *t_back; /* process's backward thread link */ 174 struct _kthread *t_thlink; /* tid (lwpid) lookup hash link */ 175 klwp_t *t_lwp; /* thread's lwp pointer */ 176 struct proc *t_procp; /* proc pointer */ 177 struct t_audit_data *t_audit_data; /* per thread audit data */ 178 struct _kthread *t_next; /* doubly linked list of all threads */ 179 struct _kthread *t_prev; 180 ushort_t t_whystop; /* reason for stopping */ 181 ushort_t t_whatstop; /* more detailed reason */ 182 int t_dslot; /* index in proc's thread directory */ 183 struct pollstate *t_pollstate; /* state used during poll(2) */ 184 struct pollcache *t_pollcache; /* to pass a pcache ptr by /dev/poll */ 185 struct cred *t_cred; /* pointer to current cred */ 186 time_t t_start; /* start time, seconds since epoch */ 187 clock_t t_lbolt; /* lbolt at last clock_tick() */ 188 hrtime_t t_stoptime; /* timestamp at stop() */ 189 uint_t t_pctcpu; /* %cpu at last clock_tick(), binary */ 190 /* point at right of high-order bit */ 191 short t_sysnum; /* system call number */ 192 kcondvar_t t_delay_cv; 193 kmutex_t t_delay_lock; 194 195 /* 196 * Pointer to the dispatcher lock protecting t_state and state-related 197 * flags. This pointer can change during waits on the lock, so 198 * it should be grabbed only by thread_lock(). 199 */ 200 disp_lock_t *t_lockp; /* pointer to the dispatcher lock */ 201 ushort_t t_oldspl; /* spl level before dispatcher locked */ 202 volatile char t_pre_sys; /* pre-syscall work needed */ 203 lock_t t_lock_flush; /* for lock_mutex_flush() impl */ 204 struct _disp *t_disp_queue; /* run queue for chosen CPU */ 205 clock_t t_disp_time; /* last time this thread was running */ 206 207 /* 208 * Post-syscall / post-trap flags. 209 * No lock is required to set these. 210 * These must be cleared only by the thread itself. 211 * 212 * t_astflag indicates that some post-trap processing is required, 213 * possibly a signal or a preemption. The thread will not 214 * return to user with this set. 215 * t_post_sys indicates that some unusualy post-system call 216 * handling is required, such as an error or tracing. 217 * t_sig_check indicates that some condition in ISSIG() must be 218 * checked, but doesn't prevent returning to user. 219 * t_post_sys_ast is a way of checking whether any of these three 220 * flags are set. 221 */ 222 union __tu { 223 struct __ts { 224 volatile char _t_astflag; /* AST requested */ 225 volatile char _t_sig_check; /* ISSIG required */ 226 volatile char _t_post_sys; /* post_syscall req */ 227 volatile char _t_trapret; /* call CL_TRAPRET */ 228 } _ts; 229 volatile int _t_post_sys_ast; /* OR of these flags */ 230 } _tu; 231 #define t_astflag _tu._ts._t_astflag 232 #define t_sig_check _tu._ts._t_sig_check 233 #define t_post_sys _tu._ts._t_post_sys 234 #define t_trapret _tu._ts._t_trapret 235 #define t_post_sys_ast _tu._t_post_sys_ast 236 237 /* 238 * Real time microstate profiling. 239 */ 240 /* possible 4-byte filler */ 241 hrtime_t t_waitrq; /* timestamp for run queue wait time */ 242 int t_mstate; /* current microstate */ 243 struct rprof { 244 int rp_anystate; /* set if any state non-zero */ 245 uint_t rp_state[NMSTATES]; /* mstate profiling counts */ 246 } *t_rprof; 247 248 /* 249 * There is a turnstile inserted into the list below for 250 * every priority inverted synchronization object that 251 * this thread holds. 252 */ 253 254 struct turnstile *t_prioinv; 255 256 /* 257 * Pointer to the turnstile attached to the synchronization 258 * object where this thread is blocked. 259 */ 260 261 struct turnstile *t_ts; 262 263 /* 264 * kernel thread specific data 265 * Borrowed from userland implementation of POSIX tsd 266 */ 267 struct tsd_thread { 268 struct tsd_thread *ts_next; /* threads with TSD */ 269 struct tsd_thread *ts_prev; /* threads with TSD */ 270 uint_t ts_nkeys; /* entries in value array */ 271 void **ts_value; /* array of value/key */ 272 } *t_tsd; 273 274 clock_t t_stime; /* time stamp used by the swapper */ 275 struct door_data *t_door; /* door invocation data */ 276 kmutex_t *t_plockp; /* pointer to process's p_lock */ 277 278 struct sc_shared *t_schedctl; /* scheduler activations shared data */ 279 uintptr_t t_sc_uaddr; /* user-level address of shared data */ 280 281 struct cpupart *t_cpupart; /* partition containing thread */ 282 int t_bind_pset; /* processor set binding */ 283 284 struct copyops *t_copyops; /* copy in/out ops vector */ 285 286 caddr_t t_stkbase; /* base of the the stack */ 287 struct page *t_red_pp; /* if non-NULL, redzone is mapped */ 288 289 afd_t t_activefd; /* active file descriptor table */ 290 291 struct _kthread *t_priforw; /* sleepq per-priority sublist */ 292 struct _kthread *t_priback; 293 294 struct sleepq *t_sleepq; /* sleep queue thread is waiting on */ 295 struct panic_trap_info *t_panic_trap; /* saved data from fatal trap */ 296 int *t_lgrp_affinity; /* lgroup affinity */ 297 struct upimutex *t_upimutex; /* list of upimutexes owned by thread */ 298 uint32_t t_nupinest; /* number of nested held upi mutexes */ 299 struct kproject *t_proj; /* project containing this thread */ 300 uint8_t t_unpark; /* modified holding t_delay_lock */ 301 uint8_t t_release; /* lwp_release() waked up the thread */ 302 uint8_t t_hatdepth; /* depth of recursive hat_memloads */ 303 uint8_t t_xpvcntr; /* see xen_block_migrate() */ 304 kcondvar_t t_joincv; /* cv used to wait for thread exit */ 305 void *t_taskq; /* for threads belonging to taskq */ 306 hrtime_t t_anttime; /* most recent time anticipatory load */ 307 /* was added to an lgroup's load */ 308 /* on this thread's behalf */ 309 char *t_pdmsg; /* privilege debugging message */ 310 311 uint_t t_predcache; /* DTrace predicate cache */ 312 hrtime_t t_dtrace_vtime; /* DTrace virtual time */ 313 hrtime_t t_dtrace_start; /* DTrace slice start time */ 314 315 uint8_t t_dtrace_stop; /* indicates a DTrace-desired stop */ 316 uint8_t t_dtrace_sig; /* signal sent via DTrace's raise() */ 317 318 union __tdu { 319 struct __tds { 320 uint8_t _t_dtrace_on; /* hit a fasttrap tracepoint */ 321 uint8_t _t_dtrace_step; /* about to return to kernel */ 322 uint8_t _t_dtrace_ret; /* handling a return probe */ 323 uint8_t _t_dtrace_ast; /* saved ast flag */ 324 #ifdef __amd64 325 uint8_t _t_dtrace_reg; /* modified register */ 326 #endif 327 } _tds; 328 ulong_t _t_dtrace_ft; /* bitwise or of these flags */ 329 } _tdu; 330 #define t_dtrace_ft _tdu._t_dtrace_ft 331 #define t_dtrace_on _tdu._tds._t_dtrace_on 332 #define t_dtrace_step _tdu._tds._t_dtrace_step 333 #define t_dtrace_ret _tdu._tds._t_dtrace_ret 334 #define t_dtrace_ast _tdu._tds._t_dtrace_ast 335 #ifdef __amd64 336 #define t_dtrace_reg _tdu._tds._t_dtrace_reg 337 #endif 338 339 uintptr_t t_dtrace_pc; /* DTrace saved pc from fasttrap */ 340 uintptr_t t_dtrace_npc; /* DTrace next pc from fasttrap */ 341 uintptr_t t_dtrace_scrpc; /* DTrace per-thread scratch location */ 342 uintptr_t t_dtrace_astpc; /* DTrace return sequence location */ 343 #ifdef __amd64 344 uint64_t t_dtrace_regv; /* DTrace saved reg from fasttrap */ 345 uint64_t t_useracc; /* SMAP state saved across swtch() */ 346 #endif 347 hrtime_t t_hrtime; /* high-res last time on cpu */ 348 kmutex_t t_ctx_lock; /* protects t_ctx in removectx() */ 349 struct waitq *t_waitq; /* wait queue */ 350 kmutex_t t_wait_mutex; /* used in CV wait functions */ 351 352 char *t_name; /* thread name */ 353 354 uint64_t t_unsafe; /* unsafe to run with SMT VCPU thread */ 355 } kthread_t; 356 357 /* 358 * Thread flag (t_flag) definitions. 359 * These flags must be changed only for the current thread, 360 * and not during preemption code, since the code being 361 * preempted could be modifying the flags. 362 * 363 * For the most part these flags do not need locking. 364 * The following flags will only be changed while the thread_lock is held, 365 * to give assurrance that they are consistent with t_state: 366 * T_WAKEABLE 367 */ 368 #define T_INTR_THREAD 0x0001 /* thread is an interrupt thread */ 369 #define T_WAKEABLE 0x0002 /* thread is blocked, signals enabled */ 370 #define T_TOMASK 0x0004 /* use lwp_sigoldmask on return from signal */ 371 #define T_TALLOCSTK 0x0008 /* thread structure allocated from stk */ 372 #define T_FORKALL 0x0010 /* thread was cloned by forkall() */ 373 #define T_WOULDBLOCK 0x0020 /* for lockfs */ 374 #define T_DONTBLOCK 0x0040 /* for lockfs */ 375 #define T_DONTPEND 0x0080 /* for lockfs */ 376 #define T_SYS_PROF 0x0100 /* profiling on for duration of system call */ 377 #define T_WAITCVSEM 0x0200 /* waiting for a lwp_cv or lwp_sema on sleepq */ 378 #define T_WATCHPT 0x0400 /* thread undergoing a watchpoint emulation */ 379 #define T_PANIC 0x0800 /* thread initiated a system panic */ 380 #define T_LWPREUSE 0x1000 /* stack and LWP can be reused */ 381 #define T_CAPTURING 0x2000 /* thread is in page capture logic */ 382 #define T_VFPARENT 0x4000 /* thread is vfork parent, must call vfwait */ 383 #define T_DONTDTRACE 0x8000 /* disable DTrace probes */ 384 385 /* 386 * Flags in t_proc_flag. 387 * These flags must be modified only when holding the p_lock 388 * for the associated process. 389 */ 390 #define TP_DAEMON 0x0001 /* this is an LWP_DAEMON lwp */ 391 #define TP_HOLDLWP 0x0002 /* hold thread's lwp */ 392 #define TP_TWAIT 0x0004 /* wait to be freed by lwp_wait() */ 393 #define TP_LWPEXIT 0x0008 /* lwp has exited */ 394 #define TP_PRSTOP 0x0010 /* thread is being stopped via /proc */ 395 #define TP_CHKPT 0x0020 /* thread is being stopped via CPR checkpoint */ 396 #define TP_EXITLWP 0x0040 /* terminate this lwp */ 397 #define TP_PRVSTOP 0x0080 /* thread is virtually stopped via /proc */ 398 #define TP_MSACCT 0x0100 /* collect micro-state accounting information */ 399 #define TP_STOPPING 0x0200 /* thread is executing stop() */ 400 #define TP_WATCHPT 0x0400 /* process has watchpoints in effect */ 401 #define TP_PAUSE 0x0800 /* process is being stopped via pauselwps() */ 402 #define TP_CHANGEBIND 0x1000 /* thread has a new cpu/cpupart binding */ 403 #define TP_ZTHREAD 0x2000 /* this is a kernel thread for a zone */ 404 #define TP_WATCHSTOP 0x4000 /* thread is stopping via holdwatch() */ 405 406 /* 407 * Thread scheduler flag (t_schedflag) definitions. 408 * The thread must be locked via thread_lock() or equiv. to change these. 409 */ 410 #define TS_LOAD 0x0001 /* thread is in memory */ 411 #define TS_DONT_SWAP 0x0002 /* thread/lwp should not be swapped */ 412 #define TS_SWAPENQ 0x0004 /* swap thread when it reaches a safe point */ 413 #define TS_ON_SWAPQ 0x0008 /* thread is on the swap queue */ 414 #define TS_SIGNALLED 0x0010 /* thread was awakened by cv_signal() */ 415 #define TS_PROJWAITQ 0x0020 /* thread is on its project's waitq */ 416 #define TS_ZONEWAITQ 0x0040 /* thread is on its zone's waitq */ 417 #define TS_VCPU 0x0080 /* thread will enter guest context */ 418 #define TS_CSTART 0x0100 /* setrun() by continuelwps() */ 419 #define TS_UNPAUSE 0x0200 /* setrun() by unpauselwps() */ 420 #define TS_XSTART 0x0400 /* setrun() by SIGCONT */ 421 #define TS_PSTART 0x0800 /* setrun() by /proc */ 422 #define TS_RESUME 0x1000 /* setrun() by CPR resume process */ 423 #define TS_CREATE 0x2000 /* setrun() by syslwp_create() */ 424 #define TS_RUNQMATCH 0x4000 /* exact run queue balancing by setbackdq() */ 425 #define TS_ALLSTART \ 426 (TS_CSTART|TS_UNPAUSE|TS_XSTART|TS_PSTART|TS_RESUME|TS_CREATE) 427 #define TS_ANYWAITQ (TS_PROJWAITQ|TS_ZONEWAITQ) 428 429 /* 430 * Thread binding types 431 */ 432 #define TB_ALLHARD 0 433 #define TB_CPU_SOFT 0x01 /* soft binding to CPU */ 434 #define TB_PSET_SOFT 0x02 /* soft binding to pset */ 435 436 #define TB_CPU_SOFT_SET(t) ((t)->t_bindflag |= TB_CPU_SOFT) 437 #define TB_CPU_HARD_SET(t) ((t)->t_bindflag &= ~TB_CPU_SOFT) 438 #define TB_PSET_SOFT_SET(t) ((t)->t_bindflag |= TB_PSET_SOFT) 439 #define TB_PSET_HARD_SET(t) ((t)->t_bindflag &= ~TB_PSET_SOFT) 440 #define TB_CPU_IS_SOFT(t) ((t)->t_bindflag & TB_CPU_SOFT) 441 #define TB_CPU_IS_HARD(t) (!TB_CPU_IS_SOFT(t)) 442 #define TB_PSET_IS_SOFT(t) ((t)->t_bindflag & TB_PSET_SOFT) 443 444 /* 445 * No locking needed for AST field. 446 */ 447 #define aston(t) ((t)->t_astflag = 1) 448 #define astoff(t) ((t)->t_astflag = 0) 449 450 /* True if thread is stopped on an event of interest */ 451 #define ISTOPPED(t) ((t)->t_state == TS_STOPPED && \ 452 !((t)->t_schedflag & TS_PSTART)) 453 454 /* True if thread is asleep and wakeable */ 455 #define ISWAKEABLE(t) (((t)->t_state == TS_SLEEP && \ 456 ((t)->t_flag & T_WAKEABLE))) 457 458 /* True if thread is on the wait queue */ 459 #define ISWAITING(t) ((t)->t_state == TS_WAIT) 460 461 /* similar to ISTOPPED except the event of interest is CPR */ 462 #define CPR_ISTOPPED(t) ((t)->t_state == TS_STOPPED && \ 463 !((t)->t_schedflag & TS_RESUME)) 464 465 /* 466 * True if thread is virtually stopped (is or was asleep in 467 * one of the lwp_*() system calls and marked to stop by /proc.) 468 */ 469 #define VSTOPPED(t) ((t)->t_proc_flag & TP_PRVSTOP) 470 471 /* similar to VSTOPPED except the point of interest is CPR */ 472 #define CPR_VSTOPPED(t) \ 473 ((t)->t_state == TS_SLEEP && \ 474 (t)->t_wchan0 != NULL && \ 475 ((t)->t_flag & T_WAKEABLE) && \ 476 ((t)->t_proc_flag & TP_CHKPT)) 477 478 /* True if thread has been stopped by hold*() or was created stopped */ 479 #define SUSPENDED(t) ((t)->t_state == TS_STOPPED && \ 480 ((t)->t_schedflag & (TS_CSTART|TS_UNPAUSE)) != (TS_CSTART|TS_UNPAUSE)) 481 482 /* True if thread possesses an inherited priority */ 483 #define INHERITED(t) ((t)->t_epri != 0) 484 485 /* The dispatch priority of a thread */ 486 #define DISP_PRIO(t) ((t)->t_epri > (t)->t_pri ? (t)->t_epri : (t)->t_pri) 487 488 /* The assigned priority of a thread */ 489 #define ASSIGNED_PRIO(t) ((t)->t_pri) 490 491 /* 492 * Macros to determine whether a thread can be swapped. 493 * If t_lock is held, the thread is either on a processor or being swapped. 494 */ 495 #define SWAP_OK(t) (!LOCK_HELD(&(t)->t_lock)) 496 497 /* 498 * proctot(x) 499 * convert a proc pointer to a thread pointer. this only works with 500 * procs that have only one lwp. 501 * 502 * proctolwp(x) 503 * convert a proc pointer to a lwp pointer. this only works with 504 * procs that have only one lwp. 505 * 506 * ttolwp(x) 507 * convert a thread pointer to its lwp pointer. 508 * 509 * ttoproc(x) 510 * convert a thread pointer to its proc pointer. 511 * 512 * ttoproj(x) 513 * convert a thread pointer to its project pointer. 514 * 515 * ttozone(x) 516 * convert a thread pointer to its zone pointer. 517 * 518 * lwptot(x) 519 * convert a lwp pointer to its thread pointer. 520 * 521 * lwptoproc(x) 522 * convert a lwp to its proc pointer. 523 */ 524 #define proctot(x) ((x)->p_tlist) 525 #define proctolwp(x) ((x)->p_tlist->t_lwp) 526 #define ttolwp(x) ((x)->t_lwp) 527 #define ttoproc(x) ((x)->t_procp) 528 #define ttoproj(x) ((x)->t_proj) 529 #define ttozone(x) ((x)->t_procp->p_zone) 530 #define lwptot(x) ((x)->lwp_thread) 531 #define lwptoproc(x) ((x)->lwp_procp) 532 533 #define t_pc t_pcb.val[0] 534 #define t_sp t_pcb.val[1] 535 536 #ifdef _KERNEL 537 538 extern kthread_t *threadp(void); /* inline, returns thread pointer */ 539 #define curthread (threadp()) /* current thread pointer */ 540 #define curproc (ttoproc(curthread)) /* current process pointer */ 541 #define curproj (ttoproj(curthread)) /* current project pointer */ 542 #define curzone (curproc->p_zone) /* current zone pointer */ 543 544 extern struct _kthread t0; /* the scheduler thread */ 545 extern kmutex_t pidlock; /* global process lock */ 546 547 /* 548 * thread_free_lock is used by the tick accounting thread to keep a thread 549 * from being freed while it is being examined. 550 * 551 * Thread structures are 32-byte aligned structures. That is why we use the 552 * following formula. 553 */ 554 #define THREAD_FREE_BITS 10 555 #define THREAD_FREE_NUM (1 << THREAD_FREE_BITS) 556 #define THREAD_FREE_MASK (THREAD_FREE_NUM - 1) 557 #define THREAD_FREE_1 PTR24_LSB 558 #define THREAD_FREE_2 (PTR24_LSB + THREAD_FREE_BITS) 559 #define THREAD_FREE_SHIFT(t) \ 560 (((ulong_t)(t) >> THREAD_FREE_1) ^ ((ulong_t)(t) >> THREAD_FREE_2)) 561 #define THREAD_FREE_HASH(t) (THREAD_FREE_SHIFT(t) & THREAD_FREE_MASK) 562 563 typedef struct thread_free_lock { 564 kmutex_t tf_lock; 565 uchar_t tf_pad[64 - sizeof (kmutex_t)]; 566 } thread_free_lock_t; 567 568 extern void thread_free_prevent(kthread_t *); 569 extern void thread_free_allow(kthread_t *); 570 571 /* 572 * Routines to change the priority and effective priority 573 * of a thread-locked thread, whatever its state. 574 */ 575 extern int thread_change_pri(kthread_t *t, pri_t disp_pri, int front); 576 extern void thread_change_epri(kthread_t *t, pri_t disp_pri); 577 578 /* 579 * Routines that manipulate the dispatcher lock for the thread. 580 * The locking heirarchy is as follows: 581 * cpu_lock > sleepq locks > run queue locks 582 */ 583 void thread_transition(kthread_t *); /* move to transition lock */ 584 void thread_stop(kthread_t *); /* move to stop lock */ 585 void thread_lock(kthread_t *); /* lock thread and its queue */ 586 void thread_lock_high(kthread_t *); /* lock thread and its queue */ 587 void thread_onproc(kthread_t *, struct cpu *); /* set onproc state lock */ 588 589 #define thread_unlock(t) disp_lock_exit((t)->t_lockp) 590 #define thread_unlock_high(t) disp_lock_exit_high((t)->t_lockp) 591 #define thread_unlock_nopreempt(t) disp_lock_exit_nopreempt((t)->t_lockp) 592 593 #define THREAD_LOCK_HELD(t) (DISP_LOCK_HELD((t)->t_lockp)) 594 595 extern disp_lock_t transition_lock; /* lock protecting transiting threads */ 596 extern disp_lock_t stop_lock; /* lock protecting stopped threads */ 597 598 caddr_t thread_stk_init(caddr_t); /* init thread stack */ 599 600 int thread_setname(kthread_t *, const char *); 601 int thread_vsetname(kthread_t *, const char *, ...); 602 603 extern int default_binding_mode; 604 605 #endif /* _KERNEL */ 606 607 #define THREAD_NAME_MAX 32 /* includes terminating NUL */ 608 609 /* 610 * Macro to change a thread's priority. 611 */ 612 #define THREAD_CHANGE_PRI(t, pri) { \ 613 pri_t __new_pri = (pri); \ 614 DTRACE_SCHED2(change__pri, kthread_t *, (t), pri_t, __new_pri); \ 615 (t)->t_pri = __new_pri; \ 616 schedctl_set_cidpri(t); \ 617 } 618 619 /* 620 * Macro to indicate that a thread's priority is about to be changed. 621 */ 622 #define THREAD_WILLCHANGE_PRI(t, pri) { \ 623 DTRACE_SCHED2(change__pri, kthread_t *, (t), pri_t, (pri)); \ 624 } 625 626 /* 627 * Macros to change thread state and the associated lock. 628 */ 629 #define THREAD_SET_STATE(tp, state, lp) \ 630 ((tp)->t_state = state, (tp)->t_lockp = lp) 631 632 /* 633 * Point it at the transition lock, which is always held. 634 * The previosly held lock is dropped. 635 */ 636 #define THREAD_TRANSITION(tp) thread_transition(tp); 637 /* 638 * Set the thread's lock to be the transition lock, without dropping 639 * previosly held lock. 640 */ 641 #define THREAD_TRANSITION_NOLOCK(tp) ((tp)->t_lockp = &transition_lock) 642 643 /* 644 * Put thread in run state, and set the lock pointer to the dispatcher queue 645 * lock pointer provided. This lock should be held. 646 */ 647 #define THREAD_RUN(tp, lp) THREAD_SET_STATE(tp, TS_RUN, lp) 648 649 /* 650 * Put thread in wait state, and set the lock pointer to the wait queue 651 * lock pointer provided. This lock should be held. 652 */ 653 #define THREAD_WAIT(tp, lp) THREAD_SET_STATE(tp, TS_WAIT, lp) 654 655 /* 656 * Put thread in run state, and set the lock pointer to the dispatcher queue 657 * lock pointer provided (i.e., the "swapped_lock"). This lock should be held. 658 */ 659 #define THREAD_SWAP(tp, lp) THREAD_SET_STATE(tp, TS_RUN, lp) 660 661 /* 662 * Put the thread in zombie state and set the lock pointer to NULL. 663 * The NULL will catch anything that tries to lock a zombie. 664 */ 665 #define THREAD_ZOMB(tp) THREAD_SET_STATE(tp, TS_ZOMB, NULL) 666 667 /* 668 * Set the thread into ONPROC state, and point the lock at the CPUs 669 * lock for the onproc thread(s). This lock should be held, so the 670 * thread deoes not become unlocked, since these stores can be reordered. 671 */ 672 #define THREAD_ONPROC(tp, cpu) \ 673 THREAD_SET_STATE(tp, TS_ONPROC, &(cpu)->cpu_thread_lock) 674 675 /* 676 * Set the thread into the TS_SLEEP state, and set the lock pointer to 677 * to some sleep queue's lock. The new lock should already be held. 678 */ 679 #define THREAD_SLEEP(tp, lp) { \ 680 disp_lock_t *tlp; \ 681 tlp = (tp)->t_lockp; \ 682 THREAD_SET_STATE(tp, TS_SLEEP, lp); \ 683 disp_lock_exit_high(tlp); \ 684 } 685 686 /* 687 * Interrupt threads are created in TS_FREE state, and their lock 688 * points at the associated CPU's lock. 689 */ 690 #define THREAD_FREEINTR(tp, cpu) \ 691 THREAD_SET_STATE(tp, TS_FREE, &(cpu)->cpu_thread_lock) 692 693 /* if tunable kmem_stackinfo is set, fill kthread stack with a pattern */ 694 #define KMEM_STKINFO_PATTERN 0xbadcbadcbadcbadcULL 695 696 /* 697 * If tunable kmem_stackinfo is set, log the latest KMEM_LOG_STK_USAGE_SIZE 698 * dead kthreads that used their kernel stack the most. 699 */ 700 #define KMEM_STKINFO_LOG_SIZE 16 701 702 /* kthread name (cmd/lwpid) string size in the stackinfo log */ 703 #define KMEM_STKINFO_STR_SIZE 64 704 705 /* 706 * stackinfo logged data. 707 */ 708 typedef struct kmem_stkinfo { 709 caddr_t kthread; /* kthread pointer */ 710 caddr_t t_startpc; /* where kthread started */ 711 caddr_t start; /* kthread stack start address */ 712 size_t stksz; /* kthread stack size */ 713 size_t percent; /* kthread stack high water mark */ 714 id_t t_tid; /* kthread id */ 715 char cmd[KMEM_STKINFO_STR_SIZE]; /* kthread name (cmd/lwpid) */ 716 } kmem_stkinfo_t; 717 718 #ifdef __cplusplus 719 } 720 #endif 721 722 #endif /* _SYS_THREAD_H */