chapter eleven

11 Kernel

 

Let’s build an operating system. By the end of the chapter, you’ll be running your own OS (or at least, a minimal subset of an OS). Not only that, but you would have compiled your own bootloader, kernel and the Rust language itself for your new operating system.

This chapter covers many features of Rust that are important for programming without an operating system. Accordingly, the chapter is important for programmers who work with Rust for embedded devices.

11.1 A fledgling operating system (FledgeOS)

This section implements an operating system kernel. The kernel of an operating system performs several important roles, such as interacting with hardware, memory management and coordinating work. Typically work is coordinated through processes and threads. We won’t be able to cover much of that in this chapter. But we will get off the ground. We’ll fledge. So let’s call the system we’re building FledgeOS.

11.1.1 Setting up a development environment for developing an operating system kernel

Creating an executable for an operating system that doesn’t exist yet is a complicated process. For instance, we need to compile the core Rust language for the OS from your current one. But, your current environment only understands your current environment. Let’s extend it.

Several tools are needed to help us out.

There are several components that you need to install and/or configure.

11.1.2 fledgeos project structure

11.1.3 A minimal operating system kernel

11.1.4 Panic handling

11.1.5 Writing to the screen with VGA-compatible text mode

11.1.6 _start(), the "main()" function for FledgeOS

11.1.7 Being power conscious by interacting with the CPU directly

11.1.8 Handling exceptions properly, almost

11.2 Nice output

11.2.1 Controlling the in-memory representation of enums

11.2.2 Why use enums?

11.2.3 Creating a type that can “print” to the VGA frame buffer

11.2.4 Printing to the screen

11.2.5 Full code listing of FledgeOS with printing enabled

11.3 Implementing a panic handler that reports the error to the user

11.3.1 Re-implementing panic() by making use of core::fmt::Write

11.3.2 Implementing core::fmt::Write

11.3.3 Full code listing for FledgeOS with user-friendly panic handling

11.4 Summing Up