Changeset 97229d6


Ignore:
Timestamp:
Dec 14, 2020, 2:31:50 PM (3 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:
5dc5da7, 90ecade, ccb8c8a
Parents:
867fca3
Message:

Changed stack creation to toggle between mmap and malloc based on the defines CFA_COROUTINE_USE_MMAP and CFA_PROCESSOR_USE_MMAP

Location:
libcfa/src/concurrency
Files:
2 edited

Legend:

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

    r867fca3 r97229d6  
    2929#include "exception.hfa"
    3030#include "math.hfa"
     31
     32#define CFA_COROUTINE_USE_MMAP 0
    3133
    3234#define __CFA_INVOKE_PRIVATE__
     
    110112        if ( ! userStack && this.storage ) {
    111113                __stack_clean( &this );
    112                 // __attribute__((may_alias)) intptr_t * istorage = (intptr_t *)&this.storage;
    113                 // *istorage &= (intptr_t)-1;
    114 
    115                 // void * storage = this.storage->limit;
    116                 // __cfaabi_dbg_debug_do(
    117                 //      storage = (char*)(storage) - __page_size;
    118                 //      if ( mprotect( storage, __page_size, PROT_READ | PROT_WRITE ) == -1 ) {
    119                 //              abort( "(coStack_t *)%p.^?{}() : internal error, mprotect failure, error(%d) %s.", &this, errno, strerror( errno ) );
    120                 //      }
    121                 // );
    122                 // __cfaabi_dbg_print_safe("Kernel : Deleting stack %p\n", storage);
    123                 // free( storage );
    124114        }
    125115}
     
    174164        // If we are running debug, we also need to allocate a guardpage to catch stack overflows.
    175165        void * storage;
    176         // __cfaabi_dbg_debug_do(
    177         //      storage = memalign( __page_size, size + __page_size );
    178         // );
    179         // __cfaabi_dbg_no_debug_do(
    180         //      storage = (void*)malloc(size);
    181         // );
    182 
    183         // __cfaabi_dbg_print_safe("Kernel : Created stack %p of size %zu\n", storage, size);
    184         // __cfaabi_dbg_debug_do(
    185         //      if ( mprotect( storage, __page_size, PROT_NONE ) == -1 ) {
    186         //              abort( "__stack_alloc : internal error, mprotect failure, error(%d) %s.", (int)errno, strerror( (int)errno ) );
    187         //      }
    188         //      storage = (void *)(((intptr_t)storage) + __page_size);
    189         // );
    190         storage = mmap(0p, size + __page_size, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
    191         if(storage == ((void*)-1)) {
    192                 abort( "coroutine stack creation : internal error, mmap failure, error(%d) %s.", errno, strerror( errno ) );
    193         }
    194         if ( mprotect( storage, __page_size, PROT_NONE ) == -1 ) {
    195                 abort( "coroutine stack creation : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) );
    196         } // if
    197         storage = (void *)(((intptr_t)storage) + __page_size);
     166        #if CFA_COROUTINE_USE_MMAP
     167                storage = mmap(0p, size + __page_size, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
     168                if(storage == ((void*)-1)) {
     169                        abort( "coroutine stack creation : internal error, mmap failure, error(%d) %s.", errno, strerror( errno ) );
     170                }
     171                if ( mprotect( storage, __page_size, PROT_NONE ) == -1 ) {
     172                        abort( "coroutine stack creation : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) );
     173                } // if
     174                storage = (void *)(((intptr_t)storage) + __page_size);
     175        #else
     176                __cfaabi_dbg_debug_do(
     177                        storage = memalign( __page_size, size + __page_size );
     178                );
     179                __cfaabi_dbg_no_debug_do(
     180                        storage = (void*)malloc(size);
     181                );
     182
     183                __cfaabi_dbg_debug_do(
     184                        if ( mprotect( storage, __page_size, PROT_NONE ) == -1 ) {
     185                                abort( "__stack_alloc : internal error, mprotect failure, error(%d) %s.", (int)errno, strerror( (int)errno ) );
     186                        }
     187                        storage = (void *)(((intptr_t)storage) + __page_size);
     188                );
     189        #endif
     190        __cfaabi_dbg_print_safe("Kernel : Created stack %p of size %zu\n", storage, size);
    198191
    199192        verify( ((intptr_t)storage & (libAlign() - 1)) == 0ul );
     
    205198        void * storage = this->storage->limit;
    206199
    207         storage = (void *)(((intptr_t)storage) - __page_size);
    208         if(munmap(storage, size + __page_size) == -1) {
    209                 abort( "coroutine stack destruction : internal error, munmap failure, error(%d) %s.", errno, strerror( errno ) );
    210         }
     200        #if CFA_COROUTINE_USE_MMAP
     201                storage = (void *)(((intptr_t)storage) - __page_size);
     202                if(munmap(storage, size + __page_size) == -1) {
     203                        abort( "coroutine stack destruction : internal error, munmap failure, error(%d) %s.", errno, strerror( errno ) );
     204                }
     205        #else
     206                __cfaabi_dbg_debug_do(
     207                        storage = (char*)(storage) - __page_size;
     208                        if ( mprotect( storage, __page_size, PROT_READ | PROT_WRITE ) == -1 ) {
     209                                abort( "(coStack_t *)%p.^?{}() : internal error, mprotect failure, error(%d) %s.", &this, errno, strerror( errno ) );
     210                        }
     211                );
     212
     213                free( storage );
     214        #endif
     215        __cfaabi_dbg_print_safe("Kernel : Deleting stack %p\n", storage);
    211216}
    212217
  • libcfa/src/concurrency/kernel/startup.cfa

    r867fca3 r97229d6  
    3030#include "startup.hfa"          // STARTUP_PRIORITY_XXX
    3131#include "math.hfa"
     32
     33#define CFA_PROCESSOR_USE_MMAP 0
    3234
    3335//-----------------------------------------------------------------------------
     
    677679
    678680        void * stack;
    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
     681        #if CFA_PROCESSOR_USE_MMAP
     682                stacksize = ceiling( stacksize, __page_size ) + __page_size;
     683                stack = mmap(0p, stacksize, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
     684                if(stack == ((void*)-1)) {
     685                        abort( "pthread stack creation : internal error, mmap failure, error(%d) %s.", errno, strerror( errno ) );
     686                }
     687                if ( mprotect( stack, __page_size, PROT_NONE ) == -1 ) {
     688                        abort( "pthread stack creation : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) );
     689                } // if
     690        #else
     691                __cfaabi_dbg_debug_do(
     692                        stack = memalign( __page_size, stacksize + __page_size );
     693                        // pthread has no mechanism to create the guard page in user supplied stack.
     694                        if ( mprotect( stack, __page_size, PROT_NONE ) == -1 ) {
     695                                abort( "mprotect : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) );
     696                        } // if
     697                );
     698                __cfaabi_dbg_no_debug_do(
     699                        stack = malloc( stacksize );
     700                );
     701        #endif
     702
    698703
    699704        check( pthread_attr_setstack( &attr, stack, stacksize ), "pthread_attr_setstack" );
     
    707712        if( err != 0 ) abort("KERNEL ERROR: joining pthread %p caused error %s\n", (void*)pthread, strerror(err));
    708713
    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         }
     714        #if CFA_PROCESSOR_USE_MMAP
     715                pthread_attr_t attr;
     716
     717                check( pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute
     718
     719                size_t stacksize;
     720                // default stack size, normally defined by shell limit
     721                check( pthread_attr_getstacksize( &attr, &stacksize ), "pthread_attr_getstacksize" );
     722                assert( stacksize >= PTHREAD_STACK_MIN );
     723                stacksize += __page_size;
     724
     725                if(munmap(stack, stacksize) == -1) {
     726                        abort( "pthread stack destruction : internal error, munmap failure, error(%d) %s.", errno, strerror( errno ) );
     727                }
     728        #else
     729                free( stack );
     730        #endif
    722731}
    723732
Note: See TracChangeset for help on using the changeset viewer.