Skip to content

Commit aad13e8

Browse files
committed
Implement WFI for Idle Loops and Enforce Shadow Stack Alignment
Optimized system reliability and interrupt latency by implementing wfi in the idle loop and enforcing 16-byte alignment for the system stack. Details: 1. Reliability (WFI) Implementation: Modified tx_thread_schedule.S to include the wfi (Wait For Interrupt) instruction in the idle loop. The scheduler now enters low-power sleep mode when no threads are ready, instead of busy-waiting, reducing significant power consumption. 2. Interrupt Latency (Shadow Stack Alignment): Updated tx_initialize_low_level.S to enforce 16-byte alignment on the system stack pointer (sp) before saving it to _tx_thread_system_stack_ptr. Ensures strict adherence to the RISC-V ABI and prevents misaligned access faults or performance penalties during context switches to the interrupt stack.
1 parent 85d0626 commit aad13e8

2 files changed

Lines changed: 6 additions & 1 deletion

File tree

ports/risc-v32/gnu/src/tx_initialize_low_level.S

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ __tx_free_memory_start:
7171
.global _tx_initialize_low_level
7272
.weak _tx_initialize_low_level
7373
_tx_initialize_low_level:
74+
andi sp, sp, -16 // Align stack pointer to 16 bytes
7475
la t0, _tx_thread_system_stack_ptr // Pickup address of system stack ptr
7576
sw sp, 0(t0) // Save system stack pointer
7677

ports/risc-v32/gnu/src/tx_thread_schedule.S

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ _tx_thread_schedule:
7878
la t0, _tx_thread_execute_ptr // Pickup address of execute ptr
7979
_tx_thread_schedule_loop:
8080
LOAD t1, 0(t0) // Pickup next thread to execute
81-
beqz t1, _tx_thread_schedule_loop // If NULL, wait for thread to execute
81+
bnez t1, _tx_thread_ready // If not NULL, break loop
82+
wfi // Wait for interrupt
83+
j _tx_thread_schedule_loop // Wait for interrupt
84+
85+
_tx_thread_ready:
8286

8387
/* }
8488
while(_tx_thread_execute_ptr == TX_NULL); */

0 commit comments

Comments
 (0)