Skip to content

arm64: expose logical CPU index in TPIDRRO_EL0 with PR_GET_TPIDRRO_CPU - #3153

Open
AxxzyWasTaken wants to merge 1 commit into
aurora-silicon:mainfrom
AxxzyWasTaken:pr/tpidrro-cpu
Open

AxxzyWasTaken wants to merge 1 commit into
aurora-silicon:mainfrom
AxxzyWasTaken:pr/tpidrro-cpu

Conversation

@AxxzyWasTaken

Copy link
Copy Markdown
Contributor

What

Maintain the current logical CPU index in TPIDRRO_EL0 for all native 64-bit EL0 tasks under CONFIG_ARM64_TPIDRRO_CPU (default y) and advertise support via the FEX-Emu PR_GET_TPIDRRO_CPU (0x7F1DBB0) query prctl:

  • tls_thread_switch() writes raw_smp_processor_id() into tpidrro_el0 on switches to native 64-bit tasks (preserving next->thread.uw.tp_value for compat 32-bit tasks).
  • kernel_entry for 64-bit EL0 restores tpidrro_el0 from thread_info.cpu while interrupts are masked so KPTI and Spectre-BHB entry trampolines (which use tpidrro_el0 as scratch and zero it) never leave a zeroed register on return without a context switch or when KVM saves host state.
  • arch_setup_new_exec() refreshes tpidrro_el0 after SET_PERSONALITY() so 64->32 exec clears the CPU index and 32->64 or initial kthread exec installs it.
  • prctl(PR_GET_TPIDRRO_CPU, 0, 0, 0, 0) returns 1 for native 64-bit tasks, 0 for compat 32-bit tasks, and -EINVAL for non-zero arguments or unsupported builds.
  • Adds KUnit suite arm64-tpidrro-cpu (arch/arm64/kernel/tpidrro_kunit.c) testing native vs compat switch values, PR_GET_TPIDRRO_CPU queries, and live TPIDRRO_EL0 updates across online CPU migrations.

This allows FEX-Emu (auto-detected via PR_GET_TPIDRRO_CPU with no config change) to emulate x86 RDTSCP, RDPID, and CPUID processor-ID queries with a single MRS TPIDRRO_EL0 instead of a getcpu() syscall, matching the Windows ARM64 ABI.

Tested

Hardware row at 5c9e5933d630: Apple M4 Max 16" (T6041 / J616C), 7.3.0-j616c-tpidrro+:

  • Boot-time KUnit (arm64-tpidrro-cpu): 3/3 PASS (tpidrro_cpu_switch_value_test, tpidrro_cpu_prctl_query_test, tpidrro_cpu_live_migration_test, fail:0 skip:0).
  • Userspace selftest (tpidrro_selftest_arm64): ALL PASS (PR_GET_TPIDRRO_CPU == 1, -EINVAL on non-zero args, MRS TPIDRRO_EL0 == sched_getcpu() across all 14 online CPUs before and after execve). Stock kernel returns -1 (EINVAL) as expected.
  • Live FEX-Emu RDTSCP (FEX 05d261888, User4K, default auto-detect with zero config change, pinned P-core c6, 2M iterations, 3 runs):
    • Stock 7.3.0-j616c-main25nov+: 256.65 ms (128.32 ns/call)
    • 7.3.0-j616c-tpidrro+ (5c9e5933d630): 46.89 ms (23.45 ns/call) — 5.48x faster (-81.7%)
    • sched_getcpu == IA32_TSC_AUX verified YES across E- and P-cores (cpu 0, 1, 6, 9, 13).
  • scripts/checkpatch.pl --strict: 0 errors, 0 checks (only new-file MAINTAINERS note).

Risk

two-way · Native 64-bit EL0 tasks previously observed 0 in TPIDRRO_EL0 and now observe the logical CPU index already exposed by getcpu(2). Compat 32-bit tasks retain their TLS pointer in TPIDRRO_EL0. Gated by CONFIG_ARM64_TPIDRRO_CPU; cleanly revertible.

Not yet

  • Compat 32-bit (CONFIG_COMPAT) runtime test on a 32-bit-capable ARM64 platform (M4 Max is 64-bit EL0 only; compat switch value is unit-tested in tpidrro_cpu_switch_value_test).
  • Testing on other Apple Silicon generations (M1/M2/M3/M5 / A18 Pro).

@RyanTheTide RyanTheTide left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: HOLD at 5c9e5933d630

Blockers:

  1. include/uapi/linux/prctl.h adds a public ABI and arch/arm64/Kconfig changes the register value for every native arm64 task by default. FEX uses the same number as a downstream convention. The current upstream Linux UAPI header contains neither the name nor that number. Please get an explicit kernel-owner ABI decision before shipping, document the scope and lifetime of the downstream contract, and record the plan for upstream arm64 ABI coordination. A revert after FEX or another program depends on this remains an ABI break.
  2. The one M4 Max row exercises native task migration and the fallback query on a stock kernel. The affected path is generic arm64 and includes compat TLS, exception entry and KVM host restore. Please add one shared current-head platform row and a compat-capable check covering 32/64-bit exec and TLS. Exercise enabled and disabled query results and returning to userspace after an exception and a KVM run on the paths this PR claims to support.

Checked: FEX selects the register only when the query returns exactly 1. Its existing fallback covers both the unsupported-build error and the compat zero. The privileged writer loads the calling task's logical CPU number, which is already available through getcpu(2); I found no new pointer, key, physical ID or other task's data exposed by the patch. The direct read is outside syscall filtering, which is worth spelling out in the ABI description.

Evidence

@AxxzyWasTaken

Copy link
Copy Markdown
Contributor Author

@RyanTheTide Fair points on both counts — happy to hold #3153 pending:

  1. A kernel-owner ABI decision on carrying downstream PR_GET_TPIDRRO_CPU (0x7F1DBB0, matching upstream FEX-Emu/FEX#6087, alongside Aurora's downstream PR_SET_USER_PAGE_SIZE), plus explicitly documenting the direct-MRS / seccomp-getcpu behavior and upstream coordination plan.
  2. Expanded validation covering exception-return, KVM host-restore, CONFIG_ARM64_TPIDRRO_CPU=n, a compat-capable 32/64-bit exec/TLS check, and a second-platform hardware row.

@RyanTheTide RyanTheTide added the hold Held on purpose; skipped by the PR digest label Nov 25, 2026
@github-actions github-actions Bot added changes-requested A reviewer requested changes (bot) door:two-way Revertible or not, from Merge risk (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Nov 25, 2026
@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Merge readiness at a35c0428: not ready yet

Check Detail
CI at head no failing: host-tests, KUnit ane-boot, KUnit ane-pm, KUnit dcp-topology, KUnit asahi, KUnit spi-hid-apple
Review no needs one approving review from Core or Omarchy Mac (not the author)
Requested changes no requested by RyanTheTide
Hardware row at head ok comment at a35c0428
Conflicts ok none
PR text ok Risk: two-way · Native 64-bit EL0 tasks previously observed 0 in TPIDRRO_EL0 and now observe the logical CPU index already exposed by getcpu(2). Compat 32-bit tasks retain their TLS pointer in TPIDRRO_EL0. Gated by CONFIG_ARM64_TPIDRRO_CPU; cleanly revertible.
Hold no labelled hold by a maintainer

Areas: none matched in CODEOWNERS

Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels.

@RyanTheTide

Copy link
Copy Markdown
Contributor

@AxxzyWasTaken: the maintainers decided on the ABI question. We accept PR_GET_TPIDRRO_CPU as an opt-in downstream ABI only:

  1. Off by default: no change to TPIDRRO_EL0 for any task that has not asked for it.
  2. Enabled per process through the prctl (FEX opts in).
  3. The prctl number 0x7F1DBB0 is documented as Aurora-reserved, in the commit message and under Documentation/ (arch/arm64).
    With those three changes and a second-family row (we run it on our J713 or J616s), this merges. Can you push them within 24 h? If not, we push them to your branch ourselves (maintainer edits are on) and keep your authorship.

…DRRO_CPU

Allow native 64-bit EL0 processes to opt in to having the current logical
CPU index maintained in TPIDRRO_EL0 under CONFIG_ARM64_TPIDRRO_CPU
(default y), gated behind the Aurora-reserved downstream prctl
PR_GET_TPIDRRO_CPU (0x7F1DBB0, matching FEX-Emu's convention).

- Off by default: tasks that have not opted in observe no change to
  TPIDRRO_EL0 (native 64-bit tasks continue to observe 0; compat 32-bit
  tasks continue to use TPIDRRO_EL0 for their TLS pointer).
- Per-process opt-in: calling prctl(PR_GET_TPIDRRO_CPU, 0, 0, 0, 0) from
  a native 64-bit task sets MMCF_TPIDRRO_CPU on current->mm->context.flags
  and sets TIF_TPIDRRO_CPU on all native 64-bit threads in the calling
  thread group (kicking running siblings), writes smp_processor_id() to
  tpidrro_el0 immediately, and returns 1. Compat 32-bit tasks return 0
  without setting either flag. Non-zero arguments or disabled
  CONFIG_ARM64_TPIDRRO_CPU return -EINVAL.
- Inheritance and execve(): TIF_TPIDRRO_CPU and MMCF_TPIDRRO_CPU are
  inherited across fork() and clone() (with tls_thread_switch() lazily
  propagating MMCF_TPIDRRO_CPU to any thread cloned concurrently with the
  prctl before it enters EL0). arch_setup_new_exec() resets
  mm->context.flags, clears TIF_TPIDRRO_CPU, and zeroes tpidrro_el0 so
  newly executed binaries start with TPIDRRO_EL0 == 0.
- Context switch, EL0 entry, and EL0 exit: tls_thread_switch() writes
  raw_smp_processor_id() into tpidrro_el0 only for opted-in native 64-bit
  tasks; kernel_entry for 64-bit EL0 tests TIF_TPIDRRO_CPU and restores
  tpidrro_el0 from thread_info.cpu while interrupts are masked (for KPTI /
  Spectre-BHB trampolines and KVM host-state saves); kernel_exit for
  64-bit EL0 also restores tpidrro_el0 when TIF_TPIDRRO_CPU is set so a
  sibling already in EL1 during opt-in returns with the CPU index loaded.
- Downstream ABI documentation: PR_GET_TPIDRRO_CPU (0x7F1DBB0) is
  documented as an Aurora-reserved downstream prctl in
  include/uapi/linux/prctl.h and Documentation/arch/arm64/tpidrro-cpu.rst,
  including scope, upstream coordination plan, and direct-MRS / seccomp
  behavior.

This lets FEX-Emu (both Linux and Wine/ARM64EC) implement x86 RDTSCP,
RDPID, and CPUID processor-ID queries with a single MRS instruction
instead of a getcpu() syscall, matching the Windows ARM64 ABI.

Add KUnit tests in arch/arm64/kernel/tpidrro_kunit.c covering default-off
vs opted-in switch values, compat 32-bit isolation, PR_GET_TPIDRRO_CPU
opt-in handling, and live TPIDRRO_EL0 behavior across CPU migrations.

Signed-off-by: AxxzyWasTaken <[email protected]>
@AxxzyWasTaken

Copy link
Copy Markdown
Contributor Author

@RyanTheTide Pushed the opt-in ABI update rebased onto main (e77d74598d216) at a35c042881882:

  1. Off by default: TPIDRRO_EL0 remains 0 for all native 64-bit tasks that have not opted in (compat 32-bit tasks keep their TLS pointer unchanged).
  2. Per-process opt-in via PR_GET_TPIDRRO_CPU (0x7F1DBB0): Calling prctl(PR_GET_TPIDRRO_CPU, 0, 0, 0, 0) from a native 64-bit task sets MMCF_TPIDRRO_CPU on mm->context.flags and TIF_TPIDRRO_CPU on all native 64-bit threads in the calling thread group (kicking running siblings and restoring tpidrro_el0 on both kernel_entry and kernel_exit for 64-bit EL0, with tls_thread_switch() lazily propagating MMCF_TPIDRRO_CPU to any thread cloned concurrently), writes smp_processor_id() immediately, and returns 1. Inherited across fork()/clone(), cleared on execve() (resetting TPIDRRO_EL0 to 0). Compat 32-bit tasks return 0 without setting either flag; non-zero args or CONFIG_ARM64_TPIDRRO_CPU=n return -EINVAL.
  3. Aurora-reserved documentation: Documented 0x7F1DBB0 as an Aurora-reserved downstream prctl (including scope, upstream coordination plan, and direct-MRS / seccomp behavior) in the commit message, include/uapi/linux/prctl.h, and Documentation/arch/arm64/tpidrro-cpu.rst (indexed in Documentation/arch/arm64/index.rst).

Updated KUnit coverage in arch/arm64/kernel/tpidrro_kunit.c for default-off vs opt-in switch values, compat isolation, PR_GET_TPIDRRO_CPU opt-in, and live CPU migration. Cross-compiled cleanly with ARCH=arm64 LLVM=1 for both CONFIG_ARM64_TPIDRRO_CPU=y (with KUNIT + ARM64_TPIDRRO_CPU_KUNIT_TEST=y) and CONFIG_ARM64_TPIDRRO_CPU=n.

Ready for your review and J713/J616s row (happy to run a fresh J616C boot row at a35c042881882 as well if needed).

@aurora-silicon-review aurora-silicon-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: HOLD at a35c0428

Blockers:

  • Hosted build/KUnit CI at a35c0428 pending/incomplete: Wait for apple-build, arm64 KUnit (arm64-tpidrro-cpu), and host-tests to complete green on a35c0428.
  • Exact-head hardware verification across two Apple Silicon families (a35c0428): Per the prior review, please post exact-head a35c0428 hardware rows on two SoC families verifying the new opt-in semantics end to end: (a) native 64-bit task observes TPIDRRO_EL0 == 0 by default, (b) prctl(PR_GET_TPIDRRO_CPU, 0, 0, 0, 0) returns 1 and updates TPIDRRO_EL0 across CPU migration and in already-running sibling threads via kick_process(), and (c) execve() clears TIF_TPIDRRO_CPU and resets TPIDRRO_EL0 to 0.
  • Update PR description to match the opt-in ABI: The PR body still describes the old default-on TPIDRRO_EL0 behavior from 5c9e5933; please update it to match the opt-in ABI implemented at a35c0428 (and resolve the prior CHANGES_REQUESTED review from RyanTheTide).

Checked:

  • Audited full diff (+386/-4 across 12 files, MERGEABLE into main):
    • Verified all three maintainer ABI requirements from 2026-11-29 are implemented cleanly:
      1. Off by default (TPIDRRO_EL0 == 0 for native 64-bit tasks without TIF_TPIDRRO_CPU / MMCF_TPIDRRO_CPU_BIT; compat 32-bit tasks retain thread.uw.tp_value).
      2. Per-process opt-in via PR_GET_TPIDRRO_CPU (0x7F1DBB0): sets MMCF_TPIDRRO_CPU_BIT before iterating for_each_thread() under siglock + kick_process(t), closing the concurrent-clone() race via tls_tpidrro_value() and updating running siblings on kernel_exit (entry.S:465-471, where x0 is scratch before ldp x0, x1).
      3. Downstream Aurora-reserved ABI 0x7F1DBB0, scope/lifetime, inheritance, execve() reset, and seccomp implications are clearly documented in Documentation/arch/arm64/tpidrro-cpu.rst, include/uapi/linux/prctl.h, and arch/arm64/Kconfig.
    • Verified entry.S restores tpidrro_el0 from TSK_TI_CPU on both 64-bit EL0 kernel_entry (after KPTI/Spectre-BHB trampolines) and kernel_exit (guarded by TIF_TPIDRRO_CPU and !PSR_MODE32_BIT), and arch_setup_new_exec() clears TIF_TPIDRRO_CPU and zeroes tpidrro_el0.
Evidence
  • Head commit: a35c042881882af74bfaa360d49c956f888fc5f8 (MERGEABLE into main).
  • Non-blocking nit: in arch/arm64/kernel/tpidrro_kunit.c (tpidrro_cpu_prctl_optin_test and tpidrro_cpu_live_migration_test), if KUnit ever runs with current->mm != NULL, restoring MMCF_TPIDRRO_CPU_BIT alongside TIF_TPIDRRO_CPU would keep the default-off assertion hermetic across runs (safe in kthread context where current->mm == NULL).

@github-actions github-actions Bot added the ci-failing A check failed at the PR head (bot) label Dec 1, 2026

@aurora-silicon-review aurora-silicon-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: MERGE at a35c04288188

Converts this App's earlier review at the same head to an approval under the 12-01 merge bar (better than before and doesn't break; block only on a shown failure). No code blocker was found in it. The merge gate (a row on one Mac at this head + fast CI) is tracked by pr-readiness, not held here.

Checked: - Audited full diff (+386/-4 across 12 files, MERGEABLE into main):

  • Verified all three maintainer ABI requirements from 2026-11-29 are implemented cleanly:
    1. Off by default (TPIDRRO_EL0 == 0 for native 64-bit tasks without TIF_TPIDRRO_CPU / MMCF_TPIDRRO_CPU_BIT; compat 32-bit tasks retain thread.uw.tp_value).
    2. Per-process opt-in via PR_GET_TPIDRRO_CPU (0x7F1DBB0): sets MMCF_TPIDRRO_CPU_BIT before iterating for_each_thread() under siglock + kick_process(t), closing the concurrent-clone() race via tls_tpidrro_value() and updating running siblings on kernel_exit (entry.S:465-471, where x0 is scratch before ldp x0, x1).
    3. Downstream Aurora-reserved ABI 0x7F1DBB0, scope/lifetime, inheritance, execve() reset, and seccomp implications are clearly documented in Documentation/arch/arm64/tpidrro-cpu.rst, include/uapi/linux/prctl.h, and arch/arm64/Kconfig.
  • Verified entry.S restores tpidrro_el0 from TSK_TI_CPU on both 64-bit EL0 kernel_entry (after KPTI/Spectre-BHB trampolines) and kernel_exit (guarded by TIF_TPIDRRO_CPU and !PSR_MODE32_BIT), and arch_setup_new_exec() clears TIF_TPIDRRO_CPU and zeroes tpidrro_el0.

Follow-ups (not blocking): CI; exact-head TPIDRRO opt-in rows (one Mac enough); update PR body to opt-in ABI; Ryan's open CR

@RyanTheTide

Copy link
Copy Markdown
Contributor

Holding this one out of today's merge train: it breaks the KUnit build. In a combined candidate (main + 28 approved PRs, this one included) the ave KUnit suite failed to compile:

arch/arm64/kernel/process.c:1345:12: error: unused function 'arch_prctl_get_tpidrro_cpu_for_task' [-Werror,-Wunused-function]

The function is static and its only caller is tpidrro_kunit.c, which is included at process.c:1385 only when CONFIG_ARM64_TPIDRRO_CPU_KUNIT_TEST is set. Any KUnit build with CONFIG_ARM64_TPIDRRO_CPU=y and that test off (the Apple suites' configs) hits it. Moving the helper inside the #if IS_ENABLED(CONFIG_ARM64_TPIDRRO_CPU_KUNIT_TEST) block, or marking it __maybe_unused, should fix it. Once that's pushed it goes into the next train.

This was referenced Dec 1, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changes-requested A reviewer requested changes (bot) ci-failing A check failed at the PR head (bot) door:two-way Revertible or not, from Merge risk (bot) hold Held on purpose; skipped by the PR digest needs-review No approving review from Core or Omarchy Mac yet (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants