Building a hobby OS with Bochs and Qemu
Kernel Development
Operating systems form an interface between applications and hardware. Accordingly, it is to be expected that direct hardware access often takes place in the kernel. This hardware access is achieved through privileged machine language commands like in
and out
. In addition, special processor registers must be filled with contents that determine, for example, in which operating mode the CPU is running.
Kernel binaries do not have the usual program headers (such as Linux ELF or Windows PE files) and cannot be linked to libraries on the fly when loaded. There is no linker at the start.
If you use a debugger, you can check whether newly added code works as desired by setting breakpoints and inspecting register and memory contents in the running system. The CPU changes some memory areas autonomously, i.e., without a memory write instruction in the program. This applies, for example, to entries in the page tables where the processor independently sets access and dirty bits for individual memory pages.
Current Linux distributions are mostly 64-bit, and so linkers, compilers, and other tools run in 64-bit mode by default. In order to build a 32-bit operating system kernel, you therefore need to call the tools with options that switch to 32-bit operation. Listing 1 shows the defaults for gcc, nasm, and ld.
Listing 1
32-bit Operation
CFLAGS=-O0 -m32 ASMFLAGS=-f eleven LDFLAGS=-m elf_i386
An easy-to-understand and compact introduction to x86 kernel development is provided by "Bran's Kernel Development Tutorial," which you can find at Archive.org [10]. In several sessions, the tutorial guides you to a mini kernel booting in an emulator, which activates the protected mode of the Intel processor, creates a segment table for memory access, contains two interrupt handlers (for the keyboard and timer module), generates the interrupt handler list, and switches on interrupts.
If you want to add further operating system features, you can, for example, work through the documents of the "Operating System Development" course offered by TH Nuremberg [11]. Some of the necessary files, further links, and a preconfigured VM with all development tools are available on the web [5].
Tool Setup
In addition to the usual tools of a development environment, you also need an assembler, because the start routine of the operating system is not a usual main()
function. For testing the kernel, an emulator like Qemu or Bochs is a good choice. I used a computer with Ubuntu 20.04 as a test system, on which the necessary packages were quickly installed with the commands from Listing 2. However, the graphical user interface of the debugger integrated in Bochs did not work properly until I downgraded to an older Bochs version (see the box entitled "Problems with Bochs 2.6.11").
Listing 2
Installation
$ sudo apt install build-essential ... $ apt install build-essential nasm qemu-system-x86 bochs bochs-x $ sudo ln -s $(which qemu-system-i386) /usr/bin/qemu
Problems with Bochs 2.6.11
All attempts to display system tables like the GDT or the IDT with the current Bochs version 2.6.11 under Ubuntu 20.04 caused the emulator and debug windows to freeze. Also, several websites make reference to problems with version 2.6.11. For Ubuntu 20.04, the problem was solved by removing (apt remove
…) the three packages bochs
, bochs-x
, and bochsbios
, and reinstalling version 2.6.9. In addition, you have to import libtinfo5
and libreadline7
(Listing 3). Instead of typing paths, you will also find the packages via Launchpad [12] or a web search for bochs 2.6.9 ubuntu
.
Listing 3
Solving the Bochs 2.6.11 Problem
$ sudo apt install libtinfo5 $ wget http://de.archive.ubuntu.com/ubuntu/pool/main/r/readline/\libreadline7_7.0-3_amd64.deb $ for pkg in 396601834/bochsbios_2.6.9+dfsg-2_all.deb \396601839/bochs-x_2.6.9+dfsg-2_amd64.deb \ 396601840/bochs_2.6.9+dfsg-2_amd64.deb; \ do wget https://launchpadlibrarian.net/$pkg; done $ sudo dpkg -i bochs*.deb libreadline*.deb
Boot Manager
A kernel image is created after compiling, assembling, and linking. The computer has to load and activate this image at power-up. From the BIOS or UEFI, the computer loads a boot sector on the selected medium, which contains further instructions for reloading the kernel.
Linux relied on the LILO (Linux Loader) bootloader in early versions; its successor Grub [4] is more flexible and is also excellent for starting your own kernel. A prepared disk image in FAT format with a Grub installation is available from Christian Steinrücken's GitHub page [13]. You only have to unzip the bootgrub.gz
with Gunzip and can then mount it or manipulate it with the Mtools.
The floppy image in FAT format lets you modify files in the image without mounting. Using the Mcopy program from the Mtools [14], the kernel file can be updated without root privileges with the following command (including the two colons at the end)
$ mcopy -o -i kernel.img MyKernel.bin ::
Alternatively, you can use a minix-formatted Grub floppy image, but then you have to make changes to the image with a three step process consisting of mount
, cp
, and umount
.
« Previous 1 2 3 Next »
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.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
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.