From 20e57717803fc88f31da280a6e19da6ef2fa4674 Mon Sep 17 00:00:00 2001 From: Julien CLEMENT Date: Fri, 3 Dec 2021 20:21:03 +0100 Subject: [PATCH] feat(idt): add simple idt Signed-off-by: Julien CLEMENT --- src/boot/boot.asm | 4 +--- src/interrupts.rs | 25 +++++++++++++++++++++++++ src/lib.rs | 16 ++++++++++++++-- src/serial.rs | 4 ++-- src/vga.rs | 15 +++++++++++---- 5 files changed, 53 insertions(+), 11 deletions(-) create mode 100644 src/interrupts.rs diff --git a/src/boot/boot.asm b/src/boot/boot.asm index a7943c2..4834c91 100644 --- a/src/boot/boot.asm +++ b/src/boot/boot.asm @@ -164,7 +164,7 @@ p2_table: resb 4096 stack_bottom: - resb 4096 + resb 65536 stack_top: @@ -184,7 +184,5 @@ long_mode_start: extern julios_main call julios_main - mov rax, 0x2f592f412f4b2f4f - mov qword [0xb8000], rax .loop: jmp .loop diff --git a/src/interrupts.rs b/src/interrupts.rs new file mode 100644 index 0000000..51b235d --- /dev/null +++ b/src/interrupts.rs @@ -0,0 +1,25 @@ +use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame}; +use crate::println; +use lazy_static::lazy_static; +use crate::vga::{self, Color, ColorCode}; + +lazy_static! { + static ref IDT: InterruptDescriptorTable = { + let mut idt = InterruptDescriptorTable::new(); + idt.breakpoint.set_handler_fn(breakpoint_handler); + idt + }; +} + +pub fn init_idt() { + println!("Load 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); +} diff --git a/src/lib.rs b/src/lib.rs index bed5c30..1c5364d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,21 +1,33 @@ #![no_std] #![no_main] +#![feature(abi_x86_interrupt)] mod serial; mod vga; +mod interrupts; + 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"); + 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"); + x86_64::instructions::interrupts::int3(); panic!("Kernel end of flow"); } diff --git a/src/serial.rs b/src/serial.rs index b71bf84..43cea73 100644 --- a/src/serial.rs +++ b/src/serial.rs @@ -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); } } diff --git a/src/vga.rs b/src/vga.rs index e2c38a4..b5b9d9b 100644 --- a/src/vga.rs +++ b/src/vga.rs @@ -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 {