Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / include / qemu-common.h
blobd47211b9d3eecb0fe9b0856eeea711088fc2a474
2 /* Common header file that is included by all of QEMU.
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.
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.
12 #ifndef QEMU_COMMON_H
13 #define QEMU_COMMON_H
15 #include "qemu/osdep.h"
16 #include "qemu/typedefs.h"
17 #include "qemu/fprintf-fn.h"
19 #if defined(__arm__) || defined(__sparc__) || defined(__mips__) || defined(__hppa__) || defined(__ia64__)
20 #define WORDS_ALIGNED
21 #endif
23 #define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
25 #include "glib-compat.h"
26 #include "qemu/option.h"
28 /* HOST_LONG_BITS is the size of a native pointer in bits. */
29 #if UINTPTR_MAX == UINT32_MAX
30 # define HOST_LONG_BITS 32
31 #elif UINTPTR_MAX == UINT64_MAX
32 # define HOST_LONG_BITS 64
33 #else
34 # error Unknown pointer size
35 #endif
37 #define KiB 1024
38 #define MiB (KiB * KiB)
40 /* Trace unassigned memory or i/o accesses. */
41 extern bool trace_unassigned;
43 #ifdef _WIN32
44 #define fsync _commit
45 #if !defined(lseek)
46 # define lseek _lseeki64
47 #endif
48 int qemu_ftruncate64(int, int64_t);
49 #if !defined(ftruncate)
50 # define ftruncate qemu_ftruncate64
51 #endif
53 static inline char *realpath(const char *path, char *resolved_path)
55 _fullpath(resolved_path, path, _MAX_PATH);
56 return resolved_path;
58 #endif
60 void cpu_ticks_init(void);
62 /* icount */
63 void configure_icount(QemuOpts *opts, Error **errp);
64 extern int use_icount;
65 extern int icount_align_option;
66 /* drift information for info jit command */
67 extern int64_t max_delay;
68 extern int64_t max_advance;
69 void dump_drift_info(FILE *f, fprintf_function cpu_fprintf);
71 #include "qemu/bswap.h"
73 /* FIXME: Remove NEED_CPU_H. */
74 #ifdef NEED_CPU_H
75 #include "cpu.h"
76 #endif /* !defined(NEED_CPU_H) */
78 /* main function, renamed */
79 #if defined(CONFIG_COCOA)
80 int qemu_main(int argc, char **argv, char **envp);
81 #endif
83 void qemu_get_timedate(struct tm *tm, int offset);
84 int qemu_timedate_diff(struct tm *tm);
86 /**
87 * is_help_option:
88 * @s: string to test
90 * Check whether @s is one of the standard strings which indicate
91 * that the user is asking for a list of the valid values for a
92 * command option like -cpu or -M. The current accepted strings
93 * are 'help' and '?'. '?' is deprecated (it is a shell wildcard
94 * which makes it annoying to use in a reliable way) but provided
95 * for backwards compatibility.
97 * Returns: true if @s is a request for a list.
99 static inline bool is_help_option(const char *s)
101 return !strcmp(s, "?") || !strcmp(s, "help");
104 /* util/cutils.c */
106 * pstrcpy:
107 * @buf: buffer to copy string into
108 * @buf_size: size of @buf in bytes
109 * @str: string to copy
111 * Copy @str into @buf, including the trailing NUL, but do not
112 * write more than @buf_size bytes. The resulting buffer is
113 * always NUL terminated (even if the source string was too long).
114 * If @buf_size is zero or negative then no bytes are copied.
116 * This function is similar to strncpy(), but avoids two of that
117 * function's problems:
118 * * if @str fits in the buffer, pstrcpy() does not zero-fill the
119 * remaining space at the end of @buf
120 * * if @str is too long, pstrcpy() will copy the first @buf_size-1
121 * bytes and then add a NUL
123 void pstrcpy(char *buf, int buf_size, const char *str);
125 * strpadcpy:
126 * @buf: buffer to copy string into
127 * @buf_size: size of @buf in bytes
128 * @str: string to copy
129 * @pad: character to pad the remainder of @buf with
131 * Copy @str into @buf (but *not* its trailing NUL!), and then pad the
132 * rest of the buffer with the @pad character. If @str is too large
133 * for the buffer then it is truncated, so that @buf contains the
134 * first @buf_size characters of @str, with no terminator.
136 void strpadcpy(char *buf, int buf_size, const char *str, char pad);
138 * pstrcat:
139 * @buf: buffer containing existing string
140 * @buf_size: size of @buf in bytes
141 * @s: string to concatenate to @buf
143 * Append a copy of @s to the string already in @buf, but do not
144 * allow the buffer to overflow. If the existing contents of @buf
145 * plus @str would total more than @buf_size bytes, then write
146 * as much of @str as will fit followed by a NUL terminator.
148 * @buf must already contain a NUL-terminated string, or the
149 * behaviour is undefined.
151 * Returns: @buf.
153 char *pstrcat(char *buf, int buf_size, const char *s);
155 * strstart:
156 * @str: string to test
157 * @val: prefix string to look for
158 * @ptr: NULL, or pointer to be written to indicate start of
159 * the remainder of the string
161 * Test whether @str starts with the prefix @val.
162 * If it does (including the degenerate case where @str and @val
163 * are equal) then return true. If @ptr is not NULL then a
164 * pointer to the first character following the prefix is written
165 * to it. If @val is not a prefix of @str then return false (and
166 * @ptr is not written to).
168 * Returns: true if @str starts with prefix @val, false otherwise.
170 int strstart(const char *str, const char *val, const char **ptr);
172 * stristart:
173 * @str: string to test
174 * @val: prefix string to look for
175 * @ptr: NULL, or pointer to be written to indicate start of
176 * the remainder of the string
178 * Test whether @str starts with the case-insensitive prefix @val.
179 * This function behaves identically to strstart(), except that the
180 * comparison is made after calling qemu_toupper() on each pair of
181 * characters.
183 * Returns: true if @str starts with case-insensitive prefix @val,
184 * false otherwise.
186 int stristart(const char *str, const char *val, const char **ptr);
188 * qemu_strnlen:
189 * @s: string
190 * @max_len: maximum number of bytes in @s to scan
192 * Return the length of the string @s, like strlen(), but do not
193 * examine more than @max_len bytes of the memory pointed to by @s.
194 * If no NUL terminator is found within @max_len bytes, then return
195 * @max_len instead.
197 * This function has the same behaviour as the POSIX strnlen()
198 * function.
200 * Returns: length of @s in bytes, or @max_len, whichever is smaller.
202 int qemu_strnlen(const char *s, int max_len);
204 * qemu_strsep:
205 * @input: pointer to string to parse
206 * @delim: string containing delimiter characters to search for
208 * Locate the first occurrence of any character in @delim within
209 * the string referenced by @input, and replace it with a NUL.
210 * The location of the next character after the delimiter character
211 * is stored into @input.
212 * If the end of the string was reached without finding a delimiter
213 * character, then NULL is stored into @input.
214 * If @input points to a NULL pointer on entry, return NULL.
215 * The return value is always the original value of *@input (and
216 * so now points to a NUL-terminated string corresponding to the
217 * part of the input up to the first delimiter).
219 * This function has the same behaviour as the BSD strsep() function.
221 * Returns: the pointer originally in @input.
223 char *qemu_strsep(char **input, const char *delim);
224 time_t mktimegm(struct tm *tm);
225 int qemu_fls(int i);
226 int qemu_fdatasync(int fd);
227 int fcntl_setfl(int fd, int flag);
228 int qemu_parse_fd(const char *param);
230 int parse_uint(const char *s, unsigned long long *value, char **endptr,
231 int base);
232 int parse_uint_full(const char *s, unsigned long long *value, int base);
235 * strtosz() suffixes used to specify the default treatment of an
236 * argument passed to strtosz() without an explicit suffix.
237 * These should be defined using upper case characters in the range
238 * A-Z, as strtosz() will use qemu_toupper() on the given argument
239 * prior to comparison.
241 #define STRTOSZ_DEFSUFFIX_EB 'E'
242 #define STRTOSZ_DEFSUFFIX_PB 'P'
243 #define STRTOSZ_DEFSUFFIX_TB 'T'
244 #define STRTOSZ_DEFSUFFIX_GB 'G'
245 #define STRTOSZ_DEFSUFFIX_MB 'M'
246 #define STRTOSZ_DEFSUFFIX_KB 'K'
247 #define STRTOSZ_DEFSUFFIX_B 'B'
248 int64_t strtosz(const char *nptr, char **end);
249 int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix);
250 int64_t strtosz_suffix_unit(const char *nptr, char **end,
251 const char default_suffix, int64_t unit);
252 #define K_BYTE (1ULL << 10)
253 #define M_BYTE (1ULL << 20)
254 #define G_BYTE (1ULL << 30)
255 #define T_BYTE (1ULL << 40)
256 #define P_BYTE (1ULL << 50)
257 #define E_BYTE (1ULL << 60)
259 /* used to print char* safely */
260 #define STR_OR_NULL(str) ((str) ? (str) : "null")
262 /* id.c */
263 bool id_wellformed(const char *id);
265 /* path.c */
266 void init_paths(const char *prefix);
267 const char *path(const char *pathname);
269 #define qemu_isalnum(c) isalnum((unsigned char)(c))
270 #define qemu_isalpha(c) isalpha((unsigned char)(c))
271 #define qemu_iscntrl(c) iscntrl((unsigned char)(c))
272 #define qemu_isdigit(c) isdigit((unsigned char)(c))
273 #define qemu_isgraph(c) isgraph((unsigned char)(c))
274 #define qemu_islower(c) islower((unsigned char)(c))
275 #define qemu_isprint(c) isprint((unsigned char)(c))
276 #define qemu_ispunct(c) ispunct((unsigned char)(c))
277 #define qemu_isspace(c) isspace((unsigned char)(c))
278 #define qemu_isupper(c) isupper((unsigned char)(c))
279 #define qemu_isxdigit(c) isxdigit((unsigned char)(c))
280 #define qemu_tolower(c) tolower((unsigned char)(c))
281 #define qemu_toupper(c) toupper((unsigned char)(c))
282 #define qemu_isascii(c) isascii((unsigned char)(c))
283 #define qemu_toascii(c) toascii((unsigned char)(c))
285 void *qemu_oom_check(void *ptr);
287 ssize_t qemu_write_full(int fd, const void *buf, size_t count)
288 QEMU_WARN_UNUSED_RESULT;
290 #ifndef _WIN32
291 int qemu_pipe(int pipefd[2]);
292 /* like openpty() but also makes it raw; return master fd */
293 int qemu_openpty_raw(int *aslave, char *pty_name);
294 #endif
296 /* Error handling. */
298 void QEMU_NORETURN hw_error(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
300 struct ParallelIOArg {
301 void *buffer;
302 int count;
305 typedef int (*DMA_transfer_handler) (void *opaque, int nchan, int pos, int size);
307 typedef uint64_t pcibus_t;
309 typedef struct PCIHostDeviceAddress {
310 unsigned int domain;
311 unsigned int bus;
312 unsigned int slot;
313 unsigned int function;
314 } PCIHostDeviceAddress;
316 void tcg_exec_init(uintptr_t tb_size);
317 bool tcg_enabled(void);
319 void cpu_exec_init_all(void);
321 /* CPU save/load. */
322 #ifdef CPU_SAVE_VERSION
323 void cpu_save(QEMUFile *f, void *opaque);
324 int cpu_load(QEMUFile *f, void *opaque, int version_id);
325 #endif
327 /* Unblock cpu */
328 void qemu_cpu_kick_self(void);
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;
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
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)
355 * The same as above, but with just a single buffer
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)
363 typedef struct QEMUIOVector {
364 struct iovec *iov;
365 int niov;
366 int nalloc;
367 size_t size;
368 } QEMUIOVector;
370 void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint);
371 void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov);
372 void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len);
373 void qemu_iovec_concat(QEMUIOVector *dst,
374 QEMUIOVector *src, size_t soffset, size_t sbytes);
375 size_t qemu_iovec_concat_iov(QEMUIOVector *dst,
376 struct iovec *src_iov, unsigned int src_cnt,
377 size_t soffset, size_t sbytes);
378 bool qemu_iovec_is_zero(QEMUIOVector *qiov);
379 void qemu_iovec_destroy(QEMUIOVector *qiov);
380 void qemu_iovec_reset(QEMUIOVector *qiov);
381 size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset,
382 void *buf, size_t bytes);
383 size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset,
384 const void *buf, size_t bytes);
385 size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
386 int fillc, size_t bytes);
387 ssize_t qemu_iovec_compare(QEMUIOVector *a, QEMUIOVector *b);
388 void qemu_iovec_clone(QEMUIOVector *dest, const QEMUIOVector *src, void *buf);
389 void qemu_iovec_discard_back(QEMUIOVector *qiov, size_t bytes);
391 bool buffer_is_zero(const void *buf, size_t len);
393 void qemu_progress_init(int enabled, float min_skip);
394 void qemu_progress_end(void);
395 void qemu_progress_print(float delta, int max);
396 const char *qemu_get_vm_name(void);
398 #define QEMU_FILE_TYPE_BIOS 0
399 #define QEMU_FILE_TYPE_KEYMAP 1
400 char *qemu_find_file(int type, const char *name);
402 /* OS specific functions */
403 void os_setup_early_signal_handling(void);
404 char *os_find_datadir(void);
405 void os_parse_cmd_args(int index, const char *optarg);
407 /* Convert a byte between binary and BCD. */
408 static inline uint8_t to_bcd(uint8_t val)
410 return ((val / 10) << 4) | (val % 10);
413 static inline uint8_t from_bcd(uint8_t val)
415 return ((val >> 4) * 10) + (val & 0x0f);
418 /* Round number down to multiple */
419 #define QEMU_ALIGN_DOWN(n, m) ((n) / (m) * (m))
421 /* Round number up to multiple */
422 #define QEMU_ALIGN_UP(n, m) QEMU_ALIGN_DOWN((n) + (m) - 1, (m))
424 static inline bool is_power_of_2(uint64_t value)
426 if (!value) {
427 return 0;
430 return !(value & (value - 1));
433 /* round down to the nearest power of 2*/
434 int64_t pow2floor(int64_t value);
436 /* round up to the nearest power of 2 (0 if overflow) */
437 uint64_t pow2ceil(uint64_t value);
439 #include "qemu/module.h"
442 * Implementation of ULEB128 (http://en.wikipedia.org/wiki/LEB128)
443 * Input is limited to 14-bit numbers
446 int uleb128_encode_small(uint8_t *out, uint32_t n);
447 int uleb128_decode_small(const uint8_t *in, uint32_t *n);
449 /* unicode.c */
450 int mod_utf8_codepoint(const char *s, size_t n, char **end);
453 * Hexdump a buffer to a file. An optional string prefix is added to every line
456 void qemu_hexdump(const char *buf, FILE *fp, const char *prefix, size_t size);
458 /* vector definitions */
459 #ifdef __ALTIVEC__
460 #include <altivec.h>
461 /* The altivec.h header says we're allowed to undef these for
462 * C++ compatibility. Here we don't care about C++, but we
463 * undef them anyway to avoid namespace pollution.
465 #undef vector
466 #undef pixel
467 #undef bool
468 #define VECTYPE __vector unsigned char
469 #define SPLAT(p) vec_splat(vec_ld(0, p), 0)
470 #define ALL_EQ(v1, v2) vec_all_eq(v1, v2)
471 #define VEC_OR(v1, v2) ((v1) | (v2))
472 /* altivec.h may redefine the bool macro as vector type.
473 * Reset it to POSIX semantics. */
474 #define bool _Bool
475 #elif defined __SSE2__
476 #include <emmintrin.h>
477 #define VECTYPE __m128i
478 #define SPLAT(p) _mm_set1_epi8(*(p))
479 #define ALL_EQ(v1, v2) (_mm_movemask_epi8(_mm_cmpeq_epi8(v1, v2)) == 0xFFFF)
480 #define VEC_OR(v1, v2) (_mm_or_si128(v1, v2))
481 #else
482 #define VECTYPE unsigned long
483 #define SPLAT(p) (*(p) * (~0UL / 255))
484 #define ALL_EQ(v1, v2) ((v1) == (v2))
485 #define VEC_OR(v1, v2) ((v1) | (v2))
486 #endif
488 #define BUFFER_FIND_NONZERO_OFFSET_UNROLL_FACTOR 8
489 static inline bool
490 can_use_buffer_find_nonzero_offset(const void *buf, size_t len)
492 return (len % (BUFFER_FIND_NONZERO_OFFSET_UNROLL_FACTOR
493 * sizeof(VECTYPE)) == 0
494 && ((uintptr_t) buf) % sizeof(VECTYPE) == 0);
496 size_t buffer_find_nonzero_offset(const void *buf, size_t len);
499 * helper to parse debug environment variables
501 int parse_debug_env(const char *name, int max, int initial);
503 const char *qemu_ether_ntoa(const MACAddr *mac);
505 #endif