[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);
|
---|
[c84e80a] | 16 | extern void __scheduler_remove__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 |
|
---|
| 35 | //Final suspend, should never return
|
---|
| 36 | __suspend_no_inline__F___1();
|
---|
| 37 | assertf(false, "Resumed dead coroutine");
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | void CtxInvokeThread(
|
---|
| 41 | void (*main)(void *),
|
---|
| 42 | struct thread_h *(*get_thread)(void *),
|
---|
| 43 | void *this
|
---|
| 44 | ) {
|
---|
[c84e80a] | 45 | // LIB_DEBUG_PRINTF("Invoke Thread : Received %p (main %p, get_t %p)\n", this, main, get_thread);
|
---|
[8118303] | 46 |
|
---|
| 47 | __suspend_no_inline__F___1();
|
---|
| 48 |
|
---|
[c84e80a] | 49 | struct thread_h* thrd = get_thread( this );
|
---|
| 50 | struct coroutine* cor = &thrd->c;
|
---|
[8118303] | 51 | cor->state = Active;
|
---|
| 52 |
|
---|
[c84e80a] | 53 | // LIB_DEBUG_PRINTF("Invoke Thread : invoking main %p (args %p)\n", main, this);
|
---|
[8118303] | 54 | main( this );
|
---|
| 55 |
|
---|
[c84e80a] | 56 | __scheduler_remove__F_P9sthread_h__1(thrd);
|
---|
| 57 |
|
---|
[8118303] | 58 | //Final suspend, should never return
|
---|
| 59 | __suspend_no_inline__F___1();
|
---|
| 60 | assertf(false, "Resumed dead thread");
|
---|
[78b3f52] | 61 | }
|
---|
| 62 |
|
---|
[5c81105] | 63 |
|
---|
[b58a5772] | 64 | void CtxStart(
|
---|
[80d9e49] | 65 | void (*main)(void *),
|
---|
| 66 | struct coroutine *(*get_coroutine)(void *),
|
---|
| 67 | void *this,
|
---|
| 68 | void (*invoke)(void *)
|
---|
| 69 | ) {
|
---|
[c84e80a] | 70 | // LIB_DEBUG_PRINTF("StartCoroutine : Passing in %p (main %p) to invoke (%p) from start (%p)\n", this, main, invoke, CtxStart);
|
---|
[5c81105] | 71 |
|
---|
[80d9e49] | 72 | struct coStack_t* stack = &get_coroutine( this )->stack;
|
---|
[78b3f52] | 73 |
|
---|
[d9c44c3] | 74 | #if defined( __i386__ )
|
---|
| 75 |
|
---|
| 76 | struct FakeStack {
|
---|
| 77 | void *fixedRegisters[3]; // fixed registers ebx, edi, esi (popped on 1st uSwitch, values unimportant)
|
---|
| 78 | void *rturn; // where to go on return from uSwitch
|
---|
| 79 | void *dummyReturn; // fake return compiler would have pushed on call to uInvoke
|
---|
[80d9e49] | 80 | void *argument[3]; // for 16-byte ABI, 16-byte alignment starts here
|
---|
| 81 | void *padding; // padding to force 16-byte alignment, as "base" is 16-byte aligned
|
---|
[d9c44c3] | 82 | };
|
---|
| 83 |
|
---|
| 84 | ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack );
|
---|
| 85 | ((struct machine_context_t *)stack->context)->FP = NULL; // terminate stack with NULL fp
|
---|
| 86 |
|
---|
| 87 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL;
|
---|
[80d9e49] | 88 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->argument[0] = this; // argument to invoke
|
---|
[d9c44c3] | 89 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = invoke;
|
---|
| 90 |
|
---|
| 91 | #elif defined( __x86_64__ )
|
---|
| 92 |
|
---|
[78b3f52] | 93 | struct FakeStack {
|
---|
| 94 | void *fixedRegisters[5]; // fixed registers rbx, r12, r13, r14, r15
|
---|
| 95 | void *rturn; // where to go on return from uSwitch
|
---|
| 96 | void *dummyReturn; // NULL return address to provide proper alignment
|
---|
[d9c44c3] | 97 | };
|
---|
[78b3f52] | 98 |
|
---|
| 99 | ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack );
|
---|
| 100 | ((struct machine_context_t *)stack->context)->FP = NULL; // terminate stack with NULL fp
|
---|
| 101 |
|
---|
| 102 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL;
|
---|
[b58a5772] | 103 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = CtxInvokeStub;
|
---|
[80d9e49] | 104 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[0] = this;
|
---|
[5c81105] | 105 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[1] = invoke;
|
---|
[d9c44c3] | 106 | #else
|
---|
| 107 | #error Only __i386__ and __x86_64__ is supported for threads in cfa
|
---|
| 108 | #endif
|
---|
[e4745d7a] | 109 | }
|
---|