source: src/libcfa/concurrency/kernel.c @ 7c03d6d

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 7c03d6d was f32e53e, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Converted CtxGet? from asm function to asm statement for more robustness, should fix full build

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