Definition
Kernel Stack
The kernel stack (also known as the system stack) is a separate stack used by the operating system when a process is executing in kernel mode.
Each process typically has its own kernel stack, which is used to store:
- Parameters and return addresses for system calls.
- The saved hardware context (e.g., Program Counter, PSW, and registers) during an interrupt or trap.
- Local variables for kernel-level routines.
Separation from User Stack
The kernel stack is distinct from the user stack to ensure system security and stability.
- Security: If the kernel used the user stack, a malicious process could modify return addresses or parameters while the kernel is executing a system call.
- Robustness: If a user process exhausts its own stack space (stack overflow), the kernel remains functional because its own stack is separate and protected.
Role in Context Switching
During a process switch, the kernel stack is used to facilitate the saving and restoring of the process context. The state of the process is pushed onto its kernel stack before being moved to the PCB for long-term storage.