Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/concurrency/kernel.cfa

    r2026bb6 r09d4b22  
    1010// Created On       : Tue Jan 17 12:27:26 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Jun 20 17:21:23 2019
    13 // Update Count     : 25
     12// Last Modified On : Thu Dec  5 16:25:52 2019
     13// Update Count     : 52
    1414//
    1515
     
    2626#include <signal.h>
    2727#include <unistd.h>
     28#include <limits.h>                                                                             // PTHREAD_STACK_MIN
     29#include <sys/mman.h>                                                                   // mprotect
    2830}
    2931
     
    4042//-----------------------------------------------------------------------------
    4143// Some assembly required
    42 #if   defined( __i386 )
     44#if defined( __i386 )
    4345        #define CtxGet( ctx )        \
    4446                __asm__ volatile (     \
     
    123125
    124126extern "C" {
    125 struct { __dllist_t(cluster) list; __spinlock_t lock; } __cfa_dbg_global_clusters;
     127        struct { __dllist_t(cluster) list; __spinlock_t lock; } __cfa_dbg_global_clusters;
    126128}
    127129
     
    131133// Global state
    132134thread_local struct KernelThreadData kernelTLS __attribute__ ((tls_model ( "initial-exec" ))) = {
     135        NULL,                                                                                           // cannot use 0p
    133136        NULL,
    134         NULL,
    135         { 1, false, false }
     137        { 1, false, false },
     138        6u //this should be seeded better but due to a bug calling rdtsc doesn't work
    136139};
    137140
     
    139142// Struct to steal stack
    140143struct current_stack_info_t {
    141         __stack_t * storage;            // pointer to stack object
    142         void *base;                             // base of stack
    143         void *limit;                    // stack grows towards stack limit
    144         void *context;                  // address of cfa_context_t
     144        __stack_t * storage;                                                            // pointer to stack object
     145        void * base;                                                                            // base of stack
     146        void * limit;                                                                           // stack grows towards stack limit
     147        void * context;                                                                         // address of cfa_context_t
    145148};
    146149
     
    171174        name = "Main Thread";
    172175        state = Start;
    173         starter = NULL;
    174         last = NULL;
    175         cancellation = NULL;
     176        starter = 0p;
     177        last = 0p;
     178        cancellation = 0p;
    176179}
    177180
     
    184187        self_mon.recursion = 1;
    185188        self_mon_p = &self_mon;
    186         next = NULL;
    187 
    188         node.next = NULL;
    189         node.prev = NULL;
     189        next = 0p;
     190
     191        node.next = 0p;
     192        node.prev = 0p;
    190193        doregister(curr_cluster, this);
    191194
     
    211214        terminated{ 0 };
    212215        do_terminate = false;
    213         preemption_alarm = NULL;
     216        preemption_alarm = 0p;
    214217        pending_preemption = false;
    215218        runner.proc = &this;
     
    231234        }
    232235
    233         pthread_join( kernel_thread, NULL );
     236        pthread_join( kernel_thread, 0p );
     237        free( this.stack );
    234238}
    235239
     
    260264//Main of the processor contexts
    261265void main(processorCtx_t & runner) {
     266        // Because of a bug, we couldn't initialized the seed on construction
     267        // Do it here
     268        kernelTLS.rand_seed ^= rdtscl();
     269
    262270        processor * this = runner.proc;
    263271        verify(this);
     
    273281                __cfaabi_dbg_print_safe("Kernel : core %p started\n", this);
    274282
    275                 thread_desc * readyThread = NULL;
    276                 for( unsigned int spin_count = 0; ! __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST); spin_count++ )
    277                 {
     283                thread_desc * readyThread = 0p;
     284                for( unsigned int spin_count = 0; ! __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST); spin_count++ ) {
    278285                        readyThread = nextThread( this->cltr );
    279286
    280                         if(readyThread)
    281                         {
     287                        if(readyThread) {
    282288                                verify( ! kernelTLS.preemption_state.enabled );
    283289
     
    290296
    291297                                spin_count = 0;
    292                         }
    293                         else
    294                         {
     298                        } else {
    295299                                // spin(this, &spin_count);
    296300                                halt(this);
     
    405409        processor * proc = (processor *) arg;
    406410        kernelTLS.this_processor = proc;
    407         kernelTLS.this_thread    = NULL;
     411        kernelTLS.this_thread    = 0p;
    408412        kernelTLS.preemption_state.[enabled, disable_count] = [false, 1];
    409413        // SKULLDUGGERY: We want to create a context for the processor coroutine
     
    418422
    419423        //Set global state
    420         kernelTLS.this_thread    = NULL;
     424        kernelTLS.this_thread = 0p;
    421425
    422426        //We now have a proper context from which to schedule threads
     
    434438        __cfaabi_dbg_print_safe("Kernel : core %p main ended (%p)\n", proc, &proc->runner);
    435439
    436         return NULL;
     440        return 0p;
     441}
     442
     443static void Abort( int ret, const char * func ) {
     444        if ( ret ) {                                                                            // pthread routines return errno values
     445                abort( "%s : internal error, error(%d) %s.", func, ret, strerror( ret ) );
     446        } // if
     447} // Abort
     448
     449void * create_pthread( pthread_t * pthread, void * (*start)(void *), void * arg ) {
     450        pthread_attr_t attr;
     451
     452        Abort( pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute
     453
     454        size_t stacksize;
     455        // default stack size, normally defined by shell limit
     456        Abort( pthread_attr_getstacksize( &attr, &stacksize ), "pthread_attr_getstacksize" );
     457        assert( stacksize >= PTHREAD_STACK_MIN );
     458
     459        void * stack;
     460        __cfaabi_dbg_debug_do(
     461                stack = memalign( __page_size, stacksize + __page_size );
     462                // pthread has no mechanism to create the guard page in user supplied stack.
     463                if ( mprotect( stack, __page_size, PROT_NONE ) == -1 ) {
     464                        abort( "mprotect : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) );
     465                } // if
     466        );
     467        __cfaabi_dbg_no_debug_do(
     468                stack = malloc( stacksize );
     469        );
     470
     471        Abort( pthread_attr_setstack( &attr, stack, stacksize ), "pthread_attr_setstack" );
     472
     473        Abort( pthread_create( pthread, &attr, start, arg ), "pthread_create" );
     474        return stack;
    437475}
    438476
     
    440478        __cfaabi_dbg_print_safe("Kernel : Starting core %p\n", this);
    441479
    442         pthread_create( &this->kernel_thread, NULL, CtxInvokeProcessor, (void*)this );
     480        this->stack = create_pthread( &this->kernel_thread, CtxInvokeProcessor, (void *)this );
    443481
    444482        __cfaabi_dbg_print_safe("Kernel : core %p started\n", this);
     
    497535        verify( ! kernelTLS.preemption_state.enabled );
    498536
    499         verifyf( thrd->next == NULL, "Expected null got %p", thrd->next );
     537        verifyf( thrd->next == 0p, "Expected null got %p", thrd->next );
    500538
    501539        with( *thrd->curr_cluster ) {
     
    676714        void ?{}(processorCtx_t & this, processor * proc) {
    677715                (this.__cor){ "Processor" };
    678                 this.__cor.starter = NULL;
     716                this.__cor.starter = 0p;
    679717                this.proc = proc;
    680718        }
     
    685723                terminated{ 0 };
    686724                do_terminate = false;
    687                 preemption_alarm = NULL;
     725                preemption_alarm = 0p;
    688726                pending_preemption = false;
    689727                kernel_thread = pthread_self();
     
    819857        if(thrd) {
    820858                int len = snprintf( abort_text, abort_text_size, "Error occurred while executing thread %.256s (%p)", thrd->self_cor.name, thrd );
    821                 __cfaabi_dbg_bits_write( abort_text, len );
     859                __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
    822860
    823861                if ( &thrd->self_cor != thrd->curr_cor ) {
    824862                        len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", thrd->curr_cor->name, thrd->curr_cor );
    825                         __cfaabi_dbg_bits_write( abort_text, len );
     863                        __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
    826864                }
    827865                else {
    828                         __cfaabi_dbg_bits_write( ".\n", 2 );
     866                        __cfaabi_bits_write( STDERR_FILENO, ".\n", 2 );
    829867                }
    830868        }
    831869        else {
    832870                int len = snprintf( abort_text, abort_text_size, "Error occurred outside of any thread.\n" );
    833                 __cfaabi_dbg_bits_write( abort_text, len );
     871                __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
    834872        }
    835873}
     
    842880
    843881extern "C" {
    844         void __cfaabi_dbg_bits_acquire() {
     882        void __cfaabi_bits_acquire() {
    845883                lock( kernel_debug_lock __cfaabi_dbg_ctx2 );
    846884        }
    847885
    848         void __cfaabi_dbg_bits_release() {
     886        void __cfaabi_bits_release() {
    849887                unlock( kernel_debug_lock );
    850888        }
     
    879917
    880918void V(semaphore & this) with( this ) {
    881         thread_desc * thrd = NULL;
     919        thread_desc * thrd = 0p;
    882920        lock( lock __cfaabi_dbg_ctx2 );
    883921        count += 1;
Note: See TracChangeset for help on using the changeset viewer.