Star···
LeveretOS hare logo

LeveretOS_

LeveretOS, a small educational Operating System built from scratch. New additions are split into separate lessons, with each step documented. Every change includes educational comments, built for beginners. This repository aims to double as a step-by-step guide on how an OS actually works.

Try the latest build

Boot the latest version of LeveretOS in browser! No install required. This runs the same ISO as the latest lesson.

The current build is Lesson 21 (Ethernet and ARP). Click inside the screen and use the shell. Try arp.

Emulated in browser with v86.

The idea

Each lesson will have its own folder, and will add one new feature on top of the last. New or changed code will contain comments to guide you through each step. Note that reused code will be left without educative comments to improve simplicity.

How to follow each lesson

Each lesson folder is a copy of the one before, with the addition of one new feature. It is unnecessary to reread every file each lesson. A few files change per lesson, and you can see them in the following ways:

When studying lessons, the recommended way is: read the overview, open the changed files, look for the new-code comments, and compare the differences with git diff.

Why "LeveretOS"?

A leveret is a baby hare, small, quick, and one of the fastest-growing young animals around. It felt like the right mascot for an OS that started as almost nothing and will grow a little better with every lesson.

A leveret, a young hare, resting in grass

Photo by Tony Stoddard on Unsplash.

Lessons so far

Fundamentals

01Hello WorldBooting via GRUB and writing text to VGA memoryBoot →
02Text OutputA moving cursor for typing, putchar/print, newlines and wrappingBoot →
03Text InputReading the keyboard: scancodes, getchar, read_lineBoot →
04Print NumbersTurning integers into on-screen digitsBoot →
05String helpersCreate helpers for strings and memoryBoot →
06ScrollingShift lines up when text reaches the bottomBoot →
07Rock Paper ScissorsA playable terminal game, built from everything so farBoot →

Talking to the Hardware

08RestructureSplit the kernel into multiple files and headers (same behaviour)Boot →
09GDTBuild our own segment table so the kernel controls its own code and data (same behaviour)Boot →
10Interrupts (IDT)Catch CPU exceptions with our own interrupt tableBoot →
11Keyboard via interruptsThe keyboard now sends key presses, instead of constant pollingBoot →
12ShellA command prompt the user can type into, with the commands help, clear, about, and echoBoot →
13Timer & ClockA timer, and a clock command which shows the real timeBoot →

Managing Memory

14Physical MemoryCreate a physical memory manager, and check RAM usage with a commandBoot →
15PagingTurn on paging, or virtual memory, using page tables instead of physical addressesBoot →
16HeapBuild a memory allocation system, allowing us to give out memory blocks of any sizeBoot →
17ReorganizeSort source files into folders to clean up the projectBoot →

Getting Online

18PCI ScanScan the PCI bus to find external devices, such as the network cardBoot →
19Find Network CardCommunicate with the network card and read its MAC addressBoot →
20Send and ReceiveSend ethernet frames out of the network cardBoot →
21Ethernet and ARPAn Ethernet layer to create headers, and ARP to find a machine's MAC from its IPBoot →

Building and running a lesson

Don't want to download anything? Every lesson can be booted in browser, just press the "Boot" button on any lesson above. If you'd rather build it yourself, you can use the following methods.

Each lesson folder is self-contained and includes a build.sh that assembles the bootloader, compiles the kernel, makes a bootable ISO, and launches it in QEMU. You will need nasm, a 32-bit-capable g++, grub-mkrescue, xorriso, and qemu-system-i386 (on Windows, a Linux environment such as WSL works well).

A good guide for setting up the necessary software to be able to boot: OSDev Bare Bones.

Building with Docker (macOS, Windows, or Linux)

If you don't want to install everything by hand, the repo includes a Dockerfile with the build environment. You only need Docker to build, and QEMU to run.

# From the repo root, build the toolchain container once:
docker build -t leveretos .

# Build a lesson's ISO inside the container (change the folder for other lessons):
docker run --platform linux/amd64 --rm -v "$PWD":/src -w /src/06-Scrolling leveretos ../build-iso.sh

# Run the resulting ISO with QEMU on your own machine:
qemu-system-i386 -cdrom 06-Scrolling/build/LeveretOS-Scrolling.iso

The container is pinned to x86-64, so on Apple Silicon Macs Docker can emulate it (a little slower, but it works). The ISO is written to the lesson's build/ folder, so you run it with QEMU on the host.

Goals

The first big goal is a LeveretOS that can go online and open a web page. These are the checkpoints we have set to reach our goal.

Once we can browse the web, our goals can get bigger. This is where LeveretOS will grow from a single program into something closer to a real operating system.