Skip to content

Commit 573c0cb

Browse files
committed
OS: Adapt task sp alignment to match riscv abi requirement
RT-Thread/ThreadX/FreeRTOS/UCOSII support are updated see https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-cc.adoc The stack grows downwards (towards lower addresses) and the stack pointer shall be aligned to a 128-bit boundary upon procedure entry. The ILP32E calling convention is designed to be usable with the RV32E ISA. This calling convention is the same as the integer calling convention, except for the following differences. The stack pointer need only be aligned to a 32-bit boundary.
1 parent 84e4262 commit 573c0cb

File tree

5 files changed

+36
-5
lines changed

5 files changed

+36
-5
lines changed

OS/FreeRTOS/Source/portable/portmacro.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,14 @@ typedef uint64_t TickType_t;
8484
/* Architecture specifics. */
8585
#define portSTACK_GROWTH ( -1 )
8686
#define portTICK_PERIOD_MS ( ( TickType_t ) 1000 / configTICK_RATE_HZ )
87-
#define portBYTE_ALIGNMENT 8
87+
88+
#ifndef __riscv_32e
89+
/* The stack grows downwards (towards lower addresses) and the stack pointer shall be aligned to a 128-bit boundary upon procedure entry. */
90+
#define portBYTE_ALIGNMENT 16
91+
#else
92+
/* ILP32E calling convention The stack pointer need only be aligned to a 32-bit boundary */
93+
#define portBYTE_ALIGNMENT 4
94+
#endif
8895
/*-----------------------------------------------------------*/
8996

9097
/* Scheduler utilities. */

OS/RTThread/libcpu/risc-v/nuclei/cpuport.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,13 @@ rt_uint8_t* rt_hw_stack_init(void* tentry,
119119
int i;
120120

121121
stk = stack_addr + sizeof(rt_ubase_t);
122-
stk = (rt_uint8_t*)RT_ALIGN_DOWN((rt_ubase_t)stk, REGBYTES);
122+
/* https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-cc.adoc */
123+
/* 32-bit boundary for ilp32e, and 128-bit boundary for others */
124+
#ifndef __riscv_32e
125+
stk = (rt_uint8_t*)RT_ALIGN_DOWN((rt_ubase_t)stk, 16);
126+
#else
127+
stk = (rt_uint8_t*)RT_ALIGN_DOWN((rt_ubase_t)stk, 4);
128+
#endif
123129
stk -= sizeof(struct rt_hw_stack_frame);
124130

125131
frame = (struct rt_hw_stack_frame*)stk;

OS/ThreadX/ports/nuclei/module_manager/src/txm_module_manager_thread_stack_build.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ VOID _txm_module_manager_thread_stack_build(TX_THREAD *thread_ptr, VOID (*functi
3434
stk = thread_ptr -> tx_thread_stack_end;
3535
// thread_ptr -> tx_thread_stack_ptr stored thread_entry_info
3636
thread_entry_info = (TXM_MODULE_THREAD_ENTRY_INFO *)(thread_ptr -> tx_thread_stack_ptr);
37-
stk = (uint8_t *)(((unsigned long)stk) & (~(unsigned long)(sizeof(ALIGN_TYPE) - 1)));
37+
/* https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-cc.adoc */
38+
/* 32-bit boundary for ilp32e, and 128-bit boundary for others */
39+
#ifndef __riscv_32e
40+
stk = (uint8_t *)(((unsigned long)stk) & (~(unsigned long)(16 - 1)));
41+
#else
42+
stk = (uint8_t *)(((unsigned long)stk) & (~(unsigned long)(4 - 1)));
43+
#endif
3844
stk -= sizeof(struct thread_stack_frame);
3945

4046
frame = (struct thread_stack_frame*)stk;

OS/ThreadX/ports/nuclei/port.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,13 @@ VOID _tx_thread_stack_build(TX_THREAD *thread_ptr, VOID (*function_ptr)(VOID))
169169
int i;
170170

171171
stk = thread_ptr -> tx_thread_stack_end;
172-
stk = (uint8_t *)(((unsigned long)stk) & (~(unsigned long)(sizeof(ALIGN_TYPE) - 1)));
172+
/* https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-cc.adoc */
173+
/* 32-bit boundary for ilp32e, and 128-bit boundary for others */
174+
#ifndef __riscv_32e
175+
stk = (uint8_t *)(((unsigned long)stk) & (~(unsigned long)(16 - 1)));
176+
#else
177+
stk = (uint8_t *)(((unsigned long)stk) & (~(unsigned long)(4 - 1)));
178+
#endif
173179
stk -= sizeof(struct thread_stack_frame);
174180

175181
frame = (struct thread_stack_frame*)stk;

OS/UCOSII/arch/os_cpu_port.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ typedef uint64_t TickType_t;
3838

3939
/* Architecture specifics. */
4040
#define portTICK_PERIOD_MS ( ( TickType_t ) 1000 / configTICK_RATE_HZ )
41-
#define portBYTE_ALIGNMENT 8
41+
#ifndef __riscv_32e
42+
/* The stack grows downwards (towards lower addresses) and the stack pointer shall be aligned to a 128-bit boundary upon procedure entry. */
43+
#define portBYTE_ALIGNMENT 16
44+
#else
45+
/* ILP32E calling convention The stack pointer need only be aligned to a 32-bit boundary */
46+
#define portBYTE_ALIGNMENT 4
47+
#endif
4248
/*-----------------------------------------------------------*/
4349

4450
/* Scheduler utilities. */

0 commit comments

Comments
 (0)