Page 1 of 1

Disabling optimizations (-O0) breaks the CortexM/RVC kernel

Posted: Sun Sep 30, 2012 9:27 pm
by iggarpe
I already filed a bug, but I'm interested in knowing if anyone else can reproduce this error.

https://sourceforge.net/tracker/?func=d ... tid=995489

I'm using ChibiOS 2.4.x branch and Keil MDK-ARM Professional 4.50.0.0. My board is a STM32F4 discovery.

From the bug tracker you can download an absolutely minimal Keil project that will trigger the error when compiling with -O0 but will work fine when compiling with -O3:

halInit();
chSysInit();
chThdSleep(3);
chThdSleep(TIME_INFINITE);

If you just launch the program and the stop it, you'll either see it was looping in the unhandled exception function (with -O0) or in the idle thread (with -O3).

I've traced the execution in both cases but still can't figure out what's wrong. I'm new to ChibiOS and thus don't know that well its internals.

Sure, I can go with -O3 enabled all the time, but if there's something fundamentally wrong it will bite me sooner or later, so I'd rather don't go past this without a fix.

Thanks.

Re: Disabling optimizations (-O0) breaks the CortexM/RVC ker

Posted: Sun Sep 30, 2012 9:40 pm
by Giovanni
Hi,

Probably this is just a stack overflow (I suspect the stack of the idle thread), disabling optimizations usually makes stack frames much larger.

You may try enabling the debug options in chconf.h and see if some assertion is triggered. I will give it a try myself hopefully tomorrow.

Giovanni

Re: Disabling optimizations (-O0) breaks the CortexM/RVC ker

Posted: Mon Oct 01, 2012 3:45 am
by iggarpe
Oops. Sorry, I forgot to mention that I've already tried to enable every single ChibiOS debug feature, and they all remain silent.

BTW, maybe something for the FAQ: make sure you clean and rebuild the entire project when you change something in chconf.h (maybe also in mcuconf.h?), because at least the Keil IDE fails to notice that some assembler files depend on those headers, and won't recompile them. I managed to get myself into a horrible mess for hours because of this (you'll notice a deleted bug in the tracker). I had objects compiled from C files with STATE_CHECK enabled, while the assembler files had been compiled with STATE_CHECK disabled. As a result, dbg_lock_count was not being clear in the assembly code, triggering a panic next time it was checked and expected to be zero.

Just for the record, I switched to IAR EWARM and runs fine with -O0.

Re: Disabling optimizations (-O0) breaks the CortexM/RVC ker

Posted: Mon Oct 15, 2012 4:33 am
by iggarpe
Props to Giovanni who just closed this bug:

http://sourceforge.net/tracker/?func=de ... tid=995489

Now, this is quite weird one from which I feel I could learn something. Unfortunately the comment in the bu tracker doesn't go into the details.

Giovanni, if you have a minute (and still remember), would you please describe what was exactly the interaction between the compiler code generation and ChibiOS that was causing the bug?. For one thing, I don't know exactly what does the "Use FPU" switch in the project options.

Thanx.

Re: Disabling optimizations (-O0) breaks the CortexM/RVC ker

Posted: Mon Oct 15, 2012 8:04 am
by Giovanni
Hi,

The compiler has an option to enable the FPU, enabling the FPU changes the structure of IRQ stack frames in order to accommodate the FPU registers.
The port option CORTEX_USE_FPU informs the kernel about how to assume those stack frames (small or large), the compiler option and the port option must be changed together and kept consistent.

In the GCC port this problem does not exist because both things are handled in the Makefile.

Giovanni