xref: /openbmc/qemu/include/migration/vmstate.h (revision 8f1e884b)
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 };
105 
106 typedef struct {
107     const char *name;
108     size_t offset;
109     size_t size;
110     size_t start;
111     int num;
112     size_t num_offset;
113     size_t size_offset;
114     const VMStateInfo *info;
115     enum VMStateFlags flags;
116     const VMStateDescription *vmsd;
117     int version_id;
118     bool (*field_exists)(void *opaque, int version_id);
119 } VMStateField;
120 
121 typedef struct VMStateSubsection {
122     const VMStateDescription *vmsd;
123     bool (*needed)(void *opaque);
124 } VMStateSubsection;
125 
126 struct VMStateDescription {
127     const char *name;
128     int unmigratable;
129     int version_id;
130     int minimum_version_id;
131     int minimum_version_id_old;
132     LoadStateHandler *load_state_old;
133     int (*pre_load)(void *opaque);
134     int (*post_load)(void *opaque, int version_id);
135     void (*pre_save)(void *opaque);
136     VMStateField *fields;
137     const VMStateSubsection *subsections;
138 };
139 
140 #ifdef CONFIG_USER_ONLY
141 extern const VMStateDescription vmstate_dummy;
142 #endif
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     (offsetof(_state, _field[_start]))
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_ARRAY_TEST(_field, _state, _num, _test, _version, _vmsd, _type) { \
362     .name         = (stringify(_field)),                             \
363     .num          = (_num),                                          \
364     .field_exists = (_test),                                         \
365     .version_id   = (_version),                                      \
366     .vmsd         = &(_vmsd),                                        \
367     .size         = sizeof(_type),                                   \
368     .flags        = VMS_STRUCT|VMS_ARRAY,                            \
369     .offset       = vmstate_offset_array(_state, _field, _type, _num),\
370 }
371 
372 #define VMSTATE_STRUCT_VARRAY_UINT8(_field, _state, _field_num, _version, _vmsd, _type) { \
373     .name       = (stringify(_field)),                               \
374     .num_offset = vmstate_offset_value(_state, _field_num, uint8_t), \
375     .version_id = (_version),                                        \
376     .vmsd       = &(_vmsd),                                          \
377     .size       = sizeof(_type),                                     \
378     .flags      = VMS_STRUCT|VMS_VARRAY_UINT8,                       \
379     .offset     = offsetof(_state, _field),                          \
380 }
381 
382 #define VMSTATE_STRUCT_VARRAY_POINTER_INT32(_field, _state, _field_num, _vmsd, _type) { \
383     .name       = (stringify(_field)),                               \
384     .version_id = 0,                                                 \
385     .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
386     .size       = sizeof(_type),                                     \
387     .vmsd       = &(_vmsd),                                          \
388     .flags      = VMS_POINTER | VMS_VARRAY_INT32 | VMS_STRUCT,       \
389     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
390 }
391 
392 #define VMSTATE_STRUCT_VARRAY_POINTER_UINT32(_field, _state, _field_num, _vmsd, _type) { \
393     .name       = (stringify(_field)),                               \
394     .version_id = 0,                                                 \
395     .num_offset = vmstate_offset_value(_state, _field_num, uint32_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_UINT16(_field, _state, _field_num, _vmsd, _type) { \
403     .name       = (stringify(_field)),                               \
404     .version_id = 0,                                                 \
405     .num_offset = vmstate_offset_value(_state, _field_num, uint16_t),\
406     .size       = sizeof(_type),                                     \
407     .vmsd       = &(_vmsd),                                          \
408     .flags      = VMS_POINTER | VMS_VARRAY_UINT16 | VMS_STRUCT,      \
409     .offset     = vmstate_offset_pointer(_state, _field, _type),     \
410 }
411 
412 #define VMSTATE_STRUCT_VARRAY_INT32(_field, _state, _field_num, _version, _vmsd, _type) { \
413     .name       = (stringify(_field)),                               \
414     .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
415     .version_id = (_version),                                        \
416     .vmsd       = &(_vmsd),                                          \
417     .size       = sizeof(_type),                                     \
418     .flags      = VMS_STRUCT|VMS_VARRAY_INT32,                       \
419     .offset     = offsetof(_state, _field),                          \
420 }
421 
422 #define VMSTATE_STRUCT_VARRAY_UINT32(_field, _state, _field_num, _version, _vmsd, _type) { \
423     .name       = (stringify(_field)),                               \
424     .num_offset = vmstate_offset_value(_state, _field_num, uint32_t), \
425     .version_id = (_version),                                        \
426     .vmsd       = &(_vmsd),                                          \
427     .size       = sizeof(_type),                                     \
428     .flags      = VMS_STRUCT|VMS_VARRAY_UINT32,                      \
429     .offset     = offsetof(_state, _field),                          \
430 }
431 
432 #define VMSTATE_STATIC_BUFFER(_field, _state, _version, _test, _start, _size) { \
433     .name         = (stringify(_field)),                             \
434     .version_id   = (_version),                                      \
435     .field_exists = (_test),                                         \
436     .size         = (_size - _start),                                \
437     .info         = &vmstate_info_buffer,                            \
438     .flags        = VMS_BUFFER,                                      \
439     .offset       = vmstate_offset_buffer(_state, _field) + _start,  \
440 }
441 
442 #define VMSTATE_VBUFFER_MULTIPLY(_field, _state, _version, _test, _start, _field_size, _multiply) { \
443     .name         = (stringify(_field)),                             \
444     .version_id   = (_version),                                      \
445     .field_exists = (_test),                                         \
446     .size_offset  = vmstate_offset_value(_state, _field_size, uint32_t),\
447     .size         = (_multiply),                                      \
448     .info         = &vmstate_info_buffer,                            \
449     .flags        = VMS_VBUFFER|VMS_POINTER|VMS_MULTIPLY,            \
450     .offset       = offsetof(_state, _field),                        \
451     .start        = (_start),                                        \
452 }
453 
454 #define VMSTATE_VBUFFER(_field, _state, _version, _test, _start, _field_size) { \
455     .name         = (stringify(_field)),                             \
456     .version_id   = (_version),                                      \
457     .field_exists = (_test),                                         \
458     .size_offset  = vmstate_offset_value(_state, _field_size, int32_t),\
459     .info         = &vmstate_info_buffer,                            \
460     .flags        = VMS_VBUFFER|VMS_POINTER,                         \
461     .offset       = offsetof(_state, _field),                        \
462     .start        = (_start),                                        \
463 }
464 
465 #define VMSTATE_VBUFFER_UINT32(_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, uint32_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_BUFFER_UNSAFE_INFO(_field, _state, _version, _info, _size) { \
477     .name       = (stringify(_field)),                               \
478     .version_id = (_version),                                        \
479     .size       = (_size),                                           \
480     .info       = &(_info),                                          \
481     .flags      = VMS_BUFFER,                                        \
482     .offset     = offsetof(_state, _field),                          \
483 }
484 
485 #define VMSTATE_BUFFER_POINTER_UNSAFE(_field, _state, _version, _size) { \
486     .name       = (stringify(_field)),                               \
487     .version_id = (_version),                                        \
488     .size       = (_size),                                           \
489     .info       = &vmstate_info_buffer,                              \
490     .flags      = VMS_BUFFER|VMS_POINTER,                            \
491     .offset     = offsetof(_state, _field),                          \
492 }
493 
494 #define VMSTATE_UNUSED_BUFFER(_test, _version, _size) {              \
495     .name         = "unused",                                        \
496     .field_exists = (_test),                                         \
497     .version_id   = (_version),                                      \
498     .size         = (_size),                                         \
499     .info         = &vmstate_info_unused_buffer,                     \
500     .flags        = VMS_BUFFER,                                      \
501 }
502 
503 /* _field_size should be a int32_t field in the _state struct giving the
504  * size of the bitmap _field in bits.
505  */
506 #define VMSTATE_BITMAP(_field, _state, _version, _field_size) {      \
507     .name         = (stringify(_field)),                             \
508     .version_id   = (_version),                                      \
509     .size_offset  = vmstate_offset_value(_state, _field_size, int32_t),\
510     .info         = &vmstate_info_bitmap,                            \
511     .flags        = VMS_VBUFFER|VMS_POINTER,                         \
512     .offset       = offsetof(_state, _field),                        \
513 }
514 
515 /* _f : field name
516    _f_n : num of elements field_name
517    _n : num of elements
518    _s : struct state name
519    _v : version
520 */
521 
522 #define VMSTATE_SINGLE(_field, _state, _version, _info, _type)        \
523     VMSTATE_SINGLE_TEST(_field, _state, NULL, _version, _info, _type)
524 
525 #define VMSTATE_STRUCT(_field, _state, _version, _vmsd, _type)        \
526     VMSTATE_STRUCT_TEST(_field, _state, NULL, _version, _vmsd, _type)
527 
528 #define VMSTATE_STRUCT_POINTER(_field, _state, _vmsd, _type)          \
529     VMSTATE_STRUCT_POINTER_V(_field, _state, 0, _vmsd, _type)
530 
531 #define VMSTATE_STRUCT_POINTER_TEST(_field, _state, _test, _vmsd, _type)     \
532     VMSTATE_STRUCT_POINTER_TEST_V(_field, _state, _test, 0, _vmsd, _type)
533 
534 #define VMSTATE_STRUCT_ARRAY(_field, _state, _num, _version, _vmsd, _type) \
535     VMSTATE_STRUCT_ARRAY_TEST(_field, _state, _num, NULL, _version,   \
536             _vmsd, _type)
537 
538 #define VMSTATE_BOOL_V(_f, _s, _v)                                    \
539     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_bool, bool)
540 
541 #define VMSTATE_INT8_V(_f, _s, _v)                                    \
542     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int8, int8_t)
543 #define VMSTATE_INT16_V(_f, _s, _v)                                   \
544     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int16, int16_t)
545 #define VMSTATE_INT32_V(_f, _s, _v)                                   \
546     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int32, int32_t)
547 #define VMSTATE_INT64_V(_f, _s, _v)                                   \
548     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int64, int64_t)
549 
550 #define VMSTATE_UINT8_V(_f, _s, _v)                                   \
551     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint8, uint8_t)
552 #define VMSTATE_UINT16_V(_f, _s, _v)                                  \
553     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint16, uint16_t)
554 #define VMSTATE_UINT32_V(_f, _s, _v)                                  \
555     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint32, uint32_t)
556 #define VMSTATE_UINT64_V(_f, _s, _v)                                  \
557     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint64, uint64_t)
558 
559 #define VMSTATE_BOOL(_f, _s)                                          \
560     VMSTATE_BOOL_V(_f, _s, 0)
561 
562 #define VMSTATE_INT8(_f, _s)                                          \
563     VMSTATE_INT8_V(_f, _s, 0)
564 #define VMSTATE_INT16(_f, _s)                                         \
565     VMSTATE_INT16_V(_f, _s, 0)
566 #define VMSTATE_INT32(_f, _s)                                         \
567     VMSTATE_INT32_V(_f, _s, 0)
568 #define VMSTATE_INT64(_f, _s)                                         \
569     VMSTATE_INT64_V(_f, _s, 0)
570 
571 #define VMSTATE_UINT8(_f, _s)                                         \
572     VMSTATE_UINT8_V(_f, _s, 0)
573 #define VMSTATE_UINT16(_f, _s)                                        \
574     VMSTATE_UINT16_V(_f, _s, 0)
575 #define VMSTATE_UINT32(_f, _s)                                        \
576     VMSTATE_UINT32_V(_f, _s, 0)
577 #define VMSTATE_UINT64(_f, _s)                                        \
578     VMSTATE_UINT64_V(_f, _s, 0)
579 
580 #define VMSTATE_UINT8_EQUAL(_f, _s)                                   \
581     VMSTATE_SINGLE(_f, _s, 0, vmstate_info_uint8_equal, uint8_t)
582 
583 #define VMSTATE_UINT16_EQUAL(_f, _s)                                  \
584     VMSTATE_SINGLE(_f, _s, 0, vmstate_info_uint16_equal, uint16_t)
585 
586 #define VMSTATE_UINT16_EQUAL_V(_f, _s, _v)                            \
587     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint16_equal, uint16_t)
588 
589 #define VMSTATE_INT32_EQUAL(_f, _s)                                   \
590     VMSTATE_SINGLE(_f, _s, 0, vmstate_info_int32_equal, int32_t)
591 
592 #define VMSTATE_UINT32_EQUAL_V(_f, _s, _v)                            \
593     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint32_equal, uint32_t)
594 
595 #define VMSTATE_UINT32_EQUAL(_f, _s)                                  \
596     VMSTATE_UINT32_EQUAL_V(_f, _s, 0)
597 
598 #define VMSTATE_UINT64_EQUAL_V(_f, _s, _v)                            \
599     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint64_equal, uint64_t)
600 
601 #define VMSTATE_UINT64_EQUAL(_f, _s)                                  \
602     VMSTATE_UINT64_EQUAL_V(_f, _s, 0)
603 
604 #define VMSTATE_INT32_POSITIVE_LE(_f, _s)                             \
605     VMSTATE_SINGLE(_f, _s, 0, vmstate_info_int32_le, int32_t)
606 
607 #define VMSTATE_UINT8_TEST(_f, _s, _t)                               \
608     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint8, uint8_t)
609 
610 #define VMSTATE_UINT16_TEST(_f, _s, _t)                               \
611     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint16, uint16_t)
612 
613 #define VMSTATE_UINT32_TEST(_f, _s, _t)                                  \
614     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint32, uint32_t)
615 
616 
617 #define VMSTATE_FLOAT64_V(_f, _s, _v)                                 \
618     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_float64, float64)
619 
620 #define VMSTATE_FLOAT64(_f, _s)                                       \
621     VMSTATE_FLOAT64_V(_f, _s, 0)
622 
623 #define VMSTATE_TIMER_TEST(_f, _s, _test)                             \
624     VMSTATE_POINTER_TEST(_f, _s, _test, vmstate_info_timer, QEMUTimer *)
625 
626 #define VMSTATE_TIMER_V(_f, _s, _v)                                   \
627     VMSTATE_POINTER(_f, _s, _v, vmstate_info_timer, QEMUTimer *)
628 
629 #define VMSTATE_TIMER(_f, _s)                                         \
630     VMSTATE_TIMER_V(_f, _s, 0)
631 
632 #define VMSTATE_TIMER_ARRAY(_f, _s, _n)                              \
633     VMSTATE_ARRAY_OF_POINTER(_f, _s, _n, 0, vmstate_info_timer, QEMUTimer *)
634 
635 #define VMSTATE_BOOL_ARRAY_V(_f, _s, _n, _v)                         \
636     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_bool, bool)
637 
638 #define VMSTATE_BOOL_ARRAY(_f, _s, _n)                               \
639     VMSTATE_BOOL_ARRAY_V(_f, _s, _n, 0)
640 
641 #define VMSTATE_UINT16_ARRAY_V(_f, _s, _n, _v)                         \
642     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint16, uint16_t)
643 
644 #define VMSTATE_UINT16_2DARRAY_V(_f, _s, _n1, _n2, _v)                \
645     VMSTATE_2DARRAY(_f, _s, _n1, _n2, _v, vmstate_info_uint16, uint16_t)
646 
647 #define VMSTATE_UINT16_ARRAY(_f, _s, _n)                               \
648     VMSTATE_UINT16_ARRAY_V(_f, _s, _n, 0)
649 
650 #define VMSTATE_UINT16_2DARRAY(_f, _s, _n1, _n2)                      \
651     VMSTATE_UINT16_2DARRAY_V(_f, _s, _n1, _n2, 0)
652 
653 #define VMSTATE_UINT8_2DARRAY_V(_f, _s, _n1, _n2, _v)                 \
654     VMSTATE_2DARRAY(_f, _s, _n1, _n2, _v, vmstate_info_uint8, uint8_t)
655 
656 #define VMSTATE_UINT8_ARRAY_V(_f, _s, _n, _v)                         \
657     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint8, uint8_t)
658 
659 #define VMSTATE_UINT8_ARRAY(_f, _s, _n)                               \
660     VMSTATE_UINT8_ARRAY_V(_f, _s, _n, 0)
661 
662 #define VMSTATE_UINT8_SUB_ARRAY(_f, _s, _start, _num)                \
663     VMSTATE_SUB_ARRAY(_f, _s, _start, _num, 0, vmstate_info_uint8, uint8_t)
664 
665 #define VMSTATE_UINT8_2DARRAY(_f, _s, _n1, _n2)                       \
666     VMSTATE_UINT8_2DARRAY_V(_f, _s, _n1, _n2, 0)
667 
668 #define VMSTATE_UINT32_ARRAY_V(_f, _s, _n, _v)                        \
669     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint32, uint32_t)
670 
671 #define VMSTATE_UINT32_2DARRAY_V(_f, _s, _n1, _n2, _v)                \
672     VMSTATE_2DARRAY(_f, _s, _n1, _n2, _v, vmstate_info_uint32, uint32_t)
673 
674 #define VMSTATE_UINT32_ARRAY(_f, _s, _n)                              \
675     VMSTATE_UINT32_ARRAY_V(_f, _s, _n, 0)
676 
677 #define VMSTATE_UINT32_2DARRAY(_f, _s, _n1, _n2)                      \
678     VMSTATE_UINT32_2DARRAY_V(_f, _s, _n1, _n2, 0)
679 
680 #define VMSTATE_UINT64_ARRAY_V(_f, _s, _n, _v)                        \
681     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint64, uint64_t)
682 
683 #define VMSTATE_UINT64_ARRAY(_f, _s, _n)                              \
684     VMSTATE_UINT64_ARRAY_V(_f, _s, _n, 0)
685 
686 #define VMSTATE_INT16_ARRAY_V(_f, _s, _n, _v)                         \
687     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_int16, int16_t)
688 
689 #define VMSTATE_INT16_ARRAY(_f, _s, _n)                               \
690     VMSTATE_INT16_ARRAY_V(_f, _s, _n, 0)
691 
692 #define VMSTATE_INT32_ARRAY_V(_f, _s, _n, _v)                         \
693     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_int32, int32_t)
694 
695 #define VMSTATE_INT32_ARRAY(_f, _s, _n)                               \
696     VMSTATE_INT32_ARRAY_V(_f, _s, _n, 0)
697 
698 #define VMSTATE_UINT32_SUB_ARRAY(_f, _s, _start, _num)                \
699     VMSTATE_SUB_ARRAY(_f, _s, _start, _num, 0, vmstate_info_uint32, uint32_t)
700 
701 #define VMSTATE_UINT32_ARRAY(_f, _s, _n)                              \
702     VMSTATE_UINT32_ARRAY_V(_f, _s, _n, 0)
703 
704 #define VMSTATE_INT64_ARRAY_V(_f, _s, _n, _v)                         \
705     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_int64, int64_t)
706 
707 #define VMSTATE_INT64_ARRAY(_f, _s, _n)                               \
708     VMSTATE_INT64_ARRAY_V(_f, _s, _n, 0)
709 
710 #define VMSTATE_FLOAT64_ARRAY_V(_f, _s, _n, _v)                       \
711     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_float64, float64)
712 
713 #define VMSTATE_FLOAT64_ARRAY(_f, _s, _n)                             \
714     VMSTATE_FLOAT64_ARRAY_V(_f, _s, _n, 0)
715 
716 #define VMSTATE_BUFFER_V(_f, _s, _v)                                  \
717     VMSTATE_STATIC_BUFFER(_f, _s, _v, NULL, 0, sizeof(typeof_field(_s, _f)))
718 
719 #define VMSTATE_BUFFER(_f, _s)                                        \
720     VMSTATE_BUFFER_V(_f, _s, 0)
721 
722 #define VMSTATE_PARTIAL_BUFFER(_f, _s, _size)                         \
723     VMSTATE_STATIC_BUFFER(_f, _s, 0, NULL, 0, _size)
724 
725 #define VMSTATE_BUFFER_START_MIDDLE(_f, _s, _start) \
726     VMSTATE_STATIC_BUFFER(_f, _s, 0, NULL, _start, sizeof(typeof_field(_s, _f)))
727 
728 #define VMSTATE_PARTIAL_VBUFFER(_f, _s, _size)                        \
729     VMSTATE_VBUFFER(_f, _s, 0, NULL, 0, _size)
730 
731 #define VMSTATE_PARTIAL_VBUFFER_UINT32(_f, _s, _size)                        \
732     VMSTATE_VBUFFER_UINT32(_f, _s, 0, NULL, 0, _size)
733 
734 #define VMSTATE_SUB_VBUFFER(_f, _s, _start, _size)                    \
735     VMSTATE_VBUFFER(_f, _s, 0, NULL, _start, _size)
736 
737 #define VMSTATE_BUFFER_TEST(_f, _s, _test)                            \
738     VMSTATE_STATIC_BUFFER(_f, _s, 0, _test, 0, sizeof(typeof_field(_s, _f)))
739 
740 #define VMSTATE_BUFFER_UNSAFE(_field, _state, _version, _size)        \
741     VMSTATE_BUFFER_UNSAFE_INFO(_field, _state, _version, vmstate_info_buffer, _size)
742 
743 #define VMSTATE_UNUSED_V(_v, _size)                                   \
744     VMSTATE_UNUSED_BUFFER(NULL, _v, _size)
745 
746 #define VMSTATE_UNUSED(_size)                                         \
747     VMSTATE_UNUSED_V(0, _size)
748 
749 #define VMSTATE_UNUSED_TEST(_test, _size)                             \
750     VMSTATE_UNUSED_BUFFER(_test, 0, _size)
751 
752 #define VMSTATE_END_OF_LIST()                                         \
753     {}
754 
755 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
756                        void *opaque, int version_id);
757 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
758                         void *opaque);
759 
760 int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
761                                    const VMStateDescription *vmsd,
762                                    void *base, int alias_id,
763                                    int required_for_version);
764 
765 static inline int vmstate_register(DeviceState *dev, int instance_id,
766                                    const VMStateDescription *vmsd,
767                                    void *opaque)
768 {
769     return vmstate_register_with_alias_id(dev, instance_id, vmsd,
770                                           opaque, -1, 0);
771 }
772 
773 void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
774                         void *opaque);
775 
776 struct MemoryRegion;
777 void vmstate_register_ram(struct MemoryRegion *memory, DeviceState *dev);
778 void vmstate_unregister_ram(struct MemoryRegion *memory, DeviceState *dev);
779 void vmstate_register_ram_global(struct MemoryRegion *memory);
780 
781 #endif
782