Skip to content

Commit 26d75ab

Browse files
ycsincarlescufi
authored andcommitted
arch: riscv: optionally stores a pointer to csf in struct arch_esf
The callee-saved-registers can be helpful to debug the state of a core upon an exception, however, currently there's no way to access that information in user-implemented `k_sys_fatal_error_handler()`, even though the csf is already stored in the stack. This patch conditionally add a `csf` member in the `arch_esf` when `CONFIG_EXTRA_EXCEPTION_INFO=y`*, which the `_isr_wrapper` would update when a fatal error occurs before invoking `z_riscv_fatal_error_csf()`. Functions such as `k_sys_fatal_error_handler()` would then be able to access the callee-saved-registers at the time of exception via `esf->csf`. * For SoCs that select `RISCV_SOC_HAS_ISR_STACKING`, the `SOC_ISR_STACKING_ESF_DECLARE` has to include the `csf` member, otherwise the build would fail. Signed-off-by: Yong Cong Sin <ycsin@meta.com> Signed-off-by: Yong Cong Sin <yongcong.sin@gmail.com>
1 parent 1968220 commit 26d75ab

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed

arch/riscv/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,14 @@ config RISCV_HART_MASK
238238
i.e. 128, 129, ..(0x80, 8x81, ..), this can be configured to 63 (0x7f)
239239
such that we can extract the bits that start from 0.
240240

241+
config EXTRA_EXCEPTION_INFO
242+
bool "Collect extra exception info"
243+
depends on EXCEPTION_DEBUG
244+
help
245+
This option enables the collection of extra information, such as
246+
register state, when a fault occurs. This information can be useful
247+
to collect for post-mortem analysis and debug of issues.
248+
241249
config RISCV_PMP
242250
bool "RISC-V PMP Support"
243251
select THREAD_STACK_INFO

arch/riscv/core/isr.S

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,10 +450,15 @@ do_fault:
450450
STORE_CALLEE_SAVED() ;
451451
mv a2, sp
452452

453+
#ifdef CONFIG_EXTRA_EXCEPTION_INFO
454+
/* Store csf's addr into esf (a1 still holds the pointer to the esf at this point) */
455+
sr a2 __struct_arch_esf_csf_OFFSET(a1)
456+
#endif /* CONFIG_EXTRA_EXCEPTION_INFO */
457+
453458
tail z_riscv_fatal_error_csf
454459
#else
455460
tail z_riscv_fatal_error
456-
#endif
461+
#endif /* CONFIG_EXCEPTION_DEBUG */
457462

458463
#if defined(CONFIG_IRQ_OFFLOAD)
459464
do_irq_offload:

arch/riscv/core/offsets/offsets.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ GEN_OFFSET_STRUCT(arch_esf, s0);
118118
GEN_OFFSET_STRUCT(arch_esf, sp);
119119
#endif
120120

121+
#ifdef CONFIG_EXTRA_EXCEPTION_INFO
122+
GEN_OFFSET_STRUCT(arch_esf, csf);
123+
#endif /* CONFIG_EXTRA_EXCEPTION_INFO */
124+
121125
#if defined(CONFIG_RISCV_SOC_CONTEXT_SAVE)
122126
GEN_OFFSET_STRUCT(arch_esf, soc_context);
123127
#endif

doc/releases/release-notes-4.0.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ Architectures
6464
* The stack traces upon fatal exception now prints the address of stack pointer (sp) or frame
6565
pointer (fp) depending on the build configuration.
6666

67+
* When :kconfig:option:`CONFIG_EXTRA_EXCEPTION_INFO` is enabled, the exception stack frame (arch_esf)
68+
has an additional field ``csf`` that points to the callee-saved-registers upon an fatal error,
69+
which can be accessed in :c:func:`k_sys_fatal_error_handler` by ``esf->csf``.
70+
71+
* For SoCs that select `RISCV_SOC_HAS_ISR_STACKING`, the `SOC_ISR_STACKING_ESF_DECLARE` has to
72+
include the `csf` member, otherwise the build would fail.
73+
6774
* Xtensa
6875

6976
* x86

include/zephyr/arch/riscv/exception.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ struct soc_esf {
4545
};
4646
#endif
4747

48+
#ifdef CONFIG_EXTRA_EXCEPTION_INFO
49+
/* Forward declaration */
50+
struct _callee_saved;
51+
typedef struct _callee_saved _callee_saved_t;
52+
#endif /* CONFIG_EXTRA_EXCEPTION_INFO */
53+
4854
#if defined(CONFIG_RISCV_SOC_HAS_ISR_STACKING)
4955
SOC_ISR_STACKING_ESF_DECLARE;
5056
#else
@@ -81,6 +87,10 @@ struct arch_esf {
8187
unsigned long sp; /* preserved (user or kernel) stack pointer */
8288
#endif
8389

90+
#ifdef CONFIG_EXTRA_EXCEPTION_INFO
91+
_callee_saved_t *csf; /* pointer to callee-saved-registers */
92+
#endif /* CONFIG_EXTRA_EXCEPTION_INFO */
93+
8494
#ifdef CONFIG_RISCV_SOC_CONTEXT_SAVE
8595
struct soc_esf soc_context;
8696
#endif

0 commit comments

Comments
 (0)