source: libcfa/src/concurrency/kernel.cfa @ 2026bb6

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 2026bb6 was 2026bb6, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

More robust fix for optionally linking threads

  • Property mode set to 100644
File size: 26.4 KB
Line 
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
10// Created On       : Tue Jan 17 12:27:26 2017
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Thu Jun 20 17:21:23 2019
13// Update Count     : 25
14//
15
16#define __cforall_thread__
17
18//C Includes
19#include <stddef.h>
20#include <errno.h>
21#include <string.h>
22extern "C" {
23#include <stdio.h>
24#include <fenv.h>
25#include <sys/resource.h>
26#include <signal.h>
27#include <unistd.h>
28}
29
30//CFA Includes
31#include "time.hfa"
32#include "kernel_private.hfa"
33#include "preemption.hfa"
34#include "startup.hfa"
35
36//Private includes
37#define __CFA_INVOKE_PRIVATE__
38#include "invoke.h"
39
40//-----------------------------------------------------------------------------
41// Some assembly required
42#if   defined( __i386 )
43        #define CtxGet( ctx )        \
44                __asm__ volatile (     \
45                        "movl %%esp,%0\n"\
46                        "movl %%ebp,%1\n"\
47                        : "=rm" (ctx.SP),\
48                                "=rm" (ctx.FP) \
49                )
50
51        // mxcr : SSE Status and Control bits (control bits are preserved across function calls)
52        // fcw  : X87 FPU control word (preserved across function calls)
53        #define __x87_store         \
54                uint32_t __mxcr;      \
55                uint16_t __fcw;       \
56                __asm__ volatile (    \
57                        "stmxcsr %0\n"  \
58                        "fnstcw  %1\n"  \
59                        : "=m" (__mxcr),\
60                                "=m" (__fcw)  \
61                )
62
63        #define __x87_load         \
64                __asm__ volatile (   \
65                        "fldcw  %1\n"  \
66                        "ldmxcsr %0\n" \
67                        ::"m" (__mxcr),\
68                                "m" (__fcw)  \
69                )
70
71#elif defined( __x86_64 )
72        #define CtxGet( ctx )        \
73                __asm__ volatile (     \
74                        "movq %%rsp,%0\n"\
75                        "movq %%rbp,%1\n"\
76                        : "=rm" (ctx.SP),\
77                                "=rm" (ctx.FP) \
78                )
79
80        #define __x87_store         \
81                uint32_t __mxcr;      \
82                uint16_t __fcw;       \
83                __asm__ volatile (    \
84                        "stmxcsr %0\n"  \
85                        "fnstcw  %1\n"  \
86                        : "=m" (__mxcr),\
87                                "=m" (__fcw)  \
88                )
89
90        #define __x87_load          \
91                __asm__ volatile (    \
92                        "fldcw  %1\n"   \
93                        "ldmxcsr %0\n"  \
94                        :: "m" (__mxcr),\
95                                "m" (__fcw)  \
96                )
97
98
99#elif defined( __ARM_ARCH )
100#define CtxGet( ctx ) __asm__ ( \
101                "mov %0,%%sp\n"   \
102                "mov %1,%%r11\n"   \
103        : "=rm" (ctx.SP), "=rm" (ctx.FP) )
104#else
105        #error unknown hardware architecture
106#endif
107
108//-----------------------------------------------------------------------------
109//Start and stop routine for the kernel, declared first to make sure they run first
110static void kernel_startup(void)  __attribute__(( constructor( STARTUP_PRIORITY_KERNEL ) ));
111static void kernel_shutdown(void) __attribute__(( destructor ( STARTUP_PRIORITY_KERNEL ) ));
112
113//-----------------------------------------------------------------------------
114// Kernel storage
115KERNEL_STORAGE(cluster,         mainCluster);
116KERNEL_STORAGE(processor,       mainProcessor);
117KERNEL_STORAGE(thread_desc,     mainThread);
118KERNEL_STORAGE(__stack_t,       mainThreadCtx);
119
120cluster     * mainCluster;
121processor   * mainProcessor;
122thread_desc * mainThread;
123
124extern "C" {
125struct { __dllist_t(cluster) list; __spinlock_t lock; } __cfa_dbg_global_clusters;
126}
127
128size_t __page_size = 0;
129
130//-----------------------------------------------------------------------------
131// Global state
132thread_local struct KernelThreadData kernelTLS __attribute__ ((tls_model ( "initial-exec" ))) = {
133        NULL,
134        NULL,
135        { 1, false, false }
136};
137
138//-----------------------------------------------------------------------------
139// Struct to steal stack
140struct current_stack_info_t {
141        __stack_t * storage;            // pointer to stack object
142        void *base;                             // base of stack
143        void *limit;                    // stack grows towards stack limit
144        void *context;                  // address of cfa_context_t
145};
146
147void ?{}( current_stack_info_t & this ) {
148        __stack_context_t ctx;
149        CtxGet( ctx );
150        this.base = ctx.FP;
151
152        rlimit r;
153        getrlimit( RLIMIT_STACK, &r);
154        size_t size = r.rlim_cur;
155
156        this.limit = (void *)(((intptr_t)this.base) - size);
157        this.context = &storage_mainThreadCtx;
158}
159
160//-----------------------------------------------------------------------------
161// Main thread construction
162
163void ?{}( coroutine_desc & this, current_stack_info_t * info) with( this ) {
164        stack.storage = info->storage;
165        with(*stack.storage) {
166                limit     = info->limit;
167                base      = info->base;
168        }
169        __attribute__((may_alias)) intptr_t * istorage = (intptr_t*) &stack.storage;
170        *istorage |= 0x1;
171        name = "Main Thread";
172        state = Start;
173        starter = NULL;
174        last = NULL;
175        cancellation = NULL;
176}
177
178void ?{}( thread_desc & this, current_stack_info_t * info) with( this ) {
179        state = Start;
180        self_cor{ info };
181        curr_cor = &self_cor;
182        curr_cluster = mainCluster;
183        self_mon.owner = &this;
184        self_mon.recursion = 1;
185        self_mon_p = &self_mon;
186        next = NULL;
187
188        node.next = NULL;
189        node.prev = NULL;
190        doregister(curr_cluster, this);
191
192        monitors{ &self_mon_p, 1, (fptr_t)0 };
193}
194
195//-----------------------------------------------------------------------------
196// Processor coroutine
197void ?{}(processorCtx_t & this) {
198
199}
200
201// Construct the processor context of non-main processors
202static void ?{}(processorCtx_t & this, processor * proc, current_stack_info_t * info) {
203        (this.__cor){ info };
204        this.proc = proc;
205}
206
207static void start(processor * this);
208void ?{}(processor & this, const char * name, cluster & cltr) with( this ) {
209        this.name = name;
210        this.cltr = &cltr;
211        terminated{ 0 };
212        do_terminate = false;
213        preemption_alarm = NULL;
214        pending_preemption = false;
215        runner.proc = &this;
216
217        idleLock{};
218
219        start( &this );
220}
221
222void ^?{}(processor & this) with( this ){
223        if( ! __atomic_load_n(&do_terminate, __ATOMIC_ACQUIRE) ) {
224                __cfaabi_dbg_print_safe("Kernel : core %p signaling termination\n", &this);
225
226                __atomic_store_n(&do_terminate, true, __ATOMIC_RELAXED);
227                wake( &this );
228
229                P( terminated );
230                verify( kernelTLS.this_processor != &this);
231        }
232
233        pthread_join( kernel_thread, NULL );
234}
235
236void ?{}(cluster & this, const char * name, Duration preemption_rate) with( this ) {
237        this.name = name;
238        this.preemption_rate = preemption_rate;
239        ready_queue{};
240        ready_queue_lock{};
241
242        procs{ __get };
243        idles{ __get };
244        threads{ __get };
245
246        doregister(this);
247}
248
249void ^?{}(cluster & this) {
250        unregister(this);
251}
252
253//=============================================================================================
254// Kernel Scheduling logic
255//=============================================================================================
256static void runThread(processor * this, thread_desc * dst);
257static void finishRunning(processor * this);
258static void halt(processor * this);
259
260//Main of the processor contexts
261void main(processorCtx_t & runner) {
262        processor * this = runner.proc;
263        verify(this);
264
265        __cfaabi_dbg_print_safe("Kernel : core %p starting\n", this);
266
267        doregister(this->cltr, this);
268
269        {
270                // Setup preemption data
271                preemption_scope scope = { this };
272
273                __cfaabi_dbg_print_safe("Kernel : core %p started\n", this);
274
275                thread_desc * readyThread = NULL;
276                for( unsigned int spin_count = 0; ! __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST); spin_count++ )
277                {
278                        readyThread = nextThread( this->cltr );
279
280                        if(readyThread)
281                        {
282                                verify( ! kernelTLS.preemption_state.enabled );
283
284                                runThread(this, readyThread);
285
286                                verify( ! kernelTLS.preemption_state.enabled );
287
288                                //Some actions need to be taken from the kernel
289                                finishRunning(this);
290
291                                spin_count = 0;
292                        }
293                        else
294                        {
295                                // spin(this, &spin_count);
296                                halt(this);
297                        }
298                }
299
300                __cfaabi_dbg_print_safe("Kernel : core %p stopping\n", this);
301        }
302
303        unregister(this->cltr, this);
304
305        V( this->terminated );
306
307        __cfaabi_dbg_print_safe("Kernel : core %p terminated\n", this);
308}
309
310static int * __volatile_errno() __attribute__((noinline));
311static int * __volatile_errno() { asm(""); return &errno; }
312
313// KERNEL ONLY
314// runThread runs a thread by context switching
315// from the processor coroutine to the target thread
316static void runThread(processor * this, thread_desc * thrd_dst) {
317        coroutine_desc * proc_cor = get_coroutine(this->runner);
318
319        // Reset the terminating actions here
320        this->finish.action_code = No_Action;
321
322        // Update global state
323        kernelTLS.this_thread = thrd_dst;
324
325        // set state of processor coroutine to inactive and the thread to active
326        proc_cor->state = proc_cor->state == Halted ? Halted : Inactive;
327        thrd_dst->state = Active;
328
329        // set context switch to the thread that the processor is executing
330        verify( thrd_dst->context.SP );
331        CtxSwitch( &proc_cor->context, &thrd_dst->context );
332        // when CtxSwitch returns we are back in the processor coroutine
333
334        // set state of processor coroutine to active and the thread to inactive
335        thrd_dst->state = thrd_dst->state == Halted ? Halted : Inactive;
336        proc_cor->state = Active;
337}
338
339// KERNEL_ONLY
340static void returnToKernel() {
341        coroutine_desc * proc_cor = get_coroutine(kernelTLS.this_processor->runner);
342        thread_desc * thrd_src = kernelTLS.this_thread;
343
344        // set state of current coroutine to inactive
345        thrd_src->state = thrd_src->state == Halted ? Halted : Inactive;
346        proc_cor->state = Active;
347        int local_errno = *__volatile_errno();
348        #if defined( __i386 ) || defined( __x86_64 )
349                __x87_store;
350        #endif
351
352        // set new coroutine that the processor is executing
353        // and context switch to it
354        verify( proc_cor->context.SP );
355        CtxSwitch( &thrd_src->context, &proc_cor->context );
356
357        // set state of new coroutine to active
358        proc_cor->state = proc_cor->state == Halted ? Halted : Inactive;
359        thrd_src->state = Active;
360
361        #if defined( __i386 ) || defined( __x86_64 )
362                __x87_load;
363        #endif
364        *__volatile_errno() = local_errno;
365}
366
367// KERNEL_ONLY
368// Once a thread has finished running, some of
369// its final actions must be executed from the kernel
370static void finishRunning(processor * this) with( this->finish ) {
371        verify( ! kernelTLS.preemption_state.enabled );
372        choose( action_code ) {
373        case No_Action:
374                break;
375        case Release:
376                unlock( *lock );
377        case Schedule:
378                ScheduleThread( thrd );
379        case Release_Schedule:
380                unlock( *lock );
381                ScheduleThread( thrd );
382        case Release_Multi:
383                for(int i = 0; i < lock_count; i++) {
384                        unlock( *locks[i] );
385                }
386        case Release_Multi_Schedule:
387                for(int i = 0; i < lock_count; i++) {
388                        unlock( *locks[i] );
389                }
390                for(int i = 0; i < thrd_count; i++) {
391                        ScheduleThread( thrds[i] );
392                }
393        case Callback:
394                callback();
395        default:
396                abort("KERNEL ERROR: Unexpected action to run after thread");
397        }
398}
399
400// KERNEL_ONLY
401// Context invoker for processors
402// This is the entry point for processors (kernel threads)
403// It effectively constructs a coroutine by stealing the pthread stack
404static void * CtxInvokeProcessor(void * arg) {
405        processor * proc = (processor *) arg;
406        kernelTLS.this_processor = proc;
407        kernelTLS.this_thread    = NULL;
408        kernelTLS.preemption_state.[enabled, disable_count] = [false, 1];
409        // SKULLDUGGERY: We want to create a context for the processor coroutine
410        // which is needed for the 2-step context switch. However, there is no reason
411        // to waste the perfectly valid stack create by pthread.
412        current_stack_info_t info;
413        __stack_t ctx;
414        info.storage = &ctx;
415        (proc->runner){ proc, &info };
416
417        __cfaabi_dbg_print_safe("Coroutine : created stack %p\n", get_coroutine(proc->runner)->stack.storage);
418
419        //Set global state
420        kernelTLS.this_thread    = NULL;
421
422        //We now have a proper context from which to schedule threads
423        __cfaabi_dbg_print_safe("Kernel : core %p created (%p, %p)\n", proc, &proc->runner, &ctx);
424
425        // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't
426        // resume it to start it like it normally would, it will just context switch
427        // back to here. Instead directly call the main since we already are on the
428        // appropriate stack.
429        get_coroutine(proc->runner)->state = Active;
430        main( proc->runner );
431        get_coroutine(proc->runner)->state = Halted;
432
433        // Main routine of the core returned, the core is now fully terminated
434        __cfaabi_dbg_print_safe("Kernel : core %p main ended (%p)\n", proc, &proc->runner);
435
436        return NULL;
437}
438
439static void start(processor * this) {
440        __cfaabi_dbg_print_safe("Kernel : Starting core %p\n", this);
441
442        pthread_create( &this->kernel_thread, NULL, CtxInvokeProcessor, (void*)this );
443
444        __cfaabi_dbg_print_safe("Kernel : core %p started\n", this);
445}
446
447// KERNEL_ONLY
448void kernel_first_resume( processor * this ) {
449        thread_desc * src = mainThread;
450        coroutine_desc * dst = get_coroutine(this->runner);
451
452        verify( ! kernelTLS.preemption_state.enabled );
453
454        __stack_prepare( &dst->stack, 65000 );
455        CtxStart(&this->runner, CtxInvokeCoroutine);
456
457        verify( ! kernelTLS.preemption_state.enabled );
458
459        dst->last = &src->self_cor;
460        dst->starter = dst->starter ? dst->starter : &src->self_cor;
461
462        // set state of current coroutine to inactive
463        src->state = src->state == Halted ? Halted : Inactive;
464
465        // context switch to specified coroutine
466        verify( dst->context.SP );
467        CtxSwitch( &src->context, &dst->context );
468        // when CtxSwitch returns we are back in the src coroutine
469
470        // set state of new coroutine to active
471        src->state = Active;
472
473        verify( ! kernelTLS.preemption_state.enabled );
474}
475
476// KERNEL_ONLY
477void kernel_last_resume( processor * this ) {
478        coroutine_desc * src = &mainThread->self_cor;
479        coroutine_desc * dst = get_coroutine(this->runner);
480
481        verify( ! kernelTLS.preemption_state.enabled );
482        verify( dst->starter == src );
483        verify( dst->context.SP );
484
485        // context switch to the processor
486        CtxSwitch( &src->context, &dst->context );
487}
488
489//-----------------------------------------------------------------------------
490// Scheduler routines
491
492// KERNEL ONLY
493void ScheduleThread( thread_desc * thrd ) {
494        verify( thrd );
495        verify( thrd->state != Halted );
496
497        verify( ! kernelTLS.preemption_state.enabled );
498
499        verifyf( thrd->next == NULL, "Expected null got %p", thrd->next );
500
501        with( *thrd->curr_cluster ) {
502                lock  ( ready_queue_lock __cfaabi_dbg_ctx2 );
503                bool was_empty = !(ready_queue != 0);
504                append( ready_queue, thrd );
505                unlock( ready_queue_lock );
506
507                if(was_empty) {
508                        lock      (proc_list_lock __cfaabi_dbg_ctx2);
509                        if(idles) {
510                                wake_fast(idles.head);
511                        }
512                        unlock    (proc_list_lock);
513                }
514                else if( struct processor * idle = idles.head ) {
515                        wake_fast(idle);
516                }
517
518        }
519
520        verify( ! kernelTLS.preemption_state.enabled );
521}
522
523// KERNEL ONLY
524thread_desc * nextThread(cluster * this) with( *this ) {
525        verify( ! kernelTLS.preemption_state.enabled );
526        lock( ready_queue_lock __cfaabi_dbg_ctx2 );
527        thread_desc * head = pop_head( ready_queue );
528        unlock( ready_queue_lock );
529        verify( ! kernelTLS.preemption_state.enabled );
530        return head;
531}
532
533void BlockInternal() {
534        disable_interrupts();
535        verify( ! kernelTLS.preemption_state.enabled );
536        returnToKernel();
537        verify( ! kernelTLS.preemption_state.enabled );
538        enable_interrupts( __cfaabi_dbg_ctx );
539}
540
541void BlockInternal( __spinlock_t * lock ) {
542        disable_interrupts();
543        with( *kernelTLS.this_processor ) {
544                finish.action_code = Release;
545                finish.lock        = lock;
546        }
547
548        verify( ! kernelTLS.preemption_state.enabled );
549        returnToKernel();
550        verify( ! kernelTLS.preemption_state.enabled );
551
552        enable_interrupts( __cfaabi_dbg_ctx );
553}
554
555void BlockInternal( thread_desc * thrd ) {
556        disable_interrupts();
557        with( * kernelTLS.this_processor ) {
558                finish.action_code = Schedule;
559                finish.thrd        = thrd;
560        }
561
562        verify( ! kernelTLS.preemption_state.enabled );
563        returnToKernel();
564        verify( ! kernelTLS.preemption_state.enabled );
565
566        enable_interrupts( __cfaabi_dbg_ctx );
567}
568
569void BlockInternal( __spinlock_t * lock, thread_desc * thrd ) {
570        assert(thrd);
571        disable_interrupts();
572        with( * kernelTLS.this_processor ) {
573                finish.action_code = Release_Schedule;
574                finish.lock        = lock;
575                finish.thrd        = thrd;
576        }
577
578        verify( ! kernelTLS.preemption_state.enabled );
579        returnToKernel();
580        verify( ! kernelTLS.preemption_state.enabled );
581
582        enable_interrupts( __cfaabi_dbg_ctx );
583}
584
585void BlockInternal(__spinlock_t * locks [], unsigned short count) {
586        disable_interrupts();
587        with( * kernelTLS.this_processor ) {
588                finish.action_code = Release_Multi;
589                finish.locks       = locks;
590                finish.lock_count  = count;
591        }
592
593        verify( ! kernelTLS.preemption_state.enabled );
594        returnToKernel();
595        verify( ! kernelTLS.preemption_state.enabled );
596
597        enable_interrupts( __cfaabi_dbg_ctx );
598}
599
600void BlockInternal(__spinlock_t * locks [], unsigned short lock_count, thread_desc * thrds [], unsigned short thrd_count) {
601        disable_interrupts();
602        with( *kernelTLS.this_processor ) {
603                finish.action_code = Release_Multi_Schedule;
604                finish.locks       = locks;
605                finish.lock_count  = lock_count;
606                finish.thrds       = thrds;
607                finish.thrd_count  = thrd_count;
608        }
609
610        verify( ! kernelTLS.preemption_state.enabled );
611        returnToKernel();
612        verify( ! kernelTLS.preemption_state.enabled );
613
614        enable_interrupts( __cfaabi_dbg_ctx );
615}
616
617void BlockInternal(__finish_callback_fptr_t callback) {
618        disable_interrupts();
619        with( *kernelTLS.this_processor ) {
620                finish.action_code = Callback;
621                finish.callback    = callback;
622        }
623
624        verify( ! kernelTLS.preemption_state.enabled );
625        returnToKernel();
626        verify( ! kernelTLS.preemption_state.enabled );
627
628        enable_interrupts( __cfaabi_dbg_ctx );
629}
630
631// KERNEL ONLY
632void LeaveThread(__spinlock_t * lock, thread_desc * thrd) {
633        verify( ! kernelTLS.preemption_state.enabled );
634        with( * kernelTLS.this_processor ) {
635                finish.action_code = thrd ? Release_Schedule : Release;
636                finish.lock        = lock;
637                finish.thrd        = thrd;
638        }
639
640        returnToKernel();
641}
642
643//=============================================================================================
644// Kernel Setup logic
645//=============================================================================================
646//-----------------------------------------------------------------------------
647// Kernel boot procedures
648static void kernel_startup(void) {
649        verify( ! kernelTLS.preemption_state.enabled );
650        __cfaabi_dbg_print_safe("Kernel : Starting\n");
651
652        __page_size = sysconf( _SC_PAGESIZE );
653
654        __cfa_dbg_global_clusters.list{ __get };
655        __cfa_dbg_global_clusters.lock{};
656
657        // Initialize the main cluster
658        mainCluster = (cluster *)&storage_mainCluster;
659        (*mainCluster){"Main Cluster"};
660
661        __cfaabi_dbg_print_safe("Kernel : Main cluster ready\n");
662
663        // Start by initializing the main thread
664        // SKULLDUGGERY: the mainThread steals the process main thread
665        // which will then be scheduled by the mainProcessor normally
666        mainThread = (thread_desc *)&storage_mainThread;
667        current_stack_info_t info;
668        info.storage = (__stack_t*)&storage_mainThreadCtx;
669        (*mainThread){ &info };
670
671        __cfaabi_dbg_print_safe("Kernel : Main thread ready\n");
672
673
674
675        // Construct the processor context of the main processor
676        void ?{}(processorCtx_t & this, processor * proc) {
677                (this.__cor){ "Processor" };
678                this.__cor.starter = NULL;
679                this.proc = proc;
680        }
681
682        void ?{}(processor & this) with( this ) {
683                name = "Main Processor";
684                cltr = mainCluster;
685                terminated{ 0 };
686                do_terminate = false;
687                preemption_alarm = NULL;
688                pending_preemption = false;
689                kernel_thread = pthread_self();
690
691                runner{ &this };
692                __cfaabi_dbg_print_safe("Kernel : constructed main processor context %p\n", &runner);
693        }
694
695        // Initialize the main processor and the main processor ctx
696        // (the coroutine that contains the processing control flow)
697        mainProcessor = (processor *)&storage_mainProcessor;
698        (*mainProcessor){};
699
700        //initialize the global state variables
701        kernelTLS.this_processor = mainProcessor;
702        kernelTLS.this_thread    = mainThread;
703
704        // Enable preemption
705        kernel_start_preemption();
706
707        // Add the main thread to the ready queue
708        // once resume is called on mainProcessor->runner the mainThread needs to be scheduled like any normal thread
709        ScheduleThread(mainThread);
710
711        // SKULLDUGGERY: Force a context switch to the main processor to set the main thread's context to the current UNIX
712        // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that
713        // mainThread is on the ready queue when this call is made.
714        kernel_first_resume( kernelTLS.this_processor );
715
716
717
718        // THE SYSTEM IS NOW COMPLETELY RUNNING
719        __cfaabi_dbg_print_safe("Kernel : Started\n--------------------------------------------------\n\n");
720
721        verify( ! kernelTLS.preemption_state.enabled );
722        enable_interrupts( __cfaabi_dbg_ctx );
723        verify( TL_GET( preemption_state.enabled ) );
724}
725
726static void kernel_shutdown(void) {
727        __cfaabi_dbg_print_safe("\n--------------------------------------------------\nKernel : Shutting down\n");
728
729        verify( TL_GET( preemption_state.enabled ) );
730        disable_interrupts();
731        verify( ! kernelTLS.preemption_state.enabled );
732
733        // SKULLDUGGERY: Notify the mainProcessor it needs to terminates.
734        // When its coroutine terminates, it return control to the mainThread
735        // which is currently here
736        __atomic_store_n(&mainProcessor->do_terminate, true, __ATOMIC_RELEASE);
737        kernel_last_resume( kernelTLS.this_processor );
738        mainThread->self_cor.state = Halted;
739
740        // THE SYSTEM IS NOW COMPLETELY STOPPED
741
742        // Disable preemption
743        kernel_stop_preemption();
744
745        // Destroy the main processor and its context in reverse order of construction
746        // These were manually constructed so we need manually destroy them
747        ^(mainProcessor->runner){};
748        ^(mainProcessor){};
749
750        // Final step, destroy the main thread since it is no longer needed
751        // Since we provided a stack to this taxk it will not destroy anything
752        ^(mainThread){};
753
754        ^(__cfa_dbg_global_clusters.list){};
755        ^(__cfa_dbg_global_clusters.lock){};
756
757        __cfaabi_dbg_print_safe("Kernel : Shutdown complete\n");
758}
759
760//=============================================================================================
761// Kernel Quiescing
762//=============================================================================================
763static void halt(processor * this) with( *this ) {
764        // verify( ! __atomic_load_n(&do_terminate, __ATOMIC_SEQ_CST) );
765
766        with( *cltr ) {
767                lock      (proc_list_lock __cfaabi_dbg_ctx2);
768                remove    (procs, *this);
769                push_front(idles, *this);
770                unlock    (proc_list_lock);
771        }
772
773        __cfaabi_dbg_print_safe("Kernel : Processor %p ready to sleep\n", this);
774
775        wait( idleLock );
776
777        __cfaabi_dbg_print_safe("Kernel : Processor %p woke up and ready to run\n", this);
778
779        with( *cltr ) {
780                lock      (proc_list_lock __cfaabi_dbg_ctx2);
781                remove    (idles, *this);
782                push_front(procs, *this);
783                unlock    (proc_list_lock);
784        }
785}
786
787//=============================================================================================
788// Unexpected Terminating logic
789//=============================================================================================
790static __spinlock_t kernel_abort_lock;
791static bool kernel_abort_called = false;
792
793void * kernel_abort(void) __attribute__ ((__nothrow__)) {
794        // abort cannot be recursively entered by the same or different processors because all signal handlers return when
795        // the globalAbort flag is true.
796        lock( kernel_abort_lock __cfaabi_dbg_ctx2 );
797
798        // first task to abort ?
799        if ( kernel_abort_called ) {                    // not first task to abort ?
800                unlock( kernel_abort_lock );
801
802                sigset_t mask;
803                sigemptyset( &mask );
804                sigaddset( &mask, SIGALRM );            // block SIGALRM signals
805                sigsuspend( &mask );                    // block the processor to prevent further damage during abort
806                _exit( EXIT_FAILURE );                  // if processor unblocks before it is killed, terminate it
807        }
808        else {
809                kernel_abort_called = true;
810                unlock( kernel_abort_lock );
811        }
812
813        return kernelTLS.this_thread;
814}
815
816void kernel_abort_msg( void * kernel_data, char * abort_text, int abort_text_size ) {
817        thread_desc * thrd = kernel_data;
818
819        if(thrd) {
820                int len = snprintf( abort_text, abort_text_size, "Error occurred while executing thread %.256s (%p)", thrd->self_cor.name, thrd );
821                __cfaabi_dbg_bits_write( abort_text, len );
822
823                if ( &thrd->self_cor != thrd->curr_cor ) {
824                        len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", thrd->curr_cor->name, thrd->curr_cor );
825                        __cfaabi_dbg_bits_write( abort_text, len );
826                }
827                else {
828                        __cfaabi_dbg_bits_write( ".\n", 2 );
829                }
830        }
831        else {
832                int len = snprintf( abort_text, abort_text_size, "Error occurred outside of any thread.\n" );
833                __cfaabi_dbg_bits_write( abort_text, len );
834        }
835}
836
837int kernel_abort_lastframe( void ) __attribute__ ((__nothrow__)) {
838        return get_coroutine(kernelTLS.this_thread) == get_coroutine(mainThread) ? 4 : 2;
839}
840
841static __spinlock_t kernel_debug_lock;
842
843extern "C" {
844        void __cfaabi_dbg_bits_acquire() {
845                lock( kernel_debug_lock __cfaabi_dbg_ctx2 );
846        }
847
848        void __cfaabi_dbg_bits_release() {
849                unlock( kernel_debug_lock );
850        }
851}
852
853//=============================================================================================
854// Kernel Utilities
855//=============================================================================================
856//-----------------------------------------------------------------------------
857// Locks
858void  ?{}( semaphore & this, int count = 1 ) {
859        (this.lock){};
860        this.count = count;
861        (this.waiting){};
862}
863void ^?{}(semaphore & this) {}
864
865void P(semaphore & this) with( this ){
866        lock( lock __cfaabi_dbg_ctx2 );
867        count -= 1;
868        if ( count < 0 ) {
869                // queue current task
870                append( waiting, kernelTLS.this_thread );
871
872                // atomically release spin lock and block
873                BlockInternal( &lock );
874        }
875        else {
876            unlock( lock );
877        }
878}
879
880void V(semaphore & this) with( this ) {
881        thread_desc * thrd = NULL;
882        lock( lock __cfaabi_dbg_ctx2 );
883        count += 1;
884        if ( count <= 0 ) {
885                // remove task at head of waiting list
886                thrd = pop_head( waiting );
887        }
888
889        unlock( lock );
890
891        // make new owner
892        WakeThread( thrd );
893}
894
895//-----------------------------------------------------------------------------
896// Global Queues
897void doregister( cluster     & cltr ) {
898        lock      ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2);
899        push_front( __cfa_dbg_global_clusters.list, cltr );
900        unlock    ( __cfa_dbg_global_clusters.lock );
901}
902
903void unregister( cluster     & cltr ) {
904        lock  ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2);
905        remove( __cfa_dbg_global_clusters.list, cltr );
906        unlock( __cfa_dbg_global_clusters.lock );
907}
908
909void doregister( cluster * cltr, thread_desc & thrd ) {
910        lock      (cltr->thread_list_lock __cfaabi_dbg_ctx2);
911        cltr->nthreads += 1;
912        push_front(cltr->threads, thrd);
913        unlock    (cltr->thread_list_lock);
914}
915
916void unregister( cluster * cltr, thread_desc & thrd ) {
917        lock  (cltr->thread_list_lock __cfaabi_dbg_ctx2);
918        remove(cltr->threads, thrd );
919        cltr->nthreads -= 1;
920        unlock(cltr->thread_list_lock);
921}
922
923void doregister( cluster * cltr, processor * proc ) {
924        lock      (cltr->proc_list_lock __cfaabi_dbg_ctx2);
925        cltr->nprocessors += 1;
926        push_front(cltr->procs, *proc);
927        unlock    (cltr->proc_list_lock);
928}
929
930void unregister( cluster * cltr, processor * proc ) {
931        lock  (cltr->proc_list_lock __cfaabi_dbg_ctx2);
932        remove(cltr->procs, *proc );
933        cltr->nprocessors -= 1;
934        unlock(cltr->proc_list_lock);
935}
936
937//-----------------------------------------------------------------------------
938// Debug
939__cfaabi_dbg_debug_do(
940        extern "C" {
941                void __cfaabi_dbg_record(__spinlock_t & this, const char * prev_name) {
942                        this.prev_name = prev_name;
943                        this.prev_thrd = kernelTLS.this_thread;
944                }
945        }
946)
947
948//-----------------------------------------------------------------------------
949// Debug
950bool threading_enabled(void) {
951        return true;
952}
953// Local Variables: //
954// mode: c //
955// tab-width: 4 //
956// End: //
Note: See TracBrowser for help on using the repository browser.