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
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.
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__)
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 */
44 #include "glib-compat.h"
45 #include "qemu/option.h"
48 #include "sysemu/os-win32.h"
52 #include "sysemu/os-posix.h"
62 #define MAP_ANONYMOUS MAP_ANON
65 #define ENOMEDIUM ENODEV
70 #if !defined(ECANCELED)
71 #define ECANCELED 4097
73 #if !defined(EMEDIUMTYPE)
74 #define EMEDIUMTYPE 4098
77 #define TIME_MAX LONG_MAX
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
86 # error Unknown pointer size
92 # define lseek _lseeki64
94 int qemu_ftruncate64(int, int64_t);
95 #if !defined(ftruncate)
96 # define ftruncate qemu_ftruncate64
99 static inline char *realpath(const char *path, char *resolved_path)
101 _fullpath(resolved_path, path, _MAX_PATH);
102 return resolved_path;
106 void cpu_ticks_init(void);
109 void configure_icount(QemuOpts *opts, Error **errp);
110 extern int use_icount;
111 extern int icount_align_option;
112 /* drift information for info jit command */
113 extern int64_t max_delay;
114 extern int64_t max_advance;
115 void dump_drift_info(FILE *f, fprintf_function cpu_fprintf);
117 #include "qemu/osdep.h"
118 #include "qemu/bswap.h"
120 /* FIXME: Remove NEED_CPU_H. */
123 #endif /* !defined(NEED_CPU_H) */
125 /* main function, renamed */
126 #if defined(CONFIG_COCOA)
127 int qemu_main(int argc, char **argv, char **envp);
130 void qemu_get_timedate(struct tm *tm, int offset);
131 int qemu_timedate_diff(struct tm *tm);
137 * Check whether @s is one of the standard strings which indicate
138 * that the user is asking for a list of the valid values for a
139 * command option like -cpu or -M. The current accepted strings
140 * are 'help' and '?'. '?' is deprecated (it is a shell wildcard
141 * which makes it annoying to use in a reliable way) but provided
142 * for backwards compatibility.
144 * Returns: true if @s is a request for a list.
146 static inline bool is_help_option(const char *s)
148 return !strcmp(s, "?") || !strcmp(s, "help");
154 * @buf: buffer to copy string into
155 * @buf_size: size of @buf in bytes
156 * @str: string to copy
158 * Copy @str into @buf, including the trailing NUL, but do not
159 * write more than @buf_size bytes. The resulting buffer is
160 * always NUL terminated (even if the source string was too long).
161 * If @buf_size is zero or negative then no bytes are copied.
163 * This function is similar to strncpy(), but avoids two of that
164 * function's problems:
165 * * if @str fits in the buffer, pstrcpy() does not zero-fill the
166 * remaining space at the end of @buf
167 * * if @str is too long, pstrcpy() will copy the first @buf_size-1
168 * bytes and then add a NUL
170 void pstrcpy(char *buf, int buf_size, const char *str);
173 * @buf: buffer to copy string into
174 * @buf_size: size of @buf in bytes
175 * @str: string to copy
176 * @pad: character to pad the remainder of @buf with
178 * Copy @str into @buf (but *not* its trailing NUL!), and then pad the
179 * rest of the buffer with the @pad character. If @str is too large
180 * for the buffer then it is truncated, so that @buf contains the
181 * first @buf_size characters of @str, with no terminator.
183 void strpadcpy(char *buf, int buf_size, const char *str, char pad);
186 * @buf: buffer containing existing string
187 * @buf_size: size of @buf in bytes
188 * @s: string to concatenate to @buf
190 * Append a copy of @s to the string already in @buf, but do not
191 * allow the buffer to overflow. If the existing contents of @buf
192 * plus @str would total more than @buf_size bytes, then write
193 * as much of @str as will fit followed by a NUL terminator.
195 * @buf must already contain a NUL-terminated string, or the
196 * behaviour is undefined.
200 char *pstrcat(char *buf, int buf_size, const char *s);
203 * @str: string to test
204 * @val: prefix string to look for
205 * @ptr: NULL, or pointer to be written to indicate start of
206 * the remainder of the string
208 * Test whether @str starts with the prefix @val.
209 * If it does (including the degenerate case where @str and @val
210 * are equal) then return true. If @ptr is not NULL then a
211 * pointer to the first character following the prefix is written
212 * to it. If @val is not a prefix of @str then return false (and
213 * @ptr is not written to).
215 * Returns: true if @str starts with prefix @val, false otherwise.
217 int strstart(const char *str, const char *val, const char **ptr);
220 * @str: string to test
221 * @val: prefix string to look for
222 * @ptr: NULL, or pointer to be written to indicate start of
223 * the remainder of the string
225 * Test whether @str starts with the case-insensitive prefix @val.
226 * This function behaves identically to strstart(), except that the
227 * comparison is made after calling qemu_toupper() on each pair of
230 * Returns: true if @str starts with case-insensitive prefix @val,
233 int stristart(const char *str, const char *val, const char **ptr);
237 * @max_len: maximum number of bytes in @s to scan
239 * Return the length of the string @s, like strlen(), but do not
240 * examine more than @max_len bytes of the memory pointed to by @s.
241 * If no NUL terminator is found within @max_len bytes, then return
244 * This function has the same behaviour as the POSIX strnlen()
247 * Returns: length of @s in bytes, or @max_len, whichever is smaller.
249 int qemu_strnlen(const char *s, int max_len);
252 * @input: pointer to string to parse
253 * @delim: string containing delimiter characters to search for
255 * Locate the first occurrence of any character in @delim within
256 * the string referenced by @input, and replace it with a NUL.
257 * The location of the next character after the delimiter character
258 * is stored into @input.
259 * If the end of the string was reached without finding a delimiter
260 * character, then NULL is stored into @input.
261 * If @input points to a NULL pointer on entry, return NULL.
262 * The return value is always the original value of *@input (and
263 * so now points to a NUL-terminated string corresponding to the
264 * part of the input up to the first delimiter).
266 * This function has the same behaviour as the BSD strsep() function.
268 * Returns: the pointer originally in @input.
270 char *qemu_strsep(char **input, const char *delim);
271 time_t mktimegm(struct tm *tm);
273 int qemu_fdatasync(int fd);
274 int fcntl_setfl(int fd, int flag);
275 int qemu_parse_fd(const char *param);
277 int parse_uint(const char *s, unsigned long long *value, char **endptr,
279 int parse_uint_full(const char *s, unsigned long long *value, int base);
282 * strtosz() suffixes used to specify the default treatment of an
283 * argument passed to strtosz() without an explicit suffix.
284 * These should be defined using upper case characters in the range
285 * A-Z, as strtosz() will use qemu_toupper() on the given argument
286 * prior to comparison.
288 #define STRTOSZ_DEFSUFFIX_EB 'E'
289 #define STRTOSZ_DEFSUFFIX_PB 'P'
290 #define STRTOSZ_DEFSUFFIX_TB 'T'
291 #define STRTOSZ_DEFSUFFIX_GB 'G'
292 #define STRTOSZ_DEFSUFFIX_MB 'M'
293 #define STRTOSZ_DEFSUFFIX_KB 'K'
294 #define STRTOSZ_DEFSUFFIX_B 'B'
295 int64_t strtosz(const char *nptr, char **end);
296 int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix);
297 int64_t strtosz_suffix_unit(const char *nptr, char **end,
298 const char default_suffix, int64_t unit);
299 #define K_BYTE (1ULL << 10)
300 #define M_BYTE (1ULL << 20)
301 #define G_BYTE (1ULL << 30)
302 #define T_BYTE (1ULL << 40)
303 #define P_BYTE (1ULL << 50)
304 #define E_BYTE (1ULL << 60)
306 /* used to print char* safely */
307 #define STR_OR_NULL(str) ((str) ? (str) : "null")
310 bool id_wellformed(const char *id);
313 void init_paths(const char *prefix);
314 const char *path(const char *pathname);
316 #define qemu_isalnum(c) isalnum((unsigned char)(c))
317 #define qemu_isalpha(c) isalpha((unsigned char)(c))
318 #define qemu_iscntrl(c) iscntrl((unsigned char)(c))
319 #define qemu_isdigit(c) isdigit((unsigned char)(c))
320 #define qemu_isgraph(c) isgraph((unsigned char)(c))
321 #define qemu_islower(c) islower((unsigned char)(c))
322 #define qemu_isprint(c) isprint((unsigned char)(c))
323 #define qemu_ispunct(c) ispunct((unsigned char)(c))
324 #define qemu_isspace(c) isspace((unsigned char)(c))
325 #define qemu_isupper(c) isupper((unsigned char)(c))
326 #define qemu_isxdigit(c) isxdigit((unsigned char)(c))
327 #define qemu_tolower(c) tolower((unsigned char)(c))
328 #define qemu_toupper(c) toupper((unsigned char)(c))
329 #define qemu_isascii(c) isascii((unsigned char)(c))
330 #define qemu_toascii(c) toascii((unsigned char)(c))
332 void *qemu_oom_check(void *ptr);
334 ssize_t qemu_write_full(int fd, const void *buf, size_t count)
335 QEMU_WARN_UNUSED_RESULT;
338 int qemu_pipe(int pipefd[2]);
339 /* like openpty() but also makes it raw; return master fd */
340 int qemu_openpty_raw(int *aslave, char *pty_name);
344 /* MinGW needs type casts for the 'buf' and 'optval' arguments. */
345 #define qemu_getsockopt(sockfd, level, optname, optval, optlen) \
346 getsockopt(sockfd, level, optname, (void *)optval, optlen)
347 #define qemu_setsockopt(sockfd, level, optname, optval, optlen) \
348 setsockopt(sockfd, level, optname, (const void *)optval, optlen)
349 #define qemu_recv(sockfd, buf, len, flags) recv(sockfd, (void *)buf, len, flags)
350 #define qemu_sendto(sockfd, buf, len, flags, destaddr, addrlen) \
351 sendto(sockfd, (const void *)buf, len, flags, destaddr, addrlen)
353 #define qemu_getsockopt(sockfd, level, optname, optval, optlen) \
354 getsockopt(sockfd, level, optname, optval, optlen)
355 #define qemu_setsockopt(sockfd, level, optname, optval, optlen) \
356 setsockopt(sockfd, level, optname, optval, optlen)
357 #define qemu_recv(sockfd, buf, len, flags) recv(sockfd, buf, len, flags)
358 #define qemu_sendto(sockfd, buf, len, flags, destaddr, addrlen) \
359 sendto(sockfd, buf, len, flags, destaddr, addrlen)
362 /* Error handling. */
364 void QEMU_NORETURN hw_error(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
366 struct ParallelIOArg {
371 typedef int (*DMA_transfer_handler) (void *opaque, int nchan, int pos, int size);
373 typedef uint64_t pcibus_t;
375 typedef struct PCIHostDeviceAddress {
379 unsigned int function;
380 } PCIHostDeviceAddress;
382 void tcg_exec_init(unsigned long tb_size);
383 bool tcg_enabled(void);
385 void cpu_exec_init_all(void);
388 #ifdef CPU_SAVE_VERSION
389 void cpu_save(QEMUFile *f, void *opaque);
390 int cpu_load(QEMUFile *f, void *opaque, int version_id);
394 void qemu_cpu_kick_self(void);
397 struct qemu_work_item {
398 struct qemu_work_item *next;
399 void (*func)(void *data);
407 * Sends a (part of) iovec down a socket, yielding when the socket is full, or
408 * Receives data into a (part of) iovec from a socket,
409 * yielding when there is no data in the socket.
410 * The same interface as qemu_sendv_recvv(), with added yielding.
411 * XXX should mark these as coroutine_fn
413 ssize_t qemu_co_sendv_recvv(int sockfd, struct iovec *iov, unsigned iov_cnt,
414 size_t offset, size_t bytes, bool do_send);
415 #define qemu_co_recvv(sockfd, iov, iov_cnt, offset, bytes) \
416 qemu_co_sendv_recvv(sockfd, iov, iov_cnt, offset, bytes, false)
417 #define qemu_co_sendv(sockfd, iov, iov_cnt, offset, bytes) \
418 qemu_co_sendv_recvv(sockfd, iov, iov_cnt, offset, bytes, true)
421 * The same as above, but with just a single buffer
423 ssize_t qemu_co_send_recv(int sockfd, void *buf, size_t bytes, bool do_send);
424 #define qemu_co_recv(sockfd, buf, bytes) \
425 qemu_co_send_recv(sockfd, buf, bytes, false)
426 #define qemu_co_send(sockfd, buf, bytes) \
427 qemu_co_send_recv(sockfd, buf, bytes, true)
429 typedef struct QEMUIOVector {
436 void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint);
437 void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov);
438 void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len);
439 void qemu_iovec_concat(QEMUIOVector *dst,
440 QEMUIOVector *src, size_t soffset, size_t sbytes);
441 size_t qemu_iovec_concat_iov(QEMUIOVector *dst,
442 struct iovec *src_iov, unsigned int src_cnt,
443 size_t soffset, size_t sbytes);
444 bool qemu_iovec_is_zero(QEMUIOVector *qiov);
445 void qemu_iovec_destroy(QEMUIOVector *qiov);
446 void qemu_iovec_reset(QEMUIOVector *qiov);
447 size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset,
448 void *buf, size_t bytes);
449 size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset,
450 const void *buf, size_t bytes);
451 size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
452 int fillc, size_t bytes);
453 ssize_t qemu_iovec_compare(QEMUIOVector *a, QEMUIOVector *b);
454 void qemu_iovec_clone(QEMUIOVector *dest, const QEMUIOVector *src, void *buf);
455 void qemu_iovec_discard_back(QEMUIOVector *qiov, size_t bytes);
457 bool buffer_is_zero(const void *buf, size_t len);
459 void qemu_progress_init(int enabled, float min_skip);
460 void qemu_progress_end(void);
461 void qemu_progress_print(float delta, int max);
462 const char *qemu_get_vm_name(void);
464 #define QEMU_FILE_TYPE_BIOS 0
465 #define QEMU_FILE_TYPE_KEYMAP 1
466 char *qemu_find_file(int type, const char *name);
468 /* OS specific functions */
469 void os_setup_early_signal_handling(void);
470 char *os_find_datadir(void);
471 void os_parse_cmd_args(int index, const char *optarg);
473 /* Convert a byte between binary and BCD. */
474 static inline uint8_t to_bcd(uint8_t val)
476 return ((val / 10) << 4) | (val % 10);
479 static inline uint8_t from_bcd(uint8_t val)
481 return ((val >> 4) * 10) + (val & 0x0f);
484 /* compute with 96 bit intermediate result: (a*b)/c */
486 static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
488 return (__int128_t)a * b / c;
491 static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
496 #ifdef HOST_WORDS_BIGENDIAN
506 rl = (uint64_t)u.l.low * (uint64_t)b;
507 rh = (uint64_t)u.l.high * (uint64_t)b;
510 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
515 /* Round number down to multiple */
516 #define QEMU_ALIGN_DOWN(n, m) ((n) / (m) * (m))
518 /* Round number up to multiple */
519 #define QEMU_ALIGN_UP(n, m) QEMU_ALIGN_DOWN((n) + (m) - 1, (m))
521 static inline bool is_power_of_2(uint64_t value)
527 return !(value & (value - 1));
530 /* round down to the nearest power of 2*/
531 int64_t pow2floor(int64_t value);
533 /* round up to the nearest power of 2 (0 if overflow) */
534 uint64_t pow2ceil(uint64_t value);
536 #include "qemu/module.h"
539 * Implementation of ULEB128 (http://en.wikipedia.org/wiki/LEB128)
540 * Input is limited to 14-bit numbers
543 int uleb128_encode_small(uint8_t *out, uint32_t n);
544 int uleb128_decode_small(const uint8_t *in, uint32_t *n);
547 int mod_utf8_codepoint(const char *s, size_t n, char **end);
550 * Hexdump a buffer to a file. An optional string prefix is added to every line
553 void qemu_hexdump(const char *buf, FILE *fp, const char *prefix, size_t size);
555 /* vector definitions */
558 /* The altivec.h header says we're allowed to undef these for
559 * C++ compatibility. Here we don't care about C++, but we
560 * undef them anyway to avoid namespace pollution.
565 #define VECTYPE __vector unsigned char
566 #define SPLAT(p) vec_splat(vec_ld(0, p), 0)
567 #define ALL_EQ(v1, v2) vec_all_eq(v1, v2)
568 #define VEC_OR(v1, v2) ((v1) | (v2))
569 /* altivec.h may redefine the bool macro as vector type.
570 * Reset it to POSIX semantics. */
572 #elif defined __SSE2__
573 #include <emmintrin.h>
574 #define VECTYPE __m128i
575 #define SPLAT(p) _mm_set1_epi8(*(p))
576 #define ALL_EQ(v1, v2) (_mm_movemask_epi8(_mm_cmpeq_epi8(v1, v2)) == 0xFFFF)
577 #define VEC_OR(v1, v2) (_mm_or_si128(v1, v2))
579 #define VECTYPE unsigned long
580 #define SPLAT(p) (*(p) * (~0UL / 255))
581 #define ALL_EQ(v1, v2) ((v1) == (v2))
582 #define VEC_OR(v1, v2) ((v1) | (v2))
585 #define BUFFER_FIND_NONZERO_OFFSET_UNROLL_FACTOR 8
587 can_use_buffer_find_nonzero_offset(const void *buf, size_t len)
589 return (len % (BUFFER_FIND_NONZERO_OFFSET_UNROLL_FACTOR
590 * sizeof(VECTYPE)) == 0
591 && ((uintptr_t) buf) % sizeof(VECTYPE) == 0);
593 size_t buffer_find_nonzero_offset(const void *buf, size_t len);
596 * helper to parse debug environment variables
598 int parse_debug_env(const char *name, int max, int initial);
600 const char *qemu_ether_ntoa(const MACAddr *mac);