Skip to content

Commit c6c092e

Browse files
committed
arch: riscv: fatal: print csf in z_riscv_fault()
Refactor the csf saving code into a macro and calls it before jumping to `z_riscv_fault()`, so that callee-saved-registers are printed on generic CPU exception as well. Signed-off-by: Yong Cong Sin <yongcong.sin@gmail.com> Signed-off-by: Yong Cong Sin <ycsin@meta.com>
1 parent e3f610a commit c6c092e

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

arch/riscv/core/isr.S

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,20 @@
5656
RV_I( sr s9, ___callee_saved_t_s9_OFFSET(sp) );\
5757
RV_I( sr s10, ___callee_saved_t_s10_OFFSET(sp) );\
5858
RV_I( sr s11, ___callee_saved_t_s11_OFFSET(sp) )
59+
60+
/**
61+
* Convenience macro to store the caller-saved registers (CSF) on stack,
62+
* and link it to the `struct arch_esf`.
63+
* 1. Restore the `s0` we saved early in ISR entry so it shows up properly in the CSF.
64+
* 2. Allocate space for CSF on current thread stack
65+
* 3. Save CSF to the allocated stack
66+
* 4. Link the saved CSF to the `struct arch_esf`
67+
*/
68+
#define STORE_CSF_IN_ESF(_esf) \
69+
lr s0, __struct_arch_esf_s0_OFFSET(sp) ; \
70+
addi sp, sp, -__callee_saved_t_SIZEOF ; \
71+
STORE_CALLEE_SAVED() ; \
72+
sr sp __struct_arch_esf_csf_OFFSET(_esf)
5973
#endif /* CONFIG_EXCEPTION_DEBUG */
6074

6175
.macro get_current_cpu dst
@@ -383,8 +397,18 @@ no_fp: /* increment _current->arch.exception_depth */
383397
* no_reschedule to restore stack.
384398
*/
385399
mv a0, sp
400+
#ifdef CONFIG_EXCEPTION_DEBUG
401+
STORE_CSF_IN_ESF(a0) ;
402+
403+
call z_riscv_fault
404+
/* Restore SP if z_riscv_fault returns before jumping to no_reschedule */
405+
addi sp, sp, __callee_saved_t_SIZEOF
406+
407+
j no_reschedule
408+
#else
386409
la ra, no_reschedule
387410
tail z_riscv_fault
411+
#endif /* CONFIG_EXCEPTION_DEBUG */
388412

389413
is_kernel_syscall:
390414
/*
@@ -454,20 +478,7 @@ do_fault:
454478
1: mv a1, sp
455479

456480
#ifdef CONFIG_EXCEPTION_DEBUG
457-
/*
458-
* Restore the s0 we saved early in ISR entry
459-
* so it shows up properly in the CSF.
460-
*/
461-
lr s0, __struct_arch_esf_s0_OFFSET(sp)
462-
463-
/* Allocate space for caller-saved registers on current thread stack */
464-
addi sp, sp, -__callee_saved_t_SIZEOF
465-
466-
/* Save callee-saved registers */
467-
STORE_CALLEE_SAVED() ;
468-
469-
/* Store csf's addr into esf (a1 still holds the pointer to the esf at this point) */
470-
sr sp __struct_arch_esf_csf_OFFSET(a1)
481+
STORE_CSF_IN_ESF(a1) ;
471482
#endif /* CONFIG_EXCEPTION_DEBUG */
472483
tail z_riscv_fatal_error
473484

0 commit comments

Comments
 (0)