qemu-common.h 17 KB

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