Ignore:
Timestamp:
Dec 10, 2020, 3:20:33 PM (4 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
297cf18
Parents:
c920317
Message:

Stacks are always created with mmap to control page permission

File:
1 edited

Legend:

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

    rc920317 rbfcf6b9  
    2929#include "kernel_private.hfa"
    3030#include "startup.hfa"          // STARTUP_PRIORITY_XXX
     31#include "math.hfa"
    3132
    3233//-----------------------------------------------------------------------------
     
    539540}
    540541
     542extern size_t __page_size;
    541543void ^?{}(processor & this) with( this ){
    542544        if( ! __atomic_load_n(&do_terminate, __ATOMIC_ACQUIRE) ) {
     
    550552        }
    551553
    552         int err = pthread_join( kernel_thread, 0p );
    553         if( err != 0 ) abort("KERNEL ERROR: joining processor %p caused error %s\n", &this, strerror(err));
    554 
    555         free( this.stack );
     554        __destroy_pthread( kernel_thread, this.stack, 0p );
    556555
    557556        disable_interrupts();
     
    678677
    679678        void * stack;
    680         __cfaabi_dbg_debug_do(
    681                 stack = memalign( __page_size, stacksize + __page_size );
    682                 // pthread has no mechanism to create the guard page in user supplied stack.
    683                 if ( mprotect( stack, __page_size, PROT_NONE ) == -1 ) {
    684                         abort( "mprotect : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) );
    685                 } // if
    686         );
    687         __cfaabi_dbg_no_debug_do(
    688                 stack = malloc( stacksize );
    689         );
     679        #warning due to the thunk problem, stack creation uses mmap, revert to malloc once this goes away
     680        // __cfaabi_dbg_debug_do(
     681        //      stack = memalign( __page_size, stacksize + __page_size );
     682        //      // pthread has no mechanism to create the guard page in user supplied stack.
     683        //      if ( mprotect( stack, __page_size, PROT_NONE ) == -1 ) {
     684        //              abort( "mprotect : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) );
     685        //      } // if
     686        // );
     687        // __cfaabi_dbg_no_debug_do(
     688        //      stack = malloc( stacksize );
     689        // );
     690        stacksize = ceiling( stacksize, __page_size ) + __page_size;
     691        stack = mmap(0p, stacksize, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
     692        if(stack == ((void*)-1)) {
     693                abort( "pthread stack creation : internal error, mmap failure, error(%d) %s.", errno, strerror( errno ) );
     694        }
     695        if ( mprotect( stack, __page_size, PROT_NONE ) == -1 ) {
     696                abort( "pthread stack creation : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) );
     697        } // if
    690698
    691699        check( pthread_attr_setstack( &attr, stack, stacksize ), "pthread_attr_setstack" );
     
    694702        return stack;
    695703}
     704
     705void __destroy_pthread( pthread_t pthread, void * stack, void ** retval ) {
     706        int err = pthread_join( pthread, retval );
     707        if( err != 0 ) abort("KERNEL ERROR: joining pthread %p caused error %s\n", (void*)pthread, strerror(err));
     708
     709        pthread_attr_t attr;
     710
     711        check( pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute
     712
     713        size_t stacksize;
     714        // default stack size, normally defined by shell limit
     715        check( pthread_attr_getstacksize( &attr, &stacksize ), "pthread_attr_getstacksize" );
     716        assert( stacksize >= PTHREAD_STACK_MIN );
     717        stacksize += __page_size;
     718
     719        if(munmap(stack, stacksize) == -1) {
     720                abort( "pthread stack destruction : internal error, munmap failure, error(%d) %s.", errno, strerror( errno ) );
     721        }
     722}
     723
    696724
    697725#if defined(__CFA_WITH_VERIFY__)
Note: See TracChangeset for help on using the changeset viewer.