The Stack

When used without other qualification, TheStack refers to one of two things (which in many systems are handled the same way).

RexxLanguage is not a stack-based language. It's an applicative, infix language just like BASIC or PL/1. I corrected this above. --SamuelFalvo?

In many architectures, TheStack has CPU support of some kind (a dedicated register to hold the current address--the StackPointer?, dedicated memory space, preferential cache treatment, etc). In many other architectures, a GeneralPurposeRegister? is used by convention (on PowerPC processors, register R1 is used as the stack pointer in the various PowerPc ApplicationBinaryInterface?s; even though there isn't anything special about this particular register in the InstructionSetArchitecture?).

As stacks generally have to occupy contiguous areas of address space, stack management can become a big problem with some operating systems, especially for multi-threaded programs. Also, since access to TheStack generally has to be fast, most languages do not peform runtime checks for stack overflow. (One common technique employed by many operating systems is to have a GuardPage? at the bottom/top of the stack; any memory access to this page results in a page fault. The JavaVirtualMachine is required to verify that the operand stack sizes are correct when loading a class.)

How does paging TheStack work? Don't interrupts use TheStack too?

In user applications, virtual memory pages related to the stack can be paged in and out, just like any other application data pages.

Software interrupts such as Unix signal/signal handlers do use TheStack; the previous context is pushed wherever the SP happens to be pointing.

More precisely: When an exception is triggered (either a software or hardware trap, interrupt, et. al.), the microprocessor pushes this information onto a kernel stack, not on the process stack. The kernel stack is located in a swath of memory that is guaranteed to always be in RAM at all times. The current user stack pointer (usually abbreviated USP, to distinguish it from the kernel or system stack pointer, SSP) is pushed onto the kernel stack, thus providing the necessary linkage to allow the kernel to return back to the user process. At it's option, the kernel MAY choose to off-load some state onto the user stack and adjust the USP accordingly, then return back to the user-mode process, thus effecting an "asynchronous subroutine" (as VMS calls them) or "signal" (as Unix calls them). --SamuelFalvo?

In some language implementations this has sometimes meant that a stack backtrace isn't available from the debugger from within a signal handler, because the previous stack frame isn't in the canonical form, since the signal handler function was called unexpectedly.

The OS stack is (in a protected memory environment) a separate area from that of application process stacks, and may or may not be paged. Operating systems typically have a certain set of pages that are locked in physical memory.

True (hardware) interrupts depend on the host cpu architecture for their details, but they switch to privileged mode and save context somewhere -- possibly in a reserved area, possibly on a stack. For the purpose of discussion one can imagine that it saves the previous context to the primary OS stack (there might be several in multi-threaded OSes).

The "bottom half" of interrupt handlers queue a request to handle the interrupt and then return as rapidly as possible; it's A Bad Thing to spend much time in interrupt routines. The OS then handles that request at its leisure.


EditText of this page (last edited November 13, 2014) or FindPage with title or text search