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