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