liveBook · Manning

Chapter 11: Kernel

What you will learn in chapter 11 of Rust in Action: Systems programming concepts and techniques

The chapter introduces a project to build a minimal operating system in Rust and frames it as relevant to programming without an OS.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduces a project to build a minimal operating system in Rust and frames it as relevant to programming without an OS.

11.1 A fledgling operating system (FledgeOS)

Introduces FledgeOS and explains the basic responsibilities of an OS kernel.

Key terms: OS kernel, hardware, memory management, processes, threads

11.1.1 Setting up a development environment for developing an OS kernel

This section explains why building FledgeOS requires a specially prepared development environment and identifies the role of each required tool.

Key terms: nightly toolchain, rust-src, core Rust language, QEMU, virtual machine monitors, bootimage crate, boot image, cargo-binutils, llvm-tools-preview, LLVM

11.1.2 Verifying the development environment

This subsection gives checks for confirming that the tools needed to develop FledgeOS are installed and accessible.

Key terms: qemu-system-x86_64, cargo-binutils, bootimage, llvm-tools-preview, rustc

11.2 Fledgeos-0: Getting something working

The section introduces FledgeOS as short code that may take patience to understand because it exposes concepts normally hidden by an operating system, then invites the reader to see it run.

11.2.1 First boot

This section shows what the first FledgeOS boot looks like and explains the build-and-boot steps that make it possible.

Key terms: target platform, compiler target, core, bootloader, virtual environment, bootimage crate, fledgeos-0, first boot

11.2.2 Compilation instructions

This section gives the file setup steps for building the fledgeos-0 project by hand and points readers to the published source.

Key terms: Cargo.toml, fledge.json, .cargo/config.toml, src/main.rs

11.2.3 Source code listings

This section explains the build configuration and source-code differences that let a small Rust kernel boot and write to the screen without an operating system.

Key terms: fledge.json, .cargo/config.toml, compiler target, std::core, [package .metadata.bootimage], bootimage, build-command, run_command, QEMU, Never type (!)

11.2.4 Panic handling

This section explains why FledgeOS needs its own panic handler, contrasts two handler choices, and identifies the information available from PanicInfo.

Key terms: #[no_std], panic handling, Rust runtime, intrinsics::abort(), infinite loop, panic handler, PanicInfo, filename, line number

11.2.5 Writing to the screen with VGA-compatible text mode

This section explains how VGA-compatible text mode maps screen cells to memory and how FledgeOS writes to that memory with raw pointers.

Key terms: VGA-compatible text mode, frame buffer, 80x25 grid, offset(), write_volatile(), volatile write, dereference operator, pointer arithmetic

11.2.6 _start(): The main() function for FledgeOS

Explains why FledgeOS uses a bootloader and _start as its entry point instead of a conventional main() function.

Key terms: main(), bootloader, OS kernel, linker, _start, _exit()

11.3 fledgeos-1: Avoiding a busy loop

11.3.1 Being power conscious by interacting with the CPU directly

This section explains how FledgeOS avoids wasting power in its main loop by issuing the CPU’s halt instruction.

Key terms: hlt, interrupt, _start(), x86_64 crate

11.3.2 fledgeos-1 source code

This subsection identifies what changes in fledgeos-1, how to compile it, and the crate used to add assembly instructions.

Key terms: fledgeos-0, fledgeos-1, src/main.rs, x86_64 crate, assembly instructions

11.4 fledgeos-2: Custom exception handling

11.4.1 Handling exceptions properly, almost

The section explains how a personality function participates in exception handling and why FledgeOS uses an empty one.

Key terms: stack frame, stack unwinding, personality function, catching an exception, FledgeOS, minimal handler, language item, #[no_std], standard library

11.4.2 fledgeos-2 source code

This subsection identifies the fledgeos-2 source file and explains how to compile the project using earlier instructions.

Key terms: fledgeos-2, fledgeos-0

11.5 fledgeos-3: Text output

The section introduces screen text output as a way for FledgeOS to report panics.

Key terms: panic, screen, frame buffer

11.5.1 Writing colored text to the screen

The section introduces an enum for VGA text-mode colors, explaining why it groups the color constants and must follow the VGA standard.

Key terms: enum, const values, type safety, VGA-compatible text mode color palette, bit patterns

11.5.2 Controlling the in-memory representation of enums

This subsection explains how to prescribe an enum’s integer representation for external requirements and why doing so limits compiler flexibility.

Key terms: repr attribute, discriminant, u8, VGA-compatible text mode, prescribed representation, space optimizations, single variant

11.5.3 Why use enums?

The section explains why an enum is preferable to an u8 for representing colors, even though numeric constants could have the same in-memory representation.

Key terms: enum, u8, illegal value, numeric constants

11.5.4 Creating a type that can print to the VGA frame buffer

This section introduces Cursor as the interface between the kernel’s code and the VGA frame buffer.

Key terms: Cursor, Color, VGA frame buffer

11.5.5 Printing to the screen

This subsection shows how to print to the screen with Cursor by setting its position and calling Cursor.print() with a reference.

Key terms: Cursor, Cursor.print(), _start()

11.5.6 fledgeos-3 source code

This section points to the complete fledgeos-3 source file and says to compile it using the earlier instructions with the project name changed.

11.6 fledgeos-4: Custom panic handling

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

The section introduces associating core::fmt::Write with a panic handler to display a message when a panic occurs.

Key terms: panic handler, core::fmt::Write, embedded development, microcontrollers

11.6.2 Reimplementing panic() by making use of core::fmt::Write

This subsection explains how FledgeOS’s panic handler clears the screen and then uses core::write! to report an error.

Key terms: panic(), core::write!, cursor, core::fmt::Write

11.6.3 Implementing core::fmt::Write

This subsection explains how Cursor implements core::fmt::Write by supplying write_str() and reusing its existing print() method.

Key terms: core::fmt::Write, write_str(), print(), &str, &[u8], to_bytes()

11.6.4 fledge-4 source code

Points to the FledgeOS-4 panic-handling source code and directs readers to reuse the earlier compilation instructions with the project name changed.

11.7 Summary

Concepts explained in this chapter

← Chapter 10: Processes, threads, and containers Chapter 12: Signals, interrupts, and exceptions →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.