| 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 : Tue Feb  4 12:29:25 2020 | 
|---|
| 13 | // Update Count     : 16 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #define __cforall_thread__ | 
|---|
| 17 |  | 
|---|
| 18 | #include "coroutine.hfa" | 
|---|
| 19 |  | 
|---|
| 20 | extern "C" { | 
|---|
| 21 | #include <stddef.h> | 
|---|
| 22 | #include <malloc.h> | 
|---|
| 23 | #include <errno.h> | 
|---|
| 24 | #include <string.h> | 
|---|
| 25 | #include <unistd.h> | 
|---|
| 26 | // use this define to make unwind.h play nice, definetely a hack | 
|---|
| 27 | #define HIDE_EXPORTS | 
|---|
| 28 | #include <unwind.h> | 
|---|
| 29 | #undef HIDE_EXPORTS | 
|---|
| 30 | #include <sys/mman.h> | 
|---|
| 31 | } | 
|---|
| 32 |  | 
|---|
| 33 | #include "kernel_private.hfa" | 
|---|
| 34 |  | 
|---|
| 35 | #define __CFA_INVOKE_PRIVATE__ | 
|---|
| 36 | #include "invoke.h" | 
|---|
| 37 |  | 
|---|
| 38 | extern "C" { | 
|---|
| 39 | void _CtxCoroutine_Unwind(struct _Unwind_Exception * storage, struct $coroutine *) __attribute__ ((__noreturn__)); | 
|---|
| 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 | } | 
|---|
| 44 |  | 
|---|
| 45 | extern void CtxRet( struct __stack_context_t * to ) asm ("CtxRet") __attribute__ ((__noreturn__)); | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|
| 48 | //----------------------------------------------------------------------------- | 
|---|
| 49 | // Global state variables | 
|---|
| 50 |  | 
|---|
| 51 | // minimum feasible stack size in bytes | 
|---|
| 52 | #define MinStackSize 1000 | 
|---|
| 53 | extern size_t __page_size;                              // architecture pagesize HACK, should go in proper runtime singleton | 
|---|
| 54 |  | 
|---|
| 55 | void __stack_prepare( __stack_info_t * this, size_t create_size ); | 
|---|
| 56 |  | 
|---|
| 57 | //----------------------------------------------------------------------------- | 
|---|
| 58 | // Coroutine ctors and dtors | 
|---|
| 59 | void ?{}( __stack_info_t & this, void * storage, size_t storageSize ) { | 
|---|
| 60 | this.storage   = (__stack_t *)storage; | 
|---|
| 61 |  | 
|---|
| 62 | // Did we get a piece of storage ? | 
|---|
| 63 | if (this.storage || storageSize != 0) { | 
|---|
| 64 | // We either got a piece of storage or the user asked for a specific size | 
|---|
| 65 | // Immediately create the stack | 
|---|
| 66 | // (This is slightly unintuitive that non-default sized coroutines create are eagerly created | 
|---|
| 67 | // but it avoids that all coroutines carry an unnecessary size) | 
|---|
| 68 | verify( storageSize != 0 ); | 
|---|
| 69 | __stack_prepare( &this, storageSize ); | 
|---|
| 70 | } | 
|---|
| 71 | } | 
|---|
| 72 |  | 
|---|
| 73 | void ^?{}(__stack_info_t & this) { | 
|---|
| 74 | bool userStack = ((intptr_t)this.storage & 0x1) != 0; | 
|---|
| 75 | if ( ! userStack && this.storage ) { | 
|---|
| 76 | __attribute__((may_alias)) intptr_t * istorage = (intptr_t *)&this.storage; | 
|---|
| 77 | *istorage &= (intptr_t)-1; | 
|---|
| 78 |  | 
|---|
| 79 | void * storage = this.storage->limit; | 
|---|
| 80 | __cfaabi_dbg_debug_do( | 
|---|
| 81 | storage = (char*)(storage) - __page_size; | 
|---|
| 82 | if ( mprotect( storage, __page_size, PROT_READ | PROT_WRITE ) == -1 ) { | 
|---|
| 83 | abort( "(coStack_t *)%p.^?{}() : internal error, mprotect failure, error(%d) %s.", &this, errno, strerror( errno ) ); | 
|---|
| 84 | } | 
|---|
| 85 | ); | 
|---|
| 86 | __cfaabi_dbg_print_safe("Kernel : Deleting stack %p\n", storage); | 
|---|
| 87 | free( storage ); | 
|---|
| 88 | } | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | void ?{}( $coroutine & this, const char name[], void * storage, size_t storageSize ) with( this ) { | 
|---|
| 92 | (this.context){0p, 0p}; | 
|---|
| 93 | (this.stack){storage, storageSize}; | 
|---|
| 94 | this.name = name; | 
|---|
| 95 | state = Start; | 
|---|
| 96 | starter = 0p; | 
|---|
| 97 | last = 0p; | 
|---|
| 98 | cancellation = 0p; | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | void ^?{}($coroutine& this) { | 
|---|
| 102 | if(this.state != Halted && this.state != Start && this.state != Primed) { | 
|---|
| 103 | $coroutine * src = TL_GET( this_thread )->curr_cor; | 
|---|
| 104 | $coroutine * dst = &this; | 
|---|
| 105 |  | 
|---|
| 106 | struct _Unwind_Exception storage; | 
|---|
| 107 | storage.exception_class = -1; | 
|---|
| 108 | storage.exception_cleanup = _CtxCoroutine_UnwindCleanup; | 
|---|
| 109 | this.cancellation = &storage; | 
|---|
| 110 | this.last = src; | 
|---|
| 111 |  | 
|---|
| 112 | // not resuming self ? | 
|---|
| 113 | if ( src == dst ) { | 
|---|
| 114 | abort( "Attempt by coroutine %.256s (%p) to terminate itself.\n", src->name, src ); | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | $ctx_switch( src, dst ); | 
|---|
| 118 | } | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | // Part of the Public API | 
|---|
| 122 | // Not inline since only ever called once per coroutine | 
|---|
| 123 | forall(dtype T | is_coroutine(T)) | 
|---|
| 124 | void prime(T& cor) { | 
|---|
| 125 | $coroutine* this = get_coroutine(cor); | 
|---|
| 126 | assert(this->state == Start); | 
|---|
| 127 |  | 
|---|
| 128 | this->state = Primed; | 
|---|
| 129 | resume(cor); | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | [void *, size_t] __stack_alloc( size_t storageSize ) { | 
|---|
| 133 | const size_t stack_data_size = libCeiling( sizeof(__stack_t), 16 ); // minimum alignment | 
|---|
| 134 | assert(__page_size != 0l); | 
|---|
| 135 | size_t size = libCeiling( storageSize, 16 ) + stack_data_size; | 
|---|
| 136 |  | 
|---|
| 137 | // If we are running debug, we also need to allocate a guardpage to catch stack overflows. | 
|---|
| 138 | void * storage; | 
|---|
| 139 | __cfaabi_dbg_debug_do( | 
|---|
| 140 | storage = memalign( __page_size, size + __page_size ); | 
|---|
| 141 | ); | 
|---|
| 142 | __cfaabi_dbg_no_debug_do( | 
|---|
| 143 | storage = (void*)malloc(size); | 
|---|
| 144 | ); | 
|---|
| 145 |  | 
|---|
| 146 | __cfaabi_dbg_print_safe("Kernel : Created stack %p of size %zu\n", storage, size); | 
|---|
| 147 | __cfaabi_dbg_debug_do( | 
|---|
| 148 | if ( mprotect( storage, __page_size, PROT_NONE ) == -1 ) { | 
|---|
| 149 | abort( "__stack_alloc : internal error, mprotect failure, error(%d) %s.", (int)errno, strerror( (int)errno ) ); | 
|---|
| 150 | } | 
|---|
| 151 | storage = (void *)(((intptr_t)storage) + __page_size); | 
|---|
| 152 | ); | 
|---|
| 153 |  | 
|---|
| 154 | verify( ((intptr_t)storage & (libAlign() - 1)) == 0ul ); | 
|---|
| 155 | return [storage, size]; | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | void __stack_prepare( __stack_info_t * this, size_t create_size ) { | 
|---|
| 159 | const size_t stack_data_size = libCeiling( sizeof(__stack_t), 16 ); // minimum alignment | 
|---|
| 160 | bool userStack; | 
|---|
| 161 | void * storage; | 
|---|
| 162 | size_t size; | 
|---|
| 163 | if ( !this->storage ) { | 
|---|
| 164 | userStack = false; | 
|---|
| 165 | [storage, size] = __stack_alloc( create_size ); | 
|---|
| 166 | } else { | 
|---|
| 167 | userStack = true; | 
|---|
| 168 | __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); | 
|---|
| 169 |  | 
|---|
| 170 | // The stack must be aligned, advance the pointer to the next align data | 
|---|
| 171 | storage = (void*)libCeiling( (intptr_t)this->storage, libAlign()); | 
|---|
| 172 |  | 
|---|
| 173 | // The size needs to be shrinked to fit all the extra data structure and be aligned | 
|---|
| 174 | ptrdiff_t diff = (intptr_t)storage - (intptr_t)this->storage; | 
|---|
| 175 | size = libFloor(create_size - stack_data_size - diff, libAlign()); | 
|---|
| 176 | } // if | 
|---|
| 177 | assertf( size >= MinStackSize, "Stack size %zd provides less than minimum of %d bytes for a stack.", size, MinStackSize ); | 
|---|
| 178 |  | 
|---|
| 179 | this->storage = (__stack_t *)((intptr_t)storage + size); | 
|---|
| 180 | this->storage->limit = storage; | 
|---|
| 181 | this->storage->base  = (void*)((intptr_t)storage + size); | 
|---|
| 182 | __attribute__((may_alias)) intptr_t * istorage = (intptr_t*)&this->storage; | 
|---|
| 183 | *istorage |= userStack ? 0x1 : 0x0; | 
|---|
| 184 | } | 
|---|
| 185 |  | 
|---|
| 186 | // We need to call suspend from invoke.c, so we expose this wrapper that | 
|---|
| 187 | // is not inline (We can't inline Cforall in C) | 
|---|
| 188 | extern "C" { | 
|---|
| 189 | void __cfactx_cor_leave( struct $coroutine * src ) { | 
|---|
| 190 | $coroutine * starter = src->cancellation != 0 ? src->last : src->starter; | 
|---|
| 191 |  | 
|---|
| 192 | src->state = Halted; | 
|---|
| 193 |  | 
|---|
| 194 | assertf( starter != 0, | 
|---|
| 195 | "Attempt to suspend/leave coroutine \"%.256s\" (%p) that has never been resumed.\n" | 
|---|
| 196 | "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.", | 
|---|
| 197 | src->name, src ); | 
|---|
| 198 | assertf( starter->state != Halted, | 
|---|
| 199 | "Attempt by coroutine \"%.256s\" (%p) to suspend/leave back to terminated coroutine \"%.256s\" (%p).\n" | 
|---|
| 200 | "Possible cause is terminated coroutine's main routine has already returned.", | 
|---|
| 201 | src->name, src, starter->name, starter ); | 
|---|
| 202 |  | 
|---|
| 203 | $ctx_switch( src, starter ); | 
|---|
| 204 | } | 
|---|
| 205 |  | 
|---|
| 206 | struct $coroutine * __cfactx_cor_finish(void) { | 
|---|
| 207 | struct $coroutine * cor = kernelTLS.this_thread->curr_cor; | 
|---|
| 208 |  | 
|---|
| 209 | if(cor->state == Primed) { | 
|---|
| 210 | __cfactx_suspend(); | 
|---|
| 211 | } | 
|---|
| 212 |  | 
|---|
| 213 | cor->state = Active; | 
|---|
| 214 |  | 
|---|
| 215 | return cor; | 
|---|
| 216 | } | 
|---|
| 217 | } | 
|---|
| 218 |  | 
|---|
| 219 | // Local Variables: // | 
|---|
| 220 | // mode: c // | 
|---|
| 221 | // tab-width: 4 // | 
|---|
| 222 | // End: // | 
|---|