| [6a3d2e7] | 1 | // | 
|---|
|  | 2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo | 
|---|
|  | 3 | // | 
|---|
|  | 4 | // The contents of this file are covered under the licence agreement in the | 
|---|
|  | 5 | // file "LICENCE" distributed with Cforall. | 
|---|
|  | 6 | // | 
|---|
| [75a17f1] | 7 | // coroutine.c -- | 
|---|
| [6a3d2e7] | 8 | // | 
|---|
|  | 9 | // Author           : Thierry Delisle | 
|---|
|  | 10 | // Created On       : Mon Nov 28 12:27:26 2016 | 
|---|
| [6b0b624] | 11 | // Last Modified By : Peter A. Buhr | 
|---|
| [dd92fe9] | 12 | // Last Modified On : Tue Dec 15 12:06:04 2020 | 
|---|
|  | 13 | // Update Count     : 23 | 
|---|
| [6a3d2e7] | 14 | // | 
|---|
|  | 15 |  | 
|---|
| [2026bb6] | 16 | #define __cforall_thread__ | 
|---|
|  | 17 |  | 
|---|
| [58b6d1b] | 18 | #include "coroutine.hfa" | 
|---|
| [bd98b58] | 19 |  | 
|---|
| [6a3d2e7] | 20 | #include <stddef.h> | 
|---|
|  | 21 | #include <malloc.h> | 
|---|
|  | 22 | #include <errno.h> | 
|---|
|  | 23 | #include <string.h> | 
|---|
|  | 24 | #include <unistd.h> | 
|---|
| [ada0246d] | 25 | #include <sys/mman.h>                                                                   // mprotect | 
|---|
| [76e069f] | 26 | #include <unwind.h> | 
|---|
| [6a3d2e7] | 27 |  | 
|---|
| [73abe95] | 28 | #include "kernel_private.hfa" | 
|---|
| [c960331] | 29 | #include "exception.hfa" | 
|---|
| [bfcf6b9] | 30 | #include "math.hfa" | 
|---|
| [6a3d2e7] | 31 |  | 
|---|
| [97229d6] | 32 | #define CFA_COROUTINE_USE_MMAP 0 | 
|---|
|  | 33 |  | 
|---|
| [6a3d2e7] | 34 | #define __CFA_INVOKE_PRIVATE__ | 
|---|
|  | 35 | #include "invoke.h" | 
|---|
|  | 36 |  | 
|---|
| [76e069f] | 37 | extern "C" { | 
|---|
| [ac2b598] | 38 | void _CtxCoroutine_Unwind(struct _Unwind_Exception * storage, struct $coroutine *) __attribute__ ((__noreturn__)); | 
|---|
| [b2f6113] | 39 | static void _CtxCoroutine_UnwindCleanup(_Unwind_Reason_Code, struct _Unwind_Exception *) __attribute__ ((__noreturn__)); | 
|---|
|  | 40 | static void _CtxCoroutine_UnwindCleanup(_Unwind_Reason_Code, struct _Unwind_Exception *) { | 
|---|
|  | 41 | abort(); | 
|---|
|  | 42 | } | 
|---|
| [f019069] | 43 |  | 
|---|
|  | 44 | extern void CtxRet( struct __stack_context_t * to ) asm ("CtxRet") __attribute__ ((__noreturn__)); | 
|---|
| [76e069f] | 45 | } | 
|---|
|  | 46 |  | 
|---|
| [1c01c58] | 47 | //----------------------------------------------------------------------------- | 
|---|
| [fd54fef] | 48 | forall(T &) | 
|---|
| [1c01c58] | 49 | void copy(CoroutineCancelled(T) * dst, CoroutineCancelled(T) * src) { | 
|---|
| [342be43] | 50 | dst->virtual_table = src->virtual_table; | 
|---|
| [1c01c58] | 51 | dst->the_coroutine = src->the_coroutine; | 
|---|
|  | 52 | dst->the_exception = src->the_exception; | 
|---|
|  | 53 | } | 
|---|
|  | 54 |  | 
|---|
| [fd54fef] | 55 | forall(T &) | 
|---|
| [1c01c58] | 56 | const char * msg(CoroutineCancelled(T) *) { | 
|---|
|  | 57 | return "CoroutineCancelled(...)"; | 
|---|
|  | 58 | } | 
|---|
|  | 59 |  | 
|---|
|  | 60 | // This code should not be inlined. It is the error path on resume. | 
|---|
| [fd54fef] | 61 | forall(T & | is_coroutine(T)) | 
|---|
| [b583113] | 62 | void __cfaehm_cancelled_coroutine( | 
|---|
| [5456537] | 63 | T & cor, $coroutine * desc, EHM_DEFAULT_VTABLE(CoroutineCancelled, (T)) ) { | 
|---|
| [1c01c58] | 64 | verify( desc->cancellation ); | 
|---|
|  | 65 | desc->state = Cancelled; | 
|---|
| [342be43] | 66 | exception_t * except = __cfaehm_cancellation_exception( desc->cancellation ); | 
|---|
| [1c01c58] | 67 |  | 
|---|
| [69c5c00] | 68 | // TODO: Remove explitate vtable set once trac#186 is fixed. | 
|---|
| [b583113] | 69 | CoroutineCancelled(T) except; | 
|---|
|  | 70 | except.virtual_table = &_default_vtable; | 
|---|
| [1c01c58] | 71 | except.the_coroutine = &cor; | 
|---|
|  | 72 | except.the_exception = except; | 
|---|
| [ecfd758] | 73 | // Why does this need a cast? | 
|---|
| [b583113] | 74 | throwResume (CoroutineCancelled(T) &)except; | 
|---|
| [1c01c58] | 75 |  | 
|---|
|  | 76 | except->virtual_table->free( except ); | 
|---|
|  | 77 | free( desc->cancellation ); | 
|---|
|  | 78 | desc->cancellation = 0p; | 
|---|
|  | 79 | } | 
|---|
|  | 80 |  | 
|---|
| [6a3d2e7] | 81 | //----------------------------------------------------------------------------- | 
|---|
|  | 82 | // Global state variables | 
|---|
|  | 83 |  | 
|---|
|  | 84 | // minimum feasible stack size in bytes | 
|---|
| [290553a] | 85 | static const size_t MinStackSize = 1000; | 
|---|
| [b2f6113] | 86 | extern size_t __page_size;                              // architecture pagesize HACK, should go in proper runtime singleton | 
|---|
| [dd92fe9] | 87 | extern int __map_prot; | 
|---|
| [b2f6113] | 88 |  | 
|---|
|  | 89 | void __stack_prepare( __stack_info_t * this, size_t create_size ); | 
|---|
| [bfcf6b9] | 90 | void __stack_clean  ( __stack_info_t * this ); | 
|---|
| [6a3d2e7] | 91 |  | 
|---|
|  | 92 | //----------------------------------------------------------------------------- | 
|---|
|  | 93 | // Coroutine ctors and dtors | 
|---|
| [b2f6113] | 94 | void ?{}( __stack_info_t & this, void * storage, size_t storageSize ) { | 
|---|
|  | 95 | this.storage   = (__stack_t *)storage; | 
|---|
|  | 96 |  | 
|---|
|  | 97 | // Did we get a piece of storage ? | 
|---|
|  | 98 | if (this.storage || storageSize != 0) { | 
|---|
|  | 99 | // We either got a piece of storage or the user asked for a specific size | 
|---|
|  | 100 | // Immediately create the stack | 
|---|
|  | 101 | // (This is slightly unintuitive that non-default sized coroutines create are eagerly created | 
|---|
|  | 102 | // but it avoids that all coroutines carry an unnecessary size) | 
|---|
|  | 103 | verify( storageSize != 0 ); | 
|---|
|  | 104 | __stack_prepare( &this, storageSize ); | 
|---|
|  | 105 | } | 
|---|
| [6a3d2e7] | 106 | } | 
|---|
|  | 107 |  | 
|---|
| [b2f6113] | 108 | void ^?{}(__stack_info_t & this) { | 
|---|
| [8c01e1b] | 109 | bool userStack = ((intptr_t)this.storage & 0x1) != 0; | 
|---|
|  | 110 | if ( ! userStack && this.storage ) { | 
|---|
| [bfcf6b9] | 111 | __stack_clean( &this ); | 
|---|
| [b2f6113] | 112 | } | 
|---|
| [6a3d2e7] | 113 | } | 
|---|
|  | 114 |  | 
|---|
| [ac2b598] | 115 | void ?{}( $coroutine & this, const char name[], void * storage, size_t storageSize ) with( this ) { | 
|---|
| [524627e] | 116 | (this.context){0p, 0p}; | 
|---|
| [b2f6113] | 117 | (this.stack){storage, storageSize}; | 
|---|
|  | 118 | this.name = name; | 
|---|
|  | 119 | state = Start; | 
|---|
| [524627e] | 120 | starter = 0p; | 
|---|
|  | 121 | last = 0p; | 
|---|
|  | 122 | cancellation = 0p; | 
|---|
| [6a3d2e7] | 123 | } | 
|---|
|  | 124 |  | 
|---|
| [ac2b598] | 125 | void ^?{}($coroutine& this) { | 
|---|
| [3623f9d] | 126 | if(this.state != Halted && this.state != Start && this.state != Primed) { | 
|---|
| [be73f30] | 127 | $coroutine * src = active_coroutine(); | 
|---|
| [ac2b598] | 128 | $coroutine * dst = &this; | 
|---|
| [b2f6113] | 129 |  | 
|---|
|  | 130 | struct _Unwind_Exception storage; | 
|---|
|  | 131 | storage.exception_class = -1; | 
|---|
|  | 132 | storage.exception_cleanup = _CtxCoroutine_UnwindCleanup; | 
|---|
|  | 133 | this.cancellation = &storage; | 
|---|
|  | 134 | this.last = src; | 
|---|
|  | 135 |  | 
|---|
|  | 136 | // not resuming self ? | 
|---|
|  | 137 | if ( src == dst ) { | 
|---|
|  | 138 | abort( "Attempt by coroutine %.256s (%p) to terminate itself.\n", src->name, src ); | 
|---|
|  | 139 | } | 
|---|
|  | 140 |  | 
|---|
| [ac2b598] | 141 | $ctx_switch( src, dst ); | 
|---|
| [b2f6113] | 142 | } | 
|---|
| [76e069f] | 143 | } | 
|---|
| [6a3d2e7] | 144 |  | 
|---|
|  | 145 | // Part of the Public API | 
|---|
|  | 146 | // Not inline since only ever called once per coroutine | 
|---|
| [5456537] | 147 | forall(T & | is_coroutine(T) | { EHM_DEFAULT_VTABLE(CoroutineCancelled, (T)); }) | 
|---|
| [83a071f9] | 148 | void prime(T& cor) { | 
|---|
| [ac2b598] | 149 | $coroutine* this = get_coroutine(cor); | 
|---|
| [b2f6113] | 150 | assert(this->state == Start); | 
|---|
| [6a3d2e7] | 151 |  | 
|---|
| [b2f6113] | 152 | this->state = Primed; | 
|---|
|  | 153 | resume(cor); | 
|---|
| [6a3d2e7] | 154 | } | 
|---|
|  | 155 |  | 
|---|
| [b2f6113] | 156 | [void *, size_t] __stack_alloc( size_t storageSize ) { | 
|---|
| [0030ada3] | 157 | const size_t stack_data_size = libCeiling( sizeof(__stack_t), 16 ); // minimum alignment | 
|---|
| [b2f6113] | 158 | assert(__page_size != 0l); | 
|---|
|  | 159 | size_t size = libCeiling( storageSize, 16 ) + stack_data_size; | 
|---|
| [bfcf6b9] | 160 | size = ceiling(size, __page_size); | 
|---|
| [b2f6113] | 161 |  | 
|---|
|  | 162 | // If we are running debug, we also need to allocate a guardpage to catch stack overflows. | 
|---|
|  | 163 | void * storage; | 
|---|
| [97229d6] | 164 | #if CFA_COROUTINE_USE_MMAP | 
|---|
|  | 165 | storage = mmap(0p, size + __page_size, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); | 
|---|
|  | 166 | if(storage == ((void*)-1)) { | 
|---|
|  | 167 | abort( "coroutine stack creation : internal error, mmap failure, error(%d) %s.", errno, strerror( errno ) ); | 
|---|
|  | 168 | } | 
|---|
|  | 169 | if ( mprotect( storage, __page_size, PROT_NONE ) == -1 ) { | 
|---|
|  | 170 | abort( "coroutine stack creation : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) ); | 
|---|
|  | 171 | } // if | 
|---|
|  | 172 | storage = (void *)(((intptr_t)storage) + __page_size); | 
|---|
|  | 173 | #else | 
|---|
|  | 174 | __cfaabi_dbg_debug_do( | 
|---|
|  | 175 | storage = memalign( __page_size, size + __page_size ); | 
|---|
|  | 176 | ); | 
|---|
|  | 177 | __cfaabi_dbg_no_debug_do( | 
|---|
|  | 178 | storage = (void*)malloc(size); | 
|---|
|  | 179 | ); | 
|---|
|  | 180 |  | 
|---|
|  | 181 | __cfaabi_dbg_debug_do( | 
|---|
|  | 182 | if ( mprotect( storage, __page_size, PROT_NONE ) == -1 ) { | 
|---|
|  | 183 | abort( "__stack_alloc : internal error, mprotect failure, error(%d) %s.", (int)errno, strerror( (int)errno ) ); | 
|---|
|  | 184 | } | 
|---|
|  | 185 | storage = (void *)(((intptr_t)storage) + __page_size); | 
|---|
|  | 186 | ); | 
|---|
|  | 187 | #endif | 
|---|
|  | 188 | __cfaabi_dbg_print_safe("Kernel : Created stack %p of size %zu\n", storage, size); | 
|---|
| [b2f6113] | 189 |  | 
|---|
|  | 190 | verify( ((intptr_t)storage & (libAlign() - 1)) == 0ul ); | 
|---|
|  | 191 | return [storage, size]; | 
|---|
|  | 192 | } | 
|---|
| [6a3d2e7] | 193 |  | 
|---|
| [bfcf6b9] | 194 | void __stack_clean  ( __stack_info_t * this ) { | 
|---|
|  | 195 | void * storage = this->storage->limit; | 
|---|
|  | 196 |  | 
|---|
| [97229d6] | 197 | #if CFA_COROUTINE_USE_MMAP | 
|---|
| [85ac70e8] | 198 | size_t size = ((intptr_t)this->storage->base) - ((intptr_t)this->storage->limit) + sizeof(__stack_t); | 
|---|
| [97229d6] | 199 | storage = (void *)(((intptr_t)storage) - __page_size); | 
|---|
|  | 200 | if(munmap(storage, size + __page_size) == -1) { | 
|---|
|  | 201 | abort( "coroutine stack destruction : internal error, munmap failure, error(%d) %s.", errno, strerror( errno ) ); | 
|---|
|  | 202 | } | 
|---|
|  | 203 | #else | 
|---|
|  | 204 | __cfaabi_dbg_debug_do( | 
|---|
|  | 205 | storage = (char*)(storage) - __page_size; | 
|---|
| [dd92fe9] | 206 | if ( mprotect( storage, __page_size, __map_prot ) == -1 ) { | 
|---|
| [97229d6] | 207 | abort( "(coStack_t *)%p.^?{}() : internal error, mprotect failure, error(%d) %s.", &this, errno, strerror( errno ) ); | 
|---|
|  | 208 | } | 
|---|
|  | 209 | ); | 
|---|
|  | 210 |  | 
|---|
|  | 211 | free( storage ); | 
|---|
|  | 212 | #endif | 
|---|
|  | 213 | __cfaabi_dbg_print_safe("Kernel : Deleting stack %p\n", storage); | 
|---|
| [bfcf6b9] | 214 | } | 
|---|
|  | 215 |  | 
|---|
| [b2f6113] | 216 | void __stack_prepare( __stack_info_t * this, size_t create_size ) { | 
|---|
| [0030ada3] | 217 | const size_t stack_data_size = libCeiling( sizeof(__stack_t), 16 ); // minimum alignment | 
|---|
| [b2f6113] | 218 | bool userStack; | 
|---|
|  | 219 | void * storage; | 
|---|
|  | 220 | size_t size; | 
|---|
|  | 221 | if ( !this->storage ) { | 
|---|
|  | 222 | userStack = false; | 
|---|
|  | 223 | [storage, size] = __stack_alloc( create_size ); | 
|---|
|  | 224 | } else { | 
|---|
|  | 225 | userStack = true; | 
|---|
| [69a61d2] | 226 | __cfaabi_dbg_print_safe("Kernel : stack obj %p using user stack %p(%zd bytes)\n", this, this->storage, (intptr_t)this->storage->limit - (intptr_t)this->storage->base); | 
|---|
| [b2f6113] | 227 |  | 
|---|
|  | 228 | // The stack must be aligned, advance the pointer to the next align data | 
|---|
|  | 229 | storage = (void*)libCeiling( (intptr_t)this->storage, libAlign()); | 
|---|
|  | 230 |  | 
|---|
|  | 231 | // The size needs to be shrinked to fit all the extra data structure and be aligned | 
|---|
|  | 232 | ptrdiff_t diff = (intptr_t)storage - (intptr_t)this->storage; | 
|---|
|  | 233 | size = libFloor(create_size - stack_data_size - diff, libAlign()); | 
|---|
|  | 234 | } // if | 
|---|
| [9b0c3ec5] | 235 | assertf( size >= MinStackSize, "Stack size %zd provides less than minimum of %zd bytes for a stack.", size, MinStackSize ); | 
|---|
| [b2f6113] | 236 |  | 
|---|
| [bfcf6b9] | 237 | this->storage = (__stack_t *)((intptr_t)storage + size - sizeof(__stack_t)); | 
|---|
| [b2f6113] | 238 | this->storage->limit = storage; | 
|---|
| [bfcf6b9] | 239 | this->storage->base  = (void*)((intptr_t)storage + size - sizeof(__stack_t)); | 
|---|
| [1c01c58] | 240 | this->storage->exception_context.top_resume = 0p; | 
|---|
|  | 241 | this->storage->exception_context.current_exception = 0p; | 
|---|
| [ffe2fad] | 242 | __attribute__((may_alias)) intptr_t * istorage = (intptr_t*)&this->storage; | 
|---|
|  | 243 | *istorage |= userStack ? 0x1 : 0x0; | 
|---|
| [6a3d2e7] | 244 | } | 
|---|
|  | 245 |  | 
|---|
| [0c92c9f] | 246 | // We need to call suspend from invoke.c, so we expose this wrapper that | 
|---|
|  | 247 | // is not inline (We can't inline Cforall in C) | 
|---|
|  | 248 | extern "C" { | 
|---|
| [ac2b598] | 249 | void __cfactx_cor_leave( struct $coroutine * src ) { | 
|---|
|  | 250 | $coroutine * starter = src->cancellation != 0 ? src->last : src->starter; | 
|---|
| [b2f6113] | 251 |  | 
|---|
|  | 252 | src->state = Halted; | 
|---|
|  | 253 |  | 
|---|
|  | 254 | assertf( starter != 0, | 
|---|
|  | 255 | "Attempt to suspend/leave coroutine \"%.256s\" (%p) that has never been resumed.\n" | 
|---|
|  | 256 | "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.", | 
|---|
|  | 257 | src->name, src ); | 
|---|
|  | 258 | assertf( starter->state != Halted, | 
|---|
|  | 259 | "Attempt by coroutine \"%.256s\" (%p) to suspend/leave back to terminated coroutine \"%.256s\" (%p).\n" | 
|---|
|  | 260 | "Possible cause is terminated coroutine's main routine has already returned.", | 
|---|
|  | 261 | src->name, src, starter->name, starter ); | 
|---|
|  | 262 |  | 
|---|
| [ac2b598] | 263 | $ctx_switch( src, starter ); | 
|---|
| [b2f6113] | 264 | } | 
|---|
| [09f357ec] | 265 |  | 
|---|
| [ac2b598] | 266 | struct $coroutine * __cfactx_cor_finish(void) { | 
|---|
| [be73f30] | 267 | struct $coroutine * cor = active_coroutine(); | 
|---|
| [09f357ec] | 268 |  | 
|---|
|  | 269 | if(cor->state == Primed) { | 
|---|
| [427854b] | 270 | __cfactx_suspend(); | 
|---|
| [09f357ec] | 271 | } | 
|---|
|  | 272 |  | 
|---|
|  | 273 | cor->state = Active; | 
|---|
|  | 274 |  | 
|---|
|  | 275 | return cor; | 
|---|
|  | 276 | } | 
|---|
| [0c92c9f] | 277 | } | 
|---|
|  | 278 |  | 
|---|
| [6a3d2e7] | 279 | // Local Variables: // | 
|---|
|  | 280 | // mode: c // | 
|---|
|  | 281 | // tab-width: 4 // | 
|---|
| [4aa2fb2] | 282 | // End: // | 
|---|