Skip to content

Commit

Permalink
feat: arm64: update thread self on sp-switch
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
polarvid committed Sep 25, 2024
1 parent b4d3006 commit 495622f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
6 changes: 6 additions & 0 deletions libcpu/aarch64/common/include/armv8.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
#ifndef __ARMV8_H__
#define __ARMV8_H__

#include <rtconfig.h>

#ifdef ARCH_USING_HW_THREAD_SELF
#define ARM64_THREAD_REG tpidr_el1
#endif /* ARCH_USING_HW_THREAD_SELF */

#ifdef __ASSEMBLY__

/*********************
Expand Down
4 changes: 2 additions & 2 deletions libcpu/aarch64/common/include/cpuport.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ void _thread_start(void);
rt_inline struct rt_thread *rt_hw_thread_self(void)
{
struct rt_thread *thread;
__asm__ volatile ("mrs %0, tpidr_el1":"=r"(thread));
__asm__ volatile ("mrs %0, " RT_STRINGIFY(ARM64_THREAD_REG) :"=r"(thread));

return thread;
}

rt_inline void rt_hw_thread_set_self(struct rt_thread *thread)
{
__asm__ volatile ("msr tpidr_el1, %0"::"r"(thread));
__asm__ volatile ("msr " RT_STRINGIFY(ARM64_THREAD_REG) ", %0"::"r"(thread));
}

#endif /* ARCH_USING_HW_THREAD_SELF */
Expand Down
7 changes: 7 additions & 0 deletions libcpu/aarch64/common/mp/context_gcc.S
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@

.globl rt_hw_context_switch_to

.macro update_tidr, srcx
msr ARM64_THREAD_REG, \srcx
.endm

/*
* void rt_hw_context_switch_to(rt_uint3 to, struct rt_thread *to_thread);
* X0 --> to (thread stack)
Expand All @@ -35,6 +39,7 @@
rt_hw_context_switch_to:
ldr x0, [x0]
mov sp, x0
update_tidr x1

/* reserved to_thread */
mov x19, x1
Expand Down Expand Up @@ -62,6 +67,7 @@ rt_hw_context_switch:
str x3, [x0] // store sp in preempted tasks TCB
ldr x0, [x1] // get new task stack pointer
mov sp, x0
update_tidr x2

/* backup thread self */
mov x19, x2
Expand Down Expand Up @@ -119,6 +125,7 @@ rt_hw_context_switch_interrupt:
/* setup SP to to-thread's */
ldr x0, [TO_SPP]
mov sp, x0
update_tidr TO_TCB

mov x0, TO_TCB
bl rt_cpus_lock_status_restore
Expand Down
3 changes: 0 additions & 3 deletions src/scheduler_mp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1069,9 +1069,6 @@ void rt_sched_post_ctx_switch(struct rt_thread *thread)
}
/* safe to access since irq is masked out */
pcpu->current_thread = thread;
#ifdef ARCH_USING_HW_THREAD_SELF
rt_hw_thread_set_self(thread);
#endif /* ARCH_USING_HW_THREAD_SELF */
}

#ifdef RT_DEBUGING_CRITICAL
Expand Down

0 comments on commit 495622f

Please sign in to comment.