Skip to content

Commit 9ed7d6a

Browse files
Juan QuintelaAnthony Liguori
authored andcommitted
New VMstate save/load infrastructure
This patch introduces VMState infrastructure, to convert the save/load functions of devices to a table approach. This new approach has the following advantages: - it is type-safe - you can't have load/save functions out of sync - will allows us to have new interesting commands, like dump <device>, that shows all its internal state. - Just now, the only added type is arrays, but we can add structures. - Uses old load_state() function for loading old state. Signed-off-by: Juan Quintela <quintela@redhat.com> Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
1 parent 4082be4 commit 9ed7d6a

File tree

2 files changed

+370
-5
lines changed

2 files changed

+370
-5
lines changed

hw/hw.h

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,4 +270,106 @@ typedef int QEMUBootSetHandler(void *opaque, const char *boot_devices);
270270
void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque);
271271
int qemu_boot_set(const char *boot_devices);
272272

273+
typedef struct VMStateInfo VMStateInfo;
274+
typedef struct VMStateDescription VMStateDescription;
275+
276+
struct VMStateInfo {
277+
const char *name;
278+
int (*get)(QEMUFile *f, void *pv, size_t size);
279+
void (*put)(QEMUFile *f, const void *pv, size_t size);
280+
};
281+
282+
enum VMStateFlags {
283+
VMS_SINGLE = 0x001,
284+
};
285+
286+
typedef struct {
287+
const char *name;
288+
size_t offset;
289+
size_t size;
290+
const VMStateInfo *info;
291+
enum VMStateFlags flags;
292+
int version_id;
293+
} VMStateField;
294+
295+
struct VMStateDescription {
296+
const char *name;
297+
int version_id;
298+
int minimum_version_id;
299+
int minimum_version_id_old;
300+
LoadStateHandler *load_state_old;
301+
VMStateField *fields;
302+
};
303+
304+
extern const VMStateInfo vmstate_info_int8;
305+
extern const VMStateInfo vmstate_info_int16;
306+
extern const VMStateInfo vmstate_info_int32;
307+
extern const VMStateInfo vmstate_info_int64;
308+
309+
extern const VMStateInfo vmstate_info_uint8;
310+
extern const VMStateInfo vmstate_info_uint16;
311+
extern const VMStateInfo vmstate_info_uint32;
312+
extern const VMStateInfo vmstate_info_uint64;
313+
314+
#define VMSTATE_SINGLE(_field, _state, _version, _info, _type) { \
315+
.name = (stringify(_field)), \
316+
.version_id = (_version), \
317+
.size = sizeof(_type), \
318+
.info = &(_info), \
319+
.flags = VMS_SINGLE, \
320+
.offset = offsetof(_state, _field) \
321+
+ type_check(_type,typeof_field(_state, _field)) \
322+
}
323+
324+
/* _f : field name
325+
_s : struct state name
326+
_v : version
327+
*/
328+
329+
#define VMSTATE_INT8_V(_f, _s, _v) \
330+
VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int8, int8_t)
331+
#define VMSTATE_INT16_V(_f, _s, _v) \
332+
VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int16, int16_t)
333+
#define VMSTATE_INT32_V(_f, _s, _v) \
334+
VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int32, int32_t)
335+
#define VMSTATE_INT64_V(_f, _s, _v) \
336+
VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int64, int64_t)
337+
338+
#define VMSTATE_UINT8_V(_f, _s, _v) \
339+
VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint8, uint8_t)
340+
#define VMSTATE_UINT16_V(_f, _s, _v) \
341+
VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint16, uint16_t)
342+
#define VMSTATE_UINT32_V(_f, _s, _v) \
343+
VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint32, uint32_t)
344+
#define VMSTATE_UINT64_V(_f, _s, _v) \
345+
VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint64, uint64_t)
346+
347+
#define VMSTATE_INT8(_f, _s) \
348+
VMSTATE_INT8_V(_f, _s, 0)
349+
#define VMSTATE_INT16(_f, _s) \
350+
VMSTATE_INT16_V(_f, _s, 0)
351+
#define VMSTATE_INT32(_f, _s) \
352+
VMSTATE_INT32_V(_f, _s, 0)
353+
#define VMSTATE_INT64(_f, _s) \
354+
VMSTATE_INT64_V(_f, _s, 0)
355+
356+
#define VMSTATE_UINT8(_f, _s) \
357+
VMSTATE_UINT8_V(_f, _s, 0)
358+
#define VMSTATE_UINT16(_f, _s) \
359+
VMSTATE_UINT16_V(_f, _s, 0)
360+
#define VMSTATE_UINT32(_f, _s) \
361+
VMSTATE_UINT32_V(_f, _s, 0)
362+
#define VMSTATE_UINT64(_f, _s) \
363+
VMSTATE_UINT64_V(_f, _s, 0)
364+
365+
#define VMSTATE_END_OF_LIST() \
366+
{}
367+
368+
extern int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
369+
void *opaque, int version_id);
370+
extern void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
371+
const void *opaque);
372+
extern int vmstate_register(int instance_id, const VMStateDescription *vmsd,
373+
void *base);
374+
extern void vmstate_unregister(const char *idstr, void *opaque);
273375
#endif

0 commit comments

Comments
 (0)