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