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