ffirs.indd ii
ffirs.indd ii
9/29/2012 5:55:03 PM
9/29/2012 5:55:03 PM
MAC OS® X AND iOS INTERNALS
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxv
PART I
CHAPTER 1
FOR POWER USERS
Darwinism: The Evolution of OS X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
CHAPTER 2
E Pluribus Unum: Architecture of OS X and iOS . . . . . . . . . . . . . . . . . . . . .17
CHAPTER 3 On the Shoulders of Giants: OS X and iOS Technologies . . . . . . . . . . . 55
CHAPTER 4
Parts of the Process: Mach-O, Process, and Thread Internals . . . . . . . . 91
CHAPTER 5 Non Sequitur: Process Tracing and Debugging . . . . . . . . . . . . . . . . . . . .147
CHAPTER 6
Alone in the Dark: The Boot Process: EFI and iBoot . . . . . . . . . . . . . . . 183
CHAPTER 7
The Alpha and the Omega — launchd . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
PART II THE KERNEL
CHAPTER 8
Some Assembly Required: Kernel Architectures . . . . . . . . . . . . . . . . . . 261
CHAPTER 9
From the Cradle to the Grave — Kernel Boot and Panics . . . . . . . . . . . 299
CHAPTER 10 The Medium Is the Message: Mach Primitives . . . . . . . . . . . . . . . . . . . . 343
CHAPTER 11
Tempus Fugit — Mach Scheduling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
CHAPTER 12 Commit to Memory: Mach Virtual Memory . . . . . . . . . . . . . . . . . . . . . . . 447
CHAPTER 13 BS”D — The BSD Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501
CHAPTER 14 Something Old, Something New: Advanced BSD Aspects . . . . . . . . . 539
CHAPTER 15 Fee, FI-FO, File: File Systems and the VFS . . . . . . . . . . . . . . . . . . . . . . . 565
CHAPTER 16 To B (-Tree) or Not to Be — The HFS+ File Systems . . . . . . . . . . . . . . . . 607
CHAPTER 17 Adhere to Protocol: The Networking Stack . . . . . . . . . . . . . . . . . . . . . . . 649
CHAPTER 18 Modu(lu)s Operandi — Kernel Extensions . . . . . . . . . . . . . . . . . . . . . . . . . 711
CHAPTER 19 Driving Force — I/O Kit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 737
APPENDIX
Welcome to the Machine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 773
INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793
ffirs.indd i
ffirs.indd i
9/29/2012 5:55:02 PM
9/29/2012 5:55:02 PM
ffirs.indd ii
ffirs.indd ii
9/29/2012 5:55:03 PM
9/29/2012 5:55:03 PM
Mac OS® X and iOS Internals
TO THE APPLE’S CORE
Jonathan Levin
ffirs.indd iii
ffirs.indd iii
9/29/2012 5:55:03 PM
9/29/2012 5:55:03 PM
Mac OS® X and iOS Internal
Published by
John Wiley & Sons, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2013 by Jonathan Levin
Published by John Wiley & Sons, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-1-11805765-0
ISBN: 978-1-11822225-6 (ebk)
ISBN: 978-1-11823605-5 (ebk)
ISBN: 978-1-11826094-4 (ebk)
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means,
electronic, mechanical, photocopying, recording, scanning, or otherwise, except as permitted under Sections 107 or 108
of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization
through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA
01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permis-
sions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008,
or online at http://www.wiley.com/go/permissions.
Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with
respect to the accuracy or completeness of the contents of this work and specifi cally disclaim all warranties, including
without limitation warranties of fi tness for a particular purpose. No warranty may be created or extended by sales or
promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is
sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional
services. If professional assistance is required, the services of a competent professional person should be sought. Neither
the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is
referred to in this work as a citation and/or a potential source of further information does not mean that the author or the
publisher endorses the information the organization or Web site may provide or recommendations it may make. Further,
readers should be aware that Internet Web sites listed in this work may have changed or disappeared between when this
work was written and when it is read.
For general information on our other products and services please contact our Customer Care Department within the
United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with
standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media
such as a CD or DVD that is not included in the version you purchased, you may download this material at
http://booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com.
Library of Congress Control Number: 2011945020
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox Programmer to Programmer, and related trade dress are
trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affi liates, in the United States and other coun-
tries, and may not be used without written permission. Mac OS is a registered trademark of Apple, Inc. All other trade-
marks are the property of their respective owners. John Wiley & Sons, Inc., is not associated with any product or vendor
mentioned in this book.
ffirs.indd iv
ffirs.indd iv
9/29/2012 5:55:06 PM
9/29/2012 5:55:06 PM
To Steven Paul Jobs: From Mac OS’s very fi rst
incarnation, to the present one, wherein the legacy of
NeXTSTEP still lives, his relationship with Apple is
forever entrenched in OS X (and iOS). People focus on
his effect on Apple as a company. No less of an effect,
though hidden to the naked eye, is on its architecture.
I resisted the pixie dust for 25 years, but he
fi nally made me love Mac OS... Just as soon as I got
my shell prompt.
— Jonathan Levin
ffirs.indd v
ffirs.indd v
9/29/2012 5:55:07 PM
9/29/2012 5:55:07 PM
CREDITS
ACQUISITIONS EDITOR
Mary James
SENIOR PROJECT EDITOR
Adaobi Obi Tulton
DEVELOPMENT EDITOR
Sydney Argenta
TECHNICAL EDITORS
Arie Haenel
Dwight Spivey
PRODUCTION EDITOR
Christine Mugnolo
COPY EDITORS
Paula Lowell
Nancy Rapoport
EDITORIAL MANAGER
Mary Beth Wakefi eld
FREELANCER EDITORIAL MANAGER
Rosemarie Graham
ASSOCIATE DIRECTOR OF MARKETING
David Mayhew
MARKETING MANAGER
Ashley Zurcher
BUSINESS MANAGER
Amy Knies
PRODUCTION MANAGER
Tim Tate
VICE PRESIDENT AND EXECUTIVE GROUP
PUBLISHER
Richard Swadley
VICE PRESIDENT AND EXECUTIVE PUBLISHER
Neil Edde
ASSOCIATE PUBLISHER
Jim Minatel
PROJECT COORDINATOR, COVER
Katie Crocker
PROOFREADER
James Saturnio, Word One New York
INDEXER
Robert Swanson
COVER DESIGNER
Ryan Sneed
COVER IMAGE
© Matt Jeacock / iStockPhoto
ffirs.indd vi
ffirs.indd vi
9/29/2012 5:55:07 PM
9/29/2012 5:55:07 PM