Skip to main content

fake_mmio/inject/
passthrough.rs

1// Copyright 2026 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::inject::{GenericVmoMemoryHandler, VmoOpHelper};
6use crate::operand::MmioOperand;
7
8/// An MMIO handler that directly passes loads and stores to the VMO memory.
9#[derive(Default, Debug, Copy, Clone)]
10pub struct Passthrough;
11
12impl GenericVmoMemoryHandler for Passthrough {
13    fn load<T: MmioOperand>(&self, op: VmoOpHelper<'_, T>) -> T {
14        op.load()
15    }
16
17    fn store<T: MmioOperand>(&self, op: VmoOpHelper<'_, T>, value: T) {
18        op.store(value)
19    }
20}
21
22#[cfg(test)]
23mod tests {
24    use super::*;
25    use crate::CachedVmoMemory;
26    use crate::inject::VmoMemory;
27    use crate::operand::MmioOperand;
28    use crate::operand::tests::OperandCallable;
29    use mmio::MmioExt as _;
30    use mmio::region::MmioRegion;
31
32    #[test]
33    fn test_passthrough() {
34        struct TestRunner<'a>(&'a mut MmioRegion<VmoMemory<Passthrough>>);
35
36        impl OperandCallable for TestRunner<'_> {
37            fn call<T: MmioOperand>(&mut self) {
38                let offset = 0;
39                let val = T::try_from(0x42).unwrap();
40                self.0.store::<T>(offset, val);
41                assert_eq!(self.0.load::<T>(offset), val);
42            }
43        }
44
45        let mut mmio = CachedVmoMemory::new_mapped(4096)
46            .expect("Failed to map VMO.")
47            .map(|vmo| VmoMemory::new(Passthrough, vmo));
48
49        TestRunner(&mut mmio).call_all();
50    }
51}