[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 | |
---|
| 15 | void invokeCoroutine(covptr_t* vthis) |
---|
[78b3f52] | 16 | { |
---|
[5c81105] | 17 | LIB_DEBUG_PRINTF("Invoke : Received %p (v %p)\n", vthis, *vthis); |
---|
[78b3f52] | 18 | |
---|
[5c81105] | 19 | struct coroutine* cor = get_coroutine( vthis ); |
---|
[78b3f52] | 20 | |
---|
| 21 | cor->state = Active; |
---|
| 22 | |
---|
[5c81105] | 23 | (*vthis)->main( get_object(vthis) ); |
---|
[78b3f52] | 24 | } |
---|
| 25 | |
---|
[5c81105] | 26 | |
---|
| 27 | void startCoroutine(covptr_t* vthis, void (*invoke)(covptr_t*)) { |
---|
| 28 | LIB_DEBUG_PRINTF("StartCoroutine : Passing in %p (v %p) to %p\n", vthis, *vthis, invoke); |
---|
| 29 | |
---|
| 30 | struct coroutine* this = get_coroutine( vthis ); |
---|
[78b3f52] | 31 | struct coStack_t* stack = &this->stack; |
---|
| 32 | |
---|
[d9c44c3] | 33 | #if defined( __i386__ ) |
---|
| 34 | |
---|
| 35 | struct FakeStack { |
---|
| 36 | void *fixedRegisters[3]; // fixed registers ebx, edi, esi (popped on 1st uSwitch, values unimportant) |
---|
| 37 | void *rturn; // where to go on return from uSwitch |
---|
| 38 | void *dummyReturn; // fake return compiler would have pushed on call to uInvoke |
---|
[5c81105] | 39 | void *argument; // for 16-byte ABI, 16-byte alignment starts here |
---|
| 40 | void *padding[3]; // padding to force 16-byte alignment, as "base" is 16-byte aligned |
---|
[d9c44c3] | 41 | }; |
---|
| 42 | |
---|
| 43 | ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack ); |
---|
| 44 | ((struct machine_context_t *)stack->context)->FP = NULL; // terminate stack with NULL fp |
---|
| 45 | |
---|
| 46 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL; |
---|
[5c81105] | 47 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->argument = vthis; // argument to invoke |
---|
[d9c44c3] | 48 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = invoke; |
---|
| 49 | |
---|
| 50 | #elif defined( __x86_64__ ) |
---|
| 51 | |
---|
[78b3f52] | 52 | struct FakeStack { |
---|
| 53 | void *fixedRegisters[5]; // fixed registers rbx, r12, r13, r14, r15 |
---|
| 54 | void *rturn; // where to go on return from uSwitch |
---|
| 55 | void *dummyReturn; // NULL return address to provide proper alignment |
---|
[d9c44c3] | 56 | }; |
---|
[78b3f52] | 57 | |
---|
| 58 | ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack ); |
---|
| 59 | ((struct machine_context_t *)stack->context)->FP = NULL; // terminate stack with NULL fp |
---|
| 60 | |
---|
| 61 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL; |
---|
| 62 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = coInvokeStub; |
---|
[5c81105] | 63 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[0] = vthis; |
---|
| 64 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[1] = invoke; |
---|
[d9c44c3] | 65 | #else |
---|
| 66 | #error Only __i386__ and __x86_64__ is supported for threads in cfa |
---|
| 67 | #endif |
---|
[e4745d7a] | 68 | } |
---|