source: libcfa/src/concurrency/invoke.c @ 5715d43

arm-ehjacob/cs343-translationnew-astnew-ast-unique-expr
Last change on this file since 5715d43 was 5715d43, checked in by Andrew Beach <ajbeach@…>, 16 months ago

Exceptions now get their context differently with libcfathread. Added a number of tests to help test this.

  • Property mode set to 100644
File size: 5.7 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#define __cforall_thread__
17
18#include <stdbool.h>
19#include <stdlib.h>
20#include <stdio.h>
21#include <unwind.h>
22
23#include "invoke.h"
24
25#define __CFA_INVOKE_PRIVATE__
26#include "invoke.h"
27
28// magically invoke the "main" of the most derived class
29// Called from the kernel when starting a coroutine or task so must switch back to user mode.
30
31extern struct $coroutine * __cfactx_cor_active(void);
32extern struct $coroutine * __cfactx_cor_finish(void);
33extern void __cfactx_cor_leave ( struct $coroutine * );
34extern void __cfactx_thrd_leave();
35
36extern void disable_interrupts() OPTIONAL_THREAD;
37extern void enable_interrupts( __cfaabi_dbg_ctx_param );
38
39struct exception_context_t * this_exception_context() {
40        return &__get_stack( __cfactx_cor_active() )->exception_context;
41}
42
43void __cfactx_invoke_coroutine(
44        void (*main)(void *),
45        void *this
46) {
47        // Finish setting up the coroutine by setting its state
48        struct $coroutine * cor = __cfactx_cor_finish();
49
50        // Call the main of the coroutine
51        main( this );
52
53        //Final suspend, should never return
54        __cfactx_cor_leave( cor );
55        __cabi_abort( "Resumed dead coroutine" );
56}
57
58static _Unwind_Reason_Code __cfactx_coroutine_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                __cfactx_cor_leave( 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 __cfactx_coroutine_unwind(struct _Unwind_Exception * storage, struct $coroutine * cor) __attribute__ ((__noreturn__));
78void __cfactx_coroutine_unwind(struct _Unwind_Exception * storage, struct $coroutine * cor) {
79        _Unwind_Reason_Code ret = _Unwind_ForcedUnwind( storage, __cfactx_coroutine_unwindstop, cor );
80        printf("UNWIND ERROR %d after force unwind\n", ret);
81        abort();
82}
83
84void __cfactx_invoke_thread(
85        void (*main)(void *),
86        void *this
87) {
88        // Officially start the thread by enabling preemption
89        enable_interrupts( __cfaabi_dbg_ctx );
90
91        // Call the main of the thread
92        main( this );
93
94        // To exit a thread we must :
95        // 1 - Mark it as halted
96        // 2 - Leave its monitor
97        // 3 - Disable the interupts
98        // 4 - Final suspend
99        // The order of these 4 operations is very important
100        //Final suspend, should never return
101        __cfactx_thrd_leave();
102        __cabi_abort( "Resumed dead thread" );
103}
104
105void __cfactx_start(
106        void (*main)(void *),
107        struct $coroutine * cor,
108        void *this,
109        void (*invoke)(void *)
110) {
111        struct __stack_t * stack = cor->stack.storage;
112
113#if defined( __i386 )
114
115        struct FakeStack {
116            void *fixedRegisters[3];              // fixed registers ebx, edi, esi (popped on 1st uSwitch, values unimportant)
117            void *rturn;                          // where to go on return from uSwitch
118            void *dummyReturn;                    // fake return compiler would have pushed on call to uInvoke
119            void *argument[3];                    // for 16-byte ABI, 16-byte alignment starts here
120            void *padding;                        // padding to force 16-byte alignment, as "base" is 16-byte aligned
121        };
122
123        cor->context.SP = (char *)stack->base - sizeof( struct FakeStack );
124        cor->context.FP = NULL;         // terminate stack with NULL fp
125
126        struct FakeStack *fs = (struct FakeStack *)cor->context.SP;
127
128        fs->dummyReturn = NULL;
129        fs->argument[0] = main;     // argument to invoke
130        fs->argument[1] = this;     // argument to invoke
131        fs->rturn = invoke;
132
133#elif defined( __x86_64 )
134
135        struct FakeStack {
136                void *fixedRegisters[5];            // fixed registers rbx, r12, r13, r14, r15
137                void *rturn;                        // where to go on return from uSwitch
138                void *dummyReturn;                  // NULL return address to provide proper alignment
139        };
140
141        cor->context.SP = (char *)stack->base - sizeof( struct FakeStack );
142        cor->context.FP = NULL;         // terminate stack with NULL fp
143
144        struct FakeStack *fs = (struct FakeStack *)cor->context.SP;
145
146        fs->dummyReturn = NULL;
147        fs->rturn = __cfactx_invoke_stub;
148        fs->fixedRegisters[0] = main;
149        fs->fixedRegisters[1] = this;
150        fs->fixedRegisters[2] = invoke;
151
152#elif defined( __ARM_ARCH )
153#error ARM needs to be upgrade to use two parameters like X86/X64 (A.K.A. : I broke this and do not know how to fix it)
154        // More details about the error:
155        // To avoid the thunk problem, I changed the invoke routine to pass the main explicitly
156        // instead of relying on an assertion. This effectively hoists any required thunk one level
157        // which was enough to get to global scope in most cases.
158        // This means that __cfactx_invoke_... now takes two parameters and the FakeStack needs
159        // to be adjusted as a consequence of that.
160        // I don't know how to do that for ARM, hence the #error
161
162        struct FakeStack {
163                float fpRegs[16];                       // floating point registers
164                void *intRegs[9];                       // integer/pointer registers
165                void *arg[2];                           // placeholder for this pointer
166        };
167
168        cor->context.SP = (char *)stack->base - sizeof( struct FakeStack );
169        cor->context.FP = NULL;
170
171        struct FakeStack *fs = (struct FakeStack *)cor->context.SP;
172
173        fs->intRegs[8] = __cfactx_invoke_stub;
174        fs->arg[0] = this;
175        fs->arg[1] = invoke;
176
177#else
178        #error uknown hardware architecture
179#endif
180}
181
182// Local Variables: //
183// mode: c //
184// tab-width: 4 //
185// End: //
Note: See TracBrowser for help on using the repository browser.