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