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