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