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