The Little Book About OS Development PDF
The Little Book About OS Development PDF
Contents
1 Introduction
1.1 About the Book
1.2 The Reader
1.3 Credits, Thanks and Acknowledgements
1.4 Contributors
1.5 Changes and Corrections
1.6 Issues and where to get help
1.7 License
2 First Steps
2.1 Tools
2.1.1 Quick Setup
2.1.2 Programming Languages
2.1.3 Host Operating System
2.1.4 Build System
2.1.5 Virtual Machine
2.2 Booting
2.2.1 BIOS
2.2.2 The Bootloader
2.2.3 The Operating System
2.3 Hello Cafebabe
2.3.1 Compiling the Operating System
2.3.2 Linking the Kernel
2.3.3 Obtaining GRUB
2.3.4 Building an ISO Image
2.3.5 Running Bochs
2.4 Further Reading
3 Getting to C
3.1 Setting Up a Stack
3.2 Calling C Code From Assembly
3.2.1 Packing Structs
3.3 Compiling C Code
3.4 Build Tools
3.5 Further Reading
4 Output
4.1 Interacting with the Hardware
4.2 The Framebuffer
4.2.1 Writing Text
4.2.2 Moving the Cursor
4.2.3 The Driver
4.3 The Serial Ports
4.3.1 Configuring the Serial Port
4.3.2 Configuring the Line
4.3.3 Configuring the Buffers
4.3.4 Configuring the Modem
4.3.5 Writing Data to the Serial Port
4.3.6 Configuring Bochs
4.3.7 The Driver
https://littleosbook.github.io/#first-steps 1/47
5/18/2019 The little book about OS development
https://littleosbook.github.io/#first-steps 2/47
5/18/2019 The little book about OS development
14 Multitasking
14.1 Creating New Processes
14.2 Cooperative Scheduling with Yielding
14.3 Preemptive Scheduling with Interrupts
14.3.1 Programmable Interval Timer
14.3.2 Separate Kernel Stacks for Processes
14.3.3 Difficulties with Preemptive Scheduling
14.4 Further Reading
1 Introduction
This text is a practical guide to writing your own x86 operating system. It is designed to give enough
help with the technical details while at the same time not reveal too much with samples and code
excerpts. We’ve tried to collect parts of the vast (and often excellent) expanse of material and tutorials
available, on the web and otherwise, and add our own insights into the problems we encountered and
struggled with.
This book is not about the theory behind operating systems, or how any specific operating system (OS)
works. For OS theory we recommend the book Modern Operating Systems by Andrew Tanenbaum [1].
Lists and details on current operating systems are available on the Internet.
The starting chapters are quite detailed and explicit, to quickly get you into coding. Later chapters give
more of an outline of what is needed, as more and more of the implementation and design becomes up
to the reader, who should now be more familiar with the world of kernel development. At the end of
some chapters there are links for further reading, which might be interesting and give a deeper
understanding of the topics covered.
In chapter 2 and 3 we set up our development environment and boot up our OS kernel in a virtual
machine, eventually starting to write code in C. We continue in chapter 4 with writing to the screen and
the serial port, and then we dive into segmentation in chapter 5 and interrupts and input in chapter 6.
After this we have a quite functional but bare-bones OS kernel. In chapter 7 we start the road to user
mode applications, with virtual memory through paging (chapter 8 and 9), memory allocation (chapter
10), and finally running a user application in chapter 11.
In the last three chapters we discuss the more advanced topics of file systems (chapter 12), system
calls (chapter 13), and multitasking (chapter 14).
The x86 architecture is, and has been for a long time, one of the most common hardware architectures.
It was not a difficult choice to use the x86 architecture as the target of the OS, with its large community,
extensive reference material and mature emulators. The documentation and information surrounding
the details of the hardware we had to work with was not always easy to find or understand, despite (or
perhaps due to) the age of the architecture.
The OS was developed in about six weeks of full-time work. The implementation was done in many
small steps, and after each step the OS was tested manually. By developing in this incremental and
iterative way, it was often easier to find any bugs that were introduced, since only a small part of the
https://littleosbook.github.io/#first-steps 3/47
5/18/2019 The little book about OS development
code had changed since the last known good state of the code. We encourage the reader to work in a
similar way.
During the six weeks of development, almost every single line of code was written by the authors
together (this way of working is also called pair-programming). It is our belief that we managed to avoid
a lot of bugs due to this style of development, but this is hard to prove scientifically.
Most of the CSS formatting of the book is based on the work by Scott Chacon for the book Pro Git,
http://progit.org/.
1.4 Contributors
We are very grateful for the patches that people send us. The following users have all contributed to this
book:
alexschneider
Avidanborisov
nirs
kedarmhaswade
vamanea
ansjob
1.7 License
All content is under the Creative Commons Attribution Non Commercial Share Alike 3.0 license,
http://creativecommons.org/licenses/by-nc-sa/3.0/us/. The code samples are in the public domain - use
them however you want. References to this book are always received with warmth.
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.
https://littleosbook.github.io/#first-steps 4/47
5/18/2019 The little book about OS development
This chapter will help you set up your development environment and booting a very small (and primitive)
operating system.
2.1 Tools
2.1.1 Quick Setup
We (the authors) have used Ubuntu [6] as the operating system for doing OS development, running it
both physically and virtually (using the virtual machine VirtualBox [7]). 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:
The operating system will be developed using the C programming language [8][9], using GCC [10]. 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 [11] as the assembler, since we prefer NASM’s
syntax over GNU Assembler.
Bash [12] will be used as the scripting language throughout the book.
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 [6] versions 11.04 and 11.10.
Make [13] has been used when constructing the Makefile examples.
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 [14] 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 [15] and VirtualBox [7]. This book uses Bochs.
https://littleosbook.github.io/#first-steps 5/47
5/18/2019 The little book about OS development
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.
2.2 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:
2.2.1 BIOS
When the PC is turned on, the computer will start a small program that adheres to the Basic Input
Output System (BIOS) [16] 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 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) [17].
Using GRUB, the operating system can be built as an ordinary ELF [18] 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).
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 [19] which GRUB adheres to. Once
GRUB has made the jump, the OS has full control of the computer.
https://littleosbook.github.io/#first-steps 6/47
5/18/2019 The little book about OS development
This section will describe how to implement of the smallest possible OS that can be used together with
GRUB. The only thing the OS will do is write 0xCAFEBABE to the eax register (most people would probably
not even call this an OS).
This part of the OS has to be written in assembly code, since C requires a stack, which isn’t available
(the chapter “Getting to C” describes how to set one up). Save the following code in a file called
loader.s:
The only thing this OS will do is write the very specific number 0xCAFEBABE to the eax register. It is very
unlikely that the number 0xCAFEBABE would be in the eax register if the OS did not put it there.
The file loader.s can be compiled into a 32 bits ELF [18] object file with the following command:
The code must now be linked to produce an executable file, which requires some extra thought
compared to when linking most programs. We want GRUB to load the kernel at a memory address
larger than or equal to 0x00100000 (1 megabyte (MB)), because addresses lower than 1 MB are used by
GRUB itself, BIOS and memory-mapped I/O. Therefore, the following linker script is needed (written for
GNU LD [20]):
SECTIONS {
. = 0x00100000; /* the code should be loaded at 1 MB */
Save the linker script into a file called link.ld. The executable can now be linked with the following
command:
The GRUB version we will use is GRUB Legacy, since the OS ISO image can then be generated on
systems using both GRUB Legacy and GRUB 2. More specifically, the GRUB Legacy stage2_eltorito
bootloader will be used. This file can be built from GRUB 0.97 by downloading the source from
ftp://alpha.gnu.org/gnu/grub/grub-0.97.tar.gz. However, the configure script doesn’t work well with
Ubuntu [21], so the binary file can be downloaded from
http://littleosbook.github.com/files/stage2_eltorito. Copy the file stage2_eltorito to the folder that already
contains loader.s and link.ld.
The executable must be placed on a media that can be loaded by a virtual or physical machine. In this
book we will use ISO [22] image files as the media, but one can also use floppy images, depending on
what the virtual or physical machine supports.
We will create the kernel ISO image with the program genisoimage. A folder must first be created that
contains the files that will be on the ISO image. The following commands create the folder and copy the
files to their correct places:
A configuration file menu.lst for GRUB must be created. This file tells GRUB where the kernel is located
and configures some options:
default=0
timeout=0
title os
kernel /boot/kernel.elf
Place the file menu.lst in the folder iso/boot/grub/. The contents of the iso folder should now look like
the following figure:
iso
|-- boot
|-- grub
| |-- menu.lst
| |-- stage2_eltorito
|-- kernel.elf
The ISO image can then be generated with the following command:
genisoimage -R \
-b boot/grub/stage2_eltorito \
-no-emul-boot \
-boot-load-size 4 \
-A os \
https://littleosbook.github.io/#first-steps 8/47
5/18/2019 The little book about OS development
-input-charset utf8 \
-quiet \
-boot-info-table \
-o os.iso \
iso
For more information about the flags used in the command, see the manual for genisoimage.
The ISO image os.iso now contains the kernel executable, the GRUB bootloader and the configuration
file.
Now we can run the OS in the Bochs emulator using the os.iso ISO image. Bochs needs a configuration
file to start and an example of a simple configuration file is given below:
megs: 32
display_library: sdl
romimage: file=/usr/share/bochs/BIOS-bochs-latest
vgaromimage: file=/usr/share/bochs/VGABIOS-lgpl-latest
ata0-master: type=cdrom, path=os.iso, status=inserted
boot: cdrom
log: bochslog.txt
clock: sync=realtime, time0=local
cpu: count=1, ips=1000000
You might need to change the path to romimage and vgaromimage depending on how you installed Bochs.
More information about the Bochs config file can be found at Boch’s website [23].
If you saved the configuration in a file named bochsrc.txt then you can run Bochs with the following
command:
bochs -f bochsrc.txt -q
The flag -f tells Bochs to use the given configuration file and the flag -q tells Bochs to skip the
interactive start menu. You should now see Bochs starting and displaying a console with some
information from GRUB on it.
cat bochslog.txt
You should now see the contents of the registers of the CPU simulated by Bochs somewhere in the
output. If you find RAX=00000000CAFEBABE or EAX=CAFEBABE (depending on if you are running Bochs with or
without 64 bit support) in the output then your OS has successfully booted!
3 Getting to C
This chapter will show you how to use C instead of assembly code as the programming language for
the OS. Assembly is very good for interacting with the CPU and enables maximum control over every
https://littleosbook.github.io/#first-steps 9/47
5/18/2019 The little book about OS development
aspect of the code. However, at least for the authors, C is a much more convenient language to use.
Therefore, we would like to use C as much as possible and use assembly code only where it make
sense.
We could point esp to a random area in memory since, so far, the only thing in the memory is GRUB,
BIOS, the OS kernel and some memory-mapped I/O. This is not a good idea - we don’t know how much
memory is available or if the area esp would point to is used by something else. A better idea is to
reserve a piece of uninitialized memory in the bss section in the ELF file of the kernel. It is better to use
the bss section instead of the data section to reduce the size of the OS executable. Since GRUB
understands ELF, GRUB will allocate any memory reserved in the bss section when loading the OS.
The NASM pseudo-instruction resb [24] can be used to declare uninitialized data:
section .bss
align 4 ; align at 4 bytes
kernel_stack: ; label points to beginning of memory
resb KERNEL_STACK_SIZE ; reserve stack for the kernel
There is no need to worry about the use of uninitialized memory for the stack, since it is not possible to
read a stack location that has not been written (without manual pointer fiddling). A (correct) program can
not pop an element from the stack without having pushed an element onto the stack first. Therefore, the
memory locations of the stack will always be written to before they are being read.
The stack pointer is then set up by pointing esp to the end of the kernel_stack memory:
/* The C function */
int sum_of_three(int arg1, int arg2, int arg3)
{
return arg1 + arg2 + arg3;
}
https://littleosbook.github.io/#first-steps 10/47
5/18/2019 The little book about OS development
In the rest of this book, you will often come across “configuration bytes” that are a collection of bits in a
very specific order. Below follows an example with 32 bits:
Bit: | 31 24 | 23 8 | 7 0 |
Content: | index | address | config |
Instead of using an unsigned integer, unsigned int, for handling such configurations, it is much more
convenient to use “packed structures”:
struct example {
unsigned char config; /* bit 0 - 7 */
unsigned short address; /* bit 8 - 23 */
unsigned char index; /* bit 24 - 31 */
};
When using the struct in the previous example there is no guarantee that the size of the struct will be
exactly 32 bits - the compiler can add some padding between elements for various reasons, for
example to speed up element access or due to requirements set by the hardware and/or compiler.
When using a struct to represent configuration bytes, it is very important that the compiler does not add
any padding, because the struct will eventually be treated as a 32 bit unsigned integer by the hardware.
The attribute packed can be used to force GCC to not add any padding:
struct example {
unsigned char config; /* bit 0 - 7 */
unsigned short address; /* bit 8 - 23 */
unsigned char index; /* bit 24 - 31 */
} __attribute__((packed));
Note that __attribute__((packed)) is not part of the C standard - it might not work with all C compilers.
As always when writing C programs we recommend turning on all warnings and treat warnings as
errors:
You can now create a function kmain in a file called kmain.c that you call from loader.s. At this point, kmain
probably won’t need any arguments (but in later chapters it will).
https://littleosbook.github.io/#first-steps 11/47
5/18/2019 The little book about OS development
all: kernel.elf
kernel.elf: $(OBJECTS)
ld $(LDFLAGS) $(OBJECTS) -o kernel.elf
os.iso: kernel.elf
cp kernel.elf iso/boot/kernel.elf
genisoimage -R \
-b boot/grub/stage2_eltorito \
-no-emul-boot \
-boot-load-size 4 \
-A os \
-input-charset utf8 \
-quiet \
-boot-info-table \
-o os.iso \
iso
run: os.iso
bochs -f bochsrc.txt -q
%.o: %.c
$(CC) $(CFLAGS) $< -o $@
%.o: %.s
$(AS) $(ASFLAGS) $< -o $@
clean:
rm -rf *.o kernel.elf os.iso
The contents of your working directory should now look like the following figure:
.
|-- bochsrc.txt
|-- iso
| |-- boot
| |-- grub
| |-- menu.lst
| |-- stage2_eltorito
|-- kmain.c
|-- loader.s
|-- Makefile
You should now be able to start the OS with the simple command make run, which will compile the kernel
and boot it up in Bochs (as defined in the Makefile above).
4 Output
This chapter will present how to display text on the console as well as writing data to the serial port.
Furthermore, we will create our first driver, that is, code that acts as a layer between the kernel and the
hardware, providing a higher abstraction than communicating directly with the hardware. The first part of
this chapter is about creating a driver for the framebuffer [26] to be able to display text on the console.
The second part shows how to create a driver for the serial port. Bochs can store output from the serial
port in a file, effectively creating a logging mechanism for the operating system.
There are usually two different ways to interact with the hardware, memory-mapped I/O and I/O ports.
If the hardware uses memory-mapped I/O then you can write to a specific memory address and the
hardware will be updated with the new data. One example of this is the framebuffer, which will be
discussed in more detail later. For example, if you write the value 0x410F to address 0x000B8000, you will
see the letter A in white color on a black background (see the section on the framebuffer for more
details).
If the hardware uses I/O ports then the assembly code instructions out and in must be used to
communicate with the hardware. The instruction out takes two parameters: the address of the I/O port
and the data to send. The instruction in takes a single parameter, the address of the I/O port, and
returns data from the hardware. One can think of I/O ports as communicating with hardware the same
way as you communicate with a server using sockets. The cursor (the blinking rectangle) of the
framebuffer is one example of hardware controlled via I/O ports on a PC.
Writing text to the console via the framebuffer is done with memory-mapped I/O. The starting address of
the memory-mapped I/O for the framebuffer is 0x000B8000 [27]. The memory is divided into 16 bit cells,
where the 16 bits determine both the character, the foreground color and the background color. The
highest eight bits is the ASCII [28] value of the character, bit 7 - 4 the background and bit 3 - 0 the
foreground, as can be seen in the following figure:
Bit: | 15 14 13 12 11 10 9 8 | 7 6 5 4 | 3 2 1 0 |
Content: | ASCII | FG | BG |
The first cell corresponds to row zero, column zero on the console. Using an ASCII table, one can see
that A corresponds to 65 or 0x41. Therefore, to write the character A with a green foreground (2) and
dark grey background (8) at place (0,0), the following assembly code instruction is used:
The second cell then corresponds to row zero, column one and its address is therefore:
0x000B8000 + 16 = 0x000B8010
Writing to the framebuffer can also be done in C by treating the address 0x000B8000 as a char pointer,
char *fb = (char *) 0x000B8000. Then, writing A at place (0,0) with green foreground and dark grey
https://littleosbook.github.io/#first-steps 13/47
5/18/2019 The little book about OS development
background becomes:
fb[0] = 'A';
fb[1] = 0x28;
The following code shows how this can be wrapped into a function:
/** fb_write_cell:
* Writes a character with the given foreground and background to position i
* in the framebuffer.
*
* @param i The location in the framebuffer
* @param c The character
* @param fg The foreground color
* @param bg The background color
*/
void fb_write_cell(unsigned int i, char c, unsigned char fg, unsigned char bg)
{
fb[i] = c;
fb[i + 1] = ((fg & 0x0F) << 4) | (bg & 0x0F)
}
#define FB_GREEN 2
#define FB_DARK_GREY 8
Moving the cursor of the framebuffer is done via two different I/O ports. The cursor’s position is
determined with a 16 bits integer: 0 means row zero, column zero; 1 means row zero, column one; 80
means row one, column zero and so on. Since the position is 16 bits large, and the out assembly code
instruction argument is 8 bits, the position must be sent in two turns, first 8 bits then the next 8 bits. The
framebuffer has two I/O ports, one for accepting the data, and one for describing the data being
received. Port 0x3D4 [29] is the port that describes the data and port 0x3D5 [29] is for the data itself.
To set the cursor at row one, column zero (position 80 = 0x0050), one would use the following assembly
code instructions:
out 0x3D4, 14 ; 14 tells the framebuffer to expect the highest 8 bits of the position
out 0x3D5, 0x00 ; sending the highest 8 bits of 0x0050
out 0x3D4, 15 ; 15 tells the framebuffer to expect the lowest 8 bits of the position
out 0x3D5, 0x50 ; sending the lowest 8 bits of 0x0050
The out assembly code instruction can’t be executed directly in C. Therefore it is a good idea to wrap
out in a function in assembly code which can be accessed from C via the cdecl calling standard [25]:
global outb ; make the label outb visible outside this file
By storing this function in a file called io.s and also creating a header io.h, the out assembly code
instruction can be conveniently accessed from C:
https://littleosbook.github.io/#first-steps 14/47
5/18/2019 The little book about OS development
#ifndef INCLUDE_IO_H
#define INCLUDE_IO_H
/** outb:
* Sends the given data to the given I/O port. Defined in io.s
*
* @param port The I/O port to send the data to
* @param data The data to send to the I/O port
*/
void outb(unsigned short port, unsigned char data);
#endif /* INCLUDE_IO_H */
#include "io.h"
/** fb_move_cursor:
* Moves the cursor of the framebuffer to the given position
*
* @param pos The new position of the cursor
*/
void fb_move_cursor(unsigned short pos)
{
outb(FB_COMMAND_PORT, FB_HIGH_BYTE_COMMAND);
outb(FB_DATA_PORT, ((pos >> 8) & 0x00FF));
outb(FB_COMMAND_PORT, FB_LOW_BYTE_COMMAND);
outb(FB_DATA_PORT, pos & 0x00FF);
}
The driver should provide an interface that the rest of the code in the OS will use for interacting with the
framebuffer. There is no right or wrong in what functionality the interface should provide, but a
suggestion is to have a write function with the following declaration:
The write function writes the contents of the buffer buf of length len to the screen. The write function
should automatically advance the cursor after a character has been written and scroll the screen if
necessary.
The first data that need to be sent to the serial port is configuration data. In order for two hardware
devices to be able to talk to each other they must agree upon a couple of things. These things include:
https://littleosbook.github.io/#first-steps 15/47
5/18/2019 The little book about OS development
Configuring the line means to configure how data is being sent over the line. The serial port has an I/O
port, the line command port, that is used for configuration.
First the speed for sending data will be set. The serial port has an internal clock that runs at 115200 Hz.
Setting the speed means sending a divisor to the serial port, for example sending 2 results in a speed of
115200 / 2 = 57600 Hz.
The divisor is a 16 bit number but we can only send 8 bits at a time. We must therefore send an
instruction telling the serial port to first expect the highest 8 bits, then the lowest 8 bits. This is done by
sending 0x80 to the line command port. An example is shown below:
/* All the I/O ports are calculated relative to the data port. This is because
* all serial ports (COM1, COM2, COM3, COM4) have their ports in the same
* order, but they start at different values.
*/
/* SERIAL_LINE_ENABLE_DLAB:
* Tells the serial port to expect first the highest 8 bits on the data port,
* then the lowest 8 bits will follow
*/
#define SERIAL_LINE_ENABLE_DLAB 0x80
/** serial_configure_baud_rate:
* Sets the speed of the data being sent. The default speed of a serial
* port is 115200 bits/s. The argument is a divisor of that number, hence
* the resulting speed becomes (115200 / divisor) bits/s.
*
* @param com The COM port to configure
* @param divisor The divisor
*/
void serial_configure_baud_rate(unsigned short com, unsigned short divisor)
{
outb(SERIAL_LINE_COMMAND_PORT(com),
SERIAL_LINE_ENABLE_DLAB);
outb(SERIAL_DATA_PORT(com),
(divisor >> 8) & 0x00FF);
outb(SERIAL_DATA_PORT(com),
divisor & 0x00FF);
}
The way that data should be sent must be configured. This is also done via the line command port by
sending a byte. The layout of the 8 bits looks like the following:
Bit: | 7 | 6 | 5 4 3 | 2 | 1 0 |
Content: | d | b | prty | s | dl |
A description for each name can be found in the table below (and in [31]):
Name Description
https://littleosbook.github.io/#first-steps 16/47
5/18/2019 The little book about OS development
Name Description
We will use the mostly standard value 0x03 [31], meaning a length of 8 bits, no parity bit, one stop bit
and break control disabled. This is sent to the line command port, as seen in the following example:
/** serial_configure_line:
* Configures the line of the given serial port. The port is set to have a
* data length of 8 bits, no parity bits, one stop bit and break control
* disabled.
*
* @param com The serial port to configure
*/
void serial_configure_line(unsigned short com)
{
/* Bit: | 7 | 6 | 5 4 3 | 2 | 1 0 |
* Content: | d | b | prty | s | dl |
* Value: | 0 | 0 | 0 0 0 | 0 | 1 1 | = 0x03
*/
outb(SERIAL_LINE_COMMAND_PORT(com), 0x03);
}
The article on OSDev [31] has a more in-depth explanation of the values.
When data is transmitted via the serial port it is placed in buffers, both when receiving and sending
data. This way, if you send data to the serial port faster than it can send it over the wire, it will be
buffered. However, if you send too much data too fast the buffer will be full and data will be lost. In other
words, the buffers are FIFO queues. The FIFO queue configuration byte looks like the following figure:
Bit: | 7 6 | 5 | 4 | 3 | 2 | 1 | 0 |
Content: | lvl | bs | r | dma | clt | clr | e |
Name Description
Enables FIFO
Clear both receiver and transmission FIFO queues
Use 14 bytes as size of queue
https://littleosbook.github.io/#first-steps 17/47
5/18/2019 The little book about OS development
The WikiBook on serial programming [32] explains the values in more depth.
The modem control register is used for very simple hardware flow control via the Ready To Transmit
(RTS) and Data Terminal Ready (DTR) pins. When configuring the serial port we want RTS and DTR to
be 1, which means that we are ready to send data.
Bit: | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
Content: | r | r | af | lb | ao2 | ao1 | rts | dtr |
Name Description
r Reserved
We don’t need to enable interrupts, because we won’t handle any received data. Therefore we use the
configuration value 0x03 = 00000011 (RTS = 1 and DTS = 1).
Writing data to the serial port is done via the data I/O port. However, before writing, the transmit FIFO
queue has to be empty (all previous writes must have finished). The transmit FIFO queue is empty if bit
5 of the line status I/O port is equal to one.
Reading the contents of an I/O port is done via the in assembly code instruction. There is no way to use
the in assembly code instruction from C, therefore it has to be wrapped (the same way as the out
assembly code instruction):
global inb
/* in file io.h */
/** inb:
* Read a byte from an I/O port.
*
* @param port The address of the I/O port
* @return The read byte
*/
unsigned char inb(unsigned short port);
https://littleosbook.github.io/#first-steps 18/47
5/18/2019 The little book about OS development
#include "io.h"
/** serial_is_transmit_fifo_empty:
* Checks whether the transmit FIFO queue is empty or not for the given COM
* port.
*
* @param com The COM port
* @return 0 if the transmit FIFO queue is not empty
* 1 if the transmit FIFO queue is empty
*/
int serial_is_transmit_fifo_empty(unsigned int com)
{
/* 0x20 = 0010 0000 */
return inb(SERIAL_LINE_STATUS_PORT(com)) & 0x20;
}
Writing to a serial port means spinning as long as the transmit FIFO queue isn’t empty, and then writing
the data to the data I/O port.
To save the output from the first serial serial port the Bochs configuration file bochsrc.txt must be
updated. The com1 configuration instructs Bochs how to handle first serial port:
The output from serial port one will now be stored in the file com1.out.
We recommend that you implement a write function for the serial port similar to the write function in the
driver for the framebuffer. To avoid name clashes with the write function for the framebuffer it is a good
idea to name the functions fb_write and serial_write to distinguish them.
We further recommend that you try to write a printf-like function, see section 7.3 in [8]. The printf
function could take an additional argument to decide to which device to write the output (framebuffer or
serial).
A final recommendation is that you create some way of distinguishing the severeness of the log
messages, for example by prepending the messages with DEBUG, INFO or ERROR.
5 Segmentation
Segmentation in x86 means accessing the memory through segments. Segments are portions of the
address space, possibly overlapping, specified by a base address and a limit. To address a byte in
segmented memory you use a 48-bit logical address: 16 bits that specifies the segment and 32-bits that
specifies what offset within that segment you want. The offset is added to the base address of the
segment, and the resulting linear address is checked against the segment’s limit - see the figure below.
If everything works out fine (including access-rights checks ignored for now) the result is a linear
address. When paging is disabled, then the linear address space is mapped 1:1 onto the physical
https://littleosbook.github.io/#first-steps 19/47
5/18/2019 The little book about OS development
address space, and the physical memory can be accessed. (See the chapter “Paging” for how to enable
paging.)
To enable segmentation you need to set up a table that describes each segment - a segment descriptor
table. In x86, there are two types of descriptor tables: the Global Descriptor Table (GDT) and Local
Descriptor Tables (LDT). An LDT is set up and managed by user-space processes, and all processes
have their own LDT. LDTs can be used if a more complex segmentation model is desired - we won’t use
it. The GDT is shared by everyone - it’s global.
As we discuss in the sections on virtual memory and paging, segmentation is rarely used more than in a
minimal setup, similar to what we do below.
func:
mov eax, [esp+4]
mov ebx, [eax]
add ebx, 8
mov [eax], ebx
ret
https://littleosbook.github.io/#first-steps 20/47
5/18/2019 The little book about OS development
The above example can be compared with the following one that makes explicit use of the segment
registers:
func:
mov eax, [ss:esp+4]
mov ebx, [ds:eax]
add ebx, 8
mov [ds:eax], ebx
ret
You don’t need to use ss for storing the stack segment selector, or ds for the data segment selector. You
could store the stack segment selector in ds and vice versa. However, in order to use the implicit style
shown above, you must store the segment selectors in their indented registers.
Segment descriptors and their fields are described in figure 3-8 in the Intel manual [33].
Table 3-1 in chapter 3 of the Intel manual [33] specifies the values for the Type field. The table shows
that the Type field can’t be both writable and executable at the same time. Therefore, two segments are
needed: one segment for executing code to put in cs (Type is Execute-only or Execute-Read) and one
segment for reading and writing data (Type is Read/Write) to put in the other segment registers.
The DPL specifies the privilege levels required to use the segment. x86 allows for four privilege levels
(PL), 0 to 3, where PL0 is the most privileged. In most operating systems (eg. Linux and Windows), only
PL0 and PL3 are used. However, some operating system, such as MINIX, make use of all levels. The
kernel should be able to do anything, therefore it uses segments with DPL set to 0 (also called kernel
mode). The current privilege level (CPL) is determined by the segment selector in cs.
Note that the segments overlap - they both encompass the entire linear address space. In our minimal
setup we’ll only use segmentation to get privilege levels. See the Intel manual [33], chapter 3, for details
on the other descriptor fields.
struct gdt {
unsigned int address;
https://littleosbook.github.io/#first-steps 21/47
5/18/2019 The little book about OS development
unsigned short size;
} __attribute__((packed));
If the content of the eax register is the address to such a struct, then the GDT can be loaded with the
assembly code shown below:
lgdt [eax]
It might be easier if you make this instruction available from C, the same way as was done with the
assembly code instructions in and out.
After the GDT has been loaded the segment registers needs to be loaded with their corresponding
segment selectors. The content of a segment selector is described in the figure and table below:
Bit: | 15 3 | 2 | 1 0 |
Content: | offset (index) | ti | rpl |
Name Description
ti Table Indicator. 0 means that this specifies a GDT segment, 1 means an LDT Segment.
The offset of the segment selector is added to the start of the GDT to get the address of the segment
descriptor: 0x08 for the first descriptor and 0x10 for the second, since each descriptor is 8 bytes. The
Requested Privilege Level (RPL) should be 0 since the kernel of the OS should execute in privilege
level 0.
Loading the segment selector registers is easy for the data registers - just copy the correct offsets to the
registers:
flush_cs:
; now we've changed cs to 0x08
A far jump is a jump where we explicitly specify the full 48-bit logical address: the segment selector to
use and the absolute address to jump to. It will first set cs to 0x08 and then jump to flush_cs using its
absolute address.
Task handler
Interrupt handler
Trap handler
The task handlers use functionality specific to the Intel version of x86, so they won’t be covered here
(see the Intel manual [33], chapter 6, for more info). The only difference between an interrupt handler
and a trap handler is that the interrupt handler disables interrupts, which means you cannot get an
interrupt while at the same time handling an interrupt. In this book, we will use trap handlers and disable
interrupts manually when we need to.
Bit: | 31 16 | 15 | 14 13 | 12 | 11 | 10 9 8 | 7 6 5 | 4 3 2 1 0 |
Content: | offset high | P | DPL | 0 | D | 1 1 0 | 0 0 0 | reserved |
Bit: | 31 16 | 15 0 |
Content: | segment selector | offset low |
Name Description
offset high The 16 highest bits of the 32 bit address in the segment.
offset low The 16 lowest bits of the 32 bits address in the segment.
DPL Descriptor Privilige Level, the privilege level the handler can be called from (0, 1, 2, 3).
r Reserved.
The offset is a pointer to code (preferably an assembly code label). For example, to create an entry for
a handler whose code starts at 0xDEADBEEF and that runs in privilege level 0 (therefore using the same
https://littleosbook.github.io/#first-steps 23/47
5/18/2019 The little book about OS development
code segment selector as the kernel) the following two bytes would be used:
0xDEAD8E00
0x0008BEEF
If the IDT is represented as an unsigned integer idt[512] then to register the above example as an
handler for interrupt 0 (divide-by-zero), the following code would be used:
idt[0] = 0xDEAD8E00
idt[1] = 0x0008BEEF
As written in the chapter “Getting to C”, we recommend that you instead of using bytes (or unsigned
integers) use packed structures to make the code more readable.
The reason for the question mark behind error code is that not all interrupts create an error code. The
specific CPU interrupts that put an error code on the stack are 8, 10, 11, 12, 13, 14 and 17. The error
code can be used by the interrupt handler to get more information on what has happened. Also, note
that the interrupt number is not pushed onto the stack. We can only determine what interrupt has
occurred by knowing what code is executing - if the handler registered for interrupt 17 is executing, then
interrupt 17 has occurred.
Once the interrupt handler is done, it uses the iret instruction to return. The instruction iret expects the
stack to be the same as at the time of the interrupt (see the figure above). Therefore, any values
pushed onto the stack by the interrupt handler must be popped. Before returning, iret restores eflags
by popping the value from the stack and then finally jumps to cs:eip as specified by the values on the
stack.
The interrupt handler has to be written in assembly code, since all registers that the interrupt handlers
use must be preserved by pushing them onto the stack. This is because the code that was interrupted
doesn’t know about the interrupt and will therefore expect that its registers stay the same. Writing all the
logic of the interrupt handler in assembly code will be tiresome. Creating a handler in assembly code
that saves the registers, calls a C function, restores the registers and finally executes iret is a good
idea!
The C handler should get the state of the registers, the state of the stack and the number of the
interrupt as arguments. The following definitions can for example be used:
struct cpu_state {
unsigned int eax;
unsigned int ebx;
unsigned int ecx;
.
.
.
unsigned int esp;
} __attribute__((packed));
struct stack_state {
unsigned int error_code;
https://littleosbook.github.io/#first-steps 24/47
5/18/2019 The little book about OS development
unsigned int eip;
unsigned int cs;
unsigned int eflags;
} __attribute__((packed));
void interrupt_handler(struct cpu_state cpu, struct stack_state stack, unsigned int interrupt);
%macro no_error_code_interrupt_handler %1
global interrupt_handler_%1
interrupt_handler_%1:
push dword 0 ; push 0 as error code
push dword %1 ; push the interrupt number
jmp common_interrupt_handler ; jump to the common handler
%endmacro
%macro error_code_interrupt_handler %1
global interrupt_handler_%1
interrupt_handler_%1:
push dword %1 ; push the interrupt number
jmp common_interrupt_handler ; jump to the common handler
%endmacro
https://littleosbook.github.io/#first-steps 25/47
5/18/2019 The little book about OS development
Since the macros declare global labels the addresses of the interrupt handlers can be accessed from C
or assembly code when creating the IDT.
global load_idt
Remap the interrupts. The PIC uses interrupts 0 - 15 for hardware interrupts by default, which
conflicts with the CPU interrupts. Therefore the PIC interrupts must be remapped to another
interval.
Select which interrupts to receive. You probably don’t want to receive interrupts from all devices
since you don’t have code that handles these interrupts anyway.
Set up the correct mode for the PIC.
In the beginning there was only one PIC (PIC 1) and eight interrupts. As more hardware were added, 8
interrupts were too few. The solution chosen was to chain on another PIC (PIC 2) on the first PIC (see
interrupt 2 on PIC 1).
5 LPT 2 13 Coprocessor
A great tutorial for configuring the PIC can be found at the SigOPS website [35]. We won’t repeat that
information here.
Every interrupt from the PIC has to be acknowledged - that is, sending a message to the PIC confirming
that the interrupt has been handled. If this isn’t done the PIC won’t generate any more interrupts.
Acknowledging a PIC interrupt is done by sending the byte 0x20 to the PIC that raised the interrupt.
Implementing a pic_acknowledge function can thus be done as follows:
https://littleosbook.github.io/#first-steps 26/47
5/18/2019 The little book about OS development
#include "io.h"
/** pic_acknowledge:
* Acknowledges an interrupt from either PIC 1 or PIC 2.
*
* @param num The number of the interrupt
*/
void pic_acknowledge(unsigned integer interrupt)
{
if (interrupt < PIC1_START_INTERRUPT || interrupt > PIC2_END_INTERRUPT) {
return;
}
#include "io.h"
/** read_scan_code:
* Reads a scan code from the keyboard
*
* @return The scan code (NOT an ASCII character!)
*/
unsigned char read_scan_code(void)
{
return inb(KBD_DATA_PORT);
}
The next step is to write a function that translates a scan code to the corresponding ASCII character. If
you want to map the scan codes to ASCII characters as is done on an American keyboard then Andries
Brouwer has a great tutorial [36].
Remember, since the keyboard interrupt is raised by the PIC, you must call pic_acknowledge at the end of
the keyboard interrupt handler. Also, the keyboard will not send you any more interrupts until you read
the scan code from the keyboard.
https://littleosbook.github.io/#first-steps 27/47
5/18/2019 The little book about OS development
proper abstractions (for memory, files, devices) to make application development easier, performs tasks
on behalf of applications (system calls) and schedules processes.
User mode, in contrast with kernel mode, is the environment in which the user’s programs execute. This
environment is less privileged than the kernel, and will prevent (badly written) user programs from
messing with other programs or the kernel. Badly written kernels are free to mess up what they want.
There’s quite a way to go until the OS created in this book can execute programs in user mode, but this
chapter will show how to easily execute a small program in kernel mode.
Instead of creating all these drivers and file systems we will use a feature in GRUB called modules to
load the program.
GRUB can load arbitrary files into memory from the ISO image, and these files are usually referred to
as modules. To make GRUB load a module, edit the file iso/boot/grub/menu.lst and add the following
line at the end of the file:
module /modules/program
mkdir -p iso/modules
The code that calls kmain must be updated to pass information to kmain about where it can find the
modules. We also want to tell GRUB that it should align all the modules on page boundaries when
loading them (see the chapter “Paging” for details about page alignment).
To instruct GRUB how to load our modules, the “multiboot header” - the first bytes of the kernel - must
be updated as follows:
; in file `loader.s`
GRUB will also store a pointer to a struct in the register ebx that, among other things, describes at
which addresses the modules are loaded. Therefore, you probably want to push ebx on the stack before
calling kmain to make it an argument for kmain.
https://littleosbook.github.io/#first-steps 28/47
5/18/2019 The little book about OS development
A program written at this stage can only perform a few actions. Therefore, a very short program that
writes a value to a register suffices as a test program. Halting Bochs after a while and then check that
register contains the correct number by looking in the Bochs log will verify that the program has run.
This is an example of such a short program:
; set eax to some distinguishable number, to read from the log afterwards
mov eax, 0xDEADBEEF
7.2.2 Compiling
Since our kernel cannot parse advanced executable formats we need to compile the code into a flat
binary. NASM can do this with the flag -f:
This is all we need. You must now move the file program to the folder iso/modules.
Before jumping to the program we must find where it resides in memory. Assuming that the contents of
ebx is passed as an argument to kmain, we can do this entirely from C.
The pointer in ebx points to a multiboot structure [19]. Download the multiboot.h file from
http://www.gnu.org/software/grub/manual/multiboot/html_node/multiboot.h.html, which describes the
structure.
The pointer passed to kmain in the ebx register can be cast to a multiboot_info_t pointer. The address of
the first module is in the field mods_addr. The following code shows an example:
However, before just blindly following the pointer, you should check that the module got loaded correctly
by GRUB. This can be done by checking the flags field of the multiboot_info_t structure. You should
also check the field mods_count to make sure it is exactly 1. For more details about the multiboot
structure, see the multiboot documentation [19].
The only thing left to do is to jump to the code loaded by GRUB. Since it is easier to parse the multiboot
structure in C than assembly code, calling the code from C is more convenient (it can of course be done
with jmp or call in assembly code as well). The C code could look like this:
https://littleosbook.github.io/#first-steps 29/47
5/18/2019 The little book about OS development
start_program();
/* we'll never get here, unless the module code returns */
If we start the kernel, wait until it has run and entered the infinite loop in the program, and then halt
Bochs, we should see 0xDEADBEEF in the register eax via the Bochs log. We have successfully started a
program in our OS!
It’s quite a lot of work and technical details to go through, but in a few chapters you’ll have working user
mode programs.
In the x86 architecture, virtual memory can be accomplished in two ways: segmentation and paging.
Paging is by far the most common and versatile technique, and we’ll implement it the next chapter.
Some use of segmentation is still necessary to allow for code to execute under different privilege levels.
Managing memory is a big part of what an operating system does. Paging and page frame allocation
deals with that.
It is interesting to note that in x86_64 (the 64-bit version of the x86 architecture), segmentation is almost
completely removed.
9 Paging
Segmentation translates a logical address into a linear address. Paging translates these linear
addresses onto the physical address space, and determines access rights and how the memory should
be cached.
https://littleosbook.github.io/#first-steps 30/47
5/18/2019 The little book about OS development
Paging is optional, and some operating systems do not make use of it. But if we want to mark certain
areas of memory accessible only to code running at a certain privilege level (to be able to have
processes running at different privilege levels), paging is the neatest way to do it.
All page directories, page tables and page frames need to be aligned on 4096 byte addresses. This
makes it possible to address a PDT, PT or PF with just the highest 20 bits of a 32 bit address, since the
lowest 12 need to be zero.
The PDE and PTE structure is very similar to each other: 32 bits (4 bytes), where the highest 20 bits
points to a PTE or PF, and the lowest 12 bits control access rights and other configurations. 4 bytes
times 1024 equals 4096 bytes, so a page directory and page table both fit in a page frame themselves.
The translation of linear addresses to physical addresses is described in the figure below.
While pages are normally 4096 bytes, it is also possible to use 4 MB pages. A PDE then points directly
to a 4 MB page frame, which needs to be aligned on a 4 MB address boundary. The address translation
is almost the same as in the figure, with just the page table step removed. It is possible to mix 4 MB and
4 KB pages.
https://littleosbook.github.io/#first-steps 31/47
5/18/2019 The little book about OS development
The 20 bits pointing to the current PDT is stored in the register cr3. The lower 12 bits of cr3 are used for
configuration.
For more details on the paging structures, see chapter 4 in the Intel manual [33]. The most interesting
bits are U/S, which determine what privilege levels can access this page (PL0 or PL3), and R/W, which
makes the memory in the page read-write or read-only.
The simplest kind of paging is when we map each virtual address onto the same physical address,
called identity paging. This can be done at compile time by creating a page directory where each entry
points to its corresponding 4 MB frame. In NASM this can be done with macros and commands (%rep,
times and dd). It can of course also be done at run-time by using ordinary assembly code instructions.
Paging is enabled by first writing the address of a page directory to cr3 and then setting bit 31 (the PG
“paging-enable” bit) of cr0 to 1. To use 4 MB pages, set the PSE bit (Page Size Extensions, bit 4) of cr4.
The following assembly code shows an example:
https://littleosbook.github.io/#first-steps 32/47
5/18/2019 The little book about OS development
mov ebx, cr0 ; read current cr0
or ebx, 0x80000000 ; set PG
mov cr0, ebx ; update cr0
It is important to note that all addresses within the page directory, page tables and in cr3 need to be
physical addresses to the structures, never virtual. This will be more relevant in later sections where we
dynamically update the paging structures (see the chapter “User Mode”).
An instruction that is useful when an updating a PDT or PT is invlpg. It invalidates the Translation
Lookaside Buffer (TLB) entry for a virtual address. The TLB is a cache for translated addresses,
mapping physical addresses corresponding to virtual addresses. This is only required when changing a
PDE or PTE that was previously mapped to something else. If the PDE or PTE had previously been
marked as not present (bit 0 was set to 0), executing invlpg is unnecessary. Changing the value of cr3
will cause all entries in the TLB to be invalidated.
If the kernel is placed at the beginning of the virtual address space - that is, the virtual address space
(0x00000000, "size of kernel") maps to the location of the kernel in memory - there will be issues when
linking the user mode process code. Normally, during linking, the linker assumes that the code will be
loaded into the memory position 0x00000000. Therefore, when resolving absolute references, 0x00000000
will be the base address for calculating the exact position. But if the kernel is mapped onto the virtual
address space (0x00000000, "size of kernel"), the user mode process cannot be loaded at virtual
address 0x00000000 - it must be placed somewhere else. Therefore, the assumption from the linker that
the user mode process is loaded into memory at position 0x00000000 is wrong. This can be corrected by
using a linker script which tells the linker to assume a different starting address, but that is a very
cumbersome solution for the users of the operating system.
This also assumes that we want the kernel to be part of the user mode process’ address space. As we
will see later, this is a nice feature, since during system calls we don’t have to change any paging
structures to get access to the kernel’s code and data. The kernel pages will of course require privilege
level 0 for access, to prevent a user process from reading or writing kernel memory.
Preferably, the kernel should be placed at a very high virtual memory address, for example 0xC0000000 (3
GB). The user mode process is not likely to be 3 GB large, which is now the only way that it can conflict
with the kernel. When the kernel uses virtual addresses at 3 GB and above it is called a higher-half
kernel. 0xC0000000 is just an example, the kernel can be placed at any address higher than 0 to get the
same benefits. Choosing the correct address depends on how much virtual memory should be available
for the kernel (it is easiest if all memory above the kernel virtual address should belong to the kernel)
and how much virtual memory should be available for the process.
https://littleosbook.github.io/#first-steps 33/47
5/18/2019 The little book about OS development
If the user mode process is larger than 3 GB, some pages will need to be swapped out by the kernel.
Swapping pages is not part of this book.
To start with, it is better to place the kernel at 0xC0100000 than 0xC0000000, since this makes it possible to
map (0x00000000, 0x00100000) to (0xC0000000, 0xC0100000). This way, the entire range (0x00000000, "size of
kernel") of memory is mapped to the range (0xC0000000, 0xC0000000 + "size of kernel").
Placing the kernel at 0xC0100000 isn’t hard, but it does require some thought. This is once again a linking
problem. When the linker resolves all absolute references in the kernel, it will assume that our kernel is
loaded at physical memory location 0x00100000, not 0x00000000, since relocation is used in the linker
script (see the section “Linking the kernel”). However, we want the jumps to be resolved using
0xC0100000 as base address, since otherwise a kernel jump will jump straight into the user mode process
code (remember that the user mode process is loaded at virtual memory 0x00000000).
However, we can’t simply tell the linker to assume that the kernel starts (is loaded) at 0xC01000000, since
we want it to be loaded at the physical address 0x00100000. The reason for having the kernel loaded at 1
MB is because it can’t be loaded at 0x00000000, since there is BIOS and GRUB code loaded below 1 MB.
Furthermore, we cannot assume that we can load the kernel at 0xC0100000, since the machine might not
have 3 GB of physical memory.
This can be solved by using both relocation (.=0xC0100000) and the AT instruction in the linker script.
Relocation specifies that non-relative memory-references should should use the relocation address as
base in address calculations. AT specifies where the kernel should be loaded into memory. Relocation is
done at link time by GNU ld [37], the load address specified by AT is handled by GRUB when loading the
kernel, and is part of the ELF format [18].
https://littleosbook.github.io/#first-steps 34/47
5/18/2019 The little book about OS development
When GRUB jumps to the kernel code, there is no paging table. Therefore, all references to 0xC0100000
+ X won’t be mapped to the correct physical address, and will therefore cause a general protection
exception (GPE) at the very best, otherwise (if the computer has more than 3 GB of memory) the
computer will just crash.
Therefore, assembly code that doesn’t use relative jumps or relative memory addressing must be used
to do the following:
If we skip the identity mapping for the first 4 MB, the CPU would generate a page fault immediately after
paging was enabled when trying to fetch the next instruction from memory. After the table has been
created, an jump can be done to a label to make eip point to a virtual address in the higher half:
higher_half:
; code here executes in the higher half kernel
; eip is larger than 0xC0000000
; can continue kernel initialisation, calling C code, etc.
The register eip will now point to a memory location somewhere right after 0xC0100000 - all the code can
now execute as if it were located at 0xC0100000, the higher-half. The entry mapping of the first 4 MB of
virtual memory to the first 4 MB of physical memory can now be removed from the page table and its
corresponding entry in the TLB invalidated with invlpg [0].
There are a few more details we must deal with when using a higher-half kernel. We must be careful
when using memory-mapped I/O that uses specific memory locations. For example, the frame buffer is
located at 0x000B8000, but since there is no entry in the page table for the address 0x000B8000 any longer,
the address 0xC00B8000 must be used, since the virtual address 0xC0000000 maps to the physical address
0x00000000.
Any explicit references to addresses within the multiboot structure needs to be changed to reflect the
new virtual addresses as well.
Mapping 4 MB pages for the kernel is simple, but wastes memory (unless you have a really big kernel).
Creating a higher-half kernel mapped in as 4 KB pages saves memory but is harder to set up. Memory
for the page directory and one page table can be reserved in the .data section, but one needs to
configure the mappings from virtual to physical addresses at run-time. The size of the kernel can be
determined by exporting labels from the linker script [37], which we’ll need to do later anyway when
writing the page frame allocator (see the chapter “Page Frame Allocation).
https://littleosbook.github.io/#first-steps 35/47
5/18/2019 The little book about OS development
located near 0x00000000 and the stack at just below 0xC0000000, and still not require more than two actual
pages.
First we need to know how much memory is available on the computer the OS is running on. The
easiest way to do this is to read it from the multiboot structure [19] passed to us by GRUB. GRUB
collects the information we need about the memory - what is reserved, I/O mapped, read-only etc. We
must also make sure that we don’t mark the part of memory used by the kernel as free (since GRUB
doesn’t mark this memory as reserved). One way to know how much memory the kernel uses is to
export labels at the beginning and the end of the kernel binary from the linker script:
kernel_virtual_end = .;
kernel_physical_end = . - 0xC0000000;
https://littleosbook.github.io/#first-steps 36/47
5/18/2019 The little book about OS development
These labels can directly be read from assembly code and pushed on the stack to make them available
to C code:
extern kernel_virtual_start
extern kernel_virtual_end
extern kernel_physical_start
extern kernel_physical_end
; ...
push kernel_physical_end
push kernel_physical_start
push kernel_virtual_end
push kernel_virtual_start
call kmain
This way we get the labels as arguments to kmain. If you want to use C instead of assembly code, one
way to do it is to declare the labels as functions and take the addresses of these functions:
void kernel_virtual_start(void);
/* ... */
If you use GRUB modules you need to make sure the memory they use is marked as reserved as well.
Note that the available memory does not need to be contiguous. In the first 1 MB there are several I/O-
mapped memory sections, as well as memory used by GRUB and the BIOS. Other parts of the memory
might be similarly unavailable.
It’s convenient to divide the memory sections into complete page frames, as we can’t map part of pages
into memory.
How do we know which page frames are in use? The page frame allocator needs to keep track of which
are free and which aren’t. There are several ways to do this: bitmaps, linked lists, trees, the Buddy
System (used by Linux) etc. For more information about the different algorithms see the article on
OSDev [38].
Bitmaps are quite easy to implement. One bit is used for each page frame and one (or more) page
frames are dedicated to store the bitmap. (Note that this is just one way to do it, other designs might be
better and/or more fun to implement.)
We need to map the page frame into virtual memory, by updating the PDT and/or PT used by the
kernel. What if all available page tables are full? Then we can’t map the page frame into memory,
because we’d need a new page table - which takes up an entire page frame - and to write to this page
frame we’d need to map its page frame… Somehow this circular dependency must be broken.
One solution is to reserve a part of the first page table used by the kernel (or some other higher-half
page table) for temporarily mapping page frames to make them accessible. If the kernel is mapped at
0xC0000000 (page directory entry with index 768), and 4 KB page frames are used, then the kernel has at
least one page table. If we assume - or limit us to - a kernel of size at most 4 MB minus 4 KB we can
https://littleosbook.github.io/#first-steps 37/47
5/18/2019 The little book about OS development
dedicate the last entry (entry 1023) of this page table for temporary mappings. The virtual address of
pages mapped in using the last entry of the kernel’s PT will be:
After we’ve temporarily mapped the page frame we want to use as a page table, and set it up to map in
our first page frame, we can add it to the paging directory, and remove the temporary mapping.
Kernighan and Ritchie [8] have an example implementation in their book [8] that we can draw inspiration
from. The only modification we need to do is to replace calls to sbrk/brk with calls to the page frame
allocator when more memory is needed. We must also make sure to map the page frames returned by
the page frame allocator to virtual addresses. A correct implementation should also return page frames
to the page frame allocator on call to free, whenever sufficiently large blocks of memory are freed.
11 User Mode
User mode is now almost within our reach, there are just a few more steps required to get there.
Although these steps might seem easy they way they are presented in this chapter, they can be tricky to
implement, since there are a lot of places where small errors will cause bugs that are hard to find.
The difference is the DPL, which now allows code to execute in PL3. The segments can still be used to
address the entire address space, just using these segments for user mode code will not protect the
kernel. For that we need paging.
Page frames for code, data and stack. At the moment it suffices to allocate one page frame for the
stack and enough page frames to fit the program’s code. Don’t worry about setting up a stack that
can be grow and shrink at this point in time, focus on getting a basic implementation work first.
The binary from the GRUB module has to be copied to the page frames used for the programs
code.
https://littleosbook.github.io/#first-steps 38/47
5/18/2019 The little book about OS development
A page directory and page tables are needed to map the page frames described above into
memory. At least two page tables are needed, because the code and data should be mapped in at
0x00000000 and increasing, and the stack should start just below the kernel, at 0xBFFFFFFB, growing
towards lower addresses. The U/S flag has to be set to allow PL3 access.
It might be convenient to store this information in a struct representing a process. This process struct
can be dynamically allocated with the kernel’s malloc function.
To enter user mode we set up the stack as if the processor had raised an inter-privilege level interrupt.
The stack should look like the following:
[esp + 16] ss ; the stack segment selector we want for user mode
[esp + 12] esp ; the user mode stack pointer
[esp + 8] eflags ; the control flags we want to use in user mode
[esp + 4] cs ; the code segment selector
[esp + 0] eip ; the instruction pointer of user mode code to execute
See the Intel manual [33], section 6.2.1, figure 6-4 for more information.
The instruction iret will then read these values from the stack and fill in the corresponding registers.
Before we execute iret we need to change to the page directory we setup for the user mode process. It
is important to remember that to continue executing kernel code after we’ve switched PDT, the kernel
needs to be mapped in. One way to accomplish this is to have a separate PDT for the kernel, which
maps all data at 0xC0000000 and above, and merge it with the user PDT (which only maps below
0xC0000000) when performing the switch. Remember that physical address of the PDT has to be used
when setting the register cr3.
The register eflags contains a set of different flags, specified in section 2.3 of the Intel manual [33]. Most
important for us is the interrupt enable (IF) flag. The assembly code instruction sti can’t be used in
privilege level 3 for enabling interrupts. If interrupts are disabled when entering user mode, then
interrupts can’t enabled once user mode is entered. Setting the IF flag in the eflags entry on the stack
will enable interrupts in user mode, since the assembly code instruction iret will set the register eflags
to the corresponding value on the stack.
For now, we should have interrupts disabled, as it requires a little more work to get inter-privilege level
interrupts to work properly (see the section “System calls”).
The value eip on the stack should point to the entry point for the user code - 0x00000000 in our case. The
value esp on the stack should be where the stack starts - 0xBFFFFFFB (0xC0000000 - 4).
The values cs and ss on the stack should be the segment selectors for the user code and user data
segments, respectively. As we saw in the segmentation chapter, the lowest two bits of a segment
selector is the RPL - the Requested Privilege Level. When using iret to enter PL3, the RPL of cs and ss
should be 0x3. The following code shows an example:
The register ds, and the other data segment registers, should be set to the same segment selector as
ss. They can be set the ordinary way, with the mov assembly code instruction.
https://littleosbook.github.io/#first-steps 39/47
5/18/2019 The little book about OS development
We are now ready to execute iret. If everything has been set up right, we should now have a kernel
that can enter user mode.
The reason we can use ELF [18] as the file format for for the kernel executable is because GRUB
knows how to parse and interpret the ELF file format. If we implemented an ELF parser, we could
compile the user mode programs into ELF binaries as well. We leave this as an exercise for the reader.
One thing we can do to make it easier to develop user mode programs is to allow the programs to be
written in C, but compile them to flat binaries instead of ELF binaries. In C the layout of the generated
code is more unpredictable and the entry point, main, might not be at offset 0 in the binary. One common
way to work around this is to add a few assembly code lines placed at offset 0 which calls main:
extern main
section .text
; push argv
; push argc
call main
; main has returned, eax is return value
jmp $ ; loop forever
If this code is saved in a file called start.s, then the following code show an example of a linker script
that places these instructions first in executable (remember that start.s gets compiled to start.o):
SECTIONS
{
. = 0; /* relocate to address 0 */
.text ALIGN(4):
{
start.o(.text) /* include the .text section of start.o */
*(.text) /* include all other .text sections */
}
.data ALIGN(4):
{
*(.data)
}
.rodata ALIGN(4):
{
*(.rodata*)
}
}
Note: *(.text) will not include the .text section of start.o again.
With this script we can write programs in C or assembler (or any other language that compiles to object
files linkable with ld), and it is easy to load and map for the kernel (.rodata will be mapped in as
writeable, though).
https://littleosbook.github.io/#first-steps 40/47
5/18/2019 The little book about OS development
The option -T instructs the linker to use the linker script link.ld.
11.4.1 A C Library
It might now be interesting to start thinking about writing a small “standard library” for your programs.
Some of the functionality requires system calls to work, but some, such as the functions in string.h,
does not.
12 File Systems
We are not required to have file systems in our operating system, but it is a very usable abstraction, and
it often plays a central part of many operating systems, especially UNIX-like operating systems. Before
we start the process of supporting multiple processes and system calls we might want to consider
implementing a simple file system.
In UNIX-like systems, with their almost-everything-is-a-file convention, these problems are solved by the
file system. (It might also be interesting to read a bit about the Plan 9 project, which takes this idea one
step further.)
A file system that is slightly more advanced than just the bits of one file is a file with metadata. The
metadata can describe the type of the file, the size of the file and so on. A utility program can be created
that runs at build time, adding this metadata to a file. This way, a “file system in a file” can be
constructed by concatenating several files with metadata into one large file. The result of this technique
is a read-only file system that resides in memory (once GRUB has loaded the file).
The program constructing the file system can traverse a directory on the host system and add all
subdirectories and files as part of the target file system. Each object in the file system (directory or file)
can consist of a header and a body, where the body of a file is the actual file and the body of a directory
is a list of entries - names and “addresses” of other files and directories.
Each object in this file system will become contiguous, so they will be easy to read from memory for the
kernel. All objects will also have a fixed size (except for the last one, which can grow), therefore it is
difficult to add new files or modify existing ones.
https://littleosbook.github.io/#first-steps 41/47
5/18/2019 The little book about OS development
When the need for a writable file system arises, then it is a good idea to look into the concept of an
inode. See the section “Further Reading” for recommended reading.
A virtual file system (VFS) creates an abstraction on top of the concrete file systems. A VFS mainly
supplies the path system and file hierarchy, it delegates operations on files to the underlying file
systems. The original paper on VFS is succinct and well worth a read. See the section “Further
Reading” for a reference.
With a VFS we could mount a special file system on the path /dev. This file system would handle all
devices such as keyboards and the console. However, one could also take the traditional UNIX
approach, with major/minor device numbers and mknod to create special files for devices. Which
approach you think is the most appropriate is up to you, there is no right or wrong when building
abstraction layers (although some abstractions turn out way more useful than others).
13 System Calls
System calls is the way user-mode applications interact with the kernel - to ask for resources, request
operations to be performed, etc. The system call API is the part of the kernel that is most exposed to the
users, therefore its design requires some thought.
When system calls are executed, the current privilege level is typically changed from PL3 to PL0 (if the
application is running in user mode). To allow this, the DPL of the entry in the IDT for the system call
interrupt needs to allow PL3 access.
Whenever inter-privilege level interrupts occur, the processor pushes a few important registers onto the
stack - the same ones we used to enter user mode before, see figure 6-4, section 6.12.1, in the Intel
manual [33]. What stack is used? The same section in [33] specifies that if an interrupt leads to code
https://littleosbook.github.io/#first-steps 42/47
5/18/2019 The little book about OS development
executing at a numerically lower privilege level, a stack switch occurs. The new values for the registers
ss and esp is loaded from the current Task State Segment (TSS). The TSS structure is specified in figure
7-2, section 7.2.1 of the Intel manual [33].
To enable system calls we need to setup a TSS before entering user mode. Setting it up can be done in
C by setting the ss0 and esp0 fields of a “packed struct” that represents a TSS. Before loading the
“packed struct” into the processor, a TSS descriptor has to be added to the GDT. The structure of the
TSS descriptor is described in section 7.2.2 in [33].
You specify the current TSS segment selector by loading it into the tr register with the ltr assembly
code instruction. If the TSS segment descriptor has index 5, and thus offset 5 * 8 = 40 = 0x28, this is the
value that should be loaded into the register tr.
When we entered user mode before in the chapter “Entering User Mode” we disabled interrupts when
executing in PL3. Since system calls are implemented using interrupts, interrupts must be enabled in
user mode. Setting the IF flag bit in the eflags value on the stack will make iret enable interrupts (since
the eflags value on the stack will be loaded into the eflags register by the assembly code instruction
iret).
14 Multitasking
How do you make multiple processes appear to run at the same time? Today, this question has two
answers:
With the availability of multi-core processors, or on system with multiple processors, two
processes can actually run at the same time by running two processes on different cores or
processors.
Fake it. That is, switch rapidly (faster than a human can notice) between the processes. At any
given moment there is only one process executing, but the rapid switching gives the impression
that they are running “at the same time”.
Since the operating system created in this book does not support multi-core processors or multiple
processors the only option is to fake it. The part of the operating system responsible for rapidly
switching between the processes is called the scheduling algorithm.
https://littleosbook.github.io/#first-steps 43/47
5/18/2019 The little book about OS development
and when the processes themselves are responsible for the scheduling it’s called cooperative
scheduling, since all the processes must cooperate with each other.
When a process yields the process’ entire state must be saved (all the registers), preferably on the
kernel heap in a structure that represents a process. When changing to a new process all the registers
must be restored from the saved values.
Scheduling can be implemented by keeping a list of which processes are running. The system call yield
should then run the next process in the list and put the current one last (other schemes are possible, but
this is a simple one).
The transfer of control to the new process is done via the iret assembly code instruction in exactly the
same way as explained in the section “Entering user mode” in the chapter “User Mode”.
We strongly recommend that you start to implement support for multiple processes by implementing
cooperative scheduling. We further recommend that you have a working solution for both exec, fork and
yield before implementing preemptive scheduling. Since cooperative scheduling is deterministic, it is
much easier to debug than preemptive scheduling.
To be able to do preemptive scheduling the PIT must first be configured to raise interrupts every x
milliseconds, where x should be configurable.
The configuration of the PIT is very similar to the configuration of other hardware devices: a byte is sent
to an I/O port. The command port of the PIT is 0x43. To read about all the configuration options, see the
article about the PIT on OSDev [39]. We use the following options:
Setting the interval for how often interrupts are to be raised is done via a divider, the same way as for
the serial port. Instead of sending the PIT a value (e.g. in milliseconds) that says how often an interrupt
should be raised you send the divider. The PIT operates at 1193182 Hz as default. Sending the divider
10 results in the PIT running at 1193182 / 10 = 119318 Hz. The divider can only be 16 bits, so it is only
possible to configure the timer’s frequency between 1193182 Hz and 1193182 / 65535 = 18.2 Hz. We
recommend that you create a function that takes an interval in milliseconds and converts it to the correct
divider.
The divider is sent to the channel 0 data I/O port of the PIT, but since only one byte can be sent at at a
time, the lowest 8 bits of the divider has to sent first, then the highest 8 bits of the divider can be sent.
The channel 0 data I/O port is located at 0x40. Again, see the article on OSDev [39] for more details.
https://littleosbook.github.io/#first-steps 44/47
5/18/2019 The little book about OS development
If all processes uses the same kernel stack (the stack exposed by the TSS) there will be trouble if a
process is interrupted while still in kernel mode. The process that is being switched to will now use the
same kernel stack and will overwrite what the previous process have written on the stack (remember
that TSS data structure points to the beginning of the stack).
To solve this problem every process should have it’s own kernel stack, the same way that each process
have their own user mode stack. When switching process the TSS must be updated to point to the new
process’ kernel stack.
When using preemptive scheduling one problem arises that doesn’t exist with cooperative scheduling.
With cooperative scheduling every time a process yields, it must be in user mode (privilege level 3),
since yield is a system call. With preemptive scheduling, the processes can be interrupted in either user
mode or kernel mode (privilege level 0), since the process itself does not control when it gets
interrupted.
Interrupting a process in kernel mode is a little bit different than interrupting a process in user mode,
due to the way the CPU sets up the stack at interrupts. If a privilege level change occurred (the process
was interrupted in user mode) the CPU will push the value of the process ss and esp register on the
stack. If no privilege level change occurs (the process was interrupted in kernel mode) the CPU won’t
push the esp register on the stack. Furthermore, if there was no privilege level change, the CPU won’t
change stack to the one defined it the TSS.
This problem is solved by calculating what the value of esp was before the interrupt. Since you know
that the CPU pushes 3 things on the stack when no privilege change happens and you know how much
you have pushed on the stack, you can calculate what the value of esp was at the time of the interrupt.
This is possible since the CPU won’t change stacks if there is no privilege level change, so the content
of esp will be the same as at the time of the interrupt.
To further complicate things, one must think of how to handle case when switching to a new process
that should be running in kernel mode. Since iret is being used without a privilege level change the
CPU won’t update the value of esp with the one placed on the stack - you must update esp yourself.
14.4 References
[1] Andrew Tanenbaum, 2007. Modern operating systems, 3rd edition. Prentice Hall, Inc.,
[8] Dennis M. Ritchie Brian W. Kernighan, 1988. The c programming language, second edition. Prentice
Hall, Inc.,
https://littleosbook.github.io/#first-steps 45/47
5/18/2019 The little book about OS development
[10] Free Software Foundation, GCC, the gNU compiler collection, http://gcc.gnu.org/,
[14] Volker Ruppert, bochs: The open souce iA-32 emulation project, http://bochs.sourceforge.net/,
[21] Lars Nodeen, Bug #426419: configure: error: GRUB requires a working absolute objcopy,
https://bugs.launchpad.net/ubuntu/+source/grub/+bug/426419,
[33] Intel, Intel 64 and iA-32 architectures software developer’s manual vol. 3A,
http://www.intel.com/content/ www/us/en/architecture-and-technology/64-ia-32-architectures-software-
developer-vol-3a-part-1-manual.html/,
https://littleosbook.github.io/#first-steps 46/47
5/18/2019 The little book about OS development
1. The bootloader must fit into the master boot record (MBR) boot sector of a hard drive, which is
only 512 bytes large.↩
https://littleosbook.github.io/#first-steps 47/47