
Xv6 - jdmoreira
https://en.wikipedia.org/wiki/Xv6
======
oskarth
A few months ago I went through xv6 on my own as an experiment in getting
better at systems programming. I wrote a bunch of posts about it, in case
people are interested:

Grokking xv6:
[http://experiments.oskarth.com/unix00/](http://experiments.oskarth.com/unix00/)

What is a shell and how does it work?:
[http://experiments.oskarth.com/unix01/](http://experiments.oskarth.com/unix01/)

What's on the stack?:
[http://experiments.oskarth.com/unix02/](http://experiments.oskarth.com/unix02/)
(video of tracing a system call from user space to kernel space and back:
[https://www.youtube.com/watch?v=TWksEdn5eoA](https://www.youtube.com/watch?v=TWksEdn5eoA))

Page tables and virtual memory:
[http://experiments.oskarth.com/unix03/](http://experiments.oskarth.com/unix03/)

Locks and concurrency:
[http://experiments.oskarth.com/unix04/](http://experiments.oskarth.com/unix04/)

A short overview of the file system:
[http://experiments.oskarth.com/unix05/](http://experiments.oskarth.com/unix05/)

Grok LOC?
[http://experiments.oskarth.com/unix06/](http://experiments.oskarth.com/unix06/)

It was a very educational experience for me, and I highly recommend the
journey for other people.

~~~
pmilot
Thank you for this. I know what I'm reading for the next two months!

In your original post, you mention one of the goals being able to contribute a
patch to a modern OS such as BSD or Linux. After your experiment, do you feel
like this is the case?

~~~
oskarth
Glad it was useful and motivating! This one I feel a bit bad about, because I
wanted to write a patch and test that hypothesis, but I didn't (yet) make the
time for it.

I _feel_ like I could after maybe ~30h (really I don't have enough information
to determine beyond it probably being between 10 and 100 hours) of focused
effort. I did read a bunch of _The Design and Implementation of FreeBSD_ and I
felt like I could understand it reasonably well, using the mental models I
picked up studying xv6.

The reasons why I didn't do it (yet) are interesting in themselves. There was
nothing in particular that I felt was missing from FreeBSD, and I hadn't used
the "advanced" parts of the OS enough to run into any bugs. Because of this I
think I lacked a sense of ownership, and the artificiality of a bug hunting
activity caught up to me, motivation-wise. To do this I think you need to be
motivated to dive into the nitty-gritty of a particular part of the OS, in
addition to having a good understanding of the OS as a whole. Instead I ended
up exploring some of the more advanced parts of FreeBSD (Jails and ZFS) with
[http://experiments.oskarth.com/netpowder/](http://experiments.oskarth.com/netpowder/)
\- hopefully I will find something useful that I want to either extend or a
bug I want to fix, or perhaps I get to test the hypothesis with something
completely different (such as Mirage OS).

------
gamesurgeon
I'm a student at Johns Hopkins, and we're using xv6 in our OS course this
semester:
[http://gaming.jhu.edu/~phf/2015/fall/cs318/](http://gaming.jhu.edu/~phf/2015/fall/cs318/)

xv6 is pretty awesome for learning, and lecture is generally spent reading
through the source code, some of which is...cute.

Most of us have the source code printed out. If you're interested in reading
the source, this document is well-formated:
[https://pdos.csail.mit.edu/6.828/2014/xv6/xv6-rev8.pdf](https://pdos.csail.mit.edu/6.828/2014/xv6/xv6-rev8.pdf)
(and can actually be generated by the Makefile!)

~~~
sqldba
So the ls and make and all those tools; where do they come from? Are they
straight compiles using GNU gcc tools etc?

I only ask because I have no idea how much is the kernel compared to
everything else; and if it has its own custom compiler or whatever.

~~~
zxexz
No, they are minimal versions written for the OS. Here is ls[0] and cat[1].
You can compile with GCC.

[0] [http://www.ccs.neu.edu/course/cs3650/unix-
xv6/HTML/S/64.html](http://www.ccs.neu.edu/course/cs3650/unix-
xv6/HTML/S/64.html) [1] [http://www.ccs.neu.edu/course/cs3650/unix-
xv6/HTML/S/42.html](http://www.ccs.neu.edu/course/cs3650/unix-
xv6/HTML/S/42.html)

~~~
userbinator
I wonder if a suitably minimalist C compiler (along the lines of C4[1] or
C4x86[2], although perhaps a bit more featured) and/or assembler to go along
with it would be a neat idea too - now you can have a complete self-
bootstrapping OS which one person can easily understand.

[1]
[https://news.ycombinator.com/item?id=8558822](https://news.ycombinator.com/item?id=8558822)

[2]
[https://news.ycombinator.com/item?id=8746054](https://news.ycombinator.com/item?id=8746054)

------
venning
Source available on GitHub: [https://github.com/mit-
pdos/xv6-public](https://github.com/mit-pdos/xv6-public)

Unless I'm missing something, the entire codebase is just 102 files (including
README and such), no subdirectories, and 12,000 lines.

What a fantastic tool.

~~~
mikeash
Any project with a TRICKS file is bound to be good.

------
sarahj
xv6 is an amazing teaching OS - it is very simple to dive into and play around
with. A while ago I wanted to explore some filesystem/permissions stuff and
use xv6 to play with some concepts (see
[http://sarahjamielewis.com/posts/file-system-permissions-
and...](http://sarahjamielewis.com/posts/file-system-permissions-and-xv6.html)
\- still lots I want to play around with there when I find some time) - If you
are at all interested in OS dev it is a great gateway.

~~~
voltagex_
I'm interested, but where would you suggest to start?

~~~
iheartmemcache
I just skimmed the PDF and it's really a good undergraduate text. I learned
off the classic Tanenbaum stuff which is probably still good as a
supplementary text, though it might be suffering the Dragon Book Syndrome at
this point.

The Linux kernel itself is really well documented. Between the docs you can
pull, KernelNewbies.org, the IRC channel, and the O'Reilly text "Understanding
the Linux Kernel, 3rd ed", you can get up to speed re: conventions and develop
a pretty good top-down view.

Then use strace/ltrace excessively on everything to get a bottom-up view.
Strace is a great skill to have in general to help identify bugs in live code
by attaching to the PID. Way more granular than a standard GDB attach. It's
got a learning curve about on par with reading pre-C++11 template error msgs,
but after a few months you'll start to recognize patterns, just as in tmpl
error msgs, to the point where you can just skim the last few hundred lines
strace piped to stdout and you'll know what sort of bug to deal with.

I haven't played with FreeBSD in nearly a decade but I do remember their
documentation was second to none, including the IBM Red Books. So if you want
to see another approach to a POSIX implementation (obviously Tanenbaum talks
about MINIX which is semi-POSIX, so yeah, read that supplementary text), going
through the Handbook from an end-user perspective then the mailing lists +
source will give you a real interesting view as to why certain engineering
decisions were made. I.e., why ipfw was replaced, the gradual progression of
standard file systems from UFS all the way up to modern day ZFS, etc. The list
offers a rare view behind the curtain exploring engineering decisions that
end-users aren't often privy to, and the caliber of conversation is
ridiculously high

------
Galanwe
Some time ago, when i discovered xv6, I started playing with it and I ended
up: \- Refactoring the build system \- Lots of clean of the code \- Adding a
real distinction between distribution and kernel \- A clean libc \- Tons of
other stuff

You can find it here:
[https://github.com/NewbiZ/xv6](https://github.com/NewbiZ/xv6)

Considering the original build system, I think having a look at this just for
the sake of the cleaner makefiles is worth it.

There is also a mailing list for xv6, to share its understanding:
[http://www.freelists.org/list/xv6](http://www.freelists.org/list/xv6)

~~~
gpvos
What did upstream think of it?

------
KenoFischer
There's also OS161, which is Harvard's version of this. It comes with a very
simple MIPS emulator, on top of which the actual operating system is built.
It's a lot of fun!

~~~
dennylin93
The University of Toronto used to use OS161 as well. The OS161 code contains
lots comments, but xv6's textbook [1] covers things in more detail which is
sometimes useful.

[1] [https://pdos.csail.mit.edu/6.828/2014/xv6/book-
rev8.pdf](https://pdos.csail.mit.edu/6.828/2014/xv6/book-rev8.pdf)

------
rcheu
I took 828 and really enjoyed it. It was one of the best classes I took at
MIT. The other OS for that class is JOS, which is the one you build through
the labs. It's similarly simple to understand and not that long to build.

~~~
wfunction
> I took 828 and really enjoyed it. It was one of the best classes I took at
> MIT. The other OS for that class is JOS, which is the one you build through
> the labs. It's similarly simple to understand and not that long to build.

I'm not sure what I'm supposed to take away from your comment aside from "I
went to MIT".

~~~
zem
that the course was really enjoyable. why is that not nice to know?

edit: here's an analogy - say the article was about a famous restaurant, and
someone chimed in to say "i ate there when i was visiting hong kong, and it
was one of the best meals i've ever had". now we already know that it's a good
restaurant; that's what the article was all about after all. nevertheless, i
like that sort of comment; it adds a human touch to the story because (rightly
or wrongly) i perceive a fellow commenter as less remote than a newspaper food
writer, and therefore their opinion has a certain anecdotal quality to it that
the article lacks.

------
escherize
So, is this a minix competitor?

------
thoughtexpt
Best thing about this is that it runs in Bochs.

As useful as it is, especially outside of i386, QEMU has grown to be a massive
program with many dependencies that requires GB of RAM/swap during
compilation.

------
turnip1979
Is there a coursea or other MOOC that uses Xv6?

~~~
gamapuna
[https://pdos.csail.mit.edu/6.828/2014/schedule.html](https://pdos.csail.mit.edu/6.828/2014/schedule.html)
The mit website pretty much has all the information open, which you can use to
go through this course at your own pace. The lectures(most if not all) are on
youtube, search for 6.824

------
lugus35
Where are the unit tests ? where are the valgrind tests ?

------
something123
If it's only for education purposes, why C? Wouldn't you want to choose a more
"clear" language to get the ideas across?

~~~
gliese1337
Because C is the de-facto standard for writing operating systems.

If you want to write an oh-cool blog piece for general programmer audiences
about how OSes work, sure, go ahead and write it in Python (seen that done,
and pretty well, in fact).

But if you want to prepare people to be able to work on real operating
systems, they need to know how to do it in C.

~~~
something123
I think an OS class isn't meant to prepare you for working on the operating
system. The vast majority of people that take an OS class aren't going to work
on OSs, but they _should_ appreciate how everything works. Using a language
optimized for communicating "how everything works" clearly should be a
priority!

~~~
argonaut
Yes, and that language is C. C is a very simple language, and it's very clear.
If you're writing anything that interfaces directly with the OS, you'll need
to know C.

