zephyrproject-rtos / zephyr

Primary Git Repository for the Zephyr Project. Zephyr is a new generation, scalable, optimized, secure RTOS for multiple hardware architectures.
https://docs.zephyrproject.org
Apache License 2.0
10.86k stars 6.62k forks source link

Usage fault when running with CONFIG_NO_OPTIMIZATIONS=y #27768

Closed JaapBosheuvel closed 4 years ago

JaapBosheuvel commented 4 years ago

Describe the bug I get an error message on the terminal when running the sample: _$ZEPHYR_BASE/zephyr/samples/bluetooth/peripherht with the CONFIG_NO_OPTIMIZATIONS=y flag. This error message comes up immediately:

*** Booting Zephyr OS build zephyr-v2.3.0-2080-gc64e0ce923cd  ***
Bluetooth initialized
temp device is 0x20000710, name is TEMP_0
[00:00:00.009,063] <inf> bt_hci_core: HW Platform: Nordic Semiconductor (0x0002)
[00:00:00.009,094] <inf> bt_hci_core: HW Variant: nRF52x (0x0002)
[00:00:00.009,094] <inf> bt_hci_core: Firmware: Standard Bluetooth controller (0x00) Version 2.3 Build 99
[00:00:00.010,894] <inf> bt_hci_core: Identity: ce:01:66:73:19:0c (random)
[00:00:00.010,894] <inf> bt_hci_core: HCI: version 5.2 (0x0b) revision 0x0000, manufacturer 0x05f1
[00:00:00.010,925] <inf> bt_hci_core: LMP: version 5.2 (0x0b) subver 0xffff
[00:00:00.018,432] <err> os: ***** USAGE FAULT *****
[00:00:00.018,432] <err> os:   Illegal load of EXC_RETURN into PC
[00:00:00.018,463] <err> os: r0/a1:  0x0000a31d  r1/a2:  0x00000000  r2/a3:  0x00000000
[00:00:00.018,463] <err> os: r3/a4:  0x00000000 r12/ip:  0xbfe617ff r14/lr:  0x40011000
[00:00:00.018,493] <err> os:  xpsr:  0x00000200
[00:00:00.018,493] <err> os: Faulting instruction address (r15/pc): 0x00000000
[00:00:00.018,524] <err> os: >>> ZEPHYR FATAL ERROR 0: CPU exception on CPU 0
[00:00:00.018,524] <err> os: Current thread: 0x20000a00 (unknown)
[00:00:01.555,633] <err> os: Halting system

When I run the binary in my debugger it will not crash immediately and advertises. But when I try to connect with nRF Connect app on android the terminal shows the following error message:

*** Booting Zephyr OS build zephyr-v2.3.0-2080-gc64e0ce923cd  ***
Bluetooth initialized
temp device is 0x20000710, name is TEMP_0
Advertising successfully started
[00:00:01.373,535] <inf> bt_hci_core: HW Platform: Nordic Semiconductor (0x0002)
[00:00:01.373,565] <inf> bt_hci_core: HW Variant: nRF52x (0x0002)
[00:00:01.373,565] <inf> bt_hci_core: Firmware: Standard Bluetooth controller (0x00) Version 2.3 Build 99
[00:00:01.375,305] <inf> bt_hci_core: Identity: ce:01:66:73:19:0c (random)
[00:00:01.375,305] <inf> bt_hci_core: HCI: version 5.2 (0x0b) revision 0x0000, manufacturer 0x05f1
[00:00:01.375,335] <inf> bt_hci_core: LMP: version 5.2 (0x0b) subver 0xffff
Connected
[00:00:24.673,614] <err> os: ***** USAGE FAULT *****
[00:00:24.673,675] <err> os:   Illegal use of the EPSR
[00:00:24.673,675] <err> os: r0/a1:  0x20003cf8  r1/a2:  0x000412c7  r2/a3:  0x20000a80
[00:00:24.673,706] <err> os: r3/a4:  0x20000a80 r12/ip:  0x20003d08 r14/lr:  0x20003d08
[00:00:24.673,706] <err> os:  xpsr:  0x00000000
[00:00:24.673,736] <err> os: Faulting instruction address (r15/pc): 0x00000080
[00:00:24.673,736] <err> os: >>> ZEPHYR FATAL ERROR 0: CPU exception on CPU 0
[00:00:24.673,767] <err> os: Current thread: 0x20000ec8 (unknown)
[00:00:24.965,270] <err> os: Halting system

NOTE that the same kind of behavior also happened with sample peripheral_hr

To Reproduce Tested on commit: c64e0ce923cde54415d203e447a1dc62894ed2b0

cd $ZEPHYR_BASE/zephyr/
echo "CONFIG_NO_OPTIMIZATIONS=y" >> samples/bluetooth/peripheral_ht/prj.conf 
west build -p auto -b nrf52dk_nrf52832 samples/bluetooth/peripheral_ht/
west flash

I check the terminal with putty, and debug with SEGGER Ozone using the zephyr.elf at _$ZEPHYRBASE/zephyr/build/zephyr/zephyr.elf with the recommended settings (SWD and 4MHz).

Expected behavior Same execution as without the optimization flag

Impact The ability to debug without optimizations

Logs and console output see above

Environment (please complete the following information):

Additional context Might have similarities with: https://github.com/zephyrproject-rtos/zephyr/issues/12820 ? Could not resolve the issue after reading it.

Kind regards, John Hendriks

carlescufi commented 4 years ago

This is a stack overflow, which is natural given that the stack sizes are going to increase with no optimizations enabled.

There's also a small bug to fix, which is that the RX priority thread in the HCI driver had a non-configurable stack size. This is fixed in #28467.

To find it, I did the following:

echo "CONFIG_NO_OPTIMIZATIONS=y" >> samples/bluetooth/peripheral_ht/prj.conf 
echo "CONFIG_HW_STACK_PROTECTION=y" >> samples/bluetooth/peripheral_ht/prj.conf 

and then played around with stack sizes.

This now runs fine with this configuration:

west build samples/bluetooth/peripheral_ht -- -DCONFIG_LOG_PROCESS_THREAD_STACK_SIZE=4096 -DCONFIG_BT_RX_STACK_SIZE=4096 -DCONFIG_BT_CTLR_RX_PRIO_STACK_SIZE=4096