xref: /qemu/include/migration/vmstate.h (revision 513823e7521a09ed7ad1e32e6454bac3b2cbf52d)
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 extern const VMStateInfo vmstate_info_fd;
234 
235 /** Put this in the stream when migrating a null pointer.*/
236 #define VMS_NULLPTR_MARKER (0x30U) /* '0' */
237 extern const VMStateInfo vmstate_info_nullptr;
238 
239 extern const VMStateInfo vmstate_info_cpudouble;
240 
241 extern const VMStateInfo vmstate_info_timer;
242 extern const VMStateInfo vmstate_info_buffer;
243 extern const VMStateInfo vmstate_info_unused_buffer;
244 extern const VMStateInfo vmstate_info_tmp;
245 extern const VMStateInfo vmstate_info_bitmap;
246 extern const VMStateInfo vmstate_info_qtailq;
247 extern const VMStateInfo vmstate_info_gtree;
248 extern const VMStateInfo vmstate_info_qlist;
249 
250 #define type_check_2darray(t1,t2,n,m) ((t1(*)[n][m])0 - (t2*)0)
251 /*
252  * Check that type t2 is an array of type t1 of size n,
253  * e.g. if t1 is 'foo' and n is 32 then t2 must be 'foo[32]'
254  */
255 #define type_check_array(t1,t2,n) ((t1(*)[n])0 - (t2*)0)
256 #define type_check_pointer(t1,t2) ((t1**)0 - (t2*)0)
257 /*
258  * type of element 0 of the specified (array) field of the type.
259  * Note that if the field is a pointer then this will return the
260  * pointed-to type rather than complaining.
261  */
262 #define typeof_elt_of_field(type, field) typeof(((type *)0)->field[0])
263 /* Check that field f in struct type t2 is an array of t1, of any size */
264 #define type_check_varray(t1, t2, f)                                 \
265     (type_check(t1, typeof_elt_of_field(t2, f))                      \
266      + QEMU_BUILD_BUG_ON_ZERO(!QEMU_IS_ARRAY(((t2 *)0)->f)))
267 
268 #define vmstate_offset_value(_state, _field, _type)                  \
269     (offsetof(_state, _field) +                                      \
270      type_check(_type, typeof_field(_state, _field)))
271 
272 #define vmstate_offset_pointer(_state, _field, _type)                \
273     (offsetof(_state, _field) +                                      \
274      type_check_pointer(_type, typeof_field(_state, _field)))
275 
276 #define vmstate_offset_array(_state, _field, _type, _num)            \
277     (offsetof(_state, _field) +                                      \
278      type_check_array(_type, typeof_field(_state, _field), _num))
279 
280 #define vmstate_offset_2darray(_state, _field, _type, _n1, _n2)      \
281     (offsetof(_state, _field) +                                      \
282      type_check_2darray(_type, typeof_field(_state, _field), _n1, _n2))
283 
284 #define vmstate_offset_sub_array(_state, _field, _type, _start)      \
285     vmstate_offset_value(_state, _field[_start], _type)
286 
287 #define vmstate_offset_buffer(_state, _field)                        \
288     vmstate_offset_array(_state, _field, uint8_t,                    \
289                          sizeof(typeof_field(_state, _field)))
290 
291 #define vmstate_offset_varray(_state, _field, _type)                 \
292     (offsetof(_state, _field) +                                      \
293      type_check_varray(_type, _state, _field))
294 
295 /* In the macros below, if there is a _version, that means the macro's
296  * field will be processed only if the version being received is >=
297  * the _version specified.  In general, if you add a new field, you
298  * would increment the structure's version and put that version
299  * number into the new field so it would only be processed with the
300  * new version.
301  *
302  * In particular, for VMSTATE_STRUCT() and friends the _version does
303  * *NOT* pick the version of the sub-structure.  It works just as
304  * specified above.  The version of the top-level structure received
305  * is passed down to all sub-structures.  This means that the
306  * sub-structures must have version that are compatible with all the
307  * structures that use them.
308  *
309  * If you want to specify the version of the sub-structure, use
310  * VMSTATE_VSTRUCT(), which allows the specific sub-structure version
311  * to be directly specified.
312  */
313 
314 #define VMSTATE_SINGLE_TEST(_field, _state, _test, _version, _info, _type) { \
315     .name         = (stringify(_field)),                             \
316     .version_id   = (_version),                                      \
317     .field_exists = (_test),                                         \
318     .size         = sizeof(_type),                                   \
319     .info         = &(_info),                                        \
320     .flags        = VMS_SINGLE,                                      \
321     .offset       = vmstate_offset_value(_state, _field, _type),     \
322 }
323 
324 #define VMSTATE_SINGLE_FULL(_field, _state, _test, _version, _info,  \
325                             _type, _err_hint) {                      \
326     .name         = (stringify(_field)),                             \
327     .err_hint     = (_err_hint),                                     \
328     .version_id   = (_version),                                      \
329     .field_exists = (_test),                                         \
330     .size         = sizeof(_type),                                   \
331     .info         = &(_info),                                        \
332     .flags        = VMS_SINGLE,                                      \
333     .offset       = vmstate_offset_value(_state, _field, _type),     \
334 }
335 
336 /* Validate state using a boolean predicate. */
337 #define VMSTATE_VALIDATE(_name, _test) { \
338     .name         = (_name),                                         \
339     .field_exists = (_test),                                         \
340     .flags        = VMS_ARRAY | VMS_MUST_EXIST,                      \
341     .num          = 0, /* 0 elements: no data, only run _test */     \
342 }
343 
344 #define VMSTATE_POINTER(_field, _state, _version, _info, _type) {    \
345     .name       = (stringify(_field)),                               \
346     .version_id = (_version),                                        \
347     .info       = &(_info),                                          \
348     .size       = sizeof(_type),                                     \
349     .flags      = VMS_SINGLE|VMS_POINTER,                            \
350     .offset     = vmstate_offset_value(_state, _field, _type),       \
351 }
352 
353 #define VMSTATE_POINTER_TEST(_field, _state, _test, _info, _type) {  \
354     .name       = (stringify(_field)),                               \
355     .info       = &(_info),                                          \
356     .field_exists = (_test),                                         \
357     .size       = sizeof(_type),                                     \
358     .flags      = VMS_SINGLE|VMS_POINTER,                            \
359     .offset     = vmstate_offset_value(_state, _field, _type),       \
360 }
361 
362 #define VMSTATE_ARRAY(_field, _state, _num, _version, _info, _type) {\
363     .name       = (stringify(_field)),                               \
364     .version_id = (_version),                                        \
365     .num        = (_num),                                            \
366     .info       = &(_info),                                          \
367     .size       = sizeof(_type),                                     \
368     .flags      = VMS_ARRAY,                                         \
369     .offset     = vmstate_offset_array(_state, _field, _type, _num), \
370 }
371 
372 #define VMSTATE_2DARRAY(_field, _state, _n1, _n2, _version, _info, _type) { \
373     .name       = (stringify(_field)),                                      \
374     .version_id = (_version),                                               \
375     .num        = (_n1) * (_n2),                                            \
376     .info       = &(_info),                                                 \
377     .size       = sizeof(_type),                                            \
378     .flags      = VMS_ARRAY,                                                \
379     .offset     = vmstate_offset_2darray(_state, _field, _type, _n1, _n2),  \
380 }
381 
382 #define VMSTATE_VARRAY_MULTIPLY(_field, _state, _field_num, _multiply, _info, _type) { \
383     .name       = (stringify(_field)),                               \
384     .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
385     .num        = (_multiply),                                       \
386     .info       = &(_info),                                          \
387     .size       = sizeof(_type),                                     \
388     .flags      = VMS_VARRAY_UINT32|VMS_MULTIPLY_ELEMENTS,           \
389     .offset     = vmstate_offset_varray(_state, _field, _type),      \
390 }
391 
392 #define VMSTATE_SUB_ARRAY(_field, _state, _start, _num, _version, _info, _type) { \
393     .name       = (stringify(_field)),                               \
394     .version_id = (_version),                                        \
395     .num        = (_num),                                            \
396     .info       = &(_info),                                          \
397     .size       = sizeof(_type),                                     \
398     .flags      = VMS_ARRAY,                                         \
399     .offset     = vmstate_offset_sub_array(_state, _field, _type, _start), \
400 }
401 
402 #define VMSTATE_ARRAY_INT32_UNSAFE(_field, _state, _field_num, _info, _type) {\
403     .name       = (stringify(_field)),                               \
404     .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
405     .info       = &(_info),                                          \
406     .size       = sizeof(_type),                                     \
407     .flags      = VMS_VARRAY_INT32,                                  \
408     .offset     = vmstate_offset_varray(_state, _field, _type),      \
409 }
410 
411 #define VMSTATE_VARRAY_INT32(_field, _state, _field_num, _version, _info, _type) {\
412     .name       = (stringify(_field)),                               \
413     .version_id = (_version),                                        \
414     .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
415     .info       = &(_info),                                          \
416     .size       = sizeof(_type),                                     \
417     .flags      = VMS_VARRAY_INT32|VMS_POINTER,                      \
418     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
419 }
420 
421 #define VMSTATE_VARRAY_UINT32(_field, _state, _field_num, _version, _info, _type) {\
422     .name       = (stringify(_field)),                               \
423     .version_id = (_version),                                        \
424     .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
425     .info       = &(_info),                                          \
426     .size       = sizeof(_type),                                     \
427     .flags      = VMS_VARRAY_UINT32|VMS_POINTER,                     \
428     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
429 }
430 
431 #define VMSTATE_VARRAY_UINT32_ALLOC(_field, _state, _field_num, _version, _info, _type) {\
432     .name       = (stringify(_field)),                               \
433     .version_id = (_version),                                        \
434     .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
435     .info       = &(_info),                                          \
436     .size       = sizeof(_type),                                     \
437     .flags      = VMS_VARRAY_UINT32|VMS_POINTER|VMS_ALLOC,           \
438     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
439 }
440 
441 #define VMSTATE_VARRAY_UINT16_ALLOC(_field, _state, _field_num, _version, _info, _type) {\
442     .name       = (stringify(_field)),                               \
443     .version_id = (_version),                                        \
444     .num_offset = vmstate_offset_value(_state, _field_num, uint16_t),\
445     .info       = &(_info),                                          \
446     .size       = sizeof(_type),                                     \
447     .flags      = VMS_VARRAY_UINT16 | VMS_POINTER | VMS_ALLOC,       \
448     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
449 }
450 
451 #define VMSTATE_VARRAY_UINT16_UNSAFE(_field, _state, _field_num, _version, _info, _type) {\
452     .name       = (stringify(_field)),                               \
453     .version_id = (_version),                                        \
454     .num_offset = vmstate_offset_value(_state, _field_num, uint16_t),\
455     .info       = &(_info),                                          \
456     .size       = sizeof(_type),                                     \
457     .flags      = VMS_VARRAY_UINT16,                                 \
458     .offset     = vmstate_offset_varray(_state, _field, _type),      \
459 }
460 
461 #define VMSTATE_VSTRUCT_TEST(_field, _state, _test, _version, _vmsd, _type, _struct_version) { \
462     .name         = (stringify(_field)),                             \
463     .version_id   = (_version),                                      \
464     .struct_version_id = (_struct_version),                          \
465     .field_exists = (_test),                                         \
466     .vmsd         = &(_vmsd),                                        \
467     .size         = sizeof(_type),                                   \
468     .flags        = VMS_VSTRUCT,                                     \
469     .offset       = vmstate_offset_value(_state, _field, _type),     \
470 }
471 
472 #define VMSTATE_STRUCT_TEST(_field, _state, _test, _version, _vmsd, _type) { \
473     .name         = (stringify(_field)),                             \
474     .version_id   = (_version),                                      \
475     .field_exists = (_test),                                         \
476     .vmsd         = &(_vmsd),                                        \
477     .size         = sizeof(_type),                                   \
478     .flags        = VMS_STRUCT,                                      \
479     .offset       = vmstate_offset_value(_state, _field, _type),     \
480 }
481 
482 #define VMSTATE_STRUCT_POINTER_V(_field, _state, _version, _vmsd, _type) { \
483     .name         = (stringify(_field)),                             \
484     .version_id   = (_version),                                        \
485     .vmsd         = &(_vmsd),                                        \
486     .size         = sizeof(_type *),                                 \
487     .flags        = VMS_STRUCT|VMS_POINTER,                          \
488     .offset       = vmstate_offset_pointer(_state, _field, _type),   \
489 }
490 
491 #define VMSTATE_STRUCT_POINTER_TEST_V(_field, _state, _test, _version, _vmsd, _type) { \
492     .name         = (stringify(_field)),                             \
493     .version_id   = (_version),                                        \
494     .field_exists = (_test),                                         \
495     .vmsd         = &(_vmsd),                                        \
496     .size         = sizeof(_type *),                                 \
497     .flags        = VMS_STRUCT|VMS_POINTER,                          \
498     .offset       = vmstate_offset_pointer(_state, _field, _type),   \
499 }
500 
501 #define VMSTATE_ARRAY_OF_POINTER(_field, _state, _num, _version, _info, _type) {\
502     .name       = (stringify(_field)),                               \
503     .version_id = (_version),                                        \
504     .num        = (_num),                                            \
505     .info       = &(_info),                                          \
506     .size       = sizeof(_type),                                     \
507     .flags      = VMS_ARRAY|VMS_ARRAY_OF_POINTER,                    \
508     .offset     = vmstate_offset_array(_state, _field, _type, _num), \
509 }
510 
511 #define VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(_f, _s, _n, _v, _vmsd, _type) { \
512     .name       = (stringify(_f)),                                   \
513     .version_id = (_v),                                              \
514     .num        = (_n),                                              \
515     .vmsd       = &(_vmsd),                                          \
516     .size       = sizeof(_type *),                                    \
517     .flags      = VMS_ARRAY|VMS_STRUCT|VMS_ARRAY_OF_POINTER,         \
518     .offset     = vmstate_offset_array(_s, _f, _type*, _n),          \
519 }
520 
521 #define VMSTATE_STRUCT_SUB_ARRAY(_field, _state, _start, _num, _version, _vmsd, _type) { \
522     .name       = (stringify(_field)),                                     \
523     .version_id = (_version),                                              \
524     .num        = (_num),                                                  \
525     .vmsd       = &(_vmsd),                                                \
526     .size       = sizeof(_type),                                           \
527     .flags      = VMS_STRUCT|VMS_ARRAY,                                    \
528     .offset     = vmstate_offset_sub_array(_state, _field, _type, _start), \
529 }
530 
531 #define VMSTATE_STRUCT_ARRAY_TEST(_field, _state, _num, _test, _version, _vmsd, _type) { \
532     .name         = (stringify(_field)),                             \
533     .num          = (_num),                                          \
534     .field_exists = (_test),                                         \
535     .version_id   = (_version),                                      \
536     .vmsd         = &(_vmsd),                                        \
537     .size         = sizeof(_type),                                   \
538     .flags        = VMS_STRUCT|VMS_ARRAY,                            \
539     .offset       = vmstate_offset_array(_state, _field, _type, _num),\
540 }
541 
542 #define VMSTATE_STRUCT_2DARRAY_TEST(_field, _state, _n1, _n2, _test, \
543                                     _version, _vmsd, _type) {        \
544     .name         = (stringify(_field)),                             \
545     .num          = (_n1) * (_n2),                                   \
546     .field_exists = (_test),                                         \
547     .version_id   = (_version),                                      \
548     .vmsd         = &(_vmsd),                                        \
549     .size         = sizeof(_type),                                   \
550     .flags        = VMS_STRUCT | VMS_ARRAY,                          \
551     .offset       = vmstate_offset_2darray(_state, _field, _type,    \
552                                            _n1, _n2),                \
553 }
554 
555 #define VMSTATE_STRUCT_VARRAY_UINT8(_field, _state, _field_num, _version, _vmsd, _type) { \
556     .name       = (stringify(_field)),                               \
557     .num_offset = vmstate_offset_value(_state, _field_num, uint8_t), \
558     .version_id = (_version),                                        \
559     .vmsd       = &(_vmsd),                                          \
560     .size       = sizeof(_type),                                     \
561     .flags      = VMS_STRUCT|VMS_VARRAY_UINT8,                       \
562     .offset     = vmstate_offset_varray(_state, _field, _type),      \
563 }
564 
565 /* a variable length array (i.e. _type *_field) but we know the
566  * length
567  */
568 #define VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(_field, _state, _num, _version, _vmsd, _type) { \
569     .name       = (stringify(_field)),                               \
570     .num          = (_num),                                          \
571     .version_id = (_version),                                        \
572     .vmsd       = &(_vmsd),                                          \
573     .size       = sizeof(_type),                                     \
574     .flags      = VMS_STRUCT|VMS_ARRAY|VMS_POINTER,                  \
575     .offset     = offsetof(_state, _field),                          \
576 }
577 
578 #define VMSTATE_STRUCT_VARRAY_POINTER_INT32(_field, _state, _field_num, _vmsd, _type) { \
579     .name       = (stringify(_field)),                               \
580     .version_id = 0,                                                 \
581     .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
582     .size       = sizeof(_type),                                     \
583     .vmsd       = &(_vmsd),                                          \
584     .flags      = VMS_POINTER | VMS_VARRAY_INT32 | VMS_STRUCT,       \
585     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
586 }
587 
588 #define VMSTATE_STRUCT_VARRAY_POINTER_UINT32(_field, _state, _field_num, _vmsd, _type) { \
589     .name       = (stringify(_field)),                               \
590     .version_id = 0,                                                 \
591     .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
592     .size       = sizeof(_type),                                     \
593     .vmsd       = &(_vmsd),                                          \
594     .flags      = VMS_POINTER | VMS_VARRAY_INT32 | VMS_STRUCT,       \
595     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
596 }
597 
598 #define VMSTATE_STRUCT_VARRAY_POINTER_UINT16(_field, _state, _field_num, _vmsd, _type) { \
599     .name       = (stringify(_field)),                               \
600     .version_id = 0,                                                 \
601     .num_offset = vmstate_offset_value(_state, _field_num, uint16_t),\
602     .size       = sizeof(_type),                                     \
603     .vmsd       = &(_vmsd),                                          \
604     .flags      = VMS_POINTER | VMS_VARRAY_UINT16 | VMS_STRUCT,      \
605     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
606 }
607 
608 #define VMSTATE_STRUCT_VARRAY_INT32(_field, _state, _field_num, _version, _vmsd, _type) { \
609     .name       = (stringify(_field)),                               \
610     .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
611     .version_id = (_version),                                        \
612     .vmsd       = &(_vmsd),                                          \
613     .size       = sizeof(_type),                                     \
614     .flags      = VMS_STRUCT|VMS_VARRAY_INT32,                       \
615     .offset     = vmstate_offset_varray(_state, _field, _type),      \
616 }
617 
618 #define VMSTATE_STRUCT_VARRAY_UINT32(_field, _state, _field_num, _version, _vmsd, _type) { \
619     .name       = (stringify(_field)),                               \
620     .num_offset = vmstate_offset_value(_state, _field_num, uint32_t), \
621     .version_id = (_version),                                        \
622     .vmsd       = &(_vmsd),                                          \
623     .size       = sizeof(_type),                                     \
624     .flags      = VMS_STRUCT|VMS_VARRAY_UINT32,                      \
625     .offset     = vmstate_offset_varray(_state, _field, _type),      \
626 }
627 
628 #define VMSTATE_STRUCT_VARRAY_ALLOC(_field, _state, _field_num, _version, _vmsd, _type) {\
629     .name       = (stringify(_field)),                               \
630     .version_id = (_version),                                        \
631     .vmsd       = &(_vmsd),                                          \
632     .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
633     .size       = sizeof(_type),                                     \
634     .flags      = VMS_STRUCT|VMS_VARRAY_INT32|VMS_ALLOC|VMS_POINTER, \
635     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
636 }
637 
638 #define VMSTATE_STATIC_BUFFER(_field, _state, _version, _test, _start, _size) { \
639     .name         = (stringify(_field)),                             \
640     .version_id   = (_version),                                      \
641     .field_exists = (_test),                                         \
642     .size         = (_size - _start),                                \
643     .info         = &vmstate_info_buffer,                            \
644     .flags        = VMS_BUFFER,                                      \
645     .offset       = vmstate_offset_buffer(_state, _field) + _start,  \
646 }
647 
648 #define VMSTATE_VBUFFER_MULTIPLY(_field, _state, _version, _test,    \
649                                  _field_size, _multiply) {           \
650     .name         = (stringify(_field)),                             \
651     .version_id   = (_version),                                      \
652     .field_exists = (_test),                                         \
653     .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
654     .size         = (_multiply),                                      \
655     .info         = &vmstate_info_buffer,                            \
656     .flags        = VMS_VBUFFER|VMS_POINTER|VMS_MULTIPLY,            \
657     .offset       = offsetof(_state, _field),                        \
658 }
659 
660 #define VMSTATE_VBUFFER(_field, _state, _version, _test, _field_size) { \
661     .name         = (stringify(_field)),                             \
662     .version_id   = (_version),                                      \
663     .field_exists = (_test),                                         \
664     .size_offset  = vmstate_offset_value(_state, _field_size, int32_t),\
665     .info         = &vmstate_info_buffer,                            \
666     .flags        = VMS_VBUFFER|VMS_POINTER,                         \
667     .offset       = offsetof(_state, _field),                        \
668 }
669 
670 #define VMSTATE_VBUFFER_UINT32(_field, _state, _version, _test, _field_size) { \
671     .name         = (stringify(_field)),                             \
672     .version_id   = (_version),                                      \
673     .field_exists = (_test),                                         \
674     .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
675     .info         = &vmstate_info_buffer,                            \
676     .flags        = VMS_VBUFFER|VMS_POINTER,                         \
677     .offset       = offsetof(_state, _field),                        \
678 }
679 
680 #define VMSTATE_VBUFFER_ALLOC_UINT32(_field, _state, _version,       \
681                                      _test, _field_size) {           \
682     .name         = (stringify(_field)),                             \
683     .version_id   = (_version),                                      \
684     .field_exists = (_test),                                         \
685     .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
686     .info         = &vmstate_info_buffer,                            \
687     .flags        = VMS_VBUFFER|VMS_POINTER|VMS_ALLOC,               \
688     .offset       = offsetof(_state, _field),                        \
689 }
690 
691 #define VMSTATE_BUFFER_UNSAFE_INFO_TEST(_field, _state, _test, _version, _info, _size) { \
692     .name       = (stringify(_field)),                               \
693     .version_id = (_version),                                        \
694     .field_exists = (_test),                                         \
695     .size       = (_size),                                           \
696     .info       = &(_info),                                          \
697     .flags      = VMS_BUFFER,                                        \
698     .offset     = offsetof(_state, _field),                          \
699 }
700 
701 #define VMSTATE_BUFFER_POINTER_UNSAFE(_field, _state, _version, _size) { \
702     .name       = (stringify(_field)),                               \
703     .version_id = (_version),                                        \
704     .size       = (_size),                                           \
705     .info       = &vmstate_info_buffer,                              \
706     .flags      = VMS_BUFFER|VMS_POINTER,                            \
707     .offset     = offsetof(_state, _field),                          \
708 }
709 
710 /* Allocate a temporary of type 'tmp_type', set tmp->parent to _state
711  * and execute the vmsd on the temporary.  Note that we're working with
712  * the whole of _state here, not a field within it.
713  * We compile time check that:
714  *    That _tmp_type contains a 'parent' member that's a pointer to the
715  *        '_state' type
716  *    That the pointer is right at the start of _tmp_type.
717  */
718 #define VMSTATE_WITH_TMP_TEST(_state, _test, _tmp_type, _vmsd) {     \
719     .name         = "tmp",                                           \
720     .field_exists = (_test),                                         \
721     .size         = sizeof(_tmp_type) +                              \
722                     QEMU_BUILD_BUG_ON_ZERO(offsetof(_tmp_type, parent) != 0) + \
723                     type_check_pointer(_state,                       \
724                         typeof_field(_tmp_type, parent)),            \
725     .vmsd         = &(_vmsd),                                        \
726     .info         = &vmstate_info_tmp,                               \
727 }
728 
729 #define VMSTATE_WITH_TMP(_state, _tmp_type, _vmsd) \
730     VMSTATE_WITH_TMP_TEST(_state, NULL, _tmp_type, _vmsd)
731 
732 #define VMSTATE_UNUSED_BUFFER(_test, _version, _size) {              \
733     .name         = "unused",                                        \
734     .field_exists = (_test),                                         \
735     .version_id   = (_version),                                      \
736     .size         = (_size),                                         \
737     .info         = &vmstate_info_unused_buffer,                     \
738     .flags        = VMS_BUFFER,                                      \
739 }
740 
741 /* Discard size * field_num bytes, where field_num is a uint32 member */
742 #define VMSTATE_UNUSED_VARRAY_UINT32(_state, _test, _version, _field_num, _size) {\
743     .name         = "unused",                                        \
744     .field_exists = (_test),                                         \
745     .num_offset   = vmstate_offset_value(_state, _field_num, uint32_t),\
746     .version_id   = (_version),                                      \
747     .size         = (_size),                                         \
748     .info         = &vmstate_info_unused_buffer,                     \
749     .flags        = VMS_VARRAY_UINT32 | VMS_BUFFER,                  \
750 }
751 
752 /* _field_size should be a int32_t field in the _state struct giving the
753  * size of the bitmap _field in bits.
754  */
755 #define VMSTATE_BITMAP_TEST(_field, _state, _test, _version, _field_size) { \
756     .name         = (stringify(_field)),                             \
757     .field_exists = (_test),                                         \
758     .version_id   = (_version),                                      \
759     .size_offset  = vmstate_offset_value(_state, _field_size, int32_t),\
760     .info         = &vmstate_info_bitmap,                            \
761     .flags        = VMS_VBUFFER|VMS_POINTER,                         \
762     .offset       = offsetof(_state, _field),                        \
763 }
764 
765 #define VMSTATE_BITMAP(_field, _state, _version, _field_size) \
766     VMSTATE_BITMAP_TEST(_field, _state, NULL, _version, _field_size)
767 
768 /* For migrating a QTAILQ.
769  * Target QTAILQ needs be properly initialized.
770  * _type: type of QTAILQ element
771  * _next: name of QTAILQ entry field in QTAILQ element
772  * _vmsd: VMSD for QTAILQ element
773  * size: size of QTAILQ element
774  * start: offset of QTAILQ entry in QTAILQ element
775  */
776 #define VMSTATE_QTAILQ_V(_field, _state, _version, _vmsd, _type, _next)  \
777 {                                                                        \
778     .name         = (stringify(_field)),                                 \
779     .version_id   = (_version),                                          \
780     .vmsd         = &(_vmsd),                                            \
781     .size         = sizeof(_type),                                       \
782     .info         = &vmstate_info_qtailq,                                \
783     .offset       = offsetof(_state, _field),                            \
784     .start        = offsetof(_type, _next),                              \
785 }
786 
787 /*
788  * For migrating a GTree whose key is a pointer to _key_type and the
789  * value, a pointer to _val_type
790  * The target tree must have been properly initialized
791  * _vmsd: Start address of the 2 element array containing the data vmsd
792  *        and the key vmsd, in that order
793  * _key_type: type of the key
794  * _val_type: type of the value
795  */
796 #define VMSTATE_GTREE_V(_field, _state, _version, _vmsd,                       \
797                         _key_type, _val_type)                                  \
798 {                                                                              \
799     .name         = (stringify(_field)),                                       \
800     .version_id   = (_version),                                                \
801     .vmsd         = (_vmsd),                                                   \
802     .info         = &vmstate_info_gtree,                                       \
803     .start        = sizeof(_key_type),                                         \
804     .size         = sizeof(_val_type),                                         \
805     .offset       = offsetof(_state, _field),                                  \
806 }
807 
808 /*
809  * For migrating a GTree with direct key and the value a pointer
810  * to _val_type
811  * The target tree must have been properly initialized
812  * _vmsd: data vmsd
813  * _val_type: type of the value
814  */
815 #define VMSTATE_GTREE_DIRECT_KEY_V(_field, _state, _version, _vmsd, _val_type) \
816 {                                                                              \
817     .name         = (stringify(_field)),                                       \
818     .version_id   = (_version),                                                \
819     .vmsd         = (_vmsd),                                                   \
820     .info         = &vmstate_info_gtree,                                       \
821     .start        = 0,                                                         \
822     .size         = sizeof(_val_type),                                         \
823     .offset       = offsetof(_state, _field),                                  \
824 }
825 
826 /*
827  * For migrating a QLIST
828  * Target QLIST needs be properly initialized.
829  * _type: type of QLIST element
830  * _next: name of QLIST_ENTRY entry field in QLIST element
831  * _vmsd: VMSD for QLIST element
832  * size: size of QLIST element
833  * start: offset of QLIST_ENTRY in QTAILQ element
834  */
835 #define VMSTATE_QLIST_V(_field, _state, _version, _vmsd, _type, _next)  \
836 {                                                                        \
837     .name         = (stringify(_field)),                                 \
838     .version_id   = (_version),                                          \
839     .vmsd         = &(_vmsd),                                            \
840     .size         = sizeof(_type),                                       \
841     .info         = &vmstate_info_qlist,                                 \
842     .offset       = offsetof(_state, _field),                            \
843     .start        = offsetof(_type, _next),                              \
844 }
845 
846 /* _f : field name
847    _f_n : num of elements field_name
848    _n : num of elements
849    _s : struct state name
850    _v : version
851 */
852 
853 #define VMSTATE_SINGLE(_field, _state, _version, _info, _type)        \
854     VMSTATE_SINGLE_TEST(_field, _state, NULL, _version, _info, _type)
855 
856 #define VMSTATE_VSTRUCT(_field, _state, _vmsd, _type, _struct_version)\
857     VMSTATE_VSTRUCT_TEST(_field, _state, NULL, 0, _vmsd, _type, _struct_version)
858 
859 #define VMSTATE_VSTRUCT_V(_field, _state, _version, _vmsd, _type, _struct_version) \
860     VMSTATE_VSTRUCT_TEST(_field, _state, NULL, _version, _vmsd, _type, \
861                          _struct_version)
862 
863 #define VMSTATE_STRUCT(_field, _state, _version, _vmsd, _type)        \
864     VMSTATE_STRUCT_TEST(_field, _state, NULL, _version, _vmsd, _type)
865 
866 #define VMSTATE_STRUCT_POINTER(_field, _state, _vmsd, _type)          \
867     VMSTATE_STRUCT_POINTER_V(_field, _state, 0, _vmsd, _type)
868 
869 #define VMSTATE_STRUCT_POINTER_TEST(_field, _state, _test, _vmsd, _type)     \
870     VMSTATE_STRUCT_POINTER_TEST_V(_field, _state, _test, 0, _vmsd, _type)
871 
872 #define VMSTATE_STRUCT_ARRAY(_field, _state, _num, _version, _vmsd, _type) \
873     VMSTATE_STRUCT_ARRAY_TEST(_field, _state, _num, NULL, _version,   \
874             _vmsd, _type)
875 
876 #define VMSTATE_STRUCT_2DARRAY(_field, _state, _n1, _n2, _version,    \
877             _vmsd, _type)                                             \
878     VMSTATE_STRUCT_2DARRAY_TEST(_field, _state, _n1, _n2, NULL,       \
879             _version, _vmsd, _type)
880 
881 #define VMSTATE_BUFFER_UNSAFE_INFO(_field, _state, _version, _info, _size) \
882     VMSTATE_BUFFER_UNSAFE_INFO_TEST(_field, _state, NULL, _version, _info, \
883             _size)
884 
885 #define VMSTATE_BOOL_V(_f, _s, _v)                                    \
886     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_bool, bool)
887 
888 #define VMSTATE_INT8_V(_f, _s, _v)                                    \
889     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int8, int8_t)
890 #define VMSTATE_INT16_V(_f, _s, _v)                                   \
891     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int16, int16_t)
892 #define VMSTATE_INT32_V(_f, _s, _v)                                   \
893     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int32, int32_t)
894 #define VMSTATE_INT64_V(_f, _s, _v)                                   \
895     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int64, int64_t)
896 
897 #define VMSTATE_UINT8_V(_f, _s, _v)                                   \
898     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint8, uint8_t)
899 #define VMSTATE_UINT16_V(_f, _s, _v)                                  \
900     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint16, uint16_t)
901 #define VMSTATE_UINT32_V(_f, _s, _v)                                  \
902     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint32, uint32_t)
903 #define VMSTATE_UINT64_V(_f, _s, _v)                                  \
904     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint64, uint64_t)
905 
906 #define VMSTATE_FD_V(_f, _s, _v)                                  \
907     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_fd, int32_t)
908 
909 #ifdef CONFIG_LINUX
910 
911 #define VMSTATE_U8_V(_f, _s, _v)                                   \
912     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint8, __u8)
913 #define VMSTATE_U16_V(_f, _s, _v)                                  \
914     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint16, __u16)
915 #define VMSTATE_U32_V(_f, _s, _v)                                  \
916     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint32, __u32)
917 #define VMSTATE_U64_V(_f, _s, _v)                                  \
918     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint64, __u64)
919 
920 #endif
921 
922 #define VMSTATE_BOOL(_f, _s)                                          \
923     VMSTATE_BOOL_V(_f, _s, 0)
924 
925 #define VMSTATE_INT8(_f, _s)                                          \
926     VMSTATE_INT8_V(_f, _s, 0)
927 #define VMSTATE_INT16(_f, _s)                                         \
928     VMSTATE_INT16_V(_f, _s, 0)
929 #define VMSTATE_INT32(_f, _s)                                         \
930     VMSTATE_INT32_V(_f, _s, 0)
931 #define VMSTATE_INT64(_f, _s)                                         \
932     VMSTATE_INT64_V(_f, _s, 0)
933 
934 #define VMSTATE_UINT8(_f, _s)                                         \
935     VMSTATE_UINT8_V(_f, _s, 0)
936 #define VMSTATE_UINT16(_f, _s)                                        \
937     VMSTATE_UINT16_V(_f, _s, 0)
938 #define VMSTATE_UINT32(_f, _s)                                        \
939     VMSTATE_UINT32_V(_f, _s, 0)
940 #define VMSTATE_UINT64(_f, _s)                                        \
941     VMSTATE_UINT64_V(_f, _s, 0)
942 
943 #define VMSTATE_FD(_f, _s)                                            \
944     VMSTATE_FD_V(_f, _s, 0)
945 
946 #ifdef CONFIG_LINUX
947 
948 #define VMSTATE_U8(_f, _s)                                         \
949     VMSTATE_U8_V(_f, _s, 0)
950 #define VMSTATE_U16(_f, _s)                                        \
951     VMSTATE_U16_V(_f, _s, 0)
952 #define VMSTATE_U32(_f, _s)                                        \
953     VMSTATE_U32_V(_f, _s, 0)
954 #define VMSTATE_U64(_f, _s)                                        \
955     VMSTATE_U64_V(_f, _s, 0)
956 
957 #endif
958 
959 #define VMSTATE_UINT8_EQUAL(_f, _s, _err_hint)                        \
960     VMSTATE_SINGLE_FULL(_f, _s, 0, 0,                                 \
961                         vmstate_info_uint8_equal, uint8_t, _err_hint)
962 
963 #define VMSTATE_UINT16_EQUAL(_f, _s, _err_hint)                       \
964     VMSTATE_SINGLE_FULL(_f, _s, 0, 0,                                 \
965                         vmstate_info_uint16_equal, uint16_t, _err_hint)
966 
967 #define VMSTATE_UINT16_EQUAL_V(_f, _s, _v, _err_hint)                 \
968     VMSTATE_SINGLE_FULL(_f, _s, 0,  _v,                               \
969                         vmstate_info_uint16_equal, uint16_t, _err_hint)
970 
971 #define VMSTATE_INT32_EQUAL(_f, _s, _err_hint)                        \
972     VMSTATE_SINGLE_FULL(_f, _s, 0, 0,                                 \
973                         vmstate_info_int32_equal, int32_t, _err_hint)
974 
975 #define VMSTATE_UINT32_EQUAL_V(_f, _s, _v, _err_hint)                 \
976     VMSTATE_SINGLE_FULL(_f, _s, 0,  _v,                               \
977                         vmstate_info_uint32_equal, uint32_t, _err_hint)
978 
979 #define VMSTATE_UINT32_EQUAL(_f, _s, _err_hint)                       \
980     VMSTATE_UINT32_EQUAL_V(_f, _s, 0, _err_hint)
981 
982 #define VMSTATE_UINT64_EQUAL_V(_f, _s, _v, _err_hint)                 \
983     VMSTATE_SINGLE_FULL(_f, _s, 0,  _v,                               \
984                         vmstate_info_uint64_equal, uint64_t, _err_hint)
985 
986 #define VMSTATE_UINT64_EQUAL(_f, _s, _err_hint)                       \
987     VMSTATE_UINT64_EQUAL_V(_f, _s, 0, _err_hint)
988 
989 #define VMSTATE_INT32_POSITIVE_LE(_f, _s)                             \
990     VMSTATE_SINGLE(_f, _s, 0, vmstate_info_int32_le, int32_t)
991 
992 #define VMSTATE_BOOL_TEST(_f, _s, _t)                               \
993     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_bool, bool)
994 
995 #define VMSTATE_INT8_TEST(_f, _s, _t)                               \
996     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_int8, int8_t)
997 
998 #define VMSTATE_INT16_TEST(_f, _s, _t)                               \
999     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_int16, int16_t)
1000 
1001 #define VMSTATE_INT32_TEST(_f, _s, _t)                                  \
1002     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_int32, int32_t)
1003 
1004 #define VMSTATE_INT64_TEST(_f, _s, _t)                                  \
1005     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_int64, int64_t)
1006 
1007 #define VMSTATE_UINT8_TEST(_f, _s, _t)                               \
1008     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint8, uint8_t)
1009 
1010 #define VMSTATE_UINT16_TEST(_f, _s, _t)                               \
1011     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint16, uint16_t)
1012 
1013 #define VMSTATE_UINT32_TEST(_f, _s, _t)                                  \
1014     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint32, uint32_t)
1015 
1016 #define VMSTATE_UINT64_TEST(_f, _s, _t)                                  \
1017     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint64, uint64_t)
1018 
1019 #define VMSTATE_FD_TEST(_f, _s, _t)                                            \
1020     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_fd, int32_t)
1021 
1022 #define VMSTATE_TIMER_PTR_TEST(_f, _s, _test)                             \
1023     VMSTATE_POINTER_TEST(_f, _s, _test, vmstate_info_timer, QEMUTimer *)
1024 
1025 #define VMSTATE_TIMER_PTR_V(_f, _s, _v)                                   \
1026     VMSTATE_POINTER(_f, _s, _v, vmstate_info_timer, QEMUTimer *)
1027 
1028 #define VMSTATE_TIMER_PTR(_f, _s)                                         \
1029     VMSTATE_TIMER_PTR_V(_f, _s, 0)
1030 
1031 #define VMSTATE_TIMER_PTR_ARRAY(_f, _s, _n)                              \
1032     VMSTATE_ARRAY_OF_POINTER(_f, _s, _n, 0, vmstate_info_timer, QEMUTimer *)
1033 
1034 #define VMSTATE_TIMER_TEST(_f, _s, _test)                             \
1035     VMSTATE_SINGLE_TEST(_f, _s, _test, 0, vmstate_info_timer, QEMUTimer)
1036 
1037 #define VMSTATE_TIMER_V(_f, _s, _v)                                   \
1038     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_timer, QEMUTimer)
1039 
1040 #define VMSTATE_TIMER(_f, _s)                                         \
1041     VMSTATE_TIMER_V(_f, _s, 0)
1042 
1043 #define VMSTATE_TIMER_ARRAY(_f, _s, _n)                              \
1044     VMSTATE_ARRAY(_f, _s, _n, 0, vmstate_info_timer, QEMUTimer)
1045 
1046 #define VMSTATE_BOOL_ARRAY_V(_f, _s, _n, _v)                         \
1047     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_bool, bool)
1048 
1049 #define VMSTATE_BOOL_ARRAY(_f, _s, _n)                               \
1050     VMSTATE_BOOL_ARRAY_V(_f, _s, _n, 0)
1051 
1052 #define VMSTATE_BOOL_SUB_ARRAY(_f, _s, _start, _num)                \
1053     VMSTATE_SUB_ARRAY(_f, _s, _start, _num, 0, vmstate_info_bool, bool)
1054 
1055 #define VMSTATE_UINT16_ARRAY_V(_f, _s, _n, _v)                         \
1056     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint16, uint16_t)
1057 
1058 #define VMSTATE_UINT16_2DARRAY_V(_f, _s, _n1, _n2, _v)                \
1059     VMSTATE_2DARRAY(_f, _s, _n1, _n2, _v, vmstate_info_uint16, uint16_t)
1060 
1061 #define VMSTATE_UINT16_ARRAY(_f, _s, _n)                               \
1062     VMSTATE_UINT16_ARRAY_V(_f, _s, _n, 0)
1063 
1064 #define VMSTATE_UINT16_SUB_ARRAY(_f, _s, _start, _num)                \
1065     VMSTATE_SUB_ARRAY(_f, _s, _start, _num, 0, vmstate_info_uint16, uint16_t)
1066 
1067 #define VMSTATE_UINT16_2DARRAY(_f, _s, _n1, _n2)                      \
1068     VMSTATE_UINT16_2DARRAY_V(_f, _s, _n1, _n2, 0)
1069 
1070 #define VMSTATE_UINT8_2DARRAY_V(_f, _s, _n1, _n2, _v)                 \
1071     VMSTATE_2DARRAY(_f, _s, _n1, _n2, _v, vmstate_info_uint8, uint8_t)
1072 
1073 #define VMSTATE_UINT8_ARRAY_V(_f, _s, _n, _v)                         \
1074     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint8, uint8_t)
1075 
1076 #define VMSTATE_UINT8_ARRAY(_f, _s, _n)                               \
1077     VMSTATE_UINT8_ARRAY_V(_f, _s, _n, 0)
1078 
1079 #define VMSTATE_UINT8_SUB_ARRAY(_f, _s, _start, _num)                \
1080     VMSTATE_SUB_ARRAY(_f, _s, _start, _num, 0, vmstate_info_uint8, uint8_t)
1081 
1082 #define VMSTATE_UINT8_2DARRAY(_f, _s, _n1, _n2)                       \
1083     VMSTATE_UINT8_2DARRAY_V(_f, _s, _n1, _n2, 0)
1084 
1085 #define VMSTATE_UINT32_ARRAY_V(_f, _s, _n, _v)                        \
1086     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint32, uint32_t)
1087 
1088 #define VMSTATE_UINT32_2DARRAY_V(_f, _s, _n1, _n2, _v)                \
1089     VMSTATE_2DARRAY(_f, _s, _n1, _n2, _v, vmstate_info_uint32, uint32_t)
1090 
1091 #define VMSTATE_UINT32_ARRAY(_f, _s, _n)                              \
1092     VMSTATE_UINT32_ARRAY_V(_f, _s, _n, 0)
1093 
1094 #define VMSTATE_UINT32_SUB_ARRAY(_f, _s, _start, _num)                \
1095     VMSTATE_SUB_ARRAY(_f, _s, _start, _num, 0, vmstate_info_uint32, uint32_t)
1096 
1097 #define VMSTATE_UINT32_2DARRAY(_f, _s, _n1, _n2)                      \
1098     VMSTATE_UINT32_2DARRAY_V(_f, _s, _n1, _n2, 0)
1099 
1100 #define VMSTATE_UINT64_ARRAY_V(_f, _s, _n, _v)                        \
1101     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint64, uint64_t)
1102 
1103 #define VMSTATE_UINT64_ARRAY(_f, _s, _n)                              \
1104     VMSTATE_UINT64_ARRAY_V(_f, _s, _n, 0)
1105 
1106 #define VMSTATE_UINT64_SUB_ARRAY(_f, _s, _start, _num)                \
1107     VMSTATE_SUB_ARRAY(_f, _s, _start, _num, 0, vmstate_info_uint64, uint64_t)
1108 
1109 #define VMSTATE_UINT64_2DARRAY(_f, _s, _n1, _n2)                      \
1110     VMSTATE_UINT64_2DARRAY_V(_f, _s, _n1, _n2, 0)
1111 
1112 #define VMSTATE_UINT64_2DARRAY_V(_f, _s, _n1, _n2, _v)                 \
1113     VMSTATE_2DARRAY(_f, _s, _n1, _n2, _v, vmstate_info_uint64, uint64_t)
1114 
1115 #define VMSTATE_INT16_ARRAY_V(_f, _s, _n, _v)                         \
1116     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_int16, int16_t)
1117 
1118 #define VMSTATE_INT16_ARRAY(_f, _s, _n)                               \
1119     VMSTATE_INT16_ARRAY_V(_f, _s, _n, 0)
1120 
1121 #define VMSTATE_INT32_ARRAY_V(_f, _s, _n, _v)                         \
1122     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_int32, int32_t)
1123 
1124 #define VMSTATE_INT32_ARRAY(_f, _s, _n)                               \
1125     VMSTATE_INT32_ARRAY_V(_f, _s, _n, 0)
1126 
1127 #define VMSTATE_INT64_ARRAY_V(_f, _s, _n, _v)                         \
1128     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_int64, int64_t)
1129 
1130 #define VMSTATE_INT64_ARRAY(_f, _s, _n)                               \
1131     VMSTATE_INT64_ARRAY_V(_f, _s, _n, 0)
1132 
1133 #define VMSTATE_CPUDOUBLE_ARRAY_V(_f, _s, _n, _v)                     \
1134     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_cpudouble, CPU_DoubleU)
1135 
1136 #define VMSTATE_CPUDOUBLE_ARRAY(_f, _s, _n)                           \
1137     VMSTATE_CPUDOUBLE_ARRAY_V(_f, _s, _n, 0)
1138 
1139 #define VMSTATE_BUFFER_V(_f, _s, _v)                                  \
1140     VMSTATE_STATIC_BUFFER(_f, _s, _v, NULL, 0, sizeof(typeof_field(_s, _f)))
1141 
1142 #define VMSTATE_BUFFER(_f, _s)                                        \
1143     VMSTATE_BUFFER_V(_f, _s, 0)
1144 
1145 #define VMSTATE_PARTIAL_BUFFER(_f, _s, _size)                         \
1146     VMSTATE_STATIC_BUFFER(_f, _s, 0, NULL, 0, _size)
1147 
1148 #define VMSTATE_BUFFER_START_MIDDLE_V(_f, _s, _start, _v) \
1149     VMSTATE_STATIC_BUFFER(_f, _s, _v, NULL, _start, sizeof(typeof_field(_s, _f)))
1150 
1151 #define VMSTATE_BUFFER_START_MIDDLE(_f, _s, _start) \
1152     VMSTATE_BUFFER_START_MIDDLE_V(_f, _s, _start, 0)
1153 
1154 #define VMSTATE_PARTIAL_VBUFFER(_f, _s, _size)                        \
1155     VMSTATE_VBUFFER(_f, _s, 0, NULL, _size)
1156 
1157 #define VMSTATE_PARTIAL_VBUFFER_UINT32(_f, _s, _size)                        \
1158     VMSTATE_VBUFFER_UINT32(_f, _s, 0, NULL, _size)
1159 
1160 #define VMSTATE_BUFFER_TEST(_f, _s, _test)                            \
1161     VMSTATE_STATIC_BUFFER(_f, _s, 0, _test, 0, sizeof(typeof_field(_s, _f)))
1162 
1163 #define VMSTATE_BUFFER_UNSAFE(_field, _state, _version, _size)        \
1164     VMSTATE_BUFFER_UNSAFE_INFO(_field, _state, _version, vmstate_info_buffer, _size)
1165 
1166 /*
1167  * These VMSTATE_UNUSED*() macros can be used to fill in the holes
1168  * when some of the vmstate fields are obsolete to be compatible with
1169  * migrations between new/old binaries.
1170  *
1171  * CAUTION: when using any of the VMSTATE_UNUSED*() macros please be
1172  * sure that the size passed in is the size that was actually *sent*
1173  * rather than the size of the *structure*.  One example is the
1174  * boolean type - the size of the structure can vary depending on the
1175  * definition of boolean, however the size we actually sent is always
1176  * 1 byte (please refer to implementation of VMSTATE_BOOL_V and
1177  * vmstate_info_bool).  So here we should always pass in size==1
1178  * rather than size==sizeof(bool).
1179  */
1180 #define VMSTATE_UNUSED_V(_v, _size)                                   \
1181     VMSTATE_UNUSED_BUFFER(NULL, _v, _size)
1182 
1183 #define VMSTATE_UNUSED(_size)                                         \
1184     VMSTATE_UNUSED_V(0, _size)
1185 
1186 #define VMSTATE_UNUSED_TEST(_test, _size)                             \
1187     VMSTATE_UNUSED_BUFFER(_test, 0, _size)
1188 
1189 #define VMSTATE_END_OF_LIST()                                         \
1190     {                     \
1191         .flags = VMS_END, \
1192     }
1193 
1194 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1195                        void *opaque, int version_id);
1196 int vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
1197                        void *opaque, JSONWriter *vmdesc);
1198 int vmstate_save_state_with_err(QEMUFile *f, const VMStateDescription *vmsd,
1199                        void *opaque, JSONWriter *vmdesc, Error **errp);
1200 int vmstate_save_state_v(QEMUFile *f, const VMStateDescription *vmsd,
1201                          void *opaque, JSONWriter *vmdesc,
1202                          int version_id, Error **errp);
1203 
1204 bool vmstate_section_needed(const VMStateDescription *vmsd, void *opaque);
1205 
1206 #define  VMSTATE_INSTANCE_ID_ANY  -1
1207 
1208 /* Returns: 0 on success, -1 on failure */
1209 int vmstate_register_with_alias_id(VMStateIf *obj, uint32_t instance_id,
1210                                    const VMStateDescription *vmsd,
1211                                    void *base, int alias_id,
1212                                    int required_for_version,
1213                                    Error **errp);
1214 
1215 /**
1216  * vmstate_register() - legacy function to register state
1217  * serialisation description
1218  *
1219  * New code shouldn't be using this function as QOM-ified devices have
1220  * dc->vmsd to store the serialisation description.
1221  *
1222  * Returns: 0 on success, -1 on failure
1223  */
1224 static inline int vmstate_register(VMStateIf *obj, int instance_id,
1225                                    const VMStateDescription *vmsd,
1226                                    void *opaque)
1227 {
1228     return vmstate_register_with_alias_id(obj, instance_id, vmsd,
1229                                           opaque, -1, 0, NULL);
1230 }
1231 
1232 /**
1233  * vmstate_replace_hack_for_ppc() - ppc used to abuse vmstate_register
1234  *
1235  * Don't even think about using this function in new code.
1236  *
1237  * Returns: 0 on success, -1 on failure
1238  */
1239 int vmstate_replace_hack_for_ppc(VMStateIf *obj, int instance_id,
1240                                  const VMStateDescription *vmsd,
1241                                  void *opaque);
1242 
1243 /**
1244  * vmstate_register_any() - legacy function to register state
1245  * serialisation description and let the function choose the id
1246  *
1247  * New code shouldn't be using this function as QOM-ified devices have
1248  * dc->vmsd to store the serialisation description.
1249  *
1250  * Returns: 0 on success, -1 on failure
1251  */
1252 static inline int vmstate_register_any(VMStateIf *obj,
1253                                        const VMStateDescription *vmsd,
1254                                        void *opaque)
1255 {
1256     return vmstate_register_with_alias_id(obj, VMSTATE_INSTANCE_ID_ANY, vmsd,
1257                                           opaque, -1, 0, NULL);
1258 }
1259 
1260 void vmstate_unregister(VMStateIf *obj, const VMStateDescription *vmsd,
1261                         void *opaque);
1262 
1263 void vmstate_register_ram(struct MemoryRegion *memory, DeviceState *dev);
1264 void vmstate_unregister_ram(struct MemoryRegion *memory, DeviceState *dev);
1265 void vmstate_register_ram_global(struct MemoryRegion *memory);
1266 
1267 bool vmstate_check_only_migratable(const VMStateDescription *vmsd);
1268 
1269 #endif
1270