qemu-common.h 11.6 KB
Newer Older
P
pbrook 已提交
1 2 3 4
/* Common header file that is included by all of qemu.  */
#ifndef QEMU_COMMON_H
#define QEMU_COMMON_H

5
#include "compiler.h"
K
Kevin Wolf 已提交
6 7
#include "config-host.h"

8 9 10 11
#if defined(__arm__) || defined(__sparc__) || defined(__mips__) || defined(__hppa__) || defined(__ia64__)
#define WORDS_ALIGNED
#endif

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

B
Blue Swirl 已提交
14 15
typedef struct QEMUTimer QEMUTimer;
typedef struct QEMUFile QEMUFile;
B
Blue Swirl 已提交
16
typedef struct DeviceState DeviceState;
B
Blue Swirl 已提交
17

18 19
struct Monitor;
typedef struct Monitor Monitor;
I
Isaku Yamahata 已提交
20
typedef struct MigrationParams MigrationParams;
21

P
pbrook 已提交
22 23 24 25
/* we put basic includes here to avoid repeating them in device drivers */
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
P
Paul Brook 已提交
26
#include <stdbool.h>
P
pbrook 已提交
27
#include <string.h>
B
balrog 已提交
28
#include <strings.h>
P
pbrook 已提交
29 30 31 32 33 34 35 36
#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>
37
#include <sys/time.h>
P
Paul Brook 已提交
38
#include <assert.h>
39
#include <signal.h>
40
#include <glib.h>
P
pbrook 已提交
41

42 43 44 45 46 47 48 49
#ifdef _WIN32
#include "qemu-os-win32.h"
#endif

#ifdef CONFIG_POSIX
#include "qemu-os-posix.h"
#endif

P
pbrook 已提交
50 51 52 53 54 55
#ifndef O_LARGEFILE
#define O_LARGEFILE 0
#endif
#ifndef O_BINARY
#define O_BINARY 0
#endif
56 57 58
#ifndef MAP_ANONYMOUS
#define MAP_ANONYMOUS MAP_ANON
#endif
P
pbrook 已提交
59 60 61
#ifndef ENOMEDIUM
#define ENOMEDIUM ENODEV
#endif
62
#if !defined(ENOTSUP)
63 64
#define ENOTSUP 4096
#endif
65 66 67
#if !defined(ECANCELED)
#define ECANCELED 4097
#endif
G
Gerd Hoffmann 已提交
68 69 70
#ifndef TIME_MAX
#define TIME_MAX LONG_MAX
#endif
P
pbrook 已提交
71

72 73 74 75 76 77 78 79 80
/* 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

81 82
#ifndef CONFIG_IOVEC
#define CONFIG_IOVEC
A
aliguori 已提交
83 84 85 86
struct iovec {
    void *iov_base;
    size_t iov_len;
};
87 88 89 90
/*
 * Use the same value as Linux for now.
 */
#define IOV_MAX		1024
91 92
#else
#include <sys/uio.h>
A
aliguori 已提交
93 94
#endif

95 96 97
typedef int (*fprintf_function)(FILE *f, const char *fmt, ...)
    GCC_FMT_ATTR(2, 3);

P
pbrook 已提交
98 99
#ifdef _WIN32
#define fsync _commit
100 101 102
#if !defined(lseek)
# define lseek _lseeki64
#endif
103
int qemu_ftruncate64(int, int64_t);
104 105 106
#if !defined(ftruncate)
# define ftruncate qemu_ftruncate64
#endif
P
pbrook 已提交
107 108 109 110 111 112 113 114

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

P
Paolo Bonzini 已提交
115 116 117 118
/* icount */
void configure_icount(const char *option);
extern int use_icount;

P
pbrook 已提交
119 120 121 122 123 124 125 126 127 128 129 130
/* FIXME: Remove NEED_CPU_H.  */
#ifndef NEED_CPU_H

#include "osdep.h"
#include "bswap.h"

#else

#include "cpu.h"

#endif /* !defined(NEED_CPU_H) */

131 132 133 134 135
/* main function, renamed */
#if defined(CONFIG_COCOA)
int qemu_main(int argc, char **argv, char **envp);
#endif

136 137 138
void qemu_get_timedate(struct tm *tm, int offset);
int qemu_timedate_diff(struct tm *tm);

P
pbrook 已提交
139 140
/* cutils.c */
void pstrcpy(char *buf, int buf_size, const char *str);
D
Dmitry Fleytman 已提交
141
void strpadcpy(char *buf, int buf_size, const char *str, char pad);
P
pbrook 已提交
142 143 144
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 已提交
145
int qemu_strnlen(const char *s, int max_len);
P
pbrook 已提交
146
time_t mktimegm(struct tm *tm);
B
blueswir1 已提交
147
int qemu_fls(int i);
148
int qemu_fdatasync(int fd);
P
Paolo Bonzini 已提交
149
int fcntl_setfl(int fd, int flag);
150
int qemu_parse_fd(const char *param);
J
Jes Sorensen 已提交
151

152 153 154 155 156 157 158
/*
 * 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.
 */
J
Jes Sorensen 已提交
159 160 161 162 163
#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'
164 165
int64_t strtosz(const char *nptr, char **end);
int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix);
166 167
int64_t strtosz_suffix_unit(const char *nptr, char **end,
                            const char default_suffix, int64_t unit);
P
pbrook 已提交
168

B
Blue Swirl 已提交
169 170 171 172
/* path.c */
void init_paths(const char *prefix);
const char *path(const char *pathname);

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
#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 已提交
189
void *qemu_oom_check(void *ptr);
190

K
Kevin Wolf 已提交
191
int qemu_open(const char *name, int flags, ...);
192 193
ssize_t qemu_write_full(int fd, const void *buf, size_t count)
    QEMU_WARN_UNUSED_RESULT;
194 195
ssize_t qemu_send_full(int fd, const void *buf, size_t count, int flags)
    QEMU_WARN_UNUSED_RESULT;
196
ssize_t qemu_recv_full(int fd, void *buf, size_t count, int flags)
197
    QEMU_WARN_UNUSED_RESULT;
K
Kevin Wolf 已提交
198 199

#ifndef _WIN32
P
Paolo Bonzini 已提交
200
int qemu_eventfd(int pipefd[2]);
K
Kevin Wolf 已提交
201 202 203
int qemu_pipe(int pipefd[2]);
#endif

B
Blue Swirl 已提交
204 205 206 207 208 209
#ifdef _WIN32
#define qemu_recv(sockfd, buf, len, flags) recv(sockfd, (void *)buf, len, flags)
#else
#define qemu_recv(sockfd, buf, len, flags) recv(sockfd, buf, len, flags)
#endif

P
pbrook 已提交
210 211
/* Error handling.  */

212
void QEMU_NORETURN hw_error(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
P
pbrook 已提交
213 214 215 216 217 218 219 220 221 222 223

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

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

/* A load of opaque types so that device init declarations don't have to
   pull in all the real definitions.  */
typedef struct NICInfo NICInfo;
224
typedef struct HCIInfo HCIInfo;
P
pbrook 已提交
225 226
typedef struct AudioState AudioState;
typedef struct BlockDriverState BlockDriverState;
B
Blue Swirl 已提交
227
typedef struct DriveInfo DriveInfo;
P
pbrook 已提交
228
typedef struct DisplayState DisplayState;
229 230
typedef struct DisplayChangeListener DisplayChangeListener;
typedef struct DisplaySurface DisplaySurface;
231
typedef struct DisplayAllocator DisplayAllocator;
232
typedef struct PixelFormat PixelFormat;
P
pbrook 已提交
233
typedef struct TextConsole TextConsole;
234
typedef TextConsole QEMUConsole;
P
pbrook 已提交
235
typedef struct CharDriverState CharDriverState;
G
Gerd Hoffmann 已提交
236
typedef struct MACAddr MACAddr;
P
pbrook 已提交
237
typedef struct VLANState VLANState;
238
typedef struct VLANClientState VLANClientState;
P
pbrook 已提交
239
typedef struct i2c_bus i2c_bus;
240
typedef struct ISABus ISABus;
M
Markus Armbruster 已提交
241
typedef struct ISADevice ISADevice;
P
pbrook 已提交
242
typedef struct SMBusDevice SMBusDevice;
243 244
typedef struct PCIHostState PCIHostState;
typedef struct PCIExpressHost PCIExpressHost;
P
pbrook 已提交
245 246
typedef struct PCIBus PCIBus;
typedef struct PCIDevice PCIDevice;
247
typedef struct PCIExpressDevice PCIExpressDevice;
248
typedef struct PCIBridge PCIBridge;
249 250 251
typedef struct PCIEAERMsg PCIEAERMsg;
typedef struct PCIEAERLog PCIEAERLog;
typedef struct PCIEAERErr PCIEAERErr;
252 253
typedef struct PCIEPort PCIEPort;
typedef struct PCIESlot PCIESlot;
J
Jan Kiszka 已提交
254
typedef struct MSIMessage MSIMessage;
P
pbrook 已提交
255 256
typedef struct SerialState SerialState;
typedef struct IRQState *qemu_irq;
P
Paul Brook 已提交
257 258 259 260
typedef struct PCMCIACardState PCMCIACardState;
typedef struct MouseTransformInfo MouseTransformInfo;
typedef struct uWireSlave uWireSlave;
typedef struct I2SCodec I2SCodec;
P
Paul Brook 已提交
261
typedef struct SSIBus SSIBus;
262
typedef struct EventNotifier EventNotifier;
263
typedef struct VirtIODevice VirtIODevice;
G
Gerd Hoffmann 已提交
264
typedef struct QEMUSGList QEMUSGList;
265
typedef struct SHPCDevice SHPCDevice;
P
pbrook 已提交
266

267 268
typedef uint64_t pcibus_t;

269 270 271 272 273
typedef enum LostTickPolicy {
    LOST_TICK_DISCARD,
    LOST_TICK_DELAY,
    LOST_TICK_MERGE,
    LOST_TICK_SLEW,
274
    LOST_TICK_MAX
275 276
} LostTickPolicy;

277 278 279 280 281 282 283
typedef struct PCIHostDeviceAddress {
    unsigned int domain;
    unsigned int bus;
    unsigned int slot;
    unsigned int function;
} PCIHostDeviceAddress;

284 285 286 287
void tcg_exec_init(unsigned long tb_size);
bool tcg_enabled(void);

void cpu_exec_init_all(void);
288

289 290 291 292
/* CPU save/load.  */
void cpu_save(QEMUFile *f, void *opaque);
int cpu_load(QEMUFile *f, void *opaque, int version_id);

293 294
/* Unblock cpu */
void qemu_cpu_kick(void *env);
295
void qemu_cpu_kick_self(void);
J
Jan Kiszka 已提交
296
int qemu_cpu_is_self(void *env);
297

M
Marcelo Tosatti 已提交
298 299 300 301 302 303 304 305
/* work queue */
struct qemu_work_item {
    struct qemu_work_item *next;
    void (*func)(void *data);
    void *data;
    int done;
};

306 307 308 309 310 311
#ifdef CONFIG_USER_ONLY
#define qemu_init_vcpu(env) do { } while (0)
#else
void qemu_init_vcpu(void *env);
#endif

312 313

/**
314 315 316 317 318
 * 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
319
 */
320 321 322 323 324 325
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)
326 327

/**
328
 * The same as above, but with just a single buffer
329
 */
330 331 332 333 334
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)
335

A
aliguori 已提交
336 337 338 339
typedef struct QEMUIOVector {
    struct iovec *iov;
    int niov;
    int nalloc;
340
    size_t size;
A
aliguori 已提交
341 342 343
} QEMUIOVector;

void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint);
344
void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov);
A
aliguori 已提交
345
void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len);
346 347
void qemu_iovec_concat(QEMUIOVector *dst,
                       QEMUIOVector *src, size_t soffset, size_t sbytes);
A
aliguori 已提交
348
void qemu_iovec_destroy(QEMUIOVector *qiov);
A
aliguori 已提交
349
void qemu_iovec_reset(QEMUIOVector *qiov);
350 351
size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset,
                         void *buf, size_t bytes);
352 353
size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset,
                           const void *buf, size_t bytes);
354 355
size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
                         int fillc, size_t bytes);
A
aliguori 已提交
356

357 358
bool buffer_is_zero(const void *buf, size_t len);

359 360
void qemu_progress_init(int enabled, float min_skip);
void qemu_progress_end(void);
361
void qemu_progress_print(float delta, int max);
362

363 364 365 366 367 368 369 370 371 372
#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);
char *os_find_datadir(const char *argv0);
void os_parse_cmd_args(int index, const char *optarg);
void os_pidfile_error(void);

P
Paul Brook 已提交
373 374 375 376 377 378 379 380 381 382 383
/* 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);
}

384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
/* compute with 96 bit intermediate result: (a*b)/c */
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;
}

408 409 410 411 412 413
/* 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))

A
Anthony Liguori 已提交
414 415
#include "module.h"

P
pbrook 已提交
416
#endif