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