# tingwei628/barukanos architecture

> Architecture diagram of the GitHub repository tingwei628/barukanos, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tingwei628/barukanos
- Repository: https://github.com/tingwei628/barukanos

## Overview

##### Project Type & Purpose
- This is an educational, full-fledged toy operating system (bare-metal software)
- Key purposes: demonstrate x86_64 boot sequence (real → protected → long mode), paging, interrupts, system calls, process scheduling, FAT16 FS, and a CLI

##### File Structure
- **boot/**: multi-stage bootloader (real, protected, long mode), assembler and C code for loader, GDT/IDT setup
- **kernel/**: core OS code—startup (`main.c`, `kernel.asm`), memory manager (`mem.c`, `vm.c`), interrupt/trap handlers (`idt.c`, `trap.asm`), process manager, syscall interface, printing, linked via `linker.ld`
- **drivers/**: PS/2 keyboard driver, I/O port abstraction
- **fs/fat16/**: FAT16 file-system implementation (init, read/write, file.h)
- **user/**: simple userland libraries and example user programs built via user Makefiles
- **examples/**: sample user binaries and FAT16 images for testing
- Top-level Makefiles orchestrate cross-compilation, image creation, QEMU/Bochs launch

##### Extract Architecture Insights from README
- Multi-stage boot → kernel load → FS and process management
- Monolithic design: all kernel services link into one binary
- Relies on virtualization (QEMU/Bochs) and FAT16 disk image

##### Main Components & Interactions

[Hardware Layer]
‣ CPU (x86_64 real/protected/long mode)
‣ Physical RAM
‣ Disk (FAT16 image)
‣ PS/2 Keyboard
‣ Programmable Interval Timer (PIT)

[Bootloader (boot/)]
‣ Stage 1 (real mode) → loads Stage 2 (protected) → Stage 3 (long mode)
‣ Loads `kernel.bin` into memory

[Kernel Core (kernel/)]
‣ **Initialization**: GDT, IDT, paging (4-level), 2 MB pages
‣ **Memory Manager**: physical and virtual memory (`mem.c`, `vm.c`)
‣ **Interrupt Handling**: trap entry (`trap.asm`), interrupt dispatch (`idt.c`)
‣ **Process Manager**: scheduling via timer interrupts
‣ **Syscall Interface**: user ↔ kernel boundary (`syscall.c`)
‣ **Console I/O**: printing on screen (`print.c`)

[Device Drivers (drivers/)]
‣ Keyboard driver (PS/2)
‣ Port I/O abstraction

[File System (fs/fat16/)]
‣ FAT16 driver for on-disk file operations
‣ Exposed to kernel and via system calls to userland

[Userland (user/ & examples/)]
‣ User libraries (`console.c`, `memory.c`, `print.c`)
‣ Sample programs (in examples/) loaded from FAT16 at boot
‣ Interact via system calls

[Development & Build Tools]
‣ Cross-compiler toolchain (dockcross/linux-x64)
‣ Makefiles for Linux/Windows
‣ Bochs/QEMU for emulation

##### Architectural Patterns & Design Principles
- **Layered Kernel**: bootloader → kernel core → subsystems → userland
- **Monolithic Integration**: all services in a single address space, compile-time linked
- **Modular Subsystems**: separate directories for drivers, FS, process, memory
- **Interrupt-driven I/O**: hardware IRQ → IDT → handler → kernel → user

##### Checklist
‣ Clear labels for each module and layer
‣ Notes on major assembler vs. C components (e.g., `kernel.asm`, `trap.asm`, `print.c`)
‣ Entry point markers (e.g., `boot.asm` start, `main.c` in kernel, user `start.c`)

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "Development & Build Tools"
        Dev["Makefiles & Toolchain"]:::dev
    end

    subgraph "Bootloader" 
        Real["Real Mode Stage"]:::boot
        Prot["Protected Mode Stage"]:::boot
        Long["Long Mode Stage"]:::boot
        Loader["Loader & Entry"]:::boot
    end

    subgraph "Kernel Core"
        Entry["Entry & Startup"]:::kernel
        LinkerScript["Linker Script"]:::kernel
        Memory["Memory Manager"]:::kernel
        Interrupt["Interrupt & Trap"]:::kernel
        Process["Process Manager"]:::kernel
        Syscall["Syscall Interface"]:::kernel
        Console["Console I/O"]:::kernel
        Utils["Utility Routines"]:::kernel
    end

    subgraph "Device Drivers"
        PS2["PS/2 Keyboard Driver"]:::driver
        PortIO["Port I/O Abstraction"]:::driver
    end

    subgraph "File System (FAT16)"
        FSImpl["Core Implementation"]:::fs
        FSInit["Init & Operations"]:::fs
        FSFormat["Format Definitions"]:::fs
    end

    subgraph "Userland"
        Lib["User Libraries"]:::user
        ExFS["FS Example Programs"]:::user
        ExPS["Process Example Programs"]:::user
    end

    subgraph "Hardware Layer"
        CPU["CPU x86_64"]:::hardware
        RAM["Physical RAM"]:::hardware
        Disk["Disk (FAT16 image)"]:::hardware
        KBD["PS/2 Keyboard"]:::hardware
        PIT["Programmable Interrupt Timer"]:::hardware
    end

    %% Bootloader Flow
    Real -->|"switch to protected"| Prot
    Prot -->|"switch to long mode"| Long
    Long -->|"invoke loader"| Loader
    Loader -->|"load kernel.bin"| Entry

    %% Kernel Initialization Flow
    Entry -->|"load linker script"| LinkerScript
    Entry -->|"init paging"| Memory
    Entry -->|"setup GDT/IDT"| Interrupt
    Entry -->|"start scheduler"| Process
    Entry -->|"register syscalls"| Syscall
    Entry -->|"init console"| Console
    Entry -->|"load utilities"| Utils

    %% Kernel to Subsystems
    KernelCoreSubgraph(( )) 
    Memory --> Disk
    Interrupt --> PIT
    PS2 --> Interrupt
    PortIO --> Interrupt
    PS2 --> KBD

    %% Device & FS
    Interrupt --> PS2
    Interrupt --> PortIO
    Disk --> FSInit
    FSInit --> FSImpl
    FSImpl --> FSFormat
    FSInit -->|"mount FS"| Entry

    %% Userland Interaction
    Lib -->|"syscall"| Syscall
    ExFS -->|"load from FS"| FSInit
    ExPS -->|"process exec"| Process

    %% Hardware to Bootloader
    CPU --> Real
    RAM --> Real

    %% Development Tools Connections
    Dev --> Bootloader
    Dev --> KernelCoreSubgraph
    Dev --> Lib
    Dev --> ExFS
    Dev --> ExPS

    %% Click Events
    click Real "https://github.com/tingwei628/barukanos/tree/main/boot/real_mode/"
    click Prot "https://github.com/tingwei628/barukanos/tree/main/boot/protected_mode/"
    click Long "https://github.com/tingwei628/barukanos/tree/main/boot/long_mode/"
    click Loader "https://github.com/tingwei628/barukanos/blob/main/boot/boot.asm"
    click Entry "https://github.com/tingwei628/barukanos/blob/main/kernel/main.c"
    click LinkerScript "https://github.com/tingwei628/barukanos/blob/main/kernel/linker.ld"
    click Memory "https://github.com/tingwei628/barukanos/blob/main/kernel/vm.c"
    click Interrupt "https://github.com/tingwei628/barukanos/blob/main/kernel/idt.c"
    click Process "https://github.com/tingwei628/barukanos/blob/main/kernel/process.c"
    click Syscall "https://github.com/tingwei628/barukanos/blob/main/kernel/syscall.c"
    click Console "https://github.com/tingwei628/barukanos/blob/main/kernel/print.c"
    click Utils "https://github.com/tingwei628/barukanos/blob/main/kernel/string.c"
    click PS2 "https://github.com/tingwei628/barukanos/blob/main/drivers/keyboard.c"
    click PortIO "https://github.com/tingwei628/barukanos/blob/main/drivers/port.c"
    click FSImpl "https://github.com/tingwei628/barukanos/blob/main/fs/fat16/impl.c"
    click FSInit "https://github.com/tingwei628/barukanos/blob/main/fs/fat16/init.c"
    click FSFormat "https://github.com/tingwei628/barukanos/blob/main/fs/fat16/file.h"
    click Lib "https://github.com/tingwei628/barukanos/blob/main/user/lib/console.c"
    click ExFS "https://github.com/tingwei628/barukanos/tree/main/examples/fs"
    click ExPS "https://github.com/tingwei628/barukanos/tree/main/examples/process"
    click Dev "https://github.com/tingwei628/barukanos/tree/main/Makefile"

    %% Styles
    classDef boot fill:#d3d3d3,stroke:#777,stroke-width:1px
    classDef kernel fill:#add8e6,stroke:#036,stroke-width:1px
    classDef driver fill:#98fb98,stroke:#060,stroke-width:1px
    classDef fs fill:#ffa500,stroke:#c60,stroke-width:1px
    classDef user fill:#ffff99,stroke:#cc0,stroke-width:1px
    classDef dev fill:#dda0dd,stroke:#940,stroke-width:1px
    classDef hardware fill:#f0f0f0,stroke:#333,stroke-width:1px,stroke-dasharray: 5 5
```
