GDT, IDT stuff

set a separate stack for double-fault handler
This commit is contained in:
greg 2019-07-18 03:01:57 -07:00
parent 4c0ce08bc8
commit 0264a5e4b6
3 changed files with 64 additions and 8 deletions

47
src/gdt.rs Normal file
View File

@ -0,0 +1,47 @@
use x86_64::VirtAddr;
use x86_64::structures::tss::TaskStateSegment;
use x86_64::structures::gdt::{GlobalDescriptorTable, Descriptor, SegmentSelector};
use lazy_static::lazy_static;
//double fault interrupt stack table
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; //4k stack
//this static mut is temporarily the stack until proper allocation can be implemented
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
};
}
struct Selectors {
code_selector: SegmentSelector,
data_selector: SegmentSelector,
}
lazy_static! {
static ref GDT: (GlobalDescriptorTable, Selectors) = {
let mut gdt = GlobalDescriptorTable::new();
let code_selector = gdt.add_entry(Descriptor::kernel_code_segment());
let data_selector = gdt.add_entry(Descriptor::tss_segment(&TSS));
(gdt, Selectors { code_selector, data_selector })
};
}
pub fn init() {
use x86_64::instructions::segmentation::set_cs;
use x86_64::instructions::tables::load_tss;
GDT.0.load();
unsafe {
set_cs(GDT.1.code_selector);
load_tss(GDT.1.data_selector);
}
}

View File

@ -2,11 +2,16 @@ use lazy_static::lazy_static;
use x86_64::structures::idt::{InterruptStackFrame, InterruptDescriptorTable};
use crate::println;
use crate::gdt;
lazy_static! {
static ref IDT: InterruptDescriptorTable = {
let mut idt = InterruptDescriptorTable::new();
idt.breakpoint.set_handler_fn(breakpoint_handler);
idt.double_fault.set_handler_fn(double_fault_handler);
unsafe {
idt.double_fault.set_handler_fn(double_fault_handler)
.set_stack_index(gdt::DOUBLE_FAULT_IST_INDEX);
}
idt
};
}

View File

@ -5,6 +5,7 @@
#[macro_use]
mod vga_buffer;
mod interrupts;
mod gdt;
use core::panic::PanicInfo;
@ -15,21 +16,24 @@ fn panic(info: &PanicInfo) -> !{
}
pub fn init() {
gdt::init();
interrupts::init_idt();
}
#[no_mangle]
pub extern "C" fn _start() -> ! {
init();
//x86_64::instructions::interrupts::int3();
unsafe {
*(0xf0f0b8b8 as *mut u64) = 10;
fn yolo() {
yolo();
}
println!("Gamarjoba, munde: {}", 1);
println!("Gamarjoba, munde: {}", 2);
yolo();
for i in 1..10 {
println!("Gamarjoba, munde: {}", i);
}
loop {}
}