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