From 1bd0c60b93bcc00db66b0321672e0941d8164c2a Mon Sep 17 00:00:00 2001 From: shibedrill Date: Mon, 26 May 2025 00:07:44 -0400 Subject: [PATCH] Critical section macro --- src/kernel/arch/x86_64/gdt.rs | 23 +++++++++-------------- src/kernel/arch/x86_64/interrupts.rs | 4 +--- src/kernel/main.rs | 5 ++--- src/kernel/util.rs | 9 +++++++++ 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/kernel/arch/x86_64/gdt.rs b/src/kernel/arch/x86_64/gdt.rs index 25c2831..68d034a 100644 --- a/src/kernel/arch/x86_64/gdt.rs +++ b/src/kernel/arch/x86_64/gdt.rs @@ -2,10 +2,8 @@ use x86_64::structures::gdt::{Descriptor, GlobalDescriptorTable}; use lazy_static::lazy_static; -use super::asm::*; - use crate::log::{LOGGER, LogLevel}; -use crate::{format, log_trace}; +use crate::{critical_section, format, log_trace}; lazy_static! { pub static ref GDT: GlobalDescriptorTable = { @@ -15,16 +13,13 @@ lazy_static! { }; } +#[allow(dead_code)] pub fn setup_gdt() { - unsafe { - interrupt_disable(); - } - GDT.load(); - log_trace!( - "GDT: Setting up global descriptor table at 0x{:x}", - core::ptr::addr_of!(GDT) as usize - ); - unsafe { - interrupt_enable(); - } + critical_section!({ + GDT.load(); + log_trace!( + "GDT: Setting up global descriptor table at 0x{:x}", + core::ptr::addr_of!(GDT) as usize + ); + }); } diff --git a/src/kernel/arch/x86_64/interrupts.rs b/src/kernel/arch/x86_64/interrupts.rs index 2acf469..26503c2 100644 --- a/src/kernel/arch/x86_64/interrupts.rs +++ b/src/kernel/arch/x86_64/interrupts.rs @@ -4,8 +4,6 @@ use lazy_static::lazy_static; use x86_64::structures::idt::*; -use crate::log_error; -use crate::log::*; use crate::format; lazy_static! { @@ -17,5 +15,5 @@ lazy_static! { } extern "x86-interrupt" fn double_fault(info: InterruptStackFrame, _: u64) -> ! { - crate::interrupt::double_fault(&format!("{:#?}", info)); + crate::interrupt::double_fault(&format!("{info:#?}")); } \ No newline at end of file diff --git a/src/kernel/main.rs b/src/kernel/main.rs index a2b74f1..e6a786a 100644 --- a/src/kernel/main.rs +++ b/src/kernel/main.rs @@ -16,10 +16,9 @@ mod log; mod memory; mod panic; mod process; +#[macro_use] +mod util; -#[cfg(target_arch = "x86_64")] -use arch::x86_64::gdt; -use arch::x86_64::interrupts::IDT; #[cfg(target_arch = "x86_64")] use arch::x86_64::serial::Serialport; diff --git a/src/kernel/util.rs b/src/kernel/util.rs index e69de29..6f43e29 100644 --- a/src/kernel/util.rs +++ b/src/kernel/util.rs @@ -0,0 +1,9 @@ + +#[macro_export] +macro_rules! critical_section { + ($b:block) => { + unsafe { $crate::arch::asm::interrupt_disable(); } + $b + unsafe { $crate::arch::asm::interrupt_enable(); } + }; +} \ No newline at end of file