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