I think I found the reason of my problem:
In ulp_riscv_vectors.S, saving of registers are implemented as:
Code: Select all
.equ SAVE_REGS, 17
.equ CONTEXT_SIZE, (SAVE_REGS * 4)
/* Macro which first allocates space on the stack to save general
* purpose registers, and then save them. GP register is excluded.
* The default size allocated on the stack is CONTEXT_SIZE, but it
* can be overridden.
*
* Note: We don't save the callee-saved s0-s11 registers to save space
*/
.macro save_general_regs cxt_size=CONTEXT_SIZE
addi sp, sp, -\cxt_size
sw ra, RV_STK_RA(sp)
sw tp, RV_STK_TP(sp)
sw t0, RV_STK_T0(sp)
sw t1, RV_STK_T1(sp)
sw t2, RV_STK_T2(sp)
sw a0, RV_STK_A0(sp)
sw a1, RV_STK_A1(sp)
sw a2, RV_STK_A2(sp)
sw a3, RV_STK_A3(sp)
sw a4, RV_STK_A4(sp)
sw a5, RV_STK_A5(sp)
sw a6, RV_STK_A6(sp)
sw a7, RV_STK_A7(sp)
sw t3, RV_STK_T3(sp)
sw t4, RV_STK_T4(sp)
sw t5, RV_STK_T5(sp)
sw t6, RV_STK_T6(sp)
.endm
When this code is compiled, it generates the instructions below:
Code: Select all
00000010 <irq_vector>:
10: fbc10113 add sp,sp,-68
14: c206 sw ra,4(sp)
16: c812 sw tp,16(sp)
18: ca16 sw t0,20(sp)
1a: cc1a sw t1,24(sp)
1c: ce1e sw t2,28(sp)
1e: d42a sw a0,40(sp)
20: d62e sw a1,44(sp)
22: d832 sw a2,48(sp)
24: da36 sw a3,52(sp)
26: dc3a sw a4,56(sp)
28: de3e sw a5,60(sp)
2a: c0c2 sw a6,64(sp)
2c: c2c6 sw a7,68(sp)
2e: d8f2 sw t3,112(sp)
30: daf6 sw t4,116(sp)
32: dcfa sw t5,120(sp)
34: defe sw t6,124(sp)
It is clear they allocated 68 bytes in the stack to save 17 registers, but save actual registers to sp-124, that causes the problem.