Ignore:
File:
1 edited

Legend:

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

    rbfcf6b9 rbe73f30  
    2828#include "kernel_private.hfa"
    2929#include "exception.hfa"
    30 #include "math.hfa"
    3130
    3231#define __CFA_INVOKE_PRIVATE__
     
    8887
    8988void __stack_prepare( __stack_info_t * this, size_t create_size );
    90 void __stack_clean  ( __stack_info_t * this );
    9189
    9290//-----------------------------------------------------------------------------
     
    109107        bool userStack = ((intptr_t)this.storage & 0x1) != 0;
    110108        if ( ! userStack && this.storage ) {
    111                 __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 );
     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 );
    124121        }
    125122}
     
    170167        assert(__page_size != 0l);
    171168        size_t size = libCeiling( storageSize, 16 ) + stack_data_size;
    172         size = ceiling(size, __page_size);
    173169
    174170        // If we are running debug, we also need to allocate a guardpage to catch stack overflows.
    175171        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);
     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 ) );
     183                }
     184                storage = (void *)(((intptr_t)storage) + __page_size);
     185        );
    198186
    199187        verify( ((intptr_t)storage & (libAlign() - 1)) == 0ul );
    200188        return [storage, size];
    201 }
    202 
    203 void __stack_clean  ( __stack_info_t * this ) {
    204         size_t size = ((intptr_t)this->storage->base) - ((intptr_t)this->storage->limit) + sizeof(__stack_t);
    205         void * storage = this->storage->limit;
    206 
    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         }
    211189}
    212190
     
    232210        assertf( size >= MinStackSize, "Stack size %zd provides less than minimum of %zd bytes for a stack.", size, MinStackSize );
    233211
    234         this->storage = (__stack_t *)((intptr_t)storage + size - sizeof(__stack_t));
     212        this->storage = (__stack_t *)((intptr_t)storage + size);
    235213        this->storage->limit = storage;
    236         this->storage->base  = (void*)((intptr_t)storage + size - sizeof(__stack_t));
     214        this->storage->base  = (void*)((intptr_t)storage + size);
    237215        this->storage->exception_context.top_resume = 0p;
    238216        this->storage->exception_context.current_exception = 0p;
Note: See TracChangeset for help on using the changeset viewer.