Kernel Overview
Key Features:
- Modern 64-bit architecture (x86-64, aarch64, riscv64)
- Pre-emptive multi-threading with SMP support
- Virtual memory with hardware protection
- Comprehensive device driver framework
- Full network stack implementation
- Multiple filesystem support
Directory Structure
The kernel source is organized by subsystem:Core Subsystems
Process and Thread Management
Located inKernel/Tasks/, this subsystem handles:
Process Management
Process Management
- Process Structure: Each process has isolated address space, file descriptor table, and security context
- Fork/Exec Model: Standard Unix process creation via
fork()andexecve() - Copy-on-Write: Efficient memory sharing between parent and child processes
- Process Groups: Session and process group management
- Capabilities: Limited privilege model for fine-grained access control
Thread Management
Thread Management
- Pre-emptive Scheduling: Fair scheduler with priority support
- SMP Support: Multi-processor thread distribution
- Thread Primitives: Kernel threads, user threads, and futexes
- Synchronization: Mutexes, semaphores, and spinlocks
- Sleep/Wake: Efficient thread blocking and wakeup mechanisms
Memory Management
The memory subsystem inKernel/Memory/ provides:
Memory Protection Features:
- NX (No-Execute) bit enforcement
- SMEP (Supervisor Mode Execution Prevention)
- SMAP (Supervisor Mode Access Prevention)
- KASLR (Kernel Address Space Layout Randomization)
- W^X (Write XOR Execute) policy
Filesystem Layer
The VFS (Virtual Filesystem) architecture inKernel/FileSystem/:
- VFS Core
- Supported Filesystems
- Inode: Represents a filesystem object (file, directory, device)
- Custody: Represents a cached path in the filesystem tree
- FileDescription: Open file handle with position and flags
- Mount: Filesystem mount point management
Device Driver Framework
Device drivers inKernel/Devices/ follow a class hierarchy:
Network Stack
The network implementation inKernel/Net/ includes:
Network Layers
Network Layers
Link Layer:
- Network device abstraction
- Ethernet frame handling
- ARP (Address Resolution Protocol)
- IPv4 routing and forwarding
- IPv6 support (growing)
- ICMP (ping, etc.)
- TCP with congestion control
- UDP datagram support
- Socket abstraction
- BSD socket API
- Unix domain sockets
- Socket options and control
Network Devices
Network Devices
Supported network adapters:
- E1000 (Intel Gigabit Ethernet)
- RTL8139 (Realtek)
- NE2000 (legacy)
- VirtIO Network
Graphics Subsystem
The graphics architecture manages framebuffers and display hardware:See the Graphics Subsystem Documentation for comprehensive details.
- DisplayConnector: Abstraction for display outputs (
/dev/gpu/connectorX) - Framebuffer Management: Direct VRAM access via
mmap() - Virtual Memory Tricks: Seamless switching between console and graphics modes
- GPU Drivers: Support for multiple graphics adapters
System Call Interface
System calls inKernel/Syscalls/ provide the kernel API:
Security Features
pledge() System Call
Inspired by OpenBSD’s
pledge(), restricts what system calls a process can make. Once pledged, a process cannot regain privileges.unveil() System Call
Restricts filesystem access to specific paths. A process can only access unveiled paths after calling
unveil().W^X Memory
Enforces that memory pages are either writable or executable, never both. Prevents code injection attacks.
ASLR
Randomizes memory layout of kernel and userspace processes to prevent exploitation.
Architecture Abstraction
TheKernel/Arch/ directory isolates platform-specific code:
- x86-64
- aarch64
- riscv64
- Boot: Multiboot2, UEFI boot support
- MMU: 4-level page tables, PAE
- Interrupts: IDT, APIC, I/O APIC
- Context Switch: CPU state save/restore
- Syscalls: SYSCALL/SYSRET instructions
Locking and Synchronization
The kernel provides various synchronization primitives inKernel/Locking/:
See Kernel Locking Documentation for locking patterns and best practices.
Interrupt Handling
The interrupt subsystem inKernel/Interrupts/ handles:
- Hardware Interrupts: Timer, keyboard, disk, network
- Software Interrupts: System calls, exceptions
- IRQ Routing: APIC, I/O APIC configuration
- Deferred Processing: Bottom-half handlers for complex operations
Kernel Debugging
Debugging facilities:- Debug Macros: Per-subsystem debug output (
#define XYZ_DEBUG) - Kernel Symbols: Symbol resolution for stack traces
- Profiling: Built-in kernel profiler
- Assertions:
VERIFY()andMUST()for invariant checking - Sanitizers: UBSAN support for catching undefined behavior
Performance Considerations
Zero-Copy Operations
Zero-Copy Operations
- Direct memory mapping for framebuffers
- Scatter-gather I/O for network and disk
- Shared memory regions between processes
Caching
Caching
- Page cache for filesystem I/O
- Inode cache for directory lookups
- Slab allocator for kernel objects
Asynchronous I/O
Asynchronous I/O
- Non-blocking I/O operations
- select()/poll() for event multiplexing
- Efficient interrupt handling
Further Reading
Containers Documentation
Learn about intrusive lists and kernel data structures
Development Guidelines
Kernel coding standards and best practices
