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
-
elementary OS 7.1 Now Available for Download
The team behind elementary OS has released the latest version of its operating system with a focus on personalization, inclusivity, accessibility, and privacy.
-
The GNU Project Celebrates Its 40th Birthday
September 27 marks the 40th anniversary of the GNU Project, and it was celebrated with a hacker meeting in Biel/Bienne, Switzerland.
-
Linux Kernel Reducing Long-Term Support
LTS support for the Linux kernel is about to undergo some serious changes that will have a considerable impact on the future.
-
Fedora 39 Beta Now Available for Testing
For fans and users of Fedora Linux, the first beta of release 39 is now available, which is a minor upgrade but does include GNOME 45.
-
Fedora Linux 40 to Drop X11 for KDE Plasma
When Fedora 40 arrives in 2024, there will be a few big changes coming, especially for the KDE Plasma option.
-
Real-Time Ubuntu Available in AWS Marketplace
Anyone looking for a Linux distribution for real-time processing could do a whole lot worse than Real-Time Ubuntu.
-
KSMBD Finally Reaches a Stable State
For those who've been looking forward to the first release of KSMBD, after two years it's no longer considered experimental.
-
Nitrux 3.0.0 Has Been Released
The latest version of Nitrux brings plenty of innovation and fresh apps to the table.
-
Linux From Scratch 12.0 Now Available
If you're looking to roll your own Linux distribution, the latest version of Linux From Scratch is now available with plenty of updates.
-
Linux Kernel 6.5 Has Been Released
The newest Linux kernel, version 6.5, now includes initial support for two very exciting features.