Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start populating the IDT and handle double faults. #2893

Merged
merged 1 commit into from May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 58 additions & 0 deletions experimental/uefi/kernel/src/interrupts.rs
@@ -0,0 +1,58 @@
//
// Copyright 2022 The Project Oak Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

use crate::i8042;
use core::ops::Deref;
use lazy_static::lazy_static;
use log::error;
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame};

lazy_static! {
static ref IDT: InterruptDescriptorTable = {
let mut idt = InterruptDescriptorTable::new();
idt.double_fault.set_handler_fn(double_fault_handler);
idt
};
}

extern "x86-interrupt" fn double_fault_handler(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is "x86-interrupt"? Is this a type of linker that the Rust compiler already knows about or is it defined somewhere? Could you add a clarifying comment?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's not about linker -- it changes the calling convention of the function and what code the compiler emits. The particular calling convention is required by the x86-64 ABI.

You can read more about the x86-interrupt in particular here: rust-lang/rust#40180

In short, you don't really expect to see that used anywhere else except for interrupt handlers.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a comment with a link to that issue would help us understand what is going on later when we look at the code.

stack_frame: InterruptStackFrame,
_error_code: u64,
) -> ! {
// We're not calling `panic!` here because (a) the panic location would be all wrong (the error
// is not in the fault handler itself as the location would suggest), (b) in theory the
// panic handler itself could cause a double fault, and (c) we want to be sure that we shut
// down the machine after us.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: is "shut down the machine after us" an idiom that I don't know of? :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it just means we want to be sure that the machine is shut down after we have a double fault.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then I think describing it in those words would be clearer :)

// Note that for double fault handlers the error code will always be 0, so there's no point in
// logging that.
error!("KERNEL PANIC: DOUBLE FAULT");
error!(
"Instruction pointer: {:#016x}",
stack_frame.deref().instruction_pointer.as_u64()
);
error!("Code segment: {:#x}", stack_frame.deref().code_segment);
error!(
"Stack pointer: {:#016x}",
stack_frame.deref().stack_pointer.as_u64()
);
error!("Stack segment: {:#x}", stack_frame.deref().stack_segment);
error!("CPU flags: {:#x}", stack_frame.deref().cpu_flags);
i8042::shutdown();
}

pub fn init_idt() {
IDT.load();
}
3 changes: 3 additions & 0 deletions experimental/uefi/kernel/src/lib.rs
Expand Up @@ -29,9 +29,11 @@
//! * Call `start_kernel` from the entry point of the bootloader.

#![no_std]
#![feature(abi_x86_interrupt)]

mod avx;
pub mod i8042;
mod interrupts;
mod libm;
mod logging;
mod memory;
Expand All @@ -45,6 +47,7 @@ use rust_hypervisor_firmware_subset::{boot, paging};
pub fn start_kernel(info: &dyn boot::Info) -> ! {
avx::enable_avx();
logging::init_logging();
interrupts::init_idt();
paging::setup();
memory::init_allocator(info);
main(info);
Expand Down