source: libcfa/src/concurrency/invoke.c@ 794db28

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 794db28 was 5715d43, checked in by Andrew Beach <ajbeach@…>, 5 years 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
RevLine 
[8def349]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
[6b0b624]11// Last Modified By : Peter A. Buhr
[bede27b]12// Last Modified On : Fri Feb 9 16:37:42 2018
13// Update Count : 5
[8def349]14//
[78b3f52]15
[2026bb6]16#define __cforall_thread__
17
[78b3f52]18#include <stdbool.h>
19#include <stdlib.h>
20#include <stdio.h>
[76e069f]21#include <unwind.h>
[78b3f52]22
23#include "invoke.h"
24
[5c81105]25#define __CFA_INVOKE_PRIVATE__
26#include "invoke.h"
[78b3f52]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.
[5c81105]30
[5715d43]31extern struct $coroutine * __cfactx_cor_active(void);
[ac2b598]32extern struct $coroutine * __cfactx_cor_finish(void);
33extern void __cfactx_cor_leave ( struct $coroutine * );
[c7a900a]34extern void __cfactx_thrd_leave();
35
[2026bb6]36extern void disable_interrupts() OPTIONAL_THREAD;
[36982fc]37extern void enable_interrupts( __cfaabi_dbg_ctx_param );
[78b3f52]38
[5715d43]39struct exception_context_t * this_exception_context() {
40 return &__get_stack( __cfactx_cor_active() )->exception_context;
41}
42
[c7a900a]43void __cfactx_invoke_coroutine(
[36982fc]44 void (*main)(void *),
45 void *this
[80d9e49]46) {
[09f357ec]47 // Finish setting up the coroutine by setting its state
[ac2b598]48 struct $coroutine * cor = __cfactx_cor_finish();
[78b3f52]49
[09f357ec]50 // Call the main of the coroutine
[36982fc]51 main( this );
[78b3f52]52
[36982fc]53 //Final suspend, should never return
[c7a900a]54 __cfactx_cor_leave( cor );
[169d944]55 __cabi_abort( "Resumed dead coroutine" );
[8118303]56}
57
[c7a900a]58static _Unwind_Reason_Code __cfactx_coroutine_unwindstop(
[76e069f]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,
[212c2187]64 void * param
[76e069f]65) {
66 if( actions & _UA_END_OF_STACK ) {
67 // We finished unwinding the coroutine,
68 // leave it
[c7a900a]69 __cfactx_cor_leave( param );
[76e069f]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
[ac2b598]77void __cfactx_coroutine_unwind(struct _Unwind_Exception * storage, struct $coroutine * cor) __attribute__ ((__noreturn__));
78void __cfactx_coroutine_unwind(struct _Unwind_Exception * storage, struct $coroutine * cor) {
[c7a900a]79 _Unwind_Reason_Code ret = _Unwind_ForcedUnwind( storage, __cfactx_coroutine_unwindstop, cor );
[76e069f]80 printf("UNWIND ERROR %d after force unwind\n", ret);
81 abort();
82}
83
[c7a900a]84void __cfactx_invoke_thread(
[36982fc]85 void (*main)(void *),
86 void *this
[8118303]87) {
[36982fc]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
[c7a900a]101 __cfactx_thrd_leave();
[169d944]102 __cabi_abort( "Resumed dead thread" );
[78b3f52]103}
104
[c7a900a]105void __cfactx_start(
[36982fc]106 void (*main)(void *),
[ac2b598]107 struct $coroutine * cor,
[36982fc]108 void *this,
109 void (*invoke)(void *)
[80d9e49]110) {
[b2f6113]111 struct __stack_t * stack = cor->stack.storage;
[78b3f52]112
[381fdee]113#if defined( __i386 )
[d9c44c3]114
115 struct FakeStack {
[b78129a]116 void *fixedRegisters[3]; // fixed registers ebx, edi, esi (popped on 1st uSwitch, values unimportant)
[17af7d1]117 void *rturn; // where to go on return from uSwitch
[b78129a]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
[d9c44c3]121 };
122
[b2f6113]123 cor->context.SP = (char *)stack->base - sizeof( struct FakeStack );
124 cor->context.FP = NULL; // terminate stack with NULL fp
[d9c44c3]125
[b2f6113]126 struct FakeStack *fs = (struct FakeStack *)cor->context.SP;
127
128 fs->dummyReturn = NULL;
[09f357ec]129 fs->argument[0] = main; // argument to invoke
130 fs->argument[1] = this; // argument to invoke
[b2f6113]131 fs->rturn = invoke;
[d9c44c3]132
[381fdee]133#elif defined( __x86_64 )
[d9c44c3]134
[36982fc]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
[b2f6113]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;
[36982fc]145
[b2f6113]146 fs->dummyReturn = NULL;
[c7a900a]147 fs->rturn = __cfactx_invoke_stub;
[09f357ec]148 fs->fixedRegisters[0] = main;
149 fs->fixedRegisters[1] = this;
150 fs->fixedRegisters[2] = invoke;
[b158d8f]151
152#elif defined( __ARM_ARCH )
[129674b]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
[b158d8f]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
[b2f6113]168 cor->context.SP = (char *)stack->base - sizeof( struct FakeStack );
169 cor->context.FP = NULL;
[b158d8f]170
[b2f6113]171 struct FakeStack *fs = (struct FakeStack *)cor->context.SP;
[b158d8f]172
[c7a900a]173 fs->intRegs[8] = __cfactx_invoke_stub;
[b158d8f]174 fs->arg[0] = this;
175 fs->arg[1] = invoke;
[381fdee]176
[d9c44c3]177#else
[381fdee]178 #error uknown hardware architecture
[d9c44c3]179#endif
[e4745d7a]180}
[6b0b624]181
182// Local Variables: //
183// mode: c //
184// tab-width: 4 //
185// End: //
Note: See TracBrowser for help on using the repository browser.