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

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 stuck-waitfor-destruct with_gc
Last change on this file since b4a835d was afd550c, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Some more work on TLS macros

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