]> git.proxmox.com Git - mirror_qemu.git/blame - include/qemu-common.h
vfio/pci: Config window quirks
[mirror_qemu.git] / include / qemu-common.h
CommitLineData
9fb26641 1
04509ad9
EH
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 */
faf07963
PB
12#ifndef QEMU_COMMON_H
13#define QEMU_COMMON_H
14
bfe7e449 15#include "qemu/osdep.h"
1de7afc9 16#include "qemu/typedefs.h"
fba0a593 17#include "qemu/fprintf-fn.h"
beb6f0de 18
332ae28d
PB
19#if defined(__arm__) || defined(__sparc__) || defined(__mips__) || defined(__hppa__) || defined(__ia64__)
20#define WORDS_ALIGNED
21#endif
22
082b5557 23#define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
24ebf5f3 24
d63c9477 25#include "glib-compat.h"
1ad9580b 26#include "qemu/option.h"
8f1ed5f5 27#include "qemu/host-utils.h"
faf07963 28
c0fd260e
SW
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
34#else
35# error Unknown pointer size
36#endif
37
4603ea01
PD
38void cpu_ticks_init(void);
39
946fb27c 40/* icount */
1ad9580b 41void configure_icount(QemuOpts *opts, Error **errp);
946fb27c 42extern int use_icount;
a8bfac37 43extern int icount_align_option;
27498bef
ST
44/* drift information for info jit command */
45extern int64_t max_delay;
46extern int64_t max_advance;
47void dump_drift_info(FILE *f, fprintf_function cpu_fprintf);
946fb27c 48
1de7afc9 49#include "qemu/bswap.h"
faf07963 50
9adea5f7
PB
51/* FIXME: Remove NEED_CPU_H. */
52#ifdef NEED_CPU_H
faf07963 53#include "cpu.h"
faf07963
PB
54#endif /* !defined(NEED_CPU_H) */
55
3bbbee18
AF
56/* main function, renamed */
57#if defined(CONFIG_COCOA)
58int qemu_main(int argc, char **argv, char **envp);
59#endif
60
f6503059
AZ
61void qemu_get_timedate(struct tm *tm, int offset);
62int qemu_timedate_diff(struct tm *tm);
63
c8057f95
PM
64/**
65 * is_help_option:
66 * @s: string to test
67 *
68 * Check whether @s is one of the standard strings which indicate
69 * that the user is asking for a list of the valid values for a
70 * command option like -cpu or -M. The current accepted strings
71 * are 'help' and '?'. '?' is deprecated (it is a shell wildcard
72 * which makes it annoying to use in a reliable way) but provided
73 * for backwards compatibility.
74 *
75 * Returns: true if @s is a request for a list.
76 */
77static inline bool is_help_option(const char *s)
78{
79 return !strcmp(s, "?") || !strcmp(s, "help");
80}
81
ab603663
PM
82/* util/cutils.c */
83/**
84 * pstrcpy:
85 * @buf: buffer to copy string into
86 * @buf_size: size of @buf in bytes
87 * @str: string to copy
88 *
89 * Copy @str into @buf, including the trailing NUL, but do not
90 * write more than @buf_size bytes. The resulting buffer is
91 * always NUL terminated (even if the source string was too long).
92 * If @buf_size is zero or negative then no bytes are copied.
93 *
94 * This function is similar to strncpy(), but avoids two of that
95 * function's problems:
96 * * if @str fits in the buffer, pstrcpy() does not zero-fill the
97 * remaining space at the end of @buf
98 * * if @str is too long, pstrcpy() will copy the first @buf_size-1
99 * bytes and then add a NUL
100 */
faf07963 101void pstrcpy(char *buf, int buf_size, const char *str);
ab603663
PM
102/**
103 * strpadcpy:
104 * @buf: buffer to copy string into
105 * @buf_size: size of @buf in bytes
106 * @str: string to copy
107 * @pad: character to pad the remainder of @buf with
108 *
109 * Copy @str into @buf (but *not* its trailing NUL!), and then pad the
110 * rest of the buffer with the @pad character. If @str is too large
111 * for the buffer then it is truncated, so that @buf contains the
112 * first @buf_size characters of @str, with no terminator.
113 */
2a025ae4 114void strpadcpy(char *buf, int buf_size, const char *str, char pad);
ab603663
PM
115/**
116 * pstrcat:
117 * @buf: buffer containing existing string
118 * @buf_size: size of @buf in bytes
119 * @s: string to concatenate to @buf
120 *
121 * Append a copy of @s to the string already in @buf, but do not
122 * allow the buffer to overflow. If the existing contents of @buf
123 * plus @str would total more than @buf_size bytes, then write
124 * as much of @str as will fit followed by a NUL terminator.
125 *
126 * @buf must already contain a NUL-terminated string, or the
127 * behaviour is undefined.
128 *
129 * Returns: @buf.
130 */
faf07963 131char *pstrcat(char *buf, int buf_size, const char *s);
ab603663
PM
132/**
133 * strstart:
134 * @str: string to test
135 * @val: prefix string to look for
136 * @ptr: NULL, or pointer to be written to indicate start of
137 * the remainder of the string
138 *
139 * Test whether @str starts with the prefix @val.
140 * If it does (including the degenerate case where @str and @val
141 * are equal) then return true. If @ptr is not NULL then a
142 * pointer to the first character following the prefix is written
143 * to it. If @val is not a prefix of @str then return false (and
144 * @ptr is not written to).
145 *
146 * Returns: true if @str starts with prefix @val, false otherwise.
147 */
faf07963 148int strstart(const char *str, const char *val, const char **ptr);
ab603663
PM
149/**
150 * stristart:
151 * @str: string to test
152 * @val: prefix string to look for
153 * @ptr: NULL, or pointer to be written to indicate start of
154 * the remainder of the string
155 *
156 * Test whether @str starts with the case-insensitive prefix @val.
157 * This function behaves identically to strstart(), except that the
158 * comparison is made after calling qemu_toupper() on each pair of
159 * characters.
160 *
161 * Returns: true if @str starts with case-insensitive prefix @val,
162 * false otherwise.
163 */
faf07963 164int stristart(const char *str, const char *val, const char **ptr);
ab603663
PM
165/**
166 * qemu_strnlen:
167 * @s: string
168 * @max_len: maximum number of bytes in @s to scan
169 *
170 * Return the length of the string @s, like strlen(), but do not
171 * examine more than @max_len bytes of the memory pointed to by @s.
172 * If no NUL terminator is found within @max_len bytes, then return
173 * @max_len instead.
174 *
175 * This function has the same behaviour as the POSIX strnlen()
176 * function.
177 *
178 * Returns: length of @s in bytes, or @max_len, whichever is smaller.
179 */
d43277c5 180int qemu_strnlen(const char *s, int max_len);
ab603663
PM
181/**
182 * qemu_strsep:
183 * @input: pointer to string to parse
184 * @delim: string containing delimiter characters to search for
185 *
186 * Locate the first occurrence of any character in @delim within
187 * the string referenced by @input, and replace it with a NUL.
188 * The location of the next character after the delimiter character
189 * is stored into @input.
190 * If the end of the string was reached without finding a delimiter
191 * character, then NULL is stored into @input.
192 * If @input points to a NULL pointer on entry, return NULL.
193 * The return value is always the original value of *@input (and
194 * so now points to a NUL-terminated string corresponding to the
195 * part of the input up to the first delimiter).
196 *
197 * This function has the same behaviour as the BSD strsep() function.
198 *
199 * Returns: the pointer originally in @input.
200 */
a38ed811 201char *qemu_strsep(char **input, const char *delim);
faf07963 202time_t mktimegm(struct tm *tm);
6f1953c4 203int qemu_fdatasync(int fd);
db1a4972 204int fcntl_setfl(int fd, int flag);
443916d1 205int qemu_parse_fd(const char *param);
764e0fa4
CT
206int qemu_strtol(const char *nptr, const char **endptr, int base,
207 long *result);
c817c015
CT
208int qemu_strtoul(const char *nptr, const char **endptr, int base,
209 unsigned long *result);
8ac4df40
CT
210int qemu_strtoll(const char *nptr, const char **endptr, int base,
211 int64_t *result);
3904e6bf
CT
212int qemu_strtoull(const char *nptr, const char **endptr, int base,
213 uint64_t *result);
d8427002 214
e3f9fe2d
EH
215int parse_uint(const char *s, unsigned long long *value, char **endptr,
216 int base);
217int parse_uint_full(const char *s, unsigned long long *value, int base);
218
d7142456
JS
219/*
220 * strtosz() suffixes used to specify the default treatment of an
221 * argument passed to strtosz() without an explicit suffix.
222 * These should be defined using upper case characters in the range
223 * A-Z, as strtosz() will use qemu_toupper() on the given argument
224 * prior to comparison.
225 */
5e00984a
KW
226#define STRTOSZ_DEFSUFFIX_EB 'E'
227#define STRTOSZ_DEFSUFFIX_PB 'P'
d8427002
JS
228#define STRTOSZ_DEFSUFFIX_TB 'T'
229#define STRTOSZ_DEFSUFFIX_GB 'G'
230#define STRTOSZ_DEFSUFFIX_MB 'M'
231#define STRTOSZ_DEFSUFFIX_KB 'K'
232#define STRTOSZ_DEFSUFFIX_B 'B'
70b4f4bb
JS
233int64_t strtosz(const char *nptr, char **end);
234int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix);
a732e1ba
JR
235int64_t strtosz_suffix_unit(const char *nptr, char **end,
236 const char default_suffix, int64_t unit);
076b35b5
ND
237#define K_BYTE (1ULL << 10)
238#define M_BYTE (1ULL << 20)
239#define G_BYTE (1ULL << 30)
240#define T_BYTE (1ULL << 40)
241#define P_BYTE (1ULL << 50)
242#define E_BYTE (1ULL << 60)
faf07963 243
44e3e053
WX
244/* used to print char* safely */
245#define STR_OR_NULL(str) ((str) ? (str) : "null")
246
f5bebbbb
MA
247/* id.c */
248bool id_wellformed(const char *id);
249
37022086
BS
250/* path.c */
251void init_paths(const char *prefix);
252const char *path(const char *pathname);
253
cd390083
BS
254#define qemu_isalnum(c) isalnum((unsigned char)(c))
255#define qemu_isalpha(c) isalpha((unsigned char)(c))
256#define qemu_iscntrl(c) iscntrl((unsigned char)(c))
257#define qemu_isdigit(c) isdigit((unsigned char)(c))
258#define qemu_isgraph(c) isgraph((unsigned char)(c))
259#define qemu_islower(c) islower((unsigned char)(c))
260#define qemu_isprint(c) isprint((unsigned char)(c))
261#define qemu_ispunct(c) ispunct((unsigned char)(c))
262#define qemu_isspace(c) isspace((unsigned char)(c))
263#define qemu_isupper(c) isupper((unsigned char)(c))
264#define qemu_isxdigit(c) isxdigit((unsigned char)(c))
265#define qemu_tolower(c) tolower((unsigned char)(c))
266#define qemu_toupper(c) toupper((unsigned char)(c))
267#define qemu_isascii(c) isascii((unsigned char)(c))
268#define qemu_toascii(c) toascii((unsigned char)(c))
269
b152aa84 270void *qemu_oom_check(void *ptr);
ca10f867 271
7c7c0629
JQ
272ssize_t qemu_write_full(int fd, const void *buf, size_t count)
273 QEMU_WARN_UNUSED_RESULT;
40ff6d7e
KW
274
275#ifndef _WIN32
276int qemu_pipe(int pipefd[2]);
4efeabbb
MT
277/* like openpty() but also makes it raw; return master fd */
278int qemu_openpty_raw(int *aslave, char *pty_name);
40ff6d7e
KW
279#endif
280
00aa0040 281#ifdef _WIN32
58455eb9
SW
282/* MinGW needs type casts for the 'buf' and 'optval' arguments. */
283#define qemu_getsockopt(sockfd, level, optname, optval, optlen) \
284 getsockopt(sockfd, level, optname, (void *)optval, optlen)
285#define qemu_setsockopt(sockfd, level, optname, optval, optlen) \
286 setsockopt(sockfd, level, optname, (const void *)optval, optlen)
00aa0040 287#define qemu_recv(sockfd, buf, len, flags) recv(sockfd, (void *)buf, len, flags)
73062dfe
SW
288#define qemu_sendto(sockfd, buf, len, flags, destaddr, addrlen) \
289 sendto(sockfd, (const void *)buf, len, flags, destaddr, addrlen)
00aa0040 290#else
58455eb9
SW
291#define qemu_getsockopt(sockfd, level, optname, optval, optlen) \
292 getsockopt(sockfd, level, optname, optval, optlen)
293#define qemu_setsockopt(sockfd, level, optname, optval, optlen) \
294 setsockopt(sockfd, level, optname, optval, optlen)
00aa0040 295#define qemu_recv(sockfd, buf, len, flags) recv(sockfd, buf, len, flags)
73062dfe
SW
296#define qemu_sendto(sockfd, buf, len, flags, destaddr, addrlen) \
297 sendto(sockfd, buf, len, flags, destaddr, addrlen)
00aa0040
BS
298#endif
299
87ecb68b
PB
300/* Error handling. */
301
e5924d89 302void QEMU_NORETURN hw_error(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
87ecb68b 303
87ecb68b
PB
304struct ParallelIOArg {
305 void *buffer;
306 int count;
307};
308
309typedef int (*DMA_transfer_handler) (void *opaque, int nchan, int pos, int size);
310
186993ee
MT
311typedef uint64_t pcibus_t;
312
679042f0
AP
313typedef struct PCIHostDeviceAddress {
314 unsigned int domain;
315 unsigned int bus;
316 unsigned int slot;
317 unsigned int function;
318} PCIHostDeviceAddress;
319
d5ab9713
JK
320void tcg_exec_init(unsigned long tb_size);
321bool tcg_enabled(void);
322
323void cpu_exec_init_all(void);
d2053c3c 324
b3c7724c 325/* CPU save/load. */
8d0f2bae 326#ifdef CPU_SAVE_VERSION
b3c7724c
PB
327void cpu_save(QEMUFile *f, void *opaque);
328int cpu_load(QEMUFile *f, void *opaque, int version_id);
8d0f2bae 329#endif
b3c7724c 330
8edac960 331/* Unblock cpu */
46d62fac 332void qemu_cpu_kick_self(void);
8edac960 333
e82bcec2
MT
334/* work queue */
335struct qemu_work_item {
336 struct qemu_work_item *next;
337 void (*func)(void *data);
338 void *data;
339 int done;
3c02270d 340 bool free;
e82bcec2
MT
341};
342
8c5135f9
PB
343
344/**
2fc8ae1d
MT
345 * Sends a (part of) iovec down a socket, yielding when the socket is full, or
346 * Receives data into a (part of) iovec from a socket,
347 * yielding when there is no data in the socket.
348 * The same interface as qemu_sendv_recvv(), with added yielding.
349 * XXX should mark these as coroutine_fn
8c5135f9 350 */
2fc8ae1d
MT
351ssize_t qemu_co_sendv_recvv(int sockfd, struct iovec *iov, unsigned iov_cnt,
352 size_t offset, size_t bytes, bool do_send);
353#define qemu_co_recvv(sockfd, iov, iov_cnt, offset, bytes) \
354 qemu_co_sendv_recvv(sockfd, iov, iov_cnt, offset, bytes, false)
355#define qemu_co_sendv(sockfd, iov, iov_cnt, offset, bytes) \
356 qemu_co_sendv_recvv(sockfd, iov, iov_cnt, offset, bytes, true)
8c5135f9
PB
357
358/**
2fc8ae1d 359 * The same as above, but with just a single buffer
8c5135f9 360 */
2fc8ae1d
MT
361ssize_t qemu_co_send_recv(int sockfd, void *buf, size_t bytes, bool do_send);
362#define qemu_co_recv(sockfd, buf, bytes) \
363 qemu_co_send_recv(sockfd, buf, bytes, false)
364#define qemu_co_send(sockfd, buf, bytes) \
365 qemu_co_send_recv(sockfd, buf, bytes, true)
8c5135f9 366
44e3ee8a
AL
367typedef struct QEMUIOVector {
368 struct iovec *iov;
369 int niov;
370 int nalloc;
249aa745 371 size_t size;
44e3ee8a
AL
372} QEMUIOVector;
373
374void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint);
522584a5 375void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov);
44e3ee8a 376void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len);
1b093c48
MT
377void qemu_iovec_concat(QEMUIOVector *dst,
378 QEMUIOVector *src, size_t soffset, size_t sbytes);
519661ee
PB
379size_t qemu_iovec_concat_iov(QEMUIOVector *dst,
380 struct iovec *src_iov, unsigned int src_cnt,
381 size_t soffset, size_t sbytes);
43f35cb5 382bool qemu_iovec_is_zero(QEMUIOVector *qiov);
44e3ee8a 383void qemu_iovec_destroy(QEMUIOVector *qiov);
be959463 384void qemu_iovec_reset(QEMUIOVector *qiov);
d5e6b161
MT
385size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset,
386 void *buf, size_t bytes);
03396148
MT
387size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset,
388 const void *buf, size_t bytes);
3d9b4925
MT
389size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
390 int fillc, size_t bytes);
f70d7f7e
BC
391ssize_t qemu_iovec_compare(QEMUIOVector *a, QEMUIOVector *b);
392void qemu_iovec_clone(QEMUIOVector *dest, const QEMUIOVector *src, void *buf);
58f423fb 393void qemu_iovec_discard_back(QEMUIOVector *qiov, size_t bytes);
44e3ee8a 394
1a6d39fd
SH
395bool buffer_is_zero(const void *buf, size_t len);
396
6b837bc4
JS
397void qemu_progress_init(int enabled, float min_skip);
398void qemu_progress_end(void);
3bfe4dbf 399void qemu_progress_print(float delta, int max);
31459f46 400const char *qemu_get_vm_name(void);
6b837bc4 401
082b5557
BS
402#define QEMU_FILE_TYPE_BIOS 0
403#define QEMU_FILE_TYPE_KEYMAP 1
404char *qemu_find_file(int type, const char *name);
405
406/* OS specific functions */
407void os_setup_early_signal_handling(void);
10f5bff6 408char *os_find_datadir(void);
082b5557 409void os_parse_cmd_args(int index, const char *optarg);
082b5557 410
abd0c6bd
PB
411/* Convert a byte between binary and BCD. */
412static inline uint8_t to_bcd(uint8_t val)
413{
414 return ((val / 10) << 4) | (val % 10);
415}
416
417static inline uint8_t from_bcd(uint8_t val)
418{
419 return ((val >> 4) * 10) + (val & 0x0f);
420}
421
3951690a
SH
422/* Round number down to multiple */
423#define QEMU_ALIGN_DOWN(n, m) ((n) / (m) * (m))
424
425/* Round number up to multiple */
426#define QEMU_ALIGN_UP(n, m) QEMU_ALIGN_DOWN((n) + (m) - 1, (m))
427
1de7afc9 428#include "qemu/module.h"
0bfe3ca5 429
e6546bb9
OW
430/*
431 * Implementation of ULEB128 (http://en.wikipedia.org/wiki/LEB128)
432 * Input is limited to 14-bit numbers
433 */
434
435int uleb128_encode_small(uint8_t *out, uint32_t n);
436int uleb128_decode_small(const uint8_t *in, uint32_t *n);
437
cb2744ea
MA
438/* unicode.c */
439int mod_utf8_codepoint(const char *s, size_t n, char **end);
440
6ff66f50
PC
441/*
442 * Hexdump a buffer to a file. An optional string prefix is added to every line
443 */
444
3568ac2a 445void qemu_hexdump(const char *buf, FILE *fp, const char *prefix, size_t size);
6ff66f50 446
c61ca00a
PL
447/* vector definitions */
448#ifdef __ALTIVEC__
449#include <altivec.h>
8593e050
PB
450/* The altivec.h header says we're allowed to undef these for
451 * C++ compatibility. Here we don't care about C++, but we
452 * undef them anyway to avoid namespace pollution.
453 */
454#undef vector
455#undef pixel
456#undef bool
457#define VECTYPE __vector unsigned char
c61ca00a
PL
458#define SPLAT(p) vec_splat(vec_ld(0, p), 0)
459#define ALL_EQ(v1, v2) vec_all_eq(v1, v2)
34664507 460#define VEC_OR(v1, v2) ((v1) | (v2))
c61ca00a
PL
461/* altivec.h may redefine the bool macro as vector type.
462 * Reset it to POSIX semantics. */
c61ca00a
PL
463#define bool _Bool
464#elif defined __SSE2__
465#include <emmintrin.h>
466#define VECTYPE __m128i
467#define SPLAT(p) _mm_set1_epi8(*(p))
468#define ALL_EQ(v1, v2) (_mm_movemask_epi8(_mm_cmpeq_epi8(v1, v2)) == 0xFFFF)
34664507 469#define VEC_OR(v1, v2) (_mm_or_si128(v1, v2))
c61ca00a
PL
470#else
471#define VECTYPE unsigned long
472#define SPLAT(p) (*(p) * (~0UL / 255))
473#define ALL_EQ(v1, v2) ((v1) == (v2))
34664507 474#define VEC_OR(v1, v2) ((v1) | (v2))
c61ca00a
PL
475#endif
476
41a259bd
PL
477#define BUFFER_FIND_NONZERO_OFFSET_UNROLL_FACTOR 8
478static inline bool
479can_use_buffer_find_nonzero_offset(const void *buf, size_t len)
480{
481 return (len % (BUFFER_FIND_NONZERO_OFFSET_UNROLL_FACTOR
482 * sizeof(VECTYPE)) == 0
483 && ((uintptr_t) buf) % sizeof(VECTYPE) == 0);
484}
485size_t buffer_find_nonzero_offset(const void *buf, size_t len);
486
b16352ac
AL
487/*
488 * helper to parse debug environment variables
489 */
490int parse_debug_env(const char *name, int max, int initial);
491
4297c8ee
AK
492const char *qemu_ether_ntoa(const MACAddr *mac);
493
faf07963 494#endif