About This eBook
ePUB is an open, industry-standard format for eBooks. However, support of ePUB and its many
features varies across reading devices and applications. Use your device or app settings to
customize the presentation to your liking. Settings that you can customize often include font,
font size, single or double column, landscape or portrait mode, and figures that you can click or
tap to enlarge. For additional information about the settings and features on your reading device
or app, visit the device manufacturer’s Web site.
Many titles include programming code or configuration examples. To optimize the presentation
of these elements, view the eBook in single-column, landscape mode and adjust the font size to
the smallest setting. In addition to presenting code and configurations in the reflowable text
format, we have included images of the code that mimic the presentation found in the print
book; therefore, where the reflowable format may compromise the presentation of the code
listing, you will see a “Click here to view code image” link. Click the link to view the print-fidelity
code image. To return to the previous page viewed, click the Back button on your device or app.
2
The Design and Implementation of the
FreeBSD®
Operating System
Second Edition
Marshall Kirk McKusick
George V. Neville-Neil
Robert N.M. Watson
Upper Saddle River, NJ • Boston • Indianapolis • San Francisco
New York • Toronto • Montreal • London • Munich • Paris • Madrid
Capetown • Sydney • Tokyo • Singapore • Mexico City
UNIX is a registered trademark of X/Open in the United States and other countries. FreeBSD
and the FreeBSD logo used on the cover of this book are registered and unregistered trademarks
of the FreeBSD Foundation and are used by Pearson Education with the permission of the
FreeBSD Foundation. Many of the designations used by manufacturers and sellers to
distinguish their products are claimed as trademarks. Where those designations appear in this
book, and Pearson was aware of a trademark claim, the designations have been printed with
initial capital letters or in all capitals.
The authors and publisher have taken care in the preparation of this book, but make no
expressed or implied warranty of any kind and assume no responsibility for errors or omissions.
No liability is assumed for incidental or consequential damages in connection with or arising out
of the use of the information or programs contained herein.
For information about buying this title in bulk quantities, or for special sales opportunities
(which may include electronic versions; custom cover designs; and content particular to your
business, training goals, marketing focus, or branding interests), please contact our corporate
sales department at corpsales@pearsoned.com or (800) 382-3419.
For government sales inquiries, please contact governmentsales@pearsoned.com.
3
For questions about sales outside the United States, please contact
international@pearsoned.com
Visit us on the Web: informit.com/aw
Library of Congress Cataloging-in-Publication Data
McKusick, Marshall Kirk.
The design and implementation of the FreeBSD operating system / Marshall
Kirk McKusick, George V. Neville-Neil, Robert N. M. Watson.
pages cm
Includes bibliographical references and index.
ISBN-13: 978-0-321-96897-5 (hardcover : alk. paper)
ISBN-10: 0-321-96897-2 (hardcover : alk. paper)
1. FreeBSD. 2. Free computer software. 3. Operating systems (Computers)
I. Neville-Neil, George V. II. Watson, Robert N. M. III. Title.
QA76.774.F74M35 2014
005.4’32—dc23 2014020072
Copyright © 2015 by Pearson Education, Inc.
All rights reserved. Printed in the United States of America. This publication is protected by
copyright, and permission must be obtained from the publisher prior to any prohibited
reproduction, storage in a retrieval system, or transmission in any form or by any means,
electronic, mechanical, photocopying, recording, or likewise. To obtain permission to use
material from this work, please submit a written request to Pearson Education, Inc.,
Permissions Department, One Lake Street, Upper Saddle River, New Jersey 07458, or you may
fax your request to (201) 236-3290.
ISBN-13: 978-0-321-96897-5
ISBN-10: 0-321-96897-2
Text printed on recycled and acid-free paper at Courier in Westford, Massachusetts.
First Printing, September 2014
4
Dedication
This book is dedicated to the BSD community.
Without the contributions of that community’s members,
there would be nothing about which to write.
5
Table of Contents
Preface ............................................................................................................................................ 12
About the Authors ......................................................................................................................... 21
Part I: Overview ..................................................................................................... 23
Chapter 1. History and Goals ...................................................................................................... 23
1.1 History of the UNIX System............................................................................................ 23
1.2 BSD and Other Systems .................................................................................................. 28
1.3 The Transition of BSD to Open Source .......................................................................... 30
1.4 The FreeBSD Development Model ................................................................................. 35
Chapter 2. Design Overview of FreeBSD.................................................................................... 44
2.1 FreeBSD Facilities and the Kernel ................................................................................. 44
2.2 Kernel Organization ........................................................................................................ 46
2.3 Kernel Services ................................................................................................................ 49
2.4 Process Management ...................................................................................................... 50
2.5 Security ............................................................................................................................ 53
2.6 Memory Management..................................................................................................... 60
2.7 I/O System Overview ...................................................................................................... 64
2.8 Devices ............................................................................................................................. 69
2.9 The Fast Filesystem ........................................................................................................ 70
2.10 The Zettabyte Filesystem ...............................................................................................75
2.11 The Network Filesystem ................................................................................................ 76
2.12 Interprocess Communication ........................................................................................ 77
2.13 Network-Layer Protocols .............................................................................................. 78
2.14 Transport-Layer Protocols ............................................................................................ 79
2.15 System Startup and Shutdown ..................................................................................... 79
Chapter 3. Kernel Services .......................................................................................................... 84
3.1 Kernel Organization ........................................................................................................ 84
3.2 System Calls ..................................................................................................................... 89
6
3.3 Traps and Interrupts ........................................................................................................ 91
3.4 Clock Interrupts .............................................................................................................. 93
3.5 Memory-Management Services ..................................................................................... 98
3.6 Timing Services ............................................................................................................. 102
3.7 Resource Services .......................................................................................................... 104
3.8 Kernel Tracing Facilities ................................................................................................ 107
Part II: Processes ...................................................................................................117
Chapter 4. Process Management ............................................................................................... 117
4.1 Introduction to Process Management .......................................................................... 117
4.2 Process State .................................................................................................................. 120
4.3 Context Switching ......................................................................................................... 128
4.4 Thread Scheduling .........................................................................................................144
4.5 Process Creation ............................................................................................................. 157
4.6 Process Termination ...................................................................................................... 159
4.7 Signals ............................................................................................................................. 161
4.8 Process Groups and Sessions ........................................................................................169
4.9 Process Debugging ......................................................................................................... 175
Chapter 5. Security ..................................................................................................................... 183
5.1 Operating-System Security ........................................................................................... 184
5.2 Security Model ................................................................................................................185
5.3 Process Credentials ....................................................................................................... 188
5.4 Users and Groups ........................................................................................................... 191
5.5 Privilege Model ...............................................................................................................194
5.6 Interprocess Access Control .......................................................................................... 197
5.7 Discretionary Access Control ........................................................................................199
5.8 Capsicum Capability Model........................................................................................... 213
5.9 Jails ................................................................................................................................ 220
5.10 Mandatory Access-Control Framework ..................................................................... 225
5.11 Security Event Auditing ............................................................................................... 242
5.12 Cryptographic Services................................................................................................ 248
5.13 GELI Full-Disk Encryption ......................................................................................... 256
7
Chapter 6. Memory Management ............................................................................................. 266
6.1 Terminology ................................................................................................................... 266
6.2 Overview of the FreeBSD Virtual-Memory System .................................................... 272
6.3 Kernel Memory Management ...................................................................................... 276
6.4 Per-Process Resources .................................................................................................. 293
6.5 Shared Memory ............................................................................................................. 299
6.6 Creation of a New Process ............................................................................................ 309
6.7 Execution of a File .......................................................................................................... 313
6.8 Process Manipulation of Its Address Space ................................................................. 314
6.9 Termination of a Process ............................................................................................... 317
6.10 The Pager Interface ..................................................................................................... 318
6.11 Paging ........................................................................................................................... 328
6.12 Page Replacement ....................................................................................................... 344
6.13 Portability..................................................................................................................... 355
Part III: I/O System ............................................................................................... 372
Chapter 7. I/O System Overview .............................................................................................. 372
7.1 Descriptor Management and Services ......................................................................... 373
7.2 Local Interprocess Communication ............................................................................. 393
7.3 The Virtual-Filesystem Interface ................................................................................. 399
7.4 Filesystem-Independent Services ................................................................................ 406
7.5 Stackable Filesystems ....................................................................................................414
Chapter 8. Devices ..................................................................................................................... 425
8.1 Device Overview ............................................................................................................ 425
8.2 I/O Mapping from User to Device ............................................................................... 432
8.3 Character Devices ......................................................................................................... 435
8.4 Disk Devices .................................................................................................................. 440
8.5 Network Devices............................................................................................................ 444
8.6 Terminal Handling ....................................................................................................... 449
8.7 The GEOM Layer ........................................................................................................... 460
8.8 The CAM Layer ............................................................................................................. 468
8.9 Device Configuration .................................................................................................... 472
8