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