Skip to content

Commit c2f6dd3

Browse files
committed
arch/risc-v: Implement up_this_task using Thread Pointer (TP)
Summary: - Added up_this_task() and up_update_task() macros to use TP register for fast task pointer access - Modified CPU startup and initial state code to initialize TP with task pointer - Updated context save/restore macros to handle TP only when TLS is enabled - Added irq.h include to riscv_cpustart.c for new macros Impact: - Improves performance by using TP for fast task pointer access - Reduces overhead of getting current task pointer in scheduler - Maintains compatibility with TLS configuration - No impact when CONFIG_SCHED_THREAD_LOCAL is enabled - Changes context save/restore behavior for TP Signed-off-by: Huang Qi <[email protected]>
1 parent 007a3ff commit c2f6dd3

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

arch/risc-v/include/irq.h

+28
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,34 @@ int up_this_cpu(void);
749749
* Inline Functions
750750
****************************************************************************/
751751

752+
/****************************************************************************
753+
* Schedule acceleration macros
754+
****************************************************************************/
755+
756+
/* When thread local storage is disabled (the usual case), the TP
757+
* (Thread Pointer) register is available for use as a general purpose
758+
* register. We use it to store the current task's TCB pointer for quick
759+
* access.
760+
* Note: If you want to use TLS (CONFIG_SCHED_THREAD_LOCAL), your toolchain
761+
* must be compiled with --enable-tls option to properly support
762+
* thread-local storage.
763+
*/
764+
765+
#ifndef CONFIG_SCHED_THREAD_LOCAL
766+
#define up_this_task() \
767+
({ \
768+
struct tcb_s* t; \
769+
__asm__ __volatile__("mv %0, tp" : "=r"(t)); \
770+
t; \
771+
})
772+
773+
/* Update the current task pointer stored in TP register */
774+
#define up_update_task(t) \
775+
{ \
776+
__asm__ __volatile__("mv tp, %0" : : "r"(t)); \
777+
}
778+
#endif /* CONFIG_SCHED_THREAD_LOCAL */
779+
752780
/****************************************************************************
753781
* Name: up_irq_save
754782
*

arch/risc-v/src/common/riscv_cpustart.c

+7-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <nuttx/arch.h>
3737
#include <nuttx/spinlock.h>
3838
#include <nuttx/sched_note.h>
39+
#include <arch/irq.h>
3940

4041
#include "sched/sched.h"
4142
#include "init/init.h"
@@ -69,6 +70,8 @@
6970

7071
void riscv_cpu_boot(int cpu)
7172
{
73+
struct tcb_s *tcb;
74+
7275
/* Clear IPI for CPU(cpu) */
7376

7477
riscv_ipi_clear(cpu);
@@ -100,9 +103,9 @@ void riscv_cpu_boot(int cpu)
100103

101104
sinfo("CPU%d Started\n", this_cpu());
102105

103-
#ifdef CONFIG_STACK_COLORATION
104-
struct tcb_s *tcb = this_task();
106+
tcb = current_task(this_cpu());
105107

108+
#ifdef CONFIG_STACK_COLORATION
106109
/* If stack debug is enabled, then fill the stack with a
107110
* recognizable value that we can use later to test for high
108111
* water marks.
@@ -111,6 +114,8 @@ void riscv_cpu_boot(int cpu)
111114
riscv_stack_color(tcb->stack_alloc_ptr, 0);
112115
#endif
113116

117+
up_update_task(tcb);
118+
114119
/* TODO: Setup FPU */
115120

116121
/* Clear machine software interrupt for CPU(cpu) */

arch/risc-v/src/common/riscv_initialstate.c

+2
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ void up_initial_state(struct tcb_s *tcb)
153153
#ifdef CONFIG_SCHED_THREAD_LOCAL
154154
xcp->regs[REG_TP] = (uintptr_t)tcb->stack_alloc_ptr +
155155
sizeof(struct tls_info_s);
156+
#else
157+
xcp->regs[REG_TP] = (uintptr_t)tcb;
156158
#endif
157159

158160
/* Set the initial value of the interrupt context register.

arch/risc-v/src/common/riscv_macros.S

+4
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@
5858
#ifdef RISCV_SAVE_GP
5959
REGSTORE x3, REG_X3(\in) /* gp */
6060
#endif
61+
#ifdef CONFIG_SCHED_THREAD_LOCAL
6162
REGSTORE x4, REG_X4(\in) /* tp */
63+
#endif
6264
REGSTORE x5, REG_X5(\in) /* t0 */
6365
REGSTORE x6, REG_X6(\in) /* t1 */
6466
REGSTORE x7, REG_X7(\in) /* t2 */
@@ -201,7 +203,9 @@
201203
#ifdef RISCV_SAVE_GP
202204
REGLOAD x3, REG_X3(\out) /* gp */
203205
#endif
206+
#ifdef CONFIG_SCHED_THREAD_LOCAL
204207
REGLOAD x4, REG_X4(\out) /* tp */
208+
#endif
205209
REGLOAD x5, REG_X5(\out) /* t0 */
206210
REGLOAD x6, REG_X6(\out) /* t1 */
207211
REGLOAD x7, REG_X7(\out) /* t2 */

0 commit comments

Comments
 (0)