Skip to content

Commit 1a752e8

Browse files
ycsinnashif
authored andcommitted
arch: riscv: implement ARCH_HAS_CUSTOM_CURRENT_IMPL with GP
Implement `arch_curr_thread()` & `arch_set_curr_thread()` with the global pointer (GP) register. Signed-off-by: Yong Cong Sin <[email protected]> Signed-off-by: Yong Cong Sin <[email protected]>
1 parent d26c712 commit 1a752e8

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

arch/riscv/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ config RISCV_GP
2828
global pointer at program start or earlier than any instruction
2929
using GP relative addressing.
3030

31+
config RISCV_CURRENT_VIA_GP
32+
bool "Store current thread into the global pointer (GP) register"
33+
depends on !RISCV_GP && !USERSPACE
34+
depends on MP_MAX_NUM_CPUS > 1
35+
select ARCH_HAS_CUSTOM_CURRENT_IMPL
36+
help
37+
Store the current thread's pointer into the global pointer (GP) register.
38+
When is enabled, calls to `_current` & `k_sched_current_thread_query()` will
39+
be reduced to a single register read.
40+
3141
config RISCV_ALWAYS_SWITCH_THROUGH_ECALL
3242
bool "Do not use mret outside a trap handler context"
3343
depends on MULTITHREADING

doc/releases/release-notes-4.1.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ Architectures
5252

5353
* RISC-V
5454

55+
* Implements :c:func:`arch_current_thread_set` & :c:func:`arch_current_thread`, which can be enabled
56+
by :kconfig:option:`CONFIG_RISCV_CURRENT_VIA_GP` (:github:`80716`).
57+
5558
* Xtensa
5659

5760
* native/POSIX

include/zephyr/arch/riscv/arch_inlines.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <zephyr/kernel_structs.h>
1313
#include "csr.h"
14+
#include "reg.h"
1415

1516
static ALWAYS_INLINE uint32_t arch_proc_id(void)
1617
{
@@ -26,6 +27,17 @@ static ALWAYS_INLINE _cpu_t *arch_curr_cpu(void)
2627
#endif
2728
}
2829

30+
#ifdef CONFIG_RISCV_CURRENT_VIA_GP
31+
register struct k_thread *__arch_current_thread __asm__("gp");
32+
33+
#define arch_current_thread() __arch_current_thread
34+
#define arch_current_thread_set(thread) \
35+
{ \
36+
_current_cpu->current = thread; \
37+
__arch_current_thread = (thread); \
38+
}
39+
#endif /* CONFIG_RISCV_CURRENT_VIA_GP */
40+
2941
static ALWAYS_INLINE unsigned int arch_num_cpus(void)
3042
{
3143
return CONFIG_MP_MAX_NUM_CPUS;

0 commit comments

Comments
 (0)