Skip to content

Commit 7fbc428

Browse files
committed
riscv/crt0: Avoid clobbering sp and gp in _trap
Previously we were overriding the previous frame's values before saving them to the trapframe. This ensures that the trap handler prints the trapping frame's stack pointer/gp rather than the ones installed in _trap.
1 parent 4d3e411 commit 7fbc428

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

picocrt/machine/riscv/crt0.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ _trap(void)
103103
/* Build a known-working C environment */
104104
__asm__(".option push\n"
105105
".option norelax\n"
106+
"csrrw sp, mscratch, sp\n"
106107
"la sp, __stack\n"
107-
"la gp, __global_pointer$\n"
108108
".option pop");
109109

110110
/* Make space for saved registers */
@@ -130,13 +130,18 @@ _trap(void)
130130
__asm__("csrr t0, "PASTE(name));\
131131
__asm__(SD" t0, %0(sp)" :: "i" (offsetof(struct fault, name)))
132132

133+
/* Save the trapping frame's stack pointer stashed in mscratch. */
134+
__asm__("csrrw t0, mscratch, zero\n"
135+
SD " t0, %1(sp)\n"
136+
:: "i"(offsetof(struct fault, r[2])));
133137
SAVE_CSR(mepc);
134138
SAVE_CSR(mcause);
135139
SAVE_CSR(mtval);
136140

137141
/*
138142
* Pass pointer to saved registers in first parameter register
139143
*/
144+
__asm__("la gp, __global_pointer$");
140145
__asm__("mv a0, sp");
141146

142147
/* Enable FPU (just in case) */

0 commit comments

Comments
 (0)