aboutsummaryrefslogtreecommitdiff
path: root/lib/arch/api.h
blob: 1b421cbbeb09f6771f6ff1f631131e7dff48b6f4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* Copyright (c) 2021 Johannes Stoelp */

#pragma once

#if !defined(linux)
static_assert(false, "Matcha Threads only supported on Linux!");
#endif

#if defined(__x86_64__) || defined(__amd64__)
// fall-through: x86_64 support
#elif defined(__aarch64__)
// fall-through: arm64 support
#elif defined(__arm__)
// fall-through: armv7 support
#elif defined(__riscv) && __riscv_xlen == 64
// fall-through: riscv64 support
#else
static_assert(false, "Unsupported architecture!");
#endif

// Platform specific API.
// When integrating a new platform the following functions must be implemented.

// Called once for every `Thread` object to setup the initial state of the
// threads stack.
//
// Arguments:
//   stack_ptr    Pointer to the bottom of the stack.
//   entry        Function pointer to the entry-point called on first `yield`.
//   ctx          Context passed to `entry`.
//
// Return:
//   Return pointer to the top of the stack after initialization.
//
// Requirements:
//   - Stack must be descending.
//   - Returned stack pointer must be aligned according to the platform specification.
//   - Stack must be setup in a way that a consequent call to `yield` will call
//     `entry` with `ctx` supplied as argument.
void* init_stack(void* stack_ptr, void (*entry)(void*), const void* ctx);

// Switch execution context from current stack to `new_stack`.
//
// Arguments:
//   new_stack    Stack pointer of new context to switch to.
//   old_stack    Pointer to location to store the current stack pointer.
extern "C" void yield(const void* new_stack, void** old_stack);