source: libcfa/src/concurrency/kernel.cfa@ 211228e0

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 211228e0 was 8c01e1b, checked in by tdelisle <tdelisle@…>, 7 years ago

UserStack flag on coroutines is now folded into the storage pointer

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