xref: /openbmc/qemu/include/migration/vmstate.h (revision 953ea14d)
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_VBUFFER_ALLOC_UINT32(_field, _state, _version, _test, _start, _field_size) { \
488     .name         = (stringify(_field)),                             \
489     .version_id   = (_version),                                      \
490     .field_exists = (_test),                                         \
491     .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
492     .info         = &vmstate_info_buffer,                            \
493     .flags        = VMS_VBUFFER|VMS_POINTER|VMS_ALLOC,               \
494     .offset       = offsetof(_state, _field),                        \
495     .start        = (_start),                                        \
496 }
497 
498 #define VMSTATE_BUFFER_UNSAFE_INFO(_field, _state, _version, _info, _size) { \
499     .name       = (stringify(_field)),                               \
500     .version_id = (_version),                                        \
501     .size       = (_size),                                           \
502     .info       = &(_info),                                          \
503     .flags      = VMS_BUFFER,                                        \
504     .offset     = offsetof(_state, _field),                          \
505 }
506 
507 #define VMSTATE_BUFFER_POINTER_UNSAFE(_field, _state, _version, _size) { \
508     .name       = (stringify(_field)),                               \
509     .version_id = (_version),                                        \
510     .size       = (_size),                                           \
511     .info       = &vmstate_info_buffer,                              \
512     .flags      = VMS_BUFFER|VMS_POINTER,                            \
513     .offset     = offsetof(_state, _field),                          \
514 }
515 
516 #define VMSTATE_UNUSED_BUFFER(_test, _version, _size) {              \
517     .name         = "unused",                                        \
518     .field_exists = (_test),                                         \
519     .version_id   = (_version),                                      \
520     .size         = (_size),                                         \
521     .info         = &vmstate_info_unused_buffer,                     \
522     .flags        = VMS_BUFFER,                                      \
523 }
524 
525 /* _field_size should be a int32_t field in the _state struct giving the
526  * size of the bitmap _field in bits.
527  */
528 #define VMSTATE_BITMAP(_field, _state, _version, _field_size) {      \
529     .name         = (stringify(_field)),                             \
530     .version_id   = (_version),                                      \
531     .size_offset  = vmstate_offset_value(_state, _field_size, int32_t),\
532     .info         = &vmstate_info_bitmap,                            \
533     .flags        = VMS_VBUFFER|VMS_POINTER,                         \
534     .offset       = offsetof(_state, _field),                        \
535 }
536 
537 /* _f : field name
538    _f_n : num of elements field_name
539    _n : num of elements
540    _s : struct state name
541    _v : version
542 */
543 
544 #define VMSTATE_SINGLE(_field, _state, _version, _info, _type)        \
545     VMSTATE_SINGLE_TEST(_field, _state, NULL, _version, _info, _type)
546 
547 #define VMSTATE_STRUCT(_field, _state, _version, _vmsd, _type)        \
548     VMSTATE_STRUCT_TEST(_field, _state, NULL, _version, _vmsd, _type)
549 
550 #define VMSTATE_STRUCT_POINTER(_field, _state, _vmsd, _type)          \
551     VMSTATE_STRUCT_POINTER_V(_field, _state, 0, _vmsd, _type)
552 
553 #define VMSTATE_STRUCT_POINTER_TEST(_field, _state, _test, _vmsd, _type)     \
554     VMSTATE_STRUCT_POINTER_TEST_V(_field, _state, _test, 0, _vmsd, _type)
555 
556 #define VMSTATE_STRUCT_ARRAY(_field, _state, _num, _version, _vmsd, _type) \
557     VMSTATE_STRUCT_ARRAY_TEST(_field, _state, _num, NULL, _version,   \
558             _vmsd, _type)
559 
560 #define VMSTATE_BOOL_V(_f, _s, _v)                                    \
561     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_bool, bool)
562 
563 #define VMSTATE_INT8_V(_f, _s, _v)                                    \
564     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int8, int8_t)
565 #define VMSTATE_INT16_V(_f, _s, _v)                                   \
566     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int16, int16_t)
567 #define VMSTATE_INT32_V(_f, _s, _v)                                   \
568     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int32, int32_t)
569 #define VMSTATE_INT64_V(_f, _s, _v)                                   \
570     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int64, int64_t)
571 
572 #define VMSTATE_UINT8_V(_f, _s, _v)                                   \
573     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint8, uint8_t)
574 #define VMSTATE_UINT16_V(_f, _s, _v)                                  \
575     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint16, uint16_t)
576 #define VMSTATE_UINT32_V(_f, _s, _v)                                  \
577     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint32, uint32_t)
578 #define VMSTATE_UINT64_V(_f, _s, _v)                                  \
579     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint64, uint64_t)
580 
581 #define VMSTATE_BOOL(_f, _s)                                          \
582     VMSTATE_BOOL_V(_f, _s, 0)
583 
584 #define VMSTATE_INT8(_f, _s)                                          \
585     VMSTATE_INT8_V(_f, _s, 0)
586 #define VMSTATE_INT16(_f, _s)                                         \
587     VMSTATE_INT16_V(_f, _s, 0)
588 #define VMSTATE_INT32(_f, _s)                                         \
589     VMSTATE_INT32_V(_f, _s, 0)
590 #define VMSTATE_INT64(_f, _s)                                         \
591     VMSTATE_INT64_V(_f, _s, 0)
592 
593 #define VMSTATE_UINT8(_f, _s)                                         \
594     VMSTATE_UINT8_V(_f, _s, 0)
595 #define VMSTATE_UINT16(_f, _s)                                        \
596     VMSTATE_UINT16_V(_f, _s, 0)
597 #define VMSTATE_UINT32(_f, _s)                                        \
598     VMSTATE_UINT32_V(_f, _s, 0)
599 #define VMSTATE_UINT64(_f, _s)                                        \
600     VMSTATE_UINT64_V(_f, _s, 0)
601 
602 #define VMSTATE_UINT8_EQUAL(_f, _s)                                   \
603     VMSTATE_SINGLE(_f, _s, 0, vmstate_info_uint8_equal, uint8_t)
604 
605 #define VMSTATE_UINT16_EQUAL(_f, _s)                                  \
606     VMSTATE_SINGLE(_f, _s, 0, vmstate_info_uint16_equal, uint16_t)
607 
608 #define VMSTATE_UINT16_EQUAL_V(_f, _s, _v)                            \
609     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint16_equal, uint16_t)
610 
611 #define VMSTATE_INT32_EQUAL(_f, _s)                                   \
612     VMSTATE_SINGLE(_f, _s, 0, vmstate_info_int32_equal, int32_t)
613 
614 #define VMSTATE_UINT32_EQUAL_V(_f, _s, _v)                            \
615     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint32_equal, uint32_t)
616 
617 #define VMSTATE_UINT32_EQUAL(_f, _s)                                  \
618     VMSTATE_UINT32_EQUAL_V(_f, _s, 0)
619 
620 #define VMSTATE_UINT64_EQUAL_V(_f, _s, _v)                            \
621     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint64_equal, uint64_t)
622 
623 #define VMSTATE_UINT64_EQUAL(_f, _s)                                  \
624     VMSTATE_UINT64_EQUAL_V(_f, _s, 0)
625 
626 #define VMSTATE_INT32_POSITIVE_LE(_f, _s)                             \
627     VMSTATE_SINGLE(_f, _s, 0, vmstate_info_int32_le, int32_t)
628 
629 #define VMSTATE_UINT8_TEST(_f, _s, _t)                               \
630     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint8, uint8_t)
631 
632 #define VMSTATE_UINT16_TEST(_f, _s, _t)                               \
633     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint16, uint16_t)
634 
635 #define VMSTATE_UINT32_TEST(_f, _s, _t)                                  \
636     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint32, uint32_t)
637 
638 
639 #define VMSTATE_FLOAT64_V(_f, _s, _v)                                 \
640     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_float64, float64)
641 
642 #define VMSTATE_FLOAT64(_f, _s)                                       \
643     VMSTATE_FLOAT64_V(_f, _s, 0)
644 
645 #define VMSTATE_TIMER_TEST(_f, _s, _test)                             \
646     VMSTATE_POINTER_TEST(_f, _s, _test, vmstate_info_timer, QEMUTimer *)
647 
648 #define VMSTATE_TIMER_V(_f, _s, _v)                                   \
649     VMSTATE_POINTER(_f, _s, _v, vmstate_info_timer, QEMUTimer *)
650 
651 #define VMSTATE_TIMER(_f, _s)                                         \
652     VMSTATE_TIMER_V(_f, _s, 0)
653 
654 #define VMSTATE_TIMER_ARRAY(_f, _s, _n)                              \
655     VMSTATE_ARRAY_OF_POINTER(_f, _s, _n, 0, vmstate_info_timer, QEMUTimer *)
656 
657 #define VMSTATE_BOOL_ARRAY_V(_f, _s, _n, _v)                         \
658     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_bool, bool)
659 
660 #define VMSTATE_BOOL_ARRAY(_f, _s, _n)                               \
661     VMSTATE_BOOL_ARRAY_V(_f, _s, _n, 0)
662 
663 #define VMSTATE_UINT16_ARRAY_V(_f, _s, _n, _v)                         \
664     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint16, uint16_t)
665 
666 #define VMSTATE_UINT16_2DARRAY_V(_f, _s, _n1, _n2, _v)                \
667     VMSTATE_2DARRAY(_f, _s, _n1, _n2, _v, vmstate_info_uint16, uint16_t)
668 
669 #define VMSTATE_UINT16_ARRAY(_f, _s, _n)                               \
670     VMSTATE_UINT16_ARRAY_V(_f, _s, _n, 0)
671 
672 #define VMSTATE_UINT16_2DARRAY(_f, _s, _n1, _n2)                      \
673     VMSTATE_UINT16_2DARRAY_V(_f, _s, _n1, _n2, 0)
674 
675 #define VMSTATE_UINT8_2DARRAY_V(_f, _s, _n1, _n2, _v)                 \
676     VMSTATE_2DARRAY(_f, _s, _n1, _n2, _v, vmstate_info_uint8, uint8_t)
677 
678 #define VMSTATE_UINT8_ARRAY_V(_f, _s, _n, _v)                         \
679     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint8, uint8_t)
680 
681 #define VMSTATE_UINT8_ARRAY(_f, _s, _n)                               \
682     VMSTATE_UINT8_ARRAY_V(_f, _s, _n, 0)
683 
684 #define VMSTATE_UINT8_SUB_ARRAY(_f, _s, _start, _num)                \
685     VMSTATE_SUB_ARRAY(_f, _s, _start, _num, 0, vmstate_info_uint8, uint8_t)
686 
687 #define VMSTATE_UINT8_2DARRAY(_f, _s, _n1, _n2)                       \
688     VMSTATE_UINT8_2DARRAY_V(_f, _s, _n1, _n2, 0)
689 
690 #define VMSTATE_UINT32_ARRAY_V(_f, _s, _n, _v)                        \
691     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint32, uint32_t)
692 
693 #define VMSTATE_UINT32_2DARRAY_V(_f, _s, _n1, _n2, _v)                \
694     VMSTATE_2DARRAY(_f, _s, _n1, _n2, _v, vmstate_info_uint32, uint32_t)
695 
696 #define VMSTATE_UINT32_ARRAY(_f, _s, _n)                              \
697     VMSTATE_UINT32_ARRAY_V(_f, _s, _n, 0)
698 
699 #define VMSTATE_UINT32_2DARRAY(_f, _s, _n1, _n2)                      \
700     VMSTATE_UINT32_2DARRAY_V(_f, _s, _n1, _n2, 0)
701 
702 #define VMSTATE_UINT64_ARRAY_V(_f, _s, _n, _v)                        \
703     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint64, uint64_t)
704 
705 #define VMSTATE_UINT64_ARRAY(_f, _s, _n)                              \
706     VMSTATE_UINT64_ARRAY_V(_f, _s, _n, 0)
707 
708 #define VMSTATE_INT16_ARRAY_V(_f, _s, _n, _v)                         \
709     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_int16, int16_t)
710 
711 #define VMSTATE_INT16_ARRAY(_f, _s, _n)                               \
712     VMSTATE_INT16_ARRAY_V(_f, _s, _n, 0)
713 
714 #define VMSTATE_INT32_ARRAY_V(_f, _s, _n, _v)                         \
715     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_int32, int32_t)
716 
717 #define VMSTATE_INT32_ARRAY(_f, _s, _n)                               \
718     VMSTATE_INT32_ARRAY_V(_f, _s, _n, 0)
719 
720 #define VMSTATE_UINT32_SUB_ARRAY(_f, _s, _start, _num)                \
721     VMSTATE_SUB_ARRAY(_f, _s, _start, _num, 0, vmstate_info_uint32, uint32_t)
722 
723 #define VMSTATE_UINT32_ARRAY(_f, _s, _n)                              \
724     VMSTATE_UINT32_ARRAY_V(_f, _s, _n, 0)
725 
726 #define VMSTATE_INT64_ARRAY_V(_f, _s, _n, _v)                         \
727     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_int64, int64_t)
728 
729 #define VMSTATE_INT64_ARRAY(_f, _s, _n)                               \
730     VMSTATE_INT64_ARRAY_V(_f, _s, _n, 0)
731 
732 #define VMSTATE_FLOAT64_ARRAY_V(_f, _s, _n, _v)                       \
733     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_float64, float64)
734 
735 #define VMSTATE_FLOAT64_ARRAY(_f, _s, _n)                             \
736     VMSTATE_FLOAT64_ARRAY_V(_f, _s, _n, 0)
737 
738 #define VMSTATE_BUFFER_V(_f, _s, _v)                                  \
739     VMSTATE_STATIC_BUFFER(_f, _s, _v, NULL, 0, sizeof(typeof_field(_s, _f)))
740 
741 #define VMSTATE_BUFFER(_f, _s)                                        \
742     VMSTATE_BUFFER_V(_f, _s, 0)
743 
744 #define VMSTATE_PARTIAL_BUFFER(_f, _s, _size)                         \
745     VMSTATE_STATIC_BUFFER(_f, _s, 0, NULL, 0, _size)
746 
747 #define VMSTATE_BUFFER_START_MIDDLE(_f, _s, _start) \
748     VMSTATE_STATIC_BUFFER(_f, _s, 0, NULL, _start, sizeof(typeof_field(_s, _f)))
749 
750 #define VMSTATE_PARTIAL_VBUFFER(_f, _s, _size)                        \
751     VMSTATE_VBUFFER(_f, _s, 0, NULL, 0, _size)
752 
753 #define VMSTATE_PARTIAL_VBUFFER_UINT32(_f, _s, _size)                        \
754     VMSTATE_VBUFFER_UINT32(_f, _s, 0, NULL, 0, _size)
755 
756 #define VMSTATE_SUB_VBUFFER(_f, _s, _start, _size)                    \
757     VMSTATE_VBUFFER(_f, _s, 0, NULL, _start, _size)
758 
759 #define VMSTATE_BUFFER_TEST(_f, _s, _test)                            \
760     VMSTATE_STATIC_BUFFER(_f, _s, 0, _test, 0, sizeof(typeof_field(_s, _f)))
761 
762 #define VMSTATE_BUFFER_UNSAFE(_field, _state, _version, _size)        \
763     VMSTATE_BUFFER_UNSAFE_INFO(_field, _state, _version, vmstate_info_buffer, _size)
764 
765 #define VMSTATE_UNUSED_V(_v, _size)                                   \
766     VMSTATE_UNUSED_BUFFER(NULL, _v, _size)
767 
768 #define VMSTATE_UNUSED(_size)                                         \
769     VMSTATE_UNUSED_V(0, _size)
770 
771 #define VMSTATE_UNUSED_TEST(_test, _size)                             \
772     VMSTATE_UNUSED_BUFFER(_test, 0, _size)
773 
774 #define VMSTATE_END_OF_LIST()                                         \
775     {}
776 
777 #define SELF_ANNOUNCE_ROUNDS 5
778 
779 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
780                        void *opaque, int version_id);
781 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
782                         void *opaque);
783 
784 int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
785                                    const VMStateDescription *vmsd,
786                                    void *base, int alias_id,
787                                    int required_for_version);
788 
789 static inline int vmstate_register(DeviceState *dev, int instance_id,
790                                    const VMStateDescription *vmsd,
791                                    void *opaque)
792 {
793     return vmstate_register_with_alias_id(dev, instance_id, vmsd,
794                                           opaque, -1, 0);
795 }
796 
797 void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
798                         void *opaque);
799 
800 struct MemoryRegion;
801 void vmstate_register_ram(struct MemoryRegion *memory, DeviceState *dev);
802 void vmstate_unregister_ram(struct MemoryRegion *memory, DeviceState *dev);
803 void vmstate_register_ram_global(struct MemoryRegion *memory);
804 
805 static inline
806 int64_t self_announce_delay(int round)
807 {
808     assert(round < SELF_ANNOUNCE_ROUNDS && round > 0);
809     /* delay 50ms, 150ms, 250ms, ... */
810     return 50 + (SELF_ANNOUNCE_ROUNDS - round - 1) * 100;
811 }
812 
813 void dump_vmstate_json_to_file(FILE *out_fp);
814 
815 #endif
816