fix(relinker): report processor 0 from RDTSCP under --to-rosetta #6111
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6111"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by Kernel-Hunter on 2026-10-14T22:32:52Z.
What
Under Rosetta, RDTSCP loads ECX with Rosetta's constant TSC_AUX, 0xAAAAAAAB, which is not a processor index.
--to-rosettaalready lowers RDPID to processor 0 for this reason, anddocs/dev/debt/relinker.mdlists RDTSCP's ECX as not converted. So the same program could read processor 0 through RDPID and 0xAAAAAAAB through RDTSCP.With
--to-rosetta, RDTSCP (0F 01 F9, no 66/F2/F3 prefix) now goes through an optional out-of-line stub, the same mechanism RDPID uses. The stub runs the original RDTSCP, so EDX:EAX and the instruction's ordering stay the same, then doesmov ecx, 0(B9 00 00 00 00). That clears all of RCX and leaves the flags alone. As with RDPID, a site that has no movable instruction after it for the jump is kept, and a kept RDTSCP still reports Rosetta's value. The Intel target and a plain relink do not change.Changes:
DecodedInstruction::IsRdtscp, theRDTSCPtable entry, and the matcher cases in_matchRosetta,_emitOutOfLineandMatchSequence, which marks a sequence that starts with RDTSCP as optional, as it does for RDPID.Amd64OnlyConverterTests: matcher cases (Rosetta stub bytes, optional, not converted for Intel, SWAPGS0F 01 F8and66 0F 01 F9left alone), plusrdtscpExecution. That test runs the stub on x86-64 Linux and macOS hosts and checks that RCX is 0, the counter is not 0 and CF is preserved. On other hosts it is empty.--to-rosettaentries indocs/dev/debt/relinker.mdanddocs/user/USAGE.md.Tested
Apple M5 (MacBook Air), macOS 27.0.1 (26A434), Apple clang 21.0.0, CMake 4.4.4, Ninja 1.13.2.
Rosetta's value, from a standalone x86-64 program built with
clang -arch x86_64and run under Rosetta: RDTSCP returns ECX =0xaaaaaaab. RDPID ends in SIGILL, which is why it is already lowered.Relinker-only build, native arm64 (
cmake -S . -B build-relinker -G Ninja -DCMAKE_BUILD_TYPE=Release -DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON):ctest --test-dir build-relinker -j8 --timeout 30: 163 of 163 pass.amd64_only_convertercases fail withRDTSCP was not lowered through an optional stub.Relinker-only build as x86-64 under Rosetta (same configure plus
-DCMAKE_OSX_ARCHITECTURES=x86_64, Python 3 run as x86-64), so thatrdtscpExecutionreally runs the stub:amd64_only_converter,to_rosettaandmacos_outputpass.RDTSCP stub was not produced.pr_overlap_encoding,stubgen_stdout_newlines,release_package,linux_fixed_base) also fail onmainin this setup, because the only x86-64 Python here is the system 3.9, which lackszip(strict=)andwrite_text(newline=).End to end under Rosetta: a byte-built PS5-style ELF (a copy of the
macos/import_fixture.pylayout) runsrdtscp, thenexit(ecx != 0). I relinked it with--macos --skip-sce-moduleand ran it with two small x86-64 stub dylibs standing in forlibc.prxandlibkernel.prx. Exit code 1 withmain's relinker, both with and without--to-rosetta. Exit code 0 with this branch and--to-rosetta.python3 tools/check_conventions.py --base origin/mainandgit diff --checkpass. I did not build or run anything on Linux or Windows; the execution test is compiled for x86-64 Linux too, so the Linux CI job runs it. No title was tested.tools/pr_overlap.pylists text overlap only, not the same function: #5784 (ANDN) touches the same_matchRosettalines and the same line inrelinker.mdandUSAGE.md, and #5683 and #5762 add calls at the same place in the converter test'smain. Whichever merges second needs a small rebase.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-14T22:48:34Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/relinker/codegen/src/x86/Amd64OnlyInstructionMatcher.cppL270core/relinker/codegen/src/x86/Amd64OnlyInstructionMatcher.cppL270Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
core/relinker/codegen/include/codegen/x86/Amd64OnlySubstitutionTable.hppcore/relinker/codegen/src/x86/Amd64OnlyInstructionMatcher.cppcore/relinker/codegen/tests/Amd64OnlyConverterTests.cppdocs/user/USAGE.mddocs/user/USAGE.mddocs/dev/debt/relinker.mddocs/user/USAGE.mddocs/user/USAGE.mddocs/user/USAGE.mddocs/dev/debt/relinker.mddocs/user/USAGE.mddocs/dev/debt/relinker.mdImported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T23:19:37Z.
Checked the RDTSCP decoder (prefix/REX handling,
0F 01 F8SWAPGS and66-prefixed forms are left alone), the stub order (original RDTSCP, thenmov ecx, 0, which keeps the flags) and the optional-site handling against RDPID.amd64_only_converter,relinker_conversion,relinker_executablesandto_rosettapass on the PR merged onto main, includingrdtscpExecutionon x86-64 Linux (RCX = 0, counter non-zero, CF preserved). Rosetta-hardware behaviour rests on your M5 run. Merging.Imported from boykopovar/AnyPS5. Originally posted by Kernel-Hunter on 2026-10-17T20:07:41Z.
Thanks for checking it. Rebased onto main again: the RDTSCP sentence in docs/dev/debt/relinker.md conflicted with the new macOS 26.4 measurements, so I kept those and changed only the RDPID/RDTSCP part. Relinker tests pass locally (178/178) and the conventions check is clean.