Define a set of default flags for kthreads
[akaros.git] / kern / include / kthread.h
1 /* Copyright (c) 2010-13 The Regents of the University of California
2  * Barret Rhoden <brho@cs.berkeley.edu>
3  * See LICENSE for details.
4  *
5  * Kernel threading.  These are for blocking within the kernel for whatever
6  * reason, usually during blocking IO operations.  Check out
7  * Documentation/kthreads.txt for more info than you care about. */
8
9 #pragma once
10
11 #include <ros/common.h>
12 #include <trap.h>
13 #include <sys/queue.h>
14 #include <atomic.h>
15 #include <setjmp.h>
16
17 struct errbuf {
18         struct jmpbuf jmpbuf;
19 };
20
21 struct proc;
22 struct kthread;
23 struct semaphore;
24 TAILQ_HEAD(kthread_tailq, kthread);
25 TAILQ_HEAD(semaphore_tailq, semaphore);
26
27 #define GENBUF_SZ 128   /* plan9 uses this as a scratch space, per syscall */
28
29 #define KTH_IS_KTASK                    (1 << 0)
30 #define KTH_KTASK_FLAGS                 (KTH_IS_KTASK)
31 #define KTH_DEFAULT_FLAGS (0)
32
33 /* This captures the essence of a kernel context that we want to suspend.  When
34  * a kthread is running, we make sure its stacktop is the default kernel stack,
35  * meaning it will receive the interrupts from userspace. */
36 struct kthread {
37         struct jmpbuf                           context;
38         uintptr_t                                       stacktop;
39         struct proc                                     *proc;
40         struct syscall                          *sysc;
41         struct errbuf                           *errbuf;
42         TAILQ_ENTRY(kthread)            link;
43         /* ID, other shit, etc */
44         int                                                     flags;
45         char                                            *name;
46         char                                            generic_buf[GENBUF_SZ];
47         struct systrace_record          *trace;
48 };
49
50 /* Semaphore for kthreads to sleep on.  0 or less means you need to sleep */
51 struct semaphore {
52         struct kthread_tailq            waiters;
53         int                                             nr_signals;
54         spinlock_t                                      lock;
55         bool                                            irq_okay;
56 #ifdef CONFIG_SEMAPHORE_DEBUG
57         TAILQ_ENTRY(semaphore)          link;
58         bool                                            is_on_list;     /* would like better sys/queue.h */
59         uintptr_t                                       bt_pc;          /* program counter of last down */
60         uintptr_t                                       bt_fp;          /* frame pointer of last down */
61         uint32_t                                        calling_core;
62 #endif
63 };
64
65 /* omitted elements (the sem debug stuff) are initialized to 0 */
66 #define SEMAPHORE_INITIALIZER(name, n)                                         \
67 {                                                                              \
68     .waiters    = TAILQ_HEAD_INITIALIZER((name).waiters),                      \
69         .nr_signals = (n),                                                         \
70     .lock       = SPINLOCK_INITIALIZER,                                        \
71     .irq_okay   = FALSE,                                                       \
72 }
73
74 #define SEMAPHORE_INITIALIZER_IRQSAVE(name, n)                                 \
75 {                                                                              \
76     .waiters    = TAILQ_HEAD_INITIALIZER((name).waiters),                      \
77         .nr_signals = (n),                                                         \
78     .lock       = SPINLOCK_INITIALIZER_IRQSAVE,                                \
79     .irq_okay   = TRUE,                                                        \
80 }
81
82 struct cond_var {
83         struct semaphore                        sem;
84         spinlock_t                                      *lock;          /* usually points to internal_ */
85         spinlock_t                                      internal_lock;
86         unsigned long                           nr_waiters;
87         bool                                            irq_okay;
88 };
89
90 struct cv_lookup_elm {
91         TAILQ_ENTRY(cv_lookup_elm)      link;
92         TAILQ_ENTRY(cv_lookup_elm)      abortall_link;          /* only used in abort_all */
93         struct cond_var                         *cv;
94         struct kthread                          *kthread;
95         struct syscall                          *sysc;
96         struct proc                                     *proc;
97         atomic_t                                        abort_in_progress;      /* 0 = no */
98 };
99 TAILQ_HEAD(cv_lookup_tailq, cv_lookup_elm);
100
101 uintptr_t get_kstack(void);
102 void put_kstack(uintptr_t stacktop);
103 uintptr_t *kstack_bottom_addr(uintptr_t stacktop);
104 void kthread_init(void);
105 struct kthread *__kthread_zalloc(void);
106 void restart_kthread(struct kthread *kthread);
107 void kthread_runnable(struct kthread *kthread);
108 void kthread_yield(void);
109 void kthread_usleep(uint64_t usec);
110 void ktask(char *name, void (*fn)(void*), void *arg);
111
112 static inline bool is_ktask(struct kthread *kthread)
113 {
114         return kthread->flags & KTH_IS_KTASK;
115 }
116
117 /* Debugging */
118 void check_poison(char *msg);
119
120 void sem_init(struct semaphore *sem, int signals);
121 void sem_init_irqsave(struct semaphore *sem, int signals);
122 bool sem_trydown(struct semaphore *sem);
123 void sem_down(struct semaphore *sem);
124 bool sem_up(struct semaphore *sem);
125 bool sem_trydown_irqsave(struct semaphore *sem, int8_t *irq_state);
126 void sem_down_irqsave(struct semaphore *sem, int8_t *irq_state);
127 bool sem_up_irqsave(struct semaphore *sem, int8_t *irq_state);
128 void print_sem_info(struct semaphore *sem);
129 void print_all_sem_info(void);
130
131 void cv_init(struct cond_var *cv);
132 void cv_init_irqsave(struct cond_var *cv);
133 void cv_init_with_lock(struct cond_var *cv, spinlock_t *lock);
134 void cv_init_irqsave_with_lock(struct cond_var *cv, spinlock_t *lock);
135 void cv_lock(struct cond_var *cv);
136 void cv_unlock(struct cond_var *cv);
137 void cv_lock_irqsave(struct cond_var *cv, int8_t *irq_state);
138 void cv_unlock_irqsave(struct cond_var *cv, int8_t *irq_state);
139 void cv_wait_and_unlock(struct cond_var *cv);   /* does not mess with irqs */
140 void cv_wait(struct cond_var *cv);
141 void __cv_signal(struct cond_var *cv);
142 void __cv_broadcast(struct cond_var *cv);
143 void cv_signal(struct cond_var *cv);
144 void cv_broadcast(struct cond_var *cv);
145 void cv_signal_irqsave(struct cond_var *cv, int8_t *irq_state);
146 void cv_broadcast_irqsave(struct cond_var *cv, int8_t *irq_state);
147
148 bool abort_sysc(struct proc *p, struct syscall *sysc);
149 void abort_all_sysc(struct proc *p);
150 int abort_all_sysc_fd(struct proc *p, int fd);
151 void __reg_abortable_cv(struct cv_lookup_elm *cle, struct cond_var *cv);
152 void dereg_abortable_cv(struct cv_lookup_elm *cle);
153 bool should_abort(struct cv_lookup_elm *cle);
154
155 /* qlocks are plan9's binary sempahore, which are wrappers around our sems.
156  * Not sure if they'll need irqsave or normal sems. */
157 typedef struct semaphore qlock_t;
158 #define qlock_init(x) sem_init((x), 1)
159 #define qlock(x) sem_down(x)
160 #define qunlock(x) sem_up(x)
161 #define canqlock(x) sem_trydown(x)
162 #define QLOCK_INITIALIZER(name) SEMAPHORE_INITIALIZER(name, 1)