1 /* Declarations for use by hardware emulation. */
5 #include "qemu-common.h"
7 #if defined(TARGET_PHYS_ADDR_BITS) && !defined(NEED_CPU_H)
10 #include "cpu-common.h"
18 /* This function writes a chunk of data to a file at the given position.
19 * The pos argument can be ignored if the file is only being used for
20 * streaming. The handler should try to write all of the data it can.
22 typedef int (QEMUFilePutBufferFunc
)(void *opaque
, const uint8_t *buf
,
23 int64_t pos
, int size
);
25 /* Read a chunk of data from a file at the given position. The pos argument
26 * can be ignored if the file is only be used for streaming. The number of
27 * bytes actually read should be returned.
29 typedef int (QEMUFileGetBufferFunc
)(void *opaque
, uint8_t *buf
,
30 int64_t pos
, int size
);
32 /* Close a file and return an error code */
33 typedef int (QEMUFileCloseFunc
)(void *opaque
);
35 /* Called to determine if the file has exceeded it's bandwidth allocation. The
36 * bandwidth capping is a soft limit, not a hard limit.
38 typedef int (QEMUFileRateLimit
)(void *opaque
);
40 /* Called to change the current bandwidth allocation. This function must return
41 * the new actual bandwidth. It should be new_rate if everything goes ok, and
42 * the old rate otherwise
44 typedef size_t (QEMUFileSetRateLimit
)(void *opaque
, size_t new_rate
);
46 QEMUFile
*qemu_fopen_ops(void *opaque
, QEMUFilePutBufferFunc
*put_buffer
,
47 QEMUFileGetBufferFunc
*get_buffer
,
48 QEMUFileCloseFunc
*close
,
49 QEMUFileRateLimit
*rate_limit
,
50 QEMUFileSetRateLimit
*set_rate_limit
);
51 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
);
52 QEMUFile
*qemu_fdopen(int fd
, const char *mode
);
53 QEMUFile
*qemu_fopen_socket(int fd
);
54 QEMUFile
*qemu_popen(FILE *popen_file
, const char *mode
);
55 QEMUFile
*qemu_popen_cmd(const char *command
, const char *mode
);
56 int qemu_stdio_fd(QEMUFile
*f
);
57 void qemu_fflush(QEMUFile
*f
);
58 int qemu_fclose(QEMUFile
*f
);
59 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
);
60 void qemu_put_byte(QEMUFile
*f
, int v
);
62 static inline void qemu_put_ubyte(QEMUFile
*f
, unsigned int v
)
64 qemu_put_byte(f
, (int)v
);
67 #define qemu_put_sbyte qemu_put_byte
69 void qemu_put_be16(QEMUFile
*f
, unsigned int v
);
70 void qemu_put_be32(QEMUFile
*f
, unsigned int v
);
71 void qemu_put_be64(QEMUFile
*f
, uint64_t v
);
72 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
);
73 int qemu_get_byte(QEMUFile
*f
);
75 static inline unsigned int qemu_get_ubyte(QEMUFile
*f
)
77 return (unsigned int)qemu_get_byte(f
);
80 #define qemu_get_sbyte qemu_get_byte
82 unsigned int qemu_get_be16(QEMUFile
*f
);
83 unsigned int qemu_get_be32(QEMUFile
*f
);
84 uint64_t qemu_get_be64(QEMUFile
*f
);
85 int qemu_file_rate_limit(QEMUFile
*f
);
86 size_t qemu_file_set_rate_limit(QEMUFile
*f
, size_t new_rate
);
87 int qemu_file_has_error(QEMUFile
*f
);
88 void qemu_file_set_error(QEMUFile
*f
);
90 /* Try to send any outstanding data. This function is useful when output is
91 * halted due to rate limiting or EAGAIN errors occur as it can be used to
93 void qemu_file_put_notify(QEMUFile
*f
);
95 static inline void qemu_put_be64s(QEMUFile
*f
, const uint64_t *pv
)
97 qemu_put_be64(f
, *pv
);
100 static inline void qemu_put_be32s(QEMUFile
*f
, const uint32_t *pv
)
102 qemu_put_be32(f
, *pv
);
105 static inline void qemu_put_be16s(QEMUFile
*f
, const uint16_t *pv
)
107 qemu_put_be16(f
, *pv
);
110 static inline void qemu_put_8s(QEMUFile
*f
, const uint8_t *pv
)
112 qemu_put_byte(f
, *pv
);
115 static inline void qemu_get_be64s(QEMUFile
*f
, uint64_t *pv
)
117 *pv
= qemu_get_be64(f
);
120 static inline void qemu_get_be32s(QEMUFile
*f
, uint32_t *pv
)
122 *pv
= qemu_get_be32(f
);
125 static inline void qemu_get_be16s(QEMUFile
*f
, uint16_t *pv
)
127 *pv
= qemu_get_be16(f
);
130 static inline void qemu_get_8s(QEMUFile
*f
, uint8_t *pv
)
132 *pv
= qemu_get_byte(f
);
135 // Signed versions for type safety
136 static inline void qemu_put_sbuffer(QEMUFile
*f
, const int8_t *buf
, int size
)
138 qemu_put_buffer(f
, (const uint8_t *)buf
, size
);
141 static inline void qemu_put_sbe16(QEMUFile
*f
, int v
)
143 qemu_put_be16(f
, (unsigned int)v
);
146 static inline void qemu_put_sbe32(QEMUFile
*f
, int v
)
148 qemu_put_be32(f
, (unsigned int)v
);
151 static inline void qemu_put_sbe64(QEMUFile
*f
, int64_t v
)
153 qemu_put_be64(f
, (uint64_t)v
);
156 static inline size_t qemu_get_sbuffer(QEMUFile
*f
, int8_t *buf
, int size
)
158 return qemu_get_buffer(f
, (uint8_t *)buf
, size
);
161 static inline int qemu_get_sbe16(QEMUFile
*f
)
163 return (int)qemu_get_be16(f
);
166 static inline int qemu_get_sbe32(QEMUFile
*f
)
168 return (int)qemu_get_be32(f
);
171 static inline int64_t qemu_get_sbe64(QEMUFile
*f
)
173 return (int64_t)qemu_get_be64(f
);
176 static inline void qemu_put_s8s(QEMUFile
*f
, const int8_t *pv
)
178 qemu_put_8s(f
, (const uint8_t *)pv
);
181 static inline void qemu_put_sbe16s(QEMUFile
*f
, const int16_t *pv
)
183 qemu_put_be16s(f
, (const uint16_t *)pv
);
186 static inline void qemu_put_sbe32s(QEMUFile
*f
, const int32_t *pv
)
188 qemu_put_be32s(f
, (const uint32_t *)pv
);
191 static inline void qemu_put_sbe64s(QEMUFile
*f
, const int64_t *pv
)
193 qemu_put_be64s(f
, (const uint64_t *)pv
);
196 static inline void qemu_get_s8s(QEMUFile
*f
, int8_t *pv
)
198 qemu_get_8s(f
, (uint8_t *)pv
);
201 static inline void qemu_get_sbe16s(QEMUFile
*f
, int16_t *pv
)
203 qemu_get_be16s(f
, (uint16_t *)pv
);
206 static inline void qemu_get_sbe32s(QEMUFile
*f
, int32_t *pv
)
208 qemu_get_be32s(f
, (uint32_t *)pv
);
211 static inline void qemu_get_sbe64s(QEMUFile
*f
, int64_t *pv
)
213 qemu_get_be64s(f
, (uint64_t *)pv
);
217 #if TARGET_LONG_BITS == 64
218 #define qemu_put_betl qemu_put_be64
219 #define qemu_get_betl qemu_get_be64
220 #define qemu_put_betls qemu_put_be64s
221 #define qemu_get_betls qemu_get_be64s
222 #define qemu_put_sbetl qemu_put_sbe64
223 #define qemu_get_sbetl qemu_get_sbe64
224 #define qemu_put_sbetls qemu_put_sbe64s
225 #define qemu_get_sbetls qemu_get_sbe64s
227 #define qemu_put_betl qemu_put_be32
228 #define qemu_get_betl qemu_get_be32
229 #define qemu_put_betls qemu_put_be32s
230 #define qemu_get_betls qemu_get_be32s
231 #define qemu_put_sbetl qemu_put_sbe32
232 #define qemu_get_sbetl qemu_get_sbe32
233 #define qemu_put_sbetls qemu_put_sbe32s
234 #define qemu_get_sbetls qemu_get_sbe32s
238 int64_t qemu_ftell(QEMUFile
*f
);
239 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
);
241 typedef void SaveStateHandler(QEMUFile
*f
, void *opaque
);
242 typedef int SaveLiveStateHandler(QEMUFile
*f
, int stage
, void *opaque
);
243 typedef int LoadStateHandler(QEMUFile
*f
, void *opaque
, int version_id
);
245 int register_savevm(const char *idstr
,
248 SaveStateHandler
*save_state
,
249 LoadStateHandler
*load_state
,
252 int register_savevm_live(const char *idstr
,
255 SaveLiveStateHandler
*save_live_state
,
256 SaveStateHandler
*save_state
,
257 LoadStateHandler
*load_state
,
260 void unregister_savevm(const char *idstr
, void *opaque
);
262 typedef void QEMUResetHandler(void *opaque
);
264 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
);
265 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
);
267 /* handler to set the boot_device order for a specific type of QEMUMachine */
268 /* return 0 if success */
269 typedef int QEMUBootSetHandler(void *opaque
, const char *boot_devices
);
270 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
);
271 int qemu_boot_set(const char *boot_devices
);
273 typedef struct VMStateInfo VMStateInfo
;
274 typedef struct VMStateDescription VMStateDescription
;
278 int (*get
)(QEMUFile
*f
, void *pv
, size_t size
);
279 void (*put
)(QEMUFile
*f
, const void *pv
, size_t size
);
287 VMS_VARRAY
= 0x010, /* Array with size in another field */
288 VMS_BUFFER
= 0x020, /* static sized buffer */
297 const VMStateInfo
*info
;
298 enum VMStateFlags flags
;
299 const VMStateDescription
*vmsd
;
303 struct VMStateDescription
{
306 int minimum_version_id
;
307 int minimum_version_id_old
;
308 LoadStateHandler
*load_state_old
;
309 int (*run_after_load
)(void *opaque
);
310 VMStateField
*fields
;
313 extern const VMStateInfo vmstate_info_int8
;
314 extern const VMStateInfo vmstate_info_int16
;
315 extern const VMStateInfo vmstate_info_int32
;
316 extern const VMStateInfo vmstate_info_int64
;
318 extern const VMStateInfo vmstate_info_int32_equal
;
319 extern const VMStateInfo vmstate_info_int32_le
;
321 extern const VMStateInfo vmstate_info_uint8
;
322 extern const VMStateInfo vmstate_info_uint16
;
323 extern const VMStateInfo vmstate_info_uint32
;
324 extern const VMStateInfo vmstate_info_uint64
;
326 extern const VMStateInfo vmstate_info_timer
;
327 extern const VMStateInfo vmstate_info_ptimer
;
328 extern const VMStateInfo vmstate_info_buffer
;
330 #define type_check_array(t1,t2,n) ((t1(*)[n])0 - (t2*)0)
331 #define type_check_pointer(t1,t2) ((t1**)0 - (t2*)0)
333 #define VMSTATE_SINGLE(_field, _state, _version, _info, _type) { \
334 .name = (stringify(_field)), \
335 .version_id = (_version), \
336 .size = sizeof(_type), \
338 .flags = VMS_SINGLE, \
339 .offset = offsetof(_state, _field) \
340 + type_check(_type,typeof_field(_state, _field)) \
343 #define VMSTATE_POINTER(_field, _state, _version, _info, _type) { \
344 .name = (stringify(_field)), \
345 .version_id = (_version), \
347 .size = sizeof(_type), \
348 .flags = VMS_SINGLE|VMS_POINTER, \
349 .offset = offsetof(_state, _field) \
350 + type_check(_type,typeof_field(_state, _field)) \
353 #define VMSTATE_ARRAY(_field, _state, _num, _version, _info, _type) {\
354 .name = (stringify(_field)), \
355 .version_id = (_version), \
358 .size = sizeof(_type), \
359 .flags = VMS_ARRAY, \
360 .offset = offsetof(_state, _field) \
361 + type_check_array(_type,typeof_field(_state, _field),_num) \
364 #define VMSTATE_VARRAY(_field, _state, _field_num, _version, _info, _type) {\
365 .name = (stringify(_field)), \
366 .version_id = (_version), \
367 .num_offset = offsetof(_state, _field_num) \
368 + type_check(int32_t,typeof_field(_state, _field_num)), \
370 .size = sizeof(_type), \
371 .flags = VMS_VARRAY|VMS_POINTER, \
372 .offset = offsetof(_state, _field) \
373 + type_check_pointer(_type,typeof_field(_state, _field)) \
376 #define VMSTATE_STRUCT(_field, _state, _version, _vmsd, _type) { \
377 .name = (stringify(_field)), \
378 .version_id = (_version), \
380 .size = sizeof(_type), \
381 .flags = VMS_STRUCT, \
382 .offset = offsetof(_state, _field) \
383 + type_check(_type,typeof_field(_state, _field)) \
386 #define VMSTATE_STRUCT_ARRAY(_field, _state, _num, _version, _vmsd, _type) { \
387 .name = (stringify(_field)), \
389 .version_id = (_version), \
391 .size = sizeof(_type), \
392 .flags = VMS_STRUCT|VMS_ARRAY, \
393 .offset = offsetof(_state, _field) \
394 + type_check_array(_type,typeof_field(_state, _field),_num) \
397 #define VMSTATE_STATIC_BUFFER(_field, _state, _version) { \
398 .name = (stringify(_field)), \
399 .version_id = (_version), \
400 .size = sizeof(typeof_field(_state,_field)), \
401 .info = &vmstate_info_buffer, \
402 .flags = VMS_BUFFER, \
403 .offset = offsetof(_state, _field) \
404 + type_check_array(uint8_t,typeof_field(_state, _field),sizeof(typeof_field(_state,_field))) \
407 extern const VMStateDescription vmstate_pci_device
;
409 #define VMSTATE_PCI_DEVICE(_field, _state) { \
410 .name = (stringify(_field)), \
411 .size = sizeof(PCIDevice), \
412 .vmsd = &vmstate_pci_device, \
413 .flags = VMS_STRUCT, \
414 .offset = offsetof(_state, _field) \
415 + type_check(PCIDevice,typeof_field(_state, _field)) \
419 _f_n : num of elements field_name
421 _s : struct state name
425 #define VMSTATE_INT8_V(_f, _s, _v) \
426 VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int8, int8_t)
427 #define VMSTATE_INT16_V(_f, _s, _v) \
428 VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int16, int16_t)
429 #define VMSTATE_INT32_V(_f, _s, _v) \
430 VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int32, int32_t)
431 #define VMSTATE_INT64_V(_f, _s, _v) \
432 VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int64, int64_t)
434 #define VMSTATE_UINT8_V(_f, _s, _v) \
435 VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint8, uint8_t)
436 #define VMSTATE_UINT16_V(_f, _s, _v) \
437 VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint16, uint16_t)
438 #define VMSTATE_UINT32_V(_f, _s, _v) \
439 VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint32, uint32_t)
440 #define VMSTATE_UINT64_V(_f, _s, _v) \
441 VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint64, uint64_t)
443 #define VMSTATE_INT8(_f, _s) \
444 VMSTATE_INT8_V(_f, _s, 0)
445 #define VMSTATE_INT16(_f, _s) \
446 VMSTATE_INT16_V(_f, _s, 0)
447 #define VMSTATE_INT32(_f, _s) \
448 VMSTATE_INT32_V(_f, _s, 0)
449 #define VMSTATE_INT64(_f, _s) \
450 VMSTATE_INT64_V(_f, _s, 0)
452 #define VMSTATE_UINT8(_f, _s) \
453 VMSTATE_UINT8_V(_f, _s, 0)
454 #define VMSTATE_UINT16(_f, _s) \
455 VMSTATE_UINT16_V(_f, _s, 0)
456 #define VMSTATE_UINT32(_f, _s) \
457 VMSTATE_UINT32_V(_f, _s, 0)
458 #define VMSTATE_UINT64(_f, _s) \
459 VMSTATE_UINT64_V(_f, _s, 0)
461 #define VMSTATE_INT32_EQUAL(_f, _s) \
462 VMSTATE_SINGLE(_f, _s, 0, vmstate_info_int32_equal, int32_t)
464 #define VMSTATE_INT32_LE(_f, _s) \
465 VMSTATE_SINGLE(_f, _s, 0, vmstate_info_int32_le, int32_t)
467 #define VMSTATE_TIMER_V(_f, _s, _v) \
468 VMSTATE_POINTER(_f, _s, _v, vmstate_info_timer, QEMUTimer *)
470 #define VMSTATE_TIMER(_f, _s) \
471 VMSTATE_TIMER_V(_f, _s, 0)
473 #define VMSTATE_PTIMER_V(_f, _s, _v) \
474 VMSTATE_POINTER(_f, _s, _v, vmstate_info_ptimer, ptimer_state *)
476 #define VMSTATE_PTIMER(_f, _s) \
477 VMSTATE_PTIMER_V(_f, _s, 0)
479 #define VMSTATE_UINT32_ARRAY_V(_f, _s, _n, _v) \
480 VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint32, uint32_t)
482 #define VMSTATE_UINT32_ARRAY(_f, _s, _n) \
483 VMSTATE_UINT32_ARRAY_V(_f, _s, _n, 0)
485 #define VMSTATE_INT32_ARRAY_V(_f, _s, _n, _v) \
486 VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_int32, int32_t)
488 #define VMSTATE_INT32_ARRAY(_f, _s, _n) \
489 VMSTATE_INT32_ARRAY_V(_f, _s, _n, 0)
491 #define VMSTATE_INT32_VARRAY_V(_f, _s, _f_n, _v) \
492 VMSTATE_VARRAY(_f, _s, _f_n, _v, vmstate_info_int32, int32_t)
494 #define VMSTATE_INT32_VARRAY(_f, _s, _f_n) \
495 VMSTATE_INT32_VARRAY_V(_f, _s, _f_n, 0)
497 #define VMSTATE_BUFFER_V(_f, _s, _v) \
498 VMSTATE_STATIC_BUFFER(_f, _s, _v)
500 #define VMSTATE_BUFFER(_f, _s) \
501 VMSTATE_STATIC_BUFFER(_f, _s, 0)
503 #define VMSTATE_END_OF_LIST() \
506 extern int vmstate_load_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
507 void *opaque
, int version_id
);
508 extern void vmstate_save_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
510 extern int vmstate_register(int instance_id
, const VMStateDescription
*vmsd
,
512 extern void vmstate_unregister(const char *idstr
, void *opaque
);