|
| 1 | +#include <assert.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <string.h> |
| 4 | + |
| 5 | +#include <unistd.h> |
| 6 | +#include <sys/mman.h> |
| 7 | + |
| 8 | +#include "coroutine.h" |
| 9 | + |
| 10 | +#define COROUTINE_DA_INIT_CAP 256 |
| 11 | +// Append an item to a dynamic array |
| 12 | +#define coroutine_da_append(da, item) \ |
| 13 | + do { \ |
| 14 | + if ((da)->count >= (da)->capacity) { \ |
| 15 | + (da)->capacity = (da)->capacity == 0 ? COROUTINE_DA_INIT_CAP : (da)->capacity*2; \ |
| 16 | + (da)->items = realloc((da)->items, (da)->capacity*sizeof(*(da)->items)); \ |
| 17 | + assert((da)->items != NULL && "Buy more RAM lol"); \ |
| 18 | + } \ |
| 19 | + \ |
| 20 | + (da)->items[(da)->count++] = (item); \ |
| 21 | + } while (0) |
| 22 | +#define COROUTINE_UNREACHABLE(message) do { fprintf(stderr, "%s:%d: UNREACHABLE: %s\n", __FILE__, __LINE__, message); abort(); } while(0) |
| 23 | + |
| 24 | +Stack *coroutine_init(void) |
| 25 | +{ |
| 26 | + Stack *stack = malloc(sizeof(*stack)); |
| 27 | + memset(stack, 0, sizeof(*stack)); |
| 28 | + |
| 29 | + Coroutine *c = malloc(sizeof(*c)); |
| 30 | + memset(c, 0, sizeof(*c)); |
| 31 | + coroutine_da_append(stack, c); |
| 32 | + |
| 33 | + return stack; |
| 34 | +} |
| 35 | + |
| 36 | +void coroutine_deinit(Stack *stack) |
| 37 | +{ |
| 38 | + assert(stack->count == 1); |
| 39 | + free(stack->items[0]); |
| 40 | + free(stack->items); |
| 41 | + free(stack); |
| 42 | +} |
| 43 | + |
| 44 | +void __attribute__((naked)) coroutine_restore_context(void *rsp) |
| 45 | +{ |
| 46 | + // @arch |
| 47 | + (void)rsp; |
| 48 | + asm( |
| 49 | + " movq %rdi, %rsp\n" |
| 50 | + " popq %r15\n" |
| 51 | + " popq %r14\n" |
| 52 | + " popq %r13\n" |
| 53 | + " popq %r12\n" |
| 54 | + " popq %rbx\n" |
| 55 | + " popq %rbp\n" |
| 56 | + " popq %rsi\n" |
| 57 | + " popq %rdi\n" |
| 58 | + " ret\n"); |
| 59 | +} |
| 60 | + |
| 61 | +void coroutine_pop_frame(Stack *stack, void *rsp) |
| 62 | +{ |
| 63 | + assert(stack->count > 0); |
| 64 | + stack->items[stack->count - 1]->rsp = rsp; |
| 65 | + stack->count -= 1; |
| 66 | + assert(stack->count > 0); |
| 67 | + coroutine_restore_context(stack->items[stack->count - 1]->rsp); |
| 68 | +} |
| 69 | + |
| 70 | +void __attribute__((naked)) coroutine__finish_current(void) |
| 71 | +{ |
| 72 | + register Stack *stack = NULL; |
| 73 | + asm("popq %0" : "=r" (stack)); |
| 74 | + assert(stack->count > 0); |
| 75 | + stack->items[stack->count-1]->finished = true; |
| 76 | + coroutine_yield(stack); |
| 77 | + COROUTINE_UNREACHABLE("Finished coroutine was resumed!!!"); |
| 78 | +} |
| 79 | + |
| 80 | +Coroutine *coroutine_create(Stack *stack, void (*f)(Stack*, void*), void *arg) |
| 81 | +{ |
| 82 | + Coroutine *c = malloc(sizeof(*c)); |
| 83 | + memset(c, 0, sizeof(*c)); |
| 84 | + c->stack_base = mmap(NULL, STACK_CAPACITY, PROT_WRITE|PROT_READ, MAP_PRIVATE|MAP_STACK|MAP_ANONYMOUS|MAP_GROWSDOWN, -1, 0); |
| 85 | + assert(c->stack_base != MAP_FAILED); |
| 86 | + void **rsp = (void**)((char*)c->stack_base + STACK_CAPACITY); |
| 87 | + // @arch |
| 88 | + *(--rsp) = 0; // pad |
| 89 | + *(--rsp) = stack; |
| 90 | + *(--rsp) = coroutine__finish_current; |
| 91 | + *(--rsp) = f; |
| 92 | + *(--rsp) = stack; // push rdi |
| 93 | + *(--rsp) = arg; // push rsi |
| 94 | + *(--rsp) = 0; // push rbx |
| 95 | + *(--rsp) = 0; // push rbp |
| 96 | + *(--rsp) = 0; // push r12 |
| 97 | + *(--rsp) = 0; // push r13 |
| 98 | + *(--rsp) = 0; // push r14 |
| 99 | + *(--rsp) = 0; // push r15 |
| 100 | + c->rsp = rsp; |
| 101 | + |
| 102 | + return c; |
| 103 | +} |
| 104 | + |
| 105 | +void coroutine_destroy(Coroutine *c) |
| 106 | +{ |
| 107 | + munmap(c->stack_base, STACK_CAPACITY); |
| 108 | + free(c); |
| 109 | +} |
| 110 | + |
| 111 | +void __attribute__((naked)) coroutine_resume(Stack *stack, Coroutine *c) |
| 112 | +{ |
| 113 | + (void) stack; |
| 114 | + (void) c; |
| 115 | + // @arch |
| 116 | + asm( |
| 117 | + " pushq %rdi\n" |
| 118 | + " pushq %rsi\n" |
| 119 | + " pushq %rbp\n" |
| 120 | + " pushq %rbx\n" |
| 121 | + " pushq %r12\n" |
| 122 | + " pushq %r13\n" |
| 123 | + " pushq %r14\n" |
| 124 | + " pushq %r15\n" |
| 125 | + " movq %rsp, %rdx\n" |
| 126 | + " jmp coroutine_push_frame\n"); |
| 127 | +} |
| 128 | + |
| 129 | +void coroutine_push_frame(Stack *stack, Coroutine *c, void *rsp) |
| 130 | +{ |
| 131 | + assert(stack->count > 0); |
| 132 | + stack->items[stack->count - 1]->rsp = rsp; |
| 133 | + |
| 134 | + coroutine_da_append(stack, c); |
| 135 | + coroutine_restore_context(c->rsp); |
| 136 | +} |
| 137 | + |
| 138 | +void __attribute__((naked)) coroutine_yield(Stack *stack) |
| 139 | +{ |
| 140 | + (void) stack; |
| 141 | + // @arch |
| 142 | + asm( |
| 143 | + " pushq %rdi\n" |
| 144 | + " pushq %rsi\n" |
| 145 | + " pushq %rbp\n" |
| 146 | + " pushq %rbx\n" |
| 147 | + " pushq %r12\n" |
| 148 | + " pushq %r13\n" |
| 149 | + " pushq %r14\n" |
| 150 | + " pushq %r15\n" |
| 151 | + " movq %rsp, %rsi\n" // rsp |
| 152 | + " jmp coroutine_pop_frame\n"); |
| 153 | +} |
0 commit comments