# abhinavlg/xv6-riscv-compressfs architecture

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

- Interactive diagram: https://gitdiagram.com/abhinavlg/xv6-riscv-compressfs
- Repository: https://github.com/abhinavlg/xv6-riscv-compressfs

## Overview

##### Project Type and Purpose
- This is an educational monolithic operating system (xv6) ported to RISC-V, enhanced with:
- A Huffman-based compression file system (transparent compression/decompression)
- A user‐land text editor (“edit”)
- Supporting test utilities (comprtest, mkfile, testcomp)
- It consists of three primary parts: a bootloader + kernel (kernel/), user-space programs (user/), and a host tool (mkfs/).

##### File Structure
- Top-level: Makefile, README.md, LICENSE, mkfs directory (mkfs.c), kernel directory, user directory.
- kernel/: Boot/trampoline (entry.S, trampoline.S, kernelvec.S), C files for core OS subsystems—processes, memory, traps, VFS, syscalls—and device drivers (virtio_disk, UART).
- user/: Small C programs linked into the disk image, including shell (sh), edit, cat, comprtest, etc., plus support libraries (ulib.c, umalloc.c).
- mkfs/: Host‐side utility that formats a disk image, installs compressed files, builds the initial file system.

##### Key README Insights
- Architecture: standard xv6 monolithic kernel, modified VFS layer to call Huffman compression routines on file write/read.
- Toolchain: built with a RISC-V cross-compiler, runs under QEMU RISC-V.
- Workflow: mkfs → build kernel+user binaries → QEMU boots disk image → user interacts via shell, edit, compression tests.

##### Main Components & Their Interactions
a. Components
- RISC-V Virtual Hardware (CPU, RAM, VirtIO Disk, UART, PLIC)
- QEMU Emulator (runs RISC-V guest)
- Bootloader/Trampoline Code
- Monolithic Kernel
– CPU/Trap Handler
– Process Scheduler
– Virtual Memory Manager
– File System Layer (VFS + Huffman compression module)
– Device Drivers (virtio_disk, UART)
– System Call Interface
- User-Space Processes
– Shell (sh), Text Editor (edit), cat, grep, comprtest, testcomp, etc.
- Host Tool: mkfs (formats disk image, installs user binaries & initial files)
- Build System (Makefile, compiler, linker)

b. Data Flows & Control Flows
1. Developer invokes Makefile:
- Cross-compile kernel and user binaries → ELF images
- mkfs runs on host: produces a disk.img containing compressed/uncompressed files
2. QEMU boots image:
- BIOS loads bootloader/trampoline → jumps into kernel main
3. Kernel Initialization:
- Initialize memory, traps, scheduler, device drivers
- Mount root file system via virtio_disk driver → VFS → compression layer
4. User Interaction:
- Shell issues system calls (read, write, open, exec)
- File writes pass through compression module (decide whether to compress, store Huffman trees, write blocks)
- File reads transparently decompress as needed
- edit runs as a user process, interacting with console driver (UART)
5. Testing Utilities: comprtest/testcomp spawn processes, exercise FS compression paths

c. Architectural Patterns & Design Principles
- Monolithic kernel: all core services (FS, VM, scheduler) in one address space
- Layered design: hardware → traps/interrupts → core kernel → VFS/compression → system calls → user
- Modular file system: separates block I/O (virtio_disk) from VFS and compression logic
- Privilege separation: kernel runs in supervisor mode; user apps in user mode

d. Key Technologies & Frameworks
- Language: C for most code, RISC-V assembly for low-level parts
- Emulator: QEMU RISC-V
- Toolchain: gcc-riscv64-linux-gnu, binutils-riscv64
- Build: Makefile
- Huffman compression: custom C functions in kernel/compress.c

- Labels: clear, per component (e.g., “VirtIO Disk Driver”, “Huffman Compression Layer”).
- Hardware: green rectangles
- Emulator: gray rounded rectangle

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Build System
    Makefile["Makefile"]:::build
    Makefile -->|build binaries| KernelBin["Kernel Binary"]:::build
    Makefile -->|build binaries| UserBins["User Binaries"]:::build
    Makefile -->|build binary| MkfsBin["mkfs (Disk Image Builder)"]:::host

    subgraph Host Tool Layer
        MkfsBin -->|formats disk.img| DiskImg["Disk Image"]:::artifact
    end

    %% Emulator Layer
    QEMU["QEMU Emulator"]:::emulator
    DiskImg -->|mounts| QEMU

    %% Bootloader & Trampoline Layer
    subgraph "Bootloader & Trampoline Code"
        Entry["entry.S"]:::kernel
        Tramp["trampoline.S"]:::kernel
        Kvec["kernelvec.S"]:::kernel
        Start["start.c"]:::kernel
    end
    QEMU -->|BIOS loads| Entry
    Entry --> Tramp
    Tramp --> Kvec
    Kvec --> Start

    %% Kernel Layer
    subgraph "Monolithic Kernel" 
        direction TB
        subgraph "Trap & Interrupt Handler"
            Trap["trap.c"]:::kernel
            Plic["plic.c"]:::kernel
        end
        subgraph "Process Scheduler & Context Switch"
            Proc["proc.c"]:::kernel
            Swtch["swtch.S"]:::kernel
        end
        subgraph "Virtual Memory Manager"
            VM["vm.c"]:::kernel
            Kalloc["kalloc.c"]:::kernel
            ML["memlayout.h"]:::kernel
        end
        subgraph "File System & Compression"
            FsC["fs.c"]:::kernel
            FsH["fs.h"]:::kernel
            FileC["file.c"]:::kernel
            FileH["file.h"]:::kernel
            Bio["bio.c"]:::kernel
            Compress["compress.c"]:::kernel
        end
        subgraph "Device Drivers"
            VirtioDisk["virtio_disk.c"]:::kernel
            Uart["uart.c"]:::kernel
            VirtioH["virtio.h"]:::kernel
        end
        subgraph "System Call Interface"
            SysC["syscall.c"]:::kernel
            SysH["syscall.h"]:::kernel
            SysProc["sysproc.c"]:::kernel
            SysFile["sysfile.c"]:::kernel
            Usys["usys.pl"]:::kernel
        end
    end
    Start --> Trap
    Trap --> Proc
    Proc --> VM
    VM --> FsC
    FsC --> VirtioDisk
    SysC --> Trap

    %% Disk I/O
    VirtioDisk -->|blk read/write| HardwareDisk["Disk"]:::hardware
    Uart -->|serial I/O| HardwareUART["UART"]:::hardware

    %% User Space Layer
    subgraph "User Space"
        direction TB
        Shell["sh (shell)"]:::user
        Edit["edit (text editor)"]:::user
        Cat["cat"]:::user
        ComprTest["comprtest"]:::user
        TestComp["testcomp"]:::user
        Mkfile["mkfile"]:::user
    end
    UserBins --> Shell
    UserBins --> Edit
    UserBins --> Cat
    UserBins --> ComprTest
    UserBins --> TestComp
    UserBins --> Mkfile

    Shell -->|syscall| SysC
    Edit -->|syscall| SysC
    ComprTest -->|syscall| SysC
    TestComp -->|syscall| SysC

    %% Privilege boundary
    class HardwareDisk,HardwareUART hardware
    class QEMU emulator
    class Makefile,KernelBin,UserBins build
    class MkfsBin host
    class KernelBin,Entry,Tramp,Kvec,Start,Trap,Plic,Proc,Swtch,VM,Kalloc,ML,FsC,FsH,FileC,FileH,Bio,Compress,VirtioDisk,Uart,VirtioH,SysC,SysH,SysProc,SysFile,Usys kernel
    class Shell,Edit,Cat,ComprTest,TestComp,Mkfile user
    class DiskImg artifact

    %% Click Events
    click Entry "https://github.com/abhinavlg/xv6-riscv-compressfs/blob/main/kernel/entry.S"
    click Tramp "https://github.com/abhinavlg/xv6-riscv-compressfs/blob/main/kernel/trampoline.S"
    click Kvec "https://github.com/abhinavlg/xv6-riscv-compressfs/blob/main/kernel/kernelvec.S"
    click Start "https://github.com/abhinavlg/xv6-riscv-compressfs/blob/main/kernel/start.c"
    click Trap "https://github.com/abhinavlg/xv6-riscv-compressfs/blob/main/kernel/trap.c"
    click Plic "https://github.com/abhinavlg/xv6-riscv-compressfs/blob/main/kernel/plic.c"
    click Proc "https://github.com/abhinavlg/xv6-riscv-compressfs/blob/main/kernel/proc.c"
    click Swtch "https://github.com/abhinavlg/xv6-riscv-compressfs/blob/main/kernel/swtch.S"
    click VM "https://github.com/abhinavlg/xv6-riscv-compressfs/blob/main/kernel/vm.c"
    click Kalloc "https://github.com/abhinavlg/xv6-riscv-compressfs/blob/main/kernel/kalloc.c"
    click ML "https://github.com/abhinavlg/xv6-riscv-compressfs/blob/main/kernel/memlayout.h"
    click FsC "https://github.com/abhinavlg/xv6-riscv-compressfs/blob/main/kernel/fs.c"
    click FsH "https://github.com/abhinavlg/xv6-riscv-compressfs/blob/main/kernel/fs.h"
    click FileC "https://github.com/abhinavlg/xv6-riscv-compressfs/blob/main/kernel/file.c"
    click FileH "https://github.com/abhinavlg/xv6-riscv-compressfs/blob/main/kernel/file.h"
    click Bio "https://github.com/abhinavlg/xv6-riscv-compressfs/blob/main/kernel/bio.c"
    click Compress "https://github.com/abhinavlg/xv6-riscv-compressfs/blob/main/kernel/compress.c"
    click VirtioDisk "https://github.com/abhinavlg/xv6-riscv-compressfs/blob/main/kernel/virtio_disk.c"
    click Uart "https://github.com/abhinavlg/xv6-riscv-compressfs/blob/main/kernel/uart.c"
    click VirtioH "https://github.com/abhinavlg/xv6-riscv-compressfs/blob/main/kernel/virtio.h"
    click SysC "https://github.com/abhinavlg/xv6-riscv-compressfs/blob/main/kernel/syscall.c"
    click SysH "https://github.com/abhinavlg/xv6-riscv-compressfs/blob/main/kernel/syscall.h"
    click SysProc "https://github.com/abhinavlg/xv6-riscv-compressfs/blob/main/kernel/sysproc.c"
    click SysFile "https://github.com/abhinavlg/xv6-riscv-compressfs/blob/main/kernel/sysfile.c"
    click Usys "https://github.com/abhinavlg/xv6-riscv-compressfs/blob/main/kernel/usys.pl"
    click Shell "https://github.com/abhinavlg/xv6-riscv-compressfs/blob/main/user/sh.c"
    click Edit "https://github.com/abhinavlg/xv6-riscv-compressfs/blob/main/user/edit.c"
    click Cat "https://github.com/abhinavlg/xv6-riscv-compressfs/blob/main/user/cat.c"
    click ComprTest "https://github.com/abhinavlg/xv6-riscv-compressfs/blob/main/user/comprtest.c"
    click TestComp "https://github.com/abhinavlg/xv6-riscv-compressfs/blob/main/user/testcomp.c"
    click Mkfile "https://github.com/abhinavlg/xv6-riscv-compressfs/blob/main/user/mkfile.c"
    click MkfsBin "https://github.com/abhinavlg/xv6-riscv-compressfs/blob/main/mkfs/mkfs.c"

    %% Styles
    classDef hardware fill:#ccffcc,stroke:#2d702d
    classDef emulator fill:#eeeeee,stroke:#666666
    classDef kernel fill:#ffcccc,stroke:#992d2d
    classDef user fill:#cce5ff,stroke:#2d4a99
    classDef build fill:#ffdd99,stroke:#aa6600
    classDef host fill:#ffcc99,stroke:#994d00
    classDef artifact fill:#ffffff,stroke:#000000,stroke-dasharray: 5 5
```
