xref: /openbmc/qemu/include/migration/vmstate.h (revision 84a3a53c)
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 /* a variable length array (i.e. _type *_field) but we know the
378  * length
379  */
380 #define VMSTATE_STRUCT_VARRAY_KNOWN(_field, _state, _num, _version, _vmsd, _type) { \
381     .name       = (stringify(_field)),                               \
382     .num          = (_num),                                          \
383     .version_id = (_version),                                        \
384     .vmsd       = &(_vmsd),                                          \
385     .size       = sizeof(_type),                                     \
386     .flags      = VMS_STRUCT|VMS_ARRAY,                              \
387     .offset     = offsetof(_state, _field),                          \
388 }
389 
390 #define VMSTATE_STRUCT_VARRAY_UINT8(_field, _state, _field_num, _version, _vmsd, _type) { \
391     .name       = (stringify(_field)),                               \
392     .num_offset = vmstate_offset_value(_state, _field_num, uint8_t), \
393     .version_id = (_version),                                        \
394     .vmsd       = &(_vmsd),                                          \
395     .size       = sizeof(_type),                                     \
396     .flags      = VMS_STRUCT|VMS_VARRAY_UINT8,                       \
397     .offset     = offsetof(_state, _field),                          \
398 }
399 
400 #define VMSTATE_STRUCT_VARRAY_POINTER_INT32(_field, _state, _field_num, _vmsd, _type) { \
401     .name       = (stringify(_field)),                               \
402     .version_id = 0,                                                 \
403     .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
404     .size       = sizeof(_type),                                     \
405     .vmsd       = &(_vmsd),                                          \
406     .flags      = VMS_POINTER | VMS_VARRAY_INT32 | VMS_STRUCT,       \
407     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
408 }
409 
410 #define VMSTATE_STRUCT_VARRAY_POINTER_UINT32(_field, _state, _field_num, _vmsd, _type) { \
411     .name       = (stringify(_field)),                               \
412     .version_id = 0,                                                 \
413     .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
414     .size       = sizeof(_type),                                     \
415     .vmsd       = &(_vmsd),                                          \
416     .flags      = VMS_POINTER | VMS_VARRAY_INT32 | VMS_STRUCT,       \
417     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
418 }
419 
420 #define VMSTATE_STRUCT_VARRAY_POINTER_UINT16(_field, _state, _field_num, _vmsd, _type) { \
421     .name       = (stringify(_field)),                               \
422     .version_id = 0,                                                 \
423     .num_offset = vmstate_offset_value(_state, _field_num, uint16_t),\
424     .size       = sizeof(_type),                                     \
425     .vmsd       = &(_vmsd),                                          \
426     .flags      = VMS_POINTER | VMS_VARRAY_UINT16 | VMS_STRUCT,      \
427     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
428 }
429 
430 #define VMSTATE_STRUCT_VARRAY_INT32(_field, _state, _field_num, _version, _vmsd, _type) { \
431     .name       = (stringify(_field)),                               \
432     .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
433     .version_id = (_version),                                        \
434     .vmsd       = &(_vmsd),                                          \
435     .size       = sizeof(_type),                                     \
436     .flags      = VMS_STRUCT|VMS_VARRAY_INT32,                       \
437     .offset     = offsetof(_state, _field),                          \
438 }
439 
440 #define VMSTATE_STRUCT_VARRAY_UINT32(_field, _state, _field_num, _version, _vmsd, _type) { \
441     .name       = (stringify(_field)),                               \
442     .num_offset = vmstate_offset_value(_state, _field_num, uint32_t), \
443     .version_id = (_version),                                        \
444     .vmsd       = &(_vmsd),                                          \
445     .size       = sizeof(_type),                                     \
446     .flags      = VMS_STRUCT|VMS_VARRAY_UINT32,                      \
447     .offset     = offsetof(_state, _field),                          \
448 }
449 
450 #define VMSTATE_STRUCT_VARRAY_ALLOC(_field, _state, _field_num, _version, _vmsd, _type) {\
451     .name       = (stringify(_field)),                               \
452     .version_id = (_version),                                        \
453     .vmsd       = &(_vmsd),                                          \
454     .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
455     .size       = sizeof(_type),                                     \
456     .flags      = VMS_STRUCT|VMS_VARRAY_INT32|VMS_ALLOC|VMS_POINTER, \
457     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
458 }
459 
460 #define VMSTATE_STATIC_BUFFER(_field, _state, _version, _test, _start, _size) { \
461     .name         = (stringify(_field)),                             \
462     .version_id   = (_version),                                      \
463     .field_exists = (_test),                                         \
464     .size         = (_size - _start),                                \
465     .info         = &vmstate_info_buffer,                            \
466     .flags        = VMS_BUFFER,                                      \
467     .offset       = vmstate_offset_buffer(_state, _field) + _start,  \
468 }
469 
470 #define VMSTATE_VBUFFER_MULTIPLY(_field, _state, _version, _test, _start, _field_size, _multiply) { \
471     .name         = (stringify(_field)),                             \
472     .version_id   = (_version),                                      \
473     .field_exists = (_test),                                         \
474     .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
475     .size         = (_multiply),                                      \
476     .info         = &vmstate_info_buffer,                            \
477     .flags        = VMS_VBUFFER|VMS_POINTER|VMS_MULTIPLY,            \
478     .offset       = offsetof(_state, _field),                        \
479     .start        = (_start),                                        \
480 }
481 
482 #define VMSTATE_VBUFFER(_field, _state, _version, _test, _start, _field_size) { \
483     .name         = (stringify(_field)),                             \
484     .version_id   = (_version),                                      \
485     .field_exists = (_test),                                         \
486     .size_offset  = vmstate_offset_value(_state, _field_size, int32_t),\
487     .info         = &vmstate_info_buffer,                            \
488     .flags        = VMS_VBUFFER|VMS_POINTER,                         \
489     .offset       = offsetof(_state, _field),                        \
490     .start        = (_start),                                        \
491 }
492 
493 #define VMSTATE_VBUFFER_UINT32(_field, _state, _version, _test, _start, _field_size) { \
494     .name         = (stringify(_field)),                             \
495     .version_id   = (_version),                                      \
496     .field_exists = (_test),                                         \
497     .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
498     .info         = &vmstate_info_buffer,                            \
499     .flags        = VMS_VBUFFER|VMS_POINTER,                         \
500     .offset       = offsetof(_state, _field),                        \
501     .start        = (_start),                                        \
502 }
503 
504 #define VMSTATE_VBUFFER_ALLOC_UINT32(_field, _state, _version, _test, _start, _field_size) { \
505     .name         = (stringify(_field)),                             \
506     .version_id   = (_version),                                      \
507     .field_exists = (_test),                                         \
508     .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
509     .info         = &vmstate_info_buffer,                            \
510     .flags        = VMS_VBUFFER|VMS_POINTER|VMS_ALLOC,               \
511     .offset       = offsetof(_state, _field),                        \
512     .start        = (_start),                                        \
513 }
514 
515 #define VMSTATE_BUFFER_UNSAFE_INFO_TEST(_field, _state, _test, _version, _info, _size) { \
516     .name       = (stringify(_field)),                               \
517     .version_id = (_version),                                        \
518     .field_exists = (_test),                                         \
519     .size       = (_size),                                           \
520     .info       = &(_info),                                          \
521     .flags      = VMS_BUFFER,                                        \
522     .offset     = offsetof(_state, _field),                          \
523 }
524 
525 #define VMSTATE_BUFFER_POINTER_UNSAFE(_field, _state, _version, _size) { \
526     .name       = (stringify(_field)),                               \
527     .version_id = (_version),                                        \
528     .size       = (_size),                                           \
529     .info       = &vmstate_info_buffer,                              \
530     .flags      = VMS_BUFFER|VMS_POINTER,                            \
531     .offset     = offsetof(_state, _field),                          \
532 }
533 
534 #define VMSTATE_UNUSED_BUFFER(_test, _version, _size) {              \
535     .name         = "unused",                                        \
536     .field_exists = (_test),                                         \
537     .version_id   = (_version),                                      \
538     .size         = (_size),                                         \
539     .info         = &vmstate_info_unused_buffer,                     \
540     .flags        = VMS_BUFFER,                                      \
541 }
542 
543 /* _field_size should be a int32_t field in the _state struct giving the
544  * size of the bitmap _field in bits.
545  */
546 #define VMSTATE_BITMAP(_field, _state, _version, _field_size) {      \
547     .name         = (stringify(_field)),                             \
548     .version_id   = (_version),                                      \
549     .size_offset  = vmstate_offset_value(_state, _field_size, int32_t),\
550     .info         = &vmstate_info_bitmap,                            \
551     .flags        = VMS_VBUFFER|VMS_POINTER,                         \
552     .offset       = offsetof(_state, _field),                        \
553 }
554 
555 /* _f : field name
556    _f_n : num of elements field_name
557    _n : num of elements
558    _s : struct state name
559    _v : version
560 */
561 
562 #define VMSTATE_SINGLE(_field, _state, _version, _info, _type)        \
563     VMSTATE_SINGLE_TEST(_field, _state, NULL, _version, _info, _type)
564 
565 #define VMSTATE_STRUCT(_field, _state, _version, _vmsd, _type)        \
566     VMSTATE_STRUCT_TEST(_field, _state, NULL, _version, _vmsd, _type)
567 
568 #define VMSTATE_STRUCT_POINTER(_field, _state, _vmsd, _type)          \
569     VMSTATE_STRUCT_POINTER_V(_field, _state, 0, _vmsd, _type)
570 
571 #define VMSTATE_STRUCT_POINTER_TEST(_field, _state, _test, _vmsd, _type)     \
572     VMSTATE_STRUCT_POINTER_TEST_V(_field, _state, _test, 0, _vmsd, _type)
573 
574 #define VMSTATE_STRUCT_ARRAY(_field, _state, _num, _version, _vmsd, _type) \
575     VMSTATE_STRUCT_ARRAY_TEST(_field, _state, _num, NULL, _version,   \
576             _vmsd, _type)
577 
578 #define VMSTATE_BUFFER_UNSAFE_INFO(_field, _state, _version, _info, _size) \
579     VMSTATE_BUFFER_UNSAFE_INFO_TEST(_field, _state, NULL, _version, _info, \
580             _size)
581 
582 #define VMSTATE_BOOL_V(_f, _s, _v)                                    \
583     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_bool, bool)
584 
585 #define VMSTATE_INT8_V(_f, _s, _v)                                    \
586     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int8, int8_t)
587 #define VMSTATE_INT16_V(_f, _s, _v)                                   \
588     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int16, int16_t)
589 #define VMSTATE_INT32_V(_f, _s, _v)                                   \
590     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int32, int32_t)
591 #define VMSTATE_INT64_V(_f, _s, _v)                                   \
592     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int64, int64_t)
593 
594 #define VMSTATE_UINT8_V(_f, _s, _v)                                   \
595     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint8, uint8_t)
596 #define VMSTATE_UINT16_V(_f, _s, _v)                                  \
597     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint16, uint16_t)
598 #define VMSTATE_UINT32_V(_f, _s, _v)                                  \
599     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint32, uint32_t)
600 #define VMSTATE_UINT64_V(_f, _s, _v)                                  \
601     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint64, uint64_t)
602 
603 #define VMSTATE_BOOL(_f, _s)                                          \
604     VMSTATE_BOOL_V(_f, _s, 0)
605 
606 #define VMSTATE_INT8(_f, _s)                                          \
607     VMSTATE_INT8_V(_f, _s, 0)
608 #define VMSTATE_INT16(_f, _s)                                         \
609     VMSTATE_INT16_V(_f, _s, 0)
610 #define VMSTATE_INT32(_f, _s)                                         \
611     VMSTATE_INT32_V(_f, _s, 0)
612 #define VMSTATE_INT64(_f, _s)                                         \
613     VMSTATE_INT64_V(_f, _s, 0)
614 
615 #define VMSTATE_UINT8(_f, _s)                                         \
616     VMSTATE_UINT8_V(_f, _s, 0)
617 #define VMSTATE_UINT16(_f, _s)                                        \
618     VMSTATE_UINT16_V(_f, _s, 0)
619 #define VMSTATE_UINT32(_f, _s)                                        \
620     VMSTATE_UINT32_V(_f, _s, 0)
621 #define VMSTATE_UINT64(_f, _s)                                        \
622     VMSTATE_UINT64_V(_f, _s, 0)
623 
624 #define VMSTATE_UINT8_EQUAL(_f, _s)                                   \
625     VMSTATE_SINGLE(_f, _s, 0, vmstate_info_uint8_equal, uint8_t)
626 
627 #define VMSTATE_UINT16_EQUAL(_f, _s)                                  \
628     VMSTATE_SINGLE(_f, _s, 0, vmstate_info_uint16_equal, uint16_t)
629 
630 #define VMSTATE_UINT16_EQUAL_V(_f, _s, _v)                            \
631     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint16_equal, uint16_t)
632 
633 #define VMSTATE_INT32_EQUAL(_f, _s)                                   \
634     VMSTATE_SINGLE(_f, _s, 0, vmstate_info_int32_equal, int32_t)
635 
636 #define VMSTATE_UINT32_EQUAL_V(_f, _s, _v)                            \
637     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint32_equal, uint32_t)
638 
639 #define VMSTATE_UINT32_EQUAL(_f, _s)                                  \
640     VMSTATE_UINT32_EQUAL_V(_f, _s, 0)
641 
642 #define VMSTATE_UINT64_EQUAL_V(_f, _s, _v)                            \
643     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint64_equal, uint64_t)
644 
645 #define VMSTATE_UINT64_EQUAL(_f, _s)                                  \
646     VMSTATE_UINT64_EQUAL_V(_f, _s, 0)
647 
648 #define VMSTATE_INT32_POSITIVE_LE(_f, _s)                             \
649     VMSTATE_SINGLE(_f, _s, 0, vmstate_info_int32_le, int32_t)
650 
651 #define VMSTATE_INT8_TEST(_f, _s, _t)                               \
652     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_int8, int8_t)
653 
654 #define VMSTATE_INT16_TEST(_f, _s, _t)                               \
655     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_int16, int16_t)
656 
657 #define VMSTATE_INT32_TEST(_f, _s, _t)                                  \
658     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_int32, int32_t)
659 
660 #define VMSTATE_INT64_TEST(_f, _s, _t)                                  \
661     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_int64, int64_t)
662 
663 #define VMSTATE_UINT8_TEST(_f, _s, _t)                               \
664     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint8, uint8_t)
665 
666 #define VMSTATE_UINT16_TEST(_f, _s, _t)                               \
667     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint16, uint16_t)
668 
669 #define VMSTATE_UINT32_TEST(_f, _s, _t)                                  \
670     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint32, uint32_t)
671 
672 #define VMSTATE_UINT64_TEST(_f, _s, _t)                                  \
673     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint64, uint64_t)
674 
675 
676 #define VMSTATE_FLOAT64_V(_f, _s, _v)                                 \
677     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_float64, float64)
678 
679 #define VMSTATE_FLOAT64(_f, _s)                                       \
680     VMSTATE_FLOAT64_V(_f, _s, 0)
681 
682 #define VMSTATE_TIMER_PTR_TEST(_f, _s, _test)                             \
683     VMSTATE_POINTER_TEST(_f, _s, _test, vmstate_info_timer, QEMUTimer *)
684 
685 #define VMSTATE_TIMER_PTR_V(_f, _s, _v)                                   \
686     VMSTATE_POINTER(_f, _s, _v, vmstate_info_timer, QEMUTimer *)
687 
688 #define VMSTATE_TIMER_PTR(_f, _s)                                         \
689     VMSTATE_TIMER_PTR_V(_f, _s, 0)
690 
691 #define VMSTATE_TIMER_PTR_ARRAY(_f, _s, _n)                              \
692     VMSTATE_ARRAY_OF_POINTER(_f, _s, _n, 0, vmstate_info_timer, QEMUTimer *)
693 
694 #define VMSTATE_TIMER_TEST(_f, _s, _test)                             \
695     VMSTATE_SINGLE_TEST(_f, _s, _test, 0, vmstate_info_timer, QEMUTimer)
696 
697 #define VMSTATE_TIMER_V(_f, _s, _v)                                   \
698     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_timer, QEMUTimer)
699 
700 #define VMSTATE_TIMER(_f, _s)                                         \
701     VMSTATE_TIMER_V(_f, _s, 0)
702 
703 #define VMSTATE_TIMER_ARRAY(_f, _s, _n)                              \
704     VMSTATE_ARRAY(_f, _s, _n, 0, vmstate_info_timer, QEMUTimer)
705 
706 #define VMSTATE_BOOL_ARRAY_V(_f, _s, _n, _v)                         \
707     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_bool, bool)
708 
709 #define VMSTATE_BOOL_ARRAY(_f, _s, _n)                               \
710     VMSTATE_BOOL_ARRAY_V(_f, _s, _n, 0)
711 
712 #define VMSTATE_UINT16_ARRAY_V(_f, _s, _n, _v)                         \
713     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint16, uint16_t)
714 
715 #define VMSTATE_UINT16_2DARRAY_V(_f, _s, _n1, _n2, _v)                \
716     VMSTATE_2DARRAY(_f, _s, _n1, _n2, _v, vmstate_info_uint16, uint16_t)
717 
718 #define VMSTATE_UINT16_ARRAY(_f, _s, _n)                               \
719     VMSTATE_UINT16_ARRAY_V(_f, _s, _n, 0)
720 
721 #define VMSTATE_UINT16_2DARRAY(_f, _s, _n1, _n2)                      \
722     VMSTATE_UINT16_2DARRAY_V(_f, _s, _n1, _n2, 0)
723 
724 #define VMSTATE_UINT8_2DARRAY_V(_f, _s, _n1, _n2, _v)                 \
725     VMSTATE_2DARRAY(_f, _s, _n1, _n2, _v, vmstate_info_uint8, uint8_t)
726 
727 #define VMSTATE_UINT8_ARRAY_V(_f, _s, _n, _v)                         \
728     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint8, uint8_t)
729 
730 #define VMSTATE_UINT8_ARRAY(_f, _s, _n)                               \
731     VMSTATE_UINT8_ARRAY_V(_f, _s, _n, 0)
732 
733 #define VMSTATE_UINT8_SUB_ARRAY(_f, _s, _start, _num)                \
734     VMSTATE_SUB_ARRAY(_f, _s, _start, _num, 0, vmstate_info_uint8, uint8_t)
735 
736 #define VMSTATE_UINT8_2DARRAY(_f, _s, _n1, _n2)                       \
737     VMSTATE_UINT8_2DARRAY_V(_f, _s, _n1, _n2, 0)
738 
739 #define VMSTATE_UINT32_ARRAY_V(_f, _s, _n, _v)                        \
740     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint32, uint32_t)
741 
742 #define VMSTATE_UINT32_2DARRAY_V(_f, _s, _n1, _n2, _v)                \
743     VMSTATE_2DARRAY(_f, _s, _n1, _n2, _v, vmstate_info_uint32, uint32_t)
744 
745 #define VMSTATE_UINT32_ARRAY(_f, _s, _n)                              \
746     VMSTATE_UINT32_ARRAY_V(_f, _s, _n, 0)
747 
748 #define VMSTATE_UINT32_2DARRAY(_f, _s, _n1, _n2)                      \
749     VMSTATE_UINT32_2DARRAY_V(_f, _s, _n1, _n2, 0)
750 
751 #define VMSTATE_UINT64_ARRAY_V(_f, _s, _n, _v)                        \
752     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint64, uint64_t)
753 
754 #define VMSTATE_UINT64_ARRAY(_f, _s, _n)                              \
755     VMSTATE_UINT64_ARRAY_V(_f, _s, _n, 0)
756 
757 #define VMSTATE_INT16_ARRAY_V(_f, _s, _n, _v)                         \
758     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_int16, int16_t)
759 
760 #define VMSTATE_INT16_ARRAY(_f, _s, _n)                               \
761     VMSTATE_INT16_ARRAY_V(_f, _s, _n, 0)
762 
763 #define VMSTATE_INT32_ARRAY_V(_f, _s, _n, _v)                         \
764     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_int32, int32_t)
765 
766 #define VMSTATE_INT32_ARRAY(_f, _s, _n)                               \
767     VMSTATE_INT32_ARRAY_V(_f, _s, _n, 0)
768 
769 #define VMSTATE_UINT32_SUB_ARRAY(_f, _s, _start, _num)                \
770     VMSTATE_SUB_ARRAY(_f, _s, _start, _num, 0, vmstate_info_uint32, uint32_t)
771 
772 #define VMSTATE_INT64_ARRAY_V(_f, _s, _n, _v)                         \
773     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_int64, int64_t)
774 
775 #define VMSTATE_INT64_ARRAY(_f, _s, _n)                               \
776     VMSTATE_INT64_ARRAY_V(_f, _s, _n, 0)
777 
778 #define VMSTATE_FLOAT64_ARRAY_V(_f, _s, _n, _v)                       \
779     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_float64, float64)
780 
781 #define VMSTATE_FLOAT64_ARRAY(_f, _s, _n)                             \
782     VMSTATE_FLOAT64_ARRAY_V(_f, _s, _n, 0)
783 
784 #define VMSTATE_BUFFER_V(_f, _s, _v)                                  \
785     VMSTATE_STATIC_BUFFER(_f, _s, _v, NULL, 0, sizeof(typeof_field(_s, _f)))
786 
787 #define VMSTATE_BUFFER(_f, _s)                                        \
788     VMSTATE_BUFFER_V(_f, _s, 0)
789 
790 #define VMSTATE_PARTIAL_BUFFER(_f, _s, _size)                         \
791     VMSTATE_STATIC_BUFFER(_f, _s, 0, NULL, 0, _size)
792 
793 #define VMSTATE_BUFFER_START_MIDDLE(_f, _s, _start) \
794     VMSTATE_STATIC_BUFFER(_f, _s, 0, NULL, _start, sizeof(typeof_field(_s, _f)))
795 
796 #define VMSTATE_PARTIAL_VBUFFER(_f, _s, _size)                        \
797     VMSTATE_VBUFFER(_f, _s, 0, NULL, 0, _size)
798 
799 #define VMSTATE_PARTIAL_VBUFFER_UINT32(_f, _s, _size)                        \
800     VMSTATE_VBUFFER_UINT32(_f, _s, 0, NULL, 0, _size)
801 
802 #define VMSTATE_SUB_VBUFFER(_f, _s, _start, _size)                    \
803     VMSTATE_VBUFFER(_f, _s, 0, NULL, _start, _size)
804 
805 #define VMSTATE_BUFFER_TEST(_f, _s, _test)                            \
806     VMSTATE_STATIC_BUFFER(_f, _s, 0, _test, 0, sizeof(typeof_field(_s, _f)))
807 
808 #define VMSTATE_BUFFER_UNSAFE(_field, _state, _version, _size)        \
809     VMSTATE_BUFFER_UNSAFE_INFO(_field, _state, _version, vmstate_info_buffer, _size)
810 
811 #define VMSTATE_UNUSED_V(_v, _size)                                   \
812     VMSTATE_UNUSED_BUFFER(NULL, _v, _size)
813 
814 #define VMSTATE_UNUSED(_size)                                         \
815     VMSTATE_UNUSED_V(0, _size)
816 
817 #define VMSTATE_UNUSED_TEST(_test, _size)                             \
818     VMSTATE_UNUSED_BUFFER(_test, 0, _size)
819 
820 #define VMSTATE_END_OF_LIST()                                         \
821     {}
822 
823 #define SELF_ANNOUNCE_ROUNDS 5
824 
825 void loadvm_free_handlers(MigrationIncomingState *mis);
826 
827 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
828                        void *opaque, int version_id);
829 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
830                         void *opaque, QJSON *vmdesc);
831 
832 bool vmstate_save_needed(const VMStateDescription *vmsd, void *opaque);
833 
834 int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
835                                    const VMStateDescription *vmsd,
836                                    void *base, int alias_id,
837                                    int required_for_version);
838 
839 static inline int vmstate_register(DeviceState *dev, int instance_id,
840                                    const VMStateDescription *vmsd,
841                                    void *opaque)
842 {
843     return vmstate_register_with_alias_id(dev, instance_id, vmsd,
844                                           opaque, -1, 0);
845 }
846 
847 void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
848                         void *opaque);
849 
850 struct MemoryRegion;
851 void vmstate_register_ram(struct MemoryRegion *memory, DeviceState *dev);
852 void vmstate_unregister_ram(struct MemoryRegion *memory, DeviceState *dev);
853 void vmstate_register_ram_global(struct MemoryRegion *memory);
854 
855 static inline
856 int64_t self_announce_delay(int round)
857 {
858     assert(round < SELF_ANNOUNCE_ROUNDS && round > 0);
859     /* delay 50ms, 150ms, 250ms, ... */
860     return 50 + (SELF_ANNOUNCE_ROUNDS - round - 1) * 100;
861 }
862 
863 void dump_vmstate_json_to_file(FILE *out_fp);
864 
865 #endif
866