2023-04-26 14:33:40 -05:00
|
|
|
use {
|
|
|
|
spin::Lazy,
|
|
|
|
x86_64::{
|
|
|
|
structures::{
|
|
|
|
gdt::{Descriptor, GlobalDescriptorTable, SegmentSelector},
|
|
|
|
tss::TaskStateSegment,
|
|
|
|
},
|
|
|
|
VirtAddr,
|
2023-03-30 16:43:04 -05:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const DOUBLE_FAULT_IX: u16 = 0;
|
|
|
|
|
|
|
|
pub unsafe fn init() {
|
2023-04-26 14:33:40 -05:00
|
|
|
use x86_64::instructions::{
|
|
|
|
segmentation::{Segment, CS, SS},
|
|
|
|
tables::load_tss,
|
|
|
|
};
|
2023-03-30 16:43:04 -05:00
|
|
|
|
|
|
|
log::info!("Initialising GDT");
|
|
|
|
GDT.0.load();
|
|
|
|
CS::set_reg(GDT.1.kcode);
|
|
|
|
SS::set_reg(GDT.1.kdata);
|
|
|
|
load_tss(GDT.1.tss);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Selectors {
|
|
|
|
kcode: SegmentSelector,
|
|
|
|
kdata: SegmentSelector,
|
2023-04-26 14:33:40 -05:00
|
|
|
tss: SegmentSelector,
|
2023-03-30 16:43:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static TSS: Lazy<TaskStateSegment> = Lazy::new(|| {
|
|
|
|
let mut tss = TaskStateSegment::new();
|
|
|
|
tss.interrupt_stack_table[usize::from(DOUBLE_FAULT_IX)] = {
|
|
|
|
const SIZE: usize = 5 * 1024;
|
|
|
|
let stack = unsafe {
|
|
|
|
alloc::alloc::alloc_zeroed(
|
|
|
|
alloc::alloc::Layout::from_size_align(SIZE, 1).expect("stack pointer"),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
VirtAddr::from_ptr(stack) + SIZE
|
|
|
|
};
|
|
|
|
tss
|
|
|
|
});
|
|
|
|
|
|
|
|
static GDT: Lazy<(GlobalDescriptorTable, Selectors)> = Lazy::new(|| {
|
|
|
|
let mut gdt = GlobalDescriptorTable::new();
|
|
|
|
let sels = Selectors {
|
|
|
|
kcode: gdt.add_entry(Descriptor::kernel_code_segment()),
|
|
|
|
kdata: gdt.add_entry(Descriptor::kernel_data_segment()),
|
2023-04-26 14:33:40 -05:00
|
|
|
tss: gdt.add_entry(Descriptor::tss_segment(&TSS)),
|
2023-03-30 16:43:04 -05:00
|
|
|
};
|
|
|
|
(gdt, sels)
|
|
|
|
});
|