Skip to content

Commit 5796e0d

Browse files
polarvidmysterywolf
authored andcommitted
feat: arm64: update thread self on sp-switch
This patch improves the atomicity of context switching by ensuring that the stack pointer (sp) and thread self updates occur simultaneously. This enhancement is crucial for maintaining thread safety and preventing potential inconsistencies during context switches. Changes: - Modified `cpuport.h` to use `ARM64_THREAD_REG` for thread self access. - Added an `update_tidr` macro in `context_gcc.S` to streamline thread ID updates. - Adjusted `rt_hw_context_switch_to` and `rt_hw_context_switch` to call `update_tidr`, ensuring atomic updates during context switches. - Cleaned up `scheduler_mp.c` by removing redundant thread self assignments. Signed-off-by: Shell <[email protected]>
1 parent 906d1ca commit 5796e0d

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

libcpu/aarch64/common/include/armv8.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
#ifndef __ARMV8_H__
1212
#define __ARMV8_H__
1313

14+
#include <rtconfig.h>
15+
16+
#ifdef ARCH_USING_HW_THREAD_SELF
17+
#define ARM64_THREAD_REG tpidr_el1
18+
#endif /* ARCH_USING_HW_THREAD_SELF */
19+
1420
#ifdef __ASSEMBLY__
1521

1622
/*********************

libcpu/aarch64/common/include/cpuport.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ void _thread_start(void);
5757
rt_inline struct rt_thread *rt_hw_thread_self(void)
5858
{
5959
struct rt_thread *thread;
60-
__asm__ volatile ("mrs %0, tpidr_el1":"=r"(thread));
60+
__asm__ volatile ("mrs %0, " RT_STRINGIFY(ARM64_THREAD_REG) :"=r"(thread));
6161

6262
return thread;
6363
}
6464

6565
rt_inline void rt_hw_thread_set_self(struct rt_thread *thread)
6666
{
67-
__asm__ volatile ("msr tpidr_el1, %0"::"r"(thread));
67+
__asm__ volatile ("msr " RT_STRINGIFY(ARM64_THREAD_REG) ", %0"::"r"(thread));
6868
}
6969

7070
#endif /* ARCH_USING_HW_THREAD_SELF */

libcpu/aarch64/common/mp/context_gcc.S

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727

2828
.globl rt_hw_context_switch_to
2929

30+
.macro update_tidr, srcx
31+
#ifdef ARCH_USING_HW_THREAD_SELF
32+
msr ARM64_THREAD_REG, \srcx
33+
#endif /* ARCH_USING_HW_THREAD_SELF */
34+
.endm
35+
3036
/*
3137
* void rt_hw_context_switch_to(rt_uint3 to, struct rt_thread *to_thread);
3238
* X0 --> to (thread stack)
@@ -35,6 +41,7 @@
3541
rt_hw_context_switch_to:
3642
ldr x0, [x0]
3743
mov sp, x0
44+
update_tidr x1
3845

3946
/* reserved to_thread */
4047
mov x19, x1
@@ -62,6 +69,7 @@ rt_hw_context_switch:
6269
str x3, [x0] // store sp in preempted tasks TCB
6370
ldr x0, [x1] // get new task stack pointer
6471
mov sp, x0
72+
update_tidr x2
6573

6674
/* backup thread self */
6775
mov x19, x2
@@ -119,6 +127,7 @@ rt_hw_context_switch_interrupt:
119127
/* setup SP to to-thread's */
120128
ldr x0, [TO_SPP]
121129
mov sp, x0
130+
update_tidr TO_TCB
122131

123132
mov x0, TO_TCB
124133
bl rt_cpus_lock_status_restore

src/scheduler_mp.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,9 +1069,6 @@ void rt_sched_post_ctx_switch(struct rt_thread *thread)
10691069
}
10701070
/* safe to access since irq is masked out */
10711071
pcpu->current_thread = thread;
1072-
#ifdef ARCH_USING_HW_THREAD_SELF
1073-
rt_hw_thread_set_self(thread);
1074-
#endif /* ARCH_USING_HW_THREAD_SELF */
10751072
}
10761073

10771074
#ifdef RT_DEBUGING_CRITICAL

0 commit comments

Comments
 (0)