]> Git Repo - qemu.git/blob - include/qemu-common.h
Merge remote-tracking branch 'remotes/kraxel/tags/pull-ui-20160309-1' into staging
[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/typedefs.h"
16 #include "qemu/fprintf-fn.h"
17
18 #if defined(__arm__) || defined(__sparc__) || defined(__mips__) || defined(__hppa__) || defined(__ia64__)
19 #define WORDS_ALIGNED
20 #endif
21
22 #define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
23
24 #include "qemu/option.h"
25 #include "qemu/host-utils.h"
26
27 /* HOST_LONG_BITS is the size of a native pointer in bits. */
28 #if UINTPTR_MAX == UINT32_MAX
29 # define HOST_LONG_BITS 32
30 #elif UINTPTR_MAX == UINT64_MAX
31 # define HOST_LONG_BITS 64
32 #else
33 # error Unknown pointer size
34 #endif
35
36 void cpu_ticks_init(void);
37
38 /* icount */
39 void configure_icount(QemuOpts *opts, Error **errp);
40 extern int use_icount;
41 extern int icount_align_option;
42 /* drift information for info jit command */
43 extern int64_t max_delay;
44 extern int64_t max_advance;
45 void dump_drift_info(FILE *f, fprintf_function cpu_fprintf);
46
47 #include "qemu/bswap.h"
48
49 /* FIXME: Remove NEED_CPU_H.  */
50 #ifdef NEED_CPU_H
51 #include "cpu.h"
52 #endif /* !defined(NEED_CPU_H) */
53
54 /* main function, renamed */
55 #if defined(CONFIG_COCOA)
56 int qemu_main(int argc, char **argv, char **envp);
57 #endif
58
59 void qemu_get_timedate(struct tm *tm, int offset);
60 int qemu_timedate_diff(struct tm *tm);
61
62 /**
63  * is_help_option:
64  * @s: string to test
65  *
66  * Check whether @s is one of the standard strings which indicate
67  * that the user is asking for a list of the valid values for a
68  * command option like -cpu or -M. The current accepted strings
69  * are 'help' and '?'. '?' is deprecated (it is a shell wildcard
70  * which makes it annoying to use in a reliable way) but provided
71  * for backwards compatibility.
72  *
73  * Returns: true if @s is a request for a list.
74  */
75 static inline bool is_help_option(const char *s)
76 {
77     return !strcmp(s, "?") || !strcmp(s, "help");
78 }
79
80 /* util/cutils.c */
81 /**
82  * pstrcpy:
83  * @buf: buffer to copy string into
84  * @buf_size: size of @buf in bytes
85  * @str: string to copy
86  *
87  * Copy @str into @buf, including the trailing NUL, but do not
88  * write more than @buf_size bytes. The resulting buffer is
89  * always NUL terminated (even if the source string was too long).
90  * If @buf_size is zero or negative then no bytes are copied.
91  *
92  * This function is similar to strncpy(), but avoids two of that
93  * function's problems:
94  *  * if @str fits in the buffer, pstrcpy() does not zero-fill the
95  *    remaining space at the end of @buf
96  *  * if @str is too long, pstrcpy() will copy the first @buf_size-1
97  *    bytes and then add a NUL
98  */
99 void pstrcpy(char *buf, int buf_size, const char *str);
100 /**
101  * strpadcpy:
102  * @buf: buffer to copy string into
103  * @buf_size: size of @buf in bytes
104  * @str: string to copy
105  * @pad: character to pad the remainder of @buf with
106  *
107  * Copy @str into @buf (but *not* its trailing NUL!), and then pad the
108  * rest of the buffer with the @pad character. If @str is too large
109  * for the buffer then it is truncated, so that @buf contains the
110  * first @buf_size characters of @str, with no terminator.
111  */
112 void strpadcpy(char *buf, int buf_size, const char *str, char pad);
113 /**
114  * pstrcat:
115  * @buf: buffer containing existing string
116  * @buf_size: size of @buf in bytes
117  * @s: string to concatenate to @buf
118  *
119  * Append a copy of @s to the string already in @buf, but do not
120  * allow the buffer to overflow. If the existing contents of @buf
121  * plus @str would total more than @buf_size bytes, then write
122  * as much of @str as will fit followed by a NUL terminator.
123  *
124  * @buf must already contain a NUL-terminated string, or the
125  * behaviour is undefined.
126  *
127  * Returns: @buf.
128  */
129 char *pstrcat(char *buf, int buf_size, const char *s);
130 /**
131  * strstart:
132  * @str: string to test
133  * @val: prefix string to look for
134  * @ptr: NULL, or pointer to be written to indicate start of
135  *       the remainder of the string
136  *
137  * Test whether @str starts with the prefix @val.
138  * If it does (including the degenerate case where @str and @val
139  * are equal) then return true. If @ptr is not NULL then a
140  * pointer to the first character following the prefix is written
141  * to it. If @val is not a prefix of @str then return false (and
142  * @ptr is not written to).
143  *
144  * Returns: true if @str starts with prefix @val, false otherwise.
145  */
146 int strstart(const char *str, const char *val, const char **ptr);
147 /**
148  * stristart:
149  * @str: string to test
150  * @val: prefix string to look for
151  * @ptr: NULL, or pointer to be written to indicate start of
152  *       the remainder of the string
153  *
154  * Test whether @str starts with the case-insensitive prefix @val.
155  * This function behaves identically to strstart(), except that the
156  * comparison is made after calling qemu_toupper() on each pair of
157  * characters.
158  *
159  * Returns: true if @str starts with case-insensitive prefix @val,
160  *          false otherwise.
161  */
162 int stristart(const char *str, const char *val, const char **ptr);
163 /**
164  * qemu_strnlen:
165  * @s: string
166  * @max_len: maximum number of bytes in @s to scan
167  *
168  * Return the length of the string @s, like strlen(), but do not
169  * examine more than @max_len bytes of the memory pointed to by @s.
170  * If no NUL terminator is found within @max_len bytes, then return
171  * @max_len instead.
172  *
173  * This function has the same behaviour as the POSIX strnlen()
174  * function.
175  *
176  * Returns: length of @s in bytes, or @max_len, whichever is smaller.
177  */
178 int qemu_strnlen(const char *s, int max_len);
179 /**
180  * qemu_strsep:
181  * @input: pointer to string to parse
182  * @delim: string containing delimiter characters to search for
183  *
184  * Locate the first occurrence of any character in @delim within
185  * the string referenced by @input, and replace it with a NUL.
186  * The location of the next character after the delimiter character
187  * is stored into @input.
188  * If the end of the string was reached without finding a delimiter
189  * character, then NULL is stored into @input.
190  * If @input points to a NULL pointer on entry, return NULL.
191  * The return value is always the original value of *@input (and
192  * so now points to a NUL-terminated string corresponding to the
193  * part of the input up to the first delimiter).
194  *
195  * This function has the same behaviour as the BSD strsep() function.
196  *
197  * Returns: the pointer originally in @input.
198  */
199 char *qemu_strsep(char **input, const char *delim);
200 time_t mktimegm(struct tm *tm);
201 int qemu_fdatasync(int fd);
202 int fcntl_setfl(int fd, int flag);
203 int qemu_parse_fd(const char *param);
204 int qemu_strtol(const char *nptr, const char **endptr, int base,
205                 long *result);
206 int qemu_strtoul(const char *nptr, const char **endptr, int base,
207                  unsigned long *result);
208 int qemu_strtoll(const char *nptr, const char **endptr, int base,
209                  int64_t *result);
210 int qemu_strtoull(const char *nptr, const char **endptr, int base,
211                   uint64_t *result);
212
213 int parse_uint(const char *s, unsigned long long *value, char **endptr,
214                int base);
215 int parse_uint_full(const char *s, unsigned long long *value, int base);
216
217 /*
218  * qemu_strtosz() suffixes used to specify the default treatment of an
219  * argument passed to qemu_strtosz() without an explicit suffix.
220  * These should be defined using upper case characters in the range
221  * A-Z, as qemu_strtosz() will use qemu_toupper() on the given argument
222  * prior to comparison.
223  */
224 #define QEMU_STRTOSZ_DEFSUFFIX_EB 'E'
225 #define QEMU_STRTOSZ_DEFSUFFIX_PB 'P'
226 #define QEMU_STRTOSZ_DEFSUFFIX_TB 'T'
227 #define QEMU_STRTOSZ_DEFSUFFIX_GB 'G'
228 #define QEMU_STRTOSZ_DEFSUFFIX_MB 'M'
229 #define QEMU_STRTOSZ_DEFSUFFIX_KB 'K'
230 #define QEMU_STRTOSZ_DEFSUFFIX_B 'B'
231 int64_t qemu_strtosz(const char *nptr, char **end);
232 int64_t qemu_strtosz_suffix(const char *nptr, char **end,
233                             const char default_suffix);
234 int64_t qemu_strtosz_suffix_unit(const char *nptr, char **end,
235                             const char default_suffix, int64_t unit);
236 #define K_BYTE     (1ULL << 10)
237 #define M_BYTE     (1ULL << 20)
238 #define G_BYTE     (1ULL << 30)
239 #define T_BYTE     (1ULL << 40)
240 #define P_BYTE     (1ULL << 50)
241 #define E_BYTE     (1ULL << 60)
242
243 /* used to print char* safely */
244 #define STR_OR_NULL(str) ((str) ? (str) : "null")
245
246 /* id.c */
247
248 typedef enum IdSubSystems {
249     ID_QDEV,
250     ID_BLOCK,
251     ID_MAX      /* last element, used as array size */
252 } IdSubSystems;
253
254 char *id_generate(IdSubSystems id);
255 bool id_wellformed(const char *id);
256
257 /* path.c */
258 void init_paths(const char *prefix);
259 const char *path(const char *pathname);
260
261 #define qemu_isalnum(c)         isalnum((unsigned char)(c))
262 #define qemu_isalpha(c)         isalpha((unsigned char)(c))
263 #define qemu_iscntrl(c)         iscntrl((unsigned char)(c))
264 #define qemu_isdigit(c)         isdigit((unsigned char)(c))
265 #define qemu_isgraph(c)         isgraph((unsigned char)(c))
266 #define qemu_islower(c)         islower((unsigned char)(c))
267 #define qemu_isprint(c)         isprint((unsigned char)(c))
268 #define qemu_ispunct(c)         ispunct((unsigned char)(c))
269 #define qemu_isspace(c)         isspace((unsigned char)(c))
270 #define qemu_isupper(c)         isupper((unsigned char)(c))
271 #define qemu_isxdigit(c)        isxdigit((unsigned char)(c))
272 #define qemu_tolower(c)         tolower((unsigned char)(c))
273 #define qemu_toupper(c)         toupper((unsigned char)(c))
274 #define qemu_isascii(c)         isascii((unsigned char)(c))
275 #define qemu_toascii(c)         toascii((unsigned char)(c))
276
277 void *qemu_oom_check(void *ptr);
278
279 ssize_t qemu_write_full(int fd, const void *buf, size_t count)
280     QEMU_WARN_UNUSED_RESULT;
281
282 #ifndef _WIN32
283 int qemu_pipe(int pipefd[2]);
284 /* like openpty() but also makes it raw; return master fd */
285 int qemu_openpty_raw(int *aslave, char *pty_name);
286 #endif
287
288 #ifdef _WIN32
289 /* MinGW needs type casts for the 'buf' and 'optval' arguments. */
290 #define qemu_getsockopt(sockfd, level, optname, optval, optlen) \
291     getsockopt(sockfd, level, optname, (void *)optval, optlen)
292 #define qemu_setsockopt(sockfd, level, optname, optval, optlen) \
293     setsockopt(sockfd, level, optname, (const void *)optval, optlen)
294 #define qemu_recv(sockfd, buf, len, flags) recv(sockfd, (void *)buf, len, flags)
295 #define qemu_sendto(sockfd, buf, len, flags, destaddr, addrlen) \
296     sendto(sockfd, (const void *)buf, len, flags, destaddr, addrlen)
297 #else
298 #define qemu_getsockopt(sockfd, level, optname, optval, optlen) \
299     getsockopt(sockfd, level, optname, optval, optlen)
300 #define qemu_setsockopt(sockfd, level, optname, optval, optlen) \
301     setsockopt(sockfd, level, optname, optval, optlen)
302 #define qemu_recv(sockfd, buf, len, flags) recv(sockfd, buf, len, flags)
303 #define qemu_sendto(sockfd, buf, len, flags, destaddr, addrlen) \
304     sendto(sockfd, buf, len, flags, destaddr, addrlen)
305 #endif
306
307 /* Error handling.  */
308
309 void QEMU_NORETURN hw_error(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
310
311 struct ParallelIOArg {
312     void *buffer;
313     int count;
314 };
315
316 typedef int (*DMA_transfer_handler) (void *opaque, int nchan, int pos, int size);
317
318 typedef uint64_t pcibus_t;
319
320 typedef struct PCIHostDeviceAddress {
321     unsigned int domain;
322     unsigned int bus;
323     unsigned int slot;
324     unsigned int function;
325 } PCIHostDeviceAddress;
326
327 void tcg_exec_init(unsigned long tb_size);
328 bool tcg_enabled(void);
329
330 void cpu_exec_init_all(void);
331
332 /* Unblock cpu */
333 void qemu_cpu_kick_self(void);
334
335 /* work queue */
336 struct qemu_work_item {
337     struct qemu_work_item *next;
338     void (*func)(void *data);
339     void *data;
340     int done;
341     bool free;
342 };
343
344
345 /**
346  * Sends a (part of) iovec down a socket, yielding when the socket is full, or
347  * Receives data into a (part of) iovec from a socket,
348  * yielding when there is no data in the socket.
349  * The same interface as qemu_sendv_recvv(), with added yielding.
350  * XXX should mark these as coroutine_fn
351  */
352 ssize_t qemu_co_sendv_recvv(int sockfd, struct iovec *iov, unsigned iov_cnt,
353                             size_t offset, size_t bytes, bool do_send);
354 #define qemu_co_recvv(sockfd, iov, iov_cnt, offset, bytes) \
355   qemu_co_sendv_recvv(sockfd, iov, iov_cnt, offset, bytes, false)
356 #define qemu_co_sendv(sockfd, iov, iov_cnt, offset, bytes) \
357   qemu_co_sendv_recvv(sockfd, iov, iov_cnt, offset, bytes, true)
358
359 /**
360  * The same as above, but with just a single buffer
361  */
362 ssize_t qemu_co_send_recv(int sockfd, void *buf, size_t bytes, bool do_send);
363 #define qemu_co_recv(sockfd, buf, bytes) \
364   qemu_co_send_recv(sockfd, buf, bytes, false)
365 #define qemu_co_send(sockfd, buf, bytes) \
366   qemu_co_send_recv(sockfd, buf, bytes, true)
367
368 typedef struct QEMUIOVector {
369     struct iovec *iov;
370     int niov;
371     int nalloc;
372     size_t size;
373 } QEMUIOVector;
374
375 void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint);
376 void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov);
377 void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len);
378 void qemu_iovec_concat(QEMUIOVector *dst,
379                        QEMUIOVector *src, size_t soffset, size_t sbytes);
380 size_t qemu_iovec_concat_iov(QEMUIOVector *dst,
381                              struct iovec *src_iov, unsigned int src_cnt,
382                              size_t soffset, size_t sbytes);
383 bool qemu_iovec_is_zero(QEMUIOVector *qiov);
384 void qemu_iovec_destroy(QEMUIOVector *qiov);
385 void qemu_iovec_reset(QEMUIOVector *qiov);
386 size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset,
387                          void *buf, size_t bytes);
388 size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset,
389                            const void *buf, size_t bytes);
390 size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
391                          int fillc, size_t bytes);
392 ssize_t qemu_iovec_compare(QEMUIOVector *a, QEMUIOVector *b);
393 void qemu_iovec_clone(QEMUIOVector *dest, const QEMUIOVector *src, void *buf);
394 void qemu_iovec_discard_back(QEMUIOVector *qiov, size_t bytes);
395
396 bool buffer_is_zero(const void *buf, size_t len);
397
398 void qemu_progress_init(int enabled, float min_skip);
399 void qemu_progress_end(void);
400 void qemu_progress_print(float delta, int max);
401 const char *qemu_get_vm_name(void);
402
403 #define QEMU_FILE_TYPE_BIOS   0
404 #define QEMU_FILE_TYPE_KEYMAP 1
405 char *qemu_find_file(int type, const char *name);
406
407 /* OS specific functions */
408 void os_setup_early_signal_handling(void);
409 char *os_find_datadir(void);
410 void os_parse_cmd_args(int index, const char *optarg);
411
412 /* Convert a byte between binary and BCD.  */
413 static inline uint8_t to_bcd(uint8_t val)
414 {
415     return ((val / 10) << 4) | (val % 10);
416 }
417
418 static inline uint8_t from_bcd(uint8_t val)
419 {
420     return ((val >> 4) * 10) + (val & 0x0f);
421 }
422
423 /* Round number down to multiple */
424 #define QEMU_ALIGN_DOWN(n, m) ((n) / (m) * (m))
425
426 /* Round number up to multiple */
427 #define QEMU_ALIGN_UP(n, m) QEMU_ALIGN_DOWN((n) + (m) - 1, (m))
428
429 #include "qemu/module.h"
430
431 /*
432  * Implementation of ULEB128 (http://en.wikipedia.org/wiki/LEB128)
433  * Input is limited to 14-bit numbers
434  */
435
436 int uleb128_encode_small(uint8_t *out, uint32_t n);
437 int uleb128_decode_small(const uint8_t *in, uint32_t *n);
438
439 /* unicode.c */
440 int mod_utf8_codepoint(const char *s, size_t n, char **end);
441
442 /*
443  * Hexdump a buffer to a file. An optional string prefix is added to every line
444  */
445
446 void qemu_hexdump(const char *buf, FILE *fp, const char *prefix, size_t size);
447
448 /* vector definitions */
449 #ifdef __ALTIVEC__
450 #include <altivec.h>
451 /* The altivec.h header says we're allowed to undef these for
452  * C++ compatibility.  Here we don't care about C++, but we
453  * undef them anyway to avoid namespace pollution.
454  */
455 #undef vector
456 #undef pixel
457 #undef bool
458 #define VECTYPE        __vector unsigned char
459 #define SPLAT(p)       vec_splat(vec_ld(0, p), 0)
460 #define ALL_EQ(v1, v2) vec_all_eq(v1, v2)
461 #define VEC_OR(v1, v2) ((v1) | (v2))
462 /* altivec.h may redefine the bool macro as vector type.
463  * Reset it to POSIX semantics. */
464 #define bool _Bool
465 #elif defined __SSE2__
466 #include <emmintrin.h>
467 #define VECTYPE        __m128i
468 #define SPLAT(p)       _mm_set1_epi8(*(p))
469 #define ALL_EQ(v1, v2) (_mm_movemask_epi8(_mm_cmpeq_epi8(v1, v2)) == 0xFFFF)
470 #define VEC_OR(v1, v2) (_mm_or_si128(v1, v2))
471 #else
472 #define VECTYPE        unsigned long
473 #define SPLAT(p)       (*(p) * (~0UL / 255))
474 #define ALL_EQ(v1, v2) ((v1) == (v2))
475 #define VEC_OR(v1, v2) ((v1) | (v2))
476 #endif
477
478 #define BUFFER_FIND_NONZERO_OFFSET_UNROLL_FACTOR 8
479 bool can_use_buffer_find_nonzero_offset(const void *buf, size_t len);
480 size_t buffer_find_nonzero_offset(const void *buf, size_t len);
481
482 /*
483  * helper to parse debug environment variables
484  */
485 int parse_debug_env(const char *name, int max, int initial);
486
487 const char *qemu_ether_ntoa(const MACAddr *mac);
488 void page_size_init(void);
489
490 /* returns non-zero if dump is in progress, otherwise zero is
491  * returned. */
492 bool dump_in_progress(void);
493
494 #endif
This page took 0.05206 seconds and 4 git commands to generate.