kernel_overview

Posted by:

|

On:

|

TAIL OS Kernel Overview

Table of Contents

  1. Introduction
  2. Kernel Architecture
  3. Core Components
  4. Memory Management
  5. Process and Thread Management
  6. Scheduling
  7. Inter-Process Communication (IPC)
  8. Kernel Calls
  9. Exception Handling
  10. System Clock
  11. File System
  12. Initialization Sequence
  13. Design Principles

Introduction

TAIL OS is a user-friendly real-time operating system designed for safety and robotics applications. The kernel is implemented in Rust and follows a microkernel architecture with the capability to become monolithic or hybrid depending on build configuration.

Key Features

  • Hard Real-time: Satisfies hard real-time task requirements
  • Safety-oriented: Designed with ISO safety certification in mind
  • Dynamic Modular: Can be configured as micro, monolithic, or hybrid kernel
  • Multi-architecture: Supports AArch64 and x86_64 architectures
  • Memory-safe: Leverages Rust’s memory safety features

Kernel Architecture

The TAIL OS kernel follows a layered architecture with clear separation of concerns:

┌─────────────────────────────────────────────────────────────┐
│                    User Space                               │
├─────────────────────────────────────────────────────────────┤
│                    Kernel Calls Interface                   │
├─────────────────────────────────────────────────────────────┤
│  Process Manager │ Thread Manager │ Memory Manager │ Scheduler │
├─────────────────────────────────────────────────────────────┤
│              Exception Handler │ System Clock               │
├─────────────────────────────────────────────────────────────┤
│                    Hardware Abstraction Layer               │
└─────────────────────────────────────────────────────────────┘

Architecture Principles

  • Microkernel Design: Core functionality is minimal, with services provided through kernel calls
  • Modular Components: Each subsystem is independently manageable
  • Hardware Abstraction: Platform-specific code is isolated in HAL layers
  • Memory Safety: Rust’s ownership system prevents common memory errors

Core Components

1. Process Manager (process/)

  • Purpose: Manages process lifecycle and process table
  • Key Features:
    • Process creation and termination
    • Process ID (PID) management
    • Process state tracking
    • ELF file loading and execution
  • Implementation: ProcessManager maintains a process table with PID-based indexing

2. Thread Manager (thread/)

  • Purpose: Manages thread lifecycle and execution context
  • Key Features:
    • Thread creation and destruction
    • Thread context switching
    • Thread priority management
    • Idle thread management
  • Implementation: Each thread has its own execution context and priority level

3. Memory Manager (memory/)

  • Purpose: Manages physical and virtual memory allocation
  • Key Features:
    • Kernel memory allocator
    • Physical page management
    • Virtual memory page management
    • MMU abstraction (AArch64/x86_64)
  • Implementation:
    • KernelMemoryAllocator: Global allocator for kernel heap
    • Physical and virtual memory page managers
    • Architecture-specific MMU implementations

4. Scheduler (schedule/)

  • Purpose: Determines which thread executes next
  • Key Features:
    • Priority-based scheduling
    • Round-robin within same priority
    • Context switching
    • Thread state management
  • Implementation: Multi-level priority queues with round-robin scheduling

Memory Management

Memory Layout

Kernel Virtual Address Space (64MB total)
  VIRTUAL_ADDR_KERNEL_BASE ..= VIRTUAL_ADDR_KERNEL_END
  0xFFFFFF8000000000        .. 0xFFFFFF8003FFFFFF

  Low Guard (4KB):           [0xFFFFFF8000000000 .. 0xFFFFFF8000000FFF]
  Kernel Stack (1MB):        [0xFFFFFF8000001000 .. 0xFFFFFF8000100FFF]
  High Guard (4KB):          [0xFFFFFF8000101000 .. 0xFFFFFF8000101FFF]
  Kernel Text Base:          VIRTUAL_ADDR_KERNEL_TEXT_SEGMENT_BASE
                              0xFFFFFF8000102000

System Manager Region
  0xFFFFFF8002000000        .. 0xFFFFFF8003FFFFFF

Kernel MMIO Callouts (examples)
  puts callout:               0xFFFFFF8020000000
  UART base:                  0xFFFFFF8020001000
  Interrupt controller base:  0xFFFFFF8020004000

User Virtual Address Space (64MB total)
  VIRTUAL_ADDR_USER_BASE     .. VIRTUAL_ADDR_USER_END
  0x0000000080000000         .. 0x0000000083FFFFFF
  User Text Base:             0x0000000080100000  (after 1MB user stack)

Startup Identity Map
  0x0000000000000000         .. 0x000000003FFFFFFF

Other
  OS load physical address:   0x0000000000080000

Memory Management Features

  • Kernel Heap: Dynamic memory allocation for kernel components
  • Physical Memory: Page-based physical memory allocation
  • Virtual Memory: Virtual address space management
  • Shared Memory: IPC shared memory regions
  • Memory Protection: User/kernel space isolation

Page Table Management

  • Kernel Page Tables: Managed by TTBR1_EL1 (AArch64)
  • User Page Tables: Managed by TTBR0_EL1 (AArch64)
  • Recursive Page Tables: Efficient page table manipulation
  • Memory Mapping: Dynamic virtual-to-physical mapping

Process and Thread Management

Process Model

  • Process: Container for threads with shared memory space
  • Thread: Basic unit of execution with independent stack and registers
  • PID Management:
    • PID 0: Startup process (formal)
    • PID 1: Kernel core process
    • PID 2+: User processes

Thread States

  • Ready: Thread is ready to execute
  • Running: Thread is currently executing
  • Blocked: Thread is waiting for an event
  • Terminated: Thread has finished execution

Context Switching

  • Register Save/Restore: Complete CPU state preservation
  • Stack Switching: Independent stack per thread
  • Page Table Switching: Process memory space isolation

Scheduling

Scheduling Algorithm

  • Priority-based: Threads have priority levels (0-255)
  • Round-robin: Within same priority level
  • Preemptive: Higher priority threads can preempt lower priority
  • Real-time: Predictable scheduling for hard real-time tasks

Scheduler Components

  • Ready Queue: Priority-based thread queues
  • Blocked Queue: Threads waiting for events
  • Idle Thread: Runs when no other threads are ready
  • Timer Interrupt: Periodic scheduling decisions

Inter-Process Communication (IPC)

IPC Mechanisms

  1. Shared Memory: Primary IPC mechanism

    • Kernel-managed shared memory regions
    • Safe shared memory with access control
    • Topic-based communication
  2. Service/Server Model:

    • Service directory for service discovery
    • Request/reply communication pattern
    • Server kernel layout management

IPC Components

  • Topic Directory: Manages topic-based communication
  • Service Directory: Manages service discovery
  • Safe Shared Memory: Memory-safe IPC implementation

Kernel Calls

Kernel Call Interface

The kernel provides a comprehensive set of system calls for user processes:

Process Management

  • create_process(): Create new process
  • terminate_process(): Terminate process

Memory Management

  • mmap(): Map memory regions
  • munmap(): Unmap memory regions

IPC Operations

  • create_topic(): Create communication topic
  • subscribe_topic(): Subscribe to topic
  • publish_to_topic(): Publish to topic
  • create_server(): Create service server
  • request_service(): Request service
  • send_service_request_and_wait_for_reply(): Synchronous service call

Kernel Call Implementation

  • Table-driven: Centralized kernel call table
  • Common Handler: Shared kernel call processing
  • Architecture-specific: Platform-specific implementations

Exception Handling

Exception Vector Table

  • AArch64: 4 sets of 4 entries (Synchronous, IRQ, FIQ, SError)
  • Exception Levels: EL0 (User), EL1 (Kernel)
  • Interrupt Handling: Timer and external interrupts

Exception Manager

  • Interrupt Controller: Hardware interrupt management
  • Exception Routing: Proper exception handling routing
  • Context Preservation: Complete CPU state preservation

System Clock

Clock Management

  • System Timer: Hardware timer abstraction
  • Interrupt Period: Configurable timer interrupt period
  • Time Services: Time-based kernel services
  • Scheduling Timer: Periodic scheduling decisions

Clock Components

  • System Clock: Core timing functionality
  • Timer Interrupts: Periodic system events
  • Time Callouts: Hardware-specific timing functions

File System

File Management

  • Kernel File: Kernel-level file operations
  • File Manager: File system abstraction
  • RFS Integration: Root File System support

Initialization Sequence

The kernel follows a specific initialization sequence:

  1. System Data Setup: Copy system data from startup
  2. Kernel Print: Initialize debug output
  3. Memory Manager: Initialize kernel memory allocator
  4. Exception Manager: Setup exception handling
  5. System Clock: Initialize timing services
  6. Process Manager: Create initial processes
  7. RFS Loading: Load initial processes from RFS
  8. Scheduler: Start thread scheduling
  9. Interrupt Enable: Enable system interrupts

Boot Process

Startup → Kernel pre_main() → Kernel main() → Scheduler → User Processes

Design Principles

1. Safety First

  • Memory Safety: Rust’s ownership system prevents memory errors
  • Type Safety: Strong typing prevents runtime errors
  • Exception Safety: Proper exception handling and recovery

2. Real-time Guarantees

  • Predictable Scheduling: Priority-based scheduling with bounded latency
  • Interrupt Handling: Fast interrupt response times
  • Memory Allocation: Bounded allocation times

3. Modularity

  • Component Isolation: Clear separation between kernel components
  • Interface Abstraction: Well-defined interfaces between components
  • Dynamic Configuration: Build-time kernel configuration

4. Scalability

  • Multi-core Ready: Designed for multi-core systems
  • Architecture Agnostic: Support for multiple CPU architectures
  • Platform Abstraction: Hardware-specific code isolation

5. Performance

  • Efficient Data Structures: Optimized kernel data structures
  • Minimal Overhead: Low kernel overhead for system calls
  • Cache-friendly: Memory layout optimized for CPU caches

Future Considerations

Planned Enhancements

  • Multi-core Support: SMP (Symmetric Multi-Processing) support
  • Kernel Modules: Dynamic kernel module loading
  • Advanced Scheduling: More sophisticated scheduling algorithms
  • Enhanced IPC: Additional IPC mechanisms
  • Security Features: Enhanced security and isolation

Extensibility

  • Plugin Architecture: Support for kernel extensions
  • Service Framework: Enhanced service discovery and management
  • Device Drivers: Comprehensive device driver framework
  • Network Stack: Integrated networking capabilities

This document provides a comprehensive overview of the TAIL OS kernel architecture and design. For detailed implementation information, refer to the individual component documentation in the kernel/doc/ directory.

Posted by

in