From 0264a5e4b626eaeb109c3339210f4911edc6a8bc Mon Sep 17 00:00:00 2001 From: greg Date: Thu, 18 Jul 2019 03:01:57 -0700 Subject: [PATCH] GDT, IDT stuff set a separate stack for double-fault handler --- src/gdt.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/interrupts.rs | 7 ++++++- src/main.rs | 18 +++++++++++------- 3 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 src/gdt.rs diff --git a/src/gdt.rs b/src/gdt.rs new file mode 100644 index 0000000..2748534 --- /dev/null +++ b/src/gdt.rs @@ -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); + } +} diff --git a/src/interrupts.rs b/src/interrupts.rs index fa67843..3d70e98 100644 --- a/src/interrupts.rs +++ b/src/interrupts.rs @@ -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 }; } diff --git a/src/main.rs b/src/main.rs index 5febffc..18f9035 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,11 @@ -#![feature(abi_x86_interrupt, asm)] +#![feature(abi_x86_interrupt, asm)] #![no_std] #![no_main] #[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 {} }