1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//! Abstractions for performing memory-mapped I/O.
//!
//! Memory-mapped I/O (MMIO) requires working with raw pointers and volatile
//! memory accesses, both of which require manually reasoning about safety.
//! This crate provides the [`VolBox`] (pronounced "volatile box") smart
//! pointer, which expresses unique ownership of a volatile memory location.
//! Additionally, it follows the "unsafe initialization, safe use" pattern to
//! offload safety reasoning to the borrow checker after [`VolBox::new`].
//!
//! Importantly, this crate is careful to [never create references to volatile
//! memory locations][volatile].
//!
//! [volatile]: https://lokathor.github.io/volatile/
//!
//! # Examples
//! ```no_run
//! # use mmio::*;
//! let mut thr = unsafe {
//!     VolBox::<u8, Allow, Allow>::new(0x1000_0000 as *mut u8)
//! };
//! let lsr = unsafe {
//!     VolBox::<u8, Allow, Allow>::new(0x1000_0005 as *mut u8)
//! };
//! loop {
//!     if lsr.read() & 0x20 != 0x0 {
//!         break;
//!     }
//! }
//! thr.write(b'\n');
//! ```

#![no_std]
#![allow(deprecated)]
#![warn(missing_docs)]
#![deny(unsafe_op_in_unsafe_fn)]

use ::core::{fmt, marker::PhantomData};

/// Allow access to a memory location.
#[derive(Debug)]
pub enum Allow {}

/// Allow access to a memory location under additional safety rules.
#[derive(Debug)]
pub enum Warn {}

/// Deny access to a memory location.
#[derive(Debug)]
pub enum Deny {}

/// An owned memory location for volatile reads and writes.
#[repr(transparent)]
#[derive(Debug)]
#[must_use]
pub struct VolBox<T, R, W> {
    loc: *mut T,
    r: PhantomData<R>,
    w: PhantomData<W>,
}

impl<T, R, W> VolBox<T, R, W> {
    /// Acquire ownership of a memory location.
    ///
    /// If either `R` or `W` are [`Warn`], this volatile box should document
    /// the additional safety requirements for [`Self::read`]/[`Self::read_at`]
    /// and [`Self::write`]/[`Self::write_at`] respectively.
    ///
    /// # Safety
    /// Behavior is undefined if any of the following conditions are violated
    /// during the lifetime of `self`:
    /// - `loc` must not be aliased by any reference or read/written thru any
    /// aliased pointer.
    /// - `loc` must be valid for reads if `R` is not [`Deny`].
    /// - `loc` must be valid for writes if `W` is not [`Deny`].
    /// - `loc` must be properly aligned.
    /// - `loc` must point to a properly initialized value of type `T` if `R`
    /// is not [`Deny`].
    pub const unsafe fn new(loc: *mut T) -> Self {
        Self {
            loc,
            r: PhantomData,
            w: PhantomData,
        }
    }

    /// Release ownership of the memory location.
    pub fn into_raw(self) -> *mut T {
        self.loc
    }
}

impl<T: Copy, W> VolBox<T, Warn, W> {
    /// Performs a volatile read on the owned memory location.
    ///
    /// # Safety
    /// Please consult the documentation on `self`.
    #[must_use]
    pub unsafe fn read(&self) -> T {
        // SAFETY: `read_volatile` is safe because the memory location is
        // owned, valid for reads, properly aligned, points to a properly
        // initialized value of type `T`, and `T` is `Copy`.
        unsafe { self.loc.read_volatile() }
    }
}

impl<T: Copy, W> VolBox<T, Allow, W> {
    /// Performs a volatile read on the owned memory location.
    #[must_use]
    pub fn read(&self) -> T {
        // SAFETY: `read_volatile` is safe because the memory location is
        // owned, valid for reads, properly aligned, points to a properly
        // initialized value of type `T`, and `T` is `Copy`.
        unsafe { self.loc.read_volatile() }
    }
}

impl<T: Copy, R> VolBox<T, R, Warn> {
    /// Performs a volatile write on the owned memory location.
    ///
    /// # Safety
    /// Please consult the documentation on `self`.
    pub unsafe fn write(&mut self, t: T) {
        // SAFETY: `write_volatile` is safe because the memory location is
        // owned, valid for writes, properly aligned, and `T` is `Copy`.
        unsafe { self.loc.write_volatile(t) };
    }
}

impl<T: Copy, R> VolBox<T, R, Allow> {
    /// Performs a volatile write on the owned memory location.
    pub fn write(&mut self, t: T) {
        // SAFETY: `write_volatile` is safe because the memory location is
        // owned, valid for writes, properly aligned, and `T` is `Copy`.
        unsafe { self.loc.write_volatile(t) };
    }
}

impl<T: Copy, W, const N: usize> VolBox<[T; N], Warn, W> {
    /// Performs a volatile read on the owned memory location at a specific
    /// index.
    ///
    /// # Panics
    /// Panics if the index is out of bounds.
    ///
    /// # Safety
    /// Please consult the documentation on `self`.
    #[must_use]
    pub unsafe fn read_at(&self, i: usize) -> T {
        assert!(i < N);
        let loc = self.loc as *mut T;
        // SAFETY: `add` is safe because the index is within bounds of the
        // array.
        let loc = unsafe { loc.add(i) };
        // SAFETY: `read_volatile` is safe because the memory location is
        // owned, valid for reads, properly aligned, points to a properly
        // initialized value of type `T`, and `T` is `Copy`.
        unsafe { loc.read_volatile() }
    }
}

impl<T: Copy, W, const N: usize> VolBox<[T; N], Allow, W> {
    /// Performs a volatile read on the owned memory location at a specific
    /// index.
    ///
    /// # Panics
    /// Panics if the index is out of bounds.
    #[must_use]
    pub fn read_at(&self, i: usize) -> T {
        assert!(i < N);
        let loc = self.loc as *mut T;
        // SAFETY: `add` is safe because the index is within bounds of the
        // array.
        let loc = unsafe { loc.add(i) };
        // SAFETY: `read_volatile` is safe because the memory location is
        // owned, valid for reads, properly aligned, points to a properly
        // initialized value of type `T`, and `T` is `Copy`.
        unsafe { loc.read_volatile() }
    }
}

impl<T: Copy, R, const N: usize> VolBox<[T; N], R, Warn> {
    /// Performs a volatile write on the owned memory location at a specific
    /// index.
    ///
    /// # Panics
    /// Panics if the index is out of bounds.
    ///
    /// # Safety
    /// Please consult the documentation on `self`.
    pub unsafe fn write_at(&mut self, i: usize, t: T) {
        assert!(i < N);
        let loc = self.loc as *mut T;
        // SAFETY: `add` is safe because the index is within bounds of the
        // array.
        let loc = unsafe { loc.add(i) };
        // SAFETY: `write_volatile` is safe because the memory location is
        // owned, valid for writes, properly aligned, and `T` is `Copy`.
        unsafe { loc.write_volatile(t) };
    }
}

impl<T: Copy, R, const N: usize> VolBox<[T; N], R, Allow> {
    /// Performs a volatile write on the owned memory location at a specific
    /// index.
    ///
    /// # Safety
    /// Please consult the documentation on `self`.
    pub fn write_at(&mut self, i: usize, t: T) {
        assert!(i < N);
        let loc = self.loc as *mut T;
        // SAFETY: `add` is safe because the index is within bounds of the
        // array.
        let loc = unsafe { loc.add(i) };
        // SAFETY: `write_volatile` is safe because the memory location is
        // owned, valid for writes, properly aligned, and `T` is `Copy`.
        unsafe { loc.write_volatile(t) };
    }
}

impl<T, R, W> fmt::Pointer for VolBox<T, R, W> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.loc.fmt(f)
    }
}

// SAFETY: `Send` is safe because `T` is `Send`.
unsafe impl<T: Send, R, W> Send for VolBox<T, R, W> {}

// SAFETY: `Sync` is safe because `T` is `Sync`.
unsafe impl<T: Sync, R, W> Sync for VolBox<T, R, W> {}