From d1959789030c1ef8647df792089bf419a100812d Mon Sep 17 00:00:00 2001 From: sahib453 <117431595+sahib453@users.noreply.github.com> Date: Wed, 25 Nov 2026 00:25:27 +0530 Subject: [PATCH 1/2] loader: accept MFENCE, LFENCE and SFENCE and document guest memory ordering (#199) Both engines refused the three fences as illegal instructions, and no document said what ordering guest memory accesses have. - instructions.toml, x86_table.rs (generated): the register forms of 0F AE digits 5, 6 and 7. The memory forms stay undeclared. - x86_exec.rs: a fence changes nothing. Each engine runs one guest instruction at a time on one host thread and completes its loads and stores before the next, so accesses are already totally ordered. - docs/cpu/instruction-registry.md: states that ordering, why the fences are no-ops under it, and that it holds only while guest threads do not run on several host threads. - Tests: five single-thread known-answer cases, three assembled fixtures, two rejected memory forms, and MASKMOVDQU as the refused SSE2 instruction in cpu_features.rs now that MFENCE runs. No multi-thread behaviour is established. Signed-off-by: sahib453 <117431595+sahib453@users.noreply.github.com> --- CHANGELOG.md | 1 + crates/loader/src/x86_exec.rs | 4 + crates/loader/src/x86_table.rs | 146 ++++++++++++++--------- crates/loader/tests/cpu_features.rs | 32 ++--- crates/loader/tests/cpu_known_answers.rs | 33 +++++ crates/loader/tests/x86/assemble.py | 2 +- crates/loader/tests/x86/integer.txt | 3 + crates/loader/tests/x86_integer.rs | 5 +- crates/loader/x86/instructions.toml | 33 +++++ docs/cpu/instruction-registry.md | 10 +- 10 files changed, 192 insertions(+), 77 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e686c9c..8dc00b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ remains unsupported. ### Added - Added the VOP3A (`_e64`) encodings of eleven gfx1030 VOP2 integer forms (`v_mul_{i32_i24,hi_i32_i24,u32_u24,hi_u32_u24}`, `v_{min,max}_{i32,u32}`, `v_xnor_b32`, `v_sub_nc_u32`, `v_subrev_nc_u32`) and of the sixteen Wave32 integer compares (explicit SGPR mask destination, `s_cbranch_vccz` through `vcc_lo`, a translation error for reads of runtime-only SGPR results) as semantic-registry encoding variants, with VGPR sources only. New fixtures `vop3-integer` and `vop3-compare` with IR and MSL goldens, and an independent guide-derived oracle (`tests/validation/tests/vop3_{integer,compare}.rs`) that agrees on both reference interpreters and on native Metal. Part of #215. - Added guest texture binding through D3D12-style descriptor tables (#303): shader-visible CBV_SRV_UAV and sampler heaps, `CreateShaderResourceView` for one-level RGBA8 and BC1 2D textures, a point/clamp `CreateSampler`, a root signature with one pixel-stage SRV table and one sampler table, and `SetDescriptorHeaps`/`SetGraphicsRootDescriptorTable` in `crates/loader/src/graphics.rs`, plus `NativeCompute::render_rgba8_sampled_bc1`. Verified locally on an Apple M5 Pro under Metal validation: a guest draw with the translated `image_sample_lz` pixel stage matches the software reference and hand-derived images on 16 of 16 pixels for two RGBA8 textures and 64 of 64 for an 8×8 BC1 texture. A wrong descriptor slot changes the image, off-centre and out-of-range coordinates point-sample with clamping, and a rejected binding, draw shape or resource state never reaches the backend. The software reference samples BC1 with the spec-exact decoder, while Apple's hardware rounds BC1's interpolated palette entries differently (171/84 against 170/85), so the BC1 fixtures use only endpoint colours. Not yet reachable from guest imports. Not yet independently accepted. +- Added MFENCE, LFENCE and SFENCE and documented the ordering of guest memory accesses (part of #199). Both engines used to stop on a fence with an illegal-instruction exit. Each engine runs one guest instruction at a time on one host thread and completes its loads and stores before the next, so accesses are already totally ordered and the fences change nothing; `docs/cpu/instruction-registry.md` states this and that it holds only while guest threads do not run on several host threads. Five single-thread cases in `cpu_known_answers.rs` and three assembled fixtures cover them, and the memory forms that share the opcode stay rejected. SSE2 is still not reported by CPUID because other SSE2 instructions are rejected; the refused SSE2 instruction that `cpu_features.rs` holds is now MASKMOVDQU. No multi-thread behaviour is established, and nothing was compared with x86 hardware; awaiting independent review. - Added four Wave32 gfx1030 VOP2 select and carry forms through the semantic registry. Conditional select, add with carry and both subtract orders with borrow use a per-lane VCC bit and have a guide-derived oracle that passes on both CPU interpreters and on native Metal (local Apple M5, API and shader validation); independent acceptance is pending. Part of #215. - Added decoding and Metal shader translation for twelve Wave32 VOPC 16-bit integer compares (#215). The signed and unsigned forms compare the low 16 bits of each source and write the Wave32 VCC mask. CPU verification covers 1,572 compiler acceptance cases and 408 rejections from the guide-derived oracle, which also passes on native Metal (1,572 cases in 1,420 programs on a local Apple M5 under API and shader validation); acceptance is pending. - Added MOVS and STOS for 8- to 64-bit elements, with and without REP, and the direction flag with CLD and STD (#129). A REP form runs one element per step and stays on the instruction until RCX is zero, so every element counts against the step budget, a repeat can stop part-way and resume, a zero count touches nothing, and a fault keeps the elements already done with RSI, RDI and RCX at the one that faulted. An FS or GS override applies to the MOVS source only. Both engines run these on the shared semantics with no bulk-copy path, so large copies are slow. LODS, CMPS, SCAS, REPE, REPNE and the address-size override stay rejected. Fourteen SDM-derived cases already in `cpu_known_answers.rs` moved from refused to verified, and `tests/cpu_rep.rs` adds a byte-array model over every form, direction and count; not compared with x86 hardware and awaiting independent review. diff --git a/crates/loader/src/x86_exec.rs b/crates/loader/src/x86_exec.rs index 3dfc248..a5ae071 100644 --- a/crates/loader/src/x86_exec.rs +++ b/crates/loader/src/x86_exec.rs @@ -576,6 +576,10 @@ pub fn execute( cpu.gpr[RSP] = sp.wrapping_add(8).wrapping_add(insn.imm); } Op::Nop => {} + // Every load and store of an instruction completes before the next + // instruction starts, on one host thread, so guest accesses are already + // totally ordered and a fence has nothing further to enforce. + Op::Lfence | Op::Mfence | Op::Sfence => {} Op::Cpuid => { // The leaf is EAX and the subleaf ECX. Each result is a 32-bit // register write, so the upper halves are cleared. diff --git a/crates/loader/src/x86_table.rs b/crates/loader/src/x86_table.rs index 6a22e27..286441b 100644 --- a/crates/loader/src/x86_table.rs +++ b/crates/loader/src/x86_table.rs @@ -153,6 +153,9 @@ pub enum Op { Pinsrw, Pextrw, Ldmxcsr, + Lfence, + Mfence, + Sfence, Stmxcsr, Sqrtps, Sqrtpd, @@ -209,7 +212,7 @@ pub enum Op { } impl Op { - pub const ALL: &[Op] = &[Op::Add, Op::Or, Op::Adc, Op::Sbb, Op::And, Op::Sub, Op::Xor, Op::Cmp, Op::Test, Op::Inc, Op::Dec, Op::Neg, Op::Not, Op::Mov, Op::Movzx, Op::Movsx, Op::Movsxd, Op::Lea, Op::Xchg, Op::Xadd, Op::Cmpxchg, Op::Bswap, Op::Cwde, Op::Cdq, Op::Rol, Op::Ror, Op::Shl, Op::Shr, Op::Sar, Op::Mul, Op::Imul, Op::Imul2, Op::Div, Op::Idiv, Op::Cmov, Op::Setcc, Op::Bt, Op::Bts, Op::Btr, Op::Btc, Op::Bsf, Op::Bsr, Op::Popcnt, Op::Tzcnt, Op::Lzcnt, Op::Push, Op::Pop, Op::Leave, Op::Call, Op::Jmp, Op::Jcc, Op::Ret, Op::Nop, Op::Hlt, Op::Syscall, Op::Clc, Op::Stc, Op::Cmc, Op::Cpuid, Op::Cld, Op::Std, Op::Movs, Op::Stos, Op::Movaps, Op::Movups, Op::Movdqa, Op::Movdqu, Op::Movntdq, Op::Movntps, Op::Movss, Op::Movsd, Op::Movq, Op::Movd, Op::Movlps, Op::Movhps, Op::Movmskps, Op::Movmskpd, Op::Pmovmskb, Op::Pand, Op::Pandn, Op::Por, Op::Pxor, Op::Punpcklbw, Op::Punpcklwd, Op::Punpckldq, Op::Packsswb, Op::Pcmpgtb, Op::Pcmpgtw, Op::Pcmpgtd, Op::Packuswb, Op::Punpckhbw, Op::Punpckhwd, Op::Punpckhdq, Op::Packssdw, Op::Punpcklqdq, Op::Punpckhqdq, Op::Pcmpeqb, Op::Pcmpeqw, Op::Pcmpeqd, Op::Paddq, Op::Pmullw, Op::Psubusb, Op::Psubusw, Op::Pminub, Op::Paddusb, Op::Paddusw, Op::Pmaxub, Op::Pavgb, Op::Pavgw, Op::Pmulhuw, Op::Pmulhw, Op::Psubsb, Op::Psubsw, Op::Pminsw, Op::Paddsb, Op::Paddsw, Op::Pmaxsw, Op::Pmuludq, Op::Pmaddwd, Op::Psadbw, Op::Psubb, Op::Psubw, Op::Psubd, Op::Psubq, Op::Paddb, Op::Paddw, Op::Paddd, Op::Psrlw, Op::Psraw, Op::Psllw, Op::Psrld, Op::Psrad, Op::Pslld, Op::Psrlq, Op::Psllq, Op::Psrldq, Op::Pslldq, Op::Pshufd, Op::Pshufhw, Op::Pshuflw, Op::Shufps, Op::Shufpd, Op::Pinsrw, Op::Pextrw, Op::Ldmxcsr, Op::Stmxcsr, Op::Sqrtps, Op::Sqrtpd, Op::Sqrtss, Op::Sqrtsd, Op::Addps, Op::Addpd, Op::Addss, Op::Addsd, Op::Mulps, Op::Mulpd, Op::Mulss, Op::Mulsd, Op::Subps, Op::Subpd, Op::Subss, Op::Subsd, Op::Minps, Op::Minpd, Op::Minss, Op::Minsd, Op::Divps, Op::Divpd, Op::Divss, Op::Divsd, Op::Maxps, Op::Maxpd, Op::Maxss, Op::Maxsd, Op::Cmpps, Op::Cmppd, Op::Cmpss, Op::Cmpsd, Op::Ucomiss, Op::Comiss, Op::Ucomisd, Op::Comisd, Op::Cvtps2pd, Op::Cvtpd2ps, Op::Cvtss2sd, Op::Cvtsd2ss, Op::Cvtdq2ps, Op::Cvtps2dq, Op::Cvttps2dq, Op::Cvttpd2dq, Op::Cvtdq2pd, Op::Cvtpd2dq, Op::Cvtsi2ss, Op::Cvttss2si, Op::Cvtss2si, Op::Cvtsi2sd, Op::Cvttsd2si, Op::Cvtsd2si]; + pub const ALL: &[Op] = &[Op::Add, Op::Or, Op::Adc, Op::Sbb, Op::And, Op::Sub, Op::Xor, Op::Cmp, Op::Test, Op::Inc, Op::Dec, Op::Neg, Op::Not, Op::Mov, Op::Movzx, Op::Movsx, Op::Movsxd, Op::Lea, Op::Xchg, Op::Xadd, Op::Cmpxchg, Op::Bswap, Op::Cwde, Op::Cdq, Op::Rol, Op::Ror, Op::Shl, Op::Shr, Op::Sar, Op::Mul, Op::Imul, Op::Imul2, Op::Div, Op::Idiv, Op::Cmov, Op::Setcc, Op::Bt, Op::Bts, Op::Btr, Op::Btc, Op::Bsf, Op::Bsr, Op::Popcnt, Op::Tzcnt, Op::Lzcnt, Op::Push, Op::Pop, Op::Leave, Op::Call, Op::Jmp, Op::Jcc, Op::Ret, Op::Nop, Op::Hlt, Op::Syscall, Op::Clc, Op::Stc, Op::Cmc, Op::Cpuid, Op::Cld, Op::Std, Op::Movs, Op::Stos, Op::Movaps, Op::Movups, Op::Movdqa, Op::Movdqu, Op::Movntdq, Op::Movntps, Op::Movss, Op::Movsd, Op::Movq, Op::Movd, Op::Movlps, Op::Movhps, Op::Movmskps, Op::Movmskpd, Op::Pmovmskb, Op::Pand, Op::Pandn, Op::Por, Op::Pxor, Op::Punpcklbw, Op::Punpcklwd, Op::Punpckldq, Op::Packsswb, Op::Pcmpgtb, Op::Pcmpgtw, Op::Pcmpgtd, Op::Packuswb, Op::Punpckhbw, Op::Punpckhwd, Op::Punpckhdq, Op::Packssdw, Op::Punpcklqdq, Op::Punpckhqdq, Op::Pcmpeqb, Op::Pcmpeqw, Op::Pcmpeqd, Op::Paddq, Op::Pmullw, Op::Psubusb, Op::Psubusw, Op::Pminub, Op::Paddusb, Op::Paddusw, Op::Pmaxub, Op::Pavgb, Op::Pavgw, Op::Pmulhuw, Op::Pmulhw, Op::Psubsb, Op::Psubsw, Op::Pminsw, Op::Paddsb, Op::Paddsw, Op::Pmaxsw, Op::Pmuludq, Op::Pmaddwd, Op::Psadbw, Op::Psubb, Op::Psubw, Op::Psubd, Op::Psubq, Op::Paddb, Op::Paddw, Op::Paddd, Op::Psrlw, Op::Psraw, Op::Psllw, Op::Psrld, Op::Psrad, Op::Pslld, Op::Psrlq, Op::Psllq, Op::Psrldq, Op::Pslldq, Op::Pshufd, Op::Pshufhw, Op::Pshuflw, Op::Shufps, Op::Shufpd, Op::Pinsrw, Op::Pextrw, Op::Ldmxcsr, Op::Lfence, Op::Mfence, Op::Sfence, Op::Stmxcsr, Op::Sqrtps, Op::Sqrtpd, Op::Sqrtss, Op::Sqrtsd, Op::Addps, Op::Addpd, Op::Addss, Op::Addsd, Op::Mulps, Op::Mulpd, Op::Mulss, Op::Mulsd, Op::Subps, Op::Subpd, Op::Subss, Op::Subsd, Op::Minps, Op::Minpd, Op::Minss, Op::Minsd, Op::Divps, Op::Divpd, Op::Divss, Op::Divsd, Op::Maxps, Op::Maxpd, Op::Maxss, Op::Maxsd, Op::Cmpps, Op::Cmppd, Op::Cmpss, Op::Cmpsd, Op::Ucomiss, Op::Comiss, Op::Ucomisd, Op::Comisd, Op::Cvtps2pd, Op::Cvtpd2ps, Op::Cvtss2sd, Op::Cvtsd2ss, Op::Cvtdq2ps, Op::Cvtps2dq, Op::Cvttps2dq, Op::Cvttpd2dq, Op::Cvtdq2pd, Op::Cvtpd2dq, Op::Cvtsi2ss, Op::Cvttss2si, Op::Cvtss2si, Op::Cvtsi2sd, Op::Cvttsd2si, Op::Cvtsd2si]; /// Lower-case mnemonic stem. pub const fn name(self) -> &'static str { @@ -359,6 +362,9 @@ impl Op { Op::Pinsrw => "pinsrw", Op::Pextrw => "pextrw", Op::Ldmxcsr => "ldmxcsr", + Op::Lfence => "lfence", + Op::Mfence => "mfence", + Op::Sfence => "sfence", Op::Stmxcsr => "stmxcsr", Op::Sqrtps => "sqrtps", Op::Sqrtpd => "sqrtpd", @@ -563,6 +569,9 @@ impl Op { Op::Pinsrw => 0, Op::Pextrw => 0, Op::Ldmxcsr => 0, + Op::Lfence => 0, + Op::Mfence => 0, + Op::Sfence => 0, Op::Stmxcsr => 0, Op::Sqrtps => 0, Op::Sqrtpd => 0, @@ -767,6 +776,9 @@ impl Op { Op::Pinsrw => 0, Op::Pextrw => 0, Op::Ldmxcsr => 0, + Op::Lfence => 0, + Op::Mfence => 0, + Op::Sfence => 0, Op::Stmxcsr => 0, Op::Sqrtps => 0, Op::Sqrtpd => 0, @@ -971,6 +983,9 @@ impl Op { Op::Pinsrw => 0, Op::Pextrw => 0, Op::Ldmxcsr => 0, + Op::Lfence => 0, + Op::Mfence => 0, + Op::Sfence => 0, Op::Stmxcsr => 0, Op::Sqrtps => 0, Op::Sqrtpd => 0, @@ -1175,6 +1190,9 @@ impl Op { Op::Pinsrw => 0, Op::Pextrw => 0, Op::Ldmxcsr => 0, + Op::Lfence => 0, + Op::Mfence => 0, + Op::Sfence => 0, Op::Stmxcsr => 0, Op::Sqrtps => 0, Op::Sqrtpd => 0, @@ -1379,6 +1397,9 @@ impl Op { Op::Pinsrw => false, Op::Pextrw => false, Op::Ldmxcsr => false, + Op::Lfence => false, + Op::Mfence => false, + Op::Sfence => false, Op::Stmxcsr => false, Op::Sqrtps => false, Op::Sqrtpd => false, @@ -1583,6 +1604,9 @@ impl Op { Op::Pinsrw => false, Op::Pextrw => false, Op::Ldmxcsr => false, + Op::Lfence => false, + Op::Mfence => false, + Op::Sfence => false, Op::Stmxcsr => false, Op::Sqrtps => true, Op::Sqrtpd => true, @@ -1787,6 +1811,9 @@ impl Op { Op::Pinsrw => true, Op::Pextrw => true, Op::Ldmxcsr => false, + Op::Lfence => false, + Op::Mfence => false, + Op::Sfence => false, Op::Stmxcsr => false, Op::Sqrtps => true, Op::Sqrtpd => true, @@ -1844,7 +1871,7 @@ impl Op { } } -pub static FORMS: [Form; 369] = [ +pub static FORMS: [Form; 372] = [ Form { op: Op::Movsxd, encoding: Encoding::RegRm, size: OperandSize::Wide, immediate: Immediate::None, source: 4, has_modrm: true, memory_only: false, register_only: false, no_rex_b: false, unaligned: false, lockable: false }, Form { op: Op::Push, encoding: Encoding::OpReg, size: OperandSize::Quad, immediate: Immediate::None, source: 0, has_modrm: false, memory_only: false, register_only: false, no_rex_b: false, unaligned: false, lockable: false }, Form { op: Op::Pop, encoding: Encoding::OpReg, size: OperandSize::Quad, immediate: Immediate::None, source: 0, has_modrm: false, memory_only: false, register_only: false, no_rex_b: false, unaligned: false, lockable: false }, @@ -2040,6 +2067,9 @@ pub static FORMS: [Form; 369] = [ Form { op: Op::Pextrw, encoding: Encoding::RegXmmImm, size: OperandSize::Wide, immediate: Immediate::Byte, source: 16, has_modrm: true, memory_only: false, register_only: true, no_rex_b: false, unaligned: false, lockable: false }, Form { op: Op::Ldmxcsr, encoding: Encoding::Rm, size: OperandSize::None, immediate: Immediate::None, source: 4, has_modrm: true, memory_only: true, register_only: false, no_rex_b: false, unaligned: false, lockable: false }, Form { op: Op::Stmxcsr, encoding: Encoding::Rm, size: OperandSize::None, immediate: Immediate::None, source: 4, has_modrm: true, memory_only: true, register_only: false, no_rex_b: false, unaligned: false, lockable: false }, + Form { op: Op::Lfence, encoding: Encoding::RmUnread, size: OperandSize::None, immediate: Immediate::None, source: 0, has_modrm: true, memory_only: false, register_only: true, no_rex_b: false, unaligned: false, lockable: false }, + Form { op: Op::Mfence, encoding: Encoding::RmUnread, size: OperandSize::None, immediate: Immediate::None, source: 0, has_modrm: true, memory_only: false, register_only: true, no_rex_b: false, unaligned: false, lockable: false }, + Form { op: Op::Sfence, encoding: Encoding::RmUnread, size: OperandSize::None, immediate: Immediate::None, source: 0, has_modrm: true, memory_only: false, register_only: true, no_rex_b: false, unaligned: false, lockable: false }, Form { op: Op::Sqrtps, encoding: Encoding::XmmRm, size: OperandSize::Xmm, immediate: Immediate::None, source: 16, has_modrm: true, memory_only: false, register_only: false, no_rex_b: false, unaligned: false, lockable: false }, Form { op: Op::Sqrtpd, encoding: Encoding::XmmRm, size: OperandSize::Xmm, immediate: Immediate::None, source: 16, has_modrm: true, memory_only: false, register_only: false, no_rex_b: false, unaligned: false, lockable: false }, Form { op: Op::Sqrtss, encoding: Encoding::XmmRm, size: OperandSize::Xmm, immediate: Immediate::None, source: 4, has_modrm: true, memory_only: false, register_only: false, no_rex_b: false, unaligned: false, lockable: false }, @@ -2218,47 +2248,47 @@ pub static FORMS: [Form; 369] = [ /// Form per ModRM digit; 65535 marks an unsupported digit. pub static GROUPS: [[u16; 8]; 22] = [ - [253, 262, 271, 280, 289, 298, 307, 316], - [254, 263, 272, 281, 290, 299, 308, 317], - [255, 264, 273, 282, 291, 300, 309, 318], + [256, 265, 274, 283, 292, 301, 310, 319], + [257, 266, 275, 284, 293, 302, 311, 320], + [258, 267, 276, 285, 294, 303, 312, 321], [6, 65535, 65535, 65535, 65535, 65535, 65535, 65535], - [339, 340, 65535, 65535, 341, 342, 65535, 343], - [344, 345, 65535, 65535, 346, 347, 65535, 348], + [342, 343, 65535, 65535, 344, 345, 65535, 346], + [347, 348, 65535, 65535, 349, 350, 65535, 351], [33, 65535, 65535, 65535, 65535, 65535, 65535, 65535], [34, 65535, 65535, 65535, 65535, 65535, 65535, 65535], - [349, 350, 65535, 65535, 351, 352, 65535, 353], - [354, 355, 65535, 65535, 356, 357, 65535, 358], - [359, 360, 65535, 65535, 361, 362, 65535, 363], - [364, 365, 65535, 65535, 366, 367, 65535, 368], - [18, 65535, 323, 324, 325, 326, 327, 328], - [19, 65535, 329, 330, 331, 332, 333, 334], - [335, 336, 65535, 65535, 65535, 65535, 65535, 65535], - [337, 338, 41, 65535, 42, 65535, 5, 65535], + [352, 353, 65535, 65535, 354, 355, 65535, 356], + [357, 358, 65535, 65535, 359, 360, 65535, 361], + [362, 363, 65535, 65535, 364, 365, 65535, 366], + [367, 368, 65535, 65535, 369, 370, 65535, 371], + [18, 65535, 326, 327, 328, 329, 330, 331], + [19, 65535, 332, 333, 334, 335, 336, 337], + [338, 339, 65535, 65535, 65535, 65535, 65535, 65535], + [340, 341, 41, 65535, 42, 65535, 5, 65535], [28, 65535, 65535, 65535, 65535, 65535, 65535, 65535], - [65535, 65535, 193, 194, 65535, 65535, 65535, 65535], - [65535, 65535, 65535, 65535, 319, 320, 321, 322], + [65535, 65535, 193, 194, 65535, 195, 196, 197], + [65535, 65535, 65535, 65535, 322, 323, 324, 325], [65535, 65535, 165, 65535, 167, 65535, 169, 65535], [65535, 65535, 171, 65535, 173, 65535, 175, 65535], [65535, 65535, 177, 180, 65535, 65535, 179, 181], ]; pub static ONE_BYTE: [Entry; 256] = [ - Entry::Form(247), Entry::Form(248), Entry::Form(249), Entry::Form(250), - Entry::Form(251), Entry::Form(252), Entry::Unsupported, Entry::Unsupported, - Entry::Form(256), Entry::Form(257), Entry::Form(258), Entry::Form(259), - Entry::Form(260), Entry::Form(261), Entry::Unsupported, Entry::Unsupported, - Entry::Form(265), Entry::Form(266), Entry::Form(267), Entry::Form(268), - Entry::Form(269), Entry::Form(270), Entry::Unsupported, Entry::Unsupported, - Entry::Form(274), Entry::Form(275), Entry::Form(276), Entry::Form(277), - Entry::Form(278), Entry::Form(279), Entry::Unsupported, Entry::Unsupported, - Entry::Form(283), Entry::Form(284), Entry::Form(285), Entry::Form(286), - Entry::Form(287), Entry::Form(288), Entry::Unsupported, Entry::Unsupported, - Entry::Form(292), Entry::Form(293), Entry::Form(294), Entry::Form(295), - Entry::Form(296), Entry::Form(297), Entry::Unsupported, Entry::Unsupported, - Entry::Form(301), Entry::Form(302), Entry::Form(303), Entry::Form(304), - Entry::Form(305), Entry::Form(306), Entry::Unsupported, Entry::Unsupported, - Entry::Form(310), Entry::Form(311), Entry::Form(312), Entry::Form(313), - Entry::Form(314), Entry::Form(315), Entry::Unsupported, Entry::Unsupported, + Entry::Form(250), Entry::Form(251), Entry::Form(252), Entry::Form(253), + Entry::Form(254), Entry::Form(255), Entry::Unsupported, Entry::Unsupported, + Entry::Form(259), Entry::Form(260), Entry::Form(261), Entry::Form(262), + Entry::Form(263), Entry::Form(264), Entry::Unsupported, Entry::Unsupported, + Entry::Form(268), Entry::Form(269), Entry::Form(270), Entry::Form(271), + Entry::Form(272), Entry::Form(273), Entry::Unsupported, Entry::Unsupported, + Entry::Form(277), Entry::Form(278), Entry::Form(279), Entry::Form(280), + Entry::Form(281), Entry::Form(282), Entry::Unsupported, Entry::Unsupported, + Entry::Form(286), Entry::Form(287), Entry::Form(288), Entry::Form(289), + Entry::Form(290), Entry::Form(291), Entry::Unsupported, Entry::Unsupported, + Entry::Form(295), Entry::Form(296), Entry::Form(297), Entry::Form(298), + Entry::Form(299), Entry::Form(300), Entry::Unsupported, Entry::Unsupported, + Entry::Form(304), Entry::Form(305), Entry::Form(306), Entry::Form(307), + Entry::Form(308), Entry::Form(309), Entry::Unsupported, Entry::Unsupported, + Entry::Form(313), Entry::Form(314), Entry::Form(315), Entry::Form(316), + Entry::Form(317), Entry::Form(318), Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, @@ -2321,7 +2351,7 @@ pub static TWO_BYTE: [Entry; 256] = [ Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Form(73), Entry::Form(74), Entry::Unsupported, Entry::Form(86), - Entry::Unsupported, Entry::Unsupported, Entry::Form(227), Entry::Form(228), + Entry::Unsupported, Entry::Unsupported, Entry::Form(230), Entry::Form(231), Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, @@ -2330,10 +2360,10 @@ pub static TWO_BYTE: [Entry; 256] = [ Entry::Form(12), Entry::Form(12), Entry::Form(12), Entry::Form(12), Entry::Form(12), Entry::Form(12), Entry::Form(12), Entry::Form(12), Entry::Form(12), Entry::Form(12), Entry::Form(12), Entry::Form(12), - Entry::Form(104), Entry::Form(195), Entry::Unsupported, Entry::Unsupported, + Entry::Form(104), Entry::Form(198), Entry::Unsupported, Entry::Unsupported, Entry::Form(108), Entry::Form(111), Entry::Form(114), Entry::Form(117), - Entry::Form(199), Entry::Form(203), Entry::Form(231), Entry::Form(235), - Entry::Form(207), Entry::Form(211), Entry::Form(215), Entry::Form(219), + Entry::Form(202), Entry::Form(206), Entry::Form(234), Entry::Form(238), + Entry::Form(210), Entry::Form(214), Entry::Form(218), Entry::Form(222), Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, @@ -2358,7 +2388,7 @@ pub static TWO_BYTE: [Entry; 256] = [ Entry::Unsupported, Entry::Unsupported, Entry::Form(55), Entry::Form(56), Entry::Unsupported, Entry::Unsupported, Entry::Group(18), Entry::Form(68), Entry::Form(60), Entry::Form(61), Entry::Form(57), Entry::Form(58), - Entry::Form(71), Entry::Form(72), Entry::Form(223), Entry::Unsupported, + Entry::Form(71), Entry::Form(72), Entry::Form(226), Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Form(185), Entry::Unsupported, Entry::Form(59), Entry::Form(59), Entry::Form(59), Entry::Form(59), Entry::Form(59), Entry::Form(59), Entry::Form(59), Entry::Form(59), @@ -2388,7 +2418,7 @@ pub static TWO_BYTE_66: [Entry; 256] = [ Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Form(75), Entry::Form(76), Entry::Unsupported, Entry::Form(87), - Entry::Unsupported, Entry::Unsupported, Entry::Form(229), Entry::Form(230), + Entry::Unsupported, Entry::Unsupported, Entry::Form(232), Entry::Form(233), Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, @@ -2397,10 +2427,10 @@ pub static TWO_BYTE_66: [Entry; 256] = [ Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, - Entry::Form(105), Entry::Form(196), Entry::Unsupported, Entry::Unsupported, + Entry::Form(105), Entry::Form(199), Entry::Unsupported, Entry::Unsupported, Entry::Form(109), Entry::Form(112), Entry::Form(115), Entry::Form(118), - Entry::Form(200), Entry::Form(204), Entry::Form(232), Entry::Form(236), - Entry::Form(208), Entry::Form(212), Entry::Form(216), Entry::Form(220), + Entry::Form(203), Entry::Form(207), Entry::Form(235), Entry::Form(239), + Entry::Form(211), Entry::Form(215), Entry::Form(219), Entry::Form(223), Entry::Form(119), Entry::Form(120), Entry::Form(121), Entry::Form(122), Entry::Form(123), Entry::Form(124), Entry::Form(125), Entry::Form(126), Entry::Form(127), Entry::Form(128), Entry::Form(129), Entry::Form(130), @@ -2425,7 +2455,7 @@ pub static TWO_BYTE_66: [Entry; 256] = [ Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, - Entry::Unsupported, Entry::Unsupported, Entry::Form(224), Entry::Unsupported, + Entry::Unsupported, Entry::Unsupported, Entry::Form(227), Entry::Unsupported, Entry::Form(191), Entry::Form(192), Entry::Form(186), Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, @@ -2434,7 +2464,7 @@ pub static TWO_BYTE_66: [Entry; 256] = [ Entry::Form(138), Entry::Form(139), Entry::Form(140), Entry::Form(107), Entry::Form(141), Entry::Form(142), Entry::Form(143), Entry::Form(110), Entry::Form(144), Entry::Form(166), Entry::Form(172), Entry::Form(145), - Entry::Form(146), Entry::Form(147), Entry::Form(238), Entry::Form(85), + Entry::Form(146), Entry::Form(147), Entry::Form(241), Entry::Form(85), Entry::Form(148), Entry::Form(149), Entry::Form(150), Entry::Form(113), Entry::Form(151), Entry::Form(152), Entry::Form(153), Entry::Form(116), Entry::Unsupported, Entry::Form(168), Entry::Form(174), Entry::Form(178), @@ -2454,8 +2484,8 @@ pub static TWO_BYTE_F3: [Entry; 256] = [ Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, - Entry::Unsupported, Entry::Unsupported, Entry::Form(241), Entry::Unsupported, - Entry::Form(242), Entry::Form(243), Entry::Unsupported, Entry::Unsupported, + Entry::Unsupported, Entry::Unsupported, Entry::Form(244), Entry::Unsupported, + Entry::Form(245), Entry::Form(246), Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, @@ -2464,10 +2494,10 @@ pub static TWO_BYTE_F3: [Entry; 256] = [ Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, - Entry::Unsupported, Entry::Form(197), Entry::Unsupported, Entry::Unsupported, + Entry::Unsupported, Entry::Form(200), Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, - Entry::Form(201), Entry::Form(205), Entry::Form(233), Entry::Form(237), - Entry::Form(209), Entry::Form(213), Entry::Form(217), Entry::Form(221), + Entry::Form(204), Entry::Form(208), Entry::Form(236), Entry::Form(240), + Entry::Form(212), Entry::Form(216), Entry::Form(220), Entry::Form(224), Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, @@ -2492,7 +2522,7 @@ pub static TWO_BYTE_F3: [Entry; 256] = [ Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Form(62), Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Form(63), Entry::Form(64), Entry::Unsupported, Entry::Unsupported, - Entry::Unsupported, Entry::Unsupported, Entry::Form(225), Entry::Unsupported, + Entry::Unsupported, Entry::Unsupported, Entry::Form(228), Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, @@ -2501,7 +2531,7 @@ pub static TWO_BYTE_F3: [Entry; 256] = [ Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, - Entry::Unsupported, Entry::Unsupported, Entry::Form(239), Entry::Unsupported, + Entry::Unsupported, Entry::Unsupported, Entry::Form(242), Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, @@ -2521,8 +2551,8 @@ pub static TWO_BYTE_F2: [Entry; 256] = [ Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, - Entry::Unsupported, Entry::Unsupported, Entry::Form(244), Entry::Unsupported, - Entry::Form(245), Entry::Form(246), Entry::Unsupported, Entry::Unsupported, + Entry::Unsupported, Entry::Unsupported, Entry::Form(247), Entry::Unsupported, + Entry::Form(248), Entry::Form(249), Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, @@ -2531,10 +2561,10 @@ pub static TWO_BYTE_F2: [Entry; 256] = [ Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, - Entry::Unsupported, Entry::Form(198), Entry::Unsupported, Entry::Unsupported, + Entry::Unsupported, Entry::Form(201), Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, - Entry::Form(202), Entry::Form(206), Entry::Form(234), Entry::Unsupported, - Entry::Form(210), Entry::Form(214), Entry::Form(218), Entry::Form(222), + Entry::Form(205), Entry::Form(209), Entry::Form(237), Entry::Unsupported, + Entry::Form(213), Entry::Form(217), Entry::Form(221), Entry::Form(225), Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, @@ -2559,7 +2589,7 @@ pub static TWO_BYTE_F2: [Entry; 256] = [ Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, - Entry::Unsupported, Entry::Unsupported, Entry::Form(226), Entry::Unsupported, + Entry::Unsupported, Entry::Unsupported, Entry::Form(229), Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, @@ -2568,7 +2598,7 @@ pub static TWO_BYTE_F2: [Entry; 256] = [ Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, - Entry::Unsupported, Entry::Unsupported, Entry::Form(240), Entry::Unsupported, + Entry::Unsupported, Entry::Unsupported, Entry::Form(243), Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, Entry::Unsupported, diff --git a/crates/loader/tests/cpu_features.rs b/crates/loader/tests/cpu_features.rs index 38cb5bf..c381eda 100644 --- a/crates/loader/tests/cpu_features.rs +++ b/crates/loader/tests/cpu_features.rs @@ -199,15 +199,15 @@ fn reported_features_have_all_their_instructions_in_the_fixtures() { fn unreported_features_each_have_an_instruction_that_is_refused() { // (feature, leaf, result register, bit, an instruction of that feature) let witnesses: &[(&str, u32, usize, u32, &[u8])] = &[ - ("FPU", 1, EDX, 0, &[0xD9, 0xE8]), // fld1 - ("TSC", 1, EDX, 4, &[0x0F, 0x31]), // rdtsc - ("CX8", 1, EDX, 8, &[0x0F, 0xC7, 0x0E]), // cmpxchg8b [rsi] - ("CLFSH", 1, EDX, 19, &[0x0F, 0xAE, 0x3E]), // clflush [rsi] - ("MMX", 1, EDX, 23, &[0x0F, 0x77]), // emms - ("FXSR", 1, EDX, 24, &[0x0F, 0xAE, 0x06]), // fxsave [rsi] - ("SSE", 1, EDX, 25, &[0x0F, 0x52, 0xC1]), // rsqrtps xmm0, xmm1 - ("SSE2", 1, EDX, 26, &[0x0F, 0xAE, 0xF0]), // mfence - ("SSE3", 1, ECX, 0, &[0xF2, 0x0F, 0x7C, 0xC1]), // haddps xmm0, xmm1 + ("FPU", 1, EDX, 0, &[0xD9, 0xE8]), // fld1 + ("TSC", 1, EDX, 4, &[0x0F, 0x31]), // rdtsc + ("CX8", 1, EDX, 8, &[0x0F, 0xC7, 0x0E]), // cmpxchg8b [rsi] + ("CLFSH", 1, EDX, 19, &[0x0F, 0xAE, 0x3E]), // clflush [rsi] + ("MMX", 1, EDX, 23, &[0x0F, 0x77]), // emms + ("FXSR", 1, EDX, 24, &[0x0F, 0xAE, 0x06]), // fxsave [rsi] + ("SSE", 1, EDX, 25, &[0x0F, 0x52, 0xC1]), // rsqrtps xmm0, xmm1 + ("SSE2", 1, EDX, 26, &[0x66, 0x0F, 0xF7, 0xC1]), // maskmovdqu xmm0, xmm1 + ("SSE3", 1, ECX, 0, &[0xF2, 0x0F, 0x7C, 0xC1]), // haddps xmm0, xmm1 ( "PCLMULQDQ", 1, @@ -222,17 +222,17 @@ fn unreported_features_each_have_an_instruction_that_is_refused() { ("SSE4.2", 1, ECX, 20, &[0xF2, 0x0F, 0x38, 0xF1, 0xC1]), // crc32 eax, ecx ("MOVBE", 1, ECX, 22, &[0x0F, 0x38, 0xF0, 0x06]), // movbe eax, [rsi] ("AESNI", 1, ECX, 25, &[0x66, 0x0F, 0x38, 0xDC, 0xC1]), // aesenc xmm0, xmm1 - ("XSAVE", 1, ECX, 26, &[0x0F, 0x01, 0xD0]), // xgetbv - ("AVX", 1, ECX, 28, &[0xC5, 0xF4, 0x58, 0xC2]), // vaddps ymm0, ymm1, ymm2 + ("XSAVE", 1, ECX, 26, &[0x0F, 0x01, 0xD0]), // xgetbv + ("AVX", 1, ECX, 28, &[0xC5, 0xF4, 0x58, 0xC2]), // vaddps ymm0, ymm1, ymm2 ("F16C", 1, ECX, 29, &[0xC4, 0xE2, 0x79, 0x13, 0xC1]), // vcvtph2ps xmm0, xmm1 - ("RDRAND", 1, ECX, 30, &[0x0F, 0xC7, 0xF0]), // rdrand eax + ("RDRAND", 1, ECX, 30, &[0x0F, 0xC7, 0xF0]), // rdrand eax ("BMI1", 7, EBX, 3, &[0xC4, 0xE2, 0x70, 0xF2, 0xC2]), // andn eax, ecx, edx - ("AVX2", 7, EBX, 5, &[0xC5, 0xF5, 0xFE, 0xC2]), // vpaddd ymm0, ymm1, ymm2 + ("AVX2", 7, EBX, 5, &[0xC5, 0xF5, 0xFE, 0xC2]), // vpaddd ymm0, ymm1, ymm2 ("BMI2", 7, EBX, 8, &[0xC4, 0xE2, 0x73, 0xF6, 0xC2]), // mulx eax, ecx, edx - ("RDSEED", 7, EBX, 18, &[0x0F, 0xC7, 0xF8]), // rdseed eax + ("RDSEED", 7, EBX, 18, &[0x0F, 0xC7, 0xF8]), // rdseed eax ("ADX", 7, EBX, 19, &[0x66, 0x0F, 0x38, 0xF6, 0xC1]), // adcx eax, ecx - ("SHA", 7, EBX, 29, &[0x0F, 0x38, 0xC9, 0xC1]), // sha1msg1 xmm0, xmm1 - ("LAHF/SAHF", 0x8000_0001, ECX, 0, &[0x9F]), // lahf + ("SHA", 7, EBX, 29, &[0x0F, 0x38, 0xC9, 0xC1]), // sha1msg1 xmm0, xmm1 + ("LAHF/SAHF", 0x8000_0001, ECX, 0, &[0x9F]), // lahf ("PREFETCHW", 0x8000_0001, ECX, 8, &[0x0F, 0x0D, 0x0E]), // prefetchw [rsi] ("SYSCALL/SYSRET", 0x8000_0001, EDX, 11, &[0x48, 0x0F, 0x07]), // sysretq ("RDTSCP", 0x8000_0001, EDX, 27, &[0x0F, 0x01, 0xF9]), // rdtscp diff --git a/crates/loader/tests/cpu_known_answers.rs b/crates/loader/tests/cpu_known_answers.rs index 7a96f2b..833e6d9 100644 --- a/crates/loader/tests/cpu_known_answers.rs +++ b/crates/loader/tests/cpu_known_answers.rs @@ -1353,6 +1353,33 @@ mov_moffs8_store: rax=77 m0=00000000 => m0=00000077 f=* thirteen_byte_fs_add_imm32: rax=10 rbx=8 fs120=0000000000000000 => fs120=4433221100000000 f=cPazso "; +/// Single-thread baseline for the fences (#199). Each engine completes every +/// load and store of an instruction before the next instruction starts, so the +/// cases only require that a fence changes nothing and that accesses on either +/// side of it happen in program order. Nothing here involves a second thread. +const FENCES: &str = r" +@ mov [rdi], rax; mfence; mov rbx, [rdi] + 488907 0faef0 488b1f + SDM 2B MFENCE +mfence_store_is_visible_to_a_later_load: rdi=D rax=1122334455667788 m0=0000000000000000 => rbx=1122334455667788 m0=8877665544332211 f=* +@ mov [rdi], rax; sfence; mov rbx, [rdi+8] + 488907 0faef8 488b5f08 + SDM 2B SFENCE +sfence_after_a_store_keeps_other_memory: rdi=D rax=1 m0=0000000000000000 m8=aabbccddeeff0011 => rbx=1100ffeeddccbbaa m0=0100000000000000 f=* +@ mov rbx, [rdi]; lfence; mov [rdi], rax + 488b1f 0faee8 488907 + SDM 2B LFENCE +lfence_load_precedes_a_later_store: rdi=D rax=5 m0=0200000000000000 => rbx=2 m0=0500000000000000 f=* +@ mfence; lfence; sfence + 0faef0 0faee8 0faef8 + SDM 2B MFENCE; 2B LFENCE; 2B SFENCE +fences_change_no_register_or_flag: rax=1122334455667788 rbx=99aabbccddeeff00 => f=* +@ .byte 0x0f,0xae,0xf5; .byte 0x0f,0xae,0xeb; .byte 0x0f,0xae,0xff + 0faef5 0faeeb 0faeff + SDM 2B MFENCE; 2B LFENCE; 2B SFENCE (the r/m field is ignored) +fences_ignore_the_rm_field: rax=1122334455667788 => f=* +"; + const MEMORY_FAULTS: &str = r" @ mov r8d, 1; add al, bl; mov rcx, [rsi]; mov r9d, 2 41b801000000 00d8 !488b0e 41b902000000 @@ -2631,6 +2658,7 @@ const GROUPS: &[(&str, &str)] = &[ ("strings", STRINGS), ("fs_gs", FS_GS), ("decoding", DECODING), + ("fences", FENCES), ("memory_faults", MEMORY_FAULTS), ( "defect_straddling_fault_address", @@ -3434,6 +3462,11 @@ fn decoding() { check_group(DECODING); } +#[test] +fn fences() { + check_group(FENCES); +} + #[test] fn memory_faults() { check_group(MEMORY_FAULTS); diff --git a/crates/loader/tests/x86/assemble.py b/crates/loader/tests/x86/assemble.py index 3d55e8f..3a58c36 100644 --- a/crates/loader/tests/x86/assemble.py +++ b/crates/loader/tests/x86/assemble.py @@ -96,7 +96,7 @@ def cases(): out += ["mov ah, bl", "mov dl, ch", "mov bh, 0x21", "xchg ah, bl", "mov sil, dil", "movabs r15, 0x8877665544332211", "mov rcx, -2", "bswap ebx", "bswap r9", "cbw", "cwde", "cdqe", "cwd", "cdq", "cqo", "clc", "stc", "cmc", "nop", "cpuid", - "nop dword ptr [rax + 8]", "pause", "push r9", "push rbx", "push -5", "push 0x12345678", + "nop dword ptr [rax + 8]", "pause", "lfence", "mfence", "sfence", "push r9", "push rbx", "push -5", "push 0x12345678", f"push {mem(64)}", "pop rbx", "pop r12", f"pop {mem(64)}", "leave"] return list(dict.fromkeys(out)) diff --git a/crates/loader/tests/x86/integer.txt b/crates/loader/tests/x86/integer.txt index c4c34ab..a71fb7c 100644 --- a/crates/loader/tests/x86/integer.txt +++ b/crates/loader/tests/x86/integer.txt @@ -1037,6 +1037,9 @@ f5 | cmc 0f a2 | cpuid 0f 1f 40 08 | nop dword ptr [rax + 8] f3 90 | pause +0f ae e8 | lfence +0f ae f0 | mfence +0f ae f8 | sfence 41 51 | push r9 53 | push rbx 6a fb | push -5 diff --git a/crates/loader/tests/x86_integer.rs b/crates/loader/tests/x86_integer.rs index 4b2765e..3637062 100644 --- a/crates/loader/tests/x86_integer.rs +++ b/crates/loader/tests/x86_integer.rs @@ -728,7 +728,8 @@ fn reference(m: &mut Machine, text: &str) -> Outcome { m.flags.cf = !m.flags.cf; Outcome::Done(none) } - "nop" | "pause" => Outcome::Done(none), + // The fences order memory accesses and compute nothing. + "nop" | "pause" | "lfence" | "mfence" | "sfence" => Outcome::Done(none), "cpuid" => { // Xenora's guest CPUID contract, restated: the vendor "Xenora guest" // and the four features whose every instruction exists, CMOV and @@ -1224,6 +1225,8 @@ fn undeclared_and_ambiguous_encodings_are_rejected() { &[0x66, 0x63, 0xC1], // 16-bit movsxd &[0x41, 0x90], // xchg r8, rax &[0x8D, 0xC0], // lea with a register operand + &[0x0F, 0xAE, 0x2E], // 0F AE /5 with a memory operand + &[0x0F, 0xAE, 0x36], // 0F AE /6 with a memory operand &[0x0F, 0xA3, 0x06], // bt [rsi], eax &[0xC0, 0xD0, 0x01], // rcl al, 1 &[0xC6, 0xC8, 0x01], // C6 /1 diff --git a/crates/loader/x86/instructions.toml b/crates/loader/x86/instructions.toml index fed1317..2f7c9d7 100644 --- a/crates/loader/x86/instructions.toml +++ b/crates/loader/x86/instructions.toml @@ -183,6 +183,9 @@ shufpd = { ref = "sdm SHUFPD" } pinsrw = { ref = "sdm PINSRW" } pextrw = { ref = "sdm PEXTRW" } ldmxcsr = { ref = "sdm LDMXCSR" } +lfence = { ref = "sdm LFENCE" } +mfence = { ref = "sdm MFENCE" } +sfence = { ref = "sdm SFENCE" } stmxcsr = { ref = "sdm STMXCSR" } # Floating point. Every form can raise a SIMD floating-point exception. @@ -1920,6 +1923,36 @@ size = "n" src = "d" memory_only = true +# The fences are the register forms of digits 5 to 7; the r/m field is ignored. +# The memory forms of those digits (the XSAVE family and CLFLUSH) are not +# declared. +[[form]] +op = "lfence" +map = "0f" +opcode = 0xAE +digit = 5 +enc = "rm_unread" +size = "n" +register_only = true + +[[form]] +op = "mfence" +map = "0f" +opcode = 0xAE +digit = 6 +enc = "rm_unread" +size = "n" +register_only = true + +[[form]] +op = "sfence" +map = "0f" +opcode = 0xAE +digit = 7 +enc = "rm_unread" +size = "n" +register_only = true + # SSE and SSE2 floating-point forms. RCP and RSQRT approximations are not # architecturally exact and are left out. diff --git a/docs/cpu/instruction-registry.md b/docs/cpu/instruction-registry.md index 30868c6..9960fca 100644 --- a/docs/cpu/instruction-registry.md +++ b/docs/cpu/instruction-registry.md @@ -59,6 +59,14 @@ These rules hold for both engines and are what a guest scheduler has to preserve 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. +## Memory ordering and fences + +Guest memory accesses are totally ordered today. Each engine runs one guest instruction at a time on one host thread, and every load and store of an instruction completes before the next instruction starts. There is no store buffer and nothing is reordered, so a load always sees the latest earlier store. That is stronger than the ordering the Intel SDM (Vol. 3A, "Memory Ordering") gives a real processor, where a store may sit in a buffer while later loads from other locations run. + +MFENCE, LFENCE and SFENCE (`0F AE` with a register ModRM byte and digit 6, 5 or 7; the SDM states that the r/m field is ignored) are therefore accepted and change no register, flag or memory. They have nothing left to enforce. Non-temporal stores are ordinary stores, so SFENCE has no weakly ordered store to wait for either. The memory forms that share those digits, the XSAVE family and CLFLUSH, stay rejected. + +This holds only while guest threads do not run on several host threads. ARM64 orders memory more weakly than x86, so an engine that ran guest threads in parallel would have to restore x86's ordering for ordinary accesses and give the fences a real effect. Nothing here provides or tests that. `tests/cpu_known_answers.rs` holds the single-thread cases: a store, a fence and a later load in program order, and no change to any other state. None of it has been compared with x86 hardware. + ## String instructions MOVS and STOS exist for 8-, 16-, 32- and 64-bit elements, alone and with REP (F3), together with the direction flag and CLD and STD. `CpuState::df` holds the flag. It is not one of the six status flags the registry tracks. @@ -85,7 +93,7 @@ CPUID (`0F A2`) returns a fixed table that never reads the host. It describes a A feature bit is set only when every instruction of that feature is implemented. That is stricter than "some of it works", and it has consequences: -- SSE and SSE2 are not reported, although most of their instructions exist, because parts are still rejected: RSQRTPS and MFENCE, for example. BMI1 is not reported although TZCNT exists, because ANDN does not. The x87 FPU, MMX, FXSAVE, CMPXCHG8B, the time-stamp counter, LAHF and SYSRET are absent too. +- SSE and SSE2 are not reported, although most of their instructions exist, because parts are still rejected: RSQRTPS and MASKMOVDQU, for example. BMI1 is not reported although TZCNT exists, because ANDN does not. The x87 FPU, MMX, FXSAVE, CMPXCHG8B, the time-stamp counter, LAHF and SYSRET are absent too. - The table is therefore truthful about Xenora, not plausible as hardware: every real 64-bit processor reports the FPU and SSE2. A guest that tests a bit before using a feature avoids it; a guest that assumes SSE2 because it runs in 64-bit mode still gets every form that exists and an illegal-instruction exit for one that does not. - A leaf that is not listed returns zeros, whatever the subleaf. Intel processors answer a leaf above the highest basic one with that leaf's data instead; software is expected to read the highest leaf first. From 94783fa60310b510e64b6e412c2141250f89cf1c Mon Sep 17 00:00:00 2001 From: sahib453 <117431595+sahib453@users.noreply.github.com> Date: Wed, 25 Nov 2026 00:44:40 +0530 Subject: [PATCH 2/2] docs: take the fences off the rejected-on-purpose list (#199) The instruction registry document listed fences among the forms rejected on purpose. This branch accepts them and explains why in the new memory-ordering section, so the list no longer names them. Signed-off-by: sahib453 <117431595+sahib453@users.noreply.github.com> --- docs/cpu/instruction-registry.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/cpu/instruction-registry.md b/docs/cpu/instruction-registry.md index 9960fca..d246cb2 100644 --- a/docs/cpu/instruction-registry.md +++ b/docs/cpu/instruction-registry.md @@ -103,4 +103,4 @@ A feature bit is set only when every instruction of that feature is implemented. ## 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, LODS, CMPS, SCAS, REPE and REPNE, MMX, the RCP and RSQRT approximations, SSE3 and later, VEX, CMPXCHG8B, CMPXCHG16B and fences. The DBT also refuses raw SYSCALL. +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, LODS, CMPS, SCAS, REPE and REPNE, MMX, the RCP and RSQRT approximations, SSE3 and later, VEX, CMPXCHG8B and CMPXCHG16B. The DBT also refuses raw SYSCALL.