xref: /openbmc/qemu/include/migration/vmstate.h (revision d341d9f3)
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 #ifndef QEMU_VMSTATE_H
27 #define QEMU_VMSTATE_H 1
28 
29 #ifndef CONFIG_USER_ONLY
30 #include <migration/qemu-file.h>
31 #endif
32 #include <qjson.h>
33 
34 typedef void SaveStateHandler(QEMUFile *f, void *opaque);
35 typedef int LoadStateHandler(QEMUFile *f, void *opaque, int version_id);
36 
37 typedef struct SaveVMHandlers {
38     /* This runs inside the iothread lock.  */
39     void (*set_params)(const MigrationParams *params, void * opaque);
40     SaveStateHandler *save_state;
41 
42     void (*cleanup)(void *opaque);
43     int (*save_live_complete_postcopy)(QEMUFile *f, void *opaque);
44     int (*save_live_complete_precopy)(QEMUFile *f, void *opaque);
45 
46     /* This runs both outside and inside the iothread lock.  */
47     bool (*is_active)(void *opaque);
48 
49     /* This runs outside the iothread lock in the migration case, and
50      * within the lock in the savevm case.  The callback had better only
51      * use data that is local to the migration thread or protected
52      * by other locks.
53      */
54     int (*save_live_iterate)(QEMUFile *f, void *opaque);
55 
56     /* This runs outside the iothread lock!  */
57     int (*save_live_setup)(QEMUFile *f, void *opaque);
58     void (*save_live_pending)(QEMUFile *f, void *opaque, uint64_t max_size,
59                               uint64_t *non_postcopiable_pending,
60                               uint64_t *postcopiable_pending);
61     LoadStateHandler *load_state;
62 } SaveVMHandlers;
63 
64 int register_savevm(DeviceState *dev,
65                     const char *idstr,
66                     int instance_id,
67                     int version_id,
68                     SaveStateHandler *save_state,
69                     LoadStateHandler *load_state,
70                     void *opaque);
71 
72 int register_savevm_live(DeviceState *dev,
73                          const char *idstr,
74                          int instance_id,
75                          int version_id,
76                          SaveVMHandlers *ops,
77                          void *opaque);
78 
79 void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque);
80 
81 typedef struct VMStateInfo VMStateInfo;
82 typedef struct VMStateDescription VMStateDescription;
83 
84 struct VMStateInfo {
85     const char *name;
86     int (*get)(QEMUFile *f, void *pv, size_t size);
87     void (*put)(QEMUFile *f, void *pv, size_t size);
88 };
89 
90 enum VMStateFlags {
91     VMS_SINGLE           = 0x001,
92     VMS_POINTER          = 0x002,
93     VMS_ARRAY            = 0x004,
94     VMS_STRUCT           = 0x008,
95     VMS_VARRAY_INT32     = 0x010,  /* Array with size in int32_t field*/
96     VMS_BUFFER           = 0x020,  /* static sized buffer */
97     VMS_ARRAY_OF_POINTER = 0x040,
98     VMS_VARRAY_UINT16    = 0x080,  /* Array with size in uint16_t field */
99     VMS_VBUFFER          = 0x100,  /* Buffer with size in int32_t field */
100     VMS_MULTIPLY         = 0x200,  /* multiply "size" field by field_size */
101     VMS_VARRAY_UINT8     = 0x400,  /* Array with size in uint8_t field*/
102     VMS_VARRAY_UINT32    = 0x800,  /* Array with size in uint32_t field*/
103     VMS_MUST_EXIST       = 0x1000, /* Field must exist in input */
104     VMS_ALLOC            = 0x2000, /* Alloc a buffer on the destination */
105     VMS_MULTIPLY_ELEMENTS = 0x4000, /* multiply varray size by field->num */
106 };
107 
108 typedef struct {
109     const char *name;
110     size_t offset;
111     size_t size;
112     size_t start;
113     int num;
114     size_t num_offset;
115     size_t size_offset;
116     const VMStateInfo *info;
117     enum VMStateFlags flags;
118     const VMStateDescription *vmsd;
119     int version_id;
120     bool (*field_exists)(void *opaque, int version_id);
121 } VMStateField;
122 
123 struct VMStateDescription {
124     const char *name;
125     int unmigratable;
126     int version_id;
127     int minimum_version_id;
128     int minimum_version_id_old;
129     LoadStateHandler *load_state_old;
130     int (*pre_load)(void *opaque);
131     int (*post_load)(void *opaque, int version_id);
132     void (*pre_save)(void *opaque);
133     bool (*needed)(void *opaque);
134     VMStateField *fields;
135     const VMStateDescription **subsections;
136 };
137 
138 extern const VMStateDescription vmstate_dummy;
139 
140 extern const VMStateInfo vmstate_info_bool;
141 
142 extern const VMStateInfo vmstate_info_int8;
143 extern const VMStateInfo vmstate_info_int16;
144 extern const VMStateInfo vmstate_info_int32;
145 extern const VMStateInfo vmstate_info_int64;
146 
147 extern const VMStateInfo vmstate_info_uint8_equal;
148 extern const VMStateInfo vmstate_info_uint16_equal;
149 extern const VMStateInfo vmstate_info_int32_equal;
150 extern const VMStateInfo vmstate_info_uint32_equal;
151 extern const VMStateInfo vmstate_info_uint64_equal;
152 extern const VMStateInfo vmstate_info_int32_le;
153 
154 extern const VMStateInfo vmstate_info_uint8;
155 extern const VMStateInfo vmstate_info_uint16;
156 extern const VMStateInfo vmstate_info_uint32;
157 extern const VMStateInfo vmstate_info_uint64;
158 
159 extern const VMStateInfo vmstate_info_float64;
160 extern const VMStateInfo vmstate_info_cpudouble;
161 
162 extern const VMStateInfo vmstate_info_timer;
163 extern const VMStateInfo vmstate_info_buffer;
164 extern const VMStateInfo vmstate_info_unused_buffer;
165 extern const VMStateInfo vmstate_info_bitmap;
166 
167 #define type_check_2darray(t1,t2,n,m) ((t1(*)[n][m])0 - (t2*)0)
168 #define type_check_array(t1,t2,n) ((t1(*)[n])0 - (t2*)0)
169 #define type_check_pointer(t1,t2) ((t1**)0 - (t2*)0)
170 
171 #define vmstate_offset_value(_state, _field, _type)                  \
172     (offsetof(_state, _field) +                                      \
173      type_check(_type, typeof_field(_state, _field)))
174 
175 #define vmstate_offset_pointer(_state, _field, _type)                \
176     (offsetof(_state, _field) +                                      \
177      type_check_pointer(_type, typeof_field(_state, _field)))
178 
179 #define vmstate_offset_array(_state, _field, _type, _num)            \
180     (offsetof(_state, _field) +                                      \
181      type_check_array(_type, typeof_field(_state, _field), _num))
182 
183 #define vmstate_offset_2darray(_state, _field, _type, _n1, _n2)      \
184     (offsetof(_state, _field) +                                      \
185      type_check_2darray(_type, typeof_field(_state, _field), _n1, _n2))
186 
187 #define vmstate_offset_sub_array(_state, _field, _type, _start)      \
188     vmstate_offset_value(_state, _field[_start], _type)
189 
190 #define vmstate_offset_buffer(_state, _field)                        \
191     vmstate_offset_array(_state, _field, uint8_t,                    \
192                          sizeof(typeof_field(_state, _field)))
193 
194 #define VMSTATE_SINGLE_TEST(_field, _state, _test, _version, _info, _type) { \
195     .name         = (stringify(_field)),                             \
196     .version_id   = (_version),                                      \
197     .field_exists = (_test),                                         \
198     .size         = sizeof(_type),                                   \
199     .info         = &(_info),                                        \
200     .flags        = VMS_SINGLE,                                      \
201     .offset       = vmstate_offset_value(_state, _field, _type),     \
202 }
203 
204 /* Validate state using a boolean predicate. */
205 #define VMSTATE_VALIDATE(_name, _test) { \
206     .name         = (_name),                                         \
207     .field_exists = (_test),                                         \
208     .flags        = VMS_ARRAY | VMS_MUST_EXIST,                      \
209     .num          = 0, /* 0 elements: no data, only run _test */     \
210 }
211 
212 #define VMSTATE_POINTER(_field, _state, _version, _info, _type) {    \
213     .name       = (stringify(_field)),                               \
214     .version_id = (_version),                                        \
215     .info       = &(_info),                                          \
216     .size       = sizeof(_type),                                     \
217     .flags      = VMS_SINGLE|VMS_POINTER,                            \
218     .offset     = vmstate_offset_value(_state, _field, _type),       \
219 }
220 
221 #define VMSTATE_POINTER_TEST(_field, _state, _test, _info, _type) {  \
222     .name       = (stringify(_field)),                               \
223     .info       = &(_info),                                          \
224     .field_exists = (_test),                                         \
225     .size       = sizeof(_type),                                     \
226     .flags      = VMS_SINGLE|VMS_POINTER,                            \
227     .offset     = vmstate_offset_value(_state, _field, _type),       \
228 }
229 
230 #define VMSTATE_ARRAY(_field, _state, _num, _version, _info, _type) {\
231     .name       = (stringify(_field)),                               \
232     .version_id = (_version),                                        \
233     .num        = (_num),                                            \
234     .info       = &(_info),                                          \
235     .size       = sizeof(_type),                                     \
236     .flags      = VMS_ARRAY,                                         \
237     .offset     = vmstate_offset_array(_state, _field, _type, _num), \
238 }
239 
240 #define VMSTATE_2DARRAY(_field, _state, _n1, _n2, _version, _info, _type) { \
241     .name       = (stringify(_field)),                                      \
242     .version_id = (_version),                                               \
243     .num        = (_n1) * (_n2),                                            \
244     .info       = &(_info),                                                 \
245     .size       = sizeof(_type),                                            \
246     .flags      = VMS_ARRAY,                                                \
247     .offset     = vmstate_offset_2darray(_state, _field, _type, _n1, _n2),  \
248 }
249 
250 #define VMSTATE_VARRAY_MULTIPLY(_field, _state, _field_num, _multiply, _info, _type) { \
251     .name       = (stringify(_field)),                               \
252     .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
253     .num        = (_multiply),                                       \
254     .info       = &(_info),                                          \
255     .size       = sizeof(_type),                                     \
256     .flags      = VMS_VARRAY_UINT32|VMS_MULTIPLY_ELEMENTS,           \
257     .offset     = offsetof(_state, _field),                          \
258 }
259 
260 #define VMSTATE_ARRAY_TEST(_field, _state, _num, _test, _info, _type) {\
261     .name         = (stringify(_field)),                              \
262     .field_exists = (_test),                                          \
263     .num          = (_num),                                           \
264     .info         = &(_info),                                         \
265     .size         = sizeof(_type),                                    \
266     .flags        = VMS_ARRAY,                                        \
267     .offset       = vmstate_offset_array(_state, _field, _type, _num),\
268 }
269 
270 #define VMSTATE_SUB_ARRAY(_field, _state, _start, _num, _version, _info, _type) { \
271     .name       = (stringify(_field)),                               \
272     .version_id = (_version),                                        \
273     .num        = (_num),                                            \
274     .info       = &(_info),                                          \
275     .size       = sizeof(_type),                                     \
276     .flags      = VMS_ARRAY,                                         \
277     .offset     = vmstate_offset_sub_array(_state, _field, _type, _start), \
278 }
279 
280 #define VMSTATE_ARRAY_INT32_UNSAFE(_field, _state, _field_num, _info, _type) {\
281     .name       = (stringify(_field)),                               \
282     .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
283     .info       = &(_info),                                          \
284     .size       = sizeof(_type),                                     \
285     .flags      = VMS_VARRAY_INT32,                                  \
286     .offset     = offsetof(_state, _field),                          \
287 }
288 
289 #define VMSTATE_VARRAY_INT32(_field, _state, _field_num, _version, _info, _type) {\
290     .name       = (stringify(_field)),                               \
291     .version_id = (_version),                                        \
292     .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
293     .info       = &(_info),                                          \
294     .size       = sizeof(_type),                                     \
295     .flags      = VMS_VARRAY_INT32|VMS_POINTER,                      \
296     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
297 }
298 
299 #define VMSTATE_VARRAY_UINT32(_field, _state, _field_num, _version, _info, _type) {\
300     .name       = (stringify(_field)),                               \
301     .version_id = (_version),                                        \
302     .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
303     .info       = &(_info),                                          \
304     .size       = sizeof(_type),                                     \
305     .flags      = VMS_VARRAY_UINT32|VMS_POINTER,                     \
306     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
307 }
308 
309 #define VMSTATE_VARRAY_UINT16_UNSAFE(_field, _state, _field_num, _version, _info, _type) {\
310     .name       = (stringify(_field)),                               \
311     .version_id = (_version),                                        \
312     .num_offset = vmstate_offset_value(_state, _field_num, uint16_t),\
313     .info       = &(_info),                                          \
314     .size       = sizeof(_type),                                     \
315     .flags      = VMS_VARRAY_UINT16,                                 \
316     .offset     = offsetof(_state, _field),                          \
317 }
318 
319 #define VMSTATE_STRUCT_TEST(_field, _state, _test, _version, _vmsd, _type) { \
320     .name         = (stringify(_field)),                             \
321     .version_id   = (_version),                                      \
322     .field_exists = (_test),                                         \
323     .vmsd         = &(_vmsd),                                        \
324     .size         = sizeof(_type),                                   \
325     .flags        = VMS_STRUCT,                                      \
326     .offset       = vmstate_offset_value(_state, _field, _type),     \
327 }
328 
329 #define VMSTATE_STRUCT_POINTER_V(_field, _state, _version, _vmsd, _type) { \
330     .name         = (stringify(_field)),                             \
331     .version_id   = (_version),                                        \
332     .vmsd         = &(_vmsd),                                        \
333     .size         = sizeof(_type *),                                 \
334     .flags        = VMS_STRUCT|VMS_POINTER,                          \
335     .offset       = vmstate_offset_pointer(_state, _field, _type),   \
336 }
337 
338 #define VMSTATE_STRUCT_POINTER_TEST_V(_field, _state, _test, _version, _vmsd, _type) { \
339     .name         = (stringify(_field)),                             \
340     .version_id   = (_version),                                        \
341     .field_exists = (_test),                                         \
342     .vmsd         = &(_vmsd),                                        \
343     .size         = sizeof(_type *),                                 \
344     .flags        = VMS_STRUCT|VMS_POINTER,                          \
345     .offset       = vmstate_offset_pointer(_state, _field, _type),   \
346 }
347 
348 #define VMSTATE_ARRAY_OF_POINTER(_field, _state, _num, _version, _info, _type) {\
349     .name       = (stringify(_field)),                               \
350     .version_id = (_version),                                        \
351     .num        = (_num),                                            \
352     .info       = &(_info),                                          \
353     .size       = sizeof(_type),                                     \
354     .flags      = VMS_ARRAY|VMS_ARRAY_OF_POINTER,                    \
355     .offset     = vmstate_offset_array(_state, _field, _type, _num), \
356 }
357 
358 #define VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(_f, _s, _n, _v, _vmsd, _type) { \
359     .name       = (stringify(_f)),                                   \
360     .version_id = (_v),                                              \
361     .num        = (_n),                                              \
362     .vmsd       = &(_vmsd),                                          \
363     .size       = sizeof(_type *),                                    \
364     .flags      = VMS_ARRAY|VMS_STRUCT|VMS_ARRAY_OF_POINTER,         \
365     .offset     = vmstate_offset_array(_s, _f, _type*, _n),          \
366 }
367 
368 #define VMSTATE_STRUCT_SUB_ARRAY(_field, _state, _start, _num, _version, _vmsd, _type) { \
369     .name       = (stringify(_field)),                                     \
370     .version_id = (_version),                                              \
371     .num        = (_num),                                                  \
372     .vmsd       = &(_vmsd),                                                \
373     .size       = sizeof(_type),                                           \
374     .flags      = VMS_STRUCT|VMS_ARRAY,                                    \
375     .offset     = vmstate_offset_sub_array(_state, _field, _type, _start), \
376 }
377 
378 #define VMSTATE_STRUCT_ARRAY_TEST(_field, _state, _num, _test, _version, _vmsd, _type) { \
379     .name         = (stringify(_field)),                             \
380     .num          = (_num),                                          \
381     .field_exists = (_test),                                         \
382     .version_id   = (_version),                                      \
383     .vmsd         = &(_vmsd),                                        \
384     .size         = sizeof(_type),                                   \
385     .flags        = VMS_STRUCT|VMS_ARRAY,                            \
386     .offset       = vmstate_offset_array(_state, _field, _type, _num),\
387 }
388 
389 /* a variable length array (i.e. _type *_field) but we know the
390  * length
391  */
392 #define VMSTATE_STRUCT_VARRAY_KNOWN(_field, _state, _num, _version, _vmsd, _type) { \
393     .name       = (stringify(_field)),                               \
394     .num          = (_num),                                          \
395     .version_id = (_version),                                        \
396     .vmsd       = &(_vmsd),                                          \
397     .size       = sizeof(_type),                                     \
398     .flags      = VMS_STRUCT|VMS_ARRAY,                              \
399     .offset     = offsetof(_state, _field),                          \
400 }
401 
402 #define VMSTATE_STRUCT_VARRAY_UINT8(_field, _state, _field_num, _version, _vmsd, _type) { \
403     .name       = (stringify(_field)),                               \
404     .num_offset = vmstate_offset_value(_state, _field_num, uint8_t), \
405     .version_id = (_version),                                        \
406     .vmsd       = &(_vmsd),                                          \
407     .size       = sizeof(_type),                                     \
408     .flags      = VMS_STRUCT|VMS_VARRAY_UINT8,                       \
409     .offset     = offsetof(_state, _field),                          \
410 }
411 
412 #define VMSTATE_STRUCT_VARRAY_POINTER_INT32(_field, _state, _field_num, _vmsd, _type) { \
413     .name       = (stringify(_field)),                               \
414     .version_id = 0,                                                 \
415     .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
416     .size       = sizeof(_type),                                     \
417     .vmsd       = &(_vmsd),                                          \
418     .flags      = VMS_POINTER | VMS_VARRAY_INT32 | VMS_STRUCT,       \
419     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
420 }
421 
422 #define VMSTATE_STRUCT_VARRAY_POINTER_UINT32(_field, _state, _field_num, _vmsd, _type) { \
423     .name       = (stringify(_field)),                               \
424     .version_id = 0,                                                 \
425     .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
426     .size       = sizeof(_type),                                     \
427     .vmsd       = &(_vmsd),                                          \
428     .flags      = VMS_POINTER | VMS_VARRAY_INT32 | VMS_STRUCT,       \
429     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
430 }
431 
432 #define VMSTATE_STRUCT_VARRAY_POINTER_UINT16(_field, _state, _field_num, _vmsd, _type) { \
433     .name       = (stringify(_field)),                               \
434     .version_id = 0,                                                 \
435     .num_offset = vmstate_offset_value(_state, _field_num, uint16_t),\
436     .size       = sizeof(_type),                                     \
437     .vmsd       = &(_vmsd),                                          \
438     .flags      = VMS_POINTER | VMS_VARRAY_UINT16 | VMS_STRUCT,      \
439     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
440 }
441 
442 #define VMSTATE_STRUCT_VARRAY_INT32(_field, _state, _field_num, _version, _vmsd, _type) { \
443     .name       = (stringify(_field)),                               \
444     .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
445     .version_id = (_version),                                        \
446     .vmsd       = &(_vmsd),                                          \
447     .size       = sizeof(_type),                                     \
448     .flags      = VMS_STRUCT|VMS_VARRAY_INT32,                       \
449     .offset     = offsetof(_state, _field),                          \
450 }
451 
452 #define VMSTATE_STRUCT_VARRAY_UINT32(_field, _state, _field_num, _version, _vmsd, _type) { \
453     .name       = (stringify(_field)),                               \
454     .num_offset = vmstate_offset_value(_state, _field_num, uint32_t), \
455     .version_id = (_version),                                        \
456     .vmsd       = &(_vmsd),                                          \
457     .size       = sizeof(_type),                                     \
458     .flags      = VMS_STRUCT|VMS_VARRAY_UINT32,                      \
459     .offset     = offsetof(_state, _field),                          \
460 }
461 
462 #define VMSTATE_STRUCT_VARRAY_ALLOC(_field, _state, _field_num, _version, _vmsd, _type) {\
463     .name       = (stringify(_field)),                               \
464     .version_id = (_version),                                        \
465     .vmsd       = &(_vmsd),                                          \
466     .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
467     .size       = sizeof(_type),                                     \
468     .flags      = VMS_STRUCT|VMS_VARRAY_INT32|VMS_ALLOC|VMS_POINTER, \
469     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
470 }
471 
472 #define VMSTATE_STATIC_BUFFER(_field, _state, _version, _test, _start, _size) { \
473     .name         = (stringify(_field)),                             \
474     .version_id   = (_version),                                      \
475     .field_exists = (_test),                                         \
476     .size         = (_size - _start),                                \
477     .info         = &vmstate_info_buffer,                            \
478     .flags        = VMS_BUFFER,                                      \
479     .offset       = vmstate_offset_buffer(_state, _field) + _start,  \
480 }
481 
482 #define VMSTATE_VBUFFER_MULTIPLY(_field, _state, _version, _test, _start, _field_size, _multiply) { \
483     .name         = (stringify(_field)),                             \
484     .version_id   = (_version),                                      \
485     .field_exists = (_test),                                         \
486     .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
487     .size         = (_multiply),                                      \
488     .info         = &vmstate_info_buffer,                            \
489     .flags        = VMS_VBUFFER|VMS_POINTER|VMS_MULTIPLY,            \
490     .offset       = offsetof(_state, _field),                        \
491     .start        = (_start),                                        \
492 }
493 
494 #define VMSTATE_VBUFFER(_field, _state, _version, _test, _start, _field_size) { \
495     .name         = (stringify(_field)),                             \
496     .version_id   = (_version),                                      \
497     .field_exists = (_test),                                         \
498     .size_offset  = vmstate_offset_value(_state, _field_size, int32_t),\
499     .info         = &vmstate_info_buffer,                            \
500     .flags        = VMS_VBUFFER|VMS_POINTER,                         \
501     .offset       = offsetof(_state, _field),                        \
502     .start        = (_start),                                        \
503 }
504 
505 #define VMSTATE_VBUFFER_UINT32(_field, _state, _version, _test, _start, _field_size) { \
506     .name         = (stringify(_field)),                             \
507     .version_id   = (_version),                                      \
508     .field_exists = (_test),                                         \
509     .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
510     .info         = &vmstate_info_buffer,                            \
511     .flags        = VMS_VBUFFER|VMS_POINTER,                         \
512     .offset       = offsetof(_state, _field),                        \
513     .start        = (_start),                                        \
514 }
515 
516 #define VMSTATE_VBUFFER_ALLOC_UINT32(_field, _state, _version, _test, _start, _field_size) { \
517     .name         = (stringify(_field)),                             \
518     .version_id   = (_version),                                      \
519     .field_exists = (_test),                                         \
520     .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
521     .info         = &vmstate_info_buffer,                            \
522     .flags        = VMS_VBUFFER|VMS_POINTER|VMS_ALLOC,               \
523     .offset       = offsetof(_state, _field),                        \
524     .start        = (_start),                                        \
525 }
526 
527 #define VMSTATE_BUFFER_UNSAFE_INFO_TEST(_field, _state, _test, _version, _info, _size) { \
528     .name       = (stringify(_field)),                               \
529     .version_id = (_version),                                        \
530     .field_exists = (_test),                                         \
531     .size       = (_size),                                           \
532     .info       = &(_info),                                          \
533     .flags      = VMS_BUFFER,                                        \
534     .offset     = offsetof(_state, _field),                          \
535 }
536 
537 #define VMSTATE_BUFFER_POINTER_UNSAFE(_field, _state, _version, _size) { \
538     .name       = (stringify(_field)),                               \
539     .version_id = (_version),                                        \
540     .size       = (_size),                                           \
541     .info       = &vmstate_info_buffer,                              \
542     .flags      = VMS_BUFFER|VMS_POINTER,                            \
543     .offset     = offsetof(_state, _field),                          \
544 }
545 
546 #define VMSTATE_UNUSED_BUFFER(_test, _version, _size) {              \
547     .name         = "unused",                                        \
548     .field_exists = (_test),                                         \
549     .version_id   = (_version),                                      \
550     .size         = (_size),                                         \
551     .info         = &vmstate_info_unused_buffer,                     \
552     .flags        = VMS_BUFFER,                                      \
553 }
554 
555 /* _field_size should be a int32_t field in the _state struct giving the
556  * size of the bitmap _field in bits.
557  */
558 #define VMSTATE_BITMAP(_field, _state, _version, _field_size) {      \
559     .name         = (stringify(_field)),                             \
560     .version_id   = (_version),                                      \
561     .size_offset  = vmstate_offset_value(_state, _field_size, int32_t),\
562     .info         = &vmstate_info_bitmap,                            \
563     .flags        = VMS_VBUFFER|VMS_POINTER,                         \
564     .offset       = offsetof(_state, _field),                        \
565 }
566 
567 /* _f : field name
568    _f_n : num of elements field_name
569    _n : num of elements
570    _s : struct state name
571    _v : version
572 */
573 
574 #define VMSTATE_SINGLE(_field, _state, _version, _info, _type)        \
575     VMSTATE_SINGLE_TEST(_field, _state, NULL, _version, _info, _type)
576 
577 #define VMSTATE_STRUCT(_field, _state, _version, _vmsd, _type)        \
578     VMSTATE_STRUCT_TEST(_field, _state, NULL, _version, _vmsd, _type)
579 
580 #define VMSTATE_STRUCT_POINTER(_field, _state, _vmsd, _type)          \
581     VMSTATE_STRUCT_POINTER_V(_field, _state, 0, _vmsd, _type)
582 
583 #define VMSTATE_STRUCT_POINTER_TEST(_field, _state, _test, _vmsd, _type)     \
584     VMSTATE_STRUCT_POINTER_TEST_V(_field, _state, _test, 0, _vmsd, _type)
585 
586 #define VMSTATE_STRUCT_ARRAY(_field, _state, _num, _version, _vmsd, _type) \
587     VMSTATE_STRUCT_ARRAY_TEST(_field, _state, _num, NULL, _version,   \
588             _vmsd, _type)
589 
590 #define VMSTATE_BUFFER_UNSAFE_INFO(_field, _state, _version, _info, _size) \
591     VMSTATE_BUFFER_UNSAFE_INFO_TEST(_field, _state, NULL, _version, _info, \
592             _size)
593 
594 #define VMSTATE_BOOL_V(_f, _s, _v)                                    \
595     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_bool, bool)
596 
597 #define VMSTATE_INT8_V(_f, _s, _v)                                    \
598     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int8, int8_t)
599 #define VMSTATE_INT16_V(_f, _s, _v)                                   \
600     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int16, int16_t)
601 #define VMSTATE_INT32_V(_f, _s, _v)                                   \
602     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int32, int32_t)
603 #define VMSTATE_INT64_V(_f, _s, _v)                                   \
604     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int64, int64_t)
605 
606 #define VMSTATE_UINT8_V(_f, _s, _v)                                   \
607     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint8, uint8_t)
608 #define VMSTATE_UINT16_V(_f, _s, _v)                                  \
609     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint16, uint16_t)
610 #define VMSTATE_UINT32_V(_f, _s, _v)                                  \
611     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint32, uint32_t)
612 #define VMSTATE_UINT64_V(_f, _s, _v)                                  \
613     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint64, uint64_t)
614 
615 #define VMSTATE_BOOL(_f, _s)                                          \
616     VMSTATE_BOOL_V(_f, _s, 0)
617 
618 #define VMSTATE_INT8(_f, _s)                                          \
619     VMSTATE_INT8_V(_f, _s, 0)
620 #define VMSTATE_INT16(_f, _s)                                         \
621     VMSTATE_INT16_V(_f, _s, 0)
622 #define VMSTATE_INT32(_f, _s)                                         \
623     VMSTATE_INT32_V(_f, _s, 0)
624 #define VMSTATE_INT64(_f, _s)                                         \
625     VMSTATE_INT64_V(_f, _s, 0)
626 
627 #define VMSTATE_UINT8(_f, _s)                                         \
628     VMSTATE_UINT8_V(_f, _s, 0)
629 #define VMSTATE_UINT16(_f, _s)                                        \
630     VMSTATE_UINT16_V(_f, _s, 0)
631 #define VMSTATE_UINT32(_f, _s)                                        \
632     VMSTATE_UINT32_V(_f, _s, 0)
633 #define VMSTATE_UINT64(_f, _s)                                        \
634     VMSTATE_UINT64_V(_f, _s, 0)
635 
636 #define VMSTATE_UINT8_EQUAL(_f, _s)                                   \
637     VMSTATE_SINGLE(_f, _s, 0, vmstate_info_uint8_equal, uint8_t)
638 
639 #define VMSTATE_UINT16_EQUAL(_f, _s)                                  \
640     VMSTATE_SINGLE(_f, _s, 0, vmstate_info_uint16_equal, uint16_t)
641 
642 #define VMSTATE_UINT16_EQUAL_V(_f, _s, _v)                            \
643     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint16_equal, uint16_t)
644 
645 #define VMSTATE_INT32_EQUAL(_f, _s)                                   \
646     VMSTATE_SINGLE(_f, _s, 0, vmstate_info_int32_equal, int32_t)
647 
648 #define VMSTATE_UINT32_EQUAL_V(_f, _s, _v)                            \
649     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint32_equal, uint32_t)
650 
651 #define VMSTATE_UINT32_EQUAL(_f, _s)                                  \
652     VMSTATE_UINT32_EQUAL_V(_f, _s, 0)
653 
654 #define VMSTATE_UINT64_EQUAL_V(_f, _s, _v)                            \
655     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint64_equal, uint64_t)
656 
657 #define VMSTATE_UINT64_EQUAL(_f, _s)                                  \
658     VMSTATE_UINT64_EQUAL_V(_f, _s, 0)
659 
660 #define VMSTATE_INT32_POSITIVE_LE(_f, _s)                             \
661     VMSTATE_SINGLE(_f, _s, 0, vmstate_info_int32_le, int32_t)
662 
663 #define VMSTATE_INT8_TEST(_f, _s, _t)                               \
664     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_int8, int8_t)
665 
666 #define VMSTATE_INT16_TEST(_f, _s, _t)                               \
667     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_int16, int16_t)
668 
669 #define VMSTATE_INT32_TEST(_f, _s, _t)                                  \
670     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_int32, int32_t)
671 
672 #define VMSTATE_INT64_TEST(_f, _s, _t)                                  \
673     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_int64, int64_t)
674 
675 #define VMSTATE_UINT8_TEST(_f, _s, _t)                               \
676     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint8, uint8_t)
677 
678 #define VMSTATE_UINT16_TEST(_f, _s, _t)                               \
679     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint16, uint16_t)
680 
681 #define VMSTATE_UINT32_TEST(_f, _s, _t)                                  \
682     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint32, uint32_t)
683 
684 #define VMSTATE_UINT64_TEST(_f, _s, _t)                                  \
685     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint64, uint64_t)
686 
687 
688 #define VMSTATE_FLOAT64_V(_f, _s, _v)                                 \
689     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_float64, float64)
690 
691 #define VMSTATE_FLOAT64(_f, _s)                                       \
692     VMSTATE_FLOAT64_V(_f, _s, 0)
693 
694 #define VMSTATE_TIMER_PTR_TEST(_f, _s, _test)                             \
695     VMSTATE_POINTER_TEST(_f, _s, _test, vmstate_info_timer, QEMUTimer *)
696 
697 #define VMSTATE_TIMER_PTR_V(_f, _s, _v)                                   \
698     VMSTATE_POINTER(_f, _s, _v, vmstate_info_timer, QEMUTimer *)
699 
700 #define VMSTATE_TIMER_PTR(_f, _s)                                         \
701     VMSTATE_TIMER_PTR_V(_f, _s, 0)
702 
703 #define VMSTATE_TIMER_PTR_ARRAY(_f, _s, _n)                              \
704     VMSTATE_ARRAY_OF_POINTER(_f, _s, _n, 0, vmstate_info_timer, QEMUTimer *)
705 
706 #define VMSTATE_TIMER_TEST(_f, _s, _test)                             \
707     VMSTATE_SINGLE_TEST(_f, _s, _test, 0, vmstate_info_timer, QEMUTimer)
708 
709 #define VMSTATE_TIMER_V(_f, _s, _v)                                   \
710     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_timer, QEMUTimer)
711 
712 #define VMSTATE_TIMER(_f, _s)                                         \
713     VMSTATE_TIMER_V(_f, _s, 0)
714 
715 #define VMSTATE_TIMER_ARRAY(_f, _s, _n)                              \
716     VMSTATE_ARRAY(_f, _s, _n, 0, vmstate_info_timer, QEMUTimer)
717 
718 #define VMSTATE_BOOL_ARRAY_V(_f, _s, _n, _v)                         \
719     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_bool, bool)
720 
721 #define VMSTATE_BOOL_ARRAY(_f, _s, _n)                               \
722     VMSTATE_BOOL_ARRAY_V(_f, _s, _n, 0)
723 
724 #define VMSTATE_UINT16_ARRAY_V(_f, _s, _n, _v)                         \
725     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint16, uint16_t)
726 
727 #define VMSTATE_UINT16_2DARRAY_V(_f, _s, _n1, _n2, _v)                \
728     VMSTATE_2DARRAY(_f, _s, _n1, _n2, _v, vmstate_info_uint16, uint16_t)
729 
730 #define VMSTATE_UINT16_ARRAY(_f, _s, _n)                               \
731     VMSTATE_UINT16_ARRAY_V(_f, _s, _n, 0)
732 
733 #define VMSTATE_UINT16_2DARRAY(_f, _s, _n1, _n2)                      \
734     VMSTATE_UINT16_2DARRAY_V(_f, _s, _n1, _n2, 0)
735 
736 #define VMSTATE_UINT8_2DARRAY_V(_f, _s, _n1, _n2, _v)                 \
737     VMSTATE_2DARRAY(_f, _s, _n1, _n2, _v, vmstate_info_uint8, uint8_t)
738 
739 #define VMSTATE_UINT8_ARRAY_V(_f, _s, _n, _v)                         \
740     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint8, uint8_t)
741 
742 #define VMSTATE_UINT8_ARRAY(_f, _s, _n)                               \
743     VMSTATE_UINT8_ARRAY_V(_f, _s, _n, 0)
744 
745 #define VMSTATE_UINT8_SUB_ARRAY(_f, _s, _start, _num)                \
746     VMSTATE_SUB_ARRAY(_f, _s, _start, _num, 0, vmstate_info_uint8, uint8_t)
747 
748 #define VMSTATE_UINT8_2DARRAY(_f, _s, _n1, _n2)                       \
749     VMSTATE_UINT8_2DARRAY_V(_f, _s, _n1, _n2, 0)
750 
751 #define VMSTATE_UINT32_ARRAY_V(_f, _s, _n, _v)                        \
752     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint32, uint32_t)
753 
754 #define VMSTATE_UINT32_2DARRAY_V(_f, _s, _n1, _n2, _v)                \
755     VMSTATE_2DARRAY(_f, _s, _n1, _n2, _v, vmstate_info_uint32, uint32_t)
756 
757 #define VMSTATE_UINT32_ARRAY(_f, _s, _n)                              \
758     VMSTATE_UINT32_ARRAY_V(_f, _s, _n, 0)
759 
760 #define VMSTATE_UINT32_2DARRAY(_f, _s, _n1, _n2)                      \
761     VMSTATE_UINT32_2DARRAY_V(_f, _s, _n1, _n2, 0)
762 
763 #define VMSTATE_UINT64_ARRAY_V(_f, _s, _n, _v)                        \
764     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint64, uint64_t)
765 
766 #define VMSTATE_UINT64_ARRAY(_f, _s, _n)                              \
767     VMSTATE_UINT64_ARRAY_V(_f, _s, _n, 0)
768 
769 #define VMSTATE_INT16_ARRAY_V(_f, _s, _n, _v)                         \
770     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_int16, int16_t)
771 
772 #define VMSTATE_INT16_ARRAY(_f, _s, _n)                               \
773     VMSTATE_INT16_ARRAY_V(_f, _s, _n, 0)
774 
775 #define VMSTATE_INT32_ARRAY_V(_f, _s, _n, _v)                         \
776     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_int32, int32_t)
777 
778 #define VMSTATE_INT32_ARRAY(_f, _s, _n)                               \
779     VMSTATE_INT32_ARRAY_V(_f, _s, _n, 0)
780 
781 #define VMSTATE_UINT32_SUB_ARRAY(_f, _s, _start, _num)                \
782     VMSTATE_SUB_ARRAY(_f, _s, _start, _num, 0, vmstate_info_uint32, uint32_t)
783 
784 #define VMSTATE_INT64_ARRAY_V(_f, _s, _n, _v)                         \
785     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_int64, int64_t)
786 
787 #define VMSTATE_INT64_ARRAY(_f, _s, _n)                               \
788     VMSTATE_INT64_ARRAY_V(_f, _s, _n, 0)
789 
790 #define VMSTATE_FLOAT64_ARRAY_V(_f, _s, _n, _v)                       \
791     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_float64, float64)
792 
793 #define VMSTATE_FLOAT64_ARRAY(_f, _s, _n)                             \
794     VMSTATE_FLOAT64_ARRAY_V(_f, _s, _n, 0)
795 
796 #define VMSTATE_CPUDOUBLE_ARRAY_V(_f, _s, _n, _v)                     \
797     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_cpudouble, CPU_DoubleU)
798 
799 #define VMSTATE_CPUDOUBLE_ARRAY(_f, _s, _n)                           \
800     VMSTATE_CPUDOUBLE_ARRAY_V(_f, _s, _n, 0)
801 
802 #define VMSTATE_BUFFER_V(_f, _s, _v)                                  \
803     VMSTATE_STATIC_BUFFER(_f, _s, _v, NULL, 0, sizeof(typeof_field(_s, _f)))
804 
805 #define VMSTATE_BUFFER(_f, _s)                                        \
806     VMSTATE_BUFFER_V(_f, _s, 0)
807 
808 #define VMSTATE_PARTIAL_BUFFER(_f, _s, _size)                         \
809     VMSTATE_STATIC_BUFFER(_f, _s, 0, NULL, 0, _size)
810 
811 #define VMSTATE_BUFFER_START_MIDDLE(_f, _s, _start) \
812     VMSTATE_STATIC_BUFFER(_f, _s, 0, NULL, _start, sizeof(typeof_field(_s, _f)))
813 
814 #define VMSTATE_PARTIAL_VBUFFER(_f, _s, _size)                        \
815     VMSTATE_VBUFFER(_f, _s, 0, NULL, 0, _size)
816 
817 #define VMSTATE_PARTIAL_VBUFFER_UINT32(_f, _s, _size)                        \
818     VMSTATE_VBUFFER_UINT32(_f, _s, 0, NULL, 0, _size)
819 
820 #define VMSTATE_SUB_VBUFFER(_f, _s, _start, _size)                    \
821     VMSTATE_VBUFFER(_f, _s, 0, NULL, _start, _size)
822 
823 #define VMSTATE_BUFFER_TEST(_f, _s, _test)                            \
824     VMSTATE_STATIC_BUFFER(_f, _s, 0, _test, 0, sizeof(typeof_field(_s, _f)))
825 
826 #define VMSTATE_BUFFER_UNSAFE(_field, _state, _version, _size)        \
827     VMSTATE_BUFFER_UNSAFE_INFO(_field, _state, _version, vmstate_info_buffer, _size)
828 
829 #define VMSTATE_UNUSED_V(_v, _size)                                   \
830     VMSTATE_UNUSED_BUFFER(NULL, _v, _size)
831 
832 #define VMSTATE_UNUSED(_size)                                         \
833     VMSTATE_UNUSED_V(0, _size)
834 
835 #define VMSTATE_UNUSED_TEST(_test, _size)                             \
836     VMSTATE_UNUSED_BUFFER(_test, 0, _size)
837 
838 #define VMSTATE_END_OF_LIST()                                         \
839     {}
840 
841 #define SELF_ANNOUNCE_ROUNDS 5
842 
843 void loadvm_free_handlers(MigrationIncomingState *mis);
844 
845 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
846                        void *opaque, int version_id);
847 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
848                         void *opaque, QJSON *vmdesc);
849 
850 bool vmstate_save_needed(const VMStateDescription *vmsd, void *opaque);
851 
852 int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
853                                    const VMStateDescription *vmsd,
854                                    void *base, int alias_id,
855                                    int required_for_version);
856 
857 static inline int vmstate_register(DeviceState *dev, int instance_id,
858                                    const VMStateDescription *vmsd,
859                                    void *opaque)
860 {
861     return vmstate_register_with_alias_id(dev, instance_id, vmsd,
862                                           opaque, -1, 0);
863 }
864 
865 void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
866                         void *opaque);
867 
868 struct MemoryRegion;
869 void vmstate_register_ram(struct MemoryRegion *memory, DeviceState *dev);
870 void vmstate_unregister_ram(struct MemoryRegion *memory, DeviceState *dev);
871 void vmstate_register_ram_global(struct MemoryRegion *memory);
872 
873 static inline
874 int64_t self_announce_delay(int round)
875 {
876     assert(round < SELF_ANNOUNCE_ROUNDS && round > 0);
877     /* delay 50ms, 150ms, 250ms, ... */
878     return 50 + (SELF_ANNOUNCE_ROUNDS - round - 1) * 100;
879 }
880 
881 void dump_vmstate_json_to_file(FILE *out_fp);
882 
883 #endif
884