Statistics
| Branch: | Revision:

root / qemu-common.h @ 14015304

History | View | Annotate | Download (10.8 kB)

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