diff --git a/.gitignore b/.gitignore index 4684b698..4449bb62 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ build/ .idea/ .vscode/settings.json +.vim/ test/ diff --git a/.rustfmt.toml b/.rustfmt.toml index 1075fd32..60008c1c 100644 --- a/.rustfmt.toml +++ b/.rustfmt.toml @@ -56,8 +56,8 @@ trailing_comma = "Vertical" match_block_trailing_comma = false blank_lines_upper_bound = 1 blank_lines_lower_bound = 0 -edition = "2015" -version = "One" +edition = "2021" +style_edition = "2021" inline_attribute_width = 0 format_generated_files = true generated_marker_line_search_limit = 5 diff --git a/crates/arcref/src/arcref.rs b/crates/arcref/src/arcref.rs index 3d01852d..12674722 100644 --- a/crates/arcref/src/arcref.rs +++ b/crates/arcref/src/arcref.rs @@ -11,7 +11,6 @@ extern crate alloc; #[cfg(all(not(feature = "std"), feature = "alloc"))] use alloc::sync::Arc; - #[cfg(feature = "std")] use std::{ borrow::Borrow, diff --git a/crates/atomic_unique_refcell/src/lib.rs b/crates/atomic_unique_refcell/src/lib.rs index a2476a92..8d0ecbf3 100644 --- a/crates/atomic_unique_refcell/src/lib.rs +++ b/crates/atomic_unique_refcell/src/lib.rs @@ -7,7 +7,6 @@ use core::{ ops::{Deref, DerefMut}, sync::atomic::{AtomicBool, Ordering}, }; - #[cfg(not(feature = "no_std"))] use std::{ cell::UnsafeCell, diff --git a/crates/eonix_hal/eonix_hal_macros/src/lib.rs b/crates/eonix_hal/eonix_hal_macros/src/lib.rs index 8371e587..03085414 100644 --- a/crates/eonix_hal/eonix_hal_macros/src/lib.rs +++ b/crates/eonix_hal/eonix_hal_macros/src/lib.rs @@ -2,7 +2,8 @@ extern crate proc_macro; use proc_macro::TokenStream; use quote::quote; -use syn::{parse, parse_macro_input, spanned::Spanned as _, FnArg, ItemFn}; +use syn::spanned::Spanned as _; +use syn::{parse, parse_macro_input, FnArg, ItemFn}; /// Define the default trap handler. The function should take exactly one argument /// of type `&mut TrapContext`. @@ -16,7 +17,9 @@ use syn::{parse, parse_macro_input, spanned::Spanned as _, FnArg, ItemFn}; /// } /// ``` #[proc_macro_attribute] -pub fn default_trap_handler(attrs: TokenStream, item: TokenStream) -> TokenStream { +pub fn default_trap_handler( + attrs: TokenStream, item: TokenStream, +) -> TokenStream { let item = parse_macro_input!(item as ItemFn); if !attrs.is_empty() { @@ -67,15 +70,21 @@ pub fn main(attrs: TokenStream, item: TokenStream) -> TokenStream { let item = parse_macro_input!(item as ItemFn); if !attrs.is_empty() { - return parse::Error::new(item.span(), "`main` attribute does not take any arguments") - .into_compile_error() - .into(); + return parse::Error::new( + item.span(), + "`main` attribute does not take any arguments", + ) + .into_compile_error() + .into(); } if item.sig.inputs.len() != 1 { - return parse::Error::new(item.span(), "`main` should have exactly one argument.") - .into_compile_error() - .into(); + return parse::Error::new( + item.span(), + "`main` should have exactly one argument.", + ) + .into_compile_error() + .into(); } let arg_ident = match item.sig.inputs.first().unwrap() { @@ -132,9 +141,12 @@ pub fn ap_main(attrs: TokenStream, item: TokenStream) -> TokenStream { } if item.sig.inputs.len() != 1 { - return parse::Error::new(item.span(), "`ap_main` should have exactly one argument.") - .into_compile_error() - .into(); + return parse::Error::new( + item.span(), + "`ap_main` should have exactly one argument.", + ) + .into_compile_error() + .into(); } let arg_ident = match item.sig.inputs.first().unwrap() { diff --git a/crates/eonix_hal/eonix_hal_traits/src/processor.rs b/crates/eonix_hal/eonix_hal_traits/src/processor.rs index 97848dae..3e929059 100644 --- a/crates/eonix_hal/eonix_hal_traits/src/processor.rs +++ b/crates/eonix_hal/eonix_hal_traits/src/processor.rs @@ -1,4 +1,5 @@ -use core::{ops::Deref, pin::Pin}; +use core::ops::Deref; +use core::pin::Pin; pub trait Processor { fn local() -> impl Deref> diff --git a/crates/eonix_hal/src/arch/loongarch64/bootstrap.rs b/crates/eonix_hal/src/arch/loongarch64/bootstrap.rs index f35ede3f..98da228a 100644 --- a/crates/eonix_hal/src/arch/loongarch64/bootstrap.rs +++ b/crates/eonix_hal/src/arch/loongarch64/bootstrap.rs @@ -1,35 +1,25 @@ -use super::cpu::CPUID; -use super::cpu::CPU_COUNT; -use crate::{ - arch::{ - cpu::CPU, - mm::{ArchPhysAccess, PageAttribute64, GLOBAL_PAGE_TABLE}, - trap::CSR_KERNEL_TP, - }, - bootstrap::BootStrapData, - mm::{ - flush_tlb_all, ArchMemory, ArchPagingMode, BasicPageAlloc, BasicPageAllocRef, - ScopedAllocator, - }, -}; -use core::arch::naked_asm; -use core::{ - alloc::Allocator, - arch::asm, - cell::RefCell, - sync::atomic::{AtomicBool, AtomicUsize, Ordering}, -}; +use core::alloc::Allocator; +use core::arch::{asm, naked_asm}; +use core::cell::RefCell; +use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; + use eonix_hal_traits::mm::Memory; -use eonix_mm::{ - address::{Addr as _, PAddr, PRange, PhysAccess, VAddr, VRange}, - page_table::{PageAttribute, PagingMode, PTE as _}, - paging::{Page, PageAccess, PageAlloc, PAGE_SIZE, PFN}, -}; +use eonix_mm::address::{Addr as _, PAddr, PRange, PhysAccess, VAddr, VRange}; +use eonix_mm::page_table::{PageAttribute, PagingMode, PTE as _}; +use eonix_mm::paging::{Page, PageAccess, PageAlloc, PAGE_SIZE, PFN}; use eonix_percpu::PercpuArea; -use loongArch64::register::ecfg; use loongArch64::register::ecfg::LineBasedInterrupt; -use loongArch64::register::tcfg; -use loongArch64::register::{euen, pgdl}; +use loongArch64::register::{ecfg, euen, pgdl, tcfg}; + +use super::cpu::{CPUID, CPU_COUNT}; +use crate::arch::cpu::CPU; +use crate::arch::mm::{ArchPhysAccess, PageAttribute64, GLOBAL_PAGE_TABLE}; +use crate::arch::trap::CSR_KERNEL_TP; +use crate::bootstrap::BootStrapData; +use crate::mm::{ + flush_tlb_all, ArchMemory, ArchPagingMode, BasicPageAlloc, + BasicPageAllocRef, ScopedAllocator, +}; #[unsafe(link_section = ".bootstrap.stack")] static BOOT_STACK: [u8; 4096 * 16] = [0; 4096 * 16]; @@ -179,8 +169,8 @@ pub unsafe extern "C" fn riscv64_start(hart_id: usize) -> ! { } let start = unsafe { - ((&BOOT_STACK_START) as *const &'static [u8; 4096 * 16]).read_volatile() as *const _ - as usize + ((&BOOT_STACK_START) as *const &'static [u8; 4096 * 16]).read_volatile() + as *const _ as usize }; let bootstrap_data = BootStrapData { early_stack: PRange::new( @@ -232,7 +222,11 @@ fn setup_kernel_page_table(alloc: impl PageAlloc) { flush_tlb_all(); unsafe { - core::ptr::write_bytes(KERNEL_BSS_START.addr() as *mut (), 0, BSS_LENGTH as usize); + core::ptr::write_bytes( + KERNEL_BSS_START.addr() as *mut (), + 0, + BSS_LENGTH as usize, + ); } } diff --git a/crates/eonix_hal/src/arch/loongarch64/context.rs b/crates/eonix_hal/src/arch/loongarch64/context.rs index d7405cd8..5ada3bba 100644 --- a/crates/eonix_hal/src/arch/loongarch64/context.rs +++ b/crates/eonix_hal/src/arch/loongarch64/context.rs @@ -1,4 +1,5 @@ use core::arch::naked_asm; + use eonix_hal_traits::context::RawTaskContext; #[repr(C)] diff --git a/crates/eonix_hal/src/arch/loongarch64/cpu.rs b/crates/eonix_hal/src/arch/loongarch64/cpu.rs index 38e84fad..ed0f1295 100644 --- a/crates/eonix_hal/src/arch/loongarch64/cpu.rs +++ b/crates/eonix_hal/src/arch/loongarch64/cpu.rs @@ -1,9 +1,13 @@ -use super::trap::setup_trap; +use core::arch::asm; +use core::pin::Pin; +use core::ptr::NonNull; use core::sync::atomic::AtomicUsize; -use core::{arch::asm, pin::Pin, ptr::NonNull}; + use eonix_preempt::PreemptGuard; use eonix_sync_base::LazyLock; +use super::trap::setup_trap; + pub static CPU_COUNT: AtomicUsize = AtomicUsize::new(0); #[eonix_percpu::define_percpu] diff --git a/crates/eonix_hal/src/arch/loongarch64/fdt.rs b/crates/eonix_hal/src/arch/loongarch64/fdt.rs index 3638b77b..a5fc1fc2 100644 --- a/crates/eonix_hal/src/arch/loongarch64/fdt.rs +++ b/crates/eonix_hal/src/arch/loongarch64/fdt.rs @@ -1,9 +1,11 @@ -use super::mm::ArchPhysAccess; use core::sync::atomic::{AtomicPtr, Ordering}; + use eonix_mm::address::{Addr, PAddr, PRange, PhysAccess}; use eonix_sync_base::LazyLock; use fdt::Fdt; +use super::mm::ArchPhysAccess; + const FDT_PADDR: PAddr = PAddr::from_val(0x100000); pub static FDT: LazyLock> = LazyLock::new(|| unsafe { diff --git a/crates/eonix_hal/src/arch/loongarch64/fence.rs b/crates/eonix_hal/src/arch/loongarch64/fence.rs index cf9f648e..34d77349 100644 --- a/crates/eonix_hal/src/arch/loongarch64/fence.rs +++ b/crates/eonix_hal/src/arch/loongarch64/fence.rs @@ -1,7 +1,5 @@ -use core::{ - arch::asm, - sync::atomic::{compiler_fence, Ordering}, -}; +use core::arch::asm; +use core::sync::atomic::{compiler_fence, Ordering}; #[doc(hidden)] /// Issues a full memory barrier. diff --git a/crates/eonix_hal/src/arch/loongarch64/fpu.rs b/crates/eonix_hal/src/arch/loongarch64/fpu.rs index b1720a0b..767fc8f6 100644 --- a/crates/eonix_hal/src/arch/loongarch64/fpu.rs +++ b/crates/eonix_hal/src/arch/loongarch64/fpu.rs @@ -1,4 +1,5 @@ use core::arch::asm; + use eonix_hal_traits::fpu::RawFpuState; #[repr(C)] diff --git a/crates/eonix_hal/src/arch/loongarch64/interrupt/mod.rs b/crates/eonix_hal/src/arch/loongarch64/interrupt/mod.rs index 8409c4d2..e0c35f52 100644 --- a/crates/eonix_hal/src/arch/loongarch64/interrupt/mod.rs +++ b/crates/eonix_hal/src/arch/loongarch64/interrupt/mod.rs @@ -15,10 +15,11 @@ //! tree advertises for the interrupt controller (`interrupts = <3>`), and //! every vector to CPU 0. -use super::mm::ArchPhysAccess; use eonix_mm::address::{PAddr, PhysAccess}; use loongArch64::iocsr; +use super::mm::ArchPhysAccess; + /// Base address of the PCH-PIC (normal MMIO). const PCH_PIC_BASE: usize = 0x1000_0000; @@ -76,14 +77,16 @@ fn pch_write8(offset: usize, value: u8) { } fn eiointc_enable_vector(vector: usize) { - let offset = EIOINTC_ENABLE + (vector / EIOINTC_GROUP_SIZE) * size_of::(); + let offset = + EIOINTC_ENABLE + (vector / EIOINTC_GROUP_SIZE) * size_of::(); let bit = 1u32 << (vector % EIOINTC_GROUP_SIZE); iocsr::iocsr_write_w(offset, iocsr::iocsr_read_w(offset) | bit); } fn eiointc_clear_vector(vector: usize) { - let offset = EIOINTC_COREISR + (vector / EIOINTC_GROUP_SIZE) * size_of::(); + let offset = + EIOINTC_COREISR + (vector / EIOINTC_GROUP_SIZE) * size_of::(); let bit = 1u32 << (vector % EIOINTC_GROUP_SIZE); // Writing one acknowledges (clears) the pending bit. @@ -105,7 +108,10 @@ pub fn init() { // Route all vectors to CPU 0. COREMAP stores four bytes (one vector // each) per 32-bit register; a byte holds a bitmask of target CPUs. for word in 0..(EIOINTC_VECTORS / 4) { - iocsr::iocsr_write_w(EIOINTC_COREMAP + word * size_of::(), 0x0101_0101); + iocsr::iocsr_write_w( + EIOINTC_COREMAP + word * size_of::(), + 0x0101_0101, + ); } } diff --git a/crates/eonix_hal/src/arch/loongarch64/mm.rs b/crates/eonix_hal/src/arch/loongarch64/mm.rs index d5b00a6b..dc0a0df4 100644 --- a/crates/eonix_hal/src/arch/loongarch64/mm.rs +++ b/crates/eonix_hal/src/arch/loongarch64/mm.rs @@ -1,30 +1,29 @@ -use crate::traits::mm::Memory; -use core::{ - arch::asm, - marker::PhantomData, - ptr::NonNull, - sync::atomic::{compiler_fence, Ordering}, -}; -use eonix_mm::{ - address::{Addr as _, AddrOps, PAddr, PRange, PhysAccess, VAddr}, - page_table::{ - PageAttribute, PageTable, PageTableLevel, PagingMode, RawAttribute, RawPageTable, - TableAttribute, PTE, - }, - paging::{NoAlloc, Page, PageBlock, PAGE_SIZE, PFN}, +use core::arch::asm; +use core::marker::PhantomData; +use core::ptr::NonNull; +use core::sync::atomic::{compiler_fence, Ordering}; + +use eonix_mm::address::{Addr as _, AddrOps, PAddr, PRange, PhysAccess, VAddr}; +use eonix_mm::page_table::{ + PageAttribute, PageTable, PageTableLevel, PagingMode, RawAttribute, + RawPageTable, TableAttribute, PTE, }; +use eonix_mm::paging::{NoAlloc, Page, PageBlock, PAGE_SIZE, PFN}; use eonix_sync_base::LazyLock; use loongArch64::register::pgdl; +use crate::traits::mm::Memory; + pub const KIMAGE_OFFSET: usize = 0xffff_ffff_0000_0000; pub const ROOT_PAGE_TABLE_PFN: usize = 0x8000_1000 >> 12; pub const PAGE_TABLE_BASE: PFN = PFN::from_val(ROOT_PAGE_TABLE_PFN); -pub static GLOBAL_PAGE_TABLE: LazyLock> = - LazyLock::new(|| unsafe { - Page::with_raw(PAGE_TABLE_BASE, |root_table_page| { - PageTable::with_root_table(root_table_page.clone()) - }) - }); +pub static GLOBAL_PAGE_TABLE: LazyLock< + PageTable, +> = LazyLock::new(|| unsafe { + Page::with_raw(PAGE_TABLE_BASE, |root_table_page| { + PageTable::with_root_table(root_table_page.clone()) + }) +}); pub const PA_VP: u64 = ((1 << 0) | (1 << 7)); pub const PA_D: u64 = 1 << 1; @@ -243,8 +242,12 @@ impl PhysAccess for ArchPhysAccess { impl Memory for ArchMemory { fn present_ram() -> impl Iterator { - let range1 = core::iter::once(PRange::from(PAddr::from_val(0)).grow(0x1000_0000)); - let range2 = core::iter::once(PRange::from(PAddr::from_val(0x8000_0000)).grow(0x3000_0000)); + let range1 = core::iter::once( + PRange::from(PAddr::from_val(0)).grow(0x1000_0000), + ); + let range2 = core::iter::once( + PRange::from(PAddr::from_val(0x8000_0000)).grow(0x3000_0000), + ); range2.chain(range1) } @@ -261,7 +264,8 @@ impl Memory for ArchMemory { Self::present_ram() .filter(move |range| { - range.end() <= kernel_start || range.end() > paddr_after_kimage_aligned + range.end() <= kernel_start + || range.end() > paddr_after_kimage_aligned }) .map(move |range| { if range.end() > paddr_after_kimage_aligned diff --git a/crates/eonix_hal/src/arch/loongarch64/trap/mod.rs b/crates/eonix_hal/src/arch/loongarch64/trap/mod.rs index 4a6c4754..d5784766 100644 --- a/crates/eonix_hal/src/arch/loongarch64/trap/mod.rs +++ b/crates/eonix_hal/src/arch/loongarch64/trap/mod.rs @@ -1,21 +1,20 @@ mod trap_context; -use super::context::TaskContext; use core::arch::{asm, global_asm, naked_asm}; use core::cell::UnsafeCell; use core::mem::{offset_of, size_of}; use core::num::NonZero; use core::ptr::NonNull; -use eonix_hal_traits::{ - context::RawTaskContext, - trap::{IrqState as IrqStateTrait, TrapReturn}, -}; + +use eonix_hal_traits::context::RawTaskContext; +use eonix_hal_traits::trap::{IrqState as IrqStateTrait, TrapReturn}; use loongArch64::register::crmd::{self, Crmd}; use loongArch64::register::ecfg; use loongArch64::register::eentry::{self, Eentry}; - pub use trap_context::*; +use super::context::TaskContext; + pub const CSR_KERNEL_TP: usize = 0x30; const CSR_CAPTURED_TRAP_CONTEXT_ADDR: usize = 0x31; const CSR_CAPTURER_TASK_CONTEXT_ADDR: usize = 0x32; diff --git a/crates/eonix_hal/src/arch/loongarch64/trap/trap_context.rs b/crates/eonix_hal/src/arch/loongarch64/trap/trap_context.rs index cc3da77f..6a86d300 100644 --- a/crates/eonix_hal/src/arch/loongarch64/trap/trap_context.rs +++ b/crates/eonix_hal/src/arch/loongarch64/trap/trap_context.rs @@ -1,15 +1,13 @@ -use crate::arch::trap::CSR_KERNEL_TP; -use core::{arch::asm, mem::offset_of}; -use eonix_hal_traits::{ - fault::{Fault, PageFaultErrorCode}, - trap::{RawTrapContext, TrapType}, -}; +use core::arch::asm; +use core::mem::offset_of; + +use eonix_hal_traits::fault::{Fault, PageFaultErrorCode}; +use eonix_hal_traits::trap::{RawTrapContext, TrapType}; use eonix_mm::address::VAddr; -use loongArch64::register::{ - badv, - estat::{Estat, Exception, Interrupt, Trap}, - ticlr, -}; +use loongArch64::register::estat::{Estat, Exception, Interrupt, Trap}; +use loongArch64::register::{badv, ticlr}; + +use crate::arch::trap::CSR_KERNEL_TP; #[repr(C)] #[derive(Default, Clone, Copy)] @@ -114,7 +112,9 @@ impl TrapContext { ] } - fn gen_page_fault(&self, mut err_code: PageFaultErrorCode) -> TrapType { + fn gen_page_fault( + &self, mut err_code: PageFaultErrorCode, + ) -> TrapType { #[inline(always)] fn get_page_fault_address() -> VAddr { VAddr::from(badv::read().vaddr()) @@ -152,7 +152,9 @@ impl RawTrapContext for TrapContext { }, Trap::Interrupt(interrupt) => match interrupt as usize { 2..=9 => TrapType::Irq { - callback: |handler| crate::arch::interrupt::dispatch_irq(handler), + callback: |handler| { + crate::arch::interrupt::dispatch_irq(handler) + }, }, interrupt => TrapType::Fault(Fault::Unknown(interrupt)), }, @@ -164,21 +166,25 @@ impl RawTrapContext for TrapContext { | Exception::PagePrivilegeIllegal, ) => TrapType::Fault(Fault::BadAccess), Trap::Exception(Exception::Breakpoint) => TrapType::Breakpoint, - Trap::Exception(Exception::InstructionNotExist) => TrapType::Fault(Fault::InvalidOp), + Trap::Exception(Exception::InstructionNotExist) => { + TrapType::Fault(Fault::InvalidOp) + } Trap::Exception(Exception::Syscall) => TrapType::Syscall { no: self.syscall_no(), args: self.syscall_args(), }, - Trap::Exception(Exception::LoadPageFault | Exception::PageNonReadableFault) => { - self.gen_page_fault(PageFaultErrorCode::Read) - } - Trap::Exception(Exception::StorePageFault | Exception::PageModifyFault) => { - self.gen_page_fault(PageFaultErrorCode::Write) - } - Trap::Exception(Exception::FetchPageFault | Exception::PageNonExecutableFault) => { - self.gen_page_fault(PageFaultErrorCode::InstructionFetch) + Trap::Exception( + Exception::LoadPageFault | Exception::PageNonReadableFault, + ) => self.gen_page_fault(PageFaultErrorCode::Read), + Trap::Exception( + Exception::StorePageFault | Exception::PageModifyFault, + ) => self.gen_page_fault(PageFaultErrorCode::Write), + Trap::Exception( + Exception::FetchPageFault | Exception::PageNonExecutableFault, + ) => self.gen_page_fault(PageFaultErrorCode::InstructionFetch), + Trap::Exception(exception) => { + TrapType::Fault(Fault::Unknown(exception as usize)) } - Trap::Exception(exception) => TrapType::Fault(Fault::Unknown(exception as usize)), Trap::MachineError(_) | Trap::Unknown => todo!(), } } @@ -236,12 +242,8 @@ impl RawTrapContext for TrapContext { } fn set_user_call_frame( - &mut self, - pc: usize, - sp: Option, - ra: Option, - args: &[usize], - _write_memory: impl Fn(VAddr, &[u8]) -> Result<(), E>, + &mut self, pc: usize, sp: Option, ra: Option, + args: &[usize], _write_memory: impl Fn(VAddr, &[u8]) -> Result<(), E>, ) -> Result<(), E> { self.set_program_counter(pc); @@ -271,7 +273,9 @@ impl RawTrapContext for TrapContext { } impl TrapContext { - fn get_page_fault_error_code(&self, exception: Exception) -> PageFaultErrorCode { + fn get_page_fault_error_code( + &self, exception: Exception, + ) -> PageFaultErrorCode { let mut error_code = PageFaultErrorCode::empty(); match exception { diff --git a/crates/eonix_hal/src/arch/riscv64/console.rs b/crates/eonix_hal/src/arch/riscv64/console.rs index 7880cca0..31c3a9c6 100644 --- a/crates/eonix_hal/src/arch/riscv64/console.rs +++ b/crates/eonix_hal/src/arch/riscv64/console.rs @@ -1,4 +1,4 @@ -use sbi::legacy::{console_putchar, console_getchar}; +use sbi::legacy::{console_getchar, console_putchar}; pub fn getchar() -> Option { let c = console_getchar(); diff --git a/crates/eonix_hal/src/arch/riscv64/cpu.rs b/crates/eonix_hal/src/arch/riscv64/cpu.rs index 3c58580e..4f21d517 100644 --- a/crates/eonix_hal/src/arch/riscv64/cpu.rs +++ b/crates/eonix_hal/src/arch/riscv64/cpu.rs @@ -1,21 +1,21 @@ -use super::{ - interrupt::InterruptControl, - trap::{setup_trap, TrapContext}, -}; -use crate::arch::fdt::{FdtExt, FDT}; -use core::{ - arch::asm, cell::UnsafeCell, mem::MaybeUninit, pin::Pin, ptr::NonNull, - sync::atomic::AtomicUsize, -}; +use core::arch::asm; +use core::cell::UnsafeCell; +use core::mem::MaybeUninit; +use core::pin::Pin; +use core::ptr::NonNull; +use core::sync::atomic::AtomicUsize; + use eonix_hal_traits::trap::RawTrapContext; use eonix_preempt::PreemptGuard; use eonix_sync_base::LazyLock; -use riscv::register::{ - medeleg::{self, Medeleg}, - mhartid, sscratch, sstatus, -}; +use riscv::register::medeleg::{self, Medeleg}; +use riscv::register::{mhartid, sscratch, sstatus}; use sbi::PhysicalAddress; +use super::interrupt::InterruptControl; +use super::trap::{setup_trap, TrapContext}; +use crate::arch::fdt::{FdtExt, FDT}; + pub static CPU_COUNT: AtomicUsize = AtomicUsize::new(0); #[eonix_percpu::define_percpu] diff --git a/crates/eonix_hal/src/arch/riscv64/fence.rs b/crates/eonix_hal/src/arch/riscv64/fence.rs index 08b72f7b..b2819536 100644 --- a/crates/eonix_hal/src/arch/riscv64/fence.rs +++ b/crates/eonix_hal/src/arch/riscv64/fence.rs @@ -1,7 +1,5 @@ -use core::{ - arch::asm, - sync::atomic::{compiler_fence, Ordering}, -}; +use core::arch::asm; +use core::sync::atomic::{compiler_fence, Ordering}; #[doc(hidden)] /// Issues a full memory barrier. diff --git a/crates/eonix_hal/src/arch/riscv64/fpu.rs b/crates/eonix_hal/src/arch/riscv64/fpu.rs index df84b448..53d1a474 100644 --- a/crates/eonix_hal/src/arch/riscv64/fpu.rs +++ b/crates/eonix_hal/src/arch/riscv64/fpu.rs @@ -1,4 +1,5 @@ use core::arch::asm; + use eonix_hal_traits::fpu::RawFpuState; #[repr(C)] diff --git a/crates/eonix_hal/src/arch/riscv64/interrupt/mod.rs b/crates/eonix_hal/src/arch/riscv64/interrupt/mod.rs index f0a95d06..ad3faa73 100644 --- a/crates/eonix_hal/src/arch/riscv64/interrupt/mod.rs +++ b/crates/eonix_hal/src/arch/riscv64/interrupt/mod.rs @@ -1,11 +1,17 @@ -use super::{config::platform::virt::*, fdt::FDT, fence::memory_barrier, mm::ArchPhysAccess}; -use crate::arch::time; -use core::{pin::Pin, ptr::NonNull}; +use core::pin::Pin; +use core::ptr::NonNull; + use eonix_mm::address::{PAddr, PhysAccess}; use eonix_sync_base::LazyLock; use riscv::register::sie; use sbi::SbiError; +use super::config::platform::virt::*; +use super::fdt::FDT; +use super::fence::memory_barrier; +use super::mm::ArchPhysAccess; +use crate::arch::time; + const PRIORITY_OFFSET: usize = 0x0; const PENDING_OFFSET: usize = 0x1000; @@ -44,10 +50,14 @@ impl PLIC { fn new(cpuid: usize) -> Self { let base = *PLIC_BASE.get(); - let enable = PAddr::from(base + (cpuid * 2 + 1) * ENABLE_STRIDE + ENABLE_OFFSET); - let threshold = PAddr::from(base + (cpuid * 2 + 1) * CONTEXT_STRIDE + THRESHOLD_OFFSET); - let claim_complete = - PAddr::from(base + (cpuid * 2 + 1) * CONTEXT_STRIDE + CLAIM_COMPLETE_OFFSET); + let enable = + PAddr::from(base + (cpuid * 2 + 1) * ENABLE_STRIDE + ENABLE_OFFSET); + let threshold = PAddr::from( + base + (cpuid * 2 + 1) * CONTEXT_STRIDE + THRESHOLD_OFFSET, + ); + let claim_complete = PAddr::from( + base + (cpuid * 2 + 1) * CONTEXT_STRIDE + CLAIM_COMPLETE_OFFSET, + ); unsafe { // SAFETY: The PLIC registers are memory-mapped and placed at specific addresses. @@ -71,7 +81,9 @@ impl PLIC { // SAFETY: The PLIC priority register is memory-mapped and placed at a specific address. // We are pretty sure that the address is valid. ArchPhysAccess::as_ptr( - *PLIC_BASE.get() + PRIORITY_OFFSET + interrupt * size_of::(), + *PLIC_BASE.get() + + PRIORITY_OFFSET + + interrupt * size_of::(), ) }; diff --git a/crates/eonix_hal/src/arch/riscv64/time.rs b/crates/eonix_hal/src/arch/riscv64/time.rs index 482c1551..7e484532 100644 --- a/crates/eonix_hal/src/arch/riscv64/time.rs +++ b/crates/eonix_hal/src/arch/riscv64/time.rs @@ -1,15 +1,16 @@ use riscv::register::time; -use super::config::{ - platform::virt::CPU_FREQ_HZ, - time::INTERRUPTS_PER_SECOND -}; + +use super::config::platform::virt::CPU_FREQ_HZ; +use super::config::time::INTERRUPTS_PER_SECOND; pub fn get_time() -> usize { time::read() } pub fn set_timer(times: usize) { - let time = (get_time() + times * CPU_FREQ_HZ as usize / INTERRUPTS_PER_SECOND) as u64; + let time = (get_time() + + times * CPU_FREQ_HZ as usize / INTERRUPTS_PER_SECOND) + as u64; sbi::legacy::set_timer(time); } diff --git a/crates/eonix_hal/src/arch/riscv64/trap/default.rs b/crates/eonix_hal/src/arch/riscv64/trap/default.rs index 4025b719..faa7ae5a 100644 --- a/crates/eonix_hal/src/arch/riscv64/trap/default.rs +++ b/crates/eonix_hal/src/arch/riscv64/trap/default.rs @@ -1,6 +1,7 @@ +use core::arch::naked_asm; + use super::Registers; use crate::trap::TrapContext; -use core::arch::naked_asm; unsafe extern "C" { fn _default_trap_handler(trap_context: &mut TrapContext); diff --git a/crates/eonix_hal/src/arch/x86_64/fence.rs b/crates/eonix_hal/src/arch/x86_64/fence.rs index 1f5b46a3..fb376dc8 100644 --- a/crates/eonix_hal/src/arch/x86_64/fence.rs +++ b/crates/eonix_hal/src/arch/x86_64/fence.rs @@ -1,7 +1,5 @@ -use core::{ - arch::asm, - sync::atomic::{compiler_fence, Ordering}, -}; +use core::arch::asm; +use core::sync::atomic::{compiler_fence, Ordering}; #[doc(hidden)] /// Issues a full memory barrier. diff --git a/crates/eonix_hal/src/arch/x86_64/fpu.rs b/crates/eonix_hal/src/arch/x86_64/fpu.rs index a0100475..cc5c57bc 100644 --- a/crates/eonix_hal/src/arch/x86_64/fpu.rs +++ b/crates/eonix_hal/src/arch/x86_64/fpu.rs @@ -1,4 +1,5 @@ use core::arch::asm; + use eonix_hal_traits::fpu::RawFpuState; #[derive(Clone, Copy)] diff --git a/crates/eonix_hal/src/bootstrap.rs b/crates/eonix_hal/src/bootstrap.rs index cfc9975a..c38b6079 100644 --- a/crates/eonix_hal/src/bootstrap.rs +++ b/crates/eonix_hal/src/bootstrap.rs @@ -1,7 +1,9 @@ -use crate::mm::{BasicPageAlloc, BasicPageAllocRef}; use core::cell::RefCell; + use eonix_mm::address::PRange; +use crate::mm::{BasicPageAlloc, BasicPageAllocRef}; + pub struct BootStrapData { pub(crate) early_stack: PRange, pub(crate) allocator: Option>, diff --git a/crates/eonix_hal/src/mm.rs b/crates/eonix_hal/src/mm.rs index ef006cb5..66a09386 100644 --- a/crates/eonix_hal/src/mm.rs +++ b/crates/eonix_hal/src/mm.rs @@ -148,8 +148,7 @@ impl<'a> ScopedAllocator<'a> { } pub fn with_alloc<'b, 'r, O>( - &'r self, - func: impl FnOnce(&'b ScopedAllocator<'a>) -> O, + &'r self, func: impl FnOnce(&'b ScopedAllocator<'a>) -> O, ) -> O where 'a: 'b, diff --git a/crates/eonix_hal/src/trap.rs b/crates/eonix_hal/src/trap.rs index 11bb9626..b8e53a34 100644 --- a/crates/eonix_hal/src/trap.rs +++ b/crates/eonix_hal/src/trap.rs @@ -1,5 +1,7 @@ use eonix_hal_traits::trap::IsRawTrapContext; -pub use crate::arch::trap::{disable_irqs, disable_irqs_save, enable_irqs, IrqState, TrapContext}; +pub use crate::arch::trap::{ + disable_irqs, disable_irqs_save, enable_irqs, IrqState, TrapContext, +}; struct _CheckTrapContext(IsRawTrapContext); diff --git a/crates/eonix_mm/src/address/addr.rs b/crates/eonix_mm/src/address/addr.rs index 4de1ce1b..51d5683e 100644 --- a/crates/eonix_mm/src/address/addr.rs +++ b/crates/eonix_mm/src/address/addr.rs @@ -1,6 +1,7 @@ -use crate::paging::PAGE_SIZE; use core::ops::{Add, Sub}; +use crate::paging::PAGE_SIZE; + #[doc(notable_trait)] pub trait Addr: Sized diff --git a/crates/eonix_mm/src/address/addr_range.rs b/crates/eonix_mm/src/address/addr_range.rs index c2ef5167..2167fc0a 100644 --- a/crates/eonix_mm/src/address/addr_range.rs +++ b/crates/eonix_mm/src/address/addr_range.rs @@ -1,5 +1,8 @@ +use core::cmp::Ordering; +use core::fmt; +use core::ops::RangeBounds; + use super::addr::Addr; -use core::{cmp::Ordering, fmt, ops::RangeBounds}; #[derive(Clone, Copy)] /// A range of addresses. @@ -153,7 +156,9 @@ impl AddrRange { ) } - pub fn mask_with_checked(&self, mask: &Self) -> Option<(Option, Self, Option)> { + pub fn mask_with_checked( + &self, mask: &Self, + ) -> Option<(Option, Self, Option)> { if mask.len() == 0 || !self.overlap_with(mask) { return None; } diff --git a/crates/eonix_mm/src/address/vaddr.rs b/crates/eonix_mm/src/address/vaddr.rs index 5d8796ca..b9af6baa 100644 --- a/crates/eonix_mm/src/address/vaddr.rs +++ b/crates/eonix_mm/src/address/vaddr.rs @@ -1,8 +1,7 @@ +use core::fmt; +use core::ops::{Add, Sub}; + use super::addr::Addr; -use core::{ - fmt, - ops::{Add, Sub}, -}; #[repr(transparent)] #[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq)] diff --git a/crates/eonix_mm/src/page_table/page_table.rs b/crates/eonix_mm/src/page_table/page_table.rs index 80be63b9..15902bf5 100644 --- a/crates/eonix_mm/src/page_table/page_table.rs +++ b/crates/eonix_mm/src/page_table/page_table.rs @@ -74,7 +74,8 @@ where { let new_root_table_page = alloc.alloc(); let new_table_data = self.access.get_ptr_for_page(&new_root_table_page); - let kernel_table_data = self.access.get_ptr_for_page(&self.root_table_page); + let kernel_table_data = + self.access.get_ptr_for_page(&self.root_table_page); unsafe { // SAFETY: `new_table_data` and `kernel_table_data` are both valid pointers @@ -101,8 +102,11 @@ where self.root_table_page.start() } - pub fn iter_user(&self, range: VRange) -> impl Iterator { - let page_table_ptr = self.access.get_ptr_for_page(&self.root_table_page); + pub fn iter_user( + &self, range: VRange, + ) -> impl Iterator { + let page_table_ptr = + self.access.get_ptr_for_page(&self.root_table_page); let root_page_table = unsafe { // SAFETY: `page_table_ptr` is a valid pointer to a page table. M::RawTable::from_ptr(page_table_ptr) @@ -130,8 +134,11 @@ where /// // Process each entry /// } /// ``` - pub fn iter_kernel(&self, range: VRange) -> impl Iterator { - let page_table_ptr = self.access.get_ptr_for_page(&self.root_table_page); + pub fn iter_kernel( + &self, range: VRange, + ) -> impl Iterator { + let page_table_ptr = + self.access.get_ptr_for_page(&self.root_table_page); let root_page_table = unsafe { // SAFETY: `page_table_ptr` is a valid pointer to a page table. M::RawTable::from_ptr(page_table_ptr) @@ -147,7 +154,9 @@ where ) } - fn drop_page_table_recursive(&self, page_table: &A::Folio, levels: &[PageTableLevel]) { + fn drop_page_table_recursive( + &self, page_table: &A::Folio, levels: &[PageTableLevel], + ) { let [level, remaining_levels @ ..] = levels else { return }; if remaining_levels.is_empty() { // We reached the last level, no need to go deeper. diff --git a/crates/eonix_mm/src/page_table/pte.rs b/crates/eonix_mm/src/page_table/pte.rs index c14d5738..e533b95b 100644 --- a/crates/eonix_mm/src/page_table/pte.rs +++ b/crates/eonix_mm/src/page_table/pte.rs @@ -29,7 +29,9 @@ bitflags! { } #[doc(notable_trait)] -pub trait RawAttribute: Copy + From + From { +pub trait RawAttribute: + Copy + From + From +{ /// Create a new attribute representing a non-present page. fn null() -> Self; diff --git a/crates/eonix_mm/src/page_table/pte_iterator.rs b/crates/eonix_mm/src/page_table/pte_iterator.rs index a9e4ff46..8d86827d 100644 --- a/crates/eonix_mm/src/page_table/pte_iterator.rs +++ b/crates/eonix_mm/src/page_table/pte_iterator.rs @@ -48,7 +48,8 @@ where // until `levels_len - 1`. self.tables.get_disjoint_unchecked_mut([idx, idx + 1]) }; - let parent_table = parent_table.as_mut().expect("Parent table is None"); + let parent_table = + parent_table.as_mut().expect("Parent table is None"); let next_pte = parent_table.index_mut(pt_idx); child_table.replace({ @@ -87,21 +88,22 @@ where } pub fn new( - page_table: M::RawTable<'a>, - range: VRange, - fill_entry_attr: TableAttribute, - alloc: A, - access: X, + page_table: M::RawTable<'a>, range: VRange, + fill_entry_attr: TableAttribute, alloc: A, access: X, ) -> Self { - Self::with_levels(page_table, range, fill_entry_attr, alloc, access, M::LEVELS) + Self::with_levels( + page_table, + range, + fill_entry_attr, + alloc, + access, + M::LEVELS, + ) } pub fn with_levels( - page_table: M::RawTable<'a>, - range: VRange, - fill_entry_attr: TableAttribute, - alloc: A, - access: X, + page_table: M::RawTable<'a>, range: VRange, + fill_entry_attr: TableAttribute, alloc: A, access: X, levels: &'static [PageTableLevel], ) -> Self { let start = range.start().floor(); diff --git a/crates/eonix_mm/src/paging.rs b/crates/eonix_mm/src/paging.rs index f0166cf3..5a08caa0 100644 --- a/crates/eonix_mm/src/paging.rs +++ b/crates/eonix_mm/src/paging.rs @@ -5,7 +5,9 @@ mod pfn; mod zone; pub use list::{FolioList, FolioListSized}; -pub use page::{BasicFolio, Folio, PageAccess, PageBlock, PAGE_SIZE, PAGE_SIZE_BITS}; +pub use page::{ + BasicFolio, Folio, PageAccess, PageBlock, PAGE_SIZE, PAGE_SIZE_BITS, +}; pub use page_alloc::{FrameAlloc, GlobalFrameAlloc}; pub use pfn::PFN; pub use zone::Zone; diff --git a/crates/eonix_mm/src/paging/pfn.rs b/crates/eonix_mm/src/paging/pfn.rs index d90971a6..12cfaf60 100644 --- a/crates/eonix_mm/src/paging/pfn.rs +++ b/crates/eonix_mm/src/paging/pfn.rs @@ -1,10 +1,8 @@ -use crate::address::{Addr as _, PAddr}; -use core::{ - fmt, - ops::{Add, Sub}, -}; +use core::fmt; +use core::ops::{Add, Sub}; use super::PAGE_SIZE_BITS; +use crate::address::{Addr as _, PAddr}; #[repr(transparent)] #[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq)] diff --git a/crates/eonix_percpu/eonix_percpu_macros/src/lib.rs b/crates/eonix_percpu/eonix_percpu_macros/src/lib.rs index 1c5a1b96..7857fa7d 100644 --- a/crates/eonix_percpu/eonix_percpu_macros/src/lib.rs +++ b/crates/eonix_percpu/eonix_percpu_macros/src/lib.rs @@ -9,8 +9,7 @@ use quote::{format_ident, quote}; use syn::{parse2, Ident, ItemStatic, Type}; fn define_percpu_impl( - attrs: TokenStream, - item: TokenStream, + attrs: TokenStream, item: TokenStream, get_percpu_pointer: fn(&Ident, &Type) -> TokenStream, ) -> TokenStream { if !attrs.is_empty() { @@ -24,10 +23,11 @@ fn define_percpu_impl( let expr = &item.expr; let is_bool = quote!(#ty).to_string().as_str() == "bool"; - let is_integer = - ["u8", "u16", "u32", "u64", "usize"].contains("e!(#ty).to_string().as_str()); + let is_integer = ["u8", "u16", "u32", "u64", "usize"] + .contains("e!(#ty).to_string().as_str()); - let is_atomic_like = is_bool || is_integer || quote!(#ty).to_string().contains("NonNull"); + let is_atomic_like = + is_bool || is_integer || quote!(#ty).to_string().contains("NonNull"); let inner_ident = format_ident!("_percpu_inner_{}", ident); let access_ident = format_ident!("_access_{}", ident); @@ -122,8 +122,7 @@ fn define_percpu_impl( } fn define_percpu_shared_impl( - attrs: TokenStream, - item: TokenStream, + attrs: TokenStream, item: TokenStream, get_percpu_pointer: fn(&Ident, &Type) -> TokenStream, get_percpu_offset: fn(&Ident) -> TokenStream, ) -> TokenStream { @@ -191,16 +190,15 @@ fn define_percpu_shared_impl( #[proc_macro_attribute] pub fn define_percpu_x86_64( - attrs: proc_macro::TokenStream, - item: proc_macro::TokenStream, + attrs: proc_macro::TokenStream, item: proc_macro::TokenStream, ) -> proc_macro::TokenStream { - define_percpu_impl(attrs.into(), item.into(), x86_64::get_percpu_pointer).into() + define_percpu_impl(attrs.into(), item.into(), x86_64::get_percpu_pointer) + .into() } #[proc_macro_attribute] pub fn define_percpu_shared_x86_64( - attrs: proc_macro::TokenStream, - item: proc_macro::TokenStream, + attrs: proc_macro::TokenStream, item: proc_macro::TokenStream, ) -> proc_macro::TokenStream { define_percpu_shared_impl( attrs.into(), @@ -213,16 +211,15 @@ pub fn define_percpu_shared_x86_64( #[proc_macro_attribute] pub fn define_percpu_riscv64( - attrs: proc_macro::TokenStream, - item: proc_macro::TokenStream, + attrs: proc_macro::TokenStream, item: proc_macro::TokenStream, ) -> proc_macro::TokenStream { - define_percpu_impl(attrs.into(), item.into(), riscv64::get_percpu_pointer).into() + define_percpu_impl(attrs.into(), item.into(), riscv64::get_percpu_pointer) + .into() } #[proc_macro_attribute] pub fn define_percpu_shared_riscv64( - attrs: proc_macro::TokenStream, - item: proc_macro::TokenStream, + attrs: proc_macro::TokenStream, item: proc_macro::TokenStream, ) -> proc_macro::TokenStream { define_percpu_shared_impl( attrs.into(), @@ -235,16 +232,19 @@ pub fn define_percpu_shared_riscv64( #[proc_macro_attribute] pub fn define_percpu_loongarch64( - attrs: proc_macro::TokenStream, - item: proc_macro::TokenStream, + attrs: proc_macro::TokenStream, item: proc_macro::TokenStream, ) -> proc_macro::TokenStream { - define_percpu_impl(attrs.into(), item.into(), loongarch64::get_percpu_pointer).into() + define_percpu_impl( + attrs.into(), + item.into(), + loongarch64::get_percpu_pointer, + ) + .into() } #[proc_macro_attribute] pub fn define_percpu_shared_loongarch64( - attrs: proc_macro::TokenStream, - item: proc_macro::TokenStream, + attrs: proc_macro::TokenStream, item: proc_macro::TokenStream, ) -> proc_macro::TokenStream { define_percpu_shared_impl( attrs.into(), diff --git a/crates/eonix_percpu/src/lib.rs b/crates/eonix_percpu/src/lib.rs index a00b5c05..706474fe 100644 --- a/crates/eonix_percpu/src/lib.rs +++ b/crates/eonix_percpu/src/lib.rs @@ -50,7 +50,8 @@ impl PercpuArea { } fn layout() -> Layout { - Layout::from_size_align(Self::len(), align_of::()).expect("Invalid layout.") + Layout::from_size_align(Self::len(), align_of::()) + .expect("Invalid layout.") } pub fn new(allocate: F) -> Self @@ -61,7 +62,8 @@ impl PercpuArea { unsafe { // SAFETY: The `data_pointer` is of valid length and properly aligned. - data_pointer.copy_from_nonoverlapping(Self::data_start(), Self::len()); + data_pointer + .copy_from_nonoverlapping(Self::data_start(), Self::len()); } Self { diff --git a/crates/eonix_preempt/src/lib.rs b/crates/eonix_preempt/src/lib.rs index 3d7cd347..9028dc42 100644 --- a/crates/eonix_preempt/src/lib.rs +++ b/crates/eonix_preempt/src/lib.rs @@ -1,9 +1,7 @@ #![no_std] -use core::{ - ops::{Deref, DerefMut}, - sync::atomic::{compiler_fence, Ordering}, -}; +use core::ops::{Deref, DerefMut}; +use core::sync::atomic::{compiler_fence, Ordering}; pub struct PreemptGuard where diff --git a/crates/eonix_runtime/src/ready_queue.rs b/crates/eonix_runtime/src/ready_queue.rs index bc93a172..fa1d3e96 100644 --- a/crates/eonix_runtime/src/ready_queue.rs +++ b/crates/eonix_runtime/src/ready_queue.rs @@ -1,7 +1,10 @@ -use crate::task::Task; -use alloc::{collections::VecDeque, sync::Arc}; +use alloc::collections::VecDeque; +use alloc::sync::Arc; + use eonix_sync::Spin; +use crate::task::Task; + #[eonix_percpu::define_percpu_shared] static READYQUEUE: Spin = Spin::new(FifoReadyQueue::new()); diff --git a/crates/eonix_runtime/src/task.rs b/crates/eonix_runtime/src/task.rs index 8a1d6ef4..dcf3f1cf 100644 --- a/crates/eonix_runtime/src/task.rs +++ b/crates/eonix_runtime/src/task.rs @@ -1,24 +1,22 @@ mod adapter; mod task_state; -use crate::{ - executor::{Executor, OutputHandle}, - ready_queue::{cpu_rq, ReadyQueue}, -}; -use alloc::{sync::Arc, task::Wake}; +use alloc::sync::Arc; +use alloc::task::Wake; +use core::ops::DerefMut; +use core::sync::atomic::{AtomicU32, Ordering}; +use core::task::{Context, Poll, Waker}; + +pub use adapter::{TaskAdapter, TaskRqAdapter}; use atomic_unique_refcell::AtomicUniqueRefCell; -use core::{ - ops::DerefMut, - sync::atomic::{AtomicU32, Ordering}, - task::{Context, Poll, Waker}, -}; use eonix_hal::processor::CPU; use eonix_sync::{Spin, SpinIrq}; use intrusive_collections::{LinkedListAtomicLink, RBTreeAtomicLink}; - -pub use adapter::{TaskAdapter, TaskRqAdapter}; pub(crate) use task_state::TaskState; +use crate::executor::{Executor, OutputHandle}; +use crate::ready_queue::{cpu_rq, ReadyQueue}; + #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub struct TaskId(u32); diff --git a/crates/eonix_runtime/src/task/adapter.rs b/crates/eonix_runtime/src/task/adapter.rs index 3b5d1583..b5f771d6 100644 --- a/crates/eonix_runtime/src/task/adapter.rs +++ b/crates/eonix_runtime/src/task/adapter.rs @@ -1,6 +1,10 @@ -use super::{Task, TaskId}; use alloc::sync::Arc; -use intrusive_collections::{intrusive_adapter, KeyAdapter, LinkedListAtomicLink, RBTreeAtomicLink}; + +use intrusive_collections::{ + intrusive_adapter, KeyAdapter, LinkedListAtomicLink, RBTreeAtomicLink, +}; + +use super::{Task, TaskId}; intrusive_adapter!(pub TaskAdapter = Arc: Task { link_task_list: RBTreeAtomicLink }); intrusive_adapter!(pub TaskRqAdapter = Arc: Task { link_ready_queue: LinkedListAtomicLink }); diff --git a/crates/eonix_runtime/src/task/task_state.rs b/crates/eonix_runtime/src/task/task_state.rs index 473310d7..822b5ea8 100644 --- a/crates/eonix_runtime/src/task/task_state.rs +++ b/crates/eonix_runtime/src/task/task_state.rs @@ -18,7 +18,9 @@ impl TaskState { self.0.swap(state, Ordering::SeqCst) } - pub(crate) fn update(&self, func: impl FnMut(u32) -> Option) -> Result { + pub(crate) fn update( + &self, func: impl FnMut(u32) -> Option, + ) -> Result { self.0 .fetch_update(Ordering::SeqCst, Ordering::SeqCst, func) } diff --git a/crates/eonix_sync/eonix_spin/src/guard.rs b/crates/eonix_sync/eonix_spin/src/guard.rs index 9d26d28e..c5cf592d 100644 --- a/crates/eonix_sync/eonix_spin/src/guard.rs +++ b/crates/eonix_sync/eonix_spin/src/guard.rs @@ -1,12 +1,13 @@ +use core::marker::PhantomData; +use core::mem::ManuallyDrop; +use core::ops::{Deref, DerefMut}; + +use eonix_sync_base::{NotSend, UnlockableGuard, UnlockedGuard}; + use super::{ - ContextUnlock, DisablePreemption, Relax, Spin, SpinContext, SpinRelax, UnlockedContext, + ContextUnlock, DisablePreemption, Relax, Spin, SpinContext, SpinRelax, + UnlockedContext, }; -use core::{ - marker::PhantomData, - mem::ManuallyDrop, - ops::{Deref, DerefMut}, -}; -use eonix_sync_base::{NotSend, UnlockableGuard, UnlockedGuard}; pub struct SpinGuard<'a, T, C = DisablePreemption, R = SpinRelax> where @@ -40,7 +41,9 @@ where T: ?Sized, C: SpinContext, { - pub(super) fn new(lock: &'a Spin, value: &'a mut T, context: C) -> Self { + pub(super) fn new( + lock: &'a Spin, value: &'a mut T, context: C, + ) -> Self { Self { lock, value, diff --git a/crates/eonix_sync/eonix_spin/src/lib.rs b/crates/eonix_sync/eonix_spin/src/lib.rs index 7225aceb..000d0de1 100644 --- a/crates/eonix_sync/eonix_spin/src/lib.rs +++ b/crates/eonix_sync/eonix_spin/src/lib.rs @@ -108,10 +108,12 @@ where } fn do_lock(&self) { - while let Err(_) = - self.locked - .compare_exchange_weak(false, true, Ordering::Acquire, Ordering::Relaxed) - { + while let Err(_) = self.locked.compare_exchange_weak( + false, + true, + Ordering::Acquire, + Ordering::Relaxed, + ) { R::relax(); } } diff --git a/crates/eonix_sync/eonix_sync_base/src/lazy_lock.rs b/crates/eonix_sync/eonix_sync_base/src/lazy_lock.rs index fe33782d..2bbd2599 100644 --- a/crates/eonix_sync/eonix_sync_base/src/lazy_lock.rs +++ b/crates/eonix_sync/eonix_sync_base/src/lazy_lock.rs @@ -1,10 +1,9 @@ +use core::cell::UnsafeCell; +use core::marker::PhantomData; +use core::ops::Deref; +use core::sync::atomic::{AtomicU8, Ordering}; + use crate::{Relax, SpinRelax}; -use core::{ - cell::UnsafeCell, - marker::PhantomData, - ops::Deref, - sync::atomic::{AtomicU8, Ordering}, -}; enum LazyState where @@ -135,7 +134,9 @@ where self.state.store(Self::INITIALIZED, Ordering::Release); } Self::INITIALIZED => {} - Self::INITIALIZING => unreachable!("We should be the only one initializing it."), + Self::INITIALIZING => { + unreachable!("We should be the only one initializing it.") + } _ => unreachable!("Invalid LazyLock state."), } diff --git a/crates/eonix_sync/eonix_sync_base/src/locked.rs b/crates/eonix_sync/eonix_sync_base/src/locked.rs index c242c5ae..9a323441 100644 --- a/crates/eonix_sync/eonix_sync_base/src/locked.rs +++ b/crates/eonix_sync/eonix_sync_base/src/locked.rs @@ -1,6 +1,8 @@ mod proof; -use core::{cell::UnsafeCell, fmt, ptr::NonNull}; +use core::cell::UnsafeCell; +use core::fmt; +use core::ptr::NonNull; pub use proof::{AsProof, AsProofMut, Proof, ProofMut}; diff --git a/crates/eonix_sync/eonix_sync_base/src/locked/proof.rs b/crates/eonix_sync/eonix_sync_base/src/locked/proof.rs index 52a5db49..e8acbcb7 100644 --- a/crates/eonix_sync/eonix_sync_base/src/locked/proof.rs +++ b/crates/eonix_sync/eonix_sync_base/src/locked/proof.rs @@ -1,4 +1,5 @@ -use core::{marker::PhantomData, ptr::NonNull}; +use core::marker::PhantomData; +use core::ptr::NonNull; /// A proof of mutable access to a position in memory with lifetime `'pos`. /// @@ -144,7 +145,9 @@ where { ProofMut { // SAFETY: The validity of the reference is guaranteed by the borrow checker. - address: unsafe { NonNull::new_unchecked(&raw const **self as *mut _) }, + address: unsafe { + NonNull::new_unchecked(&raw const **self as *mut _) + }, _phantom: PhantomData, } } @@ -161,7 +164,9 @@ where 'guard: 'pos, { Proof { - address: unsafe { NonNull::new_unchecked(&raw const **self as *mut _) }, + address: unsafe { + NonNull::new_unchecked(&raw const **self as *mut _) + }, _phantom: PhantomData, } } @@ -178,7 +183,9 @@ where 'guard: 'pos, { Proof { - address: unsafe { NonNull::new_unchecked(&raw const **self as *mut _) }, + address: unsafe { + NonNull::new_unchecked(&raw const **self as *mut _) + }, _phantom: PhantomData, } } diff --git a/crates/eonix_sync/eonix_sync_base/src/marker.rs b/crates/eonix_sync/eonix_sync_base/src/marker.rs index ce51948c..7a06e1c2 100644 --- a/crates/eonix_sync/eonix_sync_base/src/marker.rs +++ b/crates/eonix_sync/eonix_sync_base/src/marker.rs @@ -1,4 +1,5 @@ -use core::{cell::UnsafeCell, marker::PhantomData}; +use core::cell::UnsafeCell; +use core::marker::PhantomData; /// A marker type that indicates that the type is not `Send`. pub struct NotSend(PhantomData<*const ()>); diff --git a/crates/eonix_sync/eonix_sync_rt/src/mutex.rs b/crates/eonix_sync/eonix_sync_rt/src/mutex.rs index 33fe58d6..a048dbae 100644 --- a/crates/eonix_sync/eonix_sync_rt/src/mutex.rs +++ b/crates/eonix_sync/eonix_sync_rt/src/mutex.rs @@ -1,14 +1,13 @@ mod guard; -use crate::WaitList; -use core::{ - cell::UnsafeCell, - pin::pin, - sync::atomic::{AtomicBool, Ordering}, -}; +use core::cell::UnsafeCell; +use core::pin::pin; +use core::sync::atomic::{AtomicBool, Ordering}; pub use guard::MutexGuard; +use crate::WaitList; + #[derive(Debug, Default)] pub struct Mutex where @@ -53,7 +52,12 @@ where fn try_lock_weak(&self) -> Option> { self.locked - .compare_exchange_weak(false, true, Ordering::Acquire, Ordering::Relaxed) + .compare_exchange_weak( + false, + true, + Ordering::Acquire, + Ordering::Relaxed, + ) .ok() .map(|_| unsafe { self.get_lock() }) } diff --git a/crates/eonix_sync/eonix_sync_rt/src/mutex/guard.rs b/crates/eonix_sync/eonix_sync_rt/src/mutex/guard.rs index b746cf94..83dc4ca1 100644 --- a/crates/eonix_sync/eonix_sync_rt/src/mutex/guard.rs +++ b/crates/eonix_sync/eonix_sync_rt/src/mutex/guard.rs @@ -1,10 +1,10 @@ -use super::Mutex; -use core::{ - ops::{Deref, DerefMut}, - sync::atomic::Ordering, -}; +use core::ops::{Deref, DerefMut}; +use core::sync::atomic::Ordering; + use eonix_sync_base::{UnlockableGuard, UnlockedGuard}; +use super::Mutex; + pub struct MutexGuard<'a, T> where T: ?Sized, diff --git a/crates/eonix_sync/eonix_sync_rt/src/rwlock.rs b/crates/eonix_sync/eonix_sync_rt/src/rwlock.rs index 21764142..8c02b2d9 100644 --- a/crates/eonix_sync/eonix_sync_rt/src/rwlock.rs +++ b/crates/eonix_sync/eonix_sync_rt/src/rwlock.rs @@ -1,14 +1,13 @@ mod guard; -use crate::WaitList; -use core::{ - cell::UnsafeCell, - pin::pin, - sync::atomic::{AtomicIsize, Ordering}, -}; +use core::cell::UnsafeCell; +use core::pin::pin; +use core::sync::atomic::{AtomicIsize, Ordering}; pub use guard::{RwLockReadGuard, RwLockWriteGuard}; +use crate::WaitList; + #[derive(Debug, Default)] pub struct RwLock where @@ -83,7 +82,10 @@ where self.read_wait.notify_all(); } } - val => unreachable!("RwLock::read_unlock(): erroneous counter value: {}", val), + val => unreachable!( + "RwLock::read_unlock(): erroneous counter value: {}", + val + ), } } @@ -110,7 +112,12 @@ where let counter = self.counter.load(Ordering::Relaxed); if counter >= 0 { self.counter - .compare_exchange(counter, counter + 1, Ordering::Acquire, Ordering::Relaxed) + .compare_exchange( + counter, + counter + 1, + Ordering::Acquire, + Ordering::Relaxed, + ) .ok() .map(|_| unsafe { self.read_lock() }) } else { @@ -125,7 +132,12 @@ where let counter = self.counter.load(Ordering::Relaxed); if counter >= 0 { self.counter - .compare_exchange_weak(counter, counter + 1, Ordering::Acquire, Ordering::Relaxed) + .compare_exchange_weak( + counter, + counter + 1, + Ordering::Acquire, + Ordering::Relaxed, + ) .ok() .map(|_| unsafe { self.read_lock() }) } else { diff --git a/crates/eonix_sync/eonix_sync_rt/src/rwlock/guard.rs b/crates/eonix_sync/eonix_sync_rt/src/rwlock/guard.rs index a9032b48..f0786603 100644 --- a/crates/eonix_sync/eonix_sync_rt/src/rwlock/guard.rs +++ b/crates/eonix_sync/eonix_sync_rt/src/rwlock/guard.rs @@ -1,6 +1,10 @@ -use super::RwLock; use core::ops::{Deref, DerefMut}; -use eonix_sync_base::{AsProof, AsProofMut, Proof, ProofMut, UnlockableGuard, UnlockedGuard}; + +use eonix_sync_base::{ + AsProof, AsProofMut, Proof, ProofMut, UnlockableGuard, UnlockedGuard, +}; + +use super::RwLock; pub struct RwLockWriteGuard<'a, T> where @@ -114,7 +118,8 @@ where } } -unsafe impl<'guard, 'pos, T> AsProof<'guard, 'pos, T> for RwLockWriteGuard<'guard, T> +unsafe impl<'guard, 'pos, T> AsProof<'guard, 'pos, T> + for RwLockWriteGuard<'guard, T> where T: ?Sized, { @@ -123,7 +128,8 @@ where } } -unsafe impl<'guard, 'pos, T> AsProofMut<'guard, 'pos, T> for RwLockWriteGuard<'guard, T> +unsafe impl<'guard, 'pos, T> AsProofMut<'guard, 'pos, T> + for RwLockWriteGuard<'guard, T> where T: ?Sized, { @@ -132,7 +138,8 @@ where } } -unsafe impl<'guard, 'pos, T> AsProof<'guard, 'pos, T> for RwLockReadGuard<'guard, T> +unsafe impl<'guard, 'pos, T> AsProof<'guard, 'pos, T> + for RwLockReadGuard<'guard, T> where T: ?Sized, { diff --git a/crates/eonix_sync/eonix_sync_rt/src/spin_irq.rs b/crates/eonix_sync/eonix_sync_rt/src/spin_irq.rs index b70cdc3d..c0f74ded 100644 --- a/crates/eonix_sync/eonix_sync_rt/src/spin_irq.rs +++ b/crates/eonix_sync/eonix_sync_rt/src/spin_irq.rs @@ -1,6 +1,8 @@ use eonix_hal::traits::trap::IrqState as _; use eonix_hal::trap::{disable_irqs_save, IrqState}; -use eonix_spin::{ContextUnlock, Spin, SpinContext, SpinGuard, UnlockedContext}; +use eonix_spin::{ + ContextUnlock, Spin, SpinContext, SpinGuard, UnlockedContext, +}; use eonix_sync_base::Relax; pub struct IrqContext(IrqState); @@ -12,7 +14,9 @@ pub trait SpinIrq { type Context: SpinContext; type Relax; - fn lock_irq(&self) -> SpinGuard<'_, Self::Value, Self::Context, Self::Relax>; + fn lock_irq( + &self, + ) -> SpinGuard<'_, Self::Value, Self::Context, Self::Relax>; } impl SpinContext for IrqContext { @@ -50,7 +54,9 @@ where type Context = IrqContext; type Relax = R; - fn lock_irq(&self) -> SpinGuard<'_, Self::Value, Self::Context, Self::Relax> { + fn lock_irq( + &self, + ) -> SpinGuard<'_, Self::Value, Self::Context, Self::Relax> { self.lock_with_context(IrqContext::save()) } } diff --git a/crates/eonix_sync/eonix_sync_rt/src/wait_list.rs b/crates/eonix_sync/eonix_sync_rt/src/wait_list.rs index a6a77038..613b041b 100644 --- a/crates/eonix_sync/eonix_sync_rt/src/wait_list.rs +++ b/crates/eonix_sync/eonix_sync_rt/src/wait_list.rs @@ -1,14 +1,16 @@ mod wait_handle; mod wait_object; -use crate::SpinIrq as _; use core::fmt; + use eonix_spin::Spin; use eonix_sync_base::LazyLock; -use intrusive_collections::{linked_list::CursorMut, LinkedList}; +use intrusive_collections::linked_list::CursorMut; +use intrusive_collections::LinkedList; +pub use wait_handle::WaitHandle; use wait_object::{WaitObject, WaitObjectAdapter}; -pub use wait_handle::WaitHandle; +use crate::SpinIrq as _; pub struct WaitList { /// # Lock @@ -20,7 +22,9 @@ pub struct WaitList { impl WaitList { pub const fn new() -> Self { Self { - waiters: LazyLock::new(|| Spin::new(LinkedList::new(WaitObjectAdapter::new()))), + waiters: LazyLock::new(|| { + Spin::new(LinkedList::new(WaitObjectAdapter::new())) + }), } } @@ -66,7 +70,9 @@ impl WaitList { } impl WaitList { - unsafe fn notify_waiter_unchecked(&self, waiter: &mut CursorMut<'_, WaitObjectAdapter>) { + unsafe fn notify_waiter_unchecked( + &self, waiter: &mut CursorMut<'_, WaitObjectAdapter>, + ) { let wait_object = unsafe { // SAFETY: The caller guarantees that `waiter` should be `Some`. // `wait_object` is a valid reference to a `WaitObject` because we diff --git a/crates/eonix_sync/eonix_sync_rt/src/wait_list/wait_handle.rs b/crates/eonix_sync/eonix_sync_rt/src/wait_list/wait_handle.rs index 5631c8e1..b50bb5a2 100644 --- a/crates/eonix_sync/eonix_sync_rt/src/wait_list/wait_handle.rs +++ b/crates/eonix_sync/eonix_sync_rt/src/wait_list/wait_handle.rs @@ -1,13 +1,14 @@ -use super::{wait_object::WaitObject, WaitList}; -use crate::SpinIrq as _; -use core::{ - cell::UnsafeCell, - hint::spin_loop, - pin::Pin, - task::{Context, Poll, Waker}, -}; +use core::cell::UnsafeCell; +use core::hint::spin_loop; +use core::pin::Pin; +use core::task::{Context, Poll, Waker}; + use intrusive_collections::UnsafeRef; +use super::wait_object::WaitObject; +use super::WaitList; +use crate::SpinIrq as _; + pub struct WaitHandle<'a> { wait_list: &'a WaitList, wait_object: UnsafeCell, @@ -78,7 +79,9 @@ impl<'a> WaitHandle<'a> { /// # Returns /// Whether we've been woken up or not. - fn do_add_to_wait_list(mut self: Pin<&mut Self>, waker: Option<&Waker>) -> bool { + fn do_add_to_wait_list( + mut self: Pin<&mut Self>, waker: Option<&Waker>, + ) -> bool { let PrepareSplit { wait_list, state, @@ -119,7 +122,8 @@ impl<'a> WaitHandle<'a> { if let Some(waker) = waker { // Lock the waker and check if it is already set. - let waker_set = wait_object.save_waker_if_not_woken_up(&waker); + let waker_set = + wait_object.save_waker_if_not_woken_up(&waker); if waker_set { *state = State::WakerSet; @@ -144,7 +148,9 @@ impl<'a> WaitHandle<'a> { /// The caller MUST guarantee that the last use of the returned function /// is before `self` is dropped. Otherwise the value referred to in this /// function will be dangling and will cause undefined behavior. - pub unsafe fn get_waker_function(self: Pin<&Self>) -> impl Fn() + Send + Sync + 'static { + pub unsafe fn get_waker_function( + self: Pin<&Self>, + ) -> impl Fn() + Send + Sync + 'static { let wait_list: &WaitList = unsafe { // SAFETY: The caller guarantees that the last use of returned function // is before `self` is dropped. @@ -166,7 +172,9 @@ impl<'a> WaitHandle<'a> { impl Future for WaitHandle<'_> { type Output = (); - fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + fn poll( + mut self: Pin<&mut Self>, cx: &mut Context<'_>, + ) -> Poll { match self.state { State::Init | State::OnList => { if self.as_mut().do_add_to_wait_list(Some(cx.waker())) { diff --git a/crates/eonix_sync/eonix_sync_rt/src/wait_list/wait_object.rs b/crates/eonix_sync/eonix_sync_rt/src/wait_list/wait_object.rs index 0155fcb4..cf6b0237 100644 --- a/crates/eonix_sync/eonix_sync_rt/src/wait_list/wait_object.rs +++ b/crates/eonix_sync/eonix_sync_rt/src/wait_list/wait_object.rs @@ -1,15 +1,17 @@ +use core::cell::UnsafeCell; +use core::marker::PhantomPinned; +use core::pin::Pin; +use core::ptr::null_mut; +use core::sync::atomic::{AtomicBool, AtomicPtr, Ordering}; +use core::task::Waker; + +use eonix_spin::Spin; +use intrusive_collections::{ + intrusive_adapter, LinkedListAtomicLink, UnsafeRef, +}; + use super::WaitList; use crate::SpinIrq as _; -use core::{ - cell::UnsafeCell, - marker::PhantomPinned, - pin::Pin, - ptr::null_mut, - sync::atomic::{AtomicBool, AtomicPtr, Ordering}, - task::Waker, -}; -use eonix_spin::Spin; -use intrusive_collections::{intrusive_adapter, LinkedListAtomicLink, UnsafeRef}; intrusive_adapter!( pub WaitObjectAdapter = Pin>: diff --git a/crates/pointers/src/lib.rs b/crates/pointers/src/lib.rs index 22033cee..08e90832 100644 --- a/crates/pointers/src/lib.rs +++ b/crates/pointers/src/lib.rs @@ -1,7 +1,10 @@ #![no_std] use alloc::sync::Arc; -use core::{marker::PhantomData, mem::ManuallyDrop, ops::Deref, ptr::NonNull}; +use core::marker::PhantomData; +use core::mem::ManuallyDrop; +use core::ops::Deref; +use core::ptr::NonNull; extern crate alloc; diff --git a/crates/posix_types/src/signal.rs b/crates/posix_types/src/signal.rs index 415444e5..90f00978 100644 --- a/crates/posix_types/src/signal.rs +++ b/crates/posix_types/src/signal.rs @@ -3,7 +3,8 @@ mod siginfo; mod signal; pub use sig_action::{ - SigAction, SigActionFlags, SigActionHandler, SigActionRestorer, SigSet, TryFromSigAction, + SigAction, SigActionFlags, SigActionHandler, SigActionRestorer, SigSet, + TryFromSigAction, }; pub use siginfo::SigInfo; pub use signal::Signal; diff --git a/crates/posix_types/src/signal/sig_action.rs b/crates/posix_types/src/signal/sig_action.rs index 8464e6c6..ab77ee31 100644 --- a/crates/posix_types/src/signal/sig_action.rs +++ b/crates/posix_types/src/signal/sig_action.rs @@ -1,6 +1,7 @@ +use bitflags::bitflags; + use super::Signal; use crate::ctypes::PtrT; -use bitflags::bitflags; bitflags! { #[derive(Debug, Clone, Copy)] diff --git a/crates/posix_types/src/signal/signal.rs b/crates/posix_types/src/signal/signal.rs index fd5121af..929868f0 100644 --- a/crates/posix_types/src/signal/signal.rs +++ b/crates/posix_types/src/signal/signal.rs @@ -1,6 +1,7 @@ -use crate::result::PosixError; use core::fmt; +use crate::result::PosixError; + #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub struct Signal(u32); @@ -89,7 +90,9 @@ impl fmt::Debug for Signal { &Signal::SIGIO => write!(f, "SIGNOIO/SIGPOLL"), &Signal::SIGPWR => write!(f, "SIGHUPWR"), &Signal::SIGSYS => write!(f, "SIGSYS"), - &Signal(signo @ ..Signal::SIGNUM_MAX) => write!(f, "SIGUSER{}", signo), + &Signal(signo @ ..Signal::SIGNUM_MAX) => { + write!(f, "SIGUSER{}", signo) + } &Signal(signo) => write!(f, "Signal::UNKNOWN({})", signo), } } diff --git a/crates/posix_types/src/stat.rs b/crates/posix_types/src/stat.rs index 845568ff..37068091 100644 --- a/crates/posix_types/src/stat.rs +++ b/crates/posix_types/src/stat.rs @@ -73,13 +73,15 @@ pub struct Stat { impl From for Stat { fn from(statx: StatX) -> Self { Self { - st_dev: statx.stx_dev_minor as u64 | ((statx.stx_dev_major as u64) << 8), + st_dev: statx.stx_dev_minor as u64 + | ((statx.stx_dev_major as u64) << 8), st_ino: statx.stx_ino, st_mode: statx.stx_mode as u32, st_nlink: statx.stx_nlink, st_uid: statx.stx_uid, st_gid: statx.stx_gid, - st_rdev: statx.stx_rdev_minor as u64 | ((statx.stx_rdev_major as u64) << 8), + st_rdev: statx.stx_rdev_minor as u64 + | ((statx.stx_rdev_major as u64) << 8), __padding: 0, st_size: statx.stx_size, diff --git a/src/driver/ahci/control.rs b/src/driver/ahci/control.rs index 049b4501..3f290e52 100644 --- a/src/driver/ahci/control.rs +++ b/src/driver/ahci/control.rs @@ -1,9 +1,11 @@ -use super::{BitsIterator, GHC_IE}; -use crate::kernel::mem::PhysAccess as _; use core::ptr::NonNull; + use eonix_hal::fence::memory_barrier; use eonix_mm::address::PAddr; +use super::{BitsIterator, GHC_IE}; +use crate::kernel::mem::PhysAccess as _; + /// An `AdapterControl` is an HBA device Global Host Control block /// /// # Access diff --git a/src/driver/ahci/port.rs b/src/driver/ahci/port.rs index a54bbbba..f6165029 100644 --- a/src/driver/ahci/port.rs +++ b/src/driver/ahci/port.rs @@ -5,10 +5,15 @@ use async_trait::async_trait; use eonix_mm::address::{Addr as _, PAddr}; use eonix_sync::SpinIrq as _; -use super::command::{Command, IdentifyCommand, ReadLBACommand, WriteLBACommand}; +use super::command::{ + Command, IdentifyCommand, ReadLBACommand, WriteLBACommand, +}; use super::slot::CommandList; use super::stats::AdapterPortStats; -use super::{Register, PORT_CMD_CR, PORT_CMD_FR, PORT_CMD_FRE, PORT_CMD_ST, PORT_IE_DEFAULT}; +use super::{ + Register, PORT_CMD_CR, PORT_CMD_FR, PORT_CMD_FRE, PORT_CMD_ST, + PORT_IE_DEFAULT, +}; use crate::driver::ahci::command_table::CommandTable; use crate::kernel::block::{BlockDeviceRequest, BlockRequestQueue}; use crate::kernel::constants::{EINVAL, EIO}; @@ -236,9 +241,12 @@ impl AdapterPort { } pub fn print_stats(&self, writer: &mut impl Write) -> KResult<()> { - writeln!(writer, "cmd_sent: {}", self.stats.get_cmd_sent()).map_err(|_| EIO)?; - writeln!(writer, "cmd_error: {}", self.stats.get_cmd_error()).map_err(|_| EIO)?; - writeln!(writer, "int_fired: {}", self.stats.get_int_fired()).map_err(|_| EIO)?; + writeln!(writer, "cmd_sent: {}", self.stats.get_cmd_sent()) + .map_err(|_| EIO)?; + writeln!(writer, "cmd_error: {}", self.stats.get_cmd_error()) + .map_err(|_| EIO)?; + writeln!(writer, "int_fired: {}", self.stats.get_int_fired()) + .map_err(|_| EIO)?; Ok(()) } @@ -261,7 +269,8 @@ impl BlockRequestQueue for AdapterPort { return Err(EINVAL); } - let command = ReadLBACommand::new(buffer, sector, count as u16)?; + let command = + ReadLBACommand::new(buffer, sector, count as u16)?; self.send_command(&command).await } @@ -274,7 +283,8 @@ impl BlockRequestQueue for AdapterPort { return Err(EINVAL); } - let command = WriteLBACommand::new(buffer, sector, count as u16)?; + let command = + WriteLBACommand::new(buffer, sector, count as u16)?; self.send_command(&command).await } diff --git a/src/driver/ahci/register.rs b/src/driver/ahci/register.rs index 786b6bd9..09699c86 100644 --- a/src/driver/ahci/register.rs +++ b/src/driver/ahci/register.rs @@ -1,11 +1,12 @@ -use crate::{ - kernel::{constants::EIO, mem::PhysAccess as _}, - KResult, -}; use core::ptr::NonNull; + use eonix_hal::fence::memory_barrier; use eonix_mm::address::PAddr; +use crate::kernel::constants::EIO; +use crate::kernel::mem::PhysAccess as _; +use crate::KResult; + pub struct Register { addr: NonNull, } diff --git a/src/driver/ahci/slot.rs b/src/driver/ahci/slot.rs index fdb61f96..1be3841f 100644 --- a/src/driver/ahci/slot.rs +++ b/src/driver/ahci/slot.rs @@ -66,7 +66,9 @@ impl CommandList { pub fn recv_fis_base(&self) -> PAddr { self._page.start() - + (size_of::>() + size_of::>()) * 32 + + (size_of::>() + + size_of::>()) + * 32 } pub fn get(&self, index: usize) -> CommandSlot<'_> { diff --git a/src/driver/goldfish_rtc.rs b/src/driver/goldfish_rtc.rs index 0d63e03b..87f82e63 100644 --- a/src/driver/goldfish_rtc.rs +++ b/src/driver/goldfish_rtc.rs @@ -1,12 +1,13 @@ -use crate::kernel::{ - rtc::{register_rtc, RealTimeClock}, - timer::Instant, -}; use core::ptr::NonNull; -use eonix_hal::{arch_exported::fdt::FDT, mm::ArchPhysAccess}; + +use eonix_hal::arch_exported::fdt::FDT; +use eonix_hal::mm::ArchPhysAccess; use eonix_log::println_warn; use eonix_mm::address::{PAddr, PhysAccess}; +use crate::kernel::rtc::{register_rtc, RealTimeClock}; +use crate::kernel::timer::Instant; + #[cfg(not(target_arch = "riscv64"))] compile_error!("Goldfish RTC driver is only supported on RISC-V architecture"); diff --git a/src/driver/sbi_console.rs b/src/driver/sbi_console.rs index 51f3a24a..800095f9 100644 --- a/src/driver/sbi_console.rs +++ b/src/driver/sbi_console.rs @@ -1,7 +1,9 @@ -use crate::kernel::{Terminal, TerminalDevice}; use alloc::sync::Arc; + use eonix_log::ConsoleWrite; +use crate::kernel::{Terminal, TerminalDevice}; + struct SbiConsole; impl ConsoleWrite for SbiConsole { @@ -29,5 +31,6 @@ pub fn init_console() { let console = Arc::new(SbiConsole); let terminal = Terminal::new(console.clone()); - crate::kernel::console::set_console(terminal).expect("Failed to set console"); + crate::kernel::console::set_console(terminal) + .expect("Failed to set console"); } diff --git a/src/driver/serial.rs b/src/driver/serial.rs index b84a1c5f..d31199da 100644 --- a/src/driver/serial.rs +++ b/src/driver/serial.rs @@ -1,19 +1,22 @@ mod io; -use crate::{ - kernel::{ - console::set_console, constants::EIO, interrupt::register_irq_handler, - vfs::types::DeviceId, CharDevice, CharDeviceType, Terminal, TerminalDevice, - }, - prelude::*, -}; -use alloc::{collections::vec_deque::VecDeque, format, sync::Arc}; -use bitflags::bitflags; +use alloc::collections::vec_deque::VecDeque; +use alloc::format; +use alloc::sync::Arc; use core::pin::pin; + +use bitflags::bitflags; use eonix_runtime::scheduler::RUNTIME; use eonix_sync::{SpinIrq as _, WaitList}; use io::SerialIO; +use crate::kernel::console::set_console; +use crate::kernel::constants::EIO; +use crate::kernel::interrupt::register_irq_handler; +use crate::kernel::vfs::types::DeviceId; +use crate::kernel::{CharDevice, CharDeviceType, Terminal, TerminalDevice}; +use crate::prelude::*; + bitflags! { struct LineStatus: u8 { const RX_READY = 0x01; diff --git a/src/driver/virtio.rs b/src/driver/virtio.rs index ea0ff896..5ff625bb 100644 --- a/src/driver/virtio.rs +++ b/src/driver/virtio.rs @@ -1,7 +1,9 @@ mod virtio_blk; #[cfg(not(any(target_arch = "riscv64", target_arch = "loongarch64")))] -compile_error!("VirtIO drivers are only supported on RISC-V and LoongArch64 architecture"); +compile_error!( + "VirtIO drivers are only supported on RISC-V and LoongArch64 architecture" +); #[cfg(target_arch = "riscv64")] mod riscv64; diff --git a/src/driver/virtio/loongarch64.rs b/src/driver/virtio/loongarch64.rs index 996683bd..d865bbdf 100644 --- a/src/driver/virtio/loongarch64.rs +++ b/src/driver/virtio/loongarch64.rs @@ -1,29 +1,28 @@ -use super::virtio_blk::HAL; -use crate::kernel::{ - block::{make_device, BlockDevice}, - constants::EIO, - pcie::{self, PCIDevice, PCIDriver, PciError, SegmentGroup}, - task::block_on, -}; use alloc::sync::Arc; use core::sync::atomic::{AtomicUsize, Ordering}; -use eonix_hal::{fence::memory_barrier, mm::ArchPhysAccess}; + +use eonix_hal::fence::memory_barrier; +use eonix_hal::mm::ArchPhysAccess; use eonix_log::println_warn; use eonix_mm::address::PhysAccess; use eonix_sync::Spin; -use virtio_drivers::{ - device::blk::VirtIOBlk, - transport::{ - pci::{ - bus::{ConfigurationAccess, DeviceFunction, PciRoot}, - PciTransport, - }, - DeviceType, Transport, - }, +use virtio_drivers::device::blk::VirtIOBlk; +use virtio_drivers::transport::pci::bus::{ + ConfigurationAccess, DeviceFunction, PciRoot, }; +use virtio_drivers::transport::pci::PciTransport; +use virtio_drivers::transport::{DeviceType, Transport}; + +use super::virtio_blk::HAL; +use crate::kernel::block::{make_device, BlockDevice}; +use crate::kernel::constants::EIO; +use crate::kernel::pcie::{self, PCIDevice, PCIDriver, PciError, SegmentGroup}; +use crate::kernel::task::block_on; impl ConfigurationAccess for &SegmentGroup { - fn read_word(&self, device_function: DeviceFunction, register_offset: u8) -> u32 { + fn read_word( + &self, device_function: DeviceFunction, register_offset: u8, + ) -> u32 { let conf_space = self .get_conf_space( device_function.bus, @@ -49,7 +48,10 @@ impl ConfigurationAccess for &SegmentGroup { value } - fn write_word(&mut self, device_function: DeviceFunction, register_offset: u8, data: u32) { + fn write_word( + &mut self, device_function: DeviceFunction, register_offset: u8, + data: u32, + ) { let conf_space = self .get_conf_space( device_function.bus, @@ -91,7 +93,9 @@ impl PCIDriver for VirtIODriver { 0x1001 } - fn handle_device(&self, device: Arc>) -> Result<(), PciError> { + fn handle_device( + &self, device: Arc>, + ) -> Result<(), PciError> { let transport = PciTransport::new::( &mut PciRoot::new(device.segment_group()), DeviceFunction { @@ -123,13 +127,20 @@ impl PCIDriver for VirtIODriver { Err(EIO)?; } - let virtio_block = VirtIOBlk::::new(transport).map_err(|err| { - println_warn!("Failed to initialize VirtIO Block device: {}", err); - EIO - })?; + let virtio_block = + VirtIOBlk::::new(transport).map_err(|err| { + println_warn!( + "Failed to initialize VirtIO Block device: {}", + err + ); + EIO + })?; let block_device = BlockDevice::register_disk( - make_device(8, 16 * self.disk_id.fetch_add(1, Ordering::AcqRel) as u32), + make_device( + 8, + 16 * self.disk_id.fetch_add(1, Ordering::AcqRel) as u32, + ), 2147483647, // TODO: Get size from device Arc::new(Spin::new(virtio_block)), )?; diff --git a/src/driver/virtio/riscv64.rs b/src/driver/virtio/riscv64.rs index b33e16ac..01a3ae4d 100644 --- a/src/driver/virtio/riscv64.rs +++ b/src/driver/virtio/riscv64.rs @@ -1,23 +1,28 @@ -use super::virtio_blk::HAL; -use crate::kernel::{block::BlockDevice, task::block_on, vfs::types::DeviceId}; -use alloc::{sync::Arc, vec::Vec}; +use alloc::sync::Arc; +use alloc::vec::Vec; + use eonix_hal::arch_exported::fdt::FDT; use eonix_hal::mm::ArchPhysAccess; use eonix_log::{println_info, println_warn}; use eonix_mm::address::{PAddr, PhysAccess}; use eonix_sync::Spin; -use virtio_drivers::{ - device::blk::VirtIOBlk, - transport::{mmio::MmioTransport, Transport}, -}; +use virtio_drivers::device::blk::VirtIOBlk; +use virtio_drivers::transport::mmio::MmioTransport; +use virtio_drivers::transport::Transport; + +use super::virtio_blk::HAL; +use crate::kernel::block::BlockDevice; +use crate::kernel::task::block_on; +use crate::kernel::vfs::types::DeviceId; pub fn init() { let mut disk_id = 0; let mut virtio_devices: Vec<_> = FDT .all_nodes() .filter(|node| { - node.compatible() - .is_some_and(|compatible| compatible.all().any(|s| s == "virtio,mmio")) + node.compatible().is_some_and(|compatible| { + compatible.all().any(|s| s == "virtio,mmio") + }) }) .filter_map(|node| node.reg()) .flatten() @@ -46,8 +51,9 @@ pub fn init() { ) .expect("Failed to register VirtIO Block device"); - block_on(block_device.partprobe()) - .expect("Failed to probe partitions for VirtIO Block device"); + block_on(block_device.partprobe()).expect( + "Failed to probe partitions for VirtIO Block device", + ); disk_id += 1; } diff --git a/src/driver/virtio/virtio_blk.rs b/src/driver/virtio/virtio_blk.rs index 5dfed88a..a2dcf9a5 100644 --- a/src/driver/virtio/virtio_blk.rs +++ b/src/driver/virtio/virtio_blk.rs @@ -19,8 +19,7 @@ pub struct HAL; unsafe impl Hal for HAL { fn dma_alloc( - pages: usize, - _direction: virtio_drivers::BufferDirection, + pages: usize, _direction: virtio_drivers::BufferDirection, ) -> (virtio_drivers::PhysAddr, core::ptr::NonNull) { let page = Folio::alloc_at_least(pages); @@ -31,8 +30,7 @@ unsafe impl Hal for HAL { } unsafe fn dma_dealloc( - paddr: virtio_drivers::PhysAddr, - _vaddr: core::ptr::NonNull, + paddr: virtio_drivers::PhysAddr, _vaddr: core::ptr::NonNull, _pages: usize, ) -> i32 { let pfn = PFN::from(PAddr::from(paddr)); @@ -47,8 +45,7 @@ unsafe impl Hal for HAL { } unsafe fn mmio_phys_to_virt( - paddr: virtio_drivers::PhysAddr, - _size: usize, + paddr: virtio_drivers::PhysAddr, _size: usize, ) -> core::ptr::NonNull { unsafe { ArchPhysAccess::as_ptr(PAddr::from(paddr)) } } @@ -66,8 +63,7 @@ unsafe impl Hal for HAL { } unsafe fn unshare( - _paddr: virtio_drivers::PhysAddr, - _buffer: core::ptr::NonNull<[u8]>, + _paddr: virtio_drivers::PhysAddr, _buffer: core::ptr::NonNull<[u8]>, _direction: virtio_drivers::BufferDirection, ) { } @@ -91,7 +87,8 @@ where } => { let mut dev = self.lock(); for ((start, sectors), buffer_page) in - Chunks::new(sector as usize, count as usize, 8).zip(buffer.iter()) + Chunks::new(sector as usize, count as usize, 8) + .zip(buffer.iter()) { let len = sectors * 512; let pg = buffer_page.lock(); @@ -107,7 +104,8 @@ where } => { let mut dev = self.lock(); for ((start, sectors), buffer_page) in - Chunks::new(sector as usize, count as usize, 8).zip(buffer.iter()) + Chunks::new(sector as usize, count as usize, 8) + .zip(buffer.iter()) { let len = sectors * 512; let mut pg = buffer_page.lock(); diff --git a/src/fs/fat32.rs b/src/fs/fat32.rs index c1feebdf..428e4c98 100644 --- a/src/fs/fat32.rs +++ b/src/fs/fat32.rs @@ -39,7 +39,10 @@ impl RawCluster { match self.0 { ..Self::START | Self::EOC..Self::INVL => None, Self::INVL.. => { - unreachable!("invalid cluster number: RawCluster({:#08x})", self.0) + unreachable!( + "invalid cluster number: RawCluster({:#08x})", + self.0 + ) } no => Some(Cluster(no)), } @@ -113,7 +116,9 @@ struct FatFs { impl SuperBlock for FatFs {} impl FatFs { - async fn read_cluster(&self, mut cluster: Cluster, buf: &Folio) -> KResult<()> { + async fn read_cluster( + &self, mut cluster: Cluster, buf: &Folio, + ) -> KResult<()> { cluster = cluster.normalized(); let rq = BlockDeviceRequest::Read { @@ -137,7 +142,8 @@ impl FatFs { let info = info.assume_filled_ref()?; let mut fat = Box::new_uninit_slice( - 512 * info.sectors_per_fat as usize / core::mem::size_of::(), + 512 * info.sectors_per_fat as usize + / core::mem::size_of::(), ); device @@ -151,8 +157,8 @@ impl FatFs { let sectors_per_cluster = info.sectors_per_cluster; let rootdir_cluster = info.root_cluster.parse().ok_or(EINVAL)?; - let data_start_sector = - info.reserved_sectors as u64 + info.fat_copies as u64 * info.sectors_per_fat as u64; + let data_start_sector = info.reserved_sectors as u64 + + info.fat_copies as u64 * info.sectors_per_fat as u64; let volume_label = { let end = info @@ -168,8 +174,10 @@ impl FatFs { let fat = unsafe { fat.assume_init() }; - let rootdir_cluster_count = ClusterIterator::new(fat.as_ref(), rootdir_cluster).count(); - let rootdir_size = rootdir_cluster_count as u32 * sectors_per_cluster as u32 * 512; + let rootdir_cluster_count = + ClusterIterator::new(fat.as_ref(), rootdir_cluster).count(); + let rootdir_size = + rootdir_cluster_count as u32 * sectors_per_cluster as u32 * 512; let fatfs = SbUse::new( SuperBlockInfo { @@ -243,26 +251,21 @@ impl InodeOps for FileInode { type SuperBlock = FatFs; async fn read( - &self, - _: SbUse, - inode: &InodeUse, - buffer: &mut dyn Buffer, - offset: usize, + &self, _: SbUse, inode: &InodeUse, + buffer: &mut dyn Buffer, offset: usize, ) -> KResult { inode.get_page_cache().read(buffer, offset).await } async fn read_page( - &self, - sb: SbUse, - inode: &InodeUse, - page: &mut CachePage, - offset: PageOffset, + &self, sb: SbUse, inode: &InodeUse, + page: &mut CachePage, offset: PageOffset, ) -> KResult<()> { let fs = &sb.backend; let fat = sb.backend.fat.read().await; - if offset >= PageOffset::from_byte_ceil(inode.info.lock().size as usize) { + if offset >= PageOffset::from_byte_ceil(inode.info.lock().size as usize) + { unreachable!("read_page called with offset beyond file size"); } @@ -272,10 +275,11 @@ impl InodeOps for FileInode { } // XXX: Ugly and inefficient O(n^2) algorithm for sequential file read. - let cluster = ClusterIterator::new(fat.as_ref(), Cluster::from_ino(inode.ino)) - .skip(offset.page_count()) - .next() - .ok_or(EIO)?; + let cluster = + ClusterIterator::new(fat.as_ref(), Cluster::from_ino(inode.ino)) + .skip(offset.page_count()) + .next() + .ok_or(EIO)?; fs.read_cluster(cluster, &page).await?; @@ -316,7 +320,9 @@ impl DirInode { ) } - async fn read_dir_pages(&self, sb: &SbUse, inode: &InodeUse) -> KResult<()> { + async fn read_dir_pages( + &self, sb: &SbUse, inode: &InodeUse, + ) -> KResult<()> { let mut dir_pages = self.dir_pages.write().await; if !dir_pages.is_empty() { return Ok(()); @@ -325,7 +331,8 @@ impl DirInode { let fs = &sb.backend; let fat = fs.fat.read().await; - let clusters = ClusterIterator::new(fat.as_ref(), Cluster::from_ino(inode.ino)); + let clusters = + ClusterIterator::new(fat.as_ref(), Cluster::from_ino(inode.ino)); for cluster in clusters { let page = FolioOwned::alloc(); @@ -338,9 +345,7 @@ impl DirInode { } async fn get_dir_pages( - &self, - sb: &SbUse, - inode: &InodeUse, + &self, sb: &SbUse, inode: &InodeUse, ) -> KResult> + use<'_>> { { let dir_pages = self.dir_pages.read().await; @@ -363,9 +368,7 @@ impl InodeOps for DirInode { type SuperBlock = FatFs; async fn lookup( - &self, - sb: SbUse, - inode: &InodeUse, + &self, sb: SbUse, inode: &InodeUse, dentry: &Arc, ) -> KResult> { let dir_pages = self.get_dir_pages(&sb, inode).await?; @@ -389,9 +392,13 @@ impl InodeOps for DirInode { let sbref = SbRef::from(&sb); if entry.is_directory { - return Ok(Some(DirInode::new(entry.cluster, sbref, entry.size) as _)); + return Ok(Some( + DirInode::new(entry.cluster, sbref, entry.size) as _, + )); } else { - return Ok(Some(FileInode::new(entry.cluster, sbref, entry.size) as _)); + return Ok(Some( + FileInode::new(entry.cluster, sbref, entry.size) as _, + )); } } @@ -399,10 +406,7 @@ impl InodeOps for DirInode { } async fn readdir( - &self, - sb: SbUse, - inode: &InodeUse, - offset: usize, + &self, sb: SbUse, inode: &InodeUse, offset: usize, callback: &mut (dyn FnMut(&[u8], Ino) -> KResult + Send), ) -> KResult> { let fs = &sb.backend; @@ -412,7 +416,8 @@ impl InodeOps for DirInode { let cluster_offset = offset / cluster_size; let inner_offset = offset % cluster_size; - let inner_raw_dirent_offset = inner_offset / core::mem::size_of::(); + let inner_raw_dirent_offset = + inner_offset / core::mem::size_of::(); let dir_data = dir_pages .iter() @@ -448,13 +453,17 @@ struct FatMountCreator; impl MountCreator for FatMountCreator { fn check_signature(&self, mut first_block: &[u8]) -> KResult { match first_block.split_off(82..) { - Some([b'F', b'A', b'T', b'3', b'2', b' ', b' ', b' ', ..]) => Ok(true), + Some([b'F', b'A', b'T', b'3', b'2', b' ', b' ', b' ', ..]) => { + Ok(true) + } Some(..) => Ok(false), None => Err(EIO), } } - async fn create_mount(&self, _source: &str, _flags: u64, mp: &Arc) -> KResult { + async fn create_mount( + &self, _source: &str, _flags: u64, mp: &Arc, + ) -> KResult { let (fatfs, root_inode) = FatFs::create(DeviceId::new(8, 1)).await?; Mount::new(mp, fatfs, root_inode) diff --git a/src/fs/fat32/dir.rs b/src/fs/fat32/dir.rs index 8a5b6f40..98dd3496 100644 --- a/src/fs/fat32/dir.rs +++ b/src/fs/fat32/dir.rs @@ -1,14 +1,14 @@ +use alloc::boxed::Box; +use alloc::string::String; use core::pin::Pin; -use alloc::{boxed::Box, string::String}; use futures::{Stream, StreamExt}; use posix_types::result::PosixError; +use super::{Cluster, RawCluster}; use crate::kernel::constants::EINVAL; use crate::prelude::*; -use super::{Cluster, RawCluster}; - #[repr(C, packed)] pub struct RawDirEntry { name: [u8; 8], @@ -61,7 +61,11 @@ impl RawDirEntry { } fn is_long_filename(&self) -> bool { - self.attr == (Self::ATTR_RO | Self::ATTR_HIDDEN | Self::ATTR_SYSTEM | Self::ATTR_VOLUME_ID) + self.attr + == (Self::ATTR_RO + | Self::ATTR_HIDDEN + | Self::ATTR_SYSTEM + | Self::ATTR_VOLUME_ID) } fn is_volume_id(&self) -> bool { @@ -162,7 +166,8 @@ where break; } - let Some(raw_long_filename) = cur_entry.as_raw_long_filename() else { + let Some(raw_long_filename) = cur_entry.as_raw_long_filename() + else { continue; }; @@ -174,16 +179,20 @@ where .position(|&ch| ch == 0) .unwrap_or(raw_long_filename.len()); - let name_codes_rev = raw_long_filename.into_iter().take(real_len).rev(); - let name_chars_rev = char::decode_utf16(name_codes_rev).map(|r| r.unwrap_or('?')); + let name_codes_rev = + raw_long_filename.into_iter().take(real_len).rev(); + let name_chars_rev = + char::decode_utf16(name_codes_rev).map(|r| r.unwrap_or('?')); filename_rev.extend(name_chars_rev); } // From now on, `entry` represents a valid directory entry. - let raw_cluster = - RawCluster(cur_entry.cluster_low as u32 | ((cur_entry.cluster_high as u32) << 16)); + let raw_cluster = RawCluster( + cur_entry.cluster_low as u32 + | ((cur_entry.cluster_high as u32) << 16), + ); let Some(cluster) = raw_cluster.parse() else { return Some(Err(PosixError::EINVAL.into())); diff --git a/src/fs/tmpfs/dir.rs b/src/fs/tmpfs/dir.rs index 4dd64d52..1a04b98b 100644 --- a/src/fs/tmpfs/dir.rs +++ b/src/fs/tmpfs/dir.rs @@ -7,10 +7,14 @@ use eonix_sync::{LazyLock, RwLock}; use super::file::{DeviceInode, FileInode, SymlinkInode}; use super::TmpFs; -use crate::kernel::constants::{EEXIST, EINVAL, EISDIR, ENOENT, ENOSYS, ENOTDIR}; +use crate::kernel::constants::{ + EEXIST, EINVAL, EISDIR, ENOENT, ENOSYS, ENOTDIR, +}; use crate::kernel::timer::Instant; use crate::kernel::vfs::dentry::{dcache, Dentry}; -use crate::kernel::vfs::inode::{Ino, InodeInfo, InodeOps, InodeUse, RenameData}; +use crate::kernel::vfs::inode::{ + Ino, InodeInfo, InodeOps, InodeUse, RenameData, +}; use crate::kernel::vfs::types::{DeviceId, Format, Mode, Permission}; use crate::kernel::vfs::{SbRef, SbUse}; use crate::prelude::KResult; @@ -19,7 +23,10 @@ pub struct DirectoryInode { entries: RwLock, Ino)>>, } -fn link(dir: &InodeUse, entries: &mut Vec<(Arc<[u8]>, Ino)>, name: Arc<[u8]>, file: &InodeUse) { +fn link( + dir: &InodeUse, entries: &mut Vec<(Arc<[u8]>, Ino)>, name: Arc<[u8]>, + file: &InodeUse, +) { let mut dir_info = dir.info.lock(); let mut file_info = file.info.lock(); @@ -37,7 +44,8 @@ fn link(dir: &InodeUse, entries: &mut Vec<(Arc<[u8]>, Ino)>, name: Arc<[u8]>, fi impl DirectoryInode { pub fn new(ino: Ino, sb: SbRef, perm: Permission) -> InodeUse { - static DOT: LazyLock> = LazyLock::new(|| Arc::from(b".".as_slice())); + static DOT: LazyLock> = + LazyLock::new(|| Arc::from(b".".as_slice())); let now = Instant::now(); @@ -62,14 +70,9 @@ impl DirectoryInode { } fn do_unlink( - &self, - file: &InodeUse, - filename: &[u8], - entries: &mut Vec<(Arc<[u8]>, Ino)>, - now: Instant, - decrease_size: bool, - self_info: &mut InodeInfo, - file_info: &mut InodeInfo, + &self, file: &InodeUse, filename: &[u8], + entries: &mut Vec<(Arc<[u8]>, Ino)>, now: Instant, decrease_size: bool, + self_info: &mut InodeInfo, file_info: &mut InodeInfo, ) -> KResult<()> { // SAFETY: `file_lock` has done the synchronization if file.format == Format::DIR { @@ -77,7 +80,9 @@ impl DirectoryInode { } let file_ino = file.ino; - entries.retain(|(name, ino)| *ino != file_ino || name.as_ref() != filename); + entries.retain(|(name, ino)| { + *ino != file_ino || name.as_ref() != filename + }); if decrease_size { self_info.size -= 1; @@ -102,10 +107,7 @@ impl InodeOps for DirectoryInode { type SuperBlock = TmpFs; async fn readdir( - &self, - sb: SbUse, - _: &InodeUse, - offset: usize, + &self, sb: SbUse, _: &InodeUse, offset: usize, for_each_entry: &mut (dyn FnMut(&[u8], Ino) -> KResult + Send), ) -> KResult> { let _sb = sb; @@ -124,10 +126,7 @@ impl InodeOps for DirectoryInode { } async fn create( - &self, - sb: SbUse, - inode: &InodeUse, - at: &Arc, + &self, sb: SbUse, inode: &InodeUse, at: &Arc, perm: Permission, ) -> KResult<()> { let mut entries = self.entries.write().await; @@ -142,12 +141,8 @@ impl InodeOps for DirectoryInode { } async fn mknod( - &self, - sb: SbUse, - inode: &InodeUse, - at: &Dentry, - mode: Mode, - dev: DeviceId, + &self, sb: SbUse, inode: &InodeUse, at: &Dentry, + mode: Mode, dev: DeviceId, ) -> KResult<()> { if !mode.is_chr() && !mode.is_blk() { return Err(EINVAL); @@ -165,10 +160,7 @@ impl InodeOps for DirectoryInode { } async fn symlink( - &self, - sb: SbUse, - inode: &InodeUse, - at: &Arc, + &self, sb: SbUse, inode: &InodeUse, at: &Arc, target: &[u8], ) -> KResult<()> { let mut entries = self.entries.write().await; @@ -183,10 +175,7 @@ impl InodeOps for DirectoryInode { } async fn mkdir( - &self, - sb: SbUse, - inode: &InodeUse, - at: &Dentry, + &self, sb: SbUse, inode: &InodeUse, at: &Dentry, perm: Permission, ) -> KResult<()> { let mut entries = self.entries.write().await; @@ -201,10 +190,7 @@ impl InodeOps for DirectoryInode { } async fn unlink( - &self, - _sb: SbUse, - inode: &InodeUse, - at: &Arc, + &self, _sb: SbUse, inode: &InodeUse, at: &Arc, ) -> KResult<()> { let mut entries = self.entries.write().await; @@ -229,9 +215,7 @@ impl InodeOps for DirectoryInode { } async fn rename( - &self, - sb: SbUse, - inode: &InodeUse, + &self, sb: SbUse, inode: &InodeUse, rename_data: RenameData<'_, '_>, ) -> KResult<()> { let _rename_lock = sb.backend.rename_lock.lock().await; @@ -340,7 +324,9 @@ impl InodeOps for DirectoryInode { // Find the new entry in the new directory (if it exists) let has_new = new_entries .iter() - .position(|(name, ino)| Some(*ino) == new_ino && *name == new_name) + .position(|(name, ino)| { + Some(*ino) == new_ino && *name == new_name + }) .is_some(); let now = Instant::now(); @@ -390,10 +376,7 @@ impl InodeOps for DirectoryInode { } async fn chmod( - &self, - _sb: SbUse, - inode: &InodeUse, - perm: Permission, + &self, _sb: SbUse, inode: &InodeUse, perm: Permission, ) -> KResult<()> { let mut info = inode.info.lock(); info.perm = perm; diff --git a/src/fs/tmpfs/file.rs b/src/fs/tmpfs/file.rs index aafae539..666c73dd 100644 --- a/src/fs/tmpfs/file.rs +++ b/src/fs/tmpfs/file.rs @@ -5,7 +5,9 @@ use super::TmpFs; use crate::io::{Buffer, Stream}; use crate::kernel::mem::{CachePage, PageCache, PageOffset}; use crate::kernel::timer::Instant; -use crate::kernel::vfs::inode::{Ino, InodeInfo, InodeOps, InodeUse, WriteOffset}; +use crate::kernel::vfs::inode::{ + Ino, InodeInfo, InodeOps, InodeUse, WriteOffset, +}; use crate::kernel::vfs::types::{DeviceId, Format, Mode, Permission}; use crate::kernel::vfs::{SbRef, SbUse}; use crate::prelude::KResult; @@ -13,7 +15,9 @@ use crate::prelude::KResult; pub struct FileInode; impl FileInode { - pub fn new(ino: Ino, sb: SbRef, size: usize, perm: Permission) -> InodeUse { + pub fn new( + ino: Ino, sb: SbRef, size: usize, perm: Permission, + ) -> InodeUse { let now = Instant::now(); InodeUse::new( @@ -39,22 +43,16 @@ impl InodeOps for FileInode { type SuperBlock = TmpFs; async fn read( - &self, - _: SbUse, - inode: &InodeUse, - buffer: &mut dyn Buffer, - offset: usize, + &self, _: SbUse, inode: &InodeUse, + buffer: &mut dyn Buffer, offset: usize, ) -> KResult { let _lock = inode.rwsem.read().await; inode.get_page_cache().read(buffer, offset).await } async fn write( - &self, - _: SbUse, - inode: &InodeUse, - stream: &mut dyn Stream, - offset: WriteOffset<'_>, + &self, _: SbUse, inode: &InodeUse, + stream: &mut dyn Stream, offset: WriteOffset<'_>, ) -> KResult { let _lock = inode.rwsem.write().await; @@ -88,10 +86,7 @@ impl InodeOps for FileInode { } async fn truncate( - &self, - _: SbUse, - inode: &InodeUse, - length: usize, + &self, _: SbUse, inode: &InodeUse, length: usize, ) -> KResult<()> { let _lock = inode.rwsem.write().await; @@ -105,10 +100,7 @@ impl InodeOps for FileInode { } async fn chmod( - &self, - _sb: SbUse, - inode: &InodeUse, - perm: Permission, + &self, _sb: SbUse, inode: &InodeUse, perm: Permission, ) -> KResult<()> { let mut info = inode.info.lock(); @@ -119,10 +111,7 @@ impl InodeOps for FileInode { } async fn read_page( - &self, - _: SbUse, - _: &InodeUse, - page: &mut CachePage, + &self, _: SbUse, _: &InodeUse, page: &mut CachePage, _: PageOffset, ) -> KResult<()> { page.lock().as_bytes_mut().fill(0); @@ -130,10 +119,7 @@ impl InodeOps for FileInode { } async fn write_page( - &self, - _: SbUse, - _: &InodeUse, - _: &mut CachePage, + &self, _: SbUse, _: &InodeUse, _: &mut CachePage, _: PageOffset, ) -> KResult<()> { // XXX: actually we should refuse to do the writeback. @@ -142,13 +128,9 @@ impl InodeOps for FileInode { } async fn write_begin<'a>( - &self, - _: SbUse, - _: &InodeUse, - page_cache: &PageCache, - pages: &'a mut BTreeMap, - offset: usize, - _: usize, + &self, _: SbUse, _: &InodeUse, + page_cache: &PageCache, pages: &'a mut BTreeMap, + offset: usize, _: usize, ) -> KResult<&'a mut CachePage> { // TODO: Remove dependency on `page_cache`. page_cache @@ -157,13 +139,8 @@ impl InodeOps for FileInode { } async fn write_end( - &self, - _: SbUse, - inode: &InodeUse, - _: &PageCache, - _: &mut BTreeMap, - offset: usize, - _: usize, + &self, _: SbUse, inode: &InodeUse, _: &PageCache, + _: &mut BTreeMap, offset: usize, _: usize, copied: usize, ) -> KResult<()> { let now = Instant::now(); @@ -181,7 +158,9 @@ pub struct DeviceInode { } impl DeviceInode { - pub fn new(ino: Ino, sb: SbRef, mode: Mode, devid: DeviceId) -> InodeUse { + pub fn new( + ino: Ino, sb: SbRef, mode: Mode, devid: DeviceId, + ) -> InodeUse { let now = Instant::now(); InodeUse::new( @@ -207,10 +186,7 @@ impl InodeOps for DeviceInode { type SuperBlock = TmpFs; async fn chmod( - &self, - _sb: SbUse, - inode: &InodeUse, - perm: Permission, + &self, _sb: SbUse, inode: &InodeUse, perm: Permission, ) -> KResult<()> { let mut info = inode.info.lock(); info.perm = perm; @@ -219,7 +195,9 @@ impl InodeOps for DeviceInode { Ok(()) } - fn devid(&self, _: SbUse, _: &InodeUse) -> KResult { + fn devid( + &self, _: SbUse, _: &InodeUse, + ) -> KResult { Ok(self.devid) } } @@ -255,9 +233,7 @@ impl InodeOps for SymlinkInode { type SuperBlock = TmpFs; async fn readlink( - &self, - _sb: SbUse, - _inode: &InodeUse, + &self, _sb: SbUse, _inode: &InodeUse, buffer: &mut dyn Buffer, ) -> KResult { buffer diff --git a/src/fs/tmpfs/mod.rs b/src/fs/tmpfs/mod.rs index 62a0dfc2..5da2ccdf 100644 --- a/src/fs/tmpfs/mod.rs +++ b/src/fs/tmpfs/mod.rs @@ -54,7 +54,9 @@ struct TmpFsMountCreator; #[async_trait] impl MountCreator for TmpFsMountCreator { - async fn create_mount(&self, _source: &str, _flags: u64, mp: &Arc) -> KResult { + async fn create_mount( + &self, _source: &str, _flags: u64, mp: &Arc, + ) -> KResult { let (fs, root_inode) = TmpFs::create()?; Mount::new(mp, fs, root_inode) diff --git a/src/kernel/block.rs b/src/kernel/block.rs index be2146f8..6dd95613 100644 --- a/src/kernel/block.rs +++ b/src/kernel/block.rs @@ -71,13 +71,12 @@ impl Ord for BlockDevice { } } -static BLOCK_DEVICE_LIST: Spin>> = Spin::new(BTreeMap::new()); +static BLOCK_DEVICE_LIST: Spin>> = + Spin::new(BTreeMap::new()); impl BlockDevice { pub fn register_disk( - devid: DeviceId, - size: u64, - queue: Arc, + devid: DeviceId, size: u64, queue: Arc, ) -> KResult> { let device = Arc::new(Self { devid, @@ -108,14 +107,19 @@ impl BlockDevice { } } - pub fn register_partition(&self, idx: usize, offset: u64, size: u64) -> KResult> { + pub fn register_partition( + &self, idx: usize, offset: u64, size: u64, + ) -> KResult> { let queue = match &self.dev_type { BlockDeviceType::Disk { queue } => queue.clone(), BlockDeviceType::Partition { .. } => return Err(EINVAL), }; let device = Arc::new(BlockDevice { - devid: DeviceId::new(self.devid.major, self.devid.minor + idx as u16 + 1), + devid: DeviceId::new( + self.devid.major, + self.devid.minor + idx as u16 + 1, + ), sector_count: size, dev_type: BlockDeviceType::Partition { disk_dev: self.devid, @@ -136,7 +140,11 @@ impl BlockDevice { BlockDeviceType::Disk { .. } => { if let Ok(mbr_table) = MBRPartTable::from_disk(self).await { for (idx, partition) in mbr_table.partitions().enumerate() { - self.register_partition(idx, partition.lba_offset, partition.sector_count)?; + self.register_partition( + idx, + partition.lba_offset, + partition.sector_count, + )?; } } @@ -153,7 +161,9 @@ impl BlockDevice { /// - `req.sector` must be within the disk size /// - `req.buffer` must be enough to hold the data /// - pub async fn commit_request(&self, mut req: BlockDeviceRequest<'_>) -> KResult<()> { + pub async fn commit_request( + &self, mut req: BlockDeviceRequest<'_>, + ) -> KResult<()> { // Verify the request parameters. match &mut req { BlockDeviceRequest::Read { sector, count, .. } => { @@ -161,7 +171,9 @@ impl BlockDevice { return Err(EINVAL); } - if let BlockDeviceType::Partition { lba_offset, .. } = &self.dev_type { + if let BlockDeviceType::Partition { lba_offset, .. } = + &self.dev_type + { // Adjust the sector for partition offset. *sector += lba_offset; } @@ -171,7 +183,9 @@ impl BlockDevice { return Err(EINVAL); } - if let BlockDeviceType::Partition { lba_offset, .. } = &self.dev_type { + if let BlockDeviceType::Partition { lba_offset, .. } = + &self.dev_type + { // Adjust the sector for partition offset. *sector += lba_offset; } @@ -188,7 +202,9 @@ impl BlockDevice { /// # Arguments /// `offset` - offset in bytes /// - pub async fn read_some(&self, offset: usize, buffer: &mut dyn Buffer) -> KResult { + pub async fn read_some( + &self, offset: usize, buffer: &mut dyn Buffer, + ) -> KResult { let sector_start = offset as u64 / 512; let mut first_sector_offset = offset % 512; let nr_sectors = (first_sector_offset + buffer.total() + 511) / 512; @@ -196,7 +212,8 @@ impl BlockDevice { let nr_sectors_per_batch = self.queue().max_request_pages() / 2 * 2 * 8; let mut nr_filled = 0; - for (start, nr_batch) in Chunks::new(sector_start, nr_sectors as u64, nr_sectors_per_batch) + for (start, nr_batch) in + Chunks::new(sector_start, nr_sectors as u64, nr_sectors_per_batch) { let (page_slice, page, mut page_vec); match nr_batch { @@ -219,7 +236,8 @@ impl BlockDevice { let nr_pages = nr_huge_pages + nr_small_pages; page_vec = Vec::with_capacity(nr_pages); - page_vec.resize_with(nr_huge_pages, || Folio::alloc_order(2)); + page_vec + .resize_with(nr_huge_pages, || Folio::alloc_order(2)); page_vec.resize_with(nr_pages, || Folio::alloc()); page_slice = &page_vec; } diff --git a/src/kernel/block/mbr.rs b/src/kernel/block/mbr.rs index c5820679..ea2a1577 100644 --- a/src/kernel/block/mbr.rs +++ b/src/kernel/block/mbr.rs @@ -1,9 +1,7 @@ use super::{BlockDevice, PartTable, Partition}; -use crate::{ - io::UninitBuffer, - kernel::constants::{EIO, ENODEV}, - prelude::KResult, -}; +use crate::io::UninitBuffer; +use crate::kernel::constants::{EIO, ENODEV}; +use crate::prelude::KResult; #[repr(C)] #[derive(Clone, Copy)] diff --git a/src/kernel/mem/access.rs b/src/kernel/mem/access.rs index 328dcfbd..1ea03767 100644 --- a/src/kernel/mem/access.rs +++ b/src/kernel/mem/access.rs @@ -1,4 +1,5 @@ use core::ptr::NonNull; + use eonix_hal::mm::ArchPhysAccess; use eonix_mm::address::{PAddr, PhysAccess as _PhysAccess}; diff --git a/src/kernel/mem/address.rs b/src/kernel/mem/address.rs index 15f0a59d..e772f7db 100644 --- a/src/kernel/mem/address.rs +++ b/src/kernel/mem/address.rs @@ -21,7 +21,8 @@ impl VAddrExt for VAddr { impl VRangeExt for VRange { fn is_user(&self) -> bool { - !(self.end() > USER_SPACE_MEMORY_TOP || self.start() >= USER_SPACE_MEMORY_TOP) + !(self.end() > USER_SPACE_MEMORY_TOP + || self.start() >= USER_SPACE_MEMORY_TOP) } fn is_kernel(&self) -> bool { diff --git a/src/kernel/mem/folio.rs b/src/kernel/mem/folio.rs index 8ab4d6be..ac52e6bb 100644 --- a/src/kernel/mem/folio.rs +++ b/src/kernel/mem/folio.rs @@ -4,7 +4,9 @@ use core::ops::Deref; use core::ptr::NonNull; use core::sync::atomic::Ordering; -use eonix_mm::paging::{Folio as FolioTrait, FrameAlloc, GlobalFrameAlloc, Zone, PFN}; +use eonix_mm::paging::{ + Folio as FolioTrait, FrameAlloc, GlobalFrameAlloc, Zone, PFN, +}; use super::page_alloc::ZONE; use super::{GlobalPageAlloc, PhysAccess as _, RawPage}; @@ -123,7 +125,9 @@ impl Drop for Folio { fn drop(&mut self) { match self.refcount.fetch_sub(1, Ordering::AcqRel) { 0 => unreachable!("Refcount for an in-use page is 0"), - 1 => unsafe { GlobalPageAlloc::GLOBAL.dealloc_raw(self.0.as_mut()) }, + 1 => unsafe { + GlobalPageAlloc::GLOBAL.dealloc_raw(self.0.as_mut()) + }, _ => {} } } @@ -149,14 +153,20 @@ impl LockedFolio<'_> { pub fn as_bytes(&self) -> &[u8] { unsafe { // SAFETY: `self.start()` points to valid memory of length `self.len()`. - core::slice::from_raw_parts(self.start().as_ptr().as_ptr(), self.len()) + core::slice::from_raw_parts( + self.start().as_ptr().as_ptr(), + self.len(), + ) } } pub fn as_bytes_mut(&mut self) -> &mut [u8] { unsafe { // SAFETY: `self.start()` points to valid memory of length `self.len()`. - core::slice::from_raw_parts_mut(self.start().as_ptr().as_ptr(), self.len()) + core::slice::from_raw_parts_mut( + self.start().as_ptr().as_ptr(), + self.len(), + ) } } } diff --git a/src/kernel/mem/mm_list/mapping.rs b/src/kernel/mem/mm_list/mapping.rs index 2b837ae7..349e7cce 100644 --- a/src/kernel/mem/mm_list/mapping.rs +++ b/src/kernel/mem/mm_list/mapping.rs @@ -22,7 +22,9 @@ pub enum Mapping { } impl FileMapping { - pub fn new(page_cache: Arc, offset: usize, length: usize) -> Self { + pub fn new( + page_cache: Arc, offset: usize, length: usize, + ) -> Self { assert_eq!(offset & (PAGE_SIZE - 1), 0); Self { page_cache, diff --git a/src/kernel/mem/mm_list/page_table.rs b/src/kernel/mem/mm_list/page_table.rs index 8a2acc13..8dfaabc0 100644 --- a/src/kernel/mem/mm_list/page_table.rs +++ b/src/kernel/mem/mm_list/page_table.rs @@ -8,7 +8,9 @@ use eonix_mm::paging::{Folio, GlobalFrameAlloc}; use crate::kernel::mem::{FolioOwned, GlobalPageAlloc, PhysAccess}; #[repr(transparent)] -pub struct KernelPageTable(PageTable<'static, ArchPagingMode, GlobalPageAlloc, PageAccessImpl>); +pub struct KernelPageTable( + PageTable<'static, ArchPagingMode, GlobalPageAlloc, PageAccessImpl>, +); impl KernelPageTable { pub fn new() -> Self { @@ -32,7 +34,8 @@ impl KernelPageTable { } impl Deref for KernelPageTable { - type Target = PageTable<'static, ArchPagingMode, GlobalPageAlloc, PageAccessImpl>; + type Target = + PageTable<'static, ArchPagingMode, GlobalPageAlloc, PageAccessImpl>; fn deref(&self) -> &Self::Target { &self.0 diff --git a/src/kernel/mem/page_cache.rs b/src/kernel/mem/page_cache.rs index 3fe33d5b..4328a044 100644 --- a/src/kernel/mem/page_cache.rs +++ b/src/kernel/mem/page_cache.rs @@ -32,7 +32,9 @@ impl PageOffset { Self((offset + PAGE_SIZE - 1) >> PAGE_SIZE_BITS) } - pub fn iter_till(self, end: PageOffset) -> impl Iterator { + pub fn iter_till( + self, end: PageOffset, + ) -> impl Iterator { (self.0..end.0).map(PageOffset) } @@ -100,10 +102,10 @@ impl PageCache { } pub fn get_page_locked<'a>( - &self, - pages: &'a mut BTreeMap, + &self, pages: &'a mut BTreeMap, pgoff: PageOffset, - ) -> impl Future> + Send + use<'_, 'a> { + ) -> impl Future> + Send + use<'_, 'a> + { async move { match pages.entry(pgoff) { Entry::Occupied(ent) => Ok(ent.into_mut()), @@ -122,7 +124,9 @@ impl PageCache { } // TODO: Remove this. - pub async fn with_page(&self, pgoff: PageOffset, func: impl FnOnce(&CachePage)) -> KResult<()> { + pub async fn with_page( + &self, pgoff: PageOffset, func: impl FnOnce(&CachePage), + ) -> KResult<()> { let mut pages = self.pages.lock().await; if pgoff > PageOffset::from_byte_ceil(self.len()) { return Err(EINVAL); @@ -135,7 +139,9 @@ impl PageCache { Ok(()) } - pub async fn read(&self, buffer: &mut dyn Buffer, mut offset: usize) -> KResult { + pub async fn read( + &self, buffer: &mut dyn Buffer, mut offset: usize, + ) -> KResult { let mut pages = self.pages.lock().await; let total_len = self.len(); @@ -156,7 +162,10 @@ impl PageCache { let data_len = real_end - offset; if buffer - .fill(&page.lock().as_bytes()[inner_offset..inner_offset + data_len])? + .fill( + &page.lock().as_bytes() + [inner_offset..inner_offset + data_len], + )? .should_stop() || buffer.available() == 0 { @@ -169,7 +178,9 @@ impl PageCache { Ok(buffer.wrote()) } - pub async fn write(&self, stream: &mut dyn Stream, mut offset: usize) -> KResult { + pub async fn write( + &self, stream: &mut dyn Stream, mut offset: usize, + ) -> KResult { let mut pages = self.pages.lock().await; let mut total_written = 0; diff --git a/src/kernel/pcie/device.rs b/src/kernel/pcie/device.rs index 2a8f150d..b3a07eb0 100644 --- a/src/kernel/pcie/device.rs +++ b/src/kernel/pcie/device.rs @@ -47,7 +47,9 @@ pub struct PciMemoryAllocator { } impl SegmentGroup { - pub fn new(id: usize, bus_start: u8, bus_end: u8, base_address: PAddr) -> Self { + pub fn new( + id: usize, bus_start: u8, bus_end: u8, base_address: PAddr, + ) -> Self { Self { id, bus_range: bus_start..=bus_end, @@ -79,7 +81,9 @@ impl SegmentGroup { .flatten() } - pub fn get_conf_space(&self, bus: u8, device: u8, function: u8) -> Option { + pub fn get_conf_space( + &self, bus: u8, device: u8, function: u8, + ) -> Option { if self.bus_range.contains(&bus) { Some(ConfigSpace { bus, @@ -129,8 +133,7 @@ impl ConfigSpace { impl PCIDevice<'static> { pub fn new( - segment_group: SegmentGroup, - config_space: ConfigSpace, + segment_group: SegmentGroup, config_space: ConfigSpace, header: Header<'static>, ) -> Arc { Arc::new(PCIDevice { @@ -156,31 +159,37 @@ impl PCIDevice<'_> { if let Header::Endpoint(header) = self.header { for mut bar in header.bars().iter() { match bar.get() { - Bar::MemoryMapped32 { base: None, size } => bar.set(Bar::MemoryMapped32 { - base: Some( - allocator - .allocate(size as usize) - .expect("Failed to allocate BAR memory"), - ), - size, - }), - Bar::MemoryMapped64 { base: None, size } => bar.set(Bar::MemoryMapped64 { - base: Some( - allocator - .allocate(size as usize) - .map(|base| NonZero::new(base.get() as u64)) - .flatten() - .expect("Failed to allocate BAR memory"), - ), - size, - }), + Bar::MemoryMapped32 { base: None, size } => { + bar.set(Bar::MemoryMapped32 { + base: Some( + allocator + .allocate(size as usize) + .expect("Failed to allocate BAR memory"), + ), + size, + }) + } + Bar::MemoryMapped64 { base: None, size } => { + bar.set(Bar::MemoryMapped64 { + base: Some( + allocator + .allocate(size as usize) + .map(|base| NonZero::new(base.get() as u64)) + .flatten() + .expect("Failed to allocate BAR memory"), + ), + size, + }) + } _ => {} } } } self.header.command().set( - Command::IO_ACCESS_ENABLE | Command::MEMORY_ACCESS_ENABLE | Command::BUS_MASTER_ENABLE, + Command::IO_ACCESS_ENABLE + | Command::MEMORY_ACCESS_ENABLE + | Command::BUS_MASTER_ENABLE, ); } diff --git a/src/kernel/pcie/driver.rs b/src/kernel/pcie/driver.rs index eebaa896..f3216173 100644 --- a/src/kernel/pcie/driver.rs +++ b/src/kernel/pcie/driver.rs @@ -1,24 +1,26 @@ -use super::{ - device::{PCIDevice, PCIE_DEVICES}, - error::PciError, -}; -use crate::{kernel::constants::EEXIST, KResult}; -use alloc::{ - boxed::Box, - collections::btree_map::{self, BTreeMap}, - sync::Arc, -}; +use alloc::boxed::Box; +use alloc::collections::btree_map::{self, BTreeMap}; +use alloc::sync::Arc; + use async_trait::async_trait; use eonix_sync::Spin; -static PCIE_DRIVERS: Spin>> = Spin::new(BTreeMap::new()); +use super::device::{PCIDevice, PCIE_DEVICES}; +use super::error::PciError; +use crate::kernel::constants::EEXIST; +use crate::KResult; + +static PCIE_DRIVERS: Spin>> = + Spin::new(BTreeMap::new()); #[async_trait] pub trait PCIDriver: Send + Sync { fn vendor_id(&self) -> u16; fn device_id(&self) -> u16; - async fn handle_device(&self, device: Arc>) -> Result<(), PciError>; + async fn handle_device( + &self, device: Arc>, + ) -> Result<(), PciError>; } pub async fn register_driver(driver: impl PCIDriver + 'static) -> KResult<()> { @@ -27,7 +29,9 @@ pub async fn register_driver(driver: impl PCIDriver + 'static) -> KResult<()> { let driver = Arc::new(driver); match PCIE_DRIVERS.lock().entry(index) { - btree_map::Entry::Vacant(vacant_entry) => vacant_entry.insert(driver.clone()), + btree_map::Entry::Vacant(vacant_entry) => { + vacant_entry.insert(driver.clone()) + } btree_map::Entry::Occupied(_) => Err(EEXIST)?, }; diff --git a/src/kernel/pcie/error.rs b/src/kernel/pcie/error.rs index 120904c8..501dfb11 100644 --- a/src/kernel/pcie/error.rs +++ b/src/kernel/pcie/error.rs @@ -1,6 +1,7 @@ -use crate::kernel::constants::{EFAULT, EINVAL, ENOENT}; use acpi::AcpiError; +use crate::kernel::constants::{EFAULT, EINVAL, ENOENT}; + #[derive(Debug)] pub struct PciError(u32); diff --git a/src/kernel/pcie/header.rs b/src/kernel/pcie/header.rs index 0a44ea28..e140845d 100644 --- a/src/kernel/pcie/header.rs +++ b/src/kernel/pcie/header.rs @@ -216,14 +216,18 @@ where impl CommonHeader { pub fn command(&self) -> Register<'_, Command> { Register { - register: unsafe { AtomicU16::from_ptr((&raw const self._command) as *mut u16) }, + register: unsafe { + AtomicU16::from_ptr((&raw const self._command) as *mut u16) + }, _phantom: PhantomData, } } pub fn status(&self) -> Register<'_, Status> { Register { - register: unsafe { AtomicU16::from_ptr((&raw const self._status) as *mut u16) }, + register: unsafe { + AtomicU16::from_ptr((&raw const self._status) as *mut u16) + }, _phantom: PhantomData, } } @@ -290,7 +294,8 @@ impl BarsEntry<'_> { } if is_64bit { - let reg2 = self.reg2.expect("64-bit BARs must have a second register"); + let reg2 = + self.reg2.expect("64-bit BARs must have a second register"); let reg2_value = reg2.load(Ordering::Relaxed); @@ -301,7 +306,9 @@ impl BarsEntry<'_> { let size = (self.reg1.load(Ordering::Relaxed) as u64 & !0xf) | ((reg2.load(Ordering::Relaxed) as u64) << 32); let bar = NonZero::new(size).map(|size| Bar::MemoryMapped64 { - base: NonZero::new((reg1_value as u64 & !0xf) | ((reg2_value as u64) << 32)), + base: NonZero::new( + (reg1_value as u64 & !0xf) | ((reg2_value as u64) << 32), + ), size: !size.get() + 1, }); @@ -345,7 +352,8 @@ impl BarsEntry<'_> { memory_barrier(); } Bar::MemoryMapped64 { base, .. } => { - let reg2 = self.reg2.expect("64-bit BARs must have a second register"); + let reg2 = + self.reg2.expect("64-bit BARs must have a second register"); let old_value1 = self.reg1.load(Ordering::Relaxed); let old_value2 = reg2.load(Ordering::Relaxed); let base = base.map_or(0, NonZero::get) & !0xf; diff --git a/src/kernel/pcie/init.rs b/src/kernel/pcie/init.rs index 74a490b4..3beff60f 100644 --- a/src/kernel/pcie/init.rs +++ b/src/kernel/pcie/init.rs @@ -16,16 +16,20 @@ struct AcpiHandlerImpl; impl AcpiHandler for AcpiHandlerImpl { unsafe fn map_physical_region( - &self, - physical_address: usize, - size: usize, + &self, physical_address: usize, size: usize, ) -> PhysicalMapping { let virtual_address = unsafe { // SAFETY: `physical_address` is guaranteed to be valid by the caller. PAddr::from_val(physical_address).as_ptr() }; - PhysicalMapping::new(physical_address, virtual_address, size, size, self.clone()) + PhysicalMapping::new( + physical_address, + virtual_address, + size, + size, + self.clone(), + ) } fn unmap_physical_region(_: &PhysicalMapping) {} @@ -41,7 +45,8 @@ pub fn init_pcie() -> Result<(), PciError> { AcpiTables::search_for_rsdp_bios(AcpiHandlerImpl)? }; - let mut allocator = PciMemoryAllocator::new(PRange::from(PAddr::from(0))); + let mut allocator = + PciMemoryAllocator::new(PRange::from(PAddr::from(0))); let conf_regions = PciConfigRegions::new(&acpi_tables)?; for region in conf_regions.iter() { @@ -49,11 +54,16 @@ pub fn init_pcie() -> Result<(), PciError> { for config_space in segment_group.iter() { if let Some(header) = config_space.header() { - let pci_device = PCIDevice::new(segment_group.clone(), config_space, header); + let pci_device = PCIDevice::new( + segment_group.clone(), + config_space, + header, + ); pci_device.configure_io(&mut allocator); - match PCIE_DEVICES.lock().entry(pci_device.vendor_device()) { + match PCIE_DEVICES.lock().entry(pci_device.vendor_device()) + { Entry::Vacant(vacant_entry) => { vacant_entry.insert(vec![pci_device]); } @@ -106,26 +116,34 @@ pub fn init_pcie() -> Result<(), PciError> { let reg = pcie_node.reg().ok_or(EINVAL)?.next().ok_or(EINVAL)?; let mmio_range = - PRange::from(PAddr::from(reg.starting_address as usize)).grow(reg.size.ok_or(EINVAL)?); + PRange::from(PAddr::from(reg.starting_address as usize)) + .grow(reg.size.ok_or(EINVAL)?); if bus_range.value.len() != 8 { Err(EINVAL)?; } - let bus_start = u32::from_be_bytes(bus_range.value[..4].try_into().unwrap()); - let bus_end = u32::from_be_bytes(bus_range.value[4..].try_into().unwrap()); + let bus_start = + u32::from_be_bytes(bus_range.value[..4].try_into().unwrap()); + let bus_end = + u32::from_be_bytes(bus_range.value[4..].try_into().unwrap()); - if bus_start > u8::MAX as u32 || bus_end > u8::MAX as u32 || bus_start > bus_end { + if bus_start > u8::MAX as u32 + || bus_end > u8::MAX as u32 + || bus_start > bus_end + { Err(EINVAL)?; } let bus_start = bus_start as u8; let bus_end = bus_end as u8; - let segment_group = SegmentGroup::new(0, bus_start, bus_end, mmio_range.start()); + let segment_group = + SegmentGroup::new(0, bus_start, bus_end, mmio_range.start()); for config_space in segment_group.iter() { if let Some(header) = config_space.header() { - let pci_device = PCIDevice::new(segment_group.clone(), config_space, header); + let pci_device = + PCIDevice::new(segment_group.clone(), config_space, header); pci_device.configure_io(&mut allocator); match PCIE_DEVICES.lock().entry(pci_device.vendor_device()) { diff --git a/src/kernel/rtc/mod.rs b/src/kernel/rtc/mod.rs index f668ba6f..3932493c 100644 --- a/src/kernel/rtc/mod.rs +++ b/src/kernel/rtc/mod.rs @@ -1,10 +1,11 @@ +use alloc::sync::Arc; use core::time::Duration; -use super::timer::{Instant, Ticks}; -use alloc::sync::Arc; use eonix_log::println_warn; use eonix_sync::Spin; +use super::timer::{Instant, Ticks}; + static RTC: Spin>> = Spin::new(None); pub trait RealTimeClock: Send + Sync { @@ -15,7 +16,8 @@ impl Instant { pub fn now() -> Instant { RTC.lock().as_ref().map(|rtc| rtc.now()).unwrap_or_else(|| { let since_boot = Ticks::since_boot(); - let pseudo_now = Duration::from_secs((55 * 365 + 30) * 24 * 3600) + since_boot; + let pseudo_now = + Duration::from_secs((55 * 365 + 30) * 24 * 3600) + since_boot; Instant::new(pseudo_now.as_secs(), pseudo_now.subsec_nanos()) }) diff --git a/src/kernel/syscall.rs b/src/kernel/syscall.rs index d06c5d88..4ce97611 100644 --- a/src/kernel/syscall.rs +++ b/src/kernel/syscall.rs @@ -37,7 +37,10 @@ pub(self) struct RawSyscallHandler { ThreadAlloc<'alloc>, [usize; 6], ) -> Pin< - Box> + Send + 'thd, ThreadAlloc<'alloc>>, + Box< + dyn Future> + Send + 'thd, + ThreadAlloc<'alloc>, + >, >, name: &'static str, } @@ -48,7 +51,10 @@ pub struct SyscallHandler { ThreadAlloc<'alloc>, [usize; 6], ) -> Pin< - Box> + Send + 'thd, ThreadAlloc<'alloc>>, + Box< + dyn Future> + Send + 'thd, + ThreadAlloc<'alloc>, + >, >, pub name: &'static str, } @@ -285,35 +291,38 @@ impl core::fmt::Debug for UserMut { } } -static SYSCALL_HANDLERS: LazyLock<[Option; MAX_SYSCALL_NO]> = LazyLock::new(|| { - // DO NOT TOUCH THESE FUNCTIONS!!! - // THEY ARE USED FOR KEEPING THE OBJECTS NOT STRIPPED BY THE LINKER!!! - file_rw::keep_alive(); - mm::keep_alive(); - net::keep_alive(); - procops::keep_alive(); - sysinfo::keep_alive(); - - let raw_handlers_size_byte = extern_symbol_addr!(RAW_SYSCALL_HANDLERS_SIZE); - assert!(raw_handlers_size_byte % size_of::() == 0); - - let raw_handlers_count = raw_handlers_size_byte / size_of::(); - - let raw_handlers = unsafe { - core::slice::from_raw_parts( - extern_symbol_addr!(RAW_SYSCALL_HANDLERS, RawSyscallHandler), - raw_handlers_count, - ) - }; - - let mut handlers = [const { None }; MAX_SYSCALL_NO]; - - for &RawSyscallHandler { no, handler, name } in raw_handlers.iter() { - handlers[no] = Some(SyscallHandler { handler, name }) - } +static SYSCALL_HANDLERS: LazyLock<[Option; MAX_SYSCALL_NO]> = + LazyLock::new(|| { + // DO NOT TOUCH THESE FUNCTIONS!!! + // THEY ARE USED FOR KEEPING THE OBJECTS NOT STRIPPED BY THE LINKER!!! + file_rw::keep_alive(); + mm::keep_alive(); + net::keep_alive(); + procops::keep_alive(); + sysinfo::keep_alive(); + + let raw_handlers_size_byte = + extern_symbol_addr!(RAW_SYSCALL_HANDLERS_SIZE); + assert!(raw_handlers_size_byte % size_of::() == 0); + + let raw_handlers_count = + raw_handlers_size_byte / size_of::(); + + let raw_handlers = unsafe { + core::slice::from_raw_parts( + extern_symbol_addr!(RAW_SYSCALL_HANDLERS, RawSyscallHandler), + raw_handlers_count, + ) + }; + + let mut handlers = [const { None }; MAX_SYSCALL_NO]; + + for &RawSyscallHandler { no, handler, name } in raw_handlers.iter() { + handlers[no] = Some(SyscallHandler { handler, name }) + } - handlers -}); + handlers + }); pub fn syscall_handlers() -> &'static [Option] { SYSCALL_HANDLERS.as_ref() diff --git a/src/kernel/syscall/file_rw.rs b/src/kernel/syscall/file_rw.rs index 8ac9c22a..5810a460 100644 --- a/src/kernel/syscall/file_rw.rs +++ b/src/kernel/syscall/file_rw.rs @@ -41,10 +41,7 @@ impl FromSyscallArg for AtFlags { } async fn dentry_from( - thread: &Thread, - dirfd: FD, - pathname: User, - follow_symlink: bool, + thread: &Thread, dirfd: FD, pathname: User, follow_symlink: bool, ) -> KResult> { let path = UserString::new(pathname)?; @@ -87,10 +84,7 @@ async fn read(fd: FD, buffer: UserMut, bufsize: usize) -> KResult { #[eonix_macros::define_syscall(SYS_PREAD64)] async fn pread64( - fd: FD, - buffer: UserMut, - bufsize: usize, - offset: usize, + fd: FD, buffer: UserMut, bufsize: usize, offset: usize, ) -> KResult { let mut buffer = UserBuffer::new(buffer, bufsize)?; @@ -117,10 +111,7 @@ async fn write(fd: FD, buffer: User, count: usize) -> KResult { #[eonix_macros::define_syscall(SYS_PWRITE64)] async fn pwrite64( - fd: FD, - buffer: User, - count: usize, - offset: usize, + fd: FD, buffer: User, count: usize, offset: usize, ) -> KResult { let buffer = CheckedUserPointer::new(buffer, count)?; let mut stream = buffer.into_stream(); @@ -135,10 +126,7 @@ async fn pwrite64( #[eonix_macros::define_syscall(SYS_OPENAT)] async fn openat( - dirfd: FD, - pathname: User, - flags: OpenFlags, - mode: Mode, + dirfd: FD, pathname: User, flags: OpenFlags, mode: Mode, ) -> KResult { let dentry = dentry_from(thread, dirfd, pathname, flags.follow_symlink()).await?; @@ -195,9 +183,7 @@ async fn pipe(pipe_fd: UserMut<[FD; 2]>) -> KResult<()> { #[cfg(target_arch = "x86_64")] #[eonix_macros::define_syscall(SYS_GETDENTS)] async fn getdents( - fd: FD, - buffer: UserMut, - bufsize: usize, + fd: FD, buffer: UserMut, bufsize: usize, ) -> KResult { let mut buffer = UserBuffer::new(buffer, bufsize)?; @@ -213,9 +199,7 @@ async fn getdents( #[eonix_macros::define_syscall(SYS_GETDENTS64)] async fn getdents64( - fd: FD, - buffer: UserMut, - bufsize: usize, + fd: FD, buffer: UserMut, bufsize: usize, ) -> KResult { let mut buffer = UserBuffer::new(buffer, bufsize)?; @@ -234,10 +218,7 @@ async fn getdents64( )] #[cfg_attr(target_arch = "x86_64", eonix_macros::define_syscall(SYS_FSTATAT64))] async fn newfstatat( - dirfd: FD, - pathname: User, - statbuf: UserMut, - flags: AtFlags, + dirfd: FD, pathname: User, statbuf: UserMut, flags: AtFlags, ) -> KResult<()> { let dentry = if flags.at_empty_path() { let file = thread.files.get(dirfd).ok_or(EBADF)?; @@ -268,10 +249,7 @@ async fn newfstat(fd: FD, statbuf: UserMut) -> KResult<()> { #[eonix_macros::define_syscall(SYS_STATX)] async fn statx( - dirfd: FD, - pathname: User, - flags: AtFlags, - mask: u32, + dirfd: FD, pathname: User, flags: AtFlags, mask: u32, buffer: UserMut, ) -> KResult<()> { if !flags.statx_default_sync() { @@ -341,9 +319,7 @@ async fn unlink(pathname: User) -> KResult<()> { #[eonix_macros::define_syscall(SYS_SYMLINKAT)] async fn symlinkat( - target: User, - dirfd: FD, - linkpath: User, + target: User, dirfd: FD, linkpath: User, ) -> KResult<()> { let target = UserString::new(target)?; let dentry = dentry_from(thread, dirfd, linkpath, false).await?; @@ -379,10 +355,7 @@ impl UserDeviceId { #[eonix_macros::define_syscall(SYS_MKNODAT)] async fn mknodat( - dirfd: FD, - pathname: User, - mut mode: Mode, - dev: UserDeviceId, + dirfd: FD, pathname: User, mut mode: Mode, dev: UserDeviceId, ) -> KResult<()> { if !mode.is_blk() && !mode.is_chr() { return Err(EINVAL); @@ -397,19 +370,14 @@ async fn mknodat( #[cfg(target_arch = "x86_64")] #[eonix_macros::define_syscall(SYS_MKNOD)] async fn mknod( - pathname: User, - mode: Mode, - dev: UserDeviceId, + pathname: User, mode: Mode, dev: UserDeviceId, ) -> KResult<()> { sys_mknodat(thread, FD::AT_FDCWD, pathname, mode, dev).await } #[eonix_macros::define_syscall(SYS_READLINKAT)] async fn readlinkat( - dirfd: FD, - pathname: User, - buffer: UserMut, - bufsize: usize, + dirfd: FD, pathname: User, buffer: UserMut, bufsize: usize, ) -> KResult { let dentry = dentry_from(thread, dirfd, pathname, false).await?; let mut buffer = UserBuffer::new(buffer, bufsize)?; @@ -420,18 +388,13 @@ async fn readlinkat( #[cfg(target_arch = "x86_64")] #[eonix_macros::define_syscall(SYS_READLINK)] async fn readlink( - pathname: User, - buffer: UserMut, - bufsize: usize, + pathname: User, buffer: UserMut, bufsize: usize, ) -> KResult { sys_readlinkat(thread, FD::AT_FDCWD, pathname, buffer, bufsize).await } async fn do_lseek( - thread: &Thread, - fd: FD, - offset: u64, - whence: u32, + thread: &Thread, fd: FD, offset: u64, whence: u32, ) -> KResult { let file = thread.files.get(fd).ok_or(EBADF)?; @@ -452,10 +415,7 @@ async fn lseek(fd: FD, offset: u64, whence: u32) -> KResult { #[cfg(target_arch = "x86_64")] #[eonix_macros::define_syscall(SYS_LLSEEK)] async fn llseek( - fd: FD, - offset_high: u32, - offset_low: u32, - result: UserMut, + fd: FD, offset_high: u32, offset_low: u32, result: UserMut, whence: u32, ) -> KResult<()> { let mut result = @@ -552,10 +512,7 @@ async fn writev(fd: FD, iov_user: User, iovcnt: u32) -> KResult { #[eonix_macros::define_syscall(SYS_FACCESSAT)] async fn faccessat( - dirfd: FD, - pathname: User, - _mode: u32, - flags: AtFlags, + dirfd: FD, pathname: User, _mode: u32, flags: AtFlags, ) -> KResult<()> { let dentry = if flags.at_empty_path() { let file = thread.files.get(dirfd).ok_or(EBADF)?; @@ -588,10 +545,7 @@ async fn access(pathname: User, mode: u32) -> KResult<()> { #[eonix_macros::define_syscall(SYS_SENDFILE64)] async fn sendfile64( - out_fd: FD, - in_fd: FD, - offset: UserMut, - count: usize, + out_fd: FD, in_fd: FD, offset: UserMut, count: usize, ) -> KResult { let in_file = thread.files.get(in_fd).ok_or(EBADF)?; let out_file = thread.files.get(out_fd).ok_or(EBADF)?; @@ -624,10 +578,7 @@ struct UserPollFd { } async fn do_poll( - thread: &Thread, - fds: UserMut, - nfds: u32, - _timeout: u32, + thread: &Thread, fds: UserMut, nfds: u32, _timeout: u32, ) -> KResult { match nfds { 0 => Ok(0), @@ -654,9 +605,7 @@ async fn do_poll( #[eonix_macros::define_syscall(SYS_PPOLL)] async fn ppoll( - fds: UserMut, - nfds: u32, - _timeout_ptr: User, + fds: UserMut, nfds: u32, _timeout_ptr: User, _sigmask: User, ) -> KResult { // TODO: Implement ppoll with signal mask and timeout @@ -665,12 +614,8 @@ async fn ppoll( #[eonix_macros::define_syscall(SYS_PSELECT6)] async fn pselect6( - nfds: u32, - _readfds: UserMut, - _writefds: UserMut, - _exceptfds: UserMut, - timeout: UserMut, - _sigmask: User<()>, + nfds: u32, _readfds: UserMut, _writefds: UserMut, + _exceptfds: UserMut, timeout: UserMut, _sigmask: User<()>, ) -> KResult { // According to [pthread6(2)](https://linux.die.net/man/2/pselect6): // Some code calls select() with all three sets empty, nfds zero, and @@ -698,20 +643,14 @@ async fn pselect6( #[cfg(target_arch = "x86_64")] #[eonix_macros::define_syscall(SYS_POLL)] async fn poll( - fds: UserMut, - nfds: u32, - timeout: u32, + fds: UserMut, nfds: u32, timeout: u32, ) -> KResult { do_poll(thread, fds, nfds, timeout).await } #[eonix_macros::define_syscall(SYS_FCHOWNAT)] async fn fchownat( - dirfd: FD, - pathname: User, - uid: u32, - gid: u32, - flags: AtFlags, + dirfd: FD, pathname: User, uid: u32, gid: u32, flags: AtFlags, ) -> KResult<()> { let dentry = dentry_from(thread, dirfd, pathname, !flags.no_follow()).await?; @@ -724,10 +663,7 @@ async fn fchownat( #[eonix_macros::define_syscall(SYS_FCHMODAT)] async fn fchmodat( - dirfd: FD, - pathname: User, - mode: Mode, - flags: AtFlags, + dirfd: FD, pathname: User, mode: Mode, flags: AtFlags, ) -> KResult<()> { let dentry = if flags.at_empty_path() { let file = thread.files.get(dirfd).ok_or(EBADF)?; @@ -750,10 +686,7 @@ async fn chmod(pathname: User, mode: Mode) -> KResult<()> { #[eonix_macros::define_syscall(SYS_UTIMENSAT)] async fn utimensat( - dirfd: FD, - pathname: User, - times: User, - flags: AtFlags, + dirfd: FD, pathname: User, times: User, flags: AtFlags, ) -> KResult<()> { let dentry = if flags.at_empty_path() { let file = thread.files.get(dirfd).ok_or(EBADF)?; @@ -780,11 +713,8 @@ async fn utimensat( #[eonix_macros::define_syscall(SYS_RENAMEAT2)] async fn renameat2( - old_dirfd: FD, - old_pathname: User, - new_dirfd: FD, - new_pathname: User, - flags: u32, + old_dirfd: FD, old_pathname: User, new_dirfd: FD, + new_pathname: User, flags: u32, ) -> KResult<()> { let flags = RenameFlags::from_bits(flags).ok_or(EINVAL)?; diff --git a/src/kernel/syscall/mm.rs b/src/kernel/syscall/mm.rs index 825440ef..bd756048 100644 --- a/src/kernel/syscall/mm.rs +++ b/src/kernel/syscall/mm.rs @@ -4,7 +4,9 @@ use eonix_mm::paging::PAGE_SIZE; use posix_types::syscall_no::*; use super::FromSyscallArg; -use crate::kernel::constants::{UserMmapFlags, UserMmapProtocol, EBADF, EINVAL}; +use crate::kernel::constants::{ + UserMmapFlags, UserMmapProtocol, EBADF, EINVAL, +}; use crate::kernel::mem::{FileMapping, Mapping, Permission}; use crate::kernel::task::Thread; use crate::kernel::vfs::filearray::FD; @@ -34,13 +36,8 @@ fn check_impl(condition: bool, err: u32) -> KResult<()> { } async fn do_mmap2( - thread: &Thread, - addr: usize, - len: usize, - prot: UserMmapProtocol, - flags: UserMmapFlags, - fd: FD, - pgoffset: usize, + thread: &Thread, addr: usize, len: usize, prot: UserMmapProtocol, + flags: UserMmapFlags, fd: FD, pgoffset: usize, ) -> KResult { let addr = VAddr::from(addr); if !addr.is_page_aligned() || pgoffset % PAGE_SIZE != 0 || len == 0 { @@ -97,12 +94,8 @@ async fn do_mmap2( #[cfg(any(target_arch = "riscv64", target_arch = "loongarch64"))] #[eonix_macros::define_syscall(SYS_MMAP)] async fn mmap( - addr: usize, - len: usize, - prot: UserMmapProtocol, - flags: UserMmapFlags, - fd: FD, - offset: usize, + addr: usize, len: usize, prot: UserMmapProtocol, flags: UserMmapFlags, + fd: FD, offset: usize, ) -> KResult { do_mmap2(thread, addr, len, prot, flags, fd, offset).await } @@ -110,12 +103,8 @@ async fn mmap( #[cfg(target_arch = "x86_64")] #[eonix_macros::define_syscall(SYS_MMAP2)] async fn mmap2( - addr: usize, - len: usize, - prot: UserMmapProtocol, - flags: UserMmapFlags, - fd: FD, - pgoffset: usize, + addr: usize, len: usize, prot: UserMmapProtocol, flags: UserMmapFlags, + fd: FD, pgoffset: usize, ) -> KResult { do_mmap2(thread, addr, len, prot, flags, fd, pgoffset).await } @@ -143,7 +132,9 @@ async fn madvise(_addr: usize, _len: usize, _advice: u32) -> KResult<()> { } #[eonix_macros::define_syscall(SYS_MPROTECT)] -async fn mprotect(addr: usize, len: usize, prot: UserMmapProtocol) -> KResult<()> { +async fn mprotect( + addr: usize, len: usize, prot: UserMmapProtocol, +) -> KResult<()> { let addr = VAddr::from(addr); if !addr.is_page_aligned() || len == 0 { return Err(EINVAL); @@ -154,15 +145,11 @@ async fn mprotect(addr: usize, len: usize, prot: UserMmapProtocol) -> KResult<() thread .process .mm_list - .protect( - addr, - len, - Permission { - read: prot.contains(UserMmapProtocol::PROT_READ), - write: prot.contains(UserMmapProtocol::PROT_WRITE), - execute: prot.contains(UserMmapProtocol::PROT_EXEC), - }, - ) + .protect(addr, len, Permission { + read: prot.contains(UserMmapProtocol::PROT_READ), + write: prot.contains(UserMmapProtocol::PROT_WRITE), + execute: prot.contains(UserMmapProtocol::PROT_EXEC), + }) .await } diff --git a/src/kernel/syscall/net.rs b/src/kernel/syscall/net.rs index 41ac58e6..4ad3c8ef 100644 --- a/src/kernel/syscall/net.rs +++ b/src/kernel/syscall/net.rs @@ -1,9 +1,12 @@ +use posix_types::syscall_no::*; + use crate::kernel::constants::EINVAL; use crate::prelude::*; -use posix_types::syscall_no::*; #[eonix_macros::define_syscall(SYS_SOCKET)] -async fn socket(_domain: u32, _socket_type: u32, _protocol: u32) -> KResult { +async fn socket( + _domain: u32, _socket_type: u32, _protocol: u32, +) -> KResult { Err(EINVAL) } diff --git a/src/kernel/syscall/sysinfo.rs b/src/kernel/syscall/sysinfo.rs index 69316b2a..e0df2e31 100644 --- a/src/kernel/syscall/sysinfo.rs +++ b/src/kernel/syscall/sysinfo.rs @@ -1,18 +1,15 @@ -use crate::{ - io::Buffer as _, - kernel::{ - constants::{CLOCK_MONOTONIC, CLOCK_REALTIME, CLOCK_REALTIME_COARSE, EINTR, EINVAL}, - syscall::UserMut, - task::Thread, - timer::{Instant, Ticks}, - user::{UserBuffer, UserPointerMut}, - }, - prelude::*, -}; -use posix_types::{ - stat::{TimeSpec, TimeVal}, - syscall_no::*, +use posix_types::stat::{TimeSpec, TimeVal}; +use posix_types::syscall_no::*; + +use crate::io::Buffer as _; +use crate::kernel::constants::{ + CLOCK_MONOTONIC, CLOCK_REALTIME, CLOCK_REALTIME_COARSE, EINTR, EINVAL, }; +use crate::kernel::syscall::UserMut; +use crate::kernel::task::Thread; +use crate::kernel::timer::{Instant, Ticks}; +use crate::kernel::user::{UserBuffer, UserPointerMut}; +use crate::prelude::*; #[derive(Clone, Copy)] struct NewUTSName { @@ -63,7 +60,9 @@ async fn newuname(buffer: UserMut) -> KResult<()> { } #[eonix_macros::define_syscall(SYS_GETTIMEOFDAY)] -async fn gettimeofday(timeval: UserMut, timezone: UserMut<()>) -> KResult<()> { +async fn gettimeofday( + timeval: UserMut, timezone: UserMut<()>, +) -> KResult<()> { if !timezone.is_null() { return Err(EINVAL); } @@ -82,7 +81,9 @@ async fn gettimeofday(timeval: UserMut, timezone: UserMut<()>) -> KResu Ok(()) } -fn do_clock_gettime64(_thread: &Thread, clock_id: u32, timespec: UserMut) -> KResult<()> { +fn do_clock_gettime64( + _thread: &Thread, clock_id: u32, timespec: UserMut, +) -> KResult<()> { let timespec = UserPointerMut::new(timespec)?; match clock_id { @@ -107,13 +108,17 @@ fn do_clock_gettime64(_thread: &Thread, clock_id: u32, timespec: UserMut) -> KResult<()> { +async fn clock_gettime( + clock_id: u32, timespec: UserMut, +) -> KResult<()> { do_clock_gettime64(thread, clock_id, timespec) } #[cfg(target_arch = "x86_64")] #[eonix_macros::define_syscall(SYS_CLOCK_GETTIME64)] -async fn clock_gettime64(clock_id: u32, timespec: UserMut) -> KResult<()> { +async fn clock_gettime64( + clock_id: u32, timespec: UserMut, +) -> KResult<()> { do_clock_gettime64(thread, clock_id, timespec) } @@ -176,7 +181,9 @@ async fn times(tms: UserMut) -> KResult<()> { } #[eonix_macros::define_syscall(SYS_GETRANDOM)] -async fn get_random(buf: UserMut, len: usize, flags: u32) -> KResult { +async fn get_random( + buf: UserMut, len: usize, flags: u32, +) -> KResult { if flags != 0 { return Err(EINVAL); } diff --git a/src/kernel/task/futex.rs b/src/kernel/task/futex.rs index 4dd57615..bfc1810c 100644 --- a/src/kernel/task/futex.rs +++ b/src/kernel/task/futex.rs @@ -5,7 +5,9 @@ use core::pin::pin; use bitflags::bitflags; use eonix_mm::address::Addr; use eonix_sync::{LazyLock, Mutex, MutexGuard, WaitList}; -use intrusive_collections::{intrusive_adapter, KeyAdapter, RBTree, RBTreeAtomicLink}; +use intrusive_collections::{ + intrusive_adapter, KeyAdapter, RBTree, RBTreeAtomicLink, +}; use super::Thread; use crate::kernel::constants::{EAGAIN, EINVAL}; @@ -162,10 +164,7 @@ impl FutexBucket { } pub async fn futex_wait( - uaddr: usize, - pid: Option, - expected_val: u32, - _timeout: Option, + uaddr: usize, pid: Option, expected_val: u32, _timeout: Option, ) -> KResult<()> { let futex_key = FutexKey::new(uaddr, pid); @@ -188,7 +187,9 @@ pub async fn futex_wait( Ok(()) } -pub async fn futex_wake(uaddr: usize, pid: Option, max_wake_count: u32) -> KResult { +pub async fn futex_wake( + uaddr: usize, pid: Option, max_wake_count: u32, +) -> KResult { let futex_key = FutexKey::new(uaddr, pid); let (_, futex_bucket_ref) = FUTEX_TABLE.get_bucket(&futex_key); @@ -212,8 +213,7 @@ pub async fn futex_wake(uaddr: usize, pid: Option, max_wake_count: u32) -> // should make sure that two keys in different buckets // make the lock order to avoid deadlocks async fn double_lock_bucket( - futex_key0: FutexKey, - futex_key1: FutexKey, + futex_key0: FutexKey, futex_key1: FutexKey, ) -> ( MutexGuard<'static, FutexBucket>, MutexGuard<'static, FutexBucket>, @@ -233,17 +233,15 @@ async fn double_lock_bucket( } async fn futex_requeue( - uaddr: usize, - pid: Option, - wake_count: u32, - requeue_uaddr: usize, + uaddr: usize, pid: Option, wake_count: u32, requeue_uaddr: usize, _requeue_count: u32, ) -> KResult { let futex_key = FutexKey::new(uaddr, pid); let futex_requeue_key = FutexKey::new(requeue_uaddr, pid); let (bucket_idx0, _bucket_ref0) = FUTEX_TABLE.get_bucket(&futex_key); - let (bucket_idx1, _bucket_ref1) = FUTEX_TABLE.get_bucket(&futex_requeue_key); + let (bucket_idx1, _bucket_ref1) = + FUTEX_TABLE.get_bucket(&futex_requeue_key); if bucket_idx0 == bucket_idx1 { // If the keys are the same, we can just wake up the waiters. @@ -298,7 +296,9 @@ impl RobustListHead { futex_wake(futex_addr, None, usize::MAX as u32).await?; // Move to the next entry in the robust list. - let robust_list = UserPointer::new(User::::with_addr(entry_ptr))?.read()?; + let robust_list = + UserPointer::new(User::::with_addr(entry_ptr))? + .read()?; entry_ptr = robust_list.next; diff --git a/src/kernel/task/loader/elf.rs b/src/kernel/task/loader/elf.rs index 9f8aa166..9489940a 100644 --- a/src/kernel/task/loader/elf.rs +++ b/src/kernel/task/loader/elf.rs @@ -180,9 +180,11 @@ impl Elf { let ph_offset = self.ph_offset(); for program_header in &self.program_headers { if program_header.offset() <= ph_offset - && ph_offset < program_header.offset() + program_header.file_size() + && ph_offset + < program_header.offset() + program_header.file_size() { - return Ok(ph_offset - program_header.offset() + program_header.virtual_addr()); + return Ok(ph_offset - program_header.offset() + + program_header.virtual_addr()); } } Err(ENOEXEC) @@ -212,7 +214,9 @@ impl Elf { }) } - async fn load(&self, args: Vec, envs: Vec) -> KResult { + async fn load( + &self, args: Vec, envs: Vec, + ) -> KResult { let mm_list = MMList::new(); // Load Segments @@ -258,10 +262,7 @@ impl Elf { } async fn create_and_init_stack( - &self, - mm_list: &MMList, - args: Vec, - envs: Vec, + &self, mm_list: &MMList, args: Vec, envs: Vec, aux_vec: AuxVec, ) -> KResult { mm_list @@ -283,7 +284,9 @@ impl Elf { .await } - fn init_aux_vec(&self, elf_base: VAddr, ldso_base: Option) -> KResult> { + fn init_aux_vec( + &self, elf_base: VAddr, ldso_base: Option, + ) -> KResult> { let mut aux_vec: AuxVec = AuxVec::new(); let ph_addr = if self.is_shared_object() { elf_base.addr() + self.ph_addr()? @@ -294,7 +297,8 @@ impl Elf { aux_vec.set(AuxKey::AT_PAGESZ, E::Ea::from_usize(PAGE_SIZE))?; aux_vec.set(AuxKey::AT_PHDR, E::Ea::from_usize(ph_addr))?; aux_vec.set(AuxKey::AT_PHNUM, E::Ea::from_usize(self.ph_count()))?; - aux_vec.set(AuxKey::AT_PHENT, E::Ea::from_usize(self.ph_entry_size()))?; + aux_vec + .set(AuxKey::AT_PHENT, E::Ea::from_usize(self.ph_entry_size()))?; let elf_entry = if self.is_shared_object() { elf_base.addr() + self.entry_point() } else { @@ -307,13 +311,15 @@ impl Elf { )?; if let Some(ldso_base) = ldso_base { - aux_vec.set(AuxKey::AT_BASE, E::Ea::from_usize(ldso_base.addr()))?; + aux_vec + .set(AuxKey::AT_BASE, E::Ea::from_usize(ldso_base.addr()))?; } Ok(aux_vec) } async fn load_segments(&self, mm_list: &MMList) -> KResult<(VAddr, VAddr)> { - let base: VAddr = if self.is_shared_object() { E::DYN_BASE_ADDR } else { 0 }.into(); + let base: VAddr = + if self.is_shared_object() { E::DYN_BASE_ADDR } else { 0 }.into(); let mut segments_end = VAddr::NULL; @@ -321,7 +327,8 @@ impl Elf { let type_ = program_header.type_().map_err(|_| ENOEXEC)?; if type_ == program::Type::Load { - let segment_end = self.load_segment(program_header, mm_list, base).await?; + let segment_end = + self.load_segment(program_header, mm_list, base).await?; if segment_end > segments_end { segments_end = segment_end; @@ -333,10 +340,7 @@ impl Elf { } async fn load_segment( - &self, - program_header: &E::Ph, - mm_list: &MMList, - base_addr: VAddr, + &self, program_header: &E::Ph, mm_list: &MMList, base_addr: VAddr, ) -> KResult { let virtual_addr = base_addr + program_header.virtual_addr(); let vmem_vaddr_end = virtual_addr + program_header.mem_size(); @@ -377,7 +381,9 @@ impl Elf { zero_len = zero_len.min(vmem_vaddr_end - load_vaddr_end); mm_list - .access_mut(load_vaddr_end, zero_len, |_, data| data.fill(0)) + .access_mut(load_vaddr_end, zero_len, |_, data| { + data.fill(0) + }) .await?; } @@ -397,13 +403,19 @@ impl Elf { Ok(vmap_start + vmem_len) } - async fn load_ldso(&self, mm_list: &MMList) -> KResult> { + async fn load_ldso( + &self, mm_list: &MMList, + ) -> KResult> { let ldso_path = self.ldso_path().await?; if let Some(ldso_path) = ldso_path { let fs_context = FsContext::global(); - let ldso_file = - Dentry::open(fs_context, Path::new(ldso_path.as_bytes())?, true).await?; + let ldso_file = Dentry::open( + fs_context, + Path::new(ldso_path.as_bytes())?, + true, + ) + .await?; let ldso_elf = Elf::::parse(ldso_file).await?; let base = VAddr::from(E::LDSO_BASE_ADDR); @@ -412,7 +424,9 @@ impl Elf { let type_ = program_header.type_().map_err(|_| ENOEXEC)?; if type_ == program::Type::Load { - ldso_elf.load_segment(program_header, mm_list, base).await?; + ldso_elf + .load_segment(program_header, mm_list, base) + .await?; } } @@ -439,9 +453,13 @@ impl Elf { let mut ldso_vec = vec![0u8; file_size - 1]; // -1 due to '\0' self.file - .read(&mut ByteBuffer::from(ldso_vec.as_mut_slice()), file_offset) + .read( + &mut ByteBuffer::from(ldso_vec.as_mut_slice()), + file_offset, + ) .await?; - let ldso_path = String::from_utf8(ldso_vec).map_err(|_| ENOEXEC)?; + let ldso_path = + String::from_utf8(ldso_vec).map_err(|_| ENOEXEC)?; return Ok(Some(ldso_path)); } } @@ -469,7 +487,9 @@ impl ELF { } } - pub async fn load(&self, args: Vec, envs: Vec) -> KResult { + pub async fn load( + &self, args: Vec, envs: Vec, + ) -> KResult { match &self { ELF::Elf32(elf32) => elf32.load(args, envs).await, ELF::Elf64(elf64) => elf64.load(args, envs).await, @@ -487,10 +507,7 @@ struct StackInitializer<'a, T> { impl<'a, T: ElfAddr + Clone + Copy> StackInitializer<'a, T> { fn new( - mm_list: &'a MMList, - sp: usize, - args: Vec, - envs: Vec, + mm_list: &'a MMList, sp: usize, args: Vec, envs: Vec, aux_vec: AuxVec, ) -> Self { Self { @@ -524,7 +541,10 @@ impl<'a, T: ElfAddr + Clone + Copy> StackInitializer<'a, T> { self.sp -= len; self.mm_list .access_mut(VAddr::from(self.sp), len, |offset, data| { - data.copy_from_slice(&string.as_bytes_with_nul()[offset..offset + data.len()]) + data.copy_from_slice( + &string.as_bytes_with_nul() + [offset..offset + data.len()], + ) }) .await?; addrs.push(T::from_usize(self.sp)); @@ -540,7 +560,10 @@ impl<'a, T: ElfAddr + Clone + Copy> StackInitializer<'a, T> { self.sp -= len; self.mm_list .access_mut(VAddr::from(self.sp), len, |offset, data| { - data.copy_from_slice(&string.as_bytes_with_nul()[offset..offset + data.len()]) + data.copy_from_slice( + &string.as_bytes_with_nul() + [offset..offset + data.len()], + ) }) .await?; addrs.push(T::from_usize(self.sp)); @@ -550,11 +573,13 @@ impl<'a, T: ElfAddr + Clone + Copy> StackInitializer<'a, T> { } fn stack_alignment(&mut self) { - let aux_vec_size = (self.aux_vec.table().len() + 1) * (size_of::() * 2); + let aux_vec_size = + (self.aux_vec.table().len() + 1) * (size_of::() * 2); let envp_pointers_size = (self.envs.len() + 1) * size_of::(); let argv_pointers_size = (self.args.len() + 1) * size_of::(); let argc_size = size_of::(); - let all_size = aux_vec_size + envp_pointers_size + argv_pointers_size + argc_size; + let all_size = + aux_vec_size + envp_pointers_size + argv_pointers_size + argc_size; let align_sp = (self.sp - all_size).align_down(16); self.sp = align_sp + all_size; @@ -588,7 +613,10 @@ impl<'a, T: ElfAddr + Clone + Copy> StackInitializer<'a, T> { self.mm_list .access_mut(VAddr::from(self.sp), size_of::(), |_, data| { data.copy_from_slice(unsafe { - core::slice::from_raw_parts(&val as *const _ as *const u8, data.len()) + core::slice::from_raw_parts( + &val as *const _ as *const u8, + data.len(), + ) }) }) .await?; diff --git a/src/kernel/task/loader/mod.rs b/src/kernel/task/loader/mod.rs index 7679aaf4..ca7d1197 100644 --- a/src/kernel/task/loader/mod.rs +++ b/src/kernel/task/loader/mod.rs @@ -1,14 +1,16 @@ +use alloc::ffi::CString; +use alloc::sync::Arc; + +use eonix_mm::address::VAddr; + use crate::io::ByteBuffer; use crate::kernel::constants::ENOEXEC; +use crate::kernel::mem::MMList; use crate::kernel::task::loader::elf::ELF; +use crate::kernel::vfs::dentry::Dentry; use crate::kernel::vfs::FsContext; use crate::path::Path; -use crate::{ - kernel::{mem::MMList, vfs::dentry::Dentry}, - prelude::*, -}; -use alloc::{ffi::CString, sync::Arc}; -use eonix_mm::address::VAddr; +use crate::prelude::*; mod aux_vec; mod elf; @@ -34,11 +36,8 @@ pub struct ProgramLoader { impl ProgramLoader { pub async fn parse( - fs_context: &FsContext, - mut exec_path: CString, - mut file: Arc, - mut args: Vec, - envs: Vec, + fs_context: &FsContext, mut exec_path: CString, mut file: Arc, + mut args: Vec, envs: Vec, ) -> KResult { const RECURSION_LIMIT: usize = 4; let mut nrecur = 0; @@ -67,14 +66,20 @@ impl ProgramLoader { } let line = &interpreter_line[2..nread]; - let line = line.split(|&b| b == b'\n' || b == b'\0').next().unwrap(); + let line = line + .split(|&b| b == b'\n' || b == b'\0') + .next() + .unwrap(); let interpreter_name; let mut interpreter_arg = None; match line.iter().position(|&b| b == b' ') { Some(blank_pos) => { - interpreter_name = CString::new(&line[..blank_pos]).unwrap(); - interpreter_arg = Some(CString::new(&line[blank_pos + 1..]).unwrap()); + interpreter_name = + CString::new(&line[..blank_pos]).unwrap(); + interpreter_arg = Some( + CString::new(&line[blank_pos + 1..]).unwrap(), + ); } None => interpreter_name = CString::new(line).unwrap(), } diff --git a/src/kernel/task/process_group.rs b/src/kernel/task/process_group.rs index 8c708b5c..ebf3ff16 100644 --- a/src/kernel/task/process_group.rs +++ b/src/kernel/task/process_group.rs @@ -56,9 +56,7 @@ impl ProcessGroup { /// # Panics /// Panics if `leader` is already in some pgroup. pub fn new( - leader: &Arc, - session: &Arc, - procs: &mut ProcessList, + leader: &Arc, session: &Arc, procs: &mut ProcessList, ) -> Arc { let pgid = leader.pid; let pgroup_procs = { @@ -86,18 +84,14 @@ impl ProcessGroup { /// # Panics /// Panics if `process` is already in some pgroup or the pgroup is dead. pub fn add_member( - &self, - process: &Arc, - procs: ProofMut<'_, ProcessList>, + &self, process: &Arc, procs: ProofMut<'_, ProcessList>, ) { assert!(self.all_groups_link.is_linked(), "Dead pgroup"); self.procs.access_mut(procs).insert(process.clone()); } pub fn remove_member( - self: &Arc, - process: &Arc, - procs: &mut ProcessList, + self: &Arc, process: &Arc, procs: &mut ProcessList, ) { let members = self.procs.access_mut(procs.prove_mut()); assert!( diff --git a/src/kernel/task/session.rs b/src/kernel/task/session.rs index 899aa395..c8a2f13e 100644 --- a/src/kernel/task/session.rs +++ b/src/kernel/task/session.rs @@ -59,18 +59,14 @@ impl Session { } pub fn add_member( - &self, - pgroup: &Arc, - procs: ProofMut<'_, ProcessList>, + &self, pgroup: &Arc, procs: ProofMut<'_, ProcessList>, ) { assert!(self.all_sessions_link.is_linked(), "Dead session"); self.groups.access_mut(procs).insert(pgroup.clone()); } pub fn remove_member( - self: &Arc, - pgroup: &Arc, - procs: &mut ProcessList, + self: &Arc, pgroup: &Arc, procs: &mut ProcessList, ) { let members = self.groups.access_mut(procs.prove_mut()); assert!( @@ -107,8 +103,7 @@ impl Session { /// Set the foreground process group identified by `pgid`. /// The process group must belong to the session. pub fn set_foreground_pgroup( - &self, - pgroup: Option<&Arc>, + &self, pgroup: Option<&Arc>, ) -> KResult<()> { if let Some(pgroup) = pgroup { if pgroup.session.sid != self.sid { @@ -128,8 +123,7 @@ impl Session { /// Panics if we have a controlling terminal already /// or the session leader is gone. pub fn _set_control_terminal( - self: &Arc, - terminal: &Arc, + self: &Arc, terminal: &Arc, procs: Proof<'_, ProcessList>, ) { let mut job_control = self.job_control.lock(); diff --git a/src/kernel/task/signal.rs b/src/kernel/task/signal.rs index 0a7b580d..5ef28ae6 100644 --- a/src/kernel/task/signal.rs +++ b/src/kernel/task/signal.rs @@ -128,7 +128,9 @@ impl SignalList { self.inner.lock().mask.unmask(mask) } - pub fn set_action(&self, signal: Signal, action: SignalAction) -> KResult<()> { + pub fn set_action( + &self, signal: Signal, action: SignalAction, + ) -> KResult<()> { if matches!(signal, SIGNAL_NOW!()) { return Err(EINVAL); } @@ -141,7 +143,9 @@ impl SignalList { self.inner.lock().actions.get(signal) } - pub fn set_signal_waker(&self, waker: Option>) { + pub fn set_signal_waker( + &self, waker: Option>, + ) { let mut inner = self.inner.lock(); inner.signal_waker = waker; } @@ -168,7 +172,9 @@ impl SignalList { } /// Handle signals in the context of `Thread::current()`. - pub async fn handle(&self, trap_ctx: &mut TrapContext, fpu_state: &mut FpuState) { + pub async fn handle( + &self, trap_ctx: &mut TrapContext, fpu_state: &mut FpuState, + ) { loop { let signal = { let Some(signal) = self.inner.lock().pop() else { return }; @@ -182,7 +188,8 @@ impl SignalList { old_mask }; - let result = handler.handle(signal, old_mask, trap_ctx, fpu_state); + let result = + handler.handle(signal, old_mask, trap_ctx, fpu_state); if result.is_err() { self.inner.lock().mask = old_mask; } @@ -229,7 +236,10 @@ impl SignalList { Runtime::block_till_woken(|waker| { let mut inner = self.inner.lock(); let old_waker = inner.stop_waker.replace(waker.clone()); - assert!(old_waker.is_none(), "We should not have a waker here"); + assert!( + old_waker.is_none(), + "We should not have a waker here" + ); }) .await; @@ -255,9 +265,7 @@ impl SignalList { /// Load the signal mask, fpu state and trap context from the user stack. pub fn restore( - &self, - trap_ctx: &mut TrapContext, - fpu_state: &mut FpuState, + &self, trap_ctx: &mut TrapContext, fpu_state: &mut FpuState, old_sigreturn: bool, ) -> KResult<()> { #[cfg(not(any( @@ -293,15 +301,18 @@ impl SignalList { let old_fpu_state_vaddr = old_trap_ctx_vaddr + size_of::(); let old_mask_vaddr = old_fpu_state_vaddr + size_of::(); - *trap_ctx = UserPointer::::with_addr(old_trap_ctx_vaddr)?.read()?; + *trap_ctx = UserPointer::::with_addr(old_trap_ctx_vaddr)? + .read()?; // Make sure that at least we won't crash the kernel. if !trap_ctx.is_user_mode() || !trap_ctx.is_interrupt_enabled() { return Err(EFAULT)?; } - *fpu_state = UserPointer::::with_addr(old_fpu_state_vaddr)?.read()?; - self.inner.lock().mask = UserPointer::::with_addr(old_mask_vaddr)?.read()?; + *fpu_state = + UserPointer::::with_addr(old_fpu_state_vaddr)?.read()?; + self.inner.lock().mask = + UserPointer::::with_addr(old_mask_vaddr)?.read()?; Ok(()) } diff --git a/src/kernel/task/user_tls/x86_64.rs b/src/kernel/task/user_tls/x86_64.rs index 5bb33b97..5face554 100644 --- a/src/kernel/task/user_tls/x86_64.rs +++ b/src/kernel/task/user_tls/x86_64.rs @@ -43,7 +43,9 @@ impl UserTLS { impl UserTLSDescriptor<'_> { pub fn new(raw_tls: PtrT) -> KResult { Ok(Self { - ptr: UserPointerMut::new(UserMut::::with_addr(raw_tls.addr()))?, + ptr: UserPointerMut::new(UserMut::::with_addr( + raw_tls.addr(), + ))?, }) } diff --git a/src/kernel/terminal.rs b/src/kernel/terminal.rs index ddc3cc1f..87111937 100644 --- a/src/kernel/terminal.rs +++ b/src/kernel/terminal.rs @@ -458,10 +458,7 @@ impl Terminal { } async fn echo_and_signal( - &self, - inner: &mut TerminalInner, - ch: u8, - signal: Signal, + &self, inner: &mut TerminalInner, ch: u8, signal: Signal, ) { self.echo_char(inner, ch); self.signal(inner, signal).await; @@ -685,9 +682,7 @@ impl Terminal { /// Assign the `session` to this terminal. /// Drop the previous session if `forced` is true. pub async fn set_session( - self: &Arc, - session: &Arc, - forced: bool, + self: &Arc, session: &Arc, forced: bool, procs: Proof<'_, ProcessList>, ) -> KResult<()> { let mut cur_session = self.session.lock(); diff --git a/src/kernel/timer.rs b/src/kernel/timer.rs index 1dbb1382..9a65f7ca 100644 --- a/src/kernel/timer.rs +++ b/src/kernel/timer.rs @@ -1,19 +1,21 @@ -use alloc::{collections::BinaryHeap, vec, vec::Vec}; -use core::{ - cell::RefCell, - cmp::Reverse, - ops::Add, - sync::atomic::{AtomicUsize, Ordering}, - task::{Poll, Waker}, - time::Duration, -}; +use alloc::collections::BinaryHeap; +use alloc::vec; +use alloc::vec::Vec; +use core::cell::RefCell; +use core::cmp::Reverse; +use core::ops::Add; +use core::sync::atomic::{AtomicUsize, Ordering}; +use core::task::{Poll, Waker}; +use core::time::Duration; + use eonix_hal::processor::CPU; use eonix_sync::{Spin, SpinIrq as _}; use posix_types::stat::{StatXTimestamp, TimeSpec, TimeVal}; static TICKS: AtomicUsize = AtomicUsize::new(0); static WAKEUP_TICK: AtomicUsize = AtomicUsize::new(usize::MAX); -static SLEEPERS_LIST: Spin>> = Spin::new(BinaryHeap::new()); +static SLEEPERS_LIST: Spin>> = + Spin::new(BinaryHeap::new()); #[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)] pub struct Ticks(usize); @@ -154,8 +156,9 @@ impl Add for Instant { fn add(self, duration: Duration) -> Self::Output { let nsecs = self.nsecs_within + duration.subsec_nanos(); let nsecs_within = nsecs % 1_000_000_000; - let secs_since_epoch = - self.secs_since_epoch + duration.as_secs() + (nsecs / 1_000_000_000) as u64; + let secs_since_epoch = self.secs_since_epoch + + duration.as_secs() + + (nsecs / 1_000_000_000) as u64; Instant { secs_since_epoch, diff --git a/src/kernel/user.rs b/src/kernel/user.rs index 5e410c81..52236f88 100644 --- a/src/kernel/user.rs +++ b/src/kernel/user.rs @@ -1,3 +1,5 @@ mod dataflow; -pub use dataflow::{CheckedUserPointer, UserBuffer, UserPointer, UserPointerMut, UserString}; +pub use dataflow::{ + CheckedUserPointer, UserBuffer, UserPointer, UserPointerMut, UserString, +}; diff --git a/src/kernel/user/dataflow.rs b/src/kernel/user/dataflow.rs index 5d8ac167..9a0a42cc 100644 --- a/src/kernel/user/dataflow.rs +++ b/src/kernel/user/dataflow.rs @@ -44,7 +44,8 @@ pub struct UserStream<'a> { impl UserPointer<'_, T> { pub fn new(ptr: User) -> KResult { - let pointer = CheckedUserPointer::new(ptr.cast(), core::mem::size_of::())?; + let pointer = + CheckedUserPointer::new(ptr.cast(), core::mem::size_of::())?; Ok(Self { pointer, @@ -72,7 +73,10 @@ impl UserPointer<'_, T> { impl<'a, T: Copy> UserPointerMut<'a, T> { pub fn new(ptr: UserMut) -> KResult { - let pointer = CheckedUserPointer::new(ptr.cast().as_const(), core::mem::size_of::())?; + let pointer = CheckedUserPointer::new( + ptr.cast().as_const(), + core::mem::size_of::(), + )?; Ok(Self { pointer, @@ -522,7 +526,9 @@ impl UserStream<'_> { } impl Stream for UserStream<'_> { - fn poll_data<'a>(&mut self, buf: &'a mut [u8]) -> KResult> { + fn poll_data<'a>( + &mut self, buf: &'a mut [u8], + ) -> KResult> { assert_preempt_enabled!("UserStream::poll_data"); if self.cur >= self.pointer.len { diff --git a/src/kernel/vfs/dentry/dcache.rs b/src/kernel/vfs/dentry/dcache.rs index ee7503dc..1234cf4b 100644 --- a/src/kernel/vfs/dentry/dcache.rs +++ b/src/kernel/vfs/dentry/dcache.rs @@ -1,16 +1,15 @@ -use super::Dentry; -use crate::kernel::constants::ENOENT; -use crate::rcu::{RCUPointer, RCUReadLock}; -use crate::{ - prelude::*, - rcu::{RCUIterator, RCUList}, -}; use alloc::sync::Arc; -use arcref::ArcRef; use core::ops::Deref; use core::sync::atomic::Ordering; + +use arcref::ArcRef; use eonix_sync::Mutex; +use super::Dentry; +use crate::kernel::constants::ENOENT; +use crate::prelude::*; +use crate::rcu::{RCUIterator, RCUList, RCUPointer, RCUReadLock}; + const DCACHE_HASH_BITS: u32 = 8; static DCACHE: [RCUList; 1 << DCACHE_HASH_BITS] = @@ -22,12 +21,13 @@ pub trait DCacheItem { fn d_hash(&self) -> usize; fn d_parent(&self) -> *const Dentry; fn d_name<'r, 'a: 'r, 'b: 'a>( - &'a self, - rcu_read: &'b RCUReadLock, + &'a self, rcu_read: &'b RCUReadLock, ) -> impl Deref + 'r; } -fn d_eq(lhs: &impl DCacheItem, rhs: &impl DCacheItem, rcu_read: &RCUReadLock) -> bool { +fn d_eq( + lhs: &impl DCacheItem, rhs: &impl DCacheItem, rcu_read: &RCUReadLock, +) -> bool { lhs.d_hash() == rhs.d_hash() && lhs.d_parent() == rhs.d_parent() && *lhs.d_name(rcu_read) == *rhs.d_name(rcu_read) @@ -39,17 +39,16 @@ fn d_hinted(item: &impl DCacheItem) -> &'static RCUList { } fn d_iter_for<'rcu>( - item: &impl DCacheItem, - rcu_read: &'rcu RCUReadLock, + item: &impl DCacheItem, rcu_read: &'rcu RCUReadLock, ) -> RCUIterator<'static, 'rcu, Dentry> { d_hinted(item).iter(rcu_read) } pub fn d_find_rcu<'rcu>( - item: &impl DCacheItem, - rcu_read: &'rcu RCUReadLock, + item: &impl DCacheItem, rcu_read: &'rcu RCUReadLock, ) -> Option> { - d_iter_for(item, rcu_read).find(|cur_ref| cur_ref.with_arc(|cur| d_eq(cur, item, rcu_read))) + d_iter_for(item, rcu_read) + .find(|cur_ref| cur_ref.with_arc(|cur| d_eq(cur, item, rcu_read))) } /// Add the dentry to the dcache @@ -114,8 +113,7 @@ impl DCacheItem for Arc { } fn d_name<'r, 'a: 'r, 'b: 'a>( - &'a self, - rcu_read: &'b RCUReadLock, + &'a self, rcu_read: &'b RCUReadLock, ) -> impl Deref + 'r { struct Name<'a>(ArcRef<'a, Arc<[u8]>>); diff --git a/src/kernel/vfs/dentry/walk.rs b/src/kernel/vfs/dentry/walk.rs index 7b1060ac..78c9be9b 100644 --- a/src/kernel/vfs/dentry/walk.rs +++ b/src/kernel/vfs/dentry/walk.rs @@ -57,9 +57,7 @@ impl Dentry { /// returning `None` is acceptable if the actual file exists but is not in /// the dentry cache. If so, we should check again with `lookup`. fn find_rcu<'r, 's: 'r>( - self: ArcRef<'s, Self>, - name: &[u8], - rcu_read: &'r RCUReadLock, + self: ArcRef<'s, Self>, name: &[u8], rcu_read: &'r RCUReadLock, ) -> Result>, PosixError> { match self.inode.load() { Some((DentryKind::Directory, _)) => {} @@ -81,7 +79,9 @@ impl Dentry { } } - async fn find_slow(self: &Arc, name: &[u8]) -> Result, PosixError> { + async fn find_slow( + self: &Arc, name: &[u8], + ) -> Result, PosixError> { let dentry = Dentry::create(self.clone(), name); let _ = dcache::d_try_revalidate(&dentry).await; @@ -90,7 +90,9 @@ impl Dentry { Ok(dentry) } - pub async fn find_full(self: &Arc, name: &[u8]) -> Result, PosixError> { + pub async fn find_full( + self: &Arc, name: &[u8], + ) -> Result, PosixError> { if let Some(dentry) = self.aref().find_rcu(name, &rcu_read_lock())? { return Ok(dentry.clone_arc()); } @@ -103,10 +105,8 @@ impl FsContext { /// Walk the pathname and try to find the corresponding dentry FAST without /// consulting the VFS for invalid dentries encountered. fn walk_rcu<'rcu, 'path>( - &self, - mut current: ArcRef<'rcu, Dentry>, - iter: &mut PathIterator<'path>, - rcu_read: &'rcu RCUReadLock, + &self, mut current: ArcRef<'rcu, Dentry>, + iter: &mut PathIterator<'path>, rcu_read: &'rcu RCUReadLock, ) -> WalkResultRcu<'rcu, 'path> { use PathComponent::*; @@ -131,7 +131,8 @@ impl FsContext { match (inode, component) { // Skip trailing empty and dot for normal directories. - (Some((DentryKind::Directory, _)), TrailingEmpty | Current) => {} + (Some((DentryKind::Directory, _)), TrailingEmpty | Current) => { + } // Walk to parent directory unless we are at the filesystem root. (Some((DentryKind::Directory, _)), Parent) => { if current.ptr_eq_arc(&self.fsroot) { @@ -171,7 +172,9 @@ impl FsContext { } /// Walk the pathname slowly with refcounts held and VFS lookups. - async fn walk_slow(&self, mut current: Arc, iter: &mut PathIterator<'_>) -> WalkResult { + async fn walk_slow( + &self, mut current: Arc, iter: &mut PathIterator<'_>, + ) -> WalkResult { use PathComponent::*; loop { @@ -229,15 +232,15 @@ impl FsContext { /// Walk the pathname and get an accurate answer. Stop at symlinks. async fn walk_full( - &self, - current: ArcRef<'_, Dentry>, - iter: &mut PathIterator<'_>, + &self, current: ArcRef<'_, Dentry>, iter: &mut PathIterator<'_>, ) -> WalkResult { let (parent_slow, name_slow); match self.walk_rcu(current, iter, &rcu_read_lock()) { WalkResultRcu::Err(error) => return WalkResult::Err(error.into()), - WalkResultRcu::Ok(dentry) => return WalkResult::Ok(dentry.clone_arc()), + WalkResultRcu::Ok(dentry) => { + return WalkResult::Ok(dentry.clone_arc()) + } WalkResultRcu::Symlink { symlink, inode } => { return WalkResult::Symlink { symlink: symlink.clone_arc(), @@ -258,10 +261,7 @@ impl FsContext { } pub async fn follow_symlink( - &self, - symlink: ArcRef<'_, Dentry>, - inode: &InodeUse, - nr_follows: u32, + &self, symlink: ArcRef<'_, Dentry>, inode: &InodeUse, nr_follows: u32, ) -> KResult> { let mut target = [0; 256]; let mut target = ByteBuffer::new(&mut target); @@ -276,19 +276,14 @@ impl FsContext { } fn follow_symlink_boxed<'r, 'a: 'r, 'b: 'r, 'c: 'r>( - &'a self, - symlink: ArcRef<'b, Dentry>, - inode: &'c InodeUse, + &'a self, symlink: ArcRef<'b, Dentry>, inode: &'c InodeUse, nr_follows: u32, ) -> Pin>> + Send + 'r>> { Box::pin(self.follow_symlink(symlink, inode, nr_follows)) } async fn walk_recursive( - &self, - cwd: &Arc, - path: &Path, - nr_follows: u32, + &self, cwd: &Arc, path: &Path, nr_follows: u32, ) -> KResult> { const MAX_NR_FOLLOWS: u32 = 16; @@ -312,7 +307,11 @@ impl FsContext { } current_owned = self - .follow_symlink_boxed(symlink.aref(), &inode, nr_follows) + .follow_symlink_boxed( + symlink.aref(), + &inode, + nr_follows, + ) .await?; current = current_owned.aref(); } @@ -321,9 +320,7 @@ impl FsContext { } pub async fn start_recursive_walk( - &self, - cwd: &Arc, - path: &Path, + &self, cwd: &Arc, path: &Path, ) -> KResult> { self.walk_recursive(cwd, path, 0).await } @@ -352,8 +349,7 @@ impl DCacheItem for DentryFind<'_, '_> { } fn d_name<'r, 'a: 'r, 'b: 'a>( - &'a self, - _rcu_read: &'b RCUReadLock, + &'a self, _rcu_read: &'b RCUReadLock, ) -> impl Deref + 'r { self.name } diff --git a/src/kernel/vfs/file/inode_file.rs b/src/kernel/vfs/file/inode_file.rs index d302079c..c6626e31 100644 --- a/src/kernel/vfs/file/inode_file.rs +++ b/src/kernel/vfs/file/inode_file.rs @@ -52,7 +52,9 @@ impl InodeFile { } } - pub async fn write(&self, stream: &mut dyn Stream, offset: Option) -> KResult { + pub async fn write( + &self, stream: &mut dyn Stream, offset: Option, + ) -> KResult { if !self.w { return Err(EBADF); } @@ -62,7 +64,9 @@ impl InodeFile { let (offset, update_offset) = match (self.a, offset) { (true, _) => (WriteOffset::End(&mut cursor), None), (false, Some(offset)) => (WriteOffset::Position(offset), None), - (false, None) => (WriteOffset::Position(*cursor), Some(&mut *cursor)), + (false, None) => { + (WriteOffset::Position(*cursor), Some(&mut *cursor)) + } }; let nr_write = self.dentry.write(stream, offset).await?; @@ -74,7 +78,9 @@ impl InodeFile { Ok(nr_write) } - pub async fn read(&self, buffer: &mut dyn Buffer, offset: Option) -> KResult { + pub async fn read( + &self, buffer: &mut dyn Buffer, offset: Option, + ) -> KResult { if !self.r { return Err(EBADF); } @@ -111,7 +117,8 @@ impl File { .dentry .readdir(*cursor, |filename, ino| { // + 1 for filename length padding '\0', + 1 for d_type. - let real_record_len = core::mem::size_of::() + filename.len() + 2; + let real_record_len = + core::mem::size_of::() + filename.len() + 2; if buffer.available() < real_record_len { return Ok(false); @@ -147,7 +154,8 @@ impl File { .dentry .readdir(*cursor, |filename, ino| { // Filename length + 1 for padding '\0' - let real_record_len = core::mem::size_of::() + filename.len() + 1; + let real_record_len = + core::mem::size_of::() + filename.len() + 1; if buffer.available() < real_record_len { return Ok(false); @@ -181,7 +189,9 @@ impl File { let mut cursor = inode_file.cursor.lock().await; let new_cursor = match option { - SeekOption::Current(off) => cursor.checked_add_signed(off).ok_or(EOVERFLOW)?, + SeekOption::Current(off) => { + cursor.checked_add_signed(off).ok_or(EOVERFLOW)? + } SeekOption::Set(n) => n, SeekOption::End(off) => { let inode = inode_file.dentry.get_inode()?; diff --git a/src/kernel/vfs/file/pipe.rs b/src/kernel/vfs/file/pipe.rs index 910f04fa..3aedd598 100644 --- a/src/kernel/vfs/file/pipe.rs +++ b/src/kernel/vfs/file/pipe.rs @@ -1,16 +1,16 @@ -use super::{File, FileType, PollEvent}; -use crate::{ - io::{Buffer, Stream}, - kernel::{ - constants::{EINTR, EPIPE}, - task::Thread, - }, - prelude::KResult, - sync::CondVar, -}; -use alloc::{collections::vec_deque::VecDeque, sync::Arc}; +use alloc::collections::vec_deque::VecDeque; +use alloc::sync::Arc; + use eonix_sync::Mutex; -use posix_types::{open::OpenFlags, signal::Signal}; +use posix_types::open::OpenFlags; +use posix_types::signal::Signal; + +use super::{File, FileType, PollEvent}; +use crate::io::{Buffer, Stream}; +use crate::kernel::constants::{EINTR, EPIPE}; +use crate::kernel::task::Thread; +use crate::prelude::KResult; +use crate::sync::CondVar; struct PipeInner { buffer: VecDeque, @@ -53,7 +53,8 @@ impl Pipe { cv_write: CondVar::new(), }); - let read_flags = flags.difference(OpenFlags::O_WRONLY | OpenFlags::O_RDWR); + let read_flags = + flags.difference(OpenFlags::O_WRONLY | OpenFlags::O_RDWR); let mut write_flags = read_flags; write_flags.insert(OpenFlags::O_WRONLY); @@ -109,7 +110,8 @@ impl Pipe { } let (data1, data2) = inner.buffer.as_slices(); - let nread = buffer.fill(data1)?.allow_partial() + buffer.fill(data2)?.allow_partial(); + let nread = buffer.fill(data1)?.allow_partial() + + buffer.fill(data2)?.allow_partial(); inner.buffer.drain(..nread); self.cv_write.notify_all(); diff --git a/src/kernel/vfs/file/terminal_file.rs b/src/kernel/vfs/file/terminal_file.rs index 04a022b5..d6c15604 100644 --- a/src/kernel/vfs/file/terminal_file.rs +++ b/src/kernel/vfs/file/terminal_file.rs @@ -20,9 +20,7 @@ pub struct TerminalFile { impl TerminalFile { pub async fn open( - thread: &Thread, - terminal: &Arc, - flags: OpenFlags, + thread: &Thread, terminal: &Arc, flags: OpenFlags, ) -> File { let set_control_tty = !flags.contains(OpenFlags::O_NOCTTY); diff --git a/src/kernel/vfs/filearray.rs b/src/kernel/vfs/filearray.rs index c0b6a49e..eacce125 100644 --- a/src/kernel/vfs/filearray.rs +++ b/src/kernel/vfs/filearray.rs @@ -84,9 +84,7 @@ impl FDAllocator { } fn find_available( - &mut self, - from: FD, - files: &RBTree, + &mut self, from: FD, files: &RBTree, ) -> FD { files .range(Bound::Included(&from), Bound::Unbounded) @@ -232,10 +230,7 @@ impl FileArray { /// Duplicates the file to a new file descriptor, returning the old file /// description to be dropped. fn dup_to_no_close( - &self, - old_fd: FD, - new_fd: FD, - fd_flags: FDFlags, + &self, old_fd: FD, new_fd: FD, fd_flags: FDFlags, ) -> KResult> { let mut inner = self.inner.lock(); let (files, fd_alloc) = inner.split_borrow(); @@ -264,10 +259,7 @@ impl FileArray { } pub async fn dup_to( - &self, - old_fd: FD, - new_fd: FD, - flags: OpenFlags, + &self, old_fd: FD, new_fd: FD, flags: OpenFlags, ) -> KResult { if let Some(old_file) = self.dup_to_no_close(old_fd, new_fd, flags.as_fd_flags())? @@ -297,10 +289,7 @@ impl FileArray { } pub async fn open( - &self, - thread: &Thread, - dentry: &Arc, - flags: OpenFlags, + &self, thread: &Thread, dentry: &Arc, flags: OpenFlags, perm: Permission, ) -> KResult { dentry.open_check(flags, perm).await?; diff --git a/src/kernel/vfs/inode/ino.rs b/src/kernel/vfs/inode/ino.rs index b5ee7ac0..970fcc71 100644 --- a/src/kernel/vfs/inode/ino.rs +++ b/src/kernel/vfs/inode/ino.rs @@ -1,7 +1,5 @@ -use core::{ - fmt::{Debug, Display, Formatter}, - sync::atomic::AtomicU64, -}; +use core::fmt::{Debug, Display, Formatter}; +use core::sync::atomic::AtomicU64; #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Ino(u64); diff --git a/src/kernel/vfs/inode/inode.rs b/src/kernel/vfs/inode/inode.rs index 5f0b98c2..ab69b337 100644 --- a/src/kernel/vfs/inode/inode.rs +++ b/src/kernel/vfs/inode/inode.rs @@ -282,10 +282,7 @@ pub struct InodeUse(Arc); impl InodeUse { pub fn new( - sb: SbRef, - ino: Ino, - format: Format, - info: InodeInfo, + sb: SbRef, ino: Ino, format: Format, info: InodeInfo, ops: impl InodeOps, ) -> Self { let inode = Inode { diff --git a/src/kernel/vfs/inode/statx.rs b/src/kernel/vfs/inode/statx.rs index feb2a1b5..03f72f23 100644 --- a/src/kernel/vfs/inode/statx.rs +++ b/src/kernel/vfs/inode/statx.rs @@ -2,8 +2,8 @@ use posix_types::stat::StatX; use super::inode::InodeUse; use crate::kernel::constants::{ - STATX_ATIME, STATX_BLOCKS, STATX_CTIME, STATX_GID, STATX_INO, STATX_MODE, STATX_MTIME, - STATX_NLINK, STATX_SIZE, STATX_TYPE, STATX_UID, + STATX_ATIME, STATX_BLOCKS, STATX_CTIME, STATX_GID, STATX_INO, STATX_MODE, + STATX_MTIME, STATX_NLINK, STATX_SIZE, STATX_TYPE, STATX_UID, }; use crate::kernel::vfs::types::Format; use crate::prelude::KResult; diff --git a/src/kernel/vfs/mod.rs b/src/kernel/vfs/mod.rs index 5b8eca5a..8afdd573 100644 --- a/src/kernel/vfs/mod.rs +++ b/src/kernel/vfs/mod.rs @@ -6,14 +6,17 @@ pub mod mount; mod superblock; pub mod types; -use crate::prelude::*; use alloc::sync::Arc; + use dentry::Dentry; use eonix_sync::LazyLock; +pub use file::{File, FileType, PollEvent, SeekOption, TerminalFile}; +pub use superblock::{ + SbRef, SbUse, SuperBlock, SuperBlockInfo, SuperBlockLock, +}; use types::Permission; -pub use file::{File, FileType, PollEvent, SeekOption, TerminalFile}; -pub use superblock::{SbRef, SbUse, SuperBlock, SuperBlockInfo, SuperBlockLock}; +use crate::prelude::*; pub struct FsContext { pub fsroot: Arc, diff --git a/src/kernel/vfs/superblock.rs b/src/kernel/vfs/superblock.rs index e3be5cef..514dd365 100644 --- a/src/kernel/vfs/superblock.rs +++ b/src/kernel/vfs/superblock.rs @@ -73,7 +73,9 @@ where })) } - pub fn new_cyclic(info: SuperBlockInfo, backend_func: impl FnOnce(SbRef) -> S) -> Self { + pub fn new_cyclic( + info: SuperBlockInfo, backend_func: impl FnOnce(SbRef) -> S, + ) -> Self { Self(Arc::new_cyclic(|weak| SuperBlockComplex { info, rwsem: RwLock::new(SuperBlockLock(())), diff --git a/src/kernel/vfs/types/mode.rs b/src/kernel/vfs/types/mode.rs index a58c8215..b2c00865 100644 --- a/src/kernel/vfs/types/mode.rs +++ b/src/kernel/vfs/types/mode.rs @@ -1,7 +1,7 @@ -use crate::kernel::{ - constants::{S_IFBLK, S_IFCHR, S_IFDIR, S_IFLNK, S_IFMT, S_IFREG}, - syscall::{FromSyscallArg, SyscallRetVal}, +use crate::kernel::constants::{ + S_IFBLK, S_IFCHR, S_IFDIR, S_IFLNK, S_IFMT, S_IFREG, }; +use crate::kernel::syscall::{FromSyscallArg, SyscallRetVal}; #[derive(Clone, Copy, PartialEq, Eq)] pub struct Mode(u32); @@ -83,7 +83,8 @@ impl Format { } impl Permission { - const RWX: [&str; 8] = ["---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"]; + const RWX: [&str; 8] = + ["---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"]; pub const fn new(perm_bits: u32) -> Self { Self(perm_bits & 0o7777) diff --git a/src/net/netdev.rs b/src/net/netdev.rs index 120ac053..ba68ae2f 100644 --- a/src/net/netdev.rs +++ b/src/net/netdev.rs @@ -1,12 +1,11 @@ +use alloc::collections::btree_map::{BTreeMap, Entry}; +use alloc::sync::Arc; use core::sync::atomic::{AtomicU32, Ordering}; -use crate::kernel::constants::EFAULT; -use alloc::{ - collections::btree_map::{BTreeMap, Entry}, - sync::Arc, -}; use eonix_sync::Spin; +use crate::kernel::constants::EFAULT; + #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum LinkStatus { Up, @@ -57,13 +56,16 @@ impl Ord for dyn Netdev { } static NETDEVS_ID: AtomicU32 = AtomicU32::new(0); -static NETDEVS: Spin>>> = Spin::new(BTreeMap::new()); +static NETDEVS: Spin>>> = + Spin::new(BTreeMap::new()); pub fn alloc_id() -> u32 { NETDEVS_ID.fetch_add(1, Ordering::SeqCst) } -pub fn register_netdev(netdev: impl Netdev + 'static) -> Result>, u32> { +pub fn register_netdev( + netdev: impl Netdev + 'static, +) -> Result>, u32> { match NETDEVS.lock().entry(netdev.id()) { Entry::Vacant(entry) => { let netdev = Arc::new(Spin::new(netdev)); diff --git a/src/sync/arcswap.rs b/src/sync/arcswap.rs index 7421659f..4a2b7221 100644 --- a/src/sync/arcswap.rs +++ b/src/sync/arcswap.rs @@ -24,7 +24,8 @@ impl ArcSwap { /// The caller must ensure that the pointer not used elsewhere before ACTUALLLY dropping that. pub fn swap(&self, data: Option>) -> Option> { let new_pointer = data.map(Arc::into_raw).unwrap_or(core::ptr::null()); - let old_pointer = self.pointer.swap(new_pointer as *mut _, Ordering::AcqRel); + let old_pointer = + self.pointer.swap(new_pointer as *mut _, Ordering::AcqRel); if old_pointer.is_null() { None } else { diff --git a/src/sync/condvar.rs b/src/sync/condvar.rs index f767e5d5..909931cf 100644 --- a/src/sync/condvar.rs +++ b/src/sync/condvar.rs @@ -1,8 +1,10 @@ -use crate::kernel::task::Thread; use core::pin::pin; + use eonix_sync::{UnlockableGuard, UnlockedGuard as _, WaitList}; use intrusive_collections::UnsafeRef; +use crate::kernel::task::Thread; + pub struct CondVar { wait_list: WaitList, }