2021-01-22 09:21:25 -05:00
|
|
|
#ifndef COROUTINE_PPC64LE_CONTEXT_H
|
|
|
|
#define COROUTINE_PPC64LE_CONTEXT_H 1
|
|
|
|
|
2018-12-10 18:22:56 -05:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <assert.h>
|
2019-12-04 03:16:30 -05:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
2018-12-10 18:22:56 -05:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define COROUTINE __attribute__((noreturn)) void
|
|
|
|
|
2019-05-16 02:51:37 -04:00
|
|
|
enum {
|
|
|
|
COROUTINE_REGISTERS =
|
2018-12-10 18:22:56 -05:00
|
|
|
19 /* 18 general purpose registers (r14-r31) and 1 return address */
|
2019-05-16 02:51:37 -04:00
|
|
|
+ 4 /* space for fiber_entry() to store the link register */
|
|
|
|
};
|
2018-12-10 18:22:56 -05:00
|
|
|
|
2019-06-24 07:54:19 -04:00
|
|
|
struct coroutine_context
|
2018-12-10 18:22:56 -05:00
|
|
|
{
|
|
|
|
void **stack_pointer;
|
2021-06-25 18:17:26 -04:00
|
|
|
void *argument;
|
2019-06-24 07:54:19 -04:00
|
|
|
};
|
2018-12-10 18:22:56 -05:00
|
|
|
|
2019-06-24 07:54:19 -04:00
|
|
|
typedef COROUTINE(* coroutine_start)(struct coroutine_context *from, struct coroutine_context *self);
|
|
|
|
|
|
|
|
static inline void coroutine_initialize_main(struct coroutine_context * context) {
|
|
|
|
context->stack_pointer = NULL;
|
|
|
|
}
|
2018-12-10 18:22:56 -05:00
|
|
|
|
|
|
|
static inline void coroutine_initialize(
|
2019-06-24 07:54:19 -04:00
|
|
|
struct coroutine_context *context,
|
2018-12-10 18:22:56 -05:00
|
|
|
coroutine_start start,
|
2019-06-24 07:54:19 -04:00
|
|
|
void *stack,
|
|
|
|
size_t size
|
2018-12-10 18:22:56 -05:00
|
|
|
) {
|
2019-06-24 07:54:19 -04:00
|
|
|
assert(start && stack && size >= 1024);
|
2018-12-10 18:22:56 -05:00
|
|
|
|
2019-06-24 07:54:19 -04:00
|
|
|
// Stack grows down. Force 16-byte alignment.
|
|
|
|
char * top = (char*)stack + size;
|
|
|
|
context->stack_pointer = (void**)((uintptr_t)top & ~0xF);
|
2018-12-10 18:22:56 -05:00
|
|
|
|
|
|
|
context->stack_pointer -= COROUTINE_REGISTERS;
|
|
|
|
memset(context->stack_pointer, 0, sizeof(void*) * COROUTINE_REGISTERS);
|
|
|
|
|
|
|
|
/* Skip a global prologue that sets the TOC register */
|
|
|
|
context->stack_pointer[18] = ((char*)start) + 8;
|
|
|
|
}
|
|
|
|
|
2019-06-24 07:54:19 -04:00
|
|
|
struct coroutine_context * coroutine_transfer(struct coroutine_context * current, struct coroutine_context * target);
|
2018-12-10 18:22:56 -05:00
|
|
|
|
2019-06-24 07:54:19 -04:00
|
|
|
static inline void coroutine_destroy(struct coroutine_context * context)
|
2018-12-10 18:22:56 -05:00
|
|
|
{
|
|
|
|
context->stack_pointer = NULL;
|
|
|
|
}
|
2021-01-22 09:21:25 -05:00
|
|
|
|
|
|
|
#endif /* COROUTINE_PPC64LE_CONTEXT_H */
|