| [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
 | 
|---|
| [d31bb83] | 12 | // Last Modified On : Sat Oct 24 14:35:28 2020
 | 
|---|
 | 13 | // Update Count     : 32
 | 
|---|
| [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 {
 | 
|---|
| [9705ffe] | 111 |             void *fixedRegisters[3];                                            // fixed registers ebx, edi, esi (popped on 1st uSwitch, values unimportant)
 | 
|---|
 | 112 |             void *rturn;                                                                        // where to go on return from uSwitch
 | 
|---|
 | 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;
 | 
|---|
| [9705ffe] | 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 {
 | 
|---|
| [9705ffe] | 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
 | 
|---|
| [36982fc] | 134 |         };
 | 
|---|
 | 135 | 
 | 
|---|
| [b2f6113] | 136 |         cor->context.SP = (char *)stack->base - sizeof( struct FakeStack );
 | 
|---|
| [9705ffe] | 137 |         cor->context.FP = NULL;                                                         // terminate stack with NULL fp
 | 
|---|
| [b2f6113] | 138 | 
 | 
|---|
 | 139 |         struct FakeStack *fs = (struct FakeStack *)cor->context.SP;
 | 
|---|
| [36982fc] | 140 | 
 | 
|---|
| [b2f6113] | 141 |         fs->dummyReturn = NULL;
 | 
|---|
| [c7a900a] | 142 |         fs->rturn = __cfactx_invoke_stub;
 | 
|---|
| [9705ffe] | 143 |         fs->fixedRegisters[0] = main;                                           // argument to invoke
 | 
|---|
 | 144 |         fs->fixedRegisters[1] = this;                                           // argument to invoke
 | 
|---|
| [09f357ec] | 145 |         fs->fixedRegisters[2] = invoke;
 | 
|---|
| [b158d8f] | 146 | 
 | 
|---|
| [9705ffe] | 147 | #elif defined( __ARM_ARCH_32 )
 | 
|---|
| [129674b] | 148 | #error ARM needs to be upgrade to use two parameters like X86/X64 (A.K.A. : I broke this and do not know how to fix it)
 | 
|---|
 | 149 |         // More details about the error:
 | 
|---|
 | 150 |         // To avoid the thunk problem, I changed the invoke routine to pass the main explicitly
 | 
|---|
 | 151 |         // instead of relying on an assertion. This effectively hoists any required thunk one level
 | 
|---|
 | 152 |         // which was enough to get to global scope in most cases.
 | 
|---|
 | 153 |         // This means that __cfactx_invoke_... now takes two parameters and the FakeStack needs
 | 
|---|
 | 154 |         // to be adjusted as a consequence of that.
 | 
|---|
 | 155 |         // I don't know how to do that for ARM, hence the #error
 | 
|---|
 | 156 | 
 | 
|---|
| [b158d8f] | 157 |         struct FakeStack {
 | 
|---|
| [9705ffe] | 158 |                 float fpRegs[16];                                                               // floating point registers
 | 
|---|
 | 159 |                 void * intRegs[9];                                                              // integer/pointer registers
 | 
|---|
 | 160 |                 void * arg[2];                                                                  // placeholder for this pointer
 | 
|---|
| [b158d8f] | 161 |         };
 | 
|---|
 | 162 | 
 | 
|---|
| [b2f6113] | 163 |         cor->context.SP = (char *)stack->base - sizeof( struct FakeStack );
 | 
|---|
 | 164 |         cor->context.FP = NULL;
 | 
|---|
| [b158d8f] | 165 | 
 | 
|---|
| [b2f6113] | 166 |         struct FakeStack *fs = (struct FakeStack *)cor->context.SP;
 | 
|---|
| [b158d8f] | 167 | 
 | 
|---|
| [c7a900a] | 168 |         fs->intRegs[8] = __cfactx_invoke_stub;
 | 
|---|
| [b158d8f] | 169 |         fs->arg[0] = this;
 | 
|---|
 | 170 |         fs->arg[1] = invoke;
 | 
|---|
| [381fdee] | 171 | 
 | 
|---|
| [9705ffe] | 172 | #elif defined( __ARM_ARCH )
 | 
|---|
 | 173 |         struct FakeStack {
 | 
|---|
 | 174 |                 void * intRegs[12];                                                             // x19-x30 integer registers
 | 
|---|
 | 175 |                 double fpRegs[8];                                                               // v8-v15 floating point
 | 
|---|
 | 176 |         };
 | 
|---|
 | 177 | 
 | 
|---|
 | 178 |         cor->context.SP = (char *)stack->base - sizeof( struct FakeStack );
 | 
|---|
 | 179 |         cor->context.FP = NULL;
 | 
|---|
 | 180 | 
 | 
|---|
 | 181 |         struct FakeStack *fs = (struct FakeStack *)cor->context.SP;
 | 
|---|
 | 182 | 
 | 
|---|
 | 183 |         fs->intRegs[0] = main;                                                          // argument to invoke x19 => x0
 | 
|---|
 | 184 |         fs->intRegs[1] = this;                                                          // argument to invoke x20 => x1
 | 
|---|
 | 185 |         fs->intRegs[2] = invoke;
 | 
|---|
 | 186 |         fs->intRegs[11] = __cfactx_invoke_stub;                         // link register x30 => ret moves to pc
 | 
|---|
| [d9c44c3] | 187 | #else
 | 
|---|
| [381fdee] | 188 |         #error uknown hardware architecture
 | 
|---|
| [d9c44c3] | 189 | #endif
 | 
|---|
| [e4745d7a] | 190 | }
 | 
|---|
| [6b0b624] | 191 | 
 | 
|---|
 | 192 | // Local Variables: //
 | 
|---|
 | 193 | // mode: c //
 | 
|---|
 | 194 | // tab-width: 4 //
 | 
|---|
 | 195 | // End: //
 | 
|---|