scsi: add a bunch more common sense codes
[qemu/mdroth.git] / qemu-common.h
blob74d5c4b9620852985010f1e5e0854575023e1092
1 /* Common header file that is included by all of qemu. */
2 #ifndef QEMU_COMMON_H
3 #define QEMU_COMMON_H
5 #include "compiler.h"
6 #include "config-host.h"
8 #if defined(__arm__) || defined(__sparc__) || defined(__mips__) || defined(__hppa__) || defined(__ia64__)
9 #define WORDS_ALIGNED
10 #endif
12 #define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
14 typedef struct QEMUTimer QEMUTimer;
15 typedef struct QEMUFile QEMUFile;
16 typedef struct QEMUBH QEMUBH;
17 typedef struct DeviceState DeviceState;
19 struct Monitor;
20 typedef struct Monitor Monitor;
22 /* we put basic includes here to avoid repeating them in device drivers */
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <stdarg.h>
26 #include <stdbool.h>
27 #include <string.h>
28 #include <strings.h>
29 #include <inttypes.h>
30 #include <limits.h>
31 #include <time.h>
32 #include <ctype.h>
33 #include <errno.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <sys/stat.h>
37 #include <sys/time.h>
38 #include <assert.h>
39 #include <signal.h>
41 #ifdef _WIN32
42 #include "qemu-os-win32.h"
43 #endif
45 #ifdef CONFIG_POSIX
46 #include "qemu-os-posix.h"
47 #endif
49 #ifndef O_LARGEFILE
50 #define O_LARGEFILE 0
51 #endif
52 #ifndef O_BINARY
53 #define O_BINARY 0
54 #endif
55 #ifndef MAP_ANONYMOUS
56 #define MAP_ANONYMOUS MAP_ANON
57 #endif
58 #ifndef ENOMEDIUM
59 #define ENOMEDIUM ENODEV
60 #endif
61 #if !defined(ENOTSUP)
62 #define ENOTSUP 4096
63 #endif
64 #ifndef TIME_MAX
65 #define TIME_MAX LONG_MAX
66 #endif
68 #ifndef CONFIG_IOVEC
69 #define CONFIG_IOVEC
70 struct iovec {
71 void *iov_base;
72 size_t iov_len;
75 * Use the same value as Linux for now.
77 #define IOV_MAX 1024
78 #else
79 #include <sys/uio.h>
80 #endif
82 typedef int (*fprintf_function)(FILE *f, const char *fmt, ...)
83 GCC_FMT_ATTR(2, 3);
85 #ifdef _WIN32
86 #define fsync _commit
87 #define lseek _lseeki64
88 int qemu_ftruncate64(int, int64_t);
89 #define ftruncate qemu_ftruncate64
91 static inline char *realpath(const char *path, char *resolved_path)
93 _fullpath(resolved_path, path, _MAX_PATH);
94 return resolved_path;
96 #endif
98 /* FIXME: Remove NEED_CPU_H. */
99 #ifndef NEED_CPU_H
101 #include "osdep.h"
102 #include "bswap.h"
104 #else
106 #include "cpu.h"
108 #endif /* !defined(NEED_CPU_H) */
110 /* main function, renamed */
111 #if defined(CONFIG_COCOA)
112 int qemu_main(int argc, char **argv, char **envp);
113 #endif
115 /* bottom halves */
116 typedef void QEMUBHFunc(void *opaque);
118 QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque);
119 void qemu_bh_schedule(QEMUBH *bh);
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.
126 void qemu_bh_schedule_idle(QEMUBH *bh);
127 void qemu_bh_cancel(QEMUBH *bh);
128 void qemu_bh_delete(QEMUBH *bh);
129 int qemu_bh_poll(void);
130 void qemu_bh_update_timeout(int *timeout);
132 void qemu_get_timedate(struct tm *tm, int offset);
133 int qemu_timedate_diff(struct tm *tm);
135 /* cutils.c */
136 void pstrcpy(char *buf, int buf_size, const char *str);
137 char *pstrcat(char *buf, int buf_size, const char *s);
138 int strstart(const char *str, const char *val, const char **ptr);
139 int stristart(const char *str, const char *val, const char **ptr);
140 int qemu_strnlen(const char *s, int max_len);
141 time_t mktimegm(struct tm *tm);
142 int qemu_fls(int i);
143 int qemu_fdatasync(int fd);
144 int fcntl_setfl(int fd, int flag);
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.
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'
158 int64_t strtosz(const char *nptr, char **end);
159 int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix);
160 int64_t strtosz_suffix_unit(const char *nptr, char **end,
161 const char default_suffix, int64_t unit);
163 /* path.c */
164 void init_paths(const char *prefix);
165 const char *path(const char *pathname);
167 #define qemu_isalnum(c) isalnum((unsigned char)(c))
168 #define qemu_isalpha(c) isalpha((unsigned char)(c))
169 #define qemu_iscntrl(c) iscntrl((unsigned char)(c))
170 #define qemu_isdigit(c) isdigit((unsigned char)(c))
171 #define qemu_isgraph(c) isgraph((unsigned char)(c))
172 #define qemu_islower(c) islower((unsigned char)(c))
173 #define qemu_isprint(c) isprint((unsigned char)(c))
174 #define qemu_ispunct(c) ispunct((unsigned char)(c))
175 #define qemu_isspace(c) isspace((unsigned char)(c))
176 #define qemu_isupper(c) isupper((unsigned char)(c))
177 #define qemu_isxdigit(c) isxdigit((unsigned char)(c))
178 #define qemu_tolower(c) tolower((unsigned char)(c))
179 #define qemu_toupper(c) toupper((unsigned char)(c))
180 #define qemu_isascii(c) isascii((unsigned char)(c))
181 #define qemu_toascii(c) toascii((unsigned char)(c))
183 void *qemu_oom_check(void *ptr);
184 void *qemu_malloc(size_t size);
185 void *qemu_realloc(void *ptr, size_t size);
186 void *qemu_mallocz(size_t size);
187 void qemu_free(void *ptr);
188 char *qemu_strdup(const char *str);
189 char *qemu_strndup(const char *str, size_t size);
191 void qemu_mutex_lock_iothread(void);
192 void qemu_mutex_unlock_iothread(void);
194 int qemu_open(const char *name, int flags, ...);
195 ssize_t qemu_write_full(int fd, const void *buf, size_t count)
196 QEMU_WARN_UNUSED_RESULT;
197 void qemu_set_cloexec(int fd);
199 #ifndef _WIN32
200 int qemu_add_child_watch(pid_t pid);
201 int qemu_eventfd(int pipefd[2]);
202 int qemu_pipe(int pipefd[2]);
203 #endif
205 #ifdef _WIN32
206 #define qemu_recv(sockfd, buf, len, flags) recv(sockfd, (void *)buf, len, flags)
207 #else
208 #define qemu_recv(sockfd, buf, len, flags) recv(sockfd, buf, len, flags)
209 #endif
211 /* Error handling. */
213 void QEMU_NORETURN hw_error(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
215 /* IO callbacks. */
216 typedef void IOReadHandler(void *opaque, const uint8_t *buf, int size);
217 typedef int IOCanReadHandler(void *opaque);
218 typedef void IOHandler(void *opaque);
220 void qemu_iohandler_fill(int *pnfds, fd_set *readfds, fd_set *writefds, fd_set *xfds);
221 void qemu_iohandler_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds, int rc);
223 struct ParallelIOArg {
224 void *buffer;
225 int count;
228 typedef int (*DMA_transfer_handler) (void *opaque, int nchan, int pos, int size);
230 /* A load of opaque types so that device init declarations don't have to
231 pull in all the real definitions. */
232 typedef struct NICInfo NICInfo;
233 typedef struct HCIInfo HCIInfo;
234 typedef struct AudioState AudioState;
235 typedef struct BlockDriverState BlockDriverState;
236 typedef struct DriveInfo DriveInfo;
237 typedef struct DisplayState DisplayState;
238 typedef struct DisplayChangeListener DisplayChangeListener;
239 typedef struct DisplaySurface DisplaySurface;
240 typedef struct DisplayAllocator DisplayAllocator;
241 typedef struct PixelFormat PixelFormat;
242 typedef struct TextConsole TextConsole;
243 typedef TextConsole QEMUConsole;
244 typedef struct CharDriverState CharDriverState;
245 typedef struct MACAddr MACAddr;
246 typedef struct VLANState VLANState;
247 typedef struct VLANClientState VLANClientState;
248 typedef struct i2c_bus i2c_bus;
249 typedef struct i2c_slave i2c_slave;
250 typedef struct SMBusDevice SMBusDevice;
251 typedef struct PCIHostState PCIHostState;
252 typedef struct PCIExpressHost PCIExpressHost;
253 typedef struct PCIBus PCIBus;
254 typedef struct PCIDevice PCIDevice;
255 typedef struct PCIExpressDevice PCIExpressDevice;
256 typedef struct PCIBridge PCIBridge;
257 typedef struct PCIEAERMsg PCIEAERMsg;
258 typedef struct PCIEAERLog PCIEAERLog;
259 typedef struct PCIEAERErr PCIEAERErr;
260 typedef struct PCIEPort PCIEPort;
261 typedef struct PCIESlot PCIESlot;
262 typedef struct SerialState SerialState;
263 typedef struct IRQState *qemu_irq;
264 typedef struct PCMCIACardState PCMCIACardState;
265 typedef struct MouseTransformInfo MouseTransformInfo;
266 typedef struct uWireSlave uWireSlave;
267 typedef struct I2SCodec I2SCodec;
268 typedef struct SSIBus SSIBus;
269 typedef struct EventNotifier EventNotifier;
270 typedef struct VirtIODevice VirtIODevice;
271 typedef struct QEMUSGList QEMUSGList;
273 typedef uint64_t pcibus_t;
275 void tcg_exec_init(unsigned long tb_size);
276 bool tcg_enabled(void);
278 void cpu_exec_init_all(void);
280 /* CPU save/load. */
281 void cpu_save(QEMUFile *f, void *opaque);
282 int cpu_load(QEMUFile *f, void *opaque, int version_id);
284 /* Force QEMU to stop what it's doing and service IO */
285 void qemu_service_io(void);
287 /* Force QEMU to process pending events */
288 void qemu_notify_event(void);
290 /* Unblock cpu */
291 void qemu_cpu_kick(void *env);
292 void qemu_cpu_kick_self(void);
293 int qemu_cpu_is_self(void *env);
294 bool all_cpu_threads_idle(void);
296 /* work queue */
297 struct qemu_work_item {
298 struct qemu_work_item *next;
299 void (*func)(void *data);
300 void *data;
301 int done;
304 #ifdef CONFIG_USER_ONLY
305 #define qemu_init_vcpu(env) do { } while (0)
306 #else
307 void qemu_init_vcpu(void *env);
308 #endif
310 typedef struct QEMUIOVector {
311 struct iovec *iov;
312 int niov;
313 int nalloc;
314 size_t size;
315 } QEMUIOVector;
317 void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint);
318 void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov);
319 void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len);
320 void qemu_iovec_copy(QEMUIOVector *dst, QEMUIOVector *src, uint64_t skip,
321 size_t size);
322 void qemu_iovec_concat(QEMUIOVector *dst, QEMUIOVector *src, size_t size);
323 void qemu_iovec_destroy(QEMUIOVector *qiov);
324 void qemu_iovec_reset(QEMUIOVector *qiov);
325 void qemu_iovec_to_buffer(QEMUIOVector *qiov, void *buf);
326 void qemu_iovec_from_buffer(QEMUIOVector *qiov, const void *buf, size_t count);
327 void qemu_iovec_memset(QEMUIOVector *qiov, int c, size_t count);
328 void qemu_iovec_memset_skip(QEMUIOVector *qiov, int c, size_t count,
329 size_t skip);
331 void qemu_progress_init(int enabled, float min_skip);
332 void qemu_progress_end(void);
333 void qemu_progress_print(float delta, int max);
335 #define QEMU_FILE_TYPE_BIOS 0
336 #define QEMU_FILE_TYPE_KEYMAP 1
337 char *qemu_find_file(int type, const char *name);
339 /* OS specific functions */
340 void os_setup_early_signal_handling(void);
341 char *os_find_datadir(const char *argv0);
342 void os_parse_cmd_args(int index, const char *optarg);
343 void os_pidfile_error(void);
345 /* Convert a byte between binary and BCD. */
346 static inline uint8_t to_bcd(uint8_t val)
348 return ((val / 10) << 4) | (val % 10);
351 static inline uint8_t from_bcd(uint8_t val)
353 return ((val >> 4) * 10) + (val & 0x0f);
356 /* compute with 96 bit intermediate result: (a*b)/c */
357 static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
359 union {
360 uint64_t ll;
361 struct {
362 #ifdef HOST_WORDS_BIGENDIAN
363 uint32_t high, low;
364 #else
365 uint32_t low, high;
366 #endif
367 } l;
368 } u, res;
369 uint64_t rl, rh;
371 u.ll = a;
372 rl = (uint64_t)u.l.low * (uint64_t)b;
373 rh = (uint64_t)u.l.high * (uint64_t)b;
374 rh += (rl >> 32);
375 res.l.high = rh / c;
376 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
377 return res.ll;
380 #include "module.h"
382 #endif