| 1 |
//! Provides wrapper types `Volatile`, `ReadOnly`, `WriteOnly`, `ReadWrite`, which wrap any copy-able type and allows for |
| 2 |
//! volatile memory access to wrapped value. Volatile memory accesses are never optimized away by |
| 3 |
//! the compiler, and are useful in many low-level systems programming and concurrent contexts. |
| 4 |
//! |
| 5 |
//! The wrapper types *do not* enforce any atomicity guarantees; to also get atomicity, consider |
| 6 |
//! looking at the `Atomic` wrapper type found in `libcore` or `libstd`. |
| 7 |
//! |
| 8 |
//! These wrappers do not depend on the standard library and never panic. |
| 9 |
//! |
| 10 |
//! # Dealing with Volatile Pointers |
| 11 |
//! |
| 12 |
//! Frequently, one may have to deal with volatile pointers, eg, writes to specific memory |
| 13 |
//! locations. The canonical way to solve this is to cast the pointer to a volatile wrapper |
| 14 |
//! directly, eg: |
| 15 |
//! |
| 16 |
//! ```rust |
| 17 |
//! use volatile::Volatile; |
| 18 |
//! |
| 19 |
//! let mut_ptr = 0xFEE00000 as *mut u32; |
| 20 |
//! |
| 21 |
//! let volatile_ptr = mut_ptr as *mut Volatile<u32>; |
| 22 |
//! ``` |
| 23 |
//! |
| 24 |
//! and then perform operations on the pointer as usual in a volatile way. This method works as all |
| 25 |
//! of the volatile wrapper types are the same size as their contained values. |
| 26 |
#![no_std] |
| 27 |
|
| 28 |
use core::ptr; |
| 29 |
|
| 30 |
/// A wrapper type around a volatile variable, which allows for volatile reads and writes |
| 31 |
/// to the contained value. The stored type needs to be `Copy`, as volatile reads and writes |
| 32 |
/// take and return copies of the value. |
| 33 |
/// |
| 34 |
/// The size of this struct is the same as the size of the contained type. |
| 35 |
#[derive(Debug, Default)] |
| 36 |
#[repr(transparent)] |
| 37 |
pub struct Volatile<T: Copy>(T); |
| 38 |
|
| 39 |
impl<T: Copy> Volatile<T> { |
| 40 |
/// Construct a new volatile instance wrapping the given value. |
| 41 |
/// |
| 42 |
/// ```rust |
| 43 |
/// use volatile::Volatile; |
| 44 |
/// |
| 45 |
/// let value = Volatile::new(0u32); |
| 46 |
/// ``` |
| 47 |
/// |
| 48 |
/// # Panics |
| 49 |
/// |
| 50 |
/// This method never panics. |
| 51 |
#[cfg(feature = "const_fn")] |
| 52 |
pub const fn new(value: T) -> Volatile<T> { |
| 53 |
Volatile(value) |
| 54 |
} |
| 55 |
|
| 56 |
/// Construct a new volatile instance wrapping the given value. |
| 57 |
/// |
| 58 |
/// ```rust |
| 59 |
/// use volatile::Volatile; |
| 60 |
/// |
| 61 |
/// let value = Volatile::new(0u32); |
| 62 |
/// ``` |
| 63 |
/// |
| 64 |
/// # Panics |
| 65 |
/// |
| 66 |
/// This method never panics. |
| 67 |
#[cfg(not(feature = "const_fn"))] |
| 68 |
pub fn new(value: T) -> Volatile<T> { |
| 69 |
Volatile(value) |
| 70 |
} |
| 71 |
|
| 72 |
/// Performs a volatile read of the contained value, returning a copy |
| 73 |
/// of the read value. Volatile reads are guaranteed not to be optimized |
| 74 |
/// away by the compiler, but by themselves do not have atomic ordering |
| 75 |
/// guarantees. To also get atomicity, consider looking at the `Atomic` wrapper type. |
| 76 |
/// |
| 77 |
/// ```rust |
| 78 |
/// use volatile::Volatile; |
| 79 |
/// |
| 80 |
/// let value = Volatile::new(42u32); |
| 81 |
/// |
| 82 |
/// assert_eq!(value.read(), 42u32); |
| 83 |
/// ``` |
| 84 |
/// |
| 85 |
/// # Panics |
| 86 |
/// |
| 87 |
/// This method never panics. |
| 88 |
pub fn read(&self) -> T { |
| 89 |
// UNSAFE: Safe, as we know that our internal value exists. |
| 90 |
unsafe { ptr::read_volatile(&self.0) } |
| 91 |
} |
| 92 |
|
| 93 |
/// Performs a volatile write, setting the contained value to the given value `value`. Volatile |
| 94 |
/// writes are guaranteed to not be optimized away by the compiler, but by themselves do not |
| 95 |
/// have atomic ordering guarantees. To also get atomicity, consider looking at the `Atomic` |
| 96 |
/// wrapper type. |
| 97 |
/// |
| 98 |
/// ```rust |
| 99 |
/// use volatile::Volatile; |
| 100 |
/// |
| 101 |
/// let mut value = Volatile::new(0u32); |
| 102 |
/// |
| 103 |
/// value.write(42u32); |
| 104 |
/// |
| 105 |
/// assert_eq!(value.read(), 42u32); |
| 106 |
/// ``` |
| 107 |
/// |
| 108 |
/// # Panics |
| 109 |
/// |
| 110 |
/// This method never panics. |
| 111 |
pub fn write(&mut self, value: T) { |
| 112 |
// UNSAFE: Safe, as we know that our internal value exists. |
| 113 |
unsafe { ptr::write_volatile(&mut self.0, value) }; |
| 114 |
} |
| 115 |
|
| 116 |
/// Performs a volatile read of the contained value, passes a mutable reference to it to the |
| 117 |
/// function `f`, and then performs a volatile write of the (potentially updated) value back to |
| 118 |
/// the contained value. |
| 119 |
/// |
| 120 |
/// ```rust |
| 121 |
/// use volatile::Volatile; |
| 122 |
/// |
| 123 |
/// let mut value = Volatile::new(21u32); |
| 124 |
/// |
| 125 |
/// value.update(|val_ref| *val_ref *= 2); |
| 126 |
/// |
| 127 |
/// assert_eq!(value.read(), 42u32); |
| 128 |
/// ``` |
| 129 |
/// |
| 130 |
/// # Panics |
| 131 |
/// |
| 132 |
/// Ths method never panics. |
| 133 |
pub fn update<F>(&mut self, f: F) |
| 134 |
where |
| 135 |
F: FnOnce(&mut T), |
| 136 |
{ |
| 137 |
let mut value = self.read(); |
| 138 |
f(&mut value); |
| 139 |
self.write(value); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
impl<T: Copy> Clone for Volatile<T> { |
| 144 |
fn clone(&self) -> Self { |
| 145 |
Volatile(self.read()) |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
/// A volatile wrapper which only allows read operations. |
| 150 |
/// |
| 151 |
/// The size of this struct is the same as the contained type. |
| 152 |
#[derive(Debug, Clone, Default)] |
| 153 |
pub struct ReadOnly<T: Copy>(Volatile<T>); |
| 154 |
|
| 155 |
impl<T: Copy> ReadOnly<T> { |
| 156 |
/// Construct a new read-only volatile wrapper wrapping the given value. |
| 157 |
/// |
| 158 |
/// ```rust |
| 159 |
/// use volatile::ReadOnly; |
| 160 |
/// |
| 161 |
/// let value = ReadOnly::new(42u32); |
| 162 |
/// ``` |
| 163 |
/// |
| 164 |
/// # Panics |
| 165 |
/// |
| 166 |
/// This function never panics. |
| 167 |
#[cfg(feature = "const_fn")] |
| 168 |
pub const fn new(value: T) -> ReadOnly<T> { |
| 169 |
ReadOnly(Volatile::new(value)) |
| 170 |
} |
| 171 |
|
| 172 |
/// Construct a new read-only volatile wrapper wrapping the given value. |
| 173 |
/// |
| 174 |
/// ```rust |
| 175 |
/// use volatile::ReadOnly; |
| 176 |
/// |
| 177 |
/// let value = ReadOnly::new(42u32); |
| 178 |
/// ``` |
| 179 |
/// |
| 180 |
/// # Panics |
| 181 |
/// |
| 182 |
/// This function never panics. |
| 183 |
#[cfg(not(feature = "const_fn"))] |
| 184 |
pub fn new(value: T) -> ReadOnly<T> { |
| 185 |
ReadOnly(Volatile::new(value)) |
| 186 |
} |
| 187 |
|
| 188 |
/// Perform a volatile read of the contained value, returning a copy of the read value. |
| 189 |
/// Functionally equivalent to `Volatile::read`. |
| 190 |
/// |
| 191 |
/// ```rust |
| 192 |
/// use volatile::ReadOnly; |
| 193 |
/// |
| 194 |
/// let value = ReadOnly::new(42u32); |
| 195 |
/// assert_eq!(value.read(), 42u32); |
| 196 |
/// ``` |
| 197 |
/// |
| 198 |
/// # Panics |
| 199 |
/// |
| 200 |
/// This function never panics. |
| 201 |
pub fn read(&self) -> T { |
| 202 |
self.0.read() |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
/// A volatile wrapper which only allows write operations. |
| 207 |
/// |
| 208 |
/// The size of this struct is the same as the contained type. |
| 209 |
#[derive(Default)] |
| 210 |
pub struct WriteOnly<T: Copy>(Volatile<T>); |
| 211 |
|
| 212 |
impl<T: Copy> WriteOnly<T> { |
| 213 |
/// Constructs a new write only volatile wrapper around the given value. |
| 214 |
/// |
| 215 |
/// ```rust |
| 216 |
/// use volatile::WriteOnly; |
| 217 |
/// |
| 218 |
/// let value = WriteOnly::new(0u32); |
| 219 |
/// ``` |
| 220 |
/// |
| 221 |
/// # Panics |
| 222 |
/// |
| 223 |
/// This function never panics. |
| 224 |
#[cfg(feature = "const_fn")] |
| 225 |
pub const fn new(value: T) -> WriteOnly<T> { |
| 226 |
WriteOnly(Volatile::new(value)) |
| 227 |
} |
| 228 |
|
| 229 |
/// Constructs a new write only volatile wrapper around the given value. |
| 230 |
/// |
| 231 |
/// ```rust |
| 232 |
/// use volatile::WriteOnly; |
| 233 |
/// |
| 234 |
/// let value = WriteOnly::new(0u32); |
| 235 |
/// ``` |
| 236 |
/// |
| 237 |
/// # Panics |
| 238 |
/// |
| 239 |
/// This function never panics. |
| 240 |
#[cfg(not(feature = "const_fn"))] |
| 241 |
pub fn new(value: T) -> WriteOnly<T> { |
| 242 |
WriteOnly(Volatile::new(value)) |
| 243 |
} |
| 244 |
|
| 245 |
/// Performs a volatile write of value `value` into the contained value. Functionally identical |
| 246 |
/// to `Volatile::write`. |
| 247 |
/// |
| 248 |
/// ```rust |
| 249 |
/// use volatile::WriteOnly; |
| 250 |
/// |
| 251 |
/// let mut value = WriteOnly::new(0u32); |
| 252 |
/// |
| 253 |
/// value.write(42u32); |
| 254 |
/// ``` |
| 255 |
/// |
| 256 |
/// # Panics |
| 257 |
/// |
| 258 |
/// This method never panics. |
| 259 |
pub fn write(&mut self, value: T) { |
| 260 |
self.0.write(value) |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
/// A volatile wrapper which allows both read and write operations; |
| 265 |
/// functionally equivalent to the `Volatile` type, as it is a type |
| 266 |
/// alias for it. |
| 267 |
/// |
| 268 |
/// The size of this struct is the same as the contained type. |
| 269 |
pub type ReadWrite<T> = Volatile<T>; |
| 270 |
|
| 271 |
#[cfg(test)] |
| 272 |
mod tests { |
| 273 |
use super::Volatile; |
| 274 |
|
| 275 |
#[test] |
| 276 |
fn test_read() { |
| 277 |
assert_eq!(Volatile(42).read(), 42); |
| 278 |
} |
| 279 |
|
| 280 |
#[test] |
| 281 |
fn test_write() { |
| 282 |
let mut volatile = Volatile(42); |
| 283 |
volatile.write(50); |
| 284 |
assert_eq!(volatile.0, 50); |
| 285 |
} |
| 286 |
|
| 287 |
#[test] |
| 288 |
fn test_update() { |
| 289 |
let mut volatile = Volatile(42); |
| 290 |
volatile.update(|v| *v += 1); |
| 291 |
assert_eq!(volatile.0, 43); |
| 292 |
} |
| 293 |
|
| 294 |
#[test] |
| 295 |
fn test_pointer_recast() { |
| 296 |
let mut target_value = 0u32; |
| 297 |
|
| 298 |
let target_ptr: *mut u32 = &mut target_value; |
| 299 |
let volatile_ptr = target_ptr as *mut Volatile<u32>; |
| 300 |
|
| 301 |
// UNSAFE: Safe, as we know the value exists on the stack. |
| 302 |
unsafe { |
| 303 |
(*volatile_ptr).write(42u32); |
| 304 |
} |
| 305 |
|
| 306 |
assert_eq!(target_value, 42u32); |
| 307 |
} |
| 308 |
} |