Skip to content

Commit 0b23d9a

Browse files
committed
```
feat(vmm): Adjust memory mapping types and optimize passthrough device configuration parsing - In `linux-aarch64-a1000-smp8.toml` and `linux-aarch64-rk3588-smp8.toml`, Changed the mapping type of `memory_regions` and removed some redundant `passthrough_devices` configurations. - Added `VmMemMappingType::MapReserved` branch processing logic to support mapping memory regions with the same physical address. - Refactored the `parse_passthrough_devices_address` function to prioritize the device address preset in the configuration file. If not set, it will fall back to parsing from the device tree, improving flexibility and compatibility. - Added debug logging to facilitate tracking of passthrough device parsing results. ```
1 parent be91819 commit 0b23d9a

File tree

4 files changed

+83
-63
lines changed

4 files changed

+83
-63
lines changed

configs/vms/linux-aarch64-a1000-smp8.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ dtb_path = "/path/to/dtb"
3434
# Memory regions with format (`base_paddr`, `size`, `flags`, `map_type`).
3535
# For `map_type`, 0 means `MAP_ALLOC`, 1 means `MAP_IDENTICAL`.
3636
memory_regions = [
37-
[0x8000_0000, 0x2000_0000, 0x7, 1], # System RAM 1G MAP_IDENTICAL
37+
[0x1_ce80_0000, 0x2000_0000, 0x7, 1], # System RAM 1G MAP_IDENTICAL
38+
[0x8000_0000, 0x7000_0000, 0xf, 2],
3839
]
3940
#
4041
# Device specifications
@@ -50,7 +51,6 @@ emu_devices = []
5051
# Name Base-Ipa Base-Pa Length Alloc-Irq.
5152
passthrough_devices = [
5253
["most-devices", 0x0, 0x0, 0x8000_0000, 0x1],
53-
["memory", 0x8000_0000, 0x8000_0000, 0x7000_0000, 0x1]
5454
]
5555
excluded_devices = [
5656
# ["/gic-v3"],

configs/vms/linux-aarch64-rk3588-smp8.toml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ dtb_path = "/path/to/dtb"
5050
# For `map_type`, 0 means `MAP_ALLOC`, 1 means `MAP_IDENTICAL`.
5151
memory_regions = [
5252
[0x940_0000, 0xd550_0000, 0x7, 1], # ram 3G MAP_IDENTICAL
53-
[0x920_0000, 0x2000, 0x7, 1]
53+
[0x920_0000, 0x2000, 0xf, 2]
54+
5455
]
5556

5657
#
@@ -108,13 +109,6 @@ passthrough_devices = [
108109
0x10_f000,
109110
0x17,
110111
],
111-
[
112-
"device",
113-
0x920_0000,
114-
0x920_0000,
115-
0x2000,
116-
0x17,
117-
],
118112
]
119113

120114
excluded_devices = [

src/vmm/config.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ fn vm_alloc_memorys(vm_create_config: &AxVMCrateConfig, vm: &VM) {
155155
vm.alloc_memory_region(Layout::from_size_align(memory.size, ALIGN).unwrap(), None)
156156
.expect("Failed to allocate memory region for VM");
157157
}
158+
VmMemMappingType::MapReserved => {
159+
info!("VM[{}] map same region: {:#x?}", vm.id(), memory);
160+
let layout = Layout::from_size_align(memory.size, ALIGN).unwrap();
161+
vm.map_memory_region(layout, Some(GuestPhysAddr::from(memory.gpa))).expect("Failed to map memory region for VM");
162+
}
158163
}
159164
}
160165
}

src/vmm/fdt/parser.rs

Lines changed: 74 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -218,70 +218,91 @@ fn add_pci_ranges_config(vm_cfg: &mut AxVMConfig, node_name: &str, range: &PciRa
218218
}
219219

220220
pub fn parse_passthrough_devices_address(vm_cfg: &mut AxVMConfig, dtb: &[u8]) {
221-
let fdt = Fdt::from_bytes(dtb)
222-
.expect("Failed to parse DTB image, perhaps the DTB is invalid or corrupted");
221+
let devices = vm_cfg.pass_through_devices().to_vec();
222+
if !devices.is_empty() && devices[0].length != 0 {
223+
for (index, device) in devices.iter().enumerate() {
224+
add_device_address_config(
225+
vm_cfg,
226+
&device.name,
227+
device.base_gpa,
228+
device.length,
229+
index,
230+
None,
231+
);
232+
}
233+
} else {
234+
let fdt = Fdt::from_bytes(dtb)
235+
.expect("Failed to parse DTB image, perhaps the DTB is invalid or corrupted");
223236

224-
// Clear existing passthrough device configurations
225-
vm_cfg.clear_pass_through_devices();
237+
// Clear existing passthrough device configurations
238+
vm_cfg.clear_pass_through_devices();
226239

227-
// Traverse all device tree nodes
228-
for node in fdt.all_nodes() {
229-
// Skip root node
230-
if node.name() == "/" || node.name().starts_with("memory") {
231-
continue;
232-
}
240+
// Traverse all device tree nodes
241+
for node in fdt.all_nodes() {
242+
// Skip root node
243+
if node.name() == "/" || node.name().starts_with("memory") {
244+
continue;
245+
}
233246

234-
let node_name = node.name().to_string();
247+
let node_name = node.name().to_string();
235248

236-
// Check if it's a PCIe device node
237-
if node_name.starts_with("pcie@") || node_name.contains("pci") {
238-
// Process PCIe device's ranges property
239-
if let Some(pci) = node.clone().into_pci()
240-
&& let Ok(ranges) = pci.ranges()
241-
{
242-
for (index, range) in ranges.enumerate() {
243-
add_pci_ranges_config(vm_cfg, &node_name, &range, index);
249+
// Check if it's a PCIe device node
250+
if node_name.starts_with("pcie@") || node_name.contains("pci") {
251+
// Process PCIe device's ranges property
252+
if let Some(pci) = node.clone().into_pci()
253+
&& let Ok(ranges) = pci.ranges()
254+
{
255+
for (index, range) in ranges.enumerate() {
256+
add_pci_ranges_config(vm_cfg, &node_name, &range, index);
257+
}
244258
}
245-
}
246259

247-
// Process PCIe device's reg property (ECAM space)
248-
if let Some(reg_iter) = node.reg() {
249-
for (index, reg) in reg_iter.enumerate() {
250-
let base_address = reg.address as usize;
251-
let size = reg.size.unwrap_or(0);
252-
253-
add_device_address_config(
254-
vm_cfg,
255-
&node_name,
256-
base_address,
257-
size,
258-
index,
259-
Some("ecam"),
260-
);
260+
// Process PCIe device's reg property (ECAM space)
261+
if let Some(reg_iter) = node.reg() {
262+
for (index, reg) in reg_iter.enumerate() {
263+
let base_address = reg.address as usize;
264+
let size = reg.size.unwrap_or(0);
265+
266+
add_device_address_config(
267+
vm_cfg,
268+
&node_name,
269+
base_address,
270+
size,
271+
index,
272+
Some("ecam"),
273+
);
274+
}
261275
}
262-
}
263-
} else {
264-
// Get device's reg property (process regular devices)
265-
if let Some(reg_iter) = node.reg() {
266-
// Process all address segments of the device
267-
for (index, reg) in reg_iter.enumerate() {
268-
// Get device's address and size information
269-
let base_address = reg.address as usize;
270-
let size = reg.size.unwrap_or(0);
271-
272-
add_device_address_config(vm_cfg, &node_name, base_address, size, index, None);
276+
} else {
277+
// Get device's reg property (process regular devices)
278+
if let Some(reg_iter) = node.reg() {
279+
// Process all address segments of the device
280+
for (index, reg) in reg_iter.enumerate() {
281+
// Get device's address and size information
282+
let base_address = reg.address as usize;
283+
let size = reg.size.unwrap_or(0);
284+
285+
add_device_address_config(
286+
vm_cfg,
287+
&node_name,
288+
base_address,
289+
size,
290+
index,
291+
None,
292+
);
293+
}
273294
}
274295
}
275296
}
297+
trace!(
298+
"All passthrough devices: {:#x?}",
299+
vm_cfg.pass_through_devices()
300+
);
301+
debug!(
302+
"Finished parsing passthrough devices, total: {}",
303+
vm_cfg.pass_through_devices().len()
304+
);
276305
}
277-
trace!(
278-
"All passthrough devices: {:#x?}",
279-
vm_cfg.pass_through_devices()
280-
);
281-
debug!(
282-
"Finished parsing passthrough devices, total: {}",
283-
vm_cfg.pass_through_devices().len()
284-
);
285306
}
286307

287308
pub fn parse_vm_interrupt(vm_cfg: &mut AxVMConfig, dtb: &[u8]) {

0 commit comments

Comments
 (0)