Let’s make your own operating system (#week06_integrate_user_modes)

Nimantha Gayan
4 min readAug 27, 2021

--

This is my sixth article of this building an own operating system article series. If you didn’t read the last articles , here is my first article of this series.

We discussed how to handle interrupts and get inputs with our operating system in the last article. I hope you were able to complete it successfully. In this post, we’ll look at how to establish a user mode that, unlike kernel mode, allows us to run user programs.

In previous parts we done that the kernel boots, prints to screen and reads from keyboard — what do we do? Usually, a kernel is not supposed to do the application logic itself, but leave that for applications. The kernel creates the proper abstractions to make application development easier, performs tasks on behalf of applications 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 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 article can execute programs in user mode, but this article will show how to easily execute a small program in kernel mode.

Loading an External Program

Where do we get the external program from? Somehow we need to load the code we want to execute into memory. More feature-complete operating systems usually have drivers and file systems that enable them to load the software from a CD-ROM drive, a hard disk or other persistent media.

Instead of creating all these drivers and file systems we will use a feature in GRUB called modules to load the program.

GRUB Modules

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 ‘module /modules/program’ at the end of the file:

Now create the folder iso/modules:

mkdir -p iso/modules

The application program will be created later.

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:

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.

Executing a Program

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:

After compiling this code using nasm -f bin program.s -o program we should move the program file to the folder iso/modules.

Finding the Program in Memory

To find the program, 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. Following multiboot.h file describe 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:

int kmain(/* additional arguments */ unsigned int ebx)
{
multiboot_info_t *mbinfo = (multiboot_info_t *) ebx;
unsigned int address_of_module = mbinfo->mods_addr;
}

Now we should check that the module got loaded correctly by GRUB. To that, we need to check the flags field of the multiboot_info_t structure. We should also check the field mods_count to make sure it is exactly 1.

Finally, we need to jump to the code loaded by GRUB. It is easier to call the code from C, we should update the kmain.c file with the following code.

typedef void (*call_module_t)(void);
call_module_t start_program = (call_module_t) address_of_module;
start_program();
/* we'll never get here, unless the module code returns */

The final kmain.c file will look like this,

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.

That's end of this week see you in next article.

Thank you for reading…

--

--

Nimantha Gayan
Nimantha Gayan

Written by Nimantha Gayan

Software Engineering , University Of Kelaniya

No responses yet