RVVM icon indicating copy to clipboard operation
RVVM copied to clipboard

Shutdown notification to guest on window close

Open X547 opened this issue 2 years ago • 10 comments

Currently closing RVVM window cause instant shutdown that give no chance to gratefully shutdown guest and may cause data loss.

X547 avatar Nov 24 '22 12:11 X547

Currently closing RVVM window cause instant shutdown that give no chance to gratefully shutdown guest and may cause data loss.

Somewhat trying to improve on that to at least not kill the process immediately (bf692d4 and similar), this at least flushes the blkdev caches. No idea how to make a graceful shutdown notification for the guest tho (That would be best). We don't have ACPI for power button or anything like that on RISC-V afaik. USB HID has power keycode. Maybe you know other ways to notify guests about shutdown?

LekKit avatar Nov 24 '22 14:11 LekKit

Status: If I2C HID has same keycodes as USB HID, and the HID_KEY_POWER will be properly handled by guests, this may be resolved soon (See #58). If that approach fails, this most likely won't be worked on.

LekKit avatar Nov 30 '22 15:11 LekKit

This should help: https://www.kernel.org/doc/Documentation/devicetree/bindings/power/reset/gpio-poweroff.txt

X547 avatar Dec 04 '22 13:12 X547

This should help: https://www.kernel.org/doc/Documentation/devicetree/bindings/power/reset/gpio-poweroff.txt

A bit of research reveals this signals same KEY_POWER event to the guest through internal kernel facilities on Linux, I.e. not much different from pressing HID_KEY_POWER using existing devices. For handling KEY_POWER, udev rules are needed (or some systemd magic) which means this is never going to work for Linux guests without additional configuration within them.

I tested bunch of distros (Ubuntu, Debian, Arch) with extended DEs like KDE Plasma, everyone happily ignored HID_KEY_POWER until I manually added an udev rule. Haiku doesn't handle HID_KEY_POWER from i2c hid as well it seems.

LekKit avatar Dec 05 '22 15:12 LekKit

Seems that at some point HID_KEY_POWER hook was implemented in Arch RISC-V distro, so that a simple sequence of HID API calls shuts down the guest cleanly:

hid_keyboard_press(win->keyboard, HID_KEY_POWER);
hid_keyboard_release(win->keyboard, HID_KEY_POWER);

However this still doesn't work for a lot of other guests (Many operating systems do not have I2C HID driver or don't handle HID_KEY_POWER). So maybe we should keep the behavior on window closing, but introduce some new GUI button or a keybind for guest shutdown?

LekKit avatar Jun 16 '24 15:06 LekKit

I will be happy if I will be able to gracefully shutdown VM with a close button. It will improve convenience a lot, at least for me. Kill VM by close button is dangerous and easily can cause a loss if unsaved/unflushed data.

Maybe it is a good idea to first try to send HID_KEY_POWER and then if close button is pressed second time after small time interval then show dialog "Do you want to shutdown VM? Yes/No".

For non-GUI mode HID_KEY_POWER can be mapped to Ctrl+C. Behavior can be controlled with some command-line option.

X547 avatar Jun 18 '24 17:06 X547

For non-GUI mode HID_KEY_POWER can be mapped to Ctrl+C. Behavior can be controlled with some command-line option.

That's not a good choice,though. Mapping HID_KEY_POWER to Ctrl+C in non-GUI will cause trouble to stdio terminal using mode, and for most times, terminal users can stop the program by themselves and dont need a 'shutdown' option.

fish4terrisa-MSDSM avatar Jun 24 '24 01:06 fish4terrisa-MSDSM

That's not a good choice,though. Mapping HID_KEY_POWER to Ctrl+C in non-GUI will cause trouble to stdio terminal using mode, and for most times, terminal users can stop the program by themselves and dont need a 'shutdown' option.

I'm probably going to implement keybinds somewhat similar to QEMU ones:

  • Ctrl + A, X: Exit RVVM
  • Ctrl + A, P: Send KEY_POWEROFF to the machine (not sure how to access hid_kbd handle from chardev_term tho, and QEMU doesn't implement this)

It will only apply to terminals over stdio, since PTY/pipe terminals should have their own way of detaching from the session without affecting RVVM guest.

LekKit avatar Jun 24 '24 08:06 LekKit

That's not a good choice,though. Mapping HID_KEY_POWER to Ctrl+C in non-GUI will cause trouble to stdio terminal using mode, and for most times, terminal users can stop the program by themselves and dont need a 'shutdown' option.

I'm probably going to implement keybinds somewhat similar to QEMU ones:

  • Ctrl + A, X: Exit RVVM
  • Ctrl + A, P: Send KEY_POWEROFF to the machine (not sure how to access hid_kbd handle from chardev_term tho, and QEMU doesn't implement this)

It will only apply to terminals over stdio, since PTY/pipe terminals should have their own way of detaching from the session without affecting RVVM guest.

That seems good. However, will it influence the use of Ctrl + A in the guest? (For example, use Ctrl+Shift+A to behave like Ctrl+A in the guest)

fish4terrisa-MSDSM avatar Jun 24 '24 08:06 fish4terrisa-MSDSM

That seems good. However, will it influence the use of Ctrl + A in the guest? (For example, use Ctrl+Shift+A to behave like Ctrl+A in the guest)

Double Ctrl + A should send a single Ctrl + A into the guest.

LekKit avatar Jun 24 '24 09:06 LekKit