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