Declare XADD and CMPXCHG in the instruction registry for 8- to 64-bit register and memory destinations. The LOCK prefix is accepted on the forms the registry marks lockable and only with a memory destination; every other use stays an illegal instruction. Both engines run the new forms on the shared semantics. A failed CMPXCHG still writes its destination, so it faults on memory that cannot be written, and a fault changes nothing. The registry document states the ordering, alignment, compare-failure and fault rules a guest scheduler has to preserve. Nothing is lowered to ARM64 atomics. Fourteen SDM-derived cases already in cpu_known_answers.rs move from refused to verified. Adds 62 LLVM-assembled fixtures with reference arms, generator checks for the lock key and tests/x86_atomic.rs. Signed-off-by: zachdoxsee <zachdoxsee36@gmail.com>
6.6 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, x86_float.rs, softfloat.rs |
Rust primitives for integer, vector and floating-point 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
- Add the form, and its operation if new, to the registry with an Intel SDM or AMD APM reference.
- Run
python3 crates/loader/generate_x86.py generate. - Implement the operation in
x86_exec.rs. Leave flags the manuals call undefined unchanged. - Add assembly text to
crates/loader/tests/x86/assemble.py, run it withgenerate, and extend the reference intests/x86_integer.rsortests/x86_vector.rs. A declared form without a fixture failsevery_declared_form_is_executed_by_a_fixture. - 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.
FS and GS overrides together are architecturally undefined; GS applies, in both engines.
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.
Floating point
softfloat.rs follows the Intel SDM (Vol. 1, 4.8, 4.9 and 11.5) without host floating point. Expected values come from exact fractions in tests/x86/float_reference.py. Some behaviour is implemented one way but not asserted, because the sections used leave it open or it was not found there:
- a denormal dividend over zero raises divide-by-zero only, not also denormal-operand;
- flush-to-zero also flushes a tiny result that is exact, raising underflow and precision;
- DAZ applies before conversion to an integer;
- an unmasked overflow also records precision.
None of it has been compared with x86 hardware.
Locked instructions
XADD and CMPXCHG exist for 8-, 16-, 32- and 64-bit register and memory destinations. The LOCK prefix (F0) is accepted on exactly the forms the registry marks lock = true, and only when the destination is memory. Today those are the memory forms of XADD and CMPXCHG. LOCK anywhere else is an illegal instruction, as the Intel SDM (Vol. 2, "LOCK") specifies for a register destination or an instruction that cannot be locked. LOCK on the other instructions the SDM allows (ADD, ADC, AND, BTC, BTR, BTS, DEC, INC, NEG, NOT, OR, SBB, SUB, XOR and XCHG), CMPXCHG8B and CMPXCHG16B are not implemented and stay rejected.
These rules hold for both engines and are what a guest scheduler has to preserve:
- Ordering. Each engine runs one guest instruction at a time on one host thread, and an instruction either completes or changes nothing. A locked instruction is therefore indivisible and totally ordered with every other guest access, and LOCK changes no result: the locked and unlocked encodings leave identical state. A scheduler that switches guest threads only between instructions keeps this. One that runs guest threads on several host threads does not, and must first make each locked instruction atomic against every access to its operand, with x86's ordering rather than ARM64's weaker one. Nothing here provides or tests that.
- Alignment. A locked operand may have any alignment and may span two mapped regions.
- Compare failure. CMPXCHG compares the accumulator with the destination as CMP would and sets all six status flags from it. On a match it stores the source and leaves the accumulator alone. On a mismatch it loads the destination into the accumulator and stores the destination's old value back, so the destination is written either way and a mismatch on memory that cannot be written still faults. With a 32-bit register destination both stores clear the upper half of that register, following the SDM's operation text.
- Faults. If any byte of the operand cannot be read or written, the instruction faults before memory, registers or flags change.
- Lowering. The DBT runs these forms on the shared semantics. They are not lowered to ARM64 atomic instructions, which could only be judged against concurrent guest threads.
The SDM cases for these forms in tests/cpu_known_answers.rs were written before this implementation, by another contributor. tests/x86_atomic.rs adds the LOCK rules, both compare outcomes at every width, unaligned operands and faults. None of it has been compared with x86 hardware, and nothing here establishes multi-thread behaviour.
Rejected on purpose
LOCK except as described above, F2 or F3 where it is neither PAUSE nor part of a declared form, F2 combined with F3, address-size overrides, 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, the RCP and RSQRT approximations, SSE3 and later, VEX, CMPXCHG8B, CMPXCHG16B and fences. The DBT also refuses raw SYSCALL.