1

描述错误

我目前正在尝试在配备 MCU 的开发板上运行 Zephyr7.0:NXP MIMXRT1061 CVL5A。我只是编译了一个示例:\samples\basic\blinky,但它无法正确运行。一开始以为是XIP格式的问题导致Zephyr无法正确启动,后来用SWD调试,发现启动正确。但是调用时:/zephyr/arch/arm/core/aarch32/prep_c.c: z_bss_zero(); 功能 Zephyr 出错了

编译过程

我用的板子是mimxrt1060_evk,理论上是没有问题的,因为1061是基于1060的

  1. west build -p auto -b mimxrt1060_evk .\samples\basic\blinky\
  2. 西闪

调试过程

第一次调试在 z_interrupt_stacks 被破坏

z_arm_reset () at C:/Users/zhihao3x/work/zephyrproject/zephyr/arch/arm/core/aarch32/cortex_m\reset.S:105
105         msr BASEPRI, r0
(gdb) n
134         ldr r0, =z_interrupt_stacks
(gdb) n
135         ldr r1, =CONFIG_ISR_STACK_SIZE + MPU_GUARD_ALIGN_AND_SIZE
(gdb) n
136         adds r0, r0, r1
(gdb) n
137         msr PSP, r0
(gdb)
138         mrs r0, CONTROL
(gdb)
139         movs r1, #2
(gdb)
140         orrs r0, r1 /* CONTROL_SPSEL_Msk */
(gdb)
141         msr CONTROL, r0
(gdb)
147         isb
(gdb)
154         bl z_arm_prep_c
(gdb)
134         ldr r0, =z_interrupt_stacks
(gdb)

Program received signal SIGTRAP, Trace/breakpoint trap.
(gdb)

后来用单步调试,发现Zephyr达到了z_bss_zero

(gdb)
z_arm_floating_point_init ()
    at C:/Users/zhihao3x/work/zephyrproject/modules/hal/cmsis/CMSIS/Core/Include/cmsis_gcc.h:1003
1003      __ASM volatile ("MSR control, %0" : : "r" (control) : "memory");
(gdb)
163             __set_CONTROL(__get_CONTROL() & (~(CONTROL_FPCA_Msk)));
(gdb)
0x6000305c in __set_CONTROL (control=3758157056)
    at C:/Users/zhihao3x/work/zephyrproject/modules/hal/cmsis/CMSIS/Core/Include/cmsis_gcc.h:1003
1003      __ASM volatile ("MSR control, %0" : : "r" (control) : "memory");
(gdb)
1004      __ISB();
(gdb)
__ISB () at C:/Users/zhihao3x/work/zephyrproject/modules/hal/cmsis/CMSIS/Core/Include/cmsis_gcc.h:260
260       __ASM volatile ("isb 0xF":::"memory");
(gdb)
z_arm_prep_c () at C:/Users/zhihao3x/work/zephyrproject/zephyr/arch/arm/core/aarch32/prep_c.c:183
183             z_bss_zero();
(gdb) n

Program received signal SIGTRAP, Trace/breakpoint trap.
0x62652dc0 in ?? ()

最终我找到了memset函数的问题,当这个函数被调用时,它会陷入死循环,然后我的程序就会崩溃

z_arm_prep_c () at C:/Users/zhihao3x/work/zephyrproject/zephyr/arch/arm/core/aarch32/prep_c.c:183
183             z_bss_zero();
(gdb)
z_bss_zero () at C:/Users/zhihao3x/work/zephyrproject/zephyr/kernel/init.c:89
89              (void)memset(__bss_start, 0, __bss_end - __bss_start);
(gdb)
memset (buf=0x80000030 <z_idle_threads>, c=c@entry=0, n=428)
    at C:/Users/zhihao3x/work/zephyrproject/zephyr/lib/libc/minimal/source/string/string.c:355

(gdb)

Program received signal SIGTRAP, Trace/breakpoint trap.
0x63f5fbf8 in ?? ()

后来我尝试调试memset函数,发现dest地址在循环过程中没有变化很奇怪,我没有具体说明是gdb问题还是zephyr问题。地址在递增前为 0x80000030,递增后为 0x80000031,重复下一个周期时又变回 0x80000030。

357             unsigned char c_byte = (unsigned char)c;
(gdb)
389             while (n > 0) {
(gdb)
390                     *(d_byte++) = c_byte;
392                     n--;
(gdb) p d_byte
$1 = (unsigned char *) 0x80000030 <z_idle_threads> "\b"

我试图改变 z_bss_zero 函数

(void)memset(__bss_start, 0, __bss_end - __bss_start);

改变了尺寸长度

(void)memset(__bss_start, 0, 3);

但是我在用gdb调试的时候发现memset里面的while循环不停的超过了3倍,进入了死循环。我推测是这两行代码后递增的代码有问题,因为我每次递增后使用gdb print命令打印变量的值时,变量的值并没有变化

*(d_byte++) = c_byte;
        n--;

另一个奇怪的地方是,当我使用gdb进行调试时,我发现有一个指针。gdb不会断到这边,而是跳过这段代码执行。我怀疑编译器是否对我这样做。代码优化了吗?

更多细节

当我使用 next 执行 z_bss_zero 时,gdb 将中断到 0xdeadbeee 处的不确定地址。我猜可能是Zephyr在初始化内存时破坏了堆栈。

(gdb)
z_arm_prep_c () at C:/Users/zhihao3x/work/zephyrproject/zephyr/arch/arm/core/aarch32/prep_c.c:183
183             z_bss_zero();
(gdb) n

Program received signal SIGTRAP, Trace/breakpoint trap.
0xdeadbeee in ?? ()

同时Jlink调试器也输出错误日志

ERROR: Cannot read register 15 (R15) while CPU is running
Reading all registers
ERROR: Cannot read register 0 (R0) while CPU is running
ERROR: Cannot read register 1 (R1) while CPU is running
ERROR: Cannot read register 2 (R2) while CPU is running
ERROR: Cannot read register 3 (R3) while CPU is running
ERROR: Cannot read register 4 (R4) while CPU is running
ERROR: Cannot read register 5 (R5) while CPU is running
ERROR: Cannot read register 6 (R6) while CPU is running
ERROR: Cannot read register 7 (R7) while CPU is running
ERROR: Cannot read register 8 (R8) while CPU is running
ERROR: Cannot read register 9 (R9) while CPU is running
ERROR: Cannot read register 10 (R10) while CPU is running
ERROR: Cannot read register 11 (R11) while CPU is running
ERROR: Cannot read register 12 (R12) while CPU is running
ERROR: Cannot read register 13 (R13) while CPU is running
ERROR: Cannot read register 14 (R14) while CPU is running
ERROR: Cannot read register 15 (R15) while CPU is running
ERROR: Cannot read register 16 (XPSR) while CPU is running
ERROR: Cannot read register 17 (MSP) while CPU is running
ERROR: Cannot read register 18 (PSP) while CPU is running
ERROR: Cannot read register 24 (PRIMASK) while CPU is running
ERROR: Cannot read register 25 (BASEPRI) while CPU is running
ERROR: Cannot read register 26 (FAULTMASK) while CPU is running
ERROR: Cannot read register 27 (CONTROL) while CPU is running
ERROR: Cannot read register 32 (FPSCR) while CPU is running
ERROR: Cannot read register 33 (FPS0) while CPU is running
ERROR: Cannot read register 34 (FPS1) while CPU is running
ERROR: Cannot read register 35 (FPS2) while CPU is running
ERROR: Cannot read register 36 (FPS3) while CPU is running
ERROR: Cannot read register 37 (FPS4) while CPU is running
ERROR: Cannot read register 38 (FPS5) while CPU is running
ERROR: Cannot read register 39 (FPS6) while CPU is running
ERROR: Cannot read register 40 (FPS7) while CPU is running
ERROR: Cannot read register 41 (FPS8) while CPU is running
ERROR: Cannot read register 42 (FPS9) while CPU is running
ERROR: Cannot read register 43 (FPS10) while CPU is running
ERROR: Cannot read register 44 (FPS11) while CPU is running
ERROR: Cannot read register 45 (FPS12) while CPU is running
ERROR: Cannot read register 46 (FPS13) while CPU is running
ERROR: Cannot read register 47 (FPS14) while CPU is running
ERROR: Cannot read register 48 (FPS15) while CPU is running
ERROR: Cannot read register 49 (FPS16) while CPU is running
ERROR: Cannot read register 50 (FPS17) while CPU is running
ERROR: Cannot read register 51 (FPS18) while CPU is running
ERROR: Cannot read register 52 (FPS19) while CPU is running
ERROR: Cannot read register 53 (FPS20) while CPU is running
ERROR: Cannot read register 54 (FPS21) while CPU is running
ERROR: Cannot read register 55 (FPS22) while CPU is running
ERROR: Cannot read register 56 (FPS23) while CPU is running
ERROR: Cannot read register 57 (FPS24) while CPU is running
ERROR: Cannot read register 58 (FPS25) while CPU is running
ERROR: Cannot read register 59 (FPS26) while CPU is running
ERROR: Cannot read register 60 (FPS27) while CPU is running
ERROR: Cannot read register 61 (FPS28) while CPU is running
ERROR: Cannot read register 62 (FPS29) while CPU is running
ERROR: Cannot read register 63 (FPS30) while CPU is running
ERROR: Cannot read register 64 (FPS31) while CPU is running
ERROR: Cannot read register 33 (FPS0) while CPU is running
ERROR: Cannot read register 34 (FPS1) while CPU is running
ERROR: Cannot read register 35 (FPS2) while CPU is running
ERROR: Cannot read register 36 (FPS3) while CPU is running
ERROR: Cannot read register 37 (FPS4) while CPU is running
ERROR: Cannot read register 38 (FPS5) while CPU is running
ERROR: Cannot read register 39 (FPS6) while CPU is running
ERROR: Cannot read register 40 (FPS7) while CPU is running
ERROR: Cannot read register 41 (FPS8) while CPU is running
ERROR: Cannot read register 42 (FPS9) while CPU is running
ERROR: Cannot read register 43 (FPS10) while CPU is running
ERROR: Cannot read register 44 (FPS11) while CPU is running
ERROR: Cannot read register 45 (FPS12) while CPU is running
ERROR: Cannot read register 46 (FPS13) while CPU is running
ERROR: Cannot read register 47 (FPS14) while CPU is running
ERROR: Cannot read register 48 (FPS15) while CPU is running
ERROR: Cannot read register 49 (FPS16) while CPU is running
ERROR: Cannot read register 50 (FPS17) while CPU is running
ERROR: Cannot read register 51 (FPS18) while CPU is running
ERROR: Cannot read register 52 (FPS19) while CPU is running
ERROR: Cannot read register 53 (FPS20) while CPU is running
ERROR: Cannot read register 54 (FPS21) while CPU is running
ERROR: Cannot read register 55 (FPS22) while CPU is running
ERROR: Cannot read register 56 (FPS23) while CPU is running
ERROR: Cannot read register 57 (FPS24) while CPU is running
ERROR: Cannot read register 58 (FPS25) while CPU is running
ERROR: Cannot read register 59 (FPS26) while CPU is running
ERROR: Cannot read register 60 (FPS27) while CPU is running
ERROR: Cannot read register 61 (FPS28) while CPU is running
ERROR: Cannot read register 62 (FPS29) while CPU is running
ERROR: Cannot read register 63 (FPS30) while CPU is running
ERROR: Cannot read register 64 (FPS31) while CPU is running
Removing breakpoint @ address 0x60003068, Size = 2
WARNING: Failed to read memory @ address 0xDEADBEEE

Jlink 输出

以下是Jlink输出的MCU信息,不知道Zephyr是否支持这个版本

-----GDB Server start settings-----
GDBInit file:                  none
GDB Server Listening port:     2331
SWO raw output listening port: 2332
Terminal I/O port:             2333
Accept remote connection:      localhost only
Generate logfile:              off
Verify download:               off
Init regs on start:            off
Silent mode:                   on
Single run mode:               on
Target connection timeout:     5000 ms
------J-Link related settings------
J-Link Host interface:         USB
J-Link script:                 none
J-Link settings file:          none
------Target related settings------
Target device:                 MIMXRT1062xxx6A
Target interface:              SWD
Target interface speed:        auto
Target endian:                 little

我已经努力了7天,仍然无法解决这个问题。我尝试切换Zephyr的版本,也使用平台IO生成elf和bin文件,但都无效。请帮我。非常感谢。

4

0 回答 0