Statistics
| Branch: | Revision:

root / include / qemu-common.h @ 10f5bff6

History | View | Annotate | Download (14 kB)

1 9fb26641 Orit Wasserman
2 04509ad9 Eduardo Habkost
/* Common header file that is included by all of QEMU.
3 04509ad9 Eduardo Habkost
 *
4 04509ad9 Eduardo Habkost
 * This file is supposed to be included only by .c files. No header file should
5 04509ad9 Eduardo Habkost
 * depend on qemu-common.h, as this would easily lead to circular header
6 04509ad9 Eduardo Habkost
 * dependencies.
7 04509ad9 Eduardo Habkost
 *
8 04509ad9 Eduardo Habkost
 * If a header file uses a definition from qemu-common.h, that definition
9 04509ad9 Eduardo Habkost
 * must be moved to a separate header file, and the header that uses it
10 04509ad9 Eduardo Habkost
 * must include that header.
11 04509ad9 Eduardo Habkost
 */
12 faf07963 pbrook
#ifndef QEMU_COMMON_H
13 faf07963 pbrook
#define QEMU_COMMON_H
14 faf07963 pbrook
15 1de7afc9 Paolo Bonzini
#include "qemu/compiler.h"
16 beb6f0de Kevin Wolf
#include "config-host.h"
17 1de7afc9 Paolo Bonzini
#include "qemu/typedefs.h"
18 beb6f0de Kevin Wolf
19 332ae28d Paolo Bonzini
#if defined(__arm__) || defined(__sparc__) || defined(__mips__) || defined(__hppa__) || defined(__ia64__)
20 332ae28d Paolo Bonzini
#define WORDS_ALIGNED
21 332ae28d Paolo Bonzini
#endif
22 332ae28d Paolo Bonzini
23 082b5557 Blue Swirl
#define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
24 24ebf5f3 Paolo Bonzini
25 faf07963 pbrook
/* we put basic includes here to avoid repeating them in device drivers */
26 faf07963 pbrook
#include <stdlib.h>
27 faf07963 pbrook
#include <stdio.h>
28 faf07963 pbrook
#include <stdarg.h>
29 11165820 Paul Brook
#include <stdbool.h>
30 faf07963 pbrook
#include <string.h>
31 c8906845 balrog
#include <strings.h>
32 faf07963 pbrook
#include <inttypes.h>
33 faf07963 pbrook
#include <limits.h>
34 faf07963 pbrook
#include <time.h>
35 faf07963 pbrook
#include <ctype.h>
36 faf07963 pbrook
#include <errno.h>
37 faf07963 pbrook
#include <unistd.h>
38 faf07963 pbrook
#include <fcntl.h>
39 faf07963 pbrook
#include <sys/stat.h>
40 dcfd0865 Hervé Poussineau
#include <sys/time.h>
41 55616505 Paul Brook
#include <assert.h>
42 86f69a92 Alexandre Raymond
#include <signal.h>
43 d63c9477 Anthony Liguori
#include "glib-compat.h"
44 faf07963 pbrook
45 082b5557 Blue Swirl
#ifdef _WIN32
46 9c17d615 Paolo Bonzini
#include "sysemu/os-win32.h"
47 082b5557 Blue Swirl
#endif
48 082b5557 Blue Swirl
49 082b5557 Blue Swirl
#ifdef CONFIG_POSIX
50 9c17d615 Paolo Bonzini
#include "sysemu/os-posix.h"
51 082b5557 Blue Swirl
#endif
52 082b5557 Blue Swirl
53 faf07963 pbrook
#ifndef O_LARGEFILE
54 faf07963 pbrook
#define O_LARGEFILE 0
55 faf07963 pbrook
#endif
56 faf07963 pbrook
#ifndef O_BINARY
57 faf07963 pbrook
#define O_BINARY 0
58 faf07963 pbrook
#endif
59 0e74e66b Juan Quintela
#ifndef MAP_ANONYMOUS
60 0e74e66b Juan Quintela
#define MAP_ANONYMOUS MAP_ANON
61 0e74e66b Juan Quintela
#endif
62 faf07963 pbrook
#ifndef ENOMEDIUM
63 faf07963 pbrook
#define ENOMEDIUM ENODEV
64 faf07963 pbrook
#endif
65 4c955388 Anthony Liguori
#if !defined(ENOTSUP)
66 2880bc32 Juan Quintela
#define ENOTSUP 4096
67 2880bc32 Juan Quintela
#endif
68 2084a8e3 Paolo Bonzini
#if !defined(ECANCELED)
69 2084a8e3 Paolo Bonzini
#define ECANCELED 4097
70 2084a8e3 Paolo Bonzini
#endif
71 02582abd Stefan Weil
#if !defined(EMEDIUMTYPE)
72 02582abd Stefan Weil
#define EMEDIUMTYPE 4098
73 02582abd Stefan Weil
#endif
74 3c9405a0 Gerd Hoffmann
#ifndef TIME_MAX
75 3c9405a0 Gerd Hoffmann
#define TIME_MAX LONG_MAX
76 3c9405a0 Gerd Hoffmann
#endif
77 faf07963 pbrook
78 c0fd260e Stefan Weil
/* HOST_LONG_BITS is the size of a native pointer in bits. */
79 c0fd260e Stefan Weil
#if UINTPTR_MAX == UINT32_MAX
80 c0fd260e Stefan Weil
# define HOST_LONG_BITS 32
81 c0fd260e Stefan Weil
#elif UINTPTR_MAX == UINT64_MAX
82 c0fd260e Stefan Weil
# define HOST_LONG_BITS 64
83 c0fd260e Stefan Weil
#else
84 c0fd260e Stefan Weil
# error Unknown pointer size
85 c0fd260e Stefan Weil
#endif
86 c0fd260e Stefan Weil
87 f868445a Stefan Weil
typedef int (*fprintf_function)(FILE *f, const char *fmt, ...)
88 f868445a Stefan Weil
    GCC_FMT_ATTR(2, 3);
89 f868445a Stefan Weil
90 faf07963 pbrook
#ifdef _WIN32
91 faf07963 pbrook
#define fsync _commit
92 371c6489 Stefan Weil
#if !defined(lseek)
93 371c6489 Stefan Weil
# define lseek _lseeki64
94 371c6489 Stefan Weil
#endif
95 64b85a8f Blue Swirl
int qemu_ftruncate64(int, int64_t);
96 371c6489 Stefan Weil
#if !defined(ftruncate)
97 371c6489 Stefan Weil
# define ftruncate qemu_ftruncate64
98 371c6489 Stefan Weil
#endif
99 faf07963 pbrook
100 faf07963 pbrook
static inline char *realpath(const char *path, char *resolved_path)
101 faf07963 pbrook
{
102 faf07963 pbrook
    _fullpath(resolved_path, path, _MAX_PATH);
103 faf07963 pbrook
    return resolved_path;
104 faf07963 pbrook
}
105 faf07963 pbrook
#endif
106 faf07963 pbrook
107 946fb27c Paolo Bonzini
/* icount */
108 946fb27c Paolo Bonzini
void configure_icount(const char *option);
109 946fb27c Paolo Bonzini
extern int use_icount;
110 946fb27c Paolo Bonzini
111 1de7afc9 Paolo Bonzini
#include "qemu/osdep.h"
112 1de7afc9 Paolo Bonzini
#include "qemu/bswap.h"
113 faf07963 pbrook
114 9adea5f7 Paolo Bonzini
/* FIXME: Remove NEED_CPU_H.  */
115 9adea5f7 Paolo Bonzini
#ifdef NEED_CPU_H
116 faf07963 pbrook
#include "cpu.h"
117 faf07963 pbrook
#endif /* !defined(NEED_CPU_H) */
118 faf07963 pbrook
119 3bbbee18 Andreas Färber
/* main function, renamed */
120 3bbbee18 Andreas Färber
#if defined(CONFIG_COCOA)
121 3bbbee18 Andreas Färber
int qemu_main(int argc, char **argv, char **envp);
122 3bbbee18 Andreas Färber
#endif
123 3bbbee18 Andreas Färber
124 f6503059 balrog
void qemu_get_timedate(struct tm *tm, int offset);
125 f6503059 balrog
int qemu_timedate_diff(struct tm *tm);
126 f6503059 balrog
127 31e76f65 Alexander Graf
#if !GLIB_CHECK_VERSION(2, 20, 0)
128 31e76f65 Alexander Graf
/*
129 31e76f65 Alexander Graf
 * Glib before 2.20.0 doesn't implement g_poll, so wrap it to compile properly
130 31e76f65 Alexander Graf
 * on older systems.
131 31e76f65 Alexander Graf
 */
132 31e76f65 Alexander Graf
static inline gint g_poll(GPollFD *fds, guint nfds, gint timeout)
133 31e76f65 Alexander Graf
{
134 31e76f65 Alexander Graf
    GMainContext *ctx = g_main_context_default();
135 31e76f65 Alexander Graf
    return g_main_context_get_poll_func(ctx)(fds, nfds, timeout);
136 31e76f65 Alexander Graf
}
137 31e76f65 Alexander Graf
#endif
138 31e76f65 Alexander Graf
139 c8057f95 Peter Maydell
/**
140 c8057f95 Peter Maydell
 * is_help_option:
141 c8057f95 Peter Maydell
 * @s: string to test
142 c8057f95 Peter Maydell
 *
143 c8057f95 Peter Maydell
 * Check whether @s is one of the standard strings which indicate
144 c8057f95 Peter Maydell
 * that the user is asking for a list of the valid values for a
145 c8057f95 Peter Maydell
 * command option like -cpu or -M. The current accepted strings
146 c8057f95 Peter Maydell
 * are 'help' and '?'. '?' is deprecated (it is a shell wildcard
147 c8057f95 Peter Maydell
 * which makes it annoying to use in a reliable way) but provided
148 c8057f95 Peter Maydell
 * for backwards compatibility.
149 c8057f95 Peter Maydell
 *
150 c8057f95 Peter Maydell
 * Returns: true if @s is a request for a list.
151 c8057f95 Peter Maydell
 */
152 c8057f95 Peter Maydell
static inline bool is_help_option(const char *s)
153 c8057f95 Peter Maydell
{
154 c8057f95 Peter Maydell
    return !strcmp(s, "?") || !strcmp(s, "help");
155 c8057f95 Peter Maydell
}
156 c8057f95 Peter Maydell
157 faf07963 pbrook
/* cutils.c */
158 faf07963 pbrook
void pstrcpy(char *buf, int buf_size, const char *str);
159 2a025ae4 Dmitry Fleytman
void strpadcpy(char *buf, int buf_size, const char *str, char pad);
160 faf07963 pbrook
char *pstrcat(char *buf, int buf_size, const char *s);
161 faf07963 pbrook
int strstart(const char *str, const char *val, const char **ptr);
162 faf07963 pbrook
int stristart(const char *str, const char *val, const char **ptr);
163 d43277c5 Blue Swirl
int qemu_strnlen(const char *s, int max_len);
164 a38ed811 Kevin Wolf
char *qemu_strsep(char **input, const char *delim);
165 faf07963 pbrook
time_t mktimegm(struct tm *tm);
166 ad46db9a blueswir1
int qemu_fls(int i);
167 6f1953c4 Christoph Hellwig
int qemu_fdatasync(int fd);
168 db1a4972 Paolo Bonzini
int fcntl_setfl(int fd, int flag);
169 443916d1 Stefan Berger
int qemu_parse_fd(const char *param);
170 d8427002 Jes Sorensen
171 e3f9fe2d Eduardo Habkost
int parse_uint(const char *s, unsigned long long *value, char **endptr,
172 e3f9fe2d Eduardo Habkost
               int base);
173 e3f9fe2d Eduardo Habkost
int parse_uint_full(const char *s, unsigned long long *value, int base);
174 e3f9fe2d Eduardo Habkost
175 d7142456 Jes Sorensen
/*
176 d7142456 Jes Sorensen
 * strtosz() suffixes used to specify the default treatment of an
177 d7142456 Jes Sorensen
 * argument passed to strtosz() without an explicit suffix.
178 d7142456 Jes Sorensen
 * These should be defined using upper case characters in the range
179 d7142456 Jes Sorensen
 * A-Z, as strtosz() will use qemu_toupper() on the given argument
180 d7142456 Jes Sorensen
 * prior to comparison.
181 d7142456 Jes Sorensen
 */
182 5e00984a Kevin Wolf
#define STRTOSZ_DEFSUFFIX_EB        'E'
183 5e00984a Kevin Wolf
#define STRTOSZ_DEFSUFFIX_PB        'P'
184 d8427002 Jes Sorensen
#define STRTOSZ_DEFSUFFIX_TB        'T'
185 d8427002 Jes Sorensen
#define STRTOSZ_DEFSUFFIX_GB        'G'
186 d8427002 Jes Sorensen
#define STRTOSZ_DEFSUFFIX_MB        'M'
187 d8427002 Jes Sorensen
#define STRTOSZ_DEFSUFFIX_KB        'K'
188 d8427002 Jes Sorensen
#define STRTOSZ_DEFSUFFIX_B        'B'
189 70b4f4bb Jes Sorensen
int64_t strtosz(const char *nptr, char **end);
190 70b4f4bb Jes Sorensen
int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix);
191 a732e1ba Joerg Roedel
int64_t strtosz_suffix_unit(const char *nptr, char **end,
192 a732e1ba Joerg Roedel
                            const char default_suffix, int64_t unit);
193 faf07963 pbrook
194 44e3e053 Wenchao Xia
/* used to print char* safely */
195 44e3e053 Wenchao Xia
#define STR_OR_NULL(str) ((str) ? (str) : "null")
196 44e3e053 Wenchao Xia
197 37022086 Blue Swirl
/* path.c */
198 37022086 Blue Swirl
void init_paths(const char *prefix);
199 37022086 Blue Swirl
const char *path(const char *pathname);
200 37022086 Blue Swirl
201 cd390083 blueswir1
#define qemu_isalnum(c)                isalnum((unsigned char)(c))
202 cd390083 blueswir1
#define qemu_isalpha(c)                isalpha((unsigned char)(c))
203 cd390083 blueswir1
#define qemu_iscntrl(c)                iscntrl((unsigned char)(c))
204 cd390083 blueswir1
#define qemu_isdigit(c)                isdigit((unsigned char)(c))
205 cd390083 blueswir1
#define qemu_isgraph(c)                isgraph((unsigned char)(c))
206 cd390083 blueswir1
#define qemu_islower(c)                islower((unsigned char)(c))
207 cd390083 blueswir1
#define qemu_isprint(c)                isprint((unsigned char)(c))
208 cd390083 blueswir1
#define qemu_ispunct(c)                ispunct((unsigned char)(c))
209 cd390083 blueswir1
#define qemu_isspace(c)                isspace((unsigned char)(c))
210 cd390083 blueswir1
#define qemu_isupper(c)                isupper((unsigned char)(c))
211 cd390083 blueswir1
#define qemu_isxdigit(c)        isxdigit((unsigned char)(c))
212 cd390083 blueswir1
#define qemu_tolower(c)                tolower((unsigned char)(c))
213 cd390083 blueswir1
#define qemu_toupper(c)                toupper((unsigned char)(c))
214 cd390083 blueswir1
#define qemu_isascii(c)                isascii((unsigned char)(c))
215 cd390083 blueswir1
#define qemu_toascii(c)                toascii((unsigned char)(c))
216 cd390083 blueswir1
217 b152aa84 Jes Sorensen
void *qemu_oom_check(void *ptr);
218 ca10f867 aurel32
219 7c7c0629 Juan Quintela
ssize_t qemu_write_full(int fd, const void *buf, size_t count)
220 7c7c0629 Juan Quintela
    QEMU_WARN_UNUSED_RESULT;
221 993295fe Paolo Bonzini
ssize_t qemu_send_full(int fd, const void *buf, size_t count, int flags)
222 993295fe Paolo Bonzini
    QEMU_WARN_UNUSED_RESULT;
223 8c5135f9 Paolo Bonzini
ssize_t qemu_recv_full(int fd, void *buf, size_t count, int flags)
224 993295fe Paolo Bonzini
    QEMU_WARN_UNUSED_RESULT;
225 40ff6d7e Kevin Wolf
226 40ff6d7e Kevin Wolf
#ifndef _WIN32
227 40ff6d7e Kevin Wolf
int qemu_pipe(int pipefd[2]);
228 4efeabbb Michael Tokarev
/* like openpty() but also makes it raw; return master fd */
229 4efeabbb Michael Tokarev
int qemu_openpty_raw(int *aslave, char *pty_name);
230 40ff6d7e Kevin Wolf
#endif
231 40ff6d7e Kevin Wolf
232 00aa0040 Blue Swirl
#ifdef _WIN32
233 58455eb9 Stefan Weil
/* MinGW needs type casts for the 'buf' and 'optval' arguments. */
234 58455eb9 Stefan Weil
#define qemu_getsockopt(sockfd, level, optname, optval, optlen) \
235 58455eb9 Stefan Weil
    getsockopt(sockfd, level, optname, (void *)optval, optlen)
236 58455eb9 Stefan Weil
#define qemu_setsockopt(sockfd, level, optname, optval, optlen) \
237 58455eb9 Stefan Weil
    setsockopt(sockfd, level, optname, (const void *)optval, optlen)
238 00aa0040 Blue Swirl
#define qemu_recv(sockfd, buf, len, flags) recv(sockfd, (void *)buf, len, flags)
239 73062dfe Stefan Weil
#define qemu_sendto(sockfd, buf, len, flags, destaddr, addrlen) \
240 73062dfe Stefan Weil
    sendto(sockfd, (const void *)buf, len, flags, destaddr, addrlen)
241 00aa0040 Blue Swirl
#else
242 58455eb9 Stefan Weil
#define qemu_getsockopt(sockfd, level, optname, optval, optlen) \
243 58455eb9 Stefan Weil
    getsockopt(sockfd, level, optname, optval, optlen)
244 58455eb9 Stefan Weil
#define qemu_setsockopt(sockfd, level, optname, optval, optlen) \
245 58455eb9 Stefan Weil
    setsockopt(sockfd, level, optname, optval, optlen)
246 00aa0040 Blue Swirl
#define qemu_recv(sockfd, buf, len, flags) recv(sockfd, buf, len, flags)
247 73062dfe Stefan Weil
#define qemu_sendto(sockfd, buf, len, flags, destaddr, addrlen) \
248 73062dfe Stefan Weil
    sendto(sockfd, buf, len, flags, destaddr, addrlen)
249 00aa0040 Blue Swirl
#endif
250 00aa0040 Blue Swirl
251 87ecb68b pbrook
/* Error handling.  */
252 87ecb68b pbrook
253 e5924d89 Stefan Weil
void QEMU_NORETURN hw_error(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
254 87ecb68b pbrook
255 87ecb68b pbrook
struct ParallelIOArg {
256 87ecb68b pbrook
    void *buffer;
257 87ecb68b pbrook
    int count;
258 87ecb68b pbrook
};
259 87ecb68b pbrook
260 87ecb68b pbrook
typedef int (*DMA_transfer_handler) (void *opaque, int nchan, int pos, int size);
261 87ecb68b pbrook
262 186993ee Michael S. Tsirkin
typedef uint64_t pcibus_t;
263 186993ee Michael S. Tsirkin
264 4e4fa398 Jan Kiszka
typedef enum LostTickPolicy {
265 4e4fa398 Jan Kiszka
    LOST_TICK_DISCARD,
266 4e4fa398 Jan Kiszka
    LOST_TICK_DELAY,
267 4e4fa398 Jan Kiszka
    LOST_TICK_MERGE,
268 4e4fa398 Jan Kiszka
    LOST_TICK_SLEW,
269 1ce05125 Paolo Bonzini
    LOST_TICK_MAX
270 4e4fa398 Jan Kiszka
} LostTickPolicy;
271 4e4fa398 Jan Kiszka
272 679042f0 Anthony PERARD
typedef struct PCIHostDeviceAddress {
273 679042f0 Anthony PERARD
    unsigned int domain;
274 679042f0 Anthony PERARD
    unsigned int bus;
275 679042f0 Anthony PERARD
    unsigned int slot;
276 679042f0 Anthony PERARD
    unsigned int function;
277 679042f0 Anthony PERARD
} PCIHostDeviceAddress;
278 679042f0 Anthony PERARD
279 d5ab9713 Jan Kiszka
void tcg_exec_init(unsigned long tb_size);
280 d5ab9713 Jan Kiszka
bool tcg_enabled(void);
281 d5ab9713 Jan Kiszka
282 d5ab9713 Jan Kiszka
void cpu_exec_init_all(void);
283 d2053c3c Blue Swirl
284 b3c7724c pbrook
/* CPU save/load.  */
285 8d0f2bae Andreas Färber
#ifdef CPU_SAVE_VERSION
286 b3c7724c pbrook
void cpu_save(QEMUFile *f, void *opaque);
287 b3c7724c pbrook
int cpu_load(QEMUFile *f, void *opaque, int version_id);
288 8d0f2bae Andreas Färber
#endif
289 b3c7724c pbrook
290 8edac960 aliguori
/* Unblock cpu */
291 46d62fac Jan Kiszka
void qemu_cpu_kick_self(void);
292 8edac960 aliguori
293 e82bcec2 Marcelo Tosatti
/* work queue */
294 e82bcec2 Marcelo Tosatti
struct qemu_work_item {
295 e82bcec2 Marcelo Tosatti
    struct qemu_work_item *next;
296 e82bcec2 Marcelo Tosatti
    void (*func)(void *data);
297 e82bcec2 Marcelo Tosatti
    void *data;
298 e82bcec2 Marcelo Tosatti
    int done;
299 3c02270d Chegu Vinod
    bool free;
300 e82bcec2 Marcelo Tosatti
};
301 e82bcec2 Marcelo Tosatti
302 8c5135f9 Paolo Bonzini
303 8c5135f9 Paolo Bonzini
/**
304 2fc8ae1d Michael Tokarev
 * Sends a (part of) iovec down a socket, yielding when the socket is full, or
305 2fc8ae1d Michael Tokarev
 * Receives data into a (part of) iovec from a socket,
306 2fc8ae1d Michael Tokarev
 * yielding when there is no data in the socket.
307 2fc8ae1d Michael Tokarev
 * The same interface as qemu_sendv_recvv(), with added yielding.
308 2fc8ae1d Michael Tokarev
 * XXX should mark these as coroutine_fn
309 8c5135f9 Paolo Bonzini
 */
310 2fc8ae1d Michael Tokarev
ssize_t qemu_co_sendv_recvv(int sockfd, struct iovec *iov, unsigned iov_cnt,
311 2fc8ae1d Michael Tokarev
                            size_t offset, size_t bytes, bool do_send);
312 2fc8ae1d Michael Tokarev
#define qemu_co_recvv(sockfd, iov, iov_cnt, offset, bytes) \
313 2fc8ae1d Michael Tokarev
  qemu_co_sendv_recvv(sockfd, iov, iov_cnt, offset, bytes, false)
314 2fc8ae1d Michael Tokarev
#define qemu_co_sendv(sockfd, iov, iov_cnt, offset, bytes) \
315 2fc8ae1d Michael Tokarev
  qemu_co_sendv_recvv(sockfd, iov, iov_cnt, offset, bytes, true)
316 8c5135f9 Paolo Bonzini
317 8c5135f9 Paolo Bonzini
/**
318 2fc8ae1d Michael Tokarev
 * The same as above, but with just a single buffer
319 8c5135f9 Paolo Bonzini
 */
320 2fc8ae1d Michael Tokarev
ssize_t qemu_co_send_recv(int sockfd, void *buf, size_t bytes, bool do_send);
321 2fc8ae1d Michael Tokarev
#define qemu_co_recv(sockfd, buf, bytes) \
322 2fc8ae1d Michael Tokarev
  qemu_co_send_recv(sockfd, buf, bytes, false)
323 2fc8ae1d Michael Tokarev
#define qemu_co_send(sockfd, buf, bytes) \
324 2fc8ae1d Michael Tokarev
  qemu_co_send_recv(sockfd, buf, bytes, true)
325 8c5135f9 Paolo Bonzini
326 44e3ee8a aliguori
typedef struct QEMUIOVector {
327 44e3ee8a aliguori
    struct iovec *iov;
328 44e3ee8a aliguori
    int niov;
329 44e3ee8a aliguori
    int nalloc;
330 249aa745 aliguori
    size_t size;
331 44e3ee8a aliguori
} QEMUIOVector;
332 44e3ee8a aliguori
333 44e3ee8a aliguori
void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint);
334 522584a5 aliguori
void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov);
335 44e3ee8a aliguori
void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len);
336 1b093c48 Michael Tokarev
void qemu_iovec_concat(QEMUIOVector *dst,
337 1b093c48 Michael Tokarev
                       QEMUIOVector *src, size_t soffset, size_t sbytes);
338 530c0bbd Stefan Hajnoczi
void qemu_iovec_concat_iov(QEMUIOVector *dst,
339 530c0bbd Stefan Hajnoczi
                           struct iovec *src_iov, unsigned int src_cnt,
340 530c0bbd Stefan Hajnoczi
                           size_t soffset, size_t sbytes);
341 44e3ee8a aliguori
void qemu_iovec_destroy(QEMUIOVector *qiov);
342 be959463 aliguori
void qemu_iovec_reset(QEMUIOVector *qiov);
343 d5e6b161 Michael Tokarev
size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset,
344 d5e6b161 Michael Tokarev
                         void *buf, size_t bytes);
345 03396148 Michael Tokarev
size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset,
346 03396148 Michael Tokarev
                           const void *buf, size_t bytes);
347 3d9b4925 Michael Tokarev
size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
348 3d9b4925 Michael Tokarev
                         int fillc, size_t bytes);
349 44e3ee8a aliguori
350 1a6d39fd Stefan Hajnoczi
bool buffer_is_zero(const void *buf, size_t len);
351 1a6d39fd Stefan Hajnoczi
352 6b837bc4 Jes Sorensen
void qemu_progress_init(int enabled, float min_skip);
353 6b837bc4 Jes Sorensen
void qemu_progress_end(void);
354 3bfe4dbf Jes Sorensen
void qemu_progress_print(float delta, int max);
355 31459f46 Ronnie Sahlberg
const char *qemu_get_vm_name(void);
356 6b837bc4 Jes Sorensen
357 082b5557 Blue Swirl
#define QEMU_FILE_TYPE_BIOS   0
358 082b5557 Blue Swirl
#define QEMU_FILE_TYPE_KEYMAP 1
359 082b5557 Blue Swirl
char *qemu_find_file(int type, const char *name);
360 082b5557 Blue Swirl
361 082b5557 Blue Swirl
/* OS specific functions */
362 082b5557 Blue Swirl
void os_setup_early_signal_handling(void);
363 10f5bff6 Fam Zheng
char *os_find_datadir(void);
364 082b5557 Blue Swirl
void os_parse_cmd_args(int index, const char *optarg);
365 082b5557 Blue Swirl
void os_pidfile_error(void);
366 082b5557 Blue Swirl
367 abd0c6bd Paul Brook
/* Convert a byte between binary and BCD.  */
368 abd0c6bd Paul Brook
static inline uint8_t to_bcd(uint8_t val)
369 abd0c6bd Paul Brook
{
370 abd0c6bd Paul Brook
    return ((val / 10) << 4) | (val % 10);
371 abd0c6bd Paul Brook
}
372 abd0c6bd Paul Brook
373 abd0c6bd Paul Brook
static inline uint8_t from_bcd(uint8_t val)
374 abd0c6bd Paul Brook
{
375 abd0c6bd Paul Brook
    return ((val >> 4) * 10) + (val & 0x0f);
376 abd0c6bd Paul Brook
}
377 abd0c6bd Paul Brook
378 338b922e malc
/* compute with 96 bit intermediate result: (a*b)/c */
379 338b922e malc
static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
380 338b922e malc
{
381 338b922e malc
    union {
382 338b922e malc
        uint64_t ll;
383 338b922e malc
        struct {
384 338b922e malc
#ifdef HOST_WORDS_BIGENDIAN
385 338b922e malc
            uint32_t high, low;
386 338b922e malc
#else
387 338b922e malc
            uint32_t low, high;
388 338b922e malc
#endif
389 338b922e malc
        } l;
390 338b922e malc
    } u, res;
391 338b922e malc
    uint64_t rl, rh;
392 338b922e malc
393 338b922e malc
    u.ll = a;
394 338b922e malc
    rl = (uint64_t)u.l.low * (uint64_t)b;
395 338b922e malc
    rh = (uint64_t)u.l.high * (uint64_t)b;
396 338b922e malc
    rh += (rl >> 32);
397 338b922e malc
    res.l.high = rh / c;
398 338b922e malc
    res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
399 338b922e malc
    return res.ll;
400 338b922e malc
}
401 338b922e malc
402 3951690a Stefan Hajnoczi
/* Round number down to multiple */
403 3951690a Stefan Hajnoczi
#define QEMU_ALIGN_DOWN(n, m) ((n) / (m) * (m))
404 3951690a Stefan Hajnoczi
405 3951690a Stefan Hajnoczi
/* Round number up to multiple */
406 3951690a Stefan Hajnoczi
#define QEMU_ALIGN_UP(n, m) QEMU_ALIGN_DOWN((n) + (m) - 1, (m))
407 3951690a Stefan Hajnoczi
408 9fb26641 Orit Wasserman
static inline bool is_power_of_2(uint64_t value)
409 9fb26641 Orit Wasserman
{
410 9fb26641 Orit Wasserman
    if (!value) {
411 9fb26641 Orit Wasserman
        return 0;
412 9fb26641 Orit Wasserman
    }
413 9fb26641 Orit Wasserman
414 9fb26641 Orit Wasserman
    return !(value & (value - 1));
415 9fb26641 Orit Wasserman
}
416 9fb26641 Orit Wasserman
417 9fb26641 Orit Wasserman
/* round down to the nearest power of 2*/
418 9fb26641 Orit Wasserman
int64_t pow2floor(int64_t value);
419 9fb26641 Orit Wasserman
420 1de7afc9 Paolo Bonzini
#include "qemu/module.h"
421 0bfe3ca5 Anthony Liguori
422 e6546bb9 Orit Wasserman
/*
423 e6546bb9 Orit Wasserman
 * Implementation of ULEB128 (http://en.wikipedia.org/wiki/LEB128)
424 e6546bb9 Orit Wasserman
 * Input is limited to 14-bit numbers
425 e6546bb9 Orit Wasserman
 */
426 e6546bb9 Orit Wasserman
427 e6546bb9 Orit Wasserman
int uleb128_encode_small(uint8_t *out, uint32_t n);
428 e6546bb9 Orit Wasserman
int uleb128_decode_small(const uint8_t *in, uint32_t *n);
429 e6546bb9 Orit Wasserman
430 cb2744ea Markus Armbruster
/* unicode.c */
431 cb2744ea Markus Armbruster
int mod_utf8_codepoint(const char *s, size_t n, char **end);
432 cb2744ea Markus Armbruster
433 6ff66f50 Peter Crosthwaite
/*
434 6ff66f50 Peter Crosthwaite
 * Hexdump a buffer to a file. An optional string prefix is added to every line
435 6ff66f50 Peter Crosthwaite
 */
436 6ff66f50 Peter Crosthwaite
437 3568ac2a Ed Maste
void qemu_hexdump(const char *buf, FILE *fp, const char *prefix, size_t size);
438 6ff66f50 Peter Crosthwaite
439 c61ca00a Peter Lieven
/* vector definitions */
440 c61ca00a Peter Lieven
#ifdef __ALTIVEC__
441 c61ca00a Peter Lieven
#include <altivec.h>
442 8593e050 Paolo Bonzini
/* The altivec.h header says we're allowed to undef these for
443 8593e050 Paolo Bonzini
 * C++ compatibility.  Here we don't care about C++, but we
444 8593e050 Paolo Bonzini
 * undef them anyway to avoid namespace pollution.
445 8593e050 Paolo Bonzini
 */
446 8593e050 Paolo Bonzini
#undef vector
447 8593e050 Paolo Bonzini
#undef pixel
448 8593e050 Paolo Bonzini
#undef bool
449 8593e050 Paolo Bonzini
#define VECTYPE        __vector unsigned char
450 c61ca00a Peter Lieven
#define SPLAT(p)       vec_splat(vec_ld(0, p), 0)
451 c61ca00a Peter Lieven
#define ALL_EQ(v1, v2) vec_all_eq(v1, v2)
452 c61ca00a Peter Lieven
/* altivec.h may redefine the bool macro as vector type.
453 c61ca00a Peter Lieven
 * Reset it to POSIX semantics. */
454 c61ca00a Peter Lieven
#define bool _Bool
455 c61ca00a Peter Lieven
#elif defined __SSE2__
456 c61ca00a Peter Lieven
#include <emmintrin.h>
457 c61ca00a Peter Lieven
#define VECTYPE        __m128i
458 c61ca00a Peter Lieven
#define SPLAT(p)       _mm_set1_epi8(*(p))
459 c61ca00a Peter Lieven
#define ALL_EQ(v1, v2) (_mm_movemask_epi8(_mm_cmpeq_epi8(v1, v2)) == 0xFFFF)
460 c61ca00a Peter Lieven
#else
461 c61ca00a Peter Lieven
#define VECTYPE        unsigned long
462 c61ca00a Peter Lieven
#define SPLAT(p)       (*(p) * (~0UL / 255))
463 c61ca00a Peter Lieven
#define ALL_EQ(v1, v2) ((v1) == (v2))
464 c61ca00a Peter Lieven
#endif
465 c61ca00a Peter Lieven
466 41a259bd Peter Lieven
#define BUFFER_FIND_NONZERO_OFFSET_UNROLL_FACTOR 8
467 41a259bd Peter Lieven
static inline bool
468 41a259bd Peter Lieven
can_use_buffer_find_nonzero_offset(const void *buf, size_t len)
469 41a259bd Peter Lieven
{
470 41a259bd Peter Lieven
    return (len % (BUFFER_FIND_NONZERO_OFFSET_UNROLL_FACTOR
471 41a259bd Peter Lieven
                   * sizeof(VECTYPE)) == 0
472 41a259bd Peter Lieven
            && ((uintptr_t) buf) % sizeof(VECTYPE) == 0);
473 41a259bd Peter Lieven
}
474 41a259bd Peter Lieven
size_t buffer_find_nonzero_offset(const void *buf, size_t len);
475 41a259bd Peter Lieven
476 b16352ac Alon Levy
/*
477 b16352ac Alon Levy
 * helper to parse debug environment variables
478 b16352ac Alon Levy
 */
479 b16352ac Alon Levy
int parse_debug_env(const char *name, int max, int initial);
480 b16352ac Alon Levy
481 faf07963 pbrook
#endif