| [0157ca7] | 1 | // -*- Mode: C -*-
|
|---|
| [8def349] | 2 | //
|
|---|
| 3 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
|
|---|
| 4 | //
|
|---|
| 5 | // The contents of this file are covered under the licence agreement in the
|
|---|
| 6 | // file "LICENCE" distributed with Cforall.
|
|---|
| 7 | //
|
|---|
| 8 | // invoke.c --
|
|---|
| 9 | //
|
|---|
| 10 | // Author : Thierry Delisle
|
|---|
| 11 | // Created On : Tue Jan 17 12:27:26 2016
|
|---|
| 12 | // Last Modified By : Thierry Delisle
|
|---|
| 13 | // Last Modified On : --
|
|---|
| 14 | // Update Count : 0
|
|---|
| 15 | //
|
|---|
| [78b3f52] | 16 |
|
|---|
| 17 | #include <stdbool.h>
|
|---|
| 18 | #include <stdlib.h>
|
|---|
| 19 | #include <stdio.h>
|
|---|
| 20 |
|
|---|
| [d9c44c3] | 21 | #include "libhdr.h"
|
|---|
| [78b3f52] | 22 | #include "invoke.h"
|
|---|
| 23 |
|
|---|
| [5c81105] | 24 | #define __CFA_INVOKE_PRIVATE__
|
|---|
| 25 | #include "invoke.h"
|
|---|
| [78b3f52] | 26 |
|
|---|
| 27 | // magically invoke the "main" of the most derived class
|
|---|
| 28 | // Called from the kernel when starting a coroutine or task so must switch back to user mode.
|
|---|
| [5c81105] | 29 |
|
|---|
| [0c92c9f] | 30 | extern void __suspend_internal(void);
|
|---|
| [348006f] | 31 | extern void __thread_signal_termination(struct thread_desc*);
|
|---|
| [78b3f52] | 32 |
|
|---|
| [b58a5772] | 33 | void CtxInvokeCoroutine(
|
|---|
| [80d9e49] | 34 | void (*main)(void *),
|
|---|
| [c3acb841] | 35 | struct coroutine_desc *(*get_coroutine)(void *),
|
|---|
| [80d9e49] | 36 | void *this
|
|---|
| 37 | ) {
|
|---|
| [c84e80a] | 38 | // LIB_DEBUG_PRINTF("Invoke Coroutine : Received %p (main %p, get_c %p)\n", this, main, get_coroutine);
|
|---|
| [80d9e49] | 39 |
|
|---|
| [c3acb841] | 40 | struct coroutine_desc* cor = get_coroutine( this );
|
|---|
| [80d9e49] | 41 |
|
|---|
| 42 | if(cor->state == Primed) {
|
|---|
| [0c92c9f] | 43 | __suspend_internal();
|
|---|
| [80d9e49] | 44 | }
|
|---|
| [78b3f52] | 45 |
|
|---|
| 46 | cor->state = Active;
|
|---|
| 47 |
|
|---|
| [80d9e49] | 48 | main( this );
|
|---|
| [8118303] | 49 |
|
|---|
| [ee897e4b] | 50 | cor->state = Halted;
|
|---|
| [8f49a54] | 51 |
|
|---|
| [8118303] | 52 | //Final suspend, should never return
|
|---|
| [0c92c9f] | 53 | __suspend_internal();
|
|---|
| 54 | abortf("Resumed dead coroutine");
|
|---|
| [8118303] | 55 | }
|
|---|
| 56 |
|
|---|
| 57 | void CtxInvokeThread(
|
|---|
| 58 | void (*main)(void *),
|
|---|
| [348006f] | 59 | struct thread_desc *(*get_thread)(void *),
|
|---|
| [8118303] | 60 | void *this
|
|---|
| 61 | ) {
|
|---|
| [0c92c9f] | 62 | __suspend_internal();
|
|---|
| [8118303] | 63 |
|
|---|
| [348006f] | 64 | struct thread_desc* thrd = get_thread( this );
|
|---|
| [c3acb841] | 65 | struct coroutine_desc* cor = &thrd->c;
|
|---|
| [8118303] | 66 | cor->state = Active;
|
|---|
| 67 |
|
|---|
| [c84e80a] | 68 | // LIB_DEBUG_PRINTF("Invoke Thread : invoking main %p (args %p)\n", main, this);
|
|---|
| [8118303] | 69 | main( this );
|
|---|
| 70 |
|
|---|
| [0c92c9f] | 71 | __thread_signal_termination(thrd);
|
|---|
| [c84e80a] | 72 |
|
|---|
| [8118303] | 73 | //Final suspend, should never return
|
|---|
| [0c92c9f] | 74 | __suspend_internal();
|
|---|
| 75 | abortf("Resumed dead thread");
|
|---|
| [78b3f52] | 76 | }
|
|---|
| 77 |
|
|---|
| [5c81105] | 78 |
|
|---|
| [b58a5772] | 79 | void CtxStart(
|
|---|
| [80d9e49] | 80 | void (*main)(void *),
|
|---|
| [c3acb841] | 81 | struct coroutine_desc *(*get_coroutine)(void *),
|
|---|
| [80d9e49] | 82 | void *this,
|
|---|
| 83 | void (*invoke)(void *)
|
|---|
| 84 | ) {
|
|---|
| [c84e80a] | 85 | // LIB_DEBUG_PRINTF("StartCoroutine : Passing in %p (main %p) to invoke (%p) from start (%p)\n", this, main, invoke, CtxStart);
|
|---|
| [5c81105] | 86 |
|
|---|
| [80d9e49] | 87 | struct coStack_t* stack = &get_coroutine( this )->stack;
|
|---|
| [78b3f52] | 88 |
|
|---|
| [d9c44c3] | 89 | #if defined( __i386__ )
|
|---|
| 90 |
|
|---|
| 91 | struct FakeStack {
|
|---|
| 92 | void *fixedRegisters[3]; // fixed registers ebx, edi, esi (popped on 1st uSwitch, values unimportant)
|
|---|
| [7b2c2c5f] | 93 | uint32_t mxcr; // SSE Status and Control bits (control bits are preserved across function calls)
|
|---|
| 94 | uint16_t fcw; // X97 FPU control word (preserved across function calls)
|
|---|
| 95 | void *rturn; // where to go on return from uSwitch
|
|---|
| [d9c44c3] | 96 | void *dummyReturn; // fake return compiler would have pushed on call to uInvoke
|
|---|
| [80d9e49] | 97 | void *argument[3]; // for 16-byte ABI, 16-byte alignment starts here
|
|---|
| 98 | void *padding; // padding to force 16-byte alignment, as "base" is 16-byte aligned
|
|---|
| [d9c44c3] | 99 | };
|
|---|
| 100 |
|
|---|
| 101 | ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack );
|
|---|
| 102 | ((struct machine_context_t *)stack->context)->FP = NULL; // terminate stack with NULL fp
|
|---|
| 103 |
|
|---|
| 104 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL;
|
|---|
| [80d9e49] | 105 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->argument[0] = this; // argument to invoke
|
|---|
| [d9c44c3] | 106 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = invoke;
|
|---|
| 107 |
|
|---|
| 108 | #elif defined( __x86_64__ )
|
|---|
| 109 |
|
|---|
| [78b3f52] | 110 | struct FakeStack {
|
|---|
| 111 | void *fixedRegisters[5]; // fixed registers rbx, r12, r13, r14, r15
|
|---|
| [7b2c2c5f] | 112 | uint32_t mxcr; // SSE Status and Control bits (control bits are preserved across function calls)
|
|---|
| 113 | uint16_t fcw; // X97 FPU control word (preserved across function calls)
|
|---|
| 114 | void *rturn; // where to go on return from uSwitch
|
|---|
| [78b3f52] | 115 | void *dummyReturn; // NULL return address to provide proper alignment
|
|---|
| [d9c44c3] | 116 | };
|
|---|
| [78b3f52] | 117 |
|
|---|
| 118 | ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack );
|
|---|
| 119 | ((struct machine_context_t *)stack->context)->FP = NULL; // terminate stack with NULL fp
|
|---|
| 120 |
|
|---|
| 121 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL;
|
|---|
| [b58a5772] | 122 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = CtxInvokeStub;
|
|---|
| [80d9e49] | 123 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[0] = this;
|
|---|
| [5c81105] | 124 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[1] = invoke;
|
|---|
| [8761006c] | 125 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520
|
|---|
| 126 | ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F; //Vol. 1 8-7
|
|---|
| [d9c44c3] | 127 | #else
|
|---|
| 128 | #error Only __i386__ and __x86_64__ is supported for threads in cfa
|
|---|
| 129 | #endif
|
|---|
| [e4745d7a] | 130 | }
|
|---|