Kyle Moore bio photo

Kyle Moore

A recent graduate of Stanford University who enjoys rock climbing, cycling, and writing terrible operating systems.

Email Github Stackoverflow

Overview

 

This is the ​first installment in a series of articles detailing my experience writing a modern toy operating system from scratch. To see a list of articles, go to the overview.

 

History

When I was in college, I took an upper-level class on operating system design in which my partner and I had the opportunity to actually design and implement much of the functionality in a toy operating system called pintos. During the 10-week class, we implemented features such as thread scheduling, virtual memory, user-land program loaders, and a filesystem. While this certainly felt like we were making something from scratch, in reality the course instructors had provided us with a huge amount of code that glossed over many of the ugly necessities of actually writing an operating system. At the end of that class, we had a tiny OS that could boot, run compiled programs, print to the screen, and save to a hard drive, and, while I had learned a lot about the higher level algorithms and decisions you have to make when designing an OS, I realized that there were still huge and fundamental gaps in my knowledge of how computers actually work.

 

For example, when we booted our OS in a virtual machine it started running code in a function called kernel_main() in our compiled kernel. How did it know to start running that code? Do CPUs have some sort of logic embedded in their transistors that tells them to look for a file called kernel and call a function called kernel_main()? Surely they do not, but they must have some logic that tells them to start running code somewhere, so how do we use this?

 

Being the empiricist I am, I decided the only way to really answer these questions was to write my own operating system – as much from scratch as possible. And thus, in July of 2014, MosquitOS was born. There was very little fanfare (and at that point, even less actual code), but I was excited.

 

This Blog

Now, about four months later, MosquitOS is nowhere close to usable for anything. Nothing is special about right now except, I’ve reached a point where I wanted to start documenting my journey with a series of blog posts – that will continue for as long as I keep working on MosquitOS. Primarily, I am writing these to augment my imperfect memory, but hopefully they will be useful to others as well.

 

In my experience with MosquitOS, I found that most of the existing reference/tutorials for people writing their own toy operating systems is about 20 years outdated.​ I wanted MosquitOS to work with 64-bit CPUs and use modern hardware, but unfortunately, while there is copious, great writing on OS design for old hardware, many of the technologies I wanted to use lacked thorough tutorials. These blog posts aim to somewhat be that missing tutorial - for all of you fools out there who wish to write a UEFI-based, modern OS for x86_64. Good luck — it’s a jungle out there.

 

Goals For MosquitOS

From the start, I had some specific goals for my toy OS, these have been refined and added to over time (and will likely change in the future), but some rough goals are (in no real order):

  • 64-bit kernel
  • UEFI bootloader
  • Pre-emptive multitasking
  • Multiprocessor support
  • PCI support
  • USB support
  • Keyboard/mouse support
  • Basic filesystem
  • Virtual memory
  • Bootable on a real computer

At one point I dreamed of hardware-accelerated graphics (by writing a driver for the computer’s GPU), but I’ve since come to decide that this sheer folly. I will console myself with the possibility of integrating some Linux GPU drivers at some point and exposing an OpenGL API or something. Or maybe I’ll chicken out and stick with the basic graphics support provided by the motherboard firmware. We’ll see.

 

To The Bootloader!

I will begin the series with a post about the initial boot process, and explain the differences between UEFI and BIOS. Onward!