kth: Remove irq_okay from sems and CVs
[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 <ros/syscall.h>
13 #include <trap.h>
14 #include <sys/queue.h>
15 #include <atomic.h>
16 #include <setjmp.h>
17
18 struct errbuf {
19         struct jmpbuf jmpbuf;
20 };
21
22 struct proc;
23 struct kthread;
24 struct kth_db_info;
25 TAILQ_HEAD(kthread_tailq, kthread);
26 TAILQ_HEAD(kth_db_tailq, kth_db_info);
27
28 #define GENBUF_SZ 128   /* plan9 uses this as a scratch space, per syscall */
29
30 #define KTH_IS_KTASK                    (1 << 0)
31 #define KTH_SAVE_ADDR_SPACE             (1 << 1)
32 #define KTH_IS_RCU_KTASK                (1 << 2)
33
34 /* These flag sets are for toggling between ktasks and default/process ktasks */
35 /* These are the flags for *any* ktask */
36 #define KTH_KTASK_FLAGS                 (KTH_IS_KTASK)
37 /* These are the flags used for normal process context */
38 #define KTH_DEFAULT_FLAGS               (KTH_SAVE_ADDR_SPACE)
39
40 /* This captures the essence of a kernel context that we want to suspend.  When
41  * a kthread is running, we make sure its stacktop is the default kernel stack,
42  * meaning it will receive the interrupts from userspace. */
43 struct kthread {
44         struct jmpbuf                           context;
45         uintptr_t                                       stacktop;
46         struct proc                                     *proc;
47         struct syscall                          *sysc;
48         struct errbuf                           *errbuf;
49         TAILQ_ENTRY(kthread)            link;
50         /* ID, other shit, etc */
51         int                                                     flags;
52         char                                            *name;
53         char                                            generic_buf[GENBUF_SZ];
54         int                                                     errno;
55         char                                            errstr[MAX_ERRSTR_LEN];
56         struct systrace_record          *strace;
57 };
58
59 #define KTH_DB_SEM                      1
60 #define KTH_DB_CV                       2
61
62 struct kth_db_info {
63         TAILQ_ENTRY(kth_db_info)        link;
64         unsigned int                            type;
65         bool                                            on_list;
66 };
67
68 /* Semaphore for kthreads to sleep on.  0 or less means you need to sleep */
69 struct semaphore {
70 #ifdef CONFIG_SEMAPHORE_DEBUG
71         struct kth_db_info                      db;
72 #endif
73         struct kthread_tailq            waiters;
74         int                                             nr_signals;
75         spinlock_t                                      lock;
76 };
77
78 #ifdef CONFIG_SEMAPHORE_DEBUG
79 #define KTH_DB_INIT .db         = { .type = KTH_DB_SEM },
80 #else
81 #define KTH_DB_INIT
82 #endif
83
84 #define SEMAPHORE_INITIALIZER(name, n)                                         \
85 {                                                                              \
86     .waiters    = TAILQ_HEAD_INITIALIZER((name).waiters),                      \
87         .nr_signals = (n),                                                         \
88     .lock       = SPINLOCK_INITIALIZER,                                        \
89         KTH_DB_INIT                                                                \
90 }
91
92 #define SEMAPHORE_INITIALIZER_IRQSAVE(name, n)                                 \
93 {                                                                              \
94     .waiters    = TAILQ_HEAD_INITIALIZER((name).waiters),                      \
95         .nr_signals = (n),                                                         \
96     .lock       = SPINLOCK_INITIALIZER_IRQSAVE,                                \
97         KTH_DB_INIT                                                                \
98 }
99
100 struct cond_var {
101 #ifdef CONFIG_SEMAPHORE_DEBUG
102         struct kth_db_info                      db;
103 #endif
104         struct kthread_tailq            waiters;
105         spinlock_t                                      *lock;          /* usually points to internal_ */
106         spinlock_t                                      internal_lock;
107         unsigned long                           nr_waiters;
108 };
109
110 struct cv_lookup_elm {
111         TAILQ_ENTRY(cv_lookup_elm)      link;
112         TAILQ_ENTRY(cv_lookup_elm)      abortall_link;          /* only used in abort_all */
113         struct cond_var                         *cv;
114         struct kthread                          *kthread;
115         struct syscall                          *sysc;
116         struct proc                                     *proc;
117         atomic_t                                        abort_in_progress;      /* 0 = no */
118 };
119 TAILQ_HEAD(cv_lookup_tailq, cv_lookup_elm);
120
121 uintptr_t get_kstack(void);
122 void put_kstack(uintptr_t stacktop);
123 uintptr_t *kstack_bottom_addr(uintptr_t stacktop);
124 void kthread_init(void);
125 struct kthread *__kthread_zalloc(void);
126 void __use_real_kstack(void (*f)(void *arg));
127 void restart_kthread(struct kthread *kthread);
128 void kthread_runnable(struct kthread *kthread);
129 void kthread_yield(void);
130 void kthread_usleep(uint64_t usec);
131 void ktask(char *name, void (*fn)(void*), void *arg);
132
133 static inline bool is_ktask(struct kthread *kthread)
134 {
135         return kthread->flags & KTH_IS_KTASK;
136 }
137
138 static inline bool is_rcu_ktask(struct kthread *kthread)
139 {
140         return kthread->flags & KTH_IS_RCU_KTASK;
141 }
142
143 void sem_init(struct semaphore *sem, int signals);
144 void sem_init_irqsave(struct semaphore *sem, int signals);
145 bool sem_trydown_bulk(struct semaphore *sem, int nr_signals);
146 bool sem_trydown(struct semaphore *sem);
147 void sem_down_bulk(struct semaphore *sem, int nr_signals);
148 void sem_down(struct semaphore *sem);
149 bool sem_up(struct semaphore *sem);
150 bool sem_trydown_bulk_irqsave(struct semaphore *sem, int nr_signals,
151                               int8_t *irq_state);
152 bool sem_trydown_irqsave(struct semaphore *sem, int8_t *irq_state);
153 void sem_down_bulk_irqsave(struct semaphore *sem, int nr_signals,
154                            int8_t *irq_state);
155 void sem_down_irqsave(struct semaphore *sem, int8_t *irq_state);
156 bool sem_up_irqsave(struct semaphore *sem, int8_t *irq_state);
157 void print_db_blk_info(pid_t pid);
158
159 void cv_init(struct cond_var *cv);
160 void cv_init_irqsave(struct cond_var *cv);
161 void cv_init_with_lock(struct cond_var *cv, spinlock_t *lock);
162 void cv_init_irqsave_with_lock(struct cond_var *cv, spinlock_t *lock);
163 void cv_lock(struct cond_var *cv);
164 void cv_unlock(struct cond_var *cv);
165 void cv_lock_irqsave(struct cond_var *cv, int8_t *irq_state);
166 void cv_unlock_irqsave(struct cond_var *cv, int8_t *irq_state);
167 void cv_wait_and_unlock(struct cond_var *cv);   /* does not mess with irqs */
168 void cv_wait(struct cond_var *cv);
169 void __cv_signal(struct cond_var *cv);
170 void __cv_broadcast(struct cond_var *cv);
171 void cv_signal(struct cond_var *cv);
172 void cv_broadcast(struct cond_var *cv);
173 void cv_signal_irqsave(struct cond_var *cv, int8_t *irq_state);
174 void cv_broadcast_irqsave(struct cond_var *cv, int8_t *irq_state);
175
176 bool abort_sysc(struct proc *p, struct syscall *sysc);
177 void abort_all_sysc(struct proc *p);
178 int abort_all_sysc_fd(struct proc *p, int fd);
179 void __reg_abortable_cv(struct cv_lookup_elm *cle, struct cond_var *cv);
180 void dereg_abortable_cv(struct cv_lookup_elm *cle);
181 bool should_abort(struct cv_lookup_elm *cle);
182
183 uintptr_t switch_to_ktask(void);
184 void switch_back_from_ktask(uintptr_t old_ret);
185
186 /* qlocks are plan9's binary sempahore, which are wrappers around our sems.
187  * Not sure if they'll need irqsave or normal sems. */
188 typedef struct semaphore qlock_t;
189 #define qlock_init(x) sem_init((x), 1)
190 #define qlock(x) sem_down(x)
191 #define qunlock(x) sem_up(x)
192 #define canqlock(x) sem_trydown(x)
193 #define QLOCK_INITIALIZER(name) SEMAPHORE_INITIALIZER(name, 1)