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