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