Skip to content

Commit 2484df5

Browse files
author
szy
committed
update
1 parent 1a8df5d commit 2484df5

File tree

2 files changed

+80
-86
lines changed

2 files changed

+80
-86
lines changed

src/vmm/config.rs

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@ use crate::vmm::{VM, images::ImageLoader, vm_list::push_vm};
1111
#[cfg(target_arch = "aarch64")]
1212
use crate::vmm::fdt::*;
1313

14-
#[cfg(target_arch = "aarch64")]
15-
use fdt_parser::Fdt;
16-
17-
#[cfg(target_arch = "aarch64")]
18-
use alloc::vec::Vec;
19-
2014
use alloc::sync::Arc;
2115

2216
#[allow(clippy::module_inception)]
@@ -71,51 +65,6 @@ pub mod config {
7165
include!(concat!(env!("OUT_DIR"), "/vm_configs.rs"));
7266
}
7367

74-
#[cfg(target_arch = "aarch64")]
75-
pub fn get_developer_provided_dtb(
76-
vm_cfg: &AxVMConfig,
77-
crate_config: &AxVMCrateConfig,
78-
) -> Option<Vec<u8>> {
79-
match crate_config.kernel.image_location.as_deref() {
80-
Some("memory") => {
81-
let vm_imags = config::get_memory_images()
82-
.iter()
83-
.find(|&v| v.id == vm_cfg.id())?;
84-
85-
if let Some(dtb) = vm_imags.dtb {
86-
info!("DTB file in memory, size: 0x{:x}", dtb.len());
87-
return Some(dtb.to_vec());
88-
}
89-
}
90-
#[cfg(feature = "fs")]
91-
Some("fs") => {
92-
use axerrno::ax_err_type;
93-
use std::io::{BufReader, Read};
94-
if let Some(dtb_path) = &crate_config.kernel.dtb_path {
95-
let (dtb_file, dtb_size) = crate::vmm::images::open_image_file(dtb_path).unwrap();
96-
info!("DTB file in fs, size: 0x{:x}", dtb_size);
97-
98-
let mut file = BufReader::new(dtb_file);
99-
let mut dtb_buffer = vec![0; dtb_size];
100-
101-
file.read_exact(&mut dtb_buffer)
102-
.map_err(|err| {
103-
ax_err_type!(
104-
Io,
105-
format!("Failed in reading from file {}, err {:?}", dtb_path, err)
106-
)
107-
})
108-
.unwrap();
109-
return Some(dtb_buffer);
110-
}
111-
}
112-
_ => unimplemented!(
113-
"Check your \"image_location\" in config.toml, \"memory\" and \"fs\" are supported,\n."
114-
),
115-
}
116-
None
117-
}
118-
11968
pub fn get_vm_dtb_arc(_vm_cfg: &AxVMConfig) -> Option<Arc<[u8]>> {
12069
#[cfg(target_arch = "aarch64")]
12170
{
@@ -127,39 +76,6 @@ pub fn get_vm_dtb_arc(_vm_cfg: &AxVMConfig) -> Option<Arc<[u8]>> {
12776
None
12877
}
12978

130-
/// Handle all FDT-related operations for aarch64 architecture
131-
#[cfg(target_arch = "aarch64")]
132-
fn handle_fdt_operations(vm_config: &mut AxVMConfig, vm_create_config: &AxVMCrateConfig) {
133-
let host_fdt_bytes = get_host_fdt();
134-
let host_fdt = Fdt::from_bytes(host_fdt_bytes)
135-
.map_err(|e| format!("Failed to parse FDT: {e:#?}"))
136-
.expect("Failed to parse FDT");
137-
set_phys_cpu_sets(vm_config, &host_fdt, vm_create_config);
138-
139-
if let Some(provided_dtb) = get_developer_provided_dtb(vm_config, vm_create_config) {
140-
info!("VM[{}] found DTB , parsing...", vm_config.id());
141-
update_provided_fdt(&provided_dtb, host_fdt_bytes, vm_create_config);
142-
} else {
143-
info!(
144-
"VM[{}] DTB not found, generating based on the configuration file.",
145-
vm_config.id()
146-
);
147-
setup_guest_fdt_from_vmm(host_fdt_bytes, vm_config, vm_create_config);
148-
}
149-
150-
// Overlay VM config with the given DTB.
151-
if let Some(dtb_arc) = get_vm_dtb_arc(vm_config) {
152-
let dtb = dtb_arc.as_ref();
153-
parse_passthrough_devices_address(vm_config, dtb);
154-
parse_vm_interrupt(vm_config, dtb);
155-
} else {
156-
error!(
157-
"VM[{}] DTB not found in memory, skipping...",
158-
vm_config.id()
159-
);
160-
}
161-
}
162-
16379
pub fn init_guest_vms() {
16480
// Initialize the DTB cache in the fdt module
16581
#[cfg(target_arch = "aarch64")]

src/vmm/fdt/mod.rs

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@ mod print;
1010

1111
use alloc::collections::BTreeMap;
1212
use alloc::vec::Vec;
13-
use axvm::config::AxVMCrateConfig;
13+
use axvm::config::{AxVMConfig, AxVMCrateConfig};
14+
use fdt_parser::Fdt;
1415
use lazyinit::LazyInit;
1516
use spin::Mutex;
1617

17-
// Re-export public functions
1818
pub use parser::*;
1919
// pub use print::print_fdt;
2020
pub use create::*;
2121
pub use device::build_node_path;
2222

23+
use crate::vmm::config::{config, get_vm_dtb_arc};
24+
2325
// DTB cache for generated device trees
2426
static GENERATED_DTB_CACHE: LazyInit<Mutex<BTreeMap<usize, Vec<u8>>>> = LazyInit::new();
2527

@@ -41,3 +43,79 @@ pub fn crate_guest_fdt_with_cache(dtb_data: Vec<u8>, crate_config: &AxVMCrateCon
4143
let mut cache_lock = dtb_cache().lock();
4244
cache_lock.insert(crate_config.base.id, dtb_data);
4345
}
46+
47+
/// Handle all FDT-related operations for aarch64 architecture
48+
pub fn handle_fdt_operations(vm_config: &mut AxVMConfig, vm_create_config: &AxVMCrateConfig) {
49+
let host_fdt_bytes = get_host_fdt();
50+
let host_fdt = Fdt::from_bytes(host_fdt_bytes)
51+
.map_err(|e| format!("Failed to parse FDT: {e:#?}"))
52+
.expect("Failed to parse FDT");
53+
set_phys_cpu_sets(vm_config, &host_fdt, vm_create_config);
54+
55+
if let Some(provided_dtb) = get_developer_provided_dtb(vm_config, vm_create_config) {
56+
info!("VM[{}] found DTB , parsing...", vm_config.id());
57+
update_provided_fdt(&provided_dtb, host_fdt_bytes, vm_create_config);
58+
} else {
59+
info!(
60+
"VM[{}] DTB not found, generating based on the configuration file.",
61+
vm_config.id()
62+
);
63+
setup_guest_fdt_from_vmm(host_fdt_bytes, vm_config, vm_create_config);
64+
}
65+
66+
// Overlay VM config with the given DTB.
67+
if let Some(dtb_arc) = get_vm_dtb_arc(vm_config) {
68+
let dtb = dtb_arc.as_ref();
69+
parse_passthrough_devices_address(vm_config, dtb);
70+
parse_vm_interrupt(vm_config, dtb);
71+
} else {
72+
error!(
73+
"VM[{}] DTB not found in memory, skipping...",
74+
vm_config.id()
75+
);
76+
}
77+
}
78+
79+
pub fn get_developer_provided_dtb(
80+
vm_cfg: &AxVMConfig,
81+
crate_config: &AxVMCrateConfig,
82+
) -> Option<Vec<u8>> {
83+
match crate_config.kernel.image_location.as_deref() {
84+
Some("memory") => {
85+
let vm_imags = config::get_memory_images()
86+
.iter()
87+
.find(|&v| v.id == vm_cfg.id())?;
88+
89+
if let Some(dtb) = vm_imags.dtb {
90+
info!("DTB file in memory, size: 0x{:x}", dtb.len());
91+
return Some(dtb.to_vec());
92+
}
93+
}
94+
#[cfg(feature = "fs")]
95+
Some("fs") => {
96+
use axerrno::ax_err_type;
97+
use std::io::{BufReader, Read};
98+
if let Some(dtb_path) = &crate_config.kernel.dtb_path {
99+
let (dtb_file, dtb_size) = crate::vmm::images::open_image_file(dtb_path).unwrap();
100+
info!("DTB file in fs, size: 0x{:x}", dtb_size);
101+
102+
let mut file = BufReader::new(dtb_file);
103+
let mut dtb_buffer = vec![0; dtb_size];
104+
105+
file.read_exact(&mut dtb_buffer)
106+
.map_err(|err| {
107+
ax_err_type!(
108+
Io,
109+
format!("Failed in reading from file {}, err {:?}", dtb_path, err)
110+
)
111+
})
112+
.unwrap();
113+
return Some(dtb_buffer);
114+
}
115+
}
116+
_ => unimplemented!(
117+
"Check your \"image_location\" in config.toml, \"memory\" and \"fs\" are supported,\n."
118+
),
119+
}
120+
None
121+
}

0 commit comments

Comments
 (0)