From e06509e3809b29b31eeb2e0ba0728c64e3ad3b04 Mon Sep 17 00:00:00 2001 From: Ruihan Li Date: Fri, 18 Apr 2025 12:03:58 +0800 Subject: [PATCH] Make some `unsafe` blocks shorter --- ostd/src/arch/riscv/mod.rs | 8 +++----- ostd/src/arch/x86/mod.rs | 12 ++++-------- ostd/src/boot/smp.rs | 22 +++++++--------------- ostd/src/lib.rs | 5 ++--- 4 files changed, 16 insertions(+), 31 deletions(-) diff --git a/ostd/src/arch/riscv/mod.rs b/ostd/src/arch/riscv/mod.rs index a6477a78..4360b2a9 100644 --- a/ostd/src/arch/riscv/mod.rs +++ b/ostd/src/arch/riscv/mod.rs @@ -23,13 +23,11 @@ pub(crate) fn init_cvm_guest() { } pub(crate) unsafe fn late_init_on_bsp() { - // SAFETY: this function is only called once on BSP. - unsafe { - trap::init(true); - } + // SAFETY: This function is only called once on BSP. + unsafe { trap::init(true) }; irq::init(); - // SAFETY: we're on the BSP and we're ready to boot all APs. + // SAFETY: We're on the BSP and we're ready to boot all APs. unsafe { crate::boot::smp::boot_all_aps() }; timer::init(); diff --git a/ostd/src/arch/x86/mod.rs b/ostd/src/arch/x86/mod.rs index 88412e05..44aeb73e 100644 --- a/ostd/src/arch/x86/mod.rs +++ b/ostd/src/arch/x86/mod.rs @@ -64,10 +64,8 @@ static CPU_FEATURES: Once = Once::new(); /// /// This function must be called only once on the bootstrapping processor. pub(crate) unsafe fn late_init_on_bsp() { - // SAFETY: this function is only called once on BSP. - unsafe { - trap::init(true); - } + // SAFETY: This function is only called once on BSP. + unsafe { trap::init(true) }; irq::init(); kernel::acpi::init(); @@ -87,7 +85,7 @@ pub(crate) unsafe fn late_init_on_bsp() { kernel::tsc::init_tsc_freq(); timer::init_bsp(); - // SAFETY: we're on the BSP and we're ready to boot all APs. + // SAFETY: We're on the BSP and we're ready to boot all APs. unsafe { crate::boot::smp::boot_all_aps() }; if_tdx_enabled!({ @@ -105,9 +103,7 @@ pub(crate) unsafe fn late_init_on_bsp() { // 1. All the system device memory have been removed from the builder. // 2. All the port I/O regions belonging to the system device are defined using the macros. // 3. `MAX_IO_PORT` defined in `crate::arch::io` is the maximum value specified by x86-64. - unsafe { - crate::io::init(io_mem_builder); - } + unsafe { crate::io::init(io_mem_builder) }; } /// Architecture-specific initialization on the application processor. diff --git a/ostd/src/boot/smp.rs b/ostd/src/boot/smp.rs index 22b3d10f..6fec15eb 100644 --- a/ostd/src/boot/smp.rs +++ b/ostd/src/boot/smp.rs @@ -142,29 +142,21 @@ pub fn register_ap_entry(entry: fn()) { #[no_mangle] fn ap_early_entry(cpu_id: u32) -> ! { // SAFETY: `cpu_id` is the correct value of the CPU ID. - unsafe { - cpu::init_on_ap(cpu_id); - } + unsafe { cpu::init_on_ap(cpu_id) }; crate::arch::enable_cpu_features(); - // SAFETY: this function is only called once on this AP. - unsafe { - crate::arch::trap::init(false); - } + // SAFETY: This function is only called once on this AP. + unsafe { crate::arch::trap::init(false) }; - // SAFETY: this function is only called once on this AP, after the BSP has + // SAFETY: This function is only called once on this AP, after the BSP has // done the architecture-specific initialization. - unsafe { - crate::arch::init_on_ap(); - } + unsafe { crate::arch::init_on_ap() }; crate::arch::irq::enable_local(); - // SAFETY: this function is only called once on this AP. - unsafe { - crate::mm::kspace::activate_kernel_page_table(); - } + // SAFETY: This function is only called once on this AP. + unsafe { crate::mm::kspace::activate_kernel_page_table() }; // Mark the AP as started. let ap_boot_info = AP_BOOT_INFO.get().unwrap(); diff --git a/ostd/src/lib.rs b/ostd/src/lib.rs index fd081397..65df0c7d 100644 --- a/ostd/src/lib.rs +++ b/ostd/src/lib.rs @@ -78,9 +78,8 @@ unsafe fn init() { // SAFETY: This function is called only once, before `allocator::init` // and after memory regions are initialized. - unsafe { - mm::frame::allocator::init_early_allocator(); - } + unsafe { mm::frame::allocator::init_early_allocator() }; + #[cfg(target_arch = "x86_64")] arch::if_tdx_enabled!({ } else {