Convert Figma logo to code with AI

mit-pdos logoxv6-riscv

Xv6 for RISC-V

7,025
2,602
7,025
86

Top Related Projects

1,007

RISC-V Open Source Supervisor Binary Interface

Spike, a RISC-V ISA Simulator

An Agile RISC-V SoC Design Framework with in-order cores, out-of-order cores, accelerators, and more

1,357

Ibex is a small 32 bit RISC-V CPU core, previously known as zero-riscy.

Rocket Chip Generator

Quick Overview

xv6-riscv is an educational operating system developed by MIT for teaching operating system concepts. It is a re-implementation of Dennis Ritchie's and Ken Thompson's Unix Version 6 (v6), adapted for RISC-V architecture. The project aims to provide a simple, clean, and well-documented codebase for students to understand and experiment with OS fundamentals.

Pros

  • Excellent learning resource for understanding operating system internals
  • Clean, well-documented codebase that is easy to read and modify
  • Supports modern RISC-V architecture, making it relevant for current hardware trends
  • Includes a comprehensive set of basic OS features, such as process management, file systems, and device drivers

Cons

  • Limited functionality compared to full-fledged operating systems
  • Not suitable for production use or real-world applications
  • Requires specific hardware or emulator setup to run
  • May have a steeper learning curve for students unfamiliar with low-level programming or computer architecture

Code Examples

Here are a few examples of key components in xv6-riscv:

  1. Process creation (fork system call):
int
fork(void)
{
  int i, pid;
  struct proc *np;
  struct proc *p = myproc();

  // Allocate process.
  if((np = allocproc()) == 0){
    return -1;
  }

  // Copy user memory from parent to child.
  if(uvmcopy(p->pagetable, np->pagetable, p->sz) < 0){
    freeproc(np);
    release(&np->lock);
    return -1;
  }
  np->sz = p->sz;

  // Copy saved user registers.
  *(np->trapframe) = *(p->trapframe);

  // Cause fork to return 0 in the child.
  np->trapframe->a0 = 0;

  // Increment reference counts on open file descriptors.
  for(i = 0; i < NOFILE; i++)
    if(p->ofile[i])
      np->ofile[i] = filedup(p->ofile[i]);
  np->cwd = idup(p->cwd);

  safestrcpy(np->name, p->name, sizeof(p->name));

  pid = np->pid;

  release(&np->lock);

  acquire(&wait_lock);
  np->parent = p;
  release(&wait_lock);

  acquire(&np->lock);
  np->state = RUNNABLE;
  release(&np->lock);

  return pid;
}
  1. File system initialization:
void
fsinit(int dev)
{
  struct superblock sb;

  initlock(&bcache.lock, "bcache");

  readsb(dev, &sb);
  if(sb.magic != FSMAGIC)
    panic("invalid file system");
  initlog(dev, &sb);
}
  1. Context switching:
void
swtch(struct context *old, struct context *new)
{
  if(old != new) {
    asm volatile("sd ra, 0(a0)");
    asm volatile("sd sp, 8(a0)");
    asm volatile("sd s0, 16(a0)");
    asm volatile("sd s1, 24(a0)");
    asm volatile("sd s2, 32(a0)");
    asm volatile("sd s3, 40(a0)");
    asm volatile("sd s4, 48(a0)");
    asm volatile("sd s5, 56(a0)");
    asm volatile("sd s6, 64(a0)");
    asm volatile("sd s7, 72(a0)");
    asm volatile("sd s8, 80(a0)");
    asm volatile("sd s9, 88(a0)");
    asm volatile("sd s10, 96(a0)");
    asm volatile("sd s11, 104(a0)");

    asm volatile("ld ra,

Competitor Comparisons

1,007

RISC-V Open Source Supervisor Binary Interface

Pros of OpenSBI

  • Provides a more comprehensive firmware implementation for RISC-V platforms
  • Offers better hardware support and compatibility across various RISC-V implementations
  • Includes advanced features like SBI extensions and runtime services

Cons of OpenSBI

  • More complex and potentially harder to understand for educational purposes
  • Less focused on teaching operating system concepts
  • May require more setup and configuration for basic usage

Code Comparison

OpenSBI example (platform initialization):

static const struct fdt_match platform_match[] = {
    { .compatible = "riscv,platform" },
    { },
};

PLATFORM_DEF(platform) = {
    .match_table = platform_match,
    .init = platform_init,
    .final_init = platform_final_init,
    .pmp_region_count = platform_pmp_region_count,
};

xv6-riscv example (boot sequence):

void main()
{
  if(cpuid() == 0){
    consoleinit();
    printfinit();
    printf("\n");
    printf("xv6 kernel is booting\n");
    printf("\n");
    kinit();         // physical page allocator
    kvminit();       // create kernel page table
    kvminithart();   // turn on paging
    procinit();      // process table
    trapinit();      // trap vectors
    trapinithart();  // install kernel trap vector
    plicinit();      // set up interrupt controller
    plicinithart();  // ask PLIC for device interrupts
    binit();         // buffer cache
    iinit();         // inode table
    fileinit();      // file table
    virtio_disk_init(); // emulated hard disk
    userinit();      // first user process
    __sync_synchronize();
    started = 1;
  } else {
    while(started == 0)
      ;
    __sync_synchronize();
    printf("hart %d starting\n", cpuid());
    kvminithart();    // turn on paging
    trapinithart();   // install kernel trap vector
    plicinithart();   // ask PLIC for device interrupts
  }

  scheduler();        
}

Spike, a RISC-V ISA Simulator

Pros of riscv-isa-sim

  • More comprehensive RISC-V ISA simulation, supporting a wider range of RISC-V extensions and configurations
  • Actively maintained with frequent updates and contributions from the RISC-V community
  • Provides a flexible framework for developing and testing RISC-V software and hardware designs

Cons of riscv-isa-sim

  • Steeper learning curve due to its complexity and broader scope
  • May be overkill for simple educational purposes or basic RISC-V experiments
  • Requires more system resources to run compared to the lightweight xv6-riscv

Code Comparison

riscv-isa-sim (Spike):

void processor_t::step(size_t n)
{
  for (size_t i = 0; i < n; i++) {
    step_once();
  }
}

xv6-riscv:

void
riscv_trap()
{
  uint64 scause = r_scause();
  switch(scause) {
    case 8:
      // system call
      if(p->killed)
        exit(-1);
      // sepc points to the ecall instruction,
      // but we want to return to the next instruction.
      p->trapframe->epc += 4;
      // an interrupt will change sstatus &c registers,
      // so don't enable until done with those registers.
      intr_on();
      syscall();
      break;
    // ... (other cases)
  }
}

An Agile RISC-V SoC Design Framework with in-order cores, out-of-order cores, accelerators, and more

Pros of chipyard

  • Comprehensive SoC design framework with extensive tooling and integration capabilities
  • Supports a wide range of RISC-V cores and accelerators
  • Provides a flexible simulation environment for hardware-software co-design

Cons of chipyard

  • Steeper learning curve due to its complexity and breadth of features
  • Requires more computational resources for simulation and synthesis
  • May be overkill for simple OS or architecture experiments

Code comparison

xv6-riscv (kernel initialization):

void
main()
{
  kinit();         // physical page allocator
  kvminit();       // create kernel page table
  kvminithart();   // turn on paging
  procinit();      // process table
  trapinit();      // trap vectors
}

chipyard (SoC configuration):

class ExampleConfig extends Config(
  new WithTop ++
  new WithBootROM ++
  new freechips.rocketchip.system.DefaultConfig)

While xv6-riscv focuses on a minimal RISC-V operating system implementation, chipyard provides a comprehensive SoC design framework. xv6-riscv is more suitable for educational purposes and basic OS experiments, whereas chipyard is better suited for complex hardware-software co-design and full-system simulation.

1,357

Ibex is a small 32 bit RISC-V CPU core, previously known as zero-riscy.

Pros of ibex

  • Focused on RISC-V processor core implementation, providing a complete and customizable CPU design
  • Extensive documentation and design verification suite for robust development
  • Actively maintained with regular updates and community contributions

Cons of ibex

  • More complex and specialized, requiring deeper hardware knowledge
  • Less suitable for educational purposes compared to xv6-riscv's simplified OS approach
  • Narrower scope, focusing solely on CPU design rather than a full operating system

Code Comparison

xv6-riscv (kernel initialization):

void
main()
{
  kinit();         // physical page allocator
  kvminit();       // create kernel page table
  kvminithart();   // turn on paging
  procinit();      // process table
  trapinit();      // trap vectors
}

ibex (core instantiation):

ibex_core #(
  .DmHaltAddr(DMHaltAddr),
  .DmExceptionAddr(DMExceptionAddr)
) u_core (
  .clk_i(clk_i),
  .rst_ni(rst_ni),
  .test_en_i(test_en_i),
  .hart_id_i(hart_id_i),
  .boot_addr_i(boot_addr_i)
);

Rocket Chip Generator

Pros of rocket-chip

  • More comprehensive RISC-V implementation, including a full SoC generator
  • Actively maintained and developed by a larger community
  • Supports various RISC-V extensions and custom instructions

Cons of rocket-chip

  • Higher complexity and steeper learning curve
  • Less suitable for educational purposes compared to xv6-riscv
  • Requires more resources to build and simulate

Code Comparison

rocket-chip (Chisel):

class Rocket extends Module {
  val io = IO(new Bundle {
    val imem = new FrontendIO
    val dmem = new HellaCacheIO
    val ptw = new TLBPTWIO
  })
  // ...
}

xv6-riscv (C):

struct cpu {
  struct proc *proc;          // The process running on this cpu, or null
  struct context context;     // swtch() here to enter scheduler()
  int noff;                   // Depth of push_off() nesting
  int intena;                 // Were interrupts enabled before push_off()?
};

The rocket-chip code showcases its use of Chisel for hardware description, while xv6-riscv uses C for its operating system implementation. rocket-chip focuses on hardware design, while xv6-riscv is an educational operating system.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

xv6 is a re-implementation of Dennis Ritchie's and Ken Thompson's Unix Version 6 (v6). xv6 loosely follows the structure and style of v6, but is implemented for a modern RISC-V multiprocessor using ANSI C.

ACKNOWLEDGMENTS

xv6 is inspired by John Lions's Commentary on UNIX 6th Edition (Peer to Peer Communications; ISBN: 1-57398-013-7; 1st edition (June 14, 2000)). See also https://pdos.csail.mit.edu/6.1810/, which provides pointers to on-line resources for v6.

The following people have made contributions: Russ Cox (context switching, locking), Cliff Frey (MP), Xiao Yu (MP), Nickolai Zeldovich, and Austin Clements.

We are also grateful for the bug reports and patches contributed by Takahiro Aoyagi, Marcelo Arroyo, Silas Boyd-Wickizer, Anton Burtsev, carlclone, Ian Chen, Dan Cross, Cody Cutler, Mike CAT, Tej Chajed, Asami Doi,Wenyang Duan, eyalz800, Nelson Elhage, Saar Ettinger, Alice Ferrazzi, Nathaniel Filardo, flespark, Peter Froehlich, Yakir Goaron, Shivam Handa, Matt Harvey, Bryan Henry, jaichenhengjie, Jim Huang, Matúš Jókay, John Jolly, Alexander Kapshuk, Anders Kaseorg, kehao95, Wolfgang Keller, Jungwoo Kim, Jonathan Kimmitt, Eddie Kohler, Vadim Kolontsov, Austin Liew, l0stman, Pavan Maddamsetti, Imbar Marinescu, Yandong Mao, Matan Shabtay, Hitoshi Mitake, Carmi Merimovich, Mark Morrissey, mtasm, Joel Nider, Hayato Ohhashi, OptimisticSide, phosphagos, Harry Porter, Greg Price, RayAndrew, Jude Rich, segfault, Ayan Shafqat, Eldar Sehayek, Yongming Shen, Fumiya Shigemitsu, snoire, Taojie, Cam Tenny, tyfkda, Warren Toomey, Stephen Tu, Alissa Tung, Rafael Ubal, Amane Uehara, Pablo Ventura, Xi Wang, WaheedHafez, Keiichi Watanabe, Lucas Wolf, Nicolas Wolovick, wxdao, Grant Wu, x653, Jindong Zhang, Icenowy Zheng, ZhUyU1997, and Zou Chang Wei.

ERROR REPORTS

Please send errors and suggestions to Frans Kaashoek and Robert Morris (kaashoek,rtm@mit.edu). The main purpose of xv6 is as a teaching operating system for MIT's 6.1810, so we are more interested in simplifications and clarifications than new features.

BUILDING AND RUNNING XV6

You will need a RISC-V "newlib" tool chain from https://github.com/riscv/riscv-gnu-toolchain, and qemu compiled for riscv64-softmmu. Once they are installed, and in your shell search path, you can run "make qemu".