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
-
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.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.