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