Inside the Mac OS X Kernel


Published on

A presentation by Lucy, at the 24th Chaos Communication Congress, Berlin, 28 Dec 2007.

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Inside the Mac OS X Kernel

  1. 1. Inside the Mac OS XKernelDebunking Mac OS Mythslucy28 Dec 2007
  2. 2. microkernelMachFreeBSDC++64 bitOpen SourceUNIX
  3. 3. microkernelMachFreeBSDC++64 bitOpen SourceUNIXOn the Mac, this kernel is called MachPaul Thurrott,
  4. 4. microkernelMachFreeBSDC++64 bitOpen SourceUNIX its stable open-source core based on FreeBSD 5.0
  5. 5. microkernelMachFreeBSDC++64 bitOpen SourceUNIXMac OS X for UNIX UsersThe power of UNIX with the simplicity of Macintosh.FeaturesOpen source, UNIX-based foundation• SMP-optimized kernel based on FreeBSD 5and Mach 3.0• 64-bit virtual memory based on the LP64model• Standard libraries and utilities for easy portingof Linux, UNIX, and POSIX source code• Hand-tuned, standards-compliant scalar andvector math libraries• UNIX GUI support via native toolkits andincluded X11 server• Open source code available via the DarwinprojectStandards-based networking• Complete IP-based architecture supportingIPv4, IPv6, and L2TP/IPSec VPN• Rich zero-configuration discovery and namingvia Bonjour and Dynamic DNS• Interoperable file serving via NFS, AFP,SMB/CIFS, and FTP• Powerful Apache services (httpd, DAV, PHP)• Open Directory services built on LDAP andKerberos for single sign-onComprehensive UNIX user environment• Standards-based graphics built on PDF(Quartz), OpenGL, H.264, and MPEG-4(QuickTime)• Xterm-compliant Terminal integrated withAqua user interface• Choice of shells (bash, ksh, csh) and scriptinglanguages (Perl, Python, Tcl, Ruby)• Enhanced with Spotlight searching, Xgridqueuing, and Automator workflows• Comprehensive UNIX/Linux utilities (emacs,vim, gnutar, make)• Xcode 2 developer tools with GCC 4.0 foroptimized code generation and ISO/IEC14882:2003 C++ supportTechnology BriefMac OS X for UNIX UsersMac OS X version 10.4 Tiger combines a robust and open UNIX-based foundationwith the richness and usability of the Mac interface, bringing UNIX technology and64-bit power to the mass market. Apple has made open source and standards a keypart of its strategy to deliver an industrial-strength operating system that is bothinnovative and easy to use.There are over 15 million Mac OS X users—scientists, animators, developers, systemadministrators, and more—making Mac OS X the most widely used UNIX-baseddesktop operating system. In addition, Mac OS X is the only UNIX-based environmentthat natively runs Microsoft Office, Adobe Photoshop, and thousands of other consumerapplications—all side by side with traditional command-line, X11, and Java applications.Mac OS X is also the foundation for Mac OS X Server, which makes open source softwareeasy to administer. Tight integration with Apple hardware, from the sleek PowerBookG4 to the award-winning Xserve G5, is making Mac OS X the platform of choice for anemerging generation of UNIX users. foundation
  6. 6. microkernelMachFreeBSDC++64 bitOpen SourceUNIXSteve Jobs,WWDC 2007“Leopard is 64 bit top to bottom.”
  7. 7. microkernelMachFreeBSDC++64 bitOpen SourceUNIXMiles Nordin, Microkernel Dooms Mac OS X to Lag Linux in Performance
  8. 8. Mac OSMacintosh1985 1990 1995 2000128 KBM68K3.5”single tasking
  9. 9. Macintosh1985 1990 1995 2000128 KBM68K3.5”single taskingSystem 7PowerPCmultitasking
  10. 10. Mac OSMacintosh1985 1990 1995 2000128 KBM68K3.5”single taskingSystem 7PowerPCmultitaskingCopland ?
  11. 11. JobsJobs1985AppleNeXT
  12. 12. JobsJobs1985Gassée1991GasséeAppleNeXTBe
  13. 13. JobsJobs1985Gassée1991Gassée1996?AppleNeXTBe+ ?
  14. 14. +JobsJobs1985Gassée1991Gassée1996JobsAppleNeXTBe
  15. 15.
  16. 16.
  17. 17. NEXTSTEPNeXTNEXTSTEP 1.0UNIX basepreemptive multitaskingmemory protectionDisplay PostScript68KNEXTSTEP 3.1i386PA-RISCSPARC Apple acquisitionRhapsodyPowerPCMac OS X 10.01985 1990 1995 2000 2005i386
  18. 18. XNUMach BSD I/O-KitX is not UNIX
  19. 19. Monolithic Kernellibckernel modeuser modeApp App ApphardwareMM schedHW IPCVFSAPIsecdevnet
  20. 20. Microkernellibckernel modeuser modeApp App ApphardwareMM schedHW IPCVFSAPIsecdevnet
  21. 21. BSD Single ServerBSDlibckernel modeuser modeApp App ApphardwareMM schedHW IPCVFSAPIsecdevnet
  22. 22. 430write();userkernelwrite() {}monolithic
  23. 23. ipc();430ipc();userkernelipc() {}microkernel
  24. 24. ipc();430ipc();userkernelipc() {}microkernelBSDwrite() {}
  25. 25. ipc();430ipc();userkernelipc() {}microkernel
  26. 26. BSD Single ServerMM schedHW IPCVFSAPIsecdevlibckernel modeuser modeApp App AppBSDhardwarenet
  27. 27. Co-locationMM schedHW IPCVFSAPIsecdevlibckernel modeuser modeApp App AppBSDhardwarenet
  28. 28. Mach430430430task task task40RAMROMVGAI/OI/OI/O• memorymanagement• scheduling• inter-processcommunication
  29. 29. Mach IPC430430
  30. 30. Mach InterfaceGenerator (MIG)430430main() {func(1,2,3);}func(a,b,c) {}MIGgeneratedcodefunc(a,b,c) {msg.a = a;msg.b = b;msg.c = c;send(msg);}receive(msg);a = msg.a;b = msg.b;c = msg.c;func(a,b,c);
  31. 31. signalPOSIXthreadprocessMach + BSDtask thread exceptionMachBSDschedulerMM IPCVFSBSDsockets/devsyscallsint/exckerneluserfork() open() ...machmessagesyscallsint 0x80%eax>0 <0
  32. 32. Driversdriver.c driver2.c
  33. 33. Driversdriver.c#ifdef#ifdef#ifdef#ifdef#ifdef
  34. 34. I/
  35. 35. <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" ""><plist version="1.0"><dict> <key>CFBundleDevelopmentRegion</key> <string>English</string> <key>CFBundleExecutable</key> <string>msdosfs</string> <key>CFBundleIdentifier</key> <string></string> <key>CFBundleInfoDictionaryVersion</key> <string>6.0</string> <key>CFBundleName</key> <string>msdosfs</string> <key>CFBundlePackageType</key> <string>KEXT</string> <key>CFBundleShortVersionString</key> <string>1.5</string> <key>CFBundleSignature</key> <string>????</string> <key>CFBundleVersion</key> <string>1.5</string> <key>IOKitPersonalities</key> <dict/> <key>OSBundleLibraries</key> <dict> <key></key> <string>8.0.0</string> <key></key> <string>9.0.0d3</string> <key></key> <string>8.0.0</string> <key></key> <string>8.0.0</string> </dict></dict></plist>Info.plist<key>OSBundleLibraries</key> <dict> <key></key> <string>8.0.0</string> <key></key> <string>9.0.0d3</string> <key></key> <string>8.0.0</string> <key></key> <string>8.0.0</string> </dict>
  36. 36. BootingPC/BIOSMBR?BIOS640KBRAMVideo1 MB0 MB
  37. 37. BootingEFIGPT EFI2 GBRAMVideo4 GB0 GBHFS+HFS+/System/LibraryCoreServices/boot.efiCaches/
  38. 38.
  39. 39. libSystem.dylib: Mach-O universal binary with 4 architecturesMach-O$ file libSystem.dyliblibSystem.dylib (for architecture ppc7400): Mach-O dynamically linked shared library ppclibSystem.dylib (for architecture ppc64): Mach-O 64-bit dynamically linked shared library ppc64libSystem.dylib (for architecture i386): Mach-O dynamically linked shared library i386libSystem.dylib (for architecture x86_64): Mach-O 64-bit dynamically linked shared library x86_64/usr/lib/usr/lib64C:WINDOWSSYSTEMC:WINDOWSSYSTEM32C:WINDOWSSYSTEM64/mach_kernel: Mach-O universal binary with 2 architectures/mach_kernel (for architecture i386):Mach-O executable i386/mach_kernel (for architecture ppc):Mach-O executable ppc$ file /mach_kernel
  40. 40. RosettahardwareXNUuserkernellibSystemlibA libB libCppci386hardwareXNUlibSystemlibBPowerPC applibCRosetta (byte swapping)Rosetta (byte swapping)PowerPC applibARosetta (dynarec)Rosetta(dynarec)
  41. 41. Architecturesi386i386PA-RISCSPARCPowerPC(i386)68K1990 1995 2000 2005
  42. 42. 3/1 SplitkerneluserWindows, Linux, ...4 GB3 GB0 GBuseruseruser
  43. 43. userkerneluserkernel4/4 SplitMac OS X Intel4 GB0 GBswitcher
  44. 44. userkerneluser4/4 SplitMac OS X Intel4 GB0 GBswitcher
  45. 45. x86_64kernelintr handler2^4704 GBuserkernelkernel
  46. 46. x86_64kernelintr handler2^4704 GBuser
  47. 47. x86_64userintr handler2^4704 GB
  48. 48. iPhone• ARM• Custom Bootloader• No real KEXTs• Kernel Cache
  49. 49. What Makes XNU Great
  50. 50. What Makes XNU Great#8dtrace
  51. 51. What Makes XNU Great#7kernel cache
  52. 52. What Makes XNU Great#6Separation between Mach, BSD, I/O-Kit
  53. 53. What Makes XNU Great#5POSIX Conformance
  54. 54. What Makes XNU Great#4Mach Message API
  55. 55. What Makes XNU Great#3I/O-Kit
  56. 56. What Makes XNU Great#2Stable KEXT ABI
  57. 57. What Makes XNU Great#1Universal Binaries
  58. 58. Buzzwordsagain
  59. 59. Mach• The Mac OS X kernel is not “Mach”.• The Mac OS X kernel is XNU.• XNU consists of Mach, BSD and I/O-Kit.
  60. 60. Microkernel• The OS X kernel is not a microkernel.• The Mach codebase can be used as amicrokernel.• XNU is a monolithic kernel.• BSD and most drivers are in kernel mode.
  61. 61. FreeBSD Kernel• The Mac OS X kernel is not based on theFreeBSD kernel.• XNU contains some FreeBSD code.
  62. 62. C++• XNU is not written in C++.• I/O-Kit is written in Embedded C++.• Mach and BSD are written in C.
  63. 63. 64 bit• The Mac OS X kernel is not 64 bit.• It supports 64 bit user land applications.• The kernel code is 32 bit with tiny 64 bitparts for user support.
  64. 64. Open Source• The Mac OS X kernel and most of theUNIX bits are Open Source.• But there is no live repository.• Some code is missing.• But it can be compiled into a workingsystem.
  65. 65. • Mac OS X is “UNIX”.• But only since Leopard.• It passed the POSIX Conformance Test.• It may use the “UNIX” trademark.• It does not contain AT&T UNIX code.
  66. 66. Awesome• But Mac OS X is awesome.• That’s a fact.lucy