xref: /openbmc/qemu/include/migration/vmstate.h (revision 1bfe5f05)
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_2DARRAY_TEST(_field, _state, _n1, _n2, _test, \
504                                     _version, _vmsd, _type) {        \
505     .name         = (stringify(_field)),                             \
506     .num          = (_n1) * (_n2),                                   \
507     .field_exists = (_test),                                         \
508     .version_id   = (_version),                                      \
509     .vmsd         = &(_vmsd),                                        \
510     .size         = sizeof(_type),                                   \
511     .flags        = VMS_STRUCT | VMS_ARRAY,                          \
512     .offset       = vmstate_offset_2darray(_state, _field, _type,    \
513                                            _n1, _n2),                \
514 }
515 
516 #define VMSTATE_STRUCT_VARRAY_UINT8(_field, _state, _field_num, _version, _vmsd, _type) { \
517     .name       = (stringify(_field)),                               \
518     .num_offset = vmstate_offset_value(_state, _field_num, uint8_t), \
519     .version_id = (_version),                                        \
520     .vmsd       = &(_vmsd),                                          \
521     .size       = sizeof(_type),                                     \
522     .flags      = VMS_STRUCT|VMS_VARRAY_UINT8,                       \
523     .offset     = offsetof(_state, _field),                          \
524 }
525 
526 /* a variable length array (i.e. _type *_field) but we know the
527  * length
528  */
529 #define VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(_field, _state, _num, _version, _vmsd, _type) { \
530     .name       = (stringify(_field)),                               \
531     .num          = (_num),                                          \
532     .version_id = (_version),                                        \
533     .vmsd       = &(_vmsd),                                          \
534     .size       = sizeof(_type),                                     \
535     .flags      = VMS_STRUCT|VMS_ARRAY|VMS_POINTER,                  \
536     .offset     = offsetof(_state, _field),                          \
537 }
538 
539 #define VMSTATE_STRUCT_VARRAY_POINTER_INT32(_field, _state, _field_num, _vmsd, _type) { \
540     .name       = (stringify(_field)),                               \
541     .version_id = 0,                                                 \
542     .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
543     .size       = sizeof(_type),                                     \
544     .vmsd       = &(_vmsd),                                          \
545     .flags      = VMS_POINTER | VMS_VARRAY_INT32 | VMS_STRUCT,       \
546     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
547 }
548 
549 #define VMSTATE_STRUCT_VARRAY_POINTER_UINT32(_field, _state, _field_num, _vmsd, _type) { \
550     .name       = (stringify(_field)),                               \
551     .version_id = 0,                                                 \
552     .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
553     .size       = sizeof(_type),                                     \
554     .vmsd       = &(_vmsd),                                          \
555     .flags      = VMS_POINTER | VMS_VARRAY_INT32 | VMS_STRUCT,       \
556     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
557 }
558 
559 #define VMSTATE_STRUCT_VARRAY_POINTER_UINT16(_field, _state, _field_num, _vmsd, _type) { \
560     .name       = (stringify(_field)),                               \
561     .version_id = 0,                                                 \
562     .num_offset = vmstate_offset_value(_state, _field_num, uint16_t),\
563     .size       = sizeof(_type),                                     \
564     .vmsd       = &(_vmsd),                                          \
565     .flags      = VMS_POINTER | VMS_VARRAY_UINT16 | VMS_STRUCT,      \
566     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
567 }
568 
569 #define VMSTATE_STRUCT_VARRAY_INT32(_field, _state, _field_num, _version, _vmsd, _type) { \
570     .name       = (stringify(_field)),                               \
571     .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
572     .version_id = (_version),                                        \
573     .vmsd       = &(_vmsd),                                          \
574     .size       = sizeof(_type),                                     \
575     .flags      = VMS_STRUCT|VMS_VARRAY_INT32,                       \
576     .offset     = offsetof(_state, _field),                          \
577 }
578 
579 #define VMSTATE_STRUCT_VARRAY_UINT32(_field, _state, _field_num, _version, _vmsd, _type) { \
580     .name       = (stringify(_field)),                               \
581     .num_offset = vmstate_offset_value(_state, _field_num, uint32_t), \
582     .version_id = (_version),                                        \
583     .vmsd       = &(_vmsd),                                          \
584     .size       = sizeof(_type),                                     \
585     .flags      = VMS_STRUCT|VMS_VARRAY_UINT32,                      \
586     .offset     = offsetof(_state, _field),                          \
587 }
588 
589 #define VMSTATE_STRUCT_VARRAY_ALLOC(_field, _state, _field_num, _version, _vmsd, _type) {\
590     .name       = (stringify(_field)),                               \
591     .version_id = (_version),                                        \
592     .vmsd       = &(_vmsd),                                          \
593     .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
594     .size       = sizeof(_type),                                     \
595     .flags      = VMS_STRUCT|VMS_VARRAY_INT32|VMS_ALLOC|VMS_POINTER, \
596     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
597 }
598 
599 #define VMSTATE_STATIC_BUFFER(_field, _state, _version, _test, _start, _size) { \
600     .name         = (stringify(_field)),                             \
601     .version_id   = (_version),                                      \
602     .field_exists = (_test),                                         \
603     .size         = (_size - _start),                                \
604     .info         = &vmstate_info_buffer,                            \
605     .flags        = VMS_BUFFER,                                      \
606     .offset       = vmstate_offset_buffer(_state, _field) + _start,  \
607 }
608 
609 #define VMSTATE_VBUFFER_MULTIPLY(_field, _state, _version, _test,    \
610                                  _field_size, _multiply) {           \
611     .name         = (stringify(_field)),                             \
612     .version_id   = (_version),                                      \
613     .field_exists = (_test),                                         \
614     .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
615     .size         = (_multiply),                                      \
616     .info         = &vmstate_info_buffer,                            \
617     .flags        = VMS_VBUFFER|VMS_POINTER|VMS_MULTIPLY,            \
618     .offset       = offsetof(_state, _field),                        \
619 }
620 
621 #define VMSTATE_VBUFFER(_field, _state, _version, _test, _field_size) { \
622     .name         = (stringify(_field)),                             \
623     .version_id   = (_version),                                      \
624     .field_exists = (_test),                                         \
625     .size_offset  = vmstate_offset_value(_state, _field_size, int32_t),\
626     .info         = &vmstate_info_buffer,                            \
627     .flags        = VMS_VBUFFER|VMS_POINTER,                         \
628     .offset       = offsetof(_state, _field),                        \
629 }
630 
631 #define VMSTATE_VBUFFER_UINT32(_field, _state, _version, _test, _field_size) { \
632     .name         = (stringify(_field)),                             \
633     .version_id   = (_version),                                      \
634     .field_exists = (_test),                                         \
635     .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
636     .info         = &vmstate_info_buffer,                            \
637     .flags        = VMS_VBUFFER|VMS_POINTER,                         \
638     .offset       = offsetof(_state, _field),                        \
639 }
640 
641 #define VMSTATE_VBUFFER_ALLOC_UINT32(_field, _state, _version,       \
642                                      _test, _field_size) {           \
643     .name         = (stringify(_field)),                             \
644     .version_id   = (_version),                                      \
645     .field_exists = (_test),                                         \
646     .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
647     .info         = &vmstate_info_buffer,                            \
648     .flags        = VMS_VBUFFER|VMS_POINTER|VMS_ALLOC,               \
649     .offset       = offsetof(_state, _field),                        \
650 }
651 
652 #define VMSTATE_BUFFER_UNSAFE_INFO_TEST(_field, _state, _test, _version, _info, _size) { \
653     .name       = (stringify(_field)),                               \
654     .version_id = (_version),                                        \
655     .field_exists = (_test),                                         \
656     .size       = (_size),                                           \
657     .info       = &(_info),                                          \
658     .flags      = VMS_BUFFER,                                        \
659     .offset     = offsetof(_state, _field),                          \
660 }
661 
662 #define VMSTATE_BUFFER_POINTER_UNSAFE(_field, _state, _version, _size) { \
663     .name       = (stringify(_field)),                               \
664     .version_id = (_version),                                        \
665     .size       = (_size),                                           \
666     .info       = &vmstate_info_buffer,                              \
667     .flags      = VMS_BUFFER|VMS_POINTER,                            \
668     .offset     = offsetof(_state, _field),                          \
669 }
670 
671 /* Allocate a temporary of type 'tmp_type', set tmp->parent to _state
672  * and execute the vmsd on the temporary.  Note that we're working with
673  * the whole of _state here, not a field within it.
674  * We compile time check that:
675  *    That _tmp_type contains a 'parent' member that's a pointer to the
676  *        '_state' type
677  *    That the pointer is right at the start of _tmp_type.
678  */
679 #define VMSTATE_WITH_TMP(_state, _tmp_type, _vmsd) {                 \
680     .name         = "tmp",                                           \
681     .size         = sizeof(_tmp_type) +                              \
682                     QEMU_BUILD_BUG_ON_ZERO(offsetof(_tmp_type, parent) != 0) + \
683                     type_check_pointer(_state,                       \
684                         typeof_field(_tmp_type, parent)),            \
685     .vmsd         = &(_vmsd),                                        \
686     .info         = &vmstate_info_tmp,                               \
687 }
688 
689 #define VMSTATE_UNUSED_BUFFER(_test, _version, _size) {              \
690     .name         = "unused",                                        \
691     .field_exists = (_test),                                         \
692     .version_id   = (_version),                                      \
693     .size         = (_size),                                         \
694     .info         = &vmstate_info_unused_buffer,                     \
695     .flags        = VMS_BUFFER,                                      \
696 }
697 
698 /* Discard size * field_num bytes, where field_num is a uint32 member */
699 #define VMSTATE_UNUSED_VARRAY_UINT32(_state, _test, _version, _field_num, _size) {\
700     .name         = "unused",                                        \
701     .field_exists = (_test),                                         \
702     .num_offset   = vmstate_offset_value(_state, _field_num, uint32_t),\
703     .version_id   = (_version),                                      \
704     .size         = (_size),                                         \
705     .info         = &vmstate_info_unused_buffer,                     \
706     .flags        = VMS_VARRAY_UINT32 | VMS_BUFFER,                  \
707 }
708 
709 /* _field_size should be a int32_t field in the _state struct giving the
710  * size of the bitmap _field in bits.
711  */
712 #define VMSTATE_BITMAP(_field, _state, _version, _field_size) {      \
713     .name         = (stringify(_field)),                             \
714     .version_id   = (_version),                                      \
715     .size_offset  = vmstate_offset_value(_state, _field_size, int32_t),\
716     .info         = &vmstate_info_bitmap,                            \
717     .flags        = VMS_VBUFFER|VMS_POINTER,                         \
718     .offset       = offsetof(_state, _field),                        \
719 }
720 
721 /* For migrating a QTAILQ.
722  * Target QTAILQ needs be properly initialized.
723  * _type: type of QTAILQ element
724  * _next: name of QTAILQ entry field in QTAILQ element
725  * _vmsd: VMSD for QTAILQ element
726  * size: size of QTAILQ element
727  * start: offset of QTAILQ entry in QTAILQ element
728  */
729 #define VMSTATE_QTAILQ_V(_field, _state, _version, _vmsd, _type, _next)  \
730 {                                                                        \
731     .name         = (stringify(_field)),                                 \
732     .version_id   = (_version),                                          \
733     .vmsd         = &(_vmsd),                                            \
734     .size         = sizeof(_type),                                       \
735     .info         = &vmstate_info_qtailq,                                \
736     .offset       = offsetof(_state, _field),                            \
737     .start        = offsetof(_type, _next),                              \
738 }
739 
740 /* _f : field name
741    _f_n : num of elements field_name
742    _n : num of elements
743    _s : struct state name
744    _v : version
745 */
746 
747 #define VMSTATE_SINGLE(_field, _state, _version, _info, _type)        \
748     VMSTATE_SINGLE_TEST(_field, _state, NULL, _version, _info, _type)
749 
750 #define VMSTATE_STRUCT(_field, _state, _version, _vmsd, _type)        \
751     VMSTATE_STRUCT_TEST(_field, _state, NULL, _version, _vmsd, _type)
752 
753 #define VMSTATE_STRUCT_POINTER(_field, _state, _vmsd, _type)          \
754     VMSTATE_STRUCT_POINTER_V(_field, _state, 0, _vmsd, _type)
755 
756 #define VMSTATE_STRUCT_POINTER_TEST(_field, _state, _test, _vmsd, _type)     \
757     VMSTATE_STRUCT_POINTER_TEST_V(_field, _state, _test, 0, _vmsd, _type)
758 
759 #define VMSTATE_STRUCT_ARRAY(_field, _state, _num, _version, _vmsd, _type) \
760     VMSTATE_STRUCT_ARRAY_TEST(_field, _state, _num, NULL, _version,   \
761             _vmsd, _type)
762 
763 #define VMSTATE_STRUCT_2DARRAY(_field, _state, _n1, _n2, _version,    \
764             _vmsd, _type)                                             \
765     VMSTATE_STRUCT_2DARRAY_TEST(_field, _state, _n1, _n2, NULL,       \
766             _version, _vmsd, _type)
767 
768 #define VMSTATE_BUFFER_UNSAFE_INFO(_field, _state, _version, _info, _size) \
769     VMSTATE_BUFFER_UNSAFE_INFO_TEST(_field, _state, NULL, _version, _info, \
770             _size)
771 
772 #define VMSTATE_BOOL_V(_f, _s, _v)                                    \
773     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_bool, bool)
774 
775 #define VMSTATE_INT8_V(_f, _s, _v)                                    \
776     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int8, int8_t)
777 #define VMSTATE_INT16_V(_f, _s, _v)                                   \
778     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int16, int16_t)
779 #define VMSTATE_INT32_V(_f, _s, _v)                                   \
780     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int32, int32_t)
781 #define VMSTATE_INT64_V(_f, _s, _v)                                   \
782     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int64, int64_t)
783 
784 #define VMSTATE_UINT8_V(_f, _s, _v)                                   \
785     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint8, uint8_t)
786 #define VMSTATE_UINT16_V(_f, _s, _v)                                  \
787     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint16, uint16_t)
788 #define VMSTATE_UINT32_V(_f, _s, _v)                                  \
789     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint32, uint32_t)
790 #define VMSTATE_UINT64_V(_f, _s, _v)                                  \
791     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint64, uint64_t)
792 
793 #define VMSTATE_BOOL(_f, _s)                                          \
794     VMSTATE_BOOL_V(_f, _s, 0)
795 
796 #define VMSTATE_INT8(_f, _s)                                          \
797     VMSTATE_INT8_V(_f, _s, 0)
798 #define VMSTATE_INT16(_f, _s)                                         \
799     VMSTATE_INT16_V(_f, _s, 0)
800 #define VMSTATE_INT32(_f, _s)                                         \
801     VMSTATE_INT32_V(_f, _s, 0)
802 #define VMSTATE_INT64(_f, _s)                                         \
803     VMSTATE_INT64_V(_f, _s, 0)
804 
805 #define VMSTATE_UINT8(_f, _s)                                         \
806     VMSTATE_UINT8_V(_f, _s, 0)
807 #define VMSTATE_UINT16(_f, _s)                                        \
808     VMSTATE_UINT16_V(_f, _s, 0)
809 #define VMSTATE_UINT32(_f, _s)                                        \
810     VMSTATE_UINT32_V(_f, _s, 0)
811 #define VMSTATE_UINT64(_f, _s)                                        \
812     VMSTATE_UINT64_V(_f, _s, 0)
813 
814 #define VMSTATE_UINT8_EQUAL(_f, _s)                                   \
815     VMSTATE_SINGLE(_f, _s, 0, vmstate_info_uint8_equal, uint8_t)
816 
817 #define VMSTATE_UINT16_EQUAL(_f, _s)                                  \
818     VMSTATE_SINGLE(_f, _s, 0, vmstate_info_uint16_equal, uint16_t)
819 
820 #define VMSTATE_UINT16_EQUAL_V(_f, _s, _v)                            \
821     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint16_equal, uint16_t)
822 
823 #define VMSTATE_INT32_EQUAL(_f, _s)                                   \
824     VMSTATE_SINGLE(_f, _s, 0, vmstate_info_int32_equal, int32_t)
825 
826 #define VMSTATE_UINT32_EQUAL_V(_f, _s, _v)                            \
827     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint32_equal, uint32_t)
828 
829 #define VMSTATE_UINT32_EQUAL(_f, _s)                                  \
830     VMSTATE_UINT32_EQUAL_V(_f, _s, 0)
831 
832 #define VMSTATE_UINT64_EQUAL_V(_f, _s, _v)                            \
833     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint64_equal, uint64_t)
834 
835 #define VMSTATE_UINT64_EQUAL(_f, _s)                                  \
836     VMSTATE_UINT64_EQUAL_V(_f, _s, 0)
837 
838 #define VMSTATE_INT32_POSITIVE_LE(_f, _s)                             \
839     VMSTATE_SINGLE(_f, _s, 0, vmstate_info_int32_le, int32_t)
840 
841 #define VMSTATE_INT8_TEST(_f, _s, _t)                               \
842     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_int8, int8_t)
843 
844 #define VMSTATE_INT16_TEST(_f, _s, _t)                               \
845     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_int16, int16_t)
846 
847 #define VMSTATE_INT32_TEST(_f, _s, _t)                                  \
848     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_int32, int32_t)
849 
850 #define VMSTATE_INT64_TEST(_f, _s, _t)                                  \
851     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_int64, int64_t)
852 
853 #define VMSTATE_UINT8_TEST(_f, _s, _t)                               \
854     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint8, uint8_t)
855 
856 #define VMSTATE_UINT16_TEST(_f, _s, _t)                               \
857     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint16, uint16_t)
858 
859 #define VMSTATE_UINT32_TEST(_f, _s, _t)                                  \
860     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint32, uint32_t)
861 
862 #define VMSTATE_UINT64_TEST(_f, _s, _t)                                  \
863     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint64, uint64_t)
864 
865 
866 #define VMSTATE_FLOAT64_V(_f, _s, _v)                                 \
867     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_float64, float64)
868 
869 #define VMSTATE_FLOAT64(_f, _s)                                       \
870     VMSTATE_FLOAT64_V(_f, _s, 0)
871 
872 #define VMSTATE_TIMER_PTR_TEST(_f, _s, _test)                             \
873     VMSTATE_POINTER_TEST(_f, _s, _test, vmstate_info_timer, QEMUTimer *)
874 
875 #define VMSTATE_TIMER_PTR_V(_f, _s, _v)                                   \
876     VMSTATE_POINTER(_f, _s, _v, vmstate_info_timer, QEMUTimer *)
877 
878 #define VMSTATE_TIMER_PTR(_f, _s)                                         \
879     VMSTATE_TIMER_PTR_V(_f, _s, 0)
880 
881 #define VMSTATE_TIMER_PTR_ARRAY(_f, _s, _n)                              \
882     VMSTATE_ARRAY_OF_POINTER(_f, _s, _n, 0, vmstate_info_timer, QEMUTimer *)
883 
884 #define VMSTATE_TIMER_TEST(_f, _s, _test)                             \
885     VMSTATE_SINGLE_TEST(_f, _s, _test, 0, vmstate_info_timer, QEMUTimer)
886 
887 #define VMSTATE_TIMER_V(_f, _s, _v)                                   \
888     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_timer, QEMUTimer)
889 
890 #define VMSTATE_TIMER(_f, _s)                                         \
891     VMSTATE_TIMER_V(_f, _s, 0)
892 
893 #define VMSTATE_TIMER_ARRAY(_f, _s, _n)                              \
894     VMSTATE_ARRAY(_f, _s, _n, 0, vmstate_info_timer, QEMUTimer)
895 
896 #define VMSTATE_BOOL_ARRAY_V(_f, _s, _n, _v)                         \
897     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_bool, bool)
898 
899 #define VMSTATE_BOOL_ARRAY(_f, _s, _n)                               \
900     VMSTATE_BOOL_ARRAY_V(_f, _s, _n, 0)
901 
902 #define VMSTATE_UINT16_ARRAY_V(_f, _s, _n, _v)                         \
903     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint16, uint16_t)
904 
905 #define VMSTATE_UINT16_2DARRAY_V(_f, _s, _n1, _n2, _v)                \
906     VMSTATE_2DARRAY(_f, _s, _n1, _n2, _v, vmstate_info_uint16, uint16_t)
907 
908 #define VMSTATE_UINT16_ARRAY(_f, _s, _n)                               \
909     VMSTATE_UINT16_ARRAY_V(_f, _s, _n, 0)
910 
911 #define VMSTATE_UINT16_2DARRAY(_f, _s, _n1, _n2)                      \
912     VMSTATE_UINT16_2DARRAY_V(_f, _s, _n1, _n2, 0)
913 
914 #define VMSTATE_UINT8_2DARRAY_V(_f, _s, _n1, _n2, _v)                 \
915     VMSTATE_2DARRAY(_f, _s, _n1, _n2, _v, vmstate_info_uint8, uint8_t)
916 
917 #define VMSTATE_UINT8_ARRAY_V(_f, _s, _n, _v)                         \
918     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint8, uint8_t)
919 
920 #define VMSTATE_UINT8_ARRAY(_f, _s, _n)                               \
921     VMSTATE_UINT8_ARRAY_V(_f, _s, _n, 0)
922 
923 #define VMSTATE_UINT8_SUB_ARRAY(_f, _s, _start, _num)                \
924     VMSTATE_SUB_ARRAY(_f, _s, _start, _num, 0, vmstate_info_uint8, uint8_t)
925 
926 #define VMSTATE_UINT8_2DARRAY(_f, _s, _n1, _n2)                       \
927     VMSTATE_UINT8_2DARRAY_V(_f, _s, _n1, _n2, 0)
928 
929 #define VMSTATE_UINT32_ARRAY_V(_f, _s, _n, _v)                        \
930     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint32, uint32_t)
931 
932 #define VMSTATE_UINT32_2DARRAY_V(_f, _s, _n1, _n2, _v)                \
933     VMSTATE_2DARRAY(_f, _s, _n1, _n2, _v, vmstate_info_uint32, uint32_t)
934 
935 #define VMSTATE_UINT32_ARRAY(_f, _s, _n)                              \
936     VMSTATE_UINT32_ARRAY_V(_f, _s, _n, 0)
937 
938 #define VMSTATE_UINT32_2DARRAY(_f, _s, _n1, _n2)                      \
939     VMSTATE_UINT32_2DARRAY_V(_f, _s, _n1, _n2, 0)
940 
941 #define VMSTATE_UINT64_ARRAY_V(_f, _s, _n, _v)                        \
942     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint64, uint64_t)
943 
944 #define VMSTATE_UINT64_ARRAY(_f, _s, _n)                              \
945     VMSTATE_UINT64_ARRAY_V(_f, _s, _n, 0)
946 
947 #define VMSTATE_UINT64_2DARRAY(_f, _s, _n1, _n2)                      \
948     VMSTATE_UINT64_2DARRAY_V(_f, _s, _n1, _n2, 0)
949 
950 #define VMSTATE_UINT64_2DARRAY_V(_f, _s, _n1, _n2, _v)                 \
951     VMSTATE_2DARRAY(_f, _s, _n1, _n2, _v, vmstate_info_uint64, uint64_t)
952 
953 #define VMSTATE_INT16_ARRAY_V(_f, _s, _n, _v)                         \
954     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_int16, int16_t)
955 
956 #define VMSTATE_INT16_ARRAY(_f, _s, _n)                               \
957     VMSTATE_INT16_ARRAY_V(_f, _s, _n, 0)
958 
959 #define VMSTATE_INT32_ARRAY_V(_f, _s, _n, _v)                         \
960     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_int32, int32_t)
961 
962 #define VMSTATE_INT32_ARRAY(_f, _s, _n)                               \
963     VMSTATE_INT32_ARRAY_V(_f, _s, _n, 0)
964 
965 #define VMSTATE_UINT32_SUB_ARRAY(_f, _s, _start, _num)                \
966     VMSTATE_SUB_ARRAY(_f, _s, _start, _num, 0, vmstate_info_uint32, uint32_t)
967 
968 #define VMSTATE_INT64_ARRAY_V(_f, _s, _n, _v)                         \
969     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_int64, int64_t)
970 
971 #define VMSTATE_INT64_ARRAY(_f, _s, _n)                               \
972     VMSTATE_INT64_ARRAY_V(_f, _s, _n, 0)
973 
974 #define VMSTATE_FLOAT64_ARRAY_V(_f, _s, _n, _v)                       \
975     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_float64, float64)
976 
977 #define VMSTATE_FLOAT64_ARRAY(_f, _s, _n)                             \
978     VMSTATE_FLOAT64_ARRAY_V(_f, _s, _n, 0)
979 
980 #define VMSTATE_CPUDOUBLE_ARRAY_V(_f, _s, _n, _v)                     \
981     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_cpudouble, CPU_DoubleU)
982 
983 #define VMSTATE_CPUDOUBLE_ARRAY(_f, _s, _n)                           \
984     VMSTATE_CPUDOUBLE_ARRAY_V(_f, _s, _n, 0)
985 
986 #define VMSTATE_BUFFER_V(_f, _s, _v)                                  \
987     VMSTATE_STATIC_BUFFER(_f, _s, _v, NULL, 0, sizeof(typeof_field(_s, _f)))
988 
989 #define VMSTATE_BUFFER(_f, _s)                                        \
990     VMSTATE_BUFFER_V(_f, _s, 0)
991 
992 #define VMSTATE_PARTIAL_BUFFER(_f, _s, _size)                         \
993     VMSTATE_STATIC_BUFFER(_f, _s, 0, NULL, 0, _size)
994 
995 #define VMSTATE_BUFFER_START_MIDDLE_V(_f, _s, _start, _v) \
996     VMSTATE_STATIC_BUFFER(_f, _s, _v, NULL, _start, sizeof(typeof_field(_s, _f)))
997 
998 #define VMSTATE_BUFFER_START_MIDDLE(_f, _s, _start) \
999     VMSTATE_BUFFER_START_MIDDLE_V(_f, _s, _start, 0)
1000 
1001 #define VMSTATE_PARTIAL_VBUFFER(_f, _s, _size)                        \
1002     VMSTATE_VBUFFER(_f, _s, 0, NULL, _size)
1003 
1004 #define VMSTATE_PARTIAL_VBUFFER_UINT32(_f, _s, _size)                        \
1005     VMSTATE_VBUFFER_UINT32(_f, _s, 0, NULL, _size)
1006 
1007 #define VMSTATE_BUFFER_TEST(_f, _s, _test)                            \
1008     VMSTATE_STATIC_BUFFER(_f, _s, 0, _test, 0, sizeof(typeof_field(_s, _f)))
1009 
1010 #define VMSTATE_BUFFER_UNSAFE(_field, _state, _version, _size)        \
1011     VMSTATE_BUFFER_UNSAFE_INFO(_field, _state, _version, vmstate_info_buffer, _size)
1012 
1013 #define VMSTATE_UNUSED_V(_v, _size)                                   \
1014     VMSTATE_UNUSED_BUFFER(NULL, _v, _size)
1015 
1016 #define VMSTATE_UNUSED(_size)                                         \
1017     VMSTATE_UNUSED_V(0, _size)
1018 
1019 #define VMSTATE_UNUSED_TEST(_test, _size)                             \
1020     VMSTATE_UNUSED_BUFFER(_test, 0, _size)
1021 
1022 #define VMSTATE_END_OF_LIST()                                         \
1023     {}
1024 
1025 #define SELF_ANNOUNCE_ROUNDS 5
1026 
1027 void loadvm_free_handlers(MigrationIncomingState *mis);
1028 
1029 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1030                        void *opaque, int version_id);
1031 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
1032                         void *opaque, QJSON *vmdesc);
1033 
1034 bool vmstate_save_needed(const VMStateDescription *vmsd, void *opaque);
1035 
1036 /* Returns: 0 on success, -1 on failure */
1037 int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
1038                                    const VMStateDescription *vmsd,
1039                                    void *base, int alias_id,
1040                                    int required_for_version,
1041                                    Error **errp);
1042 
1043 /* Returns: 0 on success, -1 on failure */
1044 static inline int vmstate_register(DeviceState *dev, int instance_id,
1045                                    const VMStateDescription *vmsd,
1046                                    void *opaque)
1047 {
1048     return vmstate_register_with_alias_id(dev, instance_id, vmsd,
1049                                           opaque, -1, 0, NULL);
1050 }
1051 
1052 void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
1053                         void *opaque);
1054 
1055 struct MemoryRegion;
1056 void vmstate_register_ram(struct MemoryRegion *memory, DeviceState *dev);
1057 void vmstate_unregister_ram(struct MemoryRegion *memory, DeviceState *dev);
1058 void vmstate_register_ram_global(struct MemoryRegion *memory);
1059 
1060 static inline
1061 int64_t self_announce_delay(int round)
1062 {
1063     assert(round < SELF_ANNOUNCE_ROUNDS && round > 0);
1064     /* delay 50ms, 150ms, 250ms, ... */
1065     return 50 + (SELF_ANNOUNCE_ROUNDS - round - 1) * 100;
1066 }
1067 
1068 void dump_vmstate_json_to_file(FILE *out_fp);
1069 
1070 bool vmstate_check_only_migratable(const VMStateDescription *vmsd);
1071 
1072 #endif
1073