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
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.