Skip to main content

Module decode

Module decode 

Source
Expand description

s9vm : minimal x86-64 instruction decoder for exit emulation.

When a guest exit is a fault that must be emulated (MMIO, EPT violation, port I/O), the kernel needs three pieces of information: the access width, the direction (read/write), and : for MMIO : the effective address and the destination/source general-purpose register. This module decodes just enough x86-64 to answer those questions for the forms a device model actually sees:

  • mov r8, r/m8, mov r16/r32/r64, r/m8/16/32/64, mov r/m8, r8, mov r/m16/32/64, r (opcodes 0x8A/0x8B/0x88/0x89);
  • mov r8, imm8 and mov r16/32/64, imm16/32/64 (opcodes 0xB0..0xBF);
  • mov r/m8, imm8, mov r/m16/32/64, imm16/32/64 (0xC6/0xC7, /0);
  • mov AL/AX/EAX/RAX, moffs and mov moffs, AL/AX/EAX/RAX (0xA0..0xA3), with a 64-bit offset, or a 32-bit offset under 0x67;
  • movzx/movsx register from memory (0x0F 0xB6/0xB7/0xBE/0xBF);
  • in/out with an immediate port (0xE4..0xE7) or DX (0xEC..0xEF);
  • rdmsr/wrmsr (0x0F 0x30/0x32).

Prefix handling: 0x66 selects a 16-bit operand, 0x67 selects 32-bit addressing (the effective address is zero-extended), segment prefixes are consumed and ignored, and LOCK/REP/REPNE are rejected with DecodeError::Unsupported because none of the supported forms uses them. REX.W/R/X/B is honoured where architectural.

ModRM addressing supports mod = 00/01/10, SIB with scale/base/index, absolute disp32 (SIB base 5 or rm = 5 in 32-bit addressing), RIP-relative disp32 (mod = 00, rm = 101 in 64-bit addressing), signed disp8, and disp32 with the correct sign. mod = 11 forms that are not plain register moves are reported as unsupported rather than mis-decoded.

It is deliberately pure: no memory access, no hardware. The caller supplies a register reader and the instruction bytes; the decoder returns the effective address and the next RIP, or a typed error. Decoding never panics: every read is bounds-checked through slice::get and a truncated instruction yields DecodeError::Truncated. Every result is testable on the host, which is why this logic lives apart from the virtual-machine monitor.

Re-exportsยง

use alloc::string::String; ๐Ÿ”’

Structsยง

MemoryAccess
A guest memory access decoded from an instruction.
MsrAccess
An MSR access decoded from an instruction.
PortAccess
A guest port access decoded from an instruction.
Prefixes ๐Ÿ”’
Prefixes decoded before the opcode.
RegisterAccess
A register-only move decoded from an instruction.

Enumsยง

DecodeError
Reason a sequence could not be decoded.
Decoded
Result of decoding one instruction.
Extension
How a memory operand narrower than the destination register is widened.
ModRmOperand ๐Ÿ”’
Addresses of a ModRM operand, resolved before the instruction length is known.

Constantsยง

MAX_INSTRUCTION
Longest possible x86 instruction.

Functionsยง

decode
Decodes one instruction.
decode_immediate_move ๐Ÿ”’
Builds a mov r/m, imm result once the ModRM operand and immediate are known.
decode_modrm ๐Ÿ”’
Decodes the ModRM byte (and optional SIB/displacement) of an instruction.
decode_read_only_memory ๐Ÿ”’
Builds a read-only memory probe (test/cmp with a memory operand).
error_name
Human-readable name of a decode error (diagnostics).
operand_size ๐Ÿ”’
Operand width in bytes for a general instruction.
parse_prefixes ๐Ÿ”’
Parses legacy and REX prefixes.
read_i8 ๐Ÿ”’
read_i32 ๐Ÿ”’
read_immediate ๐Ÿ”’
Reads an immediate of size bytes, zero-extended to u64.
read_moffs ๐Ÿ”’
Reads the address of a moffs operand.
read_u8 ๐Ÿ”’
read_u16 ๐Ÿ”’
read_u32 ๐Ÿ”’
read_u64 ๐Ÿ”’
register_move ๐Ÿ”’
Builds a register-only move result.