The x86-64 architecture (also known as AMD64, x64, or Intel 64) is a 64-bit extension of the original x86 instruction set. It maintains backward compatibility with 32-bit and 16-bit x86 code while introducing significant enhancements for 64-bit computing.
Understanding its "anatomy" means looking at its core components, how they are organized, and their purpose from a programmer's perspective.
Here's a breakdown:
Anatomy of x86-64 Architecture
1. General Purpose Registers (GPRs)
One of the most significant changes from 32-bit x86 is the increase in the number and size of registers. All GPRs are now 64-bit.
-
RAX (Accumulator Register): Often used for return values from functions, multiplication, and division operations.
EAX (32-bit lower half)
AX (16-bit lower half of EAX)
AL (8-bit lower half of AX)
AH (8-bit upper half of AX)
-
RBX (Base Register): Historically used as a base pointer for data access.
-
RCX (Counter Register): Often used as a loop counter.
-
RDX (Data Register): Used for I/O operations, also for multiplication and division (high-order bits).
-
RSI (Source Index Register): Used as a source pointer for string and memory block operations.
-
RDI (Destination Index Register): Used as a destination pointer for string and memory block operations.
-
RBP (Base Pointer Register): Used as a frame pointer for stack frames, typically pointing to the base of the current function's stack frame.
-
RSP (Stack Pointer Register): Points to the top of the stack. The stack grows downwards in memory.
-
New 64-bit Registers (R8-R15):
- These 8 new registers are purely general-purpose and do not have the historical or implied roles of the older GPRs.
- They can be accessed as 64-bit (
R8), 32-bit (R8D), 16-bit (R8W), or 8-bit (R8B).
- Their introduction significantly reduces the need to spill values to memory, improving performance.
2. Instruction Pointer
- RIP (Instruction Pointer): A 64-bit register that stores the memory address of the next instruction to be executed.
EIP (32-bit) and IP (16-bit) are its lower halves for compatibility modes.
- RIP-relative addressing: A major improvement in x86-64, allowing data to be referenced relative to the instruction pointer, which simplifies position-independent code (PIC) generation.
3. Flags Register
- RFLAGS (Flags Register): A 64-bit register (though many bits are reserved or unused) that stores the current state of the CPU and controls certain operations.
- Status Flags: Reflect the result of arithmetic/logic operations.
- ZF (Zero Flag): Set if the result is zero.
- SF (Sign Flag): Set if the result is negative (most significant bit is 1).
- CF (Carry Flag): Set if an unsigned arithmetic operation generated a carry or borrow.
- OF (Overflow Flag): Set if a signed arithmetic operation generated an overflow.
- PF (Parity Flag): Set if the number of set bits in the low byte of the result is even.
- AF (Auxiliary Carry Flag): Used in BCD (Binary Coded Decimal) arithmetic.
- Control Flags: Influence CPU operation.
- DF (Direction Flag): Controls the direction of string operations (increment or decrement pointers).
- IF (Interrupt Flag): Enables/disables maskable hardware interrupts.
- TF (Trap Flag): Enables single-step debugging.
- IOPL (I/O Privilege Level): Specifies the CPU's I/O privilege level.
- NT (Nested Task Flag): Controls chaining of interrupted tasks.
- RF (Resume Flag): Controls instruction breakpoint processing.
- VM (Virtual Mode Flag): Enables virtual 8086 mode.
- AC (Alignment Check Flag): Enables memory alignment checking.
- VIF/VIP (Virtual Interrupt Flag/Pending): For virtualization.
- ID (ID Flag): Indicates CPU's ability to use
CPUID instruction.
4. Segment Registers
-
CS (Code Segment): Points to the segment containing the currently executing code.
-
SS (Stack Segment): Points to the segment containing the stack.
-
DS (Data Segment):): Points to the segment containing data.
-
ES (Extra Segment): An additional segment register for data.
-
FS (Segment Register F): Often used for Thread-Local Storage (TLS) in modern OSes.
-
GS (Segment Register G): Also often used for TLS or operating system-specific data.
-
Diminished Role in x86-64: In 64-bit mode (Long Mode), segmentation is largely flattened. The base address for CS, SS, DS, and ES is almost always 0, creating a flat 64-bit address space. FS and GS retain their utility for special purposes like TLS.
5. SIMD/Vector Registers (XMM, YMM, ZMM)
These registers are used for Single Instruction, Multiple Data (SIMD) operations, allowing a single instruction to operate on multiple data elements simultaneously (e.g., adding four pairs of numbers in parallel).
- XMM0-XMM15: 16 registers, each 128-bit wide. Used by SSE, SSE2, SSE3, SSSE3, SSE4.1, SSE4.2 instruction sets.
- YMM0-YMM15: 16 registers, each 256-bit wide. These registers overlay the XMM registers (e.g., YMM0 uses the same physical space as XMM0 for its lower 128 bits). Used by AVX and AVX2 instruction sets.
- ZMM0-ZMM31: 32 registers, each 512-bit wide. These registers overlay the YMM and XMM registers. Used by AVX-512 instruction set.
6. Control Registers (CR0-CR4, CR8)
These registers control various aspects of the CPU's operation, particularly for memory management, protection, and operating modes. Only accessible by privileged code (kernel).
- CR0: Contains system control flags (e.g., paging enable, protected mode enable).
- CR1: Reserved.
- CR2: Stores the linear address that caused a page fault.
- CR3: Contains the base address of the PML4 (Page Map Level 4) table, which is the root of the paging hierarchy.
- CR4: Contains flags that enable various CPU features (e.g., PAE, VME, SMAP/SMEP).
- CR8 (Task Priority Register): Controls the priority of external interrupts (specific to x86-64).
7. Debug Registers (DR0-DR7)
Used for hardware-assisted debugging, allowing debuggers to set breakpoints (code execution) and watchpoints (memory access).
- DR0-DR3: Store linear addresses for breakpoints/watchpoints.
- DR6: Debug status register.
- DR7: Debug control register.
8. Model Specific Registers (MSRs)
A set of processor-specific registers that control various low-level CPU features, power management, performance monitoring, and other advanced functionalities. Accessed via RDMSR and WRMSR instructions.
9. Memory Subsystem
- Linear Address Space: x86-64 architecture defines a 64-bit linear address space (2^64 bytes). However, current implementations typically only support 48-bit (256 TB) or 52-bit (4 PB) virtual addresses, with the upper bits being sign-extended. Physical address space is usually smaller.
- Paging: Memory management is primarily handled by paging. Virtual addresses are translated into physical addresses via a multi-level page table hierarchy (PML4, PDPT, PD, PT). This is crucial for virtual memory, memory protection, and sharing.
- Flat Memory Model: In 64-bit mode, the segment registers typically define a base address of 0 for all memory accesses, creating a single, flat 64-bit address space. This simplifies memory management compared to segmented memory in older x86 modes.
- Caches (L1, L2, L3): Hierarchical caching system to speed up memory access by storing frequently used data and instructions closer to the CPU.
10. Instruction Set Architecture (ISA)
- CISC (Complex Instruction Set Computer): x86-64 is fundamentally CISC, with variable-length instructions ranging from 1 to 15 bytes.
- New Instructions: Introduces new instructions and instruction prefixes (like the
REX prefix) to handle 64-bit operands and new registers.
- RIP-Relative Addressing: As mentioned, a key enhancement allowing more efficient position-independent code.
11. Operating Modes
x86-64 processors support several operating modes for backward compatibility:
- Long Mode (64-bit): The primary operating mode for x86-64.
- 64-bit Mode: Native 64-bit execution, all 64-bit registers are available, flat memory model, paging.
- Compatibility Mode: Allows execution of legacy 32-bit applications under a 64-bit OS without re-compilation.
- Legacy Modes (for backward compatibility, not typically used by a 64-bit OS):
- Protected Mode: The standard 32-bit x86 mode (flat 32-bit address space, paging, protection).
- Real Mode: The original 16-bit x86 mode (segmented 1MB address space, no protection).
12. Stack and Calling Conventions
- Stack: A LIFO (Last-In, First-Out) data structure used for storing return addresses, function arguments, local variables, and saved register values. It grows downwards in memory (from higher to lower addresses).
- RSP and RBP are central to stack management.
- Calling Conventions (ABI - Application Binary Interface): Standardized rules for how functions pass arguments, return values, manage the stack, and use registers. Examples include:
- System V AMD64 ABI (Linux, macOS): Passes first 6 integer/pointer arguments in RDI, RSI, RDX, RCX, R8, R9. Other arguments go on the stack. Return value in RAX.
- Microsoft x64 Calling Convention (Windows): Passes first 4 integer/pointer arguments in RCX, RDX, R8, R9. Other arguments go on the stack. Return value in RAX.
- Both define caller-saved and callee-saved registers to ensure proper state restoration across function calls.
This comprehensive overview covers the major components that define the x86-64 architecture, crucial for understanding how software interacts with the hardware at a low level.