qemu-common.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. #define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
  7. typedef struct QEMUTimer QEMUTimer;
  8. typedef struct QEMUFile QEMUFile;
  9. typedef struct QEMUBH QEMUBH;
  10. typedef struct DeviceState DeviceState;
  11. struct Monitor;
  12. typedef struct Monitor Monitor;
  13. /* we put basic includes here to avoid repeating them in device drivers */
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <stdarg.h>
  17. #include <stdbool.h>
  18. #include <string.h>
  19. #include <strings.h>
  20. #include <inttypes.h>
  21. #include <limits.h>
  22. #include <time.h>
  23. #include <ctype.h>
  24. #include <errno.h>
  25. #include <unistd.h>
  26. #include <fcntl.h>
  27. #include <sys/stat.h>
  28. #include <sys/time.h>
  29. #include <assert.h>
  30. #include <signal.h>
  31. #ifdef _WIN32
  32. #include "qemu-os-win32.h"
  33. #endif
  34. #ifdef CONFIG_POSIX
  35. #include "qemu-os-posix.h"
  36. #endif
  37. #ifndef O_LARGEFILE
  38. #define O_LARGEFILE 0
  39. #endif
  40. #ifndef O_BINARY
  41. #define O_BINARY 0
  42. #endif
  43. #ifndef MAP_ANONYMOUS
  44. #define MAP_ANONYMOUS MAP_ANON
  45. #endif
  46. #ifndef ENOMEDIUM
  47. #define ENOMEDIUM ENODEV
  48. #endif
  49. #if !defined(ENOTSUP)
  50. #define ENOTSUP 4096
  51. #endif
  52. #ifndef TIME_MAX
  53. #define TIME_MAX LONG_MAX
  54. #endif
  55. #ifndef CONFIG_IOVEC
  56. #define CONFIG_IOVEC
  57. struct iovec {
  58. void *iov_base;
  59. size_t iov_len;
  60. };
  61. /*
  62. * Use the same value as Linux for now.
  63. */
  64. #define IOV_MAX 1024
  65. #else
  66. #include <sys/uio.h>
  67. #endif
  68. typedef int (*fprintf_function)(FILE *f, const char *fmt, ...)
  69. GCC_FMT_ATTR(2, 3);
  70. #ifdef _WIN32
  71. #define fsync _commit
  72. #define lseek _lseeki64
  73. int qemu_ftruncate64(int, int64_t);
  74. #define ftruncate qemu_ftruncate64
  75. static inline char *realpath(const char *path, char *resolved_path)
  76. {
  77. _fullpath(resolved_path, path, _MAX_PATH);
  78. return resolved_path;
  79. }
  80. #endif
  81. /* FIXME: Remove NEED_CPU_H. */
  82. #ifndef NEED_CPU_H
  83. #include "osdep.h"
  84. #include "bswap.h"
  85. #else
  86. #include "cpu.h"
  87. #endif /* !defined(NEED_CPU_H) */
  88. /* main function, renamed */
  89. #if defined(CONFIG_COCOA)
  90. int qemu_main(int argc, char **argv, char **envp);
  91. #endif
  92. /* bottom halves */
  93. typedef void QEMUBHFunc(void *opaque);
  94. void async_context_push(void);
  95. void async_context_pop(void);
  96. int get_async_context_id(void);
  97. QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque);
  98. void qemu_bh_schedule(QEMUBH *bh);
  99. /* Bottom halfs that are scheduled from a bottom half handler are instantly
  100. * invoked. This can create an infinite loop if a bottom half handler
  101. * schedules itself. qemu_bh_schedule_idle() avoids this infinite loop by
  102. * ensuring that the bottom half isn't executed until the next main loop
  103. * iteration.
  104. */
  105. void qemu_bh_schedule_idle(QEMUBH *bh);
  106. void qemu_bh_cancel(QEMUBH *bh);
  107. void qemu_bh_delete(QEMUBH *bh);
  108. int qemu_bh_poll(void);
  109. void qemu_bh_update_timeout(int *timeout);
  110. void qemu_get_timedate(struct tm *tm, int offset);
  111. int qemu_timedate_diff(struct tm *tm);
  112. /* cutils.c */
  113. void pstrcpy(char *buf, int buf_size, const char *str);
  114. char *pstrcat(char *buf, int buf_size, const char *s);
  115. int strstart(const char *str, const char *val, const char **ptr);
  116. int stristart(const char *str, const char *val, const char **ptr);
  117. int qemu_strnlen(const char *s, int max_len);
  118. time_t mktimegm(struct tm *tm);
  119. int qemu_fls(int i);
  120. int qemu_fdatasync(int fd);
  121. int fcntl_setfl(int fd, int flag);
  122. /*
  123. * strtosz() suffixes used to specify the default treatment of an
  124. * argument passed to strtosz() without an explicit suffix.
  125. * These should be defined using upper case characters in the range
  126. * A-Z, as strtosz() will use qemu_toupper() on the given argument
  127. * prior to comparison.
  128. */
  129. #define STRTOSZ_DEFSUFFIX_TB 'T'
  130. #define STRTOSZ_DEFSUFFIX_GB 'G'
  131. #define STRTOSZ_DEFSUFFIX_MB 'M'
  132. #define STRTOSZ_DEFSUFFIX_KB 'K'
  133. #define STRTOSZ_DEFSUFFIX_B 'B'
  134. int64_t strtosz(const char *nptr, char **end);
  135. int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix);
  136. /* path.c */
  137. void init_paths(const char *prefix);
  138. const char *path(const char *pathname);
  139. #define qemu_isalnum(c) isalnum((unsigned char)(c))
  140. #define qemu_isalpha(c) isalpha((unsigned char)(c))
  141. #define qemu_iscntrl(c) iscntrl((unsigned char)(c))
  142. #define qemu_isdigit(c) isdigit((unsigned char)(c))
  143. #define qemu_isgraph(c) isgraph((unsigned char)(c))
  144. #define qemu_islower(c) islower((unsigned char)(c))
  145. #define qemu_isprint(c) isprint((unsigned char)(c))
  146. #define qemu_ispunct(c) ispunct((unsigned char)(c))
  147. #define qemu_isspace(c) isspace((unsigned char)(c))
  148. #define qemu_isupper(c) isupper((unsigned char)(c))
  149. #define qemu_isxdigit(c) isxdigit((unsigned char)(c))
  150. #define qemu_tolower(c) tolower((unsigned char)(c))
  151. #define qemu_toupper(c) toupper((unsigned char)(c))
  152. #define qemu_isascii(c) isascii((unsigned char)(c))
  153. #define qemu_toascii(c) toascii((unsigned char)(c))
  154. void *qemu_oom_check(void *ptr);
  155. void *qemu_malloc(size_t size);
  156. void *qemu_realloc(void *ptr, size_t size);
  157. void *qemu_mallocz(size_t size);
  158. void qemu_free(void *ptr);
  159. char *qemu_strdup(const char *str);
  160. char *qemu_strndup(const char *str, size_t size);
  161. void qemu_mutex_lock_iothread(void);
  162. void qemu_mutex_unlock_iothread(void);
  163. int qemu_open(const char *name, int flags, ...);
  164. ssize_t qemu_write_full(int fd, const void *buf, size_t count)
  165. QEMU_WARN_UNUSED_RESULT;
  166. void qemu_set_cloexec(int fd);
  167. #ifndef _WIN32
  168. int qemu_add_child_watch(pid_t pid);
  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. /* Error handling. */
  178. void QEMU_NORETURN hw_error(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
  179. /* IO callbacks. */
  180. typedef void IOReadHandler(void *opaque, const uint8_t *buf, int size);
  181. typedef int IOCanReadHandler(void *opaque);
  182. typedef void IOHandler(void *opaque);
  183. void qemu_iohandler_fill(int *pnfds, fd_set *readfds, fd_set *writefds, fd_set *xfds);
  184. void qemu_iohandler_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds, int rc);
  185. struct ParallelIOArg {
  186. void *buffer;
  187. int count;
  188. };
  189. typedef int (*DMA_transfer_handler) (void *opaque, int nchan, int pos, int size);
  190. /* A load of opaque types so that device init declarations don't have to
  191. pull in all the real definitions. */
  192. typedef struct NICInfo NICInfo;
  193. typedef struct HCIInfo HCIInfo;
  194. typedef struct AudioState AudioState;
  195. typedef struct BlockDriverState BlockDriverState;
  196. typedef struct DriveInfo DriveInfo;
  197. typedef struct DisplayState DisplayState;
  198. typedef struct DisplayChangeListener DisplayChangeListener;
  199. typedef struct DisplaySurface DisplaySurface;
  200. typedef struct DisplayAllocator DisplayAllocator;
  201. typedef struct PixelFormat PixelFormat;
  202. typedef struct TextConsole TextConsole;
  203. typedef TextConsole QEMUConsole;
  204. typedef struct CharDriverState CharDriverState;
  205. typedef struct MACAddr MACAddr;
  206. typedef struct VLANState VLANState;
  207. typedef struct VLANClientState VLANClientState;
  208. typedef struct i2c_bus i2c_bus;
  209. typedef struct i2c_slave i2c_slave;
  210. typedef struct SMBusDevice SMBusDevice;
  211. typedef struct PCIHostState PCIHostState;
  212. typedef struct PCIExpressHost PCIExpressHost;
  213. typedef struct PCIBus PCIBus;
  214. typedef struct PCIDevice PCIDevice;
  215. typedef struct PCIExpressDevice PCIExpressDevice;
  216. typedef struct PCIBridge PCIBridge;
  217. typedef struct PCIEAERMsg PCIEAERMsg;
  218. typedef struct PCIEAERLog PCIEAERLog;
  219. typedef struct PCIEAERErr PCIEAERErr;
  220. typedef struct PCIEPort PCIEPort;
  221. typedef struct PCIESlot PCIESlot;
  222. typedef struct SerialState SerialState;
  223. typedef struct IRQState *qemu_irq;
  224. typedef struct PCMCIACardState PCMCIACardState;
  225. typedef struct MouseTransformInfo MouseTransformInfo;
  226. typedef struct uWireSlave uWireSlave;
  227. typedef struct I2SCodec I2SCodec;
  228. typedef struct SSIBus SSIBus;
  229. typedef struct EventNotifier EventNotifier;
  230. typedef struct VirtIODevice VirtIODevice;
  231. typedef uint64_t pcibus_t;
  232. void cpu_exec_init_all(unsigned long tb_size);
  233. /* CPU save/load. */
  234. void cpu_save(QEMUFile *f, void *opaque);
  235. int cpu_load(QEMUFile *f, void *opaque, int version_id);
  236. /* Force QEMU to stop what it's doing and service IO */
  237. void qemu_service_io(void);
  238. /* Force QEMU to process pending events */
  239. void qemu_notify_event(void);
  240. /* Unblock cpu */
  241. void qemu_cpu_kick(void *env);
  242. void qemu_cpu_kick_self(void);
  243. int qemu_cpu_is_self(void *env);
  244. bool all_cpu_threads_idle(void);
  245. /* work queue */
  246. struct qemu_work_item {
  247. struct qemu_work_item *next;
  248. void (*func)(void *data);
  249. void *data;
  250. int done;
  251. };
  252. #ifdef CONFIG_USER_ONLY
  253. #define qemu_init_vcpu(env) do { } while (0)
  254. #else
  255. void qemu_init_vcpu(void *env);
  256. #endif
  257. typedef struct QEMUIOVector {
  258. struct iovec *iov;
  259. int niov;
  260. int nalloc;
  261. size_t size;
  262. } QEMUIOVector;
  263. void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint);
  264. void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov);
  265. void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len);
  266. void qemu_iovec_copy(QEMUIOVector *dst, QEMUIOVector *src, uint64_t skip,
  267. size_t size);
  268. void qemu_iovec_concat(QEMUIOVector *dst, QEMUIOVector *src, size_t size);
  269. void qemu_iovec_destroy(QEMUIOVector *qiov);
  270. void qemu_iovec_reset(QEMUIOVector *qiov);
  271. void qemu_iovec_to_buffer(QEMUIOVector *qiov, void *buf);
  272. void qemu_iovec_from_buffer(QEMUIOVector *qiov, const void *buf, size_t count);
  273. void qemu_iovec_memset(QEMUIOVector *qiov, int c, size_t count);
  274. void qemu_iovec_memset_skip(QEMUIOVector *qiov, int c, size_t count,
  275. size_t skip);
  276. void qemu_progress_init(int enabled, float min_skip);
  277. void qemu_progress_end(void);
  278. void qemu_progress_print(float delta, int max);
  279. #define QEMU_FILE_TYPE_BIOS 0
  280. #define QEMU_FILE_TYPE_KEYMAP 1
  281. char *qemu_find_file(int type, const char *name);
  282. /* OS specific functions */
  283. void os_setup_early_signal_handling(void);
  284. char *os_find_datadir(const char *argv0);
  285. void os_parse_cmd_args(int index, const char *optarg);
  286. void os_pidfile_error(void);
  287. /* Convert a byte between binary and BCD. */
  288. static inline uint8_t to_bcd(uint8_t val)
  289. {
  290. return ((val / 10) << 4) | (val % 10);
  291. }
  292. static inline uint8_t from_bcd(uint8_t val)
  293. {
  294. return ((val >> 4) * 10) + (val & 0x0f);
  295. }
  296. /* compute with 96 bit intermediate result: (a*b)/c */
  297. static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
  298. {
  299. union {
  300. uint64_t ll;
  301. struct {
  302. #ifdef HOST_WORDS_BIGENDIAN
  303. uint32_t high, low;
  304. #else
  305. uint32_t low, high;
  306. #endif
  307. } l;
  308. } u, res;
  309. uint64_t rl, rh;
  310. u.ll = a;
  311. rl = (uint64_t)u.l.low * (uint64_t)b;
  312. rh = (uint64_t)u.l.high * (uint64_t)b;
  313. rh += (rl >> 32);
  314. res.l.high = rh / c;
  315. res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
  316. return res.ll;
  317. }
  318. #include "module.h"
  319. #endif