Avoid void* error buffer declaration
[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 #ifndef ROS_KERN_KTHREAD_H
10 #define ROS_KERN_KTHREAD_H
11
12 #include <ros/common.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 /* This captures the essence of a kernel context that we want to suspend.  When
31  * a kthread is running, we make sure its stacktop is the default kernel stack,
32  * meaning it will receive the interrupts from userspace. */
33 struct kthread {
34         struct jmpbuf                           context;
35         uintptr_t                                       stacktop;
36         struct proc                                     *proc;
37         struct syscall                          *sysc;
38         struct errbuf                           *errbuf;
39         TAILQ_ENTRY(kthread)            link;
40         /* ID, other shit, etc */
41         bool                                            is_ktask;       /* default is FALSE */
42         char                                            *name;
43         char                                            generic_buf[GENBUF_SZ];
44         struct systrace_record          *trace;
45 };
46
47 /* Semaphore for kthreads to sleep on.  0 or less means you need to sleep */
48 struct semaphore {
49         struct kthread_tailq            waiters;
50         int                                             nr_signals;
51         spinlock_t                                      lock;
52         bool                                            irq_okay;
53 #ifdef CONFIG_SEMAPHORE_DEBUG
54         TAILQ_ENTRY(semaphore)          link;
55         bool                                            is_on_list;     /* would like better sys/queue.h */
56         uintptr_t                                       bt_pc;          /* program counter of last down */
57         uintptr_t                                       bt_fp;          /* frame pointer of last down */
58         uint32_t                                        calling_core;
59 #endif
60 };
61
62 /* omitted elements (the sem debug stuff) are initialized to 0 */
63 #define SEMAPHORE_INITIALIZER(name, n)                                         \
64 {                                                                              \
65     .waiters    = TAILQ_HEAD_INITIALIZER((name).waiters),                      \
66         .nr_signals = (n),                                                         \
67     .lock       = SPINLOCK_INITIALIZER,                                        \
68     .irq_okay   = FALSE,                                                       \
69 }
70
71 #define SEMAPHORE_INITIALIZER_IRQSAVE(name, n)                                 \
72 {                                                                              \
73     .waiters    = TAILQ_HEAD_INITIALIZER((name).waiters),                      \
74         .nr_signals = (n),                                                         \
75     .lock       = SPINLOCK_INITIALIZER_IRQSAVE,                                \
76     .irq_okay   = TRUE,                                                        \
77 }
78
79 struct cond_var {
80         struct semaphore                        sem;
81         spinlock_t                                      *lock;          /* usually points to internal_ */
82         spinlock_t                                      internal_lock;
83         unsigned long                           nr_waiters;
84         bool                                            irq_okay;
85 };
86
87 struct cv_lookup_elm {
88         TAILQ_ENTRY(cv_lookup_elm)      link;
89         TAILQ_ENTRY(cv_lookup_elm)      abortall_link;          /* only used in abort_all */
90         struct cond_var                         *cv;
91         struct kthread                          *kthread;
92         struct syscall                          *sysc;
93         struct proc                                     *proc;
94         atomic_t                                        abort_in_progress;      /* 0 = no */
95 };
96 TAILQ_HEAD(cv_lookup_tailq, cv_lookup_elm);
97
98 uintptr_t get_kstack(void);
99 void put_kstack(uintptr_t stacktop);
100 uintptr_t *kstack_bottom_addr(uintptr_t stacktop);
101 void kthread_init(void);
102 struct kthread *__kthread_zalloc(void);
103 void restart_kthread(struct kthread *kthread);
104 void kthread_runnable(struct kthread *kthread);
105 void kthread_yield(void);
106 void kthread_usleep(uint64_t usec);
107 void ktask(char *name, void (*fn)(void*), void *arg);
108 /* Debugging */
109 void check_poison(char *msg);
110
111 void sem_init(struct semaphore *sem, int signals);
112 void sem_init_irqsave(struct semaphore *sem, int signals);
113 bool sem_trydown(struct semaphore *sem);
114 void sem_down(struct semaphore *sem);
115 bool sem_up(struct semaphore *sem);
116 bool sem_trydown_irqsave(struct semaphore *sem, int8_t *irq_state);
117 void sem_down_irqsave(struct semaphore *sem, int8_t *irq_state);
118 bool sem_up_irqsave(struct semaphore *sem, int8_t *irq_state);
119 void print_sem_info(struct semaphore *sem);
120 void print_all_sem_info(void);
121
122 void cv_init(struct cond_var *cv);
123 void cv_init_irqsave(struct cond_var *cv);
124 void cv_init_with_lock(struct cond_var *cv, spinlock_t *lock);
125 void cv_init_irqsave_with_lock(struct cond_var *cv, spinlock_t *lock);
126 void cv_lock(struct cond_var *cv);
127 void cv_unlock(struct cond_var *cv);
128 void cv_lock_irqsave(struct cond_var *cv, int8_t *irq_state);
129 void cv_unlock_irqsave(struct cond_var *cv, int8_t *irq_state);
130 void cv_wait_and_unlock(struct cond_var *cv);   /* does not mess with irqs */
131 void cv_wait(struct cond_var *cv);
132 void __cv_signal(struct cond_var *cv);
133 void __cv_broadcast(struct cond_var *cv);
134 void cv_signal(struct cond_var *cv);
135 void cv_broadcast(struct cond_var *cv);
136 void cv_signal_irqsave(struct cond_var *cv, int8_t *irq_state);
137 void cv_broadcast_irqsave(struct cond_var *cv, int8_t *irq_state);
138
139 bool abort_sysc(struct proc *p, struct syscall *sysc);
140 void abort_all_sysc(struct proc *p);
141 int abort_all_sysc_fd(struct proc *p, int fd);
142 void __reg_abortable_cv(struct cv_lookup_elm *cle, struct cond_var *cv);
143 void dereg_abortable_cv(struct cv_lookup_elm *cle);
144 bool should_abort(struct cv_lookup_elm *cle);
145
146 /* qlocks are plan9's binary sempahore, which are wrappers around our sems.
147  * Not sure if they'll need irqsave or normal sems. */
148 typedef struct semaphore qlock_t;
149 #define qlock_init(x) sem_init((x), 1)
150 #define qlock(x) sem_down(x)
151 #define qunlock(x) sem_up(x)
152 #define canqlock(x) sem_trydown(x)
153 #define QLOCK_INITIALIZER(name) SEMAPHORE_INITIALIZER(name, 1)
154
155 #endif /* ROS_KERN_KTHREAD_H */