]>
Commit | Line | Data |
---|---|---|
03557b9a PM |
1 | /* |
2 | * OS includes and handling of OS dependencies | |
3 | * | |
4 | * This header exists to pull in some common system headers that | |
5 | * most code in QEMU will want, and to fix up some possible issues with | |
6 | * it (missing defines, Windows weirdness, and so on). | |
7 | * | |
8 | * To avoid getting into possible circular include dependencies, this | |
9 | * file should not include any other QEMU headers, with the exceptions | |
10 | * of config-host.h, compiler.h, os-posix.h and os-win32.h, all of which | |
11 | * are doing a similar job to this file and are under similar constraints. | |
12 | * | |
13 | * This header also contains prototypes for functions defined in | |
14 | * os-*.c and util/oslib-*.c; those would probably be better split | |
15 | * out into separate header files. | |
16 | * | |
17 | * In an ideal world this header would contain only: | |
18 | * (1) things which everybody needs | |
19 | * (2) things without which code would work on most platforms but | |
20 | * fail to compile or misbehave on a minority of host OSes | |
21 | * | |
22 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
23 | * See the COPYING file in the top-level directory. | |
24 | */ | |
ea88812f FB |
25 | #ifndef QEMU_OSDEP_H |
26 | #define QEMU_OSDEP_H | |
27 | ||
9adea5f7 | 28 | #include "config-host.h" |
49120868 | 29 | #include "qemu/compiler.h" |
ea88812f | 30 | #include <stdarg.h> |
de5071c5 | 31 | #include <stddef.h> |
0f66998f | 32 | #include <stdbool.h> |
a2b257d6 | 33 | #include <stdint.h> |
128ab2ff | 34 | #include <sys/types.h> |
bfe7e449 PM |
35 | #include <stdlib.h> |
36 | #include <stdio.h> | |
37 | #include <string.h> | |
38 | #include <strings.h> | |
39 | #include <inttypes.h> | |
40 | #include <limits.h> | |
4d9310f4 DB |
41 | /* Put unistd.h before time.h as that triggers localtime_r/gmtime_r |
42 | * function availability on recentish Mingw-w64 platforms. */ | |
43 | #include <unistd.h> | |
bfe7e449 PM |
44 | #include <time.h> |
45 | #include <ctype.h> | |
46 | #include <errno.h> | |
bfe7e449 PM |
47 | #include <fcntl.h> |
48 | #include <sys/stat.h> | |
49 | #include <sys/time.h> | |
50 | #include <assert.h> | |
51 | #include <signal.h> | |
52 | ||
98b2d199 | 53 | #ifdef __OpenBSD__ |
128ab2ff BS |
54 | #include <sys/signal.h> |
55 | #endif | |
ea88812f | 56 | |
13c7b2da PB |
57 | #ifndef _WIN32 |
58 | #include <sys/wait.h> | |
59 | #else | |
60 | #define WIFEXITED(x) 1 | |
61 | #define WEXITSTATUS(x) (x) | |
62 | #endif | |
63 | ||
bfe7e449 PM |
64 | #ifdef _WIN32 |
65 | #include "sysemu/os-win32.h" | |
66 | #endif | |
67 | ||
68 | #ifdef CONFIG_POSIX | |
69 | #include "sysemu/os-posix.h" | |
70 | #endif | |
f7b4a940 | 71 | |
dda3c2ee AF |
72 | #if defined(CONFIG_SOLARIS) && CONFIG_SOLARIS_VERSION < 10 |
73 | /* [u]int_fast*_t not in <sys/int_types.h> */ | |
74 | typedef unsigned char uint_fast8_t; | |
75 | typedef unsigned int uint_fast16_t; | |
94a49d86 | 76 | typedef signed int int_fast16_t; |
dda3c2ee AF |
77 | #endif |
78 | ||
bfe7e449 PM |
79 | #ifndef O_LARGEFILE |
80 | #define O_LARGEFILE 0 | |
81 | #endif | |
82 | #ifndef O_BINARY | |
83 | #define O_BINARY 0 | |
84 | #endif | |
85 | #ifndef MAP_ANONYMOUS | |
86 | #define MAP_ANONYMOUS MAP_ANON | |
87 | #endif | |
88 | #ifndef ENOMEDIUM | |
89 | #define ENOMEDIUM ENODEV | |
90 | #endif | |
91 | #if !defined(ENOTSUP) | |
92 | #define ENOTSUP 4096 | |
93 | #endif | |
94 | #if !defined(ECANCELED) | |
95 | #define ECANCELED 4097 | |
96 | #endif | |
97 | #if !defined(EMEDIUMTYPE) | |
98 | #define EMEDIUMTYPE 4098 | |
99 | #endif | |
100 | #ifndef TIME_MAX | |
101 | #define TIME_MAX LONG_MAX | |
102 | #endif | |
103 | ||
df2542c7 JM |
104 | #ifndef MIN |
105 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) | |
106 | #endif | |
107 | #ifndef MAX | |
108 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) | |
109 | #endif | |
110 | ||
ac3a8726 PL |
111 | /* Minimum function that returns zero only iff both values are zero. |
112 | * Intended for use with unsigned values only. */ | |
113 | #ifndef MIN_NON_ZERO | |
114 | #define MIN_NON_ZERO(a, b) (((a) != 0 && (a) < (b)) ? (a) : (b)) | |
115 | #endif | |
116 | ||
292c8e50 PB |
117 | #ifndef ROUND_UP |
118 | #define ROUND_UP(n,d) (((n) + (d) - 1) & -(d)) | |
119 | #endif | |
120 | ||
e0e53b2f CC |
121 | #ifndef DIV_ROUND_UP |
122 | #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) | |
123 | #endif | |
124 | ||
0954d0d9 BS |
125 | #ifndef ARRAY_SIZE |
126 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) | |
127 | #endif | |
128 | ||
f97742d0 | 129 | int qemu_daemon(int nochdir, int noclose); |
7d2a35cc | 130 | void *qemu_try_memalign(size_t alignment, size_t size); |
33f00271 | 131 | void *qemu_memalign(size_t alignment, size_t size); |
a2b257d6 | 132 | void *qemu_anon_ram_alloc(size_t size, uint64_t *align); |
49b470eb | 133 | void qemu_vfree(void *ptr); |
e7a09b92 | 134 | void qemu_anon_ram_free(void *ptr, size_t size); |
ea88812f | 135 | |
e78815a5 AF |
136 | #define QEMU_MADV_INVALID -1 |
137 | ||
138 | #if defined(CONFIG_MADVISE) | |
139 | ||
140 | #define QEMU_MADV_WILLNEED MADV_WILLNEED | |
141 | #define QEMU_MADV_DONTNEED MADV_DONTNEED | |
142 | #ifdef MADV_DONTFORK | |
143 | #define QEMU_MADV_DONTFORK MADV_DONTFORK | |
144 | #else | |
145 | #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID | |
146 | #endif | |
147 | #ifdef MADV_MERGEABLE | |
148 | #define QEMU_MADV_MERGEABLE MADV_MERGEABLE | |
149 | #else | |
150 | #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID | |
151 | #endif | |
605d0a94 PB |
152 | #ifdef MADV_UNMERGEABLE |
153 | #define QEMU_MADV_UNMERGEABLE MADV_UNMERGEABLE | |
154 | #else | |
155 | #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID | |
156 | #endif | |
157 | #ifdef MADV_DODUMP | |
158 | #define QEMU_MADV_DODUMP MADV_DODUMP | |
159 | #else | |
160 | #define QEMU_MADV_DODUMP QEMU_MADV_INVALID | |
161 | #endif | |
ddb97f1d JB |
162 | #ifdef MADV_DONTDUMP |
163 | #define QEMU_MADV_DONTDUMP MADV_DONTDUMP | |
164 | #else | |
165 | #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID | |
166 | #endif | |
ad0b5321 LC |
167 | #ifdef MADV_HUGEPAGE |
168 | #define QEMU_MADV_HUGEPAGE MADV_HUGEPAGE | |
169 | #else | |
170 | #define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID | |
171 | #endif | |
e78815a5 AF |
172 | |
173 | #elif defined(CONFIG_POSIX_MADVISE) | |
174 | ||
175 | #define QEMU_MADV_WILLNEED POSIX_MADV_WILLNEED | |
176 | #define QEMU_MADV_DONTNEED POSIX_MADV_DONTNEED | |
177 | #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID | |
178 | #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID | |
2925020d MT |
179 | #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID |
180 | #define QEMU_MADV_DODUMP QEMU_MADV_INVALID | |
ddb97f1d | 181 | #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID |
8473f377 | 182 | #define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID |
e78815a5 AF |
183 | |
184 | #else /* no-op */ | |
185 | ||
186 | #define QEMU_MADV_WILLNEED QEMU_MADV_INVALID | |
187 | #define QEMU_MADV_DONTNEED QEMU_MADV_INVALID | |
188 | #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID | |
189 | #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID | |
2925020d MT |
190 | #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID |
191 | #define QEMU_MADV_DODUMP QEMU_MADV_INVALID | |
ddb97f1d | 192 | #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID |
8473f377 | 193 | #define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID |
e78815a5 AF |
194 | |
195 | #endif | |
196 | ||
197 | int qemu_madvise(void *addr, size_t len, int advice); | |
198 | ||
17e0b6ab AF |
199 | int qemu_open(const char *name, int flags, ...); |
200 | int qemu_close(int fd); | |
201 | ||
953ffe0f AF |
202 | #if defined(__HAIKU__) && defined(__i386__) |
203 | #define FMT_pid "%ld" | |
0e0167ba SW |
204 | #elif defined(WIN64) |
205 | #define FMT_pid "%" PRId64 | |
953ffe0f AF |
206 | #else |
207 | #define FMT_pid "%d" | |
208 | #endif | |
209 | ||
aa26bb2d | 210 | int qemu_create_pidfile(const char *filename); |
dc7a09cf | 211 | int qemu_get_thread_id(void); |
aa26bb2d | 212 | |
9adea5f7 PB |
213 | #ifndef CONFIG_IOVEC |
214 | struct iovec { | |
215 | void *iov_base; | |
216 | size_t iov_len; | |
217 | }; | |
218 | /* | |
219 | * Use the same value as Linux for now. | |
220 | */ | |
221 | #define IOV_MAX 1024 | |
222 | ||
223 | ssize_t readv(int fd, const struct iovec *iov, int iov_cnt); | |
224 | ssize_t writev(int fd, const struct iovec *iov, int iov_cnt); | |
225 | #else | |
226 | #include <sys/uio.h> | |
227 | #endif | |
228 | ||
ad620c29 BS |
229 | #ifdef _WIN32 |
230 | static inline void qemu_timersub(const struct timeval *val1, | |
231 | const struct timeval *val2, | |
232 | struct timeval *res) | |
233 | { | |
234 | res->tv_sec = val1->tv_sec - val2->tv_sec; | |
235 | if (val1->tv_usec < val2->tv_usec) { | |
236 | res->tv_sec--; | |
237 | res->tv_usec = val1->tv_usec - val2->tv_usec + 1000 * 1000; | |
238 | } else { | |
239 | res->tv_usec = val1->tv_usec - val2->tv_usec; | |
240 | } | |
241 | } | |
242 | #else | |
243 | #define qemu_timersub timersub | |
244 | #endif | |
245 | ||
49ee3590 AL |
246 | void qemu_set_cloexec(int fd); |
247 | ||
93bfef4c CV |
248 | void qemu_set_version(const char *); |
249 | const char *qemu_get_version(void); | |
250 | ||
0f66998f PM |
251 | void fips_set_state(bool requested); |
252 | bool fips_get_state(void); | |
253 | ||
e2ea3515 LE |
254 | /* Return a dynamically allocated pathname denoting a file or directory that is |
255 | * appropriate for storing local state. | |
256 | * | |
257 | * @relative_pathname need not start with a directory separator; one will be | |
258 | * added automatically. | |
259 | * | |
260 | * The caller is responsible for releasing the value returned with g_free() | |
261 | * after use. | |
262 | */ | |
263 | char *qemu_get_local_state_pathname(const char *relative_pathname); | |
264 | ||
10f5bff6 FZ |
265 | /* Find program directory, and save it for later usage with |
266 | * qemu_get_exec_dir(). | |
267 | * Try OS specific API first, if not working, parse from argv0. */ | |
268 | void qemu_init_exec_dir(const char *argv0); | |
269 | ||
270 | /* Get the saved exec dir. | |
271 | * Caller needs to release the returned string by g_free() */ | |
272 | char *qemu_get_exec_dir(void); | |
273 | ||
b6a3e690 RH |
274 | /** |
275 | * qemu_getauxval: | |
276 | * @type: the auxiliary vector key to lookup | |
277 | * | |
278 | * Search the auxiliary vector for @type, returning the value | |
279 | * or 0 if @type is not present. | |
280 | */ | |
b6a3e690 | 281 | unsigned long qemu_getauxval(unsigned long type); |
b6a3e690 | 282 | |
13401ba0 SH |
283 | void qemu_set_tty_echo(int fd, bool echo); |
284 | ||
38183310 PB |
285 | void os_mem_prealloc(int fd, char *area, size_t sz); |
286 | ||
d57e4e48 DB |
287 | int qemu_read_password(char *buf, int buf_size); |
288 | ||
ea88812f | 289 | #endif |