feat(gdt): add gdt, tss, stack in dedicated section to no overwrite pagination pages
All checks were successful
continuous-integration/drone/push Build is passing

Signed-off-by: Julien CLEMENT <julien.clement@epita.fr>
This commit is contained in:
Julien CLEMENT 2021-12-03 22:32:03 +01:00
parent 20e5771780
commit 03acf2943e
4 changed files with 72 additions and 9 deletions

@ -163,8 +163,9 @@ p3_table:
p2_table:
resb 4096
section .stack
stack_bottom:
resb 65536
resb 0x800000
stack_top:

45
src/gdt.rs Normal file

@ -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,
}

@ -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);
}

@ -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");
}