source: libcfa/src/concurrency/kernel.cfa @ 038be32

arm-ehenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 038be32 was 038be32, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

added defines and bool for whether or not to print statistics

  • Property mode set to 100644
File size: 34.0 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 : Tue Feb  4 13:03:15 2020
13// Update Count     : 58
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#include <limits.h>                                                                             // PTHREAD_STACK_MIN
29#include <sys/mman.h>                                                                   // mprotect
30}
31
32//CFA Includes
33#include "time.hfa"
34#include "kernel_private.hfa"
35#include "preemption.hfa"
36#include "startup.hfa"
37
38//Private includes
39#define __CFA_INVOKE_PRIVATE__
40#include "invoke.h"
41
42//-----------------------------------------------------------------------------
43// Some assembly required
44#if defined( __i386 )
45        #define CtxGet( ctx )        \
46                __asm__ volatile (     \
47                        "movl %%esp,%0\n"\
48                        "movl %%ebp,%1\n"\
49                        : "=rm" (ctx.SP),\
50                                "=rm" (ctx.FP) \
51                )
52
53        // mxcr : SSE Status and Control bits (control bits are preserved across function calls)
54        // fcw  : X87 FPU control word (preserved across function calls)
55        #define __x87_store         \
56                uint32_t __mxcr;      \
57                uint16_t __fcw;       \
58                __asm__ volatile (    \
59                        "stmxcsr %0\n"  \
60                        "fnstcw  %1\n"  \
61                        : "=m" (__mxcr),\
62                                "=m" (__fcw)  \
63                )
64
65        #define __x87_load         \
66                __asm__ volatile (   \
67                        "fldcw  %1\n"  \
68                        "ldmxcsr %0\n" \
69                        ::"m" (__mxcr),\
70                                "m" (__fcw)  \
71                )
72
73#elif defined( __x86_64 )
74        #define CtxGet( ctx )        \
75                __asm__ volatile (     \
76                        "movq %%rsp,%0\n"\
77                        "movq %%rbp,%1\n"\
78                        : "=rm" (ctx.SP),\
79                                "=rm" (ctx.FP) \
80                )
81
82        #define __x87_store         \
83                uint32_t __mxcr;      \
84                uint16_t __fcw;       \
85                __asm__ volatile (    \
86                        "stmxcsr %0\n"  \
87                        "fnstcw  %1\n"  \
88                        : "=m" (__mxcr),\
89                                "=m" (__fcw)  \
90                )
91
92        #define __x87_load          \
93                __asm__ volatile (    \
94                        "fldcw  %1\n"   \
95                        "ldmxcsr %0\n"  \
96                        :: "m" (__mxcr),\
97                                "m" (__fcw)  \
98                )
99
100
101#elif defined( __ARM_ARCH )
102#define CtxGet( ctx ) __asm__ ( \
103                "mov %0,%%sp\n"   \
104                "mov %1,%%r11\n"   \
105        : "=rm" (ctx.SP), "=rm" (ctx.FP) )
106#else
107        #error unknown hardware architecture
108#endif
109
110//-----------------------------------------------------------------------------
111//Start and stop routine for the kernel, declared first to make sure they run first
112static void __kernel_startup (void) __attribute__(( constructor( STARTUP_PRIORITY_KERNEL ) ));
113static void __kernel_shutdown(void) __attribute__(( destructor ( STARTUP_PRIORITY_KERNEL ) ));
114
115//-----------------------------------------------------------------------------
116// Kernel Scheduling logic
117static $thread * __next_thread(cluster * this);
118static void __run_thread(processor * this, $thread * dst);
119static $thread * __halt(processor * this);
120static bool __wake_one(cluster * cltr, bool was_empty);
121static bool __wake_proc(processor *);
122
123//-----------------------------------------------------------------------------
124// Kernel storage
125KERNEL_STORAGE(cluster,         mainCluster);
126KERNEL_STORAGE(processor,       mainProcessor);
127KERNEL_STORAGE($thread, mainThread);
128KERNEL_STORAGE(__stack_t,       mainThreadCtx);
129
130cluster     * mainCluster;
131processor   * mainProcessor;
132$thread * mainThread;
133
134extern "C" {
135        struct { __dllist_t(cluster) list; __spinlock_t lock; } __cfa_dbg_global_clusters;
136}
137
138size_t __page_size = 0;
139
140//-----------------------------------------------------------------------------
141// Global state
142thread_local struct KernelThreadData kernelTLS __attribute__ ((tls_model ( "initial-exec" ))) = {
143        NULL,                                                                                           // cannot use 0p
144        NULL,
145        { 1, false, false },
146        6u //this should be seeded better but due to a bug calling rdtsc doesn't work
147};
148
149//-----------------------------------------------------------------------------
150// Struct to steal stack
151struct current_stack_info_t {
152        __stack_t * storage;                                                            // pointer to stack object
153        void * base;                                                                            // base of stack
154        void * limit;                                                                           // stack grows towards stack limit
155        void * context;                                                                         // address of cfa_context_t
156};
157
158void ?{}( current_stack_info_t & this ) {
159        __stack_context_t ctx;
160        CtxGet( ctx );
161        this.base = ctx.FP;
162
163        rlimit r;
164        getrlimit( RLIMIT_STACK, &r);
165        size_t size = r.rlim_cur;
166
167        this.limit = (void *)(((intptr_t)this.base) - size);
168        this.context = &storage_mainThreadCtx;
169}
170
171//-----------------------------------------------------------------------------
172// Main thread construction
173
174void ?{}( $coroutine & this, current_stack_info_t * info) with( this ) {
175        stack.storage = info->storage;
176        with(*stack.storage) {
177                limit     = info->limit;
178                base      = info->base;
179        }
180        __attribute__((may_alias)) intptr_t * istorage = (intptr_t*) &stack.storage;
181        *istorage |= 0x1;
182        name = "Main Thread";
183        state = Start;
184        starter = 0p;
185        last = 0p;
186        cancellation = 0p;
187}
188
189void ?{}( $thread & this, current_stack_info_t * info) with( this ) {
190        state = Start;
191        self_cor{ info };
192        curr_cor = &self_cor;
193        curr_cluster = mainCluster;
194        self_mon.owner = &this;
195        self_mon.recursion = 1;
196        self_mon_p = &self_mon;
197        next = 0p;
198
199        node.next = 0p;
200        node.prev = 0p;
201        doregister(curr_cluster, this);
202
203        monitors{ &self_mon_p, 1, (fptr_t)0 };
204}
205
206//-----------------------------------------------------------------------------
207// Processor coroutine
208void ?{}(processorCtx_t & this) {
209
210}
211
212// Construct the processor context of non-main processors
213static void ?{}(processorCtx_t & this, processor * proc, current_stack_info_t * info) {
214        (this.__cor){ info };
215        this.proc = proc;
216}
217
218static void * __invoke_processor(void * arg);
219
220void ?{}(processor & this, const char name[], cluster & cltr) with( this ) {
221        this.name = name;
222        this.cltr = &cltr;
223        terminated{ 0 };
224        destroyer = 0p;
225        do_terminate = false;
226        preemption_alarm = 0p;
227        pending_preemption = false;
228        runner.proc = &this;
229
230        idle{};
231
232        __cfaabi_dbg_print_safe("Kernel : Starting core %p\n", &this);
233
234        this.stack = __create_pthread( &this.kernel_thread, __invoke_processor, (void *)&this );
235
236        __cfaabi_dbg_print_safe("Kernel : core %p started\n", &this);
237}
238
239void ^?{}(processor & this) with( this ){
240        if( ! __atomic_load_n(&do_terminate, __ATOMIC_ACQUIRE) ) {
241                __cfaabi_dbg_print_safe("Kernel : core %p signaling termination\n", &this);
242
243                __atomic_store_n(&do_terminate, true, __ATOMIC_RELAXED);
244                __wake_proc( &this );
245
246                P( terminated );
247                verify( kernelTLS.this_processor != &this);
248        }
249
250        pthread_join( kernel_thread, 0p );
251        free( this.stack );
252}
253
254void ?{}(cluster & this, const char name[], Duration preemption_rate) with( this ) {
255        this.name = name;
256        this.preemption_rate = preemption_rate;
257        ready_queue{};
258        ready_queue_lock{};
259
260        #if !defined(__CFA_NO_STATISTICS__)
261                print_stats = false;
262        #endif
263
264        procs{ __get };
265        idles{ __get };
266        threads{ __get };
267
268        __kernel_io_startup( this );
269
270        doregister(this);
271}
272
273void ^?{}(cluster & this) {
274        __kernel_io_shutdown( this );
275
276        unregister(this);
277}
278
279//=============================================================================================
280// Kernel Scheduling logic
281//=============================================================================================
282//Main of the processor contexts
283void main(processorCtx_t & runner) {
284        // Because of a bug, we couldn't initialized the seed on construction
285        // Do it here
286        kernelTLS.rand_seed ^= rdtscl();
287
288        processor * this = runner.proc;
289        verify(this);
290
291        __cfaabi_dbg_print_safe("Kernel : core %p starting\n", this);
292
293        doregister(this->cltr, this);
294
295        {
296                // Setup preemption data
297                preemption_scope scope = { this };
298
299                __cfaabi_dbg_print_safe("Kernel : core %p started\n", this);
300
301                $thread * readyThread = 0p;
302                for( unsigned int spin_count = 0; ! __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST); spin_count++ ) {
303                        // Try to get the next thread
304                        readyThread = __next_thread( this->cltr );
305
306                        // If no ready thread
307                        if( readyThread == 0p ) {
308                                // Block until a thread is ready
309                                readyThread = __halt(this);
310                        }
311
312                        // Check if we actually found a thread
313                        if( readyThread ) {
314                                /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
315                                /* paranoid */ verifyf( readyThread->state == Ready || readyThread->preempted != __NO_PREEMPTION, "state : %d, preempted %d\n", readyThread->state, readyThread->preempted);
316                                /* paranoid */ verifyf( readyThread->next == 0p, "Expected null got %p", readyThread->next );
317
318                                // We found a thread run it
319                                __run_thread(this, readyThread);
320
321                                /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
322                        }
323                }
324
325                __cfaabi_dbg_print_safe("Kernel : core %p stopping\n", this);
326        }
327
328        unregister(this->cltr, this);
329
330        V( this->terminated );
331
332        __cfaabi_dbg_print_safe("Kernel : core %p terminated\n", this);
333
334        // HACK : the coroutine context switch expects this_thread to be set
335        // and it make sense for it to be set in all other cases except here
336        // fake it
337        if( this == mainProcessor ) kernelTLS.this_thread = mainThread;
338}
339
340static int * __volatile_errno() __attribute__((noinline));
341static int * __volatile_errno() { asm(""); return &errno; }
342
343// KERNEL ONLY
344// runThread runs a thread by context switching
345// from the processor coroutine to the target thread
346static void __run_thread(processor * this, $thread * thrd_dst) {
347        $coroutine * proc_cor = get_coroutine(this->runner);
348
349        // Update global state
350        kernelTLS.this_thread = thrd_dst;
351
352        // set state of processor coroutine to inactive
353        verify(proc_cor->state == Active);
354        proc_cor->state = Blocked;
355
356        // Actually run the thread
357        RUNNING:  while(true) {
358                if(unlikely(thrd_dst->preempted)) {
359                        thrd_dst->preempted = __NO_PREEMPTION;
360                        verify(thrd_dst->state == Active  || thrd_dst->state == Rerun);
361                } else {
362                        verify(thrd_dst->state == Blocked || thrd_dst->state == Ready); // Ready means scheduled normally, blocked means rerun
363                        thrd_dst->state = Active;
364                }
365
366                __cfaabi_dbg_debug_do(
367                        thrd_dst->park_stale   = true;
368                        thrd_dst->unpark_stale = true;
369                )
370
371                /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
372                /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) < ((uintptr_t)__get_stack(thrd_dst->curr_cor)->base ) || thrd_dst->curr_cor == proc_cor, "ERROR : Destination $thread %p has been corrupted.\n StackPointer too small.\n", thrd_dst ); // add escape condition if we are setting up the processor
373                /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) > ((uintptr_t)__get_stack(thrd_dst->curr_cor)->limit) || thrd_dst->curr_cor == proc_cor, "ERROR : Destination $thread %p has been corrupted.\n StackPointer too large.\n", thrd_dst ); // add escape condition if we are setting up the processor
374
375                // set context switch to the thread that the processor is executing
376                verify( thrd_dst->context.SP );
377                __cfactx_switch( &proc_cor->context, &thrd_dst->context );
378                // when __cfactx_switch returns we are back in the processor coroutine
379
380                /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) > ((uintptr_t)__get_stack(thrd_dst->curr_cor)->limit), "ERROR : Destination $thread %p has been corrupted.\n StackPointer too large.\n", thrd_dst );
381                /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) < ((uintptr_t)__get_stack(thrd_dst->curr_cor)->base ), "ERROR : Destination $thread %p has been corrupted.\n StackPointer too small.\n", thrd_dst );
382                /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
383
384
385                // We just finished running a thread, there are a few things that could have happened.
386                // 1 - Regular case : the thread has blocked and now one has scheduled it yet.
387                // 2 - Racy case    : the thread has blocked but someone has already tried to schedule it.
388                // 4 - Preempted
389                // In case 1, we may have won a race so we can't write to the state again.
390                // In case 2, we lost the race so we now own the thread.
391
392                if(unlikely(thrd_dst->preempted != __NO_PREEMPTION)) {
393                        // The thread was preempted, reschedule it and reset the flag
394                        __schedule_thread( thrd_dst );
395                        break RUNNING;
396                }
397
398                // set state of processor coroutine to active and the thread to inactive
399                static_assert(sizeof(thrd_dst->state) == sizeof(int));
400                enum coroutine_state old_state = __atomic_exchange_n(&thrd_dst->state, Blocked, __ATOMIC_SEQ_CST);
401                __cfaabi_dbg_debug_do( thrd_dst->park_result = old_state; )
402                switch(old_state) {
403                        case Halted:
404                                // The thread has halted, it should never be scheduled/run again, leave it back to Halted and move on
405                                thrd_dst->state = Halted;
406
407                                // We may need to wake someone up here since
408                                unpark( this->destroyer __cfaabi_dbg_ctx2 );
409                                this->destroyer = 0p;
410                                break RUNNING;
411                        case Active:
412                                // This is case 1, the regular case, nothing more is needed
413                                break RUNNING;
414                        case Rerun:
415                                // This is case 2, the racy case, someone tried to run this thread before it finished blocking
416                                // In this case, just run it again.
417                                continue RUNNING;
418                        default:
419                                // This makes no sense, something is wrong abort
420                                abort("Finished running a thread that was Blocked/Start/Primed %d\n", old_state);
421                }
422        }
423
424        // Just before returning to the processor, set the processor coroutine to active
425        proc_cor->state = Active;
426        kernelTLS.this_thread = 0p;
427}
428
429// KERNEL_ONLY
430void returnToKernel() {
431        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
432        $coroutine * proc_cor = get_coroutine(kernelTLS.this_processor->runner);
433        $thread * thrd_src = kernelTLS.this_thread;
434
435        // Run the thread on this processor
436        {
437                int local_errno = *__volatile_errno();
438                #if defined( __i386 ) || defined( __x86_64 )
439                        __x87_store;
440                #endif
441                verify( proc_cor->context.SP );
442                __cfactx_switch( &thrd_src->context, &proc_cor->context );
443                #if defined( __i386 ) || defined( __x86_64 )
444                        __x87_load;
445                #endif
446                *__volatile_errno() = local_errno;
447        }
448
449        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
450        /* paranoid */ verifyf( ((uintptr_t)thrd_src->context.SP) < ((uintptr_t)__get_stack(thrd_src->curr_cor)->base ), "ERROR : Returning $thread %p has been corrupted.\n StackPointer too small.\n", thrd_src );
451        /* paranoid */ verifyf( ((uintptr_t)thrd_src->context.SP) > ((uintptr_t)__get_stack(thrd_src->curr_cor)->limit), "ERROR : Returning $thread %p has been corrupted.\n StackPointer too large.\n", thrd_src );
452}
453
454// KERNEL_ONLY
455// Context invoker for processors
456// This is the entry point for processors (kernel threads)
457// It effectively constructs a coroutine by stealing the pthread stack
458static void * __invoke_processor(void * arg) {
459        processor * proc = (processor *) arg;
460        kernelTLS.this_processor = proc;
461        kernelTLS.this_thread    = 0p;
462        kernelTLS.preemption_state.[enabled, disable_count] = [false, 1];
463        // SKULLDUGGERY: We want to create a context for the processor coroutine
464        // which is needed for the 2-step context switch. However, there is no reason
465        // to waste the perfectly valid stack create by pthread.
466        current_stack_info_t info;
467        __stack_t ctx;
468        info.storage = &ctx;
469        (proc->runner){ proc, &info };
470
471        __cfaabi_dbg_print_safe("Coroutine : created stack %p\n", get_coroutine(proc->runner)->stack.storage);
472
473        //Set global state
474        kernelTLS.this_thread = 0p;
475
476        //We now have a proper context from which to schedule threads
477        __cfaabi_dbg_print_safe("Kernel : core %p created (%p, %p)\n", proc, &proc->runner, &ctx);
478
479        // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't
480        // resume it to start it like it normally would, it will just context switch
481        // back to here. Instead directly call the main since we already are on the
482        // appropriate stack.
483        get_coroutine(proc->runner)->state = Active;
484        main( proc->runner );
485        get_coroutine(proc->runner)->state = Halted;
486
487        // Main routine of the core returned, the core is now fully terminated
488        __cfaabi_dbg_print_safe("Kernel : core %p main ended (%p)\n", proc, &proc->runner);
489
490        return 0p;
491}
492
493static void Abort( int ret, const char func[] ) {
494        if ( ret ) {                                                                            // pthread routines return errno values
495                abort( "%s : internal error, error(%d) %s.", func, ret, strerror( ret ) );
496        } // if
497} // Abort
498
499void * __create_pthread( pthread_t * pthread, void * (*start)(void *), void * arg ) {
500        pthread_attr_t attr;
501
502        Abort( pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute
503
504        size_t stacksize;
505        // default stack size, normally defined by shell limit
506        Abort( pthread_attr_getstacksize( &attr, &stacksize ), "pthread_attr_getstacksize" );
507        assert( stacksize >= PTHREAD_STACK_MIN );
508
509        void * stack;
510        __cfaabi_dbg_debug_do(
511                stack = memalign( __page_size, stacksize + __page_size );
512                // pthread has no mechanism to create the guard page in user supplied stack.
513                if ( mprotect( stack, __page_size, PROT_NONE ) == -1 ) {
514                        abort( "mprotect : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) );
515                } // if
516        );
517        __cfaabi_dbg_no_debug_do(
518                stack = malloc( stacksize );
519        );
520
521        Abort( pthread_attr_setstack( &attr, stack, stacksize ), "pthread_attr_setstack" );
522
523        Abort( pthread_create( pthread, &attr, start, arg ), "pthread_create" );
524        return stack;
525}
526
527// KERNEL_ONLY
528static void __kernel_first_resume( processor * this ) {
529        $thread * src = mainThread;
530        $coroutine * dst = get_coroutine(this->runner);
531
532        verify( ! kernelTLS.preemption_state.enabled );
533
534        kernelTLS.this_thread->curr_cor = dst;
535        __stack_prepare( &dst->stack, 65000 );
536        __cfactx_start(main, dst, this->runner, __cfactx_invoke_coroutine);
537
538        verify( ! kernelTLS.preemption_state.enabled );
539
540        dst->last = &src->self_cor;
541        dst->starter = dst->starter ? dst->starter : &src->self_cor;
542
543        // make sure the current state is still correct
544        /* paranoid */ verify(src->state == Ready);
545
546        // context switch to specified coroutine
547        verify( dst->context.SP );
548        __cfactx_switch( &src->context, &dst->context );
549        // when __cfactx_switch returns we are back in the src coroutine
550
551        mainThread->curr_cor = &mainThread->self_cor;
552
553        // make sure the current state has been update
554        /* paranoid */ verify(src->state == Active);
555
556        verify( ! kernelTLS.preemption_state.enabled );
557}
558
559// KERNEL_ONLY
560static void __kernel_last_resume( processor * this ) {
561        $coroutine * src = &mainThread->self_cor;
562        $coroutine * dst = get_coroutine(this->runner);
563
564        verify( ! kernelTLS.preemption_state.enabled );
565        verify( dst->starter == src );
566        verify( dst->context.SP );
567
568        // SKULLDUGGERY in debug the processors check that the
569        // stack is still within the limit of the stack limits after running a thread.
570        // that check doesn't make sense if we context switch to the processor using the
571        // coroutine semantics. Since this is a special case, use the current context
572        // info to populate these fields.
573        __cfaabi_dbg_debug_do(
574                __stack_context_t ctx;
575                CtxGet( ctx );
576                mainThread->context.SP = ctx.SP;
577                mainThread->context.FP = ctx.FP;
578        )
579
580        // context switch to the processor
581        __cfactx_switch( &src->context, &dst->context );
582}
583
584//-----------------------------------------------------------------------------
585// Scheduler routines
586// KERNEL ONLY
587void __schedule_thread( $thread * thrd ) with( *thrd->curr_cluster ) {
588        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
589        /* paranoid */ #if defined( __CFA_WITH_VERIFY__ )
590        /* paranoid */ if( thrd->state == Blocked || thrd->state == Start ) assertf( thrd->preempted == __NO_PREEMPTION,
591                          "Error inactive thread marked as preempted, state %d, preemption %d\n", thrd->state, thrd->preempted );
592        /* paranoid */ if( thrd->preempted != __NO_PREEMPTION ) assertf(thrd->state == Active || thrd->state == Rerun,
593                          "Error preempted thread marked as not currently running, state %d, preemption %d\n", thrd->state, thrd->preempted );
594        /* paranoid */ #endif
595        /* paranoid */ verifyf( thrd->next == 0p, "Expected null got %p", thrd->next );
596
597        if (thrd->preempted == __NO_PREEMPTION) thrd->state = Ready;
598
599        lock  ( ready_queue_lock __cfaabi_dbg_ctx2 );
600        bool was_empty = !(ready_queue != 0);
601        append( ready_queue, thrd );
602        unlock( ready_queue_lock );
603
604        __wake_one(thrd->curr_cluster, was_empty);
605
606        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
607}
608
609// KERNEL ONLY
610static $thread * __next_thread(cluster * this) with( *this ) {
611        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
612
613        lock( ready_queue_lock __cfaabi_dbg_ctx2 );
614        $thread * head = pop_head( ready_queue );
615        unlock( ready_queue_lock );
616
617        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
618        return head;
619}
620
621// KERNEL ONLY unpark with out disabling interrupts
622void __unpark( $thread * thrd __cfaabi_dbg_ctx_param2 ) {
623        static_assert(sizeof(thrd->state) == sizeof(int));
624
625        // record activity
626        __cfaabi_dbg_record_thrd( *thrd, false, caller );
627
628        enum coroutine_state old_state = __atomic_exchange_n(&thrd->state, Rerun, __ATOMIC_SEQ_CST);
629        __cfaabi_dbg_debug_do( thrd->unpark_result = old_state; )
630        switch(old_state) {
631                case Active:
632                        // Wake won the race, the thread will reschedule/rerun itself
633                        break;
634                case Blocked:
635                        /* paranoid */ verify( ! thrd->preempted != __NO_PREEMPTION );
636
637                        // Wake lost the race,
638                        thrd->state = Blocked;
639                        __schedule_thread( thrd );
640                        break;
641                case Rerun:
642                        abort("More than one thread attempted to schedule thread %p\n", thrd);
643                        break;
644                case Halted:
645                case Start:
646                case Primed:
647                default:
648                        // This makes no sense, something is wrong abort
649                        abort();
650        }
651}
652
653void unpark( $thread * thrd __cfaabi_dbg_ctx_param2 ) {
654        if( !thrd ) return;
655
656        disable_interrupts();
657        __unpark( thrd __cfaabi_dbg_ctx_fwd2 );
658        enable_interrupts( __cfaabi_dbg_ctx );
659}
660
661void park( __cfaabi_dbg_ctx_param ) {
662        /* paranoid */ verify( kernelTLS.preemption_state.enabled );
663        disable_interrupts();
664        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
665        /* paranoid */ verify( kernelTLS.this_thread->preempted == __NO_PREEMPTION );
666
667        // record activity
668        __cfaabi_dbg_record_thrd( *kernelTLS.this_thread, true, caller );
669
670        returnToKernel();
671
672        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
673        enable_interrupts( __cfaabi_dbg_ctx );
674        /* paranoid */ verify( kernelTLS.preemption_state.enabled );
675
676}
677
678// KERNEL ONLY
679void __leave_thread() {
680        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
681        returnToKernel();
682        abort();
683}
684
685// KERNEL ONLY
686bool force_yield( __Preemption_Reason reason ) {
687        /* paranoid */ verify( kernelTLS.preemption_state.enabled );
688        disable_interrupts();
689        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
690
691        $thread * thrd = kernelTLS.this_thread;
692        /* paranoid */ verify(thrd->state == Active || thrd->state == Rerun);
693
694        // SKULLDUGGERY: It is possible that we are preempting this thread just before
695        // it was going to park itself. If that is the case and it is already using the
696        // intrusive fields then we can't use them to preempt the thread
697        // If that is the case, abandon the preemption.
698        bool preempted = false;
699        if(thrd->next == 0p) {
700                preempted = true;
701                thrd->preempted = reason;
702                returnToKernel();
703        }
704
705        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
706        enable_interrupts_noPoll();
707        /* paranoid */ verify( kernelTLS.preemption_state.enabled );
708
709        return preempted;
710}
711
712//=============================================================================================
713// Kernel Setup logic
714//=============================================================================================
715//-----------------------------------------------------------------------------
716// Kernel boot procedures
717static void __kernel_startup(void) {
718        verify( ! kernelTLS.preemption_state.enabled );
719        __cfaabi_dbg_print_safe("Kernel : Starting\n");
720
721        __page_size = sysconf( _SC_PAGESIZE );
722
723        __cfa_dbg_global_clusters.list{ __get };
724        __cfa_dbg_global_clusters.lock{};
725
726        // Initialize the main cluster
727        mainCluster = (cluster *)&storage_mainCluster;
728        (*mainCluster){"Main Cluster"};
729
730        __cfaabi_dbg_print_safe("Kernel : Main cluster ready\n");
731
732        // Start by initializing the main thread
733        // SKULLDUGGERY: the mainThread steals the process main thread
734        // which will then be scheduled by the mainProcessor normally
735        mainThread = ($thread *)&storage_mainThread;
736        current_stack_info_t info;
737        info.storage = (__stack_t*)&storage_mainThreadCtx;
738        (*mainThread){ &info };
739
740        __cfaabi_dbg_print_safe("Kernel : Main thread ready\n");
741
742
743
744        // Construct the processor context of the main processor
745        void ?{}(processorCtx_t & this, processor * proc) {
746                (this.__cor){ "Processor" };
747                this.__cor.starter = 0p;
748                this.proc = proc;
749        }
750
751        void ?{}(processor & this) with( this ) {
752                name = "Main Processor";
753                cltr = mainCluster;
754                terminated{ 0 };
755                do_terminate = false;
756                preemption_alarm = 0p;
757                pending_preemption = false;
758                kernel_thread = pthread_self();
759
760                runner{ &this };
761                __cfaabi_dbg_print_safe("Kernel : constructed main processor context %p\n", &runner);
762        }
763
764        // Initialize the main processor and the main processor ctx
765        // (the coroutine that contains the processing control flow)
766        mainProcessor = (processor *)&storage_mainProcessor;
767        (*mainProcessor){};
768
769        //initialize the global state variables
770        kernelTLS.this_processor = mainProcessor;
771        kernelTLS.this_thread    = mainThread;
772
773        // Enable preemption
774        kernel_start_preemption();
775
776        // Add the main thread to the ready queue
777        // once resume is called on mainProcessor->runner the mainThread needs to be scheduled like any normal thread
778        __schedule_thread(mainThread);
779
780        // SKULLDUGGERY: Force a context switch to the main processor to set the main thread's context to the current UNIX
781        // context. Hence, the main thread does not begin through __cfactx_invoke_thread, like all other threads. The trick here is that
782        // mainThread is on the ready queue when this call is made.
783        __kernel_first_resume( kernelTLS.this_processor );
784
785
786
787        // THE SYSTEM IS NOW COMPLETELY RUNNING
788        __cfaabi_dbg_print_safe("Kernel : Started\n--------------------------------------------------\n\n");
789
790        verify( ! kernelTLS.preemption_state.enabled );
791        enable_interrupts( __cfaabi_dbg_ctx );
792        verify( TL_GET( preemption_state.enabled ) );
793}
794
795static void __kernel_shutdown(void) {
796        __cfaabi_dbg_print_safe("\n--------------------------------------------------\nKernel : Shutting down\n");
797
798        /* paranoid */ verify( TL_GET( preemption_state.enabled ) );
799        disable_interrupts();
800        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
801
802        // SKULLDUGGERY: Notify the mainProcessor it needs to terminates.
803        // When its coroutine terminates, it return control to the mainThread
804        // which is currently here
805        __atomic_store_n(&mainProcessor->do_terminate, true, __ATOMIC_RELEASE);
806        __kernel_last_resume( kernelTLS.this_processor );
807        mainThread->self_cor.state = Halted;
808
809        // THE SYSTEM IS NOW COMPLETELY STOPPED
810
811        // Disable preemption
812        kernel_stop_preemption();
813
814        // Destroy the main processor and its context in reverse order of construction
815        // These were manually constructed so we need manually destroy them
816        ^(*mainProcessor){};
817
818        // Final step, destroy the main thread since it is no longer needed
819        // Since we provided a stack to this taxk it will not destroy anything
820        /* paranoid */ verify(mainThread->self_cor.stack.storage == (__stack_t*)(((uintptr_t)&storage_mainThreadCtx)| 0x1));
821        ^(*mainThread){};
822
823        ^(*mainCluster){};
824
825        ^(__cfa_dbg_global_clusters.list){};
826        ^(__cfa_dbg_global_clusters.lock){};
827
828        __cfaabi_dbg_print_safe("Kernel : Shutdown complete\n");
829}
830
831//=============================================================================================
832// Kernel Idle Sleep
833//=============================================================================================
834static $thread * __halt(processor * this) with( *this ) {
835        if( do_terminate ) return 0p;
836
837        // First, lock the cluster idle
838        lock( cltr->idle_lock __cfaabi_dbg_ctx2 );
839
840        // Check if we can find a thread
841        if( $thread * found = __next_thread( cltr ) ) {
842                unlock( cltr->idle_lock );
843                return found;
844        }
845
846        // Move this processor from the active list to the idle list
847        move_to_front(cltr->procs, cltr->idles, *this);
848
849        // Unlock the idle lock so we don't go to sleep with a lock
850        unlock    (cltr->idle_lock);
851
852        // We are ready to sleep
853        __cfaabi_dbg_print_safe("Kernel : Processor %p ready to sleep\n", this);
854        wait( idle );
855
856        // We have woken up
857        __cfaabi_dbg_print_safe("Kernel : Processor %p woke up and ready to run\n", this);
858
859        // Get ourself off the idle list
860        with( *cltr ) {
861                lock  (idle_lock __cfaabi_dbg_ctx2);
862                move_to_front(idles, procs, *this);
863                unlock(idle_lock);
864        }
865
866        // Don't check the ready queue again, we may not be in a position to run a thread
867        return 0p;
868}
869
870// Wake a thread from the front if there are any
871static bool __wake_one(cluster * this, __attribute__((unused)) bool force) {
872        // if we don't want to force check if we know it's false
873        if( !this->idles.head && !force ) return false;
874
875        // First, lock the cluster idle
876        lock( this->idle_lock __cfaabi_dbg_ctx2 );
877
878        // Check if there is someone to wake up
879        if( !this->idles.head ) {
880                // Nope unlock and return false
881                unlock( this->idle_lock );
882                return false;
883        }
884
885        // Wake them up
886        post( this->idles.head->idle );
887
888        // Unlock and return true
889        unlock( this->idle_lock );
890        return true;
891}
892
893// Unconditionnaly wake a thread
894static bool __wake_proc(processor * this) {
895        return post( this->idle );
896}
897
898//=============================================================================================
899// Unexpected Terminating logic
900//=============================================================================================
901static __spinlock_t kernel_abort_lock;
902static bool kernel_abort_called = false;
903
904void * kernel_abort(void) __attribute__ ((__nothrow__)) {
905        // abort cannot be recursively entered by the same or different processors because all signal handlers return when
906        // the globalAbort flag is true.
907        lock( kernel_abort_lock __cfaabi_dbg_ctx2 );
908
909        // first task to abort ?
910        if ( kernel_abort_called ) {                    // not first task to abort ?
911                unlock( kernel_abort_lock );
912
913                sigset_t mask;
914                sigemptyset( &mask );
915                sigaddset( &mask, SIGALRM );            // block SIGALRM signals
916                sigaddset( &mask, SIGUSR1 );            // block SIGALRM signals
917                sigsuspend( &mask );                            // block the processor to prevent further damage during abort
918                _exit( EXIT_FAILURE );                          // if processor unblocks before it is killed, terminate it
919        }
920        else {
921                kernel_abort_called = true;
922                unlock( kernel_abort_lock );
923        }
924
925        return kernelTLS.this_thread;
926}
927
928void kernel_abort_msg( void * kernel_data, char * abort_text, int abort_text_size ) {
929        $thread * thrd = kernel_data;
930
931        if(thrd) {
932                int len = snprintf( abort_text, abort_text_size, "Error occurred while executing thread %.256s (%p)", thrd->self_cor.name, thrd );
933                __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
934
935                if ( &thrd->self_cor != thrd->curr_cor ) {
936                        len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", thrd->curr_cor->name, thrd->curr_cor );
937                        __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
938                }
939                else {
940                        __cfaabi_bits_write( STDERR_FILENO, ".\n", 2 );
941                }
942        }
943        else {
944                int len = snprintf( abort_text, abort_text_size, "Error occurred outside of any thread.\n" );
945                __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
946        }
947}
948
949int kernel_abort_lastframe( void ) __attribute__ ((__nothrow__)) {
950        return get_coroutine(kernelTLS.this_thread) == get_coroutine(mainThread) ? 4 : 2;
951}
952
953static __spinlock_t kernel_debug_lock;
954
955extern "C" {
956        void __cfaabi_bits_acquire() {
957                lock( kernel_debug_lock __cfaabi_dbg_ctx2 );
958        }
959
960        void __cfaabi_bits_release() {
961                unlock( kernel_debug_lock );
962        }
963}
964
965//=============================================================================================
966// Kernel Utilities
967//=============================================================================================
968//-----------------------------------------------------------------------------
969// Locks
970void  ?{}( semaphore & this, int count = 1 ) {
971        (this.lock){};
972        this.count = count;
973        (this.waiting){};
974}
975void ^?{}(semaphore & this) {}
976
977void P(semaphore & this) with( this ){
978        lock( lock __cfaabi_dbg_ctx2 );
979        count -= 1;
980        if ( count < 0 ) {
981                // queue current task
982                append( waiting, kernelTLS.this_thread );
983
984                // atomically release spin lock and block
985                unlock( lock );
986                park( __cfaabi_dbg_ctx );
987        }
988        else {
989            unlock( lock );
990        }
991}
992
993bool V(semaphore & this) with( this ) {
994        $thread * thrd = 0p;
995        lock( lock __cfaabi_dbg_ctx2 );
996        count += 1;
997        if ( count <= 0 ) {
998                // remove task at head of waiting list
999                thrd = pop_head( waiting );
1000        }
1001
1002        unlock( lock );
1003
1004        // make new owner
1005        unpark( thrd __cfaabi_dbg_ctx2 );
1006
1007        return thrd != 0p;
1008}
1009
1010bool V(semaphore & this, unsigned diff) with( this ) {
1011        $thread * thrd = 0p;
1012        lock( lock __cfaabi_dbg_ctx2 );
1013        int release = max(-count, (int)diff);
1014        count += diff;
1015        for(release) {
1016                unpark( pop_head( waiting ) __cfaabi_dbg_ctx2 );
1017        }
1018
1019        unlock( lock );
1020
1021        return thrd != 0p;
1022}
1023
1024//-----------------------------------------------------------------------------
1025// Global Queues
1026void doregister( cluster     & cltr ) {
1027        lock      ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2);
1028        push_front( __cfa_dbg_global_clusters.list, cltr );
1029        unlock    ( __cfa_dbg_global_clusters.lock );
1030}
1031
1032void unregister( cluster     & cltr ) {
1033        lock  ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2);
1034        remove( __cfa_dbg_global_clusters.list, cltr );
1035        unlock( __cfa_dbg_global_clusters.lock );
1036}
1037
1038void doregister( cluster * cltr, $thread & thrd ) {
1039        lock      (cltr->thread_list_lock __cfaabi_dbg_ctx2);
1040        cltr->nthreads += 1;
1041        push_front(cltr->threads, thrd);
1042        unlock    (cltr->thread_list_lock);
1043}
1044
1045void unregister( cluster * cltr, $thread & thrd ) {
1046        lock  (cltr->thread_list_lock __cfaabi_dbg_ctx2);
1047        remove(cltr->threads, thrd );
1048        cltr->nthreads -= 1;
1049        unlock(cltr->thread_list_lock);
1050}
1051
1052void doregister( cluster * cltr, processor * proc ) {
1053        lock      (cltr->idle_lock __cfaabi_dbg_ctx2);
1054        cltr->nprocessors += 1;
1055        push_front(cltr->procs, *proc);
1056        unlock    (cltr->idle_lock);
1057}
1058
1059void unregister( cluster * cltr, processor * proc ) {
1060        lock  (cltr->idle_lock __cfaabi_dbg_ctx2);
1061        remove(cltr->procs, *proc );
1062        cltr->nprocessors -= 1;
1063        unlock(cltr->idle_lock);
1064}
1065
1066//-----------------------------------------------------------------------------
1067// Debug
1068__cfaabi_dbg_debug_do(
1069        extern "C" {
1070                void __cfaabi_dbg_record_lock(__spinlock_t & this, const char prev_name[]) {
1071                        this.prev_name = prev_name;
1072                        this.prev_thrd = kernelTLS.this_thread;
1073                }
1074
1075                void __cfaabi_dbg_record_thrd($thread & this, bool park, const char prev_name[]) {
1076                        if(park) {
1077                                this.park_caller   = prev_name;
1078                                this.park_stale    = false;
1079                        }
1080                        else {
1081                                this.unpark_caller = prev_name;
1082                                this.unpark_stale  = false;
1083                        }
1084                }
1085        }
1086)
1087
1088//-----------------------------------------------------------------------------
1089// Debug
1090bool threading_enabled(void) __attribute__((const)) {
1091        return true;
1092}
1093// Local Variables: //
1094// mode: c //
1095// tab-width: 4 //
1096// End: //
Note: See TracBrowser for help on using the repository browser.