All of lore.kernel.org
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH v5 0/9]: Introduce the RunState type
@ 2011-09-09 20:25 Luiz Capitulino
  2011-09-09 20:25 ` [Qemu-devel] [PATCH 1/9] Move vm_state_notify() prototype from cpus.h to sysemu.h Luiz Capitulino
                   ` (8 more replies)
  0 siblings, 9 replies; 13+ messages in thread
From: Luiz Capitulino @ 2011-09-09 20:25 UTC (permalink / raw)
  To: qemu-devel; +Cc: kwolf, aliguori, jan.kiszka, armbru, amit.shah, vilanova

It replaces the VMSTOP macros and allows us to drop some global variables.

Additionally, the problem with issuing 'cont' when the VM is in bad state
is addressed and we make the current state available in QMP and HMP.

** IMPORTANT: patch 04/09 is a new addition, it needs careful review.

This is breaking every few days due to changes in master, let's try to
review and merge it ASAP.

changelog
---------

v5

o Rebase on top of latest master
o Use the state transition table as suggested by Lluís Vilanova
o Minor reorg in the code to accommodate the new tables

v4

o Rebase on top of latest master
o Fix qemu_system_vmstop_request() to take a RunState argument
o Drop RSTATE_RESTORE_FAILED, it's not currently used
o Check for valid transitions (will abort on invalid ones)

v3

o Rebase on top of latest master
o Fix a spice VM change state handler which hasn't been converted
o Turn runstate_get() into runstate_check()
o Rename vm_is_running() to runstate_is_running()
o Other minor renames and log improvements

v2

o Rename the new type from QemuState to RunState
  (also renames related functions)
o Rename the enum values to contain proper word seperation
o Redo patch 'Monitor: Don't allow cont on bad VM state' to not use a global
  variable
o Make the current VM state also available in HMP
o Improve some commit logs a bit

 audio/audio.c      |    2 +-
 cpus.c             |   20 +++---
 cpus.h             |    1 -
 gdbstub.c          |   34 ++++++------
 hw/etraxfs_dma.c   |    2 +-
 hw/ide/ahci.c      |    2 +-
 hw/ide/core.c      |    4 +-
 hw/ide/internal.h  |    3 +-
 hw/ide/pci.c       |    2 +-
 hw/kvmclock.c      |    5 +-
 hw/qxl.c           |    5 +-
 hw/scsi-disk.c     |    4 +-
 hw/virtio-blk.c    |    5 +-
 hw/virtio.c        |    4 +-
 hw/watchdog.c      |    2 +-
 kvm-all.c          |    2 +-
 migration.c        |   14 +++--
 monitor.c          |   22 +++++--
 qemu-timer.c       |    9 ++-
 qerror.c           |    4 +
 qerror.h           |    3 +
 qmp-commands.hx    |   19 ++++++-
 savevm.c           |    8 +-
 sysemu.h           |   42 +++++++++-----
 target-i386/kvm.c  |    4 +-
 ui/sdl.c           |    6 +-
 ui/spice-display.c |    3 +-
 ui/spice-display.h |    4 +-
 vl.c               |  156 ++++++++++++++++++++++++++++++++++++++++++++++------
 xen-all.c          |    8 ++-
 30 files changed, 290 insertions(+), 109 deletions(-)

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Qemu-devel] [PATCH 1/9] Move vm_state_notify() prototype from cpus.h to sysemu.h
  2011-09-09 20:25 [Qemu-devel] [PATCH v5 0/9]: Introduce the RunState type Luiz Capitulino
@ 2011-09-09 20:25 ` Luiz Capitulino
  2011-09-09 20:25 ` [Qemu-devel] [PATCH 2/9] Replace the VMSTOP macros with a proper state type Luiz Capitulino
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 13+ messages in thread
From: Luiz Capitulino @ 2011-09-09 20:25 UTC (permalink / raw)
  To: qemu-devel; +Cc: kwolf, aliguori, jan.kiszka, armbru, amit.shah, vilanova

It's where all the state handling functions prototypes are located.

Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
---
 cpus.h   |    1 -
 sysemu.h |    1 +
 2 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/cpus.h b/cpus.h
index f42b54e..5885885 100644
--- a/cpus.h
+++ b/cpus.h
@@ -15,7 +15,6 @@ void cpu_synchronize_all_post_init(void);
 /* vl.c */
 extern int smp_cores;
 extern int smp_threads;
-void vm_state_notify(int running, int reason);
 bool cpu_exec_all(void);
 void set_numa_modes(void);
 void set_cpu_log(const char *optarg);
diff --git a/sysemu.h b/sysemu.h
index 9090457..eb66af5 100644
--- a/sysemu.h
+++ b/sysemu.h
@@ -23,6 +23,7 @@ typedef void VMChangeStateHandler(void *opaque, int running, int reason);
 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
                                                      void *opaque);
 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e);
+void vm_state_notify(int running, int reason);
 
 #define VMSTOP_USER      0
 #define VMSTOP_DEBUG     1
-- 
1.7.7.rc0.72.g4b5ea

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [Qemu-devel] [PATCH 2/9] Replace the VMSTOP macros with a proper state type
  2011-09-09 20:25 [Qemu-devel] [PATCH v5 0/9]: Introduce the RunState type Luiz Capitulino
  2011-09-09 20:25 ` [Qemu-devel] [PATCH 1/9] Move vm_state_notify() prototype from cpus.h to sysemu.h Luiz Capitulino
@ 2011-09-09 20:25 ` Luiz Capitulino
  2011-09-09 20:25 ` [Qemu-devel] [PATCH 3/9] RunState: Add additional states Luiz Capitulino
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 13+ messages in thread
From: Luiz Capitulino @ 2011-09-09 20:25 UTC (permalink / raw)
  To: qemu-devel; +Cc: kwolf, aliguori, jan.kiszka, armbru, amit.shah, vilanova

Today, when notifying a VM state change with vm_state_notify(),
we pass a VMSTOP macro as the 'reason' argument. This is not ideal
because the VMSTOP macros tell why qemu stopped and not exactly
what the current VM state is.

One example to demonstrate this problem is that vm_start() calls
vm_state_notify() with reason=0, which turns out to be VMSTOP_USER.

This commit fixes that by replacing the VMSTOP macros with a proper
state type called RunState.

Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
---
 audio/audio.c      |    2 +-
 cpus.c             |   10 +++++-----
 gdbstub.c          |   30 +++++++++++++++---------------
 hw/ide/ahci.c      |    2 +-
 hw/ide/core.c      |    4 ++--
 hw/ide/internal.h  |    3 ++-
 hw/ide/pci.c       |    2 +-
 hw/kvmclock.c      |    3 ++-
 hw/qxl.c           |    5 +++--
 hw/scsi-disk.c     |    4 ++--
 hw/virtio-blk.c    |    5 +++--
 hw/virtio.c        |    2 +-
 hw/watchdog.c      |    2 +-
 kvm-all.c          |    2 +-
 migration.c        |    2 +-
 monitor.c          |    4 ++--
 qemu-timer.c       |    3 ++-
 savevm.c           |    4 ++--
 sysemu.h           |   32 ++++++++++++++++++--------------
 target-i386/kvm.c  |    2 +-
 ui/spice-display.c |    3 ++-
 ui/spice-display.h |    4 +++-
 vl.c               |   26 +++++++++++++-------------
 xen-all.c          |    6 ++++--
 24 files changed, 88 insertions(+), 74 deletions(-)

diff --git a/audio/audio.c b/audio/audio.c
index 5649075..50d0d71 100644
--- a/audio/audio.c
+++ b/audio/audio.c
@@ -1743,7 +1743,7 @@ static int audio_driver_init (AudioState *s, struct audio_driver *drv)
 }
 
 static void audio_vm_change_state_handler (void *opaque, int running,
-                                           int reason)
+                                           RunState state)
 {
     AudioState *s = opaque;
     HWVoiceOut *hwo = NULL;
diff --git a/cpus.c b/cpus.c
index 54c188c..76a79ac 100644
--- a/cpus.c
+++ b/cpus.c
@@ -118,13 +118,13 @@ int cpu_is_stopped(CPUState *env)
     return !vm_running || env->stopped;
 }
 
-static void do_vm_stop(int reason)
+static void do_vm_stop(RunState state)
 {
     if (vm_running) {
         cpu_disable_ticks();
         vm_running = 0;
         pause_all_vcpus();
-        vm_state_notify(0, reason);
+        vm_state_notify(0, state);
         qemu_aio_flush();
         bdrv_flush_all();
         monitor_protocol_event(QEVENT_STOP, NULL);
@@ -878,10 +878,10 @@ void cpu_stop_current(void)
     }
 }
 
-void vm_stop(int reason)
+void vm_stop(RunState state)
 {
     if (!qemu_thread_is_self(&io_thread)) {
-        qemu_system_vmstop_request(reason);
+        qemu_system_vmstop_request(state);
         /*
          * FIXME: should not return to device code in case
          * vm_stop() has been requested.
@@ -889,7 +889,7 @@ void vm_stop(int reason)
         cpu_stop_current();
         return;
     }
-    do_vm_stop(reason);
+    do_vm_stop(state);
 }
 
 static int tcg_cpu_exec(CPUState *env)
diff --git a/gdbstub.c b/gdbstub.c
index 3b87c27..a308a76 100644
--- a/gdbstub.c
+++ b/gdbstub.c
@@ -2267,7 +2267,7 @@ void gdb_set_stop_cpu(CPUState *env)
 }
 
 #ifndef CONFIG_USER_ONLY
-static void gdb_vm_state_change(void *opaque, int running, int reason)
+static void gdb_vm_state_change(void *opaque, int running, RunState state)
 {
     GDBState *s = gdbserver_state;
     CPUState *env = s->c_cpu;
@@ -2278,8 +2278,8 @@ static void gdb_vm_state_change(void *opaque, int running, int reason)
     if (running || s->state == RS_INACTIVE || s->state == RS_SYSCALL) {
         return;
     }
-    switch (reason) {
-    case VMSTOP_DEBUG:
+    switch (state) {
+    case RSTATE_DEBUG:
         if (env->watchpoint_hit) {
             switch (env->watchpoint_hit->flags & BP_MEM_ACCESS) {
             case BP_MEM_READ:
@@ -2302,25 +2302,25 @@ static void gdb_vm_state_change(void *opaque, int running, int reason)
         tb_flush(env);
         ret = GDB_SIGNAL_TRAP;
         break;
-    case VMSTOP_USER:
+    case RSTATE_PAUSED:
         ret = GDB_SIGNAL_INT;
         break;
-    case VMSTOP_SHUTDOWN:
+    case RSTATE_SHUTDOWN:
         ret = GDB_SIGNAL_QUIT;
         break;
-    case VMSTOP_DISKFULL:
+    case RSTATE_IO_ERROR:
         ret = GDB_SIGNAL_IO;
         break;
-    case VMSTOP_WATCHDOG:
+    case RSTATE_WATCHDOG:
         ret = GDB_SIGNAL_ALRM;
         break;
-    case VMSTOP_PANIC:
+    case RSTATE_PANICKED:
         ret = GDB_SIGNAL_ABRT;
         break;
-    case VMSTOP_SAVEVM:
-    case VMSTOP_LOADVM:
+    case RSTATE_SAVEVM:
+    case RSTATE_RESTORE:
         return;
-    case VMSTOP_MIGRATE:
+    case RSTATE_PRE_MIGRATE:
         ret = GDB_SIGNAL_XCPU;
         break;
     default:
@@ -2357,7 +2357,7 @@ void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
     gdb_current_syscall_cb = cb;
     s->state = RS_SYSCALL;
 #ifndef CONFIG_USER_ONLY
-    vm_stop(VMSTOP_DEBUG);
+    vm_stop(RSTATE_DEBUG);
 #endif
     s->state = RS_IDLE;
     va_start(va, fmt);
@@ -2431,7 +2431,7 @@ static void gdb_read_byte(GDBState *s, int ch)
     if (vm_running) {
         /* when the CPU is running, we cannot do anything except stop
            it when receiving a char */
-        vm_stop(VMSTOP_USER);
+        vm_stop(RSTATE_PAUSED);
     } else
 #endif
     {
@@ -2693,7 +2693,7 @@ static void gdb_chr_event(void *opaque, int event)
 {
     switch (event) {
     case CHR_EVENT_OPENED:
-        vm_stop(VMSTOP_USER);
+        vm_stop(RSTATE_PAUSED);
         gdb_has_xml = 0;
         break;
     default:
@@ -2734,7 +2734,7 @@ static int gdb_monitor_write(CharDriverState *chr, const uint8_t *buf, int len)
 static void gdb_sigterm_handler(int signal)
 {
     if (vm_running) {
-        vm_stop(VMSTOP_USER);
+        vm_stop(RSTATE_PAUSED);
     }
 }
 #endif
diff --git a/hw/ide/ahci.c b/hw/ide/ahci.c
index f4fa154..b702a32 100644
--- a/hw/ide/ahci.c
+++ b/hw/ide/ahci.c
@@ -1105,7 +1105,7 @@ static void ahci_irq_set(void *opaque, int n, int level)
 {
 }
 
-static void ahci_dma_restart_cb(void *opaque, int running, int reason)
+static void ahci_dma_restart_cb(void *opaque, int running, RunState state)
 {
 }
 
diff --git a/hw/ide/core.c b/hw/ide/core.c
index 1806e00..1cc9d5e 100644
--- a/hw/ide/core.c
+++ b/hw/ide/core.c
@@ -526,7 +526,7 @@ static int ide_handle_rw_error(IDEState *s, int error, int op)
         s->bus->dma->ops->set_unit(s->bus->dma, s->unit);
         s->bus->error_status = op;
         bdrv_mon_event(s->bs, BDRV_ACTION_STOP, is_read);
-        vm_stop(VMSTOP_DISKFULL);
+        vm_stop(RSTATE_IO_ERROR);
     } else {
         if (op & BM_STATUS_DMA_RETRY) {
             dma_buf_commit(s, 0);
@@ -1840,7 +1840,7 @@ static int ide_nop_int(IDEDMA *dma, int x)
     return 0;
 }
 
-static void ide_nop_restart(void *opaque, int x, int y)
+static void ide_nop_restart(void *opaque, int x, RunState y)
 {
 }
 
diff --git a/hw/ide/internal.h b/hw/ide/internal.h
index 1117852..a0deda9 100644
--- a/hw/ide/internal.h
+++ b/hw/ide/internal.h
@@ -10,6 +10,7 @@
 #include "block_int.h"
 #include "iorange.h"
 #include "dma.h"
+#include "sysemu.h"
 
 /* debug IDE devices */
 //#define DEBUG_IDE
@@ -388,7 +389,7 @@ typedef void EndTransferFunc(IDEState *);
 typedef void DMAStartFunc(IDEDMA *, IDEState *, BlockDriverCompletionFunc *);
 typedef int DMAFunc(IDEDMA *);
 typedef int DMAIntFunc(IDEDMA *, int);
-typedef void DMARestartFunc(void *, int, int);
+typedef void DMARestartFunc(void *, int, RunState);
 
 struct unreported_events {
     bool eject_request;
diff --git a/hw/ide/pci.c b/hw/ide/pci.c
index d1a14d7..46200a5 100644
--- a/hw/ide/pci.c
+++ b/hw/ide/pci.c
@@ -223,7 +223,7 @@ static void bmdma_restart_bh(void *opaque)
     }
 }
 
-static void bmdma_restart_cb(void *opaque, int running, int reason)
+static void bmdma_restart_cb(void *opaque, int running, RunState state)
 {
     IDEDMA *dma = opaque;
     BMDMAState *bm = DO_UPCAST(BMDMAState, dma, dma);
diff --git a/hw/kvmclock.c b/hw/kvmclock.c
index b73aec4..88961be 100644
--- a/hw/kvmclock.c
+++ b/hw/kvmclock.c
@@ -59,7 +59,8 @@ static int kvmclock_post_load(void *opaque, int version_id)
     return kvm_vm_ioctl(kvm_state, KVM_SET_CLOCK, &data);
 }
 
-static void kvmclock_vm_state_change(void *opaque, int running, int reason)
+static void kvmclock_vm_state_change(void *opaque, int running,
+                                     RunState state)
 {
     KVMClockState *s = opaque;
 
diff --git a/hw/qxl.c b/hw/qxl.c
index a282d23..6db2f1a 100644
--- a/hw/qxl.c
+++ b/hw/qxl.c
@@ -1453,10 +1453,11 @@ static void qxl_hw_text_update(void *opaque, console_ch_t *chardata)
     }
 }
 
-static void qxl_vm_change_state_handler(void *opaque, int running, int reason)
+static void qxl_vm_change_state_handler(void *opaque, int running,
+                                        RunState state)
 {
     PCIQXLDevice *qxl = opaque;
-    qemu_spice_vm_change_state_handler(&qxl->ssd, running, reason);
+    qemu_spice_vm_change_state_handler(&qxl->ssd, running, state);
 
     if (running) {
         /*
diff --git a/hw/scsi-disk.c b/hw/scsi-disk.c
index 9724d0f..fc66256 100644
--- a/hw/scsi-disk.c
+++ b/hw/scsi-disk.c
@@ -211,7 +211,7 @@ static int scsi_handle_rw_error(SCSIDiskReq *r, int error, int type)
         r->status |= SCSI_REQ_STATUS_RETRY | type;
 
         bdrv_mon_event(s->bs, BDRV_ACTION_STOP, is_read);
-        vm_stop(VMSTOP_DISKFULL);
+        vm_stop(RSTATE_IO_ERROR);
     } else {
         switch (error) {
         case ENOMEM:
@@ -329,7 +329,7 @@ static void scsi_dma_restart_bh(void *opaque)
     }
 }
 
-static void scsi_dma_restart_cb(void *opaque, int running, int reason)
+static void scsi_dma_restart_cb(void *opaque, int running, RunState state)
 {
     SCSIDiskState *s = opaque;
 
diff --git a/hw/virtio-blk.c b/hw/virtio-blk.c
index 4df23f4..6e59a92 100644
--- a/hw/virtio-blk.c
+++ b/hw/virtio-blk.c
@@ -77,7 +77,7 @@ static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
         req->next = s->rq;
         s->rq = req;
         bdrv_mon_event(s->bs, BDRV_ACTION_STOP, is_read);
-        vm_stop(VMSTOP_DISKFULL);
+        vm_stop(RSTATE_IO_ERROR);
     } else {
         virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
         bdrv_acct_done(s->bs, &req->acct);
@@ -439,7 +439,8 @@ static void virtio_blk_dma_restart_bh(void *opaque)
     virtio_submit_multiwrite(s->bs, &mrb);
 }
 
-static void virtio_blk_dma_restart_cb(void *opaque, int running, int reason)
+static void virtio_blk_dma_restart_cb(void *opaque, int running,
+                                      RunState state)
 {
     VirtIOBlock *s = opaque;
 
diff --git a/hw/virtio.c b/hw/virtio.c
index 13aa0fa..74ab79e 100644
--- a/hw/virtio.c
+++ b/hw/virtio.c
@@ -837,7 +837,7 @@ void virtio_cleanup(VirtIODevice *vdev)
     g_free(vdev);
 }
 
-static void virtio_vmstate_change(void *opaque, int running, int reason)
+static void virtio_vmstate_change(void *opaque, int running, RunState state)
 {
     VirtIODevice *vdev = opaque;
     bool backend_run = running && (vdev->status & VIRTIO_CONFIG_S_DRIVER_OK);
diff --git a/hw/watchdog.c b/hw/watchdog.c
index 1c900a1..71c6c7d 100644
--- a/hw/watchdog.c
+++ b/hw/watchdog.c
@@ -132,7 +132,7 @@ void watchdog_perform_action(void)
 
     case WDT_PAUSE:             /* same as 'stop' command in monitor */
         watchdog_mon_event("pause");
-        vm_stop(VMSTOP_WATCHDOG);
+        vm_stop(RSTATE_WATCHDOG);
         break;
 
     case WDT_DEBUG:
diff --git a/kvm-all.c b/kvm-all.c
index fbb9ff3..228655b 100644
--- a/kvm-all.c
+++ b/kvm-all.c
@@ -1014,7 +1014,7 @@ int kvm_cpu_exec(CPUState *env)
 
     if (ret < 0) {
         cpu_dump_state(env, stderr, fprintf, CPU_DUMP_CODE);
-        vm_stop(VMSTOP_PANIC);
+        vm_stop(RSTATE_PANICKED);
     }
 
     env->exit_request = 0;
diff --git a/migration.c b/migration.c
index f5959b4..29f1a76 100644
--- a/migration.c
+++ b/migration.c
@@ -374,7 +374,7 @@ void migrate_fd_put_ready(void *opaque)
         int old_vm_running = vm_running;
 
         DPRINTF("done iterating\n");
-        vm_stop(VMSTOP_MIGRATE);
+        vm_stop(RSTATE_PRE_MIGRATE);
 
         if ((qemu_savevm_state_complete(s->mon, s->file)) < 0) {
             if (old_vm_running) {
diff --git a/monitor.c b/monitor.c
index df0f622..3f979ad 100644
--- a/monitor.c
+++ b/monitor.c
@@ -1293,7 +1293,7 @@ static void do_singlestep(Monitor *mon, const QDict *qdict)
  */
 static int do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
 {
-    vm_stop(VMSTOP_USER);
+    vm_stop(RSTATE_PAUSED);
     return 0;
 }
 
@@ -2828,7 +2828,7 @@ static void do_loadvm(Monitor *mon, const QDict *qdict)
     int saved_vm_running  = vm_running;
     const char *name = qdict_get_str(qdict, "name");
 
-    vm_stop(VMSTOP_LOADVM);
+    vm_stop(RSTATE_RESTORE);
 
     if (load_vmstate(name) == 0 && saved_vm_running) {
         vm_start();
diff --git a/qemu-timer.c b/qemu-timer.c
index 46dd483..b582786 100644
--- a/qemu-timer.c
+++ b/qemu-timer.c
@@ -1116,7 +1116,8 @@ static void win32_rearm_timer(struct qemu_alarm_timer *t)
 
 #endif /* _WIN32 */
 
-static void alarm_timer_on_change_state_rearm(void *opaque, int running, int reason)
+static void alarm_timer_on_change_state_rearm(void *opaque, int running,
+                                              RunState state)
 {
     if (running)
         qemu_rearm_alarm_timer((struct qemu_alarm_timer *) opaque);
diff --git a/savevm.c b/savevm.c
index 1feaa70..f1f7735 100644
--- a/savevm.c
+++ b/savevm.c
@@ -1603,7 +1603,7 @@ static int qemu_savevm_state(Monitor *mon, QEMUFile *f)
     int ret;
 
     saved_vm_running = vm_running;
-    vm_stop(VMSTOP_SAVEVM);
+    vm_stop(RSTATE_SAVEVM);
 
     if (qemu_savevm_state_blocked(mon)) {
         ret = -EINVAL;
@@ -1932,7 +1932,7 @@ void do_savevm(Monitor *mon, const QDict *qdict)
     }
 
     saved_vm_running = vm_running;
-    vm_stop(VMSTOP_SAVEVM);
+    vm_stop(RSTATE_SAVEVM);
 
     memset(sn, 0, sizeof(*sn));
 
diff --git a/sysemu.h b/sysemu.h
index eb66af5..a3846ca 100644
--- a/sysemu.h
+++ b/sysemu.h
@@ -9,6 +9,20 @@
 #include "notify.h"
 
 /* vl.c */
+
+typedef enum {
+    RSTATE_DEBUG,          /* qemu is running under gdb */
+    RSTATE_PANICKED,       /* paused due to an internal error */
+    RSTATE_IO_ERROR,       /* paused due to an I/O error */
+    RSTATE_PAUSED,         /* paused by the user (ie. the 'stop' command) */
+    RSTATE_PRE_MIGRATE,    /* paused preparing to finish migrate */
+    RSTATE_RESTORE,        /* paused restoring the VM state */
+    RSTATE_RUNNING,        /* qemu is running */
+    RSTATE_SAVEVM,         /* paused saving VM state */
+    RSTATE_SHUTDOWN,       /* guest shut down and -no-shutdown is in use */
+    RSTATE_WATCHDOG        /* watchdog fired and qemu is configured to pause */
+} RunState;
+
 extern const char *bios_name;
 
 extern int vm_running;
@@ -18,34 +32,24 @@ int qemu_uuid_parse(const char *str, uint8_t *uuid);
 #define UUID_FMT "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
 
 typedef struct vm_change_state_entry VMChangeStateEntry;
-typedef void VMChangeStateHandler(void *opaque, int running, int reason);
+typedef void VMChangeStateHandler(void *opaque, int running, RunState state);
 
 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
                                                      void *opaque);
 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e);
-void vm_state_notify(int running, int reason);
-
-#define VMSTOP_USER      0
-#define VMSTOP_DEBUG     1
-#define VMSTOP_SHUTDOWN  2
-#define VMSTOP_DISKFULL  3
-#define VMSTOP_WATCHDOG  4
-#define VMSTOP_PANIC     5
-#define VMSTOP_SAVEVM    6
-#define VMSTOP_LOADVM    7
-#define VMSTOP_MIGRATE   8
+void vm_state_notify(int running, RunState state);
 
 #define VMRESET_SILENT   false
 #define VMRESET_REPORT   true
 
 void vm_start(void);
-void vm_stop(int reason);
+void vm_stop(RunState state);
 
 void qemu_system_reset_request(void);
 void qemu_system_shutdown_request(void);
 void qemu_system_powerdown_request(void);
 void qemu_system_debug_request(void);
-void qemu_system_vmstop_request(int reason);
+void qemu_system_vmstop_request(RunState reason);
 int qemu_shutdown_requested_get(void);
 int qemu_reset_requested_get(void);
 int qemu_shutdown_requested(void);
diff --git a/target-i386/kvm.c b/target-i386/kvm.c
index 70ef74b..bb13ece 100644
--- a/target-i386/kvm.c
+++ b/target-i386/kvm.c
@@ -334,7 +334,7 @@ static int kvm_inject_mce_oldstyle(CPUState *env)
     return 0;
 }
 
-static void cpu_update_state(void *opaque, int running, int reason)
+static void cpu_update_state(void *opaque, int running, RunState state)
 {
     CPUState *env = opaque;
 
diff --git a/ui/spice-display.c b/ui/spice-display.c
index e385361..3a92a12 100644
--- a/ui/spice-display.c
+++ b/ui/spice-display.c
@@ -255,7 +255,8 @@ void qemu_spice_destroy_host_primary(SimpleSpiceDisplay *ssd)
     qemu_spice_destroy_primary_surface(ssd, 0, QXL_SYNC);
 }
 
-void qemu_spice_vm_change_state_handler(void *opaque, int running, int reason)
+void qemu_spice_vm_change_state_handler(void *opaque, int running,
+                                        RunState state)
 {
     SimpleSpiceDisplay *ssd = opaque;
 
diff --git a/ui/spice-display.h b/ui/spice-display.h
index 1388641..5e52df9 100644
--- a/ui/spice-display.h
+++ b/ui/spice-display.h
@@ -22,6 +22,7 @@
 #include "qemu-thread.h"
 #include "console.h"
 #include "pflib.h"
+#include "sysemu.h"
 
 #define NUM_MEMSLOTS 8
 #define MEMSLOT_GENERATION_BITS 8
@@ -88,7 +89,8 @@ void qemu_spice_destroy_update(SimpleSpiceDisplay *sdpy, SimpleSpiceUpdate *upda
 void qemu_spice_create_host_memslot(SimpleSpiceDisplay *ssd);
 void qemu_spice_create_host_primary(SimpleSpiceDisplay *ssd);
 void qemu_spice_destroy_host_primary(SimpleSpiceDisplay *ssd);
-void qemu_spice_vm_change_state_handler(void *opaque, int running, int reason);
+void qemu_spice_vm_change_state_handler(void *opaque, int running,
+                                        RunState state);
 void qemu_spice_display_init_common(SimpleSpiceDisplay *ssd, DisplayState *ds);
 
 void qemu_spice_display_update(SimpleSpiceDisplay *ssd,
diff --git a/vl.c b/vl.c
index 5ba9b35..ca7d3c4 100644
--- a/vl.c
+++ b/vl.c
@@ -1145,14 +1145,14 @@ void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
     g_free (e);
 }
 
-void vm_state_notify(int running, int reason)
+void vm_state_notify(int running, RunState state)
 {
     VMChangeStateEntry *e;
 
-    trace_vm_state_notify(running, reason);
+    trace_vm_state_notify(running, state);
 
     for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
-        e->cb(e->opaque, running, reason);
+        e->cb(e->opaque, running, state);
     }
 }
 
@@ -1161,7 +1161,7 @@ void vm_start(void)
     if (!vm_running) {
         cpu_enable_ticks();
         vm_running = 1;
-        vm_state_notify(1, 0);
+        vm_state_notify(1, RSTATE_RUNNING);
         resume_all_vcpus();
         monitor_protocol_event(QEVENT_RESUME, NULL);
     }
@@ -1182,7 +1182,7 @@ static int shutdown_requested, shutdown_signal = -1;
 static pid_t shutdown_pid;
 static int powerdown_requested;
 static int debug_requested;
-static int vmstop_requested;
+static RunState vmstop_requested = RSTATE_NO_STATE;
 
 int qemu_shutdown_requested_get(void)
 {
@@ -1238,11 +1238,11 @@ static int qemu_debug_requested(void)
     return r;
 }
 
-static int qemu_vmstop_requested(void)
+static RunState qemu_vmstop_requested(void)
 {
-    int r = vmstop_requested;
-    vmstop_requested = 0;
-    return r;
+    RunState s = vmstop_requested;
+    vmstop_requested = RSTATE_NO_STATE;
+    return s;
 }
 
 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
@@ -1317,9 +1317,9 @@ void qemu_system_debug_request(void)
     qemu_notify_event();
 }
 
-void qemu_system_vmstop_request(int reason)
+void qemu_system_vmstop_request(RunState state)
 {
-    vmstop_requested = reason;
+    vmstop_requested = state;
     qemu_notify_event();
 }
 
@@ -1469,13 +1469,13 @@ static void main_loop(void)
 #endif
 
         if (qemu_debug_requested()) {
-            vm_stop(VMSTOP_DEBUG);
+            vm_stop(RSTATE_DEBUG);
         }
         if (qemu_shutdown_requested()) {
             qemu_kill_report();
             monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
             if (no_shutdown) {
-                vm_stop(VMSTOP_SHUTDOWN);
+                vm_stop(RSTATE_SHUTDOWN);
             } else
                 break;
         }
diff --git a/xen-all.c b/xen-all.c
index 84420d8..24aeb83 100644
--- a/xen-all.c
+++ b/xen-all.c
@@ -846,7 +846,8 @@ static void xen_main_loop_prepare(XenIOState *state)
 
 /* Initialise Xen */
 
-static void xen_change_state_handler(void *opaque, int running, int reason)
+static void xen_change_state_handler(void *opaque, int running,
+                                     RunState state)
 {
     if (running) {
         /* record state running */
@@ -854,7 +855,8 @@ static void xen_change_state_handler(void *opaque, int running, int reason)
     }
 }
 
-static void xen_hvm_change_state_handler(void *opaque, int running, int reason)
+static void xen_hvm_change_state_handler(void *opaque, int running,
+                                         RunState state)
 {
     XenIOState *state = opaque;
     if (running) {
-- 
1.7.7.rc0.72.g4b5ea

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [Qemu-devel] [PATCH 3/9] RunState: Add additional states
  2011-09-09 20:25 [Qemu-devel] [PATCH v5 0/9]: Introduce the RunState type Luiz Capitulino
  2011-09-09 20:25 ` [Qemu-devel] [PATCH 1/9] Move vm_state_notify() prototype from cpus.h to sysemu.h Luiz Capitulino
  2011-09-09 20:25 ` [Qemu-devel] [PATCH 2/9] Replace the VMSTOP macros with a proper state type Luiz Capitulino
@ 2011-09-09 20:25 ` Luiz Capitulino
  2011-09-09 20:25 ` [Qemu-devel] [PATCH 4/9] runstate_set(): Check for valid transitions Luiz Capitulino
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 13+ messages in thread
From: Luiz Capitulino @ 2011-09-09 20:25 UTC (permalink / raw)
  To: qemu-devel; +Cc: kwolf, aliguori, jan.kiszka, armbru, amit.shah, vilanova

Currently, only vm_start() and vm_stop() change the VM state.
That's, the state is only changed when starting or stopping the VM.

This commit adds the runstate_set() function, which makes it possible
to also do state transitions when the VM is stopped or running.

Additional states are also added and the current state is stored.

Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
---
 cpus.c      |    1 +
 migration.c |    8 +++++++-
 sysemu.h    |    9 ++++++++-
 vl.c        |   20 ++++++++++++++++++++
 4 files changed, 36 insertions(+), 2 deletions(-)

diff --git a/cpus.c b/cpus.c
index 76a79ac..3444bc0 100644
--- a/cpus.c
+++ b/cpus.c
@@ -124,6 +124,7 @@ static void do_vm_stop(RunState state)
         cpu_disable_ticks();
         vm_running = 0;
         pause_all_vcpus();
+        runstate_set(state);
         vm_state_notify(0, state);
         qemu_aio_flush();
         bdrv_flush_all();
diff --git a/migration.c b/migration.c
index 29f1a76..f2499cf 100644
--- a/migration.c
+++ b/migration.c
@@ -72,8 +72,11 @@ void process_incoming_migration(QEMUFile *f)
 
     incoming_expected = false;
 
-    if (autostart)
+    if (autostart) {
         vm_start();
+    } else {
+        runstate_set(RSTATE_PRE_LAUNCH);
+    }
 }
 
 int do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
@@ -390,6 +393,9 @@ void migrate_fd_put_ready(void *opaque)
             }
             state = MIG_STATE_ERROR;
         }
+        if (state == MIG_STATE_COMPLETED) {
+            runstate_set(RSTATE_POST_MIGRATE);
+        }
         s->state = state;
         notifier_list_notify(&migration_state_notifiers, NULL);
     }
diff --git a/sysemu.h b/sysemu.h
index a3846ca..19088aa 100644
--- a/sysemu.h
+++ b/sysemu.h
@@ -11,16 +11,21 @@
 /* vl.c */
 
 typedef enum {
+    RSTATE_NO_STATE,
     RSTATE_DEBUG,          /* qemu is running under gdb */
+    RSTATE_IN_MIGRATE,     /* paused waiting for an incoming migration */
     RSTATE_PANICKED,       /* paused due to an internal error */
     RSTATE_IO_ERROR,       /* paused due to an I/O error */
     RSTATE_PAUSED,         /* paused by the user (ie. the 'stop' command) */
+    RSTATE_POST_MIGRATE,   /* paused following a successful migration */
+    RSTATE_PRE_LAUNCH,     /* qemu was started with -S and haven't started */
     RSTATE_PRE_MIGRATE,    /* paused preparing to finish migrate */
     RSTATE_RESTORE,        /* paused restoring the VM state */
     RSTATE_RUNNING,        /* qemu is running */
     RSTATE_SAVEVM,         /* paused saving VM state */
     RSTATE_SHUTDOWN,       /* guest shut down and -no-shutdown is in use */
-    RSTATE_WATCHDOG        /* watchdog fired and qemu is configured to pause */
+    RSTATE_WATCHDOG,       /* watchdog fired and qemu is configured to pause */
+    RSTATE_MAX
 } RunState;
 
 extern const char *bios_name;
@@ -31,6 +36,8 @@ extern uint8_t qemu_uuid[];
 int qemu_uuid_parse(const char *str, uint8_t *uuid);
 #define UUID_FMT "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
 
+bool runstate_check(RunState state);
+void runstate_set(RunState new_state);
 typedef struct vm_change_state_entry VMChangeStateEntry;
 typedef void VMChangeStateHandler(void *opaque, int running, RunState state);
 
diff --git a/vl.c b/vl.c
index ca7d3c4..9926d2a 100644
--- a/vl.c
+++ b/vl.c
@@ -323,6 +323,22 @@ static int default_driver_check(QemuOpts *opts, void *opaque)
 }
 
 /***********************************************************/
+/* QEMU state */
+
+static RunState current_run_state = RSTATE_NO_STATE;
+
+bool runstate_check(RunState state)
+{
+    return current_run_state == state;
+}
+
+void runstate_set(RunState new_state)
+{
+    assert(new_state < RSTATE_MAX);
+    current_run_state = new_state;
+}
+
+/***********************************************************/
 /* real time host monotonic timer */
 
 /***********************************************************/
@@ -1161,6 +1177,7 @@ void vm_start(void)
     if (!vm_running) {
         cpu_enable_ticks();
         vm_running = 1;
+        runstate_set(RSTATE_RUNNING);
         vm_state_notify(1, RSTATE_RUNNING);
         resume_all_vcpus();
         monitor_protocol_event(QEVENT_RESUME, NULL);
@@ -3437,6 +3454,7 @@ int main(int argc, char **argv, char **envp)
     }
 
     if (incoming) {
+        runstate_set(RSTATE_IN_MIGRATE);
         int ret = qemu_start_incoming_migration(incoming);
         if (ret < 0) {
             fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
@@ -3445,6 +3463,8 @@ int main(int argc, char **argv, char **envp)
         }
     } else if (autostart) {
         vm_start();
+    } else {
+        runstate_set(RSTATE_PRE_LAUNCH);
     }
 
     os_setup_post();
-- 
1.7.7.rc0.72.g4b5ea

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [Qemu-devel] [PATCH 4/9] runstate_set(): Check for valid transitions
  2011-09-09 20:25 [Qemu-devel] [PATCH v5 0/9]: Introduce the RunState type Luiz Capitulino
                   ` (2 preceding siblings ...)
  2011-09-09 20:25 ` [Qemu-devel] [PATCH 3/9] RunState: Add additional states Luiz Capitulino
@ 2011-09-09 20:25 ` Luiz Capitulino
  2011-09-14  3:06   ` Luiz Capitulino
  2011-09-09 20:25 ` [Qemu-devel] [PATCH 5/9] Drop the incoming_expected global variable Luiz Capitulino
                   ` (4 subsequent siblings)
  8 siblings, 1 reply; 13+ messages in thread
From: Luiz Capitulino @ 2011-09-09 20:25 UTC (permalink / raw)
  To: qemu-devel; +Cc: kwolf, aliguori, jan.kiszka, armbru, amit.shah, vilanova

This commit could have been folded with the previous one, however
doing it separately will allow for easy bisect and revert if needed.

Checking and testing all valid transitions wasn't trivial, chances
are this will need broader testing to become more stable.

This is a transition table as suggested by Lluís Vilanova.

Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
---
 sysemu.h |    1 +
 vl.c     |   74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 74 insertions(+), 1 deletions(-)

diff --git a/sysemu.h b/sysemu.h
index 19088aa..a01ddac 100644
--- a/sysemu.h
+++ b/sysemu.h
@@ -36,6 +36,7 @@ extern uint8_t qemu_uuid[];
 int qemu_uuid_parse(const char *str, uint8_t *uuid);
 #define UUID_FMT "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
 
+void runstate_init(void);
 bool runstate_check(RunState state);
 void runstate_set(RunState new_state);
 typedef struct vm_change_state_entry VMChangeStateEntry;
diff --git a/vl.c b/vl.c
index 9926d2a..4a8edc7 100644
--- a/vl.c
+++ b/vl.c
@@ -327,14 +327,84 @@ static int default_driver_check(QemuOpts *opts, void *opaque)
 
 static RunState current_run_state = RSTATE_NO_STATE;
 
+typedef struct {
+    RunState from;
+    RunState to;
+} RunStateTransition;
+
+static const RunStateTransition runstate_transitions_def[] = {
+    /*     from      ->     to      */
+    { RSTATE_NO_STATE, RSTATE_RUNNING },
+    { RSTATE_NO_STATE, RSTATE_IN_MIGRATE },
+    { RSTATE_NO_STATE, RSTATE_PRE_LAUNCH },
+
+    { RSTATE_DEBUG, RSTATE_RUNNING },
+
+    { RSTATE_IN_MIGRATE, RSTATE_RUNNING },
+    { RSTATE_IN_MIGRATE, RSTATE_PRE_LAUNCH },
+
+    { RSTATE_PANICKED, RSTATE_PAUSED },
+
+    { RSTATE_IO_ERROR, RSTATE_RUNNING },
+
+    { RSTATE_PAUSED, RSTATE_RUNNING },
+
+    { RSTATE_POST_MIGRATE, RSTATE_RUNNING },
+
+    { RSTATE_PRE_LAUNCH, RSTATE_RUNNING },
+    { RSTATE_PRE_LAUNCH, RSTATE_POST_MIGRATE },
+
+    { RSTATE_PRE_MIGRATE, RSTATE_RUNNING },
+    { RSTATE_PRE_MIGRATE, RSTATE_POST_MIGRATE },
+
+    { RSTATE_RESTORE, RSTATE_RUNNING },
+
+    { RSTATE_RUNNING, RSTATE_DEBUG },
+    { RSTATE_RUNNING, RSTATE_PANICKED },
+    { RSTATE_RUNNING, RSTATE_IO_ERROR },
+    { RSTATE_RUNNING, RSTATE_PAUSED },
+    { RSTATE_RUNNING, RSTATE_PRE_MIGRATE },
+    { RSTATE_RUNNING, RSTATE_RESTORE },
+    { RSTATE_RUNNING, RSTATE_SAVEVM },
+    { RSTATE_RUNNING, RSTATE_SHUTDOWN },
+    { RSTATE_RUNNING, RSTATE_WATCHDOG },
+
+    { RSTATE_SAVEVM, RSTATE_RUNNING },
+
+    { RSTATE_SHUTDOWN, RSTATE_PAUSED },
+
+    { RSTATE_WATCHDOG, RSTATE_RUNNING },
+
+    { RSTATE_MAX, RSTATE_MAX },
+};
+
+static bool runstate_valid_transitions[RSTATE_MAX][RSTATE_MAX];
+
 bool runstate_check(RunState state)
 {
     return current_run_state == state;
 }
 
+void runstate_init(void)
+{
+    const RunStateTransition *p;
+
+    memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
+
+    for (p = &runstate_transitions_def[0]; p->from != RSTATE_MAX; p++) {
+        runstate_valid_transitions[p->from][p->to] = true;
+    }
+}
+
+/* This function will abort() on invalid state transitions */
 void runstate_set(RunState new_state)
 {
-    assert(new_state < RSTATE_MAX);
+    if (new_state >= RSTATE_MAX ||
+        !runstate_valid_transitions[current_run_state][new_state]) {
+        fprintf(stderr, "invalid runstate transition\n");
+        abort();
+    }
+
     current_run_state = new_state;
 }
 
@@ -2218,6 +2288,8 @@ int main(int argc, char **argv, char **envp)
 
     g_mem_set_vtable(&mem_trace);
 
+    runstate_init();
+
     init_clocks();
 
     qemu_cache_utils_init(envp);
-- 
1.7.7.rc0.72.g4b5ea

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [Qemu-devel] [PATCH 5/9] Drop the incoming_expected global variable
  2011-09-09 20:25 [Qemu-devel] [PATCH v5 0/9]: Introduce the RunState type Luiz Capitulino
                   ` (3 preceding siblings ...)
  2011-09-09 20:25 ` [Qemu-devel] [PATCH 4/9] runstate_set(): Check for valid transitions Luiz Capitulino
@ 2011-09-09 20:25 ` Luiz Capitulino
  2011-09-09 20:25 ` [Qemu-devel] [PATCH 6/9] Drop the vm_running " Luiz Capitulino
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 13+ messages in thread
From: Luiz Capitulino @ 2011-09-09 20:25 UTC (permalink / raw)
  To: qemu-devel; +Cc: kwolf, aliguori, jan.kiszka, armbru, amit.shah, vilanova

Test against RSTATE_IN_MIGRATE instead.

Please, note that the RSTATE_IN_MIGRATE state is only set when all the
initial VM setup is done, while 'incoming_expected' was set right in
the beginning when parsing command-line options. Shouldn't be a problem
as far as I could check.

Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
---
 migration.c |    2 --
 monitor.c   |    2 +-
 vl.c        |    2 --
 3 files changed, 1 insertions(+), 5 deletions(-)

diff --git a/migration.c b/migration.c
index f2499cf..a63e2a2 100644
--- a/migration.c
+++ b/migration.c
@@ -70,8 +70,6 @@ void process_incoming_migration(QEMUFile *f)
     qemu_announce_self();
     DPRINTF("successfully loaded vm state\n");
 
-    incoming_expected = false;
-
     if (autostart) {
         vm_start();
     } else {
diff --git a/monitor.c b/monitor.c
index 3f979ad..afe858c 100644
--- a/monitor.c
+++ b/monitor.c
@@ -1311,7 +1311,7 @@ static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
 {
     struct bdrv_iterate_context context = { mon, 0 };
 
-    if (incoming_expected) {
+    if (runstate_check(RSTATE_IN_MIGRATE)) {
         qerror_report(QERR_MIGRATION_EXPECTED);
         return -1;
     }
diff --git a/vl.c b/vl.c
index 4a8edc7..3709c33 100644
--- a/vl.c
+++ b/vl.c
@@ -187,7 +187,6 @@ int nb_nics;
 NICInfo nd_table[MAX_NICS];
 int vm_running;
 int autostart;
-int incoming_expected; /* Started with -incoming and waiting for incoming */
 static int rtc_utc = 1;
 static int rtc_date_offset = -1; /* -1 means no change */
 QEMUClock *rtc_clock;
@@ -3040,7 +3039,6 @@ int main(int argc, char **argv, char **envp)
                 break;
             case QEMU_OPTION_incoming:
                 incoming = optarg;
-                incoming_expected = true;
                 break;
             case QEMU_OPTION_nodefaults:
                 default_serial = 0;
-- 
1.7.7.rc0.72.g4b5ea

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [Qemu-devel] [PATCH 6/9] Drop the vm_running global variable
  2011-09-09 20:25 [Qemu-devel] [PATCH v5 0/9]: Introduce the RunState type Luiz Capitulino
                   ` (4 preceding siblings ...)
  2011-09-09 20:25 ` [Qemu-devel] [PATCH 5/9] Drop the incoming_expected global variable Luiz Capitulino
@ 2011-09-09 20:25 ` Luiz Capitulino
  2011-09-09 20:25 ` [Qemu-devel] [PATCH 7/9] Monitor/QMP: Don't allow cont on bad VM state Luiz Capitulino
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 13+ messages in thread
From: Luiz Capitulino @ 2011-09-09 20:25 UTC (permalink / raw)
  To: qemu-devel; +Cc: kwolf, aliguori, jan.kiszka, armbru, amit.shah, vilanova

Use runstate_is_running() instead, which is introduced by this commit.

Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
---
 cpus.c            |    9 ++++-----
 gdbstub.c         |    4 ++--
 hw/etraxfs_dma.c  |    2 +-
 hw/kvmclock.c     |    2 +-
 hw/virtio.c       |    2 +-
 migration.c       |    2 +-
 monitor.c         |    4 ++--
 qemu-timer.c      |    6 +++---
 savevm.c          |    4 ++--
 sysemu.h          |    2 +-
 target-i386/kvm.c |    2 +-
 ui/sdl.c          |    6 +++---
 vl.c              |    9 ++++++---
 xen-all.c         |    2 +-
 14 files changed, 29 insertions(+), 27 deletions(-)

diff --git a/cpus.c b/cpus.c
index 3444bc0..883c27a 100644
--- a/cpus.c
+++ b/cpus.c
@@ -115,14 +115,13 @@ void cpu_synchronize_all_post_init(void)
 
 int cpu_is_stopped(CPUState *env)
 {
-    return !vm_running || env->stopped;
+    return !runstate_is_running() || env->stopped;
 }
 
 static void do_vm_stop(RunState state)
 {
-    if (vm_running) {
+    if (runstate_is_running()) {
         cpu_disable_ticks();
-        vm_running = 0;
         pause_all_vcpus();
         runstate_set(state);
         vm_state_notify(0, state);
@@ -137,7 +136,7 @@ static int cpu_can_run(CPUState *env)
     if (env->stop) {
         return 0;
     }
-    if (env->stopped || !vm_running) {
+    if (env->stopped || !runstate_is_running()) {
         return 0;
     }
     return 1;
@@ -148,7 +147,7 @@ static bool cpu_thread_is_idle(CPUState *env)
     if (env->stop || env->queued_work_first) {
         return false;
     }
-    if (env->stopped || !vm_running) {
+    if (env->stopped || !runstate_is_running()) {
         return true;
     }
     if (!env->halted || qemu_cpu_has_work(env) ||
diff --git a/gdbstub.c b/gdbstub.c
index a308a76..ceb39cc 100644
--- a/gdbstub.c
+++ b/gdbstub.c
@@ -2428,7 +2428,7 @@ static void gdb_read_byte(GDBState *s, int ch)
         if (ch != '$')
             return;
     }
-    if (vm_running) {
+    if (runstate_is_running()) {
         /* when the CPU is running, we cannot do anything except stop
            it when receiving a char */
         vm_stop(RSTATE_PAUSED);
@@ -2733,7 +2733,7 @@ static int gdb_monitor_write(CharDriverState *chr, const uint8_t *buf, int len)
 #ifndef _WIN32
 static void gdb_sigterm_handler(int signal)
 {
-    if (vm_running) {
+    if (runstate_is_running()) {
         vm_stop(RSTATE_PAUSED);
     }
 }
diff --git a/hw/etraxfs_dma.c b/hw/etraxfs_dma.c
index e8ad9e6..d3082ac 100644
--- a/hw/etraxfs_dma.c
+++ b/hw/etraxfs_dma.c
@@ -732,7 +732,7 @@ static void DMA_run(void *opaque)
     struct fs_dma_ctrl *etraxfs_dmac = opaque;
     int p = 1;
 
-    if (vm_running)
+    if (runstate_is_running())
         p = etraxfs_dmac_run(etraxfs_dmac);
 
     if (p)
diff --git a/hw/kvmclock.c b/hw/kvmclock.c
index 88961be..5388bc4 100644
--- a/hw/kvmclock.c
+++ b/hw/kvmclock.c
@@ -46,7 +46,7 @@ static void kvmclock_pre_save(void *opaque)
      * it on next vmsave (which would return a different value). Will be reset
      * when the VM is continued.
      */
-    s->clock_valid = !vm_running;
+    s->clock_valid = !runstate_is_running();
 }
 
 static int kvmclock_post_load(void *opaque, int version_id)
diff --git a/hw/virtio.c b/hw/virtio.c
index 74ab79e..c577bbe 100644
--- a/hw/virtio.c
+++ b/hw/virtio.c
@@ -870,7 +870,7 @@ VirtIODevice *virtio_common_init(const char *name, uint16_t device_id,
     vdev->queue_sel = 0;
     vdev->config_vector = VIRTIO_NO_VECTOR;
     vdev->vq = g_malloc0(sizeof(VirtQueue) * VIRTIO_PCI_QUEUE_MAX);
-    vdev->vm_running = vm_running;
+    vdev->vm_running = runstate_is_running();
     for(i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
         vdev->vq[i].vector = VIRTIO_NO_VECTOR;
         vdev->vq[i].vdev = vdev;
diff --git a/migration.c b/migration.c
index a63e2a2..7dd8f4e 100644
--- a/migration.c
+++ b/migration.c
@@ -372,7 +372,7 @@ void migrate_fd_put_ready(void *opaque)
     DPRINTF("iterate\n");
     if (qemu_savevm_state_iterate(s->mon, s->file) == 1) {
         int state;
-        int old_vm_running = vm_running;
+        int old_vm_running = runstate_is_running();
 
         DPRINTF("done iterating\n");
         vm_stop(RSTATE_PRE_MIGRATE);
diff --git a/monitor.c b/monitor.c
index afe858c..10cab1d 100644
--- a/monitor.c
+++ b/monitor.c
@@ -2632,7 +2632,7 @@ static void do_info_status_print(Monitor *mon, const QObject *data)
 static void do_info_status(Monitor *mon, QObject **ret_data)
 {
     *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
-                                    vm_running, singlestep);
+                                    runstate_is_running(), singlestep);
 }
 
 static qemu_acl *find_acl(Monitor *mon, const char *name)
@@ -2825,7 +2825,7 @@ static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
 
 static void do_loadvm(Monitor *mon, const QDict *qdict)
 {
-    int saved_vm_running  = vm_running;
+    int saved_vm_running  = runstate_is_running();
     const char *name = qdict_get_str(qdict, "name");
 
     vm_stop(RSTATE_RESTORE);
diff --git a/qemu-timer.c b/qemu-timer.c
index b582786..ad1fc8b 100644
--- a/qemu-timer.c
+++ b/qemu-timer.c
@@ -230,7 +230,7 @@ static void icount_adjust(void)
     int64_t delta;
     static int64_t last_delta;
     /* If the VM is not running, then do nothing.  */
-    if (!vm_running)
+    if (!runstate_is_running())
         return;
 
     cur_time = cpu_get_clock();
@@ -388,7 +388,7 @@ static void icount_warp_rt(void *opaque)
         return;
     }
 
-    if (vm_running) {
+    if (runstate_is_running()) {
         int64_t clock = qemu_get_clock_ns(rt_clock);
         int64_t warp_delta = clock - vm_clock_warp_start;
         if (use_icount == 1) {
@@ -710,7 +710,7 @@ void qemu_run_all_timers(void)
     }
 
     /* vm time timers */
-    if (vm_running) {
+    if (runstate_is_running()) {
         qemu_run_timers(vm_clock);
     }
 
diff --git a/savevm.c b/savevm.c
index f1f7735..46f2447 100644
--- a/savevm.c
+++ b/savevm.c
@@ -1602,7 +1602,7 @@ static int qemu_savevm_state(Monitor *mon, QEMUFile *f)
     int saved_vm_running;
     int ret;
 
-    saved_vm_running = vm_running;
+    saved_vm_running = runstate_is_running();
     vm_stop(RSTATE_SAVEVM);
 
     if (qemu_savevm_state_blocked(mon)) {
@@ -1931,7 +1931,7 @@ void do_savevm(Monitor *mon, const QDict *qdict)
         return;
     }
 
-    saved_vm_running = vm_running;
+    saved_vm_running = runstate_is_running();
     vm_stop(RSTATE_SAVEVM);
 
     memset(sn, 0, sizeof(*sn));
diff --git a/sysemu.h b/sysemu.h
index a01ddac..9a2c531 100644
--- a/sysemu.h
+++ b/sysemu.h
@@ -30,7 +30,6 @@ typedef enum {
 
 extern const char *bios_name;
 
-extern int vm_running;
 extern const char *qemu_name;
 extern uint8_t qemu_uuid[];
 int qemu_uuid_parse(const char *str, uint8_t *uuid);
@@ -39,6 +38,7 @@ int qemu_uuid_parse(const char *str, uint8_t *uuid);
 void runstate_init(void);
 bool runstate_check(RunState state);
 void runstate_set(RunState new_state);
+int runstate_is_running(void);
 typedef struct vm_change_state_entry VMChangeStateEntry;
 typedef void VMChangeStateHandler(void *opaque, int running, RunState state);
 
diff --git a/target-i386/kvm.c b/target-i386/kvm.c
index bb13ece..0dee2e2 100644
--- a/target-i386/kvm.c
+++ b/target-i386/kvm.c
@@ -1130,7 +1130,7 @@ static int kvm_get_msrs(CPUState *env)
 
     if (!env->tsc_valid) {
         msrs[n++].index = MSR_IA32_TSC;
-        env->tsc_valid = !vm_running;
+        env->tsc_valid = !runstate_is_running();
     }
 
 #ifdef TARGET_X86_64
diff --git a/ui/sdl.c b/ui/sdl.c
index c7aaedf..8cafc44 100644
--- a/ui/sdl.c
+++ b/ui/sdl.c
@@ -409,7 +409,7 @@ static void sdl_update_caption(void)
     char icon_title[1024];
     const char *status = "";
 
-    if (!vm_running)
+    if (!runstate_is_running())
         status = " [Stopped]";
     else if (gui_grab) {
         if (alt_grab)
@@ -853,8 +853,8 @@ static void sdl_refresh(DisplayState *ds)
 {
     SDL_Event ev1, *ev = &ev1;
 
-    if (last_vm_running != vm_running) {
-        last_vm_running = vm_running;
+    if (last_vm_running != runstate_is_running()) {
+        last_vm_running = runstate_is_running();
         sdl_update_caption();
     }
 
diff --git a/vl.c b/vl.c
index 3709c33..370228c 100644
--- a/vl.c
+++ b/vl.c
@@ -185,7 +185,6 @@ int mem_prealloc = 0; /* force preallocation of physical target memory */
 #endif
 int nb_nics;
 NICInfo nd_table[MAX_NICS];
-int vm_running;
 int autostart;
 static int rtc_utc = 1;
 static int rtc_date_offset = -1; /* -1 means no change */
@@ -407,6 +406,11 @@ void runstate_set(RunState new_state)
     current_run_state = new_state;
 }
 
+int runstate_is_running(void)
+{
+    return runstate_check(RSTATE_RUNNING);
+}
+
 /***********************************************************/
 /* real time host monotonic timer */
 
@@ -1243,9 +1247,8 @@ void vm_state_notify(int running, RunState state)
 
 void vm_start(void)
 {
-    if (!vm_running) {
+    if (!runstate_is_running()) {
         cpu_enable_ticks();
-        vm_running = 1;
         runstate_set(RSTATE_RUNNING);
         vm_state_notify(1, RSTATE_RUNNING);
         resume_all_vcpus();
diff --git a/xen-all.c b/xen-all.c
index 24aeb83..8bf48c2 100644
--- a/xen-all.c
+++ b/xen-all.c
@@ -736,7 +736,7 @@ static void cpu_handle_ioreq(void *opaque)
          * guest resumes and does a hlt with interrupts disabled which
          * causes Xen to powerdown the domain.
          */
-        if (vm_running) {
+        if (runstate_is_running()) {
             if (qemu_shutdown_requested_get()) {
                 destroy_hvm_domain();
             }
-- 
1.7.7.rc0.72.g4b5ea

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [Qemu-devel] [PATCH 7/9] Monitor/QMP: Don't allow cont on bad VM state
  2011-09-09 20:25 [Qemu-devel] [PATCH v5 0/9]: Introduce the RunState type Luiz Capitulino
                   ` (5 preceding siblings ...)
  2011-09-09 20:25 ` [Qemu-devel] [PATCH 6/9] Drop the vm_running " Luiz Capitulino
@ 2011-09-09 20:25 ` Luiz Capitulino
  2011-09-09 20:25 ` [Qemu-devel] [PATCH 8/9] QMP: query-status: Introduce 'status' key Luiz Capitulino
  2011-09-09 20:25 ` [Qemu-devel] [PATCH 9/9] HMP: info status: Print the VM state Luiz Capitulino
  8 siblings, 0 replies; 13+ messages in thread
From: Luiz Capitulino @ 2011-09-09 20:25 UTC (permalink / raw)
  To: qemu-devel; +Cc: kwolf, aliguori, jan.kiszka, armbru, amit.shah, vilanova

We have two states where issuing cont before system_reset can
cause problems: RSTATE_SHUTDOWN (when -no-shutdown is used) and
RSTATE_PANICKED (which only happens with kvm).

This commit fixes that by doing the following when state is
RSTATE_SHUTDOWN or RSTATE_PANICKED:

 1. returning an error to the user/client if cont is issued
 2. automatically transition to RSTATE_PAUSED during system_reset

Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
---
 monitor.c |    5 +++++
 qerror.c  |    4 ++++
 qerror.h  |    3 +++
 vl.c      |    4 ++++
 4 files changed, 16 insertions(+), 0 deletions(-)

diff --git a/monitor.c b/monitor.c
index 10cab1d..6e5bb56 100644
--- a/monitor.c
+++ b/monitor.c
@@ -1314,7 +1314,12 @@ static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
     if (runstate_check(RSTATE_IN_MIGRATE)) {
         qerror_report(QERR_MIGRATION_EXPECTED);
         return -1;
+    } else if (runstate_check(RSTATE_PANICKED) ||
+               runstate_check(RSTATE_SHUTDOWN)) {
+        qerror_report(QERR_RESET_REQUIRED);
+        return -1;
     }
+
     bdrv_iterate(encrypted_bdrv_it, &context);
     /* only resume the vm if all keys are set and valid */
     if (!context.err) {
diff --git a/qerror.c b/qerror.c
index 3d64b80..c591a54 100644
--- a/qerror.c
+++ b/qerror.c
@@ -194,6 +194,10 @@ static const QErrorStringTable qerror_table[] = {
         .desc      = "QMP input object member '%(member)' is unexpected",
     },
     {
+        .error_fmt = QERR_RESET_REQUIRED,
+        .desc      = "Resetting the Virtual Machine is required",
+    },
+    {
         .error_fmt = QERR_SET_PASSWD_FAILED,
         .desc      = "Could not set password",
     },
diff --git a/qerror.h b/qerror.h
index 8058456..d407001 100644
--- a/qerror.h
+++ b/qerror.h
@@ -163,6 +163,9 @@ QError *qobject_to_qerror(const QObject *obj);
 #define QERR_QMP_EXTRA_MEMBER \
     "{ 'class': 'QMPExtraInputObjectMember', 'data': { 'member': %s } }"
 
+#define QERR_RESET_REQUIRED \
+    "{ 'class': 'ResetRequired', 'data': {} }"
+
 #define QERR_SET_PASSWD_FAILED \
     "{ 'class': 'SetPasswdFailed', 'data': {} }"
 
diff --git a/vl.c b/vl.c
index 370228c..e0b1f85 100644
--- a/vl.c
+++ b/vl.c
@@ -1573,6 +1573,10 @@ static void main_loop(void)
             cpu_synchronize_all_states();
             qemu_system_reset(VMRESET_REPORT);
             resume_all_vcpus();
+            if (runstate_check(RSTATE_PANICKED) ||
+                runstate_check(RSTATE_SHUTDOWN)) {
+                runstate_set(RSTATE_PAUSED);
+            }
         }
         if (qemu_powerdown_requested()) {
             monitor_protocol_event(QEVENT_POWERDOWN, NULL);
-- 
1.7.7.rc0.72.g4b5ea

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [Qemu-devel] [PATCH 8/9] QMP: query-status: Introduce 'status' key
  2011-09-09 20:25 [Qemu-devel] [PATCH v5 0/9]: Introduce the RunState type Luiz Capitulino
                   ` (6 preceding siblings ...)
  2011-09-09 20:25 ` [Qemu-devel] [PATCH 7/9] Monitor/QMP: Don't allow cont on bad VM state Luiz Capitulino
@ 2011-09-09 20:25 ` Luiz Capitulino
  2011-09-09 20:25 ` [Qemu-devel] [PATCH 9/9] HMP: info status: Print the VM state Luiz Capitulino
  8 siblings, 0 replies; 13+ messages in thread
From: Luiz Capitulino @ 2011-09-09 20:25 UTC (permalink / raw)
  To: qemu-devel; +Cc: kwolf, aliguori, jan.kiszka, armbru, amit.shah, vilanova

This new key reports the current VM status to clients. Please, check
the documentation being added in this commit for more details.

Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
---
 monitor.c       |    3 +--
 qmp-commands.hx |   19 ++++++++++++++++++-
 sysemu.h        |    1 +
 vl.c            |   23 +++++++++++++++++++++++
 4 files changed, 43 insertions(+), 3 deletions(-)

diff --git a/monitor.c b/monitor.c
index 6e5bb56..7684ba9 100644
--- a/monitor.c
+++ b/monitor.c
@@ -2636,8 +2636,7 @@ static void do_info_status_print(Monitor *mon, const QObject *data)
 
 static void do_info_status(Monitor *mon, QObject **ret_data)
 {
-    *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
-                                    runstate_is_running(), singlestep);
+    *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i, 'status': %s }", runstate_is_running(), singlestep, runstate_as_string());
 }
 
 static qemu_acl *find_acl(Monitor *mon, const char *name)
diff --git a/qmp-commands.hx b/qmp-commands.hx
index 27cc66e..0cf1321 100644
--- a/qmp-commands.hx
+++ b/qmp-commands.hx
@@ -1571,11 +1571,28 @@ Return a json-object with the following information:
 - "running": true if the VM is running, or false if it is paused (json-bool)
 - "singlestep": true if the VM is in single step mode,
                 false otherwise (json-bool)
+- "status": one of the following values (json-string)
+    "debug" - QEMU is running on a debugger
+    "inmigrate" - guest is paused waiting for an incoming migration
+    "internal-error" - An internal error that prevents further guest
+    execution has occurred
+    "io-error" - the last IOP has failed and the device is configured
+    to pause on I/O errors
+    "paused" - guest has been paused via the 'stop' command
+    "postmigrate" - guest is paused following a successful 'migrate'
+    "prelaunch" - QEMU was started with -S and guest has not started
+    "finish-migrate" - guest is paused to finish the migration process
+    "restore-vm" - guest is paused to restore VM state
+    "running" - guest is actively running
+    "save-vm" - guest is paused to save the VM state
+    "shutdown" - guest is shut down (and -no-shutdown is in use)
+    "watchdog" - the watchdog action is configured to pause and
+     has been triggered
 
 Example:
 
 -> { "execute": "query-status" }
-<- { "return": { "running": true, "singlestep": false } }
+<- { "return": { "running": true, "singlestep": false, "status": "running" } }
 
 EQMP
 
diff --git a/sysemu.h b/sysemu.h
index 9a2c531..43ff546 100644
--- a/sysemu.h
+++ b/sysemu.h
@@ -39,6 +39,7 @@ void runstate_init(void);
 bool runstate_check(RunState state);
 void runstate_set(RunState new_state);
 int runstate_is_running(void);
+const char *runstate_as_string(void);
 typedef struct vm_change_state_entry VMChangeStateEntry;
 typedef void VMChangeStateHandler(void *opaque, int running, RunState state);
 
diff --git a/vl.c b/vl.c
index e0b1f85..6ebc101 100644
--- a/vl.c
+++ b/vl.c
@@ -378,6 +378,22 @@ static const RunStateTransition runstate_transitions_def[] = {
 
 static bool runstate_valid_transitions[RSTATE_MAX][RSTATE_MAX];
 
+static const char *const runstate_name_tbl[RSTATE_MAX] = {
+    [RSTATE_DEBUG] = "debug",
+    [RSTATE_IN_MIGRATE] = "incoming-migration",
+    [RSTATE_PANICKED] = "internal-error",
+    [RSTATE_IO_ERROR] = "io-error",
+    [RSTATE_PAUSED] = "paused",
+    [RSTATE_POST_MIGRATE] = "post-migrate",
+    [RSTATE_PRE_LAUNCH] = "prelaunch",
+    [RSTATE_PRE_MIGRATE] = "finish-migrate",
+    [RSTATE_RESTORE] = "restore-vm",
+    [RSTATE_RUNNING] = "running",
+    [RSTATE_SAVEVM] = "save-vm",
+    [RSTATE_SHUTDOWN] = "shutdown",
+    [RSTATE_WATCHDOG] = "watchdog",
+};
+
 bool runstate_check(RunState state)
 {
     return current_run_state == state;
@@ -406,6 +422,13 @@ void runstate_set(RunState new_state)
     current_run_state = new_state;
 }
 
+const char *runstate_as_string(void)
+{
+    assert(current_run_state > RSTATE_NO_STATE &&
+           current_run_state < RSTATE_MAX);
+    return runstate_name_tbl[current_run_state];
+}
+
 int runstate_is_running(void)
 {
     return runstate_check(RSTATE_RUNNING);
-- 
1.7.7.rc0.72.g4b5ea

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [Qemu-devel] [PATCH 9/9] HMP: info status: Print the VM state
  2011-09-09 20:25 [Qemu-devel] [PATCH v5 0/9]: Introduce the RunState type Luiz Capitulino
                   ` (7 preceding siblings ...)
  2011-09-09 20:25 ` [Qemu-devel] [PATCH 8/9] QMP: query-status: Introduce 'status' key Luiz Capitulino
@ 2011-09-09 20:25 ` Luiz Capitulino
  8 siblings, 0 replies; 13+ messages in thread
From: Luiz Capitulino @ 2011-09-09 20:25 UTC (permalink / raw)
  To: qemu-devel; +Cc: kwolf, aliguori, jan.kiszka, armbru, amit.shah, vilanova

Today our printf format for the "info status" command is:

  VM status: %s

Where the string can be "running", "running (single step mode)" or
"paused".

This commit extends it to:

 VM status: %s (%s)

The second string corresponds to the "status" field as returned
by the query-status QMP command and it's only printed if "status"
is not "running" or "paused".

Example:

 VM status: paused (shutdown)

PS: libvirt uses "info status" when using HMP, but the new format
    should not break it.

Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
---
 monitor.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)

diff --git a/monitor.c b/monitor.c
index 7684ba9..860530b 100644
--- a/monitor.c
+++ b/monitor.c
@@ -2618,6 +2618,7 @@ static int do_inject_nmi(Monitor *mon, const QDict *qdict, QObject **ret_data)
 static void do_info_status_print(Monitor *mon, const QObject *data)
 {
     QDict *qdict;
+    const char *status;
 
     qdict = qobject_to_qdict(data);
 
@@ -2631,6 +2632,11 @@ static void do_info_status_print(Monitor *mon, const QObject *data)
         monitor_printf(mon, "paused");
     }
 
+    status = qdict_get_str(qdict, "status");
+    if (strcmp(status, "paused") && strcmp(status, "running")) {
+        monitor_printf(mon, " (%s)", status);
+    }
+
     monitor_printf(mon, "\n");
 }
 
-- 
1.7.7.rc0.72.g4b5ea

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [Qemu-devel] [PATCH 4/9] runstate_set(): Check for valid transitions
  2011-09-09 20:25 ` [Qemu-devel] [PATCH 4/9] runstate_set(): Check for valid transitions Luiz Capitulino
@ 2011-09-14  3:06   ` Luiz Capitulino
  2011-09-14 19:55     ` Blue Swirl
  0 siblings, 1 reply; 13+ messages in thread
From: Luiz Capitulino @ 2011-09-14  3:06 UTC (permalink / raw)
  To: Luiz Capitulino
  Cc: kwolf, aliguori, jan.kiszka, qemu-devel, armbru, amit.shah, vilanova

On Fri,  9 Sep 2011 17:25:41 -0300
Luiz Capitulino <lcapitulino@redhat.com> wrote:

> This commit could have been folded with the previous one, however
> doing it separately will allow for easy bisect and revert if needed.
> 
> Checking and testing all valid transitions wasn't trivial, chances
> are this will need broader testing to become more stable.
> 
> This is a transition table as suggested by Lluís Vilanova.
> 
> Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>

Would be nice to get a reviewed-by for this patch, as it wasn't trivial
to get it right and tested...

> ---
>  sysemu.h |    1 +
>  vl.c     |   74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
>  2 files changed, 74 insertions(+), 1 deletions(-)
> 
> diff --git a/sysemu.h b/sysemu.h
> index 19088aa..a01ddac 100644
> --- a/sysemu.h
> +++ b/sysemu.h
> @@ -36,6 +36,7 @@ extern uint8_t qemu_uuid[];
>  int qemu_uuid_parse(const char *str, uint8_t *uuid);
>  #define UUID_FMT "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
>  
> +void runstate_init(void);
>  bool runstate_check(RunState state);
>  void runstate_set(RunState new_state);
>  typedef struct vm_change_state_entry VMChangeStateEntry;
> diff --git a/vl.c b/vl.c
> index 9926d2a..4a8edc7 100644
> --- a/vl.c
> +++ b/vl.c
> @@ -327,14 +327,84 @@ static int default_driver_check(QemuOpts *opts, void *opaque)
>  
>  static RunState current_run_state = RSTATE_NO_STATE;
>  
> +typedef struct {
> +    RunState from;
> +    RunState to;
> +} RunStateTransition;
> +
> +static const RunStateTransition runstate_transitions_def[] = {
> +    /*     from      ->     to      */
> +    { RSTATE_NO_STATE, RSTATE_RUNNING },
> +    { RSTATE_NO_STATE, RSTATE_IN_MIGRATE },
> +    { RSTATE_NO_STATE, RSTATE_PRE_LAUNCH },
> +
> +    { RSTATE_DEBUG, RSTATE_RUNNING },
> +
> +    { RSTATE_IN_MIGRATE, RSTATE_RUNNING },
> +    { RSTATE_IN_MIGRATE, RSTATE_PRE_LAUNCH },
> +
> +    { RSTATE_PANICKED, RSTATE_PAUSED },
> +
> +    { RSTATE_IO_ERROR, RSTATE_RUNNING },
> +
> +    { RSTATE_PAUSED, RSTATE_RUNNING },
> +
> +    { RSTATE_POST_MIGRATE, RSTATE_RUNNING },
> +
> +    { RSTATE_PRE_LAUNCH, RSTATE_RUNNING },
> +    { RSTATE_PRE_LAUNCH, RSTATE_POST_MIGRATE },
> +
> +    { RSTATE_PRE_MIGRATE, RSTATE_RUNNING },
> +    { RSTATE_PRE_MIGRATE, RSTATE_POST_MIGRATE },
> +
> +    { RSTATE_RESTORE, RSTATE_RUNNING },
> +
> +    { RSTATE_RUNNING, RSTATE_DEBUG },
> +    { RSTATE_RUNNING, RSTATE_PANICKED },
> +    { RSTATE_RUNNING, RSTATE_IO_ERROR },
> +    { RSTATE_RUNNING, RSTATE_PAUSED },
> +    { RSTATE_RUNNING, RSTATE_PRE_MIGRATE },
> +    { RSTATE_RUNNING, RSTATE_RESTORE },
> +    { RSTATE_RUNNING, RSTATE_SAVEVM },
> +    { RSTATE_RUNNING, RSTATE_SHUTDOWN },
> +    { RSTATE_RUNNING, RSTATE_WATCHDOG },
> +
> +    { RSTATE_SAVEVM, RSTATE_RUNNING },
> +
> +    { RSTATE_SHUTDOWN, RSTATE_PAUSED },
> +
> +    { RSTATE_WATCHDOG, RSTATE_RUNNING },
> +
> +    { RSTATE_MAX, RSTATE_MAX },
> +};
> +
> +static bool runstate_valid_transitions[RSTATE_MAX][RSTATE_MAX];
> +
>  bool runstate_check(RunState state)
>  {
>      return current_run_state == state;
>  }
>  
> +void runstate_init(void)
> +{
> +    const RunStateTransition *p;
> +
> +    memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
> +
> +    for (p = &runstate_transitions_def[0]; p->from != RSTATE_MAX; p++) {
> +        runstate_valid_transitions[p->from][p->to] = true;
> +    }
> +}
> +
> +/* This function will abort() on invalid state transitions */
>  void runstate_set(RunState new_state)
>  {
> -    assert(new_state < RSTATE_MAX);
> +    if (new_state >= RSTATE_MAX ||
> +        !runstate_valid_transitions[current_run_state][new_state]) {
> +        fprintf(stderr, "invalid runstate transition\n");
> +        abort();
> +    }
> +
>      current_run_state = new_state;
>  }
>  
> @@ -2218,6 +2288,8 @@ int main(int argc, char **argv, char **envp)
>  
>      g_mem_set_vtable(&mem_trace);
>  
> +    runstate_init();
> +
>      init_clocks();
>  
>      qemu_cache_utils_init(envp);

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Qemu-devel] [PATCH 4/9] runstate_set(): Check for valid transitions
  2011-09-14  3:06   ` Luiz Capitulino
@ 2011-09-14 19:55     ` Blue Swirl
  2011-09-14 20:23       ` Luiz Capitulino
  0 siblings, 1 reply; 13+ messages in thread
From: Blue Swirl @ 2011-09-14 19:55 UTC (permalink / raw)
  To: Luiz Capitulino
  Cc: kwolf, aliguori, jan.kiszka, qemu-devel, armbru, amit.shah, vilanova

On Wed, Sep 14, 2011 at 3:06 AM, Luiz Capitulino <lcapitulino@redhat.com> wrote:
> On Fri,  9 Sep 2011 17:25:41 -0300
> Luiz Capitulino <lcapitulino@redhat.com> wrote:
>
>> This commit could have been folded with the previous one, however
>> doing it separately will allow for easy bisect and revert if needed.
>>
>> Checking and testing all valid transitions wasn't trivial, chances
>> are this will need broader testing to become more stable.
>>
>> This is a transition table as suggested by Lluís Vilanova.
>>
>> Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
>
> Would be nice to get a reviewed-by for this patch, as it wasn't trivial
> to get it right and tested...
>
>> ---
>>  sysemu.h |    1 +
>>  vl.c     |   74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
>>  2 files changed, 74 insertions(+), 1 deletions(-)
>>
>> diff --git a/sysemu.h b/sysemu.h
>> index 19088aa..a01ddac 100644
>> --- a/sysemu.h
>> +++ b/sysemu.h
>> @@ -36,6 +36,7 @@ extern uint8_t qemu_uuid[];
>>  int qemu_uuid_parse(const char *str, uint8_t *uuid);
>>  #define UUID_FMT "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
>>
>> +void runstate_init(void);
>>  bool runstate_check(RunState state);
>>  void runstate_set(RunState new_state);
>>  typedef struct vm_change_state_entry VMChangeStateEntry;
>> diff --git a/vl.c b/vl.c
>> index 9926d2a..4a8edc7 100644
>> --- a/vl.c
>> +++ b/vl.c
>> @@ -327,14 +327,84 @@ static int default_driver_check(QemuOpts *opts, void *opaque)
>>
>>  static RunState current_run_state = RSTATE_NO_STATE;
>>
>> +typedef struct {
>> +    RunState from;
>> +    RunState to;
>> +} RunStateTransition;
>> +
>> +static const RunStateTransition runstate_transitions_def[] = {
>> +    /*     from      ->     to      */
>> +    { RSTATE_NO_STATE, RSTATE_RUNNING },
>> +    { RSTATE_NO_STATE, RSTATE_IN_MIGRATE },
>> +    { RSTATE_NO_STATE, RSTATE_PRE_LAUNCH },
>> +
>> +    { RSTATE_DEBUG, RSTATE_RUNNING },
>> +
>> +    { RSTATE_IN_MIGRATE, RSTATE_RUNNING },
>> +    { RSTATE_IN_MIGRATE, RSTATE_PRE_LAUNCH },
>> +
>> +    { RSTATE_PANICKED, RSTATE_PAUSED },
>> +
>> +    { RSTATE_IO_ERROR, RSTATE_RUNNING },
>> +
>> +    { RSTATE_PAUSED, RSTATE_RUNNING },
>> +
>> +    { RSTATE_POST_MIGRATE, RSTATE_RUNNING },
>> +
>> +    { RSTATE_PRE_LAUNCH, RSTATE_RUNNING },
>> +    { RSTATE_PRE_LAUNCH, RSTATE_POST_MIGRATE },
>> +
>> +    { RSTATE_PRE_MIGRATE, RSTATE_RUNNING },
>> +    { RSTATE_PRE_MIGRATE, RSTATE_POST_MIGRATE },
>> +
>> +    { RSTATE_RESTORE, RSTATE_RUNNING },
>> +
>> +    { RSTATE_RUNNING, RSTATE_DEBUG },
>> +    { RSTATE_RUNNING, RSTATE_PANICKED },
>> +    { RSTATE_RUNNING, RSTATE_IO_ERROR },
>> +    { RSTATE_RUNNING, RSTATE_PAUSED },
>> +    { RSTATE_RUNNING, RSTATE_PRE_MIGRATE },
>> +    { RSTATE_RUNNING, RSTATE_RESTORE },
>> +    { RSTATE_RUNNING, RSTATE_SAVEVM },
>> +    { RSTATE_RUNNING, RSTATE_SHUTDOWN },
>> +    { RSTATE_RUNNING, RSTATE_WATCHDOG },
>> +
>> +    { RSTATE_SAVEVM, RSTATE_RUNNING },
>> +
>> +    { RSTATE_SHUTDOWN, RSTATE_PAUSED },
>> +
>> +    { RSTATE_WATCHDOG, RSTATE_RUNNING },
>> +
>> +    { RSTATE_MAX, RSTATE_MAX },

I don't see a state which requires a system_reset to recover, which I
think was the original motivation. Do I miss something?

Otherwise the patch looks fine.

>> +};
>> +
>> +static bool runstate_valid_transitions[RSTATE_MAX][RSTATE_MAX];
>> +
>>  bool runstate_check(RunState state)
>>  {
>>      return current_run_state == state;
>>  }
>>
>> +void runstate_init(void)
>> +{
>> +    const RunStateTransition *p;
>> +
>> +    memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
>> +
>> +    for (p = &runstate_transitions_def[0]; p->from != RSTATE_MAX; p++) {
>> +        runstate_valid_transitions[p->from][p->to] = true;
>> +    }
>> +}
>> +
>> +/* This function will abort() on invalid state transitions */
>>  void runstate_set(RunState new_state)
>>  {
>> -    assert(new_state < RSTATE_MAX);
>> +    if (new_state >= RSTATE_MAX ||
>> +        !runstate_valid_transitions[current_run_state][new_state]) {
>> +        fprintf(stderr, "invalid runstate transition\n");
>> +        abort();
>> +    }
>> +
>>      current_run_state = new_state;
>>  }
>>
>> @@ -2218,6 +2288,8 @@ int main(int argc, char **argv, char **envp)
>>
>>      g_mem_set_vtable(&mem_trace);
>>
>> +    runstate_init();
>> +
>>      init_clocks();
>>
>>      qemu_cache_utils_init(envp);
>
>
>

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Qemu-devel] [PATCH 4/9] runstate_set(): Check for valid transitions
  2011-09-14 19:55     ` Blue Swirl
@ 2011-09-14 20:23       ` Luiz Capitulino
  0 siblings, 0 replies; 13+ messages in thread
From: Luiz Capitulino @ 2011-09-14 20:23 UTC (permalink / raw)
  To: Blue Swirl
  Cc: kwolf, aliguori, jan.kiszka, qemu-devel, armbru, amit.shah, vilanova

On Wed, 14 Sep 2011 19:55:25 +0000
Blue Swirl <blauwirbel@gmail.com> wrote:

> On Wed, Sep 14, 2011 at 3:06 AM, Luiz Capitulino <lcapitulino@redhat.com> wrote:
> > On Fri,  9 Sep 2011 17:25:41 -0300
> > Luiz Capitulino <lcapitulino@redhat.com> wrote:
> >
> >> This commit could have been folded with the previous one, however
> >> doing it separately will allow for easy bisect and revert if needed.
> >>
> >> Checking and testing all valid transitions wasn't trivial, chances
> >> are this will need broader testing to become more stable.
> >>
> >> This is a transition table as suggested by Lluís Vilanova.
> >>
> >> Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
> >
> > Would be nice to get a reviewed-by for this patch, as it wasn't trivial
> > to get it right and tested...
> >
> >> ---
> >>  sysemu.h |    1 +
> >>  vl.c     |   74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
> >>  2 files changed, 74 insertions(+), 1 deletions(-)
> >>
> >> diff --git a/sysemu.h b/sysemu.h
> >> index 19088aa..a01ddac 100644
> >> --- a/sysemu.h
> >> +++ b/sysemu.h
> >> @@ -36,6 +36,7 @@ extern uint8_t qemu_uuid[];
> >>  int qemu_uuid_parse(const char *str, uint8_t *uuid);
> >>  #define UUID_FMT "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
> >>
> >> +void runstate_init(void);
> >>  bool runstate_check(RunState state);
> >>  void runstate_set(RunState new_state);
> >>  typedef struct vm_change_state_entry VMChangeStateEntry;
> >> diff --git a/vl.c b/vl.c
> >> index 9926d2a..4a8edc7 100644
> >> --- a/vl.c
> >> +++ b/vl.c
> >> @@ -327,14 +327,84 @@ static int default_driver_check(QemuOpts *opts, void *opaque)
> >>
> >>  static RunState current_run_state = RSTATE_NO_STATE;
> >>
> >> +typedef struct {
> >> +    RunState from;
> >> +    RunState to;
> >> +} RunStateTransition;
> >> +
> >> +static const RunStateTransition runstate_transitions_def[] = {
> >> +    /*     from      ->     to      */
> >> +    { RSTATE_NO_STATE, RSTATE_RUNNING },
> >> +    { RSTATE_NO_STATE, RSTATE_IN_MIGRATE },
> >> +    { RSTATE_NO_STATE, RSTATE_PRE_LAUNCH },
> >> +
> >> +    { RSTATE_DEBUG, RSTATE_RUNNING },
> >> +
> >> +    { RSTATE_IN_MIGRATE, RSTATE_RUNNING },
> >> +    { RSTATE_IN_MIGRATE, RSTATE_PRE_LAUNCH },
> >> +
> >> +    { RSTATE_PANICKED, RSTATE_PAUSED },
> >> +
> >> +    { RSTATE_IO_ERROR, RSTATE_RUNNING },
> >> +
> >> +    { RSTATE_PAUSED, RSTATE_RUNNING },
> >> +
> >> +    { RSTATE_POST_MIGRATE, RSTATE_RUNNING },
> >> +
> >> +    { RSTATE_PRE_LAUNCH, RSTATE_RUNNING },
> >> +    { RSTATE_PRE_LAUNCH, RSTATE_POST_MIGRATE },
> >> +
> >> +    { RSTATE_PRE_MIGRATE, RSTATE_RUNNING },
> >> +    { RSTATE_PRE_MIGRATE, RSTATE_POST_MIGRATE },
> >> +
> >> +    { RSTATE_RESTORE, RSTATE_RUNNING },
> >> +
> >> +    { RSTATE_RUNNING, RSTATE_DEBUG },
> >> +    { RSTATE_RUNNING, RSTATE_PANICKED },
> >> +    { RSTATE_RUNNING, RSTATE_IO_ERROR },
> >> +    { RSTATE_RUNNING, RSTATE_PAUSED },
> >> +    { RSTATE_RUNNING, RSTATE_PRE_MIGRATE },
> >> +    { RSTATE_RUNNING, RSTATE_RESTORE },
> >> +    { RSTATE_RUNNING, RSTATE_SAVEVM },
> >> +    { RSTATE_RUNNING, RSTATE_SHUTDOWN },
> >> +    { RSTATE_RUNNING, RSTATE_WATCHDOG },
> >> +
> >> +    { RSTATE_SAVEVM, RSTATE_RUNNING },
> >> +
> >> +    { RSTATE_SHUTDOWN, RSTATE_PAUSED },
> >> +
> >> +    { RSTATE_WATCHDOG, RSTATE_RUNNING },
> >> +
> >> +    { RSTATE_MAX, RSTATE_MAX },
> 
> I don't see a state which requires a system_reset to recover, which I
> think was the original motivation. Do I miss something?

That's done by patch 7/9 Monitor/QMP: Don't allow cont on bad VM state.

> Otherwise the patch looks fine.

Thanks!

> 
> >> +};
> >> +
> >> +static bool runstate_valid_transitions[RSTATE_MAX][RSTATE_MAX];
> >> +
> >>  bool runstate_check(RunState state)
> >>  {
> >>      return current_run_state == state;
> >>  }
> >>
> >> +void runstate_init(void)
> >> +{
> >> +    const RunStateTransition *p;
> >> +
> >> +    memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
> >> +
> >> +    for (p = &runstate_transitions_def[0]; p->from != RSTATE_MAX; p++) {
> >> +        runstate_valid_transitions[p->from][p->to] = true;
> >> +    }
> >> +}
> >> +
> >> +/* This function will abort() on invalid state transitions */
> >>  void runstate_set(RunState new_state)
> >>  {
> >> -    assert(new_state < RSTATE_MAX);
> >> +    if (new_state >= RSTATE_MAX ||
> >> +        !runstate_valid_transitions[current_run_state][new_state]) {
> >> +        fprintf(stderr, "invalid runstate transition\n");
> >> +        abort();
> >> +    }
> >> +
> >>      current_run_state = new_state;
> >>  }
> >>
> >> @@ -2218,6 +2288,8 @@ int main(int argc, char **argv, char **envp)
> >>
> >>      g_mem_set_vtable(&mem_trace);
> >>
> >> +    runstate_init();
> >> +
> >>      init_clocks();
> >>
> >>      qemu_cache_utils_init(envp);
> >
> >
> >
> 

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2011-09-14 20:23 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-09-09 20:25 [Qemu-devel] [PATCH v5 0/9]: Introduce the RunState type Luiz Capitulino
2011-09-09 20:25 ` [Qemu-devel] [PATCH 1/9] Move vm_state_notify() prototype from cpus.h to sysemu.h Luiz Capitulino
2011-09-09 20:25 ` [Qemu-devel] [PATCH 2/9] Replace the VMSTOP macros with a proper state type Luiz Capitulino
2011-09-09 20:25 ` [Qemu-devel] [PATCH 3/9] RunState: Add additional states Luiz Capitulino
2011-09-09 20:25 ` [Qemu-devel] [PATCH 4/9] runstate_set(): Check for valid transitions Luiz Capitulino
2011-09-14  3:06   ` Luiz Capitulino
2011-09-14 19:55     ` Blue Swirl
2011-09-14 20:23       ` Luiz Capitulino
2011-09-09 20:25 ` [Qemu-devel] [PATCH 5/9] Drop the incoming_expected global variable Luiz Capitulino
2011-09-09 20:25 ` [Qemu-devel] [PATCH 6/9] Drop the vm_running " Luiz Capitulino
2011-09-09 20:25 ` [Qemu-devel] [PATCH 7/9] Monitor/QMP: Don't allow cont on bad VM state Luiz Capitulino
2011-09-09 20:25 ` [Qemu-devel] [PATCH 8/9] QMP: query-status: Introduce 'status' key Luiz Capitulino
2011-09-09 20:25 ` [Qemu-devel] [PATCH 9/9] HMP: info status: Print the VM state Luiz Capitulino

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.