qemu/include/migration/vmstate.h
<<
>>
Prefs
   1/*
   2 * QEMU migration/snapshot declarations
   3 *
   4 * Copyright (c) 2009-2011 Red Hat, Inc.
   5 *
   6 * Original author: Juan Quintela <quintela@redhat.com>
   7 *
   8 * Permission is hereby granted, free of charge, to any person obtaining a copy
   9 * of this software and associated documentation files (the "Software"), to deal
  10 * in the Software without restriction, including without limitation the rights
  11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12 * copies of the Software, and to permit persons to whom the Software is
  13 * furnished to do so, subject to the following conditions:
  14 *
  15 * The above copyright notice and this permission notice shall be included in
  16 * all copies or substantial portions of the Software.
  17 *
  18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24 * THE SOFTWARE.
  25 */
  26
  27#ifndef QEMU_VMSTATE_H
  28#define QEMU_VMSTATE_H
  29
  30typedef struct VMStateInfo VMStateInfo;
  31typedef struct VMStateDescription VMStateDescription;
  32typedef struct VMStateField VMStateField;
  33
  34/* VMStateInfo allows customized migration of objects that don't fit in
  35 * any category in VMStateFlags. Additional information is always passed
  36 * into get and put in terms of field and vmdesc parameters. However
  37 * these two parameters should only be used in cases when customized
  38 * handling is needed, such as QTAILQ. For primitive data types such as
  39 * integer, field and vmdesc parameters should be ignored inside get/put.
  40 */
  41struct VMStateInfo {
  42    const char *name;
  43    int (*get)(QEMUFile *f, void *pv, size_t size, const VMStateField *field);
  44    int (*put)(QEMUFile *f, void *pv, size_t size, const VMStateField *field,
  45               QJSON *vmdesc);
  46};
  47
  48enum VMStateFlags {
  49    /* Ignored */
  50    VMS_SINGLE           = 0x001,
  51
  52    /* The struct member at opaque + VMStateField.offset is a pointer
  53     * to the actual field (e.g. struct a { uint8_t *b;
  54     * }). Dereference the pointer before using it as basis for
  55     * further pointer arithmetic (see e.g. VMS_ARRAY). Does not
  56     * affect the meaning of VMStateField.num_offset or
  57     * VMStateField.size_offset; see VMS_VARRAY* and VMS_VBUFFER for
  58     * those. */
  59    VMS_POINTER          = 0x002,
  60
  61    /* The field is an array of fixed size. VMStateField.num contains
  62     * the number of entries in the array. The size of each entry is
  63     * given by VMStateField.size and / or opaque +
  64     * VMStateField.size_offset; see VMS_VBUFFER and
  65     * VMS_MULTIPLY. Each array entry will be processed individually
  66     * (VMStateField.info.get()/put() if VMS_STRUCT is not set,
  67     * recursion into VMStateField.vmsd if VMS_STRUCT is set). May not
  68     * be combined with VMS_VARRAY*. */
  69    VMS_ARRAY            = 0x004,
  70
  71    /* The field is itself a struct, containing one or more
  72     * fields. Recurse into VMStateField.vmsd. Most useful in
  73     * combination with VMS_ARRAY / VMS_VARRAY*, recursing into each
  74     * array entry. */
  75    VMS_STRUCT           = 0x008,
  76
  77    /* The field is an array of variable size. The int32_t at opaque +
  78     * VMStateField.num_offset contains the number of entries in the
  79     * array. See the VMS_ARRAY description regarding array handling
  80     * in general. May not be combined with VMS_ARRAY or any other
  81     * VMS_VARRAY*. */
  82    VMS_VARRAY_INT32     = 0x010,
  83
  84    /* Ignored */
  85    VMS_BUFFER           = 0x020,
  86
  87    /* The field is a (fixed-size or variable-size) array of pointers
  88     * (e.g. struct a { uint8_t *b[]; }). Dereference each array entry
  89     * before using it. Note: Does not imply any one of VMS_ARRAY /
  90     * VMS_VARRAY*; these need to be set explicitly. */
  91    VMS_ARRAY_OF_POINTER = 0x040,
  92
  93    /* The field is an array of variable size. The uint16_t at opaque
  94     * + VMStateField.num_offset (subject to VMS_MULTIPLY_ELEMENTS)
  95     * contains the number of entries in the array. See the VMS_ARRAY
  96     * description regarding array handling in general. May not be
  97     * combined with VMS_ARRAY or any other VMS_VARRAY*. */
  98    VMS_VARRAY_UINT16    = 0x080,
  99
 100    /* The size of the individual entries (a single array entry if
 101     * VMS_ARRAY or any of VMS_VARRAY* are set, or the field itself if
 102     * neither is set) is variable (i.e. not known at compile-time),
 103     * but the same for all entries. Use the int32_t at opaque +
 104     * VMStateField.size_offset (subject to VMS_MULTIPLY) to determine
 105     * the size of each (and every) entry. */
 106    VMS_VBUFFER          = 0x100,
 107
 108    /* Multiply the entry size given by the int32_t at opaque +
 109     * VMStateField.size_offset (see VMS_VBUFFER description) with
 110     * VMStateField.size to determine the number of bytes to be
 111     * allocated. Only valid in combination with VMS_VBUFFER. */
 112    VMS_MULTIPLY         = 0x200,
 113
 114    /* The field is an array of variable size. The uint8_t at opaque +
 115     * VMStateField.num_offset (subject to VMS_MULTIPLY_ELEMENTS)
 116     * contains the number of entries in the array. See the VMS_ARRAY
 117     * description regarding array handling in general. May not be
 118     * combined with VMS_ARRAY or any other VMS_VARRAY*. */
 119    VMS_VARRAY_UINT8     = 0x400,
 120
 121    /* The field is an array of variable size. The uint32_t at opaque
 122     * + VMStateField.num_offset (subject to VMS_MULTIPLY_ELEMENTS)
 123     * contains the number of entries in the array. See the VMS_ARRAY
 124     * description regarding array handling in general. May not be
 125     * combined with VMS_ARRAY or any other VMS_VARRAY*. */
 126    VMS_VARRAY_UINT32    = 0x800,
 127
 128    /* Fail loading the serialised VM state if this field is missing
 129     * from the input. */
 130    VMS_MUST_EXIST       = 0x1000,
 131
 132    /* When loading serialised VM state, allocate memory for the
 133     * (entire) field. Only valid in combination with
 134     * VMS_POINTER. Note: Not all combinations with other flags are
 135     * currently supported, e.g. VMS_ALLOC|VMS_ARRAY_OF_POINTER won't
 136     * cause the individual entries to be allocated. */
 137    VMS_ALLOC            = 0x2000,
 138
 139    /* Multiply the number of entries given by the integer at opaque +
 140     * VMStateField.num_offset (see VMS_VARRAY*) with VMStateField.num
 141     * to determine the number of entries in the array. Only valid in
 142     * combination with one of VMS_VARRAY*. */
 143    VMS_MULTIPLY_ELEMENTS = 0x4000,
 144
 145    /* A structure field that is like VMS_STRUCT, but uses
 146     * VMStateField.struct_version_id to tell which version of the
 147     * structure we are referencing to use. */
 148    VMS_VSTRUCT           = 0x8000,
 149};
 150
 151typedef enum {
 152    MIG_PRI_DEFAULT = 0,
 153    MIG_PRI_IOMMU,              /* Must happen before PCI devices */
 154    MIG_PRI_PCI_BUS,            /* Must happen before IOMMU */
 155    MIG_PRI_GICV3_ITS,          /* Must happen before PCI devices */
 156    MIG_PRI_GICV3,              /* Must happen before the ITS */
 157    MIG_PRI_MAX,
 158} MigrationPriority;
 159
 160struct VMStateField {
 161    const char *name;
 162    const char *err_hint;
 163    size_t offset;
 164    size_t size;
 165    size_t start;
 166    int num;
 167    size_t num_offset;
 168    size_t size_offset;
 169    const VMStateInfo *info;
 170    enum VMStateFlags flags;
 171    const VMStateDescription *vmsd;
 172    int version_id;
 173    int struct_version_id;
 174    bool (*field_exists)(void *opaque, int version_id);
 175};
 176
 177struct VMStateDescription {
 178    const char *name;
 179    int unmigratable;
 180    int version_id;
 181    int minimum_version_id;
 182    int minimum_version_id_old;
 183    MigrationPriority priority;
 184    LoadStateHandler *load_state_old;
 185    int (*pre_load)(void *opaque);
 186    int (*post_load)(void *opaque, int version_id);
 187    int (*pre_save)(void *opaque);
 188    int (*post_save)(void *opaque);
 189    bool (*needed)(void *opaque);
 190    const VMStateField *fields;
 191    const VMStateDescription **subsections;
 192};
 193
 194extern const VMStateDescription vmstate_dummy;
 195
 196extern const VMStateInfo vmstate_info_bool;
 197
 198extern const VMStateInfo vmstate_info_int8;
 199extern const VMStateInfo vmstate_info_int16;
 200extern const VMStateInfo vmstate_info_int32;
 201extern const VMStateInfo vmstate_info_int64;
 202
 203extern const VMStateInfo vmstate_info_uint8_equal;
 204extern const VMStateInfo vmstate_info_uint16_equal;
 205extern const VMStateInfo vmstate_info_int32_equal;
 206extern const VMStateInfo vmstate_info_uint32_equal;
 207extern const VMStateInfo vmstate_info_uint64_equal;
 208extern const VMStateInfo vmstate_info_int32_le;
 209
 210extern const VMStateInfo vmstate_info_uint8;
 211extern const VMStateInfo vmstate_info_uint16;
 212extern const VMStateInfo vmstate_info_uint32;
 213extern const VMStateInfo vmstate_info_uint64;
 214
 215/** Put this in the stream when migrating a null pointer.*/
 216#define VMS_NULLPTR_MARKER (0x30U) /* '0' */
 217extern const VMStateInfo vmstate_info_nullptr;
 218
 219extern const VMStateInfo vmstate_info_float64;
 220extern const VMStateInfo vmstate_info_cpudouble;
 221
 222extern const VMStateInfo vmstate_info_timer;
 223extern const VMStateInfo vmstate_info_buffer;
 224extern const VMStateInfo vmstate_info_unused_buffer;
 225extern const VMStateInfo vmstate_info_tmp;
 226extern const VMStateInfo vmstate_info_bitmap;
 227extern const VMStateInfo vmstate_info_qtailq;
 228
 229#define type_check_2darray(t1,t2,n,m) ((t1(*)[n][m])0 - (t2*)0)
 230#define type_check_array(t1,t2,n) ((t1(*)[n])0 - (t2*)0)
 231#define type_check_pointer(t1,t2) ((t1**)0 - (t2*)0)
 232
 233#define vmstate_offset_value(_state, _field, _type)                  \
 234    (offsetof(_state, _field) +                                      \
 235     type_check(_type, typeof_field(_state, _field)))
 236
 237#define vmstate_offset_pointer(_state, _field, _type)                \
 238    (offsetof(_state, _field) +                                      \
 239     type_check_pointer(_type, typeof_field(_state, _field)))
 240
 241#define vmstate_offset_array(_state, _field, _type, _num)            \
 242    (offsetof(_state, _field) +                                      \
 243     type_check_array(_type, typeof_field(_state, _field), _num))
 244
 245#define vmstate_offset_2darray(_state, _field, _type, _n1, _n2)      \
 246    (offsetof(_state, _field) +                                      \
 247     type_check_2darray(_type, typeof_field(_state, _field), _n1, _n2))
 248
 249#define vmstate_offset_sub_array(_state, _field, _type, _start)      \
 250    vmstate_offset_value(_state, _field[_start], _type)
 251
 252#define vmstate_offset_buffer(_state, _field)                        \
 253    vmstate_offset_array(_state, _field, uint8_t,                    \
 254                         sizeof(typeof_field(_state, _field)))
 255
 256/* In the macros below, if there is a _version, that means the macro's
 257 * field will be processed only if the version being received is >=
 258 * the _version specified.  In general, if you add a new field, you
 259 * would increment the structure's version and put that version
 260 * number into the new field so it would only be processed with the
 261 * new version.
 262 *
 263 * In particular, for VMSTATE_STRUCT() and friends the _version does
 264 * *NOT* pick the version of the sub-structure.  It works just as
 265 * specified above.  The version of the top-level structure received
 266 * is passed down to all sub-structures.  This means that the
 267 * sub-structures must have version that are compatible with all the
 268 * structures that use them.
 269 *
 270 * If you want to specify the version of the sub-structure, use
 271 * VMSTATE_VSTRUCT(), which allows the specific sub-structure version
 272 * to be directly specified.
 273 */
 274
 275#define VMSTATE_SINGLE_TEST(_field, _state, _test, _version, _info, _type) { \
 276    .name         = (stringify(_field)),                             \
 277    .version_id   = (_version),                                      \
 278    .field_exists = (_test),                                         \
 279    .size         = sizeof(_type),                                   \
 280    .info         = &(_info),                                        \
 281    .flags        = VMS_SINGLE,                                      \
 282    .offset       = vmstate_offset_value(_state, _field, _type),     \
 283}
 284
 285#define VMSTATE_SINGLE_FULL(_field, _state, _test, _version, _info,  \
 286                            _type, _err_hint) {                      \
 287    .name         = (stringify(_field)),                             \
 288    .err_hint     = (_err_hint),                                     \
 289    .version_id   = (_version),                                      \
 290    .field_exists = (_test),                                         \
 291    .size         = sizeof(_type),                                   \
 292    .info         = &(_info),                                        \
 293    .flags        = VMS_SINGLE,                                      \
 294    .offset       = vmstate_offset_value(_state, _field, _type),     \
 295}
 296
 297/* Validate state using a boolean predicate. */
 298#define VMSTATE_VALIDATE(_name, _test) { \
 299    .name         = (_name),                                         \
 300    .field_exists = (_test),                                         \
 301    .flags        = VMS_ARRAY | VMS_MUST_EXIST,                      \
 302    .num          = 0, /* 0 elements: no data, only run _test */     \
 303}
 304
 305#define VMSTATE_POINTER(_field, _state, _version, _info, _type) {    \
 306    .name       = (stringify(_field)),                               \
 307    .version_id = (_version),                                        \
 308    .info       = &(_info),                                          \
 309    .size       = sizeof(_type),                                     \
 310    .flags      = VMS_SINGLE|VMS_POINTER,                            \
 311    .offset     = vmstate_offset_value(_state, _field, _type),       \
 312}
 313
 314#define VMSTATE_POINTER_TEST(_field, _state, _test, _info, _type) {  \
 315    .name       = (stringify(_field)),                               \
 316    .info       = &(_info),                                          \
 317    .field_exists = (_test),                                         \
 318    .size       = sizeof(_type),                                     \
 319    .flags      = VMS_SINGLE|VMS_POINTER,                            \
 320    .offset     = vmstate_offset_value(_state, _field, _type),       \
 321}
 322
 323#define VMSTATE_ARRAY(_field, _state, _num, _version, _info, _type) {\
 324    .name       = (stringify(_field)),                               \
 325    .version_id = (_version),                                        \
 326    .num        = (_num),                                            \
 327    .info       = &(_info),                                          \
 328    .size       = sizeof(_type),                                     \
 329    .flags      = VMS_ARRAY,                                         \
 330    .offset     = vmstate_offset_array(_state, _field, _type, _num), \
 331}
 332
 333#define VMSTATE_2DARRAY(_field, _state, _n1, _n2, _version, _info, _type) { \
 334    .name       = (stringify(_field)),                                      \
 335    .version_id = (_version),                                               \
 336    .num        = (_n1) * (_n2),                                            \
 337    .info       = &(_info),                                                 \
 338    .size       = sizeof(_type),                                            \
 339    .flags      = VMS_ARRAY,                                                \
 340    .offset     = vmstate_offset_2darray(_state, _field, _type, _n1, _n2),  \
 341}
 342
 343#define VMSTATE_VARRAY_MULTIPLY(_field, _state, _field_num, _multiply, _info, _type) { \
 344    .name       = (stringify(_field)),                               \
 345    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
 346    .num        = (_multiply),                                       \
 347    .info       = &(_info),                                          \
 348    .size       = sizeof(_type),                                     \
 349    .flags      = VMS_VARRAY_UINT32|VMS_MULTIPLY_ELEMENTS,           \
 350    .offset     = offsetof(_state, _field),                          \
 351}
 352
 353#define VMSTATE_ARRAY_TEST(_field, _state, _num, _test, _info, _type) {\
 354    .name         = (stringify(_field)),                              \
 355    .field_exists = (_test),                                          \
 356    .num          = (_num),                                           \
 357    .info         = &(_info),                                         \
 358    .size         = sizeof(_type),                                    \
 359    .flags        = VMS_ARRAY,                                        \
 360    .offset       = vmstate_offset_array(_state, _field, _type, _num),\
 361}
 362
 363#define VMSTATE_SUB_ARRAY(_field, _state, _start, _num, _version, _info, _type) { \
 364    .name       = (stringify(_field)),                               \
 365    .version_id = (_version),                                        \
 366    .num        = (_num),                                            \
 367    .info       = &(_info),                                          \
 368    .size       = sizeof(_type),                                     \
 369    .flags      = VMS_ARRAY,                                         \
 370    .offset     = vmstate_offset_sub_array(_state, _field, _type, _start), \
 371}
 372
 373#define VMSTATE_ARRAY_INT32_UNSAFE(_field, _state, _field_num, _info, _type) {\
 374    .name       = (stringify(_field)),                               \
 375    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
 376    .info       = &(_info),                                          \
 377    .size       = sizeof(_type),                                     \
 378    .flags      = VMS_VARRAY_INT32,                                  \
 379    .offset     = offsetof(_state, _field),                          \
 380}
 381
 382#define VMSTATE_VARRAY_INT32(_field, _state, _field_num, _version, _info, _type) {\
 383    .name       = (stringify(_field)),                               \
 384    .version_id = (_version),                                        \
 385    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
 386    .info       = &(_info),                                          \
 387    .size       = sizeof(_type),                                     \
 388    .flags      = VMS_VARRAY_INT32|VMS_POINTER,                      \
 389    .offset     = vmstate_offset_pointer(_state, _field, _type),     \
 390}
 391
 392#define VMSTATE_VARRAY_UINT32(_field, _state, _field_num, _version, _info, _type) {\
 393    .name       = (stringify(_field)),                               \
 394    .version_id = (_version),                                        \
 395    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
 396    .info       = &(_info),                                          \
 397    .size       = sizeof(_type),                                     \
 398    .flags      = VMS_VARRAY_UINT32|VMS_POINTER,                     \
 399    .offset     = vmstate_offset_pointer(_state, _field, _type),     \
 400}
 401
 402#define VMSTATE_VARRAY_UINT32_ALLOC(_field, _state, _field_num, _version, _info, _type) {\
 403    .name       = (stringify(_field)),                               \
 404    .version_id = (_version),                                        \
 405    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
 406    .info       = &(_info),                                          \
 407    .size       = sizeof(_type),                                     \
 408    .flags      = VMS_VARRAY_UINT32|VMS_POINTER|VMS_ALLOC,           \
 409    .offset     = vmstate_offset_pointer(_state, _field, _type),     \
 410}
 411
 412#define VMSTATE_VARRAY_UINT16_UNSAFE(_field, _state, _field_num, _version, _info, _type) {\
 413    .name       = (stringify(_field)),                               \
 414    .version_id = (_version),                                        \
 415    .num_offset = vmstate_offset_value(_state, _field_num, uint16_t),\
 416    .info       = &(_info),                                          \
 417    .size       = sizeof(_type),                                     \
 418    .flags      = VMS_VARRAY_UINT16,                                 \
 419    .offset     = offsetof(_state, _field),                          \
 420}
 421
 422#define VMSTATE_VSTRUCT_TEST(_field, _state, _test, _version, _vmsd, _type, _struct_version) { \
 423    .name         = (stringify(_field)),                             \
 424    .version_id   = (_version),                                      \
 425    .struct_version_id = (_struct_version),                          \
 426    .field_exists = (_test),                                         \
 427    .vmsd         = &(_vmsd),                                        \
 428    .size         = sizeof(_type),                                   \
 429    .flags        = VMS_VSTRUCT,                                     \
 430    .offset       = vmstate_offset_value(_state, _field, _type),     \
 431}
 432
 433#define VMSTATE_STRUCT_TEST(_field, _state, _test, _version, _vmsd, _type) { \
 434    .name         = (stringify(_field)),                             \
 435    .version_id   = (_version),                                      \
 436    .field_exists = (_test),                                         \
 437    .vmsd         = &(_vmsd),                                        \
 438    .size         = sizeof(_type),                                   \
 439    .flags        = VMS_STRUCT,                                      \
 440    .offset       = vmstate_offset_value(_state, _field, _type),     \
 441}
 442
 443#define VMSTATE_STRUCT_POINTER_V(_field, _state, _version, _vmsd, _type) { \
 444    .name         = (stringify(_field)),                             \
 445    .version_id   = (_version),                                        \
 446    .vmsd         = &(_vmsd),                                        \
 447    .size         = sizeof(_type *),                                 \
 448    .flags        = VMS_STRUCT|VMS_POINTER,                          \
 449    .offset       = vmstate_offset_pointer(_state, _field, _type),   \
 450}
 451
 452#define VMSTATE_STRUCT_POINTER_TEST_V(_field, _state, _test, _version, _vmsd, _type) { \
 453    .name         = (stringify(_field)),                             \
 454    .version_id   = (_version),                                        \
 455    .field_exists = (_test),                                         \
 456    .vmsd         = &(_vmsd),                                        \
 457    .size         = sizeof(_type *),                                 \
 458    .flags        = VMS_STRUCT|VMS_POINTER,                          \
 459    .offset       = vmstate_offset_pointer(_state, _field, _type),   \
 460}
 461
 462#define VMSTATE_ARRAY_OF_POINTER(_field, _state, _num, _version, _info, _type) {\
 463    .name       = (stringify(_field)),                               \
 464    .version_id = (_version),                                        \
 465    .num        = (_num),                                            \
 466    .info       = &(_info),                                          \
 467    .size       = sizeof(_type),                                     \
 468    .flags      = VMS_ARRAY|VMS_ARRAY_OF_POINTER,                    \
 469    .offset     = vmstate_offset_array(_state, _field, _type, _num), \
 470}
 471
 472#define VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(_f, _s, _n, _v, _vmsd, _type) { \
 473    .name       = (stringify(_f)),                                   \
 474    .version_id = (_v),                                              \
 475    .num        = (_n),                                              \
 476    .vmsd       = &(_vmsd),                                          \
 477    .size       = sizeof(_type *),                                    \
 478    .flags      = VMS_ARRAY|VMS_STRUCT|VMS_ARRAY_OF_POINTER,         \
 479    .offset     = vmstate_offset_array(_s, _f, _type*, _n),          \
 480}
 481
 482#define VMSTATE_STRUCT_SUB_ARRAY(_field, _state, _start, _num, _version, _vmsd, _type) { \
 483    .name       = (stringify(_field)),                                     \
 484    .version_id = (_version),                                              \
 485    .num        = (_num),                                                  \
 486    .vmsd       = &(_vmsd),                                                \
 487    .size       = sizeof(_type),                                           \
 488    .flags      = VMS_STRUCT|VMS_ARRAY,                                    \
 489    .offset     = vmstate_offset_sub_array(_state, _field, _type, _start), \
 490}
 491
 492#define VMSTATE_STRUCT_ARRAY_TEST(_field, _state, _num, _test, _version, _vmsd, _type) { \
 493    .name         = (stringify(_field)),                             \
 494    .num          = (_num),                                          \
 495    .field_exists = (_test),                                         \
 496    .version_id   = (_version),                                      \
 497    .vmsd         = &(_vmsd),                                        \
 498    .size         = sizeof(_type),                                   \
 499    .flags        = VMS_STRUCT|VMS_ARRAY,                            \
 500    .offset       = vmstate_offset_array(_state, _field, _type, _num),\
 501}
 502
 503#define VMSTATE_STRUCT_2DARRAY_TEST(_field, _state, _n1, _n2, _test, \
 504                                    _version, _vmsd, _type) {        \
 505    .name         = (stringify(_field)),                             \
 506    .num          = (_n1) * (_n2),                                   \
 507    .field_exists = (_test),                                         \
 508    .version_id   = (_version),                                      \
 509    .vmsd         = &(_vmsd),                                        \
 510    .size         = sizeof(_type),                                   \
 511    .flags        = VMS_STRUCT | VMS_ARRAY,                          \
 512    .offset       = vmstate_offset_2darray(_state, _field, _type,    \
 513                                           _n1, _n2),                \
 514}
 515
 516#define VMSTATE_STRUCT_VARRAY_UINT8(_field, _state, _field_num, _version, _vmsd, _type) { \
 517    .name       = (stringify(_field)),                               \
 518    .num_offset = vmstate_offset_value(_state, _field_num, uint8_t), \
 519    .version_id = (_version),                                        \
 520    .vmsd       = &(_vmsd),                                          \
 521    .size       = sizeof(_type),                                     \
 522    .flags      = VMS_STRUCT|VMS_VARRAY_UINT8,                       \
 523    .offset     = offsetof(_state, _field),                          \
 524}
 525
 526/* a variable length array (i.e. _type *_field) but we know the
 527 * length
 528 */
 529#define VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(_field, _state, _num, _version, _vmsd, _type) { \
 530    .name       = (stringify(_field)),                               \
 531    .num          = (_num),                                          \
 532    .version_id = (_version),                                        \
 533    .vmsd       = &(_vmsd),                                          \
 534    .size       = sizeof(_type),                                     \
 535    .flags      = VMS_STRUCT|VMS_ARRAY|VMS_POINTER,                  \
 536    .offset     = offsetof(_state, _field),                          \
 537}
 538
 539#define VMSTATE_STRUCT_VARRAY_POINTER_INT32(_field, _state, _field_num, _vmsd, _type) { \
 540    .name       = (stringify(_field)),                               \
 541    .version_id = 0,                                                 \
 542    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
 543    .size       = sizeof(_type),                                     \
 544    .vmsd       = &(_vmsd),                                          \
 545    .flags      = VMS_POINTER | VMS_VARRAY_INT32 | VMS_STRUCT,       \
 546    .offset     = vmstate_offset_pointer(_state, _field, _type),     \
 547}
 548
 549#define VMSTATE_STRUCT_VARRAY_POINTER_UINT32(_field, _state, _field_num, _vmsd, _type) { \
 550    .name       = (stringify(_field)),                               \
 551    .version_id = 0,                                                 \
 552    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
 553    .size       = sizeof(_type),                                     \
 554    .vmsd       = &(_vmsd),                                          \
 555    .flags      = VMS_POINTER | VMS_VARRAY_INT32 | VMS_STRUCT,       \
 556    .offset     = vmstate_offset_pointer(_state, _field, _type),     \
 557}
 558
 559#define VMSTATE_STRUCT_VARRAY_POINTER_UINT16(_field, _state, _field_num, _vmsd, _type) { \
 560    .name       = (stringify(_field)),                               \
 561    .version_id = 0,                                                 \
 562    .num_offset = vmstate_offset_value(_state, _field_num, uint16_t),\
 563    .size       = sizeof(_type),                                     \
 564    .vmsd       = &(_vmsd),                                          \
 565    .flags      = VMS_POINTER | VMS_VARRAY_UINT16 | VMS_STRUCT,      \
 566    .offset     = vmstate_offset_pointer(_state, _field, _type),     \
 567}
 568
 569#define VMSTATE_STRUCT_VARRAY_INT32(_field, _state, _field_num, _version, _vmsd, _type) { \
 570    .name       = (stringify(_field)),                               \
 571    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
 572    .version_id = (_version),                                        \
 573    .vmsd       = &(_vmsd),                                          \
 574    .size       = sizeof(_type),                                     \
 575    .flags      = VMS_STRUCT|VMS_VARRAY_INT32,                       \
 576    .offset     = offsetof(_state, _field),                          \
 577}
 578
 579#define VMSTATE_STRUCT_VARRAY_UINT32(_field, _state, _field_num, _version, _vmsd, _type) { \
 580    .name       = (stringify(_field)),                               \
 581    .num_offset = vmstate_offset_value(_state, _field_num, uint32_t), \
 582    .version_id = (_version),                                        \
 583    .vmsd       = &(_vmsd),                                          \
 584    .size       = sizeof(_type),                                     \
 585    .flags      = VMS_STRUCT|VMS_VARRAY_UINT32,                      \
 586    .offset     = offsetof(_state, _field),                          \
 587}
 588
 589#define VMSTATE_STRUCT_VARRAY_ALLOC(_field, _state, _field_num, _version, _vmsd, _type) {\
 590    .name       = (stringify(_field)),                               \
 591    .version_id = (_version),                                        \
 592    .vmsd       = &(_vmsd),                                          \
 593    .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
 594    .size       = sizeof(_type),                                     \
 595    .flags      = VMS_STRUCT|VMS_VARRAY_INT32|VMS_ALLOC|VMS_POINTER, \
 596    .offset     = vmstate_offset_pointer(_state, _field, _type),     \
 597}
 598
 599#define VMSTATE_STATIC_BUFFER(_field, _state, _version, _test, _start, _size) { \
 600    .name         = (stringify(_field)),                             \
 601    .version_id   = (_version),                                      \
 602    .field_exists = (_test),                                         \
 603    .size         = (_size - _start),                                \
 604    .info         = &vmstate_info_buffer,                            \
 605    .flags        = VMS_BUFFER,                                      \
 606    .offset       = vmstate_offset_buffer(_state, _field) + _start,  \
 607}
 608
 609#define VMSTATE_VBUFFER_MULTIPLY(_field, _state, _version, _test,    \
 610                                 _field_size, _multiply) {           \
 611    .name         = (stringify(_field)),                             \
 612    .version_id   = (_version),                                      \
 613    .field_exists = (_test),                                         \
 614    .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
 615    .size         = (_multiply),                                      \
 616    .info         = &vmstate_info_buffer,                            \
 617    .flags        = VMS_VBUFFER|VMS_POINTER|VMS_MULTIPLY,            \
 618    .offset       = offsetof(_state, _field),                        \
 619}
 620
 621#define VMSTATE_VBUFFER(_field, _state, _version, _test, _field_size) { \
 622    .name         = (stringify(_field)),                             \
 623    .version_id   = (_version),                                      \
 624    .field_exists = (_test),                                         \
 625    .size_offset  = vmstate_offset_value(_state, _field_size, int32_t),\
 626    .info         = &vmstate_info_buffer,                            \
 627    .flags        = VMS_VBUFFER|VMS_POINTER,                         \
 628    .offset       = offsetof(_state, _field),                        \
 629}
 630
 631#define VMSTATE_VBUFFER_UINT32(_field, _state, _version, _test, _field_size) { \
 632    .name         = (stringify(_field)),                             \
 633    .version_id   = (_version),                                      \
 634    .field_exists = (_test),                                         \
 635    .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
 636    .info         = &vmstate_info_buffer,                            \
 637    .flags        = VMS_VBUFFER|VMS_POINTER,                         \
 638    .offset       = offsetof(_state, _field),                        \
 639}
 640
 641#define VMSTATE_VBUFFER_ALLOC_UINT32(_field, _state, _version,       \
 642                                     _test, _field_size) {           \
 643    .name         = (stringify(_field)),                             \
 644    .version_id   = (_version),                                      \
 645    .field_exists = (_test),                                         \
 646    .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
 647    .info         = &vmstate_info_buffer,                            \
 648    .flags        = VMS_VBUFFER|VMS_POINTER|VMS_ALLOC,               \
 649    .offset       = offsetof(_state, _field),                        \
 650}
 651
 652#define VMSTATE_BUFFER_UNSAFE_INFO_TEST(_field, _state, _test, _version, _info, _size) { \
 653    .name       = (stringify(_field)),                               \
 654    .version_id = (_version),                                        \
 655    .field_exists = (_test),                                         \
 656    .size       = (_size),                                           \
 657    .info       = &(_info),                                          \
 658    .flags      = VMS_BUFFER,                                        \
 659    .offset     = offsetof(_state, _field),                          \
 660}
 661
 662#define VMSTATE_BUFFER_POINTER_UNSAFE(_field, _state, _version, _size) { \
 663    .name       = (stringify(_field)),                               \
 664    .version_id = (_version),                                        \
 665    .size       = (_size),                                           \
 666    .info       = &vmstate_info_buffer,                              \
 667    .flags      = VMS_BUFFER|VMS_POINTER,                            \
 668    .offset     = offsetof(_state, _field),                          \
 669}
 670
 671/* Allocate a temporary of type 'tmp_type', set tmp->parent to _state
 672 * and execute the vmsd on the temporary.  Note that we're working with
 673 * the whole of _state here, not a field within it.
 674 * We compile time check that:
 675 *    That _tmp_type contains a 'parent' member that's a pointer to the
 676 *        '_state' type
 677 *    That the pointer is right at the start of _tmp_type.
 678 */
 679#define VMSTATE_WITH_TMP(_state, _tmp_type, _vmsd) {                 \
 680    .name         = "tmp",                                           \
 681    .size         = sizeof(_tmp_type) +                              \
 682                    QEMU_BUILD_BUG_ON_ZERO(offsetof(_tmp_type, parent) != 0) + \
 683                    type_check_pointer(_state,                       \
 684                        typeof_field(_tmp_type, parent)),            \
 685    .vmsd         = &(_vmsd),                                        \
 686    .info         = &vmstate_info_tmp,                               \
 687}
 688
 689#define VMSTATE_UNUSED_BUFFER(_test, _version, _size) {              \
 690    .name         = "unused",                                        \
 691    .field_exists = (_test),                                         \
 692    .version_id   = (_version),                                      \
 693    .size         = (_size),                                         \
 694    .info         = &vmstate_info_unused_buffer,                     \
 695    .flags        = VMS_BUFFER,                                      \
 696}
 697
 698/* Discard size * field_num bytes, where field_num is a uint32 member */
 699#define VMSTATE_UNUSED_VARRAY_UINT32(_state, _test, _version, _field_num, _size) {\
 700    .name         = "unused",                                        \
 701    .field_exists = (_test),                                         \
 702    .num_offset   = vmstate_offset_value(_state, _field_num, uint32_t),\
 703    .version_id   = (_version),                                      \
 704    .size         = (_size),                                         \
 705    .info         = &vmstate_info_unused_buffer,                     \
 706    .flags        = VMS_VARRAY_UINT32 | VMS_BUFFER,                  \
 707}
 708
 709/* _field_size should be a int32_t field in the _state struct giving the
 710 * size of the bitmap _field in bits.
 711 */
 712#define VMSTATE_BITMAP(_field, _state, _version, _field_size) {      \
 713    .name         = (stringify(_field)),                             \
 714    .version_id   = (_version),                                      \
 715    .size_offset  = vmstate_offset_value(_state, _field_size, int32_t),\
 716    .info         = &vmstate_info_bitmap,                            \
 717    .flags        = VMS_VBUFFER|VMS_POINTER,                         \
 718    .offset       = offsetof(_state, _field),                        \
 719}
 720
 721/* For migrating a QTAILQ.
 722 * Target QTAILQ needs be properly initialized.
 723 * _type: type of QTAILQ element
 724 * _next: name of QTAILQ entry field in QTAILQ element
 725 * _vmsd: VMSD for QTAILQ element
 726 * size: size of QTAILQ element
 727 * start: offset of QTAILQ entry in QTAILQ element
 728 */
 729#define VMSTATE_QTAILQ_V(_field, _state, _version, _vmsd, _type, _next)  \
 730{                                                                        \
 731    .name         = (stringify(_field)),                                 \
 732    .version_id   = (_version),                                          \
 733    .vmsd         = &(_vmsd),                                            \
 734    .size         = sizeof(_type),                                       \
 735    .info         = &vmstate_info_qtailq,                                \
 736    .offset       = offsetof(_state, _field),                            \
 737    .start        = offsetof(_type, _next),                              \
 738}
 739
 740/* _f : field name
 741   _f_n : num of elements field_name
 742   _n : num of elements
 743   _s : struct state name
 744   _v : version
 745*/
 746
 747#define VMSTATE_SINGLE(_field, _state, _version, _info, _type)        \
 748    VMSTATE_SINGLE_TEST(_field, _state, NULL, _version, _info, _type)
 749
 750#define VMSTATE_VSTRUCT(_field, _state, _vmsd, _type, _struct_version)\
 751    VMSTATE_VSTRUCT_TEST(_field, _state, NULL, 0, _vmsd, _type, _struct_version)
 752
 753#define VMSTATE_VSTRUCT_V(_field, _state, _version, _vmsd, _type, _struct_version) \
 754    VMSTATE_VSTRUCT_TEST(_field, _state, NULL, _version, _vmsd, _type, \
 755                         _struct_version)
 756
 757#define VMSTATE_STRUCT(_field, _state, _version, _vmsd, _type)        \
 758    VMSTATE_STRUCT_TEST(_field, _state, NULL, _version, _vmsd, _type)
 759
 760#define VMSTATE_STRUCT_POINTER(_field, _state, _vmsd, _type)          \
 761    VMSTATE_STRUCT_POINTER_V(_field, _state, 0, _vmsd, _type)
 762
 763#define VMSTATE_STRUCT_POINTER_TEST(_field, _state, _test, _vmsd, _type)     \
 764    VMSTATE_STRUCT_POINTER_TEST_V(_field, _state, _test, 0, _vmsd, _type)
 765
 766#define VMSTATE_STRUCT_ARRAY(_field, _state, _num, _version, _vmsd, _type) \
 767    VMSTATE_STRUCT_ARRAY_TEST(_field, _state, _num, NULL, _version,   \
 768            _vmsd, _type)
 769
 770#define VMSTATE_STRUCT_2DARRAY(_field, _state, _n1, _n2, _version,    \
 771            _vmsd, _type)                                             \
 772    VMSTATE_STRUCT_2DARRAY_TEST(_field, _state, _n1, _n2, NULL,       \
 773            _version, _vmsd, _type)
 774
 775#define VMSTATE_BUFFER_UNSAFE_INFO(_field, _state, _version, _info, _size) \
 776    VMSTATE_BUFFER_UNSAFE_INFO_TEST(_field, _state, NULL, _version, _info, \
 777            _size)
 778
 779#define VMSTATE_BOOL_V(_f, _s, _v)                                    \
 780    VMSTATE_SINGLE(_f, _s, _v, vmstate_info_bool, bool)
 781
 782#define VMSTATE_INT8_V(_f, _s, _v)                                    \
 783    VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int8, int8_t)
 784#define VMSTATE_INT16_V(_f, _s, _v)                                   \
 785    VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int16, int16_t)
 786#define VMSTATE_INT32_V(_f, _s, _v)                                   \
 787    VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int32, int32_t)
 788#define VMSTATE_INT64_V(_f, _s, _v)                                   \
 789    VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int64, int64_t)
 790
 791#define VMSTATE_UINT8_V(_f, _s, _v)                                   \
 792    VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint8, uint8_t)
 793#define VMSTATE_UINT16_V(_f, _s, _v)                                  \
 794    VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint16, uint16_t)
 795#define VMSTATE_UINT32_V(_f, _s, _v)                                  \
 796    VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint32, uint32_t)
 797#define VMSTATE_UINT64_V(_f, _s, _v)                                  \
 798    VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint64, uint64_t)
 799
 800#define VMSTATE_BOOL(_f, _s)                                          \
 801    VMSTATE_BOOL_V(_f, _s, 0)
 802
 803#define VMSTATE_INT8(_f, _s)                                          \
 804    VMSTATE_INT8_V(_f, _s, 0)
 805#define VMSTATE_INT16(_f, _s)                                         \
 806    VMSTATE_INT16_V(_f, _s, 0)
 807#define VMSTATE_INT32(_f, _s)                                         \
 808    VMSTATE_INT32_V(_f, _s, 0)
 809#define VMSTATE_INT64(_f, _s)                                         \
 810    VMSTATE_INT64_V(_f, _s, 0)
 811
 812#define VMSTATE_UINT8(_f, _s)                                         \
 813    VMSTATE_UINT8_V(_f, _s, 0)
 814#define VMSTATE_UINT16(_f, _s)                                        \
 815    VMSTATE_UINT16_V(_f, _s, 0)
 816#define VMSTATE_UINT32(_f, _s)                                        \
 817    VMSTATE_UINT32_V(_f, _s, 0)
 818#define VMSTATE_UINT64(_f, _s)                                        \
 819    VMSTATE_UINT64_V(_f, _s, 0)
 820
 821#define VMSTATE_UINT8_EQUAL(_f, _s, _err_hint)                        \
 822    VMSTATE_SINGLE_FULL(_f, _s, 0, 0,                                 \
 823                        vmstate_info_uint8_equal, uint8_t, _err_hint)
 824
 825#define VMSTATE_UINT16_EQUAL(_f, _s, _err_hint)                       \
 826    VMSTATE_SINGLE_FULL(_f, _s, 0, 0,                                 \
 827                        vmstate_info_uint16_equal, uint16_t, _err_hint)
 828
 829#define VMSTATE_UINT16_EQUAL_V(_f, _s, _v, _err_hint)                 \
 830    VMSTATE_SINGLE_FULL(_f, _s, 0,  _v,                               \
 831                        vmstate_info_uint16_equal, uint16_t, _err_hint)
 832
 833#define VMSTATE_INT32_EQUAL(_f, _s, _err_hint)                        \
 834    VMSTATE_SINGLE_FULL(_f, _s, 0, 0,                                 \
 835                        vmstate_info_int32_equal, int32_t, _err_hint)
 836
 837#define VMSTATE_UINT32_EQUAL_V(_f, _s, _v, _err_hint)                 \
 838    VMSTATE_SINGLE_FULL(_f, _s, 0,  _v,                               \
 839                        vmstate_info_uint32_equal, uint32_t, _err_hint)
 840
 841#define VMSTATE_UINT32_EQUAL(_f, _s, _err_hint)                       \
 842    VMSTATE_UINT32_EQUAL_V(_f, _s, 0, _err_hint)
 843
 844#define VMSTATE_UINT64_EQUAL_V(_f, _s, _v, _err_hint)                 \
 845    VMSTATE_SINGLE_FULL(_f, _s, 0,  _v,                               \
 846                        vmstate_info_uint64_equal, uint64_t, _err_hint)
 847
 848#define VMSTATE_UINT64_EQUAL(_f, _s, _err_hint)                       \
 849    VMSTATE_UINT64_EQUAL_V(_f, _s, 0, _err_hint)
 850
 851#define VMSTATE_INT32_POSITIVE_LE(_f, _s)                             \
 852    VMSTATE_SINGLE(_f, _s, 0, vmstate_info_int32_le, int32_t)
 853
 854#define VMSTATE_BOOL_TEST(_f, _s, _t)                               \
 855    VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_bool, bool)
 856
 857#define VMSTATE_INT8_TEST(_f, _s, _t)                               \
 858    VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_int8, int8_t)
 859
 860#define VMSTATE_INT16_TEST(_f, _s, _t)                               \
 861    VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_int16, int16_t)
 862
 863#define VMSTATE_INT32_TEST(_f, _s, _t)                                  \
 864    VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_int32, int32_t)
 865
 866#define VMSTATE_INT64_TEST(_f, _s, _t)                                  \
 867    VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_int64, int64_t)
 868
 869#define VMSTATE_UINT8_TEST(_f, _s, _t)                               \
 870    VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint8, uint8_t)
 871
 872#define VMSTATE_UINT16_TEST(_f, _s, _t)                               \
 873    VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint16, uint16_t)
 874
 875#define VMSTATE_UINT32_TEST(_f, _s, _t)                                  \
 876    VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint32, uint32_t)
 877
 878#define VMSTATE_UINT64_TEST(_f, _s, _t)                                  \
 879    VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint64, uint64_t)
 880
 881
 882#define VMSTATE_FLOAT64_V(_f, _s, _v)                                 \
 883    VMSTATE_SINGLE(_f, _s, _v, vmstate_info_float64, float64)
 884
 885#define VMSTATE_FLOAT64(_f, _s)                                       \
 886    VMSTATE_FLOAT64_V(_f, _s, 0)
 887
 888#define VMSTATE_TIMER_PTR_TEST(_f, _s, _test)                             \
 889    VMSTATE_POINTER_TEST(_f, _s, _test, vmstate_info_timer, QEMUTimer *)
 890
 891#define VMSTATE_TIMER_PTR_V(_f, _s, _v)                                   \
 892    VMSTATE_POINTER(_f, _s, _v, vmstate_info_timer, QEMUTimer *)
 893
 894#define VMSTATE_TIMER_PTR(_f, _s)                                         \
 895    VMSTATE_TIMER_PTR_V(_f, _s, 0)
 896
 897#define VMSTATE_TIMER_PTR_ARRAY(_f, _s, _n)                              \
 898    VMSTATE_ARRAY_OF_POINTER(_f, _s, _n, 0, vmstate_info_timer, QEMUTimer *)
 899
 900#define VMSTATE_TIMER_TEST(_f, _s, _test)                             \
 901    VMSTATE_SINGLE_TEST(_f, _s, _test, 0, vmstate_info_timer, QEMUTimer)
 902
 903#define VMSTATE_TIMER_V(_f, _s, _v)                                   \
 904    VMSTATE_SINGLE(_f, _s, _v, vmstate_info_timer, QEMUTimer)
 905
 906#define VMSTATE_TIMER(_f, _s)                                         \
 907    VMSTATE_TIMER_V(_f, _s, 0)
 908
 909#define VMSTATE_TIMER_ARRAY(_f, _s, _n)                              \
 910    VMSTATE_ARRAY(_f, _s, _n, 0, vmstate_info_timer, QEMUTimer)
 911
 912#define VMSTATE_BOOL_ARRAY_V(_f, _s, _n, _v)                         \
 913    VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_bool, bool)
 914
 915#define VMSTATE_BOOL_ARRAY(_f, _s, _n)                               \
 916    VMSTATE_BOOL_ARRAY_V(_f, _s, _n, 0)
 917
 918#define VMSTATE_BOOL_SUB_ARRAY(_f, _s, _start, _num)                \
 919    VMSTATE_SUB_ARRAY(_f, _s, _start, _num, 0, vmstate_info_bool, bool)
 920
 921#define VMSTATE_UINT16_ARRAY_V(_f, _s, _n, _v)                         \
 922    VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint16, uint16_t)
 923
 924#define VMSTATE_UINT16_2DARRAY_V(_f, _s, _n1, _n2, _v)                \
 925    VMSTATE_2DARRAY(_f, _s, _n1, _n2, _v, vmstate_info_uint16, uint16_t)
 926
 927#define VMSTATE_UINT16_ARRAY(_f, _s, _n)                               \
 928    VMSTATE_UINT16_ARRAY_V(_f, _s, _n, 0)
 929
 930#define VMSTATE_UINT16_SUB_ARRAY(_f, _s, _start, _num)                \
 931    VMSTATE_SUB_ARRAY(_f, _s, _start, _num, 0, vmstate_info_uint16, uint16_t)
 932
 933#define VMSTATE_UINT16_2DARRAY(_f, _s, _n1, _n2)                      \
 934    VMSTATE_UINT16_2DARRAY_V(_f, _s, _n1, _n2, 0)
 935
 936#define VMSTATE_UINT8_2DARRAY_V(_f, _s, _n1, _n2, _v)                 \
 937    VMSTATE_2DARRAY(_f, _s, _n1, _n2, _v, vmstate_info_uint8, uint8_t)
 938
 939#define VMSTATE_UINT8_ARRAY_V(_f, _s, _n, _v)                         \
 940    VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint8, uint8_t)
 941
 942#define VMSTATE_UINT8_ARRAY(_f, _s, _n)                               \
 943    VMSTATE_UINT8_ARRAY_V(_f, _s, _n, 0)
 944
 945#define VMSTATE_UINT8_SUB_ARRAY(_f, _s, _start, _num)                \
 946    VMSTATE_SUB_ARRAY(_f, _s, _start, _num, 0, vmstate_info_uint8, uint8_t)
 947
 948#define VMSTATE_UINT8_2DARRAY(_f, _s, _n1, _n2)                       \
 949    VMSTATE_UINT8_2DARRAY_V(_f, _s, _n1, _n2, 0)
 950
 951#define VMSTATE_UINT32_ARRAY_V(_f, _s, _n, _v)                        \
 952    VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint32, uint32_t)
 953
 954#define VMSTATE_UINT32_2DARRAY_V(_f, _s, _n1, _n2, _v)                \
 955    VMSTATE_2DARRAY(_f, _s, _n1, _n2, _v, vmstate_info_uint32, uint32_t)
 956
 957#define VMSTATE_UINT32_ARRAY(_f, _s, _n)                              \
 958    VMSTATE_UINT32_ARRAY_V(_f, _s, _n, 0)
 959
 960#define VMSTATE_UINT32_SUB_ARRAY(_f, _s, _start, _num)                \
 961    VMSTATE_SUB_ARRAY(_f, _s, _start, _num, 0, vmstate_info_uint32, uint32_t)
 962
 963#define VMSTATE_UINT32_2DARRAY(_f, _s, _n1, _n2)                      \
 964    VMSTATE_UINT32_2DARRAY_V(_f, _s, _n1, _n2, 0)
 965
 966#define VMSTATE_UINT64_ARRAY_V(_f, _s, _n, _v)                        \
 967    VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint64, uint64_t)
 968
 969#define VMSTATE_UINT64_ARRAY(_f, _s, _n)                              \
 970    VMSTATE_UINT64_ARRAY_V(_f, _s, _n, 0)
 971
 972#define VMSTATE_UINT64_SUB_ARRAY(_f, _s, _start, _num)                \
 973    VMSTATE_SUB_ARRAY(_f, _s, _start, _num, 0, vmstate_info_uint64, uint64_t)
 974
 975#define VMSTATE_UINT64_2DARRAY(_f, _s, _n1, _n2)                      \
 976    VMSTATE_UINT64_2DARRAY_V(_f, _s, _n1, _n2, 0)
 977
 978#define VMSTATE_UINT64_2DARRAY_V(_f, _s, _n1, _n2, _v)                 \
 979    VMSTATE_2DARRAY(_f, _s, _n1, _n2, _v, vmstate_info_uint64, uint64_t)
 980
 981#define VMSTATE_INT16_ARRAY_V(_f, _s, _n, _v)                         \
 982    VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_int16, int16_t)
 983
 984#define VMSTATE_INT16_ARRAY(_f, _s, _n)                               \
 985    VMSTATE_INT16_ARRAY_V(_f, _s, _n, 0)
 986
 987#define VMSTATE_INT32_ARRAY_V(_f, _s, _n, _v)                         \
 988    VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_int32, int32_t)
 989
 990#define VMSTATE_INT32_ARRAY(_f, _s, _n)                               \
 991    VMSTATE_INT32_ARRAY_V(_f, _s, _n, 0)
 992
 993#define VMSTATE_INT64_ARRAY_V(_f, _s, _n, _v)                         \
 994    VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_int64, int64_t)
 995
 996#define VMSTATE_INT64_ARRAY(_f, _s, _n)                               \
 997    VMSTATE_INT64_ARRAY_V(_f, _s, _n, 0)
 998
 999#define VMSTATE_FLOAT64_ARRAY_V(_f, _s, _n, _v)                       \
1000    VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_float64, float64)
1001
1002#define VMSTATE_FLOAT64_ARRAY(_f, _s, _n)                             \
1003    VMSTATE_FLOAT64_ARRAY_V(_f, _s, _n, 0)
1004
1005#define VMSTATE_CPUDOUBLE_ARRAY_V(_f, _s, _n, _v)                     \
1006    VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_cpudouble, CPU_DoubleU)
1007
1008#define VMSTATE_CPUDOUBLE_ARRAY(_f, _s, _n)                           \
1009    VMSTATE_CPUDOUBLE_ARRAY_V(_f, _s, _n, 0)
1010
1011#define VMSTATE_BUFFER_V(_f, _s, _v)                                  \
1012    VMSTATE_STATIC_BUFFER(_f, _s, _v, NULL, 0, sizeof(typeof_field(_s, _f)))
1013
1014#define VMSTATE_BUFFER(_f, _s)                                        \
1015    VMSTATE_BUFFER_V(_f, _s, 0)
1016
1017#define VMSTATE_PARTIAL_BUFFER(_f, _s, _size)                         \
1018    VMSTATE_STATIC_BUFFER(_f, _s, 0, NULL, 0, _size)
1019
1020#define VMSTATE_BUFFER_START_MIDDLE_V(_f, _s, _start, _v) \
1021    VMSTATE_STATIC_BUFFER(_f, _s, _v, NULL, _start, sizeof(typeof_field(_s, _f)))
1022
1023#define VMSTATE_BUFFER_START_MIDDLE(_f, _s, _start) \
1024    VMSTATE_BUFFER_START_MIDDLE_V(_f, _s, _start, 0)
1025
1026#define VMSTATE_PARTIAL_VBUFFER(_f, _s, _size)                        \
1027    VMSTATE_VBUFFER(_f, _s, 0, NULL, _size)
1028
1029#define VMSTATE_PARTIAL_VBUFFER_UINT32(_f, _s, _size)                        \
1030    VMSTATE_VBUFFER_UINT32(_f, _s, 0, NULL, _size)
1031
1032#define VMSTATE_BUFFER_TEST(_f, _s, _test)                            \
1033    VMSTATE_STATIC_BUFFER(_f, _s, 0, _test, 0, sizeof(typeof_field(_s, _f)))
1034
1035#define VMSTATE_BUFFER_UNSAFE(_field, _state, _version, _size)        \
1036    VMSTATE_BUFFER_UNSAFE_INFO(_field, _state, _version, vmstate_info_buffer, _size)
1037
1038#define VMSTATE_UNUSED_V(_v, _size)                                   \
1039    VMSTATE_UNUSED_BUFFER(NULL, _v, _size)
1040
1041#define VMSTATE_UNUSED(_size)                                         \
1042    VMSTATE_UNUSED_V(0, _size)
1043
1044#define VMSTATE_UNUSED_TEST(_test, _size)                             \
1045    VMSTATE_UNUSED_BUFFER(_test, 0, _size)
1046
1047#define VMSTATE_END_OF_LIST()                                         \
1048    {}
1049
1050int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1051                       void *opaque, int version_id);
1052int vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
1053                       void *opaque, QJSON *vmdesc);
1054int vmstate_save_state_v(QEMUFile *f, const VMStateDescription *vmsd,
1055                         void *opaque, QJSON *vmdesc, int version_id);
1056
1057bool vmstate_save_needed(const VMStateDescription *vmsd, void *opaque);
1058
1059/* Returns: 0 on success, -1 on failure */
1060int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
1061                                   const VMStateDescription *vmsd,
1062                                   void *base, int alias_id,
1063                                   int required_for_version,
1064                                   Error **errp);
1065
1066/* Returns: 0 on success, -1 on failure */
1067static inline int vmstate_register(DeviceState *dev, int instance_id,
1068                                   const VMStateDescription *vmsd,
1069                                   void *opaque)
1070{
1071    return vmstate_register_with_alias_id(dev, instance_id, vmsd,
1072                                          opaque, -1, 0, NULL);
1073}
1074
1075void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
1076                        void *opaque);
1077
1078struct MemoryRegion;
1079void vmstate_register_ram(struct MemoryRegion *memory, DeviceState *dev);
1080void vmstate_unregister_ram(struct MemoryRegion *memory, DeviceState *dev);
1081void vmstate_register_ram_global(struct MemoryRegion *memory);
1082
1083bool vmstate_check_only_migratable(const VMStateDescription *vmsd);
1084
1085#endif
1086