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

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 a43c561 was ea8b2f7, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Immetidate fix for halting processors, drifting still an issue

  • Property mode set to 100644
File size: 24.7 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
[6b4cdd3]149 sem_init(&idleLock, 0, 0);
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);
[4dad189]157 terminate(&this);
[ea8b2f7]158 verify( __atomic_load_n(&do_terminate, __ATOMIC_SEQ_CST) );
[14a61b5]159 verify( kernelTLS.this_processor != &this);
[65deb18]160 P( terminated );
[14a61b5]161 verify( kernelTLS.this_processor != &this);
[65deb18]162 pthread_join( kernel_thread, NULL );
[8def349]163 }
[6b4cdd3]164
165 sem_destroy(&idleLock);
[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
[0c92c9f]297// Handles spinning logic
298// TODO : find some strategy to put cores to sleep after some time
[c84e80a]299void spin(processor * this, unsigned int * spin_count) {
[6b4cdd3]300 // (*spin_count)++;
301 halt(this);
[c84e80a]302}
303
[14a61b5]304// KERNEL_ONLY
[0c92c9f]305// Context invoker for processors
306// This is the entry point for processors (kernel threads)
307// It effectively constructs a coroutine by stealing the pthread stack
[8def349]308void * CtxInvokeProcessor(void * arg) {
309 processor * proc = (processor *) arg;
[14a61b5]310 kernelTLS.this_processor = proc;
311 kernelTLS.this_coroutine = NULL;
312 kernelTLS.this_thread = NULL;
313 kernelTLS.preemption_state.[enabled, disable_count] = [false, 1];
[8def349]314 // SKULLDUGGERY: We want to create a context for the processor coroutine
315 // which is needed for the 2-step context switch. However, there is no reason
[1c273d0]316 // to waste the perfectly valid stack create by pthread.
[8def349]317 current_stack_info_t info;
318 machine_context_t ctx;
319 info.context = &ctx;
[094476d]320 (proc->runner){ proc, &info };
[8def349]321
[094476d]322 __cfaabi_dbg_print_safe("Coroutine : created stack %p\n", get_coroutine(proc->runner)->stack.base);
[8fcbb4c]323
[0c92c9f]324 //Set global state
[14a61b5]325 kernelTLS.this_coroutine = get_coroutine(proc->runner);
326 kernelTLS.this_thread = NULL;
[8def349]327
328 //We now have a proper context from which to schedule threads
[094476d]329 __cfaabi_dbg_print_safe("Kernel : core %p created (%p, %p)\n", proc, &proc->runner, &ctx);
[8def349]330
[1c273d0]331 // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't
332 // resume it to start it like it normally would, it will just context switch
333 // back to here. Instead directly call the main since we already are on the
[8def349]334 // appropriate stack.
[094476d]335 get_coroutine(proc->runner)->state = Active;
336 main( proc->runner );
337 get_coroutine(proc->runner)->state = Halted;
[8def349]338
[0c92c9f]339 // Main routine of the core returned, the core is now fully terminated
[094476d]340 __cfaabi_dbg_print_safe("Kernel : core %p main ended (%p)\n", proc, &proc->runner);
[8def349]341
342 return NULL;
[c84e80a]343}
344
[8def349]345void start(processor * this) {
[36982fc]346 __cfaabi_dbg_print_safe("Kernel : Starting core %p\n", this);
[82ff5845]347
[8fcbb4c]348 pthread_create( &this->kernel_thread, NULL, CtxInvokeProcessor, (void*)this );
[eb2e723]349
[36982fc]350 __cfaabi_dbg_print_safe("Kernel : core %p started\n", this);
[eb2e723]351}
352
[14a61b5]353// KERNEL_ONLY
[b69ea6b]354void kernel_first_resume(processor * this) {
[14a61b5]355 coroutine_desc * src = kernelTLS.this_coroutine;
[094476d]356 coroutine_desc * dst = get_coroutine(this->runner);
[b69ea6b]357
[14a61b5]358 verify( ! kernelTLS.preemption_state.enabled );
[b69ea6b]359
360 create_stack(&dst->stack, dst->stack.size);
[094476d]361 CtxStart(&this->runner, CtxInvokeCoroutine);
[b69ea6b]362
[14a61b5]363 verify( ! kernelTLS.preemption_state.enabled );
[b69ea6b]364
365 dst->last = src;
366 dst->starter = dst->starter ? dst->starter : src;
367
368 // set state of current coroutine to inactive
369 src->state = src->state == Halted ? Halted : Inactive;
370
371 // set new coroutine that task is executing
[14a61b5]372 kernelTLS.this_coroutine = dst;
[b69ea6b]373
374 // SKULLDUGGERY normally interrupts are enable before leaving a coroutine ctxswitch.
375 // Therefore, when first creating a coroutine, interrupts are enable before calling the main.
376 // This is consistent with thread creation. However, when creating the main processor coroutine,
377 // we wan't interrupts to be disabled. Therefore, we double-disable interrupts here so they will
378 // stay disabled.
379 disable_interrupts();
380
381 // context switch to specified coroutine
382 assert( src->stack.context );
383 CtxSwitch( src->stack.context, dst->stack.context );
384 // when CtxSwitch returns we are back in the src coroutine
385
386 // set state of new coroutine to active
387 src->state = Active;
388
[14a61b5]389 verify( ! kernelTLS.preemption_state.enabled );
[b69ea6b]390}
391
[8def349]392//-----------------------------------------------------------------------------
393// Scheduler routines
[14a61b5]394
395// KERNEL ONLY
[348006f]396void ScheduleThread( thread_desc * thrd ) {
[135b431]397 verify( thrd );
[b18830e]398 verify( thrd->self_cor.state != Halted );
[1c273d0]399
[14a61b5]400 verify( ! kernelTLS.preemption_state.enabled );
[690f13c]401
[4aa2fb2]402 verifyf( thrd->next == NULL, "Expected null got %p", thrd->next );
[1c273d0]403
[de6319f]404 with( *thrd->curr_cluster ) {
[65deb18]405 lock ( ready_queue_lock __cfaabi_dbg_ctx2 );
[6b4cdd3]406 bool was_empty = !(ready_queue != 0);
[65deb18]407 append( ready_queue, thrd );
408 unlock( ready_queue_lock );
[6b4cdd3]409
410 if( was_empty ) {
411 lock (proc_list_lock __cfaabi_dbg_ctx2);
412 if(idles) {
413 wake(idles.head);
414 }
415 unlock (proc_list_lock);
416 }
[65deb18]417 }
[1c273d0]418
[14a61b5]419 verify( ! kernelTLS.preemption_state.enabled );
[db6f06a]420}
421
[14a61b5]422// KERNEL ONLY
[65deb18]423thread_desc * nextThread(cluster * this) with( *this ) {
[14a61b5]424 verify( ! kernelTLS.preemption_state.enabled );
[65deb18]425 lock( ready_queue_lock __cfaabi_dbg_ctx2 );
426 thread_desc * head = pop_head( ready_queue );
427 unlock( ready_queue_lock );
[14a61b5]428 verify( ! kernelTLS.preemption_state.enabled );
[db6f06a]429 return head;
[eb2e723]430}
431
[82ff5845]432void BlockInternal() {
433 disable_interrupts();
[14a61b5]434 verify( ! kernelTLS.preemption_state.enabled );
[82c948c]435 returnToKernel();
[14a61b5]436 verify( ! kernelTLS.preemption_state.enabled );
[36982fc]437 enable_interrupts( __cfaabi_dbg_ctx );
[75f3522]438}
439
[ea7d2b0]440void BlockInternal( __spinlock_t * lock ) {
[82ff5845]441 disable_interrupts();
[14a61b5]442 with( *kernelTLS.this_processor ) {
[de6319f]443 finish.action_code = Release;
444 finish.lock = lock;
445 }
[0b33412]446
[afd550c]447 verify( ! kernelTLS.preemption_state.enabled );
[82c948c]448 returnToKernel();
[afd550c]449 verify( ! kernelTLS.preemption_state.enabled );
[0b33412]450
[36982fc]451 enable_interrupts( __cfaabi_dbg_ctx );
[db6f06a]452}
453
[82ff5845]454void BlockInternal( thread_desc * thrd ) {
455 disable_interrupts();
[14a61b5]456 with( * kernelTLS.this_processor ) {
[de6319f]457 finish.action_code = Schedule;
458 finish.thrd = thrd;
459 }
[0b33412]460
[14a61b5]461 verify( ! kernelTLS.preemption_state.enabled );
[82c948c]462 returnToKernel();
[14a61b5]463 verify( ! kernelTLS.preemption_state.enabled );
[0b33412]464
[36982fc]465 enable_interrupts( __cfaabi_dbg_ctx );
[db6f06a]466}
467
[ea7d2b0]468void BlockInternal( __spinlock_t * lock, thread_desc * thrd ) {
[97e3296]469 assert(thrd);
[82ff5845]470 disable_interrupts();
[14a61b5]471 with( * kernelTLS.this_processor ) {
[de6319f]472 finish.action_code = Release_Schedule;
473 finish.lock = lock;
474 finish.thrd = thrd;
475 }
[0b33412]476
[14a61b5]477 verify( ! kernelTLS.preemption_state.enabled );
[82c948c]478 returnToKernel();
[14a61b5]479 verify( ! kernelTLS.preemption_state.enabled );
[0b33412]480
[36982fc]481 enable_interrupts( __cfaabi_dbg_ctx );
[eb2e723]482}
483
[ea7d2b0]484void BlockInternal(__spinlock_t * locks [], unsigned short count) {
[82ff5845]485 disable_interrupts();
[14a61b5]486 with( * kernelTLS.this_processor ) {
[de6319f]487 finish.action_code = Release_Multi;
488 finish.locks = locks;
489 finish.lock_count = count;
490 }
[0b33412]491
[14a61b5]492 verify( ! kernelTLS.preemption_state.enabled );
[82c948c]493 returnToKernel();
[14a61b5]494 verify( ! kernelTLS.preemption_state.enabled );
[0b33412]495
[36982fc]496 enable_interrupts( __cfaabi_dbg_ctx );
[0c78741]497}
498
[ea7d2b0]499void BlockInternal(__spinlock_t * locks [], unsigned short lock_count, thread_desc * thrds [], unsigned short thrd_count) {
[82ff5845]500 disable_interrupts();
[14a61b5]501 with( *kernelTLS.this_processor ) {
[de6319f]502 finish.action_code = Release_Multi_Schedule;
503 finish.locks = locks;
504 finish.lock_count = lock_count;
505 finish.thrds = thrds;
506 finish.thrd_count = thrd_count;
507 }
[0b33412]508
[14a61b5]509 verify( ! kernelTLS.preemption_state.enabled );
[82c948c]510 returnToKernel();
[14a61b5]511 verify( ! kernelTLS.preemption_state.enabled );
[0b33412]512
[36982fc]513 enable_interrupts( __cfaabi_dbg_ctx );
[0c78741]514}
515
[09800e9]516void BlockInternal(__finish_callback_fptr_t callback) {
517 disable_interrupts();
518 with( *kernelTLS.this_processor ) {
519 finish.action_code = Callback;
520 finish.callback = callback;
521 }
522
523 verify( ! kernelTLS.preemption_state.enabled );
524 returnToKernel();
525 verify( ! kernelTLS.preemption_state.enabled );
526
527 enable_interrupts( __cfaabi_dbg_ctx );
528}
529
[14a61b5]530// KERNEL ONLY
[ea7d2b0]531void LeaveThread(__spinlock_t * lock, thread_desc * thrd) {
[14a61b5]532 verify( ! kernelTLS.preemption_state.enabled );
533 with( * kernelTLS.this_processor ) {
[de6319f]534 finish.action_code = thrd ? Release_Schedule : Release;
535 finish.lock = lock;
536 finish.thrd = thrd;
537 }
[f2b12406]538
[82c948c]539 returnToKernel();
[f2b12406]540}
541
[fa21ac9]542//=============================================================================================
543// Kernel Setup logic
544//=============================================================================================
[eb2e723]545//-----------------------------------------------------------------------------
546// Kernel boot procedures
547void kernel_startup(void) {
[14a61b5]548 verify( ! kernelTLS.preemption_state.enabled );
[36982fc]549 __cfaabi_dbg_print_safe("Kernel : Starting\n");
[eb2e723]550
[ea8b2f7]551 __cfa_dbg_global_clusters.list{ __get };
552 __cfa_dbg_global_clusters.lock{};
[de94a60]553
[de6319f]554 // Initialize the main cluster
555 mainCluster = (cluster *)&storage_mainCluster;
556 (*mainCluster){"Main Cluster"};
557
558 __cfaabi_dbg_print_safe("Kernel : Main cluster ready\n");
559
[eb2e723]560 // Start by initializing the main thread
[1c273d0]561 // SKULLDUGGERY: the mainThread steals the process main thread
[969b3fe]562 // which will then be scheduled by the mainProcessor normally
563 mainThread = (thread_desc *)&storage_mainThread;
[8fcbb4c]564 current_stack_info_t info;
[83a071f9]565 (*mainThread){ &info };
[eb2e723]566
[36982fc]567 __cfaabi_dbg_print_safe("Kernel : Main thread ready\n");
[fa21ac9]568
[bd98b58]569
[de6319f]570
571 // Construct the processor context of the main processor
572 void ?{}(processorCtx_t & this, processor * proc) {
573 (this.__cor){ "Processor" };
574 this.__cor.starter = NULL;
575 this.proc = proc;
576 }
577
578 void ?{}(processor & this) with( this ) {
579 name = "Main Processor";
580 cltr = mainCluster;
581 terminated{ 0 };
582 do_terminate = false;
583 preemption_alarm = NULL;
584 pending_preemption = false;
585 kernel_thread = pthread_self();
586
587 runner{ &this };
588 __cfaabi_dbg_print_safe("Kernel : constructed main processor context %p\n", &runner);
589 }
[fa21ac9]590
[969b3fe]591 // Initialize the main processor and the main processor ctx
[eb2e723]592 // (the coroutine that contains the processing control flow)
[969b3fe]593 mainProcessor = (processor *)&storage_mainProcessor;
[de6319f]594 (*mainProcessor){};
[eb2e723]595
[dcb42b8]596 //initialize the global state variables
[14a61b5]597 kernelTLS.this_processor = mainProcessor;
598 kernelTLS.this_thread = mainThread;
599 kernelTLS.this_coroutine = &mainThread->self_cor;
[eb2e723]600
[82ff5845]601 // Enable preemption
602 kernel_start_preemption();
603
[969b3fe]604 // Add the main thread to the ready queue
605 // once resume is called on mainProcessor->runner the mainThread needs to be scheduled like any normal thread
606 ScheduleThread(mainThread);
607
608 // SKULLDUGGERY: Force a context switch to the main processor to set the main thread's context to the current UNIX
[dcb42b8]609 // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that
[1c273d0]610 // mainThread is on the ready queue when this call is made.
[14a61b5]611 kernel_first_resume( kernelTLS.this_processor );
[eb2e723]612
[dcb42b8]613
614
615 // THE SYSTEM IS NOW COMPLETELY RUNNING
[36982fc]616 __cfaabi_dbg_print_safe("Kernel : Started\n--------------------------------------------------\n\n");
[82ff5845]617
[14a61b5]618 verify( ! kernelTLS.preemption_state.enabled );
[36982fc]619 enable_interrupts( __cfaabi_dbg_ctx );
[afd550c]620 verify( TL_GET( preemption_state.enabled ) );
[eb2e723]621}
622
[dcb42b8]623void kernel_shutdown(void) {
[36982fc]624 __cfaabi_dbg_print_safe("\n--------------------------------------------------\nKernel : Shutting down\n");
[eb2e723]625
[afd550c]626 verify( TL_GET( preemption_state.enabled ) );
[4e6fb8e]627 disable_interrupts();
[14a61b5]628 verify( ! kernelTLS.preemption_state.enabled );
[4e6fb8e]629
[969b3fe]630 // SKULLDUGGERY: Notify the mainProcessor it needs to terminates.
[dcb42b8]631 // When its coroutine terminates, it return control to the mainThread
632 // which is currently here
[ea8b2f7]633 __atomic_store_n(&mainProcessor->do_terminate, true, __ATOMIC_RELEASE);
[82c948c]634 returnToKernel();
[ea8b2f7]635 mainThread->self_cor.state = Halted;
[eb2e723]636
[dcb42b8]637 // THE SYSTEM IS NOW COMPLETELY STOPPED
[eb2e723]638
[82ff5845]639 // Disable preemption
640 kernel_stop_preemption();
641
[969b3fe]642 // Destroy the main processor and its context in reverse order of construction
[dcb42b8]643 // These were manually constructed so we need manually destroy them
[094476d]644 ^(mainProcessor->runner){};
[969b3fe]645 ^(mainProcessor){};
[eb2e723]646
[dcb42b8]647 // Final step, destroy the main thread since it is no longer needed
648 // Since we provided a stack to this taxk it will not destroy anything
[eb2e723]649 ^(mainThread){};
650
[ea8b2f7]651 ^(__cfa_dbg_global_clusters.list){};
652 ^(__cfa_dbg_global_clusters.lock){};
[a1a17a74]653
[36982fc]654 __cfaabi_dbg_print_safe("Kernel : Shutdown complete\n");
[9d944b2]655}
656
[14a61b5]657//=============================================================================================
658// Kernel Quiescing
659//=============================================================================================
660
[6b4cdd3]661void halt(processor * this) with( *this ) {
[ea8b2f7]662 verify( ! __atomic_load_n(&do_terminate, __ATOMIC_SEQ_CST) );
663
[6b4cdd3]664 with( *cltr ) {
665 lock (proc_list_lock __cfaabi_dbg_ctx2);
666 remove (procs, *this);
667 push_front(idles, *this);
668 unlock (proc_list_lock);
669 }
[14a61b5]670
[6b4cdd3]671 __cfaabi_dbg_print_safe("Kernel : Processor %p ready to sleep\n", this);
[14a61b5]672
[ea8b2f7]673 // #ifdef __CFA_WITH_VERIFY__
674 // int sval = 0;
675 // sem_getvalue(&this->idleLock, &sval);
676 // verifyf(sval < 200, "Binary semaphore reached value %d : \n", sval);
677 // #endif
678
679 verify( ! __atomic_load_n(&do_terminate, __ATOMIC_SEQ_CST) );
[5510027]680 int __attribute__((unused)) ret = sem_wait(&idleLock);
[ea8b2f7]681 // verifyf(ret >= 0 || errno == EINTR, "Sem_wait returned %d (errno %d : %s\n", ret, errno, strerror(errno));
682
683 // wait( idleLock );
[14a61b5]684
[6b4cdd3]685 __cfaabi_dbg_print_safe("Kernel : Processor %p woke up and ready to run\n", this);
[14a61b5]686
[6b4cdd3]687 with( *cltr ) {
688 lock (proc_list_lock __cfaabi_dbg_ctx2);
689 remove (idles, *this);
690 push_front(procs, *this);
691 unlock (proc_list_lock);
692 }
693}
694
695void wake(processor * this) {
696 __cfaabi_dbg_print_safe("Kernel : Waking up processor %p\n", this);
[5510027]697 int __attribute__((unused)) ret = sem_post(&this->idleLock);
[ea8b2f7]698 // verifyf(ret >= 0 || errno == EINTR, "Sem_post returned %d (errno %d : %s\n", ret, errno, strerror(errno));
699
700 // #ifdef __CFA_WITH_VERIFY__
701 // int sval = 0;
702 // sem_getvalue(&this->idleLock, &sval);
703 // verifyf(sval < 200, "Binary semaphore reached value %d\n", sval);
704 // #endif
705
706 // post( this->idleLock );
[6b4cdd3]707}
[14a61b5]708
[dbe9b08]709//=============================================================================================
710// Unexpected Terminating logic
711//=============================================================================================
712
713
[ea7d2b0]714static __spinlock_t kernel_abort_lock;
[9d944b2]715static bool kernel_abort_called = false;
716
[afd550c]717void * kernel_abort(void) __attribute__ ((__nothrow__)) {
[9d944b2]718 // abort cannot be recursively entered by the same or different processors because all signal handlers return when
719 // the globalAbort flag is true.
[36982fc]720 lock( kernel_abort_lock __cfaabi_dbg_ctx2 );
[9d944b2]721
722 // first task to abort ?
[de94a60]723 if ( kernel_abort_called ) { // not first task to abort ?
[ea7d2b0]724 unlock( kernel_abort_lock );
[1c273d0]725
[9d944b2]726 sigset_t mask;
727 sigemptyset( &mask );
[de94a60]728 sigaddset( &mask, SIGALRM ); // block SIGALRM signals
729 sigsuspend( &mask ); // block the processor to prevent further damage during abort
730 _exit( EXIT_FAILURE ); // if processor unblocks before it is killed, terminate it
731 }
732 else {
733 kernel_abort_called = true;
734 unlock( kernel_abort_lock );
[9d944b2]735 }
736
[14a61b5]737 return kernelTLS.this_thread;
[9d944b2]738}
739
740void kernel_abort_msg( void * kernel_data, char * abort_text, int abort_text_size ) {
741 thread_desc * thrd = kernel_data;
742
[de94a60]743 if(thrd) {
744 int len = snprintf( abort_text, abort_text_size, "Error occurred while executing thread %.256s (%p)", thrd->self_cor.name, thrd );
[36982fc]745 __cfaabi_dbg_bits_write( abort_text, len );
[de94a60]746
747 if ( get_coroutine(thrd) != kernelTLS.this_coroutine ) {
748 len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", kernelTLS.this_coroutine->name, kernelTLS.this_coroutine );
749 __cfaabi_dbg_bits_write( abort_text, len );
750 }
751 else {
752 __cfaabi_dbg_bits_write( ".\n", 2 );
753 }
[1c273d0]754 }
[9d944b2]755 else {
[de94a60]756 int len = snprintf( abort_text, abort_text_size, "Error occurred outside of any thread.\n" );
[639991a]757 __cfaabi_dbg_bits_write( abort_text, len );
[9d944b2]758 }
759}
760
[2b8bc41]761int kernel_abort_lastframe( void ) __attribute__ ((__nothrow__)) {
[14a61b5]762 return get_coroutine(kernelTLS.this_thread) == get_coroutine(mainThread) ? 4 : 2;
[2b8bc41]763}
764
[de94a60]765static __spinlock_t kernel_debug_lock;
766
[9d944b2]767extern "C" {
[36982fc]768 void __cfaabi_dbg_bits_acquire() {
769 lock( kernel_debug_lock __cfaabi_dbg_ctx2 );
[9d944b2]770 }
771
[36982fc]772 void __cfaabi_dbg_bits_release() {
[ea7d2b0]773 unlock( kernel_debug_lock );
[9d944b2]774 }
[8118303]775}
776
[fa21ac9]777//=============================================================================================
778// Kernel Utilities
779//=============================================================================================
[bd98b58]780//-----------------------------------------------------------------------------
781// Locks
[242a902]782void ?{}( semaphore & this, int count = 1 ) {
783 (this.lock){};
784 this.count = count;
785 (this.waiting){};
[db6f06a]786}
[242a902]787void ^?{}(semaphore & this) {}
[db6f06a]788
[65deb18]789void P(semaphore & this) with( this ){
790 lock( lock __cfaabi_dbg_ctx2 );
791 count -= 1;
792 if ( count < 0 ) {
[bdeba0b]793 // queue current task
[14a61b5]794 append( waiting, kernelTLS.this_thread );
[bdeba0b]795
796 // atomically release spin lock and block
[65deb18]797 BlockInternal( &lock );
[8def349]798 }
[4e6fb8e]799 else {
[65deb18]800 unlock( lock );
[4e6fb8e]801 }
[bd98b58]802}
803
[65deb18]804void V(semaphore & this) with( this ) {
[bdeba0b]805 thread_desc * thrd = NULL;
[65deb18]806 lock( lock __cfaabi_dbg_ctx2 );
807 count += 1;
808 if ( count <= 0 ) {
[bdeba0b]809 // remove task at head of waiting list
[65deb18]810 thrd = pop_head( waiting );
[bd98b58]811 }
[bdeba0b]812
[65deb18]813 unlock( lock );
[bdeba0b]814
815 // make new owner
816 WakeThread( thrd );
[bd98b58]817}
818
[f7d6bb0]819//-----------------------------------------------------------------------------
[de94a60]820// Global Queues
821void doregister( cluster & cltr ) {
[ea8b2f7]822 lock ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2);
823 push_front( __cfa_dbg_global_clusters.list, cltr );
824 unlock ( __cfa_dbg_global_clusters.lock );
[de94a60]825}
[f7d6bb0]826
[de94a60]827void unregister( cluster & cltr ) {
[ea8b2f7]828 lock ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2);
829 remove( __cfa_dbg_global_clusters.list, cltr );
830 unlock( __cfa_dbg_global_clusters.lock );
[de94a60]831}
[f7d6bb0]832
[a1a17a74]833void doregister( cluster * cltr, thread_desc & thrd ) {
834 lock (cltr->thread_list_lock __cfaabi_dbg_ctx2);
835 push_front(cltr->threads, thrd);
836 unlock (cltr->thread_list_lock);
837}
838
839void unregister( cluster * cltr, thread_desc & thrd ) {
840 lock (cltr->thread_list_lock __cfaabi_dbg_ctx2);
841 remove(cltr->threads, thrd );
842 unlock(cltr->thread_list_lock);
843}
[9181f1d]844
[de94a60]845void doregister( cluster * cltr, processor * proc ) {
[639991a]846 lock (cltr->proc_list_lock __cfaabi_dbg_ctx2);
847 push_front(cltr->procs, *proc);
848 unlock (cltr->proc_list_lock);
[de94a60]849}
850
851void unregister( cluster * cltr, processor * proc ) {
[639991a]852 lock (cltr->proc_list_lock __cfaabi_dbg_ctx2);
853 remove(cltr->procs, *proc );
854 unlock(cltr->proc_list_lock);
[de94a60]855}
856
857//-----------------------------------------------------------------------------
858// Debug
859__cfaabi_dbg_debug_do(
[9181f1d]860 void __cfaabi_dbg_record(__spinlock_t & this, const char * prev_name) {
861 this.prev_name = prev_name;
[14a61b5]862 this.prev_thrd = kernelTLS.this_thread;
[9181f1d]863 }
[f7d6bb0]864)
[8118303]865// Local Variables: //
866// mode: c //
867// tab-width: 4 //
868// End: //
Note: See TracBrowser for help on using the repository browser.