1 /* Declarations for use by hardware emulation. */
2 #ifndef QEMU_HW_H
3 #define QEMU_HW_H
4
5 #include "qemu-common.h"
6 #include "irq.h"
7
8 #if defined(TARGET_PHYS_ADDRESS_SPACE_BITS) && !defined(NEED_CPU_H)
9 #include "cpu.h"
10 #endif
11
12 /* VM Load/Save */
13
14 /* This function writes a chunk of data to a file at the given position.
15 * The pos argument can be ignored if the file is only being used for
16 * streaming. The handler should try to write all of the data it can.
17 */
18 typedef int (QEMUFilePutBufferFunc)(void *opaque, const uint8_t *buf,
19 int64_t pos, int size);
20
21 /* Read a chunk of data from a file at the given position. The pos argument
22 * can be ignored if the file is only be used for streaming. The number of
23 * bytes actually read should be returned.
24 */
25 typedef int (QEMUFileGetBufferFunc)(void *opaque, uint8_t *buf,
26 int64_t pos, int size);
27
28 /* Close a file and return an error code */
29 typedef int (QEMUFileCloseFunc)(void *opaque);
30
31 /* Called to determine if the file has exceeded it's bandwidth allocation. The
32 * bandwidth capping is a soft limit, not a hard limit.
33 */
34 typedef int (QEMUFileRateLimit)(void *opaque);
35
36 /* Called to change the current bandwidth allocation. This function must return
37 * the new actual bandwidth. It should be new_rate if everything goes ok, and
38 * the old rate otherwise
39 */
40 typedef int64_t (QEMUFileSetRateLimit)(void *opaque, int64_t new_rate);
41 typedef int64_t (QEMUFileGetRateLimit)(void *opaque);
42
43 QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
44 QEMUFileGetBufferFunc *get_buffer,
45 QEMUFileCloseFunc *close,
46 QEMUFileRateLimit *rate_limit,
47 QEMUFileSetRateLimit *set_rate_limit,
48 QEMUFileGetRateLimit *get_rate_limit);
49 QEMUFile *qemu_fopen(const char *filename, const char *mode);
50 QEMUFile *qemu_fdopen(int fd, const char *mode);
51 QEMUFile *qemu_fopen_socket(int fd);
52 QEMUFile *qemu_popen(FILE *popen_file, const char *mode);
53 QEMUFile *qemu_popen_cmd(const char *command, const char *mode);
54 int qemu_stdio_fd(QEMUFile *f);
55 void qemu_fflush(QEMUFile *f);
56 int qemu_fclose(QEMUFile *f);
57 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size);
58 void qemu_put_byte(QEMUFile *f, int v);
59
qemu_put_ubyte(QEMUFile * f,unsigned int v)60 static inline void qemu_put_ubyte(QEMUFile *f, unsigned int v)
61 {
62 qemu_put_byte(f, (int)v);
63 }
64
65 #define qemu_put_sbyte qemu_put_byte
66
67 void qemu_put_be16(QEMUFile *f, unsigned int v);
68 void qemu_put_be32(QEMUFile *f, unsigned int v);
69 void qemu_put_be64(QEMUFile *f, uint64_t v);
70 #ifdef CONFIG_ANDROID
71 void qemu_put_float(QEMUFile *f, float v);
72 #endif
73 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size);
74 int qemu_get_byte(QEMUFile *f);
75
qemu_get_ubyte(QEMUFile * f)76 static inline unsigned int qemu_get_ubyte(QEMUFile *f)
77 {
78 return (unsigned int)qemu_get_byte(f);
79 }
80
81 #define qemu_get_sbyte qemu_get_byte
82
83 unsigned int qemu_get_be16(QEMUFile *f);
84 unsigned int qemu_get_be32(QEMUFile *f);
85 uint64_t qemu_get_be64(QEMUFile *f);
86 #ifdef CONFIG_ANDROID
87 float qemu_get_float(QEMUFile *f);
88 #endif
89 int qemu_file_rate_limit(QEMUFile *f);
90 int64_t qemu_file_set_rate_limit(QEMUFile *f, int64_t new_rate);
91 int64_t qemu_file_get_rate_limit(QEMUFile *f);
92 int qemu_file_has_error(QEMUFile *f);
93 void qemu_file_set_error(QEMUFile *f);
94
95 /* Try to send any outstanding data. This function is useful when output is
96 * halted due to rate limiting or EAGAIN errors occur as it can be used to
97 * resume output. */
98 void qemu_file_put_notify(QEMUFile *f);
99
qemu_put_be64s(QEMUFile * f,const uint64_t * pv)100 static inline void qemu_put_be64s(QEMUFile *f, const uint64_t *pv)
101 {
102 qemu_put_be64(f, *pv);
103 }
104
qemu_put_be32s(QEMUFile * f,const uint32_t * pv)105 static inline void qemu_put_be32s(QEMUFile *f, const uint32_t *pv)
106 {
107 qemu_put_be32(f, *pv);
108 }
109
qemu_put_be16s(QEMUFile * f,const uint16_t * pv)110 static inline void qemu_put_be16s(QEMUFile *f, const uint16_t *pv)
111 {
112 qemu_put_be16(f, *pv);
113 }
114
qemu_put_8s(QEMUFile * f,const uint8_t * pv)115 static inline void qemu_put_8s(QEMUFile *f, const uint8_t *pv)
116 {
117 qemu_put_byte(f, *pv);
118 }
119
qemu_get_be64s(QEMUFile * f,uint64_t * pv)120 static inline void qemu_get_be64s(QEMUFile *f, uint64_t *pv)
121 {
122 *pv = qemu_get_be64(f);
123 }
124
qemu_get_be32s(QEMUFile * f,uint32_t * pv)125 static inline void qemu_get_be32s(QEMUFile *f, uint32_t *pv)
126 {
127 *pv = qemu_get_be32(f);
128 }
129
qemu_get_be16s(QEMUFile * f,uint16_t * pv)130 static inline void qemu_get_be16s(QEMUFile *f, uint16_t *pv)
131 {
132 *pv = qemu_get_be16(f);
133 }
134
qemu_get_8s(QEMUFile * f,uint8_t * pv)135 static inline void qemu_get_8s(QEMUFile *f, uint8_t *pv)
136 {
137 *pv = qemu_get_byte(f);
138 }
139
140 // Signed versions for type safety
qemu_put_sbuffer(QEMUFile * f,const int8_t * buf,int size)141 static inline void qemu_put_sbuffer(QEMUFile *f, const int8_t *buf, int size)
142 {
143 qemu_put_buffer(f, (const uint8_t *)buf, size);
144 }
145
qemu_put_sbe16(QEMUFile * f,int v)146 static inline void qemu_put_sbe16(QEMUFile *f, int v)
147 {
148 qemu_put_be16(f, (unsigned int)v);
149 }
150
qemu_put_sbe32(QEMUFile * f,int v)151 static inline void qemu_put_sbe32(QEMUFile *f, int v)
152 {
153 qemu_put_be32(f, (unsigned int)v);
154 }
155
qemu_put_sbe64(QEMUFile * f,int64_t v)156 static inline void qemu_put_sbe64(QEMUFile *f, int64_t v)
157 {
158 qemu_put_be64(f, (uint64_t)v);
159 }
160
qemu_get_sbuffer(QEMUFile * f,int8_t * buf,int size)161 static inline size_t qemu_get_sbuffer(QEMUFile *f, int8_t *buf, int size)
162 {
163 return qemu_get_buffer(f, (uint8_t *)buf, size);
164 }
165
qemu_get_sbe16(QEMUFile * f)166 static inline int qemu_get_sbe16(QEMUFile *f)
167 {
168 return (int)qemu_get_be16(f);
169 }
170
qemu_get_sbe32(QEMUFile * f)171 static inline int qemu_get_sbe32(QEMUFile *f)
172 {
173 return (int)qemu_get_be32(f);
174 }
175
qemu_get_sbe64(QEMUFile * f)176 static inline int64_t qemu_get_sbe64(QEMUFile *f)
177 {
178 return (int64_t)qemu_get_be64(f);
179 }
180
qemu_put_s8s(QEMUFile * f,const int8_t * pv)181 static inline void qemu_put_s8s(QEMUFile *f, const int8_t *pv)
182 {
183 qemu_put_8s(f, (const uint8_t *)pv);
184 }
185
qemu_put_sbe16s(QEMUFile * f,const int16_t * pv)186 static inline void qemu_put_sbe16s(QEMUFile *f, const int16_t *pv)
187 {
188 qemu_put_be16s(f, (const uint16_t *)pv);
189 }
190
qemu_put_sbe32s(QEMUFile * f,const int32_t * pv)191 static inline void qemu_put_sbe32s(QEMUFile *f, const int32_t *pv)
192 {
193 qemu_put_be32s(f, (const uint32_t *)pv);
194 }
195
qemu_put_sbe64s(QEMUFile * f,const int64_t * pv)196 static inline void qemu_put_sbe64s(QEMUFile *f, const int64_t *pv)
197 {
198 qemu_put_be64s(f, (const uint64_t *)pv);
199 }
200
qemu_get_s8s(QEMUFile * f,int8_t * pv)201 static inline void qemu_get_s8s(QEMUFile *f, int8_t *pv)
202 {
203 qemu_get_8s(f, (uint8_t *)pv);
204 }
205
qemu_get_sbe16s(QEMUFile * f,int16_t * pv)206 static inline void qemu_get_sbe16s(QEMUFile *f, int16_t *pv)
207 {
208 qemu_get_be16s(f, (uint16_t *)pv);
209 }
210
qemu_get_sbe32s(QEMUFile * f,int32_t * pv)211 static inline void qemu_get_sbe32s(QEMUFile *f, int32_t *pv)
212 {
213 qemu_get_be32s(f, (uint32_t *)pv);
214 }
215
qemu_get_sbe64s(QEMUFile * f,int64_t * pv)216 static inline void qemu_get_sbe64s(QEMUFile *f, int64_t *pv)
217 {
218 qemu_get_be64s(f, (uint64_t *)pv);
219 }
220
221 #ifdef CONFIG_ANDROID
222 void qemu_put_string(QEMUFile *f, const char* str);
223 char* qemu_get_string(QEMUFile *f);
224 #endif
225
226 #ifdef NEED_CPU_H
227 #if TARGET_LONG_BITS == 64
228 #define qemu_put_betl qemu_put_be64
229 #define qemu_get_betl qemu_get_be64
230 #define qemu_put_betls qemu_put_be64s
231 #define qemu_get_betls qemu_get_be64s
232 #define qemu_put_sbetl qemu_put_sbe64
233 #define qemu_get_sbetl qemu_get_sbe64
234 #define qemu_put_sbetls qemu_put_sbe64s
235 #define qemu_get_sbetls qemu_get_sbe64s
236 #else
237 #define qemu_put_betl qemu_put_be32
238 #define qemu_get_betl qemu_get_be32
239 #define qemu_put_betls qemu_put_be32s
240 #define qemu_get_betls qemu_get_be32s
241 #define qemu_put_sbetl qemu_put_sbe32
242 #define qemu_get_sbetl qemu_get_sbe32
243 #define qemu_put_sbetls qemu_put_sbe32s
244 #define qemu_get_sbetls qemu_get_sbe32s
245 #endif
246 #endif
247
248 int64_t qemu_ftell(QEMUFile *f);
249 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence);
250
251 typedef void SaveStateHandler(QEMUFile *f, void *opaque);
252 typedef int SaveLiveStateHandler(QEMUFile *f, int stage, void *opaque);
253 typedef int LoadStateHandler(QEMUFile *f, void *opaque, int version_id);
254
255 int register_savevm(const char *idstr,
256 int instance_id,
257 int version_id,
258 SaveStateHandler *save_state,
259 LoadStateHandler *load_state,
260 void *opaque);
261
262 int register_savevm_live(const char *idstr,
263 int instance_id,
264 int version_id,
265 SaveLiveStateHandler *save_live_state,
266 SaveStateHandler *save_state,
267 LoadStateHandler *load_state,
268 void *opaque);
269
270 void unregister_savevm(const char *idstr, void *opaque);
271
272 typedef void QEMUResetHandler(void *opaque);
273
274 void qemu_register_reset(QEMUResetHandler *func, int order, void *opaque);
275
276 /* handler to set the boot_device for a specific type of QEMUMachine */
277 /* return 0 if success */
278 typedef int QEMUBootSetHandler(void *opaque, const char *boot_device);
279 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque);
280
281 #endif
282