diff options
author | johannst <johannes.stoelp@gmail.com> | 2021-06-09 23:05:05 +0200 |
---|---|---|
committer | johannst <johannes.stoelp@gmail.com> | 2021-06-09 23:05:05 +0200 |
commit | 2e4a31287705d8437cddd109c12b7e11825192bc (patch) | |
tree | 448157595deb0b6441abebff45990dc9bd3558e6 /lib/arch/riscv64 | |
parent | 0db708a285c74526ebcce31a10f5730f3f715d61 (diff) | |
download | matcha-threads-2e4a31287705d8437cddd109c12b7e11825192bc.tar.gz matcha-threads-2e4a31287705d8437cddd109c12b7e11825192bc.zip |
added riscv64
Diffstat (limited to 'lib/arch/riscv64')
-rw-r--r-- | lib/arch/riscv64/README.md | 12 | ||||
-rw-r--r-- | lib/arch/riscv64/init_stack.cc | 39 | ||||
-rw-r--r-- | lib/arch/riscv64/thread_create.s | 19 | ||||
-rw-r--r-- | lib/arch/riscv64/yield.s | 63 |
4 files changed, 133 insertions, 0 deletions
diff --git a/lib/arch/riscv64/README.md b/lib/arch/riscv64/README.md new file mode 100644 index 0000000..517ddb2 --- /dev/null +++ b/lib/arch/riscv64/README.md @@ -0,0 +1,12 @@ +# riscv64 (RISC-V ELF) +Provide an implementation of the platform specific API as required in +[`api.h`](../api.h) according to the `RISC-V ELF ABI`. + +## Notes: RISC-V ABI +- Integer/pointer arguments via `x10 - x17` +- Integer/pointer return values via `x10` - `x11` +- Callee saved registers `x2`, `x8` - `x9`, `x18` - `x27`, `f8` - `f9`, `f18` - `f27` + +## References +- [RISC-V ABI](https://github.com/riscv/riscv-elf-psabi-doc/blob/master/riscv-elf.md) +- [RISC-V asm manual](https://github.com/riscv/riscv-asm-manual/blob/master/riscv-asm.md) diff --git a/lib/arch/riscv64/init_stack.cc b/lib/arch/riscv64/init_stack.cc new file mode 100644 index 0000000..94dab1c --- /dev/null +++ b/lib/arch/riscv64/init_stack.cc @@ -0,0 +1,39 @@ +/* Copyright (c) 2021 Johannes Stoelp */ + +#include <cassert> +#include <cstdint> // uintN_t + +extern "C" void thread_create(); + +void* init_stack(void* stack_ptr, void (*entry)(void*), const void* ctx) { + static_assert(sizeof(uint64_t) == sizeof(std::uintptr_t), "Pointer must be 64bit!"); + + // Setup initial stack frame which will be popped when yielding + // first time into the thread. + // Basic idea is to yield into Thread::entry() function which will + // then call the user function. + + uint64_t* stack = static_cast<uint64_t*>(stack_ptr); + // Arguments for `thread_create`. + *(--stack) = reinterpret_cast<uint64_t>(ctx); + *(--stack) = reinterpret_cast<uint64_t>(entry); + + // Yield epilogue. + *(--stack) = reinterpret_cast<uint64_t>(thread_create); // Return address (ra) + + // Callee saved registers. + *(--stack) = static_cast<uint64_t>(0); // x8 + *(--stack) = static_cast<uint64_t>(0); // x9 + *(--stack) = static_cast<uint64_t>(0); // x18 + *(--stack) = static_cast<uint64_t>(0); // x19 + *(--stack) = static_cast<uint64_t>(0); // x20 + *(--stack) = static_cast<uint64_t>(0); // x21 + *(--stack) = static_cast<uint64_t>(0); // x22 + *(--stack) = static_cast<uint64_t>(0); // x23 + *(--stack) = static_cast<uint64_t>(0); // x24 + *(--stack) = static_cast<uint64_t>(0); // x25 + *(--stack) = static_cast<uint64_t>(0); // x26 + *(--stack) = static_cast<uint64_t>(0); // x27 + + return static_cast<void*>(stack); +} diff --git a/lib/arch/riscv64/thread_create.s b/lib/arch/riscv64/thread_create.s new file mode 100644 index 0000000..68c09cd --- /dev/null +++ b/lib/arch/riscv64/thread_create.s @@ -0,0 +1,19 @@ +# Copyright (c) 2021 Johannes Stoelp + + .section .text, "ax", @progbits + + # extern "C" void thread_create(); + .global thread_create + .type thread_create, @function +thread_create: + .cfi_startproc + ld a0, 8(sp) + ld x5, 0(sp) + + jalr x5 + + # FIXME: no return from thread after user fn finished. +1: + j 1b + .cfi_endproc + .size thread_create, .-thread_create diff --git a/lib/arch/riscv64/yield.s b/lib/arch/riscv64/yield.s new file mode 100644 index 0000000..985cb52 --- /dev/null +++ b/lib/arch/riscv64/yield.s @@ -0,0 +1,63 @@ +# Copyright (c) 2021 Johannes Stoelp + + .macro push reg + addi sp, sp, -8 + sd \reg, 0(sp) + .endm + + .macro pop reg + ld \reg, 0(sp) + addi sp, sp, 8 + .endm + + .section .text, "ax", @progbits + + # extern "C" void yield(const void* new_stack, void** old_stack); + # ^^^^^^^^^ ^^^^^^^^^ + # a0 (x10) a1 (x11) + .global yield + .type yield, @function +yield: + .cfi_startproc + # save return address + push ra + + # push callee saved registers + push x8 + push x9 + push x18 + push x19 + push x20 + push x21 + push x22 + push x23 + push x24 + push x25 + push x26 + push x27 + + # arg0: a0 holds new stack + # arg1: a1 holds addr to location current stack must be saved + sd sp, 0(a1) # save current stack ptr + mv sp, a0 # switch to new stack ptr + + # pop callee saved registers + pop x27 + pop x26 + pop x25 + pop x24 + pop x23 + pop x22 + pop x21 + pop x20 + pop x19 + pop x18 + pop x9 + pop x8 + + # restore return address + pop ra + + ret + .cfi_endproc + .size yield, .-yield |