1 /* 2 * QEMU System Emulator 3 * 4 * Copyright (c) 2003-2008 Fabrice Bellard 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to deal 8 * in the Software without restriction, including without limitation the rights 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 * copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 * THE SOFTWARE. 23 */ 24 #ifndef QEMU_FILE_H 25 #define QEMU_FILE_H 1 26 27 #include <stdbool.h> 28 #include <stdint.h> 29 #include <stdio.h> // for FILE 30 #include <sys/types.h> // only for ssize_t, *sigh* 31 32 #include "qemu/typedefs.h" // for QEMUFile 33 34 struct iovec; 35 36 // Upstream uses this to declare ram_addr_t, but this forces a 37 // stupid dependency that generates other conflicts. Instead, this 38 // header uses uint64_t instead of ram_addr_t for portability. 39 //#include "exec/cpu-common.h" 40 41 /* This function writes a chunk of data to a file at the given position. 42 * The pos argument can be ignored if the file is only being used for 43 * streaming. The handler should try to write all of the data it can. 44 */ 45 typedef int (QEMUFilePutBufferFunc)(void *opaque, const uint8_t *buf, 46 int64_t pos, int size); 47 48 /* Read a chunk of data from a file at the given position. The pos argument 49 * can be ignored if the file is only be used for streaming. The number of 50 * bytes actually read should be returned. 51 */ 52 typedef int (QEMUFileGetBufferFunc)(void *opaque, uint8_t *buf, 53 int64_t pos, int size); 54 55 /* Close a file 56 * 57 * Return negative error number on error, 0 or positive value on success. 58 * 59 * The meaning of return value on success depends on the specific back-end being 60 * used. 61 */ 62 typedef int (QEMUFileCloseFunc)(void *opaque); 63 64 /* Called to return the OS file descriptor associated to the QEMUFile. 65 */ 66 typedef int (QEMUFileGetFD)(void *opaque); 67 68 /* 69 * This function writes an iovec to file. 70 */ 71 typedef ssize_t (QEMUFileWritevBufferFunc)(void *opaque, struct iovec *iov, 72 int iovcnt, int64_t pos); 73 74 /* 75 * This function provides hooks around different 76 * stages of RAM migration. 77 */ 78 typedef int (QEMURamHookFunc)(QEMUFile *f, void *opaque, uint64_t flags); 79 80 /* 81 * Constants used by ram_control_* hooks 82 */ 83 #define RAM_CONTROL_SETUP 0 84 #define RAM_CONTROL_ROUND 1 85 #define RAM_CONTROL_HOOK 2 86 #define RAM_CONTROL_FINISH 3 87 88 /* 89 * This function allows override of where the RAM page 90 * is saved (such as RDMA, for example.) 91 */ 92 // NOTE(digit): Upstream uses ram_addr_t instead of uint64_t here. 93 typedef size_t (QEMURamSaveFunc)(QEMUFile *f, void *opaque, 94 uint64_t block_offset, 95 uint64_t offset, 96 size_t size, 97 int *bytes_sent); 98 99 typedef struct QEMUFileOps { 100 QEMUFilePutBufferFunc *put_buffer; 101 QEMUFileGetBufferFunc *get_buffer; 102 QEMUFileCloseFunc *close; 103 QEMUFileGetFD *get_fd; 104 QEMUFileWritevBufferFunc *writev_buffer; 105 QEMURamHookFunc *before_ram_iterate; 106 QEMURamHookFunc *after_ram_iterate; 107 QEMURamHookFunc *hook_ram_load; 108 QEMURamSaveFunc *save_page; 109 } QEMUFileOps; 110 111 QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops); 112 QEMUFile *qemu_fopen(const char *filename, const char *mode); 113 QEMUFile *qemu_fdopen(int fd, const char *mode); 114 QEMUFile *qemu_fopen_socket(int fd, const char *mode); 115 QEMUFile *qemu_popen_cmd(const char *command, const char *mode); 116 int qemu_get_fd(QEMUFile *f); 117 int qemu_fclose(QEMUFile *f); 118 int64_t qemu_ftell(QEMUFile *f); 119 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size); 120 void qemu_put_byte(QEMUFile *f, int v); 121 122 void qemu_file_set_error(QEMUFile* f, int error); 123 int qemu_file_has_error(QEMUFile* f); 124 void qemu_file_put_notify(QEMUFile* f); 125 126 /* 127 * put_buffer without copying the buffer. 128 * The buffer should be available till it is sent asynchronously. 129 */ 130 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, int size); 131 bool qemu_file_mode_is_not_valid(const char *mode); 132 133 static inline void qemu_put_ubyte(QEMUFile *f, unsigned int v) 134 { 135 qemu_put_byte(f, (int)v); 136 } 137 138 #define qemu_put_sbyte qemu_put_byte 139 140 void qemu_put_be16(QEMUFile *f, unsigned int v); 141 void qemu_put_be32(QEMUFile *f, unsigned int v); 142 void qemu_put_be64(QEMUFile *f, uint64_t v); 143 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size); 144 int qemu_get_byte(QEMUFile *f); 145 #ifdef CONFIG_ANDROID 146 void qemu_put_float(QEMUFile *f, float v); 147 #endif 148 149 void qemu_update_position(QEMUFile *f, size_t size); 150 151 static inline unsigned int qemu_get_ubyte(QEMUFile *f) 152 { 153 return (unsigned int)qemu_get_byte(f); 154 } 155 156 #define qemu_get_sbyte qemu_get_byte 157 158 unsigned int qemu_get_be16(QEMUFile *f); 159 unsigned int qemu_get_be32(QEMUFile *f); 160 uint64_t qemu_get_be64(QEMUFile *f); 161 #ifdef CONFIG_ANDROID 162 float qemu_get_float(QEMUFile *f); 163 #endif 164 int qemu_file_rate_limit(QEMUFile *f); 165 void qemu_file_reset_rate_limit(QEMUFile *f); 166 void qemu_file_set_rate_limit(QEMUFile *f, int64_t new_rate); 167 int64_t qemu_file_get_rate_limit(QEMUFile *f); 168 int qemu_file_get_error(QEMUFile *f); 169 void qemu_fflush(QEMUFile *f); 170 171 static inline void qemu_put_be64s(QEMUFile *f, const uint64_t *pv) 172 { 173 qemu_put_be64(f, *pv); 174 } 175 176 static inline void qemu_put_be32s(QEMUFile *f, const uint32_t *pv) 177 { 178 qemu_put_be32(f, *pv); 179 } 180 181 static inline void qemu_put_be16s(QEMUFile *f, const uint16_t *pv) 182 { 183 qemu_put_be16(f, *pv); 184 } 185 186 static inline void qemu_put_8s(QEMUFile *f, const uint8_t *pv) 187 { 188 qemu_put_byte(f, *pv); 189 } 190 191 static inline void qemu_get_be64s(QEMUFile *f, uint64_t *pv) 192 { 193 *pv = qemu_get_be64(f); 194 } 195 196 static inline void qemu_get_be32s(QEMUFile *f, uint32_t *pv) 197 { 198 *pv = qemu_get_be32(f); 199 } 200 201 static inline void qemu_get_be16s(QEMUFile *f, uint16_t *pv) 202 { 203 *pv = qemu_get_be16(f); 204 } 205 206 static inline void qemu_get_8s(QEMUFile *f, uint8_t *pv) 207 { 208 *pv = qemu_get_byte(f); 209 } 210 211 // Signed versions for type safety 212 static inline void qemu_put_sbuffer(QEMUFile *f, const int8_t *buf, int size) 213 { 214 qemu_put_buffer(f, (const uint8_t *)buf, size); 215 } 216 217 static inline void qemu_put_sbe16(QEMUFile *f, int v) 218 { 219 qemu_put_be16(f, (unsigned int)v); 220 } 221 222 static inline void qemu_put_sbe32(QEMUFile *f, int v) 223 { 224 qemu_put_be32(f, (unsigned int)v); 225 } 226 227 static inline void qemu_put_sbe64(QEMUFile *f, int64_t v) 228 { 229 qemu_put_be64(f, (uint64_t)v); 230 } 231 232 static inline size_t qemu_get_sbuffer(QEMUFile *f, int8_t *buf, int size) 233 { 234 return qemu_get_buffer(f, (uint8_t *)buf, size); 235 } 236 237 static inline int qemu_get_sbe16(QEMUFile *f) 238 { 239 return (int)qemu_get_be16(f); 240 } 241 242 static inline int qemu_get_sbe32(QEMUFile *f) 243 { 244 return (int)qemu_get_be32(f); 245 } 246 247 static inline int64_t qemu_get_sbe64(QEMUFile *f) 248 { 249 return (int64_t)qemu_get_be64(f); 250 } 251 252 static inline void qemu_put_s8s(QEMUFile *f, const int8_t *pv) 253 { 254 qemu_put_8s(f, (const uint8_t *)pv); 255 } 256 257 static inline void qemu_put_sbe16s(QEMUFile *f, const int16_t *pv) 258 { 259 qemu_put_be16s(f, (const uint16_t *)pv); 260 } 261 262 static inline void qemu_put_sbe32s(QEMUFile *f, const int32_t *pv) 263 { 264 qemu_put_be32s(f, (const uint32_t *)pv); 265 } 266 267 static inline void qemu_put_sbe64s(QEMUFile *f, const int64_t *pv) 268 { 269 qemu_put_be64s(f, (const uint64_t *)pv); 270 } 271 272 static inline void qemu_get_s8s(QEMUFile *f, int8_t *pv) 273 { 274 qemu_get_8s(f, (uint8_t *)pv); 275 } 276 277 static inline void qemu_get_sbe16s(QEMUFile *f, int16_t *pv) 278 { 279 qemu_get_be16s(f, (uint16_t *)pv); 280 } 281 282 static inline void qemu_get_sbe32s(QEMUFile *f, int32_t *pv) 283 { 284 qemu_get_be32s(f, (uint32_t *)pv); 285 } 286 287 static inline void qemu_get_sbe64s(QEMUFile *f, int64_t *pv) 288 { 289 qemu_get_be64s(f, (uint64_t *)pv); 290 } 291 292 #ifdef CONFIG_ANDROID 293 void qemu_put_string(QEMUFile *f, const char* str); 294 char* qemu_get_string(QEMUFile *f); 295 #endif 296 297 typedef enum { 298 Q_FIELD_END, /* mark end of field list */ 299 Q_FIELD_BYTE, /* for 1-byte fields */ 300 Q_FIELD_INT16, /* for 2-byte fields */ 301 Q_FIELD_INT32, /* for 4-byte fields */ 302 Q_FIELD_INT64, /* for 8-byte fields */ 303 Q_FIELD_BUFFER, /* for buffer fields */ 304 Q_FIELD_BUFFER_SIZE, /* to specify the size of buffers */ 305 } QFieldType; 306 307 typedef struct { 308 QFieldType type : 16; /* field type */ 309 uint16_t offset; /* offset of field in structure */ 310 } QField; 311 312 #define QFIELD_BEGIN(name) \ 313 static const QField name[] = { 314 315 #define _QFIELD_(t, f) { t, offsetof(QFIELD_STRUCT,f) } 316 #define QFIELD_BYTE(f) _QFIELD_(Q_FIELD_BYTE, f) 317 #define QFIELD_INT16(f) _QFIELD_(Q_FIELD_INT16, f) 318 #define QFIELD_INT32(f) _QFIELD_(Q_FIELD_INT32, f) 319 #define QFIELD_INT64(f) _QFIELD_(Q_FIELD_INT64, f) 320 #define QFIELD_TL(f) _QFIELD_(Q_FIELD_TL, f) 321 322 #define _QFIELD_SIZEOF(f) sizeof(((QFIELD_STRUCT*)0)->f) 323 324 #define QFIELD_BUFFER(f) \ 325 _QFIELD_(Q_FIELD_BUFFER, f), \ 326 { Q_FIELD_BUFFER_SIZE, (uint16_t)(_QFIELD_SIZEOF(f) >> 16) }, \ 327 { Q_FIELD_BUFFER_SIZE, (uint16_t) _QFIELD_SIZEOF(f) } 328 329 #define QFIELD_END \ 330 { Q_FIELD_END, 0 }, \ 331 }; 332 333 extern void qemu_put_struct(QEMUFile* f, const QField* fields, const void* s); 334 extern int qemu_get_struct(QEMUFile* f, const QField* fields, void* s); 335 336 #endif /* _QEMU_FILE_H */ 337