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