Skip to contentSkip to conversation

save/restore on Xen

PATCH RESEND Superseded Stefano Stabellini 6 participants 17 messages Feb 28, 2012 – Mar 13, 2012

v5 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v5 cover save/restore on Xen

Hi all,
this is the fifth version of the Xen save/restore patch series. We have been discussing this issue for quite a while on #qemu and qemu-devel:

http://marc.info/?l=qemu-devel&m=132346828427314&w=2
http://marc.info/?l=qemu-devel&m=132377734605464&w=2

Please review the first three patches that touch generic code. This patch series have been lying around for quite a while and it is now blocking patches from getting in xen-unstable.

Changes in v5:

- rebased on b4bd0b168e9f4898b98308f4a8a089f647a86d16.

Changes in v4:

- following Anthony's suggestion I have introduced a new monitor command to save the non-ram device state to file;

- I have also removed the hack not to reset the cirrus videoram on restore, because it turns out that the videoram doesn't need to be reset in the reset handler at all (tested on Win2K, where the problem was found in the first place).

This is the list of patches with a diffstat:

Anthony PERARD (2):
xen mapcache: check if memory region has moved.
xen: do not allocate RAM during INMIGRATE runstate

Stefano Stabellini (4):
cirrus_vga: do not reset videoram
Introduce "save_devices"
xen: record physmap changes to xenstore
Set runstate to INMIGRATE earlier

 block-migration.c |    2 +-
 hmp-commands.hx   |   14 +++++++
 hw/cirrus_vga.c   |    4 --
 qmp-commands.hx   |   17 +++++++++
 savevm.c          |   72 ++++++++++++++++++++++++++++++++++++-
 sysemu.h          |    1 +
 vl.c              |    4 +-
 vmstate.h         |    1 +
 xen-all.c         |  104 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 xen-mapcache.c    |   22 ++++++++++--
 xen-mapcache.h    |    9 ++++-
 11 files changed, 235 insertions(+), 15 deletions(-)

git://xenbits.xen.org/people/sstabellini/qemu-dm.git saverestore-5

Cheers,

Stefano

#1 lore raw

PATCH RESEND v5 1/6 cirrus_vga: do not reset videoram

There is no need to set the videoram to 0xff in cirrus_reset, because it is the BIOS' job.

---
 hw/cirrus_vga.c |    4 ----
 1 files changed, 0 insertions(+), 4 deletions(-)
hw/cirrus_vga.c +0 -4
index 4edcb94..afedaa4 100644
@@ -2767,10 +2767,6 @@ static void cirrus_reset(void *opaque)
     }
     s->vga.cr[0x27] = s->device_id;
 
-    /* Win2K seems to assume that the pattern buffer is at 0xff
-       initially ! */
-    memset(s->vga.vram_ptr, 0xff, s->real_vram_size);
-
     s->cirrus_hidden_dac_lockindex = 5;
     s->cirrus_hidden_dac_data = 0;
 }
Signature · 1 line
1.7.2.5
Reviewed-by on v5 1/6
#3 lore raw

Re: [Qemu-devel] [PATCH RESEND v5 1/6] cirrus_vga: do not reset videoram

In reply to Stefano Stabellini
Quoted 3 lines · Stefano Stabellini

There is no need to set the videoram to 0xff in cirrus_reset, because it is the BIOS' job.

Signature · 1 line
error compiling committee.c: too many arguments to function
#4 lore raw

Re: [Qemu-devel] [PATCH RESEND v5 1/6] cirrus_vga: do not reset videoram

Quoted 6 lines · Avi Kivity
Quoted 3 lines · Stefano Stabellini

There is no need to set the videoram to 0xff in cirrus_reset, because it is the BIOS' job.

thanks!!

#5 lore raw

PATCH RESEND v5 2/6 Introduce "save_devices"

In reply to Stefano Stabellini

- add an "is_ram" flag to SaveStateEntry;

- add an "is_ram" parameter to register_savevm_live;

- introduce a "save_devices" monitor command that can be used to save the state of non-ram devices.

---
 block-migration.c |    2 +-
 hmp-commands.hx   |   14 ++++++++++
 qmp-commands.hx   |   17 ++++++++++++
 savevm.c          |   72 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 sysemu.h          |    1 +
 vl.c              |    2 +-
 vmstate.h         |    1 +
 7 files changed, 106 insertions(+), 3 deletions(-)
block-migration.c +1 -1
index 4467468..d283fd0 100644
@@ -722,6 +722,6 @@ void blk_mig_init(void)
     QSIMPLEQ_INIT(&block_mig_state.bmds_list);
     QSIMPLEQ_INIT(&block_mig_state.blk_list);
 
-    register_savevm_live(NULL, "block", 0, 1, block_set_params,
+    register_savevm_live(NULL, "block", 0, 1, 0, block_set_params,
                          block_save_live, NULL, block_load, &block_mig_state);
 }
hmp-commands.hx +14 -0
index 64b3656..873abc9 100644
@@ -280,6 +280,20 @@ a snapshot with the same tag or ID, it is replaced. More info at
 ETEXI
 
     {
+        .name       = "save_devices",
+        .args_type  = "filename:F",
+        .params     = "filename",
+        .help       = "save the state of non-ram devices.",
+        .mhandler.cmd_new = do_save_device_state,
+    },
+
+STEXI
+@item save_devices @var{filename}
+@findex save_devices
+Save the state of non-ram devices.
+ETEXI
+
+    {
         .name       = "loadvm",
         .args_type  = "name:s",
         .params     = "tag|id",
qmp-commands.hx +17 -0
index 705f704..619d9de 100644
@@ -444,6 +444,23 @@ Note: inject-nmi is only supported for x86 guest currently, it will
 EQMP
 
     {
+        .name       = "save_devices",
+        .args_type  = "filename:F",
+        .params     = "filename",
+        .help       = "save the state of non-ram devices.",
+        .user_print = monitor_user_noop,	
+    .mhandler.cmd_new = do_save_device_state,
+    },
+
+SQMP
+save_devices
+-------
+
+Save the state of non-ram devices.
+
+EQMP
+
+    {
         .name       = "migrate",
         .args_type  = "detach:-d,blk:-b,inc:-i,uri:s",
         .params     = "[-d] [-b] [-i] uri",
savevm.c +71 -1

108 lines not on this page · raw message

sysemu.h +1 -0
index 98118cc..f4d5bf4 100644
@@ -70,6 +70,7 @@ void qemu_remove_exit_notifier(Notifier *notify);
 void qemu_add_machine_init_done_notifier(Notifier *notify);
 
 void do_savevm(Monitor *mon, const QDict *qdict);
+int do_save_device_state(Monitor *mon, const QDict *qdict, QObject **ret_data);
 int load_vmstate(const char *name);
 void do_delvm(Monitor *mon, const QDict *qdict);
 void do_info_snapshots(Monitor *mon);
vl.c +1 -1
index 1d4c350..5b2b84c 100644
@@ -3393,7 +3393,7 @@ int main(int argc, char **argv, char **envp)
     default_drive(default_sdcard, snapshot, machine->use_scsi,
                   IF_SD, 0, SD_OPTS);
 
-    register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
+    register_savevm_live(NULL, "ram", 0, 4, 1, NULL, ram_save_live, NULL,
                          ram_load, NULL);
 
     if (nb_numa_nodes > 0) {
vmstate.h +1 -0
index 9d3c49c..3cef117 100644
@@ -44,6 +44,7 @@ int register_savevm_live(DeviceState *dev,
                          const char *idstr,
                          int instance_id,
                          int version_id,
+                         int is_ram,
                          SaveSetParamsHandler *set_params,
                          SaveLiveStateHandler *save_live_state,
                          SaveStateHandler *save_state,
Signature · 1 line
1.7.2.5
Acked-by on v5 2/6
#6 lore raw

Re: [Qemu-devel] [PATCH RESEND v5 2/6] Introduce "save_devices"

Quoted 231 lines · Stefano Stabellini

- add an "is_ram" flag to SaveStateEntry;

- add an "is_ram" parameter to register_savevm_live;

- introduce a "save_devices" monitor command that can be used to save the state of non-ram devices.

---
  block-migration.c |    2 +-
  hmp-commands.hx   |   14 ++++++++++
  qmp-commands.hx   |   17 ++++++++++++
  savevm.c          |   72 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
  sysemu.h          |    1 +
  vl.c              |    2 +-
  vmstate.h         |    1 +
  7 files changed, 106 insertions(+), 3 deletions(-)
block-migration.c +1 -1
index 4467468..d283fd0 100644
@@ -722,6 +722,6 @@ void blk_mig_init(void)
      QSIMPLEQ_INIT(&block_mig_state.bmds_list);
      QSIMPLEQ_INIT(&block_mig_state.blk_list);
 
-    register_savevm_live(NULL, "block", 0, 1, block_set_params,
+    register_savevm_live(NULL, "block", 0, 1, 0, block_set_params,
                           block_save_live, NULL, block_load,&block_mig_state);
  }
hmp-commands.hx +14 -0
index 64b3656..873abc9 100644
@@ -280,6 +280,20 @@ a snapshot with the same tag or ID, it is replaced. More info at
  ETEXI
 
      {
+        .name       = "save_devices",
+        .args_type  = "filename:F",
+        .params     = "filename",
+        .help       = "save the state of non-ram devices.",
+        .mhandler.cmd_new = do_save_device_state,
+    },
+
+STEXI
+@item save_devices @var{filename}
+@findex save_devices
+Save the state of non-ram devices.
+ETEXI
+
+    {
          .name       = "loadvm",
          .args_type  = "name:s",
          .params     = "tag|id",
qmp-commands.hx +17 -0
index 705f704..619d9de 100644
@@ -444,6 +444,23 @@ Note: inject-nmi is only supported for x86 guest currently, it will
  EQMP
 
      {
+        .name       = "save_devices",
+        .args_type  = "filename:F",
+        .params     = "filename",
+        .help       = "save the state of non-ram devices.",
+        .user_print = monitor_user_noop,	
+    .mhandler.cmd_new = do_save_device_state,
+    },
+
+SQMP
+save_devices
+-------
+
+Save the state of non-ram devices.
+
+EQMP
+
+    {
          .name       = "migrate",
          .args_type  = "detach:-d,blk:-b,inc:-i,uri:s",
          .params     = "[-d] [-b] [-i] uri",
savevm.c +71 -1
index 80be1ff..d0e62bb 100644
@@ -1177,6 +1177,7 @@ typedef struct SaveStateEntry {
      void *opaque;
      CompatEntry *compat;
      int no_migrate;
+    int is_ram;
  } SaveStateEntry;
 
 
@@ -1223,6 +1224,7 @@ int register_savevm_live(DeviceState *dev,
                           const char *idstr,
                           int instance_id,
                           int version_id,
+                         int is_ram,
                           SaveSetParamsHandler *set_params,
                           SaveLiveStateHandler *save_live_state,
                           SaveStateHandler *save_state,
@@ -1241,6 +1243,7 @@ int register_savevm_live(DeviceState *dev,
      se->opaque = opaque;
      se->vmsd = NULL;
      se->no_migrate = 0;
+    se->is_ram = is_ram;
 
      if (dev&&  dev->parent_bus&&  dev->parent_bus->info->get_dev_path) {
          char *id = dev->parent_bus->info->get_dev_path(dev);
@@ -1277,7 +1280,7 @@ int register_savevm(DeviceState *dev,
                      LoadStateHandler *load_state,
                      void *opaque)
  {
-    return register_savevm_live(dev, idstr, instance_id, version_id,
+    return register_savevm_live(dev, idstr, instance_id, version_id, 0,
                                  NULL, NULL, save_state, load_state, opaque);
  }
 
@@ -1728,6 +1731,43 @@ out:
      return ret;
  }
 
+static int qemu_save_device_state(Monitor *mon, QEMUFile *f)
+{
+    SaveStateEntry *se;
+
+    qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
+    qemu_put_be32(f, QEMU_VM_FILE_VERSION);
+
+    cpu_synchronize_all_states();
+
+    QTAILQ_FOREACH(se,&savevm_handlers, entry) {
+        int len;
+
+        if (se->is_ram)
+            continue;
+        if (se->save_state == NULL&&  se->vmsd == NULL)
+            continue;
+
+        /* Section type */
+        qemu_put_byte(f, QEMU_VM_SECTION_FULL);
+        qemu_put_be32(f, se->section_id);
+
+        /* ID string */
+        len = strlen(se->idstr);
+        qemu_put_byte(f, len);
+        qemu_put_buffer(f, (uint8_t *)se->idstr, len);
+
+        qemu_put_be32(f, se->instance_id);
+        qemu_put_be32(f, se->version_id);
+
+        vmstate_save(f, se);
+    }
+
+    qemu_put_byte(f, QEMU_VM_EOF);
+
+    return qemu_file_get_error(f);
+}
+
  static SaveStateEntry *find_se(const char *idstr, int instance_id)
  {
      SaveStateEntry *se;
@@ -2109,6 +2149,36 @@ void do_savevm(Monitor *mon, const QDict *qdict)
          vm_start();
  }
 
+int do_save_device_state(Monitor *mon, const QDict *qdict, QObject **ret_data)
+{
+    int ret;
+    QEMUFile *f;
+    int saved_vm_running;
+    const char *filename = qdict_get_try_str(qdict, "filename");
+
+    saved_vm_running = runstate_is_running();
+    vm_stop(RUN_STATE_SAVE_VM);
+
+    f = qemu_fopen(filename, "wb");
+    if (!f) {
+        monitor_printf(mon, "Could not open VM state file\n");
+        ret = -1;
+        goto the_end;
+    }
+    ret = qemu_save_device_state(mon, f);
+    qemu_fclose(f);
+    if (ret<  0) {
+        monitor_printf(mon, "Error %d while writing VM\n", ret);
+        goto the_end;
+    }
+    ret = 0;
+
+ the_end:
+    if (saved_vm_running)
+        vm_start();
+    return ret;
+}
+
  int load_vmstate(const char *name)
  {
      BlockDriverState *bs, *bs_vm_state;
sysemu.h +1 -0
index 98118cc..f4d5bf4 100644
@@ -70,6 +70,7 @@ void qemu_remove_exit_notifier(Notifier *notify);
  void qemu_add_machine_init_done_notifier(Notifier *notify);
 
  void do_savevm(Monitor *mon, const QDict *qdict);
+int do_save_device_state(Monitor *mon, const QDict *qdict, QObject **ret_data);
  int load_vmstate(const char *name);
  void do_delvm(Monitor *mon, const QDict *qdict);
  void do_info_snapshots(Monitor *mon);
vl.c +1 -1
index 1d4c350..5b2b84c 100644
@@ -3393,7 +3393,7 @@ int main(int argc, char **argv, char **envp)
      default_drive(default_sdcard, snapshot, machine->use_scsi,
                    IF_SD, 0, SD_OPTS);
 
-    register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
+    register_savevm_live(NULL, "ram", 0, 4, 1, NULL, ram_save_live, NULL,
                           ram_load, NULL);
 
      if (nb_numa_nodes>  0) {
vmstate.h +0 -0
index 9d3c49c..3cef117 100644

vmstate.h int register_savevm_live(DeviceState *dev, · line 50

Quoted 7 lines
vmstate.h +1 -0
@@ -44,6 +44,7 @@ int register_savevm_live(DeviceState *dev,
                           const char *idstr,
                           int instance_id,
                           int version_id,
+                         int is_ram,
                           SaveSetParamsHandler *set_params,
                           SaveLiveStateHandler *save_live_state,
                           SaveStateHandler *save_state,
Signature · 1 line
Anthony PERARD
#7 lore raw

Re: [Qemu-devel] [PATCH RESEND v5 2/6] Introduce "save_devices"

In reply to Stefano Stabellini
Quoted 23 lines · Stefano Stabellini

- add an "is_ram" flag to SaveStateEntry;

- add an "is_ram" parameter to register_savevm_live;

- introduce a "save_devices" monitor command that can be used to save the state of non-ram devices.

---
  block-migration.c |    2 +-
  hmp-commands.hx   |   14 ++++++++++
  qmp-commands.hx   |   17 ++++++++++++
  savevm.c          |   72 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
  sysemu.h          |    1 +
  vl.c              |    2 +-
  vmstate.h         |    1 +
  7 files changed, 106 insertions(+), 3 deletions(-)
block-migration.c +0 -0
index 4467468..d283fd0 100644

block-migration.c void blk_mig_init(void) · line 726

Quoted 6 lines
block-migration.c +1 -1
@@ -722,6 +722,6 @@ void blk_mig_init(void)
      QSIMPLEQ_INIT(&block_mig_state.bmds_list);
      QSIMPLEQ_INIT(&block_mig_state.blk_list);
 
-    register_savevm_live(NULL, "block", 0, 1, block_set_params,
+    register_savevm_live(NULL, "block", 0, 1, 0, block_set_params,
                           block_save_live, NULL, block_load,&block_mig_state);

Do you really want the block live migration to be part of Xen?

If not, then you can simplify by just making register_savevm_live always imply !device and adjust register_savevm() accordingly. Otherwise, the likelihood of a casual observer knowing what '0, 1, 0' means is pretty bad...

Quoted 44 lines
block-migration.c +0 -0
  }
hmp-commands.hx +14 -0
index 64b3656..873abc9 100644
@@ -280,6 +280,20 @@ a snapshot with the same tag or ID, it is replaced. More info at
  ETEXI
 
      {
+        .name       = "save_devices",
+        .args_type  = "filename:F",
+        .params     = "filename",
+        .help       = "save the state of non-ram devices.",
+        .mhandler.cmd_new = do_save_device_state,
+    },
+
+STEXI
+@item save_devices @var{filename}
+@findex save_devices
+Save the state of non-ram devices.
+ETEXI
+
+    {
          .name       = "loadvm",
          .args_type  = "name:s",
          .params     = "tag|id",
qmp-commands.hx +10 -0
index 705f704..619d9de 100644
@@ -444,6 +444,23 @@ Note: inject-nmi is only supported for x86 guest currently, it will
  EQMP
 
      {
+        .name       = "save_devices",
+        .args_type  = "filename:F",
+        .params     = "filename",
+        .help       = "save the state of non-ram devices.",
+        .user_print = monitor_user_noop,	
+    .mhandler.cmd_new = do_save_device_state,
+    },
+
+SQMP
+save_devices

qmp-commands.hx Note: inject-nmi is only supported for x86 guest currently, it will · line 466

Quoted 10 lines
qmp-commands.hx +7 -0
+-------
+
+Save the state of non-ram devices.
+
+EQMP
+
+    {
          .name       = "migrate",
          .args_type  = "detach:-d,blk:-b,inc:-i,uri:s",
          .params     = "[-d] [-b] [-i] uri",

Should be QAPI commands and documented a great deal more (see other examples in qapi-schema.json). Please CC Luiz too when adding new QMP commands.

Quoted 45 lines
savevm.c +8 -1
index 80be1ff..d0e62bb 100644
@@ -1177,6 +1177,7 @@ typedef struct SaveStateEntry {
      void *opaque;
      CompatEntry *compat;
      int no_migrate;
+    int is_ram;
  } SaveStateEntry;
 
 
@@ -1223,6 +1224,7 @@ int register_savevm_live(DeviceState *dev,
                           const char *idstr,
                           int instance_id,
                           int version_id,
+                         int is_ram,
                           SaveSetParamsHandler *set_params,
                           SaveLiveStateHandler *save_live_state,
                           SaveStateHandler *save_state,
@@ -1241,6 +1243,7 @@ int register_savevm_live(DeviceState *dev,
      se->opaque = opaque;
      se->vmsd = NULL;
      se->no_migrate = 0;
+    se->is_ram = is_ram;
 
      if (dev&&  dev->parent_bus&&  dev->parent_bus->info->get_dev_path) {
          char *id = dev->parent_bus->info->get_dev_path(dev);
@@ -1277,7 +1280,7 @@ int register_savevm(DeviceState *dev,
                      LoadStateHandler *load_state,
                      void *opaque)
  {
-    return register_savevm_live(dev, idstr, instance_id, version_id,
+    return register_savevm_live(dev, idstr, instance_id, version_id, 0,
                                  NULL, NULL, save_state, load_state, opaque);
  }
 
@@ -1728,6 +1731,43 @@ out:
      return ret;
  }
 
+static int qemu_save_device_state(Monitor *mon, QEMUFile *f)
+{
+    SaveStateEntry *se;
+

savevm.c out: · line 1747

Quoted 10 lines
savevm.c +10 -0
+    qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
+    qemu_put_be32(f, QEMU_VM_FILE_VERSION);
+
+    cpu_synchronize_all_states();
+
+    QTAILQ_FOREACH(se,&savevm_handlers, entry) {
+        int len;
+
+        if (se->is_ram)
+            continue;

Violating CODING_STYLE.

Quoted 12 lines
savevm.c +12 -0
+        if (se->save_state == NULL&&  se->vmsd == NULL)
+            continue;
+
+        /* Section type */
+        qemu_put_byte(f, QEMU_VM_SECTION_FULL);
+        qemu_put_be32(f, se->section_id);
+
+        /* ID string */
+        len = strlen(se->idstr);
+        qemu_put_byte(f, len);
+        qemu_put_buffer(f, (uint8_t *)se->idstr, len);
+

savevm.c out: · line 1769

Quoted 10 lines
savevm.c +10 -0
+        qemu_put_be32(f, se->instance_id);
+        qemu_put_be32(f, se->version_id);
+
+        vmstate_save(f, se);
+    }
+
+    qemu_put_byte(f, QEMU_VM_EOF);
+
+    return qemu_file_get_error(f);
+}

Please add something to docs/ documenting this format.

Quoted 28 lines
savevm.c +21 -0
+
  static SaveStateEntry *find_se(const char *idstr, int instance_id)
  {
      SaveStateEntry *se;
@@ -2109,6 +2149,36 @@ void do_savevm(Monitor *mon, const QDict *qdict)
          vm_start();
  }
 
+int do_save_device_state(Monitor *mon, const QDict *qdict, QObject **ret_data)
+{
+    int ret;
+    QEMUFile *f;
+    int saved_vm_running;
+    const char *filename = qdict_get_try_str(qdict, "filename");
+
+    saved_vm_running = runstate_is_running();
+    vm_stop(RUN_STATE_SAVE_VM);
+
+    f = qemu_fopen(filename, "wb");
+    if (!f) {
+        monitor_printf(mon, "Could not open VM state file\n");
+        ret = -1;
+        goto the_end;
+    }
+    ret = qemu_save_device_state(mon, f);
+    qemu_fclose(f);
+    if (ret<  0) {
+        monitor_printf(mon, "Error %d while writing VM\n", ret);

savevm.c void do_savevm(Monitor *mon, const QDict *qdict) · line 2181

Quoted 10 lines
savevm.c +10 -0
+        goto the_end;
+    }
+    ret = 0;
+
+ the_end:
+    if (saved_vm_running)
+        vm_start();
+    return ret;
+}
+

Would it be useful/interesting to return a binary blob via QMP instead of writing to a file?

Regards,

Anthony Liguori

Quoted 40 lines
savevm.c +0 -0
  int load_vmstate(const char *name)
  {
      BlockDriverState *bs, *bs_vm_state;
sysemu.h +1 -0
index 98118cc..f4d5bf4 100644
@@ -70,6 +70,7 @@ void qemu_remove_exit_notifier(Notifier *notify);
  void qemu_add_machine_init_done_notifier(Notifier *notify);
 
  void do_savevm(Monitor *mon, const QDict *qdict);
+int do_save_device_state(Monitor *mon, const QDict *qdict, QObject **ret_data);
  int load_vmstate(const char *name);
  void do_delvm(Monitor *mon, const QDict *qdict);
  void do_info_snapshots(Monitor *mon);
vl.c +1 -1
index 1d4c350..5b2b84c 100644
@@ -3393,7 +3393,7 @@ int main(int argc, char **argv, char **envp)
      default_drive(default_sdcard, snapshot, machine->use_scsi,
                    IF_SD, 0, SD_OPTS);
 
-    register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
+    register_savevm_live(NULL, "ram", 0, 4, 1, NULL, ram_save_live, NULL,
                           ram_load, NULL);
 
      if (nb_numa_nodes>  0) {
vmstate.h +1 -0
index 9d3c49c..3cef117 100644
@@ -44,6 +44,7 @@ int register_savevm_live(DeviceState *dev,
                           const char *idstr,
                           int instance_id,
                           int version_id,
+                         int is_ram,
                           SaveSetParamsHandler *set_params,
                           SaveLiveStateHandler *save_live_state,
                           SaveStateHandler *save_state,
#8 lore raw

Re: [Qemu-devel] [PATCH RESEND v5 2/6] Introduce "save_devices"

Quoted 33 lines · Anthony Liguori
Quoted 23 lines · Stefano Stabellini

- add an "is_ram" flag to SaveStateEntry;

- add an "is_ram" parameter to register_savevm_live;

- introduce a "save_devices" monitor command that can be used to save the state of non-ram devices.

---
  block-migration.c |    2 +-
  hmp-commands.hx   |   14 ++++++++++
  qmp-commands.hx   |   17 ++++++++++++
  savevm.c          |   72 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
  sysemu.h          |    1 +
  vl.c              |    2 +-
  vmstate.h         |    1 +
  7 files changed, 106 insertions(+), 3 deletions(-)
block-migration.c +0 -0
index 4467468..d283fd0 100644

block-migration.c void blk_mig_init(void) · line 726

Quoted 6 lines
block-migration.c +1 -1
@@ -722,6 +722,6 @@ void blk_mig_init(void)
      QSIMPLEQ_INIT(&block_mig_state.bmds_list);
      QSIMPLEQ_INIT(&block_mig_state.blk_list);
 
-    register_savevm_live(NULL, "block", 0, 1, block_set_params,
+    register_savevm_live(NULL, "block", 0, 1, 0, block_set_params,
                           block_save_live, NULL, block_load,&block_mig_state);

Do you really want the block live migration to be part of Xen?

Quoted 3 lines

If not, then you can simplify by just making register_savevm_live always imply !device and adjust register_savevm() accordingly. Otherwise, the likelihood of a casual observer knowing what '0, 1, 0' means is pretty bad...

That's a good point, I'll do that.

Quoted 50 lines
Quoted 50 lines
block-migration.c +0 -0
  }
hmp-commands.hx +14 -0
index 64b3656..873abc9 100644
@@ -280,6 +280,20 @@ a snapshot with the same tag or ID, it is replaced. More info at
  ETEXI
 
      {
+        .name       = "save_devices",
+        .args_type  = "filename:F",
+        .params     = "filename",
+        .help       = "save the state of non-ram devices.",
+        .mhandler.cmd_new = do_save_device_state,
+    },
+
+STEXI
+@item save_devices @var{filename}
+@findex save_devices
+Save the state of non-ram devices.
+ETEXI
+
+    {
          .name       = "loadvm",
          .args_type  = "name:s",
          .params     = "tag|id",
qmp-commands.hx +16 -0
index 705f704..619d9de 100644
@@ -444,6 +444,23 @@ Note: inject-nmi is only supported for x86 guest currently, it will
  EQMP
 
      {
+        .name       = "save_devices",
+        .args_type  = "filename:F",
+        .params     = "filename",
+        .help       = "save the state of non-ram devices.",
+        .user_print = monitor_user_noop,	
+    .mhandler.cmd_new = do_save_device_state,
+    },
+
+SQMP
+save_devices
+-------
+
+Save the state of non-ram devices.
+
+EQMP
+
Quoted 8 lines

qmp-commands.hx Note: inject-nmi is only supported for x86 guest currently, it will · line 466

Quoted 4 lines
qmp-commands.hx +1 -0
+    {
          .name       = "migrate",
          .args_type  = "detach:-d,blk:-b,inc:-i,uri:s",
          .params     = "[-d] [-b] [-i] uri",

Should be QAPI commands and documented a great deal more (see other examples in qapi-schema.json). Please CC Luiz too when adding new QMP commands.

OK, I'll do.

Quoted 41 lines
Quoted 41 lines
savevm.c +4 -1
index 80be1ff..d0e62bb 100644
@@ -1177,6 +1177,7 @@ typedef struct SaveStateEntry {
      void *opaque;
      CompatEntry *compat;
      int no_migrate;
+    int is_ram;
  } SaveStateEntry;
 
 
@@ -1223,6 +1224,7 @@ int register_savevm_live(DeviceState *dev,
                           const char *idstr,
                           int instance_id,
                           int version_id,
+                         int is_ram,
                           SaveSetParamsHandler *set_params,
                           SaveLiveStateHandler *save_live_state,
                           SaveStateHandler *save_state,
@@ -1241,6 +1243,7 @@ int register_savevm_live(DeviceState *dev,
      se->opaque = opaque;
      se->vmsd = NULL;
      se->no_migrate = 0;
+    se->is_ram = is_ram;
 
      if (dev&&  dev->parent_bus&&  dev->parent_bus->info->get_dev_path) {
          char *id = dev->parent_bus->info->get_dev_path(dev);
@@ -1277,7 +1280,7 @@ int register_savevm(DeviceState *dev,
                      LoadStateHandler *load_state,
                      void *opaque)
  {
-    return register_savevm_live(dev, idstr, instance_id, version_id,
+    return register_savevm_live(dev, idstr, instance_id, version_id, 0,
                                  NULL, NULL, save_state, load_state, opaque);
  }
 
@@ -1728,6 +1731,43 @@ out:
      return ret;
  }
 
Quoted 16 lines
Quoted 4 lines
savevm.c +4 -0
+static int qemu_save_device_state(Monitor *mon, QEMUFile *f)
+{
+    SaveStateEntry *se;
+

savevm.c out: · line 1747

Quoted 10 lines
savevm.c +10 -0
+    qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
+    qemu_put_be32(f, QEMU_VM_FILE_VERSION);
+
+    cpu_synchronize_all_states();
+
+    QTAILQ_FOREACH(se,&savevm_handlers, entry) {
+        int len;
+
+        if (se->is_ram)
+            continue;

Violating CODING_STYLE.

I'll fix that.

Quoted 16 lines
Quoted 16 lines
savevm.c +16 -0
+        if (se->save_state == NULL&&  se->vmsd == NULL)
+            continue;
+
+        /* Section type */
+        qemu_put_byte(f, QEMU_VM_SECTION_FULL);
+        qemu_put_be32(f, se->section_id);
+
+        /* ID string */
+        len = strlen(se->idstr);
+        qemu_put_byte(f, len);
+        qemu_put_buffer(f, (uint8_t *)se->idstr, len);
+
+        qemu_put_be32(f, se->instance_id);
+        qemu_put_be32(f, se->version_id);
+
+        vmstate_save(f, se);
Quoted 8 lines

savevm.c out: · line 1769

Quoted 6 lines
savevm.c +6 -0
+    }
+
+    qemu_put_byte(f, QEMU_VM_EOF);
+
+    return qemu_file_get_error(f);
+}

Please add something to docs/ documenting this format.

OK

Quoted 33 lines
Quoted 33 lines
savevm.c +26 -0
+
  static SaveStateEntry *find_se(const char *idstr, int instance_id)
  {
      SaveStateEntry *se;
@@ -2109,6 +2149,36 @@ void do_savevm(Monitor *mon, const QDict *qdict)
          vm_start();
  }
 
+int do_save_device_state(Monitor *mon, const QDict *qdict, QObject **ret_data)
+{
+    int ret;
+    QEMUFile *f;
+    int saved_vm_running;
+    const char *filename = qdict_get_try_str(qdict, "filename");
+
+    saved_vm_running = runstate_is_running();
+    vm_stop(RUN_STATE_SAVE_VM);
+
+    f = qemu_fopen(filename, "wb");
+    if (!f) {
+        monitor_printf(mon, "Could not open VM state file\n");
+        ret = -1;
+        goto the_end;
+    }
+    ret = qemu_save_device_state(mon, f);
+    qemu_fclose(f);
+    if (ret<  0) {
+        monitor_printf(mon, "Error %d while writing VM\n", ret);
+        goto the_end;
+    }
+    ret = 0;
+
+ the_end:
Quoted 8 lines

savevm.c void do_savevm(Monitor *mon, const QDict *qdict) · line 2181

Quoted 5 lines
savevm.c +5 -0
+    if (saved_vm_running)
+        vm_start();
+    return ret;
+}
+

Would it be useful/interesting to return a binary blob via QMP instead of writing to a file?

Yes, it is potentially useful. I have just gone with the file interface because it is easier to handle but I could change it or maybe have QMP return the binary as an alternative option.

#9 lore raw

Re: [Qemu-devel] [PATCH RESEND v5 2/6] Introduce "save_devices"

Quoted 4 lines · On Tue, 13 Mar 2012 11:51:56 +0000 Stefano Stabellini…
Quoted 2 lines

Should be QAPI commands and documented a great deal more (see other examples in qapi-schema.json). Please CC Luiz too when adding new QMP commands.

OK, I'll do.

I reviewed the RunState patch but missed that one... Btw Stefano, there's a tutorial for new QMP commands: docs/writing-qmp-commands.txt.

#10 lore raw

PATCH RESEND v5 3/6 Set runstate to INMIGRATE earlier

In reply to Stefano Stabellini

Set runstate to RUN_STATE_INMIGRATE as soon as we can on resume.

---
 vl.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
vl.c +1 -1
index 5b2b84c..341775b 100644
@@ -3099,6 +3099,7 @@ int main(int argc, char **argv, char **envp)
                 break;
             case QEMU_OPTION_incoming:
                 incoming = optarg;
+                runstate_set(RUN_STATE_INMIGRATE);
                 break;
             case QEMU_OPTION_nodefaults:
                 default_serial = 0;
@@ -3596,7 +3597,6 @@ int main(int argc, char **argv, char **envp)
     }
 
     if (incoming) {
-        runstate_set(RUN_STATE_INMIGRATE);
         int ret = qemu_start_incoming_migration(incoming);
         if (ret < 0) {
             fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
Signature · 1 line
1.7.2.5
Acked-by on v5 3/6
#12 lore raw

Re: [Qemu-devel] [PATCH RESEND v5 3/6] Set runstate to INMIGRATE earlier

In reply to Stefano Stabellini
Quoted 3 lines · On Tue, 28 Feb 2012 15:51:32 +0000 Stefano Stabellini…

Set runstate to RUN_STATE_INMIGRATE as soon as we can on resume.

Would be nice to explain why, but I suggested this myself so:

Quoted 24 lines
---
 vl.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
vl.c +1 -1
index 5b2b84c..341775b 100644
@@ -3099,6 +3099,7 @@ int main(int argc, char **argv, char **envp)
                 break;
             case QEMU_OPTION_incoming:
                 incoming = optarg;
+                runstate_set(RUN_STATE_INMIGRATE);
                 break;
             case QEMU_OPTION_nodefaults:
                 default_serial = 0;
@@ -3596,7 +3597,6 @@ int main(int argc, char **argv, char **envp)
     }
 
     if (incoming) {
-        runstate_set(RUN_STATE_INMIGRATE);
         int ret = qemu_start_incoming_migration(incoming);
         if (ret < 0) {
             fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
#13 lore raw

Re: [Qemu-devel] [PATCH RESEND v5 3/6] Set runstate to INMIGRATE earlier

Quoted 10 lines · Luiz Capitulino
Quoted 3 lines · On Tue, 28 Feb 2012 15:51:32 +0000 Stefano Stabellini…

Set runstate to RUN_STATE_INMIGRATE as soon as we can on resume.

Would be nice to explain why, but I suggested this myself so:

Thanks!
It would be nice if somebody took the time to review patch #1 and #2 too considering that they have been sitting there for a while...

#14 lore raw

PATCH RESEND v5 4/6 xen: record physmap changes to xenstore

In reply to Stefano Stabellini

Write to xenstore any physmap changes so that the hypervisor can be aware of them.
Read physmap changes from xenstore on boot.

---
 xen-all.c |   78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 77 insertions(+), 1 deletions(-)
xen-all.c +77 -1
index b0ed1ed..f2cad82 100644
@@ -65,7 +65,7 @@ static inline ioreq_t *xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)
 typedef struct XenPhysmap {
     target_phys_addr_t start_addr;
     ram_addr_t size;
-    MemoryRegion *mr;
+    char *name;
     target_phys_addr_t phys_offset;
 
     QLIST_ENTRY(XenPhysmap) list;
@@ -237,6 +237,7 @@ static int xen_add_to_physmap(XenIOState *state,
     XenPhysmap *physmap = NULL;
     target_phys_addr_t pfn, start_gpfn;
     target_phys_addr_t phys_offset = memory_region_get_ram_addr(mr);
+    char path[80], value[17];
 
     if (get_physmapping(state, start_addr, size)) {
         return 0;
@@ -275,6 +276,7 @@ go_physmap:
 
     physmap->start_addr = start_addr;
     physmap->size = size;
+    physmap->name = (char *)mr->name;
     physmap->phys_offset = phys_offset;
 
     QLIST_INSERT_HEAD(&state->physmap, physmap, list);
@@ -283,6 +285,30 @@ go_physmap:
                                    start_addr >> TARGET_PAGE_BITS,
                                    (start_addr + size) >> TARGET_PAGE_BITS,
                                    XEN_DOMCTL_MEM_CACHEATTR_WB);
+
+    snprintf(path, sizeof(path),
+            "/local/domain/0/device-model/%d/physmap/%"PRIx64"/start_addr",
+            xen_domid, (uint64_t)phys_offset);
+    snprintf(value, sizeof(value), "%"PRIx64, (uint64_t)start_addr);
+    if (!xs_write(state->xenstore, 0, path, value, strlen(value))) {
+        return -1;
+    }
+    snprintf(path, sizeof(path),
+            "/local/domain/0/device-model/%d/physmap/%"PRIx64"/size",
+            xen_domid, (uint64_t)phys_offset);
+    snprintf(value, sizeof(value), "%"PRIx64, (uint64_t)size);
+    if (!xs_write(state->xenstore, 0, path, value, strlen(value))) {
+        return -1;
+    }
+    if (mr->name) {
+        snprintf(path, sizeof(path),
+                "/local/domain/0/device-model/%d/physmap/%"PRIx64"/name",
+                xen_domid, (uint64_t)phys_offset);
+        if (!xs_write(state->xenstore, 0, path, mr->name, strlen(mr->name))) {
+            return -1;
+        }
+    }
+
     return 0;
 }
 
@@ -911,6 +937,55 @@ int xen_init(void)
     return 0;
 }
 
+static void xen_read_physmap(XenIOState *state)
+{
+    XenPhysmap *physmap = NULL;
+    unsigned int len, num, i;
+    char path[80], *value = NULL;
+    char **entries = NULL;
+
+    snprintf(path, sizeof(path),
+            "/local/domain/0/device-model/%d/physmap", xen_domid);
+    entries = xs_directory(state->xenstore, 0, path, &num);
+    if (entries == NULL)
+        return;
+
+    for (i = 0; i < num; i++) {
+        physmap = g_malloc(sizeof (XenPhysmap));
+        physmap->phys_offset = strtoull(entries[i], NULL, 16);
+        snprintf(path, sizeof(path),
+                "/local/domain/0/device-model/%d/physmap/%s/start_addr",
+                xen_domid, entries[i]);
+        value = xs_read(state->xenstore, 0, path, &len);
+        if (value == NULL) {
+            free(physmap);
+            continue;
+        }
+        physmap->start_addr = strtoull(value, NULL, 16);
+        free(value);
+
+        snprintf(path, sizeof(path),
+                "/local/domain/0/device-model/%d/physmap/%s/size",
+                xen_domid, entries[i]);
+        value = xs_read(state->xenstore, 0, path, &len);
+        if (value == NULL) {
+            free(physmap);
+            continue;
+        }
+        physmap->size = strtoull(value, NULL, 16);
+        free(value);
+
+        snprintf(path, sizeof(path),
+                "/local/domain/0/device-model/%d/physmap/%s/name",
+                xen_domid, entries[i]);
+        physmap->name = xs_read(state->xenstore, 0, path, &len);
+
+        QLIST_INSERT_HEAD(&state->physmap, physmap, list);
+    }
+    free(entries);
+    return;
+}
+
 int xen_hvm_init(void)
 {
     int i, rc;
@@ -986,6 +1061,7 @@ int xen_hvm_init(void)
     xen_be_register("console", &xen_console_ops);
     xen_be_register("vkbd", &xen_kbdmouse_ops);
     xen_be_register("qdisk", &xen_blkdev_ops);
+    xen_read_physmap(state);
 
     return 0;
 }
Signature · 1 line
1.7.2.5
#15 lore raw

PATCH RESEND v5 5/6 xen mapcache: check if memory region has moved.

In reply to Stefano Stabellini

From: Anthony PERARD <[email protected]>

This patch changes the xen_map_cache behavior. Before trying to map a guest addr, mapcache will look into the list of range of address that have been moved (physmap/set_memory). There is currently one memory space like this, the vram, "moved" from were it's allocated to were the guest will look into.

This help to have a succefull migration.

---
 xen-all.c      |   18 +++++++++++++++++-
 xen-mapcache.c |   22 +++++++++++++++++++---
 xen-mapcache.h |    9 +++++++--
 3 files changed, 43 insertions(+), 6 deletions(-)
xen-all.c +17 -1
index f2cad82..972cffd 100644
@@ -225,6 +225,22 @@ static XenPhysmap *get_physmapping(XenIOState *state,
     return NULL;
 }
 
+static target_phys_addr_t xen_phys_offset_to_gaddr(target_phys_addr_t start_addr,
+                                                   ram_addr_t size, void *opaque)
+{
+    target_phys_addr_t addr = start_addr & TARGET_PAGE_MASK;
+    XenIOState *xen_io_state = opaque;
+    XenPhysmap *physmap = NULL;
+
+    QLIST_FOREACH(physmap, &xen_io_state->physmap, list) {
+        if (range_covers_byte(physmap->phys_offset, physmap->size, addr)) {
+            return physmap->start_addr;
+        }
+    }
+
+    return start_addr;
+}
+
 #if CONFIG_XEN_CTRL_INTERFACE_VERSION >= 340
 static int xen_add_to_physmap(XenIOState *state,
                               target_phys_addr_t start_addr,
@@ -1043,7 +1059,7 @@ int xen_hvm_init(void)
     }
 
     /* Init RAM management */
-    xen_map_cache_init();
+    xen_map_cache_init(xen_phys_offset_to_gaddr, state);
     xen_ram_init(ram_size);
 
     qemu_add_vm_change_state_handler(xen_hvm_change_state_handler, state);
xen-mapcache.c +19 -3
index 585b559..a456479 100644
@@ -78,6 +78,9 @@ typedef struct MapCache {
     uint8_t *last_address_vaddr;
     unsigned long max_mcache_size;
     unsigned int mcache_bucket_shift;
+
+    phys_offset_to_gaddr_t phys_offset_to_gaddr;
+    void *opaque;
 } MapCache;
 
 static MapCache *mapcache;
@@ -91,13 +94,16 @@ static inline int test_bits(int nr, int size, const unsigned long *addr)
         return 0;
 }
 
-void xen_map_cache_init(void)
+void xen_map_cache_init(phys_offset_to_gaddr_t f, void *opaque)
 {
     unsigned long size;
     struct rlimit rlimit_as;
 
     mapcache = g_malloc0(sizeof (MapCache));
 
+    mapcache->phys_offset_to_gaddr = f;
+    mapcache->opaque = opaque;
+
     QTAILQ_INIT(&mapcache->locked_entries);
     mapcache->last_address_index = -1;
 
@@ -193,9 +199,14 @@ uint8_t *xen_map_cache(target_phys_addr_t phys_addr, target_phys_addr_t size,
                        uint8_t lock)
 {
     MapCacheEntry *entry, *pentry = NULL;
-    target_phys_addr_t address_index  = phys_addr >> MCACHE_BUCKET_SHIFT;
-    target_phys_addr_t address_offset = phys_addr & (MCACHE_BUCKET_SIZE - 1);
+    target_phys_addr_t address_index;
+    target_phys_addr_t address_offset;
     target_phys_addr_t __size = size;
+    bool translated = false;
+
+tryagain:
+    address_index  = phys_addr >> MCACHE_BUCKET_SHIFT;
+    address_offset = phys_addr & (MCACHE_BUCKET_SIZE - 1);
 
     trace_xen_map_cache(phys_addr);
 
@@ -237,6 +248,11 @@ uint8_t *xen_map_cache(target_phys_addr_t phys_addr, target_phys_addr_t size,
     if(!test_bits(address_offset >> XC_PAGE_SHIFT, size >> XC_PAGE_SHIFT,
                 entry->valid_mapping)) {
         mapcache->last_address_index = -1;
+        if (!translated && mapcache->phys_offset_to_gaddr) {
+            phys_addr = mapcache->phys_offset_to_gaddr(phys_addr, size, mapcache->opaque);
+            translated = true;
+            goto tryagain;
+        }
         trace_xen_map_cache_return(NULL);
         return NULL;
     }
xen-mapcache.h +7 -2
index da874ca..70301a5 100644
@@ -11,9 +11,13 @@
 
 #include <stdlib.h>
 
+typedef target_phys_addr_t (*phys_offset_to_gaddr_t)(target_phys_addr_t start_addr,
+                                                     ram_addr_t size,
+                                                     void *opaque);
 #ifdef CONFIG_XEN
 
-void xen_map_cache_init(void);
+void xen_map_cache_init(phys_offset_to_gaddr_t f,
+                        void *opaque);
 uint8_t *xen_map_cache(target_phys_addr_t phys_addr, target_phys_addr_t size,
                        uint8_t lock);
 ram_addr_t xen_ram_addr_from_mapcache(void *ptr);
@@ -22,7 +26,8 @@ void xen_invalidate_map_cache(void);
 
 #else
 
-static inline void xen_map_cache_init(void)
+static inline void xen_map_cache_init(phys_offset_to_gaddr_t f,
+                                      void *opaque)
 {
 }
 
Signature · 1 line
1.7.2.5
#16 lore raw

PATCH RESEND v5 6/6 xen: do not allocate RAM during INMIGRATE runstate

In reply to Stefano Stabellini

From: Anthony PERARD <[email protected]>

---
 xen-all.c |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)
xen-all.c +8 -0
index 972cffd..10d53d1 100644
@@ -190,6 +190,14 @@ void xen_ram_alloc(ram_addr_t ram_addr, ram_addr_t size, MemoryRegion *mr)
     xen_pfn_t *pfn_list;
     int i;
 
+    if (runstate_check(RUN_STATE_INMIGRATE)) {
+        /* RAM already populated in Xen */
+        fprintf(stderr, "%s: do not alloc "RAM_ADDR_FMT
+                " bytes of ram at "RAM_ADDR_FMT" when runstate is INMIGRATE\n",
+                __func__, size, ram_addr); 
+        return;
+    }
+
     if (mr == &ram_memory) {
         return;
     }
Signature · 1 line
1.7.2.5