|
|
@ -4,25 +4,33 @@ use x86_64::structures::tss::TaskStateSegment;
|
|
|
|
use x86_64::structures::gdt::SegmentSelector;
|
|
|
|
use x86_64::structures::gdt::SegmentSelector;
|
|
|
|
use x86_64::{PrivilegeLevel, VirtualAddress};
|
|
|
|
use x86_64::{PrivilegeLevel, VirtualAddress};
|
|
|
|
use spin::Once;
|
|
|
|
use spin::Once;
|
|
|
|
|
|
|
|
use alloc::boxed::Box;
|
|
|
|
|
|
|
|
|
|
|
|
static TSS: Once<TaskStateSegment> = Once::new();
|
|
|
|
/// Alloc TSS & GDT at kernel heap, then init and load it.
|
|
|
|
static GDT: Once<Gdt> = Once::new();
|
|
|
|
/// The double fault stack will be allocated at kernel heap too.
|
|
|
|
|
|
|
|
pub fn init() {
|
|
|
|
pub fn init(double_fault_stack_top: usize) {
|
|
|
|
|
|
|
|
use x86_64::structures::gdt::SegmentSelector;
|
|
|
|
use x86_64::structures::gdt::SegmentSelector;
|
|
|
|
use x86_64::instructions::segmentation::set_cs;
|
|
|
|
use x86_64::instructions::segmentation::set_cs;
|
|
|
|
use x86_64::instructions::tables::load_tss;
|
|
|
|
use x86_64::instructions::tables::load_tss;
|
|
|
|
|
|
|
|
|
|
|
|
let tss = TSS.call_once(|| {
|
|
|
|
struct DoubleFaultStack {
|
|
|
|
|
|
|
|
space: [u8; 4096]
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
let double_fault_stack_top = Box::into_raw(Box::new(
|
|
|
|
|
|
|
|
DoubleFaultStack{space: [0; 4096]}
|
|
|
|
|
|
|
|
)) as usize + 4096;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut tss = Box::new({
|
|
|
|
let mut tss = TaskStateSegment::new();
|
|
|
|
let mut tss = TaskStateSegment::new();
|
|
|
|
tss.interrupt_stack_table[DOUBLE_FAULT_IST_INDEX]
|
|
|
|
tss.interrupt_stack_table[DOUBLE_FAULT_IST_INDEX]
|
|
|
|
= VirtualAddress(double_fault_stack_top);
|
|
|
|
= VirtualAddress(double_fault_stack_top);
|
|
|
|
tss
|
|
|
|
tss
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
let tss = unsafe{ &*Box::into_raw(tss) };
|
|
|
|
|
|
|
|
|
|
|
|
let mut code_selector = SegmentSelector(0);
|
|
|
|
let mut code_selector = SegmentSelector(0);
|
|
|
|
let mut tss_selector = SegmentSelector(0);
|
|
|
|
let mut tss_selector = SegmentSelector(0);
|
|
|
|
let gdt = GDT.call_once(|| {
|
|
|
|
let gdt = Box::new({
|
|
|
|
let mut gdt = Gdt::new();
|
|
|
|
let mut gdt = Gdt::new();
|
|
|
|
gdt.add_entry(GNULL);
|
|
|
|
gdt.add_entry(GNULL);
|
|
|
|
code_selector =
|
|
|
|
code_selector =
|
|
|
@ -33,6 +41,7 @@ pub fn init(double_fault_stack_top: usize) {
|
|
|
|
tss_selector = gdt.add_entry(Descriptor::tss_segment(&tss));
|
|
|
|
tss_selector = gdt.add_entry(Descriptor::tss_segment(&tss));
|
|
|
|
gdt
|
|
|
|
gdt
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
let gdt = unsafe{ &*Box::into_raw(gdt) };
|
|
|
|
gdt.load();
|
|
|
|
gdt.load();
|
|
|
|
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
unsafe {
|
|
|
|