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
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.
16 #include "config-host.h"
18 #if defined(__arm__) || defined(__sparc__) || defined(__mips__) || defined(__hppa__) || defined(__ia64__)
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
;
30 typedef struct Monitor Monitor
;
31 typedef struct MigrationParams MigrationParams
;
33 /* we put basic includes here to avoid repeating them in device drivers */
54 #include "qemu-os-win32.h"
58 #include "qemu-os-posix.h"
68 #define MAP_ANONYMOUS MAP_ANON
71 #define ENOMEDIUM ENODEV
76 #if !defined(ECANCELED)
77 #define ECANCELED 4097
80 #define TIME_MAX LONG_MAX
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
89 # error Unknown pointer size
99 * Use the same value as Linux for now.
106 typedef int (*fprintf_function
)(FILE *f
, const char *fmt
, ...)
110 #define fsync _commit
112 # define lseek _lseeki64
114 int qemu_ftruncate64(int, int64_t);
115 #if !defined(ftruncate)
116 # define ftruncate qemu_ftruncate64
119 static inline char *realpath(const char *path
, char *resolved_path
)
121 _fullpath(resolved_path
, path
, _MAX_PATH
);
122 return resolved_path
;
127 void configure_icount(const char *option
);
128 extern int use_icount
;
130 /* FIXME: Remove NEED_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
);
147 void qemu_get_timedate(struct tm
*tm
, int offset
);
148 int qemu_timedate_diff(struct tm
*tm
);
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");
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
);
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
);
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
;
228 int qemu_pipe(int pipefd
[2]);
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)
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)
250 /* Error handling. */
252 void QEMU_NORETURN
hw_error(const char *fmt
, ...) GCC_FMT_ATTR(1, 2);
254 struct ParallelIOArg
{
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
{
313 typedef struct PCIHostDeviceAddress
{
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);
326 void cpu_save(QEMUFile
*f
, void *opaque
);
327 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
);
330 void qemu_cpu_kick_self(void);
333 struct qemu_work_item
{
334 struct qemu_work_item
*next
;
335 void (*func
)(void *data
);
340 #ifdef CONFIG_USER_ONLY
341 #define qemu_init_vcpu(env) do { } while (0)
343 void qemu_init_vcpu(void *env
);
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
{
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
)
425 #ifdef HOST_WORDS_BIGENDIAN
435 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
436 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
439 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
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
)
455 return !(value
& (value
- 1));
458 /* round down to the nearest power of 2*/
459 int64_t pow2floor(int64_t value
);
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
);