Skip to content

Commit 36ecc4f

Browse files
jackabaldShadowCurse
authored andcommitted
write fdt to memory in configure_system
From: jackabald <[email protected]> - removes writing to memory in create_fdt - add functionality of writing to memory in configure_system - uses previously unused return value of create_fdt to write fdt to memory Signed-off-by: jackabald <[email protected]> Signed-off-by: Patrick Roy <[email protected]>
1 parent 6d2bf72 commit 36ecc4f

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

src/vmm/src/arch/aarch64/fdt.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ use vm_memory::GuestMemoryError;
1414

1515
use super::super::{DeviceType, InitrdConfig};
1616
use super::cache_info::{read_cache_config, CacheEntry};
17-
use super::get_fdt_addr;
1817
use super::gic::GICDevice;
1918
use crate::devices::acpi::vmgenid::{VmGenId, VMGENID_MEM_SIZE};
20-
use crate::vstate::memory::{Address, Bytes, GuestAddress, GuestMemory, GuestMemoryMmap};
19+
use crate::vstate::memory::{Address, GuestMemory, GuestMemoryMmap};
2120

2221
// This is a value for uniquely identifying the FDT node declaring the interrupt controller.
2322
const GIC_PHANDLE: u32 = 1;
@@ -65,11 +64,11 @@ pub enum FdtError {
6564
}
6665

6766
/// Creates the flattened device tree for this aarch64 microVM.
68-
pub fn create_fdt<T: DeviceInfoForFDT + Clone + Debug, S: std::hash::BuildHasher>(
67+
pub fn create_fdt<T: DeviceInfoForFDT + Clone + Debug>(
6968
guest_mem: &GuestMemoryMmap,
7069
vcpu_mpidr: Vec<u64>,
7170
cmdline: CString,
72-
device_info: &HashMap<(DeviceType, String), T, S>,
71+
device_info: &HashMap<(DeviceType, String), T>,
7372
gic_device: &GICDevice,
7473
vmgenid: &Option<VmGenId>,
7574
initrd: &Option<InitrdConfig>,
@@ -106,10 +105,6 @@ pub fn create_fdt<T: DeviceInfoForFDT + Clone + Debug, S: std::hash::BuildHasher
106105

107106
// Allocate another buffer so we can format and then write fdt to guest.
108107
let fdt_final = fdt_writer.finish()?;
109-
110-
// Write FDT to memory.
111-
let fdt_address = GuestAddress(get_fdt_addr(guest_mem));
112-
guest_mem.write_slice(fdt_final.as_slice(), fdt_address)?;
113108
Ok(fdt_final)
114109
}
115110

@@ -461,6 +456,7 @@ mod tests {
461456
use crate::arch::aarch64::layout;
462457
use crate::device_manager::resources::ResourceAllocator;
463458
use crate::test_utils::arch_mem;
459+
use crate::vstate::memory::GuestAddress;
464460

465461
const LEN: u64 = 4096;
466462

src/vmm/src/arch/aarch64/mod.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ use std::collections::HashMap;
1717
use std::ffi::CString;
1818
use std::fmt::Debug;
1919

20+
use vm_memory::GuestMemoryError;
21+
2022
pub use self::fdt::DeviceInfoForFDT;
2123
use self::gic::GICDevice;
2224
use crate::arch::DeviceType;
2325
use crate::devices::acpi::vmgenid::VmGenId;
24-
use crate::vstate::memory::{Address, GuestAddress, GuestMemory, GuestMemoryMmap};
26+
use crate::vstate::memory::{Address, Bytes, GuestAddress, GuestMemory, GuestMemoryMmap};
2527

2628
/// Errors thrown while configuring aarch64 system.
2729
#[derive(Debug, thiserror::Error, displaydoc::Display)]
@@ -30,6 +32,8 @@ pub enum ConfigurationError {
3032
SetupFDT(#[from] fdt::FdtError),
3133
/// Failed to compute the initrd address.
3234
InitrdAddress,
35+
/// Failed to write to guest memory.
36+
MemoryError(GuestMemoryError),
3337
}
3438

3539
/// The start of the memory area reserved for MMIO devices.
@@ -55,16 +59,16 @@ pub fn arch_memory_regions(size: usize) -> Vec<(GuestAddress, usize)> {
5559
/// * `device_info` - A hashmap containing the attached devices for building FDT device nodes.
5660
/// * `gic_device` - The GIC device.
5761
/// * `initrd` - Information about an optional initrd.
58-
pub fn configure_system<T: DeviceInfoForFDT + Clone + Debug, S: std::hash::BuildHasher>(
62+
pub fn configure_system<T: DeviceInfoForFDT + Clone + Debug>(
5963
guest_mem: &GuestMemoryMmap,
6064
cmdline_cstring: CString,
6165
vcpu_mpidr: Vec<u64>,
62-
device_info: &HashMap<(DeviceType, String), T, S>,
66+
device_info: &HashMap<(DeviceType, String), T>,
6367
gic_device: &GICDevice,
6468
vmgenid: &Option<VmGenId>,
6569
initrd: &Option<super::InitrdConfig>,
6670
) -> Result<(), ConfigurationError> {
67-
fdt::create_fdt(
71+
let fdt = fdt::create_fdt(
6872
guest_mem,
6973
vcpu_mpidr,
7074
cmdline_cstring,
@@ -73,6 +77,10 @@ pub fn configure_system<T: DeviceInfoForFDT + Clone + Debug, S: std::hash::Build
7377
vmgenid,
7478
initrd,
7579
)?;
80+
let fdt_address = GuestAddress(get_fdt_addr(guest_mem));
81+
guest_mem
82+
.write_slice(fdt.as_slice(), fdt_address)
83+
.map_err(ConfigurationError::MemoryError)?;
7684
Ok(())
7785
}
7886

0 commit comments

Comments
 (0)