source: src/libcfa/concurrency/kernel.c@ b51e5fdb

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since b51e5fdb was 094476d, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Fixed dangling pointer in processor shutdown

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