Skip to content

Commit 049dd65

Browse files
author
szy
committed
move some code to fdt
1 parent 62d55f4 commit 049dd65

File tree

4 files changed

+45
-25
lines changed

4 files changed

+45
-25
lines changed

src/vmm/config.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,7 @@ use fdt_parser::Fdt;
1717
#[cfg(target_arch = "aarch64")]
1818
use alloc::vec::Vec;
1919

20-
use alloc::collections::BTreeMap;
2120
use alloc::sync::Arc;
22-
use lazyinit::LazyInit;
23-
use spin::Mutex;
24-
25-
pub static GENERATED_DTB_CACHE: LazyInit<Mutex<BTreeMap<usize, Arc<[u8]>>>> = LazyInit::new();
2621

2722
#[allow(clippy::module_inception)]
2823
pub mod config {
@@ -121,11 +116,13 @@ pub fn get_developer_provided_dtb(
121116
None
122117
}
123118

124-
pub fn get_vm_dtb_arc(vm_cfg: &AxVMConfig) -> Option<Arc<[u8]>> {
125-
if let Some(cache) = GENERATED_DTB_CACHE.get() {
126-
let cache_lock = cache.lock();
127-
if let Some(dtb) = cache_lock.get(&vm_cfg.id()) {
128-
return Some(dtb.clone());
119+
pub fn get_vm_dtb_arc(_vm_cfg: &AxVMConfig) -> Option<Arc<[u8]>> {
120+
#[cfg(target_arch = "aarch64")]
121+
{
122+
let cache_lock = dtb_cache().lock();
123+
if let Some(dtb) = cache_lock.get(&_vm_cfg.id()) {
124+
// Convert Vec<u8> to Arc<[u8]>
125+
return Some(Arc::from(dtb.as_slice()));
129126
}
130127
}
131128
None
@@ -165,7 +162,11 @@ fn handle_fdt_operations(vm_config: &mut AxVMConfig, vm_create_config: &AxVMCrat
165162
}
166163

167164
pub fn init_guest_vms() {
168-
GENERATED_DTB_CACHE.init_once(Mutex::new(BTreeMap::new()));
165+
// Initialize the DTB cache in the fdt module
166+
#[cfg(target_arch = "aarch64")]
167+
{
168+
init_dtb_cache();
169+
}
169170

170171
// First try to get configs from filesystem if fs feature is enabled
171172
let mut gvm_raw_configs = config::filesystem_vm_configs();

src/vmm/fdt/create.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use alloc::{
22
string::{String, ToString},
3-
sync::Arc,
43
vec::Vec,
54
};
65
use core::ptr::NonNull;
@@ -99,17 +98,7 @@ pub fn crate_guest_fdt(
9998
fdt_writer.finish().unwrap()
10099
}
101100

102-
/// Generate guest FDT cache the result
103-
/// # Return Value
104-
/// Returns the generated DTB data and stores it in the global cache
105-
pub fn crate_guest_fdt_with_cache(dtb_data: Vec<u8>, crate_config: &AxVMCrateConfig) {
106-
// Store data in global cache
107-
if let Some(cache) = crate::vmm::config::GENERATED_DTB_CACHE.get() {
108-
let mut cache_lock = cache.lock();
109-
let dtb_arc: Arc<[u8]> = Arc::from(dtb_data);
110-
cache_lock.insert(crate_config.base.id, dtb_arc);
111-
}
112-
}
101+
// 移除了 crate_guest_fdt_with_cache 函数,因为它现在在 mod.rs 中实现
113102

114103
/// Node processing action enumeration
115104
enum NodeAction {

src/vmm/fdt/mod.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,36 @@ mod device;
88
mod parser;
99
mod print;
1010

11+
use alloc::collections::BTreeMap;
12+
use alloc::vec::Vec;
13+
use axvm::config::AxVMCrateConfig;
14+
use lazyinit::LazyInit;
15+
use spin::Mutex;
16+
1117
// Re-export public functions
1218
pub use parser::*;
1319
// pub use print::print_fdt;
1420
pub use create::*;
1521
pub use device::build_node_path;
22+
23+
// DTB cache for generated device trees
24+
static GENERATED_DTB_CACHE: LazyInit<Mutex<BTreeMap<usize, Vec<u8>>>> = LazyInit::new();
25+
26+
/// Initialize the DTB cache
27+
pub fn init_dtb_cache() {
28+
GENERATED_DTB_CACHE.init_once(Mutex::new(BTreeMap::new()));
29+
}
30+
31+
/// Get reference to the DTB cache
32+
pub fn dtb_cache() -> &'static Mutex<BTreeMap<usize, Vec<u8>>> {
33+
GENERATED_DTB_CACHE.get().unwrap()
34+
}
35+
36+
/// Generate guest FDT cache the result
37+
/// # Return Value
38+
/// Returns the generated DTB data and stores it in the global cache
39+
pub fn crate_guest_fdt_with_cache(dtb_data: Vec<u8>, crate_config: &AxVMCrateConfig) {
40+
// Store data in global cache
41+
let mut cache_lock = dtb_cache().lock();
42+
cache_lock.insert(crate_config.base.id, dtb_data);
43+
}

src/vmm/fdt/parser.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use alloc::{string::ToString, vec::Vec};
44
use axvm::config::{AxVMConfig, AxVMCrateConfig, PassThroughDeviceConfig};
55
use fdt_parser::{Fdt, FdtHeader, PciRange, PciSpace};
66

7+
use crate::vmm::fdt::crate_guest_fdt_with_cache;
78
use crate::vmm::fdt::create::update_cpu_node;
89

910
pub fn get_host_fdt() -> &'static [u8] {
@@ -40,7 +41,8 @@ pub fn setup_guest_fdt_from_vmm(
4041
let passthrough_device_names = super::device::find_all_passthrough_devices(vm_cfg, &fdt);
4142

4243
let dtb_data = super::create::crate_guest_fdt(&fdt, &passthrough_device_names, crate_config);
43-
super::create::crate_guest_fdt_with_cache(dtb_data, crate_config);
44+
// 调用新的 crate_guest_fdt_with_cache 函数
45+
crate_guest_fdt_with_cache(dtb_data, crate_config);
4446
}
4547

4648
pub fn set_phys_cpu_sets(vm_cfg: &mut AxVMConfig, fdt: &Fdt, crate_config: &AxVMCrateConfig) {
@@ -375,5 +377,5 @@ pub fn update_provided_fdt(provided_dtb: &[u8], host_dtb: &[u8], crate_config: &
375377
let host_fdt = Fdt::from_bytes(host_dtb)
376378
.expect("Failed to parse DTB image, perhaps the DTB is invalid or corrupted");
377379
let provided_dtb_data = update_cpu_node(&provided_fdt, &host_fdt, crate_config);
378-
super::create::crate_guest_fdt_with_cache(provided_dtb_data, crate_config);
380+
crate_guest_fdt_with_cache(provided_dtb_data, crate_config);
379381
}

0 commit comments

Comments
 (0)