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