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
.
« Previous 1 2 3 Next »
Buy Linux Magazine
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.
News
-
Gnome 48 Debuts New Audio Player
To date, the audio player found within the Gnome desktop has been meh at best, but with the upcoming release that all changes.
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.