Skip to content

Commit

Permalink
write fdt to memory in configure_system
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
jackabald authored and ShadowCurse committed Oct 9, 2024
1 parent 6d2bf72 commit 36ecc4f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
12 changes: 4 additions & 8 deletions src/vmm/src/arch/aarch64/fdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ use vm_memory::GuestMemoryError;

use super::super::{DeviceType, InitrdConfig};
use super::cache_info::{read_cache_config, CacheEntry};
use super::get_fdt_addr;
use super::gic::GICDevice;
use crate::devices::acpi::vmgenid::{VmGenId, VMGENID_MEM_SIZE};
use crate::vstate::memory::{Address, Bytes, GuestAddress, GuestMemory, GuestMemoryMmap};
use crate::vstate::memory::{Address, GuestMemory, GuestMemoryMmap};

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

/// Creates the flattened device tree for this aarch64 microVM.
pub fn create_fdt<T: DeviceInfoForFDT + Clone + Debug, S: std::hash::BuildHasher>(
pub fn create_fdt<T: DeviceInfoForFDT + Clone + Debug>(
guest_mem: &GuestMemoryMmap,
vcpu_mpidr: Vec<u64>,
cmdline: CString,
device_info: &HashMap<(DeviceType, String), T, S>,
device_info: &HashMap<(DeviceType, String), T>,
gic_device: &GICDevice,
vmgenid: &Option<VmGenId>,
initrd: &Option<InitrdConfig>,
Expand Down Expand Up @@ -106,10 +105,6 @@ pub fn create_fdt<T: DeviceInfoForFDT + Clone + Debug, S: std::hash::BuildHasher

// Allocate another buffer so we can format and then write fdt to guest.
let fdt_final = fdt_writer.finish()?;

// Write FDT to memory.
let fdt_address = GuestAddress(get_fdt_addr(guest_mem));
guest_mem.write_slice(fdt_final.as_slice(), fdt_address)?;
Ok(fdt_final)
}

Expand Down Expand Up @@ -461,6 +456,7 @@ mod tests {
use crate::arch::aarch64::layout;
use crate::device_manager::resources::ResourceAllocator;
use crate::test_utils::arch_mem;
use crate::vstate::memory::GuestAddress;

const LEN: u64 = 4096;

Expand Down
16 changes: 12 additions & 4 deletions src/vmm/src/arch/aarch64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ use std::collections::HashMap;
use std::ffi::CString;
use std::fmt::Debug;

use vm_memory::GuestMemoryError;

pub use self::fdt::DeviceInfoForFDT;
use self::gic::GICDevice;
use crate::arch::DeviceType;
use crate::devices::acpi::vmgenid::VmGenId;
use crate::vstate::memory::{Address, GuestAddress, GuestMemory, GuestMemoryMmap};
use crate::vstate::memory::{Address, Bytes, GuestAddress, GuestMemory, GuestMemoryMmap};

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

/// The start of the memory area reserved for MMIO devices.
Expand All @@ -55,16 +59,16 @@ pub fn arch_memory_regions(size: usize) -> Vec<(GuestAddress, usize)> {
/// * `device_info` - A hashmap containing the attached devices for building FDT device nodes.
/// * `gic_device` - The GIC device.
/// * `initrd` - Information about an optional initrd.
pub fn configure_system<T: DeviceInfoForFDT + Clone + Debug, S: std::hash::BuildHasher>(
pub fn configure_system<T: DeviceInfoForFDT + Clone + Debug>(
guest_mem: &GuestMemoryMmap,
cmdline_cstring: CString,
vcpu_mpidr: Vec<u64>,
device_info: &HashMap<(DeviceType, String), T, S>,
device_info: &HashMap<(DeviceType, String), T>,
gic_device: &GICDevice,
vmgenid: &Option<VmGenId>,
initrd: &Option<super::InitrdConfig>,
) -> Result<(), ConfigurationError> {
fdt::create_fdt(
let fdt = fdt::create_fdt(
guest_mem,
vcpu_mpidr,
cmdline_cstring,
Expand All @@ -73,6 +77,10 @@ pub fn configure_system<T: DeviceInfoForFDT + Clone + Debug, S: std::hash::Build
vmgenid,
initrd,
)?;
let fdt_address = GuestAddress(get_fdt_addr(guest_mem));
guest_mem
.write_slice(fdt.as_slice(), fdt_address)
.map_err(ConfigurationError::MemoryError)?;
Ok(())
}

Expand Down

0 comments on commit 36ecc4f

Please sign in to comment.