The little book about OS development

Categories:

Recommended

Chapter 2 – First Steps

Developing an operating system (OS) is no easy task, and the question “How do I even begin to solve this problem?” is likely to come up several times during the course of the project for different problems. This chapter will help you set up your development environment and booting a very small (and primitive) operating system.

Tools

Quick Setup

We (the authors) have used Ubuntu  as the operating system for doing OS development, running it both physically and virtually (using the virtual machine VirtualBox ). A quick way to get everything up and running is to use the same setup as we did, since we know that these tools work with the samples provided in this book. Once Ubuntu is installed, either physical or virtual, the following packages should be installed using apt-get:

sudo apt-get install build-essential nasm genisoimage bochs bochs-sdl

Programming Languages

The operating system will be developed using the C programming language, using GCC . We use C because developing an OS requires a very precise control of the generated code and direct access to memory. Other languages that provide the same features can also be used, but this book will only cover C. The code will make use of one type attribute that is specific for GCC:

__attribute__((packed))

This attribute allows us to ensure that the compiler uses a memory layout for a struct exactly as we define it in the code. This is explained in more detail in the next chapter. Due to this attribute, the example code might be hard to compile using a C compiler other than GCC. For writing assembly code, we have chosen NASM  as the assembler, since we prefer NASM’s syntax over GNU Assembler. Bash  will be used as the scripting language throughout the book.

Host Operating System

All the code examples assumes that the code is being compiled on a UNIX like operating system. All code examples have been successfully compiled using Ubuntu  versions 11.04 and 11.10.

Build System

Make [13] has been used when constructing the Makefile examples.

Virtual Machine

When developing an OS it is very convenient to be able to run your code in a virtual machine instead of on a physical computer, since starting your OS in a virtual machine is much faster than getting your OS onto a physical medium and then running it on a physical machine. Bochs  is an emulator for the x86 (IA-32) platform which is well suited for OS development due to its debugging features. Other popular choices are QEMU  and VirtualBox . This book uses Bochs. By using a virtual machine we cannot ensure that our OS works on real, physical hardware. The environment simulated by the virtual machine is designed to be very similar to their physical counterparts, and the OS can be tested on one by just copying the executable to a CD and finding a suitable machine.

Booting

Booting an operating system consists of transferring control along a chain of small programs, each one more “powerful” than the previous one, where the operating system is the last “program”. See the following figure for an example of the boot process:

Figure 2.1: An example of the boot process. Each box is a program.

BIOS

When the PC is turned on, the computer will start a small program that adheres to the Basic Input Output System (BIOS)  standard. This program is usually stored on a read only memory chip on the motherboard of the PC. The original role of the BIOS program was to export some library functions for printing to the screen, reading keyboard input etc. Modern operating systems do not use the BIOS’ functions, they use drivers that interact directly with the hardware, bypassing the BIOS. Today, BIOS mainly runs some early diagnostics (power-on-self-test) and then transfers control to the bootloader.

The Bootloader

The BIOS program will transfer control of the PC to a program called a bootloader. The bootloader’s task is to transfer control to us, the operating system developers, and our code. However, due to some restrictions1 of the hardware and because of backward compatibility, the bootloader is often split into two parts: the first part of the bootloader will transfer control to the second part, which finally gives control of the PC to the operating system. Writing a bootloader involves writing a lot of low-level code that interacts with the BIOS. Therefore, an existing bootloader will be used: the GNU GRand Unified Bootloader (GRUB) . Using GRUB, the operating system can be built as an ordinary ELF  executable, which will be loaded by GRUB into the correct memory location. The compilation of the kernel requires that the code is laid out in memory in a specific way (how to compile the kernel will be discussed later in this chapter).

The Operating System

GRUB will transfer control to the operating system by jumping to a position in memory. Before the jump, GRUB will look for a magic number to ensure that it is actually jumping to an OS and not some random code. This magic number is part of the multiboot specification  which GRUB adheres to. Once GRUB has made the jump, the OS has full control of the computer.

Attribution

Erik Helin, Adam Renberg (2015), The little book about OS development, URL: https://littleosbook.github.io/

This work is licensed under Attribution-NonCommercial-ShareAlike 3.0 United States (CC BY-NC-SA 3.0 US):  (https://creativecommons.org/licenses/by-nc-sa/3.0/us/).

VP Flipbook Maker

Unlock your creative potential with Visual Paradigm Online Flipbook Maker! Transform your content into engaging flipbooks with ease and customize them using intuitive design tools. Experience it now and share your work in an interesting way!