From b79a68fc496974d5a86953edee76ab1946dc93ed Mon Sep 17 00:00:00 2001 From: Yong Cong Sin Date: Wed, 19 Jun 2024 16:57:07 +0800 Subject: [PATCH] arch: riscv: stacktrace: refactor fatal stack bound Pass the current thread to `walk_stackframe()`, so that we do not need to hardcode `_current` in `in_fatal_stack_bound()`, which will allow it to reuse the `in_stack_bound()` Signed-off-by: Yong Cong Sin --- arch/riscv/core/stacktrace.c | 44 +++++++++++++++--------------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/arch/riscv/core/stacktrace.c b/arch/riscv/core/stacktrace.c index 563d6cad144..a78216dcb3c 100644 --- a/arch/riscv/core/stacktrace.c +++ b/arch/riscv/core/stacktrace.c @@ -69,31 +69,6 @@ static inline bool in_user_thread_stack_bound(uintptr_t addr, const struct k_thr } #endif /* CONFIG_USERSPACE */ -static bool in_fatal_stack_bound(uintptr_t addr, const struct k_thread *const thread, - const struct arch_esf *esf) -{ - ARG_UNUSED(thread); - - if (!IS_ALIGNED(addr, sizeof(uintptr_t))) { - return false; - } - - if (_current == NULL || arch_is_in_isr()) { - /* We were servicing an interrupt */ - uint8_t cpu_id = IS_ENABLED(CONFIG_SMP) ? arch_curr_cpu()->id : 0U; - - return in_irq_stack_bound(addr, cpu_id); - } -#ifdef CONFIG_USERSPACE - if ((esf != NULL) && ((esf->mstatus & MSTATUS_MPP) == PRV_U) && - ((_current->base.user_options & K_USER) != 0)) { - return in_user_thread_stack_bound(addr, _current); - } -#endif /* CONFIG_USERSPACE */ - - return in_kernel_thread_stack_bound(addr, _current); -} - static bool in_stack_bound(uintptr_t addr, const struct k_thread *const thread, const struct arch_esf *esf) { @@ -112,6 +87,23 @@ static bool in_stack_bound(uintptr_t addr, const struct k_thread *const thread, return in_kernel_thread_stack_bound(addr, thread); } +static bool in_fatal_stack_bound(uintptr_t addr, const struct k_thread *const thread, + const struct arch_esf *esf) +{ + if (!IS_ALIGNED(addr, sizeof(uintptr_t))) { + return false; + } + + if ((thread == NULL) || arch_is_in_isr()) { + /* We were servicing an interrupt */ + uint8_t cpu_id = IS_ENABLED(CONFIG_SMP) ? arch_curr_cpu()->id : 0U; + + return in_irq_stack_bound(addr, cpu_id); + } + + return in_stack_bound(addr, thread, esf); +} + static inline bool in_text_region(uintptr_t addr) { extern uintptr_t __text_region_start, __text_region_end; @@ -245,6 +237,6 @@ void z_riscv_unwind_stack(const struct arch_esf *esf, const _callee_saved_t *csf int i = 0; LOG_ERR("call trace:"); - walk_stackframe(print_trace_address, &i, NULL, esf, in_fatal_stack_bound, csf); + walk_stackframe(print_trace_address, &i, _current, esf, in_fatal_stack_bound, csf); LOG_ERR(""); }