diff options
Diffstat (limited to 'lib/mlibc/options/internal/riscv64-include/mlibc/thread.hpp')
-rw-r--r-- | lib/mlibc/options/internal/riscv64-include/mlibc/thread.hpp | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/lib/mlibc/options/internal/riscv64-include/mlibc/thread.hpp b/lib/mlibc/options/internal/riscv64-include/mlibc/thread.hpp new file mode 100644 index 0000000..7428b75 --- /dev/null +++ b/lib/mlibc/options/internal/riscv64-include/mlibc/thread.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include <stdint.h> +#include <mlibc/tcb.hpp> +#include <bits/ensure.h> + +namespace mlibc { + +inline Tcb *get_current_tcb() { + // On RISC-V, the TCB is below the thread pointer. + uintptr_t tp = (uintptr_t)__builtin_thread_pointer(); + auto tcb = reinterpret_cast<Tcb *>(tp - sizeof(Tcb)); + __ensure(tcb == tcb->selfPointer); + return tcb; +} + +inline uintptr_t get_sp() { + uintptr_t sp; + asm ("mv %0, sp" : "=r"(sp)); + return sp; +} + +} // namespace mlibc |