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