Skip to contentSkip to conversation

Migration Stream v2

PATCH Superseded Andrew Cooper 5 participants 39 messages Apr 30, 2014 – May 8, 2014

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v4 cover Migration Stream v2

Hello,

Presented here for review is v4 of the Migration Stream v2 work. David, Frediano and myself have worked together on the implementation, and we have now managed to get PV and HVM migration working using the new format (even when transparently inserted underneath Xapi).

This series is based on staging, as 9f2f1298d0211962 is a prerequisite hypercall for a bugfix.

Draft F of the design document is available from
xenbits.xen.org/people/andrewcoop/domain-save-format-F.pdf

The series is available from the branch 'saverestore2-v4' of
git://xenbits.xen.org/people/andrewcoop/xen.git
http://xenbits.xen.org/git-http/people/andrewcoop/xen.git

The code has been a clean rewrite, using the v1 code as a reference but avoiding obsolete areas (e.g. how to modify the pagetables of a 32 non-pae guest on 32bit pae Xen).

There are certainly some areas still in need improvement. There are plans to combine the save logic for PV and HVM guests by adding a few more hooks to save_restore_ops. While reviewing the series for posting, I have noticed some accidental duplication from attempting to merge PV and HVM together (ctx->save.p2m_size and ctx->x86_pv.max_pfn) which can be consolidated.

Moving forward will be work to do with converting a legacy image to a v2 image.

Questions/issues needing discussing are:

1) What level should the conversion happen. The v2 format is capable of detecting a legacy stream, but libxc is arguably too low level for the conversion decision to happen.

2) What to do about the layer violations which is the toolstack record and device model record. Libxl for HVM guests is the only known user of the 'toolstack' chunk, which contains some xenstore key/value pairs. This data should not be a blob in the middle of the libxc layer, and should be promoted to a first class member of a libxl migration stream.

Anyway - the code is presented here for initial comment/query/critism.

~Andrew

#1 lore raw

PATCH v4 1/9 libxc: add DECLARE_HYPERCALL_BUFFER_SHADOW()

From: David Vrabel <[email protected]>

DECLARE_HYPERCALL_BUFFER_SHADOW() is like DECLARE_HYPERCALL_BUFFER() except it is backed by an already allocated hypercall buffer.

---
 tools/libxc/xenctrl.h |   18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
tools/libxc/xenctrl.h +18 -0
index 02129f7..15d2f4f 100644
@@ -266,6 +266,24 @@ typedef struct xc_hypercall_buffer xc_hypercall_buffer_t;
     }
 
 /*
+ * Like DECLARE_HYPERCALL_BUFFER() but using an already allocated
+ * hypercall buffer, _hbuf.
+ *
+ * Useful when a hypercall buffer is passed to a function and access
+ * via the user pointer is required.
+ *
+ * See DECLARE_HYPERCALL_BUFFER_ARGUMENT() if the user pointer is not
+ * required.
+ */
+#define DECLARE_HYPERCALL_BUFFER_SHADOW(_type, _name, _hbuf)   \
+    _type *_name = _hbuf->hbuf;                                \
+    xc_hypercall_buffer_t XC__HYPERCALL_BUFFER_NAME(_name) = { \
+        .hbuf = (void *)-1,                                    \
+        .param_shadow = _hbuf,                                 \
+        HYPERCALL_BUFFER_INIT_NO_BOUNCE                        \
+    }
+
+/*
  * Declare the necessary data structure to allow a hypercall buffer
  * passed as an argument to a function to be used in the normal way.
  */
Signature · 1 line
1.7.10.4
#2 lore raw

Re: [PATCH v4 1/9] libxc: add DECLARE_HYPERCALL_BUFFER_SHADOW()

Quoted 4 lines · Andrew Cooper

From: David Vrabel <[email protected]>

DECLARE_HYPERCALL_BUFFER_SHADOW() is like DECLARE_HYPERCALL_BUFFER() except it is backed by an already allocated hypercall buffer.

I suppose enhancing DECLARE_HYPERCALL_BUFFER_ARGUMENT to have this property has issues with unused variables?

HYPERCALL_BUFFER_AS_PTR() would have been an alternative implementation (similar to AS_ARG), I suppose there is no particular reason to prefer one over the other?

Quoted 35 lines
---
 tools/libxc/xenctrl.h |   18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
tools/libxc/xenctrl.h +18 -0
index 02129f7..15d2f4f 100644
@@ -266,6 +266,24 @@ typedef struct xc_hypercall_buffer xc_hypercall_buffer_t;
     }
 
 /*
+ * Like DECLARE_HYPERCALL_BUFFER() but using an already allocated
+ * hypercall buffer, _hbuf.
+ *
+ * Useful when a hypercall buffer is passed to a function and access
+ * via the user pointer is required.
+ *
+ * See DECLARE_HYPERCALL_BUFFER_ARGUMENT() if the user pointer is not
+ * required.
+ */
+#define DECLARE_HYPERCALL_BUFFER_SHADOW(_type, _name, _hbuf)   \
+    _type *_name = _hbuf->hbuf;                                \
+    xc_hypercall_buffer_t XC__HYPERCALL_BUFFER_NAME(_name) = { \
+        .hbuf = (void *)-1,                                    \
+        .param_shadow = _hbuf,                                 \
+        HYPERCALL_BUFFER_INIT_NO_BOUNCE                        \
+    }
+
+/*
  * Declare the necessary data structure to allow a hypercall buffer
  * passed as an argument to a function to be used in the normal way.
  */
#3 lore raw

Re: [PATCH v4 1/9] libxc: add DECLARE_HYPERCALL_BUFFER_SHADOW()

Quoted 8 lines · Ian Campbell
Quoted 4 lines · Andrew Cooper

From: David Vrabel <[email protected]>

DECLARE_HYPERCALL_BUFFER_SHADOW() is like DECLARE_HYPERCALL_BUFFER() except it is backed by an already allocated hypercall buffer.

I suppose enhancing DECLARE_HYPERCALL_BUFFER_ARGUMENT to have this property has issues with unused variables?

Yes, I think so. Although I didn't actually try it... Perhaps I should.

Quoted 3 lines

HYPERCALL_BUFFER_AS_PTR() would have been an alternative implementation (similar to AS_ARG), I suppose there is no particular reason to prefer one over the other?

Usage of the buffer wouldn't be consistent with a regular DECLARE_HYPERCALL_BUFFER(). It would lead to slightly more confusing code like:

DECLARE_HYPERCALL_BUFFER(hbuf);
unsigned long *to_send = HYPERCALL_BUFFER_AS_PTR(hbuf);
...

    if ( test_bit(p, to_send) )
...
    rc = xc_shadow_control(xch, ctx->domid,
                      XEN_DOMCTL_SHADOW_OP_CLEAN,
                      hbuf,
                      ctx->save.p2m_size
                      NULL, 0, shadow_stats);

And it's not as obvious that hbuf and to_send are really the same buffer without going back to the declarations.

David

#4 lore raw

Re: [PATCH v4 1/9] libxc: add DECLARE_HYPERCALL_BUFFER_SHADOW()

Quoted 11 lines · David Vrabel
Quoted 8 lines · Ian Campbell
Quoted 4 lines · Andrew Cooper

From: David Vrabel <[email protected]>

DECLARE_HYPERCALL_BUFFER_SHADOW() is like DECLARE_HYPERCALL_BUFFER() except it is backed by an already allocated hypercall buffer.

I suppose enhancing DECLARE_HYPERCALL_BUFFER_ARGUMENT to have this property has issues with unused variables?

Yes, I think so. Although I didn't actually try it... Perhaps I should.

Would be good.

Quoted 19 lines
Quoted 3 lines

HYPERCALL_BUFFER_AS_PTR() would have been an alternative implementation (similar to AS_ARG), I suppose there is no particular reason to prefer one over the other?

Usage of the buffer wouldn't be consistent with a regular DECLARE_HYPERCALL_BUFFER(). It would lead to slightly more confusing code like:

DECLARE_HYPERCALL_BUFFER(hbuf);
unsigned long *to_send = HYPERCALL_BUFFER_AS_PTR(hbuf);
...

    if ( test_bit(p, to_send) )
...
    rc = xc_shadow_control(xch, ctx->domid,
                      XEN_DOMCTL_SHADOW_OP_CLEAN,
                      hbuf,
                      ctx->save.p2m_size
                      NULL, 0, shadow_stats);
Quoted 2 lines

And it's not as obvious that hbuf and to_send are really the same buffer without going back to the declarations.

OK. Acked-by: Ian Campbell <[email protected]>.

Ian.

#5 lore raw

PATCH v4 2/9 tools/libxc: save/restore v2 framework

In reply to Andrew Cooper

For testing purposes, the environmental variable "XG_MIGRATION_V2" allows the two save/restore codepaths to coexist, and have a runtime switch.

It is indended that once this series is less RFC, the v2 framework will completely replace v1.

---
 tools/libxc/Makefile              |    1 +
 tools/libxc/saverestore/common.h  |   15 +++++++++++++++
 tools/libxc/saverestore/restore.c |   23 +++++++++++++++++++++++
 tools/libxc/saverestore/save.c    |   20 ++++++++++++++++++++
 tools/libxc/xc_domain_restore.c   |    8 ++++++++
 tools/libxc/xc_domain_save.c      |    7 +++++++
 tools/libxc/xenguest.h            |   14 ++++++++++++++
 7 files changed, 88 insertions(+)
 create mode 100644 tools/libxc/saverestore/common.h
 create mode 100644 tools/libxc/saverestore/restore.c
 create mode 100644 tools/libxc/saverestore/save.c
tools/libxc/Makefile +1 -0
index a74b19e..52213dc 100644
@@ -44,6 +44,7 @@ GUEST_SRCS-y :=
 GUEST_SRCS-y += xg_private.c xc_suspend.c
 ifeq ($(CONFIG_MIGRATE),y)
 GUEST_SRCS-y += xc_domain_restore.c xc_domain_save.c
+GUEST_SRCS-y += $(wildcard saverestore/*.c)
 GUEST_SRCS-y += xc_offline_page.c xc_compression.c
 else
 GUEST_SRCS-y += xc_nomigrate.c
tools/libxc/saverestore/common.h new file +15 -0
new file mode 100644
index 0000000..f1aff44
@@ -0,0 +1,15 @@
+#ifndef __COMMON__H
+#define __COMMON__H
+
+#include "../xg_private.h"
+
+#endif
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
tools/libxc/saverestore/restore.c new file +23 -0
new file mode 100644
index 0000000..6624baa
@@ -0,0 +1,23 @@
+#include "common.h"
+
+int xc_domain_restore2(xc_interface *xch, int io_fd, uint32_t dom,
+                       unsigned int store_evtchn, unsigned long *store_mfn,
+                       domid_t store_domid, unsigned int console_evtchn,
+                       unsigned long *console_mfn, domid_t console_domid,
+                       unsigned int hvm, unsigned int pae, int superpages,
+                       int checkpointed_stream,
+                       struct restore_callbacks *callbacks)
+{
+    IPRINTF("In experimental %s", __func__);
+    return -1;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
tools/libxc/saverestore/save.c new file +20 -0
new file mode 100644
index 0000000..c013e62
@@ -0,0 +1,20 @@
+#include "common.h"
+
+int xc_domain_save2(xc_interface *xch, int io_fd, uint32_t dom, uint32_t max_iters,
+                    uint32_t max_factor, uint32_t flags,
+                    struct save_callbacks* callbacks, int hvm,
+                    unsigned long vm_generationid_addr)
+{
+    IPRINTF("In experimental %s", __func__);
+    return -1;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
tools/libxc/xc_domain_restore.c +8 -0
index bcb0ae0..faa458a 100644
@@ -1468,6 +1468,14 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
     struct restore_ctx *ctx = &_ctx;
     struct domain_info_context *dinfo = &ctx->dinfo;
 
+    if ( getenv("XG_MIGRATION_V2") )
+    {
+        return xc_domain_restore2(
+            xch, io_fd, dom, store_evtchn, store_mfn,
+            store_domid, console_evtchn, console_mfn, console_domid,
+            hvm,  pae,  superpages, checkpointed_stream, callbacks);
+    }
+
     DPRINTF("%s: starting restore of new domid %u", __func__, dom);
 
     pagebuf_init(&pagebuf);
tools/libxc/xc_domain_save.c +7 -0
index 71f9b59..c94e3e6 100644
@@ -895,6 +895,13 @@ int xc_domain_save(xc_interface *xch, int io_fd, uint32_t dom, uint32_t max_iter
 
     int completed = 0;
 
+    if ( getenv("XG_MIGRATION_V2") )
+    {
+        return xc_domain_save2(xch, io_fd, dom, max_iters,
+                               max_factor, flags, callbacks, hvm,
+                               vm_generationid_addr);
+    }
+
     DPRINTF("%s: starting save of domid %u", __func__, dom);
 
     if ( hvm && !callbacks->switch_qemu_logdirty )
tools/libxc/xenguest.h +14 -0
index 1f216cd..b80df82 100644
@@ -89,6 +89,11 @@ int xc_domain_save(xc_interface *xch, int io_fd, uint32_t dom, uint32_t max_iter
                    struct save_callbacks* callbacks, int hvm,
                    unsigned long vm_generationid_addr);
 
+/* Domain Save v2 */
+int xc_domain_save2(xc_interface *xch, int io_fd, uint32_t dom, uint32_t max_iters,
+                    uint32_t max_factor, uint32_t flags,
+                    struct save_callbacks* callbacks, int hvm,
+                    unsigned long vm_generationid_addr);
 
 /* callbacks provided by xc_domain_restore */
 struct restore_callbacks {
@@ -128,6 +133,15 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
                       int no_incr_generationid, int checkpointed_stream,
                       unsigned long *vm_generationid_addr,
                       struct restore_callbacks *callbacks);
+
+/* Domain Restore v2 */
+int xc_domain_restore2(xc_interface *xch, int io_fd, uint32_t dom,
+                       unsigned int store_evtchn, unsigned long *store_mfn,
+                       domid_t store_domid, unsigned int console_evtchn,
+                       unsigned long *console_mfn, domid_t console_domid,
+                       unsigned int hvm, unsigned int pae, int superpages,
+                       int checkpointed_stream,
+                       struct restore_callbacks *callbacks);
 /**
  * xc_domain_restore writes a file to disk that contains the device
  * model saved state.
Signature · 1 line
1.7.10.4
#6 lore raw

PATCH v4 3/9 tools/libxc: Stream specification and some common code

In reply to Andrew Cooper
---
 tools/libxc/saverestore/common.c        |   63 ++++++++++++
 tools/libxc/saverestore/common.h        |    8 ++
 tools/libxc/saverestore/stream_format.h |  158 +++++++++++++++++++++++++++++++
 3 files changed, 229 insertions(+)
 create mode 100644 tools/libxc/saverestore/common.c
 create mode 100644 tools/libxc/saverestore/stream_format.h
tools/libxc/saverestore/common.c new file +63 -0
new file mode 100644
index 0000000..de2e727
@@ -0,0 +1,63 @@
+#include "common.h"
+
+static const char *dhdr_types[] =
+{
+    [DHDR_TYPE_x86_pv]  = "x86 PV",
+    [DHDR_TYPE_x86_hvm] = "x86 HVM",
+    [DHDR_TYPE_x86_pvh] = "x86 PVH",
+    [DHDR_TYPE_arm]     = "ARM",
+};
+
+const char *dhdr_type_to_str(uint32_t type)
+{
+    if ( type < ARRAY_SIZE(dhdr_types) && dhdr_types[type] )
+        return dhdr_types[type];
+
+    return "Reserved";
+}
+
+static const char *mandatory_rec_types[] =
+{
+    [REC_TYPE_end]                  = "End",
+    [REC_TYPE_page_data]            = "Page data",
+    [REC_TYPE_x86_pv_info]          = "x86 PV info",
+    [REC_TYPE_x86_pv_p2m_frames]    = "x86 PV P2M frames",
+    [REC_TYPE_x86_pv_vcpu_basic]    = "x86 PV vcpu basic",
+    [REC_TYPE_x86_pv_vcpu_extended] = "x86 PV vcpu extended",
+    [REC_TYPE_x86_pv_vcpu_xsave]    = "x86 PV vcpu xsave",
+    [REC_TYPE_shared_info]          = "Shared info",
+    [REC_TYPE_tsc_info]             = "TSC info",
+    [REC_TYPE_hvm_context]          = "HVM context",
+    [REC_TYPE_hvm_params]           = "HVM params",
+    [REC_TYPE_toolstack]            = "Toolstack",
+};
+
+/*
+static const char *optional_rec_types[] =
+{
+     None yet...
+};
+*/
+
+const char *rec_type_to_str(uint32_t type)
+{
+    if ( type & REC_TYPE_optional )
+        return "Reserved";
+
+    if ( ((type & REC_TYPE_optional) == 0 ) &&
+         (type < ARRAY_SIZE(mandatory_rec_types)) &&
+         (mandatory_rec_types[type]) )
+        return mandatory_rec_types[type];
+
+    return "Reserved";
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
tools/libxc/saverestore/common.h +8 -0
index f1aff44..fff0a39 100644
@@ -3,6 +3,14 @@
 
 #include "../xg_private.h"
 
+#include "stream_format.h"
+
+// TODO: Find a better place to put this...
+#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
+
+const char *dhdr_type_to_str(uint32_t type);
+const char *rec_type_to_str(uint32_t type);
+
 #endif
 /*
  * Local variables:
tools/libxc/saverestore/stream_format.h new file +158 -0

158 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#7 lore raw

Re: [PATCH v4 3/9] tools/libxc: Stream specification and some common code

tools/libxc/saverestore/stream_format.h

Quoted 5 lines · Andrew Cooper
tools/libxc/saverestore/stream_format.h new file +0 -0
new file mode 100644
index 0000000..efcca60

Reference to the spec (which doesn't appear to be in this series anywhere BTW)

tools/libxc/saverestore/stream_format.h line 6

Quoted 7 lines
tools/libxc/saverestore/stream_format.h new file +6 -0
@@ -0,0 +1,158 @@
+#ifndef __STREAM_FORMAT__H
+#define __STREAM_FORMAT__H
+
+#include <inttypes.h>
+
+/* TODO - find somewhere more appropriate. rec_tsc_info needs it for 64bit */

You seem to use it on all of them though. Can we not use explicit padding, as you seem to do for many of the other structs?

tools/libxc/saverestore/stream_format.h line 14

Quoted 8 lines
tools/libxc/saverestore/stream_format.h new file +8 -0
+#ifndef __packed
+#define __packed __attribute__((packed))
+#endif
+
+/*
+ * Image Header
+ */
+struct __packed ihdr

So long as this remains confined to libxc/saverestore/*.[ch] we can avoid namespacing/prefixing. I suppose that is a safe assumption.

Quoted 23 lines
tools/libxc/saverestore/stream_format.h new file +23 -0
+{
+    uint64_t marker;
+    uint32_t id;
+    uint32_t version;
+    uint16_t options;
+    uint16_t _res1;
+    uint32_t _res2;
+};
+
+#define IHDR_MARKER  0xffffffffffffffffULL
+#define IHDR_ID      0x58454E46U
+#define IHDR_VERSION 1
+
+#define _IHDR_OPT_ENDIAN 0
+#define IHDR_OPT_LITTLE_ENDIAN (0 << _IHDR_OPT_ENDIAN)
+#define IHDR_OPT_BIG_ENDIAN    (1 << _IHDR_OPT_ENDIAN)
+
+/*
+ * Domain Header
+ */
+struct __packed dhdr
+{
+    uint32_t type;

tools/libxc/saverestore/stream_format.h line 47

Quoted 10 lines
tools/libxc/saverestore/stream_format.h new file +10 -0
+    uint16_t page_shift;
+    uint16_t _res1;
+    uint32_t xen_major;
+    uint32_t xen_minor;
+};
+
+#define DHDR_TYPE_x86_pv  0x00000001U
+#define DHDR_TYPE_x86_hvm 0x00000002U
+#define DHDR_TYPE_x86_pvh 0x00000003U
+#define DHDR_TYPE_arm     0x00000004U

That capitalisation here (and elsewhere) seems rather unconventional.

BTW IHDR_OPT is also inconsistent with the pattern you've established. TBH I'd prefer more conventional upper case #defines. I suppose you want to avoid enums for the ABI-confusing properties they have.

#8 lore raw

Re: [PATCH v4 3/9] tools/libxc: Stream specification and some common code

Quoted 10 lines · Ian Campbell

tools/libxc/saverestore/stream_format.h

Quoted 5 lines · Andrew Cooper
tools/libxc/saverestore/stream_format.h new file +0 -0
new file mode 100644
index 0000000..efcca60

Reference to the spec (which doesn't appear to be in this series anywhere BTW)

Quoted 10 lines

tools/libxc/saverestore/stream_format.h line 6

Quoted 7 lines
tools/libxc/saverestore/stream_format.h new file +6 -0
@@ -0,0 +1,158 @@
+#ifndef __STREAM_FORMAT__H
+#define __STREAM_FORMAT__H
+
+#include <inttypes.h>
+
+/* TODO - find somewhere more appropriate. rec_tsc_info needs it for 64bit */

You seem to use it on all of them though. Can we not use explicit padding, as you seem to do for many of the other structs?

Yes, rec_tsc_info needs a trailing uint32_t _res1 field to match the spec.

There should be no need to __packed on any of the structures.

David

#9 lore raw

Re: [PATCH v4 3/9] tools/libxc: Stream specification and some common code

In reply to Ian Campbell
Quoted 8 lines · Ian Campbell

tools/libxc/saverestore/stream_format.h

Quoted 5 lines · Andrew Cooper
tools/libxc/saverestore/stream_format.h new file +0 -0
new file mode 100644
index 0000000..efcca60

Reference to the spec (which doesn't appear to be in this series anywhere BTW)

The spec pdf is referenced in the header. I suppose it should move into docs/.

Quoted 10 lines

tools/libxc/saverestore/stream_format.h line 6

Quoted 8 lines
tools/libxc/saverestore/stream_format.h new file +6 -0
@@ -0,0 +1,158 @@
+#ifndef __STREAM_FORMAT__H
+#define __STREAM_FORMAT__H
+
+#include <inttypes.h>
+
+/* TODO - find somewhere more appropriate. rec_tsc_info needs it for 64bit */

You seem to use it on all of them though. Can we not use explicit padding, as you seem to do for many of the other structs?

The issue with rec_tsc_info is its trailing alignment. i.e. sizeof(rec_tsc_info) is different between 32 and 64 bit builds, yet all fields are at the same offset from the beginning. Putting explicit trailing padding would invalidate the current code writing sizeof(rec_tsc_info) bytes into the stream.

Inside Xen when I was sorting out the __attribute__((packed)) consistency, we did agree on __packed for binary interfaces even when not strictly necessary.

Quoted 11 lines

tools/libxc/saverestore/stream_format.h line 14

Quoted 9 lines
tools/libxc/saverestore/stream_format.h new file +8 -0
+#ifndef __packed
+#define __packed __attribute__((packed))
+#endif
+
+/*
+ * Image Header
+ */
+struct __packed ihdr

So long as this remains confined to libxc/saverestore/*.[ch] we can avoid namespacing/prefixing. I suppose that is a safe assumption.

Hmm yes. (This I suppose is still "v1 code hacking to make something work")

Probably do want some namespacing even if it does only live inside this code, although an obvious prefix doesn't jump to mind.

Quoted 36 lines
Quoted 24 lines
tools/libxc/saverestore/stream_format.h new file +23 -0
+{
+    uint64_t marker;
+    uint32_t id;
+    uint32_t version;
+    uint16_t options;
+    uint16_t _res1;
+    uint32_t _res2;
+};
+
+#define IHDR_MARKER  0xffffffffffffffffULL
+#define IHDR_ID      0x58454E46U
+#define IHDR_VERSION 1
+
+#define _IHDR_OPT_ENDIAN 0
+#define IHDR_OPT_LITTLE_ENDIAN (0 << _IHDR_OPT_ENDIAN)
+#define IHDR_OPT_BIG_ENDIAN    (1 << _IHDR_OPT_ENDIAN)
+
+/*
+ * Domain Header
+ */
+struct __packed dhdr
+{
+    uint32_t type;

tools/libxc/saverestore/stream_format.h line 47

Quoted 10 lines
tools/libxc/saverestore/stream_format.h new file +10 -0
+    uint16_t page_shift;
+    uint16_t _res1;
+    uint32_t xen_major;
+    uint32_t xen_minor;
+};
+
+#define DHDR_TYPE_x86_pv  0x00000001U
+#define DHDR_TYPE_x86_hvm 0x00000002U
+#define DHDR_TYPE_x86_pvh 0x00000003U
+#define DHDR_TYPE_arm     0x00000004U

That capitalisation here (and elsewhere) seems rather unconventional.

Quoted 5 lines

BTW IHDR_OPT is also inconsistent with the pattern you've established. TBH I'd prefer more conventional upper case #defines. I suppose you want to avoid enums for the ABI-confusing properties they have.

This particular bit is to match the spec, but I see what you mean. I shall make it consistently upper case.

I hadn't actually considered an enum. It might be ok as each type field is passed as a uint32_t, although changing it would be faff for no appreciable gain.

~Andrew

#10 lore raw

Re: [PATCH v4 3/9] tools/libxc: Stream specification and some common code

Quoted 10 lines · Andrew Cooper
Quoted 8 lines · Ian Campbell

tools/libxc/saverestore/stream_format.h

Quoted 5 lines · Andrew Cooper
tools/libxc/saverestore/stream_format.h new file +0 -0
new file mode 100644
index 0000000..efcca60

Reference to the spec (which doesn't appear to be in this series anywhere BTW)

Quoted 2 lines

The spec pdf is referenced in the header. I suppose it should move into docs/.

Please.

Quoted 12 lines
Quoted 11 lines

tools/libxc/saverestore/stream_format.h line 6

Quoted 9 lines
tools/libxc/saverestore/stream_format.h new file +6 -0
@@ -0,0 +1,158 @@
+#ifndef __STREAM_FORMAT__H
+#define __STREAM_FORMAT__H
+
+#include <inttypes.h>
+
+/* TODO - find somewhere more appropriate. rec_tsc_info needs it for 64bit */

You seem to use it on all of them though. Can we not use explicit padding, as you seem to do for many of the other structs?

Quoted 5 lines

The issue with rec_tsc_info is its trailing alignment. i.e. sizeof(rec_tsc_info) is different between 32 and 64 bit builds, yet all fields are at the same offset from the beginning. Putting explicit trailing padding would invalidate the current code writing sizeof(rec_tsc_info) bytes into the stream.

Putting it inside the struct, like you do elsewhere, would not have this problem I think.

Quoted 6 lines

This particular bit is to match the spec, but I see what you mean. I shall make it consistently upper case.

I hadn't actually considered an enum. It might be ok as each type field is passed as a uint32_t, although changing it would be faff for no appreciable gain.

Yes, if you can't change the variables which store it there isn't much gain. If you can change the variables then it lets you use switch statements to make sure you handle all cases, but that probably doesn't justify the churn.

Ian.

#11 lore raw

Re: [PATCH v4 3/9] tools/libxc: Stream specification and some common code

Quoted 10 lines · Ian Campbell
Quoted 9 lines · Andrew Cooper
Quoted 9 lines

tools/libxc/saverestore/stream_format.h line 6

Quoted 7 lines
tools/libxc/saverestore/stream_format.h new file +6 -0
@@ -0,0 +1,158 @@
+#ifndef __STREAM_FORMAT__H
+#define __STREAM_FORMAT__H
+
+#include <inttypes.h>
+
+/* TODO - find somewhere more appropriate. rec_tsc_info needs it for 64bit */

You seem to use it on all of them though. Can we not use explicit padding, as you seem to do for many of the other structs?

Quoted 7 lines
Quoted 5 lines

The issue with rec_tsc_info is its trailing alignment. i.e. sizeof(rec_tsc_info) is different between 32 and 64 bit builds, yet all fields are at the same offset from the beginning. Putting explicit trailing padding would invalidate the current code writing sizeof(rec_tsc_info) bytes into the stream.

Putting it inside the struct, like you do elsewhere, would not have this problem I think.

But it changes the value in the record length header even if it doesn't change the content of the record in the stream. The issue here is between implicit trailing 0s for alignment purposes (which are not counted in the record length), and an explicit trailing _resvd field (which is counted in the record length).

Talking a literal interpretation of the spec, there is a _resvd field, meaning that the record length should indeed be 4 bytes longer than the current implementation.

If we are dropping the __packed attributes, I would want some XC_BUILD_BUG_ON()s to prevent any further surprises (guess how I discovered this surprise to start with ;p), but where to stick those is not obvious.

Might it be acceptable to have a single .c file containing stuff like this which is compiled but not linked?

~Andrew

#12 lore raw

PATCH v4 4/9 tools/libxc: Scripts for inspection/valdiation of legacy and new streams

In reply to Andrew Cooper
---
 tools/libxc/saverestore/scripts/generate.py        |   59 +++
 .../libxc/saverestore/scripts/inspect-legacy32.py  |  167 +++++++++
 tools/libxc/saverestore/scripts/streamspec.py      |  106 ++++++
 tools/libxc/saverestore/scripts/verify.py          |  385 ++++++++++++++++++++
 4 files changed, 717 insertions(+)
 create mode 100755 tools/libxc/saverestore/scripts/generate.py
 create mode 100755 tools/libxc/saverestore/scripts/inspect-legacy32.py
 create mode 100644 tools/libxc/saverestore/scripts/streamspec.py
 create mode 100755 tools/libxc/saverestore/scripts/verify.py
tools/libxc/saverestore/scripts/generate.py new file +59 -0
new file mode 100755
index 0000000..3b01e65
@@ -0,0 +1,59 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from streamspec import *
+import struct, sys
+
+ihdr = struct.pack(IHDR_FORMAT,
+                   0xffffffffffffffff, # Marker
+                   IHDR_IDENT,         # "XENF" in ASCII
+                   1,                  # Version
+                   IHDR_OPT_LE,        # Options
+                   0, 0                # Reserved
+                   )
+
+def emit_record(type, data):
+    length = len(data)
+
+    r = struct.pack(RH_FORMAT, type, length)
+    r += data
+
+    padding_len = (8 - (length & 7)) & 7
+    r += '\x00' * padding_len
+
+    sys.stdout.write(r)
+
+
+def emit_pv():
+
+    dhdr = struct.pack(DHDR_FORMAT,
+                       DHDR_TYPE_x86_pv,  # Type
+                       12, # Page size
+                       0,  # Reserved
+                       4,  # Xen major
+                       5   # Xen minor
+                       )
+
+    sys.stdout.write(ihdr)
+    sys.stdout.write(dhdr)
+
+    x86_pv_info = struct.pack(X86_PV_INFO_FORMAT,
+                              8, # Guest width
+                              4, # Guest levels
+                              0  # Options
+                              )
+
+    emit_record(REC_TYPE_x86_pv_info, x86_pv_info)
+    emit_record(REC_TYPE_end, "")
+    return 0
+
+
+def main(argv = sys.argv):
+
+    if len(argv) == 0 or argv[0] == "pv":
+        return emit_pv()
+    else:
+        return 1
+
+if __name__ == "__main__":
+    sys.exit(main(sys.argv[1:]))
tools/libxc/saverestore/scripts/inspect-legacy32.py new file +167 -0

167 lines not on this page · raw message

tools/libxc/saverestore/scripts/streamspec.py new file +106 -0

106 lines not on this page · raw message

tools/libxc/saverestore/scripts/verify.py new file +385 -0

385 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#13 lore raw

Re: [PATCH v4 4/9] tools/libxc: Scripts for inspection/valdiation of legacy and new streams

On Wed, 2014-04-30 at 19:36 +0100, Andrew Cooper wrote:

I suppose these are for debugging/inspecting rather than to be used in practice? In which case I don't think there's any need for me to review especially.

My only thought is that the need to repeat all of the data types is a bit unfortunate and risks bugs and/or deviation. I can well imagine you don't want to invent up an IDL or anything, is there anything out there which might suffice? SWIG seems like overkill.
http://code.google.com/p/ctypesgen/ perhaps?

("these debug tools are not important enough to stress over" is a valid rebuttal here IMHO).

#14 lore raw

Re: [PATCH v4 4/9] tools/libxc: Scripts for inspection/valdiation of legacy and new streams

Quoted 5 lines · Ian Campbell

On Wed, 2014-04-30 at 19:36 +0100, Andrew Cooper wrote:

I suppose these are for debugging/inspecting rather than to be used in practice? In which case I don't think there's any need for me to review especially.

It's primarily for validating that saved images comply with the spec and thus it does need to be easy to keep maintained.

Quoted 5 lines

My only thought is that the need to repeat all of the data types is a bit unfortunate and risks bugs and/or deviation. I can well imagine you don't want to invent up an IDL or anything, is there anything out there which might suffice? SWIG seems like overkill.
http://code.google.com/p/ctypesgen/ perhaps?

I think this is a valid concern.

David

#15 lore raw

Re: [PATCH v4 4/9] tools/libxc: Scripts for inspection/valdiation of legacy and new streams

In reply to Ian Campbell
Quoted 6 lines · Ian Campbell

On Wed, 2014-04-30 at 19:36 +0100, Andrew Cooper wrote:

I suppose these are for debugging/inspecting rather than to be used in practice? In which case I don't think there's any need for me to review especially.

Quoted 10 lines

My only thought is that the need to repeat all of the data types is a bit unfortunate and risks bugs and/or deviation. I can well imagine you don't want to invent up an IDL or anything, is there anything out there which might suffice? SWIG seems like overkill.
http://code.google.com/p/ctypesgen/ perhaps?

("these debug tools are not important enough to stress over" is a valid rebuttal here IMHO).

Since submitting this series, I have discarded "inspect-legacy32.py". "generate.py" is also debugging which can safely disappear when this series becomes non-RFC

I expect validate.py to remain even if only as a developer script.

The new "legacy.py" (name subject to sensible suggestions) is the converstion tool from legacy migration streams. Given two fds, it picks apart the legacy format and writes a v2 stream.

Both legacy.py and validate.py depend on streamspec.py as the stream specification. The risk of divergence from stream_format.h is a concern, but streamspec.py also includes type->"str" dictionaries to match the lookup functions in common.c. On the other hand, I cant see any way to generate one from the other without radically changing the use of python struct packing strings, which is the RightWay to do the python side of things.

~Andrew

#16 lore raw

PATCH v4 5/9 tools/libxc: common code

In reply to Andrew Cooper
---
 tools/libxc/saverestore/common.c         |   87 ++++++
 tools/libxc/saverestore/common.h         |  172 ++++++++++++
 tools/libxc/saverestore/common_x86.c     |   54 ++++
 tools/libxc/saverestore/common_x86.h     |   21 ++
 tools/libxc/saverestore/common_x86_hvm.c |   53 ++++
 tools/libxc/saverestore/common_x86_pv.c  |  431 ++++++++++++++++++++++++++++++
 tools/libxc/saverestore/common_x86_pv.h  |  104 +++++++
 tools/libxc/saverestore/restore.c        |  288 ++++++++++++++++++++
 tools/libxc/saverestore/save.c           |   42 +++
 9 files changed, 1252 insertions(+)
 create mode 100644 tools/libxc/saverestore/common_x86.c
 create mode 100644 tools/libxc/saverestore/common_x86.h
 create mode 100644 tools/libxc/saverestore/common_x86_hvm.c
 create mode 100644 tools/libxc/saverestore/common_x86_pv.c
 create mode 100644 tools/libxc/saverestore/common_x86_pv.h
tools/libxc/saverestore/common.c +87 -0
index de2e727..b159c4c 100644
@@ -1,3 +1,5 @@
+#include <assert.h>
+
 #include "common.h"
 
 static const char *dhdr_types[] =
@@ -52,6 +54,91 @@ const char *rec_type_to_str(uint32_t type)
     return "Reserved";
 }
 
+int write_split_record(struct context *ctx, struct record *rec,
+                       void *buf, size_t sz)
+{
+    static const char zeroes[7] = { 0 };
+    xc_interface *xch = ctx->xch;
+    uint32_t combined_length = rec->length + sz;
+    size_t record_length = (combined_length + 7) & ~7UL;
+
+    if ( record_length > REC_LENGTH_MAX )
+    {
+        ERROR("Record (0x%08"PRIx32", %s) length 0x%"PRIx32
+              " exceeds max (0x%"PRIx32")", rec->type,
+              rec_type_to_str(rec->type), rec->length, REC_LENGTH_MAX);
+        return -1;
+    }
+
+    if ( rec->length )
+        assert(rec->data);
+    if ( sz )
+        assert(buf);
+
+    if ( write_exact(ctx->fd, &rec->type, sizeof rec->type) ||
+         write_exact(ctx->fd, &combined_length, sizeof rec->length) ||
+         (rec->length && write_exact(ctx->fd, rec->data, rec->length)) ||
+         (sz && write_exact(ctx->fd, buf, sz)) ||
+         write_exact(ctx->fd, zeroes, record_length - combined_length) )
+    {
+        PERROR("Unable to write record to stream");
+        return -1;
+    }
+
+    return 0;
+}
+
+int read_record(struct context *ctx, struct record *rec)
+{
+    xc_interface *xch = ctx->xch;
+    struct rhdr rhdr;
+    size_t datasz;
+
+    if ( read_exact(ctx->fd, &rhdr, sizeof rhdr) )
+    {
+        PERROR("Failed to read Record Header from stream");
+        return -1;
+    }
+    else if ( rhdr.length > REC_LENGTH_MAX )
+    {
+        ERROR("Record (0x%08"PRIx32", %s) length 0x%"PRIx32
+              " exceeds max (0x%"PRIx32")",
+              rhdr.type, rec_type_to_str(rhdr.type),
+              rhdr.length, REC_LENGTH_MAX);
+        return -1;
+    }
+
+    datasz = (rhdr.length + 7) & ~7U;
+
+    if ( datasz )
+    {
+        rec->data = malloc(datasz);
+
+        if ( !rec->data )
+        {
+            ERROR("Unable to allocate %zu bytes for record data (0x%08"PRIx32", %s)",
+                  datasz, rhdr.type, rec_type_to_str(rhdr.type));
+            return -1;
+        }
+
+        if ( read_exact(ctx->fd, rec->data, datasz) )
+        {
+            free(rec->data);
+            rec->data = NULL;
+            PERROR("Failed to read %zu bytes of data for record (0x%08"PRIx32", %s)",
+                   datasz, rhdr.type, rec_type_to_str(rhdr.type));
+            return -1;
+        }
+    }
+    else
+        rec->data = NULL;
+
+    rec->type   = rhdr.type;
+    rec->length = rhdr.length;
+
+    return 0;
+};
+
 /*
  * Local variables:
  * mode: C
tools/libxc/saverestore/common.h +172 -0

185 lines not on this page · raw message

tools/libxc/saverestore/common_x86.c new file +54 -0

54 lines not on this page · raw message

tools/libxc/saverestore/common_x86.h new file +21 -0
new file mode 100644
index 0000000..429532a
@@ -0,0 +1,21 @@
+#ifndef __COMMON_X86__H
+#define __COMMON_X86__H
+
+#include "common.h"
+
+/* Obtains and writes domain TSC information to the stream */
+int write_tsc_info(struct context *ctx);
+
+/* Parses domain TSC information from the stream */
+int handle_tsc_info(struct context *ctx, struct record *rec);
+
+#endif
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
tools/libxc/saverestore/common_x86_hvm.c new file +53 -0

53 lines not on this page · raw message

tools/libxc/saverestore/common_x86_pv.c new file +431 -0

431 lines not on this page · raw message

tools/libxc/saverestore/common_x86_pv.h new file +104 -0

104 lines not on this page · raw message

tools/libxc/saverestore/restore.c +288 -0

294 lines not on this page · raw message

tools/libxc/saverestore/save.c +42 -0

47 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#17 lore raw

Re: [PATCH v4 5/9] tools/libxc: common code

Quoted 31 lines · Andrew Cooper
---
 tools/libxc/saverestore/common.c         |   87 ++++++
 tools/libxc/saverestore/common.h         |  172 ++++++++++++
 tools/libxc/saverestore/common_x86.c     |   54 ++++
 tools/libxc/saverestore/common_x86.h     |   21 ++
 tools/libxc/saverestore/common_x86_hvm.c |   53 ++++
 tools/libxc/saverestore/common_x86_pv.c  |  431 ++++++++++++++++++++++++++++++
 tools/libxc/saverestore/common_x86_pv.h  |  104 +++++++
 tools/libxc/saverestore/restore.c        |  288 ++++++++++++++++++++
 tools/libxc/saverestore/save.c           |   42 +++
 9 files changed, 1252 insertions(+)
 create mode 100644 tools/libxc/saverestore/common_x86.c
 create mode 100644 tools/libxc/saverestore/common_x86.h
 create mode 100644 tools/libxc/saverestore/common_x86_hvm.c
 create mode 100644 tools/libxc/saverestore/common_x86_pv.c
 create mode 100644 tools/libxc/saverestore/common_x86_pv.h
tools/libxc/saverestore/common.c +2 -0
index de2e727..b159c4c 100644
@@ -1,3 +1,5 @@
+#include <assert.h>
+
 #include "common.h"
 
 static const char *dhdr_types[] =

tools/libxc/saverestore/common.c const char *rec_type_to_str(uint32_t type) · line 63

Quoted 10 lines
tools/libxc/saverestore/common.c +7 -0
@@ -52,6 +54,91 @@ const char *rec_type_to_str(uint32_t type)
     return "Reserved";
 }
 
+int write_split_record(struct context *ctx, struct record *rec,
+                       void *buf, size_t sz)
+{
+    static const char zeroes[7] = { 0 };
+    xc_interface *xch = ctx->xch;
+    uint32_t combined_length = rec->length + sz;
+    size_t record_length = (combined_length + 7) & ~7UL;

Isn't this ROUNDUP(combined_length, 3)? (Where 3 and/or 7 perhaps ought to be given names)

Quoted 5 lines
tools/libxc/saverestore/common.c +5 -0
+
+    if ( record_length > REC_LENGTH_MAX )
+    {
+        ERROR("Record (0x%08"PRIx32", %s) length 0x%"PRIx32
+              " exceeds max (0x%"PRIx32")", rec->type,

tools/libxc/saverestore/common.c const char *rec_type_to_str(uint32_t type) · line 78

Quoted 10 lines
tools/libxc/saverestore/common.c +10 -0
+              rec_type_to_str(rec->type), rec->length, REC_LENGTH_MAX);
+        return -1;
+    }
+
+    if ( rec->length )
+        assert(rec->data);
+    if ( sz )
+        assert(buf);
+
+    if ( write_exact(ctx->fd, &rec->type, sizeof rec->type) ||

libxc style appears to be "sizeof (rec->type)" (the space seems odd to me but is apparently The Style, I think the brackets are a must).

Quoted 23 lines
tools/libxc/saverestore/common.c +23 -0
+         write_exact(ctx->fd, &combined_length, sizeof rec->length) ||
+         (rec->length && write_exact(ctx->fd, rec->data, rec->length)) ||
+         (sz && write_exact(ctx->fd, buf, sz)) ||
+         write_exact(ctx->fd, zeroes, record_length - combined_length) )
+    {
+        PERROR("Unable to write record to stream");
+        return -1;
+    }
+
+    return 0;
+}
+
+int read_record(struct context *ctx, struct record *rec)
+{
+    xc_interface *xch = ctx->xch;
+    struct rhdr rhdr;
+    size_t datasz;
+
+    if ( read_exact(ctx->fd, &rhdr, sizeof rhdr) )
+    {
+        PERROR("Failed to read Record Header from stream");
+        return -1;
+    }

tools/libxc/saverestore/common.c const char *rec_type_to_str(uint32_t type) · line 111

Quoted 10 lines
tools/libxc/saverestore/common.c +10 -0
+    else if ( rhdr.length > REC_LENGTH_MAX )
+    {
+        ERROR("Record (0x%08"PRIx32", %s) length 0x%"PRIx32
+              " exceeds max (0x%"PRIx32")",
+              rhdr.type, rec_type_to_str(rhdr.type),
+              rhdr.length, REC_LENGTH_MAX);
+        return -1;
+    }
+
+    datasz = (rhdr.length + 7) & ~7U;

Another ROUNDUP and #define XXX 3 or 7 opportunity. (I'm not going to mention any more of these I find).

Is it not a bug in the input for datasz to not be aligned? I thought you were padding everything.

tools/libxc/saverestore/common.h line 6

Quoted 11 lines
tools/libxc/saverestore/common.h +3 -0
index fff0a39..a35eda7 100644
@@ -1,7 +1,20 @@
 #ifndef __COMMON__H
 #define __COMMON__H
 
+#include <stdbool.h>
+
+// Hack out junk from the namespace

... namespace, because/in order to...

(to prevent accidents I suppose?)

Quoted 25 lines
tools/libxc/saverestore/common.h +21 -0
+#define mfn_to_pfn __UNUSED_mfn_to_pfn
+#define pfn_to_mfn __UNUSED_pfn_to_mfn
+
 #include "../xg_private.h"
+#include "../xg_save_restore.h"
+#include "../xc_dom.h"
+#include "../xc_bitops.h"
+
+#undef mfn_to_pfn
+#undef pfn_to_mfn
+
 
[...]
 
+struct context
+{
+    xc_interface *xch;
+    uint32_t domid;
+    int fd;
+
+    xc_dominfo_t dominfo;
+
+    struct save_restore_ops ops;
+
+    union

tools/libxc/saverestore/common.h

Quoted 10 lines
tools/libxc/saverestore/common.h +10 -0
+    {
+        struct
+        {
+            /* From Image Header */
+            uint32_t format_version;
+
+            /* From Domain Header */
+            uint32_t guest_type;
+            uint32_t guest_page_size;
+

I suppose the following block is not from the Domain Header. /* Populate by builder */ or something?

[...]

tools/libxc/saverestore/common.h line 92

Quoted 10 lines
tools/libxc/saverestore/common.h +10 -0
+    union
+    {
+        struct
+        {
+            /* 4 or 8; 32 or 64 bit domain */
+            unsigned int width;
+            /* 3 or 4 pagetable levels */
+            unsigned int levels;
+
+

Deliberate double spacing? (You don't do it consistently) [...]

tools/libxc/saverestore/common.h line 126

Quoted 5 lines
tools/libxc/saverestore/common.h +5 -0
+};
+
+/*
+ * Write the image and domain headers to the stream.
+ * (to eventually make static in save.c)

Is this because of the 2/9 HACK to maintain both? So far it seems to me that the next iteration could drop that and go for the big switcheroo, but if not could we agree some tag for things which are specific to that in order to distinguish from longer term todos or hacky bits? (So I know which to ignore and which to consider)

Quoted 7 lines

+ */

tools/libxc/saverestore/common.h +4 -0
+int write_headers(struct context *ctx, uint16_t guest_type);
+
+extern struct save_restore_ops save_restore_ops_x86_pv;
+extern struct save_restore_ops save_restore_ops_x86_hvm;

From the union in the context struct I'm only expecting x86_pv in this

patch, so perhaps x86_hvm was intended to arrive later?

(a non-zero length commit message might have confirmed what was intended to be supplied here)

Quoted 10 lines

+/*
+ * Writes a split record to the stream, applying correct padding where
+ * appropriate. It is common when sending records containing blobs from Xen
+ * that the header and blob data are separate. This function accepts a second
+ * buffer and length, and will merge it with the main record when sending.
+ *
+ * Records with a non-zero length must provide a valid data field; records
+ * with a 0 length shall have their data field ignored.
+ *
+ * Returns 0 on success and non0 on failure.

"non-0". Do the non-zero values have any particular meanings? (errno codes, other error codes?)

Quoted 20 lines

+ */

tools/libxc/saverestore/common.h +19 -0
+int write_split_record(struct context *ctx, struct record *rec, void *buf, size_t sz);
+
+/*
+ * Writes a record to the stream, applying correct padding where appropriate.
+ * Records with a non-zero length must provide a valid data field; records
+ * with a 0 length shall have their data field ignored.
+ *
+ * Returns 0 on success and non0 on failure.
+ */
+static inline int write_record(struct context *ctx, struct record *rec)
+{
+    return write_split_record(ctx, rec, NULL, 0);
+}
+
+/*
+ * Reads a record from the stream, and fills in the record structure.
+ *
+ * Returns 0 on success and non-0 on failure.
+ *

tools/libxc/saverestore/common.h line 179

Quoted 10 lines
tools/libxc/saverestore/common.h +10 -0
+ * On success, the records type and size shall be valid.
+ * - If size is 0, data shall be NULL.
+ * - If size is non-0, data shall be a buffer allocated by malloc() which must
+ *   be passed to free() by the caller.
+ *
+ * On failure, the contents of the record structure are undefined.
+ */
+int read_record(struct context *ctx, struct record *rec);
+
+int write_page_data_and_pause(struct context *ctx);

I suppose the page_data referred to here is accumulated in ctx?

Quoted 1 line

+int handle_page_data(struct context *ctx, struct record *rec);

Handle it how? Is this the restore side counterpart to write_page_data_...?

tools/libxc/saverestore/common.h line 184

Quoted 2 lines
tools/libxc/saverestore/common.h +2 -0
+int populate_pfns(struct context *ctx, unsigned count,
+                  const xen_pfn_t *original_pfns, const uint32_t *types);

Populate from what?

(You spoiled me with doc comments on the first few, so I'm being picky here ;-))

tools/libxc/saverestore/common_x86_hvm.c

Quoted 9 lines
tools/libxc/saverestore/common.h +1 -0
+
 #endif
 /*
  * Local variables:
tools/libxc/saverestore/common_x86_hvm.c new file +0 -0
new file mode 100644
index 0000000..0b9aac2

How delightfully uninteresting.

tools/libxc/saverestore/common_x86_pv.c

Quoted 5 lines
tools/libxc/saverestore/common_x86_pv.c new file +0 -0
new file mode 100644
index 0000000..35bce27

How terrifying.

tools/libxc/saverestore/common_x86_pv.c line 7

Quoted 8 lines
tools/libxc/saverestore/common_x86_pv.c new file +7 -0
@@ -0,0 +1,431 @@
+#include <assert.h>
+
+#include "common_x86_pv.h"
+
+xen_pfn_t mfn_to_pfn(struct context *ctx, xen_pfn_t mfn)
+{
+    assert(mfn <= ctx->x86_pv.max_mfn);

Just to confirm that anywhere there is an assert like this then if it is based on potentially user controller input then it has already been validated elsewhere first? (with the abstraction I couldn't spot where that was)

Quoted 8 lines
tools/libxc/saverestore/common_x86_pv.c new file +8 -0
+    return ctx->x86_pv.m2p[mfn];
+}
+
+static bool x86_pv_pfn_is_valid(struct context *ctx, xen_pfn_t pfn)
+{
+    return pfn <= ctx->x86_pv.max_pfn;
+}
+

tools/libxc/saverestore/common_x86_pv.c line 25

Quoted 10 lines
tools/libxc/saverestore/common_x86_pv.c new file +10 -0
+static xen_pfn_t x86_pv_pfn_to_gfn(struct context *ctx, xen_pfn_t pfn)
+{
+    assert(pfn <= ctx->x86_pv.max_pfn);
+
+    if ( ctx->x86_pv.width == sizeof (uint64_t) )
+        /* 64 bit guest.  Need to truncate their pfns for 32 bit toolstacks */
+        return ((uint64_t *)ctx->x86_pv.p2m)[pfn];
+    else
+    {
+        /* 32 bit guest.  Need to expand INVALID_MFN fot 64 bit toolstacks */

Typo: "fot"

Quoted 14 lines
tools/libxc/saverestore/common_x86_pv.c new file +14 -0
+        uint32_t mfn = ((uint32_t *)ctx->x86_pv.p2m)[pfn];
+
+        return mfn == ~0U ? INVALID_MFN : mfn;
+    }
+}
+
+static void x86_pv_set_page_type(struct context *ctx, xen_pfn_t pfn,
+                                 unsigned long type)
+{
+    assert(pfn <= ctx->x86_pv.max_pfn);
+
+    ctx->x86_pv.pfn_types[pfn] = type;
+}
+

tools/libxc/saverestore/common_x86_pv.c line 49

Quoted 10 lines
tools/libxc/saverestore/common_x86_pv.c new file +10 -0
+static void x86_pv_set_gfn(struct context *ctx, xen_pfn_t pfn,
+                           xen_pfn_t mfn)
+{
+    assert(pfn <= ctx->x86_pv.max_pfn);
+
+    if ( ctx->x86_pv.width == sizeof (uint64_t) )
+        /* 64 bit guest.  Need to expand INVALID_MFN for 32 bit toolstacks */
+        ((uint64_t *)ctx->x86_pv.p2m)[pfn] = mfn == INVALID_MFN ? ~0ULL : mfn;
+    else
+        /* 32 bit guest.  Can safely truncate INVALID_MFN fot 64 bit toolstacks */

Another fot.

Quoted 5 lines
tools/libxc/saverestore/common_x86_pv.c new file +5 -0
+        ((uint32_t *)ctx->x86_pv.p2m)[pfn] = mfn;
+}
+
+static int normalise_pagetable(struct context *ctx, const uint64_t *src,
+                               uint64_t *dst, unsigned long type)

tools/libxc/saverestore/common_x86_pv.c line 64

Quoted 10 lines
tools/libxc/saverestore/common_x86_pv.c new file +10 -0
+{
+    xc_interface *xch = ctx->xch;
+    uint64_t pte;
+    unsigned i, xen_first = -1, xen_last = -1; /* Indicies of Xen mappings */
+
+    type &= XEN_DOMCTL_PFINFO_LTABTYPE_MASK;
+
+    if ( ctx->x86_pv.levels == 4 )
+    {
+        /* 64bit guests only have Xen mappings in their L4 tables */

You (correctly) infer from the pt levels that this must be a 64-bit guest. Assert?

tools/libxc/saverestore/common_x86_pv.c line 68

Quoted 4 lines
tools/libxc/saverestore/common_x86_pv.c new file +4 -0
+        if ( type == XEN_DOMCTL_PFINFO_L4TAB )
+        {
+            xen_first = 256;
+            xen_last = 271;

Can we #define all these numbers please?

tools/libxc/saverestore/common_x86_pv.c line 24

Quoted 4 lines
tools/libxc/saverestore/common_x86_pv.c new file +4 -0
+        }
+    }
+    else
+    {

Must be 32 bit? Assert?

Quoted 7 lines
tools/libxc/saverestore/common_x86_pv.c new file +7 -0
+        switch ( type )
+        {
+        case XEN_DOMCTL_PFINFO_L4TAB:
+            ERROR("??? Found L4 table for 32bit guest");
+            errno = EINVAL;
+            return -1;
+

tools/libxc/saverestore/common_x86_pv.c line 89

Quoted 10 lines
tools/libxc/saverestore/common_x86_pv.c new file +10 -0
+        case XEN_DOMCTL_PFINFO_L3TAB:
+            /* 32bit guests can only use the first 4 entries of their L3 tables.
+             * All other are potentially used by Xen. */
+            xen_first = 4;
+            xen_last = 512;
+            break;
+
+        case XEN_DOMCTL_PFINFO_L2TAB:
+            /* It is hard to spot Xen mappings in a 32bit guest's L2.  Most
+             * are normal but only a few will have Xen mappings.

Internally Xen has PGT_pae_xen_l2, I wonder if it could be coaxed into exposing that here too?

tools/libxc/saverestore/common_x86_pv.c line 93

Quoted 3 lines
tools/libxc/saverestore/common_x86_pv.c new file +3 -0
+             * 428 = (HYPERVISOR_VIRT_START_PAE >> L2_PAGETABLE_SHIFT_PAE) & 0x1ff
+             *
+             * ...which is conveniently unavailable to us in a 64bit build.

Can we export it somehow? (Perhaps with COMPAT in the name)

Quoted 2 lines

... normalize_page
+ local_page = malloc(PAGE_SIZE);

How bad is the overhead of (what I pressume are) all these allocs and frees? (I've not come across the caller in this patch, maybe it will become clear later).

Would it be worth keeping an array around shadowing the "live" batch of pages?

Quoted 1 line

+static int x86_pv_localise_page(struct context *ctx, uint32_t type, void *page)

"localise" means the inverse of "normalise"?

tools/libxc/saverestore/common_x86_pv.c line 253

Quoted 6 lines
tools/libxc/saverestore/common_x86_pv.c new file +6 -0
+void pseudophysmap_walk(struct context *ctx, xen_pfn_t mfn)
+{
+    xc_interface *xch = ctx->xch;
+    xen_pfn_t pfn = ~0UL;
+
+    ERROR("mfn %#lx, max %#lx", mfn, ctx->x86_pv.max_mfn);

It took me a while to figure out why these were all errors. Can we call the function dump_bad_pseudophysmap_walk or some such?

tools/libxc/saverestore/common_x86_pv.c line 272

Quoted 6 lines
tools/libxc/saverestore/common_x86_pv.c new file +6 -0
+xen_pfn_t cr3_to_mfn(struct context *ctx, uint64_t cr3)
+{
+    if ( ctx->x86_pv.width == 8 )
+        return cr3 >> 12;
+    else
+        return (((uint32_t)cr3 >> 12) | ((uint32_t)cr3 << 20));

A comment pointing people to the phrase "extended-cr3" as grep fodder would be appreciated.

Quoted 10 lines
tools/libxc/saverestore/common_x86_pv.c new file +10 -0
+}
+
+uint64_t mfn_to_cr3(struct context *ctx, xen_pfn_t mfn)
+{
+    if ( ctx->x86_pv.width == 8 )
+        return ((uint64_t)mfn) << 12;
+    else
+        return (((uint32_t)mfn << 12) | ((uint32_t)mfn >> 20));
+}
+

[...]

Quoted 1 line

+ else if ( guest_width == 4 )

Would prefer to drop the else and insert a blank line to separate the error handling of the xc_domain_.. from the interpretation of its result.

tools/libxc/saverestore/common_x86_pv.c line 301

Quoted 6 lines
tools/libxc/saverestore/common_x86_pv.c new file +6 -0
+        guest_levels = 3;
+    else if ( guest_width == 8 )
+        guest_levels = 4;
+    else
+    {
+        ERROR("Invalid guest width %d.  Expected 32 or 64", guest_width);

Actually you expected 4 or 8. I'm not sure if this could be strengthened into an assert.

tools/libxc/saverestore/common_x86_pv.c line 310

Quoted 9 lines
tools/libxc/saverestore/common_x86_pv.c new file +9 -0
+        return -1;
+    }
+    ctx->x86_pv.width = guest_width;
+    ctx->x86_pv.levels = guest_levels;
+    ctx->x86_pv.fpp = fpp = PAGE_SIZE / ctx->x86_pv.width;
+
+    DPRINTF("%d bits, %d levels", guest_width * 8, guest_levels);
+
+    /* Get the domains maximum pfn */

Apostrophes are out of fashion at the moment I guess ;-).

tools/libxc/saverestore/common_x86_pv.c line 320

Quoted 10 lines
tools/libxc/saverestore/common_x86_pv.c new file +10 -0
+    max_pfn = xc_domain_maximum_gpfn(xch, ctx->domid);
+    if ( max_pfn < 0 )
+    {
+        PERROR("Unable to obtain guests max pfn");
+        return -1;
+    }
+    else if ( max_pfn >= ~XEN_DOMCTL_PFINFO_LTAB_MASK )
+    {
+        errno = E2BIG;
+        PERROR("Cannot save a guest this large %#x");

Missing the parameter for %#x.

tools/libxc/saverestore/common_x86_pv.c

Quoted 11 lines

[...]

tools/libxc/saverestore/common_x86_pv.c new file +9 -0
+/*
+ * Extract an MFN from a Pagetable Entry.
+ */
+static inline xen_pfn_t pte_to_frame(struct context *ctx, uint64_t pte)
+{
+    if ( ctx->x86_pv.width == 8 )
+        return (pte >> PAGE_SHIFT) & ((1ULL << (52 - PAGE_SHIFT)) - 1);
+    else
+        return (pte >> PAGE_SHIFT) & ((1ULL << (44 - PAGE_SHIFT)) - 1);

Are bits 45..52 non zero in this case?

Can we #define a couple of MASK's please.

Quoted 4 lines
tools/libxc/saverestore/common_x86_pv.c new file +4 -0
+static int pfn_set_populated(struct context *ctx, xen_pfn_t pfn)
+{
+    xc_interface *xch = ctx->xch;
+

tools/libxc/saverestore/common_x86_pv.c

Quoted 10 lines
tools/libxc/saverestore/common_x86_pv.c new file +10 -0
+    if ( !ctx->restore.populated_pfns || pfn > ctx->restore.max_populated_pfn )
+    {
+        unsigned long new_max_pfn = ((pfn + 1024) & ~1023) - 1;
+        size_t old_sz, new_sz;
+        unsigned long *p;
+
+        old_sz = bitmap_size(ctx->restore.max_populated_pfn + 1);
+        new_sz = bitmap_size(new_max_pfn + 1);
+
+        p  = realloc(ctx->restore.populated_pfns, new_sz);

In practice do you not see an initial run of increasing pfns and end up constantly adding one more? Consider doubling or adding e.g. 128M worth of space when growing?

Quoted 11 lines
tools/libxc/saverestore/common_x86_pv.c new file +11 -0
+
+    if ( nr_pages > 0 )
+    {
+        mapping = guest_page = xc_map_foreign_bulk(
+            xch, ctx->domid, PROT_READ | PROT_WRITE,
+            mfns, map_errs, nr_pages);
+        if ( !mapping )
+        {
+            PERROR("Unable to map %u mfns for %u pages of data",
+                   nr_pages, count);
+            goto err;

tools/libxc/saverestore/common_x86_pv.c

Quoted 10 lines
tools/libxc/saverestore/common_x86_pv.c new file +10 -0
+        }
+    }
+
+    for ( i = 0, j = 0; i < count; ++i )
+    {
+        switch ( types[i] )
+        {
+        case XEN_DOMCTL_PFINFO_XTAB:
+        case XEN_DOMCTL_PFINFO_BROKEN:
+            /* Nothing at all to do */

Is this a deliberate fall-thru? Please comment if so.

tools/libxc/saverestore/common_x86_pv.c

Quoted 2 lines
tools/libxc/saverestore/common_x86_pv.c new file +2 -0
+        case XEN_DOMCTL_PFINFO_XALLOC:
+            /* Nothing futher to do */

"further"

Quoted 10 lines
tools/libxc/saverestore/common_x86_pv.c new file +10 -0
+int handle_page_data(struct context *ctx, struct record *rec)
+{
+    xc_interface *xch = ctx->xch;
+    struct rec_page_data_header *pages = rec->data;
+    unsigned i, pages_of_data = 0;
+    int rc = -1;
+
+    xen_pfn_t *pfns = NULL, pfn;
+    uint32_t *types = NULL, type;
+

tools/libxc/saverestore/common_x86_pv.c

Quoted 10 lines
tools/libxc/saverestore/common_x86_pv.c new file +10 -0
+    static unsigned pg_count;
+    pg_count++;
+
+    if ( rec->length < sizeof *pages )
+    {
+        ERROR("PAGE_DATA record trucated: length %"PRIu32", min %zu",
+              rec->length, sizeof *pages);
+        goto err;
+    }
+    else if ( pages->count < 1 )

We mostly omit the else where the previous if returns or gotos etc.

tools/libxc/saverestore/common_x86_pv.c

Quoted 12 lines
tools/libxc/saverestore/common_x86_pv.c new file +12 -0
+    for ( i = 0; i < pages->count; ++i )
+    {
+        pfn = pages->pfn[i] & PAGE_DATA_PFN_MASK;
+        if ( !ctx->ops.pfn_is_valid(ctx, pfn) )
+        {
+            ERROR("pfn %#lx (index %u) outside domain maximum", pfn, i);
+            goto err;
+        }
+
+        type = (pages->pfn[i] & PAGE_DATA_TYPE_MASK) >> 32;
+        if ( ((type >> XEN_DOMCTL_PFINFO_LTAB_SHIFT) >= 5) &&
+             ((type >> XEN_DOMCTL_PFINFO_LTAB_SHIFT) <= 8) )

What are 5 and 8?

This might all be clearer with the use of a #define or two, and perhaps using a switch over the expected valid types and explicitly invalid types.

Quoted 5 lines
tools/libxc/saverestore/save.c +0 -0
index c013e62..e842e6c 100644

tools/libxc/saverestore/save.c line 10

Quoted 10 lines
tools/libxc/saverestore/save.c +8 -0
@@ -1,5 +1,47 @@
+#include <arpa/inet.h>
+
 #include "common.h"
 
+int write_headers(struct context *ctx, uint16_t guest_type)
+{
+    xc_interface *xch = ctx->xch;
+    int32_t xen_version = xc_version(xch, XENVER_version, NULL);
+    struct ihdr ihdr =
+        {

I think coding style puts this on the previous line and the body should be 4 spaces further in.

tools/libxc/saverestore/save.c line 19

Quoted 9 lines
tools/libxc/saverestore/save.c +9 -0
+            .marker  = IHDR_MARKER,
+            .id      = htonl(IHDR_ID),
+            .version = htonl(IHDR_VERSION),
+            .options = htons(IHDR_OPT_LITTLE_ENDIAN),
+        };
+    struct dhdr dhdr =
+        {
+            .type       = guest_type,
+            .page_shift = 12,

We have a define for this.

Ian.

#18 lore raw

Re: [PATCH v4 5/9] tools/libxc: common code

Quoted 35 lines · Ian Campbell
Quoted 34 lines · Andrew Cooper
---
 tools/libxc/saverestore/common.c         |   87 ++++++
 tools/libxc/saverestore/common.h         |  172 ++++++++++++
 tools/libxc/saverestore/common_x86.c     |   54 ++++
 tools/libxc/saverestore/common_x86.h     |   21 ++
 tools/libxc/saverestore/common_x86_hvm.c |   53 ++++
 tools/libxc/saverestore/common_x86_pv.c  |  431 ++++++++++++++++++++++++++++++
 tools/libxc/saverestore/common_x86_pv.h  |  104 +++++++
 tools/libxc/saverestore/restore.c        |  288 ++++++++++++++++++++
 tools/libxc/saverestore/save.c           |   42 +++
 9 files changed, 1252 insertions(+)
 create mode 100644 tools/libxc/saverestore/common_x86.c
 create mode 100644 tools/libxc/saverestore/common_x86.h
 create mode 100644 tools/libxc/saverestore/common_x86_hvm.c
 create mode 100644 tools/libxc/saverestore/common_x86_pv.c
 create mode 100644 tools/libxc/saverestore/common_x86_pv.h
tools/libxc/saverestore/common.c +2 -0
index de2e727..b159c4c 100644
@@ -1,3 +1,5 @@
+#include <assert.h>
+
 #include "common.h"
 
 static const char *dhdr_types[] =
@@ -52,6 +54,91 @@ const char *rec_type_to_str(uint32_t type)
     return "Reserved";
 }
 
Quoted 9 lines

tools/libxc/saverestore/common.c const char *rec_type_to_str(uint32_t type) · line 63

Quoted 7 lines
tools/libxc/saverestore/common.c +7 -0
+int write_split_record(struct context *ctx, struct record *rec,
+                       void *buf, size_t sz)
+{
+    static const char zeroes[7] = { 0 };
+    xc_interface *xch = ctx->xch;
+    uint32_t combined_length = rec->length + sz;
+    size_t record_length = (combined_length + 7) & ~7UL;

Isn't this ROUNDUP(combined_length, 3)? (Where 3 and/or 7 perhaps ought to be given names)

It is. I am not certain that 3/7 need specific named, given the specification mandating that all records have trailing 0s to align the subsequent record on an 8 byte boundary.

Quoted 10 lines
Quoted 10 lines
tools/libxc/saverestore/common.c +9 -0
+
+    if ( record_length > REC_LENGTH_MAX )
+    {
+        ERROR("Record (0x%08"PRIx32", %s) length 0x%"PRIx32
+              " exceeds max (0x%"PRIx32")", rec->type,
+              rec_type_to_str(rec->type), rec->length, REC_LENGTH_MAX);
+        return -1;
+    }
+
Quoted 8 lines

tools/libxc/saverestore/common.c const char *rec_type_to_str(uint32_t type) · line 78

Quoted 6 lines
tools/libxc/saverestore/common.c +6 -0
+    if ( rec->length )
+        assert(rec->data);
+    if ( sz )
+        assert(buf);
+
+    if ( write_exact(ctx->fd, &rec->type, sizeof rec->type) ||

libxc style appears to be "sizeof (rec->type)" (the space seems odd to me but is apparently The Style, I think the brackets are a must).

libxc style also states no extraneous brackets. sizeof is particularly problematic as it requires brackets when used with a type, and I keep on reverting back to my personal preference. I shall try to remain more consistent.

Quoted 37 lines
Quoted 24 lines
tools/libxc/saverestore/common.c +23 -0
+         write_exact(ctx->fd, &combined_length, sizeof rec->length) ||
+         (rec->length && write_exact(ctx->fd, rec->data, rec->length)) ||
+         (sz && write_exact(ctx->fd, buf, sz)) ||
+         write_exact(ctx->fd, zeroes, record_length - combined_length) )
+    {
+        PERROR("Unable to write record to stream");
+        return -1;
+    }
+
+    return 0;
+}
+
+int read_record(struct context *ctx, struct record *rec)
+{
+    xc_interface *xch = ctx->xch;
+    struct rhdr rhdr;
+    size_t datasz;
+
+    if ( read_exact(ctx->fd, &rhdr, sizeof rhdr) )
+    {
+        PERROR("Failed to read Record Header from stream");
+        return -1;
+    }

tools/libxc/saverestore/common.c const char *rec_type_to_str(uint32_t type) · line 111

Quoted 10 lines
tools/libxc/saverestore/common.c +10 -0
+    else if ( rhdr.length > REC_LENGTH_MAX )
+    {
+        ERROR("Record (0x%08"PRIx32", %s) length 0x%"PRIx32
+              " exceeds max (0x%"PRIx32")",
+              rhdr.type, rec_type_to_str(rhdr.type),
+              rhdr.length, REC_LENGTH_MAX);
+        return -1;
+    }
+
+    datasz = (rhdr.length + 7) & ~7U;

Another ROUNDUP and #define XXX 3 or 7 opportunity. (I'm not going to mention any more of these I find).

Quoted 2 lines

Is it not a bug in the input for datasz to not be aligned? I thought you were padding everything.

It is certainly not a bug. For the blobs which can have an arbitrary length, the record length field reflects the exact length. Trailing 0s are then inserted into the stream to align the start of the next record on an 8 byte boundary.

Here, the simple approach is to just read those trailing bytes into the buffer containing the data, so the malloc()d memory handed back from this function might be slightly longer than the length field indicates.

Quoted 9 lines
Quoted 9 lines
tools/libxc/saverestore/common.h +0 -0
index fff0a39..a35eda7 100644
@@ -1,7 +1,20 @@
 #ifndef __COMMON__H
 #define __COMMON__H
 
Quoted 6 lines

tools/libxc/saverestore/common.h line 6

Quoted 3 lines
tools/libxc/saverestore/common.h +3 -0
+#include <stdbool.h>
+
+// Hack out junk from the namespace

... namespace, because/in order to...

(to prevent accidents I suppose?)

because mfn_to_pfn and pfn_to_mfn are gross abortions of macros which look like regular functions but take implicit scoped state.

This list of junk is increased in subsequent patches.

Quoted 29 lines
Quoted 29 lines
tools/libxc/saverestore/common.h +25 -0
+#define mfn_to_pfn __UNUSED_mfn_to_pfn
+#define pfn_to_mfn __UNUSED_pfn_to_mfn
+
 #include "../xg_private.h"
+#include "../xg_save_restore.h"
+#include "../xc_dom.h"
+#include "../xc_bitops.h"
+
+#undef mfn_to_pfn
+#undef pfn_to_mfn
+
 
[...]
+struct context
+{
+    xc_interface *xch;
+    uint32_t domid;
+    int fd;
+
+    xc_dominfo_t dominfo;
+
+    struct save_restore_ops ops;
+
+    union
+    {
+        struct
+        {
+            /* From Image Header */
Quoted 8 lines

tools/libxc/saverestore/common.h line 59

Quoted 6 lines
tools/libxc/saverestore/common.h +6 -0
+            uint32_t format_version;
+
+            /* From Domain Header */
+            uint32_t guest_type;
+            uint32_t guest_page_size;
+

I suppose the following block is not from the Domain Header. /* Populate by builder */ or something?

Yes - I do have a task to insert more comments into the code.

Quoted 15 lines

[...]

tools/libxc/saverestore/common.h line 92

Quoted 10 lines
tools/libxc/saverestore/common.h +10 -0
+    union
+    {
+        struct
+        {
+            /* 4 or 8; 32 or 64 bit domain */
+            unsigned int width;
+            /* 3 or 4 pagetable levels */
+            unsigned int levels;
+
+

Deliberate double spacing? (You don't do it consistently) [...]

I have been collapsing patches from 3 separate people ;) I will do a consistency check before this becomes non-rfc.

Quoted 11 lines

tools/libxc/saverestore/common.h line 126

Quoted 6 lines
tools/libxc/saverestore/common.h +5 -0
+};
+
+/*
+ * Write the image and domain headers to the stream.
+ * (to eventually make static in save.c)

Is this because of the 2/9 HACK to maintain both? So far it seems to me that the next iteration could drop that and go for the big switcheroo, but if not could we agree some tag for things which are specific to that in order to distinguish from longer term todos or hacky bits? (So I know which to ignore and which to consider)

This is to support individual save_x86_pv.c and save_x86_hvm.c files before the next iteration which will make some more vm/arch hooks and one single save algorithm.

Quoted 11 lines

tools/libxc/saverestore/common.h line 131

Quoted 6 lines

+ */

tools/libxc/saverestore/common.h +4 -0
+int write_headers(struct context *ctx, uint16_t guest_type);
+
+extern struct save_restore_ops save_restore_ops_x86_pv;
+extern struct save_restore_ops save_restore_ops_x86_hvm;

From the union in the context struct I'm only expecting x86_pv in this patch, so perhaps x86_hvm was intended to arrive later?

(a non-zero length commit message might have confirmed what was intended to be supplied here)

Nope - I had an error when folding patches together.

Quoted 11 lines
Quoted 11 lines

+/*
+ * Writes a split record to the stream, applying correct padding where
+ * appropriate. It is common when sending records containing blobs from Xen
+ * that the header and blob data are separate. This function accepts a second
+ * buffer and length, and will merge it with the main record when sending.
+ *
+ * Records with a non-zero length must provide a valid data field; records
+ * with a 0 length shall have their data field ignored.
+ *
+ * Returns 0 on success and non0 on failure.

Quoted 2 lines

"non-0". Do the non-zero values have any particular meanings? (errno codes, other error codes?)

No - see also the other inconsistencies in libxc.

The current state of all the new functions are "0 on success, -1 on error and errno is unlikely be related". This is no worse than the vast majority of libxc currently, and stating "non-0" on error allows for a positive "libxc error" range as discussed down the pub.

I am not shaving that yak right now, but have tried to leave the code in a state where inserting consistent error handing should be less hassle than it otherwise could be.

In all error cases, the log will (should) have accurate information, including errno when relevent.

Quoted 24 lines
Quoted 24 lines

+ */

tools/libxc/saverestore/common.h +22 -0
+int write_split_record(struct context *ctx, struct record *rec, void *buf, size_t sz);
+
+/*
+ * Writes a record to the stream, applying correct padding where appropriate.
+ * Records with a non-zero length must provide a valid data field; records
+ * with a 0 length shall have their data field ignored.
+ *
+ * Returns 0 on success and non0 on failure.
+ */
+static inline int write_record(struct context *ctx, struct record *rec)
+{
+    return write_split_record(ctx, rec, NULL, 0);
+}
+
+/*
+ * Reads a record from the stream, and fills in the record structure.
+ *
+ * Returns 0 on success and non-0 on failure.
+ *
+ * On success, the records type and size shall be valid.
+ * - If size is 0, data shall be NULL.
+ * - If size is non-0, data shall be a buffer allocated by malloc() which must
Quoted 8 lines

tools/libxc/saverestore/common.h line 179

Quoted 7 lines

+ * be passed to free() by the caller.
+ *
+ * On failure, the contents of the record structure are undefined.
+ */

tools/libxc/saverestore/common.h +3 -0
+int read_record(struct context *ctx, struct record *rec);
+
+int write_page_data_and_pause(struct context *ctx);

I suppose the page_data referred to here is accumulated in ctx?

This was some of Davids code which I now realise breaks the unstated naming scheme I was working to.

Quoted 4 lines
Quoted 2 lines

+int handle_page_data(struct context *ctx, struct record *rec);

Handle it how? Is this the restore side counterpart to write_page_data_...?

Correct. handle_$RECORD_TYPE_NAME() (is supposed to) match write_$RECORD_TYPE_NAME()

Quoted 7 lines

tools/libxc/saverestore/common.h line 184

Quoted 3 lines
tools/libxc/saverestore/common.h +2 -0
+int populate_pfns(struct context *ctx, unsigned count,
+                  const xen_pfn_t *original_pfns, const uint32_t *types);

Populate from what?

(You spoiled me with doc comments on the first few, so I'm being picky here ;-))

:) You can probably work out the rough order of work based in the increasing scarcity of comments. I shall fix this for the next version. I was debating sorting it out before posting, but decided that posting something for review was better than waiting another few days (as I was already late on my guess for the next posting).

Quoted 11 lines

tools/libxc/saverestore/common_x86_hvm.c

Quoted 10 lines
tools/libxc/saverestore/common.h +1 -0
+
 #endif
 /*
  * Local variables:
tools/libxc/saverestore/common_x86_hvm.c new file +0 -0
new file mode 100644
index 0000000..0b9aac2

How delightfully uninteresting.

How curiously identical to arm :) (Not the only file this statement applies to...)

Quoted 7 lines

tools/libxc/saverestore/common_x86_pv.c

Quoted 6 lines
tools/libxc/saverestore/common_x86_pv.c new file +0 -0
new file mode 100644
index 0000000..35bce27

How terrifying.

and yet, more simple than the current legacy counterparts.

I have stripped out all legacy bits, like support for 2 level PV guests, and some vestigial code from the data with a 1G max VM size. (which would result in particularly fun results when attempting to migrate a PV guest on a host using the 44th bit in mfns)

I *think* my sanity is still intacked.

Quoted 13 lines

tools/libxc/saverestore/common_x86_pv.c line 7

Quoted 9 lines
tools/libxc/saverestore/common_x86_pv.c new file +7 -0
@@ -0,0 +1,431 @@
+#include <assert.h>
+
+#include "common_x86_pv.h"
+
+xen_pfn_t mfn_to_pfn(struct context *ctx, xen_pfn_t mfn)
+{
+    assert(mfn <= ctx->x86_pv.max_mfn);

Just to confirm that anywhere there is an assert like this then if it is based on potentially user controller input then it has already been validated elsewhere first? (with the abstraction I couldn't spot where that was)

It is validated in every case I am aware of, usually by "mfn_in_pseudophysmap()"

Quoted 12 lines
Quoted 12 lines
tools/libxc/saverestore/common_x86_pv.c new file +11 -0
+    return ctx->x86_pv.m2p[mfn];
+}
+
+static bool x86_pv_pfn_is_valid(struct context *ctx, xen_pfn_t pfn)
+{
+    return pfn <= ctx->x86_pv.max_pfn;
+}
+
+static xen_pfn_t x86_pv_pfn_to_gfn(struct context *ctx, xen_pfn_t pfn)
+{
+    assert(pfn <= ctx->x86_pv.max_pfn);
Quoted 8 lines

tools/libxc/saverestore/common_x86_pv.c line 25

Quoted 7 lines
tools/libxc/saverestore/common_x86_pv.c new file +7 -0
+
+    if ( ctx->x86_pv.width == sizeof (uint64_t) )
+        /* 64 bit guest.  Need to truncate their pfns for 32 bit toolstacks */
+        return ((uint64_t *)ctx->x86_pv.p2m)[pfn];
+    else
+    {
+        /* 32 bit guest.  Need to expand INVALID_MFN fot 64 bit toolstacks */

Typo: "fot"

Oops

Quoted 18 lines
Quoted 18 lines
tools/libxc/saverestore/common_x86_pv.c new file +17 -0
+        uint32_t mfn = ((uint32_t *)ctx->x86_pv.p2m)[pfn];
+
+        return mfn == ~0U ? INVALID_MFN : mfn;
+    }
+}
+
+static void x86_pv_set_page_type(struct context *ctx, xen_pfn_t pfn,
+                                 unsigned long type)
+{
+    assert(pfn <= ctx->x86_pv.max_pfn);
+
+    ctx->x86_pv.pfn_types[pfn] = type;
+}
+
+static void x86_pv_set_gfn(struct context *ctx, xen_pfn_t pfn,
+                           xen_pfn_t mfn)
+{
Quoted 8 lines

tools/libxc/saverestore/common_x86_pv.c line 49

Quoted 7 lines
tools/libxc/saverestore/common_x86_pv.c new file +7 -0
+    assert(pfn <= ctx->x86_pv.max_pfn);
+
+    if ( ctx->x86_pv.width == sizeof (uint64_t) )
+        /* 64 bit guest.  Need to expand INVALID_MFN for 32 bit toolstacks */
+        ((uint64_t *)ctx->x86_pv.p2m)[pfn] = mfn == INVALID_MFN ? ~0ULL : mfn;
+    else
+        /* 32 bit guest.  Can safely truncate INVALID_MFN fot 64 bit toolstacks */

Another fot.

Probably M-w C-y from above.

Quoted 10 lines
Quoted 10 lines
tools/libxc/saverestore/common_x86_pv.c new file +9 -0
+        ((uint32_t *)ctx->x86_pv.p2m)[pfn] = mfn;
+}
+
+static int normalise_pagetable(struct context *ctx, const uint64_t *src,
+                               uint64_t *dst, unsigned long type)
+{
+    xc_interface *xch = ctx->xch;
+    uint64_t pte;
+    unsigned i, xen_first = -1, xen_last = -1; /* Indicies of Xen mappings */
Quoted 8 lines

tools/libxc/saverestore/common_x86_pv.c line 64

Quoted 6 lines
tools/libxc/saverestore/common_x86_pv.c new file +6 -0
+
+    type &= XEN_DOMCTL_PFINFO_LTABTYPE_MASK;
+
+    if ( ctx->x86_pv.levels == 4 )
+    {
+        /* 64bit guests only have Xen mappings in their L4 tables */

You (correctly) infer from the pt levels that this must be a 64-bit guest. Assert?

Not really worth asserting.

Xen doesn't expose levels directly. It is calculated from the domain width and (in the past, domain abi string to differentiate 2 and 3 level guests) at the start of migration.

x86_pv_domain_info() assures that ctx->x86_pv.{width,levels} are consistent.

Quoted 6 lines

tools/libxc/saverestore/common_x86_pv.c line 68

Quoted 5 lines
tools/libxc/saverestore/common_x86_pv.c new file +4 -0
+        if ( type == XEN_DOMCTL_PFINFO_L4TAB )
+        {
+            xen_first = 256;
+            xen_last = 271;

Can we #define all these numbers please?

There are a lot of numbers Xen should expose on the public API/ABI which it currently doesn't. I shall see about collecting them together.

Quoted 18 lines

tools/libxc/saverestore/common_x86_pv.c line 24

Quoted 5 lines
tools/libxc/saverestore/common_x86_pv.c new file +4 -0
+        }
+    }
+    else
+    {

Must be 32 bit? Assert?

Quoted 11 lines
tools/libxc/saverestore/common_x86_pv.c new file +11 -0
+        switch ( type )
+        {
+        case XEN_DOMCTL_PFINFO_L4TAB:
+            ERROR("??? Found L4 table for 32bit guest");
+            errno = EINVAL;
+            return -1;
+
+        case XEN_DOMCTL_PFINFO_L3TAB:
+            /* 32bit guests can only use the first 4 entries of their L3 tables.
+             * All other are potentially used by Xen. */
+            xen_first = 4;
Quoted 8 lines

tools/libxc/saverestore/common_x86_pv.c line 89

Quoted 6 lines
tools/libxc/saverestore/common_x86_pv.c new file +6 -0
+            xen_last = 512;
+            break;
+
+        case XEN_DOMCTL_PFINFO_L2TAB:
+            /* It is hard to spot Xen mappings in a 32bit guest's L2.  Most
+             * are normal but only a few will have Xen mappings.

Internally Xen has PGT_pae_xen_l2, I wonder if it could be coaxed into exposing that here too?

That would require exposing xen struct page_info's for specific mfns. This way seems less hacky.

Quoted 6 lines

tools/libxc/saverestore/common_x86_pv.c line 93

Quoted 4 lines
tools/libxc/saverestore/common_x86_pv.c new file +3 -0
+             * 428 = (HYPERVISOR_VIRT_START_PAE >> L2_PAGETABLE_SHIFT_PAE) & 0x1ff
+             *
+             * ...which is conveniently unavailable to us in a 64bit build.

Can we export it somehow? (Perhaps with COMPAT in the name)

Quoted 8 lines
Quoted 2 lines

... normalize_page
+ local_page = malloc(PAGE_SIZE);

How bad is the overhead of (what I pressume are) all these allocs and frees? (I've not come across the caller in this patch, maybe it will become clear later).

Would it be worth keeping an array around shadowing the "live" batch of pages?

The allocation and freeing of pages has allowed valgrind to be fantastically useful at pointing out when my code was doing something silly.

I would expect the overhead is negligible (the slow parts of migration are the blocking fd reads/writes, mapping hypercalls and memcpy()s), and the aid to debugging is far more important.

Quoted 3 lines
Quoted 2 lines

+static int x86_pv_localise_page(struct context *ctx, uint32_t type, void *page)

"localise" means the inverse of "normalise"?

Yes. I specifically wanted to avoid the term "canonicalise" from the old code in reference to pages and pagetables, as a canonical 64bit address is an architectural term.

Quoted 10 lines

tools/libxc/saverestore/common_x86_pv.c line 253

Quoted 8 lines
tools/libxc/saverestore/common_x86_pv.c new file +6 -0
+void pseudophysmap_walk(struct context *ctx, xen_pfn_t mfn)
+{
+    xc_interface *xch = ctx->xch;
+    xen_pfn_t pfn = ~0UL;
+
+    ERROR("mfn %#lx, max %#lx", mfn, ctx->x86_pv.max_mfn);

It took me a while to figure out why these were all errors. Can we call the function dump_bad_pseudophysmap_walk or some such?

Yes - that makes more sense.

Quoted 9 lines

tools/libxc/saverestore/common_x86_pv.c line 272

Quoted 7 lines
tools/libxc/saverestore/common_x86_pv.c new file +6 -0
+xen_pfn_t cr3_to_mfn(struct context *ctx, uint64_t cr3)
+{
+    if ( ctx->x86_pv.width == 8 )
+        return cr3 >> 12;
+    else
+        return (((uint32_t)cr3 >> 12) | ((uint32_t)cr3 << 20));

A comment pointing people to the phrase "extended-cr3" as grep fodder would be appreciated.

Ok.

Quoted 12 lines
Quoted 11 lines
tools/libxc/saverestore/common_x86_pv.c new file +10 -0
+}
+
+uint64_t mfn_to_cr3(struct context *ctx, xen_pfn_t mfn)
+{
+    if ( ctx->x86_pv.width == 8 )
+        return ((uint64_t)mfn) << 12;
+    else
+        return (((uint32_t)mfn << 12) | ((uint32_t)mfn >> 20));
+}
+

[...]

Quoted 4 lines
Quoted 1 line

+ else if ( guest_width == 4 )

Would prefer to drop the else and insert a blank line to separate the error handling of the xc_domain_.. from the interpretation of its result.

Hmm - this has come about because of changing the "goto err;" vs "return -1" error handing. I shall try to make it more consistent.

Quoted 9 lines

tools/libxc/saverestore/common_x86_pv.c line 301

Quoted 7 lines
tools/libxc/saverestore/common_x86_pv.c new file +6 -0
+        guest_levels = 3;
+    else if ( guest_width == 8 )
+        guest_levels = 4;
+    else
+    {
+        ERROR("Invalid guest width %d.  Expected 32 or 64", guest_width);

Actually you expected 4 or 8. I'm not sure if this could be strengthened into an assert.

Hmm yes. In this case we can probably assert if Xen hands us back junk.

Quoted 11 lines

tools/libxc/saverestore/common_x86_pv.c line 310

Quoted 10 lines
tools/libxc/saverestore/common_x86_pv.c new file +9 -0
+        return -1;
+    }
+    ctx->x86_pv.width = guest_width;
+    ctx->x86_pv.levels = guest_levels;
+    ctx->x86_pv.fpp = fpp = PAGE_SIZE / ctx->x86_pv.width;
+
+    DPRINTF("%d bits, %d levels", guest_width * 8, guest_levels);
+
+    /* Get the domains maximum pfn */

Apostrophes are out of fashion at the moment I guess ;-).

Perhaps :s

Quoted 11 lines

tools/libxc/saverestore/common_x86_pv.c line 320

Quoted 11 lines
tools/libxc/saverestore/common_x86_pv.c new file +10 -0
+    max_pfn = xc_domain_maximum_gpfn(xch, ctx->domid);
+    if ( max_pfn < 0 )
+    {
+        PERROR("Unable to obtain guests max pfn");
+        return -1;
+    }
+    else if ( max_pfn >= ~XEN_DOMCTL_PFINFO_LTAB_MASK )
+    {
+        errno = E2BIG;
+        PERROR("Cannot save a guest this large %#x");
Quoted 1 line

Missing the parameter for %#x.

Hmm - xc_osdep_log() should probably have an __attribute__((format ... )) statement to go with it, although I could have sworn that the compiler would complain at me if I messed up the formatting string with ERROR() macro.

Quoted 13 lines

tools/libxc/saverestore/common_x86_pv.c

Quoted 11 lines

[...]

tools/libxc/saverestore/common_x86_pv.c new file +9 -0
+/*
+ * Extract an MFN from a Pagetable Entry.
+ */
+static inline xen_pfn_t pte_to_frame(struct context *ctx, uint64_t pte)
+{
+    if ( ctx->x86_pv.width == 8 )
+        return (pte >> PAGE_SHIFT) & ((1ULL << (52 - PAGE_SHIFT)) - 1);
+    else
+        return (pte >> PAGE_SHIFT) & ((1ULL << (44 - PAGE_SHIFT)) - 1);

Are bits 45..52 non zero in this case?

Quoted 1 line

Can we #define a couple of MASK's please.

Another Xen ABI which should be exported.

Quoted 10 lines
Quoted 10 lines
tools/libxc/saverestore/common_x86_pv.c new file +9 -0
+static int pfn_set_populated(struct context *ctx, xen_pfn_t pfn)
+{
+    xc_interface *xch = ctx->xch;
+
+    if ( !ctx->restore.populated_pfns || pfn > ctx->restore.max_populated_pfn )
+    {
+        unsigned long new_max_pfn = ((pfn + 1024) & ~1023) - 1;
+        size_t old_sz, new_sz;
+        unsigned long *p;
Quoted 8 lines

tools/libxc/saverestore/common_x86_pv.c

Quoted 5 lines
tools/libxc/saverestore/common_x86_pv.c new file +5 -0
+
+        old_sz = bitmap_size(ctx->restore.max_populated_pfn + 1);
+        new_sz = bitmap_size(new_max_pfn + 1);
+
+        p  = realloc(ctx->restore.populated_pfns, new_sz);

In practice do you not see an initial run of increasing pfns and end up constantly adding one more? Consider doubling or adding e.g. 128M worth of space when growing?

In practice, there will be one bump from 0 to the correct size.

However, it is deliberately implemented like this so when someone decides to fix ballooning/memhotplug vs migration, there is an already working method of indicating "the p2m has gotten bigger since I started".

Quoted 23 lines
Quoted 11 lines
tools/libxc/saverestore/common_x86_pv.c new file +11 -0
+
+    if ( nr_pages > 0 )
+    {
+        mapping = guest_page = xc_map_foreign_bulk(
+            xch, ctx->domid, PROT_READ | PROT_WRITE,
+            mfns, map_errs, nr_pages);
+        if ( !mapping )
+        {
+            PERROR("Unable to map %u mfns for %u pages of data",
+                   nr_pages, count);
+            goto err;

tools/libxc/saverestore/common_x86_pv.c

Quoted 10 lines
tools/libxc/saverestore/common_x86_pv.c new file +10 -0
+        }
+    }
+
+    for ( i = 0, j = 0; i < count; ++i )
+    {
+        switch ( types[i] )
+        {
+        case XEN_DOMCTL_PFINFO_XTAB:
+        case XEN_DOMCTL_PFINFO_BROKEN:
+            /* Nothing at all to do */

Is this a deliberate fall-thru? Please comment if so.

Quoted 3 lines

tools/libxc/saverestore/common_x86_pv.c

Quoted 2 lines
tools/libxc/saverestore/common_x86_pv.c new file +2 -0
+        case XEN_DOMCTL_PFINFO_XALLOC:
+            /* Nothing futher to do */

"further"

They are all fall-though, indicated by the continue on the next line, and lack of default. I suppose I am using switch() for a fancy if.

Quoted 14 lines
Quoted 14 lines
tools/libxc/saverestore/common_x86_pv.c new file +13 -0
+int handle_page_data(struct context *ctx, struct record *rec)
+{
+    xc_interface *xch = ctx->xch;
+    struct rec_page_data_header *pages = rec->data;
+    unsigned i, pages_of_data = 0;
+    int rc = -1;
+
+    xen_pfn_t *pfns = NULL, pfn;
+    uint32_t *types = NULL, type;
+
+    static unsigned pg_count;
+    pg_count++;
+
Quoted 8 lines

tools/libxc/saverestore/common_x86_pv.c

Quoted 7 lines
tools/libxc/saverestore/common_x86_pv.c new file +7 -0
+    if ( rec->length < sizeof *pages )
+    {
+        ERROR("PAGE_DATA record trucated: length %"PRIu32", min %zu",
+              rec->length, sizeof *pages);
+        goto err;
+    }
+    else if ( pages->count < 1 )

We mostly omit the else where the previous if returns or gotos etc.

This comes from merging 3 peoples pages and not quite getting the consistency correct.

Quoted 13 lines

tools/libxc/saverestore/common_x86_pv.c

Quoted 13 lines
tools/libxc/saverestore/common_x86_pv.c new file +12 -0
+    for ( i = 0; i < pages->count; ++i )
+    {
+        pfn = pages->pfn[i] & PAGE_DATA_PFN_MASK;
+        if ( !ctx->ops.pfn_is_valid(ctx, pfn) )
+        {
+            ERROR("pfn %#lx (index %u) outside domain maximum", pfn, i);
+            goto err;
+        }
+
+        type = (pages->pfn[i] & PAGE_DATA_TYPE_MASK) >> 32;
+        if ( ((type >> XEN_DOMCTL_PFINFO_LTAB_SHIFT) >= 5) &&
+             ((type >> XEN_DOMCTL_PFINFO_LTAB_SHIFT) <= 8) )
Quoted 5 lines

What are 5 and 8?

This might all be clearer with the use of a #define or two, and perhaps using a switch over the expected valid types and explicitly invalid types.

5 to 8 are the "holes" in the otherwise complete PFINFO_TYPE numerspace.

My views are that the XEN_DOMCTL_PFINFO_* macros are unconditionally horrible to use, whatever you are trying to do, which is why the use of them is gauged for local clarity rather than global consistency.

I am not sure a switch statement would help here.

Quoted 11 lines
Quoted 11 lines
tools/libxc/saverestore/save.c +2 -0
index c013e62..e842e6c 100644
@@ -1,5 +1,47 @@
+#include <arpa/inet.h>
+
 #include "common.h"
 
Quoted 8 lines

tools/libxc/saverestore/save.c line 10

Quoted 6 lines
tools/libxc/saverestore/save.c +6 -0
+int write_headers(struct context *ctx, uint16_t guest_type)
+{
+    xc_interface *xch = ctx->xch;
+    int32_t xen_version = xc_version(xch, XENVER_version, NULL);
+    struct ihdr ihdr =
+        {

I think coding style puts this on the previous line and the body should be 4 spaces further in.

Not according to the emacs mode at the bottom, which would appear to create a contradiction in CODING_SYTLE.

Quoted 10 lines

tools/libxc/saverestore/save.c line 19

Quoted 10 lines
tools/libxc/saverestore/save.c +9 -0
+            .marker  = IHDR_MARKER,
+            .id      = htonl(IHDR_ID),
+            .version = htonl(IHDR_VERSION),
+            .options = htons(IHDR_OPT_LITTLE_ENDIAN),
+        };
+    struct dhdr dhdr =
+        {
+            .type       = guest_type,
+            .page_shift = 12,
Quoted 4 lines

We have a define for this.

Ian.

So we do.

~Andrew

#19 lore raw

Re: [PATCH v4 5/9] tools/libxc: common code

Quoted 46 lines · Andrew Cooper
Quoted 35 lines · Ian Campbell
Quoted 34 lines · Andrew Cooper
---
 tools/libxc/saverestore/common.c         |   87 ++++++
 tools/libxc/saverestore/common.h         |  172 ++++++++++++
 tools/libxc/saverestore/common_x86.c     |   54 ++++
 tools/libxc/saverestore/common_x86.h     |   21 ++
 tools/libxc/saverestore/common_x86_hvm.c |   53 ++++
 tools/libxc/saverestore/common_x86_pv.c  |  431 ++++++++++++++++++++++++++++++
 tools/libxc/saverestore/common_x86_pv.h  |  104 +++++++
 tools/libxc/saverestore/restore.c        |  288 ++++++++++++++++++++
 tools/libxc/saverestore/save.c           |   42 +++
 9 files changed, 1252 insertions(+)
 create mode 100644 tools/libxc/saverestore/common_x86.c
 create mode 100644 tools/libxc/saverestore/common_x86.h
 create mode 100644 tools/libxc/saverestore/common_x86_hvm.c
 create mode 100644 tools/libxc/saverestore/common_x86_pv.c
 create mode 100644 tools/libxc/saverestore/common_x86_pv.h
tools/libxc/saverestore/common.c +2 -0
index de2e727..b159c4c 100644
@@ -1,3 +1,5 @@
+#include <assert.h>
+
 #include "common.h"
 
 static const char *dhdr_types[] =
@@ -52,6 +54,91 @@ const char *rec_type_to_str(uint32_t type)
     return "Reserved";
 }
 
Quoted 9 lines

tools/libxc/saverestore/common.c const char *rec_type_to_str(uint32_t type) · line 63

Quoted 7 lines
tools/libxc/saverestore/common.c +7 -0
+int write_split_record(struct context *ctx, struct record *rec,
+                       void *buf, size_t sz)
+{
+    static const char zeroes[7] = { 0 };
+    xc_interface *xch = ctx->xch;
+    uint32_t combined_length = rec->length + sz;
+    size_t record_length = (combined_length + 7) & ~7UL;

Isn't this ROUNDUP(combined_length, 3)? (Where 3 and/or 7 perhaps ought to be given names)

Quoted 3 lines

It is. I am not certain that 3/7 need specific named, given the specification mandating that all records have trailing 0s to align the subsequent record on an 8 byte boundary.

If the specification mandates it then that is an argument *for* a SAVE_RECORD_ALIGNMENT type #define.

Quoted 5 lines
Quoted 3 lines

tools/libxc/saverestore/common.c const char *rec_type_to_str(uint32_t type) · line 78

Quoted 1 line
tools/libxc/saverestore/common.c +1 -0
+    if ( write_exact(ctx->fd, &rec->type, sizeof rec->type) ||

libxc style appears to be "sizeof (rec->type)" (the space seems odd to me but is apparently The Style, I think the brackets are a must).

libxc style also states no extraneous brackets.

These ones aren't extraneous ;-)

Where does it say that anyway?

Quoted 11 lines
Quoted 6 lines

tools/libxc/saverestore/common.c const char *rec_type_to_str(uint32_t type) · line 111

Quoted 1 line
tools/libxc/saverestore/common.c +1 -0
+    datasz = (rhdr.length + 7) & ~7U;

Another ROUNDUP and #define XXX 3 or 7 opportunity. (I'm not going to mention any more of these I find).

Is it not a bug in the input for datasz to not be aligned? I thought you were padding everything.

It is certainly not a bug. For the blobs which can have an arbitrary length, the record length field reflects the exact length. Trailing 0s are then inserted into the stream to align the start of the next record on an 8 byte boundary.

Make sense. In which case can you add a comment /* Include padding not covered by rhdr.length */ or some such.

Quoted 7 lines
Quoted 4 lines

tools/libxc/saverestore/common.c const char *rec_type_to_str(uint32_t type) · line 66

Quoted 1 line
tools/libxc/saverestore/common.c +1 -0
+// Hack out junk from the namespace

... namespace, because/in order to...

(to prevent accidents I suppose?)

because mfn_to_pfn and pfn_to_mfn are gross abortions of macros which look like regular functions but take implicit scoped state.

So don't use them?

Quoted 2 lines

I have been collapsing patches from 3 separate people ;) I will do a consistency check before this becomes non-rfc.

nb: these patches are missing the RFC tag if that's what they are.

Quoted 16 lines
Quoted 11 lines

tools/libxc/saverestore/common.c const char *rec_type_to_str(uint32_t type) · line 76

Quoted 11 lines
tools/libxc/saverestore/common.c +10 -0
+/*
+ * Writes a split record to the stream, applying correct padding where
+ * appropriate.  It is common when sending records containing blobs from Xen
+ * that the header and blob data are separate.  This function accepts a second
+ * buffer and length, and will merge it with the main record when sending.
+ *
+ * Records with a non-zero length must provide a valid data field; records
+ * with a 0 length shall have their data field ignored.
+ *
+ * Returns 0 on success and non0 on failure.
Quoted 2 lines

"non-0". Do the non-zero values have any particular meanings? (errno codes, other error codes?)

No - see also the other inconsistencies in libxc.

Quoted 8 lines

The current state of all the new functions are "0 on success, -1 on error and errno is unlikely be related". This is no worse than the vast majority of libxc currently, and stating "non-0" on error allows for a positive "libxc error" range as discussed down the pub.

I am not shaving that yak right now, but have tried to leave the code in a state where inserting consistent error handing should be less hassle than it otherwise could be.

OK.

Quoted 2 lines

In all error cases, the log will (should) have accurate information, including errno when relevent.

Mentioning this in the comment is nice too, since it prevents callers thinking they must log as well.

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
tools/libxc/saverestore/common.c +3 -0
@@ -0,0 +1,431 @@
+#include <assert.h>
+
+#include "common_x86_pv.h"
Quoted 8 lines

tools/libxc/saverestore/common.c line 7

Quoted 4 lines
tools/libxc/saverestore/common.c +4 -0
+
+xen_pfn_t mfn_to_pfn(struct context *ctx, xen_pfn_t mfn)
+{
+    assert(mfn <= ctx->x86_pv.max_mfn);

Just to confirm that anywhere there is an assert like this then if it is based on potentially user controller input then it has already been validated elsewhere first? (with the abstraction I couldn't spot where that was)

Quoted 2 lines

It is validated in every case I am aware of, usually by "mfn_in_pseudophysmap()"

Good, because it would be a security issue if not...

Quoted 20 lines
Quoted 11 lines
Quoted 11 lines
tools/libxc/saverestore/common.c +11 -0
+        switch ( type )
+        {
+        case XEN_DOMCTL_PFINFO_L4TAB:
+            ERROR("??? Found L4 table for 32bit guest");
+            errno = EINVAL;
+            return -1;
+
+        case XEN_DOMCTL_PFINFO_L3TAB:
+            /* 32bit guests can only use the first 4 entries of their L3 tables.
+             * All other are potentially used by Xen. */
+            xen_first = 4;
Quoted 8 lines

tools/libxc/saverestore/common.c line 24

Quoted 6 lines
tools/libxc/saverestore/common.c +6 -0
+            xen_last = 512;
+            break;
+
+        case XEN_DOMCTL_PFINFO_L2TAB:
+            /* It is hard to spot Xen mappings in a 32bit guest's L2.  Most
+             * are normal but only a few will have Xen mappings.

Internally Xen has PGT_pae_xen_l2, I wonder if it could be coaxed into exposing that here too?

Quoted 2 lines

That would require exposing xen struct page_info's for specific mfns. This way seems less hacky.

I meant via the introduction of XEN_DOMCTL_PFIINF_L2XENTAB (name TBD) (AIUI this interface is essentially exposing that bit of the page info, isn't it?)

Quoted 11 lines
Quoted 8 lines
Quoted 2 lines

... normalize_page
+ local_page = malloc(PAGE_SIZE);

How bad is the overhead of (what I pressume are) all these allocs and frees? (I've not come across the caller in this patch, maybe it will become clear later).

Would it be worth keeping an array around shadowing the "live" batch of pages?

The allocation and freeing of pages has allowed valgrind to be fantastically useful at pointing out when my code was doing something silly.

Fair enough (I wonder if there is some valgrind mechanism for marking memory as explicitly uninitialised as if it had been freed/reallocated)

Quoted 3 lines

I would expect the overhead is negligible (the slow parts of migration are the blocking fd reads/writes, mapping hypercalls and memcpy()s), and the aid to debugging is far more important.

True, AIUI glibc's malloc is pretty speedy.

Quoted 8 lines
Quoted 4 lines
Quoted 3 lines

+static int x86_pv_localise_page(struct context *ctx, uint32_t type, void *page)

"localise" means the inverse of "normalise"?

Yes. I specifically wanted to avoid the term "canonicalise" from the old code in reference to pages and pagetables, as a canonical 64bit address is an architectural term.

OK.

Quoted 23 lines
Quoted 23 lines
Quoted 11 lines
tools/libxc/saverestore/common.c +11 -0
+
+    if ( nr_pages > 0 )
+    {
+        mapping = guest_page = xc_map_foreign_bulk(
+            xch, ctx->domid, PROT_READ | PROT_WRITE,
+            mfns, map_errs, nr_pages);
+        if ( !mapping )
+        {
+            PERROR("Unable to map %u mfns for %u pages of data",
+                   nr_pages, count);
+            goto err;

tools/libxc/saverestore/common.c

Quoted 10 lines
tools/libxc/saverestore/common.c +10 -0
+        }
+    }
+
+    for ( i = 0, j = 0; i < count; ++i )
+    {
+        switch ( types[i] )
+        {
+        case XEN_DOMCTL_PFINFO_XTAB:
+        case XEN_DOMCTL_PFINFO_BROKEN:
+            /* Nothing at all to do */

Is this a deliberate fall-thru? Please comment if so.

Quoted 6 lines
Quoted 3 lines

tools/libxc/saverestore/common.c

Quoted 2 lines
tools/libxc/saverestore/common.c +2 -0
+        case XEN_DOMCTL_PFINFO_XALLOC:
+            /* Nothing futher to do */

"further"

They are all fall-though, indicated by the continue on the next line, and lack of default.

The existing comment half way through the list is what breaks this though, since it's no longer clear that it does indicate that.

Quoted 20 lines
Quoted 14 lines
Quoted 4 lines
tools/libxc/saverestore/common.c +2 -0
+    for ( i = 0; i < pages->count; ++i )
+    {

tools/libxc/saverestore/common.c

Quoted 10 lines
tools/libxc/saverestore/common.c +10 -0
+        pfn = pages->pfn[i] & PAGE_DATA_PFN_MASK;
+        if ( !ctx->ops.pfn_is_valid(ctx, pfn) )
+        {
+            ERROR("pfn %#lx (index %u) outside domain maximum", pfn, i);
+            goto err;
+        }
+
+        type = (pages->pfn[i] & PAGE_DATA_TYPE_MASK) >> 32;
+        if ( ((type >> XEN_DOMCTL_PFINFO_LTAB_SHIFT) >= 5) &&
+             ((type >> XEN_DOMCTL_PFINFO_LTAB_SHIFT) <= 8) )
Quoted 5 lines

What are 5 and 8?

This might all be clearer with the use of a #define or two, and perhaps using a switch over the expected valid types and explicitly invalid types.

Quoted 7 lines

5 to 8 are the "holes" in the otherwise complete PFINFO_TYPE numerspace.

My views are that the XEN_DOMCTL_PFINFO_* macros are unconditionally horrible to use, whatever you are trying to do, which is why the use of them is gauged for local clarity rather than global consistency.

I am not sure a switch statement would help here.

In that case a comment would help.

Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
tools/libxc/saverestore/save.c +2 -0
index c013e62..e842e6c 100644
@@ -1,5 +1,47 @@
+#include <arpa/inet.h>
+
 #include "common.h"
 
Quoted 8 lines

tools/libxc/saverestore/save.c line 10

Quoted 6 lines
tools/libxc/saverestore/save.c +6 -0
+int write_headers(struct context *ctx, uint16_t guest_type)
+{
+    xc_interface *xch = ctx->xch;
+    int32_t xen_version = xc_version(xch, XENVER_version, NULL);
+    struct ihdr ihdr =
+        {

I think coding style puts this on the previous line and the body should be 4 spaces further in.

Quoted 2 lines

Not according to the emacs mode at the bottom, which would appear to create a contradiction in CODING_SYTLE.

CODING_STYLE would take precedence if it said anything about this specific case, which it doesn't. The prevailing style everywhere else I could find was with the { on the same line.

(not sure which bit of the emacs marker you think contradicts this, BSD's style(9) doesn't cover this case either)

Ian.

#20 lore raw

Re: [PATCH v4 5/9] tools/libxc: common code

Quoted 46 lines · Ian Campbell
Quoted 45 lines · Andrew Cooper
Quoted 44 lines · Ian Campbell
Quoted 31 lines · Andrew Cooper
---
 tools/libxc/saverestore/common.c         |   87 ++++++
 tools/libxc/saverestore/common.h         |  172 ++++++++++++
 tools/libxc/saverestore/common_x86.c     |   54 ++++
 tools/libxc/saverestore/common_x86.h     |   21 ++
 tools/libxc/saverestore/common_x86_hvm.c |   53 ++++
 tools/libxc/saverestore/common_x86_pv.c  |  431 ++++++++++++++++++++++++++++++
 tools/libxc/saverestore/common_x86_pv.h  |  104 +++++++
 tools/libxc/saverestore/restore.c        |  288 ++++++++++++++++++++
 tools/libxc/saverestore/save.c           |   42 +++
 9 files changed, 1252 insertions(+)
 create mode 100644 tools/libxc/saverestore/common_x86.c
 create mode 100644 tools/libxc/saverestore/common_x86.h
 create mode 100644 tools/libxc/saverestore/common_x86_hvm.c
 create mode 100644 tools/libxc/saverestore/common_x86_pv.c
 create mode 100644 tools/libxc/saverestore/common_x86_pv.h
tools/libxc/saverestore/common.c +2 -0
index de2e727..b159c4c 100644
@@ -1,3 +1,5 @@
+#include <assert.h>
+
 #include "common.h"
 
 static const char *dhdr_types[] =

tools/libxc/saverestore/common.c const char *rec_type_to_str(uint32_t type) · line 63

Quoted 10 lines
tools/libxc/saverestore/common.c +7 -0
@@ -52,6 +54,91 @@ const char *rec_type_to_str(uint32_t type)
     return "Reserved";
 }
 
+int write_split_record(struct context *ctx, struct record *rec,
+                       void *buf, size_t sz)
+{
+    static const char zeroes[7] = { 0 };
+    xc_interface *xch = ctx->xch;
+    uint32_t combined_length = rec->length + sz;
+    size_t record_length = (combined_length + 7) & ~7UL;

Isn't this ROUNDUP(combined_length, 3)? (Where 3 and/or 7 perhaps ought to be given names)

Quoted 5 lines
Quoted 3 lines

It is. I am not certain that 3/7 need specific named, given the specification mandating that all records have trailing 0s to align the subsequent record on an 8 byte boundary.

If the specification mandates it then that is an argument *for* a SAVE_RECORD_ALIGNMENT type #define.

Fair enough. I will include that.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

tools/libxc/saverestore/common.c const char *rec_type_to_str(uint32_t type) · line 111

Quoted 1 line
tools/libxc/saverestore/common.c +1 -0
+    datasz = (rhdr.length + 7) & ~7U;

Another ROUNDUP and #define XXX 3 or 7 opportunity. (I'm not going to mention any more of these I find).

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Is it not a bug in the input for datasz to not be aligned? I thought you were padding everything.

It is certainly not a bug. For the blobs which can have an arbitrary length, the record length field reflects the exact length. Trailing 0s are then inserted into the stream to align the start of the next record on an 8 byte boundary.

Make sense. In which case can you add a comment /* Include padding not covered by rhdr.length */ or some such.

Ok.

Quoted 8 lines
Quoted 7 lines
Quoted 5 lines

tools/libxc/saverestore/common.c const char *rec_type_to_str(uint32_t type) · line 65

Quoted 2 lines
tools/libxc/saverestore/common.c +1 -0
+// Hack out junk from the namespace

... namespace, because/in order to...

(to prevent accidents I suppose?)

because mfn_to_pfn and pfn_to_mfn are gross abortions of macros which look like regular functions but take implicit scoped state.

So don't use them?

But the names themselves are the only logical choice for real functions of the same purpose.

Quoted 5 lines
Quoted 4 lines

I have been collapsing patches from 3 separate people ;) I will do a consistency check before this becomes non-rfc.

nb: these patches are missing the RFC tag if that's what they are.

The RFC tag appears to have dropped off some time during formatting them. My appologies. The series as a whole is still not functionally complete but is getting there.

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

tools/libxc/saverestore/common.c line 7

Quoted 9 lines
tools/libxc/saverestore/common.c +7 -0
@@ -0,0 +1,431 @@
+#include <assert.h>
+
+#include "common_x86_pv.h"
+
+xen_pfn_t mfn_to_pfn(struct context *ctx, xen_pfn_t mfn)
+{
+    assert(mfn <= ctx->x86_pv.max_mfn);

Just to confirm that anywhere there is an assert like this then if it is based on potentially user controller input then it has already been validated elsewhere first? (with the abstraction I couldn't spot where that was)

Quoted 3 lines
Quoted 2 lines

It is validated in every case I am aware of, usually by "mfn_in_pseudophysmap()"

Good, because it would be a security issue if not...

In what way? The absolute worst that could happen is a controlled stop of the process handling migration for this domain.

It is certainly better than walking off the end of the mapped m2p.

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 8 lines
tools/libxc/saverestore/common.c +7 -0
+        switch ( type )
+        {
+        case XEN_DOMCTL_PFINFO_L4TAB:
+            ERROR("??? Found L4 table for 32bit guest");
+            errno = EINVAL;
+            return -1;
+

tools/libxc/saverestore/common.c line 24

Quoted 10 lines
tools/libxc/saverestore/common.c +10 -0
+        case XEN_DOMCTL_PFINFO_L3TAB:
+            /* 32bit guests can only use the first 4 entries of their L3 tables.
+             * All other are potentially used by Xen. */
+            xen_first = 4;
+            xen_last = 512;
+            break;
+
+        case XEN_DOMCTL_PFINFO_L2TAB:
+            /* It is hard to spot Xen mappings in a 32bit guest's L2.  Most
+             * are normal but only a few will have Xen mappings.

Internally Xen has PGT_pae_xen_l2, I wonder if it could be coaxed into exposing that here too?

Quoted 5 lines
Quoted 2 lines

That would require exposing xen struct page_info's for specific mfns. This way seems less hacky.

I meant via the introduction of XEN_DOMCTL_PFIINF_L2XENTAB (name TBD) (AIUI this interface is essentially exposing that bit of the page info, isn't it?)

Hmm. There are some spare numbers available to be used. It might be an idea.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

... normalize_page
+ local_page = malloc(PAGE_SIZE);

How bad is the overhead of (what I pressume are) all these allocs and frees? (I've not come across the caller in this patch, maybe it will become clear later).

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Would it be worth keeping an array around shadowing the "live" batch of pages?

The allocation and freeing of pages has allowed valgrind to be fantastically useful at pointing out when my code was doing something silly.

Fair enough (I wonder if there is some valgrind mechanism for marking memory as explicitly uninitialised as if it had been freed/reallocated)

I have searched but cant find anything. As valgrind is designed to wrap compiled binaries, making function calls into it is probably non-trivial.

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 11 lines
tools/libxc/saverestore/common.c +11 -0
+
+    if ( nr_pages > 0 )
+    {
+        mapping = guest_page = xc_map_foreign_bulk(
+            xch, ctx->domid, PROT_READ | PROT_WRITE,
+            mfns, map_errs, nr_pages);
+        if ( !mapping )
+        {
+            PERROR("Unable to map %u mfns for %u pages of data",
+                   nr_pages, count);
+            goto err;

tools/libxc/saverestore/common.c

Quoted 10 lines
tools/libxc/saverestore/common.c +10 -0
+        }
+    }
+
+    for ( i = 0, j = 0; i < count; ++i )
+    {
+        switch ( types[i] )
+        {
+        case XEN_DOMCTL_PFINFO_XTAB:
+        case XEN_DOMCTL_PFINFO_BROKEN:
+            /* Nothing at all to do */

Is this a deliberate fall-thru? Please comment if so.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

tools/libxc/saverestore/common.c

Quoted 2 lines
tools/libxc/saverestore/common.c +2 -0
+        case XEN_DOMCTL_PFINFO_XALLOC:
+            /* Nothing futher to do */

"further"

They are all fall-though, indicated by the continue on the next line, and lack of default.

The existing comment half way through the list is what breaks this though, since it's no longer clear that it does indicate that.

The comment is partially stale in this context anyway (following refactoring). I shall see about collapsing it down to a single if statement which can be made to be more clear.

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines

tools/libxc/saverestore/common.c

Quoted 13 lines
tools/libxc/saverestore/common.c +12 -0
+    for ( i = 0; i < pages->count; ++i )
+    {
+        pfn = pages->pfn[i] & PAGE_DATA_PFN_MASK;
+        if ( !ctx->ops.pfn_is_valid(ctx, pfn) )
+        {
+            ERROR("pfn %#lx (index %u) outside domain maximum", pfn, i);
+            goto err;
+        }
+
+        type = (pages->pfn[i] & PAGE_DATA_TYPE_MASK) >> 32;
+        if ( ((type >> XEN_DOMCTL_PFINFO_LTAB_SHIFT) >= 5) &&
+             ((type >> XEN_DOMCTL_PFINFO_LTAB_SHIFT) <= 8) )

What are 5 and 8?

This might all be clearer with the use of a #define or two, and perhaps using a switch over the expected valid types and explicitly invalid types.

Quoted 8 lines
Quoted 7 lines

5 to 8 are the "holes" in the otherwise complete PFINFO_TYPE numerspace.

My views are that the XEN_DOMCTL_PFINFO_* macros are unconditionally horrible to use, whatever you are trying to do, which is why the use of them is gauged for local clarity rather than global consistency.

I am not sure a switch statement would help here.

In that case a comment would help.

I might be able to make it a little clearer.

Quoted 20 lines
Quoted 18 lines
Quoted 10 lines
Quoted 10 lines
tools/libxc/saverestore/save.c +2 -0
index c013e62..e842e6c 100644
@@ -1,5 +1,47 @@
+#include <arpa/inet.h>
+
 #include "common.h"
 
Quoted 8 lines

tools/libxc/saverestore/save.c line 10

Quoted 6 lines
tools/libxc/saverestore/save.c +6 -0
+int write_headers(struct context *ctx, uint16_t guest_type)
+{
+    xc_interface *xch = ctx->xch;
+    int32_t xen_version = xc_version(xch, XENVER_version, NULL);
+    struct ihdr ihdr =
+        {

I think coding style puts this on the previous line and the body should be 4 spaces further in.

Quoted 2 lines

Not according to the emacs mode at the bottom, which would appear to create a contradiction in CODING_SYTLE.

Quoted 9 lines

CODING_STYLE would take precedence if it said anything about this specific case, which it doesn't. The prevailing style everywhere else I could find was with the { on the same line.

(not sure which bit of the emacs marker you think contradicts this, BSD's style(9) doesn't cover this case either)

Ian.

That would be emacs' cc-mode interpretation of the marker, when instructed to indent the entire file.

If it isn't specifically covered in BSD's style(9), I am sure there are multiple interpretations going. I will see about persuading emacs not to indent them.

~Andrew

#21 lore raw

Re: [PATCH v4 5/9] tools/libxc: common code

Quoted 13 lines · Andrew Cooper
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

tools/libxc/saverestore/common_x86_pv.c line 7

Quoted 9 lines
tools/libxc/saverestore/common_x86_pv.c new file +7 -0
@@ -0,0 +1,431 @@
+#include <assert.h>
+
+#include "common_x86_pv.h"
+
+xen_pfn_t mfn_to_pfn(struct context *ctx, xen_pfn_t mfn)
+{
+    assert(mfn <= ctx->x86_pv.max_mfn);

Just to confirm that anywhere there is an assert like this then if it is based on potentially user controller input then it has already been validated elsewhere first? (with the abstraction I couldn't spot where that was)

Quoted 6 lines
Quoted 3 lines
Quoted 2 lines

It is validated in every case I am aware of, usually by "mfn_in_pseudophysmap()"

Good, because it would be a security issue if not...

In what way? The absolute worst that could happen is a controlled stop of the process handling migration for this domain.

AKA the toolstack daemon, which would be a DoS depending on the toolstack's architecture.

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 4 lines

... normalize_page
+ local_page = malloc(PAGE_SIZE);

How bad is the overhead of (what I pressume are) all these allocs and frees? (I've not come across the caller in this patch, maybe it will become clear later).

Would it be worth keeping an array around shadowing the "live" batch of pages?

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

The allocation and freeing of pages has allowed valgrind to be fantastically useful at pointing out when my code was doing something silly.

Fair enough (I wonder if there is some valgrind mechanism for marking memory as explicitly uninitialised as if it had been freed/reallocated)

I have searched but cant find anything. As valgrind is designed to wrap compiled binaries, making function calls into it is probably non-trivial.

It was a long shot...

I was imagining you'd have to have a local

	void valgrind_invalidate(void *p) {}

which valgrind would trap calls to in its usual way "somehow"

Ian.

#22 lore raw

Re: [PATCH v4 5/9] tools/libxc: common code

In reply to Andrew Cooper
Quoted 3 lines · Andrew Cooper

because mfn_to_pfn and pfn_to_mfn are gross abortions of macros which look like regular functions but take implicit scoped state.

Please do not use such insensitive and offensive language on xen-devel.

xen-devel needs to be a safe and welcoming space for all contributors.

David

#23 lore raw

PATCH v4 6/9 tools/libxc: x86 pv save implementation

In reply to Andrew Cooper
---
 tools/libxc/saverestore/common.h      |   20 ++
 tools/libxc/saverestore/save.c        |  453 +++++++++++++++++++++++++-
 tools/libxc/saverestore/save_x86_pv.c |  568 +++++++++++++++++++++++++++++++++
 3 files changed, 1040 insertions(+), 1 deletion(-)
 create mode 100644 tools/libxc/saverestore/save_x86_pv.c
tools/libxc/saverestore/common.h +20 -0
index a35eda7..116eb13 100644
@@ -12,6 +12,8 @@
 #include "../xc_dom.h"
 #include "../xc_bitops.h"
 
+#undef GET_FIELD
+#undef SET_FIELD
 #undef mfn_to_pfn
 #undef pfn_to_mfn
 
@@ -121,6 +123,9 @@ struct context
     };
 };
 
+/* Saves an x86 PV domain. */
+int save_x86_pv(struct context *ctx);
+
 /*
  * Write the image and domain headers to the stream.
  * (to eventually make static in save.c)
@@ -137,6 +142,21 @@ struct record
     void *data;
 };
 
+/* Gets a field from an *_any union */
+#define GET_FIELD(_c, _p, _f)                   \
+    ({ (_c)->x86_pv.width == 8 ?                \
+            (_p)->x64._f:                       \
+            (_p)->x32._f;                       \
+    })                                          \
+
+/* Gets a field from an *_any union */
+#define SET_FIELD(_c, _p, _f, _v)               \
+    ({ if ( (_c)->x86_pv.width == 8 )           \
+            (_p)->x64._f = (_v);                \
+        else                                    \
+            (_p)->x32._f = (_v);                \
+    })
+
 /*
  * Writes a split record to the stream, applying correct padding where
  * appropriate.  It is common when sending records containing blobs from Xen
tools/libxc/saverestore/save.c +452 -1

468 lines not on this page · raw message

tools/libxc/saverestore/save_x86_pv.c new file +568 -0

568 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#24 lore raw

Re: [PATCH v4 6/9] tools/libxc: x86 pv save implementation

On Wed, 2014-04-30 at 19:36 +0100, Andrew Cooper wrote:

The previous "common" patch seemed to have a fair bit of x86 pv save code in it, and there seems to be a fair bit of common code here. I suppose the split was pretty artificial to make the patches manageable?

tools/libxc/saverestore/common.h struct context · line 126

Quoted 5 lines
tools/libxc/saverestore/common.h +1 -0
@@ -121,6 +123,9 @@ struct context
     };
 };
 
+/* Saves an x86 PV domain. */

I should hope so!

tools/libxc/saverestore/common.h struct record · line 145

Quoted 10 lines
tools/libxc/saverestore/common.h +3 -0
+int save_x86_pv(struct context *ctx);
+
 /*
  * Write the image and domain headers to the stream.
  * (to eventually make static in save.c)
@@ -137,6 +142,21 @@ struct record
     void *data;
 };
 
+/* Gets a field from an *_any union */

Unless the #undefs which were above before I trimmed them relate to patch #2/9 they should go right before the redefine IMHO.

Quoted 18 lines
tools/libxc/saverestore/common.h +14 -0
+#define GET_FIELD(_c, _p, _f)                   \
+    ({ (_c)->x86_pv.width == 8 ?                \
+            (_p)->x64._f:                       \
+            (_p)->x32._f;                       \
+    })                                          \
+
+/* Gets a field from an *_any union */
+#define SET_FIELD(_c, _p, _f, _v)               \
+    ({ if ( (_c)->x86_pv.width == 8 )           \
+            (_p)->x64._f = (_v);                \
+        else                                    \
+            (_p)->x32._f = (_v);                \
+    })
+
 /*
  * Writes a split record to the stream, applying correct padding where
  * appropriate.  It is common when sending records containing blobs from Xen
 

tools/libxc/saverestore/common.h struct record

Quoted 10 lines
tools/libxc/saverestore/common.h +10 -0
+    /* MFNs of the batch pfns */
+    mfns = malloc(nr_pfns * sizeof *mfns);
+    /* Types of the batch pfns */
+    types = malloc(nr_pfns * sizeof *types);
+    /* Errors from attempting to map the mfns */
+    errors = malloc(nr_pfns * sizeof *errors);
+    /* Pointers to page data to send.  Might be from mapped mfns or local allocations */
+    guest_data = calloc(nr_pfns, sizeof *guest_data);
+    /* Pointers to locally allocated pages.  Probably not all used, but need freeing */
+    local_pages = calloc(nr_pfns, sizeof *local_pages);

Wouldn't it be better to preallocate (most of) these for the entire save/restore than to do it for each batch?

tools/libxc/saverestore/save.c int write_headers(struct context *ctx, uint16_t guest_type) · line 89

Quoted 7 lines
tools/libxc/saverestore/save.c +7 -0
+    for ( i = 0; i < nr_pfns; ++i )
+    {
+        types[i] = mfns[i] = ctx->ops.pfn_to_gfn(ctx, ctx->batch_pfns[i]);
+
+        /* Likely a ballooned page */
+        if ( mfns[i] == INVALID_MFN )
+            set_bit(ctx->batch_pfns[i], ctx->deferred_pages);

deferred_pages doesn't need to grow dynamically like that bit map in a previous patch, does it?

tools/libxc/saverestore/save.c int write_headers(struct context *ctx, uint16_t guest_type) · line 165

Quoted 2 lines
tools/libxc/saverestore/save.c +2 -0
+    hdr.count = nr_pfns;
+    s = nr_pfns * sizeof *rec_pfns;

Brackets around sizeof would really help here.

tools/libxc/saverestore/save.c int write_headers(struct context *ctx, uint16_t guest_type) · line 168

Quoted 3 lines
tools/libxc/saverestore/save.c +3 -0
+
+
+    rec_pfns = malloc(s);

Given the calculation of s is this a candidate for calloc?

tools/libxc/saverestore/save.c int write_headers(struct context *ctx, uint16_t guest_type) · line 176

Quoted 8 lines
tools/libxc/saverestore/save.c +8 -0
+    if ( !rec_pfns )
+    {
+        ERROR("Unable to allocate memory for page data pfn list");
+        goto err;
+    }
+
+    for ( i = 0; i < nr_pfns; ++i )
+        rec_pfns[i] = ((uint64_t)(types[i]) << 32) | ctx->batch_pfns[i];

Could this not have a more structured type? (I noticed this in the decode bit of the previous patch too).

tools/libxc/saverestore/save.c int write_headers(struct context *ctx, uint16_t guest_type) · line 179

Quoted 3 lines
tools/libxc/saverestore/save.c +3 -0
+
+    /*        header +          pfns data           +        page data */
+    rec_size = 4 + 4 + (s) + (nr_pages * PAGE_SIZE);

Can these 4's not be sizeof something? Also the comment looks like it wants to align with something, but is failing.

tools/libxc/saverestore/save.c int write_headers(struct context *ctx, uint16_t guest_type) · line 183

Quoted 4 lines
tools/libxc/saverestore/save.c +4 -0
+    rec_count = nr_pfns;
+
+    if ( write_exact(ctx->fd, &rec_type, sizeof(uint32_t)) ||
+         write_exact(ctx->fd, &rec_size, sizeof(uint32_t)) ||

Are these not a struct rhdr?

Quoted 5 lines
tools/libxc/saverestore/save.c +5 -0
+         write_exact(ctx->fd, &rec_count, sizeof(uint32_t)) ||
+         write_exact(ctx->fd, &rec_res, sizeof(uint32_t)) )
+    {
+        PERROR("Failed to write page_type header to stream");
+        goto err;

tools/libxc/saverestore/save.c int write_headers(struct context *ctx, uint16_t guest_type) · line 198

Quoted 10 lines
tools/libxc/saverestore/save.c +10 -0
+    }
+
+    if ( write_exact(ctx->fd, rec_pfns, s) )
+    {
+        PERROR("Failed to write page_type header to stream");
+        goto err;
+    }
+
+
+    for ( i = 0; i < nr_pfns; ++i )

Extra braces are useful for clarity in these siturations.

tools/libxc/saverestore/save.c int write_headers(struct context *ctx, uint16_t guest_type) · line 241

Quoted 12 lines
tools/libxc/saverestore/save.c +12 -0
+static int flush_batch(struct context *ctx)
+{
+    int rc = 0;
+
+    if ( ctx->nr_batch_pfns == 0 )
+        return rc;
+
+    rc = write_batch(ctx);
+
+    if ( !rc )
+    {
+        /* Valgrind sanity check */

What now?

tools/libxc/saverestore/save.c int write_headers(struct context *ctx, uint16_t guest_type) · line 284

Quoted 9 lines
tools/libxc/saverestore/save.c +9 -0
+        free(ctx->batch_pfns);
+        ctx->batch_pfns = malloc(MAX_BATCH_SIZE * sizeof *ctx->batch_pfns);
+        rc = !ctx->batch_pfns;
+    }
+
+
+    for ( x = 0, pages_written = 0; x < max_iter ; ++x )
+    {
+        DPRINTF("Iteration %u", x);

Something in this series should be using xc_report_progress_* (I'm not sure that place is here, but I didn't see it elsewhere yet).

tools/libxc/saverestore/save.c int write_headers(struct context *ctx, uint16_t guest_type) · line 460

Quoted 7 lines

int xc_domain_save2(xc_interface *xch, int io_fd, uint32_t dom, uint32_t max_iters,
uint32_t max_factor, uint32_t flags,
struct save_callbacks* callbacks, int hvm, unsigned long vm_generationid_addr)
{

tools/libxc/saverestore/save.c +2 -0
+    struct context ctx =
+        {

Coding style.

tools/libxc/saverestore/save.c int write_headers(struct context *ctx, uint16_t guest_type) · line 465

Quoted 5 lines
tools/libxc/saverestore/save.c +5 -0
+            .xch = xch,
+            .fd = io_fd,
+        };
+
+    /* Older GCC cant initialise anonymous unions */

How old?

Quoted 10 lines
tools/libxc/saverestore/save_x86_pv.c new file +8 -0
+static int map_p2m(struct context *ctx)
+{
[...]
 
+    fll_mfn = GET_FIELD(ctx, ctx->x86_pv.shinfo, arch.pfn_to_mfn_frame_list_list);
+    if ( !fll_mfn )
+        IPRINTF("Waiting for domain to set up its p2m frame list list");
+
+    while ( tries-- && !fll_mfn )
+    {

tools/libxc/saverestore/save_x86_pv.c line 86

Quoted 10 lines
tools/libxc/saverestore/save_x86_pv.c new file +10 -0
+        usleep(10000);
+        fll_mfn = GET_FIELD(ctx, ctx->x86_pv.shinfo,
+                            arch.pfn_to_mfn_frame_list_list);
+    }
+
+    if ( !fll_mfn )
+    {
+        ERROR("Timed out waiting for p2m frame list list to be updated");
+        goto err;
+    }

This (preexisting) ugliness is there to "support" running migrate immediately after starting a guest, when it hasn't got its act together yet?

I wonder if we can get away with refusing to migrate under those circumstances.

Quoted 12 lines
tools/libxc/saverestore/save_x86_pv.c new file +12 -0
+static int write_one_vcpu_basic(struct context *ctx, uint32_t id)
+{
+    xc_interface *xch = ctx->xch;
+    xen_pfn_t mfn, pfn;
+    unsigned i;
+    int rc = -1;
+    vcpu_guest_context_any_t vcpu;
+    struct rec_x86_pv_vcpu vhdr = { .vcpu_id = id };
+    struct record rec =
+    {
+        .type = REC_TYPE_x86_pv_vcpu_basic,
+        .length = sizeof vhdr,

tools/libxc/saverestore/save_x86_pv.c line 193

Quoted 10 lines
tools/libxc/saverestore/save_x86_pv.c new file +10 -0
+        .data = &vhdr,
+    };
+
+    if ( xc_vcpu_getcontext(xch, ctx->domid, id, &vcpu) )
+    {
+        PERROR("Failed to get vcpu%u context", id);
+        goto err;
+    }
+
+    /* Vcpu 0 is special: Convert the suspend record to a PFN */

The "suspend record" is the guest's start_info_t I think? Would be clearer to say that I think (and perhaps allude to it being the magic 3rd parameter to the hypercall.)

Quoted 7 lines
tools/libxc/saverestore/save_x86_pv.c new file +7 -0
+    /* 64bit guests: Convert CR1 (guest pagetables) to PFN */
+    if ( ctx->x86_pv.levels == 4 && vcpu.x64.ctrlreg[1] )
+    {
+        mfn = vcpu.x64.ctrlreg[1] >> PAGE_SHIFT;
+        if ( !mfn_in_pseudophysmap(ctx, mfn) )
+        {
+            ERROR("Bad MFN for vcpu%u's cr1", id);

tools/libxc/saverestore/save_x86_pv.c line 249

Quoted 10 lines
tools/libxc/saverestore/save_x86_pv.c new file +10 -0
+            pseudophysmap_walk(ctx, mfn);
+            errno = ERANGE;
+            goto err;
+        }
+
+        pfn = mfn_to_pfn(ctx, mfn);
+        vcpu.x64.ctrlreg[1] = 1 | ((uint64_t)pfn << PAGE_SHIFT);
+    }
+
+    if ( ctx->x86_pv.width == 8 )

The use of x86_pv.levels vs x86_pv.width to determine the type of guest is rather inconsistent. .width would seem to be the right one to use almost all the time I think.

Quoted 15 lines
tools/libxc/saverestore/save_x86_pv.c new file +15 -0
+        rc = write_split_record(ctx, &rec, &vcpu, sizeof vcpu.x64);
+    else
+        rc = write_split_record(ctx, &rec, &vcpu, sizeof vcpu.x32);
+
+    if ( rc )
+        goto err;
+
+    DPRINTF("Writing vcpu%u basic context", id);
+    rc = 0;
+ err:
+
+    return rc;
+}
+
+static int write_one_vcpu_extended(struct context *ctx, uint32_t id)

tools/libxc/saverestore/save_x86_pv.c line 274

Quoted 10 lines
tools/libxc/saverestore/save_x86_pv.c new file +10 -0
+{
+    xc_interface *xch = ctx->xch;
+    int rc;
+    struct rec_x86_pv_vcpu vhdr = { .vcpu_id = id };
+    struct record rec =
+    {
+        .type = REC_TYPE_x86_pv_vcpu_extended,
+        .length = sizeof vhdr,
+        .data = &vhdr,
+    };

Does this pattern repeat a lot? DECLARE_REC(x86_pv_vcpu_extended, vhdr) perhaps?

tools/libxc/saverestore/save_x86_pv.c line 280

Quoted 6 lines
tools/libxc/saverestore/save_x86_pv.c new file +6 -0
+    struct xen_domctl domctl =
+    {
+        .cmd = XEN_DOMCTL_get_ext_vcpucontext,
+        .domain = ctx->domid,
+        .u.ext_vcpucontext.vcpu = id,
+    };

Wants to use DECLARE_DOMCTL I think (at least everywhere else in libxc seesms too).

Quoted 15 lines
tools/libxc/saverestore/save_x86_pv.c new file +15 -0
+static int write_all_vcpu_information(struct context *ctx)
+{
+    xc_interface *xch = ctx->xch;
+    xc_vcpuinfo_t vinfo;
+    unsigned int i;
+    int rc;
+
+    for ( i = 0; i <= ctx->dominfo.max_vcpu_id; ++i )
+    {
+        rc = xc_vcpu_getinfo(xch, ctx->domid, i, &vinfo);
+        if ( rc )
+        {
+            PERROR("Failed to get vcpu%u information", i);
+            return rc;
+        }

tools/libxc/saverestore/save_x86_pv.c line 385

Quoted 10 lines
tools/libxc/saverestore/save_x86_pv.c new file +10 -0
+
+        if ( !vinfo.online )
+        {
+            DPRINTF("vcpu%u offline - skipping", i);
+            continue;
+        }
+
+        rc = write_one_vcpu_basic(ctx, i) ?:
+            write_one_vcpu_extended(ctx, i) ?:
+            write_one_vcpu_xsave(ctx, i);

I'd prefer the more verbose one at a time version.

tools/libxc/saverestore/save_x86_pv.c line 487

Quoted 2 lines
tools/libxc/saverestore/save_x86_pv.c new file +2 -0
+    /* Map various structures */
+    rc = x86_pv_map_m2p(ctx) ?: map_shinfo(ctx) ?: map_p2m(ctx);

One at a time please.

tools/libxc/saverestore/save.c int write_headers(struct context *ctx, uint16_t guest_type) · line 99

Quoted 3 lines
tools/libxc/saverestore/save.c +3 -0
+    if ( rc )
+        goto err;
+

Ian.

#25 lore raw

Re: [PATCH v4 6/9] tools/libxc: x86 pv save implementation

Quoted 22 lines · Ian Campbell

On Wed, 2014-04-30 at 19:36 +0100, Andrew Cooper wrote:

The previous "common" patch seemed to have a fair bit of x86 pv save code in it, and there seems to be a fair bit of common code here. I suppose the split was pretty artificial to make the patches manageable?

tools/libxc/saverestore/common.h struct context · line 126

Quoted 5 lines
tools/libxc/saverestore/common.h +1 -0
@@ -121,6 +123,9 @@ struct context
     };
 };
 
+/* Saves an x86 PV domain. */

I should hope so!

Quoted 9 lines
tools/libxc/saverestore/common.h +2 -0
+int save_x86_pv(struct context *ctx);
+
 /*
  * Write the image and domain headers to the stream.
  * (to eventually make static in save.c)
@@ -137,6 +142,21 @@ struct record
     void *data;
 };
 
Quoted 3 lines

Review struct record · line 145

Quoted 1 line
(quoted diff) +1 -0
+/* Gets a field from an *_any union */

Unless the #undefs which were above before I trimmed them relate to patch #2/9 they should go right before the redefine IMHO.

At the moment the series is specifically designed to allow the legacy and v2 code to live together for testing purposes.

When this series leaves RFC, the old macros will be killed with fire. (See also gross abortions with scoped state)

Quoted 22 lines
Quoted 22 lines
tools/libxc/saverestore/common.h +18 -0
+#define GET_FIELD(_c, _p, _f)                   \
+    ({ (_c)->x86_pv.width == 8 ?                \
+            (_p)->x64._f:                       \
+            (_p)->x32._f;                       \
+    })                                          \
+
+/* Gets a field from an *_any union */
+#define SET_FIELD(_c, _p, _f, _v)               \
+    ({ if ( (_c)->x86_pv.width == 8 )           \
+            (_p)->x64._f = (_v);                \
+        else                                    \
+            (_p)->x32._f = (_v);                \
+    })
+
 /*
  * Writes a split record to the stream, applying correct padding where
  * appropriate.  It is common when sending records containing blobs from Xen
+    /* MFNs of the batch pfns */
+    mfns = malloc(nr_pfns * sizeof *mfns);
+    /* Types of the batch pfns */
+    types = malloc(nr_pfns * sizeof *types);
Quoted 8 lines

tools/libxc/saverestore/save.c int write_headers(struct context *ctx, uint16_t guest_type) · line 74

Quoted 6 lines
tools/libxc/saverestore/save.c +6 -0
+    /* Errors from attempting to map the mfns */
+    errors = malloc(nr_pfns * sizeof *errors);
+    /* Pointers to page data to send.  Might be from mapped mfns or local allocations */
+    guest_data = calloc(nr_pfns, sizeof *guest_data);
+    /* Pointers to locally allocated pages.  Probably not all used, but need freeing */
+    local_pages = calloc(nr_pfns, sizeof *local_pages);

Wouldn't it be better to preallocate (most of) these for the entire save/restore than to do it for each batch?

Valgrind vs (suspected)negligible overhead, particularly in this function which is probably the single most complex of the series.

Quoted 10 lines

tools/libxc/saverestore/save.c int write_headers(struct context *ctx, uint16_t guest_type) · line 89

Quoted 8 lines
tools/libxc/saverestore/save.c +7 -0
+    for ( i = 0; i < nr_pfns; ++i )
+    {
+        types[i] = mfns[i] = ctx->ops.pfn_to_gfn(ctx, ctx->batch_pfns[i]);
+
+        /* Likely a ballooned page */
+        if ( mfns[i] == INVALID_MFN )
+            set_bit(ctx->batch_pfns[i], ctx->deferred_pages);

deferred_pages doesn't need to grow dynamically like that bit map in a previous patch, does it?

On second thoughts, it probably does.

Quoted 9 lines

tools/libxc/saverestore/save.c int write_headers(struct context *ctx, uint16_t guest_type) · line 165

Quoted 3 lines
tools/libxc/saverestore/save.c +2 -0
+    hdr.count = nr_pfns;
+    s = nr_pfns * sizeof *rec_pfns;

Brackets around sizeof would really help here.

tools/libxc/saverestore/save.c int write_headers(struct context *ctx, uint16_t guest_type) · line 168

Quoted 3 lines
tools/libxc/saverestore/save.c +3 -0
+
+
+    rec_pfns = malloc(s);

Given the calculation of s is this a candidate for calloc?

s was actually some debugging which managed to stay in. I shall clean it up a little.

Quoted 11 lines

tools/libxc/saverestore/save.c int write_headers(struct context *ctx, uint16_t guest_type) · line 176

Quoted 9 lines
tools/libxc/saverestore/save.c +8 -0
+    if ( !rec_pfns )
+    {
+        ERROR("Unable to allocate memory for page data pfn list");
+        goto err;
+    }
+
+    for ( i = 0; i < nr_pfns; ++i )
+        rec_pfns[i] = ((uint64_t)(types[i]) << 32) | ctx->batch_pfns[i];

Could this not have a more structured type? (I noticed this in the decode bit of the previous patch too).

Not without resorting to bitfields, which I would prefer to avoid for data ending up being written into a stream.

Quoted 6 lines

tools/libxc/saverestore/save.c int write_headers(struct context *ctx, uint16_t guest_type) · line 179

Quoted 4 lines
tools/libxc/saverestore/save.c +3 -0
+
+    /*        header +          pfns data           +        page data */
+    rec_size = 4 + 4 + (s) + (nr_pages * PAGE_SIZE);

Can these 4's not be sizeof something? Also the comment looks like it wants to align with something, but is failing.

Again, leaked debugging code.

Quoted 6 lines

tools/libxc/saverestore/save.c int write_headers(struct context *ctx, uint16_t guest_type) · line 183

Quoted 5 lines
tools/libxc/saverestore/save.c +4 -0
+    rec_count = nr_pfns;
+
+    if ( write_exact(ctx->fd, &rec_type, sizeof(uint32_t)) ||
+         write_exact(ctx->fd, &rec_size, sizeof(uint32_t)) ||

Are these not a struct rhdr?

Yes,

I intended to implement "write_record_raw()" or so which trusted the length calculation given, although I appear to have lost the tuits. I should find them again.

Quoted 19 lines
Quoted 6 lines
tools/libxc/saverestore/save.c +5 -0
+         write_exact(ctx->fd, &rec_count, sizeof(uint32_t)) ||
+         write_exact(ctx->fd, &rec_res, sizeof(uint32_t)) )
+    {
+        PERROR("Failed to write page_type header to stream");
+        goto err;

tools/libxc/saverestore/save.c int write_headers(struct context *ctx, uint16_t guest_type) · line 198

Quoted 10 lines
tools/libxc/saverestore/save.c +10 -0
+    }
+
+    if ( write_exact(ctx->fd, rec_pfns, s) )
+    {
+        PERROR("Failed to write page_type header to stream");
+        goto err;
+    }
+
+
+    for ( i = 0; i < nr_pfns; ++i )

Extra braces are useful for clarity in these siturations.

Quoted 13 lines

tools/libxc/saverestore/save.c int write_headers(struct context *ctx, uint16_t guest_type) · line 241

Quoted 12 lines
tools/libxc/saverestore/save.c +12 -0
+static int flush_batch(struct context *ctx)
+{
+    int rc = 0;
+
+    if ( ctx->nr_batch_pfns == 0 )
+        return rc;
+
+    rc = write_batch(ctx);
+
+    if ( !rc )
+    {
+        /* Valgrind sanity check */

What now?

This results in the array being marked as unused again, which was useful when I found two pieces of code playing with the index and write_batch() finding some plausible-but-wrong pfns to send.

Quoted 12 lines

tools/libxc/saverestore/save.c int write_headers(struct context *ctx, uint16_t guest_type) · line 284

Quoted 10 lines
tools/libxc/saverestore/save.c +9 -0
+        free(ctx->batch_pfns);
+        ctx->batch_pfns = malloc(MAX_BATCH_SIZE * sizeof *ctx->batch_pfns);
+        rc = !ctx->batch_pfns;
+    }
+
+
+    for ( x = 0, pages_written = 0; x < max_iter ; ++x )
+    {
+        DPRINTF("Iteration %u", x);

Something in this series should be using xc_report_progress_* (I'm not sure that place is here, but I didn't see it elsewhere yet).

Ah yes - I think other areas want to do the same.

Quoted 8 lines

tools/libxc/saverestore/save.c int write_headers(struct context *ctx, uint16_t guest_type) · line 460

Quoted 8 lines

int xc_domain_save2(xc_interface *xch, int io_fd, uint32_t dom, uint32_t max_iters,
uint32_t max_factor, uint32_t flags,
struct save_callbacks* callbacks, int hvm, unsigned long vm_generationid_addr)
{

tools/libxc/saverestore/save.c +2 -0
+    struct context ctx =
+        {
Quoted 8 lines

Coding style.

tools/libxc/saverestore/save.c int write_headers(struct context *ctx, uint16_t guest_type) · line 465

Quoted 5 lines
tools/libxc/saverestore/save.c +5 -0
+            .xch = xch,
+            .fd = io_fd,
+        };
+
+    /* Older GCC cant initialise anonymous unions */

How old?

4.7 in debian is fine. 4.4 in CentOS 6 isn't

Quoted 16 lines
Quoted 16 lines
tools/libxc/saverestore/save_x86_pv.c new file +13 -0
+static int map_p2m(struct context *ctx)
+{
[...]
+    fll_mfn = GET_FIELD(ctx, ctx->x86_pv.shinfo, arch.pfn_to_mfn_frame_list_list);
+    if ( !fll_mfn )
+        IPRINTF("Waiting for domain to set up its p2m frame list list");
+
+    while ( tries-- && !fll_mfn )
+    {
+        usleep(10000);
+        fll_mfn = GET_FIELD(ctx, ctx->x86_pv.shinfo,
+                            arch.pfn_to_mfn_frame_list_list);
+    }
+
Quoted 8 lines

tools/libxc/saverestore/save_x86_pv.c line 86

Quoted 5 lines
tools/libxc/saverestore/save_x86_pv.c new file +5 -0
+    if ( !fll_mfn )
+    {
+        ERROR("Timed out waiting for p2m frame list list to be updated");
+        goto err;
+    }

This (preexisting) ugliness is there to "support" running migrate immediately after starting a guest, when it hasn't got its act together yet?

Yes - as the guest is in control of its p2m, it has to set itself up sufficiently first before the toolstack can migrate it.

Quoted 3 lines

I wonder if we can get away with refusing to migrate under those circumstances.

Probably, although I thought I tried removing this originally then found a case where I still needed it. It would be nice to remove it if possible.

Quoted 18 lines
Quoted 18 lines
tools/libxc/saverestore/save_x86_pv.c new file +17 -0
+static int write_one_vcpu_basic(struct context *ctx, uint32_t id)
+{
+    xc_interface *xch = ctx->xch;
+    xen_pfn_t mfn, pfn;
+    unsigned i;
+    int rc = -1;
+    vcpu_guest_context_any_t vcpu;
+    struct rec_x86_pv_vcpu vhdr = { .vcpu_id = id };
+    struct record rec =
+    {
+        .type = REC_TYPE_x86_pv_vcpu_basic,
+        .length = sizeof vhdr,
+        .data = &vhdr,
+    };
+
+    if ( xc_vcpu_getcontext(xch, ctx->domid, id, &vcpu) )
+    {
Quoted 8 lines

tools/libxc/saverestore/save_x86_pv.c line 193

Quoted 5 lines
tools/libxc/saverestore/save_x86_pv.c new file +5 -0
+        PERROR("Failed to get vcpu%u context", id);
+        goto err;
+    }
+
+    /* Vcpu 0 is special: Convert the suspend record to a PFN */

The "suspend record" is the guest's start_info_t I think? Would be clearer to say that I think (and perhaps allude to it being the magic 3rd parameter to the hypercall.)

It is inconsistently referred to in the toolstack and Xen public headers. I think I inherited the name "suspend record" from the legacy code.

Quoted 13 lines
Quoted 13 lines
tools/libxc/saverestore/save_x86_pv.c new file +12 -0
+    /* 64bit guests: Convert CR1 (guest pagetables) to PFN */
+    if ( ctx->x86_pv.levels == 4 && vcpu.x64.ctrlreg[1] )
+    {
+        mfn = vcpu.x64.ctrlreg[1] >> PAGE_SHIFT;
+        if ( !mfn_in_pseudophysmap(ctx, mfn) )
+        {
+            ERROR("Bad MFN for vcpu%u's cr1", id);
+            pseudophysmap_walk(ctx, mfn);
+            errno = ERANGE;
+            goto err;
+        }
+
Quoted 8 lines

tools/libxc/saverestore/save_x86_pv.c line 249

Quoted 5 lines
tools/libxc/saverestore/save_x86_pv.c new file +5 -0
+        pfn = mfn_to_pfn(ctx, mfn);
+        vcpu.x64.ctrlreg[1] = 1 | ((uint64_t)pfn << PAGE_SHIFT);
+    }
+
+    if ( ctx->x86_pv.width == 8 )

The use of x86_pv.levels vs x86_pv.width to determine the type of guest is rather inconsistent. .width would seem to be the right one to use almost all the time I think.

I (believe I) have consistently used levels when dealing with pagetable stuff, and width when dealing with bitness issues.

I suppose the difference was more pronounced in the legacy code with 32bit 2 and 3 level guests, but I still think this split is the better way of distinguishing.

Quoted 20 lines
Quoted 20 lines
tools/libxc/saverestore/save_x86_pv.c new file +19 -0
+        rc = write_split_record(ctx, &rec, &vcpu, sizeof vcpu.x64);
+    else
+        rc = write_split_record(ctx, &rec, &vcpu, sizeof vcpu.x32);
+
+    if ( rc )
+        goto err;
+
+    DPRINTF("Writing vcpu%u basic context", id);
+    rc = 0;
+ err:
+
+    return rc;
+}
+
+static int write_one_vcpu_extended(struct context *ctx, uint32_t id)
+{
+    xc_interface *xch = ctx->xch;
+    int rc;
+    struct rec_x86_pv_vcpu vhdr = { .vcpu_id = id };
Quoted 8 lines

tools/libxc/saverestore/save_x86_pv.c line 274

Quoted 6 lines
tools/libxc/saverestore/save_x86_pv.c new file +6 -0
+    struct record rec =
+    {
+        .type = REC_TYPE_x86_pv_vcpu_extended,
+        .length = sizeof vhdr,
+        .data = &vhdr,
+    };

Does this pattern repeat a lot? DECLARE_REC(x86_pv_vcpu_extended, vhdr) perhaps?

It repeats a little but not too often. However I frankly dislike macros like that which do opaque initialisation of trivial structures.

Quoted 9 lines

tools/libxc/saverestore/save_x86_pv.c line 280

Quoted 7 lines
tools/libxc/saverestore/save_x86_pv.c new file +6 -0
+    struct xen_domctl domctl =
+    {
+        .cmd = XEN_DOMCTL_get_ext_vcpucontext,
+        .domain = ctx->domid,
+        .u.ext_vcpucontext.vcpu = id,
+    };

Wants to use DECLARE_DOMCTL I think (at least everywhere else in libxc seesms too).

Which has no difference now I dropped -DVALGRIND from libxc, but forgoes the clarity and optimisation available from structure initialisation.

As previously indicated, I have an issue with macros which use or create unidentified symbols in scope, because they are unconditionally less clear than the plain alternative.

Quoted 28 lines
Quoted 16 lines
tools/libxc/saverestore/save_x86_pv.c new file +15 -0
+static int write_all_vcpu_information(struct context *ctx)
+{
+    xc_interface *xch = ctx->xch;
+    xc_vcpuinfo_t vinfo;
+    unsigned int i;
+    int rc;
+
+    for ( i = 0; i <= ctx->dominfo.max_vcpu_id; ++i )
+    {
+        rc = xc_vcpu_getinfo(xch, ctx->domid, i, &vinfo);
+        if ( rc )
+        {
+            PERROR("Failed to get vcpu%u information", i);
+            return rc;
+        }

tools/libxc/saverestore/save_x86_pv.c line 385

Quoted 10 lines
tools/libxc/saverestore/save_x86_pv.c new file +10 -0
+
+        if ( !vinfo.online )
+        {
+            DPRINTF("vcpu%u offline - skipping", i);
+            continue;
+        }
+
+        rc = write_one_vcpu_basic(ctx, i) ?:
+            write_one_vcpu_extended(ctx, i) ?:
+            write_one_vcpu_xsave(ctx, i);

I'd prefer the more verbose one at a time version.

Quoted 3 lines

tools/libxc/saverestore/save_x86_pv.c line 487

Quoted 2 lines
tools/libxc/saverestore/save_x86_pv.c new file +2 -0
+    /* Map various structures */
+    rc = x86_pv_map_m2p(ctx) ?: map_shinfo(ctx) ?: map_p2m(ctx);

One at a time please.

Ok.

~Andrew

#26 lore raw

Re: [PATCH v4 6/9] tools/libxc: x86 pv save implementation

Quoted 27 lines · Andrew Cooper
Quoted 22 lines · Ian Campbell

On Wed, 2014-04-30 at 19:36 +0100, Andrew Cooper wrote:

The previous "common" patch seemed to have a fair bit of x86 pv save code in it, and there seems to be a fair bit of common code here. I suppose the split was pretty artificial to make the patches manageable?

tools/libxc/saverestore/common.h struct context · line 126

Quoted 5 lines
tools/libxc/saverestore/common.h +1 -0
@@ -121,6 +123,9 @@ struct context
     };
 };
 
+/* Saves an x86 PV domain. */

I should hope so!

Quoted 9 lines
tools/libxc/saverestore/common.h +2 -0
+int save_x86_pv(struct context *ctx);
+
 /*
  * Write the image and domain headers to the stream.
  * (to eventually make static in save.c)
@@ -137,6 +142,21 @@ struct record
     void *data;
 };
 
Quoted 3 lines

Review struct record · line 145

Quoted 1 line
(quoted diff) +1 -0
+/* Gets a field from an *_any union */

Unless the #undefs which were above before I trimmed them relate to patch #2/9 they should go right before the redefine IMHO.

Quoted 5 lines

At the moment the series is specifically designed to allow the legacy and v2 code to live together for testing purposes.

When this series leaves RFC, the old macros will be killed with fire. (See also gross abortions with scoped state)

I think it was later on where I requested for things subject to that 2/9 related future path be explicily tagged as such.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
tools/libxc/saverestore/save.c +8 -0
+    if ( !rec_pfns )
+    {
+        ERROR("Unable to allocate memory for page data pfn list");
+        goto err;
+    }
+
+    for ( i = 0; i < nr_pfns; ++i )
+        rec_pfns[i] = ((uint64_t)(types[i]) << 32) | ctx->batch_pfns[i];
Quoted 5 lines
Quoted 2 lines

Could this not have a more structured type? (I noticed this in the decode bit of the previous patch too).

Not without resorting to bitfields, which I would prefer to avoid for data ending up being written into a stream.

Fair enough.

Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
tools/libxc/saverestore/save.c +5 -0
+static int flush_batch(struct context *ctx)
+{
+    int rc = 0;
+
+    if ( ctx->nr_batch_pfns == 0 )
Quoted 8 lines

tools/libxc/saverestore/save.c int write_headers(struct context *ctx, uint16_t guest_type) · line 241

Quoted 7 lines
tools/libxc/saverestore/save.c +7 -0
+        return rc;
+
+    rc = write_batch(ctx);
+
+    if ( !rc )
+    {
+        /* Valgrind sanity check */

What now?

Quoted 3 lines

This results in the array being marked as unused again, which was useful when I found two pieces of code playing with the index and write_batch() finding some plausible-but-wrong pfns to send.

If it is going to stay then the comment needs to be more explicit about this.

Maybe make all this stuff #idndef NDEBUG? Except we don't have that for tools, nevermind.

Quoted 4 lines
Quoted 2 lines
Quoted 1 line

+ /* Older GCC cant initialise anonymous unions */

How old?

4.7 in debian is fine. 4.4 in CentOS 6 isn't

How irritating. Please mention it so someone looking at this in 5 years can decide to clean it up.

Quoted 22 lines
Quoted 13 lines
Quoted 13 lines
tools/libxc/saverestore/save_x86_pv.c new file +12 -0
+    /* 64bit guests: Convert CR1 (guest pagetables) to PFN */
+    if ( ctx->x86_pv.levels == 4 && vcpu.x64.ctrlreg[1] )
+    {
+        mfn = vcpu.x64.ctrlreg[1] >> PAGE_SHIFT;
+        if ( !mfn_in_pseudophysmap(ctx, mfn) )
+        {
+            ERROR("Bad MFN for vcpu%u's cr1", id);
+            pseudophysmap_walk(ctx, mfn);
+            errno = ERANGE;
+            goto err;
+        }
+
Quoted 8 lines

tools/libxc/saverestore/save_x86_pv.c line 249

Quoted 5 lines
tools/libxc/saverestore/save_x86_pv.c new file +5 -0
+        pfn = mfn_to_pfn(ctx, mfn);
+        vcpu.x64.ctrlreg[1] = 1 | ((uint64_t)pfn << PAGE_SHIFT);
+    }
+
+    if ( ctx->x86_pv.width == 8 )

The use of x86_pv.levels vs x86_pv.width to determine the type of guest is rather inconsistent. .width would seem to be the right one to use almost all the time I think.

Quoted 2 lines

I (believe I) have consistently used levels when dealing with pagetable stuff, and width when dealing with bitness issues.

I think I first noticed it when there was one within the other, but I don't remember which patch that was in.

Quoted 3 lines

I suppose the difference was more pronounced in the legacy code with 32bit 2 and 3 level guests, but I still think this split is the better way of distinguishing.

OK then.

Ian.

#27 lore raw

PATCH v4 7/9 tools/libxc: x86 pv restore implementation

In reply to Andrew Cooper
---
 tools/libxc/saverestore/common.h         |   20 +
 tools/libxc/saverestore/restore.c        |  115 ++++-
 tools/libxc/saverestore/restore_x86_pv.c |  805 ++++++++++++++++++++++++++++++
 3 files changed, 939 insertions(+), 1 deletion(-)
 create mode 100644 tools/libxc/saverestore/restore_x86_pv.c
tools/libxc/saverestore/common.h +20 -0
index 116eb13..259cb1f 100644
@@ -14,6 +14,8 @@
 
 #undef GET_FIELD
 #undef SET_FIELD
+#undef MEMCPY_FIELD
+#undef MEMSET_ARRAY_FIELD
 #undef mfn_to_pfn
 #undef pfn_to_mfn
 
@@ -125,6 +127,8 @@ struct context
 
 /* Saves an x86 PV domain. */
 int save_x86_pv(struct context *ctx);
+/* Restores an x86 PV domain. */
+int restore_x86_pv(struct context *ctx);
 
 /*
  * Write the image and domain headers to the stream.
@@ -157,6 +161,22 @@ struct record
             (_p)->x32._f = (_v);                \
     })
 
+/* memcpy field _f from _s to _d, of an *_any union */
+#define MEMCPY_FIELD(_c, _d, _s, _f)                                    \
+    ({ if ( (_c)->x86_pv.width == 8 )                                   \
+            memcpy(&(_d)->x64._f, &(_s)->x64._f, sizeof((_d)->x64._f)); \
+        else                                                            \
+            memcpy(&(_d)->x32._f, &(_s)->x32._f, sizeof((_d)->x32._f)); \
+    })
+
+/* memset array field _f with value _v, from an *_any union */
+#define MEMSET_ARRAY_FIELD(_c, _d, _f, _v)                              \
+    ({ if ( (_c)->x86_pv.width == 8 )                                   \
+           memset(&(_d)->x64._f[0], (_v), sizeof((_d)->x64._f));        \
+       else                                                             \
+           memset(&(_d)->x32._f[0], (_v), sizeof((_d)->x32._f));        \
+    })
+
 /*
  * Writes a split record to the stream, applying correct padding where
  * appropriate.  It is common when sending records containing blobs from Xen
tools/libxc/saverestore/restore.c +114 -1

127 lines not on this page · raw message

tools/libxc/saverestore/restore_x86_pv.c new file +805 -0

805 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#28 lore raw

Re: [PATCH v4 7/9] tools/libxc: x86 pv restore implementation

tools/libxc/saverestore/restore.c line 26

Quoted 10 lines · Andrew Cooper
tools/libxc/saverestore/restore.c +10 -0
+    ihdr.id      = ntohl(ihdr.id);
+    ihdr.version = ntohl(ihdr.version);
+    ihdr.options = ntohs(ihdr.options);
+
+    if ( ihdr.marker != IHDR_MARKER )
+    {
+        ERROR("Invalid marker: Got 0x%016"PRIx64, ihdr.marker);
+        return -1;
+    }
+    else if ( ihdr.id != IHDR_ID )

Can we do away with the unnecessary elses please.

Quoted 14 lines
tools/libxc/saverestore/restore.c +14 -0
+    if ( xc_domain_getinfo(xch, dom, 1, &ctx.dominfo) != 1 )
+    {
+        PERROR("Failed to get domain info");
+        return -1;
+    }
+
+    if ( ctx.dominfo.domid != dom )
+    {
+        ERROR("Domain %d does not exist", dom);
+        return -1;
+    }
+
+    ctx.domid = dom;
+    IPRINTF("Restoring domain %d", dom);

tools/libxc/saverestore/restore.c int xc_domain_restore2(xc_interface *xch, int io_fd, uint32_t dom, · line 105

Quoted 10 lines
tools/libxc/saverestore/restore.c +10 -0
+
+    if ( read_headers(&ctx) )
+        return -1;
+
+    if ( ctx.dominfo.hvm )
+    {
+        ERROR("HVM Restore not supported yet");
+        return -1;
+    }
+    else

Unneeded.

Quoted 5 lines
tools/libxc/saverestore/restore_x86_pv.c new file +5 -0
+
+static int handle_x86_pv_vcpu_basic(struct context *ctx, struct record *rec)
+{
+    xc_interface *xch = ctx->xch;
+    struct rec_x86_pv_vcpu *vhdr = rec->data;

tools/libxc/saverestore/restore_x86_pv.c line 388

Quoted 10 lines
tools/libxc/saverestore/restore_x86_pv.c new file +10 -0
+    vcpu_guest_context_any_t vcpu;
+    size_t vcpusz = ctx->x86_pv.width == 8 ? sizeof vcpu.x64 : sizeof vcpu.x32;
+    xen_pfn_t pfn, mfn;
+    unsigned long tmp;
+    unsigned i;
+    int rc = -1;
+
+    if ( rec->length <= sizeof *vhdr )
+    {
+        ERROR("X86_PV_VCPU_BASIC record trucated: length %"PRIu32", min %zu",

"truncated"

tools/libxc/saverestore/restore_x86_pv.c line 491

Quoted 2 lines
tools/libxc/saverestore/restore_x86_pv.c new file +2 -0
+        else if ( (ctx->x86_pv.pfn_types[pfn] & XEN_DOMCTL_PFINFO_LTABTYPE_MASK) !=
+                  (((xen_pfn_t)ctx->x86_pv.levels) << XEN_DOMCTL_PFINFO_LTAB_SHIFT) )

I'd think some temporaries would help with clarity here.

tools/libxc/saverestore/restore_x86_pv.c line 531

Quoted 10 lines
tools/libxc/saverestore/restore_x86_pv.c new file +10 -0
+static int handle_x86_pv_vcpu_extended(struct context *ctx, struct record *rec)
+{
+    xc_interface *xch = ctx->xch;
+    struct rec_x86_pv_vcpu *vcpu = rec->data;
+    DECLARE_DOMCTL;
+
+    if ( rec->length <= sizeof *vcpu )
+    {
+        ERROR("X86_PV_VCPU_EXTENDED record trucated: length %"PRIu32", min %zu",
+              rec->length, sizeof *vcpu + 1);

I've just realised that there was a X86_PV_VCPU_EXTENDED in handle_x86_pv_vcpu_basic which was wrong, but I've trimmed it already so I'll comment here.

tools/libxc/saverestore/restore_x86_pv.c line 534

Quoted 3 lines
tools/libxc/saverestore/restore_x86_pv.c new file +3 -0
+        return -1;
+    }
+    else if ( rec->length > sizeof *vcpu + 128 )

There's an awful lot of magic numbers throughout this series. Can we try and use meaningful symbolic names for things please.

Quoted 5 lines
tools/libxc/saverestore/restore_x86_pv.c new file +5 -0
+int restore_x86_pv(struct context *ctx)
+{
+    xc_interface *xch = ctx->xch;
+    struct record rec;
+    int rc;

tools/libxc/saverestore/restore_x86_pv.c line 676

Quoted 10 lines
tools/libxc/saverestore/restore_x86_pv.c new file +10 -0
+
+    IPRINTF("In experimental %s", __func__);
+
+    if ( ctx->restore.guest_type != DHDR_TYPE_x86_pv )
+    {
+        ERROR("Unable to restore %s domain into an x86_pv domain",
+              dhdr_type_to_str(ctx->restore.guest_type));
+        return -1;
+    }
+    else if ( ctx->restore.guest_page_size != 4096 )

#define's exist for this.

tools/libxc/saverestore/restore_x86_pv.c line 783

Quoted 7 lines
tools/libxc/saverestore/restore_x86_pv.c new file +7 -0
+    /* all done */
+    IPRINTF("All Done");
+    assert(!rc);
+    goto cleanup;
+
+ err:
+    assert(rc);

FWIW I think you could drop this and let the success case fall through without the hop and a skip..

Quoted 22 lines

+ cleanup:

tools/libxc/saverestore/restore_x86_pv.c new file +21 -0
+
+    free(ctx->x86_pv.p2m);
+    free(ctx->x86_pv.p2m_pfns);
+    free(ctx->x86_pv.pfn_types);
+    free(ctx->restore.populated_pfns);
+
+    if ( ctx->x86_pv.m2p )
+        munmap(ctx->x86_pv.m2p, ctx->x86_pv.nr_m2p_frames * PAGE_SIZE);
+
+    return rc;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
#29 lore raw

Re: [PATCH v4 7/9] tools/libxc: x86 pv restore implementation

Quoted 18 lines · Ian Campbell
Quoted 18 lines
tools/libxc/saverestore/restore.c +17 -0
+    if ( xc_domain_getinfo(xch, dom, 1, &ctx.dominfo) != 1 )
+    {
+        PERROR("Failed to get domain info");
+        return -1;
+    }
+
+    if ( ctx.dominfo.domid != dom )
+    {
+        ERROR("Domain %d does not exist", dom);
+        return -1;
+    }
+
+    ctx.domid = dom;
+    IPRINTF("Restoring domain %d", dom);
+
+    if ( read_headers(&ctx) )
+        return -1;
Quoted 8 lines

tools/libxc/saverestore/restore.c int xc_domain_restore2(xc_interface *xch, int io_fd, uint32_t dom, · line 105

Quoted 7 lines
tools/libxc/saverestore/restore.c +7 -0
+
+    if ( ctx.dominfo.hvm )
+    {
+        ERROR("HVM Restore not supported yet");
+        return -1;
+    }
+    else

Unneeded.

Perhaps, but it makes more readable patches as hvm restore is added later.

Furthermore, all of this will need tweaking when this series stops trying to coexist alongside the legacy code.

Quoted 18 lines
Quoted 6 lines
tools/libxc/saverestore/restore_x86_pv.c new file +5 -0
+
+static int handle_x86_pv_vcpu_basic(struct context *ctx, struct record *rec)
+{
+    xc_interface *xch = ctx->xch;
+    struct rec_x86_pv_vcpu *vhdr = rec->data;

tools/libxc/saverestore/restore_x86_pv.c line 388

Quoted 10 lines
tools/libxc/saverestore/restore_x86_pv.c new file +10 -0
+    vcpu_guest_context_any_t vcpu;
+    size_t vcpusz = ctx->x86_pv.width == 8 ? sizeof vcpu.x64 : sizeof vcpu.x32;
+    xen_pfn_t pfn, mfn;
+    unsigned long tmp;
+    unsigned i;
+    int rc = -1;
+
+    if ( rec->length <= sizeof *vhdr )
+    {
+        ERROR("X86_PV_VCPU_BASIC record trucated: length %"PRIu32", min %zu",

"truncated"

Quoted 3 lines

tools/libxc/saverestore/restore_x86_pv.c line 491

Quoted 2 lines
tools/libxc/saverestore/restore_x86_pv.c new file +2 -0
+        else if ( (ctx->x86_pv.pfn_types[pfn] & XEN_DOMCTL_PFINFO_LTABTYPE_MASK) !=
+                  (((xen_pfn_t)ctx->x86_pv.levels) << XEN_DOMCTL_PFINFO_LTAB_SHIFT) )

I'd think some temporaries would help with clarity here.

This was code largely cribbed from the legacy code, as it is gnarly and brittle and I didn't fancy subtly breaking something in the middle. I will see what I can do to clean it up.

Quoted 14 lines

tools/libxc/saverestore/restore_x86_pv.c line 531

Quoted 11 lines
tools/libxc/saverestore/restore_x86_pv.c new file +10 -0
+static int handle_x86_pv_vcpu_extended(struct context *ctx, struct record *rec)
+{
+    xc_interface *xch = ctx->xch;
+    struct rec_x86_pv_vcpu *vcpu = rec->data;
+    DECLARE_DOMCTL;
+
+    if ( rec->length <= sizeof *vcpu )
+    {
+        ERROR("X86_PV_VCPU_EXTENDED record trucated: length %"PRIu32", min %zu",
+              rec->length, sizeof *vcpu + 1);

I've just realised that there was a X86_PV_VCPU_EXTENDED in handle_x86_pv_vcpu_basic which was wrong, but I've trimmed it already so I'll comment here.

Noted.

Quoted 7 lines

tools/libxc/saverestore/restore_x86_pv.c line 534

Quoted 5 lines
tools/libxc/saverestore/restore_x86_pv.c new file +3 -0
+        return -1;
+    }
+    else if ( rec->length > sizeof *vcpu + 128 )

There's an awful lot of magic numbers throughout this series. Can we try and use meaningful symbolic names for things please.

This one, no. It is magic even in the legacy stream, and is sizeof domctl.u on the sending toolstack, which thinking forward is not necessarily sizeof domctl.u on the receiving toolstack.

Quoted 25 lines
Quoted 6 lines
tools/libxc/saverestore/restore_x86_pv.c new file +5 -0
+int restore_x86_pv(struct context *ctx)
+{
+    xc_interface *xch = ctx->xch;
+    struct record rec;
+    int rc;

tools/libxc/saverestore/restore_x86_pv.c line 676

Quoted 10 lines
tools/libxc/saverestore/restore_x86_pv.c new file +10 -0
+
+    IPRINTF("In experimental %s", __func__);
+
+    if ( ctx->restore.guest_type != DHDR_TYPE_x86_pv )
+    {
+        ERROR("Unable to restore %s domain into an x86_pv domain",
+              dhdr_type_to_str(ctx->restore.guest_type));
+        return -1;
+    }
+    else if ( ctx->restore.guest_page_size != 4096 )

#define's exist for this.

tools/libxc/saverestore/restore_x86_pv.c line 783

Quoted 7 lines
tools/libxc/saverestore/restore_x86_pv.c new file +7 -0
+    /* all done */
+    IPRINTF("All Done");
+    assert(!rc);
+    goto cleanup;
+
+ err:
+    assert(rc);
Quoted 2 lines

FWIW I think you could drop this and let the success case fall through without the hop and a skip..

I would prefer not to. This assert has already caught 1 error of mine, and I seem to remember also caught a bug in an early version of c/s dda0b77d2caa

~Andrew

Quoted 24 lines
Quoted 23 lines

+ cleanup:

tools/libxc/saverestore/restore_x86_pv.c new file +21 -0
+
+    free(ctx->x86_pv.p2m);
+    free(ctx->x86_pv.p2m_pfns);
+    free(ctx->x86_pv.pfn_types);
+    free(ctx->restore.populated_pfns);
+
+    if ( ctx->x86_pv.m2p )
+        munmap(ctx->x86_pv.m2p, ctx->x86_pv.nr_m2p_frames * PAGE_SIZE);
+
+    return rc;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
#30 lore raw

Re: [PATCH v4 7/9] tools/libxc: x86 pv restore implementation

Quoted 28 lines · Andrew Cooper
Quoted 18 lines · Ian Campbell
Quoted 18 lines
tools/libxc/saverestore/restore.c +17 -0
+    if ( xc_domain_getinfo(xch, dom, 1, &ctx.dominfo) != 1 )
+    {
+        PERROR("Failed to get domain info");
+        return -1;
+    }
+
+    if ( ctx.dominfo.domid != dom )
+    {
+        ERROR("Domain %d does not exist", dom);
+        return -1;
+    }
+
+    ctx.domid = dom;
+    IPRINTF("Restoring domain %d", dom);
+
+    if ( read_headers(&ctx) )
+        return -1;
Quoted 8 lines

tools/libxc/saverestore/restore.c int xc_domain_restore2(xc_interface *xch, int io_fd, uint32_t dom, · line 105

Quoted 7 lines
tools/libxc/saverestore/restore.c +7 -0
+
+    if ( ctx.dominfo.hvm )
+    {
+        ERROR("HVM Restore not supported yet");
+        return -1;
+    }
+    else

Unneeded.

Quoted 1 line

Perhaps, but it makes more readable patches as hvm restore is added later.

OK, I think this one got dumped in with all the other actually unnecessary elses in my mind.

Quoted 12 lines

Furthermore, all of this will need tweaking when this series stops trying to coexist alongside the legacy code.

From a reviewer's point of view the sooner this happens the better.

Quoted 5 lines
Quoted 5 lines
tools/libxc/saverestore/restore_x86_pv.c new file +3 -0
+        return -1;
+    }
+    else if ( rec->length > sizeof *vcpu + 128 )
Quoted 6 lines
Quoted 2 lines

There's an awful lot of magic numbers throughout this series. Can we try and use meaningful symbolic names for things please.

This one, no. It is magic even in the legacy stream, and is sizeof domctl.u on the sending toolstack, which thinking forward is not necessarily sizeof domctl.u on the receiving toolstack.

OK. Where a legitimate magic number remains then please add a comment.

Ian.

#31 lore raw

PATCH v4 8/9 tools/libxc: x86 hvm save implementation

In reply to Andrew Cooper
---
 tools/libxc/saverestore/common.h       |    2 +
 tools/libxc/saverestore/save.c         |    4 +-
 tools/libxc/saverestore/save_x86_hvm.c |  220 ++++++++++++++++++++++++++++++++
 3 files changed, 224 insertions(+), 2 deletions(-)
 create mode 100644 tools/libxc/saverestore/save_x86_hvm.c
tools/libxc/saverestore/common.h +2 -0
index 259cb1f..e26b879 100644
@@ -127,6 +127,8 @@ struct context
 
 /* Saves an x86 PV domain. */
 int save_x86_pv(struct context *ctx);
+/* Saves an x86 HVM domain. */
+int save_x86_hvm(struct context *ctx);
 /* Restores an x86 PV domain. */
 int restore_x86_pv(struct context *ctx);
 
tools/libxc/saverestore/save.c +2 -2
index a19c217..4bff29b 100644
@@ -492,8 +492,8 @@ int xc_domain_save2(xc_interface *xch, int io_fd, uint32_t dom, uint32_t max_ite
 
     if ( ctx.dominfo.hvm )
     {
-        ERROR("HVM Save not supported yet");
-        return -1;
+        ctx.ops = save_restore_ops_x86_hvm;
+        return save_x86_hvm(&ctx);
     }
     else
     {
tools/libxc/saverestore/save_x86_hvm.c new file +220 -0

220 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#32 lore raw

Re: [PATCH v4 8/9] tools/libxc: x86 hvm save implementation

Quoted 7 lines · Andrew Cooper
tools/libxc/saverestore/save_x86_hvm.c new file +7 -0
+static int write_hvm_params(struct context *ctx)
+{
+    static const unsigned int params[] = {
+        HVM_PARAM_STORE_PFN,
+        HVM_PARAM_IOREQ_PFN,
+        HVM_PARAM_BUFIOREQ_PFN,
+        HVM_PARAM_PAGING_RING_PFN,

tools/libxc/saverestore/save_x86_hvm.c line 67

Quoted 10 lines
tools/libxc/saverestore/save_x86_hvm.c new file +10 -0
+        HVM_PARAM_ACCESS_RING_PFN,
+        HVM_PARAM_SHARING_RING_PFN,
+        HVM_PARAM_VM86_TSS,
+        HVM_PARAM_CONSOLE_PFN,
+        HVM_PARAM_ACPI_IOPORTS_LOCATION,
+        HVM_PARAM_VIRIDIAN,
+        HVM_PARAM_IDENT_PT,
+        HVM_PARAM_PAE_ENABLED,
+    };
+    static const unsigned int num_params = ARRAY_SIZE(params);

Blank line between statics and regular please.

Quoted 4 lines
tools/libxc/saverestore/save_x86_hvm.c new file +4 -0
+    xc_interface *xch = ctx->xch;
+    struct rec_hvm_params_entry *entries;
+    struct rec_hvm_params hdr = {
+        .count = 0,

tools/libxc/saverestore/save_x86_hvm.c line 81

Quoted 10 lines
tools/libxc/saverestore/save_x86_hvm.c new file +10 -0
+    };
+    struct record rec = {
+        .type   = REC_TYPE_hvm_params,
+        .length = sizeof(hdr),
+        .data   = &hdr,
+    };
+    unsigned int i;
+    int rc;
+
+    entries = malloc(num_params * sizeof(*entries));

It's small enough that I think you could get away with

	struct rec_hvm_params_entry entries[num_params];

tools/libxc/saverestore/save_x86_hvm.c line 84

Quoted 3 lines
tools/libxc/saverestore/save_x86_hvm.c new file +3 -0
+    if ( !entries )
+    {
+        PERROR("HVM params record");

"Allocating..." (or goes away)

tools/libxc/saverestore/save_x86_hvm.c line 195

Quoted 2 lines
tools/libxc/saverestore/save_x86_hvm.c new file +2 -0
+    /* Write an END record */
+    rc = write_record(ctx, &end);

Didn't I see a helper for this earlier on? Could it not be in common code in any case?

Ian.

#33 lore raw

Re: [PATCH v4 8/9] tools/libxc: x86 hvm save implementation

Quoted 34 lines · Ian Campbell
Quoted 7 lines · Andrew Cooper
tools/libxc/saverestore/save_x86_hvm.c new file +7 -0
+static int write_hvm_params(struct context *ctx)
+{
+    static const unsigned int params[] = {
+        HVM_PARAM_STORE_PFN,
+        HVM_PARAM_IOREQ_PFN,
+        HVM_PARAM_BUFIOREQ_PFN,
+        HVM_PARAM_PAGING_RING_PFN,

tools/libxc/saverestore/save_x86_hvm.c line 67

Quoted 10 lines
tools/libxc/saverestore/save_x86_hvm.c new file +10 -0
+        HVM_PARAM_ACCESS_RING_PFN,
+        HVM_PARAM_SHARING_RING_PFN,
+        HVM_PARAM_VM86_TSS,
+        HVM_PARAM_CONSOLE_PFN,
+        HVM_PARAM_ACPI_IOPORTS_LOCATION,
+        HVM_PARAM_VIRIDIAN,
+        HVM_PARAM_IDENT_PT,
+        HVM_PARAM_PAE_ENABLED,
+    };
+    static const unsigned int num_params = ARRAY_SIZE(params);

Blank line between statics and regular please.

Quoted 4 lines
tools/libxc/saverestore/save_x86_hvm.c new file +4 -0
+    xc_interface *xch = ctx->xch;
+    struct rec_hvm_params_entry *entries;
+    struct rec_hvm_params hdr = {
+        .count = 0,

tools/libxc/saverestore/save_x86_hvm.c line 81

Quoted 10 lines
tools/libxc/saverestore/save_x86_hvm.c new file +10 -0
+    };
+    struct record rec = {
+        .type   = REC_TYPE_hvm_params,
+        .length = sizeof(hdr),
+        .data   = &hdr,
+    };
+    unsigned int i;
+    int rc;
+
+    entries = malloc(num_params * sizeof(*entries));
Quoted 2 lines

It's small enough that I think you could get away with

	struct rec_hvm_params_entry entries[num_params];

I would agree. (Not my code originally). I shall fix this up without malloc.

Quoted 5 lines

tools/libxc/saverestore/save_x86_hvm.c line 84

Quoted 5 lines
tools/libxc/saverestore/save_x86_hvm.c new file +3 -0
+    if ( !entries )
+    {
+        PERROR("HVM params record");
Quoted 9 lines

"Allocating..." (or goes away)

tools/libxc/saverestore/save_x86_hvm.c line 195

Quoted 2 lines
tools/libxc/saverestore/save_x86_hvm.c new file +2 -0
+    /* Write an END record */
+    rc = write_record(ctx, &end);

Didn't I see a helper for this earlier on? Could it not be in common code in any case?

Ian.

Probably should be.

~Andrew

#34 lore raw

PATCH v4 9/9 tools/libxc: x86 hvm restore implementation

In reply to Andrew Cooper
---
 tools/libxc/saverestore/common.h          |    2 +
 tools/libxc/saverestore/restore.c         |    5 +-
 tools/libxc/saverestore/restore_x86_hvm.c |  289 +++++++++++++++++++++++++++++
 3 files changed, 294 insertions(+), 2 deletions(-)
 create mode 100644 tools/libxc/saverestore/restore_x86_hvm.c
tools/libxc/saverestore/common.h +2 -0
index e26b879..980ed55 100644
@@ -131,6 +131,8 @@ int save_x86_pv(struct context *ctx);
 int save_x86_hvm(struct context *ctx);
 /* Restores an x86 PV domain. */
 int restore_x86_pv(struct context *ctx);
+/* Restores an x86 HVM domain. */
+int restore_x86_hvm(struct context *ctx);
 
 /*
  * Write the image and domain headers to the stream.
tools/libxc/saverestore/restore.c +3 -2
index 56eeb83..6feeb6f 100644
@@ -99,8 +99,9 @@ int xc_domain_restore2(xc_interface *xch, int io_fd, uint32_t dom,
 
     if ( ctx.dominfo.hvm )
     {
-        ERROR("HVM Restore not supported yet");
-        return -1;
+        ctx.ops = save_restore_ops_x86_hvm;
+        if ( restore_x86_hvm(&ctx) )
+            return -1;
     }
     else
     {
tools/libxc/saverestore/restore_x86_hvm.c new file +289 -0

289 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#36 lore raw
In reply to Andrew Cooper
Quoted 7 lines · Andrew Cooper

Hello,

Presented here for review is v4 of the Migration Stream v2 work. David, Frediano and myself have worked together on the implementation, and we have now managed to get PV and HVM migration working using the new format (even when transparently inserted underneath Xapi).

Quoted 5 lines

This series is based on staging, as 9f2f1298d0211962 is a prerequisite hypercall for a bugfix.

Draft F of the design document is available from
xenbits.xen.org/people/andrewcoop/domain-save-format-F.pdf

pg 8 says:

body Record body of length body_length octects.

It is a bit hard to parse. Perhaps:
Data stream (body) of size defined in 'body_length' in multiples of octects.
?

pg. 11
Mentions 'XEN_DOMCTL_PFINFO_*' type. Should you at least point the reader to where those are defined in the code? Or in the spec? [edit: next page has it. You could mention that (following page defines them)

'page_data: "page_size octets of uncompressed page contents for each page set as present in the pfn array." I think you might want to
replace: "..for each page set" as "corresponding to each element in the pfn array."

Also oddly enough the picture shows 'page_data[N-1]' but for the pfn array it has 'pfn[C-1]'. I think you want s/N/C. [edit: And the next page explains why N != C].

Perhaps for the 'page_data' definition you should mention that N != C as some of the pfn array entries skip the page_data array of data?

pg.17
xfeature_mask: The xfeature_mask rom the hypercall body.

I would replace body with structure?

pg.19
TSC_MODE_* constant. You should probably list them. Or at least say what they are.

Incarnation? Wiki says it means "embodied in flesh" Umm.

pg. 20

blob? How about payload? Data stream?

pg. 21

Would it make sense to point to the list of them at least?
Say: "Can be found in XYZ file."

pg. 22

It says temporary - so... should it be ripped out of the toolstack?

pg. 23

An x86 PV guest image will have in this order: -> An x86 PV guest image will have this order of records:

pg. 23
x86 HVM guest:
You should probably say "An x86 HVM guest will have.."

but more interesingly - there is an "DEVICE_MODEL_STATE" which is not defined in the spec?

Quoted 39 lines

The series is available from the branch 'saverestore2-v4' of
git://xenbits.xen.org/people/andrewcoop/xen.git
http://xenbits.xen.org/git-http/people/andrewcoop/xen.git

The code has been a clean rewrite, using the v1 code as a reference but avoiding obsolete areas (e.g. how to modify the pagetables of a 32 non-pae guest on 32bit pae Xen).

There are certainly some areas still in need improvement. There are plans to combine the save logic for PV and HVM guests by adding a few more hooks to save_restore_ops. While reviewing the series for posting, I have noticed some accidental duplication from attempting to merge PV and HVM together (ctx->save.p2m_size and ctx->x86_pv.max_pfn) which can be consolidated.

Moving forward will be work to do with converting a legacy image to a v2 image.

Questions/issues needing discussing are:

1) What level should the conversion happen. The v2 format is capable of detecting a legacy stream, but libxc is arguably too low level for the conversion decision to happen.

2) What to do about the layer violations which is the toolstack record and device model record. Libxl for HVM guests is the only known user of the 'toolstack' chunk, which contains some xenstore key/value pairs. This data should not be a blob in the middle of the libxc layer, and should be promoted to a first class member of a libxl migration stream.

Anyway - the code is presented here for initial comment/query/critism.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#37 lore raw
Quoted 13 lines · Konrad Rzeszutek Wilk
Quoted 7 lines · Andrew Cooper

Hello,

Presented here for review is v4 of the Migration Stream v2 work. David, Frediano and myself have worked together on the implementation, and we have now managed to get PV and HVM migration working using the new format (even when transparently inserted underneath Xapi).

Quoted 5 lines

This series is based on staging, as 9f2f1298d0211962 is a prerequisite hypercall for a bugfix.

Draft F of the design document is available from
xenbits.xen.org/people/andrewcoop/domain-save-format-F.pdf

Quoted 8 lines

pg 8 says:

body Record body of length body_length octects.

It is a bit hard to parse. Perhaps:
Data stream (body) of size defined in 'body_length' in multiples of octects.
?

Hmm - that is not very well written. I shall clarify it somewhat, although the length part is clear from the 'body_length' description.

Quoted 6 lines

pg. 11
Mentions 'XEN_DOMCTL_PFINFO_*' type. Should you at least point the reader to where those are defined in the code? Or in the spec? [edit: next page has it. You could mention that (following page defines them)

This is because of the way in which markdown split the page which is hard to affect. I shall clarify the first reference to mention public/domctl.h

Quoted 8 lines

'page_data: "page_size octets of uncompressed page contents for each page set as present in the pfn array." I think you might want to
replace: "..for each page set" as "corresponding to each element in the pfn array."

Quoted 7 lines

Also oddly enough the picture shows 'page_data[N-1]' but for the pfn array it has 'pfn[C-1]'. I think you want s/N/C. [edit: And the next page explains why N != C].

Perhaps for the 'page_data' definition you should mention that N != C as some of the pfn array entries skip the page_data array of data?

I will clarify that N is strictly <= C and can validly be 0.

Quoted 6 lines

pg.17
xfeature_mask: The xfeature_mask rom the hypercall body.

I would replace body with structure?

This is rather more difficult.

As it turns out, xfeature_mask is not needed for migration.

It is the entire set of available xsave features on the saving cpu, which is not relevant for the receiving cpu to validate against. The first and second uint64_t's of the vcpu_ctx are the xcr0 and xcr0_accum which are the two masks relevant to validating the feature set and size of context against the incoming data.

At the moment, the sole use for xfeature_mask is for the receiving cpu to validate xcr0_accum against, but only after xcr0_accum has already been validated, which makes the xfeature_mask check strictly redundant.

I have half a mind to submit a patch making Xen completely ignore it on the "set" side, at which point it can be dropped from the migration stream. It does however have a use in "get" side for libxc deciding how to set up the domain cpuid mask.

Quoted 7 lines

pg.19
TSC_MODE_* constant. You should probably list them. Or at least say what they are.

Incarnation? Wiki says it means "embodied in flesh" Umm.

As a word, that is a valid meaning.

As for what it is doing in the code, I have no clue. It is something to do with how many times the toolstack has issued a set_tsc_info() hypercall, but why Xen cant count this itself is beyond me, as is why this even matters in the first place.

Quoted 5 lines

pg. 20

blob? How about payload? Data stream?

The public headers refer to it as a blob, and blob as in "binary blob" is a fairly common term.

Quoted 6 lines

pg. 21

Would it make sense to point to the list of them at least?
Say: "Can be found in XYZ file."

In my copious free time, I hope to remove some of them. A large number of them can be more appropriately done elsehow, such as domain creation flags.

Quoted 4 lines

pg. 22

It says temporary - so... should it be ripped out of the toolstack?

Absolutely - it is gross layer violation.

As far as I am aware, the only time this record is actually used is for libxl sending xenstore key/value information. As xl/libxl already has its own head & tail for the libxc migration stream, it can put xenstore keys somewhere else.

It is currently included so "pseudo-v2" can transparently replace xc_domain_save/restore() during development without also making invasive changes to the users.

Quoted 6 lines

pg. 23

An x86 PV guest image will have in this order: -> An x86 PV guest image will have this order of records:

Ok

Quoted 9 lines

pg. 23
x86 HVM guest:
You should probably say "An x86 HVM guest will have.."

but more interesingly - there is an "DEVICE_MODEL_STATE" which is not defined in the spec?

Another layer violation, although this slipup comes from my first attempt to thing what HVM might need, and David's subsequent edit which moved it back to reflecting the working code.

The device model state is also something which needs fixing. Currently on the sending side, libxl is responsible for writing it into the stream, but on the receiving side, libxc is responsible for writing it to a magic file in /var/run/xend/...

~Andrew

#38 lore raw
In reply to Andrew Cooper
Quoted 5 lines · Andrew Cooper

There are certainly some areas still in need improvement. There are plans to combine the save logic for PV and HVM guests by adding a few more hooks to save_restore_ops. While reviewing the series for posting, I have noticed some accidental duplication from attempting to merge PV and HVM together (ctx->save.p2m_size and ctx->x86_pv.max_pfn) which can be consolidated.

Overall it looks good, well done to all those involved.

A few common issues I spotted which I didn't repeat every on every instance:
* Magic numbers (some with prexisting suitable defines) should msotly be #defined, or
* Chaining of unrelated things into if/else/else (especially where mixing input validation and actual functionality as I think I saw at least once)
* { for a struct initialiser on the same line as the variable.
* Apostrophes in comments are so last year

I think that was it (but if you notice me commenting on something repeatedly but not consistently there's a good chance I meant to remember it for this list).

Quoted 8 lines

Moving forward will be work to do with converting a legacy image to a v2 image.

Questions/issues needing discussing are:

1) What level should the conversion happen. The v2 format is capable of detecting a legacy stream, but libxc is arguably too low level for the conversion decision to happen.

The detection necessarily has to read the first N bytes, doesn't it? Which pretty much means the decision has to be made in libxc I think?

Quoted 5 lines

2) What to do about the layer violations which is the toolstack record and device model record. Libxl for HVM guests is the only known user of the 'toolstack' chunk, which contains some xenstore key/value pairs. This data should not be a blob in the middle of the libxc layer, and should be promoted to a first class member of a libxl migration stream.

Stefano added this, I can't remember what it is or why it ended up in libxc, it could well just be "libxc_domain_save is unhackable", or maybe there was a reason.

Ian.