parlib: Add uthread_create()
[akaros.git] / user / parlib / thread0_sched.c
1 /* Copyright (c) 2015 Google, Inc.
2  * Barret Rhoden <brho@cs.berkeley.edu>
3  * See LICENSE for details.
4  *
5  * thread0_sched: a basic scheduler for thread0, used by SCPs without a
6  * multithreaded 2LS linked in.
7  *
8  * This is closely coupled with uthread.c */
9
10 #include <ros/arch/membar.h>
11 #include <parlib/arch/atomic.h>
12 #include <parlib/parlib.h>
13 #include <parlib/vcore.h>
14 #include <parlib/uthread.h>
15 #include <parlib/event.h>
16 #include <parlib/arch/trap.h>
17 #include <stdlib.h>
18
19 static void thread0_sched_entry(void);
20 static void thread0_thread_blockon_sysc(struct uthread *uthread, void *sysc);
21 static void thread0_thread_refl_fault(struct uthread *uth,
22                                       struct user_context *ctx);
23 static void thread0_thread_runnable(struct uthread *uth);
24 static void thread0_thread_has_blocked(struct uthread *uth, uth_sync_t sync,
25                                        int flags);
26 static void thread0_thread_exited(struct uthread *uth);
27 static struct uthread *thread0_thread_create(void *(*func)(void *), void *arg);
28 static uth_sync_t thread0_sync_alloc(void);
29 static void thread0_sync_free(uth_sync_t);
30 static struct uthread *thread0_sync_get_next(uth_sync_t);
31 static bool thread0_sync_get_uth(uth_sync_t s, struct uthread *uth);
32
33 /* externed into uthread.c */
34 struct schedule_ops thread0_2ls_ops = {
35         .sched_entry = thread0_sched_entry,
36         .thread_blockon_sysc = thread0_thread_blockon_sysc,
37         .thread_refl_fault = thread0_thread_refl_fault,
38         .thread_runnable = thread0_thread_runnable,
39         .thread_paused = thread0_thread_runnable,
40         .thread_has_blocked = thread0_thread_has_blocked,
41         .thread_exited = thread0_thread_exited,
42         .thread_create = thread0_thread_create,
43         .sync_alloc = thread0_sync_alloc,
44         .sync_free = thread0_sync_free,
45         .sync_get_next = thread0_sync_get_next,
46         .sync_get_uth = thread0_sync_get_uth,
47 };
48
49 /* externed into uthread.c */
50 struct uthread *thread0_uth;
51
52 /* Our thread0 is actually allocated in uthread as just a struct uthread, so we
53  * don't actually attach this mgmt info to it.  But since we just have one
54  * thread, it doesn't matter. */
55 struct thread0_info {
56         bool                                            is_blocked;
57 };
58 static struct thread0_info thread0_info;
59 static struct event_queue *sysc_evq;
60
61 void thread0_handle_syscall(struct event_msg *ev_msg,
62                             unsigned int ev_type, void *data)
63 {
64         thread0_info.is_blocked = FALSE;
65 }
66
67 void thread0_lib_init(void)
68 {
69         memset(&thread0_info, 0, sizeof(thread0_info));
70         /* we don't care about the message, so don't bother with a UCQ */
71         sysc_evq = get_eventq(EV_MBOX_BITMAP);
72         sysc_evq->ev_flags = EVENT_INDIR | EVENT_WAKEUP;
73 }
74
75 /* Thread0 scheduler ops (for processes that haven't linked in a full 2LS) */
76 static void thread0_sched_entry(void)
77 {
78         /* TODO: support signal handling whenever we run a uthread */
79         if (current_uthread) {
80                 uthread_prep_pending_signals(current_uthread);
81                 run_current_uthread();
82                 assert(0);
83         }
84         while (1) {
85                 if (!thread0_info.is_blocked) {
86                         uthread_prep_pending_signals(thread0_uth);
87                         run_uthread(thread0_uth);
88                         assert(0);
89                 }
90                 sys_yield(FALSE);
91                 handle_events(0);
92         }
93 }
94
95 static void thread0_thread_blockon_sysc(struct uthread *uthread, void *arg)
96 {
97         struct syscall *sysc = (struct syscall*)arg;
98         thread0_thread_has_blocked(uthread, NULL, 0);
99         if (!register_evq(sysc, sysc_evq))
100                 thread0_thread_runnable(uthread);
101 }
102
103 static void refl_error(struct uthread *uth, unsigned int trap_nr,
104                        unsigned int err, unsigned long aux)
105 {
106         printf("Thread has unhandled fault: %d, err: %d, aux: %p\n",
107                trap_nr, err, aux);
108         /* Note that uthread.c already copied out our ctx into the uth
109          * struct */
110         print_user_context(&uth->u_ctx);
111         printf("Turn on printx to spew unhandled, malignant trap info\n");
112         exit(-1);
113 }
114
115 static bool handle_page_fault(struct uthread *uth, unsigned int err,
116                               unsigned long aux)
117 {
118         if (!(err & PF_VMR_BACKED))
119                 return FALSE;
120         syscall_async(&uth->local_sysc, SYS_populate_va, aux, 1);
121         __block_uthread_on_async_sysc(uth);
122         return TRUE;
123 }
124
125 static void thread0_thread_refl_fault(struct uthread *uth,
126                                       struct user_context *ctx)
127 {
128         unsigned int trap_nr = __arch_refl_get_nr(ctx);
129         unsigned int err = __arch_refl_get_err(ctx);
130         unsigned long aux = __arch_refl_get_aux(ctx);
131
132         assert(ctx->type == ROS_HW_CTX);
133         switch (trap_nr) {
134         case HW_TRAP_PAGE_FAULT:
135                 if (!handle_page_fault(uth, err, aux))
136                         refl_error(uth, trap_nr, err, aux);
137                 break;
138         default:
139                 refl_error(uth, trap_nr, err, aux);
140         }
141 }
142
143 static void thread0_thread_runnable(struct uthread *uth)
144 {
145         thread0_info.is_blocked = FALSE;
146 }
147
148 static void thread0_thread_has_blocked(struct uthread *uth, uth_sync_t sync,
149                                        int flags)
150 {
151         assert(!thread0_info.is_blocked);
152         thread0_info.is_blocked = TRUE;
153 }
154
155 /* Actually, a 2LS only needs to implement this if it calls
156  * uth_2ls_thread_exit().  Keep it here to catch bugs. */
157 static void thread0_thread_exited(struct uthread *uth)
158 {
159         assert(0);
160 }
161
162 static struct uthread *thread0_thread_create(void *(*func)(void *), void *arg)
163 {
164         panic("Thread0 sched asked to create more threads!");
165 }
166
167 static uth_sync_t thread0_sync_alloc(void)
168 {
169         return (void*)0xf00baa;
170 }
171
172 static void thread0_sync_free(uth_sync_t s)
173 {
174 }
175
176 static struct uthread *thread0_sync_get_next(uth_sync_t s)
177 {
178         if (thread0_info.is_blocked) {
179                 /* Note we don't clear is_blocked.  Runnable does that, which should be
180                  * called before the next get_next (since we have only one thread). */
181                 return thread0_uth;
182         } else {
183                 return NULL;
184         }
185 }
186
187 static bool thread0_sync_get_uth(uth_sync_t s, struct uthread *uth)
188 {
189         assert(uth == thread0_uth);
190         if (thread0_info.is_blocked) {
191                 /* Note we don't clear is_blocked.  Runnable does that. */
192                 return TRUE;
193         }
194         return FALSE;
195 }