Skip to content

Commit

Permalink
feat(vmm): exit the VMM on kbd reset (#12)
Browse files Browse the repository at this point in the history
Currently, when killing the `init` process, the VMM hangs because it is
unable to handle the kernel trying to reboot the machine.

On an x86 PC platform, it is possible to reset the CPU by [sending byte
`0xFE` to the keyboard controller command register](https://wiki.osdev.org/%228042%22_PS/2_Controller#CPU_Reset).
This is actually part of the emergency reboot procedure of [the Linux kernel](https://elixir.bootlin.com/linux/v6.8.2/source/arch/x86/kernel/reboot.c#L627).

This adds a bit of code to handle such a write operation by exiting the
VMM process.

Signed-off-by: Kuruyia <github@kuruyia.net>
  • Loading branch information
Kuruyia authored Apr 4, 2024
1 parent 6dec6b2 commit 823743d
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/vmm/src/core/cpu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use crate::core::devices::serial::{LumperSerial, SERIAL_PORT_BASE, SERIAL_PORT_L
use kvm_bindings::{kvm_fpu, kvm_regs, CpuId};
use kvm_ioctls::{VcpuExit, VcpuFd, VmFd};
use std::convert::TryInto;
use std::io;
use std::sync::{Arc, Mutex};
use std::{io, process};
use std::{result, u64};
use tracing::{error, info, warn};
use vm_memory::{Address, Bytes, GuestAddress, GuestMemoryError, GuestMemoryMmap};
Expand Down Expand Up @@ -34,6 +34,9 @@ const X86_CR0_PE: u64 = 0x1;
const X86_CR0_PG: u64 = 0x8000_0000;
const X86_CR4_PAE: u64 = 0x20;

const KBD_CMD_IO_ADDR: u16 = 0x64;
const KBD_RESET_CMD: u8 = 0xFE;

/// Errors encountered during vCPU operation.
#[derive(Debug)]
pub enum Error {
Expand Down Expand Up @@ -246,6 +249,12 @@ impl Vcpu {
)
.unwrap();
}
KBD_CMD_IO_ADDR => {
if data[0] == KBD_RESET_CMD {
info!(?exit_reason, "Guest reset via keyboard controller. Bye!");
process::exit(0);
}
}
_ => {
warn!(address = addr, "Unsupported device write at {:x?}", addr);
}
Expand Down

0 comments on commit 823743d

Please sign in to comment.