xref: /onnv-gate/usr/src/uts/common/sys/thread.h (revision 11746:9eb67e589e3c)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52646Strevtom  * Common Development and Distribution License (the "License").
62646Strevtom  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
213828Sraf 
220Sstevel@tonic-gate /*
23*11746SMadhavan.Venkataraman@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #ifndef	_SYS_THREAD_H
280Sstevel@tonic-gate #define	_SYS_THREAD_H
290Sstevel@tonic-gate 
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <sys/t_lock.h>
330Sstevel@tonic-gate #include <sys/klwp.h>
340Sstevel@tonic-gate #include <sys/time.h>
350Sstevel@tonic-gate #include <sys/signal.h>
360Sstevel@tonic-gate #include <sys/kcpc.h>
370Sstevel@tonic-gate #if defined(__GNUC__) && defined(_ASM_INLINES) && defined(_KERNEL)
380Sstevel@tonic-gate #include <asm/thread.h>
390Sstevel@tonic-gate #endif
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #ifdef	__cplusplus
420Sstevel@tonic-gate extern "C" {
430Sstevel@tonic-gate #endif
440Sstevel@tonic-gate 
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate  * The thread object, its states, and the methods by which it
470Sstevel@tonic-gate  * is accessed.
480Sstevel@tonic-gate  */
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate  * Values that t_state may assume. Note that t_state cannot have more
520Sstevel@tonic-gate  * than one of these flags set at a time.
530Sstevel@tonic-gate  */
540Sstevel@tonic-gate #define	TS_FREE		0x00	/* Thread at loose ends */
550Sstevel@tonic-gate #define	TS_SLEEP	0x01	/* Awaiting an event */
560Sstevel@tonic-gate #define	TS_RUN		0x02	/* Runnable, but not yet on a processor */
570Sstevel@tonic-gate #define	TS_ONPROC	0x04	/* Thread is being run on a processor */
580Sstevel@tonic-gate #define	TS_ZOMB		0x08	/* Thread has died but hasn't been reaped */
590Sstevel@tonic-gate #define	TS_STOPPED	0x10	/* Stopped, initial state */
603792Sakolb #define	TS_WAIT		0x20	/* Waiting to become runnable */
610Sstevel@tonic-gate 
620Sstevel@tonic-gate typedef struct ctxop {
630Sstevel@tonic-gate 	void	(*save_op)(void *);	/* function to invoke to save context */
640Sstevel@tonic-gate 	void	(*restore_op)(void *);	/* function to invoke to restore ctx */
650Sstevel@tonic-gate 	void	(*fork_op)(void *, void *);	/* invoke to fork context */
660Sstevel@tonic-gate 	void	(*lwp_create_op)(void *, void *);	/* lwp_create context */
670Sstevel@tonic-gate 	void	(*exit_op)(void *);	/* invoked during {thread,lwp}_exit() */
680Sstevel@tonic-gate 	void	(*free_op)(void *, int); /* function which frees the context */
690Sstevel@tonic-gate 	void	*arg;		/* argument to above functions, ctx pointer */
700Sstevel@tonic-gate 	struct ctxop *next;	/* next context ops */
710Sstevel@tonic-gate } ctxop_t;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate  * The active file descriptor table.
750Sstevel@tonic-gate  * Each member of a_fd[] not equalling -1 represents an active fd.
760Sstevel@tonic-gate  * The structure is initialized on first use; all zeros means uninitialized.
770Sstevel@tonic-gate  */
7810470SRoger.Faulkner@Sun.COM typedef struct {
7910470SRoger.Faulkner@Sun.COM 	kmutex_t a_fdlock;	/* protects a_fd and a_nfd */
800Sstevel@tonic-gate 	int	*a_fd;		/* pointer to list of fds */
8110470SRoger.Faulkner@Sun.COM 	int	a_nfd;		/* number of entries in *a_fd */
8210470SRoger.Faulkner@Sun.COM 	int	a_stale;	/* one of the active fds is being closed */
8310470SRoger.Faulkner@Sun.COM 	int	a_buf[2];	/* buffer to which a_fd initially refers */
840Sstevel@tonic-gate } afd_t;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate /*
870Sstevel@tonic-gate  * An lwpchan provides uniqueness when sleeping on user-level
880Sstevel@tonic-gate  * synchronization primitives.  The lc_wchan member is used
890Sstevel@tonic-gate  * for sleeping on kernel synchronization primitives.
900Sstevel@tonic-gate  */
910Sstevel@tonic-gate typedef struct {
920Sstevel@tonic-gate 	caddr_t lc_wchan0;
930Sstevel@tonic-gate 	caddr_t lc_wchan;
940Sstevel@tonic-gate } lwpchan_t;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate typedef struct _kthread	*kthread_id_t;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate struct turnstile;
995084Sjohnlev struct panic_trap_info;
1000Sstevel@tonic-gate struct upimutex;
1010Sstevel@tonic-gate struct kproject;
1020Sstevel@tonic-gate struct on_trap_data;
1033792Sakolb struct waitq;
1046275Strevtom struct _kcpc_ctx;
1056275Strevtom struct _kcpc_set;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate /* Definition for kernel thread identifier type */
1080Sstevel@tonic-gate typedef uint64_t kt_did_t;
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate typedef struct _kthread {
1110Sstevel@tonic-gate 	struct _kthread	*t_link; /* dispq, sleepq, and free queue link */
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	caddr_t	t_stk;		/* base of stack (kernel sp value to use) */
1140Sstevel@tonic-gate 	void	(*t_startpc)(void);	/* PC where thread started */
1150Sstevel@tonic-gate 	struct cpu *t_bound_cpu; /* cpu bound to, or NULL if not bound */
1160Sstevel@tonic-gate 	short	t_affinitycnt;	/* nesting level of kernel affinity-setting */
1170Sstevel@tonic-gate 	short	t_bind_cpu;	/* user-specified CPU binding (-1 if none) */
1180Sstevel@tonic-gate 	ushort_t t_flag;		/* modified only by current thread */
1190Sstevel@tonic-gate 	ushort_t t_proc_flag;	/* modified holding ttproc(t)->p_lock */
1200Sstevel@tonic-gate 	ushort_t t_schedflag;	/* modified holding thread_lock(t) */
1210Sstevel@tonic-gate 	volatile char t_preempt;	/* don't preempt thread if set */
1220Sstevel@tonic-gate 	volatile char t_preempt_lk;
1230Sstevel@tonic-gate 	uint_t	t_state;	/* thread state	(protected by thread_lock) */
1240Sstevel@tonic-gate 	pri_t	t_pri;		/* assigned thread priority */
1250Sstevel@tonic-gate 	pri_t	t_epri;		/* inherited thread priority */
1266247Sraf 	pri_t	t_cpri;		/* thread scheduling class priority */
1270Sstevel@tonic-gate 	char	t_writer;	/* sleeping in lwp_rwlock_lock(RW_WRITE_LOCK) */
1286298Sakolb 	uchar_t	t_bindflag;	/* CPU and pset binding type */
1290Sstevel@tonic-gate 	label_t	t_pcb;		/* pcb, save area when switching */
1300Sstevel@tonic-gate 	lwpchan_t t_lwpchan;	/* reason for blocking */
1310Sstevel@tonic-gate #define	t_wchan0	t_lwpchan.lc_wchan0
1320Sstevel@tonic-gate #define	t_wchan		t_lwpchan.lc_wchan
1330Sstevel@tonic-gate 	struct _sobj_ops *t_sobj_ops;
1340Sstevel@tonic-gate 	id_t	t_cid;		/* scheduling class id */
1350Sstevel@tonic-gate 	struct thread_ops *t_clfuncs;	/* scheduling class ops vector */
1360Sstevel@tonic-gate 	void	*t_cldata;	/* per scheduling class specific data */
1370Sstevel@tonic-gate 	ctxop_t	*t_ctx;		/* thread context */
1380Sstevel@tonic-gate 	uintptr_t t_lofault;	/* ret pc for failed page faults */
1390Sstevel@tonic-gate 	label_t	*t_onfault;	/* on_fault() setjmp buf */
1400Sstevel@tonic-gate 	struct on_trap_data *t_ontrap;	/* on_trap() protection data */
14111292SJonathan.Adams@Sun.COM 	caddr_t t_swap;		/* the bottom of the stack, if from segkp */
1420Sstevel@tonic-gate 	lock_t	t_lock;		/* used to resume() a thread */
1430Sstevel@tonic-gate 	uint8_t	t_lockstat;	/* set while thread is in lockstat code */
1440Sstevel@tonic-gate 	uint8_t	t_pil;		/* interrupt thread PIL */
1450Sstevel@tonic-gate 	disp_lock_t	t_pi_lock;	/* lock protecting t_prioinv list */
1460Sstevel@tonic-gate 	char	t_nomigrate;	/* do not migrate if set */
1470Sstevel@tonic-gate 	struct cpu	*t_cpu;	/* CPU that thread last ran on */
1480Sstevel@tonic-gate 	struct cpu	*t_weakbound_cpu;	/* cpu weakly bound to */
1490Sstevel@tonic-gate 	struct lgrp_ld	*t_lpl;	/* load average for home lgroup */
1500Sstevel@tonic-gate 	void		*t_lgrp_reserv[2];	/* reserved for future */
1510Sstevel@tonic-gate 	struct _kthread	*t_intr; /* interrupted (pinned) thread */
1520Sstevel@tonic-gate 	uint64_t	t_intr_start;	/* timestamp when time slice began */
1530Sstevel@tonic-gate 	kt_did_t	t_did;	/* thread id for kernel debuggers */
1540Sstevel@tonic-gate 	caddr_t t_tnf_tpdp;	/* Trace facility data pointer */
1556275Strevtom 	struct _kcpc_ctx *t_cpc_ctx;	/* performance counter context */
1566275Strevtom 	struct _kcpc_set *t_cpc_set;	/* set this thread has bound */
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	/*
1590Sstevel@tonic-gate 	 * non swappable part of the lwp state.
1600Sstevel@tonic-gate 	 */
1610Sstevel@tonic-gate 	id_t		t_tid;		/* lwp's id */
1620Sstevel@tonic-gate 	id_t		t_waitfor;	/* target lwp id in lwp_wait() */
1630Sstevel@tonic-gate 	struct sigqueue	*t_sigqueue;	/* queue of siginfo structs */
1640Sstevel@tonic-gate 	k_sigset_t	t_sig;		/* signals pending to this process */
1650Sstevel@tonic-gate 	k_sigset_t	t_extsig;	/* signals sent from another contract */
1660Sstevel@tonic-gate 	k_sigset_t	t_hold;		/* hold signal bit mask */
1679385SRoger.Faulkner@Sun.COM 	k_sigset_t	t_sigwait;	/* sigtimedwait() is accepting these */
1680Sstevel@tonic-gate 	struct	_kthread *t_forw;	/* process's forward thread link */
1690Sstevel@tonic-gate 	struct	_kthread *t_back;	/* process's backward thread link */
1700Sstevel@tonic-gate 	struct	_kthread *t_thlink;	/* tid (lwpid) lookup hash link */
1710Sstevel@tonic-gate 	klwp_t	*t_lwp;			/* thread's lwp pointer */
1720Sstevel@tonic-gate 	struct	proc	*t_procp;	/* proc pointer */
1730Sstevel@tonic-gate 	struct	t_audit_data *t_audit_data;	/* per thread audit data */
1740Sstevel@tonic-gate 	struct	_kthread *t_next;	/* doubly linked list of all threads */
1750Sstevel@tonic-gate 	struct	_kthread *t_prev;
1760Sstevel@tonic-gate 	ushort_t t_whystop;		/* reason for stopping */
1770Sstevel@tonic-gate 	ushort_t t_whatstop;		/* more detailed reason */
1780Sstevel@tonic-gate 	int	t_dslot;		/* index in proc's thread directory */
1790Sstevel@tonic-gate 	struct	pollstate *t_pollstate;	/* state used during poll(2) */
1800Sstevel@tonic-gate 	struct	pollcache *t_pollcache;	/* to pass a pcache ptr by /dev/poll */
1810Sstevel@tonic-gate 	struct	cred	*t_cred;	/* pointer to current cred */
1820Sstevel@tonic-gate 	time_t	t_start;		/* start time, seconds since epoch */
1830Sstevel@tonic-gate 	clock_t	t_lbolt;		/* lbolt at last clock_tick() */
1840Sstevel@tonic-gate 	hrtime_t t_stoptime;		/* timestamp at stop() */
1850Sstevel@tonic-gate 	uint_t	t_pctcpu;		/* %cpu at last clock_tick(), binary */
1860Sstevel@tonic-gate 					/* point at right of high-order bit */
1870Sstevel@tonic-gate 	short	t_sysnum;		/* system call number */
1880Sstevel@tonic-gate 	kcondvar_t	t_delay_cv;
1890Sstevel@tonic-gate 	kmutex_t	t_delay_lock;
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	/*
1920Sstevel@tonic-gate 	 * Pointer to the dispatcher lock protecting t_state and state-related
1930Sstevel@tonic-gate 	 * flags.  This pointer can change during waits on the lock, so
1940Sstevel@tonic-gate 	 * it should be grabbed only by thread_lock().
1950Sstevel@tonic-gate 	 */
1960Sstevel@tonic-gate 	disp_lock_t	*t_lockp;	/* pointer to the dispatcher lock */
1970Sstevel@tonic-gate 	ushort_t 	t_oldspl;	/* spl level before dispatcher locked */
1980Sstevel@tonic-gate 	volatile char	t_pre_sys;	/* pre-syscall work needed */
1990Sstevel@tonic-gate 	lock_t		t_lock_flush;	/* for lock_mutex_flush() impl */
2000Sstevel@tonic-gate 	struct _disp	*t_disp_queue;	/* run queue for chosen CPU */
2010Sstevel@tonic-gate 	clock_t		t_disp_time;	/* last time this thread was running */
2020Sstevel@tonic-gate 	uint_t		t_kpri_req;	/* kernel priority required */
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	/*
2050Sstevel@tonic-gate 	 * Post-syscall / post-trap flags.
2060Sstevel@tonic-gate 	 * 	No lock is required to set these.
2070Sstevel@tonic-gate 	 *	These must be cleared only by the thread itself.
2080Sstevel@tonic-gate 	 *
2090Sstevel@tonic-gate 	 *	t_astflag indicates that some post-trap processing is required,
2100Sstevel@tonic-gate 	 *		possibly a signal or a preemption.  The thread will not
2110Sstevel@tonic-gate 	 *		return to user with this set.
2120Sstevel@tonic-gate 	 *	t_post_sys indicates that some unusualy post-system call
2130Sstevel@tonic-gate 	 *		handling is required, such as an error or tracing.
2140Sstevel@tonic-gate 	 *	t_sig_check indicates that some condition in ISSIG() must be
2150Sstevel@tonic-gate 	 * 		checked, but doesn't prevent returning to user.
2160Sstevel@tonic-gate 	 *	t_post_sys_ast is a way of checking whether any of these three
2170Sstevel@tonic-gate 	 *		flags are set.
2180Sstevel@tonic-gate 	 */
2190Sstevel@tonic-gate 	union __tu {
2200Sstevel@tonic-gate 		struct __ts {
2210Sstevel@tonic-gate 			volatile char	_t_astflag;	/* AST requested */
2220Sstevel@tonic-gate 			volatile char	_t_sig_check;	/* ISSIG required */
2230Sstevel@tonic-gate 			volatile char	_t_post_sys;	/* post_syscall req */
2240Sstevel@tonic-gate 			volatile char	_t_trapret;	/* call CL_TRAPRET */
2250Sstevel@tonic-gate 		} _ts;
2260Sstevel@tonic-gate 		volatile int	_t_post_sys_ast;	/* OR of these flags */
2270Sstevel@tonic-gate 	} _tu;
2280Sstevel@tonic-gate #define	t_astflag	_tu._ts._t_astflag
2290Sstevel@tonic-gate #define	t_sig_check	_tu._ts._t_sig_check
2300Sstevel@tonic-gate #define	t_post_sys	_tu._ts._t_post_sys
2310Sstevel@tonic-gate #define	t_trapret	_tu._ts._t_trapret
2320Sstevel@tonic-gate #define	t_post_sys_ast	_tu._t_post_sys_ast
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	/*
2350Sstevel@tonic-gate 	 * Real time microstate profiling.
2360Sstevel@tonic-gate 	 */
2370Sstevel@tonic-gate 					/* possible 4-byte filler */
2380Sstevel@tonic-gate 	hrtime_t t_waitrq;		/* timestamp for run queue wait time */
2390Sstevel@tonic-gate 	int	t_mstate;		/* current microstate */
2400Sstevel@tonic-gate 	struct rprof {
2410Sstevel@tonic-gate 		int	rp_anystate;		/* set if any state non-zero */
2420Sstevel@tonic-gate 		uint_t	rp_state[NMSTATES];	/* mstate profiling counts */
2430Sstevel@tonic-gate 	} *t_rprof;
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	/*
2460Sstevel@tonic-gate 	 * There is a turnstile inserted into the list below for
2470Sstevel@tonic-gate 	 * every priority inverted synchronization object that
2480Sstevel@tonic-gate 	 * this thread holds.
2490Sstevel@tonic-gate 	 */
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	struct turnstile *t_prioinv;
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	/*
2540Sstevel@tonic-gate 	 * Pointer to the turnstile attached to the synchronization
2550Sstevel@tonic-gate 	 * object where this thread is blocked.
2560Sstevel@tonic-gate 	 */
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	struct turnstile *t_ts;
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	/*
2610Sstevel@tonic-gate 	 * kernel thread specific data
2620Sstevel@tonic-gate 	 *	Borrowed from userland implementation of POSIX tsd
2630Sstevel@tonic-gate 	 */
2640Sstevel@tonic-gate 	struct tsd_thread {
2650Sstevel@tonic-gate 		struct tsd_thread *ts_next;	/* threads with TSD */
2660Sstevel@tonic-gate 		struct tsd_thread *ts_prev;	/* threads with TSD */
2670Sstevel@tonic-gate 		uint_t		  ts_nkeys;	/* entries in value array */
2680Sstevel@tonic-gate 		void		  **ts_value;	/* array of value/key */
2690Sstevel@tonic-gate 	} *t_tsd;
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	clock_t		t_stime;	/* time stamp used by the swapper */
2720Sstevel@tonic-gate 	struct door_data *t_door;	/* door invocation data */
2730Sstevel@tonic-gate 	kmutex_t	*t_plockp;	/* pointer to process's p_lock */
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	struct sc_shared *t_schedctl;	/* scheduler activations shared data */
2760Sstevel@tonic-gate 	uintptr_t	t_sc_uaddr;	/* user-level address of shared data */
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	struct cpupart	*t_cpupart;	/* partition containing thread */
2790Sstevel@tonic-gate 	int		t_bind_pset;	/* processor set binding */
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	struct copyops	*t_copyops;	/* copy in/out ops vector */
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	caddr_t		t_stkbase;	/* base of the the stack */
2840Sstevel@tonic-gate 	struct page	*t_red_pp;	/* if non-NULL, redzone is mapped */
2850Sstevel@tonic-gate 
28610470SRoger.Faulkner@Sun.COM 	afd_t		t_activefd;	/* active file descriptor table */
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	struct _kthread	*t_priforw;	/* sleepq per-priority sublist */
2890Sstevel@tonic-gate 	struct _kthread	*t_priback;
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	struct sleepq	*t_sleepq;	/* sleep queue thread is waiting on */
2925084Sjohnlev 	struct panic_trap_info *t_panic_trap;	/* saved data from fatal trap */
2930Sstevel@tonic-gate 	int		*t_lgrp_affinity;	/* lgroup affinity */
2940Sstevel@tonic-gate 	struct upimutex	*t_upimutex;	/* list of upimutexes owned by thread */
2950Sstevel@tonic-gate 	uint32_t	t_nupinest;	/* number of nested held upi mutexes */
2960Sstevel@tonic-gate 	struct kproject *t_proj;	/* project containing this thread */
2970Sstevel@tonic-gate 	uint8_t		t_unpark;	/* modified holding t_delay_lock */
2980Sstevel@tonic-gate 	uint8_t		t_release;	/* lwp_release() waked up the thread */
2990Sstevel@tonic-gate 	uint8_t		t_hatdepth;	/* depth of recursive hat_memloads */
3005741Smrj 	uint8_t		t_xpvcntr;	/* see xen_block_migrate() */
3010Sstevel@tonic-gate 	kcondvar_t	t_joincv;	/* cv used to wait for thread exit */
3020Sstevel@tonic-gate 	void		*t_taskq;	/* for threads belonging to taskq */
3030Sstevel@tonic-gate 	hrtime_t	t_anttime;	/* most recent time anticipatory load */
3040Sstevel@tonic-gate 					/*	was added to an lgroup's load */
3050Sstevel@tonic-gate 					/*	on this thread's behalf */
3060Sstevel@tonic-gate 	char		*t_pdmsg;	/* privilege debugging message */
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	uint_t		t_predcache;	/* DTrace predicate cache */
3090Sstevel@tonic-gate 	hrtime_t	t_dtrace_vtime;	/* DTrace virtual time */
3100Sstevel@tonic-gate 	hrtime_t	t_dtrace_start;	/* DTrace slice start time */
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	uint8_t		t_dtrace_stop;	/* indicates a DTrace-desired stop */
3130Sstevel@tonic-gate 	uint8_t		t_dtrace_sig;	/* signal sent via DTrace's raise() */
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	union __tdu {
3160Sstevel@tonic-gate 		struct __tds {
3170Sstevel@tonic-gate 			uint8_t	_t_dtrace_on;	/* hit a fasttrap tracepoint */
3180Sstevel@tonic-gate 			uint8_t	_t_dtrace_step;	/* about to return to kernel */
3190Sstevel@tonic-gate 			uint8_t	_t_dtrace_ret;	/* handling a return probe */
3200Sstevel@tonic-gate 			uint8_t	_t_dtrace_ast;	/* saved ast flag */
3210Sstevel@tonic-gate #ifdef __amd64
3220Sstevel@tonic-gate 			uint8_t	_t_dtrace_reg;	/* modified register */
3230Sstevel@tonic-gate #endif
3240Sstevel@tonic-gate 		} _tds;
3250Sstevel@tonic-gate 		ulong_t	_t_dtrace_ft;		/* bitwise or of these flags */
3260Sstevel@tonic-gate 	} _tdu;
3270Sstevel@tonic-gate #define	t_dtrace_ft	_tdu._t_dtrace_ft
3280Sstevel@tonic-gate #define	t_dtrace_on	_tdu._tds._t_dtrace_on
3290Sstevel@tonic-gate #define	t_dtrace_step	_tdu._tds._t_dtrace_step
3300Sstevel@tonic-gate #define	t_dtrace_ret	_tdu._tds._t_dtrace_ret
3310Sstevel@tonic-gate #define	t_dtrace_ast	_tdu._tds._t_dtrace_ast
3320Sstevel@tonic-gate #ifdef __amd64
3330Sstevel@tonic-gate #define	t_dtrace_reg	_tdu._tds._t_dtrace_reg
3340Sstevel@tonic-gate #endif
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	uintptr_t	t_dtrace_pc;	/* DTrace saved pc from fasttrap */
3370Sstevel@tonic-gate 	uintptr_t	t_dtrace_npc;	/* DTrace next pc from fasttrap */
3380Sstevel@tonic-gate 	uintptr_t	t_dtrace_scrpc;	/* DTrace per-thread scratch location */
3390Sstevel@tonic-gate 	uintptr_t	t_dtrace_astpc;	/* DTrace return sequence location */
3400Sstevel@tonic-gate #ifdef __amd64
3410Sstevel@tonic-gate 	uint64_t	t_dtrace_regv;	/* DTrace saved reg from fasttrap */
3420Sstevel@tonic-gate #endif
3430Sstevel@tonic-gate 	hrtime_t	t_hrtime;	/* high-res last time on cpu */
3442646Strevtom 	kmutex_t	t_ctx_lock;	/* protects t_ctx in removectx() */
3453792Sakolb 	struct waitq	*t_waitq;	/* wait queue */
3468566SMadhavan.Venkataraman@Sun.COM 	kmutex_t	t_wait_mutex;	/* used in CV wait functions */
3470Sstevel@tonic-gate } kthread_t;
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate /*
3500Sstevel@tonic-gate  * Thread flag (t_flag) definitions.
3510Sstevel@tonic-gate  *	These flags must be changed only for the current thread,
3520Sstevel@tonic-gate  * 	and not during preemption code, since the code being
3530Sstevel@tonic-gate  *	preempted could be modifying the flags.
3540Sstevel@tonic-gate  *
3550Sstevel@tonic-gate  *	For the most part these flags do not need locking.
3560Sstevel@tonic-gate  *	The following flags will only be changed while the thread_lock is held,
3570Sstevel@tonic-gate  *	to give assurrance that they are consistent with t_state:
3580Sstevel@tonic-gate  *		T_WAKEABLE
3590Sstevel@tonic-gate  */
3600Sstevel@tonic-gate #define	T_INTR_THREAD	0x0001	/* thread is an interrupt thread */
3610Sstevel@tonic-gate #define	T_WAKEABLE	0x0002	/* thread is blocked, signals enabled */
3620Sstevel@tonic-gate #define	T_TOMASK	0x0004	/* use lwp_sigoldmask on return from signal */
3630Sstevel@tonic-gate #define	T_TALLOCSTK	0x0008  /* thread structure allocated from stk */
364769Sraf #define	T_FORKALL	0x0010	/* thread was cloned by forkall() */
3650Sstevel@tonic-gate #define	T_WOULDBLOCK	0x0020	/* for lockfs */
3660Sstevel@tonic-gate #define	T_DONTBLOCK	0x0040	/* for lockfs */
3670Sstevel@tonic-gate #define	T_DONTPEND	0x0080	/* for lockfs */
3680Sstevel@tonic-gate #define	T_SYS_PROF	0x0100	/* profiling on for duration of system call */
3690Sstevel@tonic-gate #define	T_WAITCVSEM	0x0200	/* waiting for a lwp_cv or lwp_sema on sleepq */
3700Sstevel@tonic-gate #define	T_WATCHPT	0x0400	/* thread undergoing a watchpoint emulation */
3710Sstevel@tonic-gate #define	T_PANIC		0x0800	/* thread initiated a system panic */
37211292SJonathan.Adams@Sun.COM #define	T_LWPREUSE	0x1000	/* stack and LWP can be reused */
3733253Smec #define	T_CAPTURING	0x2000	/* thread is in page capture logic */
3743828Sraf #define	T_VFPARENT	0x4000	/* thread is vfork parent, must call vfwait */
3754421Smb158278 #define	T_DONTDTRACE	0x8000  /* disable DTrace probes */
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate /*
3780Sstevel@tonic-gate  * Flags in t_proc_flag.
3790Sstevel@tonic-gate  *	These flags must be modified only when holding the p_lock
3800Sstevel@tonic-gate  *	for the associated process.
3810Sstevel@tonic-gate  */
3820Sstevel@tonic-gate #define	TP_DAEMON	0x0001	/* this is an LWP_DAEMON lwp */
3830Sstevel@tonic-gate #define	TP_HOLDLWP	0x0002	/* hold thread's lwp */
3840Sstevel@tonic-gate #define	TP_TWAIT	0x0004	/* wait to be freed by lwp_wait() */
3850Sstevel@tonic-gate #define	TP_LWPEXIT	0x0008	/* lwp has exited */
3860Sstevel@tonic-gate #define	TP_PRSTOP	0x0010	/* thread is being stopped via /proc */
3870Sstevel@tonic-gate #define	TP_CHKPT	0x0020	/* thread is being stopped via CPR checkpoint */
3880Sstevel@tonic-gate #define	TP_EXITLWP	0x0040	/* terminate this lwp */
3890Sstevel@tonic-gate #define	TP_PRVSTOP	0x0080	/* thread is virtually stopped via /proc */
3900Sstevel@tonic-gate #define	TP_MSACCT	0x0100	/* collect micro-state accounting information */
3910Sstevel@tonic-gate #define	TP_STOPPING	0x0200	/* thread is executing stop() */
3920Sstevel@tonic-gate #define	TP_WATCHPT	0x0400	/* process has watchpoints in effect */
3930Sstevel@tonic-gate #define	TP_PAUSE	0x0800	/* process is being stopped via pauselwps() */
3940Sstevel@tonic-gate #define	TP_CHANGEBIND	0x1000	/* thread has a new cpu/cpupart binding */
3950Sstevel@tonic-gate #define	TP_ZTHREAD	0x2000	/* this is a kernel thread for a zone */
3960Sstevel@tonic-gate #define	TP_WATCHSTOP	0x4000	/* thread is stopping via holdwatch() */
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate /*
3990Sstevel@tonic-gate  * Thread scheduler flag (t_schedflag) definitions.
4000Sstevel@tonic-gate  *	The thread must be locked via thread_lock() or equiv. to change these.
4010Sstevel@tonic-gate  */
4020Sstevel@tonic-gate #define	TS_LOAD		0x0001	/* thread is in memory */
4030Sstevel@tonic-gate #define	TS_DONT_SWAP	0x0002	/* thread/lwp should not be swapped */
4040Sstevel@tonic-gate #define	TS_SWAPENQ	0x0004	/* swap thread when it reaches a safe point */
4050Sstevel@tonic-gate #define	TS_ON_SWAPQ	0x0008	/* thread is on the swap queue */
4060Sstevel@tonic-gate #define	TS_SIGNALLED	0x0010	/* thread was awakened by cv_signal() */
4073792Sakolb #define	TS_PROJWAITQ	0x0020	/* thread is on its project's waitq */
4083792Sakolb #define	TS_ZONEWAITQ	0x0040	/* thread is on its zone's waitq */
4090Sstevel@tonic-gate #define	TS_CSTART	0x0100	/* setrun() by continuelwps() */
4100Sstevel@tonic-gate #define	TS_UNPAUSE	0x0200	/* setrun() by unpauselwps() */
4110Sstevel@tonic-gate #define	TS_XSTART	0x0400	/* setrun() by SIGCONT */
4120Sstevel@tonic-gate #define	TS_PSTART	0x0800	/* setrun() by /proc */
4130Sstevel@tonic-gate #define	TS_RESUME	0x1000	/* setrun() by CPR resume process */
4140Sstevel@tonic-gate #define	TS_CREATE	0x2000	/* setrun() by syslwp_create() */
4150Sstevel@tonic-gate #define	TS_RUNQMATCH	0x4000	/* exact run queue balancing by setbackdq() */
4160Sstevel@tonic-gate #define	TS_ALLSTART	\
4170Sstevel@tonic-gate 	(TS_CSTART|TS_UNPAUSE|TS_XSTART|TS_PSTART|TS_RESUME|TS_CREATE)
4183792Sakolb #define	TS_ANYWAITQ	(TS_PROJWAITQ|TS_ZONEWAITQ)
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate /*
4216298Sakolb  * Thread binding types
4226298Sakolb  */
4236298Sakolb #define	TB_ALLHARD	0
4246298Sakolb #define	TB_CPU_SOFT	0x01		/* soft binding to CPU */
4256298Sakolb #define	TB_PSET_SOFT	0x02		/* soft binding to pset */
4266298Sakolb 
4276298Sakolb #define	TB_CPU_SOFT_SET(t)		((t)->t_bindflag |= TB_CPU_SOFT)
4286298Sakolb #define	TB_CPU_HARD_SET(t)		((t)->t_bindflag &= ~TB_CPU_SOFT)
4296298Sakolb #define	TB_PSET_SOFT_SET(t)		((t)->t_bindflag |= TB_PSET_SOFT)
4306298Sakolb #define	TB_PSET_HARD_SET(t)		((t)->t_bindflag &= ~TB_PSET_SOFT)
4316298Sakolb #define	TB_CPU_IS_SOFT(t)		((t)->t_bindflag & TB_CPU_SOFT)
4326298Sakolb #define	TB_CPU_IS_HARD(t)		(!TB_CPU_IS_SOFT(t))
4336298Sakolb #define	TB_PSET_IS_SOFT(t)		((t)->t_bindflag & TB_PSET_SOFT)
4346298Sakolb 
4356298Sakolb /*
4360Sstevel@tonic-gate  * No locking needed for AST field.
4370Sstevel@tonic-gate  */
4380Sstevel@tonic-gate #define	aston(t)		((t)->t_astflag = 1)
4390Sstevel@tonic-gate #define	astoff(t)		((t)->t_astflag = 0)
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate /* True if thread is stopped on an event of interest */
4420Sstevel@tonic-gate #define	ISTOPPED(t) ((t)->t_state == TS_STOPPED && \
4430Sstevel@tonic-gate 			!((t)->t_schedflag & TS_PSTART))
4440Sstevel@tonic-gate 
4453792Sakolb /* True if thread is asleep and wakeable */
4463792Sakolb #define	ISWAKEABLE(t) (((t)->t_state == TS_SLEEP && \
4473792Sakolb 			((t)->t_flag & T_WAKEABLE)))
4483792Sakolb 
4493792Sakolb /* True if thread is on the wait queue */
4503792Sakolb #define	ISWAITING(t) ((t)->t_state == TS_WAIT)
4513792Sakolb 
4520Sstevel@tonic-gate /* similar to ISTOPPED except the event of interest is CPR */
4530Sstevel@tonic-gate #define	CPR_ISTOPPED(t) ((t)->t_state == TS_STOPPED && \
4540Sstevel@tonic-gate 			!((t)->t_schedflag & TS_RESUME))
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate /*
4570Sstevel@tonic-gate  * True if thread is virtually stopped (is or was asleep in
4580Sstevel@tonic-gate  * one of the lwp_*() system calls and marked to stop by /proc.)
4590Sstevel@tonic-gate  */
4600Sstevel@tonic-gate #define	VSTOPPED(t)	((t)->t_proc_flag & TP_PRVSTOP)
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate /* similar to VSTOPPED except the point of interest is CPR */
4630Sstevel@tonic-gate #define	CPR_VSTOPPED(t)				\
4640Sstevel@tonic-gate 	((t)->t_state == TS_SLEEP &&		\
4650Sstevel@tonic-gate 	(t)->t_wchan0 != NULL &&		\
4660Sstevel@tonic-gate 	((t)->t_flag & T_WAKEABLE) &&		\
4670Sstevel@tonic-gate 	((t)->t_proc_flag & TP_CHKPT))
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate /* True if thread has been stopped by hold*() or was created stopped */
4700Sstevel@tonic-gate #define	SUSPENDED(t) ((t)->t_state == TS_STOPPED && \
4710Sstevel@tonic-gate 	((t)->t_schedflag & (TS_CSTART|TS_UNPAUSE)) != (TS_CSTART|TS_UNPAUSE))
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate /* True if thread possesses an inherited priority */
4740Sstevel@tonic-gate #define	INHERITED(t)	((t)->t_epri != 0)
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate /* The dispatch priority of a thread */
4770Sstevel@tonic-gate #define	DISP_PRIO(t) ((t)->t_epri > (t)->t_pri ? (t)->t_epri : (t)->t_pri)
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate /* The assigned priority of a thread */
4800Sstevel@tonic-gate #define	ASSIGNED_PRIO(t)	((t)->t_pri)
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate /*
4830Sstevel@tonic-gate  * Macros to determine whether a thread can be swapped.
4840Sstevel@tonic-gate  * If t_lock is held, the thread is either on a processor or being swapped.
4850Sstevel@tonic-gate  */
4860Sstevel@tonic-gate #define	SWAP_OK(t)	(!LOCK_HELD(&(t)->t_lock))
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate /*
4890Sstevel@tonic-gate  * proctot(x)
4900Sstevel@tonic-gate  *	convert a proc pointer to a thread pointer. this only works with
4910Sstevel@tonic-gate  *	procs that have only one lwp.
4920Sstevel@tonic-gate  *
4930Sstevel@tonic-gate  * proctolwp(x)
4940Sstevel@tonic-gate  *	convert a proc pointer to a lwp pointer. this only works with
4950Sstevel@tonic-gate  *	procs that have only one lwp.
4960Sstevel@tonic-gate  *
4970Sstevel@tonic-gate  * ttolwp(x)
4980Sstevel@tonic-gate  *	convert a thread pointer to its lwp pointer.
4990Sstevel@tonic-gate  *
5000Sstevel@tonic-gate  * ttoproc(x)
5010Sstevel@tonic-gate  *	convert a thread pointer to its proc pointer.
5020Sstevel@tonic-gate  *
5030Sstevel@tonic-gate  * ttoproj(x)
5040Sstevel@tonic-gate  * 	convert a thread pointer to its project pointer.
5050Sstevel@tonic-gate  *
5063792Sakolb  * ttozone(x)
5073792Sakolb  * 	convert a thread pointer to its zone pointer.
5083792Sakolb  *
5090Sstevel@tonic-gate  * lwptot(x)
5100Sstevel@tonic-gate  *	convert a lwp pointer to its thread pointer.
5110Sstevel@tonic-gate  *
5120Sstevel@tonic-gate  * lwptoproc(x)
5130Sstevel@tonic-gate  *	convert a lwp to its proc pointer.
5140Sstevel@tonic-gate  */
5150Sstevel@tonic-gate #define	proctot(x)	((x)->p_tlist)
5160Sstevel@tonic-gate #define	proctolwp(x)	((x)->p_tlist->t_lwp)
5170Sstevel@tonic-gate #define	ttolwp(x)	((x)->t_lwp)
5180Sstevel@tonic-gate #define	ttoproc(x)	((x)->t_procp)
5190Sstevel@tonic-gate #define	ttoproj(x)	((x)->t_proj)
5203792Sakolb #define	ttozone(x)	((x)->t_procp->p_zone)
5210Sstevel@tonic-gate #define	lwptot(x)	((x)->lwp_thread)
5220Sstevel@tonic-gate #define	lwptoproc(x)	((x)->lwp_procp)
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate #define	t_pc		t_pcb.val[0]
5250Sstevel@tonic-gate #define	t_sp		t_pcb.val[1]
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate #ifdef	_KERNEL
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate extern	kthread_t	*threadp(void);	/* inline, returns thread pointer */
5300Sstevel@tonic-gate #define	curthread	(threadp())		/* current thread pointer */
5310Sstevel@tonic-gate #define	curproc		(ttoproc(curthread))	/* current process pointer */
5320Sstevel@tonic-gate #define	curproj		(ttoproj(curthread))	/* current project pointer */
5333792Sakolb #define	curzone		(curproc->p_zone)	/* current zone pointer */
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate extern	struct _kthread	t0;		/* the scheduler thread */
5360Sstevel@tonic-gate extern	kmutex_t	pidlock;	/* global process lock */
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate /*
5395788Smv143129  * thread_free_lock is used by the tick accounting thread to keep a thread
5400Sstevel@tonic-gate  * from being freed while it is being examined.
541*11746SMadhavan.Venkataraman@Sun.COM  *
542*11746SMadhavan.Venkataraman@Sun.COM  * Thread structures are 32-byte aligned structures. That is why we use the
543*11746SMadhavan.Venkataraman@Sun.COM  * following formula.
5440Sstevel@tonic-gate  */
545*11746SMadhavan.Venkataraman@Sun.COM #define	THREAD_FREE_BITS	10
546*11746SMadhavan.Venkataraman@Sun.COM #define	THREAD_FREE_NUM		(1 << THREAD_FREE_BITS)
5475788Smv143129 #define	THREAD_FREE_MASK	(THREAD_FREE_NUM - 1)
548*11746SMadhavan.Venkataraman@Sun.COM #define	THREAD_FREE_1		PTR24_LSB
549*11746SMadhavan.Venkataraman@Sun.COM #define	THREAD_FREE_2		(PTR24_LSB + THREAD_FREE_BITS)
550*11746SMadhavan.Venkataraman@Sun.COM #define	THREAD_FREE_SHIFT(t)	\
551*11746SMadhavan.Venkataraman@Sun.COM 	(((ulong_t)(t) >> THREAD_FREE_1) ^ ((ulong_t)(t) >> THREAD_FREE_2))
5525788Smv143129 #define	THREAD_FREE_HASH(t)	(THREAD_FREE_SHIFT(t) & THREAD_FREE_MASK)
5535788Smv143129 
5545788Smv143129 typedef struct thread_free_lock {
5555788Smv143129 	kmutex_t	tf_lock;
5565788Smv143129 	uchar_t		tf_pad[64 - sizeof (kmutex_t)];
5575788Smv143129 } thread_free_lock_t;
5585788Smv143129 
5595788Smv143129 extern void	thread_free_prevent(kthread_t *);
5605788Smv143129 extern void	thread_free_allow(kthread_t *);
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate /*
5630Sstevel@tonic-gate  * Routines to change the priority and effective priority
5640Sstevel@tonic-gate  * of a thread-locked thread, whatever its state.
5650Sstevel@tonic-gate  */
5660Sstevel@tonic-gate extern int	thread_change_pri(kthread_t *t, pri_t disp_pri, int front);
5670Sstevel@tonic-gate extern void	thread_change_epri(kthread_t *t, pri_t disp_pri);
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate /*
5700Sstevel@tonic-gate  * Routines that manipulate the dispatcher lock for the thread.
5710Sstevel@tonic-gate  * The locking heirarchy is as follows:
5720Sstevel@tonic-gate  *	cpu_lock > sleepq locks > run queue locks
5730Sstevel@tonic-gate  */
5740Sstevel@tonic-gate void	thread_transition(kthread_t *); /* move to transition lock */
5750Sstevel@tonic-gate void	thread_stop(kthread_t *);	/* move to stop lock */
5760Sstevel@tonic-gate void	thread_lock(kthread_t *);	/* lock thread and its queue */
5770Sstevel@tonic-gate void	thread_lock_high(kthread_t *);	/* lock thread and its queue */
5780Sstevel@tonic-gate void	thread_onproc(kthread_t *, struct cpu *); /* set onproc state lock */
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate #define	thread_unlock(t)		disp_lock_exit((t)->t_lockp)
5810Sstevel@tonic-gate #define	thread_unlock_high(t)		disp_lock_exit_high((t)->t_lockp)
5820Sstevel@tonic-gate #define	thread_unlock_nopreempt(t)	disp_lock_exit_nopreempt((t)->t_lockp)
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate #define	THREAD_LOCK_HELD(t)	(DISP_LOCK_HELD((t)->t_lockp))
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate extern disp_lock_t transition_lock;	/* lock protecting transiting threads */
5870Sstevel@tonic-gate extern disp_lock_t stop_lock;		/* lock protecting stopped threads */
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate caddr_t	thread_stk_init(caddr_t);	/* init thread stack */
5900Sstevel@tonic-gate 
5916298Sakolb extern int default_binding_mode;
5926298Sakolb 
5930Sstevel@tonic-gate #endif	/* _KERNEL */
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate /*
5960Sstevel@tonic-gate  * Macros to indicate that the thread holds resources that could be critical
5970Sstevel@tonic-gate  * to other kernel threads, so this thread needs to have kernel priority
5980Sstevel@tonic-gate  * if it blocks or is preempted.  Note that this is not necessary if the
5990Sstevel@tonic-gate  * resource is a mutex or a writer lock because of priority inheritance.
6000Sstevel@tonic-gate  *
6010Sstevel@tonic-gate  * The only way one thread may legally manipulate another thread's t_kpri_req
6020Sstevel@tonic-gate  * is to hold the target thread's thread lock while that thread is asleep.
6030Sstevel@tonic-gate  * (The rwlock code does this to implement direct handoff to waiting readers.)
6040Sstevel@tonic-gate  */
6050Sstevel@tonic-gate #define	THREAD_KPRI_REQUEST()	(curthread->t_kpri_req++)
6060Sstevel@tonic-gate #define	THREAD_KPRI_RELEASE()	(curthread->t_kpri_req--)
6070Sstevel@tonic-gate #define	THREAD_KPRI_RELEASE_N(n) (curthread->t_kpri_req -= (n))
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate /*
6100Sstevel@tonic-gate  * Macro to change a thread's priority.
6110Sstevel@tonic-gate  */
6120Sstevel@tonic-gate #define	THREAD_CHANGE_PRI(t, pri) {					\
6130Sstevel@tonic-gate 	pri_t __new_pri = (pri);					\
6140Sstevel@tonic-gate 	DTRACE_SCHED2(change__pri, kthread_t *, (t), pri_t, __new_pri);	\
6150Sstevel@tonic-gate 	(t)->t_pri = __new_pri;						\
6166247Sraf 	schedctl_set_cidpri(t);						\
6170Sstevel@tonic-gate }
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate /*
6200Sstevel@tonic-gate  * Macro to indicate that a thread's priority is about to be changed.
6210Sstevel@tonic-gate  */
6220Sstevel@tonic-gate #define	THREAD_WILLCHANGE_PRI(t, pri) {					\
6230Sstevel@tonic-gate 	DTRACE_SCHED2(change__pri, kthread_t *, (t), pri_t, (pri));	\
6240Sstevel@tonic-gate }
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate /*
6270Sstevel@tonic-gate  * Macros to change thread state and the associated lock.
6280Sstevel@tonic-gate  */
6290Sstevel@tonic-gate #define	THREAD_SET_STATE(tp, state, lp) \
6300Sstevel@tonic-gate 		((tp)->t_state = state, (tp)->t_lockp = lp)
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate /*
6330Sstevel@tonic-gate  * Point it at the transition lock, which is always held.
6340Sstevel@tonic-gate  * The previosly held lock is dropped.
6350Sstevel@tonic-gate  */
6360Sstevel@tonic-gate #define	THREAD_TRANSITION(tp) 	thread_transition(tp);
6370Sstevel@tonic-gate /*
6380Sstevel@tonic-gate  * Set the thread's lock to be the transition lock, without dropping
6390Sstevel@tonic-gate  * previosly held lock.
6400Sstevel@tonic-gate  */
6410Sstevel@tonic-gate #define	THREAD_TRANSITION_NOLOCK(tp) 	((tp)->t_lockp = &transition_lock)
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate /*
6440Sstevel@tonic-gate  * Put thread in run state, and set the lock pointer to the dispatcher queue
6450Sstevel@tonic-gate  * lock pointer provided.  This lock should be held.
6460Sstevel@tonic-gate  */
6470Sstevel@tonic-gate #define	THREAD_RUN(tp, lp)	THREAD_SET_STATE(tp, TS_RUN, lp)
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate /*
6503792Sakolb  * Put thread in wait state, and set the lock pointer to the wait queue
6513792Sakolb  * lock pointer provided.  This lock should be held.
6523792Sakolb  */
6533792Sakolb #define	THREAD_WAIT(tp, lp)	THREAD_SET_STATE(tp, TS_WAIT, lp)
6543792Sakolb 
6553792Sakolb /*
6560Sstevel@tonic-gate  * Put thread in run state, and set the lock pointer to the dispatcher queue
6570Sstevel@tonic-gate  * lock pointer provided (i.e., the "swapped_lock").  This lock should be held.
6580Sstevel@tonic-gate  */
6590Sstevel@tonic-gate #define	THREAD_SWAP(tp, lp)	THREAD_SET_STATE(tp, TS_RUN, lp)
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate /*
6620Sstevel@tonic-gate  * Put the thread in zombie state and set the lock pointer to NULL.
6630Sstevel@tonic-gate  * The NULL will catch anything that tries to lock a zombie.
6640Sstevel@tonic-gate  */
6650Sstevel@tonic-gate #define	THREAD_ZOMB(tp)		THREAD_SET_STATE(tp, TS_ZOMB, NULL)
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate /*
6680Sstevel@tonic-gate  * Set the thread into ONPROC state, and point the lock at the CPUs
6690Sstevel@tonic-gate  * lock for the onproc thread(s).  This lock should be held, so the
6700Sstevel@tonic-gate  * thread deoes not become unlocked, since these stores can be reordered.
6710Sstevel@tonic-gate  */
6720Sstevel@tonic-gate #define	THREAD_ONPROC(tp, cpu)	\
6730Sstevel@tonic-gate 		THREAD_SET_STATE(tp, TS_ONPROC, &(cpu)->cpu_thread_lock)
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate /*
6760Sstevel@tonic-gate  * Set the thread into the TS_SLEEP state, and set the lock pointer to
6770Sstevel@tonic-gate  * to some sleep queue's lock.  The new lock should already be held.
6780Sstevel@tonic-gate  */
6790Sstevel@tonic-gate #define	THREAD_SLEEP(tp, lp)	{				\
6800Sstevel@tonic-gate 			disp_lock_t	*tlp;			\
6810Sstevel@tonic-gate 			tlp = (tp)->t_lockp;			\
6820Sstevel@tonic-gate 			THREAD_SET_STATE(tp, TS_SLEEP, lp);	\
6830Sstevel@tonic-gate 			disp_lock_exit_high(tlp);		\
6840Sstevel@tonic-gate 			}
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate /*
6870Sstevel@tonic-gate  * Interrupt threads are created in TS_FREE state, and their lock
6880Sstevel@tonic-gate  * points at the associated CPU's lock.
6890Sstevel@tonic-gate  */
6900Sstevel@tonic-gate #define	THREAD_FREEINTR(tp, cpu)	\
6910Sstevel@tonic-gate 		THREAD_SET_STATE(tp, TS_FREE, &(cpu)->cpu_thread_lock)
6920Sstevel@tonic-gate 
6937854SPhilippe.Jung@Sun.COM /* if tunable kmem_stackinfo is set, fill kthread stack with a pattern */
6947854SPhilippe.Jung@Sun.COM #define	KMEM_STKINFO_PATTERN	0xbadcbadcbadcbadcULL
6957854SPhilippe.Jung@Sun.COM 
6967854SPhilippe.Jung@Sun.COM /*
6977854SPhilippe.Jung@Sun.COM  * If tunable kmem_stackinfo is set, log the latest KMEM_LOG_STK_USAGE_SIZE
6987854SPhilippe.Jung@Sun.COM  * dead kthreads that used their kernel stack the most.
6997854SPhilippe.Jung@Sun.COM  */
7007854SPhilippe.Jung@Sun.COM #define	KMEM_STKINFO_LOG_SIZE	16
7017854SPhilippe.Jung@Sun.COM 
7027854SPhilippe.Jung@Sun.COM /* kthread name (cmd/lwpid) string size in the stackinfo log */
7037854SPhilippe.Jung@Sun.COM #define	KMEM_STKINFO_STR_SIZE	64
7047854SPhilippe.Jung@Sun.COM 
7057854SPhilippe.Jung@Sun.COM /*
7067854SPhilippe.Jung@Sun.COM  * stackinfo logged data.
7077854SPhilippe.Jung@Sun.COM  */
7087854SPhilippe.Jung@Sun.COM typedef struct kmem_stkinfo {
7097854SPhilippe.Jung@Sun.COM 	caddr_t	kthread;	/* kthread pointer */
7107854SPhilippe.Jung@Sun.COM 	caddr_t	t_startpc;	/* where kthread started */
7117854SPhilippe.Jung@Sun.COM 	caddr_t	start;		/* kthread stack start address */
7127854SPhilippe.Jung@Sun.COM 	size_t	stksz;		/* kthread stack size */
7137854SPhilippe.Jung@Sun.COM 	size_t	percent;	/* kthread stack high water mark */
7147854SPhilippe.Jung@Sun.COM 	id_t	t_tid;		/* kthread id */
7157854SPhilippe.Jung@Sun.COM 	char	cmd[KMEM_STKINFO_STR_SIZE];	/* kthread name (cmd/lwpid) */
7167854SPhilippe.Jung@Sun.COM } kmem_stkinfo_t;
7177854SPhilippe.Jung@Sun.COM 
7180Sstevel@tonic-gate #ifdef	__cplusplus
7190Sstevel@tonic-gate }
7200Sstevel@tonic-gate #endif
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate #endif /* _SYS_THREAD_H */
723