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