2021-12-03 21:32:03 +00:00
|
|
|
use crate::println;
|
|
|
|
use lazy_static::lazy_static;
|
2021-12-04 19:03:25 +00:00
|
|
|
use x86_64::instructions::{
|
|
|
|
segmentation::{Segment, CS},
|
|
|
|
tables::load_tss,
|
|
|
|
};
|
|
|
|
use x86_64::structures::gdt::{Descriptor, GlobalDescriptorTable, SegmentSelector};
|
|
|
|
use x86_64::{structures::tss::TaskStateSegment, VirtAddr};
|
2021-12-03 21:32:03 +00:00
|
|
|
|
|
|
|
pub const DOUBLE_FAULT_IST_INDEX: u16 = 0;
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref TSS: TaskStateSegment = {
|
|
|
|
let mut tss = TaskStateSegment::new();
|
|
|
|
tss.interrupt_stack_table[DOUBLE_FAULT_IST_INDEX as usize] = {
|
|
|
|
const STACK_SIZE: usize = 4096 * 5;
|
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref GDT: (GlobalDescriptorTable, Selectors) = {
|
|
|
|
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));
|
2021-12-04 19:03:25 +00:00
|
|
|
(
|
|
|
|
gdt,
|
|
|
|
Selectors {
|
|
|
|
code_selector,
|
|
|
|
tss_selector,
|
|
|
|
},
|
|
|
|
)
|
2021-12-03 21:32:03 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn init_gdt() {
|
|
|
|
println!("Loading GDT");
|
|
|
|
GDT.0.load();
|
|
|
|
unsafe {
|
|
|
|
CS::set_reg(GDT.1.code_selector);
|
|
|
|
load_tss(GDT.1.tss_selector);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Selectors {
|
|
|
|
code_selector: SegmentSelector,
|
|
|
|
tss_selector: SegmentSelector,
|
|
|
|
}
|