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