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
-
The Gnome Foundation Struggling to Stay Afloat
The foundation behind the Gnome desktop environment is having to go through some serious belt-tightening due to continued financial problems.
-
Thousands of Linux Servers Infected with Stealth Malware Since 2021
Perfctl is capable of remaining undetected, which makes it dangerous and hard to mitigate.
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.