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