aboutsummaryrefslogtreecommitdiff
path: root/lib/mlibc/options/internal/riscv64-include/mlibc/thread.hpp
blob: 7428b759b715e3f52cb60d2472547c6f8364f55c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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