Skip to content

Commit 1c24496

Browse files
committed
OS: bugfix for RT-Thread task switch stack frame not 16 bytes aligned
Make task frame 16 bytes aligned by changing saved regs from 30 to 32 https://github.com/riscv-mcu/riscv-gcc/blob/553a166de2dd7451652a0973908e1f2459118506/gcc/config/riscv/riscv.h#L201-L211 Signed-off-by: Huaqi Fang <578567190@qq.com>
1 parent 573c0cb commit 1c24496

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ volatile rt_ubase_t rt_interrupt_to_thread = 0;
6464
volatile rt_ubase_t rt_thread_switch_interrupt_flag = 0;
6565
void SysTick_Handler(void);
6666

67+
/* Stack frame size 32 REGBYTES(4/8) for most cases, but for ilp32e mode, it's 14 REGBYTES(4) */
6768
struct rt_hw_stack_frame {
6869
rt_ubase_t epc; /* epc - epc - program counter */
6970
rt_ubase_t ra; /* x1 - ra - return address for jumps */
@@ -95,6 +96,8 @@ struct rt_hw_stack_frame {
9596
rt_ubase_t t4; /* x29 - t4 - temporary register 4 */
9697
rt_ubase_t t5; /* x30 - t5 - temporary register 5 */
9798
rt_ubase_t t6; /* x31 - t6 - temporary register 6 */
99+
rt_ubase_t rsv0; /* reserved 0 - reserved to make space */
100+
rt_ubase_t rsv1; /* reserved 1 - reserved to make space */
98101
#endif
99102
rt_ubase_t xstatus; /* - m/s status register */
100103
};

OS/RTThread/libcpu/risc-v/nuclei/gcc/context_gcc.S

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010

1111
#include "riscv_encoding.h"
1212

13+
/* see https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-cc.adoc */
1314
#ifndef __riscv_32e
14-
#define portRegNum 30
15+
/* Make sure the stack pointer is 16-byte aligned when calling c function */
16+
#define portRegNum 32
1517
#else
18+
/* Make sure the stack pointer is 4-byte aligned when calling c function for ilp32e */
1619
#define portRegNum 14
1720
#endif
1821

@@ -21,11 +24,8 @@
2124
.extern rt_interrupt_from_thread
2225
.extern rt_interrupt_to_thread
2326

24-
2527
.section .text
2628

27-
28-
2929
/*
3030
* void rt_hw_context_switch_to(rt_ubase_t to);
3131
* a0 --> to_thread
@@ -48,14 +48,14 @@ rt_hw_context_switch_to:
4848
csrw CSR_XSCRATCH, t0
4949
LOAD sp, 0x0(a0) /* Read sp from first TCB member(a0) */
5050

51-
/* Pop PC from stack and set MEPC */
51+
/* Pop PC from stack and set XEPC */
5252
LOAD t0, 0 * REGBYTES(sp)
5353
csrw CSR_XEPC, t0
54-
/* Pop mstatus from stack and set it */
54+
/* Pop xstatus from stack and set it */
5555
LOAD t0, (portRegNum - 1) * REGBYTES(sp)
5656
csrw CSR_XSTATUS, t0
5757
/* Interrupt still disable here */
58-
/* Restore Registers from Stack */
58+
/* Restore registers from stack */
5959
LOAD x1, 1 * REGBYTES(sp) /* RA */
6060
LOAD x5, 2 * REGBYTES(sp)
6161
LOAD x6, 3 * REGBYTES(sp)
@@ -128,7 +128,7 @@ eclic_xsip_handler:
128128
STORE x30, 27 * REGBYTES(sp)
129129
STORE x31, 28 * REGBYTES(sp)
130130
#endif
131-
/* Push mstatus to stack */
131+
/* Push xstatus to stack */
132132
csrr t0, CSR_XSTATUS
133133
STORE t0, (portRegNum - 1) * REGBYTES(sp)
134134

@@ -147,16 +147,16 @@ eclic_xsip_handler:
147147
LOAD t0, rt_interrupt_to_thread
148148
LOAD sp, 0x0(t0)
149149

150-
/* Pop PC from stack and set MEPC */
150+
/* Pop PC from stack and set XEPC */
151151
LOAD t0, 0 * REGBYTES(sp)
152152
csrw CSR_XEPC, t0
153153
/* Pop additional registers */
154154

155-
/* Pop mstatus from stack and set it */
155+
/* Pop xstatus from stack and set it */
156156
LOAD t0, (portRegNum - 1) * REGBYTES(sp)
157157
csrw CSR_XSTATUS, t0
158158
/* Interrupt still disable here */
159-
/* Restore Registers from Stack */
159+
/* Restore registers from stack */
160160
LOAD x1, 1 * REGBYTES(sp) /* RA */
161161
LOAD x5, 2 * REGBYTES(sp)
162162
LOAD x6, 3 * REGBYTES(sp)

OS/RTThread/libcpu/risc-v/nuclei/iar/context_iar.S

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "riscv_encoding.h"
1212

1313
#ifndef __riscv_32e
14-
#define portRegNum 30
14+
#define portRegNum 32
1515
#else
1616
#define portRegNum 14
1717
#endif

0 commit comments

Comments
 (0)