# arianhaddadi/xv6-os-modification architecture

> Architecture diagram of the GitHub repository arianhaddadi/xv6-os-modification, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/arianhaddadi/xv6-os-modification
- Repository: https://github.com/arianhaddadi/xv6-os-modification

## Overview

##### Main Components
- Boot and Initialization:
- Files: bootasm.S, bootmain.c, entry.S, entryother.S, initcode.S, kernel.ld.
- Role: This component handles low-level boot operations, processor setup, initial memory configuration, and transitioning control into the kernel.
- Kernel Core:
- Files: main.c, defs.h, param.h, memlayout.h.
- Role: Central control logic, kernel centralized initialization, and global definitions.
- Process Management:
- Files: proc.c, proc.h, fork-related files (forktest.c), syscall.c, sysproc.c.
- Role: Creating, managing, and scheduling processes. The modifications might also incorporate new CPU scheduling algorithms.
- System Calls and User-space Interface:
- Files: syscall.c, syscall.h, usys.S.
- Role: Handling system call dispatch, which is also the entry point for extending the kernel’s functionality with new system calls.
- Synchronization and Scheduling Enhancements:
- Files: spinlock.c/h, sleeplock.c/h, barrier.c, chpri.c, setticket.c.
- Role: Concurrency control, thread synchronization improvements with barriers and potentially new scheduling/tracking mechanisms.
- Memory Management:
- Files: kalloc.c, vm.c, mmu.h.
- Role: Allocating and managing kernel memory, virtual memory management.
- File System and I/O:
- Files: fs.c, fs.h, file.c/file.h, mkfs.c, cat.c, ls.c, echo.c, grep.c, and related utilities.
- Role: Implementation of a simplified file system, command-line utilities to interact with files.
- Device Drivers and Hardware Handling:
- Files: kbd.c/kbd.h, uart.c, ioapic.c, lapic.c, picirq.c, ide.c, memide.c.
- Role: Handling I/O devices, keyboard inputs, serial communication, and interrupt controllers.
- IPC Mechanism Enhancements:
- Files: Possibly new or modified source files dedicated to the new IPC mechanism.
- Role: Facilitating inter-process communication similar to the Spring OS “doors” approach.
- Additional Utilities and Tools:
- Files: Various user-level programs (e.g., sh.c, grep.c, cat.c) and debugging utilities (e.g., printpcs, gdbutil).

##### Relationships and Interactions
- Boot Code → Initializes the hardware and then transfers control to the Kernel Core.
- Kernel Core → Initializes subsystems (Process Management, Memory Management, File System, Device Drivers).
- Process Management ↔ CPU Scheduling: Processes are scheduled based on the scheduler’s algorithm, which might now include custom elements from setticket.c and chpri.c.
- System Calls Interface: Connects user-space utilities and applications to kernel services.
- Device Drivers → Provide low-level I/O services to higher-level processes such as the console, file operations, etc.
- IPC Mechanism: Serves as a communication pathway between processes, integrated with the system call interface.

##### Important Architectural Patterns and Design Principles
- Monolithic Kernel: xv6 is designed as a monolithic kernel where most system components reside in a single address space.
- Modularity through Subsystems: Even though the kernel is monolithic, the project is organized into clear subsystems (boot, process management, memory, file system, device drivers, etc.).
- Layered Abstraction: Lower-level hardware interactions via assembly and driver code feed into higher-level system calls and process management.
- Extensibility: Enhancements such as additional system calls and new IPC mechanisms indicate a modular approach to extending kernel functionality.

- How the boot sequence leads to kernel initialization.
- Interactions between the system call interface, process management, and file operations.
- Being detailed – include every critical subsystem and highlight enhancements (like new IPC and scheduling improvements) to accurately mirror the modifications over the original xv6.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Boot and Initialization"
        BA1["bootasm.S"]:::boot
        BA2["bootmain.c"]:::boot
        BA3["entry.S"]:::boot
        BA4["entryother.S"]:::boot
        BA5["initcode.S"]:::boot
        BA6["kernel.ld"]:::boot
        BA1 --> BA2 --> BA3 --> BA4 --> BA5 --> BA6
    end

    subgraph "Kernel Core"
        KC1["main.c"]:::core
        KC2["defs.h"]:::core
        KC3["param.h"]:::core
        KC4["memlayout.h"]:::core
        KC1 --> KC2 --> KC3 --> KC4
    end

    subgraph "Process Management"
        PM1["proc.c"]:::process
        PM2["proc.h"]:::process
        PM3["forktest.c"]:::process
        PM4["syscall.c"]:::process
        PM5["sysproc.c"]:::process
        PM1 --> PM2 --> PM3 --> PM4 --> PM5
    end

    subgraph "System Calls and User-space Interface"
        SC1["syscall.c"]:::syscall
        SC2["syscall.h"]:::syscall
        SC3["usys.S"]:::syscall
        SC1 --> SC2 --> SC3
    end

    subgraph "Synchronization and Scheduling Enhancements"
        SS1["spinlock.c"]:::sync
        SS2["spinlock.h"]:::sync
        SS3["sleeplock.c"]:::sync
        SS4["sleeplock.h"]:::sync
        SS5["barrier.c"]:::sync
        SS6["chpri.c"]:::sync
        SS7["setticket.c"]:::sync
        SS1 --> SS2 --> SS3 --> SS4 --> SS5 --> SS6 --> SS7
    end

    subgraph "Memory Management"
        MM1["kalloc.c"]:::memory
        MM2["vm.c"]:::memory
        MM3["mmu.h"]:::memory
        MM1 --> MM2 --> MM3
    end

    subgraph "File System and I/O"
        FS1["fs.c"]:::filesystem
        FS2["fs.h"]:::filesystem
        FS3["file.c"]:::filesystem
        FS4["file.h"]:::filesystem
        FS5["mkfs.c"]:::filesystem
        FS6["cat.c"]:::filesystem
        FS7["ls.c"]:::filesystem
        FS8["echo.c"]:::filesystem
        FS9["grep.c"]:::filesystem
        FS1 --> FS2 --> FS3 --> FS4 --> FS5 --> FS6 --> FS7 --> FS8 --> FS9
    end

    subgraph "Device Drivers and Hardware Handling"
        DD1["kbd.c"]:::drivers
        DD2["kbd.h"]:::drivers
        DD3["uart.c"]:::drivers
        DD4["ide.c"]:::drivers
        DD5["memide.c"]:::drivers
        DD6["ioapic.c"]:::drivers
        DD7["lapic.c"]:::drivers
        DD8["picirq.c"]:::drivers
        DD1 --> DD2 --> DD3 --> DD4 --> DD5 --> DD6 --> DD7 --> DD8
    end

    subgraph "IPC Mechanism Enhancements"
        IPC1["IPC Module"]:::ipc
    end

    subgraph "Additional Utilities and Tools"
        AU1["sh.c"]:::user
        AU2["grep.c"]:::user
        AU3["cat.c"]:::user
        AU1 --> AU2 --> AU3
    end

    %% Main Connections between modules
    BA6 --> KC1
    KC4 --> PM1
    KC4 --> SC1
    KC4 --> MM1
    KC4 --> FS1
    KC4 --> DD1
    KC4 --> SS1
    KC4 --> IPC1
    SC3 --> AU1

    %% Styles
    classDef boot fill:#f9c,stroke:#333,stroke-width:2px;
    classDef core fill:#ccf,stroke:#333,stroke-width:2px;
    classDef process fill:#cff,stroke:#333,stroke-width:2px;
    classDef syscall fill:#cfc,stroke:#333,stroke-width:2px;
    classDef sync fill:#fcf,stroke:#333,stroke-width:2px;
    classDef memory fill:#ffc,stroke:#333,stroke-width:2px;
    classDef filesystem fill:#fcc,stroke:#333,stroke-width:2px;
    classDef drivers fill:#cc9,stroke:#333,stroke-width:2px;
    classDef ipc fill:#9cf,stroke:#333,stroke-width:2px;
    classDef user fill:#9f9,stroke:#333,stroke-width:2px;

    %% Click Events for Boot and Initialization
    click BA1 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/bootasm.S"
    click BA2 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/bootmain.c"
    click BA3 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/entry.S"
    click BA4 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/entryother.S"
    click BA5 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/initcode.S"
    click BA6 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/kernel.ld"

    %% Click Events for Kernel Core
    click KC1 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/main.c"
    click KC2 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/defs.h"
    click KC3 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/param.h"
    click KC4 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/memlayout.h"

    %% Click Events for Process Management
    click PM1 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/proc.c"
    click PM2 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/proc.h"
    click PM3 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/forktest.c"
    click PM4 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/syscall.c"
    click PM5 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/sysproc.c"

    %% Click Events for System Calls and User-space Interface
    click SC1 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/syscall.c"
    click SC2 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/syscall.h"
    click SC3 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/usys.S"

    %% Click Events for Synchronization and Scheduling Enhancements
    click SS1 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/spinlock.c"
    click SS2 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/spinlock.h"
    click SS3 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/sleeplock.c"
    click SS4 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/sleeplock.h"
    click SS5 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/barrier.c"
    click SS6 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/chpri.c"
    click SS7 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/setticket.c"

    %% Click Events for Memory Management
    click MM1 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/kalloc.c"
    click MM2 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/vm.c"
    click MM3 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/mmu.h"

    %% Click Events for File System and I/O
    click FS1 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/fs.c"
    click FS2 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/fs.h"
    click FS3 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/file.c"
    click FS4 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/file.h"
    click FS5 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/mkfs.c"
    click FS6 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/cat.c"
    click FS7 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/ls.c"
    click FS8 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/echo.c"
    click FS9 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/grep.c"

    %% Click Events for Device Drivers and Hardware Handling
    click DD1 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/kbd.c"
    click DD2 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/kbd.h"
    click DD3 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/uart.c"
    click DD4 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/ide.c"
    click DD5 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/memide.c"
    click DD6 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/ioapic.c"
    click DD7 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/lapic.c"
    click DD8 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/picirq.c"

    %% Click Events for Additional Utilities and Tools
    click AU1 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/sh.c"
    click AU2 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/grep.c"
    click AU3 "https://github.com/arianhaddadi/xv6-os-modification/blob/master/cat.c"
```
