GDT, IDT stuff
set a separate stack for double-fault handler
This commit is contained in:
parent
4c0ce08bc8
commit
0264a5e4b6
47
src/gdt.rs
Normal file
47
src/gdt.rs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -2,11 +2,16 @@ use lazy_static::lazy_static;
|
|||||||
use x86_64::structures::idt::{InterruptStackFrame, InterruptDescriptorTable};
|
use x86_64::structures::idt::{InterruptStackFrame, InterruptDescriptorTable};
|
||||||
use crate::println;
|
use crate::println;
|
||||||
|
|
||||||
|
use crate::gdt;
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref IDT: InterruptDescriptorTable = {
|
static ref IDT: InterruptDescriptorTable = {
|
||||||
let mut idt = InterruptDescriptorTable::new();
|
let mut idt = InterruptDescriptorTable::new();
|
||||||
idt.breakpoint.set_handler_fn(breakpoint_handler);
|
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
|
idt
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
16
src/main.rs
16
src/main.rs
@ -5,6 +5,7 @@
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod vga_buffer;
|
mod vga_buffer;
|
||||||
mod interrupts;
|
mod interrupts;
|
||||||
|
mod gdt;
|
||||||
|
|
||||||
use core::panic::PanicInfo;
|
use core::panic::PanicInfo;
|
||||||
|
|
||||||
@ -15,21 +16,24 @@ fn panic(info: &PanicInfo) -> !{
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn init() {
|
pub fn init() {
|
||||||
|
gdt::init();
|
||||||
interrupts::init_idt();
|
interrupts::init_idt();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn _start() -> ! {
|
pub extern "C" fn _start() -> ! {
|
||||||
|
|
||||||
init();
|
init();
|
||||||
//x86_64::instructions::interrupts::int3();
|
|
||||||
|
|
||||||
unsafe {
|
fn yolo() {
|
||||||
*(0xf0f0b8b8 as *mut u64) = 10;
|
yolo();
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("Gamarjoba, munde: {}", 1);
|
yolo();
|
||||||
println!("Gamarjoba, munde: {}", 2);
|
|
||||||
|
|
||||||
|
for i in 1..10 {
|
||||||
|
println!("Gamarjoba, munde: {}", i);
|
||||||
|
}
|
||||||
loop {}
|
loop {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user