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

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

First draft at locks in Cforall

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