1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // kernel.c -- |
---|
8 | // |
---|
9 | // Author : Thierry Delisle |
---|
10 | // Created On : Tue Jan 17 12:27:26 2017 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Mon Apr 9 16:11:46 2018 |
---|
13 | // Update Count : 24 |
---|
14 | // |
---|
15 | |
---|
16 | //C Includes |
---|
17 | #include <stddef.h> |
---|
18 | extern "C" { |
---|
19 | #include <stdio.h> |
---|
20 | #include <fenv.h> |
---|
21 | #include <sys/resource.h> |
---|
22 | #include <signal.h> |
---|
23 | #include <unistd.h> |
---|
24 | } |
---|
25 | |
---|
26 | //CFA Includes |
---|
27 | #include "time" |
---|
28 | #include "kernel_private.h" |
---|
29 | #include "preemption.h" |
---|
30 | #include "startup.h" |
---|
31 | |
---|
32 | //Private includes |
---|
33 | #define __CFA_INVOKE_PRIVATE__ |
---|
34 | #include "invoke.h" |
---|
35 | |
---|
36 | //Start and stop routine for the kernel, declared first to make sure they run first |
---|
37 | void kernel_startup(void) __attribute__(( constructor( STARTUP_PRIORITY_KERNEL ) )); |
---|
38 | void kernel_shutdown(void) __attribute__(( destructor ( STARTUP_PRIORITY_KERNEL ) )); |
---|
39 | |
---|
40 | //----------------------------------------------------------------------------- |
---|
41 | // Kernel storage |
---|
42 | KERNEL_STORAGE(cluster, mainCluster); |
---|
43 | KERNEL_STORAGE(processor, mainProcessor); |
---|
44 | KERNEL_STORAGE(processorCtx_t, mainProcessorCtx); |
---|
45 | KERNEL_STORAGE(thread_desc, mainThread); |
---|
46 | KERNEL_STORAGE(machine_context_t, mainThreadCtx); |
---|
47 | |
---|
48 | cluster * mainCluster; |
---|
49 | processor * mainProcessor; |
---|
50 | thread_desc * mainThread; |
---|
51 | |
---|
52 | //----------------------------------------------------------------------------- |
---|
53 | // Global state |
---|
54 | |
---|
55 | // volatile thread_local bool preemption_in_progress = 0; |
---|
56 | // volatile thread_local bool preemption_enabled = false; |
---|
57 | // volatile thread_local unsigned short disable_preempt_count = 1; |
---|
58 | |
---|
59 | thread_local struct KernelThreadData kernelThreadData = { |
---|
60 | NULL, |
---|
61 | NULL, |
---|
62 | NULL, |
---|
63 | { 1, false, false } |
---|
64 | }; |
---|
65 | |
---|
66 | //----------------------------------------------------------------------------- |
---|
67 | // Main thread construction |
---|
68 | struct current_stack_info_t { |
---|
69 | machine_context_t ctx; |
---|
70 | unsigned int size; // size of stack |
---|
71 | void *base; // base of stack |
---|
72 | void *storage; // pointer to stack |
---|
73 | void *limit; // stack grows towards stack limit |
---|
74 | void *context; // address of cfa_context_t |
---|
75 | void *top; // address of top of storage |
---|
76 | }; |
---|
77 | |
---|
78 | void ?{}( current_stack_info_t & this ) { |
---|
79 | CtxGet( this.ctx ); |
---|
80 | this.base = this.ctx.FP; |
---|
81 | this.storage = this.ctx.SP; |
---|
82 | |
---|
83 | rlimit r; |
---|
84 | getrlimit( RLIMIT_STACK, &r); |
---|
85 | this.size = r.rlim_cur; |
---|
86 | |
---|
87 | this.limit = (void *)(((intptr_t)this.base) - this.size); |
---|
88 | this.context = &storage_mainThreadCtx; |
---|
89 | this.top = this.base; |
---|
90 | } |
---|
91 | |
---|
92 | void ?{}( coStack_t & this, current_stack_info_t * info) with( this ) { |
---|
93 | size = info->size; |
---|
94 | storage = info->storage; |
---|
95 | limit = info->limit; |
---|
96 | base = info->base; |
---|
97 | context = info->context; |
---|
98 | top = info->top; |
---|
99 | userStack = true; |
---|
100 | } |
---|
101 | |
---|
102 | void ?{}( coroutine_desc & this, current_stack_info_t * info) with( this ) { |
---|
103 | stack{ info }; |
---|
104 | name = "Main Thread"; |
---|
105 | errno_ = 0; |
---|
106 | state = Start; |
---|
107 | starter = NULL; |
---|
108 | } |
---|
109 | |
---|
110 | void ?{}( thread_desc & this, current_stack_info_t * info) with( this ) { |
---|
111 | self_cor{ info }; |
---|
112 | curr_cor = &self_cor; |
---|
113 | self_mon.owner = &this; |
---|
114 | self_mon.recursion = 1; |
---|
115 | self_mon_p = &self_mon; |
---|
116 | next = NULL; |
---|
117 | __cfaabi_dbg_debug_do( |
---|
118 | dbg_next = NULL; |
---|
119 | dbg_prev = NULL; |
---|
120 | __cfaabi_dbg_thread_register(&this); |
---|
121 | ) |
---|
122 | |
---|
123 | monitors{ &self_mon_p, 1, (fptr_t)0 }; |
---|
124 | } |
---|
125 | |
---|
126 | //----------------------------------------------------------------------------- |
---|
127 | // Processor coroutine |
---|
128 | void ?{}(processorCtx_t & this) {} |
---|
129 | |
---|
130 | // Construct the processor context of the main processor |
---|
131 | void ?{}(processorCtx_t & this, processor * proc) { |
---|
132 | (this.__cor){ "Processor" }; |
---|
133 | this.__cor.starter = NULL; |
---|
134 | this.proc = proc; |
---|
135 | } |
---|
136 | |
---|
137 | // Construct the processor context of non-main processors |
---|
138 | void ?{}(processorCtx_t & this, processor * proc, current_stack_info_t * info) { |
---|
139 | (this.__cor){ info }; |
---|
140 | this.proc = proc; |
---|
141 | } |
---|
142 | |
---|
143 | void ?{}(processor & this) { |
---|
144 | this{ mainCluster }; |
---|
145 | } |
---|
146 | |
---|
147 | void ?{}(processor & this, cluster * cltr) with( this ) { |
---|
148 | this.cltr = cltr; |
---|
149 | terminated{ 0 }; |
---|
150 | do_terminate = false; |
---|
151 | preemption_alarm = NULL; |
---|
152 | pending_preemption = false; |
---|
153 | runner.proc = &this; |
---|
154 | |
---|
155 | start( &this ); |
---|
156 | } |
---|
157 | |
---|
158 | void ?{}(processor & this, cluster * cltr, processorCtx_t & runner) with( this ) { |
---|
159 | this.cltr = cltr; |
---|
160 | terminated{ 0 }; |
---|
161 | do_terminate = false; |
---|
162 | preemption_alarm = NULL; |
---|
163 | pending_preemption = false; |
---|
164 | kernel_thread = pthread_self(); |
---|
165 | runner.proc = &this; |
---|
166 | |
---|
167 | __cfaabi_dbg_print_safe("Kernel : constructing main processor context %p\n", &runner); |
---|
168 | runner{ &this }; |
---|
169 | } |
---|
170 | |
---|
171 | void ^?{}(processor & this) with( this ){ |
---|
172 | if( ! do_terminate ) { |
---|
173 | __cfaabi_dbg_print_safe("Kernel : core %p signaling termination\n", &this); |
---|
174 | terminate(&this); |
---|
175 | verify(this.do_terminate); |
---|
176 | verify(TL_GET( this_processor ) != &this); |
---|
177 | P( terminated ); |
---|
178 | verify(TL_GET( this_processor ) != &this); |
---|
179 | pthread_join( kernel_thread, NULL ); |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | void ?{}(cluster & this) with( this ) { |
---|
184 | ready_queue{}; |
---|
185 | ready_queue_lock{}; |
---|
186 | |
---|
187 | preemption_rate = default_preemption(); |
---|
188 | } |
---|
189 | |
---|
190 | void ^?{}(cluster & this) { |
---|
191 | |
---|
192 | } |
---|
193 | |
---|
194 | //============================================================================================= |
---|
195 | // Kernel Scheduling logic |
---|
196 | //============================================================================================= |
---|
197 | //Main of the processor contexts |
---|
198 | void main(processorCtx_t & runner) { |
---|
199 | processor * this = runner.proc; |
---|
200 | verify(this); |
---|
201 | |
---|
202 | __cfaabi_dbg_print_safe("Kernel : core %p starting\n", this); |
---|
203 | |
---|
204 | { |
---|
205 | // Setup preemption data |
---|
206 | preemption_scope scope = { this }; |
---|
207 | |
---|
208 | __cfaabi_dbg_print_safe("Kernel : core %p started\n", this); |
---|
209 | |
---|
210 | thread_desc * readyThread = NULL; |
---|
211 | for( unsigned int spin_count = 0; ! this->do_terminate; spin_count++ ) |
---|
212 | { |
---|
213 | readyThread = nextThread( this->cltr ); |
---|
214 | |
---|
215 | if(readyThread) |
---|
216 | { |
---|
217 | verify( ! TL_GET( preemption_state ).enabled ); |
---|
218 | |
---|
219 | runThread(this, readyThread); |
---|
220 | |
---|
221 | verify( ! TL_GET( preemption_state ).enabled ); |
---|
222 | |
---|
223 | //Some actions need to be taken from the kernel |
---|
224 | finishRunning(this); |
---|
225 | |
---|
226 | spin_count = 0; |
---|
227 | } |
---|
228 | else |
---|
229 | { |
---|
230 | spin(this, &spin_count); |
---|
231 | } |
---|
232 | } |
---|
233 | |
---|
234 | __cfaabi_dbg_print_safe("Kernel : core %p stopping\n", this); |
---|
235 | } |
---|
236 | |
---|
237 | V( this->terminated ); |
---|
238 | |
---|
239 | __cfaabi_dbg_print_safe("Kernel : core %p terminated\n", this); |
---|
240 | } |
---|
241 | |
---|
242 | // runThread runs a thread by context switching |
---|
243 | // from the processor coroutine to the target thread |
---|
244 | void runThread(processor * this, thread_desc * dst) { |
---|
245 | assert(dst->curr_cor); |
---|
246 | coroutine_desc * proc_cor = get_coroutine(this->runner); |
---|
247 | coroutine_desc * thrd_cor = dst->curr_cor; |
---|
248 | |
---|
249 | //Reset the terminating actions here |
---|
250 | this->finish.action_code = No_Action; |
---|
251 | |
---|
252 | //Update global state |
---|
253 | TL_SET( this_thread, dst ); |
---|
254 | |
---|
255 | // Context Switch to the thread |
---|
256 | ThreadCtxSwitch(proc_cor, thrd_cor); |
---|
257 | // when ThreadCtxSwitch returns we are back in the processor coroutine |
---|
258 | } |
---|
259 | |
---|
260 | void returnToKernel() { |
---|
261 | coroutine_desc * proc_cor = get_coroutine(TL_GET( this_processor )->runner); |
---|
262 | coroutine_desc * thrd_cor = TL_GET( this_thread )->curr_cor = TL_GET( this_coroutine ); |
---|
263 | ThreadCtxSwitch(thrd_cor, proc_cor); |
---|
264 | } |
---|
265 | |
---|
266 | // Once a thread has finished running, some of |
---|
267 | // its final actions must be executed from the kernel |
---|
268 | void finishRunning(processor * this) with( this->finish ) { |
---|
269 | if( action_code == Release ) { |
---|
270 | verify( ! TL_GET( preemption_state ).enabled ); |
---|
271 | unlock( *lock ); |
---|
272 | } |
---|
273 | else if( action_code == Schedule ) { |
---|
274 | ScheduleThread( thrd ); |
---|
275 | } |
---|
276 | else if( action_code == Release_Schedule ) { |
---|
277 | verify( ! TL_GET( preemption_state ).enabled ); |
---|
278 | unlock( *lock ); |
---|
279 | ScheduleThread( thrd ); |
---|
280 | } |
---|
281 | else if( action_code == Release_Multi ) { |
---|
282 | verify( ! TL_GET( preemption_state ).enabled ); |
---|
283 | for(int i = 0; i < lock_count; i++) { |
---|
284 | unlock( *locks[i] ); |
---|
285 | } |
---|
286 | } |
---|
287 | else if( action_code == Release_Multi_Schedule ) { |
---|
288 | for(int i = 0; i < lock_count; i++) { |
---|
289 | unlock( *locks[i] ); |
---|
290 | } |
---|
291 | for(int i = 0; i < thrd_count; i++) { |
---|
292 | ScheduleThread( thrds[i] ); |
---|
293 | } |
---|
294 | } |
---|
295 | else { |
---|
296 | assert(action_code == No_Action); |
---|
297 | } |
---|
298 | } |
---|
299 | |
---|
300 | // Handles spinning logic |
---|
301 | // TODO : find some strategy to put cores to sleep after some time |
---|
302 | void spin(processor * this, unsigned int * spin_count) { |
---|
303 | (*spin_count)++; |
---|
304 | } |
---|
305 | |
---|
306 | // Context invoker for processors |
---|
307 | // This is the entry point for processors (kernel threads) |
---|
308 | // It effectively constructs a coroutine by stealing the pthread stack |
---|
309 | void * CtxInvokeProcessor(void * arg) { |
---|
310 | processor * proc = (processor *) arg; |
---|
311 | TL_SET( this_processor, proc ); |
---|
312 | TL_SET( this_coroutine, NULL ); |
---|
313 | TL_SET( this_thread, NULL ); |
---|
314 | TL_GET( preemption_state ).enabled = false; |
---|
315 | TL_GET( preemption_state ).disable_count = 1; |
---|
316 | // SKULLDUGGERY: We want to create a context for the processor coroutine |
---|
317 | // which is needed for the 2-step context switch. However, there is no reason |
---|
318 | // to waste the perfectly valid stack create by pthread. |
---|
319 | current_stack_info_t info; |
---|
320 | machine_context_t ctx; |
---|
321 | info.context = &ctx; |
---|
322 | (proc->runner){ proc, &info }; |
---|
323 | |
---|
324 | __cfaabi_dbg_print_safe("Coroutine : created stack %p\n", get_coroutine(proc->runner)->stack.base); |
---|
325 | |
---|
326 | //Set global state |
---|
327 | TL_SET( this_coroutine, get_coroutine(proc->runner) ); |
---|
328 | TL_SET( this_thread, NULL ); |
---|
329 | |
---|
330 | //We now have a proper context from which to schedule threads |
---|
331 | __cfaabi_dbg_print_safe("Kernel : core %p created (%p, %p)\n", proc, &proc->runner, &ctx); |
---|
332 | |
---|
333 | // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't |
---|
334 | // resume it to start it like it normally would, it will just context switch |
---|
335 | // back to here. Instead directly call the main since we already are on the |
---|
336 | // appropriate stack. |
---|
337 | get_coroutine(proc->runner)->state = Active; |
---|
338 | main( proc->runner ); |
---|
339 | get_coroutine(proc->runner)->state = Halted; |
---|
340 | |
---|
341 | // Main routine of the core returned, the core is now fully terminated |
---|
342 | __cfaabi_dbg_print_safe("Kernel : core %p main ended (%p)\n", proc, &proc->runner); |
---|
343 | |
---|
344 | return NULL; |
---|
345 | } |
---|
346 | |
---|
347 | void start(processor * this) { |
---|
348 | __cfaabi_dbg_print_safe("Kernel : Starting core %p\n", this); |
---|
349 | |
---|
350 | pthread_create( &this->kernel_thread, NULL, CtxInvokeProcessor, (void*)this ); |
---|
351 | |
---|
352 | __cfaabi_dbg_print_safe("Kernel : core %p started\n", this); |
---|
353 | } |
---|
354 | |
---|
355 | void kernel_first_resume(processor * this) { |
---|
356 | coroutine_desc * src = TL_GET( this_coroutine ); |
---|
357 | coroutine_desc * dst = get_coroutine(this->runner); |
---|
358 | |
---|
359 | verify( ! TL_GET( preemption_state ).enabled ); |
---|
360 | |
---|
361 | create_stack(&dst->stack, dst->stack.size); |
---|
362 | CtxStart(&this->runner, CtxInvokeCoroutine); |
---|
363 | |
---|
364 | verify( ! TL_GET( preemption_state ).enabled ); |
---|
365 | |
---|
366 | dst->last = src; |
---|
367 | dst->starter = dst->starter ? dst->starter : src; |
---|
368 | |
---|
369 | // set state of current coroutine to inactive |
---|
370 | src->state = src->state == Halted ? Halted : Inactive; |
---|
371 | |
---|
372 | // set new coroutine that task is executing |
---|
373 | TL_SET( this_coroutine, dst ); |
---|
374 | |
---|
375 | // SKULLDUGGERY normally interrupts are enable before leaving a coroutine ctxswitch. |
---|
376 | // Therefore, when first creating a coroutine, interrupts are enable before calling the main. |
---|
377 | // This is consistent with thread creation. However, when creating the main processor coroutine, |
---|
378 | // we wan't interrupts to be disabled. Therefore, we double-disable interrupts here so they will |
---|
379 | // stay disabled. |
---|
380 | disable_interrupts(); |
---|
381 | |
---|
382 | // context switch to specified coroutine |
---|
383 | assert( src->stack.context ); |
---|
384 | CtxSwitch( src->stack.context, dst->stack.context ); |
---|
385 | // when CtxSwitch returns we are back in the src coroutine |
---|
386 | |
---|
387 | // set state of new coroutine to active |
---|
388 | src->state = Active; |
---|
389 | |
---|
390 | verify( ! TL_GET( preemption_state ).enabled ); |
---|
391 | } |
---|
392 | |
---|
393 | //----------------------------------------------------------------------------- |
---|
394 | // Scheduler routines |
---|
395 | void ScheduleThread( thread_desc * thrd ) { |
---|
396 | // if( ! thrd ) return; |
---|
397 | verify( thrd ); |
---|
398 | verify( thrd->self_cor.state != Halted ); |
---|
399 | |
---|
400 | verify( ! TL_GET( preemption_state ).enabled ); |
---|
401 | |
---|
402 | verifyf( thrd->next == NULL, "Expected null got %p", thrd->next ); |
---|
403 | |
---|
404 | with( *TL_GET( this_processor )->cltr ) { |
---|
405 | lock ( ready_queue_lock __cfaabi_dbg_ctx2 ); |
---|
406 | append( ready_queue, thrd ); |
---|
407 | unlock( ready_queue_lock ); |
---|
408 | } |
---|
409 | |
---|
410 | verify( ! TL_GET( preemption_state ).enabled ); |
---|
411 | } |
---|
412 | |
---|
413 | thread_desc * nextThread(cluster * this) with( *this ) { |
---|
414 | verify( ! TL_GET( preemption_state ).enabled ); |
---|
415 | lock( ready_queue_lock __cfaabi_dbg_ctx2 ); |
---|
416 | thread_desc * head = pop_head( ready_queue ); |
---|
417 | unlock( ready_queue_lock ); |
---|
418 | verify( ! TL_GET( preemption_state ).enabled ); |
---|
419 | return head; |
---|
420 | } |
---|
421 | |
---|
422 | void BlockInternal() { |
---|
423 | disable_interrupts(); |
---|
424 | verify( ! TL_GET( preemption_state ).enabled ); |
---|
425 | returnToKernel(); |
---|
426 | verify( ! TL_GET( preemption_state ).enabled ); |
---|
427 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
428 | } |
---|
429 | |
---|
430 | void BlockInternal( __spinlock_t * lock ) { |
---|
431 | disable_interrupts(); |
---|
432 | TL_GET( this_processor )->finish.action_code = Release; |
---|
433 | TL_GET( this_processor )->finish.lock = lock; |
---|
434 | |
---|
435 | verify( ! TL_GET( preemption_state ).enabled ); |
---|
436 | returnToKernel(); |
---|
437 | verify( ! TL_GET( preemption_state ).enabled ); |
---|
438 | |
---|
439 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
440 | } |
---|
441 | |
---|
442 | void BlockInternal( thread_desc * thrd ) { |
---|
443 | disable_interrupts(); |
---|
444 | TL_GET( this_processor )->finish.action_code = Schedule; |
---|
445 | TL_GET( this_processor )->finish.thrd = thrd; |
---|
446 | |
---|
447 | verify( ! TL_GET( preemption_state ).enabled ); |
---|
448 | returnToKernel(); |
---|
449 | verify( ! TL_GET( preemption_state ).enabled ); |
---|
450 | |
---|
451 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
452 | } |
---|
453 | |
---|
454 | void BlockInternal( __spinlock_t * lock, thread_desc * thrd ) { |
---|
455 | assert(thrd); |
---|
456 | disable_interrupts(); |
---|
457 | TL_GET( this_processor )->finish.action_code = Release_Schedule; |
---|
458 | TL_GET( this_processor )->finish.lock = lock; |
---|
459 | TL_GET( this_processor )->finish.thrd = thrd; |
---|
460 | |
---|
461 | verify( ! TL_GET( preemption_state ).enabled ); |
---|
462 | returnToKernel(); |
---|
463 | verify( ! TL_GET( preemption_state ).enabled ); |
---|
464 | |
---|
465 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
466 | } |
---|
467 | |
---|
468 | void BlockInternal(__spinlock_t * locks [], unsigned short count) { |
---|
469 | disable_interrupts(); |
---|
470 | TL_GET( this_processor )->finish.action_code = Release_Multi; |
---|
471 | TL_GET( this_processor )->finish.locks = locks; |
---|
472 | TL_GET( this_processor )->finish.lock_count = count; |
---|
473 | |
---|
474 | verify( ! TL_GET( preemption_state ).enabled ); |
---|
475 | returnToKernel(); |
---|
476 | verify( ! TL_GET( preemption_state ).enabled ); |
---|
477 | |
---|
478 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
479 | } |
---|
480 | |
---|
481 | void BlockInternal(__spinlock_t * locks [], unsigned short lock_count, thread_desc * thrds [], unsigned short thrd_count) { |
---|
482 | disable_interrupts(); |
---|
483 | TL_GET( this_processor )->finish.action_code = Release_Multi_Schedule; |
---|
484 | TL_GET( this_processor )->finish.locks = locks; |
---|
485 | TL_GET( this_processor )->finish.lock_count = lock_count; |
---|
486 | TL_GET( this_processor )->finish.thrds = thrds; |
---|
487 | TL_GET( this_processor )->finish.thrd_count = thrd_count; |
---|
488 | |
---|
489 | verify( ! TL_GET( preemption_state ).enabled ); |
---|
490 | returnToKernel(); |
---|
491 | verify( ! TL_GET( preemption_state ).enabled ); |
---|
492 | |
---|
493 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
494 | } |
---|
495 | |
---|
496 | void LeaveThread(__spinlock_t * lock, thread_desc * thrd) { |
---|
497 | verify( ! TL_GET( preemption_state ).enabled ); |
---|
498 | TL_GET( this_processor )->finish.action_code = thrd ? Release_Schedule : Release; |
---|
499 | TL_GET( this_processor )->finish.lock = lock; |
---|
500 | TL_GET( this_processor )->finish.thrd = thrd; |
---|
501 | |
---|
502 | returnToKernel(); |
---|
503 | } |
---|
504 | |
---|
505 | //============================================================================================= |
---|
506 | // Kernel Setup logic |
---|
507 | //============================================================================================= |
---|
508 | //----------------------------------------------------------------------------- |
---|
509 | // Kernel boot procedures |
---|
510 | void kernel_startup(void) { |
---|
511 | verify( ! TL_GET( preemption_state ).enabled ); |
---|
512 | __cfaabi_dbg_print_safe("Kernel : Starting\n"); |
---|
513 | |
---|
514 | // Start by initializing the main thread |
---|
515 | // SKULLDUGGERY: the mainThread steals the process main thread |
---|
516 | // which will then be scheduled by the mainProcessor normally |
---|
517 | mainThread = (thread_desc *)&storage_mainThread; |
---|
518 | current_stack_info_t info; |
---|
519 | (*mainThread){ &info }; |
---|
520 | |
---|
521 | __cfaabi_dbg_print_safe("Kernel : Main thread ready\n"); |
---|
522 | |
---|
523 | // Initialize the main cluster |
---|
524 | mainCluster = (cluster *)&storage_mainCluster; |
---|
525 | (*mainCluster){}; |
---|
526 | |
---|
527 | __cfaabi_dbg_print_safe("Kernel : main cluster ready\n"); |
---|
528 | |
---|
529 | // Initialize the main processor and the main processor ctx |
---|
530 | // (the coroutine that contains the processing control flow) |
---|
531 | mainProcessor = (processor *)&storage_mainProcessor; |
---|
532 | (*mainProcessor){ mainCluster, *(processorCtx_t *)&storage_mainProcessorCtx }; |
---|
533 | |
---|
534 | //initialize the global state variables |
---|
535 | TL_SET( this_processor, mainProcessor ); |
---|
536 | TL_SET( this_thread, mainThread ); |
---|
537 | TL_SET( this_coroutine, &mainThread->self_cor ); |
---|
538 | |
---|
539 | // Enable preemption |
---|
540 | kernel_start_preemption(); |
---|
541 | |
---|
542 | // Add the main thread to the ready queue |
---|
543 | // once resume is called on mainProcessor->runner the mainThread needs to be scheduled like any normal thread |
---|
544 | ScheduleThread(mainThread); |
---|
545 | |
---|
546 | // SKULLDUGGERY: Force a context switch to the main processor to set the main thread's context to the current UNIX |
---|
547 | // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that |
---|
548 | // mainThread is on the ready queue when this call is made. |
---|
549 | kernel_first_resume( TL_GET( this_processor ) ); |
---|
550 | |
---|
551 | |
---|
552 | |
---|
553 | // THE SYSTEM IS NOW COMPLETELY RUNNING |
---|
554 | __cfaabi_dbg_print_safe("Kernel : Started\n--------------------------------------------------\n\n"); |
---|
555 | |
---|
556 | verify( ! TL_GET( preemption_state ).enabled ); |
---|
557 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
558 | verify( TL_GET( preemption_state ).enabled ); |
---|
559 | } |
---|
560 | |
---|
561 | void kernel_shutdown(void) { |
---|
562 | __cfaabi_dbg_print_safe("\n--------------------------------------------------\nKernel : Shutting down\n"); |
---|
563 | |
---|
564 | verify( TL_GET( preemption_state ).enabled ); |
---|
565 | disable_interrupts(); |
---|
566 | verify( ! TL_GET( preemption_state ).enabled ); |
---|
567 | |
---|
568 | // SKULLDUGGERY: Notify the mainProcessor it needs to terminates. |
---|
569 | // When its coroutine terminates, it return control to the mainThread |
---|
570 | // which is currently here |
---|
571 | mainProcessor->do_terminate = true; |
---|
572 | returnToKernel(); |
---|
573 | |
---|
574 | // THE SYSTEM IS NOW COMPLETELY STOPPED |
---|
575 | |
---|
576 | // Disable preemption |
---|
577 | kernel_stop_preemption(); |
---|
578 | |
---|
579 | // Destroy the main processor and its context in reverse order of construction |
---|
580 | // These were manually constructed so we need manually destroy them |
---|
581 | ^(mainProcessor->runner){}; |
---|
582 | ^(mainProcessor){}; |
---|
583 | |
---|
584 | // Final step, destroy the main thread since it is no longer needed |
---|
585 | // Since we provided a stack to this taxk it will not destroy anything |
---|
586 | ^(mainThread){}; |
---|
587 | |
---|
588 | __cfaabi_dbg_print_safe("Kernel : Shutdown complete\n"); |
---|
589 | } |
---|
590 | |
---|
591 | //============================================================================================= |
---|
592 | // Unexpected Terminating logic |
---|
593 | //============================================================================================= |
---|
594 | |
---|
595 | |
---|
596 | static __spinlock_t kernel_abort_lock; |
---|
597 | static __spinlock_t kernel_debug_lock; |
---|
598 | static bool kernel_abort_called = false; |
---|
599 | |
---|
600 | void * kernel_abort (void) __attribute__ ((__nothrow__)) { |
---|
601 | // abort cannot be recursively entered by the same or different processors because all signal handlers return when |
---|
602 | // the globalAbort flag is true. |
---|
603 | lock( kernel_abort_lock __cfaabi_dbg_ctx2 ); |
---|
604 | |
---|
605 | // first task to abort ? |
---|
606 | if ( ! kernel_abort_called ) { // not first task to abort ? |
---|
607 | kernel_abort_called = true; |
---|
608 | unlock( kernel_abort_lock ); |
---|
609 | } |
---|
610 | else { |
---|
611 | unlock( kernel_abort_lock ); |
---|
612 | |
---|
613 | sigset_t mask; |
---|
614 | sigemptyset( &mask ); |
---|
615 | sigaddset( &mask, SIGALRM ); // block SIGALRM signals |
---|
616 | sigaddset( &mask, SIGUSR1 ); // block SIGUSR1 signals |
---|
617 | sigsuspend( &mask ); // block the processor to prevent further damage during abort |
---|
618 | _exit( EXIT_FAILURE ); // if processor unblocks before it is killed, terminate it |
---|
619 | } |
---|
620 | |
---|
621 | return TL_GET( this_thread ); |
---|
622 | } |
---|
623 | |
---|
624 | void kernel_abort_msg( void * kernel_data, char * abort_text, int abort_text_size ) { |
---|
625 | thread_desc * thrd = kernel_data; |
---|
626 | |
---|
627 | int len = snprintf( abort_text, abort_text_size, "Error occurred while executing task %.256s (%p)", thrd->self_cor.name, thrd ); |
---|
628 | __cfaabi_dbg_bits_write( abort_text, len ); |
---|
629 | |
---|
630 | if ( get_coroutine(thrd) != TL_GET( this_coroutine ) ) { |
---|
631 | len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", TL_GET( this_coroutine )->name, TL_GET( this_coroutine ) ); |
---|
632 | __cfaabi_dbg_bits_write( abort_text, len ); |
---|
633 | } |
---|
634 | else { |
---|
635 | __cfaabi_dbg_bits_write( ".\n", 2 ); |
---|
636 | } |
---|
637 | } |
---|
638 | |
---|
639 | int kernel_abort_lastframe( void ) __attribute__ ((__nothrow__)) { |
---|
640 | return get_coroutine(TL_GET( this_thread )) == get_coroutine(mainThread) ? 4 : 2; |
---|
641 | } |
---|
642 | |
---|
643 | extern "C" { |
---|
644 | void __cfaabi_dbg_bits_acquire() { |
---|
645 | lock( kernel_debug_lock __cfaabi_dbg_ctx2 ); |
---|
646 | } |
---|
647 | |
---|
648 | void __cfaabi_dbg_bits_release() { |
---|
649 | unlock( kernel_debug_lock ); |
---|
650 | } |
---|
651 | } |
---|
652 | |
---|
653 | //============================================================================================= |
---|
654 | // Kernel Utilities |
---|
655 | //============================================================================================= |
---|
656 | //----------------------------------------------------------------------------- |
---|
657 | // Locks |
---|
658 | void ?{}( semaphore & this, int count = 1 ) { |
---|
659 | (this.lock){}; |
---|
660 | this.count = count; |
---|
661 | (this.waiting){}; |
---|
662 | } |
---|
663 | void ^?{}(semaphore & this) {} |
---|
664 | |
---|
665 | void P(semaphore & this) with( this ){ |
---|
666 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
667 | count -= 1; |
---|
668 | if ( count < 0 ) { |
---|
669 | // queue current task |
---|
670 | append( waiting, (thread_desc *)TL_GET( this_thread ) ); |
---|
671 | |
---|
672 | // atomically release spin lock and block |
---|
673 | BlockInternal( &lock ); |
---|
674 | } |
---|
675 | else { |
---|
676 | unlock( lock ); |
---|
677 | } |
---|
678 | } |
---|
679 | |
---|
680 | void V(semaphore & this) with( this ) { |
---|
681 | thread_desc * thrd = NULL; |
---|
682 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
683 | count += 1; |
---|
684 | if ( count <= 0 ) { |
---|
685 | // remove task at head of waiting list |
---|
686 | thrd = pop_head( waiting ); |
---|
687 | } |
---|
688 | |
---|
689 | unlock( lock ); |
---|
690 | |
---|
691 | // make new owner |
---|
692 | WakeThread( thrd ); |
---|
693 | } |
---|
694 | |
---|
695 | //----------------------------------------------------------------------------- |
---|
696 | // Debug |
---|
697 | __cfaabi_dbg_debug_do( |
---|
698 | struct { |
---|
699 | thread_desc * tail; |
---|
700 | } __cfaabi_dbg_thread_list = { NULL }; |
---|
701 | |
---|
702 | void __cfaabi_dbg_thread_register( thread_desc * thrd ) { |
---|
703 | if( !__cfaabi_dbg_thread_list.tail ) { |
---|
704 | __cfaabi_dbg_thread_list.tail = thrd; |
---|
705 | return; |
---|
706 | } |
---|
707 | __cfaabi_dbg_thread_list.tail->dbg_next = thrd; |
---|
708 | thrd->dbg_prev = __cfaabi_dbg_thread_list.tail; |
---|
709 | __cfaabi_dbg_thread_list.tail = thrd; |
---|
710 | } |
---|
711 | |
---|
712 | void __cfaabi_dbg_thread_unregister( thread_desc * thrd ) { |
---|
713 | thread_desc * prev = thrd->dbg_prev; |
---|
714 | thread_desc * next = thrd->dbg_next; |
---|
715 | |
---|
716 | if( next ) { next->dbg_prev = prev; } |
---|
717 | else { |
---|
718 | assert( __cfaabi_dbg_thread_list.tail == thrd ); |
---|
719 | __cfaabi_dbg_thread_list.tail = prev; |
---|
720 | } |
---|
721 | |
---|
722 | if( prev ) { prev->dbg_next = next; } |
---|
723 | |
---|
724 | thrd->dbg_prev = NULL; |
---|
725 | thrd->dbg_next = NULL; |
---|
726 | } |
---|
727 | |
---|
728 | void __cfaabi_dbg_record(__spinlock_t & this, const char * prev_name) { |
---|
729 | this.prev_name = prev_name; |
---|
730 | this.prev_thrd = TL_GET( this_thread ); |
---|
731 | } |
---|
732 | ) |
---|
733 | // Local Variables: // |
---|
734 | // mode: c // |
---|
735 | // tab-width: 4 // |
---|
736 | // End: // |
---|