qemu-common.h 16.7 KB
Newer Older
O
Orit Wasserman 已提交
1

2 3 4 5 6 7 8 9 10 11
/* Common header file that is included by all of QEMU.
 *
 * This file is supposed to be included only by .c files. No header file should
 * depend on qemu-common.h, as this would easily lead to circular header
 * dependencies.
 *
 * If a header file uses a definition from qemu-common.h, that definition
 * must be moved to a separate header file, and the header that uses it
 * must include that header.
 */
P
pbrook 已提交
12 13 14
#ifndef QEMU_COMMON_H
#define QEMU_COMMON_H

15
#include "qemu/osdep.h"
16
#include "qemu/typedefs.h"
17
#include "qemu/fprintf-fn.h"
K
Kevin Wolf 已提交
18

19 20 21 22
#if defined(__arm__) || defined(__sparc__) || defined(__mips__) || defined(__hppa__) || defined(__ia64__)
#define WORDS_ALIGNED
#endif

23
#define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
P
Paolo Bonzini 已提交
24

25
#include "glib-compat.h"
26
#include "qemu/option.h"
27
#include "qemu/host-utils.h"
P
pbrook 已提交
28

29 30 31 32 33 34 35 36 37
/* HOST_LONG_BITS is the size of a native pointer in bits. */
#if UINTPTR_MAX == UINT32_MAX
# define HOST_LONG_BITS 32
#elif UINTPTR_MAX == UINT64_MAX
# define HOST_LONG_BITS 64
#else
# error Unknown pointer size
#endif

38 39
void cpu_ticks_init(void);

P
Paolo Bonzini 已提交
40
/* icount */
41
void configure_icount(QemuOpts *opts, Error **errp);
P
Paolo Bonzini 已提交
42
extern int use_icount;
43
extern int icount_align_option;
44 45 46 47
/* drift information for info jit command */
extern int64_t max_delay;
extern int64_t max_advance;
void dump_drift_info(FILE *f, fprintf_function cpu_fprintf);
P
Paolo Bonzini 已提交
48

49
#include "qemu/bswap.h"
P
pbrook 已提交
50

51 52
/* FIXME: Remove NEED_CPU_H.  */
#ifdef NEED_CPU_H
P
pbrook 已提交
53 54 55
#include "cpu.h"
#endif /* !defined(NEED_CPU_H) */

56 57 58 59 60
/* main function, renamed */
#if defined(CONFIG_COCOA)
int qemu_main(int argc, char **argv, char **envp);
#endif

61 62 63
void qemu_get_timedate(struct tm *tm, int offset);
int qemu_timedate_diff(struct tm *tm);

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
/**
 * is_help_option:
 * @s: string to test
 *
 * Check whether @s is one of the standard strings which indicate
 * that the user is asking for a list of the valid values for a
 * command option like -cpu or -M. The current accepted strings
 * are 'help' and '?'. '?' is deprecated (it is a shell wildcard
 * which makes it annoying to use in a reliable way) but provided
 * for backwards compatibility.
 *
 * Returns: true if @s is a request for a list.
 */
static inline bool is_help_option(const char *s)
{
    return !strcmp(s, "?") || !strcmp(s, "help");
}

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
/* util/cutils.c */
/**
 * pstrcpy:
 * @buf: buffer to copy string into
 * @buf_size: size of @buf in bytes
 * @str: string to copy
 *
 * Copy @str into @buf, including the trailing NUL, but do not
 * write more than @buf_size bytes. The resulting buffer is
 * always NUL terminated (even if the source string was too long).
 * If @buf_size is zero or negative then no bytes are copied.
 *
 * This function is similar to strncpy(), but avoids two of that
 * function's problems:
 *  * if @str fits in the buffer, pstrcpy() does not zero-fill the
 *    remaining space at the end of @buf
 *  * if @str is too long, pstrcpy() will copy the first @buf_size-1
 *    bytes and then add a NUL
 */
P
pbrook 已提交
101
void pstrcpy(char *buf, int buf_size, const char *str);
102 103 104 105 106 107 108 109 110 111 112 113
/**
 * strpadcpy:
 * @buf: buffer to copy string into
 * @buf_size: size of @buf in bytes
 * @str: string to copy
 * @pad: character to pad the remainder of @buf with
 *
 * Copy @str into @buf (but *not* its trailing NUL!), and then pad the
 * rest of the buffer with the @pad character. If @str is too large
 * for the buffer then it is truncated, so that @buf contains the
 * first @buf_size characters of @str, with no terminator.
 */
D
Dmitry Fleytman 已提交
114
void strpadcpy(char *buf, int buf_size, const char *str, char pad);
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
/**
 * pstrcat:
 * @buf: buffer containing existing string
 * @buf_size: size of @buf in bytes
 * @s: string to concatenate to @buf
 *
 * Append a copy of @s to the string already in @buf, but do not
 * allow the buffer to overflow. If the existing contents of @buf
 * plus @str would total more than @buf_size bytes, then write
 * as much of @str as will fit followed by a NUL terminator.
 *
 * @buf must already contain a NUL-terminated string, or the
 * behaviour is undefined.
 *
 * Returns: @buf.
 */
P
pbrook 已提交
131
char *pstrcat(char *buf, int buf_size, const char *s);
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
/**
 * strstart:
 * @str: string to test
 * @val: prefix string to look for
 * @ptr: NULL, or pointer to be written to indicate start of
 *       the remainder of the string
 *
 * Test whether @str starts with the prefix @val.
 * If it does (including the degenerate case where @str and @val
 * are equal) then return true. If @ptr is not NULL then a
 * pointer to the first character following the prefix is written
 * to it. If @val is not a prefix of @str then return false (and
 * @ptr is not written to).
 *
 * Returns: true if @str starts with prefix @val, false otherwise.
 */
P
pbrook 已提交
148
int strstart(const char *str, const char *val, const char **ptr);
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
/**
 * stristart:
 * @str: string to test
 * @val: prefix string to look for
 * @ptr: NULL, or pointer to be written to indicate start of
 *       the remainder of the string
 *
 * Test whether @str starts with the case-insensitive prefix @val.
 * This function behaves identically to strstart(), except that the
 * comparison is made after calling qemu_toupper() on each pair of
 * characters.
 *
 * Returns: true if @str starts with case-insensitive prefix @val,
 *          false otherwise.
 */
P
pbrook 已提交
164
int stristart(const char *str, const char *val, const char **ptr);
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
/**
 * qemu_strnlen:
 * @s: string
 * @max_len: maximum number of bytes in @s to scan
 *
 * Return the length of the string @s, like strlen(), but do not
 * examine more than @max_len bytes of the memory pointed to by @s.
 * If no NUL terminator is found within @max_len bytes, then return
 * @max_len instead.
 *
 * This function has the same behaviour as the POSIX strnlen()
 * function.
 *
 * Returns: length of @s in bytes, or @max_len, whichever is smaller.
 */
B
Blue Swirl 已提交
180
int qemu_strnlen(const char *s, int max_len);
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
/**
 * qemu_strsep:
 * @input: pointer to string to parse
 * @delim: string containing delimiter characters to search for
 *
 * Locate the first occurrence of any character in @delim within
 * the string referenced by @input, and replace it with a NUL.
 * The location of the next character after the delimiter character
 * is stored into @input.
 * If the end of the string was reached without finding a delimiter
 * character, then NULL is stored into @input.
 * If @input points to a NULL pointer on entry, return NULL.
 * The return value is always the original value of *@input (and
 * so now points to a NUL-terminated string corresponding to the
 * part of the input up to the first delimiter).
 *
 * This function has the same behaviour as the BSD strsep() function.
 *
 * Returns: the pointer originally in @input.
 */
201
char *qemu_strsep(char **input, const char *delim);
P
pbrook 已提交
202
time_t mktimegm(struct tm *tm);
203
int qemu_fdatasync(int fd);
P
Paolo Bonzini 已提交
204
int fcntl_setfl(int fd, int flag);
205
int qemu_parse_fd(const char *param);
206 207
int qemu_strtol(const char *nptr, const char **endptr, int base,
                long *result);
208 209
int qemu_strtoul(const char *nptr, const char **endptr, int base,
                 unsigned long *result);
210 211
int qemu_strtoll(const char *nptr, const char **endptr, int base,
                 int64_t *result);
212 213
int qemu_strtoull(const char *nptr, const char **endptr, int base,
                  uint64_t *result);
J
Jes Sorensen 已提交
214

215 216 217 218
int parse_uint(const char *s, unsigned long long *value, char **endptr,
               int base);
int parse_uint_full(const char *s, unsigned long long *value, int base);

219
/*
220 221
 * qemu_strtosz() suffixes used to specify the default treatment of an
 * argument passed to qemu_strtosz() without an explicit suffix.
222
 * These should be defined using upper case characters in the range
223
 * A-Z, as qemu_strtosz() will use qemu_toupper() on the given argument
224 225
 * prior to comparison.
 */
226 227 228 229 230 231 232 233 234 235 236
#define QEMU_STRTOSZ_DEFSUFFIX_EB 'E'
#define QEMU_STRTOSZ_DEFSUFFIX_PB 'P'
#define QEMU_STRTOSZ_DEFSUFFIX_TB 'T'
#define QEMU_STRTOSZ_DEFSUFFIX_GB 'G'
#define QEMU_STRTOSZ_DEFSUFFIX_MB 'M'
#define QEMU_STRTOSZ_DEFSUFFIX_KB 'K'
#define QEMU_STRTOSZ_DEFSUFFIX_B 'B'
int64_t qemu_strtosz(const char *nptr, char **end);
int64_t qemu_strtosz_suffix(const char *nptr, char **end,
                            const char default_suffix);
int64_t qemu_strtosz_suffix_unit(const char *nptr, char **end,
237
                            const char default_suffix, int64_t unit);
238 239 240 241 242 243
#define K_BYTE     (1ULL << 10)
#define M_BYTE     (1ULL << 20)
#define G_BYTE     (1ULL << 30)
#define T_BYTE     (1ULL << 40)
#define P_BYTE     (1ULL << 50)
#define E_BYTE     (1ULL << 60)
P
pbrook 已提交
244

245 246 247
/* used to print char* safely */
#define STR_OR_NULL(str) ((str) ? (str) : "null")

248
/* id.c */
249 250 251 252 253 254 255 256

typedef enum IdSubSystems {
    ID_QDEV,
    ID_BLOCK,
    ID_MAX      /* last element, used as array size */
} IdSubSystems;

char *id_generate(IdSubSystems id);
257 258
bool id_wellformed(const char *id);

B
Blue Swirl 已提交
259 260 261 262
/* path.c */
void init_paths(const char *prefix);
const char *path(const char *pathname);

263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
#define qemu_isalnum(c)		isalnum((unsigned char)(c))
#define qemu_isalpha(c)		isalpha((unsigned char)(c))
#define qemu_iscntrl(c)		iscntrl((unsigned char)(c))
#define qemu_isdigit(c)		isdigit((unsigned char)(c))
#define qemu_isgraph(c)		isgraph((unsigned char)(c))
#define qemu_islower(c)		islower((unsigned char)(c))
#define qemu_isprint(c)		isprint((unsigned char)(c))
#define qemu_ispunct(c)		ispunct((unsigned char)(c))
#define qemu_isspace(c)		isspace((unsigned char)(c))
#define qemu_isupper(c)		isupper((unsigned char)(c))
#define qemu_isxdigit(c)	isxdigit((unsigned char)(c))
#define qemu_tolower(c)		tolower((unsigned char)(c))
#define qemu_toupper(c)		toupper((unsigned char)(c))
#define qemu_isascii(c)		isascii((unsigned char)(c))
#define qemu_toascii(c)		toascii((unsigned char)(c))

J
Jes Sorensen 已提交
279
void *qemu_oom_check(void *ptr);
280

281 282
ssize_t qemu_write_full(int fd, const void *buf, size_t count)
    QEMU_WARN_UNUSED_RESULT;
K
Kevin Wolf 已提交
283 284 285

#ifndef _WIN32
int qemu_pipe(int pipefd[2]);
286 287
/* like openpty() but also makes it raw; return master fd */
int qemu_openpty_raw(int *aslave, char *pty_name);
K
Kevin Wolf 已提交
288 289
#endif

B
Blue Swirl 已提交
290
#ifdef _WIN32
291 292 293 294 295
/* MinGW needs type casts for the 'buf' and 'optval' arguments. */
#define qemu_getsockopt(sockfd, level, optname, optval, optlen) \
    getsockopt(sockfd, level, optname, (void *)optval, optlen)
#define qemu_setsockopt(sockfd, level, optname, optval, optlen) \
    setsockopt(sockfd, level, optname, (const void *)optval, optlen)
B
Blue Swirl 已提交
296
#define qemu_recv(sockfd, buf, len, flags) recv(sockfd, (void *)buf, len, flags)
297 298
#define qemu_sendto(sockfd, buf, len, flags, destaddr, addrlen) \
    sendto(sockfd, (const void *)buf, len, flags, destaddr, addrlen)
B
Blue Swirl 已提交
299
#else
300 301 302 303
#define qemu_getsockopt(sockfd, level, optname, optval, optlen) \
    getsockopt(sockfd, level, optname, optval, optlen)
#define qemu_setsockopt(sockfd, level, optname, optval, optlen) \
    setsockopt(sockfd, level, optname, optval, optlen)
B
Blue Swirl 已提交
304
#define qemu_recv(sockfd, buf, len, flags) recv(sockfd, buf, len, flags)
305 306
#define qemu_sendto(sockfd, buf, len, flags, destaddr, addrlen) \
    sendto(sockfd, buf, len, flags, destaddr, addrlen)
B
Blue Swirl 已提交
307 308
#endif

P
pbrook 已提交
309 310
/* Error handling.  */

311
void QEMU_NORETURN hw_error(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
P
pbrook 已提交
312 313 314 315 316 317 318 319

struct ParallelIOArg {
    void *buffer;
    int count;
};

typedef int (*DMA_transfer_handler) (void *opaque, int nchan, int pos, int size);

320 321
typedef uint64_t pcibus_t;

322 323 324 325 326 327 328
typedef struct PCIHostDeviceAddress {
    unsigned int domain;
    unsigned int bus;
    unsigned int slot;
    unsigned int function;
} PCIHostDeviceAddress;

329 330 331 332
void tcg_exec_init(unsigned long tb_size);
bool tcg_enabled(void);

void cpu_exec_init_all(void);
333

334
/* CPU save/load.  */
335
#ifdef CPU_SAVE_VERSION
336 337
void cpu_save(QEMUFile *f, void *opaque);
int cpu_load(QEMUFile *f, void *opaque, int version_id);
338
#endif
339

340
/* Unblock cpu */
341
void qemu_cpu_kick_self(void);
342

M
Marcelo Tosatti 已提交
343 344 345 346 347 348
/* work queue */
struct qemu_work_item {
    struct qemu_work_item *next;
    void (*func)(void *data);
    void *data;
    int done;
C
Chegu Vinod 已提交
349
    bool free;
M
Marcelo Tosatti 已提交
350 351
};

352 353

/**
354 355 356 357 358
 * Sends a (part of) iovec down a socket, yielding when the socket is full, or
 * Receives data into a (part of) iovec from a socket,
 * yielding when there is no data in the socket.
 * The same interface as qemu_sendv_recvv(), with added yielding.
 * XXX should mark these as coroutine_fn
359
 */
360 361 362 363 364 365
ssize_t qemu_co_sendv_recvv(int sockfd, struct iovec *iov, unsigned iov_cnt,
                            size_t offset, size_t bytes, bool do_send);
#define qemu_co_recvv(sockfd, iov, iov_cnt, offset, bytes) \
  qemu_co_sendv_recvv(sockfd, iov, iov_cnt, offset, bytes, false)
#define qemu_co_sendv(sockfd, iov, iov_cnt, offset, bytes) \
  qemu_co_sendv_recvv(sockfd, iov, iov_cnt, offset, bytes, true)
366 367

/**
368
 * The same as above, but with just a single buffer
369
 */
370 371 372 373 374
ssize_t qemu_co_send_recv(int sockfd, void *buf, size_t bytes, bool do_send);
#define qemu_co_recv(sockfd, buf, bytes) \
  qemu_co_send_recv(sockfd, buf, bytes, false)
#define qemu_co_send(sockfd, buf, bytes) \
  qemu_co_send_recv(sockfd, buf, bytes, true)
375

A
aliguori 已提交
376 377 378 379
typedef struct QEMUIOVector {
    struct iovec *iov;
    int niov;
    int nalloc;
380
    size_t size;
A
aliguori 已提交
381 382 383
} QEMUIOVector;

void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint);
384
void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov);
A
aliguori 已提交
385
void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len);
386 387
void qemu_iovec_concat(QEMUIOVector *dst,
                       QEMUIOVector *src, size_t soffset, size_t sbytes);
388 389 390
size_t qemu_iovec_concat_iov(QEMUIOVector *dst,
                             struct iovec *src_iov, unsigned int src_cnt,
                             size_t soffset, size_t sbytes);
P
Peter Lieven 已提交
391
bool qemu_iovec_is_zero(QEMUIOVector *qiov);
A
aliguori 已提交
392
void qemu_iovec_destroy(QEMUIOVector *qiov);
A
aliguori 已提交
393
void qemu_iovec_reset(QEMUIOVector *qiov);
394 395
size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset,
                         void *buf, size_t bytes);
396 397
size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset,
                           const void *buf, size_t bytes);
398 399
size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
                         int fillc, size_t bytes);
400 401
ssize_t qemu_iovec_compare(QEMUIOVector *a, QEMUIOVector *b);
void qemu_iovec_clone(QEMUIOVector *dest, const QEMUIOVector *src, void *buf);
K
Kevin Wolf 已提交
402
void qemu_iovec_discard_back(QEMUIOVector *qiov, size_t bytes);
A
aliguori 已提交
403

404 405
bool buffer_is_zero(const void *buf, size_t len);

406 407
void qemu_progress_init(int enabled, float min_skip);
void qemu_progress_end(void);
408
void qemu_progress_print(float delta, int max);
409
const char *qemu_get_vm_name(void);
410

411 412 413 414 415 416
#define QEMU_FILE_TYPE_BIOS   0
#define QEMU_FILE_TYPE_KEYMAP 1
char *qemu_find_file(int type, const char *name);

/* OS specific functions */
void os_setup_early_signal_handling(void);
417
char *os_find_datadir(void);
418 419
void os_parse_cmd_args(int index, const char *optarg);

P
Paul Brook 已提交
420 421 422 423 424 425 426 427 428 429 430
/* Convert a byte between binary and BCD.  */
static inline uint8_t to_bcd(uint8_t val)
{
    return ((val / 10) << 4) | (val % 10);
}

static inline uint8_t from_bcd(uint8_t val)
{
    return ((val >> 4) * 10) + (val & 0x0f);
}

431 432 433 434 435 436
/* Round number down to multiple */
#define QEMU_ALIGN_DOWN(n, m) ((n) / (m) * (m))

/* Round number up to multiple */
#define QEMU_ALIGN_UP(n, m) QEMU_ALIGN_DOWN((n) + (m) - 1, (m))

437
#include "qemu/module.h"
A
Anthony Liguori 已提交
438

439 440 441 442 443 444 445 446
/*
 * Implementation of ULEB128 (http://en.wikipedia.org/wiki/LEB128)
 * Input is limited to 14-bit numbers
 */

int uleb128_encode_small(uint8_t *out, uint32_t n);
int uleb128_decode_small(const uint8_t *in, uint32_t *n);

447 448 449
/* unicode.c */
int mod_utf8_codepoint(const char *s, size_t n, char **end);

P
Peter Crosthwaite 已提交
450 451 452 453
/*
 * Hexdump a buffer to a file. An optional string prefix is added to every line
 */

454
void qemu_hexdump(const char *buf, FILE *fp, const char *prefix, size_t size);
P
Peter Crosthwaite 已提交
455

456 457 458
/* vector definitions */
#ifdef __ALTIVEC__
#include <altivec.h>
459 460 461 462 463 464 465 466
/* The altivec.h header says we're allowed to undef these for
 * C++ compatibility.  Here we don't care about C++, but we
 * undef them anyway to avoid namespace pollution.
 */
#undef vector
#undef pixel
#undef bool
#define VECTYPE        __vector unsigned char
467 468
#define SPLAT(p)       vec_splat(vec_ld(0, p), 0)
#define ALL_EQ(v1, v2) vec_all_eq(v1, v2)
A
Artyom Tarasenko 已提交
469
#define VEC_OR(v1, v2) ((v1) | (v2))
470 471 472 473 474 475 476 477
/* altivec.h may redefine the bool macro as vector type.
 * Reset it to POSIX semantics. */
#define bool _Bool
#elif defined __SSE2__
#include <emmintrin.h>
#define VECTYPE        __m128i
#define SPLAT(p)       _mm_set1_epi8(*(p))
#define ALL_EQ(v1, v2) (_mm_movemask_epi8(_mm_cmpeq_epi8(v1, v2)) == 0xFFFF)
A
Artyom Tarasenko 已提交
478
#define VEC_OR(v1, v2) (_mm_or_si128(v1, v2))
479 480 481 482
#else
#define VECTYPE        unsigned long
#define SPLAT(p)       (*(p) * (~0UL / 255))
#define ALL_EQ(v1, v2) ((v1) == (v2))
A
Artyom Tarasenko 已提交
483
#define VEC_OR(v1, v2) ((v1) | (v2))
484 485
#endif

486 487 488 489 490 491 492 493 494 495
#define BUFFER_FIND_NONZERO_OFFSET_UNROLL_FACTOR 8
static inline bool
can_use_buffer_find_nonzero_offset(const void *buf, size_t len)
{
    return (len % (BUFFER_FIND_NONZERO_OFFSET_UNROLL_FACTOR
                   * sizeof(VECTYPE)) == 0
            && ((uintptr_t) buf) % sizeof(VECTYPE) == 0);
}
size_t buffer_find_nonzero_offset(const void *buf, size_t len);

496 497 498 499 500
/*
 * helper to parse debug environment variables
 */
int parse_debug_env(const char *name, int max, int initial);

A
Alexey Kardashevskiy 已提交
501 502
const char *qemu_ether_ntoa(const MACAddr *mac);

P
pbrook 已提交
503
#endif