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
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.