qemu-common.h 14.2 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/compiler.h"
K
Kevin Wolf 已提交
16
#include "config-host.h"
17
#include "qemu/typedefs.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

P
pbrook 已提交
25 26 27 28
/* we put basic includes here to avoid repeating them in device drivers */
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
P
Paul Brook 已提交
29
#include <stdbool.h>
P
pbrook 已提交
30
#include <string.h>
B
balrog 已提交
31
#include <strings.h>
P
pbrook 已提交
32 33 34 35 36 37 38 39
#include <inttypes.h>
#include <limits.h>
#include <time.h>
#include <ctype.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
40
#include <sys/time.h>
P
Paul Brook 已提交
41
#include <assert.h>
42
#include <signal.h>
43
#include "glib-compat.h"
44
#include "qemu/option.h"
P
pbrook 已提交
45

46
#ifdef _WIN32
47
#include "sysemu/os-win32.h"
48 49 50
#endif

#ifdef CONFIG_POSIX
51
#include "sysemu/os-posix.h"
52 53
#endif

P
pbrook 已提交
54 55 56 57 58 59
#ifndef O_LARGEFILE
#define O_LARGEFILE 0
#endif
#ifndef O_BINARY
#define O_BINARY 0
#endif
60 61 62
#ifndef MAP_ANONYMOUS
#define MAP_ANONYMOUS MAP_ANON
#endif
P
pbrook 已提交
63 64 65
#ifndef ENOMEDIUM
#define ENOMEDIUM ENODEV
#endif
66
#if !defined(ENOTSUP)
67 68
#define ENOTSUP 4096
#endif
69 70 71
#if !defined(ECANCELED)
#define ECANCELED 4097
#endif
72 73 74
#if !defined(EMEDIUMTYPE)
#define EMEDIUMTYPE 4098
#endif
G
Gerd Hoffmann 已提交
75 76 77
#ifndef TIME_MAX
#define TIME_MAX LONG_MAX
#endif
P
pbrook 已提交
78

79 80 81 82 83 84 85 86 87
/* 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

88 89 90
typedef int (*fprintf_function)(FILE *f, const char *fmt, ...)
    GCC_FMT_ATTR(2, 3);

P
pbrook 已提交
91 92
#ifdef _WIN32
#define fsync _commit
93 94 95
#if !defined(lseek)
# define lseek _lseeki64
#endif
96
int qemu_ftruncate64(int, int64_t);
97 98 99
#if !defined(ftruncate)
# define ftruncate qemu_ftruncate64
#endif
P
pbrook 已提交
100 101 102 103 104 105 106 107

static inline char *realpath(const char *path, char *resolved_path)
{
    _fullpath(resolved_path, path, _MAX_PATH);
    return resolved_path;
}
#endif

108 109
void cpu_ticks_init(void);

P
Paolo Bonzini 已提交
110
/* icount */
111
void configure_icount(QemuOpts *opts, Error **errp);
P
Paolo Bonzini 已提交
112
extern int use_icount;
113
extern int icount_align_option;
114 115 116 117
/* 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 已提交
118

119 120
#include "qemu/osdep.h"
#include "qemu/bswap.h"
P
pbrook 已提交
121

122 123
/* FIXME: Remove NEED_CPU_H.  */
#ifdef NEED_CPU_H
P
pbrook 已提交
124 125 126
#include "cpu.h"
#endif /* !defined(NEED_CPU_H) */

127 128 129 130 131
/* main function, renamed */
#if defined(CONFIG_COCOA)
int qemu_main(int argc, char **argv, char **envp);
#endif

132 133 134
void qemu_get_timedate(struct tm *tm, int offset);
int qemu_timedate_diff(struct tm *tm);

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
/**
 * 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");
}

P
pbrook 已提交
153 154
/* cutils.c */
void pstrcpy(char *buf, int buf_size, const char *str);
D
Dmitry Fleytman 已提交
155
void strpadcpy(char *buf, int buf_size, const char *str, char pad);
P
pbrook 已提交
156 157 158
char *pstrcat(char *buf, int buf_size, const char *s);
int strstart(const char *str, const char *val, const char **ptr);
int stristart(const char *str, const char *val, const char **ptr);
B
Blue Swirl 已提交
159
int qemu_strnlen(const char *s, int max_len);
160
char *qemu_strsep(char **input, const char *delim);
P
pbrook 已提交
161
time_t mktimegm(struct tm *tm);
B
blueswir1 已提交
162
int qemu_fls(int i);
163
int qemu_fdatasync(int fd);
P
Paolo Bonzini 已提交
164
int fcntl_setfl(int fd, int flag);
165
int qemu_parse_fd(const char *param);
J
Jes Sorensen 已提交
166

167 168 169 170
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);

171 172 173 174 175 176 177
/*
 * strtosz() suffixes used to specify the default treatment of an
 * argument passed to strtosz() without an explicit suffix.
 * These should be defined using upper case characters in the range
 * A-Z, as strtosz() will use qemu_toupper() on the given argument
 * prior to comparison.
 */
178 179
#define STRTOSZ_DEFSUFFIX_EB	'E'
#define STRTOSZ_DEFSUFFIX_PB	'P'
J
Jes Sorensen 已提交
180 181 182 183 184
#define STRTOSZ_DEFSUFFIX_TB	'T'
#define STRTOSZ_DEFSUFFIX_GB	'G'
#define STRTOSZ_DEFSUFFIX_MB	'M'
#define STRTOSZ_DEFSUFFIX_KB	'K'
#define STRTOSZ_DEFSUFFIX_B	'B'
185 186
int64_t strtosz(const char *nptr, char **end);
int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix);
187 188
int64_t strtosz_suffix_unit(const char *nptr, char **end,
                            const char default_suffix, int64_t unit);
P
pbrook 已提交
189

190 191 192
/* used to print char* safely */
#define STR_OR_NULL(str) ((str) ? (str) : "null")

193 194 195
/* id.c */
bool id_wellformed(const char *id);

B
Blue Swirl 已提交
196 197 198 199
/* path.c */
void init_paths(const char *prefix);
const char *path(const char *pathname);

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
#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 已提交
216
void *qemu_oom_check(void *ptr);
217

218 219
ssize_t qemu_write_full(int fd, const void *buf, size_t count)
    QEMU_WARN_UNUSED_RESULT;
220 221
ssize_t qemu_send_full(int fd, const void *buf, size_t count, int flags)
    QEMU_WARN_UNUSED_RESULT;
222
ssize_t qemu_recv_full(int fd, void *buf, size_t count, int flags)
223
    QEMU_WARN_UNUSED_RESULT;
K
Kevin Wolf 已提交
224 225 226

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

B
Blue Swirl 已提交
231
#ifdef _WIN32
232 233 234 235 236
/* 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 已提交
237
#define qemu_recv(sockfd, buf, len, flags) recv(sockfd, (void *)buf, len, flags)
238 239
#define qemu_sendto(sockfd, buf, len, flags, destaddr, addrlen) \
    sendto(sockfd, (const void *)buf, len, flags, destaddr, addrlen)
B
Blue Swirl 已提交
240
#else
241 242 243 244
#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 已提交
245
#define qemu_recv(sockfd, buf, len, flags) recv(sockfd, buf, len, flags)
246 247
#define qemu_sendto(sockfd, buf, len, flags, destaddr, addrlen) \
    sendto(sockfd, buf, len, flags, destaddr, addrlen)
B
Blue Swirl 已提交
248 249
#endif

P
pbrook 已提交
250 251
/* Error handling.  */

252
void QEMU_NORETURN hw_error(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
P
pbrook 已提交
253 254 255 256 257 258 259 260

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

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

261 262
typedef uint64_t pcibus_t;

263 264 265 266 267 268 269
typedef struct PCIHostDeviceAddress {
    unsigned int domain;
    unsigned int bus;
    unsigned int slot;
    unsigned int function;
} PCIHostDeviceAddress;

270 271 272 273
void tcg_exec_init(unsigned long tb_size);
bool tcg_enabled(void);

void cpu_exec_init_all(void);
274

275
/* CPU save/load.  */
276
#ifdef CPU_SAVE_VERSION
277 278
void cpu_save(QEMUFile *f, void *opaque);
int cpu_load(QEMUFile *f, void *opaque, int version_id);
279
#endif
280

281
/* Unblock cpu */
282
void qemu_cpu_kick_self(void);
283

M
Marcelo Tosatti 已提交
284 285 286 287 288 289
/* work queue */
struct qemu_work_item {
    struct qemu_work_item *next;
    void (*func)(void *data);
    void *data;
    int done;
C
Chegu Vinod 已提交
290
    bool free;
M
Marcelo Tosatti 已提交
291 292
};

293 294

/**
295 296 297 298 299
 * 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
300
 */
301 302 303 304 305 306
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)
307 308

/**
309
 * The same as above, but with just a single buffer
310
 */
311 312 313 314 315
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)
316

A
aliguori 已提交
317 318 319 320
typedef struct QEMUIOVector {
    struct iovec *iov;
    int niov;
    int nalloc;
321
    size_t size;
A
aliguori 已提交
322 323 324
} QEMUIOVector;

void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint);
325
void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov);
A
aliguori 已提交
326
void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len);
327 328
void qemu_iovec_concat(QEMUIOVector *dst,
                       QEMUIOVector *src, size_t soffset, size_t sbytes);
329 330 331
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 已提交
332
bool qemu_iovec_is_zero(QEMUIOVector *qiov);
A
aliguori 已提交
333
void qemu_iovec_destroy(QEMUIOVector *qiov);
A
aliguori 已提交
334
void qemu_iovec_reset(QEMUIOVector *qiov);
335 336
size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset,
                         void *buf, size_t bytes);
337 338
size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset,
                           const void *buf, size_t bytes);
339 340
size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
                         int fillc, size_t bytes);
341 342
ssize_t qemu_iovec_compare(QEMUIOVector *a, QEMUIOVector *b);
void qemu_iovec_clone(QEMUIOVector *dest, const QEMUIOVector *src, void *buf);
K
Kevin Wolf 已提交
343
void qemu_iovec_discard_back(QEMUIOVector *qiov, size_t bytes);
A
aliguori 已提交
344

345 346
bool buffer_is_zero(const void *buf, size_t len);

347 348
void qemu_progress_init(int enabled, float min_skip);
void qemu_progress_end(void);
349
void qemu_progress_print(float delta, int max);
350
const char *qemu_get_vm_name(void);
351

352 353 354 355 356 357
#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);
358
char *os_find_datadir(void);
359 360
void os_parse_cmd_args(int index, const char *optarg);

P
Paul Brook 已提交
361 362 363 364 365 366 367 368 369 370 371
/* 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);
}

372
/* compute with 96 bit intermediate result: (a*b)/c */
373 374 375 376 377 378
#ifdef CONFIG_INT128
static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
{
    return (__int128_t)a * b / c;
}
#else
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
{
    union {
        uint64_t ll;
        struct {
#ifdef HOST_WORDS_BIGENDIAN
            uint32_t high, low;
#else
            uint32_t low, high;
#endif
        } l;
    } u, res;
    uint64_t rl, rh;

    u.ll = a;
    rl = (uint64_t)u.l.low * (uint64_t)b;
    rh = (uint64_t)u.l.high * (uint64_t)b;
    rh += (rl >> 32);
    res.l.high = rh / c;
    res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
    return res.ll;
}
401
#endif
402

403 404 405 406 407 408
/* 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))

O
Orit Wasserman 已提交
409 410 411 412 413 414 415 416 417 418 419 420
static inline bool is_power_of_2(uint64_t value)
{
    if (!value) {
        return 0;
    }

    return !(value & (value - 1));
}

/* round down to the nearest power of 2*/
int64_t pow2floor(int64_t value);

421
#include "qemu/module.h"
A
Anthony Liguori 已提交
422

423 424 425 426 427 428 429 430
/*
 * 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);

431 432 433
/* unicode.c */
int mod_utf8_codepoint(const char *s, size_t n, char **end);

P
Peter Crosthwaite 已提交
434 435 436 437
/*
 * Hexdump a buffer to a file. An optional string prefix is added to every line
 */

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

440 441 442
/* vector definitions */
#ifdef __ALTIVEC__
#include <altivec.h>
443 444 445 446 447 448 449 450
/* 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
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
#define SPLAT(p)       vec_splat(vec_ld(0, p), 0)
#define ALL_EQ(v1, v2) vec_all_eq(v1, v2)
/* 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)
#else
#define VECTYPE        unsigned long
#define SPLAT(p)       (*(p) * (~0UL / 255))
#define ALL_EQ(v1, v2) ((v1) == (v2))
#endif

467 468 469 470 471 472 473 474 475 476
#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);

477 478 479 480 481
/*
 * helper to parse debug environment variables
 */
int parse_debug_env(const char *name, int max, int initial);

A
Alexey Kardashevskiy 已提交
482 483
const char *qemu_ether_ntoa(const MACAddr *mac);

P
pbrook 已提交
484
#endif