Skip to content

Commit 70c93e8

Browse files
author
szy
committed
optimize
1 parent e2147e4 commit 70c93e8

File tree

3 files changed

+9
-27
lines changed

3 files changed

+9
-27
lines changed

src/vmm/fdt/create.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -344,24 +344,12 @@ fn calculate_dtb_load_addr(vm: VMRef, fdt_size: usize) -> GuestPhysAddr {
344344
addr
345345
} else {
346346
// If dtb_load_gpa is None, calculate based on memory size and FDT size
347-
if main_memory.size() > 512 * MB {
348-
// When memory size is greater than 512MB, place in the last area of the first 512MB
349-
let available_space = 2 * MB;
350-
if fdt_size <= available_space {
351-
(main_memory.gpa + 512 * MB - available_space).align_down(2 * MB)
352-
} else {
353-
// If FDT is larger than available space, place it at the end of main memory
354-
(main_memory.gpa + main_memory.size() - fdt_size).align_down(2 * MB)
355-
}
356-
} else {
357-
// When memory size is less than or equal to 512MB, place at the end of main_memory
358-
if fdt_size <= main_memory.size() {
359-
(main_memory.gpa + main_memory.size() - fdt_size).align_down(2 * MB)
360-
} else {
361-
// This shouldn't happen, but just in case
362-
main_memory.gpa.align_down(2 * MB)
363-
}
347+
let main_memory_size = main_memory.size().min(512 * MB);
348+
let addr = (main_memory.gpa + main_memory_size - fdt_size).align_down(2 * MB);
349+
if fdt_size > main_memory_size {
350+
error!("DTB size is larger than available memory");
364351
}
352+
addr
365353
};
366354
config.image_config.dtb_load_gpa = Some(dtb_addr);
367355
dtb_addr

src/vmm/images/mod.rs

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

12-
#[cfg(target_arch = "aarch64")]
13-
use core::ptr::NonNull;
14-
15-
#[cfg(target_arch = "aarch64")]
16-
use crate::vmm::fdt::update_fdt;
17-
1812
mod linux;
1913

2014
pub fn get_image_header(config: &AxVMCrateConfig) -> Option<linux::Header> {
@@ -111,8 +105,8 @@ impl ImageLoader {
111105
if let Some(dtb_arc) = get_vm_dtb_arc(&vm_config) {
112106
let _dtb_slice: &[u8] = &dtb_arc;
113107
#[cfg(target_arch = "aarch64")]
114-
update_fdt(
115-
NonNull::new(_dtb_slice.as_ptr() as *mut u8).unwrap(),
108+
crate::vmm::fdt::update_fdt(
109+
core::ptr::NonNull::new(_dtb_slice.as_ptr() as *mut u8).unwrap(),
116110
_dtb_slice.len(),
117111
self.vm.clone(),
118112
);
@@ -253,8 +247,8 @@ pub mod fs {
253247
if let Some(dtb_arc) = get_vm_dtb_arc(&vm_config) {
254248
let _dtb_slice: &[u8] = &dtb_arc;
255249
#[cfg(target_arch = "aarch64")]
256-
update_fdt(
257-
NonNull::new(_dtb_slice.as_ptr() as *mut u8).unwrap(),
250+
crate::vmm::fdt::update_fdt(
251+
core::ptr::NonNull::new(_dtb_slice.as_ptr() as *mut u8).unwrap(),
258252
_dtb_slice.len(),
259253
loader.vm.clone(),
260254
);

0 commit comments

Comments
 (0)