| [8def349] | 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 | // invoke.c -- | 
|---|
|  | 8 | // | 
|---|
|  | 9 | // Author           : Thierry Delisle | 
|---|
|  | 10 | // Created On       : Tue Jan 17 12:27:26 2016 | 
|---|
| [6b0b624] | 11 | // Last Modified By : Peter A. Buhr | 
|---|
| [bede27b] | 12 | // Last Modified On : Fri Feb  9 16:37:42 2018 | 
|---|
|  | 13 | // Update Count     : 5 | 
|---|
| [8def349] | 14 | // | 
|---|
| [78b3f52] | 15 |  | 
|---|
| [2026bb6] | 16 | #define __cforall_thread__ | 
|---|
|  | 17 |  | 
|---|
| [78b3f52] | 18 | #include <stdbool.h> | 
|---|
|  | 19 | #include <stdlib.h> | 
|---|
|  | 20 | #include <stdio.h> | 
|---|
| [76e069f] | 21 | #include <unwind.h> | 
|---|
| [78b3f52] | 22 |  | 
|---|
|  | 23 | #include "invoke.h" | 
|---|
|  | 24 |  | 
|---|
| [5c81105] | 25 | #define __CFA_INVOKE_PRIVATE__ | 
|---|
|  | 26 | #include "invoke.h" | 
|---|
| [78b3f52] | 27 |  | 
|---|
|  | 28 | // magically invoke the "main" of the most derived class | 
|---|
|  | 29 | // Called from the kernel when starting a coroutine or task so must switch back to user mode. | 
|---|
| [5c81105] | 30 |  | 
|---|
| [ac2b598] | 31 | extern struct $coroutine * __cfactx_cor_finish(void); | 
|---|
|  | 32 | extern void __cfactx_cor_leave ( struct $coroutine * ); | 
|---|
| [c7a900a] | 33 | extern void __cfactx_thrd_leave(); | 
|---|
|  | 34 |  | 
|---|
| [2026bb6] | 35 | extern void disable_interrupts() OPTIONAL_THREAD; | 
|---|
| [36982fc] | 36 | extern void enable_interrupts( __cfaabi_dbg_ctx_param ); | 
|---|
| [78b3f52] | 37 |  | 
|---|
| [c7a900a] | 38 | void __cfactx_invoke_coroutine( | 
|---|
| [36982fc] | 39 | void (*main)(void *), | 
|---|
|  | 40 | void *this | 
|---|
| [80d9e49] | 41 | ) { | 
|---|
| [09f357ec] | 42 | // Finish setting up the coroutine by setting its state | 
|---|
| [ac2b598] | 43 | struct $coroutine * cor = __cfactx_cor_finish(); | 
|---|
| [78b3f52] | 44 |  | 
|---|
| [09f357ec] | 45 | // Call the main of the coroutine | 
|---|
| [36982fc] | 46 | main( this ); | 
|---|
| [78b3f52] | 47 |  | 
|---|
| [36982fc] | 48 | //Final suspend, should never return | 
|---|
| [c7a900a] | 49 | __cfactx_cor_leave( cor ); | 
|---|
| [169d944] | 50 | __cabi_abort( "Resumed dead coroutine" ); | 
|---|
| [8118303] | 51 | } | 
|---|
|  | 52 |  | 
|---|
| [c7a900a] | 53 | static _Unwind_Reason_Code __cfactx_coroutine_unwindstop( | 
|---|
| [76e069f] | 54 | __attribute((__unused__)) int version, | 
|---|
|  | 55 | _Unwind_Action actions, | 
|---|
|  | 56 | __attribute((__unused__)) _Unwind_Exception_Class exceptionClass, | 
|---|
|  | 57 | __attribute((__unused__)) struct _Unwind_Exception * unwind_exception, | 
|---|
|  | 58 | __attribute((__unused__)) struct _Unwind_Context * context, | 
|---|
| [212c2187] | 59 | void * param | 
|---|
| [76e069f] | 60 | ) { | 
|---|
|  | 61 | if( actions & _UA_END_OF_STACK  ) { | 
|---|
|  | 62 | // We finished unwinding the coroutine, | 
|---|
|  | 63 | // leave it | 
|---|
| [c7a900a] | 64 | __cfactx_cor_leave( param ); | 
|---|
| [76e069f] | 65 | __cabi_abort( "Resumed dead coroutine" ); | 
|---|
|  | 66 | } | 
|---|
|  | 67 | if( actions & _UA_CLEANUP_PHASE ) return _URC_NO_REASON; | 
|---|
|  | 68 |  | 
|---|
|  | 69 | return _URC_FATAL_PHASE2_ERROR; | 
|---|
|  | 70 | } | 
|---|
|  | 71 |  | 
|---|
| [ac2b598] | 72 | void __cfactx_coroutine_unwind(struct _Unwind_Exception * storage, struct $coroutine * cor) __attribute__ ((__noreturn__)); | 
|---|
|  | 73 | void __cfactx_coroutine_unwind(struct _Unwind_Exception * storage, struct $coroutine * cor) { | 
|---|
| [c7a900a] | 74 | _Unwind_Reason_Code ret = _Unwind_ForcedUnwind( storage, __cfactx_coroutine_unwindstop, cor ); | 
|---|
| [76e069f] | 75 | printf("UNWIND ERROR %d after force unwind\n", ret); | 
|---|
|  | 76 | abort(); | 
|---|
|  | 77 | } | 
|---|
|  | 78 |  | 
|---|
| [c7a900a] | 79 | void __cfactx_invoke_thread( | 
|---|
| [36982fc] | 80 | void (*main)(void *), | 
|---|
|  | 81 | void *this | 
|---|
| [8118303] | 82 | ) { | 
|---|
| [36982fc] | 83 | // Officially start the thread by enabling preemption | 
|---|
|  | 84 | enable_interrupts( __cfaabi_dbg_ctx ); | 
|---|
|  | 85 |  | 
|---|
|  | 86 | // Call the main of the thread | 
|---|
|  | 87 | main( this ); | 
|---|
|  | 88 |  | 
|---|
|  | 89 | // To exit a thread we must : | 
|---|
|  | 90 | // 1 - Mark it as halted | 
|---|
|  | 91 | // 2 - Leave its monitor | 
|---|
|  | 92 | // 3 - Disable the interupts | 
|---|
|  | 93 | // 4 - Final suspend | 
|---|
|  | 94 | // The order of these 4 operations is very important | 
|---|
|  | 95 | //Final suspend, should never return | 
|---|
| [c7a900a] | 96 | __cfactx_thrd_leave(); | 
|---|
| [169d944] | 97 | __cabi_abort( "Resumed dead thread" ); | 
|---|
| [78b3f52] | 98 | } | 
|---|
|  | 99 |  | 
|---|
| [c7a900a] | 100 | void __cfactx_start( | 
|---|
| [36982fc] | 101 | void (*main)(void *), | 
|---|
| [ac2b598] | 102 | struct $coroutine * cor, | 
|---|
| [36982fc] | 103 | void *this, | 
|---|
|  | 104 | void (*invoke)(void *) | 
|---|
| [80d9e49] | 105 | ) { | 
|---|
| [b2f6113] | 106 | struct __stack_t * stack = cor->stack.storage; | 
|---|
| [78b3f52] | 107 |  | 
|---|
| [381fdee] | 108 | #if defined( __i386 ) | 
|---|
| [d9c44c3] | 109 |  | 
|---|
|  | 110 | struct FakeStack { | 
|---|
| [b78129a] | 111 | void *fixedRegisters[3];              // fixed registers ebx, edi, esi (popped on 1st uSwitch, values unimportant) | 
|---|
| [17af7d1] | 112 | void *rturn;                          // where to go on return from uSwitch | 
|---|
| [b78129a] | 113 | void *dummyReturn;                    // fake return compiler would have pushed on call to uInvoke | 
|---|
|  | 114 | void *argument[3];                    // for 16-byte ABI, 16-byte alignment starts here | 
|---|
|  | 115 | void *padding;                        // padding to force 16-byte alignment, as "base" is 16-byte aligned | 
|---|
| [d9c44c3] | 116 | }; | 
|---|
|  | 117 |  | 
|---|
| [b2f6113] | 118 | cor->context.SP = (char *)stack->base - sizeof( struct FakeStack ); | 
|---|
|  | 119 | cor->context.FP = NULL;         // terminate stack with NULL fp | 
|---|
| [d9c44c3] | 120 |  | 
|---|
| [b2f6113] | 121 | struct FakeStack *fs = (struct FakeStack *)cor->context.SP; | 
|---|
|  | 122 |  | 
|---|
|  | 123 | fs->dummyReturn = NULL; | 
|---|
| [09f357ec] | 124 | fs->argument[0] = main;     // argument to invoke | 
|---|
|  | 125 | fs->argument[1] = this;     // argument to invoke | 
|---|
| [b2f6113] | 126 | fs->rturn = invoke; | 
|---|
| [d9c44c3] | 127 |  | 
|---|
| [381fdee] | 128 | #elif defined( __x86_64 ) | 
|---|
| [d9c44c3] | 129 |  | 
|---|
| [36982fc] | 130 | struct FakeStack { | 
|---|
|  | 131 | void *fixedRegisters[5];            // fixed registers rbx, r12, r13, r14, r15 | 
|---|
|  | 132 | void *rturn;                        // where to go on return from uSwitch | 
|---|
|  | 133 | void *dummyReturn;                  // NULL return address to provide proper alignment | 
|---|
|  | 134 | }; | 
|---|
|  | 135 |  | 
|---|
| [b2f6113] | 136 | cor->context.SP = (char *)stack->base - sizeof( struct FakeStack ); | 
|---|
|  | 137 | cor->context.FP = NULL;         // terminate stack with NULL fp | 
|---|
|  | 138 |  | 
|---|
|  | 139 | struct FakeStack *fs = (struct FakeStack *)cor->context.SP; | 
|---|
| [36982fc] | 140 |  | 
|---|
| [b2f6113] | 141 | fs->dummyReturn = NULL; | 
|---|
| [c7a900a] | 142 | fs->rturn = __cfactx_invoke_stub; | 
|---|
| [09f357ec] | 143 | fs->fixedRegisters[0] = main; | 
|---|
|  | 144 | fs->fixedRegisters[1] = this; | 
|---|
|  | 145 | fs->fixedRegisters[2] = invoke; | 
|---|
| [b158d8f] | 146 |  | 
|---|
|  | 147 | #elif defined( __ARM_ARCH ) | 
|---|
| [09f357ec] | 148 | #error ARM needs to be upgrade to use to parameters like X86/X64 (A.K.A. : I broke this and do not know how to fix it) | 
|---|
| [b158d8f] | 149 | struct FakeStack { | 
|---|
|  | 150 | float fpRegs[16];                       // floating point registers | 
|---|
|  | 151 | void *intRegs[9];                       // integer/pointer registers | 
|---|
|  | 152 | void *arg[2];                           // placeholder for this pointer | 
|---|
|  | 153 | }; | 
|---|
|  | 154 |  | 
|---|
| [b2f6113] | 155 | cor->context.SP = (char *)stack->base - sizeof( struct FakeStack ); | 
|---|
|  | 156 | cor->context.FP = NULL; | 
|---|
| [b158d8f] | 157 |  | 
|---|
| [b2f6113] | 158 | struct FakeStack *fs = (struct FakeStack *)cor->context.SP; | 
|---|
| [b158d8f] | 159 |  | 
|---|
| [c7a900a] | 160 | fs->intRegs[8] = __cfactx_invoke_stub; | 
|---|
| [b158d8f] | 161 | fs->arg[0] = this; | 
|---|
|  | 162 | fs->arg[1] = invoke; | 
|---|
| [381fdee] | 163 |  | 
|---|
| [d9c44c3] | 164 | #else | 
|---|
| [381fdee] | 165 | #error uknown hardware architecture | 
|---|
| [d9c44c3] | 166 | #endif | 
|---|
| [e4745d7a] | 167 | } | 
|---|
| [6b0b624] | 168 |  | 
|---|
|  | 169 | // Local Variables: // | 
|---|
|  | 170 | // mode: c // | 
|---|
|  | 171 | // tab-width: 4 // | 
|---|
|  | 172 | // End: // | 
|---|