OS Intro

Intro to the series

I’ll be trying to make an operating system.

Building a cross compiler

Following along with the instructions from OsDev Wiki - GCC Cross Compiler, I created a script to download, verify, configure, build, and install the latest binutils + gcc toolchain.

The script is really simple, and assumes that the build dependancies are installed.

For this series I will be targeting i686-elf.

The First Kernel

My first kernel will do nothing but halt. I can’t print something, or return an exit code, or anything like that because I don’t have any system libraries! I’ll create the simplest possible assembly file that only instructs the cpu to halt.

Follow along with the Git Repository

.globl _start
_start:
	hlt
	jmp _start

Compiling

I want to use the new cross-compiler to produce a binary for the target system. With the cross compiler in path, I run i686-elf-gcc -o first-kernel first-kernel.s -ffreestanding -nostdlib -lgcc. This instructs the compiler to compile the kernel without any standard libraries except for libgcc, which is used by gcc for some operations.

This compiles fine, but I’d never be able to run it. There’s no way for a standard multiboot2 bootloader (like grub) to know where to execute the program. I’ll add a simple mutliboot2 header to the start:

.set ALIGN,		1<<0
.set MEMINFO,	1<<1
.set FLAGS,		ALIGN | MEMINFO
.set MAGIC,		0x1BADB002
.set CHECKSUM,	-(MAGIC + FLAGS)

.section .text
.align 4
.long MAGIC
.long FLAGS
.long CHECKSUM

.globl _start
_start:
	hlt
	jmp _start

And this one also compiles, but with some stuff at the top.

Running

I’ll use qemu since it’s fast and easy - I might use bochs later in the series for it’s debugging features. Normally I’d have to create a grtub rescue image (and iso with grub and the kernel) and run that, but qemu has some shortcuts. I can run qemu-system-i386 -kernel first-kernel and … it does nothing! Perfect, that’s just what I told it to do.

os