1 | // -*- Mode: C -*- |
---|
2 | // |
---|
3 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
4 | // |
---|
5 | // The contents of this file are covered under the licence agreement in the |
---|
6 | // file "LICENCE" distributed with Cforall. |
---|
7 | // |
---|
8 | // invoke.c -- |
---|
9 | // |
---|
10 | // Author : Thierry Delisle |
---|
11 | // Created On : Tue Jan 17 12:27:26 2016 |
---|
12 | // Last Modified By : Thierry Delisle |
---|
13 | // Last Modified On : -- |
---|
14 | // Update Count : 0 |
---|
15 | // |
---|
16 | |
---|
17 | #include <stdbool.h> |
---|
18 | #include <stdlib.h> |
---|
19 | #include <stdio.h> |
---|
20 | |
---|
21 | #include "libhdr.h" |
---|
22 | #include "invoke.h" |
---|
23 | |
---|
24 | #define __CFA_INVOKE_PRIVATE__ |
---|
25 | #include "invoke.h" |
---|
26 | |
---|
27 | // magically invoke the "main" of the most derived class |
---|
28 | // Called from the kernel when starting a coroutine or task so must switch back to user mode. |
---|
29 | |
---|
30 | extern void __suspend_internal(void); |
---|
31 | extern void __leave_thread_monitor( struct thread_desc * this ); |
---|
32 | extern void disable_interrupts(); |
---|
33 | extern void enable_interrupts( const char * ); |
---|
34 | |
---|
35 | void CtxInvokeCoroutine( |
---|
36 | void (*main)(void *), |
---|
37 | struct coroutine_desc *(*get_coroutine)(void *), |
---|
38 | void *this |
---|
39 | ) { |
---|
40 | // LIB_DEBUG_PRINTF("Invoke Coroutine : Received %p (main %p, get_c %p)\n", this, main, get_coroutine); |
---|
41 | |
---|
42 | struct coroutine_desc* cor = get_coroutine( this ); |
---|
43 | |
---|
44 | if(cor->state == Primed) { |
---|
45 | __suspend_internal(); |
---|
46 | } |
---|
47 | |
---|
48 | cor->state = Active; |
---|
49 | |
---|
50 | main( this ); |
---|
51 | |
---|
52 | cor->state = Halted; |
---|
53 | |
---|
54 | //Final suspend, should never return |
---|
55 | __suspend_internal(); |
---|
56 | abortf("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 | __suspend_internal(); |
---|
68 | |
---|
69 | // Fetch the thread handle from the user defined thread structure |
---|
70 | struct thread_desc* thrd = get_thread( this ); |
---|
71 | |
---|
72 | // Officially start the thread by enabling preemption |
---|
73 | enable_interrupts( __PRETTY_FUNCTION__ ); |
---|
74 | |
---|
75 | // Call the main of the thread |
---|
76 | main( this ); |
---|
77 | |
---|
78 | // To exit a thread we must : |
---|
79 | // 1 - Mark it as halted |
---|
80 | // 2 - Leave its monitor |
---|
81 | // 3 - Disable the interupts |
---|
82 | // The order of these 3 operations is very important |
---|
83 | __leave_thread_monitor( thrd ); |
---|
84 | |
---|
85 | //Final suspend, should never return |
---|
86 | __suspend_internal(); |
---|
87 | abortf("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 | // LIB_DEBUG_PRINTF("StartCoroutine : Passing in %p (main %p) to invoke (%p) from start (%p)\n", this, main, invoke, CtxStart); |
---|
98 | |
---|
99 | struct coStack_t* stack = &get_coroutine( this )->stack; |
---|
100 | |
---|
101 | #if defined( __i386__ ) |
---|
102 | |
---|
103 | struct FakeStack { |
---|
104 | void *fixedRegisters[3]; // fixed registers ebx, edi, esi (popped on 1st uSwitch, values unimportant) |
---|
105 | uint32_t mxcr; // SSE Status and Control bits (control bits are preserved across function calls) |
---|
106 | uint16_t fcw; // X97 FPU control word (preserved across function calls) |
---|
107 | void *rturn; // where to go on return from uSwitch |
---|
108 | void *dummyReturn; // fake return compiler would have pushed on call to uInvoke |
---|
109 | void *argument[3]; // for 16-byte ABI, 16-byte alignment starts here |
---|
110 | void *padding; // padding to force 16-byte alignment, as "base" is 16-byte aligned |
---|
111 | }; |
---|
112 | |
---|
113 | ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack ); |
---|
114 | ((struct machine_context_t *)stack->context)->FP = NULL; // terminate stack with NULL fp |
---|
115 | |
---|
116 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL; |
---|
117 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->argument[0] = this; // argument to invoke |
---|
118 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = invoke; |
---|
119 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520 |
---|
120 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F; //Vol. 1 8-7 |
---|
121 | |
---|
122 | #elif defined( __x86_64__ ) |
---|
123 | |
---|
124 | struct FakeStack { |
---|
125 | void *fixedRegisters[5]; // fixed registers rbx, r12, r13, r14, r15 |
---|
126 | uint32_t mxcr; // SSE Status and Control bits (control bits are preserved across function calls) |
---|
127 | uint16_t fcw; // X97 FPU control word (preserved across function calls) |
---|
128 | void *rturn; // where to go on return from uSwitch |
---|
129 | void *dummyReturn; // NULL return address to provide proper alignment |
---|
130 | }; |
---|
131 | |
---|
132 | ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack ); |
---|
133 | ((struct machine_context_t *)stack->context)->FP = NULL; // terminate stack with NULL fp |
---|
134 | |
---|
135 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL; |
---|
136 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = CtxInvokeStub; |
---|
137 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[0] = this; |
---|
138 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[1] = invoke; |
---|
139 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520 |
---|
140 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F; //Vol. 1 8-7 |
---|
141 | #else |
---|
142 | #error Only __i386__ and __x86_64__ is supported for threads in cfa |
---|
143 | #endif |
---|
144 | } |
---|