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