qemu-common.h 11 KB

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