From 58010b27310cc27df542294f81a19ea95feded5f Mon Sep 17 00:00:00 2001 From: Daniel Verkamp Date: Fri, 9 Aug 2019 14:25:03 -0700 Subject: 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 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1749562 Reviewed-by: Dylan Reid Tested-by: kokoro --- devices/src/virtio/pmem.rs | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) (limited to 'devices/src/virtio/pmem.rs') 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( -- cgit 1.4.1