Orit Wasserman | 9fb2664 | 2012-08-06 21:42:50 +0300 | [diff] [blame] | 1 | |
Eduardo Habkost | 04509ad | 2012-10-23 19:44:02 -0200 | [diff] [blame] | 2 | /* Common header file that is included by all of QEMU. |
| 3 | * |
| 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 |
| 6 | * dependencies. |
| 7 | * |
| 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. |
| 11 | */ |
pbrook | faf0796 | 2007-11-11 02:51:17 +0000 | [diff] [blame] | 12 | #ifndef QEMU_COMMON_H |
| 13 | #define QEMU_COMMON_H |
| 14 | |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 15 | #include "qemu/compiler.h" |
Kevin Wolf | beb6f0d | 2010-01-15 12:56:41 +0100 | [diff] [blame] | 16 | #include "config-host.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 17 | #include "qemu/typedefs.h" |
Kevin Wolf | beb6f0d | 2010-01-15 12:56:41 +0100 | [diff] [blame] | 18 | |
Paolo Bonzini | 332ae28 | 2011-07-28 12:10:28 +0200 | [diff] [blame] | 19 | #if defined(__arm__) || defined(__sparc__) || defined(__mips__) || defined(__hppa__) || defined(__ia64__) |
| 20 | #define WORDS_ALIGNED |
| 21 | #endif |
| 22 | |
Blue Swirl | 082b555 | 2011-03-27 09:04:57 +0000 | [diff] [blame] | 23 | #define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR) |
Paolo Bonzini | 24ebf5f | 2010-02-18 21:25:23 +0100 | [diff] [blame] | 24 | |
pbrook | faf0796 | 2007-11-11 02:51:17 +0000 | [diff] [blame] | 25 | /* we put basic includes here to avoid repeating them in device drivers */ |
| 26 | #include <stdlib.h> |
| 27 | #include <stdio.h> |
| 28 | #include <stdarg.h> |
Paul Brook | 1116582 | 2010-06-13 19:00:50 +0100 | [diff] [blame] | 29 | #include <stdbool.h> |
pbrook | faf0796 | 2007-11-11 02:51:17 +0000 | [diff] [blame] | 30 | #include <string.h> |
balrog | c890684 | 2008-11-12 17:18:41 +0000 | [diff] [blame] | 31 | #include <strings.h> |
pbrook | faf0796 | 2007-11-11 02:51:17 +0000 | [diff] [blame] | 32 | #include <inttypes.h> |
| 33 | #include <limits.h> |
| 34 | #include <time.h> |
| 35 | #include <ctype.h> |
| 36 | #include <errno.h> |
| 37 | #include <unistd.h> |
| 38 | #include <fcntl.h> |
| 39 | #include <sys/stat.h> |
Hervé Poussineau | dcfd086 | 2011-03-06 13:23:13 +0000 | [diff] [blame] | 40 | #include <sys/time.h> |
Paul Brook | 5561650 | 2009-05-13 20:51:49 +0100 | [diff] [blame] | 41 | #include <assert.h> |
Alexandre Raymond | 86f69a9 | 2011-06-01 22:21:30 -0400 | [diff] [blame] | 42 | #include <signal.h> |
Anthony Liguori | 1401530 | 2011-08-20 22:18:37 -0500 | [diff] [blame] | 43 | #include <glib.h> |
pbrook | faf0796 | 2007-11-11 02:51:17 +0000 | [diff] [blame] | 44 | |
Blue Swirl | 082b555 | 2011-03-27 09:04:57 +0000 | [diff] [blame] | 45 | #ifdef _WIN32 |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 46 | #include "sysemu/os-win32.h" |
Blue Swirl | 082b555 | 2011-03-27 09:04:57 +0000 | [diff] [blame] | 47 | #endif |
| 48 | |
| 49 | #ifdef CONFIG_POSIX |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 50 | #include "sysemu/os-posix.h" |
Blue Swirl | 082b555 | 2011-03-27 09:04:57 +0000 | [diff] [blame] | 51 | #endif |
| 52 | |
pbrook | faf0796 | 2007-11-11 02:51:17 +0000 | [diff] [blame] | 53 | #ifndef O_LARGEFILE |
| 54 | #define O_LARGEFILE 0 |
| 55 | #endif |
| 56 | #ifndef O_BINARY |
| 57 | #define O_BINARY 0 |
| 58 | #endif |
Juan Quintela | 0e74e66 | 2009-07-27 16:12:57 +0200 | [diff] [blame] | 59 | #ifndef MAP_ANONYMOUS |
| 60 | #define MAP_ANONYMOUS MAP_ANON |
| 61 | #endif |
pbrook | faf0796 | 2007-11-11 02:51:17 +0000 | [diff] [blame] | 62 | #ifndef ENOMEDIUM |
| 63 | #define ENOMEDIUM ENODEV |
| 64 | #endif |
Anthony Liguori | 4c95538 | 2009-07-28 15:48:31 -0500 | [diff] [blame] | 65 | #if !defined(ENOTSUP) |
Juan Quintela | 2880bc3 | 2009-07-27 16:13:22 +0200 | [diff] [blame] | 66 | #define ENOTSUP 4096 |
| 67 | #endif |
Paolo Bonzini | 2084a8e | 2012-05-10 09:27:50 +0200 | [diff] [blame] | 68 | #if !defined(ECANCELED) |
| 69 | #define ECANCELED 4097 |
| 70 | #endif |
Gerd Hoffmann | 3c9405a | 2010-10-07 11:50:45 +0200 | [diff] [blame] | 71 | #ifndef TIME_MAX |
| 72 | #define TIME_MAX LONG_MAX |
| 73 | #endif |
pbrook | faf0796 | 2007-11-11 02:51:17 +0000 | [diff] [blame] | 74 | |
Stefan Weil | c0fd260 | 2012-02-01 21:04:13 +0100 | [diff] [blame] | 75 | /* HOST_LONG_BITS is the size of a native pointer in bits. */ |
| 76 | #if UINTPTR_MAX == UINT32_MAX |
| 77 | # define HOST_LONG_BITS 32 |
| 78 | #elif UINTPTR_MAX == UINT64_MAX |
| 79 | # define HOST_LONG_BITS 64 |
| 80 | #else |
| 81 | # error Unknown pointer size |
| 82 | #endif |
| 83 | |
Juan Quintela | 6114fdb | 2009-07-27 16:12:59 +0200 | [diff] [blame] | 84 | #ifndef CONFIG_IOVEC |
| 85 | #define CONFIG_IOVEC |
aliguori | bf9298b | 2008-12-05 20:05:26 +0000 | [diff] [blame] | 86 | struct iovec { |
| 87 | void *iov_base; |
| 88 | size_t iov_len; |
| 89 | }; |
Christoph Hellwig | e2a305f | 2010-01-26 14:49:08 +0100 | [diff] [blame] | 90 | /* |
| 91 | * Use the same value as Linux for now. |
| 92 | */ |
| 93 | #define IOV_MAX 1024 |
blueswir1 | 331dadd | 2008-12-06 19:44:56 +0000 | [diff] [blame] | 94 | #else |
| 95 | #include <sys/uio.h> |
aliguori | bf9298b | 2008-12-05 20:05:26 +0000 | [diff] [blame] | 96 | #endif |
| 97 | |
Stefan Weil | f868445 | 2010-10-22 23:03:30 +0200 | [diff] [blame] | 98 | typedef int (*fprintf_function)(FILE *f, const char *fmt, ...) |
| 99 | GCC_FMT_ATTR(2, 3); |
| 100 | |
pbrook | faf0796 | 2007-11-11 02:51:17 +0000 | [diff] [blame] | 101 | #ifdef _WIN32 |
pbrook | faf0796 | 2007-11-11 02:51:17 +0000 | [diff] [blame] | 102 | #define fsync _commit |
Stefan Weil | 371c648 | 2012-03-10 11:14:31 +0100 | [diff] [blame] | 103 | #if !defined(lseek) |
| 104 | # define lseek _lseeki64 |
| 105 | #endif |
Blue Swirl | 64b85a8 | 2011-01-23 16:21:20 +0000 | [diff] [blame] | 106 | int qemu_ftruncate64(int, int64_t); |
Stefan Weil | 371c648 | 2012-03-10 11:14:31 +0100 | [diff] [blame] | 107 | #if !defined(ftruncate) |
| 108 | # define ftruncate qemu_ftruncate64 |
| 109 | #endif |
pbrook | faf0796 | 2007-11-11 02:51:17 +0000 | [diff] [blame] | 110 | |
pbrook | faf0796 | 2007-11-11 02:51:17 +0000 | [diff] [blame] | 111 | static inline char *realpath(const char *path, char *resolved_path) |
| 112 | { |
| 113 | _fullpath(resolved_path, path, _MAX_PATH); |
| 114 | return resolved_path; |
| 115 | } |
pbrook | faf0796 | 2007-11-11 02:51:17 +0000 | [diff] [blame] | 116 | #endif |
| 117 | |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 118 | /* icount */ |
| 119 | void configure_icount(const char *option); |
| 120 | extern int use_icount; |
| 121 | |
pbrook | faf0796 | 2007-11-11 02:51:17 +0000 | [diff] [blame] | 122 | /* FIXME: Remove NEED_CPU_H. */ |
| 123 | #ifndef NEED_CPU_H |
| 124 | |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 125 | #include "qemu/osdep.h" |
| 126 | #include "qemu/bswap.h" |
pbrook | faf0796 | 2007-11-11 02:51:17 +0000 | [diff] [blame] | 127 | |
| 128 | #else |
| 129 | |
| 130 | #include "cpu.h" |
| 131 | |
| 132 | #endif /* !defined(NEED_CPU_H) */ |
| 133 | |
Andreas Färber | 3bbbee1 | 2011-05-29 19:42:51 +0200 | [diff] [blame] | 134 | /* main function, renamed */ |
| 135 | #if defined(CONFIG_COCOA) |
| 136 | int qemu_main(int argc, char **argv, char **envp); |
| 137 | #endif |
| 138 | |
balrog | f650305 | 2008-02-17 11:42:19 +0000 | [diff] [blame] | 139 | void qemu_get_timedate(struct tm *tm, int offset); |
| 140 | int qemu_timedate_diff(struct tm *tm); |
| 141 | |
Peter Maydell | c8057f9 | 2012-08-02 13:45:54 +0100 | [diff] [blame] | 142 | /** |
| 143 | * is_help_option: |
| 144 | * @s: string to test |
| 145 | * |
| 146 | * Check whether @s is one of the standard strings which indicate |
| 147 | * that the user is asking for a list of the valid values for a |
| 148 | * command option like -cpu or -M. The current accepted strings |
| 149 | * are 'help' and '?'. '?' is deprecated (it is a shell wildcard |
| 150 | * which makes it annoying to use in a reliable way) but provided |
| 151 | * for backwards compatibility. |
| 152 | * |
| 153 | * Returns: true if @s is a request for a list. |
| 154 | */ |
| 155 | static inline bool is_help_option(const char *s) |
| 156 | { |
| 157 | return !strcmp(s, "?") || !strcmp(s, "help"); |
| 158 | } |
| 159 | |
pbrook | faf0796 | 2007-11-11 02:51:17 +0000 | [diff] [blame] | 160 | /* cutils.c */ |
| 161 | void pstrcpy(char *buf, int buf_size, const char *str); |
Dmitry Fleytman | 2a025ae | 2012-07-09 08:50:43 +0200 | [diff] [blame] | 162 | void strpadcpy(char *buf, int buf_size, const char *str, char pad); |
pbrook | faf0796 | 2007-11-11 02:51:17 +0000 | [diff] [blame] | 163 | char *pstrcat(char *buf, int buf_size, const char *s); |
| 164 | int strstart(const char *str, const char *val, const char **ptr); |
| 165 | int stristart(const char *str, const char *val, const char **ptr); |
Blue Swirl | d43277c | 2009-07-01 18:24:44 +0000 | [diff] [blame] | 166 | int qemu_strnlen(const char *s, int max_len); |
pbrook | faf0796 | 2007-11-11 02:51:17 +0000 | [diff] [blame] | 167 | time_t mktimegm(struct tm *tm); |
blueswir1 | ad46db9 | 2008-12-11 19:37:54 +0000 | [diff] [blame] | 168 | int qemu_fls(int i); |
Christoph Hellwig | 6f1953c | 2009-09-04 19:01:32 +0200 | [diff] [blame] | 169 | int qemu_fdatasync(int fd); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 170 | int fcntl_setfl(int fd, int flag); |
Stefan Berger | 443916d | 2011-09-28 06:41:32 -0400 | [diff] [blame] | 171 | int qemu_parse_fd(const char *param); |
Jes Sorensen | d842700 | 2010-12-09 14:17:24 +0100 | [diff] [blame] | 172 | |
Jes Sorensen | d714245 | 2011-01-26 11:54:58 +0100 | [diff] [blame] | 173 | /* |
| 174 | * strtosz() suffixes used to specify the default treatment of an |
| 175 | * argument passed to strtosz() without an explicit suffix. |
| 176 | * These should be defined using upper case characters in the range |
| 177 | * A-Z, as strtosz() will use qemu_toupper() on the given argument |
| 178 | * prior to comparison. |
| 179 | */ |
Jes Sorensen | d842700 | 2010-12-09 14:17:24 +0100 | [diff] [blame] | 180 | #define STRTOSZ_DEFSUFFIX_TB 'T' |
| 181 | #define STRTOSZ_DEFSUFFIX_GB 'G' |
| 182 | #define STRTOSZ_DEFSUFFIX_MB 'M' |
| 183 | #define STRTOSZ_DEFSUFFIX_KB 'K' |
| 184 | #define STRTOSZ_DEFSUFFIX_B 'B' |
Jes Sorensen | 70b4f4b | 2011-01-05 11:41:02 +0100 | [diff] [blame] | 185 | int64_t strtosz(const char *nptr, char **end); |
| 186 | int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix); |
Joerg Roedel | a732e1b | 2011-07-07 16:13:11 +0200 | [diff] [blame] | 187 | int64_t strtosz_suffix_unit(const char *nptr, char **end, |
| 188 | const char default_suffix, int64_t unit); |
pbrook | faf0796 | 2007-11-11 02:51:17 +0000 | [diff] [blame] | 189 | |
Blue Swirl | 3702208 | 2009-08-15 07:51:59 +0000 | [diff] [blame] | 190 | /* path.c */ |
| 191 | void init_paths(const char *prefix); |
| 192 | const char *path(const char *pathname); |
| 193 | |
blueswir1 | cd39008 | 2008-11-16 13:53:32 +0000 | [diff] [blame] | 194 | #define qemu_isalnum(c) isalnum((unsigned char)(c)) |
| 195 | #define qemu_isalpha(c) isalpha((unsigned char)(c)) |
| 196 | #define qemu_iscntrl(c) iscntrl((unsigned char)(c)) |
| 197 | #define qemu_isdigit(c) isdigit((unsigned char)(c)) |
| 198 | #define qemu_isgraph(c) isgraph((unsigned char)(c)) |
| 199 | #define qemu_islower(c) islower((unsigned char)(c)) |
| 200 | #define qemu_isprint(c) isprint((unsigned char)(c)) |
| 201 | #define qemu_ispunct(c) ispunct((unsigned char)(c)) |
| 202 | #define qemu_isspace(c) isspace((unsigned char)(c)) |
| 203 | #define qemu_isupper(c) isupper((unsigned char)(c)) |
| 204 | #define qemu_isxdigit(c) isxdigit((unsigned char)(c)) |
| 205 | #define qemu_tolower(c) tolower((unsigned char)(c)) |
| 206 | #define qemu_toupper(c) toupper((unsigned char)(c)) |
| 207 | #define qemu_isascii(c) isascii((unsigned char)(c)) |
| 208 | #define qemu_toascii(c) toascii((unsigned char)(c)) |
| 209 | |
Jes Sorensen | b152aa8 | 2010-10-26 10:39:26 +0200 | [diff] [blame] | 210 | void *qemu_oom_check(void *ptr); |
aurel32 | ca10f86 | 2008-04-11 21:35:42 +0000 | [diff] [blame] | 211 | |
Juan Quintela | 7c7c062 | 2010-01-20 00:56:09 +0100 | [diff] [blame] | 212 | ssize_t qemu_write_full(int fd, const void *buf, size_t count) |
| 213 | QEMU_WARN_UNUSED_RESULT; |
Paolo Bonzini | 993295f | 2011-09-17 16:27:59 +0200 | [diff] [blame] | 214 | ssize_t qemu_send_full(int fd, const void *buf, size_t count, int flags) |
| 215 | QEMU_WARN_UNUSED_RESULT; |
Paolo Bonzini | 8c5135f | 2011-09-08 13:46:25 +0200 | [diff] [blame] | 216 | ssize_t qemu_recv_full(int fd, void *buf, size_t count, int flags) |
Paolo Bonzini | 993295f | 2011-09-17 16:27:59 +0200 | [diff] [blame] | 217 | QEMU_WARN_UNUSED_RESULT; |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 218 | |
| 219 | #ifndef _WIN32 |
| 220 | int qemu_pipe(int pipefd[2]); |
| 221 | #endif |
| 222 | |
Blue Swirl | 00aa004 | 2011-07-23 20:04:29 +0000 | [diff] [blame] | 223 | #ifdef _WIN32 |
Stefan Weil | 58455eb | 2012-09-28 19:07:39 +0200 | [diff] [blame] | 224 | /* MinGW needs type casts for the 'buf' and 'optval' arguments. */ |
| 225 | #define qemu_getsockopt(sockfd, level, optname, optval, optlen) \ |
| 226 | getsockopt(sockfd, level, optname, (void *)optval, optlen) |
| 227 | #define qemu_setsockopt(sockfd, level, optname, optval, optlen) \ |
| 228 | setsockopt(sockfd, level, optname, (const void *)optval, optlen) |
Blue Swirl | 00aa004 | 2011-07-23 20:04:29 +0000 | [diff] [blame] | 229 | #define qemu_recv(sockfd, buf, len, flags) recv(sockfd, (void *)buf, len, flags) |
Stefan Weil | 73062df | 2012-09-22 21:13:28 +0200 | [diff] [blame] | 230 | #define qemu_sendto(sockfd, buf, len, flags, destaddr, addrlen) \ |
| 231 | sendto(sockfd, (const void *)buf, len, flags, destaddr, addrlen) |
Blue Swirl | 00aa004 | 2011-07-23 20:04:29 +0000 | [diff] [blame] | 232 | #else |
Stefan Weil | 58455eb | 2012-09-28 19:07:39 +0200 | [diff] [blame] | 233 | #define qemu_getsockopt(sockfd, level, optname, optval, optlen) \ |
| 234 | getsockopt(sockfd, level, optname, optval, optlen) |
| 235 | #define qemu_setsockopt(sockfd, level, optname, optval, optlen) \ |
| 236 | setsockopt(sockfd, level, optname, optval, optlen) |
Blue Swirl | 00aa004 | 2011-07-23 20:04:29 +0000 | [diff] [blame] | 237 | #define qemu_recv(sockfd, buf, len, flags) recv(sockfd, buf, len, flags) |
Stefan Weil | 73062df | 2012-09-22 21:13:28 +0200 | [diff] [blame] | 238 | #define qemu_sendto(sockfd, buf, len, flags, destaddr, addrlen) \ |
| 239 | sendto(sockfd, buf, len, flags, destaddr, addrlen) |
Blue Swirl | 00aa004 | 2011-07-23 20:04:29 +0000 | [diff] [blame] | 240 | #endif |
| 241 | |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 242 | /* Error handling. */ |
| 243 | |
Stefan Weil | e5924d8 | 2010-09-23 21:28:03 +0200 | [diff] [blame] | 244 | void QEMU_NORETURN hw_error(const char *fmt, ...) GCC_FMT_ATTR(1, 2); |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 245 | |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 246 | struct ParallelIOArg { |
| 247 | void *buffer; |
| 248 | int count; |
| 249 | }; |
| 250 | |
| 251 | typedef int (*DMA_transfer_handler) (void *opaque, int nchan, int pos, int size); |
| 252 | |
Michael S. Tsirkin | 186993e | 2010-02-10 21:25:42 +0200 | [diff] [blame] | 253 | typedef uint64_t pcibus_t; |
| 254 | |
Jan Kiszka | 4e4fa39 | 2012-01-23 20:15:11 +0100 | [diff] [blame] | 255 | typedef enum LostTickPolicy { |
| 256 | LOST_TICK_DISCARD, |
| 257 | LOST_TICK_DELAY, |
| 258 | LOST_TICK_MERGE, |
| 259 | LOST_TICK_SLEW, |
Paolo Bonzini | 1ce0512 | 2012-02-02 22:09:44 +0100 | [diff] [blame] | 260 | LOST_TICK_MAX |
Jan Kiszka | 4e4fa39 | 2012-01-23 20:15:11 +0100 | [diff] [blame] | 261 | } LostTickPolicy; |
| 262 | |
Anthony PERARD | 679042f | 2012-06-21 15:36:23 +0000 | [diff] [blame] | 263 | typedef struct PCIHostDeviceAddress { |
| 264 | unsigned int domain; |
| 265 | unsigned int bus; |
| 266 | unsigned int slot; |
| 267 | unsigned int function; |
| 268 | } PCIHostDeviceAddress; |
| 269 | |
Jan Kiszka | d5ab971 | 2011-08-02 16:10:21 +0200 | [diff] [blame] | 270 | void tcg_exec_init(unsigned long tb_size); |
| 271 | bool tcg_enabled(void); |
| 272 | |
| 273 | void cpu_exec_init_all(void); |
Blue Swirl | d2053c3 | 2010-03-29 19:23:48 +0000 | [diff] [blame] | 274 | |
pbrook | b3c7724 | 2008-06-30 16:31:04 +0000 | [diff] [blame] | 275 | /* CPU save/load. */ |
| 276 | void cpu_save(QEMUFile *f, void *opaque); |
| 277 | int cpu_load(QEMUFile *f, void *opaque, int version_id); |
| 278 | |
aliguori | 8edac96 | 2009-04-24 18:03:45 +0000 | [diff] [blame] | 279 | /* Unblock cpu */ |
Jan Kiszka | 46d62fa | 2011-02-01 22:15:59 +0100 | [diff] [blame] | 280 | void qemu_cpu_kick_self(void); |
aliguori | 8edac96 | 2009-04-24 18:03:45 +0000 | [diff] [blame] | 281 | |
Marcelo Tosatti | e82bcec | 2010-05-04 09:45:22 -0300 | [diff] [blame] | 282 | /* work queue */ |
| 283 | struct qemu_work_item { |
| 284 | struct qemu_work_item *next; |
| 285 | void (*func)(void *data); |
| 286 | void *data; |
| 287 | int done; |
| 288 | }; |
| 289 | |
aliguori | 0bf46a4 | 2009-04-24 18:03:41 +0000 | [diff] [blame] | 290 | #ifdef CONFIG_USER_ONLY |
Andreas Färber | 75a192a | 2013-01-05 14:44:08 +0100 | [diff] [blame] | 291 | static inline void qemu_init_vcpu(void *env) |
| 292 | { |
| 293 | } |
aliguori | 0bf46a4 | 2009-04-24 18:03:41 +0000 | [diff] [blame] | 294 | #else |
| 295 | void qemu_init_vcpu(void *env); |
| 296 | #endif |
| 297 | |
Paolo Bonzini | 8c5135f | 2011-09-08 13:46:25 +0200 | [diff] [blame] | 298 | |
| 299 | /** |
Michael Tokarev | 2fc8ae1 | 2012-06-07 20:22:46 +0400 | [diff] [blame] | 300 | * Sends a (part of) iovec down a socket, yielding when the socket is full, or |
| 301 | * Receives data into a (part of) iovec from a socket, |
| 302 | * yielding when there is no data in the socket. |
| 303 | * The same interface as qemu_sendv_recvv(), with added yielding. |
| 304 | * XXX should mark these as coroutine_fn |
Paolo Bonzini | 8c5135f | 2011-09-08 13:46:25 +0200 | [diff] [blame] | 305 | */ |
Michael Tokarev | 2fc8ae1 | 2012-06-07 20:22:46 +0400 | [diff] [blame] | 306 | ssize_t qemu_co_sendv_recvv(int sockfd, struct iovec *iov, unsigned iov_cnt, |
| 307 | size_t offset, size_t bytes, bool do_send); |
| 308 | #define qemu_co_recvv(sockfd, iov, iov_cnt, offset, bytes) \ |
| 309 | qemu_co_sendv_recvv(sockfd, iov, iov_cnt, offset, bytes, false) |
| 310 | #define qemu_co_sendv(sockfd, iov, iov_cnt, offset, bytes) \ |
| 311 | qemu_co_sendv_recvv(sockfd, iov, iov_cnt, offset, bytes, true) |
Paolo Bonzini | 8c5135f | 2011-09-08 13:46:25 +0200 | [diff] [blame] | 312 | |
| 313 | /** |
Michael Tokarev | 2fc8ae1 | 2012-06-07 20:22:46 +0400 | [diff] [blame] | 314 | * The same as above, but with just a single buffer |
Paolo Bonzini | 8c5135f | 2011-09-08 13:46:25 +0200 | [diff] [blame] | 315 | */ |
Michael Tokarev | 2fc8ae1 | 2012-06-07 20:22:46 +0400 | [diff] [blame] | 316 | ssize_t qemu_co_send_recv(int sockfd, void *buf, size_t bytes, bool do_send); |
| 317 | #define qemu_co_recv(sockfd, buf, bytes) \ |
| 318 | qemu_co_send_recv(sockfd, buf, bytes, false) |
| 319 | #define qemu_co_send(sockfd, buf, bytes) \ |
| 320 | qemu_co_send_recv(sockfd, buf, bytes, true) |
Paolo Bonzini | 8c5135f | 2011-09-08 13:46:25 +0200 | [diff] [blame] | 321 | |
aliguori | 44e3ee8 | 2009-01-22 16:59:20 +0000 | [diff] [blame] | 322 | typedef struct QEMUIOVector { |
| 323 | struct iovec *iov; |
| 324 | int niov; |
| 325 | int nalloc; |
aliguori | 249aa74 | 2009-01-26 17:17:52 +0000 | [diff] [blame] | 326 | size_t size; |
aliguori | 44e3ee8 | 2009-01-22 16:59:20 +0000 | [diff] [blame] | 327 | } QEMUIOVector; |
| 328 | |
| 329 | void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint); |
aliguori | 522584a | 2009-03-28 17:46:10 +0000 | [diff] [blame] | 330 | void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov); |
aliguori | 44e3ee8 | 2009-01-22 16:59:20 +0000 | [diff] [blame] | 331 | void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len); |
Michael Tokarev | 1b093c4 | 2012-03-12 21:28:06 +0400 | [diff] [blame] | 332 | void qemu_iovec_concat(QEMUIOVector *dst, |
| 333 | QEMUIOVector *src, size_t soffset, size_t sbytes); |
Stefan Hajnoczi | 530c0bb | 2012-11-22 16:06:06 +0100 | [diff] [blame] | 334 | void qemu_iovec_concat_iov(QEMUIOVector *dst, |
| 335 | struct iovec *src_iov, unsigned int src_cnt, |
| 336 | size_t soffset, size_t sbytes); |
aliguori | 44e3ee8 | 2009-01-22 16:59:20 +0000 | [diff] [blame] | 337 | void qemu_iovec_destroy(QEMUIOVector *qiov); |
aliguori | be95946 | 2009-02-05 21:23:54 +0000 | [diff] [blame] | 338 | void qemu_iovec_reset(QEMUIOVector *qiov); |
Michael Tokarev | d5e6b16 | 2012-06-07 20:21:06 +0400 | [diff] [blame] | 339 | size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset, |
| 340 | void *buf, size_t bytes); |
Michael Tokarev | 0339614 | 2012-06-07 20:17:55 +0400 | [diff] [blame] | 341 | size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset, |
| 342 | const void *buf, size_t bytes); |
Michael Tokarev | 3d9b492 | 2012-03-10 16:54:23 +0400 | [diff] [blame] | 343 | size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset, |
| 344 | int fillc, size_t bytes); |
aliguori | 44e3ee8 | 2009-01-22 16:59:20 +0000 | [diff] [blame] | 345 | |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 346 | bool buffer_is_zero(const void *buf, size_t len); |
| 347 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 348 | void qemu_progress_init(int enabled, float min_skip); |
| 349 | void qemu_progress_end(void); |
Jes Sorensen | 3bfe4db | 2011-05-09 17:32:20 +0200 | [diff] [blame] | 350 | void qemu_progress_print(float delta, int max); |
Ronnie Sahlberg | 31459f4 | 2012-08-06 18:24:55 +1000 | [diff] [blame] | 351 | const char *qemu_get_vm_name(void); |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 352 | |
Blue Swirl | 082b555 | 2011-03-27 09:04:57 +0000 | [diff] [blame] | 353 | #define QEMU_FILE_TYPE_BIOS 0 |
| 354 | #define QEMU_FILE_TYPE_KEYMAP 1 |
| 355 | char *qemu_find_file(int type, const char *name); |
| 356 | |
| 357 | /* OS specific functions */ |
| 358 | void os_setup_early_signal_handling(void); |
| 359 | char *os_find_datadir(const char *argv0); |
| 360 | void os_parse_cmd_args(int index, const char *optarg); |
| 361 | void os_pidfile_error(void); |
| 362 | |
Paul Brook | abd0c6b | 2009-11-20 00:03:47 +0000 | [diff] [blame] | 363 | /* Convert a byte between binary and BCD. */ |
| 364 | static inline uint8_t to_bcd(uint8_t val) |
| 365 | { |
| 366 | return ((val / 10) << 4) | (val % 10); |
| 367 | } |
| 368 | |
| 369 | static inline uint8_t from_bcd(uint8_t val) |
| 370 | { |
| 371 | return ((val >> 4) * 10) + (val & 0x0f); |
| 372 | } |
| 373 | |
malc | 338b922 | 2010-10-30 01:41:01 +0400 | [diff] [blame] | 374 | /* compute with 96 bit intermediate result: (a*b)/c */ |
| 375 | static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) |
| 376 | { |
| 377 | union { |
| 378 | uint64_t ll; |
| 379 | struct { |
| 380 | #ifdef HOST_WORDS_BIGENDIAN |
| 381 | uint32_t high, low; |
| 382 | #else |
| 383 | uint32_t low, high; |
| 384 | #endif |
| 385 | } l; |
| 386 | } u, res; |
| 387 | uint64_t rl, rh; |
| 388 | |
| 389 | u.ll = a; |
| 390 | rl = (uint64_t)u.l.low * (uint64_t)b; |
| 391 | rh = (uint64_t)u.l.high * (uint64_t)b; |
| 392 | rh += (rl >> 32); |
| 393 | res.l.high = rh / c; |
| 394 | res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c; |
| 395 | return res.ll; |
| 396 | } |
| 397 | |
Stefan Hajnoczi | 3951690 | 2011-11-17 13:40:25 +0000 | [diff] [blame] | 398 | /* Round number down to multiple */ |
| 399 | #define QEMU_ALIGN_DOWN(n, m) ((n) / (m) * (m)) |
| 400 | |
| 401 | /* Round number up to multiple */ |
| 402 | #define QEMU_ALIGN_UP(n, m) QEMU_ALIGN_DOWN((n) + (m) - 1, (m)) |
| 403 | |
Orit Wasserman | 9fb2664 | 2012-08-06 21:42:50 +0300 | [diff] [blame] | 404 | static inline bool is_power_of_2(uint64_t value) |
| 405 | { |
| 406 | if (!value) { |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | return !(value & (value - 1)); |
| 411 | } |
| 412 | |
| 413 | /* round down to the nearest power of 2*/ |
| 414 | int64_t pow2floor(int64_t value); |
| 415 | |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 416 | #include "qemu/module.h" |
Anthony Liguori | 0bfe3ca | 2009-05-14 19:29:53 +0100 | [diff] [blame] | 417 | |
Orit Wasserman | e6546bb | 2012-08-06 21:42:51 +0300 | [diff] [blame] | 418 | /* |
| 419 | * Implementation of ULEB128 (http://en.wikipedia.org/wiki/LEB128) |
| 420 | * Input is limited to 14-bit numbers |
| 421 | */ |
| 422 | |
| 423 | int uleb128_encode_small(uint8_t *out, uint32_t n); |
| 424 | int uleb128_decode_small(const uint8_t *in, uint32_t *n); |
| 425 | |
pbrook | faf0796 | 2007-11-11 02:51:17 +0000 | [diff] [blame] | 426 | #endif |