1
2 /* Common header file that is included by all of QEMU.
3 *
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.
7 *
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.
11 */
12 #ifndef QEMU_COMMON_H
13 #define QEMU_COMMON_H
14
15 #include <inttypes.h>
16 #include <setjmp.h>
17
18 #include "qemu/compiler.h"
19 #include "config-host.h"
20 #include "qemu/typedefs.h"
21 #include "qemu/osdep.h"
22 #include "qemu/bswap.h"
23
24
25 #if defined(__arm__) || defined(__sparc__) || defined(__mips__) || defined(__hppa__) || defined(__ia64__)
26 #define WORDS_ALIGNED
27 #endif
28
29 #define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
30
31 /* we put basic includes here to avoid repeating them in device drivers */
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <stdarg.h>
35 #include <stdbool.h>
36 #include <string.h>
37 #include <strings.h>
38 #include <inttypes.h>
39 #include <limits.h>
40 #include <time.h>
41 #include <ctype.h>
42 #include <errno.h>
43 #include <unistd.h>
44 #include <fcntl.h>
45 #include <sys/stat.h>
46 #include <sys/time.h>
47 #include <assert.h>
48 #include <signal.h>
49 #include <glib.h>
50
51 #ifdef _WIN32
52 #include "sysemu/os-win32.h"
53 #endif
54
55 #ifdef CONFIG_POSIX
56 #include "sysemu/os-posix.h"
57 #endif
58
59 #ifndef O_LARGEFILE
60 #define O_LARGEFILE 0
61 #endif
62 #ifndef O_BINARY
63 #define O_BINARY 0
64 #endif
65 #ifndef MAP_ANONYMOUS
66 #define MAP_ANONYMOUS MAP_ANON
67 #endif
68 #ifndef ENOMEDIUM
69 #define ENOMEDIUM ENODEV
70 #endif
71 #if !defined(ENOTSUP)
72 #define ENOTSUP 4096
73 #endif
74 #if !defined(ECANCELED)
75 #define ECANCELED 4097
76 #endif
77 #if !defined(EMEDIUMTYPE)
78 #define EMEDIUMTYPE 4098
79 #endif
80 #ifndef TIME_MAX
81 #define TIME_MAX LONG_MAX
82 #endif
83
84 /* HOST_LONG_BITS is the size of a native pointer in bits. */
85 #if UINTPTR_MAX == UINT32_MAX
86 # define HOST_LONG_BITS 32
87 #elif UINTPTR_MAX == UINT64_MAX
88 # define HOST_LONG_BITS 64
89 #else
90 # error Unknown pointer size
91 #endif
92
93 typedef int (*fprintf_function)(FILE *f, const char *fmt, ...)
94 GCC_FMT_ATTR(2, 3);
95
96 #ifdef _WIN32
97 #define fsync _commit
98 #if !defined(lseek)
99 # define lseek _lseeki64
100 #endif
101 int qemu_ftruncate64(int, int64_t);
102 #if !defined(ftruncate)
103 # define ftruncate qemu_ftruncate64
104 #endif
105
realpath(const char * path,char * resolved_path)106 static inline char *realpath(const char *path, char *resolved_path)
107 {
108 _fullpath(resolved_path, path, _MAX_PATH);
109 return resolved_path;
110 }
111 #endif /* _WIN32 */
112
113 /* bottom halves */
114 typedef void QEMUBHFunc(void *opaque);
115
116 void async_context_push(void);
117 void async_context_pop(void);
118 int get_async_context_id(void);
119
120 QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque);
121 void qemu_bh_schedule(QEMUBH *bh);
122 /* Bottom halfs that are scheduled from a bottom half handler are instantly
123 * invoked. This can create an infinite loop if a bottom half handler
124 * schedules itself. qemu_bh_schedule_idle() avoids this infinite loop by
125 * ensuring that the bottom half isn't executed until the next main loop
126 * iteration.
127 */
128 void qemu_bh_schedule_idle(QEMUBH *bh);
129 void qemu_bh_cancel(QEMUBH *bh);
130 void qemu_bh_delete(QEMUBH *bh);
131 int qemu_bh_poll(void);
132 void qemu_bh_update_timeout(int *timeout);
133
134 void qemu_get_timedate(struct tm *tm, int offset);
135 int qemu_timedate_diff(struct tm *tm);
136
137 /**
138 * is_help_option:
139 * @s: string to test
140 *
141 * Check whether @s is one of the standard strings which indicate
142 * that the user is asking for a list of the valid values for a
143 * command option like -cpu or -M. The current accepted strings
144 * are 'help' and '?'. '?' is deprecated (it is a shell wildcard
145 * which makes it annoying to use in a reliable way) but provided
146 * for backwards compatibility.
147 *
148 * Returns: true if @s is a request for a list.
149 */
is_help_option(const char * s)150 static inline bool is_help_option(const char *s)
151 {
152 return !strcmp(s, "?") || !strcmp(s, "help");
153 }
154
155 /* cutils.c */
156 void pstrcpy(char *buf, int buf_size, const char *str);
157 void strpadcpy(char *buf, int buf_size, const char *str, char pad);
158 char *pstrcat(char *buf, int buf_size, const char *s);
159 int strstart(const char *str, const char *val, const char **ptr);
160 int stristart(const char *str, const char *val, const char **ptr);
161 int qemu_strnlen(const char *s, int max_len);
162 char *qemu_strsep(char **input, const char *delim);
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 int qemu_parse_fd(const char *param);
168
169 int parse_uint(const char *s, unsigned long long *value, char **endptr,
170 int base);
171 int parse_uint_full(const char *s, unsigned long long *value, int base);
172
173 /*
174 * strtosz() suffixes used to specify the default treatment of an
175 * argument passed to strtosz() without an explicit suffix.
176 * These should be defined using upper case characters in the range
177 * A-Z, as strtosz() will use qemu_toupper() on the given argument
178 * prior to comparison.
179 */
180 #define STRTOSZ_DEFSUFFIX_EB 'E'
181 #define STRTOSZ_DEFSUFFIX_PB 'P'
182 #define STRTOSZ_DEFSUFFIX_TB 'T'
183 #define STRTOSZ_DEFSUFFIX_GB 'G'
184 #define STRTOSZ_DEFSUFFIX_MB 'M'
185 #define STRTOSZ_DEFSUFFIX_KB 'K'
186 #define STRTOSZ_DEFSUFFIX_B 'B'
187 int64_t strtosz(const char *nptr, char **end);
188 int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix);
189 int64_t strtosz_suffix_unit(const char *nptr, char **end,
190 const char default_suffix, int64_t unit);
191
192 /* used to print char* safely */
193 #define STR_OR_NULL(str) ((str) ? (str) : "null")
194
195 /* path.c */
196 void init_paths(const char *prefix);
197 const char *path(const char *pathname);
198
199 #define qemu_isalnum(c) isalnum((unsigned char)(c))
200 #define qemu_isalpha(c) isalpha((unsigned char)(c))
201 #define qemu_iscntrl(c) iscntrl((unsigned char)(c))
202 #define qemu_isdigit(c) isdigit((unsigned char)(c))
203 #define qemu_isgraph(c) isgraph((unsigned char)(c))
204 #define qemu_islower(c) islower((unsigned char)(c))
205 #define qemu_isprint(c) isprint((unsigned char)(c))
206 #define qemu_ispunct(c) ispunct((unsigned char)(c))
207 #define qemu_isspace(c) isspace((unsigned char)(c))
208 #define qemu_isupper(c) isupper((unsigned char)(c))
209 #define qemu_isxdigit(c) isxdigit((unsigned char)(c))
210 #define qemu_tolower(c) tolower((unsigned char)(c))
211 #define qemu_toupper(c) toupper((unsigned char)(c))
212 #define qemu_isascii(c) isascii((unsigned char)(c))
213 #define qemu_toascii(c) toascii((unsigned char)(c))
214
215 void qemu_mutex_lock_iothread(void);
216 void qemu_mutex_unlock_iothread(void);
217
218 int qemu_open(const char *name, int flags, ...);
219 ssize_t qemu_write_full(int fd, const void *buf, size_t count)
220 QEMU_WARN_UNUSED_RESULT;
221 ssize_t qemu_send_full(int fd, const void *buf, size_t count, int flags)
222 QEMU_WARN_UNUSED_RESULT;
223 ssize_t qemu_recv_full(int fd, void *buf, size_t count, int flags)
224 QEMU_WARN_UNUSED_RESULT;
225 void qemu_set_cloexec(int fd);
226
227 #ifndef _WIN32
228 int qemu_add_child_watch(pid_t pid);
229 int qemu_eventfd(int pipefd[2]);
230 int qemu_pipe(int pipefd[2]);
231 #endif
232
233 #ifdef CONFIG_ANDROID
234 int qemu_recv(int sock, void* buf, size_t len, int flags);
235
236 int qemu_getsockopt(int sockfd, int level, int optname,
237 void* optval, size_t* optlen);
238
239 int qemu_setsockopt(int sockfd, int level, int optname,
240 const void* optval, size_t optlen);
241
242 #else // !CONFIG_ANDROID
243 #ifdef _WIN32
244 /* MinGW needs type casts for the 'buf' and 'optval' arguments. */
245 #define qemu_getsockopt(sockfd, level, optname, optval, optlen) \
246 getsockopt(sockfd, level, optname, (void *)optval, optlen)
247 #define qemu_setsockopt(sockfd, level, optname, optval, optlen) \
248 setsockopt(sockfd, level, optname, (const void *)optval, optlen)
249 #define qemu_recv(sockfd, buf, len, flags) recv(sockfd, (void *)buf, len, flags)
250 #define qemu_sendto(sockfd, buf, len, flags, destaddr, addrlen) \
251 sendto(sockfd, (const void *)buf, len, flags, destaddr, addrlen)
252 #else
253 #define qemu_getsockopt(sockfd, level, optname, optval, optlen) \
254 getsockopt(sockfd, level, optname, optval, optlen)
255 #define qemu_setsockopt(sockfd, level, optname, optval, optlen) \
256 setsockopt(sockfd, level, optname, optval, optlen)
257 #define qemu_recv(sockfd, buf, len, flags) recv(sockfd, buf, len, flags)
258 #define qemu_sendto(sockfd, buf, len, flags, destaddr, addrlen) \
259 sendto(sockfd, buf, len, flags, destaddr, addrlen)
260 #endif
261 #endif // !CONFIG_ANDROID
262
263 void *get_mmap_addr(unsigned long size);
264
265
266 /* Error handling. */
267
268 void QEMU_NORETURN hw_error(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
269
270 /* IO callbacks. */
271 typedef void IOReadHandler(void *opaque, const uint8_t *buf, int size);
272 typedef int IOCanReadHandler(void *opaque);
273 typedef void IOHandler(void *opaque);
274
275 void qemu_iohandler_fill(int *pnfds, fd_set *readfds, fd_set *writefds, fd_set *xfds);
276 void qemu_iohandler_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds, int rc);
277
278 struct ParallelIOArg {
279 void *buffer;
280 int count;
281 };
282
283 typedef int (*DMA_transfer_handler) (void *opaque, int nchan, int pos, int size);
284
285 typedef uint64_t pcibus_t;
286
287 typedef enum {
288 IF_NONE,
289 IF_IDE, IF_SCSI, IF_FLOPPY, IF_PFLASH, IF_MTD, IF_SD, IF_VIRTIO, IF_XEN,
290 IF_COUNT
291 } BlockInterfaceType;
292
293 typedef enum LostTickPolicy {
294 LOST_TICK_DISCARD,
295 LOST_TICK_DELAY,
296 LOST_TICK_MERGE,
297 LOST_TICK_SLEW,
298 LOST_TICK_MAX
299 } LostTickPolicy;
300
301 typedef struct PCIHostDeviceAddress {
302 unsigned int domain;
303 unsigned int bus;
304 unsigned int slot;
305 unsigned int function;
306 } PCIHostDeviceAddress;
307
308
309 void cpu_exec_init_all(unsigned long tb_size);
310
311 /* CPU save/load. */
312 void cpu_save(QEMUFile *f, void *opaque);
313 int cpu_load(QEMUFile *f, void *opaque, int version_id);
314
315 /* Force QEMU to process pending events */
316 void qemu_notify_event(void);
317
318 /* work queue */
319 struct qemu_work_item {
320 struct qemu_work_item *next;
321 void (*func)(void *data);
322 void *data;
323 int done;
324 };
325
326 typedef struct QEMUIOVector {
327 struct iovec *iov;
328 int niov;
329 int nalloc;
330 size_t size;
331 } QEMUIOVector;
332
333 void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint);
334 void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov);
335 void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len);
336 void qemu_iovec_concat(QEMUIOVector *dst,
337 QEMUIOVector *src, size_t soffset, size_t sbytes);
338 void qemu_iovec_concat_iov(QEMUIOVector *dst,
339 struct iovec *src_iov, unsigned int src_cnt,
340 size_t soffset, size_t sbytes);
341 void qemu_iovec_destroy(QEMUIOVector *qiov);
342 void qemu_iovec_reset(QEMUIOVector *qiov);
343 size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset,
344 void *buf, size_t bytes);
345 size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset,
346 const void *buf, size_t bytes);
347 size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
348 int fillc, size_t bytes);
349
350 bool buffer_is_zero(const void *buf, size_t len);
351
352 #define QEMU_FILE_TYPE_BIOS 0
353 #define QEMU_FILE_TYPE_KEYMAP 1
354 char *qemu_find_file(int type, const char *name);
355
356 /* OS specific functions */
357 void os_setup_early_signal_handling(void);
358 char *os_find_datadir(const char *argv0);
359 void os_parse_cmd_args(int index, const char *optarg);
360 void os_pidfile_error(void);
361
362 /* Convert a byte between binary and BCD. */
to_bcd(uint8_t val)363 static inline uint8_t to_bcd(uint8_t val)
364 {
365 return ((val / 10) << 4) | (val % 10);
366 }
367
from_bcd(uint8_t val)368 static inline uint8_t from_bcd(uint8_t val)
369 {
370 return ((val >> 4) * 10) + (val & 0x0f);
371 }
372
373 /* compute with 96 bit intermediate result: (a*b)/c */
muldiv64(uint64_t a,uint32_t b,uint32_t c)374 static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
375 {
376 union {
377 uint64_t ll;
378 struct {
379 #ifdef HOST_WORDS_BIGENDIAN
380 uint32_t high, low;
381 #else
382 uint32_t low, high;
383 #endif
384 } l;
385 } u, res;
386 uint64_t rl, rh;
387
388 u.ll = a;
389 rl = (uint64_t)u.l.low * (uint64_t)b;
390 rh = (uint64_t)u.l.high * (uint64_t)b;
391 rh += (rl >> 32);
392 res.l.high = rh / c;
393 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
394 return res.ll;
395 }
396
397 /* Round number down to multiple */
398 #define QEMU_ALIGN_DOWN(n, m) ((n) / (m) * (m))
399
400 /* Round number up to multiple */
401 #define QEMU_ALIGN_UP(n, m) QEMU_ALIGN_DOWN((n) + (m) - 1, (m))
402
is_power_of_2(uint64_t value)403 static inline bool is_power_of_2(uint64_t value)
404 {
405 if (!value) {
406 return 0;
407 }
408
409 return !(value & (value - 1));
410 }
411
412 /* round down to the nearest power of 2*/
413 int64_t pow2floor(int64_t value);
414
415 #include "qemu/module.h"
416
417 /*
418 * Implementation of ULEB128 (http://en.wikipedia.org/wiki/LEB128)
419 * Input is limited to 14-bit numbers
420 */
421
422 int uleb128_encode_small(uint8_t *out, uint32_t n);
423 int uleb128_decode_small(const uint8_t *in, uint32_t *n);
424
425 /* unicode.c */
426 int mod_utf8_codepoint(const char *s, size_t n, char **end);
427
428 /*
429 * Hexdump a buffer to a file. An optional string prefix is added to every line
430 */
431
432 void qemu_hexdump(const char *buf, FILE *fp, const char *prefix, size_t size);
433
434 /*
435 * A fixer for timeout value passed to select() on Mac. The issue is that Mac's
436 * version of select() will return EINVAL on timeouts larger than 100000000
437 * seconds, even though it should have just clamped it. So, for Mac we should
438 * make sure that timeout value is bound to 100000000 seconds before passing it
439 * to select().
440 */
441 #if _DARWIN_C_SOURCE
442 #define CLAMP_MAC_TIMEOUT(to) do { if (to > 100000000000LL) to = 100000000000LL; } while (0)
443 #else
444 #define CLAMP_MAC_TIMEOUT(to) ((void)0)
445 #endif // _DARWIN_C_SOURCE
446
447 #if defined(__clang__) || defined(__llvm__)
448 /* Clang and llvm-gcc don't support global register variable (GRV).
449 Clang issues compile-time error for GRV. llvm-gcc accepts GRV (because
450 its front-end is gcc) but ignores it in the llvm-based back-end.
451 Undefining GRV decl to allow external/qemu and the rest of Android
452 to compile. But emulator built w/o GRV support will not function
453 correctly. User will be greeted with an error message (issued
454 in tcg/tcg.c) when emulator built this way is launched.
455 */
456 #define SUPPORT_GLOBAL_REGISTER_VARIABLE 0
457 #define GLOBAL_REGISTER_VARIABLE_DECL
458 #else
459 #define SUPPORT_GLOBAL_REGISTER_VARIABLE 1
460 #define GLOBAL_REGISTER_VARIABLE_DECL register
461 #endif /* __clang__ || __llvm__ */
462
463 /* vector definitions */
464 #ifdef __ALTIVEC__
465 #include <altivec.h>
466 /* The altivec.h header says we're allowed to undef these for
467 * C++ compatibility. Here we don't care about C++, but we
468 * undef them anyway to avoid namespace pollution.
469 */
470 #undef vector
471 #undef pixel
472 #undef bool
473 #define VECTYPE __vector unsigned char
474 #define SPLAT(p) vec_splat(vec_ld(0, p), 0)
475 #define ALL_EQ(v1, v2) vec_all_eq(v1, v2)
476 /* altivec.h may redefine the bool macro as vector type.
477 * Reset it to POSIX semantics. */
478 #define bool _Bool
479 #elif defined __SSE2__
480 #include <emmintrin.h>
481 #define VECTYPE __m128i
482 #define SPLAT(p) _mm_set1_epi8(*(p))
483 #define ALL_EQ(v1, v2) (_mm_movemask_epi8(_mm_cmpeq_epi8(v1, v2)) == 0xFFFF)
484 #else
485 #define VECTYPE unsigned long
486 #define SPLAT(p) (*(p) * (~0UL / 255))
487 #define ALL_EQ(v1, v2) ((v1) == (v2))
488 #endif
489
490 #define BUFFER_FIND_NONZERO_OFFSET_UNROLL_FACTOR 8
491 static inline bool
can_use_buffer_find_nonzero_offset(const void * buf,size_t len)492 can_use_buffer_find_nonzero_offset(const void *buf, size_t len)
493 {
494 return (len % (BUFFER_FIND_NONZERO_OFFSET_UNROLL_FACTOR
495 * sizeof(VECTYPE)) == 0
496 && ((uintptr_t) buf) % sizeof(VECTYPE) == 0);
497 }
498 size_t buffer_find_nonzero_offset(const void *buf, size_t len);
499
500 /*
501 * helper to parse debug environment variables
502 */
503 int parse_debug_env(const char *name, int max, int initial);
504
505 extern int use_icount;
506
507 #endif
508