]> git.proxmox.com Git - qemu.git/blob - include/qemu-common.h
create qemu_openpty_raw() helper function and move it to a separate file
[qemu.git] / include / qemu-common.h
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-compat.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 typedef int (*fprintf_function)(FILE *f, const char *fmt, ...)
88 GCC_FMT_ATTR(2, 3);
89
90 #ifdef _WIN32
91 #define fsync _commit
92 #if !defined(lseek)
93 # define lseek _lseeki64
94 #endif
95 int qemu_ftruncate64(int, int64_t);
96 #if !defined(ftruncate)
97 # define ftruncate qemu_ftruncate64
98 #endif
99
100 static inline char *realpath(const char *path, char *resolved_path)
101 {
102 _fullpath(resolved_path, path, _MAX_PATH);
103 return resolved_path;
104 }
105 #endif
106
107 /* icount */
108 void configure_icount(const char *option);
109 extern int use_icount;
110
111 #include "qemu/osdep.h"
112 #include "qemu/bswap.h"
113
114 /* FIXME: Remove NEED_CPU_H. */
115 #ifdef NEED_CPU_H
116 #include "cpu.h"
117 #endif /* !defined(NEED_CPU_H) */
118
119 /* main function, renamed */
120 #if defined(CONFIG_COCOA)
121 int qemu_main(int argc, char **argv, char **envp);
122 #endif
123
124 void qemu_get_timedate(struct tm *tm, int offset);
125 int qemu_timedate_diff(struct tm *tm);
126
127 #if !GLIB_CHECK_VERSION(2, 20, 0)
128 /*
129 * Glib before 2.20.0 doesn't implement g_poll, so wrap it to compile properly
130 * on older systems.
131 */
132 static inline gint g_poll(GPollFD *fds, guint nfds, gint timeout)
133 {
134 GMainContext *ctx = g_main_context_default();
135 return g_main_context_get_poll_func(ctx)(fds, nfds, timeout);
136 }
137 #endif
138
139 /**
140 * is_help_option:
141 * @s: string to test
142 *
143 * Check whether @s is one of the standard strings which indicate
144 * that the user is asking for a list of the valid values for a
145 * command option like -cpu or -M. The current accepted strings
146 * are 'help' and '?'. '?' is deprecated (it is a shell wildcard
147 * which makes it annoying to use in a reliable way) but provided
148 * for backwards compatibility.
149 *
150 * Returns: true if @s is a request for a list.
151 */
152 static inline bool is_help_option(const char *s)
153 {
154 return !strcmp(s, "?") || !strcmp(s, "help");
155 }
156
157 /* cutils.c */
158 void pstrcpy(char *buf, int buf_size, const char *str);
159 void strpadcpy(char *buf, int buf_size, const char *str, char pad);
160 char *pstrcat(char *buf, int buf_size, const char *s);
161 int strstart(const char *str, const char *val, const char **ptr);
162 int stristart(const char *str, const char *val, const char **ptr);
163 int qemu_strnlen(const char *s, int max_len);
164 time_t mktimegm(struct tm *tm);
165 int qemu_fls(int i);
166 int qemu_fdatasync(int fd);
167 int fcntl_setfl(int fd, int flag);
168 int qemu_parse_fd(const char *param);
169
170 int parse_uint(const char *s, unsigned long long *value, char **endptr,
171 int base);
172 int parse_uint_full(const char *s, unsigned long long *value, int base);
173
174 /*
175 * strtosz() suffixes used to specify the default treatment of an
176 * argument passed to strtosz() without an explicit suffix.
177 * These should be defined using upper case characters in the range
178 * A-Z, as strtosz() will use qemu_toupper() on the given argument
179 * prior to comparison.
180 */
181 #define STRTOSZ_DEFSUFFIX_TB 'T'
182 #define STRTOSZ_DEFSUFFIX_GB 'G'
183 #define STRTOSZ_DEFSUFFIX_MB 'M'
184 #define STRTOSZ_DEFSUFFIX_KB 'K'
185 #define STRTOSZ_DEFSUFFIX_B 'B'
186 int64_t strtosz(const char *nptr, char **end);
187 int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix);
188 int64_t strtosz_suffix_unit(const char *nptr, char **end,
189 const char default_suffix, int64_t unit);
190
191 /* path.c */
192 void init_paths(const char *prefix);
193 const char *path(const char *pathname);
194
195 #define qemu_isalnum(c) isalnum((unsigned char)(c))
196 #define qemu_isalpha(c) isalpha((unsigned char)(c))
197 #define qemu_iscntrl(c) iscntrl((unsigned char)(c))
198 #define qemu_isdigit(c) isdigit((unsigned char)(c))
199 #define qemu_isgraph(c) isgraph((unsigned char)(c))
200 #define qemu_islower(c) islower((unsigned char)(c))
201 #define qemu_isprint(c) isprint((unsigned char)(c))
202 #define qemu_ispunct(c) ispunct((unsigned char)(c))
203 #define qemu_isspace(c) isspace((unsigned char)(c))
204 #define qemu_isupper(c) isupper((unsigned char)(c))
205 #define qemu_isxdigit(c) isxdigit((unsigned char)(c))
206 #define qemu_tolower(c) tolower((unsigned char)(c))
207 #define qemu_toupper(c) toupper((unsigned char)(c))
208 #define qemu_isascii(c) isascii((unsigned char)(c))
209 #define qemu_toascii(c) toascii((unsigned char)(c))
210
211 void *qemu_oom_check(void *ptr);
212
213 ssize_t qemu_write_full(int fd, const void *buf, size_t count)
214 QEMU_WARN_UNUSED_RESULT;
215 ssize_t qemu_send_full(int fd, const void *buf, size_t count, int flags)
216 QEMU_WARN_UNUSED_RESULT;
217 ssize_t qemu_recv_full(int fd, void *buf, size_t count, int flags)
218 QEMU_WARN_UNUSED_RESULT;
219
220 #ifndef _WIN32
221 int qemu_pipe(int pipefd[2]);
222 /* like openpty() but also makes it raw; return master fd */
223 int qemu_openpty_raw(int *aslave, char *pty_name);
224 #endif
225
226 #ifdef _WIN32
227 /* MinGW needs type casts for the 'buf' and 'optval' arguments. */
228 #define qemu_getsockopt(sockfd, level, optname, optval, optlen) \
229 getsockopt(sockfd, level, optname, (void *)optval, optlen)
230 #define qemu_setsockopt(sockfd, level, optname, optval, optlen) \
231 setsockopt(sockfd, level, optname, (const void *)optval, optlen)
232 #define qemu_recv(sockfd, buf, len, flags) recv(sockfd, (void *)buf, len, flags)
233 #define qemu_sendto(sockfd, buf, len, flags, destaddr, addrlen) \
234 sendto(sockfd, (const void *)buf, len, flags, destaddr, addrlen)
235 #else
236 #define qemu_getsockopt(sockfd, level, optname, optval, optlen) \
237 getsockopt(sockfd, level, optname, optval, optlen)
238 #define qemu_setsockopt(sockfd, level, optname, optval, optlen) \
239 setsockopt(sockfd, level, optname, optval, optlen)
240 #define qemu_recv(sockfd, buf, len, flags) recv(sockfd, buf, len, flags)
241 #define qemu_sendto(sockfd, buf, len, flags, destaddr, addrlen) \
242 sendto(sockfd, buf, len, flags, destaddr, addrlen)
243 #endif
244
245 /* Error handling. */
246
247 void QEMU_NORETURN hw_error(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
248
249 struct ParallelIOArg {
250 void *buffer;
251 int count;
252 };
253
254 typedef int (*DMA_transfer_handler) (void *opaque, int nchan, int pos, int size);
255
256 typedef uint64_t pcibus_t;
257
258 typedef enum LostTickPolicy {
259 LOST_TICK_DISCARD,
260 LOST_TICK_DELAY,
261 LOST_TICK_MERGE,
262 LOST_TICK_SLEW,
263 LOST_TICK_MAX
264 } LostTickPolicy;
265
266 typedef struct PCIHostDeviceAddress {
267 unsigned int domain;
268 unsigned int bus;
269 unsigned int slot;
270 unsigned int function;
271 } PCIHostDeviceAddress;
272
273 void tcg_exec_init(unsigned long tb_size);
274 bool tcg_enabled(void);
275
276 void cpu_exec_init_all(void);
277
278 /* CPU save/load. */
279 void cpu_save(QEMUFile *f, void *opaque);
280 int cpu_load(QEMUFile *f, void *opaque, int version_id);
281
282 /* Unblock cpu */
283 void qemu_cpu_kick_self(void);
284
285 /* work queue */
286 struct qemu_work_item {
287 struct qemu_work_item *next;
288 void (*func)(void *data);
289 void *data;
290 int done;
291 };
292
293 #ifdef CONFIG_USER_ONLY
294 static inline void qemu_init_vcpu(void *env)
295 {
296 }
297 #else
298 void qemu_init_vcpu(void *env);
299 #endif
300
301
302 /**
303 * Sends a (part of) iovec down a socket, yielding when the socket is full, or
304 * Receives data into a (part of) iovec from a socket,
305 * yielding when there is no data in the socket.
306 * The same interface as qemu_sendv_recvv(), with added yielding.
307 * XXX should mark these as coroutine_fn
308 */
309 ssize_t qemu_co_sendv_recvv(int sockfd, struct iovec *iov, unsigned iov_cnt,
310 size_t offset, size_t bytes, bool do_send);
311 #define qemu_co_recvv(sockfd, iov, iov_cnt, offset, bytes) \
312 qemu_co_sendv_recvv(sockfd, iov, iov_cnt, offset, bytes, false)
313 #define qemu_co_sendv(sockfd, iov, iov_cnt, offset, bytes) \
314 qemu_co_sendv_recvv(sockfd, iov, iov_cnt, offset, bytes, true)
315
316 /**
317 * The same as above, but with just a single buffer
318 */
319 ssize_t qemu_co_send_recv(int sockfd, void *buf, size_t bytes, bool do_send);
320 #define qemu_co_recv(sockfd, buf, bytes) \
321 qemu_co_send_recv(sockfd, buf, bytes, false)
322 #define qemu_co_send(sockfd, buf, bytes) \
323 qemu_co_send_recv(sockfd, buf, bytes, true)
324
325 typedef struct QEMUIOVector {
326 struct iovec *iov;
327 int niov;
328 int nalloc;
329 size_t size;
330 } QEMUIOVector;
331
332 void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint);
333 void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov);
334 void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len);
335 void qemu_iovec_concat(QEMUIOVector *dst,
336 QEMUIOVector *src, size_t soffset, size_t sbytes);
337 void qemu_iovec_concat_iov(QEMUIOVector *dst,
338 struct iovec *src_iov, unsigned int src_cnt,
339 size_t soffset, size_t sbytes);
340 void qemu_iovec_destroy(QEMUIOVector *qiov);
341 void qemu_iovec_reset(QEMUIOVector *qiov);
342 size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset,
343 void *buf, size_t bytes);
344 size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset,
345 const void *buf, size_t bytes);
346 size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
347 int fillc, size_t bytes);
348
349 bool buffer_is_zero(const void *buf, size_t len);
350
351 void qemu_progress_init(int enabled, float min_skip);
352 void qemu_progress_end(void);
353 void qemu_progress_print(float delta, int max);
354 const char *qemu_get_vm_name(void);
355
356 #define QEMU_FILE_TYPE_BIOS 0
357 #define QEMU_FILE_TYPE_KEYMAP 1
358 char *qemu_find_file(int type, const char *name);
359
360 /* OS specific functions */
361 void os_setup_early_signal_handling(void);
362 char *os_find_datadir(const char *argv0);
363 void os_parse_cmd_args(int index, const char *optarg);
364 void os_pidfile_error(void);
365
366 /* Convert a byte between binary and BCD. */
367 static inline uint8_t to_bcd(uint8_t val)
368 {
369 return ((val / 10) << 4) | (val % 10);
370 }
371
372 static inline uint8_t from_bcd(uint8_t val)
373 {
374 return ((val >> 4) * 10) + (val & 0x0f);
375 }
376
377 /* compute with 96 bit intermediate result: (a*b)/c */
378 static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
379 {
380 union {
381 uint64_t ll;
382 struct {
383 #ifdef HOST_WORDS_BIGENDIAN
384 uint32_t high, low;
385 #else
386 uint32_t low, high;
387 #endif
388 } l;
389 } u, res;
390 uint64_t rl, rh;
391
392 u.ll = a;
393 rl = (uint64_t)u.l.low * (uint64_t)b;
394 rh = (uint64_t)u.l.high * (uint64_t)b;
395 rh += (rl >> 32);
396 res.l.high = rh / c;
397 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
398 return res.ll;
399 }
400
401 /* Round number down to multiple */
402 #define QEMU_ALIGN_DOWN(n, m) ((n) / (m) * (m))
403
404 /* Round number up to multiple */
405 #define QEMU_ALIGN_UP(n, m) QEMU_ALIGN_DOWN((n) + (m) - 1, (m))
406
407 static inline bool is_power_of_2(uint64_t value)
408 {
409 if (!value) {
410 return 0;
411 }
412
413 return !(value & (value - 1));
414 }
415
416 /* round down to the nearest power of 2*/
417 int64_t pow2floor(int64_t value);
418
419 #include "qemu/module.h"
420
421 /*
422 * Implementation of ULEB128 (http://en.wikipedia.org/wiki/LEB128)
423 * Input is limited to 14-bit numbers
424 */
425
426 int uleb128_encode_small(uint8_t *out, uint32_t n);
427 int uleb128_decode_small(const uint8_t *in, uint32_t *n);
428
429 /* unicode.c */
430 int mod_utf8_codepoint(const char *s, size_t n, char **end);
431
432 /*
433 * Hexdump a buffer to a file. An optional string prefix is added to every line
434 */
435
436 void qemu_hexdump(const char *buf, FILE *fp, const char *prefix, size_t size);
437
438 /* vector definitions */
439 #ifdef __ALTIVEC__
440 #include <altivec.h>
441 /* The altivec.h header says we're allowed to undef these for
442 * C++ compatibility. Here we don't care about C++, but we
443 * undef them anyway to avoid namespace pollution.
444 */
445 #undef vector
446 #undef pixel
447 #undef bool
448 #define VECTYPE __vector unsigned char
449 #define SPLAT(p) vec_splat(vec_ld(0, p), 0)
450 #define ALL_EQ(v1, v2) vec_all_eq(v1, v2)
451 /* altivec.h may redefine the bool macro as vector type.
452 * Reset it to POSIX semantics. */
453 #define bool _Bool
454 #elif defined __SSE2__
455 #include <emmintrin.h>
456 #define VECTYPE __m128i
457 #define SPLAT(p) _mm_set1_epi8(*(p))
458 #define ALL_EQ(v1, v2) (_mm_movemask_epi8(_mm_cmpeq_epi8(v1, v2)) == 0xFFFF)
459 #else
460 #define VECTYPE unsigned long
461 #define SPLAT(p) (*(p) * (~0UL / 255))
462 #define ALL_EQ(v1, v2) ((v1) == (v2))
463 #endif
464
465 #define BUFFER_FIND_NONZERO_OFFSET_UNROLL_FACTOR 8
466 static inline bool
467 can_use_buffer_find_nonzero_offset(const void *buf, size_t len)
468 {
469 return (len % (BUFFER_FIND_NONZERO_OFFSET_UNROLL_FACTOR
470 * sizeof(VECTYPE)) == 0
471 && ((uintptr_t) buf) % sizeof(VECTYPE) == 0);
472 }
473 size_t buffer_find_nonzero_offset(const void *buf, size_t len);
474
475 /*
476 * helper to parse debug environment variables
477 */
478 int parse_debug_env(const char *name, int max, int initial);
479
480 #endif