ostd/io/mod.rs
1// SPDX-License-Identifier: MPL-2.0
2//! Device I/O access and corresponding allocator.
3//!
4//! This module allows device drivers to access the device I/O they need
5//! through _allocators_. There are two types of device I/O:
6//! - `IoMem` for memory I/O (MMIO).
7//! - `IoPort` for port I/O (PIO).
8use vstd::prelude::*;
9
10pub(crate) mod io_mem;
11
12use cfg_if::cfg_if;
13
14pub use self::io_mem::IoMem;
15pub(crate) use self::io_mem::IoMemAllocatorBuilder;
16
17cfg_if!(
18 if #[cfg(target_arch = "x86_64")] {
19 mod io_port;
20
21 pub use self::io_port::IoPort;
22 pub(crate) use self::io_port::{reserve_io_port_range, sensitive_io_port, RawIoPortRange};
23 }
24);
25
26/// Initializes the static allocator based on builder.
27///
28/// # Safety
29///
30/// User must ensure that:
31///
32/// 1. All the memory that belong to the system device have been removed
33/// by calling the `remove` function.
34///
35/// 2. All the port I/O regions belonging to the system device are defined
36/// using the macros `sensitive_io_port` and `reserve_io_port_range`.
37///
38/// 3. `MAX_IO_PORT` defined in `crate::arch::io` is guaranteed not to
39/// exceed the maximum value specified by architecture.
40#[cfg_attr(target_arch = "x86_64", verus_spec(
41 requires
42 io_mem_builder.has_registered_windows(),
43 ensures
44 io_mem::io_mem_allocator_initialized(),
45 io_port::io_port_allocator_initialized(),
46))]
47#[cfg_attr(not(target_arch = "x86_64"), verus_spec(
48 requires
49 io_mem_builder.has_registered_windows(),
50 ensures
51 io_mem::io_mem_allocator_initialized(),
52))]
53pub(crate) unsafe fn init(io_mem_builder: IoMemAllocatorBuilder) {
54 // SAFETY: The safety is upheld by the caller.
55 unsafe { io_mem::init(io_mem_builder) };
56
57 // SAFETY: The safety is upheld by the caller.
58 #[cfg(target_arch = "x86_64")]
59 unsafe {
60 io_port::init()
61 };
62}