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/osdep.h"
16 #include "qemu/typedefs.h"
17 #include "qemu/fprintf-fn.h"
19 #if defined(__arm__) || defined(__sparc__) || defined(__mips__) || defined(__hppa__) || defined(__ia64__)
23 #define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
25 #include "glib-compat.h"
26 #include "qemu/option.h"
27 #include "qemu/host-utils.h"
29 /* HOST_LONG_BITS is the size of a native pointer in bits. */
30 #if UINTPTR_MAX == UINT32_MAX
31 # define HOST_LONG_BITS 32
32 #elif UINTPTR_MAX == UINT64_MAX
33 # define HOST_LONG_BITS 64
35 # error Unknown pointer size
39 #define MiB (KiB * KiB)
41 /* Trace unassigned memory or i/o accesses. */
42 extern bool trace_unassigned
;
44 void cpu_ticks_init(void);
47 void configure_icount(QemuOpts
*opts
, Error
**errp
);
48 extern int use_icount
;
49 extern int icount_align_option
;
50 /* drift information for info jit command */
51 extern int64_t max_delay
;
52 extern int64_t max_advance
;
53 void dump_drift_info(FILE *f
, fprintf_function cpu_fprintf
);
55 #include "qemu/bswap.h"
57 /* FIXME: Remove NEED_CPU_H. */
60 #endif /* !defined(NEED_CPU_H) */
62 /* main function, renamed */
63 #if defined(CONFIG_COCOA)
64 int qemu_main(int argc
, char **argv
, char **envp
);
67 void qemu_get_timedate(struct tm
*tm
, int offset
);
68 int qemu_timedate_diff(struct tm
*tm
);
74 * Check whether @s is one of the standard strings which indicate
75 * that the user is asking for a list of the valid values for a
76 * command option like -cpu or -M. The current accepted strings
77 * are 'help' and '?'. '?' is deprecated (it is a shell wildcard
78 * which makes it annoying to use in a reliable way) but provided
79 * for backwards compatibility.
81 * Returns: true if @s is a request for a list.
83 static inline bool is_help_option(const char *s
)
85 return !strcmp(s
, "?") || !strcmp(s
, "help");
91 * @buf: buffer to copy string into
92 * @buf_size: size of @buf in bytes
93 * @str: string to copy
95 * Copy @str into @buf, including the trailing NUL, but do not
96 * write more than @buf_size bytes. The resulting buffer is
97 * always NUL terminated (even if the source string was too long).
98 * If @buf_size is zero or negative then no bytes are copied.
100 * This function is similar to strncpy(), but avoids two of that
101 * function's problems:
102 * * if @str fits in the buffer, pstrcpy() does not zero-fill the
103 * remaining space at the end of @buf
104 * * if @str is too long, pstrcpy() will copy the first @buf_size-1
105 * bytes and then add a NUL
107 void pstrcpy(char *buf
, int buf_size
, const char *str
);
110 * @buf: buffer to copy string into
111 * @buf_size: size of @buf in bytes
112 * @str: string to copy
113 * @pad: character to pad the remainder of @buf with
115 * Copy @str into @buf (but *not* its trailing NUL!), and then pad the
116 * rest of the buffer with the @pad character. If @str is too large
117 * for the buffer then it is truncated, so that @buf contains the
118 * first @buf_size characters of @str, with no terminator.
120 void strpadcpy(char *buf
, int buf_size
, const char *str
, char pad
);
123 * @buf: buffer containing existing string
124 * @buf_size: size of @buf in bytes
125 * @s: string to concatenate to @buf
127 * Append a copy of @s to the string already in @buf, but do not
128 * allow the buffer to overflow. If the existing contents of @buf
129 * plus @str would total more than @buf_size bytes, then write
130 * as much of @str as will fit followed by a NUL terminator.
132 * @buf must already contain a NUL-terminated string, or the
133 * behaviour is undefined.
137 char *pstrcat(char *buf
, int buf_size
, const char *s
);
140 * @str: string to test
141 * @val: prefix string to look for
142 * @ptr: NULL, or pointer to be written to indicate start of
143 * the remainder of the string
145 * Test whether @str starts with the prefix @val.
146 * If it does (including the degenerate case where @str and @val
147 * are equal) then return true. If @ptr is not NULL then a
148 * pointer to the first character following the prefix is written
149 * to it. If @val is not a prefix of @str then return false (and
150 * @ptr is not written to).
152 * Returns: true if @str starts with prefix @val, false otherwise.
154 int strstart(const char *str
, const char *val
, const char **ptr
);
157 * @str: string to test
158 * @val: prefix string to look for
159 * @ptr: NULL, or pointer to be written to indicate start of
160 * the remainder of the string
162 * Test whether @str starts with the case-insensitive prefix @val.
163 * This function behaves identically to strstart(), except that the
164 * comparison is made after calling qemu_toupper() on each pair of
167 * Returns: true if @str starts with case-insensitive prefix @val,
170 int stristart(const char *str
, const char *val
, const char **ptr
);
174 * @max_len: maximum number of bytes in @s to scan
176 * Return the length of the string @s, like strlen(), but do not
177 * examine more than @max_len bytes of the memory pointed to by @s.
178 * If no NUL terminator is found within @max_len bytes, then return
181 * This function has the same behaviour as the POSIX strnlen()
184 * Returns: length of @s in bytes, or @max_len, whichever is smaller.
186 int qemu_strnlen(const char *s
, int max_len
);
189 * @input: pointer to string to parse
190 * @delim: string containing delimiter characters to search for
192 * Locate the first occurrence of any character in @delim within
193 * the string referenced by @input, and replace it with a NUL.
194 * The location of the next character after the delimiter character
195 * is stored into @input.
196 * If the end of the string was reached without finding a delimiter
197 * character, then NULL is stored into @input.
198 * If @input points to a NULL pointer on entry, return NULL.
199 * The return value is always the original value of *@input (and
200 * so now points to a NUL-terminated string corresponding to the
201 * part of the input up to the first delimiter).
203 * This function has the same behaviour as the BSD strsep() function.
205 * Returns: the pointer originally in @input.
207 char *qemu_strsep(char **input
, const char *delim
);
208 time_t mktimegm(struct tm
*tm
);
209 int qemu_fdatasync(int fd
);
210 int fcntl_setfl(int fd
, int flag
);
211 int qemu_parse_fd(const char *param
);
212 int qemu_strtol(const char *nptr
, const char **endptr
, int base
,
214 int qemu_strtoul(const char *nptr
, const char **endptr
, int base
,
215 unsigned long *result
);
216 int qemu_strtoll(const char *nptr
, const char **endptr
, int base
,
218 int qemu_strtoull(const char *nptr
, const char **endptr
, int base
,
221 int parse_uint(const char *s
, unsigned long long *value
, char **endptr
,
223 int parse_uint_full(const char *s
, unsigned long long *value
, int base
);
226 * strtosz() suffixes used to specify the default treatment of an
227 * argument passed to strtosz() without an explicit suffix.
228 * These should be defined using upper case characters in the range
229 * A-Z, as strtosz() will use qemu_toupper() on the given argument
230 * prior to comparison.
232 #define STRTOSZ_DEFSUFFIX_EB 'E'
233 #define STRTOSZ_DEFSUFFIX_PB 'P'
234 #define STRTOSZ_DEFSUFFIX_TB 'T'
235 #define STRTOSZ_DEFSUFFIX_GB 'G'
236 #define STRTOSZ_DEFSUFFIX_MB 'M'
237 #define STRTOSZ_DEFSUFFIX_KB 'K'
238 #define STRTOSZ_DEFSUFFIX_B 'B'
239 int64_t strtosz(const char *nptr
, char **end
);
240 int64_t strtosz_suffix(const char *nptr
, char **end
, const char default_suffix
);
241 int64_t strtosz_suffix_unit(const char *nptr
, char **end
,
242 const char default_suffix
, int64_t unit
);
243 #define K_BYTE (1ULL << 10)
244 #define M_BYTE (1ULL << 20)
245 #define G_BYTE (1ULL << 30)
246 #define T_BYTE (1ULL << 40)
247 #define P_BYTE (1ULL << 50)
248 #define E_BYTE (1ULL << 60)
250 /* used to print char* safely */
251 #define STR_OR_NULL(str) ((str) ? (str) : "null")
254 bool id_wellformed(const char *id
);
257 void init_paths(const char *prefix
);
258 const char *path(const char *pathname
);
260 #define qemu_isalnum(c) isalnum((unsigned char)(c))
261 #define qemu_isalpha(c) isalpha((unsigned char)(c))
262 #define qemu_iscntrl(c) iscntrl((unsigned char)(c))
263 #define qemu_isdigit(c) isdigit((unsigned char)(c))
264 #define qemu_isgraph(c) isgraph((unsigned char)(c))
265 #define qemu_islower(c) islower((unsigned char)(c))
266 #define qemu_isprint(c) isprint((unsigned char)(c))
267 #define qemu_ispunct(c) ispunct((unsigned char)(c))
268 #define qemu_isspace(c) isspace((unsigned char)(c))
269 #define qemu_isupper(c) isupper((unsigned char)(c))
270 #define qemu_isxdigit(c) isxdigit((unsigned char)(c))
271 #define qemu_tolower(c) tolower((unsigned char)(c))
272 #define qemu_toupper(c) toupper((unsigned char)(c))
273 #define qemu_isascii(c) isascii((unsigned char)(c))
274 #define qemu_toascii(c) toascii((unsigned char)(c))
276 void *qemu_oom_check(void *ptr
);
278 ssize_t
qemu_write_full(int fd
, const void *buf
, size_t count
)
279 QEMU_WARN_UNUSED_RESULT
;
282 int qemu_pipe(int pipefd
[2]);
283 /* like openpty() but also makes it raw; return master fd */
284 int qemu_openpty_raw(int *aslave
, char *pty_name
);
287 /* Error handling. */
289 void QEMU_NORETURN
hw_error(const char *fmt
, ...) GCC_FMT_ATTR(1, 2);
291 struct ParallelIOArg
{
296 typedef int (*DMA_transfer_handler
) (void *opaque
, int nchan
, int pos
, int size
);
298 typedef uint64_t pcibus_t
;
300 typedef struct PCIHostDeviceAddress
{
304 unsigned int function
;
305 } PCIHostDeviceAddress
;
307 void tcg_exec_init(uintptr_t tb_size
);
308 bool tcg_enabled(void);
310 void cpu_exec_init_all(void);
313 #ifdef CPU_SAVE_VERSION
314 void cpu_save(QEMUFile
*f
, void *opaque
);
315 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
);
319 void qemu_cpu_kick_self(void);
322 struct qemu_work_item
{
323 struct qemu_work_item
*next
;
324 void (*func
)(void *data
);
332 * Sends a (part of) iovec down a socket, yielding when the socket is full, or
333 * Receives data into a (part of) iovec from a socket,
334 * yielding when there is no data in the socket.
335 * The same interface as qemu_sendv_recvv(), with added yielding.
336 * XXX should mark these as coroutine_fn
338 ssize_t
qemu_co_sendv_recvv(int sockfd
, struct iovec
*iov
, unsigned iov_cnt
,
339 size_t offset
, size_t bytes
, bool do_send
);
340 #define qemu_co_recvv(sockfd, iov, iov_cnt, offset, bytes) \
341 qemu_co_sendv_recvv(sockfd, iov, iov_cnt, offset, bytes, false)
342 #define qemu_co_sendv(sockfd, iov, iov_cnt, offset, bytes) \
343 qemu_co_sendv_recvv(sockfd, iov, iov_cnt, offset, bytes, true)
346 * The same as above, but with just a single buffer
348 ssize_t
qemu_co_send_recv(int sockfd
, void *buf
, size_t bytes
, bool do_send
);
349 #define qemu_co_recv(sockfd, buf, bytes) \
350 qemu_co_send_recv(sockfd, buf, bytes, false)
351 #define qemu_co_send(sockfd, buf, bytes) \
352 qemu_co_send_recv(sockfd, buf, bytes, true)
354 typedef struct QEMUIOVector
{
361 void qemu_iovec_init(QEMUIOVector
*qiov
, int alloc_hint
);
362 void qemu_iovec_init_external(QEMUIOVector
*qiov
, struct iovec
*iov
, int niov
);
363 void qemu_iovec_add(QEMUIOVector
*qiov
, void *base
, size_t len
);
364 void qemu_iovec_concat(QEMUIOVector
*dst
,
365 QEMUIOVector
*src
, size_t soffset
, size_t sbytes
);
366 size_t qemu_iovec_concat_iov(QEMUIOVector
*dst
,
367 struct iovec
*src_iov
, unsigned int src_cnt
,
368 size_t soffset
, size_t sbytes
);
369 bool qemu_iovec_is_zero(QEMUIOVector
*qiov
);
370 void qemu_iovec_destroy(QEMUIOVector
*qiov
);
371 void qemu_iovec_reset(QEMUIOVector
*qiov
);
372 size_t qemu_iovec_to_buf(QEMUIOVector
*qiov
, size_t offset
,
373 void *buf
, size_t bytes
);
374 size_t qemu_iovec_from_buf(QEMUIOVector
*qiov
, size_t offset
,
375 const void *buf
, size_t bytes
);
376 size_t qemu_iovec_memset(QEMUIOVector
*qiov
, size_t offset
,
377 int fillc
, size_t bytes
);
378 ssize_t
qemu_iovec_compare(QEMUIOVector
*a
, QEMUIOVector
*b
);
379 void qemu_iovec_clone(QEMUIOVector
*dest
, const QEMUIOVector
*src
, void *buf
);
380 void qemu_iovec_discard_back(QEMUIOVector
*qiov
, size_t bytes
);
382 bool buffer_is_zero(const void *buf
, size_t len
);
384 void qemu_progress_init(int enabled
, float min_skip
);
385 void qemu_progress_end(void);
386 void qemu_progress_print(float delta
, int max
);
387 const char *qemu_get_vm_name(void);
389 #define QEMU_FILE_TYPE_BIOS 0
390 #define QEMU_FILE_TYPE_KEYMAP 1
391 char *qemu_find_file(int type
, const char *name
);
393 /* OS specific functions */
394 void os_setup_early_signal_handling(void);
395 char *os_find_datadir(void);
396 void os_parse_cmd_args(int index
, const char *optarg
);
398 /* Convert a byte between binary and BCD. */
399 static inline uint8_t to_bcd(uint8_t val
)
401 return ((val
/ 10) << 4) | (val
% 10);
404 static inline uint8_t from_bcd(uint8_t val
)
406 return ((val
>> 4) * 10) + (val
& 0x0f);
409 /* Round number down to multiple */
410 #define QEMU_ALIGN_DOWN(n, m) ((n) / (m) * (m))
412 /* Round number up to multiple */
413 #define QEMU_ALIGN_UP(n, m) QEMU_ALIGN_DOWN((n) + (m) - 1, (m))
415 #include "qemu/module.h"
418 * Implementation of ULEB128 (http://en.wikipedia.org/wiki/LEB128)
419 * Input is limited to 14-bit numbers
422 int uleb128_encode_small(uint8_t *out
, uint32_t n
);
423 int uleb128_decode_small(const uint8_t *in
, uint32_t *n
);
426 int mod_utf8_codepoint(const char *s
, size_t n
, char **end
);
429 * Hexdump a buffer to a file. An optional string prefix is added to every line
432 void qemu_hexdump(const char *buf
, FILE *fp
, const char *prefix
, size_t size
);
434 /* vector definitions */
437 /* The altivec.h header says we're allowed to undef these for
438 * C++ compatibility. Here we don't care about C++, but we
439 * undef them anyway to avoid namespace pollution.
444 #define VECTYPE __vector unsigned char
445 #define SPLAT(p) vec_splat(vec_ld(0, p), 0)
446 #define ALL_EQ(v1, v2) vec_all_eq(v1, v2)
447 #define VEC_OR(v1, v2) ((v1) | (v2))
448 /* altivec.h may redefine the bool macro as vector type.
449 * Reset it to POSIX semantics. */
451 #elif defined __SSE2__
452 #include <emmintrin.h>
453 #define VECTYPE __m128i
454 #define SPLAT(p) _mm_set1_epi8(*(p))
455 #define ALL_EQ(v1, v2) (_mm_movemask_epi8(_mm_cmpeq_epi8(v1, v2)) == 0xFFFF)
456 #define VEC_OR(v1, v2) (_mm_or_si128(v1, v2))
458 #define VECTYPE unsigned long
459 #define SPLAT(p) (*(p) * (~0UL / 255))
460 #define ALL_EQ(v1, v2) ((v1) == (v2))
461 #define VEC_OR(v1, v2) ((v1) | (v2))
464 #define BUFFER_FIND_NONZERO_OFFSET_UNROLL_FACTOR 8
466 can_use_buffer_find_nonzero_offset(const void *buf
, size_t len
)
468 return (len
% (BUFFER_FIND_NONZERO_OFFSET_UNROLL_FACTOR
469 * sizeof(VECTYPE
)) == 0
470 && ((uintptr_t) buf
) % sizeof(VECTYPE
) == 0);
472 size_t buffer_find_nonzero_offset(const void *buf
, size_t len
);
475 * helper to parse debug environment variables
477 int parse_debug_env(const char *name
, int max
, int initial
);
479 const char *qemu_ether_ntoa(const MACAddr
*mac
);