Files
betterxen/docs/cpu/instruction-registry.md
T
jelly013 dce37dab18 cpu: add SSE2 state, moves and packed integer forms
Add XMM0-XMM15 and MXCSR to the guest state and extend the registry with
mandatory prefixes, so the shared decoder covers the SSE and SSE2 moves and
packed integer operations. Reject opcodes where 0x66 could be both an
operand size and a mandatory prefix.

Translate operations with identical NEON lane semantics to ARM64; other
forms call the shared semantics and are counted. Raise a distinct
general-protection exit for misaligned 16-byte operands and reserved MXCSR
bits. Floating-point arithmetic is not implemented.

Verify 374 LLVM-assembled instructions against a byte-array reference that
reads only their assembly text, and mixed sequences between the engines.

Refs #124, #125, #126, #128

Signed-off-by: jelly013 <22855910+jelly013@users.noreply.github.com>
2026-11-24 12:53:35 +01:00

3.3 KiB

x86-64 instruction registry

The static PE probe, the interpreter and the native ARM64 dynamic binary translator (DBT) decode guest code through one function, x86_decode::decode. Its tables are generated from crates/loader/x86/instructions.toml.

Part File Role
Registry crates/loader/x86/instructions.toml Opcode map, opcode, ModRM digit, operand encoding, size, immediate, flag effects and public reference. Data only.
Generator crates/loader/generate_x86.py validate, generate and --check. Rejects overlapping encodings, opcodes that are prefixes, contradictory operand rules, unknown vocabulary and flags both written and undefined.
Tables crates/loader/src/x86_table.rs Generated static tables. No metadata is parsed at run time.
Semantics crates/loader/src/x86_exec.rs, x86_simd.rs Rust primitives for integer and vector forms. Every instruction is all-or-nothing.
Lowering crates/loader/src/dbt.rs ARM64 for the forms is_native accepts. Every other form calls the shared semantics and is counted in DbtMetrics::interpreter_fallbacks.

Adding a form

  1. Add the form, and its operation if new, to the registry with an Intel SDM or AMD APM reference.
  2. Run python3 crates/loader/generate_x86.py generate.
  3. Implement the operation in x86_exec.rs. Leave flags the manuals call undefined unchanged.
  4. Add assembly text to crates/loader/tests/x86/assemble.py, run it with generate, and extend the reference in tests/x86_integer.rs or tests/x86_vector.rs. A declared form without a fixture fails every_declared_form_is_executed_by_a_fixture.
  5. Lower it natively only if the ARM64 result and flags match. The same tests require native forms never to call the helper, and helper forms always to.

A declared form is decodable, not implemented. Count a form as supported only when the tests above cover it.

Independence

The references in tests/x86_integer.rs and tests/x86_vector.rs read only LLVM assembly text. They do not use the encodings, registry, decoder or production semantics, and work bit by bit or on byte arrays. They were written by the same contributor as the implementation, so independent review is still required.

Guest flags

The DBT stores CF, ZF, SF, OF, PF and AF in CpuState::flags. It skips the store only when a later instruction in the block overwrites all six before anything can read them, fault or leave the block.

Vector state

XMM registers live in CpuState::xmm, lane 0 in the low bits, and MXCSR in CpuState::mxcsr. A mandatory 0x66, F3 or F2 prefix selects a form; the generator rejects an opcode where 0x66 could be both a mandatory prefix and an operand size. A 16-byte memory operand must be aligned unless its form says otherwise, and a violation exits with GeneralProtection before any memory access. Non-temporal stores are ordinary stores.

Rejected on purpose

LOCK, F2 or F3 where it is neither PAUSE nor part of a declared form, F2 combined with F3, address-size overrides, FS combined with GS, an ignored segment override after FS or GS, 16-bit stack and branch forms, 16-bit BSWAP and MOVSXD, bit tests of memory by a register index, RCL, RCR, SHLD, SHRD, string instructions, MMX, floating-point arithmetic and conversions, SSE3 and later, VEX, atomics and fences. The DBT also refuses raw SYSCALL.