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