1
2
3
4
5
6
7
8
9
10
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
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
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
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
122void configure_icount(const char *option);
123extern int use_icount;
124
125
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
136
137
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
147
148
149
150
151
152
153
154
155
156
157
158static inline bool is_help_option(const char *s)
159{
160 return !strcmp(s, "?") || !strcmp(s, "help");
161}
162
163
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
182
183
184
185
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
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
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
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
283void cpu_save(QEMUFile *f, void *opaque);
284int cpu_load(QEMUFile *f, void *opaque, int version_id);
285
286
287void qemu_cpu_kick_self(void);
288
289
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
308
309
310
311
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
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
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
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
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
406#define QEMU_ALIGN_DOWN(n, m) ((n) / (m) * (m))
407
408
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
421int64_t pow2floor(int64_t value);
422
423#include "qemu/module.h"
424
425
426
427
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