Reading the source
Several Mach code bases are public. They have diverged, so it helps to know which one you are reading.
Where the code is
| Tree | Use it for |
|---|---|
| XNU | The Mach code that ships today. Apple publishes it on GitHub. The osfmk directory holds the Mach-based subsystems and bsd holds the BSD layer. It is large and carries a lot of Apple-specific work. |
| GNU Mach | A smaller tree, from the Mach 4 line, used by the GNU Hurd. A better place to see a Mach kernel without Apple's additions. MIG is packaged separately from it. |
| CMU Mach project | The original project page, with links to sources and binaries, manuals, and the Mach-US multi-server work. |
| Mach 4 at Utah | The project that GNU Mach descends from. |
Where things are in XNU
The directory names under osfmk follow the Mach subsystems:
| Directory | Contents |
|---|---|
osfmk/ipc | Ports, port rights and messages. Files include ipc_port.c, ipc_right.c, ipc_space.c, ipc_kmsg.c, ipc_pset.c and mach_msg.c. |
osfmk/kern | Tasks, threads, scheduling and the core kernel services. |
osfmk/vm | Virtual memory. |
osfmk/mach | The public interface: headers such as message.h and port.h, and MIG interface definitions such as mach_port.defs. |
osfmk/default_pager | The default pager, which backs anonymous memory. |
osfmk/i386, osfmk/x86_64, osfmk/arm64 | Machine-dependent code. |
A way in
Read the headers first. osfmk/mach/message.h shows what a message looks like, and osfmk/mach/port.h lists the kinds of port rights. Then read osfmk/ipc/ipc_port.c to see how the kernel represents a port. After that, follow a call such as mach_msg through osfmk/ipc/mach_msg.c.
To see how the interface definitions turn into code, read a .defs file next to the MIG description in the MIG paper. The concepts page explains the terms used above.