diff options
| author | Felix Morgner <felix.morgner@ost.ch> | 2026-07-04 13:10:51 +0200 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@ost.ch> | 2026-07-04 13:10:51 +0200 |
| commit | ff1f66e836824da4ad3d755159a367dd73282092 (patch) | |
| tree | ddeeee2a0f43631972b97bf1e7cc1f40a3e3b509 /README.rst | |
| parent | 708f6068cd52c3a2170c184bfecfbe96afe44d49 (diff) | |
| download | kernel-ff1f66e836824da4ad3d755159a367dd73282092.tar.xz kernel-ff1f66e836824da4ad3d755159a367dd73282092.zip | |
doc: add architecture overview and project ideas
Diffstat (limited to 'README.rst')
| -rw-r--r-- | README.rst | 165 |
1 files changed, 165 insertions, 0 deletions
@@ -58,3 +58,168 @@ To reduce these issues, you can either set up a Linux VM for development, or, if | ``code .`` If you use tools such as Git Extensions or GitHub Desktop, access the repository via the WSL network path, for example ``\\wsl.localhost\<distro>\<path-to-repo>``. + +Kernel Architecture +------------------- + +The kernel is structured into four primary directories, ensuring a clear division of concerns: + +- `arch/ <arch>`_: Contains all platform-dependent implementations (currently targeting `x86_64`). +- `kapi/ <kapi>`_: The Kernel API, which defines the boundaries between platform-dependent and platform-independent components. +- `kernel/ <kernel>`_: Core platform-independent kernel logic. +- `libs/ <libs>`_: Standalone support libraries (e.g., custom standard library `kstd`, ACPI, and Multiboot2 parsers). + +Architecture specific logic (`arch`) and platform-independent (`kernel`) logic must not depend directly on each other. +Instead, all interaction must be facilitated via interfaces declared in `kapi/ <kapi>`_. + +.. code-block:: mermaid + + graph TD + kernel[kernel: Platform-Independent Core] --> kapi[kapi: Kernel API] + arch[arch: Platform-Dependent Code] --> kapi + kernel --> libs[libs: Standalone Libraries] + arch --> libs + kapi --> libs + +Kernel API (KAPI) Decoupling Design +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The decoupling of interfaces and implementations is structured via two categories of declarations within `kapi/ <kapi>`_: + +1. **Kernel-defined**: Platform-independent implementations located in `kernel/kapi/ <kernel/kapi>`_. For example: + - `memory.cpp <kernel/kapi/memory.cpp>`_: Wraps physical frame allocation and page mapping. + - `filesystem.cpp <kernel/kapi/filesystem.cpp>`_: Wraps Virtual File System (VFS) operations. + - `interrupts.cpp <kernel/kapi/interrupts.cpp>`_: Registers and routes interrupts to handlers. + +2. **Platform-defined**: Low-level hooks declared in `kapi/ <kapi>`_ and implemented by the target platform under `arch/x86_64/kapi/ <arch/x86_64/kapi>`_. For example: + - `cpu.cpp <arch/x86_64/kapi/cpu.cpp>`_: Performs early CPU setup via `init()`. + - `memory.cpp <arch/x86_64/kapi/memory.cpp>`_: Initializes page tables and platform allocator. + - `interrupts.cpp <arch/x86_64/kapi/interrupts.cpp>`_: Exposes methods to toggle external interrupts. + +This decoupling is intended to ease the porting of TeachOS to new architectures (e.g. RISC-V or ARM64) by implementing a new subdirectory under `arch` that implements the platform-defined KAPI interfaces. + +Subsystems +~~~~~~~~~~ + +Bootstrapping & CPU Initialization +.................................. + +Execution begins in platform-dependent code. +Once a basic execution environment has been established, the C++ runtime is initialized via before calling the core `main() <kernel/kernel/main.cpp>`_ function. + +Memory Management +................. + +TeachOS separates memory allocation into three specialized systems: + +* **Physical Memory Manager**: The current implementation manages physical memory using a bitmap. +* **Virtual Memory Page Mapper**: The architecture-specific page table management implementation. +* **Kernel Heap Allocator**: Memory allocations via `new`/`delete` are currently routed through a `block_list_allocator <kernel/kernel/memory/block_list_allocator.hpp>`_ using a linked list first-fit allocator. +* **Memory-Mapped I/O (MMIO)**: The `mmio_allocator <kernel/kernel/memory/mmio_allocator.hpp>`_ reserves and maps memory regions for MMIO. + +Virtual File System (VFS) +......................... + +A POSIX-like directory and file abstraction layer is located in `kernel/filesystem/ <kernel/kernel/filesystem>`_: + +* **Abstractions**: Managed via directory entries (`dentry.hpp <kernel/kernel/filesystem/dentry.hpp>`_), filesystem nodes (`inode.hpp <kernel/kernel/filesystem/inode.hpp>`_), mount hierarchies (`mount.hpp <kernel/kernel/filesystem/mount.hpp>`_), and open file tables (`open_file_table.hpp <kernel/kernel/filesystem/open_file_table.hpp>`_). +* **Concrete Filesystems**: + - `rootfs <kernel/kernel/filesystem/rootfs/filesystem.hpp>`_: An in-memory filesystem mounted at `/`. + - `devfs <kernel/kernel/filesystem/devfs/filesystem.hpp>`_: Exposes devices as files under `/dev`. + - `ext2 <kernel/kernel/filesystem/ext2/filesystem.hpp>`_: An Ext2 read/write driver operating on RAM disks loaded as multiboot modules. + +Standalone Support Libraries +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Standalone support libraries under `libs/ <libs>`_ compile independently of the kernel logic: + +* `kstd <libs/kstd/kstd>`_: A custom standard library containing containers, support infrastructure, and printing/formatting implementations in lieu of a hosted standard library. +* `multiboot2 <libs/multiboot2/multiboot2/information.hpp>`_: A Multiboot2 structure parser. +* `acpi <libs/acpi/acpi/acpi.hpp>`_: An ACPI parsing utility. +* `elf <libs/elf/elf/section_header.hpp>`_: An ELF file format parser. + +Semester & Bachelor's Thesis Opportunities +------------------------------------------ + +Due to its simple and educational design, TeachOS lacks several advanced OS features. +This makes the kernel an excellent playground for semester projects and Bachelor's theses. +The following list outlines potential areas of development, highlighting the architectural dependencies between key systems: + +Multitasking, User Space, and System Calls +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +There is a direct dependency between process/thread abstractions and the implementation of user mode execution. +To run separate user applications, the kernel must manage task states, perform context switches, and handle system calls. +Conversely, user space isolation requires page table management and hardware privilege levels. +These projects should be approached sequentially or as a joint effort: + +1. **Kernel Multitasking (Cooperative and Preemptive)**: + - *Scope*: Design a Thread Control Block (TCB) and Process Control Block (PCB) structure. Implement low-level stack setup and context switching in assembly for the active CPU. + - *Extension*: Develop a task scheduler (e.g., Round-Robin, Priority-based, or Multi-Level Feedback Queue) utilizing the local APIC timer for preemptive multitasking. + +2. **User Mode Isolation and System Call Interface**: + - *Prerequisite*: A basic multitasking or thread control subsystem. + - *Scope*: Implement user-kernel privilege transitions (Ring 3 to Ring 0) utilizing platform-specific instructions. Configure page tables to separate user space virtual memory from the higher-half kernel mapping. + - *Extension*: Establish a system call dispatcher routing file and memory operations from user space to the corresponding `kapi/ <kapi>`_ implementations. + +Platform Ports (64-bit Architectures) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +TeachOS currently targets only 64-bit x86 (`x86_64`). +To validate the platform-independence of the `kapi/ <kapi>`_ interface, ports to other 64-bit targets are highly encouraged: + +3. **ARM64 (AArch64) Port**: + - *Scope*: Implement the platform-defined KAPI interfaces for a 64-bit ARM target (e.g., QEMU `virt` board or Raspberry Pi 4). This includes writing the boot startup assembly, configuring the translation table (MMU paging), handling the Generic Interrupt Controller (GIC), and implementing timer ticks. + +4. **RISC-V 64-bit (RV64G) Port**: + - *Scope*: Port TeachOS to the RISC-V 64-bit architecture. This involves implementing boot assembly, configuring page table mappings (Sv39/Sv48), setting up the Core Local Interruptor (CLINT) and Platform-Level Interrupt Controller (PLIC), and managing supervisor/user mode transitions. + +Memory Management Subsystem Extensions +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The current memory management uses a basic bitmap page frame allocator and a first-fit heap allocator. +There are significant opportunities to implement standard production-grade memory management schemes: + +5. **Buddy Page Allocator**: + - *Scope*: Replace the current `bitmap_frame_allocator <kernel/kernel/memory/bitmap_allocator.hpp>`_ with a Buddy Allocator system. This manages memory allocations in power-of-two page sizes, significantly reducing external fragmentation and improving allocation speed. + +6. **Slab/Slub/Slob Object Allocator**: + - *Scope*: Implement a slab allocator on top of the physical page allocator. This caches kernel objects of identical size (such as inodes, file descriptors, and thread control blocks) to avoid constant heap fragmentation and overhead from the general-purpose `block_list_allocator <kernel/kernel/memory/block_list_allocator.hpp>`_. + +7. **Advanced Virtual Memory (Copy-on-Write, Demand Paging)**: + - *Prerequisite*: A basic thread multitasking subsystem. + - *Scope*: Implement a page fault handler that dynamically loads executable segments only when touched (demand paging), or implement copy-on-write page table sharing (crucial for implementing Unix-like `fork` semantics). + +Filesystem Support and VFS Extensions +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The Virtual File System (VFS) is designed to host multiple concurrent filesystem types, but support is currently limited. +Project opportunities here include implementing new drivers or core VFS caching features: + +8. **Ext2 Write Support and Dynamic Allocation** (already available in experimental stage): + - *Scope*: Extend the read-oriented `ext2 <kernel/kernel/filesystem/ext2/filesystem.hpp>`_ driver to support full write operations. This requires managing free inode and block bitmaps, updating directories, allocating data blocks dynamically, and maintaining superblock consistency. + +9. **New Filesystem Drivers (e.g., FAT32, ISO 9660)**: + - *Scope*: Implement new filesystem drivers from scratch (such as FAT32 or ISO 9660 for CD-ROMs), allowing TeachOS to interoperate with standard virtual media and flash drives. + +10. **Unified Page Cache and Directory Entry Cache**: + - *Scope*: Develop a page caching subsystem that intercepts read/write VFS calls, caching recently accessed filesystem blocks in physical memory frames, and optimize pathname lookup times using a dynamic directory entry (dentry) cache. + +11. **Virtual Filesystems (e.g., procfs, sysfs)**: + - *Scope*: Create virtual filesystems that dynamically generate contents from current kernel data structures, providing userspace with debugging and configuration access interfaces. + +Hardware Buses and Device Drivers +.................................. + +Currently, block storage is simulated via RAM disks loaded as boot modules. +Real hardware interaction requires expanding driver support: + +12. **PCI/PCIe Bus Discovery**: + - *Scope*: Develop a PCI/PCIe bus driver that scans configuration spaces, detects connected devices, and registers them to the virtual root bus using the `kapi::devices <kapi/kapi/devices.hpp>`_ interface. + +13. **Storage Controller Drivers (AHCI/SATA or NVMe)**: + - *Prerequisite*: PCI bus discovery. + - *Scope*: Write a driver for SATA controllers (AHCI) or modern NVMe drives, routing block read/write operations from the VFS to actual physical disks. + +14. **Network Stack and Driver Integration**: + - *Scope*: Interface with a network adapter (e.g., Intel e1000 or VirtIO-net), and develop a lightweight network stack (ARP, IPv4, UDP) to allow TeachOS to send and receive raw network frames. |
