source: libcfa/src/concurrency/invoke.c @ 8c01e1b

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 8c01e1b was b2f6113, checked in by tdelisle <tdelisle@…>, 5 years ago

Swapped memory storage for context and stack information inside the coroutine implementation

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