]> git.proxmox.com Git - qemu.git/blame - qemu-common.h
mc146818rtc: Handle host clock resets
[qemu.git] / qemu-common.h
CommitLineData
faf07963
PB
1/* Common header file that is included by all of qemu. */
2#ifndef QEMU_COMMON_H
3#define QEMU_COMMON_H
4
5c026320 5#include "compiler.h"
beb6f0de
KW
6#include "config-host.h"
7
082b5557 8#define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
24ebf5f3 9
29e922b6
BS
10typedef struct QEMUTimer QEMUTimer;
11typedef struct QEMUFile QEMUFile;
12typedef struct QEMUBH QEMUBH;
92a16d7a 13typedef struct DeviceState DeviceState;
29e922b6 14
316378e4
JK
15struct Monitor;
16typedef struct Monitor Monitor;
17
faf07963
PB
18/* we put basic includes here to avoid repeating them in device drivers */
19#include <stdlib.h>
20#include <stdio.h>
21#include <stdarg.h>
11165820 22#include <stdbool.h>
faf07963 23#include <string.h>
c8906845 24#include <strings.h>
faf07963
PB
25#include <inttypes.h>
26#include <limits.h>
27#include <time.h>
28#include <ctype.h>
29#include <errno.h>
30#include <unistd.h>
31#include <fcntl.h>
32#include <sys/stat.h>
dcfd0865 33#include <sys/time.h>
55616505 34#include <assert.h>
86f69a92 35#include <signal.h>
faf07963 36
082b5557
BS
37#ifdef _WIN32
38#include "qemu-os-win32.h"
39#endif
40
41#ifdef CONFIG_POSIX
42#include "qemu-os-posix.h"
43#endif
44
faf07963
PB
45#ifndef O_LARGEFILE
46#define O_LARGEFILE 0
47#endif
48#ifndef O_BINARY
49#define O_BINARY 0
50#endif
0e74e66b
JQ
51#ifndef MAP_ANONYMOUS
52#define MAP_ANONYMOUS MAP_ANON
53#endif
faf07963
PB
54#ifndef ENOMEDIUM
55#define ENOMEDIUM ENODEV
56#endif
4c955388 57#if !defined(ENOTSUP)
2880bc32
JQ
58#define ENOTSUP 4096
59#endif
3c9405a0
GH
60#ifndef TIME_MAX
61#define TIME_MAX LONG_MAX
62#endif
faf07963 63
6114fdb0
JQ
64#ifndef CONFIG_IOVEC
65#define CONFIG_IOVEC
bf9298b9
AL
66struct iovec {
67 void *iov_base;
68 size_t iov_len;
69};
e2a305fb
CH
70/*
71 * Use the same value as Linux for now.
72 */
73#define IOV_MAX 1024
331dadde
BS
74#else
75#include <sys/uio.h>
bf9298b9
AL
76#endif
77
f868445a
SW
78typedef int (*fprintf_function)(FILE *f, const char *fmt, ...)
79 GCC_FMT_ATTR(2, 3);
80
faf07963 81#ifdef _WIN32
faf07963
PB
82#define fsync _commit
83#define lseek _lseeki64
64b85a8f 84int qemu_ftruncate64(int, int64_t);
faf07963
PB
85#define ftruncate qemu_ftruncate64
86
faf07963
PB
87static inline char *realpath(const char *path, char *resolved_path)
88{
89 _fullpath(resolved_path, path, _MAX_PATH);
90 return resolved_path;
91}
faf07963
PB
92#endif
93
94/* FIXME: Remove NEED_CPU_H. */
95#ifndef NEED_CPU_H
96
faf07963
PB
97#include "osdep.h"
98#include "bswap.h"
99
100#else
101
102#include "cpu.h"
103
104#endif /* !defined(NEED_CPU_H) */
105
3bbbee18
AF
106/* main function, renamed */
107#if defined(CONFIG_COCOA)
108int qemu_main(int argc, char **argv, char **envp);
109#endif
110
faf07963 111/* bottom halves */
faf07963
PB
112typedef void QEMUBHFunc(void *opaque);
113
9a1e9481
KW
114void async_context_push(void);
115void async_context_pop(void);
116int get_async_context_id(void);
117
faf07963
PB
118QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque);
119void qemu_bh_schedule(QEMUBH *bh);
80d3580b
AL
120/* Bottom halfs that are scheduled from a bottom half handler are instantly
121 * invoked. This can create an infinite loop if a bottom half handler
122 * schedules itself. qemu_bh_schedule_idle() avoids this infinite loop by
123 * ensuring that the bottom half isn't executed until the next main loop
124 * iteration.
125 */
1b435b10 126void qemu_bh_schedule_idle(QEMUBH *bh);
faf07963
PB
127void qemu_bh_cancel(QEMUBH *bh);
128void qemu_bh_delete(QEMUBH *bh);
129int qemu_bh_poll(void);
4f999d05 130void qemu_bh_update_timeout(int *timeout);
faf07963 131
f6503059
AZ
132void qemu_get_timedate(struct tm *tm, int offset);
133int qemu_timedate_diff(struct tm *tm);
134
faf07963
PB
135/* cutils.c */
136void pstrcpy(char *buf, int buf_size, const char *str);
137char *pstrcat(char *buf, int buf_size, const char *s);
138int strstart(const char *str, const char *val, const char **ptr);
139int stristart(const char *str, const char *val, const char **ptr);
d43277c5 140int qemu_strnlen(const char *s, int max_len);
faf07963 141time_t mktimegm(struct tm *tm);
ad46db9a 142int qemu_fls(int i);
6f1953c4 143int qemu_fdatasync(int fd);
db1a4972 144int fcntl_setfl(int fd, int flag);
d8427002 145
d7142456
JS
146/*
147 * strtosz() suffixes used to specify the default treatment of an
148 * argument passed to strtosz() without an explicit suffix.
149 * These should be defined using upper case characters in the range
150 * A-Z, as strtosz() will use qemu_toupper() on the given argument
151 * prior to comparison.
152 */
d8427002
JS
153#define STRTOSZ_DEFSUFFIX_TB 'T'
154#define STRTOSZ_DEFSUFFIX_GB 'G'
155#define STRTOSZ_DEFSUFFIX_MB 'M'
156#define STRTOSZ_DEFSUFFIX_KB 'K'
157#define STRTOSZ_DEFSUFFIX_B 'B'
70b4f4bb
JS
158int64_t strtosz(const char *nptr, char **end);
159int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix);
faf07963 160
37022086
BS
161/* path.c */
162void init_paths(const char *prefix);
163const char *path(const char *pathname);
164
cd390083
BS
165#define qemu_isalnum(c) isalnum((unsigned char)(c))
166#define qemu_isalpha(c) isalpha((unsigned char)(c))
167#define qemu_iscntrl(c) iscntrl((unsigned char)(c))
168#define qemu_isdigit(c) isdigit((unsigned char)(c))
169#define qemu_isgraph(c) isgraph((unsigned char)(c))
170#define qemu_islower(c) islower((unsigned char)(c))
171#define qemu_isprint(c) isprint((unsigned char)(c))
172#define qemu_ispunct(c) ispunct((unsigned char)(c))
173#define qemu_isspace(c) isspace((unsigned char)(c))
174#define qemu_isupper(c) isupper((unsigned char)(c))
175#define qemu_isxdigit(c) isxdigit((unsigned char)(c))
176#define qemu_tolower(c) tolower((unsigned char)(c))
177#define qemu_toupper(c) toupper((unsigned char)(c))
178#define qemu_isascii(c) isascii((unsigned char)(c))
179#define qemu_toascii(c) toascii((unsigned char)(c))
180
b152aa84 181void *qemu_oom_check(void *ptr);
ca10f867 182void *qemu_malloc(size_t size);
2137b4cc 183void *qemu_realloc(void *ptr, size_t size);
ca10f867
AJ
184void *qemu_mallocz(size_t size);
185void qemu_free(void *ptr);
186char *qemu_strdup(const char *str);
ac4b0d0c 187char *qemu_strndup(const char *str, size_t size);
ca10f867 188
d549db5a
GC
189void qemu_mutex_lock_iothread(void);
190void qemu_mutex_unlock_iothread(void);
191
40ff6d7e 192int qemu_open(const char *name, int flags, ...);
7c7c0629
JQ
193ssize_t qemu_write_full(int fd, const void *buf, size_t count)
194 QEMU_WARN_UNUSED_RESULT;
40ff6d7e
KW
195void qemu_set_cloexec(int fd);
196
197#ifndef _WIN32
4d54ec78 198int qemu_add_child_watch(pid_t pid);
f3dfda61 199int qemu_eventfd(int pipefd[2]);
40ff6d7e
KW
200int qemu_pipe(int pipefd[2]);
201#endif
202
87ecb68b
PB
203/* Error handling. */
204
e5924d89 205void QEMU_NORETURN hw_error(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
87ecb68b
PB
206
207/* IO callbacks. */
208typedef void IOReadHandler(void *opaque, const uint8_t *buf, int size);
7b27a769 209typedef int IOCanReadHandler(void *opaque);
87ecb68b
PB
210typedef void IOHandler(void *opaque);
211
02981419
PB
212void qemu_iohandler_fill(int *pnfds, fd_set *readfds, fd_set *writefds, fd_set *xfds);
213void qemu_iohandler_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds, int rc);
214
87ecb68b
PB
215struct ParallelIOArg {
216 void *buffer;
217 int count;
218};
219
220typedef int (*DMA_transfer_handler) (void *opaque, int nchan, int pos, int size);
221
222/* A load of opaque types so that device init declarations don't have to
223 pull in all the real definitions. */
224typedef struct NICInfo NICInfo;
1ae26a18 225typedef struct HCIInfo HCIInfo;
87ecb68b
PB
226typedef struct AudioState AudioState;
227typedef struct BlockDriverState BlockDriverState;
2446333c 228typedef struct DriveInfo DriveInfo;
87ecb68b 229typedef struct DisplayState DisplayState;
7d957bd8
AL
230typedef struct DisplayChangeListener DisplayChangeListener;
231typedef struct DisplaySurface DisplaySurface;
7b5d76da 232typedef struct DisplayAllocator DisplayAllocator;
7d957bd8 233typedef struct PixelFormat PixelFormat;
87ecb68b 234typedef struct TextConsole TextConsole;
c60e08d9 235typedef TextConsole QEMUConsole;
87ecb68b 236typedef struct CharDriverState CharDriverState;
76d32cba 237typedef struct MACAddr MACAddr;
87ecb68b 238typedef struct VLANState VLANState;
f7105843 239typedef struct VLANClientState VLANClientState;
87ecb68b
PB
240typedef struct i2c_bus i2c_bus;
241typedef struct i2c_slave i2c_slave;
242typedef struct SMBusDevice SMBusDevice;
fb47a2e9
IY
243typedef struct PCIHostState PCIHostState;
244typedef struct PCIExpressHost PCIExpressHost;
87ecb68b
PB
245typedef struct PCIBus PCIBus;
246typedef struct PCIDevice PCIDevice;
0428527c 247typedef struct PCIExpressDevice PCIExpressDevice;
68f79994 248typedef struct PCIBridge PCIBridge;
34e65944
IY
249typedef struct PCIEAERMsg PCIEAERMsg;
250typedef struct PCIEAERLog PCIEAERLog;
251typedef struct PCIEAERErr PCIEAERErr;
bc20ba98
IY
252typedef struct PCIEPort PCIEPort;
253typedef struct PCIESlot PCIESlot;
87ecb68b
PB
254typedef struct SerialState SerialState;
255typedef struct IRQState *qemu_irq;
bc24a225
PB
256typedef struct PCMCIACardState PCMCIACardState;
257typedef struct MouseTransformInfo MouseTransformInfo;
258typedef struct uWireSlave uWireSlave;
259typedef struct I2SCodec I2SCodec;
90d37239 260typedef struct SSIBus SSIBus;
2292b339 261typedef struct EventNotifier EventNotifier;
2be24aaa 262typedef struct VirtIODevice VirtIODevice;
87ecb68b 263
186993ee
MT
264typedef uint64_t pcibus_t;
265
d2053c3c
BS
266void cpu_exec_init_all(unsigned long tb_size);
267
b3c7724c
PB
268/* CPU save/load. */
269void cpu_save(QEMUFile *f, void *opaque);
270int cpu_load(QEMUFile *f, void *opaque, int version_id);
271
9e472e10
AL
272/* Force QEMU to stop what it's doing and service IO */
273void qemu_service_io(void);
274
d9f75a4e
AL
275/* Force QEMU to process pending events */
276void qemu_notify_event(void);
277
8edac960
AL
278/* Unblock cpu */
279void qemu_cpu_kick(void *env);
46d62fac 280void qemu_cpu_kick_self(void);
b7680cb6 281int qemu_cpu_is_self(void *env);
ab33fcda 282bool all_cpu_threads_idle(void);
8edac960 283
e82bcec2
MT
284/* work queue */
285struct qemu_work_item {
286 struct qemu_work_item *next;
287 void (*func)(void *data);
288 void *data;
289 int done;
290};
291
0bf46a40
AL
292#ifdef CONFIG_USER_ONLY
293#define qemu_init_vcpu(env) do { } while (0)
294#else
295void qemu_init_vcpu(void *env);
296#endif
297
44e3ee8a
AL
298typedef struct QEMUIOVector {
299 struct iovec *iov;
300 int niov;
301 int nalloc;
249aa745 302 size_t size;
44e3ee8a
AL
303} QEMUIOVector;
304
305void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint);
522584a5 306void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov);
44e3ee8a 307void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len);
b8a83a4f
KW
308void qemu_iovec_copy(QEMUIOVector *dst, QEMUIOVector *src, uint64_t skip,
309 size_t size);
40b4f539 310void qemu_iovec_concat(QEMUIOVector *dst, QEMUIOVector *src, size_t size);
44e3ee8a 311void qemu_iovec_destroy(QEMUIOVector *qiov);
be959463 312void qemu_iovec_reset(QEMUIOVector *qiov);
44e3ee8a 313void qemu_iovec_to_buffer(QEMUIOVector *qiov, void *buf);
249aa745 314void qemu_iovec_from_buffer(QEMUIOVector *qiov, const void *buf, size_t count);
b8a83a4f 315void qemu_iovec_memset(QEMUIOVector *qiov, int c, size_t count);
e0d9c6f9
CT
316void qemu_iovec_memset_skip(QEMUIOVector *qiov, int c, size_t count,
317 size_t skip);
44e3ee8a 318
6b837bc4
JS
319void qemu_progress_init(int enabled, float min_skip);
320void qemu_progress_end(void);
3bfe4dbf 321void qemu_progress_print(float delta, int max);
6b837bc4 322
082b5557
BS
323#define QEMU_FILE_TYPE_BIOS 0
324#define QEMU_FILE_TYPE_KEYMAP 1
325char *qemu_find_file(int type, const char *name);
326
327/* OS specific functions */
328void os_setup_early_signal_handling(void);
329char *os_find_datadir(const char *argv0);
330void os_parse_cmd_args(int index, const char *optarg);
331void os_pidfile_error(void);
332
abd0c6bd
PB
333/* Convert a byte between binary and BCD. */
334static inline uint8_t to_bcd(uint8_t val)
335{
336 return ((val / 10) << 4) | (val % 10);
337}
338
339static inline uint8_t from_bcd(uint8_t val)
340{
341 return ((val >> 4) * 10) + (val & 0x0f);
342}
343
338b922e 344/* compute with 96 bit intermediate result: (a*b)/c */
345static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
346{
347 union {
348 uint64_t ll;
349 struct {
350#ifdef HOST_WORDS_BIGENDIAN
351 uint32_t high, low;
352#else
353 uint32_t low, high;
354#endif
355 } l;
356 } u, res;
357 uint64_t rl, rh;
358
359 u.ll = a;
360 rl = (uint64_t)u.l.low * (uint64_t)b;
361 rh = (uint64_t)u.l.high * (uint64_t)b;
362 rh += (rl >> 32);
363 res.l.high = rh / c;
364 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
365 return res.ll;
366}
367
0bfe3ca5
AL
368#include "module.h"
369
faf07963 370#endif