Skip to content

Commit b0d9bf7

Browse files
author
szy
committed
caculate dtb_addr
1 parent b964dcb commit b0d9bf7

File tree

3 files changed

+59
-60
lines changed

3 files changed

+59
-60
lines changed

src/vmm/config.rs

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
use axaddrspace::GuestPhysAddr;
22
use axerrno::AxResult;
3-
use axvm::{
4-
VMMemoryRegion,
5-
config::{AxVMConfig, AxVMCrateConfig, VmMemMappingType},
6-
};
3+
use axvm::config::{AxVMConfig, AxVMCrateConfig, VmMemMappingType};
74
use core::alloc::Layout;
8-
use memory_addr::MemoryAddr;
95

106
use crate::vmm::{VM, images::ImageLoader, vm_list::push_vm};
117

@@ -129,8 +125,6 @@ pub fn init_guest_vm(raw_cfg: &str) -> AxResult {
129125
.cloned()
130126
.expect("VM must have at least one memory region");
131127

132-
config_guest_address(&vm, &main_mem);
133-
134128
// Load corresponding images for VM.
135129
info!("VM[{}] created success, loading images...", vm.id());
136130

@@ -144,29 +138,6 @@ pub fn init_guest_vm(raw_cfg: &str) -> AxResult {
144138
Ok(())
145139
}
146140

147-
fn config_guest_address(vm: &VM, main_memory: &VMMemoryRegion) {
148-
const MB: usize = 1024 * 1024;
149-
vm.with_config(|config| {
150-
if main_memory.is_identical() {
151-
debug!(
152-
"Adjusting kernel load address from {:#x} to {:#x}",
153-
config.image_config.kernel_load_gpa, main_memory.gpa
154-
);
155-
let mut kernel_addr = main_memory.gpa;
156-
if config.image_config.bios_load_gpa.is_some() {
157-
kernel_addr += MB * 2; // leave 2MB for BIOS
158-
}
159-
let dtb_addr = (main_memory.gpa + (main_memory.size().min(512 * MB) / 2).max(64 * MB))
160-
.align_up(2 * MB);
161-
162-
config.image_config.kernel_load_gpa = kernel_addr;
163-
config.cpu_config.bsp_entry = kernel_addr;
164-
config.cpu_config.ap_entry = kernel_addr;
165-
config.image_config.dtb_load_gpa = Some(dtb_addr);
166-
}
167-
});
168-
}
169-
170141
fn vm_alloc_memorys(vm_create_config: &AxVMCrateConfig, vm: &VM) {
171142
const MB: usize = 1024 * 1024;
172143
const ALIGN: usize = 2 * MB;

src/vmm/fdt/create.rs

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use core::ptr::NonNull;
77
use axaddrspace::GuestPhysAddr;
88
use axvm::{VMMemoryRegion, config::AxVMCrateConfig};
99
use fdt_parser::{Fdt, Node};
10+
use memory_addr::MemoryAddr;
1011
use vm_fdt::{FdtWriter, FdtWriterNode};
1112

1213
use crate::vmm::{VMRef, images::load_vm_image_from_memory};
@@ -259,7 +260,7 @@ fn add_memory_node(new_memory: &[VMMemoryRegion], new_fdt: &mut FdtWriter) {
259260
new_fdt.property_string("device_type", "memory").unwrap();
260261
}
261262

262-
pub fn update_fdt(dest_addr: GuestPhysAddr, fdt_src: NonNull<u8>, dtb_size: usize, vm: VMRef) {
263+
pub fn update_fdt(fdt_src: NonNull<u8>, dtb_size: usize, vm: VMRef) {
263264
let mut new_fdt = FdtWriter::new().unwrap();
264265
let mut previous_node_level = 0;
265266
let mut node_stack: Vec<FdtWriterNode> = Vec::new();
@@ -315,12 +316,54 @@ pub fn update_fdt(dest_addr: GuestPhysAddr, fdt_src: NonNull<u8>, dtb_size: usiz
315316
let new_fdt_bytes = new_fdt.finish().unwrap();
316317

317318
// crate::vmm::fdt::print::print_guest_fdt(new_fdt_bytes.as_slice());
318-
319+
let vm_clone = vm.clone();
320+
let dest_addr = calculate_dtb_load_addr(vm, new_fdt_bytes.len());
321+
info!("New FDT will be loaded at {:x}", dest_addr);
319322
// Load the updated FDT into VM
320-
load_vm_image_from_memory(&new_fdt_bytes, dest_addr, vm.clone())
323+
load_vm_image_from_memory(&new_fdt_bytes, dest_addr, vm_clone)
321324
.expect("Failed to load VM images");
322325
}
323326

327+
fn calculate_dtb_load_addr(vm: VMRef, fdt_size: usize) -> GuestPhysAddr {
328+
const MB: usize = 1024 * 1024;
329+
330+
// Get main memory from VM memory regions outside the closure
331+
let main_memory = vm
332+
.memory_regions()
333+
.first()
334+
.cloned()
335+
.expect("VM must have at least one memory region");
336+
337+
vm.with_config(|config| {
338+
let dtb_addr = if let Some(addr) = config.image_config.dtb_load_gpa {
339+
// If dtb_load_gpa is already set, use the original value
340+
addr
341+
} else {
342+
// If dtb_load_gpa is None, calculate based on memory size and FDT size
343+
if main_memory.size() > 512 * MB {
344+
// When memory size is greater than 512MB, place in the last area of the first 512MB
345+
let available_space = 2 * MB;
346+
if fdt_size <= available_space {
347+
(main_memory.gpa + 512 * MB - available_space).align_down(2 * MB)
348+
} else {
349+
// If FDT is larger than available space, place it at the end of main memory
350+
(main_memory.gpa + main_memory.size() - fdt_size).align_down(2 * MB)
351+
}
352+
} else {
353+
// When memory size is less than or equal to 512MB, place at the end of main_memory
354+
if fdt_size <= main_memory.size() {
355+
(main_memory.gpa + main_memory.size() - fdt_size).align_down(2 * MB)
356+
} else {
357+
// This shouldn't happen, but just in case
358+
main_memory.gpa.align_down(2 * MB)
359+
}
360+
}
361+
};
362+
config.image_config.dtb_load_gpa = Some(dtb_addr);
363+
dtb_addr
364+
})
365+
}
366+
324367
pub fn update_cpu_node(fdt: &Fdt, host_fdt: &Fdt, crate_config: &AxVMCrateConfig) -> Vec<u8> {
325368
let mut new_fdt = FdtWriter::new().unwrap();
326369
let mut previous_node_level = 0;

src/vmm/images/mod.rs

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ use crate::vmm::VMRef;
1010
use crate::vmm::config::{config, get_vm_dtb_arc};
1111

1212
#[cfg(target_arch = "aarch64")]
13-
use crate::vmm::fdt::update_fdt;
13+
use core::ptr::NonNull;
1414

1515
#[cfg(target_arch = "aarch64")]
16-
use core::ptr::NonNull;
16+
use crate::vmm::fdt::update_fdt;
1717

1818
mod linux;
1919

@@ -107,23 +107,17 @@ impl ImageLoader {
107107
.expect("Failed to load VM images");
108108
// Load DTB image
109109
let vm_config = axvm::config::AxVMConfig::from(self.config.clone());
110-
if let Some(dtb_arc) = get_vm_dtb_arc(&vm_config)
111-
&& let Some(dtb_load_gpa) = self.dtb_load_gpa
112-
{
113-
let dtb_slice: &[u8] = &dtb_arc;
114-
debug!(
115-
"DTB buffer addr: {:x}, size: {:#}",
116-
dtb_load_gpa,
117-
Byte::from(dtb_slice.len())
118-
);
119110

111+
if let Some(dtb_arc) = get_vm_dtb_arc(&vm_config) {
112+
let _dtb_slice: &[u8] = &dtb_arc;
120113
#[cfg(target_arch = "aarch64")]
121114
update_fdt(
122-
dtb_load_gpa,
123-
NonNull::new(dtb_slice.as_ptr() as *mut u8).unwrap(),
124-
dtb_slice.len(),
115+
NonNull::new(_dtb_slice.as_ptr() as *mut u8).unwrap(),
116+
_dtb_slice.len(),
125117
self.vm.clone(),
126118
);
119+
} else {
120+
info!("dtb_load_gpa not provided");
127121
}
128122

129123
// Load BIOS image
@@ -256,21 +250,12 @@ pub mod fs {
256250
};
257251
// Load DTB image if needed.
258252
let vm_config = axvm::config::AxVMConfig::from(loader.config.clone());
259-
if let Some(dtb_arc) = get_vm_dtb_arc(&vm_config)
260-
&& let Some(dtb_load_gpa) = loader.dtb_load_gpa
261-
{
262-
let dtb_slice: &[u8] = &dtb_arc;
263-
debug!(
264-
"DTB buffer addr: {:x}, size: {:#}",
265-
dtb_load_gpa,
266-
Byte::from(dtb_slice.len())
267-
);
268-
253+
if let Some(dtb_arc) = get_vm_dtb_arc(&vm_config) {
254+
let _dtb_slice: &[u8] = &dtb_arc;
269255
#[cfg(target_arch = "aarch64")]
270256
update_fdt(
271-
dtb_load_gpa,
272-
NonNull::new(dtb_slice.as_ptr() as *mut u8).unwrap(),
273-
dtb_slice.len(),
257+
NonNull::new(_dtb_slice.as_ptr() as *mut u8).unwrap(),
258+
_dtb_slice.len(),
274259
loader.vm.clone(),
275260
);
276261
}

0 commit comments

Comments
 (0)