• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 //! Utilities to access GuestMemory with IO virtual addresses and iommu
6 
7 use anyhow::Context;
8 use vm_memory::GuestAddress;
9 use vm_memory::GuestMemory;
10 use zerocopy::AsBytes;
11 use zerocopy::FromBytes;
12 
13 use crate::virtio::iommu::ExportedRegion;
14 
15 /// A wrapper that works with gpa, or iova and an iommu.
read_obj_from_addr_wrapper<T: FromBytes>( mem: &GuestMemory, exported_region: &Option<ExportedRegion>, addr: GuestAddress, ) -> anyhow::Result<T>16 pub fn read_obj_from_addr_wrapper<T: FromBytes>(
17     mem: &GuestMemory,
18     exported_region: &Option<ExportedRegion>,
19     addr: GuestAddress,
20 ) -> anyhow::Result<T> {
21     if let Some(exported_region) = exported_region {
22         exported_region.read_obj_from_addr::<T>(mem, addr.offset())
23     } else {
24         mem.read_obj_from_addr_volatile::<T>(addr)
25             .context("read_obj_from_addr failed")
26     }
27 }
28 
29 /// A wrapper that works with gpa, or iova and an iommu.
write_obj_at_addr_wrapper<T: FromBytes + AsBytes>( mem: &GuestMemory, exported_region: &Option<ExportedRegion>, val: T, addr: GuestAddress, ) -> anyhow::Result<()>30 pub fn write_obj_at_addr_wrapper<T: FromBytes + AsBytes>(
31     mem: &GuestMemory,
32     exported_region: &Option<ExportedRegion>,
33     val: T,
34     addr: GuestAddress,
35 ) -> anyhow::Result<()> {
36     if let Some(exported_region) = exported_region {
37         exported_region.write_obj_at_addr(mem, val, addr.offset())
38     } else {
39         mem.write_obj_at_addr_volatile(val, addr)
40             .context("write_obj_at_addr failed")
41     }
42 }
43