# Xv6, a simple Unix-like teaching operating system

## Introduction

Xv6 is a teaching operating system developed in the summer of 2006 for MIT's operating systems course, 6.828: operating systems Engineering. We hope that xv6 will be useful in other courses too. This page collects resources to aid the use of xv6 in other courses, including a commentary on the source code itself.

Status: The xv6 code is in pretty good shape, but the commentary is rough.

## History and Background

For many years, MIT had no operating systems course. In the fall of 2002, Frans Kaashoek, Josh Cates, and Emil Sit created a new, experimental course (6.097) to teach operating systems engineering. In the course lectures, the class worked through Sixth Edition Unix (aka V6) using John Lions's famous commentary. In the lab assignments, students wrote most of an exokernel operating system, eventually named Jos, for the Intel x86. Exposing students to multiple systems–V6 and Jos–helped develop a sense of the spectrum of operating system designs. In the fall of 2003, the experimental 6.097 became the official course 6.828; the course has been offered each fall since then.

V6 presented pedagogic challenges from the start. Students doubted the relevance of an obsolete 30-year-old operating system written in an obsolete programming language (pre-K&R C) running on obsolete hardware (the PDP-11). Students also struggled to learn the low-level details of two different architectures (the PDP-11 and the Intel x86) at the same time. By the summer of 2006, we had decided to replace V6 with a new operating system, xv6, modeled on V6 but written in ANSI C and running on multiprocessor Intel x86 machines. Xv6's use of the x86 makes it more relevant to students' experience than V6 was and unifies the course around a single architecture. Adding multiprocessor support requires handling concurrency head on with locks and threads (instead of using special-case solutions for uniprocessors such as enabling/disabling interrupts) and helps relevance. Finally, writing a new system allowed us to write cleaner versions of the rougher parts of V6, like the scheduler and file system.

6.828 substituted xv6 for V6 in the fall of 2006. Based on that experience, we cleaned up rough patches of xv6. Since then, xv6 has stabilized, so we are making it available in the hopes that others will find it useful too.

6.828 uses both xv6 and Jos. Courses taught at UCLA, NYU, Peking University, Stanford, Tsinghua, and University Texas (Austin) have used Jos without xv6; we believe other courses could use xv6 without Jos, though we are not aware of any that have.

## Xv6 sources

The latest xv6 is xv6-rev5.tar.gz. We distribute the sources in electronic form but also as a printed booklet with line numbers that keep everyone together during lectures. The booklet is available as xv6-rev5.pdf. The xv6 source code is licensed under the traditional MIT license; see the LICENSE file in the source distribution.

xv6 compiles using the GNU C compiler, targeted at the x86 using ELF binaries. On BSD and Linux systems, you can use the native compilers; On OS X, which doesn't use ELF binaries, you must use a cross-compiler. Xv6 does boot on real hardware, but typically we run it using the Bochs emulator. Both the GCC cross compiler and Bochs can be found on the 6.828 tools page.

## Xv6 lecture material

In 6.828, the lectures in the first half of the course introduce the PC hardware, the Intel x86, and then xv6. The lectures in the second half consider advanced topics using research papers; for some, xv6 serves as a useful base for making discussions concrete. The lecture notes are available from the 6.828 schedule page, and the chapters of the commentary are below.

## Xv6 commentary (rough)

The chapters are rough drafts.

Introduction yet to be written.

- read with the code side by side
- code references look like (xxxx) or (xxxx-yyyy) in small text.
- this pdf is the one with matching line numbers.
- each chapter starts with an introduction to the topic, spends most of the text on code, and then wraps up talking about how xv6 compares to real-world operating systems.

Chapter 0: Operating system interfaces

> The Unix system call interface. (rev 4)

Chapter 1: Bootstrap

> From power on to kernel start. (rev 4)

Chapter 2: Processes

> Memory and process allocation, segments, the first user process. (rev 4)

Chapter 3: Traps

> Low-level trap mechanism, trap handler, system call arguments, sbrk, fork.

Chapter 4: Locks

> Locks and interrupts.

Chapter 5: Scheduling and coordination

> Scheduling, sleep and wakeup, pipes, wait and exit.

Chapter 6: Buffer cache

> Buffer cache and IDE disk driver.

Chapter 7: File system data

> Block in use bitmap, block allocation, inode structure, inode contents, directories, path names.

Chapter 8: File system calls

> FIle descriptors, open, close, dup, read, write.

Chapter 9: Exec

> Exec

Appendix A: Low-level C and inline assembly

> Intro to C and inline assembly for people who only know Java (say). Examples drawn entirely from xv6 source.

Appendix B: Additional drivers.

> Keyboard, screen, probably MP hardware.

## Unix Version 6

6.828's xv6 is inspired by Unix V6 and by:

- Lions' Commentary on UNIX' 6th Edition, John Lions, Peer to Peer Communications; ISBN: 1-57398-013-7; 1st edition (June 14, 2000).
  - An on-line version of the Lions commentary, and the source code.
  - The v6 source code is also available online through the PDP Unix Preservation Society.

The following are useful to read the original code:

- The PDP11/40 Processor Handbook, Digital Equipment Corporation, 1972.
  - A PDF (made from scanned images, and not text-searchable)
  - A web-based version that is indexed by instruction name.

## Feedback

If you are interested in using xv6 or have used xv6 in a course, we would love to hear from you. If there's anything that we can do to make xv6 easier to adopt, we'd like to hear about it. We'd also be interested to hear what worked well and what didn't.

Russ Cox (rsc@swtch.com)
Frans Kaashoek (kaashoek@mit.edu)
Robert Morris (rtm@mit.edu)

You can reach all of us at 6.828-staff@pdos.csail.mit.edu.