kernel - refactor cpusync and pmap_inval code, fix lockup
[dragonfly.git] / sys / sys / thread.h
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.97 2008/09/20 04:31:02 sephe 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 #ifndef _SYS_TIME_H_
29 #include <sys/time.h>           /* struct timeval */
30 #endif
31 #ifndef _SYS_SPINLOCK_H_
32 #include <sys/spinlock.h>
33 #endif
34 #ifndef _SYS_IOSCHED_H_
35 #include <sys/iosched.h>
36 #endif
37 #ifndef _MACHINE_THREAD_H_
38 #include <machine/thread.h>
39 #endif
40
41 struct globaldata;
42 struct lwp;
43 struct proc;
44 struct thread;
45 struct lwkt_queue;
46 struct lwkt_token;
47 struct lwkt_tokref;
48 struct lwkt_ipiq;
49 struct lwkt_cpu_msg;
50 struct lwkt_cpu_port;
51 struct lwkt_msg;
52 struct lwkt_port;
53 struct lwkt_cpusync;
54 union sysunion;
55
56 typedef struct lwkt_queue       *lwkt_queue_t;
57 typedef struct lwkt_token       *lwkt_token_t;
58 typedef struct lwkt_tokref      *lwkt_tokref_t;
59 typedef struct lwkt_cpu_msg     *lwkt_cpu_msg_t;
60 typedef struct lwkt_cpu_port    *lwkt_cpu_port_t;
61 typedef struct lwkt_ipiq        *lwkt_ipiq_t;
62 typedef struct lwkt_cpusync     *lwkt_cpusync_t;
63 typedef struct thread           *thread_t;
64
65 typedef TAILQ_HEAD(lwkt_queue, thread) lwkt_queue;
66
67 /*
68  * Differentiation between kernel threads and user threads.  Userland
69  * programs which want to access to kernel structures have to define
70  * _KERNEL_STRUCTURES.  This is a kinda safety valve to prevent badly
71  * written user programs from getting an LWKT thread that is neither the
72  * kernel nor the user version.
73  */
74 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
75 #ifndef _MACHINE_THREAD_H_
76 #include <machine/thread.h>             /* md_thread */
77 #endif
78 #ifndef _MACHINE_FRAME_H_
79 #include <machine/frame.h>
80 #endif
81 #else
82 struct intrframe;
83 #endif
84
85 /*
86  * Tokens are used to serialize access to information.  They are 'soft'
87  * serialization entities that only stay in effect while a thread is
88  * running.  If the thread blocks, other threads can run holding the same
89  * token(s).  The tokens are reacquired when the original thread resumes.
90  *
91  * A thread can depend on its serialization remaining intact through a
92  * preemption.  An interrupt which attempts to use the same token as the
93  * thread being preempted will reschedule itself for non-preemptive
94  * operation, so the new token code is capable of interlocking against
95  * interrupts as well as other cpus.  This means that your token can only
96  * be (temporarily) lost if you *explicitly* block.
97  *
98  * Tokens are managed through a helper reference structure, lwkt_tokref.  Each
99  * thread has a stack of tokref's to keep track of acquired tokens.  Multiple
100  * tokref's may reference the same token.
101  */
102
103 typedef struct lwkt_token {
104     struct lwkt_tokref  *t_ref;         /* Owning ref or NULL */
105     intptr_t            t_flags;        /* MP lock required */
106     long                t_collisions;   /* Collision counter */
107     cpumask_t           t_collmask;     /* Collision cpu mask for resched */
108     const char          *t_desc;        /* Descriptive name */
109 } lwkt_token;
110
111 #define LWKT_TOKEN_MPSAFE       0x0001
112
113 /*
114  * Static initialization for a lwkt_token.
115  *      UP - Not MPSAFE (full MP lock will also be acquired)
116  *      MP - Is MPSAFE  (only the token will be acquired)
117  */
118 #define LWKT_TOKEN_UP_INITIALIZER(name) \
119 {                                       \
120         .t_ref = NULL,                  \
121         .t_flags = 0,                   \
122         .t_collisions = 0,              \
123         .t_collmask = 0,                \
124         .t_desc = #name                 \
125 }
126
127 #define LWKT_TOKEN_MP_INITIALIZER(name) \
128 {                                       \
129         .t_ref = NULL,                  \
130         .t_flags = LWKT_TOKEN_MPSAFE,   \
131         .t_collisions = 0,              \
132         .t_collmask = 0,                \
133         .t_desc = #name                 \
134 }
135
136 /*
137  * Assert that a particular token is held
138  */
139 #define LWKT_TOKEN_HELD(tok)            _lwkt_token_held(tok, curthread)
140
141 #define ASSERT_LWKT_TOKEN_HELD(tok)     \
142         KKASSERT(LWKT_TOKEN_HELD(tok))
143
144 #define ASSERT_NO_TOKENS_HELD(td)       \
145         KKASSERT((td)->td_toks_stop == &td->td_toks_array[0])
146
147 /*
148  * Assert that a particular token is held and we are in a hard
149  * code execution section (interrupt, ipi, or hard code section).
150  * Hard code sections are not allowed to block or potentially block.
151  * e.g. lwkt_gettoken() would only be ok if the token were already
152  * held.
153  */
154 #define ASSERT_LWKT_TOKEN_HARD(tok)                                     \
155         do {                                                            \
156                 globaldata_t zgd __debugvar = mycpu;                    \
157                 KKASSERT((tok)->t_ref &&                                \
158                          (tok)->t_ref->tr_owner == zgd->gd_curthread && \
159                          zgd->gd_intr_nesting_level > 0);               \
160         } while(0)
161
162 /*
163  * Assert that a particular token is held and we are in a normal
164  * critical section.  Critical sections will not be preempted but
165  * can explicitly block (tsleep, lwkt_gettoken, etc).
166  */
167 #define ASSERT_LWKT_TOKEN_CRIT(tok)                                     \
168         do {                                                            \
169                 globaldata_t zgd __debugvar = mycpu;                    \
170                 KKASSERT((tok)->t_ref &&                                \
171                          (tok)->t_ref->tr_owner == zgd->gd_curthread && \
172                          zgd->gd_curthread->td_critcount > 0);          \
173         } while(0)
174
175 struct lwkt_tokref {
176     lwkt_token_t        tr_tok;         /* token in question */
177     struct thread       *tr_owner;      /* me */
178     intptr_t            tr_flags;       /* copy of t_flags */
179     const void          *tr_stallpc;    /* stalled at pc */
180 };
181
182 #define MAXCPUFIFO      16      /* power of 2 */
183 #define MAXCPUFIFO_MASK (MAXCPUFIFO - 1)
184 #define LWKT_MAXTOKENS  32      /* max tokens beneficially held by thread */
185
186 /*
187  * Always cast to ipifunc_t when registering an ipi.  The actual ipi function
188  * is called with both the data and an interrupt frame, but the ipi function
189  * that is registered might only declare a data argument.
190  */
191 typedef void (*ipifunc1_t)(void *arg);
192 typedef void (*ipifunc2_t)(void *arg, int arg2);
193 typedef void (*ipifunc3_t)(void *arg, int arg2, struct intrframe *frame);
194
195 typedef struct lwkt_ipiq {
196     int         ip_rindex;      /* only written by target cpu */
197     int         ip_xindex;      /* written by target, indicates completion */
198     int         ip_windex;      /* only written by source cpu */
199     ipifunc3_t  ip_func[MAXCPUFIFO];
200     void        *ip_arg1[MAXCPUFIFO];
201     int         ip_arg2[MAXCPUFIFO];
202     u_int       ip_npoll;       /* synchronization to avoid excess IPIs */
203 } lwkt_ipiq;
204
205 /*
206  * CPU Synchronization structure.  See lwkt_cpusync_start() and
207  * lwkt_cpusync_finish() for more information.
208  */
209 typedef void (*cpusync_func_t)(void *arg);
210
211 struct lwkt_cpusync {
212     cpumask_t   cs_mask;                /* cpus running the sync */
213     cpumask_t   cs_mack;                /* mask acknowledge */
214     cpusync_func_t cs_func;             /* function to execute */
215     void        *cs_data;               /* function data */
216 };
217
218 /*
219  * The standard message and queue structure used for communications between
220  * cpus.  Messages are typically queued via a machine-specific non-linked
221  * FIFO matrix allowing any cpu to send a message to any other cpu without
222  * blocking.
223  */
224 typedef struct lwkt_cpu_msg {
225     void        (*cm_func)(lwkt_cpu_msg_t msg); /* primary dispatch function */
226     int         cm_code;                /* request code if applicable */
227     int         cm_cpu;                 /* reply to cpu */
228     thread_t    cm_originator;          /* originating thread for wakeup */
229 } lwkt_cpu_msg;
230
231 /*
232  * Thread structure.  Note that ownership of a thread structure is special
233  * cased and there is no 'token'.  A thread is always owned by the cpu
234  * represented by td_gd, any manipulation of the thread by some other cpu
235  * must be done through cpu_*msg() functions.  e.g. you could request
236  * ownership of a thread that way, or hand a thread off to another cpu.
237  *
238  * NOTE: td_ucred is synchronized from the p_ucred on user->kernel syscall,
239  *       trap, and AST/signal transitions to provide a stable ucred for
240  *       (primarily) system calls.  This field will be NULL for pure kernel
241  *       threads.
242  */
243 struct md_intr_info;
244 struct caps_kinfo;
245
246 struct thread {
247     TAILQ_ENTRY(thread) td_threadq;
248     TAILQ_ENTRY(thread) td_allq;
249     TAILQ_ENTRY(thread) td_sleepq;
250     lwkt_port   td_msgport;     /* built-in message port for replies */
251     struct lwp  *td_lwp;        /* (optional) associated lwp */
252     struct proc *td_proc;       /* (optional) associated process */
253     struct pcb  *td_pcb;        /* points to pcb and top of kstack */
254     struct globaldata *td_gd;   /* associated with this cpu */
255     const char  *td_wmesg;      /* string name for blockage */
256     const volatile void *td_wchan;      /* waiting on channel */
257     int         td_pri;         /* 0-31, 31=highest priority (note 1) */
258     int         td_critcount;   /* critical section priority */
259     int         td_flags;       /* TDF flags */
260     int         td_wdomain;     /* domain for wchan address (typ 0) */
261     void        (*td_preemptable)(struct thread *td, int critcount);
262     void        (*td_release)(struct thread *td);
263     char        *td_kstack;     /* kernel stack */
264     int         td_kstack_size; /* size of kernel stack */
265     char        *td_sp;         /* kernel stack pointer for LWKT restore */
266     void        (*td_switch)(struct thread *ntd);
267     __uint64_t  td_uticks;      /* Statclock hits in user mode (uS) */
268     __uint64_t  td_sticks;      /* Statclock hits in system mode (uS) */
269     __uint64_t  td_iticks;      /* Statclock hits processing intr (uS) */
270     int         td_locks;       /* lockmgr lock debugging */
271     void        *td_dsched_priv1;       /* priv data for I/O schedulers */
272     int         td_refs;        /* hold position in gd_tdallq / hold free */
273     int         td_nest_count;  /* prevent splz nesting */
274     int         td_unused01[2]; /* for future fields */
275 #ifdef SMP
276     int         td_cscount;     /* cpu synchronization master */
277 #else
278     int         td_cscount_unused;
279 #endif
280     int         td_unused02[4]; /* for future fields */
281     int         td_unused03[4]; /* for future fields */
282     struct iosched_data td_iosdata;     /* Dynamic I/O scheduling data */
283     struct timeval td_start;    /* start time for a thread/process */
284     char        td_comm[MAXCOMLEN+1]; /* typ 16+1 bytes */
285     struct thread *td_preempted; /* we preempted this thread */
286     struct ucred *td_ucred;             /* synchronized from p_ucred */
287     struct caps_kinfo *td_caps; /* list of client and server registrations */
288     lwkt_tokref_t td_toks_stop;
289     struct lwkt_tokref td_toks_array[LWKT_MAXTOKENS];
290     int         td_fairq_lticks;        /* fairq wakeup accumulator reset */
291     int         td_fairq_accum;         /* fairq priority accumulator */
292     const void  *td_mplock_stallpc;     /* last mplock stall address */
293 #ifdef DEBUG_CRIT_SECTIONS
294 #define CRIT_DEBUG_ARRAY_SIZE   32
295 #define CRIT_DEBUG_ARRAY_MASK   (CRIT_DEBUG_ARRAY_SIZE - 1)
296     const char  *td_crit_debug_array[CRIT_DEBUG_ARRAY_SIZE];
297     int         td_crit_debug_index;
298     int         td_in_crit_report;      
299 #endif
300     struct md_thread td_mach;
301 #ifdef DEBUG_LOCKS
302 #define SPINLOCK_DEBUG_ARRAY_SIZE       32
303    int  td_spinlock_stack_id[SPINLOCK_DEBUG_ARRAY_SIZE];
304    struct spinlock *td_spinlock_stack[SPINLOCK_DEBUG_ARRAY_SIZE];
305    void         *td_spinlock_caller_pc[SPINLOCK_DEBUG_ARRAY_SIZE];
306 #endif
307 };
308
309 #define td_toks_base            td_toks_array[0]
310 #define td_toks_end             td_toks_array[LWKT_MAXTOKENS]
311
312 #define TD_TOKS_HELD(td)        ((td)->td_toks_stop != &(td)->td_toks_base)
313 #define TD_TOKS_NOT_HELD(td)    ((td)->td_toks_stop == &(td)->td_toks_base)
314
315 /*
316  * Thread flags.  Note that TDF_RUNNING is cleared on the old thread after
317  * we switch to the new one, which is necessary because LWKTs don't need
318  * to hold the BGL.  This flag is used by the exit code and the managed
319  * thread migration code.  Note in addition that preemption will cause
320  * TDF_RUNNING to be cleared temporarily, so any code checking TDF_RUNNING
321  * must also check TDF_PREEMPT_LOCK.
322  *
323  * LWKT threads stay on their (per-cpu) run queue while running, not to
324  * be confused with user processes which are removed from the user scheduling
325  * run queue while actually running.
326  *
327  * td_threadq can represent the thread on one of three queues... the LWKT
328  * run queue, a tsleep queue, or an lwkt blocking queue.  The LWKT subsystem
329  * does not allow a thread to be scheduled if it already resides on some
330  * queue.
331  */
332 #define TDF_RUNNING             0x0001  /* thread still active */
333 #define TDF_RUNQ                0x0002  /* on an LWKT run queue */
334 #define TDF_PREEMPT_LOCK        0x0004  /* I have been preempted */
335 #define TDF_PREEMPT_DONE        0x0008  /* acknowledge preemption complete */
336 #define TDF_UNUSED00000010      0x0010
337 #define TDF_MIGRATING           0x0020  /* thread is being migrated */
338 #define TDF_SINTR               0x0040  /* interruptability hint for 'ps' */
339 #define TDF_TSLEEPQ             0x0080  /* on a tsleep wait queue */
340
341 #define TDF_SYSTHREAD           0x0100  /* allocations may use reserve */
342 #define TDF_ALLOCATED_THREAD    0x0200  /* objcache allocated thread */
343 #define TDF_ALLOCATED_STACK     0x0400  /* objcache allocated stack */
344 #define TDF_VERBOSE             0x0800  /* verbose on exit */
345 #define TDF_DEADLKTREAT         0x1000  /* special lockmgr deadlock treatment */
346 #define TDF_STOPREQ             0x2000  /* suspend_kproc */
347 #define TDF_WAKEREQ             0x4000  /* resume_kproc */
348 #define TDF_TIMEOUT             0x8000  /* tsleep timeout */
349 #define TDF_INTTHREAD           0x00010000      /* interrupt thread */
350 #define TDF_TSLEEP_DESCHEDULED  0x00020000      /* tsleep core deschedule */
351 #define TDF_BLOCKED             0x00040000      /* Thread is blocked */
352 #define TDF_PANICWARN           0x00080000      /* panic warning in switch */
353 #define TDF_BLOCKQ              0x00100000      /* on block queue */
354 #define TDF_UNUSED00200000      0x00200000
355 #define TDF_EXITING             0x00400000      /* thread exiting */
356 #define TDF_USINGFP             0x00800000      /* thread using fp coproc */
357 #define TDF_KERNELFP            0x01000000      /* kernel using fp coproc */
358 #define TDF_UNUSED02000000      0x02000000
359 #define TDF_CRYPTO              0x04000000      /* crypto thread */
360 #define TDF_MARKER              0x80000000      /* fairq marker thread */
361
362 /*
363  * Thread priorities.  Typically only one thread from any given
364  * user process scheduling queue is on the LWKT run queue at a time.
365  * Remember that there is one LWKT run queue per cpu.
366  *
367  * Critical sections are handled by bumping td_pri above TDPRI_MAX, which
368  * causes interrupts to be masked as they occur.  When this occurs a
369  * rollup flag will be set in mycpu->gd_reqflags.
370  */
371 #define TDPRI_IDLE_THREAD       0       /* the idle thread */
372 #define TDPRI_IDLE_WORK         1       /* idle work (page zero, etc) */
373 #define TDPRI_USER_SCHEDULER    2       /* user scheduler helper */
374 #define TDPRI_USER_IDLE         4       /* user scheduler idle */
375 #define TDPRI_USER_NORM         6       /* user scheduler normal */
376 #define TDPRI_USER_REAL         8       /* user scheduler real time */
377 #define TDPRI_KERN_LPSCHED      9       /* scheduler helper for userland sch */
378 #define TDPRI_KERN_USER         10      /* kernel / block in syscall */
379 #define TDPRI_KERN_DAEMON       12      /* kernel daemon (pageout, etc) */
380 #define TDPRI_SOFT_NORM         14      /* kernel / normal */
381 #define TDPRI_SOFT_TIMER        16      /* kernel / timer */
382 #define TDPRI_EXITING           19      /* exiting thread */
383 #define TDPRI_INT_SUPPORT       20      /* kernel / high priority support */
384 #define TDPRI_INT_LOW           27      /* low priority interrupt */
385 #define TDPRI_INT_MED           28      /* medium priority interrupt */
386 #define TDPRI_INT_HIGH          29      /* high priority interrupt */
387 #define TDPRI_MAX               31
388
389 /*
390  * Scale is the approximate number of ticks for which we desire the
391  * entire gd_tdrunq to get service.  With hz = 100 a scale of 8 is 80ms.
392  *
393  * Setting this value too small will result in inefficient switching
394  * rates.
395  */
396 #define TDFAIRQ_SCALE           8
397 #define TDFAIRQ_MAX(gd)         ((gd)->gd_fairq_total_pri * TDFAIRQ_SCALE)
398
399 #define LWKT_THREAD_STACK       (UPAGES * PAGE_SIZE)
400
401 #define CACHE_NTHREADS          6
402
403 #define IN_CRITICAL_SECT(td)    ((td)->td_critcount)
404
405 #ifdef _KERNEL
406
407 /*
408  * Global tokens
409  */
410 extern struct lwkt_token mp_token;
411 extern struct lwkt_token pmap_token;
412 extern struct lwkt_token dev_token;
413 extern struct lwkt_token vm_token;
414 extern struct lwkt_token vmspace_token;
415 extern struct lwkt_token kvm_token;
416 extern struct lwkt_token proc_token;
417 extern struct lwkt_token tty_token;
418 extern struct lwkt_token vnode_token;
419 extern struct lwkt_token vmobj_token;
420
421 /*
422  * Procedures
423  */
424 extern void lwkt_init(void);
425 extern struct thread *lwkt_alloc_thread(struct thread *, int, int, int);
426 extern void lwkt_init_thread(struct thread *, void *, int, int,
427                              struct globaldata *);
428 extern void lwkt_set_comm(thread_t, const char *, ...) __printflike(2, 3);
429 extern void lwkt_wait_free(struct thread *);
430 extern void lwkt_free_thread(struct thread *);
431 extern void lwkt_gdinit(struct globaldata *);
432 extern void lwkt_switch(void);
433 extern void lwkt_preempt(thread_t, int);
434 extern void lwkt_schedule(thread_t);
435 extern void lwkt_schedule_noresched(thread_t);
436 extern void lwkt_schedule_self(thread_t);
437 extern void lwkt_deschedule(thread_t);
438 extern void lwkt_deschedule_self(thread_t);
439 extern void lwkt_yield(void);
440 extern void lwkt_user_yield(void);
441 extern void lwkt_token_wait(void);
442 extern void lwkt_hold(thread_t);
443 extern void lwkt_rele(thread_t);
444 extern void lwkt_passive_release(thread_t);
445 extern void lwkt_maybe_splz(thread_t);
446
447 extern void lwkt_gettoken(lwkt_token_t);
448 extern void lwkt_gettoken_hard(lwkt_token_t);
449 extern int  lwkt_trytoken(lwkt_token_t);
450 extern void lwkt_reltoken(lwkt_token_t);
451 extern void lwkt_reltoken_hard(lwkt_token_t);
452 extern int  lwkt_cnttoken(lwkt_token_t, thread_t);
453 extern int  lwkt_getalltokens(thread_t);
454 extern void lwkt_relalltokens(thread_t);
455 extern void lwkt_drain_token_requests(void);
456 extern void lwkt_token_init(lwkt_token_t, int, const char *);
457 extern void lwkt_token_uninit(lwkt_token_t);
458
459 extern void lwkt_token_pool_init(void);
460 extern lwkt_token_t lwkt_token_pool_lookup(void *);
461 extern lwkt_token_t lwkt_getpooltoken(void *);
462 extern void lwkt_relpooltoken(void *);
463
464 extern void lwkt_setpri(thread_t, int);
465 extern void lwkt_setpri_initial(thread_t, int);
466 extern void lwkt_setpri_self(int);
467 extern void lwkt_fairq_schedulerclock(thread_t td);
468 extern void lwkt_fairq_setpri_self(int pri);
469 extern int lwkt_fairq_push(int pri);
470 extern void lwkt_fairq_pop(int pri);
471 extern void lwkt_fairq_yield(void);
472 extern void lwkt_setcpu_self(struct globaldata *);
473 extern void lwkt_migratecpu(int);
474
475 #ifdef SMP
476
477 extern void lwkt_giveaway(struct thread *);
478 extern void lwkt_acquire(struct thread *);
479 extern int  lwkt_send_ipiq3(struct globaldata *, ipifunc3_t, void *, int);
480 extern int  lwkt_send_ipiq3_passive(struct globaldata *, ipifunc3_t,
481                                     void *, int);
482 extern int  lwkt_send_ipiq3_nowait(struct globaldata *, ipifunc3_t,
483                                    void *, int);
484 extern int  lwkt_send_ipiq3_bycpu(int, ipifunc3_t, void *, int);
485 extern int  lwkt_send_ipiq3_mask(cpumask_t, ipifunc3_t, void *, int);
486 extern void lwkt_wait_ipiq(struct globaldata *, int);
487 extern int  lwkt_seq_ipiq(struct globaldata *);
488 extern void lwkt_process_ipiq(void);
489 extern void lwkt_process_ipiq_frame(struct intrframe *);
490 extern void lwkt_smp_stopped(void);
491 extern void lwkt_synchronize_ipiqs(const char *);
492
493 #endif /* SMP */
494
495 /* lwkt_cpusync_init() - inline function in sys/thread2.h */
496 extern void lwkt_cpusync_simple(cpumask_t, cpusync_func_t, void *);
497 extern void lwkt_cpusync_interlock(lwkt_cpusync_t);
498 extern void lwkt_cpusync_deinterlock(lwkt_cpusync_t);
499
500 extern void crit_panic(void) __dead2;
501 extern struct lwp *lwkt_preempted_proc(void);
502
503 extern int  lwkt_create (void (*func)(void *), void *, struct thread **,
504                 struct thread *, int, int,
505                 const char *, ...) __printflike(7, 8);
506 extern void lwkt_exit (void) __dead2;
507 extern void lwkt_remove_tdallq (struct thread *);
508
509 #endif
510
511 #endif
512