Merge tag 'v2.4.0-rc3'
[qemu/ar7.git] / include / qemu-common.h
bloba4c7fe8a3bd993790bcede0e657212ec64e11431
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 "qemu/compiler.h"
16 #include "config-host.h"
17 #include "qemu/typedefs.h"
18 #include "qemu/fprintf-fn.h"
20 #if defined(__arm__) || defined(__sparc__) || defined(__mips__) || defined(__hppa__) || defined(__ia64__)
21 #define WORDS_ALIGNED
22 #endif
24 #define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
26 /* we put basic includes here to avoid repeating them in device drivers */
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <stdarg.h>
30 #include <stdbool.h>
31 #include <string.h>
32 #include <strings.h>
33 #include <inttypes.h>
34 #include <limits.h>
35 #include <time.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <sys/stat.h>
41 #include <sys/time.h>
42 #include <assert.h>
43 #include <signal.h>
44 #include "glib-compat.h"
45 #include "qemu/option.h"
47 #ifdef _WIN32
48 #include "sysemu/os-win32.h"
49 #endif
51 #ifdef CONFIG_POSIX
52 #include "sysemu/os-posix.h"
53 #endif
55 #ifndef O_LARGEFILE
56 #define O_LARGEFILE 0
57 #endif
58 #ifndef O_BINARY
59 #define O_BINARY 0
60 #endif
61 #ifndef MAP_ANONYMOUS
62 #define MAP_ANONYMOUS MAP_ANON
63 #endif
64 #ifndef ENOMEDIUM
65 #define ENOMEDIUM ENODEV
66 #endif
67 #if !defined(ENOTSUP)
68 #define ENOTSUP 4096
69 #endif
70 #if !defined(ECANCELED)
71 #define ECANCELED 4097
72 #endif
73 #if !defined(EMEDIUMTYPE)
74 #define EMEDIUMTYPE 4098
75 #endif
76 #ifndef TIME_MAX
77 #define TIME_MAX LONG_MAX
78 #endif
80 /* HOST_LONG_BITS is the size of a native pointer in bits. */
81 #if UINTPTR_MAX == UINT32_MAX
82 # define HOST_LONG_BITS 32
83 #elif UINTPTR_MAX == UINT64_MAX
84 # define HOST_LONG_BITS 64
85 #else
86 # error Unknown pointer size
87 #endif
89 #define KiB 1024
90 #define MiB (KiB * KiB)
92 /* Trace unassigned memory or i/o accesses. */
93 extern bool trace_unassigned;
95 #ifdef _WIN32
96 #define fsync _commit
97 #if !defined(lseek)
98 # define lseek _lseeki64
99 #endif
100 int qemu_ftruncate64(int, int64_t);
101 #if !defined(ftruncate)
102 # define ftruncate qemu_ftruncate64
103 #endif
105 static inline char *realpath(const char *path, char *resolved_path)
107 _fullpath(resolved_path, path, _MAX_PATH);
108 return resolved_path;
110 #endif
112 void cpu_ticks_init(void);
114 /* icount */
115 void configure_icount(QemuOpts *opts, Error **errp);
116 extern int use_icount;
117 extern int icount_align_option;
118 /* drift information for info jit command */
119 extern int64_t max_delay;
120 extern int64_t max_advance;
121 void dump_drift_info(FILE *f, fprintf_function cpu_fprintf);
123 #include "qemu/osdep.h"
124 #include "qemu/bswap.h"
126 /* FIXME: Remove NEED_CPU_H. */
127 #ifdef NEED_CPU_H
128 #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 /* util/cutils.c */
159 * pstrcpy:
160 * @buf: buffer to copy string into
161 * @buf_size: size of @buf in bytes
162 * @str: string to copy
164 * Copy @str into @buf, including the trailing NUL, but do not
165 * write more than @buf_size bytes. The resulting buffer is
166 * always NUL terminated (even if the source string was too long).
167 * If @buf_size is zero or negative then no bytes are copied.
169 * This function is similar to strncpy(), but avoids two of that
170 * function's problems:
171 * * if @str fits in the buffer, pstrcpy() does not zero-fill the
172 * remaining space at the end of @buf
173 * * if @str is too long, pstrcpy() will copy the first @buf_size-1
174 * bytes and then add a NUL
176 void pstrcpy(char *buf, int buf_size, const char *str);
178 * strpadcpy:
179 * @buf: buffer to copy string into
180 * @buf_size: size of @buf in bytes
181 * @str: string to copy
182 * @pad: character to pad the remainder of @buf with
184 * Copy @str into @buf (but *not* its trailing NUL!), and then pad the
185 * rest of the buffer with the @pad character. If @str is too large
186 * for the buffer then it is truncated, so that @buf contains the
187 * first @buf_size characters of @str, with no terminator.
189 void strpadcpy(char *buf, int buf_size, const char *str, char pad);
191 * pstrcat:
192 * @buf: buffer containing existing string
193 * @buf_size: size of @buf in bytes
194 * @s: string to concatenate to @buf
196 * Append a copy of @s to the string already in @buf, but do not
197 * allow the buffer to overflow. If the existing contents of @buf
198 * plus @str would total more than @buf_size bytes, then write
199 * as much of @str as will fit followed by a NUL terminator.
201 * @buf must already contain a NUL-terminated string, or the
202 * behaviour is undefined.
204 * Returns: @buf.
206 char *pstrcat(char *buf, int buf_size, const char *s);
208 * strstart:
209 * @str: string to test
210 * @val: prefix string to look for
211 * @ptr: NULL, or pointer to be written to indicate start of
212 * the remainder of the string
214 * Test whether @str starts with the prefix @val.
215 * If it does (including the degenerate case where @str and @val
216 * are equal) then return true. If @ptr is not NULL then a
217 * pointer to the first character following the prefix is written
218 * to it. If @val is not a prefix of @str then return false (and
219 * @ptr is not written to).
221 * Returns: true if @str starts with prefix @val, false otherwise.
223 int strstart(const char *str, const char *val, const char **ptr);
225 * stristart:
226 * @str: string to test
227 * @val: prefix string to look for
228 * @ptr: NULL, or pointer to be written to indicate start of
229 * the remainder of the string
231 * Test whether @str starts with the case-insensitive prefix @val.
232 * This function behaves identically to strstart(), except that the
233 * comparison is made after calling qemu_toupper() on each pair of
234 * characters.
236 * Returns: true if @str starts with case-insensitive prefix @val,
237 * false otherwise.
239 int stristart(const char *str, const char *val, const char **ptr);
241 * qemu_strnlen:
242 * @s: string
243 * @max_len: maximum number of bytes in @s to scan
245 * Return the length of the string @s, like strlen(), but do not
246 * examine more than @max_len bytes of the memory pointed to by @s.
247 * If no NUL terminator is found within @max_len bytes, then return
248 * @max_len instead.
250 * This function has the same behaviour as the POSIX strnlen()
251 * function.
253 * Returns: length of @s in bytes, or @max_len, whichever is smaller.
255 int qemu_strnlen(const char *s, int max_len);
257 * qemu_strsep:
258 * @input: pointer to string to parse
259 * @delim: string containing delimiter characters to search for
261 * Locate the first occurrence of any character in @delim within
262 * the string referenced by @input, and replace it with a NUL.
263 * The location of the next character after the delimiter character
264 * is stored into @input.
265 * If the end of the string was reached without finding a delimiter
266 * character, then NULL is stored into @input.
267 * If @input points to a NULL pointer on entry, return NULL.
268 * The return value is always the original value of *@input (and
269 * so now points to a NUL-terminated string corresponding to the
270 * part of the input up to the first delimiter).
272 * This function has the same behaviour as the BSD strsep() function.
274 * Returns: the pointer originally in @input.
276 char *qemu_strsep(char **input, const char *delim);
277 time_t mktimegm(struct tm *tm);
278 int qemu_fls(int i);
279 int qemu_fdatasync(int fd);
280 int fcntl_setfl(int fd, int flag);
281 int qemu_parse_fd(const char *param);
283 int parse_uint(const char *s, unsigned long long *value, char **endptr,
284 int base);
285 int parse_uint_full(const char *s, unsigned long long *value, int base);
288 * strtosz() suffixes used to specify the default treatment of an
289 * argument passed to strtosz() without an explicit suffix.
290 * These should be defined using upper case characters in the range
291 * A-Z, as strtosz() will use qemu_toupper() on the given argument
292 * prior to comparison.
294 #define STRTOSZ_DEFSUFFIX_EB 'E'
295 #define STRTOSZ_DEFSUFFIX_PB 'P'
296 #define STRTOSZ_DEFSUFFIX_TB 'T'
297 #define STRTOSZ_DEFSUFFIX_GB 'G'
298 #define STRTOSZ_DEFSUFFIX_MB 'M'
299 #define STRTOSZ_DEFSUFFIX_KB 'K'
300 #define STRTOSZ_DEFSUFFIX_B 'B'
301 int64_t strtosz(const char *nptr, char **end);
302 int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix);
303 int64_t strtosz_suffix_unit(const char *nptr, char **end,
304 const char default_suffix, int64_t unit);
305 #define K_BYTE (1ULL << 10)
306 #define M_BYTE (1ULL << 20)
307 #define G_BYTE (1ULL << 30)
308 #define T_BYTE (1ULL << 40)
309 #define P_BYTE (1ULL << 50)
310 #define E_BYTE (1ULL << 60)
312 /* used to print char* safely */
313 #define STR_OR_NULL(str) ((str) ? (str) : "null")
315 /* id.c */
316 bool id_wellformed(const char *id);
318 /* path.c */
319 void init_paths(const char *prefix);
320 const char *path(const char *pathname);
322 #define qemu_isalnum(c) isalnum((unsigned char)(c))
323 #define qemu_isalpha(c) isalpha((unsigned char)(c))
324 #define qemu_iscntrl(c) iscntrl((unsigned char)(c))
325 #define qemu_isdigit(c) isdigit((unsigned char)(c))
326 #define qemu_isgraph(c) isgraph((unsigned char)(c))
327 #define qemu_islower(c) islower((unsigned char)(c))
328 #define qemu_isprint(c) isprint((unsigned char)(c))
329 #define qemu_ispunct(c) ispunct((unsigned char)(c))
330 #define qemu_isspace(c) isspace((unsigned char)(c))
331 #define qemu_isupper(c) isupper((unsigned char)(c))
332 #define qemu_isxdigit(c) isxdigit((unsigned char)(c))
333 #define qemu_tolower(c) tolower((unsigned char)(c))
334 #define qemu_toupper(c) toupper((unsigned char)(c))
335 #define qemu_isascii(c) isascii((unsigned char)(c))
336 #define qemu_toascii(c) toascii((unsigned char)(c))
338 void *qemu_oom_check(void *ptr);
340 ssize_t qemu_write_full(int fd, const void *buf, size_t count)
341 QEMU_WARN_UNUSED_RESULT;
343 #ifndef _WIN32
344 int qemu_pipe(int pipefd[2]);
345 /* like openpty() but also makes it raw; return master fd */
346 int qemu_openpty_raw(int *aslave, char *pty_name);
347 #endif
349 /* Error handling. */
351 void QEMU_NORETURN hw_error(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
353 struct ParallelIOArg {
354 void *buffer;
355 int count;
358 typedef int (*DMA_transfer_handler) (void *opaque, int nchan, int pos, int size);
360 typedef uint64_t pcibus_t;
362 typedef struct PCIHostDeviceAddress {
363 unsigned int domain;
364 unsigned int bus;
365 unsigned int slot;
366 unsigned int function;
367 } PCIHostDeviceAddress;
369 void tcg_exec_init(uintptr_t tb_size);
370 bool tcg_enabled(void);
372 void cpu_exec_init_all(void);
374 /* CPU save/load. */
375 #ifdef CPU_SAVE_VERSION
376 void cpu_save(QEMUFile *f, void *opaque);
377 int cpu_load(QEMUFile *f, void *opaque, int version_id);
378 #endif
380 /* Unblock cpu */
381 void qemu_cpu_kick_self(void);
383 /* work queue */
384 struct qemu_work_item {
385 struct qemu_work_item *next;
386 void (*func)(void *data);
387 void *data;
388 int done;
389 bool free;
394 * Sends a (part of) iovec down a socket, yielding when the socket is full, or
395 * Receives data into a (part of) iovec from a socket,
396 * yielding when there is no data in the socket.
397 * The same interface as qemu_sendv_recvv(), with added yielding.
398 * XXX should mark these as coroutine_fn
400 ssize_t qemu_co_sendv_recvv(int sockfd, struct iovec *iov, unsigned iov_cnt,
401 size_t offset, size_t bytes, bool do_send);
402 #define qemu_co_recvv(sockfd, iov, iov_cnt, offset, bytes) \
403 qemu_co_sendv_recvv(sockfd, iov, iov_cnt, offset, bytes, false)
404 #define qemu_co_sendv(sockfd, iov, iov_cnt, offset, bytes) \
405 qemu_co_sendv_recvv(sockfd, iov, iov_cnt, offset, bytes, true)
408 * The same as above, but with just a single buffer
410 ssize_t qemu_co_send_recv(int sockfd, void *buf, size_t bytes, bool do_send);
411 #define qemu_co_recv(sockfd, buf, bytes) \
412 qemu_co_send_recv(sockfd, buf, bytes, false)
413 #define qemu_co_send(sockfd, buf, bytes) \
414 qemu_co_send_recv(sockfd, buf, bytes, true)
416 typedef struct QEMUIOVector {
417 struct iovec *iov;
418 int niov;
419 int nalloc;
420 size_t size;
421 } QEMUIOVector;
423 void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint);
424 void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov);
425 void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len);
426 void qemu_iovec_concat(QEMUIOVector *dst,
427 QEMUIOVector *src, size_t soffset, size_t sbytes);
428 size_t qemu_iovec_concat_iov(QEMUIOVector *dst,
429 struct iovec *src_iov, unsigned int src_cnt,
430 size_t soffset, size_t sbytes);
431 bool qemu_iovec_is_zero(QEMUIOVector *qiov);
432 void qemu_iovec_destroy(QEMUIOVector *qiov);
433 void qemu_iovec_reset(QEMUIOVector *qiov);
434 size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset,
435 void *buf, size_t bytes);
436 size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset,
437 const void *buf, size_t bytes);
438 size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
439 int fillc, size_t bytes);
440 ssize_t qemu_iovec_compare(QEMUIOVector *a, QEMUIOVector *b);
441 void qemu_iovec_clone(QEMUIOVector *dest, const QEMUIOVector *src, void *buf);
442 void qemu_iovec_discard_back(QEMUIOVector *qiov, size_t bytes);
444 bool buffer_is_zero(const void *buf, size_t len);
446 void qemu_progress_init(int enabled, float min_skip);
447 void qemu_progress_end(void);
448 void qemu_progress_print(float delta, int max);
449 const char *qemu_get_vm_name(void);
451 #define QEMU_FILE_TYPE_BIOS 0
452 #define QEMU_FILE_TYPE_KEYMAP 1
453 char *qemu_find_file(int type, const char *name);
455 /* OS specific functions */
456 void os_setup_early_signal_handling(void);
457 char *os_find_datadir(void);
458 void os_parse_cmd_args(int index, const char *optarg);
460 /* Convert a byte between binary and BCD. */
461 static inline uint8_t to_bcd(uint8_t val)
463 return ((val / 10) << 4) | (val % 10);
466 static inline uint8_t from_bcd(uint8_t val)
468 return ((val >> 4) * 10) + (val & 0x0f);
471 /* compute with 96 bit intermediate result: (a*b)/c */
472 #ifdef CONFIG_INT128
473 static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
475 return (__int128_t)a * b / c;
477 #else
478 static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
480 union {
481 uint64_t ll;
482 struct {
483 #ifdef HOST_WORDS_BIGENDIAN
484 uint32_t high, low;
485 #else
486 uint32_t low, high;
487 #endif
488 } l;
489 } u, res;
490 uint64_t rl, rh;
492 u.ll = a;
493 rl = (uint64_t)u.l.low * (uint64_t)b;
494 rh = (uint64_t)u.l.high * (uint64_t)b;
495 rh += (rl >> 32);
496 res.l.high = rh / c;
497 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
498 return res.ll;
500 #endif
502 /* Round number down to multiple */
503 #define QEMU_ALIGN_DOWN(n, m) ((n) / (m) * (m))
505 /* Round number up to multiple */
506 #define QEMU_ALIGN_UP(n, m) QEMU_ALIGN_DOWN((n) + (m) - 1, (m))
508 static inline bool is_power_of_2(uint64_t value)
510 if (!value) {
511 return 0;
514 return !(value & (value - 1));
517 /* round down to the nearest power of 2*/
518 int64_t pow2floor(int64_t value);
520 /* round up to the nearest power of 2 (0 if overflow) */
521 uint64_t pow2ceil(uint64_t value);
523 #include "qemu/module.h"
526 * Implementation of ULEB128 (http://en.wikipedia.org/wiki/LEB128)
527 * Input is limited to 14-bit numbers
530 int uleb128_encode_small(uint8_t *out, uint32_t n);
531 int uleb128_decode_small(const uint8_t *in, uint32_t *n);
533 /* unicode.c */
534 int mod_utf8_codepoint(const char *s, size_t n, char **end);
537 * Hexdump a buffer to a file. An optional string prefix is added to every line
540 void qemu_hexdump(const char *buf, FILE *fp, const char *prefix, size_t size);
542 /* vector definitions */
543 #ifdef __ALTIVEC__
544 #include <altivec.h>
545 /* The altivec.h header says we're allowed to undef these for
546 * C++ compatibility. Here we don't care about C++, but we
547 * undef them anyway to avoid namespace pollution.
549 #undef vector
550 #undef pixel
551 #undef bool
552 #define VECTYPE __vector unsigned char
553 #define SPLAT(p) vec_splat(vec_ld(0, p), 0)
554 #define ALL_EQ(v1, v2) vec_all_eq(v1, v2)
555 #define VEC_OR(v1, v2) ((v1) | (v2))
556 /* altivec.h may redefine the bool macro as vector type.
557 * Reset it to POSIX semantics. */
558 #define bool _Bool
559 #elif defined __SSE2__
560 #include <emmintrin.h>
561 #define VECTYPE __m128i
562 #define SPLAT(p) _mm_set1_epi8(*(p))
563 #define ALL_EQ(v1, v2) (_mm_movemask_epi8(_mm_cmpeq_epi8(v1, v2)) == 0xFFFF)
564 #define VEC_OR(v1, v2) (_mm_or_si128(v1, v2))
565 #else
566 #define VECTYPE unsigned long
567 #define SPLAT(p) (*(p) * (~0UL / 255))
568 #define ALL_EQ(v1, v2) ((v1) == (v2))
569 #define VEC_OR(v1, v2) ((v1) | (v2))
570 #endif
572 #define BUFFER_FIND_NONZERO_OFFSET_UNROLL_FACTOR 8
573 static inline bool
574 can_use_buffer_find_nonzero_offset(const void *buf, size_t len)
576 return (len % (BUFFER_FIND_NONZERO_OFFSET_UNROLL_FACTOR
577 * sizeof(VECTYPE)) == 0
578 && ((uintptr_t) buf) % sizeof(VECTYPE) == 0);
580 size_t buffer_find_nonzero_offset(const void *buf, size_t len);
583 * helper to parse debug environment variables
585 int parse_debug_env(const char *name, int max, int initial);
587 const char *qemu_ether_ntoa(const MACAddr *mac);
589 #endif