Writing A RISC-V OS From Scratch

If you read Japanese, you might have seen the book “Design and Implementation of Microkernels” by [Nu Tian Sheng]. An appendix covers how to write your own operating system for RISC-V in about 1,000 lines of code. Don’t speak Japanese? An English version is available free on the Web and on GitHub.

The author points out that the original Linux kernel wasn’t much bigger (about 8,500 lines). The OS allows for paging, multitasking, a file system, and exception handling. It doesn’t implement interrupt handling, timers, inter-process communication, or handling of multiple processors. But that leaves you with something to do!

The online book covers everything from booting using OpenSBI to building a command line shell. Honestly, we’d have been happier with some interrupt scheme and any sort of crude way to communicate and synchronize across processes, but the 1,000 line limit is draconian.

Since the project uses QEMU as an emulation layer, you don’t even need any special hardware to get started. Truthfully, you probably won’t want to use this for a production project, but for getting a detailed understanding of operating systems or RISC-V programming, it is well worth a look.

If you want something more production-ready, you have choices. Or, stop using an OS at all.

11 thoughts on “Writing A RISC-V OS From Scratch

  1. I don’t “speak Japanese” which wouldn’t help with a book anyway but I do read Chinese and the writer’s name is Chinese so is there a Chinese version?

  2. “Or, stop using an OS at all.”

    I have long wondered why this isn’t much more of a thing. If you’re building some appliance with a Raspbpi inside, why is it set in stone that you have to install some giant Linux distro, and then have that run your application once it’s eventually booted? Why is there no Arduino-like toolchain that builds your code directly into a minimal kernel?

    I feel like the common wisdom is that Linux is free and easy to install, so why not having everything available, even if you only want to blink an LED? But this seems very outdated. Modern distros are huge, and stripping them down is a huge project.

    1. Lots of projects use “bare metal” when it’s appropriate. If you’re wiggling a pin in response to a sensor, it’s super easy. Eventually, someone wants a web interface to wiggle that pin, so now you need a network stack and drivers. And timers, memory allocation, retries, etc. Oh, then someone needs accounts, so you stick a filesystem in there to save them. Two people tried it at the same time and corrupted your terrible filesystem? Now you need record locking and file protection. This user clobbered that user? Now you need memory protection and …

      There are many steps between “nothing” and “UNIX-like” with FreeRTOS, NuttX, eCOS, and such, comfortably living in under 20K and giving you many of the creature comforts above, but without a full-on desktop OS shaved down and trying to fit.

      Unless you have the discipline to stop at step one, you usually end up with some amount of OS underneath you. It might be called “Arduino” or ESP-IDF or whatever.

      If your goal really is to blink a light (or interact with sensors, maybe a network stack, a small LCD, etc.), you’re doing it on a microcontroller (ESP32, STM32, Atmega, etc.) and not a “real” computer where a Linux-like substance is a much more natural fit. There’s a whole world of embedded systems out there between “nothing” and “Fedora.” They aren’t’ starting with an eight-core, GB+ beast of a computer to do it, either.

    2. They aren’t that huge. MicroSD cards are $5 for a Microcenter brand 64 GB, or $9 if you want a SanDisk. Debian installs in 1.5 GB if you don’t need a GUI. Yes, 1.5 GB is outrageous if you think of what was done with MS-DOS on 30MB hard drives, but unless you’re showing off, it’s not worth worrying about.

      If you only want to blink an LED, why use a computer? You can probably do that with a tiny cheap microcontroller. High volume applications may need to squeeze every cent out of the hardware, but if you’re running a Pi, you’ll never notice the computational costs or space of a full Linux system, and in exchange for those tiny costs, you get to write your code in Python or Java, instead of trying to fit it in some kernel-safe subset of C with external libraries being hard to drag in, normal debugging not working, and none of the protection from memory corruption that a kernel provides to its programs.

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.