// -*- Mode: CFA -*- // // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo // // The contents of this file are covered under the licence agreement in the // file "LICENCE" distributed with Cforall. // // invoke.c -- // // Author : Thierry Delisle // Created On : Tue Jan 17 12:27:26 2016 // Last Modified By : Thierry Delisle // Last Modified On : -- // Update Count : 0 // #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. extern void __suspend_no_inline__F___1(void); extern void __signal_termination__F_P9sthread_h__1(struct thread*); void CtxInvokeCoroutine( void (*main)(void *), struct coroutine *(*get_coroutine)(void *), void *this ) { // LIB_DEBUG_PRINTF("Invoke Coroutine : Received %p (main %p, get_c %p)\n", this, main, get_coroutine); struct coroutine* cor = get_coroutine( this ); if(cor->state == Primed) { __suspend_no_inline__F___1(); } cor->state = Active; main( this ); cor->state = Halt; cor->notHalted = false; //Final suspend, should never return __suspend_no_inline__F___1(); assertf(false, "Resumed dead coroutine"); } void CtxInvokeThread( void (*main)(void *), struct thread *(*get_thread)(void *), void *this ) { // LIB_DEBUG_PRINTF("Invoke Thread : Received %p (main %p, get_t %p)\n", this, main, get_thread); __suspend_no_inline__F___1(); struct thread* thrd = get_thread( this ); struct coroutine* cor = &thrd->c; cor->state = Active; // LIB_DEBUG_PRINTF("Invoke Thread : invoking main %p (args %p)\n", main, this); main( this ); __signal_termination__F_P9sthread_h__1(thrd); //Final suspend, should never return __suspend_no_inline__F___1(); assertf(false, "Resumed dead thread"); } void CtxStart( void (*main)(void *), struct coroutine *(*get_coroutine)(void *), void *this, void (*invoke)(void *) ) { // LIB_DEBUG_PRINTF("StartCoroutine : Passing in %p (main %p) to invoke (%p) from start (%p)\n", this, main, invoke, CtxStart); struct coStack_t* stack = &get_coroutine( 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[3]; // for 16-byte ABI, 16-byte alignment starts here void *padding; // 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[0] = this; // 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 = CtxInvokeStub; ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[0] = this; ((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 }