Amanieu / corosensei

A fast and safe implementation of stackful coroutines in Rust
Apache License 2.0
170 stars 13 forks source link

Embedded use with ARM M0+ #6

Closed kpishere closed 2 years ago

kpishere commented 2 years ago

@Amanieu I've learned lots trying to use your crate ;) But still, I'm not seeing how/where this error is coming from.

The state of my project is in repo+branch https://github.com/kpishere/rp2040-project-template/tree/steps.

If I include the following lines

let mut coroutine = Coroutine::with_stack(stack, |yielder, input| {
        info!("[coroutine] coroutine started with input {}", input);
        for i in 0..5 {
            info!("[coroutine] yielding {}", i);
            let input: i32 = yielder.suspend(i);
            info!("[coroutine] got {} from parent", input)
        }
        info!("[coroutine] exiting coroutine");
    });

I get this error

  = note: rust-lld: error: section '.ARM.extab' will not fit in region 'BOOT2': overflowed by 16 bytes
          rust-lld: error: section '.ARM.extab' will not fit in region 'BOOT2': overflowed by 16 bytes
          rust-lld: error: section '.ARM.extab' will not fit in region 'BOOT2': overflowed by 16 bytes
          rust-lld: error: section .vector_table virtual address range overlaps with .ARM.extab
          >>> .vector_table range is [0x10000100, 0x100001A7]
          >>> .ARM.extab range is [0x10000100, 0x1000010F]

          rust-lld: error: section .vector_table load address range overlaps with .ARM.extab
          >>> .vector_table range is [0x10000100, 0x100001A7]
          >>> .ARM.extab range is [0x10000100, 0x1000010F]

I don't see any .ARM.extab section in your crate. Not sure where to look.

Amanieu commented 2 years ago

Edit your linker script to explicitly discard the .ARM.extab section. It's only used for runtime stack unwinding, but you're probably not using that.

/DISCARD/ : {
    *(.ARM.extab* .gnu.linkonce.armextab.* .ARM.exidx* .gnu.linkonce.armexidx.*)
}

If this fixes your problem then it might be worth upstreaming it to the cortex-m linker script: https://github.com/rust-embedded/cortex-m/blob/4e908625204a1e95dd3fd5bdcd8d66d6bc11c3bc/cortex-m-rt/link.x.in#L191

kpishere commented 2 years ago

@Amanieu Yep, it links now. Thanks. Not understanding why that appears though.

It crashes with a HardFault but will investigate that more. FYI .. attached picture of hard fault Screen Shot 2022-07-06 at 11 41 05 AM

.

Amanieu commented 2 years ago

Can you show the disassembly where the fault happens?

kpishere commented 2 years ago

@Amanieu Gladly.

Thread 1 hit Breakpoint 4, corosensei::arch::arm::switch_and_link (sp=..., stack_base=..., arg=<optimized out>) at /Users/kevinpeck/Desktop/RepRap-RatRigVCore3/corosensei/src/arch/arm.rs:442
442     asm_may_unwind!(
disassemble  corosensei::arch::arm::switch_and_link
Dump of assembler code for function rp2040_project_template::__cortex_m_rt_main:
 0x100006ec <+0>:   push    {r7, lr}
   0x100006ee <+2>: add r7, sp, #0
   0x100006f0 <+4>: sub sp, #120    ; 0x78
   0x100006f2 <+6>: movs    r6, #1
   0x100006f4 <+8>: lsls    r2, r6, #10
   0x100006f6 <+10>:    ldr r0, [pc, #592]  ; (0x10000948 <rp2040_project_template::__cortex_m_rt_main+604>)
   0x100006f8 <+12>:    ldr r1, [pc, #592]  ; (0x1000094c <rp2040_project_template::__cortex_m_rt_main+608>)
   0x100006fa <+14>:    bl  0x10002478 <alloc_cortex_m::CortexMHeap::init>
   0x100006fe <+18>:    bl  0x10002248 <defmt_rtt::_defmt_acquire>
   0x10000702 <+22>:    ldr r0, [pc, #588]  ; (0x10000950 <rp2040_project_template::__cortex_m_rt_main+612>)
   0x10000704 <+24>:    bl  0x100023c0 <defmt::export::make_istr>
   0x10000708 <+28>:    add r1, sp, #56 ; 0x38
   0x1000070a <+30>:    strh    r0, [r1, #0]
   0x1000070c <+32>:    mov r0, r1
   0x1000070e <+34>:    bl  0x10002428 <defmt::export::header>
   0x10000712 <+38>:    bl  0x100022e0 <defmt_rtt::_defmt_release>
   0x10000716 <+42>:    bl  0x10002c4a <lib::__primask_r>
   0x1000071a <+46>:    mov r4, r0
   0x1000071c <+48>:    ands    r4, r6
   0x1000071e <+50>:    bl  0x10002c38 <lib::__cpsid>
   0x10000722 <+54>:    bl  0x10002c54 <bare_metal::CriticalSection::new>
   0x10000726 <+58>:    ldr r0, [pc, #556]  ; (0x10000954 <rp2040_project_template::__cortex_m_rt_main+616>)
   0x10000728 <+60>:    ldrb    r5, [r0, #0]
   0x1000072a <+62>:    cmp r5, #0
   0x1000072c <+64>:    bne.n   0x10000730 <rp2040_project_template::__cortex_m_rt_main+68>
   0x1000072e <+66>:    b.n 0x100008e6 <rp2040_project_template::__cortex_m_rt_main+506>
   0x10000730 <+68>:    cmp r4, #0
   0x10000732 <+70>:    bne.n   0x10000736 <rp2040_project_template::__cortex_m_rt_main+74>
   0x10000734 <+72>:    b.n 0x100008ee <rp2040_project_template::__cortex_m_rt_main+514>
   0x10000736 <+74>:    cmp r5, #0
   0x10000738 <+76>:    beq.n   0x1000073c <rp2040_project_template::__cortex_m_rt_main+80>
   0x1000073a <+78>:    b.n 0x100008f8 <rp2040_project_template::__cortex_m_rt_main+524>
   0x1000073c <+80>:    bl  0x10002c4a <lib::__primask_r>
   0x10000740 <+84>:    mov r4, r0
   0x10000742 <+86>:    ands    r4, r6
   0x10000744 <+88>:    bl  0x10002c38 <lib::__cpsid>
   0x10000748 <+92>:    bl  0x10002c54 <bare_metal::CriticalSection::new>
   0x1000074c <+96>:    ldr r0, [pc, #528]  ; (0x10000960 <rp2040_project_template::__cortex_m_rt_main+628>)
   0x1000074e <+98>:    ldrb    r5, [r0, #0]
   0x10000750 <+100>:   cmp r5, #0
   0x10000752 <+102>:   bne.n   0x10000756 <rp2040_project_template::__cortex_m_rt_main+106>
   0x10000754 <+104>:   b.n 0x10000904 <rp2040_project_template::__cortex_m_rt_main+536>
   0x10000756 <+106>:   cmp r4, #0
   0x10000758 <+108>:   bne.n   0x1000075c <rp2040_project_template::__cortex_m_rt_main+112>
   0x1000075a <+110>:   b.n 0x1000090c <rp2040_project_template::__cortex_m_rt_main+544>
  0x1000075c <+112>:    cmp r5, #0
   0x1000075e <+114>:   beq.n   0x10000762 <rp2040_project_template::__cortex_m_rt_main+118>
   0x10000760 <+116>:   b.n 0x10000916 <rp2040_project_template::__cortex_m_rt_main+554>
   0x10000762 <+118>:   str r6, [sp, #8]
   0x10000764 <+120>:   bl  0x10001560 <rp2040_hal::watchdog::Watchdog::new>
   0x10000768 <+124>:   str r0, [sp, #12]
   0x1000076a <+126>:   bl  0x10000ce0 <rp2040_hal::adc::Adc::free>
   0x1000076e <+130>:   add r4, sp, #56 ; 0x38
   0x10000770 <+132>:   ldr r1, [pc, #500]  ; (0x10000968 <rp2040_project_template::__cortex_m_rt_main+636>)
   0x10000772 <+134>:   add r2, sp, #116    ; 0x74
   0x10000774 <+136>:   add r3, sp, #12
   0x10000776 <+138>:   mov r0, r4
   0x10000778 <+140>:   bl  0x10001014 <rp2040_hal::clocks::init_clocks_and_plls>
   0x1000077c <+144>:   ldrb    r0, [r4, #0]
   0x1000077e <+146>:   cmp r0, #0
   0x10000780 <+148>:   beq.n   0x10000784 <rp2040_project_template::__cortex_m_rt_main+152>
   0x10000782 <+150>:   b.n 0x1000093a <rp2040_project_template::__cortex_m_rt_main+590>
   0x10000784 <+152>:   add r0, sp, #56 ; 0x38
   0x10000786 <+154>:   adds    r1, r0, #4
   0x10000788 <+156>:   add r0, sp, #16
   0x1000078a <+158>:   mov r2, r0
   0x1000078c <+160>:   ldmia   r1!, {r3, r4, r5}
   0x1000078e <+162>:   stmia   r2!, {r3, r4, r5}
   0x10000790 <+164>:   ldmia   r1!, {r3, r4, r5}
   0x10000792 <+166>:   stmia   r2!, {r3, r4, r5}
   0x10000794 <+168>:   ldmia   r1!, {r3, r4, r5, r6}
   0x10000796 <+170>:   stmia   r2!, {r3, r4, r5, r6}
   0x10000798 <+172>:   adds    r0, #20
   0x1000079a <+174>:   bl  0x1000155c <rp2040_hal::rosc::RingOscillator<rp2040_hal::rosc::Enabled>::operating_frequency>
   0x1000079e <+178>:   ldr r1, [pc, #464]  ; (0x10000970 <rp2040_project_template::__cortex_m_rt_main+644>)
   0x100007a0 <+180>:   ldr r2, [r1, #0]
   0x100007a2 <+182>:   movs    r5, #4
   0x100007a4 <+184>:   orrs    r2, r5
   0x100007a6 <+186>:   str r2, [r1, #0]
   0x100007a8 <+188>:   str r0, [sp, #100]  ; 0x64
   0x100007aa <+190>:   add r0, sp, #116    ; 0x74
   0x100007ac <+192>:   bl  0x10001934 <rp2040_hal::gpio::pin::bank0::Pins::new>
   0x100007b0 <+196>:   ldr r0, [pc, #448]  ; (0x10000974 <rp2040_project_template::__cortex_m_rt_main+648>)
   0x100007b2 <+198>:   movs    r1, #18
   0x100007b4 <+200>:   str r1, [r0, #0]
   0x100007b6 <+202>:   ldr r0, [pc, #448]  ; (0x10000978 <rp2040_project_template::__cortex_m_rt_main+652>)
   0x100007b8 <+204>:   movs    r1, #5
  0x100007ba <+206>:    str r1, [r0, #0]
   0x100007bc <+208>:   ldr r6, [sp, #8]
   0x100007be <+210>:   lsls    r1, r6, #25
   0x100007c0 <+212>:   ldr r0, [pc, #440]  ; (0x1000097c <rp2040_project_template::__cortex_m_rt_main+656>)
   0x100007c2 <+214>:   str r1, [sp, #0]
   0x100007c4 <+216>:   str r1, [r0, #16]
   0x100007c6 <+218>:   movs    r4, #0
   0x100007c8 <+220>:   str r4, [sp, #64]   ; 0x40
   0x100007ca <+222>:   str r4, [sp, #60]   ; 0x3c
   0x100007cc <+224>:   str r5, [sp, #56]   ; 0x38
   0x100007ce <+226>:   add r0, sp, #56 ; 0x38
   0x100007d0 <+228>:   mov r1, r4
   0x100007d2 <+230>:   bl  0x100005d4 <alloc::raw_vec::RawVec<i32, alloc::alloc::Global>::reserve_for_push<i32, alloc::alloc::Global>>
   0x100007d6 <+234>:   ldr r0, [sp, #64]   ; 0x40
   0x100007d8 <+236>:   lsls    r1, r0, #2
   0x100007da <+238>:   ldr r2, [sp, #56]   ; 0x38
   0x100007dc <+240>:   str r6, [r2, r1]
   0x100007de <+242>:   adds    r0, r0, #1
   0x100007e0 <+244>:   str r0, [sp, #64]   ; 0x40
   0x100007e2 <+246>:   lsls    r5, r6, #12
   0x100007e4 <+248>:   movs    r1, #8
   0x100007e6 <+250>:   mov r0, r5
   0x100007e8 <+252>:   bl  0x100009bc <__rust_alloc_zeroed>
   0x100007ec <+256>:   adds    r1, r0, r5
   0x100007ee <+258>:   adcs    r4, r4
   0x100007f0 <+260>:   str r1, [sp, #4]
   0x100007f2 <+262>:   cmp r1, r0
   0x100007f4 <+264>:   bcs.n   0x100007f8 <rp2040_project_template::__cortex_m_rt_main+268>
   0x100007f6 <+266>:   b.n 0x1000092e <rp2040_project_template::__cortex_m_rt_main+578>
   0x100007f8 <+268>:   ldr r6, [sp, #4]
   0x100007fa <+270>:   mov r0, r6
   0x100007fc <+272>:   subs    r0, #12
   0x100007fe <+274>:   ldr r1, [pc, #384]  ; (0x10000980 <rp2040_project_template::__cortex_m_rt_main+660>)
   0x10000800 <+276>:   str r1, [r0, #0]
   0x10000802 <+278>:   subs    r0, r6, #4
   0x10000804 <+280>:   ldr r1, [pc, #380]  ; (0x10000984 <rp2040_project_template::__cortex_m_rt_main+664>)
   0x10000806 <+282>:   str r1, [r0, #0]
   0x10000808 <+284>:   movs    r0, #100    ; 0x64
   0x1000080a <+286>:   str r0, [sp, #104]  ; 0x68
   0x1000080c <+288>:   subs    r6, #20
0x1000080e <+290>:  ldr r4, [pc, #384]  ; (0x10000990 <rp2040_project_template::__cortex_m_rt_main+676>)
   0x10000810 <+292>:   bl  0x10002248 <defmt_rtt::_defmt_acquire>
   0x10000814 <+296>:   ldr r0, [pc, #372]  ; (0x1000098c <rp2040_project_template::__cortex_m_rt_main+672>)
   0x10000816 <+298>:   bl  0x100023c0 <defmt::export::make_istr>
   0x1000081a <+302>:   add r1, sp, #108    ; 0x6c
   0x1000081c <+304>:   strh    r0, [r1, #0]
   0x1000081e <+306>:   mov r0, r1
   0x10000820 <+308>:   bl  0x10002428 <defmt::export::header>
   0x10000824 <+312>:   mov r0, r4
   0x10000826 <+314>:   bl  0x100023c0 <defmt::export::make_istr>
   0x1000082a <+318>:   add r1, sp, #108    ; 0x6c
   0x1000082c <+320>:   strh    r0, [r1, #0]
   0x1000082e <+322>:   mov r0, r1
   0x10000830 <+324>:   bl  0x100023c2 <defmt::export::istr>
   0x10000834 <+328>:   add r0, sp, #104    ; 0x68
   0x10000836 <+330>:   bl  0x1000245c <defmt::export::integers::i32>
   0x1000083a <+334>:   bl  0x100022e0 <defmt_rtt::_defmt_release>
   0x1000083e <+338>:   ldr r0, [sp, #104]  ; 0x68
=> 0x10000840 <+340>:   ldr r1, [sp, #4]
   0x10000842 <+342>:   mov r2, r6
   0x10000844 <+344>:   mov r6, r4
   0x10000846 <+346>:   push    {r6}
   0x10000848 <+348>:   ldr r3, [r2, #8]
   0x1000084a <+350>:   blx r3
   0x1000084c <+352>:   pop {r6}
   0x1000084e <+354>:   cmp r1, #0
   0x10000850 <+356>:   beq.n   0x100008e4 <rp2040_project_template::__cortex_m_rt_main+504>
   0x10000852 <+358>:   mov r4, r1
   0x10000854 <+360>:   str r0, [sp, #108]  ; 0x6c
   0x10000856 <+362>:   bl  0x10002248 <defmt_rtt::_defmt_acquire>
   0x1000085a <+366>:   ldr r0, [pc, #312]  ; (0x10000994 <rp2040_project_template::__cortex_m_rt_main+680>)
   0x1000085c <+368>:   bl  0x100023c0 <defmt::export::make_istr>
   0x10000860 <+372>:   add r1, sp, #112    ; 0x70
   0x10000862 <+374>:   strh    r0, [r1, #0]
   0x10000864 <+376>:   mov r0, r1
   0x10000866 <+378>:   bl  0x10002428 <defmt::export::header>
   0x1000086a <+382>:   mov r0, r6
   0x1000086c <+384>:   bl  0x100023c0 <defmt::export::make_istr>
   0x10000870 <+388>:   add r1, sp, #112    ; 0x70
   0x10000872 <+390>:   strh    r0, [r1, #0]
   0x10000874 <+392>:   mov r0, r1
  0x10000876 <+394>:    bl  0x100023c2 <defmt::export::istr>
   0x1000087a <+398>:   add r0, sp, #108    ; 0x6c
   0x1000087c <+400>:   bl  0x1000245c <defmt::export::integers::i32>
   0x10000880 <+404>:   bl  0x100022e0 <defmt_rtt::_defmt_release>
   0x10000884 <+408>:   ldr r1, [sp, #104]  ; 0x68
   0x10000886 <+410>:   adds    r0, r1, #1
   0x10000888 <+412>:   mov r5, r6
   0x1000088a <+414>:   cmp r0, r1
   0x1000088c <+416>:   bvs.n   0x10000922 <rp2040_project_template::__cortex_m_rt_main+566>
   0x1000088e <+418>:   str r0, [sp, #104]  ; 0x68
   0x10000890 <+420>:   bl  0x10002248 <defmt_rtt::_defmt_acquire>
   0x10000894 <+424>:   ldr r0, [pc, #256]  ; (0x10000998 <rp2040_project_template::__cortex_m_rt_main+684>)
   0x10000896 <+426>:   bl  0x100023c0 <defmt::export::make_istr>
   0x1000089a <+430>:   add r1, sp, #108    ; 0x6c
   0x1000089c <+432>:   strh    r0, [r1, #0]
   0x1000089e <+434>:   mov r0, r1
   0x100008a0 <+436>:   bl  0x10002428 <defmt::export::header>
   0x100008a4 <+440>:   bl  0x100022e0 <defmt_rtt::_defmt_release>
   0x100008a8 <+444>:   str r4, [sp, #8]
   0x100008aa <+446>:   ldr r0, [pc, #208]  ; (0x1000097c <rp2040_project_template::__cortex_m_rt_main+656>)
   0x100008ac <+448>:   ldr r6, [sp, #0]
   0x100008ae <+450>:   str r6, [r0, #0]
   0x100008b0 <+452>:   mov r4, r5
   0x100008b2 <+454>:   add r5, sp, #100    ; 0x64
   0x100008b4 <+456>:   mov r0, r5
   0x100008b6 <+458>:   ldr r1, [pc, #208]  ; (0x10000988 <rp2040_project_template::__cortex_m_rt_main+668>)
   0x100008b8 <+460>:   bl  0x10002b68 <cortex_m::delay::Delay::delay_us>
   0x100008bc <+464>:   bl  0x10002248 <defmt_rtt::_defmt_acquire>
   0x100008c0 <+468>:   ldr r0, [pc, #216]  ; (0x1000099c <rp2040_project_template::__cortex_m_rt_main+688>)
   0x100008c2 <+470>:   bl  0x100023c0 <defmt::export::make_istr>
   0x100008c6 <+474>:   add r1, sp, #108    ; 0x6c
   0x100008c8 <+476>:   strh    r0, [r1, #0]
   0x100008ca <+478>:   mov r0, r1
   0x100008cc <+480>:   bl  0x10002428 <defmt::export::header>
   0x100008d0 <+484>:   bl  0x100022e0 <defmt_rtt::_defmt_release>
   0x100008d4 <+488>:   ldr r0, [pc, #164]  ; (0x1000097c <rp2040_project_template::__cortex_m_rt_main+656>)
   0x100008d6 <+490>:   str r6, [r0, #4]
   0x100008d8 <+492>:   ldr r6, [sp, #8]
   0x100008da <+494>:   mov r0, r5
   0x100008dc <+496>:   ldr r1, [pc, #168]  ; (0x10000988 <rp2040_project_template::__cortex_m_rt_main+668>)
   0x100008de <+498>:   bl  0x10002b68 <cortex_m::delay::Delay::delay_us>
   0x100008e2 <+502>:   b.n 0x10000810 <rp2040_project_template::__cortex_m_rt_main+292>
   0x100008e4 <+504>:   b.n 0x100008e4 <rp2040_project_template::__cortex_m_rt_main+504>
 0x100008e6 <+506>: strb    r6, [r0, #0]
   0x100008e8 <+508>:   cmp r4, #0
   0x100008ea <+510>:   beq.n   0x100008ee <rp2040_project_template::__cortex_m_rt_main+514>
   0x100008ec <+512>:   b.n 0x10000736 <rp2040_project_template::__cortex_m_rt_main+74>
   0x100008ee <+514>:   bl  0x10002c3c <lib::__cpsie>
   0x100008f2 <+518>:   cmp r5, #0
   0x100008f4 <+520>:   bne.n   0x100008f8 <rp2040_project_template::__cortex_m_rt_main+524>
   0x100008f6 <+522>:   b.n 0x1000073c <rp2040_project_template::__cortex_m_rt_main+80>
   0x100008f8 <+524>:   ldr r0, [pc, #92]   ; (0x10000958 <rp2040_project_template::__cortex_m_rt_main+620>)
   0x100008fa <+526>:   movs    r1, #43 ; 0x2b
   0x100008fc <+528>:   ldr r2, [pc, #92]   ; (0x1000095c <rp2040_project_template::__cortex_m_rt_main+624>)
   0x100008fe <+530>:   bl  0x10002e10 <core::panicking::panic>
   0x10000902 <+534>:   udf #254    ; 0xfe
   0x10000904 <+536>:   strb    r6, [r0, #0]
   0x10000906 <+538>:   cmp r4, #0
   0x10000908 <+540>:   beq.n   0x1000090c <rp2040_project_template::__cortex_m_rt_main+544>
   0x1000090a <+542>:   b.n 0x1000075c <rp2040_project_template::__cortex_m_rt_main+112>
   0x1000090c <+544>:   bl  0x10002c3c <lib::__cpsie>
   0x10000910 <+548>:   cmp r5, #0
   0x10000912 <+550>:   bne.n   0x10000916 <rp2040_project_template::__cortex_m_rt_main+554>
   0x10000914 <+552>:   b.n 0x10000762 <rp2040_project_template::__cortex_m_rt_main+118>
   0x10000916 <+554>:   ldr r0, [pc, #64]   ; (0x10000958 <rp2040_project_template::__cortex_m_rt_main+620>)
   0x10000918 <+556>:   movs    r1, #43 ; 0x2b
   0x1000091a <+558>:   ldr r2, [pc, #72]   ; (0x10000964 <rp2040_project_template::__cortex_m_rt_main+632>)
   0x1000091c <+560>:   bl  0x10002e10 <core::panicking::panic>
   0x10000920 <+564>:   udf #254    ; 0xfe
   0x10000922 <+566>:   ldr r0, [pc, #124]  ; (0x100009a0 <rp2040_project_template::__cortex_m_rt_main+692>)
   0x10000924 <+568>:   movs    r1, #28
   0x10000926 <+570>:   ldr r2, [pc, #124]  ; (0x100009a4 <rp2040_project_template::__cortex_m_rt_main+696>)
   0x10000928 <+572>:   bl  0x10002e10 <core::panicking::panic>
   0x1000092c <+576>:   udf #254    ; 0xfe
   0x1000092e <+578>:   ldr r0, [pc, #112]  ; (0x100009a0 <rp2040_project_template::__cortex_m_rt_main+692>)
   0x10000930 <+580>:   movs    r1, #28
   0x10000932 <+582>:   ldr r2, [pc, #116]  ; (0x100009a8 <rp2040_project_template::__cortex_m_rt_main+700>)
   0x10000934 <+584>:   bl  0x10002e10 <core::panicking::panic>
   0x10000938 <+588>:   udf #254    ; 0xfe
   0x1000093a <+590>:   ldr r0, [pc, #28]   ; (0x10000958 <rp2040_project_template::__cortex_m_rt_main+620>)
   0x1000093c <+592>:   movs    r1, #43 ; 0x2b
   0x1000093e <+594>:   ldr r2, [pc, #44]   ; (0x1000096c <rp2040_project_template::__cortex_m_rt_main+640>)
   0x10000940 <+596>:   bl  0x10002e10 <core::panicking::panic>
0x10000944 <+600>:  udf #254    ; 0xfe
   0x10000946 <+602>:   nop         ; (mov r8, r8)
   0x10000948 <+604>:           ; <UNDEFINED> instruction: 0xf7d82003
   0x1000094c <+608>:           ; <UNDEFINED> instruction: 0xf7f02003
   0x10000950 <+612>:   movs    r3, r0
   0x10000952 <+614>:   movs    r0, r0
   0x10000954 <+616>:           ; <UNDEFINED> instruction: 0xfbf12003
   0x10000958 <+620>:   cmn r4, r5
   0x1000095a <+622>:   asrs    r0, r0, #32
   0x1000095c <+624>:   muls    r0, r0
   0x1000095e <+626>:   asrs    r0, r0, #32
   0x10000960 <+628>:           ; <UNDEFINED> instruction: 0xfbf82003
   0x10000964 <+632>:   muls    r0, r2
   0x10000966 <+634>:   asrs    r0, r0, #32
   0x10000968 <+636>:   subs    r0, r0, r4
   0x1000096a <+638>:   lsls    r7, r6, #2
   0x1000096c <+640>:   muls    r0, r4
   0x1000096e <+642>:   asrs    r0, r0, #32
   0x10000970 <+644>:   b.n 0x10000994 <rp2040_project_template::__cortex_m_rt_main+680>
   0x10000972 <+646>:   b.n 0x10000976 <rp2040_project_template::__cortex_m_rt_main+650>
   0x10000974 <+648>:   stmia   r0!, {r3, r5, r6}
   0x10000976 <+650>:   ands    r1, r0
   0x10000978 <+652>:   lsrs    r4, r1
   0x1000097a <+654>:   ands    r1, r0
   0x1000097c <+656>:   movs    r4, r2
   0x1000097e <+658>:   beq.n   0x10000982 <rp2040_project_template::__cortex_m_rt_main+662>
   0x10000980 <+660>:   lsrs    r1, r3, #7
   0x10000982 <+662>:   asrs    r0, r0, #32
   0x10000984 <+664>:   lsls    r5, r5, #20
   0x10000986 <+666>:   asrs    r0, r0, #32
   0x10000988 <+668>:   add r1, pc, #128    ; (adr r1, 0x10000a0c <_rphal_unsigned_divmod+24>)
   0x1000098a <+670>:   movs    r7, r0
   0x1000098c <+672>:   movs    r0, r1
   0x1000098e <+674>:   movs    r0, r0
   0x10000990 <+676>:   movs    r2, r0
   0x10000992 <+678>:   movs    r0, r0
   0x10000994 <+680>:   movs    r1, r1
   0x10000996 <+682>:   movs    r0, r0
   0x10000998 <+684>:   movs    r2, r1
   0x1000099a <+686>:   movs    r0, r0
   0x1000099c <+688>:   movs    r3, r1
   0x1000099c <+688>:   movs    r3, r1
   0x1000099e <+690>:   movs    r0, r0
   0x100009a0 <+692>:   cmn r0, r2
   0x100009a2 <+694>:   asrs    r0, r0, #32
   0x100009a4 <+696>:   muls    r0, r6
   0x100009a6 <+698>:   asrs    r0, r0, #32
   0x100009a8 <+700>:   orrs    r4, r4
   0x100009aa <+702>:   asrs    r0, r0, #32
End of assembler dump.
Amanieu commented 2 years ago

That's stopped at a breakpoint. I need the disassembly at the point of the crash.

kpishere commented 2 years ago

Yeah, trying to find that. It get stuck in hard fault loop, would R7 have the return address of where it came from at that point? (sorry if dumb question) That point 0x2003F794 .. I'm looking there

kpishere commented 2 years ago

Ok, had to switch to a view I'm more familiar with ..

Screen Shot 2022-07-06 at 3 03 36 PM

And another view showing other registers .. Screen Shot 2022-07-06 at 3 03 57 PM

The first step from what is shown throws an exception. It is a disconnect in the opcode not being thumb or the PC register not having the last bit set correctly?

Amanieu commented 2 years ago

If you single-step one instruction at a time (si in gdb), can you pin-point the exact instruction that causes the fault?

kpishere commented 2 years ago

Oh! si, forgot about that. Thanks ok. this makes more sense ...

Screen Shot 2022-07-06 at 3 34 59 PM

after blx to bad address ..

Screen Shot 2022-07-06 at 3 35 29 PM

Amanieu commented 2 years ago

What is the value of r3?

kpishere commented 2 years ago

@Amanieu

Screen Shot 2022-07-06 at 4 08 49 PM

Yeah, ok, so that r6, copied to r2 looks really close the the stack size of 4096. That address isn't right? needs to be stack size plus the lowest memory location for the stack?

Here are some further memory pictures .. Screen Shot 2022-07-06 at 6 08 07 PM

.

Amanieu commented 2 years ago

Well I can't figure it out. Do you think it would be possible to run this in QEMU so I could try it out myself?

kpishere commented 2 years ago

Well, I've used QEMU before for emulating Pi Zero. Let me see if I can re-produce the same issue in QEMU ... got to set it up for PICO and Rust environment etc. You're making me learn all the tools! :) Thanks!

kpishere commented 2 years ago

@Amanieu Sadly, not seeing steps to do, think it is a low priority for qemu, thought I'd seen it for thumb7. Anyhow, stack is clearly corrupted and thought I'd look at it. Looking at first steps where stack is used. I suspect the stack is created in low memory and isn't writable. This may be my allocator that is the problem?

The stack address should not be 0x1000. That appears to be ROM. It should start with 0x20000000 as this is the beginning of SRAM.

Screen Shot 2022-07-06 at 9 35 27 PM

ROM 0x00000000 XIP 0x10000000 SRAM 0x20000000 APB Peripherals 0x40000000


I have an allocator test here, and the vector is allocated as you'd expect and looks good.

    let mut xs = Vec::new();
    xs.push(1);

Next I allocate the stack with let stack: pico::DefaultStack = pico::DefaultStack::new(MIN_STACK_SIZE).unwrap();

... yeah, I'm getting zero as an address here ... this is the wrong function . Sorry, I'm new to rust.

Screen Shot 2022-07-06 at 9 46 29 PM

Amanieu commented 2 years ago

If the allocator returns a null pointer then it means that the allocation failed. That's what's causing the problem.

kpishere commented 2 years ago

@Amanieu Happiness is! My heap only had 1K available at initialization and the stack is minimum 4K. Increased heap to 16K available and all is good.!

Thank you for your support and this crate!

~/defmt.sh 
INFO  Program start
└─ rp2040_project_template::__cortex_m_rt_main @ src/main.rs:51
INFO  [main] resuming coroutine with argument 100
└─ rp2040_project_template::__cortex_m_rt_main::{closure#0} @ src/main.rs:98
INFO  [coroutine] coroutine started with input 100
└─ rp2040_project_template::__cortex_m_rt_main::{closure#0} @ src/main.rs:87
INFO  [coroutine] yielding 0
└─ rp2040_project_template::__cortex_m_rt_main::{closure#0} @ src/main.rs:89
INFO  [main] got 0 from coroutine
└─ rp2040_project_template::__cortex_m_rt_main::{closure#0} @ src/main.rs:100
INFO  on!
└─ rp2040_project_template::__cortex_m_rt_main::{closure#0} @ src/main.rs:105
INFO  off!
└─ rp2040_project_template::__cortex_m_rt_main::{closure#0} @ src/main.rs:108
INFO  [main] resuming coroutine with argument 101
└─ rp2040_project_template::__cortex_m_rt_main::{closure#0} @ src/main.rs:98
INFO  [coroutine] got 101 from parent
└─ rp2040_project_template::__cortex_m_rt_main::{closure#0} @ src/main.rs:91
INFO  [coroutine] yielding 1
└─ rp2040_project_template::__cortex_m_rt_main::{closure#0} @ src/main.rs:89
INFO  [main] got 1 from coroutine
└─ rp2040_project_template::__cortex_m_rt_main::{closure#0} @ src/main.rs:100
INFO  on!
└─ rp2040_project_template::__cortex_m_rt_main::{closure#0} @ src/main.rs:105
INFO  off!
└─ rp2040_project_template::__cortex_m_rt_main::{closure#0} @ src/main.rs:108
INFO  [main] resuming coroutine with argument 102
└─ rp2040_project_template::__cortex_m_rt_main::{closure#0} @ src/main.rs:98
INFO  [coroutine] got 102 from parent
└─ rp2040_project_template::__cortex_m_rt_main::{closure#0} @ src/main.rs:91
INFO  [coroutine] yielding 2
└─ rp2040_project_template::__cortex_m_rt_main::{closure#0} @ src/main.rs:89
INFO  [main] got 2 from coroutine
└─ rp2040_project_template::__cortex_m_rt_main::{closure#0} @ src/main.rs:100
INFO  on!
└─ rp2040_project_template::__cortex_m_rt_main::{closure#0} @ src/main.rs:105
INFO  off!
└─ rp2040_project_template::__cortex_m_rt_main::{closure#0} @ src/main.rs:108
INFO  [main] resuming coroutine with argument 103
└─ rp2040_project_template::__cortex_m_rt_main::{closure#0} @ src/main.rs:98
INFO  [coroutine] got 103 from parent
└─ rp2040_project_template::__cortex_m_rt_main::{closure#0} @ src/main.rs:91
INFO  [coroutine] yielding 3
└─ rp2040_project_template::__cortex_m_rt_main::{closure#0} @ src/main.rs:89
INFO  [main] got 3 from coroutine
└─ rp2040_project_template::__cortex_m_rt_main::{closure#0} @ src/main.rs:100
INFO  on!
└─ rp2040_project_template::__cortex_m_rt_main::{closure#0} @ src/main.rs:105
INFO  off!
└─ rp2040_project_template::__cortex_m_rt_main::{closure#0} @ src/main.rs:108
INFO  [main] resuming coroutine with argument 104
└─ rp2040_project_template::__cortex_m_rt_main::{closure#0} @ src/main.rs:98
INFO  [coroutine] got 104 from parent
└─ rp2040_project_template::__cortex_m_rt_main::{closure#0} @ src/main.rs:91
INFO  [coroutine] yielding 4
└─ rp2040_project_template::__cortex_m_rt_main::{closure#0} @ src/main.rs:89
INFO  [main] got 4 from coroutine
└─ rp2040_project_template::__cortex_m_rt_main::{closure#0} @ src/main.rs:100
INFO  on!
└─ rp2040_project_template::__cortex_m_rt_main::{closure#0} @ src/main.rs:105
INFO  off!
└─ rp2040_project_template::__cortex_m_rt_main::{closure#0} @ src/main.rs:108
INFO  [main] resuming coroutine with argument 105
└─ rp2040_project_template::__cortex_m_rt_main::{closure#0} @ src/main.rs:98
INFO  [coroutine] got 105 from parent
└─ rp2040_project_template::__cortex_m_rt_main::{closure#0} @ src/main.rs:91
INFO  [coroutine] exiting coroutine
└─ rp2040_project_template::__cortex_m_rt_main::{closure#0} @ src/main.rs:93
Amanieu commented 2 years ago

You can probably get away with a smaller stack. Also since you don't have a guard page, you need to make sure you don't overflow the stack.

kpishere commented 2 years ago

@Amanieu Surely, I'm just getting started though. This is one of the many sane references I'll need as I progress in this project.

For stack protection, this IS working with

rustflags = [
  "-C", "linker=flip-link",

Which means the stack is at the bottom of memory and stack overflow should cause a hardware fault. Although, we saw above, that fault didn't happen when the stack was allocated at 0x0 so, maybe I need to add a handler for that hardware fault. I'll check that.