We know that when a process is created,one stack is allocated for this process.The size of the stack is typically 8 Mb in linux.My question is that,from where this stack is allocated??From user space or from system space?
I hope you know the concept that all user process will be kept in user space only. It uses system calls to get some work done by kernel.
The stack memory will be part of process context area in memory. i.e user space.
Suppose your process is running, get the PID by ps -ax
. say 1234 is your PID.
cat /proc/1234/maps
will give you the mapping of that particular process.
In thats maps file, you can check the stack
for stack mapping.
First you must understand what paging and page faults are: How does x86 paging work?
Kernel vs process memory
The Linux Kernel reserves two zones of virtual memory:
The exact split is configured by CONFIG_VMSPLIT_...
. By default:
on 32-bit:
00000000
to BFFFFFFF
C0000000
to FFFFFFFF
Like this:
------------------ FFFFFFFF
Kernel
------------------ C0000000
------------------ BFFFFFFF
Process
------------------ 00000000
on 64-bit: currently only 48-bits are actually used, split into two equally sized disjoint spaces. The Linux kernel just assigns:
00000000 00000000
to 008FFFFF FFFFFFFF
FFFF8000 00000000
to FFFFFFFF FFFFFFFF
Like this:
------------------ FFFFFFFF FFFFFFFF
Kernel
------------------ FFFF8000 00000000
(not addressable)
------------------ 008FFFFF FFFFFFFF
Process
------------------ 00000000 00000000
Process address space
Simplified program virtual memory of a process:
------------------ <--- Top of the process address space
Stack (grows down)
v v v v v v v v v
------------------
(unmapped)
------------------ <--- Maximum stack size.
(unmapped)
-------------------
mmap
-------------------
(unmapped)
-------------------
^ ^ ^ ^ ^ ^ ^ ^ ^ ^
brk (grows up)
-------------------
BSS
-------------------
Data
-------------------
Text
-------------------
------------------- <--- Bottom or process address space.
Stack allocation
The kernel maintains a list of pages that belong to each process, and synchronizes that with the paging.
If the program accesses memory that does not belong to it, the kernel handles a page-fault, and decides what to do:
More info at: https://unix.stackexchange.com/questions/145557/how-does-stack-allocation-work-in-linux/239323#239323
brk
and mmap
Those system calls allow processes to explicitly request chunks of memory to the kernel instead of just going down the stack and segfaulting.
Here is a practical example of brk
: What does brk( ) system call do?
This answer explains the advantage of using the stack when that is possible: What is the function of the push / pop instructions used on registers in x86 assembly?
Physical memory
There is no clear split between kernel and userspace memory: Is there an explict split between userspace and kernel in physical memory on Linux x86-64?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With