by DonHopkins · 5y ago · 0 comments
How expensive is changing memory mapping modes? Does it involve a system call? Does it need to happen often?
It's just an mprotect(2) call to change the page from R+W to R+X. It needs to happen between the JIT being done creating a hunk of code and the first execution. Basically: allocate a page, write generated machine code to the page, change page to executable, patch page in (so the code gets invoked instead of either interpretation or a baseline compiler).
I'm sure you can be smarter about it and involve locks such that you track how full "code pages" are and switch non-full pages back to writeable in order to update their content.
The mprotect call will fail as that isn’t a valid mechanism to toggle rw/rx
I don't have an M1 Mac to test myself, but the "faster" mechanism doesn't involve anything in the pagetables, so I think toggling pagetable walks into the existing restrictions on RWX allocations.
https://dolphin-emu.org/blog/2021/05/24/temptation-of-the-ap...