Skip to content

Commit e3f610a

Browse files
committed
arch: riscv: streamline fatal handling code
`CONFIG_EXTRA_EXCEPTION_INFO` that was added in #78065 doesn't seem necessary, as we were already storing and printing the callee-saved-registers before that. All `CONFIG_EXTRA_EXCEPTION_INFO` does in RISCV is to add an additional `_callee_saved_t *csf` in the `struct arch_esf`, which overhead is negligible to what's being enabled by `CONFIG_EXCEPTION_DEBUG`. Let's remove `CONFIG_EXTRA_EXCEPTION_INFO`, and have that extra `_callee_saved_t *csf` in the `struct arch_esf` as long as `CONFIG_EXCEPTION_DEBUG` is enabled. Then, since `*csf` is always available in the `struct arch_esf` when `CONFIG_EXCEPTION_DEBUG=y`, we can simply rely on that pointer in `z_riscv_fatal_error()` instead of an additional argument in `z_riscv_fatal_error_csf()`, rendering it redundant and thus can be removed. Signed-off-by: Yong Cong Sin <yongcong.sin@gmail.com> Signed-off-by: Yong Cong Sin <ycsin@meta.com>
1 parent 350a5ef commit e3f610a

File tree

8 files changed

+31
-40
lines changed

8 files changed

+31
-40
lines changed

arch/riscv/Kconfig

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

304-
config EXTRA_EXCEPTION_INFO
305-
bool "Collect extra exception info"
306-
depends on EXCEPTION_DEBUG
307-
help
308-
This option enables the collection of extra information, such as
309-
register state, when a fault occurs. This information can be useful
310-
to collect for post-mortem analysis and debug of issues.
311-
312304
config RISCV_PMP
313305
bool "RISC-V PMP Support"
314306
select THREAD_STACK_INFO

arch/riscv/core/fatal.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,7 @@ const char *z_riscv_mcause_str(unsigned long cause)
8080
FUNC_NORETURN void z_riscv_fatal_error(unsigned int reason,
8181
const struct arch_esf *esf)
8282
{
83-
z_riscv_fatal_error_csf(reason, esf, NULL);
84-
}
85-
86-
FUNC_NORETURN void z_riscv_fatal_error_csf(unsigned int reason, const struct arch_esf *esf,
87-
const _callee_saved_t *csf)
88-
{
83+
__maybe_unused _callee_saved_t *csf = NULL;
8984
unsigned long mcause;
9085

9186
__asm__ volatile("csrr %0, mcause" : "=r" (mcause));
@@ -122,6 +117,8 @@ FUNC_NORETURN void z_riscv_fatal_error_csf(unsigned int reason, const struct arc
122117
LOG_ERR(" mepc: " PR_REG, esf->mepc);
123118
LOG_ERR("mstatus: " PR_REG, esf->mstatus);
124119
LOG_ERR("");
120+
121+
csf = esf->csf;
125122
}
126123

127124
if (csf != NULL) {
@@ -248,6 +245,12 @@ void z_impl_user_fault(unsigned int reason)
248245
{
249246
struct arch_esf *oops_esf = _current->syscall_frame;
250247

248+
#ifdef CONFIG_EXCEPTION_DEBUG
249+
if (oops_esf != NULL) {
250+
oops_esf->csf = NULL;
251+
}
252+
#endif /* CONFIG_EXCEPTION_DEBUG */
253+
251254
if (((_current->base.user_options & K_USER) != 0) &&
252255
reason != K_ERR_STACK_CHK_FAIL) {
253256
reason = K_ERR_KERNEL_OOPS;

arch/riscv/core/isr.S

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,7 @@ GTEXT(__soc_save_context)
7878
GTEXT(__soc_restore_context)
7979
#endif /* CONFIG_RISCV_SOC_CONTEXT_SAVE */
8080

81-
#ifdef CONFIG_EXCEPTION_DEBUG
82-
GTEXT(z_riscv_fatal_error_csf)
83-
#else
8481
GTEXT(z_riscv_fatal_error)
85-
#endif /* CONFIG_EXCEPTION_DEBUG */
86-
8782
GTEXT(z_get_next_switch_handle)
8883
GTEXT(z_riscv_switch)
8984
GTEXT(z_riscv_thread_start)
@@ -468,19 +463,13 @@ do_fault:
468463
/* Allocate space for caller-saved registers on current thread stack */
469464
addi sp, sp, -__callee_saved_t_SIZEOF
470465

471-
/* Save callee-saved registers to be passed as 3rd arg */
466+
/* Save callee-saved registers */
472467
STORE_CALLEE_SAVED() ;
473-
mv a2, sp
474468

475-
#ifdef CONFIG_EXTRA_EXCEPTION_INFO
476469
/* Store csf's addr into esf (a1 still holds the pointer to the esf at this point) */
477-
sr a2 __struct_arch_esf_csf_OFFSET(a1)
478-
#endif /* CONFIG_EXTRA_EXCEPTION_INFO */
479-
480-
tail z_riscv_fatal_error_csf
481-
#else
482-
tail z_riscv_fatal_error
470+
sr sp __struct_arch_esf_csf_OFFSET(a1)
483471
#endif /* CONFIG_EXCEPTION_DEBUG */
472+
tail z_riscv_fatal_error
484473

485474
#if defined(CONFIG_IRQ_OFFLOAD)
486475
do_irq_offload:

arch/riscv/core/offsets/offsets.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,6 @@ GEN_OFFSET_STRUCT(arch_esf, s0);
122122
GEN_OFFSET_STRUCT(arch_esf, sp);
123123
#endif
124124

125-
#ifdef CONFIG_EXTRA_EXCEPTION_INFO
126-
GEN_OFFSET_STRUCT(arch_esf, csf);
127-
#endif /* CONFIG_EXTRA_EXCEPTION_INFO */
128-
129125
#if defined(CONFIG_RISCV_SOC_CONTEXT_SAVE)
130126
GEN_OFFSET_STRUCT(arch_esf, soc_context);
131127
#endif
@@ -136,6 +132,7 @@ GEN_SOC_OFFSET_SYMS();
136132
GEN_ABSOLUTE_SYM(__struct_arch_esf_SIZEOF, sizeof(struct arch_esf));
137133

138134
#ifdef CONFIG_EXCEPTION_DEBUG
135+
GEN_OFFSET_STRUCT(arch_esf, csf);
139136
GEN_ABSOLUTE_SYM(__callee_saved_t_SIZEOF, ROUND_UP(sizeof(_callee_saved_t), ARCH_STACK_PTR_ALIGN));
140137
#endif /* CONFIG_EXCEPTION_DEBUG */
141138

arch/riscv/include/kernel_arch_func.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,9 @@ arch_switch(void *switch_to, void **switched_from)
7474
#endif
7575
}
7676

77-
/* Thin wrapper around z_riscv_fatal_error_csf */
7877
FUNC_NORETURN void z_riscv_fatal_error(unsigned int reason,
7978
const struct arch_esf *esf);
8079

81-
FUNC_NORETURN void z_riscv_fatal_error_csf(unsigned int reason, const struct arch_esf *esf,
82-
const _callee_saved_t *csf);
83-
8480
static inline bool arch_is_in_isr(void)
8581
{
8682
#ifdef CONFIG_SMP

doc/releases/migration-guide-4.2.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,3 +760,9 @@ Architectures
760760
of vector table in RAM.
761761
* Renamed :kconfig:option:`CONFIG_DEBUG_INFO` to :kconfig:option:`CONFIG_X86_DEBUG_INFO` to
762762
better reflect its purpose. This option is now only available for x86 architecture.
763+
764+
* RISCV
765+
766+
* :kconfig:option:`CONFIG_EXTRA_EXCEPTION_INFO` has been removed, the ``*csf`` pointer will be
767+
available in the ``struct arch_esf`` when :kconfig:option:`CONFIG_EXCEPTION_DEBUG` is
768+
enabled.

include/zephyr/arch/riscv/exception.h

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

48-
#ifdef CONFIG_EXTRA_EXCEPTION_INFO
48+
#ifdef CONFIG_EXCEPTION_DEBUG
4949
/* Forward declaration */
5050
struct _callee_saved;
5151
typedef struct _callee_saved _callee_saved_t;
52-
#endif /* CONFIG_EXTRA_EXCEPTION_INFO */
52+
#endif /* CONFIG_EXCEPTION_DEBUG */
5353

5454
#if defined(CONFIG_RISCV_SOC_HAS_ISR_STACKING)
5555
SOC_ISR_STACKING_ESF_DECLARE;
@@ -91,9 +91,9 @@ struct arch_esf {
9191
unsigned long sp; /* preserved (user or kernel) stack pointer */
9292
#endif
9393

94-
#ifdef CONFIG_EXTRA_EXCEPTION_INFO
94+
#ifdef CONFIG_EXCEPTION_DEBUG
9595
_callee_saved_t *csf; /* pointer to callee-saved-registers */
96-
#endif /* CONFIG_EXTRA_EXCEPTION_INFO */
96+
#endif /* CONFIG_EXCEPTION_DEBUG */
9797

9898
#ifdef CONFIG_RISCV_SOC_CONTEXT_SAVE
9999
struct soc_esf soc_context;

soc/nordic/common/vpr/soc_isr_stacking.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,20 @@
1414

1515
#define VPR_CPU DT_INST(0, nordic_vpr)
1616

17+
#ifdef CONFIG_EXCEPTION_DEBUG
18+
#define ESF_CSF _callee_saved_t *csf
19+
#else
20+
#define ESF_CSF
21+
#endif /* CONFIG_EXCEPTION_DEBUG */
22+
1723
#if DT_PROP(VPR_CPU, nordic_bus_width) == 64
1824

1925
#define SOC_ISR_STACKING_ESF_DECLARE \
2026
struct arch_esf { \
2127
unsigned long s0; \
2228
unsigned long mstatus; \
2329
struct soc_esf soc_context; \
30+
ESF_CSF; \
2431
\
2532
unsigned long t2; \
2633
unsigned long ra; \
@@ -43,6 +50,7 @@
4350
unsigned long s0; \
4451
unsigned long mstatus; \
4552
struct soc_esf soc_context; \
53+
ESF_CSF; \
4654
\
4755
unsigned long ra; \
4856
unsigned long t2; \

0 commit comments

Comments
 (0)