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