Compare commits

..

2 Commits

Author SHA1 Message Date
03acf2943e 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>
2021-12-03 22:32:03 +01:00
20e5771780 feat(idt): add simple idt
Signed-off-by: Julien CLEMENT <julien.clement@epita.fr>
2021-12-03 20:21:03 +01:00
6 changed files with 117 additions and 12 deletions

@ -163,8 +163,9 @@ p3_table:
p2_table:
resb 4096
section .stack
stack_bottom:
resb 4096
resb 0x800000
stack_top:
@ -184,7 +185,5 @@ long_mode_start:
extern julios_main
call julios_main
mov rax, 0x2f592f412f4b2f4f
mov qword [0xb8000], rax
.loop:
jmp .loop

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

36
src/interrupts.rs Normal file

@ -0,0 +1,36 @@
use crate::gdt;
use crate::println;
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!("Loading IDT");
IDT.load();
}
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);
}

@ -1,21 +1,39 @@
#![no_std]
#![no_main]
#![feature(abi_x86_interrupt)]
mod gdt;
mod interrupts;
mod serial;
mod vga;
use core::panic::PanicInfo;
use vga::Color;
use vga::{Color, ColorCode};
#[panic_handler]
fn panic_handler(info: &PanicInfo) -> ! {
vga::change_color(Color::LightRed, Color::Black);
vga::change_color(ColorCode::new(Color::LightRed, Color::Black));
println!("{}", info);
loop {}
}
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));
}
#[no_mangle]
pub extern "C" fn julios_main() -> ! {
init();
println!("***JuliOS V0.1.0***");
serial_println!("Hello serial");
fn stack_overflow() {
stack_overflow(); // for each recursion, the return address is pushed
}
stack_overflow();
panic!("Kernel end of flow");
}

@ -16,7 +16,7 @@ const ENABLE_TRANSMITTER: u8 = 0x1 << 1;
const FIFO: u8 = 0x1;
const TRIGGER_LVL_14: u8 = 0x3 << 6;
const CLEAR_TRANSMIT_FIFO: u8 = 0x1 << 2;
const CLEAR_REVEIVE_FIFO: u8 = 0x1 << 1;
const CLEAR_RECEIVE_FIFO: u8 = 0x1 << 1;
const NO_PARITY: u8 = 0x0;
const EIGHT_BITS_LENGTH: u8 = 0x3;
@ -118,7 +118,7 @@ impl SerialPort {
unsafe {
self.line_control.write(NO_PARITY | EIGHT_BITS_LENGTH);
self.fifo_control
.write(FIFO | TRIGGER_LVL_14 | CLEAR_TRANSMIT_FIFO | CLEAR_REVEIVE_FIFO);
.write(FIFO | TRIGGER_LVL_14 | CLEAR_TRANSMIT_FIFO | CLEAR_RECEIVE_FIFO);
self.interrupt_enable.write(ENABLE_TRANSMITTER);
}
}

@ -30,8 +30,11 @@ pub fn _print(args: fmt::Arguments) {
use core::fmt::Write;
WRITER.lock().write_fmt(args).unwrap();
}
pub fn change_color(fg: Color, bg: Color) {
WRITER.lock().change_color(ColorCode::new(fg, bg))
pub fn change_color(color: ColorCode) {
WRITER.lock().change_color(color)
}
pub fn get_color() -> ColorCode {
WRITER.lock().get_color()
}
#[allow(dead_code)]
@ -58,10 +61,10 @@ pub enum Color {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(transparent)]
struct ColorCode(u8);
pub struct ColorCode(u8);
impl ColorCode {
fn new(fg: Color, bg: Color) -> ColorCode {
pub fn new(fg: Color, bg: Color) -> ColorCode {
ColorCode((bg as u8) << 4 | (fg as u8))
}
}
@ -139,6 +142,10 @@ impl Writer {
fn change_color(&mut self, color: ColorCode) {
self.color_code = color;
}
fn get_color(&mut self) -> ColorCode {
self.color_code
}
}
impl fmt::Write for Writer {