# xv6 - Introduction

xv6 1 / 3
2 min read
Table of Contents

Why??

I am currently going through the book Operating Systems: Three Easy Pieces, and the next step after completing the first piece, Virtualisation, is to go through the xv6 kernel. The xv6 kernel had two implementations, the x86 one and RISC-V one. As the x86 one is now unmaintained and on recommendation of a mentor, I am using xv6-riscv! So without further ado, let’s get started.

The xv6 Kernel

  • Based on Unix kernel
  • Created by MIT for educational purposes
  • Implemented for RISC-V
  • Multicore
  • ~6000 lines of code (C and Assembly)

Features

  • Processes
  • Virtual Address Spaces
    • Page Tables
  • Files and Directories
  • Pipes
  • Multitasking
    • Time-Slicing
  • 21 system calls

User Programs

  • sh
  • cat
  • echo
  • grep
  • kill
  • ln
  • ls
  • mkdir
  • rm
  • wc

What is missing?

All the complexity of a “real” operating system is missing. For example, UserID, file protections, IPC, “mount”able filesystem, etc.

SMP: Shared Memory Multiprocesser

CPU = core = HART

Main memory (RAM) is shared and is of size 128 Mbytes

Supported Devices

  • UART (Serial Comm. Tx <=> Rx)
  • Disk
  • Timer Interrupts
  • PLIC: Platform Level Interrupt Controller
  • CLINT: Core Local Interrupt Controller

Memory Management

  • Page Size = 4096 bytes (#define PGSIZE)
  • Single Free List
  • No variable sized allocation
  • No “malloc”
  • Page Tables-
    • Three Levels
    • One table per process + One table for Kernel
    • Pages marked - R/W/X/U/V

Scheduler

  • Round Robin
  • Size of TimeSlice is fixed (1,000,000 cycles)
  • All cores share one “Ready Queue”
  • Next TimeSlice may be on a different core

Boot Sequence

  • QEMU
    • Loads kernel code at a fixed address (0x8000-0000)
    • Starts all cores running
  • No bootloader/boot-block/BIOS

Locking

  • Spin Locks
  • sleep() & wakeup()

”Param.h”

  • Fixed Limits (like no. of processes, no. of open files, etc)
  • Several Arrays (“kill(pid)” => Linear search of Processes array)

User Address Space

img
Sorry for the bad handwriting!

Arguments (argc, argv) will be placed on the stack before the program begins execution.

RISC-V Virtual Addresses

  • Multiple Schemes are available

    • Sv32 -> Used for 2-level Page Tables
    • Sv39 -> For 3-levels
    • Sv48 -> For 4-levels!
  • xv6 uses Sv39

  • VA Size-

    39 bits
    2^39 = 512 GB
    = 0x80-0000-0000
  • But xv6 uses only 38 bits for its VAs

    38 bits
    2^38 = 256 GB
    = 0x40-0000-0000
  • Therefore the allowed range is - 0…0x3F-FFFF-FFFF

Startup

We go from

entry.S -> start.c -> main.c
(Setup Stack) (Machine Mode) (Supervisor Mode)
Next: xv6 - SpinLocks
My avatar

Thanks for reading my blog post! Feel free to check out my other posts or contact me via the social links in the footer.


xv6 Series