hw/arm/virt-acpi-build: don't save VirtGuestInfo on AcpiBuildState
[qemu.git] / include / migration / vmstate.h
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
27 #ifndef QEMU_VMSTATE_H
28 #define QEMU_VMSTATE_H
29
30 #ifndef CONFIG_USER_ONLY
31 #include "migration/qemu-file.h"
32 #endif
33 #include "migration/qjson.h"
34
35 typedef void SaveStateHandler(QEMUFile *f, void *opaque);
36 typedef int LoadStateHandler(QEMUFile *f, void *opaque, int version_id);
37
38 typedef struct SaveVMHandlers {
39 /* This runs inside the iothread lock. */
40 void (*set_params)(const MigrationParams *params, void * opaque);
41 SaveStateHandler *save_state;
42
43 void (*cleanup)(void *opaque);
44 int (*save_live_complete_postcopy)(QEMUFile *f, void *opaque);
45 int (*save_live_complete_precopy)(QEMUFile *f, void *opaque);
46
47 /* This runs both outside and inside the iothread lock. */
48 bool (*is_active)(void *opaque);
49
50 /* This runs outside the iothread lock in the migration case, and
51 * within the lock in the savevm case. The callback had better only
52 * use data that is local to the migration thread or protected
53 * by other locks.
54 */
55 int (*save_live_iterate)(QEMUFile *f, void *opaque);
56
57 /* This runs outside the iothread lock! */
58 int (*save_live_setup)(QEMUFile *f, void *opaque);
59 void (*save_live_pending)(QEMUFile *f, void *opaque, uint64_t max_size,
60 uint64_t *non_postcopiable_pending,
61 uint64_t *postcopiable_pending);
62 LoadStateHandler *load_state;
63 } SaveVMHandlers;
64
65 int register_savevm(DeviceState *dev,
66 const char *idstr,
67 int instance_id,
68 int version_id,
69 SaveStateHandler *save_state,
70 LoadStateHandler *load_state,
71 void *opaque);
72
73 int register_savevm_live(DeviceState *dev,
74 const char *idstr,
75 int instance_id,
76 int version_id,
77 SaveVMHandlers *ops,
78 void *opaque);
79
80 void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque);
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 /* Ignored */
93 VMS_SINGLE = 0x001,
94
95 /* The struct member at opaque + VMStateField.offset is a pointer
96 * to the actual field (e.g. struct a { uint8_t *b;
97 * }). Dereference the pointer before using it as basis for
98 * further pointer arithmetic (see e.g. VMS_ARRAY). Does not
99 * affect the meaning of VMStateField.num_offset or
100 * VMStateField.size_offset; see VMS_VARRAY* and VMS_VBUFFER for
101 * those. */
102 VMS_POINTER = 0x002,
103
104 /* The field is an array of fixed size. VMStateField.num contains
105 * the number of entries in the array. The size of each entry is
106 * given by VMStateField.size and / or opaque +
107 * VMStateField.size_offset; see VMS_VBUFFER and
108 * VMS_MULTIPLY. Each array entry will be processed individually
109 * (VMStateField.info.get()/put() if VMS_STRUCT is not set,
110 * recursion into VMStateField.vmsd if VMS_STRUCT is set). May not
111 * be combined with VMS_VARRAY*. */
112 VMS_ARRAY = 0x004,
113
114 /* The field is itself a struct, containing one or more
115 * fields. Recurse into VMStateField.vmsd. Most useful in
116 * combination with VMS_ARRAY / VMS_VARRAY*, recursing into each
117 * array entry. */
118 VMS_STRUCT = 0x008,
119
120 /* The field is an array of variable size. The int32_t at opaque +
121 * VMStateField.num_offset contains the number of entries in the
122 * array. See the VMS_ARRAY description regarding array handling
123 * in general. May not be combined with VMS_ARRAY or any other
124 * VMS_VARRAY*. */
125 VMS_VARRAY_INT32 = 0x010,
126
127 /* Ignored */
128 VMS_BUFFER = 0x020,
129
130 /* The field is a (fixed-size or variable-size) array of pointers
131 * (e.g. struct a { uint8_t *b[]; }). Dereference each array entry
132 * before using it. Note: Does not imply any one of VMS_ARRAY /
133 * VMS_VARRAY*; these need to be set explicitly. */
134 VMS_ARRAY_OF_POINTER = 0x040,
135
136 /* The field is an array of variable size. The uint16_t at opaque
137 * + VMStateField.num_offset (subject to VMS_MULTIPLY_ELEMENTS)
138 * contains the number of entries in the array. See the VMS_ARRAY
139 * description regarding array handling in general. May not be
140 * combined with VMS_ARRAY or any other VMS_VARRAY*. */
141 VMS_VARRAY_UINT16 = 0x080,
142
143 /* The size of the individual entries (a single array entry if
144 * VMS_ARRAY or any of VMS_VARRAY* are set, or the field itself if
145 * neither is set) is variable (i.e. not known at compile-time),
146 * but the same for all entries. Use the int32_t at opaque +
147 * VMStateField.size_offset (subject to VMS_MULTIPLY) to determine
148 * the size of each (and every) entry. */
149 VMS_VBUFFER = 0x100,
150
151 /* Multiply the entry size given by the int32_t at opaque +
152 * VMStateField.size_offset (see VMS_VBUFFER description) with
153 * VMStateField.size to determine the number of bytes to be
154 * allocated. Only valid in combination with VMS_VBUFFER. */
155 VMS_MULTIPLY = 0x200,
156
157 /* The field is an array of variable size. The uint8_t at opaque +
158 * VMStateField.num_offset (subject to VMS_MULTIPLY_ELEMENTS)
159 * contains the number of entries in the array. See the VMS_ARRAY
160 * description regarding array handling in general. May not be
161 * combined with VMS_ARRAY or any other VMS_VARRAY*. */
162 VMS_VARRAY_UINT8 = 0x400,
163
164 /* The field is an array of variable size. The uint32_t at opaque
165 * + VMStateField.num_offset (subject to VMS_MULTIPLY_ELEMENTS)
166 * contains the number of entries in the array. See the VMS_ARRAY
167 * description regarding array handling in general. May not be
168 * combined with VMS_ARRAY or any other VMS_VARRAY*. */
169 VMS_VARRAY_UINT32 = 0x800,
170
171 /* Fail loading the serialised VM state if this field is missing
172 * from the input. */
173 VMS_MUST_EXIST = 0x1000,
174
175 /* When loading serialised VM state, allocate memory for the
176 * (entire) field. Only valid in combination with
177 * VMS_POINTER. Note: Not all combinations with other flags are
178 * currently supported, e.g. VMS_ALLOC|VMS_ARRAY_OF_POINTER won't
179 * cause the individual entries to be allocated. */
180 VMS_ALLOC = 0x2000,
181
182 /* Multiply the number of entries given by the integer at opaque +
183 * VMStateField.num_offset (see VMS_VARRAY*) with VMStateField.num
184 * to determine the number of entries in the array. Only valid in
185 * combination with one of VMS_VARRAY*. */
186 VMS_MULTIPLY_ELEMENTS = 0x4000,
187 };
188
189 typedef struct {
190 const char *name;
191 size_t offset;
192 size_t size;
193 size_t start;
194 int num;
195 size_t num_offset;
196 size_t size_offset;
197 const VMStateInfo *info;
198 enum VMStateFlags flags;
199 const VMStateDescription *vmsd;
200 int version_id;
201 bool (*field_exists)(void *opaque, int version_id);
202 } VMStateField;
203
204 struct VMStateDescription {
205 const char *name;
206 int unmigratable;
207 int version_id;
208 int minimum_version_id;
209 int minimum_version_id_old;
210 LoadStateHandler *load_state_old;
211 int (*pre_load)(void *opaque);
212 int (*post_load)(void *opaque, int version_id);
213 void (*pre_save)(void *opaque);
214 bool (*needed)(void *opaque);
215 VMStateField *fields;
216 const VMStateDescription **subsections;
217 };
218
219 extern const VMStateDescription vmstate_dummy;
220
221 extern const VMStateInfo vmstate_info_bool;
222
223 extern const VMStateInfo vmstate_info_int8;
224 extern const VMStateInfo vmstate_info_int16;
225 extern const VMStateInfo vmstate_info_int32;
226 extern const VMStateInfo vmstate_info_int64;
227
228 extern const VMStateInfo vmstate_info_uint8_equal;
229 extern const VMStateInfo vmstate_info_uint16_equal;
230 extern const VMStateInfo vmstate_info_int32_equal;
231 extern const VMStateInfo vmstate_info_uint32_equal;
232 extern const VMStateInfo vmstate_info_uint64_equal;
233 extern const VMStateInfo vmstate_info_int32_le;
234
235 extern const VMStateInfo vmstate_info_uint8;
236 extern const VMStateInfo vmstate_info_uint16;
237 extern const VMStateInfo vmstate_info_uint32;
238 extern const VMStateInfo vmstate_info_uint64;
239
240 extern const VMStateInfo vmstate_info_float64;
241 extern const VMStateInfo vmstate_info_cpudouble;
242
243 extern const VMStateInfo vmstate_info_timer;
244 extern const VMStateInfo vmstate_info_buffer;
245 extern const VMStateInfo vmstate_info_unused_buffer;
246 extern const VMStateInfo vmstate_info_bitmap;
247
248 #define type_check_2darray(t1,t2,n,m) ((t1(*)[n][m])0 - (t2*)0)
249 #define type_check_array(t1,t2,n) ((t1(*)[n])0 - (t2*)0)
250 #define type_check_pointer(t1,t2) ((t1**)0 - (t2*)0)
251
252 #define vmstate_offset_value(_state, _field, _type) \
253 (offsetof(_state, _field) + \
254 type_check(_type, typeof_field(_state, _field)))
255
256 #define vmstate_offset_pointer(_state, _field, _type) \
257 (offsetof(_state, _field) + \
258 type_check_pointer(_type, typeof_field(_state, _field)))
259
260 #define vmstate_offset_array(_state, _field, _type, _num) \
261 (offsetof(_state, _field) + \
262 type_check_array(_type, typeof_field(_state, _field), _num))
263
264 #define vmstate_offset_2darray(_state, _field, _type, _n1, _n2) \
265 (offsetof(_state, _field) + \
266 type_check_2darray(_type, typeof_field(_state, _field), _n1, _n2))
267
268 #define vmstate_offset_sub_array(_state, _field, _type, _start) \
269 vmstate_offset_value(_state, _field[_start], _type)
270
271 #define vmstate_offset_buffer(_state, _field) \
272 vmstate_offset_array(_state, _field, uint8_t, \
273 sizeof(typeof_field(_state, _field)))
274
275 #define VMSTATE_SINGLE_TEST(_field, _state, _test, _version, _info, _type) { \
276 .name = (stringify(_field)), \
277 .version_id = (_version), \
278 .field_exists = (_test), \
279 .size = sizeof(_type), \
280 .info = &(_info), \
281 .flags = VMS_SINGLE, \
282 .offset = vmstate_offset_value(_state, _field, _type), \
283 }
284
285 /* Validate state using a boolean predicate. */
286 #define VMSTATE_VALIDATE(_name, _test) { \
287 .name = (_name), \
288 .field_exists = (_test), \
289 .flags = VMS_ARRAY | VMS_MUST_EXIST, \
290 .num = 0, /* 0 elements: no data, only run _test */ \
291 }
292
293 #define VMSTATE_POINTER(_field, _state, _version, _info, _type) { \
294 .name = (stringify(_field)), \
295 .version_id = (_version), \
296 .info = &(_info), \
297 .size = sizeof(_type), \
298 .flags = VMS_SINGLE|VMS_POINTER, \
299 .offset = vmstate_offset_value(_state, _field, _type), \
300 }
301
302 #define VMSTATE_POINTER_TEST(_field, _state, _test, _info, _type) { \
303 .name = (stringify(_field)), \
304 .info = &(_info), \
305 .field_exists = (_test), \
306 .size = sizeof(_type), \
307 .flags = VMS_SINGLE|VMS_POINTER, \
308 .offset = vmstate_offset_value(_state, _field, _type), \
309 }
310
311 #define VMSTATE_ARRAY(_field, _state, _num, _version, _info, _type) {\
312 .name = (stringify(_field)), \
313 .version_id = (_version), \
314 .num = (_num), \
315 .info = &(_info), \
316 .size = sizeof(_type), \
317 .flags = VMS_ARRAY, \
318 .offset = vmstate_offset_array(_state, _field, _type, _num), \
319 }
320
321 #define VMSTATE_2DARRAY(_field, _state, _n1, _n2, _version, _info, _type) { \
322 .name = (stringify(_field)), \
323 .version_id = (_version), \
324 .num = (_n1) * (_n2), \
325 .info = &(_info), \
326 .size = sizeof(_type), \
327 .flags = VMS_ARRAY, \
328 .offset = vmstate_offset_2darray(_state, _field, _type, _n1, _n2), \
329 }
330
331 #define VMSTATE_VARRAY_MULTIPLY(_field, _state, _field_num, _multiply, _info, _type) { \
332 .name = (stringify(_field)), \
333 .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
334 .num = (_multiply), \
335 .info = &(_info), \
336 .size = sizeof(_type), \
337 .flags = VMS_VARRAY_UINT32|VMS_MULTIPLY_ELEMENTS, \
338 .offset = offsetof(_state, _field), \
339 }
340
341 #define VMSTATE_ARRAY_TEST(_field, _state, _num, _test, _info, _type) {\
342 .name = (stringify(_field)), \
343 .field_exists = (_test), \
344 .num = (_num), \
345 .info = &(_info), \
346 .size = sizeof(_type), \
347 .flags = VMS_ARRAY, \
348 .offset = vmstate_offset_array(_state, _field, _type, _num),\
349 }
350
351 #define VMSTATE_SUB_ARRAY(_field, _state, _start, _num, _version, _info, _type) { \
352 .name = (stringify(_field)), \
353 .version_id = (_version), \
354 .num = (_num), \
355 .info = &(_info), \
356 .size = sizeof(_type), \
357 .flags = VMS_ARRAY, \
358 .offset = vmstate_offset_sub_array(_state, _field, _type, _start), \
359 }
360
361 #define VMSTATE_ARRAY_INT32_UNSAFE(_field, _state, _field_num, _info, _type) {\
362 .name = (stringify(_field)), \
363 .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
364 .info = &(_info), \
365 .size = sizeof(_type), \
366 .flags = VMS_VARRAY_INT32, \
367 .offset = offsetof(_state, _field), \
368 }
369
370 #define VMSTATE_VARRAY_INT32(_field, _state, _field_num, _version, _info, _type) {\
371 .name = (stringify(_field)), \
372 .version_id = (_version), \
373 .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
374 .info = &(_info), \
375 .size = sizeof(_type), \
376 .flags = VMS_VARRAY_INT32|VMS_POINTER, \
377 .offset = vmstate_offset_pointer(_state, _field, _type), \
378 }
379
380 #define VMSTATE_VARRAY_UINT32(_field, _state, _field_num, _version, _info, _type) {\
381 .name = (stringify(_field)), \
382 .version_id = (_version), \
383 .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
384 .info = &(_info), \
385 .size = sizeof(_type), \
386 .flags = VMS_VARRAY_UINT32|VMS_POINTER, \
387 .offset = vmstate_offset_pointer(_state, _field, _type), \
388 }
389
390 #define VMSTATE_VARRAY_UINT32_ALLOC(_field, _state, _field_num, _version, _info, _type) {\
391 .name = (stringify(_field)), \
392 .version_id = (_version), \
393 .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
394 .info = &(_info), \
395 .size = sizeof(_type), \
396 .flags = VMS_VARRAY_UINT32|VMS_POINTER|VMS_ALLOC, \
397 .offset = vmstate_offset_pointer(_state, _field, _type), \
398 }
399
400 #define VMSTATE_VARRAY_UINT16_UNSAFE(_field, _state, _field_num, _version, _info, _type) {\
401 .name = (stringify(_field)), \
402 .version_id = (_version), \
403 .num_offset = vmstate_offset_value(_state, _field_num, uint16_t),\
404 .info = &(_info), \
405 .size = sizeof(_type), \
406 .flags = VMS_VARRAY_UINT16, \
407 .offset = offsetof(_state, _field), \
408 }
409
410 #define VMSTATE_STRUCT_TEST(_field, _state, _test, _version, _vmsd, _type) { \
411 .name = (stringify(_field)), \
412 .version_id = (_version), \
413 .field_exists = (_test), \
414 .vmsd = &(_vmsd), \
415 .size = sizeof(_type), \
416 .flags = VMS_STRUCT, \
417 .offset = vmstate_offset_value(_state, _field, _type), \
418 }
419
420 #define VMSTATE_STRUCT_POINTER_V(_field, _state, _version, _vmsd, _type) { \
421 .name = (stringify(_field)), \
422 .version_id = (_version), \
423 .vmsd = &(_vmsd), \
424 .size = sizeof(_type *), \
425 .flags = VMS_STRUCT|VMS_POINTER, \
426 .offset = vmstate_offset_pointer(_state, _field, _type), \
427 }
428
429 #define VMSTATE_STRUCT_POINTER_TEST_V(_field, _state, _test, _version, _vmsd, _type) { \
430 .name = (stringify(_field)), \
431 .version_id = (_version), \
432 .field_exists = (_test), \
433 .vmsd = &(_vmsd), \
434 .size = sizeof(_type *), \
435 .flags = VMS_STRUCT|VMS_POINTER, \
436 .offset = vmstate_offset_pointer(_state, _field, _type), \
437 }
438
439 #define VMSTATE_ARRAY_OF_POINTER(_field, _state, _num, _version, _info, _type) {\
440 .name = (stringify(_field)), \
441 .version_id = (_version), \
442 .num = (_num), \
443 .info = &(_info), \
444 .size = sizeof(_type), \
445 .flags = VMS_ARRAY|VMS_ARRAY_OF_POINTER, \
446 .offset = vmstate_offset_array(_state, _field, _type, _num), \
447 }
448
449 #define VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(_f, _s, _n, _v, _vmsd, _type) { \
450 .name = (stringify(_f)), \
451 .version_id = (_v), \
452 .num = (_n), \
453 .vmsd = &(_vmsd), \
454 .size = sizeof(_type *), \
455 .flags = VMS_ARRAY|VMS_STRUCT|VMS_ARRAY_OF_POINTER, \
456 .offset = vmstate_offset_array(_s, _f, _type*, _n), \
457 }
458
459 #define VMSTATE_STRUCT_SUB_ARRAY(_field, _state, _start, _num, _version, _vmsd, _type) { \
460 .name = (stringify(_field)), \
461 .version_id = (_version), \
462 .num = (_num), \
463 .vmsd = &(_vmsd), \
464 .size = sizeof(_type), \
465 .flags = VMS_STRUCT|VMS_ARRAY, \
466 .offset = vmstate_offset_sub_array(_state, _field, _type, _start), \
467 }
468
469 #define VMSTATE_STRUCT_ARRAY_TEST(_field, _state, _num, _test, _version, _vmsd, _type) { \
470 .name = (stringify(_field)), \
471 .num = (_num), \
472 .field_exists = (_test), \
473 .version_id = (_version), \
474 .vmsd = &(_vmsd), \
475 .size = sizeof(_type), \
476 .flags = VMS_STRUCT|VMS_ARRAY, \
477 .offset = vmstate_offset_array(_state, _field, _type, _num),\
478 }
479
480 #define VMSTATE_STRUCT_VARRAY_UINT8(_field, _state, _field_num, _version, _vmsd, _type) { \
481 .name = (stringify(_field)), \
482 .num_offset = vmstate_offset_value(_state, _field_num, uint8_t), \
483 .version_id = (_version), \
484 .vmsd = &(_vmsd), \
485 .size = sizeof(_type), \
486 .flags = VMS_STRUCT|VMS_VARRAY_UINT8, \
487 .offset = offsetof(_state, _field), \
488 }
489
490 /* a variable length array (i.e. _type *_field) but we know the
491 * length
492 */
493 #define VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(_field, _state, _num, _version, _vmsd, _type) { \
494 .name = (stringify(_field)), \
495 .num = (_num), \
496 .version_id = (_version), \
497 .vmsd = &(_vmsd), \
498 .size = sizeof(_type), \
499 .flags = VMS_STRUCT|VMS_ARRAY|VMS_POINTER, \
500 .offset = offsetof(_state, _field), \
501 }
502
503 #define VMSTATE_STRUCT_VARRAY_POINTER_INT32(_field, _state, _field_num, _vmsd, _type) { \
504 .name = (stringify(_field)), \
505 .version_id = 0, \
506 .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
507 .size = sizeof(_type), \
508 .vmsd = &(_vmsd), \
509 .flags = VMS_POINTER | VMS_VARRAY_INT32 | VMS_STRUCT, \
510 .offset = vmstate_offset_pointer(_state, _field, _type), \
511 }
512
513 #define VMSTATE_STRUCT_VARRAY_POINTER_UINT32(_field, _state, _field_num, _vmsd, _type) { \
514 .name = (stringify(_field)), \
515 .version_id = 0, \
516 .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\
517 .size = sizeof(_type), \
518 .vmsd = &(_vmsd), \
519 .flags = VMS_POINTER | VMS_VARRAY_INT32 | VMS_STRUCT, \
520 .offset = vmstate_offset_pointer(_state, _field, _type), \
521 }
522
523 #define VMSTATE_STRUCT_VARRAY_POINTER_UINT16(_field, _state, _field_num, _vmsd, _type) { \
524 .name = (stringify(_field)), \
525 .version_id = 0, \
526 .num_offset = vmstate_offset_value(_state, _field_num, uint16_t),\
527 .size = sizeof(_type), \
528 .vmsd = &(_vmsd), \
529 .flags = VMS_POINTER | VMS_VARRAY_UINT16 | VMS_STRUCT, \
530 .offset = vmstate_offset_pointer(_state, _field, _type), \
531 }
532
533 #define VMSTATE_STRUCT_VARRAY_INT32(_field, _state, _field_num, _version, _vmsd, _type) { \
534 .name = (stringify(_field)), \
535 .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
536 .version_id = (_version), \
537 .vmsd = &(_vmsd), \
538 .size = sizeof(_type), \
539 .flags = VMS_STRUCT|VMS_VARRAY_INT32, \
540 .offset = offsetof(_state, _field), \
541 }
542
543 #define VMSTATE_STRUCT_VARRAY_UINT32(_field, _state, _field_num, _version, _vmsd, _type) { \
544 .name = (stringify(_field)), \
545 .num_offset = vmstate_offset_value(_state, _field_num, uint32_t), \
546 .version_id = (_version), \
547 .vmsd = &(_vmsd), \
548 .size = sizeof(_type), \
549 .flags = VMS_STRUCT|VMS_VARRAY_UINT32, \
550 .offset = offsetof(_state, _field), \
551 }
552
553 #define VMSTATE_STRUCT_VARRAY_ALLOC(_field, _state, _field_num, _version, _vmsd, _type) {\
554 .name = (stringify(_field)), \
555 .version_id = (_version), \
556 .vmsd = &(_vmsd), \
557 .num_offset = vmstate_offset_value(_state, _field_num, int32_t), \
558 .size = sizeof(_type), \
559 .flags = VMS_STRUCT|VMS_VARRAY_INT32|VMS_ALLOC|VMS_POINTER, \
560 .offset = vmstate_offset_pointer(_state, _field, _type), \
561 }
562
563 #define VMSTATE_STATIC_BUFFER(_field, _state, _version, _test, _start, _size) { \
564 .name = (stringify(_field)), \
565 .version_id = (_version), \
566 .field_exists = (_test), \
567 .size = (_size - _start), \
568 .info = &vmstate_info_buffer, \
569 .flags = VMS_BUFFER, \
570 .offset = vmstate_offset_buffer(_state, _field) + _start, \
571 }
572
573 #define VMSTATE_VBUFFER_MULTIPLY(_field, _state, _version, _test, _start, _field_size, _multiply) { \
574 .name = (stringify(_field)), \
575 .version_id = (_version), \
576 .field_exists = (_test), \
577 .size_offset = vmstate_offset_value(_state, _field_size, uint32_t),\
578 .size = (_multiply), \
579 .info = &vmstate_info_buffer, \
580 .flags = VMS_VBUFFER|VMS_POINTER|VMS_MULTIPLY, \
581 .offset = offsetof(_state, _field), \
582 .start = (_start), \
583 }
584
585 #define VMSTATE_VBUFFER(_field, _state, _version, _test, _start, _field_size) { \
586 .name = (stringify(_field)), \
587 .version_id = (_version), \
588 .field_exists = (_test), \
589 .size_offset = vmstate_offset_value(_state, _field_size, int32_t),\
590 .info = &vmstate_info_buffer, \
591 .flags = VMS_VBUFFER|VMS_POINTER, \
592 .offset = offsetof(_state, _field), \
593 .start = (_start), \
594 }
595
596 #define VMSTATE_VBUFFER_UINT32(_field, _state, _version, _test, _start, _field_size) { \
597 .name = (stringify(_field)), \
598 .version_id = (_version), \
599 .field_exists = (_test), \
600 .size_offset = vmstate_offset_value(_state, _field_size, uint32_t),\
601 .info = &vmstate_info_buffer, \
602 .flags = VMS_VBUFFER|VMS_POINTER, \
603 .offset = offsetof(_state, _field), \
604 .start = (_start), \
605 }
606
607 #define VMSTATE_VBUFFER_ALLOC_UINT32(_field, _state, _version, _test, _start, _field_size) { \
608 .name = (stringify(_field)), \
609 .version_id = (_version), \
610 .field_exists = (_test), \
611 .size_offset = vmstate_offset_value(_state, _field_size, uint32_t),\
612 .info = &vmstate_info_buffer, \
613 .flags = VMS_VBUFFER|VMS_POINTER|VMS_ALLOC, \
614 .offset = offsetof(_state, _field), \
615 .start = (_start), \
616 }
617
618 #define VMSTATE_BUFFER_UNSAFE_INFO_TEST(_field, _state, _test, _version, _info, _size) { \
619 .name = (stringify(_field)), \
620 .version_id = (_version), \
621 .field_exists = (_test), \
622 .size = (_size), \
623 .info = &(_info), \
624 .flags = VMS_BUFFER, \
625 .offset = offsetof(_state, _field), \
626 }
627
628 #define VMSTATE_BUFFER_POINTER_UNSAFE(_field, _state, _version, _size) { \
629 .name = (stringify(_field)), \
630 .version_id = (_version), \
631 .size = (_size), \
632 .info = &vmstate_info_buffer, \
633 .flags = VMS_BUFFER|VMS_POINTER, \
634 .offset = offsetof(_state, _field), \
635 }
636
637 #define VMSTATE_UNUSED_BUFFER(_test, _version, _size) { \
638 .name = "unused", \
639 .field_exists = (_test), \
640 .version_id = (_version), \
641 .size = (_size), \
642 .info = &vmstate_info_unused_buffer, \
643 .flags = VMS_BUFFER, \
644 }
645
646 /* _field_size should be a int32_t field in the _state struct giving the
647 * size of the bitmap _field in bits.
648 */
649 #define VMSTATE_BITMAP(_field, _state, _version, _field_size) { \
650 .name = (stringify(_field)), \
651 .version_id = (_version), \
652 .size_offset = vmstate_offset_value(_state, _field_size, int32_t),\
653 .info = &vmstate_info_bitmap, \
654 .flags = VMS_VBUFFER|VMS_POINTER, \
655 .offset = offsetof(_state, _field), \
656 }
657
658 /* _f : field name
659 _f_n : num of elements field_name
660 _n : num of elements
661 _s : struct state name
662 _v : version
663 */
664
665 #define VMSTATE_SINGLE(_field, _state, _version, _info, _type) \
666 VMSTATE_SINGLE_TEST(_field, _state, NULL, _version, _info, _type)
667
668 #define VMSTATE_STRUCT(_field, _state, _version, _vmsd, _type) \
669 VMSTATE_STRUCT_TEST(_field, _state, NULL, _version, _vmsd, _type)
670
671 #define VMSTATE_STRUCT_POINTER(_field, _state, _vmsd, _type) \
672 VMSTATE_STRUCT_POINTER_V(_field, _state, 0, _vmsd, _type)
673
674 #define VMSTATE_STRUCT_POINTER_TEST(_field, _state, _test, _vmsd, _type) \
675 VMSTATE_STRUCT_POINTER_TEST_V(_field, _state, _test, 0, _vmsd, _type)
676
677 #define VMSTATE_STRUCT_ARRAY(_field, _state, _num, _version, _vmsd, _type) \
678 VMSTATE_STRUCT_ARRAY_TEST(_field, _state, _num, NULL, _version, \
679 _vmsd, _type)
680
681 #define VMSTATE_BUFFER_UNSAFE_INFO(_field, _state, _version, _info, _size) \
682 VMSTATE_BUFFER_UNSAFE_INFO_TEST(_field, _state, NULL, _version, _info, \
683 _size)
684
685 #define VMSTATE_BOOL_V(_f, _s, _v) \
686 VMSTATE_SINGLE(_f, _s, _v, vmstate_info_bool, bool)
687
688 #define VMSTATE_INT8_V(_f, _s, _v) \
689 VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int8, int8_t)
690 #define VMSTATE_INT16_V(_f, _s, _v) \
691 VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int16, int16_t)
692 #define VMSTATE_INT32_V(_f, _s, _v) \
693 VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int32, int32_t)
694 #define VMSTATE_INT64_V(_f, _s, _v) \
695 VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int64, int64_t)
696
697 #define VMSTATE_UINT8_V(_f, _s, _v) \
698 VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint8, uint8_t)
699 #define VMSTATE_UINT16_V(_f, _s, _v) \
700 VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint16, uint16_t)
701 #define VMSTATE_UINT32_V(_f, _s, _v) \
702 VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint32, uint32_t)
703 #define VMSTATE_UINT64_V(_f, _s, _v) \
704 VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint64, uint64_t)
705
706 #define VMSTATE_BOOL(_f, _s) \
707 VMSTATE_BOOL_V(_f, _s, 0)
708
709 #define VMSTATE_INT8(_f, _s) \
710 VMSTATE_INT8_V(_f, _s, 0)
711 #define VMSTATE_INT16(_f, _s) \
712 VMSTATE_INT16_V(_f, _s, 0)
713 #define VMSTATE_INT32(_f, _s) \
714 VMSTATE_INT32_V(_f, _s, 0)
715 #define VMSTATE_INT64(_f, _s) \
716 VMSTATE_INT64_V(_f, _s, 0)
717
718 #define VMSTATE_UINT8(_f, _s) \
719 VMSTATE_UINT8_V(_f, _s, 0)
720 #define VMSTATE_UINT16(_f, _s) \
721 VMSTATE_UINT16_V(_f, _s, 0)
722 #define VMSTATE_UINT32(_f, _s) \
723 VMSTATE_UINT32_V(_f, _s, 0)
724 #define VMSTATE_UINT64(_f, _s) \
725 VMSTATE_UINT64_V(_f, _s, 0)
726
727 #define VMSTATE_UINT8_EQUAL(_f, _s) \
728 VMSTATE_SINGLE(_f, _s, 0, vmstate_info_uint8_equal, uint8_t)
729
730 #define VMSTATE_UINT16_EQUAL(_f, _s) \
731 VMSTATE_SINGLE(_f, _s, 0, vmstate_info_uint16_equal, uint16_t)
732
733 #define VMSTATE_UINT16_EQUAL_V(_f, _s, _v) \
734 VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint16_equal, uint16_t)
735
736 #define VMSTATE_INT32_EQUAL(_f, _s) \
737 VMSTATE_SINGLE(_f, _s, 0, vmstate_info_int32_equal, int32_t)
738
739 #define VMSTATE_UINT32_EQUAL_V(_f, _s, _v) \
740 VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint32_equal, uint32_t)
741
742 #define VMSTATE_UINT32_EQUAL(_f, _s) \
743 VMSTATE_UINT32_EQUAL_V(_f, _s, 0)
744
745 #define VMSTATE_UINT64_EQUAL_V(_f, _s, _v) \
746 VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint64_equal, uint64_t)
747
748 #define VMSTATE_UINT64_EQUAL(_f, _s) \
749 VMSTATE_UINT64_EQUAL_V(_f, _s, 0)
750
751 #define VMSTATE_INT32_POSITIVE_LE(_f, _s) \
752 VMSTATE_SINGLE(_f, _s, 0, vmstate_info_int32_le, int32_t)
753
754 #define VMSTATE_INT8_TEST(_f, _s, _t) \
755 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_int8, int8_t)
756
757 #define VMSTATE_INT16_TEST(_f, _s, _t) \
758 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_int16, int16_t)
759
760 #define VMSTATE_INT32_TEST(_f, _s, _t) \
761 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_int32, int32_t)
762
763 #define VMSTATE_INT64_TEST(_f, _s, _t) \
764 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_int64, int64_t)
765
766 #define VMSTATE_UINT8_TEST(_f, _s, _t) \
767 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint8, uint8_t)
768
769 #define VMSTATE_UINT16_TEST(_f, _s, _t) \
770 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint16, uint16_t)
771
772 #define VMSTATE_UINT32_TEST(_f, _s, _t) \
773 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint32, uint32_t)
774
775 #define VMSTATE_UINT64_TEST(_f, _s, _t) \
776 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint64, uint64_t)
777
778
779 #define VMSTATE_FLOAT64_V(_f, _s, _v) \
780 VMSTATE_SINGLE(_f, _s, _v, vmstate_info_float64, float64)
781
782 #define VMSTATE_FLOAT64(_f, _s) \
783 VMSTATE_FLOAT64_V(_f, _s, 0)
784
785 #define VMSTATE_TIMER_PTR_TEST(_f, _s, _test) \
786 VMSTATE_POINTER_TEST(_f, _s, _test, vmstate_info_timer, QEMUTimer *)
787
788 #define VMSTATE_TIMER_PTR_V(_f, _s, _v) \
789 VMSTATE_POINTER(_f, _s, _v, vmstate_info_timer, QEMUTimer *)
790
791 #define VMSTATE_TIMER_PTR(_f, _s) \
792 VMSTATE_TIMER_PTR_V(_f, _s, 0)
793
794 #define VMSTATE_TIMER_PTR_ARRAY(_f, _s, _n) \
795 VMSTATE_ARRAY_OF_POINTER(_f, _s, _n, 0, vmstate_info_timer, QEMUTimer *)
796
797 #define VMSTATE_TIMER_TEST(_f, _s, _test) \
798 VMSTATE_SINGLE_TEST(_f, _s, _test, 0, vmstate_info_timer, QEMUTimer)
799
800 #define VMSTATE_TIMER_V(_f, _s, _v) \
801 VMSTATE_SINGLE(_f, _s, _v, vmstate_info_timer, QEMUTimer)
802
803 #define VMSTATE_TIMER(_f, _s) \
804 VMSTATE_TIMER_V(_f, _s, 0)
805
806 #define VMSTATE_TIMER_ARRAY(_f, _s, _n) \
807 VMSTATE_ARRAY(_f, _s, _n, 0, vmstate_info_timer, QEMUTimer)
808
809 #define VMSTATE_BOOL_ARRAY_V(_f, _s, _n, _v) \
810 VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_bool, bool)
811
812 #define VMSTATE_BOOL_ARRAY(_f, _s, _n) \
813 VMSTATE_BOOL_ARRAY_V(_f, _s, _n, 0)
814
815 #define VMSTATE_UINT16_ARRAY_V(_f, _s, _n, _v) \
816 VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint16, uint16_t)
817
818 #define VMSTATE_UINT16_2DARRAY_V(_f, _s, _n1, _n2, _v) \
819 VMSTATE_2DARRAY(_f, _s, _n1, _n2, _v, vmstate_info_uint16, uint16_t)
820
821 #define VMSTATE_UINT16_ARRAY(_f, _s, _n) \
822 VMSTATE_UINT16_ARRAY_V(_f, _s, _n, 0)
823
824 #define VMSTATE_UINT16_2DARRAY(_f, _s, _n1, _n2) \
825 VMSTATE_UINT16_2DARRAY_V(_f, _s, _n1, _n2, 0)
826
827 #define VMSTATE_UINT8_2DARRAY_V(_f, _s, _n1, _n2, _v) \
828 VMSTATE_2DARRAY(_f, _s, _n1, _n2, _v, vmstate_info_uint8, uint8_t)
829
830 #define VMSTATE_UINT8_ARRAY_V(_f, _s, _n, _v) \
831 VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint8, uint8_t)
832
833 #define VMSTATE_UINT8_ARRAY(_f, _s, _n) \
834 VMSTATE_UINT8_ARRAY_V(_f, _s, _n, 0)
835
836 #define VMSTATE_UINT8_SUB_ARRAY(_f, _s, _start, _num) \
837 VMSTATE_SUB_ARRAY(_f, _s, _start, _num, 0, vmstate_info_uint8, uint8_t)
838
839 #define VMSTATE_UINT8_2DARRAY(_f, _s, _n1, _n2) \
840 VMSTATE_UINT8_2DARRAY_V(_f, _s, _n1, _n2, 0)
841
842 #define VMSTATE_UINT32_ARRAY_V(_f, _s, _n, _v) \
843 VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint32, uint32_t)
844
845 #define VMSTATE_UINT32_2DARRAY_V(_f, _s, _n1, _n2, _v) \
846 VMSTATE_2DARRAY(_f, _s, _n1, _n2, _v, vmstate_info_uint32, uint32_t)
847
848 #define VMSTATE_UINT32_ARRAY(_f, _s, _n) \
849 VMSTATE_UINT32_ARRAY_V(_f, _s, _n, 0)
850
851 #define VMSTATE_UINT32_2DARRAY(_f, _s, _n1, _n2) \
852 VMSTATE_UINT32_2DARRAY_V(_f, _s, _n1, _n2, 0)
853
854 #define VMSTATE_UINT64_ARRAY_V(_f, _s, _n, _v) \
855 VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint64, uint64_t)
856
857 #define VMSTATE_UINT64_ARRAY(_f, _s, _n) \
858 VMSTATE_UINT64_ARRAY_V(_f, _s, _n, 0)
859
860 #define VMSTATE_UINT64_2DARRAY(_f, _s, _n1, _n2) \
861 VMSTATE_UINT64_2DARRAY_V(_f, _s, _n1, _n2, 0)
862
863 #define VMSTATE_UINT64_2DARRAY_V(_f, _s, _n1, _n2, _v) \
864 VMSTATE_2DARRAY(_f, _s, _n1, _n2, _v, vmstate_info_uint64, uint64_t)
865
866 #define VMSTATE_INT16_ARRAY_V(_f, _s, _n, _v) \
867 VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_int16, int16_t)
868
869 #define VMSTATE_INT16_ARRAY(_f, _s, _n) \
870 VMSTATE_INT16_ARRAY_V(_f, _s, _n, 0)
871
872 #define VMSTATE_INT32_ARRAY_V(_f, _s, _n, _v) \
873 VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_int32, int32_t)
874
875 #define VMSTATE_INT32_ARRAY(_f, _s, _n) \
876 VMSTATE_INT32_ARRAY_V(_f, _s, _n, 0)
877
878 #define VMSTATE_UINT32_SUB_ARRAY(_f, _s, _start, _num) \
879 VMSTATE_SUB_ARRAY(_f, _s, _start, _num, 0, vmstate_info_uint32, uint32_t)
880
881 #define VMSTATE_INT64_ARRAY_V(_f, _s, _n, _v) \
882 VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_int64, int64_t)
883
884 #define VMSTATE_INT64_ARRAY(_f, _s, _n) \
885 VMSTATE_INT64_ARRAY_V(_f, _s, _n, 0)
886
887 #define VMSTATE_FLOAT64_ARRAY_V(_f, _s, _n, _v) \
888 VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_float64, float64)
889
890 #define VMSTATE_FLOAT64_ARRAY(_f, _s, _n) \
891 VMSTATE_FLOAT64_ARRAY_V(_f, _s, _n, 0)
892
893 #define VMSTATE_CPUDOUBLE_ARRAY_V(_f, _s, _n, _v) \
894 VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_cpudouble, CPU_DoubleU)
895
896 #define VMSTATE_CPUDOUBLE_ARRAY(_f, _s, _n) \
897 VMSTATE_CPUDOUBLE_ARRAY_V(_f, _s, _n, 0)
898
899 #define VMSTATE_BUFFER_V(_f, _s, _v) \
900 VMSTATE_STATIC_BUFFER(_f, _s, _v, NULL, 0, sizeof(typeof_field(_s, _f)))
901
902 #define VMSTATE_BUFFER(_f, _s) \
903 VMSTATE_BUFFER_V(_f, _s, 0)
904
905 #define VMSTATE_PARTIAL_BUFFER(_f, _s, _size) \
906 VMSTATE_STATIC_BUFFER(_f, _s, 0, NULL, 0, _size)
907
908 #define VMSTATE_BUFFER_START_MIDDLE_V(_f, _s, _start, _v) \
909 VMSTATE_STATIC_BUFFER(_f, _s, _v, NULL, _start, sizeof(typeof_field(_s, _f)))
910
911 #define VMSTATE_BUFFER_START_MIDDLE(_f, _s, _start) \
912 VMSTATE_BUFFER_START_MIDDLE_V(_f, _s, _start, 0)
913
914 #define VMSTATE_PARTIAL_VBUFFER(_f, _s, _size) \
915 VMSTATE_VBUFFER(_f, _s, 0, NULL, 0, _size)
916
917 #define VMSTATE_PARTIAL_VBUFFER_UINT32(_f, _s, _size) \
918 VMSTATE_VBUFFER_UINT32(_f, _s, 0, NULL, 0, _size)
919
920 #define VMSTATE_SUB_VBUFFER(_f, _s, _start, _size) \
921 VMSTATE_VBUFFER(_f, _s, 0, NULL, _start, _size)
922
923 #define VMSTATE_BUFFER_TEST(_f, _s, _test) \
924 VMSTATE_STATIC_BUFFER(_f, _s, 0, _test, 0, sizeof(typeof_field(_s, _f)))
925
926 #define VMSTATE_BUFFER_UNSAFE(_field, _state, _version, _size) \
927 VMSTATE_BUFFER_UNSAFE_INFO(_field, _state, _version, vmstate_info_buffer, _size)
928
929 #define VMSTATE_UNUSED_V(_v, _size) \
930 VMSTATE_UNUSED_BUFFER(NULL, _v, _size)
931
932 #define VMSTATE_UNUSED(_size) \
933 VMSTATE_UNUSED_V(0, _size)
934
935 #define VMSTATE_UNUSED_TEST(_test, _size) \
936 VMSTATE_UNUSED_BUFFER(_test, 0, _size)
937
938 #define VMSTATE_END_OF_LIST() \
939 {}
940
941 #define SELF_ANNOUNCE_ROUNDS 5
942
943 void loadvm_free_handlers(MigrationIncomingState *mis);
944
945 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
946 void *opaque, int version_id);
947 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
948 void *opaque, QJSON *vmdesc);
949
950 bool vmstate_save_needed(const VMStateDescription *vmsd, void *opaque);
951
952 int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
953 const VMStateDescription *vmsd,
954 void *base, int alias_id,
955 int required_for_version);
956
957 static inline int vmstate_register(DeviceState *dev, int instance_id,
958 const VMStateDescription *vmsd,
959 void *opaque)
960 {
961 return vmstate_register_with_alias_id(dev, instance_id, vmsd,
962 opaque, -1, 0);
963 }
964
965 void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
966 void *opaque);
967
968 struct MemoryRegion;
969 void vmstate_register_ram(struct MemoryRegion *memory, DeviceState *dev);
970 void vmstate_unregister_ram(struct MemoryRegion *memory, DeviceState *dev);
971 void vmstate_register_ram_global(struct MemoryRegion *memory);
972
973 static inline
974 int64_t self_announce_delay(int round)
975 {
976 assert(round < SELF_ANNOUNCE_ROUNDS && round > 0);
977 /* delay 50ms, 150ms, 250ms, ... */
978 return 50 + (SELF_ANNOUNCE_ROUNDS - round - 1) * 100;
979 }
980
981 void dump_vmstate_json_to_file(FILE *out_fp);
982
983 #endif