Confused about ARM stack usage

This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

  • Confused about ARM stack usage

    I seek to better understand stack usage in the emBOS ARM world. My current application is on an NXP ARM7, the LPC2138. I am working with IAR 4.41 tools.

    The ARM-specific documentation that came with emBOS says that emBOS internals run in SUPERVISOR mode, and tasks run in SYSTEM mode. This doc also seems to equate SYSTEM and SUPERVISOR stacks in section 6.2, which would be incorrect if that is the implication. The stack initialization included with embOS for this processor (and a few others that I glanced at) only sets up a SYSTEM and an IRQ stack. The code switches to SYSTEM mode before completing initialization and calling main. Is is the SUPERVISOR stack allocated and sized by internal OS routines ?
  • It might be confusing, but everything is correct with stacks.
    Using IAR, there are two stacks which have to be declared in the linker script file.

    CSTACK is the system stack.
    IRQ_STACK is the interrupt stack.

    The CSTACK is used during startup, during main(), or embOS internal functions, and for C-level interrupt handler.
    The IRQ_STACK is used when an exception is triggered. The exception handler saves some registers and then performs a mode switch which then uses the CSTACK as stack for further execution.

    The start up code initializes the sytem stack pointer and the IRQ stack pointer.

    When the CPU starts, it runs in Supervisor mode.
    The start up code switches to IRQ mode and sets the stack pointer to the stack which was defined as IRQ_STACK.
    The start up code switches to System mode and sets the stack pointer to the stack which was defined as CSTACK.

    The main() function therefore is called in system mode and uses the CSTACK.

    When embOS is initialized, the supervisor stack pointer is initialized. The supervisor stack and system stack are the same.
    This is no problem, because the supervisor mode is not entered as long as main() is executed. All functions run in system mode.
    After embOS is started with OS_Start(), embOS internal functions run in Supervisor mode, as long as no task is running.
    The CSTACK may then be used as Supervisor stack, because it is not used anymore.

    Tasks run in system mode, but they do not use the "system" stack. Tasks have their own stack which is defined as some variable in any RAM location.

    I hope, this explanation helps to understand how stacks are used.