xref: /illumos-gate/usr/src/uts/common/sys/thread.h (revision 454ab202)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55a64ecfaStrevtom  * Common Development and Distribution License (the "License").
65a64ecfaStrevtom  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
218d186f16Sraf 
227c478bd9Sstevel@tonic-gate /*
23*454ab202SMadhavan Venkataraman  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #ifndef	_SYS_THREAD_H
287c478bd9Sstevel@tonic-gate #define	_SYS_THREAD_H
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
337c478bd9Sstevel@tonic-gate #include <sys/klwp.h>
347c478bd9Sstevel@tonic-gate #include <sys/time.h>
357c478bd9Sstevel@tonic-gate #include <sys/signal.h>
367c478bd9Sstevel@tonic-gate #include <sys/kcpc.h>
377c478bd9Sstevel@tonic-gate #if defined(__GNUC__) && defined(_ASM_INLINES) && defined(_KERNEL)
387c478bd9Sstevel@tonic-gate #include <asm/thread.h>
397c478bd9Sstevel@tonic-gate #endif
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
427c478bd9Sstevel@tonic-gate extern "C" {
437c478bd9Sstevel@tonic-gate #endif
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * The thread object, its states, and the methods by which it
477c478bd9Sstevel@tonic-gate  * is accessed.
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  * Values that t_state may assume. Note that t_state cannot have more
527c478bd9Sstevel@tonic-gate  * than one of these flags set at a time.
537c478bd9Sstevel@tonic-gate  */
547c478bd9Sstevel@tonic-gate #define	TS_FREE		0x00	/* Thread at loose ends */
557c478bd9Sstevel@tonic-gate #define	TS_SLEEP	0x01	/* Awaiting an event */
567c478bd9Sstevel@tonic-gate #define	TS_RUN		0x02	/* Runnable, but not yet on a processor */
577c478bd9Sstevel@tonic-gate #define	TS_ONPROC	0x04	/* Thread is being run on a processor */
587c478bd9Sstevel@tonic-gate #define	TS_ZOMB		0x08	/* Thread has died but hasn't been reaped */
597c478bd9Sstevel@tonic-gate #define	TS_STOPPED	0x10	/* Stopped, initial state */
60c97ad5cdSakolb #define	TS_WAIT		0x20	/* Waiting to become runnable */
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate typedef struct ctxop {
637c478bd9Sstevel@tonic-gate 	void	(*save_op)(void *);	/* function to invoke to save context */
647c478bd9Sstevel@tonic-gate 	void	(*restore_op)(void *);	/* function to invoke to restore ctx */
657c478bd9Sstevel@tonic-gate 	void	(*fork_op)(void *, void *);	/* invoke to fork context */
667c478bd9Sstevel@tonic-gate 	void	(*lwp_create_op)(void *, void *);	/* lwp_create context */
677c478bd9Sstevel@tonic-gate 	void	(*exit_op)(void *);	/* invoked during {thread,lwp}_exit() */
687c478bd9Sstevel@tonic-gate 	void	(*free_op)(void *, int); /* function which frees the context */
697c478bd9Sstevel@tonic-gate 	void	*arg;		/* argument to above functions, ctx pointer */
707c478bd9Sstevel@tonic-gate 	struct ctxop *next;	/* next context ops */
717c478bd9Sstevel@tonic-gate } ctxop_t;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * The active file descriptor table.
757c478bd9Sstevel@tonic-gate  * Each member of a_fd[] not equalling -1 represents an active fd.
767c478bd9Sstevel@tonic-gate  * The structure is initialized on first use; all zeros means uninitialized.
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate typedef struct _afd {
797c478bd9Sstevel@tonic-gate 	int	*a_fd;		/* pointer to list of fds */
807c478bd9Sstevel@tonic-gate 	short	a_nfd;		/* number of entries in *a_fd */
817c478bd9Sstevel@tonic-gate 	short	a_stale;	/* one of the active fds is being closed */
827c478bd9Sstevel@tonic-gate 	int	a_buf[1];	/* buffer to which a_fd initially refers */
837c478bd9Sstevel@tonic-gate } afd_t;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate /*
867c478bd9Sstevel@tonic-gate  * An lwpchan provides uniqueness when sleeping on user-level
877c478bd9Sstevel@tonic-gate  * synchronization primitives.  The lc_wchan member is used
887c478bd9Sstevel@tonic-gate  * for sleeping on kernel synchronization primitives.
897c478bd9Sstevel@tonic-gate  */
907c478bd9Sstevel@tonic-gate typedef struct {
917c478bd9Sstevel@tonic-gate 	caddr_t lc_wchan0;
927c478bd9Sstevel@tonic-gate 	caddr_t lc_wchan;
937c478bd9Sstevel@tonic-gate } lwpchan_t;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate typedef struct _kthread	*kthread_id_t;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate struct turnstile;
98843e1988Sjohnlev struct panic_trap_info;
997c478bd9Sstevel@tonic-gate struct upimutex;
1007c478bd9Sstevel@tonic-gate struct kproject;
1017c478bd9Sstevel@tonic-gate struct on_trap_data;
102c97ad5cdSakolb struct waitq;
1034568bee7Strevtom struct _kcpc_ctx;
1044568bee7Strevtom struct _kcpc_set;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /* Definition for kernel thread identifier type */
1077c478bd9Sstevel@tonic-gate typedef uint64_t kt_did_t;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate typedef struct _kthread {
1107c478bd9Sstevel@tonic-gate 	struct _kthread	*t_link; /* dispq, sleepq, and free queue link */
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	caddr_t	t_stk;		/* base of stack (kernel sp value to use) */
1137c478bd9Sstevel@tonic-gate 	void	(*t_startpc)(void);	/* PC where thread started */
1147c478bd9Sstevel@tonic-gate 	struct cpu *t_bound_cpu; /* cpu bound to, or NULL if not bound */
1157c478bd9Sstevel@tonic-gate 	short	t_affinitycnt;	/* nesting level of kernel affinity-setting */
1167c478bd9Sstevel@tonic-gate 	short	t_bind_cpu;	/* user-specified CPU binding (-1 if none) */
1177c478bd9Sstevel@tonic-gate 	ushort_t t_flag;		/* modified only by current thread */
1187c478bd9Sstevel@tonic-gate 	ushort_t t_proc_flag;	/* modified holding ttproc(t)->p_lock */
1197c478bd9Sstevel@tonic-gate 	ushort_t t_schedflag;	/* modified holding thread_lock(t) */
1207c478bd9Sstevel@tonic-gate 	volatile char t_preempt;	/* don't preempt thread if set */
1217c478bd9Sstevel@tonic-gate 	volatile char t_preempt_lk;
1227c478bd9Sstevel@tonic-gate 	uint_t	t_state;	/* thread state	(protected by thread_lock) */
1237c478bd9Sstevel@tonic-gate 	pri_t	t_pri;		/* assigned thread priority */
1247c478bd9Sstevel@tonic-gate 	pri_t	t_epri;		/* inherited thread priority */
125d4204c85Sraf 	pri_t	t_cpri;		/* thread scheduling class priority */
1267c478bd9Sstevel@tonic-gate 	char	t_writer;	/* sleeping in lwp_rwlock_lock(RW_WRITE_LOCK) */
1270b70c467Sakolb 	uchar_t	t_bindflag;	/* CPU and pset binding type */
1287c478bd9Sstevel@tonic-gate 	label_t	t_pcb;		/* pcb, save area when switching */
1297c478bd9Sstevel@tonic-gate 	lwpchan_t t_lwpchan;	/* reason for blocking */
1307c478bd9Sstevel@tonic-gate #define	t_wchan0	t_lwpchan.lc_wchan0
1317c478bd9Sstevel@tonic-gate #define	t_wchan		t_lwpchan.lc_wchan
1327c478bd9Sstevel@tonic-gate 	struct _sobj_ops *t_sobj_ops;
1337c478bd9Sstevel@tonic-gate 	id_t	t_cid;		/* scheduling class id */
1347c478bd9Sstevel@tonic-gate 	struct thread_ops *t_clfuncs;	/* scheduling class ops vector */
1357c478bd9Sstevel@tonic-gate 	void	*t_cldata;	/* per scheduling class specific data */
1367c478bd9Sstevel@tonic-gate 	ctxop_t	*t_ctx;		/* thread context */
1377c478bd9Sstevel@tonic-gate 	uintptr_t t_lofault;	/* ret pc for failed page faults */
1387c478bd9Sstevel@tonic-gate 	label_t	*t_onfault;	/* on_fault() setjmp buf */
1397c478bd9Sstevel@tonic-gate 	struct on_trap_data *t_ontrap;	/* on_trap() protection data */
1407c478bd9Sstevel@tonic-gate 	caddr_t t_swap;		/* swappable thread storage */
1417c478bd9Sstevel@tonic-gate 	lock_t	t_lock;		/* used to resume() a thread */
1427c478bd9Sstevel@tonic-gate 	uint8_t	t_lockstat;	/* set while thread is in lockstat code */
1437c478bd9Sstevel@tonic-gate 	uint8_t	t_pil;		/* interrupt thread PIL */
1447c478bd9Sstevel@tonic-gate 	disp_lock_t	t_pi_lock;	/* lock protecting t_prioinv list */
1457c478bd9Sstevel@tonic-gate 	char	t_nomigrate;	/* do not migrate if set */
1467c478bd9Sstevel@tonic-gate 	struct cpu	*t_cpu;	/* CPU that thread last ran on */
1477c478bd9Sstevel@tonic-gate 	struct cpu	*t_weakbound_cpu;	/* cpu weakly bound to */
1487c478bd9Sstevel@tonic-gate 	struct lgrp_ld	*t_lpl;	/* load average for home lgroup */
1497c478bd9Sstevel@tonic-gate 	void		*t_lgrp_reserv[2];	/* reserved for future */
1507c478bd9Sstevel@tonic-gate 	struct _kthread	*t_intr; /* interrupted (pinned) thread */
1517c478bd9Sstevel@tonic-gate 	uint64_t	t_intr_start;	/* timestamp when time slice began */
1527c478bd9Sstevel@tonic-gate 	kt_did_t	t_did;	/* thread id for kernel debuggers */
1537c478bd9Sstevel@tonic-gate 	caddr_t t_tnf_tpdp;	/* Trace facility data pointer */
1544568bee7Strevtom 	struct _kcpc_ctx *t_cpc_ctx;	/* performance counter context */
1554568bee7Strevtom 	struct _kcpc_set *t_cpc_set;	/* set this thread has bound */
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	/*
1587c478bd9Sstevel@tonic-gate 	 * non swappable part of the lwp state.
1597c478bd9Sstevel@tonic-gate 	 */
1607c478bd9Sstevel@tonic-gate 	id_t		t_tid;		/* lwp's id */
1617c478bd9Sstevel@tonic-gate 	id_t		t_waitfor;	/* target lwp id in lwp_wait() */
1627c478bd9Sstevel@tonic-gate 	struct sigqueue	*t_sigqueue;	/* queue of siginfo structs */
1637c478bd9Sstevel@tonic-gate 	k_sigset_t	t_sig;		/* signals pending to this process */
1647c478bd9Sstevel@tonic-gate 	k_sigset_t	t_extsig;	/* signals sent from another contract */
1657c478bd9Sstevel@tonic-gate 	k_sigset_t	t_hold;		/* hold signal bit mask */
1667c478bd9Sstevel@tonic-gate 	struct	_kthread *t_forw;	/* process's forward thread link */
1677c478bd9Sstevel@tonic-gate 	struct	_kthread *t_back;	/* process's backward thread link */
1687c478bd9Sstevel@tonic-gate 	struct	_kthread *t_thlink;	/* tid (lwpid) lookup hash link */
1697c478bd9Sstevel@tonic-gate 	klwp_t	*t_lwp;			/* thread's lwp pointer */
1707c478bd9Sstevel@tonic-gate 	struct	proc	*t_procp;	/* proc pointer */
1717c478bd9Sstevel@tonic-gate 	struct	t_audit_data *t_audit_data;	/* per thread audit data */
1727c478bd9Sstevel@tonic-gate 	struct	_kthread *t_next;	/* doubly linked list of all threads */
1737c478bd9Sstevel@tonic-gate 	struct	_kthread *t_prev;
1747c478bd9Sstevel@tonic-gate 	ushort_t t_whystop;		/* reason for stopping */
1757c478bd9Sstevel@tonic-gate 	ushort_t t_whatstop;		/* more detailed reason */
1767c478bd9Sstevel@tonic-gate 	int	t_dslot;		/* index in proc's thread directory */
1777c478bd9Sstevel@tonic-gate 	struct	pollstate *t_pollstate;	/* state used during poll(2) */
1787c478bd9Sstevel@tonic-gate 	struct	pollcache *t_pollcache;	/* to pass a pcache ptr by /dev/poll */
1797c478bd9Sstevel@tonic-gate 	struct	cred	*t_cred;	/* pointer to current cred */
1807c478bd9Sstevel@tonic-gate 	time_t	t_start;		/* start time, seconds since epoch */
1817c478bd9Sstevel@tonic-gate 	clock_t	t_lbolt;		/* lbolt at last clock_tick() */
1827c478bd9Sstevel@tonic-gate 	hrtime_t t_stoptime;		/* timestamp at stop() */
1837c478bd9Sstevel@tonic-gate 	uint_t	t_pctcpu;		/* %cpu at last clock_tick(), binary */
1847c478bd9Sstevel@tonic-gate 					/* point at right of high-order bit */
1857c478bd9Sstevel@tonic-gate 	short	t_sysnum;		/* system call number */
1867c478bd9Sstevel@tonic-gate 	kcondvar_t	t_delay_cv;
1877c478bd9Sstevel@tonic-gate 	kmutex_t	t_delay_lock;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	/*
1907c478bd9Sstevel@tonic-gate 	 * Pointer to the dispatcher lock protecting t_state and state-related
1917c478bd9Sstevel@tonic-gate 	 * flags.  This pointer can change during waits on the lock, so
1927c478bd9Sstevel@tonic-gate 	 * it should be grabbed only by thread_lock().
1937c478bd9Sstevel@tonic-gate 	 */
1947c478bd9Sstevel@tonic-gate 	disp_lock_t	*t_lockp;	/* pointer to the dispatcher lock */
1957c478bd9Sstevel@tonic-gate 	ushort_t 	t_oldspl;	/* spl level before dispatcher locked */
1967c478bd9Sstevel@tonic-gate 	volatile char	t_pre_sys;	/* pre-syscall work needed */
1977c478bd9Sstevel@tonic-gate 	lock_t		t_lock_flush;	/* for lock_mutex_flush() impl */
1987c478bd9Sstevel@tonic-gate 	struct _disp	*t_disp_queue;	/* run queue for chosen CPU */
1997c478bd9Sstevel@tonic-gate 	clock_t		t_disp_time;	/* last time this thread was running */
2007c478bd9Sstevel@tonic-gate 	uint_t		t_kpri_req;	/* kernel priority required */
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	/*
2037c478bd9Sstevel@tonic-gate 	 * Post-syscall / post-trap flags.
2047c478bd9Sstevel@tonic-gate 	 * 	No lock is required to set these.
2057c478bd9Sstevel@tonic-gate 	 *	These must be cleared only by the thread itself.
2067c478bd9Sstevel@tonic-gate 	 *
2077c478bd9Sstevel@tonic-gate 	 *	t_astflag indicates that some post-trap processing is required,
2087c478bd9Sstevel@tonic-gate 	 *		possibly a signal or a preemption.  The thread will not
2097c478bd9Sstevel@tonic-gate 	 *		return to user with this set.
2107c478bd9Sstevel@tonic-gate 	 *	t_post_sys indicates that some unusualy post-system call
2117c478bd9Sstevel@tonic-gate 	 *		handling is required, such as an error or tracing.
2127c478bd9Sstevel@tonic-gate 	 *	t_sig_check indicates that some condition in ISSIG() must be
2137c478bd9Sstevel@tonic-gate 	 * 		checked, but doesn't prevent returning to user.
2147c478bd9Sstevel@tonic-gate 	 *	t_post_sys_ast is a way of checking whether any of these three
2157c478bd9Sstevel@tonic-gate 	 *		flags are set.
2167c478bd9Sstevel@tonic-gate 	 */
2177c478bd9Sstevel@tonic-gate 	union __tu {
2187c478bd9Sstevel@tonic-gate 		struct __ts {
2197c478bd9Sstevel@tonic-gate 			volatile char	_t_astflag;	/* AST requested */
2207c478bd9Sstevel@tonic-gate 			volatile char	_t_sig_check;	/* ISSIG required */
2217c478bd9Sstevel@tonic-gate 			volatile char	_t_post_sys;	/* post_syscall req */
2227c478bd9Sstevel@tonic-gate 			volatile char	_t_trapret;	/* call CL_TRAPRET */
2237c478bd9Sstevel@tonic-gate 		} _ts;
2247c478bd9Sstevel@tonic-gate 		volatile int	_t_post_sys_ast;	/* OR of these flags */
2257c478bd9Sstevel@tonic-gate 	} _tu;
2267c478bd9Sstevel@tonic-gate #define	t_astflag	_tu._ts._t_astflag
2277c478bd9Sstevel@tonic-gate #define	t_sig_check	_tu._ts._t_sig_check
2287c478bd9Sstevel@tonic-gate #define	t_post_sys	_tu._ts._t_post_sys
2297c478bd9Sstevel@tonic-gate #define	t_trapret	_tu._ts._t_trapret
2307c478bd9Sstevel@tonic-gate #define	t_post_sys_ast	_tu._t_post_sys_ast
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	/*
2337c478bd9Sstevel@tonic-gate 	 * Real time microstate profiling.
2347c478bd9Sstevel@tonic-gate 	 */
2357c478bd9Sstevel@tonic-gate 					/* possible 4-byte filler */
2367c478bd9Sstevel@tonic-gate 	hrtime_t t_waitrq;		/* timestamp for run queue wait time */
2377c478bd9Sstevel@tonic-gate 	int	t_mstate;		/* current microstate */
2387c478bd9Sstevel@tonic-gate 	struct rprof {
2397c478bd9Sstevel@tonic-gate 		int	rp_anystate;		/* set if any state non-zero */
2407c478bd9Sstevel@tonic-gate 		uint_t	rp_state[NMSTATES];	/* mstate profiling counts */
2417c478bd9Sstevel@tonic-gate 	} *t_rprof;
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	/*
2447c478bd9Sstevel@tonic-gate 	 * There is a turnstile inserted into the list below for
2457c478bd9Sstevel@tonic-gate 	 * every priority inverted synchronization object that
2467c478bd9Sstevel@tonic-gate 	 * this thread holds.
2477c478bd9Sstevel@tonic-gate 	 */
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	struct turnstile *t_prioinv;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	/*
2527c478bd9Sstevel@tonic-gate 	 * Pointer to the turnstile attached to the synchronization
2537c478bd9Sstevel@tonic-gate 	 * object where this thread is blocked.
2547c478bd9Sstevel@tonic-gate 	 */
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	struct turnstile *t_ts;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	/*
2597c478bd9Sstevel@tonic-gate 	 * kernel thread specific data
2607c478bd9Sstevel@tonic-gate 	 *	Borrowed from userland implementation of POSIX tsd
2617c478bd9Sstevel@tonic-gate 	 */
2627c478bd9Sstevel@tonic-gate 	struct tsd_thread {
2637c478bd9Sstevel@tonic-gate 		struct tsd_thread *ts_next;	/* threads with TSD */
2647c478bd9Sstevel@tonic-gate 		struct tsd_thread *ts_prev;	/* threads with TSD */
2657c478bd9Sstevel@tonic-gate 		uint_t		  ts_nkeys;	/* entries in value array */
2667c478bd9Sstevel@tonic-gate 		void		  **ts_value;	/* array of value/key */
2677c478bd9Sstevel@tonic-gate 	} *t_tsd;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	clock_t		t_stime;	/* time stamp used by the swapper */
2707c478bd9Sstevel@tonic-gate 	struct door_data *t_door;	/* door invocation data */
2717c478bd9Sstevel@tonic-gate 	kmutex_t	*t_plockp;	/* pointer to process's p_lock */
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	struct sc_shared *t_schedctl;	/* scheduler activations shared data */
2747c478bd9Sstevel@tonic-gate 	uintptr_t	t_sc_uaddr;	/* user-level address of shared data */
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	struct cpupart	*t_cpupart;	/* partition containing thread */
2777c478bd9Sstevel@tonic-gate 	int		t_bind_pset;	/* processor set binding */
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	struct copyops	*t_copyops;	/* copy in/out ops vector */
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	caddr_t		t_stkbase;	/* base of the the stack */
2827c478bd9Sstevel@tonic-gate 	struct page	*t_red_pp;	/* if non-NULL, redzone is mapped */
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	struct _afd	t_activefd;	/* active file descriptor table */
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	struct _kthread	*t_priforw;	/* sleepq per-priority sublist */
2877c478bd9Sstevel@tonic-gate 	struct _kthread	*t_priback;
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	struct sleepq	*t_sleepq;	/* sleep queue thread is waiting on */
290843e1988Sjohnlev 	struct panic_trap_info *t_panic_trap;	/* saved data from fatal trap */
2917c478bd9Sstevel@tonic-gate 	int		*t_lgrp_affinity;	/* lgroup affinity */
2927c478bd9Sstevel@tonic-gate 	struct upimutex	*t_upimutex;	/* list of upimutexes owned by thread */
2937c478bd9Sstevel@tonic-gate 	uint32_t	t_nupinest;	/* number of nested held upi mutexes */
2947c478bd9Sstevel@tonic-gate 	struct kproject *t_proj;	/* project containing this thread */
2957c478bd9Sstevel@tonic-gate 	uint8_t		t_unpark;	/* modified holding t_delay_lock */
2967c478bd9Sstevel@tonic-gate 	uint8_t		t_release;	/* lwp_release() waked up the thread */
2977c478bd9Sstevel@tonic-gate 	uint8_t		t_hatdepth;	/* depth of recursive hat_memloads */
298551bc2a6Smrj 	uint8_t		t_xpvcntr;	/* see xen_block_migrate() */
2997c478bd9Sstevel@tonic-gate 	kcondvar_t	t_joincv;	/* cv used to wait for thread exit */
3007c478bd9Sstevel@tonic-gate 	void		*t_taskq;	/* for threads belonging to taskq */
3017c478bd9Sstevel@tonic-gate 	hrtime_t	t_anttime;	/* most recent time anticipatory load */
3027c478bd9Sstevel@tonic-gate 					/*	was added to an lgroup's load */
3037c478bd9Sstevel@tonic-gate 					/*	on this thread's behalf */
3047c478bd9Sstevel@tonic-gate 	char		*t_pdmsg;	/* privilege debugging message */
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	uint_t		t_predcache;	/* DTrace predicate cache */
3077c478bd9Sstevel@tonic-gate 	hrtime_t	t_dtrace_vtime;	/* DTrace virtual time */
3087c478bd9Sstevel@tonic-gate 	hrtime_t	t_dtrace_start;	/* DTrace slice start time */
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	uint8_t		t_dtrace_stop;	/* indicates a DTrace-desired stop */
3117c478bd9Sstevel@tonic-gate 	uint8_t		t_dtrace_sig;	/* signal sent via DTrace's raise() */
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	union __tdu {
3147c478bd9Sstevel@tonic-gate 		struct __tds {
3157c478bd9Sstevel@tonic-gate 			uint8_t	_t_dtrace_on;	/* hit a fasttrap tracepoint */
3167c478bd9Sstevel@tonic-gate 			uint8_t	_t_dtrace_step;	/* about to return to kernel */
3177c478bd9Sstevel@tonic-gate 			uint8_t	_t_dtrace_ret;	/* handling a return probe */
3187c478bd9Sstevel@tonic-gate 			uint8_t	_t_dtrace_ast;	/* saved ast flag */
3197c478bd9Sstevel@tonic-gate #ifdef __amd64
3207c478bd9Sstevel@tonic-gate 			uint8_t	_t_dtrace_reg;	/* modified register */
3217c478bd9Sstevel@tonic-gate #endif
3227c478bd9Sstevel@tonic-gate 		} _tds;
3237c478bd9Sstevel@tonic-gate 		ulong_t	_t_dtrace_ft;		/* bitwise or of these flags */
3247c478bd9Sstevel@tonic-gate 	} _tdu;
3257c478bd9Sstevel@tonic-gate #define	t_dtrace_ft	_tdu._t_dtrace_ft
3267c478bd9Sstevel@tonic-gate #define	t_dtrace_on	_tdu._tds._t_dtrace_on
3277c478bd9Sstevel@tonic-gate #define	t_dtrace_step	_tdu._tds._t_dtrace_step
3287c478bd9Sstevel@tonic-gate #define	t_dtrace_ret	_tdu._tds._t_dtrace_ret
3297c478bd9Sstevel@tonic-gate #define	t_dtrace_ast	_tdu._tds._t_dtrace_ast
3307c478bd9Sstevel@tonic-gate #ifdef __amd64
3317c478bd9Sstevel@tonic-gate #define	t_dtrace_reg	_tdu._tds._t_dtrace_reg
3327c478bd9Sstevel@tonic-gate #endif
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	uintptr_t	t_dtrace_pc;	/* DTrace saved pc from fasttrap */
3357c478bd9Sstevel@tonic-gate 	uintptr_t	t_dtrace_npc;	/* DTrace next pc from fasttrap */
3367c478bd9Sstevel@tonic-gate 	uintptr_t	t_dtrace_scrpc;	/* DTrace per-thread scratch location */
3377c478bd9Sstevel@tonic-gate 	uintptr_t	t_dtrace_astpc;	/* DTrace return sequence location */
3387c478bd9Sstevel@tonic-gate #ifdef __amd64
3397c478bd9Sstevel@tonic-gate 	uint64_t	t_dtrace_regv;	/* DTrace saved reg from fasttrap */
3407c478bd9Sstevel@tonic-gate #endif
3417c478bd9Sstevel@tonic-gate 	hrtime_t	t_hrtime;	/* high-res last time on cpu */
3425a64ecfaStrevtom 	kmutex_t	t_ctx_lock;	/* protects t_ctx in removectx() */
343c97ad5cdSakolb 	struct waitq	*t_waitq;	/* wait queue */
344*454ab202SMadhavan Venkataraman 	kmutex_t	t_wait_mutex;	/* used in CV wait functions */
3457c478bd9Sstevel@tonic-gate } kthread_t;
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate /*
3487c478bd9Sstevel@tonic-gate  * Thread flag (t_flag) definitions.
3497c478bd9Sstevel@tonic-gate  *	These flags must be changed only for the current thread,
3507c478bd9Sstevel@tonic-gate  * 	and not during preemption code, since the code being
3517c478bd9Sstevel@tonic-gate  *	preempted could be modifying the flags.
3527c478bd9Sstevel@tonic-gate  *
3537c478bd9Sstevel@tonic-gate  *	For the most part these flags do not need locking.
3547c478bd9Sstevel@tonic-gate  *	The following flags will only be changed while the thread_lock is held,
3557c478bd9Sstevel@tonic-gate  *	to give assurrance that they are consistent with t_state:
3567c478bd9Sstevel@tonic-gate  *		T_WAKEABLE
3577c478bd9Sstevel@tonic-gate  */
3587c478bd9Sstevel@tonic-gate #define	T_INTR_THREAD	0x0001	/* thread is an interrupt thread */
3597c478bd9Sstevel@tonic-gate #define	T_WAKEABLE	0x0002	/* thread is blocked, signals enabled */
3607c478bd9Sstevel@tonic-gate #define	T_TOMASK	0x0004	/* use lwp_sigoldmask on return from signal */
3617c478bd9Sstevel@tonic-gate #define	T_TALLOCSTK	0x0008  /* thread structure allocated from stk */
362c4978b50Sraf #define	T_FORKALL	0x0010	/* thread was cloned by forkall() */
3637c478bd9Sstevel@tonic-gate #define	T_WOULDBLOCK	0x0020	/* for lockfs */
3647c478bd9Sstevel@tonic-gate #define	T_DONTBLOCK	0x0040	/* for lockfs */
3657c478bd9Sstevel@tonic-gate #define	T_DONTPEND	0x0080	/* for lockfs */
3667c478bd9Sstevel@tonic-gate #define	T_SYS_PROF	0x0100	/* profiling on for duration of system call */
3677c478bd9Sstevel@tonic-gate #define	T_WAITCVSEM	0x0200	/* waiting for a lwp_cv or lwp_sema on sleepq */
3687c478bd9Sstevel@tonic-gate #define	T_WATCHPT	0x0400	/* thread undergoing a watchpoint emulation */
3697c478bd9Sstevel@tonic-gate #define	T_PANIC		0x0800	/* thread initiated a system panic */
3707c478bd9Sstevel@tonic-gate #define	T_DFLTSTK	0x1000	/* stack is default size */
3718b464eb8Smec #define	T_CAPTURING	0x2000	/* thread is in page capture logic */
3728d186f16Sraf #define	T_VFPARENT	0x4000	/* thread is vfork parent, must call vfwait */
3735cc25c11Smb #define	T_DONTDTRACE	0x8000  /* disable DTrace probes */
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate /*
3767c478bd9Sstevel@tonic-gate  * Flags in t_proc_flag.
3777c478bd9Sstevel@tonic-gate  *	These flags must be modified only when holding the p_lock
3787c478bd9Sstevel@tonic-gate  *	for the associated process.
3797c478bd9Sstevel@tonic-gate  */
3807c478bd9Sstevel@tonic-gate #define	TP_DAEMON	0x0001	/* this is an LWP_DAEMON lwp */
3817c478bd9Sstevel@tonic-gate #define	TP_HOLDLWP	0x0002	/* hold thread's lwp */
3827c478bd9Sstevel@tonic-gate #define	TP_TWAIT	0x0004	/* wait to be freed by lwp_wait() */
3837c478bd9Sstevel@tonic-gate #define	TP_LWPEXIT	0x0008	/* lwp has exited */
3847c478bd9Sstevel@tonic-gate #define	TP_PRSTOP	0x0010	/* thread is being stopped via /proc */
3857c478bd9Sstevel@tonic-gate #define	TP_CHKPT	0x0020	/* thread is being stopped via CPR checkpoint */
3867c478bd9Sstevel@tonic-gate #define	TP_EXITLWP	0x0040	/* terminate this lwp */
3877c478bd9Sstevel@tonic-gate #define	TP_PRVSTOP	0x0080	/* thread is virtually stopped via /proc */
3887c478bd9Sstevel@tonic-gate #define	TP_MSACCT	0x0100	/* collect micro-state accounting information */
3897c478bd9Sstevel@tonic-gate #define	TP_STOPPING	0x0200	/* thread is executing stop() */
3907c478bd9Sstevel@tonic-gate #define	TP_WATCHPT	0x0400	/* process has watchpoints in effect */
3917c478bd9Sstevel@tonic-gate #define	TP_PAUSE	0x0800	/* process is being stopped via pauselwps() */
3927c478bd9Sstevel@tonic-gate #define	TP_CHANGEBIND	0x1000	/* thread has a new cpu/cpupart binding */
3937c478bd9Sstevel@tonic-gate #define	TP_ZTHREAD	0x2000	/* this is a kernel thread for a zone */
3947c478bd9Sstevel@tonic-gate #define	TP_WATCHSTOP	0x4000	/* thread is stopping via holdwatch() */
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate /*
3977c478bd9Sstevel@tonic-gate  * Thread scheduler flag (t_schedflag) definitions.
3987c478bd9Sstevel@tonic-gate  *	The thread must be locked via thread_lock() or equiv. to change these.
3997c478bd9Sstevel@tonic-gate  */
4007c478bd9Sstevel@tonic-gate #define	TS_LOAD		0x0001	/* thread is in memory */
4017c478bd9Sstevel@tonic-gate #define	TS_DONT_SWAP	0x0002	/* thread/lwp should not be swapped */
4027c478bd9Sstevel@tonic-gate #define	TS_SWAPENQ	0x0004	/* swap thread when it reaches a safe point */
4037c478bd9Sstevel@tonic-gate #define	TS_ON_SWAPQ	0x0008	/* thread is on the swap queue */
4047c478bd9Sstevel@tonic-gate #define	TS_SIGNALLED	0x0010	/* thread was awakened by cv_signal() */
405c97ad5cdSakolb #define	TS_PROJWAITQ	0x0020	/* thread is on its project's waitq */
406c97ad5cdSakolb #define	TS_ZONEWAITQ	0x0040	/* thread is on its zone's waitq */
4077c478bd9Sstevel@tonic-gate #define	TS_CSTART	0x0100	/* setrun() by continuelwps() */
4087c478bd9Sstevel@tonic-gate #define	TS_UNPAUSE	0x0200	/* setrun() by unpauselwps() */
4097c478bd9Sstevel@tonic-gate #define	TS_XSTART	0x0400	/* setrun() by SIGCONT */
4107c478bd9Sstevel@tonic-gate #define	TS_PSTART	0x0800	/* setrun() by /proc */
4117c478bd9Sstevel@tonic-gate #define	TS_RESUME	0x1000	/* setrun() by CPR resume process */
4127c478bd9Sstevel@tonic-gate #define	TS_CREATE	0x2000	/* setrun() by syslwp_create() */
4137c478bd9Sstevel@tonic-gate #define	TS_RUNQMATCH	0x4000	/* exact run queue balancing by setbackdq() */
4147c478bd9Sstevel@tonic-gate #define	TS_ALLSTART	\
4157c478bd9Sstevel@tonic-gate 	(TS_CSTART|TS_UNPAUSE|TS_XSTART|TS_PSTART|TS_RESUME|TS_CREATE)
416c97ad5cdSakolb #define	TS_ANYWAITQ	(TS_PROJWAITQ|TS_ZONEWAITQ)
4177c478bd9Sstevel@tonic-gate 
4180b70c467Sakolb /*
4190b70c467Sakolb  * Thread binding types
4200b70c467Sakolb  */
4210b70c467Sakolb #define	TB_ALLHARD	0
4220b70c467Sakolb #define	TB_CPU_SOFT	0x01		/* soft binding to CPU */
4230b70c467Sakolb #define	TB_PSET_SOFT	0x02		/* soft binding to pset */
4240b70c467Sakolb 
4250b70c467Sakolb #define	TB_CPU_SOFT_SET(t)		((t)->t_bindflag |= TB_CPU_SOFT)
4260b70c467Sakolb #define	TB_CPU_HARD_SET(t)		((t)->t_bindflag &= ~TB_CPU_SOFT)
4270b70c467Sakolb #define	TB_PSET_SOFT_SET(t)		((t)->t_bindflag |= TB_PSET_SOFT)
4280b70c467Sakolb #define	TB_PSET_HARD_SET(t)		((t)->t_bindflag &= ~TB_PSET_SOFT)
4290b70c467Sakolb #define	TB_CPU_IS_SOFT(t)		((t)->t_bindflag & TB_CPU_SOFT)
4300b70c467Sakolb #define	TB_CPU_IS_HARD(t)		(!TB_CPU_IS_SOFT(t))
4310b70c467Sakolb #define	TB_PSET_IS_SOFT(t)		((t)->t_bindflag & TB_PSET_SOFT)
4320b70c467Sakolb 
4337c478bd9Sstevel@tonic-gate /*
4347c478bd9Sstevel@tonic-gate  * No locking needed for AST field.
4357c478bd9Sstevel@tonic-gate  */
4367c478bd9Sstevel@tonic-gate #define	aston(t)		((t)->t_astflag = 1)
4377c478bd9Sstevel@tonic-gate #define	astoff(t)		((t)->t_astflag = 0)
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate /* True if thread is stopped on an event of interest */
4407c478bd9Sstevel@tonic-gate #define	ISTOPPED(t) ((t)->t_state == TS_STOPPED && \
4417c478bd9Sstevel@tonic-gate 			!((t)->t_schedflag & TS_PSTART))
4427c478bd9Sstevel@tonic-gate 
443c97ad5cdSakolb /* True if thread is asleep and wakeable */
444c97ad5cdSakolb #define	ISWAKEABLE(t) (((t)->t_state == TS_SLEEP && \
445c97ad5cdSakolb 			((t)->t_flag & T_WAKEABLE)))
446c97ad5cdSakolb 
447c97ad5cdSakolb /* True if thread is on the wait queue */
448c97ad5cdSakolb #define	ISWAITING(t) ((t)->t_state == TS_WAIT)
449c97ad5cdSakolb 
4507c478bd9Sstevel@tonic-gate /* similar to ISTOPPED except the event of interest is CPR */
4517c478bd9Sstevel@tonic-gate #define	CPR_ISTOPPED(t) ((t)->t_state == TS_STOPPED && \
4527c478bd9Sstevel@tonic-gate 			!((t)->t_schedflag & TS_RESUME))
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate /*
4557c478bd9Sstevel@tonic-gate  * True if thread is virtually stopped (is or was asleep in
4567c478bd9Sstevel@tonic-gate  * one of the lwp_*() system calls and marked to stop by /proc.)
4577c478bd9Sstevel@tonic-gate  */
4587c478bd9Sstevel@tonic-gate #define	VSTOPPED(t)	((t)->t_proc_flag & TP_PRVSTOP)
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate /* similar to VSTOPPED except the point of interest is CPR */
4617c478bd9Sstevel@tonic-gate #define	CPR_VSTOPPED(t)				\
4627c478bd9Sstevel@tonic-gate 	((t)->t_state == TS_SLEEP &&		\
4637c478bd9Sstevel@tonic-gate 	(t)->t_wchan0 != NULL &&		\
4647c478bd9Sstevel@tonic-gate 	((t)->t_flag & T_WAKEABLE) &&		\
4657c478bd9Sstevel@tonic-gate 	((t)->t_proc_flag & TP_CHKPT))
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate /* True if thread has been stopped by hold*() or was created stopped */
4687c478bd9Sstevel@tonic-gate #define	SUSPENDED(t) ((t)->t_state == TS_STOPPED && \
4697c478bd9Sstevel@tonic-gate 	((t)->t_schedflag & (TS_CSTART|TS_UNPAUSE)) != (TS_CSTART|TS_UNPAUSE))
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate /* True if thread possesses an inherited priority */
4727c478bd9Sstevel@tonic-gate #define	INHERITED(t)	((t)->t_epri != 0)
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate /* The dispatch priority of a thread */
4757c478bd9Sstevel@tonic-gate #define	DISP_PRIO(t) ((t)->t_epri > (t)->t_pri ? (t)->t_epri : (t)->t_pri)
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate /* The assigned priority of a thread */
4787c478bd9Sstevel@tonic-gate #define	ASSIGNED_PRIO(t)	((t)->t_pri)
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate /*
4817c478bd9Sstevel@tonic-gate  * Macros to determine whether a thread can be swapped.
4827c478bd9Sstevel@tonic-gate  * If t_lock is held, the thread is either on a processor or being swapped.
4837c478bd9Sstevel@tonic-gate  */
4847c478bd9Sstevel@tonic-gate #define	SWAP_OK(t)	(!LOCK_HELD(&(t)->t_lock))
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate /*
4877c478bd9Sstevel@tonic-gate  * proctot(x)
4887c478bd9Sstevel@tonic-gate  *	convert a proc pointer to a thread pointer. this only works with
4897c478bd9Sstevel@tonic-gate  *	procs that have only one lwp.
4907c478bd9Sstevel@tonic-gate  *
4917c478bd9Sstevel@tonic-gate  * proctolwp(x)
4927c478bd9Sstevel@tonic-gate  *	convert a proc pointer to a lwp pointer. this only works with
4937c478bd9Sstevel@tonic-gate  *	procs that have only one lwp.
4947c478bd9Sstevel@tonic-gate  *
4957c478bd9Sstevel@tonic-gate  * ttolwp(x)
4967c478bd9Sstevel@tonic-gate  *	convert a thread pointer to its lwp pointer.
4977c478bd9Sstevel@tonic-gate  *
4987c478bd9Sstevel@tonic-gate  * ttoproc(x)
4997c478bd9Sstevel@tonic-gate  *	convert a thread pointer to its proc pointer.
5007c478bd9Sstevel@tonic-gate  *
5017c478bd9Sstevel@tonic-gate  * ttoproj(x)
5027c478bd9Sstevel@tonic-gate  * 	convert a thread pointer to its project pointer.
5037c478bd9Sstevel@tonic-gate  *
504c97ad5cdSakolb  * ttozone(x)
505c97ad5cdSakolb  * 	convert a thread pointer to its zone pointer.
506c97ad5cdSakolb  *
5077c478bd9Sstevel@tonic-gate  * lwptot(x)
5087c478bd9Sstevel@tonic-gate  *	convert a lwp pointer to its thread pointer.
5097c478bd9Sstevel@tonic-gate  *
5107c478bd9Sstevel@tonic-gate  * lwptoproc(x)
5117c478bd9Sstevel@tonic-gate  *	convert a lwp to its proc pointer.
5127c478bd9Sstevel@tonic-gate  */
5137c478bd9Sstevel@tonic-gate #define	proctot(x)	((x)->p_tlist)
5147c478bd9Sstevel@tonic-gate #define	proctolwp(x)	((x)->p_tlist->t_lwp)
5157c478bd9Sstevel@tonic-gate #define	ttolwp(x)	((x)->t_lwp)
5167c478bd9Sstevel@tonic-gate #define	ttoproc(x)	((x)->t_procp)
5177c478bd9Sstevel@tonic-gate #define	ttoproj(x)	((x)->t_proj)
518c97ad5cdSakolb #define	ttozone(x)	((x)->t_procp->p_zone)
5197c478bd9Sstevel@tonic-gate #define	lwptot(x)	((x)->lwp_thread)
5207c478bd9Sstevel@tonic-gate #define	lwptoproc(x)	((x)->lwp_procp)
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate #define	t_pc		t_pcb.val[0]
5237c478bd9Sstevel@tonic-gate #define	t_sp		t_pcb.val[1]
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate #ifdef	_KERNEL
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate extern	kthread_t	*threadp(void);	/* inline, returns thread pointer */
5287c478bd9Sstevel@tonic-gate #define	curthread	(threadp())		/* current thread pointer */
5297c478bd9Sstevel@tonic-gate #define	curproc		(ttoproc(curthread))	/* current process pointer */
5307c478bd9Sstevel@tonic-gate #define	curproj		(ttoproj(curthread))	/* current project pointer */
531c97ad5cdSakolb #define	curzone		(curproc->p_zone)	/* current zone pointer */
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate extern	struct _kthread	t0;		/* the scheduler thread */
5347c478bd9Sstevel@tonic-gate extern	kmutex_t	pidlock;	/* global process lock */
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate /*
5372850d85bSmv  * thread_free_lock is used by the tick accounting thread to keep a thread
5387c478bd9Sstevel@tonic-gate  * from being freed while it is being examined.
5397c478bd9Sstevel@tonic-gate  */
5402850d85bSmv #define	THREAD_FREE_NUM		1024
5412850d85bSmv #define	THREAD_FREE_MASK	(THREAD_FREE_NUM - 1)
5422850d85bSmv #define	THREAD_FREE_SHIFT_BITS	5
5432850d85bSmv #define	THREAD_FREE_SHIFT(t)	((uintptr_t)t >> THREAD_FREE_SHIFT_BITS)
5442850d85bSmv #define	THREAD_FREE_HASH(t)	(THREAD_FREE_SHIFT(t) & THREAD_FREE_MASK)
5452850d85bSmv 
5462850d85bSmv typedef struct thread_free_lock {
5472850d85bSmv 	kmutex_t	tf_lock;
5482850d85bSmv 	uchar_t		tf_pad[64 - sizeof (kmutex_t)];
5492850d85bSmv } thread_free_lock_t;
5502850d85bSmv 
5512850d85bSmv extern void	thread_free_prevent(kthread_t *);
5522850d85bSmv extern void	thread_free_allow(kthread_t *);
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate /*
5557c478bd9Sstevel@tonic-gate  * Routines to change the priority and effective priority
5567c478bd9Sstevel@tonic-gate  * of a thread-locked thread, whatever its state.
5577c478bd9Sstevel@tonic-gate  */
5587c478bd9Sstevel@tonic-gate extern int	thread_change_pri(kthread_t *t, pri_t disp_pri, int front);
5597c478bd9Sstevel@tonic-gate extern void	thread_change_epri(kthread_t *t, pri_t disp_pri);
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate /*
5627c478bd9Sstevel@tonic-gate  * Routines that manipulate the dispatcher lock for the thread.
5637c478bd9Sstevel@tonic-gate  * The locking heirarchy is as follows:
5647c478bd9Sstevel@tonic-gate  *	cpu_lock > sleepq locks > run queue locks
5657c478bd9Sstevel@tonic-gate  */
5667c478bd9Sstevel@tonic-gate void	thread_transition(kthread_t *); /* move to transition lock */
5677c478bd9Sstevel@tonic-gate void	thread_stop(kthread_t *);	/* move to stop lock */
5687c478bd9Sstevel@tonic-gate void	thread_lock(kthread_t *);	/* lock thread and its queue */
5697c478bd9Sstevel@tonic-gate void	thread_lock_high(kthread_t *);	/* lock thread and its queue */
5707c478bd9Sstevel@tonic-gate void	thread_onproc(kthread_t *, struct cpu *); /* set onproc state lock */
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate #define	thread_unlock(t)		disp_lock_exit((t)->t_lockp)
5737c478bd9Sstevel@tonic-gate #define	thread_unlock_high(t)		disp_lock_exit_high((t)->t_lockp)
5747c478bd9Sstevel@tonic-gate #define	thread_unlock_nopreempt(t)	disp_lock_exit_nopreempt((t)->t_lockp)
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate #define	THREAD_LOCK_HELD(t)	(DISP_LOCK_HELD((t)->t_lockp))
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate extern disp_lock_t transition_lock;	/* lock protecting transiting threads */
5797c478bd9Sstevel@tonic-gate extern disp_lock_t stop_lock;		/* lock protecting stopped threads */
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate caddr_t	thread_stk_init(caddr_t);	/* init thread stack */
5827c478bd9Sstevel@tonic-gate 
5830b70c467Sakolb extern int default_binding_mode;
5840b70c467Sakolb 
5857c478bd9Sstevel@tonic-gate #endif	/* _KERNEL */
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate /*
5887c478bd9Sstevel@tonic-gate  * Macros to indicate that the thread holds resources that could be critical
5897c478bd9Sstevel@tonic-gate  * to other kernel threads, so this thread needs to have kernel priority
5907c478bd9Sstevel@tonic-gate  * if it blocks or is preempted.  Note that this is not necessary if the
5917c478bd9Sstevel@tonic-gate  * resource is a mutex or a writer lock because of priority inheritance.
5927c478bd9Sstevel@tonic-gate  *
5937c478bd9Sstevel@tonic-gate  * The only way one thread may legally manipulate another thread's t_kpri_req
5947c478bd9Sstevel@tonic-gate  * is to hold the target thread's thread lock while that thread is asleep.
5957c478bd9Sstevel@tonic-gate  * (The rwlock code does this to implement direct handoff to waiting readers.)
5967c478bd9Sstevel@tonic-gate  */
5977c478bd9Sstevel@tonic-gate #define	THREAD_KPRI_REQUEST()	(curthread->t_kpri_req++)
5987c478bd9Sstevel@tonic-gate #define	THREAD_KPRI_RELEASE()	(curthread->t_kpri_req--)
5997c478bd9Sstevel@tonic-gate #define	THREAD_KPRI_RELEASE_N(n) (curthread->t_kpri_req -= (n))
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate /*
6027c478bd9Sstevel@tonic-gate  * Macro to change a thread's priority.
6037c478bd9Sstevel@tonic-gate  */
6047c478bd9Sstevel@tonic-gate #define	THREAD_CHANGE_PRI(t, pri) {					\
6057c478bd9Sstevel@tonic-gate 	pri_t __new_pri = (pri);					\
6067c478bd9Sstevel@tonic-gate 	DTRACE_SCHED2(change__pri, kthread_t *, (t), pri_t, __new_pri);	\
6077c478bd9Sstevel@tonic-gate 	(t)->t_pri = __new_pri;						\
608d4204c85Sraf 	schedctl_set_cidpri(t);						\
6097c478bd9Sstevel@tonic-gate }
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate /*
6127c478bd9Sstevel@tonic-gate  * Macro to indicate that a thread's priority is about to be changed.
6137c478bd9Sstevel@tonic-gate  */
6147c478bd9Sstevel@tonic-gate #define	THREAD_WILLCHANGE_PRI(t, pri) {					\
6157c478bd9Sstevel@tonic-gate 	DTRACE_SCHED2(change__pri, kthread_t *, (t), pri_t, (pri));	\
6167c478bd9Sstevel@tonic-gate }
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate /*
6197c478bd9Sstevel@tonic-gate  * Macros to change thread state and the associated lock.
6207c478bd9Sstevel@tonic-gate  */
6217c478bd9Sstevel@tonic-gate #define	THREAD_SET_STATE(tp, state, lp) \
6227c478bd9Sstevel@tonic-gate 		((tp)->t_state = state, (tp)->t_lockp = lp)
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate /*
6257c478bd9Sstevel@tonic-gate  * Point it at the transition lock, which is always held.
6267c478bd9Sstevel@tonic-gate  * The previosly held lock is dropped.
6277c478bd9Sstevel@tonic-gate  */
6287c478bd9Sstevel@tonic-gate #define	THREAD_TRANSITION(tp) 	thread_transition(tp);
6297c478bd9Sstevel@tonic-gate /*
6307c478bd9Sstevel@tonic-gate  * Set the thread's lock to be the transition lock, without dropping
6317c478bd9Sstevel@tonic-gate  * previosly held lock.
6327c478bd9Sstevel@tonic-gate  */
6337c478bd9Sstevel@tonic-gate #define	THREAD_TRANSITION_NOLOCK(tp) 	((tp)->t_lockp = &transition_lock)
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate /*
6367c478bd9Sstevel@tonic-gate  * Put thread in run state, and set the lock pointer to the dispatcher queue
6377c478bd9Sstevel@tonic-gate  * lock pointer provided.  This lock should be held.
6387c478bd9Sstevel@tonic-gate  */
6397c478bd9Sstevel@tonic-gate #define	THREAD_RUN(tp, lp)	THREAD_SET_STATE(tp, TS_RUN, lp)
6407c478bd9Sstevel@tonic-gate 
641c97ad5cdSakolb /*
642c97ad5cdSakolb  * Put thread in wait state, and set the lock pointer to the wait queue
643c97ad5cdSakolb  * lock pointer provided.  This lock should be held.
644c97ad5cdSakolb  */
645c97ad5cdSakolb #define	THREAD_WAIT(tp, lp)	THREAD_SET_STATE(tp, TS_WAIT, lp)
646c97ad5cdSakolb 
6477c478bd9Sstevel@tonic-gate /*
6487c478bd9Sstevel@tonic-gate  * Put thread in run state, and set the lock pointer to the dispatcher queue
6497c478bd9Sstevel@tonic-gate  * lock pointer provided (i.e., the "swapped_lock").  This lock should be held.
6507c478bd9Sstevel@tonic-gate  */
6517c478bd9Sstevel@tonic-gate #define	THREAD_SWAP(tp, lp)	THREAD_SET_STATE(tp, TS_RUN, lp)
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate /*
6547c478bd9Sstevel@tonic-gate  * Put the thread in zombie state and set the lock pointer to NULL.
6557c478bd9Sstevel@tonic-gate  * The NULL will catch anything that tries to lock a zombie.
6567c478bd9Sstevel@tonic-gate  */
6577c478bd9Sstevel@tonic-gate #define	THREAD_ZOMB(tp)		THREAD_SET_STATE(tp, TS_ZOMB, NULL)
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate /*
6607c478bd9Sstevel@tonic-gate  * Set the thread into ONPROC state, and point the lock at the CPUs
6617c478bd9Sstevel@tonic-gate  * lock for the onproc thread(s).  This lock should be held, so the
6627c478bd9Sstevel@tonic-gate  * thread deoes not become unlocked, since these stores can be reordered.
6637c478bd9Sstevel@tonic-gate  */
6647c478bd9Sstevel@tonic-gate #define	THREAD_ONPROC(tp, cpu)	\
6657c478bd9Sstevel@tonic-gate 		THREAD_SET_STATE(tp, TS_ONPROC, &(cpu)->cpu_thread_lock)
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate /*
6687c478bd9Sstevel@tonic-gate  * Set the thread into the TS_SLEEP state, and set the lock pointer to
6697c478bd9Sstevel@tonic-gate  * to some sleep queue's lock.  The new lock should already be held.
6707c478bd9Sstevel@tonic-gate  */
6717c478bd9Sstevel@tonic-gate #define	THREAD_SLEEP(tp, lp)	{				\
6727c478bd9Sstevel@tonic-gate 			disp_lock_t	*tlp;			\
6737c478bd9Sstevel@tonic-gate 			tlp = (tp)->t_lockp;			\
6747c478bd9Sstevel@tonic-gate 			THREAD_SET_STATE(tp, TS_SLEEP, lp);	\
6757c478bd9Sstevel@tonic-gate 			disp_lock_exit_high(tlp);		\
6767c478bd9Sstevel@tonic-gate 			}
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate /*
6797c478bd9Sstevel@tonic-gate  * Interrupt threads are created in TS_FREE state, and their lock
6807c478bd9Sstevel@tonic-gate  * points at the associated CPU's lock.
6817c478bd9Sstevel@tonic-gate  */
6827c478bd9Sstevel@tonic-gate #define	THREAD_FREEINTR(tp, cpu)	\
6837c478bd9Sstevel@tonic-gate 		THREAD_SET_STATE(tp, TS_FREE, &(cpu)->cpu_thread_lock)
6847c478bd9Sstevel@tonic-gate 
685bff31d89SPhilippe Jung /* if tunable kmem_stackinfo is set, fill kthread stack with a pattern */
686bff31d89SPhilippe Jung #define	KMEM_STKINFO_PATTERN	0xbadcbadcbadcbadcULL
687bff31d89SPhilippe Jung 
688bff31d89SPhilippe Jung /*
689bff31d89SPhilippe Jung  * If tunable kmem_stackinfo is set, log the latest KMEM_LOG_STK_USAGE_SIZE
690bff31d89SPhilippe Jung  * dead kthreads that used their kernel stack the most.
691bff31d89SPhilippe Jung  */
692bff31d89SPhilippe Jung #define	KMEM_STKINFO_LOG_SIZE	16
693bff31d89SPhilippe Jung 
694bff31d89SPhilippe Jung /* kthread name (cmd/lwpid) string size in the stackinfo log */
695bff31d89SPhilippe Jung #define	KMEM_STKINFO_STR_SIZE	64
696bff31d89SPhilippe Jung 
697bff31d89SPhilippe Jung /*
698bff31d89SPhilippe Jung  * stackinfo logged data.
699bff31d89SPhilippe Jung  */
700bff31d89SPhilippe Jung typedef struct kmem_stkinfo {
701bff31d89SPhilippe Jung 	caddr_t	kthread;	/* kthread pointer */
702bff31d89SPhilippe Jung 	caddr_t	t_startpc;	/* where kthread started */
703bff31d89SPhilippe Jung 	caddr_t	start;		/* kthread stack start address */
704bff31d89SPhilippe Jung 	size_t	stksz;		/* kthread stack size */
705bff31d89SPhilippe Jung 	size_t	percent;	/* kthread stack high water mark */
706bff31d89SPhilippe Jung 	id_t	t_tid;		/* kthread id */
707bff31d89SPhilippe Jung 	char	cmd[KMEM_STKINFO_STR_SIZE];	/* kthread name (cmd/lwpid) */
708bff31d89SPhilippe Jung } kmem_stkinfo_t;
709bff31d89SPhilippe Jung 
7107c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
7117c478bd9Sstevel@tonic-gate }
7127c478bd9Sstevel@tonic-gate #endif
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate #endif /* _SYS_THREAD_H */
715