In this post we explore double faults in detail. We also set up an Interrupt Stack Table to catch double faults on a separate kernel stack. This way, we can completely prevent triple faults, even on kernel stack overflow.

As always, the complete source code is available on GitHub. Please file issues for any problems, questions, or improvement suggestions. There is also a gitter chat and a comment section at the end of this page.

What is a Double Fault?

In simplified terms, a double fault is a special exception that occurs when the CPU fails to invoke an exception handler. For example, it occurs when a page fault is triggered but there is no page fault handler registered in the Interrupt Descriptor Table (IDT). So it’s kind of similar to catch-all blocks in programming languages with exceptions, e.g. catch(...) in C++ or catch(Exception e) in Java or C#.

A double fault behaves like a normal exception. It has the vector number 8 and we can define a normal handler function for it in the IDT. It is really important to provide a double fault handler, because if a double fault is unhandled a fatal triple fault occurs. Triple faults can’t be caught and most hardware reacts with a system reset.

Triggering a Double Fault

Let’s provoke a double fault by triggering an exception for that we didn’t define a handler function:

  1. // in src/lib.rs
  2. #[no_mangle]
  3. pub extern "C" fn rust_main(multiboot_information_address: usize) {
  4. ...
  5. // initialize our IDT
  6. interrupts::init();
  7. // trigger a page fault
  8. unsafe {
  9. *(0xdeadbeaf as *mut u64) = 42;
  10. };
  11. println!("It did not crash!");
  12. loop {}
  13. }

We try to write to address 0xdeadbeaf, but the corresponding page is not present in the page tables. Thus, a page fault occurs. We haven’t registered a page fault handler in our IDT, so a double fault occurs.

When we start our kernel now, we see that it enters an endless boot loop:

boot loop

The reason for the boot loop is the following:

  1. The CPU tries to write to 0xdeadbeaf, which causes a page fault.
  2. The CPU looks at the corresponding entry in the IDT and sees that the present bit isn’t set. Thus, it can’t call the page fault handler and a double fault occurs.
  3. The CPU looks at the IDT entry of the double fault handler, but this entry is also non-present. Thus, a triple fault occurs.
  4. A triple fault is fatal. QEMU reacts to it like most real hardware and issues a system reset.

So in order to prevent this triple fault, we need to either provide a handler function for page faults or a double fault handler. Let’s start with the latter, since we want to avoid triple faults in all cases.

A Double Fault Handler

A double fault is a normal exception with an error code, so we can use our handler_with_error_code macro to create a wrapper function:

  1. // in src/interrupts.rs
  2. lazy_static! {
  3. static ref IDT: idt::Idt = {
  4. let mut idt = idt::Idt::new();
  5. idt.breakpoint.set_handler_fn(breakpoint_handler);
  6. idt.double_fault.set_handler_fn(double_fault_handler);
  7. idt
  8. };
  9. }
  10. // our new double fault handler
  11. extern "x86-interrupt" fn double_fault_handler(
  12. stack_frame: &mut ExceptionStackFrame, _error_code: u64)
  13. {
  14. println!("\nEXCEPTION: DOUBLE FAULT\n{:#?}", stack_frame);
  15. loop {}
  16. }

Our handler prints a short error message and dumps the exception stack frame. The error code of the double fault handler is always zero, so there’s no reason to print it.

When we start our kernel now, we should see that the double fault handler is invoked:

QEMU printing `EXCEPTION: DOUBLE FAULT` and the exception stack frame

It worked! Here is what happens this time:

  1. The CPU executes tries to write to 0xdeadbeaf, which causes a page fault.
  2. Like before, the CPU looks at the corresponding entry in the IDT and sees that the present bit isn’t set. Thus, a double fault occurs.
  3. The CPU jumps to the – now present – double fault handler.

The triple fault (and the boot-loop) no longer occurs, since the CPU can now call the double fault handler.

That was quite straightforward! So why do we need a whole post for this topic? Well, we’re now able to catch most double faults, but there are some cases where our current approach doesn’t suffice.

Causes of Double Faults

Before we look at the special cases, we need to know the exact causes of double faults. Above, we used a pretty vague definition:

A double fault is a special exception that occurs when the CPU fails to invoke an exception handler.

What does “fails to invoke” mean exactly? The handler is not present? The handler is swapped out? And what happens if a handler causes exceptions itself?

For example, what happens if… :

  1. a divide-by-zero exception occurs, but the corresponding handler function is swapped out?
  2. a page fault occurs, but the page fault handler is swapped out?
  3. a divide-by-zero handler causes a breakpoint exception, but the breakpoint handler is swapped out?
  4. our kernel overflows its stack and the guard page is hit?

Fortunately, the AMD64 manual (PDF) has an exact definition (in Section 8.2.9). According to it, a “double fault exception can occur when a second exception occurs during the handling of a prior (first) exception handler”. The “can” is important: Only very specific combinations of exceptions lead to a double fault. These combinations are:

First Exception Second Exception
Divide-by-zero,
Invalid TSS,
Segment Not Present,
Stack-Segment Fault,
General Protection Fault
Invalid TSS,
Segment Not Present,
Stack-Segment Fault,
General Protection Fault
Page Fault Page Fault,
Invalid TSS,
Segment Not Present,
Stack-Segment Fault,
General Protection Fault

So for example a divide-by-zero fault followed by a page fault is fine (the page fault handler is invoked), but a divide-by-zero fault followed by a general-protection fault leads to a double fault.

With the help of this table, we can answer the first three of the above questions:

  1. If a divide-by-zero exception occurs and the corresponding handler function is swapped out, a page fault occurs and the page fault handler is invoked.
  2. If a page fault occurs and the page fault handler is swapped out, a double fault occurs and the double fault handler is invoked.
  3. If a divide-by-zero handler causes a breakpoint exception, the CPU tries to invoke the breakpoint handler. If the breakpoint handler is swapped out, a page fault occurs and the page fault handler is invoked.

In fact, even the case of a non-present handler follows this scheme: A non-present handler causes a segment-not-present exception. We didn’t define a segment-not-present handler, so another segment-not-present exception occurs. According to the table, this leads to a double fault.

Kernel Stack Overflow

Let’s look at the fourth question:

What happens if our kernel overflows its stack and the guard page is hit?

When our kernel overflows its stack and hits the guard page, a page fault occurs. The CPU looks up the page fault handler in the IDT and tries to push the exception stack frame onto the stack. However, our current stack pointer still points to the non-present guard page. Thus, a second page fault occurs, which causes a double fault (according to the above table).

So the CPU tries to call our double fault handler now. However, on a double fault the CPU tries to push the exception stack frame, too. Our stack pointer still points to the guard page, so a third page fault occurs, which causes a triple fault and a system reboot. So our current double fault handler can’t avoid a triple fault in this case.

Let’s try it ourselves! We can easily provoke a kernel stack overflow by calling a function that recurses endlessly:

  1. // in src/lib.rs
  2. #[no_mangle]
  3. pub extern "C" fn rust_main(multiboot_information_address: usize) {
  4. ...
  5. // initialize our IDT
  6. interrupts::init();
  7. fn stack_overflow() {
  8. stack_overflow(); // for each recursion, the return address is pushed
  9. }
  10. // trigger a stack overflow
  11. stack_overflow();
  12. println!("It did not crash!");
  13. loop {}
  14. }

When we try this code in QEMU, we see that the system enters a boot-loop again.

So how can we avoid this problem? We can’t omit the pushing of the exception stack frame, since the CPU itself does it. So we need to ensure somehow that the stack is always valid when a double fault exception occurs. Fortunately, the x86_64 architecture has a solution to this problem.

Switching Stacks

The x86_64 architecture is able to switch to a predefined, known-good stack when an exception occurs. This switch happens at hardware level, so it can be performed before the CPU pushes the exception stack frame.

This switching mechanism is implemented as an Interrupt Stack Table (IST). The IST is a table of 7 pointers to known-good stacks. In Rust-like pseudo code:

  1. struct InterruptStackTable {
  2. stack_pointers: [Option<StackPointer>; 7],
  3. }

For each exception handler, we can choose a stack from the IST through the options field in the corresponding IDT entry. For example, we could use the first stack in the IST for our double fault handler. Then the CPU would automatically switch to this stack whenever a double fault occurs. This switch would happen before anything is pushed, so it would prevent the triple fault.

Allocating a new Stack

In order to fill an Interrupt Stack Table later, we need a way to allocate new stacks. Therefore we extend our memory module with a new stack_allocator submodule:

  1. // in src/memory/mod.rs
  2. mod stack_allocator;

First, we create a new StackAllocator struct and a constructor function:

  1. // in src/memory/stack_allocator.rs
  2. use memory::paging::PageIter;
  3. pub struct StackAllocator {
  4. range: PageIter,
  5. }
  6. impl StackAllocator {
  7. pub fn new(page_range: PageIter) -> StackAllocator {
  8. StackAllocator { range: page_range }
  9. }
  10. }

We create a simple StackAllocator that allocates stacks from a given range of pages (PageIter is an Iterator over a range of pages; we introduced it in the kernel heap post.).

We add a alloc_stack method that allocates a new stack:

  1. // in src/memory/stack_allocator.rs
  2. use memory::paging::{self, Page, ActivePageTable};
  3. use memory::{PAGE_SIZE, FrameAllocator};
  4. impl StackAllocator {
  5. pub fn alloc_stack<FA: FrameAllocator>(&mut self,
  6. active_table: &mut ActivePageTable,
  7. frame_allocator: &mut FA,
  8. size_in_pages: usize)
  9. -> Option<Stack> {
  10. if size_in_pages == 0 {
  11. return None; /* a zero sized stack makes no sense */
  12. }
  13. // clone the range, since we only want to change it on success
  14. let mut range = self.range.clone();
  15. // try to allocate the stack pages and a guard page
  16. let guard_page = range.next();
  17. let stack_start = range.next();
  18. let stack_end = if size_in_pages == 1 {
  19. stack_start
  20. } else {
  21. // choose the (size_in_pages-2)th element, since index
  22. // starts at 0 and we already allocated the start page
  23. range.nth(size_in_pages - 2)
  24. };
  25. match (guard_page, stack_start, stack_end) {
  26. (Some(_), Some(start), Some(end)) => {
  27. // success! write back updated range
  28. self.range = range;
  29. // map stack pages to physical frames
  30. for page in Page::range_inclusive(start, end) {
  31. active_table.map(page, paging::WRITABLE, frame_allocator);
  32. }
  33. // create a new stack
  34. let top_of_stack = end.start_address() + PAGE_SIZE;
  35. Some(Stack::new(top_of_stack, start.start_address()))
  36. }
  37. _ => None, /* not enough pages */
  38. }
  39. }
  40. }

The method takes mutable references to the ActivePageTable and a FrameAllocator, since it needs to map the new virtual stack pages to physical frames. We define that the stack size is a multiple of the page size.

Instead of operating directly on self.range, we clone it and only write it back on success. This way, subsequent stack allocations can still succeed if there are pages left (e.g., a call with size_in_pages = 3 can still succeed after a failed call with size_in_pages = 100).

In order to be able to clone PageIter, we add a #[derive(Clone)] to its definition in src/memory/paging/mod.rs. We also need to make the start_address method of the Page type public (in the same file).

The actual allocation is straightforward: First, we choose the next page as guard page. Then we choose the next size_in_pages pages as stack pages using Iterator::nth. If all three variables are Some, the allocation succeeded and we map the stack pages to physical frames using ActivePageTable::map. The guard page remains unmapped.

Finally, we create and return a new Stack, which we define as follows:

  1. // in src/memory/stack_allocator.rs
  2. #[derive(Debug)]
  3. pub struct Stack {
  4. top: usize,
  5. bottom: usize,
  6. }
  7. impl Stack {
  8. fn new(top: usize, bottom: usize) -> Stack {
  9. assert!(top > bottom);
  10. Stack {
  11. top: top,
  12. bottom: bottom,
  13. }
  14. }
  15. pub fn top(&self) -> usize {
  16. self.top
  17. }
  18. pub fn bottom(&self) -> usize {
  19. self.bottom
  20. }
  21. }

The Stack struct describes a stack though its top and bottom addresses.

The Memory Controller

Now we’re able to allocate a new double fault stack. However, we add one more level of abstraction to make things easier. For that we add a new MemoryController type to our memory module:

  1. // in src/memory/mod.rs
  2. pub use self::stack_allocator::Stack;
  3. pub struct MemoryController {
  4. active_table: paging::ActivePageTable,
  5. frame_allocator: AreaFrameAllocator,
  6. stack_allocator: stack_allocator::StackAllocator,
  7. }
  8. impl MemoryController {
  9. pub fn alloc_stack(&mut self, size_in_pages: usize) -> Option<Stack> {
  10. let &mut MemoryController { ref mut active_table,
  11. ref mut frame_allocator,
  12. ref mut stack_allocator } = self;
  13. stack_allocator.alloc_stack(active_table, frame_allocator,
  14. size_in_pages)
  15. }
  16. }

The MemoryController struct holds the three types that are required for alloc_stack and provides a simpler interface (only one argument). The alloc_stack wrapper just takes the tree types as &mut through destructuring and forwards them to the stack_allocator. The ref mut-s are needed to take the inner fields by mutable reference. Note that we’re re-exporting the Stack type since it is returned by alloc_stack.

The last step is to create a StackAllocator and return a MemoryController from memory::init:

  1. // in src/memory/mod.rs
  2. pub fn init(boot_info: &BootInformation) -> MemoryController {
  3. ...
  4. let stack_allocator = {
  5. let stack_alloc_start = heap_end_page + 1;
  6. let stack_alloc_end = stack_alloc_start + 100;
  7. let stack_alloc_range = Page::range_inclusive(stack_alloc_start,
  8. stack_alloc_end);
  9. stack_allocator::StackAllocator::new(stack_alloc_range)
  10. };
  11. MemoryController {
  12. active_table: active_table,
  13. frame_allocator: frame_allocator,
  14. stack_allocator: stack_allocator,
  15. }
  16. }

We create a new StackAllocator with a range of 100 pages starting right after the last heap page.

In order to do arithmetic on pages (e.g. calculate the hundredth page after stack_alloc_start), we implement Add<usize> for Page:

  1. // in src/memory/paging/mod.rs
  2. use core::ops::Add;
  3. impl Add<usize> for Page {
  4. type Output = Page;
  5. fn add(self, rhs: usize) -> Page {
  6. Page { number: self.number + rhs }
  7. }
  8. }

Allocating a Double Fault Stack

Now we can allocate a new double fault stack by passing the memory controller to our interrupts::init function:

  1. // in src/lib.rs
  2. #[no_mangle]
  3. pub extern "C" fn rust_main(multiboot_information_address: usize) {
  4. ...
  5. // set up guard page and map the heap pages
  6. let mut memory_controller = memory::init(boot_info); // new return type
  7. // initialize our IDT
  8. interrupts::init(&mut memory_controller); // new argument
  9. ...
  10. }
  11. // in src/interrupts.rs
  12. use memory::MemoryController;
  13. pub fn init(memory_controller: &mut MemoryController) {
  14. let double_fault_stack = memory_controller.alloc_stack(1)
  15. .expect("could not allocate double fault stack");
  16. IDT.load();
  17. }

We allocate a 4096 bytes stack (one page) for our double fault handler. Now we just need some way to tell the CPU that it should use this stack for handling double faults.

The IST and TSS

The Interrupt Stack Table (IST) is part of an old legacy structure called Task State Segment (TSS). The TSS used to hold various information (e.g. processor register state) about a task in 32-bit mode and was for example used for hardware context switching. However, hardware context switching is no longer supported in 64-bit mode and the format of the TSS changed completely.

On x86_64, the TSS no longer holds any task specific information at all. Instead, it holds two stack tables (the IST is one of them). The only common field between the 32-bit and 64-bit TSS is the pointer to the I/O port permissions bitmap.

The 64-bit TSS has the following format:

Field Type
(reserved) u32
Privilege Stack Table [u64; 3]
(reserved) u64
Interrupt Stack Table [u64; 7]
(reserved) u64
(reserved) u16
I/O Map Base Address u16

The Privilege Stack Table is used by the CPU when the privilege level changes. For example, if an exception occurs while the CPU is in user mode (privilege level 3), the CPU normally switches to kernel mode (privilege level 0) before invoking the exception handler. In that case, the CPU would switch to the 0th stack in the Privilege Stack Table (since 0 is the target privilege level). We don’t have any user mode programs yet, so we ignore this table for now.

Creating a TSS

Let’s create a new TSS that contains our double fault stack in its interrupt stack table. For that we need a TSS struct. Fortunately, the x86_64 crate already contains a TaskStateSegment struct that we can use:

  1. // in src/interrupts.rs
  2. use x86_64::structures::tss::TaskStateSegment;

Let’s create a new TSS in our interrupts::init function:

  1. // in src/interrupts.rs
  2. use x86_64::VirtualAddress;
  3. const DOUBLE_FAULT_IST_INDEX: usize = 0;
  4. pub fn init(memory_controller: &mut MemoryController) {
  5. let double_fault_stack = memory_controller.alloc_stack(1)
  6. .expect("could not allocate double fault stack");
  7. let mut tss = TaskStateSegment::new();
  8. tss.interrupt_stack_table[DOUBLE_FAULT_IST_INDEX] = VirtualAddress(
  9. double_fault_stack.top());
  10. IDT.load();
  11. }

We define that the 0th IST entry is the double fault stack (any other IST index would work too). We create a new TSS through the TaskStateSegment::new function and load the top address (stacks grow downwards) of the double fault stack into the 0th entry.

Loading the TSS

Now that we created a new TSS, we need a way to tell the CPU that it should use it. Unfortunately, this is a bit cumbersome, since the TSS is a Task State Segment (for historical reasons). So instead of loading the table directly, we need to add a new segment descriptor to the Global Descriptor Table (GDT). Then we can load our TSS invoking the ltr instruction with the respective GDT index.

The Global Descriptor Table (again)

The Global Descriptor Table (GDT) is a relict that was used for memory segmentation before paging became the de facto standard. It is still needed in 64-bit mode for various things such as kernel/user mode configuration or TSS loading.

We already created a GDT when switching to long mode. Back then, we used assembly to create valid code and data segment descriptors, which were required to enter 64-bit mode. We could just edit that assembly file and add an additional TSS descriptor. However, we now have the expressiveness of Rust, so let’s do it in Rust instead.

We start by creating a new interrupts::gdt submodule. For that we need to rename the src/interrupts.rs file to src/interrupts/mod.rs. Then we can create a new submodule:

  1. // in src/interrupts/mod.rs
  2. mod gdt;
  1. // src/interrupts/gdt.rs
  2. pub struct Gdt {
  3. table: [u64; 8],
  4. next_free: usize,
  5. }
  6. impl Gdt {
  7. pub fn new() -> Gdt {
  8. Gdt {
  9. table: [0; 8],
  10. next_free: 1,
  11. }
  12. }
  13. }

We create a simple Gdt struct with two fields. The table field contains the actual GDT modeled as a [u64; 8]. Theoretically, a GDT can have up to 8192 entries, but this doesn’t make much sense in 64-bit mode (since there is no real segmentation support). Eight entries should be more than enough for our system.

The next_free field stores the index of the next free entry. We initialize it with 1 since the 0th entry needs always needs to be 0 in a valid GDT.

User and System Segments

There are two types of GDT entries in long mode: user and system segment descriptors. Descriptors for code and data segment segments are user segment descriptors. They contain no addresses since segments always span the complete address space on x86_64 (real segmentation is no longer supported). Thus, user segment descriptors only contain a few flags (e.g. present or user mode) and fit into a single u64 entry.

System descriptors such as TSS descriptors are different. They often contain a base address and a limit (e.g. TSS start and length) and thus need more than 64 bits. Therefore, system segments are 128 bits. They are stored as two consecutive entries in the GDT.

Consequently, we model a Descriptor as an enum:

  1. // in src/interrupts/gdt.rs
  2. pub enum Descriptor {
  3. UserSegment(u64),
  4. SystemSegment(u64, u64),
  5. }

The flag bits are common between all descriptor types, so we create a general DescriptorFlags type (using the bitflags macro):

  1. // in src/interrupts/gdt.rs
  2. bitflags! {
  3. struct DescriptorFlags: u64 {
  4. const CONFORMING = 1 << 42;
  5. const EXECUTABLE = 1 << 43;
  6. const USER_SEGMENT = 1 << 44;
  7. const PRESENT = 1 << 47;
  8. const LONG_MODE = 1 << 53;
  9. }
  10. }

We only add flags that are relevant in 64-bit mode. For example, we omit the read/write bit, since it is completely ignored by the CPU in 64-bit mode.

Code Segments

We add a function to create kernel mode code segments:

  1. // in src/interrupts/gdt.rs
  2. impl Descriptor {
  3. pub fn kernel_code_segment() -> Descriptor {
  4. let flags = USER_SEGMENT | PRESENT | EXECUTABLE | LONG_MODE;
  5. Descriptor::UserSegment(flags.bits())
  6. }
  7. }

We set the USER_SEGMENT bit to indicate a 64 bit user segment descriptor (otherwise the CPU expects a 128 bit system segment descriptor). The PRESENT, EXECUTABLE, and LONG_MODE bits are also needed for a 64-bit mode code segment.

The data segment registers ds, ss, and es are completely ignored in 64-bit mode, so we don’t need any data segment descriptors in our GDT.

TSS Segments

A TSS descriptor is a system segment descriptor with the following format:

Bit(s) Name Meaning
0-15 limit 0-15 the first 2 byte of the TSS’s limit
16-39 base 0-23 the first 3 byte of the TSS’s base address
40-43 type must be 0b1001 for an available 64-bit TSS
44 zero must be 0
45-46 privilege the ring level: 0 for kernel, 3 for user
47 present must be 1 for valid selectors
48-51 limit 16-19 bits 16 to 19 of the segment’s limit
52 available freely available to the OS
53-54 ignored
55 granularity if it’s set, the limit is the number of pages, else it’s a byte number
56-63 base 24-31 the fourth byte of the base address
64-95 base 32-63 the last four bytes of the base address
96-127 ignored/must be zero bits 104-108 must be zero, the rest is ignored

We only need the bold fields for our TSS descriptor. For example, we don’t need the limit 16-19 field since a TSS has a fixed size that is smaller than 2^16.

Let’s add a function to our descriptor that creates a TSS descriptor for a given TSS:

  1. // in src/interrupts/gdt.rs
  2. use x86_64::structures::tss::TaskStateSegment;
  3. impl Descriptor {
  4. pub fn tss_segment(tss: &'static TaskStateSegment) -> Descriptor {
  5. use core::mem::size_of;
  6. use bit_field::BitField;
  7. let ptr = tss as *const _ as u64;
  8. let mut low = PRESENT.bits();
  9. // base
  10. low.set_bits(16..40, ptr.get_bits(0..24));
  11. low.set_bits(56..64, ptr.get_bits(24..32));
  12. // limit (the `-1` in needed since the bound is inclusive)
  13. low.set_bits(0..16, (size_of::<TaskStateSegment>() - 1) as u64);
  14. // type (0b1001 = available 64-bit tss)
  15. low.set_bits(40..44, 0b1001);
  16. let mut high = 0;
  17. high.set_bits(0..32, ptr.get_bits(32..64));
  18. Descriptor::SystemSegment(low, high)
  19. }
  20. }

The set_bits and get_bits methods are provided by the BitField trait of the bit_fields crate. They allow us to easily get or set specific bits in an integer without using bit masks or shift operations. For example, we can do x.set_bits(8..12, 42) instead of x = (x & 0xfffff0ff) | (42 << 8).

To link the bit_fields crate, we modify our Cargo.toml and our src/lib.rs:

  1. [dependencies]
  2. bit_field = "0.7.0"
  1. extern crate bit_field;

We require the 'static lifetime for the TaskStateSegment reference, since the hardware might access it on every interrupt as long as the OS runs.

Adding Descriptors to the GDT

In order to add descriptors to the GDT, we add a add_entry method:

  1. // in src/interrupts/gdt.rs
  2. use x86_64::structures::gdt::SegmentSelector;
  3. use x86_64::PrivilegeLevel;
  4. impl Gdt {
  5. pub fn add_entry(&mut self, entry: Descriptor) -> SegmentSelector {
  6. let index = match entry {
  7. Descriptor::UserSegment(value) => self.push(value),
  8. Descriptor::SystemSegment(value_low, value_high) => {
  9. let index = self.push(value_low);
  10. self.push(value_high);
  11. index
  12. }
  13. };
  14. SegmentSelector::new(index as u16, PrivilegeLevel::Ring0)
  15. }
  16. }

For an user segment we just push the u64 and remember the index. For a system segment, we push the low and high u64 and use the index of the low value. We then use this index to return a new SegmentSelector.

The push method looks like this:

  1. // in src/interrupts/gdt.rs
  2. impl Gdt {
  3. fn push(&mut self, value: u64) -> usize {
  4. if self.next_free < self.table.len() {
  5. let index = self.next_free;
  6. self.table[index] = value;
  7. self.next_free += 1;
  8. index
  9. } else {
  10. panic!("GDT full");
  11. }
  12. }
  13. }

The method just writes to the next_free entry and returns the corresponding index. If there is no free entry left, we panic since this likely indicates a programming error (we should never need to create more than two or three GDT entries for our kernel).

Loading the GDT

To load the GDT, we add a new load method:

  1. // in src/interrupts/gdt.rs
  2. impl Gdt {
  3. pub fn load(&'static self) {
  4. use x86_64::instructions::tables::{DescriptorTablePointer, lgdt};
  5. use core::mem::size_of;
  6. let ptr = DescriptorTablePointer {
  7. base: self.table.as_ptr() as u64,
  8. limit: (self.table.len() * size_of::<u64>() - 1) as u16,
  9. };
  10. unsafe { lgdt(&ptr) };
  11. }
  12. }

We use the DescriptorTablePointer struct and the lgdt function provided by the x86_64 crate to load our GDT. Again, we require a 'static reference since the GDT possibly needs to live for the rest of the run time.

Putting it together

We now have a double fault stack and are able to create and load a TSS (which contains an IST). So let’s put everything together to catch kernel stack overflows.

We already created a new TSS in our interrupts::init function. Now we can load this TSS by creating a new GDT:

  1. // in src/interrupts/mod.rs
  2. pub fn init(memory_controller: &mut MemoryController) {
  3. let double_fault_stack = memory_controller.alloc_stack(1)
  4. .expect("could not allocate double fault stack");
  5. let mut tss = TaskStateSegment::new();
  6. tss.interrupt_stack_table[DOUBLE_FAULT_IST_INDEX] = VirtualAddress(
  7. double_fault_stack.top());
  8. let mut gdt = gdt::Gdt::new();
  9. let code_selector = gdt.add_entry(gdt::Descriptor::kernel_code_segment());
  10. let tss_selector = gdt.add_entry(gdt::Descriptor::tss_segment(&tss));
  11. gdt.load();
  12. IDT.load();
  13. }

However, when we try to compile it, the following errors occur:

  1. error: `tss` does not live long enough
  2. --> src/interrupts/mod.rs:118:68
  3. |
  4. 118 | let tss_selector = gdt.add_entry(gdt::Descriptor::tss_segment(&tss));
  5. | does not live long enough ^^^
  6. ...
  7. 122 | }
  8. | - borrowed value only lives until here
  9. |
  10. = note: borrowed value must be valid for the static lifetime...
  11. error: `gdt` does not live long enough
  12. --> src/interrupts/mod.rs:119:5
  13. |
  14. 119 | gdt.load();
  15. | ^^^ does not live long enough
  16. ...
  17. 122 | }
  18. | - borrowed value only lives until here
  19. |
  20. = note: borrowed value must be valid for the static lifetime...

The problem is that we require that the TSS and GDT are valid for the rest of the run time (i.e. for the 'static lifetime). But our created tss and gdt live on the stack and are thus destroyed at the end of the init function. So how do we fix this problem?

We could allocate our TSS and GDT on the heap using Box and use into_raw and a bit of unsafe to convert it to &'static references (RFC 1233 was closed unfortunately).

Alternatively, we could store them in a static somehow. The lazy_static macro doesn’t work here, since we need access to the MemoryController for initialization. However, we can use its fundamental building block, the spin::Once type.

spin::Once

Let’s try to solve our problem using spin::Once:

  1. // in src/interrupts/mod.rs
  2. use spin::Once;
  3. static TSS: Once<TaskStateSegment> = Once::new();
  4. static GDT: Once<gdt::Gdt> = Once::new();

The Once type allows us to initialize a static at runtime. It is safe because the only way to access the static value is through the provided methods (call_once, try, and wait). Thus, no value can be read before initialization and the value can only be initialized once.

(The Once was added in spin 0.4, so you’re probably need to update your spin dependency.)

So let’s rewrite our interrupts::init function to use the static TSS and GDT:

  1. pub fn init(memory_controller: &mut MemoryController) {
  2. let double_fault_stack = memory_controller.alloc_stack(1)
  3. .expect("could not allocate double fault stack");
  4. let tss = TSS.call_once(|| {
  5. let mut tss = TaskStateSegment::new();
  6. tss.interrupt_stack_table[DOUBLE_FAULT_IST_INDEX] = VirtualAddress(
  7. double_fault_stack.top());
  8. tss
  9. });
  10. let gdt = GDT.call_once(|| {
  11. let mut gdt = gdt::Gdt::new();
  12. let code_selector = gdt.add_entry(gdt::Descriptor::
  13. kernel_code_segment());
  14. let tss_selector = gdt.add_entry(gdt::Descriptor::tss_segment(&tss));
  15. gdt
  16. });
  17. gdt.load();
  18. IDT.load();
  19. }

Now it should compile again!

The final Steps

We’re almost done. We successfully loaded our new GDT, which contains a TSS descriptor. Now there are just a few steps left:

  1. We changed our GDT, so we should reload the cs, the code segment register. This required since the old segment selector could point a different GDT descriptor now (e.g. a TSS descriptor).
  2. We loaded a GDT that contains a TSS selector, but we still need to tell the CPU that it should use that TSS.
  3. As soon as our TSS is loaded, the CPU has access to a valid interrupt stack table (IST). Then we can tell the CPU that it should use our new double fault stack by modifying our double fault IDT entry.

For the first two steps, we need access to the code_selector and tss_selector variables outside of the closure. We can achieve this by moving the let declarations out of the closure:

  1. // in src/interrupts/mod.rs
  2. pub fn init(memory_controller: &mut MemoryController) {
  3. use x86_64::structures::gdt::SegmentSelector;
  4. use x86_64::instructions::segmentation::set_cs;
  5. use x86_64::instructions::tables::load_tss;
  6. ...
  7. let mut code_selector = SegmentSelector(0);
  8. let mut tss_selector = SegmentSelector(0);
  9. let gdt = GDT.call_once(|| {
  10. let mut gdt = gdt::Gdt::new();
  11. code_selector = gdt.add_entry(gdt::Descriptor::kernel_code_segment());
  12. tss_selector = gdt.add_entry(gdt::Descriptor::tss_segment(&tss));
  13. gdt
  14. });
  15. gdt.load();
  16. unsafe {
  17. // reload code segment register
  18. set_cs(code_selector);
  19. // load TSS
  20. load_tss(tss_selector);
  21. }
  22. IDT.load();
  23. }

We first set the descriptors to empty and then update them from inside the closure (which implicitly borrows them as &mut). Now we’re able to reload the code segment register using set_cs and to load the TSS using load_tss.

Now that we loaded a valid TSS and interrupt stack table, we can set the stack index for our double fault handler in the IDT:

  1. // in src/interrupt/mod.rs
  2. lazy_static! {
  3. static ref IDT: idt::Idt = {
  4. let mut idt = idt::Idt::new();
  5. ...
  6. unsafe {
  7. idt.double_fault.set_handler_fn(double_fault_handler)
  8. .set_stack_index(DOUBLE_FAULT_IST_INDEX as u16);
  9. }
  10. ...
  11. };
  12. }

The set_stack_index method is unsafe because the the caller must ensure that the used index is valid and not already used for another exception.

That’s it! Now the CPU should switch to the double fault stack whenever a double fault occurs. Thus, we are able to catch all double faults, including kernel stack overflows:

QEMU printing `EXCEPTION: DOUBLE FAULT` and a dump of the exception stack frame

From now on we should never see a triple fault again!

What’s next?

Now that we mastered exceptions, it’s time to explore another kind of interrupts: interrupts from external devices such as timers, keyboards, or network controllers. These hardware interrupts are very similar to exceptions, e.g. they are also dispatched through the IDT.

However, unlike exceptions, they don’t arise directly on the CPU. Instead, an interrupt controller aggregates these interrupts and forwards them to CPU depending on their priority. In the next posts we will explore the two interrupt controller variants on x86: the Intel 8259 (“PIC”) and the APIC. This will allow us to react to keyboard and mouse input.