Designing Your Own OS from Scratch


This year as a joke I decided to try out building Hell’s Kitchen OS. And no I’m not obsessed with Gordon Ramsay, but the OS needed to be a prank that was better than a RickRoll.

As resources for the project, we don’t have a lot of clean data. We need to collect all the bits and pieces in parts. The only legit sources I know of are: ops-class.org the class Zuckerberg struggled in, and Terry O. Davis’s 830 GiB Torrent files, which from what I’ve heard it’s mostly filled with (profanity alert) videos of him fapping and n-words.

Here’s an inspiring quote from the man:

“I wrote a compiler, nigger. I am smarter than Linus. I wrote a kernel and compiler.”

Also, another alert: I have about 30 days to work on this. Like basically spending around a day on full-blown aspects like file management, connections and text editors. It really is just how much can one do when a project title and the power of the internet is given to them. I do think I might work on it as time goes by, but for now just stick the the one month.

Step 1: Deciding what all is even going to be there.

When you decide the theme for your OS, you’re flying in the air thinking it’s all going to be possible, almost too easy. I’ll just change my desktop picture. You’re wrong.

Most DIY-OSs are quite boring and just need to have the basic functionality. If you look at Windows 10, from a Linux perspective, you’ve sort of learnt how to be minimal with the softwares you use on a daily basis. And really, 90% of Windows is just basically bloat. All you really need in an OS is a file management system, a universal text editor that can handle almost any file, maybe a tux paint, something for viewing images and videos and a browser (networking capabilities).

Step 2: Making a Cross Compiler.

This is the guide you want to follow: GCC Cross Compiler.sudo apt install all those dependencies, take a gcc from here and a binutils from here. Extract them to your project directory and just follow the guide.

While working on binutils if you encounter “checking whether the C compiler works… no” run the following commands:

sudo apt-get install --reinstall build-essential
sudo apt-get install --reinstall gcc
sudo dpkg-reconfigure build-essential
sudo dpkg-reconfigure gcc

I think this is something that happens when you try to make install binutils a second time. I guess before working on anything else and after making gcc you might want to actually run it again.

dum dum dum… technical difficulties

After spending a day or more setting up that cross compiler I lost the battle. Then, having only 4 days left for the grand presentation I found myself, well how one finds themselves before deadlines. Well, where do you actually start?

I think reading OSDev right at the beginning could somewhat be a mix of exhausting, and at same time the site manages to be condescending. Don’t take it the wrong way, it’s a great resource but just spirals out of control.

Here’s where you want to begin.

You want to start with falling in love with this ultra-cute language called Assembly. It’s cute because every fucking line of code is just three words at max and so much fun dealing with the little loops and labels. This is the playlist I followed for studying for assembly language, it’s in Urdu right but maybe you can manage to look at his notes in English. The first few videos are boring but then it catches pace when starts up his MASM, which by the way is super easy to setup for Ubuntu. After you’re answering the programming questions before that guy solves it in his videos, I think it’s good to begin.

When dealing with the bootloader, all we can use is Assembly and not even just diassembly of C code. We actually start off with only 16 bits in real mode. That means registers like eax (extended registers) are outside our scope. So let’s you’re lazy and would rather disassemble stuff like this simple C program:

// filename: basic.c
int my_function () {
	return 0xbaba;
}

Good news: You CAN’T!

The answer lies in the disassembly. To disamble this:

gcc -ffreestanding -c basic.c -o basic.o
objdump -d basic.o

And you end up with an output like this:

basic.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <my_function>:
   0:	f3 0f 1e fa          	endbr64 
   4:	55                   	push   %rbp
   5:	48 89 e5             	mov    %rsp,%rbp
   8:	b8 ba ba 00 00       	mov    $0xbaba,%eax
   d:	5d                   	pop    %rbp
   e:	c3                   	ret    

Look at that eax, occupying all those 32 bits of memory just waiting to crash our OS. So in this bootloading stage you can only use assembly till you finally extend from the 16 bit real mode to the 32 bit protected mode and finally the 64 bit long mode.

Now to get started on understanding the basics follow this pdf.

draft: Beginner’s Mistakes OS Dev

That sentence that goes before giving my email to strangers: psymbio@gmail.com