This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

SYS/BIOS and the stack used by main()



This is a generic question that equally applies to all versions of SYS/BIOS. I believe I know he answer after debugging an issue but I want to get confirmation from the SYS/BIOS experts.

So a SYS/BIOS program eventually gets to main() and eventually main() calls BIOS_start() which never returns back to main. What surprised me was that any stack variables declared inside of main get clobbered after calling BIOS_start() and the threads take off running. I understand that we have a system stack and interrupts for the most part use the system stack. I was also under the impression that main used the system stack. Calling a function from main normally doesn't impact main()'s stack but apparently when you call BIOS_start(), main()'s stack is lost. It's as if the system stack reuses the same stack storage as main and overwrites anything main had on there.

Can somebody confirm this theory?

The moral of the story is, do not rely on stack variables in main() to persist once BIOS_start() is called.

Thanks

  • Hi Brad,

    Your conclusion is correct. Any local variables created on main() function's stack (i.e. System stack) will be lost once BIOS_start() is called. Whenever an interrupt is triggered, the interrupt's dispatcher switches the stack to the top of the System stack and will overwrite any content that was stored on the stack by main() function.

    Best,
    Ashish
  • IMO this is a very surprising behavior from an API design standpoint.  If you make something look like a function that never returns, there is no reason to assume that the function will trash the callers stack.

    I am curious what is the reason for this behavior.

  • Wow, I've been fighting with pseudo-consistent memory corruption seemingly caused by TI-RTOS function calls all day long and just stumbled across this post.. I thought I was going mad! I thought I was gonna have to rewrite all the libraries myself, or toss TI-RTOS completely :(

    So, how can I make the bios not clobber half my memory?

  • Note this thread is only about one specific function BIOS_start()  which you are supposed to call from your main after finishing initialization. The fact that it eats up your stack is not such a big deal once you know it, but if you dont you burn a few hours discovering this. That is why it should be changed imo.