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
16 typedef struct QEMUTimer QEMUTimer;
17 typedef struct QEMUFile QEMUFile;
18 typedef struct QEMUBH QEMUBH;
19 typedef struct DeviceState DeviceState;
20
21 struct Monitor;
22 typedef 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
71 struct 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
99 typedef 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
105 int qemu_ftruncate64(int, int64_t);
106 #define ftruncate qemu_ftruncate64
107
realpath(const char * path,char * resolved_path)108 static 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 */
134 typedef void QEMUBHFunc(void *opaque);
135
136 void async_context_push(void);
137 void async_context_pop(void);
138 int get_async_context_id(void);
139
140 QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque);
141 void 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 */
148 void qemu_bh_schedule_idle(QEMUBH *bh);
149 void qemu_bh_cancel(QEMUBH *bh);
150 void qemu_bh_delete(QEMUBH *bh);
151 int qemu_bh_poll(void);
152 void qemu_bh_update_timeout(int *timeout);
153
154 void qemu_get_timedate(struct tm *tm, int offset);
155 int qemu_timedate_diff(struct tm *tm);
156
157 /* cutils.c */
158 void pstrcpy(char *buf, int buf_size, const char *str);
159 char *pstrcat(char *buf, int buf_size, const char *s);
160 int strstart(const char *str, const char *val, const char **ptr);
161 int stristart(const char *str, const char *val, const char **ptr);
162 int qemu_strnlen(const char *s, int max_len);
163 time_t mktimegm(struct tm *tm);
164 int qemu_fls(int i);
165 int qemu_fdatasync(int fd);
166 int 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'
180 int64_t strtosz(const char *nptr, char **end);
181 int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix);
182
183 /* path.c */
184 void init_paths(const char *prefix);
185 const 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 */
205 int ffs(int i);
206 #endif
207
208 void *qemu_oom_check(void *ptr);
209 void *qemu_malloc(size_t size);
210 void *qemu_realloc(void *ptr, size_t size);
211 void *qemu_mallocz(size_t size);
212 void qemu_free(void *ptr);
213 char *qemu_strdup(const char *str);
214 char *qemu_strndup(const char *str, size_t size);
215
216 void qemu_mutex_lock_iothread(void);
217 void qemu_mutex_unlock_iothread(void);
218
219 int qemu_open(const char *name, int flags, ...);
220 ssize_t qemu_write_full(int fd, const void *buf, size_t count)
221 QEMU_WARN_UNUSED_RESULT;
222 void qemu_set_cloexec(int fd);
223
224 #ifndef _WIN32
225 int qemu_add_child_watch(pid_t pid);
226 int qemu_eventfd(int pipefd[2]);
227 int qemu_pipe(int pipefd[2]);
228 #endif
229
230 void *get_mmap_addr(unsigned long size);
231
232
233 /* Error handling. */
234
235 void QEMU_NORETURN hw_error(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
236
237 /* IO callbacks. */
238 typedef void IOReadHandler(void *opaque, const uint8_t *buf, int size);
239 typedef int IOCanReadHandler(void *opaque);
240 typedef void IOHandler(void *opaque);
241
242 void qemu_iohandler_fill(int *pnfds, fd_set *readfds, fd_set *writefds, fd_set *xfds);
243 void qemu_iohandler_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds, int rc);
244
245 struct ParallelIOArg {
246 void *buffer;
247 int count;
248 };
249
250 typedef 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. */
254 typedef struct NICInfo NICInfo;
255 typedef struct HCIInfo HCIInfo;
256 typedef struct AudioState AudioState;
257 typedef struct BlockDriverState BlockDriverState;
258 typedef struct DriveInfo DriveInfo;
259 typedef struct DisplayState DisplayState;
260 typedef struct DisplayChangeListener DisplayChangeListener;
261 typedef struct DisplaySurface DisplaySurface;
262 typedef struct DisplayAllocator DisplayAllocator;
263 typedef struct PixelFormat PixelFormat;
264 typedef struct TextConsole TextConsole;
265 typedef TextConsole QEMUConsole;
266 typedef struct CharDriverState CharDriverState;
267 typedef struct MACAddr MACAddr;
268 typedef struct VLANState VLANState;
269 typedef struct VLANClientState VLANClientState;
270 typedef struct i2c_bus i2c_bus;
271 typedef struct i2c_slave i2c_slave;
272 typedef struct SMBusDevice SMBusDevice;
273 typedef struct PCIHostState PCIHostState;
274 typedef struct PCIExpressHost PCIExpressHost;
275 typedef struct PCIBus PCIBus;
276 typedef struct PCIDevice PCIDevice;
277 typedef struct SerialState SerialState;
278 typedef struct IRQState *qemu_irq;
279 typedef struct PCMCIACardState PCMCIACardState;
280 typedef struct MouseTransformInfo MouseTransformInfo;
281 typedef struct uWireSlave uWireSlave;
282 typedef struct I2SCodec I2SCodec;
283 typedef struct SSIBus SSIBus;
284 typedef struct EventNotifier EventNotifier;
285 typedef struct VirtIODevice VirtIODevice;
286
287 typedef uint64_t pcibus_t;
288
289 typedef 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
295 void cpu_exec_init_all(unsigned long tb_size);
296
297 /* CPU save/load. */
298 void cpu_save(QEMUFile *f, void *opaque);
299 int cpu_load(QEMUFile *f, void *opaque, int version_id);
300
301 /* Force QEMU to stop what it's doing and service IO */
302 void qemu_service_io(void);
303
304 /* Force QEMU to process pending events */
305 void qemu_notify_event(void);
306
307 /* Unblock cpu */
308 void qemu_cpu_kick(void *env);
309 int qemu_cpu_self(void *env);
310
311 /* work queue */
312 struct 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
322 void qemu_init_vcpu(void *env);
323 #endif
324
325 typedef struct QEMUIOVector {
326 struct iovec *iov;
327 int niov;
328 int nalloc;
329 size_t size;
330 } QEMUIOVector;
331
332 void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint);
333 void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov);
334 void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len);
335 void qemu_iovec_copy(QEMUIOVector *dst, QEMUIOVector *src, uint64_t skip,
336 size_t size);
337 void qemu_iovec_concat(QEMUIOVector *dst, QEMUIOVector *src, size_t size);
338 void qemu_iovec_destroy(QEMUIOVector *qiov);
339 void qemu_iovec_reset(QEMUIOVector *qiov);
340 void qemu_iovec_to_buffer(QEMUIOVector *qiov, void *buf);
341 void qemu_iovec_from_buffer(QEMUIOVector *qiov, const void *buf, size_t count);
342 void qemu_iovec_memset(QEMUIOVector *qiov, int c, size_t count);
343 void qemu_iovec_memset_skip(QEMUIOVector *qiov, int c, size_t count,
344 size_t skip);
345
346 /* OS specific functions */
347 void os_setup_early_signal_handling(void);
348 char *os_find_datadir(const char *argv0);
349 void os_parse_cmd_args(int index, const char *optarg);
350 void os_pidfile_error(void);
351
352 /* Convert a byte between binary and BCD. */
to_bcd(uint8_t val)353 static inline uint8_t to_bcd(uint8_t val)
354 {
355 return ((val / 10) << 4) | (val % 10);
356 }
357
from_bcd(uint8_t val)358 static 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 */
muldiv64(uint64_t a,uint32_t b,uint32_t c)364 static 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
389 typedef 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