Thursday, 13 March 2025

XV6 RISCV OS

 Intro

The Hackaday tutorial to write an Operating System from scratch provided a wonderful inspiring introduction.  It explained many features exclusive to supervisors: context switching, system calls, paging, scheduling, privileged instructions and user shells.

XV6 was written by MIT for their OS Engineering course.  Originally they used Unix Version 6 running on a PDP-11 computer but later created XV6 which now runs on x86 and RISC-V.  XV6 is based on the classic Unix V6 from Thompson and Ritchie and is therefore similar in architecture and design to Unix, Linux, MacOS etc.
As a teaching system it contains a subset of relevant features and is easier/quicker to build.

I first came across XV6 as Michael Engel had developed a 32-bit variant which ran on Nezha RISC-V hardware.  I decided to implement XV6 on QEMU rather than on Nezha as it is easier to use an emulator initially.  I may try to build on hardware eventually.

Environment

XV6 can be found on github which has some terse build instructions.



My Raspberry Pi PI41 already has QEMU 7.12 installed.  It is necessary to have riscv64-softmmu configured tor "complete system emulation including an emulated MMU for running bare metal programs or OS" rather than "linux user mode" which just translates linux syscalls to host calls.  Since the executable qemu-system-riscv64 exists I have the correct version.

The riscv-gnu-toolchain provides a C cross-compiler and linker so that RISCV executables can be built on RPI and then executed on (QEMU virtual) RISCV hardware.  The instructions make it look very straightforward.


In practice I struggled somewhat to get a working version until I found a note in the instructions that I needed to configure a switch "--enable-qemu-system" to create a complete/correct toolchain.

There is a lot of work to do to build all the necessary executables.  On my RPI4 it took about four hours execution time and utilised 6.6GB.


XV6

Once the QEMU and Toolchain pre-requisites have been successfully completed it is simple to build XV6.  The Makefile builds a system and executes it in QEMU.  On completion we can see the magical '$' prompt from the shell command line.😀😀😀😀


Since it is a unix-style OS we have an "ls" command to display files in the current directory.

Outro


We leave the story at this point.  We have a simple working OS for which we can write user programs.  We can check out many Unix features including forking, multi-tasking, pipes, inter-process communication on our simple system.  We can add extra capabilities and modify the kernel if we want to.
In practice this programming will be more demanding than building the system.  The kind folk at MIT provide an XV6 manual and a series of labs to learn about (Unix) operating system capabilities.  There is an awful lot I can learn if my C programming is up to the task.


  



No comments:

Post a Comment