Merge remote-tracking branch 'bonzini/scsi-next' into staging
[qemu-kvm.git] / qemu-common.h
blobf16079f432ba07cc5122e697a4a0c347d465435a
1 /* Common header file that is included by all of qemu. */
2 #ifndef QEMU_COMMON_H
3 #define QEMU_COMMON_H
5 #include "compiler.h"
6 #include "config-host.h"
8 #if defined(__arm__) || defined(__sparc__) || defined(__mips__) || defined(__hppa__) || defined(__ia64__)
9 #define WORDS_ALIGNED
10 #endif
12 #define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
14 typedef struct QEMUTimer QEMUTimer;
15 typedef struct QEMUFile QEMUFile;
16 typedef struct DeviceState DeviceState;
18 struct Monitor;
19 typedef struct Monitor Monitor;
20 typedef struct MigrationParams MigrationParams;
22 /* we put basic includes here to avoid repeating them in device drivers */
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <stdarg.h>
26 #include <stdbool.h>
27 #include <string.h>
28 #include <strings.h>
29 #include <inttypes.h>
30 #include <limits.h>
31 #include <time.h>
32 #include <ctype.h>
33 #include <errno.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <sys/stat.h>
37 #include <sys/time.h>
38 #include <assert.h>
39 #include <signal.h>
40 #include <glib.h>
42 #ifdef _WIN32
43 #include "qemu-os-win32.h"
44 #endif
46 #ifdef CONFIG_POSIX
47 #include "qemu-os-posix.h"
48 #endif
50 #ifndef O_LARGEFILE
51 #define O_LARGEFILE 0
52 #endif
53 #ifndef O_BINARY
54 #define O_BINARY 0
55 #endif
56 #ifndef MAP_ANONYMOUS
57 #define MAP_ANONYMOUS MAP_ANON
58 #endif
59 #ifndef ENOMEDIUM
60 #define ENOMEDIUM ENODEV
61 #endif
62 #if !defined(ENOTSUP)
63 #define ENOTSUP 4096
64 #endif
65 #if !defined(ECANCELED)
66 #define ECANCELED 4097
67 #endif
68 #ifndef TIME_MAX
69 #define TIME_MAX LONG_MAX
70 #endif
72 /* HOST_LONG_BITS is the size of a native pointer in bits. */
73 #if UINTPTR_MAX == UINT32_MAX
74 # define HOST_LONG_BITS 32
75 #elif UINTPTR_MAX == UINT64_MAX
76 # define HOST_LONG_BITS 64
77 #else
78 # error Unknown pointer size
79 #endif
81 #ifndef CONFIG_IOVEC
82 #define CONFIG_IOVEC
83 struct iovec {
84 void *iov_base;
85 size_t iov_len;
88 * Use the same value as Linux for now.
90 #define IOV_MAX 1024
91 #else
92 #include <sys/uio.h>
93 #endif
95 typedef int (*fprintf_function)(FILE *f, const char *fmt, ...)
96 GCC_FMT_ATTR(2, 3);
98 #ifdef _WIN32
99 #define fsync _commit
100 #if !defined(lseek)
101 # define lseek _lseeki64
102 #endif
103 int qemu_ftruncate64(int, int64_t);
104 #if !defined(ftruncate)
105 # define ftruncate qemu_ftruncate64
106 #endif
108 static inline char *realpath(const char *path, char *resolved_path)
110 _fullpath(resolved_path, path, _MAX_PATH);
111 return resolved_path;
113 #endif
115 /* icount */
116 void configure_icount(const char *option);
117 extern int use_icount;
119 /* FIXME: Remove NEED_CPU_H. */
120 #ifndef NEED_CPU_H
122 #include "osdep.h"
123 #include "bswap.h"
125 #else
127 #include "cpu.h"
129 #endif /* !defined(NEED_CPU_H) */
131 /* main function, renamed */
132 #if defined(CONFIG_COCOA)
133 int qemu_main(int argc, char **argv, char **envp);
134 #endif
136 void qemu_get_timedate(struct tm *tm, int offset);
137 int qemu_timedate_diff(struct tm *tm);
140 * is_help_option:
141 * @s: string to test
143 * Check whether @s is one of the standard strings which indicate
144 * that the user is asking for a list of the valid values for a
145 * command option like -cpu or -M. The current accepted strings
146 * are 'help' and '?'. '?' is deprecated (it is a shell wildcard
147 * which makes it annoying to use in a reliable way) but provided
148 * for backwards compatibility.
150 * Returns: true if @s is a request for a list.
152 static inline bool is_help_option(const char *s)
154 return !strcmp(s, "?") || !strcmp(s, "help");
157 /* cutils.c */
158 void pstrcpy(char *buf, int buf_size, const char *str);
159 void strpadcpy(char *buf, int buf_size, const char *str, char pad);
160 char *pstrcat(char *buf, int buf_size, const char *s);
161 int strstart(const char *str, const char *val, const char **ptr);
162 int stristart(const char *str, const char *val, const char **ptr);
163 int qemu_strnlen(const char *s, int max_len);
164 time_t mktimegm(struct tm *tm);
165 int qemu_fls(int i);
166 int qemu_fdatasync(int fd);
167 int fcntl_setfl(int fd, int flag);
168 int qemu_parse_fd(const char *param);
171 * strtosz() suffixes used to specify the default treatment of an
172 * argument passed to strtosz() without an explicit suffix.
173 * These should be defined using upper case characters in the range
174 * A-Z, as strtosz() will use qemu_toupper() on the given argument
175 * prior to comparison.
177 #define STRTOSZ_DEFSUFFIX_TB 'T'
178 #define STRTOSZ_DEFSUFFIX_GB 'G'
179 #define STRTOSZ_DEFSUFFIX_MB 'M'
180 #define STRTOSZ_DEFSUFFIX_KB 'K'
181 #define STRTOSZ_DEFSUFFIX_B 'B'
182 int64_t strtosz(const char *nptr, char **end);
183 int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix);
184 int64_t strtosz_suffix_unit(const char *nptr, char **end,
185 const char default_suffix, int64_t unit);
187 /* path.c */
188 void init_paths(const char *prefix);
189 const char *path(const char *pathname);
191 #define qemu_isalnum(c) isalnum((unsigned char)(c))
192 #define qemu_isalpha(c) isalpha((unsigned char)(c))
193 #define qemu_iscntrl(c) iscntrl((unsigned char)(c))
194 #define qemu_isdigit(c) isdigit((unsigned char)(c))
195 #define qemu_isgraph(c) isgraph((unsigned char)(c))
196 #define qemu_islower(c) islower((unsigned char)(c))
197 #define qemu_isprint(c) isprint((unsigned char)(c))
198 #define qemu_ispunct(c) ispunct((unsigned char)(c))
199 #define qemu_isspace(c) isspace((unsigned char)(c))
200 #define qemu_isupper(c) isupper((unsigned char)(c))
201 #define qemu_isxdigit(c) isxdigit((unsigned char)(c))
202 #define qemu_tolower(c) tolower((unsigned char)(c))
203 #define qemu_toupper(c) toupper((unsigned char)(c))
204 #define qemu_isascii(c) isascii((unsigned char)(c))
205 #define qemu_toascii(c) toascii((unsigned char)(c))
207 void *qemu_oom_check(void *ptr);
209 int qemu_open(const char *name, int flags, ...);
210 ssize_t qemu_write_full(int fd, const void *buf, size_t count)
211 QEMU_WARN_UNUSED_RESULT;
212 ssize_t qemu_send_full(int fd, const void *buf, size_t count, int flags)
213 QEMU_WARN_UNUSED_RESULT;
214 ssize_t qemu_recv_full(int fd, void *buf, size_t count, int flags)
215 QEMU_WARN_UNUSED_RESULT;
217 #ifndef _WIN32
218 int qemu_eventfd(int pipefd[2]);
219 int qemu_pipe(int pipefd[2]);
220 #endif
222 #ifdef _WIN32
223 #define qemu_recv(sockfd, buf, len, flags) recv(sockfd, (void *)buf, len, flags)
224 #else
225 #define qemu_recv(sockfd, buf, len, flags) recv(sockfd, buf, len, flags)
226 #endif
228 /* Error handling. */
230 void QEMU_NORETURN hw_error(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
232 struct ParallelIOArg {
233 void *buffer;
234 int count;
237 typedef int (*DMA_transfer_handler) (void *opaque, int nchan, int pos, int size);
239 /* A load of opaque types so that device init declarations don't have to
240 pull in all the real definitions. */
241 typedef struct NICInfo NICInfo;
242 typedef struct HCIInfo HCIInfo;
243 typedef struct AudioState AudioState;
244 typedef struct BlockDriverState BlockDriverState;
245 typedef struct DriveInfo DriveInfo;
246 typedef struct DisplayState DisplayState;
247 typedef struct DisplayChangeListener DisplayChangeListener;
248 typedef struct DisplaySurface DisplaySurface;
249 typedef struct DisplayAllocator DisplayAllocator;
250 typedef struct PixelFormat PixelFormat;
251 typedef struct TextConsole TextConsole;
252 typedef TextConsole QEMUConsole;
253 typedef struct CharDriverState CharDriverState;
254 typedef struct MACAddr MACAddr;
255 typedef struct NetClientState NetClientState;
256 typedef struct i2c_bus i2c_bus;
257 typedef struct ISABus ISABus;
258 typedef struct ISADevice ISADevice;
259 typedef struct SMBusDevice SMBusDevice;
260 typedef struct PCIHostState PCIHostState;
261 typedef struct PCIExpressHost PCIExpressHost;
262 typedef struct PCIBus PCIBus;
263 typedef struct PCIDevice PCIDevice;
264 typedef struct PCIExpressDevice PCIExpressDevice;
265 typedef struct PCIBridge PCIBridge;
266 typedef struct PCIEAERMsg PCIEAERMsg;
267 typedef struct PCIEAERLog PCIEAERLog;
268 typedef struct PCIEAERErr PCIEAERErr;
269 typedef struct PCIEPort PCIEPort;
270 typedef struct PCIESlot PCIESlot;
271 typedef struct MSIMessage MSIMessage;
272 typedef struct SerialState SerialState;
273 typedef struct IRQState *qemu_irq;
274 typedef struct PCMCIACardState PCMCIACardState;
275 typedef struct MouseTransformInfo MouseTransformInfo;
276 typedef struct uWireSlave uWireSlave;
277 typedef struct I2SCodec I2SCodec;
278 typedef struct SSIBus SSIBus;
279 typedef struct EventNotifier EventNotifier;
280 typedef struct VirtIODevice VirtIODevice;
281 typedef struct QEMUSGList QEMUSGList;
282 typedef struct SHPCDevice SHPCDevice;
284 typedef uint64_t pcibus_t;
286 typedef enum LostTickPolicy {
287 LOST_TICK_DISCARD,
288 LOST_TICK_DELAY,
289 LOST_TICK_MERGE,
290 LOST_TICK_SLEW,
291 LOST_TICK_MAX
292 } LostTickPolicy;
294 typedef struct PCIHostDeviceAddress {
295 unsigned int domain;
296 unsigned int bus;
297 unsigned int slot;
298 unsigned int function;
299 } PCIHostDeviceAddress;
301 void tcg_exec_init(unsigned long tb_size);
302 bool tcg_enabled(void);
304 void cpu_exec_init_all(void);
306 /* CPU save/load. */
307 void cpu_save(QEMUFile *f, void *opaque);
308 int cpu_load(QEMUFile *f, void *opaque, int version_id);
310 /* Unblock cpu */
311 void qemu_cpu_kick(void *env);
312 void qemu_cpu_kick_self(void);
313 int qemu_cpu_is_self(void *env);
315 /* work queue */
316 struct qemu_work_item {
317 struct qemu_work_item *next;
318 void (*func)(void *data);
319 void *data;
320 int done;
323 #ifdef CONFIG_USER_ONLY
324 #define qemu_init_vcpu(env) do { } while (0)
325 #else
326 void qemu_init_vcpu(void *env);
327 #endif
331 * Sends a (part of) iovec down a socket, yielding when the socket is full, or
332 * Receives data into a (part of) iovec from a socket,
333 * yielding when there is no data in the socket.
334 * The same interface as qemu_sendv_recvv(), with added yielding.
335 * XXX should mark these as coroutine_fn
337 ssize_t qemu_co_sendv_recvv(int sockfd, struct iovec *iov, unsigned iov_cnt,
338 size_t offset, size_t bytes, bool do_send);
339 #define qemu_co_recvv(sockfd, iov, iov_cnt, offset, bytes) \
340 qemu_co_sendv_recvv(sockfd, iov, iov_cnt, offset, bytes, false)
341 #define qemu_co_sendv(sockfd, iov, iov_cnt, offset, bytes) \
342 qemu_co_sendv_recvv(sockfd, iov, iov_cnt, offset, bytes, true)
345 * The same as above, but with just a single buffer
347 ssize_t qemu_co_send_recv(int sockfd, void *buf, size_t bytes, bool do_send);
348 #define qemu_co_recv(sockfd, buf, bytes) \
349 qemu_co_send_recv(sockfd, buf, bytes, false)
350 #define qemu_co_send(sockfd, buf, bytes) \
351 qemu_co_send_recv(sockfd, buf, bytes, true)
353 typedef struct QEMUIOVector {
354 struct iovec *iov;
355 int niov;
356 int nalloc;
357 size_t size;
358 } QEMUIOVector;
360 void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint);
361 void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov);
362 void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len);
363 void qemu_iovec_concat(QEMUIOVector *dst,
364 QEMUIOVector *src, size_t soffset, size_t sbytes);
365 void qemu_iovec_destroy(QEMUIOVector *qiov);
366 void qemu_iovec_reset(QEMUIOVector *qiov);
367 size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset,
368 void *buf, size_t bytes);
369 size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset,
370 const void *buf, size_t bytes);
371 size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
372 int fillc, size_t bytes);
374 bool buffer_is_zero(const void *buf, size_t len);
376 void qemu_progress_init(int enabled, float min_skip);
377 void qemu_progress_end(void);
378 void qemu_progress_print(float delta, int max);
380 #define QEMU_FILE_TYPE_BIOS 0
381 #define QEMU_FILE_TYPE_KEYMAP 1
382 char *qemu_find_file(int type, const char *name);
384 /* OS specific functions */
385 void os_setup_early_signal_handling(void);
386 char *os_find_datadir(const char *argv0);
387 void os_parse_cmd_args(int index, const char *optarg);
388 void os_pidfile_error(void);
390 /* Convert a byte between binary and BCD. */
391 static inline uint8_t to_bcd(uint8_t val)
393 return ((val / 10) << 4) | (val % 10);
396 static inline uint8_t from_bcd(uint8_t val)
398 return ((val >> 4) * 10) + (val & 0x0f);
401 /* compute with 96 bit intermediate result: (a*b)/c */
402 static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
404 union {
405 uint64_t ll;
406 struct {
407 #ifdef HOST_WORDS_BIGENDIAN
408 uint32_t high, low;
409 #else
410 uint32_t low, high;
411 #endif
412 } l;
413 } u, res;
414 uint64_t rl, rh;
416 u.ll = a;
417 rl = (uint64_t)u.l.low * (uint64_t)b;
418 rh = (uint64_t)u.l.high * (uint64_t)b;
419 rh += (rl >> 32);
420 res.l.high = rh / c;
421 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
422 return res.ll;
425 /* Round number down to multiple */
426 #define QEMU_ALIGN_DOWN(n, m) ((n) / (m) * (m))
428 /* Round number up to multiple */
429 #define QEMU_ALIGN_UP(n, m) QEMU_ALIGN_DOWN((n) + (m) - 1, (m))
431 #include "module.h"
433 #endif