1 /* 2 * SYS/THREAD.H 3 * 4 * Implements the architecture independant portion of the LWKT 5 * subsystem. 6 * 7 * Types which must already be defined when this header is included by 8 * userland: struct md_thread 9 * 10 * $DragonFly: src/sys/sys/thread.h,v 1.47 2004/03/01 06:33:19 dillon Exp $ 11 */ 12 13 #ifndef _SYS_THREAD_H_ 14 #define _SYS_THREAD_H_ 15 16 #ifndef _SYS_STDINT_H_ 17 #include <sys/stdint.h> /* __int types */ 18 #endif 19 #ifndef _SYS_PARAM_H_ 20 #include <sys/param.h> /* MAXCOMLEN */ 21 #endif 22 #ifndef _SYS_QUEUE_H_ 23 #include <sys/queue.h> /* TAILQ_* macros */ 24 #endif 25 #ifndef _SYS_MSGPORT_H_ 26 #include <sys/msgport.h> /* lwkt_port */ 27 #endif 28 29 struct globaldata; 30 struct proc; 31 struct thread; 32 struct lwkt_queue; 33 struct lwkt_token; 34 struct lwkt_tokref; 35 struct lwkt_wait; 36 struct lwkt_ipiq; 37 struct lwkt_cpu_msg; 38 struct lwkt_cpu_port; 39 struct lwkt_rwlock; 40 struct lwkt_msg; 41 struct lwkt_port; 42 struct lwkt_cpusync; 43 union sysunion; 44 45 typedef struct lwkt_queue *lwkt_queue_t; 46 typedef struct lwkt_token *lwkt_token_t; 47 typedef struct lwkt_tokref *lwkt_tokref_t; 48 typedef struct lwkt_wait *lwkt_wait_t; 49 typedef struct lwkt_cpu_msg *lwkt_cpu_msg_t; 50 typedef struct lwkt_cpu_port *lwkt_cpu_port_t; 51 typedef struct lwkt_rwlock *lwkt_rwlock_t; 52 typedef struct lwkt_ipiq *lwkt_ipiq_t; 53 typedef struct lwkt_cpusync *lwkt_cpusync_t; 54 typedef struct thread *thread_t; 55 56 typedef TAILQ_HEAD(lwkt_queue, thread) lwkt_queue; 57 58 /* 59 * Differentiation between kernel threads and user threads. Userland 60 * programs which want to access to kernel structures have to define 61 * _KERNEL_STRUCTURES. This is a kinda safety valve to prevent badly 62 * written user programs from getting an LWKT thread that is neither the 63 * kernel nor the user version. 64 */ 65 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES) 66 #ifndef _MACHINE_THREAD_H_ 67 #include <machine/thread.h> /* md_thread */ 68 #endif 69 #ifndef _MACHINE_FRAME_H_ 70 #include <machine/frame.h> 71 #endif 72 #else 73 struct intrframe; 74 #endif 75 76 /* 77 * Tokens are used to serialize access to information. They are 'soft' 78 * serialization entities that only stay in effect while the thread is 79 * running. If the thread blocks, other threads can run holding the same 80 * tokens. The tokens are reacquired when the original thread resumes. 81 * 82 * A thread can depend on its serialization remaining intact through a 83 * preemption. An interrupt which attempts to use the same token as the 84 * thread being preempted will reschedule itself for non-preemptive 85 * operation, so the new token code is capable of interlocking against 86 * interrupts as well as other cpus. 87 * 88 * Tokens are managed through a helper reference structure, lwkt_tokref, 89 * which is typically declared on the caller's stack. Multiple tokref's 90 * may reference the same token. 91 */ 92 typedef struct lwkt_token { 93 struct globaldata *t_cpu; /* the current owner of the token */ 94 struct globaldata *t_reqcpu; /* requesting cpu */ 95 int t_unused01; /* (used to be generation number) */ 96 } lwkt_token; 97 98 typedef struct lwkt_tokref { 99 lwkt_token_t tr_tok; /* token in question */ 100 __uint32_t tr_magic; /* sanity check */ 101 lwkt_tokref_t tr_next; /* linked list */ 102 lwkt_tokref_t tr_gdreqnext; /* based at gd_tokreqbase */ 103 struct globaldata *tr_reqgd; /* requesting cpu */ 104 } lwkt_tokref; 105 106 #define LWKT_TOKREF_MAGIC1 \ 107 ((__uint32_t)0x544f4b52) /* normal */ 108 #define LWKT_TOKREF_MAGIC2 \ 109 ((__uint32_t)0x544f4b53) /* pending req */ 110 #define LWKT_TOKREF_INIT(tok) \ 111 { tok, LWKT_TOKREF_MAGIC1 } 112 #define LWKT_TOKREF_DECLARE(name, tok) \ 113 lwkt_tokref name = LWKT_TOKREF_INIT(tok) 114 115 /* 116 * Wait structures deal with blocked threads. Due to the way remote cpus 117 * interact with these structures stable storage must be used. 118 */ 119 typedef struct lwkt_wait { 120 lwkt_queue wa_waitq; /* list of waiting threads */ 121 lwkt_token wa_token; /* who currently owns the list */ 122 int wa_gen; 123 int wa_count; 124 } lwkt_wait; 125 126 #define MAXCPUFIFO 16 /* power of 2 */ 127 #define MAXCPUFIFO_MASK (MAXCPUFIFO - 1) 128 #define LWKT_MAXTOKENS 16 /* max tokens beneficially held by thread */ 129 130 /* 131 * Always cast to ipifunc_t when registering an ipi. The actual ipi function 132 * is called with both the data and an interrupt frame, but the ipi function 133 * that is registered might only declare a data argument. 134 */ 135 typedef void (*ipifunc_t)(void *arg); 136 typedef void (*ipifunc2_t)(void *arg, struct intrframe *frame); 137 138 typedef struct lwkt_ipiq { 139 int ip_rindex; /* only written by target cpu */ 140 int ip_xindex; /* writte by target, indicates completion */ 141 int ip_windex; /* only written by source cpu */ 142 ipifunc2_t ip_func[MAXCPUFIFO]; 143 void *ip_arg[MAXCPUFIFO]; 144 int ip_npoll; 145 } lwkt_ipiq; 146 147 /* 148 * CPU Synchronization structure. See lwkt_cpusync_start() and 149 * lwkt_cpusync_finish() for more information. 150 */ 151 typedef void (*cpusync_func_t)(lwkt_cpusync_t poll); 152 typedef void (*cpusync_func2_t)(void *data); 153 154 struct lwkt_cpusync { 155 cpusync_func_t cs_run_func; /* run (tandem w/ acquire) */ 156 cpusync_func_t cs_fin1_func; /* fin1 (synchronized) */ 157 cpusync_func2_t cs_fin2_func; /* fin2 (tandem w/ release) */ 158 void *cs_data; 159 int cs_maxcount; 160 volatile int cs_count; 161 cpumask_t cs_mask; 162 }; 163 164 /* 165 * The standard message and queue structure used for communications between 166 * cpus. Messages are typically queued via a machine-specific non-linked 167 * FIFO matrix allowing any cpu to send a message to any other cpu without 168 * blocking. 169 */ 170 typedef struct lwkt_cpu_msg { 171 void (*cm_func)(lwkt_cpu_msg_t msg); /* primary dispatch function */ 172 int cm_code; /* request code if applicable */ 173 int cm_cpu; /* reply to cpu */ 174 thread_t cm_originator; /* originating thread for wakeup */ 175 } lwkt_cpu_msg; 176 177 /* 178 * reader/writer lock 179 */ 180 typedef struct lwkt_rwlock { 181 lwkt_wait rw_wait; 182 thread_t rw_owner; 183 int rw_count; 184 int rw_requests; 185 } lwkt_rwlock; 186 187 #define rw_token rw_wait.wa_token 188 189 /* 190 * Thread structure. Note that ownership of a thread structure is special 191 * cased and there is no 'token'. A thread is always owned by the cpu 192 * represented by td_gd, any manipulation of the thread by some other cpu 193 * must be done through cpu_*msg() functions. e.g. you could request 194 * ownership of a thread that way, or hand a thread off to another cpu. 195 * 196 * NOTE: td_pri is bumped by TDPRI_CRIT when entering a critical section, 197 * but this does not effect how the thread is scheduled by LWKT. 198 */ 199 struct md_intr_info; 200 struct caps_kinfo; 201 202 struct thread { 203 TAILQ_ENTRY(thread) td_threadq; 204 TAILQ_ENTRY(thread) td_allq; 205 lwkt_port td_msgport; /* built-in message port for replies */ 206 struct proc *td_proc; /* (optional) associated process */ 207 struct pcb *td_pcb; /* points to pcb and top of kstack */ 208 struct globaldata *td_gd; /* associated with this cpu */ 209 const char *td_wmesg; /* string name for blockage */ 210 void *td_wchan; /* waiting on channel */ 211 int td_pri; /* 0-31, 31=highest priority (note 1) */ 212 int td_flags; /* TDF flags */ 213 int td_gen; /* wait queue chasing generation number */ 214 /* maybe preempt */ 215 void (*td_preemptable)(struct thread *td, int critpri); 216 void (*td_release)(struct thread *td); 217 union { 218 struct md_intr_info *intdata; 219 } td_info; 220 char *td_kstack; /* kernel stack */ 221 char *td_sp; /* kernel stack pointer for LWKT restore */ 222 void (*td_switch)(struct thread *ntd); 223 lwkt_wait_t td_wait; /* thread sitting on wait structure */ 224 __uint64_t td_uticks; /* Statclock hits in user mode (uS) */ 225 __uint64_t td_sticks; /* Statclock hits in system mode (uS) */ 226 __uint64_t td_iticks; /* Statclock hits processing intr (uS) */ 227 int td_locks; /* lockmgr lock debugging YYY */ 228 int td_refs; /* hold position in gd_tdallq / hold free */ 229 int td_nest_count; /* prevent splz nesting */ 230 #ifdef SMP 231 int td_mpcount; /* MP lock held (count) */ 232 int td_cscount; /* cpu synchronization master */ 233 #else 234 int td_unused001; 235 int td_unused002; 236 #endif 237 char td_comm[MAXCOMLEN+1]; /* typ 16+1 bytes */ 238 struct thread *td_preempted; /* we preempted this thread */ 239 struct caps_kinfo *td_caps; /* list of client and server registrations */ 240 lwkt_tokref_t td_toks; /* tokens beneficially held */ 241 struct md_thread td_mach; 242 }; 243 244 /* 245 * Thread flags. Note that TDF_RUNNING is cleared on the old thread after 246 * we switch to the new one, which is necessary because LWKTs don't need 247 * to hold the BGL. This flag is used by the exit code and the managed 248 * thread migration code. 249 * 250 * LWKT threads stay on their (per-cpu) run queue while running, not to 251 * be confused with user processes which are removed from the user scheduling 252 * run queue while actually running. 253 */ 254 #define TDF_RUNNING 0x0001 /* thread still active */ 255 #define TDF_RUNQ 0x0002 /* on an LWKT run queue */ 256 #define TDF_PREEMPT_LOCK 0x0004 /* I have been preempted */ 257 #define TDF_PREEMPT_DONE 0x0008 /* acknowledge preemption complete */ 258 #define TDF_IDLE_NOHLT 0x0010 /* we need to spin */ 259 260 #define TDF_SYSTHREAD 0x0100 /* system thread */ 261 #define TDF_ALLOCATED_THREAD 0x0200 /* zalloc allocated thread */ 262 #define TDF_ALLOCATED_STACK 0x0400 /* zalloc allocated stack */ 263 #define TDF_VERBOSE 0x0800 /* verbose on exit */ 264 #define TDF_DEADLKTREAT 0x1000 /* special lockmgr deadlock treatment */ 265 #define TDF_STOPREQ 0x2000 /* suspend_kproc */ 266 #define TDF_WAKEREQ 0x4000 /* resume_kproc */ 267 #define TDF_TIMEOUT 0x8000 /* tsleep timeout */ 268 #define TDF_INTTHREAD 0x00010000 /* interrupt thread */ 269 270 /* 271 * Thread priorities. Typically only one thread from any given 272 * user process scheduling queue is on the LWKT run queue at a time. 273 * Remember that there is one LWKT run queue per cpu. 274 * 275 * Critical sections are handled by bumping td_pri above TDPRI_MAX, which 276 * causes interrupts to be masked as they occur. When this occurs a 277 * rollup flag will be set in mycpu->gd_reqflags. 278 */ 279 #define TDPRI_IDLE_THREAD 0 /* the idle thread */ 280 #define TDPRI_USER_IDLE 4 /* user scheduler idle */ 281 #define TDPRI_USER_NORM 6 /* user scheduler normal */ 282 #define TDPRI_USER_REAL 8 /* user scheduler real time */ 283 #define TDPRI_KERN_LPSCHED 9 /* scheduler helper for userland sch */ 284 #define TDPRI_KERN_USER 10 /* kernel / block in syscall */ 285 #define TDPRI_KERN_DAEMON 12 /* kernel daemon (pageout, etc) */ 286 #define TDPRI_SOFT_NORM 14 /* kernel / normal */ 287 #define TDPRI_SOFT_TIMER 16 /* kernel / timer */ 288 #define TDPRI_EXITING 19 /* exiting thread */ 289 #define TDPRI_INT_SUPPORT 20 /* kernel / high priority support */ 290 #define TDPRI_INT_LOW 27 /* low priority interrupt */ 291 #define TDPRI_INT_MED 28 /* medium priority interrupt */ 292 #define TDPRI_INT_HIGH 29 /* high priority interrupt */ 293 #define TDPRI_MAX 31 294 295 #define TDPRI_MASK 31 296 #define TDPRI_CRIT 32 /* high bits of td_pri used for crit */ 297 298 #define CACHE_NTHREADS 6 299 300 #define IN_CRITICAL_SECT(td) ((td)->td_pri >= TDPRI_CRIT) 301 302 #ifdef _KERNEL 303 304 extern struct vm_zone *thread_zone; 305 306 #endif 307 308 /* 309 * Applies both to the kernel and to liblwkt. 310 */ 311 extern struct thread *lwkt_alloc_thread(struct thread *template, int cpu); 312 extern void lwkt_init_thread(struct thread *td, void *stack, int flags, 313 struct globaldata *gd); 314 extern void lwkt_set_comm(thread_t td, const char *ctl, ...); 315 extern void lwkt_wait_free(struct thread *td); 316 extern void lwkt_free_thread(struct thread *td); 317 extern void lwkt_wait_init(struct lwkt_wait *w); 318 extern void lwkt_gdinit(struct globaldata *gd); 319 extern void lwkt_switch(void); 320 extern void lwkt_maybe_switch(void); 321 extern void lwkt_preempt(thread_t ntd, int critpri); 322 extern void lwkt_schedule(thread_t td); 323 extern void lwkt_schedule_self(void); 324 extern void lwkt_deschedule(thread_t td); 325 extern void lwkt_deschedule_self(void); 326 extern void lwkt_acquire(thread_t td); 327 extern void lwkt_yield(void); 328 extern void lwkt_yield_quick(void); 329 extern void lwkt_token_wait(void); 330 extern void lwkt_hold(thread_t td); 331 extern void lwkt_rele(thread_t td); 332 333 extern void lwkt_block(lwkt_wait_t w, const char *wmesg, int *gen); 334 extern void lwkt_signal(lwkt_wait_t w, int count); 335 336 extern int lwkt_havetoken(lwkt_token_t tok); 337 extern int lwkt_havetokref(lwkt_tokref_t xref); 338 extern void lwkt_gettoken(lwkt_tokref_t ref, lwkt_token_t tok); 339 extern int lwkt_trytoken(lwkt_tokref_t ref, lwkt_token_t tok); 340 extern void lwkt_gettokref(lwkt_tokref_t ref); 341 extern int lwkt_trytokref(lwkt_tokref_t ref); 342 extern void lwkt_reltoken(lwkt_tokref_t ref); 343 extern void lwkt_reqtoken_remote(void *data); 344 extern int lwkt_chktokens(thread_t td); 345 extern void lwkt_drain_token_requests(void); 346 extern void lwkt_token_init(lwkt_token_t tok); 347 extern void lwkt_token_uninit(lwkt_token_t tok); 348 349 extern void lwkt_token_pool_init(void); 350 extern lwkt_token_t lwkt_token_pool_get(void *ptraddr); 351 352 extern void lwkt_rwlock_init(lwkt_rwlock_t lock); 353 extern void lwkt_rwlock_uninit(lwkt_rwlock_t lock); 354 extern void lwkt_exlock(lwkt_rwlock_t lock, const char *wmesg); 355 extern void lwkt_shlock(lwkt_rwlock_t lock, const char *wmesg); 356 extern void lwkt_exunlock(lwkt_rwlock_t lock); 357 extern void lwkt_shunlock(lwkt_rwlock_t lock); 358 359 extern void lwkt_setpri(thread_t td, int pri); 360 extern void lwkt_setpri_self(int pri); 361 extern int lwkt_send_ipiq(struct globaldata *targ, ipifunc_t func, void *arg); 362 extern int lwkt_send_ipiq_passive(struct globaldata *targ, ipifunc_t func, void *arg); 363 extern int lwkt_send_ipiq_bycpu(int dcpu, ipifunc_t func, void *arg); 364 extern int lwkt_send_ipiq_mask(cpumask_t mask, ipifunc_t func, void *arg); 365 extern void lwkt_wait_ipiq(struct globaldata *targ, int seq); 366 extern int lwkt_seq_ipiq(struct globaldata *targ); 367 extern void lwkt_process_ipiq(void); 368 #ifdef _KERNEL 369 extern void lwkt_process_ipiq_frame(struct intrframe frame); 370 #endif 371 extern void lwkt_cpusync_simple(cpumask_t mask, cpusync_func_t func, void *data); 372 extern void lwkt_cpusync_fastdata(cpumask_t mask, cpusync_func2_t func, void *data); 373 extern void lwkt_cpusync_start(cpumask_t mask, lwkt_cpusync_t poll); 374 extern void lwkt_cpusync_add(cpumask_t mask, lwkt_cpusync_t poll); 375 extern void lwkt_cpusync_finish(lwkt_cpusync_t poll); 376 extern void crit_panic(void); 377 extern struct proc *lwkt_preempted_proc(void); 378 379 extern int lwkt_create (void (*func)(void *), void *arg, struct thread **ptd, 380 struct thread *template, int tdflags, int cpu, 381 const char *ctl, ...); 382 extern void lwkt_exit (void) __dead2; 383 384 #endif 385 386