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