[Arm][Linux] Understanding Stacks in User and Kernel space
It is important to figure out how the stacks are organized in user space and kernel space in Linux system.
This post will outline the user space stack and kernel space stack:
User Stack
Every time a user task (process) is created, a user stack and a kernel stack are allocated.
The size of the user stack can be set when creating a thread. The default size depends on the CPU architecture.
To check the maximum size of the user stack, use the following command:
Kernel Stack
Each task (process) has its own kernel stack. The kernel stack is much smaller than the user stack. Its size depends on the Linux kernel version and configuration. The kernel stack usually uses 1 to 4 pages of memory.
Examples of kernel stack sizes:
Exception Stack (ARM32)
ARM32 architecture uses small mini-stacks for handling exceptions. When an exception occurs, the system first uses a 3-word mini-stack. Then, it switches to SVC mode(supervisor mode) and continues using the SVC stack (kernel stack) for further processing.
Stack Usage During Context Switching
Recommended by LinkedIn
Stack Registers in ARM32 and ARM64
sp_usr, sp_svc, sp_irq, etc.
el0_sp, el1_sp, el2_sp, etc.
Stack Usage During Interrupts
ARM32: Uses the same kernel stack as the running kernel thread.
ARM64: Uses a separate interrupt stack.
ARM32: Uses the same kernel stack.
ARM64: Uses a separate interrupt stack.
Some architectures separate stacks for hard IRQs and soft IRQs:
Lead Engineer @SRID
2moThank you Austin Kim for sharing this insightful article, I have a query, for arm32 if an interrupt occurs while cpu is in kernel space, then both sp_irq and sp_svc points to some location of same kernel stack(please correct me if I am wrong) Then why there is need to maintain 2 pointers, if only one of them will be useful at a time?
Technical Lead |Automotive|Telemetics | platform security | open source | RDKB/V | Yocto | Cyber security
3moVery informative
Sr.engineer-I in harman international
3moThanks Austin Kim for sharing the stack info usage, your posts are really helpful for freshers a alot.
Embedded Systems, Linux Kernel, and Wireless Software Developer
3moVery helpful, thankyou