Nothing too fancy, but necessary to configure the APIC.
QJPM62J2AY4RISVQIIKS33OK6QUBUHYILETWZ42CSDA77PN2VU4AC
use lazy_static::lazy_static;
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame};
lazy_static! {
static ref IDT: InterruptDescriptorTable = {
let mut idt = InterruptDescriptorTable::new();
unsafe {
idt.double_fault
.set_handler_fn(double_fault_handler)
.set_stack_index(crate::gdt::DOUBLE_FAULT_INST_INDEX);
}
idt
};
}
pub fn init() {
IDT.load();
}
extern "x86-interrupt" fn double_fault_handler(
stack_frame: InterruptStackFrame,
_error_code: u64,
) -> ! {
panic!("Double fault: {:#?}", stack_frame);
}
use lazy_static::lazy_static;
use x86_64::structures::gdt::{Descriptor, GlobalDescriptorTable, SegmentSelector};
use x86_64::structures::tss::TaskStateSegment;
use x86_64::VirtAddr;
pub const DOUBLE_FAULT_INST_INDEX: u16 = 0;
const STACK_SIZE: usize = 4096 * 5;
lazy_static! {
static ref TSS: TaskStateSegment = {
let mut tss = TaskStateSegment::new();
tss.interrupt_stack_table[DOUBLE_FAULT_INST_INDEX as usize] = {
static mut STACK: [u8; STACK_SIZE] = [0; STACK_SIZE];
let stack_start = VirtAddr::from_ptr(unsafe { &STACK });
let stack_end = stack_start + STACK_SIZE;
stack_end
};
tss
};
static ref GDT_SEGMENTS: SegmentedTable = {
let mut gdt = GlobalDescriptorTable::new();
let code_selector = gdt.add_entry(Descriptor::kernel_code_segment());
let tss_selector = gdt.add_entry(Descriptor::tss_segment(&TSS));
SegmentedTable {
table: gdt,
code_selector,
tss_selector,
}
};
}
struct SegmentedTable {
table: GlobalDescriptorTable,
code_selector: SegmentSelector,
tss_selector: SegmentSelector,
}
pub fn init() {
use x86_64::instructions::segmentation::{Segment, CS};
use x86_64::instructions::tables::load_tss;
GDT_SEGMENTS.table.load();
unsafe {
CS::set_reg(GDT_SEGMENTS.code_selector);
load_tss(GDT_SEGMENTS.tss_selector);
}
}
lazy_static = { version = "1.4.0", features = ["spin_no_std"] }
x86_64 = "0.14.11"
[[package]]
name = "x86_64"
version = "0.14.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b835097a84e4457323331ec5d6eb23d096066cbfb215d54096dcb4b2e85f500"
dependencies = [
"bit_field",
"bitflags 2.4.1",
"rustversion",
"volatile",
]