Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions arch/riscv/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ config RISCV_GP
global pointer at program start or earlier than any instruction
using GP relative addressing.

config RISCV_CURRENT_VIA_GP
bool "Store current thread into the global pointer (GP) register"
depends on !RISCV_GP && !USERSPACE
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should document somewhere (maybe just right here in the help string) why USERSPACE is disallowed, and maybe even open an issue demanding that proper entry/exit protection for GP be implemented.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created #81843

depends on MP_MAX_NUM_CPUS > 1
select ARCH_HAS_CUSTOM_CURRENT_IMPL
help
Store the current thread's pointer into the global pointer (GP) register.
When is enabled, calls to `_current` & `k_sched_current_thread_query()` will
be reduced to a single register read.

config RISCV_ALWAYS_SWITCH_THROUGH_ECALL
bool "Do not use mret outside a trap handler context"
depends on MULTITHREADING
Expand Down
3 changes: 3 additions & 0 deletions doc/releases/release-notes-4.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ Architectures

* RISC-V

* Implements :c:func:`arch_current_thread_set` & :c:func:`arch_current_thread`, which can be enabled
by :kconfig:option:`CONFIG_RISCV_CURRENT_VIA_GP` (:github:`80716`).

* Xtensa

* native/POSIX
Expand Down
12 changes: 12 additions & 0 deletions include/zephyr/arch/riscv/arch_inlines.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <zephyr/kernel_structs.h>
#include "csr.h"
#include "reg.h"

static ALWAYS_INLINE uint32_t arch_proc_id(void)
{
Expand All @@ -26,6 +27,17 @@ static ALWAYS_INLINE _cpu_t *arch_curr_cpu(void)
#endif
}

#ifdef CONFIG_RISCV_CURRENT_VIA_GP
register struct k_thread *__arch_current_thread __asm__("gp");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: while asm+register variables are a really useful trick for controlling marshalling behavior around inline assembly, I think here it might be more confusing than valuable and a more traditional asm block which reads the value directly might be clearer.


#define arch_current_thread() __arch_current_thread
#define arch_current_thread_set(thread) \
{ \
_current_cpu->current = thread; \
__arch_current_thread = (thread); \
}
#endif /* CONFIG_RISCV_CURRENT_VIA_GP */

static ALWAYS_INLINE unsigned int arch_num_cpus(void)
{
return CONFIG_MP_MAX_NUM_CPUS;
Expand Down