Add EPLAN9 to the error list (XCC)
[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 semaphore;
25 TAILQ_HEAD(kthread_tailq, kthread);
26 TAILQ_HEAD(semaphore_tailq, semaphore);
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 /* Semaphore for kthreads to sleep on.  0 or less means you need to sleep */
60 struct semaphore {
61         struct kthread_tailq            waiters;
62         int                                             nr_signals;
63         spinlock_t                                      lock;
64         bool                                            irq_okay;
65 #ifdef CONFIG_SEMAPHORE_DEBUG
66         TAILQ_ENTRY(semaphore)          link;
67         bool                                            is_on_list;     /* would like better sys/queue.h */
68 #endif
69 };
70
71 /* omitted elements (the sem debug stuff) are initialized to 0 */
72 #define SEMAPHORE_INITIALIZER(name, n)                                         \
73 {                                                                              \
74     .waiters    = TAILQ_HEAD_INITIALIZER((name).waiters),                      \
75         .nr_signals = (n),                                                         \
76     .lock       = SPINLOCK_INITIALIZER,                                        \
77     .irq_okay   = FALSE,                                                       \
78 }
79
80 #define SEMAPHORE_INITIALIZER_IRQSAVE(name, n)                                 \
81 {                                                                              \
82     .waiters    = TAILQ_HEAD_INITIALIZER((name).waiters),                      \
83         .nr_signals = (n),                                                         \
84     .lock       = SPINLOCK_INITIALIZER_IRQSAVE,                                \
85     .irq_okay   = TRUE,                                                        \
86 }
87
88 struct cond_var {
89         struct semaphore                        sem;
90         spinlock_t                                      *lock;          /* usually points to internal_ */
91         spinlock_t                                      internal_lock;
92         unsigned long                           nr_waiters;
93         bool                                            irq_okay;
94 };
95
96 struct cv_lookup_elm {
97         TAILQ_ENTRY(cv_lookup_elm)      link;
98         TAILQ_ENTRY(cv_lookup_elm)      abortall_link;          /* only used in abort_all */
99         struct cond_var                         *cv;
100         struct kthread                          *kthread;
101         struct syscall                          *sysc;
102         struct proc                                     *proc;
103         atomic_t                                        abort_in_progress;      /* 0 = no */
104 };
105 TAILQ_HEAD(cv_lookup_tailq, cv_lookup_elm);
106
107 uintptr_t get_kstack(void);
108 void put_kstack(uintptr_t stacktop);
109 uintptr_t *kstack_bottom_addr(uintptr_t stacktop);
110 void kthread_init(void);
111 struct kthread *__kthread_zalloc(void);
112 void __use_real_kstack(void (*f)(void *arg));
113 void restart_kthread(struct kthread *kthread);
114 void kthread_runnable(struct kthread *kthread);
115 void kthread_yield(void);
116 void kthread_usleep(uint64_t usec);
117 void ktask(char *name, void (*fn)(void*), void *arg);
118
119 static inline bool is_ktask(struct kthread *kthread)
120 {
121         return kthread->flags & KTH_IS_KTASK;
122 }
123
124 static inline bool is_rcu_ktask(struct kthread *kthread)
125 {
126         return kthread->flags & KTH_IS_RCU_KTASK;
127 }
128
129 void sem_init(struct semaphore *sem, int signals);
130 void sem_init_irqsave(struct semaphore *sem, int signals);
131 bool sem_trydown_bulk(struct semaphore *sem, int nr_signals);
132 bool sem_trydown(struct semaphore *sem);
133 void sem_down_bulk(struct semaphore *sem, int nr_signals);
134 void sem_down(struct semaphore *sem);
135 bool sem_up(struct semaphore *sem);
136 bool sem_trydown_bulk_irqsave(struct semaphore *sem, int nr_signals,
137                               int8_t *irq_state);
138 bool sem_trydown_irqsave(struct semaphore *sem, int8_t *irq_state);
139 void sem_down_bulk_irqsave(struct semaphore *sem, int nr_signals,
140                            int8_t *irq_state);
141 void sem_down_irqsave(struct semaphore *sem, int8_t *irq_state);
142 bool sem_up_irqsave(struct semaphore *sem, int8_t *irq_state);
143 void print_all_sem_info(pid_t pid);
144
145 void cv_init(struct cond_var *cv);
146 void cv_init_irqsave(struct cond_var *cv);
147 void cv_init_with_lock(struct cond_var *cv, spinlock_t *lock);
148 void cv_init_irqsave_with_lock(struct cond_var *cv, spinlock_t *lock);
149 void cv_lock(struct cond_var *cv);
150 void cv_unlock(struct cond_var *cv);
151 void cv_lock_irqsave(struct cond_var *cv, int8_t *irq_state);
152 void cv_unlock_irqsave(struct cond_var *cv, int8_t *irq_state);
153 void cv_wait_and_unlock(struct cond_var *cv);   /* does not mess with irqs */
154 void cv_wait(struct cond_var *cv);
155 void __cv_signal(struct cond_var *cv);
156 void __cv_broadcast(struct cond_var *cv);
157 void cv_signal(struct cond_var *cv);
158 void cv_broadcast(struct cond_var *cv);
159 void cv_signal_irqsave(struct cond_var *cv, int8_t *irq_state);
160 void cv_broadcast_irqsave(struct cond_var *cv, int8_t *irq_state);
161
162 bool abort_sysc(struct proc *p, struct syscall *sysc);
163 void abort_all_sysc(struct proc *p);
164 int abort_all_sysc_fd(struct proc *p, int fd);
165 void __reg_abortable_cv(struct cv_lookup_elm *cle, struct cond_var *cv);
166 void dereg_abortable_cv(struct cv_lookup_elm *cle);
167 bool should_abort(struct cv_lookup_elm *cle);
168
169 uintptr_t switch_to_ktask(void);
170 void switch_back_from_ktask(uintptr_t old_ret);
171
172 /* qlocks are plan9's binary sempahore, which are wrappers around our sems.
173  * Not sure if they'll need irqsave or normal sems. */
174 typedef struct semaphore qlock_t;
175 #define qlock_init(x) sem_init((x), 1)
176 #define qlock(x) sem_down(x)
177 #define qunlock(x) sem_up(x)
178 #define canqlock(x) sem_trydown(x)
179 #define QLOCK_INITIALIZER(name) SEMAPHORE_INITIALIZER(name, 1)