Can I make Qemu exit with failure on kernel panic?

QEMU -no-reboot + kernel CLI kernel.panic=-1

  • qemu-system-X -no-reboot QEMU CLI option: makes QEMU exit when the guest tries to reboot
  • kernel.panic=-1 kernel boot parameter: makes Linux try to reboot immediately after a panic: https://github.com/torvalds/linux/blob/v4.17/Documentation/admin-guide/kernel-parameters.txt#L2931

It also returns 0 like pvpanic, but has the following advantages:

  • no need to recompile anything, just a boot parameter
  • works on arm and aarch64 -M virt as well as x86, while pvpanic seems x86-specific since it is under arch/x86

Tested with this setup.

Track the panic symbol with GDB

Another way to go about this might be to detect when the address of the panic function is reached, and then try to make QEMU quit.

You can definitely break GDB on panic as explained at: https://stackoverflow.com/questions/11408041/how-to-debug-the-linux-kernel-with-gdb-and-qemu/33203642#33203642

But then TODO: how to make QEMU exit with status 1? Using monitor quit, from inside GDB, which forwards quit to the QEMU monitor from GDB, gets really close, but not quite since it does not exit with status 0.

gem5 does this tracking by default natively, which is pretty awesome.

This happens at: https://github.com/gem5/gem5/blob/1da285dfcc31b904afc27e440544d006aae25b38/src/arch/arm/linux/system.cc#L73

Maybe QEMU devs can take some inspiration from this technique and implement something similar.


I've got something that's working:

  • Configure (and build) the kernel with CONFIG_PVPANIC=y; this produces a kernel with compiled-in support for the pvpanic device.
  • Invoke qemu-system-x86_64 with the -device pvpanic option; this instructs Qemu to catch (and exit on) a kernel panic.

A kernel panic causes qemu-system-x86_64 to exit successfully (return status 0), but at least it's not hanging anymore.

Many thanks to @dsstorefile1 for pointing me in the right direction.

References:

  • https://cateee.net/lkddb/web-lkddb/PVPANIC.html
  • https://github.com/qemu/qemu/blob/master/docs/specs/pvpanic.txt