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/init_stack.cc | |
parent | 0db708a285c74526ebcce31a10f5730f3f715d61 (diff) | |
download | matcha-threads-2e4a31287705d8437cddd109c12b7e11825192bc.tar.gz matcha-threads-2e4a31287705d8437cddd109c12b7e11825192bc.zip |
added riscv64
Diffstat (limited to 'lib/arch/riscv64/init_stack.cc')
-rw-r--r-- | lib/arch/riscv64/init_stack.cc | 39 |
1 files changed, 39 insertions, 0 deletions
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); +} |