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.
News
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.