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