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