- File:
-
- 1 edited
-
libcfa/src/concurrency/coroutine.cfa (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/concurrency/coroutine.cfa
rdd92fe9 rbe73f30 10 10 // Created On : Mon Nov 28 12:27:26 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue Dec 15 12:06:04 202013 // Update Count : 2 312 // Last Modified On : Fri Oct 23 23:05:24 2020 13 // Update Count : 22 14 14 // 15 15 … … 28 28 #include "kernel_private.hfa" 29 29 #include "exception.hfa" 30 #include "math.hfa"31 32 #define CFA_COROUTINE_USE_MMAP 033 30 34 31 #define __CFA_INVOKE_PRIVATE__ … … 88 85 static const size_t MinStackSize = 1000; 89 86 extern size_t __page_size; // architecture pagesize HACK, should go in proper runtime singleton 90 extern int __map_prot;91 87 92 88 void __stack_prepare( __stack_info_t * this, size_t create_size ); 93 void __stack_clean ( __stack_info_t * this );94 89 95 90 //----------------------------------------------------------------------------- … … 112 107 bool userStack = ((intptr_t)this.storage & 0x1) != 0; 113 108 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 ); 115 121 } 116 122 } … … 161 167 assert(__page_size != 0l); 162 168 size_t size = libCeiling( storageSize, 16 ) + stack_data_size; 163 size = ceiling(size, __page_size);164 169 165 170 // If we are running debug, we also need to allocate a guardpage to catch stack overflows. 166 171 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 ) ); 171 183 } 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 } // if175 184 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 ); 192 186 193 187 verify( ((intptr_t)storage & (libAlign() - 1)) == 0ul ); 194 188 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_MMAP202 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 #else207 __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 #endif216 __cfaabi_dbg_print_safe("Kernel : Deleting stack %p\n", storage);217 189 } 218 190 … … 238 210 assertf( size >= MinStackSize, "Stack size %zd provides less than minimum of %zd bytes for a stack.", size, MinStackSize ); 239 211 240 this->storage = (__stack_t *)((intptr_t)storage + size - sizeof(__stack_t));212 this->storage = (__stack_t *)((intptr_t)storage + size); 241 213 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); 243 215 this->storage->exception_context.top_resume = 0p; 244 216 this->storage->exception_context.current_exception = 0p;
Note:
See TracChangeset
for help on using the changeset viewer.