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 Jul 21 22:28:33 2017 |
---|
13 | // Update Count : 1 |
---|
14 | // |
---|
15 | |
---|
16 | #include <stdbool.h> |
---|
17 | #include <stdlib.h> |
---|
18 | #include <stdio.h> |
---|
19 | |
---|
20 | #include "libhdr.h" |
---|
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_thread_monitor( struct thread_desc * this ); |
---|
31 | extern void disable_interrupts(); |
---|
32 | extern void enable_interrupts( DEBUG_CTX_PARAM ); |
---|
33 | |
---|
34 | void CtxInvokeCoroutine( |
---|
35 | void (*main)(void *), |
---|
36 | struct coroutine_desc *(*get_coroutine)(void *), |
---|
37 | void *this |
---|
38 | ) { |
---|
39 | // LIB_DEBUG_PRINTF("Invoke Coroutine : Received %p (main %p, get_c %p)\n", this, main, get_coroutine); |
---|
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 | cor->state = Halted; |
---|
52 | |
---|
53 | //Final suspend, should never return |
---|
54 | __suspend_internal(); |
---|
55 | abortf("Resumed dead coroutine"); |
---|
56 | } |
---|
57 | |
---|
58 | void CtxInvokeThread( |
---|
59 | void (*dtor)(void *), |
---|
60 | void (*main)(void *), |
---|
61 | struct thread_desc *(*get_thread)(void *), |
---|
62 | void *this |
---|
63 | ) { |
---|
64 | // First suspend, once the thread arrives here, |
---|
65 | // the function pointer to main can be invalidated without risk |
---|
66 | __suspend_internal(); |
---|
67 | |
---|
68 | // Fetch the thread handle from the user defined thread structure |
---|
69 | struct thread_desc* thrd = get_thread( this ); |
---|
70 | |
---|
71 | // Officially start the thread by enabling preemption |
---|
72 | enable_interrupts( DEBUG_CTX ); |
---|
73 | |
---|
74 | // Call the main of the thread |
---|
75 | main( this ); |
---|
76 | |
---|
77 | // To exit a thread we must : |
---|
78 | // 1 - Mark it as halted |
---|
79 | // 2 - Leave its monitor |
---|
80 | // 3 - Disable the interupts |
---|
81 | // 4 - Final suspend |
---|
82 | // The order of these 4 operations is very important |
---|
83 | //Final suspend, should never return |
---|
84 | __leave_thread_monitor( thrd ); |
---|
85 | abortf("Resumed dead thread"); |
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | void CtxStart( |
---|
90 | void (*main)(void *), |
---|
91 | struct coroutine_desc *(*get_coroutine)(void *), |
---|
92 | void *this, |
---|
93 | void (*invoke)(void *) |
---|
94 | ) { |
---|
95 | // LIB_DEBUG_PRINTF("StartCoroutine : Passing in %p (main %p) to invoke (%p) from start (%p)\n", this, main, invoke, CtxStart); |
---|
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 | #else |
---|
140 | #error Only __i386__ and __x86_64__ is supported for threads in cfa |
---|
141 | #endif |
---|
142 | } |
---|
143 | |
---|
144 | // Local Variables: // |
---|
145 | // mode: c // |
---|
146 | // tab-width: 4 // |
---|
147 | // End: // |
---|