#include #include #include #include "libhdr.h" #include "invoke.h" #define __CFA_INVOKE_PRIVATE__ #include "invoke.h" // magically invoke the "main" of the most derived class // Called from the kernel when starting a coroutine or task so must switch back to user mode. void invokeCoroutine(covptr_t* vthis) { LIB_DEBUG_PRINTF("Invoke : Received %p (v %p)\n", vthis, *vthis); struct coroutine* cor = get_coroutine( vthis ); cor->state = Active; (*vthis)->main( get_object(vthis) ); } void startCoroutine(covptr_t* vthis, void (*invoke)(covptr_t*)) { LIB_DEBUG_PRINTF("StartCoroutine : Passing in %p (v %p) to %p\n", vthis, *vthis, invoke); struct coroutine* this = get_coroutine( vthis ); struct coStack_t* stack = &this->stack; #if defined( __i386__ ) struct FakeStack { void *fixedRegisters[3]; // fixed registers ebx, edi, esi (popped on 1st uSwitch, values unimportant) void *rturn; // where to go on return from uSwitch void *dummyReturn; // fake return compiler would have pushed on call to uInvoke void *argument; // for 16-byte ABI, 16-byte alignment starts here void *padding[3]; // padding to force 16-byte alignment, as "base" is 16-byte aligned }; ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack ); ((struct machine_context_t *)stack->context)->FP = NULL; // terminate stack with NULL fp ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL; ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->argument = vthis; // argument to invoke ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = invoke; #elif defined( __x86_64__ ) struct FakeStack { void *fixedRegisters[5]; // fixed registers rbx, r12, r13, r14, r15 void *rturn; // where to go on return from uSwitch void *dummyReturn; // NULL return address to provide proper alignment }; ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack ); ((struct machine_context_t *)stack->context)->FP = NULL; // terminate stack with NULL fp ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL; ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = coInvokeStub; ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[0] = vthis; ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[1] = invoke; #else #error Only __i386__ and __x86_64__ is supported for threads in cfa #endif }