Ignore:
File:
1 edited

Legend:

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

    rdd92fe9 rbe73f30  
    1010// Created On       : Mon Nov 28 12:27:26 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Dec 15 12:06:04 2020
    13 // Update Count     : 23
     12// Last Modified On : Fri Oct 23 23:05:24 2020
     13// Update Count     : 22
    1414//
    1515
     
    2828#include "kernel_private.hfa"
    2929#include "exception.hfa"
    30 #include "math.hfa"
    31 
    32 #define CFA_COROUTINE_USE_MMAP 0
    3330
    3431#define __CFA_INVOKE_PRIVATE__
     
    8885static const size_t MinStackSize = 1000;
    8986extern size_t __page_size;                              // architecture pagesize HACK, should go in proper runtime singleton
    90 extern int __map_prot;
    9187
    9288void __stack_prepare( __stack_info_t * this, size_t create_size );
    93 void __stack_clean  ( __stack_info_t * this );
    9489
    9590//-----------------------------------------------------------------------------
     
    112107        bool userStack = ((intptr_t)this.storage & 0x1) != 0;
    113108        if ( ! userStack && this.storage ) {
    114                 __stack_clean( &this );
     109                __attribute__((may_alias)) intptr_t * istorage = (intptr_t *)&this.storage;
     110                *istorage &= (intptr_t)-1;
     111
     112                void * storage = this.storage->limit;
     113                __cfaabi_dbg_debug_do(
     114                        storage = (char*)(storage) - __page_size;
     115                        if ( mprotect( storage, __page_size, PROT_READ | PROT_WRITE ) == -1 ) {
     116                                abort( "(coStack_t *)%p.^?{}() : internal error, mprotect failure, error(%d) %s.", &this, errno, strerror( errno ) );
     117                        }
     118                );
     119                __cfaabi_dbg_print_safe("Kernel : Deleting stack %p\n", storage);
     120                free( storage );
    115121        }
    116122}
     
    161167        assert(__page_size != 0l);
    162168        size_t size = libCeiling( storageSize, 16 ) + stack_data_size;
    163         size = ceiling(size, __page_size);
    164169
    165170        // If we are running debug, we also need to allocate a guardpage to catch stack overflows.
    166171        void * storage;
    167         #if CFA_COROUTINE_USE_MMAP
    168                 storage = mmap(0p, size + __page_size, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
    169                 if(storage == ((void*)-1)) {
    170                         abort( "coroutine stack creation : internal error, mmap failure, error(%d) %s.", errno, strerror( errno ) );
     172        __cfaabi_dbg_debug_do(
     173                storage = memalign( __page_size, size + __page_size );
     174        );
     175        __cfaabi_dbg_no_debug_do(
     176                storage = (void*)malloc(size);
     177        );
     178
     179        __cfaabi_dbg_print_safe("Kernel : Created stack %p of size %zu\n", storage, size);
     180        __cfaabi_dbg_debug_do(
     181                if ( mprotect( storage, __page_size, PROT_NONE ) == -1 ) {
     182                        abort( "__stack_alloc : internal error, mprotect failure, error(%d) %s.", (int)errno, strerror( (int)errno ) );
    171183                }
    172                 if ( mprotect( storage, __page_size, PROT_NONE ) == -1 ) {
    173                         abort( "coroutine stack creation : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) );
    174                 } // if
    175184                storage = (void *)(((intptr_t)storage) + __page_size);
    176         #else
    177                 __cfaabi_dbg_debug_do(
    178                         storage = memalign( __page_size, size + __page_size );
    179                 );
    180                 __cfaabi_dbg_no_debug_do(
    181                         storage = (void*)malloc(size);
    182                 );
    183 
    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         #endif
    191         __cfaabi_dbg_print_safe("Kernel : Created stack %p of size %zu\n", storage, size);
     185        );
    192186
    193187        verify( ((intptr_t)storage & (libAlign() - 1)) == 0ul );
    194188        return [storage, size];
    195 }
    196 
    197 void __stack_clean  ( __stack_info_t * this ) {
    198         size_t size = ((intptr_t)this->storage->base) - ((intptr_t)this->storage->limit) + sizeof(__stack_t);
    199         void * storage = this->storage->limit;
    200 
    201         #if CFA_COROUTINE_USE_MMAP
    202                 storage = (void *)(((intptr_t)storage) - __page_size);
    203                 if(munmap(storage, size + __page_size) == -1) {
    204                         abort( "coroutine stack destruction : internal error, munmap failure, error(%d) %s.", errno, strerror( errno ) );
    205                 }
    206         #else
    207                 __cfaabi_dbg_debug_do(
    208                         storage = (char*)(storage) - __page_size;
    209                         if ( mprotect( storage, __page_size, __map_prot ) == -1 ) {
    210                                 abort( "(coStack_t *)%p.^?{}() : internal error, mprotect failure, error(%d) %s.", &this, errno, strerror( errno ) );
    211                         }
    212                 );
    213 
    214                 free( storage );
    215         #endif
    216         __cfaabi_dbg_print_safe("Kernel : Deleting stack %p\n", storage);
    217189}
    218190
     
    238210        assertf( size >= MinStackSize, "Stack size %zd provides less than minimum of %zd bytes for a stack.", size, MinStackSize );
    239211
    240         this->storage = (__stack_t *)((intptr_t)storage + size - sizeof(__stack_t));
     212        this->storage = (__stack_t *)((intptr_t)storage + size);
    241213        this->storage->limit = storage;
    242         this->storage->base  = (void*)((intptr_t)storage + size - sizeof(__stack_t));
     214        this->storage->base  = (void*)((intptr_t)storage + size);
    243215        this->storage->exception_context.top_resume = 0p;
    244216        this->storage->exception_context.current_exception = 0p;
Note: See TracChangeset for help on using the changeset viewer.