feat(relinker): lower MONITORX and MWAITX for --to-intel #201

Merged
ljkx merged 1 commit from upstream-pr/201 into main 2026-10-01 09:50:04 +00:00
ljkx commented 2026-10-01 00:29:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ljkx on 2026-10-01T00:29:18Z.


What

Addresses part of the --to-intel item in docs/dev/TechnicalDebt.md: a MONITORX (0F 01 FA) or MWAITX (0F 01 FB) anywhere in a converted segment failed the whole relink. Since #151, guest modules (a title's own sce_module PRX such as libc and libSceJobManager) go through the same conversion. No title is currently known to contain these instructions.

Both are now replaced in place, at the same length, by the matcher:

  • MONITORX → a NOP of the instruction's length.
  • MWAITX → PAUSE (F3 90) followed by NOPs up to the instruction's length.

Why this is exact rather than an approximation, from the AMD APM Vol. 3 (rev 3.36, MWAITX):

  • "The MWAITX instruction has the same effect on architectural state as the NOP instruction."
  • Its exit events include "Any unmasked interrupt", so it can return at any time, and its timer (EBX, enabled by ECX[1]) is only an upper bound.
  • "There is no indication after exiting MWAITX of why the processor exited or if the timer expired. It is up to software to check whether the awaiting store has occurred."

Correct callers therefore loop and re-check, as the Linux kernel's delay_halt does around MWAITX ("these functions may return earlier than requested"). The replacement returns at once every time, which real hardware also does. On a Ryzen 7 7800X3D, MWAITX without an armed monitor and with a 2·10⁹-cycle timer returned after ~900 cycles. rFLAGS: none for either instruction.

Limits:

  • Faults not reproduced: the faults these raise only for invalid use are no longer raised. For MONITORX that is #GP for ECX ≠ 0, non-canonical addresses, and the page and segment checks of a 1-byte read. For MWAITX it is #GP for reserved ECX bits.
  • Invalid encodings still fail: forms with a LOCK prefix (#UD) or longer than 15 bytes (#GP) still fail the relink instead of being replaced.
  • Branches into a prefix run: like the existing in-place lowerings, the converter does not check branch targets inside a replaced instruction, so a jump into the middle of a prefix run would land inside the NOP padding.
  • Cost: a busy spin with PAUSE instead of a low-power wait, which costs power and SMT throughput.

CLZERO, RDPRU and MCOMMIT still fail the relink:

  • CLZERO can be lowered exactly: four MOVNTDQ of zero to the aligned 64-byte line match its non-temporal, weakly ordered store. It needs an out-of-line stub, so it is left for a follow-up PR using the stub builder from #200.
  • RDPRU (APERF/MPERF) and MCOMMIT have no exact equivalent.

TechnicalDebt.md: the item now lists CLZERO/RDPRU/MCOMMIT.

Rebased onto main after #200; the conflicts with it in the matcher, its tests and the TechnicalDebt.md line were resolved by keeping both changes.

Tested

  • Relinker test suite (14 tests, on current main including #200) passes on Windows 11 (Clang 23, and MinGW-w64 GCC 16.2 with static linking) and on Ubuntu 24.04 under WSL (GCC 13.3).
  • amd64_only_converter:
    • Replacements: MONITORX → 0F 1F 00, MWAITX → F3 90 90, and the prefixed forms 67 0F 01 FA → 0F 1F 40 00 and 2E 41 0F 01 FB → F3 90 0F 1F 00.
    • Padding with two NOPs: an 8-byte MONITORX and a 15-byte MWAITX.
    • Still unsupported: LOCK MWAITX, a 16-byte MONITORX, and CLZERO.
    • Converter: a segment with MONITORX, MWAITX and a prefixed MWAITX is rewritten in place with the expected bytes and reports.
    • The tests that used MONITORX as the example of an unsupported instruction now use CLZERO.
  • Mutation check: wrong PAUSE bytes, an off-by-one in the NOP padding, a removed LOCK check, and a 16-byte limit each make the test fail.
  • Running the real converter over 406 prefix/REX combinations of lengths 3–17 (segment, address-size and LOCK prefixes, with and without REX.W) gives replacements of exactly the original length that end on an instruction boundary for every valid form, and Unsupported for every LOCK or over-length form.
  • Hardware (Zen 4, AMD only; nothing was run on Intel):
    • MWAITX without an armed monitor returns at once.
    • With an armed monitor and a 10⁵-cycle timer it returned after 17k–64k cycles.
    • A store from another thread wakes it.
    • A monitorx/mwaitx spin-wait loop whose flag is set by another thread after 50 ms finishes after ~61 ms both as written and with the replacement bytes (about 5k vs 3.8M iterations).
  • No title tested.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR (CLZERO)
  • No comments in code except technical debt
  • Unimplemented paths throw; silent stubs are listed in TechnicalDebt (CLZERO/RDPRU/MCOMMIT and invalid MONITORX/MWAITX encodings still throw)
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source (n/a)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4697585788 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/201). Originally posted by [ljkx](https://github.com/ljkx) on 2026-10-01T00:29:18Z. --- ### What Addresses part of the `--to-intel` item in `docs/dev/TechnicalDebt.md`: a MONITORX (`0F 01 FA`) or MWAITX (`0F 01 FB`) anywhere in a converted segment failed the whole relink. Since #151, guest modules (a title's own `sce_module` PRX such as libc and libSceJobManager) go through the same conversion. No title is currently known to contain these instructions. Both are now replaced in place, at the same length, by the matcher: - MONITORX → a NOP of the instruction's length. - MWAITX → PAUSE (`F3 90`) followed by NOPs up to the instruction's length. Why this is exact rather than an approximation, from the AMD APM Vol. 3 (rev 3.36, MWAITX): - "The MWAITX instruction has the same effect on architectural state as the NOP instruction." - Its exit events include "Any unmasked interrupt", so it can return at any time, and its timer (EBX, enabled by ECX[1]) is only an upper bound. - "There is no indication after exiting MWAITX of why the processor exited or if the timer expired. It is up to software to check whether the awaiting store has occurred." Correct callers therefore loop and re-check, as the Linux kernel's `delay_halt` does around MWAITX ("these functions may return earlier than requested"). The replacement returns at once every time, which real hardware also does. On a Ryzen 7 7800X3D, MWAITX without an armed monitor and with a 2·10⁹-cycle timer returned after ~900 cycles. rFLAGS: none for either instruction. Limits: - **Faults not reproduced:** the faults these raise only for invalid use are no longer raised. For MONITORX that is #GP for ECX ≠ 0, non-canonical addresses, and the page and segment checks of a 1-byte read. For MWAITX it is #GP for reserved ECX bits. - **Invalid encodings still fail:** forms with a LOCK prefix (#UD) or longer than 15 bytes (#GP) still fail the relink instead of being replaced. - **Branches into a prefix run:** like the existing in-place lowerings, the converter does not check branch targets inside a replaced instruction, so a jump into the middle of a prefix run would land inside the NOP padding. - **Cost:** a busy spin with PAUSE instead of a low-power wait, which costs power and SMT throughput. CLZERO, RDPRU and MCOMMIT still fail the relink: - CLZERO can be lowered exactly: four `MOVNTDQ` of zero to the aligned 64-byte line match its non-temporal, weakly ordered store. It needs an out-of-line stub, so it is left for a follow-up PR using the stub builder from #200. - RDPRU (APERF/MPERF) and MCOMMIT have no exact equivalent. `TechnicalDebt.md`: the item now lists CLZERO/RDPRU/MCOMMIT. Rebased onto `main` after #200; the conflicts with it in the matcher, its tests and the `TechnicalDebt.md` line were resolved by keeping both changes. ### Tested - Relinker test suite (14 tests, on current `main` including #200) passes on Windows 11 (Clang 23, and MinGW-w64 GCC 16.2 with static linking) and on Ubuntu 24.04 under WSL (GCC 13.3). - `amd64_only_converter`: - Replacements: MONITORX → `0F 1F 00`, MWAITX → `F3 90 90`, and the prefixed forms `67 0F 01 FA` → `0F 1F 40 00` and `2E 41 0F 01 FB` → `F3 90 0F 1F 00`. - Padding with two NOPs: an 8-byte MONITORX and a 15-byte MWAITX. - Still unsupported: LOCK MWAITX, a 16-byte MONITORX, and CLZERO. - Converter: a segment with MONITORX, MWAITX and a prefixed MWAITX is rewritten in place with the expected bytes and reports. - The tests that used MONITORX as the example of an unsupported instruction now use CLZERO. - Mutation check: wrong PAUSE bytes, an off-by-one in the NOP padding, a removed LOCK check, and a 16-byte limit each make the test fail. - Running the real converter over 406 prefix/REX combinations of lengths 3–17 (segment, address-size and LOCK prefixes, with and without REX.W) gives replacements of exactly the original length that end on an instruction boundary for every valid form, and Unsupported for every LOCK or over-length form. - Hardware (Zen 4, AMD only; nothing was run on Intel): - MWAITX without an armed monitor returns at once. - With an armed monitor and a 10⁵-cycle timer it returned after 17k–64k cycles. - A store from another thread wakes it. - A monitorx/mwaitx spin-wait loop whose flag is set by another thread after 50 ms finishes after ~61 ms both as written and with the replacement bytes (about 5k vs 3.8M iterations). - No title tested. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR (CLZERO) - [x] No comments in code except technical debt - [x] Unimplemented paths throw; silent stubs are listed in TechnicalDebt (CLZERO/RDPRU/MCOMMIT and invalid MONITORX/MWAITX encodings still throw) - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source (n/a) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!201
No description provided.