🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →Beginner–Intermediate
1 of 2
VAS & Memory Layout
When you write a C program on Linux and print the address of a variable, you are not looking at a real physical RAM address. You are looking at a virtual address. This is one of the most important ideas in the entire Linux kernel, and the concept that governs it is called the Process Virtual Address Space (Process VAS). In this free Linux kernel development course tutorial, we will break down exactly what the Process VAS is, how the Linux kernel organizes it, and how it differs between 32-bit and 64-bit architectures.
Understanding the Process Virtual Address Space is essential for anyone learning free Linux kernel programming, writing Linux device drivers, or debugging low-level embedded Linux applications. Let’s go deep into how the kernel actually manages memory for every running process.
What You Will Learn
- What virtual memory is and why the Linux kernel uses it
- How the Process Virtual Address Space (VAS) is structured
- The difference between user space VAS and kernel space VAS
- Memory layout on both 32-bit and 64-bit (x86_64) Linux systems
- What UVA (User Virtual Address) and KVA (Kernel Virtual Address) mean
- Why sparse regions exist in a 64-bit VAS
- How to inspect the VAS of a running process using /proc/PID/maps
- Key segments: text, data, BSS, heap, stack, and memory-mapped regions
What Is Virtual Memory and Why Does Linux Use It?
Modern operating systems do not let programs access physical RAM directly. Instead, every process runs inside its own private, isolated address space called virtual memory. The Linux kernel, working together with the CPU’s Memory Management Unit (MMU), translates virtual addresses into physical addresses at runtime. This translation is invisible to user programs.
This design provides several major benefits. First, each process is isolated: one process cannot accidentally (or maliciously) read or write the memory of another process. Second, a process can use more virtual address space than physical RAM actually exists on the machine, because the kernel can page data to disk and bring it back on demand. Third, the system becomes far more stable because a crash in one process does not corrupt the memory of another.
In this free Linux kernel development course, we focus on how the kernel organizes this virtual address space for every process that runs on the system.
Understanding the Process Virtual Address Space (VAS)
Every time the Linux kernel creates a new process, it gives that process its own completely private Virtual Address Space. Think of the Process VAS as a private map: the process can only see and use the addresses on its own map, and it cannot see anyone else’s map.
This VAS is split into two large regions, separated by a boundary the kernel controls:
Mapped into every process, but only
accessible when running in kernel mode
Accessible by user programs
(text, data, heap, stack, libraries)
The lower portion of the VAS belongs to the running process (user space). The upper portion is kernel space. On a modern 64-bit Linux system, both regions are enormous, but only a tiny fraction of that space is actually used by any given process at any given time.
User Virtual Addresses (UVA) and Kernel Virtual Addresses (KVA)
Addresses in the user portion of the VAS are called User Virtual Addresses (UVA). These are the addresses your C programs work with directly — the address of a local variable, a heap allocation, or a function. Addresses in the kernel portion are called Kernel Virtual Addresses (KVA). Your user program can never directly read or write a KVA; attempting to do so results in a segmentation fault.
When a process makes a system call, the CPU switches from user mode to kernel mode. At that point, the kernel code runs inside the same process VAS but using the kernel space region. This is one of the elegant properties of the Linux memory model: the kernel does not need a completely separate address space to serve a process — it is already mapped in at the top of every process VAS.
Process VAS Memory Layout: The Key Segments
Inside the user space portion of the VAS, the Linux kernel organizes the memory of a running process into several well-defined segments. Each segment has a specific purpose. Understanding these segments is fundamental to both free Linux kernel programming and writing Linux device drivers.
Shared libraries, file mappings, anonymous maps
malloc / calloc / realloc
Uninitialized global & static variables (zero-filled)
Initialized global & static variables
Compiled machine code of the program
Text Segment
This is where the compiled machine code of your program lives. It is marked read-only and executable. If your program tries to write to this region, the kernel sends a segmentation fault signal. The read-only protection means a malicious write cannot overwrite your program’s instructions at runtime.
Data Segment and BSS Segment
Global variables you initialize in your C code (like int counter = 5;) live in the data segment. Global variables you declare but do not initialize (like int total;) live in the BSS segment. The kernel automatically zeroes the BSS region when a process starts — you do not need to zero them yourself.
Heap
Dynamic memory allocation via malloc(), calloc(), or realloc() comes from the heap. The heap starts just above the BSS and grows upward toward higher addresses as your program allocates more memory.
Memory-Mapped Region
Shared libraries (like libc.so) are mapped into this region. When you call a standard library function, the CPU is actually executing code that was loaded into this mmap region. File mappings (via mmap()) and anonymous memory mappings also live here.
Stack
Local variables, function arguments, and return addresses all live on the stack. The stack grows downward — that is, toward lower addresses — as functions are called. The kernel allocates a fixed maximum size for the stack (typically 8 MB by default on Linux), and if a process exceeds it, a stack overflow occurs.
NULL Trap Page
The very bottom of the user VAS — from address 0x0 up to 0x1000 — is deliberately left unmapped. This means any attempt to dereference a NULL pointer immediately causes a page fault, which the kernel converts into a segmentation fault signal to the process. This is the kernel’s built-in defense against the most common category of programming bugs.
VAS on 32-bit vs 64-bit Linux: The Size Difference
The total size of the Process VAS depends entirely on the architecture. On a 32-bit system, addresses are 32 bits wide, which gives a total VAS of exactly 4 GB (2³² bytes). On a 64-bit system like x86_64, addresses are theoretically 64 bits wide — but in practice, modern hardware only uses 48 bits for addressing (or 57 bits with newer 5-level paging). Even with 48 bits, the available VAS is a staggering 256 TB (2⁴⁸ bytes).
| Property | 32-bit Linux | 64-bit Linux (x86_64) |
|---|---|---|
| Total VAS | 4 GB | 128 TB (48-bit addressing) |
| User Space | ~3 GB (configurable) | ~128 TB |
| Kernel Space | ~1 GB | ~128 TB |
| UVA Range | 0x00000000 – 0xBFFFFFFF | 0x0000000000000000 – 0x00007FFFFFFFFFFF |
| KVA Range | 0xC0000000 – 0xFFFFFFFF | 0xFFFF800000000000 – 0xFFFFFFFFFFFFFFFF |
| Sparse Regions | Minimal | Extremely large (most of VAS is sparse) |
On a 64-bit system, the enormous VAS means that the actual memory segments (text, heap, stack) take up an almost infinitely small fraction of the available address space. The rest is simply unmapped — these are called sparse regions. Any attempt to access a sparse region causes an immediate page fault.
Inspecting the VAS with /proc/PID/maps
Linux exposes the VAS of every running process through the proc filesystem. The file /proc/PID/maps shows every mapped memory region for the process with the given PID. This is one of the most powerful diagnostic tools available when studying free Linux kernel programming or debugging embedded Linux applications.
Reading /proc/PID/maps
To view your own process’s memory map, run:
cat /proc/self/maps
To view the map of another process (replace 1234 with the actual PID):
cat /proc/1234/maps
A typical output line looks like this:
55a3b2000000-55a3b2001000 r--p 00000000 08:01 1234567 /usr/bin/myprogram
Each field means:
| Field | Meaning |
|---|---|
| 55a3b2000000-55a3b2001000 | Start and end virtual address of this mapping |
| r–p | Permissions: r=read, w=write, x=execute, p=private (COW) |
| 00000000 | Offset into the file (0 means mapped from the beginning) |
| 08:01 | Device (major:minor) |
| 1234567 | Inode number of the mapped file (0 if anonymous) |
| /usr/bin/myprogram | Pathname of the mapped file, or [heap], [stack], [vdso], etc. |
Special Mappings You Will See
When you read /proc/PID/maps, you will encounter a few special named regions. Understanding these is important for any free Linux kernel development course student:
| Name | What It Is |
|---|---|
| [heap] | The dynamic memory region managed by malloc |
| [stack] | The main thread’s stack |
| [vdso] | Virtual Dynamic Shared Object — a small kernel-provided library that makes certain system calls faster (like getting the time) without actually entering the kernel |
| [vvar] | A read-only page the kernel shares with the process for low-overhead access to kernel variables like the current time |
| [-unnamed-] | An anonymous mapping with no associated file |
Why Does the Kernel Map Itself Into Every Process VAS?
You might wonder: why does the kernel need to be mapped into the upper part of every single process’s VAS? Why not keep it completely separate?
The answer is performance. When a user program makes a system call, the CPU switches from user mode to kernel mode. If the kernel had its own completely separate address space, the CPU would need to do a full address-space switch — which requires flushing the Translation Lookaside Buffer (TLB), an extremely expensive operation. By keeping the kernel mapped in the upper half of the same VAS, the kernel can start executing immediately after the mode switch without any TLB flush.
Of course, the kernel region is protected: any user-mode access to a KVA immediately causes a fault. The protection comes from the page table permission bits, not from address-space separation.
Common Mistakes When Learning About Process VAS
Many beginners learning free Linux kernel programming make these mistakes:
The address printed by a C program (e.g., from printf(“%p”, &x)) is always a virtual address. Physical RAM addresses are managed by the kernel and MMU — user programs never see them directly.
Two different processes can both have a local variable at virtual address 0x7fff00001234, but they are in completely separate physical memory locations. Each process has its own VAS.
On a 64-bit system, neither user space nor kernel space uses all of its theoretical range. Both regions are mostly sparse. Only a small number of pages in each region are actually mapped to physical RAM at any given moment.
Address Space Layout Randomization (ASLR) means the exact virtual addresses of heap, stack, and library mappings are randomized at each program start. You cannot hard-code virtual addresses in a security exploit because the layout changes every run.
Key Takeaways
Frequently Asked Questions (FAQ)
Conclusion
The Process Virtual Address Space is the foundation on which everything else in Linux memory management is built. Every kernel module you write, every device driver you develop, and every embedded Linux application you debug will involve understanding how the VAS is organized, how user space and kernel space relate to each other, and how virtual addresses translate to physical memory.
In this free Linux kernel development course tutorial, we covered the structure of the Process VAS, the key memory segments, the differences between 32-bit and 64-bit layouts, and how to inspect the VAS using /proc/PID/maps. In Part 2, we will go deeper into Virtual Memory Areas (VMAs) — the kernel data structures that actually implement the VAS.
This free Linux kernel programming course is published on EmbeddedPathashala, a platform dedicated to making embedded systems and Linux kernel education accessible to everyone.
EmbeddedPathashala — Free courses on Linux Kernel, Linux Device Drivers, BLE, and Embedded Systems
Next Lecture: VMAs Explained → Course Index