]> git.proxmox.com Git - mirror_qemu.git/blame - slirp/src/vmstate.h
slirp: relicense GPL files to BSD-3
[mirror_qemu.git] / slirp / src / vmstate.h
CommitLineData
b92a1ff4
MAL
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 *
87ecdc71
MAL
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
b92a1ff4 11 *
87ecdc71
MAL
12 * 1. Redistributions of source code must retain the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer.
b92a1ff4 15 *
87ecdc71
MAL
16 * 2. Redistributions in binary form must reproduce the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer in the documentation and/or other materials provided
19 * with the distribution.
20 *
21 * 3. Neither the name of the copyright holder nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
30 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
36 * OF THE POSSIBILITY OF SUCH DAMAGE.
b92a1ff4
MAL
37 */
38#ifndef VMSTATE_H_
39#define VMSTATE_H_
40
41#include <unistd.h>
42#include <stdint.h>
43#include <stdbool.h>
44#include "slirp.h"
45#include "stream.h"
46
47#define stringify(s) tostring(s)
48#define tostring(s) #s
49
50typedef struct VMStateInfo VMStateInfo;
51typedef struct VMStateDescription VMStateDescription;
52typedef struct VMStateField VMStateField;
53
54int slirp_vmstate_save_state(SlirpOStream *f, const VMStateDescription *vmsd,
55 void *opaque);
56int slirp_vmstate_load_state(SlirpIStream *f, const VMStateDescription *vmsd,
57 void *opaque, int version_id);
58
59/* VMStateInfo allows customized migration of objects that don't fit in
60 * any category in VMStateFlags. Additional information is always passed
61 * into get and put in terms of field and vmdesc parameters. However
62 * these two parameters should only be used in cases when customized
63 * handling is needed, such as QTAILQ. For primitive data types such as
64 * integer, field and vmdesc parameters should be ignored inside get/put.
65 */
66struct VMStateInfo {
67 const char *name;
68 int (*get)(SlirpIStream *f, void *pv, size_t size, const VMStateField *field);
69 int (*put)(SlirpOStream *f, void *pv, size_t size, const VMStateField *field);
70};
71
72enum VMStateFlags {
73 /* Ignored */
74 VMS_SINGLE = 0x001,
75
76 /* The struct member at opaque + VMStateField.offset is a pointer
77 * to the actual field (e.g. struct a { uint8_t *b;
78 * }). Dereference the pointer before using it as basis for
79 * further pointer arithmetic (see e.g. VMS_ARRAY). Does not
80 * affect the meaning of VMStateField.num_offset or
81 * VMStateField.size_offset; see VMS_VARRAY* and VMS_VBUFFER for
82 * those. */
83 VMS_POINTER = 0x002,
84
85 /* The field is an array of fixed size. VMStateField.num contains
86 * the number of entries in the array. The size of each entry is
87 * given by VMStateField.size and / or opaque +
88 * VMStateField.size_offset; see VMS_VBUFFER and
89 * VMS_MULTIPLY. Each array entry will be processed individually
90 * (VMStateField.info.get()/put() if VMS_STRUCT is not set,
91 * recursion into VMStateField.vmsd if VMS_STRUCT is set). May not
92 * be combined with VMS_VARRAY*. */
93 VMS_ARRAY = 0x004,
94
95 /* The field is itself a struct, containing one or more
96 * fields. Recurse into VMStateField.vmsd. Most useful in
97 * combination with VMS_ARRAY / VMS_VARRAY*, recursing into each
98 * array entry. */
99 VMS_STRUCT = 0x008,
100
101 /* The field is an array of variable size. The int32_t at opaque +
102 * VMStateField.num_offset contains the number of entries in the
103 * array. See the VMS_ARRAY description regarding array handling
104 * in general. May not be combined with VMS_ARRAY or any other
105 * VMS_VARRAY*. */
106 VMS_VARRAY_INT32 = 0x010,
107
108 /* Ignored */
109 VMS_BUFFER = 0x020,
110
111 /* The field is a (fixed-size or variable-size) array of pointers
112 * (e.g. struct a { uint8_t *b[]; }). Dereference each array entry
113 * before using it. Note: Does not imply any one of VMS_ARRAY /
114 * VMS_VARRAY*; these need to be set explicitly. */
115 VMS_ARRAY_OF_POINTER = 0x040,
116
117 /* The field is an array of variable size. The uint16_t at opaque
118 * + VMStateField.num_offset (subject to VMS_MULTIPLY_ELEMENTS)
119 * contains the number of entries in the array. See the VMS_ARRAY
120 * description regarding array handling in general. May not be
121 * combined with VMS_ARRAY or any other VMS_VARRAY*. */
122 VMS_VARRAY_UINT16 = 0x080,
123
124 /* The size of the individual entries (a single array entry if
125 * VMS_ARRAY or any of VMS_VARRAY* are set, or the field itself if
126 * neither is set) is variable (i.e. not known at compile-time),
127 * but the same for all entries. Use the int32_t at opaque +
128 * VMStateField.size_offset (subject to VMS_MULTIPLY) to determine
129 * the size of each (and every) entry. */
130 VMS_VBUFFER = 0x100,
131
132 /* Multiply the entry size given by the int32_t at opaque +
133 * VMStateField.size_offset (see VMS_VBUFFER description) with
134 * VMStateField.size to determine the number of bytes to be
135 * allocated. Only valid in combination with VMS_VBUFFER. */
136 VMS_MULTIPLY = 0x200,
137
138 /* The field is an array of variable size. The uint8_t at opaque +
139 * VMStateField.num_offset (subject to VMS_MULTIPLY_ELEMENTS)
140 * contains the number of entries in the array. See the VMS_ARRAY
141 * description regarding array handling in general. May not be
142 * combined with VMS_ARRAY or any other VMS_VARRAY*. */
143 VMS_VARRAY_UINT8 = 0x400,
144
145 /* The field is an array of variable size. The uint32_t at opaque
146 * + VMStateField.num_offset (subject to VMS_MULTIPLY_ELEMENTS)
147 * contains the number of entries in the array. See the VMS_ARRAY
148 * description regarding array handling in general. May not be
149 * combined with VMS_ARRAY or any other VMS_VARRAY*. */
150 VMS_VARRAY_UINT32 = 0x800,
151
152 /* Fail loading the serialised VM state if this field is missing
153 * from the input. */
154 VMS_MUST_EXIST = 0x1000,
155
156 /* When loading serialised VM state, allocate memory for the
157 * (entire) field. Only valid in combination with
158 * VMS_POINTER. Note: Not all combinations with other flags are
159 * currently supported, e.g. VMS_ALLOC|VMS_ARRAY_OF_POINTER won't
160 * cause the individual entries to be allocated. */
161 VMS_ALLOC = 0x2000,
162
163 /* Multiply the number of entries given by the integer at opaque +
164 * VMStateField.num_offset (see VMS_VARRAY*) with VMStateField.num
165 * to determine the number of entries in the array. Only valid in
166 * combination with one of VMS_VARRAY*. */
167 VMS_MULTIPLY_ELEMENTS = 0x4000,
168
169 /* A structure field that is like VMS_STRUCT, but uses
170 * VMStateField.struct_version_id to tell which version of the
171 * structure we are referencing to use. */
172 VMS_VSTRUCT = 0x8000,
173};
174
175struct VMStateField {
176 const char *name;
177 size_t offset;
178 size_t size;
179 size_t start;
180 int num;
181 size_t num_offset;
182 size_t size_offset;
183 const VMStateInfo *info;
184 enum VMStateFlags flags;
185 const VMStateDescription *vmsd;
186 int version_id;
187 int struct_version_id;
188 bool (*field_exists)(void *opaque, int version_id);
189};
190
191struct VMStateDescription {
192 const char *name;
193 int version_id;
194 int (*pre_load)(void *opaque);
195 int (*post_load)(void *opaque, int version_id);
196 int (*pre_save)(void *opaque);
197 VMStateField *fields;
198};
199
200
201extern const VMStateInfo slirp_vmstate_info_int16;
202extern const VMStateInfo slirp_vmstate_info_int32;
203extern const VMStateInfo slirp_vmstate_info_uint8;
204extern const VMStateInfo slirp_vmstate_info_uint16;
205extern const VMStateInfo slirp_vmstate_info_uint32;
206
207/** Put this in the stream when migrating a null pointer.*/
208#define VMS_NULLPTR_MARKER (0x30U) /* '0' */
209extern const VMStateInfo slirp_vmstate_info_nullptr;
210
211extern const VMStateInfo slirp_vmstate_info_buffer;
212extern const VMStateInfo slirp_vmstate_info_tmp;
213
214#define type_check_array(t1,t2,n) ((t1(*)[n])0 - (t2*)0)
215#define type_check_pointer(t1,t2) ((t1**)0 - (t2*)0)
216#define typeof_field(type, field) typeof(((type *)0)->field)
217#define type_check(t1,t2) ((t1*)0 - (t2*)0)
218
219#define vmstate_offset_value(_state, _field, _type) \
220 (offsetof(_state, _field) + \
221 type_check(_type, typeof_field(_state, _field)))
222
223#define vmstate_offset_pointer(_state, _field, _type) \
224 (offsetof(_state, _field) + \
225 type_check_pointer(_type, typeof_field(_state, _field)))
226
227#define vmstate_offset_array(_state, _field, _type, _num) \
228 (offsetof(_state, _field) + \
229 type_check_array(_type, typeof_field(_state, _field), _num))
230
231#define vmstate_offset_buffer(_state, _field) \
232 vmstate_offset_array(_state, _field, uint8_t, \
233 sizeof(typeof_field(_state, _field)))
234
235/* In the macros below, if there is a _version, that means the macro's
236 * field will be processed only if the version being received is >=
237 * the _version specified. In general, if you add a new field, you
238 * would increment the structure's version and put that version
239 * number into the new field so it would only be processed with the
240 * new version.
241 *
242 * In particular, for VMSTATE_STRUCT() and friends the _version does
243 * *NOT* pick the version of the sub-structure. It works just as
244 * specified above. The version of the top-level structure received
245 * is passed down to all sub-structures. This means that the
246 * sub-structures must have version that are compatible with all the
247 * structures that use them.
248 *
249 * If you want to specify the version of the sub-structure, use
250 * VMSTATE_VSTRUCT(), which allows the specific sub-structure version
251 * to be directly specified.
252 */
253
254#define VMSTATE_SINGLE_TEST(_field, _state, _test, _version, _info, _type) { \
255 .name = (stringify(_field)), \
256 .version_id = (_version), \
257 .field_exists = (_test), \
258 .size = sizeof(_type), \
259 .info = &(_info), \
260 .flags = VMS_SINGLE, \
261 .offset = vmstate_offset_value(_state, _field, _type), \
262}
263
264#define VMSTATE_ARRAY(_field, _state, _num, _version, _info, _type) {\
265 .name = (stringify(_field)), \
266 .version_id = (_version), \
267 .num = (_num), \
268 .info = &(_info), \
269 .size = sizeof(_type), \
270 .flags = VMS_ARRAY, \
271 .offset = vmstate_offset_array(_state, _field, _type, _num), \
272}
273
274#define VMSTATE_STRUCT_TEST(_field, _state, _test, _version, _vmsd, _type) { \
275 .name = (stringify(_field)), \
276 .version_id = (_version), \
277 .field_exists = (_test), \
278 .vmsd = &(_vmsd), \
279 .size = sizeof(_type), \
280 .flags = VMS_STRUCT, \
281 .offset = vmstate_offset_value(_state, _field, _type), \
282}
283
284#define VMSTATE_STRUCT_POINTER_V(_field, _state, _version, _vmsd, _type) { \
285 .name = (stringify(_field)), \
286 .version_id = (_version), \
287 .vmsd = &(_vmsd), \
288 .size = sizeof(_type *), \
289 .flags = VMS_STRUCT|VMS_POINTER, \
290 .offset = vmstate_offset_pointer(_state, _field, _type), \
291}
292
293#define VMSTATE_STRUCT_ARRAY_TEST(_field, _state, _num, _test, _version, _vmsd, _type) { \
294 .name = (stringify(_field)), \
295 .num = (_num), \
296 .field_exists = (_test), \
297 .version_id = (_version), \
298 .vmsd = &(_vmsd), \
299 .size = sizeof(_type), \
300 .flags = VMS_STRUCT|VMS_ARRAY, \
301 .offset = vmstate_offset_array(_state, _field, _type, _num),\
302}
303
304#define VMSTATE_STATIC_BUFFER(_field, _state, _version, _test, _start, _size) { \
305 .name = (stringify(_field)), \
306 .version_id = (_version), \
307 .field_exists = (_test), \
308 .size = (_size - _start), \
309 .info = &slirp_vmstate_info_buffer, \
310 .flags = VMS_BUFFER, \
311 .offset = vmstate_offset_buffer(_state, _field) + _start, \
312}
313
314#define VMSTATE_VBUFFER_UINT32(_field, _state, _version, _test, _field_size) { \
315 .name = (stringify(_field)), \
316 .version_id = (_version), \
317 .field_exists = (_test), \
318 .size_offset = vmstate_offset_value(_state, _field_size, uint32_t),\
319 .info = &slirp_vmstate_info_buffer, \
320 .flags = VMS_VBUFFER|VMS_POINTER, \
321 .offset = offsetof(_state, _field), \
322}
323
324#define QEMU_BUILD_BUG_ON_STRUCT(x) \
325 struct { \
326 int:(x) ? -1 : 1; \
327 }
328
329#define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)) - \
330 sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)))
331
332/* Allocate a temporary of type 'tmp_type', set tmp->parent to _state
333 * and execute the vmsd on the temporary. Note that we're working with
334 * the whole of _state here, not a field within it.
335 * We compile time check that:
336 * That _tmp_type contains a 'parent' member that's a pointer to the
337 * '_state' type
338 * That the pointer is right at the start of _tmp_type.
339 */
340#define VMSTATE_WITH_TMP(_state, _tmp_type, _vmsd) { \
341 .name = "tmp", \
342 .size = sizeof(_tmp_type) + \
343 QEMU_BUILD_BUG_ON_ZERO(offsetof(_tmp_type, parent) != 0) + \
344 type_check_pointer(_state, \
345 typeof_field(_tmp_type, parent)), \
346 .vmsd = &(_vmsd), \
347 .info = &slirp_vmstate_info_tmp, \
348}
349
350#define VMSTATE_SINGLE(_field, _state, _version, _info, _type) \
351 VMSTATE_SINGLE_TEST(_field, _state, NULL, _version, _info, _type)
352
353#define VMSTATE_STRUCT(_field, _state, _version, _vmsd, _type) \
354 VMSTATE_STRUCT_TEST(_field, _state, NULL, _version, _vmsd, _type)
355
356#define VMSTATE_STRUCT_POINTER(_field, _state, _vmsd, _type) \
357 VMSTATE_STRUCT_POINTER_V(_field, _state, 0, _vmsd, _type)
358
359#define VMSTATE_STRUCT_ARRAY(_field, _state, _num, _version, _vmsd, _type) \
360 VMSTATE_STRUCT_ARRAY_TEST(_field, _state, _num, NULL, _version, \
361 _vmsd, _type)
362
363#define VMSTATE_INT16_V(_f, _s, _v) \
364 VMSTATE_SINGLE(_f, _s, _v, slirp_vmstate_info_int16, int16_t)
365#define VMSTATE_INT32_V(_f, _s, _v) \
366 VMSTATE_SINGLE(_f, _s, _v, slirp_vmstate_info_int32, int32_t)
367
368#define VMSTATE_UINT8_V(_f, _s, _v) \
369 VMSTATE_SINGLE(_f, _s, _v, slirp_vmstate_info_uint8, uint8_t)
370#define VMSTATE_UINT16_V(_f, _s, _v) \
371 VMSTATE_SINGLE(_f, _s, _v, slirp_vmstate_info_uint16, uint16_t)
372#define VMSTATE_UINT32_V(_f, _s, _v) \
373 VMSTATE_SINGLE(_f, _s, _v, slirp_vmstate_info_uint32, uint32_t)
374
375#define VMSTATE_INT16(_f, _s) \
376 VMSTATE_INT16_V(_f, _s, 0)
377#define VMSTATE_INT32(_f, _s) \
378 VMSTATE_INT32_V(_f, _s, 0)
379
380#define VMSTATE_UINT8(_f, _s) \
381 VMSTATE_UINT8_V(_f, _s, 0)
382#define VMSTATE_UINT16(_f, _s) \
383 VMSTATE_UINT16_V(_f, _s, 0)
384#define VMSTATE_UINT32(_f, _s) \
385 VMSTATE_UINT32_V(_f, _s, 0)
386
387#define VMSTATE_UINT16_TEST(_f, _s, _t) \
388 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, slirp_vmstate_info_uint16, uint16_t)
389
390#define VMSTATE_UINT32_TEST(_f, _s, _t) \
391 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, slirp_vmstate_info_uint32, uint32_t)
392
393#define VMSTATE_INT16_ARRAY_V(_f, _s, _n, _v) \
394 VMSTATE_ARRAY(_f, _s, _n, _v, slirp_vmstate_info_int16, int16_t)
395
396#define VMSTATE_INT16_ARRAY(_f, _s, _n) \
397 VMSTATE_INT16_ARRAY_V(_f, _s, _n, 0)
398
399#define VMSTATE_BUFFER_V(_f, _s, _v) \
400 VMSTATE_STATIC_BUFFER(_f, _s, _v, NULL, 0, sizeof(typeof_field(_s, _f)))
401
402#define VMSTATE_BUFFER(_f, _s) \
403 VMSTATE_BUFFER_V(_f, _s, 0)
404
405#define VMSTATE_END_OF_LIST() \
406 {}
407
408#endif