@samitouri / QOSamiQemu / commits / 4003e5e65f

hw/accel: add a per-accelerator callback to change VM accelerator handle

When a confidential virtual machine is reset, a new guest context in the accelerator must be generated post reset. Therefore, the old accelerator guest file handle must be closed and a new one created. To this end, a per-accelerator callback, "rebuild_guest" is introduced that would get called when a confidential guest is reset. Subsequent patches will introduce specific implementation of this callback for KVM accelerator. Signed-off-by: Ani Sinha <anisinha@redhat.com> Link: https://lore.kernel.org/r/20260225035000.385950-4-anisinha@redhat.com Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

Ani Sinha committed Feb 25, 2026 at 09:19 UTC 4003e5e65fe0a497705275eba73aef6c89870c82
2 files changed +39 -1
include/accel/accel-ops.h
+2
@@ -23,6 +23,8 @@ struct AccelClass {
23 AccelOpsClass *ops;
24
25 int (*init_machine)(AccelState *as, MachineState *ms);
26 + /* used mainly by confidential guests to rebuild guest state upon reset */
27 + int (*rebuild_guest)(MachineState *ms);
28 bool (*cpu_common_realize)(CPUState *cpu, Error **errp);
29 void (*cpu_common_unrealize)(CPUState *cpu);
30 /* get_stats: Append statistics to @buf */
system/runstate.c
+37 -1
@@ -42,6 +42,7 @@
42 #include "qapi/qapi-commands-run-state.h"
43 #include "qapi/qapi-events-run-state.h"
44 #include "qemu/accel.h"
45 +#include "accel/accel-ops.h"
46 #include "qemu/error-report.h"
47 #include "qemu/job.h"
48 #include "qemu/log.h"
@@ -509,6 +510,9 @@ void qemu_system_reset(ShutdownCause reason)
510 {
511 MachineClass *mc;
512 ResetType type;
513 + AccelClass *ac = ACCEL_GET_CLASS(current_accel());
514 + bool guest_state_rebuilt = false;
515 + int ret;
516
517 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
518
@@ -521,6 +525,29 @@ void qemu_system_reset(ShutdownCause reason)
525 default:
526 type = RESET_TYPE_COLD;
527 }
528 +
529 + if (!cpus_are_resettable() &&
530 + (reason == SHUTDOWN_CAUSE_GUEST_RESET ||
531 + reason == SHUTDOWN_CAUSE_HOST_QMP_SYSTEM_RESET)) {
532 + if (ac->rebuild_guest) {
533 + ret = ac->rebuild_guest(current_machine);
534 + if (ret < 0) {
535 + error_report("unable to rebuild guest: %s(%d)",
536 + strerror(-ret), ret);
537 + vm_stop(RUN_STATE_INTERNAL_ERROR);
538 + } else {
539 + info_report("virtual machine state has been rebuilt with new "
540 + "guest file handle.");
541 + guest_state_rebuilt = true;
542 + }
543 + } else if (!cpus_are_resettable()) {
544 + error_report("accelerator does not support reset!");
545 + } else {
546 + error_report("accelerator does not support rebuilding guest state,"
547 + " proceeding with normal reset!");
548 + }
549 + }
550 +
551 if (mc && mc->reset) {
552 mc->reset(current_machine, type);
553 } else {
@@ -543,7 +570,16 @@ void qemu_system_reset(ShutdownCause reason)
570 * it does _more_ than cpu_synchronize_all_post_reset().
571 */
572 if (cpus_are_resettable()) {
546 - cpu_synchronize_all_post_reset();
573 + if (guest_state_rebuilt) {
574 + /*
575 + * If guest state has been rebuilt, then we
576 + * need to sync full cpu state for non confidential guests post
577 + * reset.
578 + */
579 + cpu_synchronize_all_post_init();
580 + } else {
581 + cpu_synchronize_all_post_reset();
582 + }
583 }
584
585 vm_set_suspended(false);