xref: /openbmc/qemu/include/migration/vmstate.h (revision bf0e7b068b7d156eb2009776c9b90afdf93abb2f)
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 
30 #include "hw/vmstate-if.h"
31 
32 typedef struct VMStateInfo VMStateInfo;
33 typedef struct VMStateField VMStateField;
34 
35 /* VMStateInfo allows customized migration of objects that don't fit in
36  * any category in VMStateFlags. Additional information is always passed
37  * into get and put in terms of field and vmdesc parameters. However
38  * these two parameters should only be used in cases when customized
39  * handling is needed, such as QTAILQ. For primitive data types such as
40  * integer, field and vmdesc parameters should be ignored inside get/put.
41  */
42 struct VMStateInfo {
43     const char *name;
44     int coroutine_mixed_fn (*get)(QEMUFile *f, void *pv, size_t size,
45                                   const VMStateField *field);
46     int coroutine_mixed_fn (*put)(QEMUFile *f, void *pv, size_t size,
47                                   const VMStateField *field,
48                                   JSONWriter *vmdesc);
49 };
50 
51 enum VMStateFlags {
52     /* Ignored */
53     VMS_SINGLE           = 0x001,
54 
55     /* The struct member at opaque + VMStateField.offset is a pointer
56      * to the actual field (e.g. struct a { uint8_t *b;
57      * }). Dereference the pointer before using it as basis for
58      * further pointer arithmetic (see e.g. VMS_ARRAY). Does not
59      * affect the meaning of VMStateField.num_offset or
60      * VMStateField.size_offset; see VMS_VARRAY* and VMS_VBUFFER for
61      * those. */
62     VMS_POINTER          = 0x002,
63 
64     /* The field is an array of fixed size. VMStateField.num contains
65      * the number of entries in the array. The size of each entry is
66      * given by VMStateField.size and / or opaque +
67      * VMStateField.size_offset; see VMS_VBUFFER and
68      * VMS_MULTIPLY. Each array entry will be processed individually
69      * (VMStateField.info.get()/put() if VMS_STRUCT is not set,
70      * recursion into VMStateField.vmsd if VMS_STRUCT is set). May not
71      * be combined with VMS_VARRAY*. */
72     VMS_ARRAY            = 0x004,
73 
74     /* The field is itself a struct, containing one or more
75      * fields. Recurse into VMStateField.vmsd. Most useful in
76      * combination with VMS_ARRAY / VMS_VARRAY*, recursing into each
77      * array entry. */
78     VMS_STRUCT           = 0x008,
79 
80     /* The field is an array of variable size. The int32_t at opaque +
81      * VMStateField.num_offset contains the number of entries in the
82      * array. See the VMS_ARRAY description regarding array handling
83      * in general. May not be combined with VMS_ARRAY or any other
84      * VMS_VARRAY*. */
85     VMS_VARRAY_INT32     = 0x010,
86 
87     /* Ignored */
88     VMS_BUFFER           = 0x020,
89 
90     /* The field is a (fixed-size or variable-size) array of pointers
91      * (e.g. struct a { uint8_t *b[]; }). Dereference each array entry
92      * before using it. Note: Does not imply any one of VMS_ARRAY /
93      * VMS_VARRAY*; these need to be set explicitly. */
94     VMS_ARRAY_OF_POINTER = 0x040,
95 
96     /* The field is an array of variable size. The uint16_t at opaque
97      * + VMStateField.num_offset (subject to VMS_MULTIPLY_ELEMENTS)
98      * contains the number of entries in the array. See the VMS_ARRAY
99      * description regarding array handling in general. May not be
100      * combined with VMS_ARRAY or any other VMS_VARRAY*. */
101     VMS_VARRAY_UINT16    = 0x080,
102 
103     /* The size of the individual entries (a single array entry if
104      * VMS_ARRAY or any of VMS_VARRAY* are set, or the field itself if
105      * neither is set) is variable (i.e. not known at compile-time),
106      * but the same for all entries. Use the int32_t at opaque +
107      * VMStateField.size_offset (subject to VMS_MULTIPLY) to determine
108      * the size of each (and every) entry. */
109     VMS_VBUFFER          = 0x100,
110 
111     /* Multiply the entry size given by the int32_t at opaque +
112      * VMStateField.size_offset (see VMS_VBUFFER description) with
113      * VMStateField.size to determine the number of bytes to be
114      * allocated. Only valid in combination with VMS_VBUFFER. */
115     VMS_MULTIPLY         = 0x200,
116 
117     /* The field is an array of variable size. The uint8_t at opaque +
118      * VMStateField.num_offset (subject to VMS_MULTIPLY_ELEMENTS)
119      * contains the number of entries in the array. See the VMS_ARRAY
120      * description regarding array handling in general. May not be
121      * combined with VMS_ARRAY or any other VMS_VARRAY*. */
122     VMS_VARRAY_UINT8     = 0x400,
123 
124     /* The field is an array of variable size. The uint32_t at opaque
125      * + VMStateField.num_offset (subject to VMS_MULTIPLY_ELEMENTS)
126      * contains the number of entries in the array. See the VMS_ARRAY
127      * description regarding array handling in general. May not be
128      * combined with VMS_ARRAY or any other VMS_VARRAY*. */
129     VMS_VARRAY_UINT32    = 0x800,
130 
131     /* Fail loading the serialised VM state if this field is missing
132      * from the input. */
133     VMS_MUST_EXIST       = 0x1000,
134 
135     /* When loading serialised VM state, allocate memory for the
136      * (entire) field. Only valid in combination with
137      * VMS_POINTER. Note: Not all combinations with other flags are
138      * currently supported, e.g. VMS_ALLOC|VMS_ARRAY_OF_POINTER won't
139      * cause the individual entries to be allocated. */
140     VMS_ALLOC            = 0x2000,
141 
142     /* Multiply the number of entries given by the integer at opaque +
143      * VMStateField.num_offset (see VMS_VARRAY*) with VMStateField.num
144      * to determine the number of entries in the array. Only valid in
145      * combination with one of VMS_VARRAY*. */
146     VMS_MULTIPLY_ELEMENTS = 0x4000,
147 
148     /* A structure field that is like VMS_STRUCT, but uses
149      * VMStateField.struct_version_id to tell which version of the
150      * structure we are referencing to use. */
151     VMS_VSTRUCT           = 0x8000,
152 
153     /* Marker for end of list */
154     VMS_END = 0x10000
155 };
156 
157 typedef enum {
158     MIG_PRI_UNINITIALIZED = 0,  /* An uninitialized priority field maps to */
159                                 /* MIG_PRI_DEFAULT in save_state_priority */
160 
161     MIG_PRI_LOW,                /* Must happen after default */
162     MIG_PRI_DEFAULT,
163     MIG_PRI_IOMMU,              /* Must happen before PCI devices */
164     MIG_PRI_PCI_BUS,            /* Must happen before IOMMU */
165     MIG_PRI_VIRTIO_MEM,         /* Must happen before IOMMU */
166     MIG_PRI_APIC,               /* Must happen before PCI devices */
167     MIG_PRI_GICV3_ITS,          /* Must happen before PCI devices */
168     MIG_PRI_GICV3,              /* Must happen before the ITS */
169     MIG_PRI_MAX,
170 } MigrationPriority;
171 
172 struct VMStateField {
173     const char *name;
174     const char *err_hint;
175     size_t offset;
176     size_t size;
177     size_t start;
178     int num;
179     size_t num_offset;
180     size_t size_offset;
181     const VMStateInfo *info;
182     enum VMStateFlags flags;
183     const VMStateDescription *vmsd;
184     int version_id;
185     int struct_version_id;
186     bool (*field_exists)(void *opaque, int version_id);
187 };
188 
189 struct VMStateDescription {
190     const char *name;
191     bool unmigratable;
192     /*
193      * This VMSD describes something that should be sent during setup phase
194      * of migration. It plays similar role as save_setup() for explicitly
195      * registered vmstate entries, so it can be seen as a way to describe
196      * save_setup() in VMSD structures.
197      *
198      * Note that for now, a SaveStateEntry cannot have a VMSD and
199      * operations (e.g., save_setup()) set at the same time. Consequently,
200      * save_setup() and a VMSD with early_setup set to true are mutually
201      * exclusive. For this reason, also early_setup VMSDs are migrated in a
202      * QEMU_VM_SECTION_FULL section, while save_setup() data is migrated in
203      * a QEMU_VM_SECTION_START section.
204      *
205      * There are duplicate impls of the post/pre save/load hooks.
206      * New impls should preferentally use 'errp' variants of these
207      * methods and existing impls incrementally converted.
208      * The variants without 'errp' are intended to be removed
209      * once all usage is converted.
210      *
211      * For the errp variants,
212      * Returns: 0 on success,
213      *          <0 on error where -value is an error number from errno.h
214      */
215 
216     bool early_setup;
217     int version_id;
218     int minimum_version_id;
219     MigrationPriority priority;
220     int (*pre_load)(void *opaque);
221     bool (*pre_load_errp)(void *opaque, Error **errp);
222     int (*post_load)(void *opaque, int version_id);
223     bool (*post_load_errp)(void *opaque, int version_id, Error **errp);
224     int (*pre_save)(void *opaque);
225     bool (*pre_save_errp)(void *opaque, Error **errp);
226     int (*post_save)(void *opaque);
227     bool (*needed)(void *opaque);
228     bool (*dev_unplug_pending)(void *opaque);
229 
230     const VMStateField *fields;
231     const VMStateDescription * const *subsections;
232 };
233 
234 extern const VMStateInfo vmstate_info_bool;
235 
236 extern const VMStateInfo vmstate_info_int8;
237 extern const VMStateInfo vmstate_info_int16;
238 extern const VMStateInfo vmstate_info_int32;
239 extern const VMStateInfo vmstate_info_int64;
240 
241 extern const VMStateInfo vmstate_info_uint8_equal;
242 extern const VMStateInfo vmstate_info_uint16_equal;
243 extern const VMStateInfo vmstate_info_int32_equal;
244 extern const VMStateInfo vmstate_info_uint32_equal;
245 extern const VMStateInfo vmstate_info_uint64_equal;
246 extern const VMStateInfo vmstate_info_int32_le;
247 
248 extern const VMStateInfo vmstate_info_uint8;
249 extern const VMStateInfo vmstate_info_uint16;
250 extern const VMStateInfo vmstate_info_uint32;
251 extern const VMStateInfo vmstate_info_uint64;
252 extern const VMStateInfo vmstate_info_fd;
253 
254 /** Put this in the stream when migrating a null pointer.*/
255 #define VMS_NULLPTR_MARKER (0x30U) /* '0' */
256 extern const VMStateInfo vmstate_info_nullptr;
257 
258 extern const VMStateInfo vmstate_info_cpudouble;
259 
260 extern const VMStateInfo vmstate_info_timer;
261 extern const VMStateInfo vmstate_info_buffer;
262 extern const VMStateInfo vmstate_info_unused_buffer;
263 extern const VMStateInfo vmstate_info_tmp;
264 extern const VMStateInfo vmstate_info_bitmap;
265 extern const VMStateInfo vmstate_info_qtailq;
266 extern const VMStateInfo vmstate_info_gtree;
267 extern const VMStateInfo vmstate_info_qlist;
268 
269 #define type_check_2darray(t1,t2,n,m) ((t1(*)[n][m])0 - (t2*)0)
270 /*
271  * Check that type t2 is an array of type t1 of size n,
272  * e.g. if t1 is 'foo' and n is 32 then t2 must be 'foo[32]'
273  */
274 #define type_check_array(t1,t2,n) ((t1(*)[n])0 - (t2*)0)
275 #define type_check_pointer(t1,t2) ((t1**)0 - (t2*)0)
276 /*
277  * type of element 0 of the specified (array) field of the type.
278  * Note that if the field is a pointer then this will return the
279  * pointed-to type rather than complaining.
280  */
281 #define typeof_elt_of_field(type, field) typeof(((type *)0)->field[0])
282 /* Check that field f in struct type t2 is an array of t1, of any size */
283 #define type_check_varray(t1, t2, f)                                 \
284     (type_check(t1, typeof_elt_of_field(t2, f))                      \
285      + QEMU_BUILD_BUG_ON_ZERO(!QEMU_IS_ARRAY(((t2 *)0)->f)))
286 
287 #define vmstate_offset_value(_state, _field, _type)                  \
288     (offsetof(_state, _field) +                                      \
289      type_check(_type, typeof_field(_state, _field)))
290 
291 #define vmstate_offset_pointer(_state, _field, _type)                \
292     (offsetof(_state, _field) +                                      \
293      type_check_pointer(_type, typeof_field(_state, _field)))
294 
295 #define vmstate_offset_array(_state, _field, _type, _num)            \
296     (offsetof(_state, _field) +                                      \
297      type_check_array(_type, typeof_field(_state, _field), _num))
298 
299 #define vmstate_offset_2darray(_state, _field, _type, _n1, _n2)      \
300     (offsetof(_state, _field) +                                      \
301      type_check_2darray(_type, typeof_field(_state, _field), _n1, _n2))
302 
303 #define vmstate_offset_sub_array(_state, _field, _type, _start)      \
304     vmstate_offset_value(_state, _field[_start], _type)
305 
306 #define vmstate_offset_buffer(_state, _field)                        \
307     vmstate_offset_array(_state, _field, uint8_t,                    \
308                          sizeof(typeof_field(_state, _field)))
309 
310 #define vmstate_offset_varray(_state, _field, _type)                 \
311     (offsetof(_state, _field) +                                      \
312      type_check_varray(_type, _state, _field))
313 
314 /* In the macros below, if there is a _version, that means the macro's
315  * field will be processed only if the version being received is >=
316  * the _version specified.  In general, if you add a new field, you
317  * would increment the structure's version and put that version
318  * number into the new field so it would only be processed with the
319  * new version.
320  *
321  * In particular, for VMSTATE_STRUCT() and friends the _version does
322  * *NOT* pick the version of the sub-structure.  It works just as
323  * specified above.  The version of the top-level structure received
324  * is passed down to all sub-structures.  This means that the
325  * sub-structures must have version that are compatible with all the
326  * structures that use them.
327  *
328  * If you want to specify the version of the sub-structure, use
329  * VMSTATE_VSTRUCT(), which allows the specific sub-structure version
330  * to be directly specified.
331  */
332 
333 #define VMSTATE_SINGLE_TEST(_field, _state, _test, _version, _info, _type) { \
334     .name         = (stringify(_field)),                             \
335     .version_id   = (_version),                                      \
336     .field_exists = (_test),                                         \
337     .size         = sizeof(_type),                                   \
338     .info         = &(_info),                                        \
339     .flags        = VMS_SINGLE,                                      \
340     .offset       = vmstate_offset_value(_state, _field, _type),     \
341 }
342 
343 #define VMSTATE_SINGLE_FULL(_field, _state, _test, _version, _info,  \
344                             _type, _err_hint) {                      \
345     .name         = (stringify(_field)),                             \
346     .err_hint     = (_err_hint),                                     \
347     .version_id   = (_version),                                      \
348     .field_exists = (_test),                                         \
349     .size         = sizeof(_type),                                   \
350     .info         = &(_info),                                        \
351     .flags        = VMS_SINGLE,                                      \
352     .offset       = vmstate_offset_value(_state, _field, _type),     \
353 }
354 
355 /* Validate state using a boolean predicate. */
356 #define VMSTATE_VALIDATE(_name, _test) { \
357     .name         = (_name),                                         \
358     .field_exists = (_test),                                         \
359     .flags        = VMS_ARRAY | VMS_MUST_EXIST,                      \
360     .num          = 0, /* 0 elements: no data, only run _test */     \
361 }
362 
363 #define VMSTATE_POINTER(_field, _state, _version, _info, _type) {    \
364     .name       = (stringify(_field)),                               \
365     .version_id = (_version),                                        \
366     .info       = &(_info),                                          \
367     .size       = sizeof(_type),                                     \
368     .flags      = VMS_SINGLE|VMS_POINTER,                            \
369     .offset     = vmstate_offset_value(_state, _field, _type),       \
370 }
371 
372 #define VMSTATE_POINTER_TEST(_field, _state, _test, _info, _type) {  \
373     .name       = (stringify(_field)),                               \
374     .info       = &(_info),                                          \
375     .field_exists = (_test),                                         \
376     .size       = sizeof(_type),                                     \
377     .flags      = VMS_SINGLE|VMS_POINTER,                            \
378     .offset     = vmstate_offset_value(_state, _field, _type),       \
379 }
380 
381 #define VMSTATE_ARRAY(_field, _state, _num, _version, _info, _type) {\
382     .name       = (stringify(_field)),                               \
383     .version_id = (_version),                                        \
384     .num        = (_num),                                            \
385     .info       = &(_info),                                          \
386     .size       = sizeof(_type),                                     \
387     .flags      = VMS_ARRAY,                                         \
388     .offset     = vmstate_offset_array(_state, _field, _type, _num), \
389 }
390 
391 #define VMSTATE_2DARRAY(_field, _state, _n1, _n2, _version, _info, _type) { \
392     .name       = (stringify(_field)),                                      \
393     .version_id = (_version),                                               \
394     .num        = (_n1) * (_n2),                                            \
395     .info       = &(_info),                                                 \
396     .size       = sizeof(_type),                                            \
397     .flags      = VMS_ARRAY,                                                \
398     .offset     = vmstate_offset_2darray(_state, _field, _type, _n1, _n2),  \
399 }
400 
401 #define VMSTATE_VARRAY_MULTIPLY(_field, _state, _field_num, _multiply, _info, _type) { \
402     .name       = (stringify(_field)),                               \
403     .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
404     .num        = (_multiply),                                       \
405     .info       = &(_info),                                          \
406     .size       = sizeof(_type),                                     \
407     .flags      = VMS_VARRAY_UINT32|VMS_MULTIPLY_ELEMENTS,           \
408     .offset     = vmstate_offset_varray(_state, _field, _type),      \
409 }
410 
411 #define VMSTATE_SUB_ARRAY(_field, _state, _start, _num, _version, _info, _type) { \
412     .name       = (stringify(_field)),                               \
413     .version_id = (_version),                                        \
414     .num        = (_num),                                            \
415     .info       = &(_info),                                          \
416     .size       = sizeof(_type),                                     \
417     .flags      = VMS_ARRAY,                                         \
418     .offset     = vmstate_offset_sub_array(_state, _field, _type, _start), \
419 }
420 
421 #define VMSTATE_ARRAY_INT32_UNSAFE(_field, _state, _field_num, _info, _type) {\
422     .name       = (stringify(_field)),                               \
423     .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
424     .info       = &(_info),                                          \
425     .size       = sizeof(_type),                                     \
426     .flags      = VMS_VARRAY_INT32,                                  \
427     .offset     = vmstate_offset_varray(_state, _field, _type),      \
428 }
429 
430 #define VMSTATE_VARRAY_INT32(_field, _state, _field_num, _version, _info, _type) {\
431     .name       = (stringify(_field)),                               \
432     .version_id = (_version),                                        \
433     .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
434     .info       = &(_info),                                          \
435     .size       = sizeof(_type),                                     \
436     .flags      = VMS_VARRAY_INT32|VMS_POINTER,                      \
437     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
438 }
439 
440 #define VMSTATE_VARRAY_UINT32(_field, _state, _field_num, _version, _info, _type) {\
441     .name       = (stringify(_field)),                               \
442     .version_id = (_version),                                        \
443     .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
444     .info       = &(_info),                                          \
445     .size       = sizeof(_type),                                     \
446     .flags      = VMS_VARRAY_UINT32|VMS_POINTER,                     \
447     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
448 }
449 
450 #define VMSTATE_VARRAY_UINT32_ALLOC(_field, _state, _field_num, _version, _info, _type) {\
451     .name       = (stringify(_field)),                               \
452     .version_id = (_version),                                        \
453     .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
454     .info       = &(_info),                                          \
455     .size       = sizeof(_type),                                     \
456     .flags      = VMS_VARRAY_UINT32|VMS_POINTER|VMS_ALLOC,           \
457     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
458 }
459 
460 #define VMSTATE_VARRAY_UINT16_ALLOC(_field, _state, _field_num, _version, _info, _type) {\
461     .name       = (stringify(_field)),                               \
462     .version_id = (_version),                                        \
463     .num_offset = vmstate_offset_value(_state, _field_num, uint16_t),\
464     .info       = &(_info),                                          \
465     .size       = sizeof(_type),                                     \
466     .flags      = VMS_VARRAY_UINT16 | VMS_POINTER | VMS_ALLOC,       \
467     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
468 }
469 
470 #define VMSTATE_VARRAY_UINT16_UNSAFE(_field, _state, _field_num, _version, _info, _type) {\
471     .name       = (stringify(_field)),                               \
472     .version_id = (_version),                                        \
473     .num_offset = vmstate_offset_value(_state, _field_num, uint16_t),\
474     .info       = &(_info),                                          \
475     .size       = sizeof(_type),                                     \
476     .flags      = VMS_VARRAY_UINT16,                                 \
477     .offset     = vmstate_offset_varray(_state, _field, _type),      \
478 }
479 
480 #define VMSTATE_VSTRUCT_TEST(_field, _state, _test, _version, _vmsd, _type, _struct_version) { \
481     .name         = (stringify(_field)),                             \
482     .version_id   = (_version),                                      \
483     .struct_version_id = (_struct_version),                          \
484     .field_exists = (_test),                                         \
485     .vmsd         = &(_vmsd),                                        \
486     .size         = sizeof(_type),                                   \
487     .flags        = VMS_VSTRUCT,                                     \
488     .offset       = vmstate_offset_value(_state, _field, _type),     \
489 }
490 
491 #define VMSTATE_STRUCT_TEST(_field, _state, _test, _version, _vmsd, _type) { \
492     .name         = (stringify(_field)),                             \
493     .version_id   = (_version),                                      \
494     .field_exists = (_test),                                         \
495     .vmsd         = &(_vmsd),                                        \
496     .size         = sizeof(_type),                                   \
497     .flags        = VMS_STRUCT,                                      \
498     .offset       = vmstate_offset_value(_state, _field, _type),     \
499 }
500 
501 #define VMSTATE_STRUCT_POINTER_V(_field, _state, _version, _vmsd, _type) { \
502     .name         = (stringify(_field)),                             \
503     .version_id   = (_version),                                        \
504     .vmsd         = &(_vmsd),                                        \
505     .size         = sizeof(_type *),                                 \
506     .flags        = VMS_STRUCT|VMS_POINTER,                          \
507     .offset       = vmstate_offset_pointer(_state, _field, _type),   \
508 }
509 
510 #define VMSTATE_STRUCT_POINTER_TEST_V(_field, _state, _test, _version, _vmsd, _type) { \
511     .name         = (stringify(_field)),                             \
512     .version_id   = (_version),                                        \
513     .field_exists = (_test),                                         \
514     .vmsd         = &(_vmsd),                                        \
515     .size         = sizeof(_type *),                                 \
516     .flags        = VMS_STRUCT|VMS_POINTER,                          \
517     .offset       = vmstate_offset_pointer(_state, _field, _type),   \
518 }
519 
520 #define VMSTATE_ARRAY_OF_POINTER(_field, _state, _num, _version, _info, _type) {\
521     .name       = (stringify(_field)),                               \
522     .version_id = (_version),                                        \
523     .num        = (_num),                                            \
524     .info       = &(_info),                                          \
525     .size       = sizeof(_type),                                     \
526     .flags      = VMS_ARRAY|VMS_ARRAY_OF_POINTER,                    \
527     .offset     = vmstate_offset_array(_state, _field, _type, _num), \
528 }
529 
530 #define VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(_f, _s, _n, _v, _vmsd, _type) { \
531     .name       = (stringify(_f)),                                   \
532     .version_id = (_v),                                              \
533     .num        = (_n),                                              \
534     .vmsd       = &(_vmsd),                                          \
535     .size       = sizeof(_type *),                                    \
536     .flags      = VMS_ARRAY|VMS_STRUCT|VMS_ARRAY_OF_POINTER,         \
537     .offset     = vmstate_offset_array(_s, _f, _type*, _n),          \
538 }
539 
540 #define VMSTATE_VARRAY_OF_POINTER_UINT32(_field, _state, _field_num, _version, _info, _type) { \
541     .name       = (stringify(_field)),                                    \
542     .version_id = (_version),                                             \
543     .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),     \
544     .info       = &(_info),                                               \
545     .size       = sizeof(_type),                                          \
546     .flags      = VMS_VARRAY_UINT32 | VMS_ARRAY_OF_POINTER | VMS_POINTER, \
547     .offset     = vmstate_offset_pointer(_state, _field, _type),          \
548 }
549 
550 #define VMSTATE_STRUCT_SUB_ARRAY(_field, _state, _start, _num, _version, _vmsd, _type) { \
551     .name       = (stringify(_field)),                                     \
552     .version_id = (_version),                                              \
553     .num        = (_num),                                                  \
554     .vmsd       = &(_vmsd),                                                \
555     .size       = sizeof(_type),                                           \
556     .flags      = VMS_STRUCT|VMS_ARRAY,                                    \
557     .offset     = vmstate_offset_sub_array(_state, _field, _type, _start), \
558 }
559 
560 #define VMSTATE_STRUCT_ARRAY_TEST(_field, _state, _num, _test, _version, _vmsd, _type) { \
561     .name         = (stringify(_field)),                             \
562     .num          = (_num),                                          \
563     .field_exists = (_test),                                         \
564     .version_id   = (_version),                                      \
565     .vmsd         = &(_vmsd),                                        \
566     .size         = sizeof(_type),                                   \
567     .flags        = VMS_STRUCT|VMS_ARRAY,                            \
568     .offset       = vmstate_offset_array(_state, _field, _type, _num),\
569 }
570 
571 #define VMSTATE_STRUCT_2DARRAY_TEST(_field, _state, _n1, _n2, _test, \
572                                     _version, _vmsd, _type) {        \
573     .name         = (stringify(_field)),                             \
574     .num          = (_n1) * (_n2),                                   \
575     .field_exists = (_test),                                         \
576     .version_id   = (_version),                                      \
577     .vmsd         = &(_vmsd),                                        \
578     .size         = sizeof(_type),                                   \
579     .flags        = VMS_STRUCT | VMS_ARRAY,                          \
580     .offset       = vmstate_offset_2darray(_state, _field, _type,    \
581                                            _n1, _n2),                \
582 }
583 
584 #define VMSTATE_STRUCT_VARRAY_UINT8(_field, _state, _field_num, _version, _vmsd, _type) { \
585     .name       = (stringify(_field)),                               \
586     .num_offset = vmstate_offset_value(_state, _field_num, uint8_t), \
587     .version_id = (_version),                                        \
588     .vmsd       = &(_vmsd),                                          \
589     .size       = sizeof(_type),                                     \
590     .flags      = VMS_STRUCT|VMS_VARRAY_UINT8,                       \
591     .offset     = vmstate_offset_varray(_state, _field, _type),      \
592 }
593 
594 /* a variable length array (i.e. _type *_field) but we know the
595  * length
596  */
597 #define VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(_field, _state, _num, _version, _vmsd, _type) { \
598     .name       = (stringify(_field)),                               \
599     .num          = (_num),                                          \
600     .version_id = (_version),                                        \
601     .vmsd       = &(_vmsd),                                          \
602     .size       = sizeof(_type),                                     \
603     .flags      = VMS_STRUCT|VMS_ARRAY|VMS_POINTER,                  \
604     .offset     = offsetof(_state, _field),                          \
605 }
606 
607 #define VMSTATE_STRUCT_VARRAY_POINTER_INT32(_field, _state, _field_num, _vmsd, _type) { \
608     .name       = (stringify(_field)),                               \
609     .version_id = 0,                                                 \
610     .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
611     .size       = sizeof(_type),                                     \
612     .vmsd       = &(_vmsd),                                          \
613     .flags      = VMS_POINTER | VMS_VARRAY_INT32 | VMS_STRUCT,       \
614     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
615 }
616 
617 #define VMSTATE_STRUCT_VARRAY_POINTER_UINT32(_field, _state, _field_num, _vmsd, _type) { \
618     .name       = (stringify(_field)),                               \
619     .version_id = 0,                                                 \
620     .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
621     .size       = sizeof(_type),                                     \
622     .vmsd       = &(_vmsd),                                          \
623     .flags      = VMS_POINTER | VMS_VARRAY_INT32 | VMS_STRUCT,       \
624     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
625 }
626 
627 #define VMSTATE_STRUCT_VARRAY_POINTER_UINT16(_field, _state, _field_num, _vmsd, _type) { \
628     .name       = (stringify(_field)),                               \
629     .version_id = 0,                                                 \
630     .num_offset = vmstate_offset_value(_state, _field_num, uint16_t),\
631     .size       = sizeof(_type),                                     \
632     .vmsd       = &(_vmsd),                                          \
633     .flags      = VMS_POINTER | VMS_VARRAY_UINT16 | VMS_STRUCT,      \
634     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
635 }
636 
637 #define VMSTATE_STRUCT_VARRAY_INT32(_field, _state, _field_num, _version, _vmsd, _type) { \
638     .name       = (stringify(_field)),                               \
639     .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
640     .version_id = (_version),                                        \
641     .vmsd       = &(_vmsd),                                          \
642     .size       = sizeof(_type),                                     \
643     .flags      = VMS_STRUCT|VMS_VARRAY_INT32,                       \
644     .offset     = vmstate_offset_varray(_state, _field, _type),      \
645 }
646 
647 #define VMSTATE_STRUCT_VARRAY_UINT32(_field, _state, _field_num, _version, _vmsd, _type) { \
648     .name       = (stringify(_field)),                               \
649     .num_offset = vmstate_offset_value(_state, _field_num, uint32_t), \
650     .version_id = (_version),                                        \
651     .vmsd       = &(_vmsd),                                          \
652     .size       = sizeof(_type),                                     \
653     .flags      = VMS_STRUCT|VMS_VARRAY_UINT32,                      \
654     .offset     = vmstate_offset_varray(_state, _field, _type),      \
655 }
656 
657 #define VMSTATE_STRUCT_VARRAY_ALLOC(_field, _state, _field_num, _version, _vmsd, _type) {\
658     .name       = (stringify(_field)),                               \
659     .version_id = (_version),                                        \
660     .vmsd       = &(_vmsd),                                          \
661     .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
662     .size       = sizeof(_type),                                     \
663     .flags      = VMS_STRUCT|VMS_VARRAY_INT32|VMS_ALLOC|VMS_POINTER, \
664     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
665 }
666 
667 #define VMSTATE_STATIC_BUFFER(_field, _state, _version, _test, _start, _size) { \
668     .name         = (stringify(_field)),                             \
669     .version_id   = (_version),                                      \
670     .field_exists = (_test),                                         \
671     .size         = (_size - _start),                                \
672     .info         = &vmstate_info_buffer,                            \
673     .flags        = VMS_BUFFER,                                      \
674     .offset       = vmstate_offset_buffer(_state, _field) + _start,  \
675 }
676 
677 #define VMSTATE_VBUFFER_MULTIPLY(_field, _state, _version, _test,    \
678                                  _field_size, _multiply) {           \
679     .name         = (stringify(_field)),                             \
680     .version_id   = (_version),                                      \
681     .field_exists = (_test),                                         \
682     .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
683     .size         = (_multiply),                                      \
684     .info         = &vmstate_info_buffer,                            \
685     .flags        = VMS_VBUFFER|VMS_POINTER|VMS_MULTIPLY,            \
686     .offset       = offsetof(_state, _field),                        \
687 }
688 
689 #define VMSTATE_VBUFFER(_field, _state, _version, _test, _field_size) { \
690     .name         = (stringify(_field)),                             \
691     .version_id   = (_version),                                      \
692     .field_exists = (_test),                                         \
693     .size_offset  = vmstate_offset_value(_state, _field_size, int32_t),\
694     .info         = &vmstate_info_buffer,                            \
695     .flags        = VMS_VBUFFER|VMS_POINTER,                         \
696     .offset       = offsetof(_state, _field),                        \
697 }
698 
699 #define VMSTATE_VBUFFER_UINT32(_field, _state, _version, _test, _field_size) { \
700     .name         = (stringify(_field)),                             \
701     .version_id   = (_version),                                      \
702     .field_exists = (_test),                                         \
703     .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
704     .info         = &vmstate_info_buffer,                            \
705     .flags        = VMS_VBUFFER|VMS_POINTER,                         \
706     .offset       = offsetof(_state, _field),                        \
707 }
708 
709 #define VMSTATE_VBUFFER_ALLOC_UINT32(_field, _state, _version,       \
710                                      _test, _field_size) {           \
711     .name         = (stringify(_field)),                             \
712     .version_id   = (_version),                                      \
713     .field_exists = (_test),                                         \
714     .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
715     .info         = &vmstate_info_buffer,                            \
716     .flags        = VMS_VBUFFER|VMS_POINTER|VMS_ALLOC,               \
717     .offset       = offsetof(_state, _field),                        \
718 }
719 
720 #define VMSTATE_BUFFER_UNSAFE_INFO_TEST(_field, _state, _test, _version, _info, _size) { \
721     .name       = (stringify(_field)),                               \
722     .version_id = (_version),                                        \
723     .field_exists = (_test),                                         \
724     .size       = (_size),                                           \
725     .info       = &(_info),                                          \
726     .flags      = VMS_BUFFER,                                        \
727     .offset     = offsetof(_state, _field),                          \
728 }
729 
730 #define VMSTATE_BUFFER_POINTER_UNSAFE(_field, _state, _version, _size) { \
731     .name       = (stringify(_field)),                               \
732     .version_id = (_version),                                        \
733     .size       = (_size),                                           \
734     .info       = &vmstate_info_buffer,                              \
735     .flags      = VMS_BUFFER | VMS_POINTER,                          \
736     .offset     = offsetof(_state, _field),                          \
737 }
738 
739 /* Allocate a temporary of type 'tmp_type', set tmp->parent to _state
740  * and execute the vmsd on the temporary.  Note that we're working with
741  * the whole of _state here, not a field within it.
742  * We compile time check that:
743  *    That _tmp_type contains a 'parent' member that's a pointer to the
744  *        '_state' type
745  *    That the pointer is right at the start of _tmp_type.
746  */
747 #define VMSTATE_WITH_TMP_TEST(_state, _test, _tmp_type, _vmsd) {     \
748     .name         = "tmp",                                           \
749     .field_exists = (_test),                                         \
750     .size         = sizeof(_tmp_type) +                              \
751                     QEMU_BUILD_BUG_ON_ZERO(offsetof(_tmp_type, parent) != 0) + \
752                     type_check_pointer(_state,                       \
753                         typeof_field(_tmp_type, parent)),            \
754     .vmsd         = &(_vmsd),                                        \
755     .info         = &vmstate_info_tmp,                               \
756 }
757 
758 #define VMSTATE_WITH_TMP(_state, _tmp_type, _vmsd) \
759     VMSTATE_WITH_TMP_TEST(_state, NULL, _tmp_type, _vmsd)
760 
761 #define VMSTATE_UNUSED_BUFFER(_test, _version, _size) {              \
762     .name         = "unused",                                        \
763     .field_exists = (_test),                                         \
764     .version_id   = (_version),                                      \
765     .size         = (_size),                                         \
766     .info         = &vmstate_info_unused_buffer,                     \
767     .flags        = VMS_BUFFER,                                      \
768 }
769 
770 /* Discard size * field_num bytes, where field_num is a uint32 member */
771 #define VMSTATE_UNUSED_VARRAY_UINT32(_state, _test, _version, _field_num, _size) {\
772     .name         = "unused",                                        \
773     .field_exists = (_test),                                         \
774     .num_offset   = vmstate_offset_value(_state, _field_num, uint32_t),\
775     .version_id   = (_version),                                      \
776     .size         = (_size),                                         \
777     .info         = &vmstate_info_unused_buffer,                     \
778     .flags        = VMS_VARRAY_UINT32 | VMS_BUFFER,                  \
779 }
780 
781 /* _field_size should be a int32_t field in the _state struct giving the
782  * size of the bitmap _field in bits.
783  */
784 #define VMSTATE_BITMAP_TEST(_field, _state, _test, _version, _field_size) { \
785     .name         = (stringify(_field)),                             \
786     .field_exists = (_test),                                         \
787     .version_id   = (_version),                                      \
788     .size_offset  = vmstate_offset_value(_state, _field_size, int32_t),\
789     .info         = &vmstate_info_bitmap,                            \
790     .flags        = VMS_VBUFFER|VMS_POINTER,                         \
791     .offset       = offsetof(_state, _field),                        \
792 }
793 
794 #define VMSTATE_BITMAP(_field, _state, _version, _field_size) \
795     VMSTATE_BITMAP_TEST(_field, _state, NULL, _version, _field_size)
796 
797 /* For migrating a QTAILQ.
798  * Target QTAILQ needs be properly initialized.
799  * _type: type of QTAILQ element
800  * _next: name of QTAILQ entry field in QTAILQ element
801  * _vmsd: VMSD for QTAILQ element
802  * size: size of QTAILQ element
803  * start: offset of QTAILQ entry in QTAILQ element
804  */
805 #define VMSTATE_QTAILQ_V(_field, _state, _version, _vmsd, _type, _next)  \
806 {                                                                        \
807     .name         = (stringify(_field)),                                 \
808     .version_id   = (_version),                                          \
809     .vmsd         = &(_vmsd),                                            \
810     .size         = sizeof(_type),                                       \
811     .info         = &vmstate_info_qtailq,                                \
812     .offset       = offsetof(_state, _field),                            \
813     .start        = offsetof(_type, _next),                              \
814 }
815 
816 /*
817  * For migrating a GTree whose key is a pointer to _key_type and the
818  * value, a pointer to _val_type
819  * The target tree must have been properly initialized
820  * _vmsd: Start address of the 2 element array containing the data vmsd
821  *        and the key vmsd, in that order
822  * _key_type: type of the key
823  * _val_type: type of the value
824  */
825 #define VMSTATE_GTREE_V(_field, _state, _version, _vmsd,                       \
826                         _key_type, _val_type)                                  \
827 {                                                                              \
828     .name         = (stringify(_field)),                                       \
829     .version_id   = (_version),                                                \
830     .vmsd         = (_vmsd),                                                   \
831     .info         = &vmstate_info_gtree,                                       \
832     .start        = sizeof(_key_type),                                         \
833     .size         = sizeof(_val_type),                                         \
834     .offset       = offsetof(_state, _field),                                  \
835 }
836 
837 /*
838  * For migrating a GTree with direct key and the value a pointer
839  * to _val_type
840  * The target tree must have been properly initialized
841  * _vmsd: data vmsd
842  * _val_type: type of the value
843  */
844 #define VMSTATE_GTREE_DIRECT_KEY_V(_field, _state, _version, _vmsd, _val_type) \
845 {                                                                              \
846     .name         = (stringify(_field)),                                       \
847     .version_id   = (_version),                                                \
848     .vmsd         = (_vmsd),                                                   \
849     .info         = &vmstate_info_gtree,                                       \
850     .start        = 0,                                                         \
851     .size         = sizeof(_val_type),                                         \
852     .offset       = offsetof(_state, _field),                                  \
853 }
854 
855 /*
856  * For migrating a QLIST
857  * Target QLIST needs be properly initialized.
858  * _type: type of QLIST element
859  * _next: name of QLIST_ENTRY entry field in QLIST element
860  * _vmsd: VMSD for QLIST element
861  * size: size of QLIST element
862  * start: offset of QLIST_ENTRY in QTAILQ element
863  */
864 #define VMSTATE_QLIST_V(_field, _state, _version, _vmsd, _type, _next)  \
865 {                                                                        \
866     .name         = (stringify(_field)),                                 \
867     .version_id   = (_version),                                          \
868     .vmsd         = &(_vmsd),                                            \
869     .size         = sizeof(_type),                                       \
870     .info         = &vmstate_info_qlist,                                 \
871     .offset       = offsetof(_state, _field),                            \
872     .start        = offsetof(_type, _next),                              \
873 }
874 
875 /* _f : field name
876    _f_n : num of elements field_name
877    _n : num of elements
878    _s : struct state name
879    _v : version
880 */
881 
882 #define VMSTATE_SINGLE(_field, _state, _version, _info, _type)        \
883     VMSTATE_SINGLE_TEST(_field, _state, NULL, _version, _info, _type)
884 
885 #define VMSTATE_VSTRUCT(_field, _state, _vmsd, _type, _struct_version)\
886     VMSTATE_VSTRUCT_TEST(_field, _state, NULL, 0, _vmsd, _type, _struct_version)
887 
888 #define VMSTATE_VSTRUCT_V(_field, _state, _version, _vmsd, _type, _struct_version) \
889     VMSTATE_VSTRUCT_TEST(_field, _state, NULL, _version, _vmsd, _type, \
890                          _struct_version)
891 
892 #define VMSTATE_STRUCT(_field, _state, _version, _vmsd, _type)        \
893     VMSTATE_STRUCT_TEST(_field, _state, NULL, _version, _vmsd, _type)
894 
895 #define VMSTATE_STRUCT_POINTER(_field, _state, _vmsd, _type)          \
896     VMSTATE_STRUCT_POINTER_V(_field, _state, 0, _vmsd, _type)
897 
898 #define VMSTATE_STRUCT_POINTER_TEST(_field, _state, _test, _vmsd, _type)     \
899     VMSTATE_STRUCT_POINTER_TEST_V(_field, _state, _test, 0, _vmsd, _type)
900 
901 #define VMSTATE_STRUCT_ARRAY(_field, _state, _num, _version, _vmsd, _type) \
902     VMSTATE_STRUCT_ARRAY_TEST(_field, _state, _num, NULL, _version,   \
903             _vmsd, _type)
904 
905 #define VMSTATE_STRUCT_2DARRAY(_field, _state, _n1, _n2, _version,    \
906             _vmsd, _type)                                             \
907     VMSTATE_STRUCT_2DARRAY_TEST(_field, _state, _n1, _n2, NULL,       \
908             _version, _vmsd, _type)
909 
910 #define VMSTATE_BUFFER_UNSAFE_INFO(_field, _state, _version, _info, _size) \
911     VMSTATE_BUFFER_UNSAFE_INFO_TEST(_field, _state, NULL, _version, _info, \
912             _size)
913 
914 #define VMSTATE_BOOL_V(_f, _s, _v)                                    \
915     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_bool, bool)
916 
917 #define VMSTATE_INT8_V(_f, _s, _v)                                    \
918     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int8, int8_t)
919 #define VMSTATE_INT16_V(_f, _s, _v)                                   \
920     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int16, int16_t)
921 #define VMSTATE_INT32_V(_f, _s, _v)                                   \
922     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int32, int32_t)
923 #define VMSTATE_INT64_V(_f, _s, _v)                                   \
924     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int64, int64_t)
925 
926 #define VMSTATE_UINT8_V(_f, _s, _v)                                   \
927     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint8, uint8_t)
928 #define VMSTATE_UINT16_V(_f, _s, _v)                                  \
929     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint16, uint16_t)
930 #define VMSTATE_UINT32_V(_f, _s, _v)                                  \
931     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint32, uint32_t)
932 #define VMSTATE_UINT64_V(_f, _s, _v)                                  \
933     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint64, uint64_t)
934 
935 #define VMSTATE_FD_V(_f, _s, _v)                                  \
936     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_fd, int32_t)
937 
938 #ifdef CONFIG_LINUX
939 
940 #define VMSTATE_U8_V(_f, _s, _v)                                   \
941     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint8, __u8)
942 #define VMSTATE_U16_V(_f, _s, _v)                                  \
943     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint16, __u16)
944 #define VMSTATE_U32_V(_f, _s, _v)                                  \
945     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint32, __u32)
946 #define VMSTATE_U64_V(_f, _s, _v)                                  \
947     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint64, __u64)
948 
949 #endif
950 
951 #define VMSTATE_BOOL(_f, _s)                                          \
952     VMSTATE_BOOL_V(_f, _s, 0)
953 
954 #define VMSTATE_INT8(_f, _s)                                          \
955     VMSTATE_INT8_V(_f, _s, 0)
956 #define VMSTATE_INT16(_f, _s)                                         \
957     VMSTATE_INT16_V(_f, _s, 0)
958 #define VMSTATE_INT32(_f, _s)                                         \
959     VMSTATE_INT32_V(_f, _s, 0)
960 #define VMSTATE_INT64(_f, _s)                                         \
961     VMSTATE_INT64_V(_f, _s, 0)
962 
963 #define VMSTATE_UINT8(_f, _s)                                         \
964     VMSTATE_UINT8_V(_f, _s, 0)
965 #define VMSTATE_UINT16(_f, _s)                                        \
966     VMSTATE_UINT16_V(_f, _s, 0)
967 #define VMSTATE_UINT32(_f, _s)                                        \
968     VMSTATE_UINT32_V(_f, _s, 0)
969 #define VMSTATE_UINT64(_f, _s)                                        \
970     VMSTATE_UINT64_V(_f, _s, 0)
971 
972 #define VMSTATE_FD(_f, _s)                                            \
973     VMSTATE_FD_V(_f, _s, 0)
974 
975 #ifdef CONFIG_LINUX
976 
977 #define VMSTATE_U8(_f, _s)                                         \
978     VMSTATE_U8_V(_f, _s, 0)
979 #define VMSTATE_U16(_f, _s)                                        \
980     VMSTATE_U16_V(_f, _s, 0)
981 #define VMSTATE_U32(_f, _s)                                        \
982     VMSTATE_U32_V(_f, _s, 0)
983 #define VMSTATE_U64(_f, _s)                                        \
984     VMSTATE_U64_V(_f, _s, 0)
985 
986 #endif
987 
988 #define VMSTATE_UINT8_EQUAL(_f, _s, _err_hint)                        \
989     VMSTATE_SINGLE_FULL(_f, _s, 0, 0,                                 \
990                         vmstate_info_uint8_equal, uint8_t, _err_hint)
991 
992 #define VMSTATE_UINT16_EQUAL(_f, _s, _err_hint)                       \
993     VMSTATE_SINGLE_FULL(_f, _s, 0, 0,                                 \
994                         vmstate_info_uint16_equal, uint16_t, _err_hint)
995 
996 #define VMSTATE_UINT16_EQUAL_V(_f, _s, _v, _err_hint)                 \
997     VMSTATE_SINGLE_FULL(_f, _s, 0,  _v,                               \
998                         vmstate_info_uint16_equal, uint16_t, _err_hint)
999 
1000 #define VMSTATE_INT32_EQUAL(_f, _s, _err_hint)                        \
1001     VMSTATE_SINGLE_FULL(_f, _s, 0, 0,                                 \
1002                         vmstate_info_int32_equal, int32_t, _err_hint)
1003 
1004 #define VMSTATE_UINT32_EQUAL_V(_f, _s, _v, _err_hint)                 \
1005     VMSTATE_SINGLE_FULL(_f, _s, 0,  _v,                               \
1006                         vmstate_info_uint32_equal, uint32_t, _err_hint)
1007 
1008 #define VMSTATE_UINT32_EQUAL(_f, _s, _err_hint)                       \
1009     VMSTATE_UINT32_EQUAL_V(_f, _s, 0, _err_hint)
1010 
1011 #define VMSTATE_UINT64_EQUAL_V(_f, _s, _v, _err_hint)                 \
1012     VMSTATE_SINGLE_FULL(_f, _s, 0,  _v,                               \
1013                         vmstate_info_uint64_equal, uint64_t, _err_hint)
1014 
1015 #define VMSTATE_UINT64_EQUAL(_f, _s, _err_hint)                       \
1016     VMSTATE_UINT64_EQUAL_V(_f, _s, 0, _err_hint)
1017 
1018 #define VMSTATE_INT32_POSITIVE_LE(_f, _s)                             \
1019     VMSTATE_SINGLE(_f, _s, 0, vmstate_info_int32_le, int32_t)
1020 
1021 #define VMSTATE_BOOL_TEST(_f, _s, _t)                               \
1022     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_bool, bool)
1023 
1024 #define VMSTATE_INT8_TEST(_f, _s, _t)                               \
1025     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_int8, int8_t)
1026 
1027 #define VMSTATE_INT16_TEST(_f, _s, _t)                               \
1028     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_int16, int16_t)
1029 
1030 #define VMSTATE_INT32_TEST(_f, _s, _t)                                  \
1031     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_int32, int32_t)
1032 
1033 #define VMSTATE_INT64_TEST(_f, _s, _t)                                  \
1034     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_int64, int64_t)
1035 
1036 #define VMSTATE_UINT8_TEST(_f, _s, _t)                               \
1037     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint8, uint8_t)
1038 
1039 #define VMSTATE_UINT16_TEST(_f, _s, _t)                               \
1040     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint16, uint16_t)
1041 
1042 #define VMSTATE_UINT32_TEST(_f, _s, _t)                                  \
1043     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint32, uint32_t)
1044 
1045 #define VMSTATE_UINT64_TEST(_f, _s, _t)                                  \
1046     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint64, uint64_t)
1047 
1048 #define VMSTATE_FD_TEST(_f, _s, _t)                                            \
1049     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_fd, int32_t)
1050 
1051 #define VMSTATE_TIMER_PTR_TEST(_f, _s, _test)                             \
1052     VMSTATE_POINTER_TEST(_f, _s, _test, vmstate_info_timer, QEMUTimer *)
1053 
1054 #define VMSTATE_TIMER_PTR_V(_f, _s, _v)                                   \
1055     VMSTATE_POINTER(_f, _s, _v, vmstate_info_timer, QEMUTimer *)
1056 
1057 #define VMSTATE_TIMER_PTR(_f, _s)                                         \
1058     VMSTATE_TIMER_PTR_V(_f, _s, 0)
1059 
1060 #define VMSTATE_TIMER_PTR_ARRAY(_f, _s, _n)                              \
1061     VMSTATE_ARRAY_OF_POINTER(_f, _s, _n, 0, vmstate_info_timer, QEMUTimer *)
1062 
1063 #define VMSTATE_TIMER_TEST(_f, _s, _test)                             \
1064     VMSTATE_SINGLE_TEST(_f, _s, _test, 0, vmstate_info_timer, QEMUTimer)
1065 
1066 #define VMSTATE_TIMER_V(_f, _s, _v)                                   \
1067     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_timer, QEMUTimer)
1068 
1069 #define VMSTATE_TIMER(_f, _s)                                         \
1070     VMSTATE_TIMER_V(_f, _s, 0)
1071 
1072 #define VMSTATE_TIMER_ARRAY(_f, _s, _n)                              \
1073     VMSTATE_ARRAY(_f, _s, _n, 0, vmstate_info_timer, QEMUTimer)
1074 
1075 #define VMSTATE_BOOL_ARRAY_V(_f, _s, _n, _v)                         \
1076     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_bool, bool)
1077 
1078 #define VMSTATE_BOOL_ARRAY(_f, _s, _n)                               \
1079     VMSTATE_BOOL_ARRAY_V(_f, _s, _n, 0)
1080 
1081 #define VMSTATE_BOOL_SUB_ARRAY(_f, _s, _start, _num)                \
1082     VMSTATE_SUB_ARRAY(_f, _s, _start, _num, 0, vmstate_info_bool, bool)
1083 
1084 #define VMSTATE_UINT16_ARRAY_V(_f, _s, _n, _v)                         \
1085     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint16, uint16_t)
1086 
1087 #define VMSTATE_UINT16_2DARRAY_V(_f, _s, _n1, _n2, _v)                \
1088     VMSTATE_2DARRAY(_f, _s, _n1, _n2, _v, vmstate_info_uint16, uint16_t)
1089 
1090 #define VMSTATE_UINT16_ARRAY(_f, _s, _n)                               \
1091     VMSTATE_UINT16_ARRAY_V(_f, _s, _n, 0)
1092 
1093 #define VMSTATE_UINT16_SUB_ARRAY(_f, _s, _start, _num)                \
1094     VMSTATE_SUB_ARRAY(_f, _s, _start, _num, 0, vmstate_info_uint16, uint16_t)
1095 
1096 #define VMSTATE_UINT16_2DARRAY(_f, _s, _n1, _n2)                      \
1097     VMSTATE_UINT16_2DARRAY_V(_f, _s, _n1, _n2, 0)
1098 
1099 #define VMSTATE_UINT8_2DARRAY_V(_f, _s, _n1, _n2, _v)                 \
1100     VMSTATE_2DARRAY(_f, _s, _n1, _n2, _v, vmstate_info_uint8, uint8_t)
1101 
1102 #define VMSTATE_UINT8_ARRAY_V(_f, _s, _n, _v)                         \
1103     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint8, uint8_t)
1104 
1105 #define VMSTATE_UINT8_ARRAY(_f, _s, _n)                               \
1106     VMSTATE_UINT8_ARRAY_V(_f, _s, _n, 0)
1107 
1108 #define VMSTATE_UINT8_SUB_ARRAY(_f, _s, _start, _num)                \
1109     VMSTATE_SUB_ARRAY(_f, _s, _start, _num, 0, vmstate_info_uint8, uint8_t)
1110 
1111 #define VMSTATE_UINT8_2DARRAY(_f, _s, _n1, _n2)                       \
1112     VMSTATE_UINT8_2DARRAY_V(_f, _s, _n1, _n2, 0)
1113 
1114 #define VMSTATE_UINT32_ARRAY_V(_f, _s, _n, _v)                        \
1115     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint32, uint32_t)
1116 
1117 #define VMSTATE_UINT32_2DARRAY_V(_f, _s, _n1, _n2, _v)                \
1118     VMSTATE_2DARRAY(_f, _s, _n1, _n2, _v, vmstate_info_uint32, uint32_t)
1119 
1120 #define VMSTATE_UINT32_ARRAY(_f, _s, _n)                              \
1121     VMSTATE_UINT32_ARRAY_V(_f, _s, _n, 0)
1122 
1123 #define VMSTATE_UINT32_SUB_ARRAY(_f, _s, _start, _num)                \
1124     VMSTATE_SUB_ARRAY(_f, _s, _start, _num, 0, vmstate_info_uint32, uint32_t)
1125 
1126 #define VMSTATE_UINT32_2DARRAY(_f, _s, _n1, _n2)                      \
1127     VMSTATE_UINT32_2DARRAY_V(_f, _s, _n1, _n2, 0)
1128 
1129 #define VMSTATE_UINT64_ARRAY_V(_f, _s, _n, _v)                        \
1130     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint64, uint64_t)
1131 
1132 #define VMSTATE_UINT64_ARRAY(_f, _s, _n)                              \
1133     VMSTATE_UINT64_ARRAY_V(_f, _s, _n, 0)
1134 
1135 #define VMSTATE_UINT64_SUB_ARRAY(_f, _s, _start, _num)                \
1136     VMSTATE_SUB_ARRAY(_f, _s, _start, _num, 0, vmstate_info_uint64, uint64_t)
1137 
1138 #define VMSTATE_UINT64_2DARRAY(_f, _s, _n1, _n2)                      \
1139     VMSTATE_UINT64_2DARRAY_V(_f, _s, _n1, _n2, 0)
1140 
1141 #define VMSTATE_UINT64_2DARRAY_V(_f, _s, _n1, _n2, _v)                 \
1142     VMSTATE_2DARRAY(_f, _s, _n1, _n2, _v, vmstate_info_uint64, uint64_t)
1143 
1144 #define VMSTATE_INT16_ARRAY_V(_f, _s, _n, _v)                         \
1145     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_int16, int16_t)
1146 
1147 #define VMSTATE_INT16_ARRAY(_f, _s, _n)                               \
1148     VMSTATE_INT16_ARRAY_V(_f, _s, _n, 0)
1149 
1150 #define VMSTATE_INT32_ARRAY_V(_f, _s, _n, _v)                         \
1151     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_int32, int32_t)
1152 
1153 #define VMSTATE_INT32_ARRAY(_f, _s, _n)                               \
1154     VMSTATE_INT32_ARRAY_V(_f, _s, _n, 0)
1155 
1156 #define VMSTATE_INT64_ARRAY_V(_f, _s, _n, _v)                         \
1157     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_int64, int64_t)
1158 
1159 #define VMSTATE_INT64_ARRAY(_f, _s, _n)                               \
1160     VMSTATE_INT64_ARRAY_V(_f, _s, _n, 0)
1161 
1162 #define VMSTATE_CPUDOUBLE_ARRAY_V(_f, _s, _n, _v)                     \
1163     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_cpudouble, CPU_DoubleU)
1164 
1165 #define VMSTATE_CPUDOUBLE_ARRAY(_f, _s, _n)                           \
1166     VMSTATE_CPUDOUBLE_ARRAY_V(_f, _s, _n, 0)
1167 
1168 #define VMSTATE_BUFFER_V(_f, _s, _v)                                  \
1169     VMSTATE_STATIC_BUFFER(_f, _s, _v, NULL, 0, sizeof(typeof_field(_s, _f)))
1170 
1171 #define VMSTATE_BUFFER(_f, _s)                                        \
1172     VMSTATE_BUFFER_V(_f, _s, 0)
1173 
1174 #define VMSTATE_PARTIAL_BUFFER(_f, _s, _size)                         \
1175     VMSTATE_STATIC_BUFFER(_f, _s, 0, NULL, 0, _size)
1176 
1177 #define VMSTATE_BUFFER_START_MIDDLE_V(_f, _s, _start, _v) \
1178     VMSTATE_STATIC_BUFFER(_f, _s, _v, NULL, _start, sizeof(typeof_field(_s, _f)))
1179 
1180 #define VMSTATE_BUFFER_START_MIDDLE(_f, _s, _start) \
1181     VMSTATE_BUFFER_START_MIDDLE_V(_f, _s, _start, 0)
1182 
1183 #define VMSTATE_PARTIAL_VBUFFER(_f, _s, _size)                        \
1184     VMSTATE_VBUFFER(_f, _s, 0, NULL, _size)
1185 
1186 #define VMSTATE_PARTIAL_VBUFFER_UINT32(_f, _s, _size)                        \
1187     VMSTATE_VBUFFER_UINT32(_f, _s, 0, NULL, _size)
1188 
1189 #define VMSTATE_BUFFER_TEST(_f, _s, _test)                            \
1190     VMSTATE_STATIC_BUFFER(_f, _s, 0, _test, 0, sizeof(typeof_field(_s, _f)))
1191 
1192 #define VMSTATE_BUFFER_UNSAFE(_field, _state, _version, _size)        \
1193     VMSTATE_BUFFER_UNSAFE_INFO(_field, _state, _version, vmstate_info_buffer, _size)
1194 
1195 /*
1196  * These VMSTATE_UNUSED*() macros can be used to fill in the holes
1197  * when some of the vmstate fields are obsolete to be compatible with
1198  * migrations between new/old binaries.
1199  *
1200  * CAUTION: when using any of the VMSTATE_UNUSED*() macros please be
1201  * sure that the size passed in is the size that was actually *sent*
1202  * rather than the size of the *structure*.  One example is the
1203  * boolean type - the size of the structure can vary depending on the
1204  * definition of boolean, however the size we actually sent is always
1205  * 1 byte (please refer to implementation of VMSTATE_BOOL_V and
1206  * vmstate_info_bool).  So here we should always pass in size==1
1207  * rather than size==sizeof(bool).
1208  */
1209 #define VMSTATE_UNUSED_V(_v, _size)                                   \
1210     VMSTATE_UNUSED_BUFFER(NULL, _v, _size)
1211 
1212 #define VMSTATE_UNUSED(_size)                                         \
1213     VMSTATE_UNUSED_V(0, _size)
1214 
1215 #define VMSTATE_UNUSED_TEST(_test, _size)                             \
1216     VMSTATE_UNUSED_BUFFER(_test, 0, _size)
1217 
1218 #define VMSTATE_END_OF_LIST()                                         \
1219     {                     \
1220         .flags = VMS_END, \
1221     }
1222 
1223 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1224                        void *opaque, int version_id, Error **errp);
1225 int vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
1226                        void *opaque, JSONWriter *vmdesc, Error **errp);
1227 int vmstate_save_state_v(QEMUFile *f, const VMStateDescription *vmsd,
1228                          void *opaque, JSONWriter *vmdesc,
1229                          int version_id, Error **errp);
1230 
1231 bool vmstate_section_needed(const VMStateDescription *vmsd, void *opaque);
1232 
1233 #define  VMSTATE_INSTANCE_ID_ANY  -1
1234 
1235 /* Returns: 0 on success, -1 on failure */
1236 int vmstate_register_with_alias_id(VMStateIf *obj, uint32_t instance_id,
1237                                    const VMStateDescription *vmsd,
1238                                    void *base, int alias_id,
1239                                    int required_for_version,
1240                                    Error **errp);
1241 
1242 /**
1243  * vmstate_register() - legacy function to register state
1244  * serialisation description
1245  *
1246  * New code shouldn't be using this function as QOM-ified devices have
1247  * dc->vmsd to store the serialisation description.
1248  *
1249  * Returns: 0 on success, -1 on failure
1250  */
1251 static inline int vmstate_register(VMStateIf *obj, int instance_id,
1252                                    const VMStateDescription *vmsd,
1253                                    void *opaque)
1254 {
1255     return vmstate_register_with_alias_id(obj, instance_id, vmsd,
1256                                           opaque, -1, 0, NULL);
1257 }
1258 
1259 /**
1260  * vmstate_replace_hack_for_ppc() - ppc used to abuse vmstate_register
1261  *
1262  * Don't even think about using this function in new code.
1263  *
1264  * Returns: 0 on success, -1 on failure
1265  */
1266 int vmstate_replace_hack_for_ppc(VMStateIf *obj, int instance_id,
1267                                  const VMStateDescription *vmsd,
1268                                  void *opaque);
1269 
1270 /**
1271  * vmstate_register_any() - legacy function to register state
1272  * serialisation description and let the function choose the id
1273  *
1274  * New code shouldn't be using this function as QOM-ified devices have
1275  * dc->vmsd to store the serialisation description.
1276  *
1277  * Returns: 0 on success, -1 on failure
1278  */
1279 static inline int vmstate_register_any(VMStateIf *obj,
1280                                        const VMStateDescription *vmsd,
1281                                        void *opaque)
1282 {
1283     return vmstate_register_with_alias_id(obj, VMSTATE_INSTANCE_ID_ANY, vmsd,
1284                                           opaque, -1, 0, NULL);
1285 }
1286 
1287 void vmstate_unregister(VMStateIf *obj, const VMStateDescription *vmsd,
1288                         void *opaque);
1289 
1290 void vmstate_register_ram(struct MemoryRegion *memory, DeviceState *dev);
1291 void vmstate_unregister_ram(struct MemoryRegion *memory, DeviceState *dev);
1292 void vmstate_register_ram_global(struct MemoryRegion *memory);
1293 
1294 bool vmstate_check_only_migratable(const VMStateDescription *vmsd);
1295 
1296 #endif
1297