It turns out that Apple has baked in support into its Apple Silicon processors for an instruction that was designed for Intel's 8080 processor that debuted back in 1974, using a secret undocumented extension to execute the very infrequently-used instructions, thus boosting emulation performance.
Apple’s Rosetta 2 is part of macOS that allows applications written for Intel processors to run on Apple Silicon (opens in new tab)-equipped Macs, and it works rather well. Named for the Rosetta Stone, which allowed Egyptologists to begin decoding the ancient hieroglyphic writing system in the 1820s. Rosetta has been equally mysterious, but may have begun to give up its secrets, as detailed in a blog post (opens in new tab) by Australian security researcher Dougall Johnson, in which he reveals an undocumented extension while discussing why Rosetta 2 is so fast.
The secret extension appears to alter the way the processor stores parity and adjust flags from an application, to provide more accurate emulation. The story is taken up by web developer and retro computing enthusiast Blake Patterson (opens in new tab), who explains how and why it does this, with the help of Johnson’s posts on Mastodon (opens in new tab).
The whole thing can be traced back to the Intel 8080 from 1974, which was the company’s second-ever chip (opens in new tab). This 8-bit microprocessor handled its adjust and parity flags in a particular way, using them to indicate whether to carry numbers in a binary-coded decimal operation, and this has persisted through to today’s x64 chips even though it’s hardly ever used by modern applications.
The Arm architecture used by Apple Silicon, being unrelated to x64, has no such functionality, so it needed to be emulated. Otherwise, some Intel software wouldn’t work on the new Macs, and computing the same operation without the 8080’s flag-handling would use five times as many instructions and slow the Mac down.
Rosetta 2 recompiles a binary application meant for an Intel processor at launch rather than providing real-time emulation, but cannot tell if the adjust and parity flags will be used by the app. It uses bits 26 and 27 of the Arm flags register to emulate the 8080’s handling of them to avoid needing to go the long way round every time.
It would break the Arm specification to have this running all the time, so the Apple Silicon processors only do this when running Rosetta 2 - Johnson points out that it doesn’t work when the Apple processor is running a Linux VM, as the processor isn’t as configurable in this situation.
While not exactly Earth-shattering news, it’s a fascinating insight into the workings of an extremely clever and performant piece of software, albeit one that - in the same way the original Rosetta was introduced in Mac OS X 10.4 Tiger and retired in 10.7 Lion - is likely to fade away once Apple Silicon is a few more generations old.