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