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