ChibiOS OpenOCD support

This forum is dedicated to feedback, discussions about ongoing or future developments, ideas and suggestions regarding the ChibiOS projects are welcome. This forum is NOT for support.
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: ChibiOS OpenOCD support

Post by Giovanni »

Hi, the register layout is described by the extctx and intctx structures.

Extctx are the registers saved while serving interrupts (hardware stacking), intctx are the registers saved during the thread-to-thread context switching.

There is no GCC magic, you see exactly the registers that are saved, not all registers are saved because while switching threads you only need to save the registers that functions are supposed to preserve.

Look at this picture: http://chibios.sourceforge.net/docs/ker ... html#warea

Giovanni
mabl
Posts: 417
Joined: Tue Dec 21, 2010 10:19 am
Been thanked: 1 time

Re: ChibiOS OpenOCD support

Post by mabl »

Hi Giovanni,

thx for the hint. So I was mostly right in my last post about the makeup. So for the M3 (without FPU), it is:

Code: Select all

struct intctx {
  regarm_t      r4;
  regarm_t      r5;
  regarm_t      r6;
  regarm_t      r7;
  regarm_t      r8;
  regarm_t      r9;
  regarm_t      r10;
  regarm_t      r11;
  regarm_t      lr;
};


Unfortunately, my code is still not working 100%: gdb shows me the current Point of execution, but the backtraces do not work:

Code: Select all

(gdb) info  threads 
  8 Thread 536907216 (shell :  : WTQUEUE)  chSchGoSleepS (newstate=32 ' ') at ../../os/kernel/src/chschd.c:122
  7 Thread 536904464 (http :  : WTSEM)  chSchGoSleepS (newstate=32 ' ') at ../../os/kernel/src/chschd.c:122
  6 Thread 536905776 (No Name :  : WTSEM)  chSchGoSleepS (newstate=32 ' ') at ../../os/kernel/src/chschd.c:122
  5 Thread 536901760 (lwipthread :  : WTOREVT)  chSchGoSleepS (newstate=32 ' ') at ../../os/kernel/src/chschd.c:122
  4 Thread 536899024 (blinker :  : SLEEPING)  chSchGoSleepS (newstate=32 ' ') at ../../os/kernel/src/chschd.c:122
  3 Thread 536901456 (usb_lld_pump :  : SUSPENDED)  chSchGoSleepS (newstate=32 ' ') at ../../os/kernel/src/chschd.c:122
  2 Thread 536899336 (idle :  : CURRENT)  _idle_thread (p=0x0) at ../../os/kernel/src/chsys.c:61
* 1 Thread 536873296 (main :  : WTOREVT)  chSchGoSleepS (newstate=32 ' ') at ../../os/kernel/src/chschd.c:122


Code: Select all

(gdb) thread apply 4 bt

Thread 4 (Thread 536899024):
#0  chSchGoSleepS (newstate=32 ' ') at ../../os/kernel/src/chschd.c:122
#1  0x06000000 in ?? ()
Backtrace stopped: frame did not save the PC


I guess my problem lies in how I restore the lr and pc register. What I am currently doing is using the saved lr register as Program counter and as lr I use the register above that - which is probably wrong. The openocd register configuration looks like this:

Code: Select all

static const struct stack_register_offset rtos_chibios_Cortex_M3_stack_offsets[] = {
//   {offset from head, width}
//   (-1 not available, -2 Stackpointer)
   { -1,   32 },      /* r0   */
   { -1,   32 },      /* r1   */
   { -1,   32 },      /* r2   */
   { -1,   32 },      /* r3   */
   { 0x00, 32 },      /* r4   */
   { 0x04, 32 },      /* r5   */
   { 0x08, 32 },      /* r6   */
   { 0x0c, 32 },      /* r7   */
   { 0x10, 32 },      /* r8   */
   { 0x14, 32 },      /* r9   */
   { 0x18, 32 },      /* r10  */
   { 0x1c, 32 },      /* r11  */
   { -1,   32 },      /* r12  */
   { -2,   32 },      /* sp   */
   { 0x24,   32 },      /* lr   */
   { 0x20, 32 },      /* pc   */
   { -1,   96 },      /* FPA1 */
   { -1,   96 },      /* FPA2 */
   { -1,   96 },      /* FPA3 */
   { -1,   96 },      /* FPA4 */
   { -1,   96 },      /* FPA5 */
   { -1,   96 },      /* FPA6 */
   { -1,   96 },      /* FPA7 */
   { -1,   96 },      /* FPA8 */
   { -1,   32 },      /* FPS  */
   { -1,   32 },      /* xPSR */
};


Any Ideas what I'm doing wrong?
mabl
Posts: 417
Joined: Tue Dec 21, 2010 10:19 am
Been thanked: 1 time

Re: ChibiOS OpenOCD support

Post by mabl »

Yeaha Thinks look brighter now. I think it was an alignment issue....

Code: Select all

(gdb) thread apply all bt
[New Thread 536899336]
[New Thread 536901456]
[New Thread 536899024]
[New Thread 536901760]
[New Thread 536905776]
[New Thread 536904464]
[New Thread 536907216]

Thread 8 (Thread 536907216):
#0  chSchGoSleepS (newstate=13 '\r') at ../../os/kernel/src/chschd.c:122
#1  0x08000bc4 in chSchGoSleepTimeoutS (newstate=13 '\r', time=4294967295) at ../../os/kernel/src/chschd.c:195
#2  0x08002b10 in qwait (qp=0x20006b54, time=4294967295) at ../../os/kernel/src/chqueues.c:69
#3  0x08002d16 in chIQReadTimeout (iqp=0x20006b54, bp=0x200095eb "\b\034\226", n=1, time=4294967295) at ../../os/kernel/src/chqueues.c:232
#4  0x0800a1ee in read (ip=0x20006b48, bp=0x200095eb "\b\034\226", n=1) at ../../os/hal/src/serial_usb.c:72
#5  0x080262d6 in shellGetLine (chp=0x20006b48, line=0x2000961c "x\360~x\360~P", size=64) at ../../os/various/shell.c:261
#6  0x0802607a in shell_thread (p=0x8028a08) at ../../os/various/shell.c:155
#7  0x0800048c in _port_thread_start () at ../../os/ports/GCC/ARMCMx/chcore_v7m.c:255
Backtrace stopped: frame did not save the PC

Thread 7 (Thread 536904464):
#0  chSchGoSleepS (newstate=3 '\003') at ../../os/kernel/src/chschd.c:122
#1  0x08000bc4 in chSchGoSleepTimeoutS (newstate=3 '\003', time=4294967295) at ../../os/kernel/src/chschd.c:195
#2  0x08001684 in chSemWaitTimeoutS (sp=0x20008d10, time=4294967295) at ../../os/kernel/src/chsem.c:259
#3  0x080029b2 in chMBFetchS (mbp=0x20008d00, msgp=0x20008790, time=4294967295) at ../../os/kernel/src/chmboxes.c:335
#4  0x0801004c in sys_arch_mbox_fetch (mbox=0x20003b7c, msg=0x20008790, timeout=0) at ../../os/various/lwip_bindings/arch/sys_arch.c:169
#5  0x0801c3d8 in netconn_accept (conn=0x20003b68, new_conn=0x200087ac) at ../../ext/lwip/src/api/api_lib.c:304
#6  0x080264da in http_server (p=0x0) at web/web.c:115
#7  0x0800048c in _port_thread_start () at ../../os/ports/GCC/ARMCMx/chcore_v7m.c:255
Backtrace stopped: frame did not save the PC

Thread 6 (Thread 536905776):
#0  chSchGoSleepS (newstate=3 '\003') at ../../os/kernel/src/chschd.c:122
#1  0x08000baa in chSchGoSleepTimeoutS (newstate=3 '\003', time=1000) at ../../os/kernel/src/chschd.c:190
#2  0x08001684 in chSemWaitTimeoutS (sp=0x20008808, time=1000) at ../../os/kernel/src/chsem.c:259
#3  0x080029b2 in chMBFetchS (mbp=0x200087f8, msgp=0x20008cd4, time=1000) at ../../os/kernel/src/chmboxes.c:335
#4  0x0801004c in sys_arch_mbox_fetch (mbox=0x200064d4, msg=0x20008cd4, timeout=1000) at ../../os/various/lwip_bindings/arch/sys_arch.c:169
#5  0x0801bf80 in sys_timeouts_mbox_fetch (mbox=0x200064d4, msg=0x20008cd4) at ../../ext/lwip/src/core/timers.c:430
#6  0x080205ec in tcpip_thread (arg=0x0) at ../../ext/lwip/src/api/tcpip.c:88
#7  0x0800048c in _port_thread_start () at ../../os/ports/GCC/ARMCMx/chcore_v7m.c:255
Backtrace stopped: frame did not save the PC

Thread 5 (Thread 536901760):
#0  chSchGoSleepS (newstate=8 '\b') at ../../os/kernel/src/chschd.c:122
#1  0x080022e8 in chEvtWaitAny (mask=4294967295) at ../../os/kernel/src/chevents.c:380
#2  0x0800fbd4 in lwip_thread (p=0x0) at ../../os/various/lwip_bindings/lwipthread.c:275
#3  0x0800048c in _port_thread_start () at ../../os/ports/GCC/ARMCMx/chcore_v7m.c:255
Backtrace stopped: frame did not save the PC

Thread 4 (Thread 536899024):
#0  chSchGoSleepS (newstate=6 '\006') at ../../os/kernel/src/chschd.c:122
#1  0x08000baa in chSchGoSleepTimeoutS (newstate=6 '\006', time=125) at ../../os/kernel/src/chschd.c:190
#2  0x08001018 in chThdSleep (time=125) at ../../os/kernel/src/chthreads.c:290
#3  0x08026b30 in Thread1 (arg=0x0) at main.c:595
#4  0x0800048c in _port_thread_start () at ../../os/ports/GCC/ARMCMx/chcore_v7m.c:255
Backtrace stopped: frame did not save the PC

Thread 3 (Thread 536901456):
#0  chSchGoSleepS (newstate=2 '\002') at ../../os/kernel/src/chschd.c:122
#1  0x0800ecca in usb_lld_pump (p=0x200076f8) at ../../os/hal/platforms/STM32/OTGv1/usb_lld.c:694
#2  0x0800048c in _port_thread_start () at ../../os/ports/GCC/ARMCMx/chcore_v7m.c:255
Backtrace stopped: frame did not save the PC

Thread 2 (Thread 536899336):
#0  _idle_thread (p=0x0) at ../../os/kernel/src/chsys.c:61
#1  0x0800048c in _port_thread_start () at ../../os/ports/GCC/ARMCMx/chcore_v7m.c:255
Backtrace stopped: frame did not save the PC

Thread 1 (Thread 536873296):
#0  chSchGoSleepS (newstate=8 '\b') at ../../os/kernel/src/chschd.c:122
#1  0x08000baa in chSchGoSleepTimeoutS (newstate=8 '\b', time=500) at ../../os/kernel/src/chschd.c:190
#2  0x080023d0 in chEvtWaitOneTimeout (mask=4294967295, time=500) at ../../os/kernel/src/chevents.c:451
#3  0x08026d12 in main () at main.c:685
mabl
Posts: 417
Joined: Tue Dec 21, 2010 10:19 am
Been thanked: 1 time

Re: ChibiOS OpenOCD support

Post by mabl »

openocd_chibios.gif
openocd_chibios.gif (138.93 KiB) Viewed 8250 times


:lol: :lol: :lol: :D :D :D :mrgreen: :mrgreen: :mrgreen: :lol: :P :D
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: ChibiOS OpenOCD support

Post by Giovanni »

:) :mrgreen:
mabl
Posts: 417
Joined: Tue Dec 21, 2010 10:19 am
Been thanked: 1 time

Re: ChibiOS OpenOCD support

Post by mabl »

Ok, it seems to be working mostly. I've pushed the Code to Github.

Still needs to be fixed/investigated:
  • ch_root should include the offset for readylist.current
  • ch_root needs some kind of a flag to tell openocd if FPU is enabled or not
  • Need to think about how to handle interrupts. Probably show them as own thread, right? Any internal Flag I could check which tells me I'm in an interrupt?
mabl
Posts: 417
Joined: Tue Dec 21, 2010 10:19 am
Been thanked: 1 time

Re: ChibiOS OpenOCD support

Post by mabl »

I've started to document everything in the wiki:
http://www.chibios.org/dokuwiki/doku.ph ... cd_chibios
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: ChibiOS OpenOCD support

Post by Giovanni »

Hi,

The pointer of the current thread pointer offset is the same of cf_off_name by design so it is not really required.

The FPU flag would be possible and the exception state can be read into a status register, my doubt is that the last two points are architecture dependent (Cortex-M only), is the RTOS support supposed to be architecture independent or not?

Giovanni
Abhishek
Posts: 266
Joined: Wed May 23, 2012 3:15 pm

Re: ChibiOS OpenOCD support

Post by Abhishek »

Great work!

Looking forward to see it up very soon. However there is OpenOCD v0.6.1 - Right now they are planning a maintenance bugfix release. If you complete it soon, I hope you could send on to the OpenOCD mailing list. At least an announcement should be sufficient.

Cheers
mabl
Posts: 417
Joined: Tue Dec 21, 2010 10:19 am
Been thanked: 1 time

Re: ChibiOS OpenOCD support

Post by mabl »

Hi Giovanni,

Giovanni wrote:The pointer of the current thread pointer offset is the same of cf_off_name by design so it is not really required.

Ok, but maybe this should be documented in the ChibiOS source with a comment?

Giovanni wrote:Hi,
The FPU flag would be possible and the exception state can be read into a status register, my doubt is that the last two points are architecture dependent (Cortex-M only), is the RTOS support supposed to be architecture independent or not?

The rtos support is already target specific, i.e. the unrolling of the stack is. But you are right, maybe thread support it is too much for initial code. Let's first get the basics into upstream. Still, I'd very much like to have a FPU flag in ch_root, though - it can be added to openocd very easily.

BTW: Are there any targets which are supported by ChibiOS and openocd which are not cortex-m and you want to have them supported as well?
Post Reply