Building a hobby OS with Bochs and Qemu

Kernel Development

Operating systems form an interface between applications and hardware. Accordingly, it is to be expected that direct hardware access often takes place in the kernel. This hardware access is achieved through privileged machine language commands like in and out. In addition, special processor registers must be filled with contents that determine, for example, in which operating mode the CPU is running.

Kernel binaries do not have the usual program headers (such as Linux ELF or Windows PE files) and cannot be linked to libraries on the fly when loaded. There is no linker at the start.

If you use a debugger, you can check whether newly added code works as desired by setting breakpoints and inspecting register and memory contents in the running system. The CPU changes some memory areas autonomously, i.e., without a memory write instruction in the program. This applies, for example, to entries in the page tables where the processor independently sets access and dirty bits for individual memory pages.

Current Linux distributions are mostly 64-bit, and so linkers, compilers, and other tools run in 64-bit mode by default. In order to build a 32-bit operating system kernel, you therefore need to call the tools with options that switch to 32-bit operation. Listing 1 shows the defaults for gcc, nasm, and ld.

Listing 1

32-bit Operation

CFLAGS=-O0 -m32
ASMFLAGS=-f eleven
LDFLAGS=-m elf_i386

An easy-to-understand and compact introduction to x86 kernel development is provided by "Bran's Kernel Development Tutorial," which you can find at Archive.org [10]. In several sessions, the tutorial guides you to a mini kernel booting in an emulator, which activates the protected mode of the Intel processor, creates a segment table for memory access, contains two interrupt handlers (for the keyboard and timer module), generates the interrupt handler list, and switches on interrupts.

If you want to add further operating system features, you can, for example, work through the documents of the "Operating System Development" course offered by TH Nuremberg [11]. Some of the necessary files, further links, and a preconfigured VM with all development tools are available on the web [5].

Tool Setup

In addition to the usual tools of a development environment, you also need an assembler, because the start routine of the operating system is not a usual main() function. For testing the kernel, an emulator like Qemu or Bochs is a good choice. I used a computer with Ubuntu 20.04 as a test system, on which the necessary packages were quickly installed with the commands from Listing 2. However, the graphical user interface of the debugger integrated in Bochs did not work properly until I downgraded to an older Bochs version (see the box entitled "Problems with Bochs 2.6.11").

Listing 2

Installation

$ sudo apt install build-essential ...
$ apt install build-essential nasm qemu-system-x86 bochs bochs-x
$ sudo ln -s $(which qemu-system-i386) /usr/bin/qemu

Problems with Bochs 2.6.11

All attempts to display system tables like the GDT or the IDT with the current Bochs version 2.6.11 under Ubuntu 20.04 caused the emulator and debug windows to freeze. Also, several websites make reference to problems with version 2.6.11. For Ubuntu 20.04, the problem was solved by removing (apt remove …) the three packages bochs, bochs-x, and bochsbios, and reinstalling version 2.6.9. In addition, you have to import libtinfo5 and libreadline7 (Listing 3). Instead of typing paths, you will also find the packages via Launchpad [12] or a web search for bochs 2.6.9 ubuntu.

Listing 3

Solving the Bochs 2.6.11 Problem

$ sudo apt install libtinfo5
$ wget http://de.archive.ubuntu.com/ubuntu/pool/main/r/readline/\libreadline7_7.0-3_amd64.deb
$ for pkg in 396601834/bochsbios_2.6.9+dfsg-2_all.deb \396601839/bochs-x_2.6.9+dfsg-2_amd64.deb \ 396601840/bochs_2.6.9+dfsg-2_amd64.deb; \ do wget https://launchpadlibrarian.net/$pkg; done
$ sudo dpkg -i bochs*.deb libreadline*.deb

Boot Manager

A kernel image is created after compiling, assembling, and linking. The computer has to load and activate this image at power-up. From the BIOS or UEFI, the computer loads a boot sector on the selected medium, which contains further instructions for reloading the kernel.

Linux relied on the LILO (Linux Loader) bootloader in early versions; its successor Grub [4] is more flexible and is also excellent for starting your own kernel. A prepared disk image in FAT format with a Grub installation is available from Christian Steinrücken's GitHub page [13]. You only have to unzip the bootgrub.gz with Gunzip and can then mount it or manipulate it with the Mtools.

The floppy image in FAT format lets you modify files in the image without mounting. Using the Mcopy program from the Mtools [14], the kernel file can be updated without root privileges with the following command (including the two colons at the end)

$ mcopy -o -i kernel.img MyKernel.bin ::

Alternatively, you can use a minix-formatted Grub floppy image, but then you have to make changes to the image with a three step process consisting of mount, cp, and umount.

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Bochs Emulator

    Bochs, the granddaddy of all emulators, is alive and kicking; thanks to regular vitamin jabs, the lively old pretender can even handle Windows XP.

  • Sandboxing

     

  • User-Mode Linux

    User-Mode Linux feels like Linux because it is Linux. You’ll find a hundred uses for this fast and sensible virtual Linux system

  • QEMU System Emulation

    Do you ever wish you could run Linux within Linux? Or how about DOS within Linux? QEMU is an open source application that lets you emulate a complete hardware environment within your Linux system.

  • Virtualization Intro

    You’ll find a virtualization solution for every Linux environment – from the desktop to the enterprise server. In this month's cover story, we investigate some promising virtualization tools for Linux users.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News