Let OSDK shutdown sifive_u QEMU machine
This commit is contained in:
parent
902106eb2e
commit
53803a9fb1
|
|
@ -75,6 +75,7 @@ supported_archs = ["riscv64"]
|
||||||
build.features = ["riscv_sv39_mode"]
|
build.features = ["riscv_sv39_mode"]
|
||||||
boot.method = "qemu-direct"
|
boot.method = "qemu-direct"
|
||||||
build.strip_elf = false
|
build.strip_elf = false
|
||||||
|
qemu.with_monitor = true
|
||||||
|
|
||||||
qemu.args = """\
|
qemu.args = """\
|
||||||
-machine sifive_u \
|
-machine sifive_u \
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
arch::Arch,
|
||||||
config::{
|
config::{
|
||||||
scheme::{ActionChoice, BootMethod},
|
scheme::{ActionChoice, BootMethod},
|
||||||
Config,
|
Config,
|
||||||
|
|
@ -265,27 +266,17 @@ impl Bundle {
|
||||||
}
|
}
|
||||||
|
|
||||||
let exit_status = if action.qemu.with_monitor {
|
let exit_status = if action.qemu.with_monitor {
|
||||||
let qemu_socket = NamedTempFile::new().unwrap().into_temp_path();
|
let qemu_monitor_socket_path = NamedTempFile::new().unwrap().into_temp_path();
|
||||||
qemu_cmd.arg("-monitor").arg(format!(
|
qemu_cmd.arg("-monitor").arg(format!(
|
||||||
"unix:{},server,nowait",
|
"unix:{},server,nowait",
|
||||||
qemu_socket.to_string_lossy()
|
qemu_monitor_socket_path.to_string_lossy()
|
||||||
));
|
));
|
||||||
|
|
||||||
info!("Running QEMU: {qemu_cmd:#?}");
|
info!("Running QEMU: {qemu_cmd:#?}");
|
||||||
let mut qemu_child = qemu_cmd.spawn().unwrap();
|
let mut qemu_child = qemu_cmd.spawn().unwrap();
|
||||||
std::thread::sleep(Duration::from_secs(1)); // Wait for QEMU to start
|
std::thread::sleep(Duration::from_secs(1)); // Wait for QEMU to start
|
||||||
let mut qemu_monitor_stream = UnixStream::connect(qemu_socket).unwrap();
|
let mut qemu_monitor_stream = UnixStream::connect(&qemu_monitor_socket_path).unwrap();
|
||||||
|
wait_until_guest_kernel_shutdown(config, &mut qemu_monitor_stream);
|
||||||
// Check VM status every 0.1 seconds and break the loop if the VM is stopped.
|
|
||||||
while qemu_monitor_stream.write_all(b"info status\n").is_ok() {
|
|
||||||
let status = BufReader::new(&qemu_monitor_stream)
|
|
||||||
.lines()
|
|
||||||
.find(|line| line.as_ref().is_ok_and(|s| s.starts_with("VM status:")));
|
|
||||||
if status.is_some_and(|msg| msg.unwrap() == "VM status: paused (shutdown)") {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
std::thread::sleep(Duration::from_millis(100));
|
|
||||||
}
|
|
||||||
info!("VM is paused (shutdown)");
|
info!("VM is paused (shutdown)");
|
||||||
|
|
||||||
self.post_run_action(config, Some(&mut qemu_monitor_stream));
|
self.post_run_action(config, Some(&mut qemu_monitor_stream));
|
||||||
|
|
@ -310,6 +301,32 @@ impl Bundle {
|
||||||
_ /* unknown, e.g., a triple fault */ => { std::process::exit(2) },
|
_ /* unknown, e.g., a triple fault */ => { std::process::exit(2) },
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn wait_until_guest_kernel_shutdown(config: &Config, qemu_monitor_stream: &mut UnixStream) {
|
||||||
|
let log_file = std::fs::File::open(config.work_dir.join("qemu.log")).unwrap();
|
||||||
|
|
||||||
|
// Check VM status every 0.1 seconds and break the loop if the VM is stopped or hanging.
|
||||||
|
while qemu_monitor_stream.write_all(b"info status\n").is_ok() {
|
||||||
|
let status = BufReader::new(&mut *qemu_monitor_stream)
|
||||||
|
.lines()
|
||||||
|
.find(|line| line.as_ref().is_ok_and(|s| s.starts_with("VM status:")));
|
||||||
|
if status.is_some_and(|msg| msg.unwrap() == "VM status: paused (shutdown)") {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.target_arch == Arch::RiscV64 {
|
||||||
|
let log = rev_buf_reader::RevBufReader::new(&log_file);
|
||||||
|
if log.lines().next().is_some_and(|line| {
|
||||||
|
line.as_ref().is_ok_and(|s| {
|
||||||
|
s.contains("SBI system_reset cannot shut down the underlying machine")
|
||||||
|
})
|
||||||
|
}) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::thread::sleep(Duration::from_millis(100));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Move the vm_image into the bundle.
|
/// Move the vm_image into the bundle.
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue