1use vstd::{prelude::*, resource::set::GhostSubset};
4use vstd_extra::external::{group_io_port_models, obeys_io_port_access_spec, valid_io_port_access};
5
6use crate::arch::io::MAX_IO_PORT;
7
8use crate::arch::device::io_port::{IoPortReadAccess, IoPortWriteAccess, PortRead, PortWrite};
9mod allocator;
10
11use core::{marker::PhantomData, mem::size_of};
12
13pub(super) use self::allocator::{init, io_port_allocator_initialized};
14use crate::{Error, prelude::*};
15
16verus! {
17
18broadcast use group_io_port_models;
19
20} #[verus_verify]
34#[derive(Debug)]
35pub struct IoPort<T, A> {
36 port: u16,
37 is_overlapping: bool,
38 value_marker: PhantomData<T>,
39 access_marker: PhantomData<A>,
40 #[cfg(verus_keep_ghost_body)]
42 tracked_claim: Tracked<Option<GhostSubset<usize>>>,
43}
44
45verus! {
46
47impl<T, A> View for IoPort<T, A> {
48 type V = u16;
49
50 closed spec fn view(&self) -> u16 {
51 self.port
52 }
53}
54
55impl<T, A> IoPort<T, A> {
56 #[verifier::type_invariant]
58 pub closed spec fn type_inv(self) -> bool {
59 &&& obeys_io_port_access_spec::<T>()
60 &&& valid_io_port_access::<T>(self@)
61 &&& self.tracked_claim@ matches Some(claim) ==> {
62 &&& allocator::io_port_allocator_initialized()
63 &&& self.claim_matches_set(claim@)
64 &&& claim.id() == allocator::io_port_allocator_instance_id()
65 }
66 }
67
68 pub closed spec fn is_allocated(self) -> bool {
70 self.tracked_claim@ is Some
71 }
72
73 pub closed spec fn is_overlapping(self) -> bool {
74 self.is_overlapping
75 }
76
77 pub open spec fn claim_matches_set(self, claim: Set<usize>) -> bool {
81 claim == Set::<usize>::range(
82 self@ as usize,
83 if self.is_overlapping() {
84 (self@ + 1) as usize
85 } else {
86 (self@ + size_of::<T>()) as usize
87 },
88 )
89 }
90}
91
92} #[verifier::external_body]
98#[verus_spec(
99 requires allocator::io_port_allocator_initialized(),
100)]
101fn initialized_allocator() -> &'static allocator::IoPortAllocator {
102 allocator::IO_PORT_ALLOCATOR.get().unwrap()
103}
104
105#[verus_verify]
106impl<T, A> IoPort<T, A> {
107 #[verus_spec(result =>
111 requires
112 size_of::<T>() == 1 || size_of::<T>() == 2 || size_of::<T>() == 4,
113 obeys_io_port_access_spec::<T>(),
114 allocator::io_port_allocator_initialized(),
115 ensures
116 port + size_of::<T>() > MAX_IO_PORT + 1 ==> result is Err,
117 result matches Ok(io_port) ==> {
118 &&& io_port@ == port
119 &&& !io_port.is_overlapping()
120 &&& io_port.is_allocated()
121 },
122 )]
123 pub fn acquire(port: u16) -> Result<IoPort<T, A>> {
124 initialized_allocator()
130 .acquire(port, false)
131 .ok_or(Error::AccessDenied)
132 }
133
134 #[verus_spec(result =>
138 requires
139 size_of::<T>() == 1 || size_of::<T>() == 2 || size_of::<T>() == 4,
140 obeys_io_port_access_spec::<T>(),
141 allocator::io_port_allocator_initialized(),
142 ensures
143 port + size_of::<T>() > MAX_IO_PORT + 1 ==> result is Err,
144 result matches Ok(io_port) ==> {
145 &&& io_port@ == port
146 &&& io_port.is_overlapping()
147 &&& io_port.is_allocated()
148 },
149 )]
150 pub fn acquire_overlapping(port: u16) -> Result<IoPort<T, A>> {
151 initialized_allocator()
157 .acquire(port, true)
158 .ok_or(Error::AccessDenied)
159 }
160
161 #[verus_spec(returns self@)]
163 pub const fn port(&self) -> u16 {
164 self.port
165 }
166
167 #[verus_spec(returns size_of::<T>() as u16)]
169 pub const fn size(&self) -> u16 {
170 size_of::<T>() as u16
171 }
172
173 #[verus_spec(ret =>
180 requires
181 obeys_io_port_access_spec::<T>(),
182 valid_io_port_access::<T>(port),
183 ensures
184 ret@ == port,
185 !ret.is_overlapping(),
186 )]
187 pub(crate) const unsafe fn new(port: u16) -> Self {
188 proof_with! { tracked_claim: Tracked(None)}
189 Self {
190 port,
191 is_overlapping: false,
192 value_marker: PhantomData,
193 access_marker: PhantomData,
194 }
195 }
196
197 #[verus_spec(ret =>
211 with Tracked(claim): Tracked<GhostSubset<usize>>,
212 requires
213 obeys_io_port_access_spec::<T>(),
214 valid_io_port_access::<T>(port),
215 allocator::io_port_allocator_initialized(),
216 claim.id() == allocator::io_port_allocator_instance_id(),
217 claim@ == Set::<usize>::range(
218 port as usize,
219 if is_overlapping {
220 (port + 1) as usize
221 } else {
222 (port + size_of::<T>()) as usize
223 },
224 ),
225 ensures
226 ret@ == port,
227 ret.is_overlapping() == is_overlapping,
228 ret.is_allocated(),
229 )]
230 unsafe fn new_overlapping(port: u16, is_overlapping: bool) -> Self {
231 proof_with! { tracked_claim: Tracked(Some(claim))}
232 Self {
233 port,
234 is_overlapping,
235 value_marker: PhantomData,
236 access_marker: PhantomData,
237 }
238 }
239
240 #[verus_spec(requires self.is_allocated())]
264 pub fn drop(self) {
265 proof! {
266 use_type_invariant(&self);
267 let claim = self.tracked_claim@.unwrap()@;
268 }
269 let port = self.port as usize;
270 let range = if !self.is_overlapping {
271 port..port + size_of::<T>()
272 } else {
273 port..port + 1
274 };
275
276 proof_decl! {
277 let tracked claim = self.tracked_claim.get().tracked_unwrap();
278 }
279 unsafe {
280 #[verus_spec(with Tracked(claim))]
281 initialized_allocator().recycle(range);
282 }
283 }
284}
285
286#[verus_verify]
287#[verifier::allow(undeclared_external_trait)]
288impl<T: PortRead, A: IoPortReadAccess> IoPort<T, A> {
289 pub fn read(&self) -> T {
291 proof! { use_type_invariant(self); }
292 unsafe { PortRead::read_from_port(self.port) }
293 }
294}
295
296#[verus_verify]
297#[verifier::allow(undeclared_external_trait)]
298impl<T: PortWrite, A: IoPortWriteAccess> IoPort<T, A> {
299 pub fn write(&self, value: T) {
301 proof! { use_type_invariant(self); }
302 unsafe { PortWrite::write_to_port(self.port, value) }
303 }
304}
305
306macro_rules! reserve_io_port_range {
314 ($range:expr) => {
315 crate::const_assert!(
316 $range.start < $range.end,
317 "I/O port range must be valid (start < end)"
318 );
319
320 const _: () = {
321 #[used]
322 #[unsafe(link_section = ".sensitive_io_ports")]
324 static _RANGE: crate::io::RawIoPortRange = crate::io::RawIoPortRange {
325 begin: $range.start,
326 end: $range.end,
327 };
328 };
329 };
330}
331
332macro_rules! sensitive_io_port {
352 (unsafe { $(
353 $(#[$meta:meta])*
354 $vis:vis static $name:ident: IoPort<$size:ty, $access:ty> = IoPort::new($port:expr);
355 )* }) => {
356 $(
357 $(#[$meta])*
358 $vis static $name: IoPort<$size, $access> = {
359 #[used]
360 #[unsafe(link_section = ".sensitive_io_ports")]
362 static _RESERVED_IO_PORT_RANGE: crate::io::RawIoPortRange = crate::io::RawIoPortRange {
363 begin: $name.port(),
364 end: $name.port() + $name.size(),
365 };
366
367 unsafe { IoPort::new($port) }
368 };
369 )*
370 };
371}
372
373pub(crate) use reserve_io_port_range;
374pub(crate) use sensitive_io_port;
375
376#[verus_verify]
377#[doc(hidden)]
378#[repr(C)]
379#[derive(Clone, Copy, Debug)]
380pub(crate) struct RawIoPortRange {
381 pub(crate) begin: u16,
382 pub(crate) end: u16,
383}