iPhone OS X Architecture: the Mach Kernel and RAM
July 13th, 2007
Daniel Eran Dilger
A number of people are working to crack open the iPhone in order to use it on alternative mobile networks or to install or modify software. What they are finding is more interesting to me because it reveals hints as to what latent features of the iPhone remain to be unlocked in software. Here’s a look at what’s been discovered about the core OS inside the iPhone, starting at the kernel.
La arquitectura del OSX del iPhone: el kernel Mach y la RAM – planetamac
Traducción: Oscar Reixa
[Leopard, Vista and the iPhone OS X Architecture]
[iPhone OS X Architecture: Disk, Shell, and Password Security]
[iPhone OS X Architecture: the BSD Unix Userland]
[iPhone OS X Architecture: Cocoa Frameworks and Mobile Mac Apps]
The iPhone Kernel.
Yes it’s Mach. Remember how many so-called experts insisted that Apple was stuck with Mach, but would move to the Linux kernel in a heartbeat if only it could, and perhaps as soon as Mach maven Avie Tevanian left Apple?
I countered the initial rumors that Apple would move to the Linux kernel last year, and repeated the same thing before WWDC 07, when the meme evolved into an imminent migration to Sun’s Solaris kernel.
There’s nothing wrong with Mach. In reality, it fits Apple’s needs quite well. If Apple were desperate to jump to some other OS architecture, the iPhone would certainly be the place to ditch its existing kernel and run with something else, just as Microsoft created a new kernel for Windows CE rather than using the NT kernel from its Windows NT/2000/XP/Vista desktop products.
[More Predictions for WWDC 2007: Solaris, Google, Surround: Mac OS X with the Solaris Kernel]
[The Microkernel Myth: What is Mach?]
[The Linux Kernel Myth: Much ado about Mach]
[OS X vs. WinCE: How iPhone Differs from Windows Mobile]
The iPhone’s Mach kernel uses the same I/O Kit architecture as Mac OS X, allowing Apple to reuse a lot of the mature code that is already proven to work on the Mac. The I/O Kit in Mach uses kexts or kernel extensions to add low level hardware support.
Because they plug into the kernel, it is critical that they are as solid and mature as possible. When software crashes outside of the kernel, it can usually be restarted. When something crashes within the kernel, it results in a kernel panic and the system becomes unusable and has to be rebooted. That’s bad.
Apple recommends against adding kernel extensions to Mac OS X unless there is no other way to accomplish the task at hand. That’s why people complain when they find third party software installing kexts without good reason; it frequently results in stability problems.
Apple has followed its own advice by using only two sets of kexts in the iPhone. The first sets up USB input, specifically for a user human interface device, and more specifically the multitouch screen:
The second pair of extensions relate to VPN or Virtual Private Networks. These extensions tie into the network stack to provide a way to securely tunnel encrypted traffic over a public network like the Internet.
Comparing the iPhone Kernel to the Mac’s.
The iPhone’s kernel and kexts are not identical to the Mac. For example, desktop Macs have kexts for the iSight camera, audio and video hardware, power and thermal management, and serial interfaces. The iPhone has no kext equivalents for any of these, despite having the hardware. The iPhone’s PPP.kext is also missing.
It appears that support for all of the hardware in the iPhone is built into the iPhone’s kernel, rather than being implemented as a kext. On the Mac, different models have different hardware, so a modular set of plugin is required. There is currently no variety in the hardware in shipping iPhones.
Having the I/O Kit architecture in place on the iPhone demonstrates Apple plans to expand hardware support in the future, and won’t have to resort to bolting on support for an extension mechanism after the fact. Because Mach and I/O Kit were built from the start to accommodate a wide range of hardware, the iPhone was able to inherit this functionality and use it as is without returning to the drawing board to build a “mobile OS kernel.”
Among the things accounted for in Mach is sophisticated power management, which intelligently powers up planes of devices as the system goes into standby or to sleep. That helps explain why the first generation of the iPhone has remarkable battery life, commonly twice that of comparable smartphones.
System RAM and Flash RAM.
Another significant difference between the iPhone and other smartphones relates to system RAM and Flash RAM storage. The pundits haven’t said anything about this, because it is embarrassing to other manufacturers, and it is apparently politically incorrect to point out that the iPhone blazes a new trail in technology.
On the iPhone, there are no physical disk drives, only flash RAM disk images, which work like standard DMG files on the Mac desktop. A desktop DMG file contains an archived copy of the contents of a drive. When mounted by Mac OS X, it behaves just like a physical drive, although it is faster because it doesn’t involve any moving parts; it resides entirely in RAM.
The iPhone can be restored through iTunes just like an iPod. This copies a clean pair of DMG files to the unit. The phone then mounts and runs OS X from the disk image directly from Flash RAM. Other mobiles–including Palm OS, Symbian, and Windows Mobile–have a similar “run from Flash RAM” architecture.
The main memory difference of the iPhone over existing mobiles is that most phones have 64 MB of RAM, 128 MB or less of Flash, and consume about 40 MB of their Flash with their OS and applications, leaving little storage for the user. They provide SD Flash RAM slots to add more space, but this is limited to 2 GB of expansion.
The iPhone has either 4096 MB (4 GB) or 8192 MB (8 GB) of Flash RAM installed. It consumes about 700 MB with OS X and its applications. It also contains 128 MB of system RAM used by the running applications. That’s twice the system RAM and 64 to 128 times the Flash RAM storage.
It’s also why the iPhone doesn’t need an SD card slot. It already has 2 – 4 times more Flash installed than users other phones can potentially add using SD Flash cards.
That helps explain why the iPhone can offer real Internet apps, and why Palm, Windows Mobile and Symbian are not going to catch up in functionality without entirely rewriting their mobile operating systems.
[Update: A number of readers asked about SDHD Cards, which offer more than 2 GB of Flash, but only work with new hardware specifically designed to support the SHDH standard. Earlier SD slot phones can’t use them. Only a few high end phones can use SDHD, and the N95 is one of them. Adding 8 GB to the N95 via SDHD is another ~ $250 over its $750 price tag, putting it at a very different price target than the iPhone.]
Where’s the Kernel?
One last thing that’s different about the kernel on the iPhone and the Mac: its location. On a Mac, the Mach kernel is loaded into RAM from the standard boot disk as the system starts up. Not only does it sit in plain sight, but the Mac’s kernel is also available as part of Apple’s Darwin open source project, so developers can peruse its code to better understand how it works.
The iPhone is running a related port of the Mac OS X kernel, but it is not open source. It’s also adapted to the ARM processor architecture, which requires modifications and optimizations for its unique peculiarities.
Take all the people on Earth who know a lot about Darwin and Apple’s Xnu Mach kernel, then isolate those who know much about ARM, and you have a group that could share a couple pizzas. That has implications for the iPhone’s security, as I note in the next article, which will look more at the iPhone’s core OS and security: iPhone OS X Architecture: Disk, Shell, and Password Security.
Like reading RoughlyDrafted? Share articles with your friends, link from your blog, and subscribe to my podcast! Submit to Reddit or Slashdot, or consider making a small donation supporting this site. Thanks!