arch β Instruction Set Architectures
ISA-specific implementations providing instruction decoding, register definitions, fault handling, and MMU logic for each supported architecture.
The arch module is like a universal translator. The generic sub-module defines the grammar rules (base classes), while each ISA folder (x86, AArch32, AArch64, TrustZone security extensions, SVE/SVE2 vector processing, PMU, and hardware transactional memory." href="arm/index.html">ARM, RISC-V) is a specific language dictionary. When gem5's CPU core encounters a binary instruction, it hands it to the appropriate ISA translator to decode, execute, and handle any exceptions β all through the same standardized interface.
Architecture Overview
gem5 supports multiple instruction set architectures through a plugin-style design. The arch/generic directory defines abstract base classes (InstDecoder, BaseMMU, BaseISA, BaseTLB), and each ISA directory provides concrete implementations. This allows the CPU models to remain ISA-agnostic while still accurately modeling architecture-specific behavior.
graph TB
subgraph "Generic Base Classes"
InstDecoder["InstDecoder\n(instruction fetch/decode)"]
BaseMMU["BaseMMU\n(memory management)"]
BaseISA["BaseISA\n(ISA state)"]
BaseTLB["BaseTLB\n(TLB interface)"]
PCStateBase["PCStateBase\n(program counter)"]
end
subgraph "x86"
X86Dec["X86::Decoder"]
X86MMU["X86::MMU"]
X86ISA["X86::ISA"]
X86TLB["X86::TLB"]
end
subgraph "ARM"
ArmDec["ArmISA::Decoder"]
ArmMMU["ArmISA::MMU"]
ArmISAc["ArmISA::ISA"]
ArmTLB["ArmISA::TLB"]
end
subgraph "RISC-V"
RiscvDec["RiscvISA::Decoder"]
RiscvMMU["RiscvISA::MMU"]
RiscvISAc["RiscvISA::ISA"]
RiscvTLB["RiscvISA::TLB"]
end
InstDecoder --> X86Dec
InstDecoder --> ArmDec
InstDecoder --> RiscvDec
BaseMMU --> X86MMU
BaseMMU --> ArmMMU
BaseMMU --> RiscvMMU
BaseISA --> X86ISA
BaseISA --> ArmISAc
BaseISA --> RiscvISAc
BaseTLB --> X86TLB
BaseTLB --> ArmTLB
BaseTLB --> RiscvTLB
ISA Plugin Pattern
gem5's ISA abstraction uses a combination of C++ virtual interfaces and build-system-level selection. At compile time, one or more ISAs are enabled via the build configuration. The CPU model calls generic methods on InstDecoder, BaseISA, and BaseMMU, which dispatch to the ISA-specific implementation.
Generic Base Classes
// arch/generic/decoder.hh
class InstDecoder : public SimObject
{
protected:
void *machInst; // ISA-specific machine instruction
public:
// Feed bytes from instruction fetch
virtual void moreBytes(const PCStateBase &pc, Addr fetchPC) = 0;
// Check if a complete instruction is available
virtual bool instReady() const = 0;
// Decode and return a StaticInst
virtual StaticInstPtr decode(PCStateBase &nextPC) = 0;
// Reset decoder state (e.g., on branch)
virtual void reset() {}
};
// arch/generic/mmu.hh
class BaseMMU : public SimObject
{
public:
enum Mode { Read, Write, Execute };
BaseTLB *dtb; // Data TLB
BaseTLB *itb; // Instruction TLB
// Address translation
virtual Fault translateAtomic(const RequestPtr &req,
ThreadContext *tc, Mode mode) = 0;
virtual void translateTiming(const RequestPtr &req,
ThreadContext *tc, Translation *translation,
Mode mode) = 0;
// TLB maintenance
virtual void flushAll() = 0;
virtual void demapPage(Addr vaddr, uint64_t asn) = 0;
};
PC State Abstraction
Each ISA has a different program counter representation. x86 needs microcode PC tracking, ARM needs Thumb/ARM mode bits, and RISC-V needs compressed instruction awareness. PCStateBase provides the polymorphic interface.
class PCStateBase
{
protected:
Addr _pc = 0;
Addr _npc = 0;
MicroPC _upc = 0;
MicroPC _nupc = 0;
public:
Addr pc() const { return _pc; }
Addr npc() const { return _npc; }
MicroPC upc() const { return _upc; }
virtual PCStateBase *clone() const = 0;
virtual void advance() = 0;
virtual bool branching() const = 0;
};
Supported ISAs
| ISA | Directory | Key Features | Modes |
|---|---|---|---|
| x86-64 | arch/x86/ | Microcode engine, CPUID, APIC, segmentation | SE + FS |
| ARM/AArch64 | arch/arm/ | Security extensions (TrustZone), PMU, HTM, SVE | SE + FS |
| RISC-V | arch/riscv/ | M/A/F/D/C/V extensions, PMP, PMA checker | SE + FS |
| MIPS | arch/mips/ | MIPS32/64, CP0 coprocessor | SE + FS |
| SPARC | arch/sparc/ | SPARC V9, register windows | SE + FS |
| POWER | arch/power/ | POWER ISA | SE |
| Null | arch/null/ | Stub ISA for testing infrastructure | β |
Instruction Decode Pipeline
sequenceDiagram
participant CPU as CPU Frontend
participant Dec as InstDecoder
participant Cache as StaticInst Cache
participant Exec as Execution Unit
CPU->>Dec: moreBytes(pc, fetchPC)
Dec->>Dec: Buffer and parse bytes
CPU->>Dec: instReady()?
Dec-->>CPU: true
CPU->>Dec: decode(nextPC)
Dec->>Cache: lookup machInst
alt Cache hit
Cache-->>Dec: Return cached StaticInst
else Cache miss
Dec->>Dec: Create new StaticInst
Dec->>Cache: Insert into cache
end
Dec-->>CPU: StaticInstPtr
CPU->>Exec: execute(StaticInst, ThreadContext)
π Narrated Walkthrough: From Binary to Execution
- Fetch β The CPU frontend reads bytes from the instruction Cache Hierarchy | gem5 Wiki" href="../mem_doc/cache/index.html">cache at the current PC. For x86, this may be a variable number of bytes; for RISC-V, 2 or 4 bytes.
- Feed Decoder β Fetched bytes are passed to the ISA-specific
InstDecoder::moreBytes(). The decoder accumulates bytes until a complete instruction is recognized. - Decode β
decode()converts the raw machine instruction into aStaticInstobject. For x86, this may produce multiple microops. For ARM, it selects between ARM/Thumb/AArch64 decoding. - Cache β Decoded instructions are cached to avoid redundant decoding. The cache is keyed by the machine instruction encoding.
- Execute β The
StaticInst::execute()method is called with aThreadContext, which provides access to registers, memory, and ISA state. - Faults β If execution triggers an exception (page fault, illegal instruction), the ISA's fault class handles the trap, updating PC to the exception vector.
Fault Hierarchy
Each ISA defines its own fault classes inheriting from FaultBase. Faults represent architectural exceptions (page faults, interrupts, traps) and carry ISA-specific information for handler dispatch.
// Generic fault base
class FaultBase : public RefCounted
{
public:
virtual const char *name() const = 0;
virtual void invoke(ThreadContext *tc,
const StaticInstPtr &inst) = 0;
};
typedef RefCountingPtr<FaultBase> Fault;
const Fault NoFault = nullptr;
Key Components Reference
| Component | Location | Purpose | Category |
|---|---|---|---|
InstDecoder | generic/decoder.hh | Abstract instruction decoder interface | Generic |
BaseMMU | generic/mmu.hh | Abstract MMU with iTLB/dTLB | Generic |
BaseISA | generic/isa.hh | ISA register state container | Generic |
BaseTLB | generic/tlb.hh | Abstract TLB interface | Generic |
PCStateBase | generic/pcstate.hh | Polymorphic program counter | Generic |
FaultBase | sim/faults.hh | Base class for all ISA faults | Generic |
X86::Decoder | x86/decoder.hh | x86 variable-length + microcode decoder | x86 |
ArmISA::Decoder | arm/decoder.hh | ARM/Thumb/AArch64 decoder | ARM |
RiscvISA::Decoder | riscv/decoder.hh | RISC-V 16/32-bit decoder | RISC-V |
X86ISA::Interrupts | x86/interrupts.hh | Local APIC interrupt controller | x86 |
ArmISA::PMU | arm/ArmPMU.py | ARM Performance Monitoring Unit | ARM |
PMP | riscv/PMP.py | RISC-V Physical Memory Protection | RISC-V |
Sub-Modules
π₯οΈ x86
Full x86-64 ISA with variable-length decoding, microcode engine, CPUID emulation, and APIC interrupt controller.
x86-64π± arm
ARM and AArch64 ISA with TrustZone security extensions, SVE, PMU, and hardware transactional memory support.
ARMv8π§ riscv
RISC-V ISA with modular extensions (M/A/F/D/C/V), PMP/PMA security, and compressed instruction support.
RV64GC𧬠generic
ISA-independent abstract base classes defining the decoder, MMU, TLB, ISA state, and PC interfaces.
BaseCross-Module Connections
graph LR
arch["arch\n(ISA decoders, faults, MMU)"]
sim["sim\n(SimObject, EventQueue)"]
cpu["cpu\n(O3, Minor, KVM)"]
mem["mem\n(cache, DRAM)"]
base["base\n(BitUnion, types)"]
base -->|"BitUnion, Addr"| arch
sim -->|"SimObject base"| arch
arch -->|"StaticInst, Faults"| cpu
arch -->|"MMU, TLB"| mem
cpu -->|"ThreadContext"| arch
Glossary
| Term | Definition |
|---|---|
| ISA | Instruction Set Architecture β the contract between hardware and software (x86, ARM, RISC-V) |
| StaticInst | Decoded instruction object containing operand info, execute method, and disassembly |
| MachInst | Raw machine instruction encoding (ISA-specific size and format) |
| ThreadContext | Per-thread CPU state β registers, PC, ISA state β shared between arch and CPU models |
| Microop | Micro-operation β x86 decomposes complex CISC instructions into simpler microops |
| TLB | Translation Lookaside Buffer β caches virtual-to-physical address translations |
| PMP | Physical Memory Protection β RISC-V mechanism for memory access control |
| TrustZone | ARM security extension partitioning system into Secure and Non-Secure worlds |