From 047a997e3d6e06f6d0a95c473c6f497a88af6b89 Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Thu, 10 Sep 2026 19:18:50 +0800 Subject: refactor(macros): use macro 2.0 --- kernel/src/init/earlyprintk.rs | 46 ++++++++++++++++++------------------------ kernel/src/init/int.rs | 9 +++++---- kernel/src/init/int/idt.rs | 14 ++++++++----- kernel/src/lib.rs | 6 +++++- kernel/src/main.rs | 5 ++++- kernel/src/pm/panic.rs | 2 +- report.typ | 1 + 7 files changed, 45 insertions(+), 38 deletions(-) diff --git a/kernel/src/init/earlyprintk.rs b/kernel/src/init/earlyprintk.rs index 088a4db..ae26a2f 100644 --- a/kernel/src/init/earlyprintk.rs +++ b/kernel/src/init/earlyprintk.rs @@ -3,35 +3,29 @@ use spin::{lazylock::LazyLock, mutex::Mutex}; use crate::io::output::serial::uart_16550::Uart16550; // TODO: use linkme instead -pub static EARLY_PRINTK_SERIAL: LazyLock>> = LazyLock::new(|| { - match unsafe { Uart16550::init([Some(Default::default()), None, None, None]) } { +pub static EARLY_PRINTK_SERIAL: LazyLock>> = LazyLock::new( + || match unsafe { Uart16550::init([Some(Default::default()), None, None, None]) } { Ok(o) => Some(Mutex::new(o)), Err(_) => None, - } -}); + }, +); -#[macro_export] // TODO: configurable via option or arguments // or use a system similar to the drivers(`linkme`) -macro_rules! printk { - ($($arg:tt)*) => ({ - use ::core::fmt::Write; - match &*$crate::init::earlyprintk::EARLY_PRINTK_SERIAL { - // TODO: elim the unwrap - Some(m) => write!(m.lock().coms[0].as_mut().unwrap(), $($arg)*), - // TODO: if needed find a way to return an error here, but for now just ignore it - None => Ok(()), - } - }); -} +pub macro printk($($arg:tt)*) {{ + use ::core::fmt::Write; + match &*$crate::init::earlyprintk::EARLY_PRINTK_SERIAL { + // TODO: elim the unwrap + Some(m) => write!(m.lock().coms[0].as_mut().unwrap(), $($arg)*), + // TODO: if needed find a way to return an error here, but for now just ignore it + None => Ok(()), + } +}} -#[macro_export] -macro_rules! printlnk { - ($($arg:tt)*) => ({ - use ::core::fmt::Write; - match &*$crate::init::earlyprintk::EARLY_PRINTK_SERIAL { - Some(m) => writeln!(m.lock().coms[0].as_mut().unwrap(), $($arg)*), - None => Ok(()), - } - }); -} +pub macro printlnk($($arg:tt)*) {{ + use ::core::fmt::Write; + match &*$crate::init::earlyprintk::EARLY_PRINTK_SERIAL { + Some(m) => writeln!(m.lock().coms[0].as_mut().unwrap(), $($arg)*), + None => Ok(()), + } +}} diff --git a/kernel/src/init/int.rs b/kernel/src/init/int.rs index 59f1e20..97a96a7 100644 --- a/kernel/src/init/int.rs +++ b/kernel/src/init/int.rs @@ -1,6 +1,7 @@ -use spin::LazyLock; -use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame}; - -use crate::printlnk; +use crate::init::int::idt::IDT; pub mod idt; + +pub fn init() { + IDT.load(); +} diff --git a/kernel/src/init/int/idt.rs b/kernel/src/init/int/idt.rs index 89228cf..80d42f8 100644 --- a/kernel/src/init/int/idt.rs +++ b/kernel/src/init/int/idt.rs @@ -1,3 +1,8 @@ +use spin::LazyLock; +use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame}; + +use crate::init::earlyprintk::printlnk; + pub static IDT: LazyLock = LazyLock::new( || { let mut idt = InterruptDescriptorTable::new(); @@ -7,14 +12,13 @@ pub static IDT: LazyLock = LazyLock::new( }, ); -extern "x86-interrupt" fn double_fault_handler(stack_frame: InterruptStackFrame, error_code: u64) -> ! { +extern "x86-interrupt" fn double_fault_handler( + stack_frame: InterruptStackFrame, + error_code: u64, +) -> ! { panic!("EXCEPTION: DOUBLE FAULT\n{stack_frame:#?}\nERROR CODE:{error_code}"); } extern "x86-interrupt" fn breakpoint_handler(stack_frame: InterruptStackFrame) { _ = printlnk!("EXCEPTION: BREAKPOINT\n{stack_frame:#?}"); } - -pub fn init() { - IDT.load(); -} diff --git a/kernel/src/lib.rs b/kernel/src/lib.rs index 614e8cb..7fe050e 100644 --- a/kernel/src/lib.rs +++ b/kernel/src/lib.rs @@ -1,5 +1,9 @@ #![no_std] -#![feature(array_try_from_fn, abi_x86_interrupt)] +#![feature( + array_try_from_fn, + abi_x86_interrupt, + decl_macro +)] pub mod init; pub mod io; diff --git a/kernel/src/main.rs b/kernel/src/main.rs index 6a1cae3..dbf9926 100644 --- a/kernel/src/main.rs +++ b/kernel/src/main.rs @@ -4,7 +4,10 @@ // extern crate alloc; use bootloader_api::{BootInfo, entry_point}; -use kernel::{init, pm::hcf, printlnk}; +use kernel::{ + init::{self, earlyprintk::printlnk}, + pm::hcf, +}; // const CONFIG: BootloaderConfig = BootloaderConfig::new_default(); diff --git a/kernel/src/pm/panic.rs b/kernel/src/pm/panic.rs index 5c24452..0c9e3e1 100644 --- a/kernel/src/pm/panic.rs +++ b/kernel/src/pm/panic.rs @@ -1,6 +1,6 @@ use core::panic::PanicInfo; -use crate::{pm::hcf, printlnk}; +use crate::{init::earlyprintk::printlnk, pm::hcf}; #[allow(dead_code)] #[cfg_attr( diff --git a/report.typ b/report.typ index 52e253f..758f43a 100644 --- a/report.typ +++ b/report.typ @@ -24,6 +24,7 @@ thanks ("https://www.reddit.com/r/cpp/comments/1w6us70/optimizing_a_spinlock/")[ == mm - using Higher Half Direct Map provided by bootloader api crate - custom new type for virtual and physical memory for type safety +https://www.youtube.com/watch?v=votA3_DSsak == panic & backtrace thanks ("https://eli.thegreenplace.net/2011/02/07/how-debuggers-work-part-3-debugging-information")[Eli] teaching basic concepts and ("https://wiki.osdev.org/Stack_Trace")[osdev] for a practical example -- cgit v1.2.3