User stack, interrupt stack

 1. Assuming a single thread (non-OS) program with no interrupt. I understand that User Stack is used when function is called - the stack is used to store return address, input parameters, return values, local variables, etc.

2. In a single thread (non-OS) program with interrupts, how is the Interrupt Stack used? When interrupt happens, hardware pushes some CPU registers onto Interrupt Stack, and software (compiler) should push the remaining registers onto Interrupt Stack. Is this correct?

3. What happen to callback function of the interrupt? Are the callback function's return address, input parameters, return values, local variables pushed onto User Stack or Interrupt Stack?

  • 1. Whenever a function is called, either by software or by interrupt, return address is saved on the stack. Interrupts also save the processor status word on the stack.
    By default all software only uses interrupt stack.
    Use of user stack has to be activated by software.
    Normal function calls use the active stack at the time of function call. It does not care if it is user or interrupt stack.
    Interrupt function start switches the active stack to interrupt stack and saves everything to the interrupt stack.
    2. Compiler saves necessary registers bot for normal function calls and for interrupt functions on the stack.
    3. Nothing happens to callback functions. Callback functions are normal function calls originating from an interrupt function. So everything is saved on the interrupt stack.
  • In reply to FrankL:

    >>> By default all software only uses interrupt stack.
    Q1. This is because the U-bit is '0' upon reset, correct?

    >>> Use of user stack has to be activated by software.
    Q2. This is done by setting BSP_CFG_USER_STACK_ENABLE to '1', correct? So if it is '1', then User Stack will be the active stack?

    >>> Normal function calls use the active stack at the time of function call. It does not care if it is user or interrupt stack
    >>> Callback functions are normal function calls originating from an interrupt function. So everything is saved on the interrupt stack.
    Q3.1 So for example, active stack is User Stack. The function myfunc() is called, and therefore all necessary stuff (return addr, parameters, local variables) are pushed onto User Stack. Correct?

    Q3.2 While myfunc() is still running, an interrupt happens. So now PC and PSW are pushed onto Interrupt Stack, and then U-bit is cleared to '0'. The CPU would read the vector and start executing from there. The ISR would usually push more registers onto Interrupt Stack (compiler would insert such codes at the beginning of ISR). Correct?

    Q3.2 In the interrupt handler, a function myIntFunc() is called. Now, the return addr, parameters, local variables are pushed onto Interrupt Stack, correct? Which means at this point in time, there are 2 function call stack frames; one (myfunc()) on User Stack, and the other (myIntFunc1())on Interrupt Stack?
  • In reply to swoo.quek:

    All, yes.
  • In reply to FrankL:

    But why would anyone want to use both User Stack and Interrupt Stack?
  • In reply to swoo.quek:

    swoo.quek
    But why would anyone want to use both User Stack and Interrupt Stack?

    Generally useless in a non-OS program.  When using an OS, it's valuable to separate the stacks used by the system (OS) and userland tasks.

  • In reply to swoo.quek:

    Use of user stack is closely related to use of user mode for software execution. User mode limits access to some processor registers thus making some the software a little bit more secure.