x86 Addresses
11 Jan 2021Previously, we briefly discussed about Physical Address Space and how CPU Mapping works, and observed a typical Physical Address Layout. We look further into the different address spaces in x86 in this post.
x86 Memory Addresses
When a user program issues an address access instruction to a modern x86 CPU, the general flow goes as (assuming no failure or interceptions by cache, etc):
┏━━━━━━━┓ ┏━━━━━━━┓(Linear) ┏━━━━━━━━┓ ┏━━━━━━━┓ ┏━━━━━━━━┓
┃ User ┃ Logical ┃Segmen-┃ Virtual ┃ Paging ┃ Physical ┃ CPU ┃ Devide ┃ Memory ┃
┃Program┣━━━━━━━━▷┃tation ┣━━━━━━━━▷┃ (MMU) ┣━━━━━━━━━▷┃Mapping┣━━━━━━━━▷┃ ┃
┗━━━━━━━┛ Address ┗━━━━━┳━┛ Address ┗━━━━━━━━┛ Address ┗━━━━━━━┛ Commands┗━━━━━━━━┛
┃ △
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
Physical Address (Paging Disabled)
(Linear)
Physical Address: how processor uniquely identifies a storage unit (usually a byte), or the address that used on the processor’s address bus. JOS example of physical address space;
Logical Address: term used from Intel Segmentation Mechanism to describe the segment:offset
pair consumed by the Segmentation Mechanism.
- Depending on the address mode,
segment
is the segment base address in Real Mode and segment selector in Protected Mode; - The
offset
is sometimes referred to as ‘Effective Address’.
Linear Address: another term from Segmentation, it describes the output result after Segmentation Mechanism. When Paging is enabled, it is equivalent to the Virtual Address in the graph; when Paging is disabled, it is equivalent to the Physical Address;
Virtual Address: term introduced from Paging. With Paging enabled, each process has its own Virtual Address Space, whose addresses are translated to Physical Addresses to the CPU bus. The addresses that gdb shows in a debugging session is a virtual address.
Device Commands: trivia concept to software developers, but for the sake of completeness, this is the actual commands that RAM receives. Different processors has its method of mapping Physical Addresses to Device Commands to minimize memory thrashing, increase security, etc.
In the real world, the terms can be mixed up, and even have different meaning between different versions of the official documentations. For example
- Virtual Address is unofficially used to describe the user program generated addresses, which is equivalent to the Logical Address on systems that sets a flat Segmentation mapping;
- In 80286 Manual, Logical Address is also referred as Virtual Address (that was before the introduction of Paging Mechanism);
- Linear Address sometimes is used in place of Virtual Address in the graph above, e.g. Understanding the Linux Kernel.
So it is best to analyze the context when reading about them.