[78b3f52] | 1 | |
---|
| 2 | #include <stdbool.h> |
---|
| 3 | #include <stdlib.h> |
---|
| 4 | #include <stdio.h> |
---|
| 5 | |
---|
[d9c44c3] | 6 | #include "libhdr.h" |
---|
[78b3f52] | 7 | #include "invoke.h" |
---|
| 8 | |
---|
[5c81105] | 9 | #define __CFA_INVOKE_PRIVATE__ |
---|
| 10 | #include "invoke.h" |
---|
[78b3f52] | 11 | |
---|
| 12 | // magically invoke the "main" of the most derived class |
---|
| 13 | // Called from the kernel when starting a coroutine or task so must switch back to user mode. |
---|
[5c81105] | 14 | |
---|
[596f987b] | 15 | extern void __suspend_no_inline__F___1(void); |
---|
[bd98b58] | 16 | extern void __signal_termination__F_P9sthread_h__1(struct thread_h*); |
---|
[78b3f52] | 17 | |
---|
[b58a5772] | 18 | void CtxInvokeCoroutine( |
---|
[80d9e49] | 19 | void (*main)(void *), |
---|
| 20 | struct coroutine *(*get_coroutine)(void *), |
---|
| 21 | void *this |
---|
| 22 | ) { |
---|
[c84e80a] | 23 | // LIB_DEBUG_PRINTF("Invoke Coroutine : Received %p (main %p, get_c %p)\n", this, main, get_coroutine); |
---|
[80d9e49] | 24 | |
---|
| 25 | struct coroutine* cor = get_coroutine( this ); |
---|
| 26 | |
---|
| 27 | if(cor->state == Primed) { |
---|
[596f987b] | 28 | __suspend_no_inline__F___1(); |
---|
[80d9e49] | 29 | } |
---|
[78b3f52] | 30 | |
---|
| 31 | cor->state = Active; |
---|
| 32 | |
---|
[80d9e49] | 33 | main( this ); |
---|
[8118303] | 34 | |
---|
[8f49a54] | 35 | cor->state = Halt; |
---|
| 36 | cor->notHalted = false; |
---|
| 37 | |
---|
[8118303] | 38 | //Final suspend, should never return |
---|
| 39 | __suspend_no_inline__F___1(); |
---|
| 40 | assertf(false, "Resumed dead coroutine"); |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | void CtxInvokeThread( |
---|
| 44 | void (*main)(void *), |
---|
| 45 | struct thread_h *(*get_thread)(void *), |
---|
| 46 | void *this |
---|
| 47 | ) { |
---|
[c84e80a] | 48 | // LIB_DEBUG_PRINTF("Invoke Thread : Received %p (main %p, get_t %p)\n", this, main, get_thread); |
---|
[8118303] | 49 | |
---|
| 50 | __suspend_no_inline__F___1(); |
---|
| 51 | |
---|
[c84e80a] | 52 | struct thread_h* thrd = get_thread( this ); |
---|
| 53 | struct coroutine* cor = &thrd->c; |
---|
[8118303] | 54 | cor->state = Active; |
---|
| 55 | |
---|
[c84e80a] | 56 | // LIB_DEBUG_PRINTF("Invoke Thread : invoking main %p (args %p)\n", main, this); |
---|
[8118303] | 57 | main( this ); |
---|
| 58 | |
---|
[bd98b58] | 59 | __signal_termination__F_P9sthread_h__1(thrd); |
---|
[c84e80a] | 60 | |
---|
[8118303] | 61 | //Final suspend, should never return |
---|
| 62 | __suspend_no_inline__F___1(); |
---|
| 63 | assertf(false, "Resumed dead thread"); |
---|
[78b3f52] | 64 | } |
---|
| 65 | |
---|
[5c81105] | 66 | |
---|
[b58a5772] | 67 | void CtxStart( |
---|
[80d9e49] | 68 | void (*main)(void *), |
---|
| 69 | struct coroutine *(*get_coroutine)(void *), |
---|
| 70 | void *this, |
---|
| 71 | void (*invoke)(void *) |
---|
| 72 | ) { |
---|
[c84e80a] | 73 | // LIB_DEBUG_PRINTF("StartCoroutine : Passing in %p (main %p) to invoke (%p) from start (%p)\n", this, main, invoke, CtxStart); |
---|
[5c81105] | 74 | |
---|
[80d9e49] | 75 | struct coStack_t* stack = &get_coroutine( this )->stack; |
---|
[78b3f52] | 76 | |
---|
[d9c44c3] | 77 | #if defined( __i386__ ) |
---|
| 78 | |
---|
| 79 | struct FakeStack { |
---|
| 80 | void *fixedRegisters[3]; // fixed registers ebx, edi, esi (popped on 1st uSwitch, values unimportant) |
---|
| 81 | void *rturn; // where to go on return from uSwitch |
---|
| 82 | void *dummyReturn; // fake return compiler would have pushed on call to uInvoke |
---|
[80d9e49] | 83 | void *argument[3]; // for 16-byte ABI, 16-byte alignment starts here |
---|
| 84 | void *padding; // padding to force 16-byte alignment, as "base" is 16-byte aligned |
---|
[d9c44c3] | 85 | }; |
---|
| 86 | |
---|
| 87 | ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack ); |
---|
| 88 | ((struct machine_context_t *)stack->context)->FP = NULL; // terminate stack with NULL fp |
---|
| 89 | |
---|
| 90 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL; |
---|
[80d9e49] | 91 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->argument[0] = this; // argument to invoke |
---|
[d9c44c3] | 92 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = invoke; |
---|
| 93 | |
---|
| 94 | #elif defined( __x86_64__ ) |
---|
| 95 | |
---|
[78b3f52] | 96 | struct FakeStack { |
---|
| 97 | void *fixedRegisters[5]; // fixed registers rbx, r12, r13, r14, r15 |
---|
| 98 | void *rturn; // where to go on return from uSwitch |
---|
| 99 | void *dummyReturn; // NULL return address to provide proper alignment |
---|
[d9c44c3] | 100 | }; |
---|
[78b3f52] | 101 | |
---|
| 102 | ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack ); |
---|
| 103 | ((struct machine_context_t *)stack->context)->FP = NULL; // terminate stack with NULL fp |
---|
| 104 | |
---|
| 105 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL; |
---|
[b58a5772] | 106 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = CtxInvokeStub; |
---|
[80d9e49] | 107 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[0] = this; |
---|
[5c81105] | 108 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[1] = invoke; |
---|
[d9c44c3] | 109 | #else |
---|
| 110 | #error Only __i386__ and __x86_64__ is supported for threads in cfa |
---|
| 111 | #endif |
---|
[e4745d7a] | 112 | } |
---|