4 ms·
>This was thankfully a lot less strict than on iOS devices, which strictly forbid mapping memory as executable whatsoever and made iOS untenable for us to offic
by DonHopkins 5y ago
>This was thankfully a lot less strict than on iOS devices, which strictly forbid mapping memory as executable whatsoever and made iOS untenable for us to officially support. Apple even provides documentation for helping developers port JITs to macOS on ARM. Skyler used a method described in the documentation that would change the mapped memory between Writeable when emitting code to Executable when executing code. Since Dolphin wasn't designed for this, there were a few hiccups along the way, but eventually everything was massaged into working with the new restrictions.
How expensive is changing memory mapping modes? Does it involve a system call? Does it need to happen often?
- masklinn 5y agoNo / yes / only once. 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.
- olliej 5y agoOn m1 switch RX to RW and back does not require a syscall The mprotect call will fail as that isn’t a valid mechanism to toggle rw/rx
- monocasa 5y agoCan you give a citation that mprotect will fail? Yes there's the new, faster mechanism, but I'm surprised that they broke mprotect.
- olliej 5y agoit's possible the old mprotect call will work if you use the appropriate initial mmap flag to get a jit region, but I would be surprised. The rules for rwx regions are already very restrictive compared to mmap/mprotect/etc APIs on other platforms. 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.
- monocasa 5y agoIt certainly looks like mprotect is still supported (if occasionally broken) as of 11.2 on apple silicon. Qemu and v8 both seem to rely on it. https://developer.apple.com/forums/thread/672804 https://developer.apple.com/forums/thread/672804
- saagarjha 5y agoI haven’t tested on a Mac recently, but I’m pretty sure that mprotect will change the base page permissions but SPRR will continue to apply (and you’ll need to call pthread_jit_write_protect_np).
- saagarjha 5y agoReusing pages is usually how most emulators work, as doing what you describe is essentially a memory leak.
- staticassertion 5y agoWhere is that quote from? I'm not seeing it on the linked page.
- arm 5y agoIt’s from this page (which was linked in the opening sentence of the April & May 2021 progress report): https://dolphin-emu.org/blog/2021/05/24/temptation-of-the-apple-dolphin-on-macos-m1/ https://dolphin-emu.org/blog/2021/05/24/temptation-of-the-ap...
- olliej 5y agoChanging memory modes on osx/m1 is cheap, it doesn’t involve a syscall, and it has to happen any time you want to make a change to generated code (unless you do code on a separate thread from execution)
- swiley 5y agoApple doesn't acknowledge emulators as valid software and would prefer the world not have access to them.
- saagarjha 5y agoRosetta is an emulator, though.
- deleted 5y ago[deleted]