First you need to understand what paging and page errors are: How does x86 swap work?
Process core and memory
The Linux kernel reserves two zones of virtual memory:
- one for kernel memory
- one for programs
The exact split is set to CONFIG_VMSPLIT_... Default:
on a 32-bit version:
- lower 3/4 is the program space:
00000000 to BFFFFFFF - upper 1/4 is the kernel memory:
C0000000 to FFFFFFFF
Like this:
in 64-bit mode: currently only 48 bits are actually used, divided into two equally spaced spaces. The Linux kernel simply assigns:
- bottom handles
00000000 00000000 to 008FFFFF FFFFFFFF - upper core:
FFFF8000 00000000 to FFFFFFFF FFFFFFFF
Like this:
------------------ FFFFFFFF FFFFFFFF Kernel ------------------ FFFF8000 00000000 (not addressable) ------------------ 008FFFFF FFFFFFFF Process ------------------ 00000000 00000000
Process address space
Simplified software virtual process memory:
------------------ <--- Top of the process address space Stack (grows down) vvvvvvvvv ------------------ (unmapped) ------------------ <--- Maximum stack size. (unmapped) ------------------- mmap ------------------- (unmapped) ------------------- ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ brk (grows up) ------------------- BSS ------------------- Data ------------------- Text ------------------- ------------------- <--- Bottom or process address space.
Stack placement
The kernel maintains a list of pages related to each process and synchronizes them with paging.
If the program accesses memory that does not belong to it, the kernel processes the page error and decides what to do:
- if it exceeds the maximum stack size, select these pages for the process
- otherwise, send SIGSEGV to a process that usually kills it.
Additional information: https://unix.stackexchange.com/questions/145557/how-does-stack-allocation-work-in-linux/239323#239323
brk and mmap
These system calls allow processes to explicitly request chunks of memory for the kernel instead of just popping off the stack and segfault.
Here is a practical brk example: What does the brk () system call do?
This answer explains the advantage of using the stack when possible: What is the function of the push / pop instructions used for registers in the x86 assembly?
Physical memory
There is no clear separation between the kernel and memory in user space: Is there an blurring of experimentation between user space and kernel in physical memory on Linux x86-64?
source share