What Is the Call Stack, the Kernel Stack and the User Stack?

Question
You have heard of different stacks such as the call, kernel and user stacks.

How are they different?

Answer
"The call stack, or runtime stack, is an area of memory used to store function return information and local variables." (This was taken from https://developer.arm.com/documentation/101470/2021-2/Examining-the-Target/Examining-the-call-stack)

"The user stack contains the arguments, local variables, and other data for functions executing in user mode." (Taken from https://www.gotothings.com/unix/user-and-kernel-stack-for-copy-program.htm.)

"The user stack is only used while the process is running in user mode." (Taken from https://www.baeldung.com/linux/kernel-stack-and-user-space-stack.)

"The kernel stack is a per-process memory region maintained in kernel memory that is used as the stack for execution of the functions called internally during the execution of a system call." (This was taken from page 122 of Kerrisk's The Linux Programming Interface book.)

The user stack resides in a different location from the kernel stack (taken from https://stackoverflow.com/questions/12911841/kernel-stack-and-user-space-stack). What can access to the stacks differs too (also taken from https://stackoverflow.com/questions/12911841/kernel-stack-and-user-space-stack).

"In a Linux system, every user process has 2 stacks, a user stack and a dedicated kernel stack for the process." (Taken from https://www.quora.com/What-is-a-processes-kernel-stack-What-exactly-is-its-use-besides-keeping-the-thread_info.)

The call stack is the combination of the user stack and the kernel stack.

"The kernel stack and the user stack are implemented using the stack data structure and, taken together, serve as a call stack. " (Taken from https://www.baeldung.com/linux/kernel-stack-and-user-space-stack.)

The kernel stack (as defined by https://linux-kernel-labs.github.io/refs/heads/master/lectures/intro.html) is as follows:

"Each process has a kernel stack that is used to maintain the function call chain and local variables state while it is executing in kernel mode, as a result of a system call.

The kernel stack is small (4KB - 12 KB) so the kernel developer has to avoid allocating large structures on stack or recursive calls that are not properly bounded."

"The kernel stack is directly mapped to the physical memory, mandating the arrangement to be physically in a contiguous region." (Taken from https://subscription.packtpub.com/book/application-development/9781785883057/1/ch01lvl1sec10/kernel-stack)

"The call stack is a list of call stack entries, in a last-in-first-out (LIFO) order. A call stack entry is a call to a program or procedure." (Taken from https://www.ibm.com/docs/en/i/7.4?topic=overview-call-stack.)

See these articles for more information:

This may serve as a relevant illustration: https://stackoverflow.com/questions/48250382/how-to-display-user-mode-stack-of-current-kernel-mode-context

Leave a comment

Your email address will not be published. Required fields are marked *