vnc-palette.h: Include <stdbool.h>
[qemu/kevin.git] / qemu-common.h
blob9112e2de7a28e3df72ada4d3098e114ee744b929
2 /* Common header file that is included by all of QEMU.
4 * This file is supposed to be included only by .c files. No header file should
5 * depend on qemu-common.h, as this would easily lead to circular header
6 * dependencies.
8 * If a header file uses a definition from qemu-common.h, that definition
9 * must be moved to a separate header file, and the header that uses it
10 * must include that header.
12 #ifndef QEMU_COMMON_H
13 #define QEMU_COMMON_H
15 #include "compiler.h"
16 #include "config-host.h"
18 #if defined(__arm__) || defined(__sparc__) || defined(__mips__) || defined(__hppa__) || defined(__ia64__)
19 #define WORDS_ALIGNED
20 #endif
22 #define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
24 typedef struct QEMUTimer QEMUTimer;
25 typedef struct QEMUFile QEMUFile;
26 typedef struct QEMUBH QEMUBH;
27 typedef struct DeviceState DeviceState;
29 struct Monitor;
30 typedef struct Monitor Monitor;
31 typedef struct MigrationParams MigrationParams;
33 /* we put basic includes here to avoid repeating them in device drivers */
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <stdarg.h>
37 #include <stdbool.h>
38 #include <string.h>
39 #include <strings.h>
40 #include <inttypes.h>
41 #include <limits.h>
42 #include <time.h>
43 #include <ctype.h>
44 #include <errno.h>
45 #include <unistd.h>
46 #include <fcntl.h>
47 #include <sys/stat.h>
48 #include <sys/time.h>
49 #include <assert.h>
50 #include <signal.h>
51 #include <glib.h>
53 #ifdef _WIN32
54 #include "qemu-os-win32.h"
55 #endif
57 #ifdef CONFIG_POSIX
58 #include "qemu-os-posix.h"
59 #endif
61 #ifndef O_LARGEFILE
62 #define O_LARGEFILE 0
63 #endif
64 #ifndef O_BINARY
65 #define O_BINARY 0
66 #endif
67 #ifndef MAP_ANONYMOUS
68 #define MAP_ANONYMOUS MAP_ANON
69 #endif
70 #ifndef ENOMEDIUM
71 #define ENOMEDIUM ENODEV
72 #endif
73 #if !defined(ENOTSUP)
74 #define ENOTSUP 4096
75 #endif
76 #if !defined(ECANCELED)
77 #define ECANCELED 4097
78 #endif
79 #ifndef TIME_MAX
80 #define TIME_MAX LONG_MAX
81 #endif
83 /* HOST_LONG_BITS is the size of a native pointer in bits. */
84 #if UINTPTR_MAX == UINT32_MAX
85 # define HOST_LONG_BITS 32
86 #elif UINTPTR_MAX == UINT64_MAX
87 # define HOST_LONG_BITS 64
88 #else
89 # error Unknown pointer size
90 #endif
92 #ifndef CONFIG_IOVEC
93 #define CONFIG_IOVEC
94 struct iovec {
95 void *iov_base;
96 size_t iov_len;
99 * Use the same value as Linux for now.
101 #define IOV_MAX 1024
102 #else
103 #include <sys/uio.h>
104 #endif
106 typedef int (*fprintf_function)(FILE *f, const char *fmt, ...)
107 GCC_FMT_ATTR(2, 3);
109 #ifdef _WIN32
110 #define fsync _commit
111 #if !defined(lseek)
112 # define lseek _lseeki64
113 #endif
114 int qemu_ftruncate64(int, int64_t);
115 #if !defined(ftruncate)
116 # define ftruncate qemu_ftruncate64
117 #endif
119 static inline char *realpath(const char *path, char *resolved_path)
121 _fullpath(resolved_path, path, _MAX_PATH);
122 return resolved_path;
124 #endif
126 /* icount */
127 void configure_icount(const char *option);
128 extern int use_icount;
130 /* FIXME: Remove NEED_CPU_H. */
131 #ifndef NEED_CPU_H
133 #include "osdep.h"
134 #include "bswap.h"
136 #else
138 #include "cpu.h"
140 #endif /* !defined(NEED_CPU_H) */
142 /* main function, renamed */
143 #if defined(CONFIG_COCOA)
144 int qemu_main(int argc, char **argv, char **envp);
145 #endif
147 void qemu_get_timedate(struct tm *tm, int offset);
148 int qemu_timedate_diff(struct tm *tm);
151 * is_help_option:
152 * @s: string to test
154 * Check whether @s is one of the standard strings which indicate
155 * that the user is asking for a list of the valid values for a
156 * command option like -cpu or -M. The current accepted strings
157 * are 'help' and '?'. '?' is deprecated (it is a shell wildcard
158 * which makes it annoying to use in a reliable way) but provided
159 * for backwards compatibility.
161 * Returns: true if @s is a request for a list.
163 static inline bool is_help_option(const char *s)
165 return !strcmp(s, "?") || !strcmp(s, "help");
168 /* cutils.c */
169 void pstrcpy(char *buf, int buf_size, const char *str);
170 void strpadcpy(char *buf, int buf_size, const char *str, char pad);
171 char *pstrcat(char *buf, int buf_size, const char *s);
172 int strstart(const char *str, const char *val, const char **ptr);
173 int stristart(const char *str, const char *val, const char **ptr);
174 int qemu_strnlen(const char *s, int max_len);
175 time_t mktimegm(struct tm *tm);
176 int qemu_fls(int i);
177 int qemu_fdatasync(int fd);
178 int fcntl_setfl(int fd, int flag);
179 int qemu_parse_fd(const char *param);
182 * strtosz() suffixes used to specify the default treatment of an
183 * argument passed to strtosz() without an explicit suffix.
184 * These should be defined using upper case characters in the range
185 * A-Z, as strtosz() will use qemu_toupper() on the given argument
186 * prior to comparison.
188 #define STRTOSZ_DEFSUFFIX_TB 'T'
189 #define STRTOSZ_DEFSUFFIX_GB 'G'
190 #define STRTOSZ_DEFSUFFIX_MB 'M'
191 #define STRTOSZ_DEFSUFFIX_KB 'K'
192 #define STRTOSZ_DEFSUFFIX_B 'B'
193 int64_t strtosz(const char *nptr, char **end);
194 int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix);
195 int64_t strtosz_suffix_unit(const char *nptr, char **end,
196 const char default_suffix, int64_t unit);
198 /* path.c */
199 void init_paths(const char *prefix);
200 const char *path(const char *pathname);
202 #define qemu_isalnum(c) isalnum((unsigned char)(c))
203 #define qemu_isalpha(c) isalpha((unsigned char)(c))
204 #define qemu_iscntrl(c) iscntrl((unsigned char)(c))
205 #define qemu_isdigit(c) isdigit((unsigned char)(c))
206 #define qemu_isgraph(c) isgraph((unsigned char)(c))
207 #define qemu_islower(c) islower((unsigned char)(c))
208 #define qemu_isprint(c) isprint((unsigned char)(c))
209 #define qemu_ispunct(c) ispunct((unsigned char)(c))
210 #define qemu_isspace(c) isspace((unsigned char)(c))
211 #define qemu_isupper(c) isupper((unsigned char)(c))
212 #define qemu_isxdigit(c) isxdigit((unsigned char)(c))
213 #define qemu_tolower(c) tolower((unsigned char)(c))
214 #define qemu_toupper(c) toupper((unsigned char)(c))
215 #define qemu_isascii(c) isascii((unsigned char)(c))
216 #define qemu_toascii(c) toascii((unsigned char)(c))
218 void *qemu_oom_check(void *ptr);
220 int qemu_open(const char *name, int flags, ...);
221 int qemu_close(int fd);
222 ssize_t qemu_write_full(int fd, const void *buf, size_t count)
223 QEMU_WARN_UNUSED_RESULT;
224 ssize_t qemu_send_full(int fd, const void *buf, size_t count, int flags)
225 QEMU_WARN_UNUSED_RESULT;
226 ssize_t qemu_recv_full(int fd, void *buf, size_t count, int flags)
227 QEMU_WARN_UNUSED_RESULT;
229 #ifndef _WIN32
230 int qemu_pipe(int pipefd[2]);
231 #endif
233 #ifdef _WIN32
234 /* MinGW needs type casts for the 'buf' and 'optval' arguments. */
235 #define qemu_getsockopt(sockfd, level, optname, optval, optlen) \
236 getsockopt(sockfd, level, optname, (void *)optval, optlen)
237 #define qemu_setsockopt(sockfd, level, optname, optval, optlen) \
238 setsockopt(sockfd, level, optname, (const void *)optval, optlen)
239 #define qemu_recv(sockfd, buf, len, flags) recv(sockfd, (void *)buf, len, flags)
240 #define qemu_sendto(sockfd, buf, len, flags, destaddr, addrlen) \
241 sendto(sockfd, (const void *)buf, len, flags, destaddr, addrlen)
242 #else
243 #define qemu_getsockopt(sockfd, level, optname, optval, optlen) \
244 getsockopt(sockfd, level, optname, optval, optlen)
245 #define qemu_setsockopt(sockfd, level, optname, optval, optlen) \
246 setsockopt(sockfd, level, optname, optval, optlen)
247 #define qemu_recv(sockfd, buf, len, flags) recv(sockfd, buf, len, flags)
248 #define qemu_sendto(sockfd, buf, len, flags, destaddr, addrlen) \
249 sendto(sockfd, buf, len, flags, destaddr, addrlen)
250 #endif
252 /* Error handling. */
254 void QEMU_NORETURN hw_error(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
256 struct ParallelIOArg {
257 void *buffer;
258 int count;
261 typedef int (*DMA_transfer_handler) (void *opaque, int nchan, int pos, int size);
263 /* A load of opaque types so that device init declarations don't have to
264 pull in all the real definitions. */
265 typedef struct NICInfo NICInfo;
266 typedef struct HCIInfo HCIInfo;
267 typedef struct AudioState AudioState;
268 typedef struct BlockDriverState BlockDriverState;
269 typedef struct DriveInfo DriveInfo;
270 typedef struct DisplayState DisplayState;
271 typedef struct DisplayChangeListener DisplayChangeListener;
272 typedef struct DisplaySurface DisplaySurface;
273 typedef struct PixelFormat PixelFormat;
274 typedef struct QemuConsole QemuConsole;
275 typedef struct CharDriverState CharDriverState;
276 typedef struct MACAddr MACAddr;
277 typedef struct NetClientState NetClientState;
278 typedef struct i2c_bus i2c_bus;
279 typedef struct ISABus ISABus;
280 typedef struct ISADevice ISADevice;
281 typedef struct SMBusDevice SMBusDevice;
282 typedef struct PCIHostState PCIHostState;
283 typedef struct PCIExpressHost PCIExpressHost;
284 typedef struct PCIBus PCIBus;
285 typedef struct PCIDevice PCIDevice;
286 typedef struct PCIExpressDevice PCIExpressDevice;
287 typedef struct PCIBridge PCIBridge;
288 typedef struct PCIEAERMsg PCIEAERMsg;
289 typedef struct PCIEAERLog PCIEAERLog;
290 typedef struct PCIEAERErr PCIEAERErr;
291 typedef struct PCIEPort PCIEPort;
292 typedef struct PCIESlot PCIESlot;
293 typedef struct MSIMessage MSIMessage;
294 typedef struct SerialState SerialState;
295 typedef struct PCMCIACardState PCMCIACardState;
296 typedef struct MouseTransformInfo MouseTransformInfo;
297 typedef struct uWireSlave uWireSlave;
298 typedef struct I2SCodec I2SCodec;
299 typedef struct SSIBus SSIBus;
300 typedef struct EventNotifier EventNotifier;
301 typedef struct VirtIODevice VirtIODevice;
302 typedef struct QEMUSGList QEMUSGList;
303 typedef struct SHPCDevice SHPCDevice;
305 typedef uint64_t pcibus_t;
307 typedef enum LostTickPolicy {
308 LOST_TICK_DISCARD,
309 LOST_TICK_DELAY,
310 LOST_TICK_MERGE,
311 LOST_TICK_SLEW,
312 LOST_TICK_MAX
313 } LostTickPolicy;
315 typedef struct PCIHostDeviceAddress {
316 unsigned int domain;
317 unsigned int bus;
318 unsigned int slot;
319 unsigned int function;
320 } PCIHostDeviceAddress;
322 void tcg_exec_init(unsigned long tb_size);
323 bool tcg_enabled(void);
325 void cpu_exec_init_all(void);
327 /* CPU save/load. */
328 void cpu_save(QEMUFile *f, void *opaque);
329 int cpu_load(QEMUFile *f, void *opaque, int version_id);
331 /* Unblock cpu */
332 void qemu_cpu_kick_self(void);
334 /* work queue */
335 struct qemu_work_item {
336 struct qemu_work_item *next;
337 void (*func)(void *data);
338 void *data;
339 int done;
342 #ifdef CONFIG_USER_ONLY
343 #define qemu_init_vcpu(env) do { } while (0)
344 #else
345 void qemu_init_vcpu(void *env);
346 #endif
350 * Sends a (part of) iovec down a socket, yielding when the socket is full, or
351 * Receives data into a (part of) iovec from a socket,
352 * yielding when there is no data in the socket.
353 * The same interface as qemu_sendv_recvv(), with added yielding.
354 * XXX should mark these as coroutine_fn
356 ssize_t qemu_co_sendv_recvv(int sockfd, struct iovec *iov, unsigned iov_cnt,
357 size_t offset, size_t bytes, bool do_send);
358 #define qemu_co_recvv(sockfd, iov, iov_cnt, offset, bytes) \
359 qemu_co_sendv_recvv(sockfd, iov, iov_cnt, offset, bytes, false)
360 #define qemu_co_sendv(sockfd, iov, iov_cnt, offset, bytes) \
361 qemu_co_sendv_recvv(sockfd, iov, iov_cnt, offset, bytes, true)
364 * The same as above, but with just a single buffer
366 ssize_t qemu_co_send_recv(int sockfd, void *buf, size_t bytes, bool do_send);
367 #define qemu_co_recv(sockfd, buf, bytes) \
368 qemu_co_send_recv(sockfd, buf, bytes, false)
369 #define qemu_co_send(sockfd, buf, bytes) \
370 qemu_co_send_recv(sockfd, buf, bytes, true)
372 typedef struct QEMUIOVector {
373 struct iovec *iov;
374 int niov;
375 int nalloc;
376 size_t size;
377 } QEMUIOVector;
379 void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint);
380 void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov);
381 void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len);
382 void qemu_iovec_concat(QEMUIOVector *dst,
383 QEMUIOVector *src, size_t soffset, size_t sbytes);
384 void qemu_iovec_destroy(QEMUIOVector *qiov);
385 void qemu_iovec_reset(QEMUIOVector *qiov);
386 size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset,
387 void *buf, size_t bytes);
388 size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset,
389 const void *buf, size_t bytes);
390 size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
391 int fillc, size_t bytes);
393 bool buffer_is_zero(const void *buf, size_t len);
395 void qemu_progress_init(int enabled, float min_skip);
396 void qemu_progress_end(void);
397 void qemu_progress_print(float delta, int max);
398 const char *qemu_get_vm_name(void);
400 #define QEMU_FILE_TYPE_BIOS 0
401 #define QEMU_FILE_TYPE_KEYMAP 1
402 char *qemu_find_file(int type, const char *name);
404 /* OS specific functions */
405 void os_setup_early_signal_handling(void);
406 char *os_find_datadir(const char *argv0);
407 void os_parse_cmd_args(int index, const char *optarg);
408 void os_pidfile_error(void);
410 /* Convert a byte between binary and BCD. */
411 static inline uint8_t to_bcd(uint8_t val)
413 return ((val / 10) << 4) | (val % 10);
416 static inline uint8_t from_bcd(uint8_t val)
418 return ((val >> 4) * 10) + (val & 0x0f);
421 /* compute with 96 bit intermediate result: (a*b)/c */
422 static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
424 union {
425 uint64_t ll;
426 struct {
427 #ifdef HOST_WORDS_BIGENDIAN
428 uint32_t high, low;
429 #else
430 uint32_t low, high;
431 #endif
432 } l;
433 } u, res;
434 uint64_t rl, rh;
436 u.ll = a;
437 rl = (uint64_t)u.l.low * (uint64_t)b;
438 rh = (uint64_t)u.l.high * (uint64_t)b;
439 rh += (rl >> 32);
440 res.l.high = rh / c;
441 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
442 return res.ll;
445 /* Round number down to multiple */
446 #define QEMU_ALIGN_DOWN(n, m) ((n) / (m) * (m))
448 /* Round number up to multiple */
449 #define QEMU_ALIGN_UP(n, m) QEMU_ALIGN_DOWN((n) + (m) - 1, (m))
451 static inline bool is_power_of_2(uint64_t value)
453 if (!value) {
454 return 0;
457 return !(value & (value - 1));
460 /* round down to the nearest power of 2*/
461 int64_t pow2floor(int64_t value);
463 #include "module.h"
466 * Implementation of ULEB128 (http://en.wikipedia.org/wiki/LEB128)
467 * Input is limited to 14-bit numbers
470 int uleb128_encode_small(uint8_t *out, uint32_t n);
471 int uleb128_decode_small(const uint8_t *in, uint32_t *n);
473 #endif