source: libcfa/src/concurrency/invoke.c @ 3c06bba

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

Fixed errno virtualization and enabled preemption during coroutine context switch

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