Building a hobby OS with Bochs and Qemu
Debugging
Troubleshooting an operating system is more difficult than troubleshooting an application running as a process. For the simplest form of debugging, insert informative output into the code that displays memory addresses, variable contents, and other state information on the screen.
In order not to mix the regular screen output with debug information all the time, you can write to a virtual serial or parallel port in the emulator and redirect it to a file (or a terminal window).
For Qemu, the additional -serial mon:stdio
option ensures that output appears on the serial port in the terminal from which the emulator was started. Bochs lets you redirect serial output to a file, if you add a matching line in the Bochs configuration (.bochsrc
) (Listing 4). Instead of a file, the target can also be a terminal window via mode=term, dev=/dev/pts/5
, whose device file the developer queries in advance with tty
.
Listing 4
Redirecting serial output
com1: enabled=1, mode=file, dev=serial.out
Listing 5 shows a simple implementation of the uartputc
function, which sends a single character to the first serial port (COM1): It uses the 0x3f8 port for this purpose. The functions used here, outportb
and inportb
, execute the x86 instructions out
and in
via inline assembler.
Listing 5
Debug Output on COM1
#define COM1_BASE 0x3f8 // first serial port void uartputc (char c) { while (inportb (COM1_BASE+5) & 0x20) == 0) ; outportb (COM1_BASE+0, c); } char inportb (short port) { char wert; asm ("inb %1, %0" : "=a" (rv) : "dN" (port)); return value; } void outportb (short port, char value) { asm ("outb %1, %0" : : : "dN" (port), "a" (value)); }
Bochs has an integrated text mode debugger, which is made more convenient by a graphical frontend (in the bochs-x package). At the bottom (above the status line), there is an input line in which you can set a breakpoint, for example, using lb 0xc0101496
. When the emulated computer jumps to this address, Bochs interrupts the execution.
By pressing F11, the developer can now execute one machine-language command after another, step by step; Bochs displays the disassembled code in the middle column (Figure 3). You can find the relevant address up front by looking at the symbol table, which the command from Listing 6 generates from the kernel binary.
Listing 6
Create Symbol Table
$ objdump -M intel -D kernel.bin | grep -e '^[^ ]* <' | sed -e 's/<//' -e 's/>://'
The kernel can also be debugged with Qemu. If Qemu is started with the option -s
, it listens locally on TCP port 1234 and can be remotely controlled with Gdb. Next call the debugger with the kernel binary as a parameter – it is now aware of the symbolic names, i.e., functions and variables – and then establish a connection to the kernel from within Gdb by typing target remote tcp::1234
. You can then stop the kernel in Gdb and display the assembler code of individual functions, using disassemble,
for example (Figure 4).
Outlook
By experimenting with the example files for this article [5], you can easily add additional features. If you want to turn this project into a real Unix system with multitasking and some standard shell tools, take a look at Ulix [15]: I developed the Unix-style 32-bit system for use in computer science studies and documented the complete source code in a 700-page book [16]. The book also introduces readers to the theoretical basics of operating systems. An amazingly long overview of other hobby operating systems is available at OSDev.org [17]. If you want to build your own kernel, you're not alone.
Infos
- Posts by Linus Torvalds: https://en.wikiquote.org/wiki/Linus_Torvalds
- Qemu: https://www.qemu.org
- Bochs: http://bochs.sourceforge.net
- Grub: https://www.gnu.org/software/grub/
- Example files: http://swf.hgesser.de/d/kernel/en/
- x86 assembler language: https://en.wikipedia.org/wiki/X86_assembly_language
- Paging: https://en.wikipedia.org/wiki/Paging
- Segmentation: https://en.wikipedia.org/wiki/Memory_segmentation
- Minix: http://www.minix3.org/
- Kernel Tutorial: https://web.archive.org/web/20191123051335/http://www.osdever.net/tutorials/view/brans-kernel-development-tutorial
- Operating System Development, TH Nuremberg: http://ohm.hgesser.de/be-ws2015/en/
- Bochs 2.6.9 for Ubuntu: https://launchpad.net/ubuntu/+source/bochs/2.6.9+dfsg-2/+build/15625627
- Grub FAT boot disk: https://q4.github.io/bootgrub.html
- Mtools: https://www.gnu.org/software/mtools/
- Ulix: http://ulixos.org
- "The Design and Implementation of the ULIX Operating System": http://ulixos.org/doc/ulix-book-0.13.pdf
- OSDev.org wiki: https://wiki.osdev.org/Projects
« Previous 1 2 3
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.