1 /* Copyright (c) 2010-13 The Regents of the University of California
2 * Barret Rhoden <brho@cs.berkeley.edu>
3 * See LICENSE for details.
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. */
9 #ifndef ROS_KERN_KTHREAD_H
10 #define ROS_KERN_KTHREAD_H
12 #include <ros/common.h>
14 #include <sys/queue.h>
21 TAILQ_HEAD(kthread_tailq, kthread);
22 TAILQ_HEAD(semaphore_tailq, semaphore);
24 #define GENBUF_SZ 128 /* plan9 uses this as a scratch space, per syscall */
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. */
30 struct jmpbuf context;
34 void *errbuf; /* TODO: avoiding include loops */
35 TAILQ_ENTRY(kthread) link;
36 /* ID, other shit, etc */
37 bool is_ktask; /* default is FALSE */
39 char generic_buf[GENBUF_SZ];
40 struct systrace_record *trace;
43 /* Semaphore for kthreads to sleep on. 0 or less means you need to sleep */
45 struct kthread_tailq waiters;
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;
60 spinlock_t *lock; /* usually points to internal_ */
61 spinlock_t internal_lock;
62 unsigned long nr_waiters;
66 struct cv_lookup_elm {
67 TAILQ_ENTRY(cv_lookup_elm) link;
68 TAILQ_ENTRY(cv_lookup_elm) abortall_link; /* only used in abort_all */
70 struct kthread *kthread;
73 atomic_t abort_in_progress; /* 0 = no */
75 TAILQ_HEAD(cv_lookup_tailq, cv_lookup_elm);
77 uintptr_t get_kstack(void);
78 void put_kstack(uintptr_t stacktop);
79 uintptr_t *kstack_bottom_addr(uintptr_t stacktop);
80 void kthread_init(void);
81 struct kthread *__kthread_zalloc(void);
82 void restart_kthread(struct kthread *kthread);
83 void kthread_runnable(struct kthread *kthread);
84 void kthread_yield(void);
85 void kthread_usleep(uint64_t usec);
86 void ktask(char *name, void (*fn)(void*), void *arg);
88 void check_poison(char *msg);
90 void sem_init(struct semaphore *sem, int signals);
91 void sem_init_irqsave(struct semaphore *sem, int signals);
92 bool sem_trydown(struct semaphore *sem);
93 void sem_down(struct semaphore *sem);
94 bool sem_up(struct semaphore *sem);
95 bool sem_trydown_irqsave(struct semaphore *sem, int8_t *irq_state);
96 void sem_down_irqsave(struct semaphore *sem, int8_t *irq_state);
97 bool sem_up_irqsave(struct semaphore *sem, int8_t *irq_state);
98 void print_sem_info(struct semaphore *sem);
99 void print_all_sem_info(void);
101 void cv_init(struct cond_var *cv);
102 void cv_init_irqsave(struct cond_var *cv);
103 void cv_init_with_lock(struct cond_var *cv, spinlock_t *lock);
104 void cv_init_irqsave_with_lock(struct cond_var *cv, spinlock_t *lock);
105 void cv_lock(struct cond_var *cv);
106 void cv_unlock(struct cond_var *cv);
107 void cv_lock_irqsave(struct cond_var *cv, int8_t *irq_state);
108 void cv_unlock_irqsave(struct cond_var *cv, int8_t *irq_state);
109 void cv_wait_and_unlock(struct cond_var *cv); /* does not mess with irqs */
110 void cv_wait(struct cond_var *cv);
111 void __cv_signal(struct cond_var *cv);
112 void __cv_broadcast(struct cond_var *cv);
113 void cv_signal(struct cond_var *cv);
114 void cv_broadcast(struct cond_var *cv);
115 void cv_signal_irqsave(struct cond_var *cv, int8_t *irq_state);
116 void cv_broadcast_irqsave(struct cond_var *cv, int8_t *irq_state);
118 bool abort_sysc(struct proc *p, struct syscall *sysc);
119 void abort_all_sysc(struct proc *p);
120 int abort_all_sysc_fd(struct proc *p, int fd);
121 void __reg_abortable_cv(struct cv_lookup_elm *cle, struct cond_var *cv);
122 void dereg_abortable_cv(struct cv_lookup_elm *cle);
123 bool should_abort(struct cv_lookup_elm *cle);
125 /* qlocks are plan9's binary sempahore, which are wrappers around our sems */
126 typedef struct semaphore qlock_t;
127 #define qlock_init(x) sem_init((x), 1)
128 #define qlock(x) sem_down(x)
129 #define qunlock(x) sem_up(x)
130 #define canqlock(x) sem_trydown(x)
132 #endif /* ROS_KERN_KTHREAD_H */