OS Dev as a NOOB

CHAITANY RAGHAV
6 min readJun 26, 2021

Hello readers,

This blog aims to show my journey as a complete NOOB learning how Operating Systems function at the most fundamental level.

After exploring OS dev in-depth I deepened my love for low-level coding and computer architecture. My aim is to make some contributions to KSOS and keep the project going on.

Let's first see what operating systems are — Wikipedia for operating systems mentions something about managing hardware, software resources and provide common services for computer programs. But I like to look at it differently; It is just another layer of abstraction in a whole hierarchy of abstraction layers which makes it easier to build even more complex systems (Imagine writing an operating system, or something similar… whenever you want to start a new project). And in this blog, I will try to peel this layer of abstraction apart and see what is going on under the hood.

Having no prior experience with OS Development, the first week was an intense learning experience. The major things that I learned over the past week include:-

  1. The Boot Process
  2. How the X86 processor function.
  3. Programing in Assembly.
  4. Many general concepts related to Operating Systems.
  5. OS dev is pain(😥) and FUN at the same time.

What is KSOS?

KSOS is the brainchild of Suraaj(A third-year CSE undergrad at IITR). His blog goes into the details of how he started this project. KSOS is a very basic operating system for the X86 processor that provides a very basic shell, virtual memory support, and the ability to put ASCII characters on the screen….. very basic in terms of what we think when we hear the term operating system. But even doing this much is pretty complex….Now let us dive deeper.

The BIOS

Bios is a firmware specification(There is also UEFI). The BIOS is stored in non-volatile memory, as soon as the power button on the PSU(Power Supply Unit) is pressed the BIOS takes control of the processor(not exactly, it is always more complicated than that…). The BIOS does various things like setting up the Interrupt Vector Table and providing the Basic Input Output capability. And then looks for a bootable drive(int 0X19).

What is the Interrupt Vector Table?

An Interrupt Vector table is a Data Structure that associates a list of interrupt handlers with a list of interrupt requests in a table of interrupt vectors. In this case, we are particularly referring to the BIOS Interrupt Vector Table. These act somewhat like what one would think of as inbuilt functions in a High-Level Programing Language.

What is a bootable Drive?

It is a drive with the magic word 0Xaa55 at the end of a sector(A 512-byte chunk).

If these Bytes are found, the drive is considered bootable and the bootloader will be loaded at the address 0X7C00.In KSOS these bytes (0Xaa55) are put at the end of Boot Stage-1. The BIOS thus loads the boot stage-1 at the address 0X7C00.

The Bootloader

The bootloader is a piece of code that sets up all the I/O devices, the RAM, and does a bunch of other stuff….and then loads the kernel into memory and passes the control to the kernel…You might ask then what is the need for a bootloader at all…cant the BIOS directly load up the kernel, but there are some limitations of the BIOS specification. The BIOS only loads 512 bytes(a sector) and it is pretty impossible to make a kernel that is only 512 bytes long. The bootloader also gives us the option to run multiple operating systems on the same hardware.

In KSOS the bootloader is made in 2 stages (as 512 bytes are not enough for even the bootloader). The first stage is in essence just there to transfer the control to the second stage where the real work is done. Stage-2 also does the transition from the 16-bit real mode to the 32-bit protected mode.

What are 16-bit real mode and 32-bit protected mode ???

Short Ans: Intel wants backward compatibility till the 8086 processor….so all the X86 systems in essence boot up as 8086.

Now after the bootloader has set everything up we finally call the kernel and start writing most of our code in C(We still need a bit of assembly to handle interrupts and read and write to ports).

The Kernel

The Kernel is the core of the operating system. It is the software that directly interacts with the hardware, all other software interact with the kernel.

The KSOS kernel is a very basic one….after getting the control of the processor it sets up virtual memory(I still need to learn about this, remember me=NOOB), sets up the interrupts, and calls a shell. (All of this happens in ring 0).

What is ring 0 ???

Your hardware has 2 modes of operation-supervisor mode and user mode. In X86 ring 0 is supervisor mode and ring 3 is user mode.

This is done for the protection and isolation of the code running on the system. Some operations like the access to the I/O device's id only available to ring 0 i.e. the kernel. Someone might ask that an application like MS Word is a user application, how does it get the input from the keyboard..; This is done through system calls(KSOS has nothing like this)

The Shell

This is the part of the KSOS that is visible to the end-user, the shell has some commands that can be viewed by the help command(and also some hidden ones 😉). But in totality, it does not do much and needs a lot of work..

On a side note.. Suraaj has also planted some Easter eggs in the source code which can be found out if u go through the source code. The hidden commands can also be found in the same way.

Future of KSOS…..

KSOS needs a lot of work…

1) Moving onto a readymade bootloader like GRUB (Suraj has already done that in a separate project)

2)Addition of a Virtual file system(I might be implementing this)

3)Ability to run user-mode programs and addition of the system calls to the kernel which will enable us to do so.

4)Multithreading(Far future…..).

My Progress So Far

Currently, I am just reading a lot, and there has not really been much that I have coded. I am becoming familiar with the code base and all the concepts that I will need to complete this project successfully.

My Contribution So Far

I found a Bug in the shell code and fixed it.

Details of the Bug: The bug was found in the helper function in the shell string_copy(char*, char*), In one of the cases of copying the string some garbage value could be put at the end of the string. For a detailed overview go to this Git Hub pull request.

Resources

https://eecs.wsu.edu/~cs460/cs560/booter.html

…..And Lot Of Random Websites

Other Similar Projects

--

--