Skip to content

Observe ISYNC so replaced executable RAM can invalidate stale translations - #5

Closed
bgates747 wants to merge 1 commit into
levkropp:masterfrom
bgates747:codex/iram-isync-coherence
Closed

bgates747 wants to merge 1 commit into
levkropp:masterfrom
bgates747:codex/iram-isync-coherence

Conversation

@bgates747

Copy link
Copy Markdown
Contributor

Prepared by Codex on behalf of Brandon

This PR resolves stale code executing after an embedding host replaces an
already translated RAM function and the guest executes ISYNC, when the host
supplies the attached invalidation policy. In the linked
reproducer, the replacement should return 43, but cached execution still returns
42. The correction supplies an optional ISYNC observer through which the host
can invalidate changed executable RAM.

The interpreter currently treats ISYNC as a no-op, the predecoder classifies
it as PD_NOP, and the JIT can compile across it. An embedding host therefore
cannot apply its translation-coherence policy at that architectural boundary.
Explicit xtensa_invalidate_code works, but the guest barrier does not expose
a place to call it. This is separate from host ARM64 instruction-cache flushing.

Add an optional CPU observer and context, call it for ISYNC in the canonical
and quick interpreter paths, prevent cached no-op handling and terminate JIT
blocks before the barrier. Preserve the observer across session CPU reset.
Other synchronization instructions retain their current semantics.

The synthetic regression contains both the guest
instruction bytes and a small host invalidation policy. It warms a function
returning 42, replaces it with one returning 43, executes an actual guest
ISYNC, then executes the same address again. It covers byte, halfword, word,
backing-copy and interpreted/JIT guest stores, including a translated store
prefix directly before ISYNC, plus first load into predecoded RAM. Explicit
invalidation and null-observer/barrier controls accompany it. No firmware,
ROM image, application or game assets are needed.

The same regression compiles against both APIs. Observer registration and its
callback-count checks are conditional on the new feature macro; expected guest
values are identical.
The embedding host must supply the invalidation policy: installing no observer
does not automatically make all RAM writes coherent. This does not promise
coherence for arbitrary self-modifying code without ISYNC, cross-core policy,
or hardware cache timing. Embedders must rebuild for the changed CPU structure.

Validated against master e2b79fc7a8987b2821f74065e47e70e3e261e45e
on Linux ARM64, GCC 14.2, Release with LTO/native tuning disabled:

  1. New regression with unchanged production code: 3 tests, 118 passing and 12 failing
    assertions, including all seven replacements returning 42 instead of 43.
  2. Identical regression with the core hook and example host policy: 3 tests,
    145 passing assertions, 0 failures (15 extra observer-count checks are now available).
  3. Integration/JIT/cached-interpreter neighbours: 133 tests, 24,149 passing
    assertions, 0 failures. These are unit checks, not hardware/application acceptance.

Build the normal xtensa-tests target, then run:

./build/xtensa-tests --quiet iram_isync

Reproduction instructions explain applying the regression
first, observing assertion failures, then applying the source patch and rerunning.

@bgates747

bgates747 commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor Author

The human adds: this is a rather involved change, and I don't have the personal expertise to vouch for it being any kind of elegant or optimal solution. We found it because one of my experiments involves loading arbitrary Xtensa code at runtime, so that select extended functionality can be added as desired, without requiring a full firmware flash. So far the fixes in this PR appear to be working, [edit: in the sense that they behave the same on hardware now] but I've not made any attempt to look for any refinements.

@levkropp

levkropp commented Nov 9, 2026

Copy link
Copy Markdown
Owner

Thanks for tracking this down. Loading code at runtime is a case Flexe should handle on its own, so rather than add an observer API I fixed it in the core in 9372d98.

Guest stores that change RAM holding translated code now invalidate it directly, through any alias (including the S3 DRAM view of IRAM), whether or not an ISYNC follows. That covers the predecode table, the cached interpreter and the JIT, including a compiled block that rewrites code and then jumps straight into it. No embedder hook is needed, so your loader should work as-is on master.

I'm closing this in favour of that commit. If your runtime-loading experiment still misbehaves on master, please open an issue with a reproducer and I'll take a look.

@levkropp levkropp closed this Nov 9, 2026
@bgates747

Copy link
Copy Markdown
Contributor Author

Thanks, Lev, for fixing this in the core; our development build already uses your fix. — Written by Codex on behalf of Brandon.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants