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 int qemu_open(const char *name
, int flags
, ...);
221 int qemu_close(int fd
);
222 ssize_t
qemu_write_full(int fd
, const void *buf
, size_t count
)
223 QEMU_WARN_UNUSED_RESULT
;
224 ssize_t
qemu_send_full(int fd
, const void *buf
, size_t count
, int flags
)
225 QEMU_WARN_UNUSED_RESULT
;
226 ssize_t
qemu_recv_full(int fd
, void *buf
, size_t count
, int flags
)
227 QEMU_WARN_UNUSED_RESULT
;
230 int qemu_pipe(int pipefd
[2]);
234 /* MinGW needs type casts for the 'buf' and 'optval' arguments. */
235 #define qemu_getsockopt(sockfd, level, optname, optval, optlen) \
236 getsockopt(sockfd, level, optname, (void *)optval, optlen)
237 #define qemu_setsockopt(sockfd, level, optname, optval, optlen) \
238 setsockopt(sockfd, level, optname, (const void *)optval, optlen)
239 #define qemu_recv(sockfd, buf, len, flags) recv(sockfd, (void *)buf, len, flags)
240 #define qemu_sendto(sockfd, buf, len, flags, destaddr, addrlen) \
241 sendto(sockfd, (const void *)buf, len, flags, destaddr, addrlen)
243 #define qemu_getsockopt(sockfd, level, optname, optval, optlen) \
244 getsockopt(sockfd, level, optname, optval, optlen)
245 #define qemu_setsockopt(sockfd, level, optname, optval, optlen) \
246 setsockopt(sockfd, level, optname, optval, optlen)
247 #define qemu_recv(sockfd, buf, len, flags) recv(sockfd, buf, len, flags)
248 #define qemu_sendto(sockfd, buf, len, flags, destaddr, addrlen) \
249 sendto(sockfd, buf, len, flags, destaddr, addrlen)
252 /* Error handling. */
254 void QEMU_NORETURN
hw_error(const char *fmt
, ...) GCC_FMT_ATTR(1, 2);
256 struct ParallelIOArg
{
261 typedef int (*DMA_transfer_handler
) (void *opaque
, int nchan
, int pos
, int size
);
263 /* A load of opaque types so that device init declarations don't have to
264 pull in all the real definitions. */
265 typedef struct NICInfo NICInfo
;
266 typedef struct HCIInfo HCIInfo
;
267 typedef struct AudioState AudioState
;
268 typedef struct BlockDriverState BlockDriverState
;
269 typedef struct DriveInfo DriveInfo
;
270 typedef struct DisplayState DisplayState
;
271 typedef struct DisplayChangeListener DisplayChangeListener
;
272 typedef struct DisplaySurface DisplaySurface
;
273 typedef struct PixelFormat PixelFormat
;
274 typedef struct QemuConsole QemuConsole
;
275 typedef struct CharDriverState CharDriverState
;
276 typedef struct MACAddr MACAddr
;
277 typedef struct NetClientState NetClientState
;
278 typedef struct i2c_bus i2c_bus
;
279 typedef struct ISABus ISABus
;
280 typedef struct ISADevice ISADevice
;
281 typedef struct SMBusDevice SMBusDevice
;
282 typedef struct PCIHostState PCIHostState
;
283 typedef struct PCIExpressHost PCIExpressHost
;
284 typedef struct PCIBus PCIBus
;
285 typedef struct PCIDevice PCIDevice
;
286 typedef struct PCIExpressDevice PCIExpressDevice
;
287 typedef struct PCIBridge PCIBridge
;
288 typedef struct PCIEAERMsg PCIEAERMsg
;
289 typedef struct PCIEAERLog PCIEAERLog
;
290 typedef struct PCIEAERErr PCIEAERErr
;
291 typedef struct PCIEPort PCIEPort
;
292 typedef struct PCIESlot PCIESlot
;
293 typedef struct MSIMessage MSIMessage
;
294 typedef struct SerialState SerialState
;
295 typedef struct PCMCIACardState PCMCIACardState
;
296 typedef struct MouseTransformInfo MouseTransformInfo
;
297 typedef struct uWireSlave uWireSlave
;
298 typedef struct I2SCodec I2SCodec
;
299 typedef struct SSIBus SSIBus
;
300 typedef struct EventNotifier EventNotifier
;
301 typedef struct VirtIODevice VirtIODevice
;
302 typedef struct QEMUSGList QEMUSGList
;
303 typedef struct SHPCDevice SHPCDevice
;
305 typedef uint64_t pcibus_t
;
307 typedef enum LostTickPolicy
{
315 typedef struct PCIHostDeviceAddress
{
319 unsigned int function
;
320 } PCIHostDeviceAddress
;
322 void tcg_exec_init(unsigned long tb_size
);
323 bool tcg_enabled(void);
325 void cpu_exec_init_all(void);
328 void cpu_save(QEMUFile
*f
, void *opaque
);
329 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
);
332 void qemu_cpu_kick_self(void);
335 struct qemu_work_item
{
336 struct qemu_work_item
*next
;
337 void (*func
)(void *data
);
342 #ifdef CONFIG_USER_ONLY
343 #define qemu_init_vcpu(env) do { } while (0)
345 void qemu_init_vcpu(void *env
);
350 * Sends a (part of) iovec down a socket, yielding when the socket is full, or
351 * Receives data into a (part of) iovec from a socket,
352 * yielding when there is no data in the socket.
353 * The same interface as qemu_sendv_recvv(), with added yielding.
354 * XXX should mark these as coroutine_fn
356 ssize_t
qemu_co_sendv_recvv(int sockfd
, struct iovec
*iov
, unsigned iov_cnt
,
357 size_t offset
, size_t bytes
, bool do_send
);
358 #define qemu_co_recvv(sockfd, iov, iov_cnt, offset, bytes) \
359 qemu_co_sendv_recvv(sockfd, iov, iov_cnt, offset, bytes, false)
360 #define qemu_co_sendv(sockfd, iov, iov_cnt, offset, bytes) \
361 qemu_co_sendv_recvv(sockfd, iov, iov_cnt, offset, bytes, true)
364 * The same as above, but with just a single buffer
366 ssize_t
qemu_co_send_recv(int sockfd
, void *buf
, size_t bytes
, bool do_send
);
367 #define qemu_co_recv(sockfd, buf, bytes) \
368 qemu_co_send_recv(sockfd, buf, bytes, false)
369 #define qemu_co_send(sockfd, buf, bytes) \
370 qemu_co_send_recv(sockfd, buf, bytes, true)
372 typedef struct QEMUIOVector
{
379 void qemu_iovec_init(QEMUIOVector
*qiov
, int alloc_hint
);
380 void qemu_iovec_init_external(QEMUIOVector
*qiov
, struct iovec
*iov
, int niov
);
381 void qemu_iovec_add(QEMUIOVector
*qiov
, void *base
, size_t len
);
382 void qemu_iovec_concat(QEMUIOVector
*dst
,
383 QEMUIOVector
*src
, size_t soffset
, size_t sbytes
);
384 void qemu_iovec_destroy(QEMUIOVector
*qiov
);
385 void qemu_iovec_reset(QEMUIOVector
*qiov
);
386 size_t qemu_iovec_to_buf(QEMUIOVector
*qiov
, size_t offset
,
387 void *buf
, size_t bytes
);
388 size_t qemu_iovec_from_buf(QEMUIOVector
*qiov
, size_t offset
,
389 const void *buf
, size_t bytes
);
390 size_t qemu_iovec_memset(QEMUIOVector
*qiov
, size_t offset
,
391 int fillc
, size_t bytes
);
393 bool buffer_is_zero(const void *buf
, size_t len
);
395 void qemu_progress_init(int enabled
, float min_skip
);
396 void qemu_progress_end(void);
397 void qemu_progress_print(float delta
, int max
);
398 const char *qemu_get_vm_name(void);
400 #define QEMU_FILE_TYPE_BIOS 0
401 #define QEMU_FILE_TYPE_KEYMAP 1
402 char *qemu_find_file(int type
, const char *name
);
404 /* OS specific functions */
405 void os_setup_early_signal_handling(void);
406 char *os_find_datadir(const char *argv0
);
407 void os_parse_cmd_args(int index
, const char *optarg
);
408 void os_pidfile_error(void);
410 /* Convert a byte between binary and BCD. */
411 static inline uint8_t to_bcd(uint8_t val
)
413 return ((val
/ 10) << 4) | (val
% 10);
416 static inline uint8_t from_bcd(uint8_t val
)
418 return ((val
>> 4) * 10) + (val
& 0x0f);
421 /* compute with 96 bit intermediate result: (a*b)/c */
422 static inline uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
427 #ifdef HOST_WORDS_BIGENDIAN
437 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
438 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
441 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
445 /* Round number down to multiple */
446 #define QEMU_ALIGN_DOWN(n, m) ((n) / (m) * (m))
448 /* Round number up to multiple */
449 #define QEMU_ALIGN_UP(n, m) QEMU_ALIGN_DOWN((n) + (m) - 1, (m))
451 static inline bool is_power_of_2(uint64_t value
)
457 return !(value
& (value
- 1));
460 /* round down to the nearest power of 2*/
461 int64_t pow2floor(int64_t value
);
466 * Implementation of ULEB128 (http://en.wikipedia.org/wiki/LEB128)
467 * Input is limited to 14-bit numbers
470 int uleb128_encode_small(uint8_t *out
, uint32_t n
);
471 int uleb128_decode_small(const uint8_t *in
, uint32_t *n
);