Merge pull request #337 from Thxlx/docs/299-avx-contract

This commit is contained in:
atharvranjan authored and GitHub committed 2026-11-25 10:56:04 -06:00
commit 50321b3f14
4 files changed
+228

No files matched your search

+1
View File
@@ -12,6 +12,7 @@ remains unsupported.
### Added
- Rewrote the README around the six development phases and the first-launchable-application target (#311), with colour-coded Mermaid diagrams (system flow, milestone roadmap, CPU, runtime-service and shader-form charts built from the generated registries), badges, a repository overview, a contributing section and corrected texture-sampling wording. The older X1–X25 capability map is kept in a collapsible section. The diagrams were not render-tested locally.
- Added a fast "Repository hygiene" check (`.github/workflows/hygiene.yml`, `scripts/audit/docs_hygiene.py`). It runs on Ubuntu without a Rust toolchain or network access and fails only on merge-conflict markers, broken relative Markdown links and invalid workflow YAML in tracked files. A separate advisory step warns, without failing, when source or script changes have no changelog entry. A local run over the 626 tracked files found no problems, and planted failures of each kind were caught; it has not yet run on GitHub.
- Documented the proposed guest AVX/AVX2 state and decoding contract and specified an original integer fixture with known results and fault controls (#299).
### Removed
- Removed the "PR template" check (`.github/workflows/pr-template.yml`, `scripts/pr_template_check.py` and its tests) and its CI test step. Also removed `.github/PULL_REQUEST_TEMPLATE.md`. Pull request descriptions are no longer validated or pre-filled; CONTRIBUTING.md still asks for the same information and the claim-first rule still applies.
+1
View File
@@ -38,6 +38,7 @@ Historical phase checkpoints retain their original evidence scope.
- [Guest experiment guide](../experiments/x1/README.md) — Bounded x86-64 execution, executable loading, experimental services, native compute, and offscreen rendering.
- [x86-64 instruction registry](cpu/instruction-registry.md) — Shared decoder, generated tables, semantics, native lowering and how to add a verified instruction form.
- [Proposed AVX and AVX2 guest contract](cpu/avx-contract.md) — VEX decoding, vector state, fault behavior and an original integer fixture specification.
- [Runtime service registry](runtime/registry.md) and [generated coverage](runtime/services.md) — Import resolution, typed Win64 adapters and how to add a tested service.
- [Guest threads](runtime/threads.md) — Deterministic scheduler, thread services, limits and tests.
- [Guest CPU research](research/cpu-boundary.md) — Public specifications and unresolved console requirements.
+136
View File
@@ -0,0 +1,136 @@
# Proposed AVX and AVX2 guest contract
[Issue #299](https://github.com/MacosPorters/betterxen/issues/299) defines a
reviewable first step for Advanced Vector Extensions (AVX) in the x86-64 guest.
The current [instruction registry](instruction-registry.md) rejects VEX prefixes.
[`CpuState`](../../crates/loader/src/cpu.rs) holds 16 XMM registers and MXCSR,
the SIMD floating-point control and status register. The synthetic `CPUID`
advertises basic leaves through 1 and reports no AVX or extended-state support.
The architectural expectations come from the public Intel Software Developer's
Manual and apply to BetterXen's synthetic x86-64 guest profile. Which AVX and
AVX2 features Xbox Series X|S CPUs expose, and which extended register state
the console OS enables for applications, remain open research questions.
Resolving them requires public platform documentation or authorized hardware
evidence.
## Architectural state and encoding
In 64-bit mode, XMM0–XMM15 alias bits 127:0 of YMM0–YMM15. A legacy SSE
instruction that writes an XMM destination preserves bits 255:128. For the
packed VEX.128 forms in this proposal, writing an XMM destination clears that
register's upper 128 bits. A VEX.256 register destination replaces all 256 bits.
A store to memory leaves its source vector register intact. The proposed moves
and integer add leave RFLAGS and MXCSR unchanged. [Intel SDM Vol. 1, §14.8,
Accessing YMM Registers][sdm-ymm]. [Intel SDM Vol. 2B, MOVDQU and PADDD][sdm-2b].
The shared [decoder](../../crates/loader/src/x86_decode.rs) should retain the
prefix family, opcode map, mandatory prefix, operand width and register
extensions until it selects the form. In 64-bit mode the VEX lead byte
identifies the prefix length. The table lists each payload byte's fields
from bit 7 to bit 0.
| Lead byte | Payload byte 1 | Payload byte 2 | Implied fields |
| --- | --- | --- | --- |
| `C5` | `~R ~vvvv L pp` | — | Map `0F`, `W=0`, logical `X=B=0` |
| `C4` | `~R ~X ~B mmmmm` | `W ~vvvv L pp` | Map selected by `mmmmm` |
The tilde indicates an inverted encoding. `R` extends ModRM.reg, while `X`
and `B` extend the applicable index and r/m or base fields. `vvvv` supplies
the first source of `VPADDD`. For `VMOVDQU` its encoded bits must be `1111b`.
`L=0` selects 128 bits and `L=1` selects 256 bits for these forms. The three
standard VEX maps are `00001b` (`0F`), `00010b` (`0F 38`) and `00011b`
(`0F 3A`). `pp` values 0 through 3 represent no mandatory prefix, `66`, `F3`
and `F2`. Both initial opcodes are in `0F`, with `W` ignored (`WIG`). Decode
both valid prefix lengths for them. Reject reserved maps and encodings,
including a physical `LOCK`, `66`, `F2`, `F3` or REX prefix before VEX, through
the existing illegal-instruction path. Legal forms outside the implemented
subset also stop explicitly. Keep the existing register and effective-address
rules and 15-byte fetch limit. [Intel SDM Vol. 2A, §2.3, VEX encoding][sdm-2a].
[Intel SDM Vol. 2B, MOVDQU and PADDD][sdm-2b].
## First implementation increments
1. Append one upper 128-bit half per vector register to `CpuState`, retaining
the existing XMM storage as the low halves. Add layout assertions for the
offsets the dynamic binary translator (DBT) uses. Initialize both halves
in a fresh guest thread and carry them with the thread state when switching
or resuming. Add the shared VEX decoder and encoding controls before execution.
2. Implement `VMOVDQU` (128 and 256 bits, `F3.0F`, `6F /r` and `7F /r`,
register and checked memory forms) and `VPADDD` (128 and 256 bits,
`66.0F`, `FE /r`, register sources initially). The instruction entries
assign both move widths and 128-bit `VPADDD` to AVX, and 256-bit `VPADDD`
to AVX2. Read all sources before publishing the destination, including
when the destination aliases a source. Compare both engines with the
[original fixture specification](../../tests/compat_fixtures/avx/README.md).
3. After shared semantics pass, lower register moves and `VPADDD` to native
ARM64 vector operations on one or two 128-bit halves. Each `VPADDD` half
is four independent 32-bit additions modulo 2³². Use the checked-memory
helper for loads and stores. Check the DBT's fallback counter so a run
through that helper establishes only helper coverage for those forms.
Stage memory-source `VPADDD` and aligned moves separately. Reject masked
moves, gathers, cross-half permutations, floating-point forms, FMA, EVEX
and guest extended-state instructions in the initial slice. AMD64 bytes
remain guest data. All host execution uses native ARM64 code or checked
Rust helpers.
## Memory, faults and instruction budget
`VMOVDQU` permits unaligned operands. Check the complete 16- or 32-byte
guest span before publishing a load result or writing a store. A split
span across adjacent permitted mappings succeeds. A missing or protected
mapping produces the existing guest memory fault at the first inaccessible
byte. For a later `VMOVDQA` form, a memory operand must be aligned to its
width (16 or 32 bytes), with a misaligned operand raising
`GeneralProtection`. [Intel SDM Vol. 2B, MOVDQU and MOVDQA][sdm-2b].
Intel's Type 4 rules for `VMOVDQU` specify `#SS(0)` when a memory reference
through the stack segment (SS) has a noncanonical address and `#GP(0)`
for other noncanonical references. The initial BetterXen profile uses the
[existing 48-bit guest address check](../../crates/loader/src/x86_exec.rs)
and reports `GeneralProtection` for either segment case, including a span
whose first byte is canonical and whose last byte is noncanonical. Check
canonicality before guest memory lookup and before committing the move.
Distinct stack-segment exception reporting remains outside this profile.
[Intel SDM Vol. 2A, Table 2-21, Type 4 exceptions][sdm-2a].
Before adding masked moves, use a separate per-lane access contract. For
`VPMASKMOVD`, only lanes with the high mask bit set may access memory or
report a memory fault. A clear bit zeroes a load lane and leaves a store
lane untouched. A test with lane 4 on an unmapped page must pass with that
lane disabled and fault with it enabled. A full-width span check would
incorrectly reject the disabled case. Specify store commit behavior before
admitting that form. [Intel SDM Vol. 2C, VPMASKMOV][sdm-2c].
Require interpreter and DBT parity for the exit reason, fault address when
reported, RIP, instruction count and complete guest register and memory
state. A rejected move or add has no partial destination update and does
not retire. A successful 256-bit instruction consumes one guest step even
when the DBT emits two halves. With an exhausted budget, stop at the next
guest RIP before decoding or executing that instruction. Check cold and warm
DBT paths and resume in the middle of the same sequence.
## Feature exposure
Run the initial forms only in explicitly enabled synthetic test cases.
Keep normal guest `CPUID` and extended-state behavior at the current baseline.
Before guest-visible AVX support, the guest OS model needs coherent reporting
for `CPUID.1:ECX` bits 26 (XSAVE), 27 (OSXSAVE) and 28 (AVX), and `XGETBV`
with `ECX=0` must report both XMM and YMM state enabled in `XCR0[2:1]`. The
extended-state layout reported by `CPUID.0Dh`, save and restore behavior,
and per-thread ownership must agree. Verify the exception behavior for
disabled state and invalid `XGETBV` selectors before exposing the facility.
AVX2 separately requires `CPUID.(7,0):EBX[5]` and the same OS state. Gate
each advertised feature on its full instruction and state contract,
with probes for enabled and disabled combinations. [Intel SDM Vol. 1,
§14.3, AVX detection][sdm-avx]. [Intel SDM Vol. 1, §14.7.1, AVX2 detection][sdm-avx2].
[Intel SDM Vol. 2A, CPUID][sdm-2a]. [Intel SDM Vol. 2D, XGETBV, XSAVE and XRSTOR][sdm-2d].
[sdm-ymm]: https://cdrdv2-public.intel.com/843827/253665-sdm-vol-1-dec-24.pdf#page=376
[sdm-avx]: https://cdrdv2-public.intel.com/843827/253665-sdm-vol-1-dec-24.pdf#page=359
[sdm-avx2]: https://cdrdv2-public.intel.com/843827/253665-sdm-vol-1-dec-24.pdf#page=375
[sdm-2a]: https://cdrdv2-public.intel.com/929353/253666-093-sdm-vol-2a.pdf
[sdm-2b]: https://cdrdv2-public.intel.com/929354/253667-093-sdm-vol-2b.pdf
[sdm-2c]: https://cdrdv2-public.intel.com/929355/326018-093-sdm-vol-2c.pdf
[sdm-2d]: https://cdrdv2-public.intel.com/929356/334569-093-sdm-vol-2d.pdf
+90
View File
@@ -0,0 +1,90 @@
# Original AVX integer fixture specification
This fixture is a proposed known-answer test for the
[guest AVX contract](../../../docs/cpu/avx-contract.md). It uses only the
source below, fixed register values and synthetic guest memory. Run the
eventual fixture in both guest engines on fresh copies of the same initial
state. Use the independent expected values below for each engine.
## Guest sequence
Set `r8` and `r9` to separate readable ranges containing arrays A and B.
Set `r10` to a writable 96-byte output region initialized to `0xA5` with
mapped sentinel bytes before and after it. Map the code separately as guest
executable memory. Initialize flags, MXCSR and untouched vector registers to
known nonzero values, then record their initial values. The sequence uses no
stack, imports or host calls.
```asm
.intel_syntax noprefix
.text
vmovdqu ymm1, ymmword ptr [r8]
vmovdqu ymm2, ymmword ptr [r9]
vpaddd ymm3, ymm1, ymm2
vmovdqu ymmword ptr [r10], ymm3
movdqu xmm3, xmm1
vmovdqu ymmword ptr [r10 + 32], ymm3
vmovdqu xmm3, xmm2
vmovdqu ymmword ptr [r10 + 64], ymm3
hlt
```
The high base registers require three-byte VEX for the memory moves, while
the low-register `VPADDD` and register `VMOVDQU` can use two-byte VEX.
Store each table row as eight little-endian 32-bit words, with lane 0 at
the lowest address. All entries are hexadecimal.
| Words | Lane 0 | Lane 1 | Lane 2 | Lane 3 | Lane 4 | Lane 5 | Lane 6 | Lane 7 |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
| A | `ffffffff` | `7fffffff` | `80000000` | `00000000` | `00000010` | `fffffffe` | `12345678` | `aaaaaaaa` |
| B | `00000001` | `00000001` | `80000000` | `ffffffff` | `00000020` | `00000003` | `11111111` | `55555556` |
| Output +0 | `00000000` | `80000000` | `00000000` | `ffffffff` | `00000030` | `00000001` | `23456789` | `00000000` |
| Output +32 | `ffffffff` | `7fffffff` | `80000000` | `00000000` | `00000030` | `00000001` | `23456789` | `00000000` |
| Output +64 | `00000001` | `00000001` | `80000000` | `ffffffff` | `00000000` | `00000000` | `00000000` | `00000000` |
For output +0, add corresponding words as unsigned integers and discard
bits above bit 31 in each sum. For example, lane 0 wraps `0x100000000` to
zero, so lane 1 has no incoming carry. At +32, the legacy move replaces the
low four words with A and retains the high four words of the sum. At +64,
the VEX.128 move replaces the low four words with B and clears the high
four. Final YMM1 and YMM2 equal A and B. Final YMM3 equals output +64.
All other vector registers, the input bytes, sentinels, general-purpose
registers, flags and MXCSR retain their initial values. Expect `Halt` with
nine steps, including the terminal `HLT`.
## Controls
| Case | Expected result |
| --- | --- |
| `VPADDD` writes to one of its input registers | Same eight-word sum using both inputs as they stood before the instruction. The other source survives. |
| 128-bit `VPADDD` with seeded destination upper half | Output +0's low four words and four zero words. Both sources survive when distinct from the destination. |
| 128-bit `VMOVDQU` store with sentinels | Exactly the low 16 bytes are stored. The entire source YMM and memory outside the operand survive. |
| 256-bit `VMOVDQU` at a 32-byte-aligned address, at an address offset by one byte, and at `P-16` across adjacent accessible mappings | Same 32 bytes at each address. `P` is the start of the second mapping. |
| Split load at `P-16`, with the second mapping unreadable or unmapped | Memory fault at `P`. Every destination bit retains its pre-instruction value. |
| Split store at `P-16`, with the second mapping read-only or unmapped | Memory fault at `P`. The 16 writable bytes before `P` retain their sentinels. |
| 256-bit `VMOVDQU` load and store at `0x0000_7fff_ffff_fff0` | Map the first 16 bytes read/write and leave the noncanonical half starting at `0x0000_8000_0000_0000` unmapped. Both operations report `GeneralProtection` under the BetterXen profile. The destination register and the mapped bytes retain their original values. |
| 256-bit `VMOVDQU` load from `[rsp]` with `rsp = 0x0000_8000_0000_0000` | The BetterXen profile reports `GeneralProtection` and preserves the destination. Intel's Type 4 rules specify `#SS(0)` for this SS-based operand. |
| Future `VMOVDQA` at aligned and misaligned addresses | An aligned, permitted span transfers the same bytes. A span misaligned by one byte produces `GeneralProtection` and has no data effects. Run at both widths. |
| Future masked dword load from `P-16` | With only lanes 0–3 enabled, load four words and zero lanes 4–7 even if the mapping at `P` is absent. Enabling lane 4 faults at `P`. An all-zero mask reads no lanes and returns an all-zero destination. |
| Future masked dword store to the same split span | With only lanes 0–3 enabled, write those four words; disabled lanes neither access the second mapping nor alter any bytes there when it is mapped. Enabling lane 4 with its mapping absent faults at `P`. |
| Reserved move `vvvv`, reserved VEX map, prohibited prefix before VEX | Illegal instruction at the offending instruction's RIP. No destination or memory update. |
| Equivalent legal `C5` and `C4` register forms, including both `W` values for `WIG` | Equal results and state, with RIP advancement matching each encoding length. Include a high ModRM.reg destination and high `vvvv` source. |
| Budget of two steps | Stop at `VPADDD` (`entry + 0xA` with the encoding shown below) with both inputs loaded, YMM3 and output untouched. Resume with seven steps and reach the same final state as a nine-step run. Repeat with a warm DBT cache. |
For faults, place a completed instruction before the failing instruction.
Assert that only the earlier instruction retires and its effects persist.
Check snapshots of the full destination or nearby memory as appropriate.
## Build provenance
Use native Apple Silicon LLVM tools to assemble the sequence as an x86-64
object, for example with `xcrun clang -arch x86_64 -c avx-fixture.s`.
Verify the resolved Clang and `llvm-objdump` executables support ARM64 before
running them. Record their versions and the output of disassembling the
object. Apple Clang 21.0.0 emits `C4 C1 7E 6F 08` for the first load and
`C5 F5 FE DA` for `VPADDD` at offset `0xA`. Keep generated sources and
objects under ignored `artifacts/`.
The test harness consumes only the object's verified instruction bytes
as guest data. Reserved encodings can be authored separately from Intel's
bit-field tables, since an assembler may reject them. Use source-derived
lane values and checked-memory expectations for the test oracle.