M1ssion Impossible: How Linux Researchers Reverse Engineered Apple’s Chips
The latest update to the Linux kernel, termed Linux 6.2, officially announced support for the M1 Pro, Max, and Ultra chips. This was made possible due to a team working largely on donations from Patreon and GitHub, who took on one of the most difficult tasks in software: reverse engineering an Apple M1 chip. While competitors like Intel and AMD provide comprehensive documentation on how their chips are designed, Apple hasn’t done so for its custom-made chips. This was mainly because M1 chips weren’t sold standalone, and were always bundled with MacBooks running MacOS. This meant that Apple could keep all their cards close to its chest, and no one would ask. Linus Torvalds, the creator of Linux spearheaded the efforts to bring Linux to M1. In late 2022, reports said that he was using an M2 MacBook as a daily driver. In the past too, Torvalds has expressed his feelings regarding the new MacBooks during its launch in 2020, stating, “I’ve been waiting for an ARM laptop that can run Linux for a long time. The new Air would be almost perfect, except for the OS. And I don’t have the time to tinker with it, or the inclination to fight companies that don’t want to help.” It seems that the sentiment changed somewhere down the line, as Linux version 5.19 was reported to have been released from Linus’ new M2 MacBook. In the release notes for this version, Linus revealed that he was running Linux on his ARM-powered M2 Mac, all thanks to a small team of developers from a community project called Asahi. Breathing new life into old hardware Apart from the development efforts on the driver and reverse engineering of the chip, the Asahi Linux project also has a team of dedicated developers working on the discrete components of Apple’s chips. There is still a long list of features that don’t work on Asahi Linux, notably the Neural Engine, Touch Bar, and Thunderbolt, as these products require reverse engineering of their own to crack. However, this is one of the best efforts by the Linux community to bring the OS to yet another line of products. Users on the Apple subreddit reacted extremely positively to news of the Linux support for Macs. Many were surprised at the amount of work it took to reverse-engineer the chip, while others marvelled at how low the barrier to entry had become for a capable Linux machine. Reddit user GamingChairModel stated, “The Mac Mini is a killer deal on the actual CPU, but not a great deal on storage/memory upgrades. With the memory soldered on, it makes sense to upgrade to 16GB RAM and just add on external storage through the Thunderbolt port (or a separately managed NAS)…So for any application that doesn’t require a ton of memory, the Mac Mini’s hardware could be a great ARM server.” Making Apple devices into home servers is just one of the various applications that Linux support opens up. When Apple inevitably stops supporting the M1 Macs sometime in the future, those with the technical know-how can give the still-capable devices a second life with a Linux install. What’s more, the GPU drivers also allow users to run games on an M-series GPU — something considered impossible unless the game was developed specifically for it. Breaking down the walled garden While porting Linux to ARM devices has been a work in progress for the past 20 years, this part of the Linux community has come a long way since then. However, the release of Apple’s M1 chips presented a new challenge to Linux kernel maintainers. Even though the chips were built on ARM’s IP, which has been well-documented in Linux, the architecture that Apple built on top of it was largely unknown. This left the researchers at Asahi in a difficult spot. To allow the Linux kernel to support M1 devices, they first needed to know how the OS interacted with the discrete components of the M1 processor. One of the most important parts of the development process was the creation of a hypervisor termed m1n1, which allowed the researchers to root around in software close to M1’s ‘bare-metal’, allowing them to map out the processor. The M1 chip consists of a CPU with performance and efficiency cores, an integrated Neural Engine, and the bane of Asahi’s existence — the GPU. While it was relatively easier to build code for the M1 CPU using m1n1 and a lot of work over 6 months, the GPU proved to stump the researchers’ efforts, until they brought in Alyssa Rosenzweig. While Dougall Johnson laid the groundwork for reverse engineering the M1 GPU by exploring its capabilities and creating some documentation, Rosenzweig was tasked with getting a custom-built driver up and running on M1’s GPU. In a four-part epic detailed on her website, Rosenzweig took on the behemoth task of writing a driver for the GPU. In January 2021, she decoded enough of the instruction set to disassemble some simple shaders — a program that calculates levels of light, darkness, and colour when rendering 3D images. Most modern GPUs are made up of 3 components — shader cores, rendering units, a command processor, and a memory management unit. Shader cores process 3D models and pixel data, and rendering units take this processed information and convert it into pixels. Command processors convey commands from the software to the shader cores, while memory management units make sure that any given bit of memory is not accessed by multiple programs at once. To decode this instruction set, Alyssa first had to jump into the command stream within the GPU. Once these commands were decoded, she had to decode the chip’s shader binaries. These are the low-level bits of code that determine how the GPU’s shader cores function. She could also send commands to the chip through her toolkit — a process which allowed her to work on the GPU without needing to assemble a whole new driver for the Linux kernel. In her first month of working on the project, she was able to render a single triangle on the M1 GPU core. By rendering a triangle, the researcher was able to find specific information about how the GPU processes commands and gives an output. By April 2021, she was able to write a shader compiler and render a 3D spinning cube. Around the same time, Johnson was making forays into investigating the instruction set and building compute kernels. In May, she was able to begin development on a graphics driver built on Gallium with support for OpenGL and ES 2.0 specifications. Naturally, the process involved a lot of programming nitty-gritty, which has been omitted from this explanation for brevity’s sake. However, the end result was that Alyssa got a driver to work on the M1 chip, but encountered a mystery bug she termed “the impossible bug”. While trying to implement the driver (termed the Mesa driver) on the hardware, Alyssa found that the driver cannot render large amounts of geometry. This was because the M1 chip used a different rendering method than traditional methods, something Alyssa had overlooked. This was fixed with a bit of software magic, putting the bow on top of a completely working driver, which was then integrated into the Linux kernel with help from Asahi Lina. Asahi Lina is a vtuber who was responsible for most of the community attention the project received, as she published hours-long videos of the process of creating a driver for M1 Mac. She was also responsible for writing the kernel driver, a piece of software that provided an interface between Linux and the GPU driver. Notably, this driver was the first Linux GPU driver written completely in Rust, chosen for its fast execution and support for large numbers of data structures. After bringing together the kernel driver and the Mesa driver developed by Alyssa, the GPU driver for Mac was finally complete. What’s more? The programmers managed to preserve the M1 Mac’s prodigious battery life, with tests revealing that their driver could run a 3D game at 1080p for over eight hours. Bringing Linux to Mac, as with any other Linux-based update, was a team effort by a concerted group of people including, but not limited to, Alyssa Rosenzwig, Ella Stanforth, Dougall Johnson, Asahi Lina, Sven Peter, Mark Kittens, and Hector Martin. For a select group of people, these names will be immortalized as those who broke down a big portion of Apple’s walled garden.
Two years after the release of Apple’s M1 Macs, a group of dedicated computer scientists have finally brought Linux to Apple’s crown jewels
Two years after the release of Apple’s M1 Macs, a group of dedicated computer scientists have finally brought Linux to Apple’s crown jewels
[]
["Go", "TPU", "AI", "Git", "RAG", "Ray", "ViT", "Rust", "GitHub", "R"]
["AI", "Ray", "RAG", "TPU", "R", "Go", "Rust", "Git", "GitHub", "ViT"]
I am an AI enthusiast and love keeping up with the latest events in the space. I love video games and pizza.