Thursday, 27 April 2023

XV6

What is XV6

XV6 is an Operating System.  It is a reimplementation of Unix version 6 developed by MIT for educational purposes in 2006.  Originally Unix V6 ran on PDP-11 and was written in an old version of C.  XV6 was written for x66 PCs in ANSI C with a little assembly code. 

The source code of Unix V6 and XV6 is openly available.  XV6 is only about 6000 lines of code and is well-written; individual parts of the OS are easily recognisable.  Although it would be challenging to understand it all, this is the ultimate way of familiarising yourself with Operating System functions and code in general and Unix / Linux in particular.  

Build XV6

Michael Engel has adapted xv6 to run on an Allwinner Nezha RISC-V system and provided the source plus instructions on github.  The instructions look straightforward but it has taken me a while to get them to work.



The first step is to find the riscv-unknown-elf toolchain for building xv6.  I hoped I could use the Nezha itself but it runs riskv64-linux-gnu so that was a non-starter.  I found a Ubuntu package for riscv64-gcc-linux and was able to install it on WSL using apt install gcc-riscv64-linux-gnu.  This enabled me to cross compile programs on WSL and run them under linux on Nezha.  However I need a toolchain with "unknown" in the title as I won't have an existing OS on the target architecture.

I then found riscv-unknown-elf on github together with instructions to build it.  Not as easy as installing a package with apt, but worth a shot.


The instructions were good, I followed them carefully and a few hours and 6.65GB later the build said it was successful.

Next step is to build XV6 using riscv-unknown-elf, so I cloned Micahel Engel's xv6 and built it.  There was a compilation error when building fs.img but google provided a resolution.  I now have a file kernel.bin containing my very own Unix v6 OS which I need to load into Nezha RAM.

FEL mode

There isn't a way to put xv6 on an SD card, as it doesn't contain a disk driver so we have to load it into RAM. Allwinner provide low level routine to do this:


The Allwinner user community Sunxi,  provides wonderful resources for using Allwinner SoCs.  They explain how to install / use the xfel tool in conjunction with a Nezha in FEL mode.

After much difficulty I worked out that I could install the allwinner XFEL tool on a Raspberry Pi (PI41) and connect it to Nezha in FEL mode.  It was comparatively easy to build the XFEL tool and download my kernel to PI41.  I then pressed the Nezha FEL button and connected a PI41 USB port to the Nezha OTG USB-C port.  I could now see a new USB device from PI41.  I connected a USB FTDI serial cable to Nezha GPIO header pins so that I could see a Nezha console.  I loaded and ran a same file oreboot-licheerv.bin first and saw Nezha burst into life.


Finally I was in a position to try my new kernel.  I repeated the procedure with kernel.bin and it worked๐Ÿ˜€๐Ÿ˜€๐Ÿ˜€.  A message, followed by a command prompt flashed up on the screen.  Typing ls showed me a directory listing, including available commands:


I have built a real (but rather primitive) RISC-V Unix OS which I can tinker with or add to as much as I like.

Alternative XV6


As yet I cant think of much to test on my new Unix system and will need to find a suitable tutorial to teach me.  The XV6 is an educational system, used by many universities and there many resources out there to help me.
Whilst looking for something suitable I came across a youtube video which shows a ridiculously easy method of setting up XV6 on Windows WSL. 

 I spent a few minutes doing this and now have a second version available to me.  In all honesty it will be much easier to use WSL rather than a Nezha in FEL mode to tinker, but I have both available, they both "look" the same and are likely to be interchangeable.  I could easily develop on WSL before finalising on Nezha.




No comments:

Post a Comment