misc: Stop using deprecated functions from vm-memory crate

See: https://github.com/rust-vmm/vm-memory/pull/247

Signed-off-by: Bo Chen <chen.bo@intel.com>
This commit is contained in:
Bo Chen 2023-11-08 15:01:18 -08:00 committed by Rob Bradford
parent 4d7a4c598a
commit d4892f41b3
8 changed files with 74 additions and 49 deletions

View File

@ -1,8 +1,9 @@
// Copyright 2020 Arm Limited (or its affiliates). All rights reserved.
use std::io::{Read, Seek, SeekFrom};
use std::os::fd::AsFd;
use std::result;
use vm_memory::{Bytes, GuestAddress, GuestMemory};
use vm_memory::{GuestAddress, GuestMemory};
/// Errors thrown while loading UEFI binary
#[derive(Debug)]
@ -24,7 +25,7 @@ pub fn load_uefi<F, M: GuestMemory>(
uefi_image: &mut F,
) -> Result<()>
where
F: Read + Seek,
F: Read + Seek + AsFd,
{
let uefi_size = uefi_image
.seek(SeekFrom::End(0))
@ -36,6 +37,6 @@ where
}
uefi_image.rewind().map_err(|_| Error::SeekUefiStart)?;
guest_mem
.read_exact_from(guest_addr, uefi_image, uefi_size)
.read_exact_volatile_from(guest_addr, &mut uefi_image.as_fd(), uefi_size)
.map_err(|_| Error::ReadUefiImage)
}

View File

@ -9,7 +9,6 @@ use crate::layout::{APIC_START, HIGH_RAM_START, IOAPIC_START};
use crate::x86_64::mpspec;
use crate::GuestMemoryMmap;
use libc::c_char;
use std::io;
use std::mem;
use std::result;
use std::slice;
@ -154,7 +153,7 @@ pub fn setup_mptable(offset: GuestAddress, mem: &GuestMemoryMmap, num_cpus: u8)
return Err(Error::AddressOverflow);
}
mem.read_exact_from(base_mp, &mut io::repeat(0), mp_size)
mem.read_exact_volatile_from(base_mp, &mut vec![0; mp_size].as_slice(), mp_size)
.map_err(Error::Clear)?;
{
@ -291,7 +290,10 @@ pub fn setup_mptable(offset: GuestAddress, mem: &GuestMemoryMmap, num_cpus: u8)
mod tests {
use super::*;
use crate::layout::MPTABLE_START;
use vm_memory::{GuestAddress, GuestUsize};
use vm_memory::{
bitmap::BitmapSlice, GuestAddress, GuestUsize, VolatileMemoryError, VolatileSlice,
WriteVolatile,
};
fn table_entry_size(type_: u8) -> usize {
match type_ as u32 {
@ -351,20 +353,24 @@ mod tests {
let mpc_table: MpcTableWrapper = mem.read_obj(mpc_offset).unwrap();
struct Sum(u8);
impl io::Write for Sum {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
for v in buf.iter() {
impl WriteVolatile for Sum {
fn write_volatile<B: BitmapSlice>(
&mut self,
buf: &VolatileSlice<B>,
) -> result::Result<usize, VolatileMemoryError> {
let mut tmp = vec![0u8; buf.len()];
tmp.write_all_volatile(buf)?;
for v in tmp.iter() {
self.0 = self.0.wrapping_add(*v);
}
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
let mut sum = Sum(0);
mem.write_to(mpc_offset, &mut sum, mpc_table.0.length as usize)
mem.write_volatile_to(mpc_offset, &mut sum, mpc_table.0.length as usize)
.unwrap();
assert_eq!(sum.0, 0);
}

View File

@ -143,10 +143,14 @@ pub enum ExecuteError {
Flush(io::Error),
#[error("Failed to read: {0}")]
Read(GuestMemoryError),
#[error("Failed to read_exact: {0}")]
ReadExact(io::Error),
#[error("Failed to seek: {0}")]
Seek(io::Error),
#[error("Failed to write: {0}")]
Write(GuestMemoryError),
#[error("Failed to write_all: {0}")]
WriteAll(io::Error),
#[error("Unsupported request: {0}")]
Unsupported(u32),
#[error("Failed to submit io uring: {0}")]
@ -169,8 +173,10 @@ impl ExecuteError {
ExecuteError::BadRequest(_) => VIRTIO_BLK_S_IOERR,
ExecuteError::Flush(_) => VIRTIO_BLK_S_IOERR,
ExecuteError::Read(_) => VIRTIO_BLK_S_IOERR,
ExecuteError::ReadExact(_) => VIRTIO_BLK_S_IOERR,
ExecuteError::Seek(_) => VIRTIO_BLK_S_IOERR,
ExecuteError::Write(_) => VIRTIO_BLK_S_IOERR,
ExecuteError::WriteAll(_) => VIRTIO_BLK_S_IOERR,
ExecuteError::Unsupported(_) => VIRTIO_BLK_S_UNSUPP,
ExecuteError::SubmitIoUring(_) => VIRTIO_BLK_S_IOERR,
ExecuteError::GetHostAddress(_) => VIRTIO_BLK_S_IOERR,
@ -351,13 +357,21 @@ impl Request {
match self.request_type {
RequestType::In => {
mem.read_exact_from(*data_addr, disk, *data_len as usize)
.map_err(ExecuteError::Read)?;
let mut buf = vec![0u8; *data_len as usize];
disk.read_exact(&mut buf).map_err(ExecuteError::ReadExact)?;
mem.read_exact_volatile_from(
*data_addr,
&mut buf.as_slice(),
*data_len as usize,
)
.map_err(ExecuteError::Read)?;
len += data_len;
}
RequestType::Out => {
mem.write_all_to(*data_addr, disk, *data_len as usize)
let mut buf: Vec<u8> = Vec::new();
mem.write_all_volatile_to(*data_addr, &mut buf, *data_len as usize)
.map_err(ExecuteError::Write)?;
disk.write_all(&buf).map_err(ExecuteError::WriteAll)?;
if !self.writeback {
disk.flush().map_err(ExecuteError::Flush)?;
}

View File

@ -28,7 +28,7 @@ use thiserror::Error;
use versionize::{VersionMap, Versionize, VersionizeResult};
use versionize_derive::Versionize;
use virtio_queue::{Queue, QueueT};
use vm_memory::{ByteValued, Bytes, GuestAddressSpace, GuestMemoryAtomic};
use vm_memory::{ByteValued, Bytes, GuestAddressSpace, GuestMemory, GuestMemoryAtomic};
use vm_migration::VersionMapped;
use vm_migration::{Migratable, MigratableError, Pausable, Snapshot, Snapshottable, Transportable};
use vm_virtio::{AccessPlatform, Translatable};
@ -59,6 +59,8 @@ enum Error {
GuestMemoryRead(vm_memory::guest_memory::Error),
#[error("Failed to write to guest memory: {0}")]
GuestMemoryWrite(vm_memory::guest_memory::Error),
#[error("Failed to write_all output: {0}")]
OutputWriteAll(io::Error),
#[error("Failed to flush output: {0}")]
OutputFlush(io::Error),
#[error("Failed to add used index: {0}")]
@ -264,15 +266,18 @@ impl ConsoleEpollHandler {
while let Some(mut desc_chain) = trans_queue.pop_descriptor_chain(self.mem.memory()) {
let desc = desc_chain.next().ok_or(Error::DescriptorChainTooShort)?;
if let Some(out) = &mut self.out {
let mut buf: Vec<u8> = Vec::new();
desc_chain
.memory()
.write_to(
.write_volatile_to(
desc.addr()
.translate_gva(self.access_platform.as_ref(), desc.len() as usize),
out,
&mut buf,
desc.len() as usize,
)
.map_err(Error::GuestMemoryRead)?;
out.write_all(&buf).map_err(Error::OutputWriteAll)?;
out.flush().map_err(Error::OutputFlush)?;
}
trans_queue

View File

@ -24,7 +24,7 @@ use thiserror::Error;
use versionize::{VersionMap, Versionize, VersionizeResult};
use versionize_derive::Versionize;
use virtio_queue::{Queue, QueueT};
use vm_memory::{Bytes, GuestAddressSpace, GuestMemoryAtomic};
use vm_memory::{GuestAddressSpace, GuestMemory, GuestMemoryAtomic};
use vm_migration::VersionMapped;
use vm_migration::{Migratable, MigratableError, Pausable, Snapshot, Snapshottable, Transportable};
use vm_virtio::{AccessPlatform, Translatable};
@ -75,7 +75,7 @@ impl RngEpollHandler {
// Fill the read with data from the random device on the host.
let len = desc_chain
.memory()
.read_from(
.read_volatile_from(
desc.addr()
.translate_gva(self.access_platform.as_ref(), desc.len() as usize),
&mut self.random_file,

View File

@ -51,6 +51,7 @@ use std::{result, thread};
use thiserror::Error;
use tracer::trace_scoped;
use vm_memory::bitmap::AtomicBitmap;
use vm_memory::{ReadVolatile, WriteVolatile};
use vm_migration::{protocol::*, Migratable};
use vm_migration::{MigratableError, Pausable, Snapshot, Snapshottable, Transportable};
use vmm_sys_util::eventfd::EventFd;
@ -1449,7 +1450,7 @@ impl Vmm {
memory_manager: &mut MemoryManager,
) -> std::result::Result<(), MigratableError>
where
T: Read + Write,
T: Read + ReadVolatile + Write,
{
// Read table
let table = MemoryRangeTable::read_from(socket, req.length())?;
@ -1608,7 +1609,7 @@ impl Vmm {
socket: &mut T,
) -> result::Result<bool, MigratableError>
where
T: Read + Write,
T: Read + Write + WriteVolatile,
{
// Send (dirty) memory table
let table = vm.dirty_log()?;

View File

@ -30,8 +30,10 @@ use std::collections::HashMap;
use std::convert::TryInto;
use std::ffi;
use std::fs::{File, OpenOptions};
use std::io::{self, Read};
use std::io::{self};
use std::ops::{BitAnd, Deref, Not, Sub};
#[cfg(all(target_arch = "x86_64", feature = "guest_debug"))]
use std::os::fd::AsFd;
use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
use std::path::PathBuf;
use std::result;
@ -47,8 +49,9 @@ use vm_device::BusDevice;
use vm_memory::bitmap::AtomicBitmap;
use vm_memory::guest_memory::FileOffset;
use vm_memory::{
mmap::MmapRegionError, Address, Bytes, Error as MmapError, GuestAddress, GuestAddressSpace,
mmap::MmapRegionError, Address, Error as MmapError, GuestAddress, GuestAddressSpace,
GuestMemory, GuestMemoryAtomic, GuestMemoryError, GuestMemoryRegion, GuestUsize, MmapRegion,
ReadVolatile,
};
use vm_migration::{
protocol::MemoryRange, protocol::MemoryRangeTable, Migratable, MigratableError, Pausable,
@ -728,7 +731,7 @@ impl MemoryManager {
// see: https://github.com/rust-vmm/vm-memory/issues/174
loop {
let bytes_read = guest_memory
.read_from(
.read_volatile_from(
GuestAddress(range.gpa + offset),
&mut memory_file,
(range.length - offset) as usize,
@ -2084,7 +2087,7 @@ impl MemoryManager {
return Ok(());
}
let mut coredump_file = dump_state.file.as_ref().unwrap();
let coredump_file = dump_state.file.as_ref().unwrap();
let guest_memory = self.guest_memory.memory();
let mut total_bytes: u64 = 0;
@ -2093,9 +2096,9 @@ impl MemoryManager {
let mut offset: u64 = 0;
loop {
let bytes_written = guest_memory
.write_to(
.write_volatile_to(
GuestAddress(range.gpa + offset),
&mut coredump_file,
&mut coredump_file.as_fd(),
(range.length - offset) as usize,
)
.map_err(|e| GuestDebuggableError::Coredump(e.into()))?;
@ -2118,7 +2121,7 @@ impl MemoryManager {
fd: &mut F,
) -> std::result::Result<(), MigratableError>
where
F: Read,
F: ReadVolatile,
{
let guest_memory = self.guest_memory();
let mem = guest_memory.memory();
@ -2132,7 +2135,7 @@ impl MemoryManager {
// see: https://github.com/rust-vmm/vm-memory/issues/174
loop {
let bytes_read = mem
.read_from(
.read_volatile_from(
GuestAddress(range.gpa + offset),
fd,
(range.length - offset) as usize,
@ -2600,7 +2603,7 @@ impl Transportable for MemoryManager {
// see: https://github.com/rust-vmm/vm-memory/issues/174
loop {
let bytes_written = guest_memory
.write_to(
.write_volatile_to(
GuestAddress(range.gpa + offset),
&mut memory_file,
(range.length - offset) as usize,

View File

@ -72,8 +72,6 @@ use std::collections::HashMap;
use std::convert::TryInto;
use std::fs::{File, OpenOptions};
use std::io::{self, Seek, SeekFrom, Write};
#[cfg(feature = "tdx")]
use std::mem;
#[cfg(all(target_arch = "x86_64", feature = "guest_debug"))]
use std::mem::size_of;
use std::num::Wrapping;
@ -86,8 +84,10 @@ use thiserror::Error;
use tracer::trace_scoped;
use vm_device::Bus;
#[cfg(feature = "tdx")]
use vm_memory::{Address, ByteValued, GuestMemory, GuestMemoryRegion};
use vm_memory::{Bytes, GuestAddress, GuestAddressSpace, GuestMemoryAtomic};
use vm_memory::{Address, ByteValued, GuestMemoryRegion, ReadVolatile};
use vm_memory::{
Bytes, GuestAddress, GuestAddressSpace, GuestMemory, GuestMemoryAtomic, WriteVolatile,
};
use vm_migration::protocol::{Request, Response, Status};
use vm_migration::{
protocol::MemoryRangeTable, snapshot_from_id, Migratable, MigratableError, Pausable, Snapshot,
@ -884,7 +884,7 @@ impl Vm {
}
fn load_initramfs(&mut self, guest_mem: &GuestMemoryMmap) -> Result<arch::InitramfsConfig> {
let mut initramfs = self.initramfs.as_ref().unwrap();
let initramfs = self.initramfs.as_mut().unwrap();
let size: usize = initramfs
.seek(SeekFrom::End(0))
.map_err(|_| Error::InitramfsLoad)?
@ -897,7 +897,7 @@ impl Vm {
let address = GuestAddress(address);
guest_mem
.read_from(address, &mut initramfs, size)
.read_volatile_from(address, initramfs, size)
.map_err(|_| Error::InitramfsLoad)?;
info!("Initramfs loaded: address = 0x{:x}", address.0);
@ -1744,7 +1744,7 @@ impl Vm {
firmware_file
.seek(SeekFrom::Start(section.data_offset as u64))
.map_err(Error::LoadTdvf)?;
mem.read_from(
mem.read_volatile_from(
GuestAddress(section.address),
&mut firmware_file,
section.data_size as usize,
@ -1766,13 +1766,8 @@ impl Vm {
.map_err(Error::LoadPayload)?;
let mut payload_header = linux_loader::bootparam::setup_header::default();
payload_header
.as_bytes()
.read_from(
0,
payload_file,
mem::size_of::<linux_loader::bootparam::setup_header>(),
)
payload_file
.read_volatile(&mut payload_header.as_bytes())
.unwrap();
if payload_header.header != 0x5372_6448 {
@ -1786,7 +1781,7 @@ impl Vm {
}
payload_file.rewind().map_err(Error::LoadPayload)?;
mem.read_from(
mem.read_volatile_from(
GuestAddress(section.address),
payload_file,
payload_size as usize,
@ -2129,7 +2124,7 @@ impl Vm {
fd: &mut F,
) -> std::result::Result<(), MigratableError>
where
F: Write,
F: WriteVolatile,
{
let guest_memory = self.memory_manager.lock().as_ref().unwrap().guest_memory();
let mem = guest_memory.memory();
@ -2143,7 +2138,7 @@ impl Vm {
// see: https://github.com/rust-vmm/vm-memory/issues/174
loop {
let bytes_written = mem
.write_to(
.write_volatile_to(
GuestAddress(range.gpa + offset),
fd,
(range.length - offset) as usize,