xref: /illumos-gate/usr/src/uts/common/sys/thread.h (revision cab61dfb)
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 */
1617c478bd9Sstevel@tonic-gate 	caddr_t t_tnf_tpdp;	/* Trace facility data pointer */
1624568bee7Strevtom 	struct _kcpc_ctx *t_cpc_ctx;	/* performance counter context */
1634568bee7Strevtom 	struct _kcpc_set *t_cpc_set;	/* set this thread has bound */
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	/*
1667c478bd9Sstevel@tonic-gate 	 * non swappable part of the lwp state.
1677c478bd9Sstevel@tonic-gate 	 */
1687c478bd9Sstevel@tonic-gate 	id_t		t_tid;		/* lwp's id */
1697c478bd9Sstevel@tonic-gate 	id_t		t_waitfor;	/* target lwp id in lwp_wait() */
1707c478bd9Sstevel@tonic-gate 	struct sigqueue	*t_sigqueue;	/* queue of siginfo structs */
1717c478bd9Sstevel@tonic-gate 	k_sigset_t	t_sig;		/* signals pending to this process */
1727c478bd9Sstevel@tonic-gate 	k_sigset_t	t_extsig;	/* signals sent from another contract */
1737c478bd9Sstevel@tonic-gate 	k_sigset_t	t_hold;		/* hold signal bit mask */
1743d729aecSJerry Jelinek 	k_sigset_t	t_sigwait;	/* sigtimedwait/sigfd accepting these */
1757c478bd9Sstevel@tonic-gate 	struct	_kthread *t_forw;	/* process's forward thread link */
1767c478bd9Sstevel@tonic-gate 	struct	_kthread *t_back;	/* process's backward thread link */
1777c478bd9Sstevel@tonic-gate 	struct	_kthread *t_thlink;	/* tid (lwpid) lookup hash link */
1787c478bd9Sstevel@tonic-gate 	klwp_t	*t_lwp;			/* thread's lwp pointer */
1797c478bd9Sstevel@tonic-gate 	struct	proc	*t_procp;	/* proc pointer */
1807c478bd9Sstevel@tonic-gate 	struct	t_audit_data *t_audit_data;	/* per thread audit data */
1817c478bd9Sstevel@tonic-gate 	struct	_kthread *t_next;	/* doubly linked list of all threads */
1827c478bd9Sstevel@tonic-gate 	struct	_kthread *t_prev;
1837c478bd9Sstevel@tonic-gate 	ushort_t t_whystop;		/* reason for stopping */
1847c478bd9Sstevel@tonic-gate 	ushort_t t_whatstop;		/* more detailed reason */
1857c478bd9Sstevel@tonic-gate 	int	t_dslot;		/* index in proc's thread directory */
1867c478bd9Sstevel@tonic-gate 	struct	pollstate *t_pollstate;	/* state used during poll(2) */
1877c478bd9Sstevel@tonic-gate 	struct	pollcache *t_pollcache;	/* to pass a pcache ptr by /dev/poll */
1887c478bd9Sstevel@tonic-gate 	struct	cred	*t_cred;	/* pointer to current cred */
1897c478bd9Sstevel@tonic-gate 	time_t	t_start;		/* start time, seconds since epoch */
1907c478bd9Sstevel@tonic-gate 	clock_t	t_lbolt;		/* lbolt at last clock_tick() */
1917c478bd9Sstevel@tonic-gate 	hrtime_t t_stoptime;		/* timestamp at stop() */
1927c478bd9Sstevel@tonic-gate 	uint_t	t_pctcpu;		/* %cpu at last clock_tick(), binary */
1937c478bd9Sstevel@tonic-gate 					/* point at right of high-order bit */
1947c478bd9Sstevel@tonic-gate 	short	t_sysnum;		/* system call number */
1957c478bd9Sstevel@tonic-gate 	kcondvar_t	t_delay_cv;
1967c478bd9Sstevel@tonic-gate 	kmutex_t	t_delay_lock;
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	/*
1997c478bd9Sstevel@tonic-gate 	 * Pointer to the dispatcher lock protecting t_state and state-related
2007c478bd9Sstevel@tonic-gate 	 * flags.  This pointer can change during waits on the lock, so
2017c478bd9Sstevel@tonic-gate 	 * it should be grabbed only by thread_lock().
2027c478bd9Sstevel@tonic-gate 	 */
2037c478bd9Sstevel@tonic-gate 	disp_lock_t	*t_lockp;	/* pointer to the dispatcher lock */
2046a0b1217SPatrick Mooney 	ushort_t	t_oldspl;	/* spl level before dispatcher locked */
2057c478bd9Sstevel@tonic-gate 	volatile char	t_pre_sys;	/* pre-syscall work needed */
2067c478bd9Sstevel@tonic-gate 	lock_t		t_lock_flush;	/* for lock_mutex_flush() impl */
2077c478bd9Sstevel@tonic-gate 	struct _disp	*t_disp_queue;	/* run queue for chosen CPU */
2087c478bd9Sstevel@tonic-gate 	clock_t		t_disp_time;	/* last time this thread was running */
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	/*
2117c478bd9Sstevel@tonic-gate 	 * Post-syscall / post-trap flags.
2126a0b1217SPatrick Mooney 	 *	No lock is required to set these.
2137c478bd9Sstevel@tonic-gate 	 *	These must be cleared only by the thread itself.
2147c478bd9Sstevel@tonic-gate 	 *
2157c478bd9Sstevel@tonic-gate 	 *	t_astflag indicates that some post-trap processing is required,
2167c478bd9Sstevel@tonic-gate 	 *		possibly a signal or a preemption.  The thread will not
2177c478bd9Sstevel@tonic-gate 	 *		return to user with this set.
2187c478bd9Sstevel@tonic-gate 	 *	t_post_sys indicates that some unusualy post-system call
2197c478bd9Sstevel@tonic-gate 	 *		handling is required, such as an error or tracing.
2207c478bd9Sstevel@tonic-gate 	 *	t_sig_check indicates that some condition in ISSIG() must be
2216a0b1217SPatrick Mooney 	 *		checked, but doesn't prevent returning to user.
2227c478bd9Sstevel@tonic-gate 	 *	t_post_sys_ast is a way of checking whether any of these three
2237c478bd9Sstevel@tonic-gate 	 *		flags are set.
2247c478bd9Sstevel@tonic-gate 	 */
2257c478bd9Sstevel@tonic-gate 	union __tu {
2267c478bd9Sstevel@tonic-gate 		struct __ts {
2277c478bd9Sstevel@tonic-gate 			volatile char	_t_astflag;	/* AST requested */
2287c478bd9Sstevel@tonic-gate 			volatile char	_t_sig_check;	/* ISSIG required */
2297c478bd9Sstevel@tonic-gate 			volatile char	_t_post_sys;	/* post_syscall req */
2307c478bd9Sstevel@tonic-gate 			volatile char	_t_trapret;	/* call CL_TRAPRET */
2317c478bd9Sstevel@tonic-gate 		} _ts;
2327c478bd9Sstevel@tonic-gate 		volatile int	_t_post_sys_ast;	/* OR of these flags */
2337c478bd9Sstevel@tonic-gate 	} _tu;
2347c478bd9Sstevel@tonic-gate #define	t_astflag	_tu._ts._t_astflag
2357c478bd9Sstevel@tonic-gate #define	t_sig_check	_tu._ts._t_sig_check
2367c478bd9Sstevel@tonic-gate #define	t_post_sys	_tu._ts._t_post_sys
2377c478bd9Sstevel@tonic-gate #define	t_trapret	_tu._ts._t_trapret
2387c478bd9Sstevel@tonic-gate #define	t_post_sys_ast	_tu._t_post_sys_ast
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	/*
2417c478bd9Sstevel@tonic-gate 	 * Real time microstate profiling.
2427c478bd9Sstevel@tonic-gate 	 */
2437c478bd9Sstevel@tonic-gate 					/* possible 4-byte filler */
2447c478bd9Sstevel@tonic-gate 	hrtime_t t_waitrq;		/* timestamp for run queue wait time */
2457c478bd9Sstevel@tonic-gate 	int	t_mstate;		/* current microstate */
2467c478bd9Sstevel@tonic-gate 	struct rprof {
2477c478bd9Sstevel@tonic-gate 		int	rp_anystate;		/* set if any state non-zero */
2487c478bd9Sstevel@tonic-gate 		uint_t	rp_state[NMSTATES];	/* mstate profiling counts */
2497c478bd9Sstevel@tonic-gate 	} *t_rprof;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	/*
2527c478bd9Sstevel@tonic-gate 	 * There is a turnstile inserted into the list below for
2537c478bd9Sstevel@tonic-gate 	 * every priority inverted synchronization object that
2547c478bd9Sstevel@tonic-gate 	 * this thread holds.
2557c478bd9Sstevel@tonic-gate 	 */
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	struct turnstile *t_prioinv;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	/*
2607c478bd9Sstevel@tonic-gate 	 * Pointer to the turnstile attached to the synchronization
2617c478bd9Sstevel@tonic-gate 	 * object where this thread is blocked.
2627c478bd9Sstevel@tonic-gate 	 */
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	struct turnstile *t_ts;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	/*
2677c478bd9Sstevel@tonic-gate 	 * kernel thread specific data
2687c478bd9Sstevel@tonic-gate 	 *	Borrowed from userland implementation of POSIX tsd
2697c478bd9Sstevel@tonic-gate 	 */
2707c478bd9Sstevel@tonic-gate 	struct tsd_thread {
2717c478bd9Sstevel@tonic-gate 		struct tsd_thread *ts_next;	/* threads with TSD */
2727c478bd9Sstevel@tonic-gate 		struct tsd_thread *ts_prev;	/* threads with TSD */
2737c478bd9Sstevel@tonic-gate 		uint_t		  ts_nkeys;	/* entries in value array */
2747c478bd9Sstevel@tonic-gate 		void		  **ts_value;	/* array of value/key */
2757c478bd9Sstevel@tonic-gate 	} *t_tsd;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	clock_t		t_stime;	/* time stamp used by the swapper */
2787c478bd9Sstevel@tonic-gate 	struct door_data *t_door;	/* door invocation data */
2797c478bd9Sstevel@tonic-gate 	kmutex_t	*t_plockp;	/* pointer to process's p_lock */
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	struct sc_shared *t_schedctl;	/* scheduler activations shared data */
2827c478bd9Sstevel@tonic-gate 	uintptr_t	t_sc_uaddr;	/* user-level address of shared data */
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	struct cpupart	*t_cpupart;	/* partition containing thread */
2857c478bd9Sstevel@tonic-gate 	int		t_bind_pset;	/* processor set binding */
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	struct copyops	*t_copyops;	/* copy in/out ops vector */
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	caddr_t		t_stkbase;	/* base of the the stack */
2907c478bd9Sstevel@tonic-gate 	struct page	*t_red_pp;	/* if non-NULL, redzone is mapped */
2917c478bd9Sstevel@tonic-gate 
292236317e1SRoger A. Faulkner 	afd_t		t_activefd;	/* active file descriptor table */
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	struct _kthread	*t_priforw;	/* sleepq per-priority sublist */
2957c478bd9Sstevel@tonic-gate 	struct _kthread	*t_priback;
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	struct sleepq	*t_sleepq;	/* sleep queue thread is waiting on */
298843e1988Sjohnlev 	struct panic_trap_info *t_panic_trap;	/* saved data from fatal trap */
2997c478bd9Sstevel@tonic-gate 	int		*t_lgrp_affinity;	/* lgroup affinity */
3007c478bd9Sstevel@tonic-gate 	struct upimutex	*t_upimutex;	/* list of upimutexes owned by thread */
3017c478bd9Sstevel@tonic-gate 	uint32_t	t_nupinest;	/* number of nested held upi mutexes */
3027c478bd9Sstevel@tonic-gate 	struct kproject *t_proj;	/* project containing this thread */
3037c478bd9Sstevel@tonic-gate 	uint8_t		t_unpark;	/* modified holding t_delay_lock */
3047c478bd9Sstevel@tonic-gate 	uint8_t		t_release;	/* lwp_release() waked up the thread */
3057c478bd9Sstevel@tonic-gate 	uint8_t		t_hatdepth;	/* depth of recursive hat_memloads */
306551bc2a6Smrj 	uint8_t		t_xpvcntr;	/* see xen_block_migrate() */
3077c478bd9Sstevel@tonic-gate 	kcondvar_t	t_joincv;	/* cv used to wait for thread exit */
3087c478bd9Sstevel@tonic-gate 	void		*t_taskq;	/* for threads belonging to taskq */
3097c478bd9Sstevel@tonic-gate 	hrtime_t	t_anttime;	/* most recent time anticipatory load */
3107c478bd9Sstevel@tonic-gate 					/*	was added to an lgroup's load */
3117c478bd9Sstevel@tonic-gate 					/*	on this thread's behalf */
3127c478bd9Sstevel@tonic-gate 	char		*t_pdmsg;	/* privilege debugging message */
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	uint_t		t_predcache;	/* DTrace predicate cache */
3157c478bd9Sstevel@tonic-gate 	hrtime_t	t_dtrace_vtime;	/* DTrace virtual time */
3167c478bd9Sstevel@tonic-gate 	hrtime_t	t_dtrace_start;	/* DTrace slice start time */
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	uint8_t		t_dtrace_stop;	/* indicates a DTrace-desired stop */
3197c478bd9Sstevel@tonic-gate 	uint8_t		t_dtrace_sig;	/* signal sent via DTrace's raise() */
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	union __tdu {
3227c478bd9Sstevel@tonic-gate 		struct __tds {
3237c478bd9Sstevel@tonic-gate 			uint8_t	_t_dtrace_on;	/* hit a fasttrap tracepoint */
3247c478bd9Sstevel@tonic-gate 			uint8_t	_t_dtrace_step;	/* about to return to kernel */
3257c478bd9Sstevel@tonic-gate 			uint8_t	_t_dtrace_ret;	/* handling a return probe */
3267c478bd9Sstevel@tonic-gate 			uint8_t	_t_dtrace_ast;	/* saved ast flag */
3277c478bd9Sstevel@tonic-gate #ifdef __amd64
3287c478bd9Sstevel@tonic-gate 			uint8_t	_t_dtrace_reg;	/* modified register */
3297c478bd9Sstevel@tonic-gate #endif
3307c478bd9Sstevel@tonic-gate 		} _tds;
3317c478bd9Sstevel@tonic-gate 		ulong_t	_t_dtrace_ft;		/* bitwise or of these flags */
3327c478bd9Sstevel@tonic-gate 	} _tdu;
3337c478bd9Sstevel@tonic-gate #define	t_dtrace_ft	_tdu._t_dtrace_ft
3347c478bd9Sstevel@tonic-gate #define	t_dtrace_on	_tdu._tds._t_dtrace_on
3357c478bd9Sstevel@tonic-gate #define	t_dtrace_step	_tdu._tds._t_dtrace_step
3367c478bd9Sstevel@tonic-gate #define	t_dtrace_ret	_tdu._tds._t_dtrace_ret
3377c478bd9Sstevel@tonic-gate #define	t_dtrace_ast	_tdu._tds._t_dtrace_ast
3387c478bd9Sstevel@tonic-gate #ifdef __amd64
3397c478bd9Sstevel@tonic-gate #define	t_dtrace_reg	_tdu._tds._t_dtrace_reg
3407c478bd9Sstevel@tonic-gate #endif
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	uintptr_t	t_dtrace_pc;	/* DTrace saved pc from fasttrap */
3437c478bd9Sstevel@tonic-gate 	uintptr_t	t_dtrace_npc;	/* DTrace next pc from fasttrap */
3447c478bd9Sstevel@tonic-gate 	uintptr_t	t_dtrace_scrpc;	/* DTrace per-thread scratch location */
3457c478bd9Sstevel@tonic-gate 	uintptr_t	t_dtrace_astpc;	/* DTrace return sequence location */
3467c478bd9Sstevel@tonic-gate #ifdef __amd64
3477c478bd9Sstevel@tonic-gate 	uint64_t	t_dtrace_regv;	/* DTrace saved reg from fasttrap */
3483ce2fcdcSRobert Mustacchi 	uint64_t	t_useracc;	/* SMAP state saved across swtch() */
3497c478bd9Sstevel@tonic-gate #endif
3507c478bd9Sstevel@tonic-gate 	hrtime_t	t_hrtime;	/* high-res last time on cpu */
3515a64ecfaStrevtom 	kmutex_t	t_ctx_lock;	/* protects t_ctx in removectx() */
352c97ad5cdSakolb 	struct waitq	*t_waitq;	/* wait queue */
353454ab202SMadhavan Venkataraman 	kmutex_t	t_wait_mutex;	/* used in CV wait functions */
354ab618543SJohn Levon 
355ab618543SJohn Levon 	char		*t_name;	/* thread name */
356455e370cSJohn Levon 
357c3377ee9SJohn Levon 	uint64_t	t_unsafe;	/* unsafe to run with SMT VCPU thread */
3587c478bd9Sstevel@tonic-gate } kthread_t;
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate /*
3617c478bd9Sstevel@tonic-gate  * Thread flag (t_flag) definitions.
3627c478bd9Sstevel@tonic-gate  *	These flags must be changed only for the current thread,
3636a0b1217SPatrick Mooney  *	and not during preemption code, since the code being
3647c478bd9Sstevel@tonic-gate  *	preempted could be modifying the flags.
3657c478bd9Sstevel@tonic-gate  *
3667c478bd9Sstevel@tonic-gate  *	For the most part these flags do not need locking.
3677c478bd9Sstevel@tonic-gate  *	The following flags will only be changed while the thread_lock is held,
3687c478bd9Sstevel@tonic-gate  *	to give assurrance that they are consistent with t_state:
3697c478bd9Sstevel@tonic-gate  *		T_WAKEABLE
3707c478bd9Sstevel@tonic-gate  */
3717c478bd9Sstevel@tonic-gate #define	T_INTR_THREAD	0x0001	/* thread is an interrupt thread */
3727c478bd9Sstevel@tonic-gate #define	T_WAKEABLE	0x0002	/* thread is blocked, signals enabled */
3737c478bd9Sstevel@tonic-gate #define	T_TOMASK	0x0004	/* use lwp_sigoldmask on return from signal */
3747c478bd9Sstevel@tonic-gate #define	T_TALLOCSTK	0x0008  /* thread structure allocated from stk */
375c4978b50Sraf #define	T_FORKALL	0x0010	/* thread was cloned by forkall() */
3767c478bd9Sstevel@tonic-gate #define	T_WOULDBLOCK	0x0020	/* for lockfs */
3777c478bd9Sstevel@tonic-gate #define	T_DONTBLOCK	0x0040	/* for lockfs */
3787c478bd9Sstevel@tonic-gate #define	T_DONTPEND	0x0080	/* for lockfs */
3797c478bd9Sstevel@tonic-gate #define	T_SYS_PROF	0x0100	/* profiling on for duration of system call */
3807c478bd9Sstevel@tonic-gate #define	T_WAITCVSEM	0x0200	/* waiting for a lwp_cv or lwp_sema on sleepq */
3817c478bd9Sstevel@tonic-gate #define	T_WATCHPT	0x0400	/* thread undergoing a watchpoint emulation */
3827c478bd9Sstevel@tonic-gate #define	T_PANIC		0x0800	/* thread initiated a system panic */
383d32efdadSJonathan Adams #define	T_LWPREUSE	0x1000	/* stack and LWP can be reused */
3848b464eb8Smec #define	T_CAPTURING	0x2000	/* thread is in page capture logic */
3858d186f16Sraf #define	T_VFPARENT	0x4000	/* thread is vfork parent, must call vfwait */
3865cc25c11Smb #define	T_DONTDTRACE	0x8000  /* disable DTrace probes */
3872fc9ab6eSJerry Jelinek #define	T_KFPU		0x10000	/* kernel FPU active */
388f6ef4223SJoshua M. Clulow #define	T_PUSHPAGE	0x20000	/* this thread may be assisting pageout */
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate /*
3917c478bd9Sstevel@tonic-gate  * Flags in t_proc_flag.
3927c478bd9Sstevel@tonic-gate  *	These flags must be modified only when holding the p_lock
3937c478bd9Sstevel@tonic-gate  *	for the associated process.
3947c478bd9Sstevel@tonic-gate  */
3957c478bd9Sstevel@tonic-gate #define	TP_DAEMON	0x0001	/* this is an LWP_DAEMON lwp */
3967c478bd9Sstevel@tonic-gate #define	TP_HOLDLWP	0x0002	/* hold thread's lwp */
3977c478bd9Sstevel@tonic-gate #define	TP_TWAIT	0x0004	/* wait to be freed by lwp_wait() */
3987c478bd9Sstevel@tonic-gate #define	TP_LWPEXIT	0x0008	/* lwp has exited */
3997c478bd9Sstevel@tonic-gate #define	TP_PRSTOP	0x0010	/* thread is being stopped via /proc */
4007c478bd9Sstevel@tonic-gate #define	TP_CHKPT	0x0020	/* thread is being stopped via CPR checkpoint */
4017c478bd9Sstevel@tonic-gate #define	TP_EXITLWP	0x0040	/* terminate this lwp */
4027c478bd9Sstevel@tonic-gate #define	TP_PRVSTOP	0x0080	/* thread is virtually stopped via /proc */
4037c478bd9Sstevel@tonic-gate #define	TP_MSACCT	0x0100	/* collect micro-state accounting information */
4047c478bd9Sstevel@tonic-gate #define	TP_STOPPING	0x0200	/* thread is executing stop() */
4057c478bd9Sstevel@tonic-gate #define	TP_WATCHPT	0x0400	/* process has watchpoints in effect */
4067c478bd9Sstevel@tonic-gate #define	TP_PAUSE	0x0800	/* process is being stopped via pauselwps() */
4077c478bd9Sstevel@tonic-gate #define	TP_CHANGEBIND	0x1000	/* thread has a new cpu/cpupart binding */
4087c478bd9Sstevel@tonic-gate #define	TP_ZTHREAD	0x2000	/* this is a kernel thread for a zone */
4097c478bd9Sstevel@tonic-gate #define	TP_WATCHSTOP	0x4000	/* thread is stopping via holdwatch() */
4104c819f48SJerry Jelinek #define	TP_KTHREAD	0x8000	/* in-kernel worker thread for a process */
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate /*
4137c478bd9Sstevel@tonic-gate  * Thread scheduler flag (t_schedflag) definitions.
4147c478bd9Sstevel@tonic-gate  *	The thread must be locked via thread_lock() or equiv. to change these.
4157c478bd9Sstevel@tonic-gate  */
4167c478bd9Sstevel@tonic-gate #define	TS_LOAD		0x0001	/* thread is in memory */
4177c478bd9Sstevel@tonic-gate #define	TS_DONT_SWAP	0x0002	/* thread/lwp should not be swapped */
4187c478bd9Sstevel@tonic-gate #define	TS_SWAPENQ	0x0004	/* swap thread when it reaches a safe point */
4197c478bd9Sstevel@tonic-gate #define	TS_ON_SWAPQ	0x0008	/* thread is on the swap queue */
4207c478bd9Sstevel@tonic-gate #define	TS_SIGNALLED	0x0010	/* thread was awakened by cv_signal() */
421c97ad5cdSakolb #define	TS_PROJWAITQ	0x0020	/* thread is on its project's waitq */
422c97ad5cdSakolb #define	TS_ZONEWAITQ	0x0040	/* thread is on its zone's waitq */
423455e370cSJohn Levon #define	TS_VCPU		0x0080	/* thread will enter guest context */
4247c478bd9Sstevel@tonic-gate #define	TS_CSTART	0x0100	/* setrun() by continuelwps() */
4257c478bd9Sstevel@tonic-gate #define	TS_UNPAUSE	0x0200	/* setrun() by unpauselwps() */
4267c478bd9Sstevel@tonic-gate #define	TS_XSTART	0x0400	/* setrun() by SIGCONT */
4277c478bd9Sstevel@tonic-gate #define	TS_PSTART	0x0800	/* setrun() by /proc */
4287c478bd9Sstevel@tonic-gate #define	TS_RESUME	0x1000	/* setrun() by CPR resume process */
4297c478bd9Sstevel@tonic-gate #define	TS_CREATE	0x2000	/* setrun() by syslwp_create() */
4307c478bd9Sstevel@tonic-gate #define	TS_RUNQMATCH	0x4000	/* exact run queue balancing by setbackdq() */
4317c478bd9Sstevel@tonic-gate #define	TS_ALLSTART	\
4327c478bd9Sstevel@tonic-gate 	(TS_CSTART|TS_UNPAUSE|TS_XSTART|TS_PSTART|TS_RESUME|TS_CREATE)
433c97ad5cdSakolb #define	TS_ANYWAITQ	(TS_PROJWAITQ|TS_ZONEWAITQ)
4347c478bd9Sstevel@tonic-gate 
4350b70c467Sakolb /*
4360b70c467Sakolb  * Thread binding types
4370b70c467Sakolb  */
4380b70c467Sakolb #define	TB_ALLHARD	0
4390b70c467Sakolb #define	TB_CPU_SOFT	0x01		/* soft binding to CPU */
4400b70c467Sakolb #define	TB_PSET_SOFT	0x02		/* soft binding to pset */
4410b70c467Sakolb 
4420b70c467Sakolb #define	TB_CPU_SOFT_SET(t)		((t)->t_bindflag |= TB_CPU_SOFT)
4430b70c467Sakolb #define	TB_CPU_HARD_SET(t)		((t)->t_bindflag &= ~TB_CPU_SOFT)
4440b70c467Sakolb #define	TB_PSET_SOFT_SET(t)		((t)->t_bindflag |= TB_PSET_SOFT)
4450b70c467Sakolb #define	TB_PSET_HARD_SET(t)		((t)->t_bindflag &= ~TB_PSET_SOFT)
4460b70c467Sakolb #define	TB_CPU_IS_SOFT(t)		((t)->t_bindflag & TB_CPU_SOFT)
4470b70c467Sakolb #define	TB_CPU_IS_HARD(t)		(!TB_CPU_IS_SOFT(t))
4480b70c467Sakolb #define	TB_PSET_IS_SOFT(t)		((t)->t_bindflag & TB_PSET_SOFT)
4490b70c467Sakolb 
4507c478bd9Sstevel@tonic-gate /*
4517c478bd9Sstevel@tonic-gate  * No locking needed for AST field.
4527c478bd9Sstevel@tonic-gate  */
4537c478bd9Sstevel@tonic-gate #define	aston(t)		((t)->t_astflag = 1)
4547c478bd9Sstevel@tonic-gate #define	astoff(t)		((t)->t_astflag = 0)
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate /* True if thread is stopped on an event of interest */
4577c478bd9Sstevel@tonic-gate #define	ISTOPPED(t) ((t)->t_state == TS_STOPPED && \
4587c478bd9Sstevel@tonic-gate 			!((t)->t_schedflag & TS_PSTART))
4597c478bd9Sstevel@tonic-gate 
460c97ad5cdSakolb /* True if thread is asleep and wakeable */
461c97ad5cdSakolb #define	ISWAKEABLE(t) (((t)->t_state == TS_SLEEP && \
462c97ad5cdSakolb 			((t)->t_flag & T_WAKEABLE)))
463c97ad5cdSakolb 
464c97ad5cdSakolb /* True if thread is on the wait queue */
465c97ad5cdSakolb #define	ISWAITING(t) ((t)->t_state == TS_WAIT)
466c97ad5cdSakolb 
4677c478bd9Sstevel@tonic-gate /* similar to ISTOPPED except the event of interest is CPR */
4687c478bd9Sstevel@tonic-gate #define	CPR_ISTOPPED(t) ((t)->t_state == TS_STOPPED && \
4697c478bd9Sstevel@tonic-gate 			!((t)->t_schedflag & TS_RESUME))
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate /*
4727c478bd9Sstevel@tonic-gate  * True if thread is virtually stopped (is or was asleep in
4737c478bd9Sstevel@tonic-gate  * one of the lwp_*() system calls and marked to stop by /proc.)
4747c478bd9Sstevel@tonic-gate  */
4757c478bd9Sstevel@tonic-gate #define	VSTOPPED(t)	((t)->t_proc_flag & TP_PRVSTOP)
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate /* similar to VSTOPPED except the point of interest is CPR */
4787c478bd9Sstevel@tonic-gate #define	CPR_VSTOPPED(t)				\
4797c478bd9Sstevel@tonic-gate 	((t)->t_state == TS_SLEEP &&		\
4807c478bd9Sstevel@tonic-gate 	(t)->t_wchan0 != NULL &&		\
4817c478bd9Sstevel@tonic-gate 	((t)->t_flag & T_WAKEABLE) &&		\
4827c478bd9Sstevel@tonic-gate 	((t)->t_proc_flag & TP_CHKPT))
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate /* True if thread has been stopped by hold*() or was created stopped */
4857c478bd9Sstevel@tonic-gate #define	SUSPENDED(t) ((t)->t_state == TS_STOPPED && \
4867c478bd9Sstevel@tonic-gate 	((t)->t_schedflag & (TS_CSTART|TS_UNPAUSE)) != (TS_CSTART|TS_UNPAUSE))
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate /* True if thread possesses an inherited priority */
4897c478bd9Sstevel@tonic-gate #define	INHERITED(t)	((t)->t_epri != 0)
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate /* The dispatch priority of a thread */
4927c478bd9Sstevel@tonic-gate #define	DISP_PRIO(t) ((t)->t_epri > (t)->t_pri ? (t)->t_epri : (t)->t_pri)
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate /* The assigned priority of a thread */
4957c478bd9Sstevel@tonic-gate #define	ASSIGNED_PRIO(t)	((t)->t_pri)
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate /*
4987c478bd9Sstevel@tonic-gate  * Macros to determine whether a thread can be swapped.
4997c478bd9Sstevel@tonic-gate  * If t_lock is held, the thread is either on a processor or being swapped.
5007c478bd9Sstevel@tonic-gate  */
5017c478bd9Sstevel@tonic-gate #define	SWAP_OK(t)	(!LOCK_HELD(&(t)->t_lock))
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate /*
5047c478bd9Sstevel@tonic-gate  * proctot(x)
5057c478bd9Sstevel@tonic-gate  *	convert a proc pointer to a thread pointer. this only works with
5067c478bd9Sstevel@tonic-gate  *	procs that have only one lwp.
5077c478bd9Sstevel@tonic-gate  *
5087c478bd9Sstevel@tonic-gate  * proctolwp(x)
5097c478bd9Sstevel@tonic-gate  *	convert a proc pointer to a lwp pointer. this only works with
5107c478bd9Sstevel@tonic-gate  *	procs that have only one lwp.
5117c478bd9Sstevel@tonic-gate  *
5127c478bd9Sstevel@tonic-gate  * ttolwp(x)
5137c478bd9Sstevel@tonic-gate  *	convert a thread pointer to its lwp pointer.
5147c478bd9Sstevel@tonic-gate  *
5157c478bd9Sstevel@tonic-gate  * ttoproc(x)
5167c478bd9Sstevel@tonic-gate  *	convert a thread pointer to its proc pointer.
5177c478bd9Sstevel@tonic-gate  *
5187c478bd9Sstevel@tonic-gate  * ttoproj(x)
5196a0b1217SPatrick Mooney  *	convert a thread pointer to its project pointer.
5207c478bd9Sstevel@tonic-gate  *
521c97ad5cdSakolb  * ttozone(x)
5226a0b1217SPatrick Mooney  *	convert a thread pointer to its zone pointer.
523c97ad5cdSakolb  *
5247c478bd9Sstevel@tonic-gate  * lwptot(x)
5257c478bd9Sstevel@tonic-gate  *	convert a lwp pointer to its thread pointer.
5267c478bd9Sstevel@tonic-gate  *
5277c478bd9Sstevel@tonic-gate  * lwptoproc(x)
5287c478bd9Sstevel@tonic-gate  *	convert a lwp to its proc pointer.
5297c478bd9Sstevel@tonic-gate  */
5307c478bd9Sstevel@tonic-gate #define	proctot(x)	((x)->p_tlist)
5317c478bd9Sstevel@tonic-gate #define	proctolwp(x)	((x)->p_tlist->t_lwp)
5327c478bd9Sstevel@tonic-gate #define	ttolwp(x)	((x)->t_lwp)
5337c478bd9Sstevel@tonic-gate #define	ttoproc(x)	((x)->t_procp)
5347c478bd9Sstevel@tonic-gate #define	ttoproj(x)	((x)->t_proj)
535c97ad5cdSakolb #define	ttozone(x)	((x)->t_procp->p_zone)
5367c478bd9Sstevel@tonic-gate #define	lwptot(x)	((x)->lwp_thread)
5377c478bd9Sstevel@tonic-gate #define	lwptoproc(x)	((x)->lwp_procp)
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate #define	t_pc		t_pcb.val[0]
5407c478bd9Sstevel@tonic-gate #define	t_sp		t_pcb.val[1]
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate #ifdef	_KERNEL
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate extern	kthread_t	*threadp(void);	/* inline, returns thread pointer */
5457c478bd9Sstevel@tonic-gate #define	curthread	(threadp())		/* current thread pointer */
5467c478bd9Sstevel@tonic-gate #define	curproc		(ttoproc(curthread))	/* current process pointer */
5477c478bd9Sstevel@tonic-gate #define	curproj		(ttoproj(curthread))	/* current project pointer */
548c97ad5cdSakolb #define	curzone		(curproc->p_zone)	/* current zone pointer */
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate extern	struct _kthread	t0;		/* the scheduler thread */
5517c478bd9Sstevel@tonic-gate extern	kmutex_t	pidlock;	/* global process lock */
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate /*
5542850d85bSmv  * thread_free_lock is used by the tick accounting thread to keep a thread
5557c478bd9Sstevel@tonic-gate  * from being freed while it is being examined.
55691f917c7SMadhavan Venkataraman  *
55791f917c7SMadhavan Venkataraman  * Thread structures are 32-byte aligned structures. That is why we use the
55891f917c7SMadhavan Venkataraman  * following formula.
5597c478bd9Sstevel@tonic-gate  */
56091f917c7SMadhavan Venkataraman #define	THREAD_FREE_BITS	10
56191f917c7SMadhavan Venkataraman #define	THREAD_FREE_NUM		(1 << THREAD_FREE_BITS)
5622850d85bSmv #define	THREAD_FREE_MASK	(THREAD_FREE_NUM - 1)
56391f917c7SMadhavan Venkataraman #define	THREAD_FREE_1		PTR24_LSB
56491f917c7SMadhavan Venkataraman #define	THREAD_FREE_2		(PTR24_LSB + THREAD_FREE_BITS)
56591f917c7SMadhavan Venkataraman #define	THREAD_FREE_SHIFT(t)	\
56691f917c7SMadhavan Venkataraman 	(((ulong_t)(t) >> THREAD_FREE_1) ^ ((ulong_t)(t) >> THREAD_FREE_2))
5672850d85bSmv #define	THREAD_FREE_HASH(t)	(THREAD_FREE_SHIFT(t) & THREAD_FREE_MASK)
5682850d85bSmv 
5692850d85bSmv typedef struct thread_free_lock {
5702850d85bSmv 	kmutex_t	tf_lock;
5712850d85bSmv 	uchar_t		tf_pad[64 - sizeof (kmutex_t)];
5722850d85bSmv } thread_free_lock_t;
5732850d85bSmv 
5742850d85bSmv extern void	thread_free_prevent(kthread_t *);
5752850d85bSmv extern void	thread_free_allow(kthread_t *);
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate /*
5787c478bd9Sstevel@tonic-gate  * Routines to change the priority and effective priority
5797c478bd9Sstevel@tonic-gate  * of a thread-locked thread, whatever its state.
5807c478bd9Sstevel@tonic-gate  */
5817c478bd9Sstevel@tonic-gate extern int	thread_change_pri(kthread_t *t, pri_t disp_pri, int front);
5827c478bd9Sstevel@tonic-gate extern void	thread_change_epri(kthread_t *t, pri_t disp_pri);
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate /*
5857c478bd9Sstevel@tonic-gate  * Routines that manipulate the dispatcher lock for the thread.
5867c478bd9Sstevel@tonic-gate  * The locking heirarchy is as follows:
5877c478bd9Sstevel@tonic-gate  *	cpu_lock > sleepq locks > run queue locks
5887c478bd9Sstevel@tonic-gate  */
5897c478bd9Sstevel@tonic-gate void	thread_transition(kthread_t *); /* move to transition lock */
5907c478bd9Sstevel@tonic-gate void	thread_stop(kthread_t *);	/* move to stop lock */
5917c478bd9Sstevel@tonic-gate void	thread_lock(kthread_t *);	/* lock thread and its queue */
5927c478bd9Sstevel@tonic-gate void	thread_lock_high(kthread_t *);	/* lock thread and its queue */
5937c478bd9Sstevel@tonic-gate void	thread_onproc(kthread_t *, struct cpu *); /* set onproc state lock */
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate #define	thread_unlock(t)		disp_lock_exit((t)->t_lockp)
5967c478bd9Sstevel@tonic-gate #define	thread_unlock_high(t)		disp_lock_exit_high((t)->t_lockp)
5977c478bd9Sstevel@tonic-gate #define	thread_unlock_nopreempt(t)	disp_lock_exit_nopreempt((t)->t_lockp)
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate #define	THREAD_LOCK_HELD(t)	(DISP_LOCK_HELD((t)->t_lockp))
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate extern disp_lock_t transition_lock;	/* lock protecting transiting threads */
6027c478bd9Sstevel@tonic-gate extern disp_lock_t stop_lock;		/* lock protecting stopped threads */
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate caddr_t	thread_stk_init(caddr_t);	/* init thread stack */
6057c478bd9Sstevel@tonic-gate 
606ab618543SJohn Levon int thread_setname(kthread_t *, const char *);
607ab618543SJohn Levon int thread_vsetname(kthread_t *, const char *, ...);
608ab618543SJohn Levon 
6090b70c467Sakolb extern int default_binding_mode;
610*cab61dfbSBryan Cantrill extern int default_stksize;
6110b70c467Sakolb 
6127c478bd9Sstevel@tonic-gate #endif	/* _KERNEL */
6137c478bd9Sstevel@tonic-gate 
614ab618543SJohn Levon #define	THREAD_NAME_MAX	32	/* includes terminating NUL */
615ab618543SJohn Levon 
6167c478bd9Sstevel@tonic-gate /*
6177c478bd9Sstevel@tonic-gate  * Macro to change a thread's priority.
6187c478bd9Sstevel@tonic-gate  */
6197c478bd9Sstevel@tonic-gate #define	THREAD_CHANGE_PRI(t, pri) {					\
6207c478bd9Sstevel@tonic-gate 	pri_t __new_pri = (pri);					\
6217c478bd9Sstevel@tonic-gate 	DTRACE_SCHED2(change__pri, kthread_t *, (t), pri_t, __new_pri);	\
6227c478bd9Sstevel@tonic-gate 	(t)->t_pri = __new_pri;						\
623d4204c85Sraf 	schedctl_set_cidpri(t);						\
6247c478bd9Sstevel@tonic-gate }
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate /*
6277c478bd9Sstevel@tonic-gate  * Macro to indicate that a thread's priority is about to be changed.
6287c478bd9Sstevel@tonic-gate  */
6297c478bd9Sstevel@tonic-gate #define	THREAD_WILLCHANGE_PRI(t, pri) {					\
6307c478bd9Sstevel@tonic-gate 	DTRACE_SCHED2(change__pri, kthread_t *, (t), pri_t, (pri));	\
6317c478bd9Sstevel@tonic-gate }
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate /*
6347c478bd9Sstevel@tonic-gate  * Macros to change thread state and the associated lock.
6357c478bd9Sstevel@tonic-gate  */
6367c478bd9Sstevel@tonic-gate #define	THREAD_SET_STATE(tp, state, lp) \
6377c478bd9Sstevel@tonic-gate 		((tp)->t_state = state, (tp)->t_lockp = lp)
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate /*
6407c478bd9Sstevel@tonic-gate  * Point it at the transition lock, which is always held.
6417c478bd9Sstevel@tonic-gate  * The previosly held lock is dropped.
6427c478bd9Sstevel@tonic-gate  */
6436a0b1217SPatrick Mooney #define	THREAD_TRANSITION(tp)	thread_transition(tp);
6447c478bd9Sstevel@tonic-gate /*
6457c478bd9Sstevel@tonic-gate  * Set the thread's lock to be the transition lock, without dropping
6467c478bd9Sstevel@tonic-gate  * previosly held lock.
6477c478bd9Sstevel@tonic-gate  */
6486a0b1217SPatrick Mooney #define	THREAD_TRANSITION_NOLOCK(tp)	((tp)->t_lockp = &transition_lock)
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate /*
6517c478bd9Sstevel@tonic-gate  * Put thread in run state, and set the lock pointer to the dispatcher queue
6527c478bd9Sstevel@tonic-gate  * lock pointer provided.  This lock should be held.
6537c478bd9Sstevel@tonic-gate  */
6547c478bd9Sstevel@tonic-gate #define	THREAD_RUN(tp, lp)	THREAD_SET_STATE(tp, TS_RUN, lp)
6557c478bd9Sstevel@tonic-gate 
656c97ad5cdSakolb /*
657c97ad5cdSakolb  * Put thread in wait state, and set the lock pointer to the wait queue
658c97ad5cdSakolb  * lock pointer provided.  This lock should be held.
659c97ad5cdSakolb  */
660c97ad5cdSakolb #define	THREAD_WAIT(tp, lp)	THREAD_SET_STATE(tp, TS_WAIT, lp)
661c97ad5cdSakolb 
6627c478bd9Sstevel@tonic-gate /*
6637c478bd9Sstevel@tonic-gate  * Put thread in run state, and set the lock pointer to the dispatcher queue
6647c478bd9Sstevel@tonic-gate  * lock pointer provided (i.e., the "swapped_lock").  This lock should be held.
6657c478bd9Sstevel@tonic-gate  */
6667c478bd9Sstevel@tonic-gate #define	THREAD_SWAP(tp, lp)	THREAD_SET_STATE(tp, TS_RUN, lp)
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate /*
6697c478bd9Sstevel@tonic-gate  * Put the thread in zombie state and set the lock pointer to NULL.
6707c478bd9Sstevel@tonic-gate  * The NULL will catch anything that tries to lock a zombie.
6717c478bd9Sstevel@tonic-gate  */
6727c478bd9Sstevel@tonic-gate #define	THREAD_ZOMB(tp)		THREAD_SET_STATE(tp, TS_ZOMB, NULL)
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate /*
6757c478bd9Sstevel@tonic-gate  * Set the thread into ONPROC state, and point the lock at the CPUs
6767c478bd9Sstevel@tonic-gate  * lock for the onproc thread(s).  This lock should be held, so the
6777c478bd9Sstevel@tonic-gate  * thread deoes not become unlocked, since these stores can be reordered.
6787c478bd9Sstevel@tonic-gate  */
6797c478bd9Sstevel@tonic-gate #define	THREAD_ONPROC(tp, cpu)	\
6807c478bd9Sstevel@tonic-gate 		THREAD_SET_STATE(tp, TS_ONPROC, &(cpu)->cpu_thread_lock)
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate /*
6837c478bd9Sstevel@tonic-gate  * Set the thread into the TS_SLEEP state, and set the lock pointer to
6847c478bd9Sstevel@tonic-gate  * to some sleep queue's lock.  The new lock should already be held.
6857c478bd9Sstevel@tonic-gate  */
6867c478bd9Sstevel@tonic-gate #define	THREAD_SLEEP(tp, lp)	{				\
6877c478bd9Sstevel@tonic-gate 			disp_lock_t	*tlp;			\
6887c478bd9Sstevel@tonic-gate 			tlp = (tp)->t_lockp;			\
6897c478bd9Sstevel@tonic-gate 			THREAD_SET_STATE(tp, TS_SLEEP, lp);	\
6907c478bd9Sstevel@tonic-gate 			disp_lock_exit_high(tlp);		\
6917c478bd9Sstevel@tonic-gate 			}
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate /*
6947c478bd9Sstevel@tonic-gate  * Interrupt threads are created in TS_FREE state, and their lock
6957c478bd9Sstevel@tonic-gate  * points at the associated CPU's lock.
6967c478bd9Sstevel@tonic-gate  */
6977c478bd9Sstevel@tonic-gate #define	THREAD_FREEINTR(tp, cpu)	\
6987c478bd9Sstevel@tonic-gate 		THREAD_SET_STATE(tp, TS_FREE, &(cpu)->cpu_thread_lock)
6997c478bd9Sstevel@tonic-gate 
700bff31d89SPhilippe Jung /* if tunable kmem_stackinfo is set, fill kthread stack with a pattern */
701bff31d89SPhilippe Jung #define	KMEM_STKINFO_PATTERN	0xbadcbadcbadcbadcULL
702bff31d89SPhilippe Jung 
703bff31d89SPhilippe Jung /*
704bff31d89SPhilippe Jung  * If tunable kmem_stackinfo is set, log the latest KMEM_LOG_STK_USAGE_SIZE
705bff31d89SPhilippe Jung  * dead kthreads that used their kernel stack the most.
706bff31d89SPhilippe Jung  */
707bff31d89SPhilippe Jung #define	KMEM_STKINFO_LOG_SIZE	16
708bff31d89SPhilippe Jung 
709bff31d89SPhilippe Jung /* kthread name (cmd/lwpid) string size in the stackinfo log */
710bff31d89SPhilippe Jung #define	KMEM_STKINFO_STR_SIZE	64
711bff31d89SPhilippe Jung 
712bff31d89SPhilippe Jung /*
713bff31d89SPhilippe Jung  * stackinfo logged data.
714bff31d89SPhilippe Jung  */
715bff31d89SPhilippe Jung typedef struct kmem_stkinfo {
716bff31d89SPhilippe Jung 	caddr_t	kthread;	/* kthread pointer */
717bff31d89SPhilippe Jung 	caddr_t	t_startpc;	/* where kthread started */
718bff31d89SPhilippe Jung 	caddr_t	start;		/* kthread stack start address */
719bff31d89SPhilippe Jung 	size_t	stksz;		/* kthread stack size */
720bff31d89SPhilippe Jung 	size_t	percent;	/* kthread stack high water mark */
721bff31d89SPhilippe Jung 	id_t	t_tid;		/* kthread id */
722bff31d89SPhilippe Jung 	char	cmd[KMEM_STKINFO_STR_SIZE];	/* kthread name (cmd/lwpid) */
723bff31d89SPhilippe Jung } kmem_stkinfo_t;
724bff31d89SPhilippe Jung 
7257c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
7267c478bd9Sstevel@tonic-gate }
7277c478bd9Sstevel@tonic-gate #endif
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate #endif /* _SYS_THREAD_H */
730