What is kernel stack?
- User
What is kernel stack?
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
What is kernel stack?
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Kernel stack (also called the system-mode stack or privileged stack) is the private execution stack that each thread/process uses while it is running in kernel (privileged) mode.
Below is a concise but complete mental model of what it is, why it exists, and how modern OSes implement it.
────────────────────────────────────────────────────────────────────────
• Not directly reachable from user code (different page tables / privilege level).
Often protected by a guard (unmapped) page to catch overflow.
• Typical sizes (configurable): Linux x86-64 = 16 KiB per thread; Linux ARM64 = 16 KiB; Windows = 12 KiB initial, grows on demand; embedded RTOSs may dedicate just a few KiB.
──────────────────────────────────────────────────────────────────────── 2. Why do we need a dedicated kernel stack? ────────────────────────────────────────────────────────────────────────
Privilege separation
– Prevents buggy/malicious user code from corrupting kernel data by overflowing its own stack.
– Kernel can use a tighter, known-good page layout and enable guard pages, stack canaries, etc.
Simpler context switching
– When the CPU takes an interrupt or performs a system call it automatically pushes some registers onto the current privilege-level stack. The hardware therefore needs to know that the stack it is pushing onto is writable (cannot rely on possibly paged-out, grow-down user stack).
– Having one fixed-size kernel stack per thread means the kernel can rely on always having N bytes of scratch space during the most critical moment of entry.
Performance & determinism
– No demand-faults or resizes while in kernel code; page faults inside the lowest-level fault handler would be disastrous.
– Smaller caches/TLB footprint than leaving a giant user stack mapped in kernel space.
────────────────────────────────────────────────────────────────────────
3. How is it created & switched?
────────────────────────────────────────────────────────────────────────
• Creation
– At thread creation, the scheduler (e.g., Linux’s copy_thread()) allocates a chunk of physical pages, sets up the top-of-stack (for the platform’s grow-down convention), inserts a guard page, and records the base pointer in the task’s control block (TCB / task_struct).
• Switching
– On every context switch, the OS loads the CPU’s stack pointer (SP/RSP) with the new thread’s kernel-stack top.
– User stack pointer (USP) is saved/restored but not used until the thread returns to user mode (iret, sysret, eret, etc.).
────────────────────────────────────────────────────────────────────────
4. What lives on the kernel stack?
────────────────────────────────────────────────────────────────────────
• Saved register frames (created automatically by the CPU on traps/interrupts).
• Call frames for kernel C functions executed by that thread.
• Local variables of those functions (except those optimized into registers).
• Occasionally small per-thread scratch buffers (but large allocations are done from the heap/slab).
• Some kernels also place the Thread Control Block or part of it at a fixed offset relative to the stack (handy for quick current lookup in Linux).
────────────────────────────────────────────────────────────────────────
5. Overflow & hardening
────────────────────────────────────────────────────────────────────────
• Guard page – an unmapped page placed immediately below/above the stack; touching it causes a page fault → kernel panic rather than silent corruption.
• CONFIG_VMAP_STACK (Linux) – places each kernel stack in a separate virtually mapped region, making exploits that rely on predictable addresses harder.
• CONFIG_THREAD_INFO_IN_TASK – separates metadata from the stack to reduce the blast radius of stack overflows.
──────────────────────────────────────────────────────────────────────── 6. Kernel stack vs. Interrupt stack(s) ──────────────────────────────────────────────────────────────────────── Some kernels/architectures use an additional per-CPU interrupt stack:
Linux x86-64 example
– On NMI or double fault, CPU switches to a special IST stack defined in the Task State Segment (TSS) to guarantee some safe space even if the regular kernel stack was already overflowed or corrupted.
– Normal interrupts still run on the current task’s kernel stack (configurable via CONFIG_IRQ_STACKS).
────────────────────────────────────────────────────────────────────────
7. Quick comparison: Linux vs. Windows
────────────────────────────────────────────────────────────────────────
Linux
– Fixed size per task (8 KiB on 32-bit, 16 KiB on 64-bit unless CONFIG_THREAD_SIZE_* overridden).
– No automatic grow; overflow = oops/panic.
– Supports vmap_stack to randomize addresses.
Windows
– Two kernel stacks per thread: initial (12 KiB) + “guard”/expandable area (4 KiB). It can grow up to 64 KiB if necessary, similar to user-mode stacks.
– More tolerant of deep call chains but slower (must check for stack expansion on each call).
────────────────────────────────────────────────────────────────────────
Key Takeaways
────────────────────────────────────────────────────────────────────────
• A kernel stack is the privileged, per-thread scratchpad used only when the CPU executes kernel code.
• It is mapped only in kernel space, has a fixed (or tightly bounded) size, and is essential for safe transition from user to kernel mode.
• Overflow protection, guard pages, and separate interrupt stacks are common hardening measures.
• Understanding kernel stack behavior is critical when writing drivers, low-level code, or diagnosing stack overflows and “double faults.”
Hope that clarifies what a kernel stack is and why it’s a cornerstone of modern OS design!