Skip to main content

vstd_extra/external/
io_port.rs

1//! Specifications for x86 I/O-port access types and traits.
2use core::mem::size_of;
3
4use vstd::prelude::*;
5use x86_64::{
6    instructions::port::{
7        PortAccess, PortReadAccess, PortWriteAccess, ReadWriteAccess, WriteOnlyAccess,
8    },
9    structures::port::{PortRead, PortWrite},
10};
11
12verus! {
13
14/// Whether `T` follows the standard x86 PIO width and address-validity model.
15/// Implementing `PortRead` or `PortWrite` alone does not establish this law.
16pub uninterp spec fn obeys_io_port_access_spec<T>() -> bool;
17
18/// Whether a `T`-typed access at `port` fits in the PIO byte range `0..=u16::MAX`.
19///
20/// Uninterpreted: `PortRead`/`PortWrite` are user-implementable, so meaning comes only from
21/// the trusted widths in [`group_io_port_models`]. ISA-level fact only — no claim about
22/// device decoding, access permission, or ownership.
23///
24/// Used as a precondition for typed reads and writes.
25pub uninterp spec fn valid_io_port_access<T>(port: u16) -> bool;
26
27/// Trusted: types obeying the model use a 1-, 2-, or 4-byte access within the PIO space.
28pub broadcast axiom fn axiom_io_port_access_spec<T>(port: u16)
29    ensures
30        #[trigger] obeys_io_port_access_spec::<T>() ==> (#[trigger] valid_io_port_access::<T>(port)
31            <==> {
32            &&& size_of::<T>() == 1 || size_of::<T>() == 2 || size_of::<T>() == 4
33            &&& port + size_of::<T>() <= u16::MAX + 1
34        }),
35;
36
37/// Trusted: `u8` ports are written and read via `outb`/`inb`.
38pub broadcast axiom fn axiom_u8_obeys_io_port_access_spec()
39    ensures
40        #[trigger] obeys_io_port_access_spec::<u8>(),
41;
42
43/// Trusted: `u16` ports are written and read via `outw`/`inw`.
44pub broadcast axiom fn axiom_u16_obeys_io_port_access_spec()
45    ensures
46        #[trigger] obeys_io_port_access_spec::<u16>(),
47;
48
49/// Trusted: `u32` ports are written and read via `outl`/`inl`.
50pub broadcast axiom fn axiom_u32_obeys_io_port_access_spec()
51    ensures
52        #[trigger] obeys_io_port_access_spec::<u32>(),
53;
54
55/// The trusted instances of the PIO model.
56pub broadcast group group_io_port_models {
57    axiom_io_port_access_spec,
58    axiom_u8_obeys_io_port_access_spec,
59    axiom_u16_obeys_io_port_access_spec,
60    axiom_u32_obeys_io_port_access_spec,
61}
62
63/// Opaque specification boundary for the third-party read/write access marker.
64#[verifier::external_type_specification]
65#[verifier::external_body]
66pub struct ExReadWriteAccess(ReadWriteAccess);
67
68/// Opaque specification boundary for the third-party write-only access marker.
69#[verifier::external_type_specification]
70#[verifier::external_body]
71pub struct ExWriteOnlyAccess(WriteOnlyAccess);
72
73/// External declaration of the access marker, omitting its private sealing bound.
74#[verifier::external_trait_specification]
75#[verifier::external_trait_private_bound(x86_64::sealed::Sealed)]
76pub trait ExPortAccess {
77    type ExternalTraitSpecificationFor: PortAccess;
78}
79
80/// External declaration of the marker for readable I/O ports.
81#[verifier::external_trait_specification]
82pub trait ExPortReadAccess: PortAccess {
83    type ExternalTraitSpecificationFor: PortReadAccess;
84}
85
86/// External declaration of the marker for writable I/O ports.
87#[verifier::external_trait_specification]
88pub trait ExPortWriteAccess: PortAccess {
89    type ExternalTraitSpecificationFor: PortWriteAccess;
90}
91
92/// Trusted specification boundary for values that can be read from an x86 I/O port.
93#[verifier::external_trait_specification]
94pub trait ExPortRead {
95    type ExternalTraitSpecificationFor: PortRead;
96
97    /// A port read can produce any value supplied by the device.
98    unsafe fn read_from_port(port: u16) -> Self where Self: Sized
99        requires
100            valid_io_port_access::<Self>(port),
101    ;
102}
103
104/// Trusted specification boundary for values that can be written to an x86 I/O port.
105#[verifier::external_trait_specification]
106pub trait ExPortWrite {
107    type ExternalTraitSpecificationFor: PortWrite;
108
109    /// A port write has no modeled logical effect on kernel memory.
110    unsafe fn write_to_port(port: u16, value: Self) where Self: Sized
111        requires
112            valid_io_port_access::<Self>(port),
113    ;
114}
115
116} // verus!