qemu-common.h 9.4 KB

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