riscv: ISR stack is too small for ENABLE_DEBUG in core files #16395
Labels
Area: cpu
Area: CPU/MCU ports
Platform: RISC-V
Platform: This PR/issue effects RISC-V-based platforms
Type: bug
The issue reports a bug / The PR fixes a bug (including spelling errors)
Description
I wanted to debug the RIOT thread creation code on the SiFive HiFive1, for this purpose I activated
ENABLE_DEBUG
incore/thread.c
:RIOT/core/thread.c
Lines 34 to 35 in 1dde0f4
Interestingly, some functions from
thread.c
are not executed on any thread stack but instead use the ISR/Exception stack as defined by_sp
in theriscv_common
ldscript:RIOT/cpu/riscv_common/ldscripts/riscv_base.ld
Lines 210 to 215 in b3b04fa
For example, consider that
kernel_init
, which callsthread_create
fromstart.S
, is executed with this stack. Unfortunately, the default stack size is quite small with 256 bytes:RIOT/cpu/riscv_common/ldscripts/riscv_base.ld
Line 35 in b3b04fa
This seems to be too small for using the debugging macros from
debug.h
which on RISC-V would normally require an additional 256 bytes of stack space:RIOT/cpu/riscv_common/include/cpu_conf_common.h
Lines 29 to 31 in b3b04fa
Unfortunately, the
DEBUG_EXTRA_STACKSIZE
macro, which is often used to increase stack space ifENABLE_DEBUG
is activated can't really be used here as the size of the region in the ldscript would need to be increased in this case. Since the ISR stack is also not a normal RIOT thread, the following sanity check fromdebug.h
does also not work correctly:RIOT/core/include/debug.h
Lines 49 to 56 in 2df29a6
As such, I encountered several stack overflows when using
ENABLE_DEBUG
incore/thread.c
on the HiFive1. This may lead to weird behavior during debugging and it would be nice if this could somehow be detected to avoid this pitfall during debugging. Also note that the heuristic employed by the scheduler (SCHED_TEST_STACK
) to detect stack overflows does not work for the same reason. Maybe the sanity check indebug.h
can be improved by adding an edge case for code running on the ISR stack? Furthermore, it would also be neat to improve theSCHED_TEST_STACK
to also cover the ISR stack somehow.Steps to reproduce the issue
This issue should be reproducible using the following GDB script:
This script can, for instance, be used with the hello-world application. Assuming
ENABLE_DEBUG
has been enabled incore/thread.c
, the application can be compiled as follows:Afterwards, extract the lower bound of the ISR stack (the ISR stack grows downwards towards
_eheap
) and add it to the while condition of the gdb script. The lower bound of the ISR stack should match the address of the_eheap
symbol which can be extracted as follows:Furthermore, the script requires debug symbols in newlib to find the internal
_vfprintf_r
symbol. Executing the GDB script results in the following output on my system:Though this is a bit hacky, so the issue might not be as easily reproducible using the script. However, I think the outlined problem is hopefully also fairly obvious from the description above.
The text was updated successfully, but these errors were encountered: