# mit-pdos/xv6-public architecture

> Architecture diagram of the GitHub repository mit-pdos/xv6-public, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mit-pdos/xv6-public
- Repository: https://github.com/mit-pdos/xv6-public

## Overview

##### Main System Components

a) Boot Sequence
- Bootasm.S and bootmain.c
- Transition from boot to kernel

b) Kernel Core
- Process Management (proc.c, proc.h)
- Memory Management (vm.c, kalloc.c)
- File System (fs.c, fs.h, file.c, file.h)
- Device Drivers (console.c, ide.c, uart.c)

c) Hardware Interface Layer
- Interrupt Handling (trap.c, trapasm.S, vectors.pl)
- CPU Management (lapic.c, ioapic.c)
- Multiprocessor Support (mp.c, mp.h)

d) System Calls
- System Call Interface (syscall.c, syscall.h)
- File Operations (sysfile.c)
- Process Operations (sysproc.c)

**┌─────────────────────────────────────────────┐**
- User Programs                │
- (sh.c, ls.c, cat.c, init.c, etc.)         │
- ┤
- System Call Layer              │
- (syscall.c, sysfile.c, sysproc.c)      │
- ┤
- Kernel Core                   │
- ┬───────────────┬────────────┤
- Process Mgmt  │ Memory Mgmt   │ File System│
- (proc.c)     │   (vm.c)      │  (fs.c)   │
- ┴───────────────┴────────────┤
- Hardware Interface Layer         │
- (trap.c, ioapic.c, lapic.c, ide.c)       │
- ┤
- Hardware (x86)                │
- ┘

##### Key Relationships to Highlight

a) Process Management:
- How proc.c interacts with memory management
- Context switching paths
- Scheduler interactions

b) Memory Management:
- Virtual memory mapping
- Page table management
- Memory allocation flows

c) File System:
- Relationship between file.c and fs.c
- Buffer cache interactions (bio.c)
- Inode management flows

##### Implementation Details

- Use UML or similar standardized notation
- Maintain clean separation between layers
- Both control and data flow paths
- Important data structures (proc, file, inode)

## Diagram (Mermaid)

```mermaid
graph TB
    %% Styles
    classDef user fill:#b3d9ff,stroke:#333,stroke-width:2px
    classDef kernel fill:#98FB98,stroke:#333,stroke-width:2px
    classDef hardware fill:#FFE4B5,stroke:#333,stroke-width:2px
    classDef physical fill:#D3D3D3,stroke:#333,stroke-width:2px
    classDef fs fill:#90EE90,stroke:#333,stroke-width:2px
    classDef boot fill:#FFA07A,stroke:#333,stroke-width:2px

    %% Hardware Layer
    subgraph Hardware
        HW[x86 Hardware]:::physical
    end

    %% Boot Sequence
    subgraph Boot
        BOOTASM["Boot Assembly"]:::boot
        BOOTMAIN["Boot Main"]:::boot
    end
    click BOOTASM "https://github.com/mit-pdos/xv6-public/blob/master/bootasm.S"
    click BOOTMAIN "https://github.com/mit-pdos/xv6-public/blob/master/bootmain.c"

    %% Hardware Interface Layer
    subgraph HWInterface
        TRAP["Trap Handler"]:::hardware
        LAPIC["Local APIC"]:::hardware
        IOAPIC["I/O APIC"]:::hardware
        IDE["IDE Controller"]:::hardware
        UART["UART"]:::hardware
        CONSOLE["Console"]:::hardware
        KBD["Keyboard"]:::hardware
        PICIRQ["PIC IRQ"]:::hardware
    end
    click TRAP "https://github.com/mit-pdos/xv6-public/blob/master/trap.c"
    click LAPIC "https://github.com/mit-pdos/xv6-public/blob/master/lapic.c"
    click IOAPIC "https://github.com/mit-pdos/xv6-public/blob/master/ioapic.c"
    click IDE "https://github.com/mit-pdos/xv6-public/blob/master/ide.c"
    click UART "https://github.com/mit-pdos/xv6-public/blob/master/uart.c"
    click CONSOLE "https://github.com/mit-pdos/xv6-public/blob/master/console.c"
    click KBD "https://github.com/mit-pdos/xv6-public/blob/master/kbd.c"
    click PICIRQ "https://github.com/mit-pdos/xv6-public/blob/master/picirq.c"

    %% Kernel Core
    subgraph KernelCore
        %% Process Management
        subgraph ProcessMgmt
            PROC["Process Manager"]:::kernel
            MP["Multiprocessor"]:::kernel
        end
        click PROC "https://github.com/mit-pdos/xv6-public/blob/master/proc.c"
        click MP "https://github.com/mit-pdos/xv6-public/blob/master/mp.c"

        %% Memory Management
        subgraph MemoryMgmt
            VM["Virtual Memory"]:::kernel
            KALLOC["Memory Allocator"]:::kernel
            MMU["Memory Layout"]:::kernel
        end
        click VM "https://github.com/mit-pdos/xv6-public/blob/master/vm.c"
        click KALLOC "https://github.com/mit-pdos/xv6-public/blob/master/kalloc.c"
        click MMU "https://github.com/mit-pdos/xv6-public/blob/master/mmu.h"

        %% File System
        subgraph FileSystem
            FS["File System"]:::fs
            FILE["File Operations"]:::fs
            LOG["Log"]:::fs
        end
        click FS "https://github.com/mit-pdos/xv6-public/blob/master/fs.c"
        click FILE "https://github.com/mit-pdos/xv6-public/blob/master/file.c"
        click LOG "https://github.com/mit-pdos/xv6-public/blob/master/log.c"

        %% Synchronization
        subgraph Sync
            SPINLOCK["Spin Lock"]:::kernel
            SLEEPLOCK["Sleep Lock"]:::kernel
        end
        click SPINLOCK "https://github.com/mit-pdos/xv6-public/blob/master/spinlock.c"
        click SLEEPLOCK "https://github.com/mit-pdos/xv6-public/blob/master/sleeplock.c"
    end

    %% System Call Layer
    subgraph SystemCalls
        SYSCALL["System Call Handler"]:::kernel
        SYSFILE["File System Calls"]:::kernel
        SYSPROC["Process System Calls"]:::kernel
    end
    click SYSCALL "https://github.com/mit-pdos/xv6-public/blob/master/syscall.c"
    click SYSFILE "https://github.com/mit-pdos/xv6-public/blob/master/sysfile.c"
    click SYSPROC "https://github.com/mit-pdos/xv6-public/blob/master/sysproc.c"

    %% User Programs
    subgraph UserPrograms
        SHELL["Shell"]:::user
        LS["List Directory"]:::user
        CAT["Concatenate"]:::user
        INIT["Init"]:::user
        GREP["Grep"]:::user
        KILL["Kill"]:::user
        MKDIR["Make Directory"]:::user
        RM["Remove"]:::user
        WC["Word Count"]:::user
    end
    click SHELL "https://github.com/mit-pdos/xv6-public/blob/master/sh.c"
    click LS "https://github.com/mit-pdos/xv6-public/blob/master/ls.c"
    click CAT "https://github.com/mit-pdos/xv6-public/blob/master/cat.c"
    click INIT "https://github.com/mit-pdos/xv6-public/blob/master/init.c"
    click GREP "https://github.com/mit-pdos/xv6-public/blob/master/grep.c"
    click KILL "https://github.com/mit-pdos/xv6-public/blob/master/kill.c"
    click MKDIR "https://github.com/mit-pdos/xv6-public/blob/master/mkdir.c"
    click RM "https://github.com/mit-pdos/xv6-public/blob/master/rm.c"
    click WC "https://github.com/mit-pdos/xv6-public/blob/master/wc.c"

    %% Relationships
    BOOTASM --> BOOTMAIN
    BOOTMAIN --> PROC
    
    UserPrograms --> SYSCALL
    SYSCALL --> SYSFILE & SYSPROC
    
    SYSFILE --> FS
    SYSPROC --> PROC
    
    PROC --> VM
    PROC --> SPINLOCK
    FILE --> SLEEPLOCK
    
    VM --> MMU
    VM --> KALLOC
    
    TRAP --> LAPIC
    TRAP --> IOAPIC
    TRAP --> PICIRQ
    
    CONSOLE --> KBD
    CONSOLE --> UART
    
    HWInterface --> HW

    %% Legend
    subgraph Legend
        U1[User Space]:::user
        K1[Kernel Space]:::kernel
        H1[Hardware Interface]:::hardware
        P1[Physical Hardware]:::physical
        B1[Boot Sequence]:::boot
    end
```
