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