From 03acf2943efa4842beb57f8f6d6e5892ecdb5713 Mon Sep 17 00:00:00 2001 From: Julien CLEMENT Date: Fri, 3 Dec 2021 22:32:03 +0100 Subject: [PATCH] feat(gdt): add gdt, tss, stack in dedicated section to no overwrite pagination pages Signed-off-by: Julien CLEMENT --- src/boot/boot.asm | 3 ++- src/gdt.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/interrupts.rs | 21 ++++++++++++++++----- src/lib.rs | 12 +++++++++--- 4 files changed, 72 insertions(+), 9 deletions(-) create mode 100644 src/gdt.rs diff --git a/src/boot/boot.asm b/src/boot/boot.asm index 4834c91..05ea5f4 100644 --- a/src/boot/boot.asm +++ b/src/boot/boot.asm @@ -163,8 +163,9 @@ p3_table: p2_table: resb 4096 +section .stack stack_bottom: - resb 65536 + resb 0x800000 stack_top: diff --git a/src/gdt.rs b/src/gdt.rs new file mode 100644 index 0000000..7ab41cb --- /dev/null +++ b/src/gdt.rs @@ -0,0 +1,45 @@ +use crate::println; +use lazy_static::lazy_static; +use x86_64::{VirtAddr, structures::tss::TaskStateSegment}; +use x86_64::structures::gdt::{GlobalDescriptorTable, Descriptor, SegmentSelector}; +use x86_64::instructions::{segmentation::{CS, Segment}, tables::load_tss}; + +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 * 5; + 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 + }; +} + +lazy_static! { + static ref GDT: (GlobalDescriptorTable, Selectors) = { + let mut gdt = GlobalDescriptorTable::new(); + let code_selector = gdt.add_entry(Descriptor::kernel_code_segment()); + let tss_selector = gdt.add_entry(Descriptor::tss_segment(&TSS)); + (gdt, Selectors { code_selector, tss_selector }) + }; +} + +pub fn init_gdt() { + println!("Loading GDT"); + GDT.0.load(); + unsafe { + CS::set_reg(GDT.1.code_selector); + load_tss(GDT.1.tss_selector); + } +} + +struct Selectors { + code_selector: SegmentSelector, + tss_selector: SegmentSelector, +} diff --git a/src/interrupts.rs b/src/interrupts.rs index 51b235d..ee67b4b 100644 --- a/src/interrupts.rs +++ b/src/interrupts.rs @@ -1,25 +1,36 @@ -use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame}; +use crate::gdt; use crate::println; -use lazy_static::lazy_static; use crate::vga::{self, Color, ColorCode}; +use lazy_static::lazy_static; +use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame}; lazy_static! { static ref IDT: InterruptDescriptorTable = { let mut idt = InterruptDescriptorTable::new(); idt.breakpoint.set_handler_fn(breakpoint_handler); + unsafe { + idt.double_fault.set_handler_fn(double_fault_handler) + .set_stack_index(gdt::DOUBLE_FAULT_IST_INDEX); + } idt }; } pub fn init_idt() { - println!("Load IDT"); + println!("Loading IDT"); IDT.load(); } -extern "x86-interrupt" fn breakpoint_handler( - stack_frame: InterruptStackFrame) { +extern "x86-interrupt" fn breakpoint_handler(stack_frame: InterruptStackFrame) { let color: vga::ColorCode = vga::get_color(); vga::change_color(ColorCode::new(Color::Pink, Color::Black)); println!("EXCEPTION: BREAKPOINT\n{:#?}", stack_frame); vga::change_color(color); } + +extern "x86-interrupt" fn double_fault_handler( + stack_frame: InterruptStackFrame, + _error_code: u64, +) -> ! { + panic!("EXCEPTION: DOUBLE FAULT\n{:#?}", stack_frame); +} diff --git a/src/lib.rs b/src/lib.rs index 1c5364d..2bdba0d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,9 +2,10 @@ #![no_main] #![feature(abi_x86_interrupt)] +mod gdt; +mod interrupts; mod serial; mod vga; -mod interrupts; use core::panic::PanicInfo; use vga::{Color, ColorCode}; @@ -19,6 +20,7 @@ fn panic_handler(info: &PanicInfo) -> ! { pub fn init() { vga::change_color(ColorCode::new(Color::LightCyan, Color::Black)); println!("Starting init"); + gdt::init_gdt(); interrupts::init_idt(); vga::change_color(ColorCode::new(Color::LightGreen, Color::Black)); } @@ -27,7 +29,11 @@ pub fn init() { pub extern "C" fn julios_main() -> ! { init(); println!("***JuliOS V0.1.0***"); - serial_println!("Hello serial"); - x86_64::instructions::interrupts::int3(); + fn stack_overflow() { + stack_overflow(); // for each recursion, the return address is pushed + } + + stack_overflow(); + panic!("Kernel end of flow"); }