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