[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 | |
---|
| 16 | #include <stdbool.h> |
---|
| 17 | #include <stdlib.h> |
---|
| 18 | #include <stdio.h> |
---|
[76e069f] | 19 | #include <unwind.h> |
---|
[78b3f52] | 20 | |
---|
| 21 | #include "invoke.h" |
---|
| 22 | |
---|
[5c81105] | 23 | #define __CFA_INVOKE_PRIVATE__ |
---|
| 24 | #include "invoke.h" |
---|
[78b3f52] | 25 | |
---|
| 26 | // magically invoke the "main" of the most derived class |
---|
| 27 | // Called from the kernel when starting a coroutine or task so must switch back to user mode. |
---|
[5c81105] | 28 | |
---|
[0c92c9f] | 29 | extern void __suspend_internal(void); |
---|
[212c2187] | 30 | extern void __leave_coroutine( struct coroutine_desc * ); |
---|
| 31 | extern void __finish_creation( struct coroutine_desc * ); |
---|
[1c273d0] | 32 | extern void __leave_thread_monitor( struct thread_desc * this ); |
---|
[82ff5845] | 33 | extern void disable_interrupts(); |
---|
[36982fc] | 34 | extern void enable_interrupts( __cfaabi_dbg_ctx_param ); |
---|
[78b3f52] | 35 | |
---|
[b58a5772] | 36 | void CtxInvokeCoroutine( |
---|
[36982fc] | 37 | void (*main)(void *), |
---|
| 38 | struct coroutine_desc *(*get_coroutine)(void *), |
---|
| 39 | void *this |
---|
[80d9e49] | 40 | ) { |
---|
[36982fc] | 41 | struct coroutine_desc* cor = get_coroutine( this ); |
---|
[80d9e49] | 42 | |
---|
[36982fc] | 43 | if(cor->state == Primed) { |
---|
| 44 | __suspend_internal(); |
---|
| 45 | } |
---|
[80d9e49] | 46 | |
---|
[36982fc] | 47 | cor->state = Active; |
---|
[78b3f52] | 48 | |
---|
[b69ea6b] | 49 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
| 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 |
---|
[212c2187] | 95 | __finish_creation(&thrd->self_cor); |
---|
[36982fc] | 96 | |
---|
[212c2187] | 97 | // Restore the last to NULL, we clobbered because of the thunk problem |
---|
[afd550c] | 98 | thrd->self_cor.last = NULL; |
---|
[36982fc] | 99 | |
---|
| 100 | // Officially start the thread by enabling preemption |
---|
| 101 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
| 102 | |
---|
| 103 | // Call the main of the thread |
---|
| 104 | main( this ); |
---|
| 105 | |
---|
| 106 | // To exit a thread we must : |
---|
| 107 | // 1 - Mark it as halted |
---|
| 108 | // 2 - Leave its monitor |
---|
| 109 | // 3 - Disable the interupts |
---|
| 110 | // 4 - Final suspend |
---|
| 111 | // The order of these 4 operations is very important |
---|
| 112 | //Final suspend, should never return |
---|
| 113 | __leave_thread_monitor( thrd ); |
---|
[169d944] | 114 | __cabi_abort( "Resumed dead thread" ); |
---|
[78b3f52] | 115 | } |
---|
| 116 | |
---|
[5c81105] | 117 | |
---|
[b58a5772] | 118 | void CtxStart( |
---|
[36982fc] | 119 | void (*main)(void *), |
---|
| 120 | struct coroutine_desc *(*get_coroutine)(void *), |
---|
| 121 | void *this, |
---|
| 122 | void (*invoke)(void *) |
---|
[80d9e49] | 123 | ) { |
---|
[36982fc] | 124 | struct coStack_t* stack = &get_coroutine( this )->stack; |
---|
[78b3f52] | 125 | |
---|
[381fdee] | 126 | #if defined( __i386 ) |
---|
[d9c44c3] | 127 | |
---|
| 128 | struct FakeStack { |
---|
| 129 | void *fixedRegisters[3]; // fixed registers ebx, edi, esi (popped on 1st uSwitch, values unimportant) |
---|
[17af7d1] | 130 | uint32_t mxcr; // SSE Status and Control bits (control bits are preserved across function calls) |
---|
[36982fc] | 131 | uint16_t fcw; // X97 FPU control word (preserved across function calls) |
---|
[17af7d1] | 132 | void *rturn; // where to go on return from uSwitch |
---|
[d9c44c3] | 133 | void *dummyReturn; // fake return compiler would have pushed on call to uInvoke |
---|
[80d9e49] | 134 | void *argument[3]; // for 16-byte ABI, 16-byte alignment starts here |
---|
| 135 | void *padding; // padding to force 16-byte alignment, as "base" is 16-byte aligned |
---|
[d9c44c3] | 136 | }; |
---|
| 137 | |
---|
| 138 | ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack ); |
---|
| 139 | ((struct machine_context_t *)stack->context)->FP = NULL; // terminate stack with NULL fp |
---|
| 140 | |
---|
| 141 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL; |
---|
[80d9e49] | 142 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->argument[0] = this; // argument to invoke |
---|
[d9c44c3] | 143 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = invoke; |
---|
[36982fc] | 144 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520 |
---|
| 145 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F; //Vol. 1 8-7 |
---|
[d9c44c3] | 146 | |
---|
[381fdee] | 147 | #elif defined( __x86_64 ) |
---|
[d9c44c3] | 148 | |
---|
[36982fc] | 149 | struct FakeStack { |
---|
| 150 | void *fixedRegisters[5]; // fixed registers rbx, r12, r13, r14, r15 |
---|
| 151 | uint32_t mxcr; // SSE Status and Control bits (control bits are preserved across function calls) |
---|
| 152 | uint16_t fcw; // X97 FPU control word (preserved across function calls) |
---|
| 153 | void *rturn; // where to go on return from uSwitch |
---|
| 154 | void *dummyReturn; // NULL return address to provide proper alignment |
---|
| 155 | }; |
---|
| 156 | |
---|
| 157 | ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack ); |
---|
| 158 | ((struct machine_context_t *)stack->context)->FP = NULL; // terminate stack with NULL fp |
---|
| 159 | |
---|
| 160 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL; |
---|
| 161 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = CtxInvokeStub; |
---|
| 162 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[0] = this; |
---|
| 163 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[1] = invoke; |
---|
| 164 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520 |
---|
| 165 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F; //Vol. 1 8-7 |
---|
[b158d8f] | 166 | |
---|
| 167 | #elif defined( __ARM_ARCH ) |
---|
| 168 | |
---|
| 169 | struct FakeStack { |
---|
| 170 | float fpRegs[16]; // floating point registers |
---|
| 171 | void *intRegs[9]; // integer/pointer registers |
---|
| 172 | void *arg[2]; // placeholder for this pointer |
---|
| 173 | }; |
---|
| 174 | |
---|
| 175 | ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack ); |
---|
| 176 | ((struct machine_context_t *)stack->context)->FP = NULL; |
---|
| 177 | |
---|
| 178 | struct FakeStack *fs = (struct FakeStack *)((struct machine_context_t *)stack->context)->SP; |
---|
| 179 | |
---|
| 180 | fs->intRegs[8] = CtxInvokeStub; |
---|
| 181 | fs->arg[0] = this; |
---|
| 182 | fs->arg[1] = invoke; |
---|
[381fdee] | 183 | |
---|
[d9c44c3] | 184 | #else |
---|
[381fdee] | 185 | #error uknown hardware architecture |
---|
[d9c44c3] | 186 | #endif |
---|
[e4745d7a] | 187 | } |
---|
[6b0b624] | 188 | |
---|
| 189 | // Local Variables: // |
---|
| 190 | // mode: c // |
---|
| 191 | // tab-width: 4 // |
---|
| 192 | // End: // |
---|