summary refs log tree commit diff
path: root/devices/src/virtio/pmem.rs
diff options
context:
space:
mode:
authorDaniel Verkamp <dverkamp@chromium.org>2019-08-09 14:25:03 -0700
committerCommit Bot <commit-bot@chromium.org>2019-08-16 07:20:21 +0000
commit58010b27310cc27df542294f81a19ea95feded5f (patch)
treeb9dc4cc8203edc1b4c5b26bed7c9fc585acc076d /devices/src/virtio/pmem.rs
parent35a9d838dbb92b89e8baef380a550b8d8a4b15ad (diff)
downloadcrosvm-58010b27310cc27df542294f81a19ea95feded5f.tar
crosvm-58010b27310cc27df542294f81a19ea95feded5f.tar.gz
crosvm-58010b27310cc27df542294f81a19ea95feded5f.tar.bz2
crosvm-58010b27310cc27df542294f81a19ea95feded5f.tar.lz
crosvm-58010b27310cc27df542294f81a19ea95feded5f.tar.xz
crosvm-58010b27310cc27df542294f81a19ea95feded5f.tar.zst
crosvm-58010b27310cc27df542294f81a19ea95feded5f.zip
devices: virtio: add copy_config() helper function
Add a new virtio configuration copying function to replace all of the
slightly varying read_config() and write_config() implementations in our
virtio devices.  This replaces a lot of tricky bounds-checking code with
a single central implementation, simplifying the devices to a single
call to copy_config() in most cases.

The balloon device is also changed to represent its config space as a
DataInit struct to match most other devices and remove several unwrap()
calls.

BUG=None
TEST=./build_test
TEST=Boot vm_kernel+vm_rootfs in crosvm
TEST=Start Crostini on nami

Change-Id: Ia49bd6dbe609d17455b9562086bc0b24f327be3f
Signed-off-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1749562
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Diffstat (limited to 'devices/src/virtio/pmem.rs')
-rw-r--r--devices/src/virtio/pmem.rs21
1 files changed, 5 insertions, 16 deletions
diff --git a/devices/src/virtio/pmem.rs b/devices/src/virtio/pmem.rs
index 607ce58..b9e42cc 100644
--- a/devices/src/virtio/pmem.rs
+++ b/devices/src/virtio/pmem.rs
@@ -2,11 +2,9 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-use std::cmp;
 use std::fmt::{self, Display};
 use std::fs::File;
-use std::io::Write;
-use std::mem::{size_of, size_of_val};
+use std::mem::size_of;
 use std::os::unix::io::{AsRawFd, RawFd};
 use std::result;
 use std::sync::atomic::{AtomicUsize, Ordering};
@@ -21,7 +19,8 @@ use sys_util::{
 use data_model::{DataInit, Le32, Le64};
 
 use super::{
-    DescriptorChain, Queue, VirtioDevice, INTERRUPT_STATUS_USED_RING, TYPE_PMEM, VIRTIO_F_VERSION_1,
+    copy_config, DescriptorChain, Queue, VirtioDevice, INTERRUPT_STATUS_USED_RING, TYPE_PMEM,
+    VIRTIO_F_VERSION_1,
 };
 
 const QUEUE_SIZE: u16 = 256;
@@ -297,22 +296,12 @@ impl VirtioDevice for Pmem {
         1 << VIRTIO_F_VERSION_1
     }
 
-    fn read_config(&self, offset: u64, mut data: &mut [u8]) {
+    fn read_config(&self, offset: u64, data: &mut [u8]) {
         let config = virtio_pmem_config {
             start_address: Le64::from(self.mapping_address.offset()),
             size: Le64::from(self.mapping_size as u64),
         };
-        let config_len = size_of_val(&config) as u64;
-        if offset >= config_len {
-            return;
-        }
-
-        if let Some(end) = offset.checked_add(data.len() as u64) {
-            let offset = offset as usize;
-            let end = cmp::min(end, config_len) as usize;
-            // This write can't fail, offset and end are checked against config_len.
-            data.write_all(&config.as_slice()[offset..end]).unwrap();
-        }
+        copy_config(data, 0, config.as_slice(), offset);
     }
 
     fn activate(