qemu-common.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. #ifndef TIME_MAX
  56. #define TIME_MAX LONG_MAX
  57. #endif
  58. #ifndef CONFIG_IOVEC
  59. #define CONFIG_IOVEC
  60. struct iovec {
  61. void *iov_base;
  62. size_t iov_len;
  63. };
  64. /*
  65. * Use the same value as Linux for now.
  66. */
  67. #define IOV_MAX 1024
  68. #else
  69. #include <sys/uio.h>
  70. #endif
  71. typedef int (*fprintf_function)(FILE *f, const char *fmt, ...)
  72. GCC_FMT_ATTR(2, 3);
  73. #ifdef _WIN32
  74. #define fsync _commit
  75. #define lseek _lseeki64
  76. int qemu_ftruncate64(int, int64_t);
  77. #define ftruncate qemu_ftruncate64
  78. static inline char *realpath(const char *path, char *resolved_path)
  79. {
  80. _fullpath(resolved_path, path, _MAX_PATH);
  81. return resolved_path;
  82. }
  83. #endif
  84. /* icount */
  85. void configure_icount(const char *option);
  86. extern int use_icount;
  87. /* FIXME: Remove NEED_CPU_H. */
  88. #ifndef NEED_CPU_H
  89. #include "osdep.h"
  90. #include "bswap.h"
  91. #else
  92. #include "cpu.h"
  93. #endif /* !defined(NEED_CPU_H) */
  94. /* main function, renamed */
  95. #if defined(CONFIG_COCOA)
  96. int qemu_main(int argc, char **argv, char **envp);
  97. #endif
  98. void qemu_get_timedate(struct tm *tm, int offset);
  99. int qemu_timedate_diff(struct tm *tm);
  100. /* cutils.c */
  101. void pstrcpy(char *buf, int buf_size, const char *str);
  102. char *pstrcat(char *buf, int buf_size, const char *s);
  103. int strstart(const char *str, const char *val, const char **ptr);
  104. int stristart(const char *str, const char *val, const char **ptr);
  105. int qemu_strnlen(const char *s, int max_len);
  106. time_t mktimegm(struct tm *tm);
  107. int qemu_fls(int i);
  108. int qemu_fdatasync(int fd);
  109. int fcntl_setfl(int fd, int flag);
  110. int qemu_parse_fd(const char *param);
  111. /*
  112. * strtosz() suffixes used to specify the default treatment of an
  113. * argument passed to strtosz() without an explicit suffix.
  114. * These should be defined using upper case characters in the range
  115. * A-Z, as strtosz() will use qemu_toupper() on the given argument
  116. * prior to comparison.
  117. */
  118. #define STRTOSZ_DEFSUFFIX_TB 'T'
  119. #define STRTOSZ_DEFSUFFIX_GB 'G'
  120. #define STRTOSZ_DEFSUFFIX_MB 'M'
  121. #define STRTOSZ_DEFSUFFIX_KB 'K'
  122. #define STRTOSZ_DEFSUFFIX_B 'B'
  123. int64_t strtosz(const char *nptr, char **end);
  124. int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix);
  125. int64_t strtosz_suffix_unit(const char *nptr, char **end,
  126. const char default_suffix, int64_t unit);
  127. /* path.c */
  128. void init_paths(const char *prefix);
  129. const char *path(const char *pathname);
  130. #define qemu_isalnum(c) isalnum((unsigned char)(c))
  131. #define qemu_isalpha(c) isalpha((unsigned char)(c))
  132. #define qemu_iscntrl(c) iscntrl((unsigned char)(c))
  133. #define qemu_isdigit(c) isdigit((unsigned char)(c))
  134. #define qemu_isgraph(c) isgraph((unsigned char)(c))
  135. #define qemu_islower(c) islower((unsigned char)(c))
  136. #define qemu_isprint(c) isprint((unsigned char)(c))
  137. #define qemu_ispunct(c) ispunct((unsigned char)(c))
  138. #define qemu_isspace(c) isspace((unsigned char)(c))
  139. #define qemu_isupper(c) isupper((unsigned char)(c))
  140. #define qemu_isxdigit(c) isxdigit((unsigned char)(c))
  141. #define qemu_tolower(c) tolower((unsigned char)(c))
  142. #define qemu_toupper(c) toupper((unsigned char)(c))
  143. #define qemu_isascii(c) isascii((unsigned char)(c))
  144. #define qemu_toascii(c) toascii((unsigned char)(c))
  145. void *qemu_oom_check(void *ptr);
  146. int qemu_open(const char *name, int flags, ...);
  147. ssize_t qemu_write_full(int fd, const void *buf, size_t count)
  148. QEMU_WARN_UNUSED_RESULT;
  149. void qemu_set_cloexec(int fd);
  150. #ifndef _WIN32
  151. int qemu_eventfd(int pipefd[2]);
  152. int qemu_pipe(int pipefd[2]);
  153. #endif
  154. #ifdef _WIN32
  155. #define qemu_recv(sockfd, buf, len, flags) recv(sockfd, (void *)buf, len, flags)
  156. #else
  157. #define qemu_recv(sockfd, buf, len, flags) recv(sockfd, buf, len, flags)
  158. #endif
  159. /* Error handling. */
  160. void QEMU_NORETURN hw_error(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
  161. struct ParallelIOArg {
  162. void *buffer;
  163. int count;
  164. };
  165. typedef int (*DMA_transfer_handler) (void *opaque, int nchan, int pos, int size);
  166. /* A load of opaque types so that device init declarations don't have to
  167. pull in all the real definitions. */
  168. typedef struct NICInfo NICInfo;
  169. typedef struct HCIInfo HCIInfo;
  170. typedef struct AudioState AudioState;
  171. typedef struct BlockDriverState BlockDriverState;
  172. typedef struct DriveInfo DriveInfo;
  173. typedef struct DisplayState DisplayState;
  174. typedef struct DisplayChangeListener DisplayChangeListener;
  175. typedef struct DisplaySurface DisplaySurface;
  176. typedef struct DisplayAllocator DisplayAllocator;
  177. typedef struct PixelFormat PixelFormat;
  178. typedef struct TextConsole TextConsole;
  179. typedef TextConsole QEMUConsole;
  180. typedef struct CharDriverState CharDriverState;
  181. typedef struct MACAddr MACAddr;
  182. typedef struct VLANState VLANState;
  183. typedef struct VLANClientState VLANClientState;
  184. typedef struct i2c_bus i2c_bus;
  185. typedef struct i2c_slave i2c_slave;
  186. typedef struct SMBusDevice SMBusDevice;
  187. typedef struct PCIHostState PCIHostState;
  188. typedef struct PCIExpressHost PCIExpressHost;
  189. typedef struct PCIBus PCIBus;
  190. typedef struct PCIDevice PCIDevice;
  191. typedef struct PCIExpressDevice PCIExpressDevice;
  192. typedef struct PCIBridge PCIBridge;
  193. typedef struct PCIEAERMsg PCIEAERMsg;
  194. typedef struct PCIEAERLog PCIEAERLog;
  195. typedef struct PCIEAERErr PCIEAERErr;
  196. typedef struct PCIEPort PCIEPort;
  197. typedef struct PCIESlot PCIESlot;
  198. typedef struct SerialState SerialState;
  199. typedef struct IRQState *qemu_irq;
  200. typedef struct PCMCIACardState PCMCIACardState;
  201. typedef struct MouseTransformInfo MouseTransformInfo;
  202. typedef struct uWireSlave uWireSlave;
  203. typedef struct I2SCodec I2SCodec;
  204. typedef struct SSIBus SSIBus;
  205. typedef struct EventNotifier EventNotifier;
  206. typedef struct VirtIODevice VirtIODevice;
  207. typedef struct QEMUSGList QEMUSGList;
  208. typedef uint64_t pcibus_t;
  209. void tcg_exec_init(unsigned long tb_size);
  210. bool tcg_enabled(void);
  211. void cpu_exec_init_all(void);
  212. /* CPU save/load. */
  213. void cpu_save(QEMUFile *f, void *opaque);
  214. int cpu_load(QEMUFile *f, void *opaque, int version_id);
  215. /* Unblock cpu */
  216. void qemu_cpu_kick(void *env);
  217. void qemu_cpu_kick_self(void);
  218. int qemu_cpu_is_self(void *env);
  219. bool all_cpu_threads_idle(void);
  220. /* work queue */
  221. struct qemu_work_item {
  222. struct qemu_work_item *next;
  223. void (*func)(void *data);
  224. void *data;
  225. int done;
  226. };
  227. #ifdef CONFIG_USER_ONLY
  228. #define qemu_init_vcpu(env) do { } while (0)
  229. #else
  230. void qemu_init_vcpu(void *env);
  231. #endif
  232. typedef struct QEMUIOVector {
  233. struct iovec *iov;
  234. int niov;
  235. int nalloc;
  236. size_t size;
  237. } QEMUIOVector;
  238. void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint);
  239. void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov);
  240. void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len);
  241. void qemu_iovec_copy(QEMUIOVector *dst, QEMUIOVector *src, uint64_t skip,
  242. size_t size);
  243. void qemu_iovec_concat(QEMUIOVector *dst, QEMUIOVector *src, size_t size);
  244. void qemu_iovec_destroy(QEMUIOVector *qiov);
  245. void qemu_iovec_reset(QEMUIOVector *qiov);
  246. void qemu_iovec_to_buffer(QEMUIOVector *qiov, void *buf);
  247. void qemu_iovec_from_buffer(QEMUIOVector *qiov, const void *buf, size_t count);
  248. void qemu_iovec_memset(QEMUIOVector *qiov, int c, size_t count);
  249. void qemu_iovec_memset_skip(QEMUIOVector *qiov, int c, size_t count,
  250. size_t skip);
  251. void qemu_progress_init(int enabled, float min_skip);
  252. void qemu_progress_end(void);
  253. void qemu_progress_print(float delta, int max);
  254. #define QEMU_FILE_TYPE_BIOS 0
  255. #define QEMU_FILE_TYPE_KEYMAP 1
  256. char *qemu_find_file(int type, const char *name);
  257. /* OS specific functions */
  258. void os_setup_early_signal_handling(void);
  259. char *os_find_datadir(const char *argv0);
  260. void os_parse_cmd_args(int index, const char *optarg);
  261. void os_pidfile_error(void);
  262. /* Convert a byte between binary and BCD. */
  263. static inline uint8_t to_bcd(uint8_t val)
  264. {
  265. return ((val / 10) << 4) | (val % 10);
  266. }
  267. static inline uint8_t from_bcd(uint8_t val)
  268. {
  269. return ((val >> 4) * 10) + (val & 0x0f);
  270. }
  271. /* compute with 96 bit intermediate result: (a*b)/c */
  272. static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
  273. {
  274. union {
  275. uint64_t ll;
  276. struct {
  277. #ifdef HOST_WORDS_BIGENDIAN
  278. uint32_t high, low;
  279. #else
  280. uint32_t low, high;
  281. #endif
  282. } l;
  283. } u, res;
  284. uint64_t rl, rh;
  285. u.ll = a;
  286. rl = (uint64_t)u.l.low * (uint64_t)b;
  287. rh = (uint64_t)u.l.high * (uint64_t)b;
  288. rh += (rl >> 32);
  289. res.l.high = rh / c;
  290. res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
  291. return res.ll;
  292. }
  293. #include "module.h"
  294. #endif