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

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 resolv-new with_gc
Last change on this file since e60e0dc was e60e0dc, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Some cleanu[ in the kernel, notably phasing out the system processor

  • Property mode set to 100644
File size: 19.6 KB
Line 
1// -*- Mode: CFA -*-
2//
3// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
4//
5// The contents of this file are covered under the licence agreement in the
6// file "LICENCE" distributed with Cforall.
7//
8// kernel.c --
9//
10// Author : Thierry Delisle
11// Created On : Tue Jan 17 12:27:26 2017
12// Last Modified By : Thierry Delisle
13// Last Modified On : --
14// Update Count : 0
15//
16
17#include "libhdr.h"
18
19//C Includes
20#include <stddef.h>
21extern "C" {
22#include <stdio.h>
23#include <fenv.h>
24#include <sys/resource.h>
25#include <signal.h>
26#include <unistd.h>
27}
28
29//CFA Includes
30#include "kernel_private.h"
31#include "preemption.h"
32#include "startup.h"
33
34//Private includes
35#define __CFA_INVOKE_PRIVATE__
36#include "invoke.h"
37
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
42//-----------------------------------------------------------------------------
43// Kernel storage
44#define KERNEL_STORAGE(T,X) static char X##Storage[sizeof(T)]
45
46KERNEL_STORAGE(processorCtx_t, systemProcessorCtx);
47KERNEL_STORAGE(cluster, systemCluster);
48KERNEL_STORAGE(system_proc_t, systemProcessor);
49KERNEL_STORAGE(event_kernel_t, event_kernel);
50KERNEL_STORAGE(thread_desc, mainThread);
51KERNEL_STORAGE(machine_context_t, mainThreadCtx);
52
53cluster * systemCluster;
54system_proc_t * systemProcessor;
55event_kernel_t * event_kernel;
56thread_desc * mainThread;
57
58//-----------------------------------------------------------------------------
59// Global state
60
61volatile thread_local processor * this_processor;
62volatile thread_local coroutine_desc * this_coroutine;
63volatile thread_local thread_desc * this_thread;
64volatile thread_local bool preemption_in_progress = 0;
65volatile thread_local unsigned short disable_preempt_count = 1;
66
67//-----------------------------------------------------------------------------
68// Main thread construction
69struct current_stack_info_t {
70 machine_context_t ctx;
71 unsigned int size; // size of stack
72 void *base; // base of stack
73 void *storage; // pointer to stack
74 void *limit; // stack grows towards stack limit
75 void *context; // address of cfa_context_t
76 void *top; // address of top of storage
77};
78
79void ?{}( current_stack_info_t * this ) {
80 CtxGet( this->ctx );
81 this->base = this->ctx.FP;
82 this->storage = this->ctx.SP;
83
84 rlimit r;
85 getrlimit( RLIMIT_STACK, &r);
86 this->size = r.rlim_cur;
87
88 this->limit = (void *)(((intptr_t)this->base) - this->size);
89 this->context = &mainThreadCtxStorage;
90 this->top = this->base;
91}
92
93void ?{}( coStack_t * this, current_stack_info_t * info) {
94 this->size = info->size;
95 this->storage = info->storage;
96 this->limit = info->limit;
97 this->base = info->base;
98 this->context = info->context;
99 this->top = info->top;
100 this->userStack = true;
101}
102
103void ?{}( coroutine_desc * this, current_stack_info_t * info) {
104 (&this->stack){ info };
105 this->name = "Main Thread";
106 this->errno_ = 0;
107 this->state = Start;
108}
109
110void ?{}( thread_desc * this, current_stack_info_t * info) {
111 (&this->cor){ info };
112}
113
114//-----------------------------------------------------------------------------
115// Processor coroutine
116void ?{}(processorCtx_t * this, processor * proc) {
117 (&this->__cor){ "Processor" };
118 this->proc = proc;
119 proc->runner = this;
120}
121
122void ?{}(processorCtx_t * this, processor * proc, current_stack_info_t * info) {
123 (&this->__cor){ info };
124 this->proc = proc;
125 proc->runner = this;
126}
127
128void ?{}(processor * this) {
129 this{ systemCluster };
130}
131
132void ?{}(processor * this, cluster * cltr) {
133 this->cltr = cltr;
134 (&this->terminated){ 0 };
135 this->do_terminate = false;
136 this->preemption_alarm = NULL;
137 this->pending_preemption = false;
138
139 start( this );
140}
141
142void ?{}(processor * this, cluster * cltr, processorCtx_t * runner) {
143 this->cltr = cltr;
144 (&this->terminated){ 0 };
145 this->do_terminate = false;
146 this->preemption_alarm = NULL;
147 this->pending_preemption = false;
148 this->kernel_thread = pthread_self();
149
150 this->runner = runner;
151 LIB_DEBUG_PRINT_SAFE("Kernel : constructing system processor context %p\n", runner);
152 runner{ this };
153}
154
155LIB_DEBUG_DO( bool validate( alarm_list_t * this ); )
156
157void ?{}(system_proc_t * this, cluster * cltr, processorCtx_t * runner) {
158 (&this->proc){ cltr, runner };
159}
160
161void ?{}(event_kernel_t * this) {
162 (&this->alarms){};
163 (&this->lock){};
164
165 verify( validate( &this->alarms ) );
166}
167
168void ^?{}(processor * this) {
169 if( ! this->do_terminate ) {
170 LIB_DEBUG_PRINT_SAFE("Kernel : core %p signaling termination\n", this);
171 this->do_terminate = true;
172 P( &this->terminated );
173 pthread_join( this->kernel_thread, NULL );
174 }
175}
176
177void ?{}(cluster * this) {
178 ( &this->ready_queue ){};
179 ( &this->ready_queue_lock ){};
180
181 this->preemption = default_preemption();
182}
183
184void ^?{}(cluster * this) {
185
186}
187
188//=============================================================================================
189// Kernel Scheduling logic
190//=============================================================================================
191//Main of the processor contexts
192void main(processorCtx_t * runner) {
193 processor * this = runner->proc;
194
195 LIB_DEBUG_PRINT_SAFE("Kernel : core %p starting\n", this);
196
197 {
198 // Setup preemption data
199 preemption_scope scope = { this };
200
201 LIB_DEBUG_PRINT_SAFE("Kernel : core %p started\n", this);
202
203 thread_desc * readyThread = NULL;
204 for( unsigned int spin_count = 0; ! this->do_terminate; spin_count++ )
205 {
206 readyThread = nextThread( this->cltr );
207
208 if(readyThread)
209 {
210 verify( disable_preempt_count > 0 );
211
212 runThread(this, readyThread);
213
214 verify( disable_preempt_count > 0 );
215
216 //Some actions need to be taken from the kernel
217 finishRunning(this);
218
219 spin_count = 0;
220 }
221 else
222 {
223 spin(this, &spin_count);
224 }
225 }
226
227 LIB_DEBUG_PRINT_SAFE("Kernel : core %p stopping\n", this);
228 }
229
230 V( &this->terminated );
231
232 LIB_DEBUG_PRINT_SAFE("Kernel : core %p terminated\n", this);
233}
234
235// runThread runs a thread by context switching
236// from the processor coroutine to the target thread
237void runThread(processor * this, thread_desc * dst) {
238 coroutine_desc * proc_cor = get_coroutine(this->runner);
239 coroutine_desc * thrd_cor = get_coroutine(dst);
240
241 //Reset the terminating actions here
242 this->finish.action_code = No_Action;
243
244 //Update global state
245 this_thread = dst;
246
247 // Context Switch to the thread
248 ThreadCtxSwitch(proc_cor, thrd_cor);
249 // when ThreadCtxSwitch returns we are back in the processor coroutine
250}
251
252// Once a thread has finished running, some of
253// its final actions must be executed from the kernel
254void finishRunning(processor * this) {
255 if( this->finish.action_code == Release ) {
256 unlock( this->finish.lock );
257 }
258 else if( this->finish.action_code == Schedule ) {
259 ScheduleThread( this->finish.thrd );
260 }
261 else if( this->finish.action_code == Release_Schedule ) {
262 unlock( this->finish.lock );
263 ScheduleThread( this->finish.thrd );
264 }
265 else if( this->finish.action_code == Release_Multi ) {
266 for(int i = 0; i < this->finish.lock_count; i++) {
267 unlock( this->finish.locks[i] );
268 }
269 }
270 else if( this->finish.action_code == Release_Multi_Schedule ) {
271 for(int i = 0; i < this->finish.lock_count; i++) {
272 unlock( this->finish.locks[i] );
273 }
274 for(int i = 0; i < this->finish.thrd_count; i++) {
275 ScheduleThread( this->finish.thrds[i] );
276 }
277 }
278 else {
279 assert(this->finish.action_code == No_Action);
280 }
281}
282
283// Handles spinning logic
284// TODO : find some strategy to put cores to sleep after some time
285void spin(processor * this, unsigned int * spin_count) {
286 (*spin_count)++;
287}
288
289// Context invoker for processors
290// This is the entry point for processors (kernel threads)
291// It effectively constructs a coroutine by stealing the pthread stack
292void * CtxInvokeProcessor(void * arg) {
293 processor * proc = (processor *) arg;
294 this_processor = proc;
295 this_coroutine = NULL;
296 this_thread = NULL;
297 disable_preempt_count = 1;
298 // SKULLDUGGERY: We want to create a context for the processor coroutine
299 // which is needed for the 2-step context switch. However, there is no reason
300 // to waste the perfectly valid stack create by pthread.
301 current_stack_info_t info;
302 machine_context_t ctx;
303 info.context = &ctx;
304 processorCtx_t proc_cor_storage = { proc, &info };
305
306 LIB_DEBUG_PRINT_SAFE("Coroutine : created stack %p\n", proc_cor_storage.__cor.stack.base);
307
308 //Set global state
309 this_coroutine = &proc->runner->__cor;
310 this_thread = NULL;
311
312 //We now have a proper context from which to schedule threads
313 LIB_DEBUG_PRINT_SAFE("Kernel : core %p created (%p, %p)\n", proc, proc->runner, &ctx);
314
315 // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't
316 // resume it to start it like it normally would, it will just context switch
317 // back to here. Instead directly call the main since we already are on the
318 // appropriate stack.
319 proc_cor_storage.__cor.state = Active;
320 main( &proc_cor_storage );
321 proc_cor_storage.__cor.state = Halted;
322
323 // Main routine of the core returned, the core is now fully terminated
324 LIB_DEBUG_PRINT_SAFE("Kernel : core %p main ended (%p)\n", proc, proc->runner);
325
326 return NULL;
327}
328
329void start(processor * this) {
330 LIB_DEBUG_PRINT_SAFE("Kernel : Starting core %p\n", this);
331
332 pthread_create( &this->kernel_thread, NULL, CtxInvokeProcessor, (void*)this );
333
334 LIB_DEBUG_PRINT_SAFE("Kernel : core %p started\n", this);
335}
336
337//-----------------------------------------------------------------------------
338// Scheduler routines
339void ScheduleThread( thread_desc * thrd ) {
340 // if( !thrd ) return;
341 assert( thrd );
342 assert( thrd->cor.state != Halted );
343
344 verify( disable_preempt_count > 0 );
345
346 verifyf( thrd->next == NULL, "Expected null got %p", thrd->next );
347
348 lock( &systemProcessor->proc.cltr->ready_queue_lock DEBUG_CTX2 );
349 append( &systemProcessor->proc.cltr->ready_queue, thrd );
350 unlock( &systemProcessor->proc.cltr->ready_queue_lock );
351
352 verify( disable_preempt_count > 0 );
353}
354
355thread_desc * nextThread(cluster * this) {
356 verify( disable_preempt_count > 0 );
357 lock( &this->ready_queue_lock DEBUG_CTX2 );
358 thread_desc * head = pop_head( &this->ready_queue );
359 unlock( &this->ready_queue_lock );
360 verify( disable_preempt_count > 0 );
361 return head;
362}
363
364void BlockInternal() {
365 disable_interrupts();
366 verify( disable_preempt_count > 0 );
367 suspend();
368 verify( disable_preempt_count > 0 );
369 enable_interrupts( DEBUG_CTX );
370}
371
372void BlockInternal( spinlock * lock ) {
373 disable_interrupts();
374 this_processor->finish.action_code = Release;
375 this_processor->finish.lock = lock;
376
377 verify( disable_preempt_count > 0 );
378 suspend();
379 verify( disable_preempt_count > 0 );
380
381 enable_interrupts( DEBUG_CTX );
382}
383
384void BlockInternal( thread_desc * thrd ) {
385 disable_interrupts();
386 assert( thrd->cor.state != Halted );
387 this_processor->finish.action_code = Schedule;
388 this_processor->finish.thrd = thrd;
389
390 verify( disable_preempt_count > 0 );
391 suspend();
392 verify( disable_preempt_count > 0 );
393
394 enable_interrupts( DEBUG_CTX );
395}
396
397void BlockInternal( spinlock * lock, thread_desc * thrd ) {
398 disable_interrupts();
399 this_processor->finish.action_code = Release_Schedule;
400 this_processor->finish.lock = lock;
401 this_processor->finish.thrd = thrd;
402
403 verify( disable_preempt_count > 0 );
404 suspend();
405 verify( disable_preempt_count > 0 );
406
407 enable_interrupts( DEBUG_CTX );
408}
409
410void BlockInternal(spinlock ** locks, unsigned short count) {
411 disable_interrupts();
412 this_processor->finish.action_code = Release_Multi;
413 this_processor->finish.locks = locks;
414 this_processor->finish.lock_count = count;
415
416 verify( disable_preempt_count > 0 );
417 suspend();
418 verify( disable_preempt_count > 0 );
419
420 enable_interrupts( DEBUG_CTX );
421}
422
423void BlockInternal(spinlock ** locks, unsigned short lock_count, thread_desc ** thrds, unsigned short thrd_count) {
424 disable_interrupts();
425 this_processor->finish.action_code = Release_Multi_Schedule;
426 this_processor->finish.locks = locks;
427 this_processor->finish.lock_count = lock_count;
428 this_processor->finish.thrds = thrds;
429 this_processor->finish.thrd_count = thrd_count;
430
431 verify( disable_preempt_count > 0 );
432 suspend();
433 verify( disable_preempt_count > 0 );
434
435 enable_interrupts( DEBUG_CTX );
436}
437
438void LeaveThread(spinlock * lock, thread_desc * thrd) {
439 verify( disable_preempt_count > 0 );
440 this_processor->finish.action_code = thrd ? Release_Schedule : Release;
441 this_processor->finish.lock = lock;
442 this_processor->finish.thrd = thrd;
443
444 suspend();
445}
446
447//=============================================================================================
448// Kernel Setup logic
449//=============================================================================================
450//-----------------------------------------------------------------------------
451// Kernel boot procedures
452void kernel_startup(void) {
453 LIB_DEBUG_PRINT_SAFE("Kernel : Starting\n");
454
455 // Start by initializing the main thread
456 // SKULLDUGGERY: the mainThread steals the process main thread
457 // which will then be scheduled by the systemProcessor normally
458 mainThread = (thread_desc *)&mainThreadStorage;
459 current_stack_info_t info;
460 mainThread{ &info };
461
462 LIB_DEBUG_PRINT_SAFE("Kernel : Main thread ready\n");
463
464 // Initialize the system cluster
465 systemCluster = (cluster *)&systemClusterStorage;
466 systemCluster{};
467
468 LIB_DEBUG_PRINT_SAFE("Kernel : System cluster ready\n");
469
470 // Initialize the system processor and the system processor ctx
471 // (the coroutine that contains the processing control flow)
472 systemProcessor = (system_proc_t *)&systemProcessorStorage;
473 systemProcessor{ systemCluster, (processorCtx_t *)&systemProcessorCtxStorage };
474
475 // Initialize the event kernel
476 event_kernel = (event_kernel_t *)&event_kernelStorage;
477 event_kernel{};
478
479 // Add the main thread to the ready queue
480 // once resume is called on systemProcessor->runner the mainThread needs to be scheduled like any normal thread
481 ScheduleThread(mainThread);
482
483 //initialize the global state variables
484 this_processor = &systemProcessor->proc;
485 this_thread = mainThread;
486 this_coroutine = &mainThread->cor;
487 disable_preempt_count = 1;
488
489 // Enable preemption
490 kernel_start_preemption();
491
492 // SKULLDUGGERY: Force a context switch to the system processor to set the main thread's context to the current UNIX
493 // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that
494 // mainThread is on the ready queue when this call is made.
495 resume( systemProcessor->proc.runner );
496
497
498
499 // THE SYSTEM IS NOW COMPLETELY RUNNING
500 LIB_DEBUG_PRINT_SAFE("Kernel : Started\n--------------------------------------------------\n\n");
501
502 enable_interrupts( DEBUG_CTX );
503}
504
505void kernel_shutdown(void) {
506 LIB_DEBUG_PRINT_SAFE("\n--------------------------------------------------\nKernel : Shutting down\n");
507
508 disable_interrupts();
509
510 // SKULLDUGGERY: Notify the systemProcessor it needs to terminates.
511 // When its coroutine terminates, it return control to the mainThread
512 // which is currently here
513 systemProcessor->proc.do_terminate = true;
514 suspend();
515
516 // THE SYSTEM IS NOW COMPLETELY STOPPED
517
518 // Disable preemption
519 kernel_stop_preemption();
520
521 // Destroy the system processor and its context in reverse order of construction
522 // These were manually constructed so we need manually destroy them
523 ^(systemProcessor->proc.runner){};
524 ^(systemProcessor){};
525
526 // Final step, destroy the main thread since it is no longer needed
527 // Since we provided a stack to this taxk it will not destroy anything
528 ^(mainThread){};
529
530 LIB_DEBUG_PRINT_SAFE("Kernel : Shutdown complete\n");
531}
532
533static spinlock kernel_abort_lock;
534static spinlock kernel_debug_lock;
535static bool kernel_abort_called = false;
536
537void * kernel_abort (void) __attribute__ ((__nothrow__)) {
538 // abort cannot be recursively entered by the same or different processors because all signal handlers return when
539 // the globalAbort flag is true.
540 lock( &kernel_abort_lock DEBUG_CTX2 );
541
542 // first task to abort ?
543 if ( !kernel_abort_called ) { // not first task to abort ?
544 kernel_abort_called = true;
545 unlock( &kernel_abort_lock );
546 }
547 else {
548 unlock( &kernel_abort_lock );
549
550 sigset_t mask;
551 sigemptyset( &mask );
552 sigaddset( &mask, SIGALRM ); // block SIGALRM signals
553 sigaddset( &mask, SIGUSR1 ); // block SIGUSR1 signals
554 sigsuspend( &mask ); // block the processor to prevent further damage during abort
555 _exit( EXIT_FAILURE ); // if processor unblocks before it is killed, terminate it
556 }
557
558 return this_thread;
559}
560
561void kernel_abort_msg( void * kernel_data, char * abort_text, int abort_text_size ) {
562 thread_desc * thrd = kernel_data;
563
564 int len = snprintf( abort_text, abort_text_size, "Error occurred while executing task %.256s (%p)", thrd->cor.name, thrd );
565 __lib_debug_write( STDERR_FILENO, abort_text, len );
566
567 if ( thrd != this_coroutine ) {
568 len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", this_coroutine->name, this_coroutine );
569 __lib_debug_write( STDERR_FILENO, abort_text, len );
570 }
571 else {
572 __lib_debug_write( STDERR_FILENO, ".\n", 2 );
573 }
574}
575
576extern "C" {
577 void __lib_debug_acquire() {
578 lock( &kernel_debug_lock DEBUG_CTX2 );
579 }
580
581 void __lib_debug_release() {
582 unlock( &kernel_debug_lock );
583 }
584}
585
586//=============================================================================================
587// Kernel Utilities
588//=============================================================================================
589//-----------------------------------------------------------------------------
590// Locks
591void ?{}( spinlock * this ) {
592 this->lock = 0;
593}
594void ^?{}( spinlock * this ) {
595
596}
597
598bool try_lock( spinlock * this DEBUG_CTX_PARAM2 ) {
599 return this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0;
600}
601
602void lock( spinlock * this DEBUG_CTX_PARAM2 ) {
603 for ( unsigned int i = 1;; i += 1 ) {
604 if ( this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0 ) { break; }
605 }
606 LIB_DEBUG_DO(
607 this->prev_name = caller;
608 this->prev_thrd = this_thread;
609 )
610}
611
612void lock_yield( spinlock * this DEBUG_CTX_PARAM2 ) {
613 for ( unsigned int i = 1;; i += 1 ) {
614 if ( this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0 ) { break; }
615 yield();
616 }
617 LIB_DEBUG_DO(
618 this->prev_name = caller;
619 this->prev_thrd = this_thread;
620 )
621}
622
623
624void unlock( spinlock * this ) {
625 __sync_lock_release_4( &this->lock );
626}
627
628void ?{}( semaphore * this, int count = 1 ) {
629 (&this->lock){};
630 this->count = count;
631 (&this->waiting){};
632}
633void ^?{}(semaphore * this) {}
634
635void P(semaphore * this) {
636 lock( &this->lock DEBUG_CTX2 );
637 this->count -= 1;
638 if ( this->count < 0 ) {
639 // queue current task
640 append( &this->waiting, (thread_desc *)this_thread );
641
642 // atomically release spin lock and block
643 BlockInternal( &this->lock );
644 }
645 else {
646 unlock( &this->lock );
647 }
648}
649
650void V(semaphore * this) {
651 thread_desc * thrd = NULL;
652 lock( &this->lock DEBUG_CTX2 );
653 this->count += 1;
654 if ( this->count <= 0 ) {
655 // remove task at head of waiting list
656 thrd = pop_head( &this->waiting );
657 }
658
659 unlock( &this->lock );
660
661 // make new owner
662 WakeThread( thrd );
663}
664
665//-----------------------------------------------------------------------------
666// Queues
667void ?{}( __thread_queue_t * this ) {
668 this->head = NULL;
669 this->tail = &this->head;
670}
671
672void append( __thread_queue_t * this, thread_desc * t ) {
673 verify(this->tail != NULL);
674 *this->tail = t;
675 this->tail = &t->next;
676}
677
678thread_desc * pop_head( __thread_queue_t * this ) {
679 thread_desc * head = this->head;
680 if( head ) {
681 this->head = head->next;
682 if( !head->next ) {
683 this->tail = &this->head;
684 }
685 head->next = NULL;
686 }
687 return head;
688}
689
690void ?{}( __condition_stack_t * this ) {
691 this->top = NULL;
692}
693
694void push( __condition_stack_t * this, __condition_criterion_t * t ) {
695 verify( !t->next );
696 t->next = this->top;
697 this->top = t;
698}
699
700__condition_criterion_t * pop( __condition_stack_t * this ) {
701 __condition_criterion_t * top = this->top;
702 if( top ) {
703 this->top = top->next;
704 top->next = NULL;
705 }
706 return top;
707}
708// Local Variables: //
709// mode: c //
710// tab-width: 4 //
711// End: //
Note: See TracBrowser for help on using the repository browser.