Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,12 @@ impl<P: MemoryView + Process> QemuProcfs<P> {
);
}

let qemu_map = biggest_map.ok_or_else(|| Error(ErrorOrigin::Connector, ErrorKind::NotFound)
.log_error("Unable to find the QEMU guest memory map. This usually indicates insufficient permissions to acquire the QEMU memory maps. Are you running with appropiate access rights?")
)?;

info!("qemu memory map found {:?}", qemu_map);
if let Some(qemu_map) = &biggest_map {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we changing this?

info!("qemu memory map found {:?}", qemu_map);
}

let qemu_map = biggest_map
.unwrap_or(CTup2(Address::INVALID, 0));
Self::with_cmdline_and_mem(prc, &cmdline, qemu_map)
}

Expand Down
43 changes: 40 additions & 3 deletions src/mem_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ struct Mapping {
pub range_start: umem,
pub range_end: umem,
pub remap_start: umem,
pub host_start: Option<umem>,
}

impl Mapping {
Expand All @@ -27,6 +28,7 @@ impl Mapping {
range_start,
range_end,
remap_start,
host_start: None,
}
}
}
Expand All @@ -35,6 +37,13 @@ pub fn qemu_mem_mappings(
cmdline: &str,
qemu_map: &CTup2<Address, umem>,
) -> Result<MemoryMap<(Address, umem)>> {
let qemu_map = || match qemu_map {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this have to be a function that we resolve later in the different code paths? Can't we just error out immediately?

qemu_map if qemu_map.0 != Address::INVALID =>
Err(Error(ErrorOrigin::Connector, ErrorKind::NotFound)
.log_error("Unable to find the QEMU guest memory map. This usually indicates insufficient permissions to acquire the QEMU memory maps. Are you running with appropiate access rights?")
),
qemu_map => Ok(qemu_map),
};
let mut mem_map = MemoryMap::new();

let mappings = if let Ok(mappings) = qmp_get_mtree(cmdline.split_whitespace()) {
Expand All @@ -54,15 +63,21 @@ pub fn qemu_mem_mappings(
.unwrap_or_else(|| "pc".into())
};
info!("qemu process started with machine: {}", machine);
qemu_get_mtree_fallback(&machine, qemu_map)
qemu_get_mtree_fallback(&machine, qemu_map()?)
};

// add all mappings
let mut real_base = None;
for mapping in mappings.iter() {
let real_base = match mapping.host_start.map(Address::from).or(real_base) {
Some(start) => start,
None => *real_base.insert(qemu_map()?.0) + mapping.remap_start,
};

mem_map.push_range(
mapping.range_start.into(),
mapping.range_end.into(),
qemu_map.0 + mapping.remap_start,
real_base,
);
}

Expand Down Expand Up @@ -115,7 +130,29 @@ fn qmp_get_mtree_stream<S: Read + Write + Clone>(stream: S) -> Result<Vec<Mappin
})
.map_err(|err| Error(ErrorOrigin::Connector, ErrorKind::Configuration).log_error(err))?;

Ok(qmp_parse_mtree(&mtreestr))
let mut mappings = qmp_parse_mtree(&mtreestr);
for mapping in &mut mappings {
if let Ok(line) = qmp.execute(&qmp::human_monitor_command {
command_line: format!("gpa2hva {:#10x}", mapping.range_start),
cpu_index: None,
}) {
let line = line.trim_end();
let hva = scan_fmt!(line, "Host virtual address for 0x{x} (pc.ram) is 0x{x}", [hex umem], [hex umem])
.map_err(|err| Error(
ErrorOrigin::Connector,
ErrorKind::UnsupportedOptionalFeature,
).log_info(format_args!("failed to parse host address from {line:?}: {err}")));
match hva {
Ok((gpa, hva)) if gpa == mapping.range_start =>
mapping.host_start = Some(hva),
_ => {
log::warn!("failed to parse host address from {line:?}");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this ever happen? If it does happen rarely, it might be better to throw an error, instead of silently ignoring and falling back to the largest map offset.

},
}
}
}

Ok(mappings)
}

#[cfg(not(all(target_os = "linux", feature = "qmp")))]
Expand Down