[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 | |
---|
[0c92c9f] | 31 | extern void __suspend_internal(void); |
---|
[212c2187] | 32 | extern void __leave_coroutine( struct coroutine_desc * ); |
---|
[e8e457e] | 33 | extern void __finish_creation( struct thread_desc * ); |
---|
[1c273d0] | 34 | extern void __leave_thread_monitor( struct thread_desc * this ); |
---|
[2026bb6] | 35 | extern void disable_interrupts() OPTIONAL_THREAD; |
---|
[36982fc] | 36 | extern void enable_interrupts( __cfaabi_dbg_ctx_param ); |
---|
[78b3f52] | 37 | |
---|
[b58a5772] | 38 | void CtxInvokeCoroutine( |
---|
[36982fc] | 39 | void (*main)(void *), |
---|
| 40 | struct coroutine_desc *(*get_coroutine)(void *), |
---|
| 41 | void *this |
---|
[80d9e49] | 42 | ) { |
---|
[36982fc] | 43 | struct coroutine_desc* cor = get_coroutine( this ); |
---|
[80d9e49] | 44 | |
---|
[36982fc] | 45 | if(cor->state == Primed) { |
---|
| 46 | __suspend_internal(); |
---|
| 47 | } |
---|
[80d9e49] | 48 | |
---|
[36982fc] | 49 | cor->state = Active; |
---|
[78b3f52] | 50 | |
---|
[36982fc] | 51 | main( this ); |
---|
[78b3f52] | 52 | |
---|
[36982fc] | 53 | //Final suspend, should never return |
---|
[212c2187] | 54 | __leave_coroutine( cor ); |
---|
[169d944] | 55 | __cabi_abort( "Resumed dead coroutine" ); |
---|
[8118303] | 56 | } |
---|
| 57 | |
---|
[76e069f] | 58 | static _Unwind_Reason_Code _CtxCoroutine_UnwindStop( |
---|
| 59 | __attribute((__unused__)) int version, |
---|
| 60 | _Unwind_Action actions, |
---|
| 61 | __attribute((__unused__)) _Unwind_Exception_Class exceptionClass, |
---|
| 62 | __attribute((__unused__)) struct _Unwind_Exception * unwind_exception, |
---|
| 63 | __attribute((__unused__)) struct _Unwind_Context * context, |
---|
[212c2187] | 64 | void * param |
---|
[76e069f] | 65 | ) { |
---|
| 66 | if( actions & _UA_END_OF_STACK ) { |
---|
| 67 | // We finished unwinding the coroutine, |
---|
| 68 | // leave it |
---|
[212c2187] | 69 | __leave_coroutine( param ); |
---|
[76e069f] | 70 | __cabi_abort( "Resumed dead coroutine" ); |
---|
| 71 | } |
---|
| 72 | if( actions & _UA_CLEANUP_PHASE ) return _URC_NO_REASON; |
---|
| 73 | |
---|
| 74 | return _URC_FATAL_PHASE2_ERROR; |
---|
| 75 | } |
---|
| 76 | |
---|
[212c2187] | 77 | void _CtxCoroutine_Unwind(struct _Unwind_Exception * storage, struct coroutine_desc * cor) __attribute__ ((__noreturn__)); |
---|
| 78 | void _CtxCoroutine_Unwind(struct _Unwind_Exception * storage, struct coroutine_desc * cor) { |
---|
| 79 | _Unwind_Reason_Code ret = _Unwind_ForcedUnwind( storage, _CtxCoroutine_UnwindStop, cor ); |
---|
[76e069f] | 80 | printf("UNWIND ERROR %d after force unwind\n", ret); |
---|
| 81 | abort(); |
---|
| 82 | } |
---|
| 83 | |
---|
[8118303] | 84 | void CtxInvokeThread( |
---|
[36982fc] | 85 | void (*dtor)(void *), |
---|
| 86 | void (*main)(void *), |
---|
| 87 | struct thread_desc *(*get_thread)(void *), |
---|
| 88 | void *this |
---|
[8118303] | 89 | ) { |
---|
[212c2187] | 90 | // Fetch the thread handle from the user defined thread structure |
---|
| 91 | struct thread_desc* thrd = get_thread( this ); |
---|
| 92 | |
---|
[36982fc] | 93 | // First suspend, once the thread arrives here, |
---|
| 94 | // the function pointer to main can be invalidated without risk |
---|
[e8e457e] | 95 | __finish_creation( thrd ); |
---|
[36982fc] | 96 | |
---|
| 97 | // Officially start the thread by enabling preemption |
---|
| 98 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
| 99 | |
---|
| 100 | // Call the main of the thread |
---|
| 101 | main( this ); |
---|
| 102 | |
---|
| 103 | // To exit a thread we must : |
---|
| 104 | // 1 - Mark it as halted |
---|
| 105 | // 2 - Leave its monitor |
---|
| 106 | // 3 - Disable the interupts |
---|
| 107 | // 4 - Final suspend |
---|
| 108 | // The order of these 4 operations is very important |
---|
| 109 | //Final suspend, should never return |
---|
| 110 | __leave_thread_monitor( thrd ); |
---|
[169d944] | 111 | __cabi_abort( "Resumed dead thread" ); |
---|
[78b3f52] | 112 | } |
---|
| 113 | |
---|
[5c81105] | 114 | |
---|
[b58a5772] | 115 | void CtxStart( |
---|
[36982fc] | 116 | void (*main)(void *), |
---|
| 117 | struct coroutine_desc *(*get_coroutine)(void *), |
---|
| 118 | void *this, |
---|
| 119 | void (*invoke)(void *) |
---|
[80d9e49] | 120 | ) { |
---|
[b2f6113] | 121 | struct coroutine_desc * cor = get_coroutine( this ); |
---|
| 122 | struct __stack_t * stack = cor->stack.storage; |
---|
[78b3f52] | 123 | |
---|
[381fdee] | 124 | #if defined( __i386 ) |
---|
[d9c44c3] | 125 | |
---|
| 126 | struct FakeStack { |
---|
[b78129a] | 127 | void *fixedRegisters[3]; // fixed registers ebx, edi, esi (popped on 1st uSwitch, values unimportant) |
---|
[17af7d1] | 128 | void *rturn; // where to go on return from uSwitch |
---|
[b78129a] | 129 | void *dummyReturn; // fake return compiler would have pushed on call to uInvoke |
---|
| 130 | void *argument[3]; // for 16-byte ABI, 16-byte alignment starts here |
---|
| 131 | void *padding; // padding to force 16-byte alignment, as "base" is 16-byte aligned |
---|
[d9c44c3] | 132 | }; |
---|
| 133 | |
---|
[b2f6113] | 134 | cor->context.SP = (char *)stack->base - sizeof( struct FakeStack ); |
---|
| 135 | cor->context.FP = NULL; // terminate stack with NULL fp |
---|
[d9c44c3] | 136 | |
---|
[b2f6113] | 137 | struct FakeStack *fs = (struct FakeStack *)cor->context.SP; |
---|
| 138 | |
---|
| 139 | fs->dummyReturn = NULL; |
---|
| 140 | fs->argument[0] = this; // argument to invoke |
---|
| 141 | fs->rturn = invoke; |
---|
[d9c44c3] | 142 | |
---|
[381fdee] | 143 | #elif defined( __x86_64 ) |
---|
[d9c44c3] | 144 | |
---|
[36982fc] | 145 | struct FakeStack { |
---|
| 146 | void *fixedRegisters[5]; // fixed registers rbx, r12, r13, r14, r15 |
---|
| 147 | void *rturn; // where to go on return from uSwitch |
---|
| 148 | void *dummyReturn; // NULL return address to provide proper alignment |
---|
| 149 | }; |
---|
| 150 | |
---|
[b2f6113] | 151 | cor->context.SP = (char *)stack->base - sizeof( struct FakeStack ); |
---|
| 152 | cor->context.FP = NULL; // terminate stack with NULL fp |
---|
| 153 | |
---|
| 154 | struct FakeStack *fs = (struct FakeStack *)cor->context.SP; |
---|
[36982fc] | 155 | |
---|
[b2f6113] | 156 | fs->dummyReturn = NULL; |
---|
| 157 | fs->rturn = CtxInvokeStub; |
---|
| 158 | fs->fixedRegisters[0] = this; |
---|
| 159 | fs->fixedRegisters[1] = invoke; |
---|
[b158d8f] | 160 | |
---|
| 161 | #elif defined( __ARM_ARCH ) |
---|
| 162 | |
---|
| 163 | struct FakeStack { |
---|
| 164 | float fpRegs[16]; // floating point registers |
---|
| 165 | void *intRegs[9]; // integer/pointer registers |
---|
| 166 | void *arg[2]; // placeholder for this pointer |
---|
| 167 | }; |
---|
| 168 | |
---|
[b2f6113] | 169 | cor->context.SP = (char *)stack->base - sizeof( struct FakeStack ); |
---|
| 170 | cor->context.FP = NULL; |
---|
[b158d8f] | 171 | |
---|
[b2f6113] | 172 | struct FakeStack *fs = (struct FakeStack *)cor->context.SP; |
---|
[b158d8f] | 173 | |
---|
| 174 | fs->intRegs[8] = CtxInvokeStub; |
---|
| 175 | fs->arg[0] = this; |
---|
| 176 | fs->arg[1] = invoke; |
---|
[381fdee] | 177 | |
---|
[d9c44c3] | 178 | #else |
---|
[381fdee] | 179 | #error uknown hardware architecture |
---|
[d9c44c3] | 180 | #endif |
---|
[e4745d7a] | 181 | } |
---|
[6b0b624] | 182 | |
---|
| 183 | // Local Variables: // |
---|
| 184 | // mode: c // |
---|
| 185 | // tab-width: 4 // |
---|
| 186 | // End: // |
---|