Overview

The FreeWorld OS Loadable Kernel Modules (LKM) system provides dynamic loading and unloading of kernel code at runtime, enabling drivers and kernel extensions to be loaded without rebooting.

Components

Module Loader

Complete module loading system:

  • ELF Module Parsing: Supports ELF64 relocatable and shared objects
  • Module Loading: Loads modules into kernel space
  • Module Initialization: Calls module init function
  • Module Cleanup: Calls module exit function
  • State Management: UNLOADED, LOADED, RUNNING, UNLOADING states
  • Reference Counting: Prevents premature unloading
  • Module List: Linked list of all loaded modules
  • Metadata Support: Name, version, license, description

Symbol System

Symbol export/import system:

  • Hash Table: Fast symbol lookup (256 buckets)
  • Symbol Export: Kernel and module symbol export
  • Symbol Import: Module symbol resolution
  • Symbol Types: FUNCTION, DATA, OBJECT
  • Kernel Symbols: Automatic export of kernel symbols (kmalloc, kfree, etc.)
  • Thread-Safe: Spinlock-protected operations

Dependency System

Module dependency management:

  • Dependency Tracking: Tracks module dependencies
  • Dependency Resolution: Automatic dependency loading
  • Circular Detection: Detects circular dependencies
  • Load Order: Calculates correct load order (topological sort)

Module API

Function Description
module_load() Load module from memory
module_unload() Unload module
module_find() Find module by name
module_init_func() Initialize module (call init function)
module_get() Increment reference count
module_put() Decrement reference count
module_list_all() List all loaded modules
symbol_export() Export symbol
symbol_resolve() Resolve symbol (find address)
symbol_unexport() Unexport symbol
module_resolve_dependencies() Resolve module dependencies
module_check_circular() Check for circular dependencies

Features

  • ELF module loading (ELF64 relocatable/shared objects)
  • Module metadata (name, version, license, description)
  • Symbol export/import (kernel and module symbols)
  • Dependency resolution (automatic dependency loading)
  • Reference counting (safe module unloading)
  • Module locking (thread-safe operations)
  • Module list management (track all loaded modules)
  • Module states (UNLOADED, LOADED, RUNNING, UNLOADING)
  • Module initialization/cleanup (init/exit functions)
  • Up to 256 modules supported

Implementation

Total Lines of Code: ~1,395 lines

  • kernel/modules/module.asm (~850 lines) - Module loader
  • kernel/modules/symbols.asm (~400 lines) - Symbol system
  • kernel/modules/dependencies.asm (~150 lines) - Dependency system

Integration

The module system is integrated with:

  • Kernel initialization sequence
  • Kernel symbol export during initialization
  • Memory management (kmalloc/kfree)
  • All kernel components (for module loading)