source: libcfa/src/concurrency/kernel.cfa @ f00b26d4

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

Re-worked IO to use epoll and support multiple io_contexts per cluster.
Also redid how cluster options are handled.
Changed how iofwd calls are passed to support future features and io_contexts rework.

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