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 | |
---|
20 | #include "invoke.h" |
---|
21 | |
---|
22 | #define __CFA_INVOKE_PRIVATE__ |
---|
23 | #include "invoke.h" |
---|
24 | |
---|
25 | // magically invoke the "main" of the most derived class |
---|
26 | // Called from the kernel when starting a coroutine or task so must switch back to user mode. |
---|
27 | |
---|
28 | extern void __suspend_internal(void); |
---|
29 | extern void __leave_coroutine(void); |
---|
30 | extern void __finish_creation(void); |
---|
31 | extern void __leave_thread_monitor( struct thread_desc * this ); |
---|
32 | extern void disable_interrupts(); |
---|
33 | extern void enable_interrupts( __cfaabi_dbg_ctx_param ); |
---|
34 | |
---|
35 | void CtxInvokeCoroutine( |
---|
36 | void (*main)(void *), |
---|
37 | struct coroutine_desc *(*get_coroutine)(void *), |
---|
38 | void *this |
---|
39 | ) { |
---|
40 | struct coroutine_desc* cor = get_coroutine( this ); |
---|
41 | |
---|
42 | if(cor->state == Primed) { |
---|
43 | __suspend_internal(); |
---|
44 | } |
---|
45 | |
---|
46 | cor->state = Active; |
---|
47 | |
---|
48 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
49 | |
---|
50 | main( this ); |
---|
51 | |
---|
52 | cor->state = Halted; |
---|
53 | |
---|
54 | //Final suspend, should never return |
---|
55 | __leave_coroutine(); |
---|
56 | __cabi_abort( "Resumed dead coroutine" ); |
---|
57 | } |
---|
58 | |
---|
59 | void CtxInvokeThread( |
---|
60 | void (*dtor)(void *), |
---|
61 | void (*main)(void *), |
---|
62 | struct thread_desc *(*get_thread)(void *), |
---|
63 | void *this |
---|
64 | ) { |
---|
65 | // First suspend, once the thread arrives here, |
---|
66 | // the function pointer to main can be invalidated without risk |
---|
67 | __finish_creation(); |
---|
68 | |
---|
69 | // Fetch the thread handle from the user defined thread structure |
---|
70 | struct thread_desc* thrd = get_thread( this ); |
---|
71 | thrd->self_cor.last = NULL; |
---|
72 | |
---|
73 | // Officially start the thread by enabling preemption |
---|
74 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
75 | |
---|
76 | // Call the main of the thread |
---|
77 | main( this ); |
---|
78 | |
---|
79 | // To exit a thread we must : |
---|
80 | // 1 - Mark it as halted |
---|
81 | // 2 - Leave its monitor |
---|
82 | // 3 - Disable the interupts |
---|
83 | // 4 - Final suspend |
---|
84 | // The order of these 4 operations is very important |
---|
85 | //Final suspend, should never return |
---|
86 | __leave_thread_monitor( thrd ); |
---|
87 | __cabi_abort( "Resumed dead thread" ); |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | void CtxStart( |
---|
92 | void (*main)(void *), |
---|
93 | struct coroutine_desc *(*get_coroutine)(void *), |
---|
94 | void *this, |
---|
95 | void (*invoke)(void *) |
---|
96 | ) { |
---|
97 | struct coStack_t* stack = &get_coroutine( this )->stack; |
---|
98 | |
---|
99 | #if defined( __i386 ) |
---|
100 | |
---|
101 | struct FakeStack { |
---|
102 | void *fixedRegisters[3]; // fixed registers ebx, edi, esi (popped on 1st uSwitch, values unimportant) |
---|
103 | uint32_t mxcr; // SSE Status and Control bits (control bits are preserved across function calls) |
---|
104 | uint16_t fcw; // X97 FPU control word (preserved across function calls) |
---|
105 | void *rturn; // where to go on return from uSwitch |
---|
106 | void *dummyReturn; // fake return compiler would have pushed on call to uInvoke |
---|
107 | void *argument[3]; // for 16-byte ABI, 16-byte alignment starts here |
---|
108 | void *padding; // padding to force 16-byte alignment, as "base" is 16-byte aligned |
---|
109 | }; |
---|
110 | |
---|
111 | ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack ); |
---|
112 | ((struct machine_context_t *)stack->context)->FP = NULL; // terminate stack with NULL fp |
---|
113 | |
---|
114 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL; |
---|
115 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->argument[0] = this; // argument to invoke |
---|
116 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = invoke; |
---|
117 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520 |
---|
118 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F; //Vol. 1 8-7 |
---|
119 | |
---|
120 | #elif defined( __x86_64 ) |
---|
121 | |
---|
122 | struct FakeStack { |
---|
123 | void *fixedRegisters[5]; // fixed registers rbx, r12, r13, r14, r15 |
---|
124 | uint32_t mxcr; // SSE Status and Control bits (control bits are preserved across function calls) |
---|
125 | uint16_t fcw; // X97 FPU control word (preserved across function calls) |
---|
126 | void *rturn; // where to go on return from uSwitch |
---|
127 | void *dummyReturn; // NULL return address to provide proper alignment |
---|
128 | }; |
---|
129 | |
---|
130 | ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack ); |
---|
131 | ((struct machine_context_t *)stack->context)->FP = NULL; // terminate stack with NULL fp |
---|
132 | |
---|
133 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL; |
---|
134 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = CtxInvokeStub; |
---|
135 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[0] = this; |
---|
136 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[1] = invoke; |
---|
137 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520 |
---|
138 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F; //Vol. 1 8-7 |
---|
139 | |
---|
140 | #elif defined( __ARM_ARCH ) |
---|
141 | |
---|
142 | struct FakeStack { |
---|
143 | float fpRegs[16]; // floating point registers |
---|
144 | void *intRegs[9]; // integer/pointer registers |
---|
145 | void *arg[2]; // placeholder for this pointer |
---|
146 | }; |
---|
147 | |
---|
148 | ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack ); |
---|
149 | ((struct machine_context_t *)stack->context)->FP = NULL; |
---|
150 | |
---|
151 | struct FakeStack *fs = (struct FakeStack *)((struct machine_context_t *)stack->context)->SP; |
---|
152 | |
---|
153 | fs->intRegs[8] = CtxInvokeStub; |
---|
154 | fs->arg[0] = this; |
---|
155 | fs->arg[1] = invoke; |
---|
156 | |
---|
157 | #else |
---|
158 | #error uknown hardware architecture |
---|
159 | #endif |
---|
160 | } |
---|
161 | |
---|
162 | // Local Variables: // |
---|
163 | // mode: c // |
---|
164 | // tab-width: 4 // |
---|
165 | // End: // |
---|