From fa59b8be2b829bbc98128eed261f80fda1323e07 Mon Sep 17 00:00:00 2001 From: Ruihan Li Date: Wed, 8 Oct 2025 17:10:14 +0800 Subject: [PATCH] Adjust visibility marks in `ostd::io` --- ostd/src/io/io_mem/allocator.rs | 19 ++++++++----------- ostd/src/io/io_port/allocator.rs | 14 ++++++++------ ostd/src/io/io_port/mod.rs | 8 +++----- ostd/src/io/mod.rs | 7 +++---- 4 files changed, 22 insertions(+), 26 deletions(-) diff --git a/ostd/src/io/io_mem/allocator.rs b/ostd/src/io/io_mem/allocator.rs index 4f5ae8a48..66b3cde08 100644 --- a/ostd/src/io/io_mem/allocator.rs +++ b/ostd/src/io/io_mem/allocator.rs @@ -20,7 +20,7 @@ use crate::{ }; /// I/O memory allocator that allocates memory I/O access to device drivers. -pub struct IoMemAllocator { +pub(super) struct IoMemAllocator { allocators: Vec, } @@ -28,11 +28,8 @@ impl IoMemAllocator { /// Acquires the I/O memory access for `range`. /// /// If the range is not available, then the return value will be `None`. - pub fn acquire(&self, range: Range) -> Option { - debug!( - "Try to acquire MMIO range:{:x?}..{:x?}", - range.start, range.end - ); + pub(super) fn acquire(&self, range: Range) -> Option { + debug!("Try to acquire MMIO range: {:#x?}", range); find_allocator(&self.allocators, &range)? .alloc_specific(&range) @@ -48,10 +45,10 @@ impl IoMemAllocator { /// /// The caller must have ownership of the MMIO region through the `IoMemAllocator::get` interface. #[expect(dead_code)] - pub(in crate::io) unsafe fn recycle(&self, range: Range) { - let allocator = find_allocator(&self.allocators, &range).unwrap(); + pub(super) unsafe fn recycle(&self, range: Range) { + debug!("Recycling MMIO range: {:#x?}", range); - debug!("Recycling MMIO range:{:x}..{:x}", range.start, range.end); + let allocator = find_allocator(&self.allocators, &range).unwrap(); allocator.free(range); } @@ -113,7 +110,7 @@ impl IoMemAllocatorBuilder { } /// The I/O Memory allocator of the system. -pub static IO_MEM_ALLOCATOR: Once = Once::new(); +pub(super) static IO_MEM_ALLOCATOR: Once = Once::new(); /// Initializes the static `IO_MEM_ALLOCATOR` based on builder. /// @@ -121,7 +118,7 @@ pub static IO_MEM_ALLOCATOR: Once = Once::new(); /// /// User must ensure all the memory I/O regions that belong to the system device have been removed by calling the /// `remove` function. -pub(crate) unsafe fn init(io_mem_builder: IoMemAllocatorBuilder) { +pub(in crate::io) unsafe fn init(io_mem_builder: IoMemAllocatorBuilder) { // SAFETY: The safety is upheld by the caller. IO_MEM_ALLOCATOR.call_once(|| unsafe { IoMemAllocator::new(io_mem_builder.allocators) }); } diff --git a/ostd/src/io/io_port/allocator.rs b/ostd/src/io/io_port/allocator.rs index c6fb6390d..bb81e5221 100644 --- a/ostd/src/io/io_port/allocator.rs +++ b/ostd/src/io/io_port/allocator.rs @@ -14,7 +14,7 @@ use crate::{ }; /// I/O port allocator that allocates port I/O access to device drivers. -pub struct IoPortAllocator { +pub(super) struct IoPortAllocator { /// Each ID indicates whether a Port I/O (1B) is allocated. /// /// Instead of using `RangeAllocator` like `IoMemAllocator` does, it is more reasonable to use `IdAlloc`, @@ -24,9 +24,11 @@ pub struct IoPortAllocator { impl IoPortAllocator { /// Acquires the `IoPort`. Return None if any region in `port` cannot be allocated. - pub fn acquire(&self, port: u16) -> Option> { - let mut allocator = self.allocator.lock(); + pub(super) fn acquire(&self, port: u16) -> Option> { let mut range = port..(port + size_of::() as u16); + debug!("Try to acquire PIO range: {:#x?}", range); + + let mut allocator = self.allocator.lock(); if range.any(|i| allocator.is_allocated(i as usize)) { return None; } @@ -44,8 +46,8 @@ impl IoPortAllocator { /// # Safety /// /// The caller must have ownership of the PIO region through the `IoPortAllocator::acquire` interface. - pub(in crate::io) unsafe fn recycle(&self, range: Range) { - debug!("Recycling MMIO range: {:#x?}", range); + pub(super) unsafe fn recycle(&self, range: Range) { + debug!("Recycling PIO range: {:#x?}", range); self.allocator .lock() @@ -66,7 +68,7 @@ pub(super) static IO_PORT_ALLOCATOR: Once = Once::new(); /// /// 2. `MAX_IO_PORT` defined in `crate::arch::io` is guaranteed not to exceed the maximum /// value specified by architecture. -pub(crate) unsafe fn init() { +pub(in crate::io) unsafe fn init() { // SAFETY: `MAX_IO_PORT` is guaranteed not to exceed the maximum value specified by architecture. let mut allocator = IdAlloc::with_capacity(crate::arch::io::MAX_IO_PORT as usize); diff --git a/ostd/src/io/io_port/mod.rs b/ostd/src/io/io_port/mod.rs index 424417fd5..9abfade32 100644 --- a/ostd/src/io/io_port/mod.rs +++ b/ostd/src/io/io_port/mod.rs @@ -48,12 +48,12 @@ impl IoPort { size_of::() as u16 } - /// Create an I/O port. + /// Creates an I/O port. /// /// # Safety /// - /// This function is marked unsafe as creating an I/O port is considered - /// a privileged operation. + /// Reading from or writing to the I/O port may have side effects. Those side effects must not + /// cause soundness problems (e.g., they must not corrupt the kernel memory). pub const unsafe fn new(port: u16) -> Self { Self { port, @@ -65,7 +65,6 @@ impl IoPort { impl IoPort { /// Reads from the I/O port - #[inline] pub fn read(&self) -> T { unsafe { PortRead::read_from_port(self.port) } } @@ -73,7 +72,6 @@ impl IoPort { impl IoPort { /// Writes to the I/O port - #[inline] pub fn write(&self, value: T) { unsafe { PortWrite::write_to_port(self.port, value) } } diff --git a/ostd/src/io/mod.rs b/ostd/src/io/mod.rs index 3be216ba3..7ec4eaefd 100644 --- a/ostd/src/io/mod.rs +++ b/ostd/src/io/mod.rs @@ -9,15 +9,14 @@ mod io_mem; -use cfg_if::cfg_if; - pub use self::io_mem::IoMem; pub(crate) use self::io_mem::IoMemAllocatorBuilder; -cfg_if!( +cfg_if::cfg_if!( if #[cfg(target_arch = "x86_64")] { mod io_port; - pub use io_port::IoPort; + + pub use self::io_port::IoPort; pub(crate) use self::io_port::{reserve_io_port_range, sensitive_io_port, RawIoPortRange}; } );