Kernel Design
A kernel is a privileged program that implements OS functionality. There are many types of kernels:
- Monolithic: Single black box with all OS functionality (e.g. Linux).
- Microkernels: As little functionality as possible (e.g. L4, Mach).
- Hybrid kernels: Mix of monolithic and microkernels (e.g. Windows NT, Mac OS X).
1. Monolithic Kernels
Kernel is a single executable binary with its own address space. Syscalls can be carried out simply by putting parameters on the stack and executing a special instruction which diverts control to the kernel. This is the most popular kernel design.
Advantages:
- Efficient calls within kernels.
- Easier to write kernel components due to shared memory.
Disadvantages:
- Complex design with lots of interactions.
- No protection between kernel components.
2. Microkernels
Kernel is a minimal set of funcionality in user-level servers. Kernel does direct device IO, manipulates memory page tables and allocate memory, interact with interrupt controller to support process scheduling, and provides a communication mechanism between processes called inter-process communication (IPC).
A server is a user-level process that provides a service to other processes. Servers can be added or removed without affecting the kernel. Servers can be restarted without affecting the kernel.
Advantages:
- Kernel is simpler and less error prone.
- Servers can have clean interfaces.
- Servers can crash & restart without bringing the kernel down.
Disadvantages:
- High IPC overhead causing a performance hit.
3. Hybrid Kernels
Kernel combines features of both monolithic and microkernels. This is often just a design philosophy and not a strict classification.
Advantages:
- More structured design.
Disadvantages:
- Performance penalty for user level servers.
4. Case Study: Linux
UNIX was invented at the same time as the C programming language. UNIX is a simple OS with a standardised API called POSIX. The linux kernel is a derivative of UNIX. It features a monolithic kernel design.
Linux uses syscalls implemented by putting arguments in registers and then calling into the kernel that executes the kernel code. Linux supports a rich set of programs through the GNU project (e.g. bash, compilers, editors, desktop environments, utility programs, etc.).
Interrupt handlers are the primary way to interact with devices. An interrupt handler is typically written in assembly as it is close to the hardware and needs to be efficiently implemented.
Linux supports static in-kernel components and dynamically loadable modules.
5. Case Study: Windows
The windows kernel is called Windows NT. Subsequent versions of Windows are all based on this kernel. NTOS provides syscalls, but the kernel is built around the concept of user-mode dynamic libraries (DLLs). These libraries may be loaded dynamically, allowing large parts of the kernel to implement OS services in a modular fashion. NTOS is loaded from ntoskrnl.exe. It consists of two layers:
- Executive: most of the services.
- Kernel: thread scheduling and synchronization, traps, interrupt handlers and CPU management.
Hardware Abstraction Layer (HAL) abstracts out DMA operations, BIOS config and CPU types. Device drivers are loaded into kernel memory. They are dynamically linked against NTOS and HAL layers.
NT is structured like a microkernel, but puts many services in the kernel for performance reasons. It is a hybrid kernel.
In user-mode, the windows kernel can give you many types of API: Win32, POSIX, .NET, etc.
Most of the components run in the same kernel address space which allows for a more efficient implementation.