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