summary refs log tree commit diff
path: root/devices/src/virtio/p9.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/p9.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/p9.rs')
-rw-r--r--devices/src/virtio/p9.rs17
1 files changed, 4 insertions, 13 deletions
diff --git a/devices/src/virtio/p9.rs b/devices/src/virtio/p9.rs
index a3cb007..6968733 100644
--- a/devices/src/virtio/p9.rs
+++ b/devices/src/virtio/p9.rs
@@ -2,7 +2,6 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-use std::cmp::min;
 use std::fmt::{self, Display};
 use std::io::{self, Write};
 use std::mem;
@@ -17,7 +16,9 @@ use p9;
 use sys_util::{error, warn, Error as SysError, EventFd, GuestMemory, PollContext, PollToken};
 use virtio_sys::vhost::VIRTIO_F_VERSION_1;
 
-use super::{Queue, Reader, VirtioDevice, Writer, INTERRUPT_STATUS_USED_RING, TYPE_9P};
+use super::{
+    copy_config, Queue, Reader, VirtioDevice, Writer, INTERRUPT_STATUS_USED_RING, TYPE_9P,
+};
 
 const QUEUE_SIZE: u16 = 128;
 const QUEUE_SIZES: &[u16] = &[QUEUE_SIZE];
@@ -222,17 +223,7 @@ impl VirtioDevice for P9 {
     }
 
     fn read_config(&self, offset: u64, data: &mut [u8]) {
-        if offset >= self.config.len() as u64 {
-            // Nothing to see here.
-            return;
-        }
-
-        // The config length cannot be more than ::std::u16::MAX + mem::size_of::<u16>(), which
-        // is significantly smaller than ::std::usize::MAX on the architectures we care about so
-        // if we reach this point then we know that `offset` will fit into a usize.
-        let offset = offset as usize;
-        let len = min(data.len(), self.config.len() - offset);
-        data[..len].copy_from_slice(&self.config[offset..offset + len])
+        copy_config(data, 0, self.config.as_slice(), offset);
     }
 
     fn activate(