Skip to content

Architecture Overview

Adam Mathlay edited this page Sep 8, 2024 · 3 revisions

Architecture Overview

This document provides a high-level overview of the Micro Virtual Machine (MVM) architecture. It describes the main components of the VM and how they interact to execute programs.

Key Components

MVM consists of the following main components:

  1. Instruction Decoder:
  • Purpose: Reads and interprets the instructions (bytecode) of the program.
  • Operation: The instruction decoder fetches the next instruction from memory and analyzes it to identify:
    • The instruction mnemonic (e.g. "MOV", "ADD", "JMP").
    • The operands associated with the instruction (e.g. registers, memory addresses, literal values).
  1. Execution Engine:
  • Purpose: Executes the decoded instructions.
  • Operation: The execution engine performs the actions specified by each instruction. This may involve:
    • Reading and writing values from registers.
    • Accessing memory locations.
    • Performing arithmetic, logical, or stack operations.
    • Managing the program counter (PC), which points to the next instruction to execute.
    • Handling system calls.
  1. Registers:
  • Purpose: High-speed memory locations that hold data used during program execution.
  • Types:
    • General-Purpose Registers (G1-G4): Used for general data manipulation.
    • System Call Registers (S1-S4): Used for passing arguments to system calls.
    • Return Registers (R1-R4): Used for storing the results of operations and system calls.
  1. Memory:
  • Purpose: Stores the program's instructions and data.
  • Model: MVM uses a flat, fixed-size memory model. All memory locations are directly addressable.
  1. System Call Handler:
  • Purpose: Handles requests from guest programs to interact with the underlying operating system or the VM environment.
  • Operation: When the execution engine encounters a SYSCALL instruction, it invokes the system call handler. The handler determines which system call to execute based on the system call number and dispatches the call to the appropriate function.
  1. Stack:
  • Purpose: Provides a LIFO (Last-In, First-Out) data structure used for:
    • Storing temporary data.
    • Saving return addresses for function calls.
  • Implementation: The stack is implemented using its own set of memory of a size of 32

Data Flow

  1. Fetch: The instruction decoder fetches the next instruction from memory, using the program counter (PC) to determine the address.
  2. Decode: The instruction decoder analyses the instruction to identify the mnemonic and operands.
  3. Execute: The execution engine performs the actions specified by the instruction.
  4. Update PC: The program counter is updated to point to the next instruction.

Error Handling

The VM is designed to detect and handle various errors that might occur during execution:

  • Invalid instructions
  • Invalid register access
  • Invalid memory accesses
  • Stack overflows and underflow
  • Arithmetic errors
  • System call errors

Error handling is done using exceptions. When an error is detected, the VM throws an exception, which is caught and handled by the appropriate exception handler, which always ends in the main process being terminated

Simplified Diagram

A horrendous diagram

+-----------------+
| Instruction     |
| Decoder        |------>+------------------+
+-----------------+       | Execution Engine |------>+---------------+
                          +------------------+       | System Call    |
                                                      | Handler       |
                          +-----------------+       +---------------+
                          | Registers         |
                          +-----------------+

                          +-----------------+
                          | Memory           |
                          +-----------------+

                          +-----------------+
                          | Stack            |
                          +-----------------+

Key Points:

  • Modular Design: The VM's components are separated for clarity and ease of understanding.
  • Simple Execution Model: The fetch-decode-execute cycle is straightforward.
  • Limited Features: MVM is a micro VM designed for education and experimentation; it has a limited set of features compared to full-fledged VMs.
Clone this wiki locally