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

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 3fc59bdb was 85b1deb, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Fix processor halting

  • Property mode set to 100644
File size: 23.6 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>
[9d944b2]24#include <signal.h>
25#include <unistd.h>
[eb2e723]26}
[8118303]27
28//CFA Includes
[0f56058]29#include "time"
[2ac095d]30#include "kernel_private.h"
[c81ebf9]31#include "preemption.h"
[2ac095d]32#include "startup.h"
[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
39void kernel_startup(void) __attribute__(( constructor( STARTUP_PRIORITY_KERNEL ) ));
40void kernel_shutdown(void) __attribute__(( destructor ( STARTUP_PRIORITY_KERNEL ) ));
41
[8def349]42//-----------------------------------------------------------------------------
43// Kernel storage
[969b3fe]44KERNEL_STORAGE(cluster, mainCluster);
45KERNEL_STORAGE(processor, mainProcessor);
46KERNEL_STORAGE(thread_desc, mainThread);
[f2b12406]47KERNEL_STORAGE(machine_context_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
[bd98b58]57//-----------------------------------------------------------------------------
58// Global state
[14a61b5]59thread_local struct KernelThreadData kernelTLS = {
[b10affd]60 NULL,
61 NULL,
62 NULL,
63 { 1, false, false }
64};
[c84e80a]65
66//-----------------------------------------------------------------------------
[de6319f]67// Struct to steal stack
[8def349]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
[de6319f]92//-----------------------------------------------------------------------------
93// Main thread construction
[65deb18]94void ?{}( coStack_t & this, current_stack_info_t * info) with( this ) {
95 size = info->size;
96 storage = info->storage;
97 limit = info->limit;
98 base = info->base;
99 context = info->context;
100 top = info->top;
101 userStack = true;
[8def349]102}
103
[65deb18]104void ?{}( coroutine_desc & this, current_stack_info_t * info) with( this ) {
105 stack{ info };
106 name = "Main Thread";
107 errno_ = 0;
108 state = Start;
109 starter = NULL;
[8def349]110}
111
[65deb18]112void ?{}( thread_desc & this, current_stack_info_t * info) with( this ) {
113 self_cor{ info };
[82c948c]114 curr_cor = &self_cor;
[de6319f]115 curr_cluster = mainCluster;
[82c948c]116 self_mon.owner = &this;
117 self_mon.recursion = 1;
118 self_mon_p = &self_mon;
119 next = NULL;
[de94a60]120
121 node.next = NULL;
122 node.prev = NULL;
[a1a17a74]123 doregister(curr_cluster, this);
[82c948c]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
[85b1deb]149 idleLock{};
[6b4cdd3]150
[242a902]151 start( &this );
[c84e80a]152}
153
[65deb18]154void ^?{}(processor & this) with( this ){
[ea8b2f7]155 if( ! __atomic_load_n(&do_terminate, __ATOMIC_ACQUIRE) ) {
[36982fc]156 __cfaabi_dbg_print_safe("Kernel : core %p signaling termination\n", &this);
[85b1deb]157
158 __atomic_store_n(&do_terminate, true, __ATOMIC_RELAXED);
159 wake( &this );
160
[65deb18]161 P( terminated );
[14a61b5]162 verify( kernelTLS.this_processor != &this);
[8def349]163 }
[6b4cdd3]164
[85b1deb]165 pthread_join( kernel_thread, NULL );
[8def349]166}
167
[de6319f]168void ?{}(cluster & this, const char * name, Duration preemption_rate) with( this ) {
169 this.name = name;
170 this.preemption_rate = preemption_rate;
[65deb18]171 ready_queue{};
172 ready_queue_lock{};
[de94a60]173
174 procs{ __get };
175 idles{ __get };
[a1a17a74]176 threads{ __get };
[de94a60]177
178 doregister(this);
[8def349]179}
180
[242a902]181void ^?{}(cluster & this) {
[de94a60]182 unregister(this);
[c84e80a]183}
184
[75f3522]185//=============================================================================================
186// Kernel Scheduling logic
187//=============================================================================================
[8fcbb4c]188//Main of the processor contexts
[83a071f9]189void main(processorCtx_t & runner) {
190 processor * this = runner.proc;
[094476d]191 verify(this);
[c81ebf9]192
[36982fc]193 __cfaabi_dbg_print_safe("Kernel : core %p starting\n", this);
[8118303]194
[de94a60]195 doregister(this->cltr, this);
196
[75f3522]197 {
[c81ebf9]198 // Setup preemption data
199 preemption_scope scope = { this };
200
[36982fc]201 __cfaabi_dbg_print_safe("Kernel : core %p started\n", this);
[8118303]202
[c81ebf9]203 thread_desc * readyThread = NULL;
[ea8b2f7]204 for( unsigned int spin_count = 0; ! __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST); spin_count++ )
[75f3522]205 {
[c81ebf9]206 readyThread = nextThread( this->cltr );
[75f3522]207
[c81ebf9]208 if(readyThread)
209 {
[14a61b5]210 verify( ! kernelTLS.preemption_state.enabled );
[4e6fb8e]211
[c81ebf9]212 runThread(this, readyThread);
[75f3522]213
[14a61b5]214 verify( ! kernelTLS.preemption_state.enabled );
[4e6fb8e]215
[c81ebf9]216 //Some actions need to be taken from the kernel
217 finishRunning(this);
218
219 spin_count = 0;
220 }
221 else
222 {
[ea8b2f7]223 // spin(this, &spin_count);
224 halt(this);
[c81ebf9]225 }
226 }
227
[36982fc]228 __cfaabi_dbg_print_safe("Kernel : core %p stopping\n", this);
[c84e80a]229 }
[8118303]230
[de94a60]231 unregister(this->cltr, this);
232
[4cedd9f]233 V( this->terminated );
[bdeba0b]234
[36982fc]235 __cfaabi_dbg_print_safe("Kernel : core %p terminated\n", this);
[c84e80a]236}
237
[14a61b5]238// KERNEL ONLY
[1c273d0]239// runThread runs a thread by context switching
240// from the processor coroutine to the target thread
[348006f]241void runThread(processor * this, thread_desc * dst) {
[82c948c]242 assert(dst->curr_cor);
[094476d]243 coroutine_desc * proc_cor = get_coroutine(this->runner);
[82c948c]244 coroutine_desc * thrd_cor = dst->curr_cor;
[1c273d0]245
[14a61b5]246 // Reset the terminating actions here
[db6f06a]247 this->finish.action_code = No_Action;
[8fcbb4c]248
[14a61b5]249 // Update global state
250 kernelTLS.this_thread = dst;
[75f3522]251
252 // Context Switch to the thread
253 ThreadCtxSwitch(proc_cor, thrd_cor);
254 // when ThreadCtxSwitch returns we are back in the processor coroutine
255}
256
[14a61b5]257// KERNEL_ONLY
[82c948c]258void returnToKernel() {
[14a61b5]259 coroutine_desc * proc_cor = get_coroutine(kernelTLS.this_processor->runner);
260 coroutine_desc * thrd_cor = kernelTLS.this_thread->curr_cor = kernelTLS.this_coroutine;
[82c948c]261 ThreadCtxSwitch(thrd_cor, proc_cor);
262}
263
[14a61b5]264// KERNEL_ONLY
[1c273d0]265// Once a thread has finished running, some of
[75f3522]266// its final actions must be executed from the kernel
[65deb18]267void finishRunning(processor * this) with( this->finish ) {
[09800e9]268 verify( ! kernelTLS.preemption_state.enabled );
269 choose( action_code ) {
270 case No_Action:
271 break;
272 case Release:
[65deb18]273 unlock( *lock );
[09800e9]274 case Schedule:
[65deb18]275 ScheduleThread( thrd );
[09800e9]276 case Release_Schedule:
[65deb18]277 unlock( *lock );
278 ScheduleThread( thrd );
[09800e9]279 case Release_Multi:
[65deb18]280 for(int i = 0; i < lock_count; i++) {
281 unlock( *locks[i] );
[0c78741]282 }
[09800e9]283 case Release_Multi_Schedule:
[65deb18]284 for(int i = 0; i < lock_count; i++) {
285 unlock( *locks[i] );
[0c78741]286 }
[65deb18]287 for(int i = 0; i < thrd_count; i++) {
288 ScheduleThread( thrds[i] );
[0c78741]289 }
[09800e9]290 case Callback:
291 callback();
292 default:
293 abort("KERNEL ERROR: Unexpected action to run after thread");
[8fcbb4c]294 }
[c84e80a]295}
296
[14a61b5]297// KERNEL_ONLY
[0c92c9f]298// Context invoker for processors
299// This is the entry point for processors (kernel threads)
300// It effectively constructs a coroutine by stealing the pthread stack
[8def349]301void * CtxInvokeProcessor(void * arg) {
302 processor * proc = (processor *) arg;
[14a61b5]303 kernelTLS.this_processor = proc;
304 kernelTLS.this_coroutine = NULL;
305 kernelTLS.this_thread = NULL;
306 kernelTLS.preemption_state.[enabled, disable_count] = [false, 1];
[8def349]307 // SKULLDUGGERY: We want to create a context for the processor coroutine
308 // which is needed for the 2-step context switch. However, there is no reason
[1c273d0]309 // to waste the perfectly valid stack create by pthread.
[8def349]310 current_stack_info_t info;
311 machine_context_t ctx;
312 info.context = &ctx;
[094476d]313 (proc->runner){ proc, &info };
[8def349]314
[094476d]315 __cfaabi_dbg_print_safe("Coroutine : created stack %p\n", get_coroutine(proc->runner)->stack.base);
[8fcbb4c]316
[0c92c9f]317 //Set global state
[14a61b5]318 kernelTLS.this_coroutine = get_coroutine(proc->runner);
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
[8def349]338void 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) {
[14a61b5]348 coroutine_desc * src = kernelTLS.this_coroutine;
[094476d]349 coroutine_desc * dst = get_coroutine(this->runner);
[b69ea6b]350
[14a61b5]351 verify( ! kernelTLS.preemption_state.enabled );
[b69ea6b]352
353 create_stack(&dst->stack, dst->stack.size);
[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 // set new coroutine that task is executing
[14a61b5]365 kernelTLS.this_coroutine = dst;
[b69ea6b]366
367 // SKULLDUGGERY normally interrupts are enable before leaving a coroutine ctxswitch.
368 // Therefore, when first creating a coroutine, interrupts are enable before calling the main.
369 // This is consistent with thread creation. However, when creating the main processor coroutine,
370 // we wan't interrupts to be disabled. Therefore, we double-disable interrupts here so they will
371 // stay disabled.
372 disable_interrupts();
373
374 // context switch to specified coroutine
375 assert( src->stack.context );
376 CtxSwitch( src->stack.context, dst->stack.context );
377 // when CtxSwitch returns we are back in the src coroutine
378
379 // set state of new coroutine to active
380 src->state = Active;
381
[14a61b5]382 verify( ! kernelTLS.preemption_state.enabled );
[b69ea6b]383}
384
[8def349]385//-----------------------------------------------------------------------------
386// Scheduler routines
[14a61b5]387
388// KERNEL ONLY
[348006f]389void ScheduleThread( thread_desc * thrd ) {
[135b431]390 verify( thrd );
[b18830e]391 verify( thrd->self_cor.state != Halted );
[1c273d0]392
[14a61b5]393 verify( ! kernelTLS.preemption_state.enabled );
[690f13c]394
[4aa2fb2]395 verifyf( thrd->next == NULL, "Expected null got %p", thrd->next );
[1c273d0]396
[de6319f]397 with( *thrd->curr_cluster ) {
[65deb18]398 lock ( ready_queue_lock __cfaabi_dbg_ctx2 );
[6b4cdd3]399 bool was_empty = !(ready_queue != 0);
[65deb18]400 append( ready_queue, thrd );
401 unlock( ready_queue_lock );
[6b4cdd3]402
[85b1deb]403 if(was_empty) {
[6b4cdd3]404 lock (proc_list_lock __cfaabi_dbg_ctx2);
405 if(idles) {
[85b1deb]406 wake_fast(idles.head);
[6b4cdd3]407 }
408 unlock (proc_list_lock);
409 }
[85b1deb]410 else if( struct processor * idle = idles.head ) {
411 wake_fast(idle);
412 }
413
[65deb18]414 }
[1c273d0]415
[14a61b5]416 verify( ! kernelTLS.preemption_state.enabled );
[db6f06a]417}
418
[14a61b5]419// KERNEL ONLY
[65deb18]420thread_desc * nextThread(cluster * this) with( *this ) {
[14a61b5]421 verify( ! kernelTLS.preemption_state.enabled );
[65deb18]422 lock( ready_queue_lock __cfaabi_dbg_ctx2 );
423 thread_desc * head = pop_head( ready_queue );
424 unlock( ready_queue_lock );
[14a61b5]425 verify( ! kernelTLS.preemption_state.enabled );
[db6f06a]426 return head;
[eb2e723]427}
428
[82ff5845]429void BlockInternal() {
430 disable_interrupts();
[14a61b5]431 verify( ! kernelTLS.preemption_state.enabled );
[82c948c]432 returnToKernel();
[14a61b5]433 verify( ! kernelTLS.preemption_state.enabled );
[36982fc]434 enable_interrupts( __cfaabi_dbg_ctx );
[75f3522]435}
436
[ea7d2b0]437void BlockInternal( __spinlock_t * lock ) {
[82ff5845]438 disable_interrupts();
[14a61b5]439 with( *kernelTLS.this_processor ) {
[de6319f]440 finish.action_code = Release;
441 finish.lock = lock;
442 }
[0b33412]443
[afd550c]444 verify( ! kernelTLS.preemption_state.enabled );
[82c948c]445 returnToKernel();
[afd550c]446 verify( ! kernelTLS.preemption_state.enabled );
[0b33412]447
[36982fc]448 enable_interrupts( __cfaabi_dbg_ctx );
[db6f06a]449}
450
[82ff5845]451void BlockInternal( thread_desc * thrd ) {
452 disable_interrupts();
[14a61b5]453 with( * kernelTLS.this_processor ) {
[de6319f]454 finish.action_code = Schedule;
455 finish.thrd = thrd;
456 }
[0b33412]457
[14a61b5]458 verify( ! kernelTLS.preemption_state.enabled );
[82c948c]459 returnToKernel();
[14a61b5]460 verify( ! kernelTLS.preemption_state.enabled );
[0b33412]461
[36982fc]462 enable_interrupts( __cfaabi_dbg_ctx );
[db6f06a]463}
464
[ea7d2b0]465void BlockInternal( __spinlock_t * lock, thread_desc * thrd ) {
[97e3296]466 assert(thrd);
[82ff5845]467 disable_interrupts();
[14a61b5]468 with( * kernelTLS.this_processor ) {
[de6319f]469 finish.action_code = Release_Schedule;
470 finish.lock = lock;
471 finish.thrd = thrd;
472 }
[0b33412]473
[14a61b5]474 verify( ! kernelTLS.preemption_state.enabled );
[82c948c]475 returnToKernel();
[14a61b5]476 verify( ! kernelTLS.preemption_state.enabled );
[0b33412]477
[36982fc]478 enable_interrupts( __cfaabi_dbg_ctx );
[eb2e723]479}
480
[ea7d2b0]481void BlockInternal(__spinlock_t * locks [], unsigned short count) {
[82ff5845]482 disable_interrupts();
[14a61b5]483 with( * kernelTLS.this_processor ) {
[de6319f]484 finish.action_code = Release_Multi;
485 finish.locks = locks;
486 finish.lock_count = count;
487 }
[0b33412]488
[14a61b5]489 verify( ! kernelTLS.preemption_state.enabled );
[82c948c]490 returnToKernel();
[14a61b5]491 verify( ! kernelTLS.preemption_state.enabled );
[0b33412]492
[36982fc]493 enable_interrupts( __cfaabi_dbg_ctx );
[0c78741]494}
495
[ea7d2b0]496void BlockInternal(__spinlock_t * locks [], unsigned short lock_count, thread_desc * thrds [], unsigned short thrd_count) {
[82ff5845]497 disable_interrupts();
[14a61b5]498 with( *kernelTLS.this_processor ) {
[de6319f]499 finish.action_code = Release_Multi_Schedule;
500 finish.locks = locks;
501 finish.lock_count = lock_count;
502 finish.thrds = thrds;
503 finish.thrd_count = thrd_count;
504 }
[0b33412]505
[14a61b5]506 verify( ! kernelTLS.preemption_state.enabled );
[82c948c]507 returnToKernel();
[14a61b5]508 verify( ! kernelTLS.preemption_state.enabled );
[0b33412]509
[36982fc]510 enable_interrupts( __cfaabi_dbg_ctx );
[0c78741]511}
512
[09800e9]513void BlockInternal(__finish_callback_fptr_t callback) {
514 disable_interrupts();
515 with( *kernelTLS.this_processor ) {
516 finish.action_code = Callback;
517 finish.callback = callback;
518 }
519
520 verify( ! kernelTLS.preemption_state.enabled );
521 returnToKernel();
522 verify( ! kernelTLS.preemption_state.enabled );
523
524 enable_interrupts( __cfaabi_dbg_ctx );
525}
526
[14a61b5]527// KERNEL ONLY
[ea7d2b0]528void LeaveThread(__spinlock_t * lock, thread_desc * thrd) {
[14a61b5]529 verify( ! kernelTLS.preemption_state.enabled );
530 with( * kernelTLS.this_processor ) {
[de6319f]531 finish.action_code = thrd ? Release_Schedule : Release;
532 finish.lock = lock;
533 finish.thrd = thrd;
534 }
[f2b12406]535
[82c948c]536 returnToKernel();
[f2b12406]537}
538
[fa21ac9]539//=============================================================================================
540// Kernel Setup logic
541//=============================================================================================
[eb2e723]542//-----------------------------------------------------------------------------
543// Kernel boot procedures
544void kernel_startup(void) {
[14a61b5]545 verify( ! kernelTLS.preemption_state.enabled );
[36982fc]546 __cfaabi_dbg_print_safe("Kernel : Starting\n");
[eb2e723]547
[ea8b2f7]548 __cfa_dbg_global_clusters.list{ __get };
549 __cfa_dbg_global_clusters.lock{};
[de94a60]550
[de6319f]551 // Initialize the main cluster
552 mainCluster = (cluster *)&storage_mainCluster;
553 (*mainCluster){"Main Cluster"};
554
555 __cfaabi_dbg_print_safe("Kernel : Main cluster ready\n");
556
[eb2e723]557 // Start by initializing the main thread
[1c273d0]558 // SKULLDUGGERY: the mainThread steals the process main thread
[969b3fe]559 // which will then be scheduled by the mainProcessor normally
560 mainThread = (thread_desc *)&storage_mainThread;
[8fcbb4c]561 current_stack_info_t info;
[83a071f9]562 (*mainThread){ &info };
[eb2e723]563
[36982fc]564 __cfaabi_dbg_print_safe("Kernel : Main thread ready\n");
[fa21ac9]565
[bd98b58]566
[de6319f]567
568 // Construct the processor context of the main processor
569 void ?{}(processorCtx_t & this, processor * proc) {
570 (this.__cor){ "Processor" };
571 this.__cor.starter = NULL;
572 this.proc = proc;
573 }
574
575 void ?{}(processor & this) with( this ) {
576 name = "Main Processor";
577 cltr = mainCluster;
578 terminated{ 0 };
579 do_terminate = false;
580 preemption_alarm = NULL;
581 pending_preemption = false;
582 kernel_thread = pthread_self();
583
584 runner{ &this };
585 __cfaabi_dbg_print_safe("Kernel : constructed main processor context %p\n", &runner);
586 }
[fa21ac9]587
[969b3fe]588 // Initialize the main processor and the main processor ctx
[eb2e723]589 // (the coroutine that contains the processing control flow)
[969b3fe]590 mainProcessor = (processor *)&storage_mainProcessor;
[de6319f]591 (*mainProcessor){};
[eb2e723]592
[dcb42b8]593 //initialize the global state variables
[14a61b5]594 kernelTLS.this_processor = mainProcessor;
595 kernelTLS.this_thread = mainThread;
596 kernelTLS.this_coroutine = &mainThread->self_cor;
[eb2e723]597
[82ff5845]598 // Enable preemption
599 kernel_start_preemption();
600
[969b3fe]601 // Add the main thread to the ready queue
602 // once resume is called on mainProcessor->runner the mainThread needs to be scheduled like any normal thread
603 ScheduleThread(mainThread);
604
605 // SKULLDUGGERY: Force a context switch to the main processor to set the main thread's context to the current UNIX
[dcb42b8]606 // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that
[1c273d0]607 // mainThread is on the ready queue when this call is made.
[14a61b5]608 kernel_first_resume( kernelTLS.this_processor );
[eb2e723]609
[dcb42b8]610
611
612 // THE SYSTEM IS NOW COMPLETELY RUNNING
[36982fc]613 __cfaabi_dbg_print_safe("Kernel : Started\n--------------------------------------------------\n\n");
[82ff5845]614
[14a61b5]615 verify( ! kernelTLS.preemption_state.enabled );
[36982fc]616 enable_interrupts( __cfaabi_dbg_ctx );
[afd550c]617 verify( TL_GET( preemption_state.enabled ) );
[eb2e723]618}
619
[dcb42b8]620void kernel_shutdown(void) {
[36982fc]621 __cfaabi_dbg_print_safe("\n--------------------------------------------------\nKernel : Shutting down\n");
[eb2e723]622
[afd550c]623 verify( TL_GET( preemption_state.enabled ) );
[4e6fb8e]624 disable_interrupts();
[14a61b5]625 verify( ! kernelTLS.preemption_state.enabled );
[4e6fb8e]626
[969b3fe]627 // SKULLDUGGERY: Notify the mainProcessor it needs to terminates.
[dcb42b8]628 // When its coroutine terminates, it return control to the mainThread
629 // which is currently here
[ea8b2f7]630 __atomic_store_n(&mainProcessor->do_terminate, true, __ATOMIC_RELEASE);
[82c948c]631 returnToKernel();
[ea8b2f7]632 mainThread->self_cor.state = Halted;
[eb2e723]633
[dcb42b8]634 // THE SYSTEM IS NOW COMPLETELY STOPPED
[eb2e723]635
[82ff5845]636 // Disable preemption
637 kernel_stop_preemption();
638
[969b3fe]639 // Destroy the main processor and its context in reverse order of construction
[dcb42b8]640 // These were manually constructed so we need manually destroy them
[094476d]641 ^(mainProcessor->runner){};
[969b3fe]642 ^(mainProcessor){};
[eb2e723]643
[dcb42b8]644 // Final step, destroy the main thread since it is no longer needed
645 // Since we provided a stack to this taxk it will not destroy anything
[eb2e723]646 ^(mainThread){};
647
[ea8b2f7]648 ^(__cfa_dbg_global_clusters.list){};
649 ^(__cfa_dbg_global_clusters.lock){};
[a1a17a74]650
[36982fc]651 __cfaabi_dbg_print_safe("Kernel : Shutdown complete\n");
[9d944b2]652}
653
[14a61b5]654//=============================================================================================
655// Kernel Quiescing
656//=============================================================================================
657
[6b4cdd3]658void halt(processor * this) with( *this ) {
[85b1deb]659 // verify( ! __atomic_load_n(&do_terminate, __ATOMIC_SEQ_CST) );
[ea8b2f7]660
[6b4cdd3]661 with( *cltr ) {
662 lock (proc_list_lock __cfaabi_dbg_ctx2);
663 remove (procs, *this);
664 push_front(idles, *this);
665 unlock (proc_list_lock);
666 }
[14a61b5]667
[6b4cdd3]668 __cfaabi_dbg_print_safe("Kernel : Processor %p ready to sleep\n", this);
[14a61b5]669
[85b1deb]670 wait( idleLock );
[14a61b5]671
[6b4cdd3]672 __cfaabi_dbg_print_safe("Kernel : Processor %p woke up and ready to run\n", this);
[14a61b5]673
[6b4cdd3]674 with( *cltr ) {
675 lock (proc_list_lock __cfaabi_dbg_ctx2);
676 remove (idles, *this);
677 push_front(procs, *this);
678 unlock (proc_list_lock);
679 }
680}
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 ) {
[ea8b2f7]795 lock ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2);
796 push_front( __cfa_dbg_global_clusters.list, cltr );
797 unlock ( __cfa_dbg_global_clusters.lock );
[de94a60]798}
[f7d6bb0]799
[de94a60]800void unregister( cluster & cltr ) {
[ea8b2f7]801 lock ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2);
802 remove( __cfa_dbg_global_clusters.list, cltr );
803 unlock( __cfa_dbg_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.