2
0

qemu-common.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /* Common header file that is included by all of qemu. */
  2. #ifndef QEMU_COMMON_H
  3. #define QEMU_COMMON_H
  4. #include "compiler.h"
  5. #include "config-host.h"
  6. #if defined(__arm__) || defined(__sparc__) || defined(__mips__) || defined(__hppa__) || defined(__ia64__)
  7. #define WORDS_ALIGNED
  8. #endif
  9. #define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
  10. typedef struct QEMUTimer QEMUTimer;
  11. typedef struct QEMUFile QEMUFile;
  12. typedef struct DeviceState DeviceState;
  13. struct Monitor;
  14. typedef struct Monitor Monitor;
  15. typedef struct MigrationParams MigrationParams;
  16. /* we put basic includes here to avoid repeating them in device drivers */
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <stdarg.h>
  20. #include <stdbool.h>
  21. #include <string.h>
  22. #include <strings.h>
  23. #include <inttypes.h>
  24. #include <limits.h>
  25. #include <time.h>
  26. #include <ctype.h>
  27. #include <errno.h>
  28. #include <unistd.h>
  29. #include <fcntl.h>
  30. #include <sys/stat.h>
  31. #include <sys/time.h>
  32. #include <assert.h>
  33. #include <signal.h>
  34. #include <glib.h>
  35. #ifdef _WIN32
  36. #include "qemu-os-win32.h"
  37. #endif
  38. #ifdef CONFIG_POSIX
  39. #include "qemu-os-posix.h"
  40. #endif
  41. #ifndef O_LARGEFILE
  42. #define O_LARGEFILE 0
  43. #endif
  44. #ifndef O_BINARY
  45. #define O_BINARY 0
  46. #endif
  47. #ifndef MAP_ANONYMOUS
  48. #define MAP_ANONYMOUS MAP_ANON
  49. #endif
  50. #ifndef ENOMEDIUM
  51. #define ENOMEDIUM ENODEV
  52. #endif
  53. #if !defined(ENOTSUP)
  54. #define ENOTSUP 4096
  55. #endif
  56. #if !defined(ECANCELED)
  57. #define ECANCELED 4097
  58. #endif
  59. #ifndef TIME_MAX
  60. #define TIME_MAX LONG_MAX
  61. #endif
  62. /* HOST_LONG_BITS is the size of a native pointer in bits. */
  63. #if UINTPTR_MAX == UINT32_MAX
  64. # define HOST_LONG_BITS 32
  65. #elif UINTPTR_MAX == UINT64_MAX
  66. # define HOST_LONG_BITS 64
  67. #else
  68. # error Unknown pointer size
  69. #endif
  70. #ifndef CONFIG_IOVEC
  71. #define CONFIG_IOVEC
  72. struct iovec {
  73. void *iov_base;
  74. size_t iov_len;
  75. };
  76. /*
  77. * Use the same value as Linux for now.
  78. */
  79. #define IOV_MAX 1024
  80. #else
  81. #include <sys/uio.h>
  82. #endif
  83. typedef int (*fprintf_function)(FILE *f, const char *fmt, ...)
  84. GCC_FMT_ATTR(2, 3);
  85. #ifdef _WIN32
  86. #define fsync _commit
  87. #if !defined(lseek)
  88. # define lseek _lseeki64
  89. #endif
  90. int qemu_ftruncate64(int, int64_t);
  91. #if !defined(ftruncate)
  92. # define ftruncate qemu_ftruncate64
  93. #endif
  94. static inline char *realpath(const char *path, char *resolved_path)
  95. {
  96. _fullpath(resolved_path, path, _MAX_PATH);
  97. return resolved_path;
  98. }
  99. #endif
  100. /* icount */
  101. void configure_icount(const char *option);
  102. extern int use_icount;
  103. /* FIXME: Remove NEED_CPU_H. */
  104. #ifndef NEED_CPU_H
  105. #include "osdep.h"
  106. #include "bswap.h"
  107. #else
  108. #include "cpu.h"
  109. #endif /* !defined(NEED_CPU_H) */
  110. /* main function, renamed */
  111. #if defined(CONFIG_COCOA)
  112. int qemu_main(int argc, char **argv, char **envp);
  113. #endif
  114. void qemu_get_timedate(struct tm *tm, int offset);
  115. int qemu_timedate_diff(struct tm *tm);
  116. /**
  117. * is_help_option:
  118. * @s: string to test
  119. *
  120. * Check whether @s is one of the standard strings which indicate
  121. * that the user is asking for a list of the valid values for a
  122. * command option like -cpu or -M. The current accepted strings
  123. * are 'help' and '?'. '?' is deprecated (it is a shell wildcard
  124. * which makes it annoying to use in a reliable way) but provided
  125. * for backwards compatibility.
  126. *
  127. * Returns: true if @s is a request for a list.
  128. */
  129. static inline bool is_help_option(const char *s)
  130. {
  131. return !strcmp(s, "?") || !strcmp(s, "help");
  132. }
  133. /* cutils.c */
  134. void pstrcpy(char *buf, int buf_size, const char *str);
  135. void strpadcpy(char *buf, int buf_size, const char *str, char pad);
  136. char *pstrcat(char *buf, int buf_size, const char *s);
  137. int strstart(const char *str, const char *val, const char **ptr);
  138. int stristart(const char *str, const char *val, const char **ptr);
  139. int qemu_strnlen(const char *s, int max_len);
  140. time_t mktimegm(struct tm *tm);
  141. int qemu_fls(int i);
  142. int qemu_fdatasync(int fd);
  143. int fcntl_setfl(int fd, int flag);
  144. int qemu_parse_fd(const char *param);
  145. int qemu_parse_fdset(const char *param);
  146. /*
  147. * strtosz() suffixes used to specify the default treatment of an
  148. * argument passed to strtosz() without an explicit suffix.
  149. * These should be defined using upper case characters in the range
  150. * A-Z, as strtosz() will use qemu_toupper() on the given argument
  151. * prior to comparison.
  152. */
  153. #define STRTOSZ_DEFSUFFIX_TB 'T'
  154. #define STRTOSZ_DEFSUFFIX_GB 'G'
  155. #define STRTOSZ_DEFSUFFIX_MB 'M'
  156. #define STRTOSZ_DEFSUFFIX_KB 'K'
  157. #define STRTOSZ_DEFSUFFIX_B 'B'
  158. int64_t strtosz(const char *nptr, char **end);
  159. int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix);
  160. int64_t strtosz_suffix_unit(const char *nptr, char **end,
  161. const char default_suffix, int64_t unit);
  162. /* path.c */
  163. void init_paths(const char *prefix);
  164. const char *path(const char *pathname);
  165. #define qemu_isalnum(c) isalnum((unsigned char)(c))
  166. #define qemu_isalpha(c) isalpha((unsigned char)(c))
  167. #define qemu_iscntrl(c) iscntrl((unsigned char)(c))
  168. #define qemu_isdigit(c) isdigit((unsigned char)(c))
  169. #define qemu_isgraph(c) isgraph((unsigned char)(c))
  170. #define qemu_islower(c) islower((unsigned char)(c))
  171. #define qemu_isprint(c) isprint((unsigned char)(c))
  172. #define qemu_ispunct(c) ispunct((unsigned char)(c))
  173. #define qemu_isspace(c) isspace((unsigned char)(c))
  174. #define qemu_isupper(c) isupper((unsigned char)(c))
  175. #define qemu_isxdigit(c) isxdigit((unsigned char)(c))
  176. #define qemu_tolower(c) tolower((unsigned char)(c))
  177. #define qemu_toupper(c) toupper((unsigned char)(c))
  178. #define qemu_isascii(c) isascii((unsigned char)(c))
  179. #define qemu_toascii(c) toascii((unsigned char)(c))
  180. void *qemu_oom_check(void *ptr);
  181. int qemu_open(const char *name, int flags, ...);
  182. int qemu_close(int fd);
  183. ssize_t qemu_write_full(int fd, const void *buf, size_t count)
  184. QEMU_WARN_UNUSED_RESULT;
  185. ssize_t qemu_send_full(int fd, const void *buf, size_t count, int flags)
  186. QEMU_WARN_UNUSED_RESULT;
  187. ssize_t qemu_recv_full(int fd, void *buf, size_t count, int flags)
  188. QEMU_WARN_UNUSED_RESULT;
  189. #ifndef _WIN32
  190. int qemu_eventfd(int pipefd[2]);
  191. int qemu_pipe(int pipefd[2]);
  192. #endif
  193. #ifdef _WIN32
  194. /* MinGW needs a type cast for the 'buf' argument. */
  195. #define qemu_recv(sockfd, buf, len, flags) recv(sockfd, (void *)buf, len, flags)
  196. #define qemu_sendto(sockfd, buf, len, flags, destaddr, addrlen) \
  197. sendto(sockfd, (const void *)buf, len, flags, destaddr, addrlen)
  198. #else
  199. #define qemu_recv(sockfd, buf, len, flags) recv(sockfd, buf, len, flags)
  200. #define qemu_sendto(sockfd, buf, len, flags, destaddr, addrlen) \
  201. sendto(sockfd, buf, len, flags, destaddr, addrlen)
  202. #endif
  203. /* Error handling. */
  204. void QEMU_NORETURN hw_error(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
  205. struct ParallelIOArg {
  206. void *buffer;
  207. int count;
  208. };
  209. typedef int (*DMA_transfer_handler) (void *opaque, int nchan, int pos, int size);
  210. /* A load of opaque types so that device init declarations don't have to
  211. pull in all the real definitions. */
  212. typedef struct NICInfo NICInfo;
  213. typedef struct HCIInfo HCIInfo;
  214. typedef struct AudioState AudioState;
  215. typedef struct BlockDriverState BlockDriverState;
  216. typedef struct DriveInfo DriveInfo;
  217. typedef struct DisplayState DisplayState;
  218. typedef struct DisplayChangeListener DisplayChangeListener;
  219. typedef struct DisplaySurface DisplaySurface;
  220. typedef struct DisplayAllocator DisplayAllocator;
  221. typedef struct PixelFormat PixelFormat;
  222. typedef struct TextConsole TextConsole;
  223. typedef TextConsole QEMUConsole;
  224. typedef struct CharDriverState CharDriverState;
  225. typedef struct MACAddr MACAddr;
  226. typedef struct NetClientState NetClientState;
  227. typedef struct i2c_bus i2c_bus;
  228. typedef struct ISABus ISABus;
  229. typedef struct ISADevice ISADevice;
  230. typedef struct SMBusDevice SMBusDevice;
  231. typedef struct PCIHostState PCIHostState;
  232. typedef struct PCIExpressHost PCIExpressHost;
  233. typedef struct PCIBus PCIBus;
  234. typedef struct PCIDevice PCIDevice;
  235. typedef struct PCIExpressDevice PCIExpressDevice;
  236. typedef struct PCIBridge PCIBridge;
  237. typedef struct PCIEAERMsg PCIEAERMsg;
  238. typedef struct PCIEAERLog PCIEAERLog;
  239. typedef struct PCIEAERErr PCIEAERErr;
  240. typedef struct PCIEPort PCIEPort;
  241. typedef struct PCIESlot PCIESlot;
  242. typedef struct MSIMessage MSIMessage;
  243. typedef struct SerialState SerialState;
  244. typedef struct IRQState *qemu_irq;
  245. typedef struct PCMCIACardState PCMCIACardState;
  246. typedef struct MouseTransformInfo MouseTransformInfo;
  247. typedef struct uWireSlave uWireSlave;
  248. typedef struct I2SCodec I2SCodec;
  249. typedef struct SSIBus SSIBus;
  250. typedef struct EventNotifier EventNotifier;
  251. typedef struct VirtIODevice VirtIODevice;
  252. typedef struct QEMUSGList QEMUSGList;
  253. typedef struct SHPCDevice SHPCDevice;
  254. typedef uint64_t pcibus_t;
  255. typedef enum LostTickPolicy {
  256. LOST_TICK_DISCARD,
  257. LOST_TICK_DELAY,
  258. LOST_TICK_MERGE,
  259. LOST_TICK_SLEW,
  260. LOST_TICK_MAX
  261. } LostTickPolicy;
  262. typedef struct PCIHostDeviceAddress {
  263. unsigned int domain;
  264. unsigned int bus;
  265. unsigned int slot;
  266. unsigned int function;
  267. } PCIHostDeviceAddress;
  268. void tcg_exec_init(unsigned long tb_size);
  269. bool tcg_enabled(void);
  270. void cpu_exec_init_all(void);
  271. /* CPU save/load. */
  272. void cpu_save(QEMUFile *f, void *opaque);
  273. int cpu_load(QEMUFile *f, void *opaque, int version_id);
  274. /* Unblock cpu */
  275. void qemu_cpu_kick(void *env);
  276. void qemu_cpu_kick_self(void);
  277. int qemu_cpu_is_self(void *env);
  278. /* work queue */
  279. struct qemu_work_item {
  280. struct qemu_work_item *next;
  281. void (*func)(void *data);
  282. void *data;
  283. int done;
  284. };
  285. #ifdef CONFIG_USER_ONLY
  286. #define qemu_init_vcpu(env) do { } while (0)
  287. #else
  288. void qemu_init_vcpu(void *env);
  289. #endif
  290. /**
  291. * Sends a (part of) iovec down a socket, yielding when the socket is full, or
  292. * Receives data into a (part of) iovec from a socket,
  293. * yielding when there is no data in the socket.
  294. * The same interface as qemu_sendv_recvv(), with added yielding.
  295. * XXX should mark these as coroutine_fn
  296. */
  297. ssize_t qemu_co_sendv_recvv(int sockfd, struct iovec *iov, unsigned iov_cnt,
  298. size_t offset, size_t bytes, bool do_send);
  299. #define qemu_co_recvv(sockfd, iov, iov_cnt, offset, bytes) \
  300. qemu_co_sendv_recvv(sockfd, iov, iov_cnt, offset, bytes, false)
  301. #define qemu_co_sendv(sockfd, iov, iov_cnt, offset, bytes) \
  302. qemu_co_sendv_recvv(sockfd, iov, iov_cnt, offset, bytes, true)
  303. /**
  304. * The same as above, but with just a single buffer
  305. */
  306. ssize_t qemu_co_send_recv(int sockfd, void *buf, size_t bytes, bool do_send);
  307. #define qemu_co_recv(sockfd, buf, bytes) \
  308. qemu_co_send_recv(sockfd, buf, bytes, false)
  309. #define qemu_co_send(sockfd, buf, bytes) \
  310. qemu_co_send_recv(sockfd, buf, bytes, true)
  311. typedef struct QEMUIOVector {
  312. struct iovec *iov;
  313. int niov;
  314. int nalloc;
  315. size_t size;
  316. } QEMUIOVector;
  317. void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint);
  318. void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov);
  319. void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len);
  320. void qemu_iovec_concat(QEMUIOVector *dst,
  321. QEMUIOVector *src, size_t soffset, size_t sbytes);
  322. void qemu_iovec_destroy(QEMUIOVector *qiov);
  323. void qemu_iovec_reset(QEMUIOVector *qiov);
  324. size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset,
  325. void *buf, size_t bytes);
  326. size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset,
  327. const void *buf, size_t bytes);
  328. size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
  329. int fillc, size_t bytes);
  330. bool buffer_is_zero(const void *buf, size_t len);
  331. void qemu_progress_init(int enabled, float min_skip);
  332. void qemu_progress_end(void);
  333. void qemu_progress_print(float delta, int max);
  334. const char *qemu_get_vm_name(void);
  335. #define QEMU_FILE_TYPE_BIOS 0
  336. #define QEMU_FILE_TYPE_KEYMAP 1
  337. char *qemu_find_file(int type, const char *name);
  338. /* OS specific functions */
  339. void os_setup_early_signal_handling(void);
  340. char *os_find_datadir(const char *argv0);
  341. void os_parse_cmd_args(int index, const char *optarg);
  342. void os_pidfile_error(void);
  343. /* Convert a byte between binary and BCD. */
  344. static inline uint8_t to_bcd(uint8_t val)
  345. {
  346. return ((val / 10) << 4) | (val % 10);
  347. }
  348. static inline uint8_t from_bcd(uint8_t val)
  349. {
  350. return ((val >> 4) * 10) + (val & 0x0f);
  351. }
  352. /* compute with 96 bit intermediate result: (a*b)/c */
  353. static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
  354. {
  355. union {
  356. uint64_t ll;
  357. struct {
  358. #ifdef HOST_WORDS_BIGENDIAN
  359. uint32_t high, low;
  360. #else
  361. uint32_t low, high;
  362. #endif
  363. } l;
  364. } u, res;
  365. uint64_t rl, rh;
  366. u.ll = a;
  367. rl = (uint64_t)u.l.low * (uint64_t)b;
  368. rh = (uint64_t)u.l.high * (uint64_t)b;
  369. rh += (rl >> 32);
  370. res.l.high = rh / c;
  371. res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
  372. return res.ll;
  373. }
  374. /* Round number down to multiple */
  375. #define QEMU_ALIGN_DOWN(n, m) ((n) / (m) * (m))
  376. /* Round number up to multiple */
  377. #define QEMU_ALIGN_UP(n, m) QEMU_ALIGN_DOWN((n) + (m) - 1, (m))
  378. static inline bool is_power_of_2(uint64_t value)
  379. {
  380. if (!value) {
  381. return 0;
  382. }
  383. return !(value & (value - 1));
  384. }
  385. /* round down to the nearest power of 2*/
  386. int64_t pow2floor(int64_t value);
  387. #include "module.h"
  388. /*
  389. * Implementation of ULEB128 (http://en.wikipedia.org/wiki/LEB128)
  390. * Input is limited to 14-bit numbers
  391. */
  392. int uleb128_encode_small(uint8_t *out, uint32_t n);
  393. int uleb128_decode_small(const uint8_t *in, uint32_t *n);
  394. #endif