| 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 | // | 
|---|
| 7 | // coroutine.c -- | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Thierry Delisle | 
|---|
| 10 | // Created On       : Mon Nov 28 12:27:26 2016 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr | 
|---|
| 12 | // Last Modified On : Fri Oct 23 23:05:24 2020 | 
|---|
| 13 | // Update Count     : 22 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #define __cforall_thread__ | 
|---|
| 17 |  | 
|---|
| 18 | #include "coroutine.hfa" | 
|---|
| 19 |  | 
|---|
| 20 | #include <stddef.h> | 
|---|
| 21 | #include <malloc.h> | 
|---|
| 22 | #include <errno.h> | 
|---|
| 23 | #include <string.h> | 
|---|
| 24 | #include <unistd.h> | 
|---|
| 25 | #include <sys/mman.h>                                                                   // mprotect | 
|---|
| 26 | #include <unwind.h> | 
|---|
| 27 |  | 
|---|
| 28 | #include "kernel_private.hfa" | 
|---|
| 29 | #include "exception.hfa" | 
|---|
| 30 | #include "math.hfa" | 
|---|
| 31 |  | 
|---|
| 32 | #define __CFA_INVOKE_PRIVATE__ | 
|---|
| 33 | #include "invoke.h" | 
|---|
| 34 |  | 
|---|
| 35 | extern "C" { | 
|---|
| 36 | void _CtxCoroutine_Unwind(struct _Unwind_Exception * storage, struct $coroutine *) __attribute__ ((__noreturn__)); | 
|---|
| 37 | static void _CtxCoroutine_UnwindCleanup(_Unwind_Reason_Code, struct _Unwind_Exception *) __attribute__ ((__noreturn__)); | 
|---|
| 38 | static void _CtxCoroutine_UnwindCleanup(_Unwind_Reason_Code, struct _Unwind_Exception *) { | 
|---|
| 39 | abort(); | 
|---|
| 40 | } | 
|---|
| 41 |  | 
|---|
| 42 | extern void CtxRet( struct __stack_context_t * to ) asm ("CtxRet") __attribute__ ((__noreturn__)); | 
|---|
| 43 | } | 
|---|
| 44 |  | 
|---|
| 45 | //----------------------------------------------------------------------------- | 
|---|
| 46 | FORALL_DATA_INSTANCE(CoroutineCancelled, (dtype coroutine_t), (coroutine_t)) | 
|---|
| 47 |  | 
|---|
| 48 | forall(dtype T) | 
|---|
| 49 | void mark_exception(CoroutineCancelled(T) *) {} | 
|---|
| 50 |  | 
|---|
| 51 | forall(dtype T) | 
|---|
| 52 | void copy(CoroutineCancelled(T) * dst, CoroutineCancelled(T) * src) { | 
|---|
| 53 | dst->virtual_table = src->virtual_table; | 
|---|
| 54 | dst->the_coroutine = src->the_coroutine; | 
|---|
| 55 | dst->the_exception = src->the_exception; | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | forall(dtype T) | 
|---|
| 59 | const char * msg(CoroutineCancelled(T) *) { | 
|---|
| 60 | return "CoroutineCancelled(...)"; | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | // This code should not be inlined. It is the error path on resume. | 
|---|
| 64 | forall(dtype T | is_coroutine(T)) | 
|---|
| 65 | void __cfaehm_cancelled_coroutine( T & cor, $coroutine * desc ) { | 
|---|
| 66 | verify( desc->cancellation ); | 
|---|
| 67 | desc->state = Cancelled; | 
|---|
| 68 | exception_t * except = __cfaehm_cancellation_exception( desc->cancellation ); | 
|---|
| 69 |  | 
|---|
| 70 | // TODO: Remove explitate vtable set once trac#186 is fixed. | 
|---|
| 71 | CoroutineCancelled(T) except; | 
|---|
| 72 | except.virtual_table = &get_exception_vtable(&except); | 
|---|
| 73 | except.the_coroutine = &cor; | 
|---|
| 74 | except.the_exception = except; | 
|---|
| 75 | throwResume except; | 
|---|
| 76 |  | 
|---|
| 77 | except->virtual_table->free( except ); | 
|---|
| 78 | free( desc->cancellation ); | 
|---|
| 79 | desc->cancellation = 0p; | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | //----------------------------------------------------------------------------- | 
|---|
| 83 | // Global state variables | 
|---|
| 84 |  | 
|---|
| 85 | // minimum feasible stack size in bytes | 
|---|
| 86 | static const size_t MinStackSize = 1000; | 
|---|
| 87 | extern size_t __page_size;                              // architecture pagesize HACK, should go in proper runtime singleton | 
|---|
| 88 |  | 
|---|
| 89 | void __stack_prepare( __stack_info_t * this, size_t create_size ); | 
|---|
| 90 | void __stack_clean  ( __stack_info_t * this ); | 
|---|
| 91 |  | 
|---|
| 92 | //----------------------------------------------------------------------------- | 
|---|
| 93 | // Coroutine ctors and dtors | 
|---|
| 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 | } | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | void ^?{}(__stack_info_t & this) { | 
|---|
| 109 | bool userStack = ((intptr_t)this.storage & 0x1) != 0; | 
|---|
| 110 | 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 ); | 
|---|
| 124 | } | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | void ?{}( $coroutine & this, const char name[], void * storage, size_t storageSize ) with( this ) { | 
|---|
| 128 | (this.context){0p, 0p}; | 
|---|
| 129 | (this.stack){storage, storageSize}; | 
|---|
| 130 | this.name = name; | 
|---|
| 131 | state = Start; | 
|---|
| 132 | starter = 0p; | 
|---|
| 133 | last = 0p; | 
|---|
| 134 | cancellation = 0p; | 
|---|
| 135 | } | 
|---|
| 136 |  | 
|---|
| 137 | void ^?{}($coroutine& this) { | 
|---|
| 138 | if(this.state != Halted && this.state != Start && this.state != Primed) { | 
|---|
| 139 | $coroutine * src = active_coroutine(); | 
|---|
| 140 | $coroutine * dst = &this; | 
|---|
| 141 |  | 
|---|
| 142 | struct _Unwind_Exception storage; | 
|---|
| 143 | storage.exception_class = -1; | 
|---|
| 144 | storage.exception_cleanup = _CtxCoroutine_UnwindCleanup; | 
|---|
| 145 | this.cancellation = &storage; | 
|---|
| 146 | this.last = src; | 
|---|
| 147 |  | 
|---|
| 148 | // not resuming self ? | 
|---|
| 149 | if ( src == dst ) { | 
|---|
| 150 | abort( "Attempt by coroutine %.256s (%p) to terminate itself.\n", src->name, src ); | 
|---|
| 151 | } | 
|---|
| 152 |  | 
|---|
| 153 | $ctx_switch( src, dst ); | 
|---|
| 154 | } | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 | // Part of the Public API | 
|---|
| 158 | // Not inline since only ever called once per coroutine | 
|---|
| 159 | forall(dtype T | is_coroutine(T)) | 
|---|
| 160 | void prime(T& cor) { | 
|---|
| 161 | $coroutine* this = get_coroutine(cor); | 
|---|
| 162 | assert(this->state == Start); | 
|---|
| 163 |  | 
|---|
| 164 | this->state = Primed; | 
|---|
| 165 | resume(cor); | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 | [void *, size_t] __stack_alloc( size_t storageSize ) { | 
|---|
| 169 | const size_t stack_data_size = libCeiling( sizeof(__stack_t), 16 ); // minimum alignment | 
|---|
| 170 | assert(__page_size != 0l); | 
|---|
| 171 | size_t size = libCeiling( storageSize, 16 ) + stack_data_size; | 
|---|
| 172 | size = ceiling(size, __page_size); | 
|---|
| 173 |  | 
|---|
| 174 | // If we are running debug, we also need to allocate a guardpage to catch stack overflows. | 
|---|
| 175 | 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); | 
|---|
| 198 |  | 
|---|
| 199 | verify( ((intptr_t)storage & (libAlign() - 1)) == 0ul ); | 
|---|
| 200 | 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 | } | 
|---|
| 211 | } | 
|---|
| 212 |  | 
|---|
| 213 | void __stack_prepare( __stack_info_t * this, size_t create_size ) { | 
|---|
| 214 | const size_t stack_data_size = libCeiling( sizeof(__stack_t), 16 ); // minimum alignment | 
|---|
| 215 | bool userStack; | 
|---|
| 216 | void * storage; | 
|---|
| 217 | size_t size; | 
|---|
| 218 | if ( !this->storage ) { | 
|---|
| 219 | userStack = false; | 
|---|
| 220 | [storage, size] = __stack_alloc( create_size ); | 
|---|
| 221 | } else { | 
|---|
| 222 | userStack = true; | 
|---|
| 223 | __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); | 
|---|
| 224 |  | 
|---|
| 225 | // The stack must be aligned, advance the pointer to the next align data | 
|---|
| 226 | storage = (void*)libCeiling( (intptr_t)this->storage, libAlign()); | 
|---|
| 227 |  | 
|---|
| 228 | // The size needs to be shrinked to fit all the extra data structure and be aligned | 
|---|
| 229 | ptrdiff_t diff = (intptr_t)storage - (intptr_t)this->storage; | 
|---|
| 230 | size = libFloor(create_size - stack_data_size - diff, libAlign()); | 
|---|
| 231 | } // if | 
|---|
| 232 | assertf( size >= MinStackSize, "Stack size %zd provides less than minimum of %zd bytes for a stack.", size, MinStackSize ); | 
|---|
| 233 |  | 
|---|
| 234 | this->storage = (__stack_t *)((intptr_t)storage + size - sizeof(__stack_t)); | 
|---|
| 235 | this->storage->limit = storage; | 
|---|
| 236 | this->storage->base  = (void*)((intptr_t)storage + size - sizeof(__stack_t)); | 
|---|
| 237 | this->storage->exception_context.top_resume = 0p; | 
|---|
| 238 | this->storage->exception_context.current_exception = 0p; | 
|---|
| 239 | __attribute__((may_alias)) intptr_t * istorage = (intptr_t*)&this->storage; | 
|---|
| 240 | *istorage |= userStack ? 0x1 : 0x0; | 
|---|
| 241 | } | 
|---|
| 242 |  | 
|---|
| 243 | // We need to call suspend from invoke.c, so we expose this wrapper that | 
|---|
| 244 | // is not inline (We can't inline Cforall in C) | 
|---|
| 245 | extern "C" { | 
|---|
| 246 | void __cfactx_cor_leave( struct $coroutine * src ) { | 
|---|
| 247 | $coroutine * starter = src->cancellation != 0 ? src->last : src->starter; | 
|---|
| 248 |  | 
|---|
| 249 | src->state = Halted; | 
|---|
| 250 |  | 
|---|
| 251 | assertf( starter != 0, | 
|---|
| 252 | "Attempt to suspend/leave coroutine \"%.256s\" (%p) that has never been resumed.\n" | 
|---|
| 253 | "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.", | 
|---|
| 254 | src->name, src ); | 
|---|
| 255 | assertf( starter->state != Halted, | 
|---|
| 256 | "Attempt by coroutine \"%.256s\" (%p) to suspend/leave back to terminated coroutine \"%.256s\" (%p).\n" | 
|---|
| 257 | "Possible cause is terminated coroutine's main routine has already returned.", | 
|---|
| 258 | src->name, src, starter->name, starter ); | 
|---|
| 259 |  | 
|---|
| 260 | $ctx_switch( src, starter ); | 
|---|
| 261 | } | 
|---|
| 262 |  | 
|---|
| 263 | struct $coroutine * __cfactx_cor_finish(void) { | 
|---|
| 264 | struct $coroutine * cor = active_coroutine(); | 
|---|
| 265 |  | 
|---|
| 266 | if(cor->state == Primed) { | 
|---|
| 267 | __cfactx_suspend(); | 
|---|
| 268 | } | 
|---|
| 269 |  | 
|---|
| 270 | cor->state = Active; | 
|---|
| 271 |  | 
|---|
| 272 | return cor; | 
|---|
| 273 | } | 
|---|
| 274 | } | 
|---|
| 275 |  | 
|---|
| 276 | // Local Variables: // | 
|---|
| 277 | // mode: c // | 
|---|
| 278 | // tab-width: 4 // | 
|---|
| 279 | // End: // | 
|---|