1/* Common header file that is included by all of qemu.  */
2#ifndef QEMU_COMMON_H
3#define QEMU_COMMON_H
4
5#include "config-host.h"
6
7#define QEMU_NORETURN __attribute__ ((__noreturn__))
8#ifdef CONFIG_GCC_ATTRIBUTE_WARN_UNUSED_RESULT
9#define QEMU_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
10#else
11#define QEMU_WARN_UNUSED_RESULT
12#endif
13
14#define QEMU_BUILD_BUG_ON(x) typedef char __build_bug_on__##__LINE__[(x)?-1:1];
15
16typedef struct QEMUTimer QEMUTimer;
17typedef struct QEMUFile QEMUFile;
18typedef struct QEMUBH QEMUBH;
19typedef struct DeviceState DeviceState;
20
21struct Monitor;
22typedef struct Monitor Monitor;
23
24/* we put basic includes here to avoid repeating them in device drivers */
25#include <stdlib.h>
26#include <stdio.h>
27#include <stdarg.h>
28#include <stdbool.h>
29#include <string.h>
30#include <strings.h>
31#include <inttypes.h>
32#include <limits.h>
33#include <time.h>
34#include <ctype.h>
35#include <errno.h>
36#include <unistd.h>
37#include <fcntl.h>
38#include <sys/stat.h>
39#include <sys/time.h>
40#include <assert.h>
41
42#ifdef _WIN32
43#include "qemu-os-win32.h"
44#endif
45
46#ifdef CONFIG_POSIX
47#include "qemu-os-posix.h"
48#endif
49
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#ifndef TIME_MAX
66#define TIME_MAX LONG_MAX
67#endif
68
69#ifndef CONFIG_IOVEC
70#define CONFIG_IOVEC
71struct iovec {
72    void *iov_base;
73    size_t iov_len;
74};
75/*
76 * Use the same value as Linux for now.
77 */
78#define IOV_MAX		1024
79#else
80#include <sys/uio.h>
81#endif
82
83#if defined __GNUC__
84# if (__GNUC__ < 4) || \
85     defined(__GNUC_MINOR__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 4)
86   /* gcc versions before 4.4.x don't support gnu_printf, so use printf. */
87#  define GCC_ATTR __attribute__((__unused__, format(printf, 1, 2)))
88#  define GCC_FMT_ATTR(n, m) __attribute__((format(printf, n, m)))
89# else
90   /* Use gnu_printf when supported (qemu uses standard format strings). */
91#  define GCC_ATTR __attribute__((__unused__, format(gnu_printf, 1, 2)))
92#  define GCC_FMT_ATTR(n, m) __attribute__((format(gnu_printf, n, m)))
93# endif
94#else
95#define GCC_ATTR /**/
96#define GCC_FMT_ATTR(n, m)
97#endif
98
99typedef int (*fprintf_function)(FILE *f, const char *fmt, ...)
100    GCC_FMT_ATTR(2, 3);
101
102#ifdef _WIN32
103#define fsync _commit
104#define lseek _lseeki64
105int qemu_ftruncate64(int, int64_t);
106#define ftruncate qemu_ftruncate64
107
108static inline char *realpath(const char *path, char *resolved_path)
109{
110    _fullpath(resolved_path, path, _MAX_PATH);
111    return resolved_path;
112}
113
114#define PRId64 "I64d"
115#define PRIx64 "I64x"
116#define PRIu64 "I64u"
117#define PRIo64 "I64o"
118#endif
119
120/* FIXME: Remove NEED_CPU_H.  */
121#ifndef NEED_CPU_H
122
123#include <setjmp.h>
124#include "osdep.h"
125#include "bswap.h"
126
127#else
128
129#include "cpu.h"
130
131#endif /* !defined(NEED_CPU_H) */
132
133/* bottom halves */
134typedef void QEMUBHFunc(void *opaque);
135
136void async_context_push(void);
137void async_context_pop(void);
138int get_async_context_id(void);
139
140QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque);
141void qemu_bh_schedule(QEMUBH *bh);
142/* Bottom halfs that are scheduled from a bottom half handler are instantly
143 * invoked.  This can create an infinite loop if a bottom half handler
144 * schedules itself.  qemu_bh_schedule_idle() avoids this infinite loop by
145 * ensuring that the bottom half isn't executed until the next main loop
146 * iteration.
147 */
148void qemu_bh_schedule_idle(QEMUBH *bh);
149void qemu_bh_cancel(QEMUBH *bh);
150void qemu_bh_delete(QEMUBH *bh);
151int qemu_bh_poll(void);
152void qemu_bh_update_timeout(int *timeout);
153
154void qemu_get_timedate(struct tm *tm, int offset);
155int qemu_timedate_diff(struct tm *tm);
156
157/* cutils.c */
158void pstrcpy(char *buf, int buf_size, const char *str);
159char *pstrcat(char *buf, int buf_size, const char *s);
160int strstart(const char *str, const char *val, const char **ptr);
161int stristart(const char *str, const char *val, const char **ptr);
162int qemu_strnlen(const char *s, int max_len);
163time_t mktimegm(struct tm *tm);
164int qemu_fls(int i);
165int qemu_fdatasync(int fd);
166int fcntl_setfl(int fd, int flag);
167
168/*
169 * strtosz() suffixes used to specify the default treatment of an
170 * argument passed to strtosz() without an explicit suffix.
171 * These should be defined using upper case characters in the range
172 * A-Z, as strtosz() will use qemu_toupper() on the given argument
173 * prior to comparison.
174 */
175#define STRTOSZ_DEFSUFFIX_TB	'T'
176#define STRTOSZ_DEFSUFFIX_GB	'G'
177#define STRTOSZ_DEFSUFFIX_MB	'M'
178#define STRTOSZ_DEFSUFFIX_KB	'K'
179#define STRTOSZ_DEFSUFFIX_B	'B'
180int64_t strtosz(const char *nptr, char **end);
181int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix);
182
183/* path.c */
184void init_paths(const char *prefix);
185const char *path(const char *pathname);
186
187#define qemu_isalnum(c)		isalnum((unsigned char)(c))
188#define qemu_isalpha(c)		isalpha((unsigned char)(c))
189#define qemu_iscntrl(c)		iscntrl((unsigned char)(c))
190#define qemu_isdigit(c)		isdigit((unsigned char)(c))
191#define qemu_isgraph(c)		isgraph((unsigned char)(c))
192#define qemu_islower(c)		islower((unsigned char)(c))
193#define qemu_isprint(c)		isprint((unsigned char)(c))
194#define qemu_ispunct(c)		ispunct((unsigned char)(c))
195#define qemu_isspace(c)		isspace((unsigned char)(c))
196#define qemu_isupper(c)		isupper((unsigned char)(c))
197#define qemu_isxdigit(c)	isxdigit((unsigned char)(c))
198#define qemu_tolower(c)		tolower((unsigned char)(c))
199#define qemu_toupper(c)		toupper((unsigned char)(c))
200#define qemu_isascii(c)		isascii((unsigned char)(c))
201#define qemu_toascii(c)		toascii((unsigned char)(c))
202
203#ifdef _WIN32
204/* ffs() in oslib-win32.c for WIN32, strings.h for the rest of the world */
205int ffs(int i);
206#endif
207
208void *qemu_oom_check(void *ptr);
209void *qemu_malloc(size_t size);
210void *qemu_realloc(void *ptr, size_t size);
211void *qemu_mallocz(size_t size);
212void qemu_free(void *ptr);
213char *qemu_strdup(const char *str);
214char *qemu_strndup(const char *str, size_t size);
215
216void qemu_mutex_lock_iothread(void);
217void qemu_mutex_unlock_iothread(void);
218
219int qemu_open(const char *name, int flags, ...);
220ssize_t qemu_write_full(int fd, const void *buf, size_t count)
221    QEMU_WARN_UNUSED_RESULT;
222void qemu_set_cloexec(int fd);
223
224#ifndef _WIN32
225int qemu_add_child_watch(pid_t pid);
226int qemu_eventfd(int pipefd[2]);
227int qemu_pipe(int pipefd[2]);
228#endif
229
230void *get_mmap_addr(unsigned long size);
231
232
233/* Error handling.  */
234
235void QEMU_NORETURN hw_error(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
236
237/* IO callbacks.  */
238typedef void IOReadHandler(void *opaque, const uint8_t *buf, int size);
239typedef int IOCanReadHandler(void *opaque);
240typedef void IOHandler(void *opaque);
241
242void qemu_iohandler_fill(int *pnfds, fd_set *readfds, fd_set *writefds, fd_set *xfds);
243void qemu_iohandler_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds, int rc);
244
245struct ParallelIOArg {
246    void *buffer;
247    int count;
248};
249
250typedef int (*DMA_transfer_handler) (void *opaque, int nchan, int pos, int size);
251
252/* A load of opaque types so that device init declarations don't have to
253   pull in all the real definitions.  */
254typedef struct NICInfo NICInfo;
255typedef struct HCIInfo HCIInfo;
256typedef struct AudioState AudioState;
257typedef struct BlockDriverState BlockDriverState;
258typedef struct DriveInfo DriveInfo;
259typedef struct DisplayState DisplayState;
260typedef struct DisplayChangeListener DisplayChangeListener;
261typedef struct DisplaySurface DisplaySurface;
262typedef struct DisplayAllocator DisplayAllocator;
263typedef struct PixelFormat PixelFormat;
264typedef struct TextConsole TextConsole;
265typedef TextConsole QEMUConsole;
266typedef struct CharDriverState CharDriverState;
267typedef struct MACAddr MACAddr;
268typedef struct VLANState VLANState;
269typedef struct VLANClientState VLANClientState;
270typedef struct i2c_bus i2c_bus;
271typedef struct i2c_slave i2c_slave;
272typedef struct SMBusDevice SMBusDevice;
273typedef struct PCIHostState PCIHostState;
274typedef struct PCIExpressHost PCIExpressHost;
275typedef struct PCIBus PCIBus;
276typedef struct PCIDevice PCIDevice;
277typedef struct SerialState SerialState;
278typedef struct IRQState *qemu_irq;
279typedef struct PCMCIACardState PCMCIACardState;
280typedef struct MouseTransformInfo MouseTransformInfo;
281typedef struct uWireSlave uWireSlave;
282typedef struct I2SCodec I2SCodec;
283typedef struct SSIBus SSIBus;
284typedef struct EventNotifier EventNotifier;
285typedef struct VirtIODevice VirtIODevice;
286
287typedef uint64_t pcibus_t;
288
289typedef enum {
290    IF_NONE,
291    IF_IDE, IF_SCSI, IF_FLOPPY, IF_PFLASH, IF_MTD, IF_SD, IF_VIRTIO, IF_XEN,
292    IF_COUNT
293} BlockInterfaceType;
294
295void cpu_exec_init_all(unsigned long tb_size);
296
297/* CPU save/load.  */
298void cpu_save(QEMUFile *f, void *opaque);
299int cpu_load(QEMUFile *f, void *opaque, int version_id);
300
301/* Force QEMU to stop what it's doing and service IO */
302void qemu_service_io(void);
303
304/* Force QEMU to process pending events */
305void qemu_notify_event(void);
306
307/* Unblock cpu */
308void qemu_cpu_kick(void *env);
309int qemu_cpu_self(void *env);
310
311/* work queue */
312struct qemu_work_item {
313    struct qemu_work_item *next;
314    void (*func)(void *data);
315    void *data;
316    int done;
317};
318
319#ifdef CONFIG_USER_ONLY
320#define qemu_init_vcpu(env) do { } while (0)
321#else
322void qemu_init_vcpu(void *env);
323#endif
324
325typedef struct QEMUIOVector {
326    struct iovec *iov;
327    int niov;
328    int nalloc;
329    size_t size;
330} QEMUIOVector;
331
332void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint);
333void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov);
334void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len);
335void qemu_iovec_copy(QEMUIOVector *dst, QEMUIOVector *src, uint64_t skip,
336    size_t size);
337void qemu_iovec_concat(QEMUIOVector *dst, QEMUIOVector *src, size_t size);
338void qemu_iovec_destroy(QEMUIOVector *qiov);
339void qemu_iovec_reset(QEMUIOVector *qiov);
340void qemu_iovec_to_buffer(QEMUIOVector *qiov, void *buf);
341void qemu_iovec_from_buffer(QEMUIOVector *qiov, const void *buf, size_t count);
342void qemu_iovec_memset(QEMUIOVector *qiov, int c, size_t count);
343void qemu_iovec_memset_skip(QEMUIOVector *qiov, int c, size_t count,
344                            size_t skip);
345
346/* OS specific functions */
347void os_setup_early_signal_handling(void);
348char *os_find_datadir(const char *argv0);
349void os_parse_cmd_args(int index, const char *optarg);
350void os_pidfile_error(void);
351
352/* Convert a byte between binary and BCD.  */
353static inline uint8_t to_bcd(uint8_t val)
354{
355    return ((val / 10) << 4) | (val % 10);
356}
357
358static inline uint8_t from_bcd(uint8_t val)
359{
360    return ((val >> 4) * 10) + (val & 0x0f);
361}
362
363/* compute with 96 bit intermediate result: (a*b)/c */
364static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
365{
366    union {
367        uint64_t ll;
368        struct {
369#ifdef HOST_WORDS_BIGENDIAN
370            uint32_t high, low;
371#else
372            uint32_t low, high;
373#endif
374        } l;
375    } u, res;
376    uint64_t rl, rh;
377
378    u.ll = a;
379    rl = (uint64_t)u.l.low * (uint64_t)b;
380    rh = (uint64_t)u.l.high * (uint64_t)b;
381    rh += (rl >> 32);
382    res.l.high = rh / c;
383    res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
384    return res.ll;
385}
386
387#include "module.h"
388
389typedef enum DisplayType
390{
391    DT_DEFAULT,
392    DT_CURSES,
393    DT_SDL,
394    DT_VNC,
395    DT_NOGRAPHIC,
396} DisplayType;
397
398/*
399 * A fixer for timeout value passed to select() on Mac. The issue is that Mac's
400 * version of select() will return EINVAL on timeouts larger than 100000000
401 * seconds, even though it should have just clamped it. So, for Mac we should
402 * make sure that timeout value is bound to 100000000 seconds before passing it
403 * to select().
404 */
405#if _DARWIN_C_SOURCE
406#define CLAMP_MAC_TIMEOUT(to) do { if (to > 100000000000LL) to = 100000000000LL; } while (0)
407#else
408#define CLAMP_MAC_TIMEOUT(to) ((void)0)
409#endif  // _DARWIN_C_SOURCE
410
411#if defined(__clang__) || defined(__llvm__)
412/* Clang and llvm-gcc don't support global register variable (GRV).
413   Clang issues compile-time error for GRV.  llvm-gcc accepts GRV (because
414   its front-end is gcc) but ignores it in the llvm-based back-end.
415   Undefining GRV decl to allow external/qemu and the rest of Android
416   to compile.  But emulator built w/o GRV support will not function
417   correctly.  User will be greeted with an error message (issued
418   in tcg/tcg.c) when emulator built this way is launched.
419 */
420#define SUPPORT_GLOBAL_REGISTER_VARIABLE 0
421#define GLOBAL_REGISTER_VARIABLE_DECL
422#else
423#define SUPPORT_GLOBAL_REGISTER_VARIABLE 1
424#define GLOBAL_REGISTER_VARIABLE_DECL register
425#endif /* __clang__ || __llvm__ */
426
427#endif
428