source: src/libcfa/concurrency/kernel.c@ 3ce0d440

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 3ce0d440 was 6b4cdd3, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Added basic core quiescing

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