source: src/libcfa/concurrency/kernel.c@ 32cab5b

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 with_gc
Last change on this file since 32cab5b was e93f1d2, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Merge branch 'master' of plg.uwaterloo.ca:/u/cforall/software/cfa/cfa-cc

  • Property mode set to 100644
File size: 21.5 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
[0f56058]12// Last Modified On : Mon Apr 9 16:11:46 2018
13// Update Count : 24
[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
[0f56058]27#include "time"
[2ac095d]28#include "kernel_private.h"
[c81ebf9]29#include "preemption.h"
[2ac095d]30#include "startup.h"
[8118303]31
32//Private includes
33#define __CFA_INVOKE_PRIVATE__
34#include "invoke.h"
35
[2ac095d]36//Start and stop routine for the kernel, declared first to make sure they run first
37void kernel_startup(void) __attribute__(( constructor( STARTUP_PRIORITY_KERNEL ) ));
38void kernel_shutdown(void) __attribute__(( destructor ( STARTUP_PRIORITY_KERNEL ) ));
39
[8def349]40//-----------------------------------------------------------------------------
41// Kernel storage
[969b3fe]42KERNEL_STORAGE(cluster, mainCluster);
43KERNEL_STORAGE(processor, mainProcessor);
44KERNEL_STORAGE(processorCtx_t, mainProcessorCtx);
45KERNEL_STORAGE(thread_desc, mainThread);
[f2b12406]46KERNEL_STORAGE(machine_context_t, mainThreadCtx);
[8def349]47
[969b3fe]48cluster * mainCluster;
49processor * mainProcessor;
[348006f]50thread_desc * mainThread;
[eb2e723]51
[bd98b58]52//-----------------------------------------------------------------------------
53// Global state
54
[b69ea6b]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
[b10affd]59thread_local struct KernelThreadData kernelThreadData = {
60 NULL,
61 NULL,
62 NULL,
63 { 1, false, false }
64};
[c84e80a]65
66//-----------------------------------------------------------------------------
[8def349]67// Main thread construction
68struct current_stack_info_t {
[1c273d0]69 machine_context_t ctx;
[8def349]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
[c84e80a]76};
77
[242a902]78void ?{}( current_stack_info_t & this ) {
79 CtxGet( this.ctx );
80 this.base = this.ctx.FP;
81 this.storage = this.ctx.SP;
[8def349]82
83 rlimit r;
[132fad4]84 getrlimit( RLIMIT_STACK, &r);
[242a902]85 this.size = r.rlim_cur;
[8def349]86
[242a902]87 this.limit = (void *)(((intptr_t)this.base) - this.size);
[9236060]88 this.context = &storage_mainThreadCtx;
[242a902]89 this.top = this.base;
[8def349]90}
91
[65deb18]92void ?{}( 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;
[8def349]100}
101
[65deb18]102void ?{}( 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;
[8def349]108}
109
[65deb18]110void ?{}( thread_desc & this, current_stack_info_t * info) with( this ) {
111 self_cor{ info };
[82c948c]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 };
[8def349]124}
[c84e80a]125
[8def349]126//-----------------------------------------------------------------------------
127// Processor coroutine
[094476d]128void ?{}(processorCtx_t & this) {}
[39fea2f]129
130// Construct the processor context of the main processor
[242a902]131void ?{}(processorCtx_t & this, processor * proc) {
132 (this.__cor){ "Processor" };
[b462670]133 this.__cor.starter = NULL;
[242a902]134 this.proc = proc;
[8def349]135}
136
[39fea2f]137// Construct the processor context of non-main processors
[242a902]138void ?{}(processorCtx_t & this, processor * proc, current_stack_info_t * info) {
139 (this.__cor){ info };
140 this.proc = proc;
[8def349]141}
142
[242a902]143void ?{}(processor & this) {
[969b3fe]144 this{ mainCluster };
[8def349]145}
146
[c40e7c5]147void ?{}(processor & this, cluster * cltr) with( this ) {
[242a902]148 this.cltr = cltr;
[c40e7c5]149 terminated{ 0 };
150 do_terminate = false;
151 preemption_alarm = NULL;
152 pending_preemption = false;
[094476d]153 runner.proc = &this;
[8def349]154
[242a902]155 start( &this );
[c84e80a]156}
157
[c40e7c5]158void ?{}(processor & this, cluster * cltr, processorCtx_t & runner) with( this ) {
[242a902]159 this.cltr = cltr;
[c40e7c5]160 terminated{ 0 };
161 do_terminate = false;
162 preemption_alarm = NULL;
163 pending_preemption = false;
164 kernel_thread = pthread_self();
[094476d]165 runner.proc = &this;
[8def349]166
[36982fc]167 __cfaabi_dbg_print_safe("Kernel : constructing main processor context %p\n", &runner);
[83a071f9]168 runner{ &this };
[8def349]169}
170
[65deb18]171void ^?{}(processor & this) with( this ){
172 if( ! do_terminate ) {
[36982fc]173 __cfaabi_dbg_print_safe("Kernel : core %p signaling termination\n", &this);
[4dad189]174 terminate(&this);
[1f37ed02]175 verify(this.do_terminate);
[b10affd]176 verify(TL_GET( this_processor ) != &this);
[65deb18]177 P( terminated );
[b10affd]178 verify(TL_GET( this_processor ) != &this);
[65deb18]179 pthread_join( kernel_thread, NULL );
[8def349]180 }
181}
182
[65deb18]183void ?{}(cluster & this) with( this ) {
184 ready_queue{};
185 ready_queue_lock{};
[e60e0dc]186
[d8548e2]187 preemption_rate = default_preemption();
[8def349]188}
189
[242a902]190void ^?{}(cluster & this) {
[1c273d0]191
[c84e80a]192}
193
[75f3522]194//=============================================================================================
195// Kernel Scheduling logic
196//=============================================================================================
[8fcbb4c]197//Main of the processor contexts
[83a071f9]198void main(processorCtx_t & runner) {
199 processor * this = runner.proc;
[094476d]200 verify(this);
[c81ebf9]201
[36982fc]202 __cfaabi_dbg_print_safe("Kernel : core %p starting\n", this);
[8118303]203
[75f3522]204 {
[c81ebf9]205 // Setup preemption data
206 preemption_scope scope = { this };
207
[36982fc]208 __cfaabi_dbg_print_safe("Kernel : core %p started\n", this);
[8118303]209
[c81ebf9]210 thread_desc * readyThread = NULL;
[e60e0dc]211 for( unsigned int spin_count = 0; ! this->do_terminate; spin_count++ )
[75f3522]212 {
[c81ebf9]213 readyThread = nextThread( this->cltr );
[75f3522]214
[c81ebf9]215 if(readyThread)
216 {
[b10affd]217 verify( ! TL_GET( preemption_state ).enabled );
[4e6fb8e]218
[c81ebf9]219 runThread(this, readyThread);
[75f3522]220
[b10affd]221 verify( ! TL_GET( preemption_state ).enabled );
[4e6fb8e]222
[c81ebf9]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
[36982fc]234 __cfaabi_dbg_print_safe("Kernel : core %p stopping\n", this);
[c84e80a]235 }
[8118303]236
[4cedd9f]237 V( this->terminated );
[bdeba0b]238
[36982fc]239 __cfaabi_dbg_print_safe("Kernel : core %p terminated\n", this);
[c84e80a]240}
241
[1c273d0]242// runThread runs a thread by context switching
243// from the processor coroutine to the target thread
[348006f]244void runThread(processor * this, thread_desc * dst) {
[82c948c]245 assert(dst->curr_cor);
[094476d]246 coroutine_desc * proc_cor = get_coroutine(this->runner);
[82c948c]247 coroutine_desc * thrd_cor = dst->curr_cor;
[1c273d0]248
[75f3522]249 //Reset the terminating actions here
[db6f06a]250 this->finish.action_code = No_Action;
[8fcbb4c]251
[75f3522]252 //Update global state
[b10affd]253 TL_SET( this_thread, dst );
[75f3522]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
[82c948c]260void returnToKernel() {
[b10affd]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 );
[82c948c]263 ThreadCtxSwitch(thrd_cor, proc_cor);
264}
265
[1c273d0]266// Once a thread has finished running, some of
[75f3522]267// its final actions must be executed from the kernel
[65deb18]268void finishRunning(processor * this) with( this->finish ) {
269 if( action_code == Release ) {
[b10affd]270 verify( ! TL_GET( preemption_state ).enabled );
[65deb18]271 unlock( *lock );
[db6f06a]272 }
[65deb18]273 else if( action_code == Schedule ) {
274 ScheduleThread( thrd );
[db6f06a]275 }
[65deb18]276 else if( action_code == Release_Schedule ) {
[b10affd]277 verify( ! TL_GET( preemption_state ).enabled );
[65deb18]278 unlock( *lock );
279 ScheduleThread( thrd );
[db6f06a]280 }
[65deb18]281 else if( action_code == Release_Multi ) {
[b10affd]282 verify( ! TL_GET( preemption_state ).enabled );
[65deb18]283 for(int i = 0; i < lock_count; i++) {
284 unlock( *locks[i] );
[0c78741]285 }
286 }
[65deb18]287 else if( action_code == Release_Multi_Schedule ) {
288 for(int i = 0; i < lock_count; i++) {
289 unlock( *locks[i] );
[0c78741]290 }
[65deb18]291 for(int i = 0; i < thrd_count; i++) {
292 ScheduleThread( thrds[i] );
[0c78741]293 }
294 }
[db6f06a]295 else {
[65deb18]296 assert(action_code == No_Action);
[8fcbb4c]297 }
[c84e80a]298}
299
[0c92c9f]300// Handles spinning logic
301// TODO : find some strategy to put cores to sleep after some time
[c84e80a]302void spin(processor * this, unsigned int * spin_count) {
303 (*spin_count)++;
304}
305
[0c92c9f]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
[8def349]309void * CtxInvokeProcessor(void * arg) {
310 processor * proc = (processor *) arg;
[b10affd]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;
[8def349]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
[1c273d0]318 // to waste the perfectly valid stack create by pthread.
[8def349]319 current_stack_info_t info;
320 machine_context_t ctx;
321 info.context = &ctx;
[094476d]322 (proc->runner){ proc, &info };
[8def349]323
[094476d]324 __cfaabi_dbg_print_safe("Coroutine : created stack %p\n", get_coroutine(proc->runner)->stack.base);
[8fcbb4c]325
[0c92c9f]326 //Set global state
[b10affd]327 TL_SET( this_coroutine, get_coroutine(proc->runner) );
328 TL_SET( this_thread, NULL );
[8def349]329
330 //We now have a proper context from which to schedule threads
[094476d]331 __cfaabi_dbg_print_safe("Kernel : core %p created (%p, %p)\n", proc, &proc->runner, &ctx);
[8def349]332
[1c273d0]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
[8def349]336 // appropriate stack.
[094476d]337 get_coroutine(proc->runner)->state = Active;
338 main( proc->runner );
339 get_coroutine(proc->runner)->state = Halted;
[8def349]340
[0c92c9f]341 // Main routine of the core returned, the core is now fully terminated
[094476d]342 __cfaabi_dbg_print_safe("Kernel : core %p main ended (%p)\n", proc, &proc->runner);
[8def349]343
344 return NULL;
[c84e80a]345}
346
[8def349]347void start(processor * this) {
[36982fc]348 __cfaabi_dbg_print_safe("Kernel : Starting core %p\n", this);
[82ff5845]349
[8fcbb4c]350 pthread_create( &this->kernel_thread, NULL, CtxInvokeProcessor, (void*)this );
[eb2e723]351
[36982fc]352 __cfaabi_dbg_print_safe("Kernel : core %p started\n", this);
[eb2e723]353}
354
[b69ea6b]355void kernel_first_resume(processor * this) {
[b10affd]356 coroutine_desc * src = TL_GET( this_coroutine );
[094476d]357 coroutine_desc * dst = get_coroutine(this->runner);
[b69ea6b]358
[b10affd]359 verify( ! TL_GET( preemption_state ).enabled );
[b69ea6b]360
361 create_stack(&dst->stack, dst->stack.size);
[094476d]362 CtxStart(&this->runner, CtxInvokeCoroutine);
[b69ea6b]363
[b10affd]364 verify( ! TL_GET( preemption_state ).enabled );
[b69ea6b]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
[b10affd]373 TL_SET( this_coroutine, dst );
[b69ea6b]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
[b10affd]390 verify( ! TL_GET( preemption_state ).enabled );
[b69ea6b]391}
392
[8def349]393//-----------------------------------------------------------------------------
394// Scheduler routines
[348006f]395void ScheduleThread( thread_desc * thrd ) {
[b10affd]396 // if( ! thrd ) return;
[135b431]397 verify( thrd );
[b18830e]398 verify( thrd->self_cor.state != Halted );
[1c273d0]399
[b10affd]400 verify( ! TL_GET( preemption_state ).enabled );
[690f13c]401
[4aa2fb2]402 verifyf( thrd->next == NULL, "Expected null got %p", thrd->next );
[1c273d0]403
[b10affd]404 with( *TL_GET( this_processor )->cltr ) {
[65deb18]405 lock ( ready_queue_lock __cfaabi_dbg_ctx2 );
406 append( ready_queue, thrd );
407 unlock( ready_queue_lock );
408 }
[1c273d0]409
[b10affd]410 verify( ! TL_GET( preemption_state ).enabled );
[db6f06a]411}
412
[65deb18]413thread_desc * nextThread(cluster * this) with( *this ) {
[b10affd]414 verify( ! TL_GET( preemption_state ).enabled );
[65deb18]415 lock( ready_queue_lock __cfaabi_dbg_ctx2 );
416 thread_desc * head = pop_head( ready_queue );
417 unlock( ready_queue_lock );
[b10affd]418 verify( ! TL_GET( preemption_state ).enabled );
[db6f06a]419 return head;
[eb2e723]420}
421
[82ff5845]422void BlockInternal() {
423 disable_interrupts();
[b10affd]424 verify( ! TL_GET( preemption_state ).enabled );
[82c948c]425 returnToKernel();
[b10affd]426 verify( ! TL_GET( preemption_state ).enabled );
[36982fc]427 enable_interrupts( __cfaabi_dbg_ctx );
[75f3522]428}
429
[ea7d2b0]430void BlockInternal( __spinlock_t * lock ) {
[82ff5845]431 disable_interrupts();
[b10affd]432 TL_GET( this_processor )->finish.action_code = Release;
433 TL_GET( this_processor )->finish.lock = lock;
[0b33412]434
[b10affd]435 verify( ! TL_GET( preemption_state ).enabled );
[82c948c]436 returnToKernel();
[b10affd]437 verify( ! TL_GET( preemption_state ).enabled );
[0b33412]438
[36982fc]439 enable_interrupts( __cfaabi_dbg_ctx );
[db6f06a]440}
441
[82ff5845]442void BlockInternal( thread_desc * thrd ) {
443 disable_interrupts();
[b10affd]444 TL_GET( this_processor )->finish.action_code = Schedule;
445 TL_GET( this_processor )->finish.thrd = thrd;
[0b33412]446
[b10affd]447 verify( ! TL_GET( preemption_state ).enabled );
[82c948c]448 returnToKernel();
[b10affd]449 verify( ! TL_GET( preemption_state ).enabled );
[0b33412]450
[36982fc]451 enable_interrupts( __cfaabi_dbg_ctx );
[db6f06a]452}
453
[ea7d2b0]454void BlockInternal( __spinlock_t * lock, thread_desc * thrd ) {
[97e3296]455 assert(thrd);
[82ff5845]456 disable_interrupts();
[b10affd]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;
[0b33412]460
[b10affd]461 verify( ! TL_GET( preemption_state ).enabled );
[82c948c]462 returnToKernel();
[b10affd]463 verify( ! TL_GET( preemption_state ).enabled );
[0b33412]464
[36982fc]465 enable_interrupts( __cfaabi_dbg_ctx );
[eb2e723]466}
467
[ea7d2b0]468void BlockInternal(__spinlock_t * locks [], unsigned short count) {
[82ff5845]469 disable_interrupts();
[b10affd]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;
[0b33412]473
[b10affd]474 verify( ! TL_GET( preemption_state ).enabled );
[82c948c]475 returnToKernel();
[b10affd]476 verify( ! TL_GET( preemption_state ).enabled );
[0b33412]477
[36982fc]478 enable_interrupts( __cfaabi_dbg_ctx );
[0c78741]479}
480
[ea7d2b0]481void BlockInternal(__spinlock_t * locks [], unsigned short lock_count, thread_desc * thrds [], unsigned short thrd_count) {
[82ff5845]482 disable_interrupts();
[b10affd]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;
[0b33412]488
[b10affd]489 verify( ! TL_GET( preemption_state ).enabled );
[82c948c]490 returnToKernel();
[b10affd]491 verify( ! TL_GET( preemption_state ).enabled );
[0b33412]492
[36982fc]493 enable_interrupts( __cfaabi_dbg_ctx );
[0c78741]494}
495
[ea7d2b0]496void LeaveThread(__spinlock_t * lock, thread_desc * thrd) {
[b10affd]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;
[f2b12406]501
[82c948c]502 returnToKernel();
[f2b12406]503}
504
[fa21ac9]505//=============================================================================================
506// Kernel Setup logic
507//=============================================================================================
[eb2e723]508//-----------------------------------------------------------------------------
509// Kernel boot procedures
510void kernel_startup(void) {
[b10affd]511 verify( ! TL_GET( preemption_state ).enabled );
[36982fc]512 __cfaabi_dbg_print_safe("Kernel : Starting\n");
[eb2e723]513
514 // Start by initializing the main thread
[1c273d0]515 // SKULLDUGGERY: the mainThread steals the process main thread
[969b3fe]516 // which will then be scheduled by the mainProcessor normally
517 mainThread = (thread_desc *)&storage_mainThread;
[8fcbb4c]518 current_stack_info_t info;
[83a071f9]519 (*mainThread){ &info };
[eb2e723]520
[36982fc]521 __cfaabi_dbg_print_safe("Kernel : Main thread ready\n");
[fa21ac9]522
[969b3fe]523 // Initialize the main cluster
524 mainCluster = (cluster *)&storage_mainCluster;
[9236060]525 (*mainCluster){};
[bd98b58]526
[36982fc]527 __cfaabi_dbg_print_safe("Kernel : main cluster ready\n");
[fa21ac9]528
[969b3fe]529 // Initialize the main processor and the main processor ctx
[eb2e723]530 // (the coroutine that contains the processing control flow)
[969b3fe]531 mainProcessor = (processor *)&storage_mainProcessor;
[9236060]532 (*mainProcessor){ mainCluster, *(processorCtx_t *)&storage_mainProcessorCtx };
[eb2e723]533
[dcb42b8]534 //initialize the global state variables
[b10affd]535 TL_SET( this_processor, mainProcessor );
536 TL_SET( this_thread, mainThread );
537 TL_SET( this_coroutine, &mainThread->self_cor );
[eb2e723]538
[82ff5845]539 // Enable preemption
540 kernel_start_preemption();
541
[969b3fe]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
[dcb42b8]547 // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that
[1c273d0]548 // mainThread is on the ready queue when this call is made.
[b10affd]549 kernel_first_resume( TL_GET( this_processor ) );
[eb2e723]550
[dcb42b8]551
552
553 // THE SYSTEM IS NOW COMPLETELY RUNNING
[36982fc]554 __cfaabi_dbg_print_safe("Kernel : Started\n--------------------------------------------------\n\n");
[82ff5845]555
[b10affd]556 verify( ! TL_GET( preemption_state ).enabled );
[36982fc]557 enable_interrupts( __cfaabi_dbg_ctx );
[b10affd]558 verify( TL_GET( preemption_state ).enabled );
[eb2e723]559}
560
[dcb42b8]561void kernel_shutdown(void) {
[36982fc]562 __cfaabi_dbg_print_safe("\n--------------------------------------------------\nKernel : Shutting down\n");
[eb2e723]563
[b10affd]564 verify( TL_GET( preemption_state ).enabled );
[4e6fb8e]565 disable_interrupts();
[b10affd]566 verify( ! TL_GET( preemption_state ).enabled );
[4e6fb8e]567
[969b3fe]568 // SKULLDUGGERY: Notify the mainProcessor it needs to terminates.
[dcb42b8]569 // When its coroutine terminates, it return control to the mainThread
570 // which is currently here
[969b3fe]571 mainProcessor->do_terminate = true;
[82c948c]572 returnToKernel();
[eb2e723]573
[dcb42b8]574 // THE SYSTEM IS NOW COMPLETELY STOPPED
[eb2e723]575
[82ff5845]576 // Disable preemption
577 kernel_stop_preemption();
578
[969b3fe]579 // Destroy the main processor and its context in reverse order of construction
[dcb42b8]580 // These were manually constructed so we need manually destroy them
[094476d]581 ^(mainProcessor->runner){};
[969b3fe]582 ^(mainProcessor){};
[eb2e723]583
[dcb42b8]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
[eb2e723]586 ^(mainThread){};
587
[36982fc]588 __cfaabi_dbg_print_safe("Kernel : Shutdown complete\n");
[9d944b2]589}
590
[dbe9b08]591//=============================================================================================
592// Unexpected Terminating logic
593//=============================================================================================
594
595
[ea7d2b0]596static __spinlock_t kernel_abort_lock;
597static __spinlock_t kernel_debug_lock;
[9d944b2]598static bool kernel_abort_called = false;
599
600void * 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.
[36982fc]603 lock( kernel_abort_lock __cfaabi_dbg_ctx2 );
[9d944b2]604
605 // first task to abort ?
[b10affd]606 if ( ! kernel_abort_called ) { // not first task to abort ?
[9d944b2]607 kernel_abort_called = true;
[ea7d2b0]608 unlock( kernel_abort_lock );
[1c273d0]609 }
[9d944b2]610 else {
[ea7d2b0]611 unlock( kernel_abort_lock );
[1c273d0]612
[9d944b2]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
[1c273d0]618 _exit( EXIT_FAILURE ); // if processor unblocks before it is killed, terminate it
[9d944b2]619 }
620
[b10affd]621 return TL_GET( this_thread );
[9d944b2]622}
623
624void kernel_abort_msg( void * kernel_data, char * abort_text, int abort_text_size ) {
625 thread_desc * thrd = kernel_data;
626
[b18830e]627 int len = snprintf( abort_text, abort_text_size, "Error occurred while executing task %.256s (%p)", thrd->self_cor.name, thrd );
[36982fc]628 __cfaabi_dbg_bits_write( abort_text, len );
[9d944b2]629
[01963df]630 if ( get_coroutine(thrd) != TL_GET( this_coroutine ) ) {
[b10affd]631 len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", TL_GET( this_coroutine )->name, TL_GET( this_coroutine ) );
[36982fc]632 __cfaabi_dbg_bits_write( abort_text, len );
[1c273d0]633 }
[9d944b2]634 else {
[36982fc]635 __cfaabi_dbg_bits_write( ".\n", 2 );
[9d944b2]636 }
637}
638
[2b8bc41]639int kernel_abort_lastframe( void ) __attribute__ ((__nothrow__)) {
[b10affd]640 return get_coroutine(TL_GET( this_thread )) == get_coroutine(mainThread) ? 4 : 2;
[2b8bc41]641}
642
[9d944b2]643extern "C" {
[36982fc]644 void __cfaabi_dbg_bits_acquire() {
645 lock( kernel_debug_lock __cfaabi_dbg_ctx2 );
[9d944b2]646 }
647
[36982fc]648 void __cfaabi_dbg_bits_release() {
[ea7d2b0]649 unlock( kernel_debug_lock );
[9d944b2]650 }
[8118303]651}
652
[fa21ac9]653//=============================================================================================
654// Kernel Utilities
655//=============================================================================================
[bd98b58]656//-----------------------------------------------------------------------------
657// Locks
[242a902]658void ?{}( semaphore & this, int count = 1 ) {
659 (this.lock){};
660 this.count = count;
661 (this.waiting){};
[db6f06a]662}
[242a902]663void ^?{}(semaphore & this) {}
[db6f06a]664
[65deb18]665void P(semaphore & this) with( this ){
666 lock( lock __cfaabi_dbg_ctx2 );
667 count -= 1;
668 if ( count < 0 ) {
[bdeba0b]669 // queue current task
[b10affd]670 append( waiting, (thread_desc *)TL_GET( this_thread ) );
[bdeba0b]671
672 // atomically release spin lock and block
[65deb18]673 BlockInternal( &lock );
[8def349]674 }
[4e6fb8e]675 else {
[65deb18]676 unlock( lock );
[4e6fb8e]677 }
[bd98b58]678}
679
[65deb18]680void V(semaphore & this) with( this ) {
[bdeba0b]681 thread_desc * thrd = NULL;
[65deb18]682 lock( lock __cfaabi_dbg_ctx2 );
683 count += 1;
684 if ( count <= 0 ) {
[bdeba0b]685 // remove task at head of waiting list
[65deb18]686 thrd = pop_head( waiting );
[bd98b58]687 }
[bdeba0b]688
[65deb18]689 unlock( lock );
[bdeba0b]690
691 // make new owner
692 WakeThread( thrd );
[bd98b58]693}
694
[f7d6bb0]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)
[8118303]728// Local Variables: //
729// mode: c //
730// tab-width: 4 //
731// End: //
Note: See TracBrowser for help on using the repository browser.