Skip to content

Commit

Permalink
Replace the VMSTOP macros with a proper state type
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Luiz Capitulino committed Sep 15, 2011
1 parent bff046f commit 1dfb4dd
Show file tree
Hide file tree
Showing 24 changed files with 91 additions and 76 deletions.
2 changes: 1 addition & 1 deletion audio/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions cpus.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -878,18 +878,18 @@ 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.
*/
cpu_stop_current();
return;
}
do_vm_stop(reason);
do_vm_stop(state);
}

static int tcg_cpu_exec(CPUState *env)
Expand Down
30 changes: 15 additions & 15 deletions gdbstub.c
Original file line number Diff line number Diff line change
Expand Up @@ -2373,7 +2373,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;
Expand All @@ -2384,8 +2384,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:
Expand All @@ -2408,25 +2408,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:
Expand Down Expand Up @@ -2463,7 +2463,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);
Expand Down Expand Up @@ -2537,7 +2537,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
{
Expand Down Expand Up @@ -2799,7 +2799,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:
Expand Down Expand Up @@ -2840,7 +2840,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
Expand Down
2 changes: 1 addition & 1 deletion hw/ide/ahci.c
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,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)
{
}

Expand Down
4 changes: 2 additions & 2 deletions hw/ide/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,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);
Expand Down Expand Up @@ -1910,7 +1910,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)
{
}

Expand Down
3 changes: 2 additions & 1 deletion hw/ide/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <hw/ide.h>
#include "iorange.h"
#include "dma.h"
#include "sysemu.h"

/* debug IDE devices */
//#define DEBUG_IDE
Expand Down Expand Up @@ -387,7 +388,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;
Expand Down
2 changes: 1 addition & 1 deletion hw/ide/pci.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,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);
Expand Down
3 changes: 2 additions & 1 deletion hw/kvmclock.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
5 changes: 3 additions & 2 deletions hw/qxl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
/*
Expand Down
4 changes: 2 additions & 2 deletions hw/scsi-disk.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,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:
Expand Down Expand Up @@ -338,7 +338,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;

Expand Down
5 changes: 3 additions & 2 deletions hw/virtio-blk.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion hw/virtio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion hw/watchdog.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion kvm-all.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion migration.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion qemu-timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions savevm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));

Expand Down
33 changes: 19 additions & 14 deletions sysemu.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@
#include "notify.h"

/* vl.c */

typedef enum {
RSTATE_NO_STATE,
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;
Expand All @@ -18,34 +33,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);
Expand Down
Loading

0 comments on commit 1dfb4dd

Please sign in to comment.