xref: /illumos-gate/usr/src/uts/common/sys/thread.h (revision 2570281c)
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 /*
2391f917c7SMadhavan Venkataraman  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27ab618543SJohn Levon /*
282fc9ab6eSJerry Jelinek  * Copyright 2020 Joyent, Inc.
29ab618543SJohn Levon  */
30ab618543SJohn Levon 
317c478bd9Sstevel@tonic-gate #ifndef	_SYS_THREAD_H
327c478bd9Sstevel@tonic-gate #define	_SYS_THREAD_H
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
377c478bd9Sstevel@tonic-gate #include <sys/klwp.h>
387c478bd9Sstevel@tonic-gate #include <sys/time.h>
397c478bd9Sstevel@tonic-gate #include <sys/signal.h>
407c478bd9Sstevel@tonic-gate #include <sys/kcpc.h>
417c478bd9Sstevel@tonic-gate #if defined(__GNUC__) && defined(_ASM_INLINES) && defined(_KERNEL)
427c478bd9Sstevel@tonic-gate #include <asm/thread.h>
437c478bd9Sstevel@tonic-gate #endif
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
467c478bd9Sstevel@tonic-gate extern "C" {
477c478bd9Sstevel@tonic-gate #endif
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate  * The thread object, its states, and the methods by which it
517c478bd9Sstevel@tonic-gate  * is accessed.
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /*
557c478bd9Sstevel@tonic-gate  * Values that t_state may assume. Note that t_state cannot have more
567c478bd9Sstevel@tonic-gate  * than one of these flags set at a time.
577c478bd9Sstevel@tonic-gate  */
587c478bd9Sstevel@tonic-gate #define	TS_FREE		0x00	/* Thread at loose ends */
597c478bd9Sstevel@tonic-gate #define	TS_SLEEP	0x01	/* Awaiting an event */
607c478bd9Sstevel@tonic-gate #define	TS_RUN		0x02	/* Runnable, but not yet on a processor */
617c478bd9Sstevel@tonic-gate #define	TS_ONPROC	0x04	/* Thread is being run on a processor */
627c478bd9Sstevel@tonic-gate #define	TS_ZOMB		0x08	/* Thread has died but hasn't been reaped */
637c478bd9Sstevel@tonic-gate #define	TS_STOPPED	0x10	/* Stopped, initial state */
64c97ad5cdSakolb #define	TS_WAIT		0x20	/* Waiting to become runnable */
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate typedef struct ctxop {
677c478bd9Sstevel@tonic-gate 	void	(*save_op)(void *);	/* function to invoke to save context */
687c478bd9Sstevel@tonic-gate 	void	(*restore_op)(void *);	/* function to invoke to restore ctx */
697c478bd9Sstevel@tonic-gate 	void	(*fork_op)(void *, void *);	/* invoke to fork context */
707c478bd9Sstevel@tonic-gate 	void	(*lwp_create_op)(void *, void *);	/* lwp_create context */
717c478bd9Sstevel@tonic-gate 	void	(*exit_op)(void *);	/* invoked during {thread,lwp}_exit() */
727c478bd9Sstevel@tonic-gate 	void	(*free_op)(void *, int); /* function which frees the context */
737c478bd9Sstevel@tonic-gate 	void	*arg;		/* argument to above functions, ctx pointer */
746e2e6725SPatrick Mooney 	struct ctxop *next;		/* next context ops */
756e2e6725SPatrick Mooney 	struct ctxop *prev;		/* previous context ops */
766e2e6725SPatrick Mooney 	hrtime_t save_ts;		/* timestamp of last save */
776e2e6725SPatrick Mooney 	hrtime_t restore_ts;		/* timestamp of last restore */
787c478bd9Sstevel@tonic-gate } ctxop_t;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate  * The active file descriptor table.
827c478bd9Sstevel@tonic-gate  * Each member of a_fd[] not equalling -1 represents an active fd.
837c478bd9Sstevel@tonic-gate  * The structure is initialized on first use; all zeros means uninitialized.
847c478bd9Sstevel@tonic-gate  */
85236317e1SRoger A. Faulkner typedef struct {
86236317e1SRoger A. Faulkner 	kmutex_t a_fdlock;	/* protects a_fd and a_nfd */
877c478bd9Sstevel@tonic-gate 	int	*a_fd;		/* pointer to list of fds */
88236317e1SRoger A. Faulkner 	int	a_nfd;		/* number of entries in *a_fd */
89236317e1SRoger A. Faulkner 	int	a_stale;	/* one of the active fds is being closed */
90236317e1SRoger A. Faulkner 	int	a_buf[2];	/* buffer to which a_fd initially refers */
917c478bd9Sstevel@tonic-gate } afd_t;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate  * An lwpchan provides uniqueness when sleeping on user-level
957c478bd9Sstevel@tonic-gate  * synchronization primitives.  The lc_wchan member is used
967c478bd9Sstevel@tonic-gate  * for sleeping on kernel synchronization primitives.
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate typedef struct {
997c478bd9Sstevel@tonic-gate 	caddr_t lc_wchan0;
1007c478bd9Sstevel@tonic-gate 	caddr_t lc_wchan;
1017c478bd9Sstevel@tonic-gate } lwpchan_t;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate typedef struct _kthread	*kthread_id_t;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate struct turnstile;
106843e1988Sjohnlev struct panic_trap_info;
1077c478bd9Sstevel@tonic-gate struct upimutex;
1087c478bd9Sstevel@tonic-gate struct kproject;
1097c478bd9Sstevel@tonic-gate struct on_trap_data;
110c97ad5cdSakolb struct waitq;
1114568bee7Strevtom struct _kcpc_ctx;
1124568bee7Strevtom struct _kcpc_set;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /* Definition for kernel thread identifier type */
1157c478bd9Sstevel@tonic-gate typedef uint64_t kt_did_t;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate typedef struct _kthread {
1187c478bd9Sstevel@tonic-gate 	struct _kthread	*t_link; /* dispq, sleepq, and free queue link */
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	caddr_t	t_stk;		/* base of stack (kernel sp value to use) */
1217c478bd9Sstevel@tonic-gate 	void	(*t_startpc)(void);	/* PC where thread started */
1227c478bd9Sstevel@tonic-gate 	struct cpu *t_bound_cpu; /* cpu bound to, or NULL if not bound */
1237c478bd9Sstevel@tonic-gate 	short	t_affinitycnt;	/* nesting level of kernel affinity-setting */
1247c478bd9Sstevel@tonic-gate 	short	t_bind_cpu;	/* user-specified CPU binding (-1 if none) */
1252fc9ab6eSJerry Jelinek 	uint_t t_flag;		/* modified only by current thread */
1267c478bd9Sstevel@tonic-gate 	ushort_t t_proc_flag;	/* modified holding ttproc(t)->p_lock */
1277c478bd9Sstevel@tonic-gate 	ushort_t t_schedflag;	/* modified holding thread_lock(t) */
1287c478bd9Sstevel@tonic-gate 	volatile char t_preempt;	/* don't preempt thread if set */
1297c478bd9Sstevel@tonic-gate 	volatile char t_preempt_lk;
1307c478bd9Sstevel@tonic-gate 	uint_t	t_state;	/* thread state	(protected by thread_lock) */
1317c478bd9Sstevel@tonic-gate 	pri_t	t_pri;		/* assigned thread priority */
1327c478bd9Sstevel@tonic-gate 	pri_t	t_epri;		/* inherited thread priority */
133d4204c85Sraf 	pri_t	t_cpri;		/* thread scheduling class priority */
1347c478bd9Sstevel@tonic-gate 	char	t_writer;	/* sleeping in lwp_rwlock_lock(RW_WRITE_LOCK) */
1350b70c467Sakolb 	uchar_t	t_bindflag;	/* CPU and pset binding type */
1367c478bd9Sstevel@tonic-gate 	label_t	t_pcb;		/* pcb, save area when switching */
1377c478bd9Sstevel@tonic-gate 	lwpchan_t t_lwpchan;	/* reason for blocking */
1387c478bd9Sstevel@tonic-gate #define	t_wchan0	t_lwpchan.lc_wchan0
1397c478bd9Sstevel@tonic-gate #define	t_wchan		t_lwpchan.lc_wchan
1407c478bd9Sstevel@tonic-gate 	struct _sobj_ops *t_sobj_ops;
1417c478bd9Sstevel@tonic-gate 	id_t	t_cid;		/* scheduling class id */
1427c478bd9Sstevel@tonic-gate 	struct thread_ops *t_clfuncs;	/* scheduling class ops vector */
1437c478bd9Sstevel@tonic-gate 	void	*t_cldata;	/* per scheduling class specific data */
1447c478bd9Sstevel@tonic-gate 	ctxop_t	*t_ctx;		/* thread context */
1457c478bd9Sstevel@tonic-gate 	uintptr_t t_lofault;	/* ret pc for failed page faults */
1467c478bd9Sstevel@tonic-gate 	label_t	*t_onfault;	/* on_fault() setjmp buf */
1477c478bd9Sstevel@tonic-gate 	struct on_trap_data *t_ontrap;	/* on_trap() protection data */
148d32efdadSJonathan Adams 	caddr_t t_swap;		/* the bottom of the stack, if from segkp */
1497c478bd9Sstevel@tonic-gate 	lock_t	t_lock;		/* used to resume() a thread */
1507c478bd9Sstevel@tonic-gate 	uint8_t	t_lockstat;	/* set while thread is in lockstat code */
1517c478bd9Sstevel@tonic-gate 	uint8_t	t_pil;		/* interrupt thread PIL */
1527c478bd9Sstevel@tonic-gate 	disp_lock_t	t_pi_lock;	/* lock protecting t_prioinv list */
1537c478bd9Sstevel@tonic-gate 	char	t_nomigrate;	/* do not migrate if set */
1547c478bd9Sstevel@tonic-gate 	struct cpu	*t_cpu;	/* CPU that thread last ran on */
1557c478bd9Sstevel@tonic-gate 	struct cpu	*t_weakbound_cpu;	/* cpu weakly bound to */
1567c478bd9Sstevel@tonic-gate 	struct lgrp_ld	*t_lpl;	/* load average for home lgroup */
1577c478bd9Sstevel@tonic-gate 	void		*t_lgrp_reserv[2];	/* reserved for future */
1587c478bd9Sstevel@tonic-gate 	struct _kthread	*t_intr; /* interrupted (pinned) thread */
1597c478bd9Sstevel@tonic-gate 	uint64_t	t_intr_start;	/* timestamp when time slice began */
1607c478bd9Sstevel@tonic-gate 	kt_did_t	t_did;	/* thread id for kernel debuggers */
1614568bee7Strevtom 	struct _kcpc_ctx *t_cpc_ctx;	/* performance counter context */
1624568bee7Strevtom 	struct _kcpc_set *t_cpc_set;	/* set this thread has bound */
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	/*
1657c478bd9Sstevel@tonic-gate 	 * non swappable part of the lwp state.
1667c478bd9Sstevel@tonic-gate 	 */
1677c478bd9Sstevel@tonic-gate 	id_t		t_tid;		/* lwp's id */
1687c478bd9Sstevel@tonic-gate 	id_t		t_waitfor;	/* target lwp id in lwp_wait() */
1697c478bd9Sstevel@tonic-gate 	struct sigqueue	*t_sigqueue;	/* queue of siginfo structs */
1707c478bd9Sstevel@tonic-gate 	k_sigset_t	t_sig;		/* signals pending to this process */
1717c478bd9Sstevel@tonic-gate 	k_sigset_t	t_extsig;	/* signals sent from another contract */
1727c478bd9Sstevel@tonic-gate 	k_sigset_t	t_hold;		/* hold signal bit mask */
1733d729aecSJerry Jelinek 	k_sigset_t	t_sigwait;	/* sigtimedwait/sigfd accepting these */
1747c478bd9Sstevel@tonic-gate 	struct	_kthread *t_forw;	/* process's forward thread link */
1757c478bd9Sstevel@tonic-gate 	struct	_kthread *t_back;	/* process's backward thread link */
1767c478bd9Sstevel@tonic-gate 	struct	_kthread *t_thlink;	/* tid (lwpid) lookup hash link */
1777c478bd9Sstevel@tonic-gate 	klwp_t	*t_lwp;			/* thread's lwp pointer */
1787c478bd9Sstevel@tonic-gate 	struct	proc	*t_procp;	/* proc pointer */
1797c478bd9Sstevel@tonic-gate 	struct	t_audit_data *t_audit_data;	/* per thread audit data */
1807c478bd9Sstevel@tonic-gate 	struct	_kthread *t_next;	/* doubly linked list of all threads */
1817c478bd9Sstevel@tonic-gate 	struct	_kthread *t_prev;
1827c478bd9Sstevel@tonic-gate 	ushort_t t_whystop;		/* reason for stopping */
1837c478bd9Sstevel@tonic-gate 	ushort_t t_whatstop;		/* more detailed reason */
1847c478bd9Sstevel@tonic-gate 	int	t_dslot;		/* index in proc's thread directory */
1857c478bd9Sstevel@tonic-gate 	struct	pollstate *t_pollstate;	/* state used during poll(2) */
1867c478bd9Sstevel@tonic-gate 	struct	pollcache *t_pollcache;	/* to pass a pcache ptr by /dev/poll */
1877c478bd9Sstevel@tonic-gate 	struct	cred	*t_cred;	/* pointer to current cred */
1887c478bd9Sstevel@tonic-gate 	time_t	t_start;		/* start time, seconds since epoch */
1897c478bd9Sstevel@tonic-gate 	clock_t	t_lbolt;		/* lbolt at last clock_tick() */
1907c478bd9Sstevel@tonic-gate 	hrtime_t t_stoptime;		/* timestamp at stop() */
1917c478bd9Sstevel@tonic-gate 	uint_t	t_pctcpu;		/* %cpu at last clock_tick(), binary */
1927c478bd9Sstevel@tonic-gate 					/* point at right of high-order bit */
1937c478bd9Sstevel@tonic-gate 	short	t_sysnum;		/* system call number */
1947c478bd9Sstevel@tonic-gate 	kcondvar_t	t_delay_cv;
1957c478bd9Sstevel@tonic-gate 	kmutex_t	t_delay_lock;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	/*
1987c478bd9Sstevel@tonic-gate 	 * Pointer to the dispatcher lock protecting t_state and state-related
1997c478bd9Sstevel@tonic-gate 	 * flags.  This pointer can change during waits on the lock, so
2007c478bd9Sstevel@tonic-gate 	 * it should be grabbed only by thread_lock().
2017c478bd9Sstevel@tonic-gate 	 */
2027c478bd9Sstevel@tonic-gate 	disp_lock_t	*t_lockp;	/* pointer to the dispatcher lock */
2036a0b1217SPatrick Mooney 	ushort_t	t_oldspl;	/* spl level before dispatcher locked */
2047c478bd9Sstevel@tonic-gate 	volatile char	t_pre_sys;	/* pre-syscall work needed */
2057c478bd9Sstevel@tonic-gate 	lock_t		t_lock_flush;	/* for lock_mutex_flush() impl */
2067c478bd9Sstevel@tonic-gate 	struct _disp	*t_disp_queue;	/* run queue for chosen CPU */
2077c478bd9Sstevel@tonic-gate 	clock_t		t_disp_time;	/* last time this thread was running */
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	/*
2107c478bd9Sstevel@tonic-gate 	 * Post-syscall / post-trap flags.
2116a0b1217SPatrick Mooney 	 *	No lock is required to set these.
2127c478bd9Sstevel@tonic-gate 	 *	These must be cleared only by the thread itself.
2137c478bd9Sstevel@tonic-gate 	 *
2147c478bd9Sstevel@tonic-gate 	 *	t_astflag indicates that some post-trap processing is required,
2157c478bd9Sstevel@tonic-gate 	 *		possibly a signal or a preemption.  The thread will not
2167c478bd9Sstevel@tonic-gate 	 *		return to user with this set.
2177c478bd9Sstevel@tonic-gate 	 *	t_post_sys indicates that some unusualy post-system call
2187c478bd9Sstevel@tonic-gate 	 *		handling is required, such as an error or tracing.
2197c478bd9Sstevel@tonic-gate 	 *	t_sig_check indicates that some condition in ISSIG() must be
2206a0b1217SPatrick Mooney 	 *		checked, but doesn't prevent returning to user.
2217c478bd9Sstevel@tonic-gate 	 *	t_post_sys_ast is a way of checking whether any of these three
2227c478bd9Sstevel@tonic-gate 	 *		flags are set.
2237c478bd9Sstevel@tonic-gate 	 */
2247c478bd9Sstevel@tonic-gate 	union __tu {
2257c478bd9Sstevel@tonic-gate 		struct __ts {
2267c478bd9Sstevel@tonic-gate 			volatile char	_t_astflag;	/* AST requested */
2277c478bd9Sstevel@tonic-gate 			volatile char	_t_sig_check;	/* ISSIG required */
2287c478bd9Sstevel@tonic-gate 			volatile char	_t_post_sys;	/* post_syscall req */
2297c478bd9Sstevel@tonic-gate 			volatile char	_t_trapret;	/* call CL_TRAPRET */
2307c478bd9Sstevel@tonic-gate 		} _ts;
2317c478bd9Sstevel@tonic-gate 		volatile int	_t_post_sys_ast;	/* OR of these flags */
2327c478bd9Sstevel@tonic-gate 	} _tu;
2337c478bd9Sstevel@tonic-gate #define	t_astflag	_tu._ts._t_astflag
2347c478bd9Sstevel@tonic-gate #define	t_sig_check	_tu._ts._t_sig_check
2357c478bd9Sstevel@tonic-gate #define	t_post_sys	_tu._ts._t_post_sys
2367c478bd9Sstevel@tonic-gate #define	t_trapret	_tu._ts._t_trapret
2377c478bd9Sstevel@tonic-gate #define	t_post_sys_ast	_tu._t_post_sys_ast
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	/*
2407c478bd9Sstevel@tonic-gate 	 * Real time microstate profiling.
2417c478bd9Sstevel@tonic-gate 	 */
2427c478bd9Sstevel@tonic-gate 					/* possible 4-byte filler */
2437c478bd9Sstevel@tonic-gate 	hrtime_t t_waitrq;		/* timestamp for run queue wait time */
2447c478bd9Sstevel@tonic-gate 	int	t_mstate;		/* current microstate */
2457c478bd9Sstevel@tonic-gate 	struct rprof {
2467c478bd9Sstevel@tonic-gate 		int	rp_anystate;		/* set if any state non-zero */
2477c478bd9Sstevel@tonic-gate 		uint_t	rp_state[NMSTATES];	/* mstate profiling counts */
2487c478bd9Sstevel@tonic-gate 	} *t_rprof;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	/*
2517c478bd9Sstevel@tonic-gate 	 * There is a turnstile inserted into the list below for
2527c478bd9Sstevel@tonic-gate 	 * every priority inverted synchronization object that
2537c478bd9Sstevel@tonic-gate 	 * this thread holds.
2547c478bd9Sstevel@tonic-gate 	 */
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	struct turnstile *t_prioinv;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	/*
2597c478bd9Sstevel@tonic-gate 	 * Pointer to the turnstile attached to the synchronization
2607c478bd9Sstevel@tonic-gate 	 * object where this thread is blocked.
2617c478bd9Sstevel@tonic-gate 	 */
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	struct turnstile *t_ts;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	/*
2667c478bd9Sstevel@tonic-gate 	 * kernel thread specific data
2677c478bd9Sstevel@tonic-gate 	 *	Borrowed from userland implementation of POSIX tsd
2687c478bd9Sstevel@tonic-gate 	 */
2697c478bd9Sstevel@tonic-gate 	struct tsd_thread {
2707c478bd9Sstevel@tonic-gate 		struct tsd_thread *ts_next;	/* threads with TSD */
2717c478bd9Sstevel@tonic-gate 		struct tsd_thread *ts_prev;	/* threads with TSD */
2727c478bd9Sstevel@tonic-gate 		uint_t		  ts_nkeys;	/* entries in value array */
2737c478bd9Sstevel@tonic-gate 		void		  **ts_value;	/* array of value/key */
2747c478bd9Sstevel@tonic-gate 	} *t_tsd;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	clock_t		t_stime;	/* time stamp used by the swapper */
2777c478bd9Sstevel@tonic-gate 	struct door_data *t_door;	/* door invocation data */
2787c478bd9Sstevel@tonic-gate 	kmutex_t	*t_plockp;	/* pointer to process's p_lock */
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	struct sc_shared *t_schedctl;	/* scheduler activations shared data */
2817c478bd9Sstevel@tonic-gate 	uintptr_t	t_sc_uaddr;	/* user-level address of shared data */
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	struct cpupart	*t_cpupart;	/* partition containing thread */
2847c478bd9Sstevel@tonic-gate 	int		t_bind_pset;	/* processor set binding */
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	struct copyops	*t_copyops;	/* copy in/out ops vector */
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	caddr_t		t_stkbase;	/* base of the the stack */
2897c478bd9Sstevel@tonic-gate 	struct page	*t_red_pp;	/* if non-NULL, redzone is mapped */
2907c478bd9Sstevel@tonic-gate 
291236317e1SRoger A. Faulkner 	afd_t		t_activefd;	/* active file descriptor table */
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	struct _kthread	*t_priforw;	/* sleepq per-priority sublist */
2947c478bd9Sstevel@tonic-gate 	struct _kthread	*t_priback;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	struct sleepq	*t_sleepq;	/* sleep queue thread is waiting on */
297843e1988Sjohnlev 	struct panic_trap_info *t_panic_trap;	/* saved data from fatal trap */
2987c478bd9Sstevel@tonic-gate 	int		*t_lgrp_affinity;	/* lgroup affinity */
2997c478bd9Sstevel@tonic-gate 	struct upimutex	*t_upimutex;	/* list of upimutexes owned by thread */
3007c478bd9Sstevel@tonic-gate 	uint32_t	t_nupinest;	/* number of nested held upi mutexes */
3017c478bd9Sstevel@tonic-gate 	struct kproject *t_proj;	/* project containing this thread */
3027c478bd9Sstevel@tonic-gate 	uint8_t		t_unpark;	/* modified holding t_delay_lock */
3037c478bd9Sstevel@tonic-gate 	uint8_t		t_release;	/* lwp_release() waked up the thread */
3047c478bd9Sstevel@tonic-gate 	uint8_t		t_hatdepth;	/* depth of recursive hat_memloads */
305551bc2a6Smrj 	uint8_t		t_xpvcntr;	/* see xen_block_migrate() */
3067c478bd9Sstevel@tonic-gate 	kcondvar_t	t_joincv;	/* cv used to wait for thread exit */
3077c478bd9Sstevel@tonic-gate 	void		*t_taskq;	/* for threads belonging to taskq */
3087c478bd9Sstevel@tonic-gate 	hrtime_t	t_anttime;	/* most recent time anticipatory load */
3097c478bd9Sstevel@tonic-gate 					/*	was added to an lgroup's load */
3107c478bd9Sstevel@tonic-gate 					/*	on this thread's behalf */
3117c478bd9Sstevel@tonic-gate 	char		*t_pdmsg;	/* privilege debugging message */
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	uint_t		t_predcache;	/* DTrace predicate cache */
3147c478bd9Sstevel@tonic-gate 	hrtime_t	t_dtrace_vtime;	/* DTrace virtual time */
3157c478bd9Sstevel@tonic-gate 	hrtime_t	t_dtrace_start;	/* DTrace slice start time */
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	uint8_t		t_dtrace_stop;	/* indicates a DTrace-desired stop */
3187c478bd9Sstevel@tonic-gate 	uint8_t		t_dtrace_sig;	/* signal sent via DTrace's raise() */
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	union __tdu {
3217c478bd9Sstevel@tonic-gate 		struct __tds {
3227c478bd9Sstevel@tonic-gate 			uint8_t	_t_dtrace_on;	/* hit a fasttrap tracepoint */
3237c478bd9Sstevel@tonic-gate 			uint8_t	_t_dtrace_step;	/* about to return to kernel */
3247c478bd9Sstevel@tonic-gate 			uint8_t	_t_dtrace_ret;	/* handling a return probe */
3257c478bd9Sstevel@tonic-gate 			uint8_t	_t_dtrace_ast;	/* saved ast flag */
3267c478bd9Sstevel@tonic-gate #ifdef __amd64
3277c478bd9Sstevel@tonic-gate 			uint8_t	_t_dtrace_reg;	/* modified register */
3287c478bd9Sstevel@tonic-gate #endif
3297c478bd9Sstevel@tonic-gate 		} _tds;
3307c478bd9Sstevel@tonic-gate 		ulong_t	_t_dtrace_ft;		/* bitwise or of these flags */
3317c478bd9Sstevel@tonic-gate 	} _tdu;
3327c478bd9Sstevel@tonic-gate #define	t_dtrace_ft	_tdu._t_dtrace_ft
3337c478bd9Sstevel@tonic-gate #define	t_dtrace_on	_tdu._tds._t_dtrace_on
3347c478bd9Sstevel@tonic-gate #define	t_dtrace_step	_tdu._tds._t_dtrace_step
3357c478bd9Sstevel@tonic-gate #define	t_dtrace_ret	_tdu._tds._t_dtrace_ret
3367c478bd9Sstevel@tonic-gate #define	t_dtrace_ast	_tdu._tds._t_dtrace_ast
3377c478bd9Sstevel@tonic-gate #ifdef __amd64
3387c478bd9Sstevel@tonic-gate #define	t_dtrace_reg	_tdu._tds._t_dtrace_reg
3397c478bd9Sstevel@tonic-gate #endif
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	uintptr_t	t_dtrace_pc;	/* DTrace saved pc from fasttrap */
3427c478bd9Sstevel@tonic-gate 	uintptr_t	t_dtrace_npc;	/* DTrace next pc from fasttrap */
3437c478bd9Sstevel@tonic-gate 	uintptr_t	t_dtrace_scrpc;	/* DTrace per-thread scratch location */
3447c478bd9Sstevel@tonic-gate 	uintptr_t	t_dtrace_astpc;	/* DTrace return sequence location */
3457c478bd9Sstevel@tonic-gate #ifdef __amd64
3467c478bd9Sstevel@tonic-gate 	uint64_t	t_dtrace_regv;	/* DTrace saved reg from fasttrap */
3473ce2fcdcSRobert Mustacchi 	uint64_t	t_useracc;	/* SMAP state saved across swtch() */
3487c478bd9Sstevel@tonic-gate #endif
3497c478bd9Sstevel@tonic-gate 	hrtime_t	t_hrtime;	/* high-res last time on cpu */
3505a64ecfaStrevtom 	kmutex_t	t_ctx_lock;	/* protects t_ctx in removectx() */
351c97ad5cdSakolb 	struct waitq	*t_waitq;	/* wait queue */
352454ab202SMadhavan Venkataraman 	kmutex_t	t_wait_mutex;	/* used in CV wait functions */
353ab618543SJohn Levon 
354ab618543SJohn Levon 	char		*t_name;	/* thread name */
355455e370cSJohn Levon 
356c3377ee9SJohn Levon 	uint64_t	t_unsafe;	/* unsafe to run with SMT VCPU thread */
3577c478bd9Sstevel@tonic-gate } kthread_t;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate /*
3607c478bd9Sstevel@tonic-gate  * Thread flag (t_flag) definitions.
3617c478bd9Sstevel@tonic-gate  *	These flags must be changed only for the current thread,
3626a0b1217SPatrick Mooney  *	and not during preemption code, since the code being
3637c478bd9Sstevel@tonic-gate  *	preempted could be modifying the flags.
3647c478bd9Sstevel@tonic-gate  *
3657c478bd9Sstevel@tonic-gate  *	For the most part these flags do not need locking.
3667c478bd9Sstevel@tonic-gate  *	The following flags will only be changed while the thread_lock is held,
3677c478bd9Sstevel@tonic-gate  *	to give assurrance that they are consistent with t_state:
3687c478bd9Sstevel@tonic-gate  *		T_WAKEABLE
3697c478bd9Sstevel@tonic-gate  */
3707c478bd9Sstevel@tonic-gate #define	T_INTR_THREAD	0x0001	/* thread is an interrupt thread */
3717c478bd9Sstevel@tonic-gate #define	T_WAKEABLE	0x0002	/* thread is blocked, signals enabled */
3727c478bd9Sstevel@tonic-gate #define	T_TOMASK	0x0004	/* use lwp_sigoldmask on return from signal */
3737c478bd9Sstevel@tonic-gate #define	T_TALLOCSTK	0x0008  /* thread structure allocated from stk */
374c4978b50Sraf #define	T_FORKALL	0x0010	/* thread was cloned by forkall() */
3757c478bd9Sstevel@tonic-gate #define	T_WOULDBLOCK	0x0020	/* for lockfs */
3767c478bd9Sstevel@tonic-gate #define	T_DONTBLOCK	0x0040	/* for lockfs */
3777c478bd9Sstevel@tonic-gate #define	T_DONTPEND	0x0080	/* for lockfs */
3787c478bd9Sstevel@tonic-gate #define	T_SYS_PROF	0x0100	/* profiling on for duration of system call */
3797c478bd9Sstevel@tonic-gate #define	T_WAITCVSEM	0x0200	/* waiting for a lwp_cv or lwp_sema on sleepq */
3807c478bd9Sstevel@tonic-gate #define	T_WATCHPT	0x0400	/* thread undergoing a watchpoint emulation */
3817c478bd9Sstevel@tonic-gate #define	T_PANIC		0x0800	/* thread initiated a system panic */
382d32efdadSJonathan Adams #define	T_LWPREUSE	0x1000	/* stack and LWP can be reused */
3838b464eb8Smec #define	T_CAPTURING	0x2000	/* thread is in page capture logic */
3848d186f16Sraf #define	T_VFPARENT	0x4000	/* thread is vfork parent, must call vfwait */
3855cc25c11Smb #define	T_DONTDTRACE	0x8000  /* disable DTrace probes */
3862fc9ab6eSJerry Jelinek #define	T_KFPU		0x10000	/* kernel FPU active */
387f6ef4223SJoshua M. Clulow #define	T_PUSHPAGE	0x20000	/* this thread may be assisting pageout */
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate /*
3907c478bd9Sstevel@tonic-gate  * Flags in t_proc_flag.
3917c478bd9Sstevel@tonic-gate  *	These flags must be modified only when holding the p_lock
3927c478bd9Sstevel@tonic-gate  *	for the associated process.
3937c478bd9Sstevel@tonic-gate  */
3947c478bd9Sstevel@tonic-gate #define	TP_DAEMON	0x0001	/* this is an LWP_DAEMON lwp */
3957c478bd9Sstevel@tonic-gate #define	TP_HOLDLWP	0x0002	/* hold thread's lwp */
3967c478bd9Sstevel@tonic-gate #define	TP_TWAIT	0x0004	/* wait to be freed by lwp_wait() */
3977c478bd9Sstevel@tonic-gate #define	TP_LWPEXIT	0x0008	/* lwp has exited */
3987c478bd9Sstevel@tonic-gate #define	TP_PRSTOP	0x0010	/* thread is being stopped via /proc */
3997c478bd9Sstevel@tonic-gate #define	TP_CHKPT	0x0020	/* thread is being stopped via CPR checkpoint */
4007c478bd9Sstevel@tonic-gate #define	TP_EXITLWP	0x0040	/* terminate this lwp */
4017c478bd9Sstevel@tonic-gate #define	TP_PRVSTOP	0x0080	/* thread is virtually stopped via /proc */
4027c478bd9Sstevel@tonic-gate #define	TP_MSACCT	0x0100	/* collect micro-state accounting information */
4037c478bd9Sstevel@tonic-gate #define	TP_STOPPING	0x0200	/* thread is executing stop() */
4047c478bd9Sstevel@tonic-gate #define	TP_WATCHPT	0x0400	/* process has watchpoints in effect */
4057c478bd9Sstevel@tonic-gate #define	TP_PAUSE	0x0800	/* process is being stopped via pauselwps() */
4067c478bd9Sstevel@tonic-gate #define	TP_CHANGEBIND	0x1000	/* thread has a new cpu/cpupart binding */
4077c478bd9Sstevel@tonic-gate #define	TP_ZTHREAD	0x2000	/* this is a kernel thread for a zone */
4087c478bd9Sstevel@tonic-gate #define	TP_WATCHSTOP	0x4000	/* thread is stopping via holdwatch() */
4094c819f48SJerry Jelinek #define	TP_KTHREAD	0x8000	/* in-kernel worker thread for a process */
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate /*
4127c478bd9Sstevel@tonic-gate  * Thread scheduler flag (t_schedflag) definitions.
4137c478bd9Sstevel@tonic-gate  *	The thread must be locked via thread_lock() or equiv. to change these.
4147c478bd9Sstevel@tonic-gate  */
4157c478bd9Sstevel@tonic-gate #define	TS_LOAD		0x0001	/* thread is in memory */
4167c478bd9Sstevel@tonic-gate #define	TS_DONT_SWAP	0x0002	/* thread/lwp should not be swapped */
4177c478bd9Sstevel@tonic-gate #define	TS_SWAPENQ	0x0004	/* swap thread when it reaches a safe point */
4187c478bd9Sstevel@tonic-gate #define	TS_ON_SWAPQ	0x0008	/* thread is on the swap queue */
4197c478bd9Sstevel@tonic-gate #define	TS_SIGNALLED	0x0010	/* thread was awakened by cv_signal() */
420c97ad5cdSakolb #define	TS_PROJWAITQ	0x0020	/* thread is on its project's waitq */
421c97ad5cdSakolb #define	TS_ZONEWAITQ	0x0040	/* thread is on its zone's waitq */
422455e370cSJohn Levon #define	TS_VCPU		0x0080	/* thread will enter guest context */
4237c478bd9Sstevel@tonic-gate #define	TS_CSTART	0x0100	/* setrun() by continuelwps() */
4247c478bd9Sstevel@tonic-gate #define	TS_UNPAUSE	0x0200	/* setrun() by unpauselwps() */
4257c478bd9Sstevel@tonic-gate #define	TS_XSTART	0x0400	/* setrun() by SIGCONT */
4267c478bd9Sstevel@tonic-gate #define	TS_PSTART	0x0800	/* setrun() by /proc */
4277c478bd9Sstevel@tonic-gate #define	TS_RESUME	0x1000	/* setrun() by CPR resume process */
4287c478bd9Sstevel@tonic-gate #define	TS_CREATE	0x2000	/* setrun() by syslwp_create() */
4297c478bd9Sstevel@tonic-gate #define	TS_RUNQMATCH	0x4000	/* exact run queue balancing by setbackdq() */
4307c478bd9Sstevel@tonic-gate #define	TS_ALLSTART	\
4317c478bd9Sstevel@tonic-gate 	(TS_CSTART|TS_UNPAUSE|TS_XSTART|TS_PSTART|TS_RESUME|TS_CREATE)
432c97ad5cdSakolb #define	TS_ANYWAITQ	(TS_PROJWAITQ|TS_ZONEWAITQ)
4337c478bd9Sstevel@tonic-gate 
4340b70c467Sakolb /*
4350b70c467Sakolb  * Thread binding types
4360b70c467Sakolb  */
4370b70c467Sakolb #define	TB_ALLHARD	0
4380b70c467Sakolb #define	TB_CPU_SOFT	0x01		/* soft binding to CPU */
4390b70c467Sakolb #define	TB_PSET_SOFT	0x02		/* soft binding to pset */
4400b70c467Sakolb 
4410b70c467Sakolb #define	TB_CPU_SOFT_SET(t)		((t)->t_bindflag |= TB_CPU_SOFT)
4420b70c467Sakolb #define	TB_CPU_HARD_SET(t)		((t)->t_bindflag &= ~TB_CPU_SOFT)
4430b70c467Sakolb #define	TB_PSET_SOFT_SET(t)		((t)->t_bindflag |= TB_PSET_SOFT)
4440b70c467Sakolb #define	TB_PSET_HARD_SET(t)		((t)->t_bindflag &= ~TB_PSET_SOFT)
4450b70c467Sakolb #define	TB_CPU_IS_SOFT(t)		((t)->t_bindflag & TB_CPU_SOFT)
4460b70c467Sakolb #define	TB_CPU_IS_HARD(t)		(!TB_CPU_IS_SOFT(t))
4470b70c467Sakolb #define	TB_PSET_IS_SOFT(t)		((t)->t_bindflag & TB_PSET_SOFT)
4480b70c467Sakolb 
4497c478bd9Sstevel@tonic-gate /*
4507c478bd9Sstevel@tonic-gate  * No locking needed for AST field.
4517c478bd9Sstevel@tonic-gate  */
4527c478bd9Sstevel@tonic-gate #define	aston(t)		((t)->t_astflag = 1)
4537c478bd9Sstevel@tonic-gate #define	astoff(t)		((t)->t_astflag = 0)
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate /* True if thread is stopped on an event of interest */
4567c478bd9Sstevel@tonic-gate #define	ISTOPPED(t) ((t)->t_state == TS_STOPPED && \
4577c478bd9Sstevel@tonic-gate 			!((t)->t_schedflag & TS_PSTART))
4587c478bd9Sstevel@tonic-gate 
459c97ad5cdSakolb /* True if thread is asleep and wakeable */
460c97ad5cdSakolb #define	ISWAKEABLE(t) (((t)->t_state == TS_SLEEP && \
461c97ad5cdSakolb 			((t)->t_flag & T_WAKEABLE)))
462c97ad5cdSakolb 
463c97ad5cdSakolb /* True if thread is on the wait queue */
464c97ad5cdSakolb #define	ISWAITING(t) ((t)->t_state == TS_WAIT)
465c97ad5cdSakolb 
4667c478bd9Sstevel@tonic-gate /* similar to ISTOPPED except the event of interest is CPR */
4677c478bd9Sstevel@tonic-gate #define	CPR_ISTOPPED(t) ((t)->t_state == TS_STOPPED && \
4687c478bd9Sstevel@tonic-gate 			!((t)->t_schedflag & TS_RESUME))
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate /*
4717c478bd9Sstevel@tonic-gate  * True if thread is virtually stopped (is or was asleep in
4727c478bd9Sstevel@tonic-gate  * one of the lwp_*() system calls and marked to stop by /proc.)
4737c478bd9Sstevel@tonic-gate  */
4747c478bd9Sstevel@tonic-gate #define	VSTOPPED(t)	((t)->t_proc_flag & TP_PRVSTOP)
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate /* similar to VSTOPPED except the point of interest is CPR */
4777c478bd9Sstevel@tonic-gate #define	CPR_VSTOPPED(t)				\
4787c478bd9Sstevel@tonic-gate 	((t)->t_state == TS_SLEEP &&		\
4797c478bd9Sstevel@tonic-gate 	(t)->t_wchan0 != NULL &&		\
4807c478bd9Sstevel@tonic-gate 	((t)->t_flag & T_WAKEABLE) &&		\
4817c478bd9Sstevel@tonic-gate 	((t)->t_proc_flag & TP_CHKPT))
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate /* True if thread has been stopped by hold*() or was created stopped */
4847c478bd9Sstevel@tonic-gate #define	SUSPENDED(t) ((t)->t_state == TS_STOPPED && \
4857c478bd9Sstevel@tonic-gate 	((t)->t_schedflag & (TS_CSTART|TS_UNPAUSE)) != (TS_CSTART|TS_UNPAUSE))
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate /* True if thread possesses an inherited priority */
4887c478bd9Sstevel@tonic-gate #define	INHERITED(t)	((t)->t_epri != 0)
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate /* The dispatch priority of a thread */
4917c478bd9Sstevel@tonic-gate #define	DISP_PRIO(t) ((t)->t_epri > (t)->t_pri ? (t)->t_epri : (t)->t_pri)
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate /* The assigned priority of a thread */
4947c478bd9Sstevel@tonic-gate #define	ASSIGNED_PRIO(t)	((t)->t_pri)
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate /*
4977c478bd9Sstevel@tonic-gate  * Macros to determine whether a thread can be swapped.
4987c478bd9Sstevel@tonic-gate  * If t_lock is held, the thread is either on a processor or being swapped.
4997c478bd9Sstevel@tonic-gate  */
5007c478bd9Sstevel@tonic-gate #define	SWAP_OK(t)	(!LOCK_HELD(&(t)->t_lock))
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate /*
5037c478bd9Sstevel@tonic-gate  * proctot(x)
5047c478bd9Sstevel@tonic-gate  *	convert a proc pointer to a thread pointer. this only works with
5057c478bd9Sstevel@tonic-gate  *	procs that have only one lwp.
5067c478bd9Sstevel@tonic-gate  *
5077c478bd9Sstevel@tonic-gate  * proctolwp(x)
5087c478bd9Sstevel@tonic-gate  *	convert a proc pointer to a lwp pointer. this only works with
5097c478bd9Sstevel@tonic-gate  *	procs that have only one lwp.
5107c478bd9Sstevel@tonic-gate  *
5117c478bd9Sstevel@tonic-gate  * ttolwp(x)
5127c478bd9Sstevel@tonic-gate  *	convert a thread pointer to its lwp pointer.
5137c478bd9Sstevel@tonic-gate  *
5147c478bd9Sstevel@tonic-gate  * ttoproc(x)
5157c478bd9Sstevel@tonic-gate  *	convert a thread pointer to its proc pointer.
5167c478bd9Sstevel@tonic-gate  *
5177c478bd9Sstevel@tonic-gate  * ttoproj(x)
5186a0b1217SPatrick Mooney  *	convert a thread pointer to its project pointer.
5197c478bd9Sstevel@tonic-gate  *
520c97ad5cdSakolb  * ttozone(x)
5216a0b1217SPatrick Mooney  *	convert a thread pointer to its zone pointer.
522c97ad5cdSakolb  *
5237c478bd9Sstevel@tonic-gate  * lwptot(x)
5247c478bd9Sstevel@tonic-gate  *	convert a lwp pointer to its thread pointer.
5257c478bd9Sstevel@tonic-gate  *
5267c478bd9Sstevel@tonic-gate  * lwptoproc(x)
5277c478bd9Sstevel@tonic-gate  *	convert a lwp to its proc pointer.
5287c478bd9Sstevel@tonic-gate  */
5297c478bd9Sstevel@tonic-gate #define	proctot(x)	((x)->p_tlist)
5307c478bd9Sstevel@tonic-gate #define	proctolwp(x)	((x)->p_tlist->t_lwp)
5317c478bd9Sstevel@tonic-gate #define	ttolwp(x)	((x)->t_lwp)
5327c478bd9Sstevel@tonic-gate #define	ttoproc(x)	((x)->t_procp)
5337c478bd9Sstevel@tonic-gate #define	ttoproj(x)	((x)->t_proj)
534c97ad5cdSakolb #define	ttozone(x)	((x)->t_procp->p_zone)
5357c478bd9Sstevel@tonic-gate #define	lwptot(x)	((x)->lwp_thread)
5367c478bd9Sstevel@tonic-gate #define	lwptoproc(x)	((x)->lwp_procp)
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate #define	t_pc		t_pcb.val[0]
5397c478bd9Sstevel@tonic-gate #define	t_sp		t_pcb.val[1]
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate #ifdef	_KERNEL
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate extern	kthread_t	*threadp(void);	/* inline, returns thread pointer */
5447c478bd9Sstevel@tonic-gate #define	curthread	(threadp())		/* current thread pointer */
5457c478bd9Sstevel@tonic-gate #define	curproc		(ttoproc(curthread))	/* current process pointer */
5467c478bd9Sstevel@tonic-gate #define	curproj		(ttoproj(curthread))	/* current project pointer */
547c97ad5cdSakolb #define	curzone		(curproc->p_zone)	/* current zone pointer */
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate extern	struct _kthread	t0;		/* the scheduler thread */
5507c478bd9Sstevel@tonic-gate extern	kmutex_t	pidlock;	/* global process lock */
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate /*
5532850d85bSmv  * thread_free_lock is used by the tick accounting thread to keep a thread
5547c478bd9Sstevel@tonic-gate  * from being freed while it is being examined.
55591f917c7SMadhavan Venkataraman  *
55691f917c7SMadhavan Venkataraman  * Thread structures are 32-byte aligned structures. That is why we use the
55791f917c7SMadhavan Venkataraman  * following formula.
5587c478bd9Sstevel@tonic-gate  */
55991f917c7SMadhavan Venkataraman #define	THREAD_FREE_BITS	10
56091f917c7SMadhavan Venkataraman #define	THREAD_FREE_NUM		(1 << THREAD_FREE_BITS)
5612850d85bSmv #define	THREAD_FREE_MASK	(THREAD_FREE_NUM - 1)
56291f917c7SMadhavan Venkataraman #define	THREAD_FREE_1		PTR24_LSB
56391f917c7SMadhavan Venkataraman #define	THREAD_FREE_2		(PTR24_LSB + THREAD_FREE_BITS)
56491f917c7SMadhavan Venkataraman #define	THREAD_FREE_SHIFT(t)	\
56591f917c7SMadhavan Venkataraman 	(((ulong_t)(t) >> THREAD_FREE_1) ^ ((ulong_t)(t) >> THREAD_FREE_2))
5662850d85bSmv #define	THREAD_FREE_HASH(t)	(THREAD_FREE_SHIFT(t) & THREAD_FREE_MASK)
5672850d85bSmv 
5682850d85bSmv typedef struct thread_free_lock {
5692850d85bSmv 	kmutex_t	tf_lock;
5702850d85bSmv 	uchar_t		tf_pad[64 - sizeof (kmutex_t)];
5712850d85bSmv } thread_free_lock_t;
5722850d85bSmv 
5732850d85bSmv extern void	thread_free_prevent(kthread_t *);
5742850d85bSmv extern void	thread_free_allow(kthread_t *);
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate /*
5777c478bd9Sstevel@tonic-gate  * Routines to change the priority and effective priority
5787c478bd9Sstevel@tonic-gate  * of a thread-locked thread, whatever its state.
5797c478bd9Sstevel@tonic-gate  */
5807c478bd9Sstevel@tonic-gate extern int	thread_change_pri(kthread_t *t, pri_t disp_pri, int front);
5817c478bd9Sstevel@tonic-gate extern void	thread_change_epri(kthread_t *t, pri_t disp_pri);
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate /*
5847c478bd9Sstevel@tonic-gate  * Routines that manipulate the dispatcher lock for the thread.
5857c478bd9Sstevel@tonic-gate  * The locking heirarchy is as follows:
5867c478bd9Sstevel@tonic-gate  *	cpu_lock > sleepq locks > run queue locks
5877c478bd9Sstevel@tonic-gate  */
5887c478bd9Sstevel@tonic-gate void	thread_transition(kthread_t *); /* move to transition lock */
5897c478bd9Sstevel@tonic-gate void	thread_stop(kthread_t *);	/* move to stop lock */
5907c478bd9Sstevel@tonic-gate void	thread_lock(kthread_t *);	/* lock thread and its queue */
5917c478bd9Sstevel@tonic-gate void	thread_lock_high(kthread_t *);	/* lock thread and its queue */
5927c478bd9Sstevel@tonic-gate void	thread_onproc(kthread_t *, struct cpu *); /* set onproc state lock */
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate #define	thread_unlock(t)		disp_lock_exit((t)->t_lockp)
5957c478bd9Sstevel@tonic-gate #define	thread_unlock_high(t)		disp_lock_exit_high((t)->t_lockp)
5967c478bd9Sstevel@tonic-gate #define	thread_unlock_nopreempt(t)	disp_lock_exit_nopreempt((t)->t_lockp)
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate #define	THREAD_LOCK_HELD(t)	(DISP_LOCK_HELD((t)->t_lockp))
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate extern disp_lock_t transition_lock;	/* lock protecting transiting threads */
6017c478bd9Sstevel@tonic-gate extern disp_lock_t stop_lock;		/* lock protecting stopped threads */
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate caddr_t	thread_stk_init(caddr_t);	/* init thread stack */
6047c478bd9Sstevel@tonic-gate 
605ab618543SJohn Levon int thread_setname(kthread_t *, const char *);
606ab618543SJohn Levon int thread_vsetname(kthread_t *, const char *, ...);
607ab618543SJohn Levon 
6080b70c467Sakolb extern int default_binding_mode;
609*cab61dfbSBryan Cantrill extern int default_stksize;
6100b70c467Sakolb 
6117c478bd9Sstevel@tonic-gate #endif	/* _KERNEL */
6127c478bd9Sstevel@tonic-gate 
613ab618543SJohn Levon #define	THREAD_NAME_MAX	32	/* includes terminating NUL */
614ab618543SJohn Levon 
6157c478bd9Sstevel@tonic-gate /*
6167c478bd9Sstevel@tonic-gate  * Macro to change a thread's priority.
6177c478bd9Sstevel@tonic-gate  */
6187c478bd9Sstevel@tonic-gate #define	THREAD_CHANGE_PRI(t, pri) {					\
6197c478bd9Sstevel@tonic-gate 	pri_t __new_pri = (pri);					\
6207c478bd9Sstevel@tonic-gate 	DTRACE_SCHED2(change__pri, kthread_t *, (t), pri_t, __new_pri);	\
6217c478bd9Sstevel@tonic-gate 	(t)->t_pri = __new_pri;						\
622d4204c85Sraf 	schedctl_set_cidpri(t);						\
6237c478bd9Sstevel@tonic-gate }
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate /*
6267c478bd9Sstevel@tonic-gate  * Macro to indicate that a thread's priority is about to be changed.
6277c478bd9Sstevel@tonic-gate  */
6287c478bd9Sstevel@tonic-gate #define	THREAD_WILLCHANGE_PRI(t, pri) {					\
6297c478bd9Sstevel@tonic-gate 	DTRACE_SCHED2(change__pri, kthread_t *, (t), pri_t, (pri));	\
6307c478bd9Sstevel@tonic-gate }
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate /*
6337c478bd9Sstevel@tonic-gate  * Macros to change thread state and the associated lock.
6347c478bd9Sstevel@tonic-gate  */
6357c478bd9Sstevel@tonic-gate #define	THREAD_SET_STATE(tp, state, lp) \
6367c478bd9Sstevel@tonic-gate 		((tp)->t_state = state, (tp)->t_lockp = lp)
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate /*
6397c478bd9Sstevel@tonic-gate  * Point it at the transition lock, which is always held.
6407c478bd9Sstevel@tonic-gate  * The previosly held lock is dropped.
6417c478bd9Sstevel@tonic-gate  */
6426a0b1217SPatrick Mooney #define	THREAD_TRANSITION(tp)	thread_transition(tp);
6437c478bd9Sstevel@tonic-gate /*
6447c478bd9Sstevel@tonic-gate  * Set the thread's lock to be the transition lock, without dropping
6457c478bd9Sstevel@tonic-gate  * previosly held lock.
6467c478bd9Sstevel@tonic-gate  */
6476a0b1217SPatrick Mooney #define	THREAD_TRANSITION_NOLOCK(tp)	((tp)->t_lockp = &transition_lock)
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate /*
6507c478bd9Sstevel@tonic-gate  * Put thread in run state, and set the lock pointer to the dispatcher queue
6517c478bd9Sstevel@tonic-gate  * lock pointer provided.  This lock should be held.
6527c478bd9Sstevel@tonic-gate  */
6537c478bd9Sstevel@tonic-gate #define	THREAD_RUN(tp, lp)	THREAD_SET_STATE(tp, TS_RUN, lp)
6547c478bd9Sstevel@tonic-gate 
655c97ad5cdSakolb /*
656c97ad5cdSakolb  * Put thread in wait state, and set the lock pointer to the wait queue
657c97ad5cdSakolb  * lock pointer provided.  This lock should be held.
658c97ad5cdSakolb  */
659c97ad5cdSakolb #define	THREAD_WAIT(tp, lp)	THREAD_SET_STATE(tp, TS_WAIT, lp)
660c97ad5cdSakolb 
6617c478bd9Sstevel@tonic-gate /*
6627c478bd9Sstevel@tonic-gate  * Put thread in run state, and set the lock pointer to the dispatcher queue
6637c478bd9Sstevel@tonic-gate  * lock pointer provided (i.e., the "swapped_lock").  This lock should be held.
6647c478bd9Sstevel@tonic-gate  */
6657c478bd9Sstevel@tonic-gate #define	THREAD_SWAP(tp, lp)	THREAD_SET_STATE(tp, TS_RUN, lp)
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate /*
6687c478bd9Sstevel@tonic-gate  * Put the thread in zombie state and set the lock pointer to NULL.
6697c478bd9Sstevel@tonic-gate  * The NULL will catch anything that tries to lock a zombie.
6707c478bd9Sstevel@tonic-gate  */
6717c478bd9Sstevel@tonic-gate #define	THREAD_ZOMB(tp)		THREAD_SET_STATE(tp, TS_ZOMB, NULL)
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate /*
6747c478bd9Sstevel@tonic-gate  * Set the thread into ONPROC state, and point the lock at the CPUs
6757c478bd9Sstevel@tonic-gate  * lock for the onproc thread(s).  This lock should be held, so the
6767c478bd9Sstevel@tonic-gate  * thread deoes not become unlocked, since these stores can be reordered.
6777c478bd9Sstevel@tonic-gate  */
6787c478bd9Sstevel@tonic-gate #define	THREAD_ONPROC(tp, cpu)	\
6797c478bd9Sstevel@tonic-gate 		THREAD_SET_STATE(tp, TS_ONPROC, &(cpu)->cpu_thread_lock)
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate /*
6827c478bd9Sstevel@tonic-gate  * Set the thread into the TS_SLEEP state, and set the lock pointer to
6837c478bd9Sstevel@tonic-gate  * to some sleep queue's lock.  The new lock should already be held.
6847c478bd9Sstevel@tonic-gate  */
6857c478bd9Sstevel@tonic-gate #define	THREAD_SLEEP(tp, lp)	{				\
6867c478bd9Sstevel@tonic-gate 			disp_lock_t	*tlp;			\
6877c478bd9Sstevel@tonic-gate 			tlp = (tp)->t_lockp;			\
6887c478bd9Sstevel@tonic-gate 			THREAD_SET_STATE(tp, TS_SLEEP, lp);	\
6897c478bd9Sstevel@tonic-gate 			disp_lock_exit_high(tlp);		\
6907c478bd9Sstevel@tonic-gate 			}
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate /*
6937c478bd9Sstevel@tonic-gate  * Interrupt threads are created in TS_FREE state, and their lock
6947c478bd9Sstevel@tonic-gate  * points at the associated CPU's lock.
6957c478bd9Sstevel@tonic-gate  */
6967c478bd9Sstevel@tonic-gate #define	THREAD_FREEINTR(tp, cpu)	\
6977c478bd9Sstevel@tonic-gate 		THREAD_SET_STATE(tp, TS_FREE, &(cpu)->cpu_thread_lock)
6987c478bd9Sstevel@tonic-gate 
699bff31d89SPhilippe Jung /* if tunable kmem_stackinfo is set, fill kthread stack with a pattern */
700bff31d89SPhilippe Jung #define	KMEM_STKINFO_PATTERN	0xbadcbadcbadcbadcULL
701bff31d89SPhilippe Jung 
702bff31d89SPhilippe Jung /*
703bff31d89SPhilippe Jung  * If tunable kmem_stackinfo is set, log the latest KMEM_LOG_STK_USAGE_SIZE
704bff31d89SPhilippe Jung  * dead kthreads that used their kernel stack the most.
705bff31d89SPhilippe Jung  */
706bff31d89SPhilippe Jung #define	KMEM_STKINFO_LOG_SIZE	16
707bff31d89SPhilippe Jung 
708bff31d89SPhilippe Jung /* kthread name (cmd/lwpid) string size in the stackinfo log */
709bff31d89SPhilippe Jung #define	KMEM_STKINFO_STR_SIZE	64
710bff31d89SPhilippe Jung 
711bff31d89SPhilippe Jung /*
712bff31d89SPhilippe Jung  * stackinfo logged data.
713bff31d89SPhilippe Jung  */
714bff31d89SPhilippe Jung typedef struct kmem_stkinfo {
715bff31d89SPhilippe Jung 	caddr_t	kthread;	/* kthread pointer */
716bff31d89SPhilippe Jung 	caddr_t	t_startpc;	/* where kthread started */
717bff31d89SPhilippe Jung 	caddr_t	start;		/* kthread stack start address */
718bff31d89SPhilippe Jung 	size_t	stksz;		/* kthread stack size */
719bff31d89SPhilippe Jung 	size_t	percent;	/* kthread stack high water mark */
720bff31d89SPhilippe Jung 	id_t	t_tid;		/* kthread id */
721bff31d89SPhilippe Jung 	char	cmd[KMEM_STKINFO_STR_SIZE];	/* kthread name (cmd/lwpid) */
722bff31d89SPhilippe Jung } kmem_stkinfo_t;
723bff31d89SPhilippe Jung 
7247c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
7257c478bd9Sstevel@tonic-gate }
7267c478bd9Sstevel@tonic-gate #endif
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate #endif /* _SYS_THREAD_H */
729