Page 1 of 1

Stack usage within ARM ASM

Posted: Wed Jan 09, 2013 11:29 am
by TexZK
Hi Giovanni, I would like to know if there are any stack allocations made by ARM ASM statements.
I am developing a project for a STM32F4, and I am willing to write some tools to know the stack usage in advance, instead of relying on the counters available with ChibiOS (which depend only on the executed path, not on the worst case). I am wondering about combining the GCC -fstack-usage stack predictions along with the call graphs generated by cflow. These tools only work with C code, and are obviously blind to ASM code. I have never worked with ARM ASM, so I do not know where there could be stack allocations, and you know your creature far better than I do.
So, is it possible to find an upper bound of stack allocation (function calls) made by ASM? This way I can easily bias stack computations at least in a conservative way.

Re: Stack usage within ARM ASM

Posted: Wed Jan 09, 2013 11:52 am
by Giovanni
Hi,

In ChibiOS stacks sizes declarations are mediated by the macro THD_WA_SIZE() which adds to the specified size the overhead required by the system.

There is very little ASM code called by the kernel, usually just _port_switch() does stack allocations and the stack used by _port_switch() is guaranteed to be sizeof(intctx). Intctx represents exactly the stack frame of that function.

Good luck with your idea, it is a very interesting one, you could have problems only with recursions but I don't expect any of those in any real embedded software. I was thinking to do something similar using call graphs generated by Doxygen but never had the time to implement it.

Giovanni

Re: Stack usage within ARM ASM

Posted: Wed Jan 09, 2013 10:54 pm
by TexZK
Since I am writing my project in ANSI C and all the source code is available, it is possible to generate the full call graph with cflow (= vertex set), and compute the static stack usage of every function with the -fstack-usage GCC flag (= edge set). A standard maximum-path algorithm would find the maximum stack usage. Thread root functions are the starting exploration points, and of course there is a bias (= THD_WA_SIZE(0)) because of the thread control block.

I also cannot compute the stack usage of some very low level files, which should be analyzed by hand:
../../../../os/ports/GCC/ARMCMx/crt0.c: In function '_default_exit':
../../../../os/ports/GCC/ARMCMx/crt0.c:239:1: warning: -fstack-usage not supported for this target [enabled by default]
../../../../os/ports/GCC/ARMCMx/STM32F4xx/vectors.c: In function '_unhandled_exception':
../../../../os/ports/GCC/ARMCMx/STM32F4xx/vectors.c:212:1: warning: -fstack-usage not supported for this target [enabled by default]
../../../../os/ports/GCC/ARMCMx/chcore_v7m.c: In function '_port_switch_from_isr':
../../../../os/ports/GCC/ARMCMx/chcore_v7m.c:214:1: warning: -fstack-usage not supported for this target [enabled by default]


Of course, I am not asking you to do the analysis ;) but only if you know that no ASM code would alter dramatically the estimation made with the method mentioned above. I can accept some small bias (in the order of a dozen bytes); exact fitting of stacks is insane, while I would always leave some padding.

Anyway, if my idea is successful, I will share the simple tool I am willing to make 8-)

Re: Stack usage within ARM ASM

Posted: Wed Jan 09, 2013 11:20 pm
by TexZK
Ok, I have found the struct intctx, which you say should be the only (so the maximum) stack allocation made by the ARM-CM4 port.
Thanks for the tip, it would save from A LOT of useless headaches, since I don not know the ARM ASM, and I do not even understand the GCC ASM syntax :oops: