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