Tuesday, February 19, 2008

Design of Mach

UNIX kernel - "dumping ground for virtually every new feature or facility"
- Richard Rashid of Carnegie Mellon University (CMU)

Based on the Mach 3, Mac OS X should perform well in:
  • Memory management
  • Preemptive / Real-time operating system
  • System call redirection - integration with other operating systems, e.g. BSD
Mach's implementation used 4.3BSD as the starting code base. As Mach evolved, portions of the BSD kernel were replaced by their Mach equivalents, and various new components were added.

1984
The Mach project started in 1984 with several specific goals to achieve:
  • Provide full support for multiprocessing
  • Support diverse architectures, include shared memory access scheme such as Non-uniform Memory Access (NUMA) and Non-Remote Memory Access (NORMA)
  • Support transparent and seamless distributed operation
  • Reduce the number of features in the kernel. Although system programmers will have a very small number of abstractions to work with, the abstractions would be general enough to allow several operating system to be implemented on top of Mach
  • Provide compatibility with UNIX
Initially there were four basic abstraction in the kernel:
  1. A task is a container for the resources of one or more threads. Examples of resources include virtual memory (through memory object), ports, processors, and so on.
  2. A thread is a basic unit of execution in a task. A task provides an execution environment for its threads, i.e., threads of task share its resources includes the program counter and various registers.
  3. A port is an in-kernl message queue with capabilities. Ports form the basis for Mach's IPC facilities.
  4. A message is a collection of data that threads in the same or different tasks can send to each other using ports.
Memory object is a basic Mach abstraction too. It can be considered as a container for data (including file data) mapped into a task's address space. Mach requires a paged MMU (PMMU). It provides an interface to the machine-dependent memory-management unit (MMU) facilities. Its virtual memory subsystem was designed to support large, spare virtual address spaces and was integrated with IPC. Regions of memory can be allocated from anywhere in the address space. Memory could be shared for reading and writing in a structured manner. Copy-on-write techniques were used both to optimize copy operations and for sharing physical memory between tasks. Generalized memory object abstraction allows external memory pagers to handle page faults and page-out data requests. The resource of target data could even reside on another machine.

The virtual memory architectures of FreeBSD is based on Mach

1993
Although Mach was designed to run as the operating system for various operating systems, Mach 3 was the first true microkernel version. The Mach 3 project was started at CMU and continued by OSF (Open Software Foundation). In Mach 3, BSD ran as a user-space task, with only fundamental features being provided by the Mach kernel. Changes and improvements in Mach 3 include the following:
  • Kernel preemption and a real-time scheduling framework to provide real-time support
  • Low-level device support wherein devices were presented as ports to which data or control messages could be sent. It supports both synchronous and asynchronous I/O.
  • A completely rewritten IPC-implementation
  • System call redirection - allow a set of system calls to be handled by user-space code running within the calling task
  • Use of continuations, a kernel facility that gives a thread the option to block by specifying a function (the continuation function) that is called when the threads runs again
The intended benefits of microkernel-based operating system were offset by the real-life performance problems such as listed at below:
  • The cost of maintaining separate protection domains, including the cost context switching from one domain to anther
  • The cost of kernel entry and exit code
  • Data copies in MIG-generated stub routines. Mach Interface Generator (MIG) generates client stubs for Mach IPC - RPC code for client-server-style Mach IPC from specification files
  • The use of semantically powerful but implementation-heavy IPC mechanisms, even for RPC on the same machine.