| 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"
 | 
|---|
| 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 | #include <sys/mman.h>
 | 
|---|
| 25 | }
 | 
|---|
| 26 | 
 | 
|---|
| 27 | #include "kernel_private.h"
 | 
|---|
| 28 | 
 | 
|---|
| 29 | #define __CFA_INVOKE_PRIVATE__
 | 
|---|
| 30 | #include "invoke.h"
 | 
|---|
| 31 | 
 | 
|---|
| 32 | //-----------------------------------------------------------------------------
 | 
|---|
| 33 | // Global state variables
 | 
|---|
| 34 | 
 | 
|---|
| 35 | // minimum feasible stack size in bytes
 | 
|---|
| 36 | #define MinStackSize 1000
 | 
|---|
| 37 | static size_t pageSize = 0;                             // architecture pagesize HACK, should go in proper runtime singleton
 | 
|---|
| 38 | 
 | 
|---|
| 39 | //-----------------------------------------------------------------------------
 | 
|---|
| 40 | // Coroutine ctors and dtors
 | 
|---|
| 41 | void ?{}( coStack_t & this, void * storage, size_t storageSize ) with( this ) {
 | 
|---|
| 42 |       size               = storageSize == 0 ? 65000 : storageSize; // size of stack
 | 
|---|
| 43 |       this.storage = storage;                                // pointer to stack
 | 
|---|
| 44 |       limit              = NULL;                                   // stack grows towards stack limit
 | 
|---|
| 45 |       base               = NULL;                                   // base of stack
 | 
|---|
| 46 |       context    = NULL;                                   // address of cfa_context_t
 | 
|---|
| 47 |       top                = NULL;                                   // address of top of storage
 | 
|---|
| 48 |       userStack  = storage != NULL;
 | 
|---|
| 49 | }
 | 
|---|
| 50 | 
 | 
|---|
| 51 | void ^?{}(coStack_t & this) {
 | 
|---|
| 52 |       if ( ! this.userStack && this.storage ) {
 | 
|---|
| 53 |             __cfaabi_dbg_debug_do(
 | 
|---|
| 54 |                   if ( mprotect( this.storage, pageSize, PROT_READ | PROT_WRITE ) == -1 ) {
 | 
|---|
| 55 |                         abort( "(coStack_t *)%p.^?{}() : internal error, mprotect failure, error(%d) %s.", &this, errno, strerror( errno ) );
 | 
|---|
| 56 |                   }
 | 
|---|
| 57 |             );
 | 
|---|
| 58 |             free( this.storage );
 | 
|---|
| 59 |       }
 | 
|---|
| 60 | }
 | 
|---|
| 61 | 
 | 
|---|
| 62 | void ?{}( coroutine_desc & this, const char * name, void * storage, size_t storageSize ) with( this ) {
 | 
|---|
| 63 |       (this.stack){storage, storageSize};
 | 
|---|
| 64 |       this.name = name;
 | 
|---|
| 65 |       errno_ = 0;
 | 
|---|
| 66 |       state = Start;
 | 
|---|
| 67 |       starter = NULL;
 | 
|---|
| 68 |       last = NULL;
 | 
|---|
| 69 | }
 | 
|---|
| 70 | 
 | 
|---|
| 71 | void ^?{}(coroutine_desc& this) {}
 | 
|---|
| 72 | 
 | 
|---|
| 73 | // Part of the Public API
 | 
|---|
| 74 | // Not inline since only ever called once per coroutine
 | 
|---|
| 75 | forall(dtype T | is_coroutine(T))
 | 
|---|
| 76 | void prime(T& cor) {
 | 
|---|
| 77 |       coroutine_desc* this = get_coroutine(cor);
 | 
|---|
| 78 |       assert(this->state == Start);
 | 
|---|
| 79 | 
 | 
|---|
| 80 |       this->state = Primed;
 | 
|---|
| 81 |       resume(cor);
 | 
|---|
| 82 | }
 | 
|---|
| 83 | 
 | 
|---|
| 84 | // Wrapper for co
 | 
|---|
| 85 | void CoroutineCtxSwitch(coroutine_desc* src, coroutine_desc* dst) {
 | 
|---|
| 86 |       verify( TL_GET( preemption_state ).enabled || TL_GET( this_processor )->do_terminate );
 | 
|---|
| 87 |       disable_interrupts();
 | 
|---|
| 88 | 
 | 
|---|
| 89 |       // set state of current coroutine to inactive
 | 
|---|
| 90 |       src->state = src->state == Halted ? Halted : Inactive;
 | 
|---|
| 91 | 
 | 
|---|
| 92 |       // set new coroutine that task is executing
 | 
|---|
| 93 |       TL_SET( this_coroutine, dst );
 | 
|---|
| 94 | 
 | 
|---|
| 95 |       // context switch to specified coroutine
 | 
|---|
| 96 |       assert( src->stack.context );
 | 
|---|
| 97 |       CtxSwitch( src->stack.context, dst->stack.context );
 | 
|---|
| 98 |       // when CtxSwitch returns we are back in the src coroutine
 | 
|---|
| 99 | 
 | 
|---|
| 100 |       // set state of new coroutine to active
 | 
|---|
| 101 |       src->state = Active;
 | 
|---|
| 102 | 
 | 
|---|
| 103 |       enable_interrupts( __cfaabi_dbg_ctx );
 | 
|---|
| 104 |       verify( TL_GET( preemption_state ).enabled || TL_GET( this_processor )->do_terminate );
 | 
|---|
| 105 | } //ctxSwitchDirect
 | 
|---|
| 106 | 
 | 
|---|
| 107 | void create_stack( coStack_t* this, unsigned int storageSize ) with( *this ) {
 | 
|---|
| 108 |       //TEMP HACK do this on proper kernel startup
 | 
|---|
| 109 |       if(pageSize == 0ul) pageSize = sysconf( _SC_PAGESIZE );
 | 
|---|
| 110 | 
 | 
|---|
| 111 |       size_t cxtSize = libCeiling( sizeof(machine_context_t), 8 ); // minimum alignment
 | 
|---|
| 112 | 
 | 
|---|
| 113 |       if ( !storage ) {
 | 
|---|
| 114 |             __cfaabi_dbg_print_safe("Kernel : Creating stack of size %zu for stack obj %p\n", cxtSize + size + 8, this);
 | 
|---|
| 115 | 
 | 
|---|
| 116 |             userStack = false;
 | 
|---|
| 117 |             size = libCeiling( storageSize, 16 );
 | 
|---|
| 118 |             // use malloc/memalign because "new" raises an exception for out-of-memory
 | 
|---|
| 119 | 
 | 
|---|
| 120 |             // assume malloc has 8 byte alignment so add 8 to allow rounding up to 16 byte alignment
 | 
|---|
| 121 |             __cfaabi_dbg_debug_do( storage = memalign( pageSize, cxtSize + size + pageSize ) );
 | 
|---|
| 122 |             __cfaabi_dbg_no_debug_do( storage = malloc( cxtSize + size + 8 ) );
 | 
|---|
| 123 | 
 | 
|---|
| 124 |             __cfaabi_dbg_debug_do(
 | 
|---|
| 125 |                   if ( mprotect( storage, pageSize, PROT_NONE ) == -1 ) {
 | 
|---|
| 126 |                         abort( "(uMachContext &)%p.createContext() : internal error, mprotect failure, error(%d) %s.", this, (int)errno, strerror( (int)errno ) );
 | 
|---|
| 127 |                   } // if
 | 
|---|
| 128 |             );
 | 
|---|
| 129 | 
 | 
|---|
| 130 |             if ( (intptr_t)storage == 0 ) {
 | 
|---|
| 131 |                   abort( "Attempt to allocate %zd bytes of storage for coroutine or task execution-state but insufficient memory available.", size );
 | 
|---|
| 132 |             } // if
 | 
|---|
| 133 | 
 | 
|---|
| 134 |             __cfaabi_dbg_debug_do( limit = (char *)storage + pageSize );
 | 
|---|
| 135 |             __cfaabi_dbg_no_debug_do( limit = (char *)libCeiling( (unsigned long)storage, 16 ) ); // minimum alignment
 | 
|---|
| 136 | 
 | 
|---|
| 137 |       } else {
 | 
|---|
| 138 |             __cfaabi_dbg_print_safe("Kernel : stack obj %p using user stack %p(%u bytes)\n", this, storage, storageSize);
 | 
|---|
| 139 | 
 | 
|---|
| 140 |             assertf( ((size_t)storage & (libAlign() - 1)) == 0ul, "Stack storage %p for task/coroutine must be aligned on %d byte boundary.", storage, (int)libAlign() );
 | 
|---|
| 141 |             userStack = true;
 | 
|---|
| 142 |             size = storageSize - cxtSize;
 | 
|---|
| 143 | 
 | 
|---|
| 144 |             if ( size % 16 != 0u ) size -= 8;
 | 
|---|
| 145 | 
 | 
|---|
| 146 |             limit = (char *)libCeiling( (unsigned long)storage, 16 ); // minimum alignment
 | 
|---|
| 147 |       } // if
 | 
|---|
| 148 |       assertf( size >= MinStackSize, "Stack size %zd provides less than minimum of %d bytes for a stack.", size, MinStackSize );
 | 
|---|
| 149 | 
 | 
|---|
| 150 |       base = (char *)limit + size;
 | 
|---|
| 151 |       context = base;
 | 
|---|
| 152 |       top = (char *)context + cxtSize;
 | 
|---|
| 153 | }
 | 
|---|
| 154 | 
 | 
|---|
| 155 | // We need to call suspend from invoke.c, so we expose this wrapper that
 | 
|---|
| 156 | // is not inline (We can't inline Cforall in C)
 | 
|---|
| 157 | extern "C" {
 | 
|---|
| 158 |       void __suspend_internal(void) {
 | 
|---|
| 159 |             suspend();
 | 
|---|
| 160 |       }
 | 
|---|
| 161 | 
 | 
|---|
| 162 |       void __leave_coroutine(void) {
 | 
|---|
| 163 |             coroutine_desc * src = TL_GET( this_coroutine ); // optimization
 | 
|---|
| 164 | 
 | 
|---|
| 165 |             assertf( src->starter != 0,
 | 
|---|
| 166 |                   "Attempt to suspend/leave coroutine \"%.256s\" (%p) that has never been resumed.\n"
 | 
|---|
| 167 |                   "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.",
 | 
|---|
| 168 |                   src->name, src );
 | 
|---|
| 169 |             assertf( src->starter->state != Halted,
 | 
|---|
| 170 |                   "Attempt by coroutine \"%.256s\" (%p) to suspend/leave back to terminated coroutine \"%.256s\" (%p).\n"
 | 
|---|
| 171 |                   "Possible cause is terminated coroutine's main routine has already returned.",
 | 
|---|
| 172 |                   src->name, src, src->starter->name, src->starter );
 | 
|---|
| 173 | 
 | 
|---|
| 174 |             CoroutineCtxSwitch( src, src->starter );
 | 
|---|
| 175 |       }
 | 
|---|
| 176 | }
 | 
|---|
| 177 | 
 | 
|---|
| 178 | // Local Variables: //
 | 
|---|
| 179 | // mode: c //
 | 
|---|
| 180 | // tab-width: 4 //
 | 
|---|
| 181 | // End: //
 | 
|---|