# logicalmonster/xv6-riscv-2020 architecture

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

- Interactive diagram: https://gitdiagram.com/logicalmonster/xv6-riscv-2020
- Repository: https://github.com/logicalmonster/xv6-riscv-2020

## Overview

##### Main components (“classes”) in xv6
- Process (found in kernel/proc.h and implemented in kernel/proc.c)
– Attributes: Process ID, state, context (stored in a context record), trapframe, scheduling information, etc.
– Methods (in a conceptual sense): creation, termination, context switching, scheduling.
- File (in kernel/file.h and kernel/file.c)
– Attributes: file type, reference counts, read/write pointers, inode reference (linked to the filesystem), etc.
– Methods: open, close, read, write.
- Filesystem & Inodes (coordinated by kernel/fs.h, kernel/fs.c)
– Inode “class”: Attributes related to file metadata, block pointers, etc.
– Filesystem “manager”: operations for file lookup, directory operations, etc.
- Memory Management (in kernel/vm.c and kernel/kalloc.c)
– “Memory Manager” class: handles virtual memory, page allocation, memory mapping.
- Locking Mechanisms:
– SpinLock (kernel/spinlock.h / .c) – with acquire and release functions.
– SleepLock (kernel/sleeplock.h / .c) – similar locking abstraction used during sleep–wakeup.
- Buffer Cache (from kernel/bio.c and associated header kernel/buf.h)
– “Buffer” class: encapsulates in-memory copies of disk blocks.
- System Call Interface (spread across kernel/syscall.h, syscall.c, sysfile.c, sysproc.c)
– “Syscall Manager”: interprets system calls from user programs and dispatches them to the appropriate kernel routines.
- Console/Device Handling:
– Console/Output (kernel/console.c) – handling basic I/O.
– Other device-related “classes”: UART (kernel/uart.c), Virtio disk (kernel/virtio_disk.c), PLIC (kernel/plic.c).

##### Relationships and interactions
- The Process “class” depends on the Scheduler (within proc.c and context-switch code in swtch.S) and uses the System Call Interface.
- The File “class” interacts with the Filesystem/Inode components and the Buffer Cache.
- The Filesystem “class” acts as a manager that ties together inodes, file structures, and disk buffers.
- Memory Management is a cross-cutting infrastructure that is used by process management, file caching, and device drivers.
- Locking “classes” (SpinLock and SleepLock) are utilized by nearly all other modules to secure critical sections.

- Clear class (or struct) names (e.g., Process, File, Inode, SpinLock, MemoryManager, Buffer).
- Attributes (fields) and operations (functions or “methods”) where it helps to understand responsibilities.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "User Space"
        UP["User Process"]:::user
    end

    subgraph "Kernel Core"
        SC["SystemCallManager"]:::control
        P["Process"]:::kernel
        F["File"]:::kernel
        FS["FileSystem"]:::kernel
        I["Inode"]:::kernel
        MM["MemoryManager"]:::control
        BC["BufferCache"]:::kernel
    end

    subgraph "Locking Mechanisms"
        SL["SpinLock"]:::kernel
        SLP["SleepLock"]:::kernel
    end

    subgraph "Device I/O"
        CON["Console"]:::driver
        UART_NODE["UART"]:::driver
        VD["VirtioDisk"]:::driver
        PLIC_NODE["PLIC"]:::driver
    end

    %% Relationships
    UP -->|"syscall"| SC
    SC -->|"dispatch"| P
    SC -->|"file_ops"| F
    P -->|"uses"| MM
    F -->|"cache"| BC
    FS -->|"aggregates"| F
    FS -->|"aggregates"| I
    SL -.->|"lock"| P
    SL -.->|"lock"| F
    SL -.->|"lock"| MM
    SLP -.->|"lock"| P
    SLP -.->|"lock"| F
    SLP -.->|"lock"| MM
    SC -->|"to_console"| CON
    SC -->|"to_UART"| UART_NODE
    SC -->|"to_Virtio"| VD
    SC -->|"to_PLIC"| PLIC_NODE

    %% Click Events
    click P "https://github.com/logicalmonster/xv6-riscv-2020/blob/util/kernel/proc.h"
    click P "https://github.com/logicalmonster/xv6-riscv-2020/blob/util/kernel/proc.c"
    click F "https://github.com/logicalmonster/xv6-riscv-2020/blob/util/kernel/file.h"
    click F "https://github.com/logicalmonster/xv6-riscv-2020/blob/util/kernel/file.c"
    click FS "https://github.com/logicalmonster/xv6-riscv-2020/blob/util/kernel/fs.h"
    click FS "https://github.com/logicalmonster/xv6-riscv-2020/blob/util/kernel/fs.c"
    click MM "https://github.com/logicalmonster/xv6-riscv-2020/blob/util/kernel/vm.c"
    click MM "https://github.com/logicalmonster/xv6-riscv-2020/blob/util/kernel/kalloc.c"
    click SL "https://github.com/logicalmonster/xv6-riscv-2020/blob/util/kernel/spinlock.h"
    click SL "https://github.com/logicalmonster/xv6-riscv-2020/blob/util/kernel/spinlock.c"
    click SLP "https://github.com/logicalmonster/xv6-riscv-2020/blob/util/kernel/sleeplock.h"
    click SLP "https://github.com/logicalmonster/xv6-riscv-2020/blob/util/kernel/sleeplock.c"
    click BC "https://github.com/logicalmonster/xv6-riscv-2020/blob/util/kernel/bio.c"
    click BC "https://github.com/logicalmonster/xv6-riscv-2020/blob/util/kernel/buf.h"
    click SC "https://github.com/logicalmonster/xv6-riscv-2020/blob/util/kernel/syscall.h"
    click SC "https://github.com/logicalmonster/xv6-riscv-2020/blob/util/kernel/syscall.c"
    click SC "https://github.com/logicalmonster/xv6-riscv-2020/blob/util/kernel/sysfile.c"
    click SC "https://github.com/logicalmonster/xv6-riscv-2020/blob/util/kernel/sysproc.c"
    click CON "https://github.com/logicalmonster/xv6-riscv-2020/blob/util/kernel/console.c"
    click UART_NODE "https://github.com/logicalmonster/xv6-riscv-2020/blob/util/kernel/uart.c"
    click VD "https://github.com/logicalmonster/xv6-riscv-2020/blob/util/kernel/virtio_disk.c"
    click PLIC_NODE "https://github.com/logicalmonster/xv6-riscv-2020/blob/util/kernel/plic.c"

    %% Styles
    classDef user fill:#FFDAB9,stroke:#333,stroke-width:2px;
    classDef kernel fill:#ADD8E6,stroke:#333,stroke-width:2px;
    classDef control fill:#90EE90,stroke:#333,stroke-width:2px;
    classDef driver fill:#FFFFE0,stroke:#333,stroke-width:2px;
```
