How abhinavlg/xv6-riscv-compressfs works
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