Right, if you look at the example JIT project I posted, they start by creating the memory with mmap() and PROT_EXEC before generating any code. So yes, you'd need to trap subsequent writes to allow for retranslation [I assume they have hooks in the Darwin kernel for this].
Re:About the Rosetta Translation Environment
by monocasa on Tuesday June 23, 2020 @11:48AM
(#23615516)
Or they just enforce W^X in the rosetta runtime by intercepting client mmap calls and fix it up as far as the client code is concerned by catching SIGBUS first.
I don't see anything here that requires extra kernel hooks.
Re:About the Rosetta Translation Environment
by anyfoo on Tuesday June 23, 2020 @06:00PM
(#23620911)
Pages containing x86 code are never actually being marked executable in the (ARM) pagetables, because that would be nonsensical: The CPU itself does not know how to run x86 code. mmap/mprotect from x86 with the exec bit therefore does something else than native code.
Re:About the Rosetta Translation Environment
by monocasa on Tuesday June 23, 2020 @08:59PM
(#23622641)
Right, but the x86 code _thinks_ it does, and those semantics have to be maintained.
What that practically means is that x86 executable pages get mapped as RO by default as seen by the ARM side even if they would normally be RWX. Modifications trap, the handler mprotects the pages as writeable, flush the translation cache for that region, then returns from the trap. Then on an x86 jump to that region, the JIT cache sees no traces for that region, marks the page as RO again so that modifications will trap, and starts recompiling (or maybe just interpreting; they may be using a tiered approach in the JIT).
So what you'll do is silently enforce W^X to trap code modifications, but not propagate those traps to the emulated code.