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