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 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Fri Feb 9 16:37:42 2018 |
---|
13 | // Update Count : 5 |
---|
14 | // |
---|
15 | |
---|
16 | #define __cforall_thread__ |
---|
17 | |
---|
18 | #include <stdbool.h> |
---|
19 | #include <stdlib.h> |
---|
20 | #include <stdio.h> |
---|
21 | #include <unwind.h> |
---|
22 | |
---|
23 | #include "invoke.h" |
---|
24 | |
---|
25 | #define __CFA_INVOKE_PRIVATE__ |
---|
26 | #include "invoke.h" |
---|
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. |
---|
30 | |
---|
31 | extern struct $coroutine * __cfactx_cor_finish(void); |
---|
32 | extern void __cfactx_cor_leave ( struct $coroutine * ); |
---|
33 | extern void __cfactx_thrd_leave(); |
---|
34 | |
---|
35 | extern void disable_interrupts() OPTIONAL_THREAD; |
---|
36 | extern void enable_interrupts( __cfaabi_dbg_ctx_param ); |
---|
37 | |
---|
38 | void __cfactx_invoke_coroutine( |
---|
39 | void (*main)(void *), |
---|
40 | void *this |
---|
41 | ) { |
---|
42 | // Finish setting up the coroutine by setting its state |
---|
43 | struct $coroutine * cor = __cfactx_cor_finish(); |
---|
44 | |
---|
45 | // Call the main of the coroutine |
---|
46 | main( this ); |
---|
47 | |
---|
48 | //Final suspend, should never return |
---|
49 | __cfactx_cor_leave( cor ); |
---|
50 | __cabi_abort( "Resumed dead coroutine" ); |
---|
51 | } |
---|
52 | |
---|
53 | static _Unwind_Reason_Code __cfactx_coroutine_unwindstop( |
---|
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, |
---|
59 | void * param |
---|
60 | ) { |
---|
61 | if( actions & _UA_END_OF_STACK ) { |
---|
62 | // We finished unwinding the coroutine, |
---|
63 | // leave it |
---|
64 | __cfactx_cor_leave( param ); |
---|
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 | |
---|
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) { |
---|
74 | _Unwind_Reason_Code ret = _Unwind_ForcedUnwind( storage, __cfactx_coroutine_unwindstop, cor ); |
---|
75 | printf("UNWIND ERROR %d after force unwind\n", ret); |
---|
76 | abort(); |
---|
77 | } |
---|
78 | |
---|
79 | void __cfactx_invoke_thread( |
---|
80 | void (*main)(void *), |
---|
81 | void *this |
---|
82 | ) { |
---|
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 |
---|
96 | __cfactx_thrd_leave(); |
---|
97 | __cabi_abort( "Resumed dead thread" ); |
---|
98 | } |
---|
99 | |
---|
100 | void __cfactx_start( |
---|
101 | void (*main)(void *), |
---|
102 | struct $coroutine * cor, |
---|
103 | void *this, |
---|
104 | void (*invoke)(void *) |
---|
105 | ) { |
---|
106 | struct __stack_t * stack = cor->stack.storage; |
---|
107 | |
---|
108 | #if defined( __i386 ) |
---|
109 | |
---|
110 | struct FakeStack { |
---|
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 |
---|
116 | }; |
---|
117 | |
---|
118 | cor->context.SP = (char *)stack->base - sizeof( struct FakeStack ); |
---|
119 | cor->context.FP = NULL; // terminate stack with NULL fp |
---|
120 | |
---|
121 | struct FakeStack *fs = (struct FakeStack *)cor->context.SP; |
---|
122 | |
---|
123 | fs->dummyReturn = NULL; |
---|
124 | fs->argument[0] = main; // argument to invoke |
---|
125 | fs->argument[1] = this; // argument to invoke |
---|
126 | fs->rturn = invoke; |
---|
127 | |
---|
128 | #elif defined( __x86_64 ) |
---|
129 | |
---|
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 | |
---|
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; |
---|
140 | |
---|
141 | fs->dummyReturn = NULL; |
---|
142 | fs->rturn = __cfactx_invoke_stub; |
---|
143 | fs->fixedRegisters[0] = main; |
---|
144 | fs->fixedRegisters[1] = this; |
---|
145 | fs->fixedRegisters[2] = invoke; |
---|
146 | |
---|
147 | #elif defined( __ARM_ARCH ) |
---|
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 | |
---|
157 | struct FakeStack { |
---|
158 | float fpRegs[16]; // floating point registers |
---|
159 | void *intRegs[9]; // integer/pointer registers |
---|
160 | void *arg[2]; // placeholder for this pointer |
---|
161 | }; |
---|
162 | |
---|
163 | cor->context.SP = (char *)stack->base - sizeof( struct FakeStack ); |
---|
164 | cor->context.FP = NULL; |
---|
165 | |
---|
166 | struct FakeStack *fs = (struct FakeStack *)cor->context.SP; |
---|
167 | |
---|
168 | fs->intRegs[8] = __cfactx_invoke_stub; |
---|
169 | fs->arg[0] = this; |
---|
170 | fs->arg[1] = invoke; |
---|
171 | |
---|
172 | #else |
---|
173 | #error uknown hardware architecture |
---|
174 | #endif |
---|
175 | } |
---|
176 | |
---|
177 | // Local Variables: // |
---|
178 | // mode: c // |
---|
179 | // tab-width: 4 // |
---|
180 | // End: // |
---|