Interview
What is an Operating System? with Anil Madhavapeddy
- Mirage OS Definition: A system written in pure OCaml that compiles high-level applications directly into a specialized binary known as a unikernel, rather than stopping at a standard executable.
- Unikernel Composition: Unlike traditional OSs that ship with 25 million lines of C code (Linux kernel), a unikernel includes only the specific drivers, protocol stacks, and file systems required by the application, resulting in a minimal, single-purpose OS.
- Compilation Specialization: The compiler utilizes source code and configuration files to specialize the application, emitting a full operating system that boots directly on hardware or hypervisors without a pre-existing kernel.
- Library Operating System Model: Mirage adopts a "library OS" architecture where the kernel is provided as a set of linkable libraries (e.g., TCP/IP, file systems) rather than a monolithic wrapper, sacrificing multi-user isolation for maximum hardware utilization and specialization.
- Historical Origins: Mirage OS originated 15 years ago as a "stunt" during Anil Madhavapeti's PhD research at Cambridge to rewrite network protocols in OCaml and verify if high-level languages could match C performance while enabling formal verification.
- Relationship to Xen: The project evolved organically from work on the Xen hypervisor, initially serving as a minimal testbed to exercise low-level Xen drivers before growing into a full unikernel framework.
- Adoption Scale: Mirage OS libraries and unikernels currently support tens of millions of daily active users, embedded in Docker for Mac/Windows, Tezos blockchain nodes, and government infrastructure projects.
- Docker for Mac Integration: Mirage OS powers the translation layer in Docker for Mac and Windows, translating POSIX file system semantics (APFS/HFS) to Linux container formats via OCaml network serialization, reportedly reducing support calls by ~99%.
- Language Choice Rationale: OCaml was selected for its Unix-like semantics, fast native compilation, and powerful module system, which allows interfaces (signatures) to be separated from implementations, enabling incremental evolution of hardware abstractions without breaking compatibility.
- Abstraction Evolution: The project's "Mirage" concept aims for abstractions to disappear into the standard, allowing systems to be ported between Linux sockets, bare metal, and hypervisors using the same high-level OCaml code.
- Security Benefits: Unikernels offer a smaller attack surface due to minimal code inclusion and lack of multi-user isolation, making them resistant to traditional buffer overflows and reducing the complexity of kernel patching.
- Multi-Language Interoperability: While primarily OCaml, Mirage supports multi-language stacks via Foreign Function Interface (FFI) and WebAssembly, allowing integration with Rust (e.g., Tezos) and Go (e.g., Docker tooling) provided memory models are carefully aligned.
- Climate Applications: Mirage is currently deployed in solar-powered sensor networks (e.g., Raspberry Pis, ESP32s) to reduce energy consumption by eliminating the heavy Linux runtime, enabling code execution on microcontrollers.
- Security Stunts: A 2015 "Bitcoin Piñata" experiment hid 10 BTC in a unikernel; despite hundreds of attacks and DDoS attempts over months, the system remained secure and rebooted in milliseconds due to its immutable nature.
- OCaml Labs Creation: Established at Cambridge University to support OCaml development, addressing the language's stagnation around 2005 by providing dedicated staff for compiler maintenance, tooling, and ecosystem coordination.
- Multi-Core Runtime: A long-running research project aimed at adding true multi-core parallelism to OCaml without breaking 25 years of backward compatibility or requiring a massive performance penalty for single-threaded code.
- Memory Model Innovation: The team developed the LDRF (Locally Data Race Free) memory model, a formal semantic that guarantees consistent behavior in the presence of data races, avoiding the undefined behavior of C++ and the complex temporal dependencies of Java.
- OCaml 5.0 Roadmap: The upcoming release (following 4.13) will expose a
Domainsinterface for parallel execution, ensuring minimal performance regression (<0.4% on ARM) for legacy code while enabling multi-core heap access. - Effect Systems and Concurrency: Future releases (5.1, 5.2) will introduce effect handlers to express concurrency directly in code, allowing application-specific scheduling (fibers) that is more flexible and performant than OS-level threading.
- EIO Library: A new effect-based I/O stack currently in development that abstracts platform-specific IO (io_uring, Grand Central Dispatch, IOCP) into direct-style OCaml code, competing with Rust and Go in performance.
- Academic-Industrial Bridge: Cambridge University's policy allows staff to retain IP, facilitating the transition of academic research (e.g., multi-core theory) into industrial-grade tools without the friction of typical university technology transfer agreements.
- Community Growth: OCaml Labs acts as "glue" for the ecosystem, coordinating efforts between industrial partners (Jane Street, Facebook) and academic institutions (INRIA) to maintain core tools like Dune, Merlin, and the compiler.