qemu/include/qemu-common.h
<<
>>
Prefs
   1
   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 */
  12#ifndef QEMU_COMMON_H
  13#define QEMU_COMMON_H
  14
  15#include "qemu/compiler.h"
  16#include "config-host.h"
  17#include "qemu/typedefs.h"
  18
  19#if defined(__arm__) || defined(__sparc__) || defined(__mips__) || defined(__hppa__) || defined(__ia64__)
  20#define WORDS_ALIGNED
  21#endif
  22
  23#define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
  24
  25/* we put basic includes here to avoid repeating them in device drivers */
  26#include <stdlib.h>
  27#include <stdio.h>
  28#include <stdarg.h>
  29#include <stdbool.h>
  30#include <string.h>
  31#include <strings.h>
  32#include <inttypes.h>
  33#include <limits.h>
  34#include <time.h>
  35#include <ctype.h>
  36#include <errno.h>
  37#include <unistd.h>
  38#include <fcntl.h>
  39#include <sys/stat.h>
  40#include <sys/time.h>
  41#include <assert.h>
  42#include <signal.h>
  43#include <glib.h>
  44
  45#ifdef _WIN32
  46#include "sysemu/os-win32.h"
  47#endif
  48
  49#ifdef CONFIG_POSIX
  50#include "sysemu/os-posix.h"
  51#endif
  52
  53#ifndef O_LARGEFILE
  54#define O_LARGEFILE 0
  55#endif
  56#ifndef O_BINARY
  57#define O_BINARY 0
  58#endif
  59#ifndef MAP_ANONYMOUS
  60#define MAP_ANONYMOUS MAP_ANON
  61#endif
  62#ifndef ENOMEDIUM
  63#define ENOMEDIUM ENODEV
  64#endif
  65#if !defined(ENOTSUP)
  66#define ENOTSUP 4096
  67#endif
  68#if !defined(ECANCELED)
  69#define ECANCELED 4097
  70#endif
  71#if !defined(EMEDIUMTYPE)
  72#define EMEDIUMTYPE 4098
  73#endif
  74#ifndef TIME_MAX
  75#define TIME_MAX LONG_MAX
  76#endif
  77
  78/* HOST_LONG_BITS is the size of a native pointer in bits. */
  79#if UINTPTR_MAX == UINT32_MAX
  80# define HOST_LONG_BITS 32
  81#elif UINTPTR_MAX == UINT64_MAX
  82# define HOST_LONG_BITS 64
  83#else
  84# error Unknown pointer size
  85#endif
  86
  87#ifndef CONFIG_IOVEC
  88#define CONFIG_IOVEC
  89struct iovec {
  90    void *iov_base;
  91    size_t iov_len;
  92};
  93/*
  94 * Use the same value as Linux for now.
  95 */
  96#define IOV_MAX         1024
  97#else
  98#include <sys/uio.h>
  99#endif
 100
 101typedef int (*fprintf_function)(FILE *f, const char *fmt, ...)
 102    GCC_FMT_ATTR(2, 3);
 103
 104#ifdef _WIN32
 105#define fsync _commit
 106#if !defined(lseek)
 107# define lseek _lseeki64
 108#endif
 109int qemu_ftruncate64(int, int64_t);
 110#if !defined(ftruncate)
 111# define ftruncate qemu_ftruncate64
 112#endif
 113
 114static inline char *realpath(const char *path, char *resolved_path)
 115{
 116    _fullpath(resolved_path, path, _MAX_PATH);
 117    return resolved_path;
 118}
 119#endif
 120
 121/* icount */
 122void configure_icount(const char *option);
 123extern int use_icount;
 124
 125/* FIXME: Remove NEED_CPU_H.  */
 126#ifndef NEED_CPU_H
 127
 128#include "qemu/osdep.h"
 129#include "qemu/bswap.h"
 130
 131#else
 132
 133#include "cpu.h"
 134
 135#endif /* !defined(NEED_CPU_H) */
 136
 137/* main function, renamed */
 138#if defined(CONFIG_COCOA)
 139int qemu_main(int argc, char **argv, char **envp);
 140#endif
 141
 142void qemu_get_timedate(struct tm *tm, int offset);
 143int qemu_timedate_diff(struct tm *tm);
 144
 145/**
 146 * is_help_option:
 147 * @s: string to test
 148 *
 149 * Check whether @s is one of the standard strings which indicate
 150 * that the user is asking for a list of the valid values for a
 151 * command option like -cpu or -M. The current accepted strings
 152 * are 'help' and '?'. '?' is deprecated (it is a shell wildcard
 153 * which makes it annoying to use in a reliable way) but provided
 154 * for backwards compatibility.
 155 *
 156 * Returns: true if @s is a request for a list.
 157 */
 158static inline bool is_help_option(const char *s)
 159{
 160    return !strcmp(s, "?") || !strcmp(s, "help");
 161}
 162
 163/* cutils.c */
 164void pstrcpy(char *buf, int buf_size, const char *str);
 165void strpadcpy(char *buf, int buf_size, const char *str, char pad);
 166char *pstrcat(char *buf, int buf_size, const char *s);
 167int strstart(const char *str, const char *val, const char **ptr);
 168int stristart(const char *str, const char *val, const char **ptr);
 169int qemu_strnlen(const char *s, int max_len);
 170time_t mktimegm(struct tm *tm);
 171int qemu_fls(int i);
 172int qemu_fdatasync(int fd);
 173int fcntl_setfl(int fd, int flag);
 174int qemu_parse_fd(const char *param);
 175
 176int parse_uint(const char *s, unsigned long long *value, char **endptr,
 177               int base);
 178int parse_uint_full(const char *s, unsigned long long *value, int base);
 179
 180/*
 181 * strtosz() suffixes used to specify the default treatment of an
 182 * argument passed to strtosz() without an explicit suffix.
 183 * These should be defined using upper case characters in the range
 184 * A-Z, as strtosz() will use qemu_toupper() on the given argument
 185 * prior to comparison.
 186 */
 187#define STRTOSZ_DEFSUFFIX_TB    'T'
 188#define STRTOSZ_DEFSUFFIX_GB    'G'
 189#define STRTOSZ_DEFSUFFIX_MB    'M'
 190#define STRTOSZ_DEFSUFFIX_KB    'K'
 191#define STRTOSZ_DEFSUFFIX_B     'B'
 192int64_t strtosz(const char *nptr, char **end);
 193int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix);
 194int64_t strtosz_suffix_unit(const char *nptr, char **end,
 195                            const char default_suffix, int64_t unit);
 196
 197/* path.c */
 198void init_paths(const char *prefix);
 199const char *path(const char *pathname);
 200
 201#define qemu_isalnum(c)         isalnum((unsigned char)(c))
 202#define qemu_isalpha(c)         isalpha((unsigned char)(c))
 203#define qemu_iscntrl(c)         iscntrl((unsigned char)(c))
 204#define qemu_isdigit(c)         isdigit((unsigned char)(c))
 205#define qemu_isgraph(c)         isgraph((unsigned char)(c))
 206#define qemu_islower(c)         islower((unsigned char)(c))
 207#define qemu_isprint(c)         isprint((unsigned char)(c))
 208#define qemu_ispunct(c)         ispunct((unsigned char)(c))
 209#define qemu_isspace(c)         isspace((unsigned char)(c))
 210#define qemu_isupper(c)         isupper((unsigned char)(c))
 211#define qemu_isxdigit(c)        isxdigit((unsigned char)(c))
 212#define qemu_tolower(c)         tolower((unsigned char)(c))
 213#define qemu_toupper(c)         toupper((unsigned char)(c))
 214#define qemu_isascii(c)         isascii((unsigned char)(c))
 215#define qemu_toascii(c)         toascii((unsigned char)(c))
 216
 217void *qemu_oom_check(void *ptr);
 218
 219ssize_t qemu_write_full(int fd, const void *buf, size_t count)
 220    QEMU_WARN_UNUSED_RESULT;
 221ssize_t qemu_send_full(int fd, const void *buf, size_t count, int flags)
 222    QEMU_WARN_UNUSED_RESULT;
 223ssize_t qemu_recv_full(int fd, void *buf, size_t count, int flags)
 224    QEMU_WARN_UNUSED_RESULT;
 225
 226#ifndef _WIN32
 227int qemu_pipe(int pipefd[2]);
 228#endif
 229
 230#ifdef _WIN32
 231/* MinGW needs type casts for the 'buf' and 'optval' arguments. */
 232#define qemu_getsockopt(sockfd, level, optname, optval, optlen) \
 233    getsockopt(sockfd, level, optname, (void *)optval, optlen)
 234#define qemu_setsockopt(sockfd, level, optname, optval, optlen) \
 235    setsockopt(sockfd, level, optname, (const void *)optval, optlen)
 236#define qemu_recv(sockfd, buf, len, flags) recv(sockfd, (void *)buf, len, flags)
 237#define qemu_sendto(sockfd, buf, len, flags, destaddr, addrlen) \
 238    sendto(sockfd, (const void *)buf, len, flags, destaddr, addrlen)
 239#else
 240#define qemu_getsockopt(sockfd, level, optname, optval, optlen) \
 241    getsockopt(sockfd, level, optname, optval, optlen)
 242#define qemu_setsockopt(sockfd, level, optname, optval, optlen) \
 243    setsockopt(sockfd, level, optname, optval, optlen)
 244#define qemu_recv(sockfd, buf, len, flags) recv(sockfd, buf, len, flags)
 245#define qemu_sendto(sockfd, buf, len, flags, destaddr, addrlen) \
 246    sendto(sockfd, buf, len, flags, destaddr, addrlen)
 247#endif
 248
 249/* Error handling.  */
 250
 251void QEMU_NORETURN hw_error(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
 252
 253struct ParallelIOArg {
 254    void *buffer;
 255    int count;
 256};
 257
 258typedef int (*DMA_transfer_handler) (void *opaque, int nchan, int pos, int size);
 259
 260typedef uint64_t pcibus_t;
 261
 262typedef enum LostTickPolicy {
 263    LOST_TICK_DISCARD,
 264    LOST_TICK_DELAY,
 265    LOST_TICK_MERGE,
 266    LOST_TICK_SLEW,
 267    LOST_TICK_MAX
 268} LostTickPolicy;
 269
 270typedef struct PCIHostDeviceAddress {
 271    unsigned int domain;
 272    unsigned int bus;
 273    unsigned int slot;
 274    unsigned int function;
 275} PCIHostDeviceAddress;
 276
 277void tcg_exec_init(unsigned long tb_size);
 278bool tcg_enabled(void);
 279
 280void cpu_exec_init_all(void);
 281
 282/* CPU save/load.  */
 283void cpu_save(QEMUFile *f, void *opaque);
 284int cpu_load(QEMUFile *f, void *opaque, int version_id);
 285
 286/* Unblock cpu */
 287void qemu_cpu_kick_self(void);
 288
 289/* work queue */
 290struct qemu_work_item {
 291    struct qemu_work_item *next;
 292    void (*func)(void *data);
 293    void *data;
 294    int done;
 295};
 296
 297#ifdef CONFIG_USER_ONLY
 298static inline void qemu_init_vcpu(void *env)
 299{
 300}
 301#else
 302void qemu_init_vcpu(void *env);
 303#endif
 304
 305
 306/**
 307 * Sends a (part of) iovec down a socket, yielding when the socket is full, or
 308 * Receives data into a (part of) iovec from a socket,
 309 * yielding when there is no data in the socket.
 310 * The same interface as qemu_sendv_recvv(), with added yielding.
 311 * XXX should mark these as coroutine_fn
 312 */
 313ssize_t qemu_co_sendv_recvv(int sockfd, struct iovec *iov, unsigned iov_cnt,
 314                            size_t offset, size_t bytes, bool do_send);
 315#define qemu_co_recvv(sockfd, iov, iov_cnt, offset, bytes) \
 316  qemu_co_sendv_recvv(sockfd, iov, iov_cnt, offset, bytes, false)
 317#define qemu_co_sendv(sockfd, iov, iov_cnt, offset, bytes) \
 318  qemu_co_sendv_recvv(sockfd, iov, iov_cnt, offset, bytes, true)
 319
 320/**
 321 * The same as above, but with just a single buffer
 322 */
 323ssize_t qemu_co_send_recv(int sockfd, void *buf, size_t bytes, bool do_send);
 324#define qemu_co_recv(sockfd, buf, bytes) \
 325  qemu_co_send_recv(sockfd, buf, bytes, false)
 326#define qemu_co_send(sockfd, buf, bytes) \
 327  qemu_co_send_recv(sockfd, buf, bytes, true)
 328
 329typedef struct QEMUIOVector {
 330    struct iovec *iov;
 331    int niov;
 332    int nalloc;
 333    size_t size;
 334} QEMUIOVector;
 335
 336void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint);
 337void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov);
 338void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len);
 339void qemu_iovec_concat(QEMUIOVector *dst,
 340                       QEMUIOVector *src, size_t soffset, size_t sbytes);
 341void qemu_iovec_concat_iov(QEMUIOVector *dst,
 342                           struct iovec *src_iov, unsigned int src_cnt,
 343                           size_t soffset, size_t sbytes);
 344void qemu_iovec_destroy(QEMUIOVector *qiov);
 345void qemu_iovec_reset(QEMUIOVector *qiov);
 346size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset,
 347                         void *buf, size_t bytes);
 348size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset,
 349                           const void *buf, size_t bytes);
 350size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
 351                         int fillc, size_t bytes);
 352
 353bool buffer_is_zero(const void *buf, size_t len);
 354
 355void qemu_progress_init(int enabled, float min_skip);
 356void qemu_progress_end(void);
 357void qemu_progress_print(float delta, int max);
 358const char *qemu_get_vm_name(void);
 359
 360#define QEMU_FILE_TYPE_BIOS   0
 361#define QEMU_FILE_TYPE_KEYMAP 1
 362char *qemu_find_file(int type, const char *name);
 363
 364/* OS specific functions */
 365void os_setup_early_signal_handling(void);
 366char *os_find_datadir(const char *argv0);
 367void os_parse_cmd_args(int index, const char *optarg);
 368void os_pidfile_error(void);
 369
 370/* Convert a byte between binary and BCD.  */
 371static inline uint8_t to_bcd(uint8_t val)
 372{
 373    return ((val / 10) << 4) | (val % 10);
 374}
 375
 376static inline uint8_t from_bcd(uint8_t val)
 377{
 378    return ((val >> 4) * 10) + (val & 0x0f);
 379}
 380
 381/* compute with 96 bit intermediate result: (a*b)/c */
 382static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
 383{
 384    union {
 385        uint64_t ll;
 386        struct {
 387#ifdef HOST_WORDS_BIGENDIAN
 388            uint32_t high, low;
 389#else
 390            uint32_t low, high;
 391#endif
 392        } l;
 393    } u, res;
 394    uint64_t rl, rh;
 395
 396    u.ll = a;
 397    rl = (uint64_t)u.l.low * (uint64_t)b;
 398    rh = (uint64_t)u.l.high * (uint64_t)b;
 399    rh += (rl >> 32);
 400    res.l.high = rh / c;
 401    res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
 402    return res.ll;
 403}
 404
 405/* Round number down to multiple */
 406#define QEMU_ALIGN_DOWN(n, m) ((n) / (m) * (m))
 407
 408/* Round number up to multiple */
 409#define QEMU_ALIGN_UP(n, m) QEMU_ALIGN_DOWN((n) + (m) - 1, (m))
 410
 411static inline bool is_power_of_2(uint64_t value)
 412{
 413    if (!value) {
 414        return 0;
 415    }
 416
 417    return !(value & (value - 1));
 418}
 419
 420/* round down to the nearest power of 2*/
 421int64_t pow2floor(int64_t value);
 422
 423#include "qemu/module.h"
 424
 425/*
 426 * Implementation of ULEB128 (http://en.wikipedia.org/wiki/LEB128)
 427 * Input is limited to 14-bit numbers
 428 */
 429
 430int uleb128_encode_small(uint8_t *out, uint32_t n);
 431int uleb128_decode_small(const uint8_t *in, uint32_t *n);
 432
 433#endif
 434