source: src/libcfa/concurrency/invoke.c @ 539cdfe

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 539cdfe was b158d8f, checked in by Alan Kennedy <afakenne@…>, 6 years ago

add context switch for ARM

  • Property mode set to 100644
File size: 5.6 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 : Tue Jan 23 14:04:56 2018
13// Update Count     : 2
14//
15
16#include <stdbool.h>
17#include <stdlib.h>
18#include <stdio.h>
19
20#include "invoke.h"
21
22#define __CFA_INVOKE_PRIVATE__
23#include "invoke.h"
24
25// magically invoke the "main" of the most derived class
26// Called from the kernel when starting a coroutine or task so must switch back to user mode.
27
28extern void __suspend_internal(void);
29extern void __leave_coroutine(void);
30extern void __leave_thread_monitor( struct thread_desc * this );
31extern void disable_interrupts();
32extern void enable_interrupts( __cfaabi_dbg_ctx_param );
33
34void CtxInvokeCoroutine(
35        void (*main)(void *),
36        struct coroutine_desc *(*get_coroutine)(void *),
37        void *this
38) {
39        struct coroutine_desc* cor = get_coroutine( this );
40
41        if(cor->state == Primed) {
42                __suspend_internal();
43        }
44
45        cor->state = Active;
46
47        main( this );
48
49        cor->state = Halted;
50
51        //Final suspend, should never return
52        __leave_coroutine();
53        abortf("Resumed dead coroutine");
54}
55
56void CtxInvokeThread(
57        void (*dtor)(void *),
58        void (*main)(void *),
59        struct thread_desc *(*get_thread)(void *),
60        void *this
61) {
62        // First suspend, once the thread arrives here,
63        // the function pointer to main can be invalidated without risk
64        __suspend_internal();
65
66        // Fetch the thread handle from the user defined thread structure
67        struct thread_desc* thrd = get_thread( this );
68
69        // Officially start the thread by enabling preemption
70        enable_interrupts( __cfaabi_dbg_ctx );
71
72        // Call the main of the thread
73        main( this );
74
75        // To exit a thread we must :
76        // 1 - Mark it as halted
77        // 2 - Leave its monitor
78        // 3 - Disable the interupts
79        // 4 - Final suspend
80        // The order of these 4 operations is very important
81        //Final suspend, should never return
82        __leave_thread_monitor( thrd );
83        abortf("Resumed dead thread");
84}
85
86
87void CtxStart(
88        void (*main)(void *),
89        struct coroutine_desc *(*get_coroutine)(void *),
90        void *this,
91        void (*invoke)(void *)
92) {
93        struct coStack_t* stack = &get_coroutine( this )->stack;
94
95#if defined( __i386__ )
96
97        struct FakeStack {
98            void *fixedRegisters[3];                    // fixed registers ebx, edi, esi (popped on 1st uSwitch, values unimportant)
99            uint32_t mxcr;                        // SSE Status and Control bits (control bits are preserved across function calls)
100            uint16_t fcw;                         // X97 FPU control word (preserved across function calls)
101            void *rturn;                          // where to go on return from uSwitch
102            void *dummyReturn;                          // fake return compiler would have pushed on call to uInvoke
103            void *argument[3];                          // for 16-byte ABI, 16-byte alignment starts here
104            void *padding;                              // padding to force 16-byte alignment, as "base" is 16-byte aligned
105        };
106
107        ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack );
108        ((struct machine_context_t *)stack->context)->FP = NULL;                // terminate stack with NULL fp
109
110        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL;
111        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->argument[0] = this;     // argument to invoke
112        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = invoke;
113        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520
114        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F;  //Vol. 1 8-7
115
116#elif defined( __x86_64__ )
117
118        struct FakeStack {
119                void *fixedRegisters[5];            // fixed registers rbx, r12, r13, r14, r15
120                uint32_t mxcr;                      // SSE Status and Control bits (control bits are preserved across function calls)
121                uint16_t fcw;                       // X97 FPU control word (preserved across function calls)
122                void *rturn;                        // where to go on return from uSwitch
123                void *dummyReturn;                  // NULL return address to provide proper alignment
124        };
125
126        ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack );
127        ((struct machine_context_t *)stack->context)->FP = NULL;                // terminate stack with NULL fp
128
129        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL;
130        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = CtxInvokeStub;
131        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[0] = this;
132        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[1] = invoke;
133        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520
134        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F;  //Vol. 1 8-7
135
136#elif defined( __ARM_ARCH )
137
138        struct FakeStack {
139                float fpRegs[16];                       // floating point registers
140                void *intRegs[9];                       // integer/pointer registers
141                void *arg[2];                           // placeholder for this pointer
142        };
143
144        ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack );
145        ((struct machine_context_t *)stack->context)->FP = NULL;
146
147        struct FakeStack *fs = (struct FakeStack *)((struct machine_context_t *)stack->context)->SP;
148
149        fs->intRegs[8] = CtxInvokeStub;
150        fs->arg[0] = this;
151        fs->arg[1] = invoke;
152#else
153        #error Only __i386__ and __x86_64__ is supported for threads in cfa
154#endif
155}
156
157// Local Variables: //
158// mode: c //
159// tab-width: 4 //
160// End: //
Note: See TracBrowser for help on using the repository browser.